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