Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/ip/ethermedium.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname ip/ethermedium.c 1998/0306
## diff -e /dev/null /n/emeliedump/1998/0306/sys/src/brazil/ip/ethermedium.c
0a
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"

#include "ip.h"
#include "kernel.h"

typedef struct Etherhdr Etherhdr;
struct Etherhdr
{
	uchar	d[6];
	uchar	s[6];
	uchar	t[2];
};

static void	etherread(void *a);
static void	etherbind(Ipifc *ifc, int argc, char **argv);
static void	etherunbind(Ipifc *ifc);
static void	etherbwrite(Ipifc *ifc, Block *bp, int version, uchar *ip);
static void	etheraddmulti(Ipifc *ifc, uchar *a, uchar *ia);
static void	etherremmulti(Ipifc *ifc, uchar *a, uchar *ia);
static Block*	multicastarp(Arpent *a, uchar *mac);
static void	sendarp(Ipifc *ifc, Arpent *a);
static int	multicastea(uchar *ea, uchar *ip);
static void	recvarpproc(Ipifc *ifc);

Medium ethermedium =
{
	"ether",
	14,
	60,
	1514,
	6,
	etherbind,
	etherunbind,
	etherbwrite,
	etheraddmulti,
	etherremmulti,
	nil,			/* pktin */
	nil,			/* addroute */
	nil,			/* remroute */
	nil,			/* flushroute */
	nil,			/* joinmulti */
	nil,			/* leavemulti */
};

typedef struct	Etherrock Etherrock;
struct Etherrock
{
	Proc	*arpp;		/* arp process */
	Proc	*readp;		/* reading process */
	Chan	*mchan;		/* Data channel */
	Chan	*achan;		/* Arp channel */
	Chan	*cchan;		/* Control channel */
};

/*
 *  ethernet arp request
 */
enum
{
	ETARP		= 0x0806,
	ETIP		= 0x0800,
	ARPREQUEST	= 1,
	ARPREPLY	= 2,
};
typedef struct Etherarp Etherarp;
struct Etherarp
{
	uchar	d[6];
	uchar	s[6];
	uchar	type[2];
	uchar	hrd[2];
	uchar	pro[2];
	uchar	hln;
	uchar	pln;
	uchar	op[2];
	uchar	sha[6];
	uchar	spa[4];
	uchar	tha[6];
	uchar	tpa[4];
};


/*
 *  called to bind an IP ifc to an ethernet device
 *  called with ifc wlock'd
 */
static void
etherbind(Ipifc *ifc, int argc, char **argv)
{
	Chan *mchan, *cchan, *achan;
	char addr[2*NAMELEN];
	char dir[2*NAMELEN];
	char *buf;
	int fd, cfd, n;
	char *ptr;
	Etherrock *er;

	if(argc < 2)
		error(Ebadarg);

	mchan = cchan = achan = nil;
	buf = nil;
	if(waserror()){
		if(mchan != nil)
			cclose(mchan);
		if(cchan != nil)
			cclose(cchan);
		if(achan != nil)
			cclose(achan);
		if(buf != nil)
			free(buf);
		nexterror(); 
	}

	/*
	 *  open ip conversation
	 *
	 *  the dial will fail if the type is already open on
	 *  this device.
	 */
	snprint(addr, sizeof(addr), "%s!0x800", argv[2]);
	fd = kdial(addr, nil, dir, &cfd);
	if(fd < 0)
		error("dial 0x800 failed");
	mchan = fdtochan(fd, ORDWR, 0, 1);
	cchan = fdtochan(cfd, ORDWR, 0, 1);
	kclose(fd);
	kclose(cfd);

	/*
	 *  get mac address
	 */
	snprint(addr, sizeof(addr), "%s/stats", dir);
	fd = kopen(addr, OREAD);
	if(fd < 0)
		error("can't read ether stats");

	buf = smalloc(512);
	n = kread(fd, buf, 511);
	kclose(fd);
	if(n <= 0)
		error(Eio);
	buf[n] = 0;

	ptr = strstr(buf, "addr: ");
	if(!ptr)
		error(Eio);
	ptr += 6;

	parsemac(ifc->mac, ptr, 6);

	/*
 	 *  open arp conversation
	 */
	snprint(addr, sizeof(addr), "%s!0x806", argv[2]);
	fd = kdial(addr, nil, nil, nil);
	if(fd < 0)
		error("dial 0x806 failed");
	achan = fdtochan(fd, ORDWR, 0, 1);
	kclose(fd);

	er = smalloc(sizeof(*er));
	er->mchan = mchan;
	er->cchan = cchan;
	er->achan = achan;
	ifc->arg = er;

	free(buf);
	poperror();

	kproc("etherread", etherread, ifc);
	kproc("recvarpproc", recvarpproc, ifc);
}

