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