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