tcp_input.c revision 69781
1235267Sgabor/* 2235267Sgabor * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3235267Sgabor * The Regents of the University of California. All rights reserved. 4235267Sgabor * 5235267Sgabor * Redistribution and use in source and binary forms, with or without 6235267Sgabor * modification, are permitted provided that the following conditions 7235267Sgabor * are met: 8235267Sgabor * 1. Redistributions of source code must retain the above copyright 9235267Sgabor * notice, this list of conditions and the following disclaimer. 10235267Sgabor * 2. Redistributions in binary form must reproduce the above copyright 11235267Sgabor * notice, this list of conditions and the following disclaimer in the 12235267Sgabor * documentation and/or other materials provided with the distribution. 13235267Sgabor * 3. All advertising materials mentioning features or use of this software 14235267Sgabor * must display the following acknowledgement: 15235267Sgabor * This product includes software developed by the University of 16235267Sgabor * California, Berkeley and its contributors. 17235267Sgabor * 4. Neither the name of the University nor the names of its contributors 18235267Sgabor * may be used to endorse or promote products derived from this software 19235267Sgabor * without specific prior written permission. 20235267Sgabor * 21235267Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22235267Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23235267Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24235267Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25235267Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26235267Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27235267Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28235267Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29235267Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30235267Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31235267Sgabor * SUCH DAMAGE. 32235267Sgabor * 33235267Sgabor * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 34235267Sgabor * $FreeBSD: head/sys/netinet/tcp_input.c 69781 2000-12-08 21:51:06Z dwmalone $ 35235267Sgabor */ 36235267Sgabor 37235267Sgabor#include "opt_ipfw.h" /* for ipfw_fwd */ 38235267Sgabor#include "opt_inet6.h" 39235267Sgabor#include "opt_ipsec.h" 40235267Sgabor#include "opt_tcpdebug.h" 41235267Sgabor#include "opt_tcp_input.h" 42235267Sgabor 43235267Sgabor#include <sys/param.h> 44235267Sgabor#include <sys/systm.h> 45235267Sgabor#include <sys/kernel.h> 46235267Sgabor#include <sys/sysctl.h> 47235267Sgabor#include <sys/malloc.h> 48235267Sgabor#include <sys/mbuf.h> 49235267Sgabor#include <sys/proc.h> /* for proc0 declaration */ 50235267Sgabor#include <sys/protosw.h> 51235267Sgabor#include <sys/socket.h> 52235267Sgabor#include <sys/socketvar.h> 53235267Sgabor#include <sys/syslog.h> 54235267Sgabor 55235267Sgabor#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 56235987Sgabor 57235987Sgabor#include <net/if.h> 58235267Sgabor#include <net/route.h> 59235435Sgabor 60235267Sgabor#include <netinet/in.h> 61235267Sgabor#include <netinet/in_systm.h> 62235267Sgabor#include <netinet/ip.h> 63235267Sgabor#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 64235267Sgabor#include <netinet/in_var.h> 65235267Sgabor#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 66235267Sgabor#include <netinet/in_pcb.h> 67235267Sgabor#include <netinet/ip_var.h> 68235267Sgabor#ifdef INET6 69235267Sgabor#include <netinet/ip6.h> 70235267Sgabor#include <netinet/icmp6.h> 71235267Sgabor#include <netinet6/nd6.h> 72235267Sgabor#include <netinet6/ip6_var.h> 73235267Sgabor#include <netinet6/in6_pcb.h> 74235267Sgabor#endif 75235267Sgabor#include <netinet/tcp.h> 76235267Sgabor#include <netinet/tcp_fsm.h> 77235267Sgabor#include <netinet/tcp_seq.h> 78235267Sgabor#include <netinet/tcp_timer.h> 79235267Sgabor#include <netinet/tcp_var.h> 80235267Sgabor#ifdef INET6 81235267Sgabor#include <netinet6/tcp6_var.h> 82235267Sgabor#endif 83235267Sgabor#include <netinet/tcpip.h> 84235267Sgabor#ifdef TCPDEBUG 85235267Sgabor#include <netinet/tcp_debug.h> 86235267Sgabor 87235267Sgaboru_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */ 88235267Sgaborstruct tcphdr tcp_savetcp; 89235267Sgabor#endif /* TCPDEBUG */ 90235267Sgabor 91235267Sgabor#ifdef IPSEC 92235267Sgabor#include <netinet6/ipsec.h> 93235267Sgabor#ifdef INET6 94235267Sgabor#include <netinet6/ipsec6.h> 95235267Sgabor#endif 96235267Sgabor#include <netkey/key.h> 97235267Sgabor#endif /*IPSEC*/ 98235267Sgabor 99235267Sgabor#include <machine/in_cksum.h> 100235267Sgabor 101235267SgaborMALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); 102235267Sgabor 103235267Sgaborstatic int tcprexmtthresh = 3; 104235267Sgabortcp_seq tcp_iss; 105235267Sgabortcp_cc tcp_ccgen; 106235267Sgabor 107235267Sgaborstruct tcpstat tcpstat; 108235267SgaborSYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD, 109235267Sgabor &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 110235267Sgabor 111235267Sgaborstatic int log_in_vain = 0; 112235267SgaborSYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 113235267Sgabor &log_in_vain, 0, "Log all incoming TCP connections"); 114235267Sgabor 115235267Sgaborstatic int blackhole = 0; 116235267SgaborSYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 117235267Sgabor &blackhole, 0, "Do not send RST when dropping refused connections"); 118235267Sgabor 119235267Sgaborint tcp_delack_enabled = 1; 120235267SgaborSYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 121235267Sgabor &tcp_delack_enabled, 0, 122235267Sgabor "Delay ACK to try and piggyback it onto a data packet"); 123235267Sgabor 124235267Sgaborint tcp_lq_overflow = 1; 125235267SgaborSYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_lq_overflow, CTLFLAG_RW, 126235267Sgabor &tcp_lq_overflow, 0, 127235267Sgabor "Listen Queue Overflow"); 128235267Sgabor 129235267Sgabor#ifdef TCP_DROP_SYNFIN 130235267Sgaborstatic int drop_synfin = 0; 131235267SgaborSYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 132235267Sgabor &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 133235267Sgabor#endif 134235267Sgabor 135235267Sgabor#ifdef TCP_RESTRICT_RST 136235267Sgaborstatic int restrict_rst = 0; 137235267SgaborSYSCTL_INT(_net_inet_tcp, OID_AUTO, restrict_rst, CTLFLAG_RW, 138235267Sgabor &restrict_rst, 0, "Restrict RST emission"); 139235267Sgabor#endif 140235267Sgabor 141235267Sgaborstruct inpcbhead tcb; 142235267Sgabor#define tcb6 tcb /* for KAME src sync over BSD*'s */ 143235267Sgaborstruct inpcbinfo tcbinfo; 144235267Sgabor 145235267Sgaborstatic void tcp_dooptions __P((struct tcpcb *, 146235267Sgabor u_char *, int, struct tcphdr *, struct tcpopt *)); 147235267Sgaborstatic void tcp_pulloutofband __P((struct socket *, 148235267Sgabor struct tcphdr *, struct mbuf *, int)); 149235267Sgaborstatic int tcp_reass __P((struct tcpcb *, struct tcphdr *, int *, 150235267Sgabor struct mbuf *)); 151235267Sgaborstatic void tcp_xmit_timer __P((struct tcpcb *, int)); 152235267Sgaborstatic int tcp_newreno __P((struct tcpcb *, struct tcphdr *)); 153235267Sgabor 154235267Sgabor/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 155235267Sgabor#ifdef INET6 156235267Sgabor#define ND6_HINT(tp) \ 157235267Sgabordo { \ 158235267Sgabor if ((tp) && (tp)->t_inpcb && \ 159235267Sgabor ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 160235267Sgabor (tp)->t_inpcb->in6p_route.ro_rt) \ 161235267Sgabor nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \ 162235267Sgabor} while (0) 163235267Sgabor#else 164235267Sgabor#define ND6_HINT(tp) 165235267Sgabor#endif 166235267Sgabor 167235267Sgabor/* 168235267Sgabor * Insert segment which inludes th into reassembly queue of tcp with 169235267Sgabor * control block tp. Return TH_FIN if reassembly now includes 170235267Sgabor * a segment with FIN. The macro form does the common case inline 171235267Sgabor * (segment is the next to be received on an established connection, 172235267Sgabor * and the queue is empty), avoiding linkage into and removal 173235267Sgabor * from the queue and repetition of various conversions. 174235267Sgabor * Set DELACK for segments received in order, but ack immediately 175235267Sgabor * when segments are out of order (so fast retransmit can work). 176235267Sgabor */ 177235267Sgabor#define TCP_REASS(tp, th, tlenp, m, so, flags) { \ 178235267Sgabor if ((th)->th_seq == (tp)->rcv_nxt && \ 179235267Sgabor LIST_EMPTY(&(tp)->t_segq) && \ 180235267Sgabor (tp)->t_state == TCPS_ESTABLISHED) { \ 181235267Sgabor if (tcp_delack_enabled) \ 182235267Sgabor callout_reset(tp->tt_delack, tcp_delacktime, \ 183235267Sgabor tcp_timer_delack, tp); \ 184235267Sgabor else \ 185235267Sgabor tp->t_flags |= TF_ACKNOW; \ 186235267Sgabor (tp)->rcv_nxt += *(tlenp); \ 187235267Sgabor flags = (th)->th_flags & TH_FIN; \ 188235267Sgabor tcpstat.tcps_rcvpack++;\ 189235267Sgabor tcpstat.tcps_rcvbyte += *(tlenp);\ 190235267Sgabor ND6_HINT(tp); \ 191235267Sgabor sbappend(&(so)->so_rcv, (m)); \ 192235267Sgabor sorwakeup(so); \ 193235267Sgabor } else { \ 194235267Sgabor (flags) = tcp_reass((tp), (th), (tlenp), (m)); \ 195235267Sgabor tp->t_flags |= TF_ACKNOW; \ 196235267Sgabor } \ 197235267Sgabor} 198235267Sgabor 199235267Sgaborstatic int 200235267Sgabortcp_reass(tp, th, tlenp, m) 201235267Sgabor register struct tcpcb *tp; 202235267Sgabor register struct tcphdr *th; 203235267Sgabor int *tlenp; 204235267Sgabor struct mbuf *m; 205235267Sgabor{ 206235267Sgabor struct tseg_qent *q; 207235267Sgabor struct tseg_qent *p = NULL; 208235267Sgabor struct tseg_qent *nq; 209235267Sgabor struct tseg_qent *te; 210235267Sgabor struct socket *so = tp->t_inpcb->inp_socket; 211235267Sgabor int flags; 212235267Sgabor 213235267Sgabor /* 214235267Sgabor * Call with th==0 after become established to 215235267Sgabor * force pre-ESTABLISHED data up to user socket. 216235267Sgabor */ 217235267Sgabor if (th == 0) 218235267Sgabor goto present; 219235267Sgabor 220235267Sgabor /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 221235267Sgabor MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 222235267Sgabor M_NOWAIT); 223235267Sgabor if (te == NULL) { 224235267Sgabor tcpstat.tcps_rcvmemdrop++; 225235267Sgabor m_freem(m); 226235267Sgabor return (0); 227235267Sgabor } 228235267Sgabor 229235267Sgabor /* 230235267Sgabor * Find a segment which begins after this one does. 231235267Sgabor */ 232235267Sgabor LIST_FOREACH(q, &tp->t_segq, tqe_q) { 233235267Sgabor if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 234235267Sgabor break; 235235267Sgabor p = q; 236235267Sgabor } 237235267Sgabor 238235267Sgabor /* 239235267Sgabor * If there is a preceding segment, it may provide some of 240235267Sgabor * our data already. If so, drop the data from the incoming 241235267Sgabor * segment. If it provides all of our data, drop us. 242235267Sgabor */ 243235267Sgabor if (p != NULL) { 244235267Sgabor register int i; 245235267Sgabor /* conversion to int (in i) handles seq wraparound */ 246235267Sgabor i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 247235267Sgabor if (i > 0) { 248235267Sgabor if (i >= *tlenp) { 249235267Sgabor tcpstat.tcps_rcvduppack++; 250235267Sgabor tcpstat.tcps_rcvdupbyte += *tlenp; 251235267Sgabor m_freem(m); 252235267Sgabor FREE(te, M_TSEGQ); 253235267Sgabor /* 254235267Sgabor * Try to present any queued data 255235267Sgabor * at the left window edge to the user. 256235267Sgabor * This is needed after the 3-WHS 257235267Sgabor * completes. 258235267Sgabor */ 259235267Sgabor goto present; /* ??? */ 260235267Sgabor } 261235267Sgabor m_adj(m, i); 262235267Sgabor *tlenp -= i; 263235267Sgabor th->th_seq += i; 264235267Sgabor } 265235267Sgabor } 266235267Sgabor tcpstat.tcps_rcvoopack++; 267235267Sgabor tcpstat.tcps_rcvoobyte += *tlenp; 268235267Sgabor 269235267Sgabor /* 270235267Sgabor * While we overlap succeeding segments trim them or, 271235267Sgabor * if they are completely covered, dequeue them. 272235267Sgabor */ 273235267Sgabor while (q) { 274235267Sgabor register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 275235267Sgabor if (i <= 0) 276235267Sgabor break; 277235267Sgabor if (i < q->tqe_len) { 278235267Sgabor q->tqe_th->th_seq += i; 279235267Sgabor q->tqe_len -= i; 280235267Sgabor m_adj(q->tqe_m, i); 281235267Sgabor break; 282235267Sgabor } 283235267Sgabor 284235267Sgabor nq = LIST_NEXT(q, tqe_q); 285235267Sgabor LIST_REMOVE(q, tqe_q); 286235267Sgabor m_freem(q->tqe_m); 287235267Sgabor FREE(q, M_TSEGQ); 288235267Sgabor q = nq; 289235267Sgabor } 290235267Sgabor 291235267Sgabor /* Insert the new segment queue entry into place. */ 292235267Sgabor te->tqe_m = m; 293235267Sgabor te->tqe_th = th; 294235267Sgabor te->tqe_len = *tlenp; 295235267Sgabor 296235267Sgabor if (p == NULL) { 297235267Sgabor LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 298235267Sgabor } else { 299235267Sgabor LIST_INSERT_AFTER(p, te, tqe_q); 300235267Sgabor } 301235267Sgabor 302235267Sgaborpresent: 303235267Sgabor /* 304235267Sgabor * Present data to user, advancing rcv_nxt through 305235267Sgabor * completed sequence space. 306235267Sgabor */ 307235267Sgabor if (!TCPS_HAVEESTABLISHED(tp->t_state)) 308235267Sgabor return (0); 309235267Sgabor q = LIST_FIRST(&tp->t_segq); 310235267Sgabor if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 311235267Sgabor return (0); 312235267Sgabor do { 313235267Sgabor tp->rcv_nxt += q->tqe_len; 314235267Sgabor flags = q->tqe_th->th_flags & TH_FIN; 315235267Sgabor nq = LIST_NEXT(q, tqe_q); 316235267Sgabor LIST_REMOVE(q, tqe_q); 317235267Sgabor if (so->so_state & SS_CANTRCVMORE) 318235267Sgabor m_freem(q->tqe_m); 319235267Sgabor else 320235267Sgabor sbappend(&so->so_rcv, q->tqe_m); 321235267Sgabor FREE(q, M_TSEGQ); 322235267Sgabor q = nq; 323235267Sgabor } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 324235267Sgabor ND6_HINT(tp); 325235267Sgabor sorwakeup(so); 326235267Sgabor return (flags); 327235267Sgabor} 328235267Sgabor 329235267Sgabor/* 330235267Sgabor * TCP input routine, follows pages 65-76 of the 331235267Sgabor * protocol specification dated September, 1981 very closely. 332235267Sgabor */ 333235267Sgabor#ifdef INET6 334235267Sgaborint 335235267Sgabortcp6_input(mp, offp, proto) 336235267Sgabor struct mbuf **mp; 337235267Sgabor int *offp, proto; 338235267Sgabor{ 339235267Sgabor register struct mbuf *m = *mp; 340235267Sgabor 341235267Sgabor IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 342235267Sgabor 343235267Sgabor /* 344235267Sgabor * draft-itojun-ipv6-tcp-to-anycast 345235267Sgabor * better place to put this in? 346235267Sgabor */ 347235267Sgabor if (m->m_flags & M_ANYCAST6) { 348235267Sgabor struct ip6_hdr *ip6; 349235267Sgabor 350235267Sgabor ip6 = mtod(m, struct ip6_hdr *); 351235267Sgabor icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 352235267Sgabor (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 353235267Sgabor return IPPROTO_DONE; 354235267Sgabor } 355235267Sgabor 356235267Sgabor tcp_input(m, *offp, proto); 357235267Sgabor return IPPROTO_DONE; 358235267Sgabor} 359235267Sgabor#endif 360235267Sgabor 361235267Sgaborvoid 362235267Sgabortcp_input(m, off0, proto) 363235267Sgabor register struct mbuf *m; 364235267Sgabor int off0, proto; 365235267Sgabor{ 366235267Sgabor register struct tcphdr *th; 367235267Sgabor register struct ip *ip = NULL; 368235267Sgabor register struct ipovly *ipov; 369235267Sgabor register struct inpcb *inp; 370235267Sgabor u_char *optp = NULL; 371235267Sgabor int optlen = 0; 372235267Sgabor int len, tlen, off; 373235267Sgabor int drop_hdrlen; 374235267Sgabor register struct tcpcb *tp = 0; 375235267Sgabor register int thflags; 376235267Sgabor struct socket *so = 0; 377235267Sgabor int todrop, acked, ourfinisacked, needoutput = 0; 378235267Sgabor struct in_addr laddr; 379235267Sgabor#ifdef INET6 380235267Sgabor struct in6_addr laddr6; 381235267Sgabor#endif 382235267Sgabor int dropsocket = 0; 383235267Sgabor int iss = 0; 384235267Sgabor u_long tiwin; 385235267Sgabor struct tcpopt to; /* options in this segment */ 386235267Sgabor struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 387235267Sgabor struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 388235267Sgabor#ifdef TCPDEBUG 389235267Sgabor short ostate = 0; 390235267Sgabor#endif 391235267Sgabor#ifdef INET6 392235267Sgabor struct ip6_hdr *ip6 = NULL; 393235267Sgabor int isipv6; 394235267Sgabor#endif /* INET6 */ 395235267Sgabor 396235267Sgabor#ifdef INET6 397235267Sgabor isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 398235267Sgabor#endif 399235267Sgabor bzero((char *)&to, sizeof(to)); 400235267Sgabor 401235267Sgabor tcpstat.tcps_rcvtotal++; 402235267Sgabor 403235267Sgabor#ifdef INET6 404235267Sgabor if (isipv6) { 405235267Sgabor /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 406235267Sgabor ip6 = mtod(m, struct ip6_hdr *); 407235267Sgabor tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 408235267Sgabor if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 409235267Sgabor tcpstat.tcps_rcvbadsum++; 410235987Sgabor goto drop; 411235987Sgabor } 412235987Sgabor th = (struct tcphdr *)((caddr_t)ip6 + off0); 413235987Sgabor } else 414235987Sgabor#endif /* INET6 */ 415235987Sgabor { 416235267Sgabor /* 417235987Sgabor * Get IP and TCP header together in first mbuf. 418235987Sgabor * Note: IP leaves IP header in first mbuf. 419235987Sgabor */ 420235987Sgabor if (off0 > sizeof (struct ip)) { 421235987Sgabor ip_stripoptions(m, (struct mbuf *)0); 422235267Sgabor off0 = sizeof(struct ip); 423235987Sgabor } 424235267Sgabor if (m->m_len < sizeof (struct tcpiphdr)) { 425235267Sgabor if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 426235267Sgabor tcpstat.tcps_rcvshort++; 427235267Sgabor return; 428235267Sgabor } 429235267Sgabor } 430235267Sgabor ip = mtod(m, struct ip *); 431235267Sgabor ipov = (struct ipovly *)ip; 432235267Sgabor th = (struct tcphdr *)((caddr_t)ip + off0); 433235267Sgabor tlen = ip->ip_len; 434235267Sgabor 435235267Sgabor if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 436235267Sgabor if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 437235267Sgabor th->th_sum = m->m_pkthdr.csum_data; 438235267Sgabor else 439235267Sgabor th->th_sum = in_pseudo(ip->ip_src.s_addr, 440235267Sgabor ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data + 441235267Sgabor ip->ip_len + IPPROTO_TCP)); 442235267Sgabor th->th_sum ^= 0xffff; 443235267Sgabor } else { 444235267Sgabor /* 445235267Sgabor * Checksum extended TCP header and data. 446235267Sgabor */ 447235267Sgabor len = sizeof (struct ip) + tlen; 448235267Sgabor bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 449235267Sgabor ipov->ih_len = (u_short)tlen; 450235267Sgabor HTONS(ipov->ih_len); 451235267Sgabor th->th_sum = in_cksum(m, len); 452235267Sgabor } 453235267Sgabor if (th->th_sum) { 454235267Sgabor tcpstat.tcps_rcvbadsum++; 455235267Sgabor goto drop; 456235267Sgabor } 457235267Sgabor#ifdef INET6 458235267Sgabor /* Re-initialization for later version check */ 459235267Sgabor ip->ip_v = IPVERSION; 460235267Sgabor#endif 461235267Sgabor } 462235267Sgabor 463235267Sgabor /* 464235267Sgabor * Check that TCP offset makes sense, 465235267Sgabor * pull out TCP options and adjust length. XXX 466235267Sgabor */ 467235267Sgabor off = th->th_off << 2; 468235267Sgabor if (off < sizeof (struct tcphdr) || off > tlen) { 469235267Sgabor tcpstat.tcps_rcvbadoff++; 470235267Sgabor goto drop; 471235267Sgabor } 472235267Sgabor tlen -= off; /* tlen is used instead of ti->ti_len */ 473235267Sgabor if (off > sizeof (struct tcphdr)) { 474235267Sgabor#ifdef INET6 475235267Sgabor if (isipv6) { 476235267Sgabor IP6_EXTHDR_CHECK(m, off0, off, ); 477235267Sgabor ip6 = mtod(m, struct ip6_hdr *); 478235267Sgabor th = (struct tcphdr *)((caddr_t)ip6 + off0); 479235267Sgabor } else 480235267Sgabor#endif /* INET6 */ 481235267Sgabor { 482235267Sgabor if (m->m_len < sizeof(struct ip) + off) { 483235267Sgabor if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 484235267Sgabor tcpstat.tcps_rcvshort++; 485235267Sgabor return; 486235267Sgabor } 487235267Sgabor ip = mtod(m, struct ip *); 488235267Sgabor ipov = (struct ipovly *)ip; 489235267Sgabor th = (struct tcphdr *)((caddr_t)ip + off0); 490235267Sgabor } 491235267Sgabor } 492235267Sgabor optlen = off - sizeof (struct tcphdr); 493235267Sgabor optp = (u_char *)(th + 1); 494235267Sgabor } 495235267Sgabor thflags = th->th_flags; 496235267Sgabor 497235267Sgabor#ifdef TCP_DROP_SYNFIN 498235267Sgabor /* 499235267Sgabor * If the drop_synfin option is enabled, drop all packets with 500235267Sgabor * both the SYN and FIN bits set. This prevents e.g. nmap from 501235267Sgabor * identifying the TCP/IP stack. 502235267Sgabor * 503235267Sgabor * This is incompatible with RFC1644 extensions (T/TCP). 504235267Sgabor */ 505235267Sgabor if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 506235267Sgabor goto drop; 507235267Sgabor#endif 508235267Sgabor 509235267Sgabor /* 510235267Sgabor * Convert TCP protocol specific fields to host format. 511235267Sgabor */ 512235267Sgabor NTOHL(th->th_seq); 513235267Sgabor NTOHL(th->th_ack); 514235267Sgabor NTOHS(th->th_win); 515235267Sgabor NTOHS(th->th_urp); 516235267Sgabor 517235267Sgabor /* 518235267Sgabor * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options, 519235267Sgabor * until after ip6_savecontrol() is called and before other functions 520235267Sgabor * which don't want those proto headers. 521235267Sgabor * Because ip6_savecontrol() is going to parse the mbuf to 522235267Sgabor * search for data to be passed up to user-land, it wants mbuf 523235267Sgabor * parameters to be unchanged. 524235267Sgabor */ 525235267Sgabor drop_hdrlen = off0 + off; 526235267Sgabor 527235267Sgabor /* 528235267Sgabor * Locate pcb for segment. 529235267Sgabor */ 530235267Sgaborfindpcb: 531235267Sgabor#ifdef IPFIREWALL_FORWARD 532235267Sgabor if (ip_fw_fwd_addr != NULL 533235267Sgabor#ifdef INET6 534235267Sgabor && isipv6 == NULL /* IPv6 support is not yet */ 535235267Sgabor#endif /* INET6 */ 536235267Sgabor ) { 537235267Sgabor /* 538235267Sgabor * Diverted. Pretend to be the destination. 539235267Sgabor * already got one like this? 540235267Sgabor */ 541235267Sgabor inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 542235267Sgabor ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif); 543235267Sgabor if (!inp) { 544235267Sgabor /* 545235267Sgabor * No, then it's new. Try find the ambushing socket 546235267Sgabor */ 547235267Sgabor if (!ip_fw_fwd_addr->sin_port) { 548235267Sgabor inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, 549235267Sgabor th->th_sport, ip_fw_fwd_addr->sin_addr, 550235267Sgabor th->th_dport, 1, m->m_pkthdr.rcvif); 551235267Sgabor } else { 552235267Sgabor inp = in_pcblookup_hash(&tcbinfo, 553235267Sgabor ip->ip_src, th->th_sport, 554235267Sgabor ip_fw_fwd_addr->sin_addr, 555235267Sgabor ntohs(ip_fw_fwd_addr->sin_port), 1, 556235267Sgabor m->m_pkthdr.rcvif); 557235267Sgabor } 558235267Sgabor } 559235267Sgabor ip_fw_fwd_addr = NULL; 560235267Sgabor } else 561235267Sgabor#endif /* IPFIREWALL_FORWARD */ 562235267Sgabor { 563235267Sgabor#ifdef INET6 564235267Sgabor if (isipv6) 565235267Sgabor inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport, 566235267Sgabor &ip6->ip6_dst, th->th_dport, 1, 567235267Sgabor m->m_pkthdr.rcvif); 568235267Sgabor else 569235267Sgabor#endif /* INET6 */ 570235267Sgabor inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 571235267Sgabor ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif); 572235267Sgabor } 573235267Sgabor 574235267Sgabor#ifdef IPSEC 575235267Sgabor#ifdef INET6 576235267Sgabor if (isipv6) { 577235267Sgabor if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 578235267Sgabor ipsec6stat.in_polvio++; 579235267Sgabor goto drop; 580235267Sgabor } 581235267Sgabor } else 582235267Sgabor#endif /* INET6 */ 583235267Sgabor if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 584235267Sgabor ipsecstat.in_polvio++; 585235267Sgabor goto drop; 586235267Sgabor } 587235267Sgabor#endif /*IPSEC*/ 588235267Sgabor 589235267Sgabor /* 590235267Sgabor * If the state is CLOSED (i.e., TCB does not exist) then 591235267Sgabor * all data in the incoming segment is discarded. 592235267Sgabor * If the TCB exists but is in CLOSED state, it is embryonic, 593235267Sgabor * but should either do a listen or a connect soon. 594235267Sgabor */ 595235267Sgabor if (inp == NULL) { 596235267Sgabor if (log_in_vain) { 597235267Sgabor#ifdef INET6 598235267Sgabor char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN]; 599235267Sgabor#else /* INET6 */ 600235267Sgabor char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 601235432Sgabor#endif /* INET6 */ 602235267Sgabor 603235267Sgabor#ifdef INET6 604235267Sgabor if (isipv6) { 605235267Sgabor strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst)); 606235267Sgabor strcpy(sbuf, ip6_sprintf(&ip6->ip6_src)); 607235267Sgabor } else 608235267Sgabor#endif 609235267Sgabor { 610235267Sgabor strcpy(dbuf, inet_ntoa(ip->ip_dst)); 611235267Sgabor strcpy(sbuf, inet_ntoa(ip->ip_src)); 612235267Sgabor } 613235267Sgabor switch (log_in_vain) { 614235267Sgabor case 1: 615235267Sgabor if(thflags & TH_SYN) 616235267Sgabor log(LOG_INFO, 617235267Sgabor "Connection attempt to TCP %s:%d from %s:%d\n", 618235267Sgabor dbuf, ntohs(th->th_dport), 619235267Sgabor sbuf, 620235267Sgabor ntohs(th->th_sport)); 621235267Sgabor break; 622235267Sgabor case 2: 623235267Sgabor log(LOG_INFO, 624235267Sgabor "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n", 625235267Sgabor dbuf, ntohs(th->th_dport), sbuf, 626235267Sgabor ntohs(th->th_sport), thflags); 627235267Sgabor break; 628235267Sgabor default: 629235267Sgabor break; 630235267Sgabor } 631235267Sgabor } 632235267Sgabor if (blackhole) { 633235267Sgabor switch (blackhole) { 634235267Sgabor case 1: 635235267Sgabor if (thflags & TH_SYN) 636235267Sgabor goto drop; 637235267Sgabor break; 638235267Sgabor case 2: 639235267Sgabor goto drop; 640235267Sgabor default: 641235267Sgabor goto drop; 642235267Sgabor } 643235267Sgabor } 644235267Sgabor goto maybedropwithreset; 645235267Sgabor } 646235267Sgabor tp = intotcpcb(inp); 647235267Sgabor if (tp == 0) 648235267Sgabor goto maybedropwithreset; 649235267Sgabor if (tp->t_state == TCPS_CLOSED) 650235267Sgabor goto drop; 651235267Sgabor 652235267Sgabor /* Unscale the window into a 32-bit value. */ 653235267Sgabor if ((thflags & TH_SYN) == 0) 654235267Sgabor tiwin = th->th_win << tp->snd_scale; 655235267Sgabor else 656235267Sgabor tiwin = th->th_win; 657235267Sgabor 658235267Sgabor#ifdef INET6 659235267Sgabor /* save packet options if user wanted */ 660235987Sgabor if (isipv6 && inp->in6p_flags & INP_CONTROLOPTS) { 661235267Sgabor if (inp->in6p_options) { 662235267Sgabor m_freem(inp->in6p_options); 663235267Sgabor inp->in6p_options = 0; 664235267Sgabor } 665235267Sgabor ip6_savecontrol(inp, &inp->in6p_options, ip6, m); 666235267Sgabor } 667235267Sgabor /* else, should also do ip_srcroute() here? */ 668235267Sgabor#endif /* INET6 */ 669235267Sgabor 670235267Sgabor so = inp->inp_socket; 671235267Sgabor if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 672235267Sgabor#ifdef TCPDEBUG 673235267Sgabor if (so->so_options & SO_DEBUG) { 674235267Sgabor ostate = tp->t_state; 675235267Sgabor#ifdef INET6 676235267Sgabor if (isipv6) 677235267Sgabor bcopy((char *)ip6, (char *)tcp_saveipgen, 678235267Sgabor sizeof(*ip6)); 679235267Sgabor else 680235267Sgabor#endif /* INET6 */ 681235267Sgabor bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 682235267Sgabor tcp_savetcp = *th; 683235267Sgabor } 684235267Sgabor#endif 685235267Sgabor if (so->so_options & SO_ACCEPTCONN) { 686235267Sgabor register struct tcpcb *tp0 = tp; 687235267Sgabor struct socket *so2; 688235267Sgabor#ifdef IPSEC 689235267Sgabor struct socket *oso; 690235267Sgabor#endif 691235267Sgabor#ifdef INET6 692235267Sgabor struct inpcb *oinp = sotoinpcb(so); 693235267Sgabor#endif /* INET6 */ 694235267Sgabor 695235267Sgabor#ifndef IPSEC 696235267Sgabor /* 697235267Sgabor * Current IPsec implementation makes incorrect IPsec 698235267Sgabor * cache if this check is done here. 699235267Sgabor * So delay this until duplicated socket is created. 700235267Sgabor */ 701235267Sgabor if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 702235267Sgabor /* 703235267Sgabor * Note: dropwithreset makes sure we don't 704235267Sgabor * send a RST in response to a RST. 705235267Sgabor */ 706235267Sgabor if (thflags & TH_ACK) { 707235267Sgabor tcpstat.tcps_badsyn++; 708235267Sgabor goto maybedropwithreset; 709235267Sgabor } 710235267Sgabor goto drop; 711235267Sgabor } 712235267Sgabor#endif 713235267Sgabor so2 = sonewconn(so, 0); 714235267Sgabor if (so2 == 0) { 715235267Sgabor tcpstat.tcps_listendrop++; 716235267Sgabor so2 = sodropablereq(so); 717235267Sgabor if (so2) { 718235267Sgabor if (tcp_lq_overflow) 719235267Sgabor sototcpcb(so2)->t_flags |= 720235267Sgabor TF_LQ_OVERFLOW; 721235267Sgabor tcp_drop(sototcpcb(so2), ETIMEDOUT); 722235267Sgabor so2 = sonewconn(so, 0); 723235267Sgabor } 724235267Sgabor if (!so2) 725235267Sgabor goto drop; 726235267Sgabor } 727235267Sgabor#ifdef IPSEC 728235267Sgabor oso = so; 729235267Sgabor#endif 730235267Sgabor so = so2; 731235267Sgabor /* 732235267Sgabor * This is ugly, but .... 733235267Sgabor * 734235267Sgabor * Mark socket as temporary until we're 735235267Sgabor * committed to keeping it. The code at 736235267Sgabor * ``drop'' and ``dropwithreset'' check the 737235267Sgabor * flag dropsocket to see if the temporary 738235267Sgabor * socket created here should be discarded. 739235267Sgabor * We mark the socket as discardable until 740235267Sgabor * we're committed to it below in TCPS_LISTEN. 741235267Sgabor */ 742235267Sgabor dropsocket++; 743235267Sgabor inp = (struct inpcb *)so->so_pcb; 744235267Sgabor#ifdef INET6 745235267Sgabor if (isipv6) 746235267Sgabor inp->in6p_laddr = ip6->ip6_dst; 747235267Sgabor else { 748235267Sgabor if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) { 749235267Sgabor inp->inp_vflag &= ~INP_IPV6; 750235267Sgabor inp->inp_vflag |= INP_IPV4; 751235267Sgabor } 752235267Sgabor#endif /* INET6 */ 753235267Sgabor inp->inp_laddr = ip->ip_dst; 754235267Sgabor#ifdef INET6 755235267Sgabor } 756235267Sgabor#endif /* INET6 */ 757235267Sgabor inp->inp_lport = th->th_dport; 758235267Sgabor if (in_pcbinshash(inp) != 0) { 759235267Sgabor /* 760235267Sgabor * Undo the assignments above if we failed to 761235267Sgabor * put the PCB on the hash lists. 762235267Sgabor */ 763235267Sgabor#ifdef INET6 764235267Sgabor if (isipv6) 765235267Sgabor inp->in6p_laddr = in6addr_any; 766235267Sgabor else 767235267Sgabor#endif /* INET6 */ 768235267Sgabor inp->inp_laddr.s_addr = INADDR_ANY; 769235267Sgabor inp->inp_lport = 0; 770235267Sgabor goto drop; 771235267Sgabor } 772235267Sgabor#ifdef IPSEC 773235267Sgabor /* 774235267Sgabor * To avoid creating incorrectly cached IPsec 775235267Sgabor * association, this is need to be done here. 776235267Sgabor * 777235267Sgabor * Subject: (KAME-snap 748) 778235267Sgabor * From: Wayne Knowles <w.knowles@niwa.cri.nz> 779235267Sgabor * ftp://ftp.kame.net/pub/mail-list/snap-users/748 780235267Sgabor */ 781235267Sgabor if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 782235267Sgabor /* 783235267Sgabor * Note: dropwithreset makes sure we don't 784235267Sgabor * send a RST in response to a RST. 785235267Sgabor */ 786235267Sgabor if (thflags & TH_ACK) { 787235267Sgabor tcpstat.tcps_badsyn++; 788235267Sgabor goto maybedropwithreset; 789235267Sgabor } 790235267Sgabor goto drop; 791235267Sgabor } 792235267Sgabor#endif 793235267Sgabor#ifdef INET6 794235267Sgabor if (isipv6) { 795235267Sgabor /* 796235267Sgabor * inherit socket options from the listening 797235267Sgabor * socket. 798235267Sgabor */ 799235267Sgabor inp->inp_flags |= 800235267Sgabor oinp->inp_flags & INP_CONTROLOPTS; 801235267Sgabor if (inp->inp_flags & INP_CONTROLOPTS) { 802235267Sgabor if (inp->in6p_options) { 803235267Sgabor m_freem(inp->in6p_options); 804235267Sgabor inp->in6p_options = 0; 805235267Sgabor } 806235267Sgabor ip6_savecontrol(inp, 807235267Sgabor &inp->in6p_options, 808235267Sgabor ip6, m); 809235267Sgabor } 810235267Sgabor } else 811235267Sgabor#endif /* INET6 */ 812235267Sgabor inp->inp_options = ip_srcroute(); 813235267Sgabor#ifdef IPSEC 814235267Sgabor /* copy old policy into new socket's */ 815235267Sgabor if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp, 816235267Sgabor inp->inp_sp)) 817235267Sgabor printf("tcp_input: could not copy policy\n"); 818235267Sgabor#endif 819235267Sgabor tp = intotcpcb(inp); 820235267Sgabor tp->t_state = TCPS_LISTEN; 821235267Sgabor tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 822235267Sgabor 823235267Sgabor /* Compute proper scaling value from buffer space */ 824235267Sgabor while (tp->request_r_scale < TCP_MAX_WINSHIFT && 825235267Sgabor TCP_MAXWIN << tp->request_r_scale < 826235267Sgabor so->so_rcv.sb_hiwat) 827235267Sgabor tp->request_r_scale++; 828235267Sgabor } 829235267Sgabor } 830235267Sgabor 831235267Sgabor /* 832235267Sgabor * Segment received on connection. 833235267Sgabor * Reset idle time and keep-alive timer. 834235267Sgabor */ 835235267Sgabor tp->t_rcvtime = ticks; 836235267Sgabor if (TCPS_HAVEESTABLISHED(tp->t_state)) 837235267Sgabor callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 838235267Sgabor 839235267Sgabor /* 840235267Sgabor * Process options if not in LISTEN state, 841235267Sgabor * else do it below (after getting remote address). 842235267Sgabor */ 843235267Sgabor if (tp->t_state != TCPS_LISTEN) 844235267Sgabor tcp_dooptions(tp, optp, optlen, th, &to); 845235267Sgabor 846235267Sgabor /* 847235267Sgabor * Header prediction: check for the two common cases 848235267Sgabor * of a uni-directional data xfer. If the packet has 849235267Sgabor * no control flags, is in-sequence, the window didn't 850235267Sgabor * change and we're not retransmitting, it's a 851235267Sgabor * candidate. If the length is zero and the ack moved 852235267Sgabor * forward, we're the sender side of the xfer. Just 853235267Sgabor * free the data acked & wake any higher level process 854235267Sgabor * that was blocked waiting for space. If the length 855235267Sgabor * is non-zero and the ack didn't move, we're the 856235267Sgabor * receiver side. If we're getting packets in-order 857235267Sgabor * (the reassembly queue is empty), add the data to 858235267Sgabor * the socket buffer and note that we need a delayed ack. 859235267Sgabor * Make sure that the hidden state-flags are also off. 860235267Sgabor * Since we check for TCPS_ESTABLISHED above, it can only 861235267Sgabor * be TH_NEEDSYN. 862235267Sgabor */ 863235267Sgabor if (tp->t_state == TCPS_ESTABLISHED && 864235267Sgabor (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 865235267Sgabor ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 866235267Sgabor ((to.to_flag & TOF_TS) == 0 || 867235267Sgabor TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 868235267Sgabor /* 869235267Sgabor * Using the CC option is compulsory if once started: 870235267Sgabor * the segment is OK if no T/TCP was negotiated or 871235267Sgabor * if the segment has a CC option equal to CCrecv 872235267Sgabor */ 873235267Sgabor ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 874235267Sgabor ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 875235267Sgabor th->th_seq == tp->rcv_nxt && 876235267Sgabor tiwin && tiwin == tp->snd_wnd && 877235267Sgabor tp->snd_nxt == tp->snd_max) { 878235267Sgabor 879235267Sgabor /* 880235267Sgabor * If last ACK falls within this segment's sequence numbers, 881235267Sgabor * record the timestamp. 882235267Sgabor * NOTE that the test is modified according to the latest 883235267Sgabor * proposal of the tcplw@cray.com list (Braden 1993/04/26). 884235267Sgabor */ 885235267Sgabor if ((to.to_flag & TOF_TS) != 0 && 886235267Sgabor SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 887235267Sgabor tp->ts_recent_age = ticks; 888235267Sgabor tp->ts_recent = to.to_tsval; 889235267Sgabor } 890235267Sgabor 891235267Sgabor if (tlen == 0) { 892235267Sgabor if (SEQ_GT(th->th_ack, tp->snd_una) && 893235267Sgabor SEQ_LEQ(th->th_ack, tp->snd_max) && 894235267Sgabor tp->snd_cwnd >= tp->snd_wnd && 895235267Sgabor tp->t_dupacks < tcprexmtthresh) { 896235267Sgabor /* 897235267Sgabor * this is a pure ack for outstanding data. 898235267Sgabor */ 899235267Sgabor ++tcpstat.tcps_predack; 900235267Sgabor /* 901235267Sgabor * "bad retransmit" recovery 902235267Sgabor */ 903235267Sgabor if (tp->t_rxtshift == 1 && 904235267Sgabor ticks < tp->t_badrxtwin) { 905235267Sgabor tp->snd_cwnd = tp->snd_cwnd_prev; 906235267Sgabor tp->snd_ssthresh = 907235267Sgabor tp->snd_ssthresh_prev; 908235267Sgabor tp->snd_nxt = tp->snd_max; 909235267Sgabor tp->t_badrxtwin = 0; 910235267Sgabor } 911235267Sgabor if ((to.to_flag & TOF_TS) != 0) 912235267Sgabor tcp_xmit_timer(tp, 913235267Sgabor ticks - to.to_tsecr + 1); 914235267Sgabor else if (tp->t_rtttime && 915235267Sgabor SEQ_GT(th->th_ack, tp->t_rtseq)) 916235267Sgabor tcp_xmit_timer(tp, ticks - tp->t_rtttime); 917235267Sgabor acked = th->th_ack - tp->snd_una; 918235267Sgabor tcpstat.tcps_rcvackpack++; 919235267Sgabor tcpstat.tcps_rcvackbyte += acked; 920235267Sgabor sbdrop(&so->so_snd, acked); 921235267Sgabor tp->snd_una = th->th_ack; 922235267Sgabor m_freem(m); 923235267Sgabor ND6_HINT(tp); /* some progress has been done */ 924235267Sgabor 925235267Sgabor /* 926235267Sgabor * If all outstanding data are acked, stop 927235267Sgabor * retransmit timer, otherwise restart timer 928235267Sgabor * using current (possibly backed-off) value. 929235267Sgabor * If process is waiting for space, 930235267Sgabor * wakeup/selwakeup/signal. If data 931235267Sgabor * are ready to send, let tcp_output 932235267Sgabor * decide between more output or persist. 933235267Sgabor */ 934235267Sgabor if (tp->snd_una == tp->snd_max) 935235267Sgabor callout_stop(tp->tt_rexmt); 936235267Sgabor else if (!callout_active(tp->tt_persist)) 937235267Sgabor callout_reset(tp->tt_rexmt, 938235267Sgabor tp->t_rxtcur, 939235267Sgabor tcp_timer_rexmt, tp); 940235267Sgabor 941235267Sgabor sowwakeup(so); 942235432Sgabor if (so->so_snd.sb_cc) 943235267Sgabor (void) tcp_output(tp); 944235267Sgabor return; 945235267Sgabor } 946235267Sgabor } else if (th->th_ack == tp->snd_una && 947235267Sgabor LIST_EMPTY(&tp->t_segq) && 948235267Sgabor tlen <= sbspace(&so->so_rcv)) { 949235267Sgabor /* 950235267Sgabor * this is a pure, in-sequence data packet 951235267Sgabor * with nothing on the reassembly queue and 952235267Sgabor * we have enough buffer space to take it. 953235267Sgabor */ 954235267Sgabor ++tcpstat.tcps_preddat; 955235267Sgabor tp->rcv_nxt += tlen; 956235267Sgabor tcpstat.tcps_rcvpack++; 957235267Sgabor tcpstat.tcps_rcvbyte += tlen; 958235267Sgabor ND6_HINT(tp); /* some progress has been done */ 959235267Sgabor /* 960235267Sgabor * Add data to socket buffer. 961235267Sgabor */ 962235267Sgabor m_adj(m, drop_hdrlen); /* delayed header drop */ 963235267Sgabor sbappend(&so->so_rcv, m); 964235267Sgabor sorwakeup(so); 965235267Sgabor if (tcp_delack_enabled) { 966235267Sgabor callout_reset(tp->tt_delack, tcp_delacktime, 967235267Sgabor tcp_timer_delack, tp); 968235267Sgabor } else { 969235267Sgabor tp->t_flags |= TF_ACKNOW; 970235267Sgabor tcp_output(tp); 971235267Sgabor } 972235267Sgabor return; 973235267Sgabor } 974235267Sgabor } 975235267Sgabor 976235267Sgabor /* 977235267Sgabor * Calculate amount of space in receive window, 978235267Sgabor * and then do TCP input processing. 979235267Sgabor * Receive window is amount of space in rcv queue, 980235267Sgabor * but not less than advertised window. 981235267Sgabor */ 982235267Sgabor { int win; 983235267Sgabor 984235267Sgabor win = sbspace(&so->so_rcv); 985235267Sgabor if (win < 0) 986235267Sgabor win = 0; 987235267Sgabor tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 988235267Sgabor } 989235267Sgabor 990235267Sgabor switch (tp->t_state) { 991235267Sgabor 992235267Sgabor /* 993235267Sgabor * If the state is LISTEN then ignore segment if it contains an RST. 994235267Sgabor * If the segment contains an ACK then it is bad and send a RST. 995235267Sgabor * If it does not contain a SYN then it is not interesting; drop it. 996235267Sgabor * If it is from this socket, drop it, it must be forged. 997235267Sgabor * Don't bother responding if the destination was a broadcast. 998235267Sgabor * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 999235267Sgabor * tp->iss, and send a segment: 1000235267Sgabor * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1001235267Sgabor * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1002235267Sgabor * Fill in remote peer address fields if not previously specified. 1003235267Sgabor * Enter SYN_RECEIVED state, and process any other fields of this 1004235267Sgabor * segment in this state. 1005235267Sgabor */ 1006235267Sgabor case TCPS_LISTEN: { 1007235267Sgabor register struct sockaddr_in *sin; 1008235267Sgabor#ifdef INET6 1009235267Sgabor register struct sockaddr_in6 *sin6; 1010235267Sgabor#endif 1011235267Sgabor 1012235267Sgabor if (thflags & TH_RST) 1013235267Sgabor goto drop; 1014235267Sgabor if (thflags & TH_ACK) 1015235267Sgabor goto maybedropwithreset; 1016235267Sgabor if ((thflags & TH_SYN) == 0) 1017235267Sgabor goto drop; 1018235267Sgabor if (th->th_dport == th->th_sport) { 1019235267Sgabor#ifdef INET6 1020235267Sgabor if (isipv6) { 1021235267Sgabor if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 1022235267Sgabor &ip6->ip6_src)) 1023235267Sgabor goto drop; 1024235267Sgabor } else 1025235267Sgabor#endif /* INET6 */ 1026235267Sgabor if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 1027235267Sgabor goto drop; 1028235267Sgabor } 1029235267Sgabor /* 1030235267Sgabor * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 1031235267Sgabor * in_broadcast() should never return true on a received 1032235267Sgabor * packet with M_BCAST not set. 1033235267Sgabor * 1034235267Sgabor * Packets with a multicast source address should also 1035235267Sgabor * be discarded. 1036235267Sgabor */ 1037235267Sgabor if (m->m_flags & (M_BCAST|M_MCAST)) 1038235267Sgabor goto drop; 1039235267Sgabor#ifdef INET6 1040235267Sgabor if (isipv6) { 1041235267Sgabor if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1042235267Sgabor IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 1043235267Sgabor goto drop; 1044235267Sgabor } else 1045235267Sgabor#endif 1046235267Sgabor if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1047235267Sgabor IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 1048235267Sgabor ip->ip_src.s_addr == htonl(INADDR_BROADCAST)) 1049235267Sgabor goto drop; 1050235267Sgabor#ifdef INET6 1051235267Sgabor if (isipv6) { 1052235267Sgabor MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 1053235267Sgabor M_SONAME, M_NOWAIT | M_ZERO); 1054235267Sgabor if (sin6 == NULL) 1055235267Sgabor goto drop; 1056235267Sgabor sin6->sin6_family = AF_INET6; 1057235267Sgabor sin6->sin6_len = sizeof(*sin6); 1058235267Sgabor sin6->sin6_addr = ip6->ip6_src; 1059235267Sgabor sin6->sin6_port = th->th_sport; 1060235267Sgabor laddr6 = inp->in6p_laddr; 1061235267Sgabor if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1062235267Sgabor inp->in6p_laddr = ip6->ip6_dst; 1063235267Sgabor if (in6_pcbconnect(inp, (struct sockaddr *)sin6, 1064235267Sgabor &proc0)) { 1065235267Sgabor inp->in6p_laddr = laddr6; 1066235267Sgabor FREE(sin6, M_SONAME); 1067235267Sgabor goto drop; 1068235267Sgabor } 1069235267Sgabor FREE(sin6, M_SONAME); 1070235267Sgabor } else 1071235267Sgabor#endif 1072235267Sgabor { 1073235267Sgabor MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 1074235267Sgabor M_NOWAIT); 1075235267Sgabor if (sin == NULL) 1076235267Sgabor goto drop; 1077235267Sgabor sin->sin_family = AF_INET; 1078235267Sgabor sin->sin_len = sizeof(*sin); 1079235267Sgabor sin->sin_addr = ip->ip_src; 1080235267Sgabor sin->sin_port = th->th_sport; 1081235267Sgabor bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 1082235267Sgabor laddr = inp->inp_laddr; 1083235267Sgabor if (inp->inp_laddr.s_addr == INADDR_ANY) 1084235267Sgabor inp->inp_laddr = ip->ip_dst; 1085235267Sgabor if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) { 1086235267Sgabor inp->inp_laddr = laddr; 1087235267Sgabor FREE(sin, M_SONAME); 1088235267Sgabor goto drop; 1089235267Sgabor } 1090235267Sgabor FREE(sin, M_SONAME); 1091235267Sgabor } 1092235267Sgabor tp->t_template = tcp_template(tp); 1093235267Sgabor if (tp->t_template == 0) { 1094235267Sgabor tp = tcp_drop(tp, ENOBUFS); 1095235267Sgabor dropsocket = 0; /* socket is already gone */ 1096235267Sgabor goto drop; 1097235267Sgabor } 1098235267Sgabor if ((taop = tcp_gettaocache(inp)) == NULL) { 1099235267Sgabor taop = &tao_noncached; 1100235267Sgabor bzero(taop, sizeof(*taop)); 1101235267Sgabor } 1102235267Sgabor tcp_dooptions(tp, optp, optlen, th, &to); 1103235267Sgabor if (iss) 1104235267Sgabor tp->iss = iss; 1105235267Sgabor else 1106235267Sgabor tp->iss = tcp_iss; 1107235267Sgabor tcp_iss += TCP_ISSINCR/4; 1108235267Sgabor tp->irs = th->th_seq; 1109235267Sgabor tcp_sendseqinit(tp); 1110235267Sgabor tcp_rcvseqinit(tp); 1111235267Sgabor tp->snd_recover = tp->snd_una; 1112235267Sgabor /* 1113235267Sgabor * Initialization of the tcpcb for transaction; 1114235267Sgabor * set SND.WND = SEG.WND, 1115235267Sgabor * initialize CCsend and CCrecv. 1116235267Sgabor */ 1117235267Sgabor tp->snd_wnd = tiwin; /* initial send-window */ 1118235267Sgabor tp->cc_send = CC_INC(tcp_ccgen); 1119235267Sgabor tp->cc_recv = to.to_cc; 1120235267Sgabor /* 1121235267Sgabor * Perform TAO test on incoming CC (SEG.CC) option, if any. 1122235267Sgabor * - compare SEG.CC against cached CC from the same host, 1123235267Sgabor * if any. 1124235267Sgabor * - if SEG.CC > chached value, SYN must be new and is accepted 1125235267Sgabor * immediately: save new CC in the cache, mark the socket 1126235267Sgabor * connected, enter ESTABLISHED state, turn on flag to 1127235267Sgabor * send a SYN in the next segment. 1128235267Sgabor * A virtual advertised window is set in rcv_adv to 1129235267Sgabor * initialize SWS prevention. Then enter normal segment 1130235267Sgabor * processing: drop SYN, process data and FIN. 1131235267Sgabor * - otherwise do a normal 3-way handshake. 1132235267Sgabor */ 1133235267Sgabor if ((to.to_flag & TOF_CC) != 0) { 1134235267Sgabor if (((tp->t_flags & TF_NOPUSH) != 0) && 1135235267Sgabor taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 1136235267Sgabor 1137235267Sgabor taop->tao_cc = to.to_cc; 1138235267Sgabor tp->t_starttime = ticks; 1139235267Sgabor tp->t_state = TCPS_ESTABLISHED; 1140235267Sgabor 1141235267Sgabor /* 1142235267Sgabor * If there is a FIN, or if there is data and the 1143235267Sgabor * connection is local, then delay SYN,ACK(SYN) in 1144235267Sgabor * the hope of piggy-backing it on a response 1145235267Sgabor * segment. Otherwise must send ACK now in case 1146235267Sgabor * the other side is slow starting. 1147235267Sgabor */ 1148235267Sgabor if (tcp_delack_enabled && ((thflags & TH_FIN) || 1149235267Sgabor (tlen != 0 && 1150235267Sgabor#ifdef INET6 1151235267Sgabor ((isipv6 && in6_localaddr(&inp->in6p_faddr)) 1152235267Sgabor || 1153235267Sgabor (!isipv6 && 1154235267Sgabor#endif 1155235267Sgabor in_localaddr(inp->inp_faddr) 1156235267Sgabor#ifdef INET6 1157235267Sgabor )) 1158235267Sgabor#endif 1159235267Sgabor ))) { 1160235267Sgabor callout_reset(tp->tt_delack, tcp_delacktime, 1161235267Sgabor tcp_timer_delack, tp); 1162235267Sgabor tp->t_flags |= TF_NEEDSYN; 1163235267Sgabor } else 1164235267Sgabor tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1165235267Sgabor 1166235267Sgabor /* 1167235267Sgabor * Limit the `virtual advertised window' to TCP_MAXWIN 1168235267Sgabor * here. Even if we requested window scaling, it will 1169235267Sgabor * become effective only later when our SYN is acked. 1170235267Sgabor */ 1171235267Sgabor tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 1172235267Sgabor tcpstat.tcps_connects++; 1173235267Sgabor soisconnected(so); 1174235267Sgabor callout_reset(tp->tt_keep, tcp_keepinit, 1175235267Sgabor tcp_timer_keep, tp); 1176235267Sgabor dropsocket = 0; /* committed to socket */ 1177235267Sgabor tcpstat.tcps_accepts++; 1178235267Sgabor goto trimthenstep6; 1179235267Sgabor } 1180235267Sgabor /* else do standard 3-way handshake */ 1181235267Sgabor } else { 1182235267Sgabor /* 1183235267Sgabor * No CC option, but maybe CC.NEW: 1184235267Sgabor * invalidate cached value. 1185235267Sgabor */ 1186235267Sgabor taop->tao_cc = 0; 1187235267Sgabor } 1188235267Sgabor /* 1189235267Sgabor * TAO test failed or there was no CC option, 1190235267Sgabor * do a standard 3-way handshake. 1191235267Sgabor */ 1192235267Sgabor tp->t_flags |= TF_ACKNOW; 1193235267Sgabor tp->t_state = TCPS_SYN_RECEIVED; 1194235267Sgabor callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1195235267Sgabor dropsocket = 0; /* committed to socket */ 1196235267Sgabor tcpstat.tcps_accepts++; 1197235267Sgabor goto trimthenstep6; 1198235267Sgabor } 1199235267Sgabor 1200235267Sgabor /* 1201235267Sgabor * If the state is SYN_RECEIVED: 1202235267Sgabor * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1203235267Sgabor */ 1204235267Sgabor case TCPS_SYN_RECEIVED: 1205235267Sgabor if ((thflags & TH_ACK) && 1206235267Sgabor (SEQ_LEQ(th->th_ack, tp->snd_una) || 1207235267Sgabor SEQ_GT(th->th_ack, tp->snd_max))) 1208235267Sgabor goto maybedropwithreset; 1209235267Sgabor break; 1210235267Sgabor 1211235267Sgabor /* 1212235267Sgabor * If the state is SYN_SENT: 1213235267Sgabor * if seg contains an ACK, but not for our SYN, drop the input. 1214235267Sgabor * if seg contains a RST, then drop the connection. 1215235267Sgabor * if seg does not contain SYN, then drop it. 1216235267Sgabor * Otherwise this is an acceptable SYN segment 1217235267Sgabor * initialize tp->rcv_nxt and tp->irs 1218235267Sgabor * if seg contains ack then advance tp->snd_una 1219235267Sgabor * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1220235267Sgabor * arrange for segment to be acked (eventually) 1221235267Sgabor * continue processing rest of data/controls, beginning with URG 1222235267Sgabor */ 1223235267Sgabor case TCPS_SYN_SENT: 1224235267Sgabor if ((taop = tcp_gettaocache(inp)) == NULL) { 1225235267Sgabor taop = &tao_noncached; 1226235267Sgabor bzero(taop, sizeof(*taop)); 1227235267Sgabor } 1228235267Sgabor 1229235267Sgabor if ((thflags & TH_ACK) && 1230235267Sgabor (SEQ_LEQ(th->th_ack, tp->iss) || 1231235267Sgabor SEQ_GT(th->th_ack, tp->snd_max))) { 1232235267Sgabor /* 1233235267Sgabor * If we have a cached CCsent for the remote host, 1234235267Sgabor * hence we haven't just crashed and restarted, 1235235267Sgabor * do not send a RST. This may be a retransmission 1236235267Sgabor * from the other side after our earlier ACK was lost. 1237235267Sgabor * Our new SYN, when it arrives, will serve as the 1238235267Sgabor * needed ACK. 1239235267Sgabor */ 1240235267Sgabor if (taop->tao_ccsent != 0) 1241235267Sgabor goto drop; 1242235267Sgabor else 1243235267Sgabor goto dropwithreset; 1244235267Sgabor } 1245235267Sgabor if (thflags & TH_RST) { 1246235267Sgabor if (thflags & TH_ACK) 1247235267Sgabor tp = tcp_drop(tp, ECONNREFUSED); 1248235267Sgabor goto drop; 1249235267Sgabor } 1250235267Sgabor if ((thflags & TH_SYN) == 0) 1251235267Sgabor goto drop; 1252235267Sgabor tp->snd_wnd = th->th_win; /* initial send window */ 1253235267Sgabor tp->cc_recv = to.to_cc; /* foreign CC */ 1254235267Sgabor 1255235267Sgabor tp->irs = th->th_seq; 1256235267Sgabor tcp_rcvseqinit(tp); 1257235267Sgabor if (thflags & TH_ACK) { 1258235267Sgabor /* 1259235267Sgabor * Our SYN was acked. If segment contains CC.ECHO 1260235267Sgabor * option, check it to make sure this segment really 1261235267Sgabor * matches our SYN. If not, just drop it as old 1262235267Sgabor * duplicate, but send an RST if we're still playing 1263235267Sgabor * by the old rules. If no CC.ECHO option, make sure 1264235267Sgabor * we don't get fooled into using T/TCP. 1265235267Sgabor */ 1266235267Sgabor if (to.to_flag & TOF_CCECHO) { 1267235267Sgabor if (tp->cc_send != to.to_ccecho) { 1268235267Sgabor if (taop->tao_ccsent != 0) 1269235267Sgabor goto drop; 1270235267Sgabor else 1271235267Sgabor goto dropwithreset; 1272235267Sgabor } 1273235267Sgabor } else 1274235267Sgabor tp->t_flags &= ~TF_RCVD_CC; 1275235267Sgabor tcpstat.tcps_connects++; 1276235267Sgabor soisconnected(so); 1277235267Sgabor /* Do window scaling on this connection? */ 1278235267Sgabor if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1279235432Sgabor (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1280235267Sgabor tp->snd_scale = tp->requested_s_scale; 1281235267Sgabor tp->rcv_scale = tp->request_r_scale; 1282235267Sgabor } 1283235267Sgabor /* Segment is acceptable, update cache if undefined. */ 1284235267Sgabor if (taop->tao_ccsent == 0) 1285235267Sgabor taop->tao_ccsent = to.to_ccecho; 1286235267Sgabor 1287235267Sgabor tp->rcv_adv += tp->rcv_wnd; 1288235267Sgabor tp->snd_una++; /* SYN is acked */ 1289235267Sgabor /* 1290235267Sgabor * If there's data, delay ACK; if there's also a FIN 1291235267Sgabor * ACKNOW will be turned on later. 1292235267Sgabor */ 1293235267Sgabor if (tcp_delack_enabled && tlen != 0) 1294235267Sgabor callout_reset(tp->tt_delack, tcp_delacktime, 1295235432Sgabor tcp_timer_delack, tp); 1296235267Sgabor else 1297235267Sgabor tp->t_flags |= TF_ACKNOW; 1298235267Sgabor /* 1299235267Sgabor * Received <SYN,ACK> in SYN_SENT[*] state. 1300235267Sgabor * Transitions: 1301235267Sgabor * SYN_SENT --> ESTABLISHED 1302235267Sgabor * SYN_SENT* --> FIN_WAIT_1 1303235267Sgabor */ 1304235267Sgabor tp->t_starttime = ticks; 1305235267Sgabor if (tp->t_flags & TF_NEEDFIN) { 1306235267Sgabor tp->t_state = TCPS_FIN_WAIT_1; 1307235267Sgabor tp->t_flags &= ~TF_NEEDFIN; 1308235267Sgabor thflags &= ~TH_SYN; 1309235267Sgabor } else { 1310235267Sgabor tp->t_state = TCPS_ESTABLISHED; 1311235267Sgabor callout_reset(tp->tt_keep, tcp_keepidle, 1312235267Sgabor tcp_timer_keep, tp); 1313235267Sgabor } 1314235267Sgabor } else { 1315235267Sgabor /* 1316235267Sgabor * Received initial SYN in SYN-SENT[*] state => simul- 1317235267Sgabor * taneous open. If segment contains CC option and there is 1318235267Sgabor * a cached CC, apply TAO test; if it succeeds, connection is 1319235267Sgabor * half-synchronized. Otherwise, do 3-way handshake: 1320235267Sgabor * SYN-SENT -> SYN-RECEIVED 1321235267Sgabor * SYN-SENT* -> SYN-RECEIVED* 1322235267Sgabor * If there was no CC option, clear cached CC value. 1323235267Sgabor */ 1324235267Sgabor tp->t_flags |= TF_ACKNOW; 1325235267Sgabor callout_stop(tp->tt_rexmt); 1326235267Sgabor if (to.to_flag & TOF_CC) { 1327235267Sgabor if (taop->tao_cc != 0 && 1328235267Sgabor CC_GT(to.to_cc, taop->tao_cc)) { 1329235267Sgabor /* 1330235267Sgabor * update cache and make transition: 1331235267Sgabor * SYN-SENT -> ESTABLISHED* 1332235267Sgabor * SYN-SENT* -> FIN-WAIT-1* 1333235267Sgabor */ 1334235267Sgabor taop->tao_cc = to.to_cc; 1335235267Sgabor tp->t_starttime = ticks; 1336235267Sgabor if (tp->t_flags & TF_NEEDFIN) { 1337235267Sgabor tp->t_state = TCPS_FIN_WAIT_1; 1338235267Sgabor tp->t_flags &= ~TF_NEEDFIN; 1339235267Sgabor } else { 1340235267Sgabor tp->t_state = TCPS_ESTABLISHED; 1341235267Sgabor callout_reset(tp->tt_keep, 1342235267Sgabor tcp_keepidle, 1343235267Sgabor tcp_timer_keep, 1344235267Sgabor tp); 1345235267Sgabor } 1346235267Sgabor tp->t_flags |= TF_NEEDSYN; 1347235267Sgabor } else 1348235267Sgabor tp->t_state = TCPS_SYN_RECEIVED; 1349235267Sgabor } else { 1350235267Sgabor /* CC.NEW or no option => invalidate cache */ 1351235267Sgabor taop->tao_cc = 0; 1352235267Sgabor tp->t_state = TCPS_SYN_RECEIVED; 1353235267Sgabor } 1354235267Sgabor } 1355235267Sgabor 1356235267Sgabortrimthenstep6: 1357235267Sgabor /* 1358235267Sgabor * Advance th->th_seq to correspond to first data byte. 1359235267Sgabor * If data, trim to stay within window, 1360235267Sgabor * dropping FIN if necessary. 1361235267Sgabor */ 1362235267Sgabor th->th_seq++; 1363235267Sgabor if (tlen > tp->rcv_wnd) { 1364235267Sgabor todrop = tlen - tp->rcv_wnd; 1365235267Sgabor m_adj(m, -todrop); 1366235267Sgabor tlen = tp->rcv_wnd; 1367235267Sgabor thflags &= ~TH_FIN; 1368235267Sgabor tcpstat.tcps_rcvpackafterwin++; 1369235267Sgabor tcpstat.tcps_rcvbyteafterwin += todrop; 1370235267Sgabor } 1371235267Sgabor tp->snd_wl1 = th->th_seq - 1; 1372235267Sgabor tp->rcv_up = th->th_seq; 1373235267Sgabor /* 1374235267Sgabor * Client side of transaction: already sent SYN and data. 1375235267Sgabor * If the remote host used T/TCP to validate the SYN, 1376235267Sgabor * our data will be ACK'd; if so, enter normal data segment 1377235267Sgabor * processing in the middle of step 5, ack processing. 1378235267Sgabor * Otherwise, goto step 6. 1379235267Sgabor */ 1380235267Sgabor if (thflags & TH_ACK) 1381235267Sgabor goto process_ACK; 1382235267Sgabor goto step6; 1383235267Sgabor /* 1384235267Sgabor * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1385235267Sgabor * if segment contains a SYN and CC [not CC.NEW] option: 1386235267Sgabor * if state == TIME_WAIT and connection duration > MSL, 1387235267Sgabor * drop packet and send RST; 1388235267Sgabor * 1389235267Sgabor * if SEG.CC > CCrecv then is new SYN, and can implicitly 1390235267Sgabor * ack the FIN (and data) in retransmission queue. 1391235267Sgabor * Complete close and delete TCPCB. Then reprocess 1392235267Sgabor * segment, hoping to find new TCPCB in LISTEN state; 1393235267Sgabor * 1394235267Sgabor * else must be old SYN; drop it. 1395235267Sgabor * else do normal processing. 1396235267Sgabor */ 1397235267Sgabor case TCPS_LAST_ACK: 1398235267Sgabor case TCPS_CLOSING: 1399235267Sgabor case TCPS_TIME_WAIT: 1400235267Sgabor if ((thflags & TH_SYN) && 1401235267Sgabor (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 1402235267Sgabor if (tp->t_state == TCPS_TIME_WAIT && 1403235267Sgabor (ticks - tp->t_starttime) > tcp_msl) 1404235267Sgabor goto dropwithreset; 1405235267Sgabor if (CC_GT(to.to_cc, tp->cc_recv)) { 1406235267Sgabor tp = tcp_close(tp); 1407235267Sgabor goto findpcb; 1408235267Sgabor } 1409235267Sgabor else 1410235267Sgabor goto drop; 1411235267Sgabor } 1412235267Sgabor break; /* continue normal processing */ 1413235267Sgabor } 1414235267Sgabor 1415235267Sgabor /* 1416235267Sgabor * States other than LISTEN or SYN_SENT. 1417235267Sgabor * First check the RST flag and sequence number since reset segments 1418235267Sgabor * are exempt from the timestamp and connection count tests. This 1419235267Sgabor * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1420235267Sgabor * below which allowed reset segments in half the sequence space 1421235267Sgabor * to fall though and be processed (which gives forged reset 1422235267Sgabor * segments with a random sequence number a 50 percent chance of 1423235267Sgabor * killing a connection). 1424235267Sgabor * Then check timestamp, if present. 1425235267Sgabor * Then check the connection count, if present. 1426235267Sgabor * Then check that at least some bytes of segment are within 1427235267Sgabor * receive window. If segment begins before rcv_nxt, 1428235267Sgabor * drop leading data (and SYN); if nothing left, just ack. 1429235267Sgabor * 1430235267Sgabor * 1431235267Sgabor * If the RST bit is set, check the sequence number to see 1432235267Sgabor * if this is a valid reset segment. 1433235267Sgabor * RFC 793 page 37: 1434235267Sgabor * In all states except SYN-SENT, all reset (RST) segments 1435235267Sgabor * are validated by checking their SEQ-fields. A reset is 1436235267Sgabor * valid if its sequence number is in the window. 1437235267Sgabor * Note: this does not take into account delayed ACKs, so 1438235267Sgabor * we should test against last_ack_sent instead of rcv_nxt. 1439235267Sgabor * The sequence number in the reset segment is normally an 1440235267Sgabor * echo of our outgoing acknowlegement numbers, but some hosts 1441235267Sgabor * send a reset with the sequence number at the rightmost edge 1442235267Sgabor * of our receive window, and we have to handle this case. 1443235267Sgabor * If we have multiple segments in flight, the intial reset 1444235267Sgabor * segment sequence numbers will be to the left of last_ack_sent, 1445235267Sgabor * but they will eventually catch up. 1446235267Sgabor * In any case, it never made sense to trim reset segments to 1447235267Sgabor * fit the receive window since RFC 1122 says: 1448235267Sgabor * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1449235267Sgabor * 1450235267Sgabor * A TCP SHOULD allow a received RST segment to include data. 1451235267Sgabor * 1452235267Sgabor * DISCUSSION 1453235267Sgabor * It has been suggested that a RST segment could contain 1454235267Sgabor * ASCII text that encoded and explained the cause of the 1455235267Sgabor * RST. No standard has yet been established for such 1456235267Sgabor * data. 1457235267Sgabor * 1458235267Sgabor * If the reset segment passes the sequence number test examine 1459235267Sgabor * the state: 1460235267Sgabor * SYN_RECEIVED STATE: 1461235267Sgabor * If passive open, return to LISTEN state. 1462235267Sgabor * If active open, inform user that connection was refused. 1463235267Sgabor * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1464235267Sgabor * Inform user that connection was reset, and close tcb. 1465235267Sgabor * CLOSING, LAST_ACK STATES: 1466235267Sgabor * Close the tcb. 1467235267Sgabor * TIME_WAIT STATE: 1468235267Sgabor * Drop the segment - see Stevens, vol. 2, p. 964 and 1469235267Sgabor * RFC 1337. 1470235267Sgabor */ 1471235267Sgabor if (thflags & TH_RST) { 1472235267Sgabor if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1473235267Sgabor SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1474235267Sgabor switch (tp->t_state) { 1475235267Sgabor 1476235267Sgabor case TCPS_SYN_RECEIVED: 1477235267Sgabor so->so_error = ECONNREFUSED; 1478235267Sgabor goto close; 1479235267Sgabor 1480235267Sgabor case TCPS_ESTABLISHED: 1481235267Sgabor case TCPS_FIN_WAIT_1: 1482235267Sgabor case TCPS_FIN_WAIT_2: 1483235267Sgabor case TCPS_CLOSE_WAIT: 1484235267Sgabor so->so_error = ECONNRESET; 1485235267Sgabor close: 1486235267Sgabor tp->t_state = TCPS_CLOSED; 1487235267Sgabor tcpstat.tcps_drops++; 1488235267Sgabor tp = tcp_close(tp); 1489235267Sgabor break; 1490235267Sgabor 1491235267Sgabor case TCPS_CLOSING: 1492235267Sgabor case TCPS_LAST_ACK: 1493235267Sgabor tp = tcp_close(tp); 1494235267Sgabor break; 1495235267Sgabor 1496235267Sgabor case TCPS_TIME_WAIT: 1497235267Sgabor break; 1498235267Sgabor } 1499235267Sgabor } 1500235267Sgabor goto drop; 1501235267Sgabor } 1502235267Sgabor 1503235267Sgabor /* 1504235267Sgabor * RFC 1323 PAWS: If we have a timestamp reply on this segment 1505235267Sgabor * and it's less than ts_recent, drop it. 1506235267Sgabor */ 1507235267Sgabor if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent && 1508235267Sgabor TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1509235267Sgabor 1510235267Sgabor /* Check to see if ts_recent is over 24 days old. */ 1511235267Sgabor if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1512235267Sgabor /* 1513235267Sgabor * Invalidate ts_recent. If this segment updates 1514235267Sgabor * ts_recent, the age will be reset later and ts_recent 1515235267Sgabor * will get a valid value. If it does not, setting 1516235267Sgabor * ts_recent to zero will at least satisfy the 1517235267Sgabor * requirement that zero be placed in the timestamp 1518235267Sgabor * echo reply when ts_recent isn't valid. The 1519235267Sgabor * age isn't reset until we get a valid ts_recent 1520235267Sgabor * because we don't want out-of-order segments to be 1521235267Sgabor * dropped when ts_recent is old. 1522235267Sgabor */ 1523235267Sgabor tp->ts_recent = 0; 1524235267Sgabor } else { 1525235267Sgabor tcpstat.tcps_rcvduppack++; 1526235267Sgabor tcpstat.tcps_rcvdupbyte += tlen; 1527235267Sgabor tcpstat.tcps_pawsdrop++; 1528235267Sgabor goto dropafterack; 1529235267Sgabor } 1530235267Sgabor } 1531235267Sgabor 1532235267Sgabor /* 1533235267Sgabor * T/TCP mechanism 1534235267Sgabor * If T/TCP was negotiated and the segment doesn't have CC, 1535235267Sgabor * or if its CC is wrong then drop the segment. 1536235267Sgabor * RST segments do not have to comply with this. 1537235267Sgabor */ 1538235267Sgabor if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1539235267Sgabor ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1540235267Sgabor goto dropafterack; 1541235267Sgabor 1542235987Sgabor /* 1543235987Sgabor * In the SYN-RECEIVED state, validate that the packet belongs to 1544235987Sgabor * this connection before trimming the data to fit the receive 1545235267Sgabor * window. Check the sequence number versus IRS since we know 1546235267Sgabor * the sequence numbers haven't wrapped. This is a partial fix 1547235267Sgabor * for the "LAND" DoS attack. 1548235267Sgabor */ 1549235267Sgabor if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) 1550235267Sgabor goto maybedropwithreset; 1551235267Sgabor 1552235987Sgabor todrop = tp->rcv_nxt - th->th_seq; 1553235267Sgabor if (todrop > 0) { 1554235267Sgabor if (thflags & TH_SYN) { 1555235267Sgabor thflags &= ~TH_SYN; 1556235267Sgabor th->th_seq++; 1557235267Sgabor if (th->th_urp > 1) 1558235267Sgabor th->th_urp--; 1559235267Sgabor else 1560235267Sgabor thflags &= ~TH_URG; 1561235267Sgabor todrop--; 1562235267Sgabor } 1563235267Sgabor /* 1564235267Sgabor * Following if statement from Stevens, vol. 2, p. 960. 1565235267Sgabor */ 1566235267Sgabor if (todrop > tlen 1567235267Sgabor || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1568235267Sgabor /* 1569235267Sgabor * Any valid FIN must be to the left of the window. 1570235267Sgabor * At this point the FIN must be a duplicate or out 1571235267Sgabor * of sequence; drop it. 1572235267Sgabor */ 1573235267Sgabor thflags &= ~TH_FIN; 1574235267Sgabor 1575235267Sgabor /* 1576235267Sgabor * Send an ACK to resynchronize and drop any data. 1577235267Sgabor * But keep on processing for RST or ACK. 1578235267Sgabor */ 1579235267Sgabor tp->t_flags |= TF_ACKNOW; 1580235267Sgabor todrop = tlen; 1581235267Sgabor tcpstat.tcps_rcvduppack++; 1582235267Sgabor tcpstat.tcps_rcvdupbyte += todrop; 1583235267Sgabor } else { 1584235267Sgabor tcpstat.tcps_rcvpartduppack++; 1585235267Sgabor tcpstat.tcps_rcvpartdupbyte += todrop; 1586235267Sgabor } 1587235267Sgabor drop_hdrlen += todrop; /* drop from the top afterwards */ 1588235267Sgabor th->th_seq += todrop; 1589235267Sgabor tlen -= todrop; 1590235267Sgabor if (th->th_urp > todrop) 1591235267Sgabor th->th_urp -= todrop; 1592235267Sgabor else { 1593235267Sgabor thflags &= ~TH_URG; 1594235267Sgabor th->th_urp = 0; 1595235267Sgabor } 1596235987Sgabor } 1597235987Sgabor 1598235987Sgabor /* 1599235267Sgabor * If new data are received on a connection after the 1600235987Sgabor * user processes are gone, then RST the other end. 1601235987Sgabor */ 1602235987Sgabor if ((so->so_state & SS_NOFDREF) && 1603235987Sgabor tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1604235987Sgabor tp = tcp_close(tp); 1605235987Sgabor tcpstat.tcps_rcvafterclose++; 1606235987Sgabor goto dropwithreset; 1607235987Sgabor } 1608235987Sgabor 1609235267Sgabor /* 1610235267Sgabor * If segment ends after window, drop trailing data 1611235267Sgabor * (and PUSH and FIN); if nothing left, just ACK. 1612235267Sgabor */ 1613235267Sgabor todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1614235267Sgabor if (todrop > 0) { 1615235267Sgabor tcpstat.tcps_rcvpackafterwin++; 1616235267Sgabor if (todrop >= tlen) { 1617235267Sgabor tcpstat.tcps_rcvbyteafterwin += tlen; 1618235267Sgabor /* 1619235267Sgabor * If a new connection request is received 1620235267Sgabor * while in TIME_WAIT, drop the old connection 1621235267Sgabor * and start over if the sequence numbers 1622235267Sgabor * are above the previous ones. 1623235267Sgabor */ 1624235267Sgabor if (thflags & TH_SYN && 1625235267Sgabor tp->t_state == TCPS_TIME_WAIT && 1626235267Sgabor SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1627235267Sgabor iss = tp->snd_nxt + TCP_ISSINCR; 1628235267Sgabor tp = tcp_close(tp); 1629235267Sgabor goto findpcb; 1630 } 1631 /* 1632 * If window is closed can only take segments at 1633 * window edge, and have to drop data and PUSH from 1634 * incoming segments. Continue processing, but 1635 * remember to ack. Otherwise, drop segment 1636 * and ack. 1637 */ 1638 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1639 tp->t_flags |= TF_ACKNOW; 1640 tcpstat.tcps_rcvwinprobe++; 1641 } else 1642 goto dropafterack; 1643 } else 1644 tcpstat.tcps_rcvbyteafterwin += todrop; 1645 m_adj(m, -todrop); 1646 tlen -= todrop; 1647 thflags &= ~(TH_PUSH|TH_FIN); 1648 } 1649 1650 /* 1651 * If last ACK falls within this segment's sequence numbers, 1652 * record its timestamp. 1653 * NOTE that the test is modified according to the latest 1654 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1655 */ 1656 if ((to.to_flag & TOF_TS) != 0 && 1657 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1658 tp->ts_recent_age = ticks; 1659 tp->ts_recent = to.to_tsval; 1660 } 1661 1662 /* 1663 * If a SYN is in the window, then this is an 1664 * error and we send an RST and drop the connection. 1665 */ 1666 if (thflags & TH_SYN) { 1667 tp = tcp_drop(tp, ECONNRESET); 1668 goto dropwithreset; 1669 } 1670 1671 /* 1672 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1673 * flag is on (half-synchronized state), then queue data for 1674 * later processing; else drop segment and return. 1675 */ 1676 if ((thflags & TH_ACK) == 0) { 1677 if (tp->t_state == TCPS_SYN_RECEIVED || 1678 (tp->t_flags & TF_NEEDSYN)) 1679 goto step6; 1680 else 1681 goto drop; 1682 } 1683 1684 /* 1685 * Ack processing. 1686 */ 1687 switch (tp->t_state) { 1688 1689 /* 1690 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1691 * ESTABLISHED state and continue processing. 1692 * The ACK was checked above. 1693 */ 1694 case TCPS_SYN_RECEIVED: 1695 1696 tcpstat.tcps_connects++; 1697 soisconnected(so); 1698 /* Do window scaling? */ 1699 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1700 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1701 tp->snd_scale = tp->requested_s_scale; 1702 tp->rcv_scale = tp->request_r_scale; 1703 } 1704 /* 1705 * Upon successful completion of 3-way handshake, 1706 * update cache.CC if it was undefined, pass any queued 1707 * data to the user, and advance state appropriately. 1708 */ 1709 if ((taop = tcp_gettaocache(inp)) != NULL && 1710 taop->tao_cc == 0) 1711 taop->tao_cc = tp->cc_recv; 1712 1713 /* 1714 * Make transitions: 1715 * SYN-RECEIVED -> ESTABLISHED 1716 * SYN-RECEIVED* -> FIN-WAIT-1 1717 */ 1718 tp->t_starttime = ticks; 1719 if (tp->t_flags & TF_NEEDFIN) { 1720 tp->t_state = TCPS_FIN_WAIT_1; 1721 tp->t_flags &= ~TF_NEEDFIN; 1722 } else { 1723 tp->t_state = TCPS_ESTABLISHED; 1724 callout_reset(tp->tt_keep, tcp_keepidle, 1725 tcp_timer_keep, tp); 1726 } 1727 /* 1728 * If segment contains data or ACK, will call tcp_reass() 1729 * later; if not, do so now to pass queued data to user. 1730 */ 1731 if (tlen == 0 && (thflags & TH_FIN) == 0) 1732 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1733 (struct mbuf *)0); 1734 tp->snd_wl1 = th->th_seq - 1; 1735 /* fall into ... */ 1736 1737 /* 1738 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1739 * ACKs. If the ack is in the range 1740 * tp->snd_una < th->th_ack <= tp->snd_max 1741 * then advance tp->snd_una to th->th_ack and drop 1742 * data from the retransmission queue. If this ACK reflects 1743 * more up to date window information we update our window information. 1744 */ 1745 case TCPS_ESTABLISHED: 1746 case TCPS_FIN_WAIT_1: 1747 case TCPS_FIN_WAIT_2: 1748 case TCPS_CLOSE_WAIT: 1749 case TCPS_CLOSING: 1750 case TCPS_LAST_ACK: 1751 case TCPS_TIME_WAIT: 1752 1753 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1754 if (tlen == 0 && tiwin == tp->snd_wnd) { 1755 tcpstat.tcps_rcvdupack++; 1756 /* 1757 * If we have outstanding data (other than 1758 * a window probe), this is a completely 1759 * duplicate ack (ie, window info didn't 1760 * change), the ack is the biggest we've 1761 * seen and we've seen exactly our rexmt 1762 * threshhold of them, assume a packet 1763 * has been dropped and retransmit it. 1764 * Kludge snd_nxt & the congestion 1765 * window so we send only this one 1766 * packet. 1767 * 1768 * We know we're losing at the current 1769 * window size so do congestion avoidance 1770 * (set ssthresh to half the current window 1771 * and pull our congestion window back to 1772 * the new ssthresh). 1773 * 1774 * Dup acks mean that packets have left the 1775 * network (they're now cached at the receiver) 1776 * so bump cwnd by the amount in the receiver 1777 * to keep a constant cwnd packets in the 1778 * network. 1779 */ 1780 if (!callout_active(tp->tt_rexmt) || 1781 th->th_ack != tp->snd_una) 1782 tp->t_dupacks = 0; 1783 else if (++tp->t_dupacks == tcprexmtthresh) { 1784 tcp_seq onxt = tp->snd_nxt; 1785 u_int win = 1786 min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1787 tp->t_maxseg; 1788 if (tcp_do_newreno && SEQ_LT(th->th_ack, 1789 tp->snd_recover)) { 1790 /* False retransmit, should not 1791 * cut window 1792 */ 1793 tp->snd_cwnd += tp->t_maxseg; 1794 tp->t_dupacks = 0; 1795 (void) tcp_output(tp); 1796 goto drop; 1797 } 1798 if (win < 2) 1799 win = 2; 1800 tp->snd_ssthresh = win * tp->t_maxseg; 1801 tp->snd_recover = tp->snd_max; 1802 callout_stop(tp->tt_rexmt); 1803 tp->t_rtttime = 0; 1804 tp->snd_nxt = th->th_ack; 1805 tp->snd_cwnd = tp->t_maxseg; 1806 (void) tcp_output(tp); 1807 tp->snd_cwnd = tp->snd_ssthresh + 1808 tp->t_maxseg * tp->t_dupacks; 1809 if (SEQ_GT(onxt, tp->snd_nxt)) 1810 tp->snd_nxt = onxt; 1811 goto drop; 1812 } else if (tp->t_dupacks > tcprexmtthresh) { 1813 tp->snd_cwnd += tp->t_maxseg; 1814 (void) tcp_output(tp); 1815 goto drop; 1816 } 1817 } else 1818 tp->t_dupacks = 0; 1819 break; 1820 } 1821 /* 1822 * If the congestion window was inflated to account 1823 * for the other side's cached packets, retract it. 1824 */ 1825 if (tcp_do_newreno == 0) { 1826 if (tp->t_dupacks >= tcprexmtthresh && 1827 tp->snd_cwnd > tp->snd_ssthresh) 1828 tp->snd_cwnd = tp->snd_ssthresh; 1829 tp->t_dupacks = 0; 1830 } else if (tp->t_dupacks >= tcprexmtthresh && 1831 !tcp_newreno(tp, th)) { 1832 /* 1833 * Window inflation should have left us with approx. 1834 * snd_ssthresh outstanding data. But in case we 1835 * would be inclined to send a burst, better to do 1836 * it via the slow start mechanism. 1837 */ 1838 if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max)) 1839 tp->snd_cwnd = 1840 tp->snd_max - th->th_ack + tp->t_maxseg; 1841 else 1842 tp->snd_cwnd = tp->snd_ssthresh; 1843 tp->t_dupacks = 0; 1844 } 1845 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1846 tcpstat.tcps_rcvacktoomuch++; 1847 goto dropafterack; 1848 } 1849 /* 1850 * If we reach this point, ACK is not a duplicate, 1851 * i.e., it ACKs something we sent. 1852 */ 1853 if (tp->t_flags & TF_NEEDSYN) { 1854 /* 1855 * T/TCP: Connection was half-synchronized, and our 1856 * SYN has been ACK'd (so connection is now fully 1857 * synchronized). Go to non-starred state, 1858 * increment snd_una for ACK of SYN, and check if 1859 * we can do window scaling. 1860 */ 1861 tp->t_flags &= ~TF_NEEDSYN; 1862 tp->snd_una++; 1863 /* Do window scaling? */ 1864 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1865 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1866 tp->snd_scale = tp->requested_s_scale; 1867 tp->rcv_scale = tp->request_r_scale; 1868 } 1869 } 1870 1871process_ACK: 1872 acked = th->th_ack - tp->snd_una; 1873 tcpstat.tcps_rcvackpack++; 1874 tcpstat.tcps_rcvackbyte += acked; 1875 1876 /* 1877 * If we just performed our first retransmit, and the ACK 1878 * arrives within our recovery window, then it was a mistake 1879 * to do the retransmit in the first place. Recover our 1880 * original cwnd and ssthresh, and proceed to transmit where 1881 * we left off. 1882 */ 1883 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1884 tp->snd_cwnd = tp->snd_cwnd_prev; 1885 tp->snd_ssthresh = tp->snd_ssthresh_prev; 1886 tp->snd_nxt = tp->snd_max; 1887 tp->t_badrxtwin = 0; /* XXX probably not required */ 1888 } 1889 1890 /* 1891 * If we have a timestamp reply, update smoothed 1892 * round trip time. If no timestamp is present but 1893 * transmit timer is running and timed sequence 1894 * number was acked, update smoothed round trip time. 1895 * Since we now have an rtt measurement, cancel the 1896 * timer backoff (cf., Phil Karn's retransmit alg.). 1897 * Recompute the initial retransmit timer. 1898 */ 1899 if (to.to_flag & TOF_TS) 1900 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1901 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 1902 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1903 1904 /* 1905 * If all outstanding data is acked, stop retransmit 1906 * timer and remember to restart (more output or persist). 1907 * If there is more data to be acked, restart retransmit 1908 * timer, using current (possibly backed-off) value. 1909 */ 1910 if (th->th_ack == tp->snd_max) { 1911 callout_stop(tp->tt_rexmt); 1912 needoutput = 1; 1913 } else if (!callout_active(tp->tt_persist)) 1914 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 1915 tcp_timer_rexmt, tp); 1916 1917 /* 1918 * If no data (only SYN) was ACK'd, 1919 * skip rest of ACK processing. 1920 */ 1921 if (acked == 0) 1922 goto step6; 1923 1924 /* 1925 * When new data is acked, open the congestion window. 1926 * If the window gives us less than ssthresh packets 1927 * in flight, open exponentially (maxseg per packet). 1928 * Otherwise open linearly: maxseg per window 1929 * (maxseg^2 / cwnd per packet). 1930 */ 1931 { 1932 register u_int cw = tp->snd_cwnd; 1933 register u_int incr = tp->t_maxseg; 1934 1935 if (cw > tp->snd_ssthresh) 1936 incr = incr * incr / cw; 1937 /* 1938 * If t_dupacks != 0 here, it indicates that we are still 1939 * in NewReno fast recovery mode, so we leave the congestion 1940 * window alone. 1941 */ 1942 if (tcp_do_newreno == 0 || tp->t_dupacks == 0) 1943 tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale); 1944 } 1945 if (acked > so->so_snd.sb_cc) { 1946 tp->snd_wnd -= so->so_snd.sb_cc; 1947 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1948 ourfinisacked = 1; 1949 } else { 1950 sbdrop(&so->so_snd, acked); 1951 tp->snd_wnd -= acked; 1952 ourfinisacked = 0; 1953 } 1954 sowwakeup(so); 1955 tp->snd_una = th->th_ack; 1956 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1957 tp->snd_nxt = tp->snd_una; 1958 1959 switch (tp->t_state) { 1960 1961 /* 1962 * In FIN_WAIT_1 STATE in addition to the processing 1963 * for the ESTABLISHED state if our FIN is now acknowledged 1964 * then enter FIN_WAIT_2. 1965 */ 1966 case TCPS_FIN_WAIT_1: 1967 if (ourfinisacked) { 1968 /* 1969 * If we can't receive any more 1970 * data, then closing user can proceed. 1971 * Starting the timer is contrary to the 1972 * specification, but if we don't get a FIN 1973 * we'll hang forever. 1974 */ 1975 if (so->so_state & SS_CANTRCVMORE) { 1976 soisdisconnected(so); 1977 callout_reset(tp->tt_2msl, tcp_maxidle, 1978 tcp_timer_2msl, tp); 1979 } 1980 tp->t_state = TCPS_FIN_WAIT_2; 1981 } 1982 break; 1983 1984 /* 1985 * In CLOSING STATE in addition to the processing for 1986 * the ESTABLISHED state if the ACK acknowledges our FIN 1987 * then enter the TIME-WAIT state, otherwise ignore 1988 * the segment. 1989 */ 1990 case TCPS_CLOSING: 1991 if (ourfinisacked) { 1992 tp->t_state = TCPS_TIME_WAIT; 1993 tcp_canceltimers(tp); 1994 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1995 if (tp->cc_recv != 0 && 1996 (ticks - tp->t_starttime) < tcp_msl) 1997 callout_reset(tp->tt_2msl, 1998 tp->t_rxtcur * 1999 TCPTV_TWTRUNC, 2000 tcp_timer_2msl, tp); 2001 else 2002 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2003 tcp_timer_2msl, tp); 2004 soisdisconnected(so); 2005 } 2006 break; 2007 2008 /* 2009 * In LAST_ACK, we may still be waiting for data to drain 2010 * and/or to be acked, as well as for the ack of our FIN. 2011 * If our FIN is now acknowledged, delete the TCB, 2012 * enter the closed state and return. 2013 */ 2014 case TCPS_LAST_ACK: 2015 if (ourfinisacked) { 2016 tp = tcp_close(tp); 2017 goto drop; 2018 } 2019 break; 2020 2021 /* 2022 * In TIME_WAIT state the only thing that should arrive 2023 * is a retransmission of the remote FIN. Acknowledge 2024 * it and restart the finack timer. 2025 */ 2026 case TCPS_TIME_WAIT: 2027 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2028 tcp_timer_2msl, tp); 2029 goto dropafterack; 2030 } 2031 } 2032 2033step6: 2034 /* 2035 * Update window information. 2036 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2037 */ 2038 if ((thflags & TH_ACK) && 2039 (SEQ_LT(tp->snd_wl1, th->th_seq) || 2040 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2041 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2042 /* keep track of pure window updates */ 2043 if (tlen == 0 && 2044 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2045 tcpstat.tcps_rcvwinupd++; 2046 tp->snd_wnd = tiwin; 2047 tp->snd_wl1 = th->th_seq; 2048 tp->snd_wl2 = th->th_ack; 2049 if (tp->snd_wnd > tp->max_sndwnd) 2050 tp->max_sndwnd = tp->snd_wnd; 2051 needoutput = 1; 2052 } 2053 2054 /* 2055 * Process segments with URG. 2056 */ 2057 if ((thflags & TH_URG) && th->th_urp && 2058 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2059 /* 2060 * This is a kludge, but if we receive and accept 2061 * random urgent pointers, we'll crash in 2062 * soreceive. It's hard to imagine someone 2063 * actually wanting to send this much urgent data. 2064 */ 2065 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2066 th->th_urp = 0; /* XXX */ 2067 thflags &= ~TH_URG; /* XXX */ 2068 goto dodata; /* XXX */ 2069 } 2070 /* 2071 * If this segment advances the known urgent pointer, 2072 * then mark the data stream. This should not happen 2073 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2074 * a FIN has been received from the remote side. 2075 * In these states we ignore the URG. 2076 * 2077 * According to RFC961 (Assigned Protocols), 2078 * the urgent pointer points to the last octet 2079 * of urgent data. We continue, however, 2080 * to consider it to indicate the first octet 2081 * of data past the urgent section as the original 2082 * spec states (in one of two places). 2083 */ 2084 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2085 tp->rcv_up = th->th_seq + th->th_urp; 2086 so->so_oobmark = so->so_rcv.sb_cc + 2087 (tp->rcv_up - tp->rcv_nxt) - 1; 2088 if (so->so_oobmark == 0) 2089 so->so_state |= SS_RCVATMARK; 2090 sohasoutofband(so); 2091 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2092 } 2093 /* 2094 * Remove out of band data so doesn't get presented to user. 2095 * This can happen independent of advancing the URG pointer, 2096 * but if two URG's are pending at once, some out-of-band 2097 * data may creep in... ick. 2098 */ 2099 if (th->th_urp <= (u_long)tlen 2100#ifdef SO_OOBINLINE 2101 && (so->so_options & SO_OOBINLINE) == 0 2102#endif 2103 ) 2104 tcp_pulloutofband(so, th, m, 2105 drop_hdrlen); /* hdr drop is delayed */ 2106 } else 2107 /* 2108 * If no out of band data is expected, 2109 * pull receive urgent pointer along 2110 * with the receive window. 2111 */ 2112 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2113 tp->rcv_up = tp->rcv_nxt; 2114dodata: /* XXX */ 2115 2116 /* 2117 * Process the segment text, merging it into the TCP sequencing queue, 2118 * and arranging for acknowledgment of receipt if necessary. 2119 * This process logically involves adjusting tp->rcv_wnd as data 2120 * is presented to the user (this happens in tcp_usrreq.c, 2121 * case PRU_RCVD). If a FIN has already been received on this 2122 * connection then we just ignore the text. 2123 */ 2124 if ((tlen || (thflags&TH_FIN)) && 2125 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2126 m_adj(m, drop_hdrlen); /* delayed header drop */ 2127 TCP_REASS(tp, th, &tlen, m, so, thflags); 2128 /* 2129 * Note the amount of data that peer has sent into 2130 * our window, in order to estimate the sender's 2131 * buffer size. 2132 */ 2133 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2134 } else { 2135 m_freem(m); 2136 thflags &= ~TH_FIN; 2137 } 2138 2139 /* 2140 * If FIN is received ACK the FIN and let the user know 2141 * that the connection is closing. 2142 */ 2143 if (thflags & TH_FIN) { 2144 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2145 socantrcvmore(so); 2146 /* 2147 * If connection is half-synchronized 2148 * (ie NEEDSYN flag on) then delay ACK, 2149 * so it may be piggybacked when SYN is sent. 2150 * Otherwise, since we received a FIN then no 2151 * more input can be expected, send ACK now. 2152 */ 2153 if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN)) 2154 callout_reset(tp->tt_delack, tcp_delacktime, 2155 tcp_timer_delack, tp); 2156 else 2157 tp->t_flags |= TF_ACKNOW; 2158 tp->rcv_nxt++; 2159 } 2160 switch (tp->t_state) { 2161 2162 /* 2163 * In SYN_RECEIVED and ESTABLISHED STATES 2164 * enter the CLOSE_WAIT state. 2165 */ 2166 case TCPS_SYN_RECEIVED: 2167 tp->t_starttime = ticks; 2168 /*FALLTHROUGH*/ 2169 case TCPS_ESTABLISHED: 2170 tp->t_state = TCPS_CLOSE_WAIT; 2171 break; 2172 2173 /* 2174 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2175 * enter the CLOSING state. 2176 */ 2177 case TCPS_FIN_WAIT_1: 2178 tp->t_state = TCPS_CLOSING; 2179 break; 2180 2181 /* 2182 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2183 * starting the time-wait timer, turning off the other 2184 * standard timers. 2185 */ 2186 case TCPS_FIN_WAIT_2: 2187 tp->t_state = TCPS_TIME_WAIT; 2188 tcp_canceltimers(tp); 2189 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 2190 if (tp->cc_recv != 0 && 2191 (ticks - tp->t_starttime) < tcp_msl) { 2192 callout_reset(tp->tt_2msl, 2193 tp->t_rxtcur * TCPTV_TWTRUNC, 2194 tcp_timer_2msl, tp); 2195 /* For transaction client, force ACK now. */ 2196 tp->t_flags |= TF_ACKNOW; 2197 } 2198 else 2199 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2200 tcp_timer_2msl, tp); 2201 soisdisconnected(so); 2202 break; 2203 2204 /* 2205 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2206 */ 2207 case TCPS_TIME_WAIT: 2208 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2209 tcp_timer_2msl, tp); 2210 break; 2211 } 2212 } 2213#ifdef TCPDEBUG 2214 if (so->so_options & SO_DEBUG) 2215 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2216 &tcp_savetcp, 0); 2217#endif 2218 2219 /* 2220 * Return any desired output. 2221 */ 2222 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2223 (void) tcp_output(tp); 2224 return; 2225 2226dropafterack: 2227 /* 2228 * Generate an ACK dropping incoming segment if it occupies 2229 * sequence space, where the ACK reflects our state. 2230 * 2231 * We can now skip the test for the RST flag since all 2232 * paths to this code happen after packets containing 2233 * RST have been dropped. 2234 * 2235 * In the SYN-RECEIVED state, don't send an ACK unless the 2236 * segment we received passes the SYN-RECEIVED ACK test. 2237 * If it fails send a RST. This breaks the loop in the 2238 * "LAND" DoS attack, and also prevents an ACK storm 2239 * between two listening ports that have been sent forged 2240 * SYN segments, each with the source address of the other. 2241 */ 2242 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2243 (SEQ_GT(tp->snd_una, th->th_ack) || 2244 SEQ_GT(th->th_ack, tp->snd_max)) ) 2245 goto maybedropwithreset; 2246#ifdef TCPDEBUG 2247 if (so->so_options & SO_DEBUG) 2248 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2249 &tcp_savetcp, 0); 2250#endif 2251 m_freem(m); 2252 tp->t_flags |= TF_ACKNOW; 2253 (void) tcp_output(tp); 2254 return; 2255 2256 2257 /* 2258 * Conditionally drop with reset or just drop depending on whether 2259 * we think we are under attack or not. 2260 */ 2261maybedropwithreset: 2262 if (badport_bandlim(1) < 0) 2263 goto drop; 2264 /* fall through */ 2265dropwithreset: 2266#ifdef TCP_RESTRICT_RST 2267 if (restrict_rst) 2268 goto drop; 2269#endif 2270 /* 2271 * Generate a RST, dropping incoming segment. 2272 * Make ACK acceptable to originator of segment. 2273 * Don't bother to respond if destination was broadcast/multicast. 2274 */ 2275 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2276 goto drop; 2277#ifdef INET6 2278 if (isipv6) { 2279 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2280 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2281 goto drop; 2282 } else 2283#endif /* INET6 */ 2284 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2285 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2286 ip->ip_src.s_addr == htonl(INADDR_BROADCAST)) 2287 goto drop; 2288 /* IPv6 anycast check is done at tcp6_input() */ 2289#ifdef TCPDEBUG 2290 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2291 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2292 &tcp_savetcp, 0); 2293#endif 2294 if (thflags & TH_ACK) 2295 /* mtod() below is safe as long as hdr dropping is delayed */ 2296 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2297 TH_RST); 2298 else { 2299 if (thflags & TH_SYN) 2300 tlen++; 2301 /* mtod() below is safe as long as hdr dropping is delayed */ 2302 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2303 (tcp_seq)0, TH_RST|TH_ACK); 2304 } 2305 /* destroy temporarily created socket */ 2306 if (dropsocket) 2307 (void) soabort(so); 2308 return; 2309 2310drop: 2311 /* 2312 * Drop space held by incoming segment and return. 2313 */ 2314#ifdef TCPDEBUG 2315 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2316 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2317 &tcp_savetcp, 0); 2318#endif 2319 m_freem(m); 2320 /* destroy temporarily created socket */ 2321 if (dropsocket) 2322 (void) soabort(so); 2323 return; 2324} 2325 2326static void 2327tcp_dooptions(tp, cp, cnt, th, to) 2328 struct tcpcb *tp; 2329 u_char *cp; 2330 int cnt; 2331 struct tcphdr *th; 2332 struct tcpopt *to; 2333{ 2334 u_short mss = 0; 2335 int opt, optlen; 2336 2337 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2338 opt = cp[0]; 2339 if (opt == TCPOPT_EOL) 2340 break; 2341 if (opt == TCPOPT_NOP) 2342 optlen = 1; 2343 else { 2344 if (cnt < 2) 2345 break; 2346 optlen = cp[1]; 2347 if (optlen < 2 || optlen > cnt) 2348 break; 2349 } 2350 switch (opt) { 2351 2352 default: 2353 continue; 2354 2355 case TCPOPT_MAXSEG: 2356 if (optlen != TCPOLEN_MAXSEG) 2357 continue; 2358 if (!(th->th_flags & TH_SYN)) 2359 continue; 2360 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 2361 NTOHS(mss); 2362 break; 2363 2364 case TCPOPT_WINDOW: 2365 if (optlen != TCPOLEN_WINDOW) 2366 continue; 2367 if (!(th->th_flags & TH_SYN)) 2368 continue; 2369 tp->t_flags |= TF_RCVD_SCALE; 2370 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2371 break; 2372 2373 case TCPOPT_TIMESTAMP: 2374 if (optlen != TCPOLEN_TIMESTAMP) 2375 continue; 2376 to->to_flag |= TOF_TS; 2377 bcopy((char *)cp + 2, 2378 (char *)&to->to_tsval, sizeof(to->to_tsval)); 2379 NTOHL(to->to_tsval); 2380 bcopy((char *)cp + 6, 2381 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2382 NTOHL(to->to_tsecr); 2383 2384 /* 2385 * A timestamp received in a SYN makes 2386 * it ok to send timestamp requests and replies. 2387 */ 2388 if (th->th_flags & TH_SYN) { 2389 tp->t_flags |= TF_RCVD_TSTMP; 2390 tp->ts_recent = to->to_tsval; 2391 tp->ts_recent_age = ticks; 2392 } 2393 break; 2394 case TCPOPT_CC: 2395 if (optlen != TCPOLEN_CC) 2396 continue; 2397 to->to_flag |= TOF_CC; 2398 bcopy((char *)cp + 2, 2399 (char *)&to->to_cc, sizeof(to->to_cc)); 2400 NTOHL(to->to_cc); 2401 /* 2402 * A CC or CC.new option received in a SYN makes 2403 * it ok to send CC in subsequent segments. 2404 */ 2405 if (th->th_flags & TH_SYN) 2406 tp->t_flags |= TF_RCVD_CC; 2407 break; 2408 case TCPOPT_CCNEW: 2409 if (optlen != TCPOLEN_CC) 2410 continue; 2411 if (!(th->th_flags & TH_SYN)) 2412 continue; 2413 to->to_flag |= TOF_CCNEW; 2414 bcopy((char *)cp + 2, 2415 (char *)&to->to_cc, sizeof(to->to_cc)); 2416 NTOHL(to->to_cc); 2417 /* 2418 * A CC or CC.new option received in a SYN makes 2419 * it ok to send CC in subsequent segments. 2420 */ 2421 tp->t_flags |= TF_RCVD_CC; 2422 break; 2423 case TCPOPT_CCECHO: 2424 if (optlen != TCPOLEN_CC) 2425 continue; 2426 if (!(th->th_flags & TH_SYN)) 2427 continue; 2428 to->to_flag |= TOF_CCECHO; 2429 bcopy((char *)cp + 2, 2430 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2431 NTOHL(to->to_ccecho); 2432 break; 2433 } 2434 } 2435 if (th->th_flags & TH_SYN) 2436 tcp_mss(tp, mss); /* sets t_maxseg */ 2437} 2438 2439/* 2440 * Pull out of band byte out of a segment so 2441 * it doesn't appear in the user's data queue. 2442 * It is still reflected in the segment length for 2443 * sequencing purposes. 2444 */ 2445static void 2446tcp_pulloutofband(so, th, m, off) 2447 struct socket *so; 2448 struct tcphdr *th; 2449 register struct mbuf *m; 2450 int off; /* delayed to be droped hdrlen */ 2451{ 2452 int cnt = off + th->th_urp - 1; 2453 2454 while (cnt >= 0) { 2455 if (m->m_len > cnt) { 2456 char *cp = mtod(m, caddr_t) + cnt; 2457 struct tcpcb *tp = sototcpcb(so); 2458 2459 tp->t_iobc = *cp; 2460 tp->t_oobflags |= TCPOOB_HAVEDATA; 2461 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2462 m->m_len--; 2463 if (m->m_flags & M_PKTHDR) 2464 m->m_pkthdr.len--; 2465 return; 2466 } 2467 cnt -= m->m_len; 2468 m = m->m_next; 2469 if (m == 0) 2470 break; 2471 } 2472 panic("tcp_pulloutofband"); 2473} 2474 2475/* 2476 * Collect new round-trip time estimate 2477 * and update averages and current timeout. 2478 */ 2479static void 2480tcp_xmit_timer(tp, rtt) 2481 register struct tcpcb *tp; 2482 int rtt; 2483{ 2484 register int delta; 2485 2486 tcpstat.tcps_rttupdated++; 2487 tp->t_rttupdated++; 2488 if (tp->t_srtt != 0) { 2489 /* 2490 * srtt is stored as fixed point with 5 bits after the 2491 * binary point (i.e., scaled by 8). The following magic 2492 * is equivalent to the smoothing algorithm in rfc793 with 2493 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2494 * point). Adjust rtt to origin 0. 2495 */ 2496 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2497 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2498 2499 if ((tp->t_srtt += delta) <= 0) 2500 tp->t_srtt = 1; 2501 2502 /* 2503 * We accumulate a smoothed rtt variance (actually, a 2504 * smoothed mean difference), then set the retransmit 2505 * timer to smoothed rtt + 4 times the smoothed variance. 2506 * rttvar is stored as fixed point with 4 bits after the 2507 * binary point (scaled by 16). The following is 2508 * equivalent to rfc793 smoothing with an alpha of .75 2509 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2510 * rfc793's wired-in beta. 2511 */ 2512 if (delta < 0) 2513 delta = -delta; 2514 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2515 if ((tp->t_rttvar += delta) <= 0) 2516 tp->t_rttvar = 1; 2517 } else { 2518 /* 2519 * No rtt measurement yet - use the unsmoothed rtt. 2520 * Set the variance to half the rtt (so our first 2521 * retransmit happens at 3*rtt). 2522 */ 2523 tp->t_srtt = rtt << TCP_RTT_SHIFT; 2524 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2525 } 2526 tp->t_rtttime = 0; 2527 tp->t_rxtshift = 0; 2528 2529 /* 2530 * the retransmit should happen at rtt + 4 * rttvar. 2531 * Because of the way we do the smoothing, srtt and rttvar 2532 * will each average +1/2 tick of bias. When we compute 2533 * the retransmit timer, we want 1/2 tick of rounding and 2534 * 1 extra tick because of +-1/2 tick uncertainty in the 2535 * firing of the timer. The bias will give us exactly the 2536 * 1.5 tick we need. But, because the bias is 2537 * statistical, we have to test that we don't drop below 2538 * the minimum feasible timer (which is 2 ticks). 2539 */ 2540 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 2541 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2542 2543 /* 2544 * We received an ack for a packet that wasn't retransmitted; 2545 * it is probably safe to discard any error indications we've 2546 * received recently. This isn't quite right, but close enough 2547 * for now (a route might have failed after we sent a segment, 2548 * and the return path might not be symmetrical). 2549 */ 2550 tp->t_softerror = 0; 2551} 2552 2553/* 2554 * Determine a reasonable value for maxseg size. 2555 * If the route is known, check route for mtu. 2556 * If none, use an mss that can be handled on the outgoing 2557 * interface without forcing IP to fragment; if bigger than 2558 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2559 * to utilize large mbufs. If no route is found, route has no mtu, 2560 * or the destination isn't local, use a default, hopefully conservative 2561 * size (usually 512 or the default IP max size, but no more than the mtu 2562 * of the interface), as we can't discover anything about intervening 2563 * gateways or networks. We also initialize the congestion/slow start 2564 * window to be a single segment if the destination isn't local. 2565 * While looking at the routing entry, we also initialize other path-dependent 2566 * parameters from pre-set or cached values in the routing entry. 2567 * 2568 * Also take into account the space needed for options that we 2569 * send regularly. Make maxseg shorter by that amount to assure 2570 * that we can send maxseg amount of data even when the options 2571 * are present. Store the upper limit of the length of options plus 2572 * data in maxopd. 2573 * 2574 * NOTE that this routine is only called when we process an incoming 2575 * segment, for outgoing segments only tcp_mssopt is called. 2576 * 2577 * In case of T/TCP, we call this routine during implicit connection 2578 * setup as well (offer = -1), to initialize maxseg from the cached 2579 * MSS of our peer. 2580 */ 2581void 2582tcp_mss(tp, offer) 2583 struct tcpcb *tp; 2584 int offer; 2585{ 2586 register struct rtentry *rt; 2587 struct ifnet *ifp; 2588 register int rtt, mss; 2589 u_long bufsize; 2590 struct inpcb *inp; 2591 struct socket *so; 2592 struct rmxp_tao *taop; 2593 int origoffer = offer; 2594#ifdef INET6 2595 int isipv6; 2596 int min_protoh; 2597#endif 2598 2599 inp = tp->t_inpcb; 2600#ifdef INET6 2601 isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2602 min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2603 : sizeof (struct tcpiphdr); 2604#else 2605#define min_protoh (sizeof (struct tcpiphdr)) 2606#endif 2607#ifdef INET6 2608 if (isipv6) 2609 rt = tcp_rtlookup6(inp); 2610 else 2611#endif 2612 rt = tcp_rtlookup(inp); 2613 if (rt == NULL) { 2614 tp->t_maxopd = tp->t_maxseg = 2615#ifdef INET6 2616 isipv6 ? tcp_v6mssdflt : 2617#endif /* INET6 */ 2618 tcp_mssdflt; 2619 return; 2620 } 2621 ifp = rt->rt_ifp; 2622 so = inp->inp_socket; 2623 2624 taop = rmx_taop(rt->rt_rmx); 2625 /* 2626 * Offer == -1 means that we didn't receive SYN yet, 2627 * use cached value in that case; 2628 */ 2629 if (offer == -1) 2630 offer = taop->tao_mssopt; 2631 /* 2632 * Offer == 0 means that there was no MSS on the SYN segment, 2633 * in this case we use tcp_mssdflt. 2634 */ 2635 if (offer == 0) 2636 offer = 2637#ifdef INET6 2638 isipv6 ? tcp_v6mssdflt : 2639#endif /* INET6 */ 2640 tcp_mssdflt; 2641 else 2642 /* 2643 * Sanity check: make sure that maxopd will be large 2644 * enough to allow some data on segments even is the 2645 * all the option space is used (40bytes). Otherwise 2646 * funny things may happen in tcp_output. 2647 */ 2648 offer = max(offer, 64); 2649 taop->tao_mssopt = offer; 2650 2651 /* 2652 * While we're here, check if there's an initial rtt 2653 * or rttvar. Convert from the route-table units 2654 * to scaled multiples of the slow timeout timer. 2655 */ 2656 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2657 /* 2658 * XXX the lock bit for RTT indicates that the value 2659 * is also a minimum value; this is subject to time. 2660 */ 2661 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2662 tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 2663 tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2664 tcpstat.tcps_usedrtt++; 2665 if (rt->rt_rmx.rmx_rttvar) { 2666 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2667 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2668 tcpstat.tcps_usedrttvar++; 2669 } else { 2670 /* default variation is +- 1 rtt */ 2671 tp->t_rttvar = 2672 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2673 } 2674 TCPT_RANGESET(tp->t_rxtcur, 2675 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2676 tp->t_rttmin, TCPTV_REXMTMAX); 2677 } 2678 /* 2679 * if there's an mtu associated with the route, use it 2680 * else, use the link mtu. 2681 */ 2682 if (rt->rt_rmx.rmx_mtu) 2683 mss = rt->rt_rmx.rmx_mtu - min_protoh; 2684 else 2685 { 2686 mss = 2687#ifdef INET6 2688 (isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu : 2689#endif 2690 ifp->if_mtu 2691#ifdef INET6 2692 ) 2693#endif 2694 - min_protoh; 2695#ifdef INET6 2696 if (isipv6) { 2697 if (!in6_localaddr(&inp->in6p_faddr)) 2698 mss = min(mss, tcp_v6mssdflt); 2699 } else 2700#endif 2701 if (!in_localaddr(inp->inp_faddr)) 2702 mss = min(mss, tcp_mssdflt); 2703 } 2704 mss = min(mss, offer); 2705 /* 2706 * maxopd stores the maximum length of data AND options 2707 * in a segment; maxseg is the amount of data in a normal 2708 * segment. We need to store this value (maxopd) apart 2709 * from maxseg, because now every segment carries options 2710 * and thus we normally have somewhat less data in segments. 2711 */ 2712 tp->t_maxopd = mss; 2713 2714 /* 2715 * In case of T/TCP, origoffer==-1 indicates, that no segments 2716 * were received yet. In this case we just guess, otherwise 2717 * we do the same as before T/TCP. 2718 */ 2719 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2720 (origoffer == -1 || 2721 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2722 mss -= TCPOLEN_TSTAMP_APPA; 2723 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2724 (origoffer == -1 || 2725 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2726 mss -= TCPOLEN_CC_APPA; 2727 2728#if (MCLBYTES & (MCLBYTES - 1)) == 0 2729 if (mss > MCLBYTES) 2730 mss &= ~(MCLBYTES-1); 2731#else 2732 if (mss > MCLBYTES) 2733 mss = mss / MCLBYTES * MCLBYTES; 2734#endif 2735 /* 2736 * If there's a pipesize, change the socket buffer 2737 * to that size. Make the socket buffers an integral 2738 * number of mss units; if the mss is larger than 2739 * the socket buffer, decrease the mss. 2740 */ 2741#ifdef RTV_SPIPE 2742 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2743#endif 2744 bufsize = so->so_snd.sb_hiwat; 2745 if (bufsize < mss) 2746 mss = bufsize; 2747 else { 2748 bufsize = roundup(bufsize, mss); 2749 if (bufsize > sb_max) 2750 bufsize = sb_max; 2751 (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2752 } 2753 tp->t_maxseg = mss; 2754 2755#ifdef RTV_RPIPE 2756 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2757#endif 2758 bufsize = so->so_rcv.sb_hiwat; 2759 if (bufsize > mss) { 2760 bufsize = roundup(bufsize, mss); 2761 if (bufsize > sb_max) 2762 bufsize = sb_max; 2763 (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2764 } 2765 2766 /* 2767 * Set the slow-start flight size depending on whether this 2768 * is a local network or not. 2769 */ 2770 if ( 2771#ifdef INET6 2772 (isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2773 (!isipv6 && 2774#endif 2775 in_localaddr(inp->inp_faddr) 2776#ifdef INET6 2777 ) 2778#endif 2779 ) 2780 tp->snd_cwnd = mss * ss_fltsz_local; 2781 else 2782 tp->snd_cwnd = mss * ss_fltsz; 2783 2784 if (rt->rt_rmx.rmx_ssthresh) { 2785 /* 2786 * There's some sort of gateway or interface 2787 * buffer limit on the path. Use this to set 2788 * the slow start threshhold, but set the 2789 * threshold to no less than 2*mss. 2790 */ 2791 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2792 tcpstat.tcps_usedssthresh++; 2793 } 2794} 2795 2796/* 2797 * Determine the MSS option to send on an outgoing SYN. 2798 */ 2799int 2800tcp_mssopt(tp) 2801 struct tcpcb *tp; 2802{ 2803 struct rtentry *rt; 2804#ifdef INET6 2805 int isipv6; 2806 int min_protoh; 2807#endif 2808 2809#ifdef INET6 2810 isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2811 min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2812 : sizeof (struct tcpiphdr); 2813#else 2814#define min_protoh (sizeof (struct tcpiphdr)) 2815#endif 2816#ifdef INET6 2817 if (isipv6) 2818 rt = tcp_rtlookup6(tp->t_inpcb); 2819 else 2820#endif /* INET6 */ 2821 rt = tcp_rtlookup(tp->t_inpcb); 2822 if (rt == NULL) 2823 return 2824#ifdef INET6 2825 isipv6 ? tcp_v6mssdflt : 2826#endif /* INET6 */ 2827 tcp_mssdflt; 2828 2829 return rt->rt_ifp->if_mtu - min_protoh; 2830} 2831 2832 2833/* 2834 * Checks for partial ack. If partial ack arrives, force the retransmission 2835 * of the next unacknowledged segment, do not clear tp->t_dupacks, and return 2836 * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to 2837 * be started again. If the ack advances at least to tp->snd_recover, return 0. 2838 */ 2839static int 2840tcp_newreno(tp, th) 2841 struct tcpcb *tp; 2842 struct tcphdr *th; 2843{ 2844 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 2845 tcp_seq onxt = tp->snd_nxt; 2846 u_long ocwnd = tp->snd_cwnd; 2847 2848 callout_stop(tp->tt_rexmt); 2849 tp->t_rtttime = 0; 2850 tp->snd_nxt = th->th_ack; 2851 /* 2852 * Set snd_cwnd to one segment beyond acknowledged offset 2853 * (tp->snd_una has not yet been updated when this function 2854 * is called) 2855 */ 2856 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 2857 (void) tcp_output(tp); 2858 tp->snd_cwnd = ocwnd; 2859 if (SEQ_GT(onxt, tp->snd_nxt)) 2860 tp->snd_nxt = onxt; 2861 /* 2862 * Partial window deflation. Relies on fact that tp->snd_una 2863 * not updated yet. 2864 */ 2865 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 2866 return (1); 2867 } 2868 return (0); 2869} 2870