| 1 | /* tcp_input.c 1.60 82/03/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 | #ifdef notdef |
| 25 | int tcpprintfs = 0; |
| 26 | #endif |
| 27 | int tcpcksum = 1; |
| 28 | struct sockaddr_in tcp_in = { AF_INET }; |
| 29 | struct tcpiphdr tcp_saveti; |
| 30 | extern tcpnodelack; |
| 31 | |
| 32 | struct tcpcb *tcp_newtcpcb(); |
| 33 | /* |
| 34 | * TCP input routine, follows pages 65-76 of the |
| 35 | * protocol specification dated September, 1981 very closely. |
| 36 | */ |
| 37 | tcp_input(m0) |
| 38 | struct mbuf *m0; |
| 39 | { |
| 40 | register struct tcpiphdr *ti; |
| 41 | struct inpcb *inp; |
| 42 | register struct mbuf *m; |
| 43 | struct mbuf *om = 0; |
| 44 | int len, tlen, off; |
| 45 | register struct tcpcb *tp = 0; |
| 46 | register int tiflags; |
| 47 | struct socket *so; |
| 48 | int todrop, acked; |
| 49 | short ostate; |
| 50 | struct in_addr laddr; |
| 51 | |
| 52 | COUNT(TCP_INPUT); |
| 53 | /* |
| 54 | * Get IP and TCP header together in first mbuf. |
| 55 | * Note: IP leaves IP header in first mbuf. |
| 56 | */ |
| 57 | m = m0; |
| 58 | ti = mtod(m, struct tcpiphdr *); |
| 59 | if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) |
| 60 | ip_stripoptions((struct ip *)ti, (struct mbuf *)0); |
| 61 | if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { |
| 62 | if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { |
| 63 | tcpstat.tcps_hdrops++; |
| 64 | return; |
| 65 | } |
| 66 | ti = mtod(m, struct tcpiphdr *); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Checksum extended TCP header and data. |
| 71 | */ |
| 72 | tlen = ((struct ip *)ti)->ip_len; |
| 73 | len = sizeof (struct ip) + tlen; |
| 74 | if (tcpcksum) { |
| 75 | ti->ti_next = ti->ti_prev = 0; |
| 76 | ti->ti_x1 = 0; |
| 77 | ti->ti_len = (u_short)tlen; |
| 78 | #if vax |
| 79 | ti->ti_len = htons((u_short)ti->ti_len); |
| 80 | #endif |
| 81 | if (ti->ti_sum = in_cksum(m, len)) { |
| 82 | tcpstat.tcps_badsum++; |
| 83 | printf("tcp cksum %x\n", ti->ti_sum); |
| 84 | goto drop; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Check that TCP offset makes sense, |
| 90 | * pull out TCP options and adjust length. |
| 91 | */ |
| 92 | off = ti->ti_off << 2; |
| 93 | if (off < sizeof (struct tcphdr) || off > tlen) { |
| 94 | tcpstat.tcps_badoff++; |
| 95 | goto drop; |
| 96 | } |
| 97 | ti->ti_len = tlen - off; |
| 98 | if (off > sizeof (struct tcphdr)) { |
| 99 | if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { |
| 100 | tcpstat.tcps_hdrops++; |
| 101 | goto drop; |
| 102 | } |
| 103 | ti = mtod(m, struct tcpiphdr *); |
| 104 | om = m_get(M_DONTWAIT); |
| 105 | if (om == 0) |
| 106 | goto drop; |
| 107 | om->m_off = MMINOFF; |
| 108 | om->m_len = off - sizeof (struct tcphdr); |
| 109 | { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); |
| 110 | bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); |
| 111 | m->m_len -= om->m_len; |
| 112 | bcopy(op+om->m_len, op, |
| 113 | (unsigned)(m->m_len-sizeof (struct tcpiphdr))); |
| 114 | } |
| 115 | } |
| 116 | tiflags = ti->ti_flags; |
| 117 | |
| 118 | /* |
| 119 | * drop IP header |
| 120 | */ |
| 121 | off += sizeof (struct ip); |
| 122 | m->m_off += off; |
| 123 | m->m_len -= off; |
| 124 | |
| 125 | #if vax |
| 126 | /* |
| 127 | * Convert TCP protocol specific fields to host format. |
| 128 | */ |
| 129 | ti->ti_seq = ntohl(ti->ti_seq); |
| 130 | ti->ti_ack = ntohl(ti->ti_ack); |
| 131 | ti->ti_win = ntohs(ti->ti_win); |
| 132 | ti->ti_urp = ntohs(ti->ti_urp); |
| 133 | #endif |
| 134 | |
| 135 | /* |
| 136 | * Locate pcb for segment. On match, update the local |
| 137 | * address stored in the block to reflect anchoring. |
| 138 | */ |
| 139 | inp = in_pcblookup |
| 140 | (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, |
| 141 | INPLOOKUP_WILDCARD); |
| 142 | |
| 143 | /* |
| 144 | * If the state is CLOSED (i.e., TCB does not exist) then |
| 145 | * all data in the incoming segment is discarded. |
| 146 | */ |
| 147 | if (inp == 0) |
| 148 | goto dropwithreset; |
| 149 | tp = intotcpcb(inp); |
| 150 | if (tp == 0) |
| 151 | goto dropwithreset; |
| 152 | so = inp->inp_socket; |
| 153 | if (so->so_options & SO_DEBUG) { |
| 154 | ostate = tp->t_state; |
| 155 | tcp_saveti = *ti; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Segment received on connection. |
| 160 | * Reset idle time and keep-alive timer. |
| 161 | */ |
| 162 | tp->t_idle = 0; |
| 163 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; |
| 164 | |
| 165 | /* |
| 166 | * Process options. |
| 167 | */ |
| 168 | if (om) { |
| 169 | tcp_dooptions(tp, om); |
| 170 | om = 0; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Calculate amount of space in receive window, |
| 175 | * and then do TCP input processing. |
| 176 | */ |
| 177 | tp->rcv_wnd = sbspace(&so->so_rcv); |
| 178 | if (tp->rcv_wnd < 0) |
| 179 | tp->rcv_wnd = 0; |
| 180 | |
| 181 | switch (tp->t_state) { |
| 182 | |
| 183 | /* |
| 184 | * If the state is LISTEN then ignore segment if it contains an RST. |
| 185 | * If the segment contains an ACK then it is bad and send a RST. |
| 186 | * If it does not contain a SYN then it is not interesting; drop it. |
| 187 | * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial |
| 188 | * tp->iss, and send a segment: |
| 189 | * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> |
| 190 | * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. |
| 191 | * Fill in remote peer address fields if not previously specified. |
| 192 | * Enter SYN_RECEIVED state, and process any other fields of this |
| 193 | * segment in this state. |
| 194 | */ |
| 195 | case TCPS_LISTEN: |
| 196 | if (tiflags & TH_RST) |
| 197 | goto drop; |
| 198 | if (tiflags & TH_ACK) |
| 199 | goto dropwithreset; |
| 200 | if ((tiflags & TH_SYN) == 0) |
| 201 | goto drop; |
| 202 | tcp_in.sin_addr = ti->ti_src; |
| 203 | tcp_in.sin_port = ti->ti_sport; |
| 204 | laddr = inp->inp_laddr; |
| 205 | if (inp->inp_laddr.s_addr == 0) |
| 206 | inp->inp_laddr = ti->ti_dst; |
| 207 | if (in_pcbconnect(inp, (struct sockaddr_in *)&tcp_in)) { |
| 208 | inp->inp_laddr = laddr; |
| 209 | goto drop; |
| 210 | } |
| 211 | tp->t_template = tcp_template(tp); |
| 212 | if (tp->t_template == 0) { |
| 213 | in_pcbdisconnect(inp); |
| 214 | inp->inp_laddr = laddr; |
| 215 | goto drop; |
| 216 | } |
| 217 | in_setsockaddr(inp); |
| 218 | tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; |
| 219 | tp->irs = ti->ti_seq; |
| 220 | tcp_sendseqinit(tp); |
| 221 | tcp_rcvseqinit(tp); |
| 222 | tp->t_state = TCPS_SYN_RECEIVED; |
| 223 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; |
| 224 | goto trimthenstep6; |
| 225 | |
| 226 | /* |
| 227 | * If the state is SYN_SENT: |
| 228 | * if seg contains an ACK, but not for our SYN, drop the input. |
| 229 | * if seg contains a RST, then drop the connection. |
| 230 | * if seg does not contain SYN, then drop it. |
| 231 | * Otherwise this is an acceptable SYN segment |
| 232 | * initialize tp->rcv_nxt and tp->irs |
| 233 | * if seg contains ack then advance tp->snd_una |
| 234 | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state |
| 235 | * arrange for segment to be acked (eventually) |
| 236 | * continue processing rest of data/controls, beginning with URG |
| 237 | */ |
| 238 | case TCPS_SYN_SENT: |
| 239 | if ((tiflags & TH_ACK) && |
| 240 | /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ |
| 241 | (SEQ_LT(ti->ti_ack, tp->iss) || |
| 242 | SEQ_GT(ti->ti_ack, tp->snd_max))) |
| 243 | goto dropwithreset; |
| 244 | if (tiflags & TH_RST) { |
| 245 | if (tiflags & TH_ACK) |
| 246 | tcp_drop(tp, ECONNREFUSED); |
| 247 | goto drop; |
| 248 | } |
| 249 | if ((tiflags & TH_SYN) == 0) |
| 250 | goto drop; |
| 251 | tp->snd_una = ti->ti_ack; |
| 252 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
| 253 | tp->snd_nxt = tp->snd_una; |
| 254 | tp->t_timer[TCPT_REXMT] = 0; |
| 255 | tp->irs = ti->ti_seq; |
| 256 | tcp_rcvseqinit(tp); |
| 257 | tp->t_flags |= TF_ACKNOW; |
| 258 | if (SEQ_GT(tp->snd_una, tp->iss)) { |
| 259 | if (so->so_options & SO_ACCEPTCONN) |
| 260 | so->so_state |= SS_CONNAWAITING; |
| 261 | soisconnected(so); |
| 262 | tp->t_state = TCPS_ESTABLISHED; |
| 263 | (void) tcp_reass(tp, (struct tcpiphdr *)0); |
| 264 | } else |
| 265 | tp->t_state = TCPS_SYN_RECEIVED; |
| 266 | goto trimthenstep6; |
| 267 | |
| 268 | trimthenstep6: |
| 269 | /* |
| 270 | * Advance ti->ti_seq to correspond to first data byte. |
| 271 | * If data, trim to stay within window, |
| 272 | * dropping FIN if necessary. |
| 273 | */ |
| 274 | ti->ti_seq++; |
| 275 | if (ti->ti_len > tp->rcv_wnd) { |
| 276 | todrop = ti->ti_len - tp->rcv_wnd; |
| 277 | m_adj(m, -todrop); |
| 278 | ti->ti_len = tp->rcv_wnd; |
| 279 | ti->ti_flags &= ~TH_FIN; |
| 280 | } |
| 281 | tp->snd_wl1 = ti->ti_seq - 1; |
| 282 | goto step6; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * States other than LISTEN or SYN_SENT. |
| 287 | * First check that at least some bytes of segment are within |
| 288 | * receive window. |
| 289 | */ |
| 290 | if (tp->rcv_wnd == 0) { |
| 291 | /* |
| 292 | * If window is closed can only take segments at |
| 293 | * window edge, and have to drop data and PUSH from |
| 294 | * incoming segments. |
| 295 | */ |
| 296 | if (tp->rcv_nxt != ti->ti_seq) |
| 297 | goto dropafterack; |
| 298 | if (ti->ti_len > 0) { |
| 299 | m_adj(m, ti->ti_len); |
| 300 | ti->ti_len = 0; |
| 301 | ti->ti_flags &= ~(TH_PUSH|TH_FIN); |
| 302 | } |
| 303 | } else { |
| 304 | /* |
| 305 | * If segment begins before rcv_nxt, drop leading |
| 306 | * data (and SYN); if nothing left, just ack. |
| 307 | */ |
| 308 | todrop = tp->rcv_nxt - ti->ti_seq; |
| 309 | if (todrop > 0) { |
| 310 | if (tiflags & TH_SYN) { |
| 311 | tiflags &= ~TH_SYN; |
| 312 | ti->ti_flags &= ~TH_SYN; |
| 313 | ti->ti_seq++; |
| 314 | if (ti->ti_urp > 1) |
| 315 | ti->ti_urp--; |
| 316 | else |
| 317 | tiflags &= ~TH_URG; |
| 318 | todrop--; |
| 319 | } |
| 320 | if (todrop > ti->ti_len) |
| 321 | goto dropafterack; |
| 322 | m_adj(m, todrop); |
| 323 | ti->ti_seq += todrop; |
| 324 | ti->ti_len -= todrop; |
| 325 | if (ti->ti_urp > todrop) |
| 326 | ti->ti_urp -= todrop; |
| 327 | else { |
| 328 | tiflags &= ~TH_URG; |
| 329 | ti->ti_flags &= ~TH_URG; |
| 330 | ti->ti_urp = 0; |
| 331 | } |
| 332 | } |
| 333 | /* |
| 334 | * If segment ends after window, drop trailing data |
| 335 | * (and PUSH and FIN); if nothing left, just ACK. |
| 336 | */ |
| 337 | todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); |
| 338 | if (todrop > 0) { |
| 339 | if (todrop > ti->ti_len) |
| 340 | goto dropafterack; |
| 341 | m_adj(m, -todrop); |
| 342 | ti->ti_len -= todrop; |
| 343 | ti->ti_flags &= ~(TH_PUSH|TH_FIN); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * If a segment is received on a connection after the |
| 349 | * user processes are gone, then RST the other end. |
| 350 | */ |
| 351 | if (so->so_state & SS_USERGONE) { |
| 352 | tcp_close(tp); |
| 353 | goto dropwithreset; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * If the RST bit is set examine the state: |
| 358 | * SYN_RECEIVED STATE: |
| 359 | * If passive open, return to LISTEN state. |
| 360 | * If active open, inform user that connection was refused. |
| 361 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: |
| 362 | * Inform user that connection was reset, and close tcb. |
| 363 | * CLOSING, LAST_ACK, TIME_WAIT STATES |
| 364 | * Close the tcb. |
| 365 | */ |
| 366 | if (tiflags&TH_RST) switch (tp->t_state) { |
| 367 | |
| 368 | case TCPS_SYN_RECEIVED: |
| 369 | if (inp->inp_socket->so_options & SO_ACCEPTCONN) { |
| 370 | /* a miniature tcp_close, but invisible to user */ |
| 371 | (void) m_free(dtom(tp->t_template)); |
| 372 | (void) m_free(dtom(tp)); |
| 373 | inp->inp_ppcb = 0; |
| 374 | tp = tcp_newtcpcb(inp); |
| 375 | tp->t_state = TCPS_LISTEN; |
| 376 | inp->inp_faddr.s_addr = 0; |
| 377 | inp->inp_fport = 0; |
| 378 | inp->inp_laddr.s_addr = 0; /* not quite right */ |
| 379 | goto drop; |
| 380 | } |
| 381 | tcp_drop(tp, ECONNREFUSED); |
| 382 | goto drop; |
| 383 | |
| 384 | case TCPS_ESTABLISHED: |
| 385 | case TCPS_FIN_WAIT_1: |
| 386 | case TCPS_FIN_WAIT_2: |
| 387 | case TCPS_CLOSE_WAIT: |
| 388 | tcp_drop(tp, ECONNRESET); |
| 389 | goto drop; |
| 390 | |
| 391 | case TCPS_CLOSING: |
| 392 | case TCPS_LAST_ACK: |
| 393 | case TCPS_TIME_WAIT: |
| 394 | tcp_close(tp); |
| 395 | goto drop; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * If a SYN is in the window, then this is an |
| 400 | * error and we send an RST and drop the connection. |
| 401 | */ |
| 402 | if (tiflags & TH_SYN) { |
| 403 | tcp_drop(tp, ECONNRESET); |
| 404 | goto dropwithreset; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * If the ACK bit is off we drop the segment and return. |
| 409 | */ |
| 410 | if ((tiflags & TH_ACK) == 0) |
| 411 | goto drop; |
| 412 | |
| 413 | /* |
| 414 | * Ack processing. |
| 415 | */ |
| 416 | switch (tp->t_state) { |
| 417 | |
| 418 | /* |
| 419 | * In SYN_RECEIVED state if the ack ACKs our SYN then enter |
| 420 | * ESTABLISHED state and continue processing, othewise |
| 421 | * send an RST. |
| 422 | */ |
| 423 | case TCPS_SYN_RECEIVED: |
| 424 | if (SEQ_GT(tp->snd_una, ti->ti_ack) || |
| 425 | SEQ_GT(ti->ti_ack, tp->snd_max)) |
| 426 | goto dropwithreset; |
| 427 | tp->snd_una++; /* SYN acked */ |
| 428 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
| 429 | tp->snd_nxt = tp->snd_una; |
| 430 | tp->t_timer[TCPT_REXMT] = 0; |
| 431 | if (so->so_options & SO_ACCEPTCONN) |
| 432 | so->so_state |= SS_CONNAWAITING; |
| 433 | soisconnected(so); |
| 434 | tp->t_state = TCPS_ESTABLISHED; |
| 435 | (void) tcp_reass(tp, (struct tcpiphdr *)0); |
| 436 | tp->snd_wl1 = ti->ti_seq - 1; |
| 437 | /* fall into ... */ |
| 438 | |
| 439 | /* |
| 440 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range |
| 441 | * ACKs. If the ack is in the range |
| 442 | * tp->snd_una < ti->ti_ack <= tp->snd_max |
| 443 | * then advance tp->snd_una to ti->ti_ack and drop |
| 444 | * data from the retransmission queue. If this ACK reflects |
| 445 | * more up to date window information we update our window information. |
| 446 | */ |
| 447 | case TCPS_ESTABLISHED: |
| 448 | case TCPS_FIN_WAIT_1: |
| 449 | case TCPS_FIN_WAIT_2: |
| 450 | case TCPS_CLOSE_WAIT: |
| 451 | case TCPS_CLOSING: |
| 452 | case TCPS_LAST_ACK: |
| 453 | case TCPS_TIME_WAIT: |
| 454 | #define ourfinisacked (acked > 0) |
| 455 | |
| 456 | if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) |
| 457 | break; |
| 458 | if (SEQ_GT(ti->ti_ack, tp->snd_max)) |
| 459 | goto dropafterack; |
| 460 | acked = ti->ti_ack - tp->snd_una; |
| 461 | |
| 462 | /* |
| 463 | * If transmit timer is running and timed sequence |
| 464 | * number was acked, update smoothed round trip time. |
| 465 | */ |
| 466 | if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { |
| 467 | if (tp->t_srtt == 0) |
| 468 | tp->t_srtt = tp->t_rtt; |
| 469 | else |
| 470 | tp->t_srtt = |
| 471 | tcp_alpha * tp->t_srtt + |
| 472 | (1 - tcp_alpha) * tp->t_rtt; |
| 473 | /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ |
| 474 | tp->t_rtt = 0; |
| 475 | } |
| 476 | |
| 477 | if (ti->ti_ack == tp->snd_max) |
| 478 | tp->t_timer[TCPT_REXMT] = 0; |
| 479 | else { |
| 480 | TCPT_RANGESET(tp->t_timer[TCPT_REXMT], |
| 481 | tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); |
| 482 | tp->t_rtt = 1; |
| 483 | tp->t_rxtshift = 0; |
| 484 | } |
| 485 | if (acked > so->so_snd.sb_cc) { |
| 486 | sbdrop(&so->so_snd, so->so_snd.sb_cc); |
| 487 | tp->snd_wnd -= so->so_snd.sb_cc; |
| 488 | } else { |
| 489 | sbdrop(&so->so_snd, acked); |
| 490 | tp->snd_wnd -= acked; |
| 491 | acked = 0; |
| 492 | } |
| 493 | if (so->so_snd.sb_flags & SB_WAIT) |
| 494 | sowwakeup(so); |
| 495 | tp->snd_una = ti->ti_ack; |
| 496 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
| 497 | tp->snd_nxt = tp->snd_una; |
| 498 | |
| 499 | switch (tp->t_state) { |
| 500 | |
| 501 | /* |
| 502 | * In FIN_WAIT_1 STATE in addition to the processing |
| 503 | * for the ESTABLISHED state if our FIN is now acknowledged |
| 504 | * then enter FIN_WAIT_2. |
| 505 | */ |
| 506 | case TCPS_FIN_WAIT_1: |
| 507 | if (ourfinisacked) { |
| 508 | /* |
| 509 | * If we can't receive any more |
| 510 | * data, then closing user can proceed. |
| 511 | */ |
| 512 | if (so->so_state & SS_CANTRCVMORE) |
| 513 | soisdisconnected(so); |
| 514 | tp->t_state = TCPS_FIN_WAIT_2; |
| 515 | } |
| 516 | break; |
| 517 | |
| 518 | /* |
| 519 | * In CLOSING STATE in addition to the processing for |
| 520 | * the ESTABLISHED state if the ACK acknowledges our FIN |
| 521 | * then enter the TIME-WAIT state, otherwise ignore |
| 522 | * the segment. |
| 523 | */ |
| 524 | case TCPS_CLOSING: |
| 525 | if (ourfinisacked) { |
| 526 | tp->t_state = TCPS_TIME_WAIT; |
| 527 | tcp_canceltimers(tp); |
| 528 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
| 529 | soisdisconnected(so); |
| 530 | } |
| 531 | break; |
| 532 | |
| 533 | /* |
| 534 | * The only thing that can arrive in LAST_ACK state |
| 535 | * is an acknowledgment of our FIN. If our FIN is now |
| 536 | * acknowledged, delete the TCB, enter the closed state |
| 537 | * and return. |
| 538 | */ |
| 539 | case TCPS_LAST_ACK: |
| 540 | if (ourfinisacked) |
| 541 | tcp_close(tp); |
| 542 | goto drop; |
| 543 | |
| 544 | /* |
| 545 | * In TIME_WAIT state the only thing that should arrive |
| 546 | * is a retransmission of the remote FIN. Acknowledge |
| 547 | * it and restart the finack timer. |
| 548 | */ |
| 549 | case TCPS_TIME_WAIT: |
| 550 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
| 551 | goto dropafterack; |
| 552 | } |
| 553 | #undef ourfinisacked |
| 554 | } |
| 555 | |
| 556 | step6: |
| 557 | /* |
| 558 | * Update window information. |
| 559 | */ |
| 560 | if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && |
| 561 | (SEQ_LT(tp->snd_wl2, ti->ti_ack) || |
| 562 | tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { |
| 563 | tp->snd_wnd = ti->ti_win; |
| 564 | tp->snd_wl1 = ti->ti_seq; |
| 565 | tp->snd_wl2 = ti->ti_ack; |
| 566 | if (tp->snd_wnd > 0) |
| 567 | tp->t_timer[TCPT_PERSIST] = 0; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Process segments with URG. |
| 572 | */ |
| 573 | if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
| 574 | /* |
| 575 | * If this segment advances the known urgent pointer, |
| 576 | * then mark the data stream. This should not happen |
| 577 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since |
| 578 | * a FIN has been received from the remote side. |
| 579 | * In these states we ignore the URG. |
| 580 | */ |
| 581 | if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { |
| 582 | tp->rcv_up = ti->ti_seq + ti->ti_urp; |
| 583 | so->so_oobmark = so->so_rcv.sb_cc + |
| 584 | (tp->rcv_up - tp->rcv_nxt) - 1; |
| 585 | if (so->so_oobmark == 0) |
| 586 | so->so_state |= SS_RCVATMARK; |
| 587 | #ifdef TCPTRUEOOB |
| 588 | if ((tp->t_flags & TF_DOOOB) == 0) |
| 589 | #endif |
| 590 | sohasoutofband(so); |
| 591 | tp->t_oobflags &= ~TCPOOB_HAVEDATA; |
| 592 | } |
| 593 | /* |
| 594 | * Remove out of band data so doesn't get presented to user. |
| 595 | * This can happen independent of advancing the URG pointer, |
| 596 | * but if two URG's are pending at once, some out-of-band |
| 597 | * data may creep in... ick. |
| 598 | */ |
| 599 | if (ti->ti_urp <= ti->ti_len) { |
| 600 | tcp_pulloutofband(so, ti); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Process the segment text, merging it into the TCP sequencing queue, |
| 606 | * and arranging for acknowledgment of receipt if necessary. |
| 607 | * This process logically involves adjusting tp->rcv_wnd as data |
| 608 | * is presented to the user (this happens in tcp_usrreq.c, |
| 609 | * case PRU_RCVD). If a FIN has already been received on this |
| 610 | * connection then we just ignore the text. |
| 611 | */ |
| 612 | if ((ti->ti_len || (tiflags&TH_FIN)) && |
| 613 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
| 614 | tiflags = tcp_reass(tp, ti); |
| 615 | if (tcpnodelack == 0) |
| 616 | tp->t_flags |= TF_DELACK; |
| 617 | else |
| 618 | tp->t_flags |= TF_ACKNOW; |
| 619 | } else { |
| 620 | m_freem(m); |
| 621 | tiflags &= ~TH_FIN; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * If FIN is received ACK the FIN and let the user know |
| 626 | * that the connection is closing. |
| 627 | */ |
| 628 | if (tiflags & TH_FIN) { |
| 629 | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
| 630 | socantrcvmore(so); |
| 631 | tp->t_flags |= TF_ACKNOW; |
| 632 | tp->rcv_nxt++; |
| 633 | } |
| 634 | switch (tp->t_state) { |
| 635 | |
| 636 | /* |
| 637 | * In SYN_RECEIVED and ESTABLISHED STATES |
| 638 | * enter the CLOSE_WAIT state. |
| 639 | */ |
| 640 | case TCPS_SYN_RECEIVED: |
| 641 | case TCPS_ESTABLISHED: |
| 642 | tp->t_state = TCPS_CLOSE_WAIT; |
| 643 | break; |
| 644 | |
| 645 | /* |
| 646 | * If still in FIN_WAIT_1 STATE FIN has not been acked so |
| 647 | * enter the CLOSING state. |
| 648 | */ |
| 649 | case TCPS_FIN_WAIT_1: |
| 650 | tp->t_state = TCPS_CLOSING; |
| 651 | break; |
| 652 | |
| 653 | /* |
| 654 | * In FIN_WAIT_2 state enter the TIME_WAIT state, |
| 655 | * starting the time-wait timer, turning off the other |
| 656 | * standard timers. |
| 657 | */ |
| 658 | case TCPS_FIN_WAIT_2: |
| 659 | tp->t_state = TCPS_TIME_WAIT; |
| 660 | tcp_canceltimers(tp); |
| 661 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
| 662 | soisdisconnected(so); |
| 663 | break; |
| 664 | |
| 665 | /* |
| 666 | * In TIME_WAIT state restart the 2 MSL time_wait timer. |
| 667 | */ |
| 668 | case TCPS_TIME_WAIT: |
| 669 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | if (so->so_options & SO_DEBUG) |
| 674 | tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); |
| 675 | |
| 676 | /* |
| 677 | * Return any desired output. |
| 678 | */ |
| 679 | (void) tcp_output(tp); |
| 680 | return; |
| 681 | |
| 682 | dropafterack: |
| 683 | /* |
| 684 | * Generate an ACK dropping incoming segment. |
| 685 | * Make ACK reflect our state. |
| 686 | */ |
| 687 | if (tiflags & TH_RST) |
| 688 | goto drop; |
| 689 | tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); |
| 690 | return; |
| 691 | |
| 692 | dropwithreset: |
| 693 | if (om) |
| 694 | (void) m_free(om); |
| 695 | /* |
| 696 | * Generate a RST, dropping incoming segment. |
| 697 | * Make ACK acceptable to originator of segment. |
| 698 | */ |
| 699 | if (tiflags & TH_RST) |
| 700 | goto drop; |
| 701 | if (tiflags & TH_ACK) |
| 702 | tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); |
| 703 | else { |
| 704 | if (tiflags & TH_SYN) |
| 705 | ti->ti_len++; |
| 706 | tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); |
| 707 | } |
| 708 | return; |
| 709 | |
| 710 | drop: |
| 711 | /* |
| 712 | * Drop space held by incoming segment and return. |
| 713 | */ |
| 714 | m_freem(m); |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | tcp_dooptions(tp, om) |
| 719 | struct tcpcb *tp; |
| 720 | struct mbuf *om; |
| 721 | { |
| 722 | register u_char *cp; |
| 723 | int opt, optlen, cnt; |
| 724 | |
| 725 | cp = mtod(om, u_char *); |
| 726 | cnt = om->m_len; |
| 727 | for (; cnt > 0; cnt -= optlen, cp += optlen) { |
| 728 | opt = cp[0]; |
| 729 | if (opt == TCPOPT_EOL) |
| 730 | break; |
| 731 | if (opt == TCPOPT_NOP) |
| 732 | optlen = 1; |
| 733 | else |
| 734 | optlen = cp[1]; |
| 735 | switch (opt) { |
| 736 | |
| 737 | default: |
| 738 | break; |
| 739 | |
| 740 | case TCPOPT_MAXSEG: |
| 741 | if (optlen != 4) |
| 742 | continue; |
| 743 | tp->t_maxseg = *(u_short *)(cp + 2); |
| 744 | #if vax |
| 745 | tp->t_maxseg = ntohs((u_short)tp->t_maxseg); |
| 746 | #endif |
| 747 | break; |
| 748 | |
| 749 | #ifdef TCPTRUEOOB |
| 750 | case TCPOPT_WILLOOB: |
| 751 | tp->t_flags |= TF_DOOOB; |
| 752 | printf("tp %x dooob\n", tp); |
| 753 | break; |
| 754 | |
| 755 | case TCPOPT_OOBDATA: { |
| 756 | int seq; |
| 757 | register struct socket *so = tp->t_inpcb->inp_socket; |
| 758 | tcp_seq mark; |
| 759 | |
| 760 | if (optlen != 8) |
| 761 | continue; |
| 762 | seq = cp[2]; |
| 763 | if (seq < tp->t_iobseq) |
| 764 | seq += 256; |
| 765 | printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); |
| 766 | if (seq - tp->t_iobseq > 128) { |
| 767 | printf("bad seq\n"); |
| 768 | tp->t_oobflags |= TCPOOB_OWEACK; |
| 769 | break; |
| 770 | } |
| 771 | tp->t_iobseq = cp[2]; |
| 772 | tp->t_iobc = cp[3]; |
| 773 | mark = *(tcp_seq *)(cp + 4); |
| 774 | #if vax |
| 775 | mark = ntohl(mark); |
| 776 | #endif |
| 777 | so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); |
| 778 | if (so->so_oobmark == 0) |
| 779 | so->so_state |= SS_RCVATMARK; |
| 780 | printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); |
| 781 | sohasoutofband(so); |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | case TCPOPT_OOBACK: { |
| 786 | int seq; |
| 787 | |
| 788 | if (optlen != 4) |
| 789 | continue; |
| 790 | if (tp->t_oobseq != cp[2]) { |
| 791 | printf("wrong ack\n"); |
| 792 | break; |
| 793 | } |
| 794 | printf("take oob ack %x and cancel rexmt\n", cp[2]); |
| 795 | tp->t_oobflags &= ~TCPOOB_NEEDACK; |
| 796 | tp->t_timer[TCPT_OOBREXMT] = 0; |
| 797 | break; |
| 798 | } |
| 799 | #endif TCPTRUEOOB |
| 800 | } |
| 801 | } |
| 802 | (void) m_free(om); |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Pull out of band byte out of a segment so |
| 807 | * it doesn't appear in the user's data queue. |
| 808 | * It is still reflected in the segment length for |
| 809 | * sequencing purposes. |
| 810 | */ |
| 811 | tcp_pulloutofband(so, ti) |
| 812 | struct socket *so; |
| 813 | struct tcpiphdr *ti; |
| 814 | { |
| 815 | register struct mbuf *m; |
| 816 | int cnt = ti->ti_urp - 1; |
| 817 | |
| 818 | m = dtom(ti); |
| 819 | while (cnt >= 0) { |
| 820 | if (m->m_len > cnt) { |
| 821 | char *cp = mtod(m, caddr_t) + cnt; |
| 822 | struct tcpcb *tp = sototcpcb(so); |
| 823 | |
| 824 | tp->t_iobc = *cp; |
| 825 | tp->t_oobflags |= TCPOOB_HAVEDATA; |
| 826 | bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); |
| 827 | m->m_len--; |
| 828 | return; |
| 829 | } |
| 830 | cnt -= m->m_len; |
| 831 | m = m->m_next; |
| 832 | if (m == 0) |
| 833 | break; |
| 834 | } |
| 835 | panic("tcp_pulloutofband"); |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * Insert segment ti into reassembly queue of tcp with |
| 840 | * control block tp. Return TH_FIN if reassembly now includes |
| 841 | * a segment with FIN. |
| 842 | */ |
| 843 | tcp_reass(tp, ti) |
| 844 | register struct tcpcb *tp; |
| 845 | register struct tcpiphdr *ti; |
| 846 | { |
| 847 | register struct tcpiphdr *q; |
| 848 | struct socket *so = tp->t_inpcb->inp_socket; |
| 849 | struct mbuf *m; |
| 850 | int flags; |
| 851 | COUNT(TCP_REASS); |
| 852 | |
| 853 | /* |
| 854 | * Call with ti==0 after become established to |
| 855 | * force pre-ESTABLISHED data up to user socket. |
| 856 | */ |
| 857 | if (ti == 0) |
| 858 | goto present; |
| 859 | |
| 860 | /* |
| 861 | * Find a segment which begins after this one does. |
| 862 | */ |
| 863 | for (q = tp->seg_next; q != (struct tcpiphdr *)tp; |
| 864 | q = (struct tcpiphdr *)q->ti_next) |
| 865 | if (SEQ_GT(q->ti_seq, ti->ti_seq)) |
| 866 | break; |
| 867 | |
| 868 | /* |
| 869 | * If there is a preceding segment, it may provide some of |
| 870 | * our data already. If so, drop the data from the incoming |
| 871 | * segment. If it provides all of our data, drop us. |
| 872 | */ |
| 873 | if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { |
| 874 | register int i; |
| 875 | q = (struct tcpiphdr *)q->ti_prev; |
| 876 | /* conversion to int (in i) handles seq wraparound */ |
| 877 | i = q->ti_seq + q->ti_len - ti->ti_seq; |
| 878 | if (i > 0) { |
| 879 | if (i >= ti->ti_len) |
| 880 | goto drop; |
| 881 | m_adj(dtom(tp), i); |
| 882 | ti->ti_len -= i; |
| 883 | ti->ti_seq += i; |
| 884 | } |
| 885 | q = (struct tcpiphdr *)(q->ti_next); |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * While we overlap succeeding segments trim them or, |
| 890 | * if they are completely covered, dequeue them. |
| 891 | */ |
| 892 | while (q != (struct tcpiphdr *)tp) { |
| 893 | register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; |
| 894 | if (i <= 0) |
| 895 | break; |
| 896 | if (i < q->ti_len) { |
| 897 | q->ti_seq += i; |
| 898 | q->ti_len -= i; |
| 899 | m_adj(dtom(q), i); |
| 900 | break; |
| 901 | } |
| 902 | q = (struct tcpiphdr *)q->ti_next; |
| 903 | m = dtom(q->ti_prev); |
| 904 | remque(q->ti_prev); |
| 905 | m_freem(m); |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Stick new segment in its place. |
| 910 | */ |
| 911 | insque(ti, q->ti_prev); |
| 912 | |
| 913 | present: |
| 914 | /* |
| 915 | * Present data to user, advancing rcv_nxt through |
| 916 | * completed sequence space. |
| 917 | */ |
| 918 | if (TCPS_HAVERCVDSYN(tp->t_state) == 0) |
| 919 | return (0); |
| 920 | ti = tp->seg_next; |
| 921 | if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) |
| 922 | return (0); |
| 923 | if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) |
| 924 | return (0); |
| 925 | do { |
| 926 | tp->rcv_nxt += ti->ti_len; |
| 927 | flags = ti->ti_flags & TH_FIN; |
| 928 | remque(ti); |
| 929 | m = dtom(ti); |
| 930 | ti = (struct tcpiphdr *)ti->ti_next; |
| 931 | if (so->so_state & SS_CANTRCVMORE) |
| 932 | m_freem(m); |
| 933 | else |
| 934 | sbappend(&so->so_rcv, m); |
| 935 | } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); |
| 936 | sorwakeup(so); |
| 937 | return (flags); |
| 938 | drop: |
| 939 | m_freem(dtom(ti)); |
| 940 | return (0); |
| 941 | } |