Plan 9 from Bell Labs’s /usr/web/sources/contrib/lufia/plan9/sys/src/9/ip/il.c

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


#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

#include	"ip.h"

enum				/* Connection state */
{
	Ilclosed,
	Ilsyncer,
	Ilsyncee,
	Ilestablished,
	Illistening,
	Ilclosing,
	Ilopening,		/* only for file server */
};

char	*ilstates[] = 
{ 
	"Closed",
	"Syncer",
	"Syncee",
	"Established",
	"Listen",
	"Closing",
	"Opening",		/* only for file server */
};

enum				/* Packet types */
{
	Ilsync,
	Ildata,
	Ildataquery,
	Ilack,
	Ilquery,
	Ilstate,
	Ilclose,
};

char	*iltype[] = 
{	
	"sync",
	"data",
	"dataquery",
	"ack",
	"query",
	"state",
	"close" 
};

enum
{
	Seconds		= 1000,
	Iltickms 	= 50,		/* time base */
	AckDelay	= 2*Iltickms,	/* max time twixt message rcvd & ack sent */
	MaxTimeout 	= 30*Seconds,	/* max time between rexmit */
	QueryTime	= 10*Seconds,	/* time between subsequent queries */
	DeathTime	= 30*QueryTime,

	MaxRexmit 	= 16,		/* max retransmissions before hangup */
	Defaultwin	= 20,

	LogAGain	= 3,
	AGain		= 1<<LogAGain,
	LogDGain	= 2,
	DGain		= 1<<LogDGain,

	DefByteRate	= 100,		/* assume a megabit link */
	DefRtt		= 50,		/* cross country on a great day */

	Maxrq		= 64*1024,
};

enum
{
	Nqt=	8,
};

typedef struct Ilcb Ilcb;
struct Ilcb			/* Control block */
{
	int	state;		/* Connection state */
	Conv	*conv;
	QLock	ackq;		/* Unacknowledged queue */
	Block	*unacked;
	Block	*unackedtail;
	ulong	unackedbytes;
	QLock	outo;		/* Out of order packet queue */
	Block	*outoforder;
	ulong	next;		/* Id of next to send */
	ulong	recvd;		/* Last packet received */
	ulong	acksent;	/* Last packet acked */
	ulong	start;		/* Local start id */
	ulong	rstart;		/* Remote start id */
	int	window;		/* Maximum receive window */
	int	rxquery;	/* number of queries on this connection */
	int	rxtot;		/* number of retransmits on this connection */
	int	rexmit;		/* number of retransmits of *unacked */
	ulong	qt[Nqt+1];	/* state table for query messages */
	int	qtx;		/* ... index into qt */

	/* if set, fasttimeout causes a connection request to terminate after 4*Iltickms */
	int	fasttimeout;

	/* timers */
	ulong	lastxmit;	/* time of last xmit */
	ulong	lastrecv;	/* time of last recv */
	ulong	timeout;	/* retransmission time for *unacked */
	ulong	acktime;	/* time to send next ack */
	ulong	querytime;	/* time to send next query */

	/* adaptive measurements */
	int	delay;		/* Average of the fixed rtt delay */
	int	rate;		/* Average uchar rate */
	int	mdev;		/* Mean deviation of rtt */
	int	maxrtt;		/* largest rtt seen */
	ulong	rttack;		/* The ack we are waiting for */
	int	rttlen;		/* Length of rttack packet */
	uvlong	rttstart;	/* Time we issued rttack packet */
};

enum
{
	IL4_IPSIZE 	= 20,
	IL6_IPSIZE	= 40,
	IL_HDRSIZE	= 18,	
	IL_LISTEN	= 0,
	IL_CONNECT	= 1,
	IP_ILPROTO	= 40,
};

typedef struct Il4hdr Il4hdr;
struct Il4hdr
{
	uchar	vihl;		/* Version and header length */
	uchar	tos;		/* Type of service */
	uchar	length[2];	/* packet length */
	uchar	id[2];		/* Identification */
	uchar	frag[2];	/* Fragment information */
	uchar	ttl;		/* Time to live */
	uchar	proto;		/* Protocol */
	uchar	cksum[2];	/* Header checksum */
	uchar	src[IPv4addrlen];	/* Ip source */
	uchar	dst[IPv4addrlen];	/* Ip destination */

	/* il header */
	uchar	ilsum[2];	/* Checksum including header */
	uchar	illen[2];	/* Packet length */
	uchar	iltype;		/* Packet type */
	uchar	ilspec;		/* Special */
	uchar	ilsrc[2];	/* Src port */
	uchar	ildst[2];	/* Dst port */
	uchar	ilid[4];	/* Sequence id */
	uchar	ilack[4];	/* Acked sequence */
};

typedef struct Il6hdr Il6hdr;
struct Il6hdr
{
	uchar	viclfl[4];
	uchar	len[2];
	uchar	proto;
	uchar	ttl;
	uchar	src[IPaddrlen];
	uchar	dst[IPaddrlen];

	/* il header */
	uchar	ilsum[2];
	uchar	illen[2];
	uchar	iltype;
	uchar	ilspec;
	uchar	ilsrc[2];
	uchar	ildst[2];
	uchar	ilid[4];
	uchar	ilack[4];
};

enum
{
	InMsgs,
	OutMsgs,
	CsumErrs,		/* checksum errors */
	HlenErrs,		/* header length error */
	LenErrs,		/* short packet */
	OutOfOrder,		/* out of order */
	Retrans,		/* retransmissions */
	DupMsg,
	DupBytes,
	DroppedMsgs,

	Nstats,
};

static char *statnames[] =
{
[InMsgs]	"InMsgs",
[OutMsgs]	"OutMsgs",
[CsumErrs]	"CsumErrs",
[HlenErrs]	"HlenErr",
[LenErrs]	"LenErrs",
[OutOfOrder]	"OutOfOrder",
[Retrans]	"Retrans",
[DupMsg]	"DupMsg",
[DupBytes]	"DupBytes",
[DroppedMsgs]	"DroppedMsgs",
};

typedef struct Ilpriv Ilpriv;
struct Ilpriv
{
	Ipht	ht;

	ulong	stats[Nstats];

	ulong	csumerr;		/* checksum errors */
	ulong	hlenerr;		/* header length error */
	ulong	lenerr;			/* short packet */
	ulong	order;			/* out of order */
	ulong	rexmit;			/* retransmissions */
	ulong	dup;
	ulong	dupb;

	/* keeping track of the ack kproc */
	int	ackprocstarted;
	QLock	apl;
};

/* state for query/dataquery messages */


