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