1 /* tcp_input.c 1.26 81/11/20 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/mbuf.h" 6 #include "../h/socket.h" 7 #include "../h/socketvar.h" 8 #include "../net/inet.h" 9 #include "../net/inet_pcb.h" 10 #include "../net/inet_systm.h" 11 #include "../net/imp.h" 12 #include "../net/ip.h" 13 #include "../net/ip_var.h" 14 #include "../net/tcp.h" 15 #include "../net/tcp_fsm.h" 16 #include "../net/tcp_var.h" 17 #include "/usr/include/errno.h" 18 19 int tcpcksum = 1; 20 21 tcp_drain() 22 { 23 register struct inpcb *ip; 24 25 COUNT(TCP_DRAIN); 26 for (ip = tcb.inp_next; ip != &tcb; ip = ip->inp_next) 27 tcp_drainunack(intotcpcb(ip)); 28 } 29 30 tcp_drainunack(tp) 31 register struct tcpcb *tp; 32 { 33 register struct mbuf *m; 34 35 COUNT(TCP_DRAINUNACK); 36 for (m = tp->seg_unack; m; m = m->m_act) 37 m_freem(m); 38 tp->seg_unack = 0; 39 } 40 41 tcp_ctlinput(m) 42 struct mbuf *m; 43 { 44 45 COUNT(TCP_CTLINPUT); 46 m_freem(m); 47 } 48 49 struct sockaddr_in tcp_sockaddr = { AF_INET }; 50 51 tcp_input(m0) 52 struct mbuf *m0; 53 { 54 register struct tcpiphdr *ti; 55 struct inpcb *inp; 56 register struct mbuf *m; 57 int len, tlen, off; 58 59 register struct tcpcb *tp; 60 register int j; 61 register int tiflags; 62 int nstate; 63 struct socket *so; 64 #ifdef TCPDEBUG 65 struct tcp_debug tdb; 66 #endif 67 68 COUNT(TCP_INPUT); 69 /* 70 * Get ip and tcp header together in first mbuf. 71 */ 72 m = m0; 73 ti = mtod(m, struct tcpiphdr *); 74 if (ti->ti_len > sizeof (struct ip)) 75 ip_stripoptions((struct ip *)ti, (char *)0); 76 if (m->m_len < sizeof (struct tcpiphdr) && 77 m_pullup(m, sizeof (struct tcpiphdr)) == 0) { 78 tcpstat.tcps_hdrops++; 79 goto bad; 80 } 81 82 /* 83 * Checksum extended tcp header and data. 84 */ 85 tlen = ((struct ip *)ti)->ip_len; 86 len = sizeof (struct ip) + tlen; 87 if (tcpcksum) { 88 ti->ti_next = ti->ti_prev = 0; 89 ti->ti_x1 = 0; 90 ti->ti_len = htons((u_short)tlen); 91 if (ti->ti_sum = inet_cksum(m, len)) { 92 tcpstat.tcps_badsum++; 93 printf("tcp cksum %x\ti", ti->ti_sum); 94 goto bad; 95 } 96 } 97 98 /* 99 * Check that tcp offset makes sense, 100 * process tcp options and adjust length. 101 */ 102 off = ti->ti_off << 2; 103 if (off < sizeof (struct tcphdr) || off > ti->ti_len) { 104 tcpstat.tcps_badoff++; 105 goto bad; 106 } 107 ti->ti_len = tlen - off; 108 /* PROCESS OPTIONS */ 109 110 /* 111 * Convert addresses and ports to host format. 112 * Locate pcb for segment. 113 */ 114 ti->ti_src.s_addr = ntohl(ti->ti_src.s_addr); 115 ti->ti_dst.s_addr = ntohl(ti->ti_dst.s_addr); 116 ti->ti_sport = ntohs(ti->ti_sport); 117 ti->ti_dport = ntohs(ti->ti_dport); 118 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 119 if (inp == 0) 120 goto notwanted; 121 tp = intotcpcb(inp); /* ??? */ 122 if (tp == 0) /* ??? */ 123 goto notwanted; /* ??? */ 124 125 /* 126 * Convert tcp protocol specific fields to host format. 127 */ 128 ti->ti_seq = ntohl(ti->ti_seq); 129 ti->ti_ackno = ntohl((n_long)ti->ti_ackno); 130 ti->ti_win = ntohs(ti->ti_win); 131 ti->ti_urp = ntohs(ti->ti_urp); 132 133 /* 134 * Check segment seq # and do rst processing 135 */ 136 tiflags = ti->ti_flags; 137 switch (tp->t_state) { 138 139 case LISTEN: 140 if ((tiflags&TH_ACK) || (tiflags&TH_SYN) == 0) { 141 tcp_sndrst(tp, ti); 142 goto bad; 143 } 144 if (tiflags&TH_RST) 145 goto bad; 146 goto good; 147 148 case SYN_SENT: 149 if (!ack_ok(tp, ti) || (tiflags&TH_SYN) == 0) { 150 tcp_sndrst(tp, ti); /* 71,72,75 */ 151 goto bad; 152 } 153 if (tiflags&TH_RST) { 154 tcp_error(tp, ENETRESET); 155 tcp_detach(tp); /* 70 */ 156 tp->t_state = CLOSED; 157 goto bad; 158 } 159 goto good; 160 161 default: 162 if ((tiflags&TH_RST) == 0) 163 goto common; 164 if (ti->ti_seq < tp->rcv_nxt) /* bad rst */ 165 goto bad; /* 69 */ 166 switch (tp->t_state) { 167 168 case L_SYN_RCVD: 169 if (ack_ok(tp, ti) == 0) 170 goto bad; /* 69 */ 171 tp->t_rexmt = 0; 172 tp->t_rexmttl = 0; 173 tp->t_persist = 0; 174 inp->inp_faddr.s_addr = 0; 175 tp->t_state = LISTEN; 176 goto bad; 177 178 default: 179 tcp_error(tp, ENETRESET); 180 tcp_detach(tp); /* 66 */ 181 tp->t_state = CLOSED; 182 goto bad; 183 } 184 /*NOTREACHED*/ 185 186 case SYN_RCVD: 187 common: 188 if (ack_ok(tp, ti) == 0) { 189 tcp_sndrst(tp, ti); /* 74 */ 190 goto bad; 191 } 192 if ((tiflags&TH_SYN) == 0 && ti->ti_seq != tp->irs) { 193 tcp_sndnull(tp); /* 74 */ 194 goto bad; 195 } 196 goto good; 197 } 198 bad: 199 m_freem(m); 200 return; 201 202 good: 203 /* 204 * Defer processing if no buffer space for this connection. 205 */ 206 so = inp->inp_socket; 207 if (so->so_rcv.sb_cc >= so->so_rcv.sb_hiwat && 208 ti->ti_len != 0 && mbstat.m_bufs < mbstat.m_lowat) { 209 /* 210 m->m_act = (struct mbuf *)0; 211 if ((m = tp->seg_unack) != NULL) { 212 while (m->m_act != NULL) 213 m = m->m_act; 214 m->m_act = m0; 215 } else 216 tp->seg_unack = m0; 217 */ 218 m_freem(m0); 219 return; 220 } 221 222 /* 223 * Discard ip header, and do tcp input processing. 224 */ 225 off += sizeof (struct ip); 226 m->m_off += off; 227 m->m_len -= off; 228 nstate = tp->t_state; 229 tp->tc_flags &= ~TC_NET_KEEP; 230 #ifdef KPROF 231 acounts[tp->t_state][INRECV]++; 232 #endif 233 #ifdef TCPDEBUG 234 if ((tp->t_socket->so_options & SO_DEBUG) || tcpconsdebug) { 235 tdb_setup(tp, ti, INRECV, &tdb); 236 } else 237 tdb.td_tod = 0; 238 #endif 239 switch (tp->t_state) { 240 241 case LISTEN: 242 tcp_sockaddr.sin_addr = ti->ti_src; 243 tcp_sockaddr.sin_port = ti->ti_sport; 244 if ((tiflags&TH_SYN) == 0 || in_pcbsetpeer(inp, &tcp_sockaddr)) { 245 nstate = EFAILEC; 246 goto done; 247 } 248 tp->t_template = tcp_template(tp); 249 tcp_ctldat(tp, ti, 1); 250 if (tp->tc_flags&TC_FIN_RCVD) { 251 tp->t_finack = T_2ML; /* 3 */ 252 tp->tc_flags &= ~TC_WAITED_2_ML; 253 nstate = CLOSE_WAIT; 254 } else { 255 tp->t_init = T_INIT / 2; /* 4 */ 256 nstate = L_SYN_RCVD; 257 } 258 goto done; 259 260 case SYN_SENT: 261 if (!syn_ok(tp, ti)) { 262 nstate = EFAILEC; 263 goto done; 264 } 265 tcp_ctldat(tp, ti, 1); 266 if (tp->tc_flags&TC_FIN_RCVD) { 267 if ((tiflags&TH_ACK) == 0) { 268 tp->t_finack = T_2ML; /* 9 */ 269 tp->tc_flags &= ~TC_WAITED_2_ML; 270 } 271 nstate = CLOSE_WAIT; 272 goto done; 273 } 274 nstate = (tiflags&TH_ACK) ? ESTAB : SYN_RCVD; /* 11:8 */ 275 goto done; 276 277 case SYN_RCVD: 278 case L_SYN_RCVD: 279 if ((tiflags&TH_ACK) == 0 || 280 (tiflags&TH_ACK) && ti->ti_ackno <= tp->iss) { 281 nstate = EFAILEC; 282 goto done; 283 } 284 goto input; 285 286 case ESTAB: 287 case FIN_W1: 288 case FIN_W2: 289 case TIME_WAIT: 290 input: 291 tcp_ctldat(tp, ti, 1); /* 39 */ 292 switch (tp->t_state) { 293 294 case ESTAB: 295 if (tp->tc_flags&TC_FIN_RCVD) 296 nstate = CLOSE_WAIT; 297 break; 298 299 case SYN_RCVD: 300 case L_SYN_RCVD: 301 nstate = (tp->tc_flags&TC_FIN_RCVD) ? 302 CLOSE_WAIT : ESTAB; /* 33:5 */ 303 break; 304 305 case FIN_W1: 306 j = ack_fin(tp, ti); 307 if ((tp->tc_flags & TC_FIN_RCVD) == 0) { 308 if (j) 309 nstate = FIN_W2; /* 27 */ 310 break; 311 } 312 tp->t_finack = T_2ML; 313 tp->tc_flags &= ~TC_WAITED_2_ML; 314 nstate = j ? TIME_WAIT : CLOSING; /* 28:26 */ 315 break; 316 317 case FIN_W2: 318 if (tp->tc_flags&TC_FIN_RCVD) { 319 tp->t_finack = T_2ML; /* 29 */ 320 tp->tc_flags &= ~TC_WAITED_2_ML; 321 nstate = TIME_WAIT; 322 break; 323 } 324 break; 325 } 326 goto done; 327 328 case CLOSE_WAIT: 329 if (tiflags&TH_FIN) { 330 if ((tiflags&TH_ACK) && 331 ti->ti_ackno <= tp->seq_fin) { 332 tcp_ctldat(tp, ti, 0); /* 30 */ 333 tp->t_finack = T_2ML; 334 tp->tc_flags &= ~TC_WAITED_2_ML; 335 } else 336 (void) tcp_sndctl(tp); /* 31 */ 337 goto done; 338 } 339 goto input; 340 341 case CLOSING: 342 j = ack_fin(tp, ti); 343 if (tiflags&TH_FIN) { 344 tcp_ctldat(tp, ti, 0); 345 tp->t_finack = T_2ML; 346 tp->tc_flags &= ~TC_WAITED_2_ML; 347 if (j) 348 nstate = TIME_WAIT; /* 23 */ 349 goto done; 350 } 351 if (j) { 352 if (tp->tc_flags&TC_WAITED_2_ML) 353 if (rcv_empty(tp)) { 354 sorwakeup(inp->inp_socket); 355 nstate = CLOSED; /* 15 */ 356 } else 357 nstate = RCV_WAIT; /* 18 */ 358 else 359 nstate = TIME_WAIT; 360 goto done; 361 } 362 goto input; 363 364 case LAST_ACK: 365 if (ack_fin(tp, ti)) { 366 if (rcv_empty(tp)) { /* 16 */ 367 sorwakeup(inp->inp_socket); 368 nstate = CLOSED; 369 } else 370 nstate = RCV_WAIT; /* 19 */ 371 goto done; 372 } 373 if (tiflags&TH_FIN) { 374 (void) tcp_sndctl(tp); /* 31 */ 375 goto done; 376 } 377 goto input; 378 379 case RCV_WAIT: 380 if ((tiflags&TH_FIN) && (tiflags&TH_ACK) && 381 ti->ti_ackno <= tp->seq_fin) { 382 tcp_ctldat(tp, ti, 0); 383 tp->t_finack = T_2ML; 384 tp->tc_flags &= ~TC_WAITED_2_ML; /* 30 */ 385 } 386 goto done; 387 } 388 panic("tcp_input"); 389 done: 390 391 /* 392 * Done with state*input specific processing. 393 * Form trace records, free input if not needed, 394 * and enter new state. 395 */ 396 #ifdef TCPDEBUG 397 if (tdb.td_tod) 398 tdb_stuff(&tdb, nstate); 399 #endif 400 switch (nstate) { 401 402 case EFAILEC: 403 m_freem(m); 404 return; 405 406 default: 407 tp->t_state = nstate; 408 /* fall into ... */ 409 410 case CLOSED: 411 /* IF CLOSED CANT LOOK AT tc_flags */ 412 if ((tp->tc_flags&TC_NET_KEEP) == 0) { 413 register struct mbuf *n; 414 /* inline expansion of m_freem */ 415 while (m) { 416 MFREE(m, n); 417 m = n; 418 } 419 } 420 return; 421 } 422 /* NOTREACHED */ 423 424 /* 425 * Unwanted packed; free everything 426 * but the header and return an rst. 427 */ 428 notwanted: 429 m_freem(m->m_next); 430 m->m_next = NULL; 431 m->m_len = sizeof(struct tcpiphdr); 432 #define xchg(a,b) j=a; a=b; b=j 433 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr); 434 xchg(ti->ti_dport, ti->ti_sport); 435 #undef xchg 436 if (tiflags&TH_ACK) 437 ti->ti_seq = ti->ti_ackno; 438 else { 439 ti->ti_ackno = htonl((unsigned)(ntohl(ti->ti_seq) + ti->ti_len)); 440 ti->ti_seq = 0; 441 } 442 ti->ti_flags = ((tiflags & TH_ACK) ? 0 : TH_ACK) | TH_RST; 443 ti->ti_len = htons(TCPSIZE); 444 ti->ti_off = 5; 445 ti->ti_sum = inet_cksum(m, sizeof(struct tcpiphdr)); 446 ((struct ip *)ti)->ip_len = sizeof(struct tcpiphdr); 447 ip_output(m); 448 tcpstat.tcps_badsegs++; 449 } 450 451 tcp_ctldat(tp, n0, dataok) 452 register struct tcpcb *tp; 453 struct tcpiphdr *n0; 454 int dataok; 455 { 456 register struct tcpiphdr *ti = n0; 457 register int tiflags = ti->ti_flags; 458 struct socket *so = tp->t_inpcb->inp_socket; 459 seq_t past = ti->ti_seq + ti->ti_len; 460 seq_t urgent; 461 int sent; 462 COUNT(TCP_CTLDAT); 463 464 if (tiflags & TH_URG) 465 urgent = ti->ti_seq + ti->ti_urp; 466 tp->tc_flags &= ~(TC_ACK_DUE|TC_NEW_WINDOW); 467 /* syn */ 468 if ((tp->tc_flags&TC_SYN_RCVD) == 0 && (tiflags&TH_SYN)) { 469 tp->irs = ti->ti_seq; 470 tp->rcv_nxt = ti->ti_seq + 1; 471 tp->snd_wl = tp->rcv_urp = tp->irs; 472 tp->tc_flags |= (TC_SYN_RCVD|TC_ACK_DUE); 473 } 474 /* ack */ 475 if ((tiflags&TH_ACK) && (tp->tc_flags&TC_SYN_RCVD) && 476 ti->ti_ackno > tp->snd_una) { 477 /* 478 * Reflect newly acknowledged data. 479 */ 480 tp->snd_una = ti->ti_ackno; 481 if (tp->snd_una > tp->snd_nxt) 482 tp->snd_nxt = tp->snd_una; 483 484 /* 485 * If timed msg acked, update retransmit time value. 486 */ 487 if ((tp->tc_flags&TC_SYN_ACKED) && 488 tp->snd_una > tp->t_xmt_val) { 489 /* NEED SMOOTHING HERE */ 490 tp->t_xmtime = (tp->t_xmt != 0 ? tp->t_xmt : T_REXMT); 491 if (tp->t_xmtime > T_REMAX) 492 tp->t_xmtime = T_REMAX; 493 } 494 495 /* 496 * Remove acked data from send buf 497 */ 498 sbdrop(&so->so_snd, (int)(tp->snd_una - tp->snd_off)); 499 tp->snd_off = tp->snd_una; 500 if ((tp->tc_flags&TC_SYN_ACKED) == 0 && 501 (tp->snd_una > tp->iss)) { 502 tp->tc_flags |= TC_SYN_ACKED; 503 tp->t_init = 0; 504 } 505 if (tp->seq_fin != tp->iss && tp->snd_una > tp->seq_fin) 506 tp->tc_flags &= ~TC_SND_FIN; 507 tp->t_rexmt = 0; 508 tp->t_rexmttl = 0; 509 tp->tc_flags |= TC_CANCELLED; 510 sowwakeup(tp->t_inpcb->inp_socket); 511 } 512 /* win */ 513 if ((tp->tc_flags & TC_SYN_RCVD) && ti->ti_seq >= tp->snd_wl) { 514 tp->snd_wl = ti->ti_seq; 515 tp->snd_wnd = ti->ti_win; 516 tp->tc_flags |= TC_NEW_WINDOW; 517 tp->t_persist = 0; 518 } 519 /* text */ 520 if (dataok && ti->ti_len) { 521 register struct tcpiphdr *q; 522 int overage; 523 524 /* eol */ 525 if ((tiflags&TH_EOL)) { 526 register struct mbuf *m; 527 for (m = dtom(ti); m->m_next; m = m->m_next) 528 ; 529 m->m_act = (struct mbuf *)(mtod(m, caddr_t) - 1); 530 } 531 532 /* text */ 533 /* 534 * Discard duplicate data already passed to user. 535 */ 536 if (SEQ_LT(ti->ti_seq, tp->rcv_nxt)) { 537 register int i = tp->rcv_nxt - ti->ti_seq; 538 if (i >= ti->ti_len) 539 goto notext; 540 ti->ti_seq += i; 541 ti->ti_len -= i; 542 m_adj(dtom(ti), i); 543 } 544 545 /* 546 * Find a segment which begins after this one does. 547 */ 548 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 549 q = (struct tcpiphdr *)q->ti_next) 550 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 551 break; 552 553 /* 554 * If there is a preceding segment, it may provide some of 555 * our data already. If so, drop the data from the incoming 556 * segment. If it provides all of our data, drop us. 557 */ 558 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 559 register int i; 560 q = (struct tcpiphdr *)(q->ti_prev); 561 /* conversion to int (in i) handles seq wraparound */ 562 i = q->ti_seq + q->ti_len - ti->ti_seq; 563 if (i > 0) { 564 if (i >= ti->ti_len) 565 goto notext; 566 /* w/o setting TC_NET_KEEP */ 567 m_adj(dtom(tp), i); 568 ti->ti_len -= i; 569 ti->ti_seq += i; 570 } 571 q = (struct tcpiphdr *)(q->ti_next); 572 } 573 574 /* 575 * While we overlap succeeding segments trim them or, 576 * if they are completely covered, dequeue them. 577 */ 578 while (q != (struct tcpiphdr *)tp && 579 SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 580 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 581 if (i < q->ti_len) { 582 q->ti_len -= i; 583 m_adj(dtom(q), i); 584 break; 585 } 586 q = (struct tcpiphdr *)q->ti_next; 587 m_freem(dtom(q->ti_prev)); 588 remque(q->ti_prev); 589 } 590 591 /* 592 * Stick new segment in its place. 593 */ 594 insque(ti, q->ti_prev); 595 tp->seqcnt += ti->ti_len; 596 597 /* 598 * Calculate available space and discard segments for 599 * which there is too much. 600 */ 601 overage = 602 (so->so_rcv.sb_cc /*XXX+tp->rcv_seqcnt*/) - so->so_rcv.sb_hiwat; 603 if (overage > 0) { 604 q = tp->seg_prev; 605 for (;;) { 606 register int i = MIN(q->ti_len, overage); 607 overage -= i; 608 q->ti_len -= i; 609 m_adj(dtom(q), -i); 610 if (q->ti_len) 611 break; 612 if (q == ti) 613 panic("tcp_text dropall"); 614 q = (struct tcpiphdr *)q->ti_prev; 615 remque(q->ti_next); 616 } 617 } 618 619 /* 620 * Advance rcv_next through newly completed sequence space. 621 */ 622 while (ti->ti_seq == tp->rcv_nxt) { 623 tp->rcv_nxt += ti->ti_len; 624 ti = (struct tcpiphdr *)ti->ti_next; 625 if (ti == (struct tcpiphdr *)tp) 626 break; 627 } 628 /* urg */ 629 if (tiflags&TH_URG) { 630 /* ... */ 631 if (SEQ_GT(urgent, tp->rcv_urp)) 632 tp->rcv_urp = urgent; 633 } 634 tp->tc_flags |= (TC_ACK_DUE|TC_NET_KEEP); 635 } 636 notext: 637 /* fin */ 638 if ((tiflags&TH_FIN) && past == tp->rcv_nxt) { 639 if ((tp->tc_flags&TC_FIN_RCVD) == 0) { 640 tp->tc_flags |= TC_FIN_RCVD; 641 sorwakeup(so); 642 tp->rcv_nxt++; 643 } 644 tp->tc_flags |= TC_ACK_DUE; 645 } 646 /* respond */ 647 sent = 0; 648 if (tp->tc_flags&TC_ACK_DUE) 649 sent = tcp_sndctl(tp); 650 else if ((tp->tc_flags&TC_NEW_WINDOW)) 651 if (tp->snd_nxt <= tp->snd_off + so->so_snd.sb_cc || 652 (tp->tc_flags&TC_SND_FIN)) 653 sent = tcp_send(tp); 654 655 /* set for retrans */ 656 if (!sent && tp->snd_una < tp->snd_nxt && 657 (tp->tc_flags&TC_CANCELLED)) { 658 tp->t_rexmt = tp->t_xmtime; 659 tp->t_rexmttl = T_REXMTTL; 660 tp->t_rexmt_val = tp->t_rtl_val = tp->snd_lst; 661 tp->tc_flags &= ~TC_CANCELLED; 662 } 663 /* present data to user */ 664 if ((tp->tc_flags&TC_SYN_ACKED) == 0) 665 return; 666 ti = tp->seg_next; 667 while (ti != (struct tcpiphdr *)tp && ti->ti_seq < tp->rcv_nxt) { 668 remque(ti); 669 sbappend(&so->so_rcv, dtom(ti)); 670 tp->seqcnt -= ti->ti_len; 671 if (tp->seqcnt < 0) 672 panic("tcp_input present"); 673 ti = (struct tcpiphdr *)ti->ti_next; 674 } 675 sorwakeup(so); 676 } 677