void	ilrcvmsg(Conv*, Block*);
void	ilsendctl(Conv*, Il4hdr*, int, ulong, ulong, int, uchar);
void	ilackq(Ilcb*, Block*);
void	ilprocess(Conv*, Il4hdr*, Block*);
void	ilpullup(Conv*);
void	ilhangup(Conv*, char*);
void	ilfreeq(Ilcb*);
void	ilrexmit(Ilcb*);
void	ilbackoff(Ilcb*);
void	ilsettimeout(Ilcb*);
char*	ilstart(Conv*, int, int);
void	ilackproc(void*);
void	iloutoforder(Conv*, Il4hdr*, Block*);
void	iliput(Proto*, Ipifc*, Block*);
void	iladvise(Proto*, Block*, char*);
int	ilnextqt(Ilcb*);
void	ilcbinit(Ilcb*);
int	later(ulong, ulong, char*);
void	ilreject(Fs*, Il4hdr*);
void	illocalclose(Conv *c);
	int 	ilcksum = 1;
static 	int 	initseq = 25001;
static	ulong	scalediv, scalemul;
static	char	*etime = "connection timed out";

static char*
ilconnect(Conv *c, char **argv, int argc)
{
	char *e, *p;
	int fast;

	/* huge hack to quickly try an il connection */
	fast = 0;
	if(argc > 1){
		p = strstr(argv[1], "!fasttimeout");
		if(p != nil){
			*p = 0;
			fast = 1;
		}
	}

	e = Fsstdconnect(c, argv, argc);
	if(e != nil)
		return e;
	return ilstart(c, IL_CONNECT, fast);
}

static int
ilstate(Conv *c, char *state, int n)
{
	Ilcb *ic;

	ic = (Ilcb*)(c->ptcl);
	return snprint(state, n, "%s qin %d qout %d del %5.5d Br %5.5d md %5.5d una %5.5lud rex %5.5d rxq %5.5d max %5.5d\n",
		ilstates[ic->state],
		c->rq ? qlen(c->rq) : 0,
		c->wq ? qlen(c->wq) : 0,
		ic->delay>>LogAGain, ic->rate>>LogAGain, ic->mdev>>LogDGain,
		ic->unackedbytes, ic->rxtot, ic->rxquery, ic->maxrtt);
}

static int
ilinuse(Conv *c)
{
	Ilcb *ic;

	ic = (Ilcb*)(c->ptcl);
	return ic->state != Ilclosed;

}

/* called with c locked */
static char*
ilannounce(Conv *c, char **argv, int argc)
{
	char *e;

	e = Fsstdannounce(c, argv, argc);
	if(e != nil)
		return e;
	e = ilstart(c, IL_LISTEN, 0);
	if(e != nil)
		return e;
	Fsconnected(c, nil);

	return nil;
}

void
illocalclose(Conv *c)
{
	Ilcb *ic;
	Ilpriv *ipriv;

	ipriv = c->p->priv;
	ic = (Ilcb*)c->ptcl;
	ic->state = Ilclosed;
	iphtrem(&ipriv->ht, c);
	ipmove(c->laddr, IPnoaddr);
	c->lport = 0;
}

static void
ilclose(Conv *c)
{
	Ilcb *ic;

	ic = (Ilcb*)c->ptcl;

	qclose(c->rq);
	qclose(c->wq);
	qclose(c->eq);

	switch(ic->state) {
	case Ilclosing:
	case Ilclosed:
		break;
	case Ilsyncer:
	case Ilsyncee:
	case Ilestablished:
		ic->state = Ilclosing;
		ilsettimeout(ic);
		ilsendctl(c, nil, Ilclose, ic->next, ic->recvd, 0, c->ipversion);
		break;
	case Illistening:
		illocalclose(c);
		break;
	}
	ilfreeq(ic);
}

void
ilkick(void *x, Block *bp)
{
	Conv *c = x;
	Il4hdr *ih4;
	Il6hdr *ih6;
	Ilcb *ic;
	int dlen;
	ulong id, ack;
	Fs *f;
	Ilpriv *priv;
	uchar version;
	ushort hdrlen;
	int (*ipoput)(Fs*, Block*, int, int, int, Conv*);

	f = c->p->f;
	priv = c->p->priv;
	version = c->ipversion;
	ic = (Ilcb*)c->ptcl;

	if(bp == nil)
		return;

	switch(ic->state) {
	case Ilclosed:
	case Illistening:
	case Ilclosing:
		freeblist(bp);
		qhangup(c->rq, nil);
		return;
	}

	dlen = blocklen(bp);

	switch(version){
	case V4:
		hdrlen = IL4_IPSIZE;
		ipoput = ipoput4;

		/* Make space to fit il & ip */
		bp = padblock(bp, IL4_IPSIZE+IL_HDRSIZE);
		ih4 = (Il4hdr *)(bp->rp);
		ih4->vihl = IP_VER4;

		/* Ip fields */
		ih4->frag[0] = 0;
		ih4->frag[1] = 0;
		v6tov4(ih4->dst, c->raddr);
		v6tov4(ih4->src, c->laddr);
		ih4->proto = IP_ILPROTO;

		/* Il fields */
		hnputs(ih4->illen, dlen+IL_HDRSIZE);
		hnputs(ih4->ilsrc, c->lport);
		hnputs(ih4->ildst, c->rport);

		qlock(&ic->ackq);
		id = ic->next++;
		hnputl(ih4->ilid, id);
		ack = ic->recvd;
		hnputl(ih4->ilack, ack);
		ic->acksent = ack;
		ic->acktime = NOW + AckDelay;
		ih4->iltype = Ildata;
		ih4->ilspec = 0;
		ih4->ilsum[0] = 0;
		ih4->ilsum[1] = 0;

		/* Checksum of ilheader plus data (not ip & no pseudo header) */
		if(ilcksum)
			hnputs(ih4->ilsum, ptclcsum(bp, IL4_IPSIZE, dlen+IL_HDRSIZE));
		break;
	case V6:
		hdrlen = IL6_IPSIZE;
		ipoput = ipoput6;

		bp = padblock(bp, IL6_IPSIZE+IL_HDRSIZE);
		ih6 = (Il6hdr *)(bp->rp);

		/* pseudo header for checksum */
		memset(ih6, 0, 8);
		hnputl(ih6->viclfl, dlen+IL_HDRSIZE);
		ih6->ttl = IP_ILPROTO;
		ipmove(ih6->dst, c->raddr);
		ipmove(ih6->src, c->laddr);
		hnputs(ih6->illen, dlen+IL_HDRSIZE);
		hnputs(ih6->ilsrc, c->lport);
		hnputs(ih6->ildst, c->rport);
		qlock(&ic->ackq);
		id = ic->next++;
		hnputl(ih6->ilid, id);
		ack = ic->recvd;
		hnputl(ih6->ilack, ack);
		ic->acksent = ack;
		ic->acktime = NOW + AckDelay;
		ih6->iltype = Ildata;
		ih6->ilspec = 0;
		ih6->ilsum[0] = 0;
		ih6->ilsum[1] = 0;
		hnputs(ih6->ilsum,
			ptclcsum(bp, 0, dlen+IL_HDRSIZE+IL6_IPSIZE));

		memset(ih6, 0, 8);
		ih6->viclfl[0] = IP_VER6;
		hnputs(ih6->len, dlen+IL_HDRSIZE);
		ih6->proto = IP_ILPROTO;
		break;
	default:
		panic("ilkick: version %d", version);
		return;
	}

	ilackq(ic, bp);
	qunlock(&ic->ackq);

	/* Start the round trip timer for this packet if the timer is free */
	if(ic->rttack == 0) {
		ic->rttack = id;
		ic->rttstart = fastticks(nil);
		ic->rttlen = dlen + hdrlen + IL_HDRSIZE;
	}

	if(later(NOW, ic->timeout, nil))
		ilsettimeout(ic);
	ipoput(f, bp, 0, c->ttl, c->tos, c);
	priv->stats[OutMsgs]++;
}

