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