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