static void
ilcreate(Conv *c)
{
	c->rq = qopen(Maxrq, 0, 0, c);
	c->wq = qbypass(ilkick, c);
}

int
ilxstats(Proto *il, char *buf, int len)
{
	Ilpriv *priv;
	char *p, *e;
	int i;

	priv = il->priv;
	p = buf;
	e = p+len;
	for(i = 0; i < Nstats; i++)
		p = seprint(p, e, "%s: %lud\n", statnames[i], priv->stats[i]);
	return p - buf;
}

void
ilackq(Ilcb *ic, Block *bp)
{
	Block *np;
	int n;

	n = blocklen(bp);

	/* Enqueue a copy on the unacked queue in case this one gets lost */
	np = copyblock(bp, n);
	if(ic->unacked)
		ic->unackedtail->list = np;
	else
		ic->unacked = np;
	ic->unackedtail = np;
	np->list = nil;
	ic->unackedbytes += n;
}

static
void
ilrttcalc(Ilcb *ic, Block *bp)
{
	Il4hdr *h4;
	uchar version;
	int rtt, tt, pt, delay, rate;

	rtt = fastticks(nil) - ic->rttstart;
	rtt = (rtt*scalemul)/scalediv;
	delay = ic->delay;
	rate = ic->rate;

	/* Guard against zero wrap */
	if(rtt > 120000 || rtt < 0)
		return;

	/* this block had to be transmitted after the one acked so count its size */
	h4 = (Il4hdr *)(bp->rp);
	version = ((h4->vihl&0xF0)==IP_VER6) ? V6 : V4;
	switch(version){
	case V4:
		ic->rttlen += blocklen(bp)  + IL4_IPSIZE + IL_HDRSIZE;
		break;
	case V6:
		ic->rttlen += blocklen(bp)  + IL6_IPSIZE + IL_HDRSIZE;
		break;
	default:
		panic("ilrttcalc: version %d", version);
	}

	if(ic->rttlen < 256){
		/* guess fixed delay as rtt of small packets */
		delay += rtt - (delay>>LogAGain);
		if(delay < AGain)
			delay = AGain;
		ic->delay = delay;
	} else {
		/* if packet took longer than avg rtt delay, recalc rate */
		tt = rtt - (delay>>LogAGain);
		if(tt > 0){
			rate += ic->rttlen/tt - (rate>>LogAGain);
			if(rate < AGain)
				rate = AGain;
			ic->rate = rate;
		}
	}

	/* mdev */
	pt = ic->rttlen/(rate>>LogAGain) + (delay>>LogAGain);
	ic->mdev += abs(rtt-pt) - (ic->mdev>>LogDGain);

	if(rtt > ic->maxrtt)
		ic->maxrtt = rtt;
}

void
ilackto(Ilcb *ic, ulong ackto, Block *bp)
{
	Il4hdr *h4;
	Il6hdr *h6;
	ulong id;
	uchar version;

	if(ic->rttack == ackto)
		ilrttcalc(ic, bp);

	/* Cancel if we've passed the packet we were interested in */
	if(ic->rttack <= ackto)
		ic->rttack = 0;

	qlock(&ic->ackq);
	while(ic->unacked) {
		h4 = (Il4hdr *)ic->unacked->rp;
		version = ((h4->vihl&0xF0)==IP_VER6) ? V6 : V4;
		switch(version){
		case V4:
			id = nhgetl(h4->ilid);
			break;
		case V6:
			h6 = (Il6hdr *)ic->unacked->rp;
			id = nhgetl(h6->ilid);
			break;
		default:
			panic("ilackto: version %d", version);
			return;
		}
		if(ackto < id)
			break;

		bp = ic->unacked;
		ic->unacked = bp->list;
		bp->list = nil;
		ic->unackedbytes -= blocklen(bp);
		freeblist(bp);
		ic->rexmit = 0;
		ilsettimeout(ic);
	}
	qunlock(&ic->ackq);
}

