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