/*
 *  called with ifc wlock'd
 */
static void
etherunbind(Ipifc *ifc)
{
	Etherrock *er = ifc->arg;

	if(er->readp)
		postnote(er->readp, 1, "unbind", 0);
	if(er->arpp)
		postnote(er->arpp, 1, "unbind", 0);

	/* wait for readers to die */
	while(er->arpp != 0 || er->readp != 0)
		tsleep(&up->sleep, return0, 0, 300);

	if(er->mchan != nil)
		cclose(er->mchan);
	if(er->achan != nil)
		cclose(er->achan);
	if(er->cchan != nil)
		cclose(er->cchan);

	free(er);
}

/*
 *  called by ipoput with a single block to write
 */
static void
etherbwrite(Ipifc *ifc, Block *bp, int version, uchar *ip)
{
	Etherhdr *eh;
	Arpent *a;
	uchar mac[6];
	Etherrock *er = ifc->arg;

	if(waserror()) {
		print("etherbwrite failed\n");
		ipifccheckout(ifc);
		return;
	}
	if(ipifccheckin(ifc, &ethermedium) < 0){
		freeb(bp);
		poperror();
		return;
	}

	/* get mac address of destination */
	a = arpget(bp, version, &ethermedium, ip, mac);
	if(a){
		/* check for broadcast or multicast */
		bp = multicastarp(a, mac);
		if(bp == nil){
			sendarp(ifc, a);
			goto out;
		}
	}

	/* make it a single block with space for the ether header */
	bp = padblock(bp, ifc->m->hsize);
	if(bp->next)
		bp = concatblock(bp);
	if(BLEN(bp) < ifc->minmtu)
		bp = adjustblock(bp, ifc->minmtu);
	eh = (Etherhdr*)bp->rp;

	/* copy in mac addresses and ether type */
	memmove(eh->s, ifc->mac, sizeof(eh->s));
	memmove(eh->d, mac, sizeof(eh->d));
	switch(version){
	case V4:
		eh->t[0] = 0x08;
		eh->t[1] = 0x00;
		break;
	case V6:
		eh->t[0] = 0x86;
		eh->t[1] = 0xDD;
		break;
	}

	devtab[er->mchan->type]->bwrite(er->mchan, bp, 0);
	ifc->out++;

out:
	ipifccheckout(ifc);
	poperror();
}

/*
 *  process to read from the ethernet
 */
static void
etherread(void *a)
{
	Ipifc *ifc;
	Block *bp;
	Etherrock *er;

	ifc = a;
	er = ifc->arg;
	er->readp = up;	/* hide identity under a rock for unbind */
	if(waserror()){
		er->readp = 0;
		pexit("hangup", 1);
	}
	for(;;){
		bp = devtab[er->mchan->type]->bread(er->mchan, ifc->maxmtu, 0);
		ifc->in++;
		bp->rp += ifc->m->hsize;
		if(ifc->lifc == nil)
			freeb(bp);
		else
			ipiput(ifc->lifc->local, bp);
	}
}

static void
etheraddmulti(Ipifc *ifc, uchar *a, uchar *)
{
	uchar mac[6];
	char buf[64];
	Etherrock *er = ifc->arg;

	multicastea(mac, a);
	sprint(buf, "addmulti %E", mac);
	devtab[er->cchan->type]->write(er->cchan, buf, strlen(buf), 0);
}

static void
etherremmulti(Ipifc *ifc, uchar *a, uchar *)
{
	uchar mac[6];
	char buf[64];
	Etherrock *er = ifc->arg;

	multicastea(mac, a);
	sprint(buf, "remmulti %E", mac);
	devtab[er->cchan->type]->write(er->cchan, buf, strlen(buf), 0);
}

/*
 *  send an ethernet arp
 *  (only v4, v6 uses the neighbor discovery, rfc1970)
 */
