1 /* tcp_input.c 1.49 82/01/17 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/mbuf.h" 6 #include "../h/protosw.h" 7 #include "../h/socket.h" 8 #include "../h/socketvar.h" 9 #include "../net/in.h" 10 #include "../net/in_pcb.h" 11 #include "../net/in_systm.h" 12 #include "../net/if.h" 13 #include "../net/ip.h" 14 #include "../net/ip_var.h" 15 #include "../net/tcp.h" 16 #include "../net/tcp_fsm.h" 17 #include "../net/tcp_seq.h" 18 #include "../net/tcp_timer.h" 19 #include "../net/tcp_var.h" 20 #include "../net/tcpip.h" 21 #include "../net/tcp_debug.h" 22 #include "../errno.h" 23 24 int tcpprintfs = 0; 25 int tcpcksum = 1; 26 struct sockaddr_in tcp_in = { AF_INET }; 27 struct tcpiphdr tcp_saveti; 28 extern tcpnodelack; 29 30 struct tcpcb *tcp_newtcpcb(); 31 /* 32 * TCP input routine, follows pages 65-76 of the 33 * protocol specification dated September, 1981 very closely. 34 */ 35 tcp_input(m0) 36 struct mbuf *m0; 37 { 38 register struct tcpiphdr *ti; 39 struct inpcb *inp; 40 register struct mbuf *m; 41 struct mbuf *om = 0; 42 int len, tlen, off; 43 register struct tcpcb *tp = 0; 44 register int tiflags; 45 struct socket *so; 46 int todrop, acked; 47 short ostate; 48 49 COUNT(TCP_INPUT); 50 /* 51 * Get IP and TCP header together in first mbuf. 52 * Note: IP leaves IP header in first mbuf. 53 */ 54 m = m0; 55 ti = mtod(m, struct tcpiphdr *); 56 if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 57 ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 58 if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 59 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 60 tcpstat.tcps_hdrops++; 61 return; 62 } 63 ti = mtod(m, struct tcpiphdr *); 64 } 65 66 /* 67 * Checksum extended TCP header and data. 68 */ 69 tlen = ((struct ip *)ti)->ip_len; 70 len = sizeof (struct ip) + tlen; 71 if (tcpcksum) { 72 ti->ti_next = ti->ti_prev = 0; 73 ti->ti_x1 = 0; 74 ti->ti_len = (u_short)tlen; 75 #if vax 76 ti->ti_len = htons(ti->ti_len); 77 #endif 78 if (ti->ti_sum = in_cksum(m, len)) { 79 tcpstat.tcps_badsum++; 80 printf("tcp cksum %x\n", ti->ti_sum); 81 goto drop; 82 } 83 } 84 85 /* 86 * Check that TCP offset makes sense, 87 * pull out TCP options and adjust length. 88 */ 89 off = ti->ti_off << 2; 90 if (off < sizeof (struct tcphdr) || off > tlen) { 91 tcpstat.tcps_badoff++; 92 goto drop; 93 } 94 ti->ti_len = tlen - off; 95 if (off > sizeof (struct tcphdr)) { 96 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 97 tcpstat.tcps_hdrops++; 98 goto drop; 99 } 100 ti = mtod(m, struct tcpiphdr *); 101 om = m_get(M_DONTWAIT); 102 if (om == 0) 103 goto drop; 104 om->m_off = MMINOFF; 105 om->m_len = off - sizeof (struct tcphdr); 106 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 107 bcopy(op, mtod(om, caddr_t), om->m_len); 108 m->m_len -= om->m_len; 109 bcopy(op+om->m_len, op, m->m_len-sizeof (struct tcpiphdr)); 110 } 111 } 112 tiflags = ti->ti_flags; 113 114 #if vax 115 /* 116 * Convert TCP protocol specific fields to host format. 117 */ 118 ti->ti_seq = ntohl(ti->ti_seq); 119 ti->ti_ack = ntohl(ti->ti_ack); 120 ti->ti_win = ntohs(ti->ti_win); 121 ti->ti_urp = ntohs(ti->ti_urp); 122 #endif 123 124 /* 125 * Locate pcb for segment. 126 */ 127 inp = in_pcblookup 128 (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 129 130 /* 131 * If the state is CLOSED (i.e., TCB does not exist) then 132 * all data in the incoming segment is discarded. 133 */ 134 if (inp == 0) 135 goto dropwithreset; 136 tp = intotcpcb(inp); 137 if (tp == 0) 138 goto dropwithreset; 139 so = inp->inp_socket; 140 if (so->so_options & SO_DEBUG) { 141 ostate = tp->t_state; 142 tcp_saveti = *ti; 143 } 144 145 /* 146 * Segment received on connection. 147 * Reset idle time and keep-alive timer. 148 */ 149 tp->t_idle = 0; 150 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 151 152 /* 153 * Process options. 154 */ 155 if (om) { 156 tcp_dooptions(tp, om); 157 om = 0; 158 } 159 160 /* 161 * Calculate amount of space in receive window, 162 * and then do TCP input processing. 163 */ 164 tp->rcv_wnd = sbspace(&so->so_rcv); 165 if (tp->rcv_wnd < 0) 166 tp->rcv_wnd = 0; 167 168 switch (tp->t_state) { 169 170 /* 171 * If the state is LISTEN then ignore segment if it contains an RST. 172 * If the segment contains an ACK then it is bad and send a RST. 173 * If it does not contain a SYN then it is not interesting; drop it. 174 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 175 * tp->iss, and send a segment: 176 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 177 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 178 * Fill in remote peer address fields if not previously specified. 179 * Enter SYN_RECEIVED state, and process any other fields of this 180 * segment in this state. 181 */ 182 case TCPS_LISTEN: 183 if (tiflags & TH_RST) 184 goto drop; 185 if (tiflags & TH_ACK) 186 goto dropwithreset; 187 if ((tiflags & TH_SYN) == 0) 188 goto drop; 189 tcp_in.sin_addr = ti->ti_src; 190 tcp_in.sin_port = ti->ti_sport; 191 if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) 192 goto drop; 193 tp->t_template = tcp_template(tp); 194 if (tp->t_template == 0) { 195 in_pcbdisconnect(inp); 196 goto drop; 197 } 198 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 199 tp->irs = ti->ti_seq; 200 tcp_sendseqinit(tp); 201 tcp_rcvseqinit(tp); 202 tp->t_state = TCPS_SYN_RECEIVED; 203 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 204 goto trimthenstep6; 205 206 /* 207 * If the state is SYN_SENT: 208 * if seg contains an ACK, but not for our SYN, drop the input. 209 * if seg contains a RST, then drop the connection. 210 * if seg does not contain SYN, then drop it. 211 * Otherwise this is an acceptable SYN segment 212 * initialize tp->rcv_nxt and tp->irs 213 * if seg contains ack then advance tp->snd_una 214 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 215 * arrange for segment to be acked (eventually) 216 * continue processing rest of data/controls, beginning with URG 217 */ 218 case TCPS_SYN_SENT: 219 if ((tiflags & TH_ACK) && 220 /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 221 (SEQ_LT(ti->ti_ack, tp->iss) || 222 SEQ_GT(ti->ti_ack, tp->snd_max))) 223 goto dropwithreset; 224 if (tiflags & TH_RST) { 225 if (tiflags & TH_ACK) 226 tcp_drop(tp, ECONNREFUSED); 227 goto drop; 228 } 229 if ((tiflags & TH_SYN) == 0) 230 goto drop; 231 tp->snd_una = ti->ti_ack; 232 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 233 tp->snd_nxt = tp->snd_una; 234 tp->t_timer[TCPT_REXMT] = 0; 235 tp->irs = ti->ti_seq; 236 tcp_rcvseqinit(tp); 237 tp->t_flags |= TF_ACKNOW; 238 if (SEQ_GT(tp->snd_una, tp->iss)) { 239 if (so->so_options & SO_ACCEPTCONN) 240 so->so_state |= SS_CONNAWAITING; 241 soisconnected(so); 242 tp->t_state = TCPS_ESTABLISHED; 243 (void) tcp_reass(tp, (struct tcpiphdr *)0); 244 } else 245 tp->t_state = TCPS_SYN_RECEIVED; 246 goto trimthenstep6; 247 248 trimthenstep6: 249 /* 250 * Advance ti->ti_seq to correspond to first data byte. 251 * If data, trim to stay within window, 252 * dropping FIN if necessary. 253 */ 254 ti->ti_seq++; 255 if (ti->ti_len > tp->rcv_wnd) { 256 todrop = ti->ti_len - tp->rcv_wnd; 257 m_adj(m, -todrop); 258 ti->ti_len = tp->rcv_wnd; 259 ti->ti_flags &= ~TH_FIN; 260 } 261 tp->snd_wl1 = ti->ti_seq - 1; 262 goto step6; 263 } 264 265 /* 266 * States other than LISTEN or SYN_SENT. 267 * First check that at least some bytes of segment are within 268 * receive window. 269 */ 270 if (tp->rcv_wnd == 0) { 271 /* 272 * If window is closed can only take segments at 273 * window edge, and have to drop data and PUSH from 274 * incoming segments. 275 */ 276 if (tp->rcv_nxt != ti->ti_seq) 277 goto dropafterack; 278 if (ti->ti_len > 0) { 279 ti->ti_len = 0; 280 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 281 } 282 } else { 283 /* 284 * If segment begins before rcv_nxt, drop leading 285 * data (and SYN); if nothing left, just ack. 286 */ 287 if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 288 todrop = tp->rcv_nxt - ti->ti_seq; 289 if (tiflags & TH_SYN) { 290 tiflags &= ~TH_SYN; 291 ti->ti_seq++; 292 if (ti->ti_urp > 1) 293 ti->ti_urp--; 294 else 295 tiflags &= ~TH_URG; 296 todrop--; 297 } 298 if (todrop > ti->ti_len) 299 goto dropafterack; 300 m_adj(m, todrop); 301 ti->ti_seq += todrop; 302 ti->ti_len -= todrop; 303 if (ti->ti_urp > todrop) 304 ti->ti_urp -= todrop; 305 else { 306 tiflags &= ~TH_URG; 307 /* ti->ti_flags &= ~TH_URG; */ 308 /* ti->ti_urp = 0; */ 309 } 310 /* tiflags &= ~TH_SYN; */ 311 /* ti->ti_flags &= ~TH_SYN; */ 312 } 313 /* 314 * If segment ends after window, drop trailing data 315 * (and PUSH and FIN); if nothing left, just ACK. 316 */ 317 if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 318 todrop = 319 ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 320 if (todrop > ti->ti_len) 321 goto dropafterack; 322 m_adj(m, -todrop); 323 ti->ti_len -= todrop; 324 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 325 } 326 } 327 328 /* 329 * If the RST bit is set examine the state: 330 * SYN_RECEIVED STATE: 331 * If passive open, return to LISTEN state. 332 * If active open, inform user that connection was refused. 333 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 334 * Inform user that connection was reset, and close tcb. 335 * CLOSING, LAST_ACK, TIME_WAIT STATES 336 * Close the tcb. 337 */ 338 if (tiflags&TH_RST) switch (tp->t_state) { 339 340 case TCPS_SYN_RECEIVED: 341 if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 342 /* a miniature tcp_close, but invisible to user */ 343 (void) m_free(dtom(tp->t_template)); 344 (void) m_free(dtom(tp)); 345 inp->inp_ppcb = 0; 346 tp = tcp_newtcpcb(inp); 347 tp->t_state = TCPS_LISTEN; 348 goto drop; 349 } 350 tcp_drop(tp, ECONNREFUSED); 351 goto drop; 352 353 case TCPS_ESTABLISHED: 354 case TCPS_FIN_WAIT_1: 355 case TCPS_FIN_WAIT_2: 356 case TCPS_CLOSE_WAIT: 357 tcp_drop(tp, ECONNRESET); 358 goto drop; 359 360 case TCPS_CLOSING: 361 case TCPS_LAST_ACK: 362 case TCPS_TIME_WAIT: 363 tcp_close(tp); 364 goto drop; 365 } 366 367 /* 368 * If a SYN is in the window, then this is an 369 * error and we send an RST and drop the connection. 370 */ 371 if (tiflags & TH_SYN) { 372 tcp_drop(tp, ECONNRESET); 373 goto dropwithreset; 374 } 375 376 /* 377 * If the ACK bit is off we drop the segment and return. 378 */ 379 if ((tiflags & TH_ACK) == 0) 380 goto drop; 381 382 /* 383 * Ack processing. 384 */ 385 switch (tp->t_state) { 386 387 /* 388 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 389 * ESTABLISHED state and continue processing, othewise 390 * send an RST. 391 */ 392 case TCPS_SYN_RECEIVED: 393 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 394 SEQ_GT(ti->ti_ack, tp->snd_max)) 395 goto dropwithreset; 396 tp->snd_una++; /* SYN acked */ 397 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 398 tp->snd_nxt = tp->snd_una; 399 tp->t_timer[TCPT_REXMT] = 0; 400 if (so->so_options & SO_ACCEPTCONN) 401 so->so_state |= SS_CONNAWAITING; 402 soisconnected(so); 403 tp->t_state = TCPS_ESTABLISHED; 404 (void) tcp_reass(tp, (struct tcpiphdr *)0); 405 tp->snd_wl1 = ti->ti_seq - 1; 406 /* fall into ... */ 407 408 /* 409 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 410 * ACKs. If the ack is in the range 411 * tp->snd_una < ti->ti_ack <= tp->snd_max 412 * then advance tp->snd_una to ti->ti_ack and drop 413 * data from the retransmission queue. If this ACK reflects 414 * more up to date window information we update our window information. 415 */ 416 case TCPS_ESTABLISHED: 417 case TCPS_FIN_WAIT_1: 418 case TCPS_FIN_WAIT_2: 419 case TCPS_CLOSE_WAIT: 420 case TCPS_CLOSING: 421 case TCPS_LAST_ACK: 422 case TCPS_TIME_WAIT: 423 #define ourfinisacked (acked > 0) 424 425 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 426 break; 427 if (SEQ_GT(ti->ti_ack, tp->snd_max)) 428 goto dropafterack; 429 acked = ti->ti_ack - tp->snd_una; 430 if (ti->ti_ack == tp->snd_max) 431 tp->t_timer[TCPT_REXMT] = 0; 432 else { 433 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 434 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 435 tp->t_rtt = 0; 436 tp->t_rxtshift = 0; 437 } 438 if (acked > so->so_snd.sb_cc) { 439 sbdrop(&so->so_snd, so->so_snd.sb_cc); 440 tp->snd_wnd -= so->so_snd.sb_cc; 441 } else { 442 sbdrop(&so->so_snd.sb_cc, acked); 443 tp->snd_wnd -= acked; 444 acked = 0; 445 } 446 if (so->so_snd.sb_flags & SB_WAIT) 447 sowwakeup(so); 448 tp->snd_una = ti->ti_ack; 449 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 450 tp->snd_nxt = tp->snd_una; 451 452 /* 453 * If transmit timer is running and timed sequence 454 * number was acked, update smoothed round trip time. 455 */ 456 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 457 if (tp->t_srtt == 0) 458 tp->t_srtt = tp->t_rtt; 459 else 460 tp->t_srtt = 461 tcp_alpha * tp->t_srtt + 462 (1 - tcp_alpha) * tp->t_rtt; 463 tp->t_rtt = 0; 464 } 465 466 switch (tp->t_state) { 467 468 /* 469 * In FIN_WAIT_1 STATE in addition to the processing 470 * for the ESTABLISHED state if our FIN is now acknowledged 471 * then enter FIN_WAIT_2. 472 */ 473 case TCPS_FIN_WAIT_1: 474 if (ourfinisacked) 475 tp->t_state = TCPS_FIN_WAIT_2; 476 break; 477 478 /* 479 * In CLOSING STATE in addition to the processing for 480 * the ESTABLISHED state if the ACK acknowledges our FIN 481 * then enter the TIME-WAIT state, otherwise ignore 482 * the segment. 483 */ 484 case TCPS_CLOSING: 485 if (ourfinisacked) { 486 tp->t_state = TCPS_TIME_WAIT; 487 tcp_canceltimers(tp); 488 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 489 soisdisconnected(so); 490 } 491 break; 492 493 /* 494 * The only thing that can arrive in LAST_ACK state 495 * is an acknowledgment of our FIN. If our FIN is now 496 * acknowledged, delete the TCB, enter the closed state 497 * and return. 498 */ 499 case TCPS_LAST_ACK: 500 if (ourfinisacked) 501 tcp_close(tp); 502 goto drop; 503 504 /* 505 * In TIME_WAIT state the only thing that should arrive 506 * is a retransmission of the remote FIN. Acknowledge 507 * it and restart the finack timer. 508 */ 509 case TCPS_TIME_WAIT: 510 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 511 goto dropafterack; 512 } 513 #undef ourfinisacked 514 } 515 516 step6: 517 /* 518 * Update window information. 519 */ 520 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 521 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 522 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 523 /* 524 printf("wl1 %x seq %x wl2 %x ack %x win %x wnd %x\n", tp->snd_wl1, ti->ti_seq, tp->snd_wl2, ti->ti_ack, ti->ti_win, tp->snd_wnd); 525 */ 526 tp->snd_wnd = ti->ti_win; 527 tp->snd_wl1 = ti->ti_seq; 528 tp->snd_wl2 = ti->ti_ack; 529 if (tp->snd_wnd > 0) 530 tp->t_timer[TCPT_PERSIST] = 0; 531 } 532 533 /* 534 * If an URG bit is set and in the segment and is greater than the 535 * current known urgent pointer, then mark the data stream. 536 * If the TCP is not doing out-of-band data, then indicate 537 * urgent to the user. This should not happen 538 * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 539 * a FIN has been received from the remote side. In these states 540 * we ignore the URG. 541 */ 542 if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0 && 543 SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 544 tp->rcv_up = ti->ti_seq + ti->ti_urp; 545 so->so_oobmark = so->so_rcv.sb_cc + 546 (tp->rcv_up - tp->rcv_nxt) - 1; 547 if (so->so_oobmark == 0) 548 so->so_state |= SS_RCVATMARK; 549 #ifdef TCPTRUEOOB 550 if ((tp->t_flags & TF_DOOOB) == 0) 551 #endif 552 { 553 sohasoutofband(so); 554 tp->t_oobflags |= TCPOOB_HAVEDATA; 555 } 556 557 } 558 559 /* 560 * Process the segment text, merging it into the TCP sequencing queue, 561 * and arranging for acknowledgment of receipt if necessary. 562 * This process logically involves adjusting tp->rcv_wnd as data 563 * is presented to the user (this happens in tcp_usrreq.c, 564 * case PRU_RCVD). If a FIN has already been received on this 565 * connection then we just ignore the text. 566 */ 567 if ((ti->ti_len || (tiflags&TH_FIN)) && 568 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 569 off += sizeof (struct ip); /* drop IP header */ 570 m->m_off += off; 571 m->m_len -= off; 572 tiflags = tcp_reass(tp, ti); 573 if (tcpnodelack == 0) 574 tp->t_flags |= TF_DELACK; 575 else 576 tp->t_flags |= TF_ACKNOW; 577 } else { 578 m_freem(m); 579 tiflags &= ~TH_FIN; 580 } 581 582 /* 583 * If FIN is received ACK the FIN and let the user know 584 * that the connection is closing. 585 */ 586 if (tiflags & TH_FIN) { 587 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 588 socantrcvmore(so); 589 tp->t_flags |= TF_ACKNOW; 590 tp->rcv_nxt++; 591 } 592 switch (tp->t_state) { 593 594 /* 595 * In SYN_RECEIVED and ESTABLISHED STATES 596 * enter the CLOSE_WAIT state. 597 */ 598 case TCPS_SYN_RECEIVED: 599 case TCPS_ESTABLISHED: 600 tp->t_state = TCPS_CLOSE_WAIT; 601 break; 602 603 /* 604 * If still in FIN_WAIT_1 STATE FIN has not been acked so 605 * enter the CLOSING state. 606 */ 607 case TCPS_FIN_WAIT_1: 608 tp->t_state = TCPS_CLOSING; 609 break; 610 611 /* 612 * In FIN_WAIT_2 state enter the TIME_WAIT state, 613 * starting the time-wait timer, turning off the other 614 * standard timers. 615 */ 616 case TCPS_FIN_WAIT_2: 617 tp->t_state = TCPS_TIME_WAIT; 618 tcp_canceltimers(tp); 619 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 620 soisdisconnected(so); 621 break; 622 623 /* 624 * In TIME_WAIT state restart the 2 MSL time_wait timer. 625 */ 626 case TCPS_TIME_WAIT: 627 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 628 break; 629 } 630 } 631 if (so->so_options & SO_DEBUG) 632 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 633 634 /* 635 * Return any desired output. 636 */ 637 tcp_output(tp); 638 return; 639 640 dropafterack: 641 /* 642 * Generate an ACK dropping incoming segment. 643 * Make ACK reflect our state. 644 */ 645 if (tiflags & TH_RST) 646 goto drop; 647 tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 648 return; 649 650 dropwithreset: 651 if (om) 652 m_free(om); 653 /* 654 * Generate a RST, dropping incoming segment. 655 * Make ACK acceptable to originator of segment. 656 */ 657 if (tiflags & TH_RST) 658 goto drop; 659 if (tiflags & TH_ACK) 660 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 661 else { 662 if (tiflags & TH_SYN) 663 ti->ti_len++; 664 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 665 } 666 return; 667 668 drop: 669 /* 670 * Drop space held by incoming segment and return. 671 */ 672 m_freem(m); 673 return; 674 } 675 676 tcp_dooptions(tp, om) 677 struct tcpcb *tp; 678 struct mbuf *om; 679 { 680 register u_char *cp; 681 int opt, optlen, cnt; 682 683 cp = mtod(om, u_char *); 684 cnt = om->m_len; 685 for (; cnt > 0; cnt -= optlen, cp += optlen) { 686 opt = cp[0]; 687 if (opt == TCPOPT_EOL) 688 break; 689 if (opt == TCPOPT_NOP) 690 optlen = 1; 691 else 692 optlen = cp[1]; 693 switch (opt) { 694 695 default: 696 break; 697 698 case TCPOPT_MAXSEG: 699 if (optlen != 4) 700 continue; 701 tp->t_maxseg = *(u_short *)(cp + 2); 702 #if vax 703 tp->t_maxseg = ntohs(tp->t_maxseg); 704 #endif 705 break; 706 707 #ifdef TCPTRUEOOB 708 case TCPOPT_WILLOOB: 709 tp->t_flags |= TF_DOOOB; 710 printf("tp %x dooob\n", tp); 711 break; 712 713 case TCPOPT_OOBDATA: { 714 int seq; 715 716 if (optlen != 4) 717 continue; 718 seq = cp[2]; 719 if (seq < tp->t_iobseq) 720 seq += 256; 721 printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 722 if (seq - tp->t_iobseq > 128) { 723 printf("bad seq\n"); 724 tp->t_oobflags |= TCPOOB_OWEACK; 725 break; 726 } 727 tp->t_iobseq = cp[2]; 728 tp->t_iobc = cp[3]; 729 printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 730 sohasoutofband(tp->t_inpcb->inp_socket); 731 break; 732 } 733 734 case TCPOPT_OOBACK: { 735 int seq; 736 737 if (optlen != 4) 738 continue; 739 if (tp->t_oobseq != cp[2]) { 740 printf("wrong ack\n"); 741 break; 742 } 743 printf("take oob ack %x and cancel rexmt\n", cp[2]); 744 tp->t_oobflags &= ~TCPOOB_NEEDACK; 745 tp->t_timer[TCPT_OOBREXMT] = 0; 746 break; 747 } 748 #endif TCPTRUEOOB 749 } 750 } 751 m_free(om); 752 } 753 754 /* 755 * Insert segment ti into reassembly queue of tcp with 756 * control block tp. Return TH_FIN if reassembly now includes 757 * a segment with FIN. 758 */ 759 tcp_reass(tp, ti) 760 register struct tcpcb *tp; 761 register struct tcpiphdr *ti; 762 { 763 register struct tcpiphdr *q; 764 struct socket *so = tp->t_inpcb->inp_socket; 765 struct mbuf *m; 766 int flags; 767 COUNT(TCP_REASS); 768 769 /* 770 * Call with ti==0 after become established to 771 * force pre-ESTABLISHED data up to user socket. 772 */ 773 if (ti == 0) 774 goto present; 775 776 /* 777 * Find a segment which begins after this one does. 778 */ 779 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 780 q = (struct tcpiphdr *)q->ti_next) 781 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 782 break; 783 784 /* 785 * If there is a preceding segment, it may provide some of 786 * our data already. If so, drop the data from the incoming 787 * segment. If it provides all of our data, drop us. 788 */ 789 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 790 register int i; 791 q = (struct tcpiphdr *)(q->ti_prev); 792 /* conversion to int (in i) handles seq wraparound */ 793 i = q->ti_seq + q->ti_len - ti->ti_seq; 794 if (i > 0) { 795 if (i >= ti->ti_len) 796 goto drop; 797 m_adj(dtom(tp), i); 798 ti->ti_len -= i; 799 ti->ti_seq += i; 800 } 801 q = (struct tcpiphdr *)(q->ti_next); 802 } 803 804 /* 805 * While we overlap succeeding segments trim them or, 806 * if they are completely covered, dequeue them. 807 */ 808 while (q != (struct tcpiphdr *)tp && 809 SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 810 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 811 if (i < q->ti_len) { 812 q->ti_len -= i; 813 m_adj(dtom(q), i); 814 break; 815 } 816 q = (struct tcpiphdr *)q->ti_next; 817 m_freem(dtom(q->ti_prev)); 818 remque(q->ti_prev); 819 } 820 821 /* 822 * Stick new segment in its place. 823 */ 824 insque(ti, q->ti_prev); 825 826 present: 827 /* 828 * Present data to user, advancing rcv_nxt through 829 * completed sequence space. 830 */ 831 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 832 return (0); 833 ti = tp->seg_next; 834 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 835 return (0); 836 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 837 return (0); 838 do { 839 tp->rcv_nxt += ti->ti_len; 840 flags = ti->ti_flags & TH_FIN; 841 remque(ti); 842 m = dtom(ti); 843 ti = (struct tcpiphdr *)ti->ti_next; 844 if (so->so_state & SS_CANTRCVMORE) 845 (void) m_freem(m); 846 else 847 sbappend(&so->so_rcv, m); 848 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 849 sorwakeup(so); 850 return (flags); 851 drop: 852 m_freem(dtom(ti)); 853 return (0); 854 } 855