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