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_tcpdebug.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/sysctl.h> 44 #include <sys/malloc.h> 45 #include <sys/mbuf.h> 46 #include <sys/proc.h> /* for proc0 declaration */ 47 #include <sys/protosw.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/syslog.h> 51 52 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 53 54 #include <net/if.h> 55 #include <net/route.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/ip.h> 60 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 61 #include <netinet/in_pcb.h> 62 #include <netinet/ip_var.h> 63 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 64 #include <netinet/tcp.h> 65 #include <netinet/tcp_fsm.h> 66 #include <netinet/tcp_seq.h> 67 #include <netinet/tcp_timer.h> 68 #include <netinet/tcp_var.h> 69 #include <netinet/tcpip.h> 70 #ifdef TCPDEBUG 71 #include <netinet/tcp_debug.h> 72 static struct tcpiphdr tcp_saveti; 73 #endif 74 75 static int tcprexmtthresh = 3; 76 tcp_seq tcp_iss; 77 tcp_cc tcp_ccgen; 78 79 struct tcpstat tcpstat; 80 SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD, 81 &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 82 83 static int log_in_vain = 0; 84 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 85 &log_in_vain, 0, "Log all incoming TCP connections"); 86 87 static int blackhole = 0; 88 SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 89 &blackhole, 0, "Do not send RST when dropping refused connections"); 90 91 int tcp_delack_enabled = 1; 92 SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 93 &tcp_delack_enabled, 0, 94 "Delay ACK to try and piggyback it onto a data packet"); 95 96 u_long tcp_now; 97 struct inpcbhead tcb; 98 struct inpcbinfo tcbinfo; 99 100 static void tcp_dooptions __P((struct tcpcb *, 101 u_char *, int, struct tcpiphdr *, struct tcpopt *)); 102 static void tcp_pulloutofband __P((struct socket *, 103 struct tcpiphdr *, struct mbuf *)); 104 static int tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *)); 105 static void tcp_xmit_timer __P((struct tcpcb *, int)); 106 107 108 /* 109 * Insert segment ti into reassembly queue of tcp with 110 * control block tp. Return TH_FIN if reassembly now includes 111 * a segment with FIN. The macro form does the common case inline 112 * (segment is the next to be received on an established connection, 113 * and the queue is empty), avoiding linkage into and removal 114 * from the queue and repetition of various conversions. 115 * Set DELACK for segments received in order, but ack immediately 116 * when segments are out of order (so fast retransmit can work). 117 */ 118 #define TCP_REASS(tp, ti, m, so, flags) { \ 119 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 120 (tp)->t_segq == NULL && \ 121 (tp)->t_state == TCPS_ESTABLISHED) { \ 122 if (tcp_delack_enabled) \ 123 tp->t_flags |= TF_DELACK; \ 124 else \ 125 tp->t_flags |= TF_ACKNOW; \ 126 (tp)->rcv_nxt += (ti)->ti_len; \ 127 flags = (ti)->ti_flags & TH_FIN; \ 128 tcpstat.tcps_rcvpack++;\ 129 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 130 sbappend(&(so)->so_rcv, (m)); \ 131 sorwakeup(so); \ 132 } else { \ 133 (flags) = tcp_reass((tp), (ti), (m)); \ 134 tp->t_flags |= TF_ACKNOW; \ 135 } \ 136 } 137 138 static int 139 tcp_reass(tp, ti, m) 140 register struct tcpcb *tp; 141 register struct tcpiphdr *ti; 142 struct mbuf *m; 143 { 144 struct mbuf *q; 145 struct mbuf *p; 146 struct mbuf *nq; 147 struct socket *so = tp->t_inpcb->inp_socket; 148 int flags; 149 150 #define GETTCP(m) ((struct tcpiphdr *)m->m_pkthdr.header) 151 152 /* 153 * Call with ti==0 after become established to 154 * force pre-ESTABLISHED data up to user socket. 155 */ 156 if (ti == 0) 157 goto present; 158 159 m->m_pkthdr.header = ti; 160 161 /* 162 * Find a segment which begins after this one does. 163 */ 164 for (q = tp->t_segq, p = NULL; q; p = q, q = q->m_nextpkt) 165 if (SEQ_GT(GETTCP(q)->ti_seq, ti->ti_seq)) 166 break; 167 168 /* 169 * If there is a preceding segment, it may provide some of 170 * our data already. If so, drop the data from the incoming 171 * segment. If it provides all of our data, drop us. 172 */ 173 if (p != NULL) { 174 register int i; 175 /* conversion to int (in i) handles seq wraparound */ 176 i = GETTCP(p)->ti_seq + GETTCP(p)->ti_len - ti->ti_seq; 177 if (i > 0) { 178 if (i >= ti->ti_len) { 179 tcpstat.tcps_rcvduppack++; 180 tcpstat.tcps_rcvdupbyte += ti->ti_len; 181 m_freem(m); 182 /* 183 * Try to present any queued data 184 * at the left window edge to the user. 185 * This is needed after the 3-WHS 186 * completes. 187 */ 188 goto present; /* ??? */ 189 } 190 m_adj(m, i); 191 ti->ti_len -= i; 192 ti->ti_seq += i; 193 } 194 } 195 tcpstat.tcps_rcvoopack++; 196 tcpstat.tcps_rcvoobyte += ti->ti_len; 197 198 /* 199 * While we overlap succeeding segments trim them or, 200 * if they are completely covered, dequeue them. 201 */ 202 while (q) { 203 register int i = (ti->ti_seq + ti->ti_len) - GETTCP(q)->ti_seq; 204 if (i <= 0) 205 break; 206 if (i < GETTCP(q)->ti_len) { 207 GETTCP(q)->ti_seq += i; 208 GETTCP(q)->ti_len -= i; 209 m_adj(q, i); 210 break; 211 } 212 213 nq = q->m_nextpkt; 214 if (p) 215 p->m_nextpkt = nq; 216 else 217 tp->t_segq = nq; 218 m_freem(q); 219 q = nq; 220 } 221 222 if (p == NULL) { 223 m->m_nextpkt = tp->t_segq; 224 tp->t_segq = m; 225 } else { 226 m->m_nextpkt = p->m_nextpkt; 227 p->m_nextpkt = m; 228 } 229 230 present: 231 /* 232 * Present data to user, advancing rcv_nxt through 233 * completed sequence space. 234 */ 235 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 236 return (0); 237 q = tp->t_segq; 238 if (!q || GETTCP(q)->ti_seq != tp->rcv_nxt) 239 return (0); 240 do { 241 tp->rcv_nxt += GETTCP(q)->ti_len; 242 flags = GETTCP(q)->ti_flags & TH_FIN; 243 nq = q->m_nextpkt; 244 tp->t_segq = nq; 245 q->m_nextpkt = NULL; 246 if (so->so_state & SS_CANTRCVMORE) 247 m_freem(q); 248 else 249 sbappend(&so->so_rcv, q); 250 q = nq; 251 } while (q && GETTCP(q)->ti_seq == tp->rcv_nxt); 252 sorwakeup(so); 253 return (flags); 254 255 #undef GETTCP 256 } 257 258 /* 259 * TCP input routine, follows pages 65-76 of the 260 * protocol specification dated September, 1981 very closely. 261 */ 262 void 263 tcp_input(m, iphlen) 264 register struct mbuf *m; 265 int iphlen; 266 { 267 register struct tcpiphdr *ti; 268 register struct inpcb *inp; 269 u_char *optp = NULL; 270 int optlen = 0; 271 int len, tlen, off; 272 register struct tcpcb *tp = 0; 273 register int tiflags; 274 struct socket *so = 0; 275 int todrop, acked, ourfinisacked, needoutput = 0; 276 struct in_addr laddr; 277 int dropsocket = 0; 278 int iss = 0; 279 u_long tiwin; 280 struct tcpopt to; /* options in this segment */ 281 struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 282 struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 283 #ifdef TCPDEBUG 284 short ostate = 0; 285 #endif 286 287 bzero((char *)&to, sizeof(to)); 288 289 tcpstat.tcps_rcvtotal++; 290 /* 291 * Get IP and TCP header together in first mbuf. 292 * Note: IP leaves IP header in first mbuf. 293 */ 294 ti = mtod(m, struct tcpiphdr *); 295 if (iphlen > sizeof (struct ip)) 296 ip_stripoptions(m, (struct mbuf *)0); 297 if (m->m_len < sizeof (struct tcpiphdr)) { 298 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 299 tcpstat.tcps_rcvshort++; 300 return; 301 } 302 ti = mtod(m, struct tcpiphdr *); 303 } 304 305 /* 306 * Checksum extended TCP header and data. 307 */ 308 tlen = ((struct ip *)ti)->ip_len; 309 len = sizeof (struct ip) + tlen; 310 bzero(ti->ti_x1, sizeof(ti->ti_x1)); 311 ti->ti_len = (u_short)tlen; 312 HTONS(ti->ti_len); 313 ti->ti_sum = in_cksum(m, len); 314 if (ti->ti_sum) { 315 tcpstat.tcps_rcvbadsum++; 316 goto drop; 317 } 318 319 /* 320 * Check that TCP offset makes sense, 321 * pull out TCP options and adjust length. XXX 322 */ 323 off = ti->ti_off << 2; 324 if (off < sizeof (struct tcphdr) || off > tlen) { 325 tcpstat.tcps_rcvbadoff++; 326 goto drop; 327 } 328 tlen -= off; 329 ti->ti_len = tlen; 330 if (off > sizeof (struct tcphdr)) { 331 if (m->m_len < sizeof(struct ip) + off) { 332 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 333 tcpstat.tcps_rcvshort++; 334 return; 335 } 336 ti = mtod(m, struct tcpiphdr *); 337 } 338 optlen = off - sizeof (struct tcphdr); 339 optp = mtod(m, u_char *) + sizeof (struct tcpiphdr); 340 } 341 tiflags = ti->ti_flags; 342 343 /* 344 * Convert TCP protocol specific fields to host format. 345 */ 346 NTOHL(ti->ti_seq); 347 NTOHL(ti->ti_ack); 348 NTOHS(ti->ti_win); 349 NTOHS(ti->ti_urp); 350 351 /* 352 * Drop TCP, IP headers and TCP options. 353 */ 354 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 355 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 356 357 /* 358 * Locate pcb for segment. 359 */ 360 findpcb: 361 #ifdef IPFIREWALL_FORWARD 362 if (ip_fw_fwd_addr != NULL) { 363 /* 364 * Diverted. Pretend to be the destination. 365 * already got one like this? 366 */ 367 inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport, 368 ti->ti_dst, ti->ti_dport, 0); 369 if (!inp) { 370 /* 371 * No, then it's new. Try find the ambushing socket 372 */ 373 if (!ip_fw_fwd_addr->sin_port) { 374 inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, 375 ti->ti_sport, ip_fw_fwd_addr->sin_addr, 376 ti->ti_dport, 1); 377 } else { 378 inp = in_pcblookup_hash(&tcbinfo, 379 ti->ti_src, ti->ti_sport, 380 ip_fw_fwd_addr->sin_addr, 381 ntohs(ip_fw_fwd_addr->sin_port), 1); 382 } 383 } 384 ip_fw_fwd_addr = NULL; 385 } else 386 #endif /* IPFIREWALL_FORWARD */ 387 388 inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport, 389 ti->ti_dst, ti->ti_dport, 1); 390 391 /* 392 * If the state is CLOSED (i.e., TCB does not exist) then 393 * all data in the incoming segment is discarded. 394 * If the TCB exists but is in CLOSED state, it is embryonic, 395 * but should either do a listen or a connect soon. 396 */ 397 if (inp == NULL) { 398 if (log_in_vain) { 399 char buf[4*sizeof "123"]; 400 401 strcpy(buf, inet_ntoa(ti->ti_dst)); 402 switch (log_in_vain) { 403 case 1: 404 if(tiflags & TH_SYN) 405 log(LOG_INFO, 406 "Connection attempt to TCP %s:%d from %s:%d\n", 407 buf, ntohs(ti->ti_dport), inet_ntoa(ti->ti_src), 408 ntohs(ti->ti_sport)); 409 break; 410 case 2: 411 log(LOG_INFO, 412 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n", 413 buf, ntohs(ti->ti_dport), inet_ntoa(ti->ti_src), 414 ntohs(ti->ti_sport), tiflags); 415 break; 416 default: 417 break; 418 } 419 } 420 #ifdef ICMP_BANDLIM 421 if (badport_bandlim(1) < 0) 422 goto drop; 423 #endif 424 if (blackhole) { 425 switch (blackhole) { 426 case 1: 427 if (tiflags & TH_SYN) 428 goto drop; 429 break; 430 case 2: 431 goto drop; 432 default: 433 goto drop; 434 } 435 } 436 goto dropwithreset; 437 } 438 tp = intotcpcb(inp); 439 if (tp == 0) 440 goto dropwithreset; 441 if (tp->t_state == TCPS_CLOSED) 442 goto drop; 443 444 /* Unscale the window into a 32-bit value. */ 445 if ((tiflags & TH_SYN) == 0) 446 tiwin = ti->ti_win << tp->snd_scale; 447 else 448 tiwin = ti->ti_win; 449 450 so = inp->inp_socket; 451 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 452 #ifdef TCPDEBUG 453 if (so->so_options & SO_DEBUG) { 454 ostate = tp->t_state; 455 tcp_saveti = *ti; 456 } 457 #endif 458 if (so->so_options & SO_ACCEPTCONN) { 459 register struct tcpcb *tp0 = tp; 460 struct socket *so2; 461 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 462 /* 463 * Note: dropwithreset makes sure we don't 464 * send a RST in response to a RST. 465 */ 466 if (tiflags & TH_ACK) { 467 tcpstat.tcps_badsyn++; 468 goto dropwithreset; 469 } 470 goto drop; 471 } 472 so2 = sonewconn(so, 0); 473 if (so2 == 0) { 474 tcpstat.tcps_listendrop++; 475 so2 = sodropablereq(so); 476 if (so2) { 477 tcp_drop(sototcpcb(so2), ETIMEDOUT); 478 so2 = sonewconn(so, 0); 479 } 480 if (!so2) 481 goto drop; 482 } 483 so = so2; 484 /* 485 * This is ugly, but .... 486 * 487 * Mark socket as temporary until we're 488 * committed to keeping it. The code at 489 * ``drop'' and ``dropwithreset'' check the 490 * flag dropsocket to see if the temporary 491 * socket created here should be discarded. 492 * We mark the socket as discardable until 493 * we're committed to it below in TCPS_LISTEN. 494 */ 495 dropsocket++; 496 inp = (struct inpcb *)so->so_pcb; 497 inp->inp_laddr = ti->ti_dst; 498 inp->inp_lport = ti->ti_dport; 499 if (in_pcbinshash(inp) != 0) { 500 /* 501 * Undo the assignments above if we failed to put 502 * the PCB on the hash lists. 503 */ 504 inp->inp_laddr.s_addr = INADDR_ANY; 505 inp->inp_lport = 0; 506 goto drop; 507 } 508 inp->inp_options = ip_srcroute(); 509 tp = intotcpcb(inp); 510 tp->t_state = TCPS_LISTEN; 511 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 512 513 /* Compute proper scaling value from buffer space */ 514 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 515 TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) 516 tp->request_r_scale++; 517 } 518 } 519 520 /* 521 * Segment received on connection. 522 * Reset idle time and keep-alive timer. 523 */ 524 tp->t_idle = 0; 525 if (TCPS_HAVEESTABLISHED(tp->t_state)) 526 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 527 528 /* 529 * Process options if not in LISTEN state, 530 * else do it below (after getting remote address). 531 */ 532 if (tp->t_state != TCPS_LISTEN) 533 tcp_dooptions(tp, optp, optlen, ti, &to); 534 535 /* 536 * Header prediction: check for the two common cases 537 * of a uni-directional data xfer. If the packet has 538 * no control flags, is in-sequence, the window didn't 539 * change and we're not retransmitting, it's a 540 * candidate. If the length is zero and the ack moved 541 * forward, we're the sender side of the xfer. Just 542 * free the data acked & wake any higher level process 543 * that was blocked waiting for space. If the length 544 * is non-zero and the ack didn't move, we're the 545 * receiver side. If we're getting packets in-order 546 * (the reassembly queue is empty), add the data to 547 * the socket buffer and note that we need a delayed ack. 548 * Make sure that the hidden state-flags are also off. 549 * Since we check for TCPS_ESTABLISHED above, it can only 550 * be TH_NEEDSYN. 551 */ 552 if (tp->t_state == TCPS_ESTABLISHED && 553 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 554 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 555 ((to.to_flag & TOF_TS) == 0 || 556 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 557 /* 558 * Using the CC option is compulsory if once started: 559 * the segment is OK if no T/TCP was negotiated or 560 * if the segment has a CC option equal to CCrecv 561 */ 562 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 563 ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 564 ti->ti_seq == tp->rcv_nxt && 565 tiwin && tiwin == tp->snd_wnd && 566 tp->snd_nxt == tp->snd_max) { 567 568 /* 569 * If last ACK falls within this segment's sequence numbers, 570 * record the timestamp. 571 * NOTE that the test is modified according to the latest 572 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 573 */ 574 if ((to.to_flag & TOF_TS) != 0 && 575 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 576 tp->ts_recent_age = tcp_now; 577 tp->ts_recent = to.to_tsval; 578 } 579 580 if (ti->ti_len == 0) { 581 if (SEQ_GT(ti->ti_ack, tp->snd_una) && 582 SEQ_LEQ(ti->ti_ack, tp->snd_max) && 583 tp->snd_cwnd >= tp->snd_wnd && 584 tp->t_dupacks < tcprexmtthresh) { 585 /* 586 * this is a pure ack for outstanding data. 587 */ 588 ++tcpstat.tcps_predack; 589 if ((to.to_flag & TOF_TS) != 0) 590 tcp_xmit_timer(tp, 591 tcp_now - to.to_tsecr + 1); 592 else if (tp->t_rtt && 593 SEQ_GT(ti->ti_ack, tp->t_rtseq)) 594 tcp_xmit_timer(tp, tp->t_rtt); 595 acked = ti->ti_ack - tp->snd_una; 596 tcpstat.tcps_rcvackpack++; 597 tcpstat.tcps_rcvackbyte += acked; 598 sbdrop(&so->so_snd, acked); 599 tp->snd_una = ti->ti_ack; 600 m_freem(m); 601 602 /* 603 * If all outstanding data are acked, stop 604 * retransmit timer, otherwise restart timer 605 * using current (possibly backed-off) value. 606 * If process is waiting for space, 607 * wakeup/selwakeup/signal. If data 608 * are ready to send, let tcp_output 609 * decide between more output or persist. 610 */ 611 if (tp->snd_una == tp->snd_max) 612 tp->t_timer[TCPT_REXMT] = 0; 613 else if (tp->t_timer[TCPT_PERSIST] == 0) 614 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 615 616 sowwakeup(so); 617 if (so->so_snd.sb_cc) 618 (void) tcp_output(tp); 619 return; 620 } 621 } else if (ti->ti_ack == tp->snd_una && 622 tp->t_segq == NULL && 623 ti->ti_len <= sbspace(&so->so_rcv)) { 624 /* 625 * this is a pure, in-sequence data packet 626 * with nothing on the reassembly queue and 627 * we have enough buffer space to take it. 628 */ 629 ++tcpstat.tcps_preddat; 630 tp->rcv_nxt += ti->ti_len; 631 tcpstat.tcps_rcvpack++; 632 tcpstat.tcps_rcvbyte += ti->ti_len; 633 /* 634 * Add data to socket buffer. 635 */ 636 sbappend(&so->so_rcv, m); 637 sorwakeup(so); 638 if (tcp_delack_enabled) { 639 tp->t_flags |= TF_DELACK; 640 } else { 641 tp->t_flags |= TF_ACKNOW; 642 tcp_output(tp); 643 } 644 return; 645 } 646 } 647 648 /* 649 * Calculate amount of space in receive window, 650 * and then do TCP input processing. 651 * Receive window is amount of space in rcv queue, 652 * but not less than advertised window. 653 */ 654 { int win; 655 656 win = sbspace(&so->so_rcv); 657 if (win < 0) 658 win = 0; 659 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 660 } 661 662 switch (tp->t_state) { 663 664 /* 665 * If the state is LISTEN then ignore segment if it contains an RST. 666 * If the segment contains an ACK then it is bad and send a RST. 667 * If it does not contain a SYN then it is not interesting; drop it. 668 * If it is from this socket, drop it, it must be forged. 669 * Don't bother responding if the destination was a broadcast. 670 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 671 * tp->iss, and send a segment: 672 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 673 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 674 * Fill in remote peer address fields if not previously specified. 675 * Enter SYN_RECEIVED state, and process any other fields of this 676 * segment in this state. 677 */ 678 case TCPS_LISTEN: { 679 register struct sockaddr_in *sin; 680 681 if (tiflags & TH_RST) 682 goto drop; 683 if (tiflags & TH_ACK) 684 goto dropwithreset; 685 if ((tiflags & TH_SYN) == 0) 686 goto drop; 687 if ((ti->ti_dport == ti->ti_sport) && 688 (ti->ti_dst.s_addr == ti->ti_src.s_addr)) 689 goto drop; 690 /* 691 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 692 * in_broadcast() should never return true on a received 693 * packet with M_BCAST not set. 694 */ 695 if (m->m_flags & (M_BCAST|M_MCAST) || 696 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 697 goto drop; 698 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 699 M_NOWAIT); 700 if (sin == NULL) 701 goto drop; 702 sin->sin_family = AF_INET; 703 sin->sin_len = sizeof(*sin); 704 sin->sin_addr = ti->ti_src; 705 sin->sin_port = ti->ti_sport; 706 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 707 laddr = inp->inp_laddr; 708 if (inp->inp_laddr.s_addr == INADDR_ANY) 709 inp->inp_laddr = ti->ti_dst; 710 if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) { 711 inp->inp_laddr = laddr; 712 FREE(sin, M_SONAME); 713 goto drop; 714 } 715 FREE(sin, M_SONAME); 716 tp->t_template = tcp_template(tp); 717 if (tp->t_template == 0) { 718 tp = tcp_drop(tp, ENOBUFS); 719 dropsocket = 0; /* socket is already gone */ 720 goto drop; 721 } 722 if ((taop = tcp_gettaocache(inp)) == NULL) { 723 taop = &tao_noncached; 724 bzero(taop, sizeof(*taop)); 725 } 726 tcp_dooptions(tp, optp, optlen, ti, &to); 727 if (iss) 728 tp->iss = iss; 729 else 730 tp->iss = tcp_iss; 731 tcp_iss += TCP_ISSINCR/4; 732 tp->irs = ti->ti_seq; 733 tcp_sendseqinit(tp); 734 tcp_rcvseqinit(tp); 735 /* 736 * Initialization of the tcpcb for transaction; 737 * set SND.WND = SEG.WND, 738 * initialize CCsend and CCrecv. 739 */ 740 tp->snd_wnd = tiwin; /* initial send-window */ 741 tp->cc_send = CC_INC(tcp_ccgen); 742 tp->cc_recv = to.to_cc; 743 /* 744 * Perform TAO test on incoming CC (SEG.CC) option, if any. 745 * - compare SEG.CC against cached CC from the same host, 746 * if any. 747 * - if SEG.CC > chached value, SYN must be new and is accepted 748 * immediately: save new CC in the cache, mark the socket 749 * connected, enter ESTABLISHED state, turn on flag to 750 * send a SYN in the next segment. 751 * A virtual advertised window is set in rcv_adv to 752 * initialize SWS prevention. Then enter normal segment 753 * processing: drop SYN, process data and FIN. 754 * - otherwise do a normal 3-way handshake. 755 */ 756 if ((to.to_flag & TOF_CC) != 0) { 757 if (((tp->t_flags & TF_NOPUSH) != 0) && 758 taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 759 760 taop->tao_cc = to.to_cc; 761 tp->t_state = TCPS_ESTABLISHED; 762 763 /* 764 * If there is a FIN, or if there is data and the 765 * connection is local, then delay SYN,ACK(SYN) in 766 * the hope of piggy-backing it on a response 767 * segment. Otherwise must send ACK now in case 768 * the other side is slow starting. 769 */ 770 if (tcp_delack_enabled && ((tiflags & TH_FIN) || (ti->ti_len != 0 && 771 in_localaddr(inp->inp_faddr)))) 772 tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 773 else 774 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 775 776 /* 777 * Limit the `virtual advertised window' to TCP_MAXWIN 778 * here. Even if we requested window scaling, it will 779 * become effective only later when our SYN is acked. 780 */ 781 tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 782 tcpstat.tcps_connects++; 783 soisconnected(so); 784 tp->t_timer[TCPT_KEEP] = tcp_keepinit; 785 dropsocket = 0; /* committed to socket */ 786 tcpstat.tcps_accepts++; 787 goto trimthenstep6; 788 } 789 /* else do standard 3-way handshake */ 790 } else { 791 /* 792 * No CC option, but maybe CC.NEW: 793 * invalidate cached value. 794 */ 795 taop->tao_cc = 0; 796 } 797 /* 798 * TAO test failed or there was no CC option, 799 * do a standard 3-way handshake. 800 */ 801 tp->t_flags |= TF_ACKNOW; 802 tp->t_state = TCPS_SYN_RECEIVED; 803 tp->t_timer[TCPT_KEEP] = tcp_keepinit; 804 dropsocket = 0; /* committed to socket */ 805 tcpstat.tcps_accepts++; 806 goto trimthenstep6; 807 } 808 809 /* 810 * If the state is SYN_RECEIVED: 811 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 812 */ 813 case TCPS_SYN_RECEIVED: 814 if ((tiflags & TH_ACK) && 815 (SEQ_LEQ(ti->ti_ack, tp->snd_una) || 816 SEQ_GT(ti->ti_ack, tp->snd_max))) 817 goto dropwithreset; 818 break; 819 820 /* 821 * If the state is SYN_SENT: 822 * if seg contains an ACK, but not for our SYN, drop the input. 823 * if seg contains a RST, then drop the connection. 824 * if seg does not contain SYN, then drop it. 825 * Otherwise this is an acceptable SYN segment 826 * initialize tp->rcv_nxt and tp->irs 827 * if seg contains ack then advance tp->snd_una 828 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 829 * arrange for segment to be acked (eventually) 830 * continue processing rest of data/controls, beginning with URG 831 */ 832 case TCPS_SYN_SENT: 833 if ((taop = tcp_gettaocache(inp)) == NULL) { 834 taop = &tao_noncached; 835 bzero(taop, sizeof(*taop)); 836 } 837 838 if ((tiflags & TH_ACK) && 839 (SEQ_LEQ(ti->ti_ack, tp->iss) || 840 SEQ_GT(ti->ti_ack, tp->snd_max))) { 841 /* 842 * If we have a cached CCsent for the remote host, 843 * hence we haven't just crashed and restarted, 844 * do not send a RST. This may be a retransmission 845 * from the other side after our earlier ACK was lost. 846 * Our new SYN, when it arrives, will serve as the 847 * needed ACK. 848 */ 849 if (taop->tao_ccsent != 0) 850 goto drop; 851 else 852 goto dropwithreset; 853 } 854 if (tiflags & TH_RST) { 855 if (tiflags & TH_ACK) 856 tp = tcp_drop(tp, ECONNREFUSED); 857 goto drop; 858 } 859 if ((tiflags & TH_SYN) == 0) 860 goto drop; 861 tp->snd_wnd = ti->ti_win; /* initial send window */ 862 tp->cc_recv = to.to_cc; /* foreign CC */ 863 864 tp->irs = ti->ti_seq; 865 tcp_rcvseqinit(tp); 866 if (tiflags & TH_ACK) { 867 /* 868 * Our SYN was acked. If segment contains CC.ECHO 869 * option, check it to make sure this segment really 870 * matches our SYN. If not, just drop it as old 871 * duplicate, but send an RST if we're still playing 872 * by the old rules. If no CC.ECHO option, make sure 873 * we don't get fooled into using T/TCP. 874 */ 875 if (to.to_flag & TOF_CCECHO) { 876 if (tp->cc_send != to.to_ccecho) { 877 if (taop->tao_ccsent != 0) 878 goto drop; 879 else 880 goto dropwithreset; 881 } 882 } else 883 tp->t_flags &= ~TF_RCVD_CC; 884 tcpstat.tcps_connects++; 885 soisconnected(so); 886 /* Do window scaling on this connection? */ 887 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 888 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 889 tp->snd_scale = tp->requested_s_scale; 890 tp->rcv_scale = tp->request_r_scale; 891 } 892 /* Segment is acceptable, update cache if undefined. */ 893 if (taop->tao_ccsent == 0) 894 taop->tao_ccsent = to.to_ccecho; 895 896 tp->rcv_adv += tp->rcv_wnd; 897 tp->snd_una++; /* SYN is acked */ 898 /* 899 * If there's data, delay ACK; if there's also a FIN 900 * ACKNOW will be turned on later. 901 */ 902 if (tcp_delack_enabled && ti->ti_len != 0) 903 tp->t_flags |= TF_DELACK; 904 else 905 tp->t_flags |= TF_ACKNOW; 906 /* 907 * Received <SYN,ACK> in SYN_SENT[*] state. 908 * Transitions: 909 * SYN_SENT --> ESTABLISHED 910 * SYN_SENT* --> FIN_WAIT_1 911 */ 912 if (tp->t_flags & TF_NEEDFIN) { 913 tp->t_state = TCPS_FIN_WAIT_1; 914 tp->t_flags &= ~TF_NEEDFIN; 915 tiflags &= ~TH_SYN; 916 } else { 917 tp->t_state = TCPS_ESTABLISHED; 918 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 919 } 920 } else { 921 /* 922 * Received initial SYN in SYN-SENT[*] state => simul- 923 * taneous open. If segment contains CC option and there is 924 * a cached CC, apply TAO test; if it succeeds, connection is 925 * half-synchronized. Otherwise, do 3-way handshake: 926 * SYN-SENT -> SYN-RECEIVED 927 * SYN-SENT* -> SYN-RECEIVED* 928 * If there was no CC option, clear cached CC value. 929 */ 930 tp->t_flags |= TF_ACKNOW; 931 tp->t_timer[TCPT_REXMT] = 0; 932 if (to.to_flag & TOF_CC) { 933 if (taop->tao_cc != 0 && 934 CC_GT(to.to_cc, taop->tao_cc)) { 935 /* 936 * update cache and make transition: 937 * SYN-SENT -> ESTABLISHED* 938 * SYN-SENT* -> FIN-WAIT-1* 939 */ 940 taop->tao_cc = to.to_cc; 941 if (tp->t_flags & TF_NEEDFIN) { 942 tp->t_state = TCPS_FIN_WAIT_1; 943 tp->t_flags &= ~TF_NEEDFIN; 944 } else { 945 tp->t_state = TCPS_ESTABLISHED; 946 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 947 } 948 tp->t_flags |= TF_NEEDSYN; 949 } else 950 tp->t_state = TCPS_SYN_RECEIVED; 951 } else { 952 /* CC.NEW or no option => invalidate cache */ 953 taop->tao_cc = 0; 954 tp->t_state = TCPS_SYN_RECEIVED; 955 } 956 } 957 958 trimthenstep6: 959 /* 960 * Advance ti->ti_seq to correspond to first data byte. 961 * If data, trim to stay within window, 962 * dropping FIN if necessary. 963 */ 964 ti->ti_seq++; 965 if (ti->ti_len > tp->rcv_wnd) { 966 todrop = ti->ti_len - tp->rcv_wnd; 967 m_adj(m, -todrop); 968 ti->ti_len = tp->rcv_wnd; 969 tiflags &= ~TH_FIN; 970 tcpstat.tcps_rcvpackafterwin++; 971 tcpstat.tcps_rcvbyteafterwin += todrop; 972 } 973 tp->snd_wl1 = ti->ti_seq - 1; 974 tp->rcv_up = ti->ti_seq; 975 /* 976 * Client side of transaction: already sent SYN and data. 977 * If the remote host used T/TCP to validate the SYN, 978 * our data will be ACK'd; if so, enter normal data segment 979 * processing in the middle of step 5, ack processing. 980 * Otherwise, goto step 6. 981 */ 982 if (tiflags & TH_ACK) 983 goto process_ACK; 984 goto step6; 985 /* 986 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 987 * if segment contains a SYN and CC [not CC.NEW] option: 988 * if state == TIME_WAIT and connection duration > MSL, 989 * drop packet and send RST; 990 * 991 * if SEG.CC > CCrecv then is new SYN, and can implicitly 992 * ack the FIN (and data) in retransmission queue. 993 * Complete close and delete TCPCB. Then reprocess 994 * segment, hoping to find new TCPCB in LISTEN state; 995 * 996 * else must be old SYN; drop it. 997 * else do normal processing. 998 */ 999 case TCPS_LAST_ACK: 1000 case TCPS_CLOSING: 1001 case TCPS_TIME_WAIT: 1002 if ((tiflags & TH_SYN) && 1003 (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 1004 if (tp->t_state == TCPS_TIME_WAIT && 1005 tp->t_duration > TCPTV_MSL) 1006 goto dropwithreset; 1007 if (CC_GT(to.to_cc, tp->cc_recv)) { 1008 tp = tcp_close(tp); 1009 goto findpcb; 1010 } 1011 else 1012 goto drop; 1013 } 1014 break; /* continue normal processing */ 1015 } 1016 1017 /* 1018 * States other than LISTEN or SYN_SENT. 1019 * First check the RST flag and sequence number since reset segments 1020 * are exempt from the timestamp and connection count tests. This 1021 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1022 * below which allowed reset segments in half the sequence space 1023 * to fall though and be processed (which gives forged reset 1024 * segments with a random sequence number a 50 percent chance of 1025 * killing a connection). 1026 * Then check timestamp, if present. 1027 * Then check the connection count, if present. 1028 * Then check that at least some bytes of segment are within 1029 * receive window. If segment begins before rcv_nxt, 1030 * drop leading data (and SYN); if nothing left, just ack. 1031 * 1032 * 1033 * If the RST bit is set, check the sequence number to see 1034 * if this is a valid reset segment. 1035 * RFC 793 page 37: 1036 * In all states except SYN-SENT, all reset (RST) segments 1037 * are validated by checking their SEQ-fields. A reset is 1038 * valid if its sequence number is in the window. 1039 * Note: this does not take into account delayed ACKs, so 1040 * we should test against last_ack_sent instead of rcv_nxt. 1041 * Also, it does not make sense to allow reset segments with 1042 * sequence numbers greater than last_ack_sent to be processed 1043 * since these sequence numbers are just the acknowledgement 1044 * numbers in our outgoing packets being echoed back at us, 1045 * and these acknowledgement numbers are monotonically 1046 * increasing. 1047 * If we have multiple segments in flight, the intial reset 1048 * segment sequence numbers will be to the left of last_ack_sent, 1049 * but they will eventually catch up. 1050 * In any case, it never made sense to trim reset segments to 1051 * fit the receive window since RFC 1122 says: 1052 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1053 * 1054 * A TCP SHOULD allow a received RST segment to include data. 1055 * 1056 * DISCUSSION 1057 * It has been suggested that a RST segment could contain 1058 * ASCII text that encoded and explained the cause of the 1059 * RST. No standard has yet been established for such 1060 * data. 1061 * 1062 * If the reset segment passes the sequence number test examine 1063 * the state: 1064 * SYN_RECEIVED STATE: 1065 * If passive open, return to LISTEN state. 1066 * If active open, inform user that connection was refused. 1067 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1068 * Inform user that connection was reset, and close tcb. 1069 * CLOSING, LAST_ACK STATES: 1070 * Close the tcb. 1071 * TIME_WAIT STATE: 1072 * Drop the segment - see Stevens, vol. 2, p. 964 and 1073 * RFC 1337. 1074 */ 1075 if (tiflags & TH_RST) { 1076 if (tp->last_ack_sent == ti->ti_seq) { 1077 switch (tp->t_state) { 1078 1079 case TCPS_SYN_RECEIVED: 1080 so->so_error = ECONNREFUSED; 1081 goto close; 1082 1083 case TCPS_ESTABLISHED: 1084 case TCPS_FIN_WAIT_1: 1085 case TCPS_FIN_WAIT_2: 1086 case TCPS_CLOSE_WAIT: 1087 so->so_error = ECONNRESET; 1088 close: 1089 tp->t_state = TCPS_CLOSED; 1090 tcpstat.tcps_drops++; 1091 tp = tcp_close(tp); 1092 break; 1093 1094 case TCPS_CLOSING: 1095 case TCPS_LAST_ACK: 1096 tp = tcp_close(tp); 1097 break; 1098 1099 case TCPS_TIME_WAIT: 1100 break; 1101 } 1102 } 1103 goto drop; 1104 } 1105 1106 /* 1107 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1108 * and it's less than ts_recent, drop it. 1109 */ 1110 if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent && 1111 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1112 1113 /* Check to see if ts_recent is over 24 days old. */ 1114 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1115 /* 1116 * Invalidate ts_recent. If this segment updates 1117 * ts_recent, the age will be reset later and ts_recent 1118 * will get a valid value. If it does not, setting 1119 * ts_recent to zero will at least satisfy the 1120 * requirement that zero be placed in the timestamp 1121 * echo reply when ts_recent isn't valid. The 1122 * age isn't reset until we get a valid ts_recent 1123 * because we don't want out-of-order segments to be 1124 * dropped when ts_recent is old. 1125 */ 1126 tp->ts_recent = 0; 1127 } else { 1128 tcpstat.tcps_rcvduppack++; 1129 tcpstat.tcps_rcvdupbyte += ti->ti_len; 1130 tcpstat.tcps_pawsdrop++; 1131 goto dropafterack; 1132 } 1133 } 1134 1135 /* 1136 * T/TCP mechanism 1137 * If T/TCP was negotiated and the segment doesn't have CC, 1138 * or if its CC is wrong then drop the segment. 1139 * RST segments do not have to comply with this. 1140 */ 1141 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1142 ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1143 goto dropafterack; 1144 1145 /* 1146 * In the SYN-RECEIVED state, validate that the packet belongs to 1147 * this connection before trimming the data to fit the receive 1148 * window. Check the sequence number versus IRS since we know 1149 * the sequence numbers haven't wrapped. This is a partial fix 1150 * for the "LAND" DoS attack. 1151 */ 1152 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(ti->ti_seq, tp->irs)) 1153 goto dropwithreset; 1154 1155 todrop = tp->rcv_nxt - ti->ti_seq; 1156 if (todrop > 0) { 1157 if (tiflags & TH_SYN) { 1158 tiflags &= ~TH_SYN; 1159 ti->ti_seq++; 1160 if (ti->ti_urp > 1) 1161 ti->ti_urp--; 1162 else 1163 tiflags &= ~TH_URG; 1164 todrop--; 1165 } 1166 /* 1167 * Following if statement from Stevens, vol. 2, p. 960. 1168 */ 1169 if (todrop > ti->ti_len 1170 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) { 1171 /* 1172 * Any valid FIN must be to the left of the window. 1173 * At this point the FIN must be a duplicate or out 1174 * of sequence; drop it. 1175 */ 1176 tiflags &= ~TH_FIN; 1177 1178 /* 1179 * Send an ACK to resynchronize and drop any data. 1180 * But keep on processing for RST or ACK. 1181 */ 1182 tp->t_flags |= TF_ACKNOW; 1183 todrop = ti->ti_len; 1184 tcpstat.tcps_rcvduppack++; 1185 tcpstat.tcps_rcvdupbyte += todrop; 1186 } else { 1187 tcpstat.tcps_rcvpartduppack++; 1188 tcpstat.tcps_rcvpartdupbyte += todrop; 1189 } 1190 m_adj(m, todrop); 1191 ti->ti_seq += todrop; 1192 ti->ti_len -= todrop; 1193 if (ti->ti_urp > todrop) 1194 ti->ti_urp -= todrop; 1195 else { 1196 tiflags &= ~TH_URG; 1197 ti->ti_urp = 0; 1198 } 1199 } 1200 1201 /* 1202 * If new data are received on a connection after the 1203 * user processes are gone, then RST the other end. 1204 */ 1205 if ((so->so_state & SS_NOFDREF) && 1206 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 1207 tp = tcp_close(tp); 1208 tcpstat.tcps_rcvafterclose++; 1209 goto dropwithreset; 1210 } 1211 1212 /* 1213 * If segment ends after window, drop trailing data 1214 * (and PUSH and FIN); if nothing left, just ACK. 1215 */ 1216 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 1217 if (todrop > 0) { 1218 tcpstat.tcps_rcvpackafterwin++; 1219 if (todrop >= ti->ti_len) { 1220 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 1221 /* 1222 * If a new connection request is received 1223 * while in TIME_WAIT, drop the old connection 1224 * and start over if the sequence numbers 1225 * are above the previous ones. 1226 */ 1227 if (tiflags & TH_SYN && 1228 tp->t_state == TCPS_TIME_WAIT && 1229 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 1230 iss = tp->snd_nxt + TCP_ISSINCR; 1231 tp = tcp_close(tp); 1232 goto findpcb; 1233 } 1234 /* 1235 * If window is closed can only take segments at 1236 * window edge, and have to drop data and PUSH from 1237 * incoming segments. Continue processing, but 1238 * remember to ack. Otherwise, drop segment 1239 * and ack. 1240 */ 1241 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 1242 tp->t_flags |= TF_ACKNOW; 1243 tcpstat.tcps_rcvwinprobe++; 1244 } else 1245 goto dropafterack; 1246 } else 1247 tcpstat.tcps_rcvbyteafterwin += todrop; 1248 m_adj(m, -todrop); 1249 ti->ti_len -= todrop; 1250 tiflags &= ~(TH_PUSH|TH_FIN); 1251 } 1252 1253 /* 1254 * If last ACK falls within this segment's sequence numbers, 1255 * record its timestamp. 1256 * NOTE that the test is modified according to the latest 1257 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1258 */ 1259 if ((to.to_flag & TOF_TS) != 0 && 1260 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 1261 tp->ts_recent_age = tcp_now; 1262 tp->ts_recent = to.to_tsval; 1263 } 1264 1265 /* 1266 * If a SYN is in the window, then this is an 1267 * error and we send an RST and drop the connection. 1268 */ 1269 if (tiflags & TH_SYN) { 1270 tp = tcp_drop(tp, ECONNRESET); 1271 goto dropwithreset; 1272 } 1273 1274 /* 1275 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1276 * flag is on (half-synchronized state), then queue data for 1277 * later processing; else drop segment and return. 1278 */ 1279 if ((tiflags & TH_ACK) == 0) { 1280 if (tp->t_state == TCPS_SYN_RECEIVED || 1281 (tp->t_flags & TF_NEEDSYN)) 1282 goto step6; 1283 else 1284 goto drop; 1285 } 1286 1287 /* 1288 * Ack processing. 1289 */ 1290 switch (tp->t_state) { 1291 1292 /* 1293 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1294 * ESTABLISHED state and continue processing. 1295 * The ACK was checked above. 1296 */ 1297 case TCPS_SYN_RECEIVED: 1298 1299 tcpstat.tcps_connects++; 1300 soisconnected(so); 1301 /* Do window scaling? */ 1302 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1303 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1304 tp->snd_scale = tp->requested_s_scale; 1305 tp->rcv_scale = tp->request_r_scale; 1306 } 1307 /* 1308 * Upon successful completion of 3-way handshake, 1309 * update cache.CC if it was undefined, pass any queued 1310 * data to the user, and advance state appropriately. 1311 */ 1312 if ((taop = tcp_gettaocache(inp)) != NULL && 1313 taop->tao_cc == 0) 1314 taop->tao_cc = tp->cc_recv; 1315 1316 /* 1317 * Make transitions: 1318 * SYN-RECEIVED -> ESTABLISHED 1319 * SYN-RECEIVED* -> FIN-WAIT-1 1320 */ 1321 if (tp->t_flags & TF_NEEDFIN) { 1322 tp->t_state = TCPS_FIN_WAIT_1; 1323 tp->t_flags &= ~TF_NEEDFIN; 1324 } else { 1325 tp->t_state = TCPS_ESTABLISHED; 1326 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 1327 } 1328 /* 1329 * If segment contains data or ACK, will call tcp_reass() 1330 * later; if not, do so now to pass queued data to user. 1331 */ 1332 if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0) 1333 (void) tcp_reass(tp, (struct tcpiphdr *)0, 1334 (struct mbuf *)0); 1335 tp->snd_wl1 = ti->ti_seq - 1; 1336 /* fall into ... */ 1337 1338 /* 1339 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1340 * ACKs. If the ack is in the range 1341 * tp->snd_una < ti->ti_ack <= tp->snd_max 1342 * then advance tp->snd_una to ti->ti_ack and drop 1343 * data from the retransmission queue. If this ACK reflects 1344 * more up to date window information we update our window information. 1345 */ 1346 case TCPS_ESTABLISHED: 1347 case TCPS_FIN_WAIT_1: 1348 case TCPS_FIN_WAIT_2: 1349 case TCPS_CLOSE_WAIT: 1350 case TCPS_CLOSING: 1351 case TCPS_LAST_ACK: 1352 case TCPS_TIME_WAIT: 1353 1354 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 1355 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 1356 tcpstat.tcps_rcvdupack++; 1357 /* 1358 * If we have outstanding data (other than 1359 * a window probe), this is a completely 1360 * duplicate ack (ie, window info didn't 1361 * change), the ack is the biggest we've 1362 * seen and we've seen exactly our rexmt 1363 * threshhold of them, assume a packet 1364 * has been dropped and retransmit it. 1365 * Kludge snd_nxt & the congestion 1366 * window so we send only this one 1367 * packet. 1368 * 1369 * We know we're losing at the current 1370 * window size so do congestion avoidance 1371 * (set ssthresh to half the current window 1372 * and pull our congestion window back to 1373 * the new ssthresh). 1374 * 1375 * Dup acks mean that packets have left the 1376 * network (they're now cached at the receiver) 1377 * so bump cwnd by the amount in the receiver 1378 * to keep a constant cwnd packets in the 1379 * network. 1380 */ 1381 if (tp->t_timer[TCPT_REXMT] == 0 || 1382 ti->ti_ack != tp->snd_una) 1383 tp->t_dupacks = 0; 1384 else if (++tp->t_dupacks == tcprexmtthresh) { 1385 tcp_seq onxt = tp->snd_nxt; 1386 u_int win = 1387 min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1388 tp->t_maxseg; 1389 1390 if (win < 2) 1391 win = 2; 1392 tp->snd_ssthresh = win * tp->t_maxseg; 1393 tp->t_timer[TCPT_REXMT] = 0; 1394 tp->t_rtt = 0; 1395 tp->snd_nxt = ti->ti_ack; 1396 tp->snd_cwnd = tp->t_maxseg; 1397 (void) tcp_output(tp); 1398 tp->snd_cwnd = tp->snd_ssthresh + 1399 tp->t_maxseg * tp->t_dupacks; 1400 if (SEQ_GT(onxt, tp->snd_nxt)) 1401 tp->snd_nxt = onxt; 1402 goto drop; 1403 } else if (tp->t_dupacks > tcprexmtthresh) { 1404 tp->snd_cwnd += tp->t_maxseg; 1405 (void) tcp_output(tp); 1406 goto drop; 1407 } 1408 } else 1409 tp->t_dupacks = 0; 1410 break; 1411 } 1412 /* 1413 * If the congestion window was inflated to account 1414 * for the other side's cached packets, retract it. 1415 */ 1416 if (tp->t_dupacks >= tcprexmtthresh && 1417 tp->snd_cwnd > tp->snd_ssthresh) 1418 tp->snd_cwnd = tp->snd_ssthresh; 1419 tp->t_dupacks = 0; 1420 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 1421 tcpstat.tcps_rcvacktoomuch++; 1422 goto dropafterack; 1423 } 1424 /* 1425 * If we reach this point, ACK is not a duplicate, 1426 * i.e., it ACKs something we sent. 1427 */ 1428 if (tp->t_flags & TF_NEEDSYN) { 1429 /* 1430 * T/TCP: Connection was half-synchronized, and our 1431 * SYN has been ACK'd (so connection is now fully 1432 * synchronized). Go to non-starred state, 1433 * increment snd_una for ACK of SYN, and check if 1434 * we can do window scaling. 1435 */ 1436 tp->t_flags &= ~TF_NEEDSYN; 1437 tp->snd_una++; 1438 /* Do window scaling? */ 1439 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1440 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1441 tp->snd_scale = tp->requested_s_scale; 1442 tp->rcv_scale = tp->request_r_scale; 1443 } 1444 } 1445 1446 process_ACK: 1447 acked = ti->ti_ack - tp->snd_una; 1448 tcpstat.tcps_rcvackpack++; 1449 tcpstat.tcps_rcvackbyte += acked; 1450 1451 /* 1452 * If we have a timestamp reply, update smoothed 1453 * round trip time. If no timestamp is present but 1454 * transmit timer is running and timed sequence 1455 * number was acked, update smoothed round trip time. 1456 * Since we now have an rtt measurement, cancel the 1457 * timer backoff (cf., Phil Karn's retransmit alg.). 1458 * Recompute the initial retransmit timer. 1459 */ 1460 if (to.to_flag & TOF_TS) 1461 tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1); 1462 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 1463 tcp_xmit_timer(tp,tp->t_rtt); 1464 1465 /* 1466 * If all outstanding data is acked, stop retransmit 1467 * timer and remember to restart (more output or persist). 1468 * If there is more data to be acked, restart retransmit 1469 * timer, using current (possibly backed-off) value. 1470 */ 1471 if (ti->ti_ack == tp->snd_max) { 1472 tp->t_timer[TCPT_REXMT] = 0; 1473 needoutput = 1; 1474 } else if (tp->t_timer[TCPT_PERSIST] == 0) 1475 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 1476 1477 /* 1478 * If no data (only SYN) was ACK'd, 1479 * skip rest of ACK processing. 1480 */ 1481 if (acked == 0) 1482 goto step6; 1483 1484 /* 1485 * When new data is acked, open the congestion window. 1486 * If the window gives us less than ssthresh packets 1487 * in flight, open exponentially (maxseg per packet). 1488 * Otherwise open linearly: maxseg per window 1489 * (maxseg^2 / cwnd per packet). 1490 */ 1491 { 1492 register u_int cw = tp->snd_cwnd; 1493 register u_int incr = tp->t_maxseg; 1494 1495 if (cw > tp->snd_ssthresh) 1496 incr = incr * incr / cw; 1497 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1498 } 1499 if (acked > so->so_snd.sb_cc) { 1500 tp->snd_wnd -= so->so_snd.sb_cc; 1501 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1502 ourfinisacked = 1; 1503 } else { 1504 sbdrop(&so->so_snd, acked); 1505 tp->snd_wnd -= acked; 1506 ourfinisacked = 0; 1507 } 1508 sowwakeup(so); 1509 tp->snd_una = ti->ti_ack; 1510 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1511 tp->snd_nxt = tp->snd_una; 1512 1513 switch (tp->t_state) { 1514 1515 /* 1516 * In FIN_WAIT_1 STATE in addition to the processing 1517 * for the ESTABLISHED state if our FIN is now acknowledged 1518 * then enter FIN_WAIT_2. 1519 */ 1520 case TCPS_FIN_WAIT_1: 1521 if (ourfinisacked) { 1522 /* 1523 * If we can't receive any more 1524 * data, then closing user can proceed. 1525 * Starting the timer is contrary to the 1526 * specification, but if we don't get a FIN 1527 * we'll hang forever. 1528 */ 1529 if (so->so_state & SS_CANTRCVMORE) { 1530 soisdisconnected(so); 1531 tp->t_timer[TCPT_2MSL] = tcp_maxidle; 1532 } 1533 tp->t_state = TCPS_FIN_WAIT_2; 1534 } 1535 break; 1536 1537 /* 1538 * In CLOSING STATE in addition to the processing for 1539 * the ESTABLISHED state if the ACK acknowledges our FIN 1540 * then enter the TIME-WAIT state, otherwise ignore 1541 * the segment. 1542 */ 1543 case TCPS_CLOSING: 1544 if (ourfinisacked) { 1545 tp->t_state = TCPS_TIME_WAIT; 1546 tcp_canceltimers(tp); 1547 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1548 if (tp->cc_recv != 0 && 1549 tp->t_duration < TCPTV_MSL) 1550 tp->t_timer[TCPT_2MSL] = 1551 tp->t_rxtcur * TCPTV_TWTRUNC; 1552 else 1553 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1554 soisdisconnected(so); 1555 } 1556 break; 1557 1558 /* 1559 * In LAST_ACK, we may still be waiting for data to drain 1560 * and/or to be acked, as well as for the ack of our FIN. 1561 * If our FIN is now acknowledged, delete the TCB, 1562 * enter the closed state and return. 1563 */ 1564 case TCPS_LAST_ACK: 1565 if (ourfinisacked) { 1566 tp = tcp_close(tp); 1567 goto drop; 1568 } 1569 break; 1570 1571 /* 1572 * In TIME_WAIT state the only thing that should arrive 1573 * is a retransmission of the remote FIN. Acknowledge 1574 * it and restart the finack timer. 1575 */ 1576 case TCPS_TIME_WAIT: 1577 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1578 goto dropafterack; 1579 } 1580 } 1581 1582 step6: 1583 /* 1584 * Update window information. 1585 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1586 */ 1587 if ((tiflags & TH_ACK) && 1588 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 1589 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 1590 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { 1591 /* keep track of pure window updates */ 1592 if (ti->ti_len == 0 && 1593 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 1594 tcpstat.tcps_rcvwinupd++; 1595 tp->snd_wnd = tiwin; 1596 tp->snd_wl1 = ti->ti_seq; 1597 tp->snd_wl2 = ti->ti_ack; 1598 if (tp->snd_wnd > tp->max_sndwnd) 1599 tp->max_sndwnd = tp->snd_wnd; 1600 needoutput = 1; 1601 } 1602 1603 /* 1604 * Process segments with URG. 1605 */ 1606 if ((tiflags & TH_URG) && ti->ti_urp && 1607 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1608 /* 1609 * This is a kludge, but if we receive and accept 1610 * random urgent pointers, we'll crash in 1611 * soreceive. It's hard to imagine someone 1612 * actually wanting to send this much urgent data. 1613 */ 1614 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 1615 ti->ti_urp = 0; /* XXX */ 1616 tiflags &= ~TH_URG; /* XXX */ 1617 goto dodata; /* XXX */ 1618 } 1619 /* 1620 * If this segment advances the known urgent pointer, 1621 * then mark the data stream. This should not happen 1622 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1623 * a FIN has been received from the remote side. 1624 * In these states we ignore the URG. 1625 * 1626 * According to RFC961 (Assigned Protocols), 1627 * the urgent pointer points to the last octet 1628 * of urgent data. We continue, however, 1629 * to consider it to indicate the first octet 1630 * of data past the urgent section as the original 1631 * spec states (in one of two places). 1632 */ 1633 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 1634 tp->rcv_up = ti->ti_seq + ti->ti_urp; 1635 so->so_oobmark = so->so_rcv.sb_cc + 1636 (tp->rcv_up - tp->rcv_nxt) - 1; 1637 if (so->so_oobmark == 0) 1638 so->so_state |= SS_RCVATMARK; 1639 sohasoutofband(so); 1640 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1641 } 1642 /* 1643 * Remove out of band data so doesn't get presented to user. 1644 * This can happen independent of advancing the URG pointer, 1645 * but if two URG's are pending at once, some out-of-band 1646 * data may creep in... ick. 1647 */ 1648 if (ti->ti_urp <= (u_long)ti->ti_len 1649 #ifdef SO_OOBINLINE 1650 && (so->so_options & SO_OOBINLINE) == 0 1651 #endif 1652 ) 1653 tcp_pulloutofband(so, ti, m); 1654 } else 1655 /* 1656 * If no out of band data is expected, 1657 * pull receive urgent pointer along 1658 * with the receive window. 1659 */ 1660 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1661 tp->rcv_up = tp->rcv_nxt; 1662 dodata: /* XXX */ 1663 1664 /* 1665 * Process the segment text, merging it into the TCP sequencing queue, 1666 * and arranging for acknowledgment of receipt if necessary. 1667 * This process logically involves adjusting tp->rcv_wnd as data 1668 * is presented to the user (this happens in tcp_usrreq.c, 1669 * case PRU_RCVD). If a FIN has already been received on this 1670 * connection then we just ignore the text. 1671 */ 1672 if ((ti->ti_len || (tiflags&TH_FIN)) && 1673 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1674 TCP_REASS(tp, ti, m, so, tiflags); 1675 /* 1676 * Note the amount of data that peer has sent into 1677 * our window, in order to estimate the sender's 1678 * buffer size. 1679 */ 1680 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1681 } else { 1682 m_freem(m); 1683 tiflags &= ~TH_FIN; 1684 } 1685 1686 /* 1687 * If FIN is received ACK the FIN and let the user know 1688 * that the connection is closing. 1689 */ 1690 if (tiflags & TH_FIN) { 1691 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1692 socantrcvmore(so); 1693 /* 1694 * If connection is half-synchronized 1695 * (ie NEEDSYN flag on) then delay ACK, 1696 * so it may be piggybacked when SYN is sent. 1697 * Otherwise, since we received a FIN then no 1698 * more input can be expected, send ACK now. 1699 */ 1700 if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN)) 1701 tp->t_flags |= TF_DELACK; 1702 else 1703 tp->t_flags |= TF_ACKNOW; 1704 tp->rcv_nxt++; 1705 } 1706 switch (tp->t_state) { 1707 1708 /* 1709 * In SYN_RECEIVED and ESTABLISHED STATES 1710 * enter the CLOSE_WAIT state. 1711 */ 1712 case TCPS_SYN_RECEIVED: 1713 case TCPS_ESTABLISHED: 1714 tp->t_state = TCPS_CLOSE_WAIT; 1715 break; 1716 1717 /* 1718 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1719 * enter the CLOSING state. 1720 */ 1721 case TCPS_FIN_WAIT_1: 1722 tp->t_state = TCPS_CLOSING; 1723 break; 1724 1725 /* 1726 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1727 * starting the time-wait timer, turning off the other 1728 * standard timers. 1729 */ 1730 case TCPS_FIN_WAIT_2: 1731 tp->t_state = TCPS_TIME_WAIT; 1732 tcp_canceltimers(tp); 1733 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1734 if (tp->cc_recv != 0 && 1735 tp->t_duration < TCPTV_MSL) { 1736 tp->t_timer[TCPT_2MSL] = 1737 tp->t_rxtcur * TCPTV_TWTRUNC; 1738 /* For transaction client, force ACK now. */ 1739 tp->t_flags |= TF_ACKNOW; 1740 } 1741 else 1742 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1743 soisdisconnected(so); 1744 break; 1745 1746 /* 1747 * In TIME_WAIT state restart the 2 MSL time_wait timer. 1748 */ 1749 case TCPS_TIME_WAIT: 1750 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1751 break; 1752 } 1753 } 1754 #ifdef TCPDEBUG 1755 if (so->so_options & SO_DEBUG) 1756 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1757 #endif 1758 1759 /* 1760 * Return any desired output. 1761 */ 1762 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1763 (void) tcp_output(tp); 1764 return; 1765 1766 dropafterack: 1767 /* 1768 * Generate an ACK dropping incoming segment if it occupies 1769 * sequence space, where the ACK reflects our state. 1770 * 1771 * We can now skip the test for the RST flag since all 1772 * paths to this code happen after packets containing 1773 * RST have been dropped. 1774 * 1775 * In the SYN-RECEIVED state, don't send an ACK unless the 1776 * segment we received passes the SYN-RECEIVED ACK test. 1777 * If it fails send a RST. This breaks the loop in the 1778 * "LAND" DoS attack, and also prevents an ACK storm 1779 * between two listening ports that have been sent forged 1780 * SYN segments, each with the source address of the other. 1781 */ 1782 if (tp->t_state == TCPS_SYN_RECEIVED && (tiflags & TH_ACK) && 1783 (SEQ_GT(tp->snd_una, ti->ti_ack) || 1784 SEQ_GT(ti->ti_ack, tp->snd_max)) ) 1785 goto dropwithreset; 1786 #ifdef TCPDEBUG 1787 if (so->so_options & SO_DEBUG) 1788 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1789 #endif 1790 m_freem(m); 1791 tp->t_flags |= TF_ACKNOW; 1792 (void) tcp_output(tp); 1793 return; 1794 1795 dropwithreset: 1796 /* 1797 * Generate a RST, dropping incoming segment. 1798 * Make ACK acceptable to originator of segment. 1799 * Don't bother to respond if destination was broadcast/multicast. 1800 */ 1801 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 1802 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 1803 goto drop; 1804 #ifdef TCPDEBUG 1805 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1806 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1807 #endif 1808 if (tiflags & TH_ACK) 1809 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 1810 else { 1811 if (tiflags & TH_SYN) 1812 ti->ti_len++; 1813 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1814 TH_RST|TH_ACK); 1815 } 1816 /* destroy temporarily created socket */ 1817 if (dropsocket) 1818 (void) soabort(so); 1819 return; 1820 1821 drop: 1822 /* 1823 * Drop space held by incoming segment and return. 1824 */ 1825 #ifdef TCPDEBUG 1826 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1827 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1828 #endif 1829 m_freem(m); 1830 /* destroy temporarily created socket */ 1831 if (dropsocket) 1832 (void) soabort(so); 1833 return; 1834 } 1835 1836 static void 1837 tcp_dooptions(tp, cp, cnt, ti, to) 1838 struct tcpcb *tp; 1839 u_char *cp; 1840 int cnt; 1841 struct tcpiphdr *ti; 1842 struct tcpopt *to; 1843 { 1844 u_short mss = 0; 1845 int opt, optlen; 1846 1847 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1848 opt = cp[0]; 1849 if (opt == TCPOPT_EOL) 1850 break; 1851 if (opt == TCPOPT_NOP) 1852 optlen = 1; 1853 else { 1854 optlen = cp[1]; 1855 if (optlen <= 0) 1856 break; 1857 } 1858 switch (opt) { 1859 1860 default: 1861 continue; 1862 1863 case TCPOPT_MAXSEG: 1864 if (optlen != TCPOLEN_MAXSEG) 1865 continue; 1866 if (!(ti->ti_flags & TH_SYN)) 1867 continue; 1868 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 1869 NTOHS(mss); 1870 break; 1871 1872 case TCPOPT_WINDOW: 1873 if (optlen != TCPOLEN_WINDOW) 1874 continue; 1875 if (!(ti->ti_flags & TH_SYN)) 1876 continue; 1877 tp->t_flags |= TF_RCVD_SCALE; 1878 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 1879 break; 1880 1881 case TCPOPT_TIMESTAMP: 1882 if (optlen != TCPOLEN_TIMESTAMP) 1883 continue; 1884 to->to_flag |= TOF_TS; 1885 bcopy((char *)cp + 2, 1886 (char *)&to->to_tsval, sizeof(to->to_tsval)); 1887 NTOHL(to->to_tsval); 1888 bcopy((char *)cp + 6, 1889 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 1890 NTOHL(to->to_tsecr); 1891 1892 /* 1893 * A timestamp received in a SYN makes 1894 * it ok to send timestamp requests and replies. 1895 */ 1896 if (ti->ti_flags & TH_SYN) { 1897 tp->t_flags |= TF_RCVD_TSTMP; 1898 tp->ts_recent = to->to_tsval; 1899 tp->ts_recent_age = tcp_now; 1900 } 1901 break; 1902 case TCPOPT_CC: 1903 if (optlen != TCPOLEN_CC) 1904 continue; 1905 to->to_flag |= TOF_CC; 1906 bcopy((char *)cp + 2, 1907 (char *)&to->to_cc, sizeof(to->to_cc)); 1908 NTOHL(to->to_cc); 1909 /* 1910 * A CC or CC.new option received in a SYN makes 1911 * it ok to send CC in subsequent segments. 1912 */ 1913 if (ti->ti_flags & TH_SYN) 1914 tp->t_flags |= TF_RCVD_CC; 1915 break; 1916 case TCPOPT_CCNEW: 1917 if (optlen != TCPOLEN_CC) 1918 continue; 1919 if (!(ti->ti_flags & TH_SYN)) 1920 continue; 1921 to->to_flag |= TOF_CCNEW; 1922 bcopy((char *)cp + 2, 1923 (char *)&to->to_cc, sizeof(to->to_cc)); 1924 NTOHL(to->to_cc); 1925 /* 1926 * A CC or CC.new option received in a SYN makes 1927 * it ok to send CC in subsequent segments. 1928 */ 1929 tp->t_flags |= TF_RCVD_CC; 1930 break; 1931 case TCPOPT_CCECHO: 1932 if (optlen != TCPOLEN_CC) 1933 continue; 1934 if (!(ti->ti_flags & TH_SYN)) 1935 continue; 1936 to->to_flag |= TOF_CCECHO; 1937 bcopy((char *)cp + 2, 1938 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 1939 NTOHL(to->to_ccecho); 1940 break; 1941 } 1942 } 1943 if (ti->ti_flags & TH_SYN) 1944 tcp_mss(tp, mss); /* sets t_maxseg */ 1945 } 1946 1947 /* 1948 * Pull out of band byte out of a segment so 1949 * it doesn't appear in the user's data queue. 1950 * It is still reflected in the segment length for 1951 * sequencing purposes. 1952 */ 1953 static void 1954 tcp_pulloutofband(so, ti, m) 1955 struct socket *so; 1956 struct tcpiphdr *ti; 1957 register struct mbuf *m; 1958 { 1959 int cnt = ti->ti_urp - 1; 1960 1961 while (cnt >= 0) { 1962 if (m->m_len > cnt) { 1963 char *cp = mtod(m, caddr_t) + cnt; 1964 struct tcpcb *tp = sototcpcb(so); 1965 1966 tp->t_iobc = *cp; 1967 tp->t_oobflags |= TCPOOB_HAVEDATA; 1968 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1969 m->m_len--; 1970 return; 1971 } 1972 cnt -= m->m_len; 1973 m = m->m_next; 1974 if (m == 0) 1975 break; 1976 } 1977 panic("tcp_pulloutofband"); 1978 } 1979 1980 /* 1981 * Collect new round-trip time estimate 1982 * and update averages and current timeout. 1983 */ 1984 static void 1985 tcp_xmit_timer(tp, rtt) 1986 register struct tcpcb *tp; 1987 short rtt; 1988 { 1989 register int delta; 1990 1991 tcpstat.tcps_rttupdated++; 1992 tp->t_rttupdated++; 1993 if (tp->t_srtt != 0) { 1994 /* 1995 * srtt is stored as fixed point with 5 bits after the 1996 * binary point (i.e., scaled by 8). The following magic 1997 * is equivalent to the smoothing algorithm in rfc793 with 1998 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 1999 * point). Adjust rtt to origin 0. 2000 */ 2001 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2002 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2003 2004 if ((tp->t_srtt += delta) <= 0) 2005 tp->t_srtt = 1; 2006 2007 /* 2008 * We accumulate a smoothed rtt variance (actually, a 2009 * smoothed mean difference), then set the retransmit 2010 * timer to smoothed rtt + 4 times the smoothed variance. 2011 * rttvar is stored as fixed point with 4 bits after the 2012 * binary point (scaled by 16). The following is 2013 * equivalent to rfc793 smoothing with an alpha of .75 2014 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2015 * rfc793's wired-in beta. 2016 */ 2017 if (delta < 0) 2018 delta = -delta; 2019 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2020 if ((tp->t_rttvar += delta) <= 0) 2021 tp->t_rttvar = 1; 2022 } else { 2023 /* 2024 * No rtt measurement yet - use the unsmoothed rtt. 2025 * Set the variance to half the rtt (so our first 2026 * retransmit happens at 3*rtt). 2027 */ 2028 tp->t_srtt = rtt << TCP_RTT_SHIFT; 2029 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2030 } 2031 tp->t_rtt = 0; 2032 tp->t_rxtshift = 0; 2033 2034 /* 2035 * the retransmit should happen at rtt + 4 * rttvar. 2036 * Because of the way we do the smoothing, srtt and rttvar 2037 * will each average +1/2 tick of bias. When we compute 2038 * the retransmit timer, we want 1/2 tick of rounding and 2039 * 1 extra tick because of +-1/2 tick uncertainty in the 2040 * firing of the timer. The bias will give us exactly the 2041 * 1.5 tick we need. But, because the bias is 2042 * statistical, we have to test that we don't drop below 2043 * the minimum feasible timer (which is 2 ticks). 2044 */ 2045 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 2046 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2047 2048 /* 2049 * We received an ack for a packet that wasn't retransmitted; 2050 * it is probably safe to discard any error indications we've 2051 * received recently. This isn't quite right, but close enough 2052 * for now (a route might have failed after we sent a segment, 2053 * and the return path might not be symmetrical). 2054 */ 2055 tp->t_softerror = 0; 2056 } 2057 2058 /* 2059 * Determine a reasonable value for maxseg size. 2060 * If the route is known, check route for mtu. 2061 * If none, use an mss that can be handled on the outgoing 2062 * interface without forcing IP to fragment; if bigger than 2063 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2064 * to utilize large mbufs. If no route is found, route has no mtu, 2065 * or the destination isn't local, use a default, hopefully conservative 2066 * size (usually 512 or the default IP max size, but no more than the mtu 2067 * of the interface), as we can't discover anything about intervening 2068 * gateways or networks. We also initialize the congestion/slow start 2069 * window to be a single segment if the destination isn't local. 2070 * While looking at the routing entry, we also initialize other path-dependent 2071 * parameters from pre-set or cached values in the routing entry. 2072 * 2073 * Also take into account the space needed for options that we 2074 * send regularly. Make maxseg shorter by that amount to assure 2075 * that we can send maxseg amount of data even when the options 2076 * are present. Store the upper limit of the length of options plus 2077 * data in maxopd. 2078 * 2079 * NOTE that this routine is only called when we process an incoming 2080 * segment, for outgoing segments only tcp_mssopt is called. 2081 * 2082 * In case of T/TCP, we call this routine during implicit connection 2083 * setup as well (offer = -1), to initialize maxseg from the cached 2084 * MSS of our peer. 2085 */ 2086 void 2087 tcp_mss(tp, offer) 2088 struct tcpcb *tp; 2089 int offer; 2090 { 2091 register struct rtentry *rt; 2092 struct ifnet *ifp; 2093 register int rtt, mss; 2094 u_long bufsize; 2095 struct inpcb *inp; 2096 struct socket *so; 2097 struct rmxp_tao *taop; 2098 int origoffer = offer; 2099 2100 inp = tp->t_inpcb; 2101 if ((rt = tcp_rtlookup(inp)) == NULL) { 2102 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2103 return; 2104 } 2105 ifp = rt->rt_ifp; 2106 so = inp->inp_socket; 2107 2108 taop = rmx_taop(rt->rt_rmx); 2109 /* 2110 * Offer == -1 means that we didn't receive SYN yet, 2111 * use cached value in that case; 2112 */ 2113 if (offer == -1) 2114 offer = taop->tao_mssopt; 2115 /* 2116 * Offer == 0 means that there was no MSS on the SYN segment, 2117 * in this case we use tcp_mssdflt. 2118 */ 2119 if (offer == 0) 2120 offer = tcp_mssdflt; 2121 else 2122 /* 2123 * Sanity check: make sure that maxopd will be large 2124 * enough to allow some data on segments even is the 2125 * all the option space is used (40bytes). Otherwise 2126 * funny things may happen in tcp_output. 2127 */ 2128 offer = max(offer, 64); 2129 taop->tao_mssopt = offer; 2130 2131 /* 2132 * While we're here, check if there's an initial rtt 2133 * or rttvar. Convert from the route-table units 2134 * to scaled multiples of the slow timeout timer. 2135 */ 2136 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2137 /* 2138 * XXX the lock bit for RTT indicates that the value 2139 * is also a minimum value; this is subject to time. 2140 */ 2141 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2142 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 2143 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 2144 tcpstat.tcps_usedrtt++; 2145 if (rt->rt_rmx.rmx_rttvar) { 2146 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2147 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 2148 tcpstat.tcps_usedrttvar++; 2149 } else { 2150 /* default variation is +- 1 rtt */ 2151 tp->t_rttvar = 2152 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2153 } 2154 TCPT_RANGESET(tp->t_rxtcur, 2155 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2156 tp->t_rttmin, TCPTV_REXMTMAX); 2157 } 2158 /* 2159 * if there's an mtu associated with the route, use it 2160 */ 2161 if (rt->rt_rmx.rmx_mtu) 2162 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 2163 else 2164 { 2165 mss = ifp->if_mtu - sizeof(struct tcpiphdr); 2166 if (!in_localaddr(inp->inp_faddr)) 2167 mss = min(mss, tcp_mssdflt); 2168 } 2169 mss = min(mss, offer); 2170 /* 2171 * maxopd stores the maximum length of data AND options 2172 * in a segment; maxseg is the amount of data in a normal 2173 * segment. We need to store this value (maxopd) apart 2174 * from maxseg, because now every segment carries options 2175 * and thus we normally have somewhat less data in segments. 2176 */ 2177 tp->t_maxopd = mss; 2178 2179 /* 2180 * In case of T/TCP, origoffer==-1 indicates, that no segments 2181 * were received yet. In this case we just guess, otherwise 2182 * we do the same as before T/TCP. 2183 */ 2184 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2185 (origoffer == -1 || 2186 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2187 mss -= TCPOLEN_TSTAMP_APPA; 2188 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2189 (origoffer == -1 || 2190 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2191 mss -= TCPOLEN_CC_APPA; 2192 2193 #if (MCLBYTES & (MCLBYTES - 1)) == 0 2194 if (mss > MCLBYTES) 2195 mss &= ~(MCLBYTES-1); 2196 #else 2197 if (mss > MCLBYTES) 2198 mss = mss / MCLBYTES * MCLBYTES; 2199 #endif 2200 /* 2201 * If there's a pipesize, change the socket buffer 2202 * to that size. Make the socket buffers an integral 2203 * number of mss units; if the mss is larger than 2204 * the socket buffer, decrease the mss. 2205 */ 2206 #ifdef RTV_SPIPE 2207 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2208 #endif 2209 bufsize = so->so_snd.sb_hiwat; 2210 if (bufsize < mss) 2211 mss = bufsize; 2212 else { 2213 bufsize = roundup(bufsize, mss); 2214 if (bufsize > sb_max) 2215 bufsize = sb_max; 2216 (void)sbreserve(&so->so_snd, bufsize); 2217 } 2218 tp->t_maxseg = mss; 2219 2220 #ifdef RTV_RPIPE 2221 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2222 #endif 2223 bufsize = so->so_rcv.sb_hiwat; 2224 if (bufsize > mss) { 2225 bufsize = roundup(bufsize, mss); 2226 if (bufsize > sb_max) 2227 bufsize = sb_max; 2228 (void)sbreserve(&so->so_rcv, bufsize); 2229 } 2230 /* 2231 * Don't force slow-start on local network. 2232 */ 2233 if (!in_localaddr(inp->inp_faddr)) 2234 tp->snd_cwnd = mss; 2235 2236 if (rt->rt_rmx.rmx_ssthresh) { 2237 /* 2238 * There's some sort of gateway or interface 2239 * buffer limit on the path. Use this to set 2240 * the slow start threshhold, but set the 2241 * threshold to no less than 2*mss. 2242 */ 2243 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2244 tcpstat.tcps_usedssthresh++; 2245 } 2246 } 2247 2248 /* 2249 * Determine the MSS option to send on an outgoing SYN. 2250 */ 2251 int 2252 tcp_mssopt(tp) 2253 struct tcpcb *tp; 2254 { 2255 struct rtentry *rt; 2256 2257 rt = tcp_rtlookup(tp->t_inpcb); 2258 if (rt == NULL) 2259 return tcp_mssdflt; 2260 2261 return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr); 2262 } 2263