1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_inet.h" 36 #include "opt_inet6.h" 37 #include "opt_mac.h" 38 #include "opt_tcpdebug.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/callout.h> 43 #include <sys/kernel.h> 44 #include <sys/sysctl.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/priv.h> 48 #include <sys/proc.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/protosw.h> 52 #include <sys/random.h> 53 #include <sys/vimage.h> 54 55 #include <vm/uma.h> 56 57 #include <net/route.h> 58 #include <net/if.h> 59 60 #include <netinet/in.h> 61 #include <netinet/in_systm.h> 62 #include <netinet/ip.h> 63 #ifdef INET6 64 #include <netinet/ip6.h> 65 #endif 66 #include <netinet/in_pcb.h> 67 #ifdef INET6 68 #include <netinet6/in6_pcb.h> 69 #endif 70 #include <netinet/in_var.h> 71 #include <netinet/ip_var.h> 72 #ifdef INET6 73 #include <netinet6/ip6_var.h> 74 #include <netinet6/scope6_var.h> 75 #include <netinet6/nd6.h> 76 #endif 77 #include <netinet/ip_icmp.h> 78 #include <netinet/tcp.h> 79 #include <netinet/tcp_fsm.h> 80 #include <netinet/tcp_seq.h> 81 #include <netinet/tcp_timer.h> 82 #include <netinet/tcp_var.h> 83 #ifdef INET6 84 #include <netinet6/tcp6_var.h> 85 #endif 86 #include <netinet/tcpip.h> 87 #ifdef TCPDEBUG 88 #include <netinet/tcp_debug.h> 89 #endif 90 #include <netinet6/ip6protosw.h> 91 #include <netinet/vinet.h> 92 93 #include <machine/in_cksum.h> 94 95 #include <security/mac/mac_framework.h> 96 97 static uma_zone_t tcptw_zone; 98 static int maxtcptw; 99 100 /* 101 * The timed wait queue contains references to each of the TCP sessions 102 * currently in the TIME_WAIT state. The queue pointers, including the 103 * queue pointers in each tcptw structure, are protected using the global 104 * tcbinfo lock, which must be held over queue iteration and modification. 105 */ 106 #ifdef VIMAGE_GLOBALS 107 static TAILQ_HEAD(, tcptw) twq_2msl; 108 int nolocaltimewait; 109 #endif 110 111 static void tcp_tw_2msl_reset(struct tcptw *, int); 112 static void tcp_tw_2msl_stop(struct tcptw *); 113 114 static int 115 tcptw_auto_size(void) 116 { 117 INIT_VNET_INET(curvnet); 118 int halfrange; 119 120 /* 121 * Max out at half the ephemeral port range so that TIME_WAIT 122 * sockets don't tie up too many ephemeral ports. 123 */ 124 if (V_ipport_lastauto > V_ipport_firstauto) 125 halfrange = (V_ipport_lastauto - V_ipport_firstauto) / 2; 126 else 127 halfrange = (V_ipport_firstauto - V_ipport_lastauto) / 2; 128 /* Protect against goofy port ranges smaller than 32. */ 129 return (imin(imax(halfrange, 32), maxsockets / 5)); 130 } 131 132 static int 133 sysctl_maxtcptw(SYSCTL_HANDLER_ARGS) 134 { 135 int error, new; 136 137 if (maxtcptw == 0) 138 new = tcptw_auto_size(); 139 else 140 new = maxtcptw; 141 error = sysctl_handle_int(oidp, &new, 0, req); 142 if (error == 0 && req->newptr) 143 if (new >= 32) { 144 maxtcptw = new; 145 uma_zone_set_max(tcptw_zone, maxtcptw); 146 } 147 return (error); 148 } 149 150 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxtcptw, CTLTYPE_INT|CTLFLAG_RW, 151 &maxtcptw, 0, sysctl_maxtcptw, "IU", 152 "Maximum number of compressed TCP TIME_WAIT entries"); 153 154 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, nolocaltimewait, 155 CTLFLAG_RW, nolocaltimewait, 0, 156 "Do not create compressed TCP TIME_WAIT entries for local connections"); 157 158 void 159 tcp_tw_zone_change(void) 160 { 161 162 if (maxtcptw == 0) 163 uma_zone_set_max(tcptw_zone, tcptw_auto_size()); 164 } 165 166 void 167 tcp_tw_init(void) 168 { 169 INIT_VNET_INET(curvnet); 170 171 tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw), 172 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 173 TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw); 174 if (maxtcptw == 0) 175 uma_zone_set_max(tcptw_zone, tcptw_auto_size()); 176 else 177 uma_zone_set_max(tcptw_zone, maxtcptw); 178 TAILQ_INIT(&V_twq_2msl); 179 } 180 181 /* 182 * Move a TCP connection into TIME_WAIT state. 183 * tcbinfo is locked. 184 * inp is locked, and is unlocked before returning. 185 */ 186 void 187 tcp_twstart(struct tcpcb *tp) 188 { 189 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 190 INIT_VNET_INET(tp->t_vnet); 191 #endif 192 struct tcptw *tw; 193 struct inpcb *inp = tp->t_inpcb; 194 int acknow; 195 struct socket *so; 196 197 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); /* tcp_tw_2msl_reset(). */ 198 INP_WLOCK_ASSERT(inp); 199 200 if (V_nolocaltimewait && in_localip(inp->inp_faddr)) { 201 tp = tcp_close(tp); 202 if (tp != NULL) 203 INP_WUNLOCK(inp); 204 return; 205 } 206 207 tw = uma_zalloc(tcptw_zone, M_NOWAIT); 208 if (tw == NULL) { 209 tw = tcp_tw_2msl_scan(1); 210 if (tw == NULL) { 211 tp = tcp_close(tp); 212 if (tp != NULL) 213 INP_WUNLOCK(inp); 214 return; 215 } 216 } 217 tw->tw_inpcb = inp; 218 219 /* 220 * Recover last window size sent. 221 */ 222 tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale; 223 224 /* 225 * Set t_recent if timestamps are used on the connection. 226 */ 227 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 228 (TF_REQ_TSTMP|TF_RCVD_TSTMP)) { 229 tw->t_recent = tp->ts_recent; 230 tw->ts_offset = tp->ts_offset; 231 } else { 232 tw->t_recent = 0; 233 tw->ts_offset = 0; 234 } 235 236 tw->snd_nxt = tp->snd_nxt; 237 tw->rcv_nxt = tp->rcv_nxt; 238 tw->iss = tp->iss; 239 tw->irs = tp->irs; 240 tw->t_starttime = tp->t_starttime; 241 tw->tw_time = 0; 242 243 /* XXX 244 * If this code will 245 * be used for fin-wait-2 state also, then we may need 246 * a ts_recent from the last segment. 247 */ 248 acknow = tp->t_flags & TF_ACKNOW; 249 250 /* 251 * First, discard tcpcb state, which includes stopping its timers and 252 * freeing it. tcp_discardcb() used to also release the inpcb, but 253 * that work is now done in the caller. 254 * 255 * Note: soisdisconnected() call used to be made in tcp_discardcb(), 256 * and might not be needed here any longer. 257 */ 258 tcp_discardcb(tp); 259 so = inp->inp_socket; 260 soisdisconnected(so); 261 tw->tw_cred = crhold(so->so_cred); 262 SOCK_LOCK(so); 263 tw->tw_so_options = so->so_options; 264 SOCK_UNLOCK(so); 265 if (acknow) 266 tcp_twrespond(tw, TH_ACK); 267 inp->inp_ppcb = tw; 268 inp->inp_vflag |= INP_TIMEWAIT; 269 tcp_tw_2msl_reset(tw, 0); 270 271 /* 272 * If the inpcb owns the sole reference to the socket, then we can 273 * detach and free the socket as it is not needed in time wait. 274 */ 275 if (inp->inp_vflag & INP_SOCKREF) { 276 KASSERT(so->so_state & SS_PROTOREF, 277 ("tcp_twstart: !SS_PROTOREF")); 278 inp->inp_vflag &= ~INP_SOCKREF; 279 INP_WUNLOCK(inp); 280 ACCEPT_LOCK(); 281 SOCK_LOCK(so); 282 so->so_state &= ~SS_PROTOREF; 283 sofree(so); 284 } else 285 INP_WUNLOCK(inp); 286 } 287 288 #if 0 289 /* 290 * The appromixate rate of ISN increase of Microsoft TCP stacks; 291 * the actual rate is slightly higher due to the addition of 292 * random positive increments. 293 * 294 * Most other new OSes use semi-randomized ISN values, so we 295 * do not need to worry about them. 296 */ 297 #define MS_ISN_BYTES_PER_SECOND 250000 298 299 /* 300 * Determine if the ISN we will generate has advanced beyond the last 301 * sequence number used by the previous connection. If so, indicate 302 * that it is safe to recycle this tw socket by returning 1. 303 */ 304 int 305 tcp_twrecycleable(struct tcptw *tw) 306 { 307 INIT_VNET_INET(curvnet); 308 tcp_seq new_iss = tw->iss; 309 tcp_seq new_irs = tw->irs; 310 311 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 312 new_iss += (ticks - tw->t_starttime) * (ISN_BYTES_PER_SECOND / hz); 313 new_irs += (ticks - tw->t_starttime) * (MS_ISN_BYTES_PER_SECOND / hz); 314 315 if (SEQ_GT(new_iss, tw->snd_nxt) && SEQ_GT(new_irs, tw->rcv_nxt)) 316 return (1); 317 else 318 return (0); 319 } 320 #endif 321 322 /* 323 * Returns 1 if the TIME_WAIT state was killed and we should start over, 324 * looking for a pcb in the listen state. Returns 0 otherwise. 325 */ 326 int 327 tcp_twcheck(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th, 328 struct mbuf *m, int tlen) 329 { 330 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 331 INIT_VNET_INET(curvnet); 332 #endif 333 struct tcptw *tw; 334 int thflags; 335 tcp_seq seq; 336 337 /* tcbinfo lock required for tcp_twclose(), tcp_tw_2msl_reset(). */ 338 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 339 INP_WLOCK_ASSERT(inp); 340 341 /* 342 * XXXRW: Time wait state for inpcb has been recycled, but inpcb is 343 * still present. This is undesirable, but temporarily necessary 344 * until we work out how to handle inpcb's who's timewait state has 345 * been removed. 346 */ 347 tw = intotw(inp); 348 if (tw == NULL) 349 goto drop; 350 351 thflags = th->th_flags; 352 353 /* 354 * NOTE: for FIN_WAIT_2 (to be added later), 355 * must validate sequence number before accepting RST 356 */ 357 358 /* 359 * If the segment contains RST: 360 * Drop the segment - see Stevens, vol. 2, p. 964 and 361 * RFC 1337. 362 */ 363 if (thflags & TH_RST) 364 goto drop; 365 366 #if 0 367 /* PAWS not needed at the moment */ 368 /* 369 * RFC 1323 PAWS: If we have a timestamp reply on this segment 370 * and it's less than ts_recent, drop it. 371 */ 372 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 373 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 374 if ((thflags & TH_ACK) == 0) 375 goto drop; 376 goto ack; 377 } 378 /* 379 * ts_recent is never updated because we never accept new segments. 380 */ 381 #endif 382 383 /* 384 * If a new connection request is received 385 * while in TIME_WAIT, drop the old connection 386 * and start over if the sequence numbers 387 * are above the previous ones. 388 */ 389 if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 390 tcp_twclose(tw, 0); 391 return (1); 392 } 393 394 /* 395 * Drop the the segment if it does not contain an ACK. 396 */ 397 if ((thflags & TH_ACK) == 0) 398 goto drop; 399 400 /* 401 * Reset the 2MSL timer if this is a duplicate FIN. 402 */ 403 if (thflags & TH_FIN) { 404 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 405 if (seq + 1 == tw->rcv_nxt) 406 tcp_tw_2msl_reset(tw, 1); 407 } 408 409 /* 410 * Acknowledge the segment if it has data or is not a duplicate ACK. 411 */ 412 if (thflags != TH_ACK || tlen != 0 || 413 th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 414 tcp_twrespond(tw, TH_ACK); 415 drop: 416 INP_WUNLOCK(inp); 417 m_freem(m); 418 return (0); 419 } 420 421 void 422 tcp_twclose(struct tcptw *tw, int reuse) 423 { 424 INIT_VNET_INET(curvnet); 425 struct socket *so; 426 struct inpcb *inp; 427 428 /* 429 * At this point, we are in one of two situations: 430 * 431 * (1) We have no socket, just an inpcb<->twtcp pair. We can free 432 * all state. 433 * 434 * (2) We have a socket -- if we own a reference, release it and 435 * notify the socket layer. 436 */ 437 inp = tw->tw_inpcb; 438 KASSERT((inp->inp_vflag & INP_TIMEWAIT), ("tcp_twclose: !timewait")); 439 KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw")); 440 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); /* tcp_tw_2msl_stop(). */ 441 INP_WLOCK_ASSERT(inp); 442 443 tw->tw_inpcb = NULL; 444 tcp_tw_2msl_stop(tw); 445 inp->inp_ppcb = NULL; 446 in_pcbdrop(inp); 447 448 so = inp->inp_socket; 449 if (so != NULL) { 450 /* 451 * If there's a socket, handle two cases: first, we own a 452 * strong reference, which we will now release, or we don't 453 * in which case another reference exists (XXXRW: think 454 * about this more), and we don't need to take action. 455 */ 456 if (inp->inp_vflag & INP_SOCKREF) { 457 inp->inp_vflag &= ~INP_SOCKREF; 458 INP_WUNLOCK(inp); 459 ACCEPT_LOCK(); 460 SOCK_LOCK(so); 461 KASSERT(so->so_state & SS_PROTOREF, 462 ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF")); 463 so->so_state &= ~SS_PROTOREF; 464 sofree(so); 465 } else { 466 /* 467 * If we don't own the only reference, the socket and 468 * inpcb need to be left around to be handled by 469 * tcp_usr_detach() later. 470 */ 471 INP_WUNLOCK(inp); 472 } 473 } else 474 in_pcbfree(inp); 475 V_tcpstat.tcps_closed++; 476 crfree(tw->tw_cred); 477 tw->tw_cred = NULL; 478 if (reuse) 479 return; 480 uma_zfree(tcptw_zone, tw); 481 } 482 483 int 484 tcp_twrespond(struct tcptw *tw, int flags) 485 { 486 INIT_VNET_INET(curvnet); 487 struct inpcb *inp = tw->tw_inpcb; 488 struct tcphdr *th; 489 struct mbuf *m; 490 struct ip *ip = NULL; 491 u_int hdrlen, optlen; 492 int error; 493 struct tcpopt to; 494 #ifdef INET6 495 struct ip6_hdr *ip6 = NULL; 496 int isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6; 497 #endif 498 499 INP_WLOCK_ASSERT(inp); 500 501 m = m_gethdr(M_DONTWAIT, MT_DATA); 502 if (m == NULL) 503 return (ENOBUFS); 504 m->m_data += max_linkhdr; 505 506 #ifdef MAC 507 mac_inpcb_create_mbuf(inp, m); 508 #endif 509 510 #ifdef INET6 511 if (isipv6) { 512 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 513 ip6 = mtod(m, struct ip6_hdr *); 514 th = (struct tcphdr *)(ip6 + 1); 515 tcpip_fillheaders(inp, ip6, th); 516 } else 517 #endif 518 { 519 hdrlen = sizeof(struct tcpiphdr); 520 ip = mtod(m, struct ip *); 521 th = (struct tcphdr *)(ip + 1); 522 tcpip_fillheaders(inp, ip, th); 523 } 524 to.to_flags = 0; 525 526 /* 527 * Send a timestamp and echo-reply if both our side and our peer 528 * have sent timestamps in our SYN's and this is not a RST. 529 */ 530 if (tw->t_recent && flags == TH_ACK) { 531 to.to_flags |= TOF_TS; 532 to.to_tsval = ticks + tw->ts_offset; 533 to.to_tsecr = tw->t_recent; 534 } 535 optlen = tcp_addoptions(&to, (u_char *)(th + 1)); 536 537 m->m_len = hdrlen + optlen; 538 m->m_pkthdr.len = m->m_len; 539 540 KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small")); 541 542 th->th_seq = htonl(tw->snd_nxt); 543 th->th_ack = htonl(tw->rcv_nxt); 544 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 545 th->th_flags = flags; 546 th->th_win = htons(tw->last_win); 547 548 #ifdef INET6 549 if (isipv6) { 550 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 551 sizeof(struct tcphdr) + optlen); 552 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 553 error = ip6_output(m, inp->in6p_outputopts, NULL, 554 (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp); 555 } else 556 #endif 557 { 558 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 559 htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP)); 560 m->m_pkthdr.csum_flags = CSUM_TCP; 561 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 562 ip->ip_len = m->m_pkthdr.len; 563 if (V_path_mtu_discovery) 564 ip->ip_off |= IP_DF; 565 error = ip_output(m, inp->inp_options, NULL, 566 ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 567 NULL, inp); 568 } 569 if (flags & TH_ACK) 570 V_tcpstat.tcps_sndacks++; 571 else 572 V_tcpstat.tcps_sndctrl++; 573 V_tcpstat.tcps_sndtotal++; 574 return (error); 575 } 576 577 static void 578 tcp_tw_2msl_reset(struct tcptw *tw, int rearm) 579 { 580 INIT_VNET_INET(curvnet); 581 582 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 583 INP_WLOCK_ASSERT(tw->tw_inpcb); 584 if (rearm) 585 TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl); 586 tw->tw_time = ticks + 2 * tcp_msl; 587 TAILQ_INSERT_TAIL(&V_twq_2msl, tw, tw_2msl); 588 } 589 590 static void 591 tcp_tw_2msl_stop(struct tcptw *tw) 592 { 593 INIT_VNET_INET(curvnet); 594 595 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 596 TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl); 597 } 598 599 struct tcptw * 600 tcp_tw_2msl_scan(int reuse) 601 { 602 INIT_VNET_INET(curvnet); 603 struct tcptw *tw; 604 605 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 606 for (;;) { 607 tw = TAILQ_FIRST(&V_twq_2msl); 608 if (tw == NULL || (!reuse && tw->tw_time > ticks)) 609 break; 610 INP_WLOCK(tw->tw_inpcb); 611 tcp_twclose(tw, reuse); 612 if (reuse) 613 return (tw); 614 } 615 return (NULL); 616 } 617