static void
sendarp(Ipifc *ifc, Arpent *a)
{
	int n;
	Block *bp;
	Etherarp *e;
	Etherrock *er = ifc->arg;

	/* don't do anything if it's been less than a second since the last */
	if(msec - a->time < 1000){
		arprelease(a);
		return;
	}

	/* remove all but the last message */
	while((bp = a->hold) != nil){
		if(bp == a->last)
			break;
		a->hold = bp->list;
		freeblist(bp);
	}

	/* try to keep it around for a second more */
	a->time = msec;
	arprelease(a);

	n = sizeof(Etherarp);
	if(n < a->type->minmtu)
		n = a->type->minmtu;
	bp = allocb(n);
	memset(bp->rp, 0, n);
	e = (Etherarp*)bp->rp;
	memmove(e->tpa, a->ip+IPv4off, sizeof(e->tpa));
	ipv4local(ifc, e->spa);
	memmove(e->sha, ifc->mac, sizeof(e->sha));
	memset(e->d, 0xff, sizeof(e->d));		/* ethernet broadcast */
	memmove(e->s, ifc->mac, sizeof(e->s));

	hnputs(e->type, ETARP);
	hnputs(e->hrd, 1);
	hnputs(e->pro, ETIP);
	e->hln = sizeof(e->sha);
	e->pln = sizeof(e->spa);
	hnputs(e->op, ARPREQUEST);
	bp->wp += n;

	n = devtab[er->achan->type]->bwrite(er->achan, bp, 0);
	if(n < 0)
		print("arp: send: %r\n");
}

static void
recvarp(Ipifc *ifc)
{
	int n;
	Block *ebp, *rbp;
	Etherarp *e, *r;
	uchar ip[IPaddrlen];
	Etherrock *er = ifc->arg;

	ebp = devtab[er->achan->type]->bread(er->achan, ifc->maxmtu, 0);
	if(ebp == nil) {
		print("arp: rcv: %r\n");
		return;
	}

	e = (Etherarp*)ebp->rp;
	switch(nhgets(e->op)) {
	default:
		break;

	case ARPREPLY:
		arpenter(ifc, V4, e->spa, e->sha, &ethermedium, 0);
		break;

	case ARPREQUEST:
		/* don't answer arps till we know who we are */
		if(ifc->lifc == 0)
			break;

		/* check for someone that think's they're me */
		v4tov6(ip, e->spa);
		if(iplocalonifc(ifc, ip)){
			if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0)
				print("arp: 0x%E also has ip addr %V\n", e->sha, e->spa);
		} else {
			if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) == 0){
				print("arp: %V also has ether addr %E\n", e->spa, e->sha);
				break;
			}
		}

		/* refresh what we know about sender */
		arpenter(ifc, V4, e->spa, e->sha, &ethermedium, 1);

		/* answer only requests for our address or systems we're proxying for */
		v4tov6(ip, e->tpa);
		if(!iplocalonifc(ifc, ip))
		if(ipproxyifc(ifc, ip) == 0)
			break;

/* print("arp: rem %I %E (for %I)\n", e->spa, e->sha, e->tpa); /**/

		n = sizeof(Etherarp);
		if(n < ifc->minmtu)
			n = ifc->minmtu;
		rbp = allocb(n);
		r = (Etherarp*)rbp->rp;
		memset(r, 0, sizeof(Etherarp));
		hnputs(r->type, ETARP);
		hnputs(r->hrd, 1);
		hnputs(r->pro, ETIP);
		r->hln = sizeof(r->sha);
		r->pln = sizeof(r->spa);
		hnputs(r->op, ARPREPLY);
		memmove(r->tha, e->sha, sizeof(r->tha));
		memmove(r->tpa, e->spa, sizeof(r->tpa));
		memmove(r->sha, ifc->mac, sizeof(r->sha));
		memmove(r->spa, e->tpa, sizeof(r->spa));
		memmove(r->d, e->sha, sizeof(r->d));
		memmove(r->s, ifc->mac, sizeof(r->s));
		rbp->wp += n;

		n = devtab[er->achan->type]->bwrite(er->achan, rbp, 0);
		if(n < 0)
			print("arp: write: %r\n");
	}
	freeb(ebp);
}

static void
recvarpproc(Ipifc *ifc)
{
	Etherrock *er = ifc->arg;

	er->arpp = up;
	if(waserror()){
		er->arpp = 0;
		pexit("hangup", 1);
	}
	for(;;)
		recvarp(ifc);
}

static int
multicastea(uchar *ea, uchar *ip)
{
	int x;

	switch(x = ipismulticast(ip)){
	case V4:
		ea[0] = 0x01;
		ea[1] = 0x00;
		ea[2] = 0x5e;
		ea[3] = ip[13] & 0x7f;
		ea[4] = ip[14];
		ea[5] = ip[15];
		break;
	case V6:
		ea[0] = 0x33;
		ea[1] = 0x33;
		ea[2] = ip[12];
		ea[3] = ip[13];
		ea[4] = ip[14];
		ea[5] = ip[15];
		break;
	}
	return x;
}

/*
 *  fill in an arp entry for broadcast or multicast
 *  addresses
 */
