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