spx_reass.c revision 43305
1/*
2 * Copyright (c) 1995, Mike Mitchell
3 * Copyright (c) 1984, 1985, 1986, 1987, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by the University of
17 *	California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)spx_usrreq.h
35 *
36 * $Id: spx_usrreq.c,v 1.22 1999/01/12 12:37:18 eivind Exp $
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/malloc.h>
42#include <sys/mbuf.h>
43#include <sys/proc.h>
44#include <sys/protosw.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47
48#include <net/route.h>
49#include <netinet/tcp_fsm.h>
50
51#include <netipx/ipx.h>
52#include <netipx/ipx_pcb.h>
53#include <netipx/ipx_var.h>
54#include <netipx/spx.h>
55#include <netipx/spx_timer.h>
56#include <netipx/spx_var.h>
57#include <netipx/spx_debug.h>
58
59/*
60 * SPX protocol implementation.
61 */
62static u_short 	spx_iss;
63static u_short	spx_newchecks[50];
64static int	spx_hardnosed;
65static int	spx_use_delack = 0;
66static int	traceallspxs = 0;
67static struct	spx 	spx_savesi;
68static struct	spx_istat spx_istat;
69
70/* Following was struct spxstat spxstat; */
71#ifndef spxstat
72#define spxstat spx_istat.newstats
73#endif
74
75static int spx_backoff[SPX_MAXRXTSHIFT+1] =
76    { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
77
78static	struct spxpcb *spx_close(struct spxpcb *cb);
79static	struct spxpcb *spx_disconnect(struct spxpcb *cb);
80static	struct spxpcb *spx_drop(struct spxpcb *cb, int errno);
81static	int spx_output(struct spxpcb *cb, struct mbuf *m0);
82static	int spx_reass(struct spxpcb *cb, struct spx *si);
83static	void spx_setpersist(struct spxpcb *cb);
84static	void spx_template(struct spxpcb *cb);
85static	struct spxpcb *spx_timers(struct spxpcb *cb, int timer);
86static	struct spxpcb *spx_usrclosed(struct spxpcb *cb);
87
88static	int spx_usr_abort(struct socket *so);
89static	int spx_accept(struct socket *so, struct sockaddr **nam);
90static	int spx_attach(struct socket *so, int proto, struct proc *p);
91static	int spx_bind(struct socket *so, struct sockaddr *nam, struct proc *p);
92static	int spx_connect(struct socket *so, struct sockaddr *nam,
93			struct proc *p);
94static	int spx_detach(struct socket *so);
95static	int spx_usr_disconnect(struct socket *so);
96static	int spx_listen(struct socket *so, struct proc *p);
97static	int spx_rcvd(struct socket *so, int flags);
98static	int spx_rcvoob(struct socket *so, struct mbuf *m, int flags);
99static	int spx_send(struct socket *so, int flags, struct mbuf *m,
100		     struct sockaddr *addr, struct mbuf *control,
101		     struct proc *p);
102static	int spx_shutdown(struct socket *so);
103static	int spx_sp_attach(struct socket *so, int proto, struct proc *p);
104
105struct	pr_usrreqs spx_usrreqs = {
106	spx_usr_abort, spx_accept, spx_attach, spx_bind,
107	spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
108	spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
109	spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
110	ipx_sockaddr, sosend, soreceive, sopoll
111};
112
113struct	pr_usrreqs spx_usrreq_sps = {
114	spx_usr_abort, spx_accept, spx_sp_attach, spx_bind,
115	spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
116	spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
117	spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
118	ipx_sockaddr, sosend, soreceive, sopoll
119};
120
121void
122spx_init()
123{
124
125	spx_iss = 1; /* WRONG !! should fish it out of TODR */
126}
127
128void
129spx_input(m, ipxp)
130	register struct mbuf *m;
131	register struct ipxpcb *ipxp;
132{
133	register struct spxpcb *cb;
134	register struct spx *si = mtod(m, struct spx *);
135	register struct socket *so;
136	int dropsocket = 0;
137	short ostate = 0;
138
139	spxstat.spxs_rcvtotal++;
140	if (ipxp == NULL) {
141		panic("No ipxpcb in spx_input\n");
142		return;
143	}
144
145	cb = ipxtospxpcb(ipxp);
146	if (cb == NULL)
147		goto bad;
148
149	if (m->m_len < sizeof(*si)) {
150		if ((m = m_pullup(m, sizeof(*si))) == NULL) {
151			spxstat.spxs_rcvshort++;
152			return;
153		}
154		si = mtod(m, struct spx *);
155	}
156	si->si_seq = ntohs(si->si_seq);
157	si->si_ack = ntohs(si->si_ack);
158	si->si_alo = ntohs(si->si_alo);
159
160	so = ipxp->ipxp_socket;
161
162	if (so->so_options & SO_DEBUG || traceallspxs) {
163		ostate = cb->s_state;
164		spx_savesi = *si;
165	}
166	if (so->so_options & SO_ACCEPTCONN) {
167		struct spxpcb *ocb = cb;
168
169		so = sonewconn(so, 0);
170		if (so == NULL) {
171			goto drop;
172		}
173		/*
174		 * This is ugly, but ....
175		 *
176		 * Mark socket as temporary until we're
177		 * committed to keeping it.  The code at
178		 * ``drop'' and ``dropwithreset'' check the
179		 * flag dropsocket to see if the temporary
180		 * socket created here should be discarded.
181		 * We mark the socket as discardable until
182		 * we're committed to it below in TCPS_LISTEN.
183		 */
184		dropsocket++;
185		ipxp = (struct ipxpcb *)so->so_pcb;
186		ipxp->ipxp_laddr = si->si_dna;
187		cb = ipxtospxpcb(ipxp);
188		cb->s_mtu = ocb->s_mtu;		/* preserve sockopts */
189		cb->s_flags = ocb->s_flags;	/* preserve sockopts */
190		cb->s_flags2 = ocb->s_flags2;	/* preserve sockopts */
191		cb->s_state = TCPS_LISTEN;
192	}
193
194	/*
195	 * Packet received on connection.
196	 * reset idle time and keep-alive timer;
197	 */
198	cb->s_idle = 0;
199	cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
200
201	switch (cb->s_state) {
202
203	case TCPS_LISTEN:{
204		struct sockaddr_ipx *sipx, ssipx;
205		struct ipx_addr laddr;
206
207		/*
208		 * If somebody here was carying on a conversation
209		 * and went away, and his pen pal thinks he can
210		 * still talk, we get the misdirected packet.
211		 */
212		if (spx_hardnosed && (si->si_did != 0 || si->si_seq != 0)) {
213			spx_istat.gonawy++;
214			goto dropwithreset;
215		}
216		sipx = &ssipx;
217		bzero(sipx, sizeof *sipx);
218		sipx->sipx_len = sizeof(*sipx);
219		sipx->sipx_family = AF_IPX;
220		sipx->sipx_addr = si->si_sna;
221		laddr = ipxp->ipxp_laddr;
222		if (ipx_nullhost(laddr))
223			ipxp->ipxp_laddr = si->si_dna;
224		if (ipx_pcbconnect(ipxp, (struct sockaddr *)sipx, &proc0)) {
225			ipxp->ipxp_laddr = laddr;
226			spx_istat.noconn++;
227			goto drop;
228		}
229		spx_template(cb);
230		dropsocket = 0;		/* committed to socket */
231		cb->s_did = si->si_sid;
232		cb->s_rack = si->si_ack;
233		cb->s_ralo = si->si_alo;
234#define THREEWAYSHAKE
235#ifdef THREEWAYSHAKE
236		cb->s_state = TCPS_SYN_RECEIVED;
237		cb->s_force = 1 + SPXT_KEEP;
238		spxstat.spxs_accepts++;
239		cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
240		}
241		break;
242	/*
243	 * This state means that we have heard a response
244	 * to our acceptance of their connection
245	 * It is probably logically unnecessary in this
246	 * implementation.
247	 */
248	 case TCPS_SYN_RECEIVED: {
249		if (si->si_did != cb->s_sid) {
250			spx_istat.wrncon++;
251			goto drop;
252		}
253#endif
254		ipxp->ipxp_fport =  si->si_sport;
255		cb->s_timer[SPXT_REXMT] = 0;
256		cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
257		soisconnected(so);
258		cb->s_state = TCPS_ESTABLISHED;
259		spxstat.spxs_accepts++;
260		}
261		break;
262
263	/*
264	 * This state means that we have gotten a response
265	 * to our attempt to establish a connection.
266	 * We fill in the data from the other side,
267	 * telling us which port to respond to, instead of the well-
268	 * known one we might have sent to in the first place.
269	 * We also require that this is a response to our
270	 * connection id.
271	 */
272	case TCPS_SYN_SENT:
273		if (si->si_did != cb->s_sid) {
274			spx_istat.notme++;
275			goto drop;
276		}
277		spxstat.spxs_connects++;
278		cb->s_did = si->si_sid;
279		cb->s_rack = si->si_ack;
280		cb->s_ralo = si->si_alo;
281		cb->s_dport = ipxp->ipxp_fport =  si->si_sport;
282		cb->s_timer[SPXT_REXMT] = 0;
283		cb->s_flags |= SF_ACKNOW;
284		soisconnected(so);
285		cb->s_state = TCPS_ESTABLISHED;
286		/* Use roundtrip time of connection request for initial rtt */
287		if (cb->s_rtt) {
288			cb->s_srtt = cb->s_rtt << 3;
289			cb->s_rttvar = cb->s_rtt << 1;
290			SPXT_RANGESET(cb->s_rxtcur,
291			    ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
292			    SPXTV_MIN, SPXTV_REXMTMAX);
293			    cb->s_rtt = 0;
294		}
295	}
296	if (so->so_options & SO_DEBUG || traceallspxs)
297		spx_trace(SA_INPUT, (u_char)ostate, cb, &spx_savesi, 0);
298
299	m->m_len -= sizeof(struct ipx);
300	m->m_pkthdr.len -= sizeof(struct ipx);
301	m->m_data += sizeof(struct ipx);
302
303	if (spx_reass(cb, si)) {
304		m_freem(m);
305	}
306	if (cb->s_force || (cb->s_flags & (SF_ACKNOW|SF_WIN|SF_RXT)))
307		spx_output(cb, (struct mbuf *)NULL);
308	cb->s_flags &= ~(SF_WIN|SF_RXT);
309	return;
310
311dropwithreset:
312	if (dropsocket)
313		soabort(so);
314	si->si_seq = ntohs(si->si_seq);
315	si->si_ack = ntohs(si->si_ack);
316	si->si_alo = ntohs(si->si_alo);
317	m_freem(dtom(si));
318	if (cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG || traceallspxs)
319		spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
320	return;
321
322drop:
323bad:
324	if (cb == 0 || cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG ||
325            traceallspxs)
326		spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
327	m_freem(m);
328}
329
330static int spxrexmtthresh = 3;
331
332/*
333 * This is structurally similar to the tcp reassembly routine
334 * but its function is somewhat different:  It merely queues
335 * packets up, and suppresses duplicates.
336 */
337static int
338spx_reass(cb, si)
339register struct spxpcb *cb;
340register struct spx *si;
341{
342	register struct spx_q *q;
343	register struct mbuf *m;
344	register struct socket *so = cb->s_ipxpcb->ipxp_socket;
345	char packetp = cb->s_flags & SF_HI;
346	int incr;
347	char wakeup = 0;
348
349	if (si == SI(0))
350		goto present;
351	/*
352	 * Update our news from them.
353	 */
354	if (si->si_cc & SPX_SA)
355		cb->s_flags |= (spx_use_delack ? SF_DELACK : SF_ACKNOW);
356	if (SSEQ_GT(si->si_alo, cb->s_ralo))
357		cb->s_flags |= SF_WIN;
358	if (SSEQ_LEQ(si->si_ack, cb->s_rack)) {
359		if ((si->si_cc & SPX_SP) && cb->s_rack != (cb->s_smax + 1)) {
360			spxstat.spxs_rcvdupack++;
361			/*
362			 * If this is a completely duplicate ack
363			 * and other conditions hold, we assume
364			 * a packet has been dropped and retransmit
365			 * it exactly as in tcp_input().
366			 */
367			if (si->si_ack != cb->s_rack ||
368			    si->si_alo != cb->s_ralo)
369				cb->s_dupacks = 0;
370			else if (++cb->s_dupacks == spxrexmtthresh) {
371				u_short onxt = cb->s_snxt;
372				int cwnd = cb->s_cwnd;
373
374				cb->s_snxt = si->si_ack;
375				cb->s_cwnd = CUNIT;
376				cb->s_force = 1 + SPXT_REXMT;
377				spx_output(cb, (struct mbuf *)NULL);
378				cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
379				cb->s_rtt = 0;
380				if (cwnd >= 4 * CUNIT)
381					cb->s_cwnd = cwnd / 2;
382				if (SSEQ_GT(onxt, cb->s_snxt))
383					cb->s_snxt = onxt;
384				return (1);
385			}
386		} else
387			cb->s_dupacks = 0;
388		goto update_window;
389	}
390	cb->s_dupacks = 0;
391	/*
392	 * If our correspondent acknowledges data we haven't sent
393	 * TCP would drop the packet after acking.  We'll be a little
394	 * more permissive
395	 */
396	if (SSEQ_GT(si->si_ack, (cb->s_smax + 1))) {
397		spxstat.spxs_rcvacktoomuch++;
398		si->si_ack = cb->s_smax + 1;
399	}
400	spxstat.spxs_rcvackpack++;
401	/*
402	 * If transmit timer is running and timed sequence
403	 * number was acked, update smoothed round trip time.
404	 * See discussion of algorithm in tcp_input.c
405	 */
406	if (cb->s_rtt && SSEQ_GT(si->si_ack, cb->s_rtseq)) {
407		spxstat.spxs_rttupdated++;
408		if (cb->s_srtt != 0) {
409			register short delta;
410			delta = cb->s_rtt - (cb->s_srtt >> 3);
411			if ((cb->s_srtt += delta) <= 0)
412				cb->s_srtt = 1;
413			if (delta < 0)
414				delta = -delta;
415			delta -= (cb->s_rttvar >> 2);
416			if ((cb->s_rttvar += delta) <= 0)
417				cb->s_rttvar = 1;
418		} else {
419			/*
420			 * No rtt measurement yet
421			 */
422			cb->s_srtt = cb->s_rtt << 3;
423			cb->s_rttvar = cb->s_rtt << 1;
424		}
425		cb->s_rtt = 0;
426		cb->s_rxtshift = 0;
427		SPXT_RANGESET(cb->s_rxtcur,
428			((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
429			SPXTV_MIN, SPXTV_REXMTMAX);
430	}
431	/*
432	 * If all outstanding data is acked, stop retransmit
433	 * timer and remember to restart (more output or persist).
434	 * If there is more data to be acked, restart retransmit
435	 * timer, using current (possibly backed-off) value;
436	 */
437	if (si->si_ack == cb->s_smax + 1) {
438		cb->s_timer[SPXT_REXMT] = 0;
439		cb->s_flags |= SF_RXT;
440	} else if (cb->s_timer[SPXT_PERSIST] == 0)
441		cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
442	/*
443	 * When new data is acked, open the congestion window.
444	 * If the window gives us less than ssthresh packets
445	 * in flight, open exponentially (maxseg at a time).
446	 * Otherwise open linearly (maxseg^2 / cwnd at a time).
447	 */
448	incr = CUNIT;
449	if (cb->s_cwnd > cb->s_ssthresh)
450		incr = max(incr * incr / cb->s_cwnd, 1);
451	cb->s_cwnd = min(cb->s_cwnd + incr, cb->s_cwmx);
452	/*
453	 * Trim Acked data from output queue.
454	 */
455	while ((m = so->so_snd.sb_mb) != NULL) {
456		if (SSEQ_LT((mtod(m, struct spx *))->si_seq, si->si_ack))
457			sbdroprecord(&so->so_snd);
458		else
459			break;
460	}
461	sowwakeup(so);
462	cb->s_rack = si->si_ack;
463update_window:
464	if (SSEQ_LT(cb->s_snxt, cb->s_rack))
465		cb->s_snxt = cb->s_rack;
466	if (SSEQ_LT(cb->s_swl1, si->si_seq) || (cb->s_swl1 == si->si_seq &&
467	    (SSEQ_LT(cb->s_swl2, si->si_ack)) ||
468	     (cb->s_swl2 == si->si_ack && SSEQ_LT(cb->s_ralo, si->si_alo)))) {
469		/* keep track of pure window updates */
470		if ((si->si_cc & SPX_SP) && cb->s_swl2 == si->si_ack
471		    && SSEQ_LT(cb->s_ralo, si->si_alo)) {
472			spxstat.spxs_rcvwinupd++;
473			spxstat.spxs_rcvdupack--;
474		}
475		cb->s_ralo = si->si_alo;
476		cb->s_swl1 = si->si_seq;
477		cb->s_swl2 = si->si_ack;
478		cb->s_swnd = (1 + si->si_alo - si->si_ack);
479		if (cb->s_swnd > cb->s_smxw)
480			cb->s_smxw = cb->s_swnd;
481		cb->s_flags |= SF_WIN;
482	}
483	/*
484	 * If this packet number is higher than that which
485	 * we have allocated refuse it, unless urgent
486	 */
487	if (SSEQ_GT(si->si_seq, cb->s_alo)) {
488		if (si->si_cc & SPX_SP) {
489			spxstat.spxs_rcvwinprobe++;
490			return (1);
491		} else
492			spxstat.spxs_rcvpackafterwin++;
493		if (si->si_cc & SPX_OB) {
494			if (SSEQ_GT(si->si_seq, cb->s_alo + 60)) {
495				m_freem(dtom(si));
496				return (0);
497			} /* else queue this packet; */
498		} else {
499			/*register struct socket *so = cb->s_ipxpcb->ipxp_socket;
500			if (so->so_state && SS_NOFDREF) {
501				spx_close(cb);
502			} else
503				       would crash system*/
504			spx_istat.notyet++;
505			m_freem(dtom(si));
506			return (0);
507		}
508	}
509	/*
510	 * If this is a system packet, we don't need to
511	 * queue it up, and won't update acknowledge #
512	 */
513	if (si->si_cc & SPX_SP) {
514		return (1);
515	}
516	/*
517	 * We have already seen this packet, so drop.
518	 */
519	if (SSEQ_LT(si->si_seq, cb->s_ack)) {
520		spx_istat.bdreas++;
521		spxstat.spxs_rcvduppack++;
522		if (si->si_seq == cb->s_ack - 1)
523			spx_istat.lstdup++;
524		return (1);
525	}
526	/*
527	 * Loop through all packets queued up to insert in
528	 * appropriate sequence.
529	 */
530	for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
531		if (si->si_seq == SI(q)->si_seq) {
532			spxstat.spxs_rcvduppack++;
533			return (1);
534		}
535		if (SSEQ_LT(si->si_seq, SI(q)->si_seq)) {
536			spxstat.spxs_rcvoopack++;
537			break;
538		}
539	}
540	insque(si, q->si_prev);
541	/*
542	 * If this packet is urgent, inform process
543	 */
544	if (si->si_cc & SPX_OB) {
545		cb->s_iobc = ((char *)si)[1 + sizeof(*si)];
546		sohasoutofband(so);
547		cb->s_oobflags |= SF_IOOB;
548	}
549present:
550#define SPINC sizeof(struct spxhdr)
551	/*
552	 * Loop through all packets queued up to update acknowledge
553	 * number, and present all acknowledged data to user;
554	 * If in packet interface mode, show packet headers.
555	 */
556	for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
557		  if (SI(q)->si_seq == cb->s_ack) {
558			cb->s_ack++;
559			m = dtom(q);
560			if (SI(q)->si_cc & SPX_OB) {
561				cb->s_oobflags &= ~SF_IOOB;
562				if (so->so_rcv.sb_cc)
563					so->so_oobmark = so->so_rcv.sb_cc;
564				else
565					so->so_state |= SS_RCVATMARK;
566			}
567			q = q->si_prev;
568			remque(q->si_next);
569			wakeup = 1;
570			spxstat.spxs_rcvpack++;
571#ifdef SF_NEWCALL
572			if (cb->s_flags2 & SF_NEWCALL) {
573				struct spxhdr *sp = mtod(m, struct spxhdr *);
574				u_char dt = sp->spx_dt;
575				spx_newchecks[4]++;
576				if (dt != cb->s_rhdr.spx_dt) {
577					struct mbuf *mm =
578					   m_getclr(M_DONTWAIT, MT_CONTROL);
579					spx_newchecks[0]++;
580					if (mm != NULL) {
581						u_short *s =
582							mtod(mm, u_short *);
583						cb->s_rhdr.spx_dt = dt;
584						mm->m_len = 5; /*XXX*/
585						s[0] = 5;
586						s[1] = 1;
587						*(u_char *)(&s[2]) = dt;
588						sbappend(&so->so_rcv, mm);
589					}
590				}
591				if (sp->spx_cc & SPX_OB) {
592					MCHTYPE(m, MT_OOBDATA);
593					spx_newchecks[1]++;
594					so->so_oobmark = 0;
595					so->so_state &= ~SS_RCVATMARK;
596				}
597				if (packetp == 0) {
598					m->m_data += SPINC;
599					m->m_len -= SPINC;
600					m->m_pkthdr.len -= SPINC;
601				}
602				if ((sp->spx_cc & SPX_EM) || packetp) {
603					sbappendrecord(&so->so_rcv, m);
604					spx_newchecks[9]++;
605				} else
606					sbappend(&so->so_rcv, m);
607			} else
608#endif
609			if (packetp) {
610				sbappendrecord(&so->so_rcv, m);
611			} else {
612				cb->s_rhdr = *mtod(m, struct spxhdr *);
613				m->m_data += SPINC;
614				m->m_len -= SPINC;
615				m->m_pkthdr.len -= SPINC;
616				sbappend(&so->so_rcv, m);
617			}
618		  } else
619			break;
620	}
621	if (wakeup)
622		sorwakeup(so);
623	return (0);
624}
625
626void
627spx_ctlinput(cmd, arg_as_sa, dummy)
628	int cmd;
629	struct sockaddr *arg_as_sa;	/* XXX should be swapped with dummy */
630	void *dummy;
631{
632	caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
633	struct ipx_addr *na;
634	struct sockaddr_ipx *sipx;
635
636	if (cmd < 0 || cmd > PRC_NCMDS)
637		return;
638
639	switch (cmd) {
640
641	case PRC_ROUTEDEAD:
642		return;
643
644	case PRC_IFDOWN:
645	case PRC_HOSTDEAD:
646	case PRC_HOSTUNREACH:
647		sipx = (struct sockaddr_ipx *)arg;
648		if (sipx->sipx_family != AF_IPX)
649			return;
650		na = &sipx->sipx_addr;
651		break;
652
653	default:
654		break;
655	}
656}
657
658#ifdef notdef
659int
660spx_fixmtu(ipxp)
661register struct ipxpcb *ipxp;
662{
663	register struct spxpcb *cb = (struct spxpcb *)(ipxp->ipxp_pcb);
664	register struct mbuf *m;
665	register struct spx *si;
666	struct ipx_errp *ep;
667	struct sockbuf *sb;
668	int badseq, len;
669	struct mbuf *firstbad, *m0;
670
671	if (cb != NULL) {
672		/*
673		 * The notification that we have sent
674		 * too much is bad news -- we will
675		 * have to go through queued up so far
676		 * splitting ones which are too big and
677		 * reassigning sequence numbers and checksums.
678		 * we should then retransmit all packets from
679		 * one above the offending packet to the last one
680		 * we had sent (or our allocation)
681		 * then the offending one so that the any queued
682		 * data at our destination will be discarded.
683		 */
684		 ep = (struct ipx_errp *)ipxp->ipxp_notify_param;
685		 sb = &ipxp->ipxp_socket->so_snd;
686		 cb->s_mtu = ep->ipx_err_param;
687		 badseq = SI(&ep->ipx_err_ipx)->si_seq;
688		 for (m = sb->sb_mb; m != NULL; m = m->m_act) {
689			si = mtod(m, struct spx *);
690			if (si->si_seq == badseq)
691				break;
692		 }
693		 if (m == NULL)
694			return;
695		 firstbad = m;
696		 /*for (;;) {*/
697			/* calculate length */
698			for (m0 = m, len = 0; m != NULL; m = m->m_next)
699				len += m->m_len;
700			if (len > cb->s_mtu) {
701			}
702		/* FINISH THIS
703		} */
704	}
705}
706#endif
707
708static int
709spx_output(cb, m0)
710	register struct spxpcb *cb;
711	struct mbuf *m0;
712{
713	struct socket *so = cb->s_ipxpcb->ipxp_socket;
714	register struct mbuf *m;
715	register struct spx *si = (struct spx *)NULL;
716	register struct sockbuf *sb = &so->so_snd;
717	int len = 0, win, rcv_win;
718	short span, off, recordp = 0;
719	u_short alo;
720	int error = 0, sendalot;
721#ifdef notdef
722	int idle;
723#endif
724	struct mbuf *mprev;
725
726	if (m0 != NULL) {
727		int mtu = cb->s_mtu;
728		int datalen;
729		/*
730		 * Make sure that packet isn't too big.
731		 */
732		for (m = m0; m != NULL; m = m->m_next) {
733			mprev = m;
734			len += m->m_len;
735			if (m->m_flags & M_EOR)
736				recordp = 1;
737		}
738		datalen = (cb->s_flags & SF_HO) ?
739				len - sizeof(struct spxhdr) : len;
740		if (datalen > mtu) {
741			if (cb->s_flags & SF_PI) {
742				m_freem(m0);
743				return (EMSGSIZE);
744			} else {
745				int oldEM = cb->s_cc & SPX_EM;
746
747				cb->s_cc &= ~SPX_EM;
748				while (len > mtu) {
749					/*
750					 * Here we are only being called
751					 * from usrreq(), so it is OK to
752					 * block.
753					 */
754					m = m_copym(m0, 0, mtu, M_WAIT);
755					if (cb->s_flags & SF_NEWCALL) {
756					    struct mbuf *mm = m;
757					    spx_newchecks[7]++;
758					    while (mm != NULL) {
759						mm->m_flags &= ~M_EOR;
760						mm = mm->m_next;
761					    }
762					}
763					error = spx_output(cb, m);
764					if (error) {
765						cb->s_cc |= oldEM;
766						m_freem(m0);
767						return (error);
768					}
769					m_adj(m0, mtu);
770					len -= mtu;
771				}
772				cb->s_cc |= oldEM;
773			}
774		}
775		/*
776		 * Force length even, by adding a "garbage byte" if
777		 * necessary.
778		 */
779		if (len & 1) {
780			m = mprev;
781			if (M_TRAILINGSPACE(m) >= 1)
782				m->m_len++;
783			else {
784				struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
785
786				if (m1 == NULL) {
787					m_freem(m0);
788					return (ENOBUFS);
789				}
790				m1->m_len = 1;
791				*(mtod(m1, u_char *)) = 0;
792				m->m_next = m1;
793			}
794		}
795		m = m_gethdr(M_DONTWAIT, MT_HEADER);
796		if (m == NULL) {
797			m_freem(m0);
798			return (ENOBUFS);
799		}
800		/*
801		 * Fill in mbuf with extended SP header
802		 * and addresses and length put into network format.
803		 */
804		MH_ALIGN(m, sizeof(struct spx));
805		m->m_len = sizeof(struct spx);
806		m->m_next = m0;
807		si = mtod(m, struct spx *);
808		si->si_i = *cb->s_ipx;
809		si->si_s = cb->s_shdr;
810		if ((cb->s_flags & SF_PI) && (cb->s_flags & SF_HO)) {
811			register struct spxhdr *sh;
812			if (m0->m_len < sizeof(*sh)) {
813				if((m0 = m_pullup(m0, sizeof(*sh))) == NULL) {
814					m_free(m);
815					m_freem(m0);
816					return (EINVAL);
817				}
818				m->m_next = m0;
819			}
820			sh = mtod(m0, struct spxhdr *);
821			si->si_dt = sh->spx_dt;
822			si->si_cc |= sh->spx_cc & SPX_EM;
823			m0->m_len -= sizeof(*sh);
824			m0->m_data += sizeof(*sh);
825			len -= sizeof(*sh);
826		}
827		len += sizeof(*si);
828		if ((cb->s_flags2 & SF_NEWCALL) && recordp) {
829			si->si_cc |= SPX_EM;
830			spx_newchecks[8]++;
831		}
832		if (cb->s_oobflags & SF_SOOB) {
833			/*
834			 * Per jqj@cornell:
835			 * make sure OB packets convey exactly 1 byte.
836			 * If the packet is 1 byte or larger, we
837			 * have already guaranted there to be at least
838			 * one garbage byte for the checksum, and
839			 * extra bytes shouldn't hurt!
840			 */
841			if (len > sizeof(*si)) {
842				si->si_cc |= SPX_OB;
843				len = (1 + sizeof(*si));
844			}
845		}
846		si->si_len = htons((u_short)len);
847		m->m_pkthdr.len = ((len - 1) | 1) + 1;
848		/*
849		 * queue stuff up for output
850		 */
851		sbappendrecord(sb, m);
852		cb->s_seq++;
853	}
854#ifdef notdef
855	idle = (cb->s_smax == (cb->s_rack - 1));
856#endif
857again:
858	sendalot = 0;
859	off = cb->s_snxt - cb->s_rack;
860	win = min(cb->s_swnd, (cb->s_cwnd / CUNIT));
861
862	/*
863	 * If in persist timeout with window of 0, send a probe.
864	 * Otherwise, if window is small but nonzero
865	 * and timer expired, send what we can and go into
866	 * transmit state.
867	 */
868	if (cb->s_force == 1 + SPXT_PERSIST) {
869		if (win != 0) {
870			cb->s_timer[SPXT_PERSIST] = 0;
871			cb->s_rxtshift = 0;
872		}
873	}
874	span = cb->s_seq - cb->s_rack;
875	len = min(span, win) - off;
876
877	if (len < 0) {
878		/*
879		 * Window shrank after we went into it.
880		 * If window shrank to 0, cancel pending
881		 * restransmission and pull s_snxt back
882		 * to (closed) window.  We will enter persist
883		 * state below.  If the widndow didn't close completely,
884		 * just wait for an ACK.
885		 */
886		len = 0;
887		if (win == 0) {
888			cb->s_timer[SPXT_REXMT] = 0;
889			cb->s_snxt = cb->s_rack;
890		}
891	}
892	if (len > 1)
893		sendalot = 1;
894	rcv_win = sbspace(&so->so_rcv);
895
896	/*
897	 * Send if we owe peer an ACK.
898	 */
899	if (cb->s_oobflags & SF_SOOB) {
900		/*
901		 * must transmit this out of band packet
902		 */
903		cb->s_oobflags &= ~ SF_SOOB;
904		sendalot = 1;
905		spxstat.spxs_sndurg++;
906		goto found;
907	}
908	if (cb->s_flags & SF_ACKNOW)
909		goto send;
910	if (cb->s_state < TCPS_ESTABLISHED)
911		goto send;
912	/*
913	 * Silly window can't happen in spx.
914	 * Code from tcp deleted.
915	 */
916	if (len)
917		goto send;
918	/*
919	 * Compare available window to amount of window
920	 * known to peer (as advertised window less
921	 * next expected input.)  If the difference is at least two
922	 * packets or at least 35% of the mximum possible window,
923	 * then want to send a window update to peer.
924	 */
925	if (rcv_win > 0) {
926		u_short delta =  1 + cb->s_alo - cb->s_ack;
927		int adv = rcv_win - (delta * cb->s_mtu);
928
929		if ((so->so_rcv.sb_cc == 0 && adv >= (2 * cb->s_mtu)) ||
930		    (100 * adv / so->so_rcv.sb_hiwat >= 35)) {
931			spxstat.spxs_sndwinup++;
932			cb->s_flags |= SF_ACKNOW;
933			goto send;
934		}
935
936	}
937	/*
938	 * Many comments from tcp_output.c are appropriate here
939	 * including . . .
940	 * If send window is too small, there is data to transmit, and no
941	 * retransmit or persist is pending, then go to persist state.
942	 * If nothing happens soon, send when timer expires:
943	 * if window is nonzero, transmit what we can,
944	 * otherwise send a probe.
945	 */
946	if (so->so_snd.sb_cc && cb->s_timer[SPXT_REXMT] == 0 &&
947		cb->s_timer[SPXT_PERSIST] == 0) {
948			cb->s_rxtshift = 0;
949			spx_setpersist(cb);
950	}
951	/*
952	 * No reason to send a packet, just return.
953	 */
954	cb->s_outx = 1;
955	return (0);
956
957send:
958	/*
959	 * Find requested packet.
960	 */
961	si = 0;
962	if (len > 0) {
963		cb->s_want = cb->s_snxt;
964		for (m = sb->sb_mb; m != NULL; m = m->m_act) {
965			si = mtod(m, struct spx *);
966			if (SSEQ_LEQ(cb->s_snxt, si->si_seq))
967				break;
968		}
969	found:
970		if (si != NULL) {
971			if (si->si_seq == cb->s_snxt)
972					cb->s_snxt++;
973				else
974					spxstat.spxs_sndvoid++, si = 0;
975		}
976	}
977	/*
978	 * update window
979	 */
980	if (rcv_win < 0)
981		rcv_win = 0;
982	alo = cb->s_ack - 1 + (rcv_win / ((short)cb->s_mtu));
983	if (SSEQ_LT(alo, cb->s_alo))
984		alo = cb->s_alo;
985
986	if (si != NULL) {
987		/*
988		 * must make a copy of this packet for
989		 * ipx_output to monkey with
990		 */
991		m = m_copy(dtom(si), 0, (int)M_COPYALL);
992		if (m == NULL) {
993			return (ENOBUFS);
994		}
995		si = mtod(m, struct spx *);
996		if (SSEQ_LT(si->si_seq, cb->s_smax))
997			spxstat.spxs_sndrexmitpack++;
998		else
999			spxstat.spxs_sndpack++;
1000	} else if (cb->s_force || cb->s_flags & SF_ACKNOW) {
1001		/*
1002		 * Must send an acknowledgement or a probe
1003		 */
1004		if (cb->s_force)
1005			spxstat.spxs_sndprobe++;
1006		if (cb->s_flags & SF_ACKNOW)
1007			spxstat.spxs_sndacks++;
1008		m = m_gethdr(M_DONTWAIT, MT_HEADER);
1009		if (m == NULL)
1010			return (ENOBUFS);
1011		/*
1012		 * Fill in mbuf with extended SP header
1013		 * and addresses and length put into network format.
1014		 */
1015		MH_ALIGN(m, sizeof(struct spx));
1016		m->m_len = sizeof(*si);
1017		m->m_pkthdr.len = sizeof(*si);
1018		si = mtod(m, struct spx *);
1019		si->si_i = *cb->s_ipx;
1020		si->si_s = cb->s_shdr;
1021		si->si_seq = cb->s_smax + 1;
1022		si->si_len = htons(sizeof(*si));
1023		si->si_cc |= SPX_SP;
1024	} else {
1025		cb->s_outx = 3;
1026		if (so->so_options & SO_DEBUG || traceallspxs)
1027			spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
1028		return (0);
1029	}
1030	/*
1031	 * Stuff checksum and output datagram.
1032	 */
1033	if ((si->si_cc & SPX_SP) == 0) {
1034		if (cb->s_force != (1 + SPXT_PERSIST) ||
1035		    cb->s_timer[SPXT_PERSIST] == 0) {
1036			/*
1037			 * If this is a new packet and we are not currently
1038			 * timing anything, time this one.
1039			 */
1040			if (SSEQ_LT(cb->s_smax, si->si_seq)) {
1041				cb->s_smax = si->si_seq;
1042				if (cb->s_rtt == 0) {
1043					spxstat.spxs_segstimed++;
1044					cb->s_rtseq = si->si_seq;
1045					cb->s_rtt = 1;
1046				}
1047			}
1048			/*
1049			 * Set rexmt timer if not currently set,
1050			 * Initial value for retransmit timer is smoothed
1051			 * round-trip time + 2 * round-trip time variance.
1052			 * Initialize shift counter which is used for backoff
1053			 * of retransmit time.
1054			 */
1055			if (cb->s_timer[SPXT_REXMT] == 0 &&
1056			    cb->s_snxt != cb->s_rack) {
1057				cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1058				if (cb->s_timer[SPXT_PERSIST]) {
1059					cb->s_timer[SPXT_PERSIST] = 0;
1060					cb->s_rxtshift = 0;
1061				}
1062			}
1063		} else if (SSEQ_LT(cb->s_smax, si->si_seq)) {
1064			cb->s_smax = si->si_seq;
1065		}
1066	} else if (cb->s_state < TCPS_ESTABLISHED) {
1067		if (cb->s_rtt == 0)
1068			cb->s_rtt = 1; /* Time initial handshake */
1069		if (cb->s_timer[SPXT_REXMT] == 0)
1070			cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1071	}
1072	{
1073		/*
1074		 * Do not request acks when we ack their data packets or
1075		 * when we do a gratuitous window update.
1076		 */
1077		if (((si->si_cc & SPX_SP) == 0) || cb->s_force)
1078				si->si_cc |= SPX_SA;
1079		si->si_seq = htons(si->si_seq);
1080		si->si_alo = htons(alo);
1081		si->si_ack = htons(cb->s_ack);
1082
1083		if (ipxcksum) {
1084			si->si_sum = 0;
1085			len = ntohs(si->si_len);
1086			if (len & 1)
1087				len++;
1088			si->si_sum = ipx_cksum(m, len);
1089		} else
1090			si->si_sum = 0xffff;
1091
1092		cb->s_outx = 4;
1093		if (so->so_options & SO_DEBUG || traceallspxs)
1094			spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
1095
1096		if (so->so_options & SO_DONTROUTE)
1097			error = ipx_outputfl(m, (struct route *)NULL, IPX_ROUTETOIF);
1098		else
1099			error = ipx_outputfl(m, &cb->s_ipxpcb->ipxp_route, 0);
1100	}
1101	if (error) {
1102		return (error);
1103	}
1104	spxstat.spxs_sndtotal++;
1105	/*
1106	 * Data sent (as far as we can tell).
1107	 * If this advertises a larger window than any other segment,
1108	 * then remember the size of the advertized window.
1109	 * Any pending ACK has now been sent.
1110	 */
1111	cb->s_force = 0;
1112	cb->s_flags &= ~(SF_ACKNOW|SF_DELACK);
1113	if (SSEQ_GT(alo, cb->s_alo))
1114		cb->s_alo = alo;
1115	if (sendalot)
1116		goto again;
1117	cb->s_outx = 5;
1118	return (0);
1119}
1120
1121static int spx_do_persist_panics = 0;
1122
1123static void
1124spx_setpersist(cb)
1125	register struct spxpcb *cb;
1126{
1127	register int t = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
1128
1129	if (cb->s_timer[SPXT_REXMT] && spx_do_persist_panics)
1130		panic("spx_output REXMT");
1131	/*
1132	 * Start/restart persistance timer.
1133	 */
1134	SPXT_RANGESET(cb->s_timer[SPXT_PERSIST],
1135	    t*spx_backoff[cb->s_rxtshift],
1136	    SPXTV_PERSMIN, SPXTV_PERSMAX);
1137	if (cb->s_rxtshift < SPX_MAXRXTSHIFT)
1138		cb->s_rxtshift++;
1139}
1140
1141int
1142spx_ctloutput(so, sopt)
1143	struct socket *so;
1144	struct sockopt *sopt;
1145{
1146	struct ipxpcb *ipxp = sotoipxpcb(so);
1147	register struct spxpcb *cb;
1148	int mask, error;
1149	short soptval;
1150	u_short usoptval;
1151	int optval;
1152
1153	error = 0;
1154
1155	if (sopt->sopt_level != IPXPROTO_SPX) {
1156		/* This will have to be changed when we do more general
1157		   stacking of protocols */
1158		return (ipx_ctloutput(so, sopt));
1159	}
1160	if (ipxp == NULL)
1161		return (EINVAL);
1162	else
1163		cb = ipxtospxpcb(ipxp);
1164
1165	switch (sopt->sopt_dir) {
1166	case SOPT_GET:
1167		switch (sopt->sopt_name) {
1168		case SO_HEADERS_ON_INPUT:
1169			mask = SF_HI;
1170			goto get_flags;
1171
1172		case SO_HEADERS_ON_OUTPUT:
1173			mask = SF_HO;
1174		get_flags:
1175			soptval = cb->s_flags & mask;
1176			error = sooptcopyout(sopt, &soptval, sizeof soptval);
1177			break;
1178
1179		case SO_MTU:
1180			usoptval = cb->s_mtu;
1181			error = sooptcopyout(sopt, &usoptval, sizeof usoptval);
1182			break;
1183
1184		case SO_LAST_HEADER:
1185			error = sooptcopyout(sopt, &cb->s_rhdr,
1186					     sizeof cb->s_rhdr);
1187			break;
1188
1189		case SO_DEFAULT_HEADERS:
1190			error = sooptcopyout(sopt, &cb->s_shdr,
1191					     sizeof cb->s_shdr);
1192			break;
1193
1194		default:
1195			error = ENOPROTOOPT;
1196		}
1197		break;
1198
1199	case SOPT_SET:
1200		switch (sopt->sopt_name) {
1201			/* XXX why are these shorts on get and ints on set?
1202			   that doesn't make any sense... */
1203		case SO_HEADERS_ON_INPUT:
1204			mask = SF_HI;
1205			goto set_head;
1206
1207		case SO_HEADERS_ON_OUTPUT:
1208			mask = SF_HO;
1209		set_head:
1210			error = sooptcopyin(sopt, &optval, sizeof optval,
1211					    sizeof optval);
1212			if (error)
1213				break;
1214
1215			if (cb->s_flags & SF_PI) {
1216				if (optval)
1217					cb->s_flags |= mask;
1218				else
1219					cb->s_flags &= ~mask;
1220			} else error = EINVAL;
1221			break;
1222
1223		case SO_MTU:
1224			error = sooptcopyin(sopt, &usoptval, sizeof usoptval,
1225					    sizeof usoptval);
1226			if (error)
1227				break;
1228			cb->s_mtu = usoptval;
1229			break;
1230
1231#ifdef SF_NEWCALL
1232		case SO_NEWCALL:
1233			error = sooptcopyin(sopt, &optval, sizeof optval,
1234					    sizeof optval);
1235			if (error)
1236				break;
1237			if (optval) {
1238				cb->s_flags2 |= SF_NEWCALL;
1239				spx_newchecks[5]++;
1240			} else {
1241				cb->s_flags2 &= ~SF_NEWCALL;
1242				spx_newchecks[6]++;
1243			}
1244			break;
1245#endif
1246
1247		case SO_DEFAULT_HEADERS:
1248			{
1249				struct spxhdr sp;
1250
1251				error = sooptcopyin(sopt, &sp, sizeof sp,
1252						    sizeof sp);
1253				if (error)
1254					break;
1255				cb->s_dt = sp.spx_dt;
1256				cb->s_cc = sp.spx_cc & SPX_EM;
1257			}
1258			break;
1259
1260		default:
1261			error = ENOPROTOOPT;
1262		}
1263		break;
1264	}
1265	return (error);
1266}
1267
1268static int
1269spx_usr_abort(so)
1270	struct socket *so;
1271{
1272	int s;
1273	struct ipxpcb *ipxp;
1274	struct spxpcb *cb;
1275
1276	ipxp = sotoipxpcb(so);
1277	cb = ipxtospxpcb(ipxp);
1278
1279	s = splnet();
1280	spx_drop(cb, ECONNABORTED);
1281	splx(s);
1282	return (0);
1283}
1284
1285/*
1286 * Accept a connection.  Essentially all the work is
1287 * done at higher levels; just return the address
1288 * of the peer, storing through addr.
1289 */
1290static int
1291spx_accept(so, nam)
1292	struct socket *so;
1293	struct sockaddr **nam;
1294{
1295	struct ipxpcb *ipxp;
1296	struct sockaddr_ipx *sipx, ssipx;
1297
1298	ipxp = sotoipxpcb(so);
1299	sipx = &ssipx;
1300	bzero(sipx, sizeof *sipx);
1301	sipx->sipx_len = sizeof *sipx;
1302	sipx->sipx_family = AF_IPX;
1303	sipx->sipx_addr = ipxp->ipxp_faddr;
1304	*nam = dup_sockaddr((struct sockaddr *)sipx, 0);
1305	return (0);
1306}
1307
1308static int
1309spx_attach(so, proto, p)
1310	struct socket *so;
1311	int proto;
1312	struct proc *p;
1313{
1314	int error;
1315	int s;
1316	struct ipxpcb *ipxp;
1317	struct spxpcb *cb;
1318	struct mbuf *mm;
1319	struct sockbuf *sb;
1320
1321	ipxp = sotoipxpcb(so);
1322	cb = ipxtospxpcb(ipxp);
1323
1324	if (ipxp != NULL)
1325		return (EISCONN);
1326	s = splnet();
1327	error = ipx_pcballoc(so, &ipxpcb, p);
1328	if (error)
1329		goto spx_attach_end;
1330	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1331		error = soreserve(so, (u_long) 3072, (u_long) 3072);
1332		if (error)
1333			goto spx_attach_end;
1334	}
1335	ipxp = sotoipxpcb(so);
1336
1337	MALLOC(cb, struct spxpcb *, sizeof *cb, M_PCB, M_NOWAIT);
1338	bzero(cb, sizeof *cb);
1339	sb = &so->so_snd;
1340
1341	if (cb == NULL) {
1342		error = ENOBUFS;
1343		goto spx_attach_end;
1344	}
1345
1346	mm = m_getclr(M_DONTWAIT, MT_HEADER);
1347	if (mm == NULL) {
1348		FREE(cb, M_PCB);
1349		error = ENOBUFS;
1350		goto spx_attach_end;
1351	}
1352	cb->s_ipx = mtod(mm, struct ipx *);
1353	cb->s_state = TCPS_LISTEN;
1354	cb->s_smax = -1;
1355	cb->s_swl1 = -1;
1356	cb->s_q.si_next = cb->s_q.si_prev = &cb->s_q;
1357	cb->s_ipxpcb = ipxp;
1358	cb->s_mtu = 576 - sizeof(struct spx);
1359	cb->s_cwnd = sbspace(sb) * CUNIT / cb->s_mtu;
1360	cb->s_ssthresh = cb->s_cwnd;
1361	cb->s_cwmx = sbspace(sb) * CUNIT / (2 * sizeof(struct spx));
1362	/* Above is recomputed when connecting to account
1363	   for changed buffering or mtu's */
1364	cb->s_rtt = SPXTV_SRTTBASE;
1365	cb->s_rttvar = SPXTV_SRTTDFLT << 2;
1366	SPXT_RANGESET(cb->s_rxtcur,
1367	    ((SPXTV_SRTTBASE >> 2) + (SPXTV_SRTTDFLT << 2)) >> 1,
1368	    SPXTV_MIN, SPXTV_REXMTMAX);
1369	ipxp->ipxp_pcb = (caddr_t)cb;
1370spx_attach_end:
1371	splx(s);
1372	return (error);
1373}
1374
1375static int
1376spx_bind(so, nam, p)
1377	struct socket *so;
1378	struct sockaddr *nam;
1379	struct proc *p;
1380{
1381	struct ipxpcb *ipxp;
1382
1383	ipxp = sotoipxpcb(so);
1384
1385	return (ipx_pcbbind(ipxp, nam, p));
1386}
1387
1388/*
1389 * Initiate connection to peer.
1390 * Enter SYN_SENT state, and mark socket as connecting.
1391 * Start keep-alive timer, setup prototype header,
1392 * Send initial system packet requesting connection.
1393 */
1394static int
1395spx_connect(so, nam, p)
1396	struct socket *so;
1397	struct sockaddr *nam;
1398	struct proc *p;
1399{
1400	int error;
1401	int s;
1402	struct ipxpcb *ipxp;
1403	struct spxpcb *cb;
1404
1405	ipxp = sotoipxpcb(so);
1406	cb = ipxtospxpcb(ipxp);
1407
1408	s = splnet();
1409	if (ipxp->ipxp_lport == 0) {
1410		error = ipx_pcbbind(ipxp, (struct sockaddr *)NULL, p);
1411		if (error)
1412			goto spx_connect_end;
1413	}
1414	error = ipx_pcbconnect(ipxp, nam, p);
1415	if (error)
1416		goto spx_connect_end;
1417	soisconnecting(so);
1418	spxstat.spxs_connattempt++;
1419	cb->s_state = TCPS_SYN_SENT;
1420	cb->s_did = 0;
1421	spx_template(cb);
1422	cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
1423	cb->s_force = 1 + SPXTV_KEEP;
1424	/*
1425	 * Other party is required to respond to
1426	 * the port I send from, but he is not
1427	 * required to answer from where I am sending to,
1428	 * so allow wildcarding.
1429	 * original port I am sending to is still saved in
1430	 * cb->s_dport.
1431	 */
1432	ipxp->ipxp_fport = 0;
1433	error = spx_output(cb, (struct mbuf *)NULL);
1434spx_connect_end:
1435	splx(s);
1436	return (error);
1437}
1438
1439static int
1440spx_detach(so)
1441	struct socket *so;
1442{
1443	int s;
1444	struct ipxpcb *ipxp;
1445	struct spxpcb *cb;
1446
1447	ipxp = sotoipxpcb(so);
1448	cb = ipxtospxpcb(ipxp);
1449
1450	if (ipxp == NULL)
1451		return (ENOTCONN);
1452	s = splnet();
1453	if (cb->s_state > TCPS_LISTEN)
1454		spx_disconnect(cb);
1455	else
1456		spx_close(cb);
1457	splx(s);
1458	return (0);
1459}
1460
1461/*
1462 * We may decide later to implement connection closing
1463 * handshaking at the spx level optionally.
1464 * here is the hook to do it:
1465 */
1466static int
1467spx_usr_disconnect(so)
1468	struct socket *so;
1469{
1470	int s;
1471	struct ipxpcb *ipxp;
1472	struct spxpcb *cb;
1473
1474	ipxp = sotoipxpcb(so);
1475	cb = ipxtospxpcb(ipxp);
1476
1477	s = splnet();
1478	spx_disconnect(cb);
1479	splx(s);
1480	return (0);
1481}
1482
1483static int
1484spx_listen(so, p)
1485	struct socket *so;
1486	struct proc *p;
1487{
1488	int error;
1489	struct ipxpcb *ipxp;
1490	struct spxpcb *cb;
1491
1492	error = 0;
1493	ipxp = sotoipxpcb(so);
1494	cb = ipxtospxpcb(ipxp);
1495
1496	if (ipxp->ipxp_lport == 0)
1497		error = ipx_pcbbind(ipxp, (struct sockaddr *)NULL, p);
1498	if (error == 0)
1499		cb->s_state = TCPS_LISTEN;
1500	return (error);
1501}
1502
1503/*
1504 * After a receive, possibly send acknowledgment
1505 * updating allocation.
1506 */
1507static int
1508spx_rcvd(so, flags)
1509	struct socket *so;
1510	int flags;
1511{
1512	int s;
1513	struct ipxpcb *ipxp;
1514	struct spxpcb *cb;
1515
1516	ipxp = sotoipxpcb(so);
1517	cb = ipxtospxpcb(ipxp);
1518
1519	s = splnet();
1520	cb->s_flags |= SF_RVD;
1521	spx_output(cb, (struct mbuf *)NULL);
1522	cb->s_flags &= ~SF_RVD;
1523	splx(s);
1524	return (0);
1525}
1526
1527static int
1528spx_rcvoob(so, m, flags)
1529	struct socket *so;
1530	struct mbuf *m;
1531	int flags;
1532{
1533	struct ipxpcb *ipxp;
1534	struct spxpcb *cb;
1535
1536	ipxp = sotoipxpcb(so);
1537	cb = ipxtospxpcb(ipxp);
1538
1539	if ((cb->s_oobflags & SF_IOOB) || so->so_oobmark ||
1540	    (so->so_state & SS_RCVATMARK)) {
1541		m->m_len = 1;
1542		*mtod(m, caddr_t) = cb->s_iobc;
1543		return (0);
1544	}
1545	return (EINVAL);
1546}
1547
1548static int
1549spx_send(so, flags, m, addr, controlp, p)
1550	struct socket *so;
1551	int flags;
1552	struct mbuf *m;
1553	struct sockaddr *addr;
1554	struct mbuf *controlp;
1555	struct proc *p;
1556{
1557	int error;
1558	int s;
1559	struct ipxpcb *ipxp;
1560	struct spxpcb *cb;
1561
1562	error = 0;
1563	ipxp = sotoipxpcb(so);
1564	cb = ipxtospxpcb(ipxp);
1565
1566	s = splnet();
1567	if (flags & PRUS_OOB) {
1568		if (sbspace(&so->so_snd) < -512) {
1569			error = ENOBUFS;
1570			goto spx_send_end;
1571		}
1572		cb->s_oobflags |= SF_SOOB;
1573	}
1574	if (controlp != NULL) {
1575		u_short *p = mtod(controlp, u_short *);
1576		spx_newchecks[2]++;
1577		if ((p[0] == 5) && (p[1] == 1)) { /* XXXX, for testing */
1578			cb->s_shdr.spx_dt = *(u_char *)(&p[2]);
1579			spx_newchecks[3]++;
1580		}
1581		m_freem(controlp);
1582	}
1583	controlp = NULL;
1584	error = spx_output(cb, m);
1585	m = NULL;
1586spx_send_end:
1587	if (controlp != NULL)
1588		m_freem(controlp);
1589	if (m != NULL)
1590		m_freem(m);
1591	splx(s);
1592	return (error);
1593}
1594
1595static int
1596spx_shutdown(so)
1597	struct socket *so;
1598{
1599	int error;
1600	int s;
1601	struct ipxpcb *ipxp;
1602	struct spxpcb *cb;
1603
1604	error = 0;
1605	ipxp = sotoipxpcb(so);
1606	cb = ipxtospxpcb(ipxp);
1607
1608	s = splnet();
1609	socantsendmore(so);
1610	cb = spx_usrclosed(cb);
1611	if (cb != NULL)
1612		error = spx_output(cb, (struct mbuf *)NULL);
1613	splx(s);
1614	return (error);
1615}
1616
1617static int
1618spx_sp_attach(so, proto, p)
1619	struct socket *so;
1620	int proto;
1621	struct proc *p;
1622{
1623	int error;
1624	struct ipxpcb *ipxp;
1625
1626	error = spx_attach(so, proto, p);
1627	if (error == 0) {
1628		ipxp = sotoipxpcb(so);
1629		((struct spxpcb *)ipxp->ipxp_pcb)->s_flags |=
1630					(SF_HI | SF_HO | SF_PI);
1631	}
1632	return (error);
1633}
1634
1635/*
1636 * Create template to be used to send spx packets on a connection.
1637 * Called after host entry created, fills
1638 * in a skeletal spx header (choosing connection id),
1639 * minimizing the amount of work necessary when the connection is used.
1640 */
1641static void
1642spx_template(cb)
1643	register struct spxpcb *cb;
1644{
1645	register struct ipxpcb *ipxp = cb->s_ipxpcb;
1646	register struct ipx *ipx = cb->s_ipx;
1647	register struct sockbuf *sb = &(ipxp->ipxp_socket->so_snd);
1648
1649	ipx->ipx_pt = IPXPROTO_SPX;
1650	ipx->ipx_sna = ipxp->ipxp_laddr;
1651	ipx->ipx_dna = ipxp->ipxp_faddr;
1652	cb->s_sid = htons(spx_iss);
1653	spx_iss += SPX_ISSINCR/2;
1654	cb->s_alo = 1;
1655	cb->s_cwnd = (sbspace(sb) * CUNIT) / cb->s_mtu;
1656	cb->s_ssthresh = cb->s_cwnd; /* Try to expand fast to full complement
1657					of large packets */
1658	cb->s_cwmx = (sbspace(sb) * CUNIT) / (2 * sizeof(struct spx));
1659	cb->s_cwmx = max(cb->s_cwmx, cb->s_cwnd);
1660		/* But allow for lots of little packets as well */
1661}
1662
1663/*
1664 * Close a SPIP control block:
1665 *	discard spx control block itself
1666 *	discard ipx protocol control block
1667 *	wake up any sleepers
1668 */
1669static struct spxpcb *
1670spx_close(cb)
1671	register struct spxpcb *cb;
1672{
1673	register struct spx_q *s;
1674	struct ipxpcb *ipxp = cb->s_ipxpcb;
1675	struct socket *so = ipxp->ipxp_socket;
1676	register struct mbuf *m;
1677
1678	s = cb->s_q.si_next;
1679	while (s != &(cb->s_q)) {
1680		s = s->si_next;
1681		m = dtom(s->si_prev);
1682		remque(s->si_prev);
1683		m_freem(m);
1684	}
1685	m_free(dtom(cb->s_ipx));
1686	FREE(cb, M_PCB);
1687	ipxp->ipxp_pcb = 0;
1688	soisdisconnected(so);
1689	ipx_pcbdetach(ipxp);
1690	spxstat.spxs_closed++;
1691	return ((struct spxpcb *)NULL);
1692}
1693
1694/*
1695 *	Someday we may do level 3 handshaking
1696 *	to close a connection or send a xerox style error.
1697 *	For now, just close.
1698 */
1699static struct spxpcb *
1700spx_usrclosed(cb)
1701	register struct spxpcb *cb;
1702{
1703	return (spx_close(cb));
1704}
1705
1706static struct spxpcb *
1707spx_disconnect(cb)
1708	register struct spxpcb *cb;
1709{
1710	return (spx_close(cb));
1711}
1712
1713/*
1714 * Drop connection, reporting
1715 * the specified error.
1716 */
1717static struct spxpcb *
1718spx_drop(cb, errno)
1719	register struct spxpcb *cb;
1720	int errno;
1721{
1722	struct socket *so = cb->s_ipxpcb->ipxp_socket;
1723
1724	/*
1725	 * someday, in the xerox world
1726	 * we will generate error protocol packets
1727	 * announcing that the socket has gone away.
1728	 */
1729	if (TCPS_HAVERCVDSYN(cb->s_state)) {
1730		spxstat.spxs_drops++;
1731		cb->s_state = TCPS_CLOSED;
1732		/*tcp_output(cb);*/
1733	} else
1734		spxstat.spxs_conndrops++;
1735	so->so_error = errno;
1736	return (spx_close(cb));
1737}
1738
1739/*
1740 * Fast timeout routine for processing delayed acks
1741 */
1742void
1743spx_fasttimo()
1744{
1745	register struct ipxpcb *ipxp;
1746	register struct spxpcb *cb;
1747	int s = splnet();
1748
1749	ipxp = ipxpcb.ipxp_next;
1750	if (ipxp != NULL)
1751	for (; ipxp != &ipxpcb; ipxp = ipxp->ipxp_next)
1752		if ((cb = (struct spxpcb *)ipxp->ipxp_pcb) != NULL &&
1753		    (cb->s_flags & SF_DELACK)) {
1754			cb->s_flags &= ~SF_DELACK;
1755			cb->s_flags |= SF_ACKNOW;
1756			spxstat.spxs_delack++;
1757			spx_output(cb, (struct mbuf *)NULL);
1758		}
1759	splx(s);
1760}
1761
1762/*
1763 * spx protocol timeout routine called every 500 ms.
1764 * Updates the timers in all active pcb's and
1765 * causes finite state machine actions if timers expire.
1766 */
1767void
1768spx_slowtimo()
1769{
1770	register struct ipxpcb *ip, *ipnxt;
1771	register struct spxpcb *cb;
1772	int s = splnet();
1773	register int i;
1774
1775	/*
1776	 * Search through tcb's and update active timers.
1777	 */
1778	ip = ipxpcb.ipxp_next;
1779	if (ip == NULL) {
1780		splx(s);
1781		return;
1782	}
1783	while (ip != &ipxpcb) {
1784		cb = ipxtospxpcb(ip);
1785		ipnxt = ip->ipxp_next;
1786		if (cb == NULL)
1787			goto tpgone;
1788		for (i = 0; i < SPXT_NTIMERS; i++) {
1789			if (cb->s_timer[i] && --cb->s_timer[i] == 0) {
1790				spx_timers(cb, i);
1791				if (ipnxt->ipxp_prev != ip)
1792					goto tpgone;
1793			}
1794		}
1795		cb->s_idle++;
1796		if (cb->s_rtt)
1797			cb->s_rtt++;
1798tpgone:
1799		ip = ipnxt;
1800	}
1801	spx_iss += SPX_ISSINCR/PR_SLOWHZ;		/* increment iss */
1802	splx(s);
1803}
1804
1805/*
1806 * SPX timer processing.
1807 */
1808static struct spxpcb *
1809spx_timers(cb, timer)
1810	register struct spxpcb *cb;
1811	int timer;
1812{
1813	long rexmt;
1814	int win;
1815
1816	cb->s_force = 1 + timer;
1817	switch (timer) {
1818
1819	/*
1820	 * 2 MSL timeout in shutdown went off.  TCP deletes connection
1821	 * control block.
1822	 */
1823	case SPXT_2MSL:
1824		printf("spx: SPXT_2MSL went off for no reason\n");
1825		cb->s_timer[timer] = 0;
1826		break;
1827
1828	/*
1829	 * Retransmission timer went off.  Message has not
1830	 * been acked within retransmit interval.  Back off
1831	 * to a longer retransmit interval and retransmit one packet.
1832	 */
1833	case SPXT_REXMT:
1834		if (++cb->s_rxtshift > SPX_MAXRXTSHIFT) {
1835			cb->s_rxtshift = SPX_MAXRXTSHIFT;
1836			spxstat.spxs_timeoutdrop++;
1837			cb = spx_drop(cb, ETIMEDOUT);
1838			break;
1839		}
1840		spxstat.spxs_rexmttimeo++;
1841		rexmt = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
1842		rexmt *= spx_backoff[cb->s_rxtshift];
1843		SPXT_RANGESET(cb->s_rxtcur, rexmt, SPXTV_MIN, SPXTV_REXMTMAX);
1844		cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1845		/*
1846		 * If we have backed off fairly far, our srtt
1847		 * estimate is probably bogus.  Clobber it
1848		 * so we'll take the next rtt measurement as our srtt;
1849		 * move the current srtt into rttvar to keep the current
1850		 * retransmit times until then.
1851		 */
1852		if (cb->s_rxtshift > SPX_MAXRXTSHIFT / 4 ) {
1853			cb->s_rttvar += (cb->s_srtt >> 2);
1854			cb->s_srtt = 0;
1855		}
1856		cb->s_snxt = cb->s_rack;
1857		/*
1858		 * If timing a packet, stop the timer.
1859		 */
1860		cb->s_rtt = 0;
1861		/*
1862		 * See very long discussion in tcp_timer.c about congestion
1863		 * window and sstrhesh
1864		 */
1865		win = min(cb->s_swnd, (cb->s_cwnd/CUNIT)) / 2;
1866		if (win < 2)
1867			win = 2;
1868		cb->s_cwnd = CUNIT;
1869		cb->s_ssthresh = win * CUNIT;
1870		spx_output(cb, (struct mbuf *)NULL);
1871		break;
1872
1873	/*
1874	 * Persistance timer into zero window.
1875	 * Force a probe to be sent.
1876	 */
1877	case SPXT_PERSIST:
1878		spxstat.spxs_persisttimeo++;
1879		spx_setpersist(cb);
1880		spx_output(cb, (struct mbuf *)NULL);
1881		break;
1882
1883	/*
1884	 * Keep-alive timer went off; send something
1885	 * or drop connection if idle for too long.
1886	 */
1887	case SPXT_KEEP:
1888		spxstat.spxs_keeptimeo++;
1889		if (cb->s_state < TCPS_ESTABLISHED)
1890			goto dropit;
1891		if (cb->s_ipxpcb->ipxp_socket->so_options & SO_KEEPALIVE) {
1892		    	if (cb->s_idle >= SPXTV_MAXIDLE)
1893				goto dropit;
1894			spxstat.spxs_keepprobe++;
1895			spx_output(cb, (struct mbuf *)NULL);
1896		} else
1897			cb->s_idle = 0;
1898		cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
1899		break;
1900	dropit:
1901		spxstat.spxs_keepdrops++;
1902		cb = spx_drop(cb, ETIMEDOUT);
1903		break;
1904	}
1905	return (cb);
1906}
1907