static Block*
multicastarp(Arpent *a, uchar *mac)
{
	/* is it broadcast? */
	switch(ipforme(a->ip)){
	case Runi:
		return nil;
	case Rbcast:
		memset(mac, 0xff, 6);
		return arpresolve(a, &ethermedium, mac);
	default:
		break;
	}

	/* if multicast, fill in mac */
	switch(multicastea(mac, a->ip)){
	case V4:
	case V6:
		return arpresolve(a, &ethermedium, mac);
	}

	/* let arp take care of it */
	return nil;
}
.
## diffname ip/ethermedium.c 1998/0307
## diff -e /n/emeliedump/1998/0306/sys/src/brazil/ip/ethermedium.c /n/emeliedump/1998/0307/sys/src/brazil/ip/ethermedium.c
294a
		runlock(ifc);	locked = 0;	USED(locked);

.
288a
		rlock(ifc);	locked = 1;	USED(locked);
.
283a
		if(locked)
			runlock(ifc);
.
278a
	int locked = 0;
.
264,267d
236c
			return;
.
218,228d
208c
 *  called by ipoput with a single block to write with ifc rlock'd
.
47a
	0,			/* don't unbind on last close */
.
## diffname ip/ethermedium.c 1998/0313
## diff -e /n/emeliedump/1998/0307/sys/src/brazil/ip/ethermedium.c /n/emeliedump/1998/0313/sys/src/brazil/ip/ethermedium.c
510c
		return arpresolve(f->arp, a, &ethermedium, mac);
.
501c
		return arpresolve(f->arp, a, &ethermedium, mac);
.
496c
	switch(ipforme(f, a->ip)){
.
493c
multicastarp(Fs *f, Arpent *a, uchar *mac)
.
416c
		if(ipproxyifc(er->f, ifc, ip) == 0)
.
411c
		arpenter(er->f->arp, ifc, V4, e->spa, e->sha, &ethermedium, 1);
.
390c
		arpenter(er->f->arp, ifc, V4, e->spa, e->sha, &ethermedium, 0);
.
342c
	arprelease(er->f->arp, a);
.
328c
		arprelease(er->f->arp, a);
.
284c
			ipiput(er->f, ifc->lifc->local, bp);
.
223c
		bp = multicastarp(er->f, a, mac);
.
220c
	a = arpget(er->f->arp, bp, version, &ethermedium, ip, mac);
.
171a
	er->f = ifc->conv->p->f;
.
165c
	achan = commonfdtochan(fd, ORDWR, 0, 1);
.
131,132c
	mchan = commonfdtochan(fd, ORDWR, 0, 1);
	cchan = commonfdtochan(cfd, ORDWR, 0, 1);
.
53a
	Fs	*f;		/* file system we belong to */
.
25c
static Block*	multicastarp(Fs *f, Arpent *a, uchar *mac);
.
## diffname ip/ethermedium.c 1998/0423
## diff -e /n/emeliedump/1998/0313/sys/src/brazil/ip/ethermedium.c /n/emeliedump/1998/0423/sys/src/brazil/ip/ethermedium.c
517a

void
ethermediumlink(void)
{
	addipmedium(&ethermedium);
}
.
## diffname ip/ethermedium.c 1998/0630
## diff -e /n/emeliedump/1998/0423/sys/src/brazil/ip/ethermedium.c /n/emeliedump/1998/0630/sys/src/brazil/ip/ethermedium.c
413c
		arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 1);
.
392c
		arpenter(er->f, V4, e->spa, e->sha, sizeof(e->sha), 0);
.
47a
	arpenter,		/* ares */
.
## diffname ip/ethermedium.c 1998/0808
## diff -e /n/emeliedump/1998/0630/sys/src/brazil/ip/ethermedium.c /n/emeliedump/1998/0808/sys/src/brazil/ip/ethermedium.c
289d
## diffname ip/ethermedium.c 1999/0302
## diff -e /n/emeliedump/1998/0808/sys/src/brazil/ip/ethermedium.c /n/emeliedump/1999/0302/sys/src/brazil/ip/ethermedium.c
288c
		runlock(ifc);
		poperror();
.
281c
		if(!canrlock(ifc)){
			freeb(bp);
			continue;
		}
		if(waserror()){
			runlock(ifc);
			nexterror();
		}
.
274,275d
268d
## diffname ip/ethermedium.c 1999/0320
## diff -e /n/emeliedump/1999/0302/sys/src/brazil/ip/ethermedium.c /n/emeliedump/1999/0320/sys/src/brazil/ip/ethermedium.c
457a
	Ipifc *ifc = v;
