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