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