.
456c
recvarpproc(void *v)
.
28c
static void	recvarpproc(void*);
.
## diffname ip/ethermedium.c 1999/0731
## diff -e /n/emeliedump/1999/0320/sys/src/brazil/ip/ethermedium.c /n/emeliedump/1999/0731/sys/src/brazil/ip/ethermedium.c
423c
		if(!ipproxyifc(er->f, ifc, ip))
.
407c
		if(iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)){
.
376a
sendgarp(Ipifc *ifc, uchar *ip)
{
	int n;
	Block *bp;
	Etherarp *e;
	Etherrock *er = ifc->arg;

	/* don't arp for our initial non address */
	if(ipcmp(ip, IPnoaddr) == 0)
		return;

	n = sizeof(Etherarp);
	if(n < ethermedium.minmtu)
		n = ethermedium.minmtu;
	bp = allocb(n);
	memset(bp->rp, 0, n);
	e = (Etherarp*)bp->rp;
	memmove(e->tpa, ip+IPv4off, sizeof(e->tpa));
	memmove(e->spa, ip+IPv4off, sizeof(e->spa));
	memmove(e->sha, ifc->mac, sizeof(e->sha));
	memset(e->d, 0xff, sizeof(e->d));		/* ethernet broadcast */
	memmove(e->s, ifc->mac, sizeof(e->s));

	hnputs(e->type, ETARP);
	hnputs(e->hrd, 1);
	hnputs(e->pro, ETIP);
	e->hln = sizeof(e->sha);
	e->pln = sizeof(e->spa);
	hnputs(e->op, ARPREQUEST);
	bp->wp += n;

	n = devtab[er->achan->type]->bwrite(er->achan, bp, 0);
	if(n < 0)
		print("garp: send: %r\n");
}

static void
.
375a
/*
 *  send a gratuitous arp to refresh arp caches
 */
.
32,49c
.name=		"ether",
.hsize=		14,
.minmtu=	60,
.maxmtu=	1514,
.maclen=	6,
.bind=		etherbind,
.unbind=	etherunbind,
.bwrite=	etherbwrite,
.addmulti=	etheraddmulti,
.remmulti=	etherremmulti,
.ares=		arpenter,
.areg=		sendgarp,
.
26a
static void	sendgarp(Ipifc *ifc, uchar*);
.
## diffname ip/ethermedium.c 2000/0913
## diff -e /n/emeliedump/1999/0731/sys/src/brazil/ip/ethermedium.c /n/emeliedump/2000/0913/sys/src/9/ip/ethermedium.c
553c
		return arpresolve(f->arp, a, medium, mac);
.
544c
		return arpresolve(f->arp, a, medium, mac);
.
536c
multicastarp(Fs *f, Arpent *a, Medium *medium, uchar *mac)
.
387,388c
	if(n < ifc->m->minmtu)
		n = ifc->m->minmtu;
.
221c
		bp = multicastarp(er->f, a, ifc->m, mac);
.
218c
	a = arpget(er->f->arp, bp, version, ifc->m, ip, mac);
.
25c
static Block*	multicastarp(Fs *f, Arpent *a, Medium*, uchar *mac);
.
## diffname ip/ethermedium.c 2000/0922
## diff -e /n/emeliedump/2000/0913/sys/src/9/ip/ethermedium.c /n/emeliedump/2000/0922/sys/src/9/ip/ethermedium.c
563a
	addipmedium(&gbemedium);
.
46a
Medium gbemedium =
{
.name=		"gbe",
.hsize=		14,
.minmtu=	60,
.maxmtu=	9014,
.maclen=	6,
.bind=		etherbind,
.unbind=	etherunbind,
.bwrite=	etherbwrite,
.addmulti=	etheraddmulti,
.remmulti=	etherremmulti,
.ares=		arpenter,
.areg=		sendgarp,
};

.
## diffname ip/ethermedium.c 2000/1111
## diff -e /n/emeliedump/2000/0922/sys/src/9/ip/ethermedium.c /n/emeliedump/2000/1111/sys/src/9/ip/ethermedium.c
175,179c
	achan = chandial(addr, nil, nil, nil);
.
158,161c
	achan = namec(addr, Aopen, OREAD, 0);
	n = devtab[achan->type]->read(achan, buf, 511, 0);
	cclose(achan);
.
153,156d
141,147c
	mchan = chandial(addr, nil, dir, &cchan);
.
113c
	int n;
.
9d
## diffname ip/ethermedium.c 2001/0527
## diff -e /n/emeliedump/2000/1111/sys/src/9/ip/ethermedium.c /n/emeliedump/2001/0527/sys/src/9/ip/ethermedium.c
109,110c
	char addr[Maxpath];
	char dir[Maxpath];
.
## diffname ip/ethermedium.c 2001/0623
## diff -e /n/emeliedump/2001/0527/sys/src/9/ip/ethermedium.c /n/emeliedump/2001/0623/sys/src/9/ip/ethermedium.c
286c
			ipiput(er->f, ifc, bp);
.
## diffname ip/ethermedium.c 2002/0108
## diff -e /n/emeliedump/2001/0623/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0108/sys/src/9/ip/ethermedium.c
444a
				break;
			}
