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