void
iliput(Proto *il, Ipifc*, Block *bp)
{
	char *st;
	Ilcb *ic;
	Il4hdr *ih4;
	Il6hdr *ih6;
	uchar raddr[IPaddrlen];
	uchar laddr[IPaddrlen];
	ushort sp, dp, csum;
	int plen, illen;
	Conv *new, *s;
	Ilpriv *ipriv;
	uchar version, type;
	int ottl, oviclfl, olen;
	ulong id, ack;

	ipriv = il->priv;

	ih4 = (Il4hdr *)bp->rp;
	version = ((ih4->vihl&0xF0)==IP_VER6) ? V6 : V4;

	switch(version) {
	case V4:
		plen = blocklen(bp);
		if(plen < IL4_IPSIZE+IL_HDRSIZE){
			netlog(il->f, Logil, "il: hlenerr\n");
			ipriv->stats[HlenErrs]++;
			goto raise;
		}

		illen = nhgets(ih4->illen);
		if(illen+IL4_IPSIZE > plen){
			netlog(il->f, Logil, "il: lenerr\n");
			ipriv->stats[LenErrs]++;
			goto raise;
		}

		sp = nhgets(ih4->ildst);
		dp = nhgets(ih4->ilsrc);
		v4tov6(raddr, ih4->src);
		v4tov6(laddr, ih4->dst);
		id = nhgetl(ih4->ilid);
		ack = nhgetl(ih4->ilack);
		type = ih4->iltype;

		if((csum = ptclcsum(bp, IL4_IPSIZE, illen)) != 0) {
			if(type > Ilclose)
				st = "?";
			else
				st = iltype[type];
			ipriv->stats[CsumErrs]++;
			netlog(il->f, Logil, "il: cksum %ux, pkt(%s id %uld ack %uld %I/%d->%d)\n",
				csum, st, id, ack, raddr, sp, dp);
			goto raise;
		}
		break;
	case V6:
		ih6 = (Il6hdr*)(bp->rp);
		plen = blocklen(bp);
		if(plen < IL6_IPSIZE+IL_HDRSIZE){
			netlog(il->f, Logil, "il: hlenerr\n");
			ipriv->stats[HlenErrs]++;
			goto raise;
		}

		illen = nhgets(ih6->illen);
		if(illen+IL6_IPSIZE > plen){
			netlog(il->f, Logil, "il: lenerr\n");
			ipriv->stats[LenErrs]++;
			goto raise;
		}
		oviclfl = nhgetl(ih6->viclfl);
		olen = nhgets(ih6->len);
		ottl = ih6->ttl;
		sp = nhgets(ih6->ildst);
		dp = nhgets(ih6->ilsrc);
		ipmove(raddr, ih6->src);
		ipmove(laddr, ih6->dst);
		id = nhgetl(ih6->ilid);
		ack = nhgetl(ih6->ilack);
		type = ih6->iltype;

		memset(ih6, 0, 8);
		hnputl(ih6->viclfl, illen);
		ih6->ttl = IP_ILPROTO;
		if((csum = ptclcsum(bp, 0, illen+IL6_IPSIZE)) != 0) {
			if(type > Ilclose)
				st = "?";
			else
				st = iltype[type];
			ipriv->stats[CsumErrs]++;
			netlog(il->f, Logil, "il: cksum %ux, pkt(%s id %uld ack %uld %I/%d->%d)\n",
				csum, st, id, ack, raddr, sp, dp);
			goto raise;
		}
		hnputl(ih6->viclfl, oviclfl);
		hnputs(ih6->len, olen);
		ih6->proto = IP_ILPROTO;
		ih6->ttl = ottl;
		break;
	default:
		panic("iliput: version %d", version);
		return;	/* to avoid a warning */
	}

	qlock(il);
	s = iphtlook(&ipriv->ht, raddr, dp, laddr, sp);
	if(s == nil){
		if(type == Ilsync)
			ilreject(il->f, ih4);		/* no listener */
		qunlock(il);
		goto raise;
	}

	ic = (Ilcb*)s->ptcl;
	if(ic->state == Illistening){
		if(type != Ilsync){
			qunlock(il);
			if(type > Ilclose)
				st = "?";
			else
				st = iltype[type];
			ilreject(il->f, ih4);		/* no channel and not sync */
			netlog(il->f, Logil, "il: no channel, pkt(%s id %uld ack %uld %I/%ud->%ud)\n",
				st, id, ack, raddr, sp, dp); 
			goto raise;
		}

		new = Fsnewcall(s, raddr, dp, laddr, sp, version);
		if(new == nil){
			qunlock(il);
			netlog(il->f, Logil, "il: bad newcall %I/%ud->%ud\n", raddr, sp, dp);
			ilsendctl(s, ih4, Ilclose, 0, id, 0, version);
			goto raise;
		}
		s = new;

		ic = (Ilcb*)s->ptcl;
	
		ic->conv = s;
		ic->state = Ilsyncee;
		ilcbinit(ic);
		ic->rstart = id;
		iphtadd(&ipriv->ht, s);
	}

	qlock(s);
	qunlock(il);
	if(waserror()){
		qunlock(s);
		nexterror();
	}
	ilprocess(s, ih4, bp);
	qunlock(s);
	poperror();
	return;
raise:
	freeblist(bp);
}

void
_ilprocess(Conv *s, Il4hdr *h4, Block *bp)
{
	Ilcb *ic;
	ulong id, ack;
	uchar type;
	Ilpriv *priv;
	uchar version, *spec;
	Il6hdr *h6;

	version = ((h4->vihl&0xF0)==IP_VER6) ? V6 : V4;
	switch(version){
	case V4:
		id = nhgetl(h4->ilid);
		ack = nhgetl(h4->ilack);
		type = h4->iltype;
		spec = &h4->ilspec;
		break;
	case V6:
		h6 = (Il6hdr *)h4;
		id = nhgetl(h6->ilid);
		ack = nhgetl(h6->ilack);
		type = h6->iltype;
		spec = &h6->ilspec;
		break;
	default:
		panic("_ilprocess: version %d", version);
		return;
	}

	ic = (Ilcb*)s->ptcl;

	ic->lastrecv = NOW;
	ic->querytime = NOW + QueryTime;
	priv = s->p->priv;
	priv->stats[InMsgs]++;

	switch(ic->state) {
	default:
		netlog(s->p->f, Logil, "il: unknown state %d\n", ic->state);
	case Ilclosed:
		freeblist(bp);
		break;
	case Ilsyncer:
		switch(type) {
		default:
			break;
		case Ilsync:
			if(ack != ic->start)
				ilhangup(s, "connection rejected");
			else {
				ic->recvd = id;
				ic->rstart = id;
				ilsendctl(s, nil, Ilack, ic->next, ic->recvd, 0, version);
				ic->state = Ilestablished;
				ic->fasttimeout = 0;
				ic->rexmit = 0;
				Fsconnected(s, nil);
				ilpullup(s);
			}
			break;
		case Ilclose:
			if(ack == ic->start)
				ilhangup(s, "connection rejected");
			break;
		}
		freeblist(bp);
		break;
	case Ilsyncee:
		switch(type) {
		default:
			break;
		case Ilsync:
			if(id != ic->rstart || ack != 0){
				illocalclose(s);
			} else {
				ic->recvd = id;
				ilsendctl(s, nil, Ilsync, ic->start, ic->recvd, 0, version);
			}
			break;
		case Ilack:
			if(ack == ic->start) {
				ic->state = Ilestablished;
				ic->fasttimeout = 0;
				ic->rexmit = 0;
				ilpullup(s);
			}
			break;
		case Ildata:
			if(ack == ic->start) {
				ic->state = Ilestablished;
				ic->fasttimeout = 0;
				ic->rexmit = 0;
				goto established;
			}
			break;
		case Ilclose:
			if(ack == ic->start)
				ilhangup(s, "remote close");
			break;
		}
		freeblist(bp);
		break;
	case Ilestablished:
	established:
		switch(type) {
		case Ilsync:
			if(id != ic->rstart)
				ilhangup(s, "remote close");
			else
				ilsendctl(s, nil, Ilack, ic->next, ic->rstart, 0, version);
			freeblist(bp);	
			break;
		case Ildata:
			/*
			 * avoid consuming all the mount rpc buffers in the
			 * system.  if the input queue is too long, drop this
			 * packet.
			 */
			if (s->rq && qlen(s->rq) >= Maxrq) {
				priv->stats[DroppedMsgs]++;
				freeblist(bp);
				break;
			}

			ilackto(ic, ack, bp);
			iloutoforder(s, h4, bp);
			ilpullup(s);
			break;
		case Ildataquery:
			ilackto(ic, ack, bp);
			iloutoforder(s, h4, bp);
			ilpullup(s);
			ilsendctl(s, nil, Ilstate, ic->next, ic->recvd, *spec, version);
			break;
		case Ilack:
			ilackto(ic, ack, bp);
			freeblist(bp);
			break;
		case Ilquery:
			ilackto(ic, ack, bp);
			ilsendctl(s, nil, Ilstate, ic->next, ic->recvd, *spec, version);
			freeblist(bp);
			break;
		case Ilstate:
			if(ack >= ic->rttack)
				ic->rttack = 0;
			ilackto(ic, ack, bp);
			if(*spec > Nqt)
				*spec = 0;
			if(ic->qt[*spec] > ack){
				ilrexmit(ic);
				ilsettimeout(ic);
			}
			freeblist(bp);
			break;
		case Ilclose:
			freeblist(bp);
			if(ack < ic->start || ack > ic->next) 
				break;
			ic->recvd = id;
			ilsendctl(s, nil, Ilclose, ic->next, ic->recvd, 0, version);
			ic->state = Ilclosing;
			ilsettimeout(ic);
			ilfreeq(ic);
			break;
		}
		break;
	case Illistening:
		freeblist(bp);
		break;
	case Ilclosing:
		switch(type) {
		case Ilclose:
			ic->recvd = id;
			ilsendctl(s, nil, Ilclose, ic->next, ic->recvd, 0, version);
			if(ack == ic->next)
				ilhangup(s, nil);
			break;
		default:
			break;
		}
		freeblist(bp);
		break;
	}
}

