35 */ 36 37#ifndef TUBA_INCLUDE 38#include <sys/param.h> 39#include <sys/queue.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/sysctl.h> 43#include <sys/malloc.h> 44#include <sys/mbuf.h> 45#include <sys/proc.h> /* for proc0 declaration */ 46#include <sys/protosw.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/errno.h> 50#include <sys/syslog.h> 51 52#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 53 54#include <net/if.h> 55#include <net/route.h> 56 57#include <netinet/in.h> 58#include <netinet/in_systm.h> 59#include <netinet/ip.h> 60#include <netinet/in_pcb.h> 61#include <netinet/ip_var.h> 62#include <netinet/tcp.h> 63#include <netinet/tcp_fsm.h> 64#include <netinet/tcp_seq.h> 65#include <netinet/tcp_timer.h> 66#include <netinet/tcp_var.h> 67#include <netinet/tcpip.h> 68#ifdef TCPDEBUG 69#include <netinet/tcp_debug.h> 70static struct tcpiphdr tcp_saveti; 71#endif 72 73static int tcprexmtthresh = 3; 74tcp_seq tcp_iss; 75tcp_cc tcp_ccgen; 76 77struct tcpstat tcpstat; 78SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, 79 CTLFLAG_RD, &tcpstat , tcpstat, ""); 80 81static int log_in_vain = 0; 82SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 83 &log_in_vain, 0, ""); 84 85u_long tcp_now; 86struct inpcbhead tcb; 87struct inpcbinfo tcbinfo; 88 89static void tcp_dooptions __P((struct tcpcb *, 90 u_char *, int, struct tcpiphdr *, struct tcpopt *)); 91static void tcp_pulloutofband __P((struct socket *, 92 struct tcpiphdr *, struct mbuf *)); 93static int tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *)); 94static void tcp_xmit_timer __P((struct tcpcb *, int)); 95 96#endif /* TUBA_INCLUDE */ 97 98/* 99 * Insert segment ti into reassembly queue of tcp with 100 * control block tp. Return TH_FIN if reassembly now includes 101 * a segment with FIN. The macro form does the common case inline 102 * (segment is the next to be received on an established connection, 103 * and the queue is empty), avoiding linkage into and removal 104 * from the queue and repetition of various conversions. 105 * Set DELACK for segments received in order, but ack immediately 106 * when segments are out of order (so fast retransmit can work). 107 */ 108#ifdef TCP_ACK_HACK 109#define TCP_REASS(tp, ti, m, so, flags) { \ 110 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 111 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 112 (tp)->t_state == TCPS_ESTABLISHED) { \ 113 if (ti->ti_flags & TH_PUSH) \ 114 tp->t_flags |= TF_ACKNOW; \ 115 else \ 116 tp->t_flags |= TF_DELACK; \ 117 (tp)->rcv_nxt += (ti)->ti_len; \ 118 flags = (ti)->ti_flags & TH_FIN; \ 119 tcpstat.tcps_rcvpack++;\ 120 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 121 sbappend(&(so)->so_rcv, (m)); \ 122 sorwakeup(so); \ 123 } else { \ 124 (flags) = tcp_reass((tp), (ti), (m)); \ 125 tp->t_flags |= TF_ACKNOW; \ 126 } \ 127} 128#else 129#define TCP_REASS(tp, ti, m, so, flags) { \ 130 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 131 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 132 (tp)->t_state == TCPS_ESTABLISHED) { \ 133 tp->t_flags |= TF_DELACK; \ 134 (tp)->rcv_nxt += (ti)->ti_len; \ 135 flags = (ti)->ti_flags & TH_FIN; \ 136 tcpstat.tcps_rcvpack++;\ 137 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 138 sbappend(&(so)->so_rcv, (m)); \ 139 sorwakeup(so); \ 140 } else { \ 141 (flags) = tcp_reass((tp), (ti), (m)); \ 142 tp->t_flags |= TF_ACKNOW; \ 143 } \ 144} 145#endif 146#ifndef TUBA_INCLUDE 147 148static int 149tcp_reass(tp, ti, m) 150 register struct tcpcb *tp; 151 register struct tcpiphdr *ti; 152 struct mbuf *m; 153{ 154 register struct tcpiphdr *q; 155 struct socket *so = tp->t_inpcb->inp_socket; 156 int flags; 157 158 /* 159 * Call with ti==0 after become established to 160 * force pre-ESTABLISHED data up to user socket. 161 */ 162 if (ti == 0) 163 goto present; 164 165 /* 166 * Find a segment which begins after this one does. 167 */ 168 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 169 q = (struct tcpiphdr *)q->ti_next) 170 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 171 break; 172 173 /* 174 * If there is a preceding segment, it may provide some of 175 * our data already. If so, drop the data from the incoming 176 * segment. If it provides all of our data, drop us. 177 */ 178 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 179 register int i; 180 q = (struct tcpiphdr *)q->ti_prev; 181 /* conversion to int (in i) handles seq wraparound */ 182 i = q->ti_seq + q->ti_len - ti->ti_seq; 183 if (i > 0) { 184 if (i >= ti->ti_len) { 185 tcpstat.tcps_rcvduppack++; 186 tcpstat.tcps_rcvdupbyte += ti->ti_len; 187 m_freem(m); 188 /* 189 * Try to present any queued data 190 * at the left window edge to the user. 191 * This is needed after the 3-WHS 192 * completes. 193 */ 194 goto present; /* ??? */ 195 } 196 m_adj(m, i); 197 ti->ti_len -= i; 198 ti->ti_seq += i; 199 } 200 q = (struct tcpiphdr *)(q->ti_next); 201 } 202 tcpstat.tcps_rcvoopack++; 203 tcpstat.tcps_rcvoobyte += ti->ti_len; 204 REASS_MBUF(ti) = m; /* XXX */ 205 206 /* 207 * While we overlap succeeding segments trim them or, 208 * if they are completely covered, dequeue them. 209 */ 210 while (q != (struct tcpiphdr *)tp) { 211 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 212 if (i <= 0) 213 break; 214 if (i < q->ti_len) { 215 q->ti_seq += i; 216 q->ti_len -= i; 217 m_adj(REASS_MBUF(q), i); 218 break; 219 } 220 q = (struct tcpiphdr *)q->ti_next; 221 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 222 remque(q->ti_prev); 223 m_freem(m); 224 } 225 226 /* 227 * Stick new segment in its place. 228 */ 229 insque(ti, q->ti_prev); 230 231present: 232 /* 233 * Present data to user, advancing rcv_nxt through 234 * completed sequence space. 235 */ 236 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 237 return (0); 238 ti = tp->seg_next; 239 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 240 return (0); 241 do { 242 tp->rcv_nxt += ti->ti_len; 243 flags = ti->ti_flags & TH_FIN; 244 remque(ti); 245 m = REASS_MBUF(ti); 246 ti = (struct tcpiphdr *)ti->ti_next; 247 if (so->so_state & SS_CANTRCVMORE) 248 m_freem(m); 249 else 250 sbappend(&so->so_rcv, m); 251 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 252 sorwakeup(so); 253 return (flags); 254} 255 256/* 257 * TCP input routine, follows pages 65-76 of the 258 * protocol specification dated September, 1981 very closely. 259 */ 260void 261tcp_input(m, iphlen) 262 register struct mbuf *m; 263 int iphlen; 264{ 265 register struct tcpiphdr *ti; 266 register struct inpcb *inp; 267 u_char *optp = NULL; 268 int optlen = 0; 269 int len, tlen, off; 270 register struct tcpcb *tp = 0; 271 register int tiflags; 272 struct socket *so = 0; 273 int todrop, acked, ourfinisacked, needoutput = 0; 274 struct in_addr laddr; 275 int dropsocket = 0; 276 int iss = 0; 277 u_long tiwin; 278 struct tcpopt to; /* options in this segment */ 279 struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 280 struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 281#ifdef TCPDEBUG 282 short ostate = 0; 283#endif 284 285 bzero((char *)&to, sizeof(to)); 286 287 tcpstat.tcps_rcvtotal++; 288 /* 289 * Get IP and TCP header together in first mbuf. 290 * Note: IP leaves IP header in first mbuf. 291 */ 292 ti = mtod(m, struct tcpiphdr *); 293 if (iphlen > sizeof (struct ip)) 294 ip_stripoptions(m, (struct mbuf *)0); 295 if (m->m_len < sizeof (struct tcpiphdr)) { 296 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 297 tcpstat.tcps_rcvshort++; 298 return; 299 } 300 ti = mtod(m, struct tcpiphdr *); 301 } 302 303 /* 304 * Checksum extended TCP header and data. 305 */ 306 tlen = ((struct ip *)ti)->ip_len; 307 len = sizeof (struct ip) + tlen; 308 ti->ti_next = ti->ti_prev = 0; 309 ti->ti_x1 = 0; 310 ti->ti_len = (u_short)tlen; 311 HTONS(ti->ti_len); 312 ti->ti_sum = in_cksum(m, len); 313 if (ti->ti_sum) { 314 tcpstat.tcps_rcvbadsum++; 315 goto drop; 316 } 317#endif /* TUBA_INCLUDE */ 318 319 /* 320 * Check that TCP offset makes sense, 321 * pull out TCP options and adjust length. XXX 322 */ 323 off = ti->ti_off << 2; 324 if (off < sizeof (struct tcphdr) || off > tlen) { 325 tcpstat.tcps_rcvbadoff++; 326 goto drop; 327 } 328 tlen -= off; 329 ti->ti_len = tlen; 330 if (off > sizeof (struct tcphdr)) { 331 if (m->m_len < sizeof(struct ip) + off) { 332 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 333 tcpstat.tcps_rcvshort++; 334 return; 335 } 336 ti = mtod(m, struct tcpiphdr *); 337 } 338 optlen = off - sizeof (struct tcphdr); 339 optp = mtod(m, u_char *) + sizeof (struct tcpiphdr); 340 } 341 tiflags = ti->ti_flags; 342 343 /* 344 * Convert TCP protocol specific fields to host format. 345 */ 346 NTOHL(ti->ti_seq); 347 NTOHL(ti->ti_ack); 348 NTOHS(ti->ti_win); 349 NTOHS(ti->ti_urp); 350 351 /* 352 * Drop TCP, IP headers and TCP options. 353 */ 354 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 355 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 356 357 /* 358 * Locate pcb for segment. 359 */ 360findpcb: 361 inp = in_pcblookuphash(&tcbinfo, ti->ti_src, ti->ti_sport, 362 ti->ti_dst, ti->ti_dport, 1); 363 364 /* 365 * If the state is CLOSED (i.e., TCB does not exist) then 366 * all data in the incoming segment is discarded. 367 * If the TCB exists but is in CLOSED state, it is embryonic, 368 * but should either do a listen or a connect soon. 369 */ 370 if (inp == NULL) { 371 if (log_in_vain && tiflags & TH_SYN) { 372 char buf[4*sizeof "123"]; 373 374 strcpy(buf, inet_ntoa(ti->ti_dst)); 375 log(LOG_INFO, "Connection attempt to TCP %s:%d" 376 " from %s:%d\n", 377 buf, ntohs(ti->ti_dport), 378 inet_ntoa(ti->ti_src), ntohs(ti->ti_sport)); 379 } 380 goto dropwithreset; 381 } 382 tp = intotcpcb(inp); 383 if (tp == 0) 384 goto dropwithreset; 385 if (tp->t_state == TCPS_CLOSED) 386 goto drop; 387 388 /* Unscale the window into a 32-bit value. */ 389 if ((tiflags & TH_SYN) == 0) 390 tiwin = ti->ti_win << tp->snd_scale; 391 else 392 tiwin = ti->ti_win; 393 394 so = inp->inp_socket; 395 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 396#ifdef TCPDEBUG 397 if (so->so_options & SO_DEBUG) { 398 ostate = tp->t_state; 399 tcp_saveti = *ti; 400 } 401#endif 402 if (so->so_options & SO_ACCEPTCONN) { 403 register struct tcpcb *tp0 = tp; 404 struct socket *so2; 405 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 406 /* 407 * Note: dropwithreset makes sure we don't 408 * send a RST in response to a RST. 409 */ 410 if (tiflags & TH_ACK) { 411 tcpstat.tcps_badsyn++; 412 goto dropwithreset; 413 } 414 goto drop; 415 } 416 so2 = sonewconn(so, 0); 417 if (so2 == 0) { 418 tcpstat.tcps_listendrop++; 419 so2 = sodropablereq(so); 420 if (so2) { 421 tcp_drop(sototcpcb(so2), ETIMEDOUT); 422 so2 = sonewconn(so, 0); 423 } 424 if (!so2) 425 goto drop; 426 } 427 so = so2; 428 /* 429 * This is ugly, but .... 430 * 431 * Mark socket as temporary until we're 432 * committed to keeping it. The code at 433 * ``drop'' and ``dropwithreset'' check the 434 * flag dropsocket to see if the temporary 435 * socket created here should be discarded. 436 * We mark the socket as discardable until 437 * we're committed to it below in TCPS_LISTEN. 438 */ 439 dropsocket++; 440 inp = (struct inpcb *)so->so_pcb; 441 inp->inp_laddr = ti->ti_dst; 442 inp->inp_lport = ti->ti_dport; 443 in_pcbrehash(inp); 444#if BSD>=43 445 inp->inp_options = ip_srcroute(); 446#endif 447 tp = intotcpcb(inp); 448 tp->t_state = TCPS_LISTEN; 449 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 450 451 /* Compute proper scaling value from buffer space */ 452 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 453 TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) 454 tp->request_r_scale++; 455 } 456 } 457 458 /* 459 * Segment received on connection. 460 * Reset idle time and keep-alive timer. 461 */ 462 tp->t_idle = 0; 463 if (TCPS_HAVEESTABLISHED(tp->t_state)) 464 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 465 466 /* 467 * Process options if not in LISTEN state, 468 * else do it below (after getting remote address). 469 */ 470 if (tp->t_state != TCPS_LISTEN) 471 tcp_dooptions(tp, optp, optlen, ti, &to); 472 473 /* 474 * Header prediction: check for the two common cases 475 * of a uni-directional data xfer. If the packet has 476 * no control flags, is in-sequence, the window didn't 477 * change and we're not retransmitting, it's a 478 * candidate. If the length is zero and the ack moved 479 * forward, we're the sender side of the xfer. Just 480 * free the data acked & wake any higher level process 481 * that was blocked waiting for space. If the length 482 * is non-zero and the ack didn't move, we're the 483 * receiver side. If we're getting packets in-order 484 * (the reassembly queue is empty), add the data to 485 * the socket buffer and note that we need a delayed ack. 486 * Make sure that the hidden state-flags are also off. 487 * Since we check for TCPS_ESTABLISHED above, it can only 488 * be TH_NEEDSYN. 489 */ 490 if (tp->t_state == TCPS_ESTABLISHED && 491 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 492 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 493 ((to.to_flag & TOF_TS) == 0 || 494 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 495 /* 496 * Using the CC option is compulsory if once started: 497 * the segment is OK if no T/TCP was negotiated or 498 * if the segment has a CC option equal to CCrecv 499 */ 500 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 501 (to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv) && 502 ti->ti_seq == tp->rcv_nxt && 503 tiwin && tiwin == tp->snd_wnd && 504 tp->snd_nxt == tp->snd_max) { 505 506 /* 507 * If last ACK falls within this segment's sequence numbers, 508 * record the timestamp. 509 * NOTE that the test is modified according to the latest 510 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 511 */ 512 if ((to.to_flag & TOF_TS) != 0 && 513 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 514 tp->ts_recent_age = tcp_now; 515 tp->ts_recent = to.to_tsval; 516 } 517 518 if (ti->ti_len == 0) { 519 if (SEQ_GT(ti->ti_ack, tp->snd_una) && 520 SEQ_LEQ(ti->ti_ack, tp->snd_max) && 521 tp->snd_cwnd >= tp->snd_wnd && 522 tp->t_dupacks < tcprexmtthresh) { 523 /* 524 * this is a pure ack for outstanding data. 525 */ 526 ++tcpstat.tcps_predack; 527 if ((to.to_flag & TOF_TS) != 0) 528 tcp_xmit_timer(tp, 529 tcp_now - to.to_tsecr + 1); 530 else if (tp->t_rtt && 531 SEQ_GT(ti->ti_ack, tp->t_rtseq)) 532 tcp_xmit_timer(tp, tp->t_rtt); 533 acked = ti->ti_ack - tp->snd_una; 534 tcpstat.tcps_rcvackpack++; 535 tcpstat.tcps_rcvackbyte += acked; 536 sbdrop(&so->so_snd, acked); 537 tp->snd_una = ti->ti_ack; 538 m_freem(m); 539 540 /* 541 * If all outstanding data are acked, stop 542 * retransmit timer, otherwise restart timer 543 * using current (possibly backed-off) value. 544 * If process is waiting for space, 545 * wakeup/selwakeup/signal. If data 546 * are ready to send, let tcp_output 547 * decide between more output or persist. 548 */ 549 if (tp->snd_una == tp->snd_max) 550 tp->t_timer[TCPT_REXMT] = 0; 551 else if (tp->t_timer[TCPT_PERSIST] == 0) 552 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 553 554 if (so->so_snd.sb_flags & SB_NOTIFY) 555 sowwakeup(so); 556 if (so->so_snd.sb_cc) 557 (void) tcp_output(tp); 558 return; 559 } 560 } else if (ti->ti_ack == tp->snd_una && 561 tp->seg_next == (struct tcpiphdr *)tp && 562 ti->ti_len <= sbspace(&so->so_rcv)) { 563 /* 564 * this is a pure, in-sequence data packet 565 * with nothing on the reassembly queue and 566 * we have enough buffer space to take it. 567 */ 568 ++tcpstat.tcps_preddat; 569 tp->rcv_nxt += ti->ti_len; 570 tcpstat.tcps_rcvpack++; 571 tcpstat.tcps_rcvbyte += ti->ti_len; 572 /* 573 * Add data to socket buffer. 574 */ 575 sbappend(&so->so_rcv, m); 576 sorwakeup(so); 577#ifdef TCP_ACK_HACK 578 /* 579 * If this is a short packet, then ACK now - with Nagel 580 * congestion avoidance sender won't send more until 581 * he gets an ACK. 582 */ 583 if (tiflags & TH_PUSH) { 584 tp->t_flags |= TF_ACKNOW; 585 tcp_output(tp); 586 } else { 587 tp->t_flags |= TF_DELACK; 588 } 589#else 590 tp->t_flags |= TF_DELACK; 591#endif 592 return; 593 } 594 } 595 596 /* 597 * Calculate amount of space in receive window, 598 * and then do TCP input processing. 599 * Receive window is amount of space in rcv queue, 600 * but not less than advertised window. 601 */ 602 { int win; 603 604 win = sbspace(&so->so_rcv); 605 if (win < 0) 606 win = 0; 607 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 608 } 609 610 switch (tp->t_state) { 611 612 /* 613 * If the state is LISTEN then ignore segment if it contains an RST. 614 * If the segment contains an ACK then it is bad and send a RST. 615 * If it does not contain a SYN then it is not interesting; drop it. 616 * Don't bother responding if the destination was a broadcast. 617 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 618 * tp->iss, and send a segment: 619 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 620 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 621 * Fill in remote peer address fields if not previously specified. 622 * Enter SYN_RECEIVED state, and process any other fields of this 623 * segment in this state. 624 */ 625 case TCPS_LISTEN: { 626 struct mbuf *am; 627 register struct sockaddr_in *sin; 628 629 if (tiflags & TH_RST) 630 goto drop; 631 if (tiflags & TH_ACK) 632 goto dropwithreset; 633 if ((tiflags & TH_SYN) == 0) 634 goto drop; 635 /* 636 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 637 * in_broadcast() should never return true on a received 638 * packet with M_BCAST not set. 639 */ 640 if (m->m_flags & (M_BCAST|M_MCAST) || 641 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 642 goto drop;
| 35 */ 36 37#ifndef TUBA_INCLUDE 38#include <sys/param.h> 39#include <sys/queue.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/sysctl.h> 43#include <sys/malloc.h> 44#include <sys/mbuf.h> 45#include <sys/proc.h> /* for proc0 declaration */ 46#include <sys/protosw.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/errno.h> 50#include <sys/syslog.h> 51 52#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 53 54#include <net/if.h> 55#include <net/route.h> 56 57#include <netinet/in.h> 58#include <netinet/in_systm.h> 59#include <netinet/ip.h> 60#include <netinet/in_pcb.h> 61#include <netinet/ip_var.h> 62#include <netinet/tcp.h> 63#include <netinet/tcp_fsm.h> 64#include <netinet/tcp_seq.h> 65#include <netinet/tcp_timer.h> 66#include <netinet/tcp_var.h> 67#include <netinet/tcpip.h> 68#ifdef TCPDEBUG 69#include <netinet/tcp_debug.h> 70static struct tcpiphdr tcp_saveti; 71#endif 72 73static int tcprexmtthresh = 3; 74tcp_seq tcp_iss; 75tcp_cc tcp_ccgen; 76 77struct tcpstat tcpstat; 78SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, 79 CTLFLAG_RD, &tcpstat , tcpstat, ""); 80 81static int log_in_vain = 0; 82SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 83 &log_in_vain, 0, ""); 84 85u_long tcp_now; 86struct inpcbhead tcb; 87struct inpcbinfo tcbinfo; 88 89static void tcp_dooptions __P((struct tcpcb *, 90 u_char *, int, struct tcpiphdr *, struct tcpopt *)); 91static void tcp_pulloutofband __P((struct socket *, 92 struct tcpiphdr *, struct mbuf *)); 93static int tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *)); 94static void tcp_xmit_timer __P((struct tcpcb *, int)); 95 96#endif /* TUBA_INCLUDE */ 97 98/* 99 * Insert segment ti into reassembly queue of tcp with 100 * control block tp. Return TH_FIN if reassembly now includes 101 * a segment with FIN. The macro form does the common case inline 102 * (segment is the next to be received on an established connection, 103 * and the queue is empty), avoiding linkage into and removal 104 * from the queue and repetition of various conversions. 105 * Set DELACK for segments received in order, but ack immediately 106 * when segments are out of order (so fast retransmit can work). 107 */ 108#ifdef TCP_ACK_HACK 109#define TCP_REASS(tp, ti, m, so, flags) { \ 110 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 111 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 112 (tp)->t_state == TCPS_ESTABLISHED) { \ 113 if (ti->ti_flags & TH_PUSH) \ 114 tp->t_flags |= TF_ACKNOW; \ 115 else \ 116 tp->t_flags |= TF_DELACK; \ 117 (tp)->rcv_nxt += (ti)->ti_len; \ 118 flags = (ti)->ti_flags & TH_FIN; \ 119 tcpstat.tcps_rcvpack++;\ 120 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 121 sbappend(&(so)->so_rcv, (m)); \ 122 sorwakeup(so); \ 123 } else { \ 124 (flags) = tcp_reass((tp), (ti), (m)); \ 125 tp->t_flags |= TF_ACKNOW; \ 126 } \ 127} 128#else 129#define TCP_REASS(tp, ti, m, so, flags) { \ 130 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 131 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 132 (tp)->t_state == TCPS_ESTABLISHED) { \ 133 tp->t_flags |= TF_DELACK; \ 134 (tp)->rcv_nxt += (ti)->ti_len; \ 135 flags = (ti)->ti_flags & TH_FIN; \ 136 tcpstat.tcps_rcvpack++;\ 137 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 138 sbappend(&(so)->so_rcv, (m)); \ 139 sorwakeup(so); \ 140 } else { \ 141 (flags) = tcp_reass((tp), (ti), (m)); \ 142 tp->t_flags |= TF_ACKNOW; \ 143 } \ 144} 145#endif 146#ifndef TUBA_INCLUDE 147 148static int 149tcp_reass(tp, ti, m) 150 register struct tcpcb *tp; 151 register struct tcpiphdr *ti; 152 struct mbuf *m; 153{ 154 register struct tcpiphdr *q; 155 struct socket *so = tp->t_inpcb->inp_socket; 156 int flags; 157 158 /* 159 * Call with ti==0 after become established to 160 * force pre-ESTABLISHED data up to user socket. 161 */ 162 if (ti == 0) 163 goto present; 164 165 /* 166 * Find a segment which begins after this one does. 167 */ 168 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 169 q = (struct tcpiphdr *)q->ti_next) 170 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 171 break; 172 173 /* 174 * If there is a preceding segment, it may provide some of 175 * our data already. If so, drop the data from the incoming 176 * segment. If it provides all of our data, drop us. 177 */ 178 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 179 register int i; 180 q = (struct tcpiphdr *)q->ti_prev; 181 /* conversion to int (in i) handles seq wraparound */ 182 i = q->ti_seq + q->ti_len - ti->ti_seq; 183 if (i > 0) { 184 if (i >= ti->ti_len) { 185 tcpstat.tcps_rcvduppack++; 186 tcpstat.tcps_rcvdupbyte += ti->ti_len; 187 m_freem(m); 188 /* 189 * Try to present any queued data 190 * at the left window edge to the user. 191 * This is needed after the 3-WHS 192 * completes. 193 */ 194 goto present; /* ??? */ 195 } 196 m_adj(m, i); 197 ti->ti_len -= i; 198 ti->ti_seq += i; 199 } 200 q = (struct tcpiphdr *)(q->ti_next); 201 } 202 tcpstat.tcps_rcvoopack++; 203 tcpstat.tcps_rcvoobyte += ti->ti_len; 204 REASS_MBUF(ti) = m; /* XXX */ 205 206 /* 207 * While we overlap succeeding segments trim them or, 208 * if they are completely covered, dequeue them. 209 */ 210 while (q != (struct tcpiphdr *)tp) { 211 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 212 if (i <= 0) 213 break; 214 if (i < q->ti_len) { 215 q->ti_seq += i; 216 q->ti_len -= i; 217 m_adj(REASS_MBUF(q), i); 218 break; 219 } 220 q = (struct tcpiphdr *)q->ti_next; 221 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 222 remque(q->ti_prev); 223 m_freem(m); 224 } 225 226 /* 227 * Stick new segment in its place. 228 */ 229 insque(ti, q->ti_prev); 230 231present: 232 /* 233 * Present data to user, advancing rcv_nxt through 234 * completed sequence space. 235 */ 236 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 237 return (0); 238 ti = tp->seg_next; 239 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 240 return (0); 241 do { 242 tp->rcv_nxt += ti->ti_len; 243 flags = ti->ti_flags & TH_FIN; 244 remque(ti); 245 m = REASS_MBUF(ti); 246 ti = (struct tcpiphdr *)ti->ti_next; 247 if (so->so_state & SS_CANTRCVMORE) 248 m_freem(m); 249 else 250 sbappend(&so->so_rcv, m); 251 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 252 sorwakeup(so); 253 return (flags); 254} 255 256/* 257 * TCP input routine, follows pages 65-76 of the 258 * protocol specification dated September, 1981 very closely. 259 */ 260void 261tcp_input(m, iphlen) 262 register struct mbuf *m; 263 int iphlen; 264{ 265 register struct tcpiphdr *ti; 266 register struct inpcb *inp; 267 u_char *optp = NULL; 268 int optlen = 0; 269 int len, tlen, off; 270 register struct tcpcb *tp = 0; 271 register int tiflags; 272 struct socket *so = 0; 273 int todrop, acked, ourfinisacked, needoutput = 0; 274 struct in_addr laddr; 275 int dropsocket = 0; 276 int iss = 0; 277 u_long tiwin; 278 struct tcpopt to; /* options in this segment */ 279 struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 280 struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 281#ifdef TCPDEBUG 282 short ostate = 0; 283#endif 284 285 bzero((char *)&to, sizeof(to)); 286 287 tcpstat.tcps_rcvtotal++; 288 /* 289 * Get IP and TCP header together in first mbuf. 290 * Note: IP leaves IP header in first mbuf. 291 */ 292 ti = mtod(m, struct tcpiphdr *); 293 if (iphlen > sizeof (struct ip)) 294 ip_stripoptions(m, (struct mbuf *)0); 295 if (m->m_len < sizeof (struct tcpiphdr)) { 296 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 297 tcpstat.tcps_rcvshort++; 298 return; 299 } 300 ti = mtod(m, struct tcpiphdr *); 301 } 302 303 /* 304 * Checksum extended TCP header and data. 305 */ 306 tlen = ((struct ip *)ti)->ip_len; 307 len = sizeof (struct ip) + tlen; 308 ti->ti_next = ti->ti_prev = 0; 309 ti->ti_x1 = 0; 310 ti->ti_len = (u_short)tlen; 311 HTONS(ti->ti_len); 312 ti->ti_sum = in_cksum(m, len); 313 if (ti->ti_sum) { 314 tcpstat.tcps_rcvbadsum++; 315 goto drop; 316 } 317#endif /* TUBA_INCLUDE */ 318 319 /* 320 * Check that TCP offset makes sense, 321 * pull out TCP options and adjust length. XXX 322 */ 323 off = ti->ti_off << 2; 324 if (off < sizeof (struct tcphdr) || off > tlen) { 325 tcpstat.tcps_rcvbadoff++; 326 goto drop; 327 } 328 tlen -= off; 329 ti->ti_len = tlen; 330 if (off > sizeof (struct tcphdr)) { 331 if (m->m_len < sizeof(struct ip) + off) { 332 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 333 tcpstat.tcps_rcvshort++; 334 return; 335 } 336 ti = mtod(m, struct tcpiphdr *); 337 } 338 optlen = off - sizeof (struct tcphdr); 339 optp = mtod(m, u_char *) + sizeof (struct tcpiphdr); 340 } 341 tiflags = ti->ti_flags; 342 343 /* 344 * Convert TCP protocol specific fields to host format. 345 */ 346 NTOHL(ti->ti_seq); 347 NTOHL(ti->ti_ack); 348 NTOHS(ti->ti_win); 349 NTOHS(ti->ti_urp); 350 351 /* 352 * Drop TCP, IP headers and TCP options. 353 */ 354 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 355 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 356 357 /* 358 * Locate pcb for segment. 359 */ 360findpcb: 361 inp = in_pcblookuphash(&tcbinfo, ti->ti_src, ti->ti_sport, 362 ti->ti_dst, ti->ti_dport, 1); 363 364 /* 365 * If the state is CLOSED (i.e., TCB does not exist) then 366 * all data in the incoming segment is discarded. 367 * If the TCB exists but is in CLOSED state, it is embryonic, 368 * but should either do a listen or a connect soon. 369 */ 370 if (inp == NULL) { 371 if (log_in_vain && tiflags & TH_SYN) { 372 char buf[4*sizeof "123"]; 373 374 strcpy(buf, inet_ntoa(ti->ti_dst)); 375 log(LOG_INFO, "Connection attempt to TCP %s:%d" 376 " from %s:%d\n", 377 buf, ntohs(ti->ti_dport), 378 inet_ntoa(ti->ti_src), ntohs(ti->ti_sport)); 379 } 380 goto dropwithreset; 381 } 382 tp = intotcpcb(inp); 383 if (tp == 0) 384 goto dropwithreset; 385 if (tp->t_state == TCPS_CLOSED) 386 goto drop; 387 388 /* Unscale the window into a 32-bit value. */ 389 if ((tiflags & TH_SYN) == 0) 390 tiwin = ti->ti_win << tp->snd_scale; 391 else 392 tiwin = ti->ti_win; 393 394 so = inp->inp_socket; 395 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 396#ifdef TCPDEBUG 397 if (so->so_options & SO_DEBUG) { 398 ostate = tp->t_state; 399 tcp_saveti = *ti; 400 } 401#endif 402 if (so->so_options & SO_ACCEPTCONN) { 403 register struct tcpcb *tp0 = tp; 404 struct socket *so2; 405 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 406 /* 407 * Note: dropwithreset makes sure we don't 408 * send a RST in response to a RST. 409 */ 410 if (tiflags & TH_ACK) { 411 tcpstat.tcps_badsyn++; 412 goto dropwithreset; 413 } 414 goto drop; 415 } 416 so2 = sonewconn(so, 0); 417 if (so2 == 0) { 418 tcpstat.tcps_listendrop++; 419 so2 = sodropablereq(so); 420 if (so2) { 421 tcp_drop(sototcpcb(so2), ETIMEDOUT); 422 so2 = sonewconn(so, 0); 423 } 424 if (!so2) 425 goto drop; 426 } 427 so = so2; 428 /* 429 * This is ugly, but .... 430 * 431 * Mark socket as temporary until we're 432 * committed to keeping it. The code at 433 * ``drop'' and ``dropwithreset'' check the 434 * flag dropsocket to see if the temporary 435 * socket created here should be discarded. 436 * We mark the socket as discardable until 437 * we're committed to it below in TCPS_LISTEN. 438 */ 439 dropsocket++; 440 inp = (struct inpcb *)so->so_pcb; 441 inp->inp_laddr = ti->ti_dst; 442 inp->inp_lport = ti->ti_dport; 443 in_pcbrehash(inp); 444#if BSD>=43 445 inp->inp_options = ip_srcroute(); 446#endif 447 tp = intotcpcb(inp); 448 tp->t_state = TCPS_LISTEN; 449 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 450 451 /* Compute proper scaling value from buffer space */ 452 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 453 TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) 454 tp->request_r_scale++; 455 } 456 } 457 458 /* 459 * Segment received on connection. 460 * Reset idle time and keep-alive timer. 461 */ 462 tp->t_idle = 0; 463 if (TCPS_HAVEESTABLISHED(tp->t_state)) 464 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 465 466 /* 467 * Process options if not in LISTEN state, 468 * else do it below (after getting remote address). 469 */ 470 if (tp->t_state != TCPS_LISTEN) 471 tcp_dooptions(tp, optp, optlen, ti, &to); 472 473 /* 474 * Header prediction: check for the two common cases 475 * of a uni-directional data xfer. If the packet has 476 * no control flags, is in-sequence, the window didn't 477 * change and we're not retransmitting, it's a 478 * candidate. If the length is zero and the ack moved 479 * forward, we're the sender side of the xfer. Just 480 * free the data acked & wake any higher level process 481 * that was blocked waiting for space. If the length 482 * is non-zero and the ack didn't move, we're the 483 * receiver side. If we're getting packets in-order 484 * (the reassembly queue is empty), add the data to 485 * the socket buffer and note that we need a delayed ack. 486 * Make sure that the hidden state-flags are also off. 487 * Since we check for TCPS_ESTABLISHED above, it can only 488 * be TH_NEEDSYN. 489 */ 490 if (tp->t_state == TCPS_ESTABLISHED && 491 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 492 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 493 ((to.to_flag & TOF_TS) == 0 || 494 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 495 /* 496 * Using the CC option is compulsory if once started: 497 * the segment is OK if no T/TCP was negotiated or 498 * if the segment has a CC option equal to CCrecv 499 */ 500 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 501 (to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv) && 502 ti->ti_seq == tp->rcv_nxt && 503 tiwin && tiwin == tp->snd_wnd && 504 tp->snd_nxt == tp->snd_max) { 505 506 /* 507 * If last ACK falls within this segment's sequence numbers, 508 * record the timestamp. 509 * NOTE that the test is modified according to the latest 510 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 511 */ 512 if ((to.to_flag & TOF_TS) != 0 && 513 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 514 tp->ts_recent_age = tcp_now; 515 tp->ts_recent = to.to_tsval; 516 } 517 518 if (ti->ti_len == 0) { 519 if (SEQ_GT(ti->ti_ack, tp->snd_una) && 520 SEQ_LEQ(ti->ti_ack, tp->snd_max) && 521 tp->snd_cwnd >= tp->snd_wnd && 522 tp->t_dupacks < tcprexmtthresh) { 523 /* 524 * this is a pure ack for outstanding data. 525 */ 526 ++tcpstat.tcps_predack; 527 if ((to.to_flag & TOF_TS) != 0) 528 tcp_xmit_timer(tp, 529 tcp_now - to.to_tsecr + 1); 530 else if (tp->t_rtt && 531 SEQ_GT(ti->ti_ack, tp->t_rtseq)) 532 tcp_xmit_timer(tp, tp->t_rtt); 533 acked = ti->ti_ack - tp->snd_una; 534 tcpstat.tcps_rcvackpack++; 535 tcpstat.tcps_rcvackbyte += acked; 536 sbdrop(&so->so_snd, acked); 537 tp->snd_una = ti->ti_ack; 538 m_freem(m); 539 540 /* 541 * If all outstanding data are acked, stop 542 * retransmit timer, otherwise restart timer 543 * using current (possibly backed-off) value. 544 * If process is waiting for space, 545 * wakeup/selwakeup/signal. If data 546 * are ready to send, let tcp_output 547 * decide between more output or persist. 548 */ 549 if (tp->snd_una == tp->snd_max) 550 tp->t_timer[TCPT_REXMT] = 0; 551 else if (tp->t_timer[TCPT_PERSIST] == 0) 552 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 553 554 if (so->so_snd.sb_flags & SB_NOTIFY) 555 sowwakeup(so); 556 if (so->so_snd.sb_cc) 557 (void) tcp_output(tp); 558 return; 559 } 560 } else if (ti->ti_ack == tp->snd_una && 561 tp->seg_next == (struct tcpiphdr *)tp && 562 ti->ti_len <= sbspace(&so->so_rcv)) { 563 /* 564 * this is a pure, in-sequence data packet 565 * with nothing on the reassembly queue and 566 * we have enough buffer space to take it. 567 */ 568 ++tcpstat.tcps_preddat; 569 tp->rcv_nxt += ti->ti_len; 570 tcpstat.tcps_rcvpack++; 571 tcpstat.tcps_rcvbyte += ti->ti_len; 572 /* 573 * Add data to socket buffer. 574 */ 575 sbappend(&so->so_rcv, m); 576 sorwakeup(so); 577#ifdef TCP_ACK_HACK 578 /* 579 * If this is a short packet, then ACK now - with Nagel 580 * congestion avoidance sender won't send more until 581 * he gets an ACK. 582 */ 583 if (tiflags & TH_PUSH) { 584 tp->t_flags |= TF_ACKNOW; 585 tcp_output(tp); 586 } else { 587 tp->t_flags |= TF_DELACK; 588 } 589#else 590 tp->t_flags |= TF_DELACK; 591#endif 592 return; 593 } 594 } 595 596 /* 597 * Calculate amount of space in receive window, 598 * and then do TCP input processing. 599 * Receive window is amount of space in rcv queue, 600 * but not less than advertised window. 601 */ 602 { int win; 603 604 win = sbspace(&so->so_rcv); 605 if (win < 0) 606 win = 0; 607 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 608 } 609 610 switch (tp->t_state) { 611 612 /* 613 * If the state is LISTEN then ignore segment if it contains an RST. 614 * If the segment contains an ACK then it is bad and send a RST. 615 * If it does not contain a SYN then it is not interesting; drop it. 616 * Don't bother responding if the destination was a broadcast. 617 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 618 * tp->iss, and send a segment: 619 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 620 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 621 * Fill in remote peer address fields if not previously specified. 622 * Enter SYN_RECEIVED state, and process any other fields of this 623 * segment in this state. 624 */ 625 case TCPS_LISTEN: { 626 struct mbuf *am; 627 register struct sockaddr_in *sin; 628 629 if (tiflags & TH_RST) 630 goto drop; 631 if (tiflags & TH_ACK) 632 goto dropwithreset; 633 if ((tiflags & TH_SYN) == 0) 634 goto drop; 635 /* 636 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 637 * in_broadcast() should never return true on a received 638 * packet with M_BCAST not set. 639 */ 640 if (m->m_flags & (M_BCAST|M_MCAST) || 641 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 642 goto drop;
|
662 tp->t_template = tcp_template(tp); 663 if (tp->t_template == 0) { 664 tp = tcp_drop(tp, ENOBUFS); 665 dropsocket = 0; /* socket is already gone */ 666 goto drop; 667 } 668 if ((taop = tcp_gettaocache(inp)) == NULL) { 669 taop = &tao_noncached; 670 bzero(taop, sizeof(*taop)); 671 } 672 tcp_dooptions(tp, optp, optlen, ti, &to); 673 if (iss) 674 tp->iss = iss; 675 else 676 tp->iss = tcp_iss; 677 tcp_iss += TCP_ISSINCR/4; 678 tp->irs = ti->ti_seq; 679 tcp_sendseqinit(tp); 680 tcp_rcvseqinit(tp); 681 /* 682 * Initialization of the tcpcb for transaction; 683 * set SND.WND = SEG.WND, 684 * initialize CCsend and CCrecv. 685 */ 686 tp->snd_wnd = tiwin; /* initial send-window */ 687 tp->cc_send = CC_INC(tcp_ccgen); 688 tp->cc_recv = to.to_cc; 689 /* 690 * Perform TAO test on incoming CC (SEG.CC) option, if any. 691 * - compare SEG.CC against cached CC from the same host, 692 * if any. 693 * - if SEG.CC > chached value, SYN must be new and is accepted 694 * immediately: save new CC in the cache, mark the socket 695 * connected, enter ESTABLISHED state, turn on flag to 696 * send a SYN in the next segment. 697 * A virtual advertised window is set in rcv_adv to 698 * initialize SWS prevention. Then enter normal segment 699 * processing: drop SYN, process data and FIN. 700 * - otherwise do a normal 3-way handshake. 701 */ 702 if ((to.to_flag & TOF_CC) != 0) { 703 if (taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 704 taop->tao_cc = to.to_cc; 705 tp->t_state = TCPS_ESTABLISHED; 706 707 /* 708 * If there is a FIN, or if there is data and the 709 * connection is local, then delay SYN,ACK(SYN) in 710 * the hope of piggy-backing it on a response 711 * segment. Otherwise must send ACK now in case 712 * the other side is slow starting. 713 */ 714 if ((tiflags & TH_FIN) || (ti->ti_len != 0 && 715 in_localaddr(inp->inp_faddr))) 716 tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 717 else 718 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 719 720 /* 721 * Limit the `virtual advertised window' to TCP_MAXWIN 722 * here. Even if we requested window scaling, it will 723 * become effective only later when our SYN is acked. 724 */ 725 tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 726 tcpstat.tcps_connects++; 727 soisconnected(so); 728 tp->t_timer[TCPT_KEEP] = tcp_keepinit; 729 dropsocket = 0; /* committed to socket */ 730 tcpstat.tcps_accepts++; 731 goto trimthenstep6; 732 } 733 /* else do standard 3-way handshake */ 734 } else { 735 /* 736 * No CC option, but maybe CC.NEW: 737 * invalidate cached value. 738 */ 739 taop->tao_cc = 0; 740 } 741 /* 742 * TAO test failed or there was no CC option, 743 * do a standard 3-way handshake. 744 */ 745 tp->t_flags |= TF_ACKNOW; 746 tp->t_state = TCPS_SYN_RECEIVED; 747 tp->t_timer[TCPT_KEEP] = tcp_keepinit; 748 dropsocket = 0; /* committed to socket */ 749 tcpstat.tcps_accepts++; 750 goto trimthenstep6; 751 } 752 753 /* 754 * If the state is SYN_RECEIVED: 755 * do just the ack and RST checks from SYN_SENT state. 756 * If the state is SYN_SENT: 757 * if seg contains an ACK, but not for our SYN, drop the input. 758 * if seg contains a RST, then drop the connection. 759 * if seg does not contain SYN, then drop it. 760 * Otherwise this is an acceptable SYN segment 761 * initialize tp->rcv_nxt and tp->irs 762 * if seg contains ack then advance tp->snd_una 763 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 764 * arrange for segment to be acked (eventually) 765 * continue processing rest of data/controls, beginning with URG 766 */ 767 case TCPS_SYN_RECEIVED: 768 case TCPS_SYN_SENT: 769 if ((taop = tcp_gettaocache(inp)) == NULL) { 770 taop = &tao_noncached; 771 bzero(taop, sizeof(*taop)); 772 } 773 774 if ((tiflags & TH_ACK) && 775 (SEQ_LEQ(ti->ti_ack, tp->iss) || 776 SEQ_GT(ti->ti_ack, tp->snd_max))) { 777 /* 778 * If we have a cached CCsent for the remote host, 779 * hence we haven't just crashed and restarted, 780 * do not send a RST. This may be a retransmission 781 * from the other side after our earlier ACK was lost. 782 * Our new SYN, when it arrives, will serve as the 783 * needed ACK. 784 */ 785 if (taop->tao_ccsent != 0) 786 goto drop; 787 else 788 goto dropwithreset; 789 } 790 if (tiflags & TH_RST) { 791 if (tiflags & TH_ACK) 792 tp = tcp_drop(tp, ECONNREFUSED); 793 goto drop; 794 } 795 if (tp->t_state == TCPS_SYN_RECEIVED) 796 break; 797 if ((tiflags & TH_SYN) == 0) 798 goto drop; 799 tp->snd_wnd = ti->ti_win; /* initial send window */ 800 tp->cc_recv = to.to_cc; /* foreign CC */ 801 802 tp->irs = ti->ti_seq; 803 tcp_rcvseqinit(tp); 804 if (tiflags & TH_ACK) { 805 /* 806 * Our SYN was acked. If segment contains CC.ECHO 807 * option, check it to make sure this segment really 808 * matches our SYN. If not, just drop it as old 809 * duplicate, but send an RST if we're still playing 810 * by the old rules. 811 */ 812 if ((to.to_flag & TOF_CCECHO) && 813 tp->cc_send != to.to_ccecho) { 814 if (taop->tao_ccsent != 0) 815 goto drop; 816 else 817 goto dropwithreset; 818 } 819 tcpstat.tcps_connects++; 820 soisconnected(so); 821 /* Do window scaling on this connection? */ 822 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 823 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 824 tp->snd_scale = tp->requested_s_scale; 825 tp->rcv_scale = tp->request_r_scale; 826 } 827 /* Segment is acceptable, update cache if undefined. */ 828 if (taop->tao_ccsent == 0) 829 taop->tao_ccsent = to.to_ccecho; 830 831 tp->rcv_adv += tp->rcv_wnd; 832 tp->snd_una++; /* SYN is acked */ 833 /* 834 * If there's data, delay ACK; if there's also a FIN 835 * ACKNOW will be turned on later. 836 */ 837 if (ti->ti_len != 0) 838 tp->t_flags |= TF_DELACK; 839 else 840 tp->t_flags |= TF_ACKNOW; 841 /* 842 * Received <SYN,ACK> in SYN_SENT[*] state. 843 * Transitions: 844 * SYN_SENT --> ESTABLISHED 845 * SYN_SENT* --> FIN_WAIT_1 846 */ 847 if (tp->t_flags & TF_NEEDFIN) { 848 tp->t_state = TCPS_FIN_WAIT_1; 849 tp->t_flags &= ~TF_NEEDFIN; 850 tiflags &= ~TH_SYN; 851 } else { 852 tp->t_state = TCPS_ESTABLISHED; 853 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 854 } 855 } else { 856 /* 857 * Received initial SYN in SYN-SENT[*] state => simul- 858 * taneous open. If segment contains CC option and there is 859 * a cached CC, apply TAO test; if it succeeds, connection is 860 * half-synchronized. Otherwise, do 3-way handshake: 861 * SYN-SENT -> SYN-RECEIVED 862 * SYN-SENT* -> SYN-RECEIVED* 863 * If there was no CC option, clear cached CC value. 864 */ 865 tp->t_flags |= TF_ACKNOW; 866 tp->t_timer[TCPT_REXMT] = 0; 867 if (to.to_flag & TOF_CC) { 868 if (taop->tao_cc != 0 && 869 CC_GT(to.to_cc, taop->tao_cc)) { 870 /* 871 * update cache and make transition: 872 * SYN-SENT -> ESTABLISHED* 873 * SYN-SENT* -> FIN-WAIT-1* 874 */ 875 taop->tao_cc = to.to_cc; 876 if (tp->t_flags & TF_NEEDFIN) { 877 tp->t_state = TCPS_FIN_WAIT_1; 878 tp->t_flags &= ~TF_NEEDFIN; 879 } else { 880 tp->t_state = TCPS_ESTABLISHED; 881 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 882 } 883 tp->t_flags |= TF_NEEDSYN; 884 } else 885 tp->t_state = TCPS_SYN_RECEIVED; 886 } else { 887 /* CC.NEW or no option => invalidate cache */ 888 taop->tao_cc = 0; 889 tp->t_state = TCPS_SYN_RECEIVED; 890 } 891 } 892 893trimthenstep6: 894 /* 895 * Advance ti->ti_seq to correspond to first data byte. 896 * If data, trim to stay within window, 897 * dropping FIN if necessary. 898 */ 899 ti->ti_seq++; 900 if (ti->ti_len > tp->rcv_wnd) { 901 todrop = ti->ti_len - tp->rcv_wnd; 902 m_adj(m, -todrop); 903 ti->ti_len = tp->rcv_wnd; 904 tiflags &= ~TH_FIN; 905 tcpstat.tcps_rcvpackafterwin++; 906 tcpstat.tcps_rcvbyteafterwin += todrop; 907 } 908 tp->snd_wl1 = ti->ti_seq - 1; 909 tp->rcv_up = ti->ti_seq; 910 /* 911 * Client side of transaction: already sent SYN and data. 912 * If the remote host used T/TCP to validate the SYN, 913 * our data will be ACK'd; if so, enter normal data segment 914 * processing in the middle of step 5, ack processing. 915 * Otherwise, goto step 6. 916 */ 917 if (tiflags & TH_ACK) 918 goto process_ACK; 919 goto step6; 920 /* 921 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 922 * if segment contains a SYN and CC [not CC.NEW] option: 923 * if state == TIME_WAIT and connection duration > MSL, 924 * drop packet and send RST; 925 * 926 * if SEG.CC > CCrecv then is new SYN, and can implicitly 927 * ack the FIN (and data) in retransmission queue. 928 * Complete close and delete TCPCB. Then reprocess 929 * segment, hoping to find new TCPCB in LISTEN state; 930 * 931 * else must be old SYN; drop it. 932 * else do normal processing. 933 */ 934 case TCPS_LAST_ACK: 935 case TCPS_CLOSING: 936 case TCPS_TIME_WAIT: 937 if ((tiflags & TH_SYN) && 938 (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 939 if (tp->t_state == TCPS_TIME_WAIT && 940 tp->t_duration > TCPTV_MSL) 941 goto dropwithreset; 942 if (CC_GT(to.to_cc, tp->cc_recv)) { 943 tp = tcp_close(tp); 944 goto findpcb; 945 } 946 else 947 goto drop; 948 } 949 break; /* continue normal processing */ 950 } 951 952 /* 953 * States other than LISTEN or SYN_SENT. 954 * First check timestamp, if present. 955 * Then check the connection count, if present. 956 * Then check that at least some bytes of segment are within 957 * receive window. If segment begins before rcv_nxt, 958 * drop leading data (and SYN); if nothing left, just ack. 959 * 960 * RFC 1323 PAWS: If we have a timestamp reply on this segment 961 * and it's less than ts_recent, drop it. 962 */ 963 if ((to.to_flag & TOF_TS) != 0 && (tiflags & TH_RST) == 0 && 964 tp->ts_recent && TSTMP_LT(to.to_tsval, tp->ts_recent)) { 965 966 /* Check to see if ts_recent is over 24 days old. */ 967 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 968 /* 969 * Invalidate ts_recent. If this segment updates 970 * ts_recent, the age will be reset later and ts_recent 971 * will get a valid value. If it does not, setting 972 * ts_recent to zero will at least satisfy the 973 * requirement that zero be placed in the timestamp 974 * echo reply when ts_recent isn't valid. The 975 * age isn't reset until we get a valid ts_recent 976 * because we don't want out-of-order segments to be 977 * dropped when ts_recent is old. 978 */ 979 tp->ts_recent = 0; 980 } else { 981 tcpstat.tcps_rcvduppack++; 982 tcpstat.tcps_rcvdupbyte += ti->ti_len; 983 tcpstat.tcps_pawsdrop++; 984 goto dropafterack; 985 } 986 } 987 988 /* 989 * T/TCP mechanism 990 * If T/TCP was negotiated and the segment doesn't have CC, 991 * or if it's CC is wrong then drop the segment. 992 * RST segments do not have to comply with this. 993 */ 994 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 995 ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc) && 996 (tiflags & TH_RST) == 0) 997 goto dropafterack; 998 999 todrop = tp->rcv_nxt - ti->ti_seq; 1000 if (todrop > 0) { 1001 if (tiflags & TH_SYN) { 1002 tiflags &= ~TH_SYN; 1003 ti->ti_seq++; 1004 if (ti->ti_urp > 1) 1005 ti->ti_urp--; 1006 else 1007 tiflags &= ~TH_URG; 1008 todrop--; 1009 } 1010 /* 1011 * Following if statement from Stevens, vol. 2, p. 960. 1012 */ 1013 if (todrop > ti->ti_len 1014 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) { 1015 /* 1016 * Any valid FIN must be to the left of the window. 1017 * At this point the FIN must be a duplicate or out 1018 * of sequence; drop it. 1019 */ 1020 tiflags &= ~TH_FIN; 1021 1022 /* 1023 * Send an ACK to resynchronize and drop any data. 1024 * But keep on processing for RST or ACK. 1025 */ 1026 tp->t_flags |= TF_ACKNOW; 1027 todrop = ti->ti_len; 1028 tcpstat.tcps_rcvduppack++; 1029 tcpstat.tcps_rcvdupbyte += todrop; 1030 } else { 1031 tcpstat.tcps_rcvpartduppack++; 1032 tcpstat.tcps_rcvpartdupbyte += todrop; 1033 } 1034 m_adj(m, todrop); 1035 ti->ti_seq += todrop; 1036 ti->ti_len -= todrop; 1037 if (ti->ti_urp > todrop) 1038 ti->ti_urp -= todrop; 1039 else { 1040 tiflags &= ~TH_URG; 1041 ti->ti_urp = 0; 1042 } 1043 } 1044 1045 /* 1046 * If new data are received on a connection after the 1047 * user processes are gone, then RST the other end. 1048 */ 1049 if ((so->so_state & SS_NOFDREF) && 1050 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 1051 tp = tcp_close(tp); 1052 tcpstat.tcps_rcvafterclose++; 1053 goto dropwithreset; 1054 } 1055 1056 /* 1057 * If segment ends after window, drop trailing data 1058 * (and PUSH and FIN); if nothing left, just ACK. 1059 */ 1060 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 1061 if (todrop > 0) { 1062 tcpstat.tcps_rcvpackafterwin++; 1063 if (todrop >= ti->ti_len) { 1064 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 1065 /* 1066 * If a new connection request is received 1067 * while in TIME_WAIT, drop the old connection 1068 * and start over if the sequence numbers 1069 * are above the previous ones. 1070 */ 1071 if (tiflags & TH_SYN && 1072 tp->t_state == TCPS_TIME_WAIT && 1073 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 1074 iss = tp->rcv_nxt + TCP_ISSINCR; 1075 tp = tcp_close(tp); 1076 goto findpcb; 1077 } 1078 /* 1079 * If window is closed can only take segments at 1080 * window edge, and have to drop data and PUSH from 1081 * incoming segments. Continue processing, but 1082 * remember to ack. Otherwise, drop segment 1083 * and ack. 1084 */ 1085 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 1086 tp->t_flags |= TF_ACKNOW; 1087 tcpstat.tcps_rcvwinprobe++; 1088 } else 1089 goto dropafterack; 1090 } else 1091 tcpstat.tcps_rcvbyteafterwin += todrop; 1092 m_adj(m, -todrop); 1093 ti->ti_len -= todrop; 1094 tiflags &= ~(TH_PUSH|TH_FIN); 1095 } 1096 1097 /* 1098 * If last ACK falls within this segment's sequence numbers, 1099 * record its timestamp. 1100 * NOTE that the test is modified according to the latest 1101 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1102 */ 1103 if ((to.to_flag & TOF_TS) != 0 && 1104 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 1105 tp->ts_recent_age = tcp_now; 1106 tp->ts_recent = to.to_tsval; 1107 } 1108 1109 /* 1110 * If the RST bit is set examine the state: 1111 * SYN_RECEIVED STATE: 1112 * If passive open, return to LISTEN state. 1113 * If active open, inform user that connection was refused. 1114 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1115 * Inform user that connection was reset, and close tcb. 1116 * CLOSING, LAST_ACK, TIME_WAIT STATES 1117 * Close the tcb. 1118 */ 1119 if (tiflags&TH_RST) switch (tp->t_state) { 1120 1121 case TCPS_SYN_RECEIVED: 1122 so->so_error = ECONNREFUSED; 1123 goto close; 1124 1125 case TCPS_ESTABLISHED: 1126 case TCPS_FIN_WAIT_1: 1127 case TCPS_FIN_WAIT_2: 1128 case TCPS_CLOSE_WAIT: 1129 so->so_error = ECONNRESET; 1130 close: 1131 tp->t_state = TCPS_CLOSED; 1132 tcpstat.tcps_drops++; 1133 tp = tcp_close(tp); 1134 goto drop; 1135 1136 case TCPS_CLOSING: 1137 case TCPS_LAST_ACK: 1138 case TCPS_TIME_WAIT: 1139 tp = tcp_close(tp); 1140 goto drop; 1141 } 1142 1143 /* 1144 * If a SYN is in the window, then this is an 1145 * error and we send an RST and drop the connection. 1146 */ 1147 if (tiflags & TH_SYN) { 1148 tp = tcp_drop(tp, ECONNRESET); 1149 goto dropwithreset; 1150 } 1151 1152 /* 1153 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1154 * flag is on (half-synchronized state), then queue data for 1155 * later processing; else drop segment and return. 1156 */ 1157 if ((tiflags & TH_ACK) == 0) { 1158 if (tp->t_state == TCPS_SYN_RECEIVED || 1159 (tp->t_flags & TF_NEEDSYN)) 1160 goto step6; 1161 else 1162 goto drop; 1163 } 1164 1165 /* 1166 * Ack processing. 1167 */ 1168 switch (tp->t_state) { 1169 1170 /* 1171 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 1172 * ESTABLISHED state and continue processing, otherwise 1173 * send an RST. 1174 */ 1175 case TCPS_SYN_RECEIVED: 1176 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 1177 SEQ_GT(ti->ti_ack, tp->snd_max)) 1178 goto dropwithreset; 1179 1180 tcpstat.tcps_connects++; 1181 soisconnected(so); 1182 /* Do window scaling? */ 1183 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1184 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1185 tp->snd_scale = tp->requested_s_scale; 1186 tp->rcv_scale = tp->request_r_scale; 1187 } 1188 /* 1189 * Upon successful completion of 3-way handshake, 1190 * update cache.CC if it was undefined, pass any queued 1191 * data to the user, and advance state appropriately. 1192 */ 1193 if ((taop = tcp_gettaocache(inp)) != NULL && 1194 taop->tao_cc == 0) 1195 taop->tao_cc = tp->cc_recv; 1196 1197 /* 1198 * Make transitions: 1199 * SYN-RECEIVED -> ESTABLISHED 1200 * SYN-RECEIVED* -> FIN-WAIT-1 1201 */ 1202 if (tp->t_flags & TF_NEEDFIN) { 1203 tp->t_state = TCPS_FIN_WAIT_1; 1204 tp->t_flags &= ~TF_NEEDFIN; 1205 } else { 1206 tp->t_state = TCPS_ESTABLISHED; 1207 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 1208 } 1209 /* 1210 * If segment contains data or ACK, will call tcp_reass() 1211 * later; if not, do so now to pass queued data to user. 1212 */ 1213 if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0) 1214 (void) tcp_reass(tp, (struct tcpiphdr *)0, 1215 (struct mbuf *)0); 1216 tp->snd_wl1 = ti->ti_seq - 1; 1217 /* fall into ... */ 1218 1219 /* 1220 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1221 * ACKs. If the ack is in the range 1222 * tp->snd_una < ti->ti_ack <= tp->snd_max 1223 * then advance tp->snd_una to ti->ti_ack and drop 1224 * data from the retransmission queue. If this ACK reflects 1225 * more up to date window information we update our window information. 1226 */ 1227 case TCPS_ESTABLISHED: 1228 case TCPS_FIN_WAIT_1: 1229 case TCPS_FIN_WAIT_2: 1230 case TCPS_CLOSE_WAIT: 1231 case TCPS_CLOSING: 1232 case TCPS_LAST_ACK: 1233 case TCPS_TIME_WAIT: 1234 1235 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 1236 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 1237 tcpstat.tcps_rcvdupack++; 1238 /* 1239 * If we have outstanding data (other than 1240 * a window probe), this is a completely 1241 * duplicate ack (ie, window info didn't 1242 * change), the ack is the biggest we've 1243 * seen and we've seen exactly our rexmt 1244 * threshhold of them, assume a packet 1245 * has been dropped and retransmit it. 1246 * Kludge snd_nxt & the congestion 1247 * window so we send only this one 1248 * packet. 1249 * 1250 * We know we're losing at the current 1251 * window size so do congestion avoidance 1252 * (set ssthresh to half the current window 1253 * and pull our congestion window back to 1254 * the new ssthresh). 1255 * 1256 * Dup acks mean that packets have left the 1257 * network (they're now cached at the receiver) 1258 * so bump cwnd by the amount in the receiver 1259 * to keep a constant cwnd packets in the 1260 * network. 1261 */ 1262 if (tp->t_timer[TCPT_REXMT] == 0 || 1263 ti->ti_ack != tp->snd_una) 1264 tp->t_dupacks = 0; 1265 else if (++tp->t_dupacks == tcprexmtthresh) { 1266 tcp_seq onxt = tp->snd_nxt; 1267 u_int win = 1268 min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1269 tp->t_maxseg; 1270 1271 if (win < 2) 1272 win = 2; 1273 tp->snd_ssthresh = win * tp->t_maxseg; 1274 tp->t_timer[TCPT_REXMT] = 0; 1275 tp->t_rtt = 0; 1276 tp->snd_nxt = ti->ti_ack; 1277 tp->snd_cwnd = tp->t_maxseg; 1278 (void) tcp_output(tp); 1279 tp->snd_cwnd = tp->snd_ssthresh + 1280 tp->t_maxseg * tp->t_dupacks; 1281 if (SEQ_GT(onxt, tp->snd_nxt)) 1282 tp->snd_nxt = onxt; 1283 goto drop; 1284 } else if (tp->t_dupacks > tcprexmtthresh) { 1285 tp->snd_cwnd += tp->t_maxseg; 1286 (void) tcp_output(tp); 1287 goto drop; 1288 } 1289 } else 1290 tp->t_dupacks = 0; 1291 break; 1292 } 1293 /* 1294 * If the congestion window was inflated to account 1295 * for the other side's cached packets, retract it. 1296 */ 1297 if (tp->t_dupacks >= tcprexmtthresh && 1298 tp->snd_cwnd > tp->snd_ssthresh) 1299 tp->snd_cwnd = tp->snd_ssthresh; 1300 tp->t_dupacks = 0; 1301 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 1302 tcpstat.tcps_rcvacktoomuch++; 1303 goto dropafterack; 1304 } 1305 /* 1306 * If we reach this point, ACK is not a duplicate, 1307 * i.e., it ACKs something we sent. 1308 */ 1309 if (tp->t_flags & TF_NEEDSYN) { 1310 /* 1311 * T/TCP: Connection was half-synchronized, and our 1312 * SYN has been ACK'd (so connection is now fully 1313 * synchronized). Go to non-starred state, 1314 * increment snd_una for ACK of SYN, and check if 1315 * we can do window scaling. 1316 */ 1317 tp->t_flags &= ~TF_NEEDSYN; 1318 tp->snd_una++; 1319 /* Do window scaling? */ 1320 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1321 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1322 tp->snd_scale = tp->requested_s_scale; 1323 tp->rcv_scale = tp->request_r_scale; 1324 } 1325 } 1326 1327process_ACK: 1328 acked = ti->ti_ack - tp->snd_una; 1329 tcpstat.tcps_rcvackpack++; 1330 tcpstat.tcps_rcvackbyte += acked; 1331 1332 /* 1333 * If we have a timestamp reply, update smoothed 1334 * round trip time. If no timestamp is present but 1335 * transmit timer is running and timed sequence 1336 * number was acked, update smoothed round trip time. 1337 * Since we now have an rtt measurement, cancel the 1338 * timer backoff (cf., Phil Karn's retransmit alg.). 1339 * Recompute the initial retransmit timer. 1340 */ 1341 if (to.to_flag & TOF_TS) 1342 tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1); 1343 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 1344 tcp_xmit_timer(tp,tp->t_rtt); 1345 1346 /* 1347 * If all outstanding data is acked, stop retransmit 1348 * timer and remember to restart (more output or persist). 1349 * If there is more data to be acked, restart retransmit 1350 * timer, using current (possibly backed-off) value. 1351 */ 1352 if (ti->ti_ack == tp->snd_max) { 1353 tp->t_timer[TCPT_REXMT] = 0; 1354 needoutput = 1; 1355 } else if (tp->t_timer[TCPT_PERSIST] == 0) 1356 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 1357 1358 /* 1359 * If no data (only SYN) was ACK'd, 1360 * skip rest of ACK processing. 1361 */ 1362 if (acked == 0) 1363 goto step6; 1364 1365 /* 1366 * When new data is acked, open the congestion window. 1367 * If the window gives us less than ssthresh packets 1368 * in flight, open exponentially (maxseg per packet). 1369 * Otherwise open linearly: maxseg per window 1370 * (maxseg^2 / cwnd per packet). 1371 */ 1372 { 1373 register u_int cw = tp->snd_cwnd; 1374 register u_int incr = tp->t_maxseg; 1375 1376 if (cw > tp->snd_ssthresh) 1377 incr = incr * incr / cw; 1378 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1379 } 1380 if (acked > so->so_snd.sb_cc) { 1381 tp->snd_wnd -= so->so_snd.sb_cc; 1382 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1383 ourfinisacked = 1; 1384 } else { 1385 sbdrop(&so->so_snd, acked); 1386 tp->snd_wnd -= acked; 1387 ourfinisacked = 0; 1388 } 1389 if (so->so_snd.sb_flags & SB_NOTIFY) 1390 sowwakeup(so); 1391 tp->snd_una = ti->ti_ack; 1392 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1393 tp->snd_nxt = tp->snd_una; 1394 1395 switch (tp->t_state) { 1396 1397 /* 1398 * In FIN_WAIT_1 STATE in addition to the processing 1399 * for the ESTABLISHED state if our FIN is now acknowledged 1400 * then enter FIN_WAIT_2. 1401 */ 1402 case TCPS_FIN_WAIT_1: 1403 if (ourfinisacked) { 1404 /* 1405 * If we can't receive any more 1406 * data, then closing user can proceed. 1407 * Starting the timer is contrary to the 1408 * specification, but if we don't get a FIN 1409 * we'll hang forever. 1410 */ 1411 if (so->so_state & SS_CANTRCVMORE) { 1412 soisdisconnected(so); 1413 tp->t_timer[TCPT_2MSL] = tcp_maxidle; 1414 } 1415 tp->t_state = TCPS_FIN_WAIT_2; 1416 } 1417 break; 1418 1419 /* 1420 * In CLOSING STATE in addition to the processing for 1421 * the ESTABLISHED state if the ACK acknowledges our FIN 1422 * then enter the TIME-WAIT state, otherwise ignore 1423 * the segment. 1424 */ 1425 case TCPS_CLOSING: 1426 if (ourfinisacked) { 1427 tp->t_state = TCPS_TIME_WAIT; 1428 tcp_canceltimers(tp); 1429 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1430 if (tp->cc_recv != 0 && 1431 tp->t_duration < TCPTV_MSL) 1432 tp->t_timer[TCPT_2MSL] = 1433 tp->t_rxtcur * TCPTV_TWTRUNC; 1434 else 1435 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1436 soisdisconnected(so); 1437 } 1438 break; 1439 1440 /* 1441 * In LAST_ACK, we may still be waiting for data to drain 1442 * and/or to be acked, as well as for the ack of our FIN. 1443 * If our FIN is now acknowledged, delete the TCB, 1444 * enter the closed state and return. 1445 */ 1446 case TCPS_LAST_ACK: 1447 if (ourfinisacked) { 1448 tp = tcp_close(tp); 1449 goto drop; 1450 } 1451 break; 1452 1453 /* 1454 * In TIME_WAIT state the only thing that should arrive 1455 * is a retransmission of the remote FIN. Acknowledge 1456 * it and restart the finack timer. 1457 */ 1458 case TCPS_TIME_WAIT: 1459 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1460 goto dropafterack; 1461 } 1462 } 1463 1464step6: 1465 /* 1466 * Update window information. 1467 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1468 */ 1469 if ((tiflags & TH_ACK) && 1470 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 1471 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 1472 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { 1473 /* keep track of pure window updates */ 1474 if (ti->ti_len == 0 && 1475 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 1476 tcpstat.tcps_rcvwinupd++; 1477 tp->snd_wnd = tiwin; 1478 tp->snd_wl1 = ti->ti_seq; 1479 tp->snd_wl2 = ti->ti_ack; 1480 if (tp->snd_wnd > tp->max_sndwnd) 1481 tp->max_sndwnd = tp->snd_wnd; 1482 needoutput = 1; 1483 } 1484 1485 /* 1486 * Process segments with URG. 1487 */ 1488 if ((tiflags & TH_URG) && ti->ti_urp && 1489 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1490 /* 1491 * This is a kludge, but if we receive and accept 1492 * random urgent pointers, we'll crash in 1493 * soreceive. It's hard to imagine someone 1494 * actually wanting to send this much urgent data. 1495 */ 1496 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 1497 ti->ti_urp = 0; /* XXX */ 1498 tiflags &= ~TH_URG; /* XXX */ 1499 goto dodata; /* XXX */ 1500 } 1501 /* 1502 * If this segment advances the known urgent pointer, 1503 * then mark the data stream. This should not happen 1504 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1505 * a FIN has been received from the remote side. 1506 * In these states we ignore the URG. 1507 * 1508 * According to RFC961 (Assigned Protocols), 1509 * the urgent pointer points to the last octet 1510 * of urgent data. We continue, however, 1511 * to consider it to indicate the first octet 1512 * of data past the urgent section as the original 1513 * spec states (in one of two places). 1514 */ 1515 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 1516 tp->rcv_up = ti->ti_seq + ti->ti_urp; 1517 so->so_oobmark = so->so_rcv.sb_cc + 1518 (tp->rcv_up - tp->rcv_nxt) - 1; 1519 if (so->so_oobmark == 0) 1520 so->so_state |= SS_RCVATMARK; 1521 sohasoutofband(so); 1522 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1523 } 1524 /* 1525 * Remove out of band data so doesn't get presented to user. 1526 * This can happen independent of advancing the URG pointer, 1527 * but if two URG's are pending at once, some out-of-band 1528 * data may creep in... ick. 1529 */ 1530 if (ti->ti_urp <= (u_long)ti->ti_len 1531#ifdef SO_OOBINLINE 1532 && (so->so_options & SO_OOBINLINE) == 0 1533#endif 1534 ) 1535 tcp_pulloutofband(so, ti, m); 1536 } else 1537 /* 1538 * If no out of band data is expected, 1539 * pull receive urgent pointer along 1540 * with the receive window. 1541 */ 1542 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1543 tp->rcv_up = tp->rcv_nxt; 1544dodata: /* XXX */ 1545 1546 /* 1547 * Process the segment text, merging it into the TCP sequencing queue, 1548 * and arranging for acknowledgment of receipt if necessary. 1549 * This process logically involves adjusting tp->rcv_wnd as data 1550 * is presented to the user (this happens in tcp_usrreq.c, 1551 * case PRU_RCVD). If a FIN has already been received on this 1552 * connection then we just ignore the text. 1553 */ 1554 if ((ti->ti_len || (tiflags&TH_FIN)) && 1555 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1556 TCP_REASS(tp, ti, m, so, tiflags); 1557 /* 1558 * Note the amount of data that peer has sent into 1559 * our window, in order to estimate the sender's 1560 * buffer size. 1561 */ 1562 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1563 } else { 1564 m_freem(m); 1565 tiflags &= ~TH_FIN; 1566 } 1567 1568 /* 1569 * If FIN is received ACK the FIN and let the user know 1570 * that the connection is closing. 1571 */ 1572 if (tiflags & TH_FIN) { 1573 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1574 socantrcvmore(so); 1575 /* 1576 * If connection is half-synchronized 1577 * (ie NEEDSYN flag on) then delay ACK, 1578 * so it may be piggybacked when SYN is sent. 1579 * Otherwise, since we received a FIN then no 1580 * more input can be expected, send ACK now. 1581 */ 1582 if (tp->t_flags & TF_NEEDSYN) 1583 tp->t_flags |= TF_DELACK; 1584 else 1585 tp->t_flags |= TF_ACKNOW; 1586 tp->rcv_nxt++; 1587 } 1588 switch (tp->t_state) { 1589 1590 /* 1591 * In SYN_RECEIVED and ESTABLISHED STATES 1592 * enter the CLOSE_WAIT state. 1593 */ 1594 case TCPS_SYN_RECEIVED: 1595 case TCPS_ESTABLISHED: 1596 tp->t_state = TCPS_CLOSE_WAIT; 1597 break; 1598 1599 /* 1600 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1601 * enter the CLOSING state. 1602 */ 1603 case TCPS_FIN_WAIT_1: 1604 tp->t_state = TCPS_CLOSING; 1605 break; 1606 1607 /* 1608 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1609 * starting the time-wait timer, turning off the other 1610 * standard timers. 1611 */ 1612 case TCPS_FIN_WAIT_2: 1613 tp->t_state = TCPS_TIME_WAIT; 1614 tcp_canceltimers(tp); 1615 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1616 if (tp->cc_recv != 0 && 1617 tp->t_duration < TCPTV_MSL) { 1618 tp->t_timer[TCPT_2MSL] = 1619 tp->t_rxtcur * TCPTV_TWTRUNC; 1620 /* For transaction client, force ACK now. */ 1621 tp->t_flags |= TF_ACKNOW; 1622 } 1623 else 1624 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1625 soisdisconnected(so); 1626 break; 1627 1628 /* 1629 * In TIME_WAIT state restart the 2 MSL time_wait timer. 1630 */ 1631 case TCPS_TIME_WAIT: 1632 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1633 break; 1634 } 1635 } 1636#ifdef TCPDEBUG 1637 if (so->so_options & SO_DEBUG) 1638 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1639#endif 1640 1641 /* 1642 * Return any desired output. 1643 */ 1644 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1645 (void) tcp_output(tp); 1646 return; 1647 1648dropafterack: 1649 /* 1650 * Generate an ACK dropping incoming segment if it occupies 1651 * sequence space, where the ACK reflects our state. 1652 */ 1653 if (tiflags & TH_RST) 1654 goto drop; 1655#ifdef TCPDEBUG 1656 if (so->so_options & SO_DEBUG) 1657 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1658#endif 1659 m_freem(m); 1660 tp->t_flags |= TF_ACKNOW; 1661 (void) tcp_output(tp); 1662 return; 1663 1664dropwithreset: 1665 /* 1666 * Generate a RST, dropping incoming segment. 1667 * Make ACK acceptable to originator of segment. 1668 * Don't bother to respond if destination was broadcast/multicast. 1669 */ 1670 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 1671 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 1672 goto drop; 1673#ifdef TCPDEBUG 1674 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1675 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1676#endif 1677 if (tiflags & TH_ACK) 1678 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 1679 else { 1680 if (tiflags & TH_SYN) 1681 ti->ti_len++; 1682 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1683 TH_RST|TH_ACK); 1684 } 1685 /* destroy temporarily created socket */ 1686 if (dropsocket) 1687 (void) soabort(so); 1688 return; 1689 1690drop: 1691 /* 1692 * Drop space held by incoming segment and return. 1693 */ 1694#ifdef TCPDEBUG 1695 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1696 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1697#endif 1698 m_freem(m); 1699 /* destroy temporarily created socket */ 1700 if (dropsocket) 1701 (void) soabort(so); 1702 return; 1703#ifndef TUBA_INCLUDE 1704} 1705 1706static void 1707tcp_dooptions(tp, cp, cnt, ti, to) 1708 struct tcpcb *tp; 1709 u_char *cp; 1710 int cnt; 1711 struct tcpiphdr *ti; 1712 struct tcpopt *to; 1713{ 1714 u_short mss = 0; 1715 int opt, optlen; 1716 1717 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1718 opt = cp[0]; 1719 if (opt == TCPOPT_EOL) 1720 break; 1721 if (opt == TCPOPT_NOP) 1722 optlen = 1; 1723 else { 1724 optlen = cp[1]; 1725 if (optlen <= 0) 1726 break; 1727 } 1728 switch (opt) { 1729 1730 default: 1731 continue; 1732 1733 case TCPOPT_MAXSEG: 1734 if (optlen != TCPOLEN_MAXSEG) 1735 continue; 1736 if (!(ti->ti_flags & TH_SYN)) 1737 continue; 1738 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 1739 NTOHS(mss); 1740 break; 1741 1742 case TCPOPT_WINDOW: 1743 if (optlen != TCPOLEN_WINDOW) 1744 continue; 1745 if (!(ti->ti_flags & TH_SYN)) 1746 continue; 1747 tp->t_flags |= TF_RCVD_SCALE; 1748 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 1749 break; 1750 1751 case TCPOPT_TIMESTAMP: 1752 if (optlen != TCPOLEN_TIMESTAMP) 1753 continue; 1754 to->to_flag |= TOF_TS; 1755 bcopy((char *)cp + 2, 1756 (char *)&to->to_tsval, sizeof(to->to_tsval)); 1757 NTOHL(to->to_tsval); 1758 bcopy((char *)cp + 6, 1759 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 1760 NTOHL(to->to_tsecr); 1761 1762 /* 1763 * A timestamp received in a SYN makes 1764 * it ok to send timestamp requests and replies. 1765 */ 1766 if (ti->ti_flags & TH_SYN) { 1767 tp->t_flags |= TF_RCVD_TSTMP; 1768 tp->ts_recent = to->to_tsval; 1769 tp->ts_recent_age = tcp_now; 1770 } 1771 break; 1772 case TCPOPT_CC: 1773 if (optlen != TCPOLEN_CC) 1774 continue; 1775 to->to_flag |= TOF_CC; 1776 bcopy((char *)cp + 2, 1777 (char *)&to->to_cc, sizeof(to->to_cc)); 1778 NTOHL(to->to_cc); 1779 /* 1780 * A CC or CC.new option received in a SYN makes 1781 * it ok to send CC in subsequent segments. 1782 */ 1783 if (ti->ti_flags & TH_SYN) 1784 tp->t_flags |= TF_RCVD_CC; 1785 break; 1786 case TCPOPT_CCNEW: 1787 if (optlen != TCPOLEN_CC) 1788 continue; 1789 if (!(ti->ti_flags & TH_SYN)) 1790 continue; 1791 to->to_flag |= TOF_CCNEW; 1792 bcopy((char *)cp + 2, 1793 (char *)&to->to_cc, sizeof(to->to_cc)); 1794 NTOHL(to->to_cc); 1795 /* 1796 * A CC or CC.new option received in a SYN makes 1797 * it ok to send CC in subsequent segments. 1798 */ 1799 tp->t_flags |= TF_RCVD_CC; 1800 break; 1801 case TCPOPT_CCECHO: 1802 if (optlen != TCPOLEN_CC) 1803 continue; 1804 if (!(ti->ti_flags & TH_SYN)) 1805 continue; 1806 to->to_flag |= TOF_CCECHO; 1807 bcopy((char *)cp + 2, 1808 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 1809 NTOHL(to->to_ccecho); 1810 break; 1811 } 1812 } 1813 if (ti->ti_flags & TH_SYN) 1814 tcp_mss(tp, mss); /* sets t_maxseg */ 1815} 1816 1817/* 1818 * Pull out of band byte out of a segment so 1819 * it doesn't appear in the user's data queue. 1820 * It is still reflected in the segment length for 1821 * sequencing purposes. 1822 */ 1823static void 1824tcp_pulloutofband(so, ti, m) 1825 struct socket *so; 1826 struct tcpiphdr *ti; 1827 register struct mbuf *m; 1828{ 1829 int cnt = ti->ti_urp - 1; 1830 1831 while (cnt >= 0) { 1832 if (m->m_len > cnt) { 1833 char *cp = mtod(m, caddr_t) + cnt; 1834 struct tcpcb *tp = sototcpcb(so); 1835 1836 tp->t_iobc = *cp; 1837 tp->t_oobflags |= TCPOOB_HAVEDATA; 1838 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1839 m->m_len--; 1840 return; 1841 } 1842 cnt -= m->m_len; 1843 m = m->m_next; 1844 if (m == 0) 1845 break; 1846 } 1847 panic("tcp_pulloutofband"); 1848} 1849 1850/* 1851 * Collect new round-trip time estimate 1852 * and update averages and current timeout. 1853 */ 1854static void 1855tcp_xmit_timer(tp, rtt) 1856 register struct tcpcb *tp; 1857 short rtt; 1858{ 1859 register int delta; 1860 1861 tcpstat.tcps_rttupdated++; 1862 tp->t_rttupdated++; 1863 if (tp->t_srtt != 0) { 1864 /* 1865 * srtt is stored as fixed point with 5 bits after the 1866 * binary point (i.e., scaled by 8). The following magic 1867 * is equivalent to the smoothing algorithm in rfc793 with 1868 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 1869 * point). Adjust rtt to origin 0. 1870 */ 1871 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 1872 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 1873 1874 if ((tp->t_srtt += delta) <= 0) 1875 tp->t_srtt = 1; 1876 1877 /* 1878 * We accumulate a smoothed rtt variance (actually, a 1879 * smoothed mean difference), then set the retransmit 1880 * timer to smoothed rtt + 4 times the smoothed variance. 1881 * rttvar is stored as fixed point with 4 bits after the 1882 * binary point (scaled by 16). The following is 1883 * equivalent to rfc793 smoothing with an alpha of .75 1884 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 1885 * rfc793's wired-in beta. 1886 */ 1887 if (delta < 0) 1888 delta = -delta; 1889 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 1890 if ((tp->t_rttvar += delta) <= 0) 1891 tp->t_rttvar = 1; 1892 } else { 1893 /* 1894 * No rtt measurement yet - use the unsmoothed rtt. 1895 * Set the variance to half the rtt (so our first 1896 * retransmit happens at 3*rtt). 1897 */ 1898 tp->t_srtt = rtt << TCP_RTT_SHIFT; 1899 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 1900 } 1901 tp->t_rtt = 0; 1902 tp->t_rxtshift = 0; 1903 1904 /* 1905 * the retransmit should happen at rtt + 4 * rttvar. 1906 * Because of the way we do the smoothing, srtt and rttvar 1907 * will each average +1/2 tick of bias. When we compute 1908 * the retransmit timer, we want 1/2 tick of rounding and 1909 * 1 extra tick because of +-1/2 tick uncertainty in the 1910 * firing of the timer. The bias will give us exactly the 1911 * 1.5 tick we need. But, because the bias is 1912 * statistical, we have to test that we don't drop below 1913 * the minimum feasible timer (which is 2 ticks). 1914 */ 1915 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 1916 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 1917 1918 /* 1919 * We received an ack for a packet that wasn't retransmitted; 1920 * it is probably safe to discard any error indications we've 1921 * received recently. This isn't quite right, but close enough 1922 * for now (a route might have failed after we sent a segment, 1923 * and the return path might not be symmetrical). 1924 */ 1925 tp->t_softerror = 0; 1926} 1927 1928/* 1929 * Determine a reasonable value for maxseg size. 1930 * If the route is known, check route for mtu. 1931 * If none, use an mss that can be handled on the outgoing 1932 * interface without forcing IP to fragment; if bigger than 1933 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 1934 * to utilize large mbufs. If no route is found, route has no mtu, 1935 * or the destination isn't local, use a default, hopefully conservative 1936 * size (usually 512 or the default IP max size, but no more than the mtu 1937 * of the interface), as we can't discover anything about intervening 1938 * gateways or networks. We also initialize the congestion/slow start 1939 * window to be a single segment if the destination isn't local. 1940 * While looking at the routing entry, we also initialize other path-dependent 1941 * parameters from pre-set or cached values in the routing entry. 1942 * 1943 * Also take into account the space needed for options that we 1944 * send regularly. Make maxseg shorter by that amount to assure 1945 * that we can send maxseg amount of data even when the options 1946 * are present. Store the upper limit of the length of options plus 1947 * data in maxopd. 1948 * 1949 * NOTE that this routine is only called when we process an incoming 1950 * segment, for outgoing segments only tcp_mssopt is called. 1951 * 1952 * In case of T/TCP, we call this routine during implicit connection 1953 * setup as well (offer = -1), to initialize maxseg from the cached 1954 * MSS of our peer. 1955 */ 1956void 1957tcp_mss(tp, offer) 1958 struct tcpcb *tp; 1959 int offer; 1960{ 1961 register struct rtentry *rt; 1962 struct ifnet *ifp; 1963 register int rtt, mss; 1964 u_long bufsize; 1965 struct inpcb *inp; 1966 struct socket *so; 1967 struct rmxp_tao *taop; 1968 int origoffer = offer; 1969 1970 inp = tp->t_inpcb; 1971 if ((rt = tcp_rtlookup(inp)) == NULL) { 1972 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 1973 return; 1974 } 1975 ifp = rt->rt_ifp; 1976 so = inp->inp_socket; 1977 1978 taop = rmx_taop(rt->rt_rmx); 1979 /* 1980 * Offer == -1 means that we didn't receive SYN yet, 1981 * use cached value in that case; 1982 */ 1983 if (offer == -1) 1984 offer = taop->tao_mssopt; 1985 /* 1986 * Offer == 0 means that there was no MSS on the SYN segment, 1987 * in this case we use tcp_mssdflt. 1988 */ 1989 if (offer == 0) 1990 offer = tcp_mssdflt; 1991 else 1992 /* 1993 * Sanity check: make sure that maxopd will be large 1994 * enough to allow some data on segments even is the 1995 * all the option space is used (40bytes). Otherwise 1996 * funny things may happen in tcp_output. 1997 */ 1998 offer = max(offer, 64); 1999 taop->tao_mssopt = offer; 2000 2001 /* 2002 * While we're here, check if there's an initial rtt 2003 * or rttvar. Convert from the route-table units 2004 * to scaled multiples of the slow timeout timer. 2005 */ 2006 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2007 /* 2008 * XXX the lock bit for RTT indicates that the value 2009 * is also a minimum value; this is subject to time. 2010 */ 2011 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2012 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 2013 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 2014 tcpstat.tcps_usedrtt++; 2015 if (rt->rt_rmx.rmx_rttvar) { 2016 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2017 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 2018 tcpstat.tcps_usedrttvar++; 2019 } else { 2020 /* default variation is +- 1 rtt */ 2021 tp->t_rttvar = 2022 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2023 } 2024 TCPT_RANGESET(tp->t_rxtcur, 2025 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2026 tp->t_rttmin, TCPTV_REXMTMAX); 2027 } 2028 /* 2029 * if there's an mtu associated with the route, use it 2030 */ 2031 if (rt->rt_rmx.rmx_mtu) 2032 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 2033 else 2034 { 2035 mss = ifp->if_mtu - sizeof(struct tcpiphdr); 2036 if (!in_localaddr(inp->inp_faddr)) 2037 mss = min(mss, tcp_mssdflt); 2038 } 2039 mss = min(mss, offer); 2040 /* 2041 * maxopd stores the maximum length of data AND options 2042 * in a segment; maxseg is the amount of data in a normal 2043 * segment. We need to store this value (maxopd) apart 2044 * from maxseg, because now every segment carries options 2045 * and thus we normally have somewhat less data in segments. 2046 */ 2047 tp->t_maxopd = mss; 2048 2049 /* 2050 * In case of T/TCP, origoffer==-1 indicates, that no segments 2051 * were received yet. In this case we just guess, otherwise 2052 * we do the same as before T/TCP. 2053 */ 2054 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2055 (origoffer == -1 || 2056 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2057 mss -= TCPOLEN_TSTAMP_APPA; 2058 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2059 (origoffer == -1 || 2060 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2061 mss -= TCPOLEN_CC_APPA; 2062 2063#if (MCLBYTES & (MCLBYTES - 1)) == 0 2064 if (mss > MCLBYTES) 2065 mss &= ~(MCLBYTES-1); 2066#else 2067 if (mss > MCLBYTES) 2068 mss = mss / MCLBYTES * MCLBYTES; 2069#endif 2070 /* 2071 * If there's a pipesize, change the socket buffer 2072 * to that size. Make the socket buffers an integral 2073 * number of mss units; if the mss is larger than 2074 * the socket buffer, decrease the mss. 2075 */ 2076#ifdef RTV_SPIPE 2077 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2078#endif 2079 bufsize = so->so_snd.sb_hiwat; 2080 if (bufsize < mss) 2081 mss = bufsize; 2082 else { 2083 bufsize = roundup(bufsize, mss); 2084 if (bufsize > sb_max) 2085 bufsize = sb_max; 2086 (void)sbreserve(&so->so_snd, bufsize); 2087 } 2088 tp->t_maxseg = mss; 2089 2090#ifdef RTV_RPIPE 2091 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2092#endif 2093 bufsize = so->so_rcv.sb_hiwat; 2094 if (bufsize > mss) { 2095 bufsize = roundup(bufsize, mss); 2096 if (bufsize > sb_max) 2097 bufsize = sb_max; 2098 (void)sbreserve(&so->so_rcv, bufsize); 2099 } 2100 /* 2101 * Don't force slow-start on local network. 2102 */ 2103 if (!in_localaddr(inp->inp_faddr)) 2104 tp->snd_cwnd = mss; 2105 2106 if (rt->rt_rmx.rmx_ssthresh) { 2107 /* 2108 * There's some sort of gateway or interface 2109 * buffer limit on the path. Use this to set 2110 * the slow start threshhold, but set the 2111 * threshold to no less than 2*mss. 2112 */ 2113 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2114 tcpstat.tcps_usedssthresh++; 2115 } 2116} 2117 2118/* 2119 * Determine the MSS option to send on an outgoing SYN. 2120 */ 2121int 2122tcp_mssopt(tp) 2123 struct tcpcb *tp; 2124{ 2125 struct rtentry *rt; 2126 2127 rt = tcp_rtlookup(tp->t_inpcb); 2128 if (rt == NULL) 2129 return tcp_mssdflt; 2130 2131 return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr); 2132} 2133#endif /* TUBA_INCLUDE */
| 661 tp->t_template = tcp_template(tp); 662 if (tp->t_template == 0) { 663 tp = tcp_drop(tp, ENOBUFS); 664 dropsocket = 0; /* socket is already gone */ 665 goto drop; 666 } 667 if ((taop = tcp_gettaocache(inp)) == NULL) { 668 taop = &tao_noncached; 669 bzero(taop, sizeof(*taop)); 670 } 671 tcp_dooptions(tp, optp, optlen, ti, &to); 672 if (iss) 673 tp->iss = iss; 674 else 675 tp->iss = tcp_iss; 676 tcp_iss += TCP_ISSINCR/4; 677 tp->irs = ti->ti_seq; 678 tcp_sendseqinit(tp); 679 tcp_rcvseqinit(tp); 680 /* 681 * Initialization of the tcpcb for transaction; 682 * set SND.WND = SEG.WND, 683 * initialize CCsend and CCrecv. 684 */ 685 tp->snd_wnd = tiwin; /* initial send-window */ 686 tp->cc_send = CC_INC(tcp_ccgen); 687 tp->cc_recv = to.to_cc; 688 /* 689 * Perform TAO test on incoming CC (SEG.CC) option, if any. 690 * - compare SEG.CC against cached CC from the same host, 691 * if any. 692 * - if SEG.CC > chached value, SYN must be new and is accepted 693 * immediately: save new CC in the cache, mark the socket 694 * connected, enter ESTABLISHED state, turn on flag to 695 * send a SYN in the next segment. 696 * A virtual advertised window is set in rcv_adv to 697 * initialize SWS prevention. Then enter normal segment 698 * processing: drop SYN, process data and FIN. 699 * - otherwise do a normal 3-way handshake. 700 */ 701 if ((to.to_flag & TOF_CC) != 0) { 702 if (taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 703 taop->tao_cc = to.to_cc; 704 tp->t_state = TCPS_ESTABLISHED; 705 706 /* 707 * If there is a FIN, or if there is data and the 708 * connection is local, then delay SYN,ACK(SYN) in 709 * the hope of piggy-backing it on a response 710 * segment. Otherwise must send ACK now in case 711 * the other side is slow starting. 712 */ 713 if ((tiflags & TH_FIN) || (ti->ti_len != 0 && 714 in_localaddr(inp->inp_faddr))) 715 tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 716 else 717 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 718 719 /* 720 * Limit the `virtual advertised window' to TCP_MAXWIN 721 * here. Even if we requested window scaling, it will 722 * become effective only later when our SYN is acked. 723 */ 724 tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 725 tcpstat.tcps_connects++; 726 soisconnected(so); 727 tp->t_timer[TCPT_KEEP] = tcp_keepinit; 728 dropsocket = 0; /* committed to socket */ 729 tcpstat.tcps_accepts++; 730 goto trimthenstep6; 731 } 732 /* else do standard 3-way handshake */ 733 } else { 734 /* 735 * No CC option, but maybe CC.NEW: 736 * invalidate cached value. 737 */ 738 taop->tao_cc = 0; 739 } 740 /* 741 * TAO test failed or there was no CC option, 742 * do a standard 3-way handshake. 743 */ 744 tp->t_flags |= TF_ACKNOW; 745 tp->t_state = TCPS_SYN_RECEIVED; 746 tp->t_timer[TCPT_KEEP] = tcp_keepinit; 747 dropsocket = 0; /* committed to socket */ 748 tcpstat.tcps_accepts++; 749 goto trimthenstep6; 750 } 751 752 /* 753 * If the state is SYN_RECEIVED: 754 * do just the ack and RST checks from SYN_SENT state. 755 * If the state is SYN_SENT: 756 * if seg contains an ACK, but not for our SYN, drop the input. 757 * if seg contains a RST, then drop the connection. 758 * if seg does not contain SYN, then drop it. 759 * Otherwise this is an acceptable SYN segment 760 * initialize tp->rcv_nxt and tp->irs 761 * if seg contains ack then advance tp->snd_una 762 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 763 * arrange for segment to be acked (eventually) 764 * continue processing rest of data/controls, beginning with URG 765 */ 766 case TCPS_SYN_RECEIVED: 767 case TCPS_SYN_SENT: 768 if ((taop = tcp_gettaocache(inp)) == NULL) { 769 taop = &tao_noncached; 770 bzero(taop, sizeof(*taop)); 771 } 772 773 if ((tiflags & TH_ACK) && 774 (SEQ_LEQ(ti->ti_ack, tp->iss) || 775 SEQ_GT(ti->ti_ack, tp->snd_max))) { 776 /* 777 * If we have a cached CCsent for the remote host, 778 * hence we haven't just crashed and restarted, 779 * do not send a RST. This may be a retransmission 780 * from the other side after our earlier ACK was lost. 781 * Our new SYN, when it arrives, will serve as the 782 * needed ACK. 783 */ 784 if (taop->tao_ccsent != 0) 785 goto drop; 786 else 787 goto dropwithreset; 788 } 789 if (tiflags & TH_RST) { 790 if (tiflags & TH_ACK) 791 tp = tcp_drop(tp, ECONNREFUSED); 792 goto drop; 793 } 794 if (tp->t_state == TCPS_SYN_RECEIVED) 795 break; 796 if ((tiflags & TH_SYN) == 0) 797 goto drop; 798 tp->snd_wnd = ti->ti_win; /* initial send window */ 799 tp->cc_recv = to.to_cc; /* foreign CC */ 800 801 tp->irs = ti->ti_seq; 802 tcp_rcvseqinit(tp); 803 if (tiflags & TH_ACK) { 804 /* 805 * Our SYN was acked. If segment contains CC.ECHO 806 * option, check it to make sure this segment really 807 * matches our SYN. If not, just drop it as old 808 * duplicate, but send an RST if we're still playing 809 * by the old rules. 810 */ 811 if ((to.to_flag & TOF_CCECHO) && 812 tp->cc_send != to.to_ccecho) { 813 if (taop->tao_ccsent != 0) 814 goto drop; 815 else 816 goto dropwithreset; 817 } 818 tcpstat.tcps_connects++; 819 soisconnected(so); 820 /* Do window scaling on this connection? */ 821 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 822 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 823 tp->snd_scale = tp->requested_s_scale; 824 tp->rcv_scale = tp->request_r_scale; 825 } 826 /* Segment is acceptable, update cache if undefined. */ 827 if (taop->tao_ccsent == 0) 828 taop->tao_ccsent = to.to_ccecho; 829 830 tp->rcv_adv += tp->rcv_wnd; 831 tp->snd_una++; /* SYN is acked */ 832 /* 833 * If there's data, delay ACK; if there's also a FIN 834 * ACKNOW will be turned on later. 835 */ 836 if (ti->ti_len != 0) 837 tp->t_flags |= TF_DELACK; 838 else 839 tp->t_flags |= TF_ACKNOW; 840 /* 841 * Received <SYN,ACK> in SYN_SENT[*] state. 842 * Transitions: 843 * SYN_SENT --> ESTABLISHED 844 * SYN_SENT* --> FIN_WAIT_1 845 */ 846 if (tp->t_flags & TF_NEEDFIN) { 847 tp->t_state = TCPS_FIN_WAIT_1; 848 tp->t_flags &= ~TF_NEEDFIN; 849 tiflags &= ~TH_SYN; 850 } else { 851 tp->t_state = TCPS_ESTABLISHED; 852 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 853 } 854 } else { 855 /* 856 * Received initial SYN in SYN-SENT[*] state => simul- 857 * taneous open. If segment contains CC option and there is 858 * a cached CC, apply TAO test; if it succeeds, connection is 859 * half-synchronized. Otherwise, do 3-way handshake: 860 * SYN-SENT -> SYN-RECEIVED 861 * SYN-SENT* -> SYN-RECEIVED* 862 * If there was no CC option, clear cached CC value. 863 */ 864 tp->t_flags |= TF_ACKNOW; 865 tp->t_timer[TCPT_REXMT] = 0; 866 if (to.to_flag & TOF_CC) { 867 if (taop->tao_cc != 0 && 868 CC_GT(to.to_cc, taop->tao_cc)) { 869 /* 870 * update cache and make transition: 871 * SYN-SENT -> ESTABLISHED* 872 * SYN-SENT* -> FIN-WAIT-1* 873 */ 874 taop->tao_cc = to.to_cc; 875 if (tp->t_flags & TF_NEEDFIN) { 876 tp->t_state = TCPS_FIN_WAIT_1; 877 tp->t_flags &= ~TF_NEEDFIN; 878 } else { 879 tp->t_state = TCPS_ESTABLISHED; 880 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 881 } 882 tp->t_flags |= TF_NEEDSYN; 883 } else 884 tp->t_state = TCPS_SYN_RECEIVED; 885 } else { 886 /* CC.NEW or no option => invalidate cache */ 887 taop->tao_cc = 0; 888 tp->t_state = TCPS_SYN_RECEIVED; 889 } 890 } 891 892trimthenstep6: 893 /* 894 * Advance ti->ti_seq to correspond to first data byte. 895 * If data, trim to stay within window, 896 * dropping FIN if necessary. 897 */ 898 ti->ti_seq++; 899 if (ti->ti_len > tp->rcv_wnd) { 900 todrop = ti->ti_len - tp->rcv_wnd; 901 m_adj(m, -todrop); 902 ti->ti_len = tp->rcv_wnd; 903 tiflags &= ~TH_FIN; 904 tcpstat.tcps_rcvpackafterwin++; 905 tcpstat.tcps_rcvbyteafterwin += todrop; 906 } 907 tp->snd_wl1 = ti->ti_seq - 1; 908 tp->rcv_up = ti->ti_seq; 909 /* 910 * Client side of transaction: already sent SYN and data. 911 * If the remote host used T/TCP to validate the SYN, 912 * our data will be ACK'd; if so, enter normal data segment 913 * processing in the middle of step 5, ack processing. 914 * Otherwise, goto step 6. 915 */ 916 if (tiflags & TH_ACK) 917 goto process_ACK; 918 goto step6; 919 /* 920 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 921 * if segment contains a SYN and CC [not CC.NEW] option: 922 * if state == TIME_WAIT and connection duration > MSL, 923 * drop packet and send RST; 924 * 925 * if SEG.CC > CCrecv then is new SYN, and can implicitly 926 * ack the FIN (and data) in retransmission queue. 927 * Complete close and delete TCPCB. Then reprocess 928 * segment, hoping to find new TCPCB in LISTEN state; 929 * 930 * else must be old SYN; drop it. 931 * else do normal processing. 932 */ 933 case TCPS_LAST_ACK: 934 case TCPS_CLOSING: 935 case TCPS_TIME_WAIT: 936 if ((tiflags & TH_SYN) && 937 (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 938 if (tp->t_state == TCPS_TIME_WAIT && 939 tp->t_duration > TCPTV_MSL) 940 goto dropwithreset; 941 if (CC_GT(to.to_cc, tp->cc_recv)) { 942 tp = tcp_close(tp); 943 goto findpcb; 944 } 945 else 946 goto drop; 947 } 948 break; /* continue normal processing */ 949 } 950 951 /* 952 * States other than LISTEN or SYN_SENT. 953 * First check timestamp, if present. 954 * Then check the connection count, if present. 955 * Then check that at least some bytes of segment are within 956 * receive window. If segment begins before rcv_nxt, 957 * drop leading data (and SYN); if nothing left, just ack. 958 * 959 * RFC 1323 PAWS: If we have a timestamp reply on this segment 960 * and it's less than ts_recent, drop it. 961 */ 962 if ((to.to_flag & TOF_TS) != 0 && (tiflags & TH_RST) == 0 && 963 tp->ts_recent && TSTMP_LT(to.to_tsval, tp->ts_recent)) { 964 965 /* Check to see if ts_recent is over 24 days old. */ 966 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 967 /* 968 * Invalidate ts_recent. If this segment updates 969 * ts_recent, the age will be reset later and ts_recent 970 * will get a valid value. If it does not, setting 971 * ts_recent to zero will at least satisfy the 972 * requirement that zero be placed in the timestamp 973 * echo reply when ts_recent isn't valid. The 974 * age isn't reset until we get a valid ts_recent 975 * because we don't want out-of-order segments to be 976 * dropped when ts_recent is old. 977 */ 978 tp->ts_recent = 0; 979 } else { 980 tcpstat.tcps_rcvduppack++; 981 tcpstat.tcps_rcvdupbyte += ti->ti_len; 982 tcpstat.tcps_pawsdrop++; 983 goto dropafterack; 984 } 985 } 986 987 /* 988 * T/TCP mechanism 989 * If T/TCP was negotiated and the segment doesn't have CC, 990 * or if it's CC is wrong then drop the segment. 991 * RST segments do not have to comply with this. 992 */ 993 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 994 ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc) && 995 (tiflags & TH_RST) == 0) 996 goto dropafterack; 997 998 todrop = tp->rcv_nxt - ti->ti_seq; 999 if (todrop > 0) { 1000 if (tiflags & TH_SYN) { 1001 tiflags &= ~TH_SYN; 1002 ti->ti_seq++; 1003 if (ti->ti_urp > 1) 1004 ti->ti_urp--; 1005 else 1006 tiflags &= ~TH_URG; 1007 todrop--; 1008 } 1009 /* 1010 * Following if statement from Stevens, vol. 2, p. 960. 1011 */ 1012 if (todrop > ti->ti_len 1013 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) { 1014 /* 1015 * Any valid FIN must be to the left of the window. 1016 * At this point the FIN must be a duplicate or out 1017 * of sequence; drop it. 1018 */ 1019 tiflags &= ~TH_FIN; 1020 1021 /* 1022 * Send an ACK to resynchronize and drop any data. 1023 * But keep on processing for RST or ACK. 1024 */ 1025 tp->t_flags |= TF_ACKNOW; 1026 todrop = ti->ti_len; 1027 tcpstat.tcps_rcvduppack++; 1028 tcpstat.tcps_rcvdupbyte += todrop; 1029 } else { 1030 tcpstat.tcps_rcvpartduppack++; 1031 tcpstat.tcps_rcvpartdupbyte += todrop; 1032 } 1033 m_adj(m, todrop); 1034 ti->ti_seq += todrop; 1035 ti->ti_len -= todrop; 1036 if (ti->ti_urp > todrop) 1037 ti->ti_urp -= todrop; 1038 else { 1039 tiflags &= ~TH_URG; 1040 ti->ti_urp = 0; 1041 } 1042 } 1043 1044 /* 1045 * If new data are received on a connection after the 1046 * user processes are gone, then RST the other end. 1047 */ 1048 if ((so->so_state & SS_NOFDREF) && 1049 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 1050 tp = tcp_close(tp); 1051 tcpstat.tcps_rcvafterclose++; 1052 goto dropwithreset; 1053 } 1054 1055 /* 1056 * If segment ends after window, drop trailing data 1057 * (and PUSH and FIN); if nothing left, just ACK. 1058 */ 1059 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 1060 if (todrop > 0) { 1061 tcpstat.tcps_rcvpackafterwin++; 1062 if (todrop >= ti->ti_len) { 1063 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 1064 /* 1065 * If a new connection request is received 1066 * while in TIME_WAIT, drop the old connection 1067 * and start over if the sequence numbers 1068 * are above the previous ones. 1069 */ 1070 if (tiflags & TH_SYN && 1071 tp->t_state == TCPS_TIME_WAIT && 1072 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 1073 iss = tp->rcv_nxt + TCP_ISSINCR; 1074 tp = tcp_close(tp); 1075 goto findpcb; 1076 } 1077 /* 1078 * If window is closed can only take segments at 1079 * window edge, and have to drop data and PUSH from 1080 * incoming segments. Continue processing, but 1081 * remember to ack. Otherwise, drop segment 1082 * and ack. 1083 */ 1084 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 1085 tp->t_flags |= TF_ACKNOW; 1086 tcpstat.tcps_rcvwinprobe++; 1087 } else 1088 goto dropafterack; 1089 } else 1090 tcpstat.tcps_rcvbyteafterwin += todrop; 1091 m_adj(m, -todrop); 1092 ti->ti_len -= todrop; 1093 tiflags &= ~(TH_PUSH|TH_FIN); 1094 } 1095 1096 /* 1097 * If last ACK falls within this segment's sequence numbers, 1098 * record its timestamp. 1099 * NOTE that the test is modified according to the latest 1100 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1101 */ 1102 if ((to.to_flag & TOF_TS) != 0 && 1103 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 1104 tp->ts_recent_age = tcp_now; 1105 tp->ts_recent = to.to_tsval; 1106 } 1107 1108 /* 1109 * If the RST bit is set examine the state: 1110 * SYN_RECEIVED STATE: 1111 * If passive open, return to LISTEN state. 1112 * If active open, inform user that connection was refused. 1113 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1114 * Inform user that connection was reset, and close tcb. 1115 * CLOSING, LAST_ACK, TIME_WAIT STATES 1116 * Close the tcb. 1117 */ 1118 if (tiflags&TH_RST) switch (tp->t_state) { 1119 1120 case TCPS_SYN_RECEIVED: 1121 so->so_error = ECONNREFUSED; 1122 goto close; 1123 1124 case TCPS_ESTABLISHED: 1125 case TCPS_FIN_WAIT_1: 1126 case TCPS_FIN_WAIT_2: 1127 case TCPS_CLOSE_WAIT: 1128 so->so_error = ECONNRESET; 1129 close: 1130 tp->t_state = TCPS_CLOSED; 1131 tcpstat.tcps_drops++; 1132 tp = tcp_close(tp); 1133 goto drop; 1134 1135 case TCPS_CLOSING: 1136 case TCPS_LAST_ACK: 1137 case TCPS_TIME_WAIT: 1138 tp = tcp_close(tp); 1139 goto drop; 1140 } 1141 1142 /* 1143 * If a SYN is in the window, then this is an 1144 * error and we send an RST and drop the connection. 1145 */ 1146 if (tiflags & TH_SYN) { 1147 tp = tcp_drop(tp, ECONNRESET); 1148 goto dropwithreset; 1149 } 1150 1151 /* 1152 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1153 * flag is on (half-synchronized state), then queue data for 1154 * later processing; else drop segment and return. 1155 */ 1156 if ((tiflags & TH_ACK) == 0) { 1157 if (tp->t_state == TCPS_SYN_RECEIVED || 1158 (tp->t_flags & TF_NEEDSYN)) 1159 goto step6; 1160 else 1161 goto drop; 1162 } 1163 1164 /* 1165 * Ack processing. 1166 */ 1167 switch (tp->t_state) { 1168 1169 /* 1170 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 1171 * ESTABLISHED state and continue processing, otherwise 1172 * send an RST. 1173 */ 1174 case TCPS_SYN_RECEIVED: 1175 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 1176 SEQ_GT(ti->ti_ack, tp->snd_max)) 1177 goto dropwithreset; 1178 1179 tcpstat.tcps_connects++; 1180 soisconnected(so); 1181 /* Do window scaling? */ 1182 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1183 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1184 tp->snd_scale = tp->requested_s_scale; 1185 tp->rcv_scale = tp->request_r_scale; 1186 } 1187 /* 1188 * Upon successful completion of 3-way handshake, 1189 * update cache.CC if it was undefined, pass any queued 1190 * data to the user, and advance state appropriately. 1191 */ 1192 if ((taop = tcp_gettaocache(inp)) != NULL && 1193 taop->tao_cc == 0) 1194 taop->tao_cc = tp->cc_recv; 1195 1196 /* 1197 * Make transitions: 1198 * SYN-RECEIVED -> ESTABLISHED 1199 * SYN-RECEIVED* -> FIN-WAIT-1 1200 */ 1201 if (tp->t_flags & TF_NEEDFIN) { 1202 tp->t_state = TCPS_FIN_WAIT_1; 1203 tp->t_flags &= ~TF_NEEDFIN; 1204 } else { 1205 tp->t_state = TCPS_ESTABLISHED; 1206 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 1207 } 1208 /* 1209 * If segment contains data or ACK, will call tcp_reass() 1210 * later; if not, do so now to pass queued data to user. 1211 */ 1212 if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0) 1213 (void) tcp_reass(tp, (struct tcpiphdr *)0, 1214 (struct mbuf *)0); 1215 tp->snd_wl1 = ti->ti_seq - 1; 1216 /* fall into ... */ 1217 1218 /* 1219 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1220 * ACKs. If the ack is in the range 1221 * tp->snd_una < ti->ti_ack <= tp->snd_max 1222 * then advance tp->snd_una to ti->ti_ack and drop 1223 * data from the retransmission queue. If this ACK reflects 1224 * more up to date window information we update our window information. 1225 */ 1226 case TCPS_ESTABLISHED: 1227 case TCPS_FIN_WAIT_1: 1228 case TCPS_FIN_WAIT_2: 1229 case TCPS_CLOSE_WAIT: 1230 case TCPS_CLOSING: 1231 case TCPS_LAST_ACK: 1232 case TCPS_TIME_WAIT: 1233 1234 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 1235 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 1236 tcpstat.tcps_rcvdupack++; 1237 /* 1238 * If we have outstanding data (other than 1239 * a window probe), this is a completely 1240 * duplicate ack (ie, window info didn't 1241 * change), the ack is the biggest we've 1242 * seen and we've seen exactly our rexmt 1243 * threshhold of them, assume a packet 1244 * has been dropped and retransmit it. 1245 * Kludge snd_nxt & the congestion 1246 * window so we send only this one 1247 * packet. 1248 * 1249 * We know we're losing at the current 1250 * window size so do congestion avoidance 1251 * (set ssthresh to half the current window 1252 * and pull our congestion window back to 1253 * the new ssthresh). 1254 * 1255 * Dup acks mean that packets have left the 1256 * network (they're now cached at the receiver) 1257 * so bump cwnd by the amount in the receiver 1258 * to keep a constant cwnd packets in the 1259 * network. 1260 */ 1261 if (tp->t_timer[TCPT_REXMT] == 0 || 1262 ti->ti_ack != tp->snd_una) 1263 tp->t_dupacks = 0; 1264 else if (++tp->t_dupacks == tcprexmtthresh) { 1265 tcp_seq onxt = tp->snd_nxt; 1266 u_int win = 1267 min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1268 tp->t_maxseg; 1269 1270 if (win < 2) 1271 win = 2; 1272 tp->snd_ssthresh = win * tp->t_maxseg; 1273 tp->t_timer[TCPT_REXMT] = 0; 1274 tp->t_rtt = 0; 1275 tp->snd_nxt = ti->ti_ack; 1276 tp->snd_cwnd = tp->t_maxseg; 1277 (void) tcp_output(tp); 1278 tp->snd_cwnd = tp->snd_ssthresh + 1279 tp->t_maxseg * tp->t_dupacks; 1280 if (SEQ_GT(onxt, tp->snd_nxt)) 1281 tp->snd_nxt = onxt; 1282 goto drop; 1283 } else if (tp->t_dupacks > tcprexmtthresh) { 1284 tp->snd_cwnd += tp->t_maxseg; 1285 (void) tcp_output(tp); 1286 goto drop; 1287 } 1288 } else 1289 tp->t_dupacks = 0; 1290 break; 1291 } 1292 /* 1293 * If the congestion window was inflated to account 1294 * for the other side's cached packets, retract it. 1295 */ 1296 if (tp->t_dupacks >= tcprexmtthresh && 1297 tp->snd_cwnd > tp->snd_ssthresh) 1298 tp->snd_cwnd = tp->snd_ssthresh; 1299 tp->t_dupacks = 0; 1300 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 1301 tcpstat.tcps_rcvacktoomuch++; 1302 goto dropafterack; 1303 } 1304 /* 1305 * If we reach this point, ACK is not a duplicate, 1306 * i.e., it ACKs something we sent. 1307 */ 1308 if (tp->t_flags & TF_NEEDSYN) { 1309 /* 1310 * T/TCP: Connection was half-synchronized, and our 1311 * SYN has been ACK'd (so connection is now fully 1312 * synchronized). Go to non-starred state, 1313 * increment snd_una for ACK of SYN, and check if 1314 * we can do window scaling. 1315 */ 1316 tp->t_flags &= ~TF_NEEDSYN; 1317 tp->snd_una++; 1318 /* Do window scaling? */ 1319 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1320 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1321 tp->snd_scale = tp->requested_s_scale; 1322 tp->rcv_scale = tp->request_r_scale; 1323 } 1324 } 1325 1326process_ACK: 1327 acked = ti->ti_ack - tp->snd_una; 1328 tcpstat.tcps_rcvackpack++; 1329 tcpstat.tcps_rcvackbyte += acked; 1330 1331 /* 1332 * If we have a timestamp reply, update smoothed 1333 * round trip time. If no timestamp is present but 1334 * transmit timer is running and timed sequence 1335 * number was acked, update smoothed round trip time. 1336 * Since we now have an rtt measurement, cancel the 1337 * timer backoff (cf., Phil Karn's retransmit alg.). 1338 * Recompute the initial retransmit timer. 1339 */ 1340 if (to.to_flag & TOF_TS) 1341 tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1); 1342 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 1343 tcp_xmit_timer(tp,tp->t_rtt); 1344 1345 /* 1346 * If all outstanding data is acked, stop retransmit 1347 * timer and remember to restart (more output or persist). 1348 * If there is more data to be acked, restart retransmit 1349 * timer, using current (possibly backed-off) value. 1350 */ 1351 if (ti->ti_ack == tp->snd_max) { 1352 tp->t_timer[TCPT_REXMT] = 0; 1353 needoutput = 1; 1354 } else if (tp->t_timer[TCPT_PERSIST] == 0) 1355 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 1356 1357 /* 1358 * If no data (only SYN) was ACK'd, 1359 * skip rest of ACK processing. 1360 */ 1361 if (acked == 0) 1362 goto step6; 1363 1364 /* 1365 * When new data is acked, open the congestion window. 1366 * If the window gives us less than ssthresh packets 1367 * in flight, open exponentially (maxseg per packet). 1368 * Otherwise open linearly: maxseg per window 1369 * (maxseg^2 / cwnd per packet). 1370 */ 1371 { 1372 register u_int cw = tp->snd_cwnd; 1373 register u_int incr = tp->t_maxseg; 1374 1375 if (cw > tp->snd_ssthresh) 1376 incr = incr * incr / cw; 1377 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1378 } 1379 if (acked > so->so_snd.sb_cc) { 1380 tp->snd_wnd -= so->so_snd.sb_cc; 1381 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1382 ourfinisacked = 1; 1383 } else { 1384 sbdrop(&so->so_snd, acked); 1385 tp->snd_wnd -= acked; 1386 ourfinisacked = 0; 1387 } 1388 if (so->so_snd.sb_flags & SB_NOTIFY) 1389 sowwakeup(so); 1390 tp->snd_una = ti->ti_ack; 1391 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1392 tp->snd_nxt = tp->snd_una; 1393 1394 switch (tp->t_state) { 1395 1396 /* 1397 * In FIN_WAIT_1 STATE in addition to the processing 1398 * for the ESTABLISHED state if our FIN is now acknowledged 1399 * then enter FIN_WAIT_2. 1400 */ 1401 case TCPS_FIN_WAIT_1: 1402 if (ourfinisacked) { 1403 /* 1404 * If we can't receive any more 1405 * data, then closing user can proceed. 1406 * Starting the timer is contrary to the 1407 * specification, but if we don't get a FIN 1408 * we'll hang forever. 1409 */ 1410 if (so->so_state & SS_CANTRCVMORE) { 1411 soisdisconnected(so); 1412 tp->t_timer[TCPT_2MSL] = tcp_maxidle; 1413 } 1414 tp->t_state = TCPS_FIN_WAIT_2; 1415 } 1416 break; 1417 1418 /* 1419 * In CLOSING STATE in addition to the processing for 1420 * the ESTABLISHED state if the ACK acknowledges our FIN 1421 * then enter the TIME-WAIT state, otherwise ignore 1422 * the segment. 1423 */ 1424 case TCPS_CLOSING: 1425 if (ourfinisacked) { 1426 tp->t_state = TCPS_TIME_WAIT; 1427 tcp_canceltimers(tp); 1428 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1429 if (tp->cc_recv != 0 && 1430 tp->t_duration < TCPTV_MSL) 1431 tp->t_timer[TCPT_2MSL] = 1432 tp->t_rxtcur * TCPTV_TWTRUNC; 1433 else 1434 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1435 soisdisconnected(so); 1436 } 1437 break; 1438 1439 /* 1440 * In LAST_ACK, we may still be waiting for data to drain 1441 * and/or to be acked, as well as for the ack of our FIN. 1442 * If our FIN is now acknowledged, delete the TCB, 1443 * enter the closed state and return. 1444 */ 1445 case TCPS_LAST_ACK: 1446 if (ourfinisacked) { 1447 tp = tcp_close(tp); 1448 goto drop; 1449 } 1450 break; 1451 1452 /* 1453 * In TIME_WAIT state the only thing that should arrive 1454 * is a retransmission of the remote FIN. Acknowledge 1455 * it and restart the finack timer. 1456 */ 1457 case TCPS_TIME_WAIT: 1458 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1459 goto dropafterack; 1460 } 1461 } 1462 1463step6: 1464 /* 1465 * Update window information. 1466 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1467 */ 1468 if ((tiflags & TH_ACK) && 1469 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 1470 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 1471 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { 1472 /* keep track of pure window updates */ 1473 if (ti->ti_len == 0 && 1474 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 1475 tcpstat.tcps_rcvwinupd++; 1476 tp->snd_wnd = tiwin; 1477 tp->snd_wl1 = ti->ti_seq; 1478 tp->snd_wl2 = ti->ti_ack; 1479 if (tp->snd_wnd > tp->max_sndwnd) 1480 tp->max_sndwnd = tp->snd_wnd; 1481 needoutput = 1; 1482 } 1483 1484 /* 1485 * Process segments with URG. 1486 */ 1487 if ((tiflags & TH_URG) && ti->ti_urp && 1488 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1489 /* 1490 * This is a kludge, but if we receive and accept 1491 * random urgent pointers, we'll crash in 1492 * soreceive. It's hard to imagine someone 1493 * actually wanting to send this much urgent data. 1494 */ 1495 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 1496 ti->ti_urp = 0; /* XXX */ 1497 tiflags &= ~TH_URG; /* XXX */ 1498 goto dodata; /* XXX */ 1499 } 1500 /* 1501 * If this segment advances the known urgent pointer, 1502 * then mark the data stream. This should not happen 1503 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1504 * a FIN has been received from the remote side. 1505 * In these states we ignore the URG. 1506 * 1507 * According to RFC961 (Assigned Protocols), 1508 * the urgent pointer points to the last octet 1509 * of urgent data. We continue, however, 1510 * to consider it to indicate the first octet 1511 * of data past the urgent section as the original 1512 * spec states (in one of two places). 1513 */ 1514 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 1515 tp->rcv_up = ti->ti_seq + ti->ti_urp; 1516 so->so_oobmark = so->so_rcv.sb_cc + 1517 (tp->rcv_up - tp->rcv_nxt) - 1; 1518 if (so->so_oobmark == 0) 1519 so->so_state |= SS_RCVATMARK; 1520 sohasoutofband(so); 1521 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1522 } 1523 /* 1524 * Remove out of band data so doesn't get presented to user. 1525 * This can happen independent of advancing the URG pointer, 1526 * but if two URG's are pending at once, some out-of-band 1527 * data may creep in... ick. 1528 */ 1529 if (ti->ti_urp <= (u_long)ti->ti_len 1530#ifdef SO_OOBINLINE 1531 && (so->so_options & SO_OOBINLINE) == 0 1532#endif 1533 ) 1534 tcp_pulloutofband(so, ti, m); 1535 } else 1536 /* 1537 * If no out of band data is expected, 1538 * pull receive urgent pointer along 1539 * with the receive window. 1540 */ 1541 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1542 tp->rcv_up = tp->rcv_nxt; 1543dodata: /* XXX */ 1544 1545 /* 1546 * Process the segment text, merging it into the TCP sequencing queue, 1547 * and arranging for acknowledgment of receipt if necessary. 1548 * This process logically involves adjusting tp->rcv_wnd as data 1549 * is presented to the user (this happens in tcp_usrreq.c, 1550 * case PRU_RCVD). If a FIN has already been received on this 1551 * connection then we just ignore the text. 1552 */ 1553 if ((ti->ti_len || (tiflags&TH_FIN)) && 1554 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1555 TCP_REASS(tp, ti, m, so, tiflags); 1556 /* 1557 * Note the amount of data that peer has sent into 1558 * our window, in order to estimate the sender's 1559 * buffer size. 1560 */ 1561 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1562 } else { 1563 m_freem(m); 1564 tiflags &= ~TH_FIN; 1565 } 1566 1567 /* 1568 * If FIN is received ACK the FIN and let the user know 1569 * that the connection is closing. 1570 */ 1571 if (tiflags & TH_FIN) { 1572 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1573 socantrcvmore(so); 1574 /* 1575 * If connection is half-synchronized 1576 * (ie NEEDSYN flag on) then delay ACK, 1577 * so it may be piggybacked when SYN is sent. 1578 * Otherwise, since we received a FIN then no 1579 * more input can be expected, send ACK now. 1580 */ 1581 if (tp->t_flags & TF_NEEDSYN) 1582 tp->t_flags |= TF_DELACK; 1583 else 1584 tp->t_flags |= TF_ACKNOW; 1585 tp->rcv_nxt++; 1586 } 1587 switch (tp->t_state) { 1588 1589 /* 1590 * In SYN_RECEIVED and ESTABLISHED STATES 1591 * enter the CLOSE_WAIT state. 1592 */ 1593 case TCPS_SYN_RECEIVED: 1594 case TCPS_ESTABLISHED: 1595 tp->t_state = TCPS_CLOSE_WAIT; 1596 break; 1597 1598 /* 1599 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1600 * enter the CLOSING state. 1601 */ 1602 case TCPS_FIN_WAIT_1: 1603 tp->t_state = TCPS_CLOSING; 1604 break; 1605 1606 /* 1607 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1608 * starting the time-wait timer, turning off the other 1609 * standard timers. 1610 */ 1611 case TCPS_FIN_WAIT_2: 1612 tp->t_state = TCPS_TIME_WAIT; 1613 tcp_canceltimers(tp); 1614 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1615 if (tp->cc_recv != 0 && 1616 tp->t_duration < TCPTV_MSL) { 1617 tp->t_timer[TCPT_2MSL] = 1618 tp->t_rxtcur * TCPTV_TWTRUNC; 1619 /* For transaction client, force ACK now. */ 1620 tp->t_flags |= TF_ACKNOW; 1621 } 1622 else 1623 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1624 soisdisconnected(so); 1625 break; 1626 1627 /* 1628 * In TIME_WAIT state restart the 2 MSL time_wait timer. 1629 */ 1630 case TCPS_TIME_WAIT: 1631 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1632 break; 1633 } 1634 } 1635#ifdef TCPDEBUG 1636 if (so->so_options & SO_DEBUG) 1637 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1638#endif 1639 1640 /* 1641 * Return any desired output. 1642 */ 1643 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1644 (void) tcp_output(tp); 1645 return; 1646 1647dropafterack: 1648 /* 1649 * Generate an ACK dropping incoming segment if it occupies 1650 * sequence space, where the ACK reflects our state. 1651 */ 1652 if (tiflags & TH_RST) 1653 goto drop; 1654#ifdef TCPDEBUG 1655 if (so->so_options & SO_DEBUG) 1656 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1657#endif 1658 m_freem(m); 1659 tp->t_flags |= TF_ACKNOW; 1660 (void) tcp_output(tp); 1661 return; 1662 1663dropwithreset: 1664 /* 1665 * Generate a RST, dropping incoming segment. 1666 * Make ACK acceptable to originator of segment. 1667 * Don't bother to respond if destination was broadcast/multicast. 1668 */ 1669 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 1670 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 1671 goto drop; 1672#ifdef TCPDEBUG 1673 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1674 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1675#endif 1676 if (tiflags & TH_ACK) 1677 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 1678 else { 1679 if (tiflags & TH_SYN) 1680 ti->ti_len++; 1681 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1682 TH_RST|TH_ACK); 1683 } 1684 /* destroy temporarily created socket */ 1685 if (dropsocket) 1686 (void) soabort(so); 1687 return; 1688 1689drop: 1690 /* 1691 * Drop space held by incoming segment and return. 1692 */ 1693#ifdef TCPDEBUG 1694 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1695 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1696#endif 1697 m_freem(m); 1698 /* destroy temporarily created socket */ 1699 if (dropsocket) 1700 (void) soabort(so); 1701 return; 1702#ifndef TUBA_INCLUDE 1703} 1704 1705static void 1706tcp_dooptions(tp, cp, cnt, ti, to) 1707 struct tcpcb *tp; 1708 u_char *cp; 1709 int cnt; 1710 struct tcpiphdr *ti; 1711 struct tcpopt *to; 1712{ 1713 u_short mss = 0; 1714 int opt, optlen; 1715 1716 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1717 opt = cp[0]; 1718 if (opt == TCPOPT_EOL) 1719 break; 1720 if (opt == TCPOPT_NOP) 1721 optlen = 1; 1722 else { 1723 optlen = cp[1]; 1724 if (optlen <= 0) 1725 break; 1726 } 1727 switch (opt) { 1728 1729 default: 1730 continue; 1731 1732 case TCPOPT_MAXSEG: 1733 if (optlen != TCPOLEN_MAXSEG) 1734 continue; 1735 if (!(ti->ti_flags & TH_SYN)) 1736 continue; 1737 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 1738 NTOHS(mss); 1739 break; 1740 1741 case TCPOPT_WINDOW: 1742 if (optlen != TCPOLEN_WINDOW) 1743 continue; 1744 if (!(ti->ti_flags & TH_SYN)) 1745 continue; 1746 tp->t_flags |= TF_RCVD_SCALE; 1747 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 1748 break; 1749 1750 case TCPOPT_TIMESTAMP: 1751 if (optlen != TCPOLEN_TIMESTAMP) 1752 continue; 1753 to->to_flag |= TOF_TS; 1754 bcopy((char *)cp + 2, 1755 (char *)&to->to_tsval, sizeof(to->to_tsval)); 1756 NTOHL(to->to_tsval); 1757 bcopy((char *)cp + 6, 1758 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 1759 NTOHL(to->to_tsecr); 1760 1761 /* 1762 * A timestamp received in a SYN makes 1763 * it ok to send timestamp requests and replies. 1764 */ 1765 if (ti->ti_flags & TH_SYN) { 1766 tp->t_flags |= TF_RCVD_TSTMP; 1767 tp->ts_recent = to->to_tsval; 1768 tp->ts_recent_age = tcp_now; 1769 } 1770 break; 1771 case TCPOPT_CC: 1772 if (optlen != TCPOLEN_CC) 1773 continue; 1774 to->to_flag |= TOF_CC; 1775 bcopy((char *)cp + 2, 1776 (char *)&to->to_cc, sizeof(to->to_cc)); 1777 NTOHL(to->to_cc); 1778 /* 1779 * A CC or CC.new option received in a SYN makes 1780 * it ok to send CC in subsequent segments. 1781 */ 1782 if (ti->ti_flags & TH_SYN) 1783 tp->t_flags |= TF_RCVD_CC; 1784 break; 1785 case TCPOPT_CCNEW: 1786 if (optlen != TCPOLEN_CC) 1787 continue; 1788 if (!(ti->ti_flags & TH_SYN)) 1789 continue; 1790 to->to_flag |= TOF_CCNEW; 1791 bcopy((char *)cp + 2, 1792 (char *)&to->to_cc, sizeof(to->to_cc)); 1793 NTOHL(to->to_cc); 1794 /* 1795 * A CC or CC.new option received in a SYN makes 1796 * it ok to send CC in subsequent segments. 1797 */ 1798 tp->t_flags |= TF_RCVD_CC; 1799 break; 1800 case TCPOPT_CCECHO: 1801 if (optlen != TCPOLEN_CC) 1802 continue; 1803 if (!(ti->ti_flags & TH_SYN)) 1804 continue; 1805 to->to_flag |= TOF_CCECHO; 1806 bcopy((char *)cp + 2, 1807 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 1808 NTOHL(to->to_ccecho); 1809 break; 1810 } 1811 } 1812 if (ti->ti_flags & TH_SYN) 1813 tcp_mss(tp, mss); /* sets t_maxseg */ 1814} 1815 1816/* 1817 * Pull out of band byte out of a segment so 1818 * it doesn't appear in the user's data queue. 1819 * It is still reflected in the segment length for 1820 * sequencing purposes. 1821 */ 1822static void 1823tcp_pulloutofband(so, ti, m) 1824 struct socket *so; 1825 struct tcpiphdr *ti; 1826 register struct mbuf *m; 1827{ 1828 int cnt = ti->ti_urp - 1; 1829 1830 while (cnt >= 0) { 1831 if (m->m_len > cnt) { 1832 char *cp = mtod(m, caddr_t) + cnt; 1833 struct tcpcb *tp = sototcpcb(so); 1834 1835 tp->t_iobc = *cp; 1836 tp->t_oobflags |= TCPOOB_HAVEDATA; 1837 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1838 m->m_len--; 1839 return; 1840 } 1841 cnt -= m->m_len; 1842 m = m->m_next; 1843 if (m == 0) 1844 break; 1845 } 1846 panic("tcp_pulloutofband"); 1847} 1848 1849/* 1850 * Collect new round-trip time estimate 1851 * and update averages and current timeout. 1852 */ 1853static void 1854tcp_xmit_timer(tp, rtt) 1855 register struct tcpcb *tp; 1856 short rtt; 1857{ 1858 register int delta; 1859 1860 tcpstat.tcps_rttupdated++; 1861 tp->t_rttupdated++; 1862 if (tp->t_srtt != 0) { 1863 /* 1864 * srtt is stored as fixed point with 5 bits after the 1865 * binary point (i.e., scaled by 8). The following magic 1866 * is equivalent to the smoothing algorithm in rfc793 with 1867 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 1868 * point). Adjust rtt to origin 0. 1869 */ 1870 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 1871 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 1872 1873 if ((tp->t_srtt += delta) <= 0) 1874 tp->t_srtt = 1; 1875 1876 /* 1877 * We accumulate a smoothed rtt variance (actually, a 1878 * smoothed mean difference), then set the retransmit 1879 * timer to smoothed rtt + 4 times the smoothed variance. 1880 * rttvar is stored as fixed point with 4 bits after the 1881 * binary point (scaled by 16). The following is 1882 * equivalent to rfc793 smoothing with an alpha of .75 1883 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 1884 * rfc793's wired-in beta. 1885 */ 1886 if (delta < 0) 1887 delta = -delta; 1888 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 1889 if ((tp->t_rttvar += delta) <= 0) 1890 tp->t_rttvar = 1; 1891 } else { 1892 /* 1893 * No rtt measurement yet - use the unsmoothed rtt. 1894 * Set the variance to half the rtt (so our first 1895 * retransmit happens at 3*rtt). 1896 */ 1897 tp->t_srtt = rtt << TCP_RTT_SHIFT; 1898 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 1899 } 1900 tp->t_rtt = 0; 1901 tp->t_rxtshift = 0; 1902 1903 /* 1904 * the retransmit should happen at rtt + 4 * rttvar. 1905 * Because of the way we do the smoothing, srtt and rttvar 1906 * will each average +1/2 tick of bias. When we compute 1907 * the retransmit timer, we want 1/2 tick of rounding and 1908 * 1 extra tick because of +-1/2 tick uncertainty in the 1909 * firing of the timer. The bias will give us exactly the 1910 * 1.5 tick we need. But, because the bias is 1911 * statistical, we have to test that we don't drop below 1912 * the minimum feasible timer (which is 2 ticks). 1913 */ 1914 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 1915 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 1916 1917 /* 1918 * We received an ack for a packet that wasn't retransmitted; 1919 * it is probably safe to discard any error indications we've 1920 * received recently. This isn't quite right, but close enough 1921 * for now (a route might have failed after we sent a segment, 1922 * and the return path might not be symmetrical). 1923 */ 1924 tp->t_softerror = 0; 1925} 1926 1927/* 1928 * Determine a reasonable value for maxseg size. 1929 * If the route is known, check route for mtu. 1930 * If none, use an mss that can be handled on the outgoing 1931 * interface without forcing IP to fragment; if bigger than 1932 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 1933 * to utilize large mbufs. If no route is found, route has no mtu, 1934 * or the destination isn't local, use a default, hopefully conservative 1935 * size (usually 512 or the default IP max size, but no more than the mtu 1936 * of the interface), as we can't discover anything about intervening 1937 * gateways or networks. We also initialize the congestion/slow start 1938 * window to be a single segment if the destination isn't local. 1939 * While looking at the routing entry, we also initialize other path-dependent 1940 * parameters from pre-set or cached values in the routing entry. 1941 * 1942 * Also take into account the space needed for options that we 1943 * send regularly. Make maxseg shorter by that amount to assure 1944 * that we can send maxseg amount of data even when the options 1945 * are present. Store the upper limit of the length of options plus 1946 * data in maxopd. 1947 * 1948 * NOTE that this routine is only called when we process an incoming 1949 * segment, for outgoing segments only tcp_mssopt is called. 1950 * 1951 * In case of T/TCP, we call this routine during implicit connection 1952 * setup as well (offer = -1), to initialize maxseg from the cached 1953 * MSS of our peer. 1954 */ 1955void 1956tcp_mss(tp, offer) 1957 struct tcpcb *tp; 1958 int offer; 1959{ 1960 register struct rtentry *rt; 1961 struct ifnet *ifp; 1962 register int rtt, mss; 1963 u_long bufsize; 1964 struct inpcb *inp; 1965 struct socket *so; 1966 struct rmxp_tao *taop; 1967 int origoffer = offer; 1968 1969 inp = tp->t_inpcb; 1970 if ((rt = tcp_rtlookup(inp)) == NULL) { 1971 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 1972 return; 1973 } 1974 ifp = rt->rt_ifp; 1975 so = inp->inp_socket; 1976 1977 taop = rmx_taop(rt->rt_rmx); 1978 /* 1979 * Offer == -1 means that we didn't receive SYN yet, 1980 * use cached value in that case; 1981 */ 1982 if (offer == -1) 1983 offer = taop->tao_mssopt; 1984 /* 1985 * Offer == 0 means that there was no MSS on the SYN segment, 1986 * in this case we use tcp_mssdflt. 1987 */ 1988 if (offer == 0) 1989 offer = tcp_mssdflt; 1990 else 1991 /* 1992 * Sanity check: make sure that maxopd will be large 1993 * enough to allow some data on segments even is the 1994 * all the option space is used (40bytes). Otherwise 1995 * funny things may happen in tcp_output. 1996 */ 1997 offer = max(offer, 64); 1998 taop->tao_mssopt = offer; 1999 2000 /* 2001 * While we're here, check if there's an initial rtt 2002 * or rttvar. Convert from the route-table units 2003 * to scaled multiples of the slow timeout timer. 2004 */ 2005 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2006 /* 2007 * XXX the lock bit for RTT indicates that the value 2008 * is also a minimum value; this is subject to time. 2009 */ 2010 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2011 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 2012 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 2013 tcpstat.tcps_usedrtt++; 2014 if (rt->rt_rmx.rmx_rttvar) { 2015 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2016 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 2017 tcpstat.tcps_usedrttvar++; 2018 } else { 2019 /* default variation is +- 1 rtt */ 2020 tp->t_rttvar = 2021 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2022 } 2023 TCPT_RANGESET(tp->t_rxtcur, 2024 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2025 tp->t_rttmin, TCPTV_REXMTMAX); 2026 } 2027 /* 2028 * if there's an mtu associated with the route, use it 2029 */ 2030 if (rt->rt_rmx.rmx_mtu) 2031 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 2032 else 2033 { 2034 mss = ifp->if_mtu - sizeof(struct tcpiphdr); 2035 if (!in_localaddr(inp->inp_faddr)) 2036 mss = min(mss, tcp_mssdflt); 2037 } 2038 mss = min(mss, offer); 2039 /* 2040 * maxopd stores the maximum length of data AND options 2041 * in a segment; maxseg is the amount of data in a normal 2042 * segment. We need to store this value (maxopd) apart 2043 * from maxseg, because now every segment carries options 2044 * and thus we normally have somewhat less data in segments. 2045 */ 2046 tp->t_maxopd = mss; 2047 2048 /* 2049 * In case of T/TCP, origoffer==-1 indicates, that no segments 2050 * were received yet. In this case we just guess, otherwise 2051 * we do the same as before T/TCP. 2052 */ 2053 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2054 (origoffer == -1 || 2055 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2056 mss -= TCPOLEN_TSTAMP_APPA; 2057 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2058 (origoffer == -1 || 2059 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2060 mss -= TCPOLEN_CC_APPA; 2061 2062#if (MCLBYTES & (MCLBYTES - 1)) == 0 2063 if (mss > MCLBYTES) 2064 mss &= ~(MCLBYTES-1); 2065#else 2066 if (mss > MCLBYTES) 2067 mss = mss / MCLBYTES * MCLBYTES; 2068#endif 2069 /* 2070 * If there's a pipesize, change the socket buffer 2071 * to that size. Make the socket buffers an integral 2072 * number of mss units; if the mss is larger than 2073 * the socket buffer, decrease the mss. 2074 */ 2075#ifdef RTV_SPIPE 2076 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2077#endif 2078 bufsize = so->so_snd.sb_hiwat; 2079 if (bufsize < mss) 2080 mss = bufsize; 2081 else { 2082 bufsize = roundup(bufsize, mss); 2083 if (bufsize > sb_max) 2084 bufsize = sb_max; 2085 (void)sbreserve(&so->so_snd, bufsize); 2086 } 2087 tp->t_maxseg = mss; 2088 2089#ifdef RTV_RPIPE 2090 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2091#endif 2092 bufsize = so->so_rcv.sb_hiwat; 2093 if (bufsize > mss) { 2094 bufsize = roundup(bufsize, mss); 2095 if (bufsize > sb_max) 2096 bufsize = sb_max; 2097 (void)sbreserve(&so->so_rcv, bufsize); 2098 } 2099 /* 2100 * Don't force slow-start on local network. 2101 */ 2102 if (!in_localaddr(inp->inp_faddr)) 2103 tp->snd_cwnd = mss; 2104 2105 if (rt->rt_rmx.rmx_ssthresh) { 2106 /* 2107 * There's some sort of gateway or interface 2108 * buffer limit on the path. Use this to set 2109 * the slow start threshhold, but set the 2110 * threshold to no less than 2*mss. 2111 */ 2112 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2113 tcpstat.tcps_usedssthresh++; 2114 } 2115} 2116 2117/* 2118 * Determine the MSS option to send on an outgoing SYN. 2119 */ 2120int 2121tcp_mssopt(tp) 2122 struct tcpcb *tp; 2123{ 2124 struct rtentry *rt; 2125 2126 rt = tcp_rtlookup(tp->t_inpcb); 2127 if (rt == NULL) 2128 return tcp_mssdflt; 2129 2130 return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr); 2131} 2132#endif /* TUBA_INCLUDE */
|