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