void
ilrexmit(Ilcb *ic)
{
	Il4hdr *h4;
	Il6hdr *h6;
	Block *nb;
	Conv *c;
	uint id;
	Ilpriv *priv;
	uchar version;
	int oviclfl, olen;

	nb = nil;
	qlock(&ic->ackq);
	if(ic->unacked)
		nb = copyblock(ic->unacked, blocklen(ic->unacked));
	qunlock(&ic->ackq);

	if(nb == nil)
		return;

	h4 = (Il4hdr*)nb->rp;
	version = ((h4->vihl&0xF0)==IP_VER6) ? V6 : V4;
	switch(version){
	case V4:
		h4->vihl = IP_VER4;

		h4->iltype = Ildataquery;
		hnputl(h4->ilack, ic->recvd);
		h4->ilspec = ilnextqt(ic);
		h4->ilsum[0] = 0;
		h4->ilsum[1] = 0;
		hnputs(h4->ilsum, ptclcsum(nb, IL4_IPSIZE, nhgets(h4->illen)));

		c = ic->conv;
		id = nhgetl(h4->ilid);
		netlog(c->p->f, Logil, "il: rexmit %ud %lud: %d %lud: %I %d/%d\n", id, ic->recvd,
			ic->rexmit, ic->timeout,
			c->raddr, c->lport, c->rport);

		ilbackoff(ic);

		ipoput4(c->p->f, nb, 0, c->ttl, c->tos, c);
		break;
	case V6:
		h6 = (Il6hdr*)nb->rp;
		oviclfl = nhgetl(h6->viclfl);
		olen = nhgets(h6->len);

		memset(h6, 0, 8);
		hnputl(h6->viclfl, olen);
		h6->ttl = IP_ILPROTO;
		h6->iltype = Ildataquery;
		hnputl(h6->ilack, ic->recvd);
		h6->ilspec = ilnextqt(ic);
		h6->ilsum[0] = 0;
		h6->ilsum[1] = 0;
		hnputs(h6->ilsum, ptclcsum(nb, 0, nhgets(h6->illen)+IL6_IPSIZE));

		memset(h6, 0, 8);
		hnputl(h6->viclfl, oviclfl);
		hnputs(h6->len, olen);
		h6->proto = IP_ILPROTO;

		c = ic->conv;
		id = nhgetl(h6->ilid);
		netlog(c->p->f, Logil, "il: rexmit %ud %lud: %d %lud: %I %d/%d\n", id, ic->recvd,
			ic->rexmit, ic->timeout,
			c->raddr, c->lport, c->rport);

		ilbackoff(ic);

		ipoput6(c->p->f, nb, 0, c->ttl, c->tos, c);
		break;
	default:
		panic("ilrexmit: version %d", version);
		return;
	}

	/* statistics */
	ic->rxtot++;
	priv = c->p->priv;
	priv->rexmit++;
}

/* DEBUG */
void
ilprocess(Conv *s, Il4hdr *h4, Block *bp)
{
	Il6hdr *h6;
	Ilcb *ic;
	uchar version;

	ic = (Ilcb*)s->ptcl;

	USED(ic);
	version = ((h4->vihl&0xF0)==IP_VER6) ? V6 : V4;
	switch(version){
	case V4:
		netlog(s->p->f, Logilmsg, "%11s rcv %lud/%lud snt %lud/%lud pkt(%s id %d ack %d %d->%d) ",
			ilstates[ic->state],  ic->rstart, ic->recvd, ic->start, 
			ic->next, iltype[h4->iltype], nhgetl(h4->ilid), 
			nhgetl(h4->ilack), nhgets(h4->ilsrc), nhgets(h4->ildst));
		break;
	case V6:
		h6 = (Il6hdr *)h4;
		netlog(s->p->f, Logilmsg, "%11s rcv %lud/%lud snt %lud/%lud pkt(%s id %d ack %d %d->%d) ",
			ilstates[ic->state],  ic->rstart, ic->recvd, ic->start, 
			ic->next, iltype[h6->iltype], nhgetl(h6->ilid), 
			nhgetl(h6->ilack), nhgets(h6->ilsrc), nhgets(h6->ildst));
		break;
	default:
		panic("ilprocess: version %d", version);
	}

	_ilprocess(s, h4, bp);

	netlog(s->p->f, Logilmsg, "%11s rcv %lud snt %lud\n", ilstates[ic->state], ic->recvd, ic->next);
}

void
ilhangup(Conv *s, char *msg)
{
	Ilcb *ic;
	int callout;

	netlog(s->p->f, Logil, "il: hangup! %I %d/%d: %s\n", s->raddr,
		s->lport, s->rport, msg?msg:"no reason");

	ic = (Ilcb*)s->ptcl;
	callout = ic->state == Ilsyncer;
	illocalclose(s);

	qhangup(s->rq, msg);
	qhangup(s->wq, msg);

	if(callout)
		Fsconnected(s, msg);
}

void
ilpullup(Conv *s)
{
	Ilcb *ic;
	Il4hdr *oh4;
	Il6hdr *oh6;
	Block *bp;
	ulong oid, dlen;
	Ilpriv *ipriv;
	uchar version;

	ic = (Ilcb*)s->ptcl;
	if(ic->state != Ilestablished)
		return;

	qlock(&ic->outo);
	while(ic->outoforder) {
		bp = ic->outoforder;
		oh4 = (Il4hdr*)bp->rp;
		version = ((oh4->vihl&0xF0)==IP_VER6) ? V6 : V4;
		switch(version){
		case V4:
			oid = nhgetl(oh4->ilid);
			break;
		case V6:
			oh6 = (Il6hdr*)bp->rp;
			oid = nhgetl(oh6->ilid);
			break;
		default:
			panic("ilpullup: version %d", version);
			return;
		}
		if(oid <= ic->recvd) {
			ic->outoforder = bp->list;
			freeblist(bp);
			continue;
		}
		if(oid != ic->recvd+1){
			ipriv = s->p->priv;
			ipriv->stats[OutOfOrder]++;
			break;
		}

		ic->recvd = oid;
		ic->outoforder = bp->list;

		bp->list = nil;
		switch(version){
		case V4:
			dlen = nhgets(oh4->illen)-IL_HDRSIZE;
			bp = trimblock(bp, IL4_IPSIZE+IL_HDRSIZE, dlen);
			break;
		case V6:
			dlen = nhgets(oh6->illen)-IL_HDRSIZE;
			bp = trimblock(bp, IL6_IPSIZE+IL_HDRSIZE, dlen);
			break;
		default:
			panic("ilpullup: version %d", version);
		}
		/*
		 * Upper levels don't know about multiple-block
		 * messages so copy all into one (yick).
		 */
		bp = concatblock(bp);
		if(bp == 0)
			panic("ilpullup");
		bp = packblock(bp);
		if(bp == 0)
			panic("ilpullup2");
		qpass(s->rq, bp);
	}
	qunlock(&ic->outo);
}

