1 /* tcp_input.c 1.50 82/01/18 */ 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 * Process segments with URG. 535 */ 536 if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 537 /* 538 * If this segment advances the known urgent pointer, 539 * then mark the data stream. This should not happen 540 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 541 * a FIN has been received from the remote side. 542 * In these states we ignore the URG. 543 */ 544 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 545 tp->rcv_up = ti->ti_seq + ti->ti_urp; 546 so->so_oobmark = so->so_rcv.sb_cc + 547 (tp->rcv_up - tp->rcv_nxt) - 1; 548 if (so->so_oobmark == 0) 549 so->so_state |= SS_RCVATMARK; 550 #ifdef TCPTRUEOOB 551 if ((tp->t_flags & TF_DOOOB) == 0) 552 #endif 553 sohasoutofband(so); 554 tp->t_oobflags &= ~TCPOOB_HAVEDATA; 555 } 556 /* 557 * Remove out of band data so doesn't get presented to user. 558 * This can happen independent of advancing the URG pointer, 559 * but if two URG's are pending at once, some out-of-band 560 * data may creep in... ick. 561 */ 562 if (ti->ti_urp <= ti->ti_len) { 563 tcp_pulloutofband(so, ti); 564 } 565 } 566 567 /* 568 * Process the segment text, merging it into the TCP sequencing queue, 569 * and arranging for acknowledgment of receipt if necessary. 570 * This process logically involves adjusting tp->rcv_wnd as data 571 * is presented to the user (this happens in tcp_usrreq.c, 572 * case PRU_RCVD). If a FIN has already been received on this 573 * connection then we just ignore the text. 574 */ 575 if ((ti->ti_len || (tiflags&TH_FIN)) && 576 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 577 off += sizeof (struct ip); /* drop IP header */ 578 m->m_off += off; 579 m->m_len -= off; 580 tiflags = tcp_reass(tp, ti); 581 if (tcpnodelack == 0) 582 tp->t_flags |= TF_DELACK; 583 else 584 tp->t_flags |= TF_ACKNOW; 585 } else { 586 m_freem(m); 587 tiflags &= ~TH_FIN; 588 } 589 590 /* 591 * If FIN is received ACK the FIN and let the user know 592 * that the connection is closing. 593 */ 594 if (tiflags & TH_FIN) { 595 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 596 socantrcvmore(so); 597 tp->t_flags |= TF_ACKNOW; 598 tp->rcv_nxt++; 599 } 600 switch (tp->t_state) { 601 602 /* 603 * In SYN_RECEIVED and ESTABLISHED STATES 604 * enter the CLOSE_WAIT state. 605 */ 606 case TCPS_SYN_RECEIVED: 607 case TCPS_ESTABLISHED: 608 tp->t_state = TCPS_CLOSE_WAIT; 609 break; 610 611 /* 612 * If still in FIN_WAIT_1 STATE FIN has not been acked so 613 * enter the CLOSING state. 614 */ 615 case TCPS_FIN_WAIT_1: 616 tp->t_state = TCPS_CLOSING; 617 break; 618 619 /* 620 * In FIN_WAIT_2 state enter the TIME_WAIT state, 621 * starting the time-wait timer, turning off the other 622 * standard timers. 623 */ 624 case TCPS_FIN_WAIT_2: 625 tp->t_state = TCPS_TIME_WAIT; 626 tcp_canceltimers(tp); 627 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 628 soisdisconnected(so); 629 break; 630 631 /* 632 * In TIME_WAIT state restart the 2 MSL time_wait timer. 633 */ 634 case TCPS_TIME_WAIT: 635 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 636 break; 637 } 638 } 639 if (so->so_options & SO_DEBUG) 640 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 641 642 /* 643 * Return any desired output. 644 */ 645 tcp_output(tp); 646 return; 647 648 dropafterack: 649 /* 650 * Generate an ACK dropping incoming segment. 651 * Make ACK reflect our state. 652 */ 653 if (tiflags & TH_RST) 654 goto drop; 655 tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 656 return; 657 658 dropwithreset: 659 if (om) 660 m_free(om); 661 /* 662 * Generate a RST, dropping incoming segment. 663 * Make ACK acceptable to originator of segment. 664 */ 665 if (tiflags & TH_RST) 666 goto drop; 667 if (tiflags & TH_ACK) 668 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 669 else { 670 if (tiflags & TH_SYN) 671 ti->ti_len++; 672 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 673 } 674 return; 675 676 drop: 677 /* 678 * Drop space held by incoming segment and return. 679 */ 680 m_freem(m); 681 return; 682 } 683 684 tcp_dooptions(tp, om) 685 struct tcpcb *tp; 686 struct mbuf *om; 687 { 688 register u_char *cp; 689 int opt, optlen, cnt; 690 691 cp = mtod(om, u_char *); 692 cnt = om->m_len; 693 for (; cnt > 0; cnt -= optlen, cp += optlen) { 694 opt = cp[0]; 695 if (opt == TCPOPT_EOL) 696 break; 697 if (opt == TCPOPT_NOP) 698 optlen = 1; 699 else 700 optlen = cp[1]; 701 switch (opt) { 702 703 default: 704 break; 705 706 case TCPOPT_MAXSEG: 707 if (optlen != 4) 708 continue; 709 tp->t_maxseg = *(u_short *)(cp + 2); 710 #if vax 711 tp->t_maxseg = ntohs(tp->t_maxseg); 712 #endif 713 break; 714 715 #ifdef TCPTRUEOOB 716 case TCPOPT_WILLOOB: 717 tp->t_flags |= TF_DOOOB; 718 printf("tp %x dooob\n", tp); 719 break; 720 721 case TCPOPT_OOBDATA: { 722 int seq; 723 register struct socket *so = tp->t_inpcb->inp_socket; 724 tcp_seq mark; 725 726 if (optlen != 8) 727 continue; 728 seq = cp[2]; 729 if (seq < tp->t_iobseq) 730 seq += 256; 731 printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 732 if (seq - tp->t_iobseq > 128) { 733 printf("bad seq\n"); 734 tp->t_oobflags |= TCPOOB_OWEACK; 735 break; 736 } 737 tp->t_iobseq = cp[2]; 738 tp->t_iobc = cp[3]; 739 mark = *(tcp_seq *)(cp + 4); 740 #if vax 741 mark = ntohl(mark); 742 #endif 743 so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 744 if (so->so_oobmark == 0) 745 so->so_state |= SS_RCVATMARK; 746 printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 747 sohasoutofband(so); 748 break; 749 } 750 751 case TCPOPT_OOBACK: { 752 int seq; 753 754 if (optlen != 4) 755 continue; 756 if (tp->t_oobseq != cp[2]) { 757 printf("wrong ack\n"); 758 break; 759 } 760 printf("take oob ack %x and cancel rexmt\n", cp[2]); 761 tp->t_oobflags &= ~TCPOOB_NEEDACK; 762 tp->t_timer[TCPT_OOBREXMT] = 0; 763 break; 764 } 765 #endif TCPTRUEOOB 766 } 767 } 768 m_free(om); 769 } 770 771 /* 772 * Pull out of band byte out of a segment so 773 * it doesn't appear in the user's data queue. 774 * It is still reflected in the segment length for 775 * sequencing purposes. 776 */ 777 tcp_pulloutofband(so, ti) 778 struct socket *so; 779 struct tcpiphdr *ti; 780 { 781 register struct mbuf *m; 782 int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1; 783 784 m = dtom(ti); 785 while (cnt >= 0) { 786 if (m->m_len > cnt) { 787 char *cp = mtod(m, caddr_t) + cnt; 788 struct tcpcb *tp = sototcpcb(so); 789 790 tp->t_iobc = *cp; 791 tp->t_oobflags |= TCPOOB_HAVEDATA; 792 bcopy(cp+1, cp, m->m_len - cnt - 1); 793 m->m_len--; 794 return; 795 } 796 cnt -= m->m_len; 797 m = m->m_next; 798 if (m == 0) 799 break; 800 } 801 panic("tcp_pulloutofband"); 802 } 803 804 /* 805 * Insert segment ti into reassembly queue of tcp with 806 * control block tp. Return TH_FIN if reassembly now includes 807 * a segment with FIN. 808 */ 809 tcp_reass(tp, ti) 810 register struct tcpcb *tp; 811 register struct tcpiphdr *ti; 812 { 813 register struct tcpiphdr *q; 814 struct socket *so = tp->t_inpcb->inp_socket; 815 struct mbuf *m; 816 int flags; 817 COUNT(TCP_REASS); 818 819 /* 820 * Call with ti==0 after become established to 821 * force pre-ESTABLISHED data up to user socket. 822 */ 823 if (ti == 0) 824 goto present; 825 826 /* 827 * Find a segment which begins after this one does. 828 */ 829 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 830 q = (struct tcpiphdr *)q->ti_next) 831 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 832 break; 833 834 /* 835 * If there is a preceding segment, it may provide some of 836 * our data already. If so, drop the data from the incoming 837 * segment. If it provides all of our data, drop us. 838 */ 839 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 840 register int i; 841 q = (struct tcpiphdr *)(q->ti_prev); 842 /* conversion to int (in i) handles seq wraparound */ 843 i = q->ti_seq + q->ti_len - ti->ti_seq; 844 if (i > 0) { 845 if (i >= ti->ti_len) 846 goto drop; 847 m_adj(dtom(tp), i); 848 ti->ti_len -= i; 849 ti->ti_seq += i; 850 } 851 q = (struct tcpiphdr *)(q->ti_next); 852 } 853 854 /* 855 * While we overlap succeeding segments trim them or, 856 * if they are completely covered, dequeue them. 857 */ 858 while (q != (struct tcpiphdr *)tp && 859 SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 860 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 861 if (i < q->ti_len) { 862 q->ti_len -= i; 863 m_adj(dtom(q), i); 864 break; 865 } 866 q = (struct tcpiphdr *)q->ti_next; 867 m_freem(dtom(q->ti_prev)); 868 remque(q->ti_prev); 869 } 870 871 /* 872 * Stick new segment in its place. 873 */ 874 insque(ti, q->ti_prev); 875 876 present: 877 /* 878 * Present data to user, advancing rcv_nxt through 879 * completed sequence space. 880 */ 881 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 882 return (0); 883 ti = tp->seg_next; 884 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 885 return (0); 886 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 887 return (0); 888 do { 889 tp->rcv_nxt += ti->ti_len; 890 flags = ti->ti_flags & TH_FIN; 891 remque(ti); 892 m = dtom(ti); 893 ti = (struct tcpiphdr *)ti->ti_next; 894 if (so->so_state & SS_CANTRCVMORE) 895 (void) m_freem(m); 896 else 897 sbappend(&so->so_rcv, m); 898 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 899 sorwakeup(so); 900 return (flags); 901 drop: 902 m_freem(dtom(ti)); 903 return (0); 904 } 905