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