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.8 2004/04/22 05:09:49 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,
92 struct pru_attach_info *ai);
93 static int spx_bind(struct socket *so, struct sockaddr *nam,
95 static int spx_connect(struct socket *so, struct sockaddr *nam,
97 static int spx_detach(struct socket *so);
98 static int spx_usr_disconnect(struct socket *so);
99 static int spx_listen(struct socket *so, struct thread *td);
100 static int spx_rcvd(struct socket *so, int flags);
101 static int spx_rcvoob(struct socket *so, struct mbuf *m, int flags);
102 static int spx_send(struct socket *so, int flags, struct mbuf *m,
103 struct sockaddr *addr, struct mbuf *control,
105 static int spx_shutdown(struct socket *so);
106 static int spx_sp_attach(struct socket *so, int proto,
107 struct pru_attach_info *ai);
109 struct pr_usrreqs spx_usrreqs = {
110 spx_usr_abort, spx_accept, spx_attach, spx_bind,
111 spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
112 spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
113 spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
114 ipx_sockaddr, sosend, soreceive, sopoll
117 struct pr_usrreqs spx_usrreq_sps = {
118 spx_usr_abort, spx_accept, spx_sp_attach, spx_bind,
119 spx_connect, pru_connect2_notsupp, ipx_control, spx_detach,
120 spx_usr_disconnect, spx_listen, ipx_peeraddr, spx_rcvd,
121 spx_rcvoob, spx_send, pru_sense_null, spx_shutdown,
122 ipx_sockaddr, sosend, soreceive, sopoll
129 spx_iss = 1; /* WRONG !! should fish it out of TODR */
138 struct spx *si = mtod(m, struct spx *);
143 spxstat.spxs_rcvtotal++;
145 panic("No ipxpcb in spx_input\n");
149 cb = ipxtospxpcb(ipxp);
153 if (m->m_len < sizeof(*si)) {
154 if ((m = m_pullup(m, sizeof(*si))) == NULL) {
155 spxstat.spxs_rcvshort++;
158 si = mtod(m, struct spx *);
160 si->si_seq = ntohs(si->si_seq);
161 si->si_ack = ntohs(si->si_ack);
162 si->si_alo = ntohs(si->si_alo);
164 so = ipxp->ipxp_socket;
166 if (so->so_options & SO_DEBUG || traceallspxs) {
167 ostate = cb->s_state;
170 if (so->so_options & SO_ACCEPTCONN) {
171 struct spxpcb *ocb = cb;
173 so = sonewconn(so, 0);
178 * This is ugly, but ....
180 * Mark socket as temporary until we're
181 * committed to keeping it. The code at
182 * ``drop'' and ``dropwithreset'' check the
183 * flag dropsocket to see if the temporary
184 * socket created here should be discarded.
185 * We mark the socket as discardable until
186 * we're committed to it below in TCPS_LISTEN.
189 ipxp = (struct ipxpcb *)so->so_pcb;
190 ipxp->ipxp_laddr = si->si_dna;
191 cb = ipxtospxpcb(ipxp);
192 cb->s_mtu = ocb->s_mtu; /* preserve sockopts */
193 cb->s_flags = ocb->s_flags; /* preserve sockopts */
194 cb->s_flags2 = ocb->s_flags2; /* preserve sockopts */
195 cb->s_state = TCPS_LISTEN;
199 * Packet received on connection.
200 * reset idle time and keep-alive timer;
203 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
205 switch (cb->s_state) {
208 struct sockaddr_ipx *sipx, ssipx;
209 struct ipx_addr laddr;
212 * If somebody here was carying on a conversation
213 * and went away, and his pen pal thinks he can
214 * still talk, we get the misdirected packet.
216 if (spx_hardnosed && (si->si_did != 0 || si->si_seq != 0)) {
221 bzero(sipx, sizeof *sipx);
222 sipx->sipx_len = sizeof(*sipx);
223 sipx->sipx_family = AF_IPX;
224 sipx->sipx_addr = si->si_sna;
225 laddr = ipxp->ipxp_laddr;
226 if (ipx_nullhost(laddr))
227 ipxp->ipxp_laddr = si->si_dna;
228 if (ipx_pcbconnect(ipxp, (struct sockaddr *)sipx, &thread0)) {
229 ipxp->ipxp_laddr = laddr;
234 dropsocket = 0; /* committed to socket */
235 cb->s_did = si->si_sid;
236 cb->s_rack = si->si_ack;
237 cb->s_ralo = si->si_alo;
238 #define THREEWAYSHAKE
240 cb->s_state = TCPS_SYN_RECEIVED;
241 cb->s_force = 1 + SPXT_KEEP;
242 spxstat.spxs_accepts++;
243 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
247 * This state means that we have heard a response
248 * to our acceptance of their connection
249 * It is probably logically unnecessary in this
252 case TCPS_SYN_RECEIVED: {
253 if (si->si_did != cb->s_sid) {
258 ipxp->ipxp_fport = si->si_sport;
259 cb->s_timer[SPXT_REXMT] = 0;
260 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
262 cb->s_state = TCPS_ESTABLISHED;
263 spxstat.spxs_accepts++;
268 * This state means that we have gotten a response
269 * to our attempt to establish a connection.
270 * We fill in the data from the other side,
271 * telling us which port to respond to, instead of the well-
272 * known one we might have sent to in the first place.
273 * We also require that this is a response to our
277 if (si->si_did != cb->s_sid) {
281 spxstat.spxs_connects++;
282 cb->s_did = si->si_sid;
283 cb->s_rack = si->si_ack;
284 cb->s_ralo = si->si_alo;
285 cb->s_dport = ipxp->ipxp_fport = si->si_sport;
286 cb->s_timer[SPXT_REXMT] = 0;
287 cb->s_flags |= SF_ACKNOW;
289 cb->s_state = TCPS_ESTABLISHED;
290 /* Use roundtrip time of connection request for initial rtt */
292 cb->s_srtt = cb->s_rtt << 3;
293 cb->s_rttvar = cb->s_rtt << 1;
294 SPXT_RANGESET(cb->s_rxtcur,
295 ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
296 SPXTV_MIN, SPXTV_REXMTMAX);
300 if (so->so_options & SO_DEBUG || traceallspxs)
301 spx_trace(SA_INPUT, (u_char)ostate, cb, &spx_savesi, 0);
303 m->m_len -= sizeof(struct ipx);
304 m->m_pkthdr.len -= sizeof(struct ipx);
305 m->m_data += sizeof(struct ipx);
307 if (spx_reass(cb, si)) {
310 if (cb->s_force || (cb->s_flags & (SF_ACKNOW|SF_WIN|SF_RXT)))
311 spx_output(cb, (struct mbuf *)NULL);
312 cb->s_flags &= ~(SF_WIN|SF_RXT);
318 si->si_seq = ntohs(si->si_seq);
319 si->si_ack = ntohs(si->si_ack);
320 si->si_alo = ntohs(si->si_alo);
322 if (cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG || traceallspxs)
323 spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
328 if (cb == 0 || cb->s_ipxpcb->ipxp_socket->so_options & SO_DEBUG ||
330 spx_trace(SA_DROP, (u_char)ostate, cb, &spx_savesi, 0);
334 static int spxrexmtthresh = 3;
337 * This is structurally similar to the tcp reassembly routine
338 * but its function is somewhat different: It merely queues
339 * packets up, and suppresses duplicates.
348 struct socket *so = cb->s_ipxpcb->ipxp_socket;
349 char packetp = cb->s_flags & SF_HI;
356 * Update our news from them.
358 if (si->si_cc & SPX_SA)
359 cb->s_flags |= (spx_use_delack ? SF_DELACK : SF_ACKNOW);
360 if (SSEQ_GT(si->si_alo, cb->s_ralo))
361 cb->s_flags |= SF_WIN;
362 if (SSEQ_LEQ(si->si_ack, cb->s_rack)) {
363 if ((si->si_cc & SPX_SP) && cb->s_rack != (cb->s_smax + 1)) {
364 spxstat.spxs_rcvdupack++;
366 * If this is a completely duplicate ack
367 * and other conditions hold, we assume
368 * a packet has been dropped and retransmit
369 * it exactly as in tcp_input().
371 if (si->si_ack != cb->s_rack ||
372 si->si_alo != cb->s_ralo)
374 else if (++cb->s_dupacks == spxrexmtthresh) {
375 u_short onxt = cb->s_snxt;
376 int cwnd = cb->s_cwnd;
378 cb->s_snxt = si->si_ack;
380 cb->s_force = 1 + SPXT_REXMT;
381 spx_output(cb, (struct mbuf *)NULL);
382 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
384 if (cwnd >= 4 * CUNIT)
385 cb->s_cwnd = cwnd / 2;
386 if (SSEQ_GT(onxt, cb->s_snxt))
396 * If our correspondent acknowledges data we haven't sent
397 * TCP would drop the packet after acking. We'll be a little
400 if (SSEQ_GT(si->si_ack, (cb->s_smax + 1))) {
401 spxstat.spxs_rcvacktoomuch++;
402 si->si_ack = cb->s_smax + 1;
404 spxstat.spxs_rcvackpack++;
406 * If transmit timer is running and timed sequence
407 * number was acked, update smoothed round trip time.
408 * See discussion of algorithm in tcp_input.c
410 if (cb->s_rtt && SSEQ_GT(si->si_ack, cb->s_rtseq)) {
411 spxstat.spxs_rttupdated++;
412 if (cb->s_srtt != 0) {
414 delta = cb->s_rtt - (cb->s_srtt >> 3);
415 if ((cb->s_srtt += delta) <= 0)
419 delta -= (cb->s_rttvar >> 2);
420 if ((cb->s_rttvar += delta) <= 0)
424 * No rtt measurement yet
426 cb->s_srtt = cb->s_rtt << 3;
427 cb->s_rttvar = cb->s_rtt << 1;
431 SPXT_RANGESET(cb->s_rxtcur,
432 ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1,
433 SPXTV_MIN, SPXTV_REXMTMAX);
436 * If all outstanding data is acked, stop retransmit
437 * timer and remember to restart (more output or persist).
438 * If there is more data to be acked, restart retransmit
439 * timer, using current (possibly backed-off) value;
441 if (si->si_ack == cb->s_smax + 1) {
442 cb->s_timer[SPXT_REXMT] = 0;
443 cb->s_flags |= SF_RXT;
444 } else if (cb->s_timer[SPXT_PERSIST] == 0)
445 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
447 * When new data is acked, open the congestion window.
448 * If the window gives us less than ssthresh packets
449 * in flight, open exponentially (maxseg at a time).
450 * Otherwise open linearly (maxseg^2 / cwnd at a time).
453 if (cb->s_cwnd > cb->s_ssthresh)
454 incr = max(incr * incr / cb->s_cwnd, 1);
455 cb->s_cwnd = min(cb->s_cwnd + incr, cb->s_cwmx);
457 * Trim Acked data from output queue.
459 while ((m = so->so_snd.sb_mb) != NULL) {
460 if (SSEQ_LT((mtod(m, struct spx *))->si_seq, si->si_ack))
461 sbdroprecord(&so->so_snd);
466 cb->s_rack = si->si_ack;
468 if (SSEQ_LT(cb->s_snxt, cb->s_rack))
469 cb->s_snxt = cb->s_rack;
470 if (SSEQ_LT(cb->s_swl1, si->si_seq) || ((cb->s_swl1 == si->si_seq &&
471 (SSEQ_LT(cb->s_swl2, si->si_ack))) ||
472 (cb->s_swl2 == si->si_ack && SSEQ_LT(cb->s_ralo, si->si_alo)))) {
473 /* keep track of pure window updates */
474 if ((si->si_cc & SPX_SP) && cb->s_swl2 == si->si_ack
475 && SSEQ_LT(cb->s_ralo, si->si_alo)) {
476 spxstat.spxs_rcvwinupd++;
477 spxstat.spxs_rcvdupack--;
479 cb->s_ralo = si->si_alo;
480 cb->s_swl1 = si->si_seq;
481 cb->s_swl2 = si->si_ack;
482 cb->s_swnd = (1 + si->si_alo - si->si_ack);
483 if (cb->s_swnd > cb->s_smxw)
484 cb->s_smxw = cb->s_swnd;
485 cb->s_flags |= SF_WIN;
488 * If this packet number is higher than that which
489 * we have allocated refuse it, unless urgent
491 if (SSEQ_GT(si->si_seq, cb->s_alo)) {
492 if (si->si_cc & SPX_SP) {
493 spxstat.spxs_rcvwinprobe++;
496 spxstat.spxs_rcvpackafterwin++;
497 if (si->si_cc & SPX_OB) {
498 if (SSEQ_GT(si->si_seq, cb->s_alo + 60)) {
501 } /* else queue this packet; */
503 /*register struct socket *so = cb->s_ipxpcb->ipxp_socket;
504 if (so->so_state && SS_NOFDREF) {
514 * If this is a system packet, we don't need to
515 * queue it up, and won't update acknowledge #
517 if (si->si_cc & SPX_SP) {
521 * We have already seen this packet, so drop.
523 if (SSEQ_LT(si->si_seq, cb->s_ack)) {
525 spxstat.spxs_rcvduppack++;
526 if (si->si_seq == cb->s_ack - 1)
531 * Loop through all packets queued up to insert in
532 * appropriate sequence.
534 for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
535 if (si->si_seq == SI(q)->si_seq) {
536 spxstat.spxs_rcvduppack++;
539 if (SSEQ_LT(si->si_seq, SI(q)->si_seq)) {
540 spxstat.spxs_rcvoopack++;
544 insque(si, q->si_prev);
546 * If this packet is urgent, inform process
548 if (si->si_cc & SPX_OB) {
549 cb->s_iobc = ((char *)si)[1 + sizeof(*si)];
551 cb->s_oobflags |= SF_IOOB;
554 #define SPINC sizeof(struct spxhdr)
556 * Loop through all packets queued up to update acknowledge
557 * number, and present all acknowledged data to user;
558 * If in packet interface mode, show packet headers.
560 for (q = cb->s_q.si_next; q != &cb->s_q; q = q->si_next) {
561 if (SI(q)->si_seq == cb->s_ack) {
564 if (SI(q)->si_cc & SPX_OB) {
565 cb->s_oobflags &= ~SF_IOOB;
566 if (so->so_rcv.sb_cc)
567 so->so_oobmark = so->so_rcv.sb_cc;
569 so->so_state |= SS_RCVATMARK;
574 spxstat.spxs_rcvpack++;
576 if (cb->s_flags2 & SF_NEWCALL) {
577 struct spxhdr *sp = mtod(m, struct spxhdr *);
578 u_char dt = sp->spx_dt;
580 if (dt != cb->s_rhdr.spx_dt) {
582 m_getclr(M_DONTWAIT, MT_CONTROL);
587 cb->s_rhdr.spx_dt = dt;
588 mm->m_len = 5; /*XXX*/
591 *(u_char *)(&s[2]) = dt;
592 sbappend(&so->so_rcv, mm);
595 if (sp->spx_cc & SPX_OB) {
596 MCHTYPE(m, MT_OOBDATA);
599 so->so_state &= ~SS_RCVATMARK;
604 m->m_pkthdr.len -= SPINC;
606 if ((sp->spx_cc & SPX_EM) || packetp) {
607 sbappendrecord(&so->so_rcv, m);
610 sbappend(&so->so_rcv, m);
614 sbappendrecord(&so->so_rcv, m);
616 cb->s_rhdr = *mtod(m, struct spxhdr *);
619 m->m_pkthdr.len -= SPINC;
620 sbappend(&so->so_rcv, m);
631 spx_ctlinput(cmd, arg_as_sa, dummy)
633 struct sockaddr *arg_as_sa; /* XXX should be swapped with dummy */
636 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
638 struct sockaddr_ipx *sipx;
640 if (cmd < 0 || cmd > PRC_NCMDS)
650 case PRC_HOSTUNREACH:
651 sipx = (struct sockaddr_ipx *)arg;
652 if (sipx->sipx_family != AF_IPX)
654 na = &sipx->sipx_addr;
667 struct spxpcb *cb = (struct spxpcb *)(ipxp->ipxp_pcb);
673 struct mbuf *firstbad, *m0;
677 * The notification that we have sent
678 * too much is bad news -- we will
679 * have to go through queued up so far
680 * splitting ones which are too big and
681 * reassigning sequence numbers and checksums.
682 * we should then retransmit all packets from
683 * one above the offending packet to the last one
684 * we had sent (or our allocation)
685 * then the offending one so that the any queued
686 * data at our destination will be discarded.
688 ep = (struct ipx_errp *)ipxp->ipxp_notify_param;
689 sb = &ipxp->ipxp_socket->so_snd;
690 cb->s_mtu = ep->ipx_err_param;
691 badseq = SI(&ep->ipx_err_ipx)->si_seq;
692 for (m = sb->sb_mb; m != NULL; m = m->m_act) {
693 si = mtod(m, struct spx *);
694 if (si->si_seq == badseq)
701 /* calculate length */
702 for (m0 = m, len = 0; m != NULL; m = m->m_next)
704 if (len > cb->s_mtu) {
717 struct socket *so = cb->s_ipxpcb->ipxp_socket;
719 struct spx *si = (struct spx *)NULL;
720 struct sockbuf *sb = &so->so_snd;
721 int len = 0, win, rcv_win;
722 short span, off, recordp = 0;
724 int error = 0, sendalot;
734 * Make sure that packet isn't too big.
736 for (m = m0; m != NULL; m = m->m_next) {
739 if (m->m_flags & M_EOR)
742 datalen = (cb->s_flags & SF_HO) ?
743 len - sizeof(struct spxhdr) : len;
745 if (cb->s_flags & SF_PI) {
749 int oldEM = cb->s_cc & SPX_EM;
754 * Here we are only being called
755 * from usrreq(), so it is OK to
758 m = m_copym(m0, 0, mtu, M_WAIT);
759 if (cb->s_flags & SF_NEWCALL) {
763 mm->m_flags &= ~M_EOR;
767 error = spx_output(cb, m);
780 * Force length even, by adding a "garbage byte" if
785 if (M_TRAILINGSPACE(m) >= 1)
788 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
795 *(mtod(m1, u_char *)) = 0;
799 m = m_gethdr(M_DONTWAIT, MT_HEADER);
805 * Fill in mbuf with extended SP header
806 * and addresses and length put into network format.
808 MH_ALIGN(m, sizeof(struct spx));
809 m->m_len = sizeof(struct spx);
811 si = mtod(m, struct spx *);
812 si->si_i = *cb->s_ipx;
813 si->si_s = cb->s_shdr;
814 if ((cb->s_flags & SF_PI) && (cb->s_flags & SF_HO)) {
816 if (m0->m_len < sizeof(*sh)) {
817 if((m0 = m_pullup(m0, sizeof(*sh))) == NULL) {
824 sh = mtod(m0, struct spxhdr *);
825 si->si_dt = sh->spx_dt;
826 si->si_cc |= sh->spx_cc & SPX_EM;
827 m0->m_len -= sizeof(*sh);
828 m0->m_data += sizeof(*sh);
832 if ((cb->s_flags2 & SF_NEWCALL) && recordp) {
836 if (cb->s_oobflags & SF_SOOB) {
839 * make sure OB packets convey exactly 1 byte.
840 * If the packet is 1 byte or larger, we
841 * have already guaranted there to be at least
842 * one garbage byte for the checksum, and
843 * extra bytes shouldn't hurt!
845 if (len > sizeof(*si)) {
847 len = (1 + sizeof(*si));
850 si->si_len = htons((u_short)len);
851 m->m_pkthdr.len = ((len - 1) | 1) + 1;
853 * queue stuff up for output
855 sbappendrecord(sb, m);
859 idle = (cb->s_smax == (cb->s_rack - 1));
863 off = cb->s_snxt - cb->s_rack;
864 win = min(cb->s_swnd, (cb->s_cwnd / CUNIT));
867 * If in persist timeout with window of 0, send a probe.
868 * Otherwise, if window is small but nonzero
869 * and timer expired, send what we can and go into
872 if (cb->s_force == 1 + SPXT_PERSIST) {
874 cb->s_timer[SPXT_PERSIST] = 0;
878 span = cb->s_seq - cb->s_rack;
879 len = min(span, win) - off;
883 * Window shrank after we went into it.
884 * If window shrank to 0, cancel pending
885 * restransmission and pull s_snxt back
886 * to (closed) window. We will enter persist
887 * state below. If the widndow didn't close completely,
888 * just wait for an ACK.
892 cb->s_timer[SPXT_REXMT] = 0;
893 cb->s_snxt = cb->s_rack;
898 rcv_win = sbspace(&so->so_rcv);
901 * Send if we owe peer an ACK.
903 if (cb->s_oobflags & SF_SOOB) {
905 * must transmit this out of band packet
907 cb->s_oobflags &= ~ SF_SOOB;
909 spxstat.spxs_sndurg++;
912 if (cb->s_flags & SF_ACKNOW)
914 if (cb->s_state < TCPS_ESTABLISHED)
917 * Silly window can't happen in spx.
918 * Code from tcp deleted.
923 * Compare available window to amount of window
924 * known to peer (as advertised window less
925 * next expected input.) If the difference is at least two
926 * packets or at least 35% of the mximum possible window,
927 * then want to send a window update to peer.
930 u_short delta = 1 + cb->s_alo - cb->s_ack;
931 int adv = rcv_win - (delta * cb->s_mtu);
933 if ((so->so_rcv.sb_cc == 0 && adv >= (2 * cb->s_mtu)) ||
934 (100 * adv / so->so_rcv.sb_hiwat >= 35)) {
935 spxstat.spxs_sndwinup++;
936 cb->s_flags |= SF_ACKNOW;
942 * Many comments from tcp_output.c are appropriate here
944 * If send window is too small, there is data to transmit, and no
945 * retransmit or persist is pending, then go to persist state.
946 * If nothing happens soon, send when timer expires:
947 * if window is nonzero, transmit what we can,
948 * otherwise send a probe.
950 if (so->so_snd.sb_cc && cb->s_timer[SPXT_REXMT] == 0 &&
951 cb->s_timer[SPXT_PERSIST] == 0) {
956 * No reason to send a packet, just return.
963 * Find requested packet.
967 cb->s_want = cb->s_snxt;
968 for (m = sb->sb_mb; m != NULL; m = m->m_act) {
969 si = mtod(m, struct spx *);
970 if (SSEQ_LEQ(cb->s_snxt, si->si_seq))
975 if (si->si_seq == cb->s_snxt)
978 spxstat.spxs_sndvoid++, si = 0;
986 alo = cb->s_ack - 1 + (rcv_win / ((short)cb->s_mtu));
987 if (SSEQ_LT(alo, cb->s_alo))
992 * must make a copy of this packet for
993 * ipx_output to monkey with
995 m = m_copy(dtom(si), 0, (int)M_COPYALL);
999 si = mtod(m, struct spx *);
1000 if (SSEQ_LT(si->si_seq, cb->s_smax))
1001 spxstat.spxs_sndrexmitpack++;
1003 spxstat.spxs_sndpack++;
1004 } else if (cb->s_force || cb->s_flags & SF_ACKNOW) {
1006 * Must send an acknowledgement or a probe
1009 spxstat.spxs_sndprobe++;
1010 if (cb->s_flags & SF_ACKNOW)
1011 spxstat.spxs_sndacks++;
1012 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1016 * Fill in mbuf with extended SP header
1017 * and addresses and length put into network format.
1019 MH_ALIGN(m, sizeof(struct spx));
1020 m->m_len = sizeof(*si);
1021 m->m_pkthdr.len = sizeof(*si);
1022 si = mtod(m, struct spx *);
1023 si->si_i = *cb->s_ipx;
1024 si->si_s = cb->s_shdr;
1025 si->si_seq = cb->s_smax + 1;
1026 si->si_len = htons(sizeof(*si));
1027 si->si_cc |= SPX_SP;
1030 if (so->so_options & SO_DEBUG || traceallspxs)
1031 spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
1035 * Stuff checksum and output datagram.
1037 if ((si->si_cc & SPX_SP) == 0) {
1038 if (cb->s_force != (1 + SPXT_PERSIST) ||
1039 cb->s_timer[SPXT_PERSIST] == 0) {
1041 * If this is a new packet and we are not currently
1042 * timing anything, time this one.
1044 if (SSEQ_LT(cb->s_smax, si->si_seq)) {
1045 cb->s_smax = si->si_seq;
1046 if (cb->s_rtt == 0) {
1047 spxstat.spxs_segstimed++;
1048 cb->s_rtseq = si->si_seq;
1053 * Set rexmt timer if not currently set,
1054 * Initial value for retransmit timer is smoothed
1055 * round-trip time + 2 * round-trip time variance.
1056 * Initialize shift counter which is used for backoff
1057 * of retransmit time.
1059 if (cb->s_timer[SPXT_REXMT] == 0 &&
1060 cb->s_snxt != cb->s_rack) {
1061 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1062 if (cb->s_timer[SPXT_PERSIST]) {
1063 cb->s_timer[SPXT_PERSIST] = 0;
1067 } else if (SSEQ_LT(cb->s_smax, si->si_seq)) {
1068 cb->s_smax = si->si_seq;
1070 } else if (cb->s_state < TCPS_ESTABLISHED) {
1072 cb->s_rtt = 1; /* Time initial handshake */
1073 if (cb->s_timer[SPXT_REXMT] == 0)
1074 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1078 * Do not request acks when we ack their data packets or
1079 * when we do a gratuitous window update.
1081 if (((si->si_cc & SPX_SP) == 0) || cb->s_force)
1082 si->si_cc |= SPX_SA;
1083 si->si_seq = htons(si->si_seq);
1084 si->si_alo = htons(alo);
1085 si->si_ack = htons(cb->s_ack);
1088 si->si_sum = ipx_cksum(m, ntohs(si->si_len));
1090 si->si_sum = 0xffff;
1093 if (so->so_options & SO_DEBUG || traceallspxs)
1094 spx_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
1096 if (so->so_options & SO_DONTROUTE)
1097 error = ipx_outputfl(m, (struct route *)NULL, IPX_ROUTETOIF);
1099 error = ipx_outputfl(m, &cb->s_ipxpcb->ipxp_route, 0);
1104 spxstat.spxs_sndtotal++;
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.
1112 cb->s_flags &= ~(SF_ACKNOW|SF_DELACK);
1113 if (SSEQ_GT(alo, cb->s_alo))
1121 static int spx_do_persist_panics = 0;
1127 int t = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
1129 if (cb->s_timer[SPXT_REXMT] && spx_do_persist_panics)
1130 panic("spx_output REXMT");
1132 * Start/restart persistance timer.
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)
1142 spx_ctloutput(so, sopt)
1144 struct sockopt *sopt;
1146 struct ipxpcb *ipxp = sotoipxpcb(so);
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));
1163 cb = ipxtospxpcb(ipxp);
1165 switch (sopt->sopt_dir) {
1167 switch (sopt->sopt_name) {
1168 case SO_HEADERS_ON_INPUT:
1172 case SO_HEADERS_ON_OUTPUT:
1175 soptval = cb->s_flags & mask;
1176 error = sooptcopyout(sopt, &soptval, sizeof soptval);
1180 usoptval = cb->s_mtu;
1181 error = sooptcopyout(sopt, &usoptval, sizeof usoptval);
1184 case SO_LAST_HEADER:
1185 error = sooptcopyout(sopt, &cb->s_rhdr,
1189 case SO_DEFAULT_HEADERS:
1190 error = sooptcopyout(sopt, &cb->s_shdr,
1195 error = ENOPROTOOPT;
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:
1207 case SO_HEADERS_ON_OUTPUT:
1210 error = sooptcopyin(sopt, &optval, sizeof optval,
1215 if (cb->s_flags & SF_PI) {
1217 cb->s_flags |= mask;
1219 cb->s_flags &= ~mask;
1220 } else error = EINVAL;
1224 error = sooptcopyin(sopt, &usoptval, sizeof usoptval,
1228 cb->s_mtu = usoptval;
1233 error = sooptcopyin(sopt, &optval, sizeof optval,
1238 cb->s_flags2 |= SF_NEWCALL;
1241 cb->s_flags2 &= ~SF_NEWCALL;
1247 case SO_DEFAULT_HEADERS:
1251 error = sooptcopyin(sopt, &sp, sizeof sp,
1255 cb->s_dt = sp.spx_dt;
1256 cb->s_cc = sp.spx_cc & SPX_EM;
1261 error = ENOPROTOOPT;
1273 struct ipxpcb *ipxp;
1276 ipxp = sotoipxpcb(so);
1277 cb = ipxtospxpcb(ipxp);
1280 spx_drop(cb, ECONNABORTED);
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.
1293 struct sockaddr **nam;
1295 struct ipxpcb *ipxp;
1296 struct sockaddr_ipx *sipx, ssipx;
1298 ipxp = sotoipxpcb(so);
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);
1309 spx_attach(struct socket *so, int proto, struct pru_attach_info *ai)
1313 struct ipxpcb *ipxp;
1318 ipxp = sotoipxpcb(so);
1319 cb = ipxtospxpcb(ipxp);
1324 error = ipx_pcballoc(so, &ipxpcb);
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,
1331 goto spx_attach_end;
1333 ipxp = sotoipxpcb(so);
1335 MALLOC(cb, struct spxpcb *, sizeof *cb, M_PCB, M_INTWAIT | M_ZERO);
1338 mm = m_getclr(M_DONTWAIT, MT_HEADER);
1342 goto spx_attach_end;
1344 cb->s_ipx = mtod(mm, struct ipx *);
1345 cb->s_state = TCPS_LISTEN;
1348 cb->s_q.si_next = cb->s_q.si_prev = &cb->s_q;
1349 cb->s_ipxpcb = ipxp;
1350 cb->s_mtu = 576 - sizeof(struct spx);
1351 cb->s_cwnd = sbspace(sb) * CUNIT / cb->s_mtu;
1352 cb->s_ssthresh = cb->s_cwnd;
1353 cb->s_cwmx = sbspace(sb) * CUNIT / (2 * sizeof(struct spx));
1354 /* Above is recomputed when connecting to account
1355 for changed buffering or mtu's */
1356 cb->s_rtt = SPXTV_SRTTBASE;
1357 cb->s_rttvar = SPXTV_SRTTDFLT << 2;
1358 SPXT_RANGESET(cb->s_rxtcur,
1359 ((SPXTV_SRTTBASE >> 2) + (SPXTV_SRTTDFLT << 2)) >> 1,
1360 SPXTV_MIN, SPXTV_REXMTMAX);
1361 ipxp->ipxp_pcb = (caddr_t)cb;
1368 spx_bind(so, nam, td)
1370 struct sockaddr *nam;
1373 struct ipxpcb *ipxp;
1375 ipxp = sotoipxpcb(so);
1377 return (ipx_pcbbind(ipxp, nam, td));
1381 * Initiate connection to peer.
1382 * Enter SYN_SENT state, and mark socket as connecting.
1383 * Start keep-alive timer, setup prototype header,
1384 * Send initial system packet requesting connection.
1387 spx_connect(so, nam, td)
1389 struct sockaddr *nam;
1394 struct ipxpcb *ipxp;
1397 ipxp = sotoipxpcb(so);
1398 cb = ipxtospxpcb(ipxp);
1401 if (ipxp->ipxp_lport == 0) {
1402 error = ipx_pcbbind(ipxp, (struct sockaddr *)NULL, td);
1404 goto spx_connect_end;
1406 error = ipx_pcbconnect(ipxp, nam, td);
1408 goto spx_connect_end;
1410 spxstat.spxs_connattempt++;
1411 cb->s_state = TCPS_SYN_SENT;
1414 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
1415 cb->s_force = 1 + SPXTV_KEEP;
1417 * Other party is required to respond to
1418 * the port I send from, but he is not
1419 * required to answer from where I am sending to,
1420 * so allow wildcarding.
1421 * original port I am sending to is still saved in
1424 ipxp->ipxp_fport = 0;
1425 error = spx_output(cb, (struct mbuf *)NULL);
1436 struct ipxpcb *ipxp;
1439 ipxp = sotoipxpcb(so);
1440 cb = ipxtospxpcb(ipxp);
1445 if (cb->s_state > TCPS_LISTEN)
1454 * We may decide later to implement connection closing
1455 * handshaking at the spx level optionally.
1456 * here is the hook to do it:
1459 spx_usr_disconnect(so)
1463 struct ipxpcb *ipxp;
1466 ipxp = sotoipxpcb(so);
1467 cb = ipxtospxpcb(ipxp);
1481 struct ipxpcb *ipxp;
1485 ipxp = sotoipxpcb(so);
1486 cb = ipxtospxpcb(ipxp);
1488 if (ipxp->ipxp_lport == 0)
1489 error = ipx_pcbbind(ipxp, (struct sockaddr *)NULL, td);
1491 cb->s_state = TCPS_LISTEN;
1496 * After a receive, possibly send acknowledgment
1497 * updating allocation.
1505 struct ipxpcb *ipxp;
1508 ipxp = sotoipxpcb(so);
1509 cb = ipxtospxpcb(ipxp);
1512 cb->s_flags |= SF_RVD;
1513 spx_output(cb, (struct mbuf *)NULL);
1514 cb->s_flags &= ~SF_RVD;
1520 spx_rcvoob(so, m, flags)
1525 struct ipxpcb *ipxp;
1528 ipxp = sotoipxpcb(so);
1529 cb = ipxtospxpcb(ipxp);
1531 if ((cb->s_oobflags & SF_IOOB) || so->so_oobmark ||
1532 (so->so_state & SS_RCVATMARK)) {
1534 *mtod(m, caddr_t) = cb->s_iobc;
1541 spx_send(so, flags, m, addr, controlp, td)
1545 struct sockaddr *addr;
1546 struct mbuf *controlp;
1551 struct ipxpcb *ipxp;
1555 ipxp = sotoipxpcb(so);
1556 cb = ipxtospxpcb(ipxp);
1559 if (flags & PRUS_OOB) {
1560 if (sbspace(&so->so_snd) < -512) {
1564 cb->s_oobflags |= SF_SOOB;
1566 if (controlp != NULL) {
1567 u_short *p = mtod(controlp, u_short *);
1569 if ((p[0] == 5) && (p[1] == 1)) { /* XXXX, for testing */
1570 cb->s_shdr.spx_dt = *(u_char *)(&p[2]);
1576 error = spx_output(cb, m);
1579 if (controlp != NULL)
1593 struct ipxpcb *ipxp;
1597 ipxp = sotoipxpcb(so);
1598 cb = ipxtospxpcb(ipxp);
1602 cb = spx_usrclosed(cb);
1604 error = spx_output(cb, (struct mbuf *)NULL);
1610 spx_sp_attach(so, proto, ai)
1613 struct pru_attach_info *ai;
1616 struct ipxpcb *ipxp;
1618 error = spx_attach(so, proto, ai);
1620 ipxp = sotoipxpcb(so);
1621 ((struct spxpcb *)ipxp->ipxp_pcb)->s_flags |=
1622 (SF_HI | SF_HO | SF_PI);
1628 * Create template to be used to send spx packets on a connection.
1629 * Called after host entry created, fills
1630 * in a skeletal spx header (choosing connection id),
1631 * minimizing the amount of work necessary when the connection is used.
1637 struct ipxpcb *ipxp = cb->s_ipxpcb;
1638 struct ipx *ipx = cb->s_ipx;
1639 struct sockbuf *sb = &(ipxp->ipxp_socket->so_snd);
1641 ipx->ipx_pt = IPXPROTO_SPX;
1642 ipx->ipx_sna = ipxp->ipxp_laddr;
1643 ipx->ipx_dna = ipxp->ipxp_faddr;
1644 cb->s_sid = htons(spx_iss);
1645 spx_iss += SPX_ISSINCR/2;
1647 cb->s_cwnd = (sbspace(sb) * CUNIT) / cb->s_mtu;
1648 cb->s_ssthresh = cb->s_cwnd; /* Try to expand fast to full complement
1650 cb->s_cwmx = (sbspace(sb) * CUNIT) / (2 * sizeof(struct spx));
1651 cb->s_cwmx = max(cb->s_cwmx, cb->s_cwnd);
1652 /* But allow for lots of little packets as well */
1656 * Close a SPIP control block:
1657 * discard spx control block itself
1658 * discard ipx protocol control block
1659 * wake up any sleepers
1661 static struct spxpcb *
1666 struct ipxpcb *ipxp = cb->s_ipxpcb;
1667 struct socket *so = ipxp->ipxp_socket;
1670 s = cb->s_q.si_next;
1671 while (s != &(cb->s_q)) {
1673 m = dtom(s->si_prev);
1677 m_free(dtom(cb->s_ipx));
1680 soisdisconnected(so);
1681 ipx_pcbdetach(ipxp);
1682 spxstat.spxs_closed++;
1683 return ((struct spxpcb *)NULL);
1687 * Someday we may do level 3 handshaking
1688 * to close a connection or send a xerox style error.
1689 * For now, just close.
1691 static struct spxpcb *
1695 return (spx_close(cb));
1698 static struct spxpcb *
1702 return (spx_close(cb));
1706 * Drop connection, reporting
1707 * the specified error.
1709 static struct spxpcb *
1714 struct socket *so = cb->s_ipxpcb->ipxp_socket;
1717 * someday, in the xerox world
1718 * we will generate error protocol packets
1719 * announcing that the socket has gone away.
1721 if (TCPS_HAVERCVDSYN(cb->s_state)) {
1722 spxstat.spxs_drops++;
1723 cb->s_state = TCPS_CLOSED;
1726 spxstat.spxs_conndrops++;
1727 so->so_error = errno;
1728 return (spx_close(cb));
1732 * Fast timeout routine for processing delayed acks
1737 struct ipxpcb *ipxp;
1741 ipxp = ipxpcb.ipxp_next;
1743 for (; ipxp != &ipxpcb; ipxp = ipxp->ipxp_next)
1744 if ((cb = (struct spxpcb *)ipxp->ipxp_pcb) != NULL &&
1745 (cb->s_flags & SF_DELACK)) {
1746 cb->s_flags &= ~SF_DELACK;
1747 cb->s_flags |= SF_ACKNOW;
1748 spxstat.spxs_delack++;
1749 spx_output(cb, (struct mbuf *)NULL);
1755 * spx protocol timeout routine called every 500 ms.
1756 * Updates the timers in all active pcb's and
1757 * causes finite state machine actions if timers expire.
1762 struct ipxpcb *ip, *ipnxt;
1768 * Search through tcb's and update active timers.
1770 ip = ipxpcb.ipxp_next;
1775 while (ip != &ipxpcb) {
1776 cb = ipxtospxpcb(ip);
1777 ipnxt = ip->ipxp_next;
1780 for (i = 0; i < SPXT_NTIMERS; i++) {
1781 if (cb->s_timer[i] && --cb->s_timer[i] == 0) {
1783 if (ipnxt->ipxp_prev != ip)
1793 spx_iss += SPX_ISSINCR/PR_SLOWHZ; /* increment iss */
1798 * SPX timer processing.
1800 static struct spxpcb *
1801 spx_timers(cb, timer)
1808 cb->s_force = 1 + timer;
1812 * 2 MSL timeout in shutdown went off. TCP deletes connection
1816 printf("spx: SPXT_2MSL went off for no reason\n");
1817 cb->s_timer[timer] = 0;
1821 * Retransmission timer went off. Message has not
1822 * been acked within retransmit interval. Back off
1823 * to a longer retransmit interval and retransmit one packet.
1826 if (++cb->s_rxtshift > SPX_MAXRXTSHIFT) {
1827 cb->s_rxtshift = SPX_MAXRXTSHIFT;
1828 spxstat.spxs_timeoutdrop++;
1829 cb = spx_drop(cb, ETIMEDOUT);
1832 spxstat.spxs_rexmttimeo++;
1833 rexmt = ((cb->s_srtt >> 2) + cb->s_rttvar) >> 1;
1834 rexmt *= spx_backoff[cb->s_rxtshift];
1835 SPXT_RANGESET(cb->s_rxtcur, rexmt, SPXTV_MIN, SPXTV_REXMTMAX);
1836 cb->s_timer[SPXT_REXMT] = cb->s_rxtcur;
1838 * If we have backed off fairly far, our srtt
1839 * estimate is probably bogus. Clobber it
1840 * so we'll take the next rtt measurement as our srtt;
1841 * move the current srtt into rttvar to keep the current
1842 * retransmit times until then.
1844 if (cb->s_rxtshift > SPX_MAXRXTSHIFT / 4 ) {
1845 cb->s_rttvar += (cb->s_srtt >> 2);
1848 cb->s_snxt = cb->s_rack;
1850 * If timing a packet, stop the timer.
1854 * See very long discussion in tcp_timer.c about congestion
1855 * window and sstrhesh
1857 win = min(cb->s_swnd, (cb->s_cwnd/CUNIT)) / 2;
1861 cb->s_ssthresh = win * CUNIT;
1862 spx_output(cb, (struct mbuf *)NULL);
1866 * Persistance timer into zero window.
1867 * Force a probe to be sent.
1870 spxstat.spxs_persisttimeo++;
1872 spx_output(cb, (struct mbuf *)NULL);
1876 * Keep-alive timer went off; send something
1877 * or drop connection if idle for too long.
1880 spxstat.spxs_keeptimeo++;
1881 if (cb->s_state < TCPS_ESTABLISHED)
1883 if (cb->s_ipxpcb->ipxp_socket->so_options & SO_KEEPALIVE) {
1884 if (cb->s_idle >= SPXTV_MAXIDLE)
1886 spxstat.spxs_keepprobe++;
1887 spx_output(cb, (struct mbuf *)NULL);
1890 cb->s_timer[SPXT_KEEP] = SPXTV_KEEP;
1893 spxstat.spxs_keepdrops++;
1894 cb = spx_drop(cb, ETIMEDOUT);