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