tcp_input.c revision 56041
1/* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 34 * $FreeBSD: head/sys/netinet/tcp_input.c 56041 2000-01-15 14:56:38Z shin $ 35 */ 36 37#include "opt_ipfw.h" /* for ipfw_fwd */ 38#include "opt_inet6.h" 39#include "opt_ipsec.h" 40#include "opt_tcpdebug.h" 41#include "opt_tcp_input.h" 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/kernel.h> 46#include <sys/sysctl.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/proc.h> /* for proc0 declaration */ 50#include <sys/protosw.h> 51#include <sys/socket.h> 52#include <sys/socketvar.h> 53#include <sys/syslog.h> 54 55#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 56 57#include <net/if.h> 58#include <net/route.h> 59 60#include <netinet/in.h> 61#include <netinet/in_systm.h> 62#include <netinet/ip.h> 63#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 64#ifdef INET6 65#include <netinet/ip6.h> 66#include <netinet/in_var.h> 67#include <netinet6/nd6.h> 68#include <netinet/icmp6.h> 69#endif 70#include <netinet/in_pcb.h> 71#ifdef INET6 72#include <netinet6/in6_pcb.h> 73#endif 74#include <netinet/ip_var.h> 75#ifdef INET6 76#include <netinet6/ip6_var.h> 77#endif 78#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 79#include <netinet/tcp.h> 80#include <netinet/tcp_fsm.h> 81#include <netinet/tcp_seq.h> 82#include <netinet/tcp_timer.h> 83#include <netinet/tcp_var.h> 84#ifdef INET6 85#include <netinet6/tcp6_var.h> 86#endif 87#include <netinet/tcpip.h> 88#ifdef TCPDEBUG 89#include <netinet/tcp_debug.h> 90 91u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */ 92struct tcphdr tcp_savetcp; 93#endif /* TCPDEBUG */ 94 95#ifdef IPSEC 96#include <netinet6/ipsec.h> 97#ifdef INET6 98#include <netinet6/ipsec6.h> 99#endif 100#include <netkey/key.h> 101#endif /*IPSEC*/ 102 103MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); 104 105static int tcprexmtthresh = 3; 106tcp_seq tcp_iss; 107tcp_cc tcp_ccgen; 108 109struct tcpstat tcpstat; 110SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD, 111 &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 112 113static int log_in_vain = 0; 114SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 115 &log_in_vain, 0, "Log all incoming TCP connections"); 116 117static int blackhole = 0; 118SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 119 &blackhole, 0, "Do not send RST when dropping refused connections"); 120 121int tcp_delack_enabled = 1; 122SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 123 &tcp_delack_enabled, 0, 124 "Delay ACK to try and piggyback it onto a data packet"); 125 126#ifdef TCP_DROP_SYNFIN 127static int drop_synfin = 0; 128SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 129 &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 130#endif 131 132#ifdef TCP_RESTRICT_RST 133static int restrict_rst = 0; 134SYSCTL_INT(_net_inet_tcp, OID_AUTO, restrict_rst, CTLFLAG_RW, 135 &restrict_rst, 0, "Restrict RST emission"); 136#endif 137 138struct inpcbhead tcb; 139#define tcb6 tcb /* for KAME src sync over BSD*'s */ 140struct inpcbinfo tcbinfo; 141 142static void tcp_dooptions __P((struct tcpcb *, 143 u_char *, int, struct tcphdr *, struct tcpopt *)); 144static void tcp_pulloutofband __P((struct socket *, 145 struct tcphdr *, struct mbuf *, int)); 146static int tcp_reass __P((struct tcpcb *, struct tcphdr *, int *, 147 struct mbuf *)); 148static void tcp_xmit_timer __P((struct tcpcb *, int)); 149 150/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 151#ifdef INET6 152#define ND6_HINT(tp) \ 153do { \ 154 if ((tp) && (tp)->t_inpcb && \ 155 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 156 (tp)->t_inpcb->in6p_route.ro_rt) \ 157 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL); \ 158} while (0) 159#else 160#define ND6_HINT(tp) 161#endif 162 163/* 164 * Insert segment which inludes th into reassembly queue of tcp with 165 * control block tp. Return TH_FIN if reassembly now includes 166 * a segment with FIN. The macro form does the common case inline 167 * (segment is the next to be received on an established connection, 168 * and the queue is empty), avoiding linkage into and removal 169 * from the queue and repetition of various conversions. 170 * Set DELACK for segments received in order, but ack immediately 171 * when segments are out of order (so fast retransmit can work). 172 */ 173#define TCP_REASS(tp, th, tlenp, m, so, flags) { \ 174 if ((th)->th_seq == (tp)->rcv_nxt && \ 175 LIST_EMPTY(&(tp)->t_segq) && \ 176 (tp)->t_state == TCPS_ESTABLISHED) { \ 177 if (tcp_delack_enabled) \ 178 callout_reset(tp->tt_delack, tcp_delacktime, \ 179 tcp_timer_delack, tp); \ 180 else \ 181 tp->t_flags |= TF_ACKNOW; \ 182 (tp)->rcv_nxt += *(tlenp); \ 183 flags = (th)->th_flags & TH_FIN; \ 184 tcpstat.tcps_rcvpack++;\ 185 tcpstat.tcps_rcvbyte += *(tlenp);\ 186 ND6_HINT(tp); \ 187 sbappend(&(so)->so_rcv, (m)); \ 188 sorwakeup(so); \ 189 } else { \ 190 (flags) = tcp_reass((tp), (th), (tlenp), (m)); \ 191 tp->t_flags |= TF_ACKNOW; \ 192 } \ 193} 194 195static int 196tcp_reass(tp, th, tlenp, m) 197 register struct tcpcb *tp; 198 register struct tcphdr *th; 199 int *tlenp; 200 struct mbuf *m; 201{ 202 struct tseg_qent *q; 203 struct tseg_qent *p = NULL; 204 struct tseg_qent *nq; 205 struct tseg_qent *te; 206 struct socket *so = tp->t_inpcb->inp_socket; 207 int flags; 208 209 /* 210 * Call with th==0 after become established to 211 * force pre-ESTABLISHED data up to user socket. 212 */ 213 if (th == 0) 214 goto present; 215 216 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 217 MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 218 M_NOWAIT); 219 if (te == NULL) { 220 tcpstat.tcps_rcvmemdrop++; 221 m_freem(m); 222 return (0); 223 } 224 225 /* 226 * Find a segment which begins after this one does. 227 */ 228 LIST_FOREACH(q, &tp->t_segq, tqe_q) { 229 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 230 break; 231 p = q; 232 } 233 234 /* 235 * If there is a preceding segment, it may provide some of 236 * our data already. If so, drop the data from the incoming 237 * segment. If it provides all of our data, drop us. 238 */ 239 if (p != NULL) { 240 register int i; 241 /* conversion to int (in i) handles seq wraparound */ 242 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 243 if (i > 0) { 244 if (i >= *tlenp) { 245 tcpstat.tcps_rcvduppack++; 246 tcpstat.tcps_rcvdupbyte += *tlenp; 247 m_freem(m); 248 FREE(te, M_TSEGQ); 249 /* 250 * Try to present any queued data 251 * at the left window edge to the user. 252 * This is needed after the 3-WHS 253 * completes. 254 */ 255 goto present; /* ??? */ 256 } 257 m_adj(m, i); 258 *tlenp -= i; 259 th->th_seq += i; 260 } 261 } 262 tcpstat.tcps_rcvoopack++; 263 tcpstat.tcps_rcvoobyte += *tlenp; 264 265 /* 266 * While we overlap succeeding segments trim them or, 267 * if they are completely covered, dequeue them. 268 */ 269 while (q) { 270 register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 271 if (i <= 0) 272 break; 273 if (i < q->tqe_len) { 274 q->tqe_th->th_seq += i; 275 q->tqe_len -= i; 276 m_adj(q->tqe_m, i); 277 break; 278 } 279 280 nq = LIST_NEXT(q, tqe_q); 281 LIST_REMOVE(q, tqe_q); 282 m_freem(q->tqe_m); 283 FREE(q, M_TSEGQ); 284 q = nq; 285 } 286 287 /* Insert the new segment queue entry into place. */ 288 te->tqe_m = m; 289 te->tqe_th = th; 290 te->tqe_len = *tlenp; 291 292 if (p == NULL) { 293 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 294 } else { 295 LIST_INSERT_AFTER(p, te, tqe_q); 296 } 297 298present: 299 /* 300 * Present data to user, advancing rcv_nxt through 301 * completed sequence space. 302 */ 303 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 304 return (0); 305 q = LIST_FIRST(&tp->t_segq); 306 if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 307 return (0); 308 do { 309 tp->rcv_nxt += q->tqe_len; 310 flags = q->tqe_th->th_flags & TH_FIN; 311 nq = LIST_NEXT(q, tqe_q); 312 LIST_REMOVE(q, tqe_q); 313 if (so->so_state & SS_CANTRCVMORE) 314 m_freem(q->tqe_m); 315 else 316 sbappend(&so->so_rcv, q->tqe_m); 317 FREE(q, M_TSEGQ); 318 q = nq; 319 } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 320 ND6_HINT(tp); 321 sorwakeup(so); 322 return (flags); 323} 324 325/* 326 * TCP input routine, follows pages 65-76 of the 327 * protocol specification dated September, 1981 very closely. 328 */ 329#ifdef INET6 330int 331tcp6_input(mp, offp, proto) 332 struct mbuf **mp; 333 int *offp, proto; 334{ 335 register struct mbuf *m = *mp; 336 337 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 338 339 /* 340 * draft-itojun-ipv6-tcp-to-anycast 341 * better place to put this in? 342 */ 343 if (m->m_flags & M_ANYCAST6) { 344 struct ip6_hdr *ip6; 345 346 ip6 = mtod(m, struct ip6_hdr *); 347 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 348 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 349 return IPPROTO_DONE; 350 } 351 352 tcp_input(m, *offp, proto); 353 return IPPROTO_DONE; 354} 355#endif 356 357void 358tcp_input(m, off0, proto) 359 register struct mbuf *m; 360 int off0, proto; 361{ 362 register struct tcphdr *th; 363 register struct ip *ip = NULL; 364 register struct ipovly *ipov; 365 register struct inpcb *inp; 366 u_char *optp = NULL; 367 int optlen = 0; 368 int len, tlen, off; 369 int drop_hdrlen; 370 register struct tcpcb *tp = 0; 371 register int thflags; 372 struct socket *so = 0; 373 int todrop, acked, ourfinisacked, needoutput = 0; 374 struct in_addr laddr; 375#ifdef INET6 376 struct in6_addr laddr6; 377#endif 378 int dropsocket = 0; 379 int iss = 0; 380 u_long tiwin; 381 struct tcpopt to; /* options in this segment */ 382 struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 383 struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 384#ifdef TCPDEBUG 385 short ostate = 0; 386#endif 387#ifdef INET6 388 struct ip6_hdr *ip6 = NULL; 389 int isipv6; 390#endif /* INET6 */ 391 392#ifdef INET6 393 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 394#endif 395 bzero((char *)&to, sizeof(to)); 396 397 tcpstat.tcps_rcvtotal++; 398 399#ifdef INET6 400 if (isipv6) { 401 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 402 ip6 = mtod(m, struct ip6_hdr *); 403 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 404 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 405 tcpstat.tcps_rcvbadsum++; 406 goto drop; 407 } 408 th = (struct tcphdr *)((caddr_t)ip6 + off0); 409 } else 410#endif /* INET6 */ 411 { 412 /* 413 * Get IP and TCP header together in first mbuf. 414 * Note: IP leaves IP header in first mbuf. 415 */ 416 if (off0 > sizeof (struct ip)) { 417 ip_stripoptions(m, (struct mbuf *)0); 418 off0 = sizeof(struct ip); 419 } 420 if (m->m_len < sizeof (struct tcpiphdr)) { 421 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 422 tcpstat.tcps_rcvshort++; 423 return; 424 } 425 } 426 ip = mtod(m, struct ip *); 427 ipov = (struct ipovly *)ip; 428 429 /* 430 * Checksum extended TCP header and data. 431 */ 432 tlen = ip->ip_len; 433 len = sizeof (struct ip) + tlen; 434 bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 435 ipov->ih_len = (u_short)tlen; 436 HTONS(ipov->ih_len); 437 th = (struct tcphdr *)((caddr_t)ip + off0); 438 th->th_sum = in_cksum(m, len); 439 if (th->th_sum) { 440 tcpstat.tcps_rcvbadsum++; 441 goto drop; 442 } 443#ifdef INET6 444 /* Re-initialization for later version check */ 445 ip->ip_v = IPVERSION; 446#endif 447 } 448 449 /* 450 * Check that TCP offset makes sense, 451 * pull out TCP options and adjust length. XXX 452 */ 453 off = th->th_off << 2; 454 if (off < sizeof (struct tcphdr) || off > tlen) { 455 tcpstat.tcps_rcvbadoff++; 456 goto drop; 457 } 458 tlen -= off; /* tlen is used instead of ti->ti_len */ 459 if (off > sizeof (struct tcphdr)) { 460#ifdef INET6 461 if (isipv6) { 462 IP6_EXTHDR_CHECK(m, off0, off, ); 463 ip6 = mtod(m, struct ip6_hdr *); 464 th = (struct tcphdr *)((caddr_t)ip6 + off0); 465 } else 466#endif /* INET6 */ 467 { 468 if (m->m_len < sizeof(struct ip) + off) { 469 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 470 tcpstat.tcps_rcvshort++; 471 return; 472 } 473 ip = mtod(m, struct ip *); 474 ipov = (struct ipovly *)ip; 475 th = (struct tcphdr *)((caddr_t)ip + off0); 476 } 477 } 478 optlen = off - sizeof (struct tcphdr); 479 optp = (u_char *)(th + 1); 480 } 481 thflags = th->th_flags; 482 483#ifdef TCP_DROP_SYNFIN 484 /* 485 * If the drop_synfin option is enabled, drop all packets with 486 * both the SYN and FIN bits set. This prevents e.g. nmap from 487 * identifying the TCP/IP stack. 488 * 489 * This is incompatible with RFC1644 extensions (T/TCP). 490 */ 491 if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 492 goto drop; 493#endif 494 495 /* 496 * Convert TCP protocol specific fields to host format. 497 */ 498 NTOHL(th->th_seq); 499 NTOHL(th->th_ack); 500 NTOHS(th->th_win); 501 NTOHS(th->th_urp); 502 503 /* 504 * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options, 505 * until after ip6_savecontrol() is called and before other functions 506 * which don't want those proto headers. 507 * Because ip6_savecontrol() is going to parse the mbuf to 508 * search for data to be passed up to user-land, it wants mbuf 509 * parameters to be unchanged. 510 */ 511 drop_hdrlen = off0 + off; 512 513 /* 514 * Locate pcb for segment. 515 */ 516findpcb: 517#ifdef IPFIREWALL_FORWARD 518 if (ip_fw_fwd_addr != NULL 519#ifdef INET6 520 && isipv6 == NULL /* IPv6 support is not yet */ 521#endif /* INET6 */ 522 ) { 523 /* 524 * Diverted. Pretend to be the destination. 525 * already got one like this? 526 */ 527 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 528 ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif); 529 if (!inp) { 530 /* 531 * No, then it's new. Try find the ambushing socket 532 */ 533 if (!ip_fw_fwd_addr->sin_port) { 534 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, 535 th->th_sport, ip_fw_fwd_addr->sin_addr, 536 th->th_dport, 1, m->m_pkthdr.rcvif); 537 } else { 538 inp = in_pcblookup_hash(&tcbinfo, 539 ip->ip_src, th->th_sport, 540 ip_fw_fwd_addr->sin_addr, 541 ntohs(ip_fw_fwd_addr->sin_port), 1, 542 m->m_pkthdr.rcvif); 543 } 544 } 545 ip_fw_fwd_addr = NULL; 546 } else 547#endif /* IPFIREWALL_FORWARD */ 548 { 549#ifdef INET6 550 if (isipv6) 551 inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport, 552 &ip6->ip6_dst, th->th_dport, 1, 553 m->m_pkthdr.rcvif); 554 else 555#endif /* INET6 */ 556 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 557 ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif); 558 } 559 560#ifdef IPSEC 561#ifdef INET6 562 if (isipv6) { 563 if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 564 ipsec6stat.in_polvio++; 565 goto drop; 566 } 567 } else 568#endif /* INET6 */ 569 if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 570 ipsecstat.in_polvio++; 571 goto drop; 572 } 573#endif /*IPSEC*/ 574 575 /* 576 * If the state is CLOSED (i.e., TCB does not exist) then 577 * all data in the incoming segment is discarded. 578 * If the TCB exists but is in CLOSED state, it is embryonic, 579 * but should either do a listen or a connect soon. 580 */ 581 if (inp == NULL) { 582 if (log_in_vain) { 583#ifdef INET6 584 char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN]; 585#else /* INET6 */ 586 char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 587#endif /* INET6 */ 588 589#ifdef INET6 590 if (isipv6) { 591 strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst)); 592 strcpy(sbuf, ip6_sprintf(&ip6->ip6_src)); 593 } else 594#endif 595 { 596 strcpy(dbuf, inet_ntoa(ip->ip_dst)); 597 strcpy(sbuf, inet_ntoa(ip->ip_src)); 598 } 599 switch (log_in_vain) { 600 case 1: 601 if(thflags & TH_SYN) 602 log(LOG_INFO, 603 "Connection attempt to TCP %s:%d from %s:%d\n", 604 dbuf, ntohs(th->th_dport), 605 sbuf, 606 ntohs(th->th_sport)); 607 break; 608 case 2: 609 log(LOG_INFO, 610 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n", 611 dbuf, ntohs(th->th_dport), sbuf, 612 ntohs(th->th_sport), thflags); 613 break; 614 default: 615 break; 616 } 617 } 618#ifdef ICMP_BANDLIM 619 if (badport_bandlim(1) < 0) 620 goto drop; 621#endif 622 if (blackhole) { 623 switch (blackhole) { 624 case 1: 625 if (thflags & TH_SYN) 626 goto drop; 627 break; 628 case 2: 629 goto drop; 630 default: 631 goto drop; 632 } 633 } 634 goto dropwithreset; 635 } 636 tp = intotcpcb(inp); 637 if (tp == 0) 638 goto dropwithreset; 639 if (tp->t_state == TCPS_CLOSED) 640 goto drop; 641 642 /* Unscale the window into a 32-bit value. */ 643 if ((thflags & TH_SYN) == 0) 644 tiwin = th->th_win << tp->snd_scale; 645 else 646 tiwin = th->th_win; 647 648#ifdef INET6 649 /* save packet options if user wanted */ 650 if (inp->in6p_flags & INP_CONTROLOPTS) { 651 if (inp->in6p_options) { 652 m_freem(inp->in6p_options); 653 inp->in6p_options = 0; 654 } 655 ip6_savecontrol(inp, &inp->in6p_options, ip6, m); 656 } 657 /* else, should also do ip_srcroute() here? */ 658#endif /* INET6 */ 659 660 so = inp->inp_socket; 661 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 662#ifdef TCPDEBUG 663 if (so->so_options & SO_DEBUG) { 664 ostate = tp->t_state; 665#ifdef INET6 666 if (isipv6) 667 bcopy((char *)ip6, (char *)tcp_saveipgen, 668 sizeof(*ip6)); 669 else 670#endif /* INET6 */ 671 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 672 tcp_savetcp = *th; 673 } 674#endif 675 if (so->so_options & SO_ACCEPTCONN) { 676 register struct tcpcb *tp0 = tp; 677 struct socket *so2; 678#ifdef IPSEC 679 struct socket *oso; 680#endif 681#ifdef INET6 682 struct inpcb *oinp = sotoinpcb(so); 683#endif /* INET6 */ 684 685#ifndef IPSEC 686 /* 687 * Current IPsec implementation makes incorrect IPsec 688 * cache if this check is done here. 689 * So delay this until duplicated socket is created. 690 */ 691 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 692 /* 693 * Note: dropwithreset makes sure we don't 694 * send a RST in response to a RST. 695 */ 696 if (thflags & TH_ACK) { 697 tcpstat.tcps_badsyn++; 698 goto dropwithreset; 699 } 700 goto drop; 701 } 702#endif 703 so2 = sonewconn(so, 0); 704 if (so2 == 0) { 705 tcpstat.tcps_listendrop++; 706 so2 = sodropablereq(so); 707 if (so2) { 708 tcp_drop(sototcpcb(so2), ETIMEDOUT); 709 so2 = sonewconn(so, 0); 710 } 711 if (!so2) 712 goto drop; 713 } 714#ifdef IPSEC 715 oso = so; 716#endif 717 so = so2; 718 /* 719 * This is ugly, but .... 720 * 721 * Mark socket as temporary until we're 722 * committed to keeping it. The code at 723 * ``drop'' and ``dropwithreset'' check the 724 * flag dropsocket to see if the temporary 725 * socket created here should be discarded. 726 * We mark the socket as discardable until 727 * we're committed to it below in TCPS_LISTEN. 728 */ 729 dropsocket++; 730 inp = (struct inpcb *)so->so_pcb; 731#ifdef INET6 732 if (isipv6) 733 inp->in6p_laddr = ip6->ip6_dst; 734 else { 735 if (ip6_mapped_addr_on) { 736 inp->inp_vflag &= ~INP_IPV6; 737 inp->inp_vflag |= INP_IPV4; 738 } 739#endif /* INET6 */ 740 inp->inp_laddr = ip->ip_dst; 741#ifdef INET6 742 } 743#endif /* INET6 */ 744 inp->inp_lport = th->th_dport; 745 if (in_pcbinshash(inp) != 0) { 746 /* 747 * Undo the assignments above if we failed to 748 * put the PCB on the hash lists. 749 */ 750#ifdef INET6 751 if (isipv6) 752 inp->in6p_laddr = in6addr_any; 753 else 754#endif /* INET6 */ 755 inp->inp_laddr.s_addr = INADDR_ANY; 756 inp->inp_lport = 0; 757 goto drop; 758 } 759#ifdef IPSEC 760 /* 761 * To avoid creating incorrectly cached IPsec 762 * association, this is need to be done here. 763 * 764 * Subject: (KAME-snap 748) 765 * From: Wayne Knowles <w.knowles@niwa.cri.nz> 766 * ftp://ftp.kame.net/pub/mail-list/snap-users/748 767 */ 768 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 769 /* 770 * Note: dropwithreset makes sure we don't 771 * send a RST in response to a RST. 772 */ 773 if (thflags & TH_ACK) { 774 tcpstat.tcps_badsyn++; 775 goto dropwithreset; 776 } 777 goto drop; 778 } 779#endif 780#ifdef INET6 781 if (isipv6) { 782 /* 783 * inherit socket options from the listening 784 * socket. 785 */ 786 inp->inp_flags |= 787 oinp->inp_flags & INP_CONTROLOPTS; 788 if (inp->inp_flags & INP_CONTROLOPTS) { 789 if (inp->in6p_options) { 790 m_freem(inp->in6p_options); 791 inp->in6p_options = 0; 792 } 793 ip6_savecontrol(inp, 794 &inp->in6p_options, 795 ip6, m); 796 } 797 } else 798#endif /* INET6 */ 799 inp->inp_options = ip_srcroute(); 800#ifdef IPSEC 801 /* copy old policy into new socket's */ 802 if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp, 803 inp->inp_sp)) 804 printf("tcp_input: could not copy policy\n"); 805#endif 806 tp = intotcpcb(inp); 807 tp->t_state = TCPS_LISTEN; 808 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 809 810 /* Compute proper scaling value from buffer space */ 811 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 812 TCP_MAXWIN << tp->request_r_scale < 813 so->so_rcv.sb_hiwat) 814 tp->request_r_scale++; 815 } 816 } 817 818 /* 819 * Segment received on connection. 820 * Reset idle time and keep-alive timer. 821 */ 822 tp->t_rcvtime = ticks; 823 if (TCPS_HAVEESTABLISHED(tp->t_state)) 824 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 825 826 /* 827 * Process options if not in LISTEN state, 828 * else do it below (after getting remote address). 829 */ 830 if (tp->t_state != TCPS_LISTEN) 831 tcp_dooptions(tp, optp, optlen, th, &to); 832 833 /* 834 * Header prediction: check for the two common cases 835 * of a uni-directional data xfer. If the packet has 836 * no control flags, is in-sequence, the window didn't 837 * change and we're not retransmitting, it's a 838 * candidate. If the length is zero and the ack moved 839 * forward, we're the sender side of the xfer. Just 840 * free the data acked & wake any higher level process 841 * that was blocked waiting for space. If the length 842 * is non-zero and the ack didn't move, we're the 843 * receiver side. If we're getting packets in-order 844 * (the reassembly queue is empty), add the data to 845 * the socket buffer and note that we need a delayed ack. 846 * Make sure that the hidden state-flags are also off. 847 * Since we check for TCPS_ESTABLISHED above, it can only 848 * be TH_NEEDSYN. 849 */ 850 if (tp->t_state == TCPS_ESTABLISHED && 851 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 852 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 853 ((to.to_flag & TOF_TS) == 0 || 854 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 855 /* 856 * Using the CC option is compulsory if once started: 857 * the segment is OK if no T/TCP was negotiated or 858 * if the segment has a CC option equal to CCrecv 859 */ 860 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 861 ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 862 th->th_seq == tp->rcv_nxt && 863 tiwin && tiwin == tp->snd_wnd && 864 tp->snd_nxt == tp->snd_max) { 865 866 /* 867 * If last ACK falls within this segment's sequence numbers, 868 * record the timestamp. 869 * NOTE that the test is modified according to the latest 870 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 871 */ 872 if ((to.to_flag & TOF_TS) != 0 && 873 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 874 tp->ts_recent_age = ticks; 875 tp->ts_recent = to.to_tsval; 876 } 877 878 if (tlen == 0) { 879 if (SEQ_GT(th->th_ack, tp->snd_una) && 880 SEQ_LEQ(th->th_ack, tp->snd_max) && 881 tp->snd_cwnd >= tp->snd_wnd && 882 tp->t_dupacks < tcprexmtthresh) { 883 /* 884 * this is a pure ack for outstanding data. 885 */ 886 ++tcpstat.tcps_predack; 887 /* 888 * "bad retransmit" recovery 889 */ 890 if (tp->t_rxtshift == 1 && 891 ticks < tp->t_badrxtwin) { 892 tp->snd_cwnd = tp->snd_cwnd_prev; 893 tp->snd_ssthresh = 894 tp->snd_ssthresh_prev; 895 tp->snd_nxt = tp->snd_max; 896 tp->t_badrxtwin = 0; 897 } 898 if ((to.to_flag & TOF_TS) != 0) 899 tcp_xmit_timer(tp, 900 ticks - to.to_tsecr + 1); 901 else if (tp->t_rtttime && 902 SEQ_GT(th->th_ack, tp->t_rtseq)) 903 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 904 acked = th->th_ack - tp->snd_una; 905 tcpstat.tcps_rcvackpack++; 906 tcpstat.tcps_rcvackbyte += acked; 907 sbdrop(&so->so_snd, acked); 908 tp->snd_una = th->th_ack; 909 m_freem(m); 910 ND6_HINT(tp); /* some progress has been done */ 911 912 /* 913 * If all outstanding data are acked, stop 914 * retransmit timer, otherwise restart timer 915 * using current (possibly backed-off) value. 916 * If process is waiting for space, 917 * wakeup/selwakeup/signal. If data 918 * are ready to send, let tcp_output 919 * decide between more output or persist. 920 */ 921 if (tp->snd_una == tp->snd_max) 922 callout_stop(tp->tt_rexmt); 923 else if (!callout_active(tp->tt_persist)) 924 callout_reset(tp->tt_rexmt, 925 tp->t_rxtcur, 926 tcp_timer_rexmt, tp); 927 928 sowwakeup(so); 929 if (so->so_snd.sb_cc) 930 (void) tcp_output(tp); 931 return; 932 } 933 } else if (th->th_ack == tp->snd_una && 934 LIST_EMPTY(&tp->t_segq) && 935 tlen <= sbspace(&so->so_rcv)) { 936 /* 937 * this is a pure, in-sequence data packet 938 * with nothing on the reassembly queue and 939 * we have enough buffer space to take it. 940 */ 941 ++tcpstat.tcps_preddat; 942 tp->rcv_nxt += tlen; 943 tcpstat.tcps_rcvpack++; 944 tcpstat.tcps_rcvbyte += tlen; 945 ND6_HINT(tp); /* some progress has been done */ 946 /* 947 * Add data to socket buffer. 948 */ 949 m_adj(m, drop_hdrlen); /* delayed header drop */ 950 sbappend(&so->so_rcv, m); 951 sorwakeup(so); 952 if (tcp_delack_enabled) { 953 callout_reset(tp->tt_delack, tcp_delacktime, 954 tcp_timer_delack, tp); 955 } else { 956 tp->t_flags |= TF_ACKNOW; 957 tcp_output(tp); 958 } 959 return; 960 } 961 } 962 963 /* 964 * Calculate amount of space in receive window, 965 * and then do TCP input processing. 966 * Receive window is amount of space in rcv queue, 967 * but not less than advertised window. 968 */ 969 { int win; 970 971 win = sbspace(&so->so_rcv); 972 if (win < 0) 973 win = 0; 974 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 975 } 976 977 switch (tp->t_state) { 978 979 /* 980 * If the state is LISTEN then ignore segment if it contains an RST. 981 * If the segment contains an ACK then it is bad and send a RST. 982 * If it does not contain a SYN then it is not interesting; drop it. 983 * If it is from this socket, drop it, it must be forged. 984 * Don't bother responding if the destination was a broadcast. 985 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 986 * tp->iss, and send a segment: 987 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 988 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 989 * Fill in remote peer address fields if not previously specified. 990 * Enter SYN_RECEIVED state, and process any other fields of this 991 * segment in this state. 992 */ 993 case TCPS_LISTEN: { 994 register struct sockaddr_in *sin; 995#ifdef INET6 996 register struct sockaddr_in6 *sin6; 997#endif 998 999 if (thflags & TH_RST) 1000 goto drop; 1001 if (thflags & TH_ACK) 1002 goto dropwithreset; 1003 if ((thflags & TH_SYN) == 0) 1004 goto drop; 1005 if (th->th_dport == th->th_sport) { 1006#ifdef INET6 1007 if (isipv6) { 1008 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 1009 &ip6->ip6_src)) 1010 goto drop; 1011 } else 1012#endif /* INET6 */ 1013 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 1014 goto drop; 1015 } 1016 /* 1017 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 1018 * in_broadcast() should never return true on a received 1019 * packet with M_BCAST not set. 1020 */ 1021 if (m->m_flags & (M_BCAST|M_MCAST)) 1022 goto drop; 1023#ifdef INET6 1024 if (isipv6) { 1025 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 1026 goto drop; 1027 } else 1028#endif 1029 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) 1030 goto drop; 1031#ifdef INET6 1032 if (isipv6) { 1033 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 1034 M_SONAME, M_NOWAIT); 1035 if (sin6 == NULL) 1036 goto drop; 1037 bzero(sin6, sizeof(*sin6)); 1038 sin6->sin6_family = AF_INET6; 1039 sin6->sin6_len = sizeof(*sin6); 1040 sin6->sin6_addr = ip6->ip6_src; 1041 sin6->sin6_port = th->th_sport; 1042 laddr6 = inp->in6p_laddr; 1043 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1044 inp->in6p_laddr = ip6->ip6_dst; 1045 if (in6_pcbconnect(inp, (struct sockaddr *)sin6, 1046 &proc0)) { 1047 inp->in6p_laddr = laddr6; 1048 FREE(sin6, M_SONAME); 1049 goto drop; 1050 } 1051 FREE(sin6, M_SONAME); 1052 } else 1053#endif 1054 { 1055 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 1056 M_NOWAIT); 1057 if (sin == NULL) 1058 goto drop; 1059 sin->sin_family = AF_INET; 1060 sin->sin_len = sizeof(*sin); 1061 sin->sin_addr = ip->ip_src; 1062 sin->sin_port = th->th_sport; 1063 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 1064 laddr = inp->inp_laddr; 1065 if (inp->inp_laddr.s_addr == INADDR_ANY) 1066 inp->inp_laddr = ip->ip_dst; 1067 if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) { 1068 inp->inp_laddr = laddr; 1069 FREE(sin, M_SONAME); 1070 goto drop; 1071 } 1072 FREE(sin, M_SONAME); 1073 } 1074 tp->t_template = tcp_template(tp); 1075 if (tp->t_template == 0) { 1076 tp = tcp_drop(tp, ENOBUFS); 1077 dropsocket = 0; /* socket is already gone */ 1078 goto drop; 1079 } 1080 if ((taop = tcp_gettaocache(inp)) == NULL) { 1081 taop = &tao_noncached; 1082 bzero(taop, sizeof(*taop)); 1083 } 1084 tcp_dooptions(tp, optp, optlen, th, &to); 1085 if (iss) 1086 tp->iss = iss; 1087 else 1088 tp->iss = tcp_iss; 1089 tcp_iss += TCP_ISSINCR/4; 1090 tp->irs = th->th_seq; 1091 tcp_sendseqinit(tp); 1092 tcp_rcvseqinit(tp); 1093 /* 1094 * Initialization of the tcpcb for transaction; 1095 * set SND.WND = SEG.WND, 1096 * initialize CCsend and CCrecv. 1097 */ 1098 tp->snd_wnd = tiwin; /* initial send-window */ 1099 tp->cc_send = CC_INC(tcp_ccgen); 1100 tp->cc_recv = to.to_cc; 1101 /* 1102 * Perform TAO test on incoming CC (SEG.CC) option, if any. 1103 * - compare SEG.CC against cached CC from the same host, 1104 * if any. 1105 * - if SEG.CC > chached value, SYN must be new and is accepted 1106 * immediately: save new CC in the cache, mark the socket 1107 * connected, enter ESTABLISHED state, turn on flag to 1108 * send a SYN in the next segment. 1109 * A virtual advertised window is set in rcv_adv to 1110 * initialize SWS prevention. Then enter normal segment 1111 * processing: drop SYN, process data and FIN. 1112 * - otherwise do a normal 3-way handshake. 1113 */ 1114 if ((to.to_flag & TOF_CC) != 0) { 1115 if (((tp->t_flags & TF_NOPUSH) != 0) && 1116 taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 1117 1118 taop->tao_cc = to.to_cc; 1119 tp->t_starttime = ticks; 1120 tp->t_state = TCPS_ESTABLISHED; 1121 1122 /* 1123 * If there is a FIN, or if there is data and the 1124 * connection is local, then delay SYN,ACK(SYN) in 1125 * the hope of piggy-backing it on a response 1126 * segment. Otherwise must send ACK now in case 1127 * the other side is slow starting. 1128 */ 1129 if (tcp_delack_enabled && ((thflags & TH_FIN) || 1130 (tlen != 0 && 1131#ifdef INET6 1132 ((isipv6 && in6_localaddr(&inp->in6p_faddr)) 1133 || 1134 (!isipv6 && 1135#endif 1136 in_localaddr(inp->inp_faddr) 1137#ifdef INET6 1138 )) 1139#endif 1140 ))) { 1141 callout_reset(tp->tt_delack, tcp_delacktime, 1142 tcp_timer_delack, tp); 1143 tp->t_flags |= TF_NEEDSYN; 1144 } else 1145 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1146 1147 /* 1148 * Limit the `virtual advertised window' to TCP_MAXWIN 1149 * here. Even if we requested window scaling, it will 1150 * become effective only later when our SYN is acked. 1151 */ 1152 tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 1153 tcpstat.tcps_connects++; 1154 soisconnected(so); 1155 callout_reset(tp->tt_keep, tcp_keepinit, 1156 tcp_timer_keep, tp); 1157 dropsocket = 0; /* committed to socket */ 1158 tcpstat.tcps_accepts++; 1159 goto trimthenstep6; 1160 } 1161 /* else do standard 3-way handshake */ 1162 } else { 1163 /* 1164 * No CC option, but maybe CC.NEW: 1165 * invalidate cached value. 1166 */ 1167 taop->tao_cc = 0; 1168 } 1169 /* 1170 * TAO test failed or there was no CC option, 1171 * do a standard 3-way handshake. 1172 */ 1173 tp->t_flags |= TF_ACKNOW; 1174 tp->t_state = TCPS_SYN_RECEIVED; 1175 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1176 dropsocket = 0; /* committed to socket */ 1177 tcpstat.tcps_accepts++; 1178 ND6_HINT((struct tcpcb *)inp->inp_ppcb); 1179 goto trimthenstep6; 1180 } 1181 1182 /* 1183 * If the state is SYN_RECEIVED: 1184 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1185 */ 1186 case TCPS_SYN_RECEIVED: 1187 if ((thflags & TH_ACK) && 1188 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1189 SEQ_GT(th->th_ack, tp->snd_max))) 1190 goto dropwithreset; 1191 break; 1192 1193 /* 1194 * If the state is SYN_SENT: 1195 * if seg contains an ACK, but not for our SYN, drop the input. 1196 * if seg contains a RST, then drop the connection. 1197 * if seg does not contain SYN, then drop it. 1198 * Otherwise this is an acceptable SYN segment 1199 * initialize tp->rcv_nxt and tp->irs 1200 * if seg contains ack then advance tp->snd_una 1201 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1202 * arrange for segment to be acked (eventually) 1203 * continue processing rest of data/controls, beginning with URG 1204 */ 1205 case TCPS_SYN_SENT: 1206 if ((taop = tcp_gettaocache(inp)) == NULL) { 1207 taop = &tao_noncached; 1208 bzero(taop, sizeof(*taop)); 1209 } 1210 1211 if ((thflags & TH_ACK) && 1212 (SEQ_LEQ(th->th_ack, tp->iss) || 1213 SEQ_GT(th->th_ack, tp->snd_max))) { 1214 /* 1215 * If we have a cached CCsent for the remote host, 1216 * hence we haven't just crashed and restarted, 1217 * do not send a RST. This may be a retransmission 1218 * from the other side after our earlier ACK was lost. 1219 * Our new SYN, when it arrives, will serve as the 1220 * needed ACK. 1221 */ 1222 if (taop->tao_ccsent != 0) 1223 goto drop; 1224 else 1225 goto dropwithreset; 1226 } 1227 if (thflags & TH_RST) { 1228 if (thflags & TH_ACK) 1229 tp = tcp_drop(tp, ECONNREFUSED); 1230 goto drop; 1231 } 1232 if ((thflags & TH_SYN) == 0) 1233 goto drop; 1234 tp->snd_wnd = th->th_win; /* initial send window */ 1235 tp->cc_recv = to.to_cc; /* foreign CC */ 1236 1237 tp->irs = th->th_seq; 1238 tcp_rcvseqinit(tp); 1239 if (thflags & TH_ACK) { 1240 /* 1241 * Our SYN was acked. If segment contains CC.ECHO 1242 * option, check it to make sure this segment really 1243 * matches our SYN. If not, just drop it as old 1244 * duplicate, but send an RST if we're still playing 1245 * by the old rules. If no CC.ECHO option, make sure 1246 * we don't get fooled into using T/TCP. 1247 */ 1248 if (to.to_flag & TOF_CCECHO) { 1249 if (tp->cc_send != to.to_ccecho) { 1250 if (taop->tao_ccsent != 0) 1251 goto drop; 1252 else 1253 goto dropwithreset; 1254 } 1255 } else 1256 tp->t_flags &= ~TF_RCVD_CC; 1257 tcpstat.tcps_connects++; 1258 soisconnected(so); 1259 /* Do window scaling on this connection? */ 1260 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1261 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1262 tp->snd_scale = tp->requested_s_scale; 1263 tp->rcv_scale = tp->request_r_scale; 1264 } 1265 /* Segment is acceptable, update cache if undefined. */ 1266 if (taop->tao_ccsent == 0) 1267 taop->tao_ccsent = to.to_ccecho; 1268 1269 tp->rcv_adv += tp->rcv_wnd; 1270 tp->snd_una++; /* SYN is acked */ 1271 /* 1272 * If there's data, delay ACK; if there's also a FIN 1273 * ACKNOW will be turned on later. 1274 */ 1275 if (tcp_delack_enabled && tlen != 0) 1276 callout_reset(tp->tt_delack, tcp_delacktime, 1277 tcp_timer_delack, tp); 1278 else 1279 tp->t_flags |= TF_ACKNOW; 1280 /* 1281 * Received <SYN,ACK> in SYN_SENT[*] state. 1282 * Transitions: 1283 * SYN_SENT --> ESTABLISHED 1284 * SYN_SENT* --> FIN_WAIT_1 1285 */ 1286 tp->t_starttime = ticks; 1287 if (tp->t_flags & TF_NEEDFIN) { 1288 tp->t_state = TCPS_FIN_WAIT_1; 1289 tp->t_flags &= ~TF_NEEDFIN; 1290 thflags &= ~TH_SYN; 1291 } else { 1292 tp->t_state = TCPS_ESTABLISHED; 1293 callout_reset(tp->tt_keep, tcp_keepidle, 1294 tcp_timer_keep, tp); 1295 } 1296 } else { 1297 /* 1298 * Received initial SYN in SYN-SENT[*] state => simul- 1299 * taneous open. If segment contains CC option and there is 1300 * a cached CC, apply TAO test; if it succeeds, connection is 1301 * half-synchronized. Otherwise, do 3-way handshake: 1302 * SYN-SENT -> SYN-RECEIVED 1303 * SYN-SENT* -> SYN-RECEIVED* 1304 * If there was no CC option, clear cached CC value. 1305 */ 1306 tp->t_flags |= TF_ACKNOW; 1307 callout_stop(tp->tt_rexmt); 1308 if (to.to_flag & TOF_CC) { 1309 if (taop->tao_cc != 0 && 1310 CC_GT(to.to_cc, taop->tao_cc)) { 1311 /* 1312 * update cache and make transition: 1313 * SYN-SENT -> ESTABLISHED* 1314 * SYN-SENT* -> FIN-WAIT-1* 1315 */ 1316 taop->tao_cc = to.to_cc; 1317 tp->t_starttime = ticks; 1318 if (tp->t_flags & TF_NEEDFIN) { 1319 tp->t_state = TCPS_FIN_WAIT_1; 1320 tp->t_flags &= ~TF_NEEDFIN; 1321 } else { 1322 tp->t_state = TCPS_ESTABLISHED; 1323 callout_reset(tp->tt_keep, 1324 tcp_keepidle, 1325 tcp_timer_keep, 1326 tp); 1327 } 1328 tp->t_flags |= TF_NEEDSYN; 1329 } else 1330 tp->t_state = TCPS_SYN_RECEIVED; 1331 } else { 1332 /* CC.NEW or no option => invalidate cache */ 1333 taop->tao_cc = 0; 1334 tp->t_state = TCPS_SYN_RECEIVED; 1335 } 1336 } 1337 1338trimthenstep6: 1339 /* 1340 * Advance th->th_seq to correspond to first data byte. 1341 * If data, trim to stay within window, 1342 * dropping FIN if necessary. 1343 */ 1344 th->th_seq++; 1345 if (tlen > tp->rcv_wnd) { 1346 todrop = tlen - tp->rcv_wnd; 1347 m_adj(m, -todrop); 1348 tlen = tp->rcv_wnd; 1349 thflags &= ~TH_FIN; 1350 tcpstat.tcps_rcvpackafterwin++; 1351 tcpstat.tcps_rcvbyteafterwin += todrop; 1352 } 1353 tp->snd_wl1 = th->th_seq - 1; 1354 tp->rcv_up = th->th_seq; 1355 /* 1356 * Client side of transaction: already sent SYN and data. 1357 * If the remote host used T/TCP to validate the SYN, 1358 * our data will be ACK'd; if so, enter normal data segment 1359 * processing in the middle of step 5, ack processing. 1360 * Otherwise, goto step 6. 1361 */ 1362 if (thflags & TH_ACK) 1363 goto process_ACK; 1364 goto step6; 1365 /* 1366 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1367 * if segment contains a SYN and CC [not CC.NEW] option: 1368 * if state == TIME_WAIT and connection duration > MSL, 1369 * drop packet and send RST; 1370 * 1371 * if SEG.CC > CCrecv then is new SYN, and can implicitly 1372 * ack the FIN (and data) in retransmission queue. 1373 * Complete close and delete TCPCB. Then reprocess 1374 * segment, hoping to find new TCPCB in LISTEN state; 1375 * 1376 * else must be old SYN; drop it. 1377 * else do normal processing. 1378 */ 1379 case TCPS_LAST_ACK: 1380 case TCPS_CLOSING: 1381 case TCPS_TIME_WAIT: 1382 if ((thflags & TH_SYN) && 1383 (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 1384 if (tp->t_state == TCPS_TIME_WAIT && 1385 (ticks - tp->t_starttime) > tcp_msl) 1386 goto dropwithreset; 1387 if (CC_GT(to.to_cc, tp->cc_recv)) { 1388 tp = tcp_close(tp); 1389 goto findpcb; 1390 } 1391 else 1392 goto drop; 1393 } 1394 break; /* continue normal processing */ 1395 } 1396 1397 /* 1398 * States other than LISTEN or SYN_SENT. 1399 * First check the RST flag and sequence number since reset segments 1400 * are exempt from the timestamp and connection count tests. This 1401 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1402 * below which allowed reset segments in half the sequence space 1403 * to fall though and be processed (which gives forged reset 1404 * segments with a random sequence number a 50 percent chance of 1405 * killing a connection). 1406 * Then check timestamp, if present. 1407 * Then check the connection count, if present. 1408 * Then check that at least some bytes of segment are within 1409 * receive window. If segment begins before rcv_nxt, 1410 * drop leading data (and SYN); if nothing left, just ack. 1411 * 1412 * 1413 * If the RST bit is set, check the sequence number to see 1414 * if this is a valid reset segment. 1415 * RFC 793 page 37: 1416 * In all states except SYN-SENT, all reset (RST) segments 1417 * are validated by checking their SEQ-fields. A reset is 1418 * valid if its sequence number is in the window. 1419 * Note: this does not take into account delayed ACKs, so 1420 * we should test against last_ack_sent instead of rcv_nxt. 1421 * The sequence number in the reset segment is normally an 1422 * echo of our outgoing acknowlegement numbers, but some hosts 1423 * send a reset with the sequence number at the rightmost edge 1424 * of our receive window, and we have to handle this case. 1425 * If we have multiple segments in flight, the intial reset 1426 * segment sequence numbers will be to the left of last_ack_sent, 1427 * but they will eventually catch up. 1428 * In any case, it never made sense to trim reset segments to 1429 * fit the receive window since RFC 1122 says: 1430 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1431 * 1432 * A TCP SHOULD allow a received RST segment to include data. 1433 * 1434 * DISCUSSION 1435 * It has been suggested that a RST segment could contain 1436 * ASCII text that encoded and explained the cause of the 1437 * RST. No standard has yet been established for such 1438 * data. 1439 * 1440 * If the reset segment passes the sequence number test examine 1441 * the state: 1442 * SYN_RECEIVED STATE: 1443 * If passive open, return to LISTEN state. 1444 * If active open, inform user that connection was refused. 1445 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1446 * Inform user that connection was reset, and close tcb. 1447 * CLOSING, LAST_ACK STATES: 1448 * Close the tcb. 1449 * TIME_WAIT STATE: 1450 * Drop the segment - see Stevens, vol. 2, p. 964 and 1451 * RFC 1337. 1452 */ 1453 if (thflags & TH_RST) { 1454 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1455 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1456 switch (tp->t_state) { 1457 1458 case TCPS_SYN_RECEIVED: 1459 so->so_error = ECONNREFUSED; 1460 goto close; 1461 1462 case TCPS_ESTABLISHED: 1463 case TCPS_FIN_WAIT_1: 1464 case TCPS_FIN_WAIT_2: 1465 case TCPS_CLOSE_WAIT: 1466 so->so_error = ECONNRESET; 1467 close: 1468 tp->t_state = TCPS_CLOSED; 1469 tcpstat.tcps_drops++; 1470 tp = tcp_close(tp); 1471 break; 1472 1473 case TCPS_CLOSING: 1474 case TCPS_LAST_ACK: 1475 tp = tcp_close(tp); 1476 break; 1477 1478 case TCPS_TIME_WAIT: 1479 break; 1480 } 1481 } 1482 goto drop; 1483 } 1484 1485 /* 1486 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1487 * and it's less than ts_recent, drop it. 1488 */ 1489 if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent && 1490 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1491 1492 /* Check to see if ts_recent is over 24 days old. */ 1493 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1494 /* 1495 * Invalidate ts_recent. If this segment updates 1496 * ts_recent, the age will be reset later and ts_recent 1497 * will get a valid value. If it does not, setting 1498 * ts_recent to zero will at least satisfy the 1499 * requirement that zero be placed in the timestamp 1500 * echo reply when ts_recent isn't valid. The 1501 * age isn't reset until we get a valid ts_recent 1502 * because we don't want out-of-order segments to be 1503 * dropped when ts_recent is old. 1504 */ 1505 tp->ts_recent = 0; 1506 } else { 1507 tcpstat.tcps_rcvduppack++; 1508 tcpstat.tcps_rcvdupbyte += tlen; 1509 tcpstat.tcps_pawsdrop++; 1510 goto dropafterack; 1511 } 1512 } 1513 1514 /* 1515 * T/TCP mechanism 1516 * If T/TCP was negotiated and the segment doesn't have CC, 1517 * or if its CC is wrong then drop the segment. 1518 * RST segments do not have to comply with this. 1519 */ 1520 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1521 ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1522 goto dropafterack; 1523 1524 /* 1525 * In the SYN-RECEIVED state, validate that the packet belongs to 1526 * this connection before trimming the data to fit the receive 1527 * window. Check the sequence number versus IRS since we know 1528 * the sequence numbers haven't wrapped. This is a partial fix 1529 * for the "LAND" DoS attack. 1530 */ 1531 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) 1532 goto dropwithreset; 1533 1534 todrop = tp->rcv_nxt - th->th_seq; 1535 if (todrop > 0) { 1536 if (thflags & TH_SYN) { 1537 thflags &= ~TH_SYN; 1538 th->th_seq++; 1539 if (th->th_urp > 1) 1540 th->th_urp--; 1541 else 1542 thflags &= ~TH_URG; 1543 todrop--; 1544 } 1545 /* 1546 * Following if statement from Stevens, vol. 2, p. 960. 1547 */ 1548 if (todrop > tlen 1549 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1550 /* 1551 * Any valid FIN must be to the left of the window. 1552 * At this point the FIN must be a duplicate or out 1553 * of sequence; drop it. 1554 */ 1555 thflags &= ~TH_FIN; 1556 1557 /* 1558 * Send an ACK to resynchronize and drop any data. 1559 * But keep on processing for RST or ACK. 1560 */ 1561 tp->t_flags |= TF_ACKNOW; 1562 todrop = tlen; 1563 tcpstat.tcps_rcvduppack++; 1564 tcpstat.tcps_rcvdupbyte += todrop; 1565 } else { 1566 tcpstat.tcps_rcvpartduppack++; 1567 tcpstat.tcps_rcvpartdupbyte += todrop; 1568 } 1569 drop_hdrlen += todrop; /* drop from the top afterwards */ 1570 th->th_seq += todrop; 1571 tlen -= todrop; 1572 if (th->th_urp > todrop) 1573 th->th_urp -= todrop; 1574 else { 1575 thflags &= ~TH_URG; 1576 th->th_urp = 0; 1577 } 1578 } 1579 1580 /* 1581 * If new data are received on a connection after the 1582 * user processes are gone, then RST the other end. 1583 */ 1584 if ((so->so_state & SS_NOFDREF) && 1585 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1586 tp = tcp_close(tp); 1587 tcpstat.tcps_rcvafterclose++; 1588 goto dropwithreset; 1589 } 1590 1591 /* 1592 * If segment ends after window, drop trailing data 1593 * (and PUSH and FIN); if nothing left, just ACK. 1594 */ 1595 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1596 if (todrop > 0) { 1597 tcpstat.tcps_rcvpackafterwin++; 1598 if (todrop >= tlen) { 1599 tcpstat.tcps_rcvbyteafterwin += tlen; 1600 /* 1601 * If a new connection request is received 1602 * while in TIME_WAIT, drop the old connection 1603 * and start over if the sequence numbers 1604 * are above the previous ones. 1605 */ 1606 if (thflags & TH_SYN && 1607 tp->t_state == TCPS_TIME_WAIT && 1608 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1609 iss = tp->snd_nxt + TCP_ISSINCR; 1610 tp = tcp_close(tp); 1611 goto findpcb; 1612 } 1613 /* 1614 * If window is closed can only take segments at 1615 * window edge, and have to drop data and PUSH from 1616 * incoming segments. Continue processing, but 1617 * remember to ack. Otherwise, drop segment 1618 * and ack. 1619 */ 1620 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1621 tp->t_flags |= TF_ACKNOW; 1622 tcpstat.tcps_rcvwinprobe++; 1623 } else 1624 goto dropafterack; 1625 } else 1626 tcpstat.tcps_rcvbyteafterwin += todrop; 1627 m_adj(m, -todrop); 1628 tlen -= todrop; 1629 thflags &= ~(TH_PUSH|TH_FIN); 1630 } 1631 1632 /* 1633 * If last ACK falls within this segment's sequence numbers, 1634 * record its timestamp. 1635 * NOTE that the test is modified according to the latest 1636 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1637 */ 1638 if ((to.to_flag & TOF_TS) != 0 && 1639 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1640 tp->ts_recent_age = ticks; 1641 tp->ts_recent = to.to_tsval; 1642 } 1643 1644 /* 1645 * If a SYN is in the window, then this is an 1646 * error and we send an RST and drop the connection. 1647 */ 1648 if (thflags & TH_SYN) { 1649 tp = tcp_drop(tp, ECONNRESET); 1650 goto dropwithreset; 1651 } 1652 1653 /* 1654 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1655 * flag is on (half-synchronized state), then queue data for 1656 * later processing; else drop segment and return. 1657 */ 1658 if ((thflags & TH_ACK) == 0) { 1659 if (tp->t_state == TCPS_SYN_RECEIVED || 1660 (tp->t_flags & TF_NEEDSYN)) 1661 goto step6; 1662 else 1663 goto drop; 1664 } 1665 1666 /* 1667 * Ack processing. 1668 */ 1669 switch (tp->t_state) { 1670 1671 /* 1672 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1673 * ESTABLISHED state and continue processing. 1674 * The ACK was checked above. 1675 */ 1676 case TCPS_SYN_RECEIVED: 1677 1678 tcpstat.tcps_connects++; 1679 soisconnected(so); 1680 /* Do window scaling? */ 1681 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1682 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1683 tp->snd_scale = tp->requested_s_scale; 1684 tp->rcv_scale = tp->request_r_scale; 1685 } 1686 /* 1687 * Upon successful completion of 3-way handshake, 1688 * update cache.CC if it was undefined, pass any queued 1689 * data to the user, and advance state appropriately. 1690 */ 1691 if ((taop = tcp_gettaocache(inp)) != NULL && 1692 taop->tao_cc == 0) 1693 taop->tao_cc = tp->cc_recv; 1694 1695 /* 1696 * Make transitions: 1697 * SYN-RECEIVED -> ESTABLISHED 1698 * SYN-RECEIVED* -> FIN-WAIT-1 1699 */ 1700 tp->t_starttime = ticks; 1701 if (tp->t_flags & TF_NEEDFIN) { 1702 tp->t_state = TCPS_FIN_WAIT_1; 1703 tp->t_flags &= ~TF_NEEDFIN; 1704 } else { 1705 tp->t_state = TCPS_ESTABLISHED; 1706 callout_reset(tp->tt_keep, tcp_keepidle, 1707 tcp_timer_keep, tp); 1708 } 1709 /* 1710 * If segment contains data or ACK, will call tcp_reass() 1711 * later; if not, do so now to pass queued data to user. 1712 */ 1713 if (tlen == 0 && (thflags & TH_FIN) == 0) 1714 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1715 (struct mbuf *)0); 1716 tp->snd_wl1 = th->th_seq - 1; 1717 /* fall into ... */ 1718 1719 /* 1720 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1721 * ACKs. If the ack is in the range 1722 * tp->snd_una < th->th_ack <= tp->snd_max 1723 * then advance tp->snd_una to th->th_ack and drop 1724 * data from the retransmission queue. If this ACK reflects 1725 * more up to date window information we update our window information. 1726 */ 1727 case TCPS_ESTABLISHED: 1728 case TCPS_FIN_WAIT_1: 1729 case TCPS_FIN_WAIT_2: 1730 case TCPS_CLOSE_WAIT: 1731 case TCPS_CLOSING: 1732 case TCPS_LAST_ACK: 1733 case TCPS_TIME_WAIT: 1734 1735 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1736 if (tlen == 0 && tiwin == tp->snd_wnd) { 1737 tcpstat.tcps_rcvdupack++; 1738 /* 1739 * If we have outstanding data (other than 1740 * a window probe), this is a completely 1741 * duplicate ack (ie, window info didn't 1742 * change), the ack is the biggest we've 1743 * seen and we've seen exactly our rexmt 1744 * threshhold of them, assume a packet 1745 * has been dropped and retransmit it. 1746 * Kludge snd_nxt & the congestion 1747 * window so we send only this one 1748 * packet. 1749 * 1750 * We know we're losing at the current 1751 * window size so do congestion avoidance 1752 * (set ssthresh to half the current window 1753 * and pull our congestion window back to 1754 * the new ssthresh). 1755 * 1756 * Dup acks mean that packets have left the 1757 * network (they're now cached at the receiver) 1758 * so bump cwnd by the amount in the receiver 1759 * to keep a constant cwnd packets in the 1760 * network. 1761 */ 1762 if (!callout_active(tp->tt_rexmt) || 1763 th->th_ack != tp->snd_una) 1764 tp->t_dupacks = 0; 1765 else if (++tp->t_dupacks == tcprexmtthresh) { 1766 tcp_seq onxt = tp->snd_nxt; 1767 u_int win = 1768 min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1769 tp->t_maxseg; 1770 1771 if (win < 2) 1772 win = 2; 1773 tp->snd_ssthresh = win * tp->t_maxseg; 1774 callout_stop(tp->tt_rexmt); 1775 tp->t_rtttime = 0; 1776 tp->snd_nxt = th->th_ack; 1777 tp->snd_cwnd = tp->t_maxseg; 1778 (void) tcp_output(tp); 1779 tp->snd_cwnd = tp->snd_ssthresh + 1780 tp->t_maxseg * tp->t_dupacks; 1781 if (SEQ_GT(onxt, tp->snd_nxt)) 1782 tp->snd_nxt = onxt; 1783 goto drop; 1784 } else if (tp->t_dupacks > tcprexmtthresh) { 1785 tp->snd_cwnd += tp->t_maxseg; 1786 (void) tcp_output(tp); 1787 goto drop; 1788 } 1789 } else 1790 tp->t_dupacks = 0; 1791 break; 1792 } 1793 /* 1794 * If the congestion window was inflated to account 1795 * for the other side's cached packets, retract it. 1796 */ 1797 if (tp->t_dupacks >= tcprexmtthresh && 1798 tp->snd_cwnd > tp->snd_ssthresh) 1799 tp->snd_cwnd = tp->snd_ssthresh; 1800 tp->t_dupacks = 0; 1801 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1802 tcpstat.tcps_rcvacktoomuch++; 1803 goto dropafterack; 1804 } 1805 /* 1806 * If we reach this point, ACK is not a duplicate, 1807 * i.e., it ACKs something we sent. 1808 */ 1809 if (tp->t_flags & TF_NEEDSYN) { 1810 /* 1811 * T/TCP: Connection was half-synchronized, and our 1812 * SYN has been ACK'd (so connection is now fully 1813 * synchronized). Go to non-starred state, 1814 * increment snd_una for ACK of SYN, and check if 1815 * we can do window scaling. 1816 */ 1817 tp->t_flags &= ~TF_NEEDSYN; 1818 tp->snd_una++; 1819 /* Do window scaling? */ 1820 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1821 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1822 tp->snd_scale = tp->requested_s_scale; 1823 tp->rcv_scale = tp->request_r_scale; 1824 } 1825 } 1826 1827process_ACK: 1828 acked = th->th_ack - tp->snd_una; 1829 tcpstat.tcps_rcvackpack++; 1830 tcpstat.tcps_rcvackbyte += acked; 1831 1832 /* 1833 * If we just performed our first retransmit, and the ACK 1834 * arrives within our recovery window, then it was a mistake 1835 * to do the retransmit in the first place. Recover our 1836 * original cwnd and ssthresh, and proceed to transmit where 1837 * we left off. 1838 */ 1839 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1840 tp->snd_cwnd = tp->snd_cwnd_prev; 1841 tp->snd_ssthresh = tp->snd_ssthresh_prev; 1842 tp->snd_nxt = tp->snd_max; 1843 tp->t_badrxtwin = 0; /* XXX probably not required */ 1844 } 1845 1846 /* 1847 * If we have a timestamp reply, update smoothed 1848 * round trip time. If no timestamp is present but 1849 * transmit timer is running and timed sequence 1850 * number was acked, update smoothed round trip time. 1851 * Since we now have an rtt measurement, cancel the 1852 * timer backoff (cf., Phil Karn's retransmit alg.). 1853 * Recompute the initial retransmit timer. 1854 */ 1855 if (to.to_flag & TOF_TS) 1856 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1857 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 1858 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1859 1860 /* 1861 * If all outstanding data is acked, stop retransmit 1862 * timer and remember to restart (more output or persist). 1863 * If there is more data to be acked, restart retransmit 1864 * timer, using current (possibly backed-off) value. 1865 */ 1866 if (th->th_ack == tp->snd_max) { 1867 callout_stop(tp->tt_rexmt); 1868 needoutput = 1; 1869 } else if (!callout_active(tp->tt_persist)) 1870 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 1871 tcp_timer_rexmt, tp); 1872 1873 /* 1874 * If no data (only SYN) was ACK'd, 1875 * skip rest of ACK processing. 1876 */ 1877 if (acked == 0) 1878 goto step6; 1879 1880 /* 1881 * When new data is acked, open the congestion window. 1882 * If the window gives us less than ssthresh packets 1883 * in flight, open exponentially (maxseg per packet). 1884 * Otherwise open linearly: maxseg per window 1885 * (maxseg^2 / cwnd per packet). 1886 */ 1887 { 1888 register u_int cw = tp->snd_cwnd; 1889 register u_int incr = tp->t_maxseg; 1890 1891 if (cw > tp->snd_ssthresh) 1892 incr = incr * incr / cw; 1893 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN << tp->snd_scale); 1894 } 1895 if (acked > so->so_snd.sb_cc) { 1896 tp->snd_wnd -= so->so_snd.sb_cc; 1897 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1898 ourfinisacked = 1; 1899 } else { 1900 sbdrop(&so->so_snd, acked); 1901 tp->snd_wnd -= acked; 1902 ourfinisacked = 0; 1903 } 1904 sowwakeup(so); 1905 tp->snd_una = th->th_ack; 1906 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1907 tp->snd_nxt = tp->snd_una; 1908 1909 switch (tp->t_state) { 1910 1911 /* 1912 * In FIN_WAIT_1 STATE in addition to the processing 1913 * for the ESTABLISHED state if our FIN is now acknowledged 1914 * then enter FIN_WAIT_2. 1915 */ 1916 case TCPS_FIN_WAIT_1: 1917 if (ourfinisacked) { 1918 /* 1919 * If we can't receive any more 1920 * data, then closing user can proceed. 1921 * Starting the timer is contrary to the 1922 * specification, but if we don't get a FIN 1923 * we'll hang forever. 1924 */ 1925 if (so->so_state & SS_CANTRCVMORE) { 1926 soisdisconnected(so); 1927 callout_reset(tp->tt_2msl, tcp_maxidle, 1928 tcp_timer_2msl, tp); 1929 } 1930 tp->t_state = TCPS_FIN_WAIT_2; 1931 } 1932 break; 1933 1934 /* 1935 * In CLOSING STATE in addition to the processing for 1936 * the ESTABLISHED state if the ACK acknowledges our FIN 1937 * then enter the TIME-WAIT state, otherwise ignore 1938 * the segment. 1939 */ 1940 case TCPS_CLOSING: 1941 if (ourfinisacked) { 1942 tp->t_state = TCPS_TIME_WAIT; 1943 tcp_canceltimers(tp); 1944 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1945 if (tp->cc_recv != 0 && 1946 (ticks - tp->t_starttime) < tcp_msl) 1947 callout_reset(tp->tt_2msl, 1948 tp->t_rxtcur * 1949 TCPTV_TWTRUNC, 1950 tcp_timer_2msl, tp); 1951 else 1952 callout_reset(tp->tt_2msl, 2 * tcp_msl, 1953 tcp_timer_2msl, tp); 1954 soisdisconnected(so); 1955 } 1956 break; 1957 1958 /* 1959 * In LAST_ACK, we may still be waiting for data to drain 1960 * and/or to be acked, as well as for the ack of our FIN. 1961 * If our FIN is now acknowledged, delete the TCB, 1962 * enter the closed state and return. 1963 */ 1964 case TCPS_LAST_ACK: 1965 if (ourfinisacked) { 1966 tp = tcp_close(tp); 1967 goto drop; 1968 } 1969 break; 1970 1971 /* 1972 * In TIME_WAIT state the only thing that should arrive 1973 * is a retransmission of the remote FIN. Acknowledge 1974 * it and restart the finack timer. 1975 */ 1976 case TCPS_TIME_WAIT: 1977 callout_reset(tp->tt_2msl, 2 * tcp_msl, 1978 tcp_timer_2msl, tp); 1979 goto dropafterack; 1980 } 1981 } 1982 1983step6: 1984 /* 1985 * Update window information. 1986 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1987 */ 1988 if ((thflags & TH_ACK) && 1989 (SEQ_LT(tp->snd_wl1, th->th_seq) || 1990 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 1991 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 1992 /* keep track of pure window updates */ 1993 if (tlen == 0 && 1994 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 1995 tcpstat.tcps_rcvwinupd++; 1996 tp->snd_wnd = tiwin; 1997 tp->snd_wl1 = th->th_seq; 1998 tp->snd_wl2 = th->th_ack; 1999 if (tp->snd_wnd > tp->max_sndwnd) 2000 tp->max_sndwnd = tp->snd_wnd; 2001 needoutput = 1; 2002 } 2003 2004 /* 2005 * Process segments with URG. 2006 */ 2007 if ((thflags & TH_URG) && th->th_urp && 2008 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2009 /* 2010 * This is a kludge, but if we receive and accept 2011 * random urgent pointers, we'll crash in 2012 * soreceive. It's hard to imagine someone 2013 * actually wanting to send this much urgent data. 2014 */ 2015 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2016 th->th_urp = 0; /* XXX */ 2017 thflags &= ~TH_URG; /* XXX */ 2018 goto dodata; /* XXX */ 2019 } 2020 /* 2021 * If this segment advances the known urgent pointer, 2022 * then mark the data stream. This should not happen 2023 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2024 * a FIN has been received from the remote side. 2025 * In these states we ignore the URG. 2026 * 2027 * According to RFC961 (Assigned Protocols), 2028 * the urgent pointer points to the last octet 2029 * of urgent data. We continue, however, 2030 * to consider it to indicate the first octet 2031 * of data past the urgent section as the original 2032 * spec states (in one of two places). 2033 */ 2034 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2035 tp->rcv_up = th->th_seq + th->th_urp; 2036 so->so_oobmark = so->so_rcv.sb_cc + 2037 (tp->rcv_up - tp->rcv_nxt) - 1; 2038 if (so->so_oobmark == 0) 2039 so->so_state |= SS_RCVATMARK; 2040 sohasoutofband(so); 2041 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2042 } 2043 /* 2044 * Remove out of band data so doesn't get presented to user. 2045 * This can happen independent of advancing the URG pointer, 2046 * but if two URG's are pending at once, some out-of-band 2047 * data may creep in... ick. 2048 */ 2049 if (th->th_urp <= (u_long)tlen 2050#ifdef SO_OOBINLINE 2051 && (so->so_options & SO_OOBINLINE) == 0 2052#endif 2053 ) 2054 tcp_pulloutofband(so, th, m, 2055 drop_hdrlen); /* hdr drop is delayed */ 2056 } else 2057 /* 2058 * If no out of band data is expected, 2059 * pull receive urgent pointer along 2060 * with the receive window. 2061 */ 2062 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2063 tp->rcv_up = tp->rcv_nxt; 2064dodata: /* XXX */ 2065 2066 /* 2067 * Process the segment text, merging it into the TCP sequencing queue, 2068 * and arranging for acknowledgment of receipt if necessary. 2069 * This process logically involves adjusting tp->rcv_wnd as data 2070 * is presented to the user (this happens in tcp_usrreq.c, 2071 * case PRU_RCVD). If a FIN has already been received on this 2072 * connection then we just ignore the text. 2073 */ 2074 if ((tlen || (thflags&TH_FIN)) && 2075 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2076 m_adj(m, drop_hdrlen); /* delayed header drop */ 2077 TCP_REASS(tp, th, &tlen, m, so, thflags); 2078 /* 2079 * Note the amount of data that peer has sent into 2080 * our window, in order to estimate the sender's 2081 * buffer size. 2082 */ 2083 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2084 } else { 2085 m_freem(m); 2086 thflags &= ~TH_FIN; 2087 } 2088 2089 /* 2090 * If FIN is received ACK the FIN and let the user know 2091 * that the connection is closing. 2092 */ 2093 if (thflags & TH_FIN) { 2094 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2095 socantrcvmore(so); 2096 /* 2097 * If connection is half-synchronized 2098 * (ie NEEDSYN flag on) then delay ACK, 2099 * so it may be piggybacked when SYN is sent. 2100 * Otherwise, since we received a FIN then no 2101 * more input can be expected, send ACK now. 2102 */ 2103 if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN)) 2104 callout_reset(tp->tt_delack, tcp_delacktime, 2105 tcp_timer_delack, tp); 2106 else 2107 tp->t_flags |= TF_ACKNOW; 2108 tp->rcv_nxt++; 2109 } 2110 switch (tp->t_state) { 2111 2112 /* 2113 * In SYN_RECEIVED and ESTABLISHED STATES 2114 * enter the CLOSE_WAIT state. 2115 */ 2116 case TCPS_SYN_RECEIVED: 2117 tp->t_starttime = ticks; 2118 /*FALLTHROUGH*/ 2119 case TCPS_ESTABLISHED: 2120 tp->t_state = TCPS_CLOSE_WAIT; 2121 break; 2122 2123 /* 2124 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2125 * enter the CLOSING state. 2126 */ 2127 case TCPS_FIN_WAIT_1: 2128 tp->t_state = TCPS_CLOSING; 2129 break; 2130 2131 /* 2132 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2133 * starting the time-wait timer, turning off the other 2134 * standard timers. 2135 */ 2136 case TCPS_FIN_WAIT_2: 2137 tp->t_state = TCPS_TIME_WAIT; 2138 tcp_canceltimers(tp); 2139 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 2140 if (tp->cc_recv != 0 && 2141 (ticks - tp->t_starttime) < tcp_msl) { 2142 callout_reset(tp->tt_2msl, 2143 tp->t_rxtcur * TCPTV_TWTRUNC, 2144 tcp_timer_2msl, tp); 2145 /* For transaction client, force ACK now. */ 2146 tp->t_flags |= TF_ACKNOW; 2147 } 2148 else 2149 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2150 tcp_timer_2msl, tp); 2151 soisdisconnected(so); 2152 break; 2153 2154 /* 2155 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2156 */ 2157 case TCPS_TIME_WAIT: 2158 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2159 tcp_timer_2msl, tp); 2160 break; 2161 } 2162 } 2163#ifdef TCPDEBUG 2164 if (so->so_options & SO_DEBUG) 2165 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2166 &tcp_savetcp, 0); 2167#endif 2168 2169 /* 2170 * Return any desired output. 2171 */ 2172 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2173 (void) tcp_output(tp); 2174 return; 2175 2176dropafterack: 2177 /* 2178 * Generate an ACK dropping incoming segment if it occupies 2179 * sequence space, where the ACK reflects our state. 2180 * 2181 * We can now skip the test for the RST flag since all 2182 * paths to this code happen after packets containing 2183 * RST have been dropped. 2184 * 2185 * In the SYN-RECEIVED state, don't send an ACK unless the 2186 * segment we received passes the SYN-RECEIVED ACK test. 2187 * If it fails send a RST. This breaks the loop in the 2188 * "LAND" DoS attack, and also prevents an ACK storm 2189 * between two listening ports that have been sent forged 2190 * SYN segments, each with the source address of the other. 2191 */ 2192 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2193 (SEQ_GT(tp->snd_una, th->th_ack) || 2194 SEQ_GT(th->th_ack, tp->snd_max)) ) 2195 goto dropwithreset; 2196#ifdef TCPDEBUG 2197 if (so->so_options & SO_DEBUG) 2198 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2199 &tcp_savetcp, 0); 2200#endif 2201 m_freem(m); 2202 tp->t_flags |= TF_ACKNOW; 2203 (void) tcp_output(tp); 2204 return; 2205 2206dropwithreset: 2207#ifdef TCP_RESTRICT_RST 2208 if (restrict_rst) 2209 goto drop; 2210#endif 2211 /* 2212 * Generate a RST, dropping incoming segment. 2213 * Make ACK acceptable to originator of segment. 2214 * Don't bother to respond if destination was broadcast/multicast. 2215 */ 2216 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2217 goto drop; 2218#ifdef INET6 2219 if (isipv6) { 2220 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 2221 goto drop; 2222 } else 2223#endif /* INET6 */ 2224 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) 2225 goto drop; 2226 /* IPv6 anycast check is done at tcp6_input() */ 2227#ifdef TCPDEBUG 2228 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2229 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2230 &tcp_savetcp, 0); 2231#endif 2232 if (thflags & TH_ACK) 2233 /* mtod() below is safe as long as hdr dropping is delayed */ 2234 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2235 TH_RST); 2236 else { 2237 if (thflags & TH_SYN) 2238 tlen++; 2239 /* mtod() below is safe as long as hdr dropping is delayed */ 2240 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2241 (tcp_seq)0, TH_RST|TH_ACK); 2242 } 2243 /* destroy temporarily created socket */ 2244 if (dropsocket) 2245 (void) soabort(so); 2246 return; 2247 2248drop: 2249 /* 2250 * Drop space held by incoming segment and return. 2251 */ 2252#ifdef TCPDEBUG 2253 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2254 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2255 &tcp_savetcp, 0); 2256#endif 2257 m_freem(m); 2258 /* destroy temporarily created socket */ 2259 if (dropsocket) 2260 (void) soabort(so); 2261 return; 2262} 2263 2264static void 2265tcp_dooptions(tp, cp, cnt, th, to) 2266 struct tcpcb *tp; 2267 u_char *cp; 2268 int cnt; 2269 struct tcphdr *th; 2270 struct tcpopt *to; 2271{ 2272 u_short mss = 0; 2273 int opt, optlen; 2274 2275 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2276 opt = cp[0]; 2277 if (opt == TCPOPT_EOL) 2278 break; 2279 if (opt == TCPOPT_NOP) 2280 optlen = 1; 2281 else { 2282 optlen = cp[1]; 2283 if (optlen <= 0) 2284 break; 2285 } 2286 switch (opt) { 2287 2288 default: 2289 continue; 2290 2291 case TCPOPT_MAXSEG: 2292 if (optlen != TCPOLEN_MAXSEG) 2293 continue; 2294 if (!(th->th_flags & TH_SYN)) 2295 continue; 2296 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 2297 NTOHS(mss); 2298 break; 2299 2300 case TCPOPT_WINDOW: 2301 if (optlen != TCPOLEN_WINDOW) 2302 continue; 2303 if (!(th->th_flags & TH_SYN)) 2304 continue; 2305 tp->t_flags |= TF_RCVD_SCALE; 2306 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2307 break; 2308 2309 case TCPOPT_TIMESTAMP: 2310 if (optlen != TCPOLEN_TIMESTAMP) 2311 continue; 2312 to->to_flag |= TOF_TS; 2313 bcopy((char *)cp + 2, 2314 (char *)&to->to_tsval, sizeof(to->to_tsval)); 2315 NTOHL(to->to_tsval); 2316 bcopy((char *)cp + 6, 2317 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2318 NTOHL(to->to_tsecr); 2319 2320 /* 2321 * A timestamp received in a SYN makes 2322 * it ok to send timestamp requests and replies. 2323 */ 2324 if (th->th_flags & TH_SYN) { 2325 tp->t_flags |= TF_RCVD_TSTMP; 2326 tp->ts_recent = to->to_tsval; 2327 tp->ts_recent_age = ticks; 2328 } 2329 break; 2330 case TCPOPT_CC: 2331 if (optlen != TCPOLEN_CC) 2332 continue; 2333 to->to_flag |= TOF_CC; 2334 bcopy((char *)cp + 2, 2335 (char *)&to->to_cc, sizeof(to->to_cc)); 2336 NTOHL(to->to_cc); 2337 /* 2338 * A CC or CC.new option received in a SYN makes 2339 * it ok to send CC in subsequent segments. 2340 */ 2341 if (th->th_flags & TH_SYN) 2342 tp->t_flags |= TF_RCVD_CC; 2343 break; 2344 case TCPOPT_CCNEW: 2345 if (optlen != TCPOLEN_CC) 2346 continue; 2347 if (!(th->th_flags & TH_SYN)) 2348 continue; 2349 to->to_flag |= TOF_CCNEW; 2350 bcopy((char *)cp + 2, 2351 (char *)&to->to_cc, sizeof(to->to_cc)); 2352 NTOHL(to->to_cc); 2353 /* 2354 * A CC or CC.new option received in a SYN makes 2355 * it ok to send CC in subsequent segments. 2356 */ 2357 tp->t_flags |= TF_RCVD_CC; 2358 break; 2359 case TCPOPT_CCECHO: 2360 if (optlen != TCPOLEN_CC) 2361 continue; 2362 if (!(th->th_flags & TH_SYN)) 2363 continue; 2364 to->to_flag |= TOF_CCECHO; 2365 bcopy((char *)cp + 2, 2366 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2367 NTOHL(to->to_ccecho); 2368 break; 2369 } 2370 } 2371 if (th->th_flags & TH_SYN) 2372 tcp_mss(tp, mss); /* sets t_maxseg */ 2373} 2374 2375/* 2376 * Pull out of band byte out of a segment so 2377 * it doesn't appear in the user's data queue. 2378 * It is still reflected in the segment length for 2379 * sequencing purposes. 2380 */ 2381static void 2382tcp_pulloutofband(so, th, m, off) 2383 struct socket *so; 2384 struct tcphdr *th; 2385 register struct mbuf *m; 2386 int off; /* delayed to be droped hdrlen */ 2387{ 2388 int cnt = off + th->th_urp - 1; 2389 2390 while (cnt >= 0) { 2391 if (m->m_len > cnt) { 2392 char *cp = mtod(m, caddr_t) + cnt; 2393 struct tcpcb *tp = sototcpcb(so); 2394 2395 tp->t_iobc = *cp; 2396 tp->t_oobflags |= TCPOOB_HAVEDATA; 2397 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2398 m->m_len--; 2399 return; 2400 } 2401 cnt -= m->m_len; 2402 m = m->m_next; 2403 if (m == 0) 2404 break; 2405 } 2406 panic("tcp_pulloutofband"); 2407} 2408 2409/* 2410 * Collect new round-trip time estimate 2411 * and update averages and current timeout. 2412 */ 2413static void 2414tcp_xmit_timer(tp, rtt) 2415 register struct tcpcb *tp; 2416 int rtt; 2417{ 2418 register int delta; 2419 2420 tcpstat.tcps_rttupdated++; 2421 tp->t_rttupdated++; 2422 if (tp->t_srtt != 0) { 2423 /* 2424 * srtt is stored as fixed point with 5 bits after the 2425 * binary point (i.e., scaled by 8). The following magic 2426 * is equivalent to the smoothing algorithm in rfc793 with 2427 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2428 * point). Adjust rtt to origin 0. 2429 */ 2430 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2431 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2432 2433 if ((tp->t_srtt += delta) <= 0) 2434 tp->t_srtt = 1; 2435 2436 /* 2437 * We accumulate a smoothed rtt variance (actually, a 2438 * smoothed mean difference), then set the retransmit 2439 * timer to smoothed rtt + 4 times the smoothed variance. 2440 * rttvar is stored as fixed point with 4 bits after the 2441 * binary point (scaled by 16). The following is 2442 * equivalent to rfc793 smoothing with an alpha of .75 2443 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2444 * rfc793's wired-in beta. 2445 */ 2446 if (delta < 0) 2447 delta = -delta; 2448 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2449 if ((tp->t_rttvar += delta) <= 0) 2450 tp->t_rttvar = 1; 2451 } else { 2452 /* 2453 * No rtt measurement yet - use the unsmoothed rtt. 2454 * Set the variance to half the rtt (so our first 2455 * retransmit happens at 3*rtt). 2456 */ 2457 tp->t_srtt = rtt << TCP_RTT_SHIFT; 2458 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2459 } 2460 tp->t_rtttime = 0; 2461 tp->t_rxtshift = 0; 2462 2463 /* 2464 * the retransmit should happen at rtt + 4 * rttvar. 2465 * Because of the way we do the smoothing, srtt and rttvar 2466 * will each average +1/2 tick of bias. When we compute 2467 * the retransmit timer, we want 1/2 tick of rounding and 2468 * 1 extra tick because of +-1/2 tick uncertainty in the 2469 * firing of the timer. The bias will give us exactly the 2470 * 1.5 tick we need. But, because the bias is 2471 * statistical, we have to test that we don't drop below 2472 * the minimum feasible timer (which is 2 ticks). 2473 */ 2474 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 2475 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2476 2477 /* 2478 * We received an ack for a packet that wasn't retransmitted; 2479 * it is probably safe to discard any error indications we've 2480 * received recently. This isn't quite right, but close enough 2481 * for now (a route might have failed after we sent a segment, 2482 * and the return path might not be symmetrical). 2483 */ 2484 tp->t_softerror = 0; 2485} 2486 2487/* 2488 * Determine a reasonable value for maxseg size. 2489 * If the route is known, check route for mtu. 2490 * If none, use an mss that can be handled on the outgoing 2491 * interface without forcing IP to fragment; if bigger than 2492 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2493 * to utilize large mbufs. If no route is found, route has no mtu, 2494 * or the destination isn't local, use a default, hopefully conservative 2495 * size (usually 512 or the default IP max size, but no more than the mtu 2496 * of the interface), as we can't discover anything about intervening 2497 * gateways or networks. We also initialize the congestion/slow start 2498 * window to be a single segment if the destination isn't local. 2499 * While looking at the routing entry, we also initialize other path-dependent 2500 * parameters from pre-set or cached values in the routing entry. 2501 * 2502 * Also take into account the space needed for options that we 2503 * send regularly. Make maxseg shorter by that amount to assure 2504 * that we can send maxseg amount of data even when the options 2505 * are present. Store the upper limit of the length of options plus 2506 * data in maxopd. 2507 * 2508 * NOTE that this routine is only called when we process an incoming 2509 * segment, for outgoing segments only tcp_mssopt is called. 2510 * 2511 * In case of T/TCP, we call this routine during implicit connection 2512 * setup as well (offer = -1), to initialize maxseg from the cached 2513 * MSS of our peer. 2514 */ 2515void 2516tcp_mss(tp, offer) 2517 struct tcpcb *tp; 2518 int offer; 2519{ 2520 register struct rtentry *rt; 2521 struct ifnet *ifp; 2522 register int rtt, mss; 2523 u_long bufsize; 2524 struct inpcb *inp; 2525 struct socket *so; 2526 struct rmxp_tao *taop; 2527 int origoffer = offer; 2528#ifdef INET6 2529 int isipv6; 2530 int min_protoh; 2531#endif 2532 2533 inp = tp->t_inpcb; 2534#ifdef INET6 2535 isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2536 min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2537 : sizeof (struct tcpiphdr); 2538#else 2539#define min_protoh (sizeof (struct tcpiphdr)) 2540#endif 2541#ifdef INET6 2542 if (isipv6) 2543 rt = tcp_rtlookup6(inp); 2544 else 2545#endif 2546 rt = tcp_rtlookup(inp); 2547 if (rt == NULL) { 2548 tp->t_maxopd = tp->t_maxseg = 2549#ifdef INET6 2550 isipv6 ? tcp_v6mssdflt : 2551#endif /* INET6 */ 2552 tcp_mssdflt; 2553 return; 2554 } 2555 ifp = rt->rt_ifp; 2556 so = inp->inp_socket; 2557 2558 taop = rmx_taop(rt->rt_rmx); 2559 /* 2560 * Offer == -1 means that we didn't receive SYN yet, 2561 * use cached value in that case; 2562 */ 2563 if (offer == -1) 2564 offer = taop->tao_mssopt; 2565 /* 2566 * Offer == 0 means that there was no MSS on the SYN segment, 2567 * in this case we use tcp_mssdflt. 2568 */ 2569 if (offer == 0) 2570 offer = 2571#ifdef INET6 2572 isipv6 ? tcp_v6mssdflt : 2573#endif /* INET6 */ 2574 tcp_mssdflt; 2575 else 2576 /* 2577 * Sanity check: make sure that maxopd will be large 2578 * enough to allow some data on segments even is the 2579 * all the option space is used (40bytes). Otherwise 2580 * funny things may happen in tcp_output. 2581 */ 2582 offer = max(offer, 64); 2583 taop->tao_mssopt = offer; 2584 2585 /* 2586 * While we're here, check if there's an initial rtt 2587 * or rttvar. Convert from the route-table units 2588 * to scaled multiples of the slow timeout timer. 2589 */ 2590 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2591 /* 2592 * XXX the lock bit for RTT indicates that the value 2593 * is also a minimum value; this is subject to time. 2594 */ 2595 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2596 tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 2597 tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2598 tcpstat.tcps_usedrtt++; 2599 if (rt->rt_rmx.rmx_rttvar) { 2600 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2601 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2602 tcpstat.tcps_usedrttvar++; 2603 } else { 2604 /* default variation is +- 1 rtt */ 2605 tp->t_rttvar = 2606 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2607 } 2608 TCPT_RANGESET(tp->t_rxtcur, 2609 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2610 tp->t_rttmin, TCPTV_REXMTMAX); 2611 } 2612 /* 2613 * if there's an mtu associated with the route, use it 2614 * else, use the link mtu. 2615 */ 2616 if (rt->rt_rmx.rmx_mtu) 2617 mss = rt->rt_rmx.rmx_mtu - min_protoh; 2618 else 2619 { 2620 mss = 2621#ifdef INET6 2622 (isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu : 2623#endif 2624 ifp->if_mtu 2625#ifdef INET6 2626 ) 2627#endif 2628 - min_protoh; 2629#ifdef INET6 2630 if (isipv6) { 2631 if (!in6_localaddr(&inp->in6p_faddr)) 2632 mss = min(mss, tcp_v6mssdflt); 2633 } else 2634#endif 2635 if (!in_localaddr(inp->inp_faddr)) 2636 mss = min(mss, tcp_mssdflt); 2637 } 2638 mss = min(mss, offer); 2639 /* 2640 * maxopd stores the maximum length of data AND options 2641 * in a segment; maxseg is the amount of data in a normal 2642 * segment. We need to store this value (maxopd) apart 2643 * from maxseg, because now every segment carries options 2644 * and thus we normally have somewhat less data in segments. 2645 */ 2646 tp->t_maxopd = mss; 2647 2648 /* 2649 * In case of T/TCP, origoffer==-1 indicates, that no segments 2650 * were received yet. In this case we just guess, otherwise 2651 * we do the same as before T/TCP. 2652 */ 2653 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2654 (origoffer == -1 || 2655 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2656 mss -= TCPOLEN_TSTAMP_APPA; 2657 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2658 (origoffer == -1 || 2659 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2660 mss -= TCPOLEN_CC_APPA; 2661 2662#if (MCLBYTES & (MCLBYTES - 1)) == 0 2663 if (mss > MCLBYTES) 2664 mss &= ~(MCLBYTES-1); 2665#else 2666 if (mss > MCLBYTES) 2667 mss = mss / MCLBYTES * MCLBYTES; 2668#endif 2669 /* 2670 * If there's a pipesize, change the socket buffer 2671 * to that size. Make the socket buffers an integral 2672 * number of mss units; if the mss is larger than 2673 * the socket buffer, decrease the mss. 2674 */ 2675#ifdef RTV_SPIPE 2676 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2677#endif 2678 bufsize = so->so_snd.sb_hiwat; 2679 if (bufsize < mss) 2680 mss = bufsize; 2681 else { 2682 bufsize = roundup(bufsize, mss); 2683 if (bufsize > sb_max) 2684 bufsize = sb_max; 2685 (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2686 } 2687 tp->t_maxseg = mss; 2688 2689#ifdef RTV_RPIPE 2690 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2691#endif 2692 bufsize = so->so_rcv.sb_hiwat; 2693 if (bufsize > mss) { 2694 bufsize = roundup(bufsize, mss); 2695 if (bufsize > sb_max) 2696 bufsize = sb_max; 2697 (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2698 } 2699 2700 /* 2701 * Set the slow-start flight size depending on whether this 2702 * is a local network or not. 2703 */ 2704 if ( 2705#ifdef INET6 2706 (isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2707 (!isipv6 && 2708#endif 2709 in_localaddr(inp->inp_faddr) 2710#ifdef INET6 2711 ) 2712#endif 2713 ) 2714 tp->snd_cwnd = mss * ss_fltsz_local; 2715 else 2716 tp->snd_cwnd = mss * ss_fltsz; 2717 2718 if (rt->rt_rmx.rmx_ssthresh) { 2719 /* 2720 * There's some sort of gateway or interface 2721 * buffer limit on the path. Use this to set 2722 * the slow start threshhold, but set the 2723 * threshold to no less than 2*mss. 2724 */ 2725 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2726 tcpstat.tcps_usedssthresh++; 2727 } 2728} 2729 2730/* 2731 * Determine the MSS option to send on an outgoing SYN. 2732 */ 2733int 2734tcp_mssopt(tp) 2735 struct tcpcb *tp; 2736{ 2737 struct rtentry *rt; 2738#ifdef INET6 2739 int isipv6; 2740 int min_protoh; 2741#endif 2742 2743#ifdef INET6 2744 isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2745 min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2746 : sizeof (struct tcpiphdr); 2747#else 2748#define min_protoh (sizeof (struct tcpiphdr)) 2749#endif 2750#ifdef INET6 2751 if (isipv6) 2752 rt = tcp_rtlookup6(tp->t_inpcb); 2753 else 2754#endif /* INET6 */ 2755 rt = tcp_rtlookup(tp->t_inpcb); 2756 if (rt == NULL) 2757 return 2758#ifdef INET6 2759 isipv6 ? tcp_v6mssdflt : 2760#endif /* INET6 */ 2761 tcp_mssdflt; 2762 2763 return rt->rt_ifp->if_mtu - min_protoh; 2764} 2765