1 /* 2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 * 12 * @(#)tcp_input.c 7.17 (Berkeley) 03/16/88 13 */ 14 15 #include "param.h" 16 #include "systm.h" 17 #include "mbuf.h" 18 #include "protosw.h" 19 #include "socket.h" 20 #include "socketvar.h" 21 #include "errno.h" 22 23 #include "../net/if.h" 24 #include "../net/route.h" 25 26 #include "in.h" 27 #include "in_pcb.h" 28 #include "in_systm.h" 29 #include "ip.h" 30 #include "ip_var.h" 31 #include "tcp.h" 32 #include "tcp_fsm.h" 33 #include "tcp_seq.h" 34 #include "tcp_timer.h" 35 #include "tcp_var.h" 36 #include "tcpip.h" 37 #include "tcp_debug.h" 38 39 int tcpprintfs = 0; 40 int tcpcksum = 1; 41 int tcprexmtthresh = 3; 42 struct tcpiphdr tcp_saveti; 43 extern tcpnodelack; 44 45 struct tcpcb *tcp_newtcpcb(); 46 47 /* 48 * Insert segment ti into reassembly queue of tcp with 49 * control block tp. Return TH_FIN if reassembly now includes 50 * a segment with FIN. The macro form does the common case inline 51 * (segment is the next to be received on an established connection, 52 * and the queue is empty), avoiding linkage into and removal 53 * from the queue and repetition of various conversions. 54 */ 55 #define TCP_REASS(tp, ti, m, so, flags) { \ 56 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 57 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 58 (tp)->t_state == TCPS_ESTABLISHED) { \ 59 (tp)->rcv_nxt += (ti)->ti_len; \ 60 flags = (ti)->ti_flags & TH_FIN; \ 61 tcpstat.tcps_rcvpack++;\ 62 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 63 sbappend(&(so)->so_rcv, (m)); \ 64 sorwakeup(so); \ 65 } else \ 66 (flags) = tcp_reass((tp), (ti)); \ 67 } 68 69 tcp_reass(tp, ti) 70 register struct tcpcb *tp; 71 register struct tcpiphdr *ti; 72 { 73 register struct tcpiphdr *q; 74 struct socket *so = tp->t_inpcb->inp_socket; 75 struct mbuf *m; 76 int flags; 77 78 /* 79 * Call with ti==0 after become established to 80 * force pre-ESTABLISHED data up to user socket. 81 */ 82 if (ti == 0) 83 goto present; 84 85 /* 86 * Find a segment which begins after this one does. 87 */ 88 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 89 q = (struct tcpiphdr *)q->ti_next) 90 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 91 break; 92 93 /* 94 * If there is a preceding segment, it may provide some of 95 * our data already. If so, drop the data from the incoming 96 * segment. If it provides all of our data, drop us. 97 */ 98 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 99 register int i; 100 q = (struct tcpiphdr *)q->ti_prev; 101 /* conversion to int (in i) handles seq wraparound */ 102 i = q->ti_seq + q->ti_len - ti->ti_seq; 103 if (i > 0) { 104 if (i >= ti->ti_len) { 105 tcpstat.tcps_rcvduppack++; 106 tcpstat.tcps_rcvdupbyte += ti->ti_len; 107 goto drop; 108 } 109 m_adj(dtom(ti), i); 110 ti->ti_len -= i; 111 ti->ti_seq += i; 112 } 113 q = (struct tcpiphdr *)(q->ti_next); 114 } 115 tcpstat.tcps_rcvoopack++; 116 tcpstat.tcps_rcvoobyte += ti->ti_len; 117 118 /* 119 * While we overlap succeeding segments trim them or, 120 * if they are completely covered, dequeue them. 121 */ 122 while (q != (struct tcpiphdr *)tp) { 123 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 124 if (i <= 0) 125 break; 126 if (i < q->ti_len) { 127 q->ti_seq += i; 128 q->ti_len -= i; 129 m_adj(dtom(q), i); 130 break; 131 } 132 q = (struct tcpiphdr *)q->ti_next; 133 m = dtom(q->ti_prev); 134 remque(q->ti_prev); 135 m_freem(m); 136 } 137 138 /* 139 * Stick new segment in its place. 140 */ 141 insque(ti, q->ti_prev); 142 143 present: 144 /* 145 * Present data to user, advancing rcv_nxt through 146 * completed sequence space. 147 */ 148 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 149 return (0); 150 ti = tp->seg_next; 151 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 152 return (0); 153 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 154 return (0); 155 do { 156 tp->rcv_nxt += ti->ti_len; 157 flags = ti->ti_flags & TH_FIN; 158 remque(ti); 159 m = dtom(ti); 160 ti = (struct tcpiphdr *)ti->ti_next; 161 if (so->so_state & SS_CANTRCVMORE) 162 m_freem(m); 163 else 164 sbappend(&so->so_rcv, m); 165 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 166 sorwakeup(so); 167 return (flags); 168 drop: 169 m_freem(dtom(ti)); 170 return (0); 171 } 172 173 /* 174 * TCP input routine, follows pages 65-76 of the 175 * protocol specification dated September, 1981 very closely. 176 */ 177 tcp_input(m0) 178 struct mbuf *m0; 179 { 180 register struct tcpiphdr *ti; 181 struct inpcb *inp; 182 register struct mbuf *m; 183 struct mbuf *om = 0; 184 int len, tlen, off; 185 register struct tcpcb *tp = 0; 186 register int tiflags; 187 struct socket *so; 188 int todrop, acked, ourfinisacked, needoutput = 0; 189 short ostate; 190 struct in_addr laddr; 191 int dropsocket = 0; 192 int iss = 0; 193 194 tcpstat.tcps_rcvtotal++; 195 /* 196 * Get IP and TCP header together in first mbuf. 197 * Note: IP leaves IP header in first mbuf. 198 */ 199 m = m0; 200 ti = mtod(m, struct tcpiphdr *); 201 if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 202 ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 203 if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 204 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 205 tcpstat.tcps_rcvshort++; 206 return; 207 } 208 ti = mtod(m, struct tcpiphdr *); 209 } 210 211 /* 212 * Checksum extended TCP header and data. 213 */ 214 tlen = ((struct ip *)ti)->ip_len; 215 len = sizeof (struct ip) + tlen; 216 if (tcpcksum) { 217 ti->ti_next = ti->ti_prev = 0; 218 ti->ti_x1 = 0; 219 ti->ti_len = (u_short)tlen; 220 ti->ti_len = htons((u_short)ti->ti_len); 221 if (ti->ti_sum = in_cksum(m, len)) { 222 if (tcpprintfs) 223 printf("tcp sum: src %x\n", ti->ti_src); 224 tcpstat.tcps_rcvbadsum++; 225 goto drop; 226 } 227 } 228 229 /* 230 * Check that TCP offset makes sense, 231 * pull out TCP options and adjust length. 232 */ 233 off = ti->ti_off << 2; 234 if (off < sizeof (struct tcphdr) || off > tlen) { 235 if (tcpprintfs) 236 printf("tcp off: src %x off %d\n", ti->ti_src, off); 237 tcpstat.tcps_rcvbadoff++; 238 goto drop; 239 } 240 tlen -= off; 241 ti->ti_len = tlen; 242 if (off > sizeof (struct tcphdr)) { 243 if (m->m_len < sizeof(struct ip) + off) { 244 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 245 tcpstat.tcps_rcvshort++; 246 return; 247 } 248 ti = mtod(m, struct tcpiphdr *); 249 } 250 om = m_get(M_DONTWAIT, MT_DATA); 251 if (om == 0) 252 goto drop; 253 om->m_len = off - sizeof (struct tcphdr); 254 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 255 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 256 m->m_len -= om->m_len; 257 bcopy(op+om->m_len, op, 258 (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 259 } 260 } 261 tiflags = ti->ti_flags; 262 263 /* 264 * Drop TCP and IP headers; TCP options were dropped above. 265 */ 266 m->m_off += sizeof(struct tcpiphdr); 267 m->m_len -= sizeof(struct tcpiphdr); 268 269 /* 270 * Convert TCP protocol specific fields to host format. 271 */ 272 ti->ti_seq = ntohl(ti->ti_seq); 273 ti->ti_ack = ntohl(ti->ti_ack); 274 ti->ti_win = ntohs(ti->ti_win); 275 ti->ti_urp = ntohs(ti->ti_urp); 276 277 /* 278 * Locate pcb for segment. 279 */ 280 findpcb: 281 inp = in_pcblookup 282 (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 283 INPLOOKUP_WILDCARD); 284 285 /* 286 * If the state is CLOSED (i.e., TCB does not exist) then 287 * all data in the incoming segment is discarded. 288 * If the TCB exists but is in CLOSED state, it is embryonic, 289 * but should either do a listen or a connect soon. 290 */ 291 if (inp == 0) 292 goto dropwithreset; 293 tp = intotcpcb(inp); 294 if (tp == 0) 295 goto dropwithreset; 296 if (tp->t_state == TCPS_CLOSED) 297 goto drop; 298 so = inp->inp_socket; 299 if (so->so_options & SO_DEBUG) { 300 ostate = tp->t_state; 301 tcp_saveti = *ti; 302 } 303 if (so->so_options & SO_ACCEPTCONN) { 304 so = sonewconn(so); 305 if (so == 0) 306 goto drop; 307 /* 308 * This is ugly, but .... 309 * 310 * Mark socket as temporary until we're 311 * committed to keeping it. The code at 312 * ``drop'' and ``dropwithreset'' check the 313 * flag dropsocket to see if the temporary 314 * socket created here should be discarded. 315 * We mark the socket as discardable until 316 * we're committed to it below in TCPS_LISTEN. 317 */ 318 dropsocket++; 319 inp = (struct inpcb *)so->so_pcb; 320 inp->inp_laddr = ti->ti_dst; 321 inp->inp_lport = ti->ti_dport; 322 inp->inp_options = ip_srcroute(); 323 tp = intotcpcb(inp); 324 tp->t_state = TCPS_LISTEN; 325 } 326 327 /* 328 * Segment received on connection. 329 * Reset idle time and keep-alive timer. 330 */ 331 tp->t_idle = 0; 332 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 333 334 /* 335 * Process options if not in LISTEN state, 336 * else do it below (after getting remote address). 337 */ 338 if (om && tp->t_state != TCPS_LISTEN) { 339 tcp_dooptions(tp, om, ti); 340 om = 0; 341 } 342 343 /* 344 * Calculate amount of space in receive window, 345 * and then do TCP input processing. 346 * Receive window is amount of space in rcv queue, 347 * but not less than advertised window. 348 */ 349 { int win; 350 351 win = sbspace(&so->so_rcv); 352 if (win < 0) 353 win = 0; 354 tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 355 } 356 357 switch (tp->t_state) { 358 359 /* 360 * If the state is LISTEN then ignore segment if it contains an RST. 361 * If the segment contains an ACK then it is bad and send a RST. 362 * If it does not contain a SYN then it is not interesting; drop it. 363 * Don't bother responding if the destination was a broadcast. 364 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 365 * tp->iss, and send a segment: 366 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 367 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 368 * Fill in remote peer address fields if not previously specified. 369 * Enter SYN_RECEIVED state, and process any other fields of this 370 * segment in this state. 371 */ 372 case TCPS_LISTEN: { 373 struct mbuf *am; 374 register struct sockaddr_in *sin; 375 376 if (tiflags & TH_RST) 377 goto drop; 378 if (tiflags & TH_ACK) 379 goto dropwithreset; 380 if ((tiflags & TH_SYN) == 0) 381 goto drop; 382 if (in_broadcast(ti->ti_dst)) 383 goto drop; 384 am = m_get(M_DONTWAIT, MT_SONAME); 385 if (am == NULL) 386 goto drop; 387 am->m_len = sizeof (struct sockaddr_in); 388 sin = mtod(am, struct sockaddr_in *); 389 sin->sin_family = AF_INET; 390 sin->sin_addr = ti->ti_src; 391 sin->sin_port = ti->ti_sport; 392 laddr = inp->inp_laddr; 393 if (inp->inp_laddr.s_addr == INADDR_ANY) 394 inp->inp_laddr = ti->ti_dst; 395 if (in_pcbconnect(inp, am)) { 396 inp->inp_laddr = laddr; 397 (void) m_free(am); 398 goto drop; 399 } 400 (void) m_free(am); 401 tp->t_template = tcp_template(tp); 402 if (tp->t_template == 0) { 403 tp = tcp_drop(tp, ENOBUFS); 404 dropsocket = 0; /* socket is already gone */ 405 goto drop; 406 } 407 if (om) { 408 tcp_dooptions(tp, om, ti); 409 om = 0; 410 } 411 if (iss) 412 tp->iss = iss; 413 else 414 tp->iss = tcp_iss; 415 tcp_iss += TCP_ISSINCR/2; 416 tp->irs = ti->ti_seq; 417 tcp_sendseqinit(tp); 418 tcp_rcvseqinit(tp); 419 tp->t_flags |= TF_ACKNOW; 420 tp->t_state = TCPS_SYN_RECEIVED; 421 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 422 dropsocket = 0; /* committed to socket */ 423 tcpstat.tcps_accepts++; 424 goto trimthenstep6; 425 } 426 427 /* 428 * If the state is SYN_SENT: 429 * if seg contains an ACK, but not for our SYN, drop the input. 430 * if seg contains a RST, then drop the connection. 431 * if seg does not contain SYN, then drop it. 432 * Otherwise this is an acceptable SYN segment 433 * initialize tp->rcv_nxt and tp->irs 434 * if seg contains ack then advance tp->snd_una 435 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 436 * arrange for segment to be acked (eventually) 437 * continue processing rest of data/controls, beginning with URG 438 */ 439 case TCPS_SYN_SENT: 440 if ((tiflags & TH_ACK) && 441 (SEQ_LEQ(ti->ti_ack, tp->iss) || 442 SEQ_GT(ti->ti_ack, tp->snd_max))) 443 goto dropwithreset; 444 if (tiflags & TH_RST) { 445 if (tiflags & TH_ACK) 446 tp = tcp_drop(tp, ECONNREFUSED); 447 goto drop; 448 } 449 if ((tiflags & TH_SYN) == 0) 450 goto drop; 451 if (tiflags & TH_ACK) { 452 tp->snd_una = ti->ti_ack; 453 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 454 tp->snd_nxt = tp->snd_una; 455 } 456 tp->t_timer[TCPT_REXMT] = 0; 457 tp->irs = ti->ti_seq; 458 tcp_rcvseqinit(tp); 459 tp->t_flags |= TF_ACKNOW; 460 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 461 tcpstat.tcps_connects++; 462 soisconnected(so); 463 tp->t_state = TCPS_ESTABLISHED; 464 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 465 (void) tcp_reass(tp, (struct tcpiphdr *)0); 466 /* 467 * if we didn't have to retransmit the SYN, 468 * use its rtt as our initial srtt & rtt var. 469 */ 470 if (tp->t_rtt) { 471 tp->t_srtt = tp->t_rtt << 3; 472 tp->t_rttvar = tp->t_rtt << 1; 473 TCPT_RANGESET(tp->t_rxtcur, 474 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 475 TCPTV_MIN, TCPTV_REXMTMAX); 476 tp->t_rtt = 0; 477 } 478 } else 479 tp->t_state = TCPS_SYN_RECEIVED; 480 481 trimthenstep6: 482 /* 483 * Advance ti->ti_seq to correspond to first data byte. 484 * If data, trim to stay within window, 485 * dropping FIN if necessary. 486 */ 487 ti->ti_seq++; 488 if (ti->ti_len > tp->rcv_wnd) { 489 todrop = ti->ti_len - tp->rcv_wnd; 490 m_adj(m, -todrop); 491 ti->ti_len = tp->rcv_wnd; 492 tiflags &= ~TH_FIN; 493 tcpstat.tcps_rcvpackafterwin++; 494 tcpstat.tcps_rcvbyteafterwin += todrop; 495 } 496 tp->snd_wl1 = ti->ti_seq - 1; 497 tp->rcv_up = ti->ti_seq; 498 goto step6; 499 } 500 501 /* 502 * States other than LISTEN or SYN_SENT. 503 * First check that at least some bytes of segment are within 504 * receive window. If segment begins before rcv_nxt, 505 * drop leading data (and SYN); if nothing left, just ack. 506 */ 507 todrop = tp->rcv_nxt - ti->ti_seq; 508 if (todrop > 0) { 509 if (tiflags & TH_SYN) { 510 tiflags &= ~TH_SYN; 511 ti->ti_seq++; 512 if (ti->ti_urp > 1) 513 ti->ti_urp--; 514 else 515 tiflags &= ~TH_URG; 516 todrop--; 517 } 518 if (todrop > ti->ti_len || 519 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 520 tcpstat.tcps_rcvduppack++; 521 tcpstat.tcps_rcvdupbyte += ti->ti_len; 522 /* 523 * If segment is just one to the left of the window, 524 * check two special cases: 525 * 1. Don't toss RST in response to 4.2-style keepalive. 526 * 2. If the only thing to drop is a FIN, we can drop 527 * it, but check the ACK or we will get into FIN 528 * wars if our FINs crossed (both CLOSING). 529 * In either case, send ACK to resynchronize, 530 * but keep on processing for RST or ACK. 531 */ 532 if ((tiflags & TH_FIN && todrop == ti->ti_len + 1) 533 #ifdef TCP_COMPAT_42 534 || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1) 535 #endif 536 ) { 537 todrop = ti->ti_len; 538 tiflags &= ~TH_FIN; 539 tp->t_flags |= TF_ACKNOW; 540 } else 541 goto dropafterack; 542 } else { 543 tcpstat.tcps_rcvpartduppack++; 544 tcpstat.tcps_rcvpartdupbyte += todrop; 545 } 546 m_adj(m, todrop); 547 ti->ti_seq += todrop; 548 ti->ti_len -= todrop; 549 if (ti->ti_urp > todrop) 550 ti->ti_urp -= todrop; 551 else { 552 tiflags &= ~TH_URG; 553 ti->ti_urp = 0; 554 } 555 } 556 557 /* 558 * If new data are received on a connection after the 559 * user processes are gone, then RST the other end. 560 */ 561 if ((so->so_state & SS_NOFDREF) && 562 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 563 tp = tcp_close(tp); 564 tcpstat.tcps_rcvafterclose++; 565 goto dropwithreset; 566 } 567 568 /* 569 * If segment ends after window, drop trailing data 570 * (and PUSH and FIN); if nothing left, just ACK. 571 */ 572 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 573 if (todrop > 0) { 574 tcpstat.tcps_rcvpackafterwin++; 575 if (todrop >= ti->ti_len) { 576 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 577 /* 578 * If a new connection request is received 579 * while in TIME_WAIT, drop the old connection 580 * and start over if the sequence numbers 581 * are above the previous ones. 582 */ 583 if (tiflags & TH_SYN && 584 tp->t_state == TCPS_TIME_WAIT && 585 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 586 iss = tp->rcv_nxt + TCP_ISSINCR; 587 (void) tcp_close(tp); 588 goto findpcb; 589 } 590 /* 591 * If window is closed can only take segments at 592 * window edge, and have to drop data and PUSH from 593 * incoming segments. Continue processing, but 594 * remember to ack. Otherwise, drop segment 595 * and ack. 596 */ 597 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 598 tp->t_flags |= TF_ACKNOW; 599 tcpstat.tcps_rcvwinprobe++; 600 } else 601 goto dropafterack; 602 } else 603 tcpstat.tcps_rcvbyteafterwin += todrop; 604 m_adj(m, -todrop); 605 ti->ti_len -= todrop; 606 tiflags &= ~(TH_PUSH|TH_FIN); 607 } 608 609 /* 610 * If the RST bit is set examine the state: 611 * SYN_RECEIVED STATE: 612 * If passive open, return to LISTEN state. 613 * If active open, inform user that connection was refused. 614 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 615 * Inform user that connection was reset, and close tcb. 616 * CLOSING, LAST_ACK, TIME_WAIT STATES 617 * Close the tcb. 618 */ 619 if (tiflags&TH_RST) switch (tp->t_state) { 620 621 case TCPS_SYN_RECEIVED: 622 so->so_error = ECONNREFUSED; 623 goto close; 624 625 case TCPS_ESTABLISHED: 626 case TCPS_FIN_WAIT_1: 627 case TCPS_FIN_WAIT_2: 628 case TCPS_CLOSE_WAIT: 629 so->so_error = ECONNRESET; 630 close: 631 tp->t_state = TCPS_CLOSED; 632 tcpstat.tcps_drops++; 633 tp = tcp_close(tp); 634 goto drop; 635 636 case TCPS_CLOSING: 637 case TCPS_LAST_ACK: 638 case TCPS_TIME_WAIT: 639 tp = tcp_close(tp); 640 goto drop; 641 } 642 643 /* 644 * If a SYN is in the window, then this is an 645 * error and we send an RST and drop the connection. 646 */ 647 if (tiflags & TH_SYN) { 648 tp = tcp_drop(tp, ECONNRESET); 649 goto dropwithreset; 650 } 651 652 /* 653 * If the ACK bit is off we drop the segment and return. 654 */ 655 if ((tiflags & TH_ACK) == 0) 656 goto drop; 657 658 /* 659 * Ack processing. 660 */ 661 switch (tp->t_state) { 662 663 /* 664 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 665 * ESTABLISHED state and continue processing, otherwise 666 * send an RST. 667 */ 668 case TCPS_SYN_RECEIVED: 669 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 670 SEQ_GT(ti->ti_ack, tp->snd_max)) 671 goto dropwithreset; 672 tcpstat.tcps_connects++; 673 soisconnected(so); 674 tp->t_state = TCPS_ESTABLISHED; 675 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 676 (void) tcp_reass(tp, (struct tcpiphdr *)0); 677 tp->snd_wl1 = ti->ti_seq - 1; 678 /* fall into ... */ 679 680 /* 681 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 682 * ACKs. If the ack is in the range 683 * tp->snd_una < ti->ti_ack <= tp->snd_max 684 * then advance tp->snd_una to ti->ti_ack and drop 685 * data from the retransmission queue. If this ACK reflects 686 * more up to date window information we update our window information. 687 */ 688 case TCPS_ESTABLISHED: 689 case TCPS_FIN_WAIT_1: 690 case TCPS_FIN_WAIT_2: 691 case TCPS_CLOSE_WAIT: 692 case TCPS_CLOSING: 693 case TCPS_LAST_ACK: 694 case TCPS_TIME_WAIT: 695 696 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 697 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 698 tcpstat.tcps_rcvdupack++; 699 /* 700 * If we have outstanding data (not a 701 * window probe), this is a completely 702 * duplicate ack (ie, window info didn't 703 * change), the ack is the biggest we've 704 * seen and we've seen exactly our rexmt 705 * threshhold of them, assume a packet 706 * has been dropped and retransmit it. 707 * Kludge snd_nxt & the congestion 708 * window so we send only this one 709 * packet. If this packet fills the 710 * only hole in the receiver's seq. 711 * space, the next real ack will fully 712 * open our window. This means we 713 * have to do the usual slow-start to 714 * not overwhelm an intermediate gateway 715 * with a burst of packets. Leave 716 * here with the congestion window set 717 * to allow 2 packets on the next real 718 * ack and the exp-to-linear thresh 719 * set for half the current window 720 * size (since we know we're losing at 721 * the current window size). 722 */ 723 if (tp->t_timer[TCPT_REXMT] == 0 || 724 ti->ti_ack != tp->snd_una) 725 tp->t_dupacks = 0; 726 else if (++tp->t_dupacks == tcprexmtthresh) { 727 tcp_seq onxt = tp->snd_nxt; 728 u_int win = 729 MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / 730 tp->t_maxseg; 731 732 if (win < 2) 733 win = 2; 734 tp->snd_ssthresh = win * tp->t_maxseg; 735 736 tp->t_timer[TCPT_REXMT] = 0; 737 tp->t_rtt = 0; 738 tp->snd_nxt = ti->ti_ack; 739 tp->snd_cwnd = tp->t_maxseg; 740 (void) tcp_output(tp); 741 742 if (SEQ_GT(onxt, tp->snd_nxt)) 743 tp->snd_nxt = onxt; 744 goto drop; 745 } 746 } else 747 tp->t_dupacks = 0; 748 break; 749 } 750 tp->t_dupacks = 0; 751 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 752 tcpstat.tcps_rcvacktoomuch++; 753 goto dropafterack; 754 } 755 acked = ti->ti_ack - tp->snd_una; 756 tcpstat.tcps_rcvackpack++; 757 tcpstat.tcps_rcvackbyte += acked; 758 759 /* 760 * If transmit timer is running and timed sequence 761 * number was acked, update smoothed round trip time. 762 * Since we now have an rtt measurement, cancel the 763 * timer backoff (cf., Phil Karn's retransmit alg.). 764 * Recompute the initial retransmit timer. 765 */ 766 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 767 tcpstat.tcps_rttupdated++; 768 if (tp->t_srtt != 0) { 769 register short delta; 770 771 /* 772 * srtt is stored as fixed point with 3 bits 773 * after the binary point (i.e., scaled by 8). 774 * The following magic is equivalent 775 * to the smoothing algorithm in rfc793 776 * with an alpha of .875 777 * (srtt = rtt/8 + srtt*7/8 in fixed point). 778 * Adjust t_rtt to origin 0. 779 */ 780 delta = tp->t_rtt - 1 - (tp->t_srtt >> 3); 781 if ((tp->t_srtt += delta) <= 0) 782 tp->t_srtt = 1; 783 /* 784 * We accumulate a smoothed rtt variance 785 * (actually, a smoothed mean difference), 786 * then set the retransmit timer to smoothed 787 * rtt + 2 times the smoothed variance. 788 * rttvar is stored as fixed point 789 * with 2 bits after the binary point 790 * (scaled by 4). The following is equivalent 791 * to rfc793 smoothing with an alpha of .75 792 * (rttvar = rttvar*3/4 + |delta| / 4). 793 * This replaces rfc793's wired-in beta. 794 */ 795 if (delta < 0) 796 delta = -delta; 797 delta -= (tp->t_rttvar >> 2); 798 if ((tp->t_rttvar += delta) <= 0) 799 tp->t_rttvar = 1; 800 } else { 801 /* 802 * No rtt measurement yet - use the 803 * unsmoothed rtt. Set the variance 804 * to half the rtt (so our first 805 * retransmit happens at 2*rtt) 806 */ 807 tp->t_srtt = tp->t_rtt << 3; 808 tp->t_rttvar = tp->t_rtt << 1; 809 } 810 tp->t_rtt = 0; 811 tp->t_rxtshift = 0; 812 TCPT_RANGESET(tp->t_rxtcur, 813 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 814 TCPTV_MIN, TCPTV_REXMTMAX); 815 } 816 817 /* 818 * If all outstanding data is acked, stop retransmit 819 * timer and remember to restart (more output or persist). 820 * If there is more data to be acked, restart retransmit 821 * timer, using current (possibly backed-off) value. 822 */ 823 if (ti->ti_ack == tp->snd_max) { 824 tp->t_timer[TCPT_REXMT] = 0; 825 needoutput = 1; 826 } else if (tp->t_timer[TCPT_PERSIST] == 0) 827 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 828 /* 829 * When new data is acked, open the congestion window. 830 * If the window gives us less than ssthresh packets 831 * in flight, open exponentially (maxseg per packet). 832 * Otherwise open linearly (maxseg per window, 833 * or maxseg^2 / cwnd per packet). 834 */ 835 { 836 u_int incr = tp->t_maxseg; 837 838 if (tp->snd_cwnd > tp->snd_ssthresh) 839 incr = MAX(incr * incr / tp->snd_cwnd, 1); 840 841 tp->snd_cwnd = MIN(tp->snd_cwnd + incr, IP_MAXPACKET); /* XXX */ 842 } 843 if (acked > so->so_snd.sb_cc) { 844 tp->snd_wnd -= so->so_snd.sb_cc; 845 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 846 ourfinisacked = 1; 847 } else { 848 sbdrop(&so->so_snd, acked); 849 tp->snd_wnd -= acked; 850 ourfinisacked = 0; 851 } 852 if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 853 sowwakeup(so); 854 tp->snd_una = ti->ti_ack; 855 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 856 tp->snd_nxt = tp->snd_una; 857 858 switch (tp->t_state) { 859 860 /* 861 * In FIN_WAIT_1 STATE in addition to the processing 862 * for the ESTABLISHED state if our FIN is now acknowledged 863 * then enter FIN_WAIT_2. 864 */ 865 case TCPS_FIN_WAIT_1: 866 if (ourfinisacked) { 867 /* 868 * If we can't receive any more 869 * data, then closing user can proceed. 870 * Starting the timer is contrary to the 871 * specification, but if we don't get a FIN 872 * we'll hang forever. 873 */ 874 if (so->so_state & SS_CANTRCVMORE) { 875 soisdisconnected(so); 876 tp->t_timer[TCPT_2MSL] = tcp_maxidle; 877 } 878 tp->t_state = TCPS_FIN_WAIT_2; 879 } 880 break; 881 882 /* 883 * In CLOSING STATE in addition to the processing for 884 * the ESTABLISHED state if the ACK acknowledges our FIN 885 * then enter the TIME-WAIT state, otherwise ignore 886 * the segment. 887 */ 888 case TCPS_CLOSING: 889 if (ourfinisacked) { 890 tp->t_state = TCPS_TIME_WAIT; 891 tcp_canceltimers(tp); 892 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 893 soisdisconnected(so); 894 } 895 break; 896 897 /* 898 * In LAST_ACK, we may still be waiting for data to drain 899 * and/or to be acked, as well as for the ack of our FIN. 900 * If our FIN is now acknowledged, delete the TCB, 901 * enter the closed state and return. 902 */ 903 case TCPS_LAST_ACK: 904 if (ourfinisacked) { 905 tp = tcp_close(tp); 906 goto drop; 907 } 908 break; 909 910 /* 911 * In TIME_WAIT state the only thing that should arrive 912 * is a retransmission of the remote FIN. Acknowledge 913 * it and restart the finack timer. 914 */ 915 case TCPS_TIME_WAIT: 916 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 917 goto dropafterack; 918 } 919 } 920 921 step6: 922 /* 923 * Update window information. 924 * Don't look at window if no ACK: TAC's send garbage on first SYN. 925 */ 926 if ((tiflags & TH_ACK) && 927 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 928 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 929 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 930 /* keep track of pure window updates */ 931 if (ti->ti_len == 0 && 932 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 933 tcpstat.tcps_rcvwinupd++; 934 tp->snd_wnd = ti->ti_win; 935 tp->snd_wl1 = ti->ti_seq; 936 tp->snd_wl2 = ti->ti_ack; 937 if (tp->snd_wnd > tp->max_sndwnd) 938 tp->max_sndwnd = tp->snd_wnd; 939 needoutput = 1; 940 } 941 942 /* 943 * Process segments with URG. 944 */ 945 if ((tiflags & TH_URG) && ti->ti_urp && 946 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 947 /* 948 * This is a kludge, but if we receive and accept 949 * random urgent pointers, we'll crash in 950 * soreceive. It's hard to imagine someone 951 * actually wanting to send this much urgent data. 952 */ 953 if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 954 ti->ti_urp = 0; /* XXX */ 955 tiflags &= ~TH_URG; /* XXX */ 956 goto dodata; /* XXX */ 957 } 958 /* 959 * If this segment advances the known urgent pointer, 960 * then mark the data stream. This should not happen 961 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 962 * a FIN has been received from the remote side. 963 * In these states we ignore the URG. 964 * 965 * According to RFC961 (Assigned Protocols), 966 * the urgent pointer points to the last octet 967 * of urgent data. We continue, however, 968 * to consider it to indicate the first octet 969 * of data past the urgent section 970 * as the original spec states. 971 */ 972 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 973 tp->rcv_up = ti->ti_seq + ti->ti_urp; 974 so->so_oobmark = so->so_rcv.sb_cc + 975 (tp->rcv_up - tp->rcv_nxt) - 1; 976 if (so->so_oobmark == 0) 977 so->so_state |= SS_RCVATMARK; 978 sohasoutofband(so); 979 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 980 } 981 /* 982 * Remove out of band data so doesn't get presented to user. 983 * This can happen independent of advancing the URG pointer, 984 * but if two URG's are pending at once, some out-of-band 985 * data may creep in... ick. 986 */ 987 if (ti->ti_urp <= ti->ti_len && 988 (so->so_options & SO_OOBINLINE) == 0) 989 tcp_pulloutofband(so, ti); 990 } else 991 /* 992 * If no out of band data is expected, 993 * pull receive urgent pointer along 994 * with the receive window. 995 */ 996 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 997 tp->rcv_up = tp->rcv_nxt; 998 dodata: /* XXX */ 999 1000 /* 1001 * Process the segment text, merging it into the TCP sequencing queue, 1002 * and arranging for acknowledgment of receipt if necessary. 1003 * This process logically involves adjusting tp->rcv_wnd as data 1004 * is presented to the user (this happens in tcp_usrreq.c, 1005 * case PRU_RCVD). If a FIN has already been received on this 1006 * connection then we just ignore the text. 1007 */ 1008 if ((ti->ti_len || (tiflags&TH_FIN)) && 1009 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1010 TCP_REASS(tp, ti, m, so, tiflags); 1011 if (tcpnodelack == 0) 1012 tp->t_flags |= TF_DELACK; 1013 else 1014 tp->t_flags |= TF_ACKNOW; 1015 /* 1016 * Note the amount of data that peer has sent into 1017 * our window, in order to estimate the sender's 1018 * buffer size. 1019 */ 1020 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1021 if (len > tp->max_rcvd) 1022 tp->max_rcvd = len; 1023 } else { 1024 m_freem(m); 1025 tiflags &= ~TH_FIN; 1026 } 1027 1028 /* 1029 * If FIN is received ACK the FIN and let the user know 1030 * that the connection is closing. 1031 */ 1032 if (tiflags & TH_FIN) { 1033 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1034 socantrcvmore(so); 1035 tp->t_flags |= TF_ACKNOW; 1036 tp->rcv_nxt++; 1037 } 1038 switch (tp->t_state) { 1039 1040 /* 1041 * In SYN_RECEIVED and ESTABLISHED STATES 1042 * enter the CLOSE_WAIT state. 1043 */ 1044 case TCPS_SYN_RECEIVED: 1045 case TCPS_ESTABLISHED: 1046 tp->t_state = TCPS_CLOSE_WAIT; 1047 break; 1048 1049 /* 1050 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1051 * enter the CLOSING state. 1052 */ 1053 case TCPS_FIN_WAIT_1: 1054 tp->t_state = TCPS_CLOSING; 1055 break; 1056 1057 /* 1058 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1059 * starting the time-wait timer, turning off the other 1060 * standard timers. 1061 */ 1062 case TCPS_FIN_WAIT_2: 1063 tp->t_state = TCPS_TIME_WAIT; 1064 tcp_canceltimers(tp); 1065 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1066 soisdisconnected(so); 1067 break; 1068 1069 /* 1070 * In TIME_WAIT state restart the 2 MSL time_wait timer. 1071 */ 1072 case TCPS_TIME_WAIT: 1073 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1074 break; 1075 } 1076 } 1077 if (so->so_options & SO_DEBUG) 1078 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1079 1080 /* 1081 * Return any desired output. 1082 */ 1083 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1084 (void) tcp_output(tp); 1085 return; 1086 1087 dropafterack: 1088 /* 1089 * Generate an ACK dropping incoming segment if it occupies 1090 * sequence space, where the ACK reflects our state. 1091 */ 1092 if (tiflags & TH_RST) 1093 goto drop; 1094 m_freem(m); 1095 tp->t_flags |= TF_ACKNOW; 1096 (void) tcp_output(tp); 1097 return; 1098 1099 dropwithreset: 1100 if (om) { 1101 (void) m_free(om); 1102 om = 0; 1103 } 1104 /* 1105 * Generate a RST, dropping incoming segment. 1106 * Make ACK acceptable to originator of segment. 1107 * Don't bother to respond if destination was broadcast. 1108 */ 1109 if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 1110 goto drop; 1111 if (tiflags & TH_ACK) 1112 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 1113 else { 1114 if (tiflags & TH_SYN) 1115 ti->ti_len++; 1116 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1117 TH_RST|TH_ACK); 1118 } 1119 /* destroy temporarily created socket */ 1120 if (dropsocket) 1121 (void) soabort(so); 1122 return; 1123 1124 drop: 1125 if (om) 1126 (void) m_free(om); 1127 /* 1128 * Drop space held by incoming segment and return. 1129 */ 1130 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1131 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1132 m_freem(m); 1133 /* destroy temporarily created socket */ 1134 if (dropsocket) 1135 (void) soabort(so); 1136 return; 1137 } 1138 1139 tcp_dooptions(tp, om, ti) 1140 struct tcpcb *tp; 1141 struct mbuf *om; 1142 struct tcpiphdr *ti; 1143 { 1144 register u_char *cp; 1145 int opt, optlen, cnt; 1146 1147 cp = mtod(om, u_char *); 1148 cnt = om->m_len; 1149 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1150 opt = cp[0]; 1151 if (opt == TCPOPT_EOL) 1152 break; 1153 if (opt == TCPOPT_NOP) 1154 optlen = 1; 1155 else { 1156 optlen = cp[1]; 1157 if (optlen <= 0) 1158 break; 1159 } 1160 switch (opt) { 1161 1162 default: 1163 break; 1164 1165 case TCPOPT_MAXSEG: 1166 if (optlen != 4) 1167 continue; 1168 if (!(ti->ti_flags & TH_SYN)) 1169 continue; 1170 tp->t_maxseg = *(u_short *)(cp + 2); 1171 tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 1172 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 1173 break; 1174 } 1175 } 1176 (void) m_free(om); 1177 } 1178 1179 /* 1180 * Pull out of band byte out of a segment so 1181 * it doesn't appear in the user's data queue. 1182 * It is still reflected in the segment length for 1183 * sequencing purposes. 1184 */ 1185 tcp_pulloutofband(so, ti) 1186 struct socket *so; 1187 struct tcpiphdr *ti; 1188 { 1189 register struct mbuf *m; 1190 int cnt = ti->ti_urp - 1; 1191 1192 m = dtom(ti); 1193 while (cnt >= 0) { 1194 if (m->m_len > cnt) { 1195 char *cp = mtod(m, caddr_t) + cnt; 1196 struct tcpcb *tp = sototcpcb(so); 1197 1198 tp->t_iobc = *cp; 1199 tp->t_oobflags |= TCPOOB_HAVEDATA; 1200 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1201 m->m_len--; 1202 return; 1203 } 1204 cnt -= m->m_len; 1205 m = m->m_next; 1206 if (m == 0) 1207 break; 1208 } 1209 panic("tcp_pulloutofband"); 1210 } 1211 1212 /* 1213 * Determine a reasonable value for maxseg size. 1214 * If the route is known, use one that can be handled 1215 * on the given interface without forcing IP to fragment. 1216 * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 1217 * to utilize large mbufs. 1218 * If interface pointer is unavailable, or the destination isn't local, 1219 * use a conservative size (512 or the default IP max size, but no more 1220 * than the mtu of the interface through which we route), 1221 * as we can't discover anything about intervening gateways or networks. 1222 * We also initialize the congestion/slow start window to be a single 1223 * segment if the destination isn't local; this information should 1224 * probably all be saved with the routing entry at the transport level. 1225 * 1226 * This is ugly, and doesn't belong at this level, but has to happen somehow. 1227 */ 1228 tcp_mss(tp) 1229 register struct tcpcb *tp; 1230 { 1231 struct route *ro; 1232 struct ifnet *ifp; 1233 int mss; 1234 struct inpcb *inp; 1235 1236 inp = tp->t_inpcb; 1237 ro = &inp->inp_route; 1238 if ((ro->ro_rt == (struct rtentry *)0) || 1239 (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 1240 /* No route yet, so try to acquire one */ 1241 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1242 ro->ro_dst.sa_family = AF_INET; 1243 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 1244 inp->inp_faddr; 1245 rtalloc(ro); 1246 } 1247 if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 1248 return (TCP_MSS); 1249 } 1250 1251 mss = ifp->if_mtu - sizeof(struct tcpiphdr); 1252 #if (MCLBYTES & (MCLBYTES - 1)) == 0 1253 if (mss > MCLBYTES) 1254 mss &= ~(MCLBYTES-1); 1255 #else 1256 if (mss > MCLBYTES) 1257 mss = mss / MCLBYTES * MCLBYTES; 1258 #endif 1259 if (in_localaddr(inp->inp_faddr)) 1260 return (mss); 1261 1262 mss = MIN(mss, TCP_MSS); 1263 tp->snd_cwnd = mss; 1264 return (mss); 1265 } 1266