aef3e3dc0a0d39fd66d16bc921e70b2d2bdc4059
[akaros.git] / kern / src / net / ip.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 #include <vfs.h>
17 #include <kfs.h>
18 #include <slab.h>
19 #include <kmalloc.h>
20 #include <kref.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include <error.h>
25 #include <cpio.h>
26 #include <pmap.h>
27 #include <smp.h>
28 #include <ip.h>
29
30 typedef struct Ip4hdr           Ip4hdr;
31 typedef struct IP               IP;
32 typedef struct Fragment4        Fragment4;
33 typedef struct Fragment6        Fragment6;
34 typedef struct Ipfrag           Ipfrag;
35
36 enum
37 {
38         IP4HDR          = 20,           /* sizeof(Ip4hdr) */
39         IP6HDR          = 40,           /* sizeof(Ip6hdr) */
40         IP_HLEN4        = 0x05,         /* Header length in words */
41         IP_DF           = 0x4000,       /* Don't fragment */
42         IP_MF           = 0x2000,       /* More fragments */
43         IP6FHDR         = 8,            /* sizeof(Fraghdr6) */
44         IP_MAX          = 64*1024,      /* Maximum Internet packet size */
45 };
46
47 #define BLKIPVER(xp)    (((struct Ip4hdr*)((xp)->rp))->vihl&0xF0)
48 #define NEXT_ID(x) (__sync_add_and_fetch(&(x), 1))
49
50 struct Ip4hdr
51 {
52         uint8_t vihl;           /* Version and header length */
53         uint8_t tos;            /* Type of service */
54         uint8_t length[2];      /* packet length */
55         uint8_t id[2];          /* ip->identification */
56         uint8_t frag[2];        /* Fragment information */
57         uint8_t ttl;            /* Time to live */
58         uint8_t proto;          /* Protocol */
59         uint8_t cksum[2];       /* Header checksum */
60         uint8_t src[4];         /* IP source */
61         uint8_t dst[4];         /* IP destination */
62 };
63
64 /* MIB II counters */
65 enum
66 {
67         Forwarding,
68         DefaultTTL,
69         InReceives,
70         InHdrErrors,
71         InAddrErrors,
72         ForwDatagrams,
73         InUnknownProtos,
74         InDiscards,
75         InDelivers,
76         OutRequests,
77         OutDiscards,
78         OutNoRoutes,
79         ReasmTimeout,
80         ReasmReqds,
81         ReasmOKs,
82         ReasmFails,
83         FragOKs,
84         FragFails,
85         FragCreates,
86
87         Nstats,
88 };
89
90 struct fragment4
91 {
92         struct block*   blist;
93         struct fragment4*       next;
94         uint32_t        src;
95         uint32_t        dst;
96         uint16_t        id;
97         uint32_t        age;
98 };
99
100 struct fragment6
101 {
102         struct block*   blist;
103         struct fragment6*       next;
104         uint8_t         src[IPaddrlen];
105         uint8_t         dst[IPaddrlen];
106         unsigned int    id;
107         uint32_t        age;
108 };
109
110 struct Ipfrag
111 {
112         uint16_t        foff;
113         uint16_t        flen;
114 };
115
116 /* an instance of IP */
117 struct IP
118 {
119         uint32_t                stats[Nstats];
120
121         qlock_t         fraglock4;
122         struct fragment4*       flisthead4;
123         struct fragment4*       fragfree4;
124         int             id4;
125
126         qlock_t         fraglock6;
127         struct fragment6*       flisthead6;
128         struct fragment6*       fragfree6;
129         int             id6;
130
131         int             iprouting;      /* true if we route like a gateway */
132 };
133
134 static char *statnames[] =
135 {
136 [Forwarding]    "Forwarding",
137 [DefaultTTL]    "DefaultTTL",
138 [InReceives]    "InReceives",
139 [InHdrErrors]   "InHdrErrors",
140 [InAddrErrors]  "InAddrErrors",
141 [ForwDatagrams] "ForwDatagrams",
142 [InUnknownProtos]       "InUnknownProtos",
143 [InDiscards]    "InDiscards",
144 [InDelivers]    "InDelivers",
145 [OutRequests]   "OutRequests",
146 [OutDiscards]   "OutDiscards",
147 [OutNoRoutes]   "OutNoRoutes",
148 [ReasmTimeout]  "ReasmTimeout",
149 [ReasmReqds]    "ReasmReqds",
150 [ReasmOKs]      "ReasmOKs",
151 [ReasmFails]    "ReasmFails",
152 [FragOKs]       "FragOKs",
153 [FragFails]     "FragFails",
154 [FragCreates]   "FragCreates",
155 };
156
157 #define BLKIP(xp)       ((struct Ip4hdr*)((xp)->rp))
158 /*
159  * This sleazy macro relies on the media header size being
160  * larger than sizeof(Ipfrag). ipreassemble checks this is true
161  */
162 #define BKFG(xp)        ((struct Ipfrag*)((xp)->base))
163
164 uint16_t                ipcsum( uint8_t *unused_uint8_p_t);
165 struct block*           ip4reassemble(struct IP*, int unused_int,
166                                            struct block*, struct Ip4hdr*);
167 void            ipfragfree4(struct IP*, struct fragment4*);
168 struct fragment4*       ipfragallo4(struct IP*);
169
170
171 void
172 ip_init_6(struct Fs *f)
173 {
174         struct V6params *v6p;
175
176         v6p = kzmalloc(sizeof(struct V6params), 0);
177         
178         v6p->rp.mflag           = 0;            // default not managed
179         v6p->rp.oflag           = 0;
180         v6p->rp.maxraint        = 600000;       // millisecs
181         v6p->rp.minraint        = 200000;
182         v6p->rp.linkmtu         = 0;            // no mtu sent
183         v6p->rp.reachtime       = 0;
184         v6p->rp.rxmitra         = 0;
185         v6p->rp.ttl             = MAXTTL;
186         v6p->rp.routerlt        = 3*(v6p->rp.maxraint); 
187
188         v6p->hp.rxmithost       = 1000;         // v6 RETRANS_TIMER
189
190         v6p->cdrouter           = -1;
191
192         f->v6p                  = v6p;
193
194 }
195
196 void
197 initfrag(struct IP *ip, int size)
198 {
199         struct fragment4 *fq4, *eq4;
200         struct fragment6 *fq6, *eq6;
201
202         ip->fragfree4 = (struct fragment4*)kzmalloc(sizeof(struct fragment4) * size, 0);
203         if(ip->fragfree4 == NULL)
204                 panic("initfrag");
205
206         eq4 = &ip->fragfree4[size];
207         for(fq4 = ip->fragfree4; fq4 < eq4; fq4++)
208                 fq4->next = fq4+1;
209
210         ip->fragfree4[size-1].next = NULL;
211
212         ip->fragfree6 = (struct fragment6*)kzmalloc(sizeof(struct fragment6) * size, 0);
213         if(ip->fragfree6 == NULL)
214                 panic("initfrag");
215
216         eq6 = &ip->fragfree6[size];
217         for(fq6 = ip->fragfree6; fq6 < eq6; fq6++)
218                 fq6->next = fq6+1;
219
220         ip->fragfree6[size-1].next = NULL;
221 }
222
223 void
224 ip_init(struct Fs *f)
225 {
226         struct IP *ip;
227
228         ip = kzmalloc(sizeof(struct IP), 0);
229         initfrag(ip, 100);
230         f->ip = ip;
231
232         ip_init_6(f);
233 }
234
235 void
236 iprouting(struct Fs *f, int on)
237 {
238         f->ip->iprouting = on;
239         if(f->ip->iprouting==0)
240                 f->ip->stats[Forwarding] = 2;
241         else
242                 f->ip->stats[Forwarding] = 1;   
243 }
244
245 int
246 ipoput4(struct Fs *f,
247         struct block *bp, int gating, int ttl, int tos, struct conv *c)
248 {
249         ERRSTACK(1);
250         struct Ipifc *ifc;
251         uint8_t *gate;
252         uint32_t fragoff;
253         struct block *xp, *nb;
254         struct Ip4hdr *eh, *feh;
255         int lid, len, seglen, chunk, dlen, blklen, offset, medialen;
256         struct route *r, *sr;
257         struct IP *ip;
258         int rv = 0;
259
260         ip = f->ip;
261
262         /* Fill out the ip header */
263         eh = (struct Ip4hdr*)(bp->rp);
264
265         ip->stats[OutRequests]++;
266
267         /* Number of uint8_ts in data and ip header to write */
268         len = blocklen(bp);
269
270         if(gating){
271                 chunk = nhgets(eh->length);
272                 if(chunk > len){
273                         ip->stats[OutDiscards]++;
274                         netlog(f, Logip, "short gated packet\n");
275                         goto free;
276                 }
277                 if(chunk < len)
278                         len = chunk;
279         }
280         if(len >= IP_MAX){
281                 ip->stats[OutDiscards]++;
282                 netlog(f, Logip, "exceeded ip max size %V\n", eh->dst);
283                 goto free;
284         }
285
286         r = v4lookup(f, eh->dst, c);
287         if(r == NULL){
288                 ip->stats[OutNoRoutes]++;
289                 netlog(f, Logip, "no interface %V\n", eh->dst);
290                 rv = -1;
291                 goto free;
292         }
293
294         ifc = r->rt.ifc;
295         if(r->rt.type & (Rifc|Runi))
296                 gate = eh->dst;
297         else
298         if(r->rt.type & (Rbcast|Rmulti)) {
299                 gate = eh->dst;
300                 sr = v4lookup(f, eh->src, NULL);
301                 if(sr != NULL && (sr->rt.type & Runi))
302                         ifc = sr->rt.ifc;
303         }
304         else
305                 gate = r->v4.gate;
306
307         if(!gating)
308                 eh->vihl = IP_VER4|IP_HLEN4;
309         eh->ttl = ttl;
310         if(!gating)
311                 eh->tos = tos;
312
313         if(!canrlock(&ifc->rwlock))
314                 goto free;
315         if(waserror()){
316                 runlock(&ifc->rwlock);
317                 nexterror();
318         }
319         if(ifc->m == NULL)
320                 goto raise;
321
322         /* If we dont need to fragment just send it */
323         medialen = ifc->maxtu - ifc->m->hsize;
324         if(len <= medialen) {
325                 if(!gating)
326                         hnputs(eh->id, NEXT_ID(ip->id4));
327                 hnputs(eh->length, len);
328                 if(!gating){
329                         eh->frag[0] = 0;
330                         eh->frag[1] = 0;
331                 }
332                 eh->cksum[0] = 0;
333                 eh->cksum[1] = 0;
334                 hnputs(eh->cksum, ipcsum(&eh->vihl));
335                 ifc->m->bwrite(ifc, bp, V4, gate);
336                 runlock(&ifc->rwlock);
337                 poperror();
338                 return 0;
339         }
340
341 if((eh->frag[0] & (IP_DF>>8)) && !gating) printd("%V: DF set\n", eh->dst);
342
343         if(eh->frag[0] & (IP_DF>>8)){
344                 ip->stats[FragFails]++;
345                 ip->stats[OutDiscards]++;
346                 icmpcantfrag(f, bp, medialen);
347                 netlog(f, Logip, "%V: eh->frag[0] & (IP_DF>>8)\n", eh->dst);
348                 goto raise;
349         }
350
351         seglen = (medialen - IP4HDR) & ~7;
352         if(seglen < 8){
353                 ip->stats[FragFails]++;
354                 ip->stats[OutDiscards]++;
355                 netlog(f, Logip, "%V seglen < 8\n", eh->dst);
356                 goto raise;
357         }
358
359         dlen = len - IP4HDR;
360         xp = bp;
361         if(gating)
362                 lid = nhgets(eh->id);
363         else
364                 lid = NEXT_ID(ip->id4);
365
366         offset = IP4HDR;
367         while(xp != NULL && offset && offset >= BLEN(xp)) {
368                 offset -= BLEN(xp);
369                 xp = xp->next;
370         }
371         xp->rp += offset;
372
373         if(gating)
374                 fragoff = nhgets(eh->frag)<<3;
375         else
376                 fragoff = 0;
377         dlen += fragoff;
378         for(; fragoff < dlen; fragoff += seglen) {
379                 nb = allocb(IP4HDR+seglen);
380                 feh = (struct Ip4hdr*)(nb->rp);
381
382                 memmove(nb->wp, eh, IP4HDR);
383                 nb->wp += IP4HDR;
384
385                 if((fragoff + seglen) >= dlen) {
386                         seglen = dlen - fragoff;
387                         hnputs(feh->frag, fragoff>>3);
388                 }
389                 else    
390                         hnputs(feh->frag, (fragoff>>3)|IP_MF);
391
392                 hnputs(feh->length, seglen + IP4HDR);
393                 hnputs(feh->id, lid);
394
395                 /* Copy up the data area */
396                 chunk = seglen;
397                 while(chunk) {
398                         if(!xp) {
399                                 ip->stats[OutDiscards]++;
400                                 ip->stats[FragFails]++;
401                                 freeblist(nb);
402                                 netlog(f, Logip, "!xp: chunk %d\n", chunk);
403                                 goto raise;
404                         }
405                         blklen = chunk;
406                         if(BLEN(xp) < chunk)
407                                 blklen = BLEN(xp);
408                         memmove(nb->wp, xp->rp, blklen);
409                         nb->wp += blklen;
410                         xp->rp += blklen;
411                         chunk -= blklen;
412                         if(xp->rp == xp->wp)
413                                 xp = xp->next;
414                 } 
415
416                 feh->cksum[0] = 0;
417                 feh->cksum[1] = 0;
418                 hnputs(feh->cksum, ipcsum(&feh->vihl));
419                 ifc->m->bwrite(ifc, nb, V4, gate);
420                 ip->stats[FragCreates]++;
421         }
422         ip->stats[FragOKs]++;
423 raise:
424         runlock(&ifc->rwlock);
425         poperror();
426 free:
427         freeblist(bp);
428         return rv;
429 }
430
431 void
432 ipiput4(struct Fs *f, struct Ipifc *ifc, struct block *bp)
433 {
434         int hl;
435         int hop, tos, proto, olen;
436         struct Ip4hdr *h;
437         struct Proto *p;
438         uint16_t frag;
439         int notforme;
440         uint8_t *dp, v6dst[IPaddrlen];
441         struct IP *ip;
442         struct route *r;
443
444         if(BLKIPVER(bp) != IP_VER4) {
445                 ipiput6(f, ifc, bp);
446                 return;
447         }
448
449         ip = f->ip;
450         ip->stats[InReceives]++;
451
452         /*
453          *  Ensure we have all the header info in the first
454          *  block.  Make life easier for other protocols by
455          *  collecting up to the first 64 bytes in the first block.
456          */
457         if(BLEN(bp) < 64) {
458                 hl = blocklen(bp);
459                 if(hl < IP4HDR)
460                         hl = IP4HDR;
461                 if(hl > 64)
462                         hl = 64;
463                 bp = pullupblock(bp, hl);
464                 if(bp == NULL)
465                         return;
466         }
467
468         h = (struct Ip4hdr*)(bp->rp);
469
470         /* dump anything that whose header doesn't checksum */
471         if((bp->flag & Bipck) == 0 && ipcsum(&h->vihl)) {
472                 ip->stats[InHdrErrors]++;
473                 netlog(f, Logip, "ip: checksum error %V\n", h->src);
474                 freeblist(bp);
475                 return;
476         }
477         v4tov6(v6dst, h->dst);
478         notforme = ipforme(f, v6dst) == 0;
479
480         /* Check header length and version */
481         if((h->vihl&0x0F) != IP_HLEN4) {
482                 hl = (h->vihl&0xF)<<2;
483                 if(hl < (IP_HLEN4<<2)) {
484                         ip->stats[InHdrErrors]++;
485                         netlog(f, Logip, "ip: %V bad hivl %ux\n", h->src, h->vihl);
486                         freeblist(bp);
487                         return;
488                 }
489           /* If this is not routed strip off the options */
490                 if(notforme == 0) {
491                         olen = nhgets(h->length);
492                         dp = bp->rp + (hl - (IP_HLEN4<<2));
493                         memmove(dp, h, IP_HLEN4<<2);
494                         bp->rp = dp;
495                         h = (struct Ip4hdr*)(bp->rp);
496                         h->vihl = (IP_VER4|IP_HLEN4);
497                         hnputs(h->length, olen-hl+(IP_HLEN4<<2));
498                 }
499         }
500
501         /* route */
502         if(notforme) {
503                 struct conv conv;
504
505                 if(!ip->iprouting){
506                         freeb(bp);
507                         return;
508                 }
509
510                 /* don't forward to source's network */
511                 conv.r = NULL;
512                 r = v4lookup(f, h->dst, &conv);
513                 if(r == NULL || r->rt.ifc == ifc){
514                         ip->stats[OutDiscards]++;
515                         freeblist(bp);
516                         return;
517                 }
518
519                 /* don't forward if packet has timed out */
520                 hop = h->ttl;
521                 if(hop < 1) {
522                         ip->stats[InHdrErrors]++;
523                         icmpttlexceeded(f, ifc->lifc->local, bp);
524                         freeblist(bp);
525                         return;
526                 }
527
528                 /* reassemble if the interface expects it */
529 if(r->rt.ifc == NULL) panic("NULL route rfc");
530                 if(r->rt.ifc->reassemble){
531                         frag = nhgets(h->frag);
532                         if(frag) {
533                                 h->tos = 0;
534                                 if(frag & IP_MF)
535                                         h->tos = 1;
536                                 bp = ip4reassemble(ip, frag, bp, h);
537                                 if(bp == NULL)
538                                         return;
539                                 h = (struct Ip4hdr*)(bp->rp);
540                         }
541                 }
542
543                 ip->stats[ForwDatagrams]++;
544                 tos = h->tos;
545                 hop = h->ttl;
546                 ipoput4(f, bp, 1, hop - 1, tos, &conv);
547                 return;
548         }
549
550         frag = nhgets(h->frag);
551         if(frag) {
552                 h->tos = 0;
553                 if(frag & IP_MF)
554                         h->tos = 1;
555                 bp = ip4reassemble(ip, frag, bp, h);
556                 if(bp == NULL)
557                         return;
558                 h = (struct Ip4hdr*)(bp->rp);
559         }
560
561         /* don't let any frag info go up the stack */
562         h->frag[0] = 0;
563         h->frag[1] = 0;
564
565         proto = h->proto;
566         p = Fsrcvpcol(f, proto);
567         if(p != NULL && p->rcv != NULL) {
568                 ip->stats[InDelivers]++;
569                 (*p->rcv)(p, ifc, bp);
570                 return;
571         }
572         ip->stats[InDiscards]++;
573         ip->stats[InUnknownProtos]++;
574         freeblist(bp);
575 }
576
577 int
578 ipstats(struct Fs *f, char *buf, int len)
579 {
580         struct IP *ip;
581         char *p, *e;
582         int i;
583
584         ip = f->ip;
585         ip->stats[DefaultTTL] = MAXTTL;
586
587         p = buf;
588         e = p+len;
589         for(i = 0; i < Nstats; i++)
590                 p = seprintf(p, e, "%s: %lud\n", statnames[i], ip->stats[i]);
591         return p - buf;
592 }
593
594 struct block*
595 ip4reassemble(struct IP *ip, int offset, struct block *bp, struct Ip4hdr *ih)
596 {
597         int fend;
598         uint16_t id;
599         struct fragment4 *f, *fnext;
600         uint32_t src, dst;
601         struct block *bl, **l, *last, *prev;
602         int ovlap, len, fragsize, pktposn;
603
604         src = nhgetl(ih->src);
605         dst = nhgetl(ih->dst);
606         id = nhgets(ih->id);
607
608         /*
609          *  block lists are too hard, pullupblock into a single block
610          */
611         if(bp->next){
612                 bp = pullupblock(bp, blocklen(bp));
613                 ih = (struct Ip4hdr*)(bp->rp);
614         }
615
616         qlock(&ip->fraglock4);
617
618         /*
619          *  find a reassembly queue for this fragment
620          */
621         for(f = ip->flisthead4; f; f = fnext){
622                 fnext = f->next;        /* because ipfragfree4 changes the list */
623                 if(f->src == src && f->dst == dst && f->id == id)
624                         break;
625                 if(f->age < NOW){
626                         ip->stats[ReasmTimeout]++;
627                         ipfragfree4(ip, f);
628                 }
629         }
630
631         /*
632          *  if this isn't a fragmented packet, accept it
633          *  and get rid of any fragments that might go
634          *  with it.
635          */
636         if(!ih->tos && (offset & ~(IP_MF|IP_DF)) == 0) {
637                 if(f != NULL) {
638                         ipfragfree4(ip, f);
639                         ip->stats[ReasmFails]++;
640                 }
641                 qunlock(&ip->fraglock4);
642                 return bp;
643         }
644
645         if(bp->base+sizeof(struct Ipfrag) >= bp->rp){
646                 bp = padblock(bp, sizeof(struct Ipfrag));
647                 bp->rp += sizeof(struct Ipfrag);
648         }
649
650         BKFG(bp)->foff = offset<<3;
651         BKFG(bp)->flen = nhgets(ih->length)-IP4HDR;
652
653         /* First fragment allocates a reassembly queue */
654         if(f == NULL) {
655                 f = ipfragallo4(ip);
656                 f->id = id;
657                 f->src = src;
658                 f->dst = dst;
659
660                 f->blist = bp;
661
662                 qunlock(&ip->fraglock4);
663                 ip->stats[ReasmReqds]++;
664                 return NULL;
665         }
666
667         /*
668          *  find the new fragment's position in the queue
669          */
670         prev = NULL;
671         l = &f->blist;
672         bl = f->blist;
673         while(bl != NULL && BKFG(bp)->foff > BKFG(bl)->foff) {
674                 prev = bl;
675                 l = &bl->next;
676                 bl = bl->next;
677         }
678
679         /* Check overlap of a previous fragment - trim away as necessary */
680         if(prev) {
681                 ovlap = BKFG(prev)->foff + BKFG(prev)->flen - BKFG(bp)->foff;
682                 if(ovlap > 0) {
683                         if(ovlap >= BKFG(bp)->flen) {
684                                 freeblist(bp);
685                                 qunlock(&ip->fraglock4);
686                                 return NULL;
687                         }
688                         BKFG(prev)->flen -= ovlap;
689                 }
690         }
691
692         /* Link onto assembly queue */
693         bp->next = *l;
694         *l = bp;
695
696         /* Check to see if succeeding segments overlap */
697         if(bp->next) {
698                 l = &bp->next;
699                 fend = BKFG(bp)->foff + BKFG(bp)->flen;
700                 /* Take completely covered segments out */
701                 while(*l) {
702                         ovlap = fend - BKFG(*l)->foff;
703                         if(ovlap <= 0)
704                                 break;
705                         if(ovlap < BKFG(*l)->flen) {
706                                 BKFG(*l)->flen -= ovlap;
707                                 BKFG(*l)->foff += ovlap;
708                                 /* move up ih hdrs */
709                                 memmove((*l)->rp + ovlap, (*l)->rp, IP4HDR);
710                                 (*l)->rp += ovlap;
711                                 break;
712                         }
713                         last = (*l)->next;
714                         (*l)->next = NULL;
715                         freeblist(*l);
716                         *l = last;
717                 }
718         }
719
720         /*
721          *  look for a complete packet.  if we get to a fragment
722          *  without IP_MF set, we're done.
723          */
724         pktposn = 0;
725         for(bl = f->blist; bl; bl = bl->next) {
726                 if(BKFG(bl)->foff != pktposn)
727                         break;
728                 if((BLKIP(bl)->frag[0]&(IP_MF>>8)) == 0) {
729                         bl = f->blist;
730                         len = nhgets(BLKIP(bl)->length);
731                         bl->wp = bl->rp + len;
732
733                         /* Pullup all the fragment headers and
734                          * return a complete packet
735                          */
736                         for(bl = bl->next; bl; bl = bl->next) {
737                                 fragsize = BKFG(bl)->flen;
738                                 len += fragsize;
739                                 bl->rp += IP4HDR;
740                                 bl->wp = bl->rp + fragsize;
741                         }
742
743                         bl = f->blist;
744                         f->blist = NULL;
745                         ipfragfree4(ip, f);
746                         ih = BLKIP(bl);
747                         hnputs(ih->length, len);
748                         qunlock(&ip->fraglock4);
749                         ip->stats[ReasmOKs]++;
750                         return bl;              
751                 }
752                 pktposn += BKFG(bl)->flen;
753         }
754         qunlock(&ip->fraglock4);
755         return NULL;
756 }
757
758 /*
759  * ipfragfree4 - Free a list of fragments - assume hold fraglock4
760  */
761 void
762 ipfragfree4(struct IP *ip, struct fragment4 *frag)
763 {
764         struct fragment4 *fl, **l;
765
766         if(frag->blist)
767                 freeblist(frag->blist);
768
769         frag->src = 0;
770         frag->id = 0;
771         frag->blist = NULL;
772
773         l = &ip->flisthead4;
774         for(fl = *l; fl; fl = fl->next) {
775                 if(fl == frag) {
776                         *l = frag->next;
777                         break;
778                 }
779                 l = &fl->next;
780         }
781
782         frag->next = ip->fragfree4;
783         ip->fragfree4 = frag;
784
785 }
786
787 /*
788  * ipfragallo4 - allocate a reassembly queue - assume hold fraglock4
789  */
790 struct fragment4 *
791 ipfragallo4(struct IP *ip)
792 {
793         struct fragment4 *f;
794
795         while(ip->fragfree4 == NULL) {
796                 /* free last entry on fraglist */
797                 for(f = ip->flisthead4; f->next; f = f->next)
798                         ;
799                 ipfragfree4(ip, f);
800         }
801         f = ip->fragfree4;
802         ip->fragfree4 = f->next;
803         f->next = ip->flisthead4;
804         ip->flisthead4 = f;
805         f->age = NOW + 30000;
806
807         return f;
808 }
809
810 uint16_t
811 ipcsum(uint8_t *addr)
812 {
813         int len;
814         uint32_t sum;
815
816         sum = 0;
817         len = (addr[0]&0xf)<<2;
818
819         while(len > 0) {
820                 sum += addr[0]<<8 | addr[1] ;
821                 len -= 2;
822                 addr += 2;
823         }
824
825         sum = (sum & 0xffff) + (sum >> 16);
826         sum = (sum & 0xffff) + (sum >> 16);
827
828         return (sum^0xffff);
829 }