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