1 /* 2 * Copyright (c) 1982, 1986 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.15.1.1 (Berkeley) 02/07/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 #if BSD>=43 323 inp->inp_options = ip_srcroute(); 324 #endif 325 tp = intotcpcb(inp); 326 tp->t_state = TCPS_LISTEN; 327 } 328 329 /* 330 * Segment received on connection. 331 * Reset idle time and keep-alive timer. 332 */ 333 tp->t_idle = 0; 334 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 335 336 /* 337 * Process options if not in LISTEN state, 338 * else do it below (after getting remote address). 339 */ 340 if (om && tp->t_state != TCPS_LISTEN) { 341 tcp_dooptions(tp, om, ti); 342 om = 0; 343 } 344 345 /* 346 * Calculate amount of space in receive window, 347 * and then do TCP input processing. 348 * Receive window is amount of space in rcv queue, 349 * but not less than advertised window. 350 */ 351 { int win; 352 353 win = sbspace(&so->so_rcv); 354 if (win < 0) 355 win = 0; 356 tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 357 } 358 359 switch (tp->t_state) { 360 361 /* 362 * If the state is LISTEN then ignore segment if it contains an RST. 363 * If the segment contains an ACK then it is bad and send a RST. 364 * If it does not contain a SYN then it is not interesting; drop it. 365 * Don't bother responding if the destination was a broadcast. 366 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 367 * tp->iss, and send a segment: 368 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 369 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 370 * Fill in remote peer address fields if not previously specified. 371 * Enter SYN_RECEIVED state, and process any other fields of this 372 * segment in this state. 373 */ 374 case TCPS_LISTEN: { 375 struct mbuf *am; 376 register struct sockaddr_in *sin; 377 378 if (tiflags & TH_RST) 379 goto drop; 380 if (tiflags & TH_ACK) 381 goto dropwithreset; 382 if ((tiflags & TH_SYN) == 0) 383 goto drop; 384 if (in_broadcast(ti->ti_dst)) 385 goto drop; 386 am = m_get(M_DONTWAIT, MT_SONAME); 387 if (am == NULL) 388 goto drop; 389 am->m_len = sizeof (struct sockaddr_in); 390 sin = mtod(am, struct sockaddr_in *); 391 sin->sin_family = AF_INET; 392 sin->sin_addr = ti->ti_src; 393 sin->sin_port = ti->ti_sport; 394 laddr = inp->inp_laddr; 395 if (inp->inp_laddr.s_addr == INADDR_ANY) 396 inp->inp_laddr = ti->ti_dst; 397 if (in_pcbconnect(inp, am)) { 398 inp->inp_laddr = laddr; 399 (void) m_free(am); 400 goto drop; 401 } 402 (void) m_free(am); 403 tp->t_template = tcp_template(tp); 404 if (tp->t_template == 0) { 405 tp = tcp_drop(tp, ENOBUFS); 406 dropsocket = 0; /* socket is already gone */ 407 goto drop; 408 } 409 if (om) { 410 tcp_dooptions(tp, om, ti); 411 om = 0; 412 } 413 if (iss) 414 tp->iss = iss; 415 else 416 tp->iss = tcp_iss; 417 tcp_iss += TCP_ISSINCR/2; 418 tp->irs = ti->ti_seq; 419 tcp_sendseqinit(tp); 420 tcp_rcvseqinit(tp); 421 tp->t_flags |= TF_ACKNOW; 422 tp->t_state = TCPS_SYN_RECEIVED; 423 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 424 dropsocket = 0; /* committed to socket */ 425 tcpstat.tcps_accepts++; 426 goto trimthenstep6; 427 } 428 429 /* 430 * If the state is SYN_SENT: 431 * if seg contains an ACK, but not for our SYN, drop the input. 432 * if seg contains a RST, then drop the connection. 433 * if seg does not contain SYN, then drop it. 434 * Otherwise this is an acceptable SYN segment 435 * initialize tp->rcv_nxt and tp->irs 436 * if seg contains ack then advance tp->snd_una 437 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 438 * arrange for segment to be acked (eventually) 439 * continue processing rest of data/controls, beginning with URG 440 */ 441 case TCPS_SYN_SENT: 442 if ((tiflags & TH_ACK) && 443 (SEQ_LEQ(ti->ti_ack, tp->iss) || 444 SEQ_GT(ti->ti_ack, tp->snd_max))) 445 goto dropwithreset; 446 if (tiflags & TH_RST) { 447 if (tiflags & TH_ACK) 448 tp = tcp_drop(tp, ECONNREFUSED); 449 goto drop; 450 } 451 if ((tiflags & TH_SYN) == 0) 452 goto drop; 453 if (tiflags & TH_ACK) { 454 tp->snd_una = ti->ti_ack; 455 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 456 tp->snd_nxt = tp->snd_una; 457 } 458 tp->t_timer[TCPT_REXMT] = 0; 459 tp->irs = ti->ti_seq; 460 tcp_rcvseqinit(tp); 461 tp->t_flags |= TF_ACKNOW; 462 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 463 tcpstat.tcps_connects++; 464 soisconnected(so); 465 tp->t_state = TCPS_ESTABLISHED; 466 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 467 (void) tcp_reass(tp, (struct tcpiphdr *)0); 468 /* 469 * if we didn't have to retransmit the SYN, 470 * use its rtt as our initial srtt & rtt var. 471 */ 472 if (tp->t_rtt) { 473 tp->t_srtt = tp->t_rtt << 3; 474 tp->t_rttvar = tp->t_rtt << 1; 475 TCPT_RANGESET(tp->t_rxtcur, 476 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 477 TCPTV_MIN, TCPTV_REXMTMAX); 478 tp->t_rtt = 0; 479 } 480 } else 481 tp->t_state = TCPS_SYN_RECEIVED; 482 483 trimthenstep6: 484 /* 485 * Advance ti->ti_seq to correspond to first data byte. 486 * If data, trim to stay within window, 487 * dropping FIN if necessary. 488 */ 489 ti->ti_seq++; 490 if (ti->ti_len > tp->rcv_wnd) { 491 todrop = ti->ti_len - tp->rcv_wnd; 492 #if BSD>=43 493 m_adj(m, -todrop); 494 #else 495 /* XXX work around 4.2 m_adj bug */ 496 if (m->m_len) { 497 m_adj(m, -todrop); 498 } else { 499 /* skip tcp/ip header in first mbuf */ 500 m_adj(m->m_next, -todrop); 501 } 502 #endif 503 ti->ti_len = tp->rcv_wnd; 504 tiflags &= ~TH_FIN; 505 tcpstat.tcps_rcvpackafterwin++; 506 tcpstat.tcps_rcvbyteafterwin += todrop; 507 } 508 tp->snd_wl1 = ti->ti_seq - 1; 509 tp->rcv_up = ti->ti_seq; 510 goto step6; 511 } 512 513 /* 514 * States other than LISTEN or SYN_SENT. 515 * First check that at least some bytes of segment are within 516 * receive window. If segment begins before rcv_nxt, 517 * drop leading data (and SYN); if nothing left, just ack. 518 */ 519 todrop = tp->rcv_nxt - ti->ti_seq; 520 if (todrop > 0) { 521 if (tiflags & TH_SYN) { 522 tiflags &= ~TH_SYN; 523 ti->ti_seq++; 524 if (ti->ti_urp > 1) 525 ti->ti_urp--; 526 else 527 tiflags &= ~TH_URG; 528 todrop--; 529 } 530 if (todrop > ti->ti_len || 531 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 532 #ifdef TCP_COMPAT_42 533 /* 534 * Don't toss RST in response to 4.2-style keepalive. 535 */ 536 if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST) 537 goto do_rst; 538 #endif 539 tcpstat.tcps_rcvduppack++; 540 tcpstat.tcps_rcvdupbyte += ti->ti_len; 541 todrop = ti->ti_len; 542 tiflags &= ~TH_FIN; 543 tp->t_flags |= TF_ACKNOW; 544 } else { 545 tcpstat.tcps_rcvpartduppack++; 546 tcpstat.tcps_rcvpartdupbyte += todrop; 547 } 548 m_adj(m, todrop); 549 ti->ti_seq += todrop; 550 ti->ti_len -= todrop; 551 if (ti->ti_urp > todrop) 552 ti->ti_urp -= todrop; 553 else { 554 tiflags &= ~TH_URG; 555 ti->ti_urp = 0; 556 } 557 } 558 559 /* 560 * If new data is received on a connection after the 561 * user processes are gone, then RST the other end. 562 */ 563 if ((so->so_state & SS_NOFDREF) && 564 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 565 tp = tcp_close(tp); 566 tcpstat.tcps_rcvafterclose++; 567 goto dropwithreset; 568 } 569 570 /* 571 * If segment ends after window, drop trailing data 572 * (and PUSH and FIN); if nothing left, just ACK. 573 */ 574 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 575 if (todrop > 0) { 576 tcpstat.tcps_rcvpackafterwin++; 577 if (todrop >= ti->ti_len) { 578 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 579 /* 580 * If a new connection request is received 581 * while in TIME_WAIT, drop the old connection 582 * and start over if the sequence numbers 583 * are above the previous ones. 584 */ 585 if (tiflags & TH_SYN && 586 tp->t_state == TCPS_TIME_WAIT && 587 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 588 iss = tp->rcv_nxt + TCP_ISSINCR; 589 (void) tcp_close(tp); 590 goto findpcb; 591 } 592 /* 593 * If window is closed can only take segments at 594 * window edge, and have to drop data and PUSH from 595 * incoming segments. Continue processing, but 596 * remember to ack. Otherwise, drop segment 597 * and ack. 598 */ 599 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 600 tp->t_flags |= TF_ACKNOW; 601 tcpstat.tcps_rcvwinprobe++; 602 } else 603 goto dropafterack; 604 } else 605 tcpstat.tcps_rcvbyteafterwin += todrop; 606 #if BSD>=43 607 m_adj(m, -todrop); 608 #else 609 /* XXX work around m_adj bug */ 610 if (m->m_len) { 611 m_adj(m, -todrop); 612 } else { 613 /* skip tcp/ip header in first mbuf */ 614 m_adj(m->m_next, -todrop); 615 } 616 #endif 617 ti->ti_len -= todrop; 618 tiflags &= ~(TH_PUSH|TH_FIN); 619 } 620 621 #ifdef TCP_COMPAT_42 622 do_rst: 623 #endif 624 /* 625 * If the RST bit is set examine the state: 626 * SYN_RECEIVED STATE: 627 * If passive open, return to LISTEN state. 628 * If active open, inform user that connection was refused. 629 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 630 * Inform user that connection was reset, and close tcb. 631 * CLOSING, LAST_ACK, TIME_WAIT STATES 632 * Close the tcb. 633 */ 634 if (tiflags&TH_RST) switch (tp->t_state) { 635 636 case TCPS_SYN_RECEIVED: 637 tp = tcp_drop(tp, ECONNREFUSED); 638 goto drop; 639 640 case TCPS_ESTABLISHED: 641 case TCPS_FIN_WAIT_1: 642 case TCPS_FIN_WAIT_2: 643 case TCPS_CLOSE_WAIT: 644 tp = tcp_drop(tp, ECONNRESET); 645 goto drop; 646 647 case TCPS_CLOSING: 648 case TCPS_LAST_ACK: 649 case TCPS_TIME_WAIT: 650 tp = tcp_close(tp); 651 goto drop; 652 } 653 654 /* 655 * If a SYN is in the window, then this is an 656 * error and we send an RST and drop the connection. 657 */ 658 if (tiflags & TH_SYN) { 659 tp = tcp_drop(tp, ECONNRESET); 660 goto dropwithreset; 661 } 662 663 /* 664 * If the ACK bit is off we drop the segment and return. 665 */ 666 if ((tiflags & TH_ACK) == 0) 667 goto drop; 668 669 /* 670 * Ack processing. 671 */ 672 switch (tp->t_state) { 673 674 /* 675 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 676 * ESTABLISHED state and continue processing, otherwise 677 * send an RST. 678 */ 679 case TCPS_SYN_RECEIVED: 680 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 681 SEQ_GT(ti->ti_ack, tp->snd_max)) 682 goto dropwithreset; 683 tcpstat.tcps_connects++; 684 soisconnected(so); 685 tp->t_state = TCPS_ESTABLISHED; 686 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 687 (void) tcp_reass(tp, (struct tcpiphdr *)0); 688 tp->snd_wl1 = ti->ti_seq - 1; 689 /* fall into ... */ 690 691 /* 692 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 693 * ACKs. If the ack is in the range 694 * tp->snd_una < ti->ti_ack <= tp->snd_max 695 * then advance tp->snd_una to ti->ti_ack and drop 696 * data from the retransmission queue. If this ACK reflects 697 * more up to date window information we update our window information. 698 */ 699 case TCPS_ESTABLISHED: 700 case TCPS_FIN_WAIT_1: 701 case TCPS_FIN_WAIT_2: 702 case TCPS_CLOSE_WAIT: 703 case TCPS_CLOSING: 704 case TCPS_LAST_ACK: 705 case TCPS_TIME_WAIT: 706 707 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 708 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 709 tcpstat.tcps_rcvdupack++; 710 /* 711 * If we have outstanding data (not a 712 * window probe), this is a completely 713 * duplicate ack (ie, window info didn't 714 * change), the ack is the biggest we've 715 * seen and we've seen exactly our rexmt 716 * threshhold of them, assume a packet 717 * has been dropped and retransmit it. 718 * Kludge snd_nxt & the congestion 719 * window so we send only this one 720 * packet. If this packet fills the 721 * only hole in the receiver's seq. 722 * space, the next real ack will fully 723 * open our window. This means we 724 * have to do the usual slow-start to 725 * not overwhelm an intermediate gateway 726 * with a burst of packets. Leave 727 * here with the congestion window set 728 * to allow 2 packets on the next real 729 * ack and the exp-to-linear thresh 730 * set for half the current window 731 * size (since we know we're losing at 732 * the current window size). 733 */ 734 if (tp->t_timer[TCPT_REXMT] == 0 || 735 ti->ti_ack != tp->snd_una) 736 tp->t_dupacks = 0; 737 else if (++tp->t_dupacks == tcprexmtthresh) { 738 tcp_seq onxt = tp->snd_nxt; 739 u_int win = 740 MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / 741 tp->t_maxseg; 742 743 if (win < 2) 744 win = 2; 745 tp->snd_ssthresh = win * tp->t_maxseg; 746 747 tp->t_timer[TCPT_REXMT] = 0; 748 tp->t_rtt = 0; 749 tp->snd_nxt = ti->ti_ack; 750 tp->snd_cwnd = tp->t_maxseg; 751 (void) tcp_output(tp); 752 753 if (SEQ_GT(onxt, tp->snd_nxt)) 754 tp->snd_nxt = onxt; 755 goto drop; 756 } 757 } else 758 tp->t_dupacks = 0; 759 break; 760 } 761 tp->t_dupacks = 0; 762 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 763 tcpstat.tcps_rcvacktoomuch++; 764 goto dropafterack; 765 } 766 acked = ti->ti_ack - tp->snd_una; 767 tcpstat.tcps_rcvackpack++; 768 tcpstat.tcps_rcvackbyte += acked; 769 770 /* 771 * If transmit timer is running and timed sequence 772 * number was acked, update smoothed round trip time. 773 * Since we now have an rtt measurement, cancel the 774 * timer backoff (cf., Phil Karn's retransmit alg.). 775 * Recompute the initial retransmit timer. 776 */ 777 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 778 tcpstat.tcps_rttupdated++; 779 if (tp->t_srtt != 0) { 780 register short delta; 781 782 /* 783 * srtt is stored as fixed point with 3 bits 784 * after the binary point (i.e., scaled by 8). 785 * The following magic is equivalent 786 * to the smoothing algorithm in rfc793 787 * with an alpha of .875 788 * (srtt = rtt/8 + srtt*7/8 in fixed point). 789 * Adjust t_rtt to origin 0. 790 */ 791 tp->t_rtt--; 792 delta = tp->t_rtt - (tp->t_srtt >> 3); 793 if ((tp->t_srtt += delta) <= 0) 794 tp->t_srtt = 1; 795 /* 796 * We accumulate a smoothed rtt variance 797 * (actually, a smoothed mean difference), 798 * then set the retransmit timer to smoothed 799 * rtt + 2 times the smoothed variance. 800 * rttvar is stored as fixed point 801 * with 2 bits after the binary point 802 * (scaled by 4). The following is equivalent 803 * to rfc793 smoothing with an alpha of .75 804 * (rttvar = rttvar*3/4 + |delta| / 4). 805 * This replaces rfc793's wired-in beta. 806 */ 807 if (delta < 0) 808 delta = -delta; 809 delta -= (tp->t_rttvar >> 2); 810 if ((tp->t_rttvar += delta) <= 0) 811 tp->t_rttvar = 1; 812 } else { 813 /* 814 * No rtt measurement yet - use the 815 * unsmoothed rtt. Set the variance 816 * to half the rtt (so our first 817 * retransmit happens at 2*rtt) 818 */ 819 tp->t_srtt = tp->t_rtt << 3; 820 tp->t_rttvar = tp->t_rtt << 1; 821 } 822 tp->t_rtt = 0; 823 tp->t_rxtshift = 0; 824 TCPT_RANGESET(tp->t_rxtcur, 825 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 826 TCPTV_MIN, TCPTV_REXMTMAX); 827 } 828 829 /* 830 * If all outstanding data is acked, stop retransmit 831 * timer and remember to restart (more output or persist). 832 * If there is more data to be acked, restart retransmit 833 * timer, using current (possibly backed-off) value. 834 */ 835 if (ti->ti_ack == tp->snd_max) { 836 tp->t_timer[TCPT_REXMT] = 0; 837 needoutput = 1; 838 } else if (tp->t_timer[TCPT_PERSIST] == 0) 839 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 840 /* 841 * When new data is acked, open the congestion window. 842 * If the window gives us less than ssthresh packets 843 * in flight, open exponentially (maxseg per packet). 844 * Otherwise open linearly (maxseg per window, 845 * or maxseg^2 / cwnd per packet). 846 */ 847 { 848 u_int incr = tp->t_maxseg; 849 850 if (tp->snd_cwnd > tp->snd_ssthresh) 851 incr = MAX(incr * incr / tp->snd_cwnd, 1); 852 853 tp->snd_cwnd = MIN(tp->snd_cwnd + incr, 65535); /* XXX */ 854 } 855 if (acked > so->so_snd.sb_cc) { 856 tp->snd_wnd -= so->so_snd.sb_cc; 857 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 858 ourfinisacked = 1; 859 } else { 860 sbdrop(&so->so_snd, acked); 861 tp->snd_wnd -= acked; 862 ourfinisacked = 0; 863 } 864 if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 865 sowwakeup(so); 866 tp->snd_una = ti->ti_ack; 867 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 868 tp->snd_nxt = tp->snd_una; 869 870 switch (tp->t_state) { 871 872 /* 873 * In FIN_WAIT_1 STATE in addition to the processing 874 * for the ESTABLISHED state if our FIN is now acknowledged 875 * then enter FIN_WAIT_2. 876 */ 877 case TCPS_FIN_WAIT_1: 878 if (ourfinisacked) { 879 /* 880 * If we can't receive any more 881 * data, then closing user can proceed. 882 * Starting the timer is contrary to the 883 * specification, but if we don't get a FIN 884 * we'll hang forever. 885 */ 886 if (so->so_state & SS_CANTRCVMORE) { 887 soisdisconnected(so); 888 tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 889 } 890 tp->t_state = TCPS_FIN_WAIT_2; 891 } 892 break; 893 894 /* 895 * In CLOSING STATE in addition to the processing for 896 * the ESTABLISHED state if the ACK acknowledges our FIN 897 * then enter the TIME-WAIT state, otherwise ignore 898 * the segment. 899 */ 900 case TCPS_CLOSING: 901 if (ourfinisacked) { 902 tp->t_state = TCPS_TIME_WAIT; 903 tcp_canceltimers(tp); 904 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 905 soisdisconnected(so); 906 } 907 break; 908 909 /* 910 * In LAST_ACK, we may still be waiting for data to drain 911 * and/or to be acked, as well as for the ack of our FIN. 912 * If our FIN is now acknowledged, delete the TCB, 913 * enter the closed state and return. 914 */ 915 case TCPS_LAST_ACK: 916 if (ourfinisacked) { 917 tp = tcp_close(tp); 918 goto drop; 919 } 920 break; 921 922 /* 923 * In TIME_WAIT state the only thing that should arrive 924 * is a retransmission of the remote FIN. Acknowledge 925 * it and restart the finack timer. 926 */ 927 case TCPS_TIME_WAIT: 928 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 929 goto dropafterack; 930 } 931 } 932 933 step6: 934 /* 935 * Update window information. 936 * Don't look at window if no ACK: TAC's send garbage on first SYN. 937 */ 938 if ((tiflags & TH_ACK) && 939 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 940 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 941 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 942 /* keep track of pure window updates */ 943 if (ti->ti_len == 0 && 944 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 945 tcpstat.tcps_rcvwinupd++; 946 tp->snd_wnd = ti->ti_win; 947 tp->snd_wl1 = ti->ti_seq; 948 tp->snd_wl2 = ti->ti_ack; 949 if (tp->snd_wnd > tp->max_sndwnd) 950 tp->max_sndwnd = tp->snd_wnd; 951 needoutput = 1; 952 } 953 954 /* 955 * Process segments with URG. 956 */ 957 if ((tiflags & TH_URG) && ti->ti_urp && 958 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 959 /* 960 * This is a kludge, but if we receive and accept 961 * random urgent pointers, we'll crash in 962 * soreceive. It's hard to imagine someone 963 * actually wanting to send this much urgent data. 964 */ 965 if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 966 ti->ti_urp = 0; /* XXX */ 967 tiflags &= ~TH_URG; /* XXX */ 968 goto dodata; /* XXX */ 969 } 970 /* 971 * If this segment advances the known urgent pointer, 972 * then mark the data stream. This should not happen 973 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 974 * a FIN has been received from the remote side. 975 * In these states we ignore the URG. 976 * 977 * According to RFC961 (Assigned Protocols), 978 * the urgent pointer points to the last octet 979 * of urgent data. We continue, however, 980 * to consider it to indicate the first octet 981 * of data past the urgent section 982 * as the original spec states. 983 */ 984 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 985 tp->rcv_up = ti->ti_seq + ti->ti_urp; 986 so->so_oobmark = so->so_rcv.sb_cc + 987 (tp->rcv_up - tp->rcv_nxt) - 1; 988 if (so->so_oobmark == 0) 989 so->so_state |= SS_RCVATMARK; 990 sohasoutofband(so); 991 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 992 } 993 /* 994 * Remove out of band data so doesn't get presented to user. 995 * This can happen independent of advancing the URG pointer, 996 * but if two URG's are pending at once, some out-of-band 997 * data may creep in... ick. 998 */ 999 if (ti->ti_urp <= ti->ti_len 1000 #ifdef SO_OOBINLINE 1001 && (so->so_options & SO_OOBINLINE) == 0 1002 #endif 1003 ) 1004 tcp_pulloutofband(so, ti); 1005 } else 1006 /* 1007 * If no out of band data is expected, 1008 * pull receive urgent pointer along 1009 * with the receive window. 1010 */ 1011 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1012 tp->rcv_up = tp->rcv_nxt; 1013 dodata: /* XXX */ 1014 1015 /* 1016 * Process the segment text, merging it into the TCP sequencing queue, 1017 * and arranging for acknowledgment of receipt if necessary. 1018 * This process logically involves adjusting tp->rcv_wnd as data 1019 * is presented to the user (this happens in tcp_usrreq.c, 1020 * case PRU_RCVD). If a FIN has already been received on this 1021 * connection then we just ignore the text. 1022 */ 1023 if ((ti->ti_len || (tiflags&TH_FIN)) && 1024 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1025 TCP_REASS(tp, ti, m, so, tiflags); 1026 if (tcpnodelack == 0) 1027 tp->t_flags |= TF_DELACK; 1028 else 1029 tp->t_flags |= TF_ACKNOW; 1030 /* 1031 * Note the amount of data that peer has sent into 1032 * our window, in order to estimate the sender's 1033 * buffer size. 1034 */ 1035 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1036 if (len > tp->max_rcvd) 1037 tp->max_rcvd = len; 1038 } else { 1039 m_freem(m); 1040 tiflags &= ~TH_FIN; 1041 } 1042 1043 /* 1044 * If FIN is received ACK the FIN and let the user know 1045 * that the connection is closing. 1046 */ 1047 if (tiflags & TH_FIN) { 1048 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1049 socantrcvmore(so); 1050 tp->t_flags |= TF_ACKNOW; 1051 tp->rcv_nxt++; 1052 } 1053 switch (tp->t_state) { 1054 1055 /* 1056 * In SYN_RECEIVED and ESTABLISHED STATES 1057 * enter the CLOSE_WAIT state. 1058 */ 1059 case TCPS_SYN_RECEIVED: 1060 case TCPS_ESTABLISHED: 1061 tp->t_state = TCPS_CLOSE_WAIT; 1062 break; 1063 1064 /* 1065 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1066 * enter the CLOSING state. 1067 */ 1068 case TCPS_FIN_WAIT_1: 1069 tp->t_state = TCPS_CLOSING; 1070 break; 1071 1072 /* 1073 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1074 * starting the time-wait timer, turning off the other 1075 * standard timers. 1076 */ 1077 case TCPS_FIN_WAIT_2: 1078 tp->t_state = TCPS_TIME_WAIT; 1079 tcp_canceltimers(tp); 1080 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1081 soisdisconnected(so); 1082 break; 1083 1084 /* 1085 * In TIME_WAIT state restart the 2 MSL time_wait timer. 1086 */ 1087 case TCPS_TIME_WAIT: 1088 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1089 break; 1090 } 1091 } 1092 if (so->so_options & SO_DEBUG) 1093 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1094 1095 /* 1096 * Return any desired output. 1097 */ 1098 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1099 (void) tcp_output(tp); 1100 return; 1101 1102 dropafterack: 1103 /* 1104 * Generate an ACK dropping incoming segment if it occupies 1105 * sequence space, where the ACK reflects our state. 1106 */ 1107 if (tiflags & TH_RST) 1108 goto drop; 1109 m_freem(m); 1110 tp->t_flags |= TF_ACKNOW; 1111 (void) tcp_output(tp); 1112 return; 1113 1114 dropwithreset: 1115 if (om) { 1116 (void) m_free(om); 1117 om = 0; 1118 } 1119 /* 1120 * Generate a RST, dropping incoming segment. 1121 * Make ACK acceptable to originator of segment. 1122 * Don't bother to respond if destination was broadcast. 1123 */ 1124 if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 1125 goto drop; 1126 if (tiflags & TH_ACK) 1127 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 1128 else { 1129 if (tiflags & TH_SYN) 1130 ti->ti_len++; 1131 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1132 TH_RST|TH_ACK); 1133 } 1134 /* destroy temporarily created socket */ 1135 if (dropsocket) 1136 (void) soabort(so); 1137 return; 1138 1139 drop: 1140 if (om) 1141 (void) m_free(om); 1142 /* 1143 * Drop space held by incoming segment and return. 1144 */ 1145 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1146 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1147 m_freem(m); 1148 /* destroy temporarily created socket */ 1149 if (dropsocket) 1150 (void) soabort(so); 1151 return; 1152 } 1153 1154 tcp_dooptions(tp, om, ti) 1155 struct tcpcb *tp; 1156 struct mbuf *om; 1157 struct tcpiphdr *ti; 1158 { 1159 register u_char *cp; 1160 int opt, optlen, cnt; 1161 1162 cp = mtod(om, u_char *); 1163 cnt = om->m_len; 1164 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1165 opt = cp[0]; 1166 if (opt == TCPOPT_EOL) 1167 break; 1168 if (opt == TCPOPT_NOP) 1169 optlen = 1; 1170 else { 1171 optlen = cp[1]; 1172 if (optlen <= 0) 1173 break; 1174 } 1175 switch (opt) { 1176 1177 default: 1178 break; 1179 1180 case TCPOPT_MAXSEG: 1181 if (optlen != 4) 1182 continue; 1183 if (!(ti->ti_flags & TH_SYN)) 1184 continue; 1185 tp->t_maxseg = *(u_short *)(cp + 2); 1186 tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 1187 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 1188 break; 1189 } 1190 } 1191 (void) m_free(om); 1192 } 1193 1194 /* 1195 * Pull out of band byte out of a segment so 1196 * it doesn't appear in the user's data queue. 1197 * It is still reflected in the segment length for 1198 * sequencing purposes. 1199 */ 1200 tcp_pulloutofband(so, ti) 1201 struct socket *so; 1202 struct tcpiphdr *ti; 1203 { 1204 register struct mbuf *m; 1205 int cnt = ti->ti_urp - 1; 1206 1207 m = dtom(ti); 1208 while (cnt >= 0) { 1209 if (m->m_len > cnt) { 1210 char *cp = mtod(m, caddr_t) + cnt; 1211 struct tcpcb *tp = sototcpcb(so); 1212 1213 tp->t_iobc = *cp; 1214 tp->t_oobflags |= TCPOOB_HAVEDATA; 1215 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1216 m->m_len--; 1217 return; 1218 } 1219 cnt -= m->m_len; 1220 m = m->m_next; 1221 if (m == 0) 1222 break; 1223 } 1224 panic("tcp_pulloutofband"); 1225 } 1226 1227 /* 1228 * Determine a reasonable value for maxseg size. 1229 * If the route is known, use one that can be handled 1230 * on the given interface without forcing IP to fragment. 1231 * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 1232 * to utilize large mbufs. 1233 * If interface pointer is unavailable, or the destination isn't local, 1234 * use a conservative size (512 or the default IP max size, but no more 1235 * than the mtu of the interface through which we route), 1236 * as we can't discover anything about intervening gateways or networks. 1237 * We also initialize the congestion/slow start window to be a single 1238 * segment if the destination isn't local; this information should 1239 * probably all be saved with the routing entry at the transport level. 1240 * 1241 * This is ugly, and doesn't belong at this level, but has to happen somehow. 1242 */ 1243 tcp_mss(tp) 1244 register struct tcpcb *tp; 1245 { 1246 struct route *ro; 1247 struct ifnet *ifp; 1248 int mss; 1249 struct inpcb *inp; 1250 1251 inp = tp->t_inpcb; 1252 ro = &inp->inp_route; 1253 if ((ro->ro_rt == (struct rtentry *)0) || 1254 (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 1255 /* No route yet, so try to acquire one */ 1256 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1257 ro->ro_dst.sa_family = AF_INET; 1258 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 1259 inp->inp_faddr; 1260 rtalloc(ro); 1261 } 1262 if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 1263 return (TCP_MSS); 1264 } 1265 1266 mss = ifp->if_mtu - sizeof(struct tcpiphdr); 1267 #if (MCLBYTES & (MCLBYTES - 1)) == 0 1268 if (mss > MCLBYTES) 1269 mss &= ~(MCLBYTES-1); 1270 #else 1271 if (mss > MCLBYTES) 1272 mss = mss / MCLBYTES * MCLBYTES; 1273 #endif 1274 if (in_localaddr(inp->inp_faddr)) 1275 return (mss); 1276 1277 mss = MIN(mss, TCP_MSS); 1278 tp->snd_cwnd = mss; 1279 return (mss); 1280 } 1281 1282 #if BSD<43 1283 /* XXX this belongs in netinet/in.c */ 1284 in_localaddr(in) 1285 struct in_addr in; 1286 { 1287 register u_long i = ntohl(in.s_addr); 1288 register struct ifnet *ifp; 1289 register struct sockaddr_in *sin; 1290 register u_long mask; 1291 1292 if (IN_CLASSA(i)) 1293 mask = IN_CLASSA_NET; 1294 else if (IN_CLASSB(i)) 1295 mask = IN_CLASSB_NET; 1296 else if (IN_CLASSC(i)) 1297 mask = IN_CLASSC_NET; 1298 else 1299 return (0); 1300 1301 i &= mask; 1302 for (ifp = ifnet; ifp; ifp = ifp->if_next) { 1303 if (ifp->if_addr.sa_family != AF_INET) 1304 continue; 1305 sin = (struct sockaddr_in *)&ifp->if_addr; 1306 if ((sin->sin_addr.s_addr & mask) == i) 1307 return (1); 1308 } 1309 return (0); 1310 } 1311 #endif 1312