-
Notifications
You must be signed in to change notification settings - Fork 387
/
ip.c
496 lines (450 loc) · 13.7 KB
/
ip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "platform.h"
#include "util.h"
#include "net.h"
#include "arp.h"
#include "ip.h"
struct ip_protocol {
struct ip_protocol *next;
char name[16];
uint8_t type;
void (*handler)(const uint8_t *data, size_t len, ip_addr_t src, ip_addr_t dst, struct ip_iface *iface);
};
struct ip_route {
struct ip_route *next;
ip_addr_t network;
ip_addr_t netmask;
ip_addr_t nexthop;
struct ip_iface *iface;
};
struct ip_hdr {
uint8_t vhl;
uint8_t tos;
uint16_t total;
uint16_t id;
uint16_t offset;
uint8_t ttl;
uint8_t protocol;
uint16_t sum;
ip_addr_t src;
ip_addr_t dst;
uint8_t options[0];
};
const ip_addr_t IP_ADDR_ANY = 0x00000000; /* 0.0.0.0 */
const ip_addr_t IP_ADDR_BROADCAST = 0xffffffff; /* 255.255.255.255 */
/* NOTE: if you want to add/delete the entries after net_run(), you need to protect these lists with a mutex. */
static struct ip_iface *ifaces;
static struct ip_protocol *protocols;
static struct ip_route *routes;
int
ip_addr_pton(const char *p, ip_addr_t *n)
{
char *sp, *ep;
int idx;
long ret;
sp = (char *)p;
for (idx = 0; idx < 4; idx++) {
ret = strtol(sp, &ep, 10);
if (ret < 0 || ret > 255) {
return -1;
}
if (ep == sp) {
return -1;
}
if ((idx == 3 && *ep != '\0') || (idx != 3 && *ep != '.')) {
return -1;
}
((uint8_t *)n)[idx] = ret;
sp = ep + 1;
}
return 0;
}
char *
ip_addr_ntop(const ip_addr_t n, char *p, size_t size)
{
uint8_t *u8;
u8 = (uint8_t *)&n;
snprintf(p, size, "%d.%d.%d.%d", u8[0], u8[1], u8[2], u8[3]);
return p;
}
int
ip_endpoint_pton(const char *p, struct ip_endpoint *n)
{
char *sep;
char addr[IP_ADDR_STR_LEN] = {};
long int port;
sep = strrchr(p, ':');
if (!sep) {
return -1;
}
memcpy(addr, p, sep - p);
if (ip_addr_pton(addr, &n->addr) == -1) {
return -1;
}
port = strtol(sep+1, NULL, 10);
if (port <= 0 || port > UINT16_MAX) {
return -1;
}
n->port = hton16(port);
return 0;
}
char *
ip_endpoint_ntop(const struct ip_endpoint *n, char *p, size_t size)
{
size_t offset;
ip_addr_ntop(n->addr, p, size);
offset = strlen(p);
snprintf(p + offset, size - offset, ":%d", ntoh16(n->port));
return p;
}
void
ip_dump(const uint8_t *data, size_t len)
{
struct ip_hdr *hdr;
uint8_t v, hl, hlen;
uint16_t total, offset;
char addr[IP_ADDR_STR_LEN];
flockfile(stderr);
hdr = (struct ip_hdr *)data;
v = (hdr->vhl & 0xf0) >> 4;
hl = hdr->vhl & 0x0f;
hlen = hl << 2;
fprintf(stderr, " vhl: 0x%02x [v: %u, hl: %u (%u)]\n", hdr->vhl, v, hl, hlen);
fprintf(stderr, " tos: 0x%02x\n", hdr->tos);
total = ntoh16(hdr->total);
fprintf(stderr, " total: %u (payload: %u)\n", total, total - hlen);
fprintf(stderr, " id: %u\n", ntoh16(hdr->id));
offset = ntoh16(hdr->offset);
fprintf(stderr, " offset: 0x%04x [flags=%x, offset=%u]\n", offset, (offset & 0xe000) >> 13, offset & 0x1fff);
fprintf(stderr, " ttl: %u\n", hdr->ttl);
fprintf(stderr, " protocol: %u (%s)\n", hdr->protocol, ip_protocol_name(hdr->protocol));
fprintf(stderr, " sum: 0x%04x (0x%04x)\n", ntoh16(hdr->sum), ntoh16(cksum16((uint16_t *)data, hlen, -hdr->sum)));
fprintf(stderr, " src: %s\n", ip_addr_ntop(hdr->src, addr, sizeof(addr)));
fprintf(stderr, " dst: %s\n", ip_addr_ntop(hdr->dst, addr, sizeof(addr)));
#ifdef HEXDUMP
hexdump(stderr, data, len);
#endif
funlockfile(stderr);
}
/* NOTE: must not be call after net_run() */
static struct ip_route *
ip_route_add(ip_addr_t network, ip_addr_t netmask, ip_addr_t nexthop, struct ip_iface *iface)
{
struct ip_route *route;
char addr1[IP_ADDR_STR_LEN];
char addr2[IP_ADDR_STR_LEN];
char addr3[IP_ADDR_STR_LEN];
char addr4[IP_ADDR_STR_LEN];
route = memory_alloc(sizeof(*route));
if (!route) {
errorf("memory_alloc() failure");
return NULL;
}
route->network = network;
route->netmask = netmask;
route->nexthop = nexthop;
route->iface = iface;
route->next = routes;
routes = route;
infof("network=%s, netmask=%s, nexthop=%s, iface=%s dev=%s",
ip_addr_ntop(route->network, addr1, sizeof(addr1)),
ip_addr_ntop(route->netmask, addr2, sizeof(addr2)),
ip_addr_ntop(route->nexthop, addr3, sizeof(addr3)),
ip_addr_ntop(route->iface->unicast, addr4, sizeof(addr4)),
NET_IFACE(iface)->dev->name
);
return route;
}
static struct ip_route *
ip_route_lookup(ip_addr_t dst)
{
struct ip_route *route, *candidate = NULL;
for (route = routes; route; route = route->next) {
if ((dst & route->netmask) == route->network) {
if (!candidate || ntoh32(candidate->netmask) < ntoh32(route->netmask)) {
candidate = route;
}
}
}
return candidate;
}
/* NOTE: must not be call after net_run() */
int
ip_route_set_default_gateway(struct ip_iface *iface, const char *gateway)
{
ip_addr_t gw;
if (ip_addr_pton(gateway, &gw) == -1) {
errorf("ip_addr_pton() failure, addr=%s", gateway);
return -1;
}
if (!ip_route_add(IP_ADDR_ANY, IP_ADDR_ANY, gw, iface)) {
errorf("ip_route_add() failure");
return -1;
}
return 0;
}
struct ip_iface *
ip_route_get_iface(ip_addr_t dst)
{
struct ip_route *route;
route = ip_route_lookup(dst);
if (!route) {
return NULL;
}
return route->iface;
}
struct ip_iface *
ip_iface_alloc(const char *unicast, const char *netmask)
{
struct ip_iface *iface;
iface = memory_alloc(sizeof(*iface));
if (!iface) {
errorf("memory_alloc() failure");
return NULL;
}
NET_IFACE(iface)->family = NET_IFACE_FAMILY_IP;
if (ip_addr_pton(unicast, &iface->unicast) == -1) {
errorf("ip_addr_pton() failure, addr=%s", unicast);
memory_free(iface);
return NULL;
}
if (ip_addr_pton(netmask, &iface->netmask) == -1) {
errorf("ip_addr_pton() failure, addr=%s", netmask);
memory_free(iface);
return NULL;
}
iface->broadcast = (iface->unicast & iface->netmask) | ~iface->netmask;
return iface;
}
/* NOTE: must not be call after net_run() */
int
ip_iface_register(struct net_device *dev, struct ip_iface *iface)
{
char addr1[IP_ADDR_STR_LEN];
char addr2[IP_ADDR_STR_LEN];
char addr3[IP_ADDR_STR_LEN];
if (net_device_add_iface(dev, NET_IFACE(iface)) == -1) {
errorf("net_device_add_iface() failure");
return -1;
}
if (!ip_route_add(iface->unicast & iface->netmask, iface->netmask, IP_ADDR_ANY, iface)) {
errorf("ip_route_add() failure");
return -1;
}
iface->next = ifaces;
ifaces = iface;
infof("registered: dev=%s, unicast=%s, netmask=%s, broadcast=%s",
dev->name,
ip_addr_ntop(iface->unicast, addr1, sizeof(addr1)),
ip_addr_ntop(iface->netmask, addr2, sizeof(addr2)),
ip_addr_ntop(iface->broadcast, addr3, sizeof(addr3)));
return 0;
}
struct ip_iface *
ip_iface_select(ip_addr_t addr)
{
struct ip_iface *entry;
for (entry = ifaces; entry; entry = entry->next) {
if (entry->unicast == addr) {
break;
}
}
return entry;
}
static void
ip_input(const uint8_t *data, size_t len, struct net_device *dev)
{
struct ip_hdr *hdr;
uint8_t v;
uint16_t hlen, total, offset;
struct ip_iface *iface;
char addr[IP_ADDR_STR_LEN];
struct ip_protocol *proto;
if (len < IP_HDR_SIZE_MIN) {
errorf("too short");
return;
}
hdr = (struct ip_hdr *)data;
v = hdr->vhl >> 4;
if (v != IP_VERSION_IPV4) {
errorf("ip version error: v=%u", v);
return;
}
hlen = (hdr->vhl & 0x0f) << 2;
if (len < hlen) {
errorf("header length error: hlen=%u, len=%zu", hlen, len);
return;
}
total = ntoh16(hdr->total);
if (len < total) {
errorf("total length error: total=%u, len=%zu", total, len);
return;
}
if (cksum16((uint16_t *)hdr, hlen, 0) != 0) {
errorf("checksum error: sum=0x%04x, verify=0x%04x", ntoh16(hdr->sum), ntoh16(cksum16((uint16_t *)hdr, hlen, -hdr->sum)));
return;
}
offset = ntoh16(hdr->offset);
if (offset & 0x2000 || offset & 0x1fff) {
errorf("fragments does not support");
return;
}
iface = (struct ip_iface *)net_device_get_iface(dev, NET_IFACE_FAMILY_IP);
if (!iface) {
/* iface is not registered to the device */
return;
}
if (hdr->dst != iface->unicast) {
if (hdr->dst != iface->broadcast && hdr->dst != IP_ADDR_BROADCAST) {
/* for other host */
return;
}
}
debugf("dev=%s, iface=%s, protocol=%s(0x%02x), len=%u",
dev->name, ip_addr_ntop(iface->unicast, addr, sizeof(addr)), ip_protocol_name(hdr->protocol), hdr->protocol, total);
ip_dump(data, total);
for (proto = protocols; proto; proto = proto->next) {
if (proto->type == hdr->protocol) {
proto->handler((uint8_t *)hdr + hlen, total - hlen, hdr->src, hdr->dst, iface);
return;
}
}
/* unsupported protocol */
}
static int
ip_output_device(struct ip_iface *iface, const uint8_t *data, size_t len, ip_addr_t dst)
{
uint8_t hwaddr[NET_DEVICE_ADDR_LEN] = {};
int ret;
if (NET_IFACE(iface)->dev->flags & NET_DEVICE_FLAG_NEED_ARP) {
if (dst == iface->broadcast || dst == IP_ADDR_BROADCAST) {
memcpy(hwaddr, NET_IFACE(iface)->dev->broadcast, NET_IFACE(iface)->dev->alen);
} else {
ret = arp_resolve(NET_IFACE(iface), dst, hwaddr);
if (ret != ARP_RESOLVE_FOUND) {
return ret;
}
}
}
return net_device_output(NET_IFACE(iface)->dev, NET_PROTOCOL_TYPE_IP, data, len, hwaddr);
}
static ssize_t
ip_output_core(struct ip_iface *iface, uint8_t protocol, const uint8_t *data, size_t len, ip_addr_t src, ip_addr_t dst, ip_addr_t nexthop, uint16_t id, uint16_t offset)
{
uint8_t buf[IP_TOTAL_SIZE_MAX];
struct ip_hdr *hdr;
uint16_t hlen, total;
char addr[IP_ADDR_STR_LEN];
hdr = (struct ip_hdr *)buf;
hlen = sizeof(*hdr);
hdr->vhl = (IP_VERSION_IPV4 << 4) | (hlen >> 2);
hdr->tos = 0;
total = hlen + len;
hdr->total = hton16(total);
hdr->id = hton16(id);
hdr->offset = hton16(offset);
hdr->ttl = 0xff;
hdr->protocol = protocol;
hdr->sum = 0;
hdr->src = src;
hdr->dst = dst;
hdr->sum = cksum16((uint16_t *)hdr, hlen, 0); /* don't convert byteorder */
memcpy(hdr+1, data, len);
debugf("dev=%s, iface=%s, protocol=%s(0x%02x), len=%u",
NET_IFACE(iface)->dev->name, ip_addr_ntop(iface->unicast, addr, sizeof(addr)), ip_protocol_name(protocol), protocol, total);
ip_dump(buf, total);
return ip_output_device(iface, buf, total, nexthop);
}
static uint16_t
ip_generate_id(void)
{
static mutex_t mutex = MUTEX_INITIALIZER;
static uint16_t id = 128;
uint16_t ret;
mutex_lock(&mutex);
ret = id++;
mutex_unlock(&mutex);
return ret;
}
ssize_t
ip_output(uint8_t protocol, const uint8_t *data, size_t len, ip_addr_t src, ip_addr_t dst)
{
struct ip_route *route;
struct ip_iface *iface;
char addr[IP_ADDR_STR_LEN];
ip_addr_t nexthop;
uint16_t id;
if (src == IP_ADDR_ANY && dst == IP_ADDR_BROADCAST) {
errorf("source address is required for broadcast addresses");
return -1;
}
route = ip_route_lookup(dst);
if (!route) {
errorf("no route to host, addr=%s", ip_addr_ntop(dst, addr, sizeof(addr)));
return -1;
}
iface = route->iface;
if (src != IP_ADDR_ANY && src != iface->unicast) {
errorf("unable to output with specified source address, addr=%s", ip_addr_ntop(src, addr, sizeof(addr)));
return -1;
}
nexthop = (route->nexthop != IP_ADDR_ANY) ? route->nexthop : dst;
if (NET_IFACE(iface)->dev->mtu < IP_HDR_SIZE_MIN + len) {
errorf("too long, dev=%s, mtu=%u, tatal=%zu",
NET_IFACE(iface)->dev->name, NET_IFACE(iface)->dev->mtu, IP_HDR_SIZE_MIN + len);
return -1;
}
id = ip_generate_id();
if (ip_output_core(iface, protocol, data, len, iface->unicast, dst, nexthop, id, 0) == -1) {
errorf("ip_output_core() failure");
return -1;
}
return len;
}
/* NOTE: must not be call after net_run() */
int
ip_protocol_register(const char *name, uint8_t type, void (*handler)(const uint8_t *data, size_t len, ip_addr_t src, ip_addr_t dst, struct ip_iface *iface))
{
struct ip_protocol *entry;
for (entry = protocols; entry; entry = entry->next) {
if (entry->type == type) {
errorf("already exists, type=%s(0x%02x), exist=%s(0x%02x)", name, type, entry->name, entry->type);
return -1;
}
}
entry = memory_alloc(sizeof(*entry));
if (!entry) {
errorf("memory_alloc() failure");
return -1;
}
strncpy(entry->name, name, sizeof(entry->name)-1);
entry->type = type;
entry->handler = handler;
entry->next = protocols;
protocols = entry;
infof("registered, type=%s(0x%02x)", entry->name, entry->type);
return 0;
}
char *
ip_protocol_name(uint8_t type)
{
struct ip_protocol *entry;
for (entry = protocols; entry; entry = entry->next) {
if (entry->type == type) {
return entry->name;
}
}
return "UNKNOWN";
}
int
ip_init(void)
{
if (net_protocol_register("IP", NET_PROTOCOL_TYPE_IP, ip_input) == -1) {
errorf("net_protocol_register() failure");
return -1;
}
return 0;
}