| plan 9 kernel history: overview | file list | diff list |
2001/0430/ip/ip.c (diff list | history)
| ip/ip.c on 1997/0327 | ||
| 1997/0327 | #include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "../port/error.h" | |
| 2000/1111 | #include "../ip/ip.h" | |
| 1997/0327 | typedef struct Iphdr Iphdr; typedef struct Fragment Fragment; typedef struct Ipfrag Ipfrag; enum { IPHDR = 20, /* sizeof(Iphdr) */ IP_VER = 0x40, /* Using IP version 4 */ | |
| 2000/0905 | IP_HLEN = 0x05, /* Header length in words */ | |
| 1997/0327 | IP_DF = 0x4000, /* Don't fragment */ IP_MF = 0x2000, /* More fragments */ | |
| 1998/0306 | IP_MAX = (32*1024), /* Maximum Internet packet size */ | |
| 1997/0327 | }; struct Iphdr { | |
| 1998/0306 | uchar vihl; /* Version and header length */ uchar tos; /* Type of service */ uchar length[2]; /* packet length */ | |
| 1998/0313 | uchar id[2]; /* ip->identification */ | |
| 1998/0306 | uchar frag[2]; /* Fragment information */ uchar ttl; /* Time to live */ uchar proto; /* Protocol */ uchar cksum[2]; /* Header checksum */ | |
| 1998/0313 | uchar src[4]; /* IP source */ uchar dst[4]; /* IP destination */ | |
| 1997/0327 | }; struct Fragment { Block* blist; Fragment* next; | |
| 1998/0306 | ulong src; ulong dst; | |
| 1997/0327 | ushort id; ulong age; }; struct Ipfrag { ushort foff; ushort flen; }; | |
| 1998/0313 | /* MIB II counters */ | |
| 2000/0706 | enum | |
| 1998/0313 | { | |
| 2000/0706 | Forwarding, DefaultTTL, InReceives, InHdrErrors, InAddrErrors, ForwDatagrams, InUnknownProtos, InDiscards, InDelivers, OutRequests, OutDiscards, OutNoRoutes, ReasmTimeout, ReasmReqds, ReasmOKs, ReasmFails, FragOKs, FragFails, FragCreates, Nstats, | |
| 1998/0313 | }; | |
| 1997/0327 | ||
| 2000/0706 | static char *statnames[] = { [Forwarding] "Forwarding", [DefaultTTL] "DefaultTTL", [InReceives] "InReceives", [InHdrErrors] "InHdrErrors", [InAddrErrors] "InAddrErrors", [ForwDatagrams] "ForwDatagrams", [InUnknownProtos] "InUnknownProtos", [InDiscards] "InDiscards", [InDelivers] "InDelivers", [OutRequests] "OutRequests", [OutDiscards] "OutDiscards", [OutNoRoutes] "OutNoRoutes", [ReasmTimeout] "ReasmTimeout", [ReasmReqds] "ReasmReqds", [ReasmOKs] "ReasmOKs", [ReasmFails] "ReasmFails", [FragOKs] "FragOKs", [FragFails] "FragFails", [FragCreates] "FragCreates", }; | |
| 1998/0313 | /* an instance of IP */ struct IP { | |
| 2000/0706 | ulong stats[Nstats]; | |
| 1998/0313 | QLock fraglock; Fragment* flisthead; Fragment* fragfree; | |
| 1999/0302 | Ref id; | |
| 1998/0313 | int iprouting; /* true if we route like a gateway */ }; | |
| 1997/0327 | #define BLKIP(xp) ((Iphdr*)((xp)->rp)) /* * This sleazy macro relies on the media header size being * larger than sizeof(Ipfrag). ipreassemble checks this is true */ #define BKFG(xp) ((Ipfrag*)((xp)->base)) | |
| 1998/0313 | ushort ipcsum(uchar*); Block* ipreassemble(IP*, int, Block*, Iphdr*); void ipfragfree(IP*, Fragment*); Fragment* ipfragallo(IP*); void ip_init(Fs *f) | |
| 1997/0916 | { | |
| 1998/0313 | IP *ip; | |
| 1997/0916 | ||
| 1998/0313 | ip = smalloc(sizeof(IP)); initfrag(ip, 100); f->ip = ip; } | |
| 1997/0327 | void | |
| 1998/0314 | iprouting(Fs *f, int on) { f->ip->iprouting = on; | |
| 1998/0813 | if(f->ip->iprouting==0) | |
| 2000/0706 | f->ip->stats[Forwarding] = 2; | |
| 1998/0813 | else | |
| 2000/0706 | f->ip->stats[Forwarding] = 1; | |
| 1998/0314 | } void | |
| 1999/0817 | ipoput(Fs *f, Block *bp, int gating, int ttl, int tos) | |
| 1997/0327 | { | |
| 1998/0306 | Ipifc *ifc; uchar *gate; | |
| 1997/0327 | ushort fragoff; Block *xp, *nb; Iphdr *eh, *feh; int lid, len, seglen, chunk, dlen, blklen, offset, medialen; | |
| 1998/0313 | Route *r, *sr; IP *ip; | |
| 1997/0327 | ||
| 1998/0313 | ip = f->ip; | |
| 1997/0327 | /* Fill out the ip header */ | |
| 1998/0630 | eh = (Iphdr*)(bp->rp); | |
| 1997/0327 | ||
| 2000/0706 | ip->stats[OutRequests]++; | |
| 1998/0313 | ||
| 1998/0306 | /* Number of uchars in data and ip header to write */ | |
| 1997/0327 | len = blocklen(bp); | |
| 1998/0313 | ||
| 1997/0327 | if(gating){ chunk = nhgets(eh->length); if(chunk > len){ | |
| 2000/0706 | ip->stats[OutDiscards]++; | |
| 1998/0313 | netlog(f, Logip, "short gated packet\n"); | |
| 1998/0308 | goto free; | |
| 1997/0327 | } if(chunk < len) len = chunk; } | |
| 1998/0306 | if(len >= IP_MAX){ | |
| 2000/0706 | ip->stats[OutDiscards]++; | |
| 1998/0313 | netlog(f, Logip, "exceeded ip max size %V\n", eh->dst); | |
| 1998/0308 | goto free; | |
| 1997/0327 | } | |
| 1998/0313 | r = v4lookup(f, eh->dst); | |
| 1998/0306 | if(r == nil){ | |
| 2000/0706 | ip->stats[OutNoRoutes]++; | |
| 1998/0313 | netlog(f, Logip, "no interface %V\n", eh->dst); | |
| 1998/0308 | goto free; | |
| 1997/0327 | } | |
| 1998/0313 | ifc = r->ifc; if(r->type & (Rifc|Runi)) | |
| 1998/0306 | gate = eh->dst; else | |
| 1998/0313 | if(r->type & (Rbcast|Rmulti)) { gate = eh->dst; sr = v4lookup(f, eh->src); if(sr != nil && (sr->type & Runi)) ifc = sr->ifc; } else | |
| 1998/0306 | gate = r->v4.gate; | |
| 1998/0313 | ||
| 1999/10041 | if(!gating) | |
| 1997/0327 | eh->vihl = IP_VER|IP_HLEN; | |
| 1999/10041 | eh->ttl = ttl; | |
| 2001/0430 | if(!gating) eh->tos = tos; | |
| 1997/0327 | ||
| 1999/0302 | if(!canrlock(ifc)) goto free; | |
| 1998/0307 | if(waserror()){ runlock(ifc); nexterror(); } if(ifc->m == nil) goto raise; | |
| 1997/0327 | /* If we dont need to fragment just send it */ | |
| 2000/0913 | medialen = ifc->maxmtu - ifc->m->hsize; | |
| 1997/0327 | if(len <= medialen) { if(!gating) | |
| 1999/0302 | hnputs(eh->id, incref(&ip->id)); | |
| 1997/0327 | hnputs(eh->length, len); | |
| 2001/0430 | if(!gating){ eh->frag[0] = 0; eh->frag[1] = 0; } | |
| 1997/0327 | eh->cksum[0] = 0; eh->cksum[1] = 0; hnputs(eh->cksum, ipcsum(&eh->vihl)); | |
| 1998/0313 | /* print("ipoput %V->%V via %V\n", eh->src, eh->dst, gate); /**/ | |
| 1998/0306 | ifc->m->bwrite(ifc, bp, V4, gate); | |
| 1998/0307 | runlock(ifc); poperror(); | |
| 1997/0327 | return; } if(eh->frag[0] & (IP_DF>>8)){ | |
| 2000/0706 | ip->stats[FragFails]++; ip->stats[OutDiscards]++; | |
| 1998/0313 | netlog(f, Logip, "%V: eh->frag[0] & (IP_DF>>8)\n", eh->dst); | |
| 1997/0327 | goto raise; } seglen = (medialen - IPHDR) & ~7; if(seglen < 8){ | |
| 2000/0706 | ip->stats[FragFails]++; ip->stats[OutDiscards]++; | |
| 1998/0313 | netlog(f, Logip, "%V seglen < 8\n", eh->dst); | |
| 1997/0327 | goto raise; } dlen = len - IPHDR; xp = bp; if(gating) lid = nhgets(eh->id); else | |
| 1999/0302 | lid = incref(&ip->id); | |
| 1997/0327 | offset = IPHDR; while(xp != nil && offset && offset >= BLEN(xp)) { offset -= BLEN(xp); xp = xp->next; } xp->rp += offset; | |
| 2001/0430 | if(gating) fragoff = nhgets(eh->frag); else fragoff = 0; dlen += fragoff; for(; fragoff < dlen; fragoff += seglen) { | |
| 1997/0327 | nb = allocb(IPHDR+seglen); feh = (Iphdr*)(nb->rp); memmove(nb->wp, eh, IPHDR); nb->wp += IPHDR; if((fragoff + seglen) >= dlen) { seglen = dlen - fragoff; hnputs(feh->frag, fragoff>>3); } else hnputs(feh->frag, (fragoff>>3)|IP_MF); hnputs(feh->length, seglen + IPHDR); hnputs(feh->id, lid); /* Copy up the data area */ chunk = seglen; while(chunk) { if(!xp) { | |
| 2000/0706 | ip->stats[OutDiscards]++; ip->stats[FragFails]++; | |
| 1997/0327 | freeblist(nb); | |
| 1998/0313 | netlog(f, Logip, "!xp: chunk %d\n", chunk); | |
| 1997/0327 | goto raise; } blklen = chunk; if(BLEN(xp) < chunk) blklen = BLEN(xp); memmove(nb->wp, xp->rp, blklen); nb->wp += blklen; xp->rp += blklen; chunk -= blklen; if(xp->rp == xp->wp) xp = xp->next; | |
| 1998/0313 | } | |
| 1997/0327 | feh->cksum[0] = 0; feh->cksum[1] = 0; hnputs(feh->cksum, ipcsum(&feh->vihl)); | |
| 1998/0306 | ifc->m->bwrite(ifc, nb, V4, gate); | |
| 2000/0706 | ip->stats[FragCreates]++; | |
| 1997/0327 | } | |
| 2000/0706 | ip->stats[FragOKs]++; | |
| 1997/0327 | raise: | |
| 1998/0307 | runlock(ifc); poperror(); | |
| 1998/0308 | free: | |
| 1997/0327 | freeblist(bp); } void | |
| 1998/0313 | initfrag(IP *ip, int size) | |
| 1997/0327 | { Fragment *fq, *eq; | |
| 1998/0313 | ip->fragfree = (Fragment*)malloc(sizeof(Fragment) * size); if(ip->fragfree == nil) | |
| 1997/0327 | panic("initfrag"); | |
| 1998/0313 | eq = &ip->fragfree[size]; for(fq = ip->fragfree; fq < eq; fq++) | |
| 1997/0327 | fq->next = fq+1; | |
| 1998/0313 | ip->fragfree[size-1].next = nil; | |
| 1997/0327 | } | |
| 1998/0313 | #define DBG(x) if((logmask & Logipmsg) && (iponly == 0 || x == iponly))netlog | |
| 1997/0327 | void | |
| 1998/0313 | ipiput(Fs *f, uchar *ia, Block *bp) | |
| 1997/0327 | { | |
| 1998/0313 | int hl; | |
| 1997/0327 | Iphdr *h; Proto *p; ushort frag; int notforme; | |
| 1998/0313 | uchar *dp, v6dst[IPaddrlen]; IP *ip; | |
| 1998/1209 | Route *r, *sr; | |
| 1997/0327 | ||
| 1998/0313 | ip = f->ip; | |
| 2000/0706 | ip->stats[InReceives]++; | |
| 1998/0313 | ||
| 1997/0806 | // h = (Iphdr *)(bp->rp); | |
| 1998/0313 | // DBG(nhgetl(h->src))(Logipmsg, "ipiput %V %V len %d proto %d\n", // h->src, h->dst, BLEN(bp), h->proto); | |
| 1997/0327 | ||
| 1998/0326 | /* | |
| 1998/0331 | * Ensure we have all the header info in the first | |
| 1998/0326 | * block. Make life easier for other protocols by * collecting up to the first 64 bytes in the first block. */ if(BLEN(bp) < 64) { hl = blocklen(bp); if(hl < IPHDR) hl = IPHDR; if(hl > 64) hl = 64; bp = pullupblock(bp, hl); | |
| 1997/0327 | if(bp == nil) return; } h = (Iphdr *)(bp->rp); | |
| 1998/0306 | /* dump anything that whose header doesn't checksum */ | |
| 1997/0327 | if(ipcsum(&h->vihl)) { | |
| 2000/0706 | ip->stats[InHdrErrors]++; | |
| 1998/1208 | netlog(f, Logip, "ip: checksum error %V\n", h->src); | |
| 1997/0327 | freeblist(bp); return; } | |
| 1998/0306 | v4tov6(v6dst, h->dst); | |
| 1998/0313 | notforme = ipforme(f, v6dst) == 0; /* Check header length and version */ if(h->vihl != (IP_VER|IP_HLEN)) { hl = (h->vihl&0xF)<<2; if((h->vihl&0xF0) != IP_VER || hl < (IP_HLEN<<2)) { | |
| 2000/0706 | ip->stats[InHdrErrors]++; | |
| 1998/0313 | netlog(f, Logip, "ip: %V bad hivl %ux\n", h->src, h->vihl); freeblist(bp); return; } /* If this is not routed strip off the options */ if(notforme == 0) { dp = bp->rp + (hl - (IP_HLEN<<2)); memmove(dp, h, IP_HLEN<<2); bp->rp = dp; h = (Iphdr *)(bp->rp); h->vihl = (IP_VER|IP_HLEN); } } /* route */ | |
| 1998/0306 | if(notforme) { | |
| 1998/1209 | if(!ip->iprouting){ | |
| 2000/0220 | freeb(bp); | |
| 1998/1209 | return; } /* don't forward to source's network */ sr = v4lookup(f, h->src); r = v4lookup(f, h->dst); if(r == nil || sr == r){ | |
| 2000/0706 | ip->stats[OutDiscards]++; | |
| 1998/1209 | freeblist(bp); return; } /* don't forward if packet has timed out */ if(h->ttl <= 1){ | |
| 2000/0706 | ip->stats[InHdrErrors]++; | |
| 1998/1209 | icmpttlexceeded(f, ia, bp); freeblist(bp); return; } | |
| 2000/0706 | ip->stats[ForwDatagrams]++; | |
| 1999/0817 | ipoput(f, bp, 1, h->ttl - 1, h->tos); | |
| 1998/1209 | ||
| 1998/0306 | return; } | |
| 1998/0313 | ||
| 1997/0327 | frag = nhgets(h->frag); if(frag) { h->tos = 0; if(frag & IP_MF) h->tos = 1; | |
| 1998/0313 | bp = ipreassemble(ip, frag, bp, h); | |
| 1997/0327 | if(bp == nil) return; h = (Iphdr *)(bp->rp); } | |
| 1998/0313 | p = Fsrcvpcol(f, h->proto); if(p != nil && p->rcv != nil) { | |
| 2000/0706 | ip->stats[InDelivers]++; | |
| 1998/0313 | (*p->rcv)(p, ia, bp); return; } | |
| 2000/0706 | ip->stats[InDiscards]++; ip->stats[InUnknownProtos]++; | |
| 1998/0313 | freeblist(bp); | |
| 1997/0327 | } int | |
| 1998/0313 | ipstats(Fs *f, char *buf, int len) | |
| 1997/0327 | { | |
| 1998/0313 | IP *ip; | |
| 2000/0706 | char *p, *e; int i; | |
| 1997/0327 | ||
| 1998/0313 | ip = f->ip; | |
| 2000/0706 | ip->stats[DefaultTTL] = MAXTTL; p = buf; e = p+len; for(i = 0; i < Nstats; i++) p = seprint(p, e, "%s: %lud\n", statnames[i], ip->stats[i]); return p - buf; | |
| 1997/0327 | } Block* | |
| 1998/0313 | ipreassemble(IP *ip, int offset, Block *bp, Iphdr *ih) | |
| 1997/0327 | { int fend; ushort id; Fragment *f, *fnext; | |
| 1998/0306 | ulong src, dst; | |
| 1997/0327 | Block *bl, **l, *last, *prev; int ovlap, len, fragsize, pktposn; | |
| 1998/0313 | src = nhgetl(ih->src); dst = nhgetl(ih->dst); id = nhgets(ih->id); | |
| 1997/0327 | /* * block lists are too hard, pullupblock into a single block */ if(bp->next){ bp = pullupblock(bp, blocklen(bp)); | |
| 1998/0313 | ih = (Iphdr *)(bp->rp); | |
| 1997/0327 | } | |
| 1998/0313 | qlock(&ip->fraglock); | |
| 1997/0327 | /* * find a reassembly queue for this fragment */ | |
| 1998/0313 | for(f = ip->flisthead; f; f = fnext){ | |
| 1997/0327 | fnext = f->next; /* because ipfragfree changes the list */ if(f->src == src && f->dst == dst && f->id == id) break; | |
| 1997/0916 | if(f->age < msec){ | |
| 2000/0706 | ip->stats[ReasmTimeout]++; | |
| 1998/0313 | ipfragfree(ip, f); | |
| 1997/0916 | } | |
| 1997/0327 | } /* * if this isn't a fragmented packet, accept it * and get rid of any fragments that might go * with it. */ | |
| 1998/0313 | if(!ih->tos && (offset & ~(IP_MF|IP_DF)) == 0) { if(f != nil) { ipfragfree(ip, f); | |
| 2000/0706 | ip->stats[ReasmFails]++; | |
| 1998/0313 | } qunlock(&ip->fraglock); | |
| 1997/0327 | return bp; } | |
| 1997/0504 | if(bp->base+sizeof(Ipfrag) >= bp->rp){ bp = padblock(bp, sizeof(Ipfrag)); bp->rp += sizeof(Ipfrag); } | |
| 1997/0327 | BKFG(bp)->foff = offset<<3; | |
| 1998/0313 | BKFG(bp)->flen = nhgets(ih->length)-IPHDR; | |
| 1997/0327 | /* First fragment allocates a reassembly queue */ if(f == nil) { | |
| 1998/0313 | f = ipfragallo(ip); | |
| 1997/0327 | f->id = id; f->src = src; f->dst = dst; f->blist = bp; | |
| 1998/0313 | qunlock(&ip->fraglock); | |
| 2000/0706 | ip->stats[ReasmReqds]++; | |
| 1997/0327 | return nil; } /* * find the new fragment's position in the queue */ prev = nil; l = &f->blist; bl = f->blist; while(bl != nil && BKFG(bp)->foff > BKFG(bl)->foff) { prev = bl; l = &bl->next; bl = bl->next; } /* Check overlap of a previous fragment - trim away as necessary */ if(prev) { ovlap = BKFG(prev)->foff + BKFG(prev)->flen - BKFG(bp)->foff; if(ovlap > 0) { if(ovlap >= BKFG(bp)->flen) { freeblist(bp); | |
| 1998/0313 | qunlock(&ip->fraglock); | |
| 1997/0327 | return nil; } BKFG(prev)->flen -= ovlap; } } /* Link onto assembly queue */ bp->next = *l; *l = bp; /* Check to see if succeeding segments overlap */ if(bp->next) { l = &bp->next; fend = BKFG(bp)->foff + BKFG(bp)->flen; /* Take completely covered segments out */ while(*l) { ovlap = fend - BKFG(*l)->foff; if(ovlap <= 0) break; if(ovlap < BKFG(*l)->flen) { BKFG(*l)->flen -= ovlap; BKFG(*l)->foff += ovlap; | |
| 1998/0313 | /* move up ih hdrs */ | |
| 1997/0327 | memmove((*l)->rp + ovlap, (*l)->rp, IPHDR); (*l)->rp += ovlap; break; } last = (*l)->next; (*l)->next = nil; freeblist(*l); *l = last; } } /* * look for a complete packet. if we get to a fragment * without IP_MF set, we're done. */ pktposn = 0; for(bl = f->blist; bl; bl = bl->next) { if(BKFG(bl)->foff != pktposn) break; if((BLKIP(bl)->frag[0]&(IP_MF>>8)) == 0) { bl = f->blist; len = nhgets(BLKIP(bl)->length); bl->wp = bl->rp + len; /* Pullup all the fragment headers and * return a complete packet */ for(bl = bl->next; bl; bl = bl->next) { fragsize = BKFG(bl)->flen; len += fragsize; bl->rp += IPHDR; bl->wp = bl->rp + fragsize; } bl = f->blist; f->blist = nil; | |
| 1998/0313 | ipfragfree(ip, f); ih = BLKIP(bl); hnputs(ih->length, len); qunlock(&ip->fraglock); | |
| 2000/0706 | ip->stats[ReasmOKs]++; | |
| 1997/0327 | return bl; } pktposn += BKFG(bl)->flen; } | |
| 1998/0313 | qunlock(&ip->fraglock); | |
| 1997/0327 | return nil; } /* | |
| 1997/0529 | * ipfragfree - Free a list of fragments - assume hold fraglock | |
| 1997/0327 | */ void | |
| 1998/0313 | ipfragfree(IP *ip, Fragment *frag) | |
| 1997/0327 | { Fragment *fl, **l; if(frag->blist) freeblist(frag->blist); frag->src = 0; frag->id = 0; frag->blist = nil; | |
| 1998/0313 | l = &ip->flisthead; | |
| 1997/0327 | for(fl = *l; fl; fl = fl->next) { if(fl == frag) { *l = frag->next; break; } l = &fl->next; } | |
| 1998/0313 | frag->next = ip->fragfree; ip->fragfree = frag; | |
| 1997/0327 | } /* | |
| 1997/0529 | * ipfragallo - allocate a reassembly queue - assume hold fraglock | |
| 1997/0327 | */ Fragment * | |
| 1998/0313 | ipfragallo(IP *ip) | |
| 1997/0327 | { Fragment *f; | |
| 1998/0313 | while(ip->fragfree == nil) { | |
| 1997/0529 | /* free last entry on fraglist */ | |
| 1998/0313 | for(f = ip->flisthead; f->next; f = f->next) | |
| 1997/0529 | ; | |
| 1998/0313 | ipfragfree(ip, f); | |
| 1997/0327 | } | |
| 1998/0313 | f = ip->fragfree; ip->fragfree = f->next; f->next = ip->flisthead; ip->flisthead = f; | |
| 1997/0327 | f->age = msec + 30000; return f; } ushort | |
| 1998/0306 | ipcsum(uchar *addr) | |
| 1997/0327 | { int len; ulong sum; sum = 0; len = (addr[0]&0xf)<<2; while(len > 0) { sum += addr[0]<<8 | addr[1] ; len -= 2; addr += 2; } sum = (sum & 0xffff) + (sum >> 16); sum = (sum & 0xffff) + (sum >> 16); return (sum^0xffff); } | |