tcp_input.c revision 130811
1118611Snjl/* 2118611Snjl * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3245582Sjkim * The Regents of the University of California. All rights reserved. 4118611Snjl * 5118611Snjl * Redistribution and use in source and binary forms, with or without 6118611Snjl * modification, are permitted provided that the following conditions 7217365Sjkim * are met: 8245582Sjkim * 1. Redistributions of source code must retain the above copyright 9118611Snjl * notice, this list of conditions and the following disclaimer. 10118611Snjl * 2. Redistributions in binary form must reproduce the above copyright 11217365Sjkim * notice, this list of conditions and the following disclaimer in the 12217365Sjkim * documentation and/or other materials provided with the distribution. 13217365Sjkim * 4. Neither the name of the University nor the names of its contributors 14217365Sjkim * may be used to endorse or promote products derived from this software 15217365Sjkim * without specific prior written permission. 16217365Sjkim * 17217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18217365Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19217365Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20217365Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21217365Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25118611Snjl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26217365Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27217365Sjkim * SUCH DAMAGE. 28217365Sjkim * 29118611Snjl * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 30217365Sjkim * $FreeBSD: head/sys/netinet/tcp_input.c 130811 2004-06-20 20:17:29Z rwatson $ 31217365Sjkim */ 32217365Sjkim 33217365Sjkim#include "opt_ipfw.h" /* for ipfw_fwd */ 34217365Sjkim#include "opt_inet.h" 35217365Sjkim#include "opt_inet6.h" 36217365Sjkim#include "opt_ipsec.h" 37217365Sjkim#include "opt_mac.h" 38217365Sjkim#include "opt_tcpdebug.h" 39217365Sjkim#include "opt_tcp_input.h" 40217365Sjkim 41217365Sjkim#include <sys/param.h> 42217365Sjkim#include <sys/kernel.h> 43118611Snjl#include <sys/mac.h> 44151937Sjkim#include <sys/malloc.h> 45193529Sjkim#include <sys/mbuf.h> 46118611Snjl#include <sys/proc.h> /* for proc0 declaration */ 47118611Snjl#include <sys/protosw.h> 48118611Snjl#include <sys/signalvar.h> 49118611Snjl#include <sys/socket.h> 50151937Sjkim#include <sys/socketvar.h> 51118611Snjl#include <sys/sysctl.h> 52233250Sjkim#include <sys/syslog.h> 53197104Sjkim#include <sys/systm.h> 54197104Sjkim 55197104Sjkim#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 56151937Sjkim 57197104Sjkim#include <vm/uma.h> 58151937Sjkim 59151937Sjkim#include <net/if.h> 60151937Sjkim#include <net/route.h> 61151937Sjkim 62151937Sjkim#include <netinet/in.h> 63151937Sjkim#include <netinet/in_pcb.h> 64151937Sjkim#include <netinet/in_systm.h> 65118611Snjl#include <netinet/in_var.h> 66118611Snjl#include <netinet/ip.h> 67118611Snjl#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 68118611Snjl#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 69118611Snjl#include <netinet/ip_var.h> 70118611Snjl#include <netinet/ip6.h> 71118611Snjl#include <netinet/icmp6.h> 72118611Snjl#include <netinet6/in6_pcb.h> 73118611Snjl#include <netinet6/ip6_var.h> 74118611Snjl#include <netinet6/nd6.h> 75118611Snjl#include <netinet/tcp.h> 76118611Snjl#include <netinet/tcp_fsm.h> 77118611Snjl#include <netinet/tcp_seq.h> 78118611Snjl#include <netinet/tcp_timer.h> 79234623Sjkim#include <netinet/tcp_var.h> 80118611Snjl#include <netinet6/tcp6_var.h> 81118611Snjl#include <netinet/tcpip.h> 82234623Sjkim#ifdef TCPDEBUG 83234623Sjkim#include <netinet/tcp_debug.h> 84234623Sjkim#endif /* TCPDEBUG */ 85234623Sjkim 86234623Sjkim#ifdef FAST_IPSEC 87118611Snjl#include <netipsec/ipsec.h> 88118611Snjl#include <netipsec/ipsec6.h> 89118611Snjl#endif /*FAST_IPSEC*/ 90118611Snjl 91118611Snjl#ifdef IPSEC 92234623Sjkim#include <netinet6/ipsec.h> 93234623Sjkim#include <netinet6/ipsec6.h> 94234623Sjkim#include <netkey/key.h> 95234623Sjkim#endif /*IPSEC*/ 96234623Sjkim 97234623Sjkim#include <machine/in_cksum.h> 98234623Sjkim 99234623Sjkimstatic const int tcprexmtthresh = 3; 100234623Sjkimtcp_cc tcp_ccgen; 101234623Sjkim 102234623Sjkimstruct tcpstat tcpstat; 103234623SjkimSYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 104234623Sjkim &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 105234623Sjkim 106234623Sjkimstatic int log_in_vain = 0; 107234623SjkimSYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 108234623Sjkim &log_in_vain, 0, "Log all incoming TCP connections"); 109234623Sjkim 110234623Sjkimstatic int blackhole = 0; 111234623SjkimSYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 112234623Sjkim &blackhole, 0, "Do not send RST when dropping refused connections"); 113234623Sjkim 114234623Sjkimint tcp_delack_enabled = 1; 115234623SjkimSYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 116197104Sjkim &tcp_delack_enabled, 0, 117197104Sjkim "Delay ACK to try and piggyback it onto a data packet"); 118197104Sjkim 119197104Sjkim#ifdef TCP_DROP_SYNFIN 120197104Sjkimstatic int drop_synfin = 0; 121197104SjkimSYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 122197104Sjkim &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 123197104Sjkim#endif 124197104Sjkim 125197104Sjkimstatic int tcp_do_rfc3042 = 1; 126197104SjkimSYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 127197104Sjkim &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 128197104Sjkim 129197104Sjkimstatic int tcp_do_rfc3390 = 1; 130197104SjkimSYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 131197104Sjkim &tcp_do_rfc3390, 0, 132197104Sjkim "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 133197104Sjkim 134197104SjkimSYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, 135197104Sjkim "TCP Segment Reassembly Queue"); 136197104Sjkim 137197104Sjkimstatic int tcp_reass_maxseg = 0; 138197104SjkimSYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN, 139197104Sjkim &tcp_reass_maxseg, 0, 140197104Sjkim "Global maximum number of TCP Segments in Reassembly Queue"); 141197104Sjkim 142197104Sjkimint tcp_reass_qsize = 0; 143197104SjkimSYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD, 144197104Sjkim &tcp_reass_qsize, 0, 145197104Sjkim "Global number of TCP Segments currently in Reassembly Queue"); 146197104Sjkim 147197104Sjkimstatic int tcp_reass_maxqlen = 48; 148197104SjkimSYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW, 149197104Sjkim &tcp_reass_maxqlen, 0, 150197104Sjkim "Maximum number of TCP Segments per individual Reassembly Queue"); 151197104Sjkim 152197104Sjkimstatic int tcp_reass_overflows = 0; 153197104SjkimSYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD, 154197104Sjkim &tcp_reass_overflows, 0, 155197104Sjkim "Global number of TCP Segment Reassembly Queue Overflows"); 156197104Sjkim 157197104Sjkimstruct inpcbhead tcb; 158197104Sjkim#define tcb6 tcb /* for KAME src sync over BSD*'s */ 159197104Sjkimstruct inpcbinfo tcbinfo; 160197104Sjkimstruct mtx *tcbinfo_mtx; 161197104Sjkim 162197104Sjkimstatic void tcp_dooptions(struct tcpopt *, u_char *, int, int); 163197104Sjkimstatic void tcp_pulloutofband(struct socket *, 164197104Sjkim struct tcphdr *, struct mbuf *, int); 165197104Sjkimstatic int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 166197104Sjkim struct mbuf *); 167197104Sjkimstatic void tcp_xmit_timer(struct tcpcb *, int); 168197104Sjkimstatic void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 169197104Sjkimstatic int tcp_timewait(struct tcptw *, struct tcpopt *, 170197104Sjkim struct tcphdr *, struct mbuf *, int); 171197104Sjkim 172197104Sjkim/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 173197104Sjkim#ifdef INET6 174197104Sjkim#define ND6_HINT(tp) \ 175197104Sjkimdo { \ 176197104Sjkim if ((tp) && (tp)->t_inpcb && \ 177197104Sjkim ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 178197104Sjkim nd6_nud_hint(NULL, NULL, 0); \ 179197104Sjkim} while (0) 180197104Sjkim#else 181197104Sjkim#define ND6_HINT(tp) 182197104Sjkim#endif 183235945Sjkim 184235945Sjkim/* 185235945Sjkim * Indicate whether this ack should be delayed. We can delay the ack if 186235945Sjkim * - there is no delayed ack timer in progress and 187235945Sjkim * - our last ack wasn't a 0-sized window. We never want to delay 188235945Sjkim * the ack that opens up a 0-sized window and 189235945Sjkim * - delayed acks are enabled or 190235945Sjkim * - this is a half-synchronized T/TCP connection. 191235945Sjkim */ 192235945Sjkim#define DELAY_ACK(tp) \ 193235945Sjkim ((!callout_active(tp->tt_delack) && \ 194235945Sjkim (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 195235945Sjkim (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 196235945Sjkim 197235945Sjkim/* Initialize TCP reassembly queue */ 198235945Sjkimuma_zone_t tcp_reass_zone; 199235945Sjkimvoid 200235945Sjkimtcp_reass_init() 201235945Sjkim{ 202235945Sjkim tcp_reass_maxseg = nmbclusters / 16; 203235945Sjkim TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 204235945Sjkim &tcp_reass_maxseg); 205235945Sjkim tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 206235945Sjkim NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 207235945Sjkim uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg); 208235945Sjkim} 209235945Sjkim 210235945Sjkimstatic int 211235945Sjkimtcp_reass(tp, th, tlenp, m) 212235945Sjkim register struct tcpcb *tp; 213235945Sjkim register struct tcphdr *th; 214235945Sjkim int *tlenp; 215235945Sjkim struct mbuf *m; 216235945Sjkim{ 217235945Sjkim struct tseg_qent *q; 218235945Sjkim struct tseg_qent *p = NULL; 219235945Sjkim struct tseg_qent *nq; 220235945Sjkim struct tseg_qent *te = NULL; 221235945Sjkim struct socket *so = tp->t_inpcb->inp_socket; 222235945Sjkim int flags; 223235945Sjkim 224235945Sjkim /* 225235945Sjkim * XXX: tcp_reass() is rather inefficient with its data structures 226235945Sjkim * and should be rewritten (see NetBSD for optimizations). While 227235945Sjkim * doing that it should move to its own file tcp_reass.c. 228235945Sjkim */ 229235945Sjkim 230235945Sjkim /* 231235945Sjkim * Call with th==0 after become established to 232235945Sjkim * force pre-ESTABLISHED data up to user socket. 233235945Sjkim */ 234235945Sjkim if (th == 0) 235235945Sjkim goto present; 236235945Sjkim 237235945Sjkim /* 238235945Sjkim * Limit the number of segments in the reassembly queue to prevent 239235945Sjkim * holding on to too many segments (and thus running out of mbufs). 240235945Sjkim * Make sure to let the missing segment through which caused this 241235945Sjkim * queue. Always keep one global queue entry spare to be able to 242235945Sjkim * process the missing segment. 243235945Sjkim */ 244235945Sjkim if (th->th_seq != tp->rcv_nxt && 245235945Sjkim (tcp_reass_qsize + 1 >= tcp_reass_maxseg || 246235945Sjkim tp->t_segqlen >= tcp_reass_maxqlen)) { 247235945Sjkim tcp_reass_overflows++; 248235945Sjkim tcpstat.tcps_rcvmemdrop++; 249235945Sjkim m_freem(m); 250235945Sjkim return (0); 251235945Sjkim } 252235945Sjkim 253235945Sjkim /* 254235945Sjkim * Allocate a new queue entry. If we can't, or hit the zone limit 255235945Sjkim * just drop the pkt. 256235945Sjkim */ 257235945Sjkim te = uma_zalloc(tcp_reass_zone, M_NOWAIT); 258235945Sjkim if (te == NULL) { 259235945Sjkim tcpstat.tcps_rcvmemdrop++; 260235945Sjkim m_freem(m); 261235945Sjkim return (0); 262235945Sjkim } 263235945Sjkim tp->t_segqlen++; 264235945Sjkim tcp_reass_qsize++; 265235945Sjkim 266235945Sjkim /* 267235945Sjkim * Find a segment which begins after this one does. 268235945Sjkim */ 269235945Sjkim LIST_FOREACH(q, &tp->t_segq, tqe_q) { 270235945Sjkim if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 271235945Sjkim break; 272235945Sjkim p = q; 273235945Sjkim } 274235945Sjkim 275235945Sjkim /* 276235945Sjkim * If there is a preceding segment, it may provide some of 277235945Sjkim * our data already. If so, drop the data from the incoming 278235945Sjkim * segment. If it provides all of our data, drop us. 279235945Sjkim */ 280235945Sjkim if (p != NULL) { 281235945Sjkim register int i; 282235945Sjkim /* conversion to int (in i) handles seq wraparound */ 283235945Sjkim i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 284197104Sjkim if (i > 0) { 285197104Sjkim if (i >= *tlenp) { 286197104Sjkim tcpstat.tcps_rcvduppack++; 287197104Sjkim tcpstat.tcps_rcvdupbyte += *tlenp; 288197104Sjkim m_freem(m); 289197104Sjkim uma_zfree(tcp_reass_zone, te); 290197104Sjkim tp->t_segqlen--; 291197104Sjkim tcp_reass_qsize--; 292197104Sjkim /* 293197104Sjkim * Try to present any queued data 294197104Sjkim * at the left window edge to the user. 295197104Sjkim * This is needed after the 3-WHS 296233250Sjkim * completes. 297197104Sjkim */ 298197104Sjkim goto present; /* ??? */ 299197104Sjkim } 300197104Sjkim m_adj(m, i); 301197104Sjkim *tlenp -= i; 302197104Sjkim th->th_seq += i; 303197104Sjkim } 304197104Sjkim } 305197104Sjkim tcpstat.tcps_rcvoopack++; 306197104Sjkim tcpstat.tcps_rcvoobyte += *tlenp; 307235945Sjkim 308197104Sjkim /* 309235945Sjkim * While we overlap succeeding segments trim them or, 310197104Sjkim * if they are completely covered, dequeue them. 311197104Sjkim */ 312197104Sjkim while (q) { 313197104Sjkim register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 314197104Sjkim if (i <= 0) 315243347Sjkim break; 316197104Sjkim if (i < q->tqe_len) { 317243347Sjkim q->tqe_th->th_seq += i; 318243347Sjkim q->tqe_len -= i; 319243347Sjkim m_adj(q->tqe_m, i); 320197104Sjkim break; 321197104Sjkim } 322243347Sjkim 323243347Sjkim nq = LIST_NEXT(q, tqe_q); 324243347Sjkim LIST_REMOVE(q, tqe_q); 325243347Sjkim m_freem(q->tqe_m); 326197104Sjkim uma_zfree(tcp_reass_zone, q); 327197104Sjkim tp->t_segqlen--; 328197104Sjkim tcp_reass_qsize--; 329197104Sjkim q = nq; 330197104Sjkim } 331118611Snjl 332118611Snjl /* Insert the new segment queue entry into place. */ 333118611Snjl te->tqe_m = m; 334118611Snjl te->tqe_th = th; 335118611Snjl te->tqe_len = *tlenp; 336118611Snjl 337118611Snjl if (p == NULL) { 338118611Snjl LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 339118611Snjl } else { 340118611Snjl LIST_INSERT_AFTER(p, te, tqe_q); 341118611Snjl } 342118611Snjl 343118611Snjlpresent: 344118611Snjl /* 345197104Sjkim * Present data to user, advancing rcv_nxt through 346197104Sjkim * completed sequence space. 347118611Snjl */ 348118611Snjl if (!TCPS_HAVEESTABLISHED(tp->t_state)) 349118611Snjl return (0); 350118611Snjl q = LIST_FIRST(&tp->t_segq); 351118611Snjl if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 352118611Snjl return (0); 353118611Snjl do { 354118611Snjl tp->rcv_nxt += q->tqe_len; 355118611Snjl flags = q->tqe_th->th_flags & TH_FIN; 356118611Snjl nq = LIST_NEXT(q, tqe_q); 357118611Snjl LIST_REMOVE(q, tqe_q); 358118611Snjl if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 359118611Snjl m_freem(q->tqe_m); 360118611Snjl else 361118611Snjl sbappendstream(&so->so_rcv, q->tqe_m); 362118611Snjl uma_zfree(tcp_reass_zone, q); 363118611Snjl tp->t_segqlen--; 364118611Snjl tcp_reass_qsize--; 365234623Sjkim q = nq; 366118611Snjl } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 367118611Snjl ND6_HINT(tp); 368118611Snjl sorwakeup(so); 369118611Snjl return (flags); 370197104Sjkim} 371197104Sjkim 372197104Sjkim/* 373197104Sjkim * TCP input routine, follows pages 65-76 of the 374197104Sjkim * protocol specification dated September, 1981 very closely. 375197104Sjkim */ 376197104Sjkim#ifdef INET6 377118611Snjlint 378197104Sjkimtcp6_input(mp, offp, proto) 379197104Sjkim struct mbuf **mp; 380197104Sjkim int *offp, proto; 381197104Sjkim{ 382197104Sjkim register struct mbuf *m = *mp; 383118611Snjl struct in6_ifaddr *ia6; 384118611Snjl 385118611Snjl IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 386197104Sjkim 387197104Sjkim /* 388197104Sjkim * draft-itojun-ipv6-tcp-to-anycast 389197104Sjkim * better place to put this in? 390197104Sjkim */ 391197104Sjkim ia6 = ip6_getdstifaddr(m); 392197104Sjkim if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 393197104Sjkim struct ip6_hdr *ip6; 394197104Sjkim 395197104Sjkim ip6 = mtod(m, struct ip6_hdr *); 396197104Sjkim icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 397197104Sjkim (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 398197104Sjkim return IPPROTO_DONE; 399118611Snjl } 400197104Sjkim 401197104Sjkim tcp_input(m, *offp); 402197104Sjkim return IPPROTO_DONE; 403197104Sjkim} 404197104Sjkim#endif 405197104Sjkim 406197104Sjkimvoid 407197104Sjkimtcp_input(m, off0) 408197104Sjkim register struct mbuf *m; 409197104Sjkim int off0; 410197104Sjkim{ 411197104Sjkim register struct tcphdr *th; 412197104Sjkim register struct ip *ip = NULL; 413197104Sjkim register struct ipovly *ipov; 414197104Sjkim register struct inpcb *inp = NULL; 415197104Sjkim u_char *optp = NULL; 416197104Sjkim int optlen = 0; 417197104Sjkim int len, tlen, off; 418197104Sjkim int drop_hdrlen; 419197104Sjkim register struct tcpcb *tp = 0; 420197104Sjkim register int thflags; 421118611Snjl struct socket *so = 0; 422118611Snjl int todrop, acked, ourfinisacked, needoutput = 0; 423118611Snjl u_long tiwin; 424118611Snjl struct tcpopt to; /* options in this segment */ 425118611Snjl struct rmxp_tao tao; /* our TAO cache entry */ 426118611Snjl int headlocked = 0; 427118611Snjl struct sockaddr_in *next_hop = NULL; 428118611Snjl int rstreason; /* For badport_bandlim accounting purposes */ 429118611Snjl 430118611Snjl struct ip6_hdr *ip6 = NULL; 431118611Snjl#ifdef INET6 432118611Snjl int isipv6; 433118611Snjl#else 434118611Snjl const int isipv6 = 0; 435118611Snjl#endif 436118611Snjl 437118611Snjl#ifdef TCPDEBUG 438118611Snjl /* 439118611Snjl * The size of tcp_saveipgen must be the size of the max ip header, 440118611Snjl * now IPv6. 441118611Snjl */ 442118611Snjl u_char tcp_saveipgen[40]; 443118611Snjl struct tcphdr tcp_savetcp; 444118611Snjl short ostate = 0; 445118611Snjl#endif 446237412Sjkim 447118611Snjl /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ 448118611Snjl next_hop = m_claim_next(m, PACKET_TAG_IPFORWARD); 449118611Snjl#ifdef INET6 450118611Snjl isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 451118611Snjl#endif 452118611Snjl bzero(&tao, sizeof(tao)); 453118611Snjl bzero((char *)&to, sizeof(to)); 454118611Snjl 455118611Snjl tcpstat.tcps_rcvtotal++; 456118611Snjl 457118611Snjl if (isipv6) { 458118611Snjl#ifdef INET6 459118611Snjl /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 460118611Snjl ip6 = mtod(m, struct ip6_hdr *); 461241973Sjkim tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 462118611Snjl if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 463118611Snjl tcpstat.tcps_rcvbadsum++; 464118611Snjl goto drop; 465118611Snjl } 466118611Snjl th = (struct tcphdr *)((caddr_t)ip6 + off0); 467118611Snjl 468118611Snjl /* 469118611Snjl * Be proactive about unspecified IPv6 address in source. 470118611Snjl * As we use all-zero to indicate unbounded/unconnected pcb, 471118611Snjl * unspecified IPv6 address can be used to confuse us. 472118611Snjl * 473118611Snjl * Note that packets with unspecified IPv6 destination is 474118611Snjl * already dropped in ip6_input. 475118611Snjl */ 476118611Snjl if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 477118611Snjl /* XXX stat */ 478118611Snjl goto drop; 479118611Snjl } 480118611Snjl#else 481118611Snjl th = NULL; /* XXX: avoid compiler warning */ 482118611Snjl#endif 483151937Sjkim } else { 484151937Sjkim /* 485118611Snjl * Get IP and TCP header together in first mbuf. 486118611Snjl * Note: IP leaves IP header in first mbuf. 487118611Snjl */ 488118611Snjl if (off0 > sizeof (struct ip)) { 489118611Snjl ip_stripoptions(m, (struct mbuf *)0); 490118611Snjl off0 = sizeof(struct ip); 491118611Snjl } 492118611Snjl if (m->m_len < sizeof (struct tcpiphdr)) { 493118611Snjl if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 494118611Snjl tcpstat.tcps_rcvshort++; 495118611Snjl return; 496118611Snjl } 497118611Snjl } 498118611Snjl ip = mtod(m, struct ip *); 499118611Snjl ipov = (struct ipovly *)ip; 500118611Snjl th = (struct tcphdr *)((caddr_t)ip + off0); 501118611Snjl tlen = ip->ip_len; 502118611Snjl 503118611Snjl if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 504118611Snjl if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 505118611Snjl th->th_sum = m->m_pkthdr.csum_data; 506118611Snjl else 507118611Snjl th->th_sum = in_pseudo(ip->ip_src.s_addr, 508118611Snjl ip->ip_dst.s_addr, 509118611Snjl htonl(m->m_pkthdr.csum_data + 510118611Snjl ip->ip_len + 511118611Snjl IPPROTO_TCP)); 512118611Snjl th->th_sum ^= 0xffff; 513118611Snjl#ifdef TCPDEBUG 514118611Snjl ipov->ih_len = (u_short)tlen; 515118611Snjl ipov->ih_len = htons(ipov->ih_len); 516246849Sjkim#endif 517246849Sjkim } else { 518246849Sjkim /* 519246849Sjkim * Checksum extended TCP header and data. 520246849Sjkim */ 521246849Sjkim len = sizeof (struct ip) + tlen; 522246849Sjkim bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 523209746Sjkim ipov->ih_len = (u_short)tlen; 524209746Sjkim ipov->ih_len = htons(ipov->ih_len); 525209746Sjkim th->th_sum = in_cksum(m, len); 526209746Sjkim } 527209746Sjkim if (th->th_sum) { 528209746Sjkim tcpstat.tcps_rcvbadsum++; 529209746Sjkim goto drop; 530209746Sjkim } 531209746Sjkim#ifdef INET6 532209746Sjkim /* Re-initialization for later version check */ 533209746Sjkim ip->ip_v = IPVERSION; 534209746Sjkim#endif 535209746Sjkim } 536209746Sjkim 537237412Sjkim /* 538209746Sjkim * Check that TCP offset makes sense, 539209746Sjkim * pull out TCP options and adjust length. XXX 540209746Sjkim */ 541209746Sjkim off = th->th_off << 2; 542209746Sjkim if (off < sizeof (struct tcphdr) || off > tlen) { 543209746Sjkim tcpstat.tcps_rcvbadoff++; 544209746Sjkim goto drop; 545209746Sjkim } 546209746Sjkim tlen -= off; /* tlen is used instead of ti->ti_len */ 547209746Sjkim if (off > sizeof (struct tcphdr)) { 548209746Sjkim if (isipv6) { 549209746Sjkim#ifdef INET6 550209746Sjkim IP6_EXTHDR_CHECK(m, off0, off, ); 551209746Sjkim ip6 = mtod(m, struct ip6_hdr *); 552209746Sjkim th = (struct tcphdr *)((caddr_t)ip6 + off0); 553209746Sjkim#endif 554209746Sjkim } else { 555209746Sjkim if (m->m_len < sizeof(struct ip) + off) { 556209746Sjkim if ((m = m_pullup(m, sizeof (struct ip) + off)) 557209746Sjkim == 0) { 558209746Sjkim tcpstat.tcps_rcvshort++; 559209746Sjkim return; 560209746Sjkim } 561209746Sjkim ip = mtod(m, struct ip *); 562209746Sjkim ipov = (struct ipovly *)ip; 563233250Sjkim th = (struct tcphdr *)((caddr_t)ip + off0); 564233250Sjkim } 565233250Sjkim } 566233250Sjkim optlen = off - sizeof (struct tcphdr); 567233250Sjkim optp = (u_char *)(th + 1); 568233250Sjkim } 569233250Sjkim thflags = th->th_flags; 570209746Sjkim 571209746Sjkim#ifdef TCP_DROP_SYNFIN 572209746Sjkim /* 573209746Sjkim * If the drop_synfin option is enabled, drop all packets with 574217365Sjkim * both the SYN and FIN bits set. This prevents e.g. nmap from 575217365Sjkim * identifying the TCP/IP stack. 576217365Sjkim * 577217365Sjkim * This is a violation of the TCP specification. 578217365Sjkim */ 579217365Sjkim if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 580217365Sjkim goto drop; 581217365Sjkim#endif 582217365Sjkim 583217365Sjkim /* 584217365Sjkim * Convert TCP protocol specific fields to host format. 585217365Sjkim */ 586217365Sjkim th->th_seq = ntohl(th->th_seq); 587217365Sjkim th->th_ack = ntohl(th->th_ack); 588237412Sjkim th->th_win = ntohs(th->th_win); 589217365Sjkim th->th_urp = ntohs(th->th_urp); 590217365Sjkim 591217365Sjkim /* 592217365Sjkim * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 593217365Sjkim * until after ip6_savecontrol() is called and before other functions 594234623Sjkim * which don't want those proto headers. 595233250Sjkim * Because ip6_savecontrol() is going to parse the mbuf to 596234623Sjkim * search for data to be passed up to user-land, it wants mbuf 597233250Sjkim * parameters to be unchanged. 598234623Sjkim * XXX: the call of ip6_savecontrol() has been obsoleted based on 599234623Sjkim * latest version of the advanced API (20020110). 600234623Sjkim */ 601234623Sjkim drop_hdrlen = off0 + off; 602234623Sjkim 603234623Sjkim /* 604234623Sjkim * Locate pcb for segment. 605234623Sjkim */ 606237412Sjkim INP_INFO_WLOCK(&tcbinfo); 607233250Sjkim headlocked = 1; 608233250Sjkimfindpcb: 609233250Sjkim /* IPFIREWALL_FORWARD section */ 610233250Sjkim if (next_hop != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 611209746Sjkim /* 612209746Sjkim * Transparently forwarded. Pretend to be the destination. 613209746Sjkim * already got one like this? 614209746Sjkim */ 615209746Sjkim inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 616243347Sjkim ip->ip_dst, th->th_dport, 617118611Snjl 0, m->m_pkthdr.rcvif); 618118611Snjl if (!inp) { 619118611Snjl /* It's new. Try find the ambushing socket. */ 620118611Snjl inp = in_pcblookup_hash(&tcbinfo, 621151937Sjkim ip->ip_src, th->th_sport, 622151937Sjkim next_hop->sin_addr, 623118611Snjl next_hop->sin_port ? 624118611Snjl ntohs(next_hop->sin_port) : 625118611Snjl th->th_dport, 626118611Snjl 1, m->m_pkthdr.rcvif); 627118611Snjl } 628118611Snjl } else { 629118611Snjl if (isipv6) { 630118611Snjl#ifdef INET6 631118611Snjl inp = in6_pcblookup_hash(&tcbinfo, 632118611Snjl &ip6->ip6_src, th->th_sport, 633233250Sjkim &ip6->ip6_dst, th->th_dport, 634233250Sjkim 1, m->m_pkthdr.rcvif); 635233250Sjkim#endif 636233250Sjkim } else 637118611Snjl inp = in_pcblookup_hash(&tcbinfo, 638118611Snjl ip->ip_src, th->th_sport, 639118611Snjl ip->ip_dst, th->th_dport, 640118611Snjl 1, m->m_pkthdr.rcvif); 641118611Snjl } 642118611Snjl 643118611Snjl#if defined(IPSEC) || defined(FAST_IPSEC) 644151937Sjkim#ifdef INET6 645151937Sjkim if (isipv6) { 646118611Snjl if (inp != NULL && ipsec6_in_reject(m, inp)) { 647118611Snjl#ifdef IPSEC 648118611Snjl ipsec6stat.in_polvio++; 649118611Snjl#endif 650118611Snjl goto drop; 651237412Sjkim } 652118611Snjl } else 653118611Snjl#endif /* INET6 */ 654118611Snjl if (inp != NULL && ipsec4_in_reject(m, inp)) { 655118611Snjl#ifdef IPSEC 656118611Snjl ipsecstat.in_polvio++; 657118611Snjl#endif 658118611Snjl goto drop; 659118611Snjl } 660118611Snjl#endif /*IPSEC || FAST_IPSEC*/ 661118611Snjl 662118611Snjl /* 663118611Snjl * If the state is CLOSED (i.e., TCB does not exist) then 664151937Sjkim * all data in the incoming segment is discarded. 665151937Sjkim * If the TCB exists but is in CLOSED state, it is embryonic, 666118611Snjl * but should either do a listen or a connect soon. 667118611Snjl */ 668118611Snjl if (inp == NULL) { 669118611Snjl if (log_in_vain) { 670118611Snjl#ifdef INET6 671237412Sjkim char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 672118611Snjl#else 673118611Snjl char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 674118611Snjl#endif 675118611Snjl 676118611Snjl if (isipv6) { 677118611Snjl#ifdef INET6 678118611Snjl strcpy(dbuf, "["); 679118611Snjl strcpy(sbuf, "["); 680118611Snjl strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 681118611Snjl strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 682118611Snjl strcat(dbuf, "]"); 683118611Snjl strcat(sbuf, "]"); 684118611Snjl#endif 685151937Sjkim } else { 686151937Sjkim strcpy(dbuf, inet_ntoa(ip->ip_dst)); 687118611Snjl strcpy(sbuf, inet_ntoa(ip->ip_src)); 688118611Snjl } 689118611Snjl switch (log_in_vain) { 690118611Snjl case 1: 691118611Snjl if ((thflags & TH_SYN) == 0) 692237412Sjkim break; 693118611Snjl /* FALLTHROUGH */ 694118611Snjl case 2: 695118611Snjl log(LOG_INFO, 696118611Snjl "Connection attempt to TCP %s:%d " 697118611Snjl "from %s:%d flags:0x%02x\n", 698118611Snjl dbuf, ntohs(th->th_dport), sbuf, 699118611Snjl ntohs(th->th_sport), thflags); 700118611Snjl break; 701118611Snjl default: 702118611Snjl break; 703118611Snjl } 704118611Snjl } 705151937Sjkim if (blackhole) { 706151937Sjkim switch (blackhole) { 707118611Snjl case 1: 708118611Snjl if (thflags & TH_SYN) 709118611Snjl goto drop; 710118611Snjl break; 711118611Snjl case 2: 712237412Sjkim goto drop; 713118611Snjl default: 714118611Snjl goto drop; 715118611Snjl } 716118611Snjl } 717118611Snjl rstreason = BANDLIM_RST_CLOSEDPORT; 718118611Snjl goto dropwithreset; 719118611Snjl } 720118611Snjl INP_LOCK(inp); 721118611Snjl if (inp->inp_vflag & INP_TIMEWAIT) { 722118611Snjl /* 723118611Snjl * The only option of relevance is TOF_CC, and only if 724118611Snjl * present in a SYN segment. See tcp_timewait(). 725118611Snjl */ 726151937Sjkim if (thflags & TH_SYN) 727151937Sjkim tcp_dooptions(&to, optp, optlen, 1); 728118611Snjl if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 729118611Snjl &to, th, m, tlen)) 730118611Snjl goto findpcb; 731118611Snjl /* 732118611Snjl * tcp_timewait unlocks inp. 733237412Sjkim */ 734118611Snjl INP_INFO_WUNLOCK(&tcbinfo); 735118611Snjl return; 736118611Snjl } 737118611Snjl tp = intotcpcb(inp); 738118611Snjl if (tp == 0) { 739118611Snjl INP_UNLOCK(inp); 740118611Snjl rstreason = BANDLIM_RST_CLOSEDPORT; 741118611Snjl goto dropwithreset; 742118611Snjl } 743151937Sjkim if (tp->t_state == TCPS_CLOSED) 744151937Sjkim goto drop; 745151937Sjkim 746151937Sjkim /* Unscale the window into a 32-bit value. */ 747151937Sjkim if ((thflags & TH_SYN) == 0) 748151937Sjkim tiwin = th->th_win << tp->snd_scale; 749151937Sjkim else 750151937Sjkim tiwin = th->th_win; 751151937Sjkim 752151937Sjkim#ifdef MAC 753151937Sjkim INP_LOCK_ASSERT(inp); 754151937Sjkim if (mac_check_inpcb_deliver(inp, m)) 755151937Sjkim goto drop; 756151937Sjkim#endif 757151937Sjkim so = inp->inp_socket; 758151937Sjkim#ifdef TCPDEBUG 759151937Sjkim if (so->so_options & SO_DEBUG) { 760151937Sjkim ostate = tp->t_state; 761151937Sjkim if (isipv6) 762151937Sjkim bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 763151937Sjkim else 764151937Sjkim bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 765151937Sjkim tcp_savetcp = *th; 766151937Sjkim } 767151937Sjkim#endif 768151937Sjkim if (so->so_options & SO_ACCEPTCONN) { 769151937Sjkim struct in_conninfo inc; 770151937Sjkim 771151937Sjkim#ifdef INET6 772151937Sjkim inc.inc_isipv6 = isipv6; 773151937Sjkim#endif 774151937Sjkim if (isipv6) { 775151937Sjkim inc.inc6_faddr = ip6->ip6_src; 776151937Sjkim inc.inc6_laddr = ip6->ip6_dst; 777151937Sjkim } else { 778151937Sjkim inc.inc_faddr = ip->ip_src; 779151937Sjkim inc.inc_laddr = ip->ip_dst; 780151937Sjkim } 781151937Sjkim inc.inc_fport = th->th_sport; 782151937Sjkim inc.inc_lport = th->th_dport; 783151937Sjkim 784151937Sjkim /* 785151937Sjkim * If the state is LISTEN then ignore segment if it contains 786151937Sjkim * a RST. If the segment contains an ACK then it is bad and 787151937Sjkim * send a RST. If it does not contain a SYN then it is not 788151937Sjkim * interesting; drop it. 789151937Sjkim * 790151937Sjkim * If the state is SYN_RECEIVED (syncache) and seg contains 791151937Sjkim * an ACK, but not for our SYN/ACK, send a RST. If the seg 792151937Sjkim * contains a RST, check the sequence number to see if it 793151937Sjkim * is a valid reset segment. 794151937Sjkim */ 795151937Sjkim if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 796151937Sjkim if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 797151937Sjkim if (!syncache_expand(&inc, th, &so, m)) { 798151937Sjkim /* 799151937Sjkim * No syncache entry, or ACK was not 800151937Sjkim * for our SYN/ACK. Send a RST. 801151937Sjkim */ 802151937Sjkim tcpstat.tcps_badsyn++; 803151937Sjkim rstreason = BANDLIM_RST_OPENPORT; 804151937Sjkim goto dropwithreset; 805151937Sjkim } 806151937Sjkim if (so == NULL) { 807151937Sjkim /* 808151937Sjkim * Could not complete 3-way handshake, 809 * connection is being closed down, and 810 * syncache will free mbuf. 811 */ 812 INP_UNLOCK(inp); 813 INP_INFO_WUNLOCK(&tcbinfo); 814 return; 815 } 816 /* 817 * Socket is created in state SYN_RECEIVED. 818 * Continue processing segment. 819 */ 820 INP_UNLOCK(inp); 821 inp = sotoinpcb(so); 822 INP_LOCK(inp); 823 tp = intotcpcb(inp); 824 /* 825 * This is what would have happened in 826 * tcp_output() when the SYN,ACK was sent. 827 */ 828 tp->snd_up = tp->snd_una; 829 tp->snd_max = tp->snd_nxt = tp->iss + 1; 830 tp->last_ack_sent = tp->rcv_nxt; 831 /* 832 * RFC1323: The window in SYN & SYN/ACK 833 * segments is never scaled. 834 */ 835 tp->snd_wnd = tiwin; /* unscaled */ 836 goto after_listen; 837 } 838 if (thflags & TH_RST) { 839 syncache_chkrst(&inc, th); 840 goto drop; 841 } 842 if (thflags & TH_ACK) { 843 syncache_badack(&inc); 844 tcpstat.tcps_badsyn++; 845 rstreason = BANDLIM_RST_OPENPORT; 846 goto dropwithreset; 847 } 848 goto drop; 849 } 850 851 /* 852 * Segment's flags are (SYN) or (SYN|FIN). 853 */ 854#ifdef INET6 855 /* 856 * If deprecated address is forbidden, 857 * we do not accept SYN to deprecated interface 858 * address to prevent any new inbound connection from 859 * getting established. 860 * When we do not accept SYN, we send a TCP RST, 861 * with deprecated source address (instead of dropping 862 * it). We compromise it as it is much better for peer 863 * to send a RST, and RST will be the final packet 864 * for the exchange. 865 * 866 * If we do not forbid deprecated addresses, we accept 867 * the SYN packet. RFC2462 does not suggest dropping 868 * SYN in this case. 869 * If we decipher RFC2462 5.5.4, it says like this: 870 * 1. use of deprecated addr with existing 871 * communication is okay - "SHOULD continue to be 872 * used" 873 * 2. use of it with new communication: 874 * (2a) "SHOULD NOT be used if alternate address 875 * with sufficient scope is available" 876 * (2b) nothing mentioned otherwise. 877 * Here we fall into (2b) case as we have no choice in 878 * our source address selection - we must obey the peer. 879 * 880 * The wording in RFC2462 is confusing, and there are 881 * multiple description text for deprecated address 882 * handling - worse, they are not exactly the same. 883 * I believe 5.5.4 is the best one, so we follow 5.5.4. 884 */ 885 if (isipv6 && !ip6_use_deprecated) { 886 struct in6_ifaddr *ia6; 887 888 if ((ia6 = ip6_getdstifaddr(m)) && 889 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 890 INP_UNLOCK(inp); 891 tp = NULL; 892 rstreason = BANDLIM_RST_OPENPORT; 893 goto dropwithreset; 894 } 895 } 896#endif 897 /* 898 * If it is from this socket, drop it, it must be forged. 899 * Don't bother responding if the destination was a broadcast. 900 */ 901 if (th->th_dport == th->th_sport) { 902 if (isipv6) { 903 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 904 &ip6->ip6_src)) 905 goto drop; 906 } else { 907 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 908 goto drop; 909 } 910 } 911 /* 912 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 913 * 914 * Note that it is quite possible to receive unicast 915 * link-layer packets with a broadcast IP address. Use 916 * in_broadcast() to find them. 917 */ 918 if (m->m_flags & (M_BCAST|M_MCAST)) 919 goto drop; 920 if (isipv6) { 921 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 922 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 923 goto drop; 924 } else { 925 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 926 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 927 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 928 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 929 goto drop; 930 } 931 /* 932 * SYN appears to be valid; create compressed TCP state 933 * for syncache, or perform t/tcp connection. 934 */ 935 if (so->so_qlen <= so->so_qlimit) { 936#ifdef TCPDEBUG 937 if (so->so_options & SO_DEBUG) 938 tcp_trace(TA_INPUT, ostate, tp, 939 (void *)tcp_saveipgen, &tcp_savetcp, 0); 940#endif 941 tcp_dooptions(&to, optp, optlen, 1); 942 if (!syncache_add(&inc, &to, th, &so, m)) 943 goto drop; 944 if (so == NULL) { 945 /* 946 * Entry added to syncache, mbuf used to 947 * send SYN,ACK packet. 948 */ 949 KASSERT(headlocked, ("headlocked")); 950 INP_UNLOCK(inp); 951 INP_INFO_WUNLOCK(&tcbinfo); 952 return; 953 } 954 /* 955 * Segment passed TAO tests. 956 */ 957 INP_UNLOCK(inp); 958 inp = sotoinpcb(so); 959 INP_LOCK(inp); 960 tp = intotcpcb(inp); 961 tp->snd_wnd = tiwin; 962 tp->t_starttime = ticks; 963 tp->t_state = TCPS_ESTABLISHED; 964 965 /* 966 * T/TCP logic: 967 * If there is a FIN or if there is data, then 968 * delay SYN,ACK(SYN) in the hope of piggy-backing 969 * it on a response segment. Otherwise must send 970 * ACK now in case the other side is slow starting. 971 */ 972 if (thflags & TH_FIN || tlen != 0) 973 tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 974 else 975 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 976 tcpstat.tcps_connects++; 977 soisconnected(so); 978 goto trimthenstep6; 979 } 980 goto drop; 981 } 982after_listen: 983 984 /* XXX temp debugging */ 985 /* should not happen - syncache should pick up these connections */ 986 if (tp->t_state == TCPS_LISTEN) 987 panic("tcp_input: TCPS_LISTEN"); 988 989 /* 990 * This is the second part of the MSS DoS prevention code (after 991 * minmss on the sending side) and it deals with too many too small 992 * tcp packets in a too short timeframe (1 second). 993 * 994 * For every full second we count the number of received packets 995 * and bytes. If we get a lot of packets per second for this connection 996 * (tcp_minmssoverload) we take a closer look at it and compute the 997 * average packet size for the past second. If that is less than 998 * tcp_minmss we get too many packets with very small payload which 999 * is not good and burdens our system (and every packet generates 1000 * a wakeup to the process connected to our socket). We can reasonable 1001 * expect this to be small packet DoS attack to exhaust our CPU 1002 * cycles. 1003 * 1004 * Care has to be taken for the minimum packet overload value. This 1005 * value defines the minimum number of packets per second before we 1006 * start to worry. This must not be too low to avoid killing for 1007 * example interactive connections with many small packets like 1008 * telnet or SSH. 1009 * 1010 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables 1011 * this check. 1012 * 1013 * Account for packet if payload packet, skip over ACK, etc. 1014 */ 1015 if (tcp_minmss && tcp_minmssoverload && 1016 tp->t_state == TCPS_ESTABLISHED && tlen > 0) { 1017 if (tp->rcv_second > ticks) { 1018 tp->rcv_pps++; 1019 tp->rcv_byps += tlen + off; 1020 if (tp->rcv_pps > tcp_minmssoverload) { 1021 if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) { 1022 printf("too many small tcp packets from " 1023 "%s:%u, av. %lubyte/packet, " 1024 "dropping connection\n", 1025#ifdef INET6 1026 isipv6 ? 1027 ip6_sprintf(&inp->inp_inc.inc6_faddr) : 1028#endif 1029 inet_ntoa(inp->inp_inc.inc_faddr), 1030 inp->inp_inc.inc_fport, 1031 tp->rcv_byps / tp->rcv_pps); 1032 tp = tcp_drop(tp, ECONNRESET); 1033 tcpstat.tcps_minmssdrops++; 1034 goto drop; 1035 } 1036 } 1037 } else { 1038 tp->rcv_second = ticks + hz; 1039 tp->rcv_pps = 1; 1040 tp->rcv_byps = tlen + off; 1041 } 1042 } 1043 1044 /* 1045 * Segment received on connection. 1046 * Reset idle time and keep-alive timer. 1047 */ 1048 tp->t_rcvtime = ticks; 1049 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1050 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 1051 1052 /* 1053 * Process options only when we get SYN/ACK back. The SYN case 1054 * for incoming connections is handled in tcp_syncache. 1055 * XXX this is traditional behavior, may need to be cleaned up. 1056 */ 1057 tcp_dooptions(&to, optp, optlen, thflags & TH_SYN); 1058 if (thflags & TH_SYN) { 1059 if (to.to_flags & TOF_SCALE) { 1060 tp->t_flags |= TF_RCVD_SCALE; 1061 tp->requested_s_scale = to.to_requested_s_scale; 1062 } 1063 if (to.to_flags & TOF_TS) { 1064 tp->t_flags |= TF_RCVD_TSTMP; 1065 tp->ts_recent = to.to_tsval; 1066 tp->ts_recent_age = ticks; 1067 } 1068 if (to.to_flags & (TOF_CC|TOF_CCNEW)) 1069 tp->t_flags |= TF_RCVD_CC; 1070 if (to.to_flags & TOF_MSS) 1071 tcp_mss(tp, to.to_mss); 1072 } 1073 1074 /* 1075 * Header prediction: check for the two common cases 1076 * of a uni-directional data xfer. If the packet has 1077 * no control flags, is in-sequence, the window didn't 1078 * change and we're not retransmitting, it's a 1079 * candidate. If the length is zero and the ack moved 1080 * forward, we're the sender side of the xfer. Just 1081 * free the data acked & wake any higher level process 1082 * that was blocked waiting for space. If the length 1083 * is non-zero and the ack didn't move, we're the 1084 * receiver side. If we're getting packets in-order 1085 * (the reassembly queue is empty), add the data to 1086 * the socket buffer and note that we need a delayed ack. 1087 * Make sure that the hidden state-flags are also off. 1088 * Since we check for TCPS_ESTABLISHED above, it can only 1089 * be TH_NEEDSYN. 1090 */ 1091 if (tp->t_state == TCPS_ESTABLISHED && 1092 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1093 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1094 ((to.to_flags & TOF_TS) == 0 || 1095 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 1096 /* 1097 * Using the CC option is compulsory if once started: 1098 * the segment is OK if no T/TCP was negotiated or 1099 * if the segment has a CC option equal to CCrecv 1100 */ 1101 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 1102 ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 1103 th->th_seq == tp->rcv_nxt && 1104 tiwin && tiwin == tp->snd_wnd && 1105 tp->snd_nxt == tp->snd_max) { 1106 1107 /* 1108 * If last ACK falls within this segment's sequence numbers, 1109 * record the timestamp. 1110 * NOTE that the test is modified according to the latest 1111 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1112 */ 1113 if ((to.to_flags & TOF_TS) != 0 && 1114 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1115 tp->ts_recent_age = ticks; 1116 tp->ts_recent = to.to_tsval; 1117 } 1118 1119 if (tlen == 0) { 1120 if (SEQ_GT(th->th_ack, tp->snd_una) && 1121 SEQ_LEQ(th->th_ack, tp->snd_max) && 1122 tp->snd_cwnd >= tp->snd_wnd && 1123 ((!tcp_do_newreno && 1124 tp->t_dupacks < tcprexmtthresh) || 1125 (tcp_do_newreno && !IN_FASTRECOVERY(tp)))) { 1126 KASSERT(headlocked, ("headlocked")); 1127 INP_INFO_WUNLOCK(&tcbinfo); 1128 /* 1129 * this is a pure ack for outstanding data. 1130 */ 1131 ++tcpstat.tcps_predack; 1132 /* 1133 * "bad retransmit" recovery 1134 */ 1135 if (tp->t_rxtshift == 1 && 1136 ticks < tp->t_badrxtwin) { 1137 ++tcpstat.tcps_sndrexmitbad; 1138 tp->snd_cwnd = tp->snd_cwnd_prev; 1139 tp->snd_ssthresh = 1140 tp->snd_ssthresh_prev; 1141 tp->snd_recover = tp->snd_recover_prev; 1142 if (tp->t_flags & TF_WASFRECOVERY) 1143 ENTER_FASTRECOVERY(tp); 1144 tp->snd_nxt = tp->snd_max; 1145 tp->t_badrxtwin = 0; 1146 } 1147 1148 /* 1149 * Recalculate the transmit timer / rtt. 1150 * 1151 * Some boxes send broken timestamp replies 1152 * during the SYN+ACK phase, ignore 1153 * timestamps of 0 or we could calculate a 1154 * huge RTT and blow up the retransmit timer. 1155 */ 1156 if ((to.to_flags & TOF_TS) != 0 && 1157 to.to_tsecr) { 1158 tcp_xmit_timer(tp, 1159 ticks - to.to_tsecr + 1); 1160 } else if (tp->t_rtttime && 1161 SEQ_GT(th->th_ack, tp->t_rtseq)) { 1162 tcp_xmit_timer(tp, 1163 ticks - tp->t_rtttime); 1164 } 1165 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1166 acked = th->th_ack - tp->snd_una; 1167 tcpstat.tcps_rcvackpack++; 1168 tcpstat.tcps_rcvackbyte += acked; 1169 sbdrop(&so->so_snd, acked); 1170 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 1171 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1172 tp->snd_recover = th->th_ack - 1; 1173 tp->snd_una = th->th_ack; 1174 /* 1175 * pull snd_wl2 up to prevent seq wrap relative 1176 * to th_ack. 1177 */ 1178 tp->snd_wl2 = th->th_ack; 1179 tp->t_dupacks = 0; 1180 m_freem(m); 1181 ND6_HINT(tp); /* some progress has been done */ 1182 1183 /* 1184 * If all outstanding data are acked, stop 1185 * retransmit timer, otherwise restart timer 1186 * using current (possibly backed-off) value. 1187 * If process is waiting for space, 1188 * wakeup/selwakeup/signal. If data 1189 * are ready to send, let tcp_output 1190 * decide between more output or persist. 1191 1192#ifdef TCPDEBUG 1193 if (so->so_options & SO_DEBUG) 1194 tcp_trace(TA_INPUT, ostate, tp, 1195 (void *)tcp_saveipgen, 1196 &tcp_savetcp, 0); 1197#endif 1198 */ 1199 if (tp->snd_una == tp->snd_max) 1200 callout_stop(tp->tt_rexmt); 1201 else if (!callout_active(tp->tt_persist)) 1202 callout_reset(tp->tt_rexmt, 1203 tp->t_rxtcur, 1204 tcp_timer_rexmt, tp); 1205 1206 sowwakeup(so); 1207 if (so->so_snd.sb_cc) 1208 (void) tcp_output(tp); 1209 goto check_delack; 1210 } 1211 } else if (th->th_ack == tp->snd_una && 1212 LIST_EMPTY(&tp->t_segq) && 1213 tlen <= sbspace(&so->so_rcv)) { 1214 KASSERT(headlocked, ("headlocked")); 1215 INP_INFO_WUNLOCK(&tcbinfo); 1216 /* 1217 * this is a pure, in-sequence data packet 1218 * with nothing on the reassembly queue and 1219 * we have enough buffer space to take it. 1220 */ 1221 ++tcpstat.tcps_preddat; 1222 tp->rcv_nxt += tlen; 1223 /* 1224 * Pull snd_wl1 up to prevent seq wrap relative to 1225 * th_seq. 1226 */ 1227 tp->snd_wl1 = th->th_seq; 1228 /* 1229 * Pull rcv_up up to prevent seq wrap relative to 1230 * rcv_nxt. 1231 */ 1232 tp->rcv_up = tp->rcv_nxt; 1233 tcpstat.tcps_rcvpack++; 1234 tcpstat.tcps_rcvbyte += tlen; 1235 ND6_HINT(tp); /* some progress has been done */ 1236 /* 1237#ifdef TCPDEBUG 1238 if (so->so_options & SO_DEBUG) 1239 tcp_trace(TA_INPUT, ostate, tp, 1240 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1241#endif 1242 * Add data to socket buffer. 1243 */ 1244 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1245 m_freem(m); 1246 } else { 1247 m_adj(m, drop_hdrlen); /* delayed header drop */ 1248 sbappendstream(&so->so_rcv, m); 1249 } 1250 sorwakeup(so); 1251 if (DELAY_ACK(tp)) { 1252 tp->t_flags |= TF_DELACK; 1253 } else { 1254 tp->t_flags |= TF_ACKNOW; 1255 tcp_output(tp); 1256 } 1257 goto check_delack; 1258 } 1259 } 1260 1261 /* 1262 * Calculate amount of space in receive window, 1263 * and then do TCP input processing. 1264 * Receive window is amount of space in rcv queue, 1265 * but not less than advertised window. 1266 */ 1267 { int win; 1268 1269 win = sbspace(&so->so_rcv); 1270 if (win < 0) 1271 win = 0; 1272 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1273 } 1274 1275 switch (tp->t_state) { 1276 1277 /* 1278 * If the state is SYN_RECEIVED: 1279 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1280 */ 1281 case TCPS_SYN_RECEIVED: 1282 if ((thflags & TH_ACK) && 1283 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1284 SEQ_GT(th->th_ack, tp->snd_max))) { 1285 rstreason = BANDLIM_RST_OPENPORT; 1286 goto dropwithreset; 1287 } 1288 break; 1289 1290 /* 1291 * If the state is SYN_SENT: 1292 * if seg contains an ACK, but not for our SYN, drop the input. 1293 * if seg contains a RST, then drop the connection. 1294 * if seg does not contain SYN, then drop it. 1295 * Otherwise this is an acceptable SYN segment 1296 * initialize tp->rcv_nxt and tp->irs 1297 * if seg contains ack then advance tp->snd_una 1298 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1299 * arrange for segment to be acked (eventually) 1300 * continue processing rest of data/controls, beginning with URG 1301 */ 1302 case TCPS_SYN_SENT: 1303 if (tcp_do_rfc1644) 1304 tcp_hc_gettao(&inp->inp_inc, &tao); 1305 1306 if ((thflags & TH_ACK) && 1307 (SEQ_LEQ(th->th_ack, tp->iss) || 1308 SEQ_GT(th->th_ack, tp->snd_max))) { 1309 /* 1310 * If we have a cached CCsent for the remote host, 1311 * hence we haven't just crashed and restarted, 1312 * do not send a RST. This may be a retransmission 1313 * from the other side after our earlier ACK was lost. 1314 * Our new SYN, when it arrives, will serve as the 1315 * needed ACK. 1316 */ 1317 if (tao.tao_ccsent != 0) 1318 goto drop; 1319 else { 1320 rstreason = BANDLIM_UNLIMITED; 1321 goto dropwithreset; 1322 } 1323 } 1324 if (thflags & TH_RST) { 1325 if (thflags & TH_ACK) 1326 tp = tcp_drop(tp, ECONNREFUSED); 1327 goto drop; 1328 } 1329 if ((thflags & TH_SYN) == 0) 1330 goto drop; 1331 tp->snd_wnd = th->th_win; /* initial send window */ 1332 tp->cc_recv = to.to_cc; /* foreign CC */ 1333 1334 tp->irs = th->th_seq; 1335 tcp_rcvseqinit(tp); 1336 if (thflags & TH_ACK) { 1337 /* 1338 * Our SYN was acked. If segment contains CC.ECHO 1339 * option, check it to make sure this segment really 1340 * matches our SYN. If not, just drop it as old 1341 * duplicate, but send an RST if we're still playing 1342 * by the old rules. If no CC.ECHO option, make sure 1343 * we don't get fooled into using T/TCP. 1344 */ 1345 if (to.to_flags & TOF_CCECHO) { 1346 if (tp->cc_send != to.to_ccecho) { 1347 if (tao.tao_ccsent != 0) 1348 goto drop; 1349 else { 1350 rstreason = BANDLIM_UNLIMITED; 1351 goto dropwithreset; 1352 } 1353 } 1354 } else 1355 tp->t_flags &= ~TF_RCVD_CC; 1356 tcpstat.tcps_connects++; 1357 soisconnected(so); 1358#ifdef MAC 1359 SOCK_LOCK(so); 1360 mac_set_socket_peer_from_mbuf(m, so); 1361 SOCK_UNLOCK(so); 1362#endif 1363 /* Do window scaling on this connection? */ 1364 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1365 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1366 tp->snd_scale = tp->requested_s_scale; 1367 tp->rcv_scale = tp->request_r_scale; 1368 } 1369 /* Segment is acceptable, update cache if undefined. */ 1370 if (tao.tao_ccsent == 0 && tcp_do_rfc1644) 1371 tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT, to.to_ccecho, 0); 1372 1373 tp->rcv_adv += tp->rcv_wnd; 1374 tp->snd_una++; /* SYN is acked */ 1375 /* 1376 * If there's data, delay ACK; if there's also a FIN 1377 * ACKNOW will be turned on later. 1378 */ 1379 if (DELAY_ACK(tp) && tlen != 0) 1380 callout_reset(tp->tt_delack, tcp_delacktime, 1381 tcp_timer_delack, tp); 1382 else 1383 tp->t_flags |= TF_ACKNOW; 1384 /* 1385 * Received <SYN,ACK> in SYN_SENT[*] state. 1386 * Transitions: 1387 * SYN_SENT --> ESTABLISHED 1388 * SYN_SENT* --> FIN_WAIT_1 1389 */ 1390 tp->t_starttime = ticks; 1391 if (tp->t_flags & TF_NEEDFIN) { 1392 tp->t_state = TCPS_FIN_WAIT_1; 1393 tp->t_flags &= ~TF_NEEDFIN; 1394 thflags &= ~TH_SYN; 1395 } else { 1396 tp->t_state = TCPS_ESTABLISHED; 1397 callout_reset(tp->tt_keep, tcp_keepidle, 1398 tcp_timer_keep, tp); 1399 } 1400 } else { 1401 /* 1402 * Received initial SYN in SYN-SENT[*] state => 1403 * simultaneous open. If segment contains CC option 1404 * and there is a cached CC, apply TAO test. 1405 * If it succeeds, connection is * half-synchronized. 1406 * Otherwise, do 3-way handshake: 1407 * SYN-SENT -> SYN-RECEIVED 1408 * SYN-SENT* -> SYN-RECEIVED* 1409 * If there was no CC option, clear cached CC value. 1410 */ 1411 tp->t_flags |= TF_ACKNOW; 1412 callout_stop(tp->tt_rexmt); 1413 if (to.to_flags & TOF_CC) { 1414 if (tao.tao_cc != 0 && 1415 CC_GT(to.to_cc, tao.tao_cc)) { 1416 /* 1417 * update cache and make transition: 1418 * SYN-SENT -> ESTABLISHED* 1419 * SYN-SENT* -> FIN-WAIT-1* 1420 */ 1421 tao.tao_cc = to.to_cc; 1422 tcp_hc_updatetao(&inp->inp_inc, 1423 TCP_HC_TAO_CC, to.to_cc, 0); 1424 tp->t_starttime = ticks; 1425 if (tp->t_flags & TF_NEEDFIN) { 1426 tp->t_state = TCPS_FIN_WAIT_1; 1427 tp->t_flags &= ~TF_NEEDFIN; 1428 } else { 1429 tp->t_state = TCPS_ESTABLISHED; 1430 callout_reset(tp->tt_keep, 1431 tcp_keepidle, 1432 tcp_timer_keep, 1433 tp); 1434 } 1435 tp->t_flags |= TF_NEEDSYN; 1436 } else 1437 tp->t_state = TCPS_SYN_RECEIVED; 1438 } else { 1439 if (tcp_do_rfc1644) { 1440 /* CC.NEW or no option => invalidate cache */ 1441 tao.tao_cc = 0; 1442 tcp_hc_updatetao(&inp->inp_inc, 1443 TCP_HC_TAO_CC, to.to_cc, 0); 1444 } 1445 tp->t_state = TCPS_SYN_RECEIVED; 1446 } 1447 } 1448 1449trimthenstep6: 1450 /* 1451 * Advance th->th_seq to correspond to first data byte. 1452 * If data, trim to stay within window, 1453 * dropping FIN if necessary. 1454 */ 1455 th->th_seq++; 1456 if (tlen > tp->rcv_wnd) { 1457 todrop = tlen - tp->rcv_wnd; 1458 m_adj(m, -todrop); 1459 tlen = tp->rcv_wnd; 1460 thflags &= ~TH_FIN; 1461 tcpstat.tcps_rcvpackafterwin++; 1462 tcpstat.tcps_rcvbyteafterwin += todrop; 1463 } 1464 tp->snd_wl1 = th->th_seq - 1; 1465 tp->rcv_up = th->th_seq; 1466 /* 1467 * Client side of transaction: already sent SYN and data. 1468 * If the remote host used T/TCP to validate the SYN, 1469 * our data will be ACK'd; if so, enter normal data segment 1470 * processing in the middle of step 5, ack processing. 1471 * Otherwise, goto step 6. 1472 */ 1473 if (thflags & TH_ACK) 1474 goto process_ACK; 1475 1476 goto step6; 1477 1478 /* 1479 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1480 * if segment contains a SYN and CC [not CC.NEW] option: 1481 * if state == TIME_WAIT and connection duration > MSL, 1482 * drop packet and send RST; 1483 * 1484 * if SEG.CC > CCrecv then is new SYN, and can implicitly 1485 * ack the FIN (and data) in retransmission queue. 1486 * Complete close and delete TCPCB. Then reprocess 1487 * segment, hoping to find new TCPCB in LISTEN state; 1488 * 1489 * else must be old SYN; drop it. 1490 * else do normal processing. 1491 */ 1492 case TCPS_LAST_ACK: 1493 case TCPS_CLOSING: 1494 case TCPS_TIME_WAIT: 1495 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1496 if ((thflags & TH_SYN) && 1497 (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1498 if (tp->t_state == TCPS_TIME_WAIT && 1499 (ticks - tp->t_starttime) > tcp_msl) { 1500 rstreason = BANDLIM_UNLIMITED; 1501 goto dropwithreset; 1502 } 1503 if (CC_GT(to.to_cc, tp->cc_recv)) { 1504 tp = tcp_close(tp); 1505 goto findpcb; 1506 } 1507 else 1508 goto drop; 1509 } 1510 break; /* continue normal processing */ 1511 } 1512 1513 /* 1514 * States other than LISTEN or SYN_SENT. 1515 * First check the RST flag and sequence number since reset segments 1516 * are exempt from the timestamp and connection count tests. This 1517 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1518 * below which allowed reset segments in half the sequence space 1519 * to fall though and be processed (which gives forged reset 1520 * segments with a random sequence number a 50 percent chance of 1521 * killing a connection). 1522 * Then check timestamp, if present. 1523 * Then check the connection count, if present. 1524 * Then check that at least some bytes of segment are within 1525 * receive window. If segment begins before rcv_nxt, 1526 * drop leading data (and SYN); if nothing left, just ack. 1527 * 1528 * 1529 * If the RST bit is set, check the sequence number to see 1530 * if this is a valid reset segment. 1531 * RFC 793 page 37: 1532 * In all states except SYN-SENT, all reset (RST) segments 1533 * are validated by checking their SEQ-fields. A reset is 1534 * valid if its sequence number is in the window. 1535 * Note: this does not take into account delayed ACKs, so 1536 * we should test against last_ack_sent instead of rcv_nxt. 1537 * The sequence number in the reset segment is normally an 1538 * echo of our outgoing acknowlegement numbers, but some hosts 1539 * send a reset with the sequence number at the rightmost edge 1540 * of our receive window, and we have to handle this case. 1541 * Note 2: Paul Watson's paper "Slipping in the Window" has shown 1542 * that brute force RST attacks are possible. To combat this, 1543 * we use a much stricter check while in the ESTABLISHED state, 1544 * only accepting RSTs where the sequence number is equal to 1545 * last_ack_sent. In all other states (the states in which a 1546 * RST is more likely), the more permissive check is used. 1547 * If we have multiple segments in flight, the intial reset 1548 * segment sequence numbers will be to the left of last_ack_sent, 1549 * but they will eventually catch up. 1550 * In any case, it never made sense to trim reset segments to 1551 * fit the receive window since RFC 1122 says: 1552 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1553 * 1554 * A TCP SHOULD allow a received RST segment to include data. 1555 * 1556 * DISCUSSION 1557 * It has been suggested that a RST segment could contain 1558 * ASCII text that encoded and explained the cause of the 1559 * RST. No standard has yet been established for such 1560 * data. 1561 * 1562 * If the reset segment passes the sequence number test examine 1563 * the state: 1564 * SYN_RECEIVED STATE: 1565 * If passive open, return to LISTEN state. 1566 * If active open, inform user that connection was refused. 1567 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 1568 * Inform user that connection was reset, and close tcb. 1569 * CLOSING, LAST_ACK STATES: 1570 * Close the tcb. 1571 * TIME_WAIT STATE: 1572 * Drop the segment - see Stevens, vol. 2, p. 964 and 1573 * RFC 1337. 1574 */ 1575 if (thflags & TH_RST) { 1576 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1577 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1578 switch (tp->t_state) { 1579 1580 case TCPS_SYN_RECEIVED: 1581 so->so_error = ECONNREFUSED; 1582 goto close; 1583 1584 case TCPS_ESTABLISHED: 1585 if (tp->last_ack_sent != th->th_seq) { 1586 tcpstat.tcps_badrst++; 1587 goto drop; 1588 } 1589 case TCPS_FIN_WAIT_1: 1590 case TCPS_FIN_WAIT_2: 1591 case TCPS_CLOSE_WAIT: 1592 so->so_error = ECONNRESET; 1593 close: 1594 tp->t_state = TCPS_CLOSED; 1595 tcpstat.tcps_drops++; 1596 tp = tcp_close(tp); 1597 break; 1598 1599 case TCPS_CLOSING: 1600 case TCPS_LAST_ACK: 1601 tp = tcp_close(tp); 1602 break; 1603 1604 case TCPS_TIME_WAIT: 1605 KASSERT(tp->t_state != TCPS_TIME_WAIT, 1606 ("timewait")); 1607 break; 1608 } 1609 } 1610 goto drop; 1611 } 1612 1613 /* 1614 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1615 * and it's less than ts_recent, drop it. 1616 */ 1617 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 1618 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1619 1620 /* Check to see if ts_recent is over 24 days old. */ 1621 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1622 /* 1623 * Invalidate ts_recent. If this segment updates 1624 * ts_recent, the age will be reset later and ts_recent 1625 * will get a valid value. If it does not, setting 1626 * ts_recent to zero will at least satisfy the 1627 * requirement that zero be placed in the timestamp 1628 * echo reply when ts_recent isn't valid. The 1629 * age isn't reset until we get a valid ts_recent 1630 * because we don't want out-of-order segments to be 1631 * dropped when ts_recent is old. 1632 */ 1633 tp->ts_recent = 0; 1634 } else { 1635 tcpstat.tcps_rcvduppack++; 1636 tcpstat.tcps_rcvdupbyte += tlen; 1637 tcpstat.tcps_pawsdrop++; 1638 if (tlen) 1639 goto dropafterack; 1640 goto drop; 1641 } 1642 } 1643 1644 /* 1645 * T/TCP mechanism 1646 * If T/TCP was negotiated and the segment doesn't have CC, 1647 * or if its CC is wrong then drop the segment. 1648 * RST segments do not have to comply with this. 1649 */ 1650 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1651 ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1652 goto dropafterack; 1653 1654 /* 1655 * In the SYN-RECEIVED state, validate that the packet belongs to 1656 * this connection before trimming the data to fit the receive 1657 * window. Check the sequence number versus IRS since we know 1658 * the sequence numbers haven't wrapped. This is a partial fix 1659 * for the "LAND" DoS attack. 1660 */ 1661 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1662 rstreason = BANDLIM_RST_OPENPORT; 1663 goto dropwithreset; 1664 } 1665 1666 todrop = tp->rcv_nxt - th->th_seq; 1667 if (todrop > 0) { 1668 if (thflags & TH_SYN) { 1669 thflags &= ~TH_SYN; 1670 th->th_seq++; 1671 if (th->th_urp > 1) 1672 th->th_urp--; 1673 else 1674 thflags &= ~TH_URG; 1675 todrop--; 1676 } 1677 /* 1678 * Following if statement from Stevens, vol. 2, p. 960. 1679 */ 1680 if (todrop > tlen 1681 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1682 /* 1683 * Any valid FIN must be to the left of the window. 1684 * At this point the FIN must be a duplicate or out 1685 * of sequence; drop it. 1686 */ 1687 thflags &= ~TH_FIN; 1688 1689 /* 1690 * Send an ACK to resynchronize and drop any data. 1691 * But keep on processing for RST or ACK. 1692 */ 1693 tp->t_flags |= TF_ACKNOW; 1694 todrop = tlen; 1695 tcpstat.tcps_rcvduppack++; 1696 tcpstat.tcps_rcvdupbyte += todrop; 1697 } else { 1698 tcpstat.tcps_rcvpartduppack++; 1699 tcpstat.tcps_rcvpartdupbyte += todrop; 1700 } 1701 drop_hdrlen += todrop; /* drop from the top afterwards */ 1702 th->th_seq += todrop; 1703 tlen -= todrop; 1704 if (th->th_urp > todrop) 1705 th->th_urp -= todrop; 1706 else { 1707 thflags &= ~TH_URG; 1708 th->th_urp = 0; 1709 } 1710 } 1711 1712 /* 1713 * If new data are received on a connection after the 1714 * user processes are gone, then RST the other end. 1715 */ 1716 if ((so->so_state & SS_NOFDREF) && 1717 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1718 tp = tcp_close(tp); 1719 tcpstat.tcps_rcvafterclose++; 1720 rstreason = BANDLIM_UNLIMITED; 1721 goto dropwithreset; 1722 } 1723 1724 /* 1725 * If segment ends after window, drop trailing data 1726 * (and PUSH and FIN); if nothing left, just ACK. 1727 */ 1728 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1729 if (todrop > 0) { 1730 tcpstat.tcps_rcvpackafterwin++; 1731 if (todrop >= tlen) { 1732 tcpstat.tcps_rcvbyteafterwin += tlen; 1733 /* 1734 * If a new connection request is received 1735 * while in TIME_WAIT, drop the old connection 1736 * and start over if the sequence numbers 1737 * are above the previous ones. 1738 */ 1739 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1740 if (thflags & TH_SYN && 1741 tp->t_state == TCPS_TIME_WAIT && 1742 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1743 tp = tcp_close(tp); 1744 goto findpcb; 1745 } 1746 /* 1747 * If window is closed can only take segments at 1748 * window edge, and have to drop data and PUSH from 1749 * incoming segments. Continue processing, but 1750 * remember to ack. Otherwise, drop segment 1751 * and ack. 1752 */ 1753 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1754 tp->t_flags |= TF_ACKNOW; 1755 tcpstat.tcps_rcvwinprobe++; 1756 } else 1757 goto dropafterack; 1758 } else 1759 tcpstat.tcps_rcvbyteafterwin += todrop; 1760 m_adj(m, -todrop); 1761 tlen -= todrop; 1762 thflags &= ~(TH_PUSH|TH_FIN); 1763 } 1764 1765 /* 1766 * If last ACK falls within this segment's sequence numbers, 1767 * record its timestamp. 1768 * NOTE that the test is modified according to the latest 1769 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1770 */ 1771 if ((to.to_flags & TOF_TS) != 0 && 1772 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1773 tp->ts_recent_age = ticks; 1774 tp->ts_recent = to.to_tsval; 1775 } 1776 1777 /* 1778 * If a SYN is in the window, then this is an 1779 * error and we send an RST and drop the connection. 1780 */ 1781 if (thflags & TH_SYN) { 1782 tp = tcp_drop(tp, ECONNRESET); 1783 rstreason = BANDLIM_UNLIMITED; 1784 goto drop; 1785 } 1786 1787 /* 1788 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1789 * flag is on (half-synchronized state), then queue data for 1790 * later processing; else drop segment and return. 1791 */ 1792 if ((thflags & TH_ACK) == 0) { 1793 if (tp->t_state == TCPS_SYN_RECEIVED || 1794 (tp->t_flags & TF_NEEDSYN)) 1795 goto step6; 1796 else 1797 goto drop; 1798 } 1799 1800 /* 1801 * Ack processing. 1802 */ 1803 switch (tp->t_state) { 1804 1805 /* 1806 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1807 * ESTABLISHED state and continue processing. 1808 * The ACK was checked above. 1809 */ 1810 case TCPS_SYN_RECEIVED: 1811 1812 tcpstat.tcps_connects++; 1813 soisconnected(so); 1814 /* Do window scaling? */ 1815 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1816 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1817 tp->snd_scale = tp->requested_s_scale; 1818 tp->rcv_scale = tp->request_r_scale; 1819 } 1820 /* 1821 * Upon successful completion of 3-way handshake, 1822 * update cache.CC, pass any queued data to the user, 1823 * and advance state appropriately. 1824 */ 1825 if (tcp_do_rfc1644) { 1826 tao.tao_cc = tp->cc_recv; 1827 tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CC, 1828 tp->cc_recv, 0); 1829 } 1830 /* 1831 * Make transitions: 1832 * SYN-RECEIVED -> ESTABLISHED 1833 * SYN-RECEIVED* -> FIN-WAIT-1 1834 */ 1835 tp->t_starttime = ticks; 1836 if (tp->t_flags & TF_NEEDFIN) { 1837 tp->t_state = TCPS_FIN_WAIT_1; 1838 tp->t_flags &= ~TF_NEEDFIN; 1839 } else { 1840 tp->t_state = TCPS_ESTABLISHED; 1841 callout_reset(tp->tt_keep, tcp_keepidle, 1842 tcp_timer_keep, tp); 1843 } 1844 /* 1845 * If segment contains data or ACK, will call tcp_reass() 1846 * later; if not, do so now to pass queued data to user. 1847 */ 1848 if (tlen == 0 && (thflags & TH_FIN) == 0) 1849 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1850 (struct mbuf *)0); 1851 tp->snd_wl1 = th->th_seq - 1; 1852 /* FALLTHROUGH */ 1853 1854 /* 1855 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1856 * ACKs. If the ack is in the range 1857 * tp->snd_una < th->th_ack <= tp->snd_max 1858 * then advance tp->snd_una to th->th_ack and drop 1859 * data from the retransmission queue. If this ACK reflects 1860 * more up to date window information we update our window information. 1861 */ 1862 case TCPS_ESTABLISHED: 1863 case TCPS_FIN_WAIT_1: 1864 case TCPS_FIN_WAIT_2: 1865 case TCPS_CLOSE_WAIT: 1866 case TCPS_CLOSING: 1867 case TCPS_LAST_ACK: 1868 case TCPS_TIME_WAIT: 1869 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1870 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1871 if (tlen == 0 && tiwin == tp->snd_wnd) { 1872 tcpstat.tcps_rcvdupack++; 1873 /* 1874 * If we have outstanding data (other than 1875 * a window probe), this is a completely 1876 * duplicate ack (ie, window info didn't 1877 * change), the ack is the biggest we've 1878 * seen and we've seen exactly our rexmt 1879 * threshhold of them, assume a packet 1880 * has been dropped and retransmit it. 1881 * Kludge snd_nxt & the congestion 1882 * window so we send only this one 1883 * packet. 1884 * 1885 * We know we're losing at the current 1886 * window size so do congestion avoidance 1887 * (set ssthresh to half the current window 1888 * and pull our congestion window back to 1889 * the new ssthresh). 1890 * 1891 * Dup acks mean that packets have left the 1892 * network (they're now cached at the receiver) 1893 * so bump cwnd by the amount in the receiver 1894 * to keep a constant cwnd packets in the 1895 * network. 1896 */ 1897 if (!callout_active(tp->tt_rexmt) || 1898 th->th_ack != tp->snd_una) 1899 tp->t_dupacks = 0; 1900 else if (++tp->t_dupacks > tcprexmtthresh || 1901 (tcp_do_newreno && 1902 IN_FASTRECOVERY(tp))) { 1903 tp->snd_cwnd += tp->t_maxseg; 1904 (void) tcp_output(tp); 1905 goto drop; 1906 } else if (tp->t_dupacks == tcprexmtthresh) { 1907 tcp_seq onxt = tp->snd_nxt; 1908 u_int win; 1909 if (tcp_do_newreno && 1910 SEQ_LEQ(th->th_ack, 1911 tp->snd_recover)) { 1912 tp->t_dupacks = 0; 1913 break; 1914 } 1915 win = min(tp->snd_wnd, tp->snd_cwnd) / 1916 2 / tp->t_maxseg; 1917 if (win < 2) 1918 win = 2; 1919 tp->snd_ssthresh = win * tp->t_maxseg; 1920 ENTER_FASTRECOVERY(tp); 1921 tp->snd_recover = tp->snd_max; 1922 callout_stop(tp->tt_rexmt); 1923 tp->t_rtttime = 0; 1924 tp->snd_nxt = th->th_ack; 1925 tp->snd_cwnd = tp->t_maxseg; 1926 (void) tcp_output(tp); 1927 KASSERT(tp->snd_limited <= 2, 1928 ("tp->snd_limited too big")); 1929 tp->snd_cwnd = tp->snd_ssthresh + 1930 tp->t_maxseg * 1931 (tp->t_dupacks - tp->snd_limited); 1932 if (SEQ_GT(onxt, tp->snd_nxt)) 1933 tp->snd_nxt = onxt; 1934 goto drop; 1935 } else if (tcp_do_rfc3042) { 1936 u_long oldcwnd = tp->snd_cwnd; 1937 tcp_seq oldsndmax = tp->snd_max; 1938 u_int sent; 1939 1940 KASSERT(tp->t_dupacks == 1 || 1941 tp->t_dupacks == 2, 1942 ("dupacks not 1 or 2")); 1943 if (tp->t_dupacks == 1) 1944 tp->snd_limited = 0; 1945 tp->snd_cwnd = 1946 (tp->snd_nxt - tp->snd_una) + 1947 (tp->t_dupacks - tp->snd_limited) * 1948 tp->t_maxseg; 1949 (void) tcp_output(tp); 1950 sent = tp->snd_max - oldsndmax; 1951 if (sent > tp->t_maxseg) { 1952 KASSERT((tp->t_dupacks == 2 && 1953 tp->snd_limited == 0) || 1954 (sent == tp->t_maxseg + 1 && 1955 tp->t_flags & TF_SENTFIN), 1956 ("sent too much")); 1957 tp->snd_limited = 2; 1958 } else if (sent > 0) 1959 ++tp->snd_limited; 1960 tp->snd_cwnd = oldcwnd; 1961 goto drop; 1962 } 1963 } else 1964 tp->t_dupacks = 0; 1965 break; 1966 } 1967 1968 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1969 1970 /* 1971 * If the congestion window was inflated to account 1972 * for the other side's cached packets, retract it. 1973 */ 1974 if (tcp_do_newreno) { 1975 if (IN_FASTRECOVERY(tp)) { 1976 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1977 tcp_newreno_partial_ack(tp, th); 1978 } else { 1979 /* 1980 * Window inflation should have left us 1981 * with approximately snd_ssthresh 1982 * outstanding data. 1983 * But in case we would be inclined to 1984 * send a burst, better to do it via 1985 * the slow start mechanism. 1986 */ 1987 if (SEQ_GT(th->th_ack + 1988 tp->snd_ssthresh, 1989 tp->snd_max)) 1990 tp->snd_cwnd = tp->snd_max - 1991 th->th_ack + 1992 tp->t_maxseg; 1993 else 1994 tp->snd_cwnd = tp->snd_ssthresh; 1995 } 1996 } 1997 } else { 1998 if (tp->t_dupacks >= tcprexmtthresh && 1999 tp->snd_cwnd > tp->snd_ssthresh) 2000 tp->snd_cwnd = tp->snd_ssthresh; 2001 } 2002 tp->t_dupacks = 0; 2003 if (SEQ_GT(th->th_ack, tp->snd_max)) { 2004 tcpstat.tcps_rcvacktoomuch++; 2005 goto dropafterack; 2006 } 2007 /* 2008 * If we reach this point, ACK is not a duplicate, 2009 * i.e., it ACKs something we sent. 2010 */ 2011 if (tp->t_flags & TF_NEEDSYN) { 2012 /* 2013 * T/TCP: Connection was half-synchronized, and our 2014 * SYN has been ACK'd (so connection is now fully 2015 * synchronized). Go to non-starred state, 2016 * increment snd_una for ACK of SYN, and check if 2017 * we can do window scaling. 2018 */ 2019 tp->t_flags &= ~TF_NEEDSYN; 2020 tp->snd_una++; 2021 /* Do window scaling? */ 2022 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2023 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2024 tp->snd_scale = tp->requested_s_scale; 2025 tp->rcv_scale = tp->request_r_scale; 2026 } 2027 } 2028 2029process_ACK: 2030 acked = th->th_ack - tp->snd_una; 2031 tcpstat.tcps_rcvackpack++; 2032 tcpstat.tcps_rcvackbyte += acked; 2033 2034 /* 2035 * If we just performed our first retransmit, and the ACK 2036 * arrives within our recovery window, then it was a mistake 2037 * to do the retransmit in the first place. Recover our 2038 * original cwnd and ssthresh, and proceed to transmit where 2039 * we left off. 2040 */ 2041 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 2042 ++tcpstat.tcps_sndrexmitbad; 2043 tp->snd_cwnd = tp->snd_cwnd_prev; 2044 tp->snd_ssthresh = tp->snd_ssthresh_prev; 2045 tp->snd_recover = tp->snd_recover_prev; 2046 if (tp->t_flags & TF_WASFRECOVERY) 2047 ENTER_FASTRECOVERY(tp); 2048 tp->snd_nxt = tp->snd_max; 2049 tp->t_badrxtwin = 0; /* XXX probably not required */ 2050 } 2051 2052 /* 2053 * If we have a timestamp reply, update smoothed 2054 * round trip time. If no timestamp is present but 2055 * transmit timer is running and timed sequence 2056 * number was acked, update smoothed round trip time. 2057 * Since we now have an rtt measurement, cancel the 2058 * timer backoff (cf., Phil Karn's retransmit alg.). 2059 * Recompute the initial retransmit timer. 2060 * 2061 * Some boxes send broken timestamp replies 2062 * during the SYN+ACK phase, ignore 2063 * timestamps of 0 or we could calculate a 2064 * huge RTT and blow up the retransmit timer. 2065 */ 2066 if ((to.to_flags & TOF_TS) != 0 && 2067 to.to_tsecr) { 2068 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2069 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2070 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2071 } 2072 tcp_xmit_bandwidth_limit(tp, th->th_ack); 2073 2074 /* 2075 * If all outstanding data is acked, stop retransmit 2076 * timer and remember to restart (more output or persist). 2077 * If there is more data to be acked, restart retransmit 2078 * timer, using current (possibly backed-off) value. 2079 */ 2080 if (th->th_ack == tp->snd_max) { 2081 callout_stop(tp->tt_rexmt); 2082 needoutput = 1; 2083 } else if (!callout_active(tp->tt_persist)) 2084 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 2085 tcp_timer_rexmt, tp); 2086 2087 /* 2088 * If no data (only SYN) was ACK'd, 2089 * skip rest of ACK processing. 2090 */ 2091 if (acked == 0) 2092 goto step6; 2093 2094 /* 2095 * When new data is acked, open the congestion window. 2096 * If the window gives us less than ssthresh packets 2097 * in flight, open exponentially (maxseg per packet). 2098 * Otherwise open linearly: maxseg per window 2099 * (maxseg^2 / cwnd per packet). 2100 */ 2101 if (!tcp_do_newreno || !IN_FASTRECOVERY(tp)) { 2102 register u_int cw = tp->snd_cwnd; 2103 register u_int incr = tp->t_maxseg; 2104 if (cw > tp->snd_ssthresh) 2105 incr = incr * incr / cw; 2106 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2107 } 2108 if (acked > so->so_snd.sb_cc) { 2109 tp->snd_wnd -= so->so_snd.sb_cc; 2110 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 2111 ourfinisacked = 1; 2112 } else { 2113 sbdrop(&so->so_snd, acked); 2114 tp->snd_wnd -= acked; 2115 ourfinisacked = 0; 2116 } 2117 sowwakeup(so); 2118 /* detect una wraparound */ 2119 if (tcp_do_newreno && !IN_FASTRECOVERY(tp) && 2120 SEQ_GT(tp->snd_una, tp->snd_recover) && 2121 SEQ_LEQ(th->th_ack, tp->snd_recover)) 2122 tp->snd_recover = th->th_ack - 1; 2123 if (tcp_do_newreno && IN_FASTRECOVERY(tp) && 2124 SEQ_GEQ(th->th_ack, tp->snd_recover)) 2125 EXIT_FASTRECOVERY(tp); 2126 tp->snd_una = th->th_ack; 2127 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2128 tp->snd_nxt = tp->snd_una; 2129 2130 switch (tp->t_state) { 2131 2132 /* 2133 * In FIN_WAIT_1 STATE in addition to the processing 2134 * for the ESTABLISHED state if our FIN is now acknowledged 2135 * then enter FIN_WAIT_2. 2136 */ 2137 case TCPS_FIN_WAIT_1: 2138 if (ourfinisacked) { 2139 /* 2140 * If we can't receive any more 2141 * data, then closing user can proceed. 2142 * Starting the timer is contrary to the 2143 * specification, but if we don't get a FIN 2144 * we'll hang forever. 2145 */ 2146 /* XXXjl 2147 * we should release the tp also, and use a 2148 * compressed state. 2149 */ 2150 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 2151 soisdisconnected(so); 2152 callout_reset(tp->tt_2msl, tcp_maxidle, 2153 tcp_timer_2msl, tp); 2154 } 2155 tp->t_state = TCPS_FIN_WAIT_2; 2156 } 2157 break; 2158 2159 /* 2160 * In CLOSING STATE in addition to the processing for 2161 * the ESTABLISHED state if the ACK acknowledges our FIN 2162 * then enter the TIME-WAIT state, otherwise ignore 2163 * the segment. 2164 */ 2165 case TCPS_CLOSING: 2166 if (ourfinisacked) { 2167 KASSERT(headlocked, ("headlocked")); 2168 tcp_twstart(tp); 2169 INP_INFO_WUNLOCK(&tcbinfo); 2170 m_freem(m); 2171 return; 2172 } 2173 break; 2174 2175 /* 2176 * In LAST_ACK, we may still be waiting for data to drain 2177 * and/or to be acked, as well as for the ack of our FIN. 2178 * If our FIN is now acknowledged, delete the TCB, 2179 * enter the closed state and return. 2180 */ 2181 case TCPS_LAST_ACK: 2182 if (ourfinisacked) { 2183 tp = tcp_close(tp); 2184 goto drop; 2185 } 2186 break; 2187 2188 /* 2189 * In TIME_WAIT state the only thing that should arrive 2190 * is a retransmission of the remote FIN. Acknowledge 2191 * it and restart the finack timer. 2192 */ 2193 case TCPS_TIME_WAIT: 2194 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 2195 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2196 tcp_timer_2msl, tp); 2197 goto dropafterack; 2198 } 2199 } 2200 2201step6: 2202 /* 2203 * Update window information. 2204 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2205 */ 2206 if ((thflags & TH_ACK) && 2207 (SEQ_LT(tp->snd_wl1, th->th_seq) || 2208 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2209 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2210 /* keep track of pure window updates */ 2211 if (tlen == 0 && 2212 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2213 tcpstat.tcps_rcvwinupd++; 2214 tp->snd_wnd = tiwin; 2215 tp->snd_wl1 = th->th_seq; 2216 tp->snd_wl2 = th->th_ack; 2217 if (tp->snd_wnd > tp->max_sndwnd) 2218 tp->max_sndwnd = tp->snd_wnd; 2219 needoutput = 1; 2220 } 2221 2222 /* 2223 * Process segments with URG. 2224 */ 2225 if ((thflags & TH_URG) && th->th_urp && 2226 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2227 /* 2228 * This is a kludge, but if we receive and accept 2229 * random urgent pointers, we'll crash in 2230 * soreceive. It's hard to imagine someone 2231 * actually wanting to send this much urgent data. 2232 */ 2233 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2234 th->th_urp = 0; /* XXX */ 2235 thflags &= ~TH_URG; /* XXX */ 2236 goto dodata; /* XXX */ 2237 } 2238 /* 2239 * If this segment advances the known urgent pointer, 2240 * then mark the data stream. This should not happen 2241 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2242 * a FIN has been received from the remote side. 2243 * In these states we ignore the URG. 2244 * 2245 * According to RFC961 (Assigned Protocols), 2246 * the urgent pointer points to the last octet 2247 * of urgent data. We continue, however, 2248 * to consider it to indicate the first octet 2249 * of data past the urgent section as the original 2250 * spec states (in one of two places). 2251 */ 2252 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2253 tp->rcv_up = th->th_seq + th->th_urp; 2254 so->so_oobmark = so->so_rcv.sb_cc + 2255 (tp->rcv_up - tp->rcv_nxt) - 1; 2256 if (so->so_oobmark == 0) { 2257 SOCKBUF_LOCK(&so->so_rcv); 2258 so->so_rcv.sb_state |= SBS_RCVATMARK; 2259 SOCKBUF_UNLOCK(&so->so_rcv); 2260 } 2261 sohasoutofband(so); 2262 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2263 } 2264 /* 2265 * Remove out of band data so doesn't get presented to user. 2266 * This can happen independent of advancing the URG pointer, 2267 * but if two URG's are pending at once, some out-of-band 2268 * data may creep in... ick. 2269 */ 2270 if (th->th_urp <= (u_long)tlen && 2271 !(so->so_options & SO_OOBINLINE)) { 2272 /* hdr drop is delayed */ 2273 tcp_pulloutofband(so, th, m, drop_hdrlen); 2274 } 2275 } else { 2276 /* 2277 * If no out of band data is expected, 2278 * pull receive urgent pointer along 2279 * with the receive window. 2280 */ 2281 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2282 tp->rcv_up = tp->rcv_nxt; 2283 } 2284dodata: /* XXX */ 2285 KASSERT(headlocked, ("headlocked")); 2286 /* 2287 * Process the segment text, merging it into the TCP sequencing queue, 2288 * and arranging for acknowledgment of receipt if necessary. 2289 * This process logically involves adjusting tp->rcv_wnd as data 2290 * is presented to the user (this happens in tcp_usrreq.c, 2291 * case PRU_RCVD). If a FIN has already been received on this 2292 * connection then we just ignore the text. 2293 */ 2294 if ((tlen || (thflags & TH_FIN)) && 2295 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2296 m_adj(m, drop_hdrlen); /* delayed header drop */ 2297 /* 2298 * Insert segment which includes th into TCP reassembly queue 2299 * with control block tp. Set thflags to whether reassembly now 2300 * includes a segment with FIN. This handles the common case 2301 * inline (segment is the next to be received on an established 2302 * connection, and the queue is empty), avoiding linkage into 2303 * and removal from the queue and repetition of various 2304 * conversions. 2305 * Set DELACK for segments received in order, but ack 2306 * immediately when segments are out of order (so 2307 * fast retransmit can work). 2308 */ 2309 if (th->th_seq == tp->rcv_nxt && 2310 LIST_EMPTY(&tp->t_segq) && 2311 TCPS_HAVEESTABLISHED(tp->t_state)) { 2312 if (DELAY_ACK(tp)) 2313 tp->t_flags |= TF_DELACK; 2314 else 2315 tp->t_flags |= TF_ACKNOW; 2316 tp->rcv_nxt += tlen; 2317 thflags = th->th_flags & TH_FIN; 2318 tcpstat.tcps_rcvpack++; 2319 tcpstat.tcps_rcvbyte += tlen; 2320 ND6_HINT(tp); 2321 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2322 m_freem(m); 2323 else 2324 sbappendstream(&so->so_rcv, m); 2325 sorwakeup(so); 2326 } else { 2327 thflags = tcp_reass(tp, th, &tlen, m); 2328 tp->t_flags |= TF_ACKNOW; 2329 } 2330 2331 /* 2332 * Note the amount of data that peer has sent into 2333 * our window, in order to estimate the sender's 2334 * buffer size. 2335 */ 2336 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2337 } else { 2338 m_freem(m); 2339 thflags &= ~TH_FIN; 2340 } 2341 2342 /* 2343 * If FIN is received ACK the FIN and let the user know 2344 * that the connection is closing. 2345 */ 2346 if (thflags & TH_FIN) { 2347 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2348 socantrcvmore(so); 2349 /* 2350 * If connection is half-synchronized 2351 * (ie NEEDSYN flag on) then delay ACK, 2352 * so it may be piggybacked when SYN is sent. 2353 * Otherwise, since we received a FIN then no 2354 * more input can be expected, send ACK now. 2355 */ 2356 if (tp->t_flags & TF_NEEDSYN) 2357 tp->t_flags |= TF_DELACK; 2358 else 2359 tp->t_flags |= TF_ACKNOW; 2360 tp->rcv_nxt++; 2361 } 2362 switch (tp->t_state) { 2363 2364 /* 2365 * In SYN_RECEIVED and ESTABLISHED STATES 2366 * enter the CLOSE_WAIT state. 2367 */ 2368 case TCPS_SYN_RECEIVED: 2369 tp->t_starttime = ticks; 2370 /*FALLTHROUGH*/ 2371 case TCPS_ESTABLISHED: 2372 tp->t_state = TCPS_CLOSE_WAIT; 2373 break; 2374 2375 /* 2376 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2377 * enter the CLOSING state. 2378 */ 2379 case TCPS_FIN_WAIT_1: 2380 tp->t_state = TCPS_CLOSING; 2381 break; 2382 2383 /* 2384 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2385 * starting the time-wait timer, turning off the other 2386 * standard timers. 2387 */ 2388 case TCPS_FIN_WAIT_2: 2389 KASSERT(headlocked == 1, ("headlocked should be 1")); 2390 tcp_twstart(tp); 2391 INP_INFO_WUNLOCK(&tcbinfo); 2392 return; 2393 2394 /* 2395 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2396 */ 2397 case TCPS_TIME_WAIT: 2398 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 2399 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2400 tcp_timer_2msl, tp); 2401 break; 2402 } 2403 } 2404 INP_INFO_WUNLOCK(&tcbinfo); 2405#ifdef TCPDEBUG 2406 if (so->so_options & SO_DEBUG) 2407 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2408 &tcp_savetcp, 0); 2409#endif 2410 2411 /* 2412 * Return any desired output. 2413 */ 2414 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2415 (void) tcp_output(tp); 2416 2417check_delack: 2418 if (tp->t_flags & TF_DELACK) { 2419 tp->t_flags &= ~TF_DELACK; 2420 callout_reset(tp->tt_delack, tcp_delacktime, 2421 tcp_timer_delack, tp); 2422 } 2423 INP_UNLOCK(inp); 2424 return; 2425 2426dropafterack: 2427 /* 2428 * Generate an ACK dropping incoming segment if it occupies 2429 * sequence space, where the ACK reflects our state. 2430 * 2431 * We can now skip the test for the RST flag since all 2432 * paths to this code happen after packets containing 2433 * RST have been dropped. 2434 * 2435 * In the SYN-RECEIVED state, don't send an ACK unless the 2436 * segment we received passes the SYN-RECEIVED ACK test. 2437 * If it fails send a RST. This breaks the loop in the 2438 * "LAND" DoS attack, and also prevents an ACK storm 2439 * between two listening ports that have been sent forged 2440 * SYN segments, each with the source address of the other. 2441 */ 2442 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2443 (SEQ_GT(tp->snd_una, th->th_ack) || 2444 SEQ_GT(th->th_ack, tp->snd_max)) ) { 2445 rstreason = BANDLIM_RST_OPENPORT; 2446 goto dropwithreset; 2447 } 2448#ifdef TCPDEBUG 2449 if (so->so_options & SO_DEBUG) 2450 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2451 &tcp_savetcp, 0); 2452#endif 2453 KASSERT(headlocked, ("headlocked should be 1")); 2454 INP_INFO_WUNLOCK(&tcbinfo); 2455 m_freem(m); 2456 tp->t_flags |= TF_ACKNOW; 2457 (void) tcp_output(tp); 2458 INP_UNLOCK(inp); 2459 return; 2460 2461dropwithreset: 2462 /* 2463 * Generate a RST, dropping incoming segment. 2464 * Make ACK acceptable to originator of segment. 2465 * Don't bother to respond if destination was broadcast/multicast. 2466 */ 2467 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2468 goto drop; 2469 if (isipv6) { 2470 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2471 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2472 goto drop; 2473 } else { 2474 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2475 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2476 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2477 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2478 goto drop; 2479 } 2480 /* IPv6 anycast check is done at tcp6_input() */ 2481 2482 /* 2483 * Perform bandwidth limiting. 2484 */ 2485 if (badport_bandlim(rstreason) < 0) 2486 goto drop; 2487 2488#ifdef TCPDEBUG 2489 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2490 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2491 &tcp_savetcp, 0); 2492#endif 2493 2494 if (thflags & TH_ACK) 2495 /* mtod() below is safe as long as hdr dropping is delayed */ 2496 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2497 TH_RST); 2498 else { 2499 if (thflags & TH_SYN) 2500 tlen++; 2501 /* mtod() below is safe as long as hdr dropping is delayed */ 2502 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2503 (tcp_seq)0, TH_RST|TH_ACK); 2504 } 2505 2506 if (tp) 2507 INP_UNLOCK(inp); 2508 if (headlocked) 2509 INP_INFO_WUNLOCK(&tcbinfo); 2510 return; 2511 2512drop: 2513 /* 2514 * Drop space held by incoming segment and return. 2515 */ 2516#ifdef TCPDEBUG 2517 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2518 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2519 &tcp_savetcp, 0); 2520#endif 2521 if (tp) 2522 INP_UNLOCK(inp); 2523 m_freem(m); 2524 if (headlocked) 2525 INP_INFO_WUNLOCK(&tcbinfo); 2526 return; 2527} 2528 2529/* 2530 * Parse TCP options and place in tcpopt. 2531 */ 2532static void 2533tcp_dooptions(to, cp, cnt, is_syn) 2534 struct tcpopt *to; 2535 u_char *cp; 2536 int cnt; 2537 int is_syn; 2538{ 2539 int opt, optlen; 2540 2541 to->to_flags = 0; 2542 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2543 opt = cp[0]; 2544 if (opt == TCPOPT_EOL) 2545 break; 2546 if (opt == TCPOPT_NOP) 2547 optlen = 1; 2548 else { 2549 if (cnt < 2) 2550 break; 2551 optlen = cp[1]; 2552 if (optlen < 2 || optlen > cnt) 2553 break; 2554 } 2555 switch (opt) { 2556 case TCPOPT_MAXSEG: 2557 if (optlen != TCPOLEN_MAXSEG) 2558 continue; 2559 if (!is_syn) 2560 continue; 2561 to->to_flags |= TOF_MSS; 2562 bcopy((char *)cp + 2, 2563 (char *)&to->to_mss, sizeof(to->to_mss)); 2564 to->to_mss = ntohs(to->to_mss); 2565 break; 2566 case TCPOPT_WINDOW: 2567 if (optlen != TCPOLEN_WINDOW) 2568 continue; 2569 if (! is_syn) 2570 continue; 2571 to->to_flags |= TOF_SCALE; 2572 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2573 break; 2574 case TCPOPT_TIMESTAMP: 2575 if (optlen != TCPOLEN_TIMESTAMP) 2576 continue; 2577 to->to_flags |= TOF_TS; 2578 bcopy((char *)cp + 2, 2579 (char *)&to->to_tsval, sizeof(to->to_tsval)); 2580 to->to_tsval = ntohl(to->to_tsval); 2581 bcopy((char *)cp + 6, 2582 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2583 to->to_tsecr = ntohl(to->to_tsecr); 2584 break; 2585 case TCPOPT_CC: 2586 if (optlen != TCPOLEN_CC) 2587 continue; 2588 to->to_flags |= TOF_CC; 2589 bcopy((char *)cp + 2, 2590 (char *)&to->to_cc, sizeof(to->to_cc)); 2591 to->to_cc = ntohl(to->to_cc); 2592 break; 2593 case TCPOPT_CCNEW: 2594 if (optlen != TCPOLEN_CC) 2595 continue; 2596 if (!is_syn) 2597 continue; 2598 to->to_flags |= TOF_CCNEW; 2599 bcopy((char *)cp + 2, 2600 (char *)&to->to_cc, sizeof(to->to_cc)); 2601 to->to_cc = ntohl(to->to_cc); 2602 break; 2603 case TCPOPT_CCECHO: 2604 if (optlen != TCPOLEN_CC) 2605 continue; 2606 if (!is_syn) 2607 continue; 2608 to->to_flags |= TOF_CCECHO; 2609 bcopy((char *)cp + 2, 2610 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2611 to->to_ccecho = ntohl(to->to_ccecho); 2612 break; 2613#ifdef TCP_SIGNATURE 2614 /* 2615 * XXX In order to reply to a host which has set the 2616 * TCP_SIGNATURE option in its initial SYN, we have to 2617 * record the fact that the option was observed here 2618 * for the syncache code to perform the correct response. 2619 */ 2620 case TCPOPT_SIGNATURE: 2621 if (optlen != TCPOLEN_SIGNATURE) 2622 continue; 2623 to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN); 2624 break; 2625#endif 2626 default: 2627 continue; 2628 } 2629 } 2630} 2631 2632/* 2633 * Pull out of band byte out of a segment so 2634 * it doesn't appear in the user's data queue. 2635 * It is still reflected in the segment length for 2636 * sequencing purposes. 2637 */ 2638static void 2639tcp_pulloutofband(so, th, m, off) 2640 struct socket *so; 2641 struct tcphdr *th; 2642 register struct mbuf *m; 2643 int off; /* delayed to be droped hdrlen */ 2644{ 2645 int cnt = off + th->th_urp - 1; 2646 2647 while (cnt >= 0) { 2648 if (m->m_len > cnt) { 2649 char *cp = mtod(m, caddr_t) + cnt; 2650 struct tcpcb *tp = sototcpcb(so); 2651 2652 tp->t_iobc = *cp; 2653 tp->t_oobflags |= TCPOOB_HAVEDATA; 2654 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2655 m->m_len--; 2656 if (m->m_flags & M_PKTHDR) 2657 m->m_pkthdr.len--; 2658 return; 2659 } 2660 cnt -= m->m_len; 2661 m = m->m_next; 2662 if (m == 0) 2663 break; 2664 } 2665 panic("tcp_pulloutofband"); 2666} 2667 2668/* 2669 * Collect new round-trip time estimate 2670 * and update averages and current timeout. 2671 */ 2672static void 2673tcp_xmit_timer(tp, rtt) 2674 register struct tcpcb *tp; 2675 int rtt; 2676{ 2677 register int delta; 2678 2679 tcpstat.tcps_rttupdated++; 2680 tp->t_rttupdated++; 2681 if (tp->t_srtt != 0) { 2682 /* 2683 * srtt is stored as fixed point with 5 bits after the 2684 * binary point (i.e., scaled by 8). The following magic 2685 * is equivalent to the smoothing algorithm in rfc793 with 2686 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2687 * point). Adjust rtt to origin 0. 2688 */ 2689 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2690 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2691 2692 if ((tp->t_srtt += delta) <= 0) 2693 tp->t_srtt = 1; 2694 2695 /* 2696 * We accumulate a smoothed rtt variance (actually, a 2697 * smoothed mean difference), then set the retransmit 2698 * timer to smoothed rtt + 4 times the smoothed variance. 2699 * rttvar is stored as fixed point with 4 bits after the 2700 * binary point (scaled by 16). The following is 2701 * equivalent to rfc793 smoothing with an alpha of .75 2702 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2703 * rfc793's wired-in beta. 2704 */ 2705 if (delta < 0) 2706 delta = -delta; 2707 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2708 if ((tp->t_rttvar += delta) <= 0) 2709 tp->t_rttvar = 1; 2710 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 2711 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2712 } else { 2713 /* 2714 * No rtt measurement yet - use the unsmoothed rtt. 2715 * Set the variance to half the rtt (so our first 2716 * retransmit happens at 3*rtt). 2717 */ 2718 tp->t_srtt = rtt << TCP_RTT_SHIFT; 2719 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2720 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2721 } 2722 tp->t_rtttime = 0; 2723 tp->t_rxtshift = 0; 2724 2725 /* 2726 * the retransmit should happen at rtt + 4 * rttvar. 2727 * Because of the way we do the smoothing, srtt and rttvar 2728 * will each average +1/2 tick of bias. When we compute 2729 * the retransmit timer, we want 1/2 tick of rounding and 2730 * 1 extra tick because of +-1/2 tick uncertainty in the 2731 * firing of the timer. The bias will give us exactly the 2732 * 1.5 tick we need. But, because the bias is 2733 * statistical, we have to test that we don't drop below 2734 * the minimum feasible timer (which is 2 ticks). 2735 */ 2736 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 2737 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2738 2739 /* 2740 * We received an ack for a packet that wasn't retransmitted; 2741 * it is probably safe to discard any error indications we've 2742 * received recently. This isn't quite right, but close enough 2743 * for now (a route might have failed after we sent a segment, 2744 * and the return path might not be symmetrical). 2745 */ 2746 tp->t_softerror = 0; 2747} 2748 2749/* 2750 * Determine a reasonable value for maxseg size. 2751 * If the route is known, check route for mtu. 2752 * If none, use an mss that can be handled on the outgoing 2753 * interface without forcing IP to fragment; if bigger than 2754 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2755 * to utilize large mbufs. If no route is found, route has no mtu, 2756 * or the destination isn't local, use a default, hopefully conservative 2757 * size (usually 512 or the default IP max size, but no more than the mtu 2758 * of the interface), as we can't discover anything about intervening 2759 * gateways or networks. We also initialize the congestion/slow start 2760 * window to be a single segment if the destination isn't local. 2761 * While looking at the routing entry, we also initialize other path-dependent 2762 * parameters from pre-set or cached values in the routing entry. 2763 * 2764 * Also take into account the space needed for options that we 2765 * send regularly. Make maxseg shorter by that amount to assure 2766 * that we can send maxseg amount of data even when the options 2767 * are present. Store the upper limit of the length of options plus 2768 * data in maxopd. 2769 * 2770 * 2771 * In case of T/TCP, we call this routine during implicit connection 2772 * setup as well (offer = -1), to initialize maxseg from the cached 2773 * MSS of our peer. 2774 * 2775 * NOTE that this routine is only called when we process an incoming 2776 * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2777 */ 2778void 2779tcp_mss(tp, offer) 2780 struct tcpcb *tp; 2781 int offer; 2782{ 2783 int rtt, mss; 2784 u_long bufsize; 2785 u_long maxmtu; 2786 struct inpcb *inp = tp->t_inpcb; 2787 struct socket *so; 2788 struct hc_metrics_lite metrics; 2789 struct rmxp_tao tao; 2790 int origoffer = offer; 2791#ifdef INET6 2792 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2793 size_t min_protoh = isipv6 ? 2794 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2795 sizeof (struct tcpiphdr); 2796#else 2797 const size_t min_protoh = sizeof(struct tcpiphdr); 2798#endif 2799 bzero(&tao, sizeof(tao)); 2800 2801 /* initialize */ 2802#ifdef INET6 2803 if (isipv6) { 2804 maxmtu = tcp_maxmtu6(&inp->inp_inc); 2805 tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 2806 } else 2807#endif 2808 { 2809 maxmtu = tcp_maxmtu(&inp->inp_inc); 2810 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2811 } 2812 so = inp->inp_socket; 2813 2814 /* 2815 * no route to sender, stay with default mss and return 2816 */ 2817 if (maxmtu == 0) 2818 return; 2819 2820 /* what have we got? */ 2821 switch (offer) { 2822 case 0: 2823 /* 2824 * Offer == 0 means that there was no MSS on the SYN 2825 * segment, in this case we use tcp_mssdflt. 2826 */ 2827 offer = 2828#ifdef INET6 2829 isipv6 ? tcp_v6mssdflt : 2830#endif 2831 tcp_mssdflt; 2832 break; 2833 2834 case -1: 2835 /* 2836 * Offer == -1 means that we didn't receive SYN yet, 2837 * use cached value in that case; 2838 */ 2839 if (tcp_do_rfc1644) 2840 tcp_hc_gettao(&inp->inp_inc, &tao); 2841 if (tao.tao_mssopt != 0) 2842 offer = tao.tao_mssopt; 2843 /* FALLTHROUGH */ 2844 2845 default: 2846 /* 2847 * Prevent DoS attack with too small MSS. Round up 2848 * to at least minmss. 2849 */ 2850 offer = max(offer, tcp_minmss); 2851 /* 2852 * Sanity check: make sure that maxopd will be large 2853 * enough to allow some data on segments even if the 2854 * all the option space is used (40bytes). Otherwise 2855 * funny things may happen in tcp_output. 2856 */ 2857 offer = max(offer, 64); 2858 if (tcp_do_rfc1644) 2859 tcp_hc_updatetao(&inp->inp_inc, 2860 TCP_HC_TAO_MSSOPT, 0, offer); 2861 } 2862 2863 /* 2864 * rmx information is now retrieved from tcp_hostcache 2865 */ 2866 tcp_hc_get(&inp->inp_inc, &metrics); 2867 2868 /* 2869 * if there's a discovered mtu int tcp hostcache, use it 2870 * else, use the link mtu. 2871 */ 2872 if (metrics.rmx_mtu) 2873 mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2874 else { 2875#ifdef INET6 2876 if (isipv6) { 2877 mss = maxmtu - min_protoh; 2878 if (!path_mtu_discovery && 2879 !in6_localaddr(&inp->in6p_faddr)) 2880 mss = min(mss, tcp_v6mssdflt); 2881 } else 2882#endif 2883 { 2884 mss = maxmtu - min_protoh; 2885 if (!path_mtu_discovery && 2886 !in_localaddr(inp->inp_faddr)) 2887 mss = min(mss, tcp_mssdflt); 2888 } 2889 } 2890 mss = min(mss, offer); 2891 2892 /* 2893 * maxopd stores the maximum length of data AND options 2894 * in a segment; maxseg is the amount of data in a normal 2895 * segment. We need to store this value (maxopd) apart 2896 * from maxseg, because now every segment carries options 2897 * and thus we normally have somewhat less data in segments. 2898 */ 2899 tp->t_maxopd = mss; 2900 2901 /* 2902 * In case of T/TCP, origoffer==-1 indicates, that no segments 2903 * were received yet. In this case we just guess, otherwise 2904 * we do the same as before T/TCP. 2905 */ 2906 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2907 (origoffer == -1 || 2908 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2909 mss -= TCPOLEN_TSTAMP_APPA; 2910 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2911 (origoffer == -1 || 2912 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2913 mss -= TCPOLEN_CC_APPA; 2914 tp->t_maxseg = mss; 2915 2916#if (MCLBYTES & (MCLBYTES - 1)) == 0 2917 if (mss > MCLBYTES) 2918 mss &= ~(MCLBYTES-1); 2919#else 2920 if (mss > MCLBYTES) 2921 mss = mss / MCLBYTES * MCLBYTES; 2922#endif 2923 tp->t_maxseg = mss; 2924 2925 /* 2926 * If there's a pipesize, change the socket buffer to that size, 2927 * don't change if sb_hiwat is different than default (then it 2928 * has been changed on purpose with setsockopt). 2929 * Make the socket buffers an integral number of mss units; 2930 * if the mss is larger than the socket buffer, decrease the mss. 2931 */ 2932 if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 2933 bufsize = metrics.rmx_sendpipe; 2934 else 2935 bufsize = so->so_snd.sb_hiwat; 2936 if (bufsize < mss) 2937 mss = bufsize; 2938 else { 2939 bufsize = roundup(bufsize, mss); 2940 if (bufsize > sb_max) 2941 bufsize = sb_max; 2942 if (bufsize > so->so_snd.sb_hiwat) 2943 (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2944 } 2945 tp->t_maxseg = mss; 2946 2947 if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 2948 bufsize = metrics.rmx_recvpipe; 2949 else 2950 bufsize = so->so_rcv.sb_hiwat; 2951 if (bufsize > mss) { 2952 bufsize = roundup(bufsize, mss); 2953 if (bufsize > sb_max) 2954 bufsize = sb_max; 2955 if (bufsize > so->so_rcv.sb_hiwat) 2956 (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2957 } 2958 /* 2959 * While we're here, check the others too 2960 */ 2961 if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 2962 tp->t_srtt = rtt; 2963 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 2964 tcpstat.tcps_usedrtt++; 2965 if (metrics.rmx_rttvar) { 2966 tp->t_rttvar = metrics.rmx_rttvar; 2967 tcpstat.tcps_usedrttvar++; 2968 } else { 2969 /* default variation is +- 1 rtt */ 2970 tp->t_rttvar = 2971 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2972 } 2973 TCPT_RANGESET(tp->t_rxtcur, 2974 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2975 tp->t_rttmin, TCPTV_REXMTMAX); 2976 } 2977 if (metrics.rmx_ssthresh) { 2978 /* 2979 * There's some sort of gateway or interface 2980 * buffer limit on the path. Use this to set 2981 * the slow start threshhold, but set the 2982 * threshold to no less than 2*mss. 2983 */ 2984 tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 2985 tcpstat.tcps_usedssthresh++; 2986 } 2987 if (metrics.rmx_bandwidth) 2988 tp->snd_bandwidth = metrics.rmx_bandwidth; 2989 2990 /* 2991 * Set the slow-start flight size depending on whether this 2992 * is a local network or not. 2993 * 2994 * Extend this so we cache the cwnd too and retrieve it here. 2995 * Make cwnd even bigger than RFC3390 suggests but only if we 2996 * have previous experience with the remote host. Be careful 2997 * not make cwnd bigger than remote receive window or our own 2998 * send socket buffer. Maybe put some additional upper bound 2999 * on the retrieved cwnd. Should do incremental updates to 3000 * hostcache when cwnd collapses so next connection doesn't 3001 * overloads the path again. 3002 * 3003 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 3004 * We currently check only in syncache_socket for that. 3005 */ 3006#define TCP_METRICS_CWND 3007#ifdef TCP_METRICS_CWND 3008 if (metrics.rmx_cwnd) 3009 tp->snd_cwnd = max(mss, 3010 min(metrics.rmx_cwnd / 2, 3011 min(tp->snd_wnd, so->so_snd.sb_hiwat))); 3012 else 3013#endif 3014 if (tcp_do_rfc3390) 3015 tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 3016#ifdef INET6 3017 else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 3018 (!isipv6 && in_localaddr(inp->inp_faddr))) 3019#else 3020 else if (in_localaddr(inp->inp_faddr)) 3021#endif 3022 tp->snd_cwnd = mss * ss_fltsz_local; 3023 else 3024 tp->snd_cwnd = mss * ss_fltsz; 3025} 3026 3027/* 3028 * Determine the MSS option to send on an outgoing SYN. 3029 */ 3030int 3031tcp_mssopt(inc) 3032 struct in_conninfo *inc; 3033{ 3034 int mss = 0; 3035 u_long maxmtu = 0; 3036 u_long thcmtu = 0; 3037 size_t min_protoh; 3038#ifdef INET6 3039 int isipv6 = inc->inc_isipv6 ? 1 : 0; 3040#endif 3041 3042 KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3043 3044#ifdef INET6 3045 if (isipv6) { 3046 mss = tcp_v6mssdflt; 3047 maxmtu = tcp_maxmtu6(inc); 3048 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 3049 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3050 } else 3051#endif 3052 { 3053 mss = tcp_mssdflt; 3054 maxmtu = tcp_maxmtu(inc); 3055 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 3056 min_protoh = sizeof(struct tcpiphdr); 3057 } 3058 if (maxmtu && thcmtu) 3059 mss = min(maxmtu, thcmtu) - min_protoh; 3060 else if (maxmtu || thcmtu) 3061 mss = max(maxmtu, thcmtu) - min_protoh; 3062 3063 return (mss); 3064} 3065 3066 3067/* 3068 * On a partial ack arrives, force the retransmission of the 3069 * next unacknowledged segment. Do not clear tp->t_dupacks. 3070 * By setting snd_nxt to ti_ack, this forces retransmission timer to 3071 * be started again. 3072 */ 3073static void 3074tcp_newreno_partial_ack(tp, th) 3075 struct tcpcb *tp; 3076 struct tcphdr *th; 3077{ 3078 tcp_seq onxt = tp->snd_nxt; 3079 u_long ocwnd = tp->snd_cwnd; 3080 3081 callout_stop(tp->tt_rexmt); 3082 tp->t_rtttime = 0; 3083 tp->snd_nxt = th->th_ack; 3084 /* 3085 * Set snd_cwnd to one segment beyond acknowledged offset. 3086 * (tp->snd_una has not yet been updated when this function is called.) 3087 */ 3088 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3089 tp->t_flags |= TF_ACKNOW; 3090 (void) tcp_output(tp); 3091 tp->snd_cwnd = ocwnd; 3092 if (SEQ_GT(onxt, tp->snd_nxt)) 3093 tp->snd_nxt = onxt; 3094 /* 3095 * Partial window deflation. Relies on fact that tp->snd_una 3096 * not updated yet. 3097 */ 3098 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 3099} 3100 3101/* 3102 * Returns 1 if the TIME_WAIT state was killed and we should start over, 3103 * looking for a pcb in the listen state. Returns 0 otherwise. 3104 */ 3105static int 3106tcp_timewait(tw, to, th, m, tlen) 3107 struct tcptw *tw; 3108 struct tcpopt *to; 3109 struct tcphdr *th; 3110 struct mbuf *m; 3111 int tlen; 3112{ 3113 int thflags; 3114 tcp_seq seq; 3115#ifdef INET6 3116 int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 3117#else 3118 const int isipv6 = 0; 3119#endif 3120 3121 thflags = th->th_flags; 3122 3123 /* 3124 * NOTE: for FIN_WAIT_2 (to be added later), 3125 * must validate sequence number before accepting RST 3126 */ 3127 3128 /* 3129 * If the segment contains RST: 3130 * Drop the segment - see Stevens, vol. 2, p. 964 and 3131 * RFC 1337. 3132 */ 3133 if (thflags & TH_RST) 3134 goto drop; 3135 3136 /* 3137 * If segment contains a SYN and CC [not CC.NEW] option: 3138 * if connection duration > MSL, drop packet and send RST; 3139 * 3140 * if SEG.CC > CCrecv then is new SYN. 3141 * Complete close and delete TCPCB. Then reprocess 3142 * segment, hoping to find new TCPCB in LISTEN state; 3143 * 3144 * else must be old SYN; drop it. 3145 * else do normal processing. 3146 */ 3147 if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) { 3148 if ((ticks - tw->t_starttime) > tcp_msl) 3149 goto reset; 3150 if (CC_GT(to->to_cc, tw->cc_recv)) { 3151 (void) tcp_twclose(tw, 0); 3152 return (1); 3153 } 3154 goto drop; 3155 } 3156 3157#if 0 3158/* PAWS not needed at the moment */ 3159 /* 3160 * RFC 1323 PAWS: If we have a timestamp reply on this segment 3161 * and it's less than ts_recent, drop it. 3162 */ 3163 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 3164 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 3165 if ((thflags & TH_ACK) == 0) 3166 goto drop; 3167 goto ack; 3168 } 3169 /* 3170 * ts_recent is never updated because we never accept new segments. 3171 */ 3172#endif 3173 3174 /* 3175 * If a new connection request is received 3176 * while in TIME_WAIT, drop the old connection 3177 * and start over if the sequence numbers 3178 * are above the previous ones. 3179 */ 3180 if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 3181 (void) tcp_twclose(tw, 0); 3182 return (1); 3183 } 3184 3185 /* 3186 * Drop the the segment if it does not contain an ACK. 3187 */ 3188 if ((thflags & TH_ACK) == 0) 3189 goto drop; 3190 3191 /* 3192 * Reset the 2MSL timer if this is a duplicate FIN. 3193 */ 3194 if (thflags & TH_FIN) { 3195 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 3196 if (seq + 1 == tw->rcv_nxt) 3197 tcp_timer_2msl_reset(tw, 2 * tcp_msl); 3198 } 3199 3200 /* 3201 * Acknowledge the segment if it has data or is not a duplicate ACK. 3202 */ 3203 if (thflags != TH_ACK || tlen != 0 || 3204 th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 3205 tcp_twrespond(tw, TH_ACK); 3206 goto drop; 3207 3208reset: 3209 /* 3210 * Generate a RST, dropping incoming segment. 3211 * Make ACK acceptable to originator of segment. 3212 * Don't bother to respond if destination was broadcast/multicast. 3213 */ 3214 if (m->m_flags & (M_BCAST|M_MCAST)) 3215 goto drop; 3216 if (isipv6) { 3217 struct ip6_hdr *ip6; 3218 3219 /* IPv6 anycast check is done at tcp6_input() */ 3220 ip6 = mtod(m, struct ip6_hdr *); 3221 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3222 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3223 goto drop; 3224 } else { 3225 struct ip *ip; 3226 3227 ip = mtod(m, struct ip *); 3228 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3229 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3230 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3231 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3232 goto drop; 3233 } 3234 if (thflags & TH_ACK) { 3235 tcp_respond(NULL, 3236 mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 3237 } else { 3238 seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 3239 tcp_respond(NULL, 3240 mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 3241 } 3242 INP_UNLOCK(tw->tw_inpcb); 3243 return (0); 3244 3245drop: 3246 INP_UNLOCK(tw->tw_inpcb); 3247 m_freem(m); 3248 return (0); 3249} 3250