717 lines · 13.6 KB · C
RawDownload
#include	"u.h"#include	"../port/lib.h"#include	"mem.h"#include	"dat.h"#include	"fns.h"#include	"../port/error.h" #include	"ip.h" #define BLKIPVER(xp)	(((Ip4hdr*)((xp)->rp))->vihl&0xF0) 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", [OutCantLock]	"OutCantLock",}; #define BLKIP(xp)	((Ip4hdr*)((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)) ushort		ipcsum(uchar*);Block*		ip4reassemble(IP*, int, Block*, Ip4hdr*);void		ipfragfree4(IP*, Fragment4*);Fragment4*	ipfragallo4(IP*); voidip_init_6(Fs *f){	v6params *v6p; 	v6p = smalloc(sizeof(v6params)); 	v6p->rp.mflag		= 0;		/* default not managed */	v6p->rp.oflag		= 0;	v6p->rp.maxraint	= 600000;	/* millisecs */	v6p->rp.minraint	= 200000;	v6p->rp.linkmtu		= 0;		/* no mtu sent */	v6p->rp.reachtime	= 0;	v6p->rp.rxmitra		= 0;	v6p->rp.ttl		= MAXTTL;	v6p->rp.routerlt	= 3 * v6p->rp.maxraint; 	v6p->hp.rxmithost	= 1000;		/* v6 RETRANS_TIMER */ 	v6p->cdrouter 		= -1; 	f->v6p			= v6p;} voidinitfrag(IP *ip, int size){	Fragment4 *fq4, *eq4;	Fragment6 *fq6, *eq6; 	ip->fragfree4 = (Fragment4*)malloc(sizeof(Fragment4) * size);	if(ip->fragfree4 == nil)		panic("initfrag"); 	eq4 = &ip->fragfree4[size];	for(fq4 = ip->fragfree4; fq4 < eq4; fq4++)		fq4->next = fq4+1; 	ip->fragfree4[size-1].next = nil; 	ip->fragfree6 = (Fragment6*)malloc(sizeof(Fragment6) * size);	if(ip->fragfree6 == nil)		panic("initfrag"); 	eq6 = &ip->fragfree6[size];	for(fq6 = ip->fragfree6; fq6 < eq6; fq6++)		fq6->next = fq6+1; 	ip->fragfree6[size-1].next = nil;} voidip_init(Fs *f){	IP *ip; 	ip = smalloc(sizeof(IP));	initfrag(ip, 100);	f->ip = ip; 	ip_init_6(f);} voidiprouting(Fs *f, int on){	f->ip->iprouting = on;	if(f->ip->iprouting==0)		f->ip->stats[Forwarding] = 2;	else		f->ip->stats[Forwarding] = 1;} intipoput4(Fs *f, Block *bp, int gating, int ttl, int tos, Conv *c){	Ipifc *ifc;	uchar *gate;	ulong fragoff;	Block *xp, *nb;	Ip4hdr *eh, *feh;	int lid, len, seglen, chunk, dlen, blklen, offset, medialen;	Route *r, *sr;	IP *ip;	int rv = 0; 	ip = f->ip; 	/* Fill out the ip header */	eh = (Ip4hdr*)(bp->rp); 	ip->stats[OutRequests]++; 	/* Number of uchars in data and ip header to write */	len = blocklen(bp); 	if(gating){		chunk = nhgets(eh->length);		if(chunk > len){			ip->stats[OutDiscards]++;			netlog(f, Logip, "short gated packet\n");			goto free;		}		if(chunk < len)			len = chunk;	}	if(len >= IP_MAX){		ip->stats[OutDiscards]++;		netlog(f, Logip, "exceeded ip max size %V\n", eh->dst);		goto free;	} 	r = v4lookup(f, eh->dst, c);	if(r == nil){		ip->stats[OutNoRoutes]++;		netlog(f, Logip, "no interface %V\n", eh->dst);		rv = -1;		goto free;	} 	ifc = r->ifc;	if(r->type & (Rifc|Runi))		gate = eh->dst;	else	if(r->type & (Rbcast|Rmulti)) {		gate = eh->dst;		sr = v4lookup(f, eh->src, nil);		if(sr != nil && (sr->type & Runi))			ifc = sr->ifc;	}	else		gate = r->v4.gate; 	if(!gating)		eh->vihl = IP_VER4|IP_HLEN4;	eh->ttl = ttl;	if(!gating)		eh->tos = tos; 	if(!canrlock(ifc)){		ip->stats[OutCantLock]++;		goto free;	}	if(waserror()){		runlock(ifc);		nexterror();	}	if(ifc->m == nil)		goto raise; 	/* If we dont need to fragment just send it */	if(c && c->maxfragsize && c->maxfragsize < ifc->maxtu)		medialen = c->maxfragsize - ifc->m->hsize;	else		medialen = ifc->maxtu - ifc->m->hsize;	if(len <= medialen) {		if(!gating)			hnputs(eh->id, incref(&ip->id4));		hnputs(eh->length, len);		if(!gating){			eh->frag[0] = 0;			eh->frag[1] = 0;		}		eh->cksum[0] = 0;		eh->cksum[1] = 0;		hnputs(eh->cksum, ipcsum(&eh->vihl));		assert(bp->next == nil);		ifc->m->bwrite(ifc, bp, V4, gate);		runlock(ifc);		poperror();		return 0;	} if((eh->frag[0] & (IP_DF>>8)) && !gating) print("%V: DF set\n", eh->dst); 	if(eh->frag[0] & (IP_DF>>8)){		ip->stats[FragFails]++;		ip->stats[OutDiscards]++;		icmpcantfrag(f, bp, medialen);		netlog(f, Logip, "%V: eh->frag[0] & (IP_DF>>8)\n", eh->dst);		goto raise;	} 	seglen = (medialen - IP4HDR) & ~7;	if(seglen < 8){		ip->stats[FragFails]++;		ip->stats[OutDiscards]++;		netlog(f, Logip, "%V seglen < 8\n", eh->dst);		goto raise;	} 	dlen = len - IP4HDR;	xp = bp;	if(gating)		lid = nhgets(eh->id);	else		lid = incref(&ip->id4); 	offset = IP4HDR;	while(xp != nil && offset && offset >= BLEN(xp)) {		offset -= BLEN(xp);		xp = xp->next;	}	xp->rp += offset; 	if(gating)		fragoff = nhgets(eh->frag)<<3;	else		fragoff = 0;	dlen += fragoff;	for(; fragoff < dlen; fragoff += seglen) {		nb = allocb(IP4HDR+seglen);		feh = (Ip4hdr*)(nb->rp); 		memmove(nb->wp, eh, IP4HDR);		nb->wp += IP4HDR; 		if((fragoff + seglen) >= dlen) {			seglen = dlen - fragoff;			hnputs(feh->frag, fragoff>>3);		}		else			hnputs(feh->frag, (fragoff>>3)|IP_MF); 		hnputs(feh->length, seglen + IP4HDR);		hnputs(feh->id, lid); 		/* Copy up the data area */		chunk = seglen;		while(chunk) {			if(!xp) {				ip->stats[OutDiscards]++;				ip->stats[FragFails]++;				freeblist(nb);				netlog(f, Logip, "!xp: chunk %d\n", chunk);				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;		} 		feh->cksum[0] = 0;		feh->cksum[1] = 0;		hnputs(feh->cksum, ipcsum(&feh->vihl));		ifc->m->bwrite(ifc, nb, V4, gate);		ip->stats[FragCreates]++;	}	ip->stats[FragOKs]++;raise:	runlock(ifc);	poperror();free:	freeblist(bp);	return rv;} voidipiput4(Fs *f, Ipifc *ifc, Block *bp){	int hl;	int hop, tos, proto, olen;	Ip4hdr *h;	Proto *p;	ushort frag;	int notforme;	uchar *dp, v6dst[IPaddrlen];	IP *ip;	Route *r;	Conv conv; 	if(BLKIPVER(bp) != IP_VER4) {		ipiput6(f, ifc, bp);		return;	} 	ip = f->ip;	ip->stats[InReceives]++; 	/*	 *  Ensure we have all the header info in the first	 *  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 < IP4HDR)			hl = IP4HDR;		if(hl > 64)			hl = 64;		bp = pullupblock(bp, hl);		if(bp == nil)			return;	} 	h = (Ip4hdr*)(bp->rp); 	/* dump anything that whose header doesn't checksum */	if((bp->flag & Bipck) == 0 && ipcsum(&h->vihl)) {		ip->stats[InHdrErrors]++;		netlog(f, Logip, "ip: checksum error %V\n", h->src);		freeblist(bp);		return;	}	v4tov6(v6dst, h->dst);	notforme = ipforme(f, v6dst) == 0; 	/* Check header length and version */	if((h->vihl&0x0F) != IP_HLEN4) {		hl = (h->vihl&0xF)<<2;		if(hl < (IP_HLEN4<<2)) {			ip->stats[InHdrErrors]++;			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) {			olen = nhgets(h->length);			dp = bp->rp + (hl - (IP_HLEN4<<2));			memmove(dp, h, IP_HLEN4<<2);			bp->rp = dp;			h = (Ip4hdr*)(bp->rp);			h->vihl = (IP_VER4|IP_HLEN4);			hnputs(h->length, olen-hl+(IP_HLEN4<<2));		}	} 	/* route */	if(notforme) {		if(!ip->iprouting){			freeblist(bp);			return;		} 		/* don't forward to source's network */		memset(&conv, 0, sizeof conv);		r = v4lookup(f, h->dst, &conv);		if(r == nil || r->ifc == ifc){			ip->stats[OutDiscards]++;			freeblist(bp);			return;		} 		/* don't forward if packet has timed out */		hop = h->ttl;		if(hop < 1) {			ip->stats[InHdrErrors]++;			icmpttlexceeded(f, ifc->lifc->local, bp);			freeblist(bp);			return;		} 		/* reassemble if the interface expects it */if(r->ifc == nil) panic("nil route rfc");		if(r->ifc->reassemble){			frag = nhgets(h->frag);			if(frag) {				h->tos = 0;				if(frag & IP_MF)					h->tos = 1;				bp = ip4reassemble(ip, frag, bp, h);				if(bp == nil)					return;				h = (Ip4hdr*)(bp->rp);			}		} 		ip->stats[ForwDatagrams]++;		tos = h->tos;		hop = h->ttl;		ipoput4(f, bp, 1, hop - 1, tos, &conv);		return;	} 	frag = nhgets(h->frag);	if(frag) {		h->tos = 0;		if(frag & IP_MF)			h->tos = 1;		bp = ip4reassemble(ip, frag, bp, h);		if(bp == nil)			return;		h = (Ip4hdr*)(bp->rp);	} 	/* don't let any frag info go up the stack */	h->frag[0] = 0;	h->frag[1] = 0; 	proto = h->proto;	p = Fsrcvpcol(f, proto);	if(p != nil && p->rcv != nil) {		ip->stats[InDelivers]++;		(*p->rcv)(p, ifc, bp);		return;	}	ip->stats[InDiscards]++;	ip->stats[InUnknownProtos]++;	freeblist(bp);} intipstats(Fs *f, char *buf, int len){	IP *ip;	char *p, *e;	int i; 	ip = f->ip;	ip->stats[DefaultTTL] = MAXTTL; 	p = buf;	e = p+len;	for(i = 0; i < Nipstats; i++)		p = seprint(p, e, "%s: %llud\n", statnames[i], ip->stats[i]);	return p - buf;} Block*ip4reassemble(IP *ip, int offset, Block *bp, Ip4hdr *ih){	int fend;	ushort id;	Fragment4 *f, *fnext;	ulong src, dst;	Block *bl, **l, *last, *prev;	int ovlap, len, fragsize, pktposn; 	if(offset & IP_MBZ){		/* pedantic: check must-be-zero bit */		ip->stats[InHdrErrors]++;		freeblist(bp);		return nil;	} 	src = nhgetl(ih->src);	dst = nhgetl(ih->dst);	id = nhgets(ih->id); 	/*	 *  block lists are too hard, pullupblock into a single block	 */	if(bp->next){		bp = pullupblock(bp, blocklen(bp));		ih = (Ip4hdr*)(bp->rp);

Lines 1–500 of 717.