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