1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 2007-2008,2010 5 * Swinburne University of Technology, Melbourne, Australia. 6 * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 7 * Copyright (c) 2010 The FreeBSD Foundation 8 * Copyright (c) 2010-2011 Juniper Networks, Inc. 9 * All rights reserved. 10 * 11 * Portions of this software were developed at the Centre for Advanced Internet 12 * Architectures, Swinburne University of Technology, by Lawrence Stewart, 13 * James Healy and David Hayes, made possible in part by a grant from the Cisco 14 * University Research Program Fund at Community Foundation Silicon Valley. 15 * 16 * Portions of this software were developed at the Centre for Advanced 17 * Internet Architectures, Swinburne University of Technology, Melbourne, 18 * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 19 * 20 * Portions of this software were developed by Robert N. M. Watson under 21 * contract to Juniper Networks, Inc. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the above copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 4. Neither the name of the University nor the names of its contributors 32 * may be used to endorse or promote products derived from this software 33 * without specific prior written permission. 34 * 35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 45 * SUCH DAMAGE. 46 * 47 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 48 */ 49 50 #include <sys/cdefs.h> 51 __FBSDID("$FreeBSD$"); 52 53 #include "opt_ipfw.h" /* for ipfw_fwd */ 54 #include "opt_inet.h" 55 #include "opt_inet6.h" 56 #include "opt_ipsec.h" 57 #include "opt_tcpdebug.h" 58 59 #include <sys/param.h> 60 #include <sys/kernel.h> 61 #include <sys/hhook.h> 62 #include <sys/malloc.h> 63 #include <sys/mbuf.h> 64 #include <sys/proc.h> /* for proc0 declaration */ 65 #include <sys/protosw.h> 66 #include <sys/signalvar.h> 67 #include <sys/socket.h> 68 #include <sys/socketvar.h> 69 #include <sys/sysctl.h> 70 #include <sys/syslog.h> 71 #include <sys/systm.h> 72 73 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 74 75 #include <vm/uma.h> 76 77 #include <net/if.h> 78 #include <net/route.h> 79 #include <net/vnet.h> 80 81 #define TCPSTATES /* for logging */ 82 83 #include <netinet/cc.h> 84 #include <netinet/in.h> 85 #include <netinet/in_pcb.h> 86 #include <netinet/in_systm.h> 87 #include <netinet/in_var.h> 88 #include <netinet/ip.h> 89 #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 90 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 91 #include <netinet/ip_var.h> 92 #include <netinet/ip_options.h> 93 #include <netinet/ip6.h> 94 #include <netinet/icmp6.h> 95 #include <netinet6/in6_pcb.h> 96 #include <netinet6/ip6_var.h> 97 #include <netinet6/nd6.h> 98 #include <netinet/tcp_fsm.h> 99 #include <netinet/tcp_seq.h> 100 #include <netinet/tcp_timer.h> 101 #include <netinet/tcp_var.h> 102 #include <netinet6/tcp6_var.h> 103 #include <netinet/tcpip.h> 104 #include <netinet/tcp_syncache.h> 105 #ifdef TCPDEBUG 106 #include <netinet/tcp_debug.h> 107 #endif /* TCPDEBUG */ 108 #ifdef TCP_OFFLOAD 109 #include <netinet/tcp_offload.h> 110 #endif 111 112 #ifdef IPSEC 113 #include <netipsec/ipsec.h> 114 #include <netipsec/ipsec6.h> 115 #endif /*IPSEC*/ 116 117 #include <machine/in_cksum.h> 118 119 #include <security/mac/mac_framework.h> 120 121 const int tcprexmtthresh = 3; 122 123 VNET_DEFINE(struct tcpstat, tcpstat); 124 SYSCTL_VNET_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 125 &VNET_NAME(tcpstat), tcpstat, 126 "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 127 128 int tcp_log_in_vain = 0; 129 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 130 &tcp_log_in_vain, 0, 131 "Log all incoming TCP segments to closed ports"); 132 133 VNET_DEFINE(int, blackhole) = 0; 134 #define V_blackhole VNET(blackhole) 135 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 136 &VNET_NAME(blackhole), 0, 137 "Do not send RST on segments to closed ports"); 138 139 VNET_DEFINE(int, tcp_delack_enabled) = 1; 140 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 141 &VNET_NAME(tcp_delack_enabled), 0, 142 "Delay ACK to try and piggyback it onto a data packet"); 143 144 VNET_DEFINE(int, drop_synfin) = 0; 145 #define V_drop_synfin VNET(drop_synfin) 146 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 147 &VNET_NAME(drop_synfin), 0, 148 "Drop TCP packets with SYN+FIN set"); 149 150 VNET_DEFINE(int, tcp_do_rfc3042) = 1; 151 #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 152 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 153 &VNET_NAME(tcp_do_rfc3042), 0, 154 "Enable RFC 3042 (Limited Transmit)"); 155 156 VNET_DEFINE(int, tcp_do_rfc3390) = 1; 157 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 158 &VNET_NAME(tcp_do_rfc3390), 0, 159 "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 160 161 VNET_DEFINE(int, tcp_do_rfc3465) = 1; 162 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW, 163 &VNET_NAME(tcp_do_rfc3465), 0, 164 "Enable RFC 3465 (Appropriate Byte Counting)"); 165 166 VNET_DEFINE(int, tcp_abc_l_var) = 2; 167 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW, 168 &VNET_NAME(tcp_abc_l_var), 2, 169 "Cap the max cwnd increment during slow-start to this number of segments"); 170 171 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 172 173 VNET_DEFINE(int, tcp_do_ecn) = 0; 174 SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW, 175 &VNET_NAME(tcp_do_ecn), 0, 176 "TCP ECN support"); 177 178 VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 179 SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW, 180 &VNET_NAME(tcp_ecn_maxretries), 0, 181 "Max retries before giving up on ECN"); 182 183 VNET_DEFINE(int, tcp_insecure_rst) = 0; 184 #define V_tcp_insecure_rst VNET(tcp_insecure_rst) 185 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW, 186 &VNET_NAME(tcp_insecure_rst), 0, 187 "Follow the old (insecure) criteria for accepting RST packets"); 188 189 VNET_DEFINE(int, tcp_recvspace) = 1024*64; 190 #define V_tcp_recvspace VNET(tcp_recvspace) 191 SYSCTL_VNET_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 192 &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 193 194 VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 195 #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 196 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 197 &VNET_NAME(tcp_do_autorcvbuf), 0, 198 "Enable automatic receive buffer sizing"); 199 200 VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 201 #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 202 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 203 &VNET_NAME(tcp_autorcvbuf_inc), 0, 204 "Incrementor step size of automatic receive buffer"); 205 206 VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 207 #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 208 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 209 &VNET_NAME(tcp_autorcvbuf_max), 0, 210 "Max size of automatic receive buffer"); 211 212 VNET_DEFINE(struct inpcbhead, tcb); 213 #define tcb6 tcb /* for KAME src sync over BSD*'s */ 214 VNET_DEFINE(struct inpcbinfo, tcbinfo); 215 216 static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 217 static void tcp_do_segment(struct mbuf *, struct tcphdr *, 218 struct socket *, struct tcpcb *, int, int, uint8_t, 219 int); 220 static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 221 struct tcpcb *, int, int); 222 static void tcp_pulloutofband(struct socket *, 223 struct tcphdr *, struct mbuf *, int); 224 static void tcp_xmit_timer(struct tcpcb *, int); 225 static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 226 static void inline tcp_fields_to_host(struct tcphdr *); 227 #ifdef TCP_SIGNATURE 228 static void inline tcp_fields_to_net(struct tcphdr *); 229 static int inline tcp_signature_verify_input(struct mbuf *, int, int, 230 int, struct tcpopt *, struct tcphdr *, u_int); 231 #endif 232 static void inline cc_ack_received(struct tcpcb *tp, struct tcphdr *th, 233 uint16_t type); 234 static void inline cc_conn_init(struct tcpcb *tp); 235 static void inline cc_post_recovery(struct tcpcb *tp, struct tcphdr *th); 236 static void inline hhook_run_tcp_est_in(struct tcpcb *tp, 237 struct tcphdr *th, struct tcpopt *to); 238 239 /* 240 * Kernel module interface for updating tcpstat. The argument is an index 241 * into tcpstat treated as an array of u_long. While this encodes the 242 * general layout of tcpstat into the caller, it doesn't encode its location, 243 * so that future changes to add, for example, per-CPU stats support won't 244 * cause binary compatibility problems for kernel modules. 245 */ 246 void 247 kmod_tcpstat_inc(int statnum) 248 { 249 250 (*((u_long *)&V_tcpstat + statnum))++; 251 } 252 253 /* 254 * Wrapper for the TCP established input helper hook. 255 */ 256 static void inline 257 hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 258 { 259 struct tcp_hhook_data hhook_data; 260 261 if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 262 hhook_data.tp = tp; 263 hhook_data.th = th; 264 hhook_data.to = to; 265 266 hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 267 tp->osd); 268 } 269 } 270 271 /* 272 * CC wrapper hook functions 273 */ 274 static void inline 275 cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type) 276 { 277 INP_WLOCK_ASSERT(tp->t_inpcb); 278 279 tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 280 if (tp->snd_cwnd == min(tp->snd_cwnd, tp->snd_wnd)) 281 tp->ccv->flags |= CCF_CWND_LIMITED; 282 else 283 tp->ccv->flags &= ~CCF_CWND_LIMITED; 284 285 if (type == CC_ACK) { 286 if (tp->snd_cwnd > tp->snd_ssthresh) { 287 tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 288 V_tcp_abc_l_var * tp->t_maxseg); 289 if (tp->t_bytes_acked >= tp->snd_cwnd) { 290 tp->t_bytes_acked -= tp->snd_cwnd; 291 tp->ccv->flags |= CCF_ABC_SENTAWND; 292 } 293 } else { 294 tp->ccv->flags &= ~CCF_ABC_SENTAWND; 295 tp->t_bytes_acked = 0; 296 } 297 } 298 299 if (CC_ALGO(tp)->ack_received != NULL) { 300 /* XXXLAS: Find a way to live without this */ 301 tp->ccv->curack = th->th_ack; 302 CC_ALGO(tp)->ack_received(tp->ccv, type); 303 } 304 } 305 306 static void inline 307 cc_conn_init(struct tcpcb *tp) 308 { 309 struct hc_metrics_lite metrics; 310 struct inpcb *inp = tp->t_inpcb; 311 int rtt; 312 313 INP_WLOCK_ASSERT(tp->t_inpcb); 314 315 tcp_hc_get(&inp->inp_inc, &metrics); 316 317 if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 318 tp->t_srtt = rtt; 319 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 320 TCPSTAT_INC(tcps_usedrtt); 321 if (metrics.rmx_rttvar) { 322 tp->t_rttvar = metrics.rmx_rttvar; 323 TCPSTAT_INC(tcps_usedrttvar); 324 } else { 325 /* default variation is +- 1 rtt */ 326 tp->t_rttvar = 327 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 328 } 329 TCPT_RANGESET(tp->t_rxtcur, 330 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 331 tp->t_rttmin, TCPTV_REXMTMAX); 332 } 333 if (metrics.rmx_ssthresh) { 334 /* 335 * There's some sort of gateway or interface 336 * buffer limit on the path. Use this to set 337 * the slow start threshhold, but set the 338 * threshold to no less than 2*mss. 339 */ 340 tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh); 341 TCPSTAT_INC(tcps_usedssthresh); 342 } 343 344 /* 345 * Set the initial slow-start flight size. 346 * 347 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 348 * XXX: We currently check only in syncache_socket for that. 349 */ 350 if (V_tcp_do_rfc3390) 351 tp->snd_cwnd = min(4 * tp->t_maxseg, 352 max(2 * tp->t_maxseg, 4380)); 353 else 354 tp->snd_cwnd = tp->t_maxseg; 355 356 if (CC_ALGO(tp)->conn_init != NULL) 357 CC_ALGO(tp)->conn_init(tp->ccv); 358 } 359 360 void inline 361 cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 362 { 363 INP_WLOCK_ASSERT(tp->t_inpcb); 364 365 switch(type) { 366 case CC_NDUPACK: 367 if (!IN_FASTRECOVERY(tp->t_flags)) { 368 tp->snd_recover = tp->snd_max; 369 if (tp->t_flags & TF_ECN_PERMIT) 370 tp->t_flags |= TF_ECN_SND_CWR; 371 } 372 break; 373 case CC_ECN: 374 if (!IN_CONGRECOVERY(tp->t_flags)) { 375 TCPSTAT_INC(tcps_ecn_rcwnd); 376 tp->snd_recover = tp->snd_max; 377 if (tp->t_flags & TF_ECN_PERMIT) 378 tp->t_flags |= TF_ECN_SND_CWR; 379 } 380 break; 381 case CC_RTO: 382 tp->t_dupacks = 0; 383 tp->t_bytes_acked = 0; 384 EXIT_RECOVERY(tp->t_flags); 385 tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 386 tp->t_maxseg) * tp->t_maxseg; 387 tp->snd_cwnd = tp->t_maxseg; 388 break; 389 case CC_RTO_ERR: 390 TCPSTAT_INC(tcps_sndrexmitbad); 391 /* RTO was unnecessary, so reset everything. */ 392 tp->snd_cwnd = tp->snd_cwnd_prev; 393 tp->snd_ssthresh = tp->snd_ssthresh_prev; 394 tp->snd_recover = tp->snd_recover_prev; 395 if (tp->t_flags & TF_WASFRECOVERY) 396 ENTER_FASTRECOVERY(tp->t_flags); 397 if (tp->t_flags & TF_WASCRECOVERY) 398 ENTER_CONGRECOVERY(tp->t_flags); 399 tp->snd_nxt = tp->snd_max; 400 tp->t_flags &= ~TF_PREVVALID; 401 tp->t_badrxtwin = 0; 402 break; 403 } 404 405 if (CC_ALGO(tp)->cong_signal != NULL) { 406 if (th != NULL) 407 tp->ccv->curack = th->th_ack; 408 CC_ALGO(tp)->cong_signal(tp->ccv, type); 409 } 410 } 411 412 static void inline 413 cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 414 { 415 INP_WLOCK_ASSERT(tp->t_inpcb); 416 417 /* XXXLAS: KASSERT that we're in recovery? */ 418 419 if (CC_ALGO(tp)->post_recovery != NULL) { 420 tp->ccv->curack = th->th_ack; 421 CC_ALGO(tp)->post_recovery(tp->ccv); 422 } 423 /* XXXLAS: EXIT_RECOVERY ? */ 424 tp->t_bytes_acked = 0; 425 } 426 427 static inline void 428 tcp_fields_to_host(struct tcphdr *th) 429 { 430 431 th->th_seq = ntohl(th->th_seq); 432 th->th_ack = ntohl(th->th_ack); 433 th->th_win = ntohs(th->th_win); 434 th->th_urp = ntohs(th->th_urp); 435 } 436 437 #ifdef TCP_SIGNATURE 438 static inline void 439 tcp_fields_to_net(struct tcphdr *th) 440 { 441 442 th->th_seq = htonl(th->th_seq); 443 th->th_ack = htonl(th->th_ack); 444 th->th_win = htons(th->th_win); 445 th->th_urp = htons(th->th_urp); 446 } 447 448 static inline int 449 tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen, 450 struct tcpopt *to, struct tcphdr *th, u_int tcpbflag) 451 { 452 int ret; 453 454 tcp_fields_to_net(th); 455 ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag); 456 tcp_fields_to_host(th); 457 return (ret); 458 } 459 #endif 460 461 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 462 #ifdef INET6 463 #define ND6_HINT(tp) \ 464 do { \ 465 if ((tp) && (tp)->t_inpcb && \ 466 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 467 nd6_nud_hint(NULL, NULL, 0); \ 468 } while (0) 469 #else 470 #define ND6_HINT(tp) 471 #endif 472 473 /* 474 * Indicate whether this ack should be delayed. We can delay the ack if 475 * - there is no delayed ack timer in progress and 476 * - our last ack wasn't a 0-sized window. We never want to delay 477 * the ack that opens up a 0-sized window and 478 * - delayed acks are enabled or 479 * - this is a half-synchronized T/TCP connection. 480 */ 481 #define DELAY_ACK(tp) \ 482 ((!tcp_timer_active(tp, TT_DELACK) && \ 483 (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 484 (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 485 486 /* 487 * TCP input handling is split into multiple parts: 488 * tcp6_input is a thin wrapper around tcp_input for the extended 489 * ip6_protox[] call format in ip6_input 490 * tcp_input handles primary segment validation, inpcb lookup and 491 * SYN processing on listen sockets 492 * tcp_do_segment processes the ACK and text of the segment for 493 * establishing, established and closing connections 494 */ 495 #ifdef INET6 496 int 497 tcp6_input(struct mbuf **mp, int *offp, int proto) 498 { 499 struct mbuf *m = *mp; 500 struct in6_ifaddr *ia6; 501 502 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 503 504 /* 505 * draft-itojun-ipv6-tcp-to-anycast 506 * better place to put this in? 507 */ 508 ia6 = ip6_getdstifaddr(m); 509 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 510 struct ip6_hdr *ip6; 511 512 ifa_free(&ia6->ia_ifa); 513 ip6 = mtod(m, struct ip6_hdr *); 514 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 515 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 516 return IPPROTO_DONE; 517 } 518 if (ia6) 519 ifa_free(&ia6->ia_ifa); 520 521 tcp_input(m, *offp); 522 return IPPROTO_DONE; 523 } 524 #endif /* INET6 */ 525 526 void 527 tcp_input(struct mbuf *m, int off0) 528 { 529 struct tcphdr *th = NULL; 530 struct ip *ip = NULL; 531 #ifdef INET 532 struct ipovly *ipov; 533 #endif 534 struct inpcb *inp = NULL; 535 struct tcpcb *tp = NULL; 536 struct socket *so = NULL; 537 u_char *optp = NULL; 538 int optlen = 0; 539 #ifdef INET 540 int len; 541 #endif 542 int tlen = 0, off; 543 int drop_hdrlen; 544 int thflags; 545 int rstreason = 0; /* For badport_bandlim accounting purposes */ 546 #ifdef TCP_SIGNATURE 547 uint8_t sig_checked = 0; 548 #endif 549 uint8_t iptos = 0; 550 #ifdef IPFIREWALL_FORWARD 551 struct m_tag *fwd_tag; 552 #endif 553 #ifdef INET6 554 struct ip6_hdr *ip6 = NULL; 555 int isipv6; 556 #else 557 const void *ip6 = NULL; 558 #endif /* INET6 */ 559 struct tcpopt to; /* options in this segment */ 560 char *s = NULL; /* address and port logging */ 561 int ti_locked; 562 #define TI_UNLOCKED 1 563 #define TI_WLOCKED 2 564 565 #ifdef TCPDEBUG 566 /* 567 * The size of tcp_saveipgen must be the size of the max ip header, 568 * now IPv6. 569 */ 570 u_char tcp_saveipgen[IP6_HDR_LEN]; 571 struct tcphdr tcp_savetcp; 572 short ostate = 0; 573 #endif 574 575 #ifdef INET6 576 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 577 #endif 578 579 to.to_flags = 0; 580 TCPSTAT_INC(tcps_rcvtotal); 581 582 #ifdef INET6 583 if (isipv6) { 584 /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 585 586 if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 587 m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 588 if (m == NULL) { 589 TCPSTAT_INC(tcps_rcvshort); 590 return; 591 } 592 } 593 594 ip6 = mtod(m, struct ip6_hdr *); 595 th = (struct tcphdr *)((caddr_t)ip6 + off0); 596 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 597 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 598 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 599 th->th_sum = m->m_pkthdr.csum_data; 600 else 601 th->th_sum = in6_cksum_pseudo(ip6, tlen, 602 IPPROTO_TCP, m->m_pkthdr.csum_data); 603 th->th_sum ^= 0xffff; 604 } else 605 th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 606 if (th->th_sum) { 607 TCPSTAT_INC(tcps_rcvbadsum); 608 goto drop; 609 } 610 611 /* 612 * Be proactive about unspecified IPv6 address in source. 613 * As we use all-zero to indicate unbounded/unconnected pcb, 614 * unspecified IPv6 address can be used to confuse us. 615 * 616 * Note that packets with unspecified IPv6 destination is 617 * already dropped in ip6_input. 618 */ 619 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 620 /* XXX stat */ 621 goto drop; 622 } 623 } 624 #endif 625 #if defined(INET) && defined(INET6) 626 else 627 #endif 628 #ifdef INET 629 { 630 /* 631 * Get IP and TCP header together in first mbuf. 632 * Note: IP leaves IP header in first mbuf. 633 */ 634 if (off0 > sizeof (struct ip)) { 635 ip_stripoptions(m, (struct mbuf *)0); 636 off0 = sizeof(struct ip); 637 } 638 if (m->m_len < sizeof (struct tcpiphdr)) { 639 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 640 == NULL) { 641 TCPSTAT_INC(tcps_rcvshort); 642 return; 643 } 644 } 645 ip = mtod(m, struct ip *); 646 ipov = (struct ipovly *)ip; 647 th = (struct tcphdr *)((caddr_t)ip + off0); 648 tlen = ip->ip_len; 649 650 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 651 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 652 th->th_sum = m->m_pkthdr.csum_data; 653 else 654 th->th_sum = in_pseudo(ip->ip_src.s_addr, 655 ip->ip_dst.s_addr, 656 htonl(m->m_pkthdr.csum_data + 657 ip->ip_len + 658 IPPROTO_TCP)); 659 th->th_sum ^= 0xffff; 660 #ifdef TCPDEBUG 661 ipov->ih_len = (u_short)tlen; 662 ipov->ih_len = htons(ipov->ih_len); 663 #endif 664 } else { 665 /* 666 * Checksum extended TCP header and data. 667 */ 668 len = sizeof (struct ip) + tlen; 669 bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 670 ipov->ih_len = (u_short)tlen; 671 ipov->ih_len = htons(ipov->ih_len); 672 th->th_sum = in_cksum(m, len); 673 } 674 if (th->th_sum) { 675 TCPSTAT_INC(tcps_rcvbadsum); 676 goto drop; 677 } 678 /* Re-initialization for later version check */ 679 ip->ip_v = IPVERSION; 680 } 681 #endif /* INET */ 682 683 #ifdef INET6 684 if (isipv6) 685 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 686 #endif 687 #if defined(INET) && defined(INET6) 688 else 689 #endif 690 #ifdef INET 691 iptos = ip->ip_tos; 692 #endif 693 694 /* 695 * Check that TCP offset makes sense, 696 * pull out TCP options and adjust length. XXX 697 */ 698 off = th->th_off << 2; 699 if (off < sizeof (struct tcphdr) || off > tlen) { 700 TCPSTAT_INC(tcps_rcvbadoff); 701 goto drop; 702 } 703 tlen -= off; /* tlen is used instead of ti->ti_len */ 704 if (off > sizeof (struct tcphdr)) { 705 #ifdef INET6 706 if (isipv6) { 707 IP6_EXTHDR_CHECK(m, off0, off, ); 708 ip6 = mtod(m, struct ip6_hdr *); 709 th = (struct tcphdr *)((caddr_t)ip6 + off0); 710 } 711 #endif 712 #if defined(INET) && defined(INET6) 713 else 714 #endif 715 #ifdef INET 716 { 717 if (m->m_len < sizeof(struct ip) + off) { 718 if ((m = m_pullup(m, sizeof (struct ip) + off)) 719 == NULL) { 720 TCPSTAT_INC(tcps_rcvshort); 721 return; 722 } 723 ip = mtod(m, struct ip *); 724 ipov = (struct ipovly *)ip; 725 th = (struct tcphdr *)((caddr_t)ip + off0); 726 } 727 } 728 #endif 729 optlen = off - sizeof (struct tcphdr); 730 optp = (u_char *)(th + 1); 731 } 732 thflags = th->th_flags; 733 734 /* 735 * Convert TCP protocol specific fields to host format. 736 */ 737 tcp_fields_to_host(th); 738 739 /* 740 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 741 */ 742 drop_hdrlen = off0 + off; 743 744 /* 745 * Locate pcb for segment; if we're likely to add or remove a 746 * connection then first acquire pcbinfo lock. There are two cases 747 * where we might discover later we need a write lock despite the 748 * flags: ACKs moving a connection out of the syncache, and ACKs for 749 * a connection in TIMEWAIT. 750 */ 751 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) { 752 INP_INFO_WLOCK(&V_tcbinfo); 753 ti_locked = TI_WLOCKED; 754 } else 755 ti_locked = TI_UNLOCKED; 756 757 findpcb: 758 #ifdef INVARIANTS 759 if (ti_locked == TI_WLOCKED) { 760 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 761 } else { 762 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 763 } 764 #endif 765 766 #ifdef IPFIREWALL_FORWARD 767 /* 768 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 769 */ 770 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 771 #endif /* IPFIREWALL_FORWARD */ 772 773 #ifdef INET6 774 #ifdef IPFIREWALL_FORWARD 775 if (isipv6 && fwd_tag != NULL) { 776 struct sockaddr_in6 *next_hop6; 777 778 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 779 /* 780 * Transparently forwarded. Pretend to be the destination. 781 * Already got one like this? 782 */ 783 inp = in6_pcblookup_mbuf(&V_tcbinfo, 784 &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 785 INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 786 if (!inp) { 787 /* 788 * It's new. Try to find the ambushing socket. 789 * Because we've rewritten the destination address, 790 * any hardware-generated hash is ignored. 791 */ 792 inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 793 th->th_sport, &next_hop6->sin6_addr, 794 next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 795 th->th_dport, INPLOOKUP_WILDCARD | 796 INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 797 } 798 /* Remove the tag from the packet. We don't need it anymore. */ 799 m_tag_delete(m, fwd_tag); 800 } else 801 #endif /* IPFIREWALL_FORWARD */ 802 if (isipv6) { 803 inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 804 th->th_sport, &ip6->ip6_dst, th->th_dport, 805 INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 806 m->m_pkthdr.rcvif, m); 807 } 808 #endif /* INET6 */ 809 #if defined(INET6) && defined(INET) 810 else 811 #endif 812 #ifdef INET 813 #ifdef IPFIREWALL_FORWARD 814 if (fwd_tag != NULL) { 815 struct sockaddr_in *next_hop; 816 817 next_hop = (struct sockaddr_in *)(fwd_tag+1); 818 /* 819 * Transparently forwarded. Pretend to be the destination. 820 * already got one like this? 821 */ 822 inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 823 ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 824 m->m_pkthdr.rcvif, m); 825 if (!inp) { 826 /* 827 * It's new. Try to find the ambushing socket. 828 * Because we've rewritten the destination address, 829 * any hardware-generated hash is ignored. 830 */ 831 inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 832 th->th_sport, next_hop->sin_addr, 833 next_hop->sin_port ? ntohs(next_hop->sin_port) : 834 th->th_dport, INPLOOKUP_WILDCARD | 835 INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 836 } 837 /* Remove the tag from the packet. We don't need it anymore. */ 838 m_tag_delete(m, fwd_tag); 839 } else 840 #endif /* IPFIREWALL_FORWARD */ 841 inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 842 th->th_sport, ip->ip_dst, th->th_dport, 843 INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 844 m->m_pkthdr.rcvif, m); 845 #endif /* INET */ 846 847 /* 848 * If the INPCB does not exist then all data in the incoming 849 * segment is discarded and an appropriate RST is sent back. 850 * XXX MRT Send RST using which routing table? 851 */ 852 if (inp == NULL) { 853 /* 854 * Log communication attempts to ports that are not 855 * in use. 856 */ 857 if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 858 tcp_log_in_vain == 2) { 859 if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 860 log(LOG_INFO, "%s; %s: Connection attempt " 861 "to closed port\n", s, __func__); 862 } 863 /* 864 * When blackholing do not respond with a RST but 865 * completely ignore the segment and drop it. 866 */ 867 if ((V_blackhole == 1 && (thflags & TH_SYN)) || 868 V_blackhole == 2) 869 goto dropunlock; 870 871 rstreason = BANDLIM_RST_CLOSEDPORT; 872 goto dropwithreset; 873 } 874 INP_WLOCK_ASSERT(inp); 875 if (!(inp->inp_flags & INP_HW_FLOWID) 876 && (m->m_flags & M_FLOWID) 877 && ((inp->inp_socket == NULL) 878 || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 879 inp->inp_flags |= INP_HW_FLOWID; 880 inp->inp_flags &= ~INP_SW_FLOWID; 881 inp->inp_flowid = m->m_pkthdr.flowid; 882 } 883 #ifdef IPSEC 884 #ifdef INET6 885 if (isipv6 && ipsec6_in_reject(m, inp)) { 886 V_ipsec6stat.in_polvio++; 887 goto dropunlock; 888 } else 889 #endif /* INET6 */ 890 if (ipsec4_in_reject(m, inp) != 0) { 891 V_ipsec4stat.in_polvio++; 892 goto dropunlock; 893 } 894 #endif /* IPSEC */ 895 896 /* 897 * Check the minimum TTL for socket. 898 */ 899 if (inp->inp_ip_minttl != 0) { 900 #ifdef INET6 901 if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 902 goto dropunlock; 903 else 904 #endif 905 if (inp->inp_ip_minttl > ip->ip_ttl) 906 goto dropunlock; 907 } 908 909 /* 910 * A previous connection in TIMEWAIT state is supposed to catch stray 911 * or duplicate segments arriving late. If this segment was a 912 * legitimate new connection attempt the old INPCB gets removed and 913 * we can try again to find a listening socket. 914 * 915 * At this point, due to earlier optimism, we may hold only an inpcb 916 * lock, and not the inpcbinfo write lock. If so, we need to try to 917 * acquire it, or if that fails, acquire a reference on the inpcb, 918 * drop all locks, acquire a global write lock, and then re-acquire 919 * the inpcb lock. We may at that point discover that another thread 920 * has tried to free the inpcb, in which case we need to loop back 921 * and try to find a new inpcb to deliver to. 922 * 923 * XXXRW: It may be time to rethink timewait locking. 924 */ 925 relocked: 926 if (inp->inp_flags & INP_TIMEWAIT) { 927 if (ti_locked == TI_UNLOCKED) { 928 if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 929 in_pcbref(inp); 930 INP_WUNLOCK(inp); 931 INP_INFO_WLOCK(&V_tcbinfo); 932 ti_locked = TI_WLOCKED; 933 INP_WLOCK(inp); 934 if (in_pcbrele_wlocked(inp)) { 935 inp = NULL; 936 goto findpcb; 937 } 938 } else 939 ti_locked = TI_WLOCKED; 940 } 941 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 942 943 if (thflags & TH_SYN) 944 tcp_dooptions(&to, optp, optlen, TO_SYN); 945 /* 946 * NB: tcp_twcheck unlocks the INP and frees the mbuf. 947 */ 948 if (tcp_twcheck(inp, &to, th, m, tlen)) 949 goto findpcb; 950 INP_INFO_WUNLOCK(&V_tcbinfo); 951 return; 952 } 953 /* 954 * The TCPCB may no longer exist if the connection is winding 955 * down or it is in the CLOSED state. Either way we drop the 956 * segment and send an appropriate response. 957 */ 958 tp = intotcpcb(inp); 959 if (tp == NULL || tp->t_state == TCPS_CLOSED) { 960 rstreason = BANDLIM_RST_CLOSEDPORT; 961 goto dropwithreset; 962 } 963 964 #ifdef TCP_OFFLOAD 965 if (tp->t_flags & TF_TOE) { 966 tcp_offload_input(tp, m); 967 m = NULL; /* consumed by the TOE driver */ 968 goto dropunlock; 969 } 970 #endif 971 972 /* 973 * We've identified a valid inpcb, but it could be that we need an 974 * inpcbinfo write lock but don't hold it. In this case, attempt to 975 * acquire using the same strategy as the TIMEWAIT case above. If we 976 * relock, we have to jump back to 'relocked' as the connection might 977 * now be in TIMEWAIT. 978 */ 979 #ifdef INVARIANTS 980 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) 981 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 982 #endif 983 if (tp->t_state != TCPS_ESTABLISHED) { 984 if (ti_locked == TI_UNLOCKED) { 985 if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 986 in_pcbref(inp); 987 INP_WUNLOCK(inp); 988 INP_INFO_WLOCK(&V_tcbinfo); 989 ti_locked = TI_WLOCKED; 990 INP_WLOCK(inp); 991 if (in_pcbrele_wlocked(inp)) { 992 inp = NULL; 993 goto findpcb; 994 } 995 goto relocked; 996 } else 997 ti_locked = TI_WLOCKED; 998 } 999 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1000 } 1001 1002 #ifdef MAC 1003 INP_WLOCK_ASSERT(inp); 1004 if (mac_inpcb_check_deliver(inp, m)) 1005 goto dropunlock; 1006 #endif 1007 so = inp->inp_socket; 1008 KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1009 #ifdef TCPDEBUG 1010 if (so->so_options & SO_DEBUG) { 1011 ostate = tp->t_state; 1012 #ifdef INET6 1013 if (isipv6) { 1014 bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1015 } else 1016 #endif 1017 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1018 tcp_savetcp = *th; 1019 } 1020 #endif /* TCPDEBUG */ 1021 /* 1022 * When the socket is accepting connections (the INPCB is in LISTEN 1023 * state) we look into the SYN cache if this is a new connection 1024 * attempt or the completion of a previous one. Because listen 1025 * sockets are never in TCPS_ESTABLISHED, the V_tcbinfo lock will be 1026 * held in this case. 1027 */ 1028 if (so->so_options & SO_ACCEPTCONN) { 1029 struct in_conninfo inc; 1030 1031 KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 1032 "tp not listening", __func__)); 1033 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1034 1035 bzero(&inc, sizeof(inc)); 1036 #ifdef INET6 1037 if (isipv6) { 1038 inc.inc_flags |= INC_ISIPV6; 1039 inc.inc6_faddr = ip6->ip6_src; 1040 inc.inc6_laddr = ip6->ip6_dst; 1041 } else 1042 #endif 1043 { 1044 inc.inc_faddr = ip->ip_src; 1045 inc.inc_laddr = ip->ip_dst; 1046 } 1047 inc.inc_fport = th->th_sport; 1048 inc.inc_lport = th->th_dport; 1049 inc.inc_fibnum = so->so_fibnum; 1050 1051 /* 1052 * Check for an existing connection attempt in syncache if 1053 * the flag is only ACK. A successful lookup creates a new 1054 * socket appended to the listen queue in SYN_RECEIVED state. 1055 */ 1056 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1057 /* 1058 * Parse the TCP options here because 1059 * syncookies need access to the reflected 1060 * timestamp. 1061 */ 1062 tcp_dooptions(&to, optp, optlen, 0); 1063 /* 1064 * NB: syncache_expand() doesn't unlock 1065 * inp and tcpinfo locks. 1066 */ 1067 if (!syncache_expand(&inc, &to, th, &so, m)) { 1068 /* 1069 * No syncache entry or ACK was not 1070 * for our SYN/ACK. Send a RST. 1071 * NB: syncache did its own logging 1072 * of the failure cause. 1073 */ 1074 rstreason = BANDLIM_RST_OPENPORT; 1075 goto dropwithreset; 1076 } 1077 if (so == NULL) { 1078 /* 1079 * We completed the 3-way handshake 1080 * but could not allocate a socket 1081 * either due to memory shortage, 1082 * listen queue length limits or 1083 * global socket limits. Send RST 1084 * or wait and have the remote end 1085 * retransmit the ACK for another 1086 * try. 1087 */ 1088 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1089 log(LOG_DEBUG, "%s; %s: Listen socket: " 1090 "Socket allocation failed due to " 1091 "limits or memory shortage, %s\n", 1092 s, __func__, 1093 V_tcp_sc_rst_sock_fail ? 1094 "sending RST" : "try again"); 1095 if (V_tcp_sc_rst_sock_fail) { 1096 rstreason = BANDLIM_UNLIMITED; 1097 goto dropwithreset; 1098 } else 1099 goto dropunlock; 1100 } 1101 /* 1102 * Socket is created in state SYN_RECEIVED. 1103 * Unlock the listen socket, lock the newly 1104 * created socket and update the tp variable. 1105 */ 1106 INP_WUNLOCK(inp); /* listen socket */ 1107 inp = sotoinpcb(so); 1108 INP_WLOCK(inp); /* new connection */ 1109 tp = intotcpcb(inp); 1110 KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 1111 ("%s: ", __func__)); 1112 #ifdef TCP_SIGNATURE 1113 if (sig_checked == 0) { 1114 tcp_dooptions(&to, optp, optlen, 1115 (thflags & TH_SYN) ? TO_SYN : 0); 1116 if (!tcp_signature_verify_input(m, off0, tlen, 1117 optlen, &to, th, tp->t_flags)) { 1118 1119 /* 1120 * In SYN_SENT state if it receives an 1121 * RST, it is allowed for further 1122 * processing. 1123 */ 1124 if ((thflags & TH_RST) == 0 || 1125 (tp->t_state == TCPS_SYN_SENT) == 0) 1126 goto dropunlock; 1127 } 1128 sig_checked = 1; 1129 } 1130 #endif 1131 1132 /* 1133 * Process the segment and the data it 1134 * contains. tcp_do_segment() consumes 1135 * the mbuf chain and unlocks the inpcb. 1136 */ 1137 tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1138 iptos, ti_locked); 1139 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1140 return; 1141 } 1142 /* 1143 * Segment flag validation for new connection attempts: 1144 * 1145 * Our (SYN|ACK) response was rejected. 1146 * Check with syncache and remove entry to prevent 1147 * retransmits. 1148 * 1149 * NB: syncache_chkrst does its own logging of failure 1150 * causes. 1151 */ 1152 if (thflags & TH_RST) { 1153 syncache_chkrst(&inc, th); 1154 goto dropunlock; 1155 } 1156 /* 1157 * We can't do anything without SYN. 1158 */ 1159 if ((thflags & TH_SYN) == 0) { 1160 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1161 log(LOG_DEBUG, "%s; %s: Listen socket: " 1162 "SYN is missing, segment ignored\n", 1163 s, __func__); 1164 TCPSTAT_INC(tcps_badsyn); 1165 goto dropunlock; 1166 } 1167 /* 1168 * (SYN|ACK) is bogus on a listen socket. 1169 */ 1170 if (thflags & TH_ACK) { 1171 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1172 log(LOG_DEBUG, "%s; %s: Listen socket: " 1173 "SYN|ACK invalid, segment rejected\n", 1174 s, __func__); 1175 syncache_badack(&inc); /* XXX: Not needed! */ 1176 TCPSTAT_INC(tcps_badsyn); 1177 rstreason = BANDLIM_RST_OPENPORT; 1178 goto dropwithreset; 1179 } 1180 /* 1181 * If the drop_synfin option is enabled, drop all 1182 * segments with both the SYN and FIN bits set. 1183 * This prevents e.g. nmap from identifying the 1184 * TCP/IP stack. 1185 * XXX: Poor reasoning. nmap has other methods 1186 * and is constantly refining its stack detection 1187 * strategies. 1188 * XXX: This is a violation of the TCP specification 1189 * and was used by RFC1644. 1190 */ 1191 if ((thflags & TH_FIN) && V_drop_synfin) { 1192 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1193 log(LOG_DEBUG, "%s; %s: Listen socket: " 1194 "SYN|FIN segment ignored (based on " 1195 "sysctl setting)\n", s, __func__); 1196 TCPSTAT_INC(tcps_badsyn); 1197 goto dropunlock; 1198 } 1199 /* 1200 * Segment's flags are (SYN) or (SYN|FIN). 1201 * 1202 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1203 * as they do not affect the state of the TCP FSM. 1204 * The data pointed to by TH_URG and th_urp is ignored. 1205 */ 1206 KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1207 ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1208 KASSERT(thflags & (TH_SYN), 1209 ("%s: Listen socket: TH_SYN not set", __func__)); 1210 #ifdef INET6 1211 /* 1212 * If deprecated address is forbidden, 1213 * we do not accept SYN to deprecated interface 1214 * address to prevent any new inbound connection from 1215 * getting established. 1216 * When we do not accept SYN, we send a TCP RST, 1217 * with deprecated source address (instead of dropping 1218 * it). We compromise it as it is much better for peer 1219 * to send a RST, and RST will be the final packet 1220 * for the exchange. 1221 * 1222 * If we do not forbid deprecated addresses, we accept 1223 * the SYN packet. RFC2462 does not suggest dropping 1224 * SYN in this case. 1225 * If we decipher RFC2462 5.5.4, it says like this: 1226 * 1. use of deprecated addr with existing 1227 * communication is okay - "SHOULD continue to be 1228 * used" 1229 * 2. use of it with new communication: 1230 * (2a) "SHOULD NOT be used if alternate address 1231 * with sufficient scope is available" 1232 * (2b) nothing mentioned otherwise. 1233 * Here we fall into (2b) case as we have no choice in 1234 * our source address selection - we must obey the peer. 1235 * 1236 * The wording in RFC2462 is confusing, and there are 1237 * multiple description text for deprecated address 1238 * handling - worse, they are not exactly the same. 1239 * I believe 5.5.4 is the best one, so we follow 5.5.4. 1240 */ 1241 if (isipv6 && !V_ip6_use_deprecated) { 1242 struct in6_ifaddr *ia6; 1243 1244 ia6 = ip6_getdstifaddr(m); 1245 if (ia6 != NULL && 1246 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 1247 ifa_free(&ia6->ia_ifa); 1248 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1249 log(LOG_DEBUG, "%s; %s: Listen socket: " 1250 "Connection attempt to deprecated " 1251 "IPv6 address rejected\n", 1252 s, __func__); 1253 rstreason = BANDLIM_RST_OPENPORT; 1254 goto dropwithreset; 1255 } 1256 if (ia6) 1257 ifa_free(&ia6->ia_ifa); 1258 } 1259 #endif /* INET6 */ 1260 /* 1261 * Basic sanity checks on incoming SYN requests: 1262 * Don't respond if the destination is a link layer 1263 * broadcast according to RFC1122 4.2.3.10, p. 104. 1264 * If it is from this socket it must be forged. 1265 * Don't respond if the source or destination is a 1266 * global or subnet broad- or multicast address. 1267 * Note that it is quite possible to receive unicast 1268 * link-layer packets with a broadcast IP address. Use 1269 * in_broadcast() to find them. 1270 */ 1271 if (m->m_flags & (M_BCAST|M_MCAST)) { 1272 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1273 log(LOG_DEBUG, "%s; %s: Listen socket: " 1274 "Connection attempt from broad- or multicast " 1275 "link layer address ignored\n", s, __func__); 1276 goto dropunlock; 1277 } 1278 #ifdef INET6 1279 if (isipv6) { 1280 if (th->th_dport == th->th_sport && 1281 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1282 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1283 log(LOG_DEBUG, "%s; %s: Listen socket: " 1284 "Connection attempt to/from self " 1285 "ignored\n", s, __func__); 1286 goto dropunlock; 1287 } 1288 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1289 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1290 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1291 log(LOG_DEBUG, "%s; %s: Listen socket: " 1292 "Connection attempt from/to multicast " 1293 "address ignored\n", s, __func__); 1294 goto dropunlock; 1295 } 1296 } 1297 #endif 1298 #if defined(INET) && defined(INET6) 1299 else 1300 #endif 1301 #ifdef INET 1302 { 1303 if (th->th_dport == th->th_sport && 1304 ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1305 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1306 log(LOG_DEBUG, "%s; %s: Listen socket: " 1307 "Connection attempt from/to self " 1308 "ignored\n", s, __func__); 1309 goto dropunlock; 1310 } 1311 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1312 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 1313 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1314 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1315 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1316 log(LOG_DEBUG, "%s; %s: Listen socket: " 1317 "Connection attempt from/to broad- " 1318 "or multicast address ignored\n", 1319 s, __func__); 1320 goto dropunlock; 1321 } 1322 } 1323 #endif 1324 /* 1325 * SYN appears to be valid. Create compressed TCP state 1326 * for syncache. 1327 */ 1328 #ifdef TCPDEBUG 1329 if (so->so_options & SO_DEBUG) 1330 tcp_trace(TA_INPUT, ostate, tp, 1331 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1332 #endif 1333 tcp_dooptions(&to, optp, optlen, TO_SYN); 1334 syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL); 1335 /* 1336 * Entry added to syncache and mbuf consumed. 1337 * Everything already unlocked by syncache_add(). 1338 */ 1339 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1340 return; 1341 } 1342 1343 #ifdef TCP_SIGNATURE 1344 if (sig_checked == 0) { 1345 tcp_dooptions(&to, optp, optlen, 1346 (thflags & TH_SYN) ? TO_SYN : 0); 1347 if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to, 1348 th, tp->t_flags)) { 1349 1350 /* 1351 * In SYN_SENT state if it receives an RST, it is 1352 * allowed for further processing. 1353 */ 1354 if ((thflags & TH_RST) == 0 || 1355 (tp->t_state == TCPS_SYN_SENT) == 0) 1356 goto dropunlock; 1357 } 1358 sig_checked = 1; 1359 } 1360 #endif 1361 1362 /* 1363 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 1364 * state. tcp_do_segment() always consumes the mbuf chain, unlocks 1365 * the inpcb, and unlocks pcbinfo. 1366 */ 1367 tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1368 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1369 return; 1370 1371 dropwithreset: 1372 if (ti_locked == TI_WLOCKED) { 1373 INP_INFO_WUNLOCK(&V_tcbinfo); 1374 ti_locked = TI_UNLOCKED; 1375 } 1376 #ifdef INVARIANTS 1377 else { 1378 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1379 "ti_locked: %d", __func__, ti_locked)); 1380 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1381 } 1382 #endif 1383 1384 if (inp != NULL) { 1385 tcp_dropwithreset(m, th, tp, tlen, rstreason); 1386 INP_WUNLOCK(inp); 1387 } else 1388 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1389 m = NULL; /* mbuf chain got consumed. */ 1390 goto drop; 1391 1392 dropunlock: 1393 if (ti_locked == TI_WLOCKED) { 1394 INP_INFO_WUNLOCK(&V_tcbinfo); 1395 ti_locked = TI_UNLOCKED; 1396 } 1397 #ifdef INVARIANTS 1398 else { 1399 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1400 "ti_locked: %d", __func__, ti_locked)); 1401 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1402 } 1403 #endif 1404 1405 if (inp != NULL) 1406 INP_WUNLOCK(inp); 1407 1408 drop: 1409 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1410 if (s != NULL) 1411 free(s, M_TCPLOG); 1412 if (m != NULL) 1413 m_freem(m); 1414 } 1415 1416 static void 1417 tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1418 struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1419 int ti_locked) 1420 { 1421 int thflags, acked, ourfinisacked, needoutput = 0; 1422 int rstreason, todrop, win; 1423 u_long tiwin; 1424 struct tcpopt to; 1425 1426 #ifdef TCPDEBUG 1427 /* 1428 * The size of tcp_saveipgen must be the size of the max ip header, 1429 * now IPv6. 1430 */ 1431 u_char tcp_saveipgen[IP6_HDR_LEN]; 1432 struct tcphdr tcp_savetcp; 1433 short ostate = 0; 1434 #endif 1435 thflags = th->th_flags; 1436 tp->sackhint.last_sack_ack = 0; 1437 1438 /* 1439 * If this is either a state-changing packet or current state isn't 1440 * established, we require a write lock on tcbinfo. Otherwise, we 1441 * allow either a read lock or a write lock, as we may have acquired 1442 * a write lock due to a race. 1443 * 1444 * Require a global write lock for SYN/FIN/RST segments or 1445 * non-established connections; otherwise accept either a read or 1446 * write lock, as we may have conservatively acquired a write lock in 1447 * certain cases in tcp_input() (is this still true?). Currently we 1448 * will never enter with no lock, so we try to drop it quickly in the 1449 * common pure ack/pure data cases. 1450 */ 1451 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1452 tp->t_state != TCPS_ESTABLISHED) { 1453 KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1454 "SYN/FIN/RST/!EST", __func__, ti_locked)); 1455 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1456 } else { 1457 #ifdef INVARIANTS 1458 if (ti_locked == TI_WLOCKED) 1459 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1460 else { 1461 KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1462 "ti_locked: %d", __func__, ti_locked)); 1463 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1464 } 1465 #endif 1466 } 1467 INP_WLOCK_ASSERT(tp->t_inpcb); 1468 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1469 __func__)); 1470 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1471 __func__)); 1472 1473 /* 1474 * Segment received on connection. 1475 * Reset idle time and keep-alive timer. 1476 * XXX: This should be done after segment 1477 * validation to ignore broken/spoofed segs. 1478 */ 1479 tp->t_rcvtime = ticks; 1480 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1481 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 1482 1483 /* 1484 * Unscale the window into a 32-bit value. 1485 * For the SYN_SENT state the scale is zero. 1486 */ 1487 tiwin = th->th_win << tp->snd_scale; 1488 1489 /* 1490 * TCP ECN processing. 1491 */ 1492 if (tp->t_flags & TF_ECN_PERMIT) { 1493 if (thflags & TH_CWR) 1494 tp->t_flags &= ~TF_ECN_SND_ECE; 1495 switch (iptos & IPTOS_ECN_MASK) { 1496 case IPTOS_ECN_CE: 1497 tp->t_flags |= TF_ECN_SND_ECE; 1498 TCPSTAT_INC(tcps_ecn_ce); 1499 break; 1500 case IPTOS_ECN_ECT0: 1501 TCPSTAT_INC(tcps_ecn_ect0); 1502 break; 1503 case IPTOS_ECN_ECT1: 1504 TCPSTAT_INC(tcps_ecn_ect1); 1505 break; 1506 } 1507 /* Congestion experienced. */ 1508 if (thflags & TH_ECE) { 1509 cc_cong_signal(tp, th, CC_ECN); 1510 } 1511 } 1512 1513 /* 1514 * Parse options on any incoming segment. 1515 */ 1516 tcp_dooptions(&to, (u_char *)(th + 1), 1517 (th->th_off << 2) - sizeof(struct tcphdr), 1518 (thflags & TH_SYN) ? TO_SYN : 0); 1519 1520 /* 1521 * If echoed timestamp is later than the current time, 1522 * fall back to non RFC1323 RTT calculation. Normalize 1523 * timestamp if syncookies were used when this connection 1524 * was established. 1525 */ 1526 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1527 to.to_tsecr -= tp->ts_offset; 1528 if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1529 to.to_tsecr = 0; 1530 } 1531 1532 /* 1533 * Process options only when we get SYN/ACK back. The SYN case 1534 * for incoming connections is handled in tcp_syncache. 1535 * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1536 * or <SYN,ACK>) segment itself is never scaled. 1537 * XXX this is traditional behavior, may need to be cleaned up. 1538 */ 1539 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1540 if ((to.to_flags & TOF_SCALE) && 1541 (tp->t_flags & TF_REQ_SCALE)) { 1542 tp->t_flags |= TF_RCVD_SCALE; 1543 tp->snd_scale = to.to_wscale; 1544 } 1545 /* 1546 * Initial send window. It will be updated with 1547 * the next incoming segment to the scaled value. 1548 */ 1549 tp->snd_wnd = th->th_win; 1550 if (to.to_flags & TOF_TS) { 1551 tp->t_flags |= TF_RCVD_TSTMP; 1552 tp->ts_recent = to.to_tsval; 1553 tp->ts_recent_age = tcp_ts_getticks(); 1554 } 1555 if (to.to_flags & TOF_MSS) 1556 tcp_mss(tp, to.to_mss); 1557 if ((tp->t_flags & TF_SACK_PERMIT) && 1558 (to.to_flags & TOF_SACKPERM) == 0) 1559 tp->t_flags &= ~TF_SACK_PERMIT; 1560 } 1561 1562 /* 1563 * Header prediction: check for the two common cases 1564 * of a uni-directional data xfer. If the packet has 1565 * no control flags, is in-sequence, the window didn't 1566 * change and we're not retransmitting, it's a 1567 * candidate. If the length is zero and the ack moved 1568 * forward, we're the sender side of the xfer. Just 1569 * free the data acked & wake any higher level process 1570 * that was blocked waiting for space. If the length 1571 * is non-zero and the ack didn't move, we're the 1572 * receiver side. If we're getting packets in-order 1573 * (the reassembly queue is empty), add the data to 1574 * the socket buffer and note that we need a delayed ack. 1575 * Make sure that the hidden state-flags are also off. 1576 * Since we check for TCPS_ESTABLISHED first, it can only 1577 * be TH_NEEDSYN. 1578 */ 1579 if (tp->t_state == TCPS_ESTABLISHED && 1580 th->th_seq == tp->rcv_nxt && 1581 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1582 tp->snd_nxt == tp->snd_max && 1583 tiwin && tiwin == tp->snd_wnd && 1584 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1585 LIST_EMPTY(&tp->t_segq) && 1586 ((to.to_flags & TOF_TS) == 0 || 1587 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1588 1589 /* 1590 * If last ACK falls within this segment's sequence numbers, 1591 * record the timestamp. 1592 * NOTE that the test is modified according to the latest 1593 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1594 */ 1595 if ((to.to_flags & TOF_TS) != 0 && 1596 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1597 tp->ts_recent_age = tcp_ts_getticks(); 1598 tp->ts_recent = to.to_tsval; 1599 } 1600 1601 if (tlen == 0) { 1602 if (SEQ_GT(th->th_ack, tp->snd_una) && 1603 SEQ_LEQ(th->th_ack, tp->snd_max) && 1604 !IN_RECOVERY(tp->t_flags) && 1605 (to.to_flags & TOF_SACK) == 0 && 1606 TAILQ_EMPTY(&tp->snd_holes)) { 1607 /* 1608 * This is a pure ack for outstanding data. 1609 */ 1610 if (ti_locked == TI_WLOCKED) 1611 INP_INFO_WUNLOCK(&V_tcbinfo); 1612 ti_locked = TI_UNLOCKED; 1613 1614 TCPSTAT_INC(tcps_predack); 1615 1616 /* 1617 * "bad retransmit" recovery. 1618 */ 1619 if (tp->t_rxtshift == 1 && 1620 tp->t_flags & TF_PREVVALID && 1621 (int)(ticks - tp->t_badrxtwin) < 0) { 1622 cc_cong_signal(tp, th, CC_RTO_ERR); 1623 } 1624 1625 /* 1626 * Recalculate the transmit timer / rtt. 1627 * 1628 * Some boxes send broken timestamp replies 1629 * during the SYN+ACK phase, ignore 1630 * timestamps of 0 or we could calculate a 1631 * huge RTT and blow up the retransmit timer. 1632 */ 1633 if ((to.to_flags & TOF_TS) != 0 && 1634 to.to_tsecr) { 1635 u_int t; 1636 1637 t = tcp_ts_getticks() - to.to_tsecr; 1638 if (!tp->t_rttlow || tp->t_rttlow > t) 1639 tp->t_rttlow = t; 1640 tcp_xmit_timer(tp, 1641 TCP_TS_TO_TICKS(t) + 1); 1642 } else if (tp->t_rtttime && 1643 SEQ_GT(th->th_ack, tp->t_rtseq)) { 1644 if (!tp->t_rttlow || 1645 tp->t_rttlow > ticks - tp->t_rtttime) 1646 tp->t_rttlow = ticks - tp->t_rtttime; 1647 tcp_xmit_timer(tp, 1648 ticks - tp->t_rtttime); 1649 } 1650 acked = BYTES_THIS_ACK(tp, th); 1651 1652 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 1653 hhook_run_tcp_est_in(tp, th, &to); 1654 1655 TCPSTAT_INC(tcps_rcvackpack); 1656 TCPSTAT_ADD(tcps_rcvackbyte, acked); 1657 sbdrop(&so->so_snd, acked); 1658 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 1659 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1660 tp->snd_recover = th->th_ack - 1; 1661 1662 /* 1663 * Let the congestion control algorithm update 1664 * congestion control related information. This 1665 * typically means increasing the congestion 1666 * window. 1667 */ 1668 cc_ack_received(tp, th, CC_ACK); 1669 1670 tp->snd_una = th->th_ack; 1671 /* 1672 * Pull snd_wl2 up to prevent seq wrap relative 1673 * to th_ack. 1674 */ 1675 tp->snd_wl2 = th->th_ack; 1676 tp->t_dupacks = 0; 1677 m_freem(m); 1678 ND6_HINT(tp); /* Some progress has been made. */ 1679 1680 /* 1681 * If all outstanding data are acked, stop 1682 * retransmit timer, otherwise restart timer 1683 * using current (possibly backed-off) value. 1684 * If process is waiting for space, 1685 * wakeup/selwakeup/signal. If data 1686 * are ready to send, let tcp_output 1687 * decide between more output or persist. 1688 */ 1689 #ifdef TCPDEBUG 1690 if (so->so_options & SO_DEBUG) 1691 tcp_trace(TA_INPUT, ostate, tp, 1692 (void *)tcp_saveipgen, 1693 &tcp_savetcp, 0); 1694 #endif 1695 if (tp->snd_una == tp->snd_max) 1696 tcp_timer_activate(tp, TT_REXMT, 0); 1697 else if (!tcp_timer_active(tp, TT_PERSIST)) 1698 tcp_timer_activate(tp, TT_REXMT, 1699 tp->t_rxtcur); 1700 sowwakeup(so); 1701 if (so->so_snd.sb_cc) 1702 (void) tcp_output(tp); 1703 goto check_delack; 1704 } 1705 } else if (th->th_ack == tp->snd_una && 1706 tlen <= sbspace(&so->so_rcv)) { 1707 int newsize = 0; /* automatic sockbuf scaling */ 1708 1709 /* 1710 * This is a pure, in-sequence data packet with 1711 * nothing on the reassembly queue and we have enough 1712 * buffer space to take it. 1713 */ 1714 if (ti_locked == TI_WLOCKED) 1715 INP_INFO_WUNLOCK(&V_tcbinfo); 1716 ti_locked = TI_UNLOCKED; 1717 1718 /* Clean receiver SACK report if present */ 1719 if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 1720 tcp_clean_sackreport(tp); 1721 TCPSTAT_INC(tcps_preddat); 1722 tp->rcv_nxt += tlen; 1723 /* 1724 * Pull snd_wl1 up to prevent seq wrap relative to 1725 * th_seq. 1726 */ 1727 tp->snd_wl1 = th->th_seq; 1728 /* 1729 * Pull rcv_up up to prevent seq wrap relative to 1730 * rcv_nxt. 1731 */ 1732 tp->rcv_up = tp->rcv_nxt; 1733 TCPSTAT_INC(tcps_rcvpack); 1734 TCPSTAT_ADD(tcps_rcvbyte, tlen); 1735 ND6_HINT(tp); /* Some progress has been made */ 1736 #ifdef TCPDEBUG 1737 if (so->so_options & SO_DEBUG) 1738 tcp_trace(TA_INPUT, ostate, tp, 1739 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1740 #endif 1741 /* 1742 * Automatic sizing of receive socket buffer. Often the send 1743 * buffer size is not optimally adjusted to the actual network 1744 * conditions at hand (delay bandwidth product). Setting the 1745 * buffer size too small limits throughput on links with high 1746 * bandwidth and high delay (eg. trans-continental/oceanic links). 1747 * 1748 * On the receive side the socket buffer memory is only rarely 1749 * used to any significant extent. This allows us to be much 1750 * more aggressive in scaling the receive socket buffer. For 1751 * the case that the buffer space is actually used to a large 1752 * extent and we run out of kernel memory we can simply drop 1753 * the new segments; TCP on the sender will just retransmit it 1754 * later. Setting the buffer size too big may only consume too 1755 * much kernel memory if the application doesn't read() from 1756 * the socket or packet loss or reordering makes use of the 1757 * reassembly queue. 1758 * 1759 * The criteria to step up the receive buffer one notch are: 1760 * 1. the number of bytes received during the time it takes 1761 * one timestamp to be reflected back to us (the RTT); 1762 * 2. received bytes per RTT is within seven eighth of the 1763 * current socket buffer size; 1764 * 3. receive buffer size has not hit maximal automatic size; 1765 * 1766 * This algorithm does one step per RTT at most and only if 1767 * we receive a bulk stream w/o packet losses or reorderings. 1768 * Shrinking the buffer during idle times is not necessary as 1769 * it doesn't consume any memory when idle. 1770 * 1771 * TODO: Only step up if the application is actually serving 1772 * the buffer to better manage the socket buffer resources. 1773 */ 1774 if (V_tcp_do_autorcvbuf && 1775 to.to_tsecr && 1776 (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 1777 if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 1778 to.to_tsecr - tp->rfbuf_ts < hz) { 1779 if (tp->rfbuf_cnt > 1780 (so->so_rcv.sb_hiwat / 8 * 7) && 1781 so->so_rcv.sb_hiwat < 1782 V_tcp_autorcvbuf_max) { 1783 newsize = 1784 min(so->so_rcv.sb_hiwat + 1785 V_tcp_autorcvbuf_inc, 1786 V_tcp_autorcvbuf_max); 1787 } 1788 /* Start over with next RTT. */ 1789 tp->rfbuf_ts = 0; 1790 tp->rfbuf_cnt = 0; 1791 } else 1792 tp->rfbuf_cnt += tlen; /* add up */ 1793 } 1794 1795 /* Add data to socket buffer. */ 1796 SOCKBUF_LOCK(&so->so_rcv); 1797 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1798 m_freem(m); 1799 } else { 1800 /* 1801 * Set new socket buffer size. 1802 * Give up when limit is reached. 1803 */ 1804 if (newsize) 1805 if (!sbreserve_locked(&so->so_rcv, 1806 newsize, so, NULL)) 1807 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1808 m_adj(m, drop_hdrlen); /* delayed header drop */ 1809 sbappendstream_locked(&so->so_rcv, m); 1810 } 1811 /* NB: sorwakeup_locked() does an implicit unlock. */ 1812 sorwakeup_locked(so); 1813 if (DELAY_ACK(tp)) { 1814 tp->t_flags |= TF_DELACK; 1815 } else { 1816 tp->t_flags |= TF_ACKNOW; 1817 tcp_output(tp); 1818 } 1819 goto check_delack; 1820 } 1821 } 1822 1823 /* 1824 * Calculate amount of space in receive window, 1825 * and then do TCP input processing. 1826 * Receive window is amount of space in rcv queue, 1827 * but not less than advertised window. 1828 */ 1829 win = sbspace(&so->so_rcv); 1830 if (win < 0) 1831 win = 0; 1832 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1833 1834 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 1835 tp->rfbuf_ts = 0; 1836 tp->rfbuf_cnt = 0; 1837 1838 switch (tp->t_state) { 1839 1840 /* 1841 * If the state is SYN_RECEIVED: 1842 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1843 */ 1844 case TCPS_SYN_RECEIVED: 1845 if ((thflags & TH_ACK) && 1846 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1847 SEQ_GT(th->th_ack, tp->snd_max))) { 1848 rstreason = BANDLIM_RST_OPENPORT; 1849 goto dropwithreset; 1850 } 1851 break; 1852 1853 /* 1854 * If the state is SYN_SENT: 1855 * if seg contains an ACK, but not for our SYN, drop the input. 1856 * if seg contains a RST, then drop the connection. 1857 * if seg does not contain SYN, then drop it. 1858 * Otherwise this is an acceptable SYN segment 1859 * initialize tp->rcv_nxt and tp->irs 1860 * if seg contains ack then advance tp->snd_una 1861 * if seg contains an ECE and ECN support is enabled, the stream 1862 * is ECN capable. 1863 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1864 * arrange for segment to be acked (eventually) 1865 * continue processing rest of data/controls, beginning with URG 1866 */ 1867 case TCPS_SYN_SENT: 1868 if ((thflags & TH_ACK) && 1869 (SEQ_LEQ(th->th_ack, tp->iss) || 1870 SEQ_GT(th->th_ack, tp->snd_max))) { 1871 rstreason = BANDLIM_UNLIMITED; 1872 goto dropwithreset; 1873 } 1874 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1875 tp = tcp_drop(tp, ECONNREFUSED); 1876 if (thflags & TH_RST) 1877 goto drop; 1878 if (!(thflags & TH_SYN)) 1879 goto drop; 1880 1881 tp->irs = th->th_seq; 1882 tcp_rcvseqinit(tp); 1883 if (thflags & TH_ACK) { 1884 TCPSTAT_INC(tcps_connects); 1885 soisconnected(so); 1886 #ifdef MAC 1887 mac_socketpeer_set_from_mbuf(m, so); 1888 #endif 1889 /* Do window scaling on this connection? */ 1890 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1891 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1892 tp->rcv_scale = tp->request_r_scale; 1893 } 1894 tp->rcv_adv += imin(tp->rcv_wnd, 1895 TCP_MAXWIN << tp->rcv_scale); 1896 tp->snd_una++; /* SYN is acked */ 1897 /* 1898 * If there's data, delay ACK; if there's also a FIN 1899 * ACKNOW will be turned on later. 1900 */ 1901 if (DELAY_ACK(tp) && tlen != 0) 1902 tcp_timer_activate(tp, TT_DELACK, 1903 tcp_delacktime); 1904 else 1905 tp->t_flags |= TF_ACKNOW; 1906 1907 if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1908 tp->t_flags |= TF_ECN_PERMIT; 1909 TCPSTAT_INC(tcps_ecn_shs); 1910 } 1911 1912 /* 1913 * Received <SYN,ACK> in SYN_SENT[*] state. 1914 * Transitions: 1915 * SYN_SENT --> ESTABLISHED 1916 * SYN_SENT* --> FIN_WAIT_1 1917 */ 1918 tp->t_starttime = ticks; 1919 if (tp->t_flags & TF_NEEDFIN) { 1920 tp->t_state = TCPS_FIN_WAIT_1; 1921 tp->t_flags &= ~TF_NEEDFIN; 1922 thflags &= ~TH_SYN; 1923 } else { 1924 tp->t_state = TCPS_ESTABLISHED; 1925 cc_conn_init(tp); 1926 tcp_timer_activate(tp, TT_KEEP, 1927 TP_KEEPIDLE(tp)); 1928 } 1929 } else { 1930 /* 1931 * Received initial SYN in SYN-SENT[*] state => 1932 * simultaneous open. If segment contains CC option 1933 * and there is a cached CC, apply TAO test. 1934 * If it succeeds, connection is * half-synchronized. 1935 * Otherwise, do 3-way handshake: 1936 * SYN-SENT -> SYN-RECEIVED 1937 * SYN-SENT* -> SYN-RECEIVED* 1938 * If there was no CC option, clear cached CC value. 1939 */ 1940 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1941 tcp_timer_activate(tp, TT_REXMT, 0); 1942 tp->t_state = TCPS_SYN_RECEIVED; 1943 } 1944 1945 KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1946 "ti_locked %d", __func__, ti_locked)); 1947 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1948 INP_WLOCK_ASSERT(tp->t_inpcb); 1949 1950 /* 1951 * Advance th->th_seq to correspond to first data byte. 1952 * If data, trim to stay within window, 1953 * dropping FIN if necessary. 1954 */ 1955 th->th_seq++; 1956 if (tlen > tp->rcv_wnd) { 1957 todrop = tlen - tp->rcv_wnd; 1958 m_adj(m, -todrop); 1959 tlen = tp->rcv_wnd; 1960 thflags &= ~TH_FIN; 1961 TCPSTAT_INC(tcps_rcvpackafterwin); 1962 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1963 } 1964 tp->snd_wl1 = th->th_seq - 1; 1965 tp->rcv_up = th->th_seq; 1966 /* 1967 * Client side of transaction: already sent SYN and data. 1968 * If the remote host used T/TCP to validate the SYN, 1969 * our data will be ACK'd; if so, enter normal data segment 1970 * processing in the middle of step 5, ack processing. 1971 * Otherwise, goto step 6. 1972 */ 1973 if (thflags & TH_ACK) 1974 goto process_ACK; 1975 1976 goto step6; 1977 1978 /* 1979 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1980 * do normal processing. 1981 * 1982 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1983 */ 1984 case TCPS_LAST_ACK: 1985 case TCPS_CLOSING: 1986 break; /* continue normal processing */ 1987 } 1988 1989 /* 1990 * States other than LISTEN or SYN_SENT. 1991 * First check the RST flag and sequence number since reset segments 1992 * are exempt from the timestamp and connection count tests. This 1993 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1994 * below which allowed reset segments in half the sequence space 1995 * to fall though and be processed (which gives forged reset 1996 * segments with a random sequence number a 50 percent chance of 1997 * killing a connection). 1998 * Then check timestamp, if present. 1999 * Then check the connection count, if present. 2000 * Then check that at least some bytes of segment are within 2001 * receive window. If segment begins before rcv_nxt, 2002 * drop leading data (and SYN); if nothing left, just ack. 2003 * 2004 * 2005 * If the RST bit is set, check the sequence number to see 2006 * if this is a valid reset segment. 2007 * RFC 793 page 37: 2008 * In all states except SYN-SENT, all reset (RST) segments 2009 * are validated by checking their SEQ-fields. A reset is 2010 * valid if its sequence number is in the window. 2011 * Note: this does not take into account delayed ACKs, so 2012 * we should test against last_ack_sent instead of rcv_nxt. 2013 * The sequence number in the reset segment is normally an 2014 * echo of our outgoing acknowlegement numbers, but some hosts 2015 * send a reset with the sequence number at the rightmost edge 2016 * of our receive window, and we have to handle this case. 2017 * Note 2: Paul Watson's paper "Slipping in the Window" has shown 2018 * that brute force RST attacks are possible. To combat this, 2019 * we use a much stricter check while in the ESTABLISHED state, 2020 * only accepting RSTs where the sequence number is equal to 2021 * last_ack_sent. In all other states (the states in which a 2022 * RST is more likely), the more permissive check is used. 2023 * If we have multiple segments in flight, the initial reset 2024 * segment sequence numbers will be to the left of last_ack_sent, 2025 * but they will eventually catch up. 2026 * In any case, it never made sense to trim reset segments to 2027 * fit the receive window since RFC 1122 says: 2028 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 2029 * 2030 * A TCP SHOULD allow a received RST segment to include data. 2031 * 2032 * DISCUSSION 2033 * It has been suggested that a RST segment could contain 2034 * ASCII text that encoded and explained the cause of the 2035 * RST. No standard has yet been established for such 2036 * data. 2037 * 2038 * If the reset segment passes the sequence number test examine 2039 * the state: 2040 * SYN_RECEIVED STATE: 2041 * If passive open, return to LISTEN state. 2042 * If active open, inform user that connection was refused. 2043 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 2044 * Inform user that connection was reset, and close tcb. 2045 * CLOSING, LAST_ACK STATES: 2046 * Close the tcb. 2047 * TIME_WAIT STATE: 2048 * Drop the segment - see Stevens, vol. 2, p. 964 and 2049 * RFC 1337. 2050 */ 2051 if (thflags & TH_RST) { 2052 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 2053 SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 2054 switch (tp->t_state) { 2055 2056 case TCPS_SYN_RECEIVED: 2057 so->so_error = ECONNREFUSED; 2058 goto close; 2059 2060 case TCPS_ESTABLISHED: 2061 if (V_tcp_insecure_rst == 0 && 2062 !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 2063 SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 2064 !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 2065 SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 2066 TCPSTAT_INC(tcps_badrst); 2067 goto drop; 2068 } 2069 /* FALLTHROUGH */ 2070 case TCPS_FIN_WAIT_1: 2071 case TCPS_FIN_WAIT_2: 2072 case TCPS_CLOSE_WAIT: 2073 so->so_error = ECONNRESET; 2074 close: 2075 KASSERT(ti_locked == TI_WLOCKED, 2076 ("tcp_do_segment: TH_RST 1 ti_locked %d", 2077 ti_locked)); 2078 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2079 2080 tp->t_state = TCPS_CLOSED; 2081 TCPSTAT_INC(tcps_drops); 2082 tp = tcp_close(tp); 2083 break; 2084 2085 case TCPS_CLOSING: 2086 case TCPS_LAST_ACK: 2087 KASSERT(ti_locked == TI_WLOCKED, 2088 ("tcp_do_segment: TH_RST 2 ti_locked %d", 2089 ti_locked)); 2090 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2091 2092 tp = tcp_close(tp); 2093 break; 2094 } 2095 } 2096 goto drop; 2097 } 2098 2099 /* 2100 * RFC 1323 PAWS: If we have a timestamp reply on this segment 2101 * and it's less than ts_recent, drop it. 2102 */ 2103 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 2104 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2105 2106 /* Check to see if ts_recent is over 24 days old. */ 2107 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2108 /* 2109 * Invalidate ts_recent. If this segment updates 2110 * ts_recent, the age will be reset later and ts_recent 2111 * will get a valid value. If it does not, setting 2112 * ts_recent to zero will at least satisfy the 2113 * requirement that zero be placed in the timestamp 2114 * echo reply when ts_recent isn't valid. The 2115 * age isn't reset until we get a valid ts_recent 2116 * because we don't want out-of-order segments to be 2117 * dropped when ts_recent is old. 2118 */ 2119 tp->ts_recent = 0; 2120 } else { 2121 TCPSTAT_INC(tcps_rcvduppack); 2122 TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 2123 TCPSTAT_INC(tcps_pawsdrop); 2124 if (tlen) 2125 goto dropafterack; 2126 goto drop; 2127 } 2128 } 2129 2130 /* 2131 * In the SYN-RECEIVED state, validate that the packet belongs to 2132 * this connection before trimming the data to fit the receive 2133 * window. Check the sequence number versus IRS since we know 2134 * the sequence numbers haven't wrapped. This is a partial fix 2135 * for the "LAND" DoS attack. 2136 */ 2137 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2138 rstreason = BANDLIM_RST_OPENPORT; 2139 goto dropwithreset; 2140 } 2141 2142 todrop = tp->rcv_nxt - th->th_seq; 2143 if (todrop > 0) { 2144 /* 2145 * If this is a duplicate SYN for our current connection, 2146 * advance over it and pretend and it's not a SYN. 2147 */ 2148 if (thflags & TH_SYN && th->th_seq == tp->irs) { 2149 thflags &= ~TH_SYN; 2150 th->th_seq++; 2151 if (th->th_urp > 1) 2152 th->th_urp--; 2153 else 2154 thflags &= ~TH_URG; 2155 todrop--; 2156 } 2157 /* 2158 * Following if statement from Stevens, vol. 2, p. 960. 2159 */ 2160 if (todrop > tlen 2161 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2162 /* 2163 * Any valid FIN must be to the left of the window. 2164 * At this point the FIN must be a duplicate or out 2165 * of sequence; drop it. 2166 */ 2167 thflags &= ~TH_FIN; 2168 2169 /* 2170 * Send an ACK to resynchronize and drop any data. 2171 * But keep on processing for RST or ACK. 2172 */ 2173 tp->t_flags |= TF_ACKNOW; 2174 todrop = tlen; 2175 TCPSTAT_INC(tcps_rcvduppack); 2176 TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2177 } else { 2178 TCPSTAT_INC(tcps_rcvpartduppack); 2179 TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2180 } 2181 drop_hdrlen += todrop; /* drop from the top afterwards */ 2182 th->th_seq += todrop; 2183 tlen -= todrop; 2184 if (th->th_urp > todrop) 2185 th->th_urp -= todrop; 2186 else { 2187 thflags &= ~TH_URG; 2188 th->th_urp = 0; 2189 } 2190 } 2191 2192 /* 2193 * If new data are received on a connection after the 2194 * user processes are gone, then RST the other end. 2195 */ 2196 if ((so->so_state & SS_NOFDREF) && 2197 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2198 char *s; 2199 2200 KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 2201 "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2202 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2203 2204 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 2205 log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 2206 "was closed, sending RST and removing tcpcb\n", 2207 s, __func__, tcpstates[tp->t_state], tlen); 2208 free(s, M_TCPLOG); 2209 } 2210 tp = tcp_close(tp); 2211 TCPSTAT_INC(tcps_rcvafterclose); 2212 rstreason = BANDLIM_UNLIMITED; 2213 goto dropwithreset; 2214 } 2215 2216 /* 2217 * If segment ends after window, drop trailing data 2218 * (and PUSH and FIN); if nothing left, just ACK. 2219 */ 2220 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2221 if (todrop > 0) { 2222 TCPSTAT_INC(tcps_rcvpackafterwin); 2223 if (todrop >= tlen) { 2224 TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2225 /* 2226 * If window is closed can only take segments at 2227 * window edge, and have to drop data and PUSH from 2228 * incoming segments. Continue processing, but 2229 * remember to ack. Otherwise, drop segment 2230 * and ack. 2231 */ 2232 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2233 tp->t_flags |= TF_ACKNOW; 2234 TCPSTAT_INC(tcps_rcvwinprobe); 2235 } else 2236 goto dropafterack; 2237 } else 2238 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2239 m_adj(m, -todrop); 2240 tlen -= todrop; 2241 thflags &= ~(TH_PUSH|TH_FIN); 2242 } 2243 2244 /* 2245 * If last ACK falls within this segment's sequence numbers, 2246 * record its timestamp. 2247 * NOTE: 2248 * 1) That the test incorporates suggestions from the latest 2249 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2250 * 2) That updating only on newer timestamps interferes with 2251 * our earlier PAWS tests, so this check should be solely 2252 * predicated on the sequence space of this segment. 2253 * 3) That we modify the segment boundary check to be 2254 * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2255 * instead of RFC1323's 2256 * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2257 * This modified check allows us to overcome RFC1323's 2258 * limitations as described in Stevens TCP/IP Illustrated 2259 * Vol. 2 p.869. In such cases, we can still calculate the 2260 * RTT correctly when RCV.NXT == Last.ACK.Sent. 2261 */ 2262 if ((to.to_flags & TOF_TS) != 0 && 2263 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2264 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2265 ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2266 tp->ts_recent_age = tcp_ts_getticks(); 2267 tp->ts_recent = to.to_tsval; 2268 } 2269 2270 /* 2271 * If a SYN is in the window, then this is an 2272 * error and we send an RST and drop the connection. 2273 */ 2274 if (thflags & TH_SYN) { 2275 KASSERT(ti_locked == TI_WLOCKED, 2276 ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2277 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2278 2279 tp = tcp_drop(tp, ECONNRESET); 2280 rstreason = BANDLIM_UNLIMITED; 2281 goto drop; 2282 } 2283 2284 /* 2285 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2286 * flag is on (half-synchronized state), then queue data for 2287 * later processing; else drop segment and return. 2288 */ 2289 if ((thflags & TH_ACK) == 0) { 2290 if (tp->t_state == TCPS_SYN_RECEIVED || 2291 (tp->t_flags & TF_NEEDSYN)) 2292 goto step6; 2293 else if (tp->t_flags & TF_ACKNOW) 2294 goto dropafterack; 2295 else 2296 goto drop; 2297 } 2298 2299 /* 2300 * Ack processing. 2301 */ 2302 switch (tp->t_state) { 2303 2304 /* 2305 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2306 * ESTABLISHED state and continue processing. 2307 * The ACK was checked above. 2308 */ 2309 case TCPS_SYN_RECEIVED: 2310 2311 TCPSTAT_INC(tcps_connects); 2312 soisconnected(so); 2313 /* Do window scaling? */ 2314 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2315 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2316 tp->rcv_scale = tp->request_r_scale; 2317 tp->snd_wnd = tiwin; 2318 } 2319 /* 2320 * Make transitions: 2321 * SYN-RECEIVED -> ESTABLISHED 2322 * SYN-RECEIVED* -> FIN-WAIT-1 2323 */ 2324 tp->t_starttime = ticks; 2325 if (tp->t_flags & TF_NEEDFIN) { 2326 tp->t_state = TCPS_FIN_WAIT_1; 2327 tp->t_flags &= ~TF_NEEDFIN; 2328 } else { 2329 tp->t_state = TCPS_ESTABLISHED; 2330 cc_conn_init(tp); 2331 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 2332 } 2333 /* 2334 * If segment contains data or ACK, will call tcp_reass() 2335 * later; if not, do so now to pass queued data to user. 2336 */ 2337 if (tlen == 0 && (thflags & TH_FIN) == 0) 2338 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2339 (struct mbuf *)0); 2340 tp->snd_wl1 = th->th_seq - 1; 2341 /* FALLTHROUGH */ 2342 2343 /* 2344 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2345 * ACKs. If the ack is in the range 2346 * tp->snd_una < th->th_ack <= tp->snd_max 2347 * then advance tp->snd_una to th->th_ack and drop 2348 * data from the retransmission queue. If this ACK reflects 2349 * more up to date window information we update our window information. 2350 */ 2351 case TCPS_ESTABLISHED: 2352 case TCPS_FIN_WAIT_1: 2353 case TCPS_FIN_WAIT_2: 2354 case TCPS_CLOSE_WAIT: 2355 case TCPS_CLOSING: 2356 case TCPS_LAST_ACK: 2357 if (SEQ_GT(th->th_ack, tp->snd_max)) { 2358 TCPSTAT_INC(tcps_rcvacktoomuch); 2359 goto dropafterack; 2360 } 2361 if ((tp->t_flags & TF_SACK_PERMIT) && 2362 ((to.to_flags & TOF_SACK) || 2363 !TAILQ_EMPTY(&tp->snd_holes))) 2364 tcp_sack_doack(tp, &to, th->th_ack); 2365 2366 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 2367 hhook_run_tcp_est_in(tp, th, &to); 2368 2369 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2370 if (tlen == 0 && tiwin == tp->snd_wnd) { 2371 TCPSTAT_INC(tcps_rcvdupack); 2372 /* 2373 * If we have outstanding data (other than 2374 * a window probe), this is a completely 2375 * duplicate ack (ie, window info didn't 2376 * change), the ack is the biggest we've 2377 * seen and we've seen exactly our rexmt 2378 * threshhold of them, assume a packet 2379 * has been dropped and retransmit it. 2380 * Kludge snd_nxt & the congestion 2381 * window so we send only this one 2382 * packet. 2383 * 2384 * We know we're losing at the current 2385 * window size so do congestion avoidance 2386 * (set ssthresh to half the current window 2387 * and pull our congestion window back to 2388 * the new ssthresh). 2389 * 2390 * Dup acks mean that packets have left the 2391 * network (they're now cached at the receiver) 2392 * so bump cwnd by the amount in the receiver 2393 * to keep a constant cwnd packets in the 2394 * network. 2395 * 2396 * When using TCP ECN, notify the peer that 2397 * we reduced the cwnd. 2398 */ 2399 if (!tcp_timer_active(tp, TT_REXMT) || 2400 th->th_ack != tp->snd_una) 2401 tp->t_dupacks = 0; 2402 else if (++tp->t_dupacks > tcprexmtthresh || 2403 IN_FASTRECOVERY(tp->t_flags)) { 2404 cc_ack_received(tp, th, CC_DUPACK); 2405 if ((tp->t_flags & TF_SACK_PERMIT) && 2406 IN_FASTRECOVERY(tp->t_flags)) { 2407 int awnd; 2408 2409 /* 2410 * Compute the amount of data in flight first. 2411 * We can inject new data into the pipe iff 2412 * we have less than 1/2 the original window's 2413 * worth of data in flight. 2414 */ 2415 awnd = (tp->snd_nxt - tp->snd_fack) + 2416 tp->sackhint.sack_bytes_rexmit; 2417 if (awnd < tp->snd_ssthresh) { 2418 tp->snd_cwnd += tp->t_maxseg; 2419 if (tp->snd_cwnd > tp->snd_ssthresh) 2420 tp->snd_cwnd = tp->snd_ssthresh; 2421 } 2422 } else 2423 tp->snd_cwnd += tp->t_maxseg; 2424 (void) tcp_output(tp); 2425 goto drop; 2426 } else if (tp->t_dupacks == tcprexmtthresh) { 2427 tcp_seq onxt = tp->snd_nxt; 2428 2429 /* 2430 * If we're doing sack, check to 2431 * see if we're already in sack 2432 * recovery. If we're not doing sack, 2433 * check to see if we're in newreno 2434 * recovery. 2435 */ 2436 if (tp->t_flags & TF_SACK_PERMIT) { 2437 if (IN_FASTRECOVERY(tp->t_flags)) { 2438 tp->t_dupacks = 0; 2439 break; 2440 } 2441 } else { 2442 if (SEQ_LEQ(th->th_ack, 2443 tp->snd_recover)) { 2444 tp->t_dupacks = 0; 2445 break; 2446 } 2447 } 2448 /* Congestion signal before ack. */ 2449 cc_cong_signal(tp, th, CC_NDUPACK); 2450 cc_ack_received(tp, th, CC_DUPACK); 2451 tcp_timer_activate(tp, TT_REXMT, 0); 2452 tp->t_rtttime = 0; 2453 if (tp->t_flags & TF_SACK_PERMIT) { 2454 TCPSTAT_INC( 2455 tcps_sack_recovery_episode); 2456 tp->sack_newdata = tp->snd_nxt; 2457 tp->snd_cwnd = tp->t_maxseg; 2458 (void) tcp_output(tp); 2459 goto drop; 2460 } 2461 tp->snd_nxt = th->th_ack; 2462 tp->snd_cwnd = tp->t_maxseg; 2463 (void) tcp_output(tp); 2464 KASSERT(tp->snd_limited <= 2, 2465 ("%s: tp->snd_limited too big", 2466 __func__)); 2467 tp->snd_cwnd = tp->snd_ssthresh + 2468 tp->t_maxseg * 2469 (tp->t_dupacks - tp->snd_limited); 2470 if (SEQ_GT(onxt, tp->snd_nxt)) 2471 tp->snd_nxt = onxt; 2472 goto drop; 2473 } else if (V_tcp_do_rfc3042) { 2474 cc_ack_received(tp, th, CC_DUPACK); 2475 u_long oldcwnd = tp->snd_cwnd; 2476 tcp_seq oldsndmax = tp->snd_max; 2477 u_int sent; 2478 2479 KASSERT(tp->t_dupacks == 1 || 2480 tp->t_dupacks == 2, 2481 ("%s: dupacks not 1 or 2", 2482 __func__)); 2483 if (tp->t_dupacks == 1) 2484 tp->snd_limited = 0; 2485 tp->snd_cwnd = 2486 (tp->snd_nxt - tp->snd_una) + 2487 (tp->t_dupacks - tp->snd_limited) * 2488 tp->t_maxseg; 2489 (void) tcp_output(tp); 2490 sent = tp->snd_max - oldsndmax; 2491 if (sent > tp->t_maxseg) { 2492 KASSERT((tp->t_dupacks == 2 && 2493 tp->snd_limited == 0) || 2494 (sent == tp->t_maxseg + 1 && 2495 tp->t_flags & TF_SENTFIN), 2496 ("%s: sent too much", 2497 __func__)); 2498 tp->snd_limited = 2; 2499 } else if (sent > 0) 2500 ++tp->snd_limited; 2501 tp->snd_cwnd = oldcwnd; 2502 goto drop; 2503 } 2504 } else 2505 tp->t_dupacks = 0; 2506 break; 2507 } 2508 2509 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2510 ("%s: th_ack <= snd_una", __func__)); 2511 2512 /* 2513 * If the congestion window was inflated to account 2514 * for the other side's cached packets, retract it. 2515 */ 2516 if (IN_FASTRECOVERY(tp->t_flags)) { 2517 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 2518 if (tp->t_flags & TF_SACK_PERMIT) 2519 tcp_sack_partialack(tp, th); 2520 else 2521 tcp_newreno_partial_ack(tp, th); 2522 } else 2523 cc_post_recovery(tp, th); 2524 } 2525 tp->t_dupacks = 0; 2526 /* 2527 * If we reach this point, ACK is not a duplicate, 2528 * i.e., it ACKs something we sent. 2529 */ 2530 if (tp->t_flags & TF_NEEDSYN) { 2531 /* 2532 * T/TCP: Connection was half-synchronized, and our 2533 * SYN has been ACK'd (so connection is now fully 2534 * synchronized). Go to non-starred state, 2535 * increment snd_una for ACK of SYN, and check if 2536 * we can do window scaling. 2537 */ 2538 tp->t_flags &= ~TF_NEEDSYN; 2539 tp->snd_una++; 2540 /* Do window scaling? */ 2541 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2542 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2543 tp->rcv_scale = tp->request_r_scale; 2544 /* Send window already scaled. */ 2545 } 2546 } 2547 2548 process_ACK: 2549 INP_WLOCK_ASSERT(tp->t_inpcb); 2550 2551 acked = BYTES_THIS_ACK(tp, th); 2552 TCPSTAT_INC(tcps_rcvackpack); 2553 TCPSTAT_ADD(tcps_rcvackbyte, acked); 2554 2555 /* 2556 * If we just performed our first retransmit, and the ACK 2557 * arrives within our recovery window, then it was a mistake 2558 * to do the retransmit in the first place. Recover our 2559 * original cwnd and ssthresh, and proceed to transmit where 2560 * we left off. 2561 */ 2562 if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2563 (int)(ticks - tp->t_badrxtwin) < 0) 2564 cc_cong_signal(tp, th, CC_RTO_ERR); 2565 2566 /* 2567 * If we have a timestamp reply, update smoothed 2568 * round trip time. If no timestamp is present but 2569 * transmit timer is running and timed sequence 2570 * number was acked, update smoothed round trip time. 2571 * Since we now have an rtt measurement, cancel the 2572 * timer backoff (cf., Phil Karn's retransmit alg.). 2573 * Recompute the initial retransmit timer. 2574 * 2575 * Some boxes send broken timestamp replies 2576 * during the SYN+ACK phase, ignore 2577 * timestamps of 0 or we could calculate a 2578 * huge RTT and blow up the retransmit timer. 2579 */ 2580 if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 2581 u_int t; 2582 2583 t = tcp_ts_getticks() - to.to_tsecr; 2584 if (!tp->t_rttlow || tp->t_rttlow > t) 2585 tp->t_rttlow = t; 2586 tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2587 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2588 if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2589 tp->t_rttlow = ticks - tp->t_rtttime; 2590 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2591 } 2592 2593 /* 2594 * If all outstanding data is acked, stop retransmit 2595 * timer and remember to restart (more output or persist). 2596 * If there is more data to be acked, restart retransmit 2597 * timer, using current (possibly backed-off) value. 2598 */ 2599 if (th->th_ack == tp->snd_max) { 2600 tcp_timer_activate(tp, TT_REXMT, 0); 2601 needoutput = 1; 2602 } else if (!tcp_timer_active(tp, TT_PERSIST)) 2603 tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2604 2605 /* 2606 * If no data (only SYN) was ACK'd, 2607 * skip rest of ACK processing. 2608 */ 2609 if (acked == 0) 2610 goto step6; 2611 2612 /* 2613 * Let the congestion control algorithm update congestion 2614 * control related information. This typically means increasing 2615 * the congestion window. 2616 */ 2617 cc_ack_received(tp, th, CC_ACK); 2618 2619 SOCKBUF_LOCK(&so->so_snd); 2620 if (acked > so->so_snd.sb_cc) { 2621 tp->snd_wnd -= so->so_snd.sb_cc; 2622 sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2623 ourfinisacked = 1; 2624 } else { 2625 sbdrop_locked(&so->so_snd, acked); 2626 tp->snd_wnd -= acked; 2627 ourfinisacked = 0; 2628 } 2629 /* NB: sowwakeup_locked() does an implicit unlock. */ 2630 sowwakeup_locked(so); 2631 /* Detect una wraparound. */ 2632 if (!IN_RECOVERY(tp->t_flags) && 2633 SEQ_GT(tp->snd_una, tp->snd_recover) && 2634 SEQ_LEQ(th->th_ack, tp->snd_recover)) 2635 tp->snd_recover = th->th_ack - 1; 2636 /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2637 if (IN_RECOVERY(tp->t_flags) && 2638 SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2639 EXIT_RECOVERY(tp->t_flags); 2640 } 2641 tp->snd_una = th->th_ack; 2642 if (tp->t_flags & TF_SACK_PERMIT) { 2643 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 2644 tp->snd_recover = tp->snd_una; 2645 } 2646 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2647 tp->snd_nxt = tp->snd_una; 2648 2649 switch (tp->t_state) { 2650 2651 /* 2652 * In FIN_WAIT_1 STATE in addition to the processing 2653 * for the ESTABLISHED state if our FIN is now acknowledged 2654 * then enter FIN_WAIT_2. 2655 */ 2656 case TCPS_FIN_WAIT_1: 2657 if (ourfinisacked) { 2658 /* 2659 * If we can't receive any more 2660 * data, then closing user can proceed. 2661 * Starting the timer is contrary to the 2662 * specification, but if we don't get a FIN 2663 * we'll hang forever. 2664 * 2665 * XXXjl: 2666 * we should release the tp also, and use a 2667 * compressed state. 2668 */ 2669 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 2670 soisdisconnected(so); 2671 tcp_timer_activate(tp, TT_2MSL, 2672 (tcp_fast_finwait2_recycle ? 2673 tcp_finwait2_timeout : 2674 TP_MAXIDLE(tp))); 2675 } 2676 tp->t_state = TCPS_FIN_WAIT_2; 2677 } 2678 break; 2679 2680 /* 2681 * In CLOSING STATE in addition to the processing for 2682 * the ESTABLISHED state if the ACK acknowledges our FIN 2683 * then enter the TIME-WAIT state, otherwise ignore 2684 * the segment. 2685 */ 2686 case TCPS_CLOSING: 2687 if (ourfinisacked) { 2688 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2689 tcp_twstart(tp); 2690 INP_INFO_WUNLOCK(&V_tcbinfo); 2691 m_freem(m); 2692 return; 2693 } 2694 break; 2695 2696 /* 2697 * In LAST_ACK, we may still be waiting for data to drain 2698 * and/or to be acked, as well as for the ack of our FIN. 2699 * If our FIN is now acknowledged, delete the TCB, 2700 * enter the closed state and return. 2701 */ 2702 case TCPS_LAST_ACK: 2703 if (ourfinisacked) { 2704 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2705 tp = tcp_close(tp); 2706 goto drop; 2707 } 2708 break; 2709 } 2710 } 2711 2712 step6: 2713 INP_WLOCK_ASSERT(tp->t_inpcb); 2714 2715 /* 2716 * Update window information. 2717 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2718 */ 2719 if ((thflags & TH_ACK) && 2720 (SEQ_LT(tp->snd_wl1, th->th_seq) || 2721 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2722 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2723 /* keep track of pure window updates */ 2724 if (tlen == 0 && 2725 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2726 TCPSTAT_INC(tcps_rcvwinupd); 2727 tp->snd_wnd = tiwin; 2728 tp->snd_wl1 = th->th_seq; 2729 tp->snd_wl2 = th->th_ack; 2730 if (tp->snd_wnd > tp->max_sndwnd) 2731 tp->max_sndwnd = tp->snd_wnd; 2732 needoutput = 1; 2733 } 2734 2735 /* 2736 * Process segments with URG. 2737 */ 2738 if ((thflags & TH_URG) && th->th_urp && 2739 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2740 /* 2741 * This is a kludge, but if we receive and accept 2742 * random urgent pointers, we'll crash in 2743 * soreceive. It's hard to imagine someone 2744 * actually wanting to send this much urgent data. 2745 */ 2746 SOCKBUF_LOCK(&so->so_rcv); 2747 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2748 th->th_urp = 0; /* XXX */ 2749 thflags &= ~TH_URG; /* XXX */ 2750 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2751 goto dodata; /* XXX */ 2752 } 2753 /* 2754 * If this segment advances the known urgent pointer, 2755 * then mark the data stream. This should not happen 2756 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2757 * a FIN has been received from the remote side. 2758 * In these states we ignore the URG. 2759 * 2760 * According to RFC961 (Assigned Protocols), 2761 * the urgent pointer points to the last octet 2762 * of urgent data. We continue, however, 2763 * to consider it to indicate the first octet 2764 * of data past the urgent section as the original 2765 * spec states (in one of two places). 2766 */ 2767 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2768 tp->rcv_up = th->th_seq + th->th_urp; 2769 so->so_oobmark = so->so_rcv.sb_cc + 2770 (tp->rcv_up - tp->rcv_nxt) - 1; 2771 if (so->so_oobmark == 0) 2772 so->so_rcv.sb_state |= SBS_RCVATMARK; 2773 sohasoutofband(so); 2774 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2775 } 2776 SOCKBUF_UNLOCK(&so->so_rcv); 2777 /* 2778 * Remove out of band data so doesn't get presented to user. 2779 * This can happen independent of advancing the URG pointer, 2780 * but if two URG's are pending at once, some out-of-band 2781 * data may creep in... ick. 2782 */ 2783 if (th->th_urp <= (u_long)tlen && 2784 !(so->so_options & SO_OOBINLINE)) { 2785 /* hdr drop is delayed */ 2786 tcp_pulloutofband(so, th, m, drop_hdrlen); 2787 } 2788 } else { 2789 /* 2790 * If no out of band data is expected, 2791 * pull receive urgent pointer along 2792 * with the receive window. 2793 */ 2794 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2795 tp->rcv_up = tp->rcv_nxt; 2796 } 2797 dodata: /* XXX */ 2798 INP_WLOCK_ASSERT(tp->t_inpcb); 2799 2800 /* 2801 * Process the segment text, merging it into the TCP sequencing queue, 2802 * and arranging for acknowledgment of receipt if necessary. 2803 * This process logically involves adjusting tp->rcv_wnd as data 2804 * is presented to the user (this happens in tcp_usrreq.c, 2805 * case PRU_RCVD). If a FIN has already been received on this 2806 * connection then we just ignore the text. 2807 */ 2808 if ((tlen || (thflags & TH_FIN)) && 2809 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2810 tcp_seq save_start = th->th_seq; 2811 m_adj(m, drop_hdrlen); /* delayed header drop */ 2812 /* 2813 * Insert segment which includes th into TCP reassembly queue 2814 * with control block tp. Set thflags to whether reassembly now 2815 * includes a segment with FIN. This handles the common case 2816 * inline (segment is the next to be received on an established 2817 * connection, and the queue is empty), avoiding linkage into 2818 * and removal from the queue and repetition of various 2819 * conversions. 2820 * Set DELACK for segments received in order, but ack 2821 * immediately when segments are out of order (so 2822 * fast retransmit can work). 2823 */ 2824 if (th->th_seq == tp->rcv_nxt && 2825 LIST_EMPTY(&tp->t_segq) && 2826 TCPS_HAVEESTABLISHED(tp->t_state)) { 2827 if (DELAY_ACK(tp)) 2828 tp->t_flags |= TF_DELACK; 2829 else 2830 tp->t_flags |= TF_ACKNOW; 2831 tp->rcv_nxt += tlen; 2832 thflags = th->th_flags & TH_FIN; 2833 TCPSTAT_INC(tcps_rcvpack); 2834 TCPSTAT_ADD(tcps_rcvbyte, tlen); 2835 ND6_HINT(tp); 2836 SOCKBUF_LOCK(&so->so_rcv); 2837 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2838 m_freem(m); 2839 else 2840 sbappendstream_locked(&so->so_rcv, m); 2841 /* NB: sorwakeup_locked() does an implicit unlock. */ 2842 sorwakeup_locked(so); 2843 } else { 2844 /* 2845 * XXX: Due to the header drop above "th" is 2846 * theoretically invalid by now. Fortunately 2847 * m_adj() doesn't actually frees any mbufs 2848 * when trimming from the head. 2849 */ 2850 thflags = tcp_reass(tp, th, &tlen, m); 2851 tp->t_flags |= TF_ACKNOW; 2852 } 2853 if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2854 tcp_update_sack_list(tp, save_start, save_start + tlen); 2855 #if 0 2856 /* 2857 * Note the amount of data that peer has sent into 2858 * our window, in order to estimate the sender's 2859 * buffer size. 2860 * XXX: Unused. 2861 */ 2862 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 2863 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2864 else 2865 len = so->so_rcv.sb_hiwat; 2866 #endif 2867 } else { 2868 m_freem(m); 2869 thflags &= ~TH_FIN; 2870 } 2871 2872 /* 2873 * If FIN is received ACK the FIN and let the user know 2874 * that the connection is closing. 2875 */ 2876 if (thflags & TH_FIN) { 2877 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2878 socantrcvmore(so); 2879 /* 2880 * If connection is half-synchronized 2881 * (ie NEEDSYN flag on) then delay ACK, 2882 * so it may be piggybacked when SYN is sent. 2883 * Otherwise, since we received a FIN then no 2884 * more input can be expected, send ACK now. 2885 */ 2886 if (tp->t_flags & TF_NEEDSYN) 2887 tp->t_flags |= TF_DELACK; 2888 else 2889 tp->t_flags |= TF_ACKNOW; 2890 tp->rcv_nxt++; 2891 } 2892 switch (tp->t_state) { 2893 2894 /* 2895 * In SYN_RECEIVED and ESTABLISHED STATES 2896 * enter the CLOSE_WAIT state. 2897 */ 2898 case TCPS_SYN_RECEIVED: 2899 tp->t_starttime = ticks; 2900 /* FALLTHROUGH */ 2901 case TCPS_ESTABLISHED: 2902 tp->t_state = TCPS_CLOSE_WAIT; 2903 break; 2904 2905 /* 2906 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2907 * enter the CLOSING state. 2908 */ 2909 case TCPS_FIN_WAIT_1: 2910 tp->t_state = TCPS_CLOSING; 2911 break; 2912 2913 /* 2914 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2915 * starting the time-wait timer, turning off the other 2916 * standard timers. 2917 */ 2918 case TCPS_FIN_WAIT_2: 2919 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2920 KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2921 "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2922 ti_locked)); 2923 2924 tcp_twstart(tp); 2925 INP_INFO_WUNLOCK(&V_tcbinfo); 2926 return; 2927 } 2928 } 2929 if (ti_locked == TI_WLOCKED) 2930 INP_INFO_WUNLOCK(&V_tcbinfo); 2931 ti_locked = TI_UNLOCKED; 2932 2933 #ifdef TCPDEBUG 2934 if (so->so_options & SO_DEBUG) 2935 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2936 &tcp_savetcp, 0); 2937 #endif 2938 2939 /* 2940 * Return any desired output. 2941 */ 2942 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2943 (void) tcp_output(tp); 2944 2945 check_delack: 2946 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2947 __func__, ti_locked)); 2948 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2949 INP_WLOCK_ASSERT(tp->t_inpcb); 2950 2951 if (tp->t_flags & TF_DELACK) { 2952 tp->t_flags &= ~TF_DELACK; 2953 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 2954 } 2955 INP_WUNLOCK(tp->t_inpcb); 2956 return; 2957 2958 dropafterack: 2959 /* 2960 * Generate an ACK dropping incoming segment if it occupies 2961 * sequence space, where the ACK reflects our state. 2962 * 2963 * We can now skip the test for the RST flag since all 2964 * paths to this code happen after packets containing 2965 * RST have been dropped. 2966 * 2967 * In the SYN-RECEIVED state, don't send an ACK unless the 2968 * segment we received passes the SYN-RECEIVED ACK test. 2969 * If it fails send a RST. This breaks the loop in the 2970 * "LAND" DoS attack, and also prevents an ACK storm 2971 * between two listening ports that have been sent forged 2972 * SYN segments, each with the source address of the other. 2973 */ 2974 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2975 (SEQ_GT(tp->snd_una, th->th_ack) || 2976 SEQ_GT(th->th_ack, tp->snd_max)) ) { 2977 rstreason = BANDLIM_RST_OPENPORT; 2978 goto dropwithreset; 2979 } 2980 #ifdef TCPDEBUG 2981 if (so->so_options & SO_DEBUG) 2982 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2983 &tcp_savetcp, 0); 2984 #endif 2985 if (ti_locked == TI_WLOCKED) 2986 INP_INFO_WUNLOCK(&V_tcbinfo); 2987 ti_locked = TI_UNLOCKED; 2988 2989 tp->t_flags |= TF_ACKNOW; 2990 (void) tcp_output(tp); 2991 INP_WUNLOCK(tp->t_inpcb); 2992 m_freem(m); 2993 return; 2994 2995 dropwithreset: 2996 if (ti_locked == TI_WLOCKED) 2997 INP_INFO_WUNLOCK(&V_tcbinfo); 2998 ti_locked = TI_UNLOCKED; 2999 3000 if (tp != NULL) { 3001 tcp_dropwithreset(m, th, tp, tlen, rstreason); 3002 INP_WUNLOCK(tp->t_inpcb); 3003 } else 3004 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3005 return; 3006 3007 drop: 3008 if (ti_locked == TI_WLOCKED) { 3009 INP_INFO_WUNLOCK(&V_tcbinfo); 3010 ti_locked = TI_UNLOCKED; 3011 } 3012 #ifdef INVARIANTS 3013 else 3014 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3015 #endif 3016 3017 /* 3018 * Drop space held by incoming segment and return. 3019 */ 3020 #ifdef TCPDEBUG 3021 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3022 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3023 &tcp_savetcp, 0); 3024 #endif 3025 if (tp != NULL) 3026 INP_WUNLOCK(tp->t_inpcb); 3027 m_freem(m); 3028 } 3029 3030 /* 3031 * Issue RST and make ACK acceptable to originator of segment. 3032 * The mbuf must still include the original packet header. 3033 * tp may be NULL. 3034 */ 3035 static void 3036 tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3037 int tlen, int rstreason) 3038 { 3039 #ifdef INET 3040 struct ip *ip; 3041 #endif 3042 #ifdef INET6 3043 struct ip6_hdr *ip6; 3044 #endif 3045 3046 if (tp != NULL) { 3047 INP_WLOCK_ASSERT(tp->t_inpcb); 3048 } 3049 3050 /* Don't bother if destination was broadcast/multicast. */ 3051 if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3052 goto drop; 3053 #ifdef INET6 3054 if (mtod(m, struct ip *)->ip_v == 6) { 3055 ip6 = mtod(m, struct ip6_hdr *); 3056 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3057 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3058 goto drop; 3059 /* IPv6 anycast check is done at tcp6_input() */ 3060 } 3061 #endif 3062 #if defined(INET) && defined(INET6) 3063 else 3064 #endif 3065 #ifdef INET 3066 { 3067 ip = mtod(m, struct ip *); 3068 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3069 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3070 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3071 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3072 goto drop; 3073 } 3074 #endif 3075 3076 /* Perform bandwidth limiting. */ 3077 if (badport_bandlim(rstreason) < 0) 3078 goto drop; 3079 3080 /* tcp_respond consumes the mbuf chain. */ 3081 if (th->th_flags & TH_ACK) { 3082 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3083 th->th_ack, TH_RST); 3084 } else { 3085 if (th->th_flags & TH_SYN) 3086 tlen++; 3087 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3088 (tcp_seq)0, TH_RST|TH_ACK); 3089 } 3090 return; 3091 drop: 3092 m_freem(m); 3093 } 3094 3095 /* 3096 * Parse TCP options and place in tcpopt. 3097 */ 3098 static void 3099 tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3100 { 3101 int opt, optlen; 3102 3103 to->to_flags = 0; 3104 for (; cnt > 0; cnt -= optlen, cp += optlen) { 3105 opt = cp[0]; 3106 if (opt == TCPOPT_EOL) 3107 break; 3108 if (opt == TCPOPT_NOP) 3109 optlen = 1; 3110 else { 3111 if (cnt < 2) 3112 break; 3113 optlen = cp[1]; 3114 if (optlen < 2 || optlen > cnt) 3115 break; 3116 } 3117 switch (opt) { 3118 case TCPOPT_MAXSEG: 3119 if (optlen != TCPOLEN_MAXSEG) 3120 continue; 3121 if (!(flags & TO_SYN)) 3122 continue; 3123 to->to_flags |= TOF_MSS; 3124 bcopy((char *)cp + 2, 3125 (char *)&to->to_mss, sizeof(to->to_mss)); 3126 to->to_mss = ntohs(to->to_mss); 3127 break; 3128 case TCPOPT_WINDOW: 3129 if (optlen != TCPOLEN_WINDOW) 3130 continue; 3131 if (!(flags & TO_SYN)) 3132 continue; 3133 to->to_flags |= TOF_SCALE; 3134 to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3135 break; 3136 case TCPOPT_TIMESTAMP: 3137 if (optlen != TCPOLEN_TIMESTAMP) 3138 continue; 3139 to->to_flags |= TOF_TS; 3140 bcopy((char *)cp + 2, 3141 (char *)&to->to_tsval, sizeof(to->to_tsval)); 3142 to->to_tsval = ntohl(to->to_tsval); 3143 bcopy((char *)cp + 6, 3144 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3145 to->to_tsecr = ntohl(to->to_tsecr); 3146 break; 3147 #ifdef TCP_SIGNATURE 3148 /* 3149 * XXX In order to reply to a host which has set the 3150 * TCP_SIGNATURE option in its initial SYN, we have to 3151 * record the fact that the option was observed here 3152 * for the syncache code to perform the correct response. 3153 */ 3154 case TCPOPT_SIGNATURE: 3155 if (optlen != TCPOLEN_SIGNATURE) 3156 continue; 3157 to->to_flags |= TOF_SIGNATURE; 3158 to->to_signature = cp + 2; 3159 break; 3160 #endif 3161 case TCPOPT_SACK_PERMITTED: 3162 if (optlen != TCPOLEN_SACK_PERMITTED) 3163 continue; 3164 if (!(flags & TO_SYN)) 3165 continue; 3166 if (!V_tcp_do_sack) 3167 continue; 3168 to->to_flags |= TOF_SACKPERM; 3169 break; 3170 case TCPOPT_SACK: 3171 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 3172 continue; 3173 if (flags & TO_SYN) 3174 continue; 3175 to->to_flags |= TOF_SACK; 3176 to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 3177 to->to_sacks = cp + 2; 3178 TCPSTAT_INC(tcps_sack_rcv_blocks); 3179 break; 3180 default: 3181 continue; 3182 } 3183 } 3184 } 3185 3186 /* 3187 * Pull out of band byte out of a segment so 3188 * it doesn't appear in the user's data queue. 3189 * It is still reflected in the segment length for 3190 * sequencing purposes. 3191 */ 3192 static void 3193 tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3194 int off) 3195 { 3196 int cnt = off + th->th_urp - 1; 3197 3198 while (cnt >= 0) { 3199 if (m->m_len > cnt) { 3200 char *cp = mtod(m, caddr_t) + cnt; 3201 struct tcpcb *tp = sototcpcb(so); 3202 3203 INP_WLOCK_ASSERT(tp->t_inpcb); 3204 3205 tp->t_iobc = *cp; 3206 tp->t_oobflags |= TCPOOB_HAVEDATA; 3207 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3208 m->m_len--; 3209 if (m->m_flags & M_PKTHDR) 3210 m->m_pkthdr.len--; 3211 return; 3212 } 3213 cnt -= m->m_len; 3214 m = m->m_next; 3215 if (m == NULL) 3216 break; 3217 } 3218 panic("tcp_pulloutofband"); 3219 } 3220 3221 /* 3222 * Collect new round-trip time estimate 3223 * and update averages and current timeout. 3224 */ 3225 static void 3226 tcp_xmit_timer(struct tcpcb *tp, int rtt) 3227 { 3228 int delta; 3229 3230 INP_WLOCK_ASSERT(tp->t_inpcb); 3231 3232 TCPSTAT_INC(tcps_rttupdated); 3233 tp->t_rttupdated++; 3234 if (tp->t_srtt != 0) { 3235 /* 3236 * srtt is stored as fixed point with 5 bits after the 3237 * binary point (i.e., scaled by 8). The following magic 3238 * is equivalent to the smoothing algorithm in rfc793 with 3239 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3240 * point). Adjust rtt to origin 0. 3241 */ 3242 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3243 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3244 3245 if ((tp->t_srtt += delta) <= 0) 3246 tp->t_srtt = 1; 3247 3248 /* 3249 * We accumulate a smoothed rtt variance (actually, a 3250 * smoothed mean difference), then set the retransmit 3251 * timer to smoothed rtt + 4 times the smoothed variance. 3252 * rttvar is stored as fixed point with 4 bits after the 3253 * binary point (scaled by 16). The following is 3254 * equivalent to rfc793 smoothing with an alpha of .75 3255 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3256 * rfc793's wired-in beta. 3257 */ 3258 if (delta < 0) 3259 delta = -delta; 3260 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3261 if ((tp->t_rttvar += delta) <= 0) 3262 tp->t_rttvar = 1; 3263 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 3264 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3265 } else { 3266 /* 3267 * No rtt measurement yet - use the unsmoothed rtt. 3268 * Set the variance to half the rtt (so our first 3269 * retransmit happens at 3*rtt). 3270 */ 3271 tp->t_srtt = rtt << TCP_RTT_SHIFT; 3272 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 3273 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3274 } 3275 tp->t_rtttime = 0; 3276 tp->t_rxtshift = 0; 3277 3278 /* 3279 * the retransmit should happen at rtt + 4 * rttvar. 3280 * Because of the way we do the smoothing, srtt and rttvar 3281 * will each average +1/2 tick of bias. When we compute 3282 * the retransmit timer, we want 1/2 tick of rounding and 3283 * 1 extra tick because of +-1/2 tick uncertainty in the 3284 * firing of the timer. The bias will give us exactly the 3285 * 1.5 tick we need. But, because the bias is 3286 * statistical, we have to test that we don't drop below 3287 * the minimum feasible timer (which is 2 ticks). 3288 */ 3289 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 3290 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3291 3292 /* 3293 * We received an ack for a packet that wasn't retransmitted; 3294 * it is probably safe to discard any error indications we've 3295 * received recently. This isn't quite right, but close enough 3296 * for now (a route might have failed after we sent a segment, 3297 * and the return path might not be symmetrical). 3298 */ 3299 tp->t_softerror = 0; 3300 } 3301 3302 /* 3303 * Determine a reasonable value for maxseg size. 3304 * If the route is known, check route for mtu. 3305 * If none, use an mss that can be handled on the outgoing 3306 * interface without forcing IP to fragment; if bigger than 3307 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3308 * to utilize large mbufs. If no route is found, route has no mtu, 3309 * or the destination isn't local, use a default, hopefully conservative 3310 * size (usually 512 or the default IP max size, but no more than the mtu 3311 * of the interface), as we can't discover anything about intervening 3312 * gateways or networks. We also initialize the congestion/slow start 3313 * window to be a single segment if the destination isn't local. 3314 * While looking at the routing entry, we also initialize other path-dependent 3315 * parameters from pre-set or cached values in the routing entry. 3316 * 3317 * Also take into account the space needed for options that we 3318 * send regularly. Make maxseg shorter by that amount to assure 3319 * that we can send maxseg amount of data even when the options 3320 * are present. Store the upper limit of the length of options plus 3321 * data in maxopd. 3322 * 3323 * NOTE that this routine is only called when we process an incoming 3324 * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3325 * settings are handled in tcp_mssopt(). 3326 */ 3327 void 3328 tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 3329 struct hc_metrics_lite *metricptr, int *mtuflags) 3330 { 3331 int mss = 0; 3332 u_long maxmtu = 0; 3333 struct inpcb *inp = tp->t_inpcb; 3334 struct hc_metrics_lite metrics; 3335 int origoffer; 3336 #ifdef INET6 3337 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3338 size_t min_protoh = isipv6 ? 3339 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3340 sizeof (struct tcpiphdr); 3341 #else 3342 const size_t min_protoh = sizeof(struct tcpiphdr); 3343 #endif 3344 3345 INP_WLOCK_ASSERT(tp->t_inpcb); 3346 3347 if (mtuoffer != -1) { 3348 KASSERT(offer == -1, ("%s: conflict", __func__)); 3349 offer = mtuoffer - min_protoh; 3350 } 3351 origoffer = offer; 3352 3353 /* Initialize. */ 3354 #ifdef INET6 3355 if (isipv6) { 3356 maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3357 tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 3358 } 3359 #endif 3360 #if defined(INET) && defined(INET6) 3361 else 3362 #endif 3363 #ifdef INET 3364 { 3365 maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3366 tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3367 } 3368 #endif 3369 3370 /* 3371 * No route to sender, stay with default mss and return. 3372 */ 3373 if (maxmtu == 0) { 3374 /* 3375 * In case we return early we need to initialize metrics 3376 * to a defined state as tcp_hc_get() would do for us 3377 * if there was no cache hit. 3378 */ 3379 if (metricptr != NULL) 3380 bzero(metricptr, sizeof(struct hc_metrics_lite)); 3381 return; 3382 } 3383 3384 /* What have we got? */ 3385 switch (offer) { 3386 case 0: 3387 /* 3388 * Offer == 0 means that there was no MSS on the SYN 3389 * segment, in this case we use tcp_mssdflt as 3390 * already assigned to t_maxopd above. 3391 */ 3392 offer = tp->t_maxopd; 3393 break; 3394 3395 case -1: 3396 /* 3397 * Offer == -1 means that we didn't receive SYN yet. 3398 */ 3399 /* FALLTHROUGH */ 3400 3401 default: 3402 /* 3403 * Prevent DoS attack with too small MSS. Round up 3404 * to at least minmss. 3405 */ 3406 offer = max(offer, V_tcp_minmss); 3407 } 3408 3409 /* 3410 * rmx information is now retrieved from tcp_hostcache. 3411 */ 3412 tcp_hc_get(&inp->inp_inc, &metrics); 3413 if (metricptr != NULL) 3414 bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 3415 3416 /* 3417 * If there's a discovered mtu int tcp hostcache, use it 3418 * else, use the link mtu. 3419 */ 3420 if (metrics.rmx_mtu) 3421 mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3422 else { 3423 #ifdef INET6 3424 if (isipv6) { 3425 mss = maxmtu - min_protoh; 3426 if (!V_path_mtu_discovery && 3427 !in6_localaddr(&inp->in6p_faddr)) 3428 mss = min(mss, V_tcp_v6mssdflt); 3429 } 3430 #endif 3431 #if defined(INET) && defined(INET6) 3432 else 3433 #endif 3434 #ifdef INET 3435 { 3436 mss = maxmtu - min_protoh; 3437 if (!V_path_mtu_discovery && 3438 !in_localaddr(inp->inp_faddr)) 3439 mss = min(mss, V_tcp_mssdflt); 3440 } 3441 #endif 3442 /* 3443 * XXX - The above conditional (mss = maxmtu - min_protoh) 3444 * probably violates the TCP spec. 3445 * The problem is that, since we don't know the 3446 * other end's MSS, we are supposed to use a conservative 3447 * default. But, if we do that, then MTU discovery will 3448 * never actually take place, because the conservative 3449 * default is much less than the MTUs typically seen 3450 * on the Internet today. For the moment, we'll sweep 3451 * this under the carpet. 3452 * 3453 * The conservative default might not actually be a problem 3454 * if the only case this occurs is when sending an initial 3455 * SYN with options and data to a host we've never talked 3456 * to before. Then, they will reply with an MSS value which 3457 * will get recorded and the new parameters should get 3458 * recomputed. For Further Study. 3459 */ 3460 } 3461 mss = min(mss, offer); 3462 3463 /* 3464 * Sanity check: make sure that maxopd will be large 3465 * enough to allow some data on segments even if the 3466 * all the option space is used (40bytes). Otherwise 3467 * funny things may happen in tcp_output. 3468 */ 3469 mss = max(mss, 64); 3470 3471 /* 3472 * maxopd stores the maximum length of data AND options 3473 * in a segment; maxseg is the amount of data in a normal 3474 * segment. We need to store this value (maxopd) apart 3475 * from maxseg, because now every segment carries options 3476 * and thus we normally have somewhat less data in segments. 3477 */ 3478 tp->t_maxopd = mss; 3479 3480 /* 3481 * origoffer==-1 indicates that no segments were received yet. 3482 * In this case we just guess. 3483 */ 3484 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3485 (origoffer == -1 || 3486 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3487 mss -= TCPOLEN_TSTAMP_APPA; 3488 3489 #if (MCLBYTES & (MCLBYTES - 1)) == 0 3490 if (mss > MCLBYTES) 3491 mss &= ~(MCLBYTES-1); 3492 #else 3493 if (mss > MCLBYTES) 3494 mss = mss / MCLBYTES * MCLBYTES; 3495 #endif 3496 tp->t_maxseg = mss; 3497 } 3498 3499 void 3500 tcp_mss(struct tcpcb *tp, int offer) 3501 { 3502 int mss; 3503 u_long bufsize; 3504 struct inpcb *inp; 3505 struct socket *so; 3506 struct hc_metrics_lite metrics; 3507 int mtuflags = 0; 3508 3509 KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 3510 3511 tcp_mss_update(tp, offer, -1, &metrics, &mtuflags); 3512 3513 mss = tp->t_maxseg; 3514 inp = tp->t_inpcb; 3515 3516 /* 3517 * If there's a pipesize, change the socket buffer to that size, 3518 * don't change if sb_hiwat is different than default (then it 3519 * has been changed on purpose with setsockopt). 3520 * Make the socket buffers an integral number of mss units; 3521 * if the mss is larger than the socket buffer, decrease the mss. 3522 */ 3523 so = inp->inp_socket; 3524 SOCKBUF_LOCK(&so->so_snd); 3525 if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 3526 bufsize = metrics.rmx_sendpipe; 3527 else 3528 bufsize = so->so_snd.sb_hiwat; 3529 if (bufsize < mss) 3530 mss = bufsize; 3531 else { 3532 bufsize = roundup(bufsize, mss); 3533 if (bufsize > sb_max) 3534 bufsize = sb_max; 3535 if (bufsize > so->so_snd.sb_hiwat) 3536 (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3537 } 3538 SOCKBUF_UNLOCK(&so->so_snd); 3539 tp->t_maxseg = mss; 3540 3541 SOCKBUF_LOCK(&so->so_rcv); 3542 if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 3543 bufsize = metrics.rmx_recvpipe; 3544 else 3545 bufsize = so->so_rcv.sb_hiwat; 3546 if (bufsize > mss) { 3547 bufsize = roundup(bufsize, mss); 3548 if (bufsize > sb_max) 3549 bufsize = sb_max; 3550 if (bufsize > so->so_rcv.sb_hiwat) 3551 (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3552 } 3553 SOCKBUF_UNLOCK(&so->so_rcv); 3554 3555 /* Check the interface for TSO capabilities. */ 3556 if (mtuflags & CSUM_TSO) 3557 tp->t_flags |= TF_TSO; 3558 } 3559 3560 /* 3561 * Determine the MSS option to send on an outgoing SYN. 3562 */ 3563 int 3564 tcp_mssopt(struct in_conninfo *inc) 3565 { 3566 int mss = 0; 3567 u_long maxmtu = 0; 3568 u_long thcmtu = 0; 3569 size_t min_protoh; 3570 3571 KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3572 3573 #ifdef INET6 3574 if (inc->inc_flags & INC_ISIPV6) { 3575 mss = V_tcp_v6mssdflt; 3576 maxmtu = tcp_maxmtu6(inc, NULL); 3577 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3578 } 3579 #endif 3580 #if defined(INET) && defined(INET6) 3581 else 3582 #endif 3583 #ifdef INET 3584 { 3585 mss = V_tcp_mssdflt; 3586 maxmtu = tcp_maxmtu(inc, NULL); 3587 min_protoh = sizeof(struct tcpiphdr); 3588 } 3589 #endif 3590 #if defined(INET6) || defined(INET) 3591 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 3592 #endif 3593 3594 if (maxmtu && thcmtu) 3595 mss = min(maxmtu, thcmtu) - min_protoh; 3596 else if (maxmtu || thcmtu) 3597 mss = max(maxmtu, thcmtu) - min_protoh; 3598 3599 return (mss); 3600 } 3601 3602 3603 /* 3604 * On a partial ack arrives, force the retransmission of the 3605 * next unacknowledged segment. Do not clear tp->t_dupacks. 3606 * By setting snd_nxt to ti_ack, this forces retransmission timer to 3607 * be started again. 3608 */ 3609 static void 3610 tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 3611 { 3612 tcp_seq onxt = tp->snd_nxt; 3613 u_long ocwnd = tp->snd_cwnd; 3614 3615 INP_WLOCK_ASSERT(tp->t_inpcb); 3616 3617 tcp_timer_activate(tp, TT_REXMT, 0); 3618 tp->t_rtttime = 0; 3619 tp->snd_nxt = th->th_ack; 3620 /* 3621 * Set snd_cwnd to one segment beyond acknowledged offset. 3622 * (tp->snd_una has not yet been updated when this function is called.) 3623 */ 3624 tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3625 tp->t_flags |= TF_ACKNOW; 3626 (void) tcp_output(tp); 3627 tp->snd_cwnd = ocwnd; 3628 if (SEQ_GT(onxt, tp->snd_nxt)) 3629 tp->snd_nxt = onxt; 3630 /* 3631 * Partial window deflation. Relies on fact that tp->snd_una 3632 * not updated yet. 3633 */ 3634 if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3635 tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3636 else 3637 tp->snd_cwnd = 0; 3638 tp->snd_cwnd += tp->t_maxseg; 3639 } 3640