replaced tcpexprexmtbackoff with sppexprexmtbackoff for bbn net code
[unix-history] / usr / src / sys / netns / spp_usrreq.c
CommitLineData
8ae0e4b4
KM
1/*
2 * Copyright (c) 1982 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 *
a5c5bc2d 6 * @(#)spp_usrreq.c 6.10 (Berkeley) %G%
8ae0e4b4 7 */
4258e601
KS
8
9#include "param.h"
10#include "dir.h"
11#include "user.h"
12#include "mbuf.h"
13#include "protosw.h"
14#include "socket.h"
15#include "socketvar.h"
16#include "errno.h"
17
18#include "../net/if.h"
19#include "../net/route.h"
20#include "../netinet/tcp_fsm.h"
21#include "../netinet/tcp_timer.h"
22
23#include "ns.h"
24#include "ns_pcb.h"
25#include "idp.h"
26#include "idp_var.h"
27#include "ns_error.h"
28#include "sp.h"
29#include "spidp.h"
30#include "spp_var.h"
31#include "spp_debug.h"
32
33/*
34 * SP protocol implementation.
35 */
36spp_init()
37{
38
39 spp_iss = 1; /* WRONG !! should fish it out of TODR */
40}
41struct spidp spp_savesi;
42int traceallspps = 0;
43extern int sppconsdebug;
44
45int spp_hardnosed;
e3c149da 46spp_input(m, nsp)
4258e601 47 register struct mbuf *m;
fe594349 48 register struct nspcb *nsp;
e3c149da 49{
4258e601
KS
50 register struct sppcb *cb;
51 register struct spidp *si = mtod(m, struct spidp *);
52 register struct socket *so;
f97be0c9 53 short ostate;
4258e601
KS
54 int dropsocket = 0;
55
56
99e0091e 57 if (nsp == 0) {
e3c149da 58 panic("No nspcb in spp_input\n");
fe594349
KS
59 return;
60 }
4258e601
KS
61
62 cb = nstosppcb(nsp);
63 if (cb == 0) goto bad;
64
e3c149da 65 if (m->m_len < sizeof(*si)) {
99e0091e 66 if ((m = m_pullup(m, sizeof(*si))) == 0) {
e3c149da
KS
67 spp_istat.hdrops++;
68 return;
69 }
70 si = mtod(m, struct spidp *);
71 }
4258e601
KS
72 si->si_seq = ntohs(si->si_seq);
73 si->si_ack = ntohs(si->si_ack);
74 si->si_alo = ntohs(si->si_alo);
75
76 so = nsp->nsp_socket;
77 if (so->so_options & SO_DEBUG || traceallspps) {
78 ostate = cb->s_state;
79 spp_savesi = *si;
80 }
81 if (so->so_options & SO_ACCEPTCONN) {
82 so = sonewconn(so);
83 if (so == 0) {
84 spp_istat.nonucn++;
85 goto drop;
86 }
87 /*
88 * This is ugly, but ....
89 *
90 * Mark socket as temporary until we're
91 * committed to keeping it. The code at
92 * ``drop'' and ``dropwithreset'' check the
93 * flag dropsocket to see if the temporary
94 * socket created here should be discarded.
95 * We mark the socket as discardable until
96 * we're committed to it below in TCPS_LISTEN.
97 */
98 dropsocket++;
99 nsp = (struct nspcb *)so->so_pcb;
100 nsp->nsp_laddr = si->si_dna;
101 cb = nstosppcb(nsp);
102 cb->s_state = TCPS_LISTEN;
103 }
104
105 /*
106 * Packet received on connection.
107 * reset idle time and keep-alive timer;
108 */
109 cb->s_idle = 0;
110 cb->s_timer[TCPT_KEEP] = TCPTV_KEEP;
111
112 switch (cb->s_state) {
9ad31c34 113
4258e601
KS
114 case TCPS_LISTEN:{
115 struct mbuf *am;
116 register struct sockaddr_ns *sns;
117 struct ns_addr laddr;
118
119 /*
120 * If somebody here was carying on a conversation
121 * and went away, and his pen pal thinks he can
122 * still talk, we get the misdirected packet.
123 */
124 if (spp_hardnosed && (si->si_did != 0 || si->si_seq != 0)) {
125 spp_istat.gonawy++;
126 goto dropwithreset;
127 }
128 am = m_get(M_DONTWAIT, MT_SONAME);
129 if (am == NULL)
130 goto drop;
131 am->m_len = sizeof (struct sockaddr_ns);
132 sns = mtod(am, struct sockaddr_ns *);
133 sns->sns_family = AF_NS;
134 sns->sns_addr = si->si_sna;
135 laddr = nsp->nsp_laddr;
136 if (ns_nullhost(laddr))
137 nsp->nsp_laddr = si->si_dna;
138 if (ns_pcbconnect(nsp, am)) {
139 nsp->nsp_laddr = laddr;
140 (void) m_free(am);
141 spp_istat.noconn++;
142 goto drop;
143 }
144 (void) m_free(am);
145 cb->s_state = TCPS_SYN_RECEIVED;
146 spp_template(cb);
147 cb->s_did = si->si_sid;
148 cb->s_rack = si->si_ack;
149 cb->s_ralo = si->si_alo;
150 cb->s_flags |= SF_AK;
151 cb->s_timer[TCPT_KEEP] = TCPTV_KEEP;
152 dropsocket = 0; /* committed to socket */
153 }
154 break;
155
156 /*
157 * This state means that we have gotten a response
158 * to our attempt to establish a connection.
fe594349
KS
159 * We fill in the data from the other side,
160 * telling us which port to respond to, instead of the well-
161 * known one we might have sent to in the first place.
4258e601 162 * We also require that this is a response to our
fe594349 163 * connection id.
4258e601
KS
164 */
165 case TCPS_SYN_SENT:
166 if (si->si_did!=cb->s_sid) {
167 spp_istat.notme++;
168 goto drop;
169 }
170 cb->s_did = si->si_sid;
171 cb->s_rack = si->si_ack;
172 cb->s_ralo = si->si_alo;
173 cb->s_dport = nsp->nsp_fport = si->si_sport;
174 cb->s_timer[TCPT_REXMT] = 0;
175 cb->s_flags |= SF_AK;
176 soisconnected(so);
177 cb->s_state = TCPS_ESTABLISHED;
178 break;
179 /*
180 * This state means that we have heard a response
181 * to our acceptance of their connection
182 * It is probably logically unnecessary in this
183 * implementation.
184 */
185 case TCPS_SYN_RECEIVED:
186 if (si->si_did!=cb->s_sid) {
187 spp_istat.wrncon++;
188 goto drop;
189 }
190 nsp->nsp_fport = si->si_sport;
191 cb->s_timer[TCPT_REXMT] = 0;
192 cb->s_timer[TCPT_KEEP] = TCPTV_KEEP;
193 soisconnected(so);
194 cb->s_state = TCPS_ESTABLISHED;
195 }
196 if (so->so_options & SO_DEBUG || traceallspps)
f97be0c9 197 spp_trace(SA_INPUT, (u_char)ostate, cb, &spp_savesi, 0);
4258e601
KS
198
199 m->m_len -= sizeof (struct idp);
200 m->m_off += sizeof (struct idp);
201
202 if (spp_reass(cb,si)) {
4258e601
KS
203 goto drop;
204 }
f97be0c9 205 (void) spp_output(cb,(struct mbuf *)0);
4258e601
KS
206 return;
207
208dropwithreset:
209 if (dropsocket)
210 (void) soabort(so);
211 si->si_seq = ntohs(si->si_seq);
212 si->si_ack = ntohs(si->si_ack);
213 si->si_alo = ntohs(si->si_alo);
214 ns_error(dtom(si), NS_ERR_NOSOCK, 0);
215 if (cb->s_nspcb->nsp_socket->so_options & SO_DEBUG || traceallspps)
f97be0c9 216 spp_trace(SA_DROP, (u_char)ostate, cb, &spp_savesi, 0);
4258e601
KS
217 return;
218
219drop:
220bad:
99e0091e 221 if (cb == 0 || cb->s_nspcb->nsp_socket->so_options & SO_DEBUG || traceallspps)
f97be0c9 222 spp_trace(SA_DROP, (u_char)ostate, cb, &spp_savesi, 0);
4258e601
KS
223 m_freem(m);
224}
225
226/*
227 * This is structurally similar to the tcp reassembly routine
228 * but its function is somewhat different: It merely queues
229 * packets up, and suppresses duplicates.
230 */
231spp_reass(cb,si)
232register struct sppcb *cb;
233register struct spidp *si;
234{
235 register struct spidp_q *q;
236 register struct mbuf *m;
237 struct socket *so = cb->s_nspcb->nsp_socket;
238 struct sockbuf *sb = & (so->so_rcv);
239 char packetp = cb->s_flags & SF_HI;
240 char wakeup = 0;
241
242
99e0091e 243 if (si == SI(0))
4258e601
KS
244 goto present;
245 /*
246 * Update our news from them.
247 */
248 if (si->si_cc & SP_SA)
249 cb->s_flags |= SF_DELACK;
250 if (SSEQ_GT(si->si_ack,cb->s_rack)) {
251 cb->s_rack = si->si_ack;
252 cb->s_timer[TCPT_REXMT] = 0;
253
254 /*
255 * If transmit timer is running and timed sequence
256 * number was acked, update smoothed round trip time.
257 */
258 if (cb->s_rtt && SSEQ_GT(si->si_ack, cb->s_rtseq)) {
259 if (cb->s_srtt == 0)
260 cb->s_srtt = cb->s_rtt;
261 else
262 cb->s_srtt =
263 tcp_alpha * cb->s_srtt +
264 (1 - tcp_alpha) * cb->s_rtt;
265 cb->s_rtt = 0;
266 }
267 }
268 if (SSEQ_GT(si->si_alo,cb->s_ralo)) {
269 cb->s_ralo = si->si_alo;
270 cb->s_timer[TCPT_PERSIST] = 0;
271 }
272 /*
273 * If this is a system packet, we don't need to
274 * queue it up, and won't update acknowledge #
275 */
ca1bb209
KS
276 if (si->si_cc & SP_SP) {
277 m_freem(dtom(si));
9ad31c34 278 return (0);
ca1bb209 279 }
4258e601
KS
280
281 /*
282 * If this packet number has a sequence number less
283 * than that of the first packet not yet seen coming
284 * from them, this must be a duplicate, so drop.
285 */
fe594349
KS
286 if (SSEQ_LT(si->si_seq,cb->s_ack)) {
287 spp_istat.bdreas++;
99e0091e 288 if (si->si_seq == cb->s_ack-1)
fe594349 289 spp_istat.lstdup++;
9ad31c34 290 return (1);
fe594349 291 }
4258e601
KS
292 /*
293 * If this packet number is higher than that which
294 * we have allocated refuse it, unless urgent
295 */
296 if (SSEQ_GT(si->si_seq,cb->s_alo) && (!(si->si_cc & SP_OB))) {
fe594349 297 spp_istat.notyet++;
9ad31c34 298 return (1);
4258e601
KS
299 }
300 /*
301 * If this packet is urgent, inform process
302 */
303 if (si->si_cc & SP_OB) {
304 cb->s_iobc = ((char *)si)[1 + sizeof(*si)];
305 sohasoutofband(so);
306 }
307
308 /*
309 * Loop through all packets queued up to insert in
310 * appropriate sequence.
311 */
312
313 for (q = cb->s_q.si_next; q!=&cb->s_q; q = q->si_next) {
99e0091e 314 if (si->si_seq == SI(q)->si_seq) return (1); /*duplicate */
4258e601
KS
315 if (SSEQ_LT(si->si_seq,SI(q)->si_seq)) break;
316 }
317 insque(si,q->si_prev);
318
319present:
320#define SPINC sizeof(struct sphdr)
321 /*
322 * Loop through all packets queued up to update acknowledge
323 * number, and present all acknowledged data to user;
324 * If in packet interface mode, show packet headers.
325 */
326 for (q = cb->s_q.si_next; q!=&cb->s_q; q = q->si_next) {
99e0091e 327 if (SI(q)->si_seq == cb->s_ack) {
4258e601
KS
328 cb->s_ack++;
329 m = dtom(q);
330 if (SI(q)->si_cc & SP_OB) {
331 if (sb->sb_cc)
332 so->so_oobmark = sb->sb_cc;
333 else
334 so->so_state |= SS_RCVATMARK;
335 }
336 q = q->si_prev;
337 remque(q->si_next);
338 wakeup = 1;
339 if (packetp) {
340 sbappendrecord(sb,m);
341 } else {
342 cb->s_rhdr = *mtod(m, struct sphdr *);
343 m->m_off += SPINC;
344 m->m_len -= SPINC;
345 sbappend(sb,m);
346 }
347 } else
348 break;
349 }
350 if (wakeup) sorwakeup(so);
9ad31c34 351 return (0);
4258e601
KS
352}
353
354spp_ctlinput(cmd, arg)
355 int cmd;
356 caddr_t arg;
357{
358 struct ns_addr *na;
359 extern u_char nsctlerrmap[];
360 extern spp_abort();
f97be0c9 361 extern struct nspcb *idp_drop();
fe594349
KS
362 struct ns_errp *errp;
363 struct nspcb *nsp;
4258e601
KS
364 int type;
365
366 if (cmd < 0 || cmd > PRC_NCMDS)
367 return;
368 type = NS_ERR_UNREACH_HOST;
369
370 switch (cmd) {
9ad31c34 371
4258e601
KS
372 case PRC_ROUTEDEAD:
373 case PRC_QUENCH:
374 break;
375
376 case PRC_IFDOWN:
377 na = &((struct sockaddr_ns *)arg)->sns_addr;
378 break;
379
380 case PRC_HOSTDEAD:
381 case PRC_HOSTUNREACH:
382 na = (struct ns_addr *)arg;
383 break;
384
385 default:
fe594349
KS
386 errp = (struct ns_errp *)arg;
387 na = &errp->ns_err_idp.idp_dna;
388 type = errp->ns_err_num;
f97be0c9 389 type = ntohs((u_short)type);
4258e601
KS
390 }
391 switch (type) {
9ad31c34 392
4258e601 393 case NS_ERR_UNREACH_HOST:
fe594349 394 ns_pcbnotify(na, (int)nsctlerrmap[cmd], spp_abort, (long) 0);
4258e601 395 break;
9ad31c34 396
4258e601 397 case NS_ERR_TOO_BIG:
fe594349
KS
398 case NS_ERR_NOSOCK:
399 nsp = ns_pcblookup(na, errp->ns_err_idp.idp_sna.x_port,
400 NS_WILDCARD);
401 if (nsp) {
402 if(nsp->nsp_pcb)
f97be0c9
KS
403 (void) spp_drop((struct sppcb *)nsp->nsp_pcb,
404 (int)nsctlerrmap[cmd]);
fe594349 405 else
f97be0c9 406 (void) idp_drop(nsp, (int)nsctlerrmap[cmd]);
fe594349 407 }
4258e601
KS
408 }
409}
410
f97be0c9 411#ifdef notdef
4258e601
KS
412int
413spp_fixmtu(nsp)
414register struct nspcb *nsp;
415{
416 register struct sppcb *cb = (struct sppcb *)(nsp->nsp_pcb);
417 register struct mbuf *m;
418 register struct spidp *si;
419 struct ns_errp *ep;
420 struct sockbuf *sb;
421 int badseq, len;
422 struct mbuf *firstbad, *m0;
423
424 if (cb) {
425 /*
426 * The notification that we have sent
427 * too much is bad news -- we will
428 * have to go through queued up so far
429 * splitting ones which are too big and
430 * reassigning sequence numbers and checksums.
431 * we should then retransmit all packets from
432 * one above the offending packet to the last one
433 * we had sent (or our allocation)
434 * then the offending one so that the any queued
435 * data at our destination will be discarded.
436 */
437 ep = (struct ns_errp *)nsp->nsp_notify_param;
438 sb = &nsp->nsp_socket->so_snd;
439 cb->s_mtu = ep->ns_err_param;
440 badseq = SI(&ep->ns_err_idp)->si_seq;
441 for (m = sb->sb_mb; m; m = m->m_act) {
442 si = mtod(m, struct spidp *);
443 if (si->si_seq == badseq)
444 break;
445 }
99e0091e 446 if (m == 0) return;
4258e601
KS
447 firstbad = m;
448 /*for (;;) {*/
449 /* calculate length */
450 for (m0 = m, len = 0; m ; m = m->m_next)
451 len += m->m_len;
452 if (len > cb->s_mtu) {
453 }
454 /* FINISH THIS
455 } */
456 }
457}
f97be0c9 458#endif
4258e601
KS
459
460int spp_output_cnt = 0;
fe594349 461
4258e601
KS
462spp_output(cb, m0)
463 register struct sppcb *cb;
464 struct mbuf *m0;
465{
466 struct socket *so = cb->s_nspcb->nsp_socket;
467 register struct mbuf *m;
468 register struct spidp *si = (struct spidp *) 0;
469 register struct sockbuf *sb = &(so->so_snd);
470 register int len = 0;
f97be0c9 471 int mtu = cb->s_mtu;
99e0091e
KS
472 int error = 0;
473 u_short lookfor = 0;
4258e601
KS
474 struct mbuf *mprev;
475 extern int idpcksum;
476
99e0091e 477 if (m0) {
4258e601
KS
478 for (m = m0; m ; m = m->m_next) {
479 mprev = m;
480 len += m->m_len;
481 }
482 if (len > mtu) {
483 if (cb->s_flags & SF_PI) {
484 m_freem(m0);
9ad31c34 485 return (EMSGSIZE);
4258e601
KS
486 } else {
487 int off = 0;
488 while (len > mtu) {
489 m = m_copy(m0, off, mtu);
99e0091e 490 if (m == NULL) {
9ad31c34
KS
491 m_freem(m0);
492 return (ENOBUFS);
493 }
4258e601
KS
494 error = spp_output(cb, m);
495 if (error) {
496 m_freem(m0);
9ad31c34 497 return (error);
4258e601
KS
498 }
499 m_adj(m0, mtu);
500 len -= mtu;
501 }
502 }
503 }
99e0091e
KS
504 /*
505 * Force length even, by adding a "garbage byte" if
506 * necessary.
507 */
4258e601 508 if (len & 1) {
fe594349 509 m = mprev;
99e0091e 510 if (m->m_len + m->m_off < MMAXOFF)
4258e601 511 m->m_len++;
99e0091e 512 else {
4258e601
KS
513 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
514
515 if (m1 == 0) {
516 m_freem(m0);
517 return (ENOBUFS);
518 }
519 m1->m_len = 1;
520 m1->m_off = MMAXOFF - 1;
99e0091e 521 m->m_next = m1;
4258e601
KS
522 }
523 }
524 m = m_get(M_DONTWAIT, MT_HEADER);
525 if (m == 0) {
526 m_freem(m0);
9ad31c34 527 return (ENOBUFS);
4258e601
KS
528 }
529
530 /*
531 * Fill in mbuf with extended SP header
532 * and addresses and length put into network format.
533 */
534 m->m_off = MMAXOFF - sizeof (struct spidp);
535 m->m_len = sizeof (struct spidp);
536 m->m_next = m0;
537 si = mtod(m, struct spidp *);
538 *si = cb->s_shdr;
539 if ((cb->s_flags & SF_PI) && (cb->s_flags & SF_HO)) {
99e0091e
KS
540 register struct sphdr *sh;
541 if (m0->m_len < sizeof (*sh)) {
542 if((m0 = m_pullup(m0, sizeof(*sh))) == NULL) {
543 (void) m_free(m);
544 m_freem(m0);
545 return (EINVAL);
546 }
547 m->m_next = m0;
548 }
549 sh = mtod(m0, struct sphdr *);
4258e601
KS
550 si->si_dt = sh->sp_dt;
551 si->si_cc |= sh->sp_cc & SP_EM;
552 m0->m_len -= sizeof (*sh);
553 m0->m_off += sizeof (*sh);
554 len -= sizeof (*sh);
555 }
556 len += sizeof(*si);
fe594349
KS
557 if (cb->s_oobflags & SF_SOOB) {
558 /*
559 * Per jqj@cornell:
560 * make sure OB packets convey exactly 1 byte.
561 * If the packet is 1 byte or larger, we
562 * have already guaranted there to be at least
563 * one garbage byte for the checksum, and
564 * extra bytes shouldn't hurt!
fe594349
KS
565 */
566 if (len > sizeof(*si)) {
567 si->si_cc |= SP_OB;
568 len = (1 + sizeof(*si));
569 }
570 }
f97be0c9 571 si->si_len = htons((u_short)len);
4258e601
KS
572 /*
573 * queue stuff up for output
574 */
575 sbappendrecord(sb,m);
576 cb->s_seq++;
577 }
4258e601
KS
578 /*
579 * update window
580 */
581 {
f97be0c9
KS
582 register struct sockbuf *sb2 = &so->so_rcv;
583 int credit = ((sb2->sb_mbmax - sb2->sb_mbcnt) / cb->s_mtu);
4258e601
KS
584 int alo = cb->s_ack + credit;
585
99e0091e
KS
586 if (cb->s_alo < alo)
587 cb->s_alo = alo;
4258e601
KS
588 }
589
590 if (cb->s_oobflags & SF_SOOB) {
591 /*
592 * must transmit this out of band packet
593 */
594 cb->s_oobflags &= ~ SF_SOOB;
595 } else {
596 /*
597 * Decide what to transmit:
598 * If we have a new packet, send that
599 * (So long as it is in our allocation)
600 * If it is time to retransmit a packet,
601 * send that.
602 * Otherwise, see if it time to bang on them
603 * to ask for our current allocation.
604 */
605 if (SSEQ_LT(cb->s_snt, cb->s_ralo))
606 lookfor = cb->s_snt + 1;
99e0091e 607 else if (cb->s_force == (1+TCPT_REXMT)) {
4258e601
KS
608 lookfor = cb->s_rack;
609 } else if (SSEQ_LT(cb->s_ralo, cb->s_seq)) {
610 lookfor = 0;
99e0091e 611 if (cb->s_timer[TCPT_PERSIST] == 0) {
4258e601 612 spp_setpersist(cb);
99e0091e 613 /* tcp has cb->s_rxtshift = 0; here */
4258e601
KS
614 }
615 }
616 m = sb->sb_mb;
99e0091e 617 while (m) {
4258e601
KS
618 si = mtod(m, struct spidp *);
619 m = m->m_act;
620 if (SSEQ_LT(si->si_seq, cb->s_rack)) {
621 if ((sb->sb_flags & SB_WAIT)
622 || so->so_snd.sb_sel)
623 sowwakeup(so);
624 sbdroprecord(sb);
625 si = 0;
626 continue;
627 }
628 if (SSEQ_LT(si->si_seq, lookfor))
629 continue;
630 break;
631 }
99e0091e
KS
632 if (si && (si->si_seq != lookfor))
633 si = 0;
4258e601
KS
634 }
635 cb->s_want = lookfor;
636
637 if (si) {
638 /*
639 * must make a copy of this packet for
640 * idp_output to monkey with
641 */
99e0091e
KS
642 m = m_copy(dtom(si), 0, (int)M_COPYALL);
643 if (m == NULL)
9ad31c34
KS
644 return (ENOBUFS);
645 m0 = m;
4258e601
KS
646 si = mtod(m, struct spidp *);
647 } else if (cb->s_force || cb->s_flags & SF_AK) {
648 /*
649 * Must send an acknowledgement or a probe
650 */
651 m = m_get(M_DONTWAIT, MT_HEADER);
652 if (m == 0)
9ad31c34 653 return (ENOBUFS);
4258e601
KS
654 /*
655 * Fill in mbuf with extended SP header
656 * and addresses and length put into network format.
657 */
658 m->m_off = MMAXOFF - sizeof (struct spidp);
659 m->m_len = sizeof (*si);
660 m->m_next = 0;
661 si = mtod(m, struct spidp *);
662 *si = cb->s_shdr;
663 si->si_seq = cb->s_snt + 1;
fe594349 664 si->si_len = htons(sizeof (*si));
4258e601
KS
665 si->si_cc |= SP_SP;
666 cb->s_flags &= ~SF_AK;
667 }
668 /*
669 * Stuff checksum and output datagram.
670 */
671 if (si) {
672 /*
673 * If we are almost out of allocation
674 * or one of the timers has gone off
675 * request an ack.
676 */
99e0091e 677 if (SSEQ_GEQ(cb->s_seq, cb->s_ralo))
4258e601
KS
678 si->si_cc |= SP_SA;
679 if (cb->s_force) {
680 si->si_cc |= SP_SA;
681 cb->s_force = 0;
682 }
99e0091e
KS
683 /*
684 * If this is a new packet (and not a system packet),
fe594349
KS
685 * and we are not currently timing anything,
686 * time this one and ask for an ack.
4258e601 687 */
99e0091e 688 if (SSEQ_LT(cb->s_snt, si->si_seq) && (!(si->si_cc & SP_SP))) {
4258e601 689 cb->s_snt = si->si_seq;
99e0091e 690 if (cb->s_rtt == 0) {
4258e601
KS
691 cb->s_rtseq = si->si_seq;
692 cb->s_rtt = 1;
693 si->si_cc |= SP_SA;
694 }
695 /*
696 * If the retransmit timer has not been set
697 * and this is a real packet
698 * then start the retransmit timer
699 */
99e0091e 700 if (cb->s_timer[TCPT_REXMT] == 0) {
4258e601
KS
701 TCPT_RANGESET(cb->s_timer[TCPT_REXMT],
702 tcp_beta * cb->s_srtt, TCPTV_MIN,
703 TCPTV_MAX);
704 cb->s_rxtshift = 0;
705 }
706 }
707 si->si_seq = htons(si->si_seq);
708 si->si_alo = htons(cb->s_alo);
709 si->si_ack = htons(cb->s_ack);
710
711 if (idpcksum) {
712 si->si_sum = 0;
fe594349 713 len = ntohs(si->si_len);
99e0091e
KS
714 if (len & 1)
715 len++;
4258e601
KS
716 si->si_sum = ns_cksum(dtom(si), len);
717 } else
718 si->si_sum = 0xffff;
719
720 if (so->so_options & SO_DEBUG || traceallspps)
721 spp_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
722 spp_output_cnt++;
723 if (so->so_options & SO_DONTROUTE)
724 error = ns_output(m, (struct route *)0, NS_ROUTETOIF);
725 else
726 error = ns_output(m, &cb->s_nspcb->nsp_route, 0);
727 if (traceallspps && sppconsdebug) {
728 printf("spp_out: %x\n", error);
729 }
9ad31c34 730 return (error);
4258e601
KS
731 }
732 if (so->so_options & SO_DEBUG || traceallspps)
733 spp_trace(SA_OUTPUT, cb->s_state, cb, si, 0);
9ad31c34 734 return (error);
4258e601
KS
735}
736
737/*ARGSUSED*/
738spp_ctloutput(req, so, level, name, value)
739 int req;
740 struct socket *so;
741 int name;
742 struct mbuf **value;
743{
744 register struct mbuf *m;
745 struct nspcb *nsp = sotonspcb(so);
746 register struct sppcb *cb;
747 int mask, error = 0;
748
749 if (level != NSPROTO_SPP) {
750 /* This will have to be changed when we do more general
751 stacking of protocols */
9ad31c34 752 return (idp_ctloutput(req, so, level, name, value));
4258e601
KS
753 }
754 if (nsp == NULL) {
755 error = EINVAL;
756 goto release;
757 } else
758 cb = nstosppcb(nsp);
759
760 switch (req) {
9ad31c34 761
4258e601 762 case PRCO_GETOPT:
99e0091e 763 if (value == NULL)
9ad31c34 764 return (EINVAL);
4258e601 765 m = m_get(M_DONTWAIT, MT_DATA);
99e0091e 766 if (m == NULL)
9ad31c34 767 return (ENOBUFS);
4258e601 768 switch (name) {
9ad31c34 769
4258e601
KS
770 case SO_HEADERS_ON_INPUT:
771 mask = SF_HI;
772 goto get_flags;
9ad31c34 773
4258e601
KS
774 case SO_HEADERS_ON_OUTPUT:
775 mask = SF_HO;
776 get_flags:
777 m->m_len = sizeof(short);
778 m->m_off = MMAXOFF - sizeof(short);
779 *mtod(m, short *) = cb->s_flags & mask;
780 break;
9ad31c34 781
4258e601
KS
782 case SO_LAST_HEADER:
783 m->m_len = sizeof(struct sphdr);
784 m->m_off = MMAXOFF - sizeof(struct sphdr);
785 *mtod(m, struct sphdr *) = cb->s_rhdr;
786 break;
9ad31c34 787
4258e601
KS
788 case SO_DEFAULT_HEADERS:
789 m->m_len = sizeof(struct spidp);
790 m->m_off = MMAXOFF - sizeof(struct sphdr);
791 *mtod(m, struct sphdr *) = cb->s_shdr.si_s;
792 }
793 *value = m;
794 break;
9ad31c34 795
4258e601
KS
796 case PRCO_SETOPT:
797 switch (name) {
f97be0c9 798 int *ok;
4258e601
KS
799
800 case SO_HEADERS_ON_INPUT:
801 mask = SF_HI;
802 goto set_head;
9ad31c34 803
4258e601
KS
804 case SO_HEADERS_ON_OUTPUT:
805 mask = SF_HO;
806 set_head:
807 if (value && *value) {
808 ok = mtod(*value, int *);
809 if (*ok)
810 cb->s_flags |= mask;
811 else
812 cb->s_flags &= ~mask;
813 } else error = EINVAL;
814 break;
9ad31c34 815
4258e601
KS
816 case SO_DEFAULT_HEADERS:
817 {
818 register struct sphdr *sp
819 = mtod(*value, struct sphdr *);
820 cb->s_dt = sp->sp_dt;
821 cb->s_cc = sp->sp_cc & SP_EM;
822 }
823 }
824 if (value && *value)
825 m_freem(*value);
826 break;
827 }
828 release:
9ad31c34 829 return (error);
4258e601
KS
830}
831
832/*ARGSUSED*/
833spp_usrreq(so, req, m, nam, rights)
834 struct socket *so;
835 int req;
836 struct mbuf *m, *nam, *rights;
837{
838 struct nspcb *nsp = sotonspcb(so);
839 register struct sppcb *cb;
840 int s = splnet();
841 int error = 0, ostate;
842
843 if (req == PRU_CONTROL)
844 return (ns_control(so, (int)m, (caddr_t)nam,
845 (struct ifnet *)rights));
846 if (rights && rights->m_len) {
847 error = EINVAL;
848 goto release;
849 }
850 if (nsp == NULL) {
851 if (req != PRU_ATTACH) {
852 error = EINVAL;
853 goto release;
854 }
855 } else
856 cb = nstosppcb(nsp);
857
858 ostate = cb ? cb->s_state : 0;
859
860 switch (req) {
9ad31c34 861
4258e601
KS
862 case PRU_ATTACH:
863 if (nsp != NULL) {
864 error = EISCONN;
865 break;
866 }
867 error = ns_pcballoc(so, &nspcb);
868 if (error)
869 break;
870 error = soreserve(so, 2048, 2048);
871 if (error)
872 break;
873 nsp = sotonspcb(so);
874 {
875 struct mbuf *mm = m_getclr(M_DONTWAIT,MT_PCB);
876
99e0091e 877 if (mm == NULL) {
4258e601
KS
878 error = ENOBUFS;
879 break;
880 }
881 cb = mtod(mm, struct sppcb *);
882 cb->s_state = TCPS_LISTEN;
4258e601
KS
883 cb->s_snt = -1;
884 cb->s_q.si_next = cb->s_q.si_prev = &cb->s_q;
885 cb->s_nspcb = nsp;
886 nsp->nsp_pcb = (caddr_t) cb;
887 }
888 break;
889
890 case PRU_DETACH:
891 if (nsp == NULL) {
892 error = ENOTCONN;
893 break;
894 }
895 if (cb->s_state > TCPS_LISTEN)
896 cb = spp_disconnect(cb);
897 else
898 cb = spp_close(cb);
899 break;
900
901 case PRU_BIND:
902 error = ns_pcbbind(nsp, nam);
903 break;
904
905 case PRU_LISTEN:
906 if (nsp->nsp_lport == 0)
907 error = ns_pcbbind(nsp, (struct mbuf *)0);
908 if (error == 0)
909 cb->s_state = TCPS_LISTEN;
910 break;
911
912 /*
913 * Initiate connection to peer.
914 * Enter SYN_SENT state, and mark socket as connecting.
915 * Start keep-alive timer, setup prototype header,
916 * Send initial system packet requesting connection.
917 */
918 case PRU_CONNECT:
919 if (nsp->nsp_lport == 0) {
920 error = ns_pcbbind(nsp, (struct mbuf *)0);
921 if (error)
922 break;
923 }
924 error = ns_pcbconnect(nsp, nam);
925 if (error)
926 break;
927 soisconnecting(so);
928 cb->s_state = TCPS_SYN_SENT;
929 cb->s_did = 0;
930 spp_template(cb);
931 cb->s_timer[TCPT_KEEP] = TCPTV_KEEP;
932 cb->s_force = 1 + TCPTV_KEEP;
933 /*
934 * Other party is required to respond to
935 * the port I send from, but he is not
936 * required to answer from where I am sending to,
937 * so allow wildcarding.
938 * original port I am sending to is still saved in
939 * cb->s_dport.
940 */
941 nsp->nsp_fport = 0;
942 error = spp_output(cb, (struct mbuf *) 0);
943 break;
944
945 case PRU_CONNECT2:
946 error = EOPNOTSUPP;
947 break;
948
949 /*
950 * We may decide later to implement connection closing
951 * handshaking at the spp level optionally.
952 * here is the hook to do it:
953 */
954 case PRU_DISCONNECT:
955 cb = spp_disconnect(cb);
956 break;
957
958 /*
959 * Accept a connection. Essentially all the work is
960 * done at higher levels; just return the address
961 * of the peer, storing through addr.
962 */
963 case PRU_ACCEPT: {
964 struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
965
966 nam->m_len = sizeof (struct sockaddr_ns);
967 sns->sns_family = AF_NS;
968 sns->sns_addr = nsp->nsp_faddr;
969 break;
970 }
971
972 case PRU_SHUTDOWN:
973 socantsendmore(so);
974 cb = spp_usrclosed(cb);
975 if (cb)
976 error = spp_output(cb, (struct mbuf *) 0);
977 break;
978
979 /*
980 * After a receive, possibly send acknowledgment
981 * updating allocation.
982 */
983 case PRU_RCVD:
984 (void) spp_output(cb, (struct mbuf *) 0);
985 break;
986
987 case PRU_SEND:
988 error = spp_output(cb, m);
989 m = NULL;
990 break;
991
992 case PRU_ABORT:
f97be0c9 993 (void) spp_drop(cb, ECONNABORTED);
4258e601
KS
994 break;
995
996 case PRU_SENSE:
997 case PRU_CONTROL:
998 m = NULL;
999 error = EOPNOTSUPP;
1000 break;
1001
1002 case PRU_RCVOOB:
1003 if ( ! (cb->s_oobflags & SF_IOOB) ) {
1004 error = EWOULDBLOCK;
1005 break;
1006 }
1007 m->m_len = 1;
1008 *mtod(m, caddr_t) = cb->s_iobc;
1009 cb->s_oobflags &= ~ SF_IOOB;
1010 break;
1011
1012 case PRU_SENDOOB:
1013 if (sbspace(&so->so_snd) < -512) {
1014 m_freem(m);
1015 error = ENOBUFS;
1016 break;
1017 }
1018 cb->s_oobflags |= SF_SOOB;
1019 error = spp_output(cb, m);
1020 m = NULL;
1021 cb->s_oobflags &= ~SF_SOOB;
1022 break;
1023
1024 case PRU_SOCKADDR:
1025 ns_setsockaddr(nsp, nam);
1026 break;
1027
1028 case PRU_PEERADDR:
1029 ns_setpeeraddr(nsp, nam);
1030 break;
1031
1032 case PRU_SLOWTIMO:
1033 cb = spp_timers(cb, (int)nam);
1034 break;
1035
1036 case PRU_FASTTIMO:
1037 case PRU_PROTORCV:
1038 case PRU_PROTOSEND:
1039 error = EOPNOTSUPP;
1040 break;
1041
1042 default:
1043 panic("sp_usrreq");
1044 }
1045 if (cb && (so->so_options & SO_DEBUG || traceallspps))
f97be0c9 1046 spp_trace(SA_USER, (u_char)ostate, cb, (struct spidp *)0, req);
4258e601
KS
1047release:
1048 if (m != NULL)
1049 m_freem(m);
1050 splx(s);
1051 return (error);
1052}
1053
1054spp_usrreq_sp(so, req, m, nam, rights)
1055 struct socket *so;
1056 int req;
1057 struct mbuf *m, *nam, *rights;
1058{
1059 int error = spp_usrreq(so, req, m, nam, rights);
1060
99e0091e 1061 if (req == PRU_ATTACH && error == 0) {
4258e601
KS
1062 struct nspcb *nsp = sotonspcb(so);
1063 ((struct sppcb *)nsp->nsp_pcb)->s_flags |=
1064 (SF_HI | SF_HO | SF_PI);
1065 }
9ad31c34 1066 return (error);
4258e601
KS
1067}
1068
1069/*
1070 * Create template to be used to send spp packets on a connection.
1071 * Called after host entry created, fills
1072 * in a skeletal spp header (choosing connection id),
1073 * minimizing the amount of work necessary when the connection is used.
1074 */
1075spp_template(cb)
1076 struct sppcb *cb;
1077{
1078 register struct nspcb *nsp = cb->s_nspcb;
1079 register struct spidp *n = &(cb->s_shdr);
1080
1081 cb->s_mtu = 1024;
1082 n->si_pt = NSPROTO_SPP;
1083 n->si_sna = nsp->nsp_laddr;
1084 n->si_dna = nsp->nsp_faddr;
1085 n->si_sid = htons(spp_iss);
1086 spp_iss += SPP_ISSINCR/2;
1087 n->si_alo = 1;
1088}
1089
1090/*
1091 * Close a SPIP control block:
1092 * discard spp control block itself
1093 * discard ns protocol control block
1094 * wake up any sleepers
1095 */
1096struct sppcb *
1097spp_close(cb)
1098 register struct sppcb *cb;
1099{
1100 register struct spidp_q *s;
1101 struct nspcb *nsp = cb->s_nspcb;
1102 struct socket *so = nsp->nsp_socket;
1103 register struct mbuf *m;
1104
1105 s = cb->s_q.si_next;
1106 while (s != &(cb->s_q)) {
1107 s = s->si_next;
1108 m = dtom(s->si_prev);
1109 remque(s->si_prev);
1110 m_freem(m);
1111 }
1112 (void) m_free(dtom(cb));
1113 nsp->nsp_pcb = 0;
1114 soisdisconnected(so);
1115 ns_pcbdetach(nsp);
9ad31c34 1116 return ((struct sppcb *)0);
4258e601
KS
1117}
1118/*
1119 * Someday we may do level 3 handshaking
1120 * to close a connection or send a xerox style error.
1121 * For now, just close.
1122 */
1123struct sppcb *
1124spp_usrclosed(cb)
1125 register struct sppcb *cb;
1126{
9ad31c34 1127 return (spp_close(cb));
4258e601
KS
1128}
1129struct sppcb *
1130spp_disconnect(cb)
1131 register struct sppcb *cb;
1132{
9ad31c34 1133 return (spp_close(cb));
4258e601
KS
1134}
1135/*
1136 * Drop connection, reporting
1137 * the specified error.
1138 */
1139struct sppcb *
1140spp_drop(cb, errno)
1141 register struct sppcb *cb;
1142 int errno;
1143{
1144 struct socket *so = cb->s_nspcb->nsp_socket;
1145
1146 /*
1147 * someday, in the xerox world
1148 * we will generate error protocol packets
1149 * announcing that the socket has gone away.
1150 */
1151 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
1152 tp->t_state = TCPS_CLOSED;
1153 (void) tcp_output(tp);
1154 }*/
1155 so->so_error = errno;
1156 return (spp_close(cb));
1157}
1158
1159spp_abort(nsp)
1160 struct nspcb *nsp;
1161{
1162
f97be0c9 1163 (void) spp_close((struct sppcb *)nsp->nsp_pcb);
4258e601
KS
1164}
1165
1166spp_setpersist(cb)
1167 register struct sppcb *cb;
1168{
1169
1170 /*if (cb->s_timer[TCPT_REXMT])
1171 panic("spp_output REXMT");*/
1172 /*
1173 * Start/restart persistance timer.
1174 */
1175 TCPT_RANGESET(cb->s_timer[TCPT_PERSIST],
1176 ((int)(tcp_beta * cb->s_srtt)) << cb->s_rxtshift,
1177 TCPTV_PERSMIN, TCPTV_MAX);
1178 cb->s_rxtshift++;
1179 if (cb->s_rxtshift >= TCP_MAXRXTSHIFT)
1180 cb->s_rxtshift = 0;
1181}
1182/*
1183 * Fast timeout routine for processing delayed acks
1184 */
1185int spp_ftcnt;
1186spp_fasttimo()
1187{
1188 register struct nspcb *nsp;
1189 register struct sppcb *cb;
1190 int s = splnet();
1191
1192 nsp = nspcb.nsp_next;
1193 spp_ftcnt++;
1194 if (nsp)
1195 for (; nsp != &nspcb; nsp = nsp->nsp_next)
1196 if ((cb = (struct sppcb *)nsp->nsp_pcb) &&
1197 (cb->s_flags & SF_DELACK)) {
1198 cb->s_flags &= ~SF_DELACK;
1199 cb->s_flags |= SF_AK;
1200 (void) spp_output(cb, (struct mbuf *) 0);
1201 }
1202 splx(s);
1203}
1204
1205/*
1206 * spp protocol timeout routine called every 500 ms.
1207 * Updates the timers in all active pcb's and
1208 * causes finite state machine actions if timers expire.
1209 */
1210spp_slowtimo()
1211{
1212 register struct nspcb *ip, *ipnxt;
1213 register struct sppcb *cb;
1214 int s = splnet();
1215 register int i;
1216
1217 /*
1218 * Search through tcb's and update active timers.
1219 */
1220 ip = nspcb.nsp_next;
1221 if (ip == 0) {
1222 splx(s);
1223 return;
1224 }
1225 while (ip != &nspcb) {
1226 cb = nstosppcb(ip);
1227 ipnxt = ip->nsp_next;
1228 if (cb == 0)
1229 goto tpgone;
1230 for (i = 0; i < TCPT_NTIMERS; i++) {
1231 if (cb->s_timer[i] && --cb->s_timer[i] == 0) {
1232 (void) spp_usrreq(cb->s_nspcb->nsp_socket,
1233 PRU_SLOWTIMO, (struct mbuf *)0,
1234 (struct mbuf *)i, (struct mbuf *)0);
1235 if (ipnxt->nsp_prev != ip)
1236 goto tpgone;
1237 }
1238 }
1239 cb->s_idle++;
1240 if (cb->s_rtt)
1241 cb->s_rtt++;
1242tpgone:
1243 ip = ipnxt;
1244 }
1245 spp_iss += SPP_ISSINCR/PR_SLOWHZ; /* increment iss */
1246 splx(s);
1247}
1248
1249float spp_backoff[TCP_MAXRXTSHIFT] =
1250 { 1.0, 1.2, 1.4, 1.7, 2.0, 3.0, 5.0, 8.0, 16.0, 32.0 };
a5c5bc2d 1251int sppexprexmtbackoff = 0;
4258e601
KS
1252/*
1253 * TCP timer processing.
1254 */
1255struct sppcb *
1256spp_timers(cb, timer)
1257 register struct sppcb *cb;
1258 int timer;
1259{
1260
1261 cb->s_force = 1 + timer;
1262 switch (timer) {
1263
1264 /*
1265 * 2 MSL timeout in shutdown went off. Delete connection
1266 * control block.
1267 */
1268 case TCPT_2MSL:
1269 cb = spp_close(cb);
1270 break;
1271
1272 /*
1273 * Retransmission timer went off. Message has not
1274 * been acked within retransmit interval. Back off
1275 * to a longer retransmit interval and retransmit all
1276 * unacknowledged messages in the window.
1277 */
1278 case TCPT_REXMT:
1279 cb->s_rxtshift++;
1280 if (cb->s_rxtshift > TCP_MAXRXTSHIFT) {
1281 cb = spp_drop(cb, ETIMEDOUT);
1282 break;
1283 }
1284 (void) spp_output(cb, (struct mbuf *) 0);
1285 TCPT_RANGESET(cb->s_timer[TCPT_REXMT],
1286 (int)cb->s_srtt, TCPTV_MIN, TCPTV_MAX);
a5c5bc2d 1287 if (sppexprexmtbackoff) {
4258e601
KS
1288 TCPT_RANGESET(cb->s_timer[TCPT_REXMT],
1289 cb->s_timer[TCPT_REXMT] << cb->s_rxtshift,
1290 TCPTV_MIN, TCPTV_MAX);
1291 } else {
1292 TCPT_RANGESET(cb->s_timer[TCPT_REXMT],
1293 cb->s_timer[TCPT_REXMT] *
1294 spp_backoff[cb->s_rxtshift - 1],
1295 TCPTV_MIN, TCPTV_MAX);
1296 }
1297 break;
1298
1299 /*
1300 * Persistance timer into zero window.
1301 * Force a probe to be sent.
1302 */
1303 case TCPT_PERSIST:
1304 (void) spp_output(cb, (struct mbuf *) 0);
1305 spp_setpersist(cb);
1306 break;
1307
1308 /*
1309 * Keep-alive timer went off; send something
1310 * or drop connection if idle for too long.
1311 */
1312 case TCPT_KEEP:
1313 if (cb->s_state < TCPS_ESTABLISHED)
1314 goto dropit;
1315 if (cb->s_nspcb->nsp_socket->so_options & SO_KEEPALIVE) {
1316 if (cb->s_idle >= TCPTV_MAXIDLE)
1317 goto dropit;
1318 (void) spp_output(cb, (struct mbuf *) 0);
1319 } else
1320 cb->s_idle = 0;
1321 cb->s_timer[TCPT_KEEP] = TCPTV_KEEP;
1322 break;
1323 dropit:
1324 cb = spp_drop(cb, ETIMEDOUT);
1325 break;
1326 }
1327 return (cb);
1328}