]>
Commit | Line | Data |
---|---|---|
f0cbd3ec FB |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
2f5f8996 | 13 | * 3. Neither the name of the University nor the names of its contributors |
f0cbd3ec FB |
14 | * may be used to endorse or promote products derived from this software |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
28 | * | |
29 | * @(#)tcp_input.c 8.5 (Berkeley) 4/10/94 | |
30 | * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp | |
31 | */ | |
32 | ||
33 | /* | |
34 | * Changes and additions relating to SLiRP | |
35 | * Copyright (c) 1995 Danny Gasparovski. | |
5fafdf24 TS |
36 | * |
37 | * Please read the file COPYRIGHT for the | |
f0cbd3ec FB |
38 | * terms and conditions of the copyright. |
39 | */ | |
40 | ||
41 | #include <slirp.h> | |
42 | #include "ip_icmp.h" | |
43 | ||
44 | struct socket tcb; | |
45 | ||
9634d903 | 46 | #define TCPREXMTTHRESH 3 |
f0cbd3ec FB |
47 | struct socket *tcp_last_so = &tcb; |
48 | ||
49 | tcp_seq tcp_iss; /* tcp initial send seq # */ | |
50 | ||
51 | #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ) | |
52 | ||
53 | /* for modulo comparisons of timestamps */ | |
54 | #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) | |
55 | #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) | |
56 | ||
57 | /* | |
58 | * Insert segment ti into reassembly queue of tcp with | |
59 | * control block tp. Return TH_FIN if reassembly now includes | |
60 | * a segment with FIN. The macro form does the common case inline | |
61 | * (segment is the next to be received on an established connection, | |
62 | * and the queue is empty), avoiding linkage into and removal | |
63 | * from the queue and repetition of various conversions. | |
64 | * Set DELACK for segments received in order, but ack immediately | |
65 | * when segments are out of order (so fast retransmit can work). | |
66 | */ | |
67 | #ifdef TCP_ACK_HACK | |
68 | #define TCP_REASS(tp, ti, m, so, flags) {\ | |
69 | if ((ti)->ti_seq == (tp)->rcv_nxt && \ | |
429d0a3d | 70 | tcpfrag_list_empty(tp) && \ |
f0cbd3ec FB |
71 | (tp)->t_state == TCPS_ESTABLISHED) {\ |
72 | if (ti->ti_flags & TH_PUSH) \ | |
73 | tp->t_flags |= TF_ACKNOW; \ | |
74 | else \ | |
75 | tp->t_flags |= TF_DELACK; \ | |
76 | (tp)->rcv_nxt += (ti)->ti_len; \ | |
77 | flags = (ti)->ti_flags & TH_FIN; \ | |
31a60e22 BS |
78 | STAT(tcpstat.tcps_rcvpack++); \ |
79 | STAT(tcpstat.tcps_rcvbyte += (ti)->ti_len); \ | |
f0cbd3ec FB |
80 | if (so->so_emu) { \ |
81 | if (tcp_emu((so),(m))) sbappend((so), (m)); \ | |
82 | } else \ | |
83 | sbappend((so), (m)); \ | |
84 | /* sorwakeup(so); */ \ | |
85 | } else {\ | |
86 | (flags) = tcp_reass((tp), (ti), (m)); \ | |
87 | tp->t_flags |= TF_ACKNOW; \ | |
88 | } \ | |
89 | } | |
90 | #else | |
91 | #define TCP_REASS(tp, ti, m, so, flags) { \ | |
92 | if ((ti)->ti_seq == (tp)->rcv_nxt && \ | |
429d0a3d | 93 | tcpfrag_list_empty(tp) && \ |
f0cbd3ec FB |
94 | (tp)->t_state == TCPS_ESTABLISHED) { \ |
95 | tp->t_flags |= TF_DELACK; \ | |
96 | (tp)->rcv_nxt += (ti)->ti_len; \ | |
97 | flags = (ti)->ti_flags & TH_FIN; \ | |
31a60e22 BS |
98 | STAT(tcpstat.tcps_rcvpack++); \ |
99 | STAT(tcpstat.tcps_rcvbyte += (ti)->ti_len); \ | |
f0cbd3ec FB |
100 | if (so->so_emu) { \ |
101 | if (tcp_emu((so),(m))) sbappend(so, (m)); \ | |
102 | } else \ | |
103 | sbappend((so), (m)); \ | |
104 | /* sorwakeup(so); */ \ | |
105 | } else { \ | |
106 | (flags) = tcp_reass((tp), (ti), (m)); \ | |
107 | tp->t_flags |= TF_ACKNOW; \ | |
108 | } \ | |
109 | } | |
110 | #endif | |
9634d903 BS |
111 | static void tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, |
112 | struct tcpiphdr *ti); | |
113 | static void tcp_xmit_timer(register struct tcpcb *tp, int rtt); | |
f0cbd3ec | 114 | |
9634d903 BS |
115 | static int |
116 | tcp_reass(register struct tcpcb *tp, register struct tcpiphdr *ti, | |
117 | struct mbuf *m) | |
f0cbd3ec FB |
118 | { |
119 | register struct tcpiphdr *q; | |
120 | struct socket *so = tp->t_socket; | |
121 | int flags; | |
5fafdf24 | 122 | |
f0cbd3ec | 123 | /* |
511d2b14 | 124 | * Call with ti==NULL after become established to |
f0cbd3ec FB |
125 | * force pre-ESTABLISHED data up to user socket. |
126 | */ | |
511d2b14 | 127 | if (ti == NULL) |
f0cbd3ec FB |
128 | goto present; |
129 | ||
130 | /* | |
131 | * Find a segment which begins after this one does. | |
132 | */ | |
429d0a3d BS |
133 | for (q = tcpfrag_list_first(tp); !tcpfrag_list_end(q, tp); |
134 | q = tcpiphdr_next(q)) | |
f0cbd3ec FB |
135 | if (SEQ_GT(q->ti_seq, ti->ti_seq)) |
136 | break; | |
137 | ||
138 | /* | |
139 | * If there is a preceding segment, it may provide some of | |
140 | * our data already. If so, drop the data from the incoming | |
141 | * segment. If it provides all of our data, drop us. | |
142 | */ | |
429d0a3d | 143 | if (!tcpfrag_list_end(tcpiphdr_prev(q), tp)) { |
f0cbd3ec | 144 | register int i; |
429d0a3d | 145 | q = tcpiphdr_prev(q); |
f0cbd3ec FB |
146 | /* conversion to int (in i) handles seq wraparound */ |
147 | i = q->ti_seq + q->ti_len - ti->ti_seq; | |
148 | if (i > 0) { | |
149 | if (i >= ti->ti_len) { | |
31a60e22 BS |
150 | STAT(tcpstat.tcps_rcvduppack++); |
151 | STAT(tcpstat.tcps_rcvdupbyte += ti->ti_len); | |
f0cbd3ec FB |
152 | m_freem(m); |
153 | /* | |
154 | * Try to present any queued data | |
155 | * at the left window edge to the user. | |
156 | * This is needed after the 3-WHS | |
157 | * completes. | |
158 | */ | |
159 | goto present; /* ??? */ | |
160 | } | |
161 | m_adj(m, i); | |
162 | ti->ti_len -= i; | |
163 | ti->ti_seq += i; | |
164 | } | |
429d0a3d | 165 | q = tcpiphdr_next(q); |
f0cbd3ec | 166 | } |
31a60e22 BS |
167 | STAT(tcpstat.tcps_rcvoopack++); |
168 | STAT(tcpstat.tcps_rcvoobyte += ti->ti_len); | |
429d0a3d | 169 | ti->ti_mbuf = m; |
f0cbd3ec FB |
170 | |
171 | /* | |
172 | * While we overlap succeeding segments trim them or, | |
173 | * if they are completely covered, dequeue them. | |
174 | */ | |
429d0a3d | 175 | while (!tcpfrag_list_end(q, tp)) { |
f0cbd3ec FB |
176 | register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; |
177 | if (i <= 0) | |
178 | break; | |
179 | if (i < q->ti_len) { | |
180 | q->ti_seq += i; | |
181 | q->ti_len -= i; | |
429d0a3d | 182 | m_adj(q->ti_mbuf, i); |
f0cbd3ec FB |
183 | break; |
184 | } | |
429d0a3d BS |
185 | q = tcpiphdr_next(q); |
186 | m = tcpiphdr_prev(q)->ti_mbuf; | |
187 | remque(tcpiphdr2qlink(tcpiphdr_prev(q))); | |
f0cbd3ec FB |
188 | m_freem(m); |
189 | } | |
190 | ||
191 | /* | |
192 | * Stick new segment in its place. | |
193 | */ | |
429d0a3d | 194 | insque(tcpiphdr2qlink(ti), tcpiphdr2qlink(tcpiphdr_prev(q))); |
f0cbd3ec FB |
195 | |
196 | present: | |
197 | /* | |
198 | * Present data to user, advancing rcv_nxt through | |
199 | * completed sequence space. | |
200 | */ | |
201 | if (!TCPS_HAVEESTABLISHED(tp->t_state)) | |
202 | return (0); | |
429d0a3d BS |
203 | ti = tcpfrag_list_first(tp); |
204 | if (tcpfrag_list_end(ti, tp) || ti->ti_seq != tp->rcv_nxt) | |
f0cbd3ec FB |
205 | return (0); |
206 | if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) | |
207 | return (0); | |
208 | do { | |
209 | tp->rcv_nxt += ti->ti_len; | |
210 | flags = ti->ti_flags & TH_FIN; | |
429d0a3d BS |
211 | remque(tcpiphdr2qlink(ti)); |
212 | m = ti->ti_mbuf; | |
213 | ti = tcpiphdr_next(ti); | |
f0cbd3ec FB |
214 | /* if (so->so_state & SS_FCANTRCVMORE) */ |
215 | if (so->so_state & SS_FCANTSENDMORE) | |
216 | m_freem(m); | |
217 | else { | |
218 | if (so->so_emu) { | |
219 | if (tcp_emu(so,m)) sbappend(so, m); | |
220 | } else | |
221 | sbappend(so, m); | |
222 | } | |
223 | } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); | |
224 | /* sorwakeup(so); */ | |
225 | return (flags); | |
226 | } | |
227 | ||
228 | /* | |
229 | * TCP input routine, follows pages 65-76 of the | |
230 | * protocol specification dated September, 1981 very closely. | |
231 | */ | |
232 | void | |
511d2b14 | 233 | tcp_input(struct mbuf *m, int iphlen, struct socket *inso) |
f0cbd3ec FB |
234 | { |
235 | struct ip save_ip, *ip; | |
236 | register struct tcpiphdr *ti; | |
237 | caddr_t optp = NULL; | |
238 | int optlen = 0; | |
239 | int len, tlen, off; | |
511d2b14 | 240 | register struct tcpcb *tp = NULL; |
f0cbd3ec | 241 | register int tiflags; |
511d2b14 | 242 | struct socket *so = NULL; |
f0cbd3ec FB |
243 | int todrop, acked, ourfinisacked, needoutput = 0; |
244 | /* int dropsocket = 0; */ | |
245 | int iss = 0; | |
246 | u_long tiwin; | |
247 | int ret; | |
248 | /* int ts_present = 0; */ | |
a9ba3a85 | 249 | struct ex_list *ex_ptr; |
f0cbd3ec FB |
250 | |
251 | DEBUG_CALL("tcp_input"); | |
5fafdf24 | 252 | DEBUG_ARGS((dfd," m = %8lx iphlen = %2d inso = %lx\n", |
f0cbd3ec | 253 | (long )m, iphlen, (long )inso )); |
5fafdf24 | 254 | |
f0cbd3ec FB |
255 | /* |
256 | * If called with m == 0, then we're continuing the connect | |
257 | */ | |
258 | if (m == NULL) { | |
259 | so = inso; | |
3b46e624 | 260 | |
f0cbd3ec FB |
261 | /* Re-set a few variables */ |
262 | tp = sototcpcb(so); | |
263 | m = so->so_m; | |
511d2b14 | 264 | so->so_m = NULL; |
f0cbd3ec FB |
265 | ti = so->so_ti; |
266 | tiwin = ti->ti_win; | |
267 | tiflags = ti->ti_flags; | |
3b46e624 | 268 | |
f0cbd3ec FB |
269 | goto cont_conn; |
270 | } | |
5fafdf24 TS |
271 | |
272 | ||
31a60e22 | 273 | STAT(tcpstat.tcps_rcvtotal++); |
f0cbd3ec FB |
274 | /* |
275 | * Get IP and TCP header together in first mbuf. | |
276 | * Note: IP leaves IP header in first mbuf. | |
277 | */ | |
278 | ti = mtod(m, struct tcpiphdr *); | |
279 | if (iphlen > sizeof(struct ip )) { | |
280 | ip_stripoptions(m, (struct mbuf *)0); | |
281 | iphlen=sizeof(struct ip ); | |
282 | } | |
283 | /* XXX Check if too short */ | |
5fafdf24 | 284 | |
f0cbd3ec FB |
285 | |
286 | /* | |
287 | * Save a copy of the IP header in case we want restore it | |
288 | * for sending an ICMP error message in response. | |
289 | */ | |
290 | ip=mtod(m, struct ip *); | |
5fafdf24 | 291 | save_ip = *ip; |
f0cbd3ec FB |
292 | save_ip.ip_len+= iphlen; |
293 | ||
294 | /* | |
295 | * Checksum extended TCP header and data. | |
296 | */ | |
297 | tlen = ((struct ip *)ti)->ip_len; | |
511d2b14 BS |
298 | tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL; |
299 | memset(&ti->ti_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr)); | |
f0cbd3ec FB |
300 | ti->ti_x1 = 0; |
301 | ti->ti_len = htons((u_int16_t)tlen); | |
302 | len = sizeof(struct ip ) + tlen; | |
303 | /* keep checksum for ICMP reply | |
5fafdf24 | 304 | * ti->ti_sum = cksum(m, len); |
f0cbd3ec FB |
305 | * if (ti->ti_sum) { */ |
306 | if(cksum(m, len)) { | |
31a60e22 | 307 | STAT(tcpstat.tcps_rcvbadsum++); |
f0cbd3ec FB |
308 | goto drop; |
309 | } | |
310 | ||
311 | /* | |
312 | * Check that TCP offset makes sense, | |
313 | * pull out TCP options and adjust length. XXX | |
314 | */ | |
315 | off = ti->ti_off << 2; | |
316 | if (off < sizeof (struct tcphdr) || off > tlen) { | |
31a60e22 | 317 | STAT(tcpstat.tcps_rcvbadoff++); |
f0cbd3ec FB |
318 | goto drop; |
319 | } | |
320 | tlen -= off; | |
321 | ti->ti_len = tlen; | |
322 | if (off > sizeof (struct tcphdr)) { | |
323 | optlen = off - sizeof (struct tcphdr); | |
324 | optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr); | |
325 | ||
5fafdf24 | 326 | /* |
f0cbd3ec FB |
327 | * Do quick retrieval of timestamp options ("options |
328 | * prediction?"). If timestamp is the only option and it's | |
329 | * formatted as recommended in RFC 1323 appendix A, we | |
330 | * quickly get the values now and not bother calling | |
331 | * tcp_dooptions(), etc. | |
332 | */ | |
333 | /* if ((optlen == TCPOLEN_TSTAMP_APPA || | |
334 | * (optlen > TCPOLEN_TSTAMP_APPA && | |
335 | * optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && | |
336 | * *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) && | |
337 | * (ti->ti_flags & TH_SYN) == 0) { | |
338 | * ts_present = 1; | |
339 | * ts_val = ntohl(*(u_int32_t *)(optp + 4)); | |
340 | * ts_ecr = ntohl(*(u_int32_t *)(optp + 8)); | |
341 | * optp = NULL; / * we've parsed the options * / | |
342 | * } | |
343 | */ | |
344 | } | |
345 | tiflags = ti->ti_flags; | |
5fafdf24 | 346 | |
f0cbd3ec FB |
347 | /* |
348 | * Convert TCP protocol specific fields to host format. | |
349 | */ | |
350 | NTOHL(ti->ti_seq); | |
351 | NTOHL(ti->ti_ack); | |
352 | NTOHS(ti->ti_win); | |
353 | NTOHS(ti->ti_urp); | |
354 | ||
355 | /* | |
356 | * Drop TCP, IP headers and TCP options. | |
357 | */ | |
358 | m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); | |
359 | m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); | |
5fafdf24 | 360 | |
a9ba3a85 | 361 | if (slirp_restrict) { |
a13a4126 | 362 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
a9ba3a85 | 363 | if (ex_ptr->ex_fport == ti->ti_dport && |
a13a4126 | 364 | ti->ti_dst.s_addr == ex_ptr->ex_addr.s_addr) { |
a9ba3a85 | 365 | break; |
a13a4126 JK |
366 | } |
367 | } | |
a9ba3a85 AL |
368 | if (!ex_ptr) |
369 | goto drop; | |
370 | } | |
f0cbd3ec FB |
371 | /* |
372 | * Locate pcb for segment. | |
373 | */ | |
374 | findso: | |
375 | so = tcp_last_so; | |
376 | if (so->so_fport != ti->ti_dport || | |
377 | so->so_lport != ti->ti_sport || | |
378 | so->so_laddr.s_addr != ti->ti_src.s_addr || | |
379 | so->so_faddr.s_addr != ti->ti_dst.s_addr) { | |
380 | so = solookup(&tcb, ti->ti_src, ti->ti_sport, | |
381 | ti->ti_dst, ti->ti_dport); | |
382 | if (so) | |
383 | tcp_last_so = so; | |
31a60e22 | 384 | STAT(tcpstat.tcps_socachemiss++); |
f0cbd3ec FB |
385 | } |
386 | ||
387 | /* | |
388 | * If the state is CLOSED (i.e., TCB does not exist) then | |
389 | * all data in the incoming segment is discarded. | |
390 | * If the TCB exists but is in CLOSED state, it is embryonic, | |
391 | * but should either do a listen or a connect soon. | |
392 | * | |
393 | * state == CLOSED means we've done socreate() but haven't | |
5fafdf24 TS |
394 | * attached it to a protocol yet... |
395 | * | |
f0cbd3ec FB |
396 | * XXX If a TCB does not exist, and the TH_SYN flag is |
397 | * the only flag set, then create a session, mark it | |
398 | * as if it was LISTENING, and continue... | |
399 | */ | |
511d2b14 | 400 | if (so == NULL) { |
f0cbd3ec FB |
401 | if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN) |
402 | goto dropwithreset; | |
3b46e624 | 403 | |
f0cbd3ec FB |
404 | if ((so = socreate()) == NULL) |
405 | goto dropwithreset; | |
406 | if (tcp_attach(so) < 0) { | |
407 | free(so); /* Not sofree (if it failed, it's not insqued) */ | |
408 | goto dropwithreset; | |
409 | } | |
3b46e624 | 410 | |
9634d903 BS |
411 | sbreserve(&so->so_snd, TCP_SNDSPACE); |
412 | sbreserve(&so->so_rcv, TCP_RCVSPACE); | |
3b46e624 | 413 | |
f0cbd3ec FB |
414 | /* tcp_last_so = so; */ /* XXX ? */ |
415 | /* tp = sototcpcb(so); */ | |
3b46e624 | 416 | |
f0cbd3ec FB |
417 | so->so_laddr = ti->ti_src; |
418 | so->so_lport = ti->ti_sport; | |
419 | so->so_faddr = ti->ti_dst; | |
420 | so->so_fport = ti->ti_dport; | |
3b46e624 | 421 | |
f0cbd3ec FB |
422 | if ((so->so_iptos = tcp_tos(so)) == 0) |
423 | so->so_iptos = ((struct ip *)ti)->ip_tos; | |
3b46e624 | 424 | |
f0cbd3ec FB |
425 | tp = sototcpcb(so); |
426 | tp->t_state = TCPS_LISTEN; | |
427 | } | |
3b46e624 | 428 | |
f0cbd3ec FB |
429 | /* |
430 | * If this is a still-connecting socket, this probably | |
431 | * a retransmit of the SYN. Whether it's a retransmit SYN | |
432 | * or something else, we nuke it. | |
433 | */ | |
434 | if (so->so_state & SS_ISFCONNECTING) | |
435 | goto drop; | |
436 | ||
437 | tp = sototcpcb(so); | |
5fafdf24 | 438 | |
f0cbd3ec | 439 | /* XXX Should never fail */ |
511d2b14 | 440 | if (tp == NULL) |
f0cbd3ec FB |
441 | goto dropwithreset; |
442 | if (tp->t_state == TCPS_CLOSED) | |
443 | goto drop; | |
5fafdf24 | 444 | |
f0cbd3ec FB |
445 | /* Unscale the window into a 32-bit value. */ |
446 | /* if ((tiflags & TH_SYN) == 0) | |
447 | * tiwin = ti->ti_win << tp->snd_scale; | |
448 | * else | |
449 | */ | |
450 | tiwin = ti->ti_win; | |
451 | ||
452 | /* | |
453 | * Segment received on connection. | |
454 | * Reset idle time and keep-alive timer. | |
455 | */ | |
456 | tp->t_idle = 0; | |
9634d903 BS |
457 | if (SO_OPTIONS) |
458 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL; | |
f0cbd3ec | 459 | else |
9634d903 | 460 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE; |
f0cbd3ec FB |
461 | |
462 | /* | |
463 | * Process options if not in LISTEN state, | |
464 | * else do it below (after getting remote address). | |
465 | */ | |
466 | if (optp && tp->t_state != TCPS_LISTEN) | |
5fafdf24 | 467 | tcp_dooptions(tp, (u_char *)optp, optlen, ti); |
f0cbd3ec FB |
468 | /* , */ |
469 | /* &ts_present, &ts_val, &ts_ecr); */ | |
470 | ||
5fafdf24 | 471 | /* |
f0cbd3ec FB |
472 | * Header prediction: check for the two common cases |
473 | * of a uni-directional data xfer. If the packet has | |
474 | * no control flags, is in-sequence, the window didn't | |
475 | * change and we're not retransmitting, it's a | |
476 | * candidate. If the length is zero and the ack moved | |
477 | * forward, we're the sender side of the xfer. Just | |
478 | * free the data acked & wake any higher level process | |
479 | * that was blocked waiting for space. If the length | |
480 | * is non-zero and the ack didn't move, we're the | |
481 | * receiver side. If we're getting packets in-order | |
482 | * (the reassembly queue is empty), add the data to | |
483 | * the socket buffer and note that we need a delayed ack. | |
484 | * | |
485 | * XXX Some of these tests are not needed | |
486 | * eg: the tiwin == tp->snd_wnd prevents many more | |
487 | * predictions.. with no *real* advantage.. | |
488 | */ | |
489 | if (tp->t_state == TCPS_ESTABLISHED && | |
490 | (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && | |
491 | /* (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && */ | |
492 | ti->ti_seq == tp->rcv_nxt && | |
493 | tiwin && tiwin == tp->snd_wnd && | |
494 | tp->snd_nxt == tp->snd_max) { | |
5fafdf24 | 495 | /* |
f0cbd3ec FB |
496 | * If last ACK falls within this segment's sequence numbers, |
497 | * record the timestamp. | |
498 | */ | |
499 | /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && | |
500 | * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) { | |
501 | * tp->ts_recent_age = tcp_now; | |
502 | * tp->ts_recent = ts_val; | |
503 | * } | |
504 | */ | |
505 | if (ti->ti_len == 0) { | |
506 | if (SEQ_GT(ti->ti_ack, tp->snd_una) && | |
507 | SEQ_LEQ(ti->ti_ack, tp->snd_max) && | |
508 | tp->snd_cwnd >= tp->snd_wnd) { | |
509 | /* | |
510 | * this is a pure ack for outstanding data. | |
511 | */ | |
31a60e22 | 512 | STAT(tcpstat.tcps_predack++); |
f0cbd3ec FB |
513 | /* if (ts_present) |
514 | * tcp_xmit_timer(tp, tcp_now-ts_ecr+1); | |
5fafdf24 | 515 | * else |
f0cbd3ec FB |
516 | */ if (tp->t_rtt && |
517 | SEQ_GT(ti->ti_ack, tp->t_rtseq)) | |
518 | tcp_xmit_timer(tp, tp->t_rtt); | |
519 | acked = ti->ti_ack - tp->snd_una; | |
31a60e22 BS |
520 | STAT(tcpstat.tcps_rcvackpack++); |
521 | STAT(tcpstat.tcps_rcvackbyte += acked); | |
f0cbd3ec FB |
522 | sbdrop(&so->so_snd, acked); |
523 | tp->snd_una = ti->ti_ack; | |
524 | m_freem(m); | |
525 | ||
526 | /* | |
527 | * If all outstanding data are acked, stop | |
528 | * retransmit timer, otherwise restart timer | |
529 | * using current (possibly backed-off) value. | |
530 | * If process is waiting for space, | |
531 | * wakeup/selwakeup/signal. If data | |
532 | * are ready to send, let tcp_output | |
533 | * decide between more output or persist. | |
534 | */ | |
535 | if (tp->snd_una == tp->snd_max) | |
536 | tp->t_timer[TCPT_REXMT] = 0; | |
537 | else if (tp->t_timer[TCPT_PERSIST] == 0) | |
538 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; | |
539 | ||
5fafdf24 | 540 | /* |
f0cbd3ec FB |
541 | * There's room in so_snd, sowwakup will read() |
542 | * from the socket if we can | |
543 | */ | |
544 | /* if (so->so_snd.sb_flags & SB_NOTIFY) | |
545 | * sowwakeup(so); | |
546 | */ | |
5fafdf24 | 547 | /* |
f0cbd3ec FB |
548 | * This is called because sowwakeup might have |
549 | * put data into so_snd. Since we don't so sowwakeup, | |
550 | * we don't need this.. XXX??? | |
551 | */ | |
552 | if (so->so_snd.sb_cc) | |
553 | (void) tcp_output(tp); | |
554 | ||
555 | return; | |
556 | } | |
557 | } else if (ti->ti_ack == tp->snd_una && | |
429d0a3d | 558 | tcpfrag_list_empty(tp) && |
f0cbd3ec FB |
559 | ti->ti_len <= sbspace(&so->so_rcv)) { |
560 | /* | |
561 | * this is a pure, in-sequence data packet | |
562 | * with nothing on the reassembly queue and | |
563 | * we have enough buffer space to take it. | |
564 | */ | |
31a60e22 | 565 | STAT(tcpstat.tcps_preddat++); |
f0cbd3ec | 566 | tp->rcv_nxt += ti->ti_len; |
31a60e22 BS |
567 | STAT(tcpstat.tcps_rcvpack++); |
568 | STAT(tcpstat.tcps_rcvbyte += ti->ti_len); | |
f0cbd3ec FB |
569 | /* |
570 | * Add data to socket buffer. | |
571 | */ | |
572 | if (so->so_emu) { | |
573 | if (tcp_emu(so,m)) sbappend(so, m); | |
574 | } else | |
575 | sbappend(so, m); | |
3b46e624 | 576 | |
5fafdf24 | 577 | /* |
f0cbd3ec FB |
578 | * XXX This is called when data arrives. Later, check |
579 | * if we can actually write() to the socket | |
580 | * XXX Need to check? It's be NON_BLOCKING | |
581 | */ | |
582 | /* sorwakeup(so); */ | |
3b46e624 | 583 | |
f0cbd3ec FB |
584 | /* |
585 | * If this is a short packet, then ACK now - with Nagel | |
586 | * congestion avoidance sender won't send more until | |
587 | * he gets an ACK. | |
5fafdf24 | 588 | * |
4f552e3b FB |
589 | * It is better to not delay acks at all to maximize |
590 | * TCP throughput. See RFC 2581. | |
5fafdf24 | 591 | */ |
4f552e3b FB |
592 | tp->t_flags |= TF_ACKNOW; |
593 | tcp_output(tp); | |
f0cbd3ec FB |
594 | return; |
595 | } | |
596 | } /* header prediction */ | |
597 | /* | |
598 | * Calculate amount of space in receive window, | |
599 | * and then do TCP input processing. | |
600 | * Receive window is amount of space in rcv queue, | |
601 | * but not less than advertised window. | |
602 | */ | |
603 | { int win; | |
604 | win = sbspace(&so->so_rcv); | |
605 | if (win < 0) | |
606 | win = 0; | |
607 | tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); | |
608 | } | |
609 | ||
610 | switch (tp->t_state) { | |
611 | ||
612 | /* | |
613 | * If the state is LISTEN then ignore segment if it contains an RST. | |
614 | * If the segment contains an ACK then it is bad and send a RST. | |
615 | * If it does not contain a SYN then it is not interesting; drop it. | |
616 | * Don't bother responding if the destination was a broadcast. | |
617 | * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial | |
618 | * tp->iss, and send a segment: | |
619 | * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> | |
620 | * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. | |
621 | * Fill in remote peer address fields if not previously specified. | |
622 | * Enter SYN_RECEIVED state, and process any other fields of this | |
623 | * segment in this state. | |
624 | */ | |
625 | case TCPS_LISTEN: { | |
626 | ||
627 | if (tiflags & TH_RST) | |
628 | goto drop; | |
629 | if (tiflags & TH_ACK) | |
630 | goto dropwithreset; | |
631 | if ((tiflags & TH_SYN) == 0) | |
632 | goto drop; | |
3b46e624 | 633 | |
f0cbd3ec FB |
634 | /* |
635 | * This has way too many gotos... | |
636 | * But a bit of spaghetti code never hurt anybody :) | |
637 | */ | |
3b46e624 | 638 | |
f0cbd3ec FB |
639 | /* |
640 | * If this is destined for the control address, then flag to | |
641 | * tcp_ctl once connected, otherwise connect | |
642 | */ | |
a13a4126 JK |
643 | if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) == |
644 | vnetwork_addr.s_addr) { | |
645 | if (so->so_faddr.s_addr != vhost_addr.s_addr && | |
646 | so->so_faddr.s_addr != vnameserver_addr.s_addr) { | |
f0cbd3ec FB |
647 | #if 0 |
648 | if(lastbyte==CTL_CMD || lastbyte==CTL_EXEC) { | |
649 | /* Command or exec adress */ | |
650 | so->so_state |= SS_CTL; | |
5fafdf24 | 651 | } else |
a3d4af03 FB |
652 | #endif |
653 | { | |
f0cbd3ec | 654 | /* May be an add exec */ |
f0cbd3ec | 655 | for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
5fafdf24 | 656 | if(ex_ptr->ex_fport == so->so_fport && |
a13a4126 | 657 | so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) { |
f0cbd3ec FB |
658 | so->so_state |= SS_CTL; |
659 | break; | |
660 | } | |
661 | } | |
662 | } | |
663 | if(so->so_state & SS_CTL) goto cont_input; | |
f0cbd3ec FB |
664 | } |
665 | /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */ | |
666 | } | |
3b46e624 | 667 | |
f0cbd3ec FB |
668 | if (so->so_emu & EMU_NOCONNECT) { |
669 | so->so_emu &= ~EMU_NOCONNECT; | |
670 | goto cont_input; | |
671 | } | |
3b46e624 | 672 | |
02d2c54c | 673 | if((tcp_fconnect(so) == -1) && (errno != EINPROGRESS) && (errno != EWOULDBLOCK)) { |
f0cbd3ec FB |
674 | u_char code=ICMP_UNREACH_NET; |
675 | DEBUG_MISC((dfd," tcp fconnect errno = %d-%s\n", | |
676 | errno,strerror(errno))); | |
677 | if(errno == ECONNREFUSED) { | |
678 | /* ACK the SYN, send RST to refuse the connection */ | |
679 | tcp_respond(tp, ti, m, ti->ti_seq+1, (tcp_seq)0, | |
5fafdf24 | 680 | TH_RST|TH_ACK); |
f0cbd3ec FB |
681 | } else { |
682 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST; | |
683 | HTONL(ti->ti_seq); /* restore tcp header */ | |
684 | HTONL(ti->ti_ack); | |
685 | HTONS(ti->ti_win); | |
686 | HTONS(ti->ti_urp); | |
687 | m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); | |
688 | m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); | |
689 | *ip=save_ip; | |
690 | icmp_error(m, ICMP_UNREACH,code, 0,strerror(errno)); | |
691 | } | |
692 | tp = tcp_close(tp); | |
693 | m_free(m); | |
694 | } else { | |
695 | /* | |
696 | * Haven't connected yet, save the current mbuf | |
697 | * and ti, and return | |
698 | * XXX Some OS's don't tell us whether the connect() | |
699 | * succeeded or not. So we must time it out. | |
700 | */ | |
701 | so->so_m = m; | |
702 | so->so_ti = ti; | |
703 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; | |
704 | tp->t_state = TCPS_SYN_RECEIVED; | |
705 | } | |
706 | return; | |
707 | ||
3b46e624 | 708 | cont_conn: |
5fafdf24 | 709 | /* m==NULL |
f0cbd3ec FB |
710 | * Check if the connect succeeded |
711 | */ | |
712 | if (so->so_state & SS_NOFDREF) { | |
713 | tp = tcp_close(tp); | |
714 | goto dropwithreset; | |
715 | } | |
3b46e624 | 716 | cont_input: |
f0cbd3ec | 717 | tcp_template(tp); |
3b46e624 | 718 | |
f0cbd3ec FB |
719 | if (optp) |
720 | tcp_dooptions(tp, (u_char *)optp, optlen, ti); | |
721 | /* , */ | |
722 | /* &ts_present, &ts_val, &ts_ecr); */ | |
3b46e624 | 723 | |
f0cbd3ec FB |
724 | if (iss) |
725 | tp->iss = iss; | |
5fafdf24 | 726 | else |
f0cbd3ec FB |
727 | tp->iss = tcp_iss; |
728 | tcp_iss += TCP_ISSINCR/2; | |
729 | tp->irs = ti->ti_seq; | |
730 | tcp_sendseqinit(tp); | |
731 | tcp_rcvseqinit(tp); | |
732 | tp->t_flags |= TF_ACKNOW; | |
733 | tp->t_state = TCPS_SYN_RECEIVED; | |
734 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; | |
31a60e22 | 735 | STAT(tcpstat.tcps_accepts++); |
f0cbd3ec FB |
736 | goto trimthenstep6; |
737 | } /* case TCPS_LISTEN */ | |
5fafdf24 | 738 | |
f0cbd3ec FB |
739 | /* |
740 | * If the state is SYN_SENT: | |
741 | * if seg contains an ACK, but not for our SYN, drop the input. | |
742 | * if seg contains a RST, then drop the connection. | |
743 | * if seg does not contain SYN, then drop it. | |
744 | * Otherwise this is an acceptable SYN segment | |
745 | * initialize tp->rcv_nxt and tp->irs | |
746 | * if seg contains ack then advance tp->snd_una | |
747 | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state | |
748 | * arrange for segment to be acked (eventually) | |
749 | * continue processing rest of data/controls, beginning with URG | |
750 | */ | |
751 | case TCPS_SYN_SENT: | |
752 | if ((tiflags & TH_ACK) && | |
753 | (SEQ_LEQ(ti->ti_ack, tp->iss) || | |
754 | SEQ_GT(ti->ti_ack, tp->snd_max))) | |
755 | goto dropwithreset; | |
756 | ||
757 | if (tiflags & TH_RST) { | |
758 | if (tiflags & TH_ACK) | |
759 | tp = tcp_drop(tp,0); /* XXX Check t_softerror! */ | |
760 | goto drop; | |
761 | } | |
762 | ||
763 | if ((tiflags & TH_SYN) == 0) | |
764 | goto drop; | |
765 | if (tiflags & TH_ACK) { | |
766 | tp->snd_una = ti->ti_ack; | |
767 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) | |
768 | tp->snd_nxt = tp->snd_una; | |
769 | } | |
770 | ||
771 | tp->t_timer[TCPT_REXMT] = 0; | |
772 | tp->irs = ti->ti_seq; | |
773 | tcp_rcvseqinit(tp); | |
774 | tp->t_flags |= TF_ACKNOW; | |
775 | if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { | |
31a60e22 | 776 | STAT(tcpstat.tcps_connects++); |
f0cbd3ec FB |
777 | soisfconnected(so); |
778 | tp->t_state = TCPS_ESTABLISHED; | |
3b46e624 | 779 | |
f0cbd3ec FB |
780 | /* Do window scaling on this connection? */ |
781 | /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == | |
782 | * (TF_RCVD_SCALE|TF_REQ_SCALE)) { | |
783 | * tp->snd_scale = tp->requested_s_scale; | |
784 | * tp->rcv_scale = tp->request_r_scale; | |
785 | * } | |
786 | */ | |
787 | (void) tcp_reass(tp, (struct tcpiphdr *)0, | |
788 | (struct mbuf *)0); | |
789 | /* | |
790 | * if we didn't have to retransmit the SYN, | |
791 | * use its rtt as our initial srtt & rtt var. | |
792 | */ | |
793 | if (tp->t_rtt) | |
794 | tcp_xmit_timer(tp, tp->t_rtt); | |
795 | } else | |
796 | tp->t_state = TCPS_SYN_RECEIVED; | |
797 | ||
798 | trimthenstep6: | |
799 | /* | |
800 | * Advance ti->ti_seq to correspond to first data byte. | |
801 | * If data, trim to stay within window, | |
802 | * dropping FIN if necessary. | |
803 | */ | |
804 | ti->ti_seq++; | |
805 | if (ti->ti_len > tp->rcv_wnd) { | |
806 | todrop = ti->ti_len - tp->rcv_wnd; | |
807 | m_adj(m, -todrop); | |
808 | ti->ti_len = tp->rcv_wnd; | |
809 | tiflags &= ~TH_FIN; | |
31a60e22 BS |
810 | STAT(tcpstat.tcps_rcvpackafterwin++); |
811 | STAT(tcpstat.tcps_rcvbyteafterwin += todrop); | |
f0cbd3ec FB |
812 | } |
813 | tp->snd_wl1 = ti->ti_seq - 1; | |
814 | tp->rcv_up = ti->ti_seq; | |
815 | goto step6; | |
816 | } /* switch tp->t_state */ | |
817 | /* | |
818 | * States other than LISTEN or SYN_SENT. | |
819 | * First check timestamp, if present. | |
5fafdf24 | 820 | * Then check that at least some bytes of segment are within |
f0cbd3ec FB |
821 | * receive window. If segment begins before rcv_nxt, |
822 | * drop leading data (and SYN); if nothing left, just ack. | |
5fafdf24 | 823 | * |
f0cbd3ec FB |
824 | * RFC 1323 PAWS: If we have a timestamp reply on this segment |
825 | * and it's less than ts_recent, drop it. | |
826 | */ | |
827 | /* if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && | |
828 | * TSTMP_LT(ts_val, tp->ts_recent)) { | |
829 | * | |
830 | */ /* Check to see if ts_recent is over 24 days old. */ | |
831 | /* if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { | |
832 | */ /* | |
833 | * * Invalidate ts_recent. If this segment updates | |
834 | * * ts_recent, the age will be reset later and ts_recent | |
835 | * * will get a valid value. If it does not, setting | |
836 | * * ts_recent to zero will at least satisfy the | |
837 | * * requirement that zero be placed in the timestamp | |
838 | * * echo reply when ts_recent isn't valid. The | |
839 | * * age isn't reset until we get a valid ts_recent | |
840 | * * because we don't want out-of-order segments to be | |
841 | * * dropped when ts_recent is old. | |
842 | * */ | |
843 | /* tp->ts_recent = 0; | |
844 | * } else { | |
845 | * tcpstat.tcps_rcvduppack++; | |
846 | * tcpstat.tcps_rcvdupbyte += ti->ti_len; | |
847 | * tcpstat.tcps_pawsdrop++; | |
848 | * goto dropafterack; | |
849 | * } | |
850 | * } | |
851 | */ | |
852 | ||
853 | todrop = tp->rcv_nxt - ti->ti_seq; | |
854 | if (todrop > 0) { | |
855 | if (tiflags & TH_SYN) { | |
856 | tiflags &= ~TH_SYN; | |
857 | ti->ti_seq++; | |
5fafdf24 | 858 | if (ti->ti_urp > 1) |
f0cbd3ec FB |
859 | ti->ti_urp--; |
860 | else | |
861 | tiflags &= ~TH_URG; | |
862 | todrop--; | |
863 | } | |
864 | /* | |
865 | * Following if statement from Stevens, vol. 2, p. 960. | |
866 | */ | |
867 | if (todrop > ti->ti_len | |
868 | || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) { | |
869 | /* | |
870 | * Any valid FIN must be to the left of the window. | |
871 | * At this point the FIN must be a duplicate or out | |
872 | * of sequence; drop it. | |
873 | */ | |
874 | tiflags &= ~TH_FIN; | |
3b46e624 | 875 | |
f0cbd3ec FB |
876 | /* |
877 | * Send an ACK to resynchronize and drop any data. | |
878 | * But keep on processing for RST or ACK. | |
879 | */ | |
880 | tp->t_flags |= TF_ACKNOW; | |
881 | todrop = ti->ti_len; | |
31a60e22 BS |
882 | STAT(tcpstat.tcps_rcvduppack++); |
883 | STAT(tcpstat.tcps_rcvdupbyte += todrop); | |
f0cbd3ec | 884 | } else { |
31a60e22 BS |
885 | STAT(tcpstat.tcps_rcvpartduppack++); |
886 | STAT(tcpstat.tcps_rcvpartdupbyte += todrop); | |
f0cbd3ec FB |
887 | } |
888 | m_adj(m, todrop); | |
889 | ti->ti_seq += todrop; | |
890 | ti->ti_len -= todrop; | |
891 | if (ti->ti_urp > todrop) | |
892 | ti->ti_urp -= todrop; | |
893 | else { | |
894 | tiflags &= ~TH_URG; | |
895 | ti->ti_urp = 0; | |
896 | } | |
897 | } | |
898 | /* | |
899 | * If new data are received on a connection after the | |
900 | * user processes are gone, then RST the other end. | |
901 | */ | |
902 | if ((so->so_state & SS_NOFDREF) && | |
903 | tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { | |
904 | tp = tcp_close(tp); | |
31a60e22 | 905 | STAT(tcpstat.tcps_rcvafterclose++); |
f0cbd3ec FB |
906 | goto dropwithreset; |
907 | } | |
908 | ||
909 | /* | |
910 | * If segment ends after window, drop trailing data | |
911 | * (and PUSH and FIN); if nothing left, just ACK. | |
912 | */ | |
913 | todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); | |
914 | if (todrop > 0) { | |
31a60e22 | 915 | STAT(tcpstat.tcps_rcvpackafterwin++); |
f0cbd3ec | 916 | if (todrop >= ti->ti_len) { |
31a60e22 | 917 | STAT(tcpstat.tcps_rcvbyteafterwin += ti->ti_len); |
f0cbd3ec FB |
918 | /* |
919 | * If a new connection request is received | |
920 | * while in TIME_WAIT, drop the old connection | |
921 | * and start over if the sequence numbers | |
922 | * are above the previous ones. | |
923 | */ | |
924 | if (tiflags & TH_SYN && | |
925 | tp->t_state == TCPS_TIME_WAIT && | |
926 | SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { | |
927 | iss = tp->rcv_nxt + TCP_ISSINCR; | |
928 | tp = tcp_close(tp); | |
929 | goto findso; | |
930 | } | |
931 | /* | |
932 | * If window is closed can only take segments at | |
933 | * window edge, and have to drop data and PUSH from | |
934 | * incoming segments. Continue processing, but | |
935 | * remember to ack. Otherwise, drop segment | |
936 | * and ack. | |
937 | */ | |
938 | if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { | |
939 | tp->t_flags |= TF_ACKNOW; | |
31a60e22 | 940 | STAT(tcpstat.tcps_rcvwinprobe++); |
f0cbd3ec FB |
941 | } else |
942 | goto dropafterack; | |
943 | } else | |
31a60e22 | 944 | STAT(tcpstat.tcps_rcvbyteafterwin += todrop); |
f0cbd3ec FB |
945 | m_adj(m, -todrop); |
946 | ti->ti_len -= todrop; | |
947 | tiflags &= ~(TH_PUSH|TH_FIN); | |
948 | } | |
949 | ||
950 | /* | |
951 | * If last ACK falls within this segment's sequence numbers, | |
952 | * record its timestamp. | |
953 | */ | |
954 | /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && | |
955 | * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len + | |
956 | * ((tiflags & (TH_SYN|TH_FIN)) != 0))) { | |
957 | * tp->ts_recent_age = tcp_now; | |
958 | * tp->ts_recent = ts_val; | |
959 | * } | |
960 | */ | |
961 | ||
962 | /* | |
963 | * If the RST bit is set examine the state: | |
964 | * SYN_RECEIVED STATE: | |
965 | * If passive open, return to LISTEN state. | |
966 | * If active open, inform user that connection was refused. | |
967 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: | |
968 | * Inform user that connection was reset, and close tcb. | |
969 | * CLOSING, LAST_ACK, TIME_WAIT STATES | |
970 | * Close the tcb. | |
971 | */ | |
972 | if (tiflags&TH_RST) switch (tp->t_state) { | |
973 | ||
974 | case TCPS_SYN_RECEIVED: | |
975 | /* so->so_error = ECONNREFUSED; */ | |
976 | goto close; | |
977 | ||
978 | case TCPS_ESTABLISHED: | |
979 | case TCPS_FIN_WAIT_1: | |
980 | case TCPS_FIN_WAIT_2: | |
981 | case TCPS_CLOSE_WAIT: | |
982 | /* so->so_error = ECONNRESET; */ | |
983 | close: | |
984 | tp->t_state = TCPS_CLOSED; | |
31a60e22 | 985 | STAT(tcpstat.tcps_drops++); |
f0cbd3ec FB |
986 | tp = tcp_close(tp); |
987 | goto drop; | |
988 | ||
989 | case TCPS_CLOSING: | |
990 | case TCPS_LAST_ACK: | |
991 | case TCPS_TIME_WAIT: | |
992 | tp = tcp_close(tp); | |
993 | goto drop; | |
994 | } | |
995 | ||
996 | /* | |
997 | * If a SYN is in the window, then this is an | |
998 | * error and we send an RST and drop the connection. | |
999 | */ | |
1000 | if (tiflags & TH_SYN) { | |
1001 | tp = tcp_drop(tp,0); | |
1002 | goto dropwithreset; | |
1003 | } | |
1004 | ||
1005 | /* | |
1006 | * If the ACK bit is off we drop the segment and return. | |
1007 | */ | |
1008 | if ((tiflags & TH_ACK) == 0) goto drop; | |
1009 | ||
1010 | /* | |
1011 | * Ack processing. | |
1012 | */ | |
1013 | switch (tp->t_state) { | |
1014 | /* | |
1015 | * In SYN_RECEIVED state if the ack ACKs our SYN then enter | |
1016 | * ESTABLISHED state and continue processing, otherwise | |
1017 | * send an RST. una<=ack<=max | |
1018 | */ | |
1019 | case TCPS_SYN_RECEIVED: | |
1020 | ||
1021 | if (SEQ_GT(tp->snd_una, ti->ti_ack) || | |
1022 | SEQ_GT(ti->ti_ack, tp->snd_max)) | |
1023 | goto dropwithreset; | |
31a60e22 | 1024 | STAT(tcpstat.tcps_connects++); |
f0cbd3ec | 1025 | tp->t_state = TCPS_ESTABLISHED; |
5fafdf24 TS |
1026 | /* |
1027 | * The sent SYN is ack'ed with our sequence number +1 | |
1028 | * The first data byte already in the buffer will get | |
f0cbd3ec FB |
1029 | * lost if no correction is made. This is only needed for |
1030 | * SS_CTL since the buffer is empty otherwise. | |
3b46e624 | 1031 | * tp->snd_una++; or: |
f0cbd3ec FB |
1032 | */ |
1033 | tp->snd_una=ti->ti_ack; | |
1034 | if (so->so_state & SS_CTL) { | |
1035 | /* So tcp_ctl reports the right state */ | |
1036 | ret = tcp_ctl(so); | |
1037 | if (ret == 1) { | |
1038 | soisfconnected(so); | |
1039 | so->so_state &= ~SS_CTL; /* success XXX */ | |
1040 | } else if (ret == 2) { | |
1041 | so->so_state = SS_NOFDREF; /* CTL_CMD */ | |
1042 | } else { | |
1043 | needoutput = 1; | |
1044 | tp->t_state = TCPS_FIN_WAIT_1; | |
1045 | } | |
1046 | } else { | |
1047 | soisfconnected(so); | |
1048 | } | |
3b46e624 | 1049 | |
f0cbd3ec FB |
1050 | /* Do window scaling? */ |
1051 | /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == | |
1052 | * (TF_RCVD_SCALE|TF_REQ_SCALE)) { | |
1053 | * tp->snd_scale = tp->requested_s_scale; | |
1054 | * tp->rcv_scale = tp->request_r_scale; | |
1055 | * } | |
1056 | */ | |
1057 | (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0); | |
1058 | tp->snd_wl1 = ti->ti_seq - 1; | |
1059 | /* Avoid ack processing; snd_una==ti_ack => dup ack */ | |
1060 | goto synrx_to_est; | |
1061 | /* fall into ... */ | |
1062 | ||
1063 | /* | |
1064 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range | |
1065 | * ACKs. If the ack is in the range | |
1066 | * tp->snd_una < ti->ti_ack <= tp->snd_max | |
1067 | * then advance tp->snd_una to ti->ti_ack and drop | |
1068 | * data from the retransmission queue. If this ACK reflects | |
1069 | * more up to date window information we update our window information. | |
1070 | */ | |
1071 | case TCPS_ESTABLISHED: | |
1072 | case TCPS_FIN_WAIT_1: | |
1073 | case TCPS_FIN_WAIT_2: | |
1074 | case TCPS_CLOSE_WAIT: | |
1075 | case TCPS_CLOSING: | |
1076 | case TCPS_LAST_ACK: | |
1077 | case TCPS_TIME_WAIT: | |
1078 | ||
1079 | if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { | |
1080 | if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { | |
31a60e22 | 1081 | STAT(tcpstat.tcps_rcvdupack++); |
f0cbd3ec FB |
1082 | DEBUG_MISC((dfd," dup ack m = %lx so = %lx \n", |
1083 | (long )m, (long )so)); | |
1084 | /* | |
1085 | * If we have outstanding data (other than | |
1086 | * a window probe), this is a completely | |
1087 | * duplicate ack (ie, window info didn't | |
1088 | * change), the ack is the biggest we've | |
1089 | * seen and we've seen exactly our rexmt | |
1090 | * threshold of them, assume a packet | |
1091 | * has been dropped and retransmit it. | |
1092 | * Kludge snd_nxt & the congestion | |
1093 | * window so we send only this one | |
1094 | * packet. | |
1095 | * | |
1096 | * We know we're losing at the current | |
1097 | * window size so do congestion avoidance | |
1098 | * (set ssthresh to half the current window | |
1099 | * and pull our congestion window back to | |
1100 | * the new ssthresh). | |
1101 | * | |
1102 | * Dup acks mean that packets have left the | |
5fafdf24 | 1103 | * network (they're now cached at the receiver) |
f0cbd3ec FB |
1104 | * so bump cwnd by the amount in the receiver |
1105 | * to keep a constant cwnd packets in the | |
1106 | * network. | |
1107 | */ | |
1108 | if (tp->t_timer[TCPT_REXMT] == 0 || | |
1109 | ti->ti_ack != tp->snd_una) | |
1110 | tp->t_dupacks = 0; | |
9634d903 | 1111 | else if (++tp->t_dupacks == TCPREXMTTHRESH) { |
f0cbd3ec FB |
1112 | tcp_seq onxt = tp->snd_nxt; |
1113 | u_int win = | |
1114 | min(tp->snd_wnd, tp->snd_cwnd) / 2 / | |
1115 | tp->t_maxseg; | |
1116 | ||
1117 | if (win < 2) | |
1118 | win = 2; | |
1119 | tp->snd_ssthresh = win * tp->t_maxseg; | |
1120 | tp->t_timer[TCPT_REXMT] = 0; | |
1121 | tp->t_rtt = 0; | |
1122 | tp->snd_nxt = ti->ti_ack; | |
1123 | tp->snd_cwnd = tp->t_maxseg; | |
1124 | (void) tcp_output(tp); | |
1125 | tp->snd_cwnd = tp->snd_ssthresh + | |
1126 | tp->t_maxseg * tp->t_dupacks; | |
1127 | if (SEQ_GT(onxt, tp->snd_nxt)) | |
1128 | tp->snd_nxt = onxt; | |
1129 | goto drop; | |
9634d903 | 1130 | } else if (tp->t_dupacks > TCPREXMTTHRESH) { |
f0cbd3ec FB |
1131 | tp->snd_cwnd += tp->t_maxseg; |
1132 | (void) tcp_output(tp); | |
1133 | goto drop; | |
1134 | } | |
1135 | } else | |
1136 | tp->t_dupacks = 0; | |
1137 | break; | |
1138 | } | |
1139 | synrx_to_est: | |
1140 | /* | |
1141 | * If the congestion window was inflated to account | |
1142 | * for the other side's cached packets, retract it. | |
1143 | */ | |
9634d903 | 1144 | if (tp->t_dupacks > TCPREXMTTHRESH && |
f0cbd3ec FB |
1145 | tp->snd_cwnd > tp->snd_ssthresh) |
1146 | tp->snd_cwnd = tp->snd_ssthresh; | |
1147 | tp->t_dupacks = 0; | |
1148 | if (SEQ_GT(ti->ti_ack, tp->snd_max)) { | |
31a60e22 | 1149 | STAT(tcpstat.tcps_rcvacktoomuch++); |
f0cbd3ec FB |
1150 | goto dropafterack; |
1151 | } | |
1152 | acked = ti->ti_ack - tp->snd_una; | |
31a60e22 BS |
1153 | STAT(tcpstat.tcps_rcvackpack++); |
1154 | STAT(tcpstat.tcps_rcvackbyte += acked); | |
f0cbd3ec FB |
1155 | |
1156 | /* | |
1157 | * If we have a timestamp reply, update smoothed | |
1158 | * round trip time. If no timestamp is present but | |
1159 | * transmit timer is running and timed sequence | |
1160 | * number was acked, update smoothed round trip time. | |
1161 | * Since we now have an rtt measurement, cancel the | |
1162 | * timer backoff (cf., Phil Karn's retransmit alg.). | |
1163 | * Recompute the initial retransmit timer. | |
1164 | */ | |
1165 | /* if (ts_present) | |
1166 | * tcp_xmit_timer(tp, tcp_now-ts_ecr+1); | |
1167 | * else | |
3b46e624 | 1168 | */ |
f0cbd3ec FB |
1169 | if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) |
1170 | tcp_xmit_timer(tp,tp->t_rtt); | |
1171 | ||
1172 | /* | |
1173 | * If all outstanding data is acked, stop retransmit | |
1174 | * timer and remember to restart (more output or persist). | |
1175 | * If there is more data to be acked, restart retransmit | |
1176 | * timer, using current (possibly backed-off) value. | |
1177 | */ | |
1178 | if (ti->ti_ack == tp->snd_max) { | |
1179 | tp->t_timer[TCPT_REXMT] = 0; | |
1180 | needoutput = 1; | |
1181 | } else if (tp->t_timer[TCPT_PERSIST] == 0) | |
1182 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; | |
1183 | /* | |
1184 | * When new data is acked, open the congestion window. | |
1185 | * If the window gives us less than ssthresh packets | |
1186 | * in flight, open exponentially (maxseg per packet). | |
1187 | * Otherwise open linearly: maxseg per window | |
1188 | * (maxseg^2 / cwnd per packet). | |
1189 | */ | |
1190 | { | |
1191 | register u_int cw = tp->snd_cwnd; | |
1192 | register u_int incr = tp->t_maxseg; | |
1193 | ||
1194 | if (cw > tp->snd_ssthresh) | |
1195 | incr = incr * incr / cw; | |
1196 | tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); | |
1197 | } | |
1198 | if (acked > so->so_snd.sb_cc) { | |
1199 | tp->snd_wnd -= so->so_snd.sb_cc; | |
1200 | sbdrop(&so->so_snd, (int )so->so_snd.sb_cc); | |
1201 | ourfinisacked = 1; | |
1202 | } else { | |
1203 | sbdrop(&so->so_snd, acked); | |
1204 | tp->snd_wnd -= acked; | |
1205 | ourfinisacked = 0; | |
1206 | } | |
1207 | /* | |
1208 | * XXX sowwakup is called when data is acked and there's room for | |
5fafdf24 | 1209 | * for more data... it should read() the socket |
f0cbd3ec FB |
1210 | */ |
1211 | /* if (so->so_snd.sb_flags & SB_NOTIFY) | |
1212 | * sowwakeup(so); | |
1213 | */ | |
1214 | tp->snd_una = ti->ti_ack; | |
1215 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) | |
1216 | tp->snd_nxt = tp->snd_una; | |
1217 | ||
1218 | switch (tp->t_state) { | |
1219 | ||
1220 | /* | |
1221 | * In FIN_WAIT_1 STATE in addition to the processing | |
1222 | * for the ESTABLISHED state if our FIN is now acknowledged | |
1223 | * then enter FIN_WAIT_2. | |
1224 | */ | |
1225 | case TCPS_FIN_WAIT_1: | |
1226 | if (ourfinisacked) { | |
1227 | /* | |
1228 | * If we can't receive any more | |
1229 | * data, then closing user can proceed. | |
1230 | * Starting the timer is contrary to the | |
1231 | * specification, but if we don't get a FIN | |
1232 | * we'll hang forever. | |
1233 | */ | |
1234 | if (so->so_state & SS_FCANTRCVMORE) { | |
1235 | soisfdisconnected(so); | |
9634d903 | 1236 | tp->t_timer[TCPT_2MSL] = TCP_MAXIDLE; |
f0cbd3ec FB |
1237 | } |
1238 | tp->t_state = TCPS_FIN_WAIT_2; | |
1239 | } | |
1240 | break; | |
1241 | ||
1242 | /* | |
1243 | * In CLOSING STATE in addition to the processing for | |
1244 | * the ESTABLISHED state if the ACK acknowledges our FIN | |
1245 | * then enter the TIME-WAIT state, otherwise ignore | |
1246 | * the segment. | |
1247 | */ | |
1248 | case TCPS_CLOSING: | |
1249 | if (ourfinisacked) { | |
1250 | tp->t_state = TCPS_TIME_WAIT; | |
1251 | tcp_canceltimers(tp); | |
1252 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
1253 | soisfdisconnected(so); | |
1254 | } | |
1255 | break; | |
1256 | ||
1257 | /* | |
1258 | * In LAST_ACK, we may still be waiting for data to drain | |
1259 | * and/or to be acked, as well as for the ack of our FIN. | |
1260 | * If our FIN is now acknowledged, delete the TCB, | |
1261 | * enter the closed state and return. | |
1262 | */ | |
1263 | case TCPS_LAST_ACK: | |
1264 | if (ourfinisacked) { | |
1265 | tp = tcp_close(tp); | |
1266 | goto drop; | |
1267 | } | |
1268 | break; | |
1269 | ||
1270 | /* | |
1271 | * In TIME_WAIT state the only thing that should arrive | |
1272 | * is a retransmission of the remote FIN. Acknowledge | |
1273 | * it and restart the finack timer. | |
1274 | */ | |
1275 | case TCPS_TIME_WAIT: | |
1276 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
1277 | goto dropafterack; | |
1278 | } | |
1279 | } /* switch(tp->t_state) */ | |
1280 | ||
1281 | step6: | |
1282 | /* | |
1283 | * Update window information. | |
1284 | * Don't look at window if no ACK: TAC's send garbage on first SYN. | |
1285 | */ | |
1286 | if ((tiflags & TH_ACK) && | |
5fafdf24 | 1287 | (SEQ_LT(tp->snd_wl1, ti->ti_seq) || |
f0cbd3ec FB |
1288 | (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || |
1289 | (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { | |
1290 | /* keep track of pure window updates */ | |
1291 | if (ti->ti_len == 0 && | |
1292 | tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) | |
31a60e22 | 1293 | STAT(tcpstat.tcps_rcvwinupd++); |
f0cbd3ec FB |
1294 | tp->snd_wnd = tiwin; |
1295 | tp->snd_wl1 = ti->ti_seq; | |
1296 | tp->snd_wl2 = ti->ti_ack; | |
1297 | if (tp->snd_wnd > tp->max_sndwnd) | |
1298 | tp->max_sndwnd = tp->snd_wnd; | |
1299 | needoutput = 1; | |
1300 | } | |
1301 | ||
1302 | /* | |
1303 | * Process segments with URG. | |
1304 | */ | |
1305 | if ((tiflags & TH_URG) && ti->ti_urp && | |
1306 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
1307 | /* | |
1308 | * This is a kludge, but if we receive and accept | |
1309 | * random urgent pointers, we'll crash in | |
1310 | * soreceive. It's hard to imagine someone | |
1311 | * actually wanting to send this much urgent data. | |
1312 | */ | |
1313 | if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) { | |
1314 | ti->ti_urp = 0; | |
1315 | tiflags &= ~TH_URG; | |
1316 | goto dodata; | |
1317 | } | |
1318 | /* | |
1319 | * If this segment advances the known urgent pointer, | |
1320 | * then mark the data stream. This should not happen | |
1321 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since | |
5fafdf24 | 1322 | * a FIN has been received from the remote side. |
f0cbd3ec FB |
1323 | * In these states we ignore the URG. |
1324 | * | |
1325 | * According to RFC961 (Assigned Protocols), | |
1326 | * the urgent pointer points to the last octet | |
1327 | * of urgent data. We continue, however, | |
1328 | * to consider it to indicate the first octet | |
5fafdf24 | 1329 | * of data past the urgent section as the original |
f0cbd3ec FB |
1330 | * spec states (in one of two places). |
1331 | */ | |
1332 | if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { | |
1333 | tp->rcv_up = ti->ti_seq + ti->ti_urp; | |
1334 | so->so_urgc = so->so_rcv.sb_cc + | |
1335 | (tp->rcv_up - tp->rcv_nxt); /* -1; */ | |
1336 | tp->rcv_up = ti->ti_seq + ti->ti_urp; | |
3b46e624 | 1337 | |
f0cbd3ec FB |
1338 | } |
1339 | } else | |
1340 | /* | |
1341 | * If no out of band data is expected, | |
1342 | * pull receive urgent pointer along | |
1343 | * with the receive window. | |
1344 | */ | |
1345 | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) | |
1346 | tp->rcv_up = tp->rcv_nxt; | |
1347 | dodata: | |
1348 | ||
1349 | /* | |
1350 | * Process the segment text, merging it into the TCP sequencing queue, | |
1351 | * and arranging for acknowledgment of receipt if necessary. | |
1352 | * This process logically involves adjusting tp->rcv_wnd as data | |
1353 | * is presented to the user (this happens in tcp_usrreq.c, | |
1354 | * case PRU_RCVD). If a FIN has already been received on this | |
1355 | * connection then we just ignore the text. | |
1356 | */ | |
1357 | if ((ti->ti_len || (tiflags&TH_FIN)) && | |
1358 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
1359 | TCP_REASS(tp, ti, m, so, tiflags); | |
1360 | /* | |
1361 | * Note the amount of data that peer has sent into | |
1362 | * our window, in order to estimate the sender's | |
1363 | * buffer size. | |
1364 | */ | |
1365 | len = so->so_rcv.sb_datalen - (tp->rcv_adv - tp->rcv_nxt); | |
1366 | } else { | |
1367 | m_free(m); | |
1368 | tiflags &= ~TH_FIN; | |
1369 | } | |
1370 | ||
1371 | /* | |
1372 | * If FIN is received ACK the FIN and let the user know | |
1373 | * that the connection is closing. | |
1374 | */ | |
1375 | if (tiflags & TH_FIN) { | |
1376 | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
1377 | /* | |
1378 | * If we receive a FIN we can't send more data, | |
1379 | * set it SS_FDRAIN | |
1380 | * Shutdown the socket if there is no rx data in the | |
1381 | * buffer. | |
1382 | * soread() is called on completion of shutdown() and | |
1383 | * will got to TCPS_LAST_ACK, and use tcp_output() | |
1384 | * to send the FIN. | |
1385 | */ | |
1386 | /* sofcantrcvmore(so); */ | |
1387 | sofwdrain(so); | |
3b46e624 | 1388 | |
f0cbd3ec FB |
1389 | tp->t_flags |= TF_ACKNOW; |
1390 | tp->rcv_nxt++; | |
1391 | } | |
1392 | switch (tp->t_state) { | |
1393 | ||
1394 | /* | |
1395 | * In SYN_RECEIVED and ESTABLISHED STATES | |
1396 | * enter the CLOSE_WAIT state. | |
1397 | */ | |
1398 | case TCPS_SYN_RECEIVED: | |
1399 | case TCPS_ESTABLISHED: | |
1400 | if(so->so_emu == EMU_CTL) /* no shutdown on socket */ | |
1401 | tp->t_state = TCPS_LAST_ACK; | |
5fafdf24 | 1402 | else |
f0cbd3ec FB |
1403 | tp->t_state = TCPS_CLOSE_WAIT; |
1404 | break; | |
1405 | ||
1406 | /* | |
1407 | * If still in FIN_WAIT_1 STATE FIN has not been acked so | |
1408 | * enter the CLOSING state. | |
1409 | */ | |
1410 | case TCPS_FIN_WAIT_1: | |
1411 | tp->t_state = TCPS_CLOSING; | |
1412 | break; | |
1413 | ||
1414 | /* | |
1415 | * In FIN_WAIT_2 state enter the TIME_WAIT state, | |
5fafdf24 | 1416 | * starting the time-wait timer, turning off the other |
f0cbd3ec FB |
1417 | * standard timers. |
1418 | */ | |
1419 | case TCPS_FIN_WAIT_2: | |
1420 | tp->t_state = TCPS_TIME_WAIT; | |
1421 | tcp_canceltimers(tp); | |
1422 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
1423 | soisfdisconnected(so); | |
1424 | break; | |
1425 | ||
1426 | /* | |
1427 | * In TIME_WAIT state restart the 2 MSL time_wait timer. | |
1428 | */ | |
1429 | case TCPS_TIME_WAIT: | |
1430 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
1431 | break; | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | /* | |
1436 | * If this is a small packet, then ACK now - with Nagel | |
1437 | * congestion avoidance sender won't send more until | |
1438 | * he gets an ACK. | |
5fafdf24 | 1439 | * |
f0cbd3ec FB |
1440 | * See above. |
1441 | */ | |
1442 | /* if (ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg) { | |
1443 | */ | |
1444 | /* if ((ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg && | |
1445 | * (so->so_iptos & IPTOS_LOWDELAY) == 0) || | |
1446 | * ((so->so_iptos & IPTOS_LOWDELAY) && | |
1447 | * ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) { | |
1448 | */ | |
1449 | if (ti->ti_len && (unsigned)ti->ti_len <= 5 && | |
1450 | ((struct tcpiphdr_2 *)ti)->first_char == (char)27) { | |
1451 | tp->t_flags |= TF_ACKNOW; | |
1452 | } | |
1453 | ||
1454 | /* | |
1455 | * Return any desired output. | |
1456 | */ | |
1457 | if (needoutput || (tp->t_flags & TF_ACKNOW)) { | |
1458 | (void) tcp_output(tp); | |
1459 | } | |
1460 | return; | |
1461 | ||
1462 | dropafterack: | |
1463 | /* | |
1464 | * Generate an ACK dropping incoming segment if it occupies | |
1465 | * sequence space, where the ACK reflects our state. | |
1466 | */ | |
1467 | if (tiflags & TH_RST) | |
1468 | goto drop; | |
1469 | m_freem(m); | |
1470 | tp->t_flags |= TF_ACKNOW; | |
1471 | (void) tcp_output(tp); | |
1472 | return; | |
1473 | ||
1474 | dropwithreset: | |
1475 | /* reuses m if m!=NULL, m_free() unnecessary */ | |
1476 | if (tiflags & TH_ACK) | |
1477 | tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); | |
1478 | else { | |
1479 | if (tiflags & TH_SYN) ti->ti_len++; | |
1480 | tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, | |
1481 | TH_RST|TH_ACK); | |
1482 | } | |
1483 | ||
1484 | return; | |
1485 | ||
1486 | drop: | |
1487 | /* | |
1488 | * Drop space held by incoming segment and return. | |
1489 | */ | |
1490 | m_free(m); | |
1491 | ||
1492 | return; | |
1493 | } | |
1494 | ||
1495 | /* , ts_present, ts_val, ts_ecr) */ | |
1496 | /* int *ts_present; | |
1497 | * u_int32_t *ts_val, *ts_ecr; | |
1498 | */ | |
9634d903 BS |
1499 | static void |
1500 | tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti) | |
f0cbd3ec FB |
1501 | { |
1502 | u_int16_t mss; | |
1503 | int opt, optlen; | |
1504 | ||
1505 | DEBUG_CALL("tcp_dooptions"); | |
1506 | DEBUG_ARGS((dfd," tp = %lx cnt=%i \n", (long )tp, cnt)); | |
1507 | ||
1508 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
1509 | opt = cp[0]; | |
1510 | if (opt == TCPOPT_EOL) | |
1511 | break; | |
1512 | if (opt == TCPOPT_NOP) | |
1513 | optlen = 1; | |
1514 | else { | |
1515 | optlen = cp[1]; | |
1516 | if (optlen <= 0) | |
1517 | break; | |
1518 | } | |
1519 | switch (opt) { | |
1520 | ||
1521 | default: | |
1522 | continue; | |
1523 | ||
1524 | case TCPOPT_MAXSEG: | |
1525 | if (optlen != TCPOLEN_MAXSEG) | |
1526 | continue; | |
1527 | if (!(ti->ti_flags & TH_SYN)) | |
1528 | continue; | |
1529 | memcpy((char *) &mss, (char *) cp + 2, sizeof(mss)); | |
1530 | NTOHS(mss); | |
1531 | (void) tcp_mss(tp, mss); /* sets t_maxseg */ | |
1532 | break; | |
1533 | ||
1534 | /* case TCPOPT_WINDOW: | |
1535 | * if (optlen != TCPOLEN_WINDOW) | |
1536 | * continue; | |
1537 | * if (!(ti->ti_flags & TH_SYN)) | |
1538 | * continue; | |
1539 | * tp->t_flags |= TF_RCVD_SCALE; | |
1540 | * tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); | |
1541 | * break; | |
1542 | */ | |
1543 | /* case TCPOPT_TIMESTAMP: | |
1544 | * if (optlen != TCPOLEN_TIMESTAMP) | |
1545 | * continue; | |
1546 | * *ts_present = 1; | |
1547 | * memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val)); | |
1548 | * NTOHL(*ts_val); | |
1549 | * memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr)); | |
1550 | * NTOHL(*ts_ecr); | |
1551 | * | |
5fafdf24 | 1552 | */ /* |
f0cbd3ec FB |
1553 | * * A timestamp received in a SYN makes |
1554 | * * it ok to send timestamp requests and replies. | |
1555 | * */ | |
1556 | /* if (ti->ti_flags & TH_SYN) { | |
1557 | * tp->t_flags |= TF_RCVD_TSTMP; | |
1558 | * tp->ts_recent = *ts_val; | |
1559 | * tp->ts_recent_age = tcp_now; | |
1560 | * } | |
1561 | */ break; | |
1562 | } | |
1563 | } | |
1564 | } | |
1565 | ||
1566 | ||
1567 | /* | |
1568 | * Pull out of band byte out of a segment so | |
1569 | * it doesn't appear in the user's data queue. | |
1570 | * It is still reflected in the segment length for | |
1571 | * sequencing purposes. | |
1572 | */ | |
1573 | ||
1574 | #ifdef notdef | |
1575 | ||
1576 | void | |
1577 | tcp_pulloutofband(so, ti, m) | |
1578 | struct socket *so; | |
1579 | struct tcpiphdr *ti; | |
1580 | register struct mbuf *m; | |
1581 | { | |
1582 | int cnt = ti->ti_urp - 1; | |
5fafdf24 | 1583 | |
f0cbd3ec FB |
1584 | while (cnt >= 0) { |
1585 | if (m->m_len > cnt) { | |
1586 | char *cp = mtod(m, caddr_t) + cnt; | |
1587 | struct tcpcb *tp = sototcpcb(so); | |
1588 | ||
1589 | tp->t_iobc = *cp; | |
1590 | tp->t_oobflags |= TCPOOB_HAVEDATA; | |
1591 | memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1)); | |
1592 | m->m_len--; | |
1593 | return; | |
1594 | } | |
1595 | cnt -= m->m_len; | |
1596 | m = m->m_next; /* XXX WRONG! Fix it! */ | |
1597 | if (m == 0) | |
1598 | break; | |
1599 | } | |
1600 | panic("tcp_pulloutofband"); | |
1601 | } | |
1602 | ||
1603 | #endif /* notdef */ | |
1604 | ||
1605 | /* | |
1606 | * Collect new round-trip time estimate | |
1607 | * and update averages and current timeout. | |
1608 | */ | |
1609 | ||
9634d903 BS |
1610 | static void |
1611 | tcp_xmit_timer(register struct tcpcb *tp, int rtt) | |
f0cbd3ec FB |
1612 | { |
1613 | register short delta; | |
1614 | ||
1615 | DEBUG_CALL("tcp_xmit_timer"); | |
1616 | DEBUG_ARG("tp = %lx", (long)tp); | |
1617 | DEBUG_ARG("rtt = %d", rtt); | |
5fafdf24 | 1618 | |
31a60e22 | 1619 | STAT(tcpstat.tcps_rttupdated++); |
f0cbd3ec FB |
1620 | if (tp->t_srtt != 0) { |
1621 | /* | |
1622 | * srtt is stored as fixed point with 3 bits after the | |
1623 | * binary point (i.e., scaled by 8). The following magic | |
1624 | * is equivalent to the smoothing algorithm in rfc793 with | |
1625 | * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed | |
1626 | * point). Adjust rtt to origin 0. | |
1627 | */ | |
1628 | delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); | |
1629 | if ((tp->t_srtt += delta) <= 0) | |
1630 | tp->t_srtt = 1; | |
1631 | /* | |
1632 | * We accumulate a smoothed rtt variance (actually, a | |
1633 | * smoothed mean difference), then set the retransmit | |
1634 | * timer to smoothed rtt + 4 times the smoothed variance. | |
1635 | * rttvar is stored as fixed point with 2 bits after the | |
1636 | * binary point (scaled by 4). The following is | |
1637 | * equivalent to rfc793 smoothing with an alpha of .75 | |
1638 | * (rttvar = rttvar*3/4 + |delta| / 4). This replaces | |
1639 | * rfc793's wired-in beta. | |
1640 | */ | |
1641 | if (delta < 0) | |
1642 | delta = -delta; | |
1643 | delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); | |
1644 | if ((tp->t_rttvar += delta) <= 0) | |
1645 | tp->t_rttvar = 1; | |
1646 | } else { | |
5fafdf24 | 1647 | /* |
f0cbd3ec FB |
1648 | * No rtt measurement yet - use the unsmoothed rtt. |
1649 | * Set the variance to half the rtt (so our first | |
1650 | * retransmit happens at 3*rtt). | |
1651 | */ | |
1652 | tp->t_srtt = rtt << TCP_RTT_SHIFT; | |
1653 | tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); | |
1654 | } | |
1655 | tp->t_rtt = 0; | |
1656 | tp->t_rxtshift = 0; | |
1657 | ||
1658 | /* | |
1659 | * the retransmit should happen at rtt + 4 * rttvar. | |
1660 | * Because of the way we do the smoothing, srtt and rttvar | |
1661 | * will each average +1/2 tick of bias. When we compute | |
1662 | * the retransmit timer, we want 1/2 tick of rounding and | |
1663 | * 1 extra tick because of +-1/2 tick uncertainty in the | |
1664 | * firing of the timer. The bias will give us exactly the | |
1665 | * 1.5 tick we need. But, because the bias is | |
1666 | * statistical, we have to test that we don't drop below | |
1667 | * the minimum feasible timer (which is 2 ticks). | |
1668 | */ | |
1669 | TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), | |
1670 | (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */ | |
5fafdf24 | 1671 | |
f0cbd3ec FB |
1672 | /* |
1673 | * We received an ack for a packet that wasn't retransmitted; | |
1674 | * it is probably safe to discard any error indications we've | |
1675 | * received recently. This isn't quite right, but close enough | |
1676 | * for now (a route might have failed after we sent a segment, | |
1677 | * and the return path might not be symmetrical). | |
1678 | */ | |
1679 | tp->t_softerror = 0; | |
1680 | } | |
1681 | ||
1682 | /* | |
1683 | * Determine a reasonable value for maxseg size. | |
1684 | * If the route is known, check route for mtu. | |
1685 | * If none, use an mss that can be handled on the outgoing | |
1686 | * interface without forcing IP to fragment; if bigger than | |
1687 | * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES | |
1688 | * to utilize large mbufs. If no route is found, route has no mtu, | |
1689 | * or the destination isn't local, use a default, hopefully conservative | |
1690 | * size (usually 512 or the default IP max size, but no more than the mtu | |
1691 | * of the interface), as we can't discover anything about intervening | |
1692 | * gateways or networks. We also initialize the congestion/slow start | |
1693 | * window to be a single segment if the destination isn't local. | |
1694 | * While looking at the routing entry, we also initialize other path-dependent | |
1695 | * parameters from pre-set or cached values in the routing entry. | |
1696 | */ | |
1697 | ||
1698 | int | |
511d2b14 | 1699 | tcp_mss(struct tcpcb *tp, u_int offer) |
f0cbd3ec FB |
1700 | { |
1701 | struct socket *so = tp->t_socket; | |
1702 | int mss; | |
5fafdf24 | 1703 | |
f0cbd3ec FB |
1704 | DEBUG_CALL("tcp_mss"); |
1705 | DEBUG_ARG("tp = %lx", (long)tp); | |
1706 | DEBUG_ARG("offer = %d", offer); | |
5fafdf24 | 1707 | |
9634d903 | 1708 | mss = min(IF_MTU, IF_MRU) - sizeof(struct tcpiphdr); |
f0cbd3ec FB |
1709 | if (offer) |
1710 | mss = min(mss, offer); | |
1711 | mss = max(mss, 32); | |
1712 | if (mss < tp->t_maxseg || offer != 0) | |
1713 | tp->t_maxseg = mss; | |
5fafdf24 | 1714 | |
f0cbd3ec | 1715 | tp->snd_cwnd = mss; |
5fafdf24 | 1716 | |
9634d903 BS |
1717 | sbreserve(&so->so_snd, TCP_SNDSPACE + ((TCP_SNDSPACE % mss) ? |
1718 | (mss - (TCP_SNDSPACE % mss)) : | |
1719 | 0)); | |
1720 | sbreserve(&so->so_rcv, TCP_RCVSPACE + ((TCP_RCVSPACE % mss) ? | |
1721 | (mss - (TCP_RCVSPACE % mss)) : | |
1722 | 0)); | |
5fafdf24 | 1723 | |
f0cbd3ec | 1724 | DEBUG_MISC((dfd, " returning mss = %d\n", mss)); |
5fafdf24 | 1725 | |
f0cbd3ec FB |
1726 | return mss; |
1727 | } |