1 /* tcp_input.c 1.45 81/12/23 */ 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_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 57 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 58 tcpstat.tcps_hdrops++; 59 return; 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 (ti->ti_ack == tp->snd_max) 402 tp->t_timer[TCPT_REXMT] = 0; 403 else { 404 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 405 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 406 tp->t_rtt = 0; 407 tp->t_rxtshift = 0; 408 } 409 if (acked > so->so_snd.sb_cc) { 410 sbdrop(&so->so_snd, so->so_snd.sb_cc); 411 tp->snd_wnd -= so->so_snd.sb_cc; 412 } else { 413 sbdrop(&so->so_snd.sb_cc, acked); 414 tp->snd_wnd -= acked; 415 acked = 0; 416 } 417 if (so->so_snd.sb_flags & SB_WAIT) 418 sowwakeup(so); 419 tp->snd_una = ti->ti_ack; 420 421 /* 422 * If transmit timer is running and timed sequence 423 * number was acked, update smoothed round trip time. 424 */ 425 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 426 if (tp->t_srtt == 0) 427 tp->t_srtt = tp->t_rtt; 428 else 429 tp->t_srtt = 430 tcp_alpha * tp->t_srtt + 431 (1 - tcp_alpha) * tp->t_rtt; 432 tp->t_rtt = 0; 433 } 434 435 switch (tp->t_state) { 436 437 /* 438 * In FIN_WAIT_1 STATE in addition to the processing 439 * for the ESTABLISHED state if our FIN is now acknowledged 440 * then enter FIN_WAIT_2. 441 */ 442 case TCPS_FIN_WAIT_1: 443 if (ourfinisacked) 444 tp->t_state = TCPS_FIN_WAIT_2; 445 break; 446 447 /* 448 * In CLOSING STATE in addition to the processing for 449 * the ESTABLISHED state if the ACK acknowledges our FIN 450 * then enter the TIME-WAIT state, otherwise ignore 451 * the segment. 452 */ 453 case TCPS_CLOSING: 454 if (ourfinisacked) { 455 tp->t_state = TCPS_TIME_WAIT; 456 tcp_canceltimers(tp); 457 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 458 soisdisconnected(so); 459 } 460 break; 461 462 /* 463 * The only thing that can arrive in LAST_ACK state 464 * is an acknowledgment of our FIN. If our FIN is now 465 * acknowledged, delete the TCB, enter the closed state 466 * and return. 467 */ 468 case TCPS_LAST_ACK: 469 if (ourfinisacked) 470 tcp_close(tp); 471 goto drop; 472 473 /* 474 * In TIME_WAIT state the only thing that should arrive 475 * is a retransmission of the remote FIN. Acknowledge 476 * it and restart the finack timer. 477 */ 478 case TCPS_TIME_WAIT: 479 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 480 goto dropafterack; 481 } 482 #undef ourfinisacked 483 } 484 485 step6: 486 /* 487 * Update window information. 488 */ 489 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 490 (SEQ_LEQ(tp->snd_wl2, ti->ti_ack) || 491 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 492 tp->snd_wnd = ti->ti_win; 493 tp->snd_wl1 = ti->ti_seq; 494 tp->snd_wl2 = ti->ti_ack; 495 if (tp->snd_wnd > 0) 496 tp->t_timer[TCPT_PERSIST] = 0; 497 } 498 499 /* 500 * If an URG bit is set in the segment and is greater than the 501 * current known urgent pointer, then signal the user that the 502 * remote side has out of band data. This should not happen 503 * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 504 * a FIN has been received from the remote side. In these states 505 * we ignore the URG. 506 */ 507 if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 508 if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 509 tp->rcv_up = ti->ti_urp; 510 #if 0 511 sohasoutofband(so); /* XXX */ 512 #endif 513 } 514 515 /* 516 * Process the segment text, merging it into the TCP sequencing queue, 517 * and arranging for acknowledgment of receipt if necessary. 518 * This process logically involves adjusting tp->rcv_wnd as data 519 * is presented to the user (this happens in tcp_usrreq.c, 520 * case PRU_RCVD). If a FIN has already been received on this 521 * connection then we just ignore the text. 522 */ 523 if ((ti->ti_len || (tiflags&TH_FIN)) && 524 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 525 off += sizeof (struct ip); /* drop IP header */ 526 m->m_off += off; 527 m->m_len -= off; 528 tiflags = tcp_reass(tp, ti); 529 { extern tcpdelack; 530 if (tcpdelack) tp->t_flags |= TF_DELACK; else 531 tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 532 } 533 } else { 534 m_freem(m); 535 tiflags &= ~TH_FIN; 536 } 537 538 /* 539 * If FIN is received ACK the FIN and let the user know 540 * that the connection is closing. 541 */ 542 if (tiflags & TH_FIN) { 543 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 544 socantrcvmore(so); 545 tp->t_flags |= TF_ACKNOW; 546 tp->rcv_nxt++; 547 } 548 switch (tp->t_state) { 549 550 /* 551 * In SYN_RECEIVED and ESTABLISHED STATES 552 * enter the CLOSE_WAIT state. 553 */ 554 case TCPS_SYN_RECEIVED: 555 case TCPS_ESTABLISHED: 556 tp->t_state = TCPS_CLOSE_WAIT; 557 break; 558 559 /* 560 * If still in FIN_WAIT_1 STATE FIN has not been acked so 561 * enter the CLOSING state. 562 */ 563 case TCPS_FIN_WAIT_1: 564 tp->t_state = TCPS_CLOSING; 565 break; 566 567 /* 568 * In FIN_WAIT_2 state enter the TIME_WAIT state, 569 * starting the time-wait timer, turning off the other 570 * standard timers. 571 */ 572 case TCPS_FIN_WAIT_2: 573 tp->t_state = TCPS_TIME_WAIT; 574 tcp_canceltimers(tp); 575 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 576 soisdisconnected(so); 577 break; 578 579 /* 580 * In TIME_WAIT state restart the 2 MSL time_wait timer. 581 */ 582 case TCPS_TIME_WAIT: 583 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 584 break; 585 } 586 } 587 if (so->so_options & SO_DEBUG) 588 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 589 590 /* 591 * Return any desired output. 592 */ 593 tcp_output(tp); 594 return; 595 596 dropafterack: 597 /* 598 * Generate an ACK dropping incoming segment. 599 * Make ACK reflect our state. 600 */ 601 if (tiflags & TH_RST) 602 goto drop; 603 tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 604 return; 605 606 dropwithreset: 607 /* 608 * Generate a RST, dropping incoming segment. 609 * Make ACK acceptable to originator of segment. 610 */ 611 if (tiflags & TH_RST) 612 goto drop; 613 if (tiflags & TH_ACK) 614 tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 615 else { 616 if (tiflags & TH_SYN) 617 ti->ti_len++; 618 tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 619 } 620 return; 621 622 drop: 623 /* 624 * Drop space held by incoming segment and return. 625 */ 626 m_freem(m); 627 return; 628 } 629 630 /* 631 * Insert segment ti into reassembly queue of tcp with 632 * control block tp. Return TH_FIN if reassembly now includes 633 * a segment with FIN. 634 */ 635 tcp_reass(tp, ti) 636 register struct tcpcb *tp; 637 register struct tcpiphdr *ti; 638 { 639 register struct tcpiphdr *q; 640 struct socket *so = tp->t_inpcb->inp_socket; 641 struct mbuf *m; 642 int flags; 643 COUNT(TCP_REASS); 644 645 /* 646 * Call with ti==0 after become established to 647 * force pre-ESTABLISHED data up to user socket. 648 */ 649 if (ti == 0) 650 goto present; 651 652 /* 653 * Find a segment which begins after this one does. 654 */ 655 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 656 q = (struct tcpiphdr *)q->ti_next) 657 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 658 break; 659 660 /* 661 * If there is a preceding segment, it may provide some of 662 * our data already. If so, drop the data from the incoming 663 * segment. If it provides all of our data, drop us. 664 */ 665 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 666 register int i; 667 q = (struct tcpiphdr *)(q->ti_prev); 668 /* conversion to int (in i) handles seq wraparound */ 669 i = q->ti_seq + q->ti_len - ti->ti_seq; 670 if (i > 0) { 671 if (i >= ti->ti_len) 672 goto drop; 673 m_adj(dtom(tp), i); 674 ti->ti_len -= i; 675 ti->ti_seq += i; 676 } 677 q = (struct tcpiphdr *)(q->ti_next); 678 } 679 680 /* 681 * While we overlap succeeding segments trim them or, 682 * if they are completely covered, dequeue them. 683 */ 684 while (q != (struct tcpiphdr *)tp && 685 SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 686 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 687 if (i < q->ti_len) { 688 q->ti_len -= i; 689 m_adj(dtom(q), i); 690 break; 691 } 692 q = (struct tcpiphdr *)q->ti_next; 693 m_freem(dtom(q->ti_prev)); 694 remque(q->ti_prev); 695 } 696 697 /* 698 * Stick new segment in its place. 699 */ 700 insque(ti, q->ti_prev); 701 702 present: 703 /* 704 * Present data to user, advancing rcv_nxt through 705 * completed sequence space. 706 */ 707 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 708 return (0); 709 ti = tp->seg_next; 710 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 711 return (0); 712 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 713 return (0); 714 do { 715 tp->rcv_nxt += ti->ti_len; 716 flags = ti->ti_flags & TH_FIN; 717 remque(ti); 718 m = dtom(ti); 719 ti = (struct tcpiphdr *)ti->ti_next; 720 if (so->so_state & SS_CANTRCVMORE) 721 (void) m_freem(m); 722 else 723 sbappend(&so->so_rcv, m); 724 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 725 sorwakeup(so); 726 return (flags); 727 drop: 728 m_freem(dtom(ti)); 729 return (0); 730 } 731