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