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