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