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