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