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