2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2004-2006 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1995, Mike Mitchell
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
68 #include <sys/param.h>
70 #include <sys/malloc.h>
72 #include <sys/mutex.h>
74 #include <sys/protosw.h>
75 #include <sys/signalvar.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
79 #include <sys/systm.h>
81 #include <net/route.h>
82 #include <netinet/tcp_fsm.h>
84 #include <netipx/ipx.h>
85 #include <netipx/ipx_pcb.h>
86 #include <netipx/ipx_var.h>
87 #include <netipx/spx.h>
88 #include <netipx/spx_debug.h>
89 #include <netipx/spx_timer.h>
90 #include <netipx/spx_var.h>
93 * SPX protocol implementation.
95 static struct mtx spx_mtx; /* Protects only spx_iss. */
96 static u_short spx_iss;
97 static u_short spx_newchecks[50];
98 static int spx_hardnosed;
99 static int spx_use_delack = 0;
100 static int traceallspxs = 0;
101 static struct spx_istat spx_istat;
102 static int spxrexmtthresh = 3;
104 #define SPX_LOCK_INIT() mtx_init(&spx_mtx, "spx_mtx", NULL, MTX_DEF)
105 #define SPX_LOCK() mtx_lock(&spx_mtx)
106 #define SPX_UNLOCK() mtx_unlock(&spx_mtx)
108 /* Following was struct spxstat spxstat; */
110 #define spxstat spx_istat.newstats
113 static const int spx_backoff[SPX_MAXRXTSHIFT+1] =
114 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
116 static void spx_close(struct spxpcb *cb);
117 static void spx_disconnect(struct spxpcb *cb);
118 static void spx_drop(struct spxpcb *cb, int errno);
119 static int spx_output(struct spxpcb *cb, struct mbuf *m0);
120 static int spx_reass(struct spxpcb *cb, struct spx *si);
121 static void spx_setpersist(struct spxpcb *cb);
122 static void spx_template(struct spxpcb *cb);
123 static void spx_timers(struct spxpcb *cb, int timer);
124 static void spx_usrclosed(struct spxpcb *cb);
126 static void spx_usr_abort(struct socket *so);
127 static int spx_accept(struct socket *so, struct sockaddr **nam);
128 static int spx_attach(struct socket *so, int proto, struct thread *td);
129 static int spx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
130 static void spx_usr_close(struct socket *so);
131 static int spx_connect(struct socket *so, struct sockaddr *nam,
133 static void spx_detach(struct socket *so);
134 static void spx_pcbdetach(struct ipxpcb *ipxp);
135 static int spx_usr_disconnect(struct socket *so);
136 static int spx_listen(struct socket *so, int backlog, struct thread *td);
137 static int spx_rcvd(struct socket *so, int flags);
138 static int spx_rcvoob(struct socket *so, struct mbuf *m, int flags);
139 static int spx_send(struct socket *so, int flags, struct mbuf *m,
140 struct sockaddr *addr, struct mbuf *control,
142 static int spx_shutdown(struct socket *so);
143 static int spx_sp_attach(struct socket *so, int proto, struct thread *td);
145 struct pr_usrreqs spx_usrreqs = {
146 .pru_abort = spx_usr_abort,
147 .pru_accept = spx_accept,
148 .pru_attach = spx_attach,
149 .pru_bind = spx_bind,
150 .pru_connect = spx_connect,
151 .pru_control = ipx_control,
152 .pru_detach = spx_detach,
153 .pru_disconnect = spx_usr_disconnect,
154 .pru_listen = spx_listen,
155 .pru_peeraddr = ipx_peeraddr,
156 .pru_rcvd = spx_rcvd,
157 .pru_rcvoob = spx_rcvoob,
158 .pru_send = spx_send,
159 .pru_shutdown = spx_shutdown,
160 .pru_sockaddr = ipx_sockaddr,
161 .pru_close = spx_usr_close,
164 struct pr_usrreqs spx_usrreq_sps = {
165 .pru_abort = spx_usr_abort,
166 .pru_accept = spx_accept,
167 .pru_attach = spx_sp_attach,
168 .pru_bind = spx_bind,
169 .pru_connect = spx_connect,
170 .pru_control = ipx_control,
171 .pru_detach = spx_detach,
172 .pru_disconnect = spx_usr_disconnect,
173 .pru_listen = spx_listen,
174 .pru_peeraddr = ipx_peeraddr,
175 .pru_rcvd = spx_rcvd,
176 .pru_rcvoob = spx_rcvoob,
177 .pru_send = spx_send,
178 .pru_shutdown = spx_shutdown,
179 .pru_sockaddr = ipx_sockaddr,
180 .pru_close = spx_usr_close,
184 spx_insque(struct spx_q *element, struct spx_q *head)
187 element->si_next = head->si_next;
188 element->si_prev = head;
189 head->si_next = element;
190 element->si_next->si_prev = element;
194 spx_remque(struct spx_q *element)
197 element->si_next->si_prev = element->si_prev;
198 element->si_prev->si_next = element->si_next;
199 element->si_prev = NULL;
207 spx_iss = 1; /* WRONG !! should fish it out of TODR */
211 spx_input(struct mbuf *m, struct ipxpcb *ipxp)
214 struct spx *si = mtod(m, struct spx *);
216 struct spx spx_savesi;
220 spxstat.spxs_rcvtotal++;
221 KASSERT(ipxp != NULL, ("spx_input: ipxpcb == NULL"));
224 * spx_input() assumes that the caller will hold both the pcb list
225 * lock and also the ipxp lock. spx_input() will release both before
226 * returning, and may in fact trade in the ipxp lock for another pcb
227 * lock following sonewconn().
229 IPX_LIST_LOCK_ASSERT();
230 IPX_LOCK_ASSERT(ipxp);
232 cb = ipxtospxpcb(ipxp);
233 KASSERT(cb != NULL, ("spx_input: cb == NULL"));
235 if (ipxp->ipxp_flags & IPXP_DROPPED)
238 if (m->m_len < sizeof(*si)) {
239 if ((m = m_pullup(m, sizeof(*si))) == NULL) {
242 spxstat.spxs_rcvshort++;
245 si = mtod(m, struct spx *);
247 si->si_seq = ntohs(si->si_seq);
248 si->si_ack = ntohs(si->si_ack);
249 si->si_alo = ntohs(si->si_alo);
251 so = ipxp->ipxp_socket;
252 KASSERT(so != NULL, ("spx_input: so == NULL"));
254 if (so->so_options & SO_DEBUG || traceallspxs) {
255 ostate = cb->s_state;
258 if (so->so_options & SO_ACCEPTCONN) {
259 struct spxpcb *ocb = cb;
261 so = sonewconn(so, 0);
266 * This is ugly, but ....
268 * Mark socket as temporary until we're committed to keeping
269 * it. The code at ``drop'' and ``dropwithreset'' check the
270 * flag dropsocket to see if the temporary socket created
271 * here should be discarded. We mark the socket as
272 * discardable until we're committed to it below in
275 * XXXRW: In the new world order of real kernel parallelism,
276 * temporarily allocating the socket when we're "not sure"
277 * seems like a bad idea, as we might race to remove it if
278 * the listen socket is closed...?
280 * We drop the lock of the listen socket ipxp, and acquire
281 * the lock of the new socket ippx.
285 ipxp = (struct ipxpcb *)so->so_pcb;
287 ipxp->ipxp_laddr = si->si_dna;
288 cb = ipxtospxpcb(ipxp);
289 cb->s_mtu = ocb->s_mtu; /* preserve sockopts */
290 cb->s_flags = ocb->s_flags; /* preserve sockopts */
291 cb->s_flags2 = ocb->s_flags2; /* preserve sockopts */
292 cb->s_state = TCPS_LISTEN;
294 IPX_LOCK_ASSERT(ipxp);
297 * Packet received on connection. Reset idle time and keep-alive
301 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
303 switch (cb->s_state) {
305 struct sockaddr_ipx *sipx, ssipx;
306 struct ipx_addr laddr;
309 * If somebody here was carying on a conversation and went
310 * away, and his pen pal thinks he can still talk, we get the
311 * misdirected packet.
313 if (spx_hardnosed && (si->si_did != 0 || si->si_seq != 0)) {
318 bzero(sipx, sizeof *sipx);
319 sipx->sipx_len = sizeof(*sipx);
320 sipx->sipx_family = AF_IPX;
321 sipx->sipx_addr = si->si_sna;
322 laddr = ipxp->ipxp_laddr;
323 if (ipx_nullhost(laddr))
324 ipxp->ipxp_laddr = si->si_dna;
325 if (ipx_pcbconnect(ipxp, (struct sockaddr *)sipx, &thread0)) {
326 ipxp->ipxp_laddr = laddr;
331 dropsocket = 0; /* committed to socket */
332 cb->s_did = si->si_sid;
333 cb->s_rack = si->si_ack;
334 cb->s_ralo = si->si_alo;
335 #define THREEWAYSHAKE
337 cb->s_state = TCPS_SYN_RECEIVED;
338 cb->s_force = 1 + SPXT_KEEP;
339 spxstat.spxs_accepts++;
340 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
344 case TCPS_SYN_RECEIVED: {
346 * This state means that we have heard a response to our
347 * acceptance of their connection. It is probably logically
348 * unnecessary in this implementation.
350 if (si->si_did != cb->s_sid) {
355 ipxp->ipxp_fport = si->si_sport;
356 cb->s_timer[SPXT_REXMT] = 0;
357 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
359 cb->s_state = TCPS_ESTABLISHED;
360 spxstat.spxs_accepts++;
366 * This state means that we have gotten a response to our
367 * attempt to establish a connection. We fill in the data
368 * from the other side, telling us which port to respond to,
369 * instead of the well-known one we might have sent to in the
370 * first place. We also require that this is a response to
373 if (si->si_did != cb->s_sid) {
377 spxstat.spxs_connects++;
378 cb->s_did = si->si_sid;
379 cb->s_rack = si->si_ack;
380 cb->s_ralo = si->si_alo;
381 cb->s_dport = ipxp->ipxp_fport = si->si_sport;
382 cb->s_timer[SPXT_REXMT] = 0;
383 cb->s_flags |= SF_ACKNOW;
385 cb->s_state = TCPS_ESTABLISHED;
388 * Use roundtrip time of connection request for initial rtt.
391 cb->s_srtt = cb->s_rtt << 3;
392 cb->s_rttvar = cb->s_rtt << 1;
393 SPXT_RANGESET(cb->s_rxtcur,
394 ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
395 SPXTV_MIN, SPXTV_REXMTMAX);
400 if (so->so_options & SO_DEBUG || traceallspxs)
401 spx_trace(SA_INPUT, (u_char)ostate, cb, &spx_savesi, 0);
403 m->m_len -= sizeof(struct ipx);
404 m->m_pkthdr.len -= sizeof(struct ipx);
405 m->m_data += sizeof(struct ipx);
407 if (spx_reass(cb, si))
409 if (cb->s_force || (cb->s_flags & (SF_ACKNOW|SF_WIN|SF_RXT)))
410 spx_output(cb, NULL);
411 cb->s_flags &= ~(SF_WIN|SF_RXT);
417 IPX_LOCK_ASSERT(ipxp);
418 if (cb == NULL || (cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG ||
420 spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
425 KASSERT((so->so_qstate & SQ_INCOMP) != 0,
426 ("spx_input: nascent socket not SQ_INCOMP on soabort()"));
428 TAILQ_REMOVE(&head->so_incomp, so, so_list);
430 so->so_qstate &= ~SQ_INCOMP;
440 IPX_LOCK_ASSERT(ipxp);
441 if (cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG || traceallspxs)
442 spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
449 * This is structurally similar to the tcp reassembly routine but its
450 * function is somewhat different: it merely queues packets up, and
451 * suppresses duplicates.
454 spx_reass(struct spxpcb *cb, struct spx *si)
458 struct socket *so = cb->s_ipxpcb->ipxp_socket;
459 char packetp = cb->s_flags & SF_HI;
463 IPX_LOCK_ASSERT(cb->s_ipxpcb);
469 * Update our news from them.
471 if (si->si_cc & SPX_SA)
472 cb->s_flags |= (spx_use_delack ? SF_DELACK : SF_ACKNOW);
473 if (SSEQ_GT(si->si_alo, cb->s_ralo))
474 cb->s_flags |= SF_WIN;
475 if (SSEQ_LEQ(si->si_ack, cb->s_rack)) {
476 if ((si->si_cc & SPX_SP) && cb->s_rack != (cb->s_smax + 1)) {
477 spxstat.spxs_rcvdupack++;
480 * If this is a completely duplicate ack and other
481 * conditions hold, we assume a packet has been
482 * dropped and retransmit it exactly as in
485 if (si->si_ack != cb->s_rack ||
486 si->si_alo != cb->s_ralo)
488 else if (++cb->s_dupacks == spxrexmtthresh) {
489 u_short onxt = cb->s_snxt;
490 int cwnd = cb->s_cwnd;
492 cb->s_snxt = si->si_ack;
494 cb->s_force = 1 + SPXT_REXMT;
495 spx_output(cb, NULL);
496 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
498 if (cwnd >= 4 * CUNIT)
499 cb->s_cwnd = cwnd / 2;
500 if (SSEQ_GT(onxt, cb->s_snxt))
511 * If our correspondent acknowledges data we haven't sent TCP would
512 * drop the packet after acking. We'll be a little more permissive.
514 if (SSEQ_GT(si->si_ack, (cb->s_smax + 1))) {
515 spxstat.spxs_rcvacktoomuch++;
516 si->si_ack = cb->s_smax + 1;
518 spxstat.spxs_rcvackpack++;
521 * If transmit timer is running and timed sequence number was acked,
522 * update smoothed round trip time. See discussion of algorithm in
525 if (cb->s_rtt && SSEQ_GT(si->si_ack, cb->s_rtseq)) {
526 spxstat.spxs_rttupdated++;
527 if (cb->s_srtt != 0) {
529 delta = cb->s_rtt - (cb->s_srtt >> 3);
530 if ((cb->s_srtt += delta) <= 0)
534 delta -= (cb->s_rttvar >> 2);
535 if ((cb->s_rttvar += delta) <= 0)
539 * No rtt measurement yet.
541 cb->s_srtt = cb->s_rtt << 3;
542 cb->s_rttvar = cb->s_rtt << 1;
546 SPXT_RANGESET(cb->s_rxtcur,
547 ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
548 SPXTV_MIN, SPXTV_REXMTMAX);
552 * If all outstanding data is acked, stop retransmit timer and
553 * remember to restart (more output or persist). If there is more
554 * data to be acked, restart retransmit timer, using current
555 * (possibly backed-off) value;
557 if (si->si_ack == cb->s_smax + 1) {
558 cb->s_timer[SPXT_REXMT] = 0;
559 cb->s_flags |= SF_RXT;
560 } else if (cb->s_timer[SPXT_PERSIST] == 0)
561 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
564 * When new data is acked, open the congestion window. If the window
565 * gives us less than ssthresh packets in flight, open exponentially
566 * (maxseg at a time). Otherwise open linearly (maxseg^2 / cwnd at a
570 if (cb->s_cwnd > cb->s_ssthresh)
571 incr = max(incr * incr / cb->s_cwnd, 1);
572 cb->s_cwnd = min(cb->s_cwnd + incr, cb->s_cwmx);
575 * Trim Acked data from output queue.
577 SOCKBUF_LOCK(&so->so_snd);
578 while ((m = so->so_snd.sb_mb) != NULL) {
579 if (SSEQ_LT((mtod(m, struct spx *))->si_seq, si->si_ack))
580 sbdroprecord_locked(&so->so_snd);
584 sowwakeup_locked(so);
585 cb->s_rack = si->si_ack;
587 if (SSEQ_LT(cb->s_snxt, cb->s_rack))
588 cb->s_snxt = cb->s_rack;
589 if (SSEQ_LT(cb->s_swl1, si->si_seq) || ((cb->s_swl1 == si->si_seq &&
590 (SSEQ_LT(cb->s_swl2, si->si_ack))) ||
591 (cb->s_swl2 == si->si_ack && SSEQ_LT(cb->s_ralo, si->si_alo)))) {
592 /* keep track of pure window updates */
593 if ((si->si_cc & SPX_SP) && cb->s_swl2 == si->si_ack
594 && SSEQ_LT(cb->s_ralo, si->si_alo)) {
595 spxstat.spxs_rcvwinupd++;
596 spxstat.spxs_rcvdupack--;
598 cb->s_ralo = si->si_alo;
599 cb->s_swl1 = si->si_seq;
600 cb->s_swl2 = si->si_ack;
601 cb->s_swnd = (1 + si->si_alo - si->si_ack);
602 if (cb->s_swnd > cb->s_smxw)
603 cb->s_smxw = cb->s_swnd;
604 cb->s_flags |= SF_WIN;
608 * If this packet number is higher than that which we have allocated
609 * refuse it, unless urgent.
611 if (SSEQ_GT(si->si_seq, cb->s_alo)) {
612 if (si->si_cc & SPX_SP) {
613 spxstat.spxs_rcvwinprobe++;
616 spxstat.spxs_rcvpackafterwin++;
617 if (si->si_cc & SPX_OB) {
618 if (SSEQ_GT(si->si_seq, cb->s_alo + 60))
619 return (1); /* else queue this packet; */
623 * XXXRW: This is broken on at least one count:
624 * spx_close() will free the ipxp and related parts,
625 * which are then touched by spx_input() after the
626 * return from spx_reass().
628 /*struct socket *so = cb->s_ipxpcb->ipxp_socket;
629 if (so->so_state && SS_NOFDREF) {
640 * If this is a system packet, we don't need to queue it up, and
641 * won't update acknowledge #.
643 if (si->si_cc & SPX_SP)
647 * We have already seen this packet, so drop.
649 if (SSEQ_LT(si->si_seq, cb->s_ack)) {
651 spxstat.spxs_rcvduppack++;
652 if (si->si_seq == cb->s_ack - 1)
658 * Loop through all packets queued up to insert in appropriate
661 for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
662 if (si->si_seq == SI(q)->si_seq) {
663 spxstat.spxs_rcvduppack++;
666 if (SSEQ_LT(si->si_seq, SI(q)->si_seq)) {
667 spxstat.spxs_rcvoopack++;
671 spx_insque((struct spx_q *)si, q->si_prev);
674 * If this packet is urgent, inform process
676 if (si->si_cc & SPX_OB) {
677 cb->s_iobc = ((char *)si)[1 + sizeof(*si)];
679 cb->s_oobflags |= SF_IOOB;
682 #define SPINC sizeof(struct spxhdr)
683 SOCKBUF_LOCK(&so->so_rcv);
686 * Loop through all packets queued up to update acknowledge number,
687 * and present all acknowledged data to user; if in packet interface
688 * mode, show packet headers.
690 for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
691 if (SI(q)->si_seq == cb->s_ack) {
694 if (SI(q)->si_cc & SPX_OB) {
695 cb->s_oobflags &= ~SF_IOOB;
696 if (so->so_rcv.sb_cc)
697 so->so_oobmark = so->so_rcv.sb_cc;
699 so->so_rcv.sb_state |= SBS_RCVATMARK;
702 spx_remque(q->si_next);
704 spxstat.spxs_rcvpack++;
706 if (cb->s_flags2 & SF_NEWCALL) {
707 struct spxhdr *sp = mtod(m, struct spxhdr *);
708 u_char dt = sp->spx_dt;
710 if (dt != cb->s_rhdr.spx_dt) {
712 m_getclr(M_DONTWAIT, MT_CONTROL);
717 cb->s_rhdr.spx_dt = dt;
718 mm->m_len = 5; /*XXX*/
721 *(u_char *)(&s[2]) = dt;
722 sbappend_locked(&so->so_rcv, mm);
725 if (sp->spx_cc & SPX_OB) {
726 MCHTYPE(m, MT_OOBDATA);
729 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
734 m->m_pkthdr.len -= SPINC;
736 if ((sp->spx_cc & SPX_EM) || packetp) {
737 sbappendrecord_locked(&so->so_rcv, m);
740 sbappend_locked(&so->so_rcv, m);
744 sbappendrecord_locked(&so->so_rcv, m);
746 cb->s_rhdr = *mtod(m, struct spxhdr *);
749 m->m_pkthdr.len -= SPINC;
750 sbappend_locked(&so->so_rcv, m);
756 sorwakeup_locked(so);
758 SOCKBUF_UNLOCK(&so->so_rcv);
763 spx_ctlinput(int cmd, struct sockaddr *arg_as_sa, void *dummy)
766 /* Currently, nothing. */
770 spx_output(struct spxpcb *cb, struct mbuf *m0)
772 struct socket *so = cb->s_ipxpcb->ipxp_socket;
774 struct spx *si = NULL;
775 struct sockbuf *sb = &so->so_snd;
776 int len = 0, win, rcv_win;
777 short span, off, recordp = 0;
779 int error = 0, sendalot;
785 IPX_LOCK_ASSERT(cb->s_ipxpcb);
792 * Make sure that packet isn't too big.
794 for (m = m0; m != NULL; m = m->m_next) {
797 if (m->m_flags & M_EOR)
800 datalen = (cb->s_flags & SF_HO) ?
801 len - sizeof(struct spxhdr) : len;
803 if (cb->s_flags & SF_PI) {
807 int oldEM = cb->s_cc & SPX_EM;
811 m = m_copym(m0, 0, mtu, M_DONTWAIT);
817 if (cb->s_flags & SF_NEWCALL) {
821 mm->m_flags &= ~M_EOR;
825 error = spx_output(cb, m);
839 * Force length even, by adding a "garbage byte" if
844 if (M_TRAILINGSPACE(m) >= 1)
847 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
854 *(mtod(m1, u_char *)) = 0;
858 m = m_gethdr(M_DONTWAIT, MT_DATA);
865 * Fill in mbuf with extended SP header and addresses and
866 * length put into network format.
868 MH_ALIGN(m, sizeof(struct spx));
869 m->m_len = sizeof(struct spx);
871 si = mtod(m, struct spx *);
872 si->si_i = *cb->s_ipx;
873 si->si_s = cb->s_shdr;
874 if ((cb->s_flags & SF_PI) && (cb->s_flags & SF_HO)) {
876 if (m0->m_len < sizeof(*sh)) {
877 if((m0 = m_pullup(m0, sizeof(*sh))) == NULL) {
884 sh = mtod(m0, struct spxhdr *);
885 si->si_dt = sh->spx_dt;
886 si->si_cc |= sh->spx_cc & SPX_EM;
887 m0->m_len -= sizeof(*sh);
888 m0->m_data += sizeof(*sh);
892 if ((cb->s_flags2 & SF_NEWCALL) && recordp) {
896 if (cb->s_oobflags & SF_SOOB) {
898 * Per jqj@cornell: Make sure OB packets convey
899 * exactly 1 byte. If the packet is 1 byte or
900 * larger, we have already guaranted there to be at
901 * least one garbage byte for the checksum, and extra
902 * bytes shouldn't hurt!
904 if (len > sizeof(*si)) {
906 len = (1 + sizeof(*si));
909 si->si_len = htons((u_short)len);
910 m->m_pkthdr.len = ((len - 1) | 1) + 1;
913 * Queue stuff up for output.
915 sbappendrecord(sb, m);
919 idle = (cb->s_smax == (cb->s_rack - 1));
923 off = cb->s_snxt - cb->s_rack;
924 win = min(cb->s_swnd, (cb->s_cwnd / CUNIT));
927 * If in persist timeout with window of 0, send a probe. Otherwise,
928 * if window is small but non-zero and timer expired, send what we
929 * can and go into transmit state.
931 if (cb->s_force == 1 + SPXT_PERSIST) {
933 cb->s_timer[SPXT_PERSIST] = 0;
937 span = cb->s_seq - cb->s_rack;
938 len = min(span, win) - off;
942 * Window shrank after we went into it. If window shrank to
943 * 0, cancel pending restransmission and pull s_snxt back to
944 * (closed) window. We will enter persist state below. If
945 * the widndow didn't close completely, just wait for an ACK.
949 cb->s_timer[SPXT_REXMT] = 0;
950 cb->s_snxt = cb->s_rack;
955 rcv_win = sbspace(&so->so_rcv);
958 * Send if we owe peer an ACK.
960 if (cb->s_oobflags & SF_SOOB) {
962 * Must transmit this out of band packet.
964 cb->s_oobflags &= ~ SF_SOOB;
966 spxstat.spxs_sndurg++;
969 if (cb->s_flags & SF_ACKNOW)
971 if (cb->s_state < TCPS_ESTABLISHED)
975 * Silly window can't happen in spx. Code from TCP deleted.
981 * Compare available window to amount of window known to peer (as
982 * advertised window less next expected input.) If the difference is
983 * at least two packets or at least 35% of the mximum possible
984 * window, then want to send a window update to peer.
987 u_short delta = 1 + cb->s_alo - cb->s_ack;
988 int adv = rcv_win - (delta * cb->s_mtu);
990 if ((so->so_rcv.sb_cc == 0 && adv >= (2 * cb->s_mtu)) ||
991 (100 * adv / so->so_rcv.sb_hiwat >= 35)) {
992 spxstat.spxs_sndwinup++;
993 cb->s_flags |= SF_ACKNOW;
1000 * Many comments from tcp_output.c are appropriate here including ...
1001 * If send window is too small, there is data to transmit, and no
1002 * retransmit or persist is pending, then go to persist state. If
1003 * nothing happens soon, send when timer expires: if window is
1004 * non-zero, transmit what we can, otherwise send a probe.
1006 if (so->so_snd.sb_cc && cb->s_timer[SPXT_REXMT] == 0 &&
1007 cb->s_timer[SPXT_PERSIST] == 0) {
1013 * No reason to send a packet, just return.
1020 * Find requested packet.
1024 cb->s_want = cb->s_snxt;
1025 for (m = sb->sb_mb; m != NULL; m = m->m_act) {
1026 si = mtod(m, struct spx *);
1027 if (SSEQ_LEQ(cb->s_snxt, si->si_seq))
1032 if (si->si_seq == cb->s_snxt)
1035 spxstat.spxs_sndvoid++, si = 0;
1044 alo = cb->s_ack - 1 + (rcv_win / ((short)cb->s_mtu));
1045 if (SSEQ_LT(alo, cb->s_alo))
1050 * Must make a copy of this packet for ipx_output to monkey
1053 m = m_copy(dtom(si), 0, (int)M_COPYALL);
1056 si = mtod(m, struct spx *);
1057 if (SSEQ_LT(si->si_seq, cb->s_smax))
1058 spxstat.spxs_sndrexmitpack++;
1060 spxstat.spxs_sndpack++;
1061 } else if (cb->s_force || cb->s_flags & SF_ACKNOW) {
1063 * Must send an acknowledgement or a probe.
1066 spxstat.spxs_sndprobe++;
1067 if (cb->s_flags & SF_ACKNOW)
1068 spxstat.spxs_sndacks++;
1069 m = m_gethdr(M_DONTWAIT, MT_DATA);
1074 * Fill in mbuf with extended SP header and addresses and
1075 * length put into network format.
1077 MH_ALIGN(m, sizeof(struct spx));
1078 m->m_len = sizeof(*si);
1079 m->m_pkthdr.len = sizeof(*si);
1080 si = mtod(m, struct spx *);
1081 si->si_i = *cb->s_ipx;
1082 si->si_s = cb->s_shdr;
1083 si->si_seq = cb->s_smax + 1;
1084 si->si_len = htons(sizeof(*si));
1085 si->si_cc |= SPX_SP;
1088 if (so->so_options & SO_DEBUG || traceallspxs)
1089 spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
1094 * Stuff checksum and output datagram.
1096 if ((si->si_cc & SPX_SP) == 0) {
1097 if (cb->s_force != (1 + SPXT_PERSIST) ||
1098 cb->s_timer[SPXT_PERSIST] == 0) {
1100 * If this is a new packet and we are not currently
1101 * timing anything, time this one.
1103 if (SSEQ_LT(cb->s_smax, si->si_seq)) {
1104 cb->s_smax = si->si_seq;
1105 if (cb->s_rtt == 0) {
1106 spxstat.spxs_segstimed++;
1107 cb->s_rtseq = si->si_seq;
1113 * Set rexmt timer if not currently set, initial
1114 * value for retransmit timer is smoothed round-trip
1115 * time + 2 * round-trip time variance. Initialize
1116 * shift counter which is used for backoff of
1119 if (cb->s_timer[SPXT_REXMT] == 0 &&
1120 cb->s_snxt != cb->s_rack) {
1121 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1122 if (cb->s_timer[SPXT_PERSIST]) {
1123 cb->s_timer[SPXT_PERSIST] = 0;
1127 } else if (SSEQ_LT(cb->s_smax, si->si_seq))
1128 cb->s_smax = si->si_seq;
1129 } else if (cb->s_state < TCPS_ESTABLISHED) {
1131 cb->s_rtt = 1; /* Time initial handshake */
1132 if (cb->s_timer[SPXT_REXMT] == 0)
1133 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1137 * Do not request acks when we ack their data packets or when we do a
1138 * gratuitous window update.
1140 if (((si->si_cc & SPX_SP) == 0) || cb->s_force)
1141 si->si_cc |= SPX_SA;
1142 si->si_seq = htons(si->si_seq);
1143 si->si_alo = htons(alo);
1144 si->si_ack = htons(cb->s_ack);
1147 si->si_sum = ipx_cksum(m, ntohs(si->si_len));
1149 si->si_sum = 0xffff;
1152 if (so->so_options & SO_DEBUG || traceallspxs)
1153 spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
1155 if (so->so_options & SO_DONTROUTE)
1156 error = ipx_outputfl(m, NULL, IPX_ROUTETOIF);
1158 error = ipx_outputfl(m, &cb->s_ipxpcb->ipxp_route, 0);
1161 spxstat.spxs_sndtotal++;
1164 * Data sent (as far as we can tell). If this advertises a larger
1165 * window than any other segment, then remember the size of the
1166 * advertized window. Any pending ACK has now been sent.
1169 cb->s_flags &= ~(SF_ACKNOW|SF_DELACK);
1170 if (SSEQ_GT(alo, cb->s_alo))
1178 static int spx_do_persist_panics = 0;
1181 spx_setpersist(struct spxpcb *cb)
1183 int t = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
1185 IPX_LOCK_ASSERT(cb->s_ipxpcb);
1187 if (cb->s_timer[SPXT_REXMT] && spx_do_persist_panics)
1188 panic("spx_output REXMT");
1191 * Start/restart persistance timer.
1193 SPXT_RANGESET(cb->s_timer[SPXT_PERSIST],
1194 t*spx_backoff[cb->s_rxtshift],
1195 SPXTV_PERSMIN, SPXTV_PERSMAX);
1196 if (cb->s_rxtshift < SPX_MAXRXTSHIFT)
1201 spx_ctloutput(struct socket *so, struct sockopt *sopt)
1203 struct spxhdr spxhdr;
1204 struct ipxpcb *ipxp;
1211 ipxp = sotoipxpcb(so);
1212 KASSERT(ipxp != NULL, ("spx_ctloutput: ipxp == NULL"));
1215 * This will have to be changed when we do more general stacking of
1218 if (sopt->sopt_level != IPXPROTO_SPX)
1219 return (ipx_ctloutput(so, sopt));
1222 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1224 return (ECONNRESET);
1228 cb = ipxtospxpcb(ipxp);
1229 KASSERT(cb != NULL, ("spx_ctloutput: cb == NULL"));
1232 switch (sopt->sopt_dir) {
1234 switch (sopt->sopt_name) {
1235 case SO_HEADERS_ON_INPUT:
1239 case SO_HEADERS_ON_OUTPUT:
1242 soptval = cb->s_flags & mask;
1244 error = sooptcopyout(sopt, &soptval,
1249 usoptval = cb->s_mtu;
1251 error = sooptcopyout(sopt, &usoptval,
1255 case SO_LAST_HEADER:
1256 spxhdr = cb->s_rhdr;
1258 error = sooptcopyout(sopt, &spxhdr, sizeof(spxhdr));
1261 case SO_DEFAULT_HEADERS:
1262 spxhdr = cb->s_shdr;
1264 error = sooptcopyout(sopt, &spxhdr, sizeof(spxhdr));
1269 error = ENOPROTOOPT;
1275 * XXX Why are these shorts on get and ints on set? That
1276 * doesn't make any sense...
1278 * XXXRW: Note, when we re-acquire the ipxp lock, we should
1279 * re-check that it's not dropped.
1282 switch (sopt->sopt_name) {
1283 case SO_HEADERS_ON_INPUT:
1287 case SO_HEADERS_ON_OUTPUT:
1290 error = sooptcopyin(sopt, &optval, sizeof optval,
1296 if (cb->s_flags & SF_PI) {
1298 cb->s_flags |= mask;
1300 cb->s_flags &= ~mask;
1301 } else error = EINVAL;
1306 error = sooptcopyin(sopt, &usoptval, sizeof usoptval,
1310 /* Unlocked write. */
1311 cb->s_mtu = usoptval;
1316 error = sooptcopyin(sopt, &optval, sizeof optval,
1322 cb->s_flags2 |= SF_NEWCALL;
1325 cb->s_flags2 &= ~SF_NEWCALL;
1332 case SO_DEFAULT_HEADERS:
1336 error = sooptcopyin(sopt, &sp, sizeof sp,
1341 cb->s_dt = sp.spx_dt;
1342 cb->s_cc = sp.spx_cc & SPX_EM;
1348 error = ENOPROTOOPT;
1353 panic("spx_ctloutput: bad socket option direction");
1359 spx_usr_abort(struct socket *so)
1361 struct ipxpcb *ipxp;
1364 ipxp = sotoipxpcb(so);
1365 KASSERT(ipxp != NULL, ("spx_usr_abort: ipxp == NULL"));
1367 cb = ipxtospxpcb(ipxp);
1368 KASSERT(cb != NULL, ("spx_usr_abort: cb == NULL"));
1372 spx_drop(cb, ECONNABORTED);
1378 * Accept a connection. Essentially all the work is done at higher levels;
1379 * just return the address of the peer, storing through addr.
1382 spx_accept(struct socket *so, struct sockaddr **nam)
1384 struct ipxpcb *ipxp;
1385 struct sockaddr_ipx *sipx, ssipx;
1387 ipxp = sotoipxpcb(so);
1388 KASSERT(ipxp != NULL, ("spx_accept: ipxp == NULL"));
1391 bzero(sipx, sizeof *sipx);
1392 sipx->sipx_len = sizeof *sipx;
1393 sipx->sipx_family = AF_IPX;
1395 sipx->sipx_addr = ipxp->ipxp_faddr;
1397 *nam = sodupsockaddr((struct sockaddr *)sipx, M_WAITOK);
1402 spx_attach(struct socket *so, int proto, struct thread *td)
1404 struct ipxpcb *ipxp;
1410 ipxp = sotoipxpcb(so);
1411 KASSERT(ipxp == NULL, ("spx_attach: ipxp != NULL"));
1413 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1414 error = soreserve(so, (u_long) 3072, (u_long) 3072);
1419 cb = malloc(sizeof *cb, M_PCB, M_NOWAIT | M_ZERO);
1422 mm = m_getclr(M_DONTWAIT, MT_DATA);
1429 error = ipx_pcballoc(so, &ipxpcb_list, td);
1436 ipxp = sotoipxpcb(so);
1437 ipxp->ipxp_flags |= IPXP_SPX;
1439 cb->s_ipx = mtod(mm, struct ipx *);
1440 cb->s_state = TCPS_LISTEN;
1443 cb->s_q.si_next = cb->s_q.si_prev = &cb->s_q;
1444 cb->s_ipxpcb = ipxp;
1445 cb->s_mtu = 576 - sizeof(struct spx);
1447 cb->s_cwnd = sbspace(sb) * CUNIT / cb->s_mtu;
1448 cb->s_ssthresh = cb->s_cwnd;
1449 cb->s_cwmx = sbspace(sb) * CUNIT / (2 * sizeof(struct spx));
1452 * Above is recomputed when connecting to account for changed
1453 * buffering or mtu's.
1455 cb->s_rtt = SPXTV_SRTTBASE;
1456 cb->s_rttvar = SPXTV_SRTTDFLT << 2;
1457 SPXT_RANGESET(cb->s_rxtcur,
1458 ((SPXTV_SRTTBASE >> 2) + (SPXTV_SRTTDFLT << 2)) >> 1,
1459 SPXTV_MIN, SPXTV_REXMTMAX);
1460 ipxp->ipxp_pcb = (caddr_t)cb;
1466 spx_pcbdetach(struct ipxpcb *ipxp)
1472 IPX_LOCK_ASSERT(ipxp);
1474 cb = ipxtospxpcb(ipxp);
1475 KASSERT(cb != NULL, ("spx_pcbdetach: cb == NULL"));
1477 s = cb->s_q.si_next;
1478 while (s != &(cb->s_q)) {
1484 m_free(dtom(cb->s_ipx));
1486 ipxp->ipxp_pcb = NULL;
1490 spx_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1492 struct ipxpcb *ipxp;
1495 ipxp = sotoipxpcb(so);
1496 KASSERT(ipxp != NULL, ("spx_bind: ipxp == NULL"));
1500 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1504 error = ipx_pcbbind(ipxp, nam, td);
1512 spx_usr_close(struct socket *so)
1514 struct ipxpcb *ipxp;
1517 ipxp = sotoipxpcb(so);
1518 KASSERT(ipxp != NULL, ("spx_usr_close: ipxp == NULL"));
1520 cb = ipxtospxpcb(ipxp);
1521 KASSERT(cb != NULL, ("spx_usr_close: cb == NULL"));
1525 if (cb->s_state > TCPS_LISTEN)
1534 * Initiate connection to peer. Enter SYN_SENT state, and mark socket as
1535 * connecting. Start keep-alive timer, setup prototype header, send initial
1536 * system packet requesting connection.
1539 spx_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1541 struct ipxpcb *ipxp;
1545 ipxp = sotoipxpcb(so);
1546 KASSERT(ipxp != NULL, ("spx_connect: ipxp == NULL"));
1548 cb = ipxtospxpcb(ipxp);
1549 KASSERT(cb != NULL, ("spx_connect: cb == NULL"));
1553 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1555 goto spx_connect_end;
1557 if (ipxp->ipxp_lport == 0) {
1558 error = ipx_pcbbind(ipxp, NULL, td);
1560 goto spx_connect_end;
1562 error = ipx_pcbconnect(ipxp, nam, td);
1564 goto spx_connect_end;
1566 spxstat.spxs_connattempt++;
1567 cb->s_state = TCPS_SYN_SENT;
1570 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
1571 cb->s_force = 1 + SPXTV_KEEP;
1574 * Other party is required to respond to the port I send from, but he
1575 * is not required to answer from where I am sending to, so allow
1576 * wildcarding. Original port I am sending to is still saved in
1579 ipxp->ipxp_fport = 0;
1580 error = spx_output(cb, NULL);
1588 spx_detach(struct socket *so)
1590 struct ipxpcb *ipxp;
1594 * XXXRW: Should assert appropriately detached.
1596 ipxp = sotoipxpcb(so);
1597 KASSERT(ipxp != NULL, ("spx_detach: ipxp == NULL"));
1599 cb = ipxtospxpcb(ipxp);
1600 KASSERT(cb != NULL, ("spx_detach: cb == NULL"));
1604 spx_pcbdetach(ipxp);
1610 * We may decide later to implement connection closing handshaking at the spx
1611 * level optionally. Here is the hook to do it:
1614 spx_usr_disconnect(struct socket *so)
1616 struct ipxpcb *ipxp;
1620 ipxp = sotoipxpcb(so);
1621 KASSERT(ipxp != NULL, ("spx_usr_disconnect: ipxp == NULL"));
1623 cb = ipxtospxpcb(ipxp);
1624 KASSERT(cb != NULL, ("spx_usr_disconnect: cb == NULL"));
1628 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1641 spx_listen(struct socket *so, int backlog, struct thread *td)
1644 struct ipxpcb *ipxp;
1648 ipxp = sotoipxpcb(so);
1649 KASSERT(ipxp != NULL, ("spx_listen: ipxp == NULL"));
1651 cb = ipxtospxpcb(ipxp);
1652 KASSERT(cb != NULL, ("spx_listen: cb == NULL"));
1656 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1661 error = solisten_proto_check(so);
1662 if (error == 0 && ipxp->ipxp_lport == 0)
1663 error = ipx_pcbbind(ipxp, NULL, td);
1665 cb->s_state = TCPS_LISTEN;
1666 solisten_proto(so, backlog);
1676 * After a receive, possibly send acknowledgment updating allocation.
1679 spx_rcvd(struct socket *so, int flags)
1681 struct ipxpcb *ipxp;
1685 ipxp = sotoipxpcb(so);
1686 KASSERT(ipxp != NULL, ("spx_rcvd: ipxp == NULL"));
1688 cb = ipxtospxpcb(ipxp);
1689 KASSERT(cb != NULL, ("spx_rcvd: cb == NULL"));
1692 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1696 cb->s_flags |= SF_RVD;
1697 spx_output(cb, NULL);
1698 cb->s_flags &= ~SF_RVD;
1706 spx_rcvoob(struct socket *so, struct mbuf *m, int flags)
1708 struct ipxpcb *ipxp;
1712 ipxp = sotoipxpcb(so);
1713 KASSERT(ipxp != NULL, ("spx_rcvoob: ipxp == NULL"));
1715 cb = ipxtospxpcb(ipxp);
1716 KASSERT(cb != NULL, ("spx_rcvoob: cb == NULL"));
1719 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1723 SOCKBUF_LOCK(&so->so_rcv);
1724 if ((cb->s_oobflags & SF_IOOB) || so->so_oobmark ||
1725 (so->so_rcv.sb_state & SBS_RCVATMARK)) {
1726 SOCKBUF_UNLOCK(&so->so_rcv);
1728 *mtod(m, caddr_t) = cb->s_iobc;
1732 SOCKBUF_UNLOCK(&so->so_rcv);
1740 spx_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1741 struct mbuf *controlp, struct thread *td)
1743 struct ipxpcb *ipxp;
1747 ipxp = sotoipxpcb(so);
1748 KASSERT(ipxp != NULL, ("spx_send: ipxp == NULL"));
1750 cb = ipxtospxpcb(ipxp);
1751 KASSERT(cb != NULL, ("spx_send: cb == NULL"));
1755 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1759 if (flags & PRUS_OOB) {
1760 if (sbspace(&so->so_snd) < -512) {
1764 cb->s_oobflags |= SF_SOOB;
1766 if (controlp != NULL) {
1767 u_short *p = mtod(controlp, u_short *);
1769 if ((p[0] == 5) && (p[1] == 1)) { /* XXXX, for testing */
1770 cb->s_shdr.spx_dt = *(u_char *)(&p[2]);
1776 error = spx_output(cb, m);
1780 if (controlp != NULL)
1788 spx_shutdown(struct socket *so)
1790 struct ipxpcb *ipxp;
1794 ipxp = sotoipxpcb(so);
1795 KASSERT(ipxp != NULL, ("spx_shutdown: ipxp == NULL"));
1797 cb = ipxtospxpcb(ipxp);
1798 KASSERT(cb != NULL, ("spx_shutdown: cb == NULL"));
1803 if (ipxp->ipxp_flags & IPXP_DROPPED) {
1816 spx_sp_attach(struct socket *so, int proto, struct thread *td)
1818 struct ipxpcb *ipxp;
1822 KASSERT(so->so_pcb == NULL, ("spx_sp_attach: so_pcb != NULL"));
1824 error = spx_attach(so, proto, td);
1828 ipxp = sotoipxpcb(so);
1829 KASSERT(ipxp != NULL, ("spx_sp_attach: ipxp == NULL"));
1831 cb = ipxtospxpcb(ipxp);
1832 KASSERT(cb != NULL, ("spx_sp_attach: cb == NULL"));
1835 cb->s_flags |= (SF_HI | SF_HO | SF_PI);
1841 * Create template to be used to send spx packets on a connection. Called
1842 * after host entry created, fills in a skeletal spx header (choosing
1843 * connection id), minimizing the amount of work necessary when the
1844 * connection is used.
1847 spx_template(struct spxpcb *cb)
1849 struct ipxpcb *ipxp = cb->s_ipxpcb;
1850 struct ipx *ipx = cb->s_ipx;
1851 struct sockbuf *sb = &(ipxp->ipxp_socket->so_snd);
1853 IPX_LOCK_ASSERT(ipxp);
1855 ipx->ipx_pt = IPXPROTO_SPX;
1856 ipx->ipx_sna = ipxp->ipxp_laddr;
1857 ipx->ipx_dna = ipxp->ipxp_faddr;
1859 cb->s_sid = htons(spx_iss);
1860 spx_iss += SPX_ISSINCR/2;
1863 cb->s_cwnd = (sbspace(sb) * CUNIT) / cb->s_mtu;
1866 * Try to expand fast to full complement of large packets.
1868 cb->s_ssthresh = cb->s_cwnd;
1869 cb->s_cwmx = (sbspace(sb) * CUNIT) / (2 * sizeof(struct spx));
1872 * But allow for lots of little packets as well.
1874 cb->s_cwmx = max(cb->s_cwmx, cb->s_cwnd);
1878 * Close a SPIP control block. Wake up any sleepers. We used to free any
1879 * queued packets and cb->s_ipx here, but now we defer that until the pcb is
1883 spx_close(struct spxpcb *cb)
1885 struct ipxpcb *ipxp = cb->s_ipxpcb;
1886 struct socket *so = ipxp->ipxp_socket;
1888 KASSERT(ipxp != NULL, ("spx_close: ipxp == NULL"));
1889 IPX_LIST_LOCK_ASSERT();
1890 IPX_LOCK_ASSERT(ipxp);
1892 ipxp->ipxp_flags |= IPXP_DROPPED;
1893 soisdisconnected(so);
1894 spxstat.spxs_closed++;
1898 * Someday we may do level 3 handshaking to close a connection or send a
1899 * xerox style error. For now, just close. cb will always be invalid after
1903 spx_usrclosed(struct spxpcb *cb)
1906 IPX_LIST_LOCK_ASSERT();
1907 IPX_LOCK_ASSERT(cb->s_ipxpcb);
1913 * cb will always be invalid after this call.
1916 spx_disconnect(struct spxpcb *cb)
1919 IPX_LIST_LOCK_ASSERT();
1920 IPX_LOCK_ASSERT(cb->s_ipxpcb);
1926 * Drop connection, reporting the specified error. cb will always be invalid
1930 spx_drop(struct spxpcb *cb, int errno)
1932 struct socket *so = cb->s_ipxpcb->ipxp_socket;
1934 IPX_LIST_LOCK_ASSERT();
1935 IPX_LOCK_ASSERT(cb->s_ipxpcb);
1938 * Someday, in the xerox world we will generate error protocol
1939 * packets announcing that the socket has gone away.
1941 if (TCPS_HAVERCVDSYN(cb->s_state)) {
1942 spxstat.spxs_drops++;
1943 cb->s_state = TCPS_CLOSED;
1946 spxstat.spxs_conndrops++;
1947 so->so_error = errno;
1952 * Fast timeout routine for processing delayed acks.
1957 struct ipxpcb *ipxp;
1961 LIST_FOREACH(ipxp, &ipxpcb_list, ipxp_list) {
1963 if (!(ipxp->ipxp_flags & IPXP_SPX) ||
1964 (ipxp->ipxp_flags & IPXP_DROPPED)) {
1968 cb = ipxtospxpcb(ipxp);
1969 if (cb->s_flags & SF_DELACK) {
1970 cb->s_flags &= ~SF_DELACK;
1971 cb->s_flags |= SF_ACKNOW;
1972 spxstat.spxs_delack++;
1973 spx_output(cb, NULL);
1981 * spx protocol timeout routine called every 500 ms. Updates the timers in
1982 * all active pcb's and causes finite state machine actions if timers expire.
1987 struct ipxpcb *ipxp;
1992 * Search through tcb's and update active timers. Once, timers could
1993 * free ipxp's, but now we do that only when detaching a socket.
1996 LIST_FOREACH(ipxp, &ipxpcb_list, ipxp_list) {
1998 if (!(ipxp->ipxp_flags & IPXP_SPX) ||
1999 (ipxp->ipxp_flags & IPXP_DROPPED)) {
2004 cb = (struct spxpcb *)ipxp->ipxp_pcb;
2005 KASSERT(cb != NULL, ("spx_slowtimo: cb == NULL"));
2006 for (i = 0; i < SPXT_NTIMERS; i++) {
2007 if (cb->s_timer[i] && --cb->s_timer[i] == 0) {
2009 if (ipxp->ipxp_flags & IPXP_DROPPED)
2013 if (!(ipxp->ipxp_flags & IPXP_DROPPED)) {
2022 spx_iss += SPX_ISSINCR/PR_SLOWHZ; /* increment iss */
2027 * SPX timer processing.
2030 spx_timers(struct spxpcb *cb, int timer)
2035 IPX_LIST_LOCK_ASSERT();
2036 IPX_LOCK_ASSERT(cb->s_ipxpcb);
2038 cb->s_force = 1 + timer;
2042 * 2 MSL timeout in shutdown went off. TCP deletes
2043 * connection control block.
2045 printf("spx: SPXT_2MSL went off for no reason\n");
2046 cb->s_timer[timer] = 0;
2051 * Retransmission timer went off. Message has not been acked
2052 * within retransmit interval. Back off to a longer
2053 * retransmit interval and retransmit one packet.
2055 if (++cb->s_rxtshift > SPX_MAXRXTSHIFT) {
2056 cb->s_rxtshift = SPX_MAXRXTSHIFT;
2057 spxstat.spxs_timeoutdrop++;
2058 spx_drop(cb, ETIMEDOUT);
2061 spxstat.spxs_rexmttimeo++;
2062 rexmt = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
2063 rexmt *= spx_backoff[cb->s_rxtshift];
2064 SPXT_RANGESET(cb->s_rxtcur, rexmt, SPXTV_MIN, SPXTV_REXMTMAX);
2065 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
2068 * If we have backed off fairly far, our srtt estimate is
2069 * probably bogus. Clobber it so we'll take the next rtt
2070 * measurement as our srtt; move the current srtt into rttvar
2071 * to keep the current retransmit times until then.
2073 if (cb->s_rxtshift > SPX_MAXRXTSHIFT / 4 ) {
2074 cb->s_rttvar += (cb->s_srtt >> 2);
2077 cb->s_snxt = cb->s_rack;
2080 * If timing a packet, stop the timer.
2085 * See very long discussion in tcp_timer.c about congestion
2086 * window and sstrhesh.
2088 win = min(cb->s_swnd, (cb->s_cwnd/CUNIT)) / 2;
2092 cb->s_ssthresh = win * CUNIT;
2093 spx_output(cb, NULL);
2098 * Persistance timer into zero window. Force a probe to be
2101 spxstat.spxs_persisttimeo++;
2103 spx_output(cb, NULL);
2108 * Keep-alive timer went off; send something or drop
2109 * connection if idle for too long.
2111 spxstat.spxs_keeptimeo++;
2112 if (cb->s_state < TCPS_ESTABLISHED)
2114 if (cb->s_ipxpcb->ipxp_socket->so_options & SO_KEEPALIVE) {
2115 if (cb->s_idle >= SPXTV_MAXIDLE)
2117 spxstat.spxs_keepprobe++;
2118 spx_output(cb, NULL);
2121 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
2125 spxstat.spxs_keepdrops++;
2126 spx_drop(cb, ETIMEDOUT);
2130 panic("spx_timers: unknown timer %d", timer);