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