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