void
iloutoforder(Conv *s, Il4hdr *h4, Block *bp)
{
	Ilcb *ic;
	Block *f, **l;
	ulong id, newid;
	Ilpriv *ipriv;
	Il6hdr *h6;
	uchar version;

	ipriv = s->p->priv;
	ic = (Ilcb*)s->ptcl;
	bp->list = nil;

	version = ((h4->vihl&0xF0)==IP_VER6) ? V6 : V4;
	switch(version){
	case V4:
		id = nhgetl(h4->ilid);
		break;
	case V6:
		h6 = (Il6hdr *)h4;
		id = nhgetl(h6->ilid);
		break;
	default:
		panic("iloutoforder: version %d", version);
		return;
	}

	/* Window checks */
	if(id <= ic->recvd || id > ic->recvd+ic->window) {
		netlog(s->p->f, Logil, "il: message outside window %lud <%lud-%lud>: %I %d/%d\n",
			id, ic->recvd, ic->recvd+ic->window, s->raddr, s->lport, s->rport);
		freeblist(bp);
		return;
	}

	/* Packet is acceptable so sort onto receive queue for pullup */
	qlock(&ic->outo);
	if(ic->outoforder == nil)
		ic->outoforder = bp;
	else {
		l = &ic->outoforder;
		for(f = *l; f; f = f->list) {
			h4 = (Il4hdr*)(f->rp);
			version = ((h4->vihl&0xF0)==IP_VER6) ? V6 : V4;
			switch(version){
			case V4:
				newid = nhgetl(h4->ilid);
				break;
			case V6:
				h6 = (Il6hdr*)(f->rp);
				newid = nhgetl(h6->ilid);
				break;
			default:
				panic("iloutoforder: version %d", version);
				return;
			}
			if(id <= newid) {
				if(id == newid) {
					ipriv->stats[DupMsg]++;
					ipriv->stats[DupBytes] += blocklen(bp);
					qunlock(&ic->outo);
					freeblist(bp);
					return;
				}
				bp->list = f;
				*l = bp;
				qunlock(&ic->outo);
				return;
			}
			l = &f->list;
		}
		*l = bp;
	}
	qunlock(&ic->outo);
}

void
ilsendctl(Conv *ipc, Il4hdr *inih4, int type, ulong id, ulong ack, int ilspec, uchar version)
{
	Il4hdr *ih4;
	Il6hdr *ih6, *inih6;
	Ilcb *ic;
	Block *bp;
	int ttl, tos;

	switch(version){
	case V4:
		bp = allocb(IL4_IPSIZE+IL_HDRSIZE);
		bp->wp += IL4_IPSIZE+IL_HDRSIZE;

		ih4 = (Il4hdr *)(bp->rp);
		ih4->vihl = IP_VER4;

		/* Ip fields */
		ih4->proto = IP_ILPROTO;
		hnputs(ih4->illen, IL_HDRSIZE);
		ih4->frag[0] = 0;
		ih4->frag[1] = 0;
		if(inih4) {
			hnputl(ih4->dst, nhgetl(inih4->src));
			hnputl(ih4->src, nhgetl(inih4->dst));
			hnputs(ih4->ilsrc, nhgets(inih4->ildst));
			hnputs(ih4->ildst, nhgets(inih4->ilsrc));
			hnputl(ih4->ilid, nhgetl(inih4->ilack));
			hnputl(ih4->ilack, nhgetl(inih4->ilid));
			ttl = MAXTTL;
			tos = DFLTTOS;
		}
		else {
			v6tov4(ih4->dst, ipc->raddr);
			v6tov4(ih4->src, ipc->laddr);
			hnputs(ih4->ilsrc, ipc->lport);
			hnputs(ih4->ildst, ipc->rport);
			hnputl(ih4->ilid, id);
			hnputl(ih4->ilack, ack);
			ic = (Ilcb*)ipc->ptcl;
			ic->acksent = ack;
			ic->acktime = NOW;
			ttl = ipc->ttl;
			tos = ipc->tos;
		}
		ih4->iltype = type;
		ih4->ilspec = ilspec;
		ih4->ilsum[0] = 0;
		ih4->ilsum[1] = 0;

		if(ilcksum)
			hnputs(ih4->ilsum, ptclcsum(bp, IL4_IPSIZE, IL_HDRSIZE));

		if(ipc==nil)
			panic("ipc is nil caller is %#p", getcallerpc(&ipc));
		if(ipc->p==nil)
			panic("ipc->p is nil");

		netlog(ipc->p->f, Logilmsg, "ctl(%s id %d ack %d %d->%d)\n",
			iltype[ih4->iltype], nhgetl(ih4->ilid), nhgetl(ih4->ilack), 
			nhgets(ih4->ilsrc), nhgets(ih4->ildst));

		ipoput4(ipc->p->f, bp, 0, ttl, tos, ipc);
		break;
	case V6:
		inih6 = (Il6hdr *)inih4;
		bp = allocb(IL6_IPSIZE+IL_HDRSIZE);
		bp->wp += IL6_IPSIZE+IL_HDRSIZE;

		ih6 = (Il6hdr *)(bp->rp);
		memset(ih6, 0, sizeof(*ih6));

		hnputl(ih6->viclfl, IL_HDRSIZE);
		ih6->ttl = IP_ILPROTO;
		hnputs(ih6->illen, IL_HDRSIZE);
		if(inih6) {
			ipmove(ih6->dst, inih6->src);
			ipmove(ih6->src, inih6->dst);
			hnputs(ih6->ilsrc, nhgets(inih6->ildst));
			hnputs(ih6->ildst, nhgets(inih6->ilsrc));
			hnputl(ih6->ilid, nhgetl(inih6->ilack));
			hnputl(ih6->ilack, nhgetl(inih6->ilid));
			ttl = MAXTTL;
			tos = DFLTTOS;
		}
		else {
			ipmove(ih6->dst, ipc->raddr);
			ipmove(ih6->src, ipc->laddr);
			hnputs(ih6->ilsrc, ipc->lport);
			hnputs(ih6->ildst, ipc->rport);
			hnputl(ih6->ilid, id);
			hnputl(ih6->ilack, ack);
			ic = (Ilcb*)ipc->ptcl;
			ic->acksent = ack;
			ic->acktime = NOW;
			ttl = ipc->ttl;
			tos = ipc->tos;
		}
		ih6->iltype = type;
		ih6->ilspec = ilspec;
		ih6->ilsum[0] = 0;
		ih6->ilsum[1] = 0;
		if(ilcksum)
			hnputs(ih6->ilsum, ptclcsum(bp, 0, IL_HDRSIZE+IL6_IPSIZE));

		memset(ih6, 0, 8);
		ih6->viclfl[0] = IP_VER6;
		ih6->proto = IP_ILPROTO;

		if(ipc==nil)
			panic("ipc is nil caller is %#p", getcallerpc(&ipc));
		if(ipc->p==nil)
			panic("ipc->p is nil");

		netlog(ipc->p->f, Logilmsg, "ctl(%s id %d ack %d %d->%d)\n",
			iltype[ih6->iltype], nhgetl(ih6->ilid), nhgetl(ih6->ilack), 
			nhgets(ih6->ilsrc), nhgets(ih6->ildst));

		ipoput6(ipc->p->f, bp, 0, ttl, tos, ipc);
		break;
	default:
		panic("ilsendctl: version %d", version);
	}
}

