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