#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_ipsec.h"
#include "opt_rss.h"
#include <sys/param.h>
#include <sys/arb.h>
#include <sys/kernel.h>
#ifdef TCP_HHOOK
#include <sys/hhook.h>
#endif
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/protosw.h>
#include <sys/qmath.h>
#include <sys/sdt.h>
#include <sys/signalvar.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/stats.h>
#include <machine/cpu.h>
#include <vm/uma.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/route.h>
#include <net/rss_config.h>
#include <net/vnet.h>
#define TCPSTATES
#include <netinet/in.h>
#include <netinet/in_kdtrace.h>
#include <netinet/in_pcb.h>
#include <netinet/in_rss.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp_var.h>
#include <netinet/ip_var.h>
#include <netinet/ip_options.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/in6_rss.h>
#include <netinet6/in6_var.h>
#include <netinet6/ip6_var.h>
#include <netinet6/nd6.h>
#include <netinet/tcp.h>
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcp_log_buf.h>
#include <netinet6/tcp6_var.h>
#include <netinet/tcpip.h>
#include <netinet/cc/cc.h>
#include <netinet/tcp_fastopen.h>
#include <netinet/tcp_syncache.h>
#ifdef TCP_OFFLOAD
#include <netinet/tcp_offload.h>
#endif
#include <netinet/tcp_ecn.h>
#include <netinet/udp.h>
#include <netipsec/ipsec_support.h>
#include <machine/in_cksum.h>
#include <security/mac/mac_framework.h>
const int tcprexmtthresh = 3;
VNET_DEFINE(int, tcp_log_in_vain) = 0;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_log_in_vain), 0,
"Log all incoming TCP segments to closed ports");
VNET_DEFINE(int, tcp_bind_all_fibs) = 1;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, bind_all_fibs, CTLFLAG_VNET | CTLFLAG_RDTUN,
&VNET_NAME(tcp_bind_all_fibs), 0,
"Bound sockets receive traffic from all FIBs");
VNET_DEFINE(int, blackhole) = 0;
#define V_blackhole VNET(blackhole)
SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(blackhole), 0,
"Do not send RST on segments to closed ports");
VNET_DEFINE(bool, blackhole_local) = false;
#define V_blackhole_local VNET(blackhole_local)
SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
CTLFLAG_RW, &VNET_NAME(blackhole_local), false,
"Enforce net.inet.tcp.blackhole for locally originated packets");
VNET_DEFINE(int, tcp_delack_enabled) = 1;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_delack_enabled), 0,
"Delay ACK to try and piggyback it onto a data packet");
VNET_DEFINE(int, drop_synfin) = 0;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(drop_synfin), 0,
"Drop TCP packets with SYN+FIN set");
VNET_DEFINE(int, tcp_do_prr) = 1;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_do_prr), 1,
"Enable Proportional Rate Reduction per RFC 6937");
VNET_DEFINE(int, tcp_do_newcwv) = 0;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_do_newcwv), 0,
"Enable New Congestion Window Validation per RFC7661");
VNET_DEFINE(int, tcp_do_rfc3042) = 1;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_do_rfc3042), 0,
"Enable RFC 3042 (Limited Transmit)");
VNET_DEFINE(int, tcp_do_rfc3390) = 1;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_do_rfc3390), 0,
"Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
"Slow-start flight size (initial congestion window) in number of segments");
VNET_DEFINE(int, tcp_do_rfc3465) = 1;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_do_rfc3465), 0,
"Enable RFC 3465 (Appropriate Byte Counting)");
VNET_DEFINE(int, tcp_abc_l_var) = 2;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_abc_l_var), 2,
"Cap the max cwnd increment during slow-start to this number of segments");
VNET_DEFINE(int, tcp_insecure_syn) = 0;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_insecure_syn), 0,
"Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
VNET_DEFINE(int, tcp_insecure_rst) = 0;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_insecure_rst), 0,
"Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
VNET_DEFINE(int, tcp_insecure_ack) = 0;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_ack, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_insecure_ack), 0,
"Follow RFC793 criteria for validating SEG.ACK");
VNET_DEFINE(int, tcp_recvspace) = 1024*64;
#define V_tcp_recvspace VNET(tcp_recvspace)
SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_do_autorcvbuf), 0,
"Enable automatic receive buffer sizing");
VNET_DEFINE(int, tcp_autorcvbuf_max) = 8*1024*1024;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(tcp_autorcvbuf_max), 0,
"Max size of automatic receive buffer");
VNET_DEFINE(struct inpcbinfo, tcbinfo);
VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
"TCP connection counts by TCP state");
void
kmod_tcpstat_add(int statnum, int val)
{
counter_u64_add(VNET(tcpstat)[statnum], val);
}
static bool inline
tcp_is_sack_recovery(struct tcpcb *tp, struct tcpopt *to)
{
return ((tp->t_flags & TF_SACK_PERMIT) &&
((to->to_flags & TOF_SACK) ||
(!TAILQ_EMPTY(&tp->snd_holes))));
}
#ifdef TCP_HHOOK
void
hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
{
struct tcp_hhook_data hhook_data;
if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
hhook_data.tp = tp;
hhook_data.th = th;
hhook_data.to = to;
hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
&tp->t_osd);
}
}
#endif
void
cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
uint16_t type)
{
#ifdef STATS
int32_t gput;
#endif
INP_WLOCK_ASSERT(tptoinpcb(tp));
tp->t_ccv.nsegs = nsegs;
tp->t_ccv.bytes_this_ack = BYTES_THIS_ACK(tp, th);
if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) ||
(V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) &&
(tp->snd_cwnd < (tcp_compute_pipe(tp) * 2))))
tp->t_ccv.flags |= CCF_CWND_LIMITED;
else
tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
if (type == CC_ACK) {
#ifdef STATS
stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
((int32_t)tp->snd_cwnd) - tp->snd_wnd);
if (!IN_RECOVERY(tp->t_flags))
stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN,
tp->t_ccv.bytes_this_ack / (tcp_maxseg(tp) * nsegs));
if ((tp->t_flags & TF_GPUTINPROG) &&
SEQ_GEQ(th->th_ack, tp->gput_ack)) {
gput = (((int64_t)SEQ_SUB(th->th_ack, tp->gput_seq)) << 3) /
max(1, tcp_ts_getticks() - tp->gput_ts);
stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
gput);
if (tp->t_stats_gput_prev > 0)
stats_voi_update_abs_s32(tp->t_stats,
VOI_TCP_GPUT_ND,
((gput - tp->t_stats_gput_prev) * 100) /
tp->t_stats_gput_prev);
tp->t_flags &= ~TF_GPUTINPROG;
tp->t_stats_gput_prev = gput;
}
#endif
if (tp->snd_cwnd > tp->snd_ssthresh) {
tp->t_bytes_acked += tp->t_ccv.bytes_this_ack;
if (tp->t_bytes_acked >= tp->snd_cwnd) {
tp->t_bytes_acked -= tp->snd_cwnd;
tp->t_ccv.flags |= CCF_ABC_SENTAWND;
}
} else {
tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
tp->t_bytes_acked = 0;
}
}
if (CC_ALGO(tp)->ack_received != NULL) {
tp->t_ccv.curack = th->th_ack;
CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
}
#ifdef STATS
stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
#endif
}
void
cc_conn_init(struct tcpcb *tp)
{
struct hc_metrics_lite metrics;
struct inpcb *inp = tptoinpcb(tp);
u_int maxseg;
int rtt;
INP_WLOCK_ASSERT(inp);
tcp_hc_get(&inp->inp_inc, &metrics);
maxseg = tcp_maxseg(tp);
if (tp->t_srtt == 0 && (rtt = metrics.hc_rtt)) {
tp->t_srtt = rtt;
TCPSTAT_INC(tcps_usedrtt);
if (metrics.hc_rttvar) {
tp->t_rttvar = metrics.hc_rttvar;
TCPSTAT_INC(tcps_usedrttvar);
} else {
tp->t_rttvar =
tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
}
TCPT_RANGESET(tp->t_rxtcur,
((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
tp->t_rttmin, tcp_rexmit_max);
}
if (metrics.hc_ssthresh) {
tp->snd_ssthresh = max(2 * maxseg, metrics.hc_ssthresh);
TCPSTAT_INC(tcps_usedssthresh);
}
if (tp->snd_cwnd == 1)
tp->snd_cwnd = maxseg;
else
tp->snd_cwnd = tcp_compute_initwnd(maxseg);
if (CC_ALGO(tp)->conn_init != NULL)
CC_ALGO(tp)->conn_init(&tp->t_ccv);
}
void inline
cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
{
INP_WLOCK_ASSERT(tptoinpcb(tp));
#ifdef STATS
stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
#endif
switch(type) {
case CC_NDUPACK:
if (!IN_FASTRECOVERY(tp->t_flags)) {
tp->snd_recover = tp->snd_max;
if (tp->t_flags2 & TF2_ECN_PERMIT)
tp->t_flags2 |= TF2_ECN_SND_CWR;
}
break;
case CC_ECN:
if (!IN_CONGRECOVERY(tp->t_flags) ||
SEQ_GEQ(th->th_ack, tp->snd_recover)) {
EXIT_CONGRECOVERY(tp->t_flags);
TCPSTAT_INC(tcps_ecn_rcwnd);
tp->snd_recover = tp->snd_max + 1;
if (tp->t_flags2 & TF2_ECN_PERMIT)
tp->t_flags2 |= TF2_ECN_SND_CWR;
}
break;
case CC_RTO:
tp->t_dupacks = 0;
tp->t_bytes_acked = 0;
EXIT_RECOVERY(tp->t_flags);
if (tp->t_flags2 & TF2_ECN_PERMIT)
tp->t_flags2 |= TF2_ECN_SND_CWR;
break;
case CC_RTO_ERR:
TCPSTAT_INC(tcps_sndrexmitbad);
tp->snd_cwnd = tp->snd_cwnd_prev;
tp->snd_ssthresh = tp->snd_ssthresh_prev;
tp->snd_recover = tp->snd_recover_prev;
if (tp->t_flags & TF_WASFRECOVERY)
ENTER_FASTRECOVERY(tp->t_flags);
if (tp->t_flags & TF_WASCRECOVERY)
ENTER_CONGRECOVERY(tp->t_flags);
tp->snd_nxt = tp->snd_max;
tp->t_flags &= ~TF_PREVVALID;
tp->t_rxtshift = 0;
tp->t_badrxtwin = 0;
break;
}
if (SEQ_LT(tp->snd_fack, tp->snd_una) ||
SEQ_GT(tp->snd_fack, tp->snd_max)) {
tp->snd_fack = tp->snd_una;
}
if (CC_ALGO(tp)->cong_signal != NULL) {
if (th != NULL)
tp->t_ccv.curack = th->th_ack;
CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
}
}
void inline
cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
{
INP_WLOCK_ASSERT(tptoinpcb(tp));
if (CC_ALGO(tp)->post_recovery != NULL) {
if (SEQ_LT(tp->snd_fack, th->th_ack) ||
SEQ_GT(tp->snd_fack, tp->snd_max)) {
tp->snd_fack = th->th_ack;
}
tp->t_ccv.curack = th->th_ack;
CC_ALGO(tp)->post_recovery(&tp->t_ccv);
}
EXIT_RECOVERY(tp->t_flags);
tp->t_bytes_acked = 0;
tp->sackhint.delivered_data = 0;
tp->sackhint.prr_delivered = 0;
tp->sackhint.prr_out = 0;
}
#define DELAY_ACK(tp, tlen) \
((!tcp_timer_active(tp, TT_DELACK) && \
(tp->t_flags & TF_RXWIN0SENT) == 0) && \
(tlen <= tp->t_maxseg) && \
(V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
void inline
cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos)
{
INP_WLOCK_ASSERT(tptoinpcb(tp));
if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
switch (iptos & IPTOS_ECN_MASK) {
case IPTOS_ECN_CE:
tp->t_ccv.flags |= CCF_IPHDR_CE;
break;
case IPTOS_ECN_ECT0:
case IPTOS_ECN_ECT1:
case IPTOS_ECN_NOTECT:
tp->t_ccv.flags &= ~CCF_IPHDR_CE;
break;
}
if (flags & TH_CWR)
tp->t_ccv.flags |= CCF_TCPHDR_CWR;
else
tp->t_ccv.flags &= ~CCF_TCPHDR_CWR;
CC_ALGO(tp)->ecnpkt_handler(&tp->t_ccv);
if (((tp->t_state == TCPS_ESTABLISHED) ||
(tp->t_state == TCPS_FIN_WAIT_1) ||
(tp->t_state == TCPS_FIN_WAIT_2)) &&
(tp->t_ccv.flags & CCF_ACKNOW)) {
tp->t_flags |= TF_ACKNOW;
tp->t_ccv.flags &= ~CCF_ACKNOW;
}
}
}
void inline
cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
{
cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos);
}
#ifdef INET6
int
tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
{
struct mbuf *m;
m = *mp;
if (m->m_len < *offp + sizeof(struct tcphdr)) {
m = m_pullup(m, *offp + sizeof(struct tcphdr));
if (m == NULL) {
*mp = m;
TCPSTAT_INC(tcps_rcvshort);
return (IPPROTO_DONE);
}
}
*mp = m;
return (tcp_input_with_port(mp, offp, proto, port));
}
int
tcp6_input(struct mbuf **mp, int *offp, int proto)
{
return(tcp6_input_with_port(mp, offp, proto, 0));
}
#endif
int
tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
{
struct mbuf *m = *mp;
struct tcphdr *th = NULL;
struct ip *ip = NULL;
struct inpcb *inp = NULL;
struct tcpcb *tp = NULL;
struct socket *so = NULL;
u_char *optp = NULL;
int off0;
int optlen = 0;
#ifdef INET
int len;
uint8_t ipttl;
#endif
int tlen = 0, off;
int drop_hdrlen;
int thflags;
int lookupflag;
uint8_t iptos;
struct m_tag *fwd_tag = NULL;
#ifdef INET6
struct ip6_hdr *ip6 = NULL;
int isipv6;
#else
const void *ip6 = NULL;
#endif
struct tcpopt to;
char *s = NULL;
bool closed_port = false;
NET_EPOCH_ASSERT();
#ifdef INET6
isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
#endif
off0 = *offp;
m = *mp;
*mp = NULL;
to.to_flags = 0;
TCPSTAT_INC(tcps_rcvtotal);
m->m_pkthdr.tcp_tun_port = port;
#ifdef INET6
if (isipv6) {
ip6 = mtod(m, struct ip6_hdr *);
th = (struct tcphdr *)((caddr_t)ip6 + off0);
tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
if (port)
goto skip6_csum;
if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
th->th_sum = m->m_pkthdr.csum_data;
else
th->th_sum = in6_cksum_pseudo(ip6, tlen,
IPPROTO_TCP, m->m_pkthdr.csum_data);
th->th_sum ^= 0xffff;
} else if (m->m_pkthdr.csum_flags & CSUM_IP6_TCP) {
th->th_sum = 0;
} else
th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
if (th->th_sum) {
TCPSTAT_INC(tcps_rcvbadsum);
goto drop;
}
skip6_csum:
KASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst),
("%s: unspecified destination v6 address", __func__));
if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
IP6STAT_INC(ip6s_badscope);
goto drop;
}
iptos = IPV6_TRAFFIC_CLASS(ip6);
}
#endif
#if defined(INET) && defined(INET6)
else
#endif
#ifdef INET
{
if (off0 > sizeof (struct ip)) {
ip_stripoptions(m);
off0 = sizeof(struct ip);
}
if (m->m_len < sizeof (struct tcpiphdr)) {
if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
== NULL) {
TCPSTAT_INC(tcps_rcvshort);
return (IPPROTO_DONE);
}
}
ip = mtod(m, struct ip *);
th = (struct tcphdr *)((caddr_t)ip + off0);
tlen = ntohs(ip->ip_len) - off0;
iptos = ip->ip_tos;
if (port)
goto skip_csum;
if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
th->th_sum = m->m_pkthdr.csum_data;
else
th->th_sum = in_pseudo(ip->ip_src.s_addr,
ip->ip_dst.s_addr,
htonl(m->m_pkthdr.csum_data + tlen +
IPPROTO_TCP));
th->th_sum ^= 0xffff;
} else if (m->m_pkthdr.csum_flags & CSUM_IP_TCP) {
th->th_sum = 0;
} else {
struct ipovly *ipov = (struct ipovly *)ip;
len = off0 + tlen;
ipttl = ip->ip_ttl;
bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
ipov->ih_len = htons(tlen);
th->th_sum = in_cksum(m, len);
ip->ip_len = htons(len);
ip->ip_tos = iptos;
ip->ip_ttl = ipttl;
ip->ip_v = IPVERSION;
ip->ip_hl = off0 >> 2;
}
skip_csum:
if (th->th_sum && (port == 0)) {
TCPSTAT_INC(tcps_rcvbadsum);
goto drop;
}
KASSERT(ip->ip_dst.s_addr != INADDR_ANY,
("%s: unspecified destination v4 address", __func__));
if (__predict_false(ip->ip_src.s_addr == INADDR_ANY)) {
IPSTAT_INC(ips_badaddr);
goto drop;
}
}
#endif
off = th->th_off << 2;
if (off < sizeof (struct tcphdr) || off > tlen) {
TCPSTAT_INC(tcps_rcvbadoff);
goto drop;
}
tlen -= off;
if (off > sizeof (struct tcphdr)) {
#ifdef INET6
if (isipv6) {
if (m->m_len < off0 + off) {
m = m_pullup(m, off0 + off);
if (m == NULL) {
TCPSTAT_INC(tcps_rcvshort);
return (IPPROTO_DONE);
}
}
ip6 = mtod(m, struct ip6_hdr *);
th = (struct tcphdr *)((caddr_t)ip6 + off0);
}
#endif
#if defined(INET) && defined(INET6)
else
#endif
#ifdef INET
{
if (m->m_len < sizeof(struct ip) + off) {
if ((m = m_pullup(m, sizeof (struct ip) + off))
== NULL) {
TCPSTAT_INC(tcps_rcvshort);
return (IPPROTO_DONE);
}
ip = mtod(m, struct ip *);
th = (struct tcphdr *)((caddr_t)ip + off0);
}
}
#endif
optlen = off - sizeof (struct tcphdr);
optp = (u_char *)(th + 1);
}
thflags = tcp_get_flags(th);
tcp_fields_to_host(th);
drop_hdrlen = off0 + off;
if (
#ifdef INET6
(isipv6 && (m->m_flags & M_IP6_NEXTHOP))
#ifdef INET
|| (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
#endif
#endif
#if defined(INET) && !defined(INET6)
(m->m_flags & M_IP_NEXTHOP)
#endif
)
fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
lookupflag = INPLOOKUP_WILDCARD |
((thflags & (TH_ACK|TH_SYN)) == TH_SYN ?
INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB) |
(V_tcp_bind_all_fibs ? 0 : INPLOOKUP_FIB);
findpcb:
tp = NULL;
#ifdef INET6
if (isipv6 && fwd_tag != NULL) {
struct sockaddr_in6 *next_hop6;
next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
inp = in6_pcblookup_mbuf(&V_tcbinfo,
&ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
lookupflag & ~INPLOOKUP_WILDCARD, m->m_pkthdr.rcvif, m);
if (!inp) {
inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
th->th_sport, &next_hop6->sin6_addr,
next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
th->th_dport, lookupflag, m->m_pkthdr.rcvif);
}
} else if (isipv6) {
inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
th->th_sport, &ip6->ip6_dst, th->th_dport, lookupflag,
m->m_pkthdr.rcvif, m);
}
#endif
#if defined(INET6) && defined(INET)
else
#endif
#ifdef INET
if (fwd_tag != NULL) {
struct sockaddr_in *next_hop;
next_hop = (struct sockaddr_in *)(fwd_tag+1);
inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
ip->ip_dst, th->th_dport, lookupflag & ~INPLOOKUP_WILDCARD,
m->m_pkthdr.rcvif, m);
if (!inp) {
inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
th->th_sport, next_hop->sin_addr,
next_hop->sin_port ? ntohs(next_hop->sin_port) :
th->th_dport, lookupflag, m->m_pkthdr.rcvif);
}
} else
inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
th->th_sport, ip->ip_dst, th->th_dport, lookupflag,
m->m_pkthdr.rcvif, m);
#endif
if (inp == NULL) {
if ((lookupflag & INPLOOKUP_WILDCARD) == 0) {
MPASS(!closed_port);
} else {
if (((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
V_tcp_log_in_vain == 2) &&
(s = tcp_log_vain(NULL, th, (void *)ip, ip6))) {
log(LOG_INFO, "%s; %s: Connection attempt "
"to closed port\n", s, __func__);
}
closed_port = true;
}
goto dropwithreset;
}
INP_LOCK_ASSERT(inp);
#if defined(IPSEC) || defined(IPSEC_SUPPORT)
#ifdef INET6
if (isipv6 && IPSEC_ENABLED(ipv6) &&
IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
goto dropunlock;
}
#ifdef INET
else
#endif
#endif
#ifdef INET
if (IPSEC_ENABLED(ipv4) &&
IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
goto dropunlock;
}
#endif
#endif
if (inp->inp_ip_minttl != 0) {
#ifdef INET6
if (isipv6) {
if (inp->inp_ip_minttl > ip6->ip6_hlim)
goto dropunlock;
} else
#endif
if (inp->inp_ip_minttl > ip->ip_ttl)
goto dropunlock;
}
tp = intotcpcb(inp);
switch (tp->t_state) {
case TCPS_TIME_WAIT:
tcp_dooptions(&to, optp, optlen,
(thflags & TH_SYN) ? TO_SYN : 0);
if (tcp_twcheck(inp, &to, th, m, tlen))
goto findpcb;
return (IPPROTO_DONE);
case TCPS_CLOSED:
closed_port = true;
goto dropwithreset;
}
if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) {
closed_port = true;
goto dropwithreset;
}
#ifdef TCP_OFFLOAD
if (tp->t_flags & TF_TOE) {
tcp_offload_input(tp, m);
m = NULL;
goto dropunlock;
}
#endif
#ifdef MAC
if (mac_inpcb_check_deliver(inp, m))
goto dropunlock;
#endif
so = inp->inp_socket;
KASSERT(so != NULL, ("%s: so == NULL", __func__));
KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so),
("%s: so accepting but tp %p not listening", __func__, tp));
if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) {
struct in_conninfo inc;
bzero(&inc, sizeof(inc));
#ifdef INET6
if (isipv6) {
inc.inc_flags |= INC_ISIPV6;
if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
inc.inc_flags |= INC_IPV6MINMTU;
inc.inc6_faddr = ip6->ip6_src;
inc.inc6_laddr = ip6->ip6_dst;
} else
#endif
{
inc.inc_faddr = ip->ip_src;
inc.inc_laddr = ip->ip_dst;
}
inc.inc_fport = th->th_sport;
inc.inc_lport = th->th_dport;
inc.inc_fibnum = so->so_fibnum;
if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
int result;
tcp_dooptions(&to, optp, optlen, 0);
result = syncache_expand(&inc, &to, th, &so, m, port);
if (result < 0) {
goto dropunlock;
} else if (result == 0) {
INP_WUNLOCK(inp);
lookupflag &= ~INPLOOKUP_WILDCARD;
goto findpcb;
}
tfo_socket_result:
if (so == NULL) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"Socket allocation failed due to "
"limits or memory shortage, %s\n",
s, __func__,
V_tcp_sc_rst_sock_fail ?
"sending RST" : "try again");
if (V_tcp_sc_rst_sock_fail) {
goto dropwithreset;
} else
goto dropunlock;
}
INP_UNLOCK(inp);
inp = sotoinpcb(so);
INP_WLOCK_ASSERT(inp);
tp = intotcpcb(inp);
KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
("%s: ", __func__));
TCP_PROBE5(receive, NULL, tp, m, tp, th);
tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen,
tlen, iptos);
return (IPPROTO_DONE);
}
if (thflags & TH_RST) {
syncache_chkrst(&inc, th, port);
goto dropunlock;
}
if ((thflags & TH_SYN) == 0) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"SYN is missing, segment ignored\n",
s, __func__);
TCPSTAT_INC(tcps_badsyn);
goto dropunlock;
}
if (thflags & TH_ACK) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"SYN|ACK invalid, segment ignored\n",
s, __func__);
TCPSTAT_INC(tcps_badsyn);
goto dropunlock;
}
if ((thflags & TH_FIN) && V_drop_synfin) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"SYN|FIN segment ignored (based on "
"sysctl setting)\n", s, __func__);
TCPSTAT_INC(tcps_badsyn);
goto dropunlock;
}
KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
("%s: Listen socket: TH_RST or TH_ACK set", __func__));
KASSERT(thflags & (TH_SYN),
("%s: Listen socket: TH_SYN not set", __func__));
INP_RLOCK_ASSERT(inp);
#ifdef INET6
if (isipv6 && !V_ip6_use_deprecated) {
struct in6_ifaddr *ia6;
ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 , false);
if (ia6 != NULL &&
(ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"Connection attempt to deprecated "
"IPv6 address rejected\n",
s, __func__);
goto dropwithreset;
}
}
#endif
if (m->m_flags & (M_BCAST|M_MCAST)) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"Connection attempt from broad- or multicast "
"link layer address ignored\n", s, __func__);
goto dropunlock;
}
#ifdef INET6
if (isipv6) {
if (th->th_dport == th->th_sport &&
IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"Connection attempt to/from self "
"ignored\n", s, __func__);
goto dropunlock;
}
if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"Connection attempt from/to multicast "
"address ignored\n", s, __func__);
goto dropunlock;
}
}
#endif
#if defined(INET) && defined(INET6)
else
#endif
#ifdef INET
{
if (th->th_dport == th->th_sport &&
ip->ip_dst.s_addr == ip->ip_src.s_addr) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"Connection attempt from/to self "
"ignored\n", s, __func__);
goto dropunlock;
}
if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
in_ifnet_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
log(LOG_DEBUG, "%s; %s: Listen socket: "
"Connection attempt from/to broad- "
"or multicast address ignored\n",
s, __func__);
goto dropunlock;
}
}
#endif
TCP_PROBE3(debug__input, tp, th, m);
tcp_dooptions(&to, optp, optlen, TO_SYN);
if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL,
iptos, port)) != NULL)
goto tfo_socket_result;
return (IPPROTO_DONE);
}
#if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
if (tp->t_flags & TF_SIGNATURE) {
tcp_dooptions(&to, optp, optlen, thflags);
if ((to.to_flags & TOF_SIGNATURE) == 0) {
TCPSTAT_INC(tcps_sig_err_nosigopt);
goto dropunlock;
}
if (!TCPMD5_ENABLED() ||
TCPMD5_INPUT(m, th, to.to_signature) != 0)
goto dropunlock;
}
#endif
TCP_PROBE5(receive, NULL, tp, m, tp, th);
if ((lookupflag & INPLOOKUP_RLOCKPCB) && INP_TRY_UPGRADE(inp) == 0)
goto dropunlock;
tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen, tlen, iptos);
return (IPPROTO_DONE);
dropwithreset:
if (((!closed_port && V_blackhole == 3) ||
(closed_port &&
((V_blackhole == 1 && (thflags & TH_SYN)) || V_blackhole > 1))) &&
(V_blackhole_local || (
#ifdef INET6
isipv6 ? !in6_localip(&ip6->ip6_src) :
#endif
#ifdef INET
!in_localip(ip->ip_src)
#else
true
#endif
)))
goto dropunlock;
TCP_PROBE5(receive, NULL, tp, m, tp, th);
tcp_dropwithreset(m, th, tp, tlen);
m = NULL;
dropunlock:
if (m != NULL)
TCP_PROBE5(receive, NULL, tp, m, tp, th);
if (inp != NULL)
INP_UNLOCK(inp);
drop:
if (s != NULL)
free(s, M_TCPLOG);
if (m != NULL)
m_freem(m);
return (IPPROTO_DONE);
}
int
tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
struct tcpcb *tp, int tlen)
{
int newsize = 0;
if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
}
TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
tp->rfbuf_ts = 0;
tp->rfbuf_cnt = 0;
} else {
tp->rfbuf_cnt += tlen;
}
return (newsize);
}
int
tcp_input(struct mbuf **mp, int *offp, int proto)
{
return(tcp_input_with_port(mp, offp, proto, 0));
}
static void
tcp_handle_wakeup(struct tcpcb *tp)
{
INP_WLOCK_ASSERT(tptoinpcb(tp));
if (tp->t_flags & TF_WAKESOR) {
struct socket *so = tptosocket(tp);
tp->t_flags &= ~TF_WAKESOR;
SOCK_RECVBUF_LOCK_ASSERT(so);
sorwakeup_locked(so);
}
}
void
tcp_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
int drop_hdrlen, int tlen, uint8_t iptos)
{
uint16_t thflags;
int acked, ourfinisacked, needoutput = 0;
sackstatus_t sack_changed;
int todrop, win, incforsyn = 0;
uint32_t tiwin;
uint16_t nsegs;
char *s;
struct inpcb *inp = tptoinpcb(tp);
struct socket *so = tptosocket(tp);
struct in_conninfo *inc = &inp->inp_inc;
struct mbuf *mfree;
struct tcpopt to;
int tfo_syn;
u_int maxseg = 0;
bool no_data;
no_data = (tlen == 0);
thflags = tcp_get_flags(th);
tp->sackhint.last_sack_ack = 0;
sack_changed = SACK_NOCHANGE;
nsegs = max(1, m->m_pkthdr.lro_nsegs);
NET_EPOCH_ASSERT();
INP_WLOCK_ASSERT(inp);
KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
__func__));
KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
__func__));
TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
tlen, NULL, true);
if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
log(LOG_DEBUG, "%s; %s: "
"SYN|FIN segment ignored (based on "
"sysctl setting)\n", s, __func__);
free(s, M_TCPLOG);
}
goto drop;
}
if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
(SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
goto dropwithreset;
}
if (tp->t_idle_reduce &&
(tp->snd_max == tp->snd_una) &&
((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
cc_after_idle(tp);
tp->t_rcvtime = ticks;
if (thflags & TH_FIN)
tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
tiwin = th->th_win << tp->snd_scale;
#ifdef STATS
stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
#endif
if (tcp_ecn_input_segment(tp, thflags, tlen,
tcp_packets_this_ack(tp, th->th_ack),
iptos))
cc_cong_signal(tp, th, CC_ECN);
tcp_dooptions(&to, (u_char *)(th + 1),
(th->th_off << 2) - sizeof(struct tcphdr),
(thflags & TH_SYN) ? TO_SYN : 0);
if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) {
to.to_flags &= ~TOF_SACK;
}
#if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
if ((tp->t_flags & TF_SIGNATURE) != 0 &&
(to.to_flags & TOF_SIGNATURE) == 0) {
TCPSTAT_INC(tcps_sig_err_sigopt);
}
#endif
if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
to.to_tsecr -= tp->ts_offset;
if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) {
to.to_tsecr = 0;
}
}
if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
tcp_ecn_input_parallel_syn(tp, thflags, iptos);
if ((to.to_flags & TOF_SCALE) &&
(tp->t_flags & TF_REQ_SCALE) &&
!(tp->t_flags & TF_NOOPT)) {
tp->t_flags |= TF_RCVD_SCALE;
tp->snd_scale = to.to_wscale;
} else {
tp->t_flags &= ~TF_REQ_SCALE;
}
tp->snd_wnd = th->th_win;
if ((to.to_flags & TOF_TS) &&
(tp->t_flags & TF_REQ_TSTMP) &&
!(tp->t_flags & TF_NOOPT)) {
tp->t_flags |= TF_RCVD_TSTMP;
tp->ts_recent = to.to_tsval;
tp->ts_recent_age = tcp_ts_getticks();
} else {
tp->t_flags &= ~TF_REQ_TSTMP;
}
if (to.to_flags & TOF_MSS) {
tcp_mss(tp, to.to_mss);
}
if ((tp->t_flags & TF_SACK_PERMIT) &&
(!(to.to_flags & TOF_SACKPERM) ||
(tp->t_flags & TF_NOOPT))) {
tp->t_flags &= ~TF_SACK_PERMIT;
}
if (tp->t_flags & TF_FASTOPEN) {
if ((to.to_flags & TOF_FASTOPEN) &&
!(tp->t_flags & TF_NOOPT)) {
uint16_t mss;
if (to.to_flags & TOF_MSS) {
mss = to.to_mss;
} else {
if ((inp->inp_vflag & INP_IPV6) != 0) {
mss = TCP6_MSS;
} else {
mss = TCP_MSS;
}
}
tcp_fastopen_update_cache(tp, mss,
to.to_tfo_len, to.to_tfo_cookie);
} else {
tcp_fastopen_disable_path(tp);
}
}
}
if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) {
if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
log(LOG_DEBUG, "%s; %s: Timestamp missing, "
"segment processed normally\n",
s, __func__);
free(s, M_TCPLOG);
}
} else {
if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
log(LOG_DEBUG, "%s; %s: Timestamp missing, "
"segment silently dropped\n", s, __func__);
free(s, M_TCPLOG);
}
goto drop;
}
}
if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
"segment processed normally\n", s, __func__);
free(s, M_TCPLOG);
}
}
if (tp->t_state == TCPS_ESTABLISHED &&
th->th_seq == tp->rcv_nxt &&
(thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
tp->snd_nxt == tp->snd_max &&
tiwin && tiwin == tp->snd_wnd &&
((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
SEGQ_EMPTY(tp) &&
((to.to_flags & TOF_TS) == 0 ||
TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
if ((to.to_flags & TOF_TS) != 0 &&
SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
tp->ts_recent_age = tcp_ts_getticks();
tp->ts_recent = to.to_tsval;
}
if (no_data) {
if (SEQ_GT(th->th_ack, tp->snd_una) &&
SEQ_LEQ(th->th_ack, tp->snd_max) &&
!IN_RECOVERY(tp->t_flags) &&
(to.to_flags & TOF_SACK) == 0 &&
TAILQ_EMPTY(&tp->snd_holes)) {
TCPSTAT_INC(tcps_predack);
if (tp->t_rxtshift == 1 &&
tp->t_flags & TF_PREVVALID &&
tp->t_badrxtwin != 0 &&
(((to.to_flags & TOF_TS) != 0 &&
to.to_tsecr != 0 &&
TSTMP_LT(to.to_tsecr, tp->t_badrxtwin)) ||
((to.to_flags & TOF_TS) == 0 &&
TSTMP_LT(ticks, tp->t_badrxtwin))))
cc_cong_signal(tp, th, CC_RTO_ERR);
if ((to.to_flags & TOF_TS) != 0 &&
to.to_tsecr) {
uint32_t t;
t = tcp_ts_getticks() - to.to_tsecr;
if (!tp->t_rttlow || tp->t_rttlow > t)
tp->t_rttlow = t;
tcp_xmit_timer(tp,
TCP_TS_TO_TICKS(t) + 1);
} else if (tp->t_rtttime &&
SEQ_GT(th->th_ack, tp->t_rtseq)) {
if (!tp->t_rttlow ||
tp->t_rttlow > ticks - tp->t_rtttime)
tp->t_rttlow = ticks - tp->t_rtttime;
tcp_xmit_timer(tp,
ticks - tp->t_rtttime);
}
acked = BYTES_THIS_ACK(tp, th);
#ifdef TCP_HHOOK
hhook_run_tcp_est_in(tp, th, &to);
#endif
TCPSTAT_ADD(tcps_rcvackpack, nsegs);
TCPSTAT_ADD(tcps_rcvackbyte, acked);
sbdrop(&so->so_snd, acked);
if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
SEQ_LEQ(th->th_ack, tp->snd_recover))
tp->snd_recover = th->th_ack - 1;
cc_ack_received(tp, th, nsegs, CC_ACK);
tp->snd_una = th->th_ack;
tp->snd_wl2 = th->th_ack;
tp->t_dupacks = 0;
m_freem(m);
TCP_PROBE3(debug__input, tp, th, m);
if (sbavail(&so->so_snd) == 0)
tp->t_acktime = 0;
else if (acked > 1)
tp->t_acktime = ticks;
if (tp->snd_una == tp->snd_max)
tcp_timer_activate(tp, TT_REXMT, 0);
else if (!tcp_timer_active(tp, TT_PERSIST))
tcp_timer_activate(tp, TT_REXMT,
TP_RXTCUR(tp));
sowwakeup(so);
if ((tp->t_flags & TF_ACKNOW) ||
(sbavail(&so->so_snd) >=
SEQ_SUB(tp->snd_max, tp->snd_una))) {
(void) tcp_output(tp);
}
goto check_delack;
}
} else if (th->th_ack == tp->snd_una &&
tlen <= sbspace(&so->so_rcv)) {
int newsize = 0;
if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
tcp_clean_sackreport(tp);
TCPSTAT_INC(tcps_preddat);
tp->rcv_nxt += tlen;
if (tlen &&
((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
(tp->t_fbyte_in == 0)) {
tp->t_fbyte_in = ticks;
if (tp->t_fbyte_in == 0)
tp->t_fbyte_in = 1;
if (tp->t_fbyte_out && tp->t_fbyte_in)
tp->t_flags2 |= TF2_FBYTES_COMPLETE;
}
tp->snd_wl1 = th->th_seq;
tp->rcv_up = tp->rcv_nxt;
TCPSTAT_ADD(tcps_rcvpack, nsegs);
TCPSTAT_ADD(tcps_rcvbyte, tlen);
TCP_PROBE3(debug__input, tp, th, m);
newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
SOCK_RECVBUF_LOCK(so);
if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
m_freem(m);
} else {
if (newsize)
if (!sbreserve_locked(so, SO_RCV,
newsize, NULL))
so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
m_adj(m, drop_hdrlen);
sbappendstream_locked(&so->so_rcv, m, 0);
}
sorwakeup_locked(so);
if (DELAY_ACK(tp, tlen)) {
tp->t_flags |= TF_DELACK;
} else {
tp->t_flags |= TF_ACKNOW;
(void) tcp_output(tp);
}
goto check_delack;
}
}
win = sbspace(&so->so_rcv);
if (win < 0)
win = 0;
tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
if (thflags & TH_RST) {
break;
}
if ((thflags & TH_ACK) &&
(SEQ_LEQ(th->th_ack, tp->snd_una) ||
SEQ_GT(th->th_ack, tp->snd_max))) {
tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
goto dropwithreset;
}
if (tp->t_flags & TF_FASTOPEN) {
if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
goto dropwithreset;
} else if (thflags & TH_SYN) {
if ((tcp_timer_active(tp, TT_DELACK) ||
tcp_timer_active(tp, TT_REXMT)))
goto drop;
} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
goto drop;
}
}
break;
case TCPS_SYN_SENT:
if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
TCP_PROBE5(connect__refused, NULL, tp,
m, tp, th);
tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
tp = tcp_drop(tp, ECONNREFUSED);
}
if (thflags & TH_RST)
goto drop;
if (!(thflags & TH_SYN))
goto drop;
tp->irs = th->th_seq;
tcp_rcvseqinit(tp);
if (thflags & TH_ACK) {
int tfo_partial_ack = 0;
TCPSTAT_INC(tcps_connects);
soisconnected(so);
#ifdef MAC
mac_socketpeer_set_from_mbuf(m, so);
#endif
if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
(TF_RCVD_SCALE|TF_REQ_SCALE)) {
tp->rcv_scale = tp->request_r_scale;
}
tp->rcv_adv += min(tp->rcv_wnd,
TCP_MAXWIN << tp->rcv_scale);
tp->snd_una++;
if (SEQ_LT(tp->snd_nxt, tp->snd_una))
tp->snd_nxt = tp->snd_una;
if ((tp->t_flags & TF_FASTOPEN) &&
(tp->snd_una != tp->snd_max)) {
tp->snd_nxt = th->th_ack;
tfo_partial_ack = 1;
}
if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
tcp_timer_activate(tp, TT_DELACK,
tcp_delacktime);
else
tp->t_flags |= TF_ACKNOW;
tcp_ecn_input_syn_sent(tp, thflags, iptos);
tp->t_starttime = ticks;
if (tp->t_flags & TF_NEEDFIN) {
tp->t_acktime = ticks;
tcp_state_change(tp, TCPS_FIN_WAIT_1);
tp->t_flags &= ~TF_NEEDFIN;
thflags &= ~TH_SYN;
} else {
tcp_state_change(tp, TCPS_ESTABLISHED);
TCP_PROBE5(connect__established, NULL, tp,
m, tp, th);
cc_conn_init(tp);
tcp_timer_activate(tp, TT_KEEP,
TP_KEEPIDLE(tp));
}
} else {
tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
tcp_timer_activate(tp, TT_REXMT, 0);
tcp_state_change(tp, TCPS_SYN_RECEIVED);
}
th->th_seq++;
if (tlen > tp->rcv_wnd) {
todrop = tlen - tp->rcv_wnd;
m_adj(m, -todrop);
tlen = tp->rcv_wnd;
thflags &= ~TH_FIN;
TCPSTAT_INC(tcps_rcvpackafterwin);
TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
}
tp->snd_wl1 = th->th_seq - 1;
tp->rcv_up = th->th_seq;
if (thflags & TH_ACK)
goto process_ACK;
goto step6;
}
if (thflags & TH_RST) {
if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
(tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
KASSERT(tp->t_state != TCPS_SYN_SENT,
("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
__func__, th, tp));
if (V_tcp_insecure_rst ||
tp->last_ack_sent == th->th_seq) {
TCPSTAT_INC(tcps_drops);
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
so->so_error = ECONNREFUSED;
goto close;
case TCPS_ESTABLISHED:
case TCPS_FIN_WAIT_1:
case TCPS_FIN_WAIT_2:
case TCPS_CLOSE_WAIT:
case TCPS_CLOSING:
case TCPS_LAST_ACK:
so->so_error = ECONNRESET;
close:
default:
tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST);
tp = tcp_close(tp);
}
} else {
TCPSTAT_INC(tcps_badrst);
tcp_send_challenge_ack(tp, th, m);
m = NULL;
}
}
goto drop;
}
if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
tp->t_state != TCPS_SYN_RECEIVED) {
TCPSTAT_INC(tcps_badsyn);
if (V_tcp_insecure_syn &&
SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
tp = tcp_drop(tp, ECONNRESET);
} else {
tcp_ecn_input_syn_sent(tp, thflags, iptos);
tcp_send_challenge_ack(tp, th, m);
m = NULL;
}
goto drop;
}
if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
TSTMP_LT(to.to_tsval, tp->ts_recent)) {
if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
tp->ts_recent = 0;
} else {
TCPSTAT_INC(tcps_rcvduppack);
TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
TCPSTAT_INC(tcps_pawsdrop);
if (tlen)
goto dropafterack;
goto drop;
}
}
if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
goto dropwithreset;
}
todrop = tp->rcv_nxt - th->th_seq;
if (todrop > 0) {
if (thflags & TH_SYN) {
thflags &= ~TH_SYN;
th->th_seq++;
if (th->th_urp > 1)
th->th_urp--;
else
thflags &= ~TH_URG;
todrop--;
}
if (todrop > tlen
|| (todrop == tlen && (thflags & TH_FIN) == 0)) {
thflags &= ~TH_FIN;
tp->t_flags |= TF_ACKNOW;
todrop = tlen;
TCPSTAT_INC(tcps_rcvduppack);
TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
} else {
TCPSTAT_INC(tcps_rcvpartduppack);
TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
}
if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
tcp_update_sack_list(tp, th->th_seq,
th->th_seq + todrop);
tp->t_flags |= TF_ACKNOW;
}
drop_hdrlen += todrop;
th->th_seq += todrop;
tlen -= todrop;
if (th->th_urp > todrop)
th->th_urp -= todrop;
else {
thflags &= ~TH_URG;
th->th_urp = 0;
}
}
if ((tp->t_flags & TF_CLOSED) && tlen > 0 &&
TCPS_HAVERCVDFIN(tp->t_state) == 0) {
if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
"after socket was closed, "
"sending RST and removing tcpcb\n",
s, __func__, tcpstates[tp->t_state], tlen);
free(s, M_TCPLOG);
}
tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
tp = tcp_close(tp);
TCPSTAT_INC(tcps_rcvafterclose);
goto dropwithreset;
}
todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
if (todrop > 0) {
TCPSTAT_INC(tcps_rcvpackafterwin);
if (todrop >= tlen) {
TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
tp->t_flags |= TF_ACKNOW;
TCPSTAT_INC(tcps_rcvwinprobe);
} else
goto dropafterack;
} else
TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
m_adj(m, -todrop);
tlen -= todrop;
thflags &= ~(TH_PUSH|TH_FIN);
}
if ((to.to_flags & TOF_TS) != 0 &&
SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
((thflags & (TH_SYN|TH_FIN)) != 0))) {
tp->ts_recent_age = tcp_ts_getticks();
tp->ts_recent = to.to_tsval;
}
if ((thflags & TH_ACK) == 0) {
if (tp->t_state == TCPS_SYN_RECEIVED ||
(tp->t_flags & TF_NEEDSYN)) {
if (tp->t_state == TCPS_SYN_RECEIVED &&
(tp->t_flags & TF_FASTOPEN)) {
tp->snd_wnd = tiwin;
cc_conn_init(tp);
}
goto step6;
} else if (tp->t_flags & TF_ACKNOW)
goto dropafterack;
else
goto drop;
}
if (SEQ_GEQ(tp->snd_una, tp->iss + (TCP_MAXWIN << tp->snd_scale))) {
tp->t_flags2 |= TF2_NO_ISS_CHECK;
}
if (!V_tcp_insecure_ack) {
tcp_seq seq_min;
bool ghost_ack_check;
if (tp->t_flags2 & TF2_NO_ISS_CHECK) {
seq_min = tp->snd_una - tp->max_sndwnd;
ghost_ack_check = false;
} else {
if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) {
seq_min = tp->iss + 1;
ghost_ack_check = true;
} else {
seq_min = tp->snd_una - tp->max_sndwnd;
ghost_ack_check = false;
}
}
if (SEQ_LT(th->th_ack, seq_min)) {
if (ghost_ack_check)
TCPSTAT_INC(tcps_rcvghostack);
else
TCPSTAT_INC(tcps_rcvacktooold);
tcp_send_challenge_ack(tp, th, m);
m = NULL;
goto drop;
}
}
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
TCPSTAT_INC(tcps_connects);
if (tp->t_flags & TF_SONOTCONN) {
tp->t_flags &= ~TF_SONOTCONN;
soisconnected(so);
}
if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
(TF_RCVD_SCALE|TF_REQ_SCALE)) {
tp->rcv_scale = tp->request_r_scale;
}
tp->snd_wnd = tiwin;
tp->t_starttime = ticks;
if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) {
tcp_fastopen_decrement_counter(tp->t_tfo_pending);
tp->t_tfo_pending = NULL;
}
if (tp->t_flags & TF_NEEDFIN) {
tp->t_acktime = ticks;
tcp_state_change(tp, TCPS_FIN_WAIT_1);
tp->t_flags &= ~TF_NEEDFIN;
} else {
tcp_state_change(tp, TCPS_ESTABLISHED);
TCP_PROBE5(accept__established, NULL, tp,
m, tp, th);
if (!(tp->t_flags & TF_FASTOPEN))
cc_conn_init(tp);
tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
}
if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
incforsyn = 1;
if (tlen == 0 && (thflags & TH_FIN) == 0) {
(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
(struct mbuf *)0);
tcp_handle_wakeup(tp);
}
tp->snd_wl1 = th->th_seq - 1;
case TCPS_ESTABLISHED:
case TCPS_FIN_WAIT_1:
case TCPS_FIN_WAIT_2:
case TCPS_CLOSE_WAIT:
case TCPS_CLOSING:
case TCPS_LAST_ACK:
if (SEQ_GT(th->th_ack, tp->snd_max)) {
TCPSTAT_INC(tcps_rcvacktoomuch);
goto dropafterack;
}
if (tcp_is_sack_recovery(tp, &to)) {
sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
if ((sack_changed != SACK_NOCHANGE) &&
(tp->t_flags & TF_LRD)) {
tcp_sack_lost_retransmission(tp, th);
}
} else
tp->sackhint.sacked_bytes = 0;
#ifdef TCP_HHOOK
hhook_run_tcp_est_in(tp, th, &to);
#endif
if (SEQ_LT(th->th_ack, tp->snd_una)) {
break;
}
if (th->th_ack == tp->snd_una) {
if (tp->t_flags & TF_SACK_PERMIT) {
if (sack_changed == SACK_NOCHANGE) {
break;
}
} else {
if (!no_data || tiwin != tp->snd_wnd) {
break;
}
}
if ((thflags & TH_FIN) &&
(TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
tp->t_dupacks = 0;
break;
}
TCPSTAT_INC(tcps_rcvdupack);
maxseg = tcp_maxseg(tp);
if (!tcp_timer_active(tp, TT_REXMT)) {
tp->t_dupacks = 0;
} else if (++tp->t_dupacks > tcprexmtthresh ||
IN_FASTRECOVERY(tp->t_flags)) {
cc_ack_received(tp, th, nsegs, CC_DUPACK);
if (V_tcp_do_prr &&
IN_FASTRECOVERY(tp->t_flags) &&
(tp->t_flags & TF_SACK_PERMIT)) {
tcp_do_prr_ack(tp, th, &to,
sack_changed, &maxseg);
} else if (tcp_is_sack_recovery(tp, &to) &&
IN_FASTRECOVERY(tp->t_flags) &&
(tp->snd_nxt == tp->snd_max)) {
int awnd;
awnd = tcp_compute_pipe(tp);
if (awnd < tp->snd_ssthresh) {
tp->snd_cwnd += imax(maxseg,
imin(2 * maxseg,
tp->sackhint.delivered_data));
if (tp->snd_cwnd > tp->snd_ssthresh)
tp->snd_cwnd = tp->snd_ssthresh;
}
} else if (tcp_is_sack_recovery(tp, &to) &&
IN_FASTRECOVERY(tp->t_flags) &&
SEQ_LT(tp->snd_nxt, tp->snd_max)) {
tp->snd_cwnd += imax(maxseg,
imin(2 * maxseg,
tp->sackhint.delivered_data));
} else {
tp->snd_cwnd += maxseg;
}
(void) tcp_output(tp);
goto drop;
} else if (tp->t_dupacks == tcprexmtthresh ||
(tp->t_flags & TF_SACK_PERMIT &&
tp->sackhint.sacked_bytes >
(tcprexmtthresh - 1) * maxseg)) {
enter_recovery:
tp->t_dupacks = tcprexmtthresh;
tcp_seq onxt = tp->snd_nxt;
if (tcp_is_sack_recovery(tp, &to)) {
if (IN_FASTRECOVERY(tp->t_flags)) {
tp->t_dupacks = 0;
break;
}
} else {
if (SEQ_LEQ(th->th_ack,
tp->snd_recover)) {
tp->t_dupacks = 0;
break;
}
}
cc_cong_signal(tp, th, CC_NDUPACK);
cc_ack_received(tp, th, nsegs, CC_DUPACK);
tcp_timer_activate(tp, TT_REXMT, 0);
tp->t_rtttime = 0;
if (V_tcp_do_prr) {
if (tcp_is_sack_recovery(tp, &to)) {
tp->sackhint.prr_delivered =
imin(tp->snd_max - th->th_ack,
(tp->snd_limited + 1) * maxseg);
} else {
tp->sackhint.prr_delivered =
maxseg;
}
tp->sackhint.recover_fs = max(1,
tp->snd_nxt - tp->snd_una);
}
tp->snd_limited = 0;
if (tcp_is_sack_recovery(tp, &to)) {
TCPSTAT_INC(tcps_sack_recovery_episode);
tcp_resend_sackholes(tp);
tp->snd_nxt = tp->snd_max;
tp->snd_cwnd = tcp_compute_pipe(tp) +
maxseg;
(void) tcp_output(tp);
tp->snd_cwnd = tp->snd_ssthresh;
goto drop;
}
tp->snd_nxt = th->th_ack;
tp->snd_cwnd = maxseg;
(void) tcp_output(tp);
KASSERT(tp->snd_limited <= 2,
("%s: tp->snd_limited too big",
__func__));
tp->snd_cwnd = tp->snd_ssthresh +
maxseg *
(tp->t_dupacks - tp->snd_limited);
if (SEQ_GT(onxt, tp->snd_nxt))
tp->snd_nxt = onxt;
goto drop;
} else if (V_tcp_do_rfc3042) {
cc_ack_received(tp, th, nsegs, CC_DUPACK);
uint32_t oldcwnd = tp->snd_cwnd;
tcp_seq oldsndmax = tp->snd_max;
u_int sent;
int avail;
KASSERT(tp->t_dupacks == 1 ||
tp->t_dupacks == 2,
("%s: dupacks not 1 or 2",
__func__));
if (tp->t_dupacks == 1)
tp->snd_limited = 0;
if ((tp->snd_nxt == tp->snd_max) &&
(tp->t_rxtshift == 0))
tp->snd_cwnd =
SEQ_SUB(tp->snd_nxt, tp->snd_una);
tp->snd_cwnd +=
(tp->t_dupacks - tp->snd_limited) * maxseg;
tp->snd_cwnd -= tcp_sack_adjust(tp);
SOCK_SENDBUF_LOCK(so);
avail = sbavail(&so->so_snd);
SOCK_SENDBUF_UNLOCK(so);
if (tp->t_flags & TF_ACKNOW ||
(avail >=
SEQ_SUB(tp->snd_nxt, tp->snd_una))) {
(void) tcp_output(tp);
}
sent = SEQ_SUB(tp->snd_max, oldsndmax);
if (sent > maxseg) {
KASSERT((tp->t_dupacks == 2 &&
tp->snd_limited == 0) ||
(sent == maxseg + 1 &&
tp->t_flags & TF_SENTFIN) ||
(sent < 2 * maxseg &&
tp->t_flags & TF_NODELAY),
("%s: sent too much: %u>%u",
__func__, sent, maxseg));
tp->snd_limited = 2;
} else if (sent > 0) {
++tp->snd_limited;
}
tp->snd_cwnd = oldcwnd;
goto drop;
}
break;
}
KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
("%s: SEQ_LEQ(th_ack, snd_una)", __func__));
tp->t_dupacks = 0;
if (tcp_is_sack_recovery(tp, &to) &&
(((tp->t_rxtshift == 0) && (sack_changed != SACK_NOCHANGE)) ||
((tp->t_rxtshift > 0) && (sack_changed == SACK_NEWLOSS))) &&
(tp->snd_nxt == tp->snd_max)) {
tp->t_dupacks++;
if (!IN_FASTRECOVERY(tp->t_flags) &&
(tp->sackhint.sacked_bytes >
(tcprexmtthresh - 1) * (maxseg = tcp_maxseg(tp)))) {
goto enter_recovery;
}
}
if (SEQ_LT(th->th_ack, tp->snd_recover)) {
if (IN_FASTRECOVERY(tp->t_flags)) {
if (tp->t_flags & TF_SACK_PERMIT) {
if (V_tcp_do_prr &&
(to.to_flags & TOF_SACK)) {
tcp_timer_activate(tp,
TT_REXMT, 0);
tp->t_rtttime = 0;
tcp_do_prr_ack(tp, th, &to,
sack_changed, &maxseg);
tp->t_flags |= TF_ACKNOW;
(void) tcp_output(tp);
} else {
tcp_sack_partialack(tp, th,
&maxseg);
}
} else {
tcp_newreno_partial_ack(tp, th);
}
} else if (IN_CONGRECOVERY(tp->t_flags) &&
(V_tcp_do_prr)) {
tp->sackhint.delivered_data =
BYTES_THIS_ACK(tp, th);
tp->snd_fack = th->th_ack;
tcp_do_prr_ack(tp, th, &to, SACK_CHANGE,
&maxseg);
(void) tcp_output(tp);
}
}
if (tp->t_flags & TF_NEEDSYN) {
tp->t_flags &= ~TF_NEEDSYN;
tp->snd_una++;
if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
(TF_RCVD_SCALE|TF_REQ_SCALE)) {
tp->rcv_scale = tp->request_r_scale;
}
}
process_ACK:
INP_WLOCK_ASSERT(inp);
if (incforsyn)
tp->snd_una++;
acked = BYTES_THIS_ACK(tp, th);
KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
"(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
tp->snd_una, th->th_ack, tp, m));
TCPSTAT_ADD(tcps_rcvackpack, nsegs);
TCPSTAT_ADD(tcps_rcvackbyte, acked);
if (tp->t_rxtshift == 1 &&
tp->t_flags & TF_PREVVALID &&
tp->t_badrxtwin != 0 &&
to.to_flags & TOF_TS &&
to.to_tsecr != 0 &&
TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
cc_cong_signal(tp, th, CC_RTO_ERR);
if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
uint32_t t;
t = tcp_ts_getticks() - to.to_tsecr;
if (!tp->t_rttlow || tp->t_rttlow > t)
tp->t_rttlow = t;
tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
tp->t_rttlow = ticks - tp->t_rtttime;
tcp_xmit_timer(tp, ticks - tp->t_rtttime);
}
SOCK_SENDBUF_LOCK(so);
if ((tp->t_state <= TCPS_CLOSE_WAIT &&
acked == sbavail(&so->so_snd)) ||
acked > sbavail(&so->so_snd))
tp->t_acktime = 0;
else if (acked > 1)
tp->t_acktime = ticks;
if (th->th_ack == tp->snd_max) {
tcp_timer_activate(tp, TT_REXMT, 0);
needoutput = 1;
} else if (!tcp_timer_active(tp, TT_PERSIST))
tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
if (acked == 0) {
SOCK_SENDBUF_UNLOCK(so);
goto step6;
}
cc_ack_received(tp, th, nsegs, CC_ACK);
if (acked > sbavail(&so->so_snd)) {
if (tp->snd_wnd >= sbavail(&so->so_snd))
tp->snd_wnd -= sbavail(&so->so_snd);
else
tp->snd_wnd = 0;
mfree = sbcut_locked(&so->so_snd,
(int)sbavail(&so->so_snd));
ourfinisacked = 1;
} else {
mfree = sbcut_locked(&so->so_snd, acked);
if (tp->snd_wnd >= (uint32_t) acked)
tp->snd_wnd -= acked;
else
tp->snd_wnd = 0;
ourfinisacked = 0;
}
sowwakeup_locked(so);
m_freem(mfree);
if (!IN_RECOVERY(tp->t_flags) &&
SEQ_GT(tp->snd_una, tp->snd_recover) &&
SEQ_LEQ(th->th_ack, tp->snd_recover))
tp->snd_recover = th->th_ack - 1;
tp->snd_una = th->th_ack;
if (IN_RECOVERY(tp->t_flags) &&
SEQ_GEQ(th->th_ack, tp->snd_recover)) {
cc_post_recovery(tp, th);
}
if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
tp->snd_recover = tp->snd_una;
}
if (SEQ_LT(tp->snd_nxt, tp->snd_una))
tp->snd_nxt = tp->snd_una;
switch (tp->t_state) {
case TCPS_FIN_WAIT_1:
if (ourfinisacked) {
if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
tcp_free_sackholes(tp);
soisdisconnected(so);
tcp_timer_activate(tp, TT_2MSL,
(tcp_fast_finwait2_recycle ?
tcp_finwait2_timeout :
TP_MAXIDLE(tp)));
}
tcp_state_change(tp, TCPS_FIN_WAIT_2);
}
break;
case TCPS_CLOSING:
if (ourfinisacked) {
tcp_twstart(tp);
m_freem(m);
return;
}
break;
case TCPS_LAST_ACK:
if (ourfinisacked) {
tp = tcp_close(tp);
goto drop;
}
break;
}
}
step6:
INP_WLOCK_ASSERT(inp);
if ((thflags & TH_ACK) &&
(SEQ_LT(tp->snd_wl1, th->th_seq) ||
(tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
(tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
if (no_data && tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
TCPSTAT_INC(tcps_rcvwinupd);
tp->snd_wnd = tiwin;
tp->snd_wl1 = th->th_seq;
tp->snd_wl2 = th->th_ack;
if (tp->snd_wnd > tp->max_sndwnd)
tp->max_sndwnd = tp->snd_wnd;
needoutput = 1;
}
if ((thflags & TH_URG) && th->th_urp &&
TCPS_HAVERCVDFIN(tp->t_state) == 0) {
SOCK_RECVBUF_LOCK(so);
if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
th->th_urp = 0;
thflags &= ~TH_URG;
SOCK_RECVBUF_UNLOCK(so);
goto dodata;
}
if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
tp->rcv_up = th->th_seq + th->th_urp;
so->so_oobmark = sbavail(&so->so_rcv) +
(tp->rcv_up - tp->rcv_nxt) - 1;
if (so->so_oobmark == 0)
so->so_rcv.sb_state |= SBS_RCVATMARK;
sohasoutofband(so);
tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
}
SOCK_RECVBUF_UNLOCK(so);
if (th->th_urp <= (uint32_t)tlen &&
!(so->so_options & SO_OOBINLINE)) {
tcp_pulloutofband(so, th, m, drop_hdrlen);
}
} else {
if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
tp->rcv_up = tp->rcv_nxt;
}
dodata:
INP_WLOCK_ASSERT(inp);
tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
(tp->t_flags & TF_FASTOPEN));
if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
TCPS_HAVERCVDFIN(tp->t_state) == 0) {
tcp_seq save_start = th->th_seq;
tcp_seq save_rnxt = tp->rcv_nxt;
int save_tlen = tlen;
m_adj(m, drop_hdrlen);
if (th->th_seq == tp->rcv_nxt &&
SEGQ_EMPTY(tp) &&
(TCPS_HAVEESTABLISHED(tp->t_state) ||
tfo_syn)) {
if (DELAY_ACK(tp, tlen) || tfo_syn)
tp->t_flags |= TF_DELACK;
else
tp->t_flags |= TF_ACKNOW;
tp->rcv_nxt += tlen;
if (tlen &&
((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
(tp->t_fbyte_in == 0)) {
tp->t_fbyte_in = ticks;
if (tp->t_fbyte_in == 0)
tp->t_fbyte_in = 1;
if (tp->t_fbyte_out && tp->t_fbyte_in)
tp->t_flags2 |= TF2_FBYTES_COMPLETE;
}
thflags = tcp_get_flags(th) & TH_FIN;
TCPSTAT_INC(tcps_rcvpack);
TCPSTAT_ADD(tcps_rcvbyte, tlen);
SOCK_RECVBUF_LOCK(so);
if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
m_freem(m);
else
sbappendstream_locked(&so->so_rcv, m, 0);
tp->t_flags |= TF_WAKESOR;
} else {
tcp_seq temp = save_start;
thflags = tcp_reass(tp, th, &temp, &tlen, m);
tp->t_flags |= TF_ACKNOW;
}
if ((tp->t_flags & TF_SACK_PERMIT) &&
(save_tlen > 0) &&
TCPS_HAVEESTABLISHED(tp->t_state)) {
if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
tcp_update_sack_list(tp, save_start,
save_start + save_tlen);
} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
if ((tp->rcv_numsacks >= 1) &&
(tp->sackblks[0].end == save_start)) {
tcp_update_sack_list(tp,
tp->sackblks[0].start,
tp->sackblks[0].end);
} else {
tcp_update_dsack_list(tp, save_start,
save_start + save_tlen);
}
} else if (tlen >= save_tlen) {
tcp_update_dsack_list(tp, save_start,
save_start + save_tlen);
} else if (tlen > 0) {
tcp_update_dsack_list(tp, save_start,
save_start + tlen);
}
}
tcp_handle_wakeup(tp);
#if 0
if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
else
len = so->so_rcv.sb_hiwat;
#endif
} else {
if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
if (tlen > 0) {
if ((thflags & TH_FIN) != 0) {
log(LOG_DEBUG, "%s; %s: %s: "
"Received %d bytes of data and FIN "
"after having received a FIN, "
"just dropping both\n",
s, __func__,
tcpstates[tp->t_state], tlen);
} else {
log(LOG_DEBUG, "%s; %s: %s: "
"Received %d bytes of data "
"after having received a FIN, "
"just dropping it\n",
s, __func__,
tcpstates[tp->t_state], tlen);
}
} else {
if ((thflags & TH_FIN) != 0) {
log(LOG_DEBUG, "%s; %s: %s: "
"Received FIN "
"after having received a FIN, "
"just dropping it\n",
s, __func__,
tcpstates[tp->t_state]);
}
}
free(s, M_TCPLOG);
}
m_freem(m);
thflags &= ~TH_FIN;
}
if (thflags & TH_FIN) {
if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
socantrcvmore(so);
if (tp->t_flags & TF_NEEDSYN)
tp->t_flags |= TF_DELACK;
else
tp->t_flags |= TF_ACKNOW;
tp->rcv_nxt++;
}
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
tp->t_starttime = ticks;
case TCPS_ESTABLISHED:
tcp_state_change(tp, TCPS_CLOSE_WAIT);
break;
case TCPS_FIN_WAIT_1:
tcp_state_change(tp, TCPS_CLOSING);
break;
case TCPS_FIN_WAIT_2:
tcp_twstart(tp);
return;
}
}
TCP_PROBE3(debug__input, tp, th, m);
if (needoutput || (tp->t_flags & TF_ACKNOW)) {
(void) tcp_output(tp);
}
check_delack:
INP_WLOCK_ASSERT(inp);
if (tp->t_flags & TF_DELACK) {
tp->t_flags &= ~TF_DELACK;
tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
}
INP_WUNLOCK(inp);
return;
dropafterack:
if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
(SEQ_GT(tp->snd_una, th->th_ack) ||
SEQ_GT(th->th_ack, tp->snd_max)) ) {
tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
goto dropwithreset;
}
TCP_PROBE3(debug__input, tp, th, m);
tp->t_flags |= TF_ACKNOW;
(void) tcp_output(tp);
INP_WUNLOCK(inp);
m_freem(m);
return;
dropwithreset:
tcp_dropwithreset(m, th, tp, tlen);
if (tp != NULL) {
INP_WUNLOCK(inp);
}
return;
drop:
TCP_PROBE3(debug__input, tp, th, m);
if (tp != NULL) {
INP_WUNLOCK(inp);
}
m_freem(m);
}
void
tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int tlen)
{
#ifdef INET
struct ip *ip;
#endif
#ifdef INET6
struct ip6_hdr *ip6;
#endif
if (tp != NULL) {
INP_LOCK_ASSERT(tptoinpcb(tp));
}
if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
goto drop;
#ifdef INET6
if (mtod(m, struct ip *)->ip_v == 6) {
ip6 = mtod(m, struct ip6_hdr *);
if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
goto drop;
}
#endif
#if defined(INET) && defined(INET6)
else
#endif
#ifdef INET
{
ip = mtod(m, struct ip *);
if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
in_ifnet_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
goto drop;
}
#endif
if (badport_bandlim(BANDLIM_TCP_RST) < 0)
goto drop;
if (tcp_get_flags(th) & TH_ACK) {
tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
th->th_ack, TH_RST);
} else {
if (tcp_get_flags(th) & TH_SYN)
tlen++;
if (tcp_get_flags(th) & TH_FIN)
tlen++;
tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
(tcp_seq)0, TH_RST|TH_ACK);
}
return;
drop:
m_freem(m);
}
void
tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
{
int opt, optlen;
to->to_flags = 0;
for (; cnt > 0; cnt -= optlen, cp += optlen) {
opt = cp[0];
if (opt == TCPOPT_EOL)
break;
if (opt == TCPOPT_NOP)
optlen = 1;
else {
if (cnt < 2)
break;
optlen = cp[1];
if (optlen < 2 || optlen > cnt)
break;
}
switch (opt) {
case TCPOPT_MAXSEG:
if (optlen != TCPOLEN_MAXSEG)
continue;
if (!(flags & TO_SYN))
continue;
to->to_flags |= TOF_MSS;
bcopy((char *)cp + 2,
(char *)&to->to_mss, sizeof(to->to_mss));
to->to_mss = ntohs(to->to_mss);
break;
case TCPOPT_WINDOW:
if (optlen != TCPOLEN_WINDOW)
continue;
if (!(flags & TO_SYN))
continue;
to->to_flags |= TOF_SCALE;
to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
break;
case TCPOPT_TIMESTAMP:
if (optlen != TCPOLEN_TIMESTAMP)
continue;
to->to_flags |= TOF_TS;
bcopy((char *)cp + 2,
(char *)&to->to_tsval, sizeof(to->to_tsval));
to->to_tsval = ntohl(to->to_tsval);
bcopy((char *)cp + 6,
(char *)&to->to_tsecr, sizeof(to->to_tsecr));
to->to_tsecr = ntohl(to->to_tsecr);
break;
case TCPOPT_SIGNATURE:
if (optlen != TCPOLEN_SIGNATURE)
continue;
to->to_flags |= TOF_SIGNATURE;
to->to_signature = cp + 2;
break;
case TCPOPT_SACK_PERMITTED:
if (optlen != TCPOLEN_SACK_PERMITTED)
continue;
if (!(flags & TO_SYN))
continue;
if (!V_tcp_do_sack)
continue;
to->to_flags |= TOF_SACKPERM;
break;
case TCPOPT_SACK:
if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
continue;
if (flags & TO_SYN)
continue;
to->to_flags |= TOF_SACK;
to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
to->to_sacks = cp + 2;
TCPSTAT_INC(tcps_sack_rcv_blocks);
break;
case TCPOPT_FAST_OPEN:
if (!(flags & TO_SYN))
continue;
if (!V_tcp_fastopen_client_enable &&
!V_tcp_fastopen_server_enable)
continue;
to->to_flags |= TOF_FASTOPEN;
to->to_tfo_len = optlen - 2;
to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
break;
default:
continue;
}
}
}
void
tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
int off)
{
int cnt = off + th->th_urp - 1;
while (cnt >= 0) {
if (m->m_len > cnt) {
char *cp = mtod(m, caddr_t) + cnt;
struct tcpcb *tp = sototcpcb(so);
INP_WLOCK_ASSERT(tptoinpcb(tp));
tp->t_iobc = *cp;
tp->t_oobflags |= TCPOOB_HAVEDATA;
bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
m->m_len--;
if (m->m_flags & M_PKTHDR)
m->m_pkthdr.len--;
return;
}
cnt -= m->m_len;
m = m->m_next;
if (m == NULL)
break;
}
panic("tcp_pulloutofband");
}
void
tcp_xmit_timer(struct tcpcb *tp, int rtt)
{
int delta;
INP_WLOCK_ASSERT(tptoinpcb(tp));
TCPSTAT_INC(tcps_rttupdated);
if (tp->t_rttupdated < UCHAR_MAX)
tp->t_rttupdated++;
#ifdef STATS
stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
imax(0, rtt * 1000 / hz));
#endif
if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
delta = ((rtt - 1) << TCP_DELTA_SHIFT)
- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
if ((tp->t_srtt += delta) <= 0)
tp->t_srtt = 1;
if (delta < 0)
delta = -delta;
delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
if ((tp->t_rttvar += delta) <= 0)
tp->t_rttvar = 1;
} else {
tp->t_srtt = rtt << TCP_RTT_SHIFT;
tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
}
tp->t_rtttime = 0;
tp->t_rxtshift = 0;
TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
max(tp->t_rttmin, rtt + 2), tcp_rexmit_max);
tp->t_softerror = 0;
}
void
tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
{
int mss = 0;
uint32_t maxmtu = 0;
struct inpcb *inp = tptoinpcb(tp);
struct hc_metrics_lite metrics;
#ifdef INET6
int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
size_t min_protoh = isipv6 ?
sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
sizeof (struct tcpiphdr);
#else
size_t min_protoh = sizeof(struct tcpiphdr);
#endif
INP_WLOCK_ASSERT(inp);
if (tp->t_port)
min_protoh += V_tcp_udp_tunneling_overhead;
if (mtuoffer != -1) {
KASSERT(offer == -1, ("%s: conflict", __func__));
offer = mtuoffer - min_protoh;
}
#ifdef INET6
if (isipv6) {
maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
tp->t_maxseg = V_tcp_v6mssdflt;
}
#endif
#if defined(INET) && defined(INET6)
else
#endif
#ifdef INET
{
maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
tp->t_maxseg = V_tcp_mssdflt;
}
#endif
if (maxmtu == 0) {
if (metricptr != NULL)
bzero(metricptr, sizeof(struct hc_metrics_lite));
return;
}
switch (offer) {
case 0:
offer = tp->t_maxseg;
break;
case -1:
default:
offer = max(offer, V_tcp_minmss);
}
if (metricptr == NULL)
metricptr = &metrics;
tcp_hc_get(&inp->inp_inc, metricptr);
if (metricptr->hc_mtu)
mss = min(metricptr->hc_mtu, maxmtu) - min_protoh;
else {
#ifdef INET6
if (isipv6) {
mss = maxmtu - min_protoh;
if (!V_path_mtu_discovery &&
!in6_localaddr(&inp->in6p_faddr))
mss = min(mss, V_tcp_v6mssdflt);
}
#endif
#if defined(INET) && defined(INET6)
else
#endif
#ifdef INET
{
mss = maxmtu - min_protoh;
if (!V_path_mtu_discovery &&
!in_localaddr(inp->inp_faddr))
mss = min(mss, V_tcp_mssdflt);
}
#endif
}
mss = min(mss, offer);
mss = max(mss, 64);
tp->t_maxseg = mss;
if (tp->t_maxseg < V_tcp_mssdflt) {
tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
} else {
tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
}
}
void
tcp_mss(struct tcpcb *tp, int offer)
{
int mss;
uint32_t bufsize;
struct inpcb *inp = tptoinpcb(tp);
struct socket *so;
struct hc_metrics_lite metrics;
struct tcp_ifcap cap;
KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
bzero(&cap, sizeof(cap));
tcp_mss_update(tp, offer, -1, &metrics, &cap);
mss = tp->t_maxseg;
so = inp->inp_socket;
SOCK_SENDBUF_LOCK(so);
if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.hc_sendpipe)
bufsize = metrics.hc_sendpipe;
else
bufsize = so->so_snd.sb_hiwat;
if (bufsize < mss)
mss = bufsize;
else {
bufsize = roundup(bufsize, mss);
if (bufsize > sb_max)
bufsize = sb_max;
if (bufsize > so->so_snd.sb_hiwat)
(void)sbreserve_locked(so, SO_SND, bufsize, NULL);
}
SOCK_SENDBUF_UNLOCK(so);
tp->t_maxseg = max(mss, 64);
if (tp->t_maxseg < V_tcp_mssdflt) {
tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
} else {
tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
}
SOCK_RECVBUF_LOCK(so);
if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.hc_recvpipe)
bufsize = metrics.hc_recvpipe;
else
bufsize = so->so_rcv.sb_hiwat;
if (bufsize > mss) {
bufsize = roundup(bufsize, mss);
if (bufsize > sb_max)
bufsize = sb_max;
if (bufsize > so->so_rcv.sb_hiwat)
(void)sbreserve_locked(so, SO_RCV, bufsize, NULL);
}
SOCK_RECVBUF_UNLOCK(so);
if (cap.ifcap & CSUM_TSO) {
tp->t_flags |= TF_TSO;
tp->t_tsomax = cap.tsomax;
tp->t_tsomaxsegcount = cap.tsomaxsegcount;
tp->t_tsomaxsegsize = cap.tsomaxsegsize;
if (cap.ipsec_tso)
tp->t_flags2 |= TF2_IPSEC_TSO;
}
}
int
tcp_mssopt(struct in_conninfo *inc)
{
int mss = 0;
uint32_t thcmtu = 0;
uint32_t maxmtu = 0;
size_t min_protoh;
KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
#ifdef INET6
if (inc->inc_flags & INC_ISIPV6) {
mss = V_tcp_v6mssdflt;
maxmtu = tcp_maxmtu6(inc, NULL);
min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
}
#endif
#if defined(INET) && defined(INET6)
else
#endif
#ifdef INET
{
mss = V_tcp_mssdflt;
maxmtu = tcp_maxmtu(inc, NULL);
min_protoh = sizeof(struct tcpiphdr);
}
#endif
#if defined(INET6) || defined(INET)
thcmtu = tcp_hc_getmtu(inc);
#endif
if (maxmtu && thcmtu)
mss = min(maxmtu, thcmtu) - min_protoh;
else if (maxmtu || thcmtu)
mss = max(maxmtu, thcmtu) - min_protoh;
return (mss);
}
void
tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to,
sackstatus_t sack_changed, u_int *maxsegp)
{
int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0;
u_int maxseg;
INP_WLOCK_ASSERT(tptoinpcb(tp));
if (*maxsegp == 0) {
*maxsegp = tcp_maxseg(tp);
}
maxseg = *maxsegp;
if (tcp_is_sack_recovery(tp, to) ||
(IN_CONGRECOVERY(tp->t_flags) &&
!IN_FASTRECOVERY(tp->t_flags))) {
del_data = tp->sackhint.delivered_data;
pipe = tcp_compute_pipe(tp);
} else {
if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg +
tp->snd_recover - tp->snd_una)) {
del_data = maxseg;
}
pipe = imax(0, tp->snd_max - tp->snd_una -
imin(INT_MAX / 65536, tp->t_dupacks) * maxseg);
}
tp->sackhint.prr_delivered += del_data;
if (pipe >= tp->snd_ssthresh) {
if (tp->sackhint.recover_fs == 0)
tp->sackhint.recover_fs =
imax(1, tp->snd_nxt - tp->snd_una);
snd_cnt = howmany((long)tp->sackhint.prr_delivered *
tp->snd_ssthresh, tp->sackhint.recover_fs) -
tp->sackhint.prr_out + maxseg - 1;
} else {
if ((sack_changed == SACK_NEWLOSS) || (del_data == 0)) {
limit = tp->sackhint.prr_delivered -
tp->sackhint.prr_out;
} else {
limit = imax(tp->sackhint.prr_delivered -
tp->sackhint.prr_out, del_data) +
maxseg;
}
snd_cnt = imin((tp->snd_ssthresh - pipe), limit);
}
snd_cnt = imax(snd_cnt, 0) / maxseg;
if (IN_FASTRECOVERY(tp->t_flags)) {
if (tcp_is_sack_recovery(tp, to)) {
tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
} else {
tp->snd_cwnd = (tp->snd_max - tp->snd_una) +
(snd_cnt * maxseg);
}
} else if (IN_CONGRECOVERY(tp->t_flags)) {
tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
}
tp->snd_cwnd = imax(maxseg, tp->snd_cwnd);
}
void
tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
{
tcp_seq onxt = tp->snd_nxt;
uint32_t ocwnd = tp->snd_cwnd;
u_int maxseg = tcp_maxseg(tp);
INP_WLOCK_ASSERT(tptoinpcb(tp));
tcp_timer_activate(tp, TT_REXMT, 0);
tp->t_rtttime = 0;
if (IN_FASTRECOVERY(tp->t_flags)) {
tp->snd_nxt = th->th_ack;
tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
tp->t_flags |= TF_ACKNOW;
(void) tcp_output(tp);
tp->snd_cwnd = ocwnd;
if (SEQ_GT(onxt, tp->snd_nxt))
tp->snd_nxt = onxt;
}
if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
else
tp->snd_cwnd = 0;
tp->snd_cwnd += maxseg;
}
int
tcp_compute_pipe(struct tcpcb *tp)
{
int pipe;
if (tp->t_fb->tfb_compute_pipe != NULL) {
pipe = (*tp->t_fb->tfb_compute_pipe)(tp);
} else {
pipe = tp->snd_max - tp->snd_una +
tp->sackhint.sack_bytes_rexmit -
tp->sackhint.sacked_bytes -
tp->sackhint.lost_bytes;
}
return (imax(pipe, 0));
}
uint32_t
tcp_compute_initwnd(uint32_t maxseg)
{
if (V_tcp_initcwnd_segments)
return min(V_tcp_initcwnd_segments * maxseg,
max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
else if (V_tcp_do_rfc3390)
return min(4 * maxseg, max(2 * maxseg, 4380));
else {
if (maxseg > 2190)
return (2 * maxseg);
else if (maxseg > 1095)
return (3 * maxseg);
else
return (4 * maxseg);
}
}