void
ilreject(Fs *f, Il4hdr *inih4)
{
	Il4hdr *ih4;
	Il6hdr *ih6, *inih6;
	Block *bp;
	uchar version;

	version = ((inih4->vihl&0xF0)==IP_VER6) ? V6 : V4;
	switch(version){
	case V4:
		bp = allocb(IL4_IPSIZE+IL_HDRSIZE);
		bp->wp += IL4_IPSIZE+IL_HDRSIZE;

		ih4 = (Il4hdr *)(bp->rp);
		ih4->vihl = IP_VER4;

		/* Ip fields */
		ih4->proto = IP_ILPROTO;
		hnputs(ih4->illen, IL_HDRSIZE);
		ih4->frag[0] = 0;
		ih4->frag[1] = 0;
		hnputl(ih4->dst, nhgetl(inih4->src));
		hnputl(ih4->src, nhgetl(inih4->dst));
		hnputs(ih4->ilsrc, nhgets(inih4->ildst));
		hnputs(ih4->ildst, nhgets(inih4->ilsrc));
		hnputl(ih4->ilid, nhgetl(inih4->ilack));
		hnputl(ih4->ilack, nhgetl(inih4->ilid));
		ih4->iltype = Ilclose;
		ih4->ilspec = 0;
		ih4->ilsum[0] = 0;
		ih4->ilsum[1] = 0;

		if(ilcksum)
			hnputs(ih4->ilsum, ptclcsum(bp, IL4_IPSIZE, IL_HDRSIZE));

		ipoput4(f, bp, 0, MAXTTL, DFLTTOS, nil);
		break;
	case V6:
		inih6 = (Il6hdr *)inih4;
		bp = allocb(IL6_IPSIZE+IL_HDRSIZE);
		bp->wp += IL6_IPSIZE+IL_HDRSIZE;

		ih6 = (Il6hdr *)(bp->rp);
		memset(ih6, 0, sizeof(*ih6));

		hnputl(ih6->viclfl, IL_HDRSIZE);
		ih6->ttl = IP_ILPROTO;
		ipmove(ih6->dst, inih6->src);
		ipmove(ih6->src, inih6->dst);
		hnputs(ih6->illen, IL_HDRSIZE);
		hnputs(ih6->ilsrc, nhgets(inih6->ildst));
		hnputs(ih6->ildst, nhgets(inih6->ilsrc));
		hnputl(ih6->ilid, nhgetl(inih6->ilack));
		hnputl(ih6->ilack, nhgetl(inih6->ilid));
		ih6->iltype = Ilclose;
		ih6->ilspec = 0;
		ih6->ilsum[0] = 0;
		ih6->ilsum[1] = 0;
		if(ilcksum)
			hnputs(ih6->ilsum, ptclcsum(bp, 0, IL_HDRSIZE+IL6_IPSIZE));

		memset(ih6, 0, 8);
		ih6->viclfl[0] = IP_VER6;
		hnputs(ih6->len, IL_HDRSIZE);
		ih6->proto = IP_ILPROTO;

		ipoput6(f, bp, 0, MAXTTL, DFLTTOS, nil);
		break;
	default:
		panic("ilreject: version %d", version);
	}
}

void
ilsettimeout(Ilcb *ic)
{
	ulong pt;

	pt = (ic->delay>>LogAGain)
		+ ic->unackedbytes/(ic->rate>>LogAGain)
		+ (ic->mdev>>(LogDGain-1))
		+ AckDelay;
	if(pt > MaxTimeout)
		pt = MaxTimeout;
	ic->timeout = NOW + pt;
}

void
ilbackoff(Ilcb *ic)
{
	ulong pt;
	int i;

	pt = (ic->delay>>LogAGain)
		+ ic->unackedbytes/(ic->rate>>LogAGain)
		+ (ic->mdev>>(LogDGain-1))
		+ AckDelay;
	for(i = 0; i < ic->rexmit; i++)
		pt = pt + (pt>>1);
	if(pt > MaxTimeout)
		pt = MaxTimeout;
	ic->timeout = NOW + pt;

	if(ic->fasttimeout)
		ic->timeout = NOW+Iltickms;

	ic->rexmit++;
}

// complain if two numbers not within an hour of each other
#define Tfuture (1000*60*60)
int
later(ulong t1, ulong t2, char *x)
{
	int dt;

	dt = t1 - t2;
	if(dt > 0) {
		if(x != nil && dt > Tfuture)
			print("%s: way future %d\n", x, dt);
		return 1;
	}
	if(dt < -Tfuture) {
		if(x != nil)
			print("%s: way past %d\n", x, -dt);
		return 1;
	}
	return 0;
}

