spx_reass.c revision 76166
111819Sjulian/*
211819Sjulian * Copyright (c) 1995, Mike Mitchell
311819Sjulian * Copyright (c) 1984, 1985, 1986, 1987, 1993
411819Sjulian *	The Regents of the University of California.  All rights reserved.
511819Sjulian *
611819Sjulian * Redistribution and use in source and binary forms, with or without
711819Sjulian * modification, are permitted provided that the following conditions
811819Sjulian * are met:
911819Sjulian * 1. Redistributions of source code must retain the above copyright
1011819Sjulian *    notice, this list of conditions and the following disclaimer.
1111819Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1211819Sjulian *    notice, this list of conditions and the following disclaimer in the
1311819Sjulian *    documentation and/or other materials provided with the distribution.
1411819Sjulian * 3. All advertising materials mentioning features or use of this software
1511819Sjulian *    must display the following acknowledgement:
1611819Sjulian *	This product includes software developed by the University of
1711819Sjulian *	California, Berkeley and its contributors.
1811819Sjulian * 4. Neither the name of the University nor the names of its contributors
1911819Sjulian *    may be used to endorse or promote products derived from this software
2011819Sjulian *    without specific prior written permission.
2111819Sjulian *
2211819Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2311819Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2411819Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2511819Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2611819Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2711819Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2811819Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2911819Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3011819Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3111819Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3211819Sjulian * SUCH DAMAGE.
3311819Sjulian *
3412057Sjulian *	@(#)spx_usrreq.h
3512057Sjulian *
3650477Speter * $FreeBSD: head/sys/netipx/spx_usrreq.c 76166 2001-05-01 08:13:21Z markm $
3711819Sjulian */
3811819Sjulian
3911819Sjulian#include <sys/param.h>
4011819Sjulian#include <sys/systm.h>
4176166Smarkm#include <sys/lock.h>
4229024Sbde#include <sys/malloc.h>
4311819Sjulian#include <sys/mbuf.h>
4476166Smarkm#include <sys/mutex.h>
4525345Sjhay#include <sys/proc.h>
4611819Sjulian#include <sys/protosw.h>
4711819Sjulian#include <sys/socket.h>
4811819Sjulian#include <sys/socketvar.h>
4911819Sjulian
5011819Sjulian#include <net/route.h>
5111819Sjulian#include <netinet/tcp_fsm.h>
5211819Sjulian
5311819Sjulian#include <netipx/ipx.h>
5411819Sjulian#include <netipx/ipx_pcb.h>
5511819Sjulian#include <netipx/ipx_var.h>
5611819Sjulian#include <netipx/spx.h>
5711819Sjulian#include <netipx/spx_timer.h>
5811819Sjulian#include <netipx/spx_var.h>
5911819Sjulian#include <netipx/spx_debug.h>
6011819Sjulian
6111819Sjulian/*
6211819Sjulian * SPX protocol implementation.
6311819Sjulian */
6433181Seivindstatic u_short 	spx_iss;
6533181Seivindstatic u_short	spx_newchecks[50];
6633181Seivindstatic int	spx_hardnosed;
6733181Seivindstatic int	spx_use_delack = 0;
6833181Seivindstatic int	traceallspxs = 0;
6933181Seivindstatic struct	spx 	spx_savesi;
7033181Seivindstatic struct	spx_istat spx_istat;
7111819Sjulian
7225652Sjhay/* Following was struct spxstat spxstat; */
7325652Sjhay#ifndef spxstat
7425652Sjhay#define spxstat spx_istat.newstats
7525652Sjhay#endif
7611819Sjulian
7733181Seivindstatic int spx_backoff[SPX_MAXRXTSHIFT+1] =
7825652Sjhay    { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
7911819Sjulian
8025652Sjhaystatic	struct spxpcb *spx_close(struct spxpcb *cb);
8125652Sjhaystatic	struct spxpcb *spx_disconnect(struct spxpcb *cb);
8225652Sjhaystatic	struct spxpcb *spx_drop(struct spxpcb *cb, int errno);
8325652Sjhaystatic	int spx_output(struct spxpcb *cb, struct mbuf *m0);
8425652Sjhaystatic	int spx_reass(struct spxpcb *cb, struct spx *si);
8525652Sjhaystatic	void spx_setpersist(struct spxpcb *cb);
8625652Sjhaystatic	void spx_template(struct spxpcb *cb);
8725652Sjhaystatic	struct spxpcb *spx_timers(struct spxpcb *cb, int timer);
8825652Sjhaystatic	struct spxpcb *spx_usrclosed(struct spxpcb *cb);
8925652Sjhay
9024659Sjhaystatic	int spx_usr_abort(struct socket *so);
9128270Swollmanstatic	int spx_accept(struct socket *so, struct sockaddr **nam);
9225345Sjhaystatic	int spx_attach(struct socket *so, int proto, struct proc *p);
9328270Swollmanstatic	int spx_bind(struct socket *so, struct sockaddr *nam, struct proc *p);
9428270Swollmanstatic	int spx_connect(struct socket *so, struct sockaddr *nam,
9528270Swollman			struct proc *p);
9624659Sjhaystatic	int spx_detach(struct socket *so);
9724659Sjhaystatic	int spx_usr_disconnect(struct socket *so);
9825345Sjhaystatic	int spx_listen(struct socket *so, struct proc *p);
9924659Sjhaystatic	int spx_rcvd(struct socket *so, int flags);
10024659Sjhaystatic	int spx_rcvoob(struct socket *so, struct mbuf *m, int flags);
10124659Sjhaystatic	int spx_send(struct socket *so, int flags, struct mbuf *m,
10228270Swollman		     struct sockaddr *addr, struct mbuf *control,
10328270Swollman		     struct proc *p);
10424659Sjhaystatic	int spx_shutdown(struct socket *so);
10525345Sjhaystatic	int spx_sp_attach(struct socket *so, int proto, struct proc *p);
10624659Sjhay
10724659Sjhaystruct	pr_usrreqs spx_usrreqs = {
10824659Sjhay	spx_usr_abort, spx_accept, spx_attach, spx_bind,
10924659Sjhay	spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
11024659Sjhay	spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
11124659Sjhay	spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
11229366Speter	ipx_sockaddr, sosend, soreceive, sopoll
11324659Sjhay};
11424659Sjhay
11524659Sjhaystruct	pr_usrreqs spx_usrreq_sps = {
11624659Sjhay	spx_usr_abort, spx_accept, spx_sp_attach, spx_bind,
11724659Sjhay	spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
11824659Sjhay	spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
11924659Sjhay	spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
12029366Speter	ipx_sockaddr, sosend, soreceive, sopoll
12124659Sjhay};
12224659Sjhay
12311819Sjulianvoid
12411819Sjulianspx_init()
12511819Sjulian{
12611819Sjulian
12711819Sjulian	spx_iss = 1; /* WRONG !! should fish it out of TODR */
12811819Sjulian}
12911819Sjulian
13011819Sjulianvoid
13111819Sjulianspx_input(m, ipxp)
13211819Sjulian	register struct mbuf *m;
13311819Sjulian	register struct ipxpcb *ipxp;
13411819Sjulian{
13511819Sjulian	register struct spxpcb *cb;
13611819Sjulian	register struct spx *si = mtod(m, struct spx *);
13711819Sjulian	register struct socket *so;
13811819Sjulian	int dropsocket = 0;
13911819Sjulian	short ostate = 0;
14011819Sjulian
14111819Sjulian	spxstat.spxs_rcvtotal++;
14225652Sjhay	if (ipxp == NULL) {
14311819Sjulian		panic("No ipxpcb in spx_input\n");
14411819Sjulian		return;
14511819Sjulian	}
14611819Sjulian
14711819Sjulian	cb = ipxtospxpcb(ipxp);
14825652Sjhay	if (cb == NULL)
14925652Sjhay		goto bad;
15011819Sjulian
15111819Sjulian	if (m->m_len < sizeof(*si)) {
15225652Sjhay		if ((m = m_pullup(m, sizeof(*si))) == NULL) {
15311819Sjulian			spxstat.spxs_rcvshort++;
15411819Sjulian			return;
15511819Sjulian		}
15611819Sjulian		si = mtod(m, struct spx *);
15711819Sjulian	}
15811819Sjulian	si->si_seq = ntohs(si->si_seq);
15911819Sjulian	si->si_ack = ntohs(si->si_ack);
16011819Sjulian	si->si_alo = ntohs(si->si_alo);
16111819Sjulian
16211819Sjulian	so = ipxp->ipxp_socket;
16311819Sjulian
16411819Sjulian	if (so->so_options & SO_DEBUG || traceallspxs) {
16511819Sjulian		ostate = cb->s_state;
16611819Sjulian		spx_savesi = *si;
16711819Sjulian	}
16811819Sjulian	if (so->so_options & SO_ACCEPTCONN) {
16911819Sjulian		struct spxpcb *ocb = cb;
17011819Sjulian
17111819Sjulian		so = sonewconn(so, 0);
17225652Sjhay		if (so == NULL) {
17311819Sjulian			goto drop;
17411819Sjulian		}
17511819Sjulian		/*
17611819Sjulian		 * This is ugly, but ....
17711819Sjulian		 *
17811819Sjulian		 * Mark socket as temporary until we're
17911819Sjulian		 * committed to keeping it.  The code at
18011819Sjulian		 * ``drop'' and ``dropwithreset'' check the
18111819Sjulian		 * flag dropsocket to see if the temporary
18211819Sjulian		 * socket created here should be discarded.
18311819Sjulian		 * We mark the socket as discardable until
18411819Sjulian		 * we're committed to it below in TCPS_LISTEN.
18511819Sjulian		 */
18611819Sjulian		dropsocket++;
18711819Sjulian		ipxp = (struct ipxpcb *)so->so_pcb;
18811819Sjulian		ipxp->ipxp_laddr = si->si_dna;
18911819Sjulian		cb = ipxtospxpcb(ipxp);
19011819Sjulian		cb->s_mtu = ocb->s_mtu;		/* preserve sockopts */
19111819Sjulian		cb->s_flags = ocb->s_flags;	/* preserve sockopts */
19211819Sjulian		cb->s_flags2 = ocb->s_flags2;	/* preserve sockopts */
19311819Sjulian		cb->s_state = TCPS_LISTEN;
19411819Sjulian	}
19511819Sjulian
19611819Sjulian	/*
19711819Sjulian	 * Packet received on connection.
19811819Sjulian	 * reset idle time and keep-alive timer;
19911819Sjulian	 */
20011819Sjulian	cb->s_idle = 0;
20111819Sjulian	cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
20211819Sjulian
20311819Sjulian	switch (cb->s_state) {
20411819Sjulian
20511819Sjulian	case TCPS_LISTEN:{
20628270Swollman		struct sockaddr_ipx *sipx, ssipx;
20711819Sjulian		struct ipx_addr laddr;
20811819Sjulian
20911819Sjulian		/*
21011819Sjulian		 * If somebody here was carying on a conversation
21111819Sjulian		 * and went away, and his pen pal thinks he can
21211819Sjulian		 * still talk, we get the misdirected packet.
21311819Sjulian		 */
21411819Sjulian		if (spx_hardnosed && (si->si_did != 0 || si->si_seq != 0)) {
21511819Sjulian			spx_istat.gonawy++;
21611819Sjulian			goto dropwithreset;
21711819Sjulian		}
21828270Swollman		sipx = &ssipx;
21928270Swollman		bzero(sipx, sizeof *sipx);
22011819Sjulian		sipx->sipx_len = sizeof(*sipx);
22111819Sjulian		sipx->sipx_family = AF_IPX;
22211819Sjulian		sipx->sipx_addr = si->si_sna;
22311819Sjulian		laddr = ipxp->ipxp_laddr;
22411819Sjulian		if (ipx_nullhost(laddr))
22511819Sjulian			ipxp->ipxp_laddr = si->si_dna;
22628270Swollman		if (ipx_pcbconnect(ipxp, (struct sockaddr *)sipx, &proc0)) {
22711819Sjulian			ipxp->ipxp_laddr = laddr;
22811819Sjulian			spx_istat.noconn++;
22911819Sjulian			goto drop;
23011819Sjulian		}
23111819Sjulian		spx_template(cb);
23211819Sjulian		dropsocket = 0;		/* committed to socket */
23311819Sjulian		cb->s_did = si->si_sid;
23411819Sjulian		cb->s_rack = si->si_ack;
23511819Sjulian		cb->s_ralo = si->si_alo;
23611819Sjulian#define THREEWAYSHAKE
23711819Sjulian#ifdef THREEWAYSHAKE
23811819Sjulian		cb->s_state = TCPS_SYN_RECEIVED;
23911819Sjulian		cb->s_force = 1 + SPXT_KEEP;
24011819Sjulian		spxstat.spxs_accepts++;
24111819Sjulian		cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
24211819Sjulian		}
24311819Sjulian		break;
24411819Sjulian	/*
24511819Sjulian	 * This state means that we have heard a response
24611819Sjulian	 * to our acceptance of their connection
24711819Sjulian	 * It is probably logically unnecessary in this
24811819Sjulian	 * implementation.
24911819Sjulian	 */
25011819Sjulian	 case TCPS_SYN_RECEIVED: {
25125652Sjhay		if (si->si_did != cb->s_sid) {
25211819Sjulian			spx_istat.wrncon++;
25311819Sjulian			goto drop;
25411819Sjulian		}
25511819Sjulian#endif
25611819Sjulian		ipxp->ipxp_fport =  si->si_sport;
25711819Sjulian		cb->s_timer[SPXT_REXMT] = 0;
25811819Sjulian		cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
25911819Sjulian		soisconnected(so);
26011819Sjulian		cb->s_state = TCPS_ESTABLISHED;
26111819Sjulian		spxstat.spxs_accepts++;
26211819Sjulian		}
26311819Sjulian		break;
26411819Sjulian
26511819Sjulian	/*
26611819Sjulian	 * This state means that we have gotten a response
26711819Sjulian	 * to our attempt to establish a connection.
26811819Sjulian	 * We fill in the data from the other side,
26911819Sjulian	 * telling us which port to respond to, instead of the well-
27011819Sjulian	 * known one we might have sent to in the first place.
27111819Sjulian	 * We also require that this is a response to our
27211819Sjulian	 * connection id.
27311819Sjulian	 */
27411819Sjulian	case TCPS_SYN_SENT:
27525652Sjhay		if (si->si_did != cb->s_sid) {
27611819Sjulian			spx_istat.notme++;
27711819Sjulian			goto drop;
27811819Sjulian		}
27911819Sjulian		spxstat.spxs_connects++;
28011819Sjulian		cb->s_did = si->si_sid;
28111819Sjulian		cb->s_rack = si->si_ack;
28211819Sjulian		cb->s_ralo = si->si_alo;
28311819Sjulian		cb->s_dport = ipxp->ipxp_fport =  si->si_sport;
28411819Sjulian		cb->s_timer[SPXT_REXMT] = 0;
28511819Sjulian		cb->s_flags |= SF_ACKNOW;
28611819Sjulian		soisconnected(so);
28711819Sjulian		cb->s_state = TCPS_ESTABLISHED;
28811819Sjulian		/* Use roundtrip time of connection request for initial rtt */
28911819Sjulian		if (cb->s_rtt) {
29011819Sjulian			cb->s_srtt = cb->s_rtt << 3;
29111819Sjulian			cb->s_rttvar = cb->s_rtt << 1;
29211819Sjulian			SPXT_RANGESET(cb->s_rxtcur,
29311819Sjulian			    ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
29411819Sjulian			    SPXTV_MIN, SPXTV_REXMTMAX);
29511819Sjulian			    cb->s_rtt = 0;
29611819Sjulian		}
29711819Sjulian	}
29811819Sjulian	if (so->so_options & SO_DEBUG || traceallspxs)
29911819Sjulian		spx_trace(SA_INPUT, (u_char)ostate, cb, &spx_savesi, 0);
30011819Sjulian
30125652Sjhay	m->m_len -= sizeof(struct ipx);
30225652Sjhay	m->m_pkthdr.len -= sizeof(struct ipx);
30325652Sjhay	m->m_data += sizeof(struct ipx);
30411819Sjulian
30511819Sjulian	if (spx_reass(cb, si)) {
30625652Sjhay		m_freem(m);
30711819Sjulian	}
30811819Sjulian	if (cb->s_force || (cb->s_flags & (SF_ACKNOW|SF_WIN|SF_RXT)))
30925652Sjhay		spx_output(cb, (struct mbuf *)NULL);
31011819Sjulian	cb->s_flags &= ~(SF_WIN|SF_RXT);
31111819Sjulian	return;
31211819Sjulian
31311819Sjuliandropwithreset:
31411819Sjulian	if (dropsocket)
31525652Sjhay		soabort(so);
31611819Sjulian	si->si_seq = ntohs(si->si_seq);
31711819Sjulian	si->si_ack = ntohs(si->si_ack);
31811819Sjulian	si->si_alo = ntohs(si->si_alo);
31925652Sjhay	m_freem(dtom(si));
32011819Sjulian	if (cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG || traceallspxs)
32111819Sjulian		spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
32211819Sjulian	return;
32311819Sjulian
32411819Sjuliandrop:
32511819Sjulianbad:
32611819Sjulian	if (cb == 0 || cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG ||
32711819Sjulian            traceallspxs)
32811819Sjulian		spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
32911819Sjulian	m_freem(m);
33011819Sjulian}
33111819Sjulian
33233181Seivindstatic int spxrexmtthresh = 3;
33311819Sjulian
33411819Sjulian/*
33511819Sjulian * This is structurally similar to the tcp reassembly routine
33611819Sjulian * but its function is somewhat different:  It merely queues
33711819Sjulian * packets up, and suppresses duplicates.
33811819Sjulian */
33925652Sjhaystatic int
34011819Sjulianspx_reass(cb, si)
34111819Sjulianregister struct spxpcb *cb;
34211819Sjulianregister struct spx *si;
34311819Sjulian{
34411819Sjulian	register struct spx_q *q;
34511819Sjulian	register struct mbuf *m;
34611819Sjulian	register struct socket *so = cb->s_ipxpcb->ipxp_socket;
34711819Sjulian	char packetp = cb->s_flags & SF_HI;
34811819Sjulian	int incr;
34911819Sjulian	char wakeup = 0;
35011819Sjulian
35111819Sjulian	if (si == SI(0))
35211819Sjulian		goto present;
35311819Sjulian	/*
35411819Sjulian	 * Update our news from them.
35511819Sjulian	 */
35611819Sjulian	if (si->si_cc & SPX_SA)
35711819Sjulian		cb->s_flags |= (spx_use_delack ? SF_DELACK : SF_ACKNOW);
35811819Sjulian	if (SSEQ_GT(si->si_alo, cb->s_ralo))
35911819Sjulian		cb->s_flags |= SF_WIN;
36011819Sjulian	if (SSEQ_LEQ(si->si_ack, cb->s_rack)) {
36111819Sjulian		if ((si->si_cc & SPX_SP) && cb->s_rack != (cb->s_smax + 1)) {
36211819Sjulian			spxstat.spxs_rcvdupack++;
36311819Sjulian			/*
36411819Sjulian			 * If this is a completely duplicate ack
36511819Sjulian			 * and other conditions hold, we assume
36611819Sjulian			 * a packet has been dropped and retransmit
36711819Sjulian			 * it exactly as in tcp_input().
36811819Sjulian			 */
36911819Sjulian			if (si->si_ack != cb->s_rack ||
37011819Sjulian			    si->si_alo != cb->s_ralo)
37111819Sjulian				cb->s_dupacks = 0;
37211819Sjulian			else if (++cb->s_dupacks == spxrexmtthresh) {
37311819Sjulian				u_short onxt = cb->s_snxt;
37411819Sjulian				int cwnd = cb->s_cwnd;
37511819Sjulian
37611819Sjulian				cb->s_snxt = si->si_ack;
37711819Sjulian				cb->s_cwnd = CUNIT;
37811819Sjulian				cb->s_force = 1 + SPXT_REXMT;
37925652Sjhay				spx_output(cb, (struct mbuf *)NULL);
38011819Sjulian				cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
38111819Sjulian				cb->s_rtt = 0;
38211819Sjulian				if (cwnd >= 4 * CUNIT)
38311819Sjulian					cb->s_cwnd = cwnd / 2;
38411819Sjulian				if (SSEQ_GT(onxt, cb->s_snxt))
38511819Sjulian					cb->s_snxt = onxt;
38611819Sjulian				return (1);
38711819Sjulian			}
38811819Sjulian		} else
38911819Sjulian			cb->s_dupacks = 0;
39011819Sjulian		goto update_window;
39111819Sjulian	}
39211819Sjulian	cb->s_dupacks = 0;
39311819Sjulian	/*
39411819Sjulian	 * If our correspondent acknowledges data we haven't sent
39511819Sjulian	 * TCP would drop the packet after acking.  We'll be a little
39611819Sjulian	 * more permissive
39711819Sjulian	 */
39811819Sjulian	if (SSEQ_GT(si->si_ack, (cb->s_smax + 1))) {
39911819Sjulian		spxstat.spxs_rcvacktoomuch++;
40011819Sjulian		si->si_ack = cb->s_smax + 1;
40111819Sjulian	}
40211819Sjulian	spxstat.spxs_rcvackpack++;
40311819Sjulian	/*
40411819Sjulian	 * If transmit timer is running and timed sequence
40511819Sjulian	 * number was acked, update smoothed round trip time.
40611819Sjulian	 * See discussion of algorithm in tcp_input.c
40711819Sjulian	 */
40811819Sjulian	if (cb->s_rtt && SSEQ_GT(si->si_ack, cb->s_rtseq)) {
40911819Sjulian		spxstat.spxs_rttupdated++;
41011819Sjulian		if (cb->s_srtt != 0) {
41111819Sjulian			register short delta;
41211819Sjulian			delta = cb->s_rtt - (cb->s_srtt >> 3);
41311819Sjulian			if ((cb->s_srtt += delta) <= 0)
41411819Sjulian				cb->s_srtt = 1;
41511819Sjulian			if (delta < 0)
41611819Sjulian				delta = -delta;
41711819Sjulian			delta -= (cb->s_rttvar >> 2);
41811819Sjulian			if ((cb->s_rttvar += delta) <= 0)
41911819Sjulian				cb->s_rttvar = 1;
42011819Sjulian		} else {
42111819Sjulian			/*
42211819Sjulian			 * No rtt measurement yet
42311819Sjulian			 */
42411819Sjulian			cb->s_srtt = cb->s_rtt << 3;
42511819Sjulian			cb->s_rttvar = cb->s_rtt << 1;
42611819Sjulian		}
42711819Sjulian		cb->s_rtt = 0;
42811819Sjulian		cb->s_rxtshift = 0;
42911819Sjulian		SPXT_RANGESET(cb->s_rxtcur,
43011819Sjulian			((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
43111819Sjulian			SPXTV_MIN, SPXTV_REXMTMAX);
43211819Sjulian	}
43311819Sjulian	/*
43411819Sjulian	 * If all outstanding data is acked, stop retransmit
43511819Sjulian	 * timer and remember to restart (more output or persist).
43611819Sjulian	 * If there is more data to be acked, restart retransmit
43711819Sjulian	 * timer, using current (possibly backed-off) value;
43811819Sjulian	 */
43911819Sjulian	if (si->si_ack == cb->s_smax + 1) {
44011819Sjulian		cb->s_timer[SPXT_REXMT] = 0;
44111819Sjulian		cb->s_flags |= SF_RXT;
44211819Sjulian	} else if (cb->s_timer[SPXT_PERSIST] == 0)
44311819Sjulian		cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
44411819Sjulian	/*
44511819Sjulian	 * When new data is acked, open the congestion window.
44611819Sjulian	 * If the window gives us less than ssthresh packets
44711819Sjulian	 * in flight, open exponentially (maxseg at a time).
44811819Sjulian	 * Otherwise open linearly (maxseg^2 / cwnd at a time).
44911819Sjulian	 */
45011819Sjulian	incr = CUNIT;
45111819Sjulian	if (cb->s_cwnd > cb->s_ssthresh)
45211819Sjulian		incr = max(incr * incr / cb->s_cwnd, 1);
45311819Sjulian	cb->s_cwnd = min(cb->s_cwnd + incr, cb->s_cwmx);
45411819Sjulian	/*
45511819Sjulian	 * Trim Acked data from output queue.
45611819Sjulian	 */
45711819Sjulian	while ((m = so->so_snd.sb_mb) != NULL) {
45811819Sjulian		if (SSEQ_LT((mtod(m, struct spx *))->si_seq, si->si_ack))
45911819Sjulian			sbdroprecord(&so->so_snd);
46011819Sjulian		else
46111819Sjulian			break;
46211819Sjulian	}
46311819Sjulian	sowwakeup(so);
46411819Sjulian	cb->s_rack = si->si_ack;
46511819Sjulianupdate_window:
46611819Sjulian	if (SSEQ_LT(cb->s_snxt, cb->s_rack))
46711819Sjulian		cb->s_snxt = cb->s_rack;
46843311Sdillon	if (SSEQ_LT(cb->s_swl1, si->si_seq) || ((cb->s_swl1 == si->si_seq &&
46943311Sdillon	    (SSEQ_LT(cb->s_swl2, si->si_ack))) ||
47043305Sdillon	     (cb->s_swl2 == si->si_ack && SSEQ_LT(cb->s_ralo, si->si_alo)))) {
47111819Sjulian		/* keep track of pure window updates */
47211819Sjulian		if ((si->si_cc & SPX_SP) && cb->s_swl2 == si->si_ack
47311819Sjulian		    && SSEQ_LT(cb->s_ralo, si->si_alo)) {
47411819Sjulian			spxstat.spxs_rcvwinupd++;
47511819Sjulian			spxstat.spxs_rcvdupack--;
47611819Sjulian		}
47711819Sjulian		cb->s_ralo = si->si_alo;
47811819Sjulian		cb->s_swl1 = si->si_seq;
47911819Sjulian		cb->s_swl2 = si->si_ack;
48011819Sjulian		cb->s_swnd = (1 + si->si_alo - si->si_ack);
48111819Sjulian		if (cb->s_swnd > cb->s_smxw)
48211819Sjulian			cb->s_smxw = cb->s_swnd;
48311819Sjulian		cb->s_flags |= SF_WIN;
48411819Sjulian	}
48511819Sjulian	/*
48611819Sjulian	 * If this packet number is higher than that which
48711819Sjulian	 * we have allocated refuse it, unless urgent
48811819Sjulian	 */
48911819Sjulian	if (SSEQ_GT(si->si_seq, cb->s_alo)) {
49011819Sjulian		if (si->si_cc & SPX_SP) {
49111819Sjulian			spxstat.spxs_rcvwinprobe++;
49211819Sjulian			return (1);
49311819Sjulian		} else
49411819Sjulian			spxstat.spxs_rcvpackafterwin++;
49511819Sjulian		if (si->si_cc & SPX_OB) {
49611819Sjulian			if (SSEQ_GT(si->si_seq, cb->s_alo + 60)) {
49725652Sjhay				m_freem(dtom(si));
49811819Sjulian				return (0);
49911819Sjulian			} /* else queue this packet; */
50011819Sjulian		} else {
50111819Sjulian			/*register struct socket *so = cb->s_ipxpcb->ipxp_socket;
50211819Sjulian			if (so->so_state && SS_NOFDREF) {
50325652Sjhay				spx_close(cb);
50411819Sjulian			} else
50511819Sjulian				       would crash system*/
50611819Sjulian			spx_istat.notyet++;
50725652Sjhay			m_freem(dtom(si));
50811819Sjulian			return (0);
50911819Sjulian		}
51011819Sjulian	}
51111819Sjulian	/*
51211819Sjulian	 * If this is a system packet, we don't need to
51311819Sjulian	 * queue it up, and won't update acknowledge #
51411819Sjulian	 */
51511819Sjulian	if (si->si_cc & SPX_SP) {
51611819Sjulian		return (1);
51711819Sjulian	}
51811819Sjulian	/*
51911819Sjulian	 * We have already seen this packet, so drop.
52011819Sjulian	 */
52111819Sjulian	if (SSEQ_LT(si->si_seq, cb->s_ack)) {
52211819Sjulian		spx_istat.bdreas++;
52311819Sjulian		spxstat.spxs_rcvduppack++;
52411819Sjulian		if (si->si_seq == cb->s_ack - 1)
52511819Sjulian			spx_istat.lstdup++;
52611819Sjulian		return (1);
52711819Sjulian	}
52811819Sjulian	/*
52911819Sjulian	 * Loop through all packets queued up to insert in
53011819Sjulian	 * appropriate sequence.
53111819Sjulian	 */
53225652Sjhay	for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
53311819Sjulian		if (si->si_seq == SI(q)->si_seq) {
53411819Sjulian			spxstat.spxs_rcvduppack++;
53511819Sjulian			return (1);
53611819Sjulian		}
53711819Sjulian		if (SSEQ_LT(si->si_seq, SI(q)->si_seq)) {
53811819Sjulian			spxstat.spxs_rcvoopack++;
53911819Sjulian			break;
54011819Sjulian		}
54111819Sjulian	}
54211819Sjulian	insque(si, q->si_prev);
54311819Sjulian	/*
54411819Sjulian	 * If this packet is urgent, inform process
54511819Sjulian	 */
54611819Sjulian	if (si->si_cc & SPX_OB) {
54711819Sjulian		cb->s_iobc = ((char *)si)[1 + sizeof(*si)];
54811819Sjulian		sohasoutofband(so);
54911819Sjulian		cb->s_oobflags |= SF_IOOB;
55011819Sjulian	}
55111819Sjulianpresent:
55211819Sjulian#define SPINC sizeof(struct spxhdr)
55311819Sjulian	/*
55411819Sjulian	 * Loop through all packets queued up to update acknowledge
55511819Sjulian	 * number, and present all acknowledged data to user;
55611819Sjulian	 * If in packet interface mode, show packet headers.
55711819Sjulian	 */
55825652Sjhay	for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
55911819Sjulian		  if (SI(q)->si_seq == cb->s_ack) {
56011819Sjulian			cb->s_ack++;
56111819Sjulian			m = dtom(q);
56211819Sjulian			if (SI(q)->si_cc & SPX_OB) {
56311819Sjulian				cb->s_oobflags &= ~SF_IOOB;
56411819Sjulian				if (so->so_rcv.sb_cc)
56511819Sjulian					so->so_oobmark = so->so_rcv.sb_cc;
56611819Sjulian				else
56711819Sjulian					so->so_state |= SS_RCVATMARK;
56811819Sjulian			}
56911819Sjulian			q = q->si_prev;
57011819Sjulian			remque(q->si_next);
57111819Sjulian			wakeup = 1;
57211819Sjulian			spxstat.spxs_rcvpack++;
57311819Sjulian#ifdef SF_NEWCALL
57411819Sjulian			if (cb->s_flags2 & SF_NEWCALL) {
57511819Sjulian				struct spxhdr *sp = mtod(m, struct spxhdr *);
57611819Sjulian				u_char dt = sp->spx_dt;
57711819Sjulian				spx_newchecks[4]++;
57811819Sjulian				if (dt != cb->s_rhdr.spx_dt) {
57911819Sjulian					struct mbuf *mm =
58011819Sjulian					   m_getclr(M_DONTWAIT, MT_CONTROL);
58111819Sjulian					spx_newchecks[0]++;
58211819Sjulian					if (mm != NULL) {
58311819Sjulian						u_short *s =
58411819Sjulian							mtod(mm, u_short *);
58511819Sjulian						cb->s_rhdr.spx_dt = dt;
58611819Sjulian						mm->m_len = 5; /*XXX*/
58711819Sjulian						s[0] = 5;
58811819Sjulian						s[1] = 1;
58911819Sjulian						*(u_char *)(&s[2]) = dt;
59011819Sjulian						sbappend(&so->so_rcv, mm);
59111819Sjulian					}
59211819Sjulian				}
59311819Sjulian				if (sp->spx_cc & SPX_OB) {
59411819Sjulian					MCHTYPE(m, MT_OOBDATA);
59511819Sjulian					spx_newchecks[1]++;
59611819Sjulian					so->so_oobmark = 0;
59711819Sjulian					so->so_state &= ~SS_RCVATMARK;
59811819Sjulian				}
59911819Sjulian				if (packetp == 0) {
60011819Sjulian					m->m_data += SPINC;
60111819Sjulian					m->m_len -= SPINC;
60211819Sjulian					m->m_pkthdr.len -= SPINC;
60311819Sjulian				}
60411819Sjulian				if ((sp->spx_cc & SPX_EM) || packetp) {
60511819Sjulian					sbappendrecord(&so->so_rcv, m);
60611819Sjulian					spx_newchecks[9]++;
60711819Sjulian				} else
60811819Sjulian					sbappend(&so->so_rcv, m);
60911819Sjulian			} else
61011819Sjulian#endif
61111819Sjulian			if (packetp) {
61211819Sjulian				sbappendrecord(&so->so_rcv, m);
61311819Sjulian			} else {
61411819Sjulian				cb->s_rhdr = *mtod(m, struct spxhdr *);
61511819Sjulian				m->m_data += SPINC;
61611819Sjulian				m->m_len -= SPINC;
61711819Sjulian				m->m_pkthdr.len -= SPINC;
61811819Sjulian				sbappend(&so->so_rcv, m);
61911819Sjulian			}
62011819Sjulian		  } else
62111819Sjulian			break;
62211819Sjulian	}
62325652Sjhay	if (wakeup)
62425652Sjhay		sorwakeup(so);
62511819Sjulian	return (0);
62611819Sjulian}
62711819Sjulian
62811819Sjulianvoid
62912881Sbdespx_ctlinput(cmd, arg_as_sa, dummy)
63011819Sjulian	int cmd;
63112881Sbde	struct sockaddr *arg_as_sa;	/* XXX should be swapped with dummy */
63212881Sbde	void *dummy;
63311819Sjulian{
63412881Sbde	caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
63511819Sjulian	struct ipx_addr *na;
63625652Sjhay	struct sockaddr_ipx *sipx;
63711819Sjulian
63811819Sjulian	if (cmd < 0 || cmd > PRC_NCMDS)
63911819Sjulian		return;
64025652Sjhay
64111819Sjulian	switch (cmd) {
64211819Sjulian
64311819Sjulian	case PRC_ROUTEDEAD:
64411819Sjulian		return;
64511819Sjulian
64611819Sjulian	case PRC_IFDOWN:
64711819Sjulian	case PRC_HOSTDEAD:
64811819Sjulian	case PRC_HOSTUNREACH:
64911819Sjulian		sipx = (struct sockaddr_ipx *)arg;
65011819Sjulian		if (sipx->sipx_family != AF_IPX)
65111819Sjulian			return;
65211819Sjulian		na = &sipx->sipx_addr;
65311819Sjulian		break;
65411819Sjulian
65511819Sjulian	default:
65611819Sjulian		break;
65711819Sjulian	}
65811819Sjulian}
65911819Sjulian
66011819Sjulian#ifdef notdef
66111819Sjulianint
66211819Sjulianspx_fixmtu(ipxp)
66311819Sjulianregister struct ipxpcb *ipxp;
66411819Sjulian{
66511819Sjulian	register struct spxpcb *cb = (struct spxpcb *)(ipxp->ipxp_pcb);
66611819Sjulian	register struct mbuf *m;
66711819Sjulian	register struct spx *si;
66811819Sjulian	struct ipx_errp *ep;
66911819Sjulian	struct sockbuf *sb;
67011819Sjulian	int badseq, len;
67111819Sjulian	struct mbuf *firstbad, *m0;
67211819Sjulian
67325652Sjhay	if (cb != NULL) {
67411819Sjulian		/*
67511819Sjulian		 * The notification that we have sent
67611819Sjulian		 * too much is bad news -- we will
67711819Sjulian		 * have to go through queued up so far
67811819Sjulian		 * splitting ones which are too big and
67911819Sjulian		 * reassigning sequence numbers and checksums.
68011819Sjulian		 * we should then retransmit all packets from
68111819Sjulian		 * one above the offending packet to the last one
68211819Sjulian		 * we had sent (or our allocation)
68311819Sjulian		 * then the offending one so that the any queued
68411819Sjulian		 * data at our destination will be discarded.
68511819Sjulian		 */
68611819Sjulian		 ep = (struct ipx_errp *)ipxp->ipxp_notify_param;
68711819Sjulian		 sb = &ipxp->ipxp_socket->so_snd;
68811819Sjulian		 cb->s_mtu = ep->ipx_err_param;
68911819Sjulian		 badseq = SI(&ep->ipx_err_ipx)->si_seq;
69025652Sjhay		 for (m = sb->sb_mb; m != NULL; m = m->m_act) {
69111819Sjulian			si = mtod(m, struct spx *);
69211819Sjulian			if (si->si_seq == badseq)
69311819Sjulian				break;
69411819Sjulian		 }
69525652Sjhay		 if (m == NULL)
69625652Sjhay			return;
69711819Sjulian		 firstbad = m;
69811819Sjulian		 /*for (;;) {*/
69911819Sjulian			/* calculate length */
70025652Sjhay			for (m0 = m, len = 0; m != NULL; m = m->m_next)
70111819Sjulian				len += m->m_len;
70211819Sjulian			if (len > cb->s_mtu) {
70311819Sjulian			}
70411819Sjulian		/* FINISH THIS
70511819Sjulian		} */
70611819Sjulian	}
70711819Sjulian}
70811819Sjulian#endif
70911819Sjulian
71025652Sjhaystatic int
71111819Sjulianspx_output(cb, m0)
71211819Sjulian	register struct spxpcb *cb;
71311819Sjulian	struct mbuf *m0;
71411819Sjulian{
71511819Sjulian	struct socket *so = cb->s_ipxpcb->ipxp_socket;
71611819Sjulian	register struct mbuf *m;
71725652Sjhay	register struct spx *si = (struct spx *)NULL;
71811819Sjulian	register struct sockbuf *sb = &so->so_snd;
71911819Sjulian	int len = 0, win, rcv_win;
72011819Sjulian	short span, off, recordp = 0;
72111819Sjulian	u_short alo;
72211819Sjulian	int error = 0, sendalot;
72311819Sjulian#ifdef notdef
72411819Sjulian	int idle;
72511819Sjulian#endif
72611819Sjulian	struct mbuf *mprev;
72711819Sjulian
72825652Sjhay	if (m0 != NULL) {
72911819Sjulian		int mtu = cb->s_mtu;
73011819Sjulian		int datalen;
73111819Sjulian		/*
73211819Sjulian		 * Make sure that packet isn't too big.
73311819Sjulian		 */
73425652Sjhay		for (m = m0; m != NULL; m = m->m_next) {
73511819Sjulian			mprev = m;
73611819Sjulian			len += m->m_len;
73711819Sjulian			if (m->m_flags & M_EOR)
73811819Sjulian				recordp = 1;
73911819Sjulian		}
74011819Sjulian		datalen = (cb->s_flags & SF_HO) ?
74125652Sjhay				len - sizeof(struct spxhdr) : len;
74211819Sjulian		if (datalen > mtu) {
74311819Sjulian			if (cb->s_flags & SF_PI) {
74411819Sjulian				m_freem(m0);
74511819Sjulian				return (EMSGSIZE);
74611819Sjulian			} else {
74711819Sjulian				int oldEM = cb->s_cc & SPX_EM;
74811819Sjulian
74911819Sjulian				cb->s_cc &= ~SPX_EM;
75011819Sjulian				while (len > mtu) {
75111819Sjulian					/*
75211819Sjulian					 * Here we are only being called
75311819Sjulian					 * from usrreq(), so it is OK to
75411819Sjulian					 * block.
75511819Sjulian					 */
75670254Sbmilekic					m = m_copym(m0, 0, mtu, M_TRYWAIT);
75711819Sjulian					if (cb->s_flags & SF_NEWCALL) {
75811819Sjulian					    struct mbuf *mm = m;
75911819Sjulian					    spx_newchecks[7]++;
76025652Sjhay					    while (mm != NULL) {
76111819Sjulian						mm->m_flags &= ~M_EOR;
76211819Sjulian						mm = mm->m_next;
76311819Sjulian					    }
76411819Sjulian					}
76511819Sjulian					error = spx_output(cb, m);
76611819Sjulian					if (error) {
76711819Sjulian						cb->s_cc |= oldEM;
76811819Sjulian						m_freem(m0);
76925652Sjhay						return (error);
77011819Sjulian					}
77111819Sjulian					m_adj(m0, mtu);
77211819Sjulian					len -= mtu;
77311819Sjulian				}
77411819Sjulian				cb->s_cc |= oldEM;
77511819Sjulian			}
77611819Sjulian		}
77711819Sjulian		/*
77811819Sjulian		 * Force length even, by adding a "garbage byte" if
77911819Sjulian		 * necessary.
78011819Sjulian		 */
78111819Sjulian		if (len & 1) {
78211819Sjulian			m = mprev;
78311819Sjulian			if (M_TRAILINGSPACE(m) >= 1)
78411819Sjulian				m->m_len++;
78511819Sjulian			else {
78611819Sjulian				struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
78711819Sjulian
78825652Sjhay				if (m1 == NULL) {
78911819Sjulian					m_freem(m0);
79011819Sjulian					return (ENOBUFS);
79111819Sjulian				}
79211819Sjulian				m1->m_len = 1;
79311819Sjulian				*(mtod(m1, u_char *)) = 0;
79411819Sjulian				m->m_next = m1;
79511819Sjulian			}
79611819Sjulian		}
79711819Sjulian		m = m_gethdr(M_DONTWAIT, MT_HEADER);
79825652Sjhay		if (m == NULL) {
79911819Sjulian			m_freem(m0);
80011819Sjulian			return (ENOBUFS);
80111819Sjulian		}
80211819Sjulian		/*
80311819Sjulian		 * Fill in mbuf with extended SP header
80411819Sjulian		 * and addresses and length put into network format.
80511819Sjulian		 */
80625652Sjhay		MH_ALIGN(m, sizeof(struct spx));
80725652Sjhay		m->m_len = sizeof(struct spx);
80811819Sjulian		m->m_next = m0;
80911819Sjulian		si = mtod(m, struct spx *);
81011819Sjulian		si->si_i = *cb->s_ipx;
81111819Sjulian		si->si_s = cb->s_shdr;
81211819Sjulian		if ((cb->s_flags & SF_PI) && (cb->s_flags & SF_HO)) {
81311819Sjulian			register struct spxhdr *sh;
81425652Sjhay			if (m0->m_len < sizeof(*sh)) {
81511819Sjulian				if((m0 = m_pullup(m0, sizeof(*sh))) == NULL) {
81625652Sjhay					m_free(m);
81711819Sjulian					m_freem(m0);
81811819Sjulian					return (EINVAL);
81911819Sjulian				}
82011819Sjulian				m->m_next = m0;
82111819Sjulian			}
82211819Sjulian			sh = mtod(m0, struct spxhdr *);
82311819Sjulian			si->si_dt = sh->spx_dt;
82411819Sjulian			si->si_cc |= sh->spx_cc & SPX_EM;
82525652Sjhay			m0->m_len -= sizeof(*sh);
82625652Sjhay			m0->m_data += sizeof(*sh);
82725652Sjhay			len -= sizeof(*sh);
82811819Sjulian		}
82911819Sjulian		len += sizeof(*si);
83011819Sjulian		if ((cb->s_flags2 & SF_NEWCALL) && recordp) {
83125652Sjhay			si->si_cc |= SPX_EM;
83211819Sjulian			spx_newchecks[8]++;
83311819Sjulian		}
83411819Sjulian		if (cb->s_oobflags & SF_SOOB) {
83511819Sjulian			/*
83611819Sjulian			 * Per jqj@cornell:
83711819Sjulian			 * make sure OB packets convey exactly 1 byte.
83811819Sjulian			 * If the packet is 1 byte or larger, we
83911819Sjulian			 * have already guaranted there to be at least
84011819Sjulian			 * one garbage byte for the checksum, and
84111819Sjulian			 * extra bytes shouldn't hurt!
84211819Sjulian			 */
84311819Sjulian			if (len > sizeof(*si)) {
84411819Sjulian				si->si_cc |= SPX_OB;
84511819Sjulian				len = (1 + sizeof(*si));
84611819Sjulian			}
84711819Sjulian		}
84811819Sjulian		si->si_len = htons((u_short)len);
84911819Sjulian		m->m_pkthdr.len = ((len - 1) | 1) + 1;
85011819Sjulian		/*
85111819Sjulian		 * queue stuff up for output
85211819Sjulian		 */
85311819Sjulian		sbappendrecord(sb, m);
85411819Sjulian		cb->s_seq++;
85511819Sjulian	}
85611819Sjulian#ifdef notdef
85711819Sjulian	idle = (cb->s_smax == (cb->s_rack - 1));
85811819Sjulian#endif
85911819Sjulianagain:
86011819Sjulian	sendalot = 0;
86111819Sjulian	off = cb->s_snxt - cb->s_rack;
86225652Sjhay	win = min(cb->s_swnd, (cb->s_cwnd / CUNIT));
86311819Sjulian
86411819Sjulian	/*
86511819Sjulian	 * If in persist timeout with window of 0, send a probe.
86611819Sjulian	 * Otherwise, if window is small but nonzero
86711819Sjulian	 * and timer expired, send what we can and go into
86811819Sjulian	 * transmit state.
86911819Sjulian	 */
87011819Sjulian	if (cb->s_force == 1 + SPXT_PERSIST) {
87111819Sjulian		if (win != 0) {
87211819Sjulian			cb->s_timer[SPXT_PERSIST] = 0;
87311819Sjulian			cb->s_rxtshift = 0;
87411819Sjulian		}
87511819Sjulian	}
87611819Sjulian	span = cb->s_seq - cb->s_rack;
87711819Sjulian	len = min(span, win) - off;
87811819Sjulian
87911819Sjulian	if (len < 0) {
88011819Sjulian		/*
88111819Sjulian		 * Window shrank after we went into it.
88211819Sjulian		 * If window shrank to 0, cancel pending
88311819Sjulian		 * restransmission and pull s_snxt back
88411819Sjulian		 * to (closed) window.  We will enter persist
88511819Sjulian		 * state below.  If the widndow didn't close completely,
88611819Sjulian		 * just wait for an ACK.
88711819Sjulian		 */
88811819Sjulian		len = 0;
88911819Sjulian		if (win == 0) {
89011819Sjulian			cb->s_timer[SPXT_REXMT] = 0;
89111819Sjulian			cb->s_snxt = cb->s_rack;
89211819Sjulian		}
89311819Sjulian	}
89411819Sjulian	if (len > 1)
89511819Sjulian		sendalot = 1;
89611819Sjulian	rcv_win = sbspace(&so->so_rcv);
89711819Sjulian
89811819Sjulian	/*
89911819Sjulian	 * Send if we owe peer an ACK.
90011819Sjulian	 */
90111819Sjulian	if (cb->s_oobflags & SF_SOOB) {
90211819Sjulian		/*
90311819Sjulian		 * must transmit this out of band packet
90411819Sjulian		 */
90511819Sjulian		cb->s_oobflags &= ~ SF_SOOB;
90611819Sjulian		sendalot = 1;
90711819Sjulian		spxstat.spxs_sndurg++;
90811819Sjulian		goto found;
90911819Sjulian	}
91011819Sjulian	if (cb->s_flags & SF_ACKNOW)
91111819Sjulian		goto send;
91211819Sjulian	if (cb->s_state < TCPS_ESTABLISHED)
91311819Sjulian		goto send;
91411819Sjulian	/*
91511819Sjulian	 * Silly window can't happen in spx.
91611819Sjulian	 * Code from tcp deleted.
91711819Sjulian	 */
91811819Sjulian	if (len)
91911819Sjulian		goto send;
92011819Sjulian	/*
92111819Sjulian	 * Compare available window to amount of window
92211819Sjulian	 * known to peer (as advertised window less
92311819Sjulian	 * next expected input.)  If the difference is at least two
92411819Sjulian	 * packets or at least 35% of the mximum possible window,
92511819Sjulian	 * then want to send a window update to peer.
92611819Sjulian	 */
92711819Sjulian	if (rcv_win > 0) {
92811819Sjulian		u_short delta =  1 + cb->s_alo - cb->s_ack;
92911819Sjulian		int adv = rcv_win - (delta * cb->s_mtu);
93011819Sjulian
93111819Sjulian		if ((so->so_rcv.sb_cc == 0 && adv >= (2 * cb->s_mtu)) ||
93211819Sjulian		    (100 * adv / so->so_rcv.sb_hiwat >= 35)) {
93311819Sjulian			spxstat.spxs_sndwinup++;
93411819Sjulian			cb->s_flags |= SF_ACKNOW;
93511819Sjulian			goto send;
93611819Sjulian		}
93711819Sjulian
93811819Sjulian	}
93911819Sjulian	/*
94011819Sjulian	 * Many comments from tcp_output.c are appropriate here
94111819Sjulian	 * including . . .
94211819Sjulian	 * If send window is too small, there is data to transmit, and no
94311819Sjulian	 * retransmit or persist is pending, then go to persist state.
94411819Sjulian	 * If nothing happens soon, send when timer expires:
94511819Sjulian	 * if window is nonzero, transmit what we can,
94611819Sjulian	 * otherwise send a probe.
94711819Sjulian	 */
94811819Sjulian	if (so->so_snd.sb_cc && cb->s_timer[SPXT_REXMT] == 0 &&
94911819Sjulian		cb->s_timer[SPXT_PERSIST] == 0) {
95011819Sjulian			cb->s_rxtshift = 0;
95111819Sjulian			spx_setpersist(cb);
95211819Sjulian	}
95311819Sjulian	/*
95411819Sjulian	 * No reason to send a packet, just return.
95511819Sjulian	 */
95611819Sjulian	cb->s_outx = 1;
95711819Sjulian	return (0);
95811819Sjulian
95911819Sjuliansend:
96011819Sjulian	/*
96111819Sjulian	 * Find requested packet.
96211819Sjulian	 */
96311819Sjulian	si = 0;
96411819Sjulian	if (len > 0) {
96511819Sjulian		cb->s_want = cb->s_snxt;
96625652Sjhay		for (m = sb->sb_mb; m != NULL; m = m->m_act) {
96711819Sjulian			si = mtod(m, struct spx *);
96811819Sjulian			if (SSEQ_LEQ(cb->s_snxt, si->si_seq))
96911819Sjulian				break;
97011819Sjulian		}
97111819Sjulian	found:
97225652Sjhay		if (si != NULL) {
97311819Sjulian			if (si->si_seq == cb->s_snxt)
97411819Sjulian					cb->s_snxt++;
97511819Sjulian				else
97611819Sjulian					spxstat.spxs_sndvoid++, si = 0;
97711819Sjulian		}
97811819Sjulian	}
97911819Sjulian	/*
98011819Sjulian	 * update window
98111819Sjulian	 */
98211819Sjulian	if (rcv_win < 0)
98311819Sjulian		rcv_win = 0;
98411819Sjulian	alo = cb->s_ack - 1 + (rcv_win / ((short)cb->s_mtu));
98511819Sjulian	if (SSEQ_LT(alo, cb->s_alo))
98611819Sjulian		alo = cb->s_alo;
98711819Sjulian
98825652Sjhay	if (si != NULL) {
98911819Sjulian		/*
99011819Sjulian		 * must make a copy of this packet for
99111819Sjulian		 * ipx_output to monkey with
99211819Sjulian		 */
99311819Sjulian		m = m_copy(dtom(si), 0, (int)M_COPYALL);
99411819Sjulian		if (m == NULL) {
99511819Sjulian			return (ENOBUFS);
99611819Sjulian		}
99711819Sjulian		si = mtod(m, struct spx *);
99811819Sjulian		if (SSEQ_LT(si->si_seq, cb->s_smax))
99911819Sjulian			spxstat.spxs_sndrexmitpack++;
100011819Sjulian		else
100111819Sjulian			spxstat.spxs_sndpack++;
100211819Sjulian	} else if (cb->s_force || cb->s_flags & SF_ACKNOW) {
100311819Sjulian		/*
100411819Sjulian		 * Must send an acknowledgement or a probe
100511819Sjulian		 */
100611819Sjulian		if (cb->s_force)
100711819Sjulian			spxstat.spxs_sndprobe++;
100811819Sjulian		if (cb->s_flags & SF_ACKNOW)
100911819Sjulian			spxstat.spxs_sndacks++;
101011819Sjulian		m = m_gethdr(M_DONTWAIT, MT_HEADER);
101125652Sjhay		if (m == NULL)
101211819Sjulian			return (ENOBUFS);
101311819Sjulian		/*
101411819Sjulian		 * Fill in mbuf with extended SP header
101511819Sjulian		 * and addresses and length put into network format.
101611819Sjulian		 */
101725652Sjhay		MH_ALIGN(m, sizeof(struct spx));
101825652Sjhay		m->m_len = sizeof(*si);
101925652Sjhay		m->m_pkthdr.len = sizeof(*si);
102011819Sjulian		si = mtod(m, struct spx *);
102111819Sjulian		si->si_i = *cb->s_ipx;
102211819Sjulian		si->si_s = cb->s_shdr;
102311819Sjulian		si->si_seq = cb->s_smax + 1;
102425652Sjhay		si->si_len = htons(sizeof(*si));
102511819Sjulian		si->si_cc |= SPX_SP;
102611819Sjulian	} else {
102711819Sjulian		cb->s_outx = 3;
102811819Sjulian		if (so->so_options & SO_DEBUG || traceallspxs)
102911819Sjulian			spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
103011819Sjulian		return (0);
103111819Sjulian	}
103211819Sjulian	/*
103311819Sjulian	 * Stuff checksum and output datagram.
103411819Sjulian	 */
103511819Sjulian	if ((si->si_cc & SPX_SP) == 0) {
103611819Sjulian		if (cb->s_force != (1 + SPXT_PERSIST) ||
103711819Sjulian		    cb->s_timer[SPXT_PERSIST] == 0) {
103811819Sjulian			/*
103911819Sjulian			 * If this is a new packet and we are not currently
104011819Sjulian			 * timing anything, time this one.
104111819Sjulian			 */
104211819Sjulian			if (SSEQ_LT(cb->s_smax, si->si_seq)) {
104311819Sjulian				cb->s_smax = si->si_seq;
104411819Sjulian				if (cb->s_rtt == 0) {
104511819Sjulian					spxstat.spxs_segstimed++;
104611819Sjulian					cb->s_rtseq = si->si_seq;
104711819Sjulian					cb->s_rtt = 1;
104811819Sjulian				}
104911819Sjulian			}
105011819Sjulian			/*
105111819Sjulian			 * Set rexmt timer if not currently set,
105211819Sjulian			 * Initial value for retransmit timer is smoothed
105311819Sjulian			 * round-trip time + 2 * round-trip time variance.
105411819Sjulian			 * Initialize shift counter which is used for backoff
105511819Sjulian			 * of retransmit time.
105611819Sjulian			 */
105711819Sjulian			if (cb->s_timer[SPXT_REXMT] == 0 &&
105811819Sjulian			    cb->s_snxt != cb->s_rack) {
105911819Sjulian				cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
106011819Sjulian				if (cb->s_timer[SPXT_PERSIST]) {
106111819Sjulian					cb->s_timer[SPXT_PERSIST] = 0;
106211819Sjulian					cb->s_rxtshift = 0;
106311819Sjulian				}
106411819Sjulian			}
106511819Sjulian		} else if (SSEQ_LT(cb->s_smax, si->si_seq)) {
106611819Sjulian			cb->s_smax = si->si_seq;
106711819Sjulian		}
106811819Sjulian	} else if (cb->s_state < TCPS_ESTABLISHED) {
106911819Sjulian		if (cb->s_rtt == 0)
107011819Sjulian			cb->s_rtt = 1; /* Time initial handshake */
107111819Sjulian		if (cb->s_timer[SPXT_REXMT] == 0)
107211819Sjulian			cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
107311819Sjulian	}
107411819Sjulian	{
107511819Sjulian		/*
107611819Sjulian		 * Do not request acks when we ack their data packets or
107711819Sjulian		 * when we do a gratuitous window update.
107811819Sjulian		 */
107911819Sjulian		if (((si->si_cc & SPX_SP) == 0) || cb->s_force)
108011819Sjulian				si->si_cc |= SPX_SA;
108111819Sjulian		si->si_seq = htons(si->si_seq);
108211819Sjulian		si->si_alo = htons(alo);
108311819Sjulian		si->si_ack = htons(cb->s_ack);
108411819Sjulian
108511819Sjulian		if (ipxcksum) {
108650519Sjhay			si->si_sum = ipx_cksum(m, ntohs(si->si_len));
108711819Sjulian		} else
108811819Sjulian			si->si_sum = 0xffff;
108911819Sjulian
109011819Sjulian		cb->s_outx = 4;
109111819Sjulian		if (so->so_options & SO_DEBUG || traceallspxs)
109211819Sjulian			spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
109311819Sjulian
109411819Sjulian		if (so->so_options & SO_DONTROUTE)
109525652Sjhay			error = ipx_outputfl(m, (struct route *)NULL, IPX_ROUTETOIF);
109611819Sjulian		else
109711819Sjulian			error = ipx_outputfl(m, &cb->s_ipxpcb->ipxp_route, 0);
109811819Sjulian	}
109911819Sjulian	if (error) {
110011819Sjulian		return (error);
110111819Sjulian	}
110211819Sjulian	spxstat.spxs_sndtotal++;
110311819Sjulian	/*
110411819Sjulian	 * Data sent (as far as we can tell).
110511819Sjulian	 * If this advertises a larger window than any other segment,
110611819Sjulian	 * then remember the size of the advertized window.
110711819Sjulian	 * Any pending ACK has now been sent.
110811819Sjulian	 */
110911819Sjulian	cb->s_force = 0;
111011819Sjulian	cb->s_flags &= ~(SF_ACKNOW|SF_DELACK);
111111819Sjulian	if (SSEQ_GT(alo, cb->s_alo))
111211819Sjulian		cb->s_alo = alo;
111311819Sjulian	if (sendalot)
111411819Sjulian		goto again;
111511819Sjulian	cb->s_outx = 5;
111611819Sjulian	return (0);
111711819Sjulian}
111811819Sjulian
111933181Seivindstatic int spx_do_persist_panics = 0;
112011819Sjulian
112125652Sjhaystatic void
112211819Sjulianspx_setpersist(cb)
112311819Sjulian	register struct spxpcb *cb;
112411819Sjulian{
112535599Sbde	register int t = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
112611819Sjulian
112711819Sjulian	if (cb->s_timer[SPXT_REXMT] && spx_do_persist_panics)
112811819Sjulian		panic("spx_output REXMT");
112911819Sjulian	/*
113011819Sjulian	 * Start/restart persistance timer.
113111819Sjulian	 */
113211819Sjulian	SPXT_RANGESET(cb->s_timer[SPXT_PERSIST],
113311819Sjulian	    t*spx_backoff[cb->s_rxtshift],
113411819Sjulian	    SPXTV_PERSMIN, SPXTV_PERSMAX);
113511819Sjulian	if (cb->s_rxtshift < SPX_MAXRXTSHIFT)
113611819Sjulian		cb->s_rxtshift++;
113711819Sjulian}
113825652Sjhay
113911819Sjulianint
114038482Swollmanspx_ctloutput(so, sopt)
114111819Sjulian	struct socket *so;
114238482Swollman	struct sockopt *sopt;
114311819Sjulian{
114411819Sjulian	struct ipxpcb *ipxp = sotoipxpcb(so);
114511819Sjulian	register struct spxpcb *cb;
114638482Swollman	int mask, error;
114738482Swollman	short soptval;
114838482Swollman	u_short usoptval;
114938482Swollman	int optval;
115011819Sjulian
115138482Swollman	error = 0;
115238482Swollman
115338482Swollman	if (sopt->sopt_level != IPXPROTO_SPX) {
115411819Sjulian		/* This will have to be changed when we do more general
115511819Sjulian		   stacking of protocols */
115638482Swollman		return (ipx_ctloutput(so, sopt));
115711819Sjulian	}
115838482Swollman	if (ipxp == NULL)
115938482Swollman		return (EINVAL);
116038482Swollman	else
116111819Sjulian		cb = ipxtospxpcb(ipxp);
116211819Sjulian
116338482Swollman	switch (sopt->sopt_dir) {
116438482Swollman	case SOPT_GET:
116538482Swollman		switch (sopt->sopt_name) {
116611819Sjulian		case SO_HEADERS_ON_INPUT:
116711819Sjulian			mask = SF_HI;
116811819Sjulian			goto get_flags;
116911819Sjulian
117011819Sjulian		case SO_HEADERS_ON_OUTPUT:
117111819Sjulian			mask = SF_HO;
117211819Sjulian		get_flags:
117338482Swollman			soptval = cb->s_flags & mask;
117438482Swollman			error = sooptcopyout(sopt, &soptval, sizeof soptval);
117511819Sjulian			break;
117611819Sjulian
117711819Sjulian		case SO_MTU:
117838482Swollman			usoptval = cb->s_mtu;
117938482Swollman			error = sooptcopyout(sopt, &usoptval, sizeof usoptval);
118011819Sjulian			break;
118111819Sjulian
118211819Sjulian		case SO_LAST_HEADER:
118338482Swollman			error = sooptcopyout(sopt, &cb->s_rhdr,
118438482Swollman					     sizeof cb->s_rhdr);
118511819Sjulian			break;
118611819Sjulian
118711819Sjulian		case SO_DEFAULT_HEADERS:
118838482Swollman			error = sooptcopyout(sopt, &cb->s_shdr,
118938482Swollman					     sizeof cb->s_shdr);
119011819Sjulian			break;
119111819Sjulian
119211819Sjulian		default:
119338482Swollman			error = ENOPROTOOPT;
119411819Sjulian		}
119511819Sjulian		break;
119611819Sjulian
119738482Swollman	case SOPT_SET:
119838482Swollman		switch (sopt->sopt_name) {
119938482Swollman			/* XXX why are these shorts on get and ints on set?
120038482Swollman			   that doesn't make any sense... */
120111819Sjulian		case SO_HEADERS_ON_INPUT:
120211819Sjulian			mask = SF_HI;
120311819Sjulian			goto set_head;
120411819Sjulian
120511819Sjulian		case SO_HEADERS_ON_OUTPUT:
120611819Sjulian			mask = SF_HO;
120711819Sjulian		set_head:
120838482Swollman			error = sooptcopyin(sopt, &optval, sizeof optval,
120938482Swollman					    sizeof optval);
121038482Swollman			if (error)
121138482Swollman				break;
121238482Swollman
121311819Sjulian			if (cb->s_flags & SF_PI) {
121438482Swollman				if (optval)
121511819Sjulian					cb->s_flags |= mask;
121611819Sjulian				else
121711819Sjulian					cb->s_flags &= ~mask;
121811819Sjulian			} else error = EINVAL;
121911819Sjulian			break;
122011819Sjulian
122111819Sjulian		case SO_MTU:
122238482Swollman			error = sooptcopyin(sopt, &usoptval, sizeof usoptval,
122338482Swollman					    sizeof usoptval);
122438482Swollman			if (error)
122538482Swollman				break;
122638482Swollman			cb->s_mtu = usoptval;
122711819Sjulian			break;
122811819Sjulian
122911819Sjulian#ifdef SF_NEWCALL
123011819Sjulian		case SO_NEWCALL:
123138482Swollman			error = sooptcopyin(sopt, &optval, sizeof optval,
123238482Swollman					    sizeof optval);
123338482Swollman			if (error)
123438482Swollman				break;
123538482Swollman			if (optval) {
123611819Sjulian				cb->s_flags2 |= SF_NEWCALL;
123711819Sjulian				spx_newchecks[5]++;
123811819Sjulian			} else {
123911819Sjulian				cb->s_flags2 &= ~SF_NEWCALL;
124011819Sjulian				spx_newchecks[6]++;
124111819Sjulian			}
124211819Sjulian			break;
124311819Sjulian#endif
124411819Sjulian
124511819Sjulian		case SO_DEFAULT_HEADERS:
124611819Sjulian			{
124738482Swollman				struct spxhdr sp;
124838482Swollman
124938482Swollman				error = sooptcopyin(sopt, &sp, sizeof sp,
125038482Swollman						    sizeof sp);
125138482Swollman				if (error)
125238482Swollman					break;
125338482Swollman				cb->s_dt = sp.spx_dt;
125438482Swollman				cb->s_cc = sp.spx_cc & SPX_EM;
125511819Sjulian			}
125611819Sjulian			break;
125711819Sjulian
125811819Sjulian		default:
125938482Swollman			error = ENOPROTOOPT;
126011819Sjulian		}
126111819Sjulian		break;
126211819Sjulian	}
126338482Swollman	return (error);
126411819Sjulian}
126511819Sjulian
126624659Sjhaystatic int
126724659Sjhayspx_usr_abort(so)
126811819Sjulian	struct socket *so;
126911819Sjulian{
127024659Sjhay	int s;
127124659Sjhay	struct ipxpcb *ipxp;
127224659Sjhay	struct spxpcb *cb;
127311819Sjulian
127424659Sjhay	ipxp = sotoipxpcb(so);
127524659Sjhay	cb = ipxtospxpcb(ipxp);
127611819Sjulian
127724659Sjhay	s = splnet();
127824659Sjhay	spx_drop(cb, ECONNABORTED);
127924659Sjhay	splx(s);
128024659Sjhay	return (0);
128124659Sjhay}
128211819Sjulian
128324659Sjhay/*
128424659Sjhay * Accept a connection.  Essentially all the work is
128524659Sjhay * done at higher levels; just return the address
128624659Sjhay * of the peer, storing through addr.
128724659Sjhay */
128824659Sjhaystatic int
128924659Sjhayspx_accept(so, nam)
129024659Sjhay	struct socket *so;
129128270Swollman	struct sockaddr **nam;
129224659Sjhay{
129324659Sjhay	struct ipxpcb *ipxp;
129428270Swollman	struct sockaddr_ipx *sipx, ssipx;
129511819Sjulian
129624659Sjhay	ipxp = sotoipxpcb(so);
129728270Swollman	sipx = &ssipx;
129828270Swollman	bzero(sipx, sizeof *sipx);
129928270Swollman	sipx->sipx_len = sizeof *sipx;
130024659Sjhay	sipx->sipx_family = AF_IPX;
130124659Sjhay	sipx->sipx_addr = ipxp->ipxp_faddr;
130228270Swollman	*nam = dup_sockaddr((struct sockaddr *)sipx, 0);
130324659Sjhay	return (0);
130424659Sjhay}
130524659Sjhay
130624659Sjhaystatic int
130725345Sjhayspx_attach(so, proto, p)
130824659Sjhay	struct socket *so;
130924659Sjhay	int proto;
131025345Sjhay	struct proc *p;
131124659Sjhay{
131224659Sjhay	int error;
131324659Sjhay	int s;
131424659Sjhay	struct ipxpcb *ipxp;
131524659Sjhay	struct spxpcb *cb;
131624659Sjhay	struct mbuf *mm;
131724659Sjhay	struct sockbuf *sb;
131824659Sjhay
131924659Sjhay	ipxp = sotoipxpcb(so);
132024659Sjhay	cb = ipxtospxpcb(ipxp);
132124659Sjhay
132224659Sjhay	if (ipxp != NULL)
132324659Sjhay		return (EISCONN);
132424659Sjhay	s = splnet();
132525345Sjhay	error = ipx_pcballoc(so, &ipxpcb, p);
132624659Sjhay	if (error)
132724659Sjhay		goto spx_attach_end;
132824659Sjhay	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
132924659Sjhay		error = soreserve(so, (u_long) 3072, (u_long) 3072);
133011819Sjulian		if (error)
133124659Sjhay			goto spx_attach_end;
133224659Sjhay	}
133324659Sjhay	ipxp = sotoipxpcb(so);
133411819Sjulian
133569781Sdwmalone	MALLOC(cb, struct spxpcb *, sizeof *cb, M_PCB, M_NOWAIT | M_ZERO);
133611819Sjulian
133728270Swollman	if (cb == NULL) {
133824659Sjhay		error = ENOBUFS;
133924659Sjhay		goto spx_attach_end;
134024659Sjhay	}
134143711Sjhay	sb = &so->so_snd;
134228270Swollman
134324659Sjhay	mm = m_getclr(M_DONTWAIT, MT_HEADER);
134424659Sjhay	if (mm == NULL) {
134528270Swollman		FREE(cb, M_PCB);
134624659Sjhay		error = ENOBUFS;
134724659Sjhay		goto spx_attach_end;
134824659Sjhay	}
134924659Sjhay	cb->s_ipx = mtod(mm, struct ipx *);
135024659Sjhay	cb->s_state = TCPS_LISTEN;
135124659Sjhay	cb->s_smax = -1;
135224659Sjhay	cb->s_swl1 = -1;
135324659Sjhay	cb->s_q.si_next = cb->s_q.si_prev = &cb->s_q;
135424659Sjhay	cb->s_ipxpcb = ipxp;
135525652Sjhay	cb->s_mtu = 576 - sizeof(struct spx);
135624659Sjhay	cb->s_cwnd = sbspace(sb) * CUNIT / cb->s_mtu;
135724659Sjhay	cb->s_ssthresh = cb->s_cwnd;
135825652Sjhay	cb->s_cwmx = sbspace(sb) * CUNIT / (2 * sizeof(struct spx));
135924659Sjhay	/* Above is recomputed when connecting to account
136024659Sjhay	   for changed buffering or mtu's */
136124659Sjhay	cb->s_rtt = SPXTV_SRTTBASE;
136224659Sjhay	cb->s_rttvar = SPXTV_SRTTDFLT << 2;
136324659Sjhay	SPXT_RANGESET(cb->s_rxtcur,
136424659Sjhay	    ((SPXTV_SRTTBASE >> 2) + (SPXTV_SRTTDFLT << 2)) >> 1,
136524659Sjhay	    SPXTV_MIN, SPXTV_REXMTMAX);
136625652Sjhay	ipxp->ipxp_pcb = (caddr_t)cb;
136724659Sjhayspx_attach_end:
136824659Sjhay	splx(s);
136924659Sjhay	return (error);
137024659Sjhay}
137111819Sjulian
137224659Sjhaystatic int
137325345Sjhayspx_bind(so, nam, p)
137424659Sjhay	struct socket *so;
137528270Swollman	struct sockaddr *nam;
137625345Sjhay	struct proc *p;
137724659Sjhay{
137824659Sjhay	struct ipxpcb *ipxp;
137911819Sjulian
138024659Sjhay	ipxp = sotoipxpcb(so);
138111819Sjulian
138225345Sjhay	return (ipx_pcbbind(ipxp, nam, p));
138324659Sjhay}
138424659Sjhay
138524659Sjhay/*
138624659Sjhay * Initiate connection to peer.
138724659Sjhay * Enter SYN_SENT state, and mark socket as connecting.
138824659Sjhay * Start keep-alive timer, setup prototype header,
138924659Sjhay * Send initial system packet requesting connection.
139024659Sjhay */
139124659Sjhaystatic int
139225345Sjhayspx_connect(so, nam, p)
139324659Sjhay	struct socket *so;
139428270Swollman	struct sockaddr *nam;
139525345Sjhay	struct proc *p;
139624659Sjhay{
139724659Sjhay	int error;
139824659Sjhay	int s;
139924659Sjhay	struct ipxpcb *ipxp;
140024659Sjhay	struct spxpcb *cb;
140111819Sjulian
140224659Sjhay	ipxp = sotoipxpcb(so);
140324659Sjhay	cb = ipxtospxpcb(ipxp);
140424659Sjhay
140524659Sjhay	s = splnet();
140624659Sjhay	if (ipxp->ipxp_lport == 0) {
140728270Swollman		error = ipx_pcbbind(ipxp, (struct sockaddr *)NULL, p);
140824659Sjhay		if (error)
140924659Sjhay			goto spx_connect_end;
141024659Sjhay	}
141125345Sjhay	error = ipx_pcbconnect(ipxp, nam, p);
141224659Sjhay	if (error)
141324659Sjhay		goto spx_connect_end;
141424659Sjhay	soisconnecting(so);
141524659Sjhay	spxstat.spxs_connattempt++;
141624659Sjhay	cb->s_state = TCPS_SYN_SENT;
141724659Sjhay	cb->s_did = 0;
141824659Sjhay	spx_template(cb);
141924659Sjhay	cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
142024659Sjhay	cb->s_force = 1 + SPXTV_KEEP;
142111819Sjulian	/*
142224659Sjhay	 * Other party is required to respond to
142324659Sjhay	 * the port I send from, but he is not
142424659Sjhay	 * required to answer from where I am sending to,
142524659Sjhay	 * so allow wildcarding.
142624659Sjhay	 * original port I am sending to is still saved in
142724659Sjhay	 * cb->s_dport.
142811819Sjulian	 */
142924659Sjhay	ipxp->ipxp_fport = 0;
143025652Sjhay	error = spx_output(cb, (struct mbuf *)NULL);
143124659Sjhayspx_connect_end:
143224659Sjhay	splx(s);
143324659Sjhay	return (error);
143424659Sjhay}
143511819Sjulian
143624659Sjhaystatic int
143724659Sjhayspx_detach(so)
143824659Sjhay	struct socket *so;
143924659Sjhay{
144024659Sjhay	int s;
144124659Sjhay	struct ipxpcb *ipxp;
144224659Sjhay	struct spxpcb *cb;
144311819Sjulian
144424659Sjhay	ipxp = sotoipxpcb(so);
144524659Sjhay	cb = ipxtospxpcb(ipxp);
144611819Sjulian
144724659Sjhay	if (ipxp == NULL)
144824659Sjhay		return (ENOTCONN);
144924659Sjhay	s = splnet();
145024659Sjhay	if (cb->s_state > TCPS_LISTEN)
145124659Sjhay		spx_disconnect(cb);
145224659Sjhay	else
145324659Sjhay		spx_close(cb);
145424659Sjhay	splx(s);
145524659Sjhay	return (0);
145624659Sjhay}
145711819Sjulian
145824659Sjhay/*
145924659Sjhay * We may decide later to implement connection closing
146024659Sjhay * handshaking at the spx level optionally.
146124659Sjhay * here is the hook to do it:
146224659Sjhay */
146324659Sjhaystatic int
146424659Sjhayspx_usr_disconnect(so)
146524659Sjhay	struct socket *so;
146624659Sjhay{
146724659Sjhay	int s;
146824659Sjhay	struct ipxpcb *ipxp;
146924659Sjhay	struct spxpcb *cb;
147011819Sjulian
147124659Sjhay	ipxp = sotoipxpcb(so);
147224659Sjhay	cb = ipxtospxpcb(ipxp);
147311819Sjulian
147424659Sjhay	s = splnet();
147524659Sjhay	spx_disconnect(cb);
147624659Sjhay	splx(s);
147724659Sjhay	return (0);
147824659Sjhay}
147911819Sjulian
148024659Sjhaystatic int
148125345Sjhayspx_listen(so, p)
148224659Sjhay	struct socket *so;
148325345Sjhay	struct proc *p;
148424659Sjhay{
148524659Sjhay	int error;
148624659Sjhay	struct ipxpcb *ipxp;
148724659Sjhay	struct spxpcb *cb;
148811819Sjulian
148924659Sjhay	error = 0;
149024659Sjhay	ipxp = sotoipxpcb(so);
149124659Sjhay	cb = ipxtospxpcb(ipxp);
149211819Sjulian
149324659Sjhay	if (ipxp->ipxp_lport == 0)
149428270Swollman		error = ipx_pcbbind(ipxp, (struct sockaddr *)NULL, p);
149524659Sjhay	if (error == 0)
149624659Sjhay		cb->s_state = TCPS_LISTEN;
149724659Sjhay	return (error);
149824659Sjhay}
149911819Sjulian
150024659Sjhay/*
150124659Sjhay * After a receive, possibly send acknowledgment
150224659Sjhay * updating allocation.
150324659Sjhay */
150424659Sjhaystatic int
150524659Sjhayspx_rcvd(so, flags)
150624659Sjhay	struct socket *so;
150724659Sjhay	int flags;
150824659Sjhay{
150924659Sjhay	int s;
151024659Sjhay	struct ipxpcb *ipxp;
151124659Sjhay	struct spxpcb *cb;
151211819Sjulian
151324659Sjhay	ipxp = sotoipxpcb(so);
151424659Sjhay	cb = ipxtospxpcb(ipxp);
151511819Sjulian
151624659Sjhay	s = splnet();
151724659Sjhay	cb->s_flags |= SF_RVD;
151825652Sjhay	spx_output(cb, (struct mbuf *)NULL);
151924659Sjhay	cb->s_flags &= ~SF_RVD;
152024659Sjhay	splx(s);
152124659Sjhay	return (0);
152224659Sjhay}
152311819Sjulian
152424659Sjhaystatic int
152524659Sjhayspx_rcvoob(so, m, flags)
152624659Sjhay	struct socket *so;
152724659Sjhay	struct mbuf *m;
152824659Sjhay	int flags;
152924659Sjhay{
153024659Sjhay	struct ipxpcb *ipxp;
153124659Sjhay	struct spxpcb *cb;
153211819Sjulian
153324659Sjhay	ipxp = sotoipxpcb(so);
153424659Sjhay	cb = ipxtospxpcb(ipxp);
153511819Sjulian
153624659Sjhay	if ((cb->s_oobflags & SF_IOOB) || so->so_oobmark ||
153724659Sjhay	    (so->so_state & SS_RCVATMARK)) {
153824659Sjhay		m->m_len = 1;
153924659Sjhay		*mtod(m, caddr_t) = cb->s_iobc;
154024659Sjhay		return (0);
154111819Sjulian	}
154224659Sjhay	return (EINVAL);
154324659Sjhay}
154424659Sjhay
154524659Sjhaystatic int
154625345Sjhayspx_send(so, flags, m, addr, controlp, p)
154724659Sjhay	struct socket *so;
154824659Sjhay	int flags;
154924659Sjhay	struct mbuf *m;
155028270Swollman	struct sockaddr *addr;
155124659Sjhay	struct mbuf *controlp;
155225345Sjhay	struct proc *p;
155324659Sjhay{
155424659Sjhay	int error;
155524659Sjhay	int s;
155624659Sjhay	struct ipxpcb *ipxp;
155724659Sjhay	struct spxpcb *cb;
155824659Sjhay
155924659Sjhay	error = 0;
156024659Sjhay	ipxp = sotoipxpcb(so);
156124659Sjhay	cb = ipxtospxpcb(ipxp);
156224659Sjhay
156324659Sjhay	s = splnet();
156424659Sjhay	if (flags & PRUS_OOB) {
156524659Sjhay		if (sbspace(&so->so_snd) < -512) {
156624659Sjhay			error = ENOBUFS;
156724659Sjhay			goto spx_send_end;
156824659Sjhay		}
156924659Sjhay		cb->s_oobflags |= SF_SOOB;
157024659Sjhay	}
157125652Sjhay	if (controlp != NULL) {
157224659Sjhay		u_short *p = mtod(controlp, u_short *);
157324659Sjhay		spx_newchecks[2]++;
157425652Sjhay		if ((p[0] == 5) && (p[1] == 1)) { /* XXXX, for testing */
157524659Sjhay			cb->s_shdr.spx_dt = *(u_char *)(&p[2]);
157624659Sjhay			spx_newchecks[3]++;
157724659Sjhay		}
157824659Sjhay		m_freem(controlp);
157924659Sjhay	}
158024659Sjhay	controlp = NULL;
158124659Sjhay	error = spx_output(cb, m);
158224659Sjhay	m = NULL;
158324659Sjhayspx_send_end:
158411819Sjulian	if (controlp != NULL)
158511819Sjulian		m_freem(controlp);
158611819Sjulian	if (m != NULL)
158711819Sjulian		m_freem(m);
158811819Sjulian	splx(s);
158911819Sjulian	return (error);
159011819Sjulian}
159111819Sjulian
159224659Sjhaystatic int
159324659Sjhayspx_shutdown(so)
159424659Sjhay	struct socket *so;
159524659Sjhay{
159624659Sjhay	int error;
159724659Sjhay	int s;
159824659Sjhay	struct ipxpcb *ipxp;
159924659Sjhay	struct spxpcb *cb;
160024659Sjhay
160124659Sjhay	error = 0;
160224659Sjhay	ipxp = sotoipxpcb(so);
160324659Sjhay	cb = ipxtospxpcb(ipxp);
160424659Sjhay
160524659Sjhay	s = splnet();
160624659Sjhay	socantsendmore(so);
160724659Sjhay	cb = spx_usrclosed(cb);
160825652Sjhay	if (cb != NULL)
160925652Sjhay		error = spx_output(cb, (struct mbuf *)NULL);
161024659Sjhay	splx(s);
161124659Sjhay	return (error);
161224659Sjhay}
161324659Sjhay
161424659Sjhaystatic int
161525345Sjhayspx_sp_attach(so, proto, p)
161611819Sjulian	struct socket *so;
161724659Sjhay	int proto;
161825345Sjhay	struct proc *p;
161911819Sjulian{
162024659Sjhay	int error;
162124659Sjhay	struct ipxpcb *ipxp;
162211819Sjulian
162325345Sjhay	error = spx_attach(so, proto, p);
162424659Sjhay	if (error == 0) {
162524659Sjhay		ipxp = sotoipxpcb(so);
162611819Sjulian		((struct spxpcb *)ipxp->ipxp_pcb)->s_flags |=
162711819Sjulian					(SF_HI | SF_HO | SF_PI);
162811819Sjulian	}
162911819Sjulian	return (error);
163011819Sjulian}
163111819Sjulian
163211819Sjulian/*
163311819Sjulian * Create template to be used to send spx packets on a connection.
163411819Sjulian * Called after host entry created, fills
163511819Sjulian * in a skeletal spx header (choosing connection id),
163611819Sjulian * minimizing the amount of work necessary when the connection is used.
163711819Sjulian */
163825652Sjhaystatic void
163911819Sjulianspx_template(cb)
164011819Sjulian	register struct spxpcb *cb;
164111819Sjulian{
164211819Sjulian	register struct ipxpcb *ipxp = cb->s_ipxpcb;
164311819Sjulian	register struct ipx *ipx = cb->s_ipx;
164411819Sjulian	register struct sockbuf *sb = &(ipxp->ipxp_socket->so_snd);
164511819Sjulian
164611819Sjulian	ipx->ipx_pt = IPXPROTO_SPX;
164711819Sjulian	ipx->ipx_sna = ipxp->ipxp_laddr;
164811819Sjulian	ipx->ipx_dna = ipxp->ipxp_faddr;
164911819Sjulian	cb->s_sid = htons(spx_iss);
165011819Sjulian	spx_iss += SPX_ISSINCR/2;
165111819Sjulian	cb->s_alo = 1;
165211819Sjulian	cb->s_cwnd = (sbspace(sb) * CUNIT) / cb->s_mtu;
165311819Sjulian	cb->s_ssthresh = cb->s_cwnd; /* Try to expand fast to full complement
165411819Sjulian					of large packets */
165511819Sjulian	cb->s_cwmx = (sbspace(sb) * CUNIT) / (2 * sizeof(struct spx));
165611819Sjulian	cb->s_cwmx = max(cb->s_cwmx, cb->s_cwnd);
165711819Sjulian		/* But allow for lots of little packets as well */
165811819Sjulian}
165911819Sjulian
166011819Sjulian/*
166111819Sjulian * Close a SPIP control block:
166211819Sjulian *	discard spx control block itself
166311819Sjulian *	discard ipx protocol control block
166411819Sjulian *	wake up any sleepers
166511819Sjulian */
166625652Sjhaystatic struct spxpcb *
166711819Sjulianspx_close(cb)
166811819Sjulian	register struct spxpcb *cb;
166911819Sjulian{
167011819Sjulian	register struct spx_q *s;
167111819Sjulian	struct ipxpcb *ipxp = cb->s_ipxpcb;
167211819Sjulian	struct socket *so = ipxp->ipxp_socket;
167311819Sjulian	register struct mbuf *m;
167411819Sjulian
167511819Sjulian	s = cb->s_q.si_next;
167611819Sjulian	while (s != &(cb->s_q)) {
167711819Sjulian		s = s->si_next;
167811819Sjulian		m = dtom(s->si_prev);
167911819Sjulian		remque(s->si_prev);
168011819Sjulian		m_freem(m);
168111819Sjulian	}
168225652Sjhay	m_free(dtom(cb->s_ipx));
168328270Swollman	FREE(cb, M_PCB);
168411819Sjulian	ipxp->ipxp_pcb = 0;
168511819Sjulian	soisdisconnected(so);
168611819Sjulian	ipx_pcbdetach(ipxp);
168711819Sjulian	spxstat.spxs_closed++;
168825652Sjhay	return ((struct spxpcb *)NULL);
168911819Sjulian}
169025652Sjhay
169111819Sjulian/*
169211819Sjulian *	Someday we may do level 3 handshaking
169311819Sjulian *	to close a connection or send a xerox style error.
169411819Sjulian *	For now, just close.
169511819Sjulian */
169625652Sjhaystatic struct spxpcb *
169711819Sjulianspx_usrclosed(cb)
169811819Sjulian	register struct spxpcb *cb;
169911819Sjulian{
170011819Sjulian	return (spx_close(cb));
170111819Sjulian}
170225652Sjhay
170325652Sjhaystatic struct spxpcb *
170411819Sjulianspx_disconnect(cb)
170511819Sjulian	register struct spxpcb *cb;
170611819Sjulian{
170711819Sjulian	return (spx_close(cb));
170811819Sjulian}
170925652Sjhay
171011819Sjulian/*
171111819Sjulian * Drop connection, reporting
171211819Sjulian * the specified error.
171311819Sjulian */
171425652Sjhaystatic struct spxpcb *
171511819Sjulianspx_drop(cb, errno)
171611819Sjulian	register struct spxpcb *cb;
171711819Sjulian	int errno;
171811819Sjulian{
171911819Sjulian	struct socket *so = cb->s_ipxpcb->ipxp_socket;
172011819Sjulian
172111819Sjulian	/*
172211819Sjulian	 * someday, in the xerox world
172311819Sjulian	 * we will generate error protocol packets
172411819Sjulian	 * announcing that the socket has gone away.
172511819Sjulian	 */
172611819Sjulian	if (TCPS_HAVERCVDSYN(cb->s_state)) {
172711819Sjulian		spxstat.spxs_drops++;
172811819Sjulian		cb->s_state = TCPS_CLOSED;
172925652Sjhay		/*tcp_output(cb);*/
173011819Sjulian	} else
173111819Sjulian		spxstat.spxs_conndrops++;
173211819Sjulian	so->so_error = errno;
173311819Sjulian	return (spx_close(cb));
173411819Sjulian}
173511819Sjulian
173611819Sjulian/*
173711819Sjulian * Fast timeout routine for processing delayed acks
173811819Sjulian */
173911819Sjulianvoid
174011819Sjulianspx_fasttimo()
174111819Sjulian{
174211819Sjulian	register struct ipxpcb *ipxp;
174311819Sjulian	register struct spxpcb *cb;
174411819Sjulian	int s = splnet();
174511819Sjulian
174611819Sjulian	ipxp = ipxpcb.ipxp_next;
174725652Sjhay	if (ipxp != NULL)
174811819Sjulian	for (; ipxp != &ipxpcb; ipxp = ipxp->ipxp_next)
174925652Sjhay		if ((cb = (struct spxpcb *)ipxp->ipxp_pcb) != NULL &&
175011819Sjulian		    (cb->s_flags & SF_DELACK)) {
175111819Sjulian			cb->s_flags &= ~SF_DELACK;
175211819Sjulian			cb->s_flags |= SF_ACKNOW;
175311819Sjulian			spxstat.spxs_delack++;
175425652Sjhay			spx_output(cb, (struct mbuf *)NULL);
175511819Sjulian		}
175611819Sjulian	splx(s);
175711819Sjulian}
175811819Sjulian
175911819Sjulian/*
176011819Sjulian * spx protocol timeout routine called every 500 ms.
176111819Sjulian * Updates the timers in all active pcb's and
176211819Sjulian * causes finite state machine actions if timers expire.
176311819Sjulian */
176411819Sjulianvoid
176511819Sjulianspx_slowtimo()
176611819Sjulian{
176711819Sjulian	register struct ipxpcb *ip, *ipnxt;
176811819Sjulian	register struct spxpcb *cb;
176911819Sjulian	int s = splnet();
177011819Sjulian	register int i;
177111819Sjulian
177211819Sjulian	/*
177311819Sjulian	 * Search through tcb's and update active timers.
177411819Sjulian	 */
177511819Sjulian	ip = ipxpcb.ipxp_next;
177625652Sjhay	if (ip == NULL) {
177711819Sjulian		splx(s);
177811819Sjulian		return;
177911819Sjulian	}
178011819Sjulian	while (ip != &ipxpcb) {
178111819Sjulian		cb = ipxtospxpcb(ip);
178211819Sjulian		ipnxt = ip->ipxp_next;
178325652Sjhay		if (cb == NULL)
178411819Sjulian			goto tpgone;
178511819Sjulian		for (i = 0; i < SPXT_NTIMERS; i++) {
178611819Sjulian			if (cb->s_timer[i] && --cb->s_timer[i] == 0) {
178724659Sjhay				spx_timers(cb, i);
178811819Sjulian				if (ipnxt->ipxp_prev != ip)
178911819Sjulian					goto tpgone;
179011819Sjulian			}
179111819Sjulian		}
179211819Sjulian		cb->s_idle++;
179311819Sjulian		if (cb->s_rtt)
179411819Sjulian			cb->s_rtt++;
179511819Sjuliantpgone:
179611819Sjulian		ip = ipnxt;
179711819Sjulian	}
179811819Sjulian	spx_iss += SPX_ISSINCR/PR_SLOWHZ;		/* increment iss */
179911819Sjulian	splx(s);
180011819Sjulian}
180125652Sjhay
180211819Sjulian/*
180311819Sjulian * SPX timer processing.
180411819Sjulian */
180525652Sjhaystatic struct spxpcb *
180611819Sjulianspx_timers(cb, timer)
180711819Sjulian	register struct spxpcb *cb;
180811819Sjulian	int timer;
180911819Sjulian{
181011819Sjulian	long rexmt;
181111819Sjulian	int win;
181211819Sjulian
181311819Sjulian	cb->s_force = 1 + timer;
181411819Sjulian	switch (timer) {
181511819Sjulian
181611819Sjulian	/*
181711819Sjulian	 * 2 MSL timeout in shutdown went off.  TCP deletes connection
181811819Sjulian	 * control block.
181911819Sjulian	 */
182011819Sjulian	case SPXT_2MSL:
182111819Sjulian		printf("spx: SPXT_2MSL went off for no reason\n");
182211819Sjulian		cb->s_timer[timer] = 0;
182311819Sjulian		break;
182411819Sjulian
182511819Sjulian	/*
182611819Sjulian	 * Retransmission timer went off.  Message has not
182711819Sjulian	 * been acked within retransmit interval.  Back off
182811819Sjulian	 * to a longer retransmit interval and retransmit one packet.
182911819Sjulian	 */
183011819Sjulian	case SPXT_REXMT:
183111819Sjulian		if (++cb->s_rxtshift > SPX_MAXRXTSHIFT) {
183211819Sjulian			cb->s_rxtshift = SPX_MAXRXTSHIFT;
183311819Sjulian			spxstat.spxs_timeoutdrop++;
183411819Sjulian			cb = spx_drop(cb, ETIMEDOUT);
183511819Sjulian			break;
183611819Sjulian		}
183711819Sjulian		spxstat.spxs_rexmttimeo++;
183811819Sjulian		rexmt = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
183911819Sjulian		rexmt *= spx_backoff[cb->s_rxtshift];
184011819Sjulian		SPXT_RANGESET(cb->s_rxtcur, rexmt, SPXTV_MIN, SPXTV_REXMTMAX);
184111819Sjulian		cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
184211819Sjulian		/*
184311819Sjulian		 * If we have backed off fairly far, our srtt
184411819Sjulian		 * estimate is probably bogus.  Clobber it
184511819Sjulian		 * so we'll take the next rtt measurement as our srtt;
184611819Sjulian		 * move the current srtt into rttvar to keep the current
184711819Sjulian		 * retransmit times until then.
184811819Sjulian		 */
184911819Sjulian		if (cb->s_rxtshift > SPX_MAXRXTSHIFT / 4 ) {
185011819Sjulian			cb->s_rttvar += (cb->s_srtt >> 2);
185111819Sjulian			cb->s_srtt = 0;
185211819Sjulian		}
185311819Sjulian		cb->s_snxt = cb->s_rack;
185411819Sjulian		/*
185511819Sjulian		 * If timing a packet, stop the timer.
185611819Sjulian		 */
185711819Sjulian		cb->s_rtt = 0;
185811819Sjulian		/*
185911819Sjulian		 * See very long discussion in tcp_timer.c about congestion
186011819Sjulian		 * window and sstrhesh
186111819Sjulian		 */
186211819Sjulian		win = min(cb->s_swnd, (cb->s_cwnd/CUNIT)) / 2;
186311819Sjulian		if (win < 2)
186411819Sjulian			win = 2;
186511819Sjulian		cb->s_cwnd = CUNIT;
186611819Sjulian		cb->s_ssthresh = win * CUNIT;
186725652Sjhay		spx_output(cb, (struct mbuf *)NULL);
186811819Sjulian		break;
186911819Sjulian
187011819Sjulian	/*
187111819Sjulian	 * Persistance timer into zero window.
187211819Sjulian	 * Force a probe to be sent.
187311819Sjulian	 */
187411819Sjulian	case SPXT_PERSIST:
187511819Sjulian		spxstat.spxs_persisttimeo++;
187611819Sjulian		spx_setpersist(cb);
187725652Sjhay		spx_output(cb, (struct mbuf *)NULL);
187811819Sjulian		break;
187911819Sjulian
188011819Sjulian	/*
188111819Sjulian	 * Keep-alive timer went off; send something
188211819Sjulian	 * or drop connection if idle for too long.
188311819Sjulian	 */
188411819Sjulian	case SPXT_KEEP:
188511819Sjulian		spxstat.spxs_keeptimeo++;
188611819Sjulian		if (cb->s_state < TCPS_ESTABLISHED)
188711819Sjulian			goto dropit;
188811819Sjulian		if (cb->s_ipxpcb->ipxp_socket->so_options & SO_KEEPALIVE) {
188911819Sjulian		    	if (cb->s_idle >= SPXTV_MAXIDLE)
189011819Sjulian				goto dropit;
189111819Sjulian			spxstat.spxs_keepprobe++;
189225652Sjhay			spx_output(cb, (struct mbuf *)NULL);
189311819Sjulian		} else
189411819Sjulian			cb->s_idle = 0;
189511819Sjulian		cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
189611819Sjulian		break;
189711819Sjulian	dropit:
189811819Sjulian		spxstat.spxs_keepdrops++;
189911819Sjulian		cb = spx_drop(cb, ETIMEDOUT);
190011819Sjulian		break;
190111819Sjulian	}
190211819Sjulian	return (cb);
190311819Sjulian}
1904