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