#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.441 2024/10/08 06:17:14 rin Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
#include "opt_ipsec.h"
#include "opt_inet_csum.h"
#include "opt_tcp_debug.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <sys/pool.h>
#include <sys/domain.h>
#include <sys/kernel.h>
#ifdef TCP_SIGNATURE
#include <sys/md5.h>
#endif
#include <sys/lwp.h>
#include <sys/cprng.h>
#include <net/if.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>
#include <netinet/in_offload.h>
#if NARP > 0
#include <netinet/if_inarp.h>
#endif
#ifdef INET6
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_var.h>
#include <netinet/icmp6.h>
#include <netinet6/nd6.h>
#ifdef TCP_SIGNATURE
#include <netinet6/scope6_var.h>
#endif
#endif
#ifndef INET6
#include <netinet/ip6.h>
#endif
#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_private.h>
#include <netinet/tcp_congctl.h>
#include <netinet/tcp_debug.h>
#include <netinet/tcp_syncache.h>
#ifdef INET6
#include "faith.h"
#if defined(NFAITH) && NFAITH > 0
#include <net/if_faith.h>
#endif
#endif
#ifdef IPSEC
#include <netipsec/ipsec.h>
#include <netipsec/key.h>
#ifdef INET6
#include <netipsec/ipsec6.h>
#endif
#endif
#include <netinet/tcp_vtw.h>
int tcprexmtthresh = 3;
int tcp_log_refused;
int tcp_do_autorcvbuf = 1;
int tcp_autorcvbuf_inc = 16 * 1024;
int tcp_autorcvbuf_max = 256 * 1024;
int tcp_msl = (TCPTV_MSL / PR_SLOWHZ);
int tcp_reass_maxqueuelen = 100;
static int tcp_rst_ppslim_count = 0;
static struct timeval tcp_rst_ppslim_last;
static int tcp_ackdrop_ppslim_count = 0;
static struct timeval tcp_ackdrop_ppslim_last;
#define TCP_PAWS_IDLE (24U * 24 * 60 * 60 * PR_SLOWHZ)
#define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
#define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
static void
nd_hint(struct tcpcb *tp)
{
struct route *ro = NULL;
struct rtentry *rt;
if (tp == NULL)
return;
ro = &tp->t_inpcb->inp_route;
if (ro == NULL)
return;
rt = rtcache_validate(ro);
if (rt == NULL)
return;
switch (tp->t_family) {
#if NARP > 0
case AF_INET:
arp_nud_hint(rt);
break;
#endif
#ifdef INET6
case AF_INET6:
nd6_nud_hint(rt);
break;
#endif
}
rtcache_unref(rt, ro);
}
static void
tcp_setup_ack(struct tcpcb *tp, const struct tcphdr *th)
{
if (tp->t_flags & TF_DELACK ||
(tcp_ack_on_push && th->th_flags & TH_PUSH))
tp->t_flags |= TF_ACKNOW;
else
TCP_SET_DELACK(tp);
}
static void
icmp_check(struct tcpcb *tp, const struct tcphdr *th, int acked)
{
if ((tp->t_flags & TF_PMTUD_PEND) &&
SEQ_GT(th->th_ack, tp->t_pmtud_th_seq))
tp->t_flags &= ~TF_PMTUD_PEND;
if (tp->t_pmtud_mss_acked < acked)
tp->t_pmtud_mss_acked = acked;
}
static void
tcp_fields_to_host(struct tcphdr *th)
{
NTOHL(th->th_seq);
NTOHL(th->th_ack);
NTOHS(th->th_win);
NTOHS(th->th_urp);
}
static void
tcp_fields_to_net(struct tcphdr *th)
{
HTONL(th->th_seq);
HTONL(th->th_ack);
HTONS(th->th_win);
HTONS(th->th_urp);
}
static void
tcp_urp_drop(struct tcphdr *th, int todrop, int *tiflags)
{
if (th->th_urp > todrop) {
th->th_urp -= todrop;
} else {
*tiflags &= ~TH_URG;
th->th_urp = 0;
}
}
#ifdef TCP_CSUM_COUNTERS
#include <sys/device.h>
extern struct evcnt tcp_hwcsum_ok;
extern struct evcnt tcp_hwcsum_bad;
extern struct evcnt tcp_hwcsum_data;
extern struct evcnt tcp_swcsum;
#if defined(INET6)
extern struct evcnt tcp6_hwcsum_ok;
extern struct evcnt tcp6_hwcsum_bad;
extern struct evcnt tcp6_hwcsum_data;
extern struct evcnt tcp6_swcsum;
#endif
#define TCP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
#else
#define TCP_CSUM_COUNTER_INCR(ev)
#endif
#ifdef TCP_REASS_COUNTERS
#include <sys/device.h>
extern struct evcnt tcp_reass_;
extern struct evcnt tcp_reass_empty;
extern struct evcnt tcp_reass_iteration[8];
extern struct evcnt tcp_reass_prependfirst;
extern struct evcnt tcp_reass_prepend;
extern struct evcnt tcp_reass_insert;
extern struct evcnt tcp_reass_inserttail;
extern struct evcnt tcp_reass_append;
extern struct evcnt tcp_reass_appendtail;
extern struct evcnt tcp_reass_overlaptail;
extern struct evcnt tcp_reass_overlapfront;
extern struct evcnt tcp_reass_segdup;
extern struct evcnt tcp_reass_fragdup;
#define TCP_REASS_COUNTER_INCR(ev) (ev)->ev_count++
#else
#define TCP_REASS_COUNTER_INCR(ev)
#endif
static int tcp_reass(struct tcpcb *, const struct tcphdr *, struct mbuf *,
int);
static void tcp4_log_refused(const struct ip *, const struct tcphdr *);
#ifdef INET6
static void tcp6_log_refused(const struct ip6_hdr *, const struct tcphdr *);
#endif
#if defined(MBUFTRACE)
struct mowner tcp_reass_mowner = MOWNER_INIT("tcp", "reass");
#endif
static struct pool tcpipqent_pool;
void
tcpipqent_init(void)
{
pool_init(&tcpipqent_pool, sizeof(struct ipqent), 0, 0, 0, "tcpipqepl",
NULL, IPL_VM);
}
struct ipqent *
tcpipqent_alloc(void)
{
struct ipqent *ipqe;
int s;
s = splvm();
ipqe = pool_get(&tcpipqent_pool, PR_NOWAIT);
splx(s);
return ipqe;
}
void
tcpipqent_free(struct ipqent *ipqe)
{
int s;
s = splvm();
pool_put(&tcpipqent_pool, ipqe);
splx(s);
}
static int
tcp_reass(struct tcpcb *tp, const struct tcphdr *th, struct mbuf *m, int tlen)
{
struct ipqent *p, *q, *nq, *tiqe = NULL;
struct socket *so = NULL;
int pkt_flags;
tcp_seq pkt_seq;
unsigned pkt_len;
u_long rcvpartdupbyte = 0;
u_long rcvoobyte;
#ifdef TCP_REASS_COUNTERS
u_int count = 0;
#endif
net_stat_ref_t tcps;
so = tp->t_inpcb->inp_socket;
TCP_REASS_LOCK_CHECK(tp);
if (th == NULL)
goto present;
m_claimm(m, &tcp_reass_mowner);
rcvoobyte = tlen;
pkt_seq = th->th_seq;
pkt_len = tlen;
pkt_flags = th->th_flags;
TCP_REASS_COUNTER_INCR(&tcp_reass_);
if ((p = TAILQ_LAST(&tp->segq, ipqehead)) != NULL) {
if (pkt_seq == p->ipqe_seq + p->ipqe_len) {
p->ipqe_len += pkt_len;
p->ipqe_flags |= pkt_flags;
m_cat(p->ipqe_m, m);
m = NULL;
tiqe = p;
TAILQ_REMOVE(&tp->timeq, p, ipqe_timeq);
TCP_REASS_COUNTER_INCR(&tcp_reass_appendtail);
goto skip_replacement;
}
if (SEQ_GT(pkt_seq, p->ipqe_seq + p->ipqe_len)) {
TCP_REASS_COUNTER_INCR(&tcp_reass_inserttail);
goto insert_it;
}
}
q = TAILQ_FIRST(&tp->segq);
if (q != NULL) {
if (pkt_seq + pkt_len == q->ipqe_seq) {
q->ipqe_seq = pkt_seq;
q->ipqe_len += pkt_len;
q->ipqe_flags |= pkt_flags;
m_cat(m, q->ipqe_m);
q->ipqe_m = m;
tiqe = q;
TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
TCP_REASS_COUNTER_INCR(&tcp_reass_prependfirst);
goto skip_replacement;
}
} else {
TCP_REASS_COUNTER_INCR(&tcp_reass_empty);
}
for (p = NULL; q != NULL; q = nq) {
nq = TAILQ_NEXT(q, ipqe_q);
#ifdef TCP_REASS_COUNTERS
count++;
#endif
if (q->ipqe_seq + q->ipqe_len == pkt_seq) {
pkt_len += q->ipqe_len;
pkt_flags |= q->ipqe_flags;
pkt_seq = q->ipqe_seq;
m_cat(q->ipqe_m, m);
m = q->ipqe_m;
TCP_REASS_COUNTER_INCR(&tcp_reass_append);
goto free_ipqe;
}
if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
p = q;
continue;
}
if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len)) {
TCP_REASS_COUNTER_INCR(&tcp_reass_insert);
break;
}
if (SEQ_LEQ(q->ipqe_seq, pkt_seq) &&
SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVDUPPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVDUPBYTE, pkt_len);
TCP_STAT_PUTREF();
tcp_new_dsack(tp, pkt_seq, pkt_len);
m_freem(m);
if (tiqe != NULL) {
tcpipqent_free(tiqe);
}
TCP_REASS_COUNTER_INCR(&tcp_reass_segdup);
goto out;
}
if (SEQ_GEQ(q->ipqe_seq, pkt_seq) &&
SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
rcvpartdupbyte += q->ipqe_len;
m_freem(q->ipqe_m);
TCP_REASS_COUNTER_INCR(&tcp_reass_fragdup);
goto free_ipqe;
}
if (SEQ_LT(q->ipqe_seq, pkt_seq) &&
SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq;
m_adj(m, overlap);
rcvpartdupbyte += overlap;
m_cat(q->ipqe_m, m);
m = q->ipqe_m;
pkt_seq = q->ipqe_seq;
pkt_len += q->ipqe_len - overlap;
rcvoobyte -= overlap;
TCP_REASS_COUNTER_INCR(&tcp_reass_overlaptail);
goto free_ipqe;
}
if (SEQ_GT(q->ipqe_seq, pkt_seq) &&
SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len)) {
int overlap = pkt_seq + pkt_len - q->ipqe_seq;
m_adj(m, -overlap);
pkt_len -= overlap;
rcvpartdupbyte += overlap;
TCP_REASS_COUNTER_INCR(&tcp_reass_overlapfront);
rcvoobyte -= overlap;
}
if (q->ipqe_seq == pkt_seq + pkt_len) {
pkt_len += q->ipqe_len;
pkt_flags |= q->ipqe_flags;
m_cat(m, q->ipqe_m);
TAILQ_REMOVE(&tp->segq, q, ipqe_q);
TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
tp->t_segqlen--;
KASSERT(tp->t_segqlen >= 0);
KASSERT(tp->t_segqlen != 0 ||
(TAILQ_EMPTY(&tp->segq) &&
TAILQ_EMPTY(&tp->timeq)));
if (tiqe == NULL) {
tiqe = q;
} else {
tcpipqent_free(q);
}
TCP_REASS_COUNTER_INCR(&tcp_reass_prepend);
break;
}
if (SEQ_LEQ(q->ipqe_seq, pkt_seq))
p = q;
continue;
free_ipqe:
TAILQ_REMOVE(&tp->segq, q, ipqe_q);
TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
tp->t_segqlen--;
KASSERT(tp->t_segqlen >= 0);
KASSERT(tp->t_segqlen != 0 ||
(TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq)));
if (tiqe == NULL) {
tiqe = q;
} else {
tcpipqent_free(q);
}
}
#ifdef TCP_REASS_COUNTERS
if (count > 7)
TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[0]);
else if (count > 0)
TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[count]);
#endif
insert_it:
if (tp->t_segqlen > tcp_reass_maxqueuelen) {
TCP_STATINC(TCP_STAT_RCVMEMDROP);
m_freem(m);
goto out;
}
if (tiqe == NULL) {
tiqe = tcpipqent_alloc();
if (tiqe == NULL) {
TCP_STATINC(TCP_STAT_RCVMEMDROP);
m_freem(m);
goto out;
}
}
tp->t_rcvoopack++;
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVOOPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVOOBYTE, rcvoobyte);
if (rcvpartdupbyte) {
_NET_STATINC_REF(tcps, TCP_STAT_RCVPARTDUPPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVPARTDUPBYTE,
rcvpartdupbyte);
}
TCP_STAT_PUTREF();
tiqe->ipqe_m = m;
tiqe->ipqe_seq = pkt_seq;
tiqe->ipqe_len = pkt_len;
tiqe->ipqe_flags = pkt_flags;
if (p == NULL) {
TAILQ_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
} else {
TAILQ_INSERT_AFTER(&tp->segq, p, tiqe, ipqe_q);
}
tp->t_segqlen++;
skip_replacement:
TAILQ_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq);
present:
if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
goto out;
q = TAILQ_FIRST(&tp->segq);
if (q == NULL || q->ipqe_seq != tp->rcv_nxt)
goto out;
if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len)
goto out;
tp->rcv_nxt += q->ipqe_len;
pkt_flags = q->ipqe_flags & TH_FIN;
nd_hint(tp);
TAILQ_REMOVE(&tp->segq, q, ipqe_q);
TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
tp->t_segqlen--;
KASSERT(tp->t_segqlen >= 0);
KASSERT(tp->t_segqlen != 0 ||
(TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq)));
if (so->so_state & SS_CANTRCVMORE)
m_freem(q->ipqe_m);
else
sbappendstream(&so->so_rcv, q->ipqe_m);
tcpipqent_free(q);
TCP_REASS_UNLOCK(tp);
sorwakeup(so);
return pkt_flags;
out:
TCP_REASS_UNLOCK(tp);
return 0;
}
#ifdef INET6
int
tcp6_input(struct mbuf **mp, int *offp, int proto)
{
struct mbuf *m = *mp;
if (m->m_flags & M_ANYCAST6) {
struct ip6_hdr *ip6;
if (m->m_len < sizeof(struct ip6_hdr)) {
if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
TCP_STATINC(TCP_STAT_RCVSHORT);
return IPPROTO_DONE;
}
}
ip6 = mtod(m, struct ip6_hdr *);
icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
(char *)&ip6->ip6_dst - (char *)ip6);
return IPPROTO_DONE;
}
tcp_input(m, *offp, proto);
return IPPROTO_DONE;
}
#endif
static void
tcp4_log_refused(const struct ip *ip, const struct tcphdr *th)
{
char src[INET_ADDRSTRLEN];
char dst[INET_ADDRSTRLEN];
if (ip) {
in_print(src, sizeof(src), &ip->ip_src);
in_print(dst, sizeof(dst), &ip->ip_dst);
} else {
strlcpy(src, "(unknown)", sizeof(src));
strlcpy(dst, "(unknown)", sizeof(dst));
}
log(LOG_INFO,
"Connection attempt to TCP %s:%d from %s:%d\n",
dst, ntohs(th->th_dport),
src, ntohs(th->th_sport));
}
#ifdef INET6
static void
tcp6_log_refused(const struct ip6_hdr *ip6, const struct tcphdr *th)
{
char src[INET6_ADDRSTRLEN];
char dst[INET6_ADDRSTRLEN];
if (ip6) {
in6_print(src, sizeof(src), &ip6->ip6_src);
in6_print(dst, sizeof(dst), &ip6->ip6_dst);
} else {
strlcpy(src, "(unknown v6)", sizeof(src));
strlcpy(dst, "(unknown v6)", sizeof(dst));
}
log(LOG_INFO,
"Connection attempt to TCP [%s]:%d from [%s]:%d\n",
dst, ntohs(th->th_dport),
src, ntohs(th->th_sport));
}
#endif
int
tcp_input_checksum(int af, struct mbuf *m, const struct tcphdr *th,
int toff, int off, int tlen)
{
struct ifnet *rcvif;
int s;
rcvif = m_get_rcvif(m, &s);
if (__predict_false(rcvif == NULL))
goto badcsum;
switch (af) {
case AF_INET:
switch (m->m_pkthdr.csum_flags &
((rcvif->if_csum_flags_rx & M_CSUM_TCPv4) |
M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
case M_CSUM_TCPv4|M_CSUM_TCP_UDP_BAD:
TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_bad);
goto badcsum;
case M_CSUM_TCPv4|M_CSUM_DATA: {
u_int32_t hw_csum = m->m_pkthdr.csum_data;
TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_data);
if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
const struct ip *ip =
mtod(m, const struct ip *);
hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
ip->ip_dst.s_addr,
htons(hw_csum + tlen + off + IPPROTO_TCP));
}
if ((hw_csum ^ 0xffff) != 0)
goto badcsum;
break;
}
case M_CSUM_TCPv4:
TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_ok);
break;
default:
if (__predict_true(!(rcvif->if_flags & IFF_LOOPBACK) ||
tcp_do_loopback_cksum)) {
TCP_CSUM_COUNTER_INCR(&tcp_swcsum);
if (in4_cksum(m, IPPROTO_TCP, toff,
tlen + off) != 0)
goto badcsum;
}
break;
}
break;
#ifdef INET6
case AF_INET6:
switch (m->m_pkthdr.csum_flags &
((rcvif->if_csum_flags_rx & M_CSUM_TCPv6) |
M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
case M_CSUM_TCPv6|M_CSUM_TCP_UDP_BAD:
TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_bad);
goto badcsum;
#if 0
case M_CSUM_TCPv6|M_CSUM_DATA:
#endif
case M_CSUM_TCPv6:
TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_ok);
break;
default:
if (__predict_true((m->m_flags & M_LOOP) == 0 ||
tcp_do_loopback_cksum)) {
TCP_CSUM_COUNTER_INCR(&tcp6_swcsum);
if (in6_cksum(m, IPPROTO_TCP, toff,
tlen + off) != 0)
goto badcsum;
}
}
break;
#endif
}
m_put_rcvif(rcvif, &s);
return 0;
badcsum:
m_put_rcvif(rcvif, &s);
TCP_STATINC(TCP_STAT_RCVBADSUM);
return -1;
}
static void tcp_vtw_input(struct tcphdr *th, vestigial_inpcb_t *vp,
struct mbuf *m, int tlen)
{
int tiflags;
int todrop;
uint32_t t_flags = 0;
net_stat_ref_t tcps;
tiflags = th->th_flags;
todrop = vp->rcv_nxt - th->th_seq;
if (todrop > 0) {
if (tiflags & TH_SYN) {
tiflags &= ~TH_SYN;
th->th_seq++;
tcp_urp_drop(th, 1, &tiflags);
todrop--;
}
if (todrop > tlen ||
(todrop == tlen && (tiflags & TH_FIN) == 0)) {
if (tiflags & TH_RST)
goto drop;
tiflags &= ~(TH_FIN|TH_RST);
t_flags |= TF_ACKNOW;
todrop = tlen;
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVDUPPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVDUPBYTE, todrop);
TCP_STAT_PUTREF();
} else if ((tiflags & TH_RST) &&
th->th_seq != vp->rcv_nxt) {
goto dropafterack_ratelim;
} else {
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVPARTDUPPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVPARTDUPBYTE,
todrop);
TCP_STAT_PUTREF();
}
th->th_seq += todrop;
tlen -= todrop;
tcp_urp_drop(th, todrop, &tiflags);
}
if (tlen) {
TCP_STATINC(TCP_STAT_RCVAFTERCLOSE);
goto dropwithreset;
}
todrop = (th->th_seq + tlen) - (vp->rcv_nxt + vp->rcv_wnd);
if (todrop > 0) {
TCP_STATINC(TCP_STAT_RCVPACKAFTERWIN);
if (todrop >= tlen) {
TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, tlen);
if ((tiflags & TH_SYN) &&
SEQ_GT(th->th_seq, vp->rcv_nxt)) {
goto dropwithreset;
}
if (vp->rcv_wnd == 0 && th->th_seq == vp->rcv_nxt) {
t_flags |= TF_ACKNOW;
TCP_STATINC(TCP_STAT_RCVWINPROBE);
} else {
goto dropafterack;
}
} else {
TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, todrop);
}
m_adj(m, -todrop);
tlen -= todrop;
tiflags &= ~(TH_PUSH|TH_FIN);
}
if (tiflags & TH_RST) {
if (th->th_seq != vp->rcv_nxt)
goto dropafterack_ratelim;
vtw_del(vp->ctl, vp->vtw);
goto drop;
}
if ((tiflags & TH_ACK) == 0) {
if (t_flags & TF_ACKNOW)
goto dropafterack;
goto drop;
}
vtw_restart(vp);
goto dropafterack;
dropafterack:
if (tiflags & TH_RST)
goto drop;
goto dropafterack2;
dropafterack_ratelim:
if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count,
tcp_ackdrop_ppslim) == 0) {
goto drop;
}
dropafterack2:
(void)tcp_respond(0, m, m, th, th->th_seq + tlen, th->th_ack, TH_ACK);
return;
dropwithreset:
if (tiflags & TH_RST)
goto drop;
if (tiflags & TH_ACK) {
tcp_respond(0, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
} else {
if (tiflags & TH_SYN)
++tlen;
(void)tcp_respond(0, m, m, th, th->th_seq + tlen, (tcp_seq)0,
TH_RST|TH_ACK);
}
return;
drop:
m_freem(m);
}
void
tcp_input(struct mbuf *m, int off, int proto)
{
struct tcphdr *th;
struct ip *ip;
struct inpcb *inp;
#ifdef INET6
struct ip6_hdr *ip6;
#endif
u_int8_t *optp = NULL;
int optlen = 0;
int len, tlen, hdroptlen = 0;
struct tcpcb *tp = NULL;
int tiflags;
struct socket *so = NULL;
int todrop, acked, ourfinisacked, needoutput = 0;
bool dupseg;
#ifdef TCP_DEBUG
short ostate = 0;
#endif
u_long tiwin;
struct tcp_opt_info opti;
int thlen, iphlen;
int af;
struct mbuf *tcp_saveti = NULL;
uint32_t ts_rtt;
uint8_t iptos;
net_stat_ref_t tcps;
vestigial_inpcb_t vestige;
vestige.valid = 0;
MCLAIM(m, &tcp_rx_mowner);
TCP_STATINC(TCP_STAT_RCVTOTAL);
memset(&opti, 0, sizeof(opti));
opti.ts_present = 0;
opti.maxseg = 0;
if (m->m_flags & (M_BCAST|M_MCAST)) {
goto drop;
}
#ifdef INET6
if (m->m_flags & M_ANYCAST6) {
goto drop;
}
#endif
M_REGION_GET(th, struct tcphdr *, m, off, sizeof(struct tcphdr));
if (th == NULL) {
TCP_STATINC(TCP_STAT_RCVSHORT);
return;
}
if (ACCESSIBLE_POINTER(th, struct tcphdr) == 0) {
m = m_copyup(m, off + sizeof(struct tcphdr), 0);
if (m == NULL) {
TCP_STATINC(TCP_STAT_RCVSHORT);
return;
}
th = (struct tcphdr *)(mtod(m, char *) + off);
}
KASSERT(ACCESSIBLE_POINTER(th, struct tcphdr));
ip = mtod(m, struct ip *);
#ifdef INET6
ip6 = mtod(m, struct ip6_hdr *);
#endif
switch (ip->ip_v) {
case 4:
af = AF_INET;
iphlen = sizeof(struct ip);
if (IN_MULTICAST(ip->ip_dst.s_addr) ||
in_broadcast(ip->ip_dst, m_get_rcvif_NOMPSAFE(m)))
goto drop;
len = ntohs(ip->ip_len);
tlen = len - off;
iptos = ip->ip_tos;
break;
#ifdef INET6
case 6:
iphlen = sizeof(struct ip6_hdr);
af = AF_INET6;
if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
goto drop;
}
if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
goto drop;
}
len = m->m_pkthdr.len;
tlen = len - off;
iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
break;
#endif
default:
m_freem(m);
return;
}
thlen = th->th_off << 2;
if (thlen < sizeof(struct tcphdr) || thlen > tlen) {
TCP_STATINC(TCP_STAT_RCVBADOFF);
goto drop;
}
tlen -= thlen;
if (thlen > sizeof(struct tcphdr)) {
M_REGION_GET(th, struct tcphdr *, m, off, thlen);
if (th == NULL) {
TCP_STATINC(TCP_STAT_RCVSHORT);
return;
}
KASSERT(ACCESSIBLE_POINTER(th, struct tcphdr));
optlen = thlen - sizeof(struct tcphdr);
optp = ((u_int8_t *)th) + sizeof(struct tcphdr);
if ((optlen == TCPOLEN_TSTAMP_APPA ||
(optlen > TCPOLEN_TSTAMP_APPA &&
optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
be32dec(optp) == TCPOPT_TSTAMP_HDR &&
(th->th_flags & TH_SYN) == 0) {
opti.ts_present = 1;
opti.ts_val = be32dec(optp + 4);
opti.ts_ecr = be32dec(optp + 8);
optp = NULL;
}
}
tiflags = th->th_flags;
if (tcp_input_checksum(af, m, th, off, thlen, tlen))
goto badcsum;
findpcb:
inp = NULL;
switch (af) {
case AF_INET:
inp = inpcb_lookup(&tcbtable, ip->ip_src, th->th_sport,
ip->ip_dst, th->th_dport, &vestige);
if (inp == NULL && !vestige.valid) {
TCP_STATINC(TCP_STAT_PCBHASHMISS);
inp = inpcb_lookup_bound(&tcbtable, ip->ip_dst,
th->th_dport);
}
#ifdef INET6
if (inp == NULL && !vestige.valid) {
struct in6_addr s, d;
in6_in_2_v4mapin6(&ip->ip_src, &s);
in6_in_2_v4mapin6(&ip->ip_dst, &d);
inp = in6pcb_lookup(&tcbtable, &s,
th->th_sport, &d, th->th_dport, 0, &vestige);
if (inp == NULL && !vestige.valid) {
TCP_STATINC(TCP_STAT_PCBHASHMISS);
inp = in6pcb_lookup_bound(&tcbtable, &d,
th->th_dport, 0);
}
}
#endif
if (inp == NULL && !vestige.valid) {
TCP_STATINC(TCP_STAT_NOPORT);
if (tcp_log_refused &&
(tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) {
tcp4_log_refused(ip, th);
}
tcp_fields_to_host(th);
goto dropwithreset_ratelim;
}
#if defined(IPSEC)
if (ipsec_used) {
if (inp && ipsec_in_reject(m, inp))
goto drop;
}
#endif
break;
#ifdef INET6
case AF_INET6:
{
int faith;
#if defined(NFAITH) && NFAITH > 0
faith = faithprefix(&ip6->ip6_dst);
#else
faith = 0;
#endif
inp = in6pcb_lookup(&tcbtable, &ip6->ip6_src,
th->th_sport, &ip6->ip6_dst, th->th_dport, faith, &vestige);
if (inp == NULL && !vestige.valid) {
TCP_STATINC(TCP_STAT_PCBHASHMISS);
inp = in6pcb_lookup_bound(&tcbtable, &ip6->ip6_dst,
th->th_dport, faith);
}
if (inp == NULL && !vestige.valid) {
TCP_STATINC(TCP_STAT_NOPORT);
if (tcp_log_refused &&
(tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) {
tcp6_log_refused(ip6, th);
}
tcp_fields_to_host(th);
goto dropwithreset_ratelim;
}
#if defined(IPSEC)
if (ipsec_used && inp && ipsec_in_reject(m, inp))
goto drop;
#endif
break;
}
#endif
}
tcp_fields_to_host(th);
tp = NULL;
so = NULL;
if (inp) {
if (inp->inp_af == AF_INET && ip->ip_ttl < in4p_ip_minttl(inp))
goto drop;
tp = intotcpcb(inp);
so = inp->inp_socket;
} else if (vestige.valid) {
tcp_vtw_input(th, &vestige, m, tlen);
m = NULL;
goto drop;
}
if (tp == NULL)
goto dropwithreset_ratelim;
if (tp->t_state == TCPS_CLOSED)
goto drop;
KASSERT(so->so_lock == softnet_lock);
KASSERT(solocked(so));
if ((tiflags & TH_SYN) == 0)
tiwin = th->th_win << tp->snd_scale;
else
tiwin = th->th_win;
#ifdef INET6
if (inp->inp_af == AF_INET6 && (inp->inp_flags & IN6P_CONTROLOPTS)) {
m_freem(inp->inp_options);
inp->inp_options = NULL;
ip6_savecontrol(inp, &inp->inp_options, ip6, m);
}
#endif
if (so->so_options & SO_DEBUG) {
#ifdef TCP_DEBUG
ostate = tp->t_state;
#endif
tcp_saveti = NULL;
if (iphlen + sizeof(struct tcphdr) > MHLEN)
goto nosave;
if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) {
tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT);
if (tcp_saveti == NULL)
goto nosave;
} else {
MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER);
if (tcp_saveti == NULL)
goto nosave;
MCLAIM(m, &tcp_mowner);
tcp_saveti->m_len = iphlen;
m_copydata(m, 0, iphlen,
mtod(tcp_saveti, void *));
}
if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) {
m_freem(tcp_saveti);
tcp_saveti = NULL;
} else {
tcp_saveti->m_len += sizeof(struct tcphdr);
memcpy(mtod(tcp_saveti, char *) + iphlen, th,
sizeof(struct tcphdr));
}
nosave:;
}
if (so->so_options & SO_ACCEPTCONN) {
union syn_cache_sa src;
union syn_cache_sa dst;
KASSERT(tp->t_state == TCPS_LISTEN);
memset(&src, 0, sizeof(src));
memset(&dst, 0, sizeof(dst));
switch (af) {
case AF_INET:
src.sin.sin_len = sizeof(struct sockaddr_in);
src.sin.sin_family = AF_INET;
src.sin.sin_addr = ip->ip_src;
src.sin.sin_port = th->th_sport;
dst.sin.sin_len = sizeof(struct sockaddr_in);
dst.sin.sin_family = AF_INET;
dst.sin.sin_addr = ip->ip_dst;
dst.sin.sin_port = th->th_dport;
break;
#ifdef INET6
case AF_INET6:
src.sin6.sin6_len = sizeof(struct sockaddr_in6);
src.sin6.sin6_family = AF_INET6;
src.sin6.sin6_addr = ip6->ip6_src;
src.sin6.sin6_port = th->th_sport;
dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
dst.sin6.sin6_family = AF_INET6;
dst.sin6.sin6_addr = ip6->ip6_dst;
dst.sin6.sin6_port = th->th_dport;
break;
#endif
}
if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
if (tiflags & TH_RST) {
syn_cache_reset(&src.sa, &dst.sa, th);
} else if ((tiflags & (TH_ACK|TH_SYN)) ==
(TH_ACK|TH_SYN)) {
goto badsyn;
} else if (tiflags & TH_ACK) {
so = syn_cache_get(&src.sa, &dst.sa, th, so, m);
if (so == NULL) {
goto badsyn;
} else if (so == (struct socket *)(-1)) {
m = NULL;
} else {
inp = sotoinpcb(so);
tp = intotcpcb(inp);
if (tp == NULL)
goto badsyn;
tiwin <<= tp->snd_scale;
goto after_listen;
}
} else {
goto badsyn;
}
} else {
#ifdef INET6
if (af == AF_INET6 && !ip6_use_deprecated) {
struct in6_ifaddr *ia6;
int s;
struct ifnet *rcvif = m_get_rcvif(m, &s);
if (rcvif == NULL)
goto dropwithreset;
if ((ia6 = in6ifa_ifpwithaddr(rcvif,
&ip6->ip6_dst)) &&
(ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
tp = NULL;
m_put_rcvif(rcvif, &s);
goto dropwithreset;
}
m_put_rcvif(rcvif, &s);
}
#endif
if (th->th_sport == th->th_dport) {
int eq = 0;
switch (af) {
case AF_INET:
eq = in_hosteq(ip->ip_src, ip->ip_dst);
break;
#ifdef INET6
case AF_INET6:
eq = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
&ip6->ip6_dst);
break;
#endif
}
if (eq) {
TCP_STATINC(TCP_STAT_BADSYN);
goto drop;
}
}
if (so->so_qlen <= so->so_qlimit &&
syn_cache_add(&src.sa, &dst.sa, th, off,
so, m, optp, optlen, &opti))
m = NULL;
}
goto drop;
}
after_listen:
KASSERT(tp->t_state != TCPS_LISTEN);
tp->t_rcvtime = tcp_now;
if (TCPS_HAVEESTABLISHED(tp->t_state))
TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
#ifdef TCP_SIGNATURE
if (optp || (tp->t_flags & TF_SIGNATURE))
#else
if (optp)
#endif
if (tcp_dooptions(tp, optp, optlen, th, m, off, &opti) < 0)
goto drop;
if (TCP_SACK_ENABLED(tp)) {
tcp_del_sackholes(tp, th);
}
if (TCP_ECN_ALLOWED(tp)) {
if (tiflags & TH_CWR) {
tp->t_flags &= ~TF_ECN_SND_ECE;
}
switch (iptos & IPTOS_ECN_MASK) {
case IPTOS_ECN_CE:
tp->t_flags |= TF_ECN_SND_ECE;
TCP_STATINC(TCP_STAT_ECN_CE);
break;
case IPTOS_ECN_ECT0:
TCP_STATINC(TCP_STAT_ECN_ECT);
break;
case IPTOS_ECN_ECT1:
break;
}
if ((tiflags & TH_ECE) && SEQ_GEQ(tp->snd_una, tp->snd_recover))
tp->t_congctl->cong_exp(tp);
}
if (opti.ts_present && opti.ts_ecr) {
ts_rtt = TCP_TIMESTAMP(tp) - opti.ts_ecr + 1;
if (ts_rtt > TCP_PAWS_IDLE)
ts_rtt = 0;
} else {
ts_rtt = 0;
}
if (tp->t_state == TCPS_ESTABLISHED &&
(tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK))
== TH_ACK &&
(!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
th->th_seq == tp->rcv_nxt &&
tiwin && tiwin == tp->snd_wnd &&
tp->snd_nxt == tp->snd_max) {
if (opti.ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
tp->ts_recent_age = tcp_now;
tp->ts_recent = opti.ts_val;
}
if (tlen == 0) {
if (SEQ_GT(th->th_ack, tp->snd_una) &&
SEQ_LEQ(th->th_ack, tp->snd_max) &&
tp->snd_cwnd >= tp->snd_wnd &&
tp->t_partialacks < 0) {
if (ts_rtt)
tcp_xmit_timer(tp, ts_rtt - 1);
else if (tp->t_rtttime &&
SEQ_GT(th->th_ack, tp->t_rtseq))
tcp_xmit_timer(tp,
tcp_now - tp->t_rtttime);
acked = th->th_ack - tp->snd_una;
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_PREDACK);
_NET_STATINC_REF(tcps, TCP_STAT_RCVACKPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVACKBYTE,
acked);
TCP_STAT_PUTREF();
nd_hint(tp);
if (acked > (tp->t_lastoff - tp->t_inoff))
tp->t_lastm = NULL;
sbdrop(&so->so_snd, acked);
tp->t_lastoff -= acked;
icmp_check(tp, th, acked);
tp->snd_una = th->th_ack;
tp->snd_fack = tp->snd_una;
if (SEQ_LT(tp->snd_high, tp->snd_una))
tp->snd_high = tp->snd_una;
tp->snd_wl2 = tp->snd_una;
m_freem(m);
if (tp->snd_una == tp->snd_max)
TCP_TIMER_DISARM(tp, TCPT_REXMT);
else if (TCP_TIMER_ISARMED(tp,
TCPT_PERSIST) == 0)
TCP_TIMER_ARM(tp, TCPT_REXMT,
tp->t_rxtcur);
sowwakeup(so);
if (so->so_snd.sb_cc) {
KERNEL_LOCK(1, NULL);
(void)tcp_output(tp);
KERNEL_UNLOCK_ONE(NULL);
}
m_freem(tcp_saveti);
return;
}
} else if (th->th_ack == tp->snd_una &&
TAILQ_FIRST(&tp->segq) == NULL &&
tlen <= sbspace(&so->so_rcv)) {
int newsize = 0;
tp->rcv_nxt += tlen;
tp->rcv_up = tp->rcv_nxt;
tp->snd_wl1 = th->th_seq;
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_PREDDAT);
_NET_STATINC_REF(tcps, TCP_STAT_RCVPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVBYTE, tlen);
TCP_STAT_PUTREF();
nd_hint(tp);
if (tcp_do_autorcvbuf &&
opti.ts_ecr &&
(so->so_rcv.sb_flags & SB_AUTOSIZE)) {
if (opti.ts_ecr > tp->rfbuf_ts &&
opti.ts_ecr - tp->rfbuf_ts < PR_SLOWHZ) {
if (tp->rfbuf_cnt >
(so->so_rcv.sb_hiwat / 8 * 7) &&
so->so_rcv.sb_hiwat <
tcp_autorcvbuf_max) {
newsize =
uimin(so->so_rcv.sb_hiwat +
tcp_autorcvbuf_inc,
tcp_autorcvbuf_max);
}
tp->rfbuf_ts = 0;
tp->rfbuf_cnt = 0;
} else
tp->rfbuf_cnt += tlen;
}
if (so->so_state & SS_CANTRCVMORE) {
m_freem(m);
} else {
if (newsize)
if (!sbreserve(&so->so_rcv,
newsize, so))
so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
m_adj(m, off + thlen);
sbappendstream(&so->so_rcv, m);
}
sorwakeup(so);
tcp_setup_ack(tp, th);
if (tp->t_flags & TF_ACKNOW) {
KERNEL_LOCK(1, NULL);
(void)tcp_output(tp);
KERNEL_UNLOCK_ONE(NULL);
}
m_freem(tcp_saveti);
return;
}
}
hdroptlen = off + thlen;
{
int win;
win = sbspace(&so->so_rcv);
if (win < 0)
win = 0;
tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
}
tp->rfbuf_ts = 0;
tp->rfbuf_cnt = 0;
switch (tp->t_state) {
case TCPS_SYN_SENT:
if ((tiflags & TH_ACK) &&
(SEQ_LEQ(th->th_ack, tp->iss) ||
SEQ_GT(th->th_ack, tp->snd_max)))
goto dropwithreset;
if (tiflags & TH_RST) {
if (tiflags & TH_ACK)
tp = tcp_drop(tp, ECONNREFUSED);
goto drop;
}
if ((tiflags & TH_SYN) == 0)
goto drop;
if (tiflags & TH_ACK) {
tp->snd_una = th->th_ack;
if (SEQ_LT(tp->snd_nxt, tp->snd_una))
tp->snd_nxt = tp->snd_una;
if (SEQ_LT(tp->snd_high, tp->snd_una))
tp->snd_high = tp->snd_una;
TCP_TIMER_DISARM(tp, TCPT_REXMT);
if ((tiflags & TH_ECE) && tcp_do_ecn) {
tp->t_flags |= TF_ECN_PERMIT;
TCP_STATINC(TCP_STAT_ECN_SHS);
}
}
tp->irs = th->th_seq;
tcp_rcvseqinit(tp);
tp->t_flags |= TF_ACKNOW;
tcp_mss_from_peer(tp, opti.maxseg);
if (tp->t_flags & TF_SYN_REXMT)
tp->snd_cwnd = tp->t_peermss;
else {
int ss = tcp_init_win;
if (inp->inp_af == AF_INET && in_localaddr(in4p_faddr(inp)))
ss = tcp_init_win_local;
#ifdef INET6
else if (inp->inp_af == AF_INET6 && in6_localaddr(&in6p_faddr(inp)))
ss = tcp_init_win_local;
#endif
tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss);
}
tcp_rmx_rtt(tp);
if (tiflags & TH_ACK) {
TCP_STATINC(TCP_STAT_CONNECTS);
tcp_established(tp);
soisconnected(so);
if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
(TF_RCVD_SCALE|TF_REQ_SCALE)) {
tp->snd_scale = tp->requested_s_scale;
tp->rcv_scale = tp->request_r_scale;
}
TCP_REASS_LOCK(tp);
(void)tcp_reass(tp, NULL, NULL, tlen);
if (tp->t_rtttime)
tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
} else {
tp->t_state = TCPS_SYN_RECEIVED;
}
th->th_seq++;
if (tlen > tp->rcv_wnd) {
todrop = tlen - tp->rcv_wnd;
m_adj(m, -todrop);
tlen = tp->rcv_wnd;
tiflags &= ~TH_FIN;
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVPACKAFTERWIN);
_NET_STATADD_REF(tcps, TCP_STAT_RCVBYTEAFTERWIN,
todrop);
TCP_STAT_PUTREF();
}
tp->snd_wl1 = th->th_seq - 1;
tp->rcv_up = th->th_seq;
goto step6;
case TCPS_SYN_RECEIVED:
if ((tiflags & TH_ACK) &&
(SEQ_LEQ(th->th_ack, tp->iss) ||
SEQ_GT(th->th_ack, tp->snd_max)))
goto dropwithreset;
break;
}
KASSERT(tp->t_state != TCPS_LISTEN &&
tp->t_state != TCPS_SYN_SENT);
if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
TSTMP_LT(opti.ts_val, tp->ts_recent)) {
if (tcp_now - tp->ts_recent_age > TCP_PAWS_IDLE) {
tp->ts_recent = 0;
} else {
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVDUPPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVDUPBYTE, tlen);
_NET_STATINC_REF(tcps, TCP_STAT_PAWSDROP);
TCP_STAT_PUTREF();
tcp_new_dsack(tp, th->th_seq, tlen);
goto dropafterack;
}
}
todrop = tp->rcv_nxt - th->th_seq;
dupseg = false;
if (todrop > 0) {
if (tiflags & TH_SYN) {
tiflags &= ~TH_SYN;
th->th_seq++;
tcp_urp_drop(th, 1, &tiflags);
todrop--;
}
if (todrop > tlen ||
(todrop == tlen && (tiflags & TH_FIN) == 0)) {
if (tiflags & TH_RST)
goto drop;
tiflags &= ~(TH_FIN|TH_RST);
tp->t_flags |= TF_ACKNOW;
todrop = tlen;
dupseg = true;
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVDUPPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVDUPBYTE, todrop);
TCP_STAT_PUTREF();
} else if ((tiflags & TH_RST) && th->th_seq != tp->rcv_nxt) {
goto dropafterack_ratelim;
} else {
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVPARTDUPPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVPARTDUPBYTE,
todrop);
TCP_STAT_PUTREF();
}
tcp_new_dsack(tp, th->th_seq, todrop);
hdroptlen += todrop;
th->th_seq += todrop;
tlen -= todrop;
tcp_urp_drop(th, todrop, &tiflags);
}
if ((so->so_state & SS_NOFDREF) &&
tp->t_state > TCPS_CLOSE_WAIT && tlen) {
tp = tcp_close(tp);
TCP_STATINC(TCP_STAT_RCVAFTERCLOSE);
goto dropwithreset;
}
todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
if (todrop > 0) {
TCP_STATINC(TCP_STAT_RCVPACKAFTERWIN);
if (todrop >= tlen) {
TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, tlen);
if ((tiflags & TH_SYN) &&
tp->t_state == TCPS_TIME_WAIT &&
SEQ_GT(th->th_seq, tp->rcv_nxt)) {
tp = tcp_close(tp);
tcp_fields_to_net(th);
m_freem(tcp_saveti);
tcp_saveti = NULL;
goto findpcb;
}
if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
KASSERT(todrop == tlen);
tp->t_flags |= TF_ACKNOW;
TCP_STATINC(TCP_STAT_RCVWINPROBE);
} else {
goto dropafterack;
}
} else {
TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, todrop);
}
m_adj(m, -todrop);
tlen -= todrop;
tiflags &= ~(TH_PUSH|TH_FIN);
}
if (opti.ts_present &&
SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
((tiflags & (TH_SYN|TH_FIN)) != 0))) {
tp->ts_recent_age = tcp_now;
tp->ts_recent = opti.ts_val;
}
if (tiflags & TH_RST) {
if (th->th_seq != tp->rcv_nxt)
goto dropafterack_ratelim;
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:
so->so_error = ECONNRESET;
close:
tp->t_state = TCPS_CLOSED;
TCP_STATINC(TCP_STAT_DROPS);
tp = tcp_close(tp);
goto drop;
case TCPS_CLOSING:
case TCPS_LAST_ACK:
case TCPS_TIME_WAIT:
tp = tcp_close(tp);
goto drop;
}
}
if (tiflags & TH_SYN) {
if (tp->rcv_nxt == th->th_seq) {
tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack - 1,
TH_ACK);
m_freem(tcp_saveti);
return;
}
goto dropafterack_ratelim;
}
if ((tiflags & TH_ACK) == 0) {
if (tp->t_flags & TF_ACKNOW)
goto dropafterack;
goto drop;
}
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
if (SEQ_GT(tp->snd_una, th->th_ack) ||
SEQ_GT(th->th_ack, tp->snd_max))
goto dropwithreset;
TCP_STATINC(TCP_STAT_CONNECTS);
soisconnected(so);
tcp_established(tp);
if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
(TF_RCVD_SCALE|TF_REQ_SCALE)) {
tp->snd_scale = tp->requested_s_scale;
tp->rcv_scale = tp->request_r_scale;
}
TCP_REASS_LOCK(tp);
(void)tcp_reass(tp, NULL, NULL, tlen);
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:
case TCPS_TIME_WAIT:
if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
if (tlen == 0 && !dupseg && tiwin == tp->snd_wnd) {
TCP_STATINC(TCP_STAT_RCVDUPACK);
if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
th->th_ack != tp->snd_una)
tp->t_dupacks = 0;
else if (tp->t_partialacks < 0 &&
(++tp->t_dupacks == tcprexmtthresh ||
TCP_FACK_FASTRECOV(tp))) {
if (tp->t_congctl->fast_retransmit(tp, th)) {
break;
}
goto drop;
} else if (tp->t_dupacks > tcprexmtthresh) {
tp->snd_cwnd += tp->t_segsz;
KERNEL_LOCK(1, NULL);
(void)tcp_output(tp);
KERNEL_UNLOCK_ONE(NULL);
goto drop;
}
} else {
if (tlen && th->th_seq != tp->rcv_nxt &&
SEQ_LT(th->th_ack,
tp->snd_una - tp->max_sndwnd))
goto dropafterack;
}
break;
}
tp->t_congctl->fast_retransmit_newack(tp, th);
if (SEQ_GT(th->th_ack, tp->snd_max)) {
TCP_STATINC(TCP_STAT_RCVACKTOOMUCH);
goto dropafterack;
}
acked = th->th_ack - tp->snd_una;
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVACKPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVACKBYTE, acked);
TCP_STAT_PUTREF();
if (ts_rtt)
tcp_xmit_timer(tp, ts_rtt - 1);
else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
if (th->th_ack == tp->snd_max) {
TCP_TIMER_DISARM(tp, TCPT_REXMT);
needoutput = 1;
} else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
tp->t_congctl->newack(tp, th);
nd_hint(tp);
if (acked > so->so_snd.sb_cc) {
tp->snd_wnd -= so->so_snd.sb_cc;
sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
ourfinisacked = 1;
} else {
if (acked > (tp->t_lastoff - tp->t_inoff))
tp->t_lastm = NULL;
sbdrop(&so->so_snd, acked);
tp->t_lastoff -= acked;
if (tp->snd_wnd > acked)
tp->snd_wnd -= acked;
else
tp->snd_wnd = 0;
ourfinisacked = 0;
}
sowwakeup(so);
icmp_check(tp, th, acked);
tp->snd_una = th->th_ack;
if (SEQ_GT(tp->snd_una, tp->snd_fack))
tp->snd_fack = tp->snd_una;
if (SEQ_LT(tp->snd_nxt, tp->snd_una))
tp->snd_nxt = tp->snd_una;
if (SEQ_LT(tp->snd_high, tp->snd_una))
tp->snd_high = tp->snd_una;
switch (tp->t_state) {
case TCPS_FIN_WAIT_1:
if (ourfinisacked) {
if (so->so_state & SS_CANTRCVMORE) {
soisdisconnected(so);
if (tp->t_maxidle > 0)
TCP_TIMER_ARM(tp, TCPT_2MSL,
tp->t_maxidle);
}
tp->t_state = TCPS_FIN_WAIT_2;
}
break;
case TCPS_CLOSING:
if (ourfinisacked) {
tp->t_state = TCPS_TIME_WAIT;
tcp_canceltimers(tp);
TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl);
soisdisconnected(so);
}
break;
case TCPS_LAST_ACK:
if (ourfinisacked) {
tp = tcp_close(tp);
goto drop;
}
break;
case TCPS_TIME_WAIT:
TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl);
goto dropafterack;
}
}
step6:
if ((tiflags & 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 (tlen == 0 &&
tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
TCP_STATINC(TCP_STAT_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 ((tiflags & TH_URG) && th->th_urp &&
TCPS_HAVERCVDFIN(tp->t_state) == 0) {
if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
th->th_urp = 0;
tiflags &= ~TH_URG;
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 = so->so_rcv.sb_cc +
(tp->rcv_up - tp->rcv_nxt) - 1;
if (so->so_oobmark == 0)
so->so_state |= SS_RCVATMARK;
sohasoutofband(so);
tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
}
if (th->th_urp <= (u_int16_t)tlen &&
(so->so_options & SO_OOBINLINE) == 0)
tcp_pulloutofband(so, th, m, hdroptlen);
} else {
if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
tp->rcv_up = tp->rcv_nxt;
}
dodata:
if ((tlen || (tiflags & TH_FIN)) &&
TCPS_HAVERCVDFIN(tp->t_state) == 0) {
TCP_REASS_LOCK(tp);
if (th->th_seq == tp->rcv_nxt &&
TAILQ_FIRST(&tp->segq) == NULL &&
tp->t_state == TCPS_ESTABLISHED) {
tcp_setup_ack(tp, th);
tp->rcv_nxt += tlen;
tiflags = th->th_flags & TH_FIN;
tcps = TCP_STAT_GETREF();
_NET_STATINC_REF(tcps, TCP_STAT_RCVPACK);
_NET_STATADD_REF(tcps, TCP_STAT_RCVBYTE, tlen);
TCP_STAT_PUTREF();
nd_hint(tp);
if (so->so_state & SS_CANTRCVMORE) {
m_freem(m);
} else {
m_adj(m, hdroptlen);
sbappendstream(&(so)->so_rcv, m);
}
TCP_REASS_UNLOCK(tp);
sorwakeup(so);
} else {
m_adj(m, hdroptlen);
tiflags = tcp_reass(tp, th, m, tlen);
tp->t_flags |= TF_ACKNOW;
}
len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
} else {
m_freem(m);
m = NULL;
tiflags &= ~TH_FIN;
}
if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
socantrcvmore(so);
tp->t_flags |= TF_ACKNOW;
tp->rcv_nxt++;
}
switch (tp->t_state) {
case TCPS_ESTABLISHED:
tp->t_state = TCPS_CLOSE_WAIT;
break;
case TCPS_FIN_WAIT_1:
tp->t_state = TCPS_CLOSING;
break;
case TCPS_FIN_WAIT_2:
tp->t_state = TCPS_TIME_WAIT;
tcp_canceltimers(tp);
TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl);
soisdisconnected(so);
break;
case TCPS_TIME_WAIT:
TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl);
break;
}
}
#ifdef TCP_DEBUG
if (so->so_options & SO_DEBUG)
tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0);
#endif
if (needoutput || (tp->t_flags & TF_ACKNOW)) {
KERNEL_LOCK(1, NULL);
(void)tcp_output(tp);
KERNEL_UNLOCK_ONE(NULL);
}
m_freem(tcp_saveti);
if (tp->t_state == TCPS_TIME_WAIT
&& (so->so_state & SS_NOFDREF)
&& (tp->t_inpcb || af != AF_INET || af != AF_INET6)
&& ((af == AF_INET ? tcp4_vtw_enable : tcp6_vtw_enable) & 1) != 0
&& TAILQ_EMPTY(&tp->segq)
&& vtw_add(af, tp)) {
;
}
return;
badsyn:
TCP_STATINC(TCP_STAT_BADSYN);
tp = NULL;
goto dropwithreset;
dropafterack:
if (tiflags & TH_RST)
goto drop;
goto dropafterack2;
dropafterack_ratelim:
if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count,
tcp_ackdrop_ppslim) == 0) {
goto drop;
}
dropafterack2:
m_freem(m);
tp->t_flags |= TF_ACKNOW;
KERNEL_LOCK(1, NULL);
(void)tcp_output(tp);
KERNEL_UNLOCK_ONE(NULL);
m_freem(tcp_saveti);
return;
dropwithreset_ratelim:
if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count,
tcp_rst_ppslim) == 0) {
goto drop;
}
dropwithreset:
if (tiflags & TH_RST)
goto drop;
if (tiflags & TH_ACK) {
(void)tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
} else {
if (tiflags & TH_SYN)
tlen++;
(void)tcp_respond(tp, m, m, th, th->th_seq + tlen, (tcp_seq)0,
TH_RST|TH_ACK);
}
m_freem(tcp_saveti);
return;
badcsum:
drop:
if (tp) {
so = tp->t_inpcb->inp_socket;
#ifdef TCP_DEBUG
if (so && (so->so_options & SO_DEBUG) != 0)
tcp_trace(TA_DROP, ostate, tp, tcp_saveti, 0);
#endif
}
m_freem(tcp_saveti);
m_freem(m);
return;
}
#ifdef TCP_SIGNATURE
int
tcp_signature_apply(void *fstate, void *data, u_int len)
{
MD5Update(fstate, (u_char *)data, len);
return (0);
}
struct secasvar *
tcp_signature_getsav(struct mbuf *m)
{
struct ip *ip;
struct ip6_hdr *ip6;
ip = mtod(m, struct ip *);
switch (ip->ip_v) {
case 4:
ip = mtod(m, struct ip *);
ip6 = NULL;
break;
case 6:
ip = NULL;
ip6 = mtod(m, struct ip6_hdr *);
break;
default:
return (NULL);
}
#ifdef IPSEC
union sockaddr_union dst;
memset(&dst, 0, sizeof(union sockaddr_union));
if (ip != NULL) {
dst.sa.sa_len = sizeof(struct sockaddr_in);
dst.sa.sa_family = AF_INET;
dst.sin.sin_addr = ip->ip_dst;
} else {
dst.sa.sa_len = sizeof(struct sockaddr_in6);
dst.sa.sa_family = AF_INET6;
dst.sin6.sin6_addr = ip6->ip6_dst;
}
return KEY_LOOKUP_SA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI), 0, 0);
#else
return NULL;
#endif
}
int
tcp_signature(struct mbuf *m, struct tcphdr *th, int thoff,
struct secasvar *sav, char *sig)
{
MD5_CTX ctx;
struct ip *ip;
struct ipovly *ipovly;
#ifdef INET6
struct ip6_hdr *ip6;
struct ip6_hdr_pseudo ip6pseudo;
#endif
struct ippseudo ippseudo;
struct tcphdr th0;
int l, tcphdrlen;
if (sav == NULL)
return (-1);
tcphdrlen = th->th_off * 4;
switch (mtod(m, struct ip *)->ip_v) {
case 4:
MD5Init(&ctx);
ip = mtod(m, struct ip *);
memset(&ippseudo, 0, sizeof(ippseudo));
ipovly = (struct ipovly *)ip;
ippseudo.ippseudo_src = ipovly->ih_src;
ippseudo.ippseudo_dst = ipovly->ih_dst;
ippseudo.ippseudo_pad = 0;
ippseudo.ippseudo_p = IPPROTO_TCP;
ippseudo.ippseudo_len = htons(m->m_pkthdr.len - thoff);
MD5Update(&ctx, (char *)&ippseudo, sizeof(ippseudo));
break;
#if INET6
case 6:
MD5Init(&ctx);
ip6 = mtod(m, struct ip6_hdr *);
memset(&ip6pseudo, 0, sizeof(ip6pseudo));
ip6pseudo.ip6ph_src = ip6->ip6_src;
in6_clearscope(&ip6pseudo.ip6ph_src);
ip6pseudo.ip6ph_dst = ip6->ip6_dst;
in6_clearscope(&ip6pseudo.ip6ph_dst);
ip6pseudo.ip6ph_len = htons(m->m_pkthdr.len - thoff);
ip6pseudo.ip6ph_nxt = IPPROTO_TCP;
MD5Update(&ctx, (char *)&ip6pseudo, sizeof(ip6pseudo));
break;
#endif
default:
return (-1);
}
th0 = *th;
th0.th_sum = 0;
MD5Update(&ctx, (char *)&th0, sizeof(th0));
l = m->m_pkthdr.len - thoff - tcphdrlen;
if (l > 0)
m_apply(m, thoff + tcphdrlen,
m->m_pkthdr.len - thoff - tcphdrlen,
tcp_signature_apply, &ctx);
MD5Update(&ctx, _KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
MD5Final(sig, &ctx);
return (0);
}
#endif
int
tcp_dooptions(struct tcpcb *tp, const u_char *cp, int cnt, struct tcphdr *th,
struct mbuf *m, int toff, struct tcp_opt_info *oi)
{
u_int16_t mss;
int opt, optlen = 0;
#ifdef TCP_SIGNATURE
void *sigp = NULL;
char sigbuf[TCP_SIGLEN];
struct secasvar *sav = NULL;
#endif
for (; cp && 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) {
default:
continue;
case TCPOPT_MAXSEG:
if (optlen != TCPOLEN_MAXSEG)
continue;
if (!(th->th_flags & TH_SYN))
continue;
if (TCPS_HAVERCVDSYN(tp->t_state))
continue;
memcpy(&mss, cp + 2, sizeof(mss));
oi->maxseg = ntohs(mss);
break;
case TCPOPT_WINDOW:
if (optlen != TCPOLEN_WINDOW)
continue;
if (!(th->th_flags & TH_SYN))
continue;
if (TCPS_HAVERCVDSYN(tp->t_state))
continue;
tp->t_flags |= TF_RCVD_SCALE;
tp->requested_s_scale = cp[2];
if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
char buf[INET6_ADDRSTRLEN];
struct ip *ip = mtod(m, struct ip *);
#ifdef INET6
struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
#endif
switch (ip->ip_v) {
case 4:
in_print(buf, sizeof(buf),
&ip->ip_src);
break;
#ifdef INET6
case 6:
in6_print(buf, sizeof(buf),
&ip6->ip6_src);
break;
#endif
default:
strlcpy(buf, "(unknown)", sizeof(buf));
break;
}
log(LOG_ERR, "TCP: invalid wscale %d from %s, "
"assuming %d\n",
tp->requested_s_scale, buf,
TCP_MAX_WINSHIFT);
tp->requested_s_scale = TCP_MAX_WINSHIFT;
}
break;
case TCPOPT_TIMESTAMP:
if (optlen != TCPOLEN_TIMESTAMP)
continue;
oi->ts_present = 1;
memcpy(&oi->ts_val, cp + 2, sizeof(oi->ts_val));
NTOHL(oi->ts_val);
memcpy(&oi->ts_ecr, cp + 6, sizeof(oi->ts_ecr));
NTOHL(oi->ts_ecr);
if (!(th->th_flags & TH_SYN))
continue;
if (TCPS_HAVERCVDSYN(tp->t_state))
continue;
tp->t_flags |= TF_RCVD_TSTMP;
tp->ts_recent = oi->ts_val;
tp->ts_recent_age = tcp_now;
break;
case TCPOPT_SACK_PERMITTED:
if (optlen != TCPOLEN_SACK_PERMITTED)
continue;
if (!(th->th_flags & TH_SYN))
continue;
if (TCPS_HAVERCVDSYN(tp->t_state))
continue;
if (tcp_do_sack) {
tp->t_flags |= TF_SACK_PERMIT;
tp->t_flags |= TF_WILL_SACK;
}
break;
case TCPOPT_SACK:
tcp_sack_option(tp, th, cp, optlen);
break;
#ifdef TCP_SIGNATURE
case TCPOPT_SIGNATURE:
if (optlen != TCPOLEN_SIGNATURE)
continue;
if (sigp &&
!consttime_memequal(sigp, cp + 2, TCP_SIGLEN))
return (-1);
sigp = sigbuf;
memcpy(sigbuf, cp + 2, TCP_SIGLEN);
tp->t_flags |= TF_SIGNATURE;
break;
#endif
}
}
#ifndef TCP_SIGNATURE
return 0;
#else
if (tp->t_flags & TF_SIGNATURE) {
sav = tcp_signature_getsav(m);
if (sav == NULL && tp->t_state == TCPS_LISTEN)
return (-1);
}
if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE))
goto out;
if (sigp) {
char sig[TCP_SIGLEN];
tcp_fields_to_net(th);
if (tcp_signature(m, th, toff, sav, sig) < 0) {
tcp_fields_to_host(th);
goto out;
}
tcp_fields_to_host(th);
if (!consttime_memequal(sig, sigp, TCP_SIGLEN)) {
TCP_STATINC(TCP_STAT_BADSIG);
goto out;
} else
TCP_STATINC(TCP_STAT_GOODSIG);
key_sa_recordxfer(sav, m);
KEY_SA_UNREF(&sav);
}
return 0;
out:
if (sav != NULL)
KEY_SA_UNREF(&sav);
return -1;
#endif
}
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, char *) + cnt;
struct tcpcb *tp = sototcpcb(so);
tp->t_iobc = *cp;
tp->t_oobflags |= TCPOOB_HAVEDATA;
memmove(cp, cp + 1, (unsigned)(m->m_len - cnt - 1));
m->m_len--;
return;
}
cnt -= m->m_len;
m = m->m_next;
if (m == NULL)
break;
}
panic("tcp_pulloutofband");
}
void
tcp_xmit_timer(struct tcpcb *tp, uint32_t rtt)
{
int32_t delta;
TCP_STATINC(TCP_STAT_RTTUPDATED);
if (tp->t_srtt != 0) {
delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
if ((tp->t_srtt += delta) <= 0)
tp->t_srtt = 1 << 2;
if (delta < 0)
delta = -delta;
delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
if ((tp->t_rttvar += delta) <= 0)
tp->t_rttvar = 1 << 2;
if (__predict_false(tcp_rttlocal) && tcp_msl_enable
&& tp->t_srtt > tcp_msl_remote_threshold
&& tp->t_msl < tcp_msl_remote) {
tp->t_msl = MIN(tcp_msl_remote, TCP_MAXMSL);
}
} else {
tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
}
tp->t_rtttime = 0;
tp->t_rxtshift = 0;
TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
uimax(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
tp->t_softerror = 0;
}