/
ip.c
628 lines (582 loc) · 15.2 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#undef SIM
/* Upper half of IP, consisting of send/receive primitives, including
* fragment reassembly, for higher level protocols.
* Not needed when running as a standalone gateway.
*/
#include "global.h"
#include "mbuf.h"
#include "timer.h"
#include "internet.h"
#include "netuser.h"
#include "iface.h"
#include "pktdrvr.h"
#include "ip.h"
#include "icmp.h"
static int fraghandle(struct ip *ip,struct mbuf **bpp);
static void ip_timeout(void *arg);
static void free_reasm(struct reasm *rp);
static void freefrag(struct frag *fp);
static struct reasm *lookup_reasm(struct ip *ip);
static struct reasm *creat_reasm(struct ip *ip);
static struct frag *newfrag(uint16 offset,uint16 last,struct mbuf **bpp);
void ttldec(struct iface *ifp);
struct mib_entry Ip_mib[20] = {
"", 0,
"ipForwarding", 1,
"ipDefaultTTL", MAXTTL,
"ipInReceives", 0,
"ipInHdrErrors", 0,
"ipInAddrErrors", 0,
"ipForwDatagrams", 0,
"ipInUnknownProtos", 0,
"ipInDiscards", 0,
"ipInDelivers", 0,
"ipOutRequests", 0,
"ipOutDiscards", 0,
"ipOutNoRoutes", 0,
"ipReasmTimeout", TLB,
"ipReasmReqds", 0,
"ipReasmOKs", 0,
"ipReasmFails", 0,
"ipFragOKs", 0,
"ipFragFails", 0,
"ipFragCreates", 0,
};
struct reasm *Reasmq;
uint16 Id_cntr = 0; /* Datagram serial number */
static struct raw_ip *Raw_ip;
int Ip_trace = 0;
#define INSERT 0
#define APPEND 1
#define PREPEND 2
/* Send an IP datagram. Modeled after the example interface on p 32 of
* RFC 791
*/
int
ip_send(
int32 source, /* source address */
int32 dest, /* Destination address */
char protocol, /* Protocol */
char tos, /* Type of service */
char ttl, /* Time-to-live */
struct mbuf **bpp, /* Data portion of datagram */
uint16 length, /* Optional length of data portion */
uint16 id, /* Optional identification */
char df /* Don't-fragment flag */
){
struct ip ip; /* IP header */
ipOutRequests++;
if(bpp == NULL)
return -1;
if(source == INADDR_ANY)
source = locaddr(dest);
if(length == 0 && *bpp != NULL)
length = len_p(*bpp);
if(id == 0)
id = Id_cntr++;
if(ttl == 0)
ttl = ipDefaultTTL;
/* Fill in IP header */
ip.version = IPVERSION;
ip.tos = tos;
ip.length = IPLEN + length;
ip.id = id;
ip.offset = 0;
ip.flags.mf = 0;
ip.flags.df = df;
ip.flags.congest = 0;
ip.ttl = ttl;
ip.protocol = protocol;
ip.source = source;
ip.dest = dest;
ip.optlen = 0;
if(Ip_trace)
dumpip(NULL,&ip,*bpp,0);
htonip(&ip,bpp,IP_CS_NEW);
if(ismyaddr(ip.dest)){
/* Pretend it has been sent by the loopback interface before
* it appears in the receive queue
*/
#ifdef SIM
net_sim(bpp);
#else
net_route(&Loopback,bpp);
#endif
Loopback.ipsndcnt++;
Loopback.rawsndcnt++;
Loopback.lastsent = secclock();
} else
net_route(NULL,bpp);
return 0;
}
/* Reassemble incoming IP fragments and dispatch completed datagrams
* to the proper transport module
*/
void
ip_recv(
struct iface *iface, /* Incoming interface */
struct ip *ip, /* Extracted IP header */
struct mbuf **bpp, /* Data portion */
int rxbroadcast, /* True if received on subnet broadcast address */
int32 spi /* Security association, if any */
){
/* Function to call with completed datagram */
register struct raw_ip *rp;
struct mbuf *bp1;
int rxcnt = 0;
register struct iplink *ipp;
/* If we have a complete packet, call the next layer
* to handle the result. Note that fraghandle passes back
* a length field that does NOT include the IP header
*/
if(bpp == NULL || fraghandle(ip,bpp) == -1)
return; /* Not done yet */
/* Trim data segment if necessary. */
trim_mbuf(bpp,ip->length - (IPLEN + ip->optlen));
ipInDelivers++;
if(Ip_trace)
dumpip(iface,ip,*bpp,spi);
for(rp = Raw_ip;rp != NULL;rp = rp->next){
if(rp->protocol != ip->protocol)
continue;
rxcnt++;
/* Duplicate the data portion, and put the header back on */
dup_p(&bp1,*bpp,0,len_p(*bpp));
if(bp1 != NULL){
htonip(ip,&bp1,IP_CS_OLD);
enqueue(&rp->rcvq,&bp1);
if(rp->r_upcall != NULL)
(*rp->r_upcall)(rp);
} else {
free_p(&bp1);
}
}
/* Look it up in the transport protocol table */
for(ipp = Iplink;ipp->funct != NULL;ipp++){
if(ipp->proto == ip->protocol)
break;
}
if(ipp->funct != NULL){
/* Found, call transport protocol */
(*ipp->funct)(iface,ip,bpp,rxbroadcast,spi);
} else {
/* Not found */
if(rxcnt == 0){
/* Send an ICMP Protocol Unknown response... */
ipInUnknownProtos++;
/* ...unless it's a broadcast */
if(!rxbroadcast){
icmp_output(ip,*bpp,ICMP_DEST_UNREACH,
ICMP_PROT_UNREACH,NULL);
}
}
free_p(bpp);
}
}
/* Handle IP packets encapsulated inside IP */
void
ipip_recv(
struct iface *iface, /* Incoming interface */
struct ip *ip, /* Extracted IP header */
struct mbuf **bpp, /* Data portion */
int rxbroadcast, /* True if received on subnet broadcast address */
int32 spi
){
net_route(&Encap,bpp);
}
/* Process IP datagram fragments
* If datagram is complete, return its length (MINUS header);
* otherwise return -1
*/
static int
fraghandle(
struct ip *ip, /* IP header, host byte order */
struct mbuf **bpp /* The fragment itself */
){
register struct reasm *rp; /* Pointer to reassembly descriptor */
struct frag *lastfrag,*nextfrag,*tfp;
struct mbuf *tbp;
uint16 i;
uint16 last; /* Index of first byte beyond fragment */
last = ip->offset + ip->length - (IPLEN + ip->optlen);
rp = lookup_reasm(ip);
if(ip->offset == 0 && !ip->flags.mf){
/* Complete datagram received. Discard any earlier fragments */
if(rp != NULL){
free_reasm(rp);
ipReasmOKs++;
}
return ip->length;
}
ipReasmReqds++;
if(rp == NULL){
/* First fragment; create new reassembly descriptor */
if((rp = creat_reasm(ip)) == NULL){
/* No space for descriptor, drop fragment */
ipReasmFails++;
free_p(bpp);
return -1;
}
}
/* Keep restarting timer as long as we keep getting fragments */
stop_timer(&rp->timer);
start_timer(&rp->timer);
/* If this is the last fragment, we now know how long the
* entire datagram is; record it
*/
if(!ip->flags.mf)
rp->length = last;
/* Set nextfrag to the first fragment which begins after us,
* and lastfrag to the last fragment which begins before us
*/
lastfrag = NULL;
for(nextfrag = rp->fraglist;nextfrag != NULL;nextfrag = nextfrag->next){
if(nextfrag->offset > ip->offset)
break;
lastfrag = nextfrag;
}
/* Check for overlap with preceeding fragment */
if(lastfrag != NULL && ip->offset < lastfrag->last){
/* Strip overlap from new fragment */
i = lastfrag->last - ip->offset;
pullup(bpp,NULL,i);
if(*bpp == NULL)
return -1; /* Nothing left */
ip->offset += i;
}
/* Look for overlap with succeeding segments */
for(; nextfrag != NULL; nextfrag = tfp){
tfp = nextfrag->next; /* save in case we delete fp */
if(nextfrag->offset >= last)
break; /* Past our end */
/* Trim the front of this entry; if nothing is
* left, remove it.
*/
i = last - nextfrag->offset;
pullup(&nextfrag->buf,NULL,i);
if(nextfrag->buf == NULL){
/* superseded; delete from list */
if(nextfrag->prev != NULL)
nextfrag->prev->next = nextfrag->next;
else
rp->fraglist = nextfrag->next;
if(tfp->next != NULL)
nextfrag->next->prev = nextfrag->prev;
freefrag(nextfrag);
} else
nextfrag->offset = last;
}
/* Lastfrag now points, as before, to the fragment before us;
* nextfrag points at the next fragment. Check to see if we can
* join to either or both fragments.
*/
i = INSERT;
if(lastfrag != NULL && lastfrag->last == ip->offset)
i |= APPEND;
if(nextfrag != NULL && nextfrag->offset == last)
i |= PREPEND;
switch(i){
case INSERT: /* Insert new desc between lastfrag and nextfrag */
tfp = newfrag(ip->offset,last,bpp);
tfp->prev = lastfrag;
tfp->next = nextfrag;
if(lastfrag != NULL)
lastfrag->next = tfp; /* Middle of list */
else
rp->fraglist = tfp; /* First on list */
if(nextfrag != NULL)
nextfrag->prev = tfp;
break;
case APPEND: /* Append to lastfrag */
append(&lastfrag->buf,bpp);
lastfrag->last = last; /* Extend forward */
break;
case PREPEND: /* Prepend to nextfrag */
tbp = nextfrag->buf;
nextfrag->buf = *bpp;
bpp = NULL;
append(&nextfrag->buf,&tbp);
nextfrag->offset = ip->offset; /* Extend backward */
break;
case (APPEND|PREPEND):
/* Consolidate by appending this fragment and nextfrag
* to lastfrag and removing the nextfrag descriptor
*/
append(&lastfrag->buf,bpp);
append(&lastfrag->buf,&nextfrag->buf);
nextfrag->buf = NULL;
lastfrag->last = nextfrag->last;
/* Finally unlink and delete the now unneeded nextfrag */
lastfrag->next = nextfrag->next;
if(nextfrag->next != NULL)
nextfrag->next->prev = lastfrag;
freefrag(nextfrag);
break;
}
if(rp->fraglist->offset == 0 && rp->fraglist->next == NULL
&& rp->length != 0){
/* We've gotten a complete datagram, so extract it from the
* reassembly buffer and pass it on.
*/
*bpp = rp->fraglist->buf;
rp->fraglist->buf = NULL;
/* Tell IP the entire length */
ip->length = rp->length + (IPLEN + ip->optlen);
free_reasm(rp);
ipReasmOKs++;
ip->offset = 0;
ip->flags.mf = 0;
return ip->length;
} else
return -1;
}
/* Arrange for receipt of raw IP datagrams */
struct raw_ip *
raw_ip(
int protocol,
void (*r_upcall)()
){
register struct raw_ip *rp;
rp = (struct raw_ip *)callocw(1,sizeof(struct raw_ip));
rp->protocol = protocol;
rp->r_upcall = r_upcall;
rp->next = Raw_ip;
Raw_ip = rp;
return rp;
}
/* Free a raw IP descriptor */
void
del_ip(
struct raw_ip *rpp
){
struct raw_ip *rplast = NULL;
register struct raw_ip *rp;
/* Do sanity check on arg */
for(rp = Raw_ip;rp != NULL;rplast=rp,rp = rp->next)
if(rp == rpp)
break;
if(rp == NULL)
return; /* Doesn't exist */
/* Unlink */
if(rplast != NULL)
rplast->next = rp->next;
else
Raw_ip = rp->next;
/* Free resources */
free_q(&rp->rcvq);
free(rp);
}
static struct reasm *
lookup_reasm(
struct ip *ip
){
register struct reasm *rp;
struct reasm *rplast = NULL;
for(rp = Reasmq;rp != NULL;rplast=rp,rp = rp->next){
if(ip->id == rp->id && ip->source == rp->source
&& ip->dest == rp->dest && ip->protocol == rp->protocol){
if(rplast != NULL){
/* Move to top of list for speed */
rplast->next = rp->next;
rp->next = Reasmq;
Reasmq = rp;
}
return rp;
}
}
return NULL;
}
/* Create a reassembly descriptor,
* put at head of reassembly list
*/
static struct reasm *
creat_reasm(
struct ip *ip
){
register struct reasm *rp;
if((rp = (struct reasm *)calloc(1,sizeof(struct reasm))) == NULL)
return rp; /* No space for descriptor */
rp->source = ip->source;
rp->dest = ip->dest;
rp->id = ip->id;
rp->protocol = ip->protocol;
set_timer(&rp->timer,ipReasmTimeout * 1000L);
rp->timer.func = ip_timeout;
rp->timer.arg = rp;
rp->next = Reasmq;
Reasmq = rp;
return rp;
}
/* Free all resources associated with a reassembly descriptor */
static void
free_reasm(
struct reasm *r
){
register struct reasm *rp;
struct reasm *rplast = NULL;
register struct frag *fp;
for(rp = Reasmq;rp != NULL;rplast = rp,rp=rp->next)
if(r == rp)
break;
if(rp == NULL)
return; /* Not on list */
stop_timer(&rp->timer);
/* Remove from list of reassembly descriptors */
if(rplast != NULL)
rplast->next = rp->next;
else
Reasmq = rp->next;
/* Free any fragments on list, starting at beginning */
while((fp = rp->fraglist) != NULL){
rp->fraglist = fp->next;
free_p(&fp->buf);
free(fp);
}
free(rp);
}
/* Handle reassembly timeouts by deleting all reassembly resources */
static void
ip_timeout(
void *arg
){
free_reasm((struct reasm *)arg);
ipReasmFails++;
}
/* Create a fragment */
static struct frag *
newfrag(
uint16 offset,
uint16 last,
struct mbuf **bpp
){
struct frag *fp;
if((fp = (struct frag *)calloc(1,sizeof(struct frag))) == NULL){
/* Drop fragment */
free_p(bpp);
return NULL;
}
fp->buf = *bpp;
*bpp = NULL;
fp->offset = offset;
fp->last = last;
return fp;
}
/* Delete a fragment, return next one on queue */
static void
freefrag(
struct frag *fp
){
free_p(&fp->buf);
free(fp);
}
/* In red alert mode, blow away the whole reassembly queue. Otherwise crunch
* each fragment on each reassembly descriptor
*/
void
ip_garbage(
int red
){
struct reasm *rp,*rp1;
struct frag *fp;
struct raw_ip *rwp;
struct iface *ifp;
/* Run through the reassembly queue */
for(rp = Reasmq;rp != NULL;rp = rp1){
rp1 = rp->next;
if(red){
free_reasm(rp);
} else {
for(fp = rp->fraglist;fp != NULL;fp = fp->next){
mbuf_crunch(&fp->buf);
}
}
}
/* Run through the raw IP queue */
for(rwp = Raw_ip;rwp != NULL;rwp = rwp->next)
mbuf_crunch(&rwp->rcvq);
/* Walk through interface output queues and decrement IP TTLs.
* Discard and return ICMP TTL exceeded messages for any that
* go to zero. (Some argue that this ought to be done all the
* time, but it would probably break a lot of machines with
* small IP TTL settings using amateur packet radio paths.)
*
* Also send an ICMP source quench message to one
* randomly chosen packet on each queue. If in red mode,
* also drop the packet.
*/
for(ifp=Ifaces;ifp != NULL;ifp = ifp->next){
ttldec(ifp);
rquench(ifp,red);
}
}
/* Decrement the IP TTL field in each packet on the send queue. If
* a TTL goes to zero, discard the packet.
*/
void
ttldec(
struct iface *ifp
){
struct mbuf *bp,*bpprev,*bpnext;
struct qhdr qhdr;
struct ip ip;
bpprev = NULL;
for(bp = ifp->outq; bp != NULL;bpprev = bp,bp = bpnext){
bpnext = bp->anext;
pullup(&bp,&qhdr,sizeof(qhdr));
ntohip(&ip,&bp);
if(--ip.ttl == 0){
/* Drop packet */
icmp_output(&ip,bp,ICMP_TIME_EXCEED,0,NULL);
if(bpprev == NULL) /* First on queue */
ifp->outq = bpnext;
else
bpprev->anext = bpnext;
free_p(&bp);
bp = bpprev;
continue;
}
/* Put IP and queue headers back, restore to queue */
htonip(&ip,&bp,0);
pushdown(&bp,&qhdr,sizeof(qhdr));
if(bpprev == NULL) /* First on queue */
ifp->outq = bp;
else
bpprev->anext = bp;
bp->anext = bpnext;
}
}
/* Execute random quench algorithm on an interface's output queue */
void
rquench(
struct iface *ifp,
int drop
){
struct mbuf *bp,*bplast;
int i;
struct qhdr qhdr;
struct ip ip;
struct mbuf *bpdup;
if((i = len_q(ifp->outq)) == 0)
return; /* Queue is empty */
i = urandom(i); /* Select a victim */
/* Search for i-th message on queue */
bplast = NULL;
for(bp = ifp->outq;bp != NULL && i>0;i--,bplast=bp,bp=bp->anext)
;
if(bp == NULL)
return; /* "Can't happen" */
/* Send a source quench */
dup_p(&bpdup,bp,0,len_p(bp));
pullup(&bpdup,&qhdr,sizeof(qhdr));
ntohip(&ip,&bpdup);
icmp_output(&ip,bpdup,ICMP_QUENCH,0,NULL);
free_p(&bpdup);
if(!drop)
return; /* All done */
/* Drop the packet */
if(bplast != NULL)
bplast->anext = bp->anext;
else
ifp->outq = bp->anext; /* First on list */
free_p(&bp);
}