tcp_input.c revision 361436
155714Skris/*- 255714Skris * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 355714Skris * The Regents of the University of California. All rights reserved. 455714Skris * Copyright (c) 2007-2008,2010 555714Skris * Swinburne University of Technology, Melbourne, Australia. 655714Skris * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 755714Skris * Copyright (c) 2010 The FreeBSD Foundation 8296341Sdelphij * Copyright (c) 2010-2011 Juniper Networks, Inc. 955714Skris * All rights reserved. 1055714Skris * 1155714Skris * Portions of this software were developed at the Centre for Advanced Internet 1255714Skris * Architectures, Swinburne University of Technology, by Lawrence Stewart, 1355714Skris * James Healy and David Hayes, made possible in part by a grant from the Cisco 1455714Skris * University Research Program Fund at Community Foundation Silicon Valley. 15296341Sdelphij * 1655714Skris * Portions of this software were developed at the Centre for Advanced 1755714Skris * Internet Architectures, Swinburne University of Technology, Melbourne, 1855714Skris * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 1955714Skris * 2055714Skris * Portions of this software were developed by Robert N. M. Watson under 2155714Skris * contract to Juniper Networks, Inc. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the above copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 4. Neither the name of the University nor the names of its contributors 3255714Skris * may be used to endorse or promote products derived from this software 3355714Skris * without specific prior written permission. 3455714Skris * 3555714Skris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 3655714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37296341Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3855714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3955714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 40296341Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4155714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4255714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4355714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 4455714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 4555714Skris * SUCH DAMAGE. 4655714Skris * 4755714Skris * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 4855714Skris */ 4955714Skris 5055714Skris#include <sys/cdefs.h> 5155714Skris__FBSDID("$FreeBSD: stable/11/sys/netinet/tcp_input.c 361436 2020-05-24 17:51:14Z rscheff $"); 52296341Sdelphij 5355714Skris#include "opt_inet.h" 5455714Skris#include "opt_inet6.h" 5555714Skris#include "opt_ipsec.h" 5655714Skris#include "opt_tcpdebug.h" 5755714Skris 5855714Skris#include <sys/param.h> 5955714Skris#include <sys/kernel.h> 60296341Sdelphij#include <sys/hhook.h> 6155714Skris#include <sys/malloc.h> 62296341Sdelphij#include <sys/mbuf.h> 63160814Ssimon#include <sys/proc.h> /* for proc0 declaration */ 64296341Sdelphij#include <sys/protosw.h> 65296341Sdelphij#include <sys/sdt.h> 66296341Sdelphij#include <sys/signalvar.h> 67296341Sdelphij#include <sys/socket.h> 6868651Skris#include <sys/socketvar.h> 69296341Sdelphij#include <sys/sysctl.h> 7068651Skris#include <sys/syslog.h> 71296341Sdelphij#include <sys/systm.h> 72296341Sdelphij 73296341Sdelphij#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 74296341Sdelphij 75296341Sdelphij#include <vm/uma.h> 76296341Sdelphij 77238405Sjkim#include <net/if.h> 78238405Sjkim#include <net/if_var.h> 7955714Skris#include <net/route.h> 8055714Skris#include <net/vnet.h> 8155714Skris 8255714Skris#define TCPSTATES /* for logging */ 8355714Skris 84296341Sdelphij#include <netinet/in.h> 85296341Sdelphij#include <netinet/in_kdtrace.h> 86296341Sdelphij#include <netinet/in_pcb.h> 8755714Skris#include <netinet/in_systm.h> 88296341Sdelphij#include <netinet/ip.h> 89296341Sdelphij#include <netinet/ip_icmp.h> /* required for icmp_var.h */ 90296341Sdelphij#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 91296341Sdelphij#include <netinet/ip_var.h> 92296341Sdelphij#include <netinet/ip_options.h> 93296341Sdelphij#include <netinet/ip6.h> 94296341Sdelphij#include <netinet/icmp6.h> 95296341Sdelphij#include <netinet6/in6_pcb.h> 96296341Sdelphij#include <netinet6/in6_var.h> 97296341Sdelphij#include <netinet6/ip6_var.h> 98296341Sdelphij#include <netinet6/nd6.h> 99296341Sdelphij#ifdef TCP_RFC7413 100296341Sdelphij#include <netinet/tcp_fastopen.h> 101296341Sdelphij#endif 102296341Sdelphij#include <netinet/tcp.h> 103296341Sdelphij#include <netinet/tcp_fsm.h> 104296341Sdelphij#include <netinet/tcp_seq.h> 105296341Sdelphij#include <netinet/tcp_timer.h> 106296341Sdelphij#include <netinet/tcp_var.h> 107296341Sdelphij#include <netinet6/tcp6_var.h> 108296341Sdelphij#include <netinet/tcpip.h> 109296341Sdelphij#include <netinet/cc/cc.h> 110296341Sdelphij#ifdef TCPPCAP 11155714Skris#include <netinet/tcp_pcap.h> 112296341Sdelphij#endif 113296341Sdelphij#include <netinet/tcp_syncache.h> 114296341Sdelphij#ifdef TCPDEBUG 11555714Skris#include <netinet/tcp_debug.h> 116296341Sdelphij#endif /* TCPDEBUG */ 117296341Sdelphij#ifdef TCP_OFFLOAD 118296341Sdelphij#include <netinet/tcp_offload.h> 119296341Sdelphij#endif 120296341Sdelphij 121296341Sdelphij#include <netipsec/ipsec_support.h> 12255714Skris 123296341Sdelphij#include <machine/in_cksum.h> 124296341Sdelphij 125296341Sdelphij#include <security/mac/mac_framework.h> 126296341Sdelphij 127296341Sdelphijconst int tcprexmtthresh = 3; 128296341Sdelphij 129296341Sdelphijint tcp_log_in_vain = 0; 130296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 131296341Sdelphij &tcp_log_in_vain, 0, 132296341Sdelphij "Log all incoming TCP segments to closed ports"); 133296341Sdelphij 134296341SdelphijVNET_DEFINE(int, blackhole) = 0; 135296341Sdelphij#define V_blackhole VNET(blackhole) 136296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 137296341Sdelphij &VNET_NAME(blackhole), 0, 138296341Sdelphij "Do not send RST on segments to closed ports"); 13955714Skris 140296341SdelphijVNET_DEFINE(int, tcp_delack_enabled) = 1; 141296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 14255714Skris &VNET_NAME(tcp_delack_enabled), 0, 143296341Sdelphij "Delay ACK to try and piggyback it onto a data packet"); 14455714Skris 145160814SsimonVNET_DEFINE(int, drop_synfin) = 0; 146296341Sdelphij#define V_drop_synfin VNET(drop_synfin) 147296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 148296341Sdelphij &VNET_NAME(drop_synfin), 0, 149296341Sdelphij "Drop TCP packets with SYN+FIN set"); 150296341Sdelphij 151296341SdelphijVNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0; 152296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW, 153160814Ssimon &VNET_NAME(tcp_do_rfc6675_pipe), 0, 154296341Sdelphij "Use calculated pipe/in-flight bytes per RFC 6675"); 155296341Sdelphij 156296341SdelphijVNET_DEFINE(int, tcp_do_rfc3042) = 1; 157160814Ssimon#define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 158296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 159160814Ssimon &VNET_NAME(tcp_do_rfc3042), 0, 160160814Ssimon "Enable RFC 3042 (Limited Transmit)"); 161296341Sdelphij 162296341SdelphijVNET_DEFINE(int, tcp_do_rfc3390) = 1; 163296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 164296341Sdelphij &VNET_NAME(tcp_do_rfc3390), 0, 165296341Sdelphij "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 166296341Sdelphij 167160814SsimonVNET_DEFINE(int, tcp_initcwnd_segments) = 10; 168296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 169296341Sdelphij CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 170296341Sdelphij "Slow-start flight size (initial congestion window) in number of segments"); 171160814Ssimon 172296341SdelphijVNET_DEFINE(int, tcp_do_rfc3465) = 1; 173296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 174160814Ssimon &VNET_NAME(tcp_do_rfc3465), 0, 175296341Sdelphij "Enable RFC 3465 (Appropriate Byte Counting)"); 176296341Sdelphij 177160814SsimonVNET_DEFINE(int, tcp_abc_l_var) = 2; 178296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 179279264Sdelphij &VNET_NAME(tcp_abc_l_var), 2, 180296341Sdelphij "Cap the max cwnd increment during slow-start to this number of segments"); 181238405Sjkim 182296341Sdelphijstatic SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 183296341Sdelphij 184296341SdelphijVNET_DEFINE(int, tcp_do_ecn) = 2; 185296341SdelphijSYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 186296341Sdelphij &VNET_NAME(tcp_do_ecn), 0, 187296341Sdelphij "TCP ECN support"); 188296341Sdelphij 189296341SdelphijVNET_DEFINE(int, tcp_ecn_maxretries) = 1; 190296341SdelphijSYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 191296341Sdelphij &VNET_NAME(tcp_ecn_maxretries), 0, 192296341Sdelphij "Max retries before giving up on ECN"); 193296341Sdelphij 194238405SjkimVNET_DEFINE(int, tcp_insecure_syn) = 0; 19555714Skris#define V_tcp_insecure_syn VNET(tcp_insecure_syn) 196296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 197296341Sdelphij &VNET_NAME(tcp_insecure_syn), 0, 198296341Sdelphij "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 199296341Sdelphij 20055714SkrisVNET_DEFINE(int, tcp_insecure_rst) = 0; 201296341Sdelphij#define V_tcp_insecure_rst VNET(tcp_insecure_rst) 202296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 203296341Sdelphij &VNET_NAME(tcp_insecure_rst), 0, 204296341Sdelphij "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 205296341Sdelphij 206296341SdelphijVNET_DEFINE(int, tcp_recvspace) = 1024*64; 207296341Sdelphij#define V_tcp_recvspace VNET(tcp_recvspace) 208296341SdelphijSYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 209296341Sdelphij &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 210296341Sdelphij 211296341SdelphijVNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 212296341Sdelphij#define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 21355714SkrisSYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 21455714Skris &VNET_NAME(tcp_do_autorcvbuf), 0, 215296341Sdelphij "Enable automatic receive buffer sizing"); 216296341Sdelphij 217296341SdelphijVNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 218296341Sdelphij#define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 219296341SdelphijSYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 22055714Skris &VNET_NAME(tcp_autorcvbuf_inc), 0, 22155714Skris "Incrementor step size of automatic receive buffer"); 222296341Sdelphij 223296341SdelphijVNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 22455714Skris#define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 22555714SkrisSYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 226296341Sdelphij &VNET_NAME(tcp_autorcvbuf_max), 0, 22755714Skris "Max size of automatic receive buffer"); 22855714Skris 229296341SdelphijVNET_DEFINE(struct inpcbhead, tcb); 23055714Skris#define tcb6 tcb /* for KAME src sync over BSD*'s */ 231296341SdelphijVNET_DEFINE(struct inpcbinfo, tcbinfo); 232296341Sdelphij 23359191Skris/* 23459191Skris * TCP statistics are stored in an array of counter(9)s, which size matches 235296341Sdelphij * size of struct tcpstat. TCP running connection count is a regular array. 23659191Skris */ 237167612SsimonVNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 238167612SsimonSYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 239167612Ssimon tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 240296341SdelphijVNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 241167612SsimonSYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 242167612Ssimon CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 243296341Sdelphij "TCP connection counts by TCP state"); 244296341Sdelphij 245296341Sdelphijstatic void 246296341Sdelphijtcp_vnet_init(const void *unused) 247296341Sdelphij{ 248296341Sdelphij 249296341Sdelphij COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK); 25055714Skris VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK); 25155714Skris} 252296341SdelphijVNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 253296341Sdelphij tcp_vnet_init, NULL); 254296341Sdelphij 255296341Sdelphij#ifdef VIMAGE 25655714Skrisstatic void 25759191Skristcp_vnet_uninit(const void *unused) 258296341Sdelphij{ 259296341Sdelphij 260296341Sdelphij COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES); 261296341Sdelphij VNET_PCPUSTAT_FREE(tcpstat); 262296341Sdelphij} 26355714SkrisVNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 264296341Sdelphij tcp_vnet_uninit, NULL); 265296341Sdelphij#endif /* VIMAGE */ 266296341Sdelphij 267296341Sdelphij/* 268296341Sdelphij * Kernel module interface for updating tcpstat. The argument is an index 269296341Sdelphij * into tcpstat treated as an array. 270296341Sdelphij */ 271296341Sdelphijvoid 272296341Sdelphijkmod_tcpstat_inc(int statnum) 273296341Sdelphij{ 274296341Sdelphij 27555714Skris counter_u64_add(VNET(tcpstat)[statnum], 1); 276296341Sdelphij} 277109998Smarkm 278296341Sdelphij/* 27955714Skris * Wrapper for the TCP established input helper hook. 28055714Skris */ 281296341Sdelphijvoid 282296341Sdelphijhhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 283296341Sdelphij{ 284296341Sdelphij struct tcp_hhook_data hhook_data; 285296341Sdelphij 286296341Sdelphij if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 28755714Skris hhook_data.tp = tp; 288296341Sdelphij hhook_data.th = th; 289296341Sdelphij hhook_data.to = to; 290296341Sdelphij 291296341Sdelphij hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 292296341Sdelphij tp->osd); 293296341Sdelphij } 294296341Sdelphij} 295296341Sdelphij 29655714Skris/* 297296341Sdelphij * CC wrapper hook functions 298296341Sdelphij */ 299296341Sdelphijvoid 300296341Sdelphijcc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type) 301296341Sdelphij{ 302167612Ssimon INP_WLOCK_ASSERT(tp->t_inpcb); 303167612Ssimon 30455714Skris tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 305296341Sdelphij if (tp->snd_cwnd <= tp->snd_wnd) 306167612Ssimon tp->ccv->flags |= CCF_CWND_LIMITED; 30768651Skris else 308296341Sdelphij tp->ccv->flags &= ~CCF_CWND_LIMITED; 309296341Sdelphij 31068651Skris if (type == CC_ACK) { 311296341Sdelphij if (tp->snd_cwnd > tp->snd_ssthresh) { 312296341Sdelphij tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 313296341Sdelphij V_tcp_abc_l_var * tcp_maxseg(tp)); 314296341Sdelphij if (tp->t_bytes_acked >= tp->snd_cwnd) { 315296341Sdelphij tp->t_bytes_acked -= tp->snd_cwnd; 316296341Sdelphij tp->ccv->flags |= CCF_ABC_SENTAWND; 317296341Sdelphij } 318296341Sdelphij } else { 319296341Sdelphij tp->ccv->flags &= ~CCF_ABC_SENTAWND; 320296341Sdelphij tp->t_bytes_acked = 0; 321296341Sdelphij } 322296341Sdelphij } 32355714Skris 324296341Sdelphij if (CC_ALGO(tp)->ack_received != NULL) { 325296341Sdelphij /* XXXLAS: Find a way to live without this */ 326296341Sdelphij tp->ccv->curack = th->th_ack; 327296341Sdelphij CC_ALGO(tp)->ack_received(tp->ccv, type); 328296341Sdelphij } 329296341Sdelphij} 330296341Sdelphij 331296341Sdelphijvoid 332296341Sdelphijcc_conn_init(struct tcpcb *tp) 333296341Sdelphij{ 334296341Sdelphij struct hc_metrics_lite metrics; 335296341Sdelphij struct inpcb *inp = tp->t_inpcb; 336296341Sdelphij u_int maxseg; 337296341Sdelphij int rtt; 338296341Sdelphij 339296341Sdelphij INP_WLOCK_ASSERT(tp->t_inpcb); 340296341Sdelphij 341296341Sdelphij tcp_hc_get(&inp->inp_inc, &metrics); 34255714Skris maxseg = tcp_maxseg(tp); 34368651Skris 34468651Skris if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 345296341Sdelphij tp->t_srtt = rtt; 346296341Sdelphij tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 347296341Sdelphij TCPSTAT_INC(tcps_usedrtt); 348296341Sdelphij if (metrics.rmx_rttvar) { 349296341Sdelphij tp->t_rttvar = metrics.rmx_rttvar; 350296341Sdelphij TCPSTAT_INC(tcps_usedrttvar); 351296341Sdelphij } else { 352296341Sdelphij /* default variation is +- 1 rtt */ 353296341Sdelphij tp->t_rttvar = 354296341Sdelphij tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 355296341Sdelphij } 356296341Sdelphij TCPT_RANGESET(tp->t_rxtcur, 357296341Sdelphij ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 358296341Sdelphij tp->t_rttmin, TCPTV_REXMTMAX); 359296341Sdelphij } 360296341Sdelphij if (metrics.rmx_ssthresh) { 361296341Sdelphij /* 362296341Sdelphij * There's some sort of gateway or interface 363296341Sdelphij * buffer limit on the path. Use this to set 364296341Sdelphij * the slow start threshold, but set the 365296341Sdelphij * threshold to no less than 2*mss. 366296341Sdelphij */ 367237657Sjkim tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh); 368238405Sjkim TCPSTAT_INC(tcps_usedssthresh); 369296341Sdelphij } 370296341Sdelphij 371238405Sjkim /* 372296341Sdelphij * Set the initial slow-start flight size. 373238405Sjkim * 374296341Sdelphij * RFC5681 Section 3.1 specifies the default conservative values. 375296341Sdelphij * RFC3390 specifies slightly more aggressive values. 376296341Sdelphij * RFC6928 increases it to ten segments. 377296341Sdelphij * Support for user specified value for initial flight size. 378296341Sdelphij * 379296341Sdelphij * If a SYN or SYN/ACK was lost and retransmitted, we have to 380238405Sjkim * reduce the initial CWND to one segment as congestion is likely 381296341Sdelphij * requiring us to be cautious. 382296341Sdelphij */ 383296341Sdelphij if (tp->snd_cwnd == 1) 384296341Sdelphij tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 385296341Sdelphij else if (V_tcp_initcwnd_segments) 386296341Sdelphij tp->snd_cwnd = min(V_tcp_initcwnd_segments * maxseg, 387296341Sdelphij max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 388296341Sdelphij else if (V_tcp_do_rfc3390) 389296341Sdelphij tp->snd_cwnd = min(4 * maxseg, max(2 * maxseg, 4380)); 390238405Sjkim else { 391296341Sdelphij /* Per RFC5681 Section 3.1 */ 392296341Sdelphij if (maxseg > 2190) 393296341Sdelphij tp->snd_cwnd = 2 * maxseg; 394296341Sdelphij else if (maxseg > 1095) 395296341Sdelphij tp->snd_cwnd = 3 * maxseg; 396238405Sjkim else 39755714Skris tp->snd_cwnd = 4 * maxseg; 398296341Sdelphij } 399296341Sdelphij 400296341Sdelphij if (CC_ALGO(tp)->conn_init != NULL) 401296341Sdelphij CC_ALGO(tp)->conn_init(tp->ccv); 402296341Sdelphij} 403296341Sdelphij 404296341Sdelphijvoid inline 405296341Sdelphijcc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 406296341Sdelphij{ 407296341Sdelphij u_int maxseg; 408296341Sdelphij 40955714Skris INP_WLOCK_ASSERT(tp->t_inpcb); 410296341Sdelphij 411296341Sdelphij switch(type) { 412296341Sdelphij case CC_NDUPACK: 413296341Sdelphij if (!IN_FASTRECOVERY(tp->t_flags)) { 414296341Sdelphij tp->snd_recover = tp->snd_max; 415296341Sdelphij if (tp->t_flags & TF_ECN_PERMIT) 416296341Sdelphij tp->t_flags |= TF_ECN_SND_CWR; 417296341Sdelphij } 418296341Sdelphij break; 419296341Sdelphij case CC_ECN: 420296341Sdelphij if (!IN_CONGRECOVERY(tp->t_flags) || 421296341Sdelphij /* 422296341Sdelphij * Allow ECN reaction on ACK to CWR, if 423296341Sdelphij * that data segment was also CE marked. 424296341Sdelphij */ 425296341Sdelphij SEQ_GEQ(th->th_ack, tp->snd_recover)) { 426296341Sdelphij EXIT_CONGRECOVERY(tp->t_flags); 427296341Sdelphij TCPSTAT_INC(tcps_ecn_rcwnd); 428296341Sdelphij tp->snd_recover = tp->snd_max + 1; 429296341Sdelphij if (tp->t_flags & TF_ECN_PERMIT) 430296341Sdelphij tp->t_flags |= TF_ECN_SND_CWR; 431296341Sdelphij } 432296341Sdelphij break; 433296341Sdelphij case CC_RTO: 434296341Sdelphij maxseg = tcp_maxseg(tp); 435296341Sdelphij tp->t_dupacks = 0; 436296341Sdelphij tp->t_bytes_acked = 0; 437296341Sdelphij EXIT_RECOVERY(tp->t_flags); 438296341Sdelphij tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 439296341Sdelphij maxseg) * maxseg; 440296341Sdelphij tp->snd_cwnd = maxseg; 441296341Sdelphij break; 442296341Sdelphij case CC_RTO_ERR: 443296341Sdelphij TCPSTAT_INC(tcps_sndrexmitbad); 444296341Sdelphij /* RTO was unnecessary, so reset everything. */ 445296341Sdelphij tp->snd_cwnd = tp->snd_cwnd_prev; 446296341Sdelphij tp->snd_ssthresh = tp->snd_ssthresh_prev; 44755714Skris tp->snd_recover = tp->snd_recover_prev; 448296341Sdelphij if (tp->t_flags & TF_WASFRECOVERY) 449296341Sdelphij ENTER_FASTRECOVERY(tp->t_flags); 450296341Sdelphij if (tp->t_flags & TF_WASCRECOVERY) 451296341Sdelphij ENTER_CONGRECOVERY(tp->t_flags); 452296341Sdelphij tp->snd_nxt = tp->snd_max; 453296341Sdelphij tp->t_flags &= ~TF_PREVVALID; 454296341Sdelphij tp->t_badrxtwin = 0; 455296341Sdelphij break; 456296341Sdelphij } 457296341Sdelphij 458296341Sdelphij if (CC_ALGO(tp)->cong_signal != NULL) { 459296341Sdelphij if (th != NULL) 460296341Sdelphij tp->ccv->curack = th->th_ack; 46155714Skris CC_ALGO(tp)->cong_signal(tp->ccv, type); 462296341Sdelphij } 463296341Sdelphij} 464296341Sdelphij 465296341Sdelphijvoid inline 46655714Skriscc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 467296341Sdelphij{ 468296341Sdelphij INP_WLOCK_ASSERT(tp->t_inpcb); 469238405Sjkim 470296341Sdelphij /* XXXLAS: KASSERT that we're in recovery? */ 471296341Sdelphij 47255714Skris if (CC_ALGO(tp)->post_recovery != NULL) { 47355714Skris tp->ccv->curack = th->th_ack; 474296341Sdelphij CC_ALGO(tp)->post_recovery(tp->ccv); 475296341Sdelphij } 476296341Sdelphij /* XXXLAS: EXIT_RECOVERY ? */ 477296341Sdelphij tp->t_bytes_acked = 0; 478296341Sdelphij} 479296341Sdelphij 480296341Sdelphij/* 481296341Sdelphij * Indicate whether this ack should be delayed. We can delay the ack if 48255714Skris * following conditions are met: 483296341Sdelphij * - There is no delayed ack timer in progress. 48455714Skris * - Our last ack wasn't a 0-sized window. We never want to delay 48555714Skris * the ack that opens up a 0-sized window. 486296341Sdelphij * - LRO wasn't used for this segment. We make sure by checking that the 487296341Sdelphij * segment size is not larger than the MSS. 488296341Sdelphij */ 489296341Sdelphij#define DELAY_ACK(tp, tlen) \ 490296341Sdelphij ((!tcp_timer_active(tp, TT_DELACK) && \ 49155714Skris (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 492296341Sdelphij (tlen <= tp->t_maxseg) && \ 493296341Sdelphij (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 494296341Sdelphij 495296341Sdelphijstatic void inline 496296341Sdelphijcc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 49755714Skris{ 498296341Sdelphij INP_WLOCK_ASSERT(tp->t_inpcb); 499296341Sdelphij 500296341Sdelphij if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 50155714Skris switch (iptos & IPTOS_ECN_MASK) { 50255714Skris case IPTOS_ECN_CE: 503296341Sdelphij tp->ccv->flags |= CCF_IPHDR_CE; 504296341Sdelphij break; 50555714Skris case IPTOS_ECN_ECT0: 506296341Sdelphij tp->ccv->flags &= ~CCF_IPHDR_CE; 50755714Skris break; 508296341Sdelphij case IPTOS_ECN_ECT1: 509296341Sdelphij tp->ccv->flags &= ~CCF_IPHDR_CE; 510296341Sdelphij break; 51155714Skris } 512296341Sdelphij 513296341Sdelphij if (th->th_flags & TH_CWR) 514296341Sdelphij tp->ccv->flags |= CCF_TCPHDR_CWR; 515296341Sdelphij else 51655714Skris tp->ccv->flags &= ~CCF_TCPHDR_CWR; 517296341Sdelphij 518296341Sdelphij if (tp->t_flags & TF_DELACK) 51955714Skris tp->ccv->flags |= CCF_DELACK; 520296341Sdelphij else 521296341Sdelphij tp->ccv->flags &= ~CCF_DELACK; 52255714Skris 523296341Sdelphij CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 524296341Sdelphij 52555714Skris if (tp->ccv->flags & CCF_ACKNOW) 526296341Sdelphij tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 527296341Sdelphij } 528296341Sdelphij} 529296341Sdelphij 530296341Sdelphij/* 531296341Sdelphij * TCP input handling is split into multiple parts: 532296341Sdelphij * tcp6_input is a thin wrapper around tcp_input for the extended 53355714Skris * ip6_protox[] call format in ip6_input 53455714Skris * tcp_input handles primary segment validation, inpcb lookup and 535296341Sdelphij * SYN processing on listen sockets 536296341Sdelphij * tcp_do_segment processes the ACK and text of the segment for 537296341Sdelphij * establishing, established and closing connections 538296341Sdelphij */ 539296341Sdelphij#ifdef INET6 540296341Sdelphijint 541296341Sdelphijtcp6_input(struct mbuf **mp, int *offp, int proto) 542296341Sdelphij{ 543296341Sdelphij struct mbuf *m = *mp; 544296341Sdelphij struct in6_ifaddr *ia6; 545296341Sdelphij struct ip6_hdr *ip6; 54655714Skris 547296341Sdelphij IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 548296341Sdelphij 549296341Sdelphij /* 550296341Sdelphij * draft-itojun-ipv6-tcp-to-anycast 551296341Sdelphij * better place to put this in? 552296341Sdelphij */ 553296341Sdelphij ip6 = mtod(m, struct ip6_hdr *); 554296341Sdelphij ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 555296341Sdelphij if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 556296341Sdelphij struct ip6_hdr *ip6; 557296341Sdelphij 558296341Sdelphij ifa_free(&ia6->ia_ifa); 559296341Sdelphij ip6 = mtod(m, struct ip6_hdr *); 560296341Sdelphij icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 561296341Sdelphij (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 56255714Skris return (IPPROTO_DONE); 56355714Skris } 564296341Sdelphij if (ia6) 56555714Skris ifa_free(&ia6->ia_ifa); 566296341Sdelphij 567296341Sdelphij *mp = m; 568296341Sdelphij return (tcp_input(mp, offp, proto)); 569296341Sdelphij} 570296341Sdelphij#endif /* INET6 */ 57155714Skris 57255714Skrisint 573296341Sdelphijtcp_input(struct mbuf **mp, int *offp, int proto) 574296341Sdelphij{ 575296341Sdelphij struct mbuf *m = *mp; 576296341Sdelphij struct tcphdr *th = NULL; 577296341Sdelphij struct ip *ip = NULL; 57855714Skris struct inpcb *inp = NULL; 57955714Skris struct tcpcb *tp = NULL; 580296341Sdelphij struct socket *so = NULL; 58155714Skris u_char *optp = NULL; 582296341Sdelphij int off0; 583296341Sdelphij int optlen = 0; 584296341Sdelphij#ifdef INET 585296341Sdelphij int len; 586296341Sdelphij uint8_t ipttl; 587296341Sdelphij#endif 58855714Skris int tlen = 0, off; 58955714Skris int drop_hdrlen; 59055714Skris int thflags; 591296341Sdelphij int rstreason = 0; /* For badport_bandlim accounting purposes */ 592296341Sdelphij uint8_t iptos; 593296341Sdelphij struct m_tag *fwd_tag = NULL; 594296341Sdelphij#ifdef INET6 59555714Skris struct ip6_hdr *ip6 = NULL; 59655714Skris int isipv6; 597296341Sdelphij#else 59855714Skris const void *ip6 = NULL; 59955714Skris#endif /* INET6 */ 600296341Sdelphij struct tcpopt to; /* options in this segment */ 601296341Sdelphij char *s = NULL; /* address and port logging */ 602296341Sdelphij int ti_locked; 603296341Sdelphij#ifdef TCPDEBUG 604296341Sdelphij /* 60555714Skris * The size of tcp_saveipgen must be the size of the max ip header, 606296341Sdelphij * now IPv6. 607296341Sdelphij */ 60855714Skris u_char tcp_saveipgen[IP6_HDR_LEN]; 60955714Skris struct tcphdr tcp_savetcp; 61059191Skris short ostate = 0; 61155714Skris#endif 612160814Ssimon 613296341Sdelphij#ifdef INET6 614160814Ssimon isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 615296341Sdelphij#endif 616160814Ssimon 617296341Sdelphij off0 = *offp; 618160814Ssimon m = *mp; 619296341Sdelphij *mp = NULL; 620160814Ssimon to.to_flags = 0; 621296341Sdelphij TCPSTAT_INC(tcps_rcvtotal); 622205128Ssimon 623296341Sdelphij#ifdef INET6 624160814Ssimon if (isipv6) { 625296341Sdelphij /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 626279264Sdelphij 627160814Ssimon if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 62855714Skris m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 62955714Skris if (m == NULL) { 63055714Skris TCPSTAT_INC(tcps_rcvshort); 631296341Sdelphij return (IPPROTO_DONE); 632296341Sdelphij } 63359191Skris } 634296341Sdelphij 63559191Skris ip6 = mtod(m, struct ip6_hdr *); 63659191Skris th = (struct tcphdr *)((caddr_t)ip6 + off0); 63755714Skris tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 638238405Sjkim if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 639238405Sjkim if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 640296341Sdelphij th->th_sum = m->m_pkthdr.csum_data; 641238405Sjkim else 642296341Sdelphij th->th_sum = in6_cksum_pseudo(ip6, tlen, 643238405Sjkim IPPROTO_TCP, m->m_pkthdr.csum_data); 644296341Sdelphij th->th_sum ^= 0xffff; 645238405Sjkim } else 646296341Sdelphij th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 647238405Sjkim if (th->th_sum) { 648109998Smarkm TCPSTAT_INC(tcps_rcvbadsum); 649296341Sdelphij goto drop; 65055714Skris } 65155714Skris 652296341Sdelphij /* 65368651Skris * Be proactive about unspecified IPv6 address in source. 654296341Sdelphij * As we use all-zero to indicate unbounded/unconnected pcb, 655296341Sdelphij * unspecified IPv6 address can be used to confuse us. 656296341Sdelphij * 657296341Sdelphij * Note that packets with unspecified IPv6 destination is 658296341Sdelphij * already dropped in ip6_input. 659296341Sdelphij */ 660296341Sdelphij if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 661296341Sdelphij /* XXX stat */ 662296341Sdelphij goto drop; 663296341Sdelphij } 664296341Sdelphij iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 665296341Sdelphij } 666296341Sdelphij#endif 667296341Sdelphij#if defined(INET) && defined(INET6) 668296341Sdelphij else 669296341Sdelphij#endif 670296341Sdelphij#ifdef INET 671296341Sdelphij { 672296341Sdelphij /* 673296341Sdelphij * Get IP and TCP header together in first mbuf. 674296341Sdelphij * Note: IP leaves IP header in first mbuf. 675296341Sdelphij */ 676296341Sdelphij if (off0 > sizeof (struct ip)) { 67755714Skris ip_stripoptions(m); 67859191Skris off0 = sizeof(struct ip); 67959191Skris } 68059191Skris if (m->m_len < sizeof (struct tcpiphdr)) { 68159191Skris if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 68259191Skris == NULL) { 683296341Sdelphij TCPSTAT_INC(tcps_rcvshort); 684296341Sdelphij return (IPPROTO_DONE); 68555714Skris } 68655714Skris } 68759191Skris ip = mtod(m, struct ip *); 68855714Skris th = (struct tcphdr *)((caddr_t)ip + off0); 68955714Skris tlen = ntohs(ip->ip_len) - off0; 69055714Skris 69155714Skris iptos = ip->ip_tos; 692296341Sdelphij if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 69355714Skris if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 694296341Sdelphij th->th_sum = m->m_pkthdr.csum_data; 69555714Skris else 69655714Skris th->th_sum = in_pseudo(ip->ip_src.s_addr, 69755714Skris ip->ip_dst.s_addr, 69855714Skris htonl(m->m_pkthdr.csum_data + tlen + 699296341Sdelphij IPPROTO_TCP)); 70068651Skris th->th_sum ^= 0xffff; 701296341Sdelphij } else { 70255714Skris struct ipovly *ipov = (struct ipovly *)ip; 703296341Sdelphij 704160814Ssimon /* 705296341Sdelphij * Checksum extended TCP header and data. 706238405Sjkim */ 707296341Sdelphij len = off0 + tlen; 708296341Sdelphij ipttl = ip->ip_ttl; 709160814Ssimon bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 71055714Skris ipov->ih_len = htons(tlen); 71155714Skris th->th_sum = in_cksum(m, len); 71255714Skris /* Reset length for SDT probes. */ 71355714Skris ip->ip_len = htons(len); 714160814Ssimon /* Reset TOS bits */ 715160814Ssimon ip->ip_tos = iptos; 71655714Skris /* Re-initialization for later version check */ 71755714Skris ip->ip_ttl = ipttl; 718296341Sdelphij ip->ip_v = IPVERSION; 719296341Sdelphij ip->ip_hl = off0 >> 2; 720296341Sdelphij } 721296341Sdelphij 722296341Sdelphij if (th->th_sum) { 723296341Sdelphij TCPSTAT_INC(tcps_rcvbadsum); 724296341Sdelphij goto drop; 725160814Ssimon } 726160814Ssimon } 727296341Sdelphij#endif /* INET */ 72855714Skris 729296341Sdelphij /* 730296341Sdelphij * Check that TCP offset makes sense, 73155714Skris * pull out TCP options and adjust length. XXX 73255714Skris */ 73355714Skris off = th->th_off << 2; 73455714Skris if (off < sizeof (struct tcphdr) || off > tlen) { 73555714Skris TCPSTAT_INC(tcps_rcvbadoff); 73655714Skris goto drop; 73755714Skris } 73855714Skris tlen -= off; /* tlen is used instead of ti->ti_len */ 739111147Snectar if (off > sizeof (struct tcphdr)) { 740296341Sdelphij#ifdef INET6 74155714Skris if (isipv6) { 74255714Skris IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); 743296341Sdelphij ip6 = mtod(m, struct ip6_hdr *); 744296341Sdelphij th = (struct tcphdr *)((caddr_t)ip6 + off0); 745296341Sdelphij } 74655714Skris#endif 747296341Sdelphij#if defined(INET) && defined(INET6) 74855714Skris else 74955714Skris#endif 750160814Ssimon#ifdef INET 751296341Sdelphij { 752238405Sjkim if (m->m_len < sizeof(struct ip) + off) { 753238405Sjkim if ((m = m_pullup(m, sizeof (struct ip) + off)) 754238405Sjkim == NULL) { 755296341Sdelphij TCPSTAT_INC(tcps_rcvshort); 756296341Sdelphij return (IPPROTO_DONE); 757296341Sdelphij } 758296341Sdelphij ip = mtod(m, struct ip *); 759238405Sjkim th = (struct tcphdr *)((caddr_t)ip + off0); 760238405Sjkim } 761238405Sjkim } 762296341Sdelphij#endif 76355714Skris optlen = off - sizeof (struct tcphdr); 76455714Skris optp = (u_char *)(th + 1); 76555714Skris } 76655714Skris thflags = th->th_flags; 76755714Skris 768296341Sdelphij /* 769296341Sdelphij * Convert TCP protocol specific fields to host format. 770296341Sdelphij */ 771296341Sdelphij tcp_fields_to_host(th); 772296341Sdelphij 77355714Skris /* 77455714Skris * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 77555714Skris */ 77655714Skris drop_hdrlen = off0 + off; 777296341Sdelphij 778296341Sdelphij /* 779296341Sdelphij * Locate pcb for segment; if we're likely to add or remove a 78055714Skris * connection then first acquire pcbinfo lock. There are three cases 781296341Sdelphij * where we might discover later we need a write lock despite the 782160814Ssimon * flags: ACKs moving a connection out of the syncache, ACKs for a 783296341Sdelphij * connection in TIMEWAIT and SYNs not targeting a listening socket. 784160814Ssimon */ 785296341Sdelphij if ((thflags & (TH_FIN | TH_RST)) != 0) { 786160814Ssimon INP_INFO_RLOCK(&V_tcbinfo); 787296341Sdelphij ti_locked = TI_RLOCKED; 788160814Ssimon } else 789296341Sdelphij ti_locked = TI_UNLOCKED; 790160814Ssimon 791296341Sdelphij /* 792160814Ssimon * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 793296341Sdelphij */ 794296341Sdelphij if ( 79555714Skris#ifdef INET6 79655714Skris (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 797296341Sdelphij#ifdef INET 798296341Sdelphij || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 79955714Skris#endif 80055714Skris#endif 80189837Skris#if defined(INET) && !defined(INET6) 80255714Skris (m->m_flags & M_IP_NEXTHOP) 80355714Skris#endif 80455714Skris ) 80555714Skris fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 806296341Sdelphij 807296341Sdelphijfindpcb: 808296341Sdelphij#ifdef INVARIANTS 809296341Sdelphij if (ti_locked == TI_RLOCKED) { 810296341Sdelphij INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 811296341Sdelphij } else { 812296341Sdelphij INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 813296341Sdelphij } 814296341Sdelphij#endif 815296341Sdelphij#ifdef INET6 816296341Sdelphij if (isipv6 && fwd_tag != NULL) { 817296341Sdelphij struct sockaddr_in6 *next_hop6; 818296341Sdelphij 819296341Sdelphij next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 820296341Sdelphij /* 821296341Sdelphij * Transparently forwarded. Pretend to be the destination. 822296341Sdelphij * Already got one like this? 823296341Sdelphij */ 824296341Sdelphij inp = in6_pcblookup_mbuf(&V_tcbinfo, 825296341Sdelphij &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 826296341Sdelphij INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 827296341Sdelphij if (!inp) { 828296341Sdelphij /* 829296341Sdelphij * It's new. Try to find the ambushing socket. 830296341Sdelphij * Because we've rewritten the destination address, 831296341Sdelphij * any hardware-generated hash is ignored. 832296341Sdelphij */ 833296341Sdelphij inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 834296341Sdelphij th->th_sport, &next_hop6->sin6_addr, 835296341Sdelphij next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 836296341Sdelphij th->th_dport, INPLOOKUP_WILDCARD | 837296341Sdelphij INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 838296341Sdelphij } 839296341Sdelphij } else if (isipv6) { 84055714Skris inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 84155714Skris th->th_sport, &ip6->ip6_dst, th->th_dport, 842296341Sdelphij INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 843296341Sdelphij m->m_pkthdr.rcvif, m); 844296341Sdelphij } 845296341Sdelphij#endif /* INET6 */ 846296341Sdelphij#if defined(INET6) && defined(INET) 847296341Sdelphij else 848296341Sdelphij#endif 849296341Sdelphij#ifdef INET 850296341Sdelphij if (fwd_tag != NULL) { 851296341Sdelphij struct sockaddr_in *next_hop; 852296341Sdelphij 853296341Sdelphij next_hop = (struct sockaddr_in *)(fwd_tag+1); 854296341Sdelphij /* 855296341Sdelphij * Transparently forwarded. Pretend to be the destination. 856296341Sdelphij * already got one like this? 857296341Sdelphij */ 858296341Sdelphij inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 859296341Sdelphij ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 860296341Sdelphij m->m_pkthdr.rcvif, m); 861296341Sdelphij if (!inp) { 862296341Sdelphij /* 863296341Sdelphij * It's new. Try to find the ambushing socket. 864296341Sdelphij * Because we've rewritten the destination address, 865296341Sdelphij * any hardware-generated hash is ignored. 866296341Sdelphij */ 867296341Sdelphij inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 868296341Sdelphij th->th_sport, next_hop->sin_addr, 869296341Sdelphij next_hop->sin_port ? ntohs(next_hop->sin_port) : 870296341Sdelphij th->th_dport, INPLOOKUP_WILDCARD | 87155714Skris INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 87255714Skris } 87355714Skris } else 87455714Skris inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 87555714Skris th->th_sport, ip->ip_dst, th->th_dport, 876 INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 877 m->m_pkthdr.rcvif, m); 878#endif /* INET */ 879 880 /* 881 * If the INPCB does not exist then all data in the incoming 882 * segment is discarded and an appropriate RST is sent back. 883 * XXX MRT Send RST using which routing table? 884 */ 885 if (inp == NULL) { 886 /* 887 * Log communication attempts to ports that are not 888 * in use. 889 */ 890 if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 891 tcp_log_in_vain == 2) { 892 if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 893 log(LOG_INFO, "%s; %s: Connection attempt " 894 "to closed port\n", s, __func__); 895 } 896 /* 897 * When blackholing do not respond with a RST but 898 * completely ignore the segment and drop it. 899 */ 900 if ((V_blackhole == 1 && (thflags & TH_SYN)) || 901 V_blackhole == 2) 902 goto dropunlock; 903 904 rstreason = BANDLIM_RST_CLOSEDPORT; 905 goto dropwithreset; 906 } 907 INP_WLOCK_ASSERT(inp); 908 /* 909 * While waiting for inp lock during the lookup, another thread 910 * can have dropped the inpcb, in which case we need to loop back 911 * and try to find a new inpcb to deliver to. 912 */ 913 if (inp->inp_flags & INP_DROPPED) { 914 INP_WUNLOCK(inp); 915 inp = NULL; 916 goto findpcb; 917 } 918 if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 919 (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 920 ((inp->inp_socket == NULL) || 921 (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 922 inp->inp_flowid = m->m_pkthdr.flowid; 923 inp->inp_flowtype = M_HASHTYPE_GET(m); 924 } 925#if defined(IPSEC) || defined(IPSEC_SUPPORT) 926#ifdef INET6 927 if (isipv6 && IPSEC_ENABLED(ipv6) && 928 IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 929 goto dropunlock; 930 } 931#ifdef INET 932 else 933#endif 934#endif /* INET6 */ 935#ifdef INET 936 if (IPSEC_ENABLED(ipv4) && 937 IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 938 goto dropunlock; 939 } 940#endif /* INET */ 941#endif /* IPSEC */ 942 943 /* 944 * Check the minimum TTL for socket. 945 */ 946 if (inp->inp_ip_minttl != 0) { 947#ifdef INET6 948 if (isipv6) { 949 if (inp->inp_ip_minttl > ip6->ip6_hlim) 950 goto dropunlock; 951 } else 952#endif 953 if (inp->inp_ip_minttl > ip->ip_ttl) 954 goto dropunlock; 955 } 956 957 /* 958 * A previous connection in TIMEWAIT state is supposed to catch stray 959 * or duplicate segments arriving late. If this segment was a 960 * legitimate new connection attempt, the old INPCB gets removed and 961 * we can try again to find a listening socket. 962 * 963 * At this point, due to earlier optimism, we may hold only an inpcb 964 * lock, and not the inpcbinfo write lock. If so, we need to try to 965 * acquire it, or if that fails, acquire a reference on the inpcb, 966 * drop all locks, acquire a global write lock, and then re-acquire 967 * the inpcb lock. We may at that point discover that another thread 968 * has tried to free the inpcb, in which case we need to loop back 969 * and try to find a new inpcb to deliver to. 970 * 971 * XXXRW: It may be time to rethink timewait locking. 972 */ 973relocked: 974 if (inp->inp_flags & INP_TIMEWAIT) { 975 if (ti_locked == TI_UNLOCKED) { 976 if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 977 in_pcbref(inp); 978 INP_WUNLOCK(inp); 979 INP_INFO_RLOCK(&V_tcbinfo); 980 ti_locked = TI_RLOCKED; 981 INP_WLOCK(inp); 982 if (in_pcbrele_wlocked(inp)) { 983 inp = NULL; 984 goto findpcb; 985 } else if (inp->inp_flags & INP_DROPPED) { 986 INP_WUNLOCK(inp); 987 inp = NULL; 988 goto findpcb; 989 } 990 } else 991 ti_locked = TI_RLOCKED; 992 } 993 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 994 995 if (thflags & TH_SYN) 996 tcp_dooptions(&to, optp, optlen, TO_SYN); 997 /* 998 * NB: tcp_twcheck unlocks the INP and frees the mbuf. 999 */ 1000 if (tcp_twcheck(inp, &to, th, m, tlen)) 1001 goto findpcb; 1002 INP_INFO_RUNLOCK(&V_tcbinfo); 1003 return (IPPROTO_DONE); 1004 } 1005 /* 1006 * The TCPCB may no longer exist if the connection is winding 1007 * down or it is in the CLOSED state. Either way we drop the 1008 * segment and send an appropriate response. 1009 */ 1010 tp = intotcpcb(inp); 1011 if (tp == NULL || tp->t_state == TCPS_CLOSED) { 1012 rstreason = BANDLIM_RST_CLOSEDPORT; 1013 goto dropwithreset; 1014 } 1015 1016#ifdef TCP_OFFLOAD 1017 if (tp->t_flags & TF_TOE) { 1018 tcp_offload_input(tp, m); 1019 m = NULL; /* consumed by the TOE driver */ 1020 goto dropunlock; 1021 } 1022#endif 1023 1024 /* 1025 * We've identified a valid inpcb, but it could be that we need an 1026 * inpcbinfo write lock but don't hold it. In this case, attempt to 1027 * acquire using the same strategy as the TIMEWAIT case above. If we 1028 * relock, we have to jump back to 'relocked' as the connection might 1029 * now be in TIMEWAIT. 1030 */ 1031#ifdef INVARIANTS 1032 if ((thflags & (TH_FIN | TH_RST)) != 0) 1033 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1034#endif 1035 if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) || 1036 (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) && 1037 !(tp->t_flags & TF_FASTOPEN)))) { 1038 if (ti_locked == TI_UNLOCKED) { 1039 if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 1040 in_pcbref(inp); 1041 INP_WUNLOCK(inp); 1042 INP_INFO_RLOCK(&V_tcbinfo); 1043 ti_locked = TI_RLOCKED; 1044 INP_WLOCK(inp); 1045 if (in_pcbrele_wlocked(inp)) { 1046 inp = NULL; 1047 goto findpcb; 1048 } else if (inp->inp_flags & INP_DROPPED) { 1049 INP_WUNLOCK(inp); 1050 inp = NULL; 1051 goto findpcb; 1052 } 1053 goto relocked; 1054 } else 1055 ti_locked = TI_RLOCKED; 1056 } 1057 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1058 } 1059 1060#ifdef MAC 1061 INP_WLOCK_ASSERT(inp); 1062 if (mac_inpcb_check_deliver(inp, m)) 1063 goto dropunlock; 1064#endif 1065 so = inp->inp_socket; 1066 KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1067#ifdef TCPDEBUG 1068 if (so->so_options & SO_DEBUG) { 1069 ostate = tp->t_state; 1070#ifdef INET6 1071 if (isipv6) { 1072 bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1073 } else 1074#endif 1075 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1076 tcp_savetcp = *th; 1077 } 1078#endif /* TCPDEBUG */ 1079 /* 1080 * When the socket is accepting connections (the INPCB is in LISTEN 1081 * state) we look into the SYN cache if this is a new connection 1082 * attempt or the completion of a previous one. 1083 */ 1084 KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN), 1085 ("%s: so accepting but tp %p not listening", __func__, tp)); 1086 if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) { 1087 struct in_conninfo inc; 1088 1089 bzero(&inc, sizeof(inc)); 1090#ifdef INET6 1091 if (isipv6) { 1092 inc.inc_flags |= INC_ISIPV6; 1093 if (inp->inp_inc.inc_flags & INC_IPV6MINMTU) 1094 inc.inc_flags |= INC_IPV6MINMTU; 1095 inc.inc6_faddr = ip6->ip6_src; 1096 inc.inc6_laddr = ip6->ip6_dst; 1097 } else 1098#endif 1099 { 1100 inc.inc_faddr = ip->ip_src; 1101 inc.inc_laddr = ip->ip_dst; 1102 } 1103 inc.inc_fport = th->th_sport; 1104 inc.inc_lport = th->th_dport; 1105 inc.inc_fibnum = so->so_fibnum; 1106 1107 /* 1108 * Check for an existing connection attempt in syncache if 1109 * the flag is only ACK. A successful lookup creates a new 1110 * socket appended to the listen queue in SYN_RECEIVED state. 1111 */ 1112 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1113 1114 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1115 /* 1116 * Parse the TCP options here because 1117 * syncookies need access to the reflected 1118 * timestamp. 1119 */ 1120 tcp_dooptions(&to, optp, optlen, 0); 1121 /* 1122 * NB: syncache_expand() doesn't unlock 1123 * inp and tcpinfo locks. 1124 */ 1125 rstreason = syncache_expand(&inc, &to, th, &so, m); 1126 if (rstreason < 0) { 1127 /* 1128 * A failing TCP MD5 signature comparison 1129 * must result in the segment being dropped 1130 * and must not produce any response back 1131 * to the sender. 1132 */ 1133 goto dropunlock; 1134 } else if (rstreason == 0) { 1135 /* 1136 * No syncache entry or ACK was not 1137 * for our SYN/ACK. Send a RST. 1138 * NB: syncache did its own logging 1139 * of the failure cause. 1140 */ 1141 rstreason = BANDLIM_RST_OPENPORT; 1142 goto dropwithreset; 1143 } 1144#ifdef TCP_RFC7413 1145new_tfo_socket: 1146#endif 1147 if (so == NULL) { 1148 /* 1149 * We completed the 3-way handshake 1150 * but could not allocate a socket 1151 * either due to memory shortage, 1152 * listen queue length limits or 1153 * global socket limits. Send RST 1154 * or wait and have the remote end 1155 * retransmit the ACK for another 1156 * try. 1157 */ 1158 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1159 log(LOG_DEBUG, "%s; %s: Listen socket: " 1160 "Socket allocation failed due to " 1161 "limits or memory shortage, %s\n", 1162 s, __func__, 1163 V_tcp_sc_rst_sock_fail ? 1164 "sending RST" : "try again"); 1165 if (V_tcp_sc_rst_sock_fail) { 1166 rstreason = BANDLIM_UNLIMITED; 1167 goto dropwithreset; 1168 } else 1169 goto dropunlock; 1170 } 1171 /* 1172 * Socket is created in state SYN_RECEIVED. 1173 * Unlock the listen socket, lock the newly 1174 * created socket and update the tp variable. 1175 */ 1176 INP_WUNLOCK(inp); /* listen socket */ 1177 inp = sotoinpcb(so); 1178 /* 1179 * New connection inpcb is already locked by 1180 * syncache_expand(). 1181 */ 1182 INP_WLOCK_ASSERT(inp); 1183 tp = intotcpcb(inp); 1184 KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 1185 ("%s: ", __func__)); 1186 /* 1187 * Process the segment and the data it 1188 * contains. tcp_do_segment() consumes 1189 * the mbuf chain and unlocks the inpcb. 1190 */ 1191 TCP_PROBE5(receive, NULL, tp, m, tp, th); 1192 tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1193 iptos, ti_locked); 1194 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1195 return (IPPROTO_DONE); 1196 } 1197 /* 1198 * Segment flag validation for new connection attempts: 1199 * 1200 * Our (SYN|ACK) response was rejected. 1201 * Check with syncache and remove entry to prevent 1202 * retransmits. 1203 * 1204 * NB: syncache_chkrst does its own logging of failure 1205 * causes. 1206 */ 1207 if (thflags & TH_RST) { 1208 syncache_chkrst(&inc, th); 1209 goto dropunlock; 1210 } 1211 /* 1212 * We can't do anything without SYN. 1213 */ 1214 if ((thflags & TH_SYN) == 0) { 1215 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1216 log(LOG_DEBUG, "%s; %s: Listen socket: " 1217 "SYN is missing, segment ignored\n", 1218 s, __func__); 1219 TCPSTAT_INC(tcps_badsyn); 1220 goto dropunlock; 1221 } 1222 /* 1223 * (SYN|ACK) is bogus on a listen socket. 1224 */ 1225 if (thflags & TH_ACK) { 1226 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1227 log(LOG_DEBUG, "%s; %s: Listen socket: " 1228 "SYN|ACK invalid, segment rejected\n", 1229 s, __func__); 1230 syncache_badack(&inc); /* XXX: Not needed! */ 1231 TCPSTAT_INC(tcps_badsyn); 1232 rstreason = BANDLIM_RST_OPENPORT; 1233 goto dropwithreset; 1234 } 1235 /* 1236 * If the drop_synfin option is enabled, drop all 1237 * segments with both the SYN and FIN bits set. 1238 * This prevents e.g. nmap from identifying the 1239 * TCP/IP stack. 1240 * XXX: Poor reasoning. nmap has other methods 1241 * and is constantly refining its stack detection 1242 * strategies. 1243 * XXX: This is a violation of the TCP specification 1244 * and was used by RFC1644. 1245 */ 1246 if ((thflags & TH_FIN) && V_drop_synfin) { 1247 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1248 log(LOG_DEBUG, "%s; %s: Listen socket: " 1249 "SYN|FIN segment ignored (based on " 1250 "sysctl setting)\n", s, __func__); 1251 TCPSTAT_INC(tcps_badsyn); 1252 goto dropunlock; 1253 } 1254 /* 1255 * Segment's flags are (SYN) or (SYN|FIN). 1256 * 1257 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1258 * as they do not affect the state of the TCP FSM. 1259 * The data pointed to by TH_URG and th_urp is ignored. 1260 */ 1261 KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1262 ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1263 KASSERT(thflags & (TH_SYN), 1264 ("%s: Listen socket: TH_SYN not set", __func__)); 1265#ifdef INET6 1266 /* 1267 * If deprecated address is forbidden, 1268 * we do not accept SYN to deprecated interface 1269 * address to prevent any new inbound connection from 1270 * getting established. 1271 * When we do not accept SYN, we send a TCP RST, 1272 * with deprecated source address (instead of dropping 1273 * it). We compromise it as it is much better for peer 1274 * to send a RST, and RST will be the final packet 1275 * for the exchange. 1276 * 1277 * If we do not forbid deprecated addresses, we accept 1278 * the SYN packet. RFC2462 does not suggest dropping 1279 * SYN in this case. 1280 * If we decipher RFC2462 5.5.4, it says like this: 1281 * 1. use of deprecated addr with existing 1282 * communication is okay - "SHOULD continue to be 1283 * used" 1284 * 2. use of it with new communication: 1285 * (2a) "SHOULD NOT be used if alternate address 1286 * with sufficient scope is available" 1287 * (2b) nothing mentioned otherwise. 1288 * Here we fall into (2b) case as we have no choice in 1289 * our source address selection - we must obey the peer. 1290 * 1291 * The wording in RFC2462 is confusing, and there are 1292 * multiple description text for deprecated address 1293 * handling - worse, they are not exactly the same. 1294 * I believe 5.5.4 is the best one, so we follow 5.5.4. 1295 */ 1296 if (isipv6 && !V_ip6_use_deprecated) { 1297 struct in6_ifaddr *ia6; 1298 1299 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 1300 if (ia6 != NULL && 1301 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 1302 ifa_free(&ia6->ia_ifa); 1303 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1304 log(LOG_DEBUG, "%s; %s: Listen socket: " 1305 "Connection attempt to deprecated " 1306 "IPv6 address rejected\n", 1307 s, __func__); 1308 rstreason = BANDLIM_RST_OPENPORT; 1309 goto dropwithreset; 1310 } 1311 if (ia6) 1312 ifa_free(&ia6->ia_ifa); 1313 } 1314#endif /* INET6 */ 1315 /* 1316 * Basic sanity checks on incoming SYN requests: 1317 * Don't respond if the destination is a link layer 1318 * broadcast according to RFC1122 4.2.3.10, p. 104. 1319 * If it is from this socket it must be forged. 1320 * Don't respond if the source or destination is a 1321 * global or subnet broad- or multicast address. 1322 * Note that it is quite possible to receive unicast 1323 * link-layer packets with a broadcast IP address. Use 1324 * in_broadcast() to find them. 1325 */ 1326 if (m->m_flags & (M_BCAST|M_MCAST)) { 1327 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1328 log(LOG_DEBUG, "%s; %s: Listen socket: " 1329 "Connection attempt from broad- or multicast " 1330 "link layer address ignored\n", s, __func__); 1331 goto dropunlock; 1332 } 1333#ifdef INET6 1334 if (isipv6) { 1335 if (th->th_dport == th->th_sport && 1336 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1337 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1338 log(LOG_DEBUG, "%s; %s: Listen socket: " 1339 "Connection attempt to/from self " 1340 "ignored\n", s, __func__); 1341 goto dropunlock; 1342 } 1343 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1344 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1345 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1346 log(LOG_DEBUG, "%s; %s: Listen socket: " 1347 "Connection attempt from/to multicast " 1348 "address ignored\n", s, __func__); 1349 goto dropunlock; 1350 } 1351 } 1352#endif 1353#if defined(INET) && defined(INET6) 1354 else 1355#endif 1356#ifdef INET 1357 { 1358 if (th->th_dport == th->th_sport && 1359 ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1360 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1361 log(LOG_DEBUG, "%s; %s: Listen socket: " 1362 "Connection attempt from/to self " 1363 "ignored\n", s, __func__); 1364 goto dropunlock; 1365 } 1366 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1367 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 1368 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1369 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1370 if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1371 log(LOG_DEBUG, "%s; %s: Listen socket: " 1372 "Connection attempt from/to broad- " 1373 "or multicast address ignored\n", 1374 s, __func__); 1375 goto dropunlock; 1376 } 1377 } 1378#endif 1379 /* 1380 * SYN appears to be valid. Create compressed TCP state 1381 * for syncache. 1382 */ 1383#ifdef TCPDEBUG 1384 if (so->so_options & SO_DEBUG) 1385 tcp_trace(TA_INPUT, ostate, tp, 1386 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1387#endif 1388 TCP_PROBE3(debug__input, tp, th, m); 1389 tcp_dooptions(&to, optp, optlen, TO_SYN); 1390#ifdef TCP_RFC7413 1391 if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL)) 1392 goto new_tfo_socket; 1393#else 1394 syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL); 1395#endif 1396 /* 1397 * Entry added to syncache and mbuf consumed. 1398 * Only the listen socket is unlocked by syncache_add(). 1399 */ 1400 if (ti_locked == TI_RLOCKED) { 1401 INP_INFO_RUNLOCK(&V_tcbinfo); 1402 ti_locked = TI_UNLOCKED; 1403 } 1404 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1405 return (IPPROTO_DONE); 1406 } else if (tp->t_state == TCPS_LISTEN) { 1407 /* 1408 * When a listen socket is torn down the SO_ACCEPTCONN 1409 * flag is removed first while connections are drained 1410 * from the accept queue in a unlock/lock cycle of the 1411 * ACCEPT_LOCK, opening a race condition allowing a SYN 1412 * attempt go through unhandled. 1413 */ 1414 goto dropunlock; 1415 } 1416#if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1417 if (tp->t_flags & TF_SIGNATURE) { 1418 tcp_dooptions(&to, optp, optlen, thflags); 1419 if ((to.to_flags & TOF_SIGNATURE) == 0) { 1420 TCPSTAT_INC(tcps_sig_err_nosigopt); 1421 goto dropunlock; 1422 } 1423 if (!TCPMD5_ENABLED() || 1424 TCPMD5_INPUT(m, th, to.to_signature) != 0) 1425 goto dropunlock; 1426 } 1427#endif 1428 TCP_PROBE5(receive, NULL, tp, m, tp, th); 1429 1430 /* 1431 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 1432 * state. tcp_do_segment() always consumes the mbuf chain, unlocks 1433 * the inpcb, and unlocks pcbinfo. 1434 */ 1435 tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1436 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1437 return (IPPROTO_DONE); 1438 1439dropwithreset: 1440 TCP_PROBE5(receive, NULL, tp, m, tp, th); 1441 1442 if (ti_locked == TI_RLOCKED) { 1443 INP_INFO_RUNLOCK(&V_tcbinfo); 1444 ti_locked = TI_UNLOCKED; 1445 } 1446#ifdef INVARIANTS 1447 else { 1448 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1449 "ti_locked: %d", __func__, ti_locked)); 1450 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1451 } 1452#endif 1453 1454 if (inp != NULL) { 1455 tcp_dropwithreset(m, th, tp, tlen, rstreason); 1456 INP_WUNLOCK(inp); 1457 } else 1458 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1459 m = NULL; /* mbuf chain got consumed. */ 1460 goto drop; 1461 1462dropunlock: 1463 if (m != NULL) 1464 TCP_PROBE5(receive, NULL, tp, m, tp, th); 1465 1466 if (ti_locked == TI_RLOCKED) { 1467 INP_INFO_RUNLOCK(&V_tcbinfo); 1468 ti_locked = TI_UNLOCKED; 1469 } 1470#ifdef INVARIANTS 1471 else { 1472 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1473 "ti_locked: %d", __func__, ti_locked)); 1474 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1475 } 1476#endif 1477 1478 if (inp != NULL) 1479 INP_WUNLOCK(inp); 1480 1481drop: 1482 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1483 if (s != NULL) 1484 free(s, M_TCPLOG); 1485 if (m != NULL) 1486 m_freem(m); 1487 return (IPPROTO_DONE); 1488} 1489 1490/* 1491 * Automatic sizing of receive socket buffer. Often the send 1492 * buffer size is not optimally adjusted to the actual network 1493 * conditions at hand (delay bandwidth product). Setting the 1494 * buffer size too small limits throughput on links with high 1495 * bandwidth and high delay (eg. trans-continental/oceanic links). 1496 * 1497 * On the receive side the socket buffer memory is only rarely 1498 * used to any significant extent. This allows us to be much 1499 * more aggressive in scaling the receive socket buffer. For 1500 * the case that the buffer space is actually used to a large 1501 * extent and we run out of kernel memory we can simply drop 1502 * the new segments; TCP on the sender will just retransmit it 1503 * later. Setting the buffer size too big may only consume too 1504 * much kernel memory if the application doesn't read() from 1505 * the socket or packet loss or reordering makes use of the 1506 * reassembly queue. 1507 * 1508 * The criteria to step up the receive buffer one notch are: 1509 * 1. Application has not set receive buffer size with 1510 * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1511 * 2. the number of bytes received during the time it takes 1512 * one timestamp to be reflected back to us (the RTT); 1513 * 3. received bytes per RTT is within seven eighth of the 1514 * current socket buffer size; 1515 * 4. receive buffer size has not hit maximal automatic size; 1516 * 1517 * This algorithm does one step per RTT at most and only if 1518 * we receive a bulk stream w/o packet losses or reorderings. 1519 * Shrinking the buffer during idle times is not necessary as 1520 * it doesn't consume any memory when idle. 1521 * 1522 * TODO: Only step up if the application is actually serving 1523 * the buffer to better manage the socket buffer resources. 1524 */ 1525int 1526tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1527 struct tcpcb *tp, int tlen) 1528{ 1529 int newsize = 0; 1530 1531 if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1532 tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1533 TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1534 (tp->t_srtt >> TCP_RTT_SHIFT)) { 1535 if (tp->rfbuf_cnt > (so->so_rcv.sb_hiwat / 8 * 7) && 1536 so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1537 newsize = min(so->so_rcv.sb_hiwat + 1538 V_tcp_autorcvbuf_inc, V_tcp_autorcvbuf_max); 1539 } 1540 TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1541 1542 /* Start over with next RTT. */ 1543 tp->rfbuf_ts = 0; 1544 tp->rfbuf_cnt = 0; 1545 } else { 1546 tp->rfbuf_cnt += tlen; /* add up */ 1547 } 1548 return (newsize); 1549} 1550 1551void 1552tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1553 struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1554 int ti_locked) 1555{ 1556 int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1557 int rstreason, todrop, win; 1558 u_long tiwin; 1559 char *s; 1560 struct in_conninfo *inc; 1561 struct mbuf *mfree; 1562 struct tcpopt to; 1563 int tfo_syn; 1564 1565#ifdef TCPDEBUG 1566 /* 1567 * The size of tcp_saveipgen must be the size of the max ip header, 1568 * now IPv6. 1569 */ 1570 u_char tcp_saveipgen[IP6_HDR_LEN]; 1571 struct tcphdr tcp_savetcp; 1572 short ostate = 0; 1573#endif 1574 thflags = th->th_flags; 1575 inc = &tp->t_inpcb->inp_inc; 1576 tp->sackhint.last_sack_ack = 0; 1577 sack_changed = 0; 1578 1579 /* 1580 * If this is either a state-changing packet or current state isn't 1581 * established, we require a write lock on tcbinfo. Otherwise, we 1582 * allow the tcbinfo to be in either alocked or unlocked, as the 1583 * caller may have unnecessarily acquired a write lock due to a race. 1584 */ 1585 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1586 tp->t_state != TCPS_ESTABLISHED) { 1587 KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 1588 "SYN/FIN/RST/!EST", __func__, ti_locked)); 1589 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1590 } else { 1591#ifdef INVARIANTS 1592 if (ti_locked == TI_RLOCKED) 1593 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1594 else { 1595 KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1596 "ti_locked: %d", __func__, ti_locked)); 1597 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1598 } 1599#endif 1600 } 1601 INP_WLOCK_ASSERT(tp->t_inpcb); 1602 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1603 __func__)); 1604 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1605 __func__)); 1606 1607#ifdef TCPPCAP 1608 /* Save segment, if requested. */ 1609 tcp_pcap_add(th, m, &(tp->t_inpkts)); 1610#endif 1611 1612 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1613 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1614 log(LOG_DEBUG, "%s; %s: " 1615 "SYN|FIN segment ignored (based on " 1616 "sysctl setting)\n", s, __func__); 1617 free(s, M_TCPLOG); 1618 } 1619 goto drop; 1620 } 1621 1622 /* 1623 * If a segment with the ACK-bit set arrives in the SYN-SENT state 1624 * check SEQ.ACK first. 1625 */ 1626 if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1627 (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1628 rstreason = BANDLIM_UNLIMITED; 1629 goto dropwithreset; 1630 } 1631 1632 /* 1633 * Segment received on connection. 1634 * Reset idle time and keep-alive timer. 1635 * XXX: This should be done after segment 1636 * validation to ignore broken/spoofed segs. 1637 */ 1638 tp->t_rcvtime = ticks; 1639 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1640 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 1641 1642 /* 1643 * Scale up the window into a 32-bit value. 1644 * For the SYN_SENT state the scale is zero. 1645 */ 1646 tiwin = th->th_win << tp->snd_scale; 1647 1648 /* 1649 * TCP ECN processing. 1650 */ 1651 if (tp->t_flags & TF_ECN_PERMIT) { 1652 if (thflags & TH_CWR) 1653 tp->t_flags &= ~TF_ECN_SND_ECE; 1654 switch (iptos & IPTOS_ECN_MASK) { 1655 case IPTOS_ECN_CE: 1656 tp->t_flags |= TF_ECN_SND_ECE; 1657 TCPSTAT_INC(tcps_ecn_ce); 1658 break; 1659 case IPTOS_ECN_ECT0: 1660 TCPSTAT_INC(tcps_ecn_ect0); 1661 break; 1662 case IPTOS_ECN_ECT1: 1663 TCPSTAT_INC(tcps_ecn_ect1); 1664 break; 1665 } 1666 1667 /* Process a packet differently from RFC3168. */ 1668 cc_ecnpkt_handler(tp, th, iptos); 1669 1670 /* Congestion experienced. */ 1671 if (thflags & TH_ECE) { 1672 cc_cong_signal(tp, th, CC_ECN); 1673 } 1674 } 1675 1676 /* 1677 * Parse options on any incoming segment. 1678 */ 1679 tcp_dooptions(&to, (u_char *)(th + 1), 1680 (th->th_off << 2) - sizeof(struct tcphdr), 1681 (thflags & TH_SYN) ? TO_SYN : 0); 1682 1683#if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1684 if ((tp->t_flags & TF_SIGNATURE) != 0 && 1685 (to.to_flags & TOF_SIGNATURE) == 0) { 1686 TCPSTAT_INC(tcps_sig_err_sigopt); 1687 /* XXX: should drop? */ 1688 } 1689#endif 1690 /* 1691 * If echoed timestamp is later than the current time, 1692 * fall back to non RFC1323 RTT calculation. Normalize 1693 * timestamp if syncookies were used when this connection 1694 * was established. 1695 */ 1696 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1697 to.to_tsecr -= tp->ts_offset; 1698 if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1699 to.to_tsecr = 0; 1700 } 1701 /* 1702 * Process options only when we get SYN/ACK back. The SYN case 1703 * for incoming connections is handled in tcp_syncache. 1704 * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1705 * or <SYN,ACK>) segment itself is never scaled. 1706 * XXX this is traditional behavior, may need to be cleaned up. 1707 */ 1708 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1709 if ((to.to_flags & TOF_SCALE) && 1710 (tp->t_flags & TF_REQ_SCALE)) { 1711 tp->t_flags |= TF_RCVD_SCALE; 1712 tp->snd_scale = to.to_wscale; 1713 } 1714 /* 1715 * Initial send window. It will be updated with 1716 * the next incoming segment to the scaled value. 1717 */ 1718 tp->snd_wnd = th->th_win; 1719 if (to.to_flags & TOF_TS) { 1720 tp->t_flags |= TF_RCVD_TSTMP; 1721 tp->ts_recent = to.to_tsval; 1722 tp->ts_recent_age = tcp_ts_getticks(); 1723 } 1724 if (to.to_flags & TOF_MSS) 1725 tcp_mss(tp, to.to_mss); 1726 if ((tp->t_flags & TF_SACK_PERMIT) && 1727 (to.to_flags & TOF_SACKPERM) == 0) 1728 tp->t_flags &= ~TF_SACK_PERMIT; 1729 } 1730 1731 /* 1732 * If timestamps were negotiated during SYN/ACK they should 1733 * appear on every segment during this session and vice versa. 1734 */ 1735 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 1736 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1737 log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1738 "no action\n", s, __func__); 1739 free(s, M_TCPLOG); 1740 } 1741 } 1742 if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 1743 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1744 log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 1745 "no action\n", s, __func__); 1746 free(s, M_TCPLOG); 1747 } 1748 } 1749 1750 /* 1751 * Header prediction: check for the two common cases 1752 * of a uni-directional data xfer. If the packet has 1753 * no control flags, is in-sequence, the window didn't 1754 * change and we're not retransmitting, it's a 1755 * candidate. If the length is zero and the ack moved 1756 * forward, we're the sender side of the xfer. Just 1757 * free the data acked & wake any higher level process 1758 * that was blocked waiting for space. If the length 1759 * is non-zero and the ack didn't move, we're the 1760 * receiver side. If we're getting packets in-order 1761 * (the reassembly queue is empty), add the data to 1762 * the socket buffer and note that we need a delayed ack. 1763 * Make sure that the hidden state-flags are also off. 1764 * Since we check for TCPS_ESTABLISHED first, it can only 1765 * be TH_NEEDSYN. 1766 */ 1767 if (tp->t_state == TCPS_ESTABLISHED && 1768 th->th_seq == tp->rcv_nxt && 1769 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1770 tp->snd_nxt == tp->snd_max && 1771 tiwin && tiwin == tp->snd_wnd && 1772 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1773 SEGQ_EMPTY(tp) && 1774 ((to.to_flags & TOF_TS) == 0 || 1775 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1776 1777 /* 1778 * If last ACK falls within this segment's sequence numbers, 1779 * record the timestamp. 1780 * NOTE that the test is modified according to the latest 1781 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1782 */ 1783 if ((to.to_flags & TOF_TS) != 0 && 1784 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1785 tp->ts_recent_age = tcp_ts_getticks(); 1786 tp->ts_recent = to.to_tsval; 1787 } 1788 1789 if (tlen == 0) { 1790 if (SEQ_GT(th->th_ack, tp->snd_una) && 1791 SEQ_LEQ(th->th_ack, tp->snd_max) && 1792 !IN_RECOVERY(tp->t_flags) && 1793 (to.to_flags & TOF_SACK) == 0 && 1794 TAILQ_EMPTY(&tp->snd_holes)) { 1795 /* 1796 * This is a pure ack for outstanding data. 1797 */ 1798 if (ti_locked == TI_RLOCKED) 1799 INP_INFO_RUNLOCK(&V_tcbinfo); 1800 ti_locked = TI_UNLOCKED; 1801 1802 TCPSTAT_INC(tcps_predack); 1803 1804 /* 1805 * "bad retransmit" recovery. 1806 */ 1807 if (tp->t_rxtshift == 1 && 1808 tp->t_flags & TF_PREVVALID && 1809 (int)(ticks - tp->t_badrxtwin) < 0) { 1810 cc_cong_signal(tp, th, CC_RTO_ERR); 1811 } 1812 1813 /* 1814 * Recalculate the transmit timer / rtt. 1815 * 1816 * Some boxes send broken timestamp replies 1817 * during the SYN+ACK phase, ignore 1818 * timestamps of 0 or we could calculate a 1819 * huge RTT and blow up the retransmit timer. 1820 */ 1821 if ((to.to_flags & TOF_TS) != 0 && 1822 to.to_tsecr) { 1823 u_int t; 1824 1825 t = tcp_ts_getticks() - to.to_tsecr; 1826 if (!tp->t_rttlow || tp->t_rttlow > t) 1827 tp->t_rttlow = t; 1828 tcp_xmit_timer(tp, 1829 TCP_TS_TO_TICKS(t) + 1); 1830 } else if (tp->t_rtttime && 1831 SEQ_GT(th->th_ack, tp->t_rtseq)) { 1832 if (!tp->t_rttlow || 1833 tp->t_rttlow > ticks - tp->t_rtttime) 1834 tp->t_rttlow = ticks - tp->t_rtttime; 1835 tcp_xmit_timer(tp, 1836 ticks - tp->t_rtttime); 1837 } 1838 acked = BYTES_THIS_ACK(tp, th); 1839 1840 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 1841 hhook_run_tcp_est_in(tp, th, &to); 1842 1843 TCPSTAT_INC(tcps_rcvackpack); 1844 TCPSTAT_ADD(tcps_rcvackbyte, acked); 1845 sbdrop(&so->so_snd, acked); 1846 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 1847 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1848 tp->snd_recover = th->th_ack - 1; 1849 1850 /* 1851 * Let the congestion control algorithm update 1852 * congestion control related information. This 1853 * typically means increasing the congestion 1854 * window. 1855 */ 1856 cc_ack_received(tp, th, CC_ACK); 1857 1858 tp->snd_una = th->th_ack; 1859 /* 1860 * Pull snd_wl2 up to prevent seq wrap relative 1861 * to th_ack. 1862 */ 1863 tp->snd_wl2 = th->th_ack; 1864 tp->t_dupacks = 0; 1865 m_freem(m); 1866 1867 /* 1868 * If all outstanding data are acked, stop 1869 * retransmit timer, otherwise restart timer 1870 * using current (possibly backed-off) value. 1871 * If process is waiting for space, 1872 * wakeup/selwakeup/signal. If data 1873 * are ready to send, let tcp_output 1874 * decide between more output or persist. 1875 */ 1876#ifdef TCPDEBUG 1877 if (so->so_options & SO_DEBUG) 1878 tcp_trace(TA_INPUT, ostate, tp, 1879 (void *)tcp_saveipgen, 1880 &tcp_savetcp, 0); 1881#endif 1882 TCP_PROBE3(debug__input, tp, th, m); 1883 if (tp->snd_una == tp->snd_max) 1884 tcp_timer_activate(tp, TT_REXMT, 0); 1885 else if (!tcp_timer_active(tp, TT_PERSIST)) 1886 tcp_timer_activate(tp, TT_REXMT, 1887 tp->t_rxtcur); 1888 sowwakeup(so); 1889 if (sbavail(&so->so_snd)) 1890 (void) tp->t_fb->tfb_tcp_output(tp); 1891 goto check_delack; 1892 } 1893 } else if (th->th_ack == tp->snd_una && 1894 tlen <= sbspace(&so->so_rcv)) { 1895 int newsize = 0; /* automatic sockbuf scaling */ 1896 1897 /* 1898 * This is a pure, in-sequence data packet with 1899 * nothing on the reassembly queue and we have enough 1900 * buffer space to take it. 1901 */ 1902 if (ti_locked == TI_RLOCKED) 1903 INP_INFO_RUNLOCK(&V_tcbinfo); 1904 ti_locked = TI_UNLOCKED; 1905 1906 /* Clean receiver SACK report if present */ 1907 if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 1908 tcp_clean_sackreport(tp); 1909 TCPSTAT_INC(tcps_preddat); 1910 tp->rcv_nxt += tlen; 1911 /* 1912 * Pull snd_wl1 up to prevent seq wrap relative to 1913 * th_seq. 1914 */ 1915 tp->snd_wl1 = th->th_seq; 1916 /* 1917 * Pull rcv_up up to prevent seq wrap relative to 1918 * rcv_nxt. 1919 */ 1920 tp->rcv_up = tp->rcv_nxt; 1921 TCPSTAT_INC(tcps_rcvpack); 1922 TCPSTAT_ADD(tcps_rcvbyte, tlen); 1923#ifdef TCPDEBUG 1924 if (so->so_options & SO_DEBUG) 1925 tcp_trace(TA_INPUT, ostate, tp, 1926 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1927#endif 1928 TCP_PROBE3(debug__input, tp, th, m); 1929 1930 newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 1931 1932 /* Add data to socket buffer. */ 1933 SOCKBUF_LOCK(&so->so_rcv); 1934 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1935 m_freem(m); 1936 } else { 1937 /* 1938 * Set new socket buffer size. 1939 * Give up when limit is reached. 1940 */ 1941 if (newsize) 1942 if (!sbreserve_locked(&so->so_rcv, 1943 newsize, so, NULL)) 1944 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1945 m_adj(m, drop_hdrlen); /* delayed header drop */ 1946 sbappendstream_locked(&so->so_rcv, m, 0); 1947 } 1948 /* NB: sorwakeup_locked() does an implicit unlock. */ 1949 sorwakeup_locked(so); 1950 if (DELAY_ACK(tp, tlen)) { 1951 tp->t_flags |= TF_DELACK; 1952 } else { 1953 tp->t_flags |= TF_ACKNOW; 1954 tp->t_fb->tfb_tcp_output(tp); 1955 } 1956 goto check_delack; 1957 } 1958 } 1959 1960 /* 1961 * Calculate amount of space in receive window, 1962 * and then do TCP input processing. 1963 * Receive window is amount of space in rcv queue, 1964 * but not less than advertised window. 1965 */ 1966 win = sbspace(&so->so_rcv); 1967 if (win < 0) 1968 win = 0; 1969 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1970 1971 switch (tp->t_state) { 1972 1973 /* 1974 * If the state is SYN_RECEIVED: 1975 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1976 */ 1977 case TCPS_SYN_RECEIVED: 1978 if ((thflags & TH_ACK) && 1979 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1980 SEQ_GT(th->th_ack, tp->snd_max))) { 1981 rstreason = BANDLIM_RST_OPENPORT; 1982 goto dropwithreset; 1983 } 1984#ifdef TCP_RFC7413 1985 if (tp->t_flags & TF_FASTOPEN) { 1986 /* 1987 * When a TFO connection is in SYN_RECEIVED, the 1988 * only valid packets are the initial SYN, a 1989 * retransmit/copy of the initial SYN (possibly with 1990 * a subset of the original data), a valid ACK, a 1991 * FIN, or a RST. 1992 */ 1993 if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 1994 rstreason = BANDLIM_RST_OPENPORT; 1995 goto dropwithreset; 1996 } else if (thflags & TH_SYN) { 1997 /* non-initial SYN is ignored */ 1998 if ((tcp_timer_active(tp, TT_DELACK) || 1999 tcp_timer_active(tp, TT_REXMT))) 2000 goto drop; 2001 } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 2002 goto drop; 2003 } 2004 } 2005#endif 2006 break; 2007 2008 /* 2009 * If the state is SYN_SENT: 2010 * if seg contains a RST, then drop the connection. 2011 * if seg does not contain SYN, then drop it. 2012 * Otherwise this is an acceptable SYN segment 2013 * initialize tp->rcv_nxt and tp->irs 2014 * if seg contains ack then advance tp->snd_una 2015 * if seg contains an ECE and ECN support is enabled, the stream 2016 * is ECN capable. 2017 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2018 * arrange for segment to be acked (eventually) 2019 * continue processing rest of data/controls, beginning with URG 2020 */ 2021 case TCPS_SYN_SENT: 2022 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 2023 TCP_PROBE5(connect__refused, NULL, tp, 2024 m, tp, th); 2025 tp = tcp_drop(tp, ECONNREFUSED); 2026 } 2027 if (thflags & TH_RST) 2028 goto drop; 2029 if (!(thflags & TH_SYN)) 2030 goto drop; 2031 2032 tp->irs = th->th_seq; 2033 tcp_rcvseqinit(tp); 2034 if (thflags & TH_ACK) { 2035 TCPSTAT_INC(tcps_connects); 2036 soisconnected(so); 2037#ifdef MAC 2038 mac_socketpeer_set_from_mbuf(m, so); 2039#endif 2040 /* Do window scaling on this connection? */ 2041 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2042 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2043 tp->rcv_scale = tp->request_r_scale; 2044 } 2045 tp->rcv_adv += imin(tp->rcv_wnd, 2046 TCP_MAXWIN << tp->rcv_scale); 2047 tp->snd_una++; /* SYN is acked */ 2048 /* 2049 * If there's data, delay ACK; if there's also a FIN 2050 * ACKNOW will be turned on later. 2051 */ 2052 if (DELAY_ACK(tp, tlen) && tlen != 0) 2053 tcp_timer_activate(tp, TT_DELACK, 2054 tcp_delacktime); 2055 else 2056 tp->t_flags |= TF_ACKNOW; 2057 2058 if ((thflags & TH_ECE) && V_tcp_do_ecn) { 2059 tp->t_flags |= TF_ECN_PERMIT; 2060 TCPSTAT_INC(tcps_ecn_shs); 2061 } 2062 2063 /* 2064 * Received <SYN,ACK> in SYN_SENT[*] state. 2065 * Transitions: 2066 * SYN_SENT --> ESTABLISHED 2067 * SYN_SENT* --> FIN_WAIT_1 2068 */ 2069 tp->t_starttime = ticks; 2070 if (tp->t_flags & TF_NEEDFIN) { 2071 tcp_state_change(tp, TCPS_FIN_WAIT_1); 2072 tp->t_flags &= ~TF_NEEDFIN; 2073 thflags &= ~TH_SYN; 2074 } else { 2075 tcp_state_change(tp, TCPS_ESTABLISHED); 2076 TCP_PROBE5(connect__established, NULL, tp, 2077 m, tp, th); 2078 cc_conn_init(tp); 2079 tcp_timer_activate(tp, TT_KEEP, 2080 TP_KEEPIDLE(tp)); 2081 } 2082 } else { 2083 /* 2084 * Received initial SYN in SYN-SENT[*] state => 2085 * simultaneous open. 2086 * If it succeeds, connection is * half-synchronized. 2087 * Otherwise, do 3-way handshake: 2088 * SYN-SENT -> SYN-RECEIVED 2089 * SYN-SENT* -> SYN-RECEIVED* 2090 */ 2091 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2092 tcp_timer_activate(tp, TT_REXMT, 0); 2093 tcp_state_change(tp, TCPS_SYN_RECEIVED); 2094 } 2095 2096 KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: " 2097 "ti_locked %d", __func__, ti_locked)); 2098 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2099 INP_WLOCK_ASSERT(tp->t_inpcb); 2100 2101 /* 2102 * Advance th->th_seq to correspond to first data byte. 2103 * If data, trim to stay within window, 2104 * dropping FIN if necessary. 2105 */ 2106 th->th_seq++; 2107 if (tlen > tp->rcv_wnd) { 2108 todrop = tlen - tp->rcv_wnd; 2109 m_adj(m, -todrop); 2110 tlen = tp->rcv_wnd; 2111 thflags &= ~TH_FIN; 2112 TCPSTAT_INC(tcps_rcvpackafterwin); 2113 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2114 } 2115 tp->snd_wl1 = th->th_seq - 1; 2116 tp->rcv_up = th->th_seq; 2117 /* 2118 * Client side of transaction: already sent SYN and data. 2119 * If the remote host used T/TCP to validate the SYN, 2120 * our data will be ACK'd; if so, enter normal data segment 2121 * processing in the middle of step 5, ack processing. 2122 * Otherwise, goto step 6. 2123 */ 2124 if (thflags & TH_ACK) 2125 goto process_ACK; 2126 2127 goto step6; 2128 2129 /* 2130 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2131 * do normal processing. 2132 * 2133 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2134 */ 2135 case TCPS_LAST_ACK: 2136 case TCPS_CLOSING: 2137 break; /* continue normal processing */ 2138 } 2139 2140 /* 2141 * States other than LISTEN or SYN_SENT. 2142 * First check the RST flag and sequence number since reset segments 2143 * are exempt from the timestamp and connection count tests. This 2144 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 2145 * below which allowed reset segments in half the sequence space 2146 * to fall though and be processed (which gives forged reset 2147 * segments with a random sequence number a 50 percent chance of 2148 * killing a connection). 2149 * Then check timestamp, if present. 2150 * Then check the connection count, if present. 2151 * Then check that at least some bytes of segment are within 2152 * receive window. If segment begins before rcv_nxt, 2153 * drop leading data (and SYN); if nothing left, just ack. 2154 */ 2155 if (thflags & TH_RST) { 2156 /* 2157 * RFC5961 Section 3.2 2158 * 2159 * - RST drops connection only if SEG.SEQ == RCV.NXT. 2160 * - If RST is in window, we send challenge ACK. 2161 * 2162 * Note: to take into account delayed ACKs, we should 2163 * test against last_ack_sent instead of rcv_nxt. 2164 * Note 2: we handle special case of closed window, not 2165 * covered by the RFC. 2166 */ 2167 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 2168 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 2169 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 2170 2171 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2172 KASSERT(ti_locked == TI_RLOCKED, 2173 ("%s: TH_RST ti_locked %d, th %p tp %p", 2174 __func__, ti_locked, th, tp)); 2175 KASSERT(tp->t_state != TCPS_SYN_SENT, 2176 ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 2177 __func__, th, tp)); 2178 2179 if (V_tcp_insecure_rst || 2180 tp->last_ack_sent == th->th_seq) { 2181 TCPSTAT_INC(tcps_drops); 2182 /* Drop the connection. */ 2183 switch (tp->t_state) { 2184 case TCPS_SYN_RECEIVED: 2185 so->so_error = ECONNREFUSED; 2186 goto close; 2187 case TCPS_ESTABLISHED: 2188 case TCPS_FIN_WAIT_1: 2189 case TCPS_FIN_WAIT_2: 2190 case TCPS_CLOSE_WAIT: 2191 case TCPS_CLOSING: 2192 case TCPS_LAST_ACK: 2193 so->so_error = ECONNRESET; 2194 close: 2195 /* FALLTHROUGH */ 2196 default: 2197 tp = tcp_close(tp); 2198 } 2199 } else { 2200 TCPSTAT_INC(tcps_badrst); 2201 /* Send challenge ACK. */ 2202 tcp_respond(tp, mtod(m, void *), th, m, 2203 tp->rcv_nxt, tp->snd_nxt, TH_ACK); 2204 tp->last_ack_sent = tp->rcv_nxt; 2205 m = NULL; 2206 } 2207 } 2208 goto drop; 2209 } 2210 2211 /* 2212 * RFC5961 Section 4.2 2213 * Send challenge ACK for any SYN in synchronized state. 2214 */ 2215 if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2216 tp->t_state != TCPS_SYN_RECEIVED) { 2217 KASSERT(ti_locked == TI_RLOCKED, 2218 ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2219 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2220 2221 TCPSTAT_INC(tcps_badsyn); 2222 if (V_tcp_insecure_syn && 2223 SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 2224 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 2225 tp = tcp_drop(tp, ECONNRESET); 2226 rstreason = BANDLIM_UNLIMITED; 2227 } else { 2228 /* Send challenge ACK. */ 2229 tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 2230 tp->snd_nxt, TH_ACK); 2231 tp->last_ack_sent = tp->rcv_nxt; 2232 m = NULL; 2233 } 2234 goto drop; 2235 } 2236 2237 /* 2238 * RFC 1323 PAWS: If we have a timestamp reply on this segment 2239 * and it's less than ts_recent, drop it. 2240 */ 2241 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 2242 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2243 2244 /* Check to see if ts_recent is over 24 days old. */ 2245 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2246 /* 2247 * Invalidate ts_recent. If this segment updates 2248 * ts_recent, the age will be reset later and ts_recent 2249 * will get a valid value. If it does not, setting 2250 * ts_recent to zero will at least satisfy the 2251 * requirement that zero be placed in the timestamp 2252 * echo reply when ts_recent isn't valid. The 2253 * age isn't reset until we get a valid ts_recent 2254 * because we don't want out-of-order segments to be 2255 * dropped when ts_recent is old. 2256 */ 2257 tp->ts_recent = 0; 2258 } else { 2259 TCPSTAT_INC(tcps_rcvduppack); 2260 TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 2261 TCPSTAT_INC(tcps_pawsdrop); 2262 if (tlen) 2263 goto dropafterack; 2264 goto drop; 2265 } 2266 } 2267 2268 /* 2269 * In the SYN-RECEIVED state, validate that the packet belongs to 2270 * this connection before trimming the data to fit the receive 2271 * window. Check the sequence number versus IRS since we know 2272 * the sequence numbers haven't wrapped. This is a partial fix 2273 * for the "LAND" DoS attack. 2274 */ 2275 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2276 rstreason = BANDLIM_RST_OPENPORT; 2277 goto dropwithreset; 2278 } 2279 2280 todrop = tp->rcv_nxt - th->th_seq; 2281 if (todrop > 0) { 2282 if (thflags & TH_SYN) { 2283 thflags &= ~TH_SYN; 2284 th->th_seq++; 2285 if (th->th_urp > 1) 2286 th->th_urp--; 2287 else 2288 thflags &= ~TH_URG; 2289 todrop--; 2290 } 2291 /* 2292 * Following if statement from Stevens, vol. 2, p. 960. 2293 */ 2294 if (todrop > tlen 2295 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2296 /* 2297 * Any valid FIN must be to the left of the window. 2298 * At this point the FIN must be a duplicate or out 2299 * of sequence; drop it. 2300 */ 2301 thflags &= ~TH_FIN; 2302 2303 /* 2304 * Send an ACK to resynchronize and drop any data. 2305 * But keep on processing for RST or ACK. 2306 */ 2307 tp->t_flags |= TF_ACKNOW; 2308 todrop = tlen; 2309 TCPSTAT_INC(tcps_rcvduppack); 2310 TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2311 } else { 2312 TCPSTAT_INC(tcps_rcvpartduppack); 2313 TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2314 } 2315 /* 2316 * DSACK - add SACK block for dropped range 2317 */ 2318 if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) { 2319 tcp_update_sack_list(tp, th->th_seq, 2320 th->th_seq + todrop); 2321 /* 2322 * ACK now, as the next in-sequence segment 2323 * will clear the DSACK block again 2324 */ 2325 tp->t_flags |= TF_ACKNOW; 2326 } 2327 drop_hdrlen += todrop; /* drop from the top afterwards */ 2328 th->th_seq += todrop; 2329 tlen -= todrop; 2330 if (th->th_urp > todrop) 2331 th->th_urp -= todrop; 2332 else { 2333 thflags &= ~TH_URG; 2334 th->th_urp = 0; 2335 } 2336 } 2337 2338 /* 2339 * If new data are received on a connection after the 2340 * user processes are gone, then RST the other end. 2341 */ 2342 if ((so->so_state & SS_NOFDREF) && 2343 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2344 KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && " 2345 "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2346 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2347 2348 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 2349 log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 2350 "after socket was closed, " 2351 "sending RST and removing tcpcb\n", 2352 s, __func__, tcpstates[tp->t_state], tlen); 2353 free(s, M_TCPLOG); 2354 } 2355 tp = tcp_close(tp); 2356 TCPSTAT_INC(tcps_rcvafterclose); 2357 rstreason = BANDLIM_UNLIMITED; 2358 goto dropwithreset; 2359 } 2360 2361 /* 2362 * If segment ends after window, drop trailing data 2363 * (and PUSH and FIN); if nothing left, just ACK. 2364 */ 2365 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2366 if (todrop > 0) { 2367 TCPSTAT_INC(tcps_rcvpackafterwin); 2368 if (todrop >= tlen) { 2369 TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2370 /* 2371 * If window is closed can only take segments at 2372 * window edge, and have to drop data and PUSH from 2373 * incoming segments. Continue processing, but 2374 * remember to ack. Otherwise, drop segment 2375 * and ack. 2376 */ 2377 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2378 tp->t_flags |= TF_ACKNOW; 2379 TCPSTAT_INC(tcps_rcvwinprobe); 2380 } else 2381 goto dropafterack; 2382 } else 2383 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2384 m_adj(m, -todrop); 2385 tlen -= todrop; 2386 thflags &= ~(TH_PUSH|TH_FIN); 2387 } 2388 2389 /* 2390 * If last ACK falls within this segment's sequence numbers, 2391 * record its timestamp. 2392 * NOTE: 2393 * 1) That the test incorporates suggestions from the latest 2394 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2395 * 2) That updating only on newer timestamps interferes with 2396 * our earlier PAWS tests, so this check should be solely 2397 * predicated on the sequence space of this segment. 2398 * 3) That we modify the segment boundary check to be 2399 * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2400 * instead of RFC1323's 2401 * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2402 * This modified check allows us to overcome RFC1323's 2403 * limitations as described in Stevens TCP/IP Illustrated 2404 * Vol. 2 p.869. In such cases, we can still calculate the 2405 * RTT correctly when RCV.NXT == Last.ACK.Sent. 2406 */ 2407 if ((to.to_flags & TOF_TS) != 0 && 2408 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2409 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2410 ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2411 tp->ts_recent_age = tcp_ts_getticks(); 2412 tp->ts_recent = to.to_tsval; 2413 } 2414 2415 /* 2416 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2417 * flag is on (half-synchronized state), then queue data for 2418 * later processing; else drop segment and return. 2419 */ 2420 if ((thflags & TH_ACK) == 0) { 2421 if (tp->t_state == TCPS_SYN_RECEIVED || 2422 (tp->t_flags & TF_NEEDSYN)) { 2423#ifdef TCP_RFC7413 2424 if (tp->t_state == TCPS_SYN_RECEIVED && 2425 tp->t_flags & TF_FASTOPEN) { 2426 tp->snd_wnd = tiwin; 2427 cc_conn_init(tp); 2428 } 2429#endif 2430 goto step6; 2431 } else if (tp->t_flags & TF_ACKNOW) 2432 goto dropafterack; 2433 else 2434 goto drop; 2435 } 2436 2437 /* 2438 * Ack processing. 2439 */ 2440 switch (tp->t_state) { 2441 2442 /* 2443 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2444 * ESTABLISHED state and continue processing. 2445 * The ACK was checked above. 2446 */ 2447 case TCPS_SYN_RECEIVED: 2448 2449 TCPSTAT_INC(tcps_connects); 2450 soisconnected(so); 2451 /* Do window scaling? */ 2452 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2453 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2454 tp->rcv_scale = tp->request_r_scale; 2455 tp->snd_wnd = tiwin; 2456 } 2457 /* 2458 * Make transitions: 2459 * SYN-RECEIVED -> ESTABLISHED 2460 * SYN-RECEIVED* -> FIN-WAIT-1 2461 */ 2462 tp->t_starttime = ticks; 2463 if (tp->t_flags & TF_NEEDFIN) { 2464 tcp_state_change(tp, TCPS_FIN_WAIT_1); 2465 tp->t_flags &= ~TF_NEEDFIN; 2466 } else { 2467 tcp_state_change(tp, TCPS_ESTABLISHED); 2468 TCP_PROBE5(accept__established, NULL, tp, 2469 m, tp, th); 2470#ifdef TCP_RFC7413 2471 if (tp->t_tfo_pending) { 2472 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2473 tp->t_tfo_pending = NULL; 2474 2475 /* 2476 * Account for the ACK of our SYN prior to 2477 * regular ACK processing below. 2478 */ 2479 tp->snd_una++; 2480 } 2481 /* 2482 * TFO connections call cc_conn_init() during SYN 2483 * processing. Calling it again here for such 2484 * connections is not harmless as it would undo the 2485 * snd_cwnd reduction that occurs when a TFO SYN|ACK 2486 * is retransmitted. 2487 */ 2488 if (!(tp->t_flags & TF_FASTOPEN)) 2489#endif 2490 cc_conn_init(tp); 2491 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 2492 } 2493 /* 2494 * If segment contains data or ACK, will call tcp_reass() 2495 * later; if not, do so now to pass queued data to user. 2496 */ 2497 if (tlen == 0 && (thflags & TH_FIN) == 0) 2498 (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 2499 (struct mbuf *)0); 2500 tp->snd_wl1 = th->th_seq - 1; 2501 /* FALLTHROUGH */ 2502 2503 /* 2504 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2505 * ACKs. If the ack is in the range 2506 * tp->snd_una < th->th_ack <= tp->snd_max 2507 * then advance tp->snd_una to th->th_ack and drop 2508 * data from the retransmission queue. If this ACK reflects 2509 * more up to date window information we update our window information. 2510 */ 2511 case TCPS_ESTABLISHED: 2512 case TCPS_FIN_WAIT_1: 2513 case TCPS_FIN_WAIT_2: 2514 case TCPS_CLOSE_WAIT: 2515 case TCPS_CLOSING: 2516 case TCPS_LAST_ACK: 2517 if (SEQ_GT(th->th_ack, tp->snd_max)) { 2518 TCPSTAT_INC(tcps_rcvacktoomuch); 2519 goto dropafterack; 2520 } 2521 if ((tp->t_flags & TF_SACK_PERMIT) && 2522 ((to.to_flags & TOF_SACK) || 2523 !TAILQ_EMPTY(&tp->snd_holes))) 2524 sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 2525 else 2526 /* 2527 * Reset the value so that previous (valid) value 2528 * from the last ack with SACK doesn't get used. 2529 */ 2530 tp->sackhint.sacked_bytes = 0; 2531 2532 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 2533 hhook_run_tcp_est_in(tp, th, &to); 2534 2535 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2536 u_int maxseg; 2537 2538 maxseg = tcp_maxseg(tp); 2539 if (tlen == 0 && 2540 (tiwin == tp->snd_wnd || 2541 (tp->t_flags & TF_SACK_PERMIT))) { 2542 /* 2543 * If this is the first time we've seen a 2544 * FIN from the remote, this is not a 2545 * duplicate and it needs to be processed 2546 * normally. This happens during a 2547 * simultaneous close. 2548 */ 2549 if ((thflags & TH_FIN) && 2550 (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 2551 tp->t_dupacks = 0; 2552 break; 2553 } 2554 TCPSTAT_INC(tcps_rcvdupack); 2555 /* 2556 * If we have outstanding data (other than 2557 * a window probe), this is a completely 2558 * duplicate ack (ie, window info didn't 2559 * change and FIN isn't set), 2560 * the ack is the biggest we've 2561 * seen and we've seen exactly our rexmt 2562 * threshold of them, assume a packet 2563 * has been dropped and retransmit it. 2564 * Kludge snd_nxt & the congestion 2565 * window so we send only this one 2566 * packet. 2567 * 2568 * We know we're losing at the current 2569 * window size so do congestion avoidance 2570 * (set ssthresh to half the current window 2571 * and pull our congestion window back to 2572 * the new ssthresh). 2573 * 2574 * Dup acks mean that packets have left the 2575 * network (they're now cached at the receiver) 2576 * so bump cwnd by the amount in the receiver 2577 * to keep a constant cwnd packets in the 2578 * network. 2579 * 2580 * When using TCP ECN, notify the peer that 2581 * we reduced the cwnd. 2582 */ 2583 /* 2584 * Following 2 kinds of acks should not affect 2585 * dupack counting: 2586 * 1) Old acks 2587 * 2) Acks with SACK but without any new SACK 2588 * information in them. These could result from 2589 * any anomaly in the network like a switch 2590 * duplicating packets or a possible DoS attack. 2591 */ 2592 if (th->th_ack != tp->snd_una || 2593 ((tp->t_flags & TF_SACK_PERMIT) && 2594 !sack_changed)) 2595 break; 2596 else if (!tcp_timer_active(tp, TT_REXMT)) 2597 tp->t_dupacks = 0; 2598 else if (++tp->t_dupacks > tcprexmtthresh || 2599 IN_FASTRECOVERY(tp->t_flags)) { 2600 cc_ack_received(tp, th, CC_DUPACK); 2601 if ((tp->t_flags & TF_SACK_PERMIT) && 2602 IN_FASTRECOVERY(tp->t_flags)) { 2603 int awnd; 2604 2605 /* 2606 * Compute the amount of data in flight first. 2607 * We can inject new data into the pipe iff 2608 * we have less than 1/2 the original window's 2609 * worth of data in flight. 2610 */ 2611 if (V_tcp_do_rfc6675_pipe) 2612 awnd = tcp_compute_pipe(tp); 2613 else 2614 awnd = (tp->snd_nxt - tp->snd_fack) + 2615 tp->sackhint.sack_bytes_rexmit; 2616 2617 if (awnd < tp->snd_ssthresh) { 2618 tp->snd_cwnd += maxseg; 2619 if (tp->snd_cwnd > tp->snd_ssthresh) 2620 tp->snd_cwnd = tp->snd_ssthresh; 2621 } 2622 } else 2623 tp->snd_cwnd += maxseg; 2624 (void) tp->t_fb->tfb_tcp_output(tp); 2625 goto drop; 2626 } else if (tp->t_dupacks == tcprexmtthresh) { 2627 tcp_seq onxt = tp->snd_nxt; 2628 2629 /* 2630 * If we're doing sack, check to 2631 * see if we're already in sack 2632 * recovery. If we're not doing sack, 2633 * check to see if we're in newreno 2634 * recovery. 2635 */ 2636 if (tp->t_flags & TF_SACK_PERMIT) { 2637 if (IN_FASTRECOVERY(tp->t_flags)) { 2638 tp->t_dupacks = 0; 2639 break; 2640 } 2641 } else { 2642 if (SEQ_LEQ(th->th_ack, 2643 tp->snd_recover)) { 2644 tp->t_dupacks = 0; 2645 break; 2646 } 2647 } 2648 /* Congestion signal before ack. */ 2649 cc_cong_signal(tp, th, CC_NDUPACK); 2650 cc_ack_received(tp, th, CC_DUPACK); 2651 tcp_timer_activate(tp, TT_REXMT, 0); 2652 tp->t_rtttime = 0; 2653 if (tp->t_flags & TF_SACK_PERMIT) { 2654 TCPSTAT_INC( 2655 tcps_sack_recovery_episode); 2656 tp->sack_newdata = tp->snd_nxt; 2657 tp->snd_cwnd = maxseg; 2658 (void) tp->t_fb->tfb_tcp_output(tp); 2659 goto drop; 2660 } 2661 tp->snd_nxt = th->th_ack; 2662 tp->snd_cwnd = maxseg; 2663 (void) tp->t_fb->tfb_tcp_output(tp); 2664 KASSERT(tp->snd_limited <= 2, 2665 ("%s: tp->snd_limited too big", 2666 __func__)); 2667 tp->snd_cwnd = tp->snd_ssthresh + 2668 maxseg * 2669 (tp->t_dupacks - tp->snd_limited); 2670 if (SEQ_GT(onxt, tp->snd_nxt)) 2671 tp->snd_nxt = onxt; 2672 goto drop; 2673 } else if (V_tcp_do_rfc3042) { 2674 /* 2675 * Process first and second duplicate 2676 * ACKs. Each indicates a segment 2677 * leaving the network, creating room 2678 * for more. Make sure we can send a 2679 * packet on reception of each duplicate 2680 * ACK by increasing snd_cwnd by one 2681 * segment. Restore the original 2682 * snd_cwnd after packet transmission. 2683 */ 2684 cc_ack_received(tp, th, CC_DUPACK); 2685 u_long oldcwnd = tp->snd_cwnd; 2686 tcp_seq oldsndmax = tp->snd_max; 2687 u_int sent; 2688 int avail; 2689 2690 KASSERT(tp->t_dupacks == 1 || 2691 tp->t_dupacks == 2, 2692 ("%s: dupacks not 1 or 2", 2693 __func__)); 2694 if (tp->t_dupacks == 1) 2695 tp->snd_limited = 0; 2696 tp->snd_cwnd = 2697 (tp->snd_nxt - tp->snd_una) + 2698 (tp->t_dupacks - tp->snd_limited) * 2699 maxseg; 2700 /* 2701 * Only call tcp_output when there 2702 * is new data available to be sent. 2703 * Otherwise we would send pure ACKs. 2704 */ 2705 SOCKBUF_LOCK(&so->so_snd); 2706 avail = sbavail(&so->so_snd) - 2707 (tp->snd_nxt - tp->snd_una); 2708 SOCKBUF_UNLOCK(&so->so_snd); 2709 if (avail > 0) 2710 (void) tp->t_fb->tfb_tcp_output(tp); 2711 sent = tp->snd_max - oldsndmax; 2712 if (sent > maxseg) { 2713 KASSERT((tp->t_dupacks == 2 && 2714 tp->snd_limited == 0) || 2715 (sent == maxseg + 1 && 2716 tp->t_flags & TF_SENTFIN), 2717 ("%s: sent too much", 2718 __func__)); 2719 tp->snd_limited = 2; 2720 } else if (sent > 0) 2721 ++tp->snd_limited; 2722 tp->snd_cwnd = oldcwnd; 2723 goto drop; 2724 } 2725 } 2726 break; 2727 } else { 2728 /* 2729 * This ack is advancing the left edge, reset the 2730 * counter. 2731 */ 2732 tp->t_dupacks = 0; 2733 /* 2734 * If this ack also has new SACK info, increment the 2735 * counter as per rfc6675. 2736 */ 2737 if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed) 2738 tp->t_dupacks++; 2739 } 2740 2741 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2742 ("%s: th_ack <= snd_una", __func__)); 2743 2744 /* 2745 * If the congestion window was inflated to account 2746 * for the other side's cached packets, retract it. 2747 */ 2748 if (IN_FASTRECOVERY(tp->t_flags)) { 2749 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 2750 if (tp->t_flags & TF_SACK_PERMIT) 2751 tcp_sack_partialack(tp, th); 2752 else 2753 tcp_newreno_partial_ack(tp, th); 2754 } else 2755 cc_post_recovery(tp, th); 2756 } 2757 /* 2758 * If we reach this point, ACK is not a duplicate, 2759 * i.e., it ACKs something we sent. 2760 */ 2761 if (tp->t_flags & TF_NEEDSYN) { 2762 /* 2763 * T/TCP: Connection was half-synchronized, and our 2764 * SYN has been ACK'd (so connection is now fully 2765 * synchronized). Go to non-starred state, 2766 * increment snd_una for ACK of SYN, and check if 2767 * we can do window scaling. 2768 */ 2769 tp->t_flags &= ~TF_NEEDSYN; 2770 tp->snd_una++; 2771 /* Do window scaling? */ 2772 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2773 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2774 tp->rcv_scale = tp->request_r_scale; 2775 /* Send window already scaled. */ 2776 } 2777 } 2778 2779process_ACK: 2780 INP_WLOCK_ASSERT(tp->t_inpcb); 2781 2782 acked = BYTES_THIS_ACK(tp, th); 2783 KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2784 "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2785 tp->snd_una, th->th_ack, tp, m)); 2786 TCPSTAT_INC(tcps_rcvackpack); 2787 TCPSTAT_ADD(tcps_rcvackbyte, acked); 2788 2789 /* 2790 * If we just performed our first retransmit, and the ACK 2791 * arrives within our recovery window, then it was a mistake 2792 * to do the retransmit in the first place. Recover our 2793 * original cwnd and ssthresh, and proceed to transmit where 2794 * we left off. 2795 */ 2796 if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2797 (int)(ticks - tp->t_badrxtwin) < 0) 2798 cc_cong_signal(tp, th, CC_RTO_ERR); 2799 2800 /* 2801 * If we have a timestamp reply, update smoothed 2802 * round trip time. If no timestamp is present but 2803 * transmit timer is running and timed sequence 2804 * number was acked, update smoothed round trip time. 2805 * Since we now have an rtt measurement, cancel the 2806 * timer backoff (cf., Phil Karn's retransmit alg.). 2807 * Recompute the initial retransmit timer. 2808 * 2809 * Some boxes send broken timestamp replies 2810 * during the SYN+ACK phase, ignore 2811 * timestamps of 0 or we could calculate a 2812 * huge RTT and blow up the retransmit timer. 2813 */ 2814 if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 2815 u_int t; 2816 2817 t = tcp_ts_getticks() - to.to_tsecr; 2818 if (!tp->t_rttlow || tp->t_rttlow > t) 2819 tp->t_rttlow = t; 2820 tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2821 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2822 if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2823 tp->t_rttlow = ticks - tp->t_rtttime; 2824 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2825 } 2826 2827 /* 2828 * If all outstanding data is acked, stop retransmit 2829 * timer and remember to restart (more output or persist). 2830 * If there is more data to be acked, restart retransmit 2831 * timer, using current (possibly backed-off) value. 2832 */ 2833 if (th->th_ack == tp->snd_max) { 2834 tcp_timer_activate(tp, TT_REXMT, 0); 2835 needoutput = 1; 2836 } else if (!tcp_timer_active(tp, TT_PERSIST)) 2837 tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2838 2839 /* 2840 * If no data (only SYN) was ACK'd, 2841 * skip rest of ACK processing. 2842 */ 2843 if (acked == 0) 2844 goto step6; 2845 2846 /* 2847 * Let the congestion control algorithm update congestion 2848 * control related information. This typically means increasing 2849 * the congestion window. 2850 */ 2851 cc_ack_received(tp, th, CC_ACK); 2852 2853 SOCKBUF_LOCK(&so->so_snd); 2854 if (acked > sbavail(&so->so_snd)) { 2855 if (tp->snd_wnd >= sbavail(&so->so_snd)) 2856 tp->snd_wnd -= sbavail(&so->so_snd); 2857 else 2858 tp->snd_wnd = 0; 2859 mfree = sbcut_locked(&so->so_snd, 2860 (int)sbavail(&so->so_snd)); 2861 ourfinisacked = 1; 2862 } else { 2863 mfree = sbcut_locked(&so->so_snd, acked); 2864 if (tp->snd_wnd >= (u_long) acked) 2865 tp->snd_wnd -= acked; 2866 else 2867 tp->snd_wnd = 0; 2868 ourfinisacked = 0; 2869 } 2870 /* NB: sowwakeup_locked() does an implicit unlock. */ 2871 sowwakeup_locked(so); 2872 m_freem(mfree); 2873 /* Detect una wraparound. */ 2874 if (!IN_RECOVERY(tp->t_flags) && 2875 SEQ_GT(tp->snd_una, tp->snd_recover) && 2876 SEQ_LEQ(th->th_ack, tp->snd_recover)) 2877 tp->snd_recover = th->th_ack - 1; 2878 /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2879 if (IN_RECOVERY(tp->t_flags) && 2880 SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2881 EXIT_RECOVERY(tp->t_flags); 2882 } 2883 tp->snd_una = th->th_ack; 2884 if (tp->t_flags & TF_SACK_PERMIT) { 2885 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 2886 tp->snd_recover = tp->snd_una; 2887 } 2888 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2889 tp->snd_nxt = tp->snd_una; 2890 2891 switch (tp->t_state) { 2892 2893 /* 2894 * In FIN_WAIT_1 STATE in addition to the processing 2895 * for the ESTABLISHED state if our FIN is now acknowledged 2896 * then enter FIN_WAIT_2. 2897 */ 2898 case TCPS_FIN_WAIT_1: 2899 if (ourfinisacked) { 2900 /* 2901 * If we can't receive any more 2902 * data, then closing user can proceed. 2903 * Starting the timer is contrary to the 2904 * specification, but if we don't get a FIN 2905 * we'll hang forever. 2906 * 2907 * XXXjl: 2908 * we should release the tp also, and use a 2909 * compressed state. 2910 */ 2911 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 2912 soisdisconnected(so); 2913 tcp_timer_activate(tp, TT_2MSL, 2914 (tcp_fast_finwait2_recycle ? 2915 tcp_finwait2_timeout : 2916 TP_MAXIDLE(tp))); 2917 } 2918 tcp_state_change(tp, TCPS_FIN_WAIT_2); 2919 } 2920 break; 2921 2922 /* 2923 * In CLOSING STATE in addition to the processing for 2924 * the ESTABLISHED state if the ACK acknowledges our FIN 2925 * then enter the TIME-WAIT state, otherwise ignore 2926 * the segment. 2927 */ 2928 case TCPS_CLOSING: 2929 if (ourfinisacked) { 2930 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2931 tcp_twstart(tp); 2932 INP_INFO_RUNLOCK(&V_tcbinfo); 2933 m_freem(m); 2934 return; 2935 } 2936 break; 2937 2938 /* 2939 * In LAST_ACK, we may still be waiting for data to drain 2940 * and/or to be acked, as well as for the ack of our FIN. 2941 * If our FIN is now acknowledged, delete the TCB, 2942 * enter the closed state and return. 2943 */ 2944 case TCPS_LAST_ACK: 2945 if (ourfinisacked) { 2946 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2947 tp = tcp_close(tp); 2948 goto drop; 2949 } 2950 break; 2951 } 2952 } 2953 2954step6: 2955 INP_WLOCK_ASSERT(tp->t_inpcb); 2956 2957 /* 2958 * Update window information. 2959 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2960 */ 2961 if ((thflags & TH_ACK) && 2962 (SEQ_LT(tp->snd_wl1, th->th_seq) || 2963 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2964 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2965 /* keep track of pure window updates */ 2966 if (tlen == 0 && 2967 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2968 TCPSTAT_INC(tcps_rcvwinupd); 2969 tp->snd_wnd = tiwin; 2970 tp->snd_wl1 = th->th_seq; 2971 tp->snd_wl2 = th->th_ack; 2972 if (tp->snd_wnd > tp->max_sndwnd) 2973 tp->max_sndwnd = tp->snd_wnd; 2974 needoutput = 1; 2975 } 2976 2977 /* 2978 * Process segments with URG. 2979 */ 2980 if ((thflags & TH_URG) && th->th_urp && 2981 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2982 /* 2983 * This is a kludge, but if we receive and accept 2984 * random urgent pointers, we'll crash in 2985 * soreceive. It's hard to imagine someone 2986 * actually wanting to send this much urgent data. 2987 */ 2988 SOCKBUF_LOCK(&so->so_rcv); 2989 if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 2990 th->th_urp = 0; /* XXX */ 2991 thflags &= ~TH_URG; /* XXX */ 2992 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2993 goto dodata; /* XXX */ 2994 } 2995 /* 2996 * If this segment advances the known urgent pointer, 2997 * then mark the data stream. This should not happen 2998 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2999 * a FIN has been received from the remote side. 3000 * In these states we ignore the URG. 3001 * 3002 * According to RFC961 (Assigned Protocols), 3003 * the urgent pointer points to the last octet 3004 * of urgent data. We continue, however, 3005 * to consider it to indicate the first octet 3006 * of data past the urgent section as the original 3007 * spec states (in one of two places). 3008 */ 3009 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 3010 tp->rcv_up = th->th_seq + th->th_urp; 3011 so->so_oobmark = sbavail(&so->so_rcv) + 3012 (tp->rcv_up - tp->rcv_nxt) - 1; 3013 if (so->so_oobmark == 0) 3014 so->so_rcv.sb_state |= SBS_RCVATMARK; 3015 sohasoutofband(so); 3016 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 3017 } 3018 SOCKBUF_UNLOCK(&so->so_rcv); 3019 /* 3020 * Remove out of band data so doesn't get presented to user. 3021 * This can happen independent of advancing the URG pointer, 3022 * but if two URG's are pending at once, some out-of-band 3023 * data may creep in... ick. 3024 */ 3025 if (th->th_urp <= (u_long)tlen && 3026 !(so->so_options & SO_OOBINLINE)) { 3027 /* hdr drop is delayed */ 3028 tcp_pulloutofband(so, th, m, drop_hdrlen); 3029 } 3030 } else { 3031 /* 3032 * If no out of band data is expected, 3033 * pull receive urgent pointer along 3034 * with the receive window. 3035 */ 3036 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 3037 tp->rcv_up = tp->rcv_nxt; 3038 } 3039dodata: /* XXX */ 3040 INP_WLOCK_ASSERT(tp->t_inpcb); 3041 3042 /* 3043 * Process the segment text, merging it into the TCP sequencing queue, 3044 * and arranging for acknowledgment of receipt if necessary. 3045 * This process logically involves adjusting tp->rcv_wnd as data 3046 * is presented to the user (this happens in tcp_usrreq.c, 3047 * case PRU_RCVD). If a FIN has already been received on this 3048 * connection then we just ignore the text. 3049 */ 3050 tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 3051 (tp->t_flags & TF_FASTOPEN)); 3052 if ((tlen || (thflags & TH_FIN) || tfo_syn) && 3053 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3054 tcp_seq save_start = th->th_seq; 3055 tcp_seq save_rnxt = tp->rcv_nxt; 3056 int save_tlen = tlen; 3057 m_adj(m, drop_hdrlen); /* delayed header drop */ 3058 /* 3059 * Insert segment which includes th into TCP reassembly queue 3060 * with control block tp. Set thflags to whether reassembly now 3061 * includes a segment with FIN. This handles the common case 3062 * inline (segment is the next to be received on an established 3063 * connection, and the queue is empty), avoiding linkage into 3064 * and removal from the queue and repetition of various 3065 * conversions. 3066 * Set DELACK for segments received in order, but ack 3067 * immediately when segments are out of order (so 3068 * fast retransmit can work). 3069 */ 3070 if (th->th_seq == tp->rcv_nxt && 3071 SEGQ_EMPTY(tp) && 3072 (TCPS_HAVEESTABLISHED(tp->t_state) || 3073 tfo_syn)) { 3074 if (DELAY_ACK(tp, tlen) || tfo_syn) 3075 tp->t_flags |= TF_DELACK; 3076 else 3077 tp->t_flags |= TF_ACKNOW; 3078 tp->rcv_nxt += tlen; 3079 thflags = th->th_flags & TH_FIN; 3080 TCPSTAT_INC(tcps_rcvpack); 3081 TCPSTAT_ADD(tcps_rcvbyte, tlen); 3082 SOCKBUF_LOCK(&so->so_rcv); 3083 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3084 m_freem(m); 3085 else 3086 sbappendstream_locked(&so->so_rcv, m, 0); 3087 /* NB: sorwakeup_locked() does an implicit unlock. */ 3088 sorwakeup_locked(so); 3089 } else { 3090 /* 3091 * XXX: Due to the header drop above "th" is 3092 * theoretically invalid by now. Fortunately 3093 * m_adj() doesn't actually frees any mbufs 3094 * when trimming from the head. 3095 */ 3096 tcp_seq temp = save_start; 3097 thflags = tcp_reass(tp, th, &temp, &tlen, m); 3098 tp->t_flags |= TF_ACKNOW; 3099 } 3100 if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) { 3101 if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 3102 /* 3103 * DSACK actually handled in the fastpath 3104 * above. 3105 */ 3106 tcp_update_sack_list(tp, save_start, 3107 save_start + save_tlen); 3108 } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 3109 if ((tp->rcv_numsacks >= 1) && 3110 (tp->sackblks[0].end == save_start)) { 3111 /* 3112 * Partial overlap, recorded at todrop 3113 * above. 3114 */ 3115 tcp_update_sack_list(tp, 3116 tp->sackblks[0].start, 3117 tp->sackblks[0].end); 3118 } else { 3119 tcp_update_dsack_list(tp, save_start, 3120 save_start + save_tlen); 3121 } 3122 } else if (tlen >= save_tlen) { 3123 /* Update of sackblks. */ 3124 tcp_update_dsack_list(tp, save_start, 3125 save_start + save_tlen); 3126 } else if (tlen > 0) { 3127 tcp_update_dsack_list(tp, save_start, 3128 save_start + tlen); 3129 } 3130 } 3131#if 0 3132 /* 3133 * Note the amount of data that peer has sent into 3134 * our window, in order to estimate the sender's 3135 * buffer size. 3136 * XXX: Unused. 3137 */ 3138 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3139 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3140 else 3141 len = so->so_rcv.sb_hiwat; 3142#endif 3143 } else { 3144 m_freem(m); 3145 thflags &= ~TH_FIN; 3146 } 3147 3148 /* 3149 * If FIN is received ACK the FIN and let the user know 3150 * that the connection is closing. 3151 */ 3152 if (thflags & TH_FIN) { 3153 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3154 socantrcvmore(so); 3155 /* 3156 * If connection is half-synchronized 3157 * (ie NEEDSYN flag on) then delay ACK, 3158 * so it may be piggybacked when SYN is sent. 3159 * Otherwise, since we received a FIN then no 3160 * more input can be expected, send ACK now. 3161 */ 3162 if (tp->t_flags & TF_NEEDSYN) 3163 tp->t_flags |= TF_DELACK; 3164 else 3165 tp->t_flags |= TF_ACKNOW; 3166 tp->rcv_nxt++; 3167 } 3168 switch (tp->t_state) { 3169 3170 /* 3171 * In SYN_RECEIVED and ESTABLISHED STATES 3172 * enter the CLOSE_WAIT state. 3173 */ 3174 case TCPS_SYN_RECEIVED: 3175 tp->t_starttime = ticks; 3176 /* FALLTHROUGH */ 3177 case TCPS_ESTABLISHED: 3178 tcp_state_change(tp, TCPS_CLOSE_WAIT); 3179 break; 3180 3181 /* 3182 * If still in FIN_WAIT_1 STATE FIN has not been acked so 3183 * enter the CLOSING state. 3184 */ 3185 case TCPS_FIN_WAIT_1: 3186 tcp_state_change(tp, TCPS_CLOSING); 3187 break; 3188 3189 /* 3190 * In FIN_WAIT_2 state enter the TIME_WAIT state, 3191 * starting the time-wait timer, turning off the other 3192 * standard timers. 3193 */ 3194 case TCPS_FIN_WAIT_2: 3195 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 3196 KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata " 3197 "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 3198 ti_locked)); 3199 3200 tcp_twstart(tp); 3201 INP_INFO_RUNLOCK(&V_tcbinfo); 3202 return; 3203 } 3204 } 3205 if (ti_locked == TI_RLOCKED) 3206 INP_INFO_RUNLOCK(&V_tcbinfo); 3207 ti_locked = TI_UNLOCKED; 3208 3209#ifdef TCPDEBUG 3210 if (so->so_options & SO_DEBUG) 3211 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3212 &tcp_savetcp, 0); 3213#endif 3214 TCP_PROBE3(debug__input, tp, th, m); 3215 3216 /* 3217 * Return any desired output. 3218 */ 3219 if (needoutput || (tp->t_flags & TF_ACKNOW)) 3220 (void) tp->t_fb->tfb_tcp_output(tp); 3221 3222check_delack: 3223 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 3224 __func__, ti_locked)); 3225 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3226 INP_WLOCK_ASSERT(tp->t_inpcb); 3227 3228 if (tp->t_flags & TF_DELACK) { 3229 tp->t_flags &= ~TF_DELACK; 3230 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 3231 } 3232 INP_WUNLOCK(tp->t_inpcb); 3233 return; 3234 3235dropafterack: 3236 /* 3237 * Generate an ACK dropping incoming segment if it occupies 3238 * sequence space, where the ACK reflects our state. 3239 * 3240 * We can now skip the test for the RST flag since all 3241 * paths to this code happen after packets containing 3242 * RST have been dropped. 3243 * 3244 * In the SYN-RECEIVED state, don't send an ACK unless the 3245 * segment we received passes the SYN-RECEIVED ACK test. 3246 * If it fails send a RST. This breaks the loop in the 3247 * "LAND" DoS attack, and also prevents an ACK storm 3248 * between two listening ports that have been sent forged 3249 * SYN segments, each with the source address of the other. 3250 */ 3251 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3252 (SEQ_GT(tp->snd_una, th->th_ack) || 3253 SEQ_GT(th->th_ack, tp->snd_max)) ) { 3254 rstreason = BANDLIM_RST_OPENPORT; 3255 goto dropwithreset; 3256 } 3257#ifdef TCPDEBUG 3258 if (so->so_options & SO_DEBUG) 3259 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3260 &tcp_savetcp, 0); 3261#endif 3262 TCP_PROBE3(debug__input, tp, th, m); 3263 if (ti_locked == TI_RLOCKED) 3264 INP_INFO_RUNLOCK(&V_tcbinfo); 3265 ti_locked = TI_UNLOCKED; 3266 3267 tp->t_flags |= TF_ACKNOW; 3268 (void) tp->t_fb->tfb_tcp_output(tp); 3269 INP_WUNLOCK(tp->t_inpcb); 3270 m_freem(m); 3271 return; 3272 3273dropwithreset: 3274 if (ti_locked == TI_RLOCKED) 3275 INP_INFO_RUNLOCK(&V_tcbinfo); 3276 ti_locked = TI_UNLOCKED; 3277 3278 if (tp != NULL) { 3279 tcp_dropwithreset(m, th, tp, tlen, rstreason); 3280 INP_WUNLOCK(tp->t_inpcb); 3281 } else 3282 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3283 return; 3284 3285drop: 3286 if (ti_locked == TI_RLOCKED) { 3287 INP_INFO_RUNLOCK(&V_tcbinfo); 3288 ti_locked = TI_UNLOCKED; 3289 } 3290#ifdef INVARIANTS 3291 else 3292 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3293#endif 3294 3295 /* 3296 * Drop space held by incoming segment and return. 3297 */ 3298#ifdef TCPDEBUG 3299 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3300 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3301 &tcp_savetcp, 0); 3302#endif 3303 TCP_PROBE3(debug__input, tp, th, m); 3304 if (tp != NULL) 3305 INP_WUNLOCK(tp->t_inpcb); 3306 m_freem(m); 3307} 3308 3309/* 3310 * Issue RST and make ACK acceptable to originator of segment. 3311 * The mbuf must still include the original packet header. 3312 * tp may be NULL. 3313 */ 3314void 3315tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3316 int tlen, int rstreason) 3317{ 3318#ifdef INET 3319 struct ip *ip; 3320#endif 3321#ifdef INET6 3322 struct ip6_hdr *ip6; 3323#endif 3324 3325 if (tp != NULL) { 3326 INP_WLOCK_ASSERT(tp->t_inpcb); 3327 } 3328 3329 /* Don't bother if destination was broadcast/multicast. */ 3330 if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3331 goto drop; 3332#ifdef INET6 3333 if (mtod(m, struct ip *)->ip_v == 6) { 3334 ip6 = mtod(m, struct ip6_hdr *); 3335 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3336 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3337 goto drop; 3338 /* IPv6 anycast check is done at tcp6_input() */ 3339 } 3340#endif 3341#if defined(INET) && defined(INET6) 3342 else 3343#endif 3344#ifdef INET 3345 { 3346 ip = mtod(m, struct ip *); 3347 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3348 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3349 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3350 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3351 goto drop; 3352 } 3353#endif 3354 3355 /* Perform bandwidth limiting. */ 3356 if (badport_bandlim(rstreason) < 0) 3357 goto drop; 3358 3359 /* tcp_respond consumes the mbuf chain. */ 3360 if (th->th_flags & TH_ACK) { 3361 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3362 th->th_ack, TH_RST); 3363 } else { 3364 if (th->th_flags & TH_SYN) 3365 tlen++; 3366 if (th->th_flags & TH_FIN) 3367 tlen++; 3368 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3369 (tcp_seq)0, TH_RST|TH_ACK); 3370 } 3371 return; 3372drop: 3373 m_freem(m); 3374} 3375 3376/* 3377 * Parse TCP options and place in tcpopt. 3378 */ 3379void 3380tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3381{ 3382 int opt, optlen; 3383 3384 to->to_flags = 0; 3385 for (; cnt > 0; cnt -= optlen, cp += optlen) { 3386 opt = cp[0]; 3387 if (opt == TCPOPT_EOL) 3388 break; 3389 if (opt == TCPOPT_NOP) 3390 optlen = 1; 3391 else { 3392 if (cnt < 2) 3393 break; 3394 optlen = cp[1]; 3395 if (optlen < 2 || optlen > cnt) 3396 break; 3397 } 3398 switch (opt) { 3399 case TCPOPT_MAXSEG: 3400 if (optlen != TCPOLEN_MAXSEG) 3401 continue; 3402 if (!(flags & TO_SYN)) 3403 continue; 3404 to->to_flags |= TOF_MSS; 3405 bcopy((char *)cp + 2, 3406 (char *)&to->to_mss, sizeof(to->to_mss)); 3407 to->to_mss = ntohs(to->to_mss); 3408 break; 3409 case TCPOPT_WINDOW: 3410 if (optlen != TCPOLEN_WINDOW) 3411 continue; 3412 if (!(flags & TO_SYN)) 3413 continue; 3414 to->to_flags |= TOF_SCALE; 3415 to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3416 break; 3417 case TCPOPT_TIMESTAMP: 3418 if (optlen != TCPOLEN_TIMESTAMP) 3419 continue; 3420 to->to_flags |= TOF_TS; 3421 bcopy((char *)cp + 2, 3422 (char *)&to->to_tsval, sizeof(to->to_tsval)); 3423 to->to_tsval = ntohl(to->to_tsval); 3424 bcopy((char *)cp + 6, 3425 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3426 to->to_tsecr = ntohl(to->to_tsecr); 3427 break; 3428 case TCPOPT_SIGNATURE: 3429 /* 3430 * In order to reply to a host which has set the 3431 * TCP_SIGNATURE option in its initial SYN, we have 3432 * to record the fact that the option was observed 3433 * here for the syncache code to perform the correct 3434 * response. 3435 */ 3436 if (optlen != TCPOLEN_SIGNATURE) 3437 continue; 3438 to->to_flags |= TOF_SIGNATURE; 3439 to->to_signature = cp + 2; 3440 break; 3441 case TCPOPT_SACK_PERMITTED: 3442 if (optlen != TCPOLEN_SACK_PERMITTED) 3443 continue; 3444 if (!(flags & TO_SYN)) 3445 continue; 3446 if (!V_tcp_do_sack) 3447 continue; 3448 to->to_flags |= TOF_SACKPERM; 3449 break; 3450 case TCPOPT_SACK: 3451 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 3452 continue; 3453 if (flags & TO_SYN) 3454 continue; 3455 to->to_flags |= TOF_SACK; 3456 to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 3457 to->to_sacks = cp + 2; 3458 TCPSTAT_INC(tcps_sack_rcv_blocks); 3459 break; 3460#ifdef TCP_RFC7413 3461 case TCPOPT_FAST_OPEN: 3462 if ((optlen != TCPOLEN_FAST_OPEN_EMPTY) && 3463 (optlen < TCPOLEN_FAST_OPEN_MIN) && 3464 (optlen > TCPOLEN_FAST_OPEN_MAX)) 3465 continue; 3466 if (!(flags & TO_SYN)) 3467 continue; 3468 if (!V_tcp_fastopen_enabled) 3469 continue; 3470 to->to_flags |= TOF_FASTOPEN; 3471 to->to_tfo_len = optlen - 2; 3472 to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3473 break; 3474#endif 3475 default: 3476 continue; 3477 } 3478 } 3479} 3480 3481/* 3482 * Pull out of band byte out of a segment so 3483 * it doesn't appear in the user's data queue. 3484 * It is still reflected in the segment length for 3485 * sequencing purposes. 3486 */ 3487void 3488tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3489 int off) 3490{ 3491 int cnt = off + th->th_urp - 1; 3492 3493 while (cnt >= 0) { 3494 if (m->m_len > cnt) { 3495 char *cp = mtod(m, caddr_t) + cnt; 3496 struct tcpcb *tp = sototcpcb(so); 3497 3498 INP_WLOCK_ASSERT(tp->t_inpcb); 3499 3500 tp->t_iobc = *cp; 3501 tp->t_oobflags |= TCPOOB_HAVEDATA; 3502 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3503 m->m_len--; 3504 if (m->m_flags & M_PKTHDR) 3505 m->m_pkthdr.len--; 3506 return; 3507 } 3508 cnt -= m->m_len; 3509 m = m->m_next; 3510 if (m == NULL) 3511 break; 3512 } 3513 panic("tcp_pulloutofband"); 3514} 3515 3516/* 3517 * Collect new round-trip time estimate 3518 * and update averages and current timeout. 3519 */ 3520void 3521tcp_xmit_timer(struct tcpcb *tp, int rtt) 3522{ 3523 int delta; 3524 3525 INP_WLOCK_ASSERT(tp->t_inpcb); 3526 3527 TCPSTAT_INC(tcps_rttupdated); 3528 tp->t_rttupdated++; 3529 if (tp->t_srtt != 0) { 3530 /* 3531 * srtt is stored as fixed point with 5 bits after the 3532 * binary point (i.e., scaled by 8). The following magic 3533 * is equivalent to the smoothing algorithm in rfc793 with 3534 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3535 * point). Adjust rtt to origin 0. 3536 */ 3537 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3538 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3539 3540 if ((tp->t_srtt += delta) <= 0) 3541 tp->t_srtt = 1; 3542 3543 /* 3544 * We accumulate a smoothed rtt variance (actually, a 3545 * smoothed mean difference), then set the retransmit 3546 * timer to smoothed rtt + 4 times the smoothed variance. 3547 * rttvar is stored as fixed point with 4 bits after the 3548 * binary point (scaled by 16). The following is 3549 * equivalent to rfc793 smoothing with an alpha of .75 3550 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3551 * rfc793's wired-in beta. 3552 */ 3553 if (delta < 0) 3554 delta = -delta; 3555 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3556 if ((tp->t_rttvar += delta) <= 0) 3557 tp->t_rttvar = 1; 3558 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 3559 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3560 } else { 3561 /* 3562 * No rtt measurement yet - use the unsmoothed rtt. 3563 * Set the variance to half the rtt (so our first 3564 * retransmit happens at 3*rtt). 3565 */ 3566 tp->t_srtt = rtt << TCP_RTT_SHIFT; 3567 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 3568 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3569 } 3570 tp->t_rtttime = 0; 3571 tp->t_rxtshift = 0; 3572 3573 /* 3574 * the retransmit should happen at rtt + 4 * rttvar. 3575 * Because of the way we do the smoothing, srtt and rttvar 3576 * will each average +1/2 tick of bias. When we compute 3577 * the retransmit timer, we want 1/2 tick of rounding and 3578 * 1 extra tick because of +-1/2 tick uncertainty in the 3579 * firing of the timer. The bias will give us exactly the 3580 * 1.5 tick we need. But, because the bias is 3581 * statistical, we have to test that we don't drop below 3582 * the minimum feasible timer (which is 2 ticks). 3583 */ 3584 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 3585 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3586 3587 /* 3588 * We received an ack for a packet that wasn't retransmitted; 3589 * it is probably safe to discard any error indications we've 3590 * received recently. This isn't quite right, but close enough 3591 * for now (a route might have failed after we sent a segment, 3592 * and the return path might not be symmetrical). 3593 */ 3594 tp->t_softerror = 0; 3595} 3596 3597/* 3598 * Determine a reasonable value for maxseg size. 3599 * If the route is known, check route for mtu. 3600 * If none, use an mss that can be handled on the outgoing interface 3601 * without forcing IP to fragment. If no route is found, route has no mtu, 3602 * or the destination isn't local, use a default, hopefully conservative 3603 * size (usually 512 or the default IP max size, but no more than the mtu 3604 * of the interface), as we can't discover anything about intervening 3605 * gateways or networks. We also initialize the congestion/slow start 3606 * window to be a single segment if the destination isn't local. 3607 * While looking at the routing entry, we also initialize other path-dependent 3608 * parameters from pre-set or cached values in the routing entry. 3609 * 3610 * NOTE that resulting t_maxseg doesn't include space for TCP options or 3611 * IP options, e.g. IPSEC data, since length of this data may vary, and 3612 * thus it is calculated for every segment separately in tcp_output(). 3613 * 3614 * NOTE that this routine is only called when we process an incoming 3615 * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3616 * settings are handled in tcp_mssopt(). 3617 */ 3618void 3619tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 3620 struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3621{ 3622 int mss = 0; 3623 u_long maxmtu = 0; 3624 struct inpcb *inp = tp->t_inpcb; 3625 struct hc_metrics_lite metrics; 3626#ifdef INET6 3627 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3628 size_t min_protoh = isipv6 ? 3629 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3630 sizeof (struct tcpiphdr); 3631#else 3632 const size_t min_protoh = sizeof(struct tcpiphdr); 3633#endif 3634 3635 INP_WLOCK_ASSERT(tp->t_inpcb); 3636 3637 if (mtuoffer != -1) { 3638 KASSERT(offer == -1, ("%s: conflict", __func__)); 3639 offer = mtuoffer - min_protoh; 3640 } 3641 3642 /* Initialize. */ 3643#ifdef INET6 3644 if (isipv6) { 3645 maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 3646 tp->t_maxseg = V_tcp_v6mssdflt; 3647 } 3648#endif 3649#if defined(INET) && defined(INET6) 3650 else 3651#endif 3652#ifdef INET 3653 { 3654 maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 3655 tp->t_maxseg = V_tcp_mssdflt; 3656 } 3657#endif 3658 3659 /* 3660 * No route to sender, stay with default mss and return. 3661 */ 3662 if (maxmtu == 0) { 3663 /* 3664 * In case we return early we need to initialize metrics 3665 * to a defined state as tcp_hc_get() would do for us 3666 * if there was no cache hit. 3667 */ 3668 if (metricptr != NULL) 3669 bzero(metricptr, sizeof(struct hc_metrics_lite)); 3670 return; 3671 } 3672 3673 /* What have we got? */ 3674 switch (offer) { 3675 case 0: 3676 /* 3677 * Offer == 0 means that there was no MSS on the SYN 3678 * segment, in this case we use tcp_mssdflt as 3679 * already assigned to t_maxseg above. 3680 */ 3681 offer = tp->t_maxseg; 3682 break; 3683 3684 case -1: 3685 /* 3686 * Offer == -1 means that we didn't receive SYN yet. 3687 */ 3688 /* FALLTHROUGH */ 3689 3690 default: 3691 /* 3692 * Prevent DoS attack with too small MSS. Round up 3693 * to at least minmss. 3694 */ 3695 offer = max(offer, V_tcp_minmss); 3696 } 3697 3698 /* 3699 * rmx information is now retrieved from tcp_hostcache. 3700 */ 3701 tcp_hc_get(&inp->inp_inc, &metrics); 3702 if (metricptr != NULL) 3703 bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 3704 3705 /* 3706 * If there's a discovered mtu in tcp hostcache, use it. 3707 * Else, use the link mtu. 3708 */ 3709 if (metrics.rmx_mtu) 3710 mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3711 else { 3712#ifdef INET6 3713 if (isipv6) { 3714 mss = maxmtu - min_protoh; 3715 if (!V_path_mtu_discovery && 3716 !in6_localaddr(&inp->in6p_faddr)) 3717 mss = min(mss, V_tcp_v6mssdflt); 3718 } 3719#endif 3720#if defined(INET) && defined(INET6) 3721 else 3722#endif 3723#ifdef INET 3724 { 3725 mss = maxmtu - min_protoh; 3726 if (!V_path_mtu_discovery && 3727 !in_localaddr(inp->inp_faddr)) 3728 mss = min(mss, V_tcp_mssdflt); 3729 } 3730#endif 3731 /* 3732 * XXX - The above conditional (mss = maxmtu - min_protoh) 3733 * probably violates the TCP spec. 3734 * The problem is that, since we don't know the 3735 * other end's MSS, we are supposed to use a conservative 3736 * default. But, if we do that, then MTU discovery will 3737 * never actually take place, because the conservative 3738 * default is much less than the MTUs typically seen 3739 * on the Internet today. For the moment, we'll sweep 3740 * this under the carpet. 3741 * 3742 * The conservative default might not actually be a problem 3743 * if the only case this occurs is when sending an initial 3744 * SYN with options and data to a host we've never talked 3745 * to before. Then, they will reply with an MSS value which 3746 * will get recorded and the new parameters should get 3747 * recomputed. For Further Study. 3748 */ 3749 } 3750 mss = min(mss, offer); 3751 3752 /* 3753 * Sanity check: make sure that maxseg will be large 3754 * enough to allow some data on segments even if the 3755 * all the option space is used (40bytes). Otherwise 3756 * funny things may happen in tcp_output. 3757 * 3758 * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3759 */ 3760 mss = max(mss, 64); 3761 3762 tp->t_maxseg = mss; 3763} 3764 3765void 3766tcp_mss(struct tcpcb *tp, int offer) 3767{ 3768 int mss; 3769 u_long bufsize; 3770 struct inpcb *inp; 3771 struct socket *so; 3772 struct hc_metrics_lite metrics; 3773 struct tcp_ifcap cap; 3774 3775 KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 3776 3777 bzero(&cap, sizeof(cap)); 3778 tcp_mss_update(tp, offer, -1, &metrics, &cap); 3779 3780 mss = tp->t_maxseg; 3781 inp = tp->t_inpcb; 3782 3783 /* 3784 * If there's a pipesize, change the socket buffer to that size, 3785 * don't change if sb_hiwat is different than default (then it 3786 * has been changed on purpose with setsockopt). 3787 * Make the socket buffers an integral number of mss units; 3788 * if the mss is larger than the socket buffer, decrease the mss. 3789 */ 3790 so = inp->inp_socket; 3791 SOCKBUF_LOCK(&so->so_snd); 3792 if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 3793 bufsize = metrics.rmx_sendpipe; 3794 else 3795 bufsize = so->so_snd.sb_hiwat; 3796 if (bufsize < mss) 3797 mss = bufsize; 3798 else { 3799 bufsize = roundup(bufsize, mss); 3800 if (bufsize > sb_max) 3801 bufsize = sb_max; 3802 if (bufsize > so->so_snd.sb_hiwat) 3803 (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3804 } 3805 SOCKBUF_UNLOCK(&so->so_snd); 3806 /* 3807 * Sanity check: make sure that maxseg will be large 3808 * enough to allow some data on segments even if the 3809 * all the option space is used (40bytes). Otherwise 3810 * funny things may happen in tcp_output. 3811 * 3812 * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3813 */ 3814 tp->t_maxseg = max(mss, 64); 3815 3816 SOCKBUF_LOCK(&so->so_rcv); 3817 if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 3818 bufsize = metrics.rmx_recvpipe; 3819 else 3820 bufsize = so->so_rcv.sb_hiwat; 3821 if (bufsize > mss) { 3822 bufsize = roundup(bufsize, mss); 3823 if (bufsize > sb_max) 3824 bufsize = sb_max; 3825 if (bufsize > so->so_rcv.sb_hiwat) 3826 (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3827 } 3828 SOCKBUF_UNLOCK(&so->so_rcv); 3829 3830 /* Check the interface for TSO capabilities. */ 3831 if (cap.ifcap & CSUM_TSO) { 3832 tp->t_flags |= TF_TSO; 3833 tp->t_tsomax = cap.tsomax; 3834 tp->t_tsomaxsegcount = cap.tsomaxsegcount; 3835 tp->t_tsomaxsegsize = cap.tsomaxsegsize; 3836 } 3837} 3838 3839/* 3840 * Determine the MSS option to send on an outgoing SYN. 3841 */ 3842int 3843tcp_mssopt(struct in_conninfo *inc) 3844{ 3845 int mss = 0; 3846 u_long maxmtu = 0; 3847 u_long thcmtu = 0; 3848 size_t min_protoh; 3849 3850 KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3851 3852#ifdef INET6 3853 if (inc->inc_flags & INC_ISIPV6) { 3854 mss = V_tcp_v6mssdflt; 3855 maxmtu = tcp_maxmtu6(inc, NULL); 3856 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3857 } 3858#endif 3859#if defined(INET) && defined(INET6) 3860 else 3861#endif 3862#ifdef INET 3863 { 3864 mss = V_tcp_mssdflt; 3865 maxmtu = tcp_maxmtu(inc, NULL); 3866 min_protoh = sizeof(struct tcpiphdr); 3867 } 3868#endif 3869#if defined(INET6) || defined(INET) 3870 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 3871#endif 3872 3873 if (maxmtu && thcmtu) 3874 mss = min(maxmtu, thcmtu) - min_protoh; 3875 else if (maxmtu || thcmtu) 3876 mss = max(maxmtu, thcmtu) - min_protoh; 3877 3878 return (mss); 3879} 3880 3881 3882/* 3883 * On a partial ack arrives, force the retransmission of the 3884 * next unacknowledged segment. Do not clear tp->t_dupacks. 3885 * By setting snd_nxt to ti_ack, this forces retransmission timer to 3886 * be started again. 3887 */ 3888void 3889tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 3890{ 3891 tcp_seq onxt = tp->snd_nxt; 3892 u_long ocwnd = tp->snd_cwnd; 3893 u_int maxseg = tcp_maxseg(tp); 3894 3895 INP_WLOCK_ASSERT(tp->t_inpcb); 3896 3897 tcp_timer_activate(tp, TT_REXMT, 0); 3898 tp->t_rtttime = 0; 3899 tp->snd_nxt = th->th_ack; 3900 /* 3901 * Set snd_cwnd to one segment beyond acknowledged offset. 3902 * (tp->snd_una has not yet been updated when this function is called.) 3903 */ 3904 tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 3905 tp->t_flags |= TF_ACKNOW; 3906 (void) tp->t_fb->tfb_tcp_output(tp); 3907 tp->snd_cwnd = ocwnd; 3908 if (SEQ_GT(onxt, tp->snd_nxt)) 3909 tp->snd_nxt = onxt; 3910 /* 3911 * Partial window deflation. Relies on fact that tp->snd_una 3912 * not updated yet. 3913 */ 3914 if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3915 tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3916 else 3917 tp->snd_cwnd = 0; 3918 tp->snd_cwnd += maxseg; 3919} 3920 3921int 3922tcp_compute_pipe(struct tcpcb *tp) 3923{ 3924 return (tp->snd_max - tp->snd_una + 3925 tp->sackhint.sack_bytes_rexmit - 3926 tp->sackhint.sacked_bytes); 3927} 3928