.
443c
			if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0){
.
431a
		if(iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)){
			if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0){
				print("arp: 0x%E also has ip addr %V\n", e->sha, e->spa);
				break;
			}
		}
.
## diffname ip/ethermedium.c 2002/0223
## diff -e /n/emeliedump/2002/0108/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0223/sys/src/9/ip/ethermedium.c
450c
				print("arpreq: 0x%E/0x%E also has ip addr %V\n",
					e->s, e->sha, e->spa);
.
434c
				print("arprep: 0x%E/0x%E also has ip addr %V\n",
					e->s, e->sha, e->spa);
.
## diffname ip/ethermedium.c 2002/0507
## diff -e /n/emeliedump/2002/0223/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0507/sys/src/9/ip/ethermedium.c
574a
}


static void 
etherpref2addr(uchar *pref, uchar *ea)
{
	pref[8]  = ea[0] | 0x2;
	pref[9]  = ea[1];
	pref[10] = ea[2];
	pref[11] = 0xFF;
	pref[12] = 0xFE;
	pref[13] = ea[3];
	pref[14] = ea[4];
	pref[15] = ea[5];
.
534,536c
 		ea[4] = ip[14];
 		ea[5] = ip[15];
 		break;
.
529,532c
 	case V6:
 		ea[0] = 0x33;
 		ea[1] = 0x33;
 		ea[2] = ip[12];
.
481c
		hnputs(r->pro, ETIP4);
.
471,472d
450,454c
			if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0)
				print("arp: 0x%E also has ip addr %V\n", e->sha, e->spa);
.
432,438d
400c
	hnputs(e->pro, ETIP4);
.
370a
static void
resolveaddr6(Ipifc *ifc, Arpent *a)
{
	int sflag;
	Block *bp;
	Etherrock *er = ifc->arg;
	uchar ipsrc[IPaddrlen];

	/* don't do anything if it's been less than a second since the last */
	if(msec - a->time < ReTransTimer){
		arprelease(er->f->arp, a);
		return;
	}

	/* remove all but the last message */
	while((bp = a->hold) != nil){
		if(bp == a->last)
			break;
		a->hold = bp->list;
		freeblist(bp);
	}

	/* try to keep it around for a second more */
	a->time = msec;
	a->rxtat = msec + ReTransTimer;
	if(a->rxtsrem <= 0) {
		arprelease(er->f->arp, a);
		return;
	}

	a->rxtsrem--;
	arprelease(er->f->arp, a);

	if(sflag = ipv6anylocal(ifc, ipsrc)) 
		icmpns(er->f, ipsrc, sflag, a->ip, TARG_MULTI, ifc->mac);
}

.
360c
	hnputs(e->pro, ETIP4);
.
313c
	if(type == V4) 
		devtab[er->cchan4->type]->write(er->cchan4, buf, strlen(buf), 0);
	else if(type == V6) 
		devtab[er->cchan6->type]->write(er->cchan6, buf, strlen(buf), 0);
.
311c
	type = multicastea(mac, a);
.
309a
	int type;
.
301c
	if(type == V4) 
		devtab[er->cchan4->type]->write(er->cchan4, buf, strlen(buf), 0);
	else if(type == V6) 
		devtab[er->cchan6->type]->write(er->cchan6, buf, strlen(buf), 0);
.
299c
	type = multicastea(mac, a);
.
297a
	int type;
.
292a
etherread6(void *a)
{
	Ipifc *ifc;
	Block *bp;
	Etherrock *er;

	ifc = a;
	er = ifc->arg;
	er->read6p = up;	/* hide identity under a rock for unbind */
	if(waserror()){
		er->read6p = 0;
		pexit("hangup", 1);
	}
	for(;;){
		bp = devtab[er->mchan6->type]->bread(er->mchan6, ifc->maxmtu, 0);
		if(!canrlock(ifc)){
			freeb(bp);
			continue;
		}
		if(waserror()){
			runlock(ifc);
			nexterror();
		}
		ifc->in++;
		bp->rp += ifc->m->hsize;
		if(ifc->lifc == nil)
			freeb(bp);
		else
			ipiput6(er->f, ifc, bp);
		runlock(ifc);
		poperror();
	}
}

static void
.
291a

/*
 *  process to read from the ethernet, IPv6
 */
