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