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