1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994 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.5 (Berkeley) 4/10/94 34 * $Id: tcp_input.c,v 1.9 1994/10/02 17:48:43 phk Exp $ 35 */ 36 37 #ifndef TUBA_INCLUDE 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/protosw.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/errno.h> 46 47 #include <net/if.h> 48 #include <net/route.h> 49 50 #include <netinet/in.h> 51 #include <netinet/in_systm.h> 52 #include <netinet/ip.h> 53 #include <netinet/in_pcb.h> 54 #include <netinet/ip_var.h> 55 #include <netinet/tcp.h> 56 #include <netinet/tcp_fsm.h> 57 #include <netinet/tcp_seq.h> 58 #include <netinet/tcp_timer.h> 59 #include <netinet/tcp_var.h> 60 #include <netinet/tcpip.h> 61 #ifdef TCPDEBUG 62 #include <netinet/tcp_debug.h> 63 struct tcpiphdr tcp_saveti; 64 #endif 65 66 int tcprexmtthresh = 3; 67 struct inpcb *tcp_last_inpcb = &tcb; 68 69 #endif /* TUBA_INCLUDE */ 70 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ) 71 72 /* for modulo comparisons of timestamps */ 73 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) 74 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) 75 76 77 /* 78 * Insert segment ti into reassembly queue of tcp with 79 * control block tp. Return TH_FIN if reassembly now includes 80 * a segment with FIN. The macro form does the common case inline 81 * (segment is the next to be received on an established connection, 82 * and the queue is empty), avoiding linkage into and removal 83 * from the queue and repetition of various conversions. 84 * Set DELACK for segments received in order, but ack immediately 85 * when segments are out of order (so fast retransmit can work). 86 */ 87 #define TCP_REASS(tp, ti, m, so, flags) { \ 88 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 89 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 90 (tp)->t_state == TCPS_ESTABLISHED) { \ 91 tp->t_flags |= TF_DELACK; \ 92 (tp)->rcv_nxt += (ti)->ti_len; \ 93 flags = (ti)->ti_flags & TH_FIN; \ 94 tcpstat.tcps_rcvpack++;\ 95 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 96 sbappend(&(so)->so_rcv, (m)); \ 97 sorwakeup(so); \ 98 } else { \ 99 (flags) = tcp_reass((tp), (ti), (m)); \ 100 tp->t_flags |= TF_ACKNOW; \ 101 } \ 102 } 103 #ifndef TUBA_INCLUDE 104 105 int 106 tcp_reass(tp, ti, m) 107 register struct tcpcb *tp; 108 register struct tcpiphdr *ti; 109 struct mbuf *m; 110 { 111 register struct tcpiphdr *q; 112 struct socket *so = tp->t_inpcb->inp_socket; 113 int flags; 114 115 /* 116 * Call with ti==0 after become established to 117 * force pre-ESTABLISHED data up to user socket. 118 */ 119 if (ti == 0) 120 goto present; 121 122 /* 123 * Find a segment which begins after this one does. 124 */ 125 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 126 q = (struct tcpiphdr *)q->ti_next) 127 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 128 break; 129 130 /* 131 * If there is a preceding segment, it may provide some of 132 * our data already. If so, drop the data from the incoming 133 * segment. If it provides all of our data, drop us. 134 */ 135 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 136 register int i; 137 q = (struct tcpiphdr *)q->ti_prev; 138 /* conversion to int (in i) handles seq wraparound */ 139 i = q->ti_seq + q->ti_len - ti->ti_seq; 140 if (i > 0) { 141 if (i >= ti->ti_len) { 142 tcpstat.tcps_rcvduppack++; 143 tcpstat.tcps_rcvdupbyte += ti->ti_len; 144 m_freem(m); 145 return (0); 146 } 147 m_adj(m, i); 148 ti->ti_len -= i; 149 ti->ti_seq += i; 150 } 151 q = (struct tcpiphdr *)(q->ti_next); 152 } 153 tcpstat.tcps_rcvoopack++; 154 tcpstat.tcps_rcvoobyte += ti->ti_len; 155 REASS_MBUF(ti) = m; /* XXX */ 156 157 /* 158 * While we overlap succeeding segments trim them or, 159 * if they are completely covered, dequeue them. 160 */ 161 while (q != (struct tcpiphdr *)tp) { 162 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 163 if (i <= 0) 164 break; 165 if (i < q->ti_len) { 166 q->ti_seq += i; 167 q->ti_len -= i; 168 m_adj(REASS_MBUF(q), i); 169 break; 170 } 171 q = (struct tcpiphdr *)q->ti_next; 172 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 173 remque(q->ti_prev); 174 m_freem(m); 175 } 176 177 /* 178 * Stick new segment in its place. 179 */ 180 insque(ti, q->ti_prev); 181 182 present: 183 /* 184 * Present data to user, advancing rcv_nxt through 185 * completed sequence space. 186 */ 187 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 188 return (0); 189 ti = tp->seg_next; 190 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 191 return (0); 192 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 193 return (0); 194 do { 195 tp->rcv_nxt += ti->ti_len; 196 flags = ti->ti_flags & TH_FIN; 197 remque(ti); 198 m = REASS_MBUF(ti); 199 ti = (struct tcpiphdr *)ti->ti_next; 200 if (so->so_state & SS_CANTRCVMORE) 201 m_freem(m); 202 else 203 sbappend(&so->so_rcv, m); 204 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 205 sorwakeup(so); 206 return (flags); 207 } 208 209 /* 210 * TCP input routine, follows pages 65-76 of the 211 * protocol specification dated September, 1981 very closely. 212 */ 213 void 214 tcp_input(m, iphlen) 215 register struct mbuf *m; 216 int iphlen; 217 { 218 register struct tcpiphdr *ti; 219 register struct inpcb *inp; 220 caddr_t optp = NULL; 221 int optlen = 0; 222 int len, tlen, off; 223 register struct tcpcb *tp = 0; 224 register int tiflags; 225 struct socket *so = 0; 226 int todrop, acked, ourfinisacked, needoutput = 0; 227 struct in_addr laddr; 228 int dropsocket = 0; 229 int iss = 0; 230 u_long tiwin, ts_val, ts_ecr; 231 int ts_present = 0; 232 #ifdef TCPDEBUG 233 short ostate = 0; 234 #endif 235 236 tcpstat.tcps_rcvtotal++; 237 /* 238 * Get IP and TCP header together in first mbuf. 239 * Note: IP leaves IP header in first mbuf. 240 */ 241 ti = mtod(m, struct tcpiphdr *); 242 if (iphlen > sizeof (struct ip)) 243 ip_stripoptions(m, (struct mbuf *)0); 244 if (m->m_len < sizeof (struct tcpiphdr)) { 245 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 246 tcpstat.tcps_rcvshort++; 247 return; 248 } 249 ti = mtod(m, struct tcpiphdr *); 250 } 251 252 /* 253 * Checksum extended TCP header and data. 254 */ 255 tlen = ((struct ip *)ti)->ip_len; 256 len = sizeof (struct ip) + tlen; 257 ti->ti_next = ti->ti_prev = 0; 258 ti->ti_x1 = 0; 259 ti->ti_len = (u_short)tlen; 260 HTONS(ti->ti_len); 261 ti->ti_sum = in_cksum(m, len); 262 if (ti->ti_sum) { 263 tcpstat.tcps_rcvbadsum++; 264 goto drop; 265 } 266 #endif /* TUBA_INCLUDE */ 267 268 /* 269 * Check that TCP offset makes sense, 270 * pull out TCP options and adjust length. XXX 271 */ 272 off = ti->ti_off << 2; 273 if (off < sizeof (struct tcphdr) || off > tlen) { 274 tcpstat.tcps_rcvbadoff++; 275 goto drop; 276 } 277 tlen -= off; 278 ti->ti_len = tlen; 279 if (off > sizeof (struct tcphdr)) { 280 if (m->m_len < sizeof(struct ip) + off) { 281 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 282 tcpstat.tcps_rcvshort++; 283 return; 284 } 285 ti = mtod(m, struct tcpiphdr *); 286 } 287 optlen = off - sizeof (struct tcphdr); 288 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 289 /* 290 * Do quick retrieval of timestamp options ("options 291 * prediction?"). If timestamp is the only option and it's 292 * formatted as recommended in RFC 1323 appendix A, we 293 * quickly get the values now and not bother calling 294 * tcp_dooptions(), etc. 295 */ 296 if ((optlen == TCPOLEN_TSTAMP_APPA || 297 (optlen > TCPOLEN_TSTAMP_APPA && 298 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && 299 *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) && 300 (ti->ti_flags & TH_SYN) == 0) { 301 ts_present = 1; 302 ts_val = ntohl(*(u_long *)(optp + 4)); 303 ts_ecr = ntohl(*(u_long *)(optp + 8)); 304 optp = NULL; /* we've parsed the options */ 305 } 306 } 307 tiflags = ti->ti_flags; 308 309 /* 310 * Convert TCP protocol specific fields to host format. 311 */ 312 NTOHL(ti->ti_seq); 313 NTOHL(ti->ti_ack); 314 NTOHS(ti->ti_win); 315 NTOHS(ti->ti_urp); 316 317 /* 318 * Locate pcb for segment. 319 */ 320 findpcb: 321 inp = tcp_last_inpcb; 322 if (inp->inp_lport != ti->ti_dport || 323 inp->inp_fport != ti->ti_sport || 324 inp->inp_faddr.s_addr != ti->ti_src.s_addr || 325 inp->inp_laddr.s_addr != ti->ti_dst.s_addr) { 326 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport, 327 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD); 328 if (inp) 329 tcp_last_inpcb = inp; 330 ++tcpstat.tcps_pcbcachemiss; 331 } 332 333 /* 334 * If the state is CLOSED (i.e., TCB does not exist) then 335 * all data in the incoming segment is discarded. 336 * If the TCB exists but is in CLOSED state, it is embryonic, 337 * but should either do a listen or a connect soon. 338 */ 339 if (inp == 0) 340 goto dropwithreset; 341 tp = intotcpcb(inp); 342 if (tp == 0) 343 goto dropwithreset; 344 if (tp->t_state == TCPS_CLOSED) 345 goto drop; 346 347 /* Unscale the window into a 32-bit value. */ 348 if ((tiflags & TH_SYN) == 0) 349 tiwin = ti->ti_win << tp->snd_scale; 350 else 351 tiwin = ti->ti_win; 352 353 so = inp->inp_socket; 354 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 355 #ifdef TCPDEBUG 356 if (so->so_options & SO_DEBUG) { 357 ostate = tp->t_state; 358 tcp_saveti = *ti; 359 } 360 #endif 361 if (so->so_options & SO_ACCEPTCONN) { 362 so = sonewconn(so, 0); 363 if (so == 0) 364 goto drop; 365 /* 366 * This is ugly, but .... 367 * 368 * Mark socket as temporary until we're 369 * committed to keeping it. The code at 370 * ``drop'' and ``dropwithreset'' check the 371 * flag dropsocket to see if the temporary 372 * socket created here should be discarded. 373 * We mark the socket as discardable until 374 * we're committed to it below in TCPS_LISTEN. 375 */ 376 dropsocket++; 377 inp = (struct inpcb *)so->so_pcb; 378 inp->inp_laddr = ti->ti_dst; 379 inp->inp_lport = ti->ti_dport; 380 #if BSD>=43 381 inp->inp_options = ip_srcroute(); 382 #endif 383 tp = intotcpcb(inp); 384 tp->t_state = TCPS_LISTEN; 385 386 /* Compute proper scaling value from buffer space 387 */ 388 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 389 TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) 390 tp->request_r_scale++; 391 } 392 } 393 394 /* 395 * Segment received on connection. 396 * Reset idle time and keep-alive timer. 397 */ 398 tp->t_idle = 0; 399 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 400 401 /* 402 * Process options if not in LISTEN state, 403 * else do it below (after getting remote address). 404 */ 405 if (optp && tp->t_state != TCPS_LISTEN) 406 tcp_dooptions(tp, optp, optlen, ti, 407 &ts_present, &ts_val, &ts_ecr); 408 409 /* 410 * Header prediction: check for the two common cases 411 * of a uni-directional data xfer. If the packet has 412 * no control flags, is in-sequence, the window didn't 413 * change and we're not retransmitting, it's a 414 * candidate. If the length is zero and the ack moved 415 * forward, we're the sender side of the xfer. Just 416 * free the data acked & wake any higher level process 417 * that was blocked waiting for space. If the length 418 * is non-zero and the ack didn't move, we're the 419 * receiver side. If we're getting packets in-order 420 * (the reassembly queue is empty), add the data to 421 * the socket buffer and note that we need a delayed ack. 422 */ 423 if (tp->t_state == TCPS_ESTABLISHED && 424 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 425 (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && 426 ti->ti_seq == tp->rcv_nxt && 427 tiwin && tiwin == tp->snd_wnd && 428 tp->snd_nxt == tp->snd_max) { 429 430 /* 431 * If last ACK falls within this segment's sequence numbers, 432 * record the timestamp. 433 */ 434 if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && 435 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) { 436 tp->ts_recent_age = tcp_now; 437 tp->ts_recent = ts_val; 438 } 439 440 if (ti->ti_len == 0) { 441 if (SEQ_GT(ti->ti_ack, tp->snd_una) && 442 SEQ_LEQ(ti->ti_ack, tp->snd_max) && 443 tp->snd_cwnd >= tp->snd_wnd) { 444 /* 445 * this is a pure ack for outstanding data. 446 */ 447 ++tcpstat.tcps_predack; 448 if (ts_present) 449 tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 450 else if (tp->t_rtt && 451 SEQ_GT(ti->ti_ack, tp->t_rtseq)) 452 tcp_xmit_timer(tp, tp->t_rtt); 453 acked = ti->ti_ack - tp->snd_una; 454 tcpstat.tcps_rcvackpack++; 455 tcpstat.tcps_rcvackbyte += acked; 456 sbdrop(&so->so_snd, acked); 457 tp->snd_una = ti->ti_ack; 458 m_freem(m); 459 460 /* 461 * If all outstanding data are acked, stop 462 * retransmit timer, otherwise restart timer 463 * using current (possibly backed-off) value. 464 * If process is waiting for space, 465 * wakeup/selwakeup/signal. If data 466 * are ready to send, let tcp_output 467 * decide between more output or persist. 468 */ 469 if (tp->snd_una == tp->snd_max) 470 tp->t_timer[TCPT_REXMT] = 0; 471 else if (tp->t_timer[TCPT_PERSIST] == 0) 472 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 473 474 if (so->so_snd.sb_flags & SB_NOTIFY) 475 sowwakeup(so); 476 if (so->so_snd.sb_cc) 477 (void) tcp_output(tp); 478 return; 479 } 480 } else if (ti->ti_ack == tp->snd_una && 481 tp->seg_next == (struct tcpiphdr *)tp && 482 ti->ti_len <= sbspace(&so->so_rcv)) { 483 /* 484 * this is a pure, in-sequence data packet 485 * with nothing on the reassembly queue and 486 * we have enough buffer space to take it. 487 */ 488 ++tcpstat.tcps_preddat; 489 tp->rcv_nxt += ti->ti_len; 490 tcpstat.tcps_rcvpack++; 491 tcpstat.tcps_rcvbyte += ti->ti_len; 492 /* 493 * Drop TCP, IP headers and TCP options then add data 494 * to socket buffer. 495 */ 496 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 497 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 498 sbappend(&so->so_rcv, m); 499 sorwakeup(so); 500 /* 501 * If this is a small packet, then ACK now - with Nagel 502 * congestion avoidance sender won't send more until 503 * he gets an ACK. 504 */ 505 if ((unsigned)ti->ti_len < tp->t_maxseg) { 506 tp->t_flags |= TF_ACKNOW; 507 tcp_output(tp); 508 } else { 509 tp->t_flags |= TF_DELACK; 510 } 511 return; 512 } 513 } 514 515 /* 516 * Drop TCP, IP headers and TCP options. 517 */ 518 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 519 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 520 521 /* 522 * Calculate amount of space in receive window, 523 * and then do TCP input processing. 524 * Receive window is amount of space in rcv queue, 525 * but not less than advertised window. 526 */ 527 { int win; 528 529 win = sbspace(&so->so_rcv); 530 if (win < 0) 531 win = 0; 532 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 533 } 534 535 switch (tp->t_state) { 536 537 /* 538 * If the state is LISTEN then ignore segment if it contains an RST. 539 * If the segment contains an ACK then it is bad and send a RST. 540 * If it does not contain a SYN then it is not interesting; drop it. 541 * Don't bother responding if the destination was a broadcast. 542 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 543 * tp->iss, and send a segment: 544 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 545 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 546 * Fill in remote peer address fields if not previously specified. 547 * Enter SYN_RECEIVED state, and process any other fields of this 548 * segment in this state. 549 */ 550 case TCPS_LISTEN: { 551 struct mbuf *am; 552 register struct sockaddr_in *sin; 553 554 if (tiflags & TH_RST) 555 goto drop; 556 if (tiflags & TH_ACK) 557 goto dropwithreset; 558 if ((tiflags & TH_SYN) == 0) 559 goto drop; 560 /* 561 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 562 * in_broadcast() should never return true on a received 563 * packet with M_BCAST not set. 564 */ 565 if (m->m_flags & (M_BCAST|M_MCAST) || 566 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 567 goto drop; 568 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 569 if (am == NULL) 570 goto drop; 571 am->m_len = sizeof (struct sockaddr_in); 572 sin = mtod(am, struct sockaddr_in *); 573 sin->sin_family = AF_INET; 574 sin->sin_len = sizeof(*sin); 575 sin->sin_addr = ti->ti_src; 576 sin->sin_port = ti->ti_sport; 577 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 578 laddr = inp->inp_laddr; 579 if (inp->inp_laddr.s_addr == INADDR_ANY) 580 inp->inp_laddr = ti->ti_dst; 581 if (in_pcbconnect(inp, am)) { 582 inp->inp_laddr = laddr; 583 (void) m_free(am); 584 goto drop; 585 } 586 (void) m_free(am); 587 tp->t_template = tcp_template(tp); 588 if (tp->t_template == 0) { 589 tp = tcp_drop(tp, ENOBUFS); 590 dropsocket = 0; /* socket is already gone */ 591 goto drop; 592 } 593 if (optp) 594 tcp_dooptions(tp, optp, optlen, ti, 595 &ts_present, &ts_val, &ts_ecr); 596 if (iss) 597 tp->iss = iss; 598 else 599 tp->iss = tcp_iss; 600 tcp_iss += TCP_ISSINCR/2; 601 tp->irs = ti->ti_seq; 602 tcp_sendseqinit(tp); 603 tcp_rcvseqinit(tp); 604 tp->t_flags |= TF_ACKNOW; 605 tp->t_state = TCPS_SYN_RECEIVED; 606 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 607 dropsocket = 0; /* committed to socket */ 608 tcpstat.tcps_accepts++; 609 goto trimthenstep6; 610 } 611 612 /* 613 * If the state is SYN_SENT: 614 * if seg contains an ACK, but not for our SYN, drop the input. 615 * if seg contains a RST, then drop the connection. 616 * if seg does not contain SYN, then drop it. 617 * Otherwise this is an acceptable SYN segment 618 * initialize tp->rcv_nxt and tp->irs 619 * if seg contains ack then advance tp->snd_una 620 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 621 * arrange for segment to be acked (eventually) 622 * continue processing rest of data/controls, beginning with URG 623 */ 624 case TCPS_SYN_SENT: 625 if ((tiflags & TH_ACK) && 626 (SEQ_LEQ(ti->ti_ack, tp->iss) || 627 SEQ_GT(ti->ti_ack, tp->snd_max))) 628 goto dropwithreset; 629 if (tiflags & TH_RST) { 630 if (tiflags & TH_ACK) 631 tp = tcp_drop(tp, ECONNREFUSED); 632 goto drop; 633 } 634 if ((tiflags & TH_SYN) == 0) 635 goto drop; 636 if (tiflags & TH_ACK) { 637 tp->snd_una = ti->ti_ack; 638 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 639 tp->snd_nxt = tp->snd_una; 640 } 641 tp->t_timer[TCPT_REXMT] = 0; 642 tp->irs = ti->ti_seq; 643 tcp_rcvseqinit(tp); 644 tp->t_flags |= TF_ACKNOW; 645 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 646 tcpstat.tcps_connects++; 647 soisconnected(so); 648 tp->t_state = TCPS_ESTABLISHED; 649 /* Do window scaling on this connection? */ 650 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 651 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 652 tp->snd_scale = tp->requested_s_scale; 653 tp->rcv_scale = tp->request_r_scale; 654 } 655 (void) tcp_reass(tp, (struct tcpiphdr *)0, 656 (struct mbuf *)0); 657 /* 658 * if we didn't have to retransmit the SYN, 659 * use its rtt as our initial srtt & rtt var. 660 */ 661 if (tp->t_rtt) 662 tcp_xmit_timer(tp, tp->t_rtt); 663 } else 664 tp->t_state = TCPS_SYN_RECEIVED; 665 666 trimthenstep6: 667 /* 668 * Advance ti->ti_seq to correspond to first data byte. 669 * If data, trim to stay within window, 670 * dropping FIN if necessary. 671 */ 672 ti->ti_seq++; 673 if (ti->ti_len > tp->rcv_wnd) { 674 todrop = ti->ti_len - tp->rcv_wnd; 675 m_adj(m, -todrop); 676 ti->ti_len = tp->rcv_wnd; 677 tiflags &= ~TH_FIN; 678 tcpstat.tcps_rcvpackafterwin++; 679 tcpstat.tcps_rcvbyteafterwin += todrop; 680 } 681 tp->snd_wl1 = ti->ti_seq - 1; 682 tp->rcv_up = ti->ti_seq; 683 goto step6; 684 } 685 686 /* 687 * States other than LISTEN or SYN_SENT. 688 * First check timestamp, if present. 689 * Then check that at least some bytes of segment are within 690 * receive window. If segment begins before rcv_nxt, 691 * drop leading data (and SYN); if nothing left, just ack. 692 * 693 * RFC 1323 PAWS: If we have a timestamp reply on this segment 694 * and it's less than ts_recent, drop it. 695 */ 696 if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 697 TSTMP_LT(ts_val, tp->ts_recent)) { 698 699 /* Check to see if ts_recent is over 24 days old. */ 700 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 701 /* 702 * Invalidate ts_recent. If this segment updates 703 * ts_recent, the age will be reset later and ts_recent 704 * will get a valid value. If it does not, setting 705 * ts_recent to zero will at least satisfy the 706 * requirement that zero be placed in the timestamp 707 * echo reply when ts_recent isn't valid. The 708 * age isn't reset until we get a valid ts_recent 709 * because we don't want out-of-order segments to be 710 * dropped when ts_recent is old. 711 */ 712 tp->ts_recent = 0; 713 } else { 714 tcpstat.tcps_rcvduppack++; 715 tcpstat.tcps_rcvdupbyte += ti->ti_len; 716 tcpstat.tcps_pawsdrop++; 717 goto dropafterack; 718 } 719 } 720 721 todrop = tp->rcv_nxt - ti->ti_seq; 722 if (todrop > 0) { 723 if (tiflags & TH_SYN) { 724 tiflags &= ~TH_SYN; 725 ti->ti_seq++; 726 if (ti->ti_urp > 1) 727 ti->ti_urp--; 728 else 729 tiflags &= ~TH_URG; 730 todrop--; 731 } 732 if (todrop >= ti->ti_len) { 733 tcpstat.tcps_rcvduppack++; 734 tcpstat.tcps_rcvdupbyte += ti->ti_len; 735 /* 736 * If segment is just one to the left of the window, 737 * check two special cases: 738 * 1. Don't toss RST in response to 4.2-style keepalive. 739 * 2. If the only thing to drop is a FIN, we can drop 740 * it, but check the ACK or we will get into FIN 741 * wars if our FINs crossed (both CLOSING). 742 * In either case, send ACK to resynchronize, 743 * but keep on processing for RST or ACK. 744 */ 745 if ((tiflags & TH_FIN && todrop == ti->ti_len + 1) 746 #ifdef TCP_COMPAT_42 747 || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1) 748 #endif 749 ) { 750 todrop = ti->ti_len; 751 tiflags &= ~TH_FIN; 752 tp->t_flags |= TF_ACKNOW; 753 } else { 754 /* 755 * Handle the case when a bound socket connects 756 * to itself. Allow packets with a SYN and 757 * an ACK to continue with the processing. 758 */ 759 if (todrop != 0 || (tiflags & TH_ACK) == 0) 760 goto dropafterack; 761 } 762 } else { 763 tcpstat.tcps_rcvpartduppack++; 764 tcpstat.tcps_rcvpartdupbyte += todrop; 765 } 766 m_adj(m, todrop); 767 ti->ti_seq += todrop; 768 ti->ti_len -= todrop; 769 if (ti->ti_urp > todrop) 770 ti->ti_urp -= todrop; 771 else { 772 tiflags &= ~TH_URG; 773 ti->ti_urp = 0; 774 } 775 } 776 777 /* 778 * If new data are received on a connection after the 779 * user processes are gone, then RST the other end. 780 */ 781 if ((so->so_state & SS_NOFDREF) && 782 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 783 tp = tcp_close(tp); 784 tcpstat.tcps_rcvafterclose++; 785 goto dropwithreset; 786 } 787 788 /* 789 * If segment ends after window, drop trailing data 790 * (and PUSH and FIN); if nothing left, just ACK. 791 */ 792 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 793 if (todrop > 0) { 794 tcpstat.tcps_rcvpackafterwin++; 795 if (todrop >= ti->ti_len) { 796 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 797 /* 798 * If a new connection request is received 799 * while in TIME_WAIT, drop the old connection 800 * and start over if the sequence numbers 801 * are above the previous ones. 802 */ 803 if (tiflags & TH_SYN && 804 tp->t_state == TCPS_TIME_WAIT && 805 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 806 iss = tp->rcv_nxt + TCP_ISSINCR; 807 tp = tcp_close(tp); 808 goto findpcb; 809 } 810 /* 811 * If window is closed can only take segments at 812 * window edge, and have to drop data and PUSH from 813 * incoming segments. Continue processing, but 814 * remember to ack. Otherwise, drop segment 815 * and ack. 816 */ 817 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 818 tp->t_flags |= TF_ACKNOW; 819 tcpstat.tcps_rcvwinprobe++; 820 } else 821 goto dropafterack; 822 } else 823 tcpstat.tcps_rcvbyteafterwin += todrop; 824 m_adj(m, -todrop); 825 ti->ti_len -= todrop; 826 tiflags &= ~(TH_PUSH|TH_FIN); 827 } 828 829 /* 830 * If last ACK falls within this segment's sequence numbers, 831 * record its timestamp. 832 */ 833 if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && 834 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len + 835 ((tiflags & (TH_SYN|TH_FIN)) != 0))) { 836 tp->ts_recent_age = tcp_now; 837 tp->ts_recent = ts_val; 838 } 839 840 /* 841 * If the RST bit is set examine the state: 842 * SYN_RECEIVED STATE: 843 * If passive open, return to LISTEN state. 844 * If active open, inform user that connection was refused. 845 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 846 * Inform user that connection was reset, and close tcb. 847 * CLOSING, LAST_ACK, TIME_WAIT STATES 848 * Close the tcb. 849 */ 850 if (tiflags&TH_RST) switch (tp->t_state) { 851 852 case TCPS_SYN_RECEIVED: 853 so->so_error = ECONNREFUSED; 854 goto close; 855 856 case TCPS_ESTABLISHED: 857 case TCPS_FIN_WAIT_1: 858 case TCPS_FIN_WAIT_2: 859 case TCPS_CLOSE_WAIT: 860 so->so_error = ECONNRESET; 861 close: 862 tp->t_state = TCPS_CLOSED; 863 tcpstat.tcps_drops++; 864 tp = tcp_close(tp); 865 goto drop; 866 867 case TCPS_CLOSING: 868 case TCPS_LAST_ACK: 869 case TCPS_TIME_WAIT: 870 tp = tcp_close(tp); 871 goto drop; 872 } 873 874 /* 875 * If a SYN is in the window, then this is an 876 * error and we send an RST and drop the connection. 877 */ 878 if (tiflags & TH_SYN) { 879 tp = tcp_drop(tp, ECONNRESET); 880 goto dropwithreset; 881 } 882 883 /* 884 * If the ACK bit is off we drop the segment and return. 885 */ 886 if ((tiflags & TH_ACK) == 0) 887 goto drop; 888 889 /* 890 * Ack processing. 891 */ 892 switch (tp->t_state) { 893 894 /* 895 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 896 * ESTABLISHED state and continue processing, otherwise 897 * send an RST. 898 */ 899 case TCPS_SYN_RECEIVED: 900 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 901 SEQ_GT(ti->ti_ack, tp->snd_max)) 902 goto dropwithreset; 903 tcpstat.tcps_connects++; 904 soisconnected(so); 905 tp->t_state = TCPS_ESTABLISHED; 906 /* Do window scaling? */ 907 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 908 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 909 tp->snd_scale = tp->requested_s_scale; 910 tp->rcv_scale = tp->request_r_scale; 911 } 912 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0); 913 tp->snd_wl1 = ti->ti_seq - 1; 914 /* fall into ... */ 915 916 /* 917 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 918 * ACKs. If the ack is in the range 919 * tp->snd_una < ti->ti_ack <= tp->snd_max 920 * then advance tp->snd_una to ti->ti_ack and drop 921 * data from the retransmission queue. If this ACK reflects 922 * more up to date window information we update our window information. 923 */ 924 case TCPS_ESTABLISHED: 925 case TCPS_FIN_WAIT_1: 926 case TCPS_FIN_WAIT_2: 927 case TCPS_CLOSE_WAIT: 928 case TCPS_CLOSING: 929 case TCPS_LAST_ACK: 930 case TCPS_TIME_WAIT: 931 932 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 933 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 934 tcpstat.tcps_rcvdupack++; 935 /* 936 * If we have outstanding data (other than 937 * a window probe), this is a completely 938 * duplicate ack (ie, window info didn't 939 * change), the ack is the biggest we've 940 * seen and we've seen exactly our rexmt 941 * threshhold of them, assume a packet 942 * has been dropped and retransmit it. 943 * Kludge snd_nxt & the congestion 944 * window so we send only this one 945 * packet. 946 * 947 * We know we're losing at the current 948 * window size so do congestion avoidance 949 * (set ssthresh to half the current window 950 * and pull our congestion window back to 951 * the new ssthresh). 952 * 953 * Dup acks mean that packets have left the 954 * network (they're now cached at the receiver) 955 * so bump cwnd by the amount in the receiver 956 * to keep a constant cwnd packets in the 957 * network. 958 */ 959 if (tp->t_timer[TCPT_REXMT] == 0 || 960 ti->ti_ack != tp->snd_una) 961 tp->t_dupacks = 0; 962 else if (++tp->t_dupacks == tcprexmtthresh) { 963 tcp_seq onxt = tp->snd_nxt; 964 u_int win = 965 min(tp->snd_wnd, tp->snd_cwnd) / 2 / 966 tp->t_maxseg; 967 968 if (win < 2) 969 win = 2; 970 tp->snd_ssthresh = win * tp->t_maxseg; 971 tp->t_timer[TCPT_REXMT] = 0; 972 tp->t_rtt = 0; 973 tp->snd_nxt = ti->ti_ack; 974 tp->snd_cwnd = tp->t_maxseg; 975 (void) tcp_output(tp); 976 tp->snd_cwnd = tp->snd_ssthresh + 977 tp->t_maxseg * tp->t_dupacks; 978 if (SEQ_GT(onxt, tp->snd_nxt)) 979 tp->snd_nxt = onxt; 980 goto drop; 981 } else if (tp->t_dupacks > tcprexmtthresh) { 982 tp->snd_cwnd += tp->t_maxseg; 983 (void) tcp_output(tp); 984 goto drop; 985 } 986 } else 987 tp->t_dupacks = 0; 988 break; 989 } 990 /* 991 * If the congestion window was inflated to account 992 * for the other side's cached packets, retract it. 993 */ 994 if (tp->t_dupacks > tcprexmtthresh && 995 tp->snd_cwnd > tp->snd_ssthresh) 996 tp->snd_cwnd = tp->snd_ssthresh; 997 tp->t_dupacks = 0; 998 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 999 tcpstat.tcps_rcvacktoomuch++; 1000 goto dropafterack; 1001 } 1002 acked = ti->ti_ack - tp->snd_una; 1003 tcpstat.tcps_rcvackpack++; 1004 tcpstat.tcps_rcvackbyte += acked; 1005 1006 /* 1007 * If we have a timestamp reply, update smoothed 1008 * round trip time. If no timestamp is present but 1009 * transmit timer is running and timed sequence 1010 * number was acked, update smoothed round trip time. 1011 * Since we now have an rtt measurement, cancel the 1012 * timer backoff (cf., Phil Karn's retransmit alg.). 1013 * Recompute the initial retransmit timer. 1014 */ 1015 if (ts_present) 1016 tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 1017 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 1018 tcp_xmit_timer(tp,tp->t_rtt); 1019 1020 /* 1021 * If all outstanding data is acked, stop retransmit 1022 * timer and remember to restart (more output or persist). 1023 * If there is more data to be acked, restart retransmit 1024 * timer, using current (possibly backed-off) value. 1025 */ 1026 if (ti->ti_ack == tp->snd_max) { 1027 tp->t_timer[TCPT_REXMT] = 0; 1028 needoutput = 1; 1029 } else if (tp->t_timer[TCPT_PERSIST] == 0) 1030 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 1031 /* 1032 * When new data is acked, open the congestion window. 1033 * If the window gives us less than ssthresh packets 1034 * in flight, open exponentially (maxseg per packet). 1035 * Otherwise open linearly: maxseg per window 1036 * (maxseg^2 / cwnd per packet). 1037 */ 1038 { 1039 register u_int cw = tp->snd_cwnd; 1040 register u_int incr = tp->t_maxseg; 1041 1042 if (cw > tp->snd_ssthresh) 1043 incr = incr * incr / cw; 1044 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1045 } 1046 if (acked > so->so_snd.sb_cc) { 1047 tp->snd_wnd -= so->so_snd.sb_cc; 1048 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1049 ourfinisacked = 1; 1050 } else { 1051 sbdrop(&so->so_snd, acked); 1052 tp->snd_wnd -= acked; 1053 ourfinisacked = 0; 1054 } 1055 if (so->so_snd.sb_flags & SB_NOTIFY) 1056 sowwakeup(so); 1057 tp->snd_una = ti->ti_ack; 1058 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1059 tp->snd_nxt = tp->snd_una; 1060 1061 switch (tp->t_state) { 1062 1063 /* 1064 * In FIN_WAIT_1 STATE in addition to the processing 1065 * for the ESTABLISHED state if our FIN is now acknowledged 1066 * then enter FIN_WAIT_2. 1067 */ 1068 case TCPS_FIN_WAIT_1: 1069 if (ourfinisacked) { 1070 /* 1071 * If we can't receive any more 1072 * data, then closing user can proceed. 1073 * Starting the timer is contrary to the 1074 * specification, but if we don't get a FIN 1075 * we'll hang forever. 1076 */ 1077 if (so->so_state & SS_CANTRCVMORE) { 1078 soisdisconnected(so); 1079 tp->t_timer[TCPT_2MSL] = tcp_maxidle; 1080 } 1081 tp->t_state = TCPS_FIN_WAIT_2; 1082 } 1083 break; 1084 1085 /* 1086 * In CLOSING STATE in addition to the processing for 1087 * the ESTABLISHED state if the ACK acknowledges our FIN 1088 * then enter the TIME-WAIT state, otherwise ignore 1089 * the segment. 1090 */ 1091 case TCPS_CLOSING: 1092 if (ourfinisacked) { 1093 tp->t_state = TCPS_TIME_WAIT; 1094 tcp_canceltimers(tp); 1095 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1096 soisdisconnected(so); 1097 } 1098 break; 1099 1100 /* 1101 * In LAST_ACK, we may still be waiting for data to drain 1102 * and/or to be acked, as well as for the ack of our FIN. 1103 * If our FIN is now acknowledged, delete the TCB, 1104 * enter the closed state and return. 1105 */ 1106 case TCPS_LAST_ACK: 1107 if (ourfinisacked) { 1108 tp = tcp_close(tp); 1109 goto drop; 1110 } 1111 break; 1112 1113 /* 1114 * In TIME_WAIT state the only thing that should arrive 1115 * is a retransmission of the remote FIN. Acknowledge 1116 * it and restart the finack timer. 1117 */ 1118 case TCPS_TIME_WAIT: 1119 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1120 goto dropafterack; 1121 } 1122 } 1123 1124 step6: 1125 /* 1126 * Update window information. 1127 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1128 */ 1129 if ((tiflags & TH_ACK) && 1130 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 1131 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 1132 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { 1133 /* keep track of pure window updates */ 1134 if (ti->ti_len == 0 && 1135 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 1136 tcpstat.tcps_rcvwinupd++; 1137 tp->snd_wnd = tiwin; 1138 tp->snd_wl1 = ti->ti_seq; 1139 tp->snd_wl2 = ti->ti_ack; 1140 if (tp->snd_wnd > tp->max_sndwnd) 1141 tp->max_sndwnd = tp->snd_wnd; 1142 needoutput = 1; 1143 } 1144 1145 /* 1146 * Process segments with URG. 1147 */ 1148 if ((tiflags & TH_URG) && ti->ti_urp && 1149 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1150 /* 1151 * This is a kludge, but if we receive and accept 1152 * random urgent pointers, we'll crash in 1153 * soreceive. It's hard to imagine someone 1154 * actually wanting to send this much urgent data. 1155 */ 1156 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 1157 ti->ti_urp = 0; /* XXX */ 1158 tiflags &= ~TH_URG; /* XXX */ 1159 goto dodata; /* XXX */ 1160 } 1161 /* 1162 * If this segment advances the known urgent pointer, 1163 * then mark the data stream. This should not happen 1164 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1165 * a FIN has been received from the remote side. 1166 * In these states we ignore the URG. 1167 * 1168 * According to RFC961 (Assigned Protocols), 1169 * the urgent pointer points to the last octet 1170 * of urgent data. We continue, however, 1171 * to consider it to indicate the first octet 1172 * of data past the urgent section as the original 1173 * spec states (in one of two places). 1174 */ 1175 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 1176 tp->rcv_up = ti->ti_seq + ti->ti_urp; 1177 so->so_oobmark = so->so_rcv.sb_cc + 1178 (tp->rcv_up - tp->rcv_nxt) - 1; 1179 if (so->so_oobmark == 0) 1180 so->so_state |= SS_RCVATMARK; 1181 sohasoutofband(so); 1182 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1183 } 1184 /* 1185 * Remove out of band data so doesn't get presented to user. 1186 * This can happen independent of advancing the URG pointer, 1187 * but if two URG's are pending at once, some out-of-band 1188 * data may creep in... ick. 1189 */ 1190 if (ti->ti_urp <= (u_long)ti->ti_len 1191 #ifdef SO_OOBINLINE 1192 && (so->so_options & SO_OOBINLINE) == 0 1193 #endif 1194 ) 1195 tcp_pulloutofband(so, ti, m); 1196 } else 1197 /* 1198 * If no out of band data is expected, 1199 * pull receive urgent pointer along 1200 * with the receive window. 1201 */ 1202 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1203 tp->rcv_up = tp->rcv_nxt; 1204 dodata: /* XXX */ 1205 1206 /* 1207 * Process the segment text, merging it into the TCP sequencing queue, 1208 * and arranging for acknowledgment of receipt if necessary. 1209 * This process logically involves adjusting tp->rcv_wnd as data 1210 * is presented to the user (this happens in tcp_usrreq.c, 1211 * case PRU_RCVD). If a FIN has already been received on this 1212 * connection then we just ignore the text. 1213 */ 1214 if ((ti->ti_len || (tiflags&TH_FIN)) && 1215 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1216 TCP_REASS(tp, ti, m, so, tiflags); 1217 /* 1218 * Note the amount of data that peer has sent into 1219 * our window, in order to estimate the sender's 1220 * buffer size. 1221 */ 1222 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1223 } else { 1224 m_freem(m); 1225 tiflags &= ~TH_FIN; 1226 } 1227 1228 /* 1229 * If FIN is received ACK the FIN and let the user know 1230 * that the connection is closing. 1231 */ 1232 if (tiflags & TH_FIN) { 1233 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1234 socantrcvmore(so); 1235 tp->t_flags |= TF_ACKNOW; 1236 tp->rcv_nxt++; 1237 } 1238 switch (tp->t_state) { 1239 1240 /* 1241 * In SYN_RECEIVED and ESTABLISHED STATES 1242 * enter the CLOSE_WAIT state. 1243 */ 1244 case TCPS_SYN_RECEIVED: 1245 case TCPS_ESTABLISHED: 1246 tp->t_state = TCPS_CLOSE_WAIT; 1247 break; 1248 1249 /* 1250 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1251 * enter the CLOSING state. 1252 */ 1253 case TCPS_FIN_WAIT_1: 1254 tp->t_state = TCPS_CLOSING; 1255 break; 1256 1257 /* 1258 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1259 * starting the time-wait timer, turning off the other 1260 * standard timers. 1261 */ 1262 case TCPS_FIN_WAIT_2: 1263 tp->t_state = TCPS_TIME_WAIT; 1264 tcp_canceltimers(tp); 1265 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1266 soisdisconnected(so); 1267 break; 1268 1269 /* 1270 * In TIME_WAIT state restart the 2 MSL time_wait timer. 1271 */ 1272 case TCPS_TIME_WAIT: 1273 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1274 break; 1275 } 1276 } 1277 #ifdef TCPDEBUG 1278 if (so->so_options & SO_DEBUG) 1279 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1280 #endif 1281 1282 /* 1283 * If this is a small packet, then ACK now - with Nagel 1284 * congestion avoidance sender won't send more until 1285 * he gets an ACK. 1286 */ 1287 if (ti->ti_len && ((unsigned)ti->ti_len < tp->t_maxseg)) 1288 tp->t_flags |= TF_ACKNOW; 1289 1290 /* 1291 * Return any desired output. 1292 */ 1293 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1294 (void) tcp_output(tp); 1295 return; 1296 1297 dropafterack: 1298 /* 1299 * Generate an ACK dropping incoming segment if it occupies 1300 * sequence space, where the ACK reflects our state. 1301 */ 1302 if (tiflags & TH_RST) 1303 goto drop; 1304 m_freem(m); 1305 tp->t_flags |= TF_ACKNOW; 1306 (void) tcp_output(tp); 1307 return; 1308 1309 dropwithreset: 1310 /* 1311 * Generate a RST, dropping incoming segment. 1312 * Make ACK acceptable to originator of segment. 1313 * Don't bother to respond if destination was broadcast/multicast. 1314 */ 1315 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 1316 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 1317 goto drop; 1318 if (tiflags & TH_ACK) 1319 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 1320 else { 1321 if (tiflags & TH_SYN) 1322 ti->ti_len++; 1323 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1324 TH_RST|TH_ACK); 1325 } 1326 /* destroy temporarily created socket */ 1327 if (dropsocket) 1328 (void) soabort(so); 1329 return; 1330 1331 drop: 1332 /* 1333 * Drop space held by incoming segment and return. 1334 */ 1335 #ifdef TCPDEBUG 1336 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1337 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1338 #endif 1339 m_freem(m); 1340 /* destroy temporarily created socket */ 1341 if (dropsocket) 1342 (void) soabort(so); 1343 return; 1344 #ifndef TUBA_INCLUDE 1345 } 1346 1347 void 1348 tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr) 1349 struct tcpcb *tp; 1350 u_char *cp; 1351 int cnt; 1352 struct tcpiphdr *ti; 1353 int *ts_present; 1354 u_long *ts_val, *ts_ecr; 1355 { 1356 u_short mss; 1357 int opt, optlen; 1358 1359 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1360 opt = cp[0]; 1361 if (opt == TCPOPT_EOL) 1362 break; 1363 if (opt == TCPOPT_NOP) 1364 optlen = 1; 1365 else { 1366 optlen = cp[1]; 1367 if (optlen <= 0) 1368 break; 1369 } 1370 switch (opt) { 1371 1372 default: 1373 continue; 1374 1375 case TCPOPT_MAXSEG: 1376 if (optlen != TCPOLEN_MAXSEG) 1377 continue; 1378 if (!(ti->ti_flags & TH_SYN)) 1379 continue; 1380 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 1381 NTOHS(mss); 1382 (void) tcp_mss(tp, mss); /* sets t_maxseg */ 1383 break; 1384 1385 case TCPOPT_WINDOW: 1386 if (optlen != TCPOLEN_WINDOW) 1387 continue; 1388 if (!(ti->ti_flags & TH_SYN)) 1389 continue; 1390 tp->t_flags |= TF_RCVD_SCALE; 1391 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 1392 break; 1393 1394 case TCPOPT_TIMESTAMP: 1395 if (optlen != TCPOLEN_TIMESTAMP) 1396 continue; 1397 *ts_present = 1; 1398 bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val)); 1399 NTOHL(*ts_val); 1400 bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr)); 1401 NTOHL(*ts_ecr); 1402 1403 /* 1404 * A timestamp received in a SYN makes 1405 * it ok to send timestamp requests and replies. 1406 */ 1407 if (ti->ti_flags & TH_SYN) { 1408 tp->t_flags |= TF_RCVD_TSTMP; 1409 tp->ts_recent = *ts_val; 1410 tp->ts_recent_age = tcp_now; 1411 } 1412 break; 1413 } 1414 } 1415 } 1416 1417 /* 1418 * Pull out of band byte out of a segment so 1419 * it doesn't appear in the user's data queue. 1420 * It is still reflected in the segment length for 1421 * sequencing purposes. 1422 */ 1423 void 1424 tcp_pulloutofband(so, ti, m) 1425 struct socket *so; 1426 struct tcpiphdr *ti; 1427 register struct mbuf *m; 1428 { 1429 int cnt = ti->ti_urp - 1; 1430 1431 while (cnt >= 0) { 1432 if (m->m_len > cnt) { 1433 char *cp = mtod(m, caddr_t) + cnt; 1434 struct tcpcb *tp = sototcpcb(so); 1435 1436 tp->t_iobc = *cp; 1437 tp->t_oobflags |= TCPOOB_HAVEDATA; 1438 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1439 m->m_len--; 1440 return; 1441 } 1442 cnt -= m->m_len; 1443 m = m->m_next; 1444 if (m == 0) 1445 break; 1446 } 1447 panic("tcp_pulloutofband"); 1448 } 1449 1450 /* 1451 * Collect new round-trip time estimate 1452 * and update averages and current timeout. 1453 */ 1454 void 1455 tcp_xmit_timer(tp, rtt) 1456 register struct tcpcb *tp; 1457 short rtt; 1458 { 1459 register short delta; 1460 1461 tcpstat.tcps_rttupdated++; 1462 if (tp->t_srtt != 0) { 1463 /* 1464 * srtt is stored as fixed point with 3 bits after the 1465 * binary point (i.e., scaled by 8). The following magic 1466 * is equivalent to the smoothing algorithm in rfc793 with 1467 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 1468 * point). Adjust rtt to origin 0. 1469 */ 1470 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); 1471 if ((tp->t_srtt += delta) <= 0) 1472 tp->t_srtt = 1; 1473 /* 1474 * We accumulate a smoothed rtt variance (actually, a 1475 * smoothed mean difference), then set the retransmit 1476 * timer to smoothed rtt + 4 times the smoothed variance. 1477 * rttvar is stored as fixed point with 2 bits after the 1478 * binary point (scaled by 4). The following is 1479 * equivalent to rfc793 smoothing with an alpha of .75 1480 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 1481 * rfc793's wired-in beta. 1482 */ 1483 if (delta < 0) 1484 delta = -delta; 1485 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 1486 if ((tp->t_rttvar += delta) <= 0) 1487 tp->t_rttvar = 1; 1488 } else { 1489 /* 1490 * No rtt measurement yet - use the unsmoothed rtt. 1491 * Set the variance to half the rtt (so our first 1492 * retransmit happens at 3*rtt). 1493 */ 1494 tp->t_srtt = rtt << TCP_RTT_SHIFT; 1495 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 1496 } 1497 tp->t_rtt = 0; 1498 tp->t_rxtshift = 0; 1499 1500 /* 1501 * the retransmit should happen at rtt + 4 * rttvar. 1502 * Because of the way we do the smoothing, srtt and rttvar 1503 * will each average +1/2 tick of bias. When we compute 1504 * the retransmit timer, we want 1/2 tick of rounding and 1505 * 1 extra tick because of +-1/2 tick uncertainty in the 1506 * firing of the timer. The bias will give us exactly the 1507 * 1.5 tick we need. But, because the bias is 1508 * statistical, we have to test that we don't drop below 1509 * the minimum feasible timer (which is 2 ticks). 1510 */ 1511 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 1512 tp->t_rttmin, TCPTV_REXMTMAX); 1513 1514 /* 1515 * We received an ack for a packet that wasn't retransmitted; 1516 * it is probably safe to discard any error indications we've 1517 * received recently. This isn't quite right, but close enough 1518 * for now (a route might have failed after we sent a segment, 1519 * and the return path might not be symmetrical). 1520 */ 1521 tp->t_softerror = 0; 1522 } 1523 1524 /* 1525 * Determine a reasonable value for maxseg size. 1526 * If the route is known, check route for mtu. 1527 * If none, use an mss that can be handled on the outgoing 1528 * interface without forcing IP to fragment; if bigger than 1529 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 1530 * to utilize large mbufs. If no route is found, route has no mtu, 1531 * or the destination isn't local, use a default, hopefully conservative 1532 * size (usually 512 or the default IP max size, but no more than the mtu 1533 * of the interface), as we can't discover anything about intervening 1534 * gateways or networks. We also initialize the congestion/slow start 1535 * window to be a single segment if the destination isn't local. 1536 * While looking at the routing entry, we also initialize other path-dependent 1537 * parameters from pre-set or cached values in the routing entry. 1538 */ 1539 int 1540 tcp_mss(tp, offer) 1541 register struct tcpcb *tp; 1542 u_int offer; 1543 { 1544 struct route *ro; 1545 register struct rtentry *rt; 1546 struct ifnet *ifp; 1547 register int rtt, mss; 1548 u_long bufsize; 1549 struct inpcb *inp; 1550 struct socket *so; 1551 extern int tcp_mssdflt; 1552 1553 inp = tp->t_inpcb; 1554 ro = &inp->inp_route; 1555 1556 if ((rt = ro->ro_rt) == (struct rtentry *)0) { 1557 /* No route yet, so try to acquire one */ 1558 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1559 ro->ro_dst.sa_family = AF_INET; 1560 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 1561 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 1562 inp->inp_faddr; 1563 rtalloc(ro); 1564 } 1565 if ((rt = ro->ro_rt) == (struct rtentry *)0) 1566 return (tcp_mssdflt); 1567 } 1568 ifp = rt->rt_ifp; 1569 so = inp->inp_socket; 1570 1571 #ifdef RTV_MTU /* if route characteristics exist ... */ 1572 /* 1573 * While we're here, check if there's an initial rtt 1574 * or rttvar. Convert from the route-table units 1575 * to scaled multiples of the slow timeout timer. 1576 */ 1577 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 1578 /* 1579 * XXX the lock bit for MTU indicates that the value 1580 * is also a minimum value; this is subject to time. 1581 */ 1582 if (rt->rt_rmx.rmx_locks & RTV_RTT) 1583 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 1584 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 1585 if (rt->rt_rmx.rmx_rttvar) 1586 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 1587 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 1588 else 1589 /* default variation is +- 1 rtt */ 1590 tp->t_rttvar = 1591 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 1592 TCPT_RANGESET(tp->t_rxtcur, 1593 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 1594 tp->t_rttmin, TCPTV_REXMTMAX); 1595 } 1596 /* 1597 * if there's an mtu associated with the route, use it 1598 */ 1599 if (rt->rt_rmx.rmx_mtu) 1600 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 1601 else 1602 #endif /* RTV_MTU */ 1603 { 1604 mss = ifp->if_mtu - sizeof(struct tcpiphdr); 1605 #if (MCLBYTES & (MCLBYTES - 1)) == 0 1606 if (mss > MCLBYTES) 1607 mss &= ~(MCLBYTES-1); 1608 #else 1609 if (mss > MCLBYTES) 1610 mss = mss / MCLBYTES * MCLBYTES; 1611 #endif 1612 if (!in_localaddr(inp->inp_faddr)) 1613 mss = min(mss, tcp_mssdflt); 1614 } 1615 /* 1616 * The current mss, t_maxseg, is initialized to the default value. 1617 * If we compute a smaller value, reduce the current mss. 1618 * If we compute a larger value, return it for use in sending 1619 * a max seg size option, but don't store it for use 1620 * unless we received an offer at least that large from peer. 1621 * However, do not accept offers under 32 bytes. 1622 */ 1623 if (offer) 1624 mss = min(mss, offer); 1625 mss = max(mss, 32); /* sanity */ 1626 if (mss < tp->t_maxseg || offer != 0) { 1627 /* 1628 * If there's a pipesize, change the socket buffer 1629 * to that size. Make the socket buffers an integral 1630 * number of mss units; if the mss is larger than 1631 * the socket buffer, decrease the mss. 1632 */ 1633 #ifdef RTV_SPIPE 1634 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 1635 #endif 1636 bufsize = so->so_snd.sb_hiwat; 1637 if (bufsize < mss) 1638 mss = bufsize; 1639 else { 1640 bufsize = roundup(bufsize, mss); 1641 if (bufsize > sb_max) 1642 bufsize = sb_max; 1643 (void)sbreserve(&so->so_snd, bufsize); 1644 } 1645 tp->t_maxseg = mss; 1646 1647 #ifdef RTV_RPIPE 1648 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 1649 #endif 1650 bufsize = so->so_rcv.sb_hiwat; 1651 if (bufsize > mss) { 1652 bufsize = roundup(bufsize, mss); 1653 if (bufsize > sb_max) 1654 bufsize = sb_max; 1655 (void)sbreserve(&so->so_rcv, bufsize); 1656 } 1657 } 1658 tp->snd_cwnd = mss; 1659 1660 #ifdef RTV_SSTHRESH 1661 if (rt->rt_rmx.rmx_ssthresh) { 1662 /* 1663 * There's some sort of gateway or interface 1664 * buffer limit on the path. Use this to set 1665 * the slow start threshhold, but set the 1666 * threshold to no less than 2*mss. 1667 */ 1668 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 1669 } 1670 #endif /* RTV_MTU */ 1671 return (mss); 1672 } 1673 #endif /* TUBA_INCLUDE */ 1674