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