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