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