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