| Commit | Line | Data |
|---|---|---|
| 984263bc | 1 | /* |
| 66d6c637 JH |
2 | * Copyright (c) 2002, 2003, 2004 Jeffrey M. Hsu. All rights reserved. |
| 3 | * Copyright (c) 2002, 2003, 2004 The DragonFly Project. All rights reserved. | |
| 95b22adf | 4 | * |
| 66d6c637 JH |
5 | * This code is derived from software contributed to The DragonFly Project |
| 6 | * by Jeffrey M. Hsu. | |
| 95b22adf | 7 | * |
| 66d6c637 JH |
8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions | |
| 10 | * are met: | |
| 11 | * 1. Redistributions of source code must retain the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer. | |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer in the | |
| 15 | * documentation and/or other materials provided with the distribution. | |
| 16 | * 3. Neither the name of The DragonFly Project nor the names of its | |
| 17 | * contributors may be used to endorse or promote products derived | |
| 18 | * from this software without specific, prior written permission. | |
| 95b22adf | 19 | * |
| 66d6c637 JH |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 24 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 31 | * SUCH DAMAGE. | |
| 32 | */ | |
| 33 | ||
| 34 | /* | |
| 984263bc MD |
35 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 |
| 36 | * The Regents of the University of California. All rights reserved. | |
| 37 | * | |
| 38 | * Redistribution and use in source and binary forms, with or without | |
| 39 | * modification, are permitted provided that the following conditions | |
| 40 | * are met: | |
| 41 | * 1. Redistributions of source code must retain the above copyright | |
| 42 | * notice, this list of conditions and the following disclaimer. | |
| 43 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 44 | * notice, this list of conditions and the following disclaimer in the | |
| 45 | * documentation and/or other materials provided with the distribution. | |
| 46 | * 3. All advertising materials mentioning features or use of this software | |
| 47 | * must display the following acknowledgement: | |
| 48 | * This product includes software developed by the University of | |
| 49 | * California, Berkeley and its contributors. | |
| 50 | * 4. Neither the name of the University nor the names of its contributors | |
| 51 | * may be used to endorse or promote products derived from this software | |
| 52 | * without specific prior written permission. | |
| 53 | * | |
| 54 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 55 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 56 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 57 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 58 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 59 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 60 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 64 | * SUCH DAMAGE. | |
| 65 | * | |
| 66 | * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 | |
| 67 | * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.38 2003/05/21 04:46:41 cjc Exp $ | |
| 68 | */ | |
| 69 | ||
| b1992928 | 70 | #include "opt_inet.h" |
| 984263bc MD |
71 | #include "opt_inet6.h" |
| 72 | #include "opt_ipsec.h" | |
| 73 | #include "opt_tcpdebug.h" | |
| 74 | #include "opt_tcp_input.h" | |
| 75 | ||
| 76 | #include <sys/param.h> | |
| 77 | #include <sys/systm.h> | |
| 78 | #include <sys/kernel.h> | |
| 79 | #include <sys/sysctl.h> | |
| 80 | #include <sys/malloc.h> | |
| 81 | #include <sys/mbuf.h> | |
| 82 | #include <sys/proc.h> /* for proc0 declaration */ | |
| 83 | #include <sys/protosw.h> | |
| 84 | #include <sys/socket.h> | |
| 85 | #include <sys/socketvar.h> | |
| 86 | #include <sys/syslog.h> | |
| 3f9db7f8 | 87 | #include <sys/in_cksum.h> |
| 984263bc | 88 | |
| 6cef7136 MD |
89 | #include <sys/socketvar2.h> |
| 90 | ||
| 984263bc | 91 | #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ |
| a00138cb | 92 | #include <machine/stdarg.h> |
| 984263bc MD |
93 | |
| 94 | #include <net/if.h> | |
| 95 | #include <net/route.h> | |
| 96 | ||
| 97 | #include <netinet/in.h> | |
| 98 | #include <netinet/in_systm.h> | |
| 99 | #include <netinet/ip.h> | |
| 95b22adf | 100 | #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ |
| 984263bc | 101 | #include <netinet/in_var.h> |
| 95b22adf | 102 | #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ |
| 984263bc MD |
103 | #include <netinet/in_pcb.h> |
| 104 | #include <netinet/ip_var.h> | |
| 105 | #include <netinet/ip6.h> | |
| 106 | #include <netinet/icmp6.h> | |
| 107 | #include <netinet6/nd6.h> | |
| 108 | #include <netinet6/ip6_var.h> | |
| 109 | #include <netinet6/in6_pcb.h> | |
| 110 | #include <netinet/tcp.h> | |
| 111 | #include <netinet/tcp_fsm.h> | |
| 112 | #include <netinet/tcp_seq.h> | |
| 113 | #include <netinet/tcp_timer.h> | |
| a48c5dd5 | 114 | #include <netinet/tcp_timer2.h> |
| 984263bc MD |
115 | #include <netinet/tcp_var.h> |
| 116 | #include <netinet6/tcp6_var.h> | |
| 117 | #include <netinet/tcpip.h> | |
| 95b22adf | 118 | |
| 984263bc MD |
119 | #ifdef TCPDEBUG |
| 120 | #include <netinet/tcp_debug.h> | |
| 121 | ||
| 95b22adf | 122 | u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */ |
| 984263bc | 123 | struct tcphdr tcp_savetcp; |
| 95b22adf | 124 | #endif |
| 984263bc MD |
125 | |
| 126 | #ifdef FAST_IPSEC | |
| bf844ffa JH |
127 | #include <netproto/ipsec/ipsec.h> |
| 128 | #include <netproto/ipsec/ipsec6.h> | |
| 984263bc MD |
129 | #endif |
| 130 | ||
| 131 | #ifdef IPSEC | |
| 132 | #include <netinet6/ipsec.h> | |
| 133 | #include <netinet6/ipsec6.h> | |
| d2438d69 | 134 | #include <netproto/key/key.h> |
| 95b22adf | 135 | #endif |
| 984263bc | 136 | |
| 984263bc MD |
137 | MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); |
| 138 | ||
| 984263bc | 139 | static int log_in_vain = 0; |
| d24ce1dc | 140 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, |
| 984263bc MD |
141 | &log_in_vain, 0, "Log all incoming TCP connections"); |
| 142 | ||
| 143 | static int blackhole = 0; | |
| 144 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, | |
| 145 | &blackhole, 0, "Do not send RST when dropping refused connections"); | |
| 146 | ||
| 147 | int tcp_delack_enabled = 1; | |
| d24ce1dc JH |
148 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, |
| 149 | &tcp_delack_enabled, 0, | |
| 984263bc MD |
150 | "Delay ACK to try and piggyback it onto a data packet"); |
| 151 | ||
| 152 | #ifdef TCP_DROP_SYNFIN | |
| 153 | static int drop_synfin = 0; | |
| 154 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, | |
| 155 | &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); | |
| 156 | #endif | |
| 157 | ||
| 33abdd1c MD |
158 | static int tcp_do_limitedtransmit = 1; |
| 159 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, limitedtransmit, CTLFLAG_RW, | |
| 160 | &tcp_do_limitedtransmit, 0, "Enable RFC 3042 (Limited Transmit)"); | |
| 161 | ||
| 91489f6b | 162 | static int tcp_do_early_retransmit = 1; |
| 8819433a JH |
163 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, earlyretransmit, CTLFLAG_RW, |
| 164 | &tcp_do_early_retransmit, 0, "Early retransmit"); | |
| 165 | ||
| 8acdb67c | 166 | int tcp_aggregate_acks = 1; |
| 72b37eeb MD |
167 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, aggregate_acks, CTLFLAG_RW, |
| 168 | &tcp_aggregate_acks, 0, "Aggregate built-up acks into one ack"); | |
| 169 | ||
| efd4b327 JH |
170 | static int tcp_do_eifel_detect = 1; |
| 171 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, eifel, CTLFLAG_RW, | |
| 172 | &tcp_do_eifel_detect, 0, "Eifel detection algorithm (RFC 3522)"); | |
| 173 | ||
| 8acdb67c JH |
174 | static int tcp_do_abc = 1; |
| 175 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc, CTLFLAG_RW, | |
| 176 | &tcp_do_abc, 0, | |
| 177 | "TCP Appropriate Byte Counting (RFC 3465)"); | |
| 178 | ||
| 91489f6b | 179 | /* |
| 9de1f696 SZ |
180 | * The following value actually takes range [25ms, 250ms], |
| 181 | * given that most modern systems use 1ms ~ 10ms as the unit | |
| 182 | * of timestamp option. | |
| 183 | */ | |
| 184 | static u_int tcp_paws_tolerance = 25; | |
| 185 | SYSCTL_UINT(_net_inet_tcp, OID_AUTO, paws_tolerance, CTLFLAG_RW, | |
| 186 | &tcp_paws_tolerance, 0, "RFC1323 PAWS tolerance"); | |
| 187 | ||
| 188 | /* | |
| 91489f6b JH |
189 | * Define as tunable for easy testing with SACK on and off. |
| 190 | * Warning: do not change setting in the middle of an existing active TCP flow, | |
| 191 | * else strange things might happen to that flow. | |
| 192 | */ | |
| 6e5bbdda | 193 | int tcp_do_sack = 1; |
| 91489f6b JH |
194 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, |
| 195 | &tcp_do_sack, 0, "Enable SACK Algorithms"); | |
| 196 | ||
| 197 | int tcp_do_smartsack = 1; | |
| 198 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, smartsack, CTLFLAG_RW, | |
| 199 | &tcp_do_smartsack, 0, "Enable Smart SACK Algorithms"); | |
| 200 | ||
| 1bdfd728 SZ |
201 | int tcp_do_rescuesack = 1; |
| 202 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, rescuesack, CTLFLAG_RW, | |
| 203 | &tcp_do_rescuesack, 0, "Rescue retransmission for SACK"); | |
| 204 | ||
| c01f968f | 205 | int tcp_aggressive_rescuesack = 0; |
| a098966f SZ |
206 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, rescuesack_agg, CTLFLAG_RW, |
| 207 | &tcp_aggressive_rescuesack, 0, "Aggressive rescue retransmission for SACK"); | |
| 208 | ||
| ccb518ea SZ |
209 | static int tcp_force_sackrxt = 1; |
| 210 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, force_sackrxt, CTLFLAG_RW, | |
| 211 | &tcp_force_sackrxt, 0, "Allowed forced SACK retransmit burst"); | |
| 212 | ||
| 859bc3f7 SZ |
213 | int tcp_do_rfc6675 = 1; |
| 214 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675, CTLFLAG_RW, | |
| 215 | &tcp_do_rfc6675, 0, "Enable RFC6675"); | |
| ffe35e17 | 216 | |
| 859bc3f7 SZ |
217 | int tcp_rfc6675_rxt = 0; |
| 218 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_rxt, CTLFLAG_RW, | |
| 219 | &tcp_rfc6675_rxt, 0, "Enable RFC6675 retransmit"); | |
| 5fd89c20 | 220 | |
| 3edf7c37 RG |
221 | SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, |
| 222 | "TCP Segment Reassembly Queue"); | |
| 223 | ||
| 224 | int tcp_reass_maxseg = 0; | |
| 225 | SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RD, | |
| 226 | &tcp_reass_maxseg, 0, | |
| 227 | "Global maximum number of TCP Segments in Reassembly Queue"); | |
| 228 | ||
| 229 | int tcp_reass_qsize = 0; | |
| 230 | SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD, | |
| 231 | &tcp_reass_qsize, 0, | |
| 232 | "Global number of TCP Segments currently in Reassembly Queue"); | |
| 233 | ||
| 234 | static int tcp_reass_overflows = 0; | |
| 235 | SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD, | |
| 236 | &tcp_reass_overflows, 0, | |
| 237 | "Global number of TCP Segment Reassembly Queue Overflows"); | |
| 238 | ||
| 5b0b9fa5 PA |
239 | int tcp_do_autorcvbuf = 1; |
| 240 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, | |
| 241 | &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing"); | |
| 242 | ||
| 243 | int tcp_autorcvbuf_inc = 16*1024; | |
| 244 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, | |
| 245 | &tcp_autorcvbuf_inc, 0, | |
| 246 | "Incrementor step size of automatic receive buffer"); | |
| 247 | ||
| 46e92930 | 248 | int tcp_autorcvbuf_max = 2*1024*1024; |
| 5b0b9fa5 PA |
249 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, |
| 250 | &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer"); | |
| 251 | ||
| b3f79714 | 252 | int tcp_sosend_agglim = 3; |
| 0df7608b SZ |
253 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, sosend_agglim, CTLFLAG_RW, |
| 254 | &tcp_sosend_agglim, 0, "TCP sosend mbuf aggregation limit"); | |
| 5b0b9fa5 | 255 | |
| 0df7608b SZ |
256 | int tcp_sosend_async = 1; |
| 257 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, sosend_async, CTLFLAG_RW, | |
| 258 | &tcp_sosend_async, 0, "TCP asynchronized pru_send"); | |
| f2a3782e | 259 | |
| 12e60b57 SZ |
260 | static int tcp_ignore_redun_dsack = 1; |
| 261 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, ignore_redun_dsack, CTLFLAG_RW, | |
| 262 | &tcp_ignore_redun_dsack, 0, "Ignore redundant DSACK"); | |
| 263 | ||
| 6c1bbf57 SZ |
264 | static void tcp_dooptions(struct tcpopt *, u_char *, int, boolean_t, |
| 265 | tcp_seq); | |
| 984263bc MD |
266 | static void tcp_pulloutofband(struct socket *, |
| 267 | struct tcphdr *, struct mbuf *, int); | |
| 268 | static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, | |
| 269 | struct mbuf *); | |
| 073ec6c4 | 270 | static void tcp_xmit_timer(struct tcpcb *, int, tcp_seq); |
| 91489f6b | 271 | static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *, int); |
| ccb518ea | 272 | static void tcp_sack_rexmt(struct tcpcb *, boolean_t); |
| ffe35e17 | 273 | static boolean_t tcp_sack_limitedxmit(struct tcpcb *); |
| 01d3427a | 274 | static int tcp_rmx_msl(const struct tcpcb *); |
| 8651f7f8 | 275 | static void tcp_established(struct tcpcb *); |
| 0a832381 | 276 | static boolean_t tcp_recv_dupack(struct tcpcb *, tcp_seq, u_int); |
| 984263bc MD |
277 | |
| 278 | /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ | |
| 279 | #ifdef INET6 | |
| 280 | #define ND6_HINT(tp) \ | |
| 281 | do { \ | |
| 282 | if ((tp) && (tp)->t_inpcb && \ | |
| 61896e3c | 283 | ((tp)->t_inpcb->inp_vflag & INP_IPV6) && \ |
| 984263bc MD |
284 | (tp)->t_inpcb->in6p_route.ro_rt) \ |
| 285 | nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \ | |
| 286 | } while (0) | |
| 287 | #else | |
| 288 | #define ND6_HINT(tp) | |
| 289 | #endif | |
| 290 | ||
| 291 | /* | |
| 292 | * Indicate whether this ack should be delayed. We can delay the ack if | |
| 293 | * - delayed acks are enabled and | |
| 294 | * - there is no delayed ack timer in progress and | |
| 295 | * - our last ack wasn't a 0-sized window. We never want to delay | |
| 296 | * the ack that opens up a 0-sized window. | |
| 297 | */ | |
| 298 | #define DELAY_ACK(tp) \ | |
| a48c5dd5 | 299 | (tcp_delack_enabled && !tcp_callout_pending(tp, tp->tt_delack) && \ |
| 61896e3c | 300 | !(tp->t_flags & TF_RXWIN0SENT)) |
| 984263bc | 301 | |
| df9d7670 JH |
302 | #define acceptable_window_update(tp, th, tiwin) \ |
| 303 | (SEQ_LT(tp->snd_wl1, th->th_seq) || \ | |
| 304 | (tp->snd_wl1 == th->th_seq && \ | |
| 305 | (SEQ_LT(tp->snd_wl2, th->th_ack) || \ | |
| 306 | (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)))) | |
| 307 | ||
| ffe35e17 SZ |
308 | #define iceildiv(n, d) (((n)+(d)-1) / (d)) |
| 309 | #define need_early_retransmit(tp, ownd) \ | |
| 310 | (tcp_do_early_retransmit && \ | |
| 311 | (tcp_do_eifel_detect && (tp->t_flags & TF_RCVD_TSTMP)) && \ | |
| 4c0f4a00 | 312 | ownd < ((tp->t_rxtthresh + 1) * tp->t_maxseg) && \ |
| ffe35e17 SZ |
313 | tp->t_dupacks + 1 >= iceildiv(ownd, tp->t_maxseg) && \ |
| 314 | (!TCP_DO_SACK(tp) || ownd <= tp->t_maxseg || \ | |
| 315 | tcp_sack_has_sacked(&tp->scb, ownd - tp->t_maxseg))) | |
| 316 | ||
| 9de1f696 SZ |
317 | /* |
| 318 | * Returns TRUE, if this segment can be merged with the last | |
| 319 | * pending segment in the reassemble queue and this segment | |
| 320 | * does not overlap with the pending segment immediately | |
| 321 | * preceeding the last pending segment. | |
| 322 | */ | |
| 323 | static __inline boolean_t | |
| 324 | tcp_paws_canreasslast(const struct tcpcb *tp, const struct tcphdr *th, int tlen) | |
| 325 | { | |
| 326 | const struct tseg_qent *last, *prev; | |
| 327 | ||
| 328 | last = TAILQ_LAST(&tp->t_segq, tsegqe_head); | |
| 329 | if (last == NULL) | |
| 330 | return FALSE; | |
| 331 | ||
| 332 | /* This segment comes immediately after the last pending segment */ | |
| 04e6f08e SZ |
333 | if (last->tqe_th->th_seq + last->tqe_len == th->th_seq) { |
| 334 | if (last->tqe_th->th_flags & TH_FIN) { | |
| 335 | /* No segments should follow segment w/ FIN */ | |
| 336 | return FALSE; | |
| 337 | } | |
| 9de1f696 | 338 | return TRUE; |
| 04e6f08e | 339 | } |
| 9de1f696 SZ |
340 | |
| 341 | if (th->th_seq + tlen != last->tqe_th->th_seq) | |
| 342 | return FALSE; | |
| 343 | /* This segment comes immediately before the last pending segment */ | |
| 344 | ||
| 345 | prev = TAILQ_PREV(last, tsegqe_head, tqe_q); | |
| 346 | if (prev == NULL) { | |
| 347 | /* | |
| 348 | * No pending preceeding segment, we assume this segment | |
| 349 | * could be reassembled. | |
| 350 | */ | |
| 351 | return TRUE; | |
| 352 | } | |
| 353 | ||
| 354 | /* This segment does not overlap with the preceeding segment */ | |
| 355 | if (SEQ_GEQ(th->th_seq, prev->tqe_th->th_seq + prev->tqe_len)) | |
| 356 | return TRUE; | |
| 357 | ||
| 358 | return FALSE; | |
| 359 | } | |
| 360 | ||
| e2289e66 SZ |
361 | static __inline void |
| 362 | tcp_ncr_update_rxtthresh(struct tcpcb *tp) | |
| 363 | { | |
| 364 | int old_rxtthresh = tp->t_rxtthresh; | |
| 365 | uint32_t ownd = tp->snd_max - tp->snd_una; | |
| 366 | ||
| 367 | tp->t_rxtthresh = max(3, ((ownd / tp->t_maxseg) >> 1)); | |
| 368 | if (tp->t_rxtthresh != old_rxtthresh) { | |
| 369 | tcp_sack_update_lostseq(&tp->scb, tp->snd_una, | |
| 370 | tp->t_maxseg, tp->t_rxtthresh); | |
| 371 | } | |
| 372 | } | |
| 373 | ||
| 984263bc | 374 | static int |
| 95b22adf | 375 | tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m) |
| 984263bc MD |
376 | { |
| 377 | struct tseg_qent *q; | |
| 378 | struct tseg_qent *p = NULL; | |
| 984263bc MD |
379 | struct tseg_qent *te; |
| 380 | struct socket *so = tp->t_inpcb->inp_socket; | |
| 381 | int flags; | |
| 382 | ||
| 383 | /* | |
| 61896e3c | 384 | * Call with th == NULL after become established to |
| 984263bc MD |
385 | * force pre-ESTABLISHED data up to user socket. |
| 386 | */ | |
| 61896e3c | 387 | if (th == NULL) |
| 984263bc MD |
388 | goto present; |
| 389 | ||
| 3edf7c37 RG |
390 | /* |
| 391 | * Limit the number of segments in the reassembly queue to prevent | |
| 392 | * holding on to too many segments (and thus running out of mbufs). | |
| 393 | * Make sure to let the missing segment through which caused this | |
| 394 | * queue. Always keep one global queue entry spare to be able to | |
| 395 | * process the missing segment. | |
| 396 | */ | |
| 397 | if (th->th_seq != tp->rcv_nxt && | |
| 398 | tcp_reass_qsize + 1 >= tcp_reass_maxseg) { | |
| 399 | tcp_reass_overflows++; | |
| 400 | tcpstat.tcps_rcvmemdrop++; | |
| 401 | m_freem(m); | |
| 91489f6b JH |
402 | /* no SACK block to report */ |
| 403 | tp->reportblk.rblk_start = tp->reportblk.rblk_end; | |
| 3edf7c37 RG |
404 | return (0); |
| 405 | } | |
| 406 | ||
| ba4e3dbe | 407 | /* Allocate a new queue entry. */ |
| 884717e1 | 408 | te = kmalloc(sizeof(struct tseg_qent), M_TSEGQ, M_INTWAIT | M_NULLOK); |
| 984263bc MD |
409 | if (te == NULL) { |
| 410 | tcpstat.tcps_rcvmemdrop++; | |
| 411 | m_freem(m); | |
| 91489f6b JH |
412 | /* no SACK block to report */ |
| 413 | tp->reportblk.rblk_start = tp->reportblk.rblk_end; | |
| 984263bc MD |
414 | return (0); |
| 415 | } | |
| 2d23a8be | 416 | atomic_add_int(&tcp_reass_qsize, 1); |
| 984263bc | 417 | |
| 942d88ef SZ |
418 | if (th->th_flags & TH_FIN) |
| 419 | tp->t_flags |= TF_QUEDFIN; | |
| 420 | ||
| 984263bc MD |
421 | /* |
| 422 | * Find a segment which begins after this one does. | |
| 423 | */ | |
| 0f9e45de | 424 | TAILQ_FOREACH(q, &tp->t_segq, tqe_q) { |
| 984263bc MD |
425 | if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) |
| 426 | break; | |
| 427 | p = q; | |
| 428 | } | |
| 429 | ||
| 430 | /* | |
| 431 | * If there is a preceding segment, it may provide some of | |
| 432 | * our data already. If so, drop the data from the incoming | |
| 433 | * segment. If it provides all of our data, drop us. | |
| 434 | */ | |
| 435 | if (p != NULL) { | |
| 76cf5e41 | 436 | tcp_seq_diff_t i; |
| 91489f6b | 437 | |
| 984263bc MD |
438 | /* conversion to int (in i) handles seq wraparound */ |
| 439 | i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; | |
| 91489f6b | 440 | if (i > 0) { /* overlaps preceding segment */ |
| c7e6499a SZ |
441 | tp->sack_flags |= |
| 442 | (TSACK_F_DUPSEG | TSACK_F_ENCLOSESEG); | |
| 91489f6b JH |
443 | /* enclosing block starts w/ preceding segment */ |
| 444 | tp->encloseblk.rblk_start = p->tqe_th->th_seq; | |
| 984263bc | 445 | if (i >= *tlenp) { |
| 53432bee SZ |
446 | if (th->th_flags & TH_FIN) |
| 447 | p->tqe_th->th_flags |= TH_FIN; | |
| 448 | ||
| 91489f6b | 449 | /* preceding encloses incoming segment */ |
| 3a5d999b SZ |
450 | tp->encloseblk.rblk_end = TCP_SACK_BLKEND( |
| 451 | p->tqe_th->th_seq + p->tqe_len, | |
| 452 | p->tqe_th->th_flags); | |
| 984263bc MD |
453 | tcpstat.tcps_rcvduppack++; |
| 454 | tcpstat.tcps_rcvdupbyte += *tlenp; | |
| 455 | m_freem(m); | |
| efda3bd0 | 456 | kfree(te, M_TSEGQ); |
| 2d23a8be | 457 | atomic_add_int(&tcp_reass_qsize, -1); |
| 984263bc MD |
458 | /* |
| 459 | * Try to present any queued data | |
| 460 | * at the left window edge to the user. | |
| 461 | * This is needed after the 3-WHS | |
| 462 | * completes. | |
| 463 | */ | |
| 464 | goto present; /* ??? */ | |
| 465 | } | |
| 466 | m_adj(m, i); | |
| 467 | *tlenp -= i; | |
| 468 | th->th_seq += i; | |
| 91489f6b | 469 | /* incoming segment end is enclosing block end */ |
| 3a5d999b SZ |
470 | tp->encloseblk.rblk_end = TCP_SACK_BLKEND( |
| 471 | th->th_seq + *tlenp, th->th_flags); | |
| 91489f6b JH |
472 | /* trim end of reported D-SACK block */ |
| 473 | tp->reportblk.rblk_end = th->th_seq; | |
| 984263bc MD |
474 | } |
| 475 | } | |
| 476 | tcpstat.tcps_rcvoopack++; | |
| 477 | tcpstat.tcps_rcvoobyte += *tlenp; | |
| 478 | ||
| 479 | /* | |
| 480 | * While we overlap succeeding segments trim them or, | |
| 481 | * if they are completely covered, dequeue them. | |
| 482 | */ | |
| 483 | while (q) { | |
| 76cf5e41 | 484 | tcp_seq_diff_t i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; |
| 91489f6b | 485 | tcp_seq qend = q->tqe_th->th_seq + q->tqe_len; |
| 3a5d999b | 486 | tcp_seq qend_sack = TCP_SACK_BLKEND(qend, q->tqe_th->th_flags); |
| 91489f6b JH |
487 | struct tseg_qent *nq; |
| 488 | ||
| 984263bc MD |
489 | if (i <= 0) |
| 490 | break; | |
| c7e6499a SZ |
491 | if (!(tp->sack_flags & TSACK_F_DUPSEG)) { |
| 492 | /* first time through */ | |
| 493 | tp->sack_flags |= (TSACK_F_DUPSEG | TSACK_F_ENCLOSESEG); | |
| 91489f6b JH |
494 | tp->encloseblk = tp->reportblk; |
| 495 | /* report trailing duplicate D-SACK segment */ | |
| 496 | tp->reportblk.rblk_start = q->tqe_th->th_seq; | |
| 497 | } | |
| c7e6499a | 498 | if ((tp->sack_flags & TSACK_F_ENCLOSESEG) && |
| 3a5d999b | 499 | SEQ_GT(qend_sack, tp->encloseblk.rblk_end)) { |
| 91489f6b | 500 | /* extend enclosing block if one exists */ |
| 3a5d999b | 501 | tp->encloseblk.rblk_end = qend_sack; |
| 91489f6b | 502 | } |
| 984263bc MD |
503 | if (i < q->tqe_len) { |
| 504 | q->tqe_th->th_seq += i; | |
| 505 | q->tqe_len -= i; | |
| 506 | m_adj(q->tqe_m, i); | |
| 507 | break; | |
| 508 | } | |
| 509 | ||
| 53432bee SZ |
510 | if (q->tqe_th->th_flags & TH_FIN) |
| 511 | th->th_flags |= TH_FIN; | |
| 512 | ||
| 0f9e45de SZ |
513 | nq = TAILQ_NEXT(q, tqe_q); |
| 514 | TAILQ_REMOVE(&tp->t_segq, q, tqe_q); | |
| 984263bc | 515 | m_freem(q->tqe_m); |
| efda3bd0 | 516 | kfree(q, M_TSEGQ); |
| 2d23a8be | 517 | atomic_add_int(&tcp_reass_qsize, -1); |
| 984263bc MD |
518 | q = nq; |
| 519 | } | |
| 520 | ||
| 521 | /* Insert the new segment queue entry into place. */ | |
| 522 | te->tqe_m = m; | |
| 523 | te->tqe_th = th; | |
| 524 | te->tqe_len = *tlenp; | |
| 525 | ||
| 91489f6b JH |
526 | /* check if can coalesce with following segment */ |
| 527 | if (q != NULL && (th->th_seq + *tlenp == q->tqe_th->th_seq)) { | |
| 53432bee | 528 | tcp_seq tend_sack; |
| 91489f6b JH |
529 | |
| 530 | te->tqe_len += q->tqe_len; | |
| a174690a | 531 | if (q->tqe_th->th_flags & TH_FIN) |
| f16f8cc3 | 532 | te->tqe_th->th_flags |= TH_FIN; |
| 53432bee SZ |
533 | tend_sack = TCP_SACK_BLKEND(te->tqe_th->th_seq + te->tqe_len, |
| 534 | te->tqe_th->th_flags); | |
| 535 | ||
| 91489f6b | 536 | m_cat(te->tqe_m, q->tqe_m); |
| 3a5d999b | 537 | tp->encloseblk.rblk_end = tend_sack; |
| 91489f6b JH |
538 | /* |
| 539 | * When not reporting a duplicate segment, use | |
| 540 | * the larger enclosing block as the SACK block. | |
| 541 | */ | |
| c7e6499a | 542 | if (!(tp->sack_flags & TSACK_F_DUPSEG)) |
| 3a5d999b | 543 | tp->reportblk.rblk_end = tend_sack; |
| 0f9e45de | 544 | TAILQ_REMOVE(&tp->t_segq, q, tqe_q); |
| efda3bd0 | 545 | kfree(q, M_TSEGQ); |
| 2d23a8be | 546 | atomic_add_int(&tcp_reass_qsize, -1); |
| 91489f6b JH |
547 | } |
| 548 | ||
| 984263bc | 549 | if (p == NULL) { |
| 0f9e45de | 550 | TAILQ_INSERT_HEAD(&tp->t_segq, te, tqe_q); |
| 984263bc | 551 | } else { |
| 91489f6b JH |
552 | /* check if can coalesce with preceding segment */ |
| 553 | if (p->tqe_th->th_seq + p->tqe_len == th->th_seq) { | |
| 53432bee SZ |
554 | if (te->tqe_th->th_flags & TH_FIN) |
| 555 | p->tqe_th->th_flags |= TH_FIN; | |
| a174690a JH |
556 | p->tqe_len += te->tqe_len; |
| 557 | m_cat(p->tqe_m, te->tqe_m); | |
| 91489f6b JH |
558 | tp->encloseblk.rblk_start = p->tqe_th->th_seq; |
| 559 | /* | |
| 560 | * When not reporting a duplicate segment, use | |
| 561 | * the larger enclosing block as the SACK block. | |
| 562 | */ | |
| c7e6499a | 563 | if (!(tp->sack_flags & TSACK_F_DUPSEG)) |
| 91489f6b | 564 | tp->reportblk.rblk_start = p->tqe_th->th_seq; |
| efda3bd0 | 565 | kfree(te, M_TSEGQ); |
| 2d23a8be MD |
566 | atomic_add_int(&tcp_reass_qsize, -1); |
| 567 | } else { | |
| 0f9e45de | 568 | TAILQ_INSERT_AFTER(&tp->t_segq, p, te, tqe_q); |
| 2d23a8be | 569 | } |
| 984263bc MD |
570 | } |
| 571 | ||
| 572 | present: | |
| 573 | /* | |
| 574 | * Present data to user, advancing rcv_nxt through | |
| 575 | * completed sequence space. | |
| 576 | */ | |
| 577 | if (!TCPS_HAVEESTABLISHED(tp->t_state)) | |
| 578 | return (0); | |
| 0f9e45de | 579 | q = TAILQ_FIRST(&tp->t_segq); |
| 61896e3c | 580 | if (q == NULL || q->tqe_th->th_seq != tp->rcv_nxt) |
| 984263bc | 581 | return (0); |
| 91489f6b | 582 | tp->rcv_nxt += q->tqe_len; |
| c7e6499a | 583 | if (!(tp->sack_flags & TSACK_F_DUPSEG)) { |
| 91489f6b JH |
584 | /* no SACK block to report since ACK advanced */ |
| 585 | tp->reportblk.rblk_start = tp->reportblk.rblk_end; | |
| 586 | } | |
| 587 | /* no enclosing block to report since ACK advanced */ | |
| c7e6499a | 588 | tp->sack_flags &= ~TSACK_F_ENCLOSESEG; |
| 91489f6b | 589 | flags = q->tqe_th->th_flags & TH_FIN; |
| 0f9e45de SZ |
590 | TAILQ_REMOVE(&tp->t_segq, q, tqe_q); |
| 591 | KASSERT(TAILQ_EMPTY(&tp->t_segq) || | |
| 592 | TAILQ_FIRST(&tp->t_segq)->tqe_th->th_seq != tp->rcv_nxt, | |
| 91489f6b | 593 | ("segment not coalesced")); |
| 6cef7136 | 594 | if (so->so_state & SS_CANTRCVMORE) { |
| 91489f6b | 595 | m_freem(q->tqe_m); |
| 6cef7136 MD |
596 | } else { |
| 597 | lwkt_gettoken(&so->so_rcv.ssb_token); | |
| 6d49aa6f | 598 | ssb_appendstream(&so->so_rcv, q->tqe_m); |
| 6cef7136 MD |
599 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 600 | } | |
| efda3bd0 | 601 | kfree(q, M_TSEGQ); |
| 2d23a8be | 602 | atomic_add_int(&tcp_reass_qsize, -1); |
| 984263bc MD |
603 | ND6_HINT(tp); |
| 604 | sorwakeup(so); | |
| 605 | return (flags); | |
| 606 | } | |
| 607 | ||
| 608 | /* | |
| 609 | * TCP input routine, follows pages 65-76 of the | |
| 610 | * protocol specification dated September, 1981 very closely. | |
| 611 | */ | |
| 612 | #ifdef INET6 | |
| 613 | int | |
| 95b22adf | 614 | tcp6_input(struct mbuf **mp, int *offp, int proto) |
| 984263bc | 615 | { |
| 2256ba69 | 616 | struct mbuf *m = *mp; |
| 984263bc MD |
617 | struct in6_ifaddr *ia6; |
| 618 | ||
| 619 | IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); | |
| 620 | ||
| 621 | /* | |
| 622 | * draft-itojun-ipv6-tcp-to-anycast | |
| 623 | * better place to put this in? | |
| 624 | */ | |
| 625 | ia6 = ip6_getdstifaddr(m); | |
| d24ce1dc | 626 | if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { |
| 984263bc MD |
627 | struct ip6_hdr *ip6; |
| 628 | ||
| 629 | ip6 = mtod(m, struct ip6_hdr *); | |
| 630 | icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, | |
| f23061d4 | 631 | offsetof(struct ip6_hdr, ip6_dst)); |
| d24ce1dc | 632 | return (IPPROTO_DONE); |
| 984263bc MD |
633 | } |
| 634 | ||
| 002c1265 | 635 | tcp_input(mp, offp, proto); |
| d24ce1dc | 636 | return (IPPROTO_DONE); |
| 984263bc MD |
637 | } |
| 638 | #endif | |
| 639 | ||
| 002c1265 MD |
640 | int |
| 641 | tcp_input(struct mbuf **mp, int *offp, int proto) | |
| 984263bc | 642 | { |
| 002c1265 | 643 | int off0; |
| 2256ba69 RG |
644 | struct tcphdr *th; |
| 645 | struct ip *ip = NULL; | |
| 646 | struct ipovly *ipov; | |
| 647 | struct inpcb *inp = NULL; | |
| 984263bc MD |
648 | u_char *optp = NULL; |
| 649 | int optlen = 0; | |
| b1992928 MD |
650 | int tlen, off; |
| 651 | int len = 0; | |
| 984263bc | 652 | int drop_hdrlen; |
| 2256ba69 RG |
653 | struct tcpcb *tp = NULL; |
| 654 | int thflags; | |
| 4090d6ff | 655 | struct socket *so = NULL; |
| 61896e3c | 656 | int todrop, acked; |
| 27f4bf33 SZ |
657 | boolean_t ourfinisacked, needoutput = FALSE, delayed_dupack = FALSE; |
| 658 | tcp_seq th_dupack = 0; /* XXX gcc warning */ | |
| 0a832381 | 659 | u_int to_flags = 0; /* XXX gcc warning */ |
| 984263bc | 660 | u_long tiwin; |
| 61896e3c | 661 | int recvwin; |
| 984263bc | 662 | struct tcpopt to; /* options in this segment */ |
| 984263bc MD |
663 | struct sockaddr_in *next_hop = NULL; |
| 664 | int rstreason; /* For badport_bandlim accounting purposes */ | |
| d371a63a | 665 | int cpu; |
| 984263bc | 666 | struct ip6_hdr *ip6 = NULL; |
| 002c1265 | 667 | struct mbuf *m; |
| 984263bc | 668 | #ifdef INET6 |
| d24ce1dc | 669 | boolean_t isipv6; |
| 984263bc | 670 | #else |
| d24ce1dc | 671 | const boolean_t isipv6 = FALSE; |
| 984263bc MD |
672 | #endif |
| 673 | #ifdef TCPDEBUG | |
| 674 | short ostate = 0; | |
| 675 | #endif | |
| 676 | ||
| 002c1265 MD |
677 | off0 = *offp; |
| 678 | m = *mp; | |
| 679 | *mp = NULL; | |
| a00138cb | 680 | |
| d24ce1dc JH |
681 | tcpstat.tcps_rcvtotal++; |
| 682 | ||
| 5de23090 SZ |
683 | if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { |
| 684 | struct m_tag *mtag; | |
| 685 | ||
| 686 | mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); | |
| 687 | KKASSERT(mtag != NULL); | |
| 688 | next_hop = m_tag_data(mtag); | |
| 984263bc | 689 | } |
| d24ce1dc | 690 | |
| 984263bc | 691 | #ifdef INET6 |
| d24ce1dc | 692 | isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? TRUE : FALSE; |
| 984263bc | 693 | #endif |
| 984263bc MD |
694 | |
| 695 | if (isipv6) { | |
| 696 | /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ | |
| 697 | ip6 = mtod(m, struct ip6_hdr *); | |
| 407e896e | 698 | tlen = (sizeof *ip6) + ntohs(ip6->ip6_plen) - off0; |
| 984263bc MD |
699 | if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { |
| 700 | tcpstat.tcps_rcvbadsum++; | |
| 701 | goto drop; | |
| 702 | } | |
| 703 | th = (struct tcphdr *)((caddr_t)ip6 + off0); | |
| 704 | ||
| 705 | /* | |
| 706 | * Be proactive about unspecified IPv6 address in source. | |
| 707 | * As we use all-zero to indicate unbounded/unconnected pcb, | |
| 708 | * unspecified IPv6 address can be used to confuse us. | |
| 709 | * | |
| 710 | * Note that packets with unspecified IPv6 destination is | |
| 711 | * already dropped in ip6_input. | |
| 712 | */ | |
| 713 | if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { | |
| 714 | /* XXX stat */ | |
| 715 | goto drop; | |
| 716 | } | |
| 717 | } else { | |
| 718 | /* | |
| 719 | * Get IP and TCP header together in first mbuf. | |
| 720 | * Note: IP leaves IP header in first mbuf. | |
| 721 | */ | |
| 722 | if (off0 > sizeof(struct ip)) { | |
| bddf0751 | 723 | ip_stripoptions(m); |
| 984263bc MD |
724 | off0 = sizeof(struct ip); |
| 725 | } | |
| 55d829f8 JH |
726 | /* already checked and pulled up in ip_demux() */ |
| 727 | KASSERT(m->m_len >= sizeof(struct tcpiphdr), | |
| 61896e3c | 728 | ("TCP header not in one mbuf: m->m_len %d", m->m_len)); |
| 984263bc MD |
729 | ip = mtod(m, struct ip *); |
| 730 | ipov = (struct ipovly *)ip; | |
| 731 | th = (struct tcphdr *)((caddr_t)ip + off0); | |
| 732 | tlen = ip->ip_len; | |
| 733 | ||
| 734 | if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { | |
| 735 | if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) | |
| 736 | th->th_sum = m->m_pkthdr.csum_data; | |
| 737 | else | |
| 738 | th->th_sum = in_pseudo(ip->ip_src.s_addr, | |
| 739 | ip->ip_dst.s_addr, | |
| 740 | htonl(m->m_pkthdr.csum_data + | |
| 741 | ip->ip_len + | |
| 742 | IPPROTO_TCP)); | |
| 743 | th->th_sum ^= 0xffff; | |
| 744 | } else { | |
| 745 | /* | |
| 746 | * Checksum extended TCP header and data. | |
| 747 | */ | |
| 748 | len = sizeof(struct ip) + tlen; | |
| 407e896e | 749 | bzero(ipov->ih_x1, sizeof ipov->ih_x1); |
| 984263bc MD |
750 | ipov->ih_len = (u_short)tlen; |
| 751 | ipov->ih_len = htons(ipov->ih_len); | |
| 752 | th->th_sum = in_cksum(m, len); | |
| 753 | } | |
| 754 | if (th->th_sum) { | |
| 755 | tcpstat.tcps_rcvbadsum++; | |
| 756 | goto drop; | |
| 757 | } | |
| 758 | #ifdef INET6 | |
| 759 | /* Re-initialization for later version check */ | |
| 760 | ip->ip_v = IPVERSION; | |
| 761 | #endif | |
| 762 | } | |
| 763 | ||
| 764 | /* | |
| 765 | * Check that TCP offset makes sense, | |
| 766 | * pull out TCP options and adjust length. XXX | |
| 767 | */ | |
| 768 | off = th->th_off << 2; | |
| 55d829f8 JH |
769 | /* already checked and pulled up in ip_demux() */ |
| 770 | KASSERT(off >= sizeof(struct tcphdr) && off <= tlen, | |
| 61896e3c | 771 | ("bad TCP data offset %d (tlen %d)", off, tlen)); |
| 984263bc MD |
772 | tlen -= off; /* tlen is used instead of ti->ti_len */ |
| 773 | if (off > sizeof(struct tcphdr)) { | |
| 774 | if (isipv6) { | |
| 002c1265 | 775 | IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); |
| 984263bc MD |
776 | ip6 = mtod(m, struct ip6_hdr *); |
| 777 | th = (struct tcphdr *)((caddr_t)ip6 + off0); | |
| 778 | } else { | |
| 55d829f8 JH |
779 | /* already pulled up in ip_demux() */ |
| 780 | KASSERT(m->m_len >= sizeof(struct ip) + off, | |
| 61896e3c JH |
781 | ("TCP header and options not in one mbuf: " |
| 782 | "m_len %d, off %d", m->m_len, off)); | |
| 984263bc MD |
783 | } |
| 784 | optlen = off - sizeof(struct tcphdr); | |
| 785 | optp = (u_char *)(th + 1); | |
| 786 | } | |
| 787 | thflags = th->th_flags; | |
| 788 | ||
| 789 | #ifdef TCP_DROP_SYNFIN | |
| 790 | /* | |
| 791 | * If the drop_synfin option is enabled, drop all packets with | |
| 792 | * both the SYN and FIN bits set. This prevents e.g. nmap from | |
| 793 | * identifying the TCP/IP stack. | |
| 794 | * | |
| 795 | * This is a violation of the TCP specification. | |
| 796 | */ | |
| 61896e3c | 797 | if (drop_synfin && (thflags & (TH_SYN | TH_FIN)) == (TH_SYN | TH_FIN)) |
| 984263bc MD |
798 | goto drop; |
| 799 | #endif | |
| 800 | ||
| 801 | /* | |
| 802 | * Convert TCP protocol specific fields to host format. | |
| 803 | */ | |
| 804 | th->th_seq = ntohl(th->th_seq); | |
| 805 | th->th_ack = ntohl(th->th_ack); | |
| 806 | th->th_win = ntohs(th->th_win); | |
| 807 | th->th_urp = ntohs(th->th_urp); | |
| 808 | ||
| 809 | /* | |
| d24ce1dc | 810 | * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, |
| 984263bc MD |
811 | * until after ip6_savecontrol() is called and before other functions |
| 812 | * which don't want those proto headers. | |
| 813 | * Because ip6_savecontrol() is going to parse the mbuf to | |
| 814 | * search for data to be passed up to user-land, it wants mbuf | |
| 815 | * parameters to be unchanged. | |
| 816 | * XXX: the call of ip6_savecontrol() has been obsoleted based on | |
| 817 | * latest version of the advanced API (20020110). | |
| 818 | */ | |
| 819 | drop_hdrlen = off0 + off; | |
| 820 | ||
| 821 | /* | |
| 822 | * Locate pcb for segment. | |
| 823 | */ | |
| 824 | findpcb: | |
| 825 | /* IPFIREWALL_FORWARD section */ | |
| d24ce1dc | 826 | if (next_hop != NULL && !isipv6) { /* IPv6 support is not there yet */ |
| 984263bc MD |
827 | /* |
| 828 | * Transparently forwarded. Pretend to be the destination. | |
| d24ce1dc | 829 | * already got one like this? |
| 984263bc | 830 | */ |
| 6ca1a1cd | 831 | cpu = mycpu->gd_cpuid; |
| 6ca1a1cd | 832 | inp = in_pcblookup_hash(&tcbinfo[cpu], |
| d371a63a | 833 | ip->ip_src, th->th_sport, |
| 984263bc MD |
834 | ip->ip_dst, th->th_dport, |
| 835 | 0, m->m_pkthdr.rcvif); | |
| 836 | if (!inp) { | |
| 83be63fe JH |
837 | /* |
| 838 | * It's new. Try to find the ambushing socket. | |
| 839 | */ | |
| 840 | ||
| 841 | /* | |
| 842 | * The rest of the ipfw code stores the port in | |
| 843 | * host order. XXX | |
| 844 | * (The IP address is still in network order.) | |
| 845 | */ | |
| 846 | in_port_t dport = next_hop->sin_port ? | |
| 847 | htons(next_hop->sin_port) : | |
| 848 | th->th_dport; | |
| 849 | ||
| d371a63a | 850 | cpu = tcp_addrcpu(ip->ip_src.s_addr, th->th_sport, |
| 83be63fe | 851 | next_hop->sin_addr.s_addr, dport); |
| d371a63a | 852 | inp = in_pcblookup_hash(&tcbinfo[cpu], |
| 984263bc | 853 | ip->ip_src, th->th_sport, |
| 83be63fe | 854 | next_hop->sin_addr, dport, |
| 984263bc MD |
855 | 1, m->m_pkthdr.rcvif); |
| 856 | } | |
| 857 | } else { | |
| 6ca1a1cd | 858 | if (isipv6) { |
| d371a63a | 859 | inp = in6_pcblookup_hash(&tcbinfo[0], |
| 984263bc MD |
860 | &ip6->ip6_src, th->th_sport, |
| 861 | &ip6->ip6_dst, th->th_dport, | |
| 862 | 1, m->m_pkthdr.rcvif); | |
| 6ca1a1cd | 863 | } else { |
| 6ca1a1cd | 864 | cpu = mycpu->gd_cpuid; |
| 6ca1a1cd | 865 | inp = in_pcblookup_hash(&tcbinfo[cpu], |
| 984263bc MD |
866 | ip->ip_src, th->th_sport, |
| 867 | ip->ip_dst, th->th_dport, | |
| 868 | 1, m->m_pkthdr.rcvif); | |
| 6ca1a1cd | 869 | } |
| 984263bc | 870 | } |
| 984263bc MD |
871 | |
| 872 | /* | |
| 873 | * If the state is CLOSED (i.e., TCB does not exist) then | |
| 874 | * all data in the incoming segment is discarded. | |
| 875 | * If the TCB exists but is in CLOSED state, it is embryonic, | |
| 876 | * but should either do a listen or a connect soon. | |
| 877 | */ | |
| 878 | if (inp == NULL) { | |
| 879 | if (log_in_vain) { | |
| 880 | #ifdef INET6 | |
| 881 | char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; | |
| 882 | #else | |
| 1141eb20 JH |
883 | char dbuf[sizeof "aaa.bbb.ccc.ddd"]; |
| 884 | char sbuf[sizeof "aaa.bbb.ccc.ddd"]; | |
| 984263bc MD |
885 | #endif |
| 886 | if (isipv6) { | |
| 887 | strcpy(dbuf, "["); | |
| 984263bc | 888 | strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); |
| 984263bc | 889 | strcat(dbuf, "]"); |
| 95b22adf JH |
890 | strcpy(sbuf, "["); |
| 891 | strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); | |
| 984263bc MD |
892 | strcat(sbuf, "]"); |
| 893 | } else { | |
| 894 | strcpy(dbuf, inet_ntoa(ip->ip_dst)); | |
| 895 | strcpy(sbuf, inet_ntoa(ip->ip_src)); | |
| 896 | } | |
| 897 | switch (log_in_vain) { | |
| 898 | case 1: | |
| 61896e3c | 899 | if (!(thflags & TH_SYN)) |
| 984263bc MD |
900 | break; |
| 901 | case 2: | |
| 902 | log(LOG_INFO, | |
| 903 | "Connection attempt to TCP %s:%d " | |
| 904 | "from %s:%d flags:0x%02x\n", | |
| 905 | dbuf, ntohs(th->th_dport), sbuf, | |
| 906 | ntohs(th->th_sport), thflags); | |
| 907 | break; | |
| 908 | default: | |
| 909 | break; | |
| 910 | } | |
| 911 | } | |
| d24ce1dc | 912 | if (blackhole) { |
| 984263bc MD |
913 | switch (blackhole) { |
| 914 | case 1: | |
| 915 | if (thflags & TH_SYN) | |
| 916 | goto drop; | |
| 917 | break; | |
| 918 | case 2: | |
| 919 | goto drop; | |
| 920 | default: | |
| 921 | goto drop; | |
| 922 | } | |
| 923 | } | |
| 924 | rstreason = BANDLIM_RST_CLOSEDPORT; | |
| 925 | goto dropwithreset; | |
| 926 | } | |
| 61896e3c JH |
927 | |
| 928 | #ifdef IPSEC | |
| 929 | if (isipv6) { | |
| 930 | if (ipsec6_in_reject_so(m, inp->inp_socket)) { | |
| 931 | ipsec6stat.in_polvio++; | |
| 932 | goto drop; | |
| 933 | } | |
| 934 | } else { | |
| 935 | if (ipsec4_in_reject_so(m, inp->inp_socket)) { | |
| 936 | ipsecstat.in_polvio++; | |
| 937 | goto drop; | |
| 938 | } | |
| 939 | } | |
| 940 | #endif | |
| 941 | #ifdef FAST_IPSEC | |
| 942 | if (isipv6) { | |
| 943 | if (ipsec6_in_reject(m, inp)) | |
| 944 | goto drop; | |
| 945 | } else { | |
| 946 | if (ipsec4_in_reject(m, inp)) | |
| 947 | goto drop; | |
| 948 | } | |
| 949 | #endif | |
| 95926362 MD |
950 | /* Check the minimum TTL for socket. */ |
| 951 | #ifdef INET6 | |
| 952 | if ((isipv6 ? ip6->ip6_hlim : ip->ip_ttl) < inp->inp_ip_minttl) | |
| 953 | goto drop; | |
| 954 | #endif | |
| 61896e3c | 955 | |
| 984263bc MD |
956 | tp = intotcpcb(inp); |
| 957 | if (tp == NULL) { | |
| 958 | rstreason = BANDLIM_RST_CLOSEDPORT; | |
| 959 | goto dropwithreset; | |
| 960 | } | |
| eb594563 | 961 | if (tp->t_state <= TCPS_CLOSED) |
| 984263bc MD |
962 | goto drop; |
| 963 | ||
| 984263bc | 964 | so = inp->inp_socket; |
| 47654766 | 965 | |
| 984263bc | 966 | #ifdef TCPDEBUG |
| 47654766 JH |
967 | if (so->so_options & SO_DEBUG) { |
| 968 | ostate = tp->t_state; | |
| 969 | if (isipv6) | |
| 95b22adf | 970 | bcopy(ip6, tcp_saveipgen, sizeof(*ip6)); |
| 47654766 | 971 | else |
| 95b22adf | 972 | bcopy(ip, tcp_saveipgen, sizeof(*ip)); |
| 47654766 JH |
973 | tcp_savetcp = *th; |
| 974 | } | |
| 984263bc | 975 | #endif |
| 47654766 | 976 | |
| 407e896e | 977 | bzero(&to, sizeof to); |
| d24ce1dc | 978 | |
| 47654766 JH |
979 | if (so->so_options & SO_ACCEPTCONN) { |
| 980 | struct in_conninfo inc; | |
| 981 | ||
| 984263bc | 982 | #ifdef INET6 |
| d24ce1dc | 983 | inc.inc_isipv6 = (isipv6 == TRUE); |
| 984263bc MD |
984 | #endif |
| 985 | if (isipv6) { | |
| 986 | inc.inc6_faddr = ip6->ip6_src; | |
| 987 | inc.inc6_laddr = ip6->ip6_dst; | |
| 988 | inc.inc6_route.ro_rt = NULL; /* XXX */ | |
| 989 | } else { | |
| 990 | inc.inc_faddr = ip->ip_src; | |
| 991 | inc.inc_laddr = ip->ip_dst; | |
| 992 | inc.inc_route.ro_rt = NULL; /* XXX */ | |
| 993 | } | |
| 994 | inc.inc_fport = th->th_sport; | |
| 995 | inc.inc_lport = th->th_dport; | |
| 996 | ||
| 61896e3c JH |
997 | /* |
| 998 | * If the state is LISTEN then ignore segment if it contains | |
| 984263bc MD |
999 | * a RST. If the segment contains an ACK then it is bad and |
| 1000 | * send a RST. If it does not contain a SYN then it is not | |
| 1001 | * interesting; drop it. | |
| 1002 | * | |
| 1003 | * If the state is SYN_RECEIVED (syncache) and seg contains | |
| 1004 | * an ACK, but not for our SYN/ACK, send a RST. If the seg | |
| 1005 | * contains a RST, check the sequence number to see if it | |
| 1006 | * is a valid reset segment. | |
| 1007 | */ | |
| 61896e3c JH |
1008 | if ((thflags & (TH_RST | TH_ACK | TH_SYN)) != TH_SYN) { |
| 1009 | if ((thflags & (TH_RST | TH_ACK | TH_SYN)) == TH_ACK) { | |
| 984263bc MD |
1010 | if (!syncache_expand(&inc, th, &so, m)) { |
| 1011 | /* | |
| 1012 | * No syncache entry, or ACK was not | |
| 1013 | * for our SYN/ACK. Send a RST. | |
| 1014 | */ | |
| 1015 | tcpstat.tcps_badsyn++; | |
| 1016 | rstreason = BANDLIM_RST_OPENPORT; | |
| 1017 | goto dropwithreset; | |
| 1018 | } | |
| 2d23a8be MD |
1019 | |
| 1020 | /* | |
| 1021 | * Could not complete 3-way handshake, | |
| 1022 | * connection is being closed down, and | |
| 1023 | * syncache will free mbuf. | |
| 1024 | */ | |
| 984263bc | 1025 | if (so == NULL) |
| 002c1265 | 1026 | return(IPPROTO_DONE); |
| 2d23a8be MD |
1027 | |
| 1028 | /* | |
| 1029 | * We must be in the correct protocol thread | |
| 1030 | * for this connection. | |
| 1031 | */ | |
| 1032 | KKASSERT(so->so_port == &curthread->td_msgport); | |
| 1033 | ||
| 984263bc MD |
1034 | /* |
| 1035 | * Socket is created in state SYN_RECEIVED. | |
| 1036 | * Continue processing segment. | |
| 1037 | */ | |
| ed894f8c | 1038 | inp = so->so_pcb; |
| 984263bc MD |
1039 | tp = intotcpcb(inp); |
| 1040 | /* | |
| 1041 | * This is what would have happened in | |
| 1042 | * tcp_output() when the SYN,ACK was sent. | |
| 1043 | */ | |
| 1044 | tp->snd_up = tp->snd_una; | |
| 1045 | tp->snd_max = tp->snd_nxt = tp->iss + 1; | |
| 1046 | tp->last_ack_sent = tp->rcv_nxt; | |
| df1d2774 | 1047 | |
| 984263bc MD |
1048 | goto after_listen; |
| 1049 | } | |
| 1050 | if (thflags & TH_RST) { | |
| 1051 | syncache_chkrst(&inc, th); | |
| 1052 | goto drop; | |
| 1053 | } | |
| 1054 | if (thflags & TH_ACK) { | |
| 1055 | syncache_badack(&inc); | |
| 1056 | tcpstat.tcps_badsyn++; | |
| 1057 | rstreason = BANDLIM_RST_OPENPORT; | |
| 1058 | goto dropwithreset; | |
| 1059 | } | |
| 1060 | goto drop; | |
| 1061 | } | |
| 1062 | ||
| 1063 | /* | |
| 61896e3c | 1064 | * Segment's flags are (SYN) or (SYN | FIN). |
| 984263bc MD |
1065 | */ |
| 1066 | #ifdef INET6 | |
| 1067 | /* | |
| 1068 | * If deprecated address is forbidden, | |
| 1069 | * we do not accept SYN to deprecated interface | |
| 1070 | * address to prevent any new inbound connection from | |
| 1071 | * getting established. | |
| 1072 | * When we do not accept SYN, we send a TCP RST, | |
| 1073 | * with deprecated source address (instead of dropping | |
| 1074 | * it). We compromise it as it is much better for peer | |
| 1075 | * to send a RST, and RST will be the final packet | |
| 1076 | * for the exchange. | |
| 1077 | * | |
| 1078 | * If we do not forbid deprecated addresses, we accept | |
| 1079 | * the SYN packet. RFC2462 does not suggest dropping | |
| 1080 | * SYN in this case. | |
| 1081 | * If we decipher RFC2462 5.5.4, it says like this: | |
| 1082 | * 1. use of deprecated addr with existing | |
| 1083 | * communication is okay - "SHOULD continue to be | |
| 1084 | * used" | |
| 1085 | * 2. use of it with new communication: | |
| 1086 | * (2a) "SHOULD NOT be used if alternate address | |
| 61896e3c | 1087 | * with sufficient scope is available" |
| 984263bc MD |
1088 | * (2b) nothing mentioned otherwise. |
| 1089 | * Here we fall into (2b) case as we have no choice in | |
| 1090 | * our source address selection - we must obey the peer. | |
| 1091 | * | |
| 1092 | * The wording in RFC2462 is confusing, and there are | |
| 1093 | * multiple description text for deprecated address | |
| 1094 | * handling - worse, they are not exactly the same. | |
| 1095 | * I believe 5.5.4 is the best one, so we follow 5.5.4. | |
| 1096 | */ | |
| 1097 | if (isipv6 && !ip6_use_deprecated) { | |
| 1098 | struct in6_ifaddr *ia6; | |
| 1099 | ||
| 1100 | if ((ia6 = ip6_getdstifaddr(m)) && | |
| 1101 | (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { | |
| 1102 | tp = NULL; | |
| 1103 | rstreason = BANDLIM_RST_OPENPORT; | |
| 1104 | goto dropwithreset; | |
| 1105 | } | |
| 1106 | } | |
| 1107 | #endif | |
| 1108 | /* | |
| 1109 | * If it is from this socket, drop it, it must be forged. | |
| 1110 | * Don't bother responding if the destination was a broadcast. | |
| 1111 | */ | |
| 1112 | if (th->th_dport == th->th_sport) { | |
| 1113 | if (isipv6) { | |
| 1114 | if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, | |
| 1115 | &ip6->ip6_src)) | |
| 1116 | goto drop; | |
| 1117 | } else { | |
| 1118 | if (ip->ip_dst.s_addr == ip->ip_src.s_addr) | |
| 1119 | goto drop; | |
| 1120 | } | |
| 1121 | } | |
| 1122 | /* | |
| 1123 | * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN | |
| 1124 | * | |
| 1125 | * Note that it is quite possible to receive unicast | |
| 1126 | * link-layer packets with a broadcast IP address. Use | |
| 1127 | * in_broadcast() to find them. | |
| 1128 | */ | |
| 61896e3c | 1129 | if (m->m_flags & (M_BCAST | M_MCAST)) |
| 984263bc MD |
1130 | goto drop; |
| 1131 | if (isipv6) { | |
| 1132 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || | |
| 1133 | IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) | |
| 1134 | goto drop; | |
| 1135 | } else { | |
| 1136 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || | |
| 1137 | IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || | |
| 1138 | ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || | |
| 1139 | in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) | |
| 1140 | goto drop; | |
| 1141 | } | |
| 1142 | /* | |
| 1143 | * SYN appears to be valid; create compressed TCP state | |
| 1144 | * for syncache, or perform t/tcp connection. | |
| 1145 | */ | |
| 1146 | if (so->so_qlen <= so->so_qlimit) { | |
| 6c1bbf57 | 1147 | tcp_dooptions(&to, optp, optlen, TRUE, th->th_ack); |
| b09567cc | 1148 | if (!syncache_add(&inc, &to, th, so, m)) |
| 984263bc | 1149 | goto drop; |
| 2d23a8be MD |
1150 | |
| 1151 | /* | |
| 1152 | * Entry added to syncache, mbuf used to | |
| 1153 | * send SYN,ACK packet. | |
| 1154 | */ | |
| b09567cc | 1155 | return(IPPROTO_DONE); |
| 984263bc MD |
1156 | } |
| 1157 | goto drop; | |
| 1158 | } | |
| 984263bc | 1159 | |
| 2d23a8be MD |
1160 | after_listen: |
| 1161 | /* | |
| 1162 | * Should not happen - syncache should pick up these connections. | |
| 1163 | * | |
| 1164 | * Once we are past handling listen sockets we must be in the | |
| 1165 | * correct protocol processing thread. | |
| 1166 | */ | |
| 61896e3c | 1167 | KASSERT(tp->t_state != TCPS_LISTEN, ("tcp_input: TCPS_LISTEN state")); |
| 2d23a8be | 1168 | KKASSERT(so->so_port == &curthread->td_msgport); |
| 984263bc | 1169 | |
| df1d2774 SZ |
1170 | /* Unscale the window into a 32-bit value. */ |
| 1171 | if (!(thflags & TH_SYN)) | |
| 1172 | tiwin = th->th_win << tp->snd_scale; | |
| 1173 | else | |
| 1174 | tiwin = th->th_win; | |
| 1175 | ||
| 984263bc | 1176 | /* |
| 5b0b9fa5 PA |
1177 | * This is the second part of the MSS DoS prevention code (after |
| 1178 | * minmss on the sending side) and it deals with too many too small | |
| 1179 | * tcp packets in a too short timeframe (1 second). | |
| 1180 | * | |
| 153d34ac MD |
1181 | * XXX Removed. This code was crap. It does not scale to network |
| 1182 | * speed, and default values break NFS. Gone. | |
| 5b0b9fa5 | 1183 | */ |
| 153d34ac | 1184 | /* REMOVED */ |
| 5b0b9fa5 PA |
1185 | |
| 1186 | /* | |
| 984263bc | 1187 | * Segment received on connection. |
| efca2b8e MD |
1188 | * |
| 1189 | * Reset idle time and keep-alive timer. Don't waste time if less | |
| 0ecd93f9 | 1190 | * then a second has elapsed. |
| 984263bc | 1191 | */ |
| 0ecd93f9 MD |
1192 | if ((int)(ticks - tp->t_rcvtime) > hz) |
| 1193 | tcp_timer_keep_activity(tp, thflags); | |
| 984263bc MD |
1194 | |
| 1195 | /* | |
| 1196 | * Process options. | |
| 1197 | * XXX this is tradtitional behavior, may need to be cleaned up. | |
| 1198 | */ | |
| 6c1bbf57 | 1199 | tcp_dooptions(&to, optp, optlen, (thflags & TH_SYN) != 0, th->th_ack); |
| ad0af98b | 1200 | if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { |
| df1d2774 | 1201 | if ((to.to_flags & TOF_SCALE) && (tp->t_flags & TF_REQ_SCALE)) { |
| 984263bc | 1202 | tp->t_flags |= TF_RCVD_SCALE; |
| df1d2774 | 1203 | tp->snd_scale = to.to_requested_s_scale; |
| 984263bc | 1204 | } |
| df1d2774 SZ |
1205 | |
| 1206 | /* | |
| 1207 | * Initial send window; will be updated upon next ACK | |
| 1208 | */ | |
| 1209 | tp->snd_wnd = th->th_win; | |
| 1210 | ||
| 984263bc MD |
1211 | if (to.to_flags & TOF_TS) { |
| 1212 | tp->t_flags |= TF_RCVD_TSTMP; | |
| 1213 | tp->ts_recent = to.to_tsval; | |
| 1214 | tp->ts_recent_age = ticks; | |
| 1215 | } | |
| f34fd0f2 SZ |
1216 | if (!(to.to_flags & TOF_MSS)) |
| 1217 | to.to_mss = 0; | |
| 1218 | tcp_mss(tp, to.to_mss); | |
| 91489f6b JH |
1219 | /* |
| 1220 | * Only set the TF_SACK_PERMITTED per-connection flag | |
| 1221 | * if we got a SACK_PERMITTED option from the other side | |
| 1222 | * and the global tcp_do_sack variable is true. | |
| 1223 | */ | |
| 1224 | if (tcp_do_sack && (to.to_flags & TOF_SACK_PERMITTED)) | |
| 1225 | tp->t_flags |= TF_SACK_PERMITTED; | |
| 984263bc MD |
1226 | } |
| 1227 | ||
| 1228 | /* | |
| 1229 | * Header prediction: check for the two common cases | |
| 1230 | * of a uni-directional data xfer. If the packet has | |
| 1231 | * no control flags, is in-sequence, the window didn't | |
| 1232 | * change and we're not retransmitting, it's a | |
| 1233 | * candidate. If the length is zero and the ack moved | |
| 1234 | * forward, we're the sender side of the xfer. Just | |
| 1235 | * free the data acked & wake any higher level process | |
| 1236 | * that was blocked waiting for space. If the length | |
| 1237 | * is non-zero and the ack didn't move, we're the | |
| 1238 | * receiver side. If we're getting packets in-order | |
| 1239 | * (the reassembly queue is empty), add the data to | |
| 1240 | * the socket buffer and note that we need a delayed ack. | |
| 1241 | * Make sure that the hidden state-flags are also off. | |
| 1242 | * Since we check for TCPS_ESTABLISHED above, it can only | |
| 1243 | * be TH_NEEDSYN. | |
| 1244 | */ | |
| 1245 | if (tp->t_state == TCPS_ESTABLISHED && | |
| 1246 | (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && | |
| 61896e3c JH |
1247 | !(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)) && |
| 1248 | (!(to.to_flags & TOF_TS) || | |
| 984263bc | 1249 | TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && |
| 984263bc | 1250 | th->th_seq == tp->rcv_nxt && |
| 984263bc MD |
1251 | tp->snd_nxt == tp->snd_max) { |
| 1252 | ||
| 1253 | /* | |
| 1254 | * If last ACK falls within this segment's sequence numbers, | |
| 1255 | * record the timestamp. | |
| 1256 | * NOTE that the test is modified according to the latest | |
| 1257 | * proposal of the tcplw@cray.com list (Braden 1993/04/26). | |
| 1258 | */ | |
| 61896e3c | 1259 | if ((to.to_flags & TOF_TS) && |
| 984263bc MD |
1260 | SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { |
| 1261 | tp->ts_recent_age = ticks; | |
| 1262 | tp->ts_recent = to.to_tsval; | |
| 1263 | } | |
| 1264 | ||
| 1265 | if (tlen == 0) { | |
| 1266 | if (SEQ_GT(th->th_ack, tp->snd_una) && | |
| 1267 | SEQ_LEQ(th->th_ack, tp->snd_max) && | |
| 1268 | tp->snd_cwnd >= tp->snd_wnd && | |
| 95b22adf | 1269 | !IN_FASTRECOVERY(tp)) { |
| 984263bc | 1270 | /* |
| be23faf1 | 1271 | * This is a pure ack for outstanding data. |
| 984263bc MD |
1272 | */ |
| 1273 | ++tcpstat.tcps_predack; | |
| 1274 | /* | |
| 1275 | * "bad retransmit" recovery | |
| bfdb979e JH |
1276 | * |
| 1277 | * If Eifel detection applies, then | |
| 1278 | * it is deterministic, so use it | |
| 1279 | * unconditionally over the old heuristic. | |
| 1280 | * Otherwise, fall back to the old heuristic. | |
| 984263bc | 1281 | */ |
| bfdb979e JH |
1282 | if (tcp_do_eifel_detect && |
| 1283 | (to.to_flags & TOF_TS) && to.to_tsecr && | |
| 1c8b7a61 | 1284 | (tp->rxt_flags & TRXT_F_FIRSTACCACK)) { |
| bfdb979e JH |
1285 | /* Eifel detection applicable. */ |
| 1286 | if (to.to_tsecr < tp->t_rexmtTS) { | |
| 1287 | tcp_revert_congestion_state(tp); | |
| 1288 | ++tcpstat.tcps_eifeldetected; | |
| 928c3291 SZ |
1289 | if (tp->t_rxtshift != 1 || |
| 1290 | ticks >= tp->t_badrxtwin) | |
| 1291 | ++tcpstat.tcps_rttcantdetect; | |
| bfdb979e JH |
1292 | } |
| 1293 | } else if (tp->t_rxtshift == 1 && | |
| 1294 | ticks < tp->t_badrxtwin) { | |
| 1295 | tcp_revert_congestion_state(tp); | |
| 1296 | ++tcpstat.tcps_rttdetected; | |
| 984263bc | 1297 | } |
| 1c8b7a61 SZ |
1298 | tp->rxt_flags &= ~(TRXT_F_FIRSTACCACK | |
| 1299 | TRXT_F_FASTREXMT | TRXT_F_EARLYREXMT); | |
| 984263bc MD |
1300 | /* |
| 1301 | * Recalculate the retransmit timer / rtt. | |
| 1302 | * | |
| d24ce1dc | 1303 | * Some machines (certain windows boxes) |
| 984263bc MD |
1304 | * send broken timestamp replies during the |
| 1305 | * SYN+ACK phase, ignore timestamps of 0. | |
| 1306 | */ | |
| 95b22adf | 1307 | if ((to.to_flags & TOF_TS) && to.to_tsecr) { |
| 984263bc | 1308 | tcp_xmit_timer(tp, |
| 073ec6c4 SZ |
1309 | ticks - to.to_tsecr + 1, |
| 1310 | th->th_ack); | |
| 984263bc | 1311 | } else if (tp->t_rtttime && |
| 95b22adf | 1312 | SEQ_GT(th->th_ack, tp->t_rtseq)) { |
| 984263bc | 1313 | tcp_xmit_timer(tp, |
| 073ec6c4 SZ |
1314 | ticks - tp->t_rtttime, |
| 1315 | th->th_ack); | |
| 984263bc MD |
1316 | } |
| 1317 | tcp_xmit_bandwidth_limit(tp, th->th_ack); | |
| 1318 | acked = th->th_ack - tp->snd_una; | |
| 1319 | tcpstat.tcps_rcvackpack++; | |
| 1320 | tcpstat.tcps_rcvackbyte += acked; | |
| 6d49aa6f | 1321 | sbdrop(&so->so_snd.sb, acked); |
| cfb3f3f4 | 1322 | tp->snd_recover = th->th_ack - 1; |
| 9845754e | 1323 | tp->snd_una = th->th_ack; |
| 984263bc | 1324 | tp->t_dupacks = 0; |
| df9d7670 JH |
1325 | /* |
| 1326 | * Update window information. | |
| 1327 | */ | |
| 1328 | if (tiwin != tp->snd_wnd && | |
| 1329 | acceptable_window_update(tp, th, tiwin)) { | |
| 1330 | /* keep track of pure window updates */ | |
| 1331 | if (tp->snd_wl2 == th->th_ack && | |
| 1332 | tiwin > tp->snd_wnd) | |
| 1333 | tcpstat.tcps_rcvwinupd++; | |
| 1334 | tp->snd_wnd = tiwin; | |
| 1335 | tp->snd_wl1 = th->th_seq; | |
| 1336 | tp->snd_wl2 = th->th_ack; | |
| 1337 | if (tp->snd_wnd > tp->max_sndwnd) | |
| 1338 | tp->max_sndwnd = tp->snd_wnd; | |
| 1339 | } | |
| 984263bc MD |
1340 | m_freem(m); |
| 1341 | ND6_HINT(tp); /* some progress has been done */ | |
| 984263bc MD |
1342 | /* |
| 1343 | * If all outstanding data are acked, stop | |
| 1344 | * retransmit timer, otherwise restart timer | |
| 1345 | * using current (possibly backed-off) value. | |
| 1346 | * If process is waiting for space, | |
| 1347 | * wakeup/selwakeup/signal. If data | |
| 1348 | * are ready to send, let tcp_output | |
| 1349 | * decide between more output or persist. | |
| 1350 | */ | |
| a48c5dd5 SZ |
1351 | if (tp->snd_una == tp->snd_max) { |
| 1352 | tcp_callout_stop(tp, tp->tt_rexmt); | |
| 1353 | } else if (!tcp_callout_active(tp, | |
| 1354 | tp->tt_persist)) { | |
| 1355 | tcp_callout_reset(tp, tp->tt_rexmt, | |
| 1356 | tp->t_rxtcur, tcp_timer_rexmt); | |
| 1357 | } | |
| 984263bc | 1358 | sowwakeup(so); |
| 6d49aa6f | 1359 | if (so->so_snd.ssb_cc > 0) |
| f23061d4 | 1360 | tcp_output(tp); |
| 002c1265 | 1361 | return(IPPROTO_DONE); |
| 984263bc | 1362 | } |
| df9d7670 JH |
1363 | } else if (tiwin == tp->snd_wnd && |
| 1364 | th->th_ack == tp->snd_una && | |
| 0f9e45de | 1365 | TAILQ_EMPTY(&tp->t_segq) && |
| 6d49aa6f | 1366 | tlen <= ssb_space(&so->so_rcv)) { |
| 46e92930 | 1367 | u_long newsize = 0; /* automatic sockbuf scaling */ |
| 984263bc | 1368 | /* |
| be23faf1 | 1369 | * This is a pure, in-sequence data packet |
| 984263bc MD |
1370 | * with nothing on the reassembly queue and |
| 1371 | * we have enough buffer space to take it. | |
| 1372 | */ | |
| 1373 | ++tcpstat.tcps_preddat; | |
| 1374 | tp->rcv_nxt += tlen; | |
| 1375 | tcpstat.tcps_rcvpack++; | |
| 1376 | tcpstat.tcps_rcvbyte += tlen; | |
| 1377 | ND6_HINT(tp); /* some progress has been done */ | |
| 5b0b9fa5 PA |
1378 | /* |
| 1379 | * Automatic sizing of receive socket buffer. Often the send | |
| 1380 | * buffer size is not optimally adjusted to the actual network | |
| 1381 | * conditions at hand (delay bandwidth product). Setting the | |
| 1382 | * buffer size too small limits throughput on links with high | |
| 1383 | * bandwidth and high delay (eg. trans-continental/oceanic links). | |
| 1384 | * | |
| 1385 | * On the receive side the socket buffer memory is only rarely | |
| 1386 | * used to any significant extent. This allows us to be much | |
| 1387 | * more aggressive in scaling the receive socket buffer. For | |
| 1388 | * the case that the buffer space is actually used to a large | |
| 1389 | * extent and we run out of kernel memory we can simply drop | |
| 1390 | * the new segments; TCP on the sender will just retransmit it | |
| 1391 | * later. Setting the buffer size too big may only consume too | |
| 1392 | * much kernel memory if the application doesn't read() from | |
| 1393 | * the socket or packet loss or reordering makes use of the | |
| 1394 | * reassembly queue. | |
| 1395 | * | |
| 1396 | * The criteria to step up the receive buffer one notch are: | |
| 1397 | * 1. the number of bytes received during the time it takes | |
| 1398 | * one timestamp to be reflected back to us (the RTT); | |
| 1399 | * 2. received bytes per RTT is within seven eighth of the | |
| 1400 | * current socket buffer size; | |
| 1401 | * 3. receive buffer size has not hit maximal automatic size; | |
| 1402 | * | |
| 1403 | * This algorithm does one step per RTT at most and only if | |
| 1404 | * we receive a bulk stream w/o packet losses or reorderings. | |
| 1405 | * Shrinking the buffer during idle times is not necessary as | |
| 1406 | * it doesn't consume any memory when idle. | |
| 1407 | * | |
| 1408 | * TODO: Only step up if the application is actually serving | |
| 1409 | * the buffer to better manage the socket buffer resources. | |
| 1410 | */ | |
| 1411 | if (tcp_do_autorcvbuf && | |
| 1412 | to.to_tsecr && | |
| 1413 | (so->so_rcv.ssb_flags & SSB_AUTOSIZE)) { | |
| 1414 | if (to.to_tsecr > tp->rfbuf_ts && | |
| 1415 | to.to_tsecr - tp->rfbuf_ts < hz) { | |
| 1416 | if (tp->rfbuf_cnt > | |
| 1417 | (so->so_rcv.ssb_hiwat / 8 * 7) && | |
| 1418 | so->so_rcv.ssb_hiwat < | |
| 1419 | tcp_autorcvbuf_max) { | |
| 1420 | newsize = | |
| 46e92930 MD |
1421 | ulmin(so->so_rcv.ssb_hiwat + |
| 1422 | tcp_autorcvbuf_inc, | |
| 1423 | tcp_autorcvbuf_max); | |
| 5b0b9fa5 PA |
1424 | } |
| 1425 | /* Start over with next RTT. */ | |
| 1426 | tp->rfbuf_ts = 0; | |
| 1427 | tp->rfbuf_cnt = 0; | |
| 1428 | } else | |
| 1429 | tp->rfbuf_cnt += tlen; /* add up */ | |
| 1430 | } | |
| 984263bc MD |
1431 | /* |
| 1432 | * Add data to socket buffer. | |
| 1433 | */ | |
| 1434 | if (so->so_state & SS_CANTRCVMORE) { | |
| 1435 | m_freem(m); | |
| 1436 | } else { | |
| 5b0b9fa5 | 1437 | /* |
| 46e92930 MD |
1438 | * Set new socket buffer size, give up when |
| 1439 | * limit is reached. | |
| 1440 | * | |
| 1441 | * Adjusting the size can mess up ACK | |
| 1442 | * sequencing when pure window updates are | |
| 1443 | * being avoided (which is the default), | |
| 1444 | * so force an ack. | |
| 5b0b9fa5 | 1445 | */ |
| 6cef7136 | 1446 | lwkt_gettoken(&so->so_rcv.ssb_token); |
| 46e92930 MD |
1447 | if (newsize) { |
| 1448 | tp->t_flags |= TF_RXRESIZED; | |
| 5b0b9fa5 | 1449 | if (!ssb_reserve(&so->so_rcv, newsize, |
| 46e92930 | 1450 | so, NULL)) { |
| 14343ad3 | 1451 | atomic_clear_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE); |
| 46e92930 MD |
1452 | } |
| 1453 | if (newsize >= | |
| 1454 | (TCP_MAXWIN << tp->rcv_scale)) { | |
| 14343ad3 | 1455 | atomic_clear_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE); |
| 46e92930 MD |
1456 | } |
| 1457 | } | |
| 09cf73e0 | 1458 | m_adj(m, drop_hdrlen); /* delayed header drop */ |
| 6d49aa6f | 1459 | ssb_appendstream(&so->so_rcv, m); |
| 6cef7136 | 1460 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 984263bc MD |
1461 | } |
| 1462 | sorwakeup(so); | |
| 2b1ce38a MD |
1463 | /* |
| 1464 | * This code is responsible for most of the ACKs | |
| 1465 | * the TCP stack sends back after receiving a data | |
| 1466 | * packet. Note that the DELAY_ACK check fails if | |
| 1467 | * the delack timer is already running, which results | |
| 1468 | * in an ack being sent every other packet (which is | |
| 1469 | * what we want). | |
| 72b37eeb MD |
1470 | * |
| 1471 | * We then further aggregate acks by not actually | |
| 1472 | * sending one until the protocol thread has completed | |
| 1473 | * processing the current backlog of packets. This | |
| 1474 | * does not delay the ack any further, but allows us | |
| 1475 | * to take advantage of the packet aggregation that | |
| 1476 | * high speed NICs do (usually blocks of 8-10 packets) | |
| 1477 | * to send a single ack rather then four or five acks, | |
| 1478 | * greatly reducing the ack rate, the return channel | |
| 1479 | * bandwidth, and the protocol overhead on both ends. | |
| 1480 | * | |
| 1481 | * Since this also has the effect of slowing down | |
| 1482 | * the exponential slow-start ramp-up, systems with | |
| 1483 | * very large bandwidth-delay products might want | |
| 1484 | * to turn the feature off. | |
| 2b1ce38a | 1485 | */ |
| 984263bc | 1486 | if (DELAY_ACK(tp)) { |
| a48c5dd5 SZ |
1487 | tcp_callout_reset(tp, tp->tt_delack, |
| 1488 | tcp_delacktime, tcp_timer_delack); | |
| 72b37eeb | 1489 | } else if (tcp_aggregate_acks) { |
| 984263bc | 1490 | tp->t_flags |= TF_ACKNOW; |
| 61896e3c | 1491 | if (!(tp->t_flags & TF_ONOUTPUTQ)) { |
| 2b1ce38a MD |
1492 | tp->t_flags |= TF_ONOUTPUTQ; |
| 1493 | tp->tt_cpu = mycpu->gd_cpuid; | |
| 1494 | TAILQ_INSERT_TAIL( | |
| 1495 | &tcpcbackq[tp->tt_cpu], | |
| 1496 | tp, t_outputq); | |
| 1497 | } | |
| 72b37eeb MD |
1498 | } else { |
| 1499 | tp->t_flags |= TF_ACKNOW; | |
| 1500 | tcp_output(tp); | |
| 984263bc | 1501 | } |
| 002c1265 | 1502 | return(IPPROTO_DONE); |
| 984263bc MD |
1503 | } |
| 1504 | } | |
| 1505 | ||
| 1506 | /* | |
| 1507 | * Calculate amount of space in receive window, | |
| 1508 | * and then do TCP input processing. | |
| 1509 | * Receive window is amount of space in rcv queue, | |
| 1510 | * but not less than advertised window. | |
| 1511 | */ | |
| 6d49aa6f | 1512 | recvwin = ssb_space(&so->so_rcv); |
| 61896e3c JH |
1513 | if (recvwin < 0) |
| 1514 | recvwin = 0; | |
| 1515 | tp->rcv_wnd = imax(recvwin, (int)(tp->rcv_adv - tp->rcv_nxt)); | |
| 984263bc | 1516 | |
| 5b0b9fa5 PA |
1517 | /* Reset receive buffer auto scaling when not in bulk receive mode. */ |
| 1518 | tp->rfbuf_ts = 0; | |
| 1519 | tp->rfbuf_cnt = 0; | |
| 1520 | ||
| 984263bc | 1521 | switch (tp->t_state) { |
| 984263bc MD |
1522 | /* |
| 1523 | * If the state is SYN_RECEIVED: | |
| 1524 | * if seg contains an ACK, but not for our SYN/ACK, send a RST. | |
| 1525 | */ | |
| 1526 | case TCPS_SYN_RECEIVED: | |
| 1527 | if ((thflags & TH_ACK) && | |
| 1528 | (SEQ_LEQ(th->th_ack, tp->snd_una) || | |
| 1529 | SEQ_GT(th->th_ack, tp->snd_max))) { | |
| 1530 | rstreason = BANDLIM_RST_OPENPORT; | |
| 1531 | goto dropwithreset; | |
| 1532 | } | |
| 1533 | break; | |
| 1534 | ||
| 1535 | /* | |
| 1536 | * If the state is SYN_SENT: | |
| 1537 | * if seg contains an ACK, but not for our SYN, drop the input. | |
| 1538 | * if seg contains a RST, then drop the connection. | |
| 1539 | * if seg does not contain SYN, then drop it. | |
| 1540 | * Otherwise this is an acceptable SYN segment | |
| 1541 | * initialize tp->rcv_nxt and tp->irs | |
| 1542 | * if seg contains ack then advance tp->snd_una | |
| 1543 | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state | |
| 1544 | * arrange for segment to be acked (eventually) | |
| 1545 | * continue processing rest of data/controls, beginning with URG | |
| 1546 | */ | |
| 1547 | case TCPS_SYN_SENT: | |
| 984263bc MD |
1548 | if ((thflags & TH_ACK) && |
| 1549 | (SEQ_LEQ(th->th_ack, tp->iss) || | |
| 1550 | SEQ_GT(th->th_ack, tp->snd_max))) { | |
| 27b8aee3 AE |
1551 | rstreason = BANDLIM_UNLIMITED; |
| 1552 | goto dropwithreset; | |
| 984263bc MD |
1553 | } |
| 1554 | if (thflags & TH_RST) { | |
| 1555 | if (thflags & TH_ACK) | |
| 1556 | tp = tcp_drop(tp, ECONNREFUSED); | |
| 1557 | goto drop; | |
| 1558 | } | |
| 61896e3c | 1559 | if (!(thflags & TH_SYN)) |
| 984263bc | 1560 | goto drop; |
| 984263bc MD |
1561 | |
| 1562 | tp->irs = th->th_seq; | |
| 1563 | tcp_rcvseqinit(tp); | |
| 1564 | if (thflags & TH_ACK) { | |
| 27b8aee3 | 1565 | /* Our SYN was acked. */ |
| 984263bc MD |
1566 | tcpstat.tcps_connects++; |
| 1567 | soisconnected(so); | |
| 1568 | /* Do window scaling on this connection? */ | |
| 61896e3c | 1569 | if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == |
| df1d2774 | 1570 | (TF_RCVD_SCALE | TF_REQ_SCALE)) |
| 984263bc | 1571 | tp->rcv_scale = tp->request_r_scale; |
| 984263bc MD |
1572 | tp->rcv_adv += tp->rcv_wnd; |
| 1573 | tp->snd_una++; /* SYN is acked */ | |
| a48c5dd5 | 1574 | tcp_callout_stop(tp, tp->tt_rexmt); |
| 984263bc MD |
1575 | /* |
| 1576 | * If there's data, delay ACK; if there's also a FIN | |
| 1577 | * ACKNOW will be turned on later. | |
| 1578 | */ | |
| a48c5dd5 SZ |
1579 | if (DELAY_ACK(tp) && tlen != 0) { |
| 1580 | tcp_callout_reset(tp, tp->tt_delack, | |
| 1581 | tcp_delacktime, tcp_timer_delack); | |
| 1582 | } else { | |
| 984263bc | 1583 | tp->t_flags |= TF_ACKNOW; |
| a48c5dd5 | 1584 | } |
| 984263bc MD |
1585 | /* |
| 1586 | * Received <SYN,ACK> in SYN_SENT[*] state. | |
| 1587 | * Transitions: | |
| 1588 | * SYN_SENT --> ESTABLISHED | |
| 1589 | * SYN_SENT* --> FIN_WAIT_1 | |
| 1590 | */ | |
| 1591 | tp->t_starttime = ticks; | |
| 1592 | if (tp->t_flags & TF_NEEDFIN) { | |
| 1593 | tp->t_state = TCPS_FIN_WAIT_1; | |
| 1594 | tp->t_flags &= ~TF_NEEDFIN; | |
| 1595 | thflags &= ~TH_SYN; | |
| 1596 | } else { | |
| 8651f7f8 | 1597 | tcp_established(tp); |
| 984263bc MD |
1598 | } |
| 1599 | } else { | |
| 1600 | /* | |
| 95b22adf | 1601 | * Received initial SYN in SYN-SENT[*] state => |
| 27b8aee3 AE |
1602 | * simultaneous open. |
| 1603 | * Do 3-way handshake: | |
| 61896e3c JH |
1604 | * SYN-SENT -> SYN-RECEIVED |
| 1605 | * SYN-SENT* -> SYN-RECEIVED* | |
| 95b22adf | 1606 | */ |
| 984263bc | 1607 | tp->t_flags |= TF_ACKNOW; |
| a48c5dd5 | 1608 | tcp_callout_stop(tp, tp->tt_rexmt); |
| 27b8aee3 | 1609 | tp->t_state = TCPS_SYN_RECEIVED; |
| 984263bc MD |
1610 | } |
| 1611 | ||
| 984263bc MD |
1612 | /* |
| 1613 | * Advance th->th_seq to correspond to first data byte. | |
| 1614 | * If data, trim to stay within window, | |
| 1615 | * dropping FIN if necessary. | |
| 1616 | */ | |
| 1617 | th->th_seq++; | |
| 1618 | if (tlen > tp->rcv_wnd) { | |
| 1619 | todrop = tlen - tp->rcv_wnd; | |
| 1620 | m_adj(m, -todrop); | |
| 1621 | tlen = tp->rcv_wnd; | |
| 1622 | thflags &= ~TH_FIN; | |
| 1623 | tcpstat.tcps_rcvpackafterwin++; | |
| 1624 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
| 1625 | } | |
| 1626 | tp->snd_wl1 = th->th_seq - 1; | |
| 1627 | tp->rcv_up = th->th_seq; | |
| 1628 | /* | |
| 1629 | * Client side of transaction: already sent SYN and data. | |
| 1630 | * If the remote host used T/TCP to validate the SYN, | |
| 1631 | * our data will be ACK'd; if so, enter normal data segment | |
| 1632 | * processing in the middle of step 5, ack processing. | |
| 1633 | * Otherwise, goto step 6. | |
| 1634 | */ | |
| 95b22adf | 1635 | if (thflags & TH_ACK) |
| 984263bc MD |
1636 | goto process_ACK; |
| 1637 | ||
| 1638 | goto step6; | |
| 1639 | ||
| 1640 | /* | |
| 1641 | * If the state is LAST_ACK or CLOSING or TIME_WAIT: | |
| 27b8aee3 | 1642 | * do normal processing (we no longer bother with T/TCP). |
| 984263bc MD |
1643 | */ |
| 1644 | case TCPS_LAST_ACK: | |
| 1645 | case TCPS_CLOSING: | |
| 1646 | case TCPS_TIME_WAIT: | |
| 95b22adf | 1647 | break; /* continue normal processing */ |
| 984263bc MD |
1648 | } |
| 1649 | ||
| 1650 | /* | |
| 1651 | * States other than LISTEN or SYN_SENT. | |
| 1652 | * First check the RST flag and sequence number since reset segments | |
| 1653 | * are exempt from the timestamp and connection count tests. This | |
| 1654 | * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix | |
| 1655 | * below which allowed reset segments in half the sequence space | |
| 1656 | * to fall though and be processed (which gives forged reset | |
| 1657 | * segments with a random sequence number a 50 percent chance of | |
| 1658 | * killing a connection). | |
| 1659 | * Then check timestamp, if present. | |
| 1660 | * Then check the connection count, if present. | |
| 1661 | * Then check that at least some bytes of segment are within | |
| 1662 | * receive window. If segment begins before rcv_nxt, | |
| 1663 | * drop leading data (and SYN); if nothing left, just ack. | |
| 1664 | * | |
| 1665 | * | |
| 1666 | * If the RST bit is set, check the sequence number to see | |
| 1667 | * if this is a valid reset segment. | |
| 1668 | * RFC 793 page 37: | |
| 1669 | * In all states except SYN-SENT, all reset (RST) segments | |
| 1670 | * are validated by checking their SEQ-fields. A reset is | |
| 1671 | * valid if its sequence number is in the window. | |
| 1672 | * Note: this does not take into account delayed ACKs, so | |
| 1673 | * we should test against last_ack_sent instead of rcv_nxt. | |
| 1674 | * The sequence number in the reset segment is normally an | |
| 91489f6b | 1675 | * echo of our outgoing acknowledgement numbers, but some hosts |
| 984263bc MD |
1676 | * send a reset with the sequence number at the rightmost edge |
| 1677 | * of our receive window, and we have to handle this case. | |
| 1678 | * If we have multiple segments in flight, the intial reset | |
| 1679 | * segment sequence numbers will be to the left of last_ack_sent, | |
| 1680 | * but they will eventually catch up. | |
| 1681 | * In any case, it never made sense to trim reset segments to | |
| 1682 | * fit the receive window since RFC 1122 says: | |
| 1683 | * 4.2.2.12 RST Segment: RFC-793 Section 3.4 | |
| 1684 | * | |
| 1685 | * A TCP SHOULD allow a received RST segment to include data. | |
| 1686 | * | |
| 1687 | * DISCUSSION | |
| 61896e3c JH |
1688 | * It has been suggested that a RST segment could contain |
| 1689 | * ASCII text that encoded and explained the cause of the | |
| 91489f6b | 1690 | * RST. No standard has yet been established for such |
| 61896e3c | 1691 | * data. |
| 984263bc MD |
1692 | * |
| 1693 | * If the reset segment passes the sequence number test examine | |
| 1694 | * the state: | |
| 1695 | * SYN_RECEIVED STATE: | |
| 1696 | * If passive open, return to LISTEN state. | |
| 1697 | * If active open, inform user that connection was refused. | |
| 1698 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: | |
| 1699 | * Inform user that connection was reset, and close tcb. | |
| 1700 | * CLOSING, LAST_ACK STATES: | |
| 1701 | * Close the tcb. | |
| 1702 | * TIME_WAIT STATE: | |
| 1703 | * Drop the segment - see Stevens, vol. 2, p. 964 and | |
| 61896e3c | 1704 | * RFC 1337. |
| 984263bc MD |
1705 | */ |
| 1706 | if (thflags & TH_RST) { | |
| 1707 | if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && | |
| d4dbb5be | 1708 | SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { |
| 984263bc MD |
1709 | switch (tp->t_state) { |
| 1710 | ||
| 1711 | case TCPS_SYN_RECEIVED: | |
| 1712 | so->so_error = ECONNREFUSED; | |
| 1713 | goto close; | |
| 1714 | ||
| 1715 | case TCPS_ESTABLISHED: | |
| 1716 | case TCPS_FIN_WAIT_1: | |
| 1717 | case TCPS_FIN_WAIT_2: | |
| 1718 | case TCPS_CLOSE_WAIT: | |
| 1719 | so->so_error = ECONNRESET; | |
| 1720 | close: | |
| 1721 | tp->t_state = TCPS_CLOSED; | |
| 1722 | tcpstat.tcps_drops++; | |
| 1723 | tp = tcp_close(tp); | |
| 1724 | break; | |
| 1725 | ||
| 1726 | case TCPS_CLOSING: | |
| 1727 | case TCPS_LAST_ACK: | |
| 1728 | tp = tcp_close(tp); | |
| 1729 | break; | |
| 1730 | ||
| 1731 | case TCPS_TIME_WAIT: | |
| 1732 | break; | |
| 1733 | } | |
| 1734 | } | |
| 1735 | goto drop; | |
| 1736 | } | |
| 1737 | ||
| 1738 | /* | |
| 1739 | * RFC 1323 PAWS: If we have a timestamp reply on this segment | |
| 1740 | * and it's less than ts_recent, drop it. | |
| 1741 | */ | |
| 61896e3c | 1742 | if ((to.to_flags & TOF_TS) && tp->ts_recent != 0 && |
| 984263bc | 1743 | TSTMP_LT(to.to_tsval, tp->ts_recent)) { |
| 984263bc MD |
1744 | /* Check to see if ts_recent is over 24 days old. */ |
| 1745 | if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { | |
| 1746 | /* | |
| 1747 | * Invalidate ts_recent. If this segment updates | |
| 1748 | * ts_recent, the age will be reset later and ts_recent | |
| 1749 | * will get a valid value. If it does not, setting | |
| 1750 | * ts_recent to zero will at least satisfy the | |
| 1751 | * requirement that zero be placed in the timestamp | |
| 1752 | * echo reply when ts_recent isn't valid. The | |
| 1753 | * age isn't reset until we get a valid ts_recent | |
| 1754 | * because we don't want out-of-order segments to be | |
| 1755 | * dropped when ts_recent is old. | |
| 1756 | */ | |
| 1757 | tp->ts_recent = 0; | |
| 9de1f696 SZ |
1758 | } else if (tcp_paws_tolerance && tlen != 0 && |
| 1759 | tp->t_state == TCPS_ESTABLISHED && | |
| 1760 | (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK&& | |
| 1761 | !(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)) && | |
| 1762 | th->th_ack == tp->snd_una && | |
| 1763 | tiwin == tp->snd_wnd && | |
| 1764 | TSTMP_GEQ(to.to_tsval + tcp_paws_tolerance, tp->ts_recent)&& | |
| 1765 | (th->th_seq == tp->rcv_nxt || | |
| 1766 | (SEQ_GT(th->th_seq, tp->rcv_nxt) && | |
| 1767 | tcp_paws_canreasslast(tp, th, tlen)))) { | |
| 1768 | /* | |
| 1769 | * This tends to prevent valid new segments from being | |
| 1770 | * dropped by the reordered segments sent by the fast | |
| 1771 | * retransmission algorithm on the sending side, i.e. | |
| 1772 | * the fast retransmitted segment w/ larger timestamp | |
| 1773 | * arrives earlier than the previously sent new segments | |
| 1774 | * w/ smaller timestamp. | |
| 1775 | * | |
| 1776 | * If following conditions are met, the segment is | |
| 1777 | * accepted: | |
| 1778 | * - The segment contains data | |
| 1779 | * - The connection is established | |
| 1780 | * - The header does not contain important flags | |
| 1781 | * - SYN or FIN is not needed | |
| 1782 | * - It does not acknowledge new data | |
| 1783 | * - Receive window is not changed | |
| 1784 | * - The timestamp is within "acceptable" range | |
| 1785 | * - The new segment is what we are expecting or | |
| 1786 | * the new segment could be merged w/ the last | |
| 1787 | * pending segment on the reassemble queue | |
| 1788 | */ | |
| 1789 | tcpstat.tcps_pawsaccept++; | |
| 1790 | tcpstat.tcps_pawsdrop++; | |
| 984263bc MD |
1791 | } else { |
| 1792 | tcpstat.tcps_rcvduppack++; | |
| 1793 | tcpstat.tcps_rcvdupbyte += tlen; | |
| 1794 | tcpstat.tcps_pawsdrop++; | |
| 1795 | if (tlen) | |
| 1796 | goto dropafterack; | |
| 1797 | goto drop; | |
| 1798 | } | |
| 1799 | } | |
| 1800 | ||
| 1801 | /* | |
| 984263bc MD |
1802 | * In the SYN-RECEIVED state, validate that the packet belongs to |
| 1803 | * this connection before trimming the data to fit the receive | |
| 1804 | * window. Check the sequence number versus IRS since we know | |
| 1805 | * the sequence numbers haven't wrapped. This is a partial fix | |
| 1806 | * for the "LAND" DoS attack. | |
| 1807 | */ | |
| 1808 | if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { | |
| 1809 | rstreason = BANDLIM_RST_OPENPORT; | |
| 1810 | goto dropwithreset; | |
| 1811 | } | |
| 1812 | ||
| 1813 | todrop = tp->rcv_nxt - th->th_seq; | |
| 1814 | if (todrop > 0) { | |
| 91489f6b JH |
1815 | if (TCP_DO_SACK(tp)) { |
| 1816 | /* Report duplicate segment at head of packet. */ | |
| 1817 | tp->reportblk.rblk_start = th->th_seq; | |
| 3a5d999b SZ |
1818 | tp->reportblk.rblk_end = TCP_SACK_BLKEND( |
| 1819 | th->th_seq + tlen, thflags); | |
| 91489f6b JH |
1820 | if (SEQ_GT(tp->reportblk.rblk_end, tp->rcv_nxt)) |
| 1821 | tp->reportblk.rblk_end = tp->rcv_nxt; | |
| c7e6499a SZ |
1822 | tp->sack_flags |= (TSACK_F_DUPSEG | TSACK_F_SACKLEFT); |
| 1823 | tp->t_flags |= TF_ACKNOW; | |
| 91489f6b | 1824 | } |
| 984263bc MD |
1825 | if (thflags & TH_SYN) { |
| 1826 | thflags &= ~TH_SYN; | |
| 1827 | th->th_seq++; | |
| 1828 | if (th->th_urp > 1) | |
| 1829 | th->th_urp--; | |
| 1830 | else | |
| 1831 | thflags &= ~TH_URG; | |
| 1832 | todrop--; | |
| 1833 | } | |
| 1834 | /* | |
| 1835 | * Following if statement from Stevens, vol. 2, p. 960. | |
| 1836 | */ | |
| 61896e3c JH |
1837 | if (todrop > tlen || |
| 1838 | (todrop == tlen && !(thflags & TH_FIN))) { | |
| 984263bc MD |
1839 | /* |
| 1840 | * Any valid FIN must be to the left of the window. | |
| 1841 | * At this point the FIN must be a duplicate or out | |
| 1842 | * of sequence; drop it. | |
| 1843 | */ | |
| 1844 | thflags &= ~TH_FIN; | |
| 1845 | ||
| 1846 | /* | |
| 1847 | * Send an ACK to resynchronize and drop any data. | |
| 1848 | * But keep on processing for RST or ACK. | |
| 1849 | */ | |
| 1850 | tp->t_flags |= TF_ACKNOW; | |
| 1851 | todrop = tlen; | |
| 1852 | tcpstat.tcps_rcvduppack++; | |
| 1853 | tcpstat.tcps_rcvdupbyte += todrop; | |
| 1854 | } else { | |
| 1855 | tcpstat.tcps_rcvpartduppack++; | |
| 1856 | tcpstat.tcps_rcvpartdupbyte += todrop; | |
| 1857 | } | |
| 1858 | drop_hdrlen += todrop; /* drop from the top afterwards */ | |
| 1859 | th->th_seq += todrop; | |
| 1860 | tlen -= todrop; | |
| 1861 | if (th->th_urp > todrop) | |
| 1862 | th->th_urp -= todrop; | |
| 1863 | else { | |
| 1864 | thflags &= ~TH_URG; | |
| 1865 | th->th_urp = 0; | |
| 1866 | } | |
| 1867 | } | |
| 1868 | ||
| 1869 | /* | |
| 1870 | * If new data are received on a connection after the | |
| 1871 | * user processes are gone, then RST the other end. | |
| 1872 | */ | |
| 1873 | if ((so->so_state & SS_NOFDREF) && | |
| 1874 | tp->t_state > TCPS_CLOSE_WAIT && tlen) { | |
| 1875 | tp = tcp_close(tp); | |
| 1876 | tcpstat.tcps_rcvafterclose++; | |
| 1877 | rstreason = BANDLIM_UNLIMITED; | |
| 1878 | goto dropwithreset; | |
| 1879 | } | |
| 1880 | ||
| 1881 | /* | |
| 1882 | * If segment ends after window, drop trailing data | |
| 1883 | * (and PUSH and FIN); if nothing left, just ACK. | |
| 1884 | */ | |
| 61896e3c | 1885 | todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); |
| 984263bc MD |
1886 | if (todrop > 0) { |
| 1887 | tcpstat.tcps_rcvpackafterwin++; | |
| 1888 | if (todrop >= tlen) { | |
| 1889 | tcpstat.tcps_rcvbyteafterwin += tlen; | |
| 1890 | /* | |
| 1891 | * If a new connection request is received | |
| 1892 | * while in TIME_WAIT, drop the old connection | |
| 1893 | * and start over if the sequence numbers | |
| 1894 | * are above the previous ones. | |
| 1895 | */ | |
| 1896 | if (thflags & TH_SYN && | |
| 1897 | tp->t_state == TCPS_TIME_WAIT && | |
| 1898 | SEQ_GT(th->th_seq, tp->rcv_nxt)) { | |
| 1899 | tp = tcp_close(tp); | |
| 1900 | goto findpcb; | |
| 1901 | } | |
| 1902 | /* | |
| 1903 | * If window is closed can only take segments at | |
| 1904 | * window edge, and have to drop data and PUSH from | |
| 1905 | * incoming segments. Continue processing, but | |
| 1906 | * remember to ack. Otherwise, drop segment | |
| 1907 | * and ack. | |
| 1908 | */ | |
| 1909 | if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { | |
| 1910 | tp->t_flags |= TF_ACKNOW; | |
| 1911 | tcpstat.tcps_rcvwinprobe++; | |
| 1912 | } else | |
| 1913 | goto dropafterack; | |
| 1914 | } else | |
| 1915 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
| 1916 | m_adj(m, -todrop); | |
| 1917 | tlen -= todrop; | |
| 61896e3c | 1918 | thflags &= ~(TH_PUSH | TH_FIN); |
| 984263bc MD |
1919 | } |
| 1920 | ||
| 1921 | /* | |
| 1922 | * If last ACK falls within this segment's sequence numbers, | |
| 1923 | * record its timestamp. | |
| ad0af98b ND |
1924 | * NOTE: |
| 1925 | * 1) That the test incorporates suggestions from the latest | |
| 1926 | * proposal of the tcplw@cray.com list (Braden 1993/04/26). | |
| 1927 | * 2) That updating only on newer timestamps interferes with | |
| 1928 | * our earlier PAWS tests, so this check should be solely | |
| 1929 | * predicated on the sequence space of this segment. | |
| 1930 | * 3) That we modify the segment boundary check to be | |
| 1931 | * Last.ACK.Sent <= SEG.SEQ + SEG.LEN | |
| 1932 | * instead of RFC1323's | |
| 1933 | * Last.ACK.Sent < SEG.SEQ + SEG.LEN, | |
| 1934 | * This modified check allows us to overcome RFC1323's | |
| 1935 | * limitations as described in Stevens TCP/IP Illustrated | |
| 1936 | * Vol. 2 p.869. In such cases, we can still calculate the | |
| 1937 | * RTT correctly when RCV.NXT == Last.ACK.Sent. | |
| 984263bc | 1938 | */ |
| ad0af98b ND |
1939 | if ((to.to_flags & TOF_TS) && SEQ_LEQ(th->th_seq, tp->last_ack_sent) && |
| 1940 | SEQ_LEQ(tp->last_ack_sent, (th->th_seq + tlen | |
| 1941 | + ((thflags & TH_SYN) != 0) | |
| 1942 | + ((thflags & TH_FIN) != 0)))) { | |
| 984263bc MD |
1943 | tp->ts_recent_age = ticks; |
| 1944 | tp->ts_recent = to.to_tsval; | |
| 1945 | } | |
| 1946 | ||
| 1947 | /* | |
| 1948 | * If a SYN is in the window, then this is an | |
| 1949 | * error and we send an RST and drop the connection. | |
| 1950 | */ | |
| 1951 | if (thflags & TH_SYN) { | |
| 1952 | tp = tcp_drop(tp, ECONNRESET); | |
| 1953 | rstreason = BANDLIM_UNLIMITED; | |
| 1954 | goto dropwithreset; | |
| 1955 | } | |
| 1956 | ||
| 1957 | /* | |
| 1958 | * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN | |
| 1959 | * flag is on (half-synchronized state), then queue data for | |
| 1960 | * later processing; else drop segment and return. | |
| 1961 | */ | |
| 61896e3c | 1962 | if (!(thflags & TH_ACK)) { |
| 984263bc MD |
1963 | if (tp->t_state == TCPS_SYN_RECEIVED || |
| 1964 | (tp->t_flags & TF_NEEDSYN)) | |
| 1965 | goto step6; | |
| 1966 | else | |
| 1967 | goto drop; | |
| 1968 | } | |
| 1969 | ||
| 1970 | /* | |
| 1971 | * Ack processing. | |
| 1972 | */ | |
| 1973 | switch (tp->t_state) { | |
| 984263bc | 1974 | /* |
| 91489f6b | 1975 | * In SYN_RECEIVED state, the ACK acknowledges our SYN, so enter |
| 984263bc MD |
1976 | * ESTABLISHED state and continue processing. |
| 1977 | * The ACK was checked above. | |
| 1978 | */ | |
| 1979 | case TCPS_SYN_RECEIVED: | |
| 1980 | ||
| 1981 | tcpstat.tcps_connects++; | |
| 1982 | soisconnected(so); | |
| 1983 | /* Do window scaling? */ | |
| 61896e3c | 1984 | if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == |
| df1d2774 | 1985 | (TF_RCVD_SCALE | TF_REQ_SCALE)) |
| 984263bc | 1986 | tp->rcv_scale = tp->request_r_scale; |
| 984263bc | 1987 | /* |
| 984263bc MD |
1988 | * Make transitions: |
| 1989 | * SYN-RECEIVED -> ESTABLISHED | |
| 1990 | * SYN-RECEIVED* -> FIN-WAIT-1 | |
| 1991 | */ | |
| 1992 | tp->t_starttime = ticks; | |
| 1993 | if (tp->t_flags & TF_NEEDFIN) { | |
| 1994 | tp->t_state = TCPS_FIN_WAIT_1; | |
| 1995 | tp->t_flags &= ~TF_NEEDFIN; | |
| 1996 | } else { | |
| 8651f7f8 | 1997 | tcp_established(tp); |
| 984263bc MD |
1998 | } |
| 1999 | /* | |
| 2000 | * If segment contains data or ACK, will call tcp_reass() | |
| 2001 | * later; if not, do so now to pass queued data to user. | |
| 2002 | */ | |
| 61896e3c | 2003 | if (tlen == 0 && !(thflags & TH_FIN)) |
| f23061d4 | 2004 | tcp_reass(tp, NULL, NULL, NULL); |
| 984263bc MD |
2005 | /* fall into ... */ |
| 2006 | ||
| 2007 | /* | |
| 2008 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range | |
| 2009 | * ACKs. If the ack is in the range | |
| 2010 | * tp->snd_una < th->th_ack <= tp->snd_max | |
| 2011 | * then advance tp->snd_una to th->th_ack and drop | |
| 2012 | * data from the retransmission queue. If this ACK reflects | |
| 2013 | * more up to date window information we update our window information. | |
| 2014 | */ | |
| 2015 | case TCPS_ESTABLISHED: | |
| 2016 | case TCPS_FIN_WAIT_1: | |
| 2017 | case TCPS_FIN_WAIT_2: | |
| 2018 | case TCPS_CLOSE_WAIT: | |
| 2019 | case TCPS_CLOSING: | |
| 2020 | case TCPS_LAST_ACK: | |
| 2021 | case TCPS_TIME_WAIT: | |
| 2022 | ||
| 2023 | if (SEQ_LEQ(th->th_ack, tp->snd_una)) { | |
| 942d88ef SZ |
2024 | boolean_t maynotdup = FALSE; |
| 2025 | ||
| 91489f6b JH |
2026 | if (TCP_DO_SACK(tp)) |
| 2027 | tcp_sack_update_scoreboard(tp, &to); | |
| 942d88ef SZ |
2028 | |
| 2029 | if (tlen != 0 || tiwin != tp->snd_wnd || | |
| 2030 | ((thflags & TH_FIN) && !(tp->t_flags & TF_SAWFIN))) | |
| 2031 | maynotdup = TRUE; | |
| 2032 | ||
| a48c5dd5 | 2033 | if (!tcp_callout_active(tp, tp->tt_rexmt) || |
| 91489f6b | 2034 | th->th_ack != tp->snd_una) { |
| 942d88ef | 2035 | if (!maynotdup) |
| 52f9ffcf | 2036 | tcpstat.tcps_rcvdupack++; |
| 91489f6b JH |
2037 | tp->t_dupacks = 0; |
| 2038 | break; | |
| 2039 | } | |
| 942d88ef SZ |
2040 | |
| 2041 | #define DELAY_DUPACK \ | |
| 2042 | do { \ | |
| 2043 | delayed_dupack = TRUE; \ | |
| 2044 | th_dupack = th->th_ack; \ | |
| 2045 | to_flags = to.to_flags; \ | |
| 2046 | } while (0) | |
| 2047 | if (maynotdup) { | |
| 859bc3f7 | 2048 | if (!tcp_do_rfc6675 || |
| ffe35e17 SZ |
2049 | !TCP_DO_SACK(tp) || |
| 2050 | (to.to_flags & | |
| 2051 | (TOF_SACK | TOF_SACK_REDUNDANT)) | |
| 2052 | != TOF_SACK) { | |
| 2053 | tp->t_dupacks = 0; | |
| 91489f6b | 2054 | } else { |
| 942d88ef | 2055 | DELAY_DUPACK; |
| 91489f6b | 2056 | } |
| 95b22adf | 2057 | break; |
| 95b22adf | 2058 | } |
| 942d88ef SZ |
2059 | if ((thflags & TH_FIN) && !(tp->t_flags & TF_QUEDFIN)) { |
| 2060 | /* | |
| 2061 | * This could happen, if the reassemable | |
| 2062 | * queue overflew or was drained. Don't | |
| 2063 | * drop this FIN here; defer the duplicated | |
| 2064 | * ACK processing until this FIN gets queued. | |
| 2065 | */ | |
| 2066 | DELAY_DUPACK; | |
| 2067 | break; | |
| 2068 | } | |
| 2069 | #undef DELAY_DUPACK | |
| 2070 | ||
| 0a832381 | 2071 | if (tcp_recv_dupack(tp, th->th_ack, to.to_flags)) |
| ffe35e17 | 2072 | goto drop; |
| 27f4bf33 SZ |
2073 | else |
| 2074 | break; | |
| 984263bc MD |
2075 | } |
| 2076 | ||
| 2077 | KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); | |
| 984263bc MD |
2078 | tp->t_dupacks = 0; |
| 2079 | if (SEQ_GT(th->th_ack, tp->snd_max)) { | |
| 5a274421 JH |
2080 | /* |
| 2081 | * Detected optimistic ACK attack. | |
| 2082 | * Force slow-start to de-synchronize attack. | |
| 2083 | */ | |
| 2084 | tp->snd_cwnd = tp->t_maxseg; | |
| 8acdb67c | 2085 | tp->snd_wacked = 0; |
| 5a274421 | 2086 | |
| 984263bc MD |
2087 | tcpstat.tcps_rcvacktoomuch++; |
| 2088 | goto dropafterack; | |
| 2089 | } | |
| 2090 | /* | |
| 2091 | * If we reach this point, ACK is not a duplicate, | |
| 2092 | * i.e., it ACKs something we sent. | |
| 2093 | */ | |
| 2094 | if (tp->t_flags & TF_NEEDSYN) { | |
| 2095 | /* | |
| 2096 | * T/TCP: Connection was half-synchronized, and our | |
| 2097 | * SYN has been ACK'd (so connection is now fully | |
| 2098 | * synchronized). Go to non-starred state, | |
| 2099 | * increment snd_una for ACK of SYN, and check if | |
| 2100 | * we can do window scaling. | |
| 2101 | */ | |
| 2102 | tp->t_flags &= ~TF_NEEDSYN; | |
| 2103 | tp->snd_una++; | |
| 2104 | /* Do window scaling? */ | |
| 61896e3c | 2105 | if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == |
| df1d2774 | 2106 | (TF_RCVD_SCALE | TF_REQ_SCALE)) |
| 984263bc | 2107 | tp->rcv_scale = tp->request_r_scale; |
| 984263bc MD |
2108 | } |
| 2109 | ||
| 2110 | process_ACK: | |
| 2111 | acked = th->th_ack - tp->snd_una; | |
| 2112 | tcpstat.tcps_rcvackpack++; | |
| 2113 | tcpstat.tcps_rcvackbyte += acked; | |
| 2114 | ||
| b5572302 | 2115 | if (tcp_do_eifel_detect && acked > 0 && |
| 95b22adf | 2116 | (to.to_flags & TOF_TS) && (to.to_tsecr != 0) && |
| 1c8b7a61 | 2117 | (tp->rxt_flags & TRXT_F_FIRSTACCACK)) { |
| bfdb979e JH |
2118 | /* Eifel detection applicable. */ |
| 2119 | if (to.to_tsecr < tp->t_rexmtTS) { | |
| bfdb979e | 2120 | ++tcpstat.tcps_eifeldetected; |
| 8819433a | 2121 | tcp_revert_congestion_state(tp); |
| 928c3291 | 2122 | if (tp->t_rxtshift != 1 || |
| 8819433a JH |
2123 | ticks >= tp->t_badrxtwin) |
| 2124 | ++tcpstat.tcps_rttcantdetect; | |
| bfdb979e JH |
2125 | } |
| 2126 | } else if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { | |
| b5572302 JH |
2127 | /* |
| 2128 | * If we just performed our first retransmit, | |
| 2129 | * and the ACK arrives within our recovery window, | |
| 2130 | * then it was a mistake to do the retransmit | |
| 91489f6b | 2131 | * in the first place. Recover our original cwnd |
| b5572302 JH |
2132 | * and ssthresh, and proceed to transmit where we |
| 2133 | * left off. | |
| 2134 | */ | |
| bfdb979e JH |
2135 | tcp_revert_congestion_state(tp); |
| 2136 | ++tcpstat.tcps_rttdetected; | |
| 984263bc MD |
2137 | } |
| 2138 | ||
| 2139 | /* | |
| 2140 | * If we have a timestamp reply, update smoothed | |
| 2141 | * round trip time. If no timestamp is present but | |
| 2142 | * transmit timer is running and timed sequence | |
| 2143 | * number was acked, update smoothed round trip time. | |
| 2144 | * Since we now have an rtt measurement, cancel the | |
| 2145 | * timer backoff (cf., Phil Karn's retransmit alg.). | |
| 2146 | * Recompute the initial retransmit timer. | |
| 2147 | * | |
| 2148 | * Some machines (certain windows boxes) send broken | |
| d24ce1dc | 2149 | * timestamp replies during the SYN+ACK phase, ignore |
| 984263bc MD |
2150 | * timestamps of 0. |
| 2151 | */ | |
| 95b22adf | 2152 | if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) |
| 073ec6c4 | 2153 | tcp_xmit_timer(tp, ticks - to.to_tsecr + 1, th->th_ack); |
| 95b22adf | 2154 | else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) |
| 073ec6c4 | 2155 | tcp_xmit_timer(tp, ticks - tp->t_rtttime, th->th_ack); |
| 984263bc MD |
2156 | tcp_xmit_bandwidth_limit(tp, th->th_ack); |
| 2157 | ||
| 2158 | /* | |
| 984263bc MD |
2159 | * If no data (only SYN) was ACK'd, |
| 2160 | * skip rest of ACK processing. | |
| 2161 | */ | |
| 2162 | if (acked == 0) | |
| 2163 | goto step6; | |
| 2164 | ||
| efd4b327 | 2165 | /* Stop looking for an acceptable ACK since one was received. */ |
| 1c8b7a61 SZ |
2166 | tp->rxt_flags &= ~(TRXT_F_FIRSTACCACK | |
| 2167 | TRXT_F_FASTREXMT | TRXT_F_EARLYREXMT); | |
| efd4b327 | 2168 | |
| 6d49aa6f MD |
2169 | if (acked > so->so_snd.ssb_cc) { |
| 2170 | tp->snd_wnd -= so->so_snd.ssb_cc; | |
| 2171 | sbdrop(&so->so_snd.sb, (int)so->so_snd.ssb_cc); | |
| 61896e3c | 2172 | ourfinisacked = TRUE; |
| 984263bc | 2173 | } else { |
| 6d49aa6f | 2174 | sbdrop(&so->so_snd.sb, acked); |
| 984263bc | 2175 | tp->snd_wnd -= acked; |
| 61896e3c | 2176 | ourfinisacked = FALSE; |
| 984263bc MD |
2177 | } |
| 2178 | sowwakeup(so); | |
| 91489f6b JH |
2179 | |
| 2180 | /* | |
| 2181 | * Update window information. | |
| 91489f6b | 2182 | */ |
| e126661b | 2183 | if (acceptable_window_update(tp, th, tiwin)) { |
| 91489f6b JH |
2184 | /* keep track of pure window updates */ |
| 2185 | if (tlen == 0 && tp->snd_wl2 == th->th_ack && | |
| 2186 | tiwin > tp->snd_wnd) | |
| 2187 | tcpstat.tcps_rcvwinupd++; | |
| 2188 | tp->snd_wnd = tiwin; | |
| 2189 | tp->snd_wl1 = th->th_seq; | |
| 2190 | tp->snd_wl2 = th->th_ack; | |
| 2191 | if (tp->snd_wnd > tp->max_sndwnd) | |
| 2192 | tp->max_sndwnd = tp->snd_wnd; | |
| 2193 | needoutput = TRUE; | |
| 2194 | } | |
| 2195 | ||
| 2196 | tp->snd_una = th->th_ack; | |
| 2197 | if (TCP_DO_SACK(tp)) | |
| 2198 | tcp_sack_update_scoreboard(tp, &to); | |
| 95b22adf | 2199 | if (IN_FASTRECOVERY(tp)) { |
| 91489f6b | 2200 | if (SEQ_GEQ(th->th_ack, tp->snd_recover)) { |
| 95b22adf | 2201 | EXIT_FASTRECOVERY(tp); |
| 91489f6b JH |
2202 | needoutput = TRUE; |
| 2203 | /* | |
| 2204 | * If the congestion window was inflated | |
| 2205 | * to account for the other side's | |
| 2206 | * cached packets, retract it. | |
| 8acdb67c JH |
2207 | */ |
| 2208 | if (!TCP_DO_SACK(tp)) | |
| 2209 | tp->snd_cwnd = tp->snd_ssthresh; | |
| 2210 | ||
| 2211 | /* | |
| 91489f6b JH |
2212 | * Window inflation should have left us |
| 2213 | * with approximately snd_ssthresh outstanding | |
| 2214 | * data. But, in case we would be inclined | |
| 2215 | * to send a burst, better do it using | |
| 2216 | * slow start. | |
| 2217 | */ | |
| 91489f6b JH |
2218 | if (SEQ_GT(th->th_ack + tp->snd_cwnd, |
| 2219 | tp->snd_max + 2 * tp->t_maxseg)) | |
| 2220 | tp->snd_cwnd = | |
| 2221 | (tp->snd_max - tp->snd_una) + | |
| 2222 | 2 * tp->t_maxseg; | |
| 8acdb67c JH |
2223 | |
| 2224 | tp->snd_wacked = 0; | |
| 91489f6b JH |
2225 | } else { |
| 2226 | if (TCP_DO_SACK(tp)) { | |
| 2227 | tp->snd_max_rexmt = tp->snd_max; | |
| ccb518ea SZ |
2228 | tcp_sack_rexmt(tp, |
| 2229 | tp->snd_una == tp->rexmt_high); | |
| 91489f6b JH |
2230 | } else { |
| 2231 | tcp_newreno_partial_ack(tp, th, acked); | |
| 2232 | } | |
| 2233 | needoutput = FALSE; | |
| 2234 | } | |
| 95b22adf | 2235 | } else { |
| 91489f6b | 2236 | /* |
| 8acdb67c JH |
2237 | * Open the congestion window. When in slow-start, |
| 2238 | * open exponentially: maxseg per packet. Otherwise, | |
| 2239 | * open linearly: maxseg per window. | |
| 91489f6b | 2240 | */ |
| 8acdb67c JH |
2241 | if (tp->snd_cwnd <= tp->snd_ssthresh) { |
| 2242 | u_int abc_sslimit = | |
| 2243 | (SEQ_LT(tp->snd_nxt, tp->snd_max) ? | |
| 2244 | tp->t_maxseg : 2 * tp->t_maxseg); | |
| 2245 | ||
| 2246 | /* slow-start */ | |
| 2247 | tp->snd_cwnd += tcp_do_abc ? | |
| 2248 | min(acked, abc_sslimit) : tp->t_maxseg; | |
| 2249 | } else { | |
| 2250 | /* linear increase */ | |
| 2251 | tp->snd_wacked += tcp_do_abc ? acked : | |
| 2252 | tp->t_maxseg; | |
| 2253 | if (tp->snd_wacked >= tp->snd_cwnd) { | |
| 2254 | tp->snd_wacked -= tp->snd_cwnd; | |
| 2255 | tp->snd_cwnd += tp->t_maxseg; | |
| 2256 | } | |
| 2257 | } | |
| 2258 | tp->snd_cwnd = min(tp->snd_cwnd, | |
| 2259 | TCP_MAXWIN << tp->snd_scale); | |
| 95b22adf | 2260 | tp->snd_recover = th->th_ack - 1; |
| cfb3f3f4 | 2261 | } |
| 984263bc MD |
2262 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
| 2263 | tp->snd_nxt = tp->snd_una; | |
| 2264 | ||
| 91489f6b JH |
2265 | /* |
| 2266 | * If all outstanding data is acked, stop retransmit | |
| 2267 | * timer and remember to restart (more output or persist). | |
| 2268 | * If there is more data to be acked, restart retransmit | |
| 2269 | * timer, using current (possibly backed-off) value. | |
| 2270 | */ | |
| 2271 | if (th->th_ack == tp->snd_max) { | |
| a48c5dd5 | 2272 | tcp_callout_stop(tp, tp->tt_rexmt); |
| 91489f6b | 2273 | needoutput = TRUE; |
| a48c5dd5 SZ |
2274 | } else if (!tcp_callout_active(tp, tp->tt_persist)) { |
| 2275 | tcp_callout_reset(tp, tp->tt_rexmt, tp->t_rxtcur, | |
| 2276 | tcp_timer_rexmt); | |
| 2277 | } | |
| 91489f6b | 2278 | |
| 984263bc | 2279 | switch (tp->t_state) { |
| 984263bc MD |
2280 | /* |
| 2281 | * In FIN_WAIT_1 STATE in addition to the processing | |
| 2282 | * for the ESTABLISHED state if our FIN is now acknowledged | |
| 2283 | * then enter FIN_WAIT_2. | |
| 2284 | */ | |
| 2285 | case TCPS_FIN_WAIT_1: | |
| 2286 | if (ourfinisacked) { | |
| 2287 | /* | |
| 2288 | * If we can't receive any more | |
| 2289 | * data, then closing user can proceed. | |
| 2290 | * Starting the timer is contrary to the | |
| 2291 | * specification, but if we don't get a FIN | |
| 2292 | * we'll hang forever. | |
| 2293 | */ | |
| 2294 | if (so->so_state & SS_CANTRCVMORE) { | |
| 2295 | soisdisconnected(so); | |
| a48c5dd5 | 2296 | tcp_callout_reset(tp, tp->tt_2msl, |
| 5d61ded3 | 2297 | tp->t_maxidle, tcp_timer_2msl); |
| 984263bc MD |
2298 | } |
| 2299 | tp->t_state = TCPS_FIN_WAIT_2; | |
| 2300 | } | |
| 2301 | break; | |
| 2302 | ||
| 95b22adf | 2303 | /* |
| 984263bc MD |
2304 | * In CLOSING STATE in addition to the processing for |
| 2305 | * the ESTABLISHED state if the ACK acknowledges our FIN | |
| 2306 | * then enter the TIME-WAIT state, otherwise ignore | |
| 2307 | * the segment. | |
| 2308 | */ | |
| 2309 | case TCPS_CLOSING: | |
| 2310 | if (ourfinisacked) { | |
| 2311 | tp->t_state = TCPS_TIME_WAIT; | |
| 2312 | tcp_canceltimers(tp); | |
| 27b8aee3 | 2313 | tcp_callout_reset(tp, tp->tt_2msl, |
| 01d3427a SZ |
2314 | 2 * tcp_rmx_msl(tp), |
| 2315 | tcp_timer_2msl); | |
| 984263bc MD |
2316 | soisdisconnected(so); |
| 2317 | } | |
| 2318 | break; | |
| 2319 | ||
| 2320 | /* | |
| 2321 | * In LAST_ACK, we may still be waiting for data to drain | |
| 2322 | * and/or to be acked, as well as for the ack of our FIN. | |
| 2323 | * If our FIN is now acknowledged, delete the TCB, | |
| 2324 | * enter the closed state and return. | |
| 2325 | */ | |
| 2326 | case TCPS_LAST_ACK: | |
| 2327 | if (ourfinisacked) { | |
| 2328 | tp = tcp_close(tp); | |
| 2329 | goto drop; | |
| 2330 | } | |
| 2331 | break; | |
| 2332 | ||
| 2333 | /* | |
| 2334 | * In TIME_WAIT state the only thing that should arrive | |
| 2335 | * is a retransmission of the remote FIN. Acknowledge | |
| 2336 | * it and restart the finack timer. | |
| 2337 | */ | |
| 2338 | case TCPS_TIME_WAIT: | |
| 01d3427a | 2339 | tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp), |
| a48c5dd5 | 2340 | tcp_timer_2msl); |
| 984263bc MD |
2341 | goto dropafterack; |
| 2342 | } | |
| 2343 | } | |
| 2344 | ||
| 2345 | step6: | |
| 2346 | /* | |
| 2347 | * Update window information. | |
| 2348 | * Don't look at window if no ACK: TAC's send garbage on first SYN. | |
| 2349 | */ | |
| 2350 | if ((thflags & TH_ACK) && | |
| df9d7670 | 2351 | acceptable_window_update(tp, th, tiwin)) { |
| 984263bc | 2352 | /* keep track of pure window updates */ |
| b5572302 JH |
2353 | if (tlen == 0 && tp->snd_wl2 == th->th_ack && |
| 2354 | tiwin > tp->snd_wnd) | |
| 984263bc MD |
2355 | tcpstat.tcps_rcvwinupd++; |
| 2356 | tp->snd_wnd = tiwin; | |
| 2357 | tp->snd_wl1 = th->th_seq; | |
| 2358 | tp->snd_wl2 = th->th_ack; | |
| 2359 | if (tp->snd_wnd > tp->max_sndwnd) | |
| 2360 | tp->max_sndwnd = tp->snd_wnd; | |
| 61896e3c | 2361 | needoutput = TRUE; |
| 984263bc MD |
2362 | } |
| 2363 | ||
| 2364 | /* | |
| 2365 | * Process segments with URG. | |
| 2366 | */ | |
| 2367 | if ((thflags & TH_URG) && th->th_urp && | |
| 95b22adf | 2368 | !TCPS_HAVERCVDFIN(tp->t_state)) { |
| 984263bc MD |
2369 | /* |
| 2370 | * This is a kludge, but if we receive and accept | |
| 2371 | * random urgent pointers, we'll crash in | |
| 2372 | * soreceive. It's hard to imagine someone | |
| 2373 | * actually wanting to send this much urgent data. | |
| 2374 | */ | |
| 6d49aa6f | 2375 | if (th->th_urp + so->so_rcv.ssb_cc > sb_max) { |
| 984263bc MD |
2376 | th->th_urp = 0; /* XXX */ |
| 2377 | thflags &= ~TH_URG; /* XXX */ | |
| 2378 | goto dodata; /* XXX */ | |
| 2379 | } | |
| 2380 | /* | |
| 2381 | * If this segment advances the known urgent pointer, | |
| 2382 | * then mark the data stream. This should not happen | |
| 2383 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since | |
| 2384 | * a FIN has been received from the remote side. | |
| 2385 | * In these states we ignore the URG. | |
| 2386 | * | |
| 2387 | * According to RFC961 (Assigned Protocols), | |
| 2388 | * the urgent pointer points to the last octet | |
| 2389 | * of urgent data. We continue, however, | |
| 2390 | * to consider it to indicate the first octet | |
| 2391 | * of data past the urgent section as the original | |
| 2392 | * spec states (in one of two places). | |
| 2393 | */ | |
| 61896e3c | 2394 | if (SEQ_GT(th->th_seq + th->th_urp, tp->rcv_up)) { |
| 984263bc | 2395 | tp->rcv_up = th->th_seq + th->th_urp; |
| 6d49aa6f | 2396 | so->so_oobmark = so->so_rcv.ssb_cc + |
| 984263bc MD |
2397 | (tp->rcv_up - tp->rcv_nxt) - 1; |
| 2398 | if (so->so_oobmark == 0) | |
| 6cef7136 | 2399 | sosetstate(so, SS_RCVATMARK); |
| 984263bc MD |
2400 | sohasoutofband(so); |
| 2401 | tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); | |
| 2402 | } | |
| 2403 | /* | |
| 2404 | * Remove out of band data so doesn't get presented to user. | |
| 2405 | * This can happen independent of advancing the URG pointer, | |
| 2406 | * but if two URG's are pending at once, some out-of-band | |
| 2407 | * data may creep in... ick. | |
| 2408 | */ | |
| 61896e3c JH |
2409 | if (th->th_urp <= (u_long)tlen && |
| 2410 | !(so->so_options & SO_OOBINLINE)) { | |
| 2411 | /* hdr drop is delayed */ | |
| 2412 | tcp_pulloutofband(so, th, m, drop_hdrlen); | |
| 2413 | } | |
| 984263bc MD |
2414 | } else { |
| 2415 | /* | |
| 2416 | * If no out of band data is expected, | |
| 2417 | * pull receive urgent pointer along | |
| 2418 | * with the receive window. | |
| 2419 | */ | |
| 2420 | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) | |
| 2421 | tp->rcv_up = tp->rcv_nxt; | |
| 2422 | } | |
| 984263bc | 2423 | |
| 61896e3c | 2424 | dodata: /* XXX */ |
| 984263bc MD |
2425 | /* |
| 2426 | * Process the segment text, merging it into the TCP sequencing queue, | |
| 2427 | * and arranging for acknowledgment of receipt if necessary. | |
| 2428 | * This process logically involves adjusting tp->rcv_wnd as data | |
| 2429 | * is presented to the user (this happens in tcp_usrreq.c, | |
| 2430 | * case PRU_RCVD). If a FIN has already been received on this | |
| 2431 | * connection then we just ignore the text. | |
| 2432 | */ | |
| 95b22adf | 2433 | if ((tlen || (thflags & TH_FIN)) && !TCPS_HAVERCVDFIN(tp->t_state)) { |
| 942d88ef SZ |
2434 | if (thflags & TH_FIN) |
| 2435 | tp->t_flags |= TF_SAWFIN; | |
| 984263bc MD |
2436 | m_adj(m, drop_hdrlen); /* delayed header drop */ |
| 2437 | /* | |
| 2438 | * Insert segment which includes th into TCP reassembly queue | |
| 2439 | * with control block tp. Set thflags to whether reassembly now | |
| 2440 | * includes a segment with FIN. This handles the common case | |
| 2441 | * inline (segment is the next to be received on an established | |
| 2442 | * connection, and the queue is empty), avoiding linkage into | |
| 2443 | * and removal from the queue and repetition of various | |
| 2444 | * conversions. | |
| 2445 | * Set DELACK for segments received in order, but ack | |
| 2446 | * immediately when segments are out of order (so | |
| 2447 | * fast retransmit can work). | |
| 2448 | */ | |
| 2449 | if (th->th_seq == tp->rcv_nxt && | |
| 0f9e45de | 2450 | TAILQ_EMPTY(&tp->t_segq) && |
| 984263bc | 2451 | TCPS_HAVEESTABLISHED(tp->t_state)) { |
| 942d88ef SZ |
2452 | if (thflags & TH_FIN) |
| 2453 | tp->t_flags |= TF_QUEDFIN; | |
| a48c5dd5 SZ |
2454 | if (DELAY_ACK(tp)) { |
| 2455 | tcp_callout_reset(tp, tp->tt_delack, | |
| 2456 | tcp_delacktime, tcp_timer_delack); | |
| 2457 | } else { | |
| 984263bc | 2458 | tp->t_flags |= TF_ACKNOW; |
| a48c5dd5 | 2459 | } |
| 984263bc MD |
2460 | tp->rcv_nxt += tlen; |
| 2461 | thflags = th->th_flags & TH_FIN; | |
| 2462 | tcpstat.tcps_rcvpack++; | |
| 2463 | tcpstat.tcps_rcvbyte += tlen; | |
| 2464 | ND6_HINT(tp); | |
| 6cef7136 | 2465 | if (so->so_state & SS_CANTRCVMORE) { |
| 984263bc | 2466 | m_freem(m); |
| 6cef7136 MD |
2467 | } else { |
| 2468 | lwkt_gettoken(&so->so_rcv.ssb_token); | |
| 6d49aa6f | 2469 | ssb_appendstream(&so->so_rcv, m); |
| 6cef7136 MD |
2470 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 2471 | } | |
| 984263bc MD |
2472 | sorwakeup(so); |
| 2473 | } else { | |
| c7e6499a | 2474 | if (!(tp->sack_flags & TSACK_F_DUPSEG)) { |
| 91489f6b JH |
2475 | /* Initialize SACK report block. */ |
| 2476 | tp->reportblk.rblk_start = th->th_seq; | |
| 3a5d999b SZ |
2477 | tp->reportblk.rblk_end = TCP_SACK_BLKEND( |
| 2478 | th->th_seq + tlen, thflags); | |
| 91489f6b | 2479 | } |
| 984263bc MD |
2480 | thflags = tcp_reass(tp, th, &tlen, m); |
| 2481 | tp->t_flags |= TF_ACKNOW; | |
| 2482 | } | |
| 2483 | ||
| 2484 | /* | |
| 2485 | * Note the amount of data that peer has sent into | |
| 2486 | * our window, in order to estimate the sender's | |
| 2487 | * buffer size. | |
| 2488 | */ | |
| 6d49aa6f | 2489 | len = so->so_rcv.ssb_hiwat - (tp->rcv_adv - tp->rcv_nxt); |
| 984263bc MD |
2490 | } else { |
| 2491 | m_freem(m); | |
| 2492 | thflags &= ~TH_FIN; | |
| 2493 | } | |
| 2494 | ||
| 2495 | /* | |
| 2496 | * If FIN is received ACK the FIN and let the user know | |
| 2497 | * that the connection is closing. | |
| 2498 | */ | |
| 2499 | if (thflags & TH_FIN) { | |
| 95b22adf | 2500 | if (!TCPS_HAVERCVDFIN(tp->t_state)) { |
| 984263bc MD |
2501 | socantrcvmore(so); |
| 2502 | /* | |
| 2503 | * If connection is half-synchronized | |
| 2504 | * (ie NEEDSYN flag on) then delay ACK, | |
| 2505 | * so it may be piggybacked when SYN is sent. | |
| 2506 | * Otherwise, since we received a FIN then no | |
| 2507 | * more input can be expected, send ACK now. | |
| 2508 | */ | |
| a48c5dd5 SZ |
2509 | if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN)) { |
| 2510 | tcp_callout_reset(tp, tp->tt_delack, | |
| 2511 | tcp_delacktime, tcp_timer_delack); | |
| 2512 | } else { | |
| 984263bc | 2513 | tp->t_flags |= TF_ACKNOW; |
| a48c5dd5 | 2514 | } |
| 984263bc MD |
2515 | tp->rcv_nxt++; |
| 2516 | } | |
| 984263bc | 2517 | |
| 61896e3c | 2518 | switch (tp->t_state) { |
| 95b22adf | 2519 | /* |
| 984263bc MD |
2520 | * In SYN_RECEIVED and ESTABLISHED STATES |
| 2521 | * enter the CLOSE_WAIT state. | |
| 2522 | */ | |
| 2523 | case TCPS_SYN_RECEIVED: | |
| 2524 | tp->t_starttime = ticks; | |
| 2525 | /*FALLTHROUGH*/ | |
| 2526 | case TCPS_ESTABLISHED: | |
| 2527 | tp->t_state = TCPS_CLOSE_WAIT; | |
| 2528 | break; | |
| 2529 | ||
| 95b22adf | 2530 | /* |
| 984263bc MD |
2531 | * If still in FIN_WAIT_1 STATE FIN has not been acked so |
| 2532 | * enter the CLOSING state. | |
| 2533 | */ | |
| 2534 | case TCPS_FIN_WAIT_1: | |
| 2535 | tp->t_state = TCPS_CLOSING; | |
| 2536 | break; | |
| 2537 | ||
| 95b22adf | 2538 | /* |
| 984263bc MD |
2539 | * In FIN_WAIT_2 state enter the TIME_WAIT state, |
| 2540 | * starting the time-wait timer, turning off the other | |
| 2541 | * standard timers. | |
| 2542 | */ | |
| 2543 | case TCPS_FIN_WAIT_2: | |
| 2544 | tp->t_state = TCPS_TIME_WAIT; | |
| 2545 | tcp_canceltimers(tp); | |
| 01d3427a | 2546 | tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp), |
| a48c5dd5 | 2547 | tcp_timer_2msl); |
| 984263bc MD |
2548 | soisdisconnected(so); |
| 2549 | break; | |
| 2550 | ||
| 2551 | /* | |
| 2552 | * In TIME_WAIT state restart the 2 MSL time_wait timer. | |
| 2553 | */ | |
| 2554 | case TCPS_TIME_WAIT: | |
| 01d3427a | 2555 | tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp), |
| a48c5dd5 | 2556 | tcp_timer_2msl); |
| 984263bc MD |
2557 | break; |
| 2558 | } | |
| 2559 | } | |
| 61896e3c | 2560 | |
| 984263bc MD |
2561 | #ifdef TCPDEBUG |
| 2562 | if (so->so_options & SO_DEBUG) | |
| f23061d4 | 2563 | tcp_trace(TA_INPUT, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0); |
| 984263bc MD |
2564 | #endif |
| 2565 | ||
| 2566 | /* | |
| 27f4bf33 SZ |
2567 | * Delayed duplicated ACK processing |
| 2568 | */ | |
| 0a832381 | 2569 | if (delayed_dupack && tcp_recv_dupack(tp, th_dupack, to_flags)) |
| 27f4bf33 SZ |
2570 | needoutput = FALSE; |
| 2571 | ||
| 2572 | /* | |
| 984263bc MD |
2573 | * Return any desired output. |
| 2574 | */ | |
| 2575 | if (needoutput || (tp->t_flags & TF_ACKNOW)) | |
| f23061d4 | 2576 | tcp_output(tp); |
| d58ca578 | 2577 | tcp_sack_report_cleanup(tp); |
| 002c1265 | 2578 | return(IPPROTO_DONE); |
| 984263bc MD |
2579 | |
| 2580 | dropafterack: | |
| 2581 | /* | |
| 2582 | * Generate an ACK dropping incoming segment if it occupies | |
| 2583 | * sequence space, where the ACK reflects our state. | |
| 2584 | * | |
| 2585 | * We can now skip the test for the RST flag since all | |
| 2586 | * paths to this code happen after packets containing | |
| 2587 | * RST have been dropped. | |
| 2588 | * | |
| 2589 | * In the SYN-RECEIVED state, don't send an ACK unless the | |
| 2590 | * segment we received passes the SYN-RECEIVED ACK test. | |
| 2591 | * If it fails send a RST. This breaks the loop in the | |
| 2592 | * "LAND" DoS attack, and also prevents an ACK storm | |
| 2593 | * between two listening ports that have been sent forged | |
| 2594 | * SYN segments, each with the source address of the other. | |
| 2595 | */ | |
| 2596 | if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && | |
| 2597 | (SEQ_GT(tp->snd_una, th->th_ack) || | |
| 2598 | SEQ_GT(th->th_ack, tp->snd_max)) ) { | |
| 2599 | rstreason = BANDLIM_RST_OPENPORT; | |
| 2600 | goto dropwithreset; | |
| 2601 | } | |
| 2602 | #ifdef TCPDEBUG | |
| 2603 | if (so->so_options & SO_DEBUG) | |
| f23061d4 | 2604 | tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0); |
| 984263bc MD |
2605 | #endif |
| 2606 | m_freem(m); | |
| 2607 | tp->t_flags |= TF_ACKNOW; | |
| f23061d4 | 2608 | tcp_output(tp); |
| d58ca578 | 2609 | tcp_sack_report_cleanup(tp); |
| 002c1265 | 2610 | return(IPPROTO_DONE); |
| 984263bc MD |
2611 | |
| 2612 | dropwithreset: | |
| 2613 | /* | |
| 2614 | * Generate a RST, dropping incoming segment. | |
| 2615 | * Make ACK acceptable to originator of segment. | |
| 2616 | * Don't bother to respond if destination was broadcast/multicast. | |
| 2617 | */ | |
| 61896e3c | 2618 | if ((thflags & TH_RST) || m->m_flags & (M_BCAST | M_MCAST)) |
| 984263bc MD |
2619 | goto drop; |
| 2620 | if (isipv6) { | |
| 2621 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || | |
| 2622 | IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) | |
| 2623 | goto drop; | |
| 2624 | } else { | |
| 2625 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || | |
| 2626 | IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || | |
| 95b22adf JH |
2627 | ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || |
| 2628 | in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) | |
| 984263bc MD |
2629 | goto drop; |
| 2630 | } | |
| 2631 | /* IPv6 anycast check is done at tcp6_input() */ | |
| 2632 | ||
| 2633 | /* | |
| 2634 | * Perform bandwidth limiting. | |
| 2635 | */ | |
| 2636 | #ifdef ICMP_BANDLIM | |
| 2637 | if (badport_bandlim(rstreason) < 0) | |
| 2638 | goto drop; | |
| 2639 | #endif | |
| 2640 | ||
| 2641 | #ifdef TCPDEBUG | |
| 2642 | if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) | |
| f23061d4 | 2643 | tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0); |
| 984263bc MD |
2644 | #endif |
| 2645 | if (thflags & TH_ACK) | |
| 2646 | /* mtod() below is safe as long as hdr dropping is delayed */ | |
| 2647 | tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, | |
| 2648 | TH_RST); | |
| 2649 | else { | |
| 2650 | if (thflags & TH_SYN) | |
| 2651 | tlen++; | |
| 2652 | /* mtod() below is safe as long as hdr dropping is delayed */ | |
| 61896e3c JH |
2653 | tcp_respond(tp, mtod(m, void *), th, m, th->th_seq + tlen, |
| 2654 | (tcp_seq)0, TH_RST | TH_ACK); | |
| 984263bc | 2655 | } |
| d58ca578 SZ |
2656 | if (tp != NULL) |
| 2657 | tcp_sack_report_cleanup(tp); | |
| 002c1265 | 2658 | return(IPPROTO_DONE); |
| 984263bc MD |
2659 | |
| 2660 | drop: | |
| 2661 | /* | |
| 2662 | * Drop space held by incoming segment and return. | |
| 2663 | */ | |
| 2664 | #ifdef TCPDEBUG | |
| 2665 | if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) | |
| f23061d4 | 2666 | tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0); |
| 984263bc MD |
2667 | #endif |
| 2668 | m_freem(m); | |
| d58ca578 SZ |
2669 | if (tp != NULL) |
| 2670 | tcp_sack_report_cleanup(tp); | |
| 002c1265 | 2671 | return(IPPROTO_DONE); |
| 984263bc MD |
2672 | } |
| 2673 | ||
| 2674 | /* | |
| 2675 | * Parse TCP options and place in tcpopt. | |
| 2676 | */ | |
| 2677 | static void | |
| 6c1bbf57 SZ |
2678 | tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, boolean_t is_syn, |
| 2679 | tcp_seq ack) | |
| 984263bc | 2680 | { |
| 91489f6b | 2681 | int opt, optlen, i; |
| 984263bc MD |
2682 | |
| 2683 | to->to_flags = 0; | |
| 2684 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
| 2685 | opt = cp[0]; | |
| 2686 | if (opt == TCPOPT_EOL) | |
| 2687 | break; | |
| 2688 | if (opt == TCPOPT_NOP) | |
| 2689 | optlen = 1; | |
| 2690 | else { | |
| 2691 | if (cnt < 2) | |
| 2692 | break; | |
| 2693 | optlen = cp[1]; | |
| 2694 | if (optlen < 2 || optlen > cnt) | |
| 2695 | break; | |
| 2696 | } | |
| 2697 | switch (opt) { | |
| 2698 | case TCPOPT_MAXSEG: | |
| 2699 | if (optlen != TCPOLEN_MAXSEG) | |
| 2700 | continue; | |
| 2701 | if (!is_syn) | |
| 2702 | continue; | |
| 2703 | to->to_flags |= TOF_MSS; | |
| 407e896e | 2704 | bcopy(cp + 2, &to->to_mss, sizeof to->to_mss); |
| 984263bc MD |
2705 | to->to_mss = ntohs(to->to_mss); |
| 2706 | break; | |
| 2707 | case TCPOPT_WINDOW: | |
| 2708 | if (optlen != TCPOLEN_WINDOW) | |
| 2709 | continue; | |
| 95b22adf | 2710 | if (!is_syn) |
| 984263bc MD |
2711 | continue; |
| 2712 | to->to_flags |= TOF_SCALE; | |
| 2713 | to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); | |
| 2714 | break; | |
| 2715 | case TCPOPT_TIMESTAMP: | |
| 2716 | if (optlen != TCPOLEN_TIMESTAMP) | |
| 2717 | continue; | |
| 2718 | to->to_flags |= TOF_TS; | |
| 407e896e | 2719 | bcopy(cp + 2, &to->to_tsval, sizeof to->to_tsval); |
| 984263bc | 2720 | to->to_tsval = ntohl(to->to_tsval); |
| 407e896e | 2721 | bcopy(cp + 6, &to->to_tsecr, sizeof to->to_tsecr); |
| 984263bc | 2722 | to->to_tsecr = ntohl(to->to_tsecr); |
| ad0af98b ND |
2723 | /* |
| 2724 | * If echoed timestamp is later than the current time, | |
| 2725 | * fall back to non RFC1323 RTT calculation. | |
| 2726 | */ | |
| 2727 | if (to->to_tsecr != 0 && TSTMP_GT(to->to_tsecr, ticks)) | |
| 2728 | to->to_tsecr = 0; | |
| 984263bc | 2729 | break; |
| 91489f6b JH |
2730 | case TCPOPT_SACK_PERMITTED: |
| 2731 | if (optlen != TCPOLEN_SACK_PERMITTED) | |
| 2732 | continue; | |
| 2733 | if (!is_syn) | |
| 2734 | continue; | |
| 2735 | to->to_flags |= TOF_SACK_PERMITTED; | |
| 2736 | break; | |
| 2737 | case TCPOPT_SACK: | |
| 2738 | if ((optlen - 2) & 0x07) /* not multiple of 8 */ | |
| 2739 | continue; | |
| 2740 | to->to_nsackblocks = (optlen - 2) / 8; | |
| 2741 | to->to_sackblocks = (struct raw_sackblock *) (cp + 2); | |
| 2742 | to->to_flags |= TOF_SACK; | |
| 2743 | for (i = 0; i < to->to_nsackblocks; i++) { | |
| 2744 | struct raw_sackblock *r = &to->to_sackblocks[i]; | |
| 2745 | ||
| 2746 | r->rblk_start = ntohl(r->rblk_start); | |
| 2747 | r->rblk_end = ntohl(r->rblk_end); | |
| 865b0477 SZ |
2748 | |
| 2749 | if (SEQ_LEQ(r->rblk_end, r->rblk_start)) { | |
| 2750 | /* | |
| 2751 | * Invalid SACK block; discard all | |
| 2752 | * SACK blocks | |
| 2753 | */ | |
| 02cc2f35 | 2754 | tcpstat.tcps_rcvbadsackopt++; |
| 865b0477 SZ |
2755 | to->to_nsackblocks = 0; |
| 2756 | to->to_sackblocks = NULL; | |
| 2757 | to->to_flags &= ~TOF_SACK; | |
| 2758 | break; | |
| 2759 | } | |
| 91489f6b | 2760 | } |
| 6c1bbf57 SZ |
2761 | if ((to->to_flags & TOF_SACK) && |
| 2762 | tcp_sack_ndsack_blocks(to->to_sackblocks, | |
| 2763 | to->to_nsackblocks, ack)) | |
| 2764 | to->to_flags |= TOF_DSACK; | |
| 91489f6b | 2765 | break; |
| b1992928 MD |
2766 | #ifdef TCP_SIGNATURE |
| 2767 | /* | |
| 2768 | * XXX In order to reply to a host which has set the | |
| 2769 | * TCP_SIGNATURE option in its initial SYN, we have to | |
| 2770 | * record the fact that the option was observed here | |
| 2771 | * for the syncache code to perform the correct response. | |
| 2772 | */ | |
| 2773 | case TCPOPT_SIGNATURE: | |
| 2774 | if (optlen != TCPOLEN_SIGNATURE) | |
| 2775 | continue; | |
| 2776 | to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN); | |
| 2777 | break; | |
| 2778 | #endif /* TCP_SIGNATURE */ | |
| 984263bc MD |
2779 | default: |
| 2780 | continue; | |
| 2781 | } | |
| 2782 | } | |
| 2783 | } | |
| 2784 | ||
| 2785 | /* | |
| 2786 | * Pull out of band byte out of a segment so | |
| 2787 | * it doesn't appear in the user's data queue. | |
| 2788 | * It is still reflected in the segment length for | |
| 2789 | * sequencing purposes. | |
| 95b22adf | 2790 | * "off" is the delayed to be dropped hdrlen. |
| 984263bc MD |
2791 | */ |
| 2792 | static void | |
| 95b22adf | 2793 | tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, int off) |
| 984263bc MD |
2794 | { |
| 2795 | int cnt = off + th->th_urp - 1; | |
| 2796 | ||
| 2797 | while (cnt >= 0) { | |
| 2798 | if (m->m_len > cnt) { | |
| 2799 | char *cp = mtod(m, caddr_t) + cnt; | |
| 2800 | struct tcpcb *tp = sototcpcb(so); | |
| 2801 | ||
| 2802 | tp->t_iobc = *cp; | |
| 2803 | tp->t_oobflags |= TCPOOB_HAVEDATA; | |
| 95b22adf | 2804 | bcopy(cp + 1, cp, m->m_len - cnt - 1); |
| 984263bc MD |
2805 | m->m_len--; |
| 2806 | if (m->m_flags & M_PKTHDR) | |
| 2807 | m->m_pkthdr.len--; | |
| 2808 | return; | |
| 2809 | } | |
| 2810 | cnt -= m->m_len; | |
| 2811 | m = m->m_next; | |
| 4090d6ff | 2812 | if (m == NULL) |
| 984263bc MD |
2813 | break; |
| 2814 | } | |
| 2815 | panic("tcp_pulloutofband"); | |
| 2816 | } | |
| 2817 | ||
| 2818 | /* | |
| 2819 | * Collect new round-trip time estimate | |
| 2820 | * and update averages and current timeout. | |
| 2821 | */ | |
| 2822 | static void | |
| 073ec6c4 | 2823 | tcp_xmit_timer(struct tcpcb *tp, int rtt, tcp_seq ack) |
| 984263bc | 2824 | { |
| 073ec6c4 | 2825 | int rebaserto = 0; |
| 984263bc MD |
2826 | |
| 2827 | tcpstat.tcps_rttupdated++; | |
| 2828 | tp->t_rttupdated++; | |
| 1c8b7a61 SZ |
2829 | if ((tp->rxt_flags & TRXT_F_REBASERTO) && |
| 2830 | SEQ_GT(ack, tp->snd_max_prev)) { | |
| 073ec6c4 SZ |
2831 | #ifdef DEBUG_EIFEL_RESPONSE |
| 2832 | kprintf("srtt/rttvar, prev %d/%d, cur %d/%d, ", | |
| 2833 | tp->t_srtt_prev, tp->t_rttvar_prev, | |
| 2834 | tp->t_srtt, tp->t_rttvar); | |
| 2835 | #endif | |
| 2836 | ||
| 2837 | tcpstat.tcps_eifelresponse++; | |
| 2838 | rebaserto = 1; | |
| 1c8b7a61 | 2839 | tp->rxt_flags &= ~TRXT_F_REBASERTO; |
| 073ec6c4 SZ |
2840 | tp->t_srtt = max(tp->t_srtt_prev, (rtt << TCP_RTT_SHIFT)); |
| 2841 | tp->t_rttvar = max(tp->t_rttvar_prev, | |
| 2842 | (rtt << (TCP_RTTVAR_SHIFT - 1))); | |
| 2843 | if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) | |
| 2844 | tp->t_rttbest = tp->t_srtt + tp->t_rttvar; | |
| 2845 | ||
| 2846 | #ifdef DEBUG_EIFEL_RESPONSE | |
| 2847 | kprintf("new %d/%d ", tp->t_srtt, tp->t_rttvar); | |
| 2848 | #endif | |
| 2849 | } else if (tp->t_srtt != 0) { | |
| 2850 | int delta; | |
| 2851 | ||
| 984263bc MD |
2852 | /* |
| 2853 | * srtt is stored as fixed point with 5 bits after the | |
| 2854 | * binary point (i.e., scaled by 8). The following magic | |
| 2855 | * is equivalent to the smoothing algorithm in rfc793 with | |
| 2856 | * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed | |
| 2857 | * point). Adjust rtt to origin 0. | |
| 2858 | */ | |
| 2859 | delta = ((rtt - 1) << TCP_DELTA_SHIFT) | |
| 2860 | - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); | |
| 2861 | ||
| 2862 | if ((tp->t_srtt += delta) <= 0) | |
| 2863 | tp->t_srtt = 1; | |
| 2864 | ||
| 2865 | /* | |
| 2866 | * We accumulate a smoothed rtt variance (actually, a | |
| 2867 | * smoothed mean difference), then set the retransmit | |
| 2868 | * timer to smoothed rtt + 4 times the smoothed variance. | |
| 2869 | * rttvar is stored as fixed point with 4 bits after the | |
| 2870 | * binary point (scaled by 16). The following is | |
| 2871 | * equivalent to rfc793 smoothing with an alpha of .75 | |
| 2872 | * (rttvar = rttvar*3/4 + |delta| / 4). This replaces | |
| 2873 | * rfc793's wired-in beta. | |
| 2874 | */ | |
| 2875 | if (delta < 0) | |
| 2876 | delta = -delta; | |
| 2877 | delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); | |
| 2878 | if ((tp->t_rttvar += delta) <= 0) | |
| 2879 | tp->t_rttvar = 1; | |
| 2880 | if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) | |
| 2881 | tp->t_rttbest = tp->t_srtt + tp->t_rttvar; | |
| 2882 | } else { | |
| 2883 | /* | |
| 2884 | * No rtt measurement yet - use the unsmoothed rtt. | |
| 2885 | * Set the variance to half the rtt (so our first | |
| 2886 | * retransmit happens at 3*rtt). | |
| 2887 | */ | |
| 2888 | tp->t_srtt = rtt << TCP_RTT_SHIFT; | |
| 2889 | tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); | |
| 2890 | tp->t_rttbest = tp->t_srtt + tp->t_rttvar; | |
| 2891 | } | |
| 2892 | tp->t_rtttime = 0; | |
| 2893 | tp->t_rxtshift = 0; | |
| 2894 | ||
| 073ec6c4 SZ |
2895 | #ifdef DEBUG_EIFEL_RESPONSE |
| 2896 | if (rebaserto) { | |
| 2897 | kprintf("| rxtcur prev %d, old %d, ", | |
| 2898 | tp->t_rxtcur_prev, tp->t_rxtcur); | |
| 2899 | } | |
| 2900 | #endif | |
| 2901 | ||
| 984263bc MD |
2902 | /* |
| 2903 | * the retransmit should happen at rtt + 4 * rttvar. | |
| 2904 | * Because of the way we do the smoothing, srtt and rttvar | |
| 2905 | * will each average +1/2 tick of bias. When we compute | |
| 2906 | * the retransmit timer, we want 1/2 tick of rounding and | |
| 2907 | * 1 extra tick because of +-1/2 tick uncertainty in the | |
| 2908 | * firing of the timer. The bias will give us exactly the | |
| 2909 | * 1.5 tick we need. But, because the bias is | |
| 2910 | * statistical, we have to test that we don't drop below | |
| 2911 | * the minimum feasible timer (which is 2 ticks). | |
| 2912 | */ | |
| 2913 | TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), | |
| 2914 | max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); | |
| 2915 | ||
| 073ec6c4 SZ |
2916 | if (rebaserto) { |
| 2917 | if (tp->t_rxtcur < tp->t_rxtcur_prev + tcp_eifel_rtoinc) { | |
| 2918 | /* | |
| 2919 | * RFC4015 requires that the new RTO is at least | |
| 2920 | * 2*G (tcp_eifel_rtoinc) greater then the RTO | |
| 2921 | * (t_rxtcur_prev) when the spurious retransmit | |
| 2922 | * timeout happens. | |
| 2923 | * | |
| 2924 | * The above condition could be true, if the SRTT | |
| 2925 | * and RTTVAR used to calculate t_rxtcur_prev | |
| 2926 | * resulted in a value less than t_rttmin. So | |
| 2927 | * simply increasing SRTT by tcp_eifel_rtoinc when | |
| e1dd9e15 | 2928 | * preparing for the Eifel response could not ensure |
| 073ec6c4 SZ |
2929 | * that the new RTO will be tcp_eifel_rtoinc greater |
| 2930 | * t_rxtcur_prev. | |
| 2931 | */ | |
| 2932 | tp->t_rxtcur = tp->t_rxtcur_prev + tcp_eifel_rtoinc; | |
| 2933 | } | |
| 2934 | #ifdef DEBUG_EIFEL_RESPONSE | |
| 2935 | kprintf("new %d\n", tp->t_rxtcur); | |
| 2936 | #endif | |
| 2937 | } | |
| 2938 | ||
| 984263bc MD |
2939 | /* |
| 2940 | * We received an ack for a packet that wasn't retransmitted; | |
| 2941 | * it is probably safe to discard any error indications we've | |
| 2942 | * received recently. This isn't quite right, but close enough | |
| 2943 | * for now (a route might have failed after we sent a segment, | |
| 2944 | * and the return path might not be symmetrical). | |
| 2945 | */ | |
| 2946 | tp->t_softerror = 0; | |
| 2947 | } | |
| 2948 | ||
| 2949 | /* | |
| 2950 | * Determine a reasonable value for maxseg size. | |
| 2951 | * If the route is known, check route for mtu. | |
| 2952 | * If none, use an mss that can be handled on the outgoing | |
| 2953 | * interface without forcing IP to fragment; if bigger than | |
| 2954 | * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES | |
| 2955 | * to utilize large mbufs. If no route is found, route has no mtu, | |
| 2956 | * or the destination isn't local, use a default, hopefully conservative | |
| 2957 | * size (usually 512 or the default IP max size, but no more than the mtu | |
| 2958 | * of the interface), as we can't discover anything about intervening | |
| 2959 | * gateways or networks. We also initialize the congestion/slow start | |
| 2960 | * window to be a single segment if the destination isn't local. | |
| 2961 | * While looking at the routing entry, we also initialize other path-dependent | |
| 2962 | * parameters from pre-set or cached values in the routing entry. | |
| 2963 | * | |
| 2964 | * Also take into account the space needed for options that we | |
| 2965 | * send regularly. Make maxseg shorter by that amount to assure | |
| 2966 | * that we can send maxseg amount of data even when the options | |
| 2967 | * are present. Store the upper limit of the length of options plus | |
| 2968 | * data in maxopd. | |
| 2969 | * | |
| 2970 | * NOTE that this routine is only called when we process an incoming | |
| 2971 | * segment, for outgoing segments only tcp_mssopt is called. | |
| 984263bc MD |
2972 | */ |
| 2973 | void | |
| 95b22adf | 2974 | tcp_mss(struct tcpcb *tp, int offer) |
| 984263bc | 2975 | { |
| 2256ba69 | 2976 | struct rtentry *rt; |
| 984263bc | 2977 | struct ifnet *ifp; |
| 2256ba69 | 2978 | int rtt, mss; |
| 984263bc MD |
2979 | u_long bufsize; |
| 2980 | struct inpcb *inp = tp->t_inpcb; | |
| 2981 | struct socket *so; | |
| 984263bc | 2982 | #ifdef INET6 |
| d24ce1dc | 2983 | boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) ? TRUE : FALSE); |
| 984263bc MD |
2984 | size_t min_protoh = isipv6 ? |
| 2985 | sizeof(struct ip6_hdr) + sizeof(struct tcphdr) : | |
| 2986 | sizeof(struct tcpiphdr); | |
| 2987 | #else | |
| d24ce1dc | 2988 | const boolean_t isipv6 = FALSE; |
| 984263bc MD |
2989 | const size_t min_protoh = sizeof(struct tcpiphdr); |
| 2990 | #endif | |
| 2991 | ||
| 2992 | if (isipv6) | |
| 2993 | rt = tcp_rtlookup6(&inp->inp_inc); | |
| 2994 | else | |
| 2995 | rt = tcp_rtlookup(&inp->inp_inc); | |
| 2996 | if (rt == NULL) { | |
| 2997 | tp->t_maxopd = tp->t_maxseg = | |
| d24ce1dc | 2998 | (isipv6 ? tcp_v6mssdflt : tcp_mssdflt); |
| 984263bc MD |
2999 | return; |
| 3000 | } | |
| 3001 | ifp = rt->rt_ifp; | |
| 3002 | so = inp->inp_socket; | |
| 3003 | ||
| 984263bc MD |
3004 | /* |
| 3005 | * Offer == 0 means that there was no MSS on the SYN segment, | |
| b235ad6d MD |
3006 | * in this case we use either the interface mtu or tcp_mssdflt. |
| 3007 | * | |
| 3008 | * An offer which is too large will be cut down later. | |
| 984263bc | 3009 | */ |
| 5b0b9fa5 | 3010 | if (offer == 0) { |
| b235ad6d MD |
3011 | if (isipv6) { |
| 3012 | if (in6_localaddr(&inp->in6p_faddr)) { | |
| 3013 | offer = ND_IFINFO(rt->rt_ifp)->linkmtu - | |
| 3014 | min_protoh; | |
| 3015 | } else { | |
| 3016 | offer = tcp_v6mssdflt; | |
| 3017 | } | |
| 3018 | } else { | |
| 3019 | if (in_localaddr(inp->inp_faddr)) | |
| 3020 | offer = ifp->if_mtu - min_protoh; | |
| 3021 | else | |
| 3022 | offer = tcp_mssdflt; | |
| 3023 | } | |
| 5b0b9fa5 | 3024 | } |
| b235ad6d MD |
3025 | |
| 3026 | /* | |
| 3027 | * Prevent DoS attack with too small MSS. Round up | |
| 3028 | * to at least minmss. | |
| 3029 | * | |
| 3030 | * Sanity check: make sure that maxopd will be large | |
| 3031 | * enough to allow some data on segments even is the | |
| 3032 | * all the option space is used (40bytes). Otherwise | |
| 3033 | * funny things may happen in tcp_output. | |
| 3034 | */ | |
| 3035 | offer = max(offer, tcp_minmss); | |
| 3036 | offer = max(offer, 64); | |
| 3037 | ||
| 27b8aee3 | 3038 | rt->rt_rmx.rmx_mssopt = offer; |
| 984263bc MD |
3039 | |
| 3040 | /* | |
| 3041 | * While we're here, check if there's an initial rtt | |
| 3042 | * or rttvar. Convert from the route-table units | |
| 3043 | * to scaled multiples of the slow timeout timer. | |
| 3044 | */ | |
| 3045 | if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { | |
| 3046 | /* | |
| 3047 | * XXX the lock bit for RTT indicates that the value | |
| 3048 | * is also a minimum value; this is subject to time. | |
| 3049 | */ | |
| 3050 | if (rt->rt_rmx.rmx_locks & RTV_RTT) | |
| 3051 | tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); | |
| 3052 | tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); | |
| 3053 | tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; | |
| 3054 | tcpstat.tcps_usedrtt++; | |
| 3055 | if (rt->rt_rmx.rmx_rttvar) { | |
| 3056 | tp->t_rttvar = rt->rt_rmx.rmx_rttvar / | |
| 3057 | (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); | |
| 3058 | tcpstat.tcps_usedrttvar++; | |
| 3059 | } else { | |
| 3060 | /* default variation is +- 1 rtt */ | |
| 3061 | tp->t_rttvar = | |
| 3062 | tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; | |
| 3063 | } | |
| 3064 | TCPT_RANGESET(tp->t_rxtcur, | |
| 3065 | ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, | |
| 3066 | tp->t_rttmin, TCPTV_REXMTMAX); | |
| 3067 | } | |
| b235ad6d | 3068 | |
| 984263bc MD |
3069 | /* |
| 3070 | * if there's an mtu associated with the route, use it | |
| b235ad6d MD |
3071 | * else, use the link mtu. Take the smaller of mss or offer |
| 3072 | * as our final mss. | |
| 984263bc | 3073 | */ |
| b235ad6d | 3074 | if (rt->rt_rmx.rmx_mtu) { |
| 984263bc | 3075 | mss = rt->rt_rmx.rmx_mtu - min_protoh; |
| b235ad6d MD |
3076 | } else { |
| 3077 | if (isipv6) | |
| 698ac46c | 3078 | mss = ND_IFINFO(rt->rt_ifp)->linkmtu - min_protoh; |
| b235ad6d | 3079 | else |
| 984263bc | 3080 | mss = ifp->if_mtu - min_protoh; |
| 984263bc MD |
3081 | } |
| 3082 | mss = min(mss, offer); | |
| b235ad6d | 3083 | |
| 984263bc MD |
3084 | /* |
| 3085 | * maxopd stores the maximum length of data AND options | |
| 3086 | * in a segment; maxseg is the amount of data in a normal | |
| 3087 | * segment. We need to store this value (maxopd) apart | |
| 3088 | * from maxseg, because now every segment carries options | |
| 3089 | * and thus we normally have somewhat less data in segments. | |
| 3090 | */ | |
| 3091 | tp->t_maxopd = mss; | |
| 3092 | ||
| 61896e3c | 3093 | if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP && |
| 27b8aee3 | 3094 | ((tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) |
| 984263bc | 3095 | mss -= TCPOLEN_TSTAMP_APPA; |
| 984263bc MD |
3096 | |
| 3097 | #if (MCLBYTES & (MCLBYTES - 1)) == 0 | |
| f99dae5e SW |
3098 | if (mss > MCLBYTES) |
| 3099 | mss &= ~(MCLBYTES-1); | |
| 984263bc | 3100 | #else |
| f99dae5e SW |
3101 | if (mss > MCLBYTES) |
| 3102 | mss = mss / MCLBYTES * MCLBYTES; | |
| 984263bc MD |
3103 | #endif |
| 3104 | /* | |
| 3105 | * If there's a pipesize, change the socket buffer | |
| 3106 | * to that size. Make the socket buffers an integral | |
| 3107 | * number of mss units; if the mss is larger than | |
| 3108 | * the socket buffer, decrease the mss. | |
| 3109 | */ | |
| 3110 | #ifdef RTV_SPIPE | |
| 3111 | if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) | |
| 3112 | #endif | |
| 6d49aa6f | 3113 | bufsize = so->so_snd.ssb_hiwat; |
| 984263bc MD |
3114 | if (bufsize < mss) |
| 3115 | mss = bufsize; | |
| 3116 | else { | |
| 3117 | bufsize = roundup(bufsize, mss); | |
| 3118 | if (bufsize > sb_max) | |
| 3119 | bufsize = sb_max; | |
| 6d49aa6f MD |
3120 | if (bufsize > so->so_snd.ssb_hiwat) |
| 3121 | ssb_reserve(&so->so_snd, bufsize, so, NULL); | |
| 984263bc MD |
3122 | } |
| 3123 | tp->t_maxseg = mss; | |
| 3124 | ||
| 3125 | #ifdef RTV_RPIPE | |
| 3126 | if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) | |
| 3127 | #endif | |
| 6d49aa6f | 3128 | bufsize = so->so_rcv.ssb_hiwat; |
| 984263bc MD |
3129 | if (bufsize > mss) { |
| 3130 | bufsize = roundup(bufsize, mss); | |
| 3131 | if (bufsize > sb_max) | |
| 3132 | bufsize = sb_max; | |
| 6cef7136 MD |
3133 | if (bufsize > so->so_rcv.ssb_hiwat) { |
| 3134 | lwkt_gettoken(&so->so_rcv.ssb_token); | |
| 6d49aa6f | 3135 | ssb_reserve(&so->so_rcv, bufsize, so, NULL); |
| 6cef7136 MD |
3136 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 3137 | } | |
| 984263bc MD |
3138 | } |
| 3139 | ||
| 3140 | /* | |
| 697020f5 SZ |
3141 | * Set the slow-start flight size |
| 3142 | * | |
| 3143 | * NOTE: t_maxseg must have been configured! | |
| 984263bc | 3144 | */ |
| 697020f5 | 3145 | tp->snd_cwnd = tcp_initial_window(tp); |
| 984263bc MD |
3146 | |
| 3147 | if (rt->rt_rmx.rmx_ssthresh) { | |
| 3148 | /* | |
| 3149 | * There's some sort of gateway or interface | |
| 3150 | * buffer limit on the path. Use this to set | |
| 3151 | * the slow start threshhold, but set the | |
| 3152 | * threshold to no less than 2*mss. | |
| 3153 | */ | |
| 3154 | tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); | |
| 3155 | tcpstat.tcps_usedssthresh++; | |
| 3156 | } | |
| 3157 | } | |
| 3158 | ||
| 3159 | /* | |
| 3160 | * Determine the MSS option to send on an outgoing SYN. | |
| 3161 | */ | |
| 3162 | int | |
| 95b22adf | 3163 | tcp_mssopt(struct tcpcb *tp) |
| 984263bc MD |
3164 | { |
| 3165 | struct rtentry *rt; | |
| 3166 | #ifdef INET6 | |
| d24ce1dc JH |
3167 | boolean_t isipv6 = |
| 3168 | ((tp->t_inpcb->inp_vflag & INP_IPV6) ? TRUE : FALSE); | |
| 984263bc MD |
3169 | int min_protoh = isipv6 ? |
| 3170 | sizeof(struct ip6_hdr) + sizeof(struct tcphdr) : | |
| 3171 | sizeof(struct tcpiphdr); | |
| 3172 | #else | |
| d24ce1dc | 3173 | const boolean_t isipv6 = FALSE; |
| 984263bc MD |
3174 | const size_t min_protoh = sizeof(struct tcpiphdr); |
| 3175 | #endif | |
| 3176 | ||
| 3177 | if (isipv6) | |
| 3178 | rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc); | |
| 3179 | else | |
| 3180 | rt = tcp_rtlookup(&tp->t_inpcb->inp_inc); | |
| 3181 | if (rt == NULL) | |
| 3182 | return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt); | |
| 3183 | ||
| 3184 | return (rt->rt_ifp->if_mtu - min_protoh); | |
| 3185 | } | |
| 3186 | ||
| 984263bc MD |
3187 | /* |
| 3188 | * When a partial ack arrives, force the retransmission of the | |
| 91489f6b JH |
3189 | * next unacknowledged segment. Do not exit Fast Recovery. |
| 3190 | * | |
| 3191 | * Implement the Slow-but-Steady variant of NewReno by restarting the | |
| 3192 | * the retransmission timer. Turn it off here so it can be restarted | |
| 3193 | * later in tcp_output(). | |
| 984263bc MD |
3194 | */ |
| 3195 | static void | |
| 91489f6b | 3196 | tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th, int acked) |
| 984263bc | 3197 | { |
| 91489f6b JH |
3198 | tcp_seq old_snd_nxt = tp->snd_nxt; |
| 3199 | u_long ocwnd = tp->snd_cwnd; | |
| 984263bc | 3200 | |
| a48c5dd5 | 3201 | tcp_callout_stop(tp, tp->tt_rexmt); |
| 984263bc MD |
3202 | tp->t_rtttime = 0; |
| 3203 | tp->snd_nxt = th->th_ack; | |
| 91489f6b JH |
3204 | /* Set snd_cwnd to one segment beyond acknowledged offset. */ |
| 3205 | tp->snd_cwnd = tp->t_maxseg; | |
| 984263bc | 3206 | tp->t_flags |= TF_ACKNOW; |
| f23061d4 | 3207 | tcp_output(tp); |
| 91489f6b JH |
3208 | if (SEQ_GT(old_snd_nxt, tp->snd_nxt)) |
| 3209 | tp->snd_nxt = old_snd_nxt; | |
| 3210 | /* partial window deflation */ | |
| 68947d12 ND |
3211 | if (ocwnd > acked) |
| 3212 | tp->snd_cwnd = ocwnd - acked + tp->t_maxseg; | |
| 3213 | else | |
| 3214 | tp->snd_cwnd = tp->t_maxseg; | |
| 91489f6b JH |
3215 | } |
| 3216 | ||
| 3217 | /* | |
| 3218 | * In contrast to the Slow-but-Steady NewReno variant, | |
| 3219 | * we do not reset the retransmission timer for SACK retransmissions, | |
| 3220 | * except when retransmitting snd_una. | |
| 3221 | */ | |
| 3222 | static void | |
| ccb518ea | 3223 | tcp_sack_rexmt(struct tcpcb *tp, boolean_t force) |
| 91489f6b | 3224 | { |
| 91489f6b JH |
3225 | tcp_seq old_snd_nxt = tp->snd_nxt; |
| 3226 | u_long ocwnd = tp->snd_cwnd; | |
| 1bdfd728 | 3227 | uint32_t pipe; |
| 91489f6b | 3228 | int nseg = 0; /* consecutive new segments */ |
| a098966f | 3229 | int nseg_rexmt = 0; /* retransmitted segments */ |
| ccb518ea | 3230 | int maxrexmt = 0; |
| 91489f6b JH |
3231 | #define MAXBURST 4 /* limit burst of new packets on partial ack */ |
| 3232 | ||
| ccb518ea SZ |
3233 | if (force) { |
| 3234 | uint32_t unsacked = tcp_sack_first_unsacked_len(tp); | |
| 3235 | ||
| 3236 | /* | |
| 3237 | * Try to fill the first hole in the receiver's | |
| 3238 | * reassemble queue. | |
| 3239 | */ | |
| 3240 | maxrexmt = howmany(unsacked, tp->t_maxseg); | |
| 3241 | if (maxrexmt > tcp_force_sackrxt) | |
| 3242 | maxrexmt = tcp_force_sackrxt; | |
| 3243 | } | |
| 3244 | ||
| 91489f6b JH |
3245 | tp->t_rtttime = 0; |
| 3246 | pipe = tcp_sack_compute_pipe(tp); | |
| ccb518ea SZ |
3247 | while (((tcp_seq_diff_t)(ocwnd - pipe) >= (tcp_seq_diff_t)tp->t_maxseg |
| 3248 | || (force && nseg_rexmt < maxrexmt && nseg == 0)) && | |
| 1bdfd728 SZ |
3249 | (!tcp_do_smartsack || nseg < MAXBURST)) { |
| 3250 | tcp_seq old_snd_max, old_rexmt_high, nextrexmt; | |
| 3251 | uint32_t sent, seglen; | |
| 3252 | boolean_t rescue; | |
| 91489f6b JH |
3253 | int error; |
| 3254 | ||
| 1bdfd728 | 3255 | old_rexmt_high = tp->rexmt_high; |
| 64debfd6 | 3256 | if (!tcp_sack_nextseg(tp, &nextrexmt, &seglen, &rescue)) { |
| 1bdfd728 SZ |
3257 | tp->rexmt_high = old_rexmt_high; |
| 3258 | break; | |
| 3259 | } | |
| 3260 | ||
| a098966f SZ |
3261 | /* |
| 3262 | * If the next tranmission is a rescue retranmission, | |
| 3263 | * we check whether we have already sent some data | |
| 3264 | * (either new segments or retransmitted segments) | |
| 3265 | * into the the network or not. Since the idea of rescue | |
| 3266 | * retransmission is to sustain ACK clock, as long as | |
| 3267 | * some segments are in the network, ACK clock will be | |
| 3268 | * kept ticking. | |
| 3269 | */ | |
| 3270 | if (rescue && (nseg_rexmt > 0 || nseg > 0)) { | |
| 3271 | tp->rexmt_high = old_rexmt_high; | |
| 3272 | break; | |
| 3273 | } | |
| 3274 | ||
| 98cb2337 MD |
3275 | if (nextrexmt == tp->snd_max) |
| 3276 | ++nseg; | |
| a098966f SZ |
3277 | else |
| 3278 | ++nseg_rexmt; | |
| 91489f6b JH |
3279 | tp->snd_nxt = nextrexmt; |
| 3280 | tp->snd_cwnd = nextrexmt - tp->snd_una + seglen; | |
| 3281 | old_snd_max = tp->snd_max; | |
| 3282 | if (nextrexmt == tp->snd_una) | |
| a48c5dd5 | 3283 | tcp_callout_stop(tp, tp->tt_rexmt); |
| c1fabe85 | 3284 | tp->t_flags |= TF_XMITNOW; |
| 91489f6b | 3285 | error = tcp_output(tp); |
| 1bdfd728 SZ |
3286 | if (error != 0) { |
| 3287 | tp->rexmt_high = old_rexmt_high; | |
| 91489f6b | 3288 | break; |
| 1bdfd728 | 3289 | } |
| 91489f6b | 3290 | sent = tp->snd_nxt - nextrexmt; |
| 1bdfd728 SZ |
3291 | if (sent <= 0) { |
| 3292 | tp->rexmt_high = old_rexmt_high; | |
| 91489f6b | 3293 | break; |
| 1bdfd728 SZ |
3294 | } |
| 3295 | pipe += sent; | |
| 91489f6b JH |
3296 | tcpstat.tcps_sndsackpack++; |
| 3297 | tcpstat.tcps_sndsackbyte += sent; | |
| 1bdfd728 SZ |
3298 | |
| 3299 | if (rescue) { | |
| a098966f | 3300 | tcpstat.tcps_sackrescue++; |
| 1bdfd728 | 3301 | tp->rexmt_rescue = tp->snd_nxt; |
| c7e6499a | 3302 | tp->sack_flags |= TSACK_F_SACKRESCUED; |
| 1bdfd728 SZ |
3303 | break; |
| 3304 | } | |
| 91489f6b | 3305 | if (SEQ_LT(nextrexmt, old_snd_max) && |
| a098966f | 3306 | SEQ_LT(tp->rexmt_high, tp->snd_nxt)) { |
| 91489f6b | 3307 | tp->rexmt_high = seq_min(tp->snd_nxt, old_snd_max); |
| 21f337ca | 3308 | if (tcp_aggressive_rescuesack && |
| c7e6499a | 3309 | (tp->sack_flags & TSACK_F_SACKRESCUED) && |
| a098966f SZ |
3310 | SEQ_LT(tp->rexmt_rescue, tp->rexmt_high)) { |
| 3311 | /* Drag RescueRxt along with HighRxt */ | |
| 3312 | tp->rexmt_rescue = tp->rexmt_high; | |
| 3313 | } | |
| 3314 | } | |
| 91489f6b JH |
3315 | } |
| 3316 | if (SEQ_GT(old_snd_nxt, tp->snd_nxt)) | |
| 3317 | tp->snd_nxt = old_snd_nxt; | |
| 984263bc | 3318 | tp->snd_cwnd = ocwnd; |
| 984263bc | 3319 | } |
| 0ecd93f9 | 3320 | |
| e2289e66 SZ |
3321 | /* |
| 3322 | * Return TRUE, if some new segments are sent | |
| 3323 | */ | |
| ffe35e17 SZ |
3324 | static boolean_t |
| 3325 | tcp_sack_limitedxmit(struct tcpcb *tp) | |
| 3326 | { | |
| 3327 | tcp_seq oldsndnxt = tp->snd_nxt; | |
| 3328 | tcp_seq oldsndmax = tp->snd_max; | |
| 3329 | u_long ocwnd = tp->snd_cwnd; | |
| db421eef | 3330 | uint32_t pipe, sent; |
| ffe35e17 | 3331 | boolean_t ret = FALSE; |
| db421eef SZ |
3332 | tcp_seq_diff_t cwnd_left; |
| 3333 | tcp_seq next; | |
| ffe35e17 SZ |
3334 | |
| 3335 | tp->rexmt_high = tp->snd_una - 1; | |
| 3336 | pipe = tcp_sack_compute_pipe(tp); | |
| db421eef SZ |
3337 | cwnd_left = (tcp_seq_diff_t)(ocwnd - pipe); |
| 3338 | if (cwnd_left < (tcp_seq_diff_t)tp->t_maxseg) | |
| 3339 | return FALSE; | |
| ffe35e17 | 3340 | |
| db421eef SZ |
3341 | next = tp->snd_nxt = tp->snd_max; |
| 3342 | tp->snd_cwnd = tp->snd_nxt - tp->snd_una + | |
| 3343 | rounddown(cwnd_left, tp->t_maxseg); | |
| ffe35e17 | 3344 | |
| c1fabe85 | 3345 | tp->t_flags |= TF_XMITNOW; |
| db421eef | 3346 | tcp_output(tp); |
| ffe35e17 | 3347 | |
| db421eef SZ |
3348 | sent = tp->snd_nxt - next; |
| 3349 | if (sent > 0) { | |
| 3350 | tcpstat.tcps_sndlimited += howmany(sent, tp->t_maxseg); | |
| ffe35e17 SZ |
3351 | ret = TRUE; |
| 3352 | } | |
| 3353 | ||
| 3354 | if (SEQ_LT(oldsndnxt, oldsndmax)) { | |
| 3355 | KASSERT(SEQ_GEQ(oldsndnxt, tp->snd_una), | |
| 3356 | ("snd_una moved in other threads")); | |
| 3357 | tp->snd_nxt = oldsndnxt; | |
| 3358 | } | |
| 3359 | tp->snd_cwnd = ocwnd; | |
| 3360 | ||
| e2289e66 SZ |
3361 | if (ret && TCP_DO_NCR(tp)) |
| 3362 | tcp_ncr_update_rxtthresh(tp); | |
| 3363 | ||
| ffe35e17 SZ |
3364 | return ret; |
| 3365 | } | |
| 3366 | ||
| 0ecd93f9 MD |
3367 | /* |
| 3368 | * Reset idle time and keep-alive timer, typically called when a valid | |
| 3369 | * tcp packet is received but may also be called when FASTKEEP is set | |
| 3370 | * to prevent the previous long-timeout from calculating to a drop. | |
| 3371 | * | |
| 3372 | * Only update t_rcvtime for non-SYN packets. | |
| 3373 | * | |
| 3374 | * Handle the case where one side thinks the connection is established | |
| 3375 | * but the other side has, say, rebooted without cleaning out the | |
| 3376 | * connection. The SYNs could be construed as an attack and wind | |
| 3377 | * up ignored, but in case it isn't an attack we can validate the | |
| 3378 | * connection by forcing a keepalive. | |
| 3379 | */ | |
| 3380 | void | |
| 3381 | tcp_timer_keep_activity(struct tcpcb *tp, int thflags) | |
| 3382 | { | |
| 3383 | if (TCPS_HAVEESTABLISHED(tp->t_state)) { | |
| 3384 | if ((thflags & (TH_SYN | TH_ACK)) == TH_SYN) { | |
| 3385 | tp->t_flags |= TF_KEEPALIVE; | |
| 3386 | tcp_callout_reset(tp, tp->tt_keep, hz / 2, | |
| 3387 | tcp_timer_keep); | |
| 3388 | } else { | |
| 3389 | tp->t_rcvtime = ticks; | |
| 3390 | tp->t_flags &= ~TF_KEEPALIVE; | |
| 3391 | tcp_callout_reset(tp, tp->tt_keep, | |
| fb8d5c6d | 3392 | tp->t_keepidle, |
| 0ecd93f9 MD |
3393 | tcp_timer_keep); |
| 3394 | } | |
| 3395 | } | |
| 3396 | } | |
| 01d3427a SZ |
3397 | |
| 3398 | static int | |
| 3399 | tcp_rmx_msl(const struct tcpcb *tp) | |
| 3400 | { | |
| 3401 | struct rtentry *rt; | |
| 3402 | struct inpcb *inp = tp->t_inpcb; | |
| 9855a4ef | 3403 | int msl; |
| 01d3427a SZ |
3404 | #ifdef INET6 |
| 3405 | boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) ? TRUE : FALSE); | |
| 3406 | #else | |
| 3407 | const boolean_t isipv6 = FALSE; | |
| 3408 | #endif | |
| 3409 | ||
| 3410 | if (isipv6) | |
| 3411 | rt = tcp_rtlookup6(&inp->inp_inc); | |
| 3412 | else | |
| 3413 | rt = tcp_rtlookup(&inp->inp_inc); | |
| 3414 | if (rt == NULL || rt->rt_rmx.rmx_msl == 0) | |
| 3415 | return tcp_msl; | |
| 3416 | ||
| 9855a4ef SZ |
3417 | msl = (rt->rt_rmx.rmx_msl * hz) / 1000; |
| 3418 | if (msl == 0) | |
| 3419 | msl = 1; | |
| 3420 | ||
| 3421 | return msl; | |
| 01d3427a | 3422 | } |
| 8651f7f8 SZ |
3423 | |
| 3424 | static void | |
| 3425 | tcp_established(struct tcpcb *tp) | |
| 3426 | { | |
| 3427 | tp->t_state = TCPS_ESTABLISHED; | |
| fb8d5c6d | 3428 | tcp_callout_reset(tp, tp->tt_keep, tp->t_keepidle, tcp_timer_keep); |
| be34e534 | 3429 | |
| d5082e3d | 3430 | if (tp->t_rxtsyn > 0) { |
| 48a4676d SZ |
3431 | /* |
| 3432 | * RFC6298: | |
| 3433 | * "If the timer expires awaiting the ACK of a SYN segment | |
| 3434 | * and the TCP implementation is using an RTO less than 3 | |
| 3435 | * seconds, the RTO MUST be re-initialized to 3 seconds | |
| 3436 | * when data transmission begins" | |
| 3437 | */ | |
| 3438 | if (tp->t_rxtcur < TCPTV_RTOBASE3) | |
| 3439 | tp->t_rxtcur = TCPTV_RTOBASE3; | |
| be34e534 | 3440 | } |
| 8651f7f8 | 3441 | } |
| 27f4bf33 SZ |
3442 | |
| 3443 | /* | |
| 3444 | * Returns TRUE, if the ACK should be dropped | |
| 3445 | */ | |
| 3446 | static boolean_t | |
| 0a832381 | 3447 | tcp_recv_dupack(struct tcpcb *tp, tcp_seq th_ack, u_int to_flags) |
| 27f4bf33 | 3448 | { |
| ba0d6f99 SZ |
3449 | boolean_t fast_sack_rexmt = TRUE; |
| 3450 | ||
| 27f4bf33 SZ |
3451 | tcpstat.tcps_rcvdupack++; |
| 3452 | ||
| 3453 | /* | |
| 3454 | * We have outstanding data (other than a window probe), | |
| 3455 | * this is a completely duplicate ack (ie, window info | |
| 3456 | * didn't change), the ack is the biggest we've seen and | |
| 3457 | * we've seen exactly our rexmt threshhold of them, so | |
| 3458 | * assume a packet has been dropped and retransmit it. | |
| 3459 | * Kludge snd_nxt & the congestion window so we send only | |
| 3460 | * this one packet. | |
| 3461 | */ | |
| 3462 | if (IN_FASTRECOVERY(tp)) { | |
| 3463 | if (TCP_DO_SACK(tp)) { | |