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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
36 * $FreeBSD: src/sys/netipx/spx_usrreq.c,v 1.27.2.1 2001/02/22 09:44:18 bp Exp $
37 * $DragonFly: src/sys/netproto/ipx/spx_usrreq.c,v 1.5 2003/08/07 21:17:37 dillon Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
49 #include <net/route.h>
50 #include <netinet/tcp_fsm.h>
56 #include "spx_timer.h"
58 #include "spx_debug.h"
61 * SPX protocol implementation.
63 static u_short spx_iss;
64 static u_short spx_newchecks[50];
65 static int spx_hardnosed;
66 static int spx_use_delack = 0;
67 static int traceallspxs = 0;
68 static struct spx spx_savesi;
69 static struct spx_istat spx_istat;
71 /* Following was struct spxstat spxstat; */
73 #define spxstat spx_istat.newstats
76 static int spx_backoff[SPX_MAXRXTSHIFT+1] =
77 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
79 static struct spxpcb *spx_close(struct spxpcb *cb);
80 static struct spxpcb *spx_disconnect(struct spxpcb *cb);
81 static struct spxpcb *spx_drop(struct spxpcb *cb, int errno);
82 static int spx_output(struct spxpcb *cb, struct mbuf *m0);
83 static int spx_reass(struct spxpcb *cb, struct spx *si);
84 static void spx_setpersist(struct spxpcb *cb);
85 static void spx_template(struct spxpcb *cb);
86 static struct spxpcb *spx_timers(struct spxpcb *cb, int timer);
87 static struct spxpcb *spx_usrclosed(struct spxpcb *cb);
89 static int spx_usr_abort(struct socket *so);
90 static int spx_accept(struct socket *so, struct sockaddr **nam);
91 static int spx_attach(struct socket *so, int proto, struct thread *td);
92 static int spx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
93 static int spx_connect(struct socket *so, struct sockaddr *nam,
95 static int spx_detach(struct socket *so);
96 static int spx_usr_disconnect(struct socket *so);
97 static int spx_listen(struct socket *so, struct thread *td);
98 static int spx_rcvd(struct socket *so, int flags);
99 static int spx_rcvoob(struct socket *so, struct mbuf *m, int flags);
100 static int spx_send(struct socket *so, int flags, struct mbuf *m,
101 struct sockaddr *addr, struct mbuf *control,
103 static int spx_shutdown(struct socket *so);
104 static int spx_sp_attach(struct socket *so, int proto, struct thread *td);
106 struct pr_usrreqs spx_usrreqs = {
107 spx_usr_abort, spx_accept, spx_attach, spx_bind,
108 spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
109 spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
110 spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
111 ipx_sockaddr, sosend, soreceive, sopoll
114 struct pr_usrreqs spx_usrreq_sps = {
115 spx_usr_abort, spx_accept, spx_sp_attach, spx_bind,
116 spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
117 spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
118 spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
119 ipx_sockaddr, sosend, soreceive, sopoll
126 spx_iss = 1; /* WRONG !! should fish it out of TODR */
135 struct spx *si = mtod(m, struct spx *);
140 spxstat.spxs_rcvtotal++;
142 panic("No ipxpcb in spx_input\n");
146 cb = ipxtospxpcb(ipxp);
150 if (m->m_len < sizeof(*si)) {
151 if ((m = m_pullup(m, sizeof(*si))) == NULL) {
152 spxstat.spxs_rcvshort++;
155 si = mtod(m, struct spx *);
157 si->si_seq = ntohs(si->si_seq);
158 si->si_ack = ntohs(si->si_ack);
159 si->si_alo = ntohs(si->si_alo);
161 so = ipxp->ipxp_socket;
163 if (so->so_options & SO_DEBUG || traceallspxs) {
164 ostate = cb->s_state;
167 if (so->so_options & SO_ACCEPTCONN) {
168 struct spxpcb *ocb = cb;
170 so = sonewconn(so, 0);
175 * This is ugly, but ....
177 * Mark socket as temporary until we're
178 * committed to keeping it. The code at
179 * ``drop'' and ``dropwithreset'' check the
180 * flag dropsocket to see if the temporary
181 * socket created here should be discarded.
182 * We mark the socket as discardable until
183 * we're committed to it below in TCPS_LISTEN.
186 ipxp = (struct ipxpcb *)so->so_pcb;
187 ipxp->ipxp_laddr = si->si_dna;
188 cb = ipxtospxpcb(ipxp);
189 cb->s_mtu = ocb->s_mtu; /* preserve sockopts */
190 cb->s_flags = ocb->s_flags; /* preserve sockopts */
191 cb->s_flags2 = ocb->s_flags2; /* preserve sockopts */
192 cb->s_state = TCPS_LISTEN;
196 * Packet received on connection.
197 * reset idle time and keep-alive timer;
200 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
202 switch (cb->s_state) {
205 struct sockaddr_ipx *sipx, ssipx;
206 struct ipx_addr laddr;
209 * If somebody here was carying on a conversation
210 * and went away, and his pen pal thinks he can
211 * still talk, we get the misdirected packet.
213 if (spx_hardnosed && (si->si_did != 0 || si->si_seq != 0)) {
218 bzero(sipx, sizeof *sipx);
219 sipx->sipx_len = sizeof(*sipx);
220 sipx->sipx_family = AF_IPX;
221 sipx->sipx_addr = si->si_sna;
222 laddr = ipxp->ipxp_laddr;
223 if (ipx_nullhost(laddr))
224 ipxp->ipxp_laddr = si->si_dna;
225 if (ipx_pcbconnect(ipxp, (struct sockaddr *)sipx, &thread0)) {
226 ipxp->ipxp_laddr = laddr;
231 dropsocket = 0; /* committed to socket */
232 cb->s_did = si->si_sid;
233 cb->s_rack = si->si_ack;
234 cb->s_ralo = si->si_alo;
235 #define THREEWAYSHAKE
237 cb->s_state = TCPS_SYN_RECEIVED;
238 cb->s_force = 1 + SPXT_KEEP;
239 spxstat.spxs_accepts++;
240 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
244 * This state means that we have heard a response
245 * to our acceptance of their connection
246 * It is probably logically unnecessary in this
249 case TCPS_SYN_RECEIVED: {
250 if (si->si_did != cb->s_sid) {
255 ipxp->ipxp_fport = si->si_sport;
256 cb->s_timer[SPXT_REXMT] = 0;
257 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
259 cb->s_state = TCPS_ESTABLISHED;
260 spxstat.spxs_accepts++;
265 * This state means that we have gotten a response
266 * to our attempt to establish a connection.
267 * We fill in the data from the other side,
268 * telling us which port to respond to, instead of the well-
269 * known one we might have sent to in the first place.
270 * We also require that this is a response to our
274 if (si->si_did != cb->s_sid) {
278 spxstat.spxs_connects++;
279 cb->s_did = si->si_sid;
280 cb->s_rack = si->si_ack;
281 cb->s_ralo = si->si_alo;
282 cb->s_dport = ipxp->ipxp_fport = si->si_sport;
283 cb->s_timer[SPXT_REXMT] = 0;
284 cb->s_flags |= SF_ACKNOW;
286 cb->s_state = TCPS_ESTABLISHED;
287 /* Use roundtrip time of connection request for initial rtt */
289 cb->s_srtt = cb->s_rtt << 3;
290 cb->s_rttvar = cb->s_rtt << 1;
291 SPXT_RANGESET(cb->s_rxtcur,
292 ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
293 SPXTV_MIN, SPXTV_REXMTMAX);
297 if (so->so_options & SO_DEBUG || traceallspxs)
298 spx_trace(SA_INPUT, (u_char)ostate, cb, &spx_savesi, 0);
300 m->m_len -= sizeof(struct ipx);
301 m->m_pkthdr.len -= sizeof(struct ipx);
302 m->m_data += sizeof(struct ipx);
304 if (spx_reass(cb, si)) {
307 if (cb->s_force || (cb->s_flags & (SF_ACKNOW|SF_WIN|SF_RXT)))
308 spx_output(cb, (struct mbuf *)NULL);
309 cb->s_flags &= ~(SF_WIN|SF_RXT);
315 si->si_seq = ntohs(si->si_seq);
316 si->si_ack = ntohs(si->si_ack);
317 si->si_alo = ntohs(si->si_alo);
319 if (cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG || traceallspxs)
320 spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
325 if (cb == 0 || cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG ||
327 spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
331 static int spxrexmtthresh = 3;
334 * This is structurally similar to the tcp reassembly routine
335 * but its function is somewhat different: It merely queues
336 * packets up, and suppresses duplicates.
345 struct socket *so = cb->s_ipxpcb->ipxp_socket;
346 char packetp = cb->s_flags & SF_HI;
353 * Update our news from them.
355 if (si->si_cc & SPX_SA)
356 cb->s_flags |= (spx_use_delack ? SF_DELACK : SF_ACKNOW);
357 if (SSEQ_GT(si->si_alo, cb->s_ralo))
358 cb->s_flags |= SF_WIN;
359 if (SSEQ_LEQ(si->si_ack, cb->s_rack)) {
360 if ((si->si_cc & SPX_SP) && cb->s_rack != (cb->s_smax + 1)) {
361 spxstat.spxs_rcvdupack++;
363 * If this is a completely duplicate ack
364 * and other conditions hold, we assume
365 * a packet has been dropped and retransmit
366 * it exactly as in tcp_input().
368 if (si->si_ack != cb->s_rack ||
369 si->si_alo != cb->s_ralo)
371 else if (++cb->s_dupacks == spxrexmtthresh) {
372 u_short onxt = cb->s_snxt;
373 int cwnd = cb->s_cwnd;
375 cb->s_snxt = si->si_ack;
377 cb->s_force = 1 + SPXT_REXMT;
378 spx_output(cb, (struct mbuf *)NULL);
379 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
381 if (cwnd >= 4 * CUNIT)
382 cb->s_cwnd = cwnd / 2;
383 if (SSEQ_GT(onxt, cb->s_snxt))
393 * If our correspondent acknowledges data we haven't sent
394 * TCP would drop the packet after acking. We'll be a little
397 if (SSEQ_GT(si->si_ack, (cb->s_smax + 1))) {
398 spxstat.spxs_rcvacktoomuch++;
399 si->si_ack = cb->s_smax + 1;
401 spxstat.spxs_rcvackpack++;
403 * If transmit timer is running and timed sequence
404 * number was acked, update smoothed round trip time.
405 * See discussion of algorithm in tcp_input.c
407 if (cb->s_rtt && SSEQ_GT(si->si_ack, cb->s_rtseq)) {
408 spxstat.spxs_rttupdated++;
409 if (cb->s_srtt != 0) {
411 delta = cb->s_rtt - (cb->s_srtt >> 3);
412 if ((cb->s_srtt += delta) <= 0)
416 delta -= (cb->s_rttvar >> 2);
417 if ((cb->s_rttvar += delta) <= 0)
421 * No rtt measurement yet
423 cb->s_srtt = cb->s_rtt << 3;
424 cb->s_rttvar = cb->s_rtt << 1;
428 SPXT_RANGESET(cb->s_rxtcur,
429 ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
430 SPXTV_MIN, SPXTV_REXMTMAX);
433 * If all outstanding data is acked, stop retransmit
434 * timer and remember to restart (more output or persist).
435 * If there is more data to be acked, restart retransmit
436 * timer, using current (possibly backed-off) value;
438 if (si->si_ack == cb->s_smax + 1) {
439 cb->s_timer[SPXT_REXMT] = 0;
440 cb->s_flags |= SF_RXT;
441 } else if (cb->s_timer[SPXT_PERSIST] == 0)
442 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
444 * When new data is acked, open the congestion window.
445 * If the window gives us less than ssthresh packets
446 * in flight, open exponentially (maxseg at a time).
447 * Otherwise open linearly (maxseg^2 / cwnd at a time).
450 if (cb->s_cwnd > cb->s_ssthresh)
451 incr = max(incr * incr / cb->s_cwnd, 1);
452 cb->s_cwnd = min(cb->s_cwnd + incr, cb->s_cwmx);
454 * Trim Acked data from output queue.
456 while ((m = so->so_snd.sb_mb) != NULL) {
457 if (SSEQ_LT((mtod(m, struct spx *))->si_seq, si->si_ack))
458 sbdroprecord(&so->so_snd);
463 cb->s_rack = si->si_ack;
465 if (SSEQ_LT(cb->s_snxt, cb->s_rack))
466 cb->s_snxt = cb->s_rack;
467 if (SSEQ_LT(cb->s_swl1, si->si_seq) || ((cb->s_swl1 == si->si_seq &&
468 (SSEQ_LT(cb->s_swl2, si->si_ack))) ||
469 (cb->s_swl2 == si->si_ack && SSEQ_LT(cb->s_ralo, si->si_alo)))) {
470 /* keep track of pure window updates */
471 if ((si->si_cc & SPX_SP) && cb->s_swl2 == si->si_ack
472 && SSEQ_LT(cb->s_ralo, si->si_alo)) {
473 spxstat.spxs_rcvwinupd++;
474 spxstat.spxs_rcvdupack--;
476 cb->s_ralo = si->si_alo;
477 cb->s_swl1 = si->si_seq;
478 cb->s_swl2 = si->si_ack;
479 cb->s_swnd = (1 + si->si_alo - si->si_ack);
480 if (cb->s_swnd > cb->s_smxw)
481 cb->s_smxw = cb->s_swnd;
482 cb->s_flags |= SF_WIN;
485 * If this packet number is higher than that which
486 * we have allocated refuse it, unless urgent
488 if (SSEQ_GT(si->si_seq, cb->s_alo)) {
489 if (si->si_cc & SPX_SP) {
490 spxstat.spxs_rcvwinprobe++;
493 spxstat.spxs_rcvpackafterwin++;
494 if (si->si_cc & SPX_OB) {
495 if (SSEQ_GT(si->si_seq, cb->s_alo + 60)) {
498 } /* else queue this packet; */
500 /*register struct socket *so = cb->s_ipxpcb->ipxp_socket;
501 if (so->so_state && SS_NOFDREF) {
511 * If this is a system packet, we don't need to
512 * queue it up, and won't update acknowledge #
514 if (si->si_cc & SPX_SP) {
518 * We have already seen this packet, so drop.
520 if (SSEQ_LT(si->si_seq, cb->s_ack)) {
522 spxstat.spxs_rcvduppack++;
523 if (si->si_seq == cb->s_ack - 1)
528 * Loop through all packets queued up to insert in
529 * appropriate sequence.
531 for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
532 if (si->si_seq == SI(q)->si_seq) {
533 spxstat.spxs_rcvduppack++;
536 if (SSEQ_LT(si->si_seq, SI(q)->si_seq)) {
537 spxstat.spxs_rcvoopack++;
541 insque(si, q->si_prev);
543 * If this packet is urgent, inform process
545 if (si->si_cc & SPX_OB) {
546 cb->s_iobc = ((char *)si)[1 + sizeof(*si)];
548 cb->s_oobflags |= SF_IOOB;
551 #define SPINC sizeof(struct spxhdr)
553 * Loop through all packets queued up to update acknowledge
554 * number, and present all acknowledged data to user;
555 * If in packet interface mode, show packet headers.
557 for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
558 if (SI(q)->si_seq == cb->s_ack) {
561 if (SI(q)->si_cc & SPX_OB) {
562 cb->s_oobflags &= ~SF_IOOB;
563 if (so->so_rcv.sb_cc)
564 so->so_oobmark = so->so_rcv.sb_cc;
566 so->so_state |= SS_RCVATMARK;
571 spxstat.spxs_rcvpack++;
573 if (cb->s_flags2 & SF_NEWCALL) {
574 struct spxhdr *sp = mtod(m, struct spxhdr *);
575 u_char dt = sp->spx_dt;
577 if (dt != cb->s_rhdr.spx_dt) {
579 m_getclr(M_DONTWAIT, MT_CONTROL);
584 cb->s_rhdr.spx_dt = dt;
585 mm->m_len = 5; /*XXX*/
588 *(u_char *)(&s[2]) = dt;
589 sbappend(&so->so_rcv, mm);
592 if (sp->spx_cc & SPX_OB) {
593 MCHTYPE(m, MT_OOBDATA);
596 so->so_state &= ~SS_RCVATMARK;
601 m->m_pkthdr.len -= SPINC;
603 if ((sp->spx_cc & SPX_EM) || packetp) {
604 sbappendrecord(&so->so_rcv, m);
607 sbappend(&so->so_rcv, m);
611 sbappendrecord(&so->so_rcv, m);
613 cb->s_rhdr = *mtod(m, struct spxhdr *);
616 m->m_pkthdr.len -= SPINC;
617 sbappend(&so->so_rcv, m);
628 spx_ctlinput(cmd, arg_as_sa, dummy)
630 struct sockaddr *arg_as_sa; /* XXX should be swapped with dummy */
633 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
635 struct sockaddr_ipx *sipx;
637 if (cmd < 0 || cmd > PRC_NCMDS)
647 case PRC_HOSTUNREACH:
648 sipx = (struct sockaddr_ipx *)arg;
649 if (sipx->sipx_family != AF_IPX)
651 na = &sipx->sipx_addr;
664 struct spxpcb *cb = (struct spxpcb *)(ipxp->ipxp_pcb);
670 struct mbuf *firstbad, *m0;
674 * The notification that we have sent
675 * too much is bad news -- we will
676 * have to go through queued up so far
677 * splitting ones which are too big and
678 * reassigning sequence numbers and checksums.
679 * we should then retransmit all packets from
680 * one above the offending packet to the last one
681 * we had sent (or our allocation)
682 * then the offending one so that the any queued
683 * data at our destination will be discarded.
685 ep = (struct ipx_errp *)ipxp->ipxp_notify_param;
686 sb = &ipxp->ipxp_socket->so_snd;
687 cb->s_mtu = ep->ipx_err_param;
688 badseq = SI(&ep->ipx_err_ipx)->si_seq;
689 for (m = sb->sb_mb; m != NULL; m = m->m_act) {
690 si = mtod(m, struct spx *);
691 if (si->si_seq == badseq)
698 /* calculate length */
699 for (m0 = m, len = 0; m != NULL; m = m->m_next)
701 if (len > cb->s_mtu) {
714 struct socket *so = cb->s_ipxpcb->ipxp_socket;
716 struct spx *si = (struct spx *)NULL;
717 struct sockbuf *sb = &so->so_snd;
718 int len = 0, win, rcv_win;
719 short span, off, recordp = 0;
721 int error = 0, sendalot;
731 * Make sure that packet isn't too big.
733 for (m = m0; m != NULL; m = m->m_next) {
736 if (m->m_flags & M_EOR)
739 datalen = (cb->s_flags & SF_HO) ?
740 len - sizeof(struct spxhdr) : len;
742 if (cb->s_flags & SF_PI) {
746 int oldEM = cb->s_cc & SPX_EM;
751 * Here we are only being called
752 * from usrreq(), so it is OK to
755 m = m_copym(m0, 0, mtu, M_WAIT);
756 if (cb->s_flags & SF_NEWCALL) {
760 mm->m_flags &= ~M_EOR;
764 error = spx_output(cb, m);
777 * Force length even, by adding a "garbage byte" if
782 if (M_TRAILINGSPACE(m) >= 1)
785 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
792 *(mtod(m1, u_char *)) = 0;
796 m = m_gethdr(M_DONTWAIT, MT_HEADER);
802 * Fill in mbuf with extended SP header
803 * and addresses and length put into network format.
805 MH_ALIGN(m, sizeof(struct spx));
806 m->m_len = sizeof(struct spx);
808 si = mtod(m, struct spx *);
809 si->si_i = *cb->s_ipx;
810 si->si_s = cb->s_shdr;
811 if ((cb->s_flags & SF_PI) && (cb->s_flags & SF_HO)) {
813 if (m0->m_len < sizeof(*sh)) {
814 if((m0 = m_pullup(m0, sizeof(*sh))) == NULL) {
821 sh = mtod(m0, struct spxhdr *);
822 si->si_dt = sh->spx_dt;
823 si->si_cc |= sh->spx_cc & SPX_EM;
824 m0->m_len -= sizeof(*sh);
825 m0->m_data += sizeof(*sh);
829 if ((cb->s_flags2 & SF_NEWCALL) && recordp) {
833 if (cb->s_oobflags & SF_SOOB) {
836 * make sure OB packets convey exactly 1 byte.
837 * If the packet is 1 byte or larger, we
838 * have already guaranted there to be at least
839 * one garbage byte for the checksum, and
840 * extra bytes shouldn't hurt!
842 if (len > sizeof(*si)) {
844 len = (1 + sizeof(*si));
847 si->si_len = htons((u_short)len);
848 m->m_pkthdr.len = ((len - 1) | 1) + 1;
850 * queue stuff up for output
852 sbappendrecord(sb, m);
856 idle = (cb->s_smax == (cb->s_rack - 1));
860 off = cb->s_snxt - cb->s_rack;
861 win = min(cb->s_swnd, (cb->s_cwnd / CUNIT));
864 * If in persist timeout with window of 0, send a probe.
865 * Otherwise, if window is small but nonzero
866 * and timer expired, send what we can and go into
869 if (cb->s_force == 1 + SPXT_PERSIST) {
871 cb->s_timer[SPXT_PERSIST] = 0;
875 span = cb->s_seq - cb->s_rack;
876 len = min(span, win) - off;
880 * Window shrank after we went into it.
881 * If window shrank to 0, cancel pending
882 * restransmission and pull s_snxt back
883 * to (closed) window. We will enter persist
884 * state below. If the widndow didn't close completely,
885 * just wait for an ACK.
889 cb->s_timer[SPXT_REXMT] = 0;
890 cb->s_snxt = cb->s_rack;
895 rcv_win = sbspace(&so->so_rcv);
898 * Send if we owe peer an ACK.
900 if (cb->s_oobflags & SF_SOOB) {
902 * must transmit this out of band packet
904 cb->s_oobflags &= ~ SF_SOOB;
906 spxstat.spxs_sndurg++;
909 if (cb->s_flags & SF_ACKNOW)
911 if (cb->s_state < TCPS_ESTABLISHED)
914 * Silly window can't happen in spx.
915 * Code from tcp deleted.
920 * Compare available window to amount of window
921 * known to peer (as advertised window less
922 * next expected input.) If the difference is at least two
923 * packets or at least 35% of the mximum possible window,
924 * then want to send a window update to peer.
927 u_short delta = 1 + cb->s_alo - cb->s_ack;
928 int adv = rcv_win - (delta * cb->s_mtu);
930 if ((so->so_rcv.sb_cc == 0 && adv >= (2 * cb->s_mtu)) ||
931 (100 * adv / so->so_rcv.sb_hiwat >= 35)) {
932 spxstat.spxs_sndwinup++;
933 cb->s_flags |= SF_ACKNOW;
939 * Many comments from tcp_output.c are appropriate here
941 * If send window is too small, there is data to transmit, and no
942 * retransmit or persist is pending, then go to persist state.
943 * If nothing happens soon, send when timer expires:
944 * if window is nonzero, transmit what we can,
945 * otherwise send a probe.
947 if (so->so_snd.sb_cc && cb->s_timer[SPXT_REXMT] == 0 &&
948 cb->s_timer[SPXT_PERSIST] == 0) {
953 * No reason to send a packet, just return.
960 * Find requested packet.
964 cb->s_want = cb->s_snxt;
965 for (m = sb->sb_mb; m != NULL; m = m->m_act) {
966 si = mtod(m, struct spx *);
967 if (SSEQ_LEQ(cb->s_snxt, si->si_seq))
972 if (si->si_seq == cb->s_snxt)
975 spxstat.spxs_sndvoid++, si = 0;
983 alo = cb->s_ack - 1 + (rcv_win / ((short)cb->s_mtu));
984 if (SSEQ_LT(alo, cb->s_alo))
989 * must make a copy of this packet for
990 * ipx_output to monkey with
992 m = m_copy(dtom(si), 0, (int)M_COPYALL);
996 si = mtod(m, struct spx *);
997 if (SSEQ_LT(si->si_seq, cb->s_smax))
998 spxstat.spxs_sndrexmitpack++;
1000 spxstat.spxs_sndpack++;
1001 } else if (cb->s_force || cb->s_flags & SF_ACKNOW) {
1003 * Must send an acknowledgement or a probe
1006 spxstat.spxs_sndprobe++;
1007 if (cb->s_flags & SF_ACKNOW)
1008 spxstat.spxs_sndacks++;
1009 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1013 * Fill in mbuf with extended SP header
1014 * and addresses and length put into network format.
1016 MH_ALIGN(m, sizeof(struct spx));
1017 m->m_len = sizeof(*si);
1018 m->m_pkthdr.len = sizeof(*si);
1019 si = mtod(m, struct spx *);
1020 si->si_i = *cb->s_ipx;
1021 si->si_s = cb->s_shdr;
1022 si->si_seq = cb->s_smax + 1;
1023 si->si_len = htons(sizeof(*si));
1024 si->si_cc |= SPX_SP;
1027 if (so->so_options & SO_DEBUG || traceallspxs)
1028 spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
1032 * Stuff checksum and output datagram.
1034 if ((si->si_cc & SPX_SP) == 0) {
1035 if (cb->s_force != (1 + SPXT_PERSIST) ||
1036 cb->s_timer[SPXT_PERSIST] == 0) {
1038 * If this is a new packet and we are not currently
1039 * timing anything, time this one.
1041 if (SSEQ_LT(cb->s_smax, si->si_seq)) {
1042 cb->s_smax = si->si_seq;
1043 if (cb->s_rtt == 0) {
1044 spxstat.spxs_segstimed++;
1045 cb->s_rtseq = si->si_seq;
1050 * Set rexmt timer if not currently set,
1051 * Initial value for retransmit timer is smoothed
1052 * round-trip time + 2 * round-trip time variance.
1053 * Initialize shift counter which is used for backoff
1054 * of retransmit time.
1056 if (cb->s_timer[SPXT_REXMT] == 0 &&
1057 cb->s_snxt != cb->s_rack) {
1058 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1059 if (cb->s_timer[SPXT_PERSIST]) {
1060 cb->s_timer[SPXT_PERSIST] = 0;
1064 } else if (SSEQ_LT(cb->s_smax, si->si_seq)) {
1065 cb->s_smax = si->si_seq;
1067 } else if (cb->s_state < TCPS_ESTABLISHED) {
1069 cb->s_rtt = 1; /* Time initial handshake */
1070 if (cb->s_timer[SPXT_REXMT] == 0)
1071 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1075 * Do not request acks when we ack their data packets or
1076 * when we do a gratuitous window update.
1078 if (((si->si_cc & SPX_SP) == 0) || cb->s_force)
1079 si->si_cc |= SPX_SA;
1080 si->si_seq = htons(si->si_seq);
1081 si->si_alo = htons(alo);
1082 si->si_ack = htons(cb->s_ack);
1085 si->si_sum = ipx_cksum(m, ntohs(si->si_len));
1087 si->si_sum = 0xffff;
1090 if (so->so_options & SO_DEBUG || traceallspxs)
1091 spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
1093 if (so->so_options & SO_DONTROUTE)
1094 error = ipx_outputfl(m, (struct route *)NULL, IPX_ROUTETOIF);
1096 error = ipx_outputfl(m, &cb->s_ipxpcb->ipxp_route, 0);
1101 spxstat.spxs_sndtotal++;
1103 * Data sent (as far as we can tell).
1104 * If this advertises a larger window than any other segment,
1105 * then remember the size of the advertized window.
1106 * Any pending ACK has now been sent.
1109 cb->s_flags &= ~(SF_ACKNOW|SF_DELACK);
1110 if (SSEQ_GT(alo, cb->s_alo))
1118 static int spx_do_persist_panics = 0;
1124 int t = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
1126 if (cb->s_timer[SPXT_REXMT] && spx_do_persist_panics)
1127 panic("spx_output REXMT");
1129 * Start/restart persistance timer.
1131 SPXT_RANGESET(cb->s_timer[SPXT_PERSIST],
1132 t*spx_backoff[cb->s_rxtshift],
1133 SPXTV_PERSMIN, SPXTV_PERSMAX);
1134 if (cb->s_rxtshift < SPX_MAXRXTSHIFT)
1139 spx_ctloutput(so, sopt)
1141 struct sockopt *sopt;
1143 struct ipxpcb *ipxp = sotoipxpcb(so);
1152 if (sopt->sopt_level != IPXPROTO_SPX) {
1153 /* This will have to be changed when we do more general
1154 stacking of protocols */
1155 return (ipx_ctloutput(so, sopt));
1160 cb = ipxtospxpcb(ipxp);
1162 switch (sopt->sopt_dir) {
1164 switch (sopt->sopt_name) {
1165 case SO_HEADERS_ON_INPUT:
1169 case SO_HEADERS_ON_OUTPUT:
1172 soptval = cb->s_flags & mask;
1173 error = sooptcopyout(sopt, &soptval, sizeof soptval);
1177 usoptval = cb->s_mtu;
1178 error = sooptcopyout(sopt, &usoptval, sizeof usoptval);
1181 case SO_LAST_HEADER:
1182 error = sooptcopyout(sopt, &cb->s_rhdr,
1186 case SO_DEFAULT_HEADERS:
1187 error = sooptcopyout(sopt, &cb->s_shdr,
1192 error = ENOPROTOOPT;
1197 switch (sopt->sopt_name) {
1198 /* XXX why are these shorts on get and ints on set?
1199 that doesn't make any sense... */
1200 case SO_HEADERS_ON_INPUT:
1204 case SO_HEADERS_ON_OUTPUT:
1207 error = sooptcopyin(sopt, &optval, sizeof optval,
1212 if (cb->s_flags & SF_PI) {
1214 cb->s_flags |= mask;
1216 cb->s_flags &= ~mask;
1217 } else error = EINVAL;
1221 error = sooptcopyin(sopt, &usoptval, sizeof usoptval,
1225 cb->s_mtu = usoptval;
1230 error = sooptcopyin(sopt, &optval, sizeof optval,
1235 cb->s_flags2 |= SF_NEWCALL;
1238 cb->s_flags2 &= ~SF_NEWCALL;
1244 case SO_DEFAULT_HEADERS:
1248 error = sooptcopyin(sopt, &sp, sizeof sp,
1252 cb->s_dt = sp.spx_dt;
1253 cb->s_cc = sp.spx_cc & SPX_EM;
1258 error = ENOPROTOOPT;
1270 struct ipxpcb *ipxp;
1273 ipxp = sotoipxpcb(so);
1274 cb = ipxtospxpcb(ipxp);
1277 spx_drop(cb, ECONNABORTED);
1283 * Accept a connection. Essentially all the work is
1284 * done at higher levels; just return the address
1285 * of the peer, storing through addr.
1290 struct sockaddr **nam;
1292 struct ipxpcb *ipxp;
1293 struct sockaddr_ipx *sipx, ssipx;
1295 ipxp = sotoipxpcb(so);
1297 bzero(sipx, sizeof *sipx);
1298 sipx->sipx_len = sizeof *sipx;
1299 sipx->sipx_family = AF_IPX;
1300 sipx->sipx_addr = ipxp->ipxp_faddr;
1301 *nam = dup_sockaddr((struct sockaddr *)sipx, 0);
1306 spx_attach(so, proto, td)
1313 struct ipxpcb *ipxp;
1318 ipxp = sotoipxpcb(so);
1319 cb = ipxtospxpcb(ipxp);
1324 error = ipx_pcballoc(so, &ipxpcb, td);
1326 goto spx_attach_end;
1327 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1328 error = soreserve(so, (u_long) 3072, (u_long) 3072);
1330 goto spx_attach_end;
1332 ipxp = sotoipxpcb(so);
1334 MALLOC(cb, struct spxpcb *, sizeof *cb, M_PCB, M_NOWAIT | M_ZERO);
1338 goto spx_attach_end;
1342 mm = m_getclr(M_DONTWAIT, MT_HEADER);
1346 goto spx_attach_end;
1348 cb->s_ipx = mtod(mm, struct ipx *);
1349 cb->s_state = TCPS_LISTEN;
1352 cb->s_q.si_next = cb->s_q.si_prev = &cb->s_q;
1353 cb->s_ipxpcb = ipxp;
1354 cb->s_mtu = 576 - sizeof(struct spx);
1355 cb->s_cwnd = sbspace(sb) * CUNIT / cb->s_mtu;
1356 cb->s_ssthresh = cb->s_cwnd;
1357 cb->s_cwmx = sbspace(sb) * CUNIT / (2 * sizeof(struct spx));
1358 /* Above is recomputed when connecting to account
1359 for changed buffering or mtu's */
1360 cb->s_rtt = SPXTV_SRTTBASE;
1361 cb->s_rttvar = SPXTV_SRTTDFLT << 2;
1362 SPXT_RANGESET(cb->s_rxtcur,
1363 ((SPXTV_SRTTBASE >> 2) + (SPXTV_SRTTDFLT << 2)) >> 1,
1364 SPXTV_MIN, SPXTV_REXMTMAX);
1365 ipxp->ipxp_pcb = (caddr_t)cb;
1372 spx_bind(so, nam, td)
1374 struct sockaddr *nam;
1377 struct ipxpcb *ipxp;
1379 ipxp = sotoipxpcb(so);
1381 return (ipx_pcbbind(ipxp, nam, td));
1385 * Initiate connection to peer.
1386 * Enter SYN_SENT state, and mark socket as connecting.
1387 * Start keep-alive timer, setup prototype header,
1388 * Send initial system packet requesting connection.
1391 spx_connect(so, nam, td)
1393 struct sockaddr *nam;
1398 struct ipxpcb *ipxp;
1401 ipxp = sotoipxpcb(so);
1402 cb = ipxtospxpcb(ipxp);
1405 if (ipxp->ipxp_lport == 0) {
1406 error = ipx_pcbbind(ipxp, (struct sockaddr *)NULL, td);
1408 goto spx_connect_end;
1410 error = ipx_pcbconnect(ipxp, nam, td);
1412 goto spx_connect_end;
1414 spxstat.spxs_connattempt++;
1415 cb->s_state = TCPS_SYN_SENT;
1418 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
1419 cb->s_force = 1 + SPXTV_KEEP;
1421 * Other party is required to respond to
1422 * the port I send from, but he is not
1423 * required to answer from where I am sending to,
1424 * so allow wildcarding.
1425 * original port I am sending to is still saved in
1428 ipxp->ipxp_fport = 0;
1429 error = spx_output(cb, (struct mbuf *)NULL);
1440 struct ipxpcb *ipxp;
1443 ipxp = sotoipxpcb(so);
1444 cb = ipxtospxpcb(ipxp);
1449 if (cb->s_state > TCPS_LISTEN)
1458 * We may decide later to implement connection closing
1459 * handshaking at the spx level optionally.
1460 * here is the hook to do it:
1463 spx_usr_disconnect(so)
1467 struct ipxpcb *ipxp;
1470 ipxp = sotoipxpcb(so);
1471 cb = ipxtospxpcb(ipxp);
1485 struct ipxpcb *ipxp;
1489 ipxp = sotoipxpcb(so);
1490 cb = ipxtospxpcb(ipxp);
1492 if (ipxp->ipxp_lport == 0)
1493 error = ipx_pcbbind(ipxp, (struct sockaddr *)NULL, td);
1495 cb->s_state = TCPS_LISTEN;
1500 * After a receive, possibly send acknowledgment
1501 * updating allocation.
1509 struct ipxpcb *ipxp;
1512 ipxp = sotoipxpcb(so);
1513 cb = ipxtospxpcb(ipxp);
1516 cb->s_flags |= SF_RVD;
1517 spx_output(cb, (struct mbuf *)NULL);
1518 cb->s_flags &= ~SF_RVD;
1524 spx_rcvoob(so, m, flags)
1529 struct ipxpcb *ipxp;
1532 ipxp = sotoipxpcb(so);
1533 cb = ipxtospxpcb(ipxp);
1535 if ((cb->s_oobflags & SF_IOOB) || so->so_oobmark ||
1536 (so->so_state & SS_RCVATMARK)) {
1538 *mtod(m, caddr_t) = cb->s_iobc;
1545 spx_send(so, flags, m, addr, controlp, td)
1549 struct sockaddr *addr;
1550 struct mbuf *controlp;
1555 struct ipxpcb *ipxp;
1559 ipxp = sotoipxpcb(so);
1560 cb = ipxtospxpcb(ipxp);
1563 if (flags & PRUS_OOB) {
1564 if (sbspace(&so->so_snd) < -512) {
1568 cb->s_oobflags |= SF_SOOB;
1570 if (controlp != NULL) {
1571 u_short *p = mtod(controlp, u_short *);
1573 if ((p[0] == 5) && (p[1] == 1)) { /* XXXX, for testing */
1574 cb->s_shdr.spx_dt = *(u_char *)(&p[2]);
1580 error = spx_output(cb, m);
1583 if (controlp != NULL)
1597 struct ipxpcb *ipxp;
1601 ipxp = sotoipxpcb(so);
1602 cb = ipxtospxpcb(ipxp);
1606 cb = spx_usrclosed(cb);
1608 error = spx_output(cb, (struct mbuf *)NULL);
1614 spx_sp_attach(so, proto, td)
1620 struct ipxpcb *ipxp;
1622 error = spx_attach(so, proto, td);
1624 ipxp = sotoipxpcb(so);
1625 ((struct spxpcb *)ipxp->ipxp_pcb)->s_flags |=
1626 (SF_HI | SF_HO | SF_PI);
1632 * Create template to be used to send spx packets on a connection.
1633 * Called after host entry created, fills
1634 * in a skeletal spx header (choosing connection id),
1635 * minimizing the amount of work necessary when the connection is used.
1641 struct ipxpcb *ipxp = cb->s_ipxpcb;
1642 struct ipx *ipx = cb->s_ipx;
1643 struct sockbuf *sb = &(ipxp->ipxp_socket->so_snd);
1645 ipx->ipx_pt = IPXPROTO_SPX;
1646 ipx->ipx_sna = ipxp->ipxp_laddr;
1647 ipx->ipx_dna = ipxp->ipxp_faddr;
1648 cb->s_sid = htons(spx_iss);
1649 spx_iss += SPX_ISSINCR/2;
1651 cb->s_cwnd = (sbspace(sb) * CUNIT) / cb->s_mtu;
1652 cb->s_ssthresh = cb->s_cwnd; /* Try to expand fast to full complement
1654 cb->s_cwmx = (sbspace(sb) * CUNIT) / (2 * sizeof(struct spx));
1655 cb->s_cwmx = max(cb->s_cwmx, cb->s_cwnd);
1656 /* But allow for lots of little packets as well */
1660 * Close a SPIP control block:
1661 * discard spx control block itself
1662 * discard ipx protocol control block
1663 * wake up any sleepers
1665 static struct spxpcb *
1670 struct ipxpcb *ipxp = cb->s_ipxpcb;
1671 struct socket *so = ipxp->ipxp_socket;
1674 s = cb->s_q.si_next;
1675 while (s != &(cb->s_q)) {
1677 m = dtom(s->si_prev);
1681 m_free(dtom(cb->s_ipx));
1684 soisdisconnected(so);
1685 ipx_pcbdetach(ipxp);
1686 spxstat.spxs_closed++;
1687 return ((struct spxpcb *)NULL);
1691 * Someday we may do level 3 handshaking
1692 * to close a connection or send a xerox style error.
1693 * For now, just close.
1695 static struct spxpcb *
1699 return (spx_close(cb));
1702 static struct spxpcb *
1706 return (spx_close(cb));
1710 * Drop connection, reporting
1711 * the specified error.
1713 static struct spxpcb *
1718 struct socket *so = cb->s_ipxpcb->ipxp_socket;
1721 * someday, in the xerox world
1722 * we will generate error protocol packets
1723 * announcing that the socket has gone away.
1725 if (TCPS_HAVERCVDSYN(cb->s_state)) {
1726 spxstat.spxs_drops++;
1727 cb->s_state = TCPS_CLOSED;
1730 spxstat.spxs_conndrops++;
1731 so->so_error = errno;
1732 return (spx_close(cb));
1736 * Fast timeout routine for processing delayed acks
1741 struct ipxpcb *ipxp;
1745 ipxp = ipxpcb.ipxp_next;
1747 for (; ipxp != &ipxpcb; ipxp = ipxp->ipxp_next)
1748 if ((cb = (struct spxpcb *)ipxp->ipxp_pcb) != NULL &&
1749 (cb->s_flags & SF_DELACK)) {
1750 cb->s_flags &= ~SF_DELACK;
1751 cb->s_flags |= SF_ACKNOW;
1752 spxstat.spxs_delack++;
1753 spx_output(cb, (struct mbuf *)NULL);
1759 * spx protocol timeout routine called every 500 ms.
1760 * Updates the timers in all active pcb's and
1761 * causes finite state machine actions if timers expire.
1766 struct ipxpcb *ip, *ipnxt;
1772 * Search through tcb's and update active timers.
1774 ip = ipxpcb.ipxp_next;
1779 while (ip != &ipxpcb) {
1780 cb = ipxtospxpcb(ip);
1781 ipnxt = ip->ipxp_next;
1784 for (i = 0; i < SPXT_NTIMERS; i++) {
1785 if (cb->s_timer[i] && --cb->s_timer[i] == 0) {
1787 if (ipnxt->ipxp_prev != ip)
1797 spx_iss += SPX_ISSINCR/PR_SLOWHZ; /* increment iss */
1802 * SPX timer processing.
1804 static struct spxpcb *
1805 spx_timers(cb, timer)
1812 cb->s_force = 1 + timer;
1816 * 2 MSL timeout in shutdown went off. TCP deletes connection
1820 printf("spx: SPXT_2MSL went off for no reason\n");
1821 cb->s_timer[timer] = 0;
1825 * Retransmission timer went off. Message has not
1826 * been acked within retransmit interval. Back off
1827 * to a longer retransmit interval and retransmit one packet.
1830 if (++cb->s_rxtshift > SPX_MAXRXTSHIFT) {
1831 cb->s_rxtshift = SPX_MAXRXTSHIFT;
1832 spxstat.spxs_timeoutdrop++;
1833 cb = spx_drop(cb, ETIMEDOUT);
1836 spxstat.spxs_rexmttimeo++;
1837 rexmt = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
1838 rexmt *= spx_backoff[cb->s_rxtshift];
1839 SPXT_RANGESET(cb->s_rxtcur, rexmt, SPXTV_MIN, SPXTV_REXMTMAX);
1840 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1842 * If we have backed off fairly far, our srtt
1843 * estimate is probably bogus. Clobber it
1844 * so we'll take the next rtt measurement as our srtt;
1845 * move the current srtt into rttvar to keep the current
1846 * retransmit times until then.
1848 if (cb->s_rxtshift > SPX_MAXRXTSHIFT / 4 ) {
1849 cb->s_rttvar += (cb->s_srtt >> 2);
1852 cb->s_snxt = cb->s_rack;
1854 * If timing a packet, stop the timer.
1858 * See very long discussion in tcp_timer.c about congestion
1859 * window and sstrhesh
1861 win = min(cb->s_swnd, (cb->s_cwnd/CUNIT)) / 2;
1865 cb->s_ssthresh = win * CUNIT;
1866 spx_output(cb, (struct mbuf *)NULL);
1870 * Persistance timer into zero window.
1871 * Force a probe to be sent.
1874 spxstat.spxs_persisttimeo++;
1876 spx_output(cb, (struct mbuf *)NULL);
1880 * Keep-alive timer went off; send something
1881 * or drop connection if idle for too long.
1884 spxstat.spxs_keeptimeo++;
1885 if (cb->s_state < TCPS_ESTABLISHED)
1887 if (cb->s_ipxpcb->ipxp_socket->so_options & SO_KEEPALIVE) {
1888 if (cb->s_idle >= SPXTV_MAXIDLE)
1890 spxstat.spxs_keepprobe++;
1891 spx_output(cb, (struct mbuf *)NULL);
1894 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
1897 spxstat.spxs_keepdrops++;
1898 cb = spx_drop(cb, ETIMEDOUT);