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