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