.
286c
			ipiput4(er->f, ifc, bp);
.
272c
		bp = devtab[er->mchan4->type]->bread(er->mchan4, ifc->maxmtu, 0);
.
268c
		er->read4p = 0;
.
266c
	er->read4p = up;	/* hide identity under a rock for unbind */
.
258c
etherread4(void *a)
.
253a

.
249,250d
246a
		devtab[er->mchan6->type]->bwrite(er->mchan6, bp, 0);
.
242a
		devtab[er->mchan4->type]->bwrite(er->mchan4, bp, 0);
.
239c

 	switch(version){
.
222,223c
		if(bp==nil){
			if(version == V4) 
				sendarp(ifc, a);
			else 
				resolveaddr6(ifc, a);
.
218c
	a = arpget(er->f->arp, bp, version, ifc, ip, mac);
.
200,201c
	if(er->cchan4 != nil)
		cclose(er->cchan4);
	if(er->mchan6 != nil)
		cclose(er->mchan6);
	if(er->cchan6 != nil)
		cclose(er->cchan6);
.
196,197c
	if(er->mchan4 != nil)
		cclose(er->mchan4);
.
193c
	while(er->arpp != 0 || er->read4p != 0 || er->read6p != 0)
.
187,188c
	if(er->read4p)
		postnote(er->read4p, 1, "unbind", 0);
	if(er->read6p)
		postnote(er->read6p, 1, "unbind", 0);
.
176a
	kproc("etherread6", etherread6, ifc);
.
175c
	kproc("etherread4", etherread4, ifc);
.
168a
	er->mchan6 = mchan6;
	er->cchan6 = cchan6;
.
166,167c
	er->mchan4 = mchan4;
	er->cchan4 = cchan4;
.
164a
	/*
	 *  open ip conversation
	 *
	 *  the dial will fail if the type is already open on
	 *  this device.
	 */
	snprint(addr, sizeof(addr), "%s!0x86DD", argv[2]);
	mchan6 = chandial(addr, nil, dir, &cchan6);

.
140c
	mchan4 = chandial(addr, nil, dir, &cchan4);
.
127a
		if(mchan6 != nil)
			cclose(mchan6);
		if(cchan6 != nil)
			cclose(cchan6);
.
122,125c
		if(mchan4 != nil)
			cclose(mchan4);
		if(cchan4 != nil)
			cclose(cchan4);
.
119c
	mchan4 = cchan4 = achan = mchan6 = cchan6 = nil;
.
108,110c
	Chan *mchan4, *cchan4, *achan, *mchan6, *cchan6;
	char addr[Maxpath];	//char addr[2*KNAMELEN];
	char dir[Maxpath];	//char dir[2*KNAMELEN];
.
100d
82a

.
79c
	ETIP4		= 0x0800,
	ETIP6		= 0x86DD,
.
70c
	Chan	*cchan4;	/* Control channel for v4 */
	Chan	*mchan6;	/* Data channel for v6 */
	Chan	*cchan6;	/* Control channel for v6 */
.
67,68c
	Proc	*read4p;	/* reading process (v4)*/
	Proc	*read6p;	/* reading process (v6)*/
	Chan	*mchan4;	/* Data channel for v4 */
.
59a
.pref2addr=	etherpref2addr,
.
43a
.pref2addr=	etherpref2addr,
.
28a
static void	resolveaddr6(Ipifc *ifc, Arpent *a);
static void	etherpref2addr(uchar *pref, uchar *ea);
.
18c
static void	etherread4(void *a);
static void	etherread6(void *a);
.
8a
#include "ipv6.h"
.
## diffname ip/ethermedium.c 2002/0514
## diff -e /n/emeliedump/2002/0507/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0514/sys/src/9/ip/ethermedium.c
568c
				print("arpreq: %V also has ether addr %E\n", e->spa, e->sha);
.
565c
				print("arpreq: 0x%E also has ip addr %V\n", e->sha, e->spa);
.
561c
		/* check for machine using my ip or ether address */
.
552a
		/* check for machine using my ip address */
		v4tov6(ip, e->spa);
		if(iplocalonifc(ifc, ip) || ipproxyifc(er->f, ifc, ip)){
			if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0){
				print("arprep: 0x%E/0x%E also has ip addr %V\n",
					e->s, e->sha, e->spa);
				break;
			}
		}

.
## diffname ip/ethermedium.c 2002/0601
## diff -e /n/emeliedump/2002/0514/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0601/sys/src/9/ip/ethermedium.c
662c
 *  addresses.  Return the first queued packet for the
 *  IP address.
.
397a
		break;
	default:
		panic("etherremmulti: version %d", version);
	}
