#if ISFB31
#include "opt_ipfw.h"
#include "opt_tcpdebug.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/syslog.h>
#include <kern/cpu_number.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>
#include <netinet/icmp_var.h>
#include <netinet/in_pcb.h>
#if INET6
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet6/nd6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_pcb.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/tcpip.h>
#if TCPDEBUG
#include <netinet/tcp_debug.h>
#if INET6
union {
struct ip _tcp_si4;
struct ip6_hdr _tcp_si6;
} tcp_saveip;
#else
struct ip tcp_saveip;
#endif
struct tcphdr tcp_savetcp;
#endif
#if IPSEC
#include <netinet6/ipsec.h>
#include <netkey/key.h>
#endif
#include <sys/kdebug.h>
#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
#define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
#define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
#define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
static int tcprexmtthresh = 3;
tcp_seq tcp_iss;
tcp_cc tcp_ccgen;
extern int apple_hwcksum_rx;
struct tcpstat tcpstat;
SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats,
CTLFLAG_RD, &tcpstat , tcpstat, "");
int log_in_vain = 0;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
&log_in_vain, 0, "");
int tcp_delack_enabled = 1;
SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
&tcp_delack_enabled, 0, "");
u_long tcp_now;
struct inpcbhead tcb;
#define tcb6 tcb
struct inpcbinfo tcbinfo;
static void tcp_dooptions __P((struct tcpcb *,
u_char *, int, struct tcphdr *, struct tcpopt *));
static void tcp_pulloutofband __P((struct socket *,
struct tcphdr *, struct mbuf *));
static void tcp_xmit_timer __P((struct tcpcb *, int));
#define ND6_HINT(tp) { \
if ((tp) && (tp)->t_inpcb && (tp)->t_inpcb->in6p_route.ro_rt) \
nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL); \
}
extern u_long current_active_connections;
extern u_long last_active_conn_count;
extern u_long *delack_bitmask;
#if INET6
#define _ONLY_IF_INET6_(x) x
#else
#define _ONLY_IF_INET6_(x)
#endif
#define TCP_REASS(tp, th, tilen, m, so, flags, isipv6, needwakeup) { \
if ((th)->th_seq == (tp)->rcv_nxt && \
(tp)->segq.lh_first == NULL && \
(tp)->t_state == TCPS_ESTABLISHED) { \
if (tcp_delack_enabled) {\
if (last_active_conn_count > DELACK_BITMASK_THRESH) \
TCP_DELACK_BITSET(tp->t_inpcb->hash_element); \
tp->t_flags |= TF_DELACK; \
} \
else \
tp->t_flags |= TF_ACKNOW; \
(tp)->rcv_nxt += (tilen); \
flags = (th)->th_flags & TH_FIN; \
tcpstat.tcps_rcvpack++;\
tcpstat.tcps_rcvbyte += (tilen);\
_ONLY_IF_INET6_(ND6_HINT(tp);) \
sbappend(&(so)->so_rcv, (m)); \
needwakeup++; \
} else { \
(flags) = tcp_reass((tp), (th), (tilen), (m), (isipv6)); \
tp->t_flags |= TF_ACKNOW; \
} \
}
int
tcp_reass(tp, th, tilen, m, isipv6)
register struct tcpcb *tp;
register struct tcphdr *th;
u_int16_t tilen;
struct mbuf *m;
#if INET6
int isipv6;
#endif
{
register struct ipqent *p, *q, *nq, *tiqe;
struct socket *so = tp->t_inpcb->inp_socket;
int flags;
if (th == 0)
goto present;
#if 0
m->m_pkthdr.header = ti;
#endif
MALLOC(tiqe, struct ipqent *, sizeof (struct ipqent), M_SONAME, M_NOWAIT);
if (tiqe == NULL) {
tcpstat.tcps_rcvmemdrop++;
m_freem(m);
return (0);
}
for (p = NULL, q = tp->segq.lh_first; q != NULL;
p = q, q = q->ipqe_q.le_next)
if (SEQ_GT(q->ipqe_tcp->ti_seq, th->th_seq))
break;
if (p != NULL) {
register struct tcpiphdr *phdr = p->ipqe_tcp;
register int i;
i = phdr->ti_seq + phdr->ti_len - th->th_seq;
if (i > 0) {
if (i >= tilen) {
tcpstat.tcps_rcvduppack++;
tcpstat.tcps_rcvdupbyte += tilen;
m_freem(m);
FREE(tiqe, M_SONAME);
#if 1
goto present;
#endif
}
m_adj(m, i);
tilen -= i;
th->th_seq += i;
}
}
tcpstat.tcps_rcvoopack++;
tcpstat.tcps_rcvoobyte += tilen;
while (q) {
register struct tcpiphdr *qhdr = q->ipqe_tcp;
register int i = (th->th_seq + tilen) - qhdr->ti_seq;
if (i <= 0)
break;
if (i < qhdr->ti_len) {
qhdr->ti_seq += i;
qhdr->ti_len -= i;
m_adj(q->ipqe_m, i);
break;
}
nq = q->ipqe_q.le_next;
m_freem(q->ipqe_m);
LIST_REMOVE(q, ipqe_q);
FREE(q, M_SONAME);
q = nq;
}
tiqe->ipqe_m = m;
#define thtoti(x) \
((struct tcpiphdr *)(((char *)(x)) - (sizeof (struct ip))))
tiqe->ipqe_tcp = thtoti(th);
tiqe->ipqe_tcp->ti_len = tilen;
if (p == NULL) {
LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
} else {
LIST_INSERT_AFTER(p, tiqe, ipqe_q);
}
present:
if (!TCPS_HAVEESTABLISHED(tp->t_state))
return (0);
q = tp->segq.lh_first;
if (!q || q->ipqe_tcp->ti_seq != tp->rcv_nxt)
return (0);
#if 0
if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_tcp->ti_len)
return (0);
#endif
do {
tp->rcv_nxt += q->ipqe_tcp->ti_len;
flags = q->ipqe_tcp->ti_flags & TH_FIN;
nq = q->ipqe_q.le_next;
LIST_REMOVE(q, ipqe_q);
if (so->so_state & SS_CANTRCVMORE)
m_freem(q->ipqe_m);
else
sbappend(&so->so_rcv, q->ipqe_m);
FREE(q, M_SONAME);
q = nq;
} while (q && q->ipqe_tcp->ti_seq == tp->rcv_nxt);
#if INET6
if (isipv6)
ND6_HINT(tp);
#endif
KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
(((thtoti(th)->ti_src.s_addr & 0xffff) << 16) | (thtoti(th)->ti_dst.s_addr & 0xffff)),
th->th_seq, th->th_ack, th->th_win);
sorwakeup(so);
return (flags);
}
#if INET6
int
tcp6_input(mp, offp, proto)
struct mbuf **mp;
int *offp, proto;
{
tcp_input(*mp, *offp);
return IPPROTO_DONE;
}
#endif
void
tcp_input(m, off)
struct mbuf *m;
int off;
{
register struct tcphdr *th;
register struct ip *ip = NULL;
register struct ipovly *ipov;
register struct inpcb *inp;
u_char *optp = NULL;
int optlen = 0;
int len, toff;
int hdroptlen;
u_int16_t tilen;
register struct tcpcb *tp = 0;
register int thflags;
struct socket *so = 0;
int todrop, acked, ourfinisacked, needoutput = 0;
struct in_addr laddr;
#if 0
struct in6_addr laddr6;
#endif
int dropsocket = 0;
int iss = 0;
u_long tiwin;
struct tcpopt to;
struct rmxp_tao *taop;
struct rmxp_tao tao_noncached;
int need_sowwakeup = 0;
int need_sorwakeup = 0;
#if TCPDEBUG
short ostate = 0;
#endif
#if INET6
struct ip6_hdr *ip6 = NULL;
int lgminh;
#else
#define lgminh (sizeof (struct tcpiphdr))
#endif
int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
struct proc *proc0=current_proc();
KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_START,0,0,0,0,0);
bzero((char *)&to, sizeof(to));
tcpstat.tcps_rcvtotal++;
th = mtod(m, struct tcpiphdr *);
KERNEL_DEBUG(DBG_LAYER_BEG, ((th->th_dport << 16) | th->th_sport),
(((thtoti(th)->ti_src.s_addr & 0xffff) << 16) | (thtoti(th)->ti_dst.s_addr & 0xffff)),
th->th_seq, th->th_ack, th->th_win);
#if INET6
if (isipv6) {
ip6 = mtod(m, struct ip6_hdr *);
lgminh = sizeof(struct tcpip6hdr);
} else {
lgminh = sizeof(struct tcpiphdr);
#endif
ip = mtod(m, struct ip *);
ipov = (struct ipovly *)ip;
#if INET6
}
#endif
#if INET6
if (isipv6 &&
m && (m->m_flags & M_ANYCAST6)) {
icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
(caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
return;
}
#endif
#if INET6
if (isipv6) {
IP6_EXTHDR_CHECK(m, off, sizeof(struct tcphdr), );
ip6 = mtod(m, struct ip6_hdr *);
tilen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
if (in6_cksum(m, IPPROTO_TCP, off, tilen)) {
tcpstat.tcps_rcvbadsum++;
goto drop;
}
th = (struct tcphdr *)((caddr_t)ip6 + off);
} else
#endif
{
if (off > sizeof (struct ip)) {
ip_stripoptions(m, (struct mbuf *)0);
off = sizeof(struct ip);
if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16)
m->m_pkthdr.csum_flags = 0;
}
if (m->m_len < lgminh) {
if ((m = m_pullup(m, lgminh)) == 0) {
tcpstat.tcps_rcvshort++;
return;
}
}
ip = mtod(m, struct ip *);
ipov = (struct ipovly *)ip;
th = (struct tcphdr *)((caddr_t)ip + off);
tilen = ip->ip_len;
len = sizeof (struct ip) + tilen;
if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
if (apple_hwcksum_rx && (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16)) {
u_short pseudo;
bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
ipov->ih_len = (u_short)tilen;
HTONS(ipov->ih_len);
pseudo = in_cksum(m, sizeof (struct ip));
th->th_sum = in_addword(pseudo, (m->m_pkthdr.csum_data & 0xFFFF));
}
else {
if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
th->th_sum = m->m_pkthdr.csum_data;
else goto dotcpcksum;
}
th->th_sum ^= 0xffff;
} else {
dotcpcksum:
if (th->th_sum) {
len = sizeof (struct ip) + tilen;
bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
ipov->ih_len = (u_short)tilen;
HTONS(ipov->ih_len);
th = (struct tcphdr *)((caddr_t)ip + off);
th->th_sum = in_cksum(m, len);
}
}
if (th->th_sum) {
tcpstat.tcps_rcvbadsum++;
goto drop;
}
}
toff = th->th_off << 2;
if (toff < sizeof (struct tcphdr) || toff > tilen) {
tcpstat.tcps_rcvbadoff++;
goto drop;
}
tilen -= toff;
if (toff > sizeof (struct tcphdr)) {
#if INET6
if (isipv6) {
IP6_EXTHDR_CHECK(m, off, toff, );
ip6 = mtod(m, struct ip6_hdr *);
th = (struct tcphdr *)((caddr_t)ip6 + off);
} else
#endif
{
if (m->m_len < sizeof(struct ip) + toff) {
if ((m = m_pullup(m, sizeof (struct ip) + toff)) == 0) {
tcpstat.tcps_rcvshort++;
return;
}
ip = mtod(m, struct ip *);
ipov = (struct ipovly *)ip;
th = (struct tcphdr *)((caddr_t)ip + off);
}
}
optlen = toff - sizeof (struct tcphdr);
optp = (u_char *)(th + 1);
if ((optlen == TCPOLEN_TSTAMP_APPA ||
(optlen > TCPOLEN_TSTAMP_APPA &&
optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
*(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
(th->th_flags & TH_SYN) == 0) {
to.to_flag |= TOF_TS;
to.to_tsval = ntohl(*(u_int32_t *)(optp + 4));
to.to_tsecr = ntohl(*(u_int32_t *)(optp + 8));
optp = NULL;
}
}
thflags = th->th_flags;
NTOHL(th->th_seq);
NTOHL(th->th_ack);
NTOHS(th->th_win);
NTOHS(th->th_urp);
hdroptlen = off+toff;
m->m_data += hdroptlen;
m->m_len -= hdroptlen;
findpcb:
#if IPFIREWALL_FORWARD
if (ip_fw_fwd_addr != NULL
#if INET6
&& isipv6 == NULL
#endif
) {
inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
if (!inp) {
if (!ip_fw_fwd_addr->sin_port) {
inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
th->th_sport, ip_fw_fwd_addr->sin_addr,
th->th_dport, 1, m->m_pkthdr.rcvif);
} else {
inp = in_pcblookup_hash(&tcbinfo,
ip->ip_src, th->th_sport,
ip_fw_fwd_addr->sin_addr,
ntohs(ip_fw_fwd_addr->sin_port), 1,
m->m_pkthdr.rcvif);
}
}
ip_fw_fwd_addr = NULL;
} else
#endif
#if INET6
if (isipv6)
inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
&ip6->ip6_dst, th->th_dport, 1,
m->m_pkthdr.rcvif);
else
#endif
inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
#if IPSEC
m->m_data -= hdroptlen;
m->m_len += hdroptlen;
#if INET6
if (isipv6) {
if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
ipsec6stat.in_polvio++;
goto drop;
}
} else
#endif
if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
ipsecstat.in_polvio++;
goto drop;
}
m->m_data += hdroptlen;
m->m_len -= hdroptlen;
#endif
if (inp == NULL) {
if (log_in_vain && thflags & TH_SYN) {
#if INET6
char buf[INET6_ADDRSTRLEN];
#else
char buf[4*sizeof "123"];
#endif
#if INET6
if (isipv6) {
strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
log(LOG_INFO,
"Connection attempt to TCP %s:%d from %s:%d\n",
buf, ntohs(th->th_dport),
ip6_sprintf(&ip6->ip6_src),
ntohs(th->th_sport));
} else {
#endif
strcpy(buf, inet_ntoa(ip->ip_dst));
log(LOG_INFO,
"Connection attempt to TCP %s:%d from %s:%d\n",
buf, ntohs(th->th_dport), inet_ntoa(ip->ip_src),
ntohs(th->th_sport));
#if INET6
}
#endif
}
#if ICMP_BANDLIM
if (badport_bandlim(1) < 0)
goto drop;
#endif
goto dropwithreset;
}
tp = intotcpcb(inp);
if (tp == 0)
goto dropwithreset;
if (tp->t_state == TCPS_CLOSED)
goto drop;
if (tp->t_state == TCP_NSTATES)
goto drop;
if ((thflags & TH_SYN) == 0)
tiwin = th->th_win << tp->snd_scale;
else
tiwin = th->th_win;
so = inp->inp_socket;
if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
#if TCPDEBUG
if (so->so_options & SO_DEBUG) {
ostate = tp->t_state;
#if INET6
if (isipv6)
tcp_saveip._tcp_si6 = *ip6;
else
tcp_saveip._tcp_si4 = *ip;
#else
tcp_saveip = *ip;
#endif
tcp_savetcp = *th;
}
#endif
if (so->so_options & SO_ACCEPTCONN) {
register struct tcpcb *tp0 = tp;
struct socket *so2;
#if IPSEC
struct socket *oso;
#endif
#if INET6
struct inpcb *oinp = sotoinpcb(so);
#endif
#if !IPSEC
if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
if (thflags & TH_ACK) {
tcpstat.tcps_badsyn++;
goto dropwithreset;
}
goto drop;
}
#endif
KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_START,0,0,0,0,0);
so2 = sonewconn(so, 0);
if (so2 == 0) {
tcpstat.tcps_listendrop++;
so2 = sodropablereq(so);
if (so2) {
tcp_drop(sototcpcb(so2), ETIMEDOUT);
so2 = sonewconn(so, 0);
}
if (!so2)
goto drop;
}
#if IPSEC
oso = so;
#endif
so = so2;
dropsocket++;
inp = (struct inpcb *)so->so_pcb;
#if INET6
if (isipv6)
inp->in6p_laddr = ip6->ip6_dst;
else {
if (ip6_mapped_addr_on) {
inp->inp_vflag &= ~INP_IPV6;
inp->inp_vflag |= INP_IPV4;
}
#endif
inp->inp_laddr = ip->ip_dst;
#if INET6
}
#endif
inp->inp_lport = th->th_dport;
if (in_pcbinshash(inp) != 0) {
#if INET6
if (isipv6)
inp->in6p_laddr = in6addr_any;
else
#endif
inp->inp_laddr.s_addr = INADDR_ANY;
inp->inp_lport = 0;
goto drop;
}
#if IPSEC
if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
if (thflags & TH_ACK) {
tcpstat.tcps_badsyn++;
goto dropwithreset;
}
goto drop;
}
#endif
#if INET6
if (isipv6) {
struct ip6_recvpktopts newopts;
inp->inp_flags |=
oinp->inp_flags & INP_CONTROLOPTS;
if (oinp->in6p_outputopts)
inp->in6p_outputopts =
ip6_copypktopts(oinp->in6p_outputopts,
M_NOWAIT);
} else
#endif
inp->inp_options = ip_srcroute();
#if IPSEC
if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp,
inp->inp_sp))
printf("tcp_input: could not copy policy\n");
#endif
tp = intotcpcb(inp);
tp->t_state = TCPS_LISTEN;
tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
tp->request_r_scale++;
KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_END,0,0,0,0,0);
}
}
#if INET6
if (isipv6 && (inp->in6p_flags & INP_CONTROLOPTS) != 0) {
struct ip6_recvpktopts opts6;
#ifndef DEFER_MADJ
m->m_data -= hdroptlen;
m->m_len += hdroptlen;
#endif
ip6_savecontrol(inp, ip6, m, &opts6, &inp->in6p_inputopts);
if (inp->in6p_inputopts)
ip6_update_recvpcbopt(inp->in6p_inputopts, &opts6);
if (opts6.head) {
if (sbappendcontrol(&inp->in6p_socket->so_rcv,
NULL, opts6.head)
== 0)
m_freem(opts6.head);
}
#ifndef DEFER_MADJ
m->m_data += hdroptlen;
m->m_len -= hdroptlen;
#endif
}
#endif
tp->t_idle = 0;
if (TCPS_HAVEESTABLISHED(tp->t_state))
tp->t_timer[TCPT_KEEP] = tcp_keepidle;
if (tp->t_state != TCPS_LISTEN && optp)
tcp_dooptions(tp, optp, optlen, th, &to);
if (th->th_flags & TH_SYN)
tcp_mss(tp, to.to_maxseg, isipv6);
if (tp->t_state == TCPS_ESTABLISHED &&
(thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
((to.to_flag & TOF_TS) == 0 ||
TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
th->th_seq == tp->rcv_nxt &&
tiwin && tiwin == tp->snd_wnd &&
tp->snd_nxt == tp->snd_max) {
if ((to.to_flag & TOF_TS) != 0 &&
SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
tp->ts_recent_age = tcp_now;
tp->ts_recent = to.to_tsval;
}
if (tilen == 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_dupacks < tcprexmtthresh) {
++tcpstat.tcps_predack;
if ((to.to_flag & TOF_TS) != 0)
tcp_xmit_timer(tp,
tcp_now - to.to_tsecr + 1);
else if (tp->t_rtt &&
SEQ_GT(th->th_ack, tp->t_rtseq))
tcp_xmit_timer(tp, tp->t_rtt);
acked = th->th_ack - tp->snd_una;
tcpstat.tcps_rcvackpack++;
tcpstat.tcps_rcvackbyte += acked;
sbdrop(&so->so_snd, acked);
tp->snd_una = th->th_ack;
m_freem(m);
#if INET6
if (isipv6)
ND6_HINT(tp);
#endif
if (tp->snd_una == tp->snd_max)
tp->t_timer[TCPT_REXMT] = 0;
else if (tp->t_timer[TCPT_PERSIST] == 0)
tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
if (so->so_snd.sb_cc)
(void) tcp_output(tp);
sowwakeup(so);
KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
return;
}
} else if (th->th_ack == tp->snd_una &&
tp->segq.lh_first == NULL &&
tilen <= sbspace(&so->so_rcv)) {
++tcpstat.tcps_preddat;
tp->rcv_nxt += tilen;
tcpstat.tcps_rcvpack++;
tcpstat.tcps_rcvbyte += tilen;
#if INET6
if (isipv6)
ND6_HINT(tp);
#endif
sbappend(&so->so_rcv, m);
KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
(((thtoti(th)->ti_src.s_addr & 0xffff) << 16) | (thtoti(th)->ti_dst.s_addr & 0xffff)),
th->th_seq, th->th_ack, th->th_win);
if (tcp_delack_enabled) {
if (last_active_conn_count > DELACK_BITMASK_THRESH)
TCP_DELACK_BITSET(tp->t_inpcb->hash_element);
tp->t_flags |= TF_DELACK;
} else {
tp->t_flags |= TF_ACKNOW;
tcp_output(tp);
}
sorwakeup(so);
KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
return;
}
}
{ int win;
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_LISTEN: {
register struct sockaddr_in *sin;
#if 0
register struct sockaddr_in6 *sin6;
#endif
if (thflags & TH_RST)
goto drop;
if (thflags & TH_ACK)
goto dropwithreset;
if ((thflags & TH_SYN) == 0)
goto drop;
if (th->th_dport == th->th_sport) {
#if INET6
if (isipv6) {
if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
&ip6->ip6_src))
goto drop;
} else
#endif
if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
goto drop;
}
#if INET6
if (isipv6) {
if (m->m_flags & (M_BCAST|M_MCAST) ||
IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
goto drop;
#if 1
if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
inp->in6p_laddr = ip6->ip6_dst;
inp->in6p_faddr = ip6->ip6_src;
inp->inp_fport = th->th_sport;
in_pcbrehash(inp);
#else
MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6,
M_SONAME, M_NOWAIT);
if (sin6 == NULL)
goto drop;
bzero(sin6, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_addr = ip6->ip6_src;
sin6->sin6_port = th->th_sport;
laddr6 = inp->in6p_laddr;
if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
inp->in6p_laddr = ip6->ip6_dst;
if (in6_pcbconnect(inp, (struct sockaddr *)sin6,
&proc0)) {
inp->in6p_laddr = laddr6;
FREE(sin6, M_SONAME);
goto drop;
}
FREE(sin6, M_SONAME);
#endif
}
else {
#endif
if (m->m_flags & (M_BCAST|M_MCAST))
goto drop;
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_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
goto drop;
MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
M_NOWAIT);
if (sin == NULL)
goto drop;
sin->sin_family = AF_INET;
sin->sin_len = sizeof(*sin);
sin->sin_addr = ip->ip_src;
sin->sin_port = th->th_sport;
bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
laddr = inp->inp_laddr;
if (inp->inp_laddr.s_addr == INADDR_ANY)
inp->inp_laddr = ip->ip_dst;
if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) {
inp->inp_laddr = laddr;
FREE(sin, M_SONAME);
goto drop;
}
FREE(sin, M_SONAME);
#if INET6
}
#endif
tp->t_template = tcp_template(tp);
if (tp->t_template == 0) {
tp = tcp_drop(tp, ENOBUFS);
dropsocket = 0;
goto drop;
}
if ((taop = tcp_gettaocache(inp)) == NULL) {
taop = &tao_noncached;
bzero(taop, sizeof(*taop));
}
tcp_dooptions(tp, optp, optlen, th, &to);
if (th->th_flags & TH_SYN)
tcp_mss(tp, to.to_maxseg, isipv6);
if (iss)
tp->iss = iss;
else {
#ifdef TCP_COMPAT_42
tcp_iss += TCP_ISSINCR/2;
tp->iss = tcp_iss;
#else
tp->iss = tcp_rndiss_next();
#endif
}
tp->irs = th->th_seq;
tcp_sendseqinit(tp);
tcp_rcvseqinit(tp);
tp->snd_wnd = tiwin;
tp->cc_send = CC_INC(tcp_ccgen);
tp->cc_recv = to.to_cc;
if ((to.to_flag & TOF_CC) != 0) {
if (((tp->t_flags & TF_NOPUSH) != 0) &&
taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
taop->tao_cc = to.to_cc;
if (tp->t_state != TCPS_ESTABLISHED)
current_active_connections++;
tp->t_state = TCPS_ESTABLISHED;
if (tcp_delack_enabled &&
((thflags & TH_FIN) ||
(tilen != 0 &&
#if INET6
(isipv6 && in6_localaddr(&inp->in6p_faddr))
||
(!isipv6 &&
#endif
in_localaddr(inp->inp_faddr)
#if INET6
)
#endif
))) {
if (last_active_conn_count > DELACK_BITMASK_THRESH)
TCP_DELACK_BITSET(tp->t_inpcb->hash_element);
tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
}
else
tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
tcpstat.tcps_connects++;
soisconnected(so);
tp->t_timer[TCPT_KEEP] = tcp_keepinit;
dropsocket = 0;
tcpstat.tcps_accepts++;
goto trimthenstep6;
}
} else {
taop->tao_cc = 0;
}
tp->t_flags |= TF_ACKNOW;
tp->t_state = TCPS_SYN_RECEIVED;
tp->t_timer[TCPT_KEEP] = tcp_keepinit;
dropsocket = 0;
tcpstat.tcps_accepts++;
goto trimthenstep6;
}
case TCPS_SYN_RECEIVED:
if ((thflags & TH_ACK) &&
(SEQ_LEQ(th->th_ack, tp->snd_una) ||
SEQ_GT(th->th_ack, tp->snd_max)))
goto dropwithreset;
break;
case TCPS_SYN_SENT:
if ((taop = tcp_gettaocache(inp)) == NULL) {
taop = &tao_noncached;
bzero(taop, sizeof(*taop));
}
if ((thflags & TH_ACK) &&
(SEQ_LEQ(th->th_ack, tp->iss) ||
SEQ_GT(th->th_ack, tp->snd_max))) {
if (taop->tao_ccsent != 0)
goto drop;
else
goto dropwithreset;
}
if (thflags & TH_RST) {
if (thflags & TH_ACK) {
tp = tcp_drop(tp, ECONNREFUSED);
postevent(so, 0, EV_RESET);
}
goto drop;
}
if ((thflags & TH_SYN) == 0)
goto drop;
tp->snd_wnd = th->th_win;
tp->cc_recv = to.to_cc;
tp->irs = th->th_seq;
tcp_rcvseqinit(tp);
if (thflags & TH_ACK) {
if (to.to_flag & TOF_CCECHO) {
if (tp->cc_send != to.to_ccecho)
if (taop->tao_ccsent != 0)
goto drop;
else
goto dropwithreset;
} else
tp->t_flags &= ~TF_RCVD_CC;
tcpstat.tcps_connects++;
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;
}
if (taop->tao_ccsent == 0)
taop->tao_ccsent = to.to_ccecho;
tp->rcv_adv += tp->rcv_wnd;
tp->snd_una++;
if (tcp_delack_enabled && tilen != 0) {
if (last_active_conn_count > DELACK_BITMASK_THRESH)
TCP_DELACK_BITSET(tp->t_inpcb->hash_element);
tp->t_flags |= TF_DELACK;
}
else
tp->t_flags |= TF_ACKNOW;
if (tp->t_flags & TF_NEEDFIN) {
tp->t_state = TCPS_FIN_WAIT_1;
tp->t_flags &= ~TF_NEEDFIN;
thflags &= ~TH_SYN;
} else {
if (tp->t_state != TCPS_ESTABLISHED)
current_active_connections++;
tp->t_state = TCPS_ESTABLISHED;
tp->t_timer[TCPT_KEEP] = tcp_keepidle;
}
} else {
tp->t_flags |= TF_ACKNOW;
tp->t_timer[TCPT_REXMT] = 0;
if (to.to_flag & TOF_CC) {
if (taop->tao_cc != 0 &&
CC_GT(to.to_cc, taop->tao_cc)) {
taop->tao_cc = to.to_cc;
if (tp->t_flags & TF_NEEDFIN) {
tp->t_state = TCPS_FIN_WAIT_1;
tp->t_flags &= ~TF_NEEDFIN;
} else {
if (tp->t_state != TCPS_ESTABLISHED)
current_active_connections++;
tp->t_state = TCPS_ESTABLISHED;
tp->t_timer[TCPT_KEEP] = tcp_keepidle;
}
tp->t_flags |= TF_NEEDSYN;
} else
tp->t_state = TCPS_SYN_RECEIVED;
} else {
taop->tao_cc = 0;
tp->t_state = TCPS_SYN_RECEIVED;
}
}
trimthenstep6:
th->th_seq++;
if (tilen > tp->rcv_wnd) {
todrop = tilen - tp->rcv_wnd;
m_adj(m, -todrop);
tilen = tp->rcv_wnd;
thflags &= ~TH_FIN;
tcpstat.tcps_rcvpackafterwin++;
tcpstat.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;
case TCPS_LAST_ACK:
case TCPS_CLOSING:
case TCPS_TIME_WAIT:
if ((thflags & TH_SYN) &&
(to.to_flag & TOF_CC) && tp->cc_recv != 0) {
if (tp->t_state == TCPS_TIME_WAIT &&
tp->t_duration > TCPTV_MSL)
goto dropwithreset;
if (CC_GT(to.to_cc, tp->cc_recv)) {
tp = tcp_close(tp);
goto findpcb;
}
else
goto drop;
}
break;
}
if (thflags & TH_RST) {
if (tp->last_ack_sent == th->th_seq) {
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
so->so_error = ECONNREFUSED;
goto close;
case TCPS_ESTABLISHED:
case TCPS_FIN_WAIT_1:
case TCPS_CLOSE_WAIT:
current_active_connections--;
case TCPS_FIN_WAIT_2:
so->so_error = ECONNRESET;
close:
postevent(so, 0, EV_RESET);
tp->t_state = TCPS_CLOSED;
tcpstat.tcps_drops++;
tp = tcp_close(tp);
break;
case TCPS_CLOSING:
case TCPS_LAST_ACK:
current_active_connections--;
tp = tcp_close(tp);
break;
case TCPS_TIME_WAIT:
break;
}
}
goto drop;
}
if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent &&
TSTMP_LT(to.to_tsval, tp->ts_recent)) {
if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
tp->ts_recent = 0;
} else {
tcpstat.tcps_rcvduppack++;
tcpstat.tcps_rcvdupbyte += tilen;
tcpstat.tcps_pawsdrop++;
goto dropafterack;
}
}
if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
goto dropafterack;
if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs))
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 > tilen
|| (todrop == tilen && (thflags & TH_FIN) == 0)) {
thflags &= ~TH_FIN;
tp->t_flags |= TF_ACKNOW;
todrop = tilen;
tcpstat.tcps_rcvduppack++;
tcpstat.tcps_rcvdupbyte += todrop;
} else {
tcpstat.tcps_rcvpartduppack++;
tcpstat.tcps_rcvpartdupbyte += todrop;
}
m_adj(m, todrop);
th->th_seq += todrop;
tilen -= todrop;
if (th->th_urp > todrop)
th->th_urp -= todrop;
else {
thflags &= ~TH_URG;
th->th_urp = 0;
}
}
if ((so->so_state & SS_NOFDREF) &&
tp->t_state > TCPS_CLOSE_WAIT && tilen) {
tp = tcp_close(tp);
tcpstat.tcps_rcvafterclose++;
goto dropwithreset;
}
todrop = (th->th_seq+tilen) - (tp->rcv_nxt+tp->rcv_wnd);
if (todrop > 0) {
tcpstat.tcps_rcvpackafterwin++;
if (todrop >= tilen) {
tcpstat.tcps_rcvbyteafterwin += tilen;
if (thflags & TH_SYN &&
tp->t_state == TCPS_TIME_WAIT &&
SEQ_GT(th->th_seq, tp->rcv_nxt)) {
#ifdef TCP_COMPAT_42
iss = tp->rcv_nxt + TCP_ISSINCR;
#else
iss = tcp_rndiss_next();
#endif
tp = tcp_close(tp);
goto findpcb;
}
if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
tp->t_flags |= TF_ACKNOW;
tcpstat.tcps_rcvwinprobe++;
} else
goto dropafterack;
} else
tcpstat.tcps_rcvbyteafterwin += todrop;
m_adj(m, -todrop);
tilen -= todrop;
thflags &= ~(TH_PUSH|TH_FIN);
}
if ((to.to_flag & TOF_TS) != 0 &&
SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
tp->ts_recent_age = tcp_now;
tp->ts_recent = to.to_tsval;
}
if (thflags & TH_SYN) {
tp = tcp_drop(tp, ECONNRESET);
postevent(so, 0, EV_RESET);
goto dropwithreset;
}
if ((thflags & TH_ACK) == 0) {
if (tp->t_state == TCPS_SYN_RECEIVED ||
(tp->t_flags & TF_NEEDSYN))
goto step6;
else
goto drop;
}
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
tcpstat.tcps_connects++;
soisconnected(so);
current_active_connections++;
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;
}
if ((taop = tcp_gettaocache(inp)) != NULL &&
taop->tao_cc == 0)
taop->tao_cc = tp->cc_recv;
if (tp->t_flags & TF_NEEDFIN) {
tp->t_state = TCPS_FIN_WAIT_1;
tp->t_flags &= ~TF_NEEDFIN;
} else {
tp->t_state = TCPS_ESTABLISHED;
tp->t_timer[TCPT_KEEP] = tcp_keepidle;
}
if (tilen == 0 && (thflags & TH_FIN) == 0)
(void) tcp_reass(tp, (struct tcphdr *)0, 0,
(struct mbuf *)0, isipv6);
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 (tilen == 0 && tiwin == tp->snd_wnd) {
tcpstat.tcps_rcvdupack++;
if (tp->t_timer[TCPT_REXMT] == 0 ||
th->th_ack != tp->snd_una)
tp->t_dupacks = 0;
else if (++tp->t_dupacks == tcprexmtthresh) {
tcp_seq onxt = tp->snd_nxt;
u_int win =
min(tp->snd_wnd, tp->snd_cwnd) / 2 /
tp->t_maxseg;
if (win < 2)
win = 2;
tp->snd_ssthresh = win * tp->t_maxseg;
tp->t_timer[TCPT_REXMT] = 0;
tp->t_rtt = 0;
tp->snd_nxt = th->th_ack;
tp->snd_cwnd = tp->t_maxseg;
(void) tcp_output(tp);
tp->snd_cwnd = tp->snd_ssthresh +
tp->t_maxseg * tp->t_dupacks;
if (SEQ_GT(onxt, tp->snd_nxt))
tp->snd_nxt = onxt;
goto drop;
} else if (tp->t_dupacks > tcprexmtthresh) {
tp->snd_cwnd += tp->t_maxseg;
(void) tcp_output(tp);
goto drop;
}
} else
tp->t_dupacks = 0;
break;
}
if (tp->t_dupacks >= tcprexmtthresh &&
tp->snd_cwnd > tp->snd_ssthresh)
tp->snd_cwnd = tp->snd_ssthresh;
tp->t_dupacks = 0;
if (SEQ_GT(th->th_ack, tp->snd_max)) {
tcpstat.tcps_rcvacktoomuch++;
goto dropafterack;
}
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->snd_scale = tp->requested_s_scale;
tp->rcv_scale = tp->request_r_scale;
}
}
process_ACK:
acked = th->th_ack - tp->snd_una;
tcpstat.tcps_rcvackpack++;
tcpstat.tcps_rcvackbyte += acked;
if (to.to_flag & TOF_TS)
tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1);
else if (tp->t_rtt && SEQ_GT(th->th_ack, tp->t_rtseq))
tcp_xmit_timer(tp,tp->t_rtt);
if (th->th_ack == tp->snd_max) {
tp->t_timer[TCPT_REXMT] = 0;
needoutput = 1;
} else if (tp->t_timer[TCPT_PERSIST] == 0)
tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
if (acked == 0)
goto step6;
{
register u_int cw = tp->snd_cwnd;
register u_int incr = tp->t_maxseg;
if (cw > tp->snd_ssthresh)
incr = incr * incr / cw;
tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
}
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 {
sbdrop(&so->so_snd, acked);
tp->snd_wnd -= acked;
ourfinisacked = 0;
}
need_sowwakeup++;
tp->snd_una = th->th_ack;
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_state & SS_CANTRCVMORE) {
soisdisconnected(so);
tp->t_timer[TCPT_2MSL] = tcp_maxidle;
}
add_to_time_wait(tp);
current_active_connections--;
tp->t_state = TCPS_FIN_WAIT_2;
}
break;
case TCPS_CLOSING:
if (ourfinisacked) {
tp->t_state = TCPS_TIME_WAIT;
tcp_canceltimers(tp);
if (tp->cc_recv != 0 &&
tp->t_duration < TCPTV_MSL)
tp->t_timer[TCPT_2MSL] =
tp->t_rxtcur * TCPTV_TWTRUNC;
else
tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
add_to_time_wait(tp);
current_active_connections--;
soisdisconnected(so);
}
break;
case TCPS_LAST_ACK:
if (ourfinisacked) {
tp = tcp_close(tp);
goto drop;
}
break;
case TCPS_TIME_WAIT:
tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
add_to_time_wait(tp);
goto dropafterack;
}
}
step6:
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 (tilen == 0 &&
tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
tcpstat.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) {
if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
th->th_urp = 0;
thflags &= ~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;
postevent(so, 0, EV_OOB);
}
sohasoutofband(so);
tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
}
if (th->th_urp <= (u_long)tilen
#if SO_OOBINLINE
&& (so->so_options & SO_OOBINLINE) == 0
#endif
)
tcp_pulloutofband(so, th, m);
} else
if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
tp->rcv_up = tp->rcv_nxt;
dodata:
if ((tilen || (thflags&TH_FIN)) &&
TCPS_HAVERCVDFIN(tp->t_state) == 0) {
TCP_REASS(tp, th, tilen, m, so, thflags, isipv6, need_sorwakeup);
if (tp->t_flags & TF_DELACK)
{
KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
(((thtoti(th)->ti_src.s_addr & 0xffff) << 16) | (thtoti(th)->ti_dst.s_addr & 0xffff)),
th->th_seq, th->th_ack, th->th_win);
}
len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
} else {
m_freem(m);
thflags &= ~TH_FIN;
}
if (thflags & TH_FIN) {
if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
socantrcvmore(so);
postevent(so, 0, EV_FIN);
if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN)) {
if (last_active_conn_count > DELACK_BITMASK_THRESH)
TCP_DELACK_BITSET(tp->t_inpcb->hash_element);
tp->t_flags |= TF_DELACK;
}
else
tp->t_flags |= TF_ACKNOW;
tp->rcv_nxt++;
}
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
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);
if (tp->cc_recv != 0 &&
tp->t_duration < TCPTV_MSL) {
tp->t_timer[TCPT_2MSL] =
tp->t_rxtcur * TCPTV_TWTRUNC;
tp->t_flags |= TF_ACKNOW;
}
else
tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
add_to_time_wait(tp);
soisdisconnected(so);
break;
case TCPS_TIME_WAIT:
tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
add_to_time_wait(tp);
break;
}
}
#if TCPDEBUG
if (so->so_options & SO_DEBUG) {
#if INET6
if (isipv6)
tcp_saveip._tcp_si6.ip6_plen = tilen;
else
tcp_saveip._tcp_si4.ip_len = tilen;
#else
tcp_saveip.ip_len = tilen;
#endif
tcp_trace(TA_INPUT, ostate, tp, (void *)&tcp_saveip,
&tcp_savetcp, 0);
}
#endif
if (needoutput || (tp->t_flags & TF_ACKNOW))
(void) tcp_output(tp);
if (need_sorwakeup)
sorwakeup(so);
if (need_sowwakeup)
sowwakeup(so);
KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
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)) )
goto dropwithreset;
#if TCPDEBUG
if (so->so_options & SO_DEBUG) {
#if INET6
if (isipv6)
tcp_saveip._tcp_si6.ip6_plen = tilen;
else
tcp_saveip._tcp_si4.ip_len = tilen;
#else
tcp_saveip.ip_len = tilen;
#endif
tcp_trace(TA_DROP, ostate, tp, (void *)&tcp_saveip,
&tcp_savetcp, 0);
}
#endif
m_freem(m);
tp->t_flags |= TF_ACKNOW;
(void) tcp_output(tp);
if (need_sorwakeup)
sorwakeup(so);
if (need_sowwakeup)
sowwakeup(so);
KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
return;
dropwithreset:
if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
goto drop;
#if INET6
if (isipv6) {
if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
goto drop;
} else
#endif
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_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
goto drop;
#if TCPDEBUG
if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) {
if (tp == 0) {
#if INET6
if (isipv6)
tcp_saveip._tcp_si6 = *ip6;
else
tcp_saveip._tcp_si4 = *ip;
#else
tcp_saveip = *ip;
#endif
}
#if INET6
if (isipv6)
tcp_saveip._tcp_si6.ip6_plen = tilen;
else
tcp_saveip._tcp_si4.ip_len = tilen;
#else
tcp_saveip.ip_len = tilen;
#endif
tcp_trace(TA_DROP, ostate, tp, (void *)&tcp_saveip,
&tcp_savetcp, 0);
}
#endif
if (thflags & TH_ACK)
#if INET6
tcp_respond(tp, isipv6 ? (void *)ip6 : (void *)ip, th, m,
(tcp_seq)0, th->th_ack, TH_RST, isipv6);
#else
tcp_respond(tp, (void *)ip, th, m,
(tcp_seq)0, th->th_ack, TH_RST, isipv6);
#endif
else {
if (thflags & TH_SYN)
tilen++;
#if INET6
tcp_respond(tp, isipv6 ? (void *)ip6 : (void *)ip, th, m,
th->th_seq+tilen, (tcp_seq)0, TH_RST|TH_ACK,
isipv6);
#else
tcp_respond(tp, (void *)ip, th, m,
th->th_seq+tilen, (tcp_seq)0, TH_RST|TH_ACK,
isipv6);
#endif
}
if (need_sorwakeup)
sorwakeup(so);
if (need_sowwakeup)
sowwakeup(so);
if (dropsocket)
(void) soabort(so);
KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
return;
drop:
#if TCPDEBUG
if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) {
if (tp == 0) {
#if INET6
if (isipv6)
tcp_saveip._tcp_si6 = *ip6;
else
tcp_saveip._tcp_si4 = *ip;
#else
tcp_saveip = *ip;
#endif
}
#if INET6
if (isipv6)
tcp_saveip._tcp_si6.ip6_plen = tilen;
else
tcp_saveip._tcp_si4.ip_len = tilen;
#else
tcp_saveip.ip_len = tilen;
#endif
tcp_trace(TA_DROP, ostate, tp, (void *)&tcp_saveip,
&tcp_savetcp, 0);
}
#endif
m_freem(m);
if (need_sorwakeup)
sorwakeup(so);
if (need_sowwakeup)
sowwakeup(so);
if (dropsocket)
(void) soabort(so);
KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
return;
}
static void
tcp_dooptions(tp, cp, cnt, th, to)
struct tcpcb *tp;
u_char *cp;
int cnt;
struct tcphdr *th;
struct tcpopt *to;
{
u_short mss = 0;
int opt, optlen;
for (; cnt > 0; cnt -= optlen, cp += optlen) {
opt = cp[0];
if (opt == TCPOPT_EOL)
break;
if (opt == TCPOPT_NOP)
optlen = 1;
else {
optlen = cp[1];
if (optlen <= 0)
break;
}
switch (opt) {
default:
continue;
case TCPOPT_MAXSEG:
if (optlen != TCPOLEN_MAXSEG)
continue;
if (!(th->th_flags & TH_SYN))
continue;
bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
to->to_maxseg = ntohs(mss);
break;
case TCPOPT_WINDOW:
if (optlen != TCPOLEN_WINDOW)
continue;
if (!(th->th_flags & TH_SYN))
continue;
tp->t_flags |= TF_RCVD_SCALE;
tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
break;
case TCPOPT_TIMESTAMP:
if (optlen != TCPOLEN_TIMESTAMP)
continue;
to->to_flag |= TOF_TS;
bcopy((char *)cp + 2,
(char *)&to->to_tsval, sizeof(to->to_tsval));
NTOHL(to->to_tsval);
bcopy((char *)cp + 6,
(char *)&to->to_tsecr, sizeof(to->to_tsecr));
NTOHL(to->to_tsecr);
if (th->th_flags & TH_SYN) {
tp->t_flags |= TF_RCVD_TSTMP;
tp->ts_recent = to->to_tsval;
tp->ts_recent_age = tcp_now;
}
break;
case TCPOPT_CC:
if (optlen != TCPOLEN_CC)
continue;
to->to_flag |= TOF_CC;
bcopy((char *)cp + 2,
(char *)&to->to_cc, sizeof(to->to_cc));
NTOHL(to->to_cc);
if (th->th_flags & TH_SYN)
tp->t_flags |= TF_RCVD_CC;
break;
case TCPOPT_CCNEW:
if (optlen != TCPOLEN_CC)
continue;
if (!(th->th_flags & TH_SYN))
continue;
to->to_flag |= TOF_CCNEW;
bcopy((char *)cp + 2,
(char *)&to->to_cc, sizeof(to->to_cc));
NTOHL(to->to_cc);
tp->t_flags |= TF_RCVD_CC;
break;
case TCPOPT_CCECHO:
if (optlen != TCPOLEN_CC)
continue;
if (!(th->th_flags & TH_SYN))
continue;
to->to_flag |= TOF_CCECHO;
bcopy((char *)cp + 2,
(char *)&to->to_ccecho, sizeof(to->to_ccecho));
NTOHL(to->to_ccecho);
break;
}
}
}
static void
tcp_pulloutofband(so, th, m)
struct socket *so;
struct tcphdr *th;
register struct mbuf *m;
{
int cnt = th->th_urp - 1;
while (cnt >= 0) {
if (m->m_len > cnt) {
char *cp = mtod(m, caddr_t) + cnt;
struct tcpcb *tp = sototcpcb(so);
tp->t_iobc = *cp;
tp->t_oobflags |= TCPOOB_HAVEDATA;
bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
m->m_len--;
return;
}
cnt -= m->m_len;
m = m->m_next;
if (m == 0)
break;
}
panic("tcp_pulloutofband");
}
static void
tcp_xmit_timer(tp, rtt)
register struct tcpcb *tp;
short rtt;
{
register int delta;
tcpstat.tcps_rttupdated++;
tp->t_rttupdated++;
if (tp->t_srtt != 0) {
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_rtt = 0;
tp->t_rxtshift = 0;
TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
tp->t_softerror = 0;
}
void
tcp_mss(tp, offer, isipv6)
struct tcpcb *tp;
int offer;
#if INET6
int isipv6;
#endif
{
register struct rtentry *rt;
struct ifnet *ifp;
register int rtt, mss;
u_long bufsize;
struct inpcb *inp;
struct socket *so;
struct rmxp_tao *taop;
int origoffer = offer;
#if INET6
int lgminh = isipv6 ? sizeof (struct tcpip6hdr) :
sizeof (struct tcpiphdr);
#else
#define lgminh (sizeof (struct tcpiphdr))
#endif
inp = tp->t_inpcb;
#if INET6
if (isipv6)
rt = tcp_rtlookup6(inp);
else
#endif
rt = tcp_rtlookup(inp);
if (rt == NULL) {
tp->t_maxopd = tp->t_maxseg =
#if INET6
isipv6 ? tcp_v6mssdflt :
#endif
tcp_mssdflt;
return;
}
ifp = rt->rt_ifp;
so = inp->inp_socket;
taop = rmx_taop(rt->rt_rmx);
if (offer == -1)
offer = taop->tao_mssopt;
if (offer == 0)
offer =
#if INET6
isipv6 ? tcp_v6mssdflt :
#endif
tcp_mssdflt;
else
offer = max(offer, 64);
taop->tao_mssopt = offer;
if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
if (rt->rt_rmx.rmx_locks & RTV_RTT)
tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
tcpstat.tcps_usedrtt++;
if (rt->rt_rmx.rmx_rttvar) {
tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
(RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
tcpstat.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, TCPTV_REXMTMAX);
}
if (rt->rt_rmx.rmx_mtu)
mss = rt->rt_rmx.rmx_mtu - lgminh;
else
mss =
#if INET6
isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu :
#endif
ifp->if_mtu - lgminh;
if (rt->rt_rmx.rmx_mtu == 0) {
#if INET6
if (isipv6) {
if (!in6_localaddr(&inp->in6p_faddr))
mss = min(mss, tcp_v6mssdflt);
} else
#endif
if (!in_localaddr(inp->inp_faddr))
mss = min(mss, tcp_mssdflt);
}
mss = min(mss, offer);
tp->t_maxopd = mss;
if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
(origoffer == -1 ||
(tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
mss -= TCPOLEN_TSTAMP_APPA;
if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
(origoffer == -1 ||
(tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
mss -= TCPOLEN_CC_APPA;
#if (MCLBYTES & (MCLBYTES - 1)) == 0
if (mss > MCLBYTES)
mss &= ~(MCLBYTES-1);
#else
if (mss > MCLBYTES)
mss = mss / MCLBYTES * MCLBYTES;
#endif
#if RTV_SPIPE
if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
#endif
bufsize = so->so_snd.sb_hiwat;
if (bufsize < mss)
mss = bufsize;
else {
bufsize = roundup(bufsize, mss);
if (bufsize > sb_max)
bufsize = sb_max;
(void)sbreserve(&so->so_snd, bufsize);
}
tp->t_maxseg = mss;
#if RTV_RPIPE
if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
#endif
bufsize = so->so_rcv.sb_hiwat;
if (bufsize > mss) {
bufsize = roundup(bufsize, mss);
if (bufsize > sb_max)
bufsize = sb_max;
(void)sbreserve(&so->so_rcv, bufsize);
}
#if INET6
if (isipv6) {
if (!in6_localaddr(&inp->in6p_faddr))
tp->snd_cwnd = mss;
} else
#endif
if (!in_localaddr(inp->inp_faddr))
tp->snd_cwnd = mss;
if (rt->rt_rmx.rmx_ssthresh) {
tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
tcpstat.tcps_usedssthresh++;
}
}
int
tcp_mssopt(tp, isipv6)
struct tcpcb *tp;
#if INET6
int isipv6;
#endif
{
struct rtentry *rt;
int mss;
#if INET6
int lgminh = isipv6 ? sizeof (struct tcpip6hdr) :
sizeof (struct tcpiphdr);
#else
#define lgminh (sizeof (struct tcpiphdr))
#endif
#if INET6
if (isipv6)
rt = tcp_rtlookup6(tp->t_inpcb);
else
#endif
rt = tcp_rtlookup(tp->t_inpcb);
if (rt == NULL)
return
#if INET6
isipv6 ? tcp_v6mssdflt :
#endif
tcp_mssdflt;
mss = rt->rt_ifp->if_mtu - lgminh;
return mss;
}