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