.
396c
		break;
	case V6:
.
394c
	switch(version){
	case V4:
.
392c
	version = multicastea(mac, a);
.
390c
	int version;
.
381a
		break;
	default:
		panic("etheraddmulti: version %d", version);
	}
.
380c
		break;
	case V6:
.
378c
	switch(version){
	case V4:
.
376c
	version = multicastea(mac, a);
.
374c
	int version;
.
285a
	default:
		panic("etherbwrite2: version %d", version);
.
258a
				break;
			default:
				panic("etherbwrite: version %d", version);
			}
.
257c
				break;
			case V6: 
.
255c
			switch(version){
			case V4:
.
## diffname ip/ethermedium.c 2002/0704
## diff -e /n/emeliedump/2002/0601/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0704/sys/src/9/ip/ethermedium.c
161,163c
	schan = namec(addr, Aopen, OREAD, 0);
	if(waserror()){
		cclose(schan);
		nexterror();
	}
	n = devtab[schan->type]->read(schan, buf, 511, 0);
	cclose(schan);
	poperror();
.
118c
	Chan *mchan4, *cchan4, *achan, *mchan6, *cchan6, *schan;
.
## diffname ip/ethermedium.c 2002/0710
## diff -e /n/emeliedump/2002/0704/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0710/sys/src/9/ip/ethermedium.c
503,504c
	a->time = NOW;
	a->rxtat = NOW + ReTransTimer;
.
489c
	if(NOW - a->time < ReTransTimer){
.
452c
	a->time = NOW;
.
438c
	if(NOW - a->time < 1000){
.
## diffname ip/ethermedium.c 2002/0712
## diff -e /n/emeliedump/2002/0710/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0712/sys/src/9/ip/ethermedium.c
191a

	/*
	 *  make it non-blocking
	 */
	devtab[cchan6->type]->write(cchan6, nbmsg, strlen(nbmsg), 0);
.
156a
	 *  make it non-blocking
	 */
	devtab[cchan4->type]->write(cchan4, nbmsg, strlen(nbmsg), 0);

	/*
.
148c
	 *  open ip converstation
.
110a
static char *nbmsg = "nonblocking";

.
## diffname ip/ethermedium.c 2002/0920
## diff -e /n/emeliedump/2002/0712/sys/src/9/ip/ethermedium.c /n/emeliedump/2002/0920/sys/src/9/ip/ethermedium.c
611,612c
			if(memcmp(e->sha, ifc->mac, sizeof(e->sha)) != 0){
				if (memcmp(eprinted, e->spa, sizeof(e->spa))){
					/* print only once */
					print("arpreq: 0x%E also has ip addr %V\n", e->sha, e->spa);
					memmove(eprinted, e->spa, sizeof(e->spa));
				}
			}
.
575a
	static uchar eprinted[4];
.
## diffname ip/ethermedium.c 2003/0209
## diff -e /n/emeliedump/2002/0920/sys/src/9/ip/ethermedium.c /n/emeliedump/2003/0209/sys/src/9/ip/ethermedium.c
636,637c
		if(n < ifc->mintu)
			n = ifc->mintu;
.
579c
	ebp = devtab[er->achan->type]->bread(er->achan, ifc->maxtu, 0);
.
545,546c
	if(n < ifc->m->mintu)
		n = ifc->m->mintu;
.
468,469c
	if(n < a->type->mintu)
		n = a->type->mintu;
.
373c
		bp = devtab[er->mchan6->type]->bread(er->mchan6, ifc->maxtu, 0);
.
334c
		bp = devtab[er->mchan4->type]->bread(er->mchan4, ifc->maxtu, 0);
.
290,291c
	if(BLEN(bp) < ifc->mintu)
		bp = adjustblock(bp, ifc->mintu);
.
184a
	ptr = strstr(buf, "mbps: ");
	if(ptr){
		ptr += 6;
		ifc->mbps = atoi(ptr);
	} else
		ifc->mbps = 100;

.
182d
164c
	 *  get mac address and speed
.
55,56c
.mintu=		60,
.maxtu=		9014,
.
38,39c
.mintu=		60,
.maxtu=		1514,
.
## diffname ip/ethermedium.c 2003/0310
## diff -e /n/emeliedump/2003/0209/sys/src/9/ip/ethermedium.c /n/emeliedump/2003/0310/sys/src/9/ip/ethermedium.c
521,522c
	a->ctime = NOW;
	a->rtime = NOW + ReTransTimer;
.
507c
	if(NOW - a->ctime < ReTransTimer){
.
470c
	a->ctime = NOW;
.
456c
	if(NOW - a->ctime < 1000){
.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].