1 /* 2 * Copyright (c) 1982, 1986 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)tcp_input.c 7.12 (Berkeley) 10/09/87 7 */ 8 9 #include "param.h" 10 #include "systm.h" 11 #include "mbuf.h" 12 #include "protosw.h" 13 #include "socket.h" 14 #include "socketvar.h" 15 #include "errno.h" 16 17 #include "../net/if.h" 18 #include "../net/route.h" 19 20 #include "in.h" 21 #include "in_pcb.h" 22 #include "in_systm.h" 23 #include "ip.h" 24 #include "ip_var.h" 25 #include "tcp.h" 26 #include "tcp_fsm.h" 27 #include "tcp_seq.h" 28 #include "tcp_timer.h" 29 #include "tcp_var.h" 30 #include "tcpip.h" 31 #include "tcp_debug.h" 32 33 int tcpprintfs = 0; 34 int tcpcksum = 1; 35 int tcprexmtthresh = 3; 36 struct tcpiphdr tcp_saveti; 37 extern tcpnodelack; 38 39 struct tcpcb *tcp_newtcpcb(); 40 41 /* 42 * Insert segment ti into reassembly queue of tcp with 43 * control block tp. Return TH_FIN if reassembly now includes 44 * a segment with FIN. The macro form does the common case inline 45 * (segment is the next to be received on an established connection, 46 * and the queue is empty), avoiding linkage into and removal 47 * from the queue and repetition of various conversions. 48 */ 49 #define TCP_REASS(tp, ti, m, so, flags) { \ 50 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 51 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 52 (tp)->t_state == TCPS_ESTABLISHED) { \ 53 (tp)->rcv_nxt += (ti)->ti_len; \ 54 flags = (ti)->ti_flags & TH_FIN; \ 55 tcpstat.tcps_rcvpack++;\ 56 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 57 sbappend(&(so)->so_rcv, (m)); \ 58 sorwakeup(so); \ 59 } else \ 60 (flags) = tcp_reass((tp), (ti)); \ 61 } 62 63 tcp_reass(tp, ti) 64 register struct tcpcb *tp; 65 register struct tcpiphdr *ti; 66 { 67 register struct tcpiphdr *q; 68 struct socket *so = tp->t_inpcb->inp_socket; 69 struct mbuf *m; 70 int flags; 71 72 /* 73 * Call with ti==0 after become established to 74 * force pre-ESTABLISHED data up to user socket. 75 */ 76 if (ti == 0) 77 goto present; 78 79 /* 80 * Find a segment which begins after this one does. 81 */ 82 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 83 q = (struct tcpiphdr *)q->ti_next) 84 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 85 break; 86 87 /* 88 * If there is a preceding segment, it may provide some of 89 * our data already. If so, drop the data from the incoming 90 * segment. If it provides all of our data, drop us. 91 */ 92 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 93 register int i; 94 q = (struct tcpiphdr *)q->ti_prev; 95 /* conversion to int (in i) handles seq wraparound */ 96 i = q->ti_seq + q->ti_len - ti->ti_seq; 97 if (i > 0) { 98 if (i >= ti->ti_len) { 99 tcpstat.tcps_rcvduppack++; 100 tcpstat.tcps_rcvdupbyte += ti->ti_len; 101 goto drop; 102 } 103 m_adj(dtom(ti), i); 104 ti->ti_len -= i; 105 ti->ti_seq += i; 106 } 107 q = (struct tcpiphdr *)(q->ti_next); 108 } 109 tcpstat.tcps_rcvoopack++; 110 tcpstat.tcps_rcvoobyte += ti->ti_len; 111 112 /* 113 * While we overlap succeeding segments trim them or, 114 * if they are completely covered, dequeue them. 115 */ 116 while (q != (struct tcpiphdr *)tp) { 117 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 118 if (i <= 0) 119 break; 120 if (i < q->ti_len) { 121 q->ti_seq += i; 122 q->ti_len -= i; 123 m_adj(dtom(q), i); 124 break; 125 } 126 q = (struct tcpiphdr *)q->ti_next; 127 m = dtom(q->ti_prev); 128 remque(q->ti_prev); 129 m_freem(m); 130 } 131 132 /* 133 * Stick new segment in its place. 134 */ 135 insque(ti, q->ti_prev); 136 137 present: 138 /* 139 * Present data to user, advancing rcv_nxt through 140 * completed sequence space. 141 */ 142 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 143 return (0); 144 ti = tp->seg_next; 145 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 146 return (0); 147 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 148 return (0); 149 do { 150 tp->rcv_nxt += ti->ti_len; 151 flags = ti->ti_flags & TH_FIN; 152 remque(ti); 153 m = dtom(ti); 154 ti = (struct tcpiphdr *)ti->ti_next; 155 if (so->so_state & SS_CANTRCVMORE) 156 m_freem(m); 157 else 158 sbappend(&so->so_rcv, m); 159 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 160 sorwakeup(so); 161 return (flags); 162 drop: 163 m_freem(dtom(ti)); 164 return (0); 165 } 166 167 /* 168 * TCP input routine, follows pages 65-76 of the 169 * protocol specification dated September, 1981 very closely. 170 */ 171 tcp_input(m0) 172 struct mbuf *m0; 173 { 174 register struct tcpiphdr *ti; 175 struct inpcb *inp; 176 register struct mbuf *m; 177 struct mbuf *om = 0; 178 int len, tlen, off; 179 register struct tcpcb *tp = 0; 180 register int tiflags; 181 struct socket *so; 182 int todrop, acked, ourfinisacked, needoutput = 0; 183 short ostate; 184 struct in_addr laddr; 185 int dropsocket = 0; 186 int iss = 0; 187 188 tcpstat.tcps_rcvtotal++; 189 /* 190 * Get IP and TCP header together in first mbuf. 191 * Note: IP leaves IP header in first mbuf. 192 */ 193 m = m0; 194 ti = mtod(m, struct tcpiphdr *); 195 if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 196 ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 197 if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 198 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 199 tcpstat.tcps_rcvshort++; 200 return; 201 } 202 ti = mtod(m, struct tcpiphdr *); 203 } 204 205 /* 206 * Checksum extended TCP header and data. 207 */ 208 tlen = ((struct ip *)ti)->ip_len; 209 len = sizeof (struct ip) + tlen; 210 if (tcpcksum) { 211 ti->ti_next = ti->ti_prev = 0; 212 ti->ti_x1 = 0; 213 ti->ti_len = (u_short)tlen; 214 ti->ti_len = htons((u_short)ti->ti_len); 215 if (ti->ti_sum = in_cksum(m, len)) { 216 if (tcpprintfs) 217 printf("tcp sum: src %x\n", ti->ti_src); 218 tcpstat.tcps_rcvbadsum++; 219 goto drop; 220 } 221 } 222 223 /* 224 * Check that TCP offset makes sense, 225 * pull out TCP options and adjust length. 226 */ 227 off = ti->ti_off << 2; 228 if (off < sizeof (struct tcphdr) || off > tlen) { 229 if (tcpprintfs) 230 printf("tcp off: src %x off %d\n", ti->ti_src, off); 231 tcpstat.tcps_rcvbadoff++; 232 goto drop; 233 } 234 tlen -= off; 235 ti->ti_len = tlen; 236 if (off > sizeof (struct tcphdr)) { 237 if (m->m_len < sizeof(struct ip) + off) { 238 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 239 tcpstat.tcps_rcvshort++; 240 return; 241 } 242 ti = mtod(m, struct tcpiphdr *); 243 } 244 om = m_get(M_DONTWAIT, MT_DATA); 245 if (om == 0) 246 goto drop; 247 om->m_len = off - sizeof (struct tcphdr); 248 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 249 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 250 m->m_len -= om->m_len; 251 bcopy(op+om->m_len, op, 252 (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 253 } 254 } 255 tiflags = ti->ti_flags; 256 257 /* 258 * Drop TCP and IP headers; TCP options were dropped above. 259 */ 260 m->m_off += sizeof(struct tcpiphdr); 261 m->m_len -= sizeof(struct tcpiphdr); 262 263 /* 264 * Convert TCP protocol specific fields to host format. 265 */ 266 ti->ti_seq = ntohl(ti->ti_seq); 267 ti->ti_ack = ntohl(ti->ti_ack); 268 ti->ti_win = ntohs(ti->ti_win); 269 ti->ti_urp = ntohs(ti->ti_urp); 270 271 /* 272 * Locate pcb for segment. 273 */ 274 findpcb: 275 inp = in_pcblookup 276 (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 277 INPLOOKUP_WILDCARD); 278 279 /* 280 * If the state is CLOSED (i.e., TCB does not exist) then 281 * all data in the incoming segment is discarded. 282 * If the TCB exists but is in CLOSED state, it is embryonic, 283 * but should either do a listen or a connect soon. 284 */ 285 if (inp == 0) 286 goto dropwithreset; 287 tp = intotcpcb(inp); 288 if (tp == 0) 289 goto dropwithreset; 290 if (tp->t_state == TCPS_CLOSED) 291 goto drop; 292 so = inp->inp_socket; 293 if (so->so_options & SO_DEBUG) { 294 ostate = tp->t_state; 295 tcp_saveti = *ti; 296 } 297 if (so->so_options & SO_ACCEPTCONN) { 298 so = sonewconn(so); 299 if (so == 0) 300 goto drop; 301 /* 302 * This is ugly, but .... 303 * 304 * Mark socket as temporary until we're 305 * committed to keeping it. The code at 306 * ``drop'' and ``dropwithreset'' check the 307 * flag dropsocket to see if the temporary 308 * socket created here should be discarded. 309 * We mark the socket as discardable until 310 * we're committed to it below in TCPS_LISTEN. 311 */ 312 dropsocket++; 313 inp = (struct inpcb *)so->so_pcb; 314 inp->inp_laddr = ti->ti_dst; 315 inp->inp_lport = ti->ti_dport; 316 inp->inp_options = ip_srcroute(); 317 tp = intotcpcb(inp); 318 tp->t_state = TCPS_LISTEN; 319 } 320 321 /* 322 * Segment received on connection. 323 * Reset idle time and keep-alive timer. 324 */ 325 tp->t_idle = 0; 326 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 327 328 /* 329 * Process options if not in LISTEN state, 330 * else do it below (after getting remote address). 331 */ 332 if (om && tp->t_state != TCPS_LISTEN) { 333 tcp_dooptions(tp, om, ti); 334 om = 0; 335 } 336 337 /* 338 * Calculate amount of space in receive window, 339 * and then do TCP input processing. 340 * Receive window is amount of space in rcv queue, 341 * but not less than advertised window. 342 */ 343 { int win; 344 345 win = sbspace(&so->so_rcv); 346 if (win < 0) 347 win = 0; 348 tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 349 } 350 351 switch (tp->t_state) { 352 353 /* 354 * If the state is LISTEN then ignore segment if it contains an RST. 355 * If the segment contains an ACK then it is bad and send a RST. 356 * If it does not contain a SYN then it is not interesting; drop it. 357 * Don't bother responding if the destination was a broadcast. 358 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 359 * tp->iss, and send a segment: 360 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 361 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 362 * Fill in remote peer address fields if not previously specified. 363 * Enter SYN_RECEIVED state, and process any other fields of this 364 * segment in this state. 365 */ 366 case TCPS_LISTEN: { 367 struct mbuf *am; 368 register struct sockaddr_in *sin; 369 370 if (tiflags & TH_RST) 371 goto drop; 372 if (tiflags & TH_ACK) 373 goto dropwithreset; 374 if ((tiflags & TH_SYN) == 0) 375 goto drop; 376 if (in_broadcast(ti->ti_dst)) 377 goto drop; 378 am = m_get(M_DONTWAIT, MT_SONAME); 379 if (am == NULL) 380 goto drop; 381 am->m_len = sizeof (struct sockaddr_in); 382 sin = mtod(am, struct sockaddr_in *); 383 sin->sin_family = AF_INET; 384 sin->sin_addr = ti->ti_src; 385 sin->sin_port = ti->ti_sport; 386 laddr = inp->inp_laddr; 387 if (inp->inp_laddr.s_addr == INADDR_ANY) 388 inp->inp_laddr = ti->ti_dst; 389 if (in_pcbconnect(inp, am)) { 390 inp->inp_laddr = laddr; 391 (void) m_free(am); 392 goto drop; 393 } 394 (void) m_free(am); 395 tp->t_template = tcp_template(tp); 396 if (tp->t_template == 0) { 397 tp = tcp_drop(tp, ENOBUFS); 398 dropsocket = 0; /* socket is already gone */ 399 goto drop; 400 } 401 if (om) { 402 tcp_dooptions(tp, om, ti); 403 om = 0; 404 } 405 if (iss) 406 tp->iss = iss; 407 else 408 tp->iss = tcp_iss; 409 tcp_iss += TCP_ISSINCR/2; 410 tp->irs = ti->ti_seq; 411 tcp_sendseqinit(tp); 412 tcp_rcvseqinit(tp); 413 tp->t_flags |= TF_ACKNOW; 414 tp->t_state = TCPS_SYN_RECEIVED; 415 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 416 dropsocket = 0; /* committed to socket */ 417 tcpstat.tcps_accepts++; 418 goto trimthenstep6; 419 } 420 421 /* 422 * If the state is SYN_SENT: 423 * if seg contains an ACK, but not for our SYN, drop the input. 424 * if seg contains a RST, then drop the connection. 425 * if seg does not contain SYN, then drop it. 426 * Otherwise this is an acceptable SYN segment 427 * initialize tp->rcv_nxt and tp->irs 428 * if seg contains ack then advance tp->snd_una 429 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 430 * arrange for segment to be acked (eventually) 431 * continue processing rest of data/controls, beginning with URG 432 */ 433 case TCPS_SYN_SENT: 434 if ((tiflags & TH_ACK) && 435 (SEQ_LEQ(ti->ti_ack, tp->iss) || 436 SEQ_GT(ti->ti_ack, tp->snd_max))) 437 goto dropwithreset; 438 if (tiflags & TH_RST) { 439 if (tiflags & TH_ACK) 440 tp = tcp_drop(tp, ECONNREFUSED); 441 goto drop; 442 } 443 if ((tiflags & TH_SYN) == 0) 444 goto drop; 445 if (tiflags & TH_ACK) { 446 tp->snd_una = ti->ti_ack; 447 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 448 tp->snd_nxt = tp->snd_una; 449 } 450 tp->t_timer[TCPT_REXMT] = 0; 451 tp->irs = ti->ti_seq; 452 tcp_rcvseqinit(tp); 453 tp->t_flags |= TF_ACKNOW; 454 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 455 tcpstat.tcps_connects++; 456 soisconnected(so); 457 tp->t_state = TCPS_ESTABLISHED; 458 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 459 (void) tcp_reass(tp, (struct tcpiphdr *)0); 460 /* 461 * if we didn't have to retransmit the SYN, 462 * use its rtt as our initial srtt & rtt var. 463 */ 464 if (tp->t_rtt) { 465 tp->t_srtt = tp->t_rtt << 3; 466 tp->t_rttvar = tp->t_rtt << 1; 467 TCPT_RANGESET(tp->t_rxtcur, 468 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 469 TCPTV_MIN, TCPTV_REXMTMAX); 470 tp->t_rtt = 0; 471 } 472 } else 473 tp->t_state = TCPS_SYN_RECEIVED; 474 475 trimthenstep6: 476 /* 477 * Advance ti->ti_seq to correspond to first data byte. 478 * If data, trim to stay within window, 479 * dropping FIN if necessary. 480 */ 481 ti->ti_seq++; 482 if (ti->ti_len > tp->rcv_wnd) { 483 todrop = ti->ti_len - tp->rcv_wnd; 484 m_adj(m, -todrop); 485 ti->ti_len = tp->rcv_wnd; 486 tiflags &= ~TH_FIN; 487 tcpstat.tcps_rcvpackafterwin++; 488 tcpstat.tcps_rcvbyteafterwin += todrop; 489 } 490 tp->snd_wl1 = ti->ti_seq - 1; 491 tp->rcv_up = ti->ti_seq; 492 goto step6; 493 } 494 495 /* 496 * States other than LISTEN or SYN_SENT. 497 * First check that at least some bytes of segment are within 498 * receive window. If segment begins before rcv_nxt, 499 * drop leading data (and SYN); if nothing left, just ack. 500 */ 501 todrop = tp->rcv_nxt - ti->ti_seq; 502 if (todrop > 0) { 503 if (tiflags & TH_SYN) { 504 tiflags &= ~TH_SYN; 505 ti->ti_seq++; 506 if (ti->ti_urp > 1) 507 ti->ti_urp--; 508 else 509 tiflags &= ~TH_URG; 510 todrop--; 511 } 512 if (todrop > ti->ti_len || 513 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 514 #ifdef TCP_COMPAT_42 515 /* 516 * Don't toss RST in response to 4.2-style keepalive. 517 */ 518 if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST) 519 goto do_rst; 520 #endif 521 tcpstat.tcps_rcvduppack++; 522 tcpstat.tcps_rcvdupbyte += ti->ti_len; 523 todrop = ti->ti_len; 524 tiflags &= ~TH_FIN; 525 tp->t_flags |= TF_ACKNOW; 526 } else { 527 tcpstat.tcps_rcvpartduppack++; 528 tcpstat.tcps_rcvpartdupbyte += todrop; 529 } 530 m_adj(m, todrop); 531 ti->ti_seq += todrop; 532 ti->ti_len -= todrop; 533 if (ti->ti_urp > todrop) 534 ti->ti_urp -= todrop; 535 else { 536 tiflags &= ~TH_URG; 537 ti->ti_urp = 0; 538 } 539 } 540 541 if (tp->rcv_wnd == 0) { 542 /* 543 * If window is closed can only take segments at 544 * window edge, and have to drop data and PUSH from 545 * incoming segments. 546 * 547 * If new data is received on a connection after the 548 * user processes are gone, then RST the other end. 549 */ 550 if ((so->so_state & SS_NOFDREF) && 551 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 552 tp = tcp_close(tp); 553 tcpstat.tcps_rcvafterclose++; 554 goto dropwithreset; 555 } 556 if (tp->rcv_nxt != ti->ti_seq) { 557 tcpstat.tcps_rcvpackafterwin++; 558 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 559 goto dropafterack; 560 } 561 if (ti->ti_len > 0) { 562 if (ti->ti_len == 1) 563 tcpstat.tcps_rcvwinprobe++; 564 else { 565 tcpstat.tcps_rcvpackafterwin++; 566 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 567 } 568 m_adj(m, ti->ti_len); 569 ti->ti_len = 0; 570 tiflags &= ~(TH_PUSH|TH_FIN); 571 } 572 } else { 573 /* 574 * If segment ends after window, drop trailing data 575 * (and PUSH and FIN); if nothing left, just ACK. 576 */ 577 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 578 if (todrop > 0) { 579 if (todrop >= ti->ti_len) { 580 /* 581 * If a new connection request is received 582 * while in TIME_WAIT, drop the old connection 583 * and start over if the sequence numbers 584 * are above the previous ones. 585 */ 586 if (tiflags & TH_SYN && 587 tp->t_state == TCPS_TIME_WAIT && 588 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 589 iss = tp->rcv_nxt + TCP_ISSINCR; 590 (void) tcp_close(tp); 591 goto findpcb; 592 } 593 if (todrop == 1) 594 tcpstat.tcps_rcvwinprobe++; 595 else { 596 tcpstat.tcps_rcvpackafterwin++; 597 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 598 } 599 goto dropafterack; 600 } 601 tcpstat.tcps_rcvpackafterwin++; 602 tcpstat.tcps_rcvbyteafterwin += todrop; 603 m_adj(m, -todrop); 604 ti->ti_len -= todrop; 605 tiflags &= ~(TH_PUSH|TH_FIN); 606 } 607 } 608 609 #ifdef TCP_COMPAT_42 610 do_rst: 611 #endif 612 /* 613 * If the RST bit is set examine the state: 614 * SYN_RECEIVED STATE: 615 * If passive open, return to LISTEN state. 616 * If active open, inform user that connection was refused. 617 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 618 * Inform user that connection was reset, and close tcb. 619 * CLOSING, LAST_ACK, TIME_WAIT STATES 620 * Close the tcb. 621 */ 622 if (tiflags&TH_RST) switch (tp->t_state) { 623 624 case TCPS_SYN_RECEIVED: 625 tp = tcp_drop(tp, ECONNREFUSED); 626 goto drop; 627 628 case TCPS_ESTABLISHED: 629 case TCPS_FIN_WAIT_1: 630 case TCPS_FIN_WAIT_2: 631 case TCPS_CLOSE_WAIT: 632 tp = tcp_drop(tp, ECONNRESET); 633 goto drop; 634 635 case TCPS_CLOSING: 636 case TCPS_LAST_ACK: 637 case TCPS_TIME_WAIT: 638 tp = tcp_close(tp); 639 goto drop; 640 } 641 642 /* 643 * If a SYN is in the window, then this is an 644 * error and we send an RST and drop the connection. 645 */ 646 if (tiflags & TH_SYN) { 647 tp = tcp_drop(tp, ECONNRESET); 648 goto dropwithreset; 649 } 650 651 /* 652 * If the ACK bit is off we drop the segment and return. 653 */ 654 if ((tiflags & TH_ACK) == 0) 655 goto drop; 656 657 /* 658 * Ack processing. 659 */ 660 switch (tp->t_state) { 661 662 /* 663 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 664 * ESTABLISHED state and continue processing, otherwise 665 * send an RST. 666 */ 667 case TCPS_SYN_RECEIVED: 668 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 669 SEQ_GT(ti->ti_ack, tp->snd_max)) 670 goto dropwithreset; 671 tcpstat.tcps_connects++; 672 soisconnected(so); 673 tp->t_state = TCPS_ESTABLISHED; 674 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 675 (void) tcp_reass(tp, (struct tcpiphdr *)0); 676 tp->snd_wl1 = ti->ti_seq - 1; 677 /* fall into ... */ 678 679 /* 680 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 681 * ACKs. If the ack is in the range 682 * tp->snd_una < ti->ti_ack <= tp->snd_max 683 * then advance tp->snd_una to ti->ti_ack and drop 684 * data from the retransmission queue. If this ACK reflects 685 * more up to date window information we update our window information. 686 */ 687 case TCPS_ESTABLISHED: 688 case TCPS_FIN_WAIT_1: 689 case TCPS_FIN_WAIT_2: 690 case TCPS_CLOSE_WAIT: 691 case TCPS_CLOSING: 692 case TCPS_LAST_ACK: 693 case TCPS_TIME_WAIT: 694 695 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 696 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 697 tcpstat.tcps_rcvdupack++; 698 /* 699 * If we have outstanding data (not a 700 * window probe), this is a completely 701 * duplicate ack (ie, window info didn't 702 * change), the ack is the biggest we've 703 * seen and we've seen exactly our rexmt 704 * threshhold of them, assume a packet 705 * has been dropped and retransmit it. 706 * Kludge snd_nxt & the congestion 707 * window so we send only this one 708 * packet. If this packet fills the 709 * only hole in the receiver's seq. 710 * space, the next real ack will fully 711 * open our window. This means we 712 * have to do the usual slow-start to 713 * not overwhelm an intermediate gateway 714 * with a burst of packets. Leave 715 * here with the congestion window set 716 * to allow 2 packets on the next real 717 * ack and the exp-to-linear thresh 718 * set for half the current window 719 * size (since we know we're losing at 720 * the current window size). 721 */ 722 if (tp->t_timer[TCPT_REXMT] == 0 || 723 ti->ti_ack != tp->snd_una) 724 tp->t_dupacks = 0; 725 else if (++tp->t_dupacks == tcprexmtthresh) { 726 tcp_seq onxt = tp->snd_nxt; 727 u_int win = 728 MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / 729 tp->t_maxseg; 730 731 if (win < 2) 732 win = 2; 733 tp->snd_ssthresh = win * tp->t_maxseg; 734 735 tp->t_timer[TCPT_REXMT] = 0; 736 tp->t_rtt = 0; 737 tp->snd_nxt = ti->ti_ack; 738 tp->snd_cwnd = tp->t_maxseg; 739 (void) tcp_output(tp); 740 741 if (SEQ_GT(onxt, tp->snd_nxt)) 742 tp->snd_nxt = onxt; 743 goto drop; 744 } 745 } else 746 tp->t_dupacks = 0; 747 break; 748 } 749 tp->t_dupacks = 0; 750 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 751 tcpstat.tcps_rcvacktoomuch++; 752 goto dropafterack; 753 } 754 acked = ti->ti_ack - tp->snd_una; 755 tcpstat.tcps_rcvackpack++; 756 tcpstat.tcps_rcvackbyte += acked; 757 758 /* 759 * If transmit timer is running and timed sequence 760 * number was acked, update smoothed round trip time. 761 * Since we now have an rtt measurement, cancel the 762 * timer backoff (cf., Phil Karn's retransmit alg.). 763 * Recompute the initial retransmit timer. 764 */ 765 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 766 tcpstat.tcps_rttupdated++; 767 if (tp->t_srtt != 0) { 768 register short delta; 769 770 /* 771 * srtt is stored as fixed point with 3 bits 772 * after the binary point (i.e., scaled by 8). 773 * The following magic is equivalent 774 * to the smoothing algorithm in rfc793 775 * with an alpha of .875 776 * (srtt = rtt/8 + srtt*7/8 in fixed point). 777 * Adjust t_rtt to origin 0. 778 */ 779 tp->t_rtt--; 780 delta = tp->t_rtt - (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, 65535); /* 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] = TCPTV_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