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