void
ilackproc(void *x)
{
	Ilcb *ic;
	Conv **s, *p;
	Proto *il;

	il = x;

loop:
	tsleep(&up->sleep, return0, 0, Iltickms);
	for(s = il->conv; s && *s; s++) {
		p = *s;
		ic = (Ilcb*)p->ptcl;

		switch(ic->state) {
		case Ilclosed:
		case Illistening:
			break;
		case Ilclosing:
			if(later(NOW, ic->timeout, "timeout0")) {
				if(ic->rexmit > MaxRexmit){
					ilhangup(p, nil);
					break;
				}
				ilsendctl(p, nil, Ilclose, ic->next, ic->recvd, 0, p->ipversion);
				ilbackoff(ic);
			}
			break;

		case Ilsyncee:
		case Ilsyncer:
			if(later(NOW, ic->timeout, "timeout1")) {
				if(ic->rexmit > MaxRexmit){
					ilhangup(p, etime);
					break;
				}
				ilsendctl(p, nil, Ilsync, ic->start, ic->recvd, 0, p->ipversion);
				ilbackoff(ic);
			}
			break;

		case Ilestablished:
			if(ic->recvd != ic->acksent)
			if(later(NOW, ic->acktime, "acktime"))
				ilsendctl(p, nil, Ilack, ic->next, ic->recvd, 0, p->ipversion);

			if(later(NOW, ic->querytime, "querytime")){
				if(later(NOW, ic->lastrecv+DeathTime, "deathtime")){
					netlog(il->f, Logil, "il: hangup: deathtime\n");
					ilhangup(p, etime);
					break;
				}
				ilsendctl(p, nil, Ilquery, ic->next, ic->recvd, ilnextqt(ic), p->ipversion);
				ic->querytime = NOW + QueryTime;
			}

			if(ic->unacked != nil)
			if(later(NOW, ic->timeout, "timeout2")) {
				if(ic->rexmit > MaxRexmit){
					netlog(il->f, Logil, "il: hangup: too many rexmits\n");
					ilhangup(p, etime);
					break;
				}
				ilsendctl(p, nil, Ilquery, ic->next, ic->recvd, ilnextqt(ic), p->ipversion);
				ic->rxquery++;
				ilbackoff(ic);
			}
			break;
		}
	}
	goto loop;
}

void
ilcbinit(Ilcb *ic)
{
	ic->start = nrand(0x1000000);
	ic->next = ic->start+1;
	ic->recvd = 0;
	ic->window = Defaultwin;
	ic->unackedbytes = 0;
	ic->unacked = nil;
	ic->outoforder = nil;
	ic->rexmit = 0;
	ic->rxtot = 0;
	ic->rxquery = 0;
	ic->qtx = 1;
	ic->fasttimeout = 0;

	/* timers */
	ic->delay = DefRtt<<LogAGain;
	ic->mdev = DefRtt<<LogDGain;
	ic->rate = DefByteRate<<LogAGain;
	ic->querytime = NOW + QueryTime;
	ic->lastrecv = NOW;	/* or we'll timeout right away */
	ilsettimeout(ic);
}

char*
ilstart(Conv *c, int type, int fasttimeout)
{
	Ilcb *ic;
	Ilpriv *ipriv;
	char kpname[KNAMELEN];

	ipriv = c->p->priv;

	if(ipriv->ackprocstarted == 0){
		qlock(&ipriv->apl);
		if(ipriv->ackprocstarted == 0){
			sprint(kpname, "#I%dilack", c->p->f->dev);
			kproc(kpname, ilackproc, c->p);
			ipriv->ackprocstarted = 1;
		}
		qunlock(&ipriv->apl);
	}

	ic = (Ilcb*)c->ptcl;
	ic->conv = c;

	if(ic->state != Ilclosed)
		return nil;

	ilcbinit(ic);

	if(fasttimeout){
		/* timeout if we can't connect quickly */
		ic->fasttimeout = 1;
		ic->timeout = NOW+Iltickms;
		ic->rexmit = MaxRexmit - 4;
	};

	switch(type) {
	default:
		netlog(c->p->f, Logil, "il: start: type %d\n", type);
		break;
	case IL_LISTEN:
		ic->state = Illistening;
		iphtadd(&ipriv->ht, c);
		break;
	case IL_CONNECT:
		ic->state = Ilsyncer;
		iphtadd(&ipriv->ht, c);
		ilsendctl(c, nil, Ilsync, ic->start, ic->recvd, 0, c->ipversion);
		break;
	}

	return nil;
}

void
ilfreeq(Ilcb *ic)
{
	Block *bp, *next;

	qlock(&ic->ackq);
	for(bp = ic->unacked; bp; bp = next) {
		next = bp->list;
		freeblist(bp);
	}
	ic->unacked = nil;
	qunlock(&ic->ackq);

	qlock(&ic->outo);
	for(bp = ic->outoforder; bp; bp = next) {
		next = bp->list;
		freeblist(bp);
	}
	ic->outoforder = nil;
	qunlock(&ic->outo);
}

void
iladvise(Proto *il, Block *bp, char *msg)
{
	Il4hdr *h4;
	Il6hdr *h6;
	Ilcb *ic;		
	uchar source[IPaddrlen], dest[IPaddrlen];
	ushort psource;
	Conv *s, **p;
	int version;

	h4 = (Il4hdr*)(bp->rp);
	version = ((h4->vihl&0xF0)==IP_VER6) ? V6 : V4;

	switch(version) {
	case V4:
		v4tov6(dest, h4->dst);
		v4tov6(source, h4->src);
		psource = nhgets(h4->ilsrc);
		break;
	case V6:
		h6 = (Il6hdr*)(bp->rp);
		ipmove(dest, h6->dst);
		ipmove(source, h6->src);
		psource = nhgets(h6->ilsrc);
		break;
	default:
		panic("iladvise: version %d", version);
		return; /* to avoid a warning */
	}

	/* Look for a connection, unfortunately the destination port is missing */
	qlock(il);
	for(p = il->conv; *p; p++) {
		s = *p;
		if(s->lport == psource)
		if(ipcmp(s->laddr, source) == 0)
		if(ipcmp(s->raddr, dest) == 0){
			qunlock(il);
			ic = (Ilcb*)s->ptcl;
			switch(ic->state){
			case Ilsyncer:
				ilhangup(s, msg);
				break;
			}
			freeblist(bp);
			return;
		}
	}
	qunlock(il);
	freeblist(bp);
}

int
ilnextqt(Ilcb *ic)
{
	int x;

	qlock(&ic->ackq);
	x = ic->qtx;
	if(++x > Nqt)
		x = 1;
	ic->qtx = x;
	ic->qt[x] = ic->next-1;	/* highest xmitted packet */
	ic->qt[0] = ic->qt[x];	/* compatibility with old implementations */
	qunlock(&ic->ackq);

	return x;
}

/* calculate scale constants that converts fast ticks to ms (more or less) */
static void
inittimescale(void)
{
	uvlong hz;

	fastticks(&hz);
	if(hz > 1000){
		scalediv = hz/1000;
		scalemul = 1;
	} else {
		scalediv = 1;
		scalemul = 1000/hz;
	}
}

void
ilinit(Fs *f)
{
	Proto *il;

	inittimescale();

	il = smalloc(sizeof(Proto));
	il->priv = smalloc(sizeof(Ilpriv));
	il->name = "il";
	il->connect = ilconnect;
	il->announce = ilannounce;
	il->state = ilstate;
	il->create = ilcreate;
	il->close = ilclose;
	il->rcv = iliput;
	il->ctl = nil;
	il->advise = iladvise;
	il->stats = ilxstats;
	il->inuse = ilinuse;
	il->gc = nil;
	il->ipproto = IP_ILPROTO;
	il->nc = scalednconv();
	il->ptclsize = sizeof(Ilcb);
	Fsproto(f, il);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.