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