1 /* tcp_input.c 1.54 82/02/25 */ 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 m_adj(m, ti->ti_len); 280 ti->ti_len = 0; 281 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 282 } 283 } else { 284 /* 285 * If segment begins before rcv_nxt, drop leading 286 * data (and SYN); if nothing left, just ack. 287 */ 288 todrop = tp->rcv_nxt - ti->ti_seq; 289 if (todrop > 0) { 290 if (tiflags & TH_SYN) { 291 tiflags &= ~TH_SYN; 292 ti->ti_flags &= ~TH_SYN; 293 ti->ti_seq++; 294 if (ti->ti_urp > 1) 295 ti->ti_urp--; 296 else 297 tiflags &= ~TH_URG; 298 todrop--; 299 } 300 if (todrop > ti->ti_len) 301 goto dropafterack; 302 m_adj(m, todrop); 303 ti->ti_seq += todrop; 304 ti->ti_len -= todrop; 305 if (ti->ti_urp > todrop) 306 ti->ti_urp -= todrop; 307 else { 308 tiflags &= ~TH_URG; 309 ti->ti_flags &= ~TH_URG; 310 ti->ti_urp = 0; 311 } 312 } 313 /* 314 * If segment ends after window, drop trailing data 315 * (and PUSH and FIN); if nothing left, just ACK. 316 */ 317 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 318 if (todrop > 0) { 319 if (todrop > ti->ti_len) 320 goto dropafterack; 321 m_adj(m, -todrop); 322 ti->ti_len -= todrop; 323 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 324 } 325 } 326 327 /* 328 * If a segment is received on a connection after the 329 * user processes are gone, then RST the other end. 330 */ 331 if (so->so_state & SS_USERGONE) { 332 tcp_close(tp); 333 goto dropwithreset; 334 } 335 336 /* 337 * If the RST bit is set examine the state: 338 * SYN_RECEIVED STATE: 339 * If passive open, return to LISTEN state. 340 * If active open, inform user that connection was refused. 341 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 342 * Inform user that connection was reset, and close tcb. 343 * CLOSING, LAST_ACK, TIME_WAIT STATES 344 * Close the tcb. 345 */ 346 if (tiflags&TH_RST) switch (tp->t_state) { 347 348 case TCPS_SYN_RECEIVED: 349 if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 350 /* a miniature tcp_close, but invisible to user */ 351 (void) m_free(dtom(tp->t_template)); 352 (void) m_free(dtom(tp)); 353 inp->inp_ppcb = 0; 354 tp = tcp_newtcpcb(inp); 355 tp->t_state = TCPS_LISTEN; 356 goto drop; 357 } 358 tcp_drop(tp, ECONNREFUSED); 359 goto drop; 360 361 case TCPS_ESTABLISHED: 362 case TCPS_FIN_WAIT_1: 363 case TCPS_FIN_WAIT_2: 364 case TCPS_CLOSE_WAIT: 365 tcp_drop(tp, ECONNRESET); 366 goto drop; 367 368 case TCPS_CLOSING: 369 case TCPS_LAST_ACK: 370 case TCPS_TIME_WAIT: 371 tcp_close(tp); 372 goto drop; 373 } 374 375 /* 376 * If a SYN is in the window, then this is an 377 * error and we send an RST and drop the connection. 378 */ 379 if (tiflags & TH_SYN) { 380 tcp_drop(tp, ECONNRESET); 381 goto dropwithreset; 382 } 383 384 /* 385 * If the ACK bit is off we drop the segment and return. 386 */ 387 if ((tiflags & TH_ACK) == 0) 388 goto drop; 389 390 /* 391 * Ack processing. 392 */ 393 switch (tp->t_state) { 394 395 /* 396 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 397 * ESTABLISHED state and continue processing, othewise 398 * send an RST. 399 */ 400 case TCPS_SYN_RECEIVED: 401 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 402 SEQ_GT(ti->ti_ack, tp->snd_max)) 403 goto dropwithreset; 404 tp->snd_una++; /* SYN acked */ 405 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 406 tp->snd_nxt = tp->snd_una; 407 tp->t_timer[TCPT_REXMT] = 0; 408 if (so->so_options & SO_ACCEPTCONN) 409 so->so_state |= SS_CONNAWAITING; 410 soisconnected(so); 411 tp->t_state = TCPS_ESTABLISHED; 412 (void) tcp_reass(tp, (struct tcpiphdr *)0); 413 tp->snd_wl1 = ti->ti_seq - 1; 414 /* fall into ... */ 415 416 /* 417 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 418 * ACKs. If the ack is in the range 419 * tp->snd_una < ti->ti_ack <= tp->snd_max 420 * then advance tp->snd_una to ti->ti_ack and drop 421 * data from the retransmission queue. If this ACK reflects 422 * more up to date window information we update our window information. 423 */ 424 case TCPS_ESTABLISHED: 425 case TCPS_FIN_WAIT_1: 426 case TCPS_FIN_WAIT_2: 427 case TCPS_CLOSE_WAIT: 428 case TCPS_CLOSING: 429 case TCPS_LAST_ACK: 430 case TCPS_TIME_WAIT: 431 #define ourfinisacked (acked > 0) 432 433 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 434 break; 435 if (SEQ_GT(ti->ti_ack, tp->snd_max)) 436 goto dropafterack; 437 acked = ti->ti_ack - tp->snd_una; 438 439 /* 440 * If transmit timer is running and timed sequence 441 * number was acked, update smoothed round trip time. 442 */ 443 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 444 if (tp->t_srtt == 0) 445 tp->t_srtt = tp->t_rtt; 446 else 447 tp->t_srtt = 448 tcp_alpha * tp->t_srtt + 449 (1 - tcp_alpha) * tp->t_rtt; 450 /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 451 tp->t_rtt = 0; 452 } 453 454 if (ti->ti_ack == tp->snd_max) 455 tp->t_timer[TCPT_REXMT] = 0; 456 else { 457 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 458 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 459 tp->t_rtt = 1; 460 tp->t_rxtshift = 0; 461 } 462 if (acked > so->so_snd.sb_cc) { 463 sbdrop(&so->so_snd, so->so_snd.sb_cc); 464 tp->snd_wnd -= so->so_snd.sb_cc; 465 } else { 466 sbdrop(&so->so_snd.sb_cc, acked); 467 tp->snd_wnd -= acked; 468 acked = 0; 469 } 470 if (so->so_snd.sb_flags & SB_WAIT) 471 sowwakeup(so); 472 tp->snd_una = ti->ti_ack; 473 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 474 tp->snd_nxt = tp->snd_una; 475 476 switch (tp->t_state) { 477 478 /* 479 * In FIN_WAIT_1 STATE in addition to the processing 480 * for the ESTABLISHED state if our FIN is now acknowledged 481 * then enter FIN_WAIT_2. 482 */ 483 case TCPS_FIN_WAIT_1: 484 if (ourfinisacked) { 485 /* 486 * If we can't receive any more 487 * data, then closing user can proceed. 488 */ 489 if (so->so_state & SS_CANTRCVMORE) 490 soisdisconnected(so); 491 tp->t_state = TCPS_FIN_WAIT_2; 492 } 493 break; 494 495 /* 496 * In CLOSING STATE in addition to the processing for 497 * the ESTABLISHED state if the ACK acknowledges our FIN 498 * then enter the TIME-WAIT state, otherwise ignore 499 * the segment. 500 */ 501 case TCPS_CLOSING: 502 if (ourfinisacked) { 503 tp->t_state = TCPS_TIME_WAIT; 504 tcp_canceltimers(tp); 505 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 506 soisdisconnected(so); 507 } 508 break; 509 510 /* 511 * The only thing that can arrive in LAST_ACK state 512 * is an acknowledgment of our FIN. If our FIN is now 513 * acknowledged, delete the TCB, enter the closed state 514 * and return. 515 */ 516 case TCPS_LAST_ACK: 517 if (ourfinisacked) 518 tcp_close(tp); 519 goto drop; 520 521 /* 522 * In TIME_WAIT state the only thing that should arrive 523 * is a retransmission of the remote FIN. Acknowledge 524 * it and restart the finack timer. 525 */ 526 case TCPS_TIME_WAIT: 527 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 528 goto dropafterack; 529 } 530 #undef ourfinisacked 531 } 532 533 step6: 534 /* 535 * Update window information. 536 */ 537 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 538 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 539 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 540 tp->snd_wnd = ti->ti_win; 541 tp->snd_wl1 = ti->ti_seq; 542 tp->snd_wl2 = ti->ti_ack; 543 if (tp->snd_wnd > 0) 544 tp->t_timer[TCPT_PERSIST] = 0; 545 } 546 547 /* 548 * Process segments with URG. 549 */ 550 if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 551 /* 552 * If this segment advances the known urgent pointer, 553 * then mark the data stream. This should not happen 554 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 555 * a FIN has been received from the remote side. 556 * In these states we ignore the URG. 557 */ 558 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 559 tp->rcv_up = ti->ti_seq + ti->ti_urp; 560 so->so_oobmark = so->so_rcv.sb_cc + 561 (tp->rcv_up - tp->rcv_nxt) - 1; 562 if (so->so_oobmark == 0) 563 so->so_state |= SS_RCVATMARK; 564 #ifdef TCPTRUEOOB 565 if ((tp->t_flags & TF_DOOOB) == 0) 566 #endif 567 sohasoutofband(so); 568 tp->t_oobflags &= ~TCPOOB_HAVEDATA; 569 } 570 /* 571 * Remove out of band data so doesn't get presented to user. 572 * This can happen independent of advancing the URG pointer, 573 * but if two URG's are pending at once, some out-of-band 574 * data may creep in... ick. 575 */ 576 if (ti->ti_urp <= ti->ti_len) { 577 tcp_pulloutofband(so, ti); 578 } 579 } 580 581 /* 582 * Process the segment text, merging it into the TCP sequencing queue, 583 * and arranging for acknowledgment of receipt if necessary. 584 * This process logically involves adjusting tp->rcv_wnd as data 585 * is presented to the user (this happens in tcp_usrreq.c, 586 * case PRU_RCVD). If a FIN has already been received on this 587 * connection then we just ignore the text. 588 */ 589 if ((ti->ti_len || (tiflags&TH_FIN)) && 590 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 591 off += sizeof (struct ip); /* drop IP header */ 592 m->m_off += off; 593 m->m_len -= off; 594 tiflags = tcp_reass(tp, ti); 595 if (tcpnodelack == 0) 596 tp->t_flags |= TF_DELACK; 597 else 598 tp->t_flags |= TF_ACKNOW; 599 } else { 600 m_freem(m); 601 tiflags &= ~TH_FIN; 602 } 603 604 /* 605 * If FIN is received ACK the FIN and let the user know 606 * that the connection is closing. 607 */ 608 if (tiflags & TH_FIN) { 609 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 610 socantrcvmore(so); 611 tp->t_flags |= TF_ACKNOW; 612 tp->rcv_nxt++; 613 } 614 switch (tp->t_state) { 615 616 /* 617 * In SYN_RECEIVED and ESTABLISHED STATES 618 * enter the CLOSE_WAIT state. 619 */ 620 case TCPS_SYN_RECEIVED: 621 case TCPS_ESTABLISHED: 622 tp->t_state = TCPS_CLOSE_WAIT; 623 break; 624 625 /* 626 * If still in FIN_WAIT_1 STATE FIN has not been acked so 627 * enter the CLOSING state. 628 */ 629 case TCPS_FIN_WAIT_1: 630 tp->t_state = TCPS_CLOSING; 631 break; 632 633 /* 634 * In FIN_WAIT_2 state enter the TIME_WAIT state, 635 * starting the time-wait timer, turning off the other 636 * standard timers. 637 */ 638 case TCPS_FIN_WAIT_2: 639 tp->t_state = TCPS_TIME_WAIT; 640 tcp_canceltimers(tp); 641 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 642 soisdisconnected(so); 643 break; 644 645 /* 646 * In TIME_WAIT state restart the 2 MSL time_wait timer. 647 */ 648 case TCPS_TIME_WAIT: 649 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 650 break; 651 } 652 } 653 if (so->so_options & SO_DEBUG) 654 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 655 656 /* 657 * Return any desired output. 658 */ 659 tcp_output(tp); 660 return; 661 662 dropafterack: 663 /* 664 * Generate an ACK dropping incoming segment. 665 * Make ACK reflect our state. 666 */ 667 if (tiflags & TH_RST) 668 goto drop; 669 tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 670 return; 671 672 dropwithreset: 673 if (om) 674 m_free(om); 675 /* 676 * Generate a RST, dropping incoming segment. 677 * Make ACK acceptable to originator of segment. 678 */ 679 if (tiflags & TH_RST) 680 goto drop; 681 if (tiflags & TH_ACK) 682 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 683 else { 684 if (tiflags & TH_SYN) 685 ti->ti_len++; 686 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 687 } 688 return; 689 690 drop: 691 /* 692 * Drop space held by incoming segment and return. 693 */ 694 m_freem(m); 695 return; 696 } 697 698 tcp_dooptions(tp, om) 699 struct tcpcb *tp; 700 struct mbuf *om; 701 { 702 register u_char *cp; 703 int opt, optlen, cnt; 704 705 cp = mtod(om, u_char *); 706 cnt = om->m_len; 707 for (; cnt > 0; cnt -= optlen, cp += optlen) { 708 opt = cp[0]; 709 if (opt == TCPOPT_EOL) 710 break; 711 if (opt == TCPOPT_NOP) 712 optlen = 1; 713 else 714 optlen = cp[1]; 715 switch (opt) { 716 717 default: 718 break; 719 720 case TCPOPT_MAXSEG: 721 if (optlen != 4) 722 continue; 723 tp->t_maxseg = *(u_short *)(cp + 2); 724 #if vax 725 tp->t_maxseg = ntohs(tp->t_maxseg); 726 #endif 727 break; 728 729 #ifdef TCPTRUEOOB 730 case TCPOPT_WILLOOB: 731 tp->t_flags |= TF_DOOOB; 732 printf("tp %x dooob\n", tp); 733 break; 734 735 case TCPOPT_OOBDATA: { 736 int seq; 737 register struct socket *so = tp->t_inpcb->inp_socket; 738 tcp_seq mark; 739 740 if (optlen != 8) 741 continue; 742 seq = cp[2]; 743 if (seq < tp->t_iobseq) 744 seq += 256; 745 printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 746 if (seq - tp->t_iobseq > 128) { 747 printf("bad seq\n"); 748 tp->t_oobflags |= TCPOOB_OWEACK; 749 break; 750 } 751 tp->t_iobseq = cp[2]; 752 tp->t_iobc = cp[3]; 753 mark = *(tcp_seq *)(cp + 4); 754 #if vax 755 mark = ntohl(mark); 756 #endif 757 so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 758 if (so->so_oobmark == 0) 759 so->so_state |= SS_RCVATMARK; 760 printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 761 sohasoutofband(so); 762 break; 763 } 764 765 case TCPOPT_OOBACK: { 766 int seq; 767 768 if (optlen != 4) 769 continue; 770 if (tp->t_oobseq != cp[2]) { 771 printf("wrong ack\n"); 772 break; 773 } 774 printf("take oob ack %x and cancel rexmt\n", cp[2]); 775 tp->t_oobflags &= ~TCPOOB_NEEDACK; 776 tp->t_timer[TCPT_OOBREXMT] = 0; 777 break; 778 } 779 #endif TCPTRUEOOB 780 } 781 } 782 m_free(om); 783 } 784 785 /* 786 * Pull out of band byte out of a segment so 787 * it doesn't appear in the user's data queue. 788 * It is still reflected in the segment length for 789 * sequencing purposes. 790 */ 791 tcp_pulloutofband(so, ti) 792 struct socket *so; 793 struct tcpiphdr *ti; 794 { 795 register struct mbuf *m; 796 int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1; 797 798 m = dtom(ti); 799 while (cnt >= 0) { 800 if (m->m_len > cnt) { 801 char *cp = mtod(m, caddr_t) + cnt; 802 struct tcpcb *tp = sototcpcb(so); 803 804 tp->t_iobc = *cp; 805 tp->t_oobflags |= TCPOOB_HAVEDATA; 806 bcopy(cp+1, cp, m->m_len - cnt - 1); 807 m->m_len--; 808 return; 809 } 810 cnt -= m->m_len; 811 m = m->m_next; 812 if (m == 0) 813 break; 814 } 815 panic("tcp_pulloutofband"); 816 } 817 818 /* 819 * Insert segment ti into reassembly queue of tcp with 820 * control block tp. Return TH_FIN if reassembly now includes 821 * a segment with FIN. 822 */ 823 tcp_reass(tp, ti) 824 register struct tcpcb *tp; 825 register struct tcpiphdr *ti; 826 { 827 register struct tcpiphdr *q; 828 struct socket *so = tp->t_inpcb->inp_socket; 829 struct mbuf *m; 830 int flags; 831 COUNT(TCP_REASS); 832 833 /* 834 * Call with ti==0 after become established to 835 * force pre-ESTABLISHED data up to user socket. 836 */ 837 if (ti == 0) 838 goto present; 839 840 /* 841 * Find a segment which begins after this one does. 842 */ 843 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 844 q = (struct tcpiphdr *)q->ti_next) 845 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 846 break; 847 848 /* 849 * If there is a preceding segment, it may provide some of 850 * our data already. If so, drop the data from the incoming 851 * segment. If it provides all of our data, drop us. 852 */ 853 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 854 register int i; 855 q = (struct tcpiphdr *)q->ti_prev; 856 /* conversion to int (in i) handles seq wraparound */ 857 i = q->ti_seq + q->ti_len - ti->ti_seq; 858 if (i > 0) { 859 if (i >= ti->ti_len) 860 goto drop; 861 m_adj(dtom(tp), i); 862 ti->ti_len -= i; 863 ti->ti_seq += i; 864 } 865 q = (struct tcpiphdr *)(q->ti_next); 866 } 867 868 /* 869 * While we overlap succeeding segments trim them or, 870 * if they are completely covered, dequeue them. 871 */ 872 while (q != (struct tcpiphdr *)tp) { 873 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 874 if (i <= 0) 875 break; 876 if (i < q->ti_len) { 877 q->ti_seq += i; 878 q->ti_len -= i; 879 m_adj(dtom(q), i); 880 break; 881 } 882 q = (struct tcpiphdr *)q->ti_next; 883 m = dtom(q->ti_prev); 884 remque(q->ti_prev); 885 m_freem(m); 886 } 887 888 /* 889 * Stick new segment in its place. 890 */ 891 insque(ti, q->ti_prev); 892 893 present: 894 /* 895 * Present data to user, advancing rcv_nxt through 896 * completed sequence space. 897 */ 898 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 899 return (0); 900 ti = tp->seg_next; 901 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 902 return (0); 903 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 904 return (0); 905 do { 906 tp->rcv_nxt += ti->ti_len; 907 flags = ti->ti_flags & TH_FIN; 908 remque(ti); 909 m = dtom(ti); 910 ti = (struct tcpiphdr *)ti->ti_next; 911 if (so->so_state & SS_CANTRCVMORE) 912 (void) m_freem(m); 913 else 914 sbappend(&so->so_rcv, m); 915 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 916 sorwakeup(so); 917 return (flags); 918 drop: 919 m_freem(dtom(ti)); 920 return (0); 921 } 922