tcp_timewait.c revision 264356
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: head/sys/netinet/tcp_timewait.c 264356 2014-04-11 21:00:59Z jhb $"); 34 35#include "opt_inet.h" 36#include "opt_inet6.h" 37#include "opt_tcpdebug.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/callout.h> 42#include <sys/kernel.h> 43#include <sys/sysctl.h> 44#include <sys/malloc.h> 45#include <sys/mbuf.h> 46#include <sys/priv.h> 47#include <sys/proc.h> 48#include <sys/socket.h> 49#include <sys/socketvar.h> 50#include <sys/protosw.h> 51#include <sys/random.h> 52#include <sys/refcount.h> 53 54#include <vm/uma.h> 55 56#include <net/route.h> 57#include <net/if.h> 58#include <net/if_var.h> 59#include <net/vnet.h> 60 61#include <netinet/in.h> 62#include <netinet/in_pcb.h> 63#include <netinet/in_systm.h> 64#include <netinet/in_var.h> 65#include <netinet/ip.h> 66#include <netinet/ip_icmp.h> 67#include <netinet/ip_var.h> 68#ifdef INET6 69#include <netinet/ip6.h> 70#include <netinet6/in6_pcb.h> 71#include <netinet6/ip6_var.h> 72#include <netinet6/scope6_var.h> 73#include <netinet6/nd6.h> 74#endif 75#include <netinet/tcp.h> 76#include <netinet/tcp_fsm.h> 77#include <netinet/tcp_seq.h> 78#include <netinet/tcp_timer.h> 79#include <netinet/tcp_var.h> 80#ifdef INET6 81#include <netinet6/tcp6_var.h> 82#endif 83#include <netinet/tcpip.h> 84#ifdef TCPDEBUG 85#include <netinet/tcp_debug.h> 86#endif 87#ifdef INET6 88#include <netinet6/ip6protosw.h> 89#endif 90 91#include <machine/in_cksum.h> 92 93#include <security/mac/mac_framework.h> 94 95static VNET_DEFINE(uma_zone_t, tcptw_zone); 96#define V_tcptw_zone VNET(tcptw_zone) 97static int maxtcptw; 98 99/* 100 * The timed wait queue contains references to each of the TCP sessions 101 * currently in the TIME_WAIT state. The queue pointers, including the 102 * queue pointers in each tcptw structure, are protected using the global 103 * timewait lock, which must be held over queue iteration and modification. 104 */ 105static VNET_DEFINE(TAILQ_HEAD(, tcptw), twq_2msl); 106#define V_twq_2msl VNET(twq_2msl) 107 108/* Global timewait lock */ 109static VNET_DEFINE(struct rwlock, tw_lock); 110#define V_tw_lock VNET(tw_lock) 111 112#define TW_LOCK_INIT(tw, d) rw_init_flags(&(tw), (d), 0) 113#define TW_LOCK_DESTROY(tw) rw_destroy(&(tw)) 114#define TW_RLOCK(tw) rw_rlock(&(tw)) 115#define TW_WLOCK(tw) rw_wlock(&(tw)) 116#define TW_RUNLOCK(tw) rw_runlock(&(tw)) 117#define TW_WUNLOCK(tw) rw_wunlock(&(tw)) 118#define TW_LOCK_ASSERT(tw) rw_assert(&(tw), RA_LOCKED) 119#define TW_RLOCK_ASSERT(tw) rw_assert(&(tw), RA_RLOCKED) 120#define TW_WLOCK_ASSERT(tw) rw_assert(&(tw), RA_WLOCKED) 121#define TW_UNLOCK_ASSERT(tw) rw_assert(&(tw), RA_UNLOCKED) 122 123static void tcp_tw_2msl_reset(struct tcptw *, int); 124static void tcp_tw_2msl_stop(struct tcptw *, int); 125 126/* 127 * tw_pcbref() bumps the reference count on an tw in order to maintain 128 * stability of an tw pointer despite the tw lock being released. 129 */ 130static void 131tw_pcbref(struct tcptw *tw) 132{ 133 134 KASSERT(tw->tw_refcount > 0, ("%s: refcount 0", __func__)); 135 refcount_acquire(&tw->tw_refcount); 136} 137 138/* 139 * Drop a refcount on an tw elevated using tw_pcbref(). 140 */ 141static int 142tw_pcbrele(struct tcptw *tw) 143{ 144 145 KASSERT(tw->tw_refcount > 0, ("%s: refcount 0", __func__)); 146 if (!refcount_release(&tw->tw_refcount)) 147 return (0); 148 uma_zfree(V_tcptw_zone, tw); 149 return (1); 150} 151 152static int 153tcptw_auto_size(void) 154{ 155 int halfrange; 156 157 /* 158 * Max out at half the ephemeral port range so that TIME_WAIT 159 * sockets don't tie up too many ephemeral ports. 160 */ 161 if (V_ipport_lastauto > V_ipport_firstauto) 162 halfrange = (V_ipport_lastauto - V_ipport_firstauto) / 2; 163 else 164 halfrange = (V_ipport_firstauto - V_ipport_lastauto) / 2; 165 /* Protect against goofy port ranges smaller than 32. */ 166 return (imin(imax(halfrange, 32), maxsockets / 5)); 167} 168 169static int 170sysctl_maxtcptw(SYSCTL_HANDLER_ARGS) 171{ 172 int error, new; 173 174 if (maxtcptw == 0) 175 new = tcptw_auto_size(); 176 else 177 new = maxtcptw; 178 error = sysctl_handle_int(oidp, &new, 0, req); 179 if (error == 0 && req->newptr) 180 if (new >= 32) { 181 maxtcptw = new; 182 uma_zone_set_max(V_tcptw_zone, maxtcptw); 183 } 184 return (error); 185} 186 187SYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxtcptw, CTLTYPE_INT|CTLFLAG_RW, 188 &maxtcptw, 0, sysctl_maxtcptw, "IU", 189 "Maximum number of compressed TCP TIME_WAIT entries"); 190 191VNET_DEFINE(int, nolocaltimewait) = 0; 192#define V_nolocaltimewait VNET(nolocaltimewait) 193SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, nolocaltimewait, CTLFLAG_RW, 194 &VNET_NAME(nolocaltimewait), 0, 195 "Do not create compressed TCP TIME_WAIT entries for local connections"); 196 197void 198tcp_tw_zone_change(void) 199{ 200 201 if (maxtcptw == 0) 202 uma_zone_set_max(V_tcptw_zone, tcptw_auto_size()); 203} 204 205void 206tcp_tw_init(void) 207{ 208 209 V_tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw), 210 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 211 TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw); 212 if (maxtcptw == 0) 213 uma_zone_set_max(V_tcptw_zone, tcptw_auto_size()); 214 else 215 uma_zone_set_max(V_tcptw_zone, maxtcptw); 216 TAILQ_INIT(&V_twq_2msl); 217 TW_LOCK_INIT(V_tw_lock, "tcptw"); 218} 219 220#ifdef VIMAGE 221void 222tcp_tw_destroy(void) 223{ 224 struct tcptw *tw; 225 226 INP_INFO_WLOCK(&V_tcbinfo); 227 while ((tw = TAILQ_FIRST(&V_twq_2msl)) != NULL) 228 tcp_twclose(tw, 0); 229 INP_INFO_WUNLOCK(&V_tcbinfo); 230 231 TW_LOCK_DESTROY(V_tw_lock); 232 uma_zdestroy(V_tcptw_zone); 233} 234#endif 235 236/* 237 * Move a TCP connection into TIME_WAIT state. 238 * tcbinfo is locked. 239 * inp is locked, and is unlocked before returning. 240 */ 241void 242tcp_twstart(struct tcpcb *tp) 243{ 244 struct tcptw *tw; 245 struct inpcb *inp = tp->t_inpcb; 246 int acknow; 247 struct socket *so; 248#ifdef INET6 249 int isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6; 250#endif 251 252 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 253 INP_WLOCK_ASSERT(inp); 254 255 if (V_nolocaltimewait) { 256 int error = 0; 257#ifdef INET6 258 if (isipv6) 259 error = in6_localaddr(&inp->in6p_faddr); 260#endif 261#if defined(INET6) && defined(INET) 262 else 263#endif 264#ifdef INET 265 error = in_localip(inp->inp_faddr); 266#endif 267 if (error) { 268 tp = tcp_close(tp); 269 if (tp != NULL) 270 INP_WUNLOCK(inp); 271 return; 272 } 273 } 274 275 tw = uma_zalloc(V_tcptw_zone, M_NOWAIT); 276 if (tw == NULL) { 277 tw = tcp_tw_2msl_reuse(); 278 if (tw == NULL) { 279 tp = tcp_close(tp); 280 if (tp != NULL) 281 INP_WUNLOCK(inp); 282 return; 283 } 284 } 285 tw->tw_inpcb = inp; 286 refcount_init(&tw->tw_refcount, 1); 287 288 /* 289 * Recover last window size sent. 290 */ 291 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 292 tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale; 293 else 294 tw->last_win = 0; 295 296 /* 297 * Set t_recent if timestamps are used on the connection. 298 */ 299 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 300 (TF_REQ_TSTMP|TF_RCVD_TSTMP)) { 301 tw->t_recent = tp->ts_recent; 302 tw->ts_offset = tp->ts_offset; 303 } else { 304 tw->t_recent = 0; 305 tw->ts_offset = 0; 306 } 307 308 tw->snd_nxt = tp->snd_nxt; 309 tw->rcv_nxt = tp->rcv_nxt; 310 tw->iss = tp->iss; 311 tw->irs = tp->irs; 312 tw->t_starttime = tp->t_starttime; 313 tw->tw_time = 0; 314 315/* XXX 316 * If this code will 317 * be used for fin-wait-2 state also, then we may need 318 * a ts_recent from the last segment. 319 */ 320 acknow = tp->t_flags & TF_ACKNOW; 321 322 /* 323 * First, discard tcpcb state, which includes stopping its timers and 324 * freeing it. tcp_discardcb() used to also release the inpcb, but 325 * that work is now done in the caller. 326 * 327 * Note: soisdisconnected() call used to be made in tcp_discardcb(), 328 * and might not be needed here any longer. 329 */ 330 tcp_discardcb(tp); 331 so = inp->inp_socket; 332 soisdisconnected(so); 333 tw->tw_cred = crhold(so->so_cred); 334 SOCK_LOCK(so); 335 tw->tw_so_options = so->so_options; 336 SOCK_UNLOCK(so); 337 if (acknow) 338 tcp_twrespond(tw, TH_ACK); 339 inp->inp_ppcb = tw; 340 inp->inp_flags |= INP_TIMEWAIT; 341 tcp_tw_2msl_reset(tw, 0); 342 343 /* 344 * If the inpcb owns the sole reference to the socket, then we can 345 * detach and free the socket as it is not needed in time wait. 346 */ 347 if (inp->inp_flags & INP_SOCKREF) { 348 KASSERT(so->so_state & SS_PROTOREF, 349 ("tcp_twstart: !SS_PROTOREF")); 350 inp->inp_flags &= ~INP_SOCKREF; 351 INP_WUNLOCK(inp); 352 ACCEPT_LOCK(); 353 SOCK_LOCK(so); 354 so->so_state &= ~SS_PROTOREF; 355 sofree(so); 356 } else 357 INP_WUNLOCK(inp); 358} 359 360#if 0 361/* 362 * The appromixate rate of ISN increase of Microsoft TCP stacks; 363 * the actual rate is slightly higher due to the addition of 364 * random positive increments. 365 * 366 * Most other new OSes use semi-randomized ISN values, so we 367 * do not need to worry about them. 368 */ 369#define MS_ISN_BYTES_PER_SECOND 250000 370 371/* 372 * Determine if the ISN we will generate has advanced beyond the last 373 * sequence number used by the previous connection. If so, indicate 374 * that it is safe to recycle this tw socket by returning 1. 375 */ 376int 377tcp_twrecycleable(struct tcptw *tw) 378{ 379 tcp_seq new_iss = tw->iss; 380 tcp_seq new_irs = tw->irs; 381 382 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 383 new_iss += (ticks - tw->t_starttime) * (ISN_BYTES_PER_SECOND / hz); 384 new_irs += (ticks - tw->t_starttime) * (MS_ISN_BYTES_PER_SECOND / hz); 385 386 if (SEQ_GT(new_iss, tw->snd_nxt) && SEQ_GT(new_irs, tw->rcv_nxt)) 387 return (1); 388 else 389 return (0); 390} 391#endif 392 393/* 394 * Returns 1 if the TIME_WAIT state was killed and we should start over, 395 * looking for a pcb in the listen state. Returns 0 otherwise. 396 */ 397int 398tcp_twcheck(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th, 399 struct mbuf *m, int tlen) 400{ 401 struct tcptw *tw; 402 int thflags; 403 tcp_seq seq; 404 405 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 406 INP_WLOCK_ASSERT(inp); 407 408 /* 409 * XXXRW: Time wait state for inpcb has been recycled, but inpcb is 410 * still present. This is undesirable, but temporarily necessary 411 * until we work out how to handle inpcb's who's timewait state has 412 * been removed. 413 */ 414 tw = intotw(inp); 415 if (tw == NULL) 416 goto drop; 417 418 thflags = th->th_flags; 419 420 /* 421 * NOTE: for FIN_WAIT_2 (to be added later), 422 * must validate sequence number before accepting RST 423 */ 424 425 /* 426 * If the segment contains RST: 427 * Drop the segment - see Stevens, vol. 2, p. 964 and 428 * RFC 1337. 429 */ 430 if (thflags & TH_RST) 431 goto drop; 432 433#if 0 434/* PAWS not needed at the moment */ 435 /* 436 * RFC 1323 PAWS: If we have a timestamp reply on this segment 437 * and it's less than ts_recent, drop it. 438 */ 439 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 440 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 441 if ((thflags & TH_ACK) == 0) 442 goto drop; 443 goto ack; 444 } 445 /* 446 * ts_recent is never updated because we never accept new segments. 447 */ 448#endif 449 450 /* 451 * If a new connection request is received 452 * while in TIME_WAIT, drop the old connection 453 * and start over if the sequence numbers 454 * are above the previous ones. 455 */ 456 if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 457 tcp_twclose(tw, 0); 458 return (1); 459 } 460 461 /* 462 * Drop the segment if it does not contain an ACK. 463 */ 464 if ((thflags & TH_ACK) == 0) 465 goto drop; 466 467 /* 468 * Reset the 2MSL timer if this is a duplicate FIN. 469 */ 470 if (thflags & TH_FIN) { 471 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 472 if (seq + 1 == tw->rcv_nxt) 473 tcp_tw_2msl_reset(tw, 1); 474 } 475 476 /* 477 * Acknowledge the segment if it has data or is not a duplicate ACK. 478 */ 479 if (thflags != TH_ACK || tlen != 0 || 480 th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 481 tcp_twrespond(tw, TH_ACK); 482drop: 483 INP_WUNLOCK(inp); 484 m_freem(m); 485 return (0); 486} 487 488void 489tcp_twclose(struct tcptw *tw, int reuse) 490{ 491 struct socket *so; 492 struct inpcb *inp; 493 494 /* 495 * At this point, we are in one of two situations: 496 * 497 * (1) We have no socket, just an inpcb<->twtcp pair. We can free 498 * all state. 499 * 500 * (2) We have a socket -- if we own a reference, release it and 501 * notify the socket layer. 502 */ 503 inp = tw->tw_inpcb; 504 KASSERT((inp->inp_flags & INP_TIMEWAIT), ("tcp_twclose: !timewait")); 505 KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw")); 506 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); /* in_pcbfree() */ 507 INP_WLOCK_ASSERT(inp); 508 509 tw->tw_inpcb = NULL; 510 tcp_tw_2msl_stop(tw, reuse); 511 inp->inp_ppcb = NULL; 512 in_pcbdrop(inp); 513 514 so = inp->inp_socket; 515 if (so != NULL) { 516 /* 517 * If there's a socket, handle two cases: first, we own a 518 * strong reference, which we will now release, or we don't 519 * in which case another reference exists (XXXRW: think 520 * about this more), and we don't need to take action. 521 */ 522 if (inp->inp_flags & INP_SOCKREF) { 523 inp->inp_flags &= ~INP_SOCKREF; 524 INP_WUNLOCK(inp); 525 ACCEPT_LOCK(); 526 SOCK_LOCK(so); 527 KASSERT(so->so_state & SS_PROTOREF, 528 ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF")); 529 so->so_state &= ~SS_PROTOREF; 530 sofree(so); 531 } else { 532 /* 533 * If we don't own the only reference, the socket and 534 * inpcb need to be left around to be handled by 535 * tcp_usr_detach() later. 536 */ 537 INP_WUNLOCK(inp); 538 } 539 } else 540 in_pcbfree(inp); 541 TCPSTAT_INC(tcps_closed); 542} 543 544int 545tcp_twrespond(struct tcptw *tw, int flags) 546{ 547 struct inpcb *inp = tw->tw_inpcb; 548#if defined(INET6) || defined(INET) 549 struct tcphdr *th = NULL; 550#endif 551 struct mbuf *m; 552#ifdef INET 553 struct ip *ip = NULL; 554#endif 555 u_int hdrlen, optlen; 556 int error = 0; /* Keep compiler happy */ 557 struct tcpopt to; 558#ifdef INET6 559 struct ip6_hdr *ip6 = NULL; 560 int isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6; 561#endif 562 hdrlen = 0; /* Keep compiler happy */ 563 564 INP_WLOCK_ASSERT(inp); 565 566 m = m_gethdr(M_NOWAIT, MT_DATA); 567 if (m == NULL) 568 return (ENOBUFS); 569 m->m_data += max_linkhdr; 570 571#ifdef MAC 572 mac_inpcb_create_mbuf(inp, m); 573#endif 574 575#ifdef INET6 576 if (isipv6) { 577 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 578 ip6 = mtod(m, struct ip6_hdr *); 579 th = (struct tcphdr *)(ip6 + 1); 580 tcpip_fillheaders(inp, ip6, th); 581 } 582#endif 583#if defined(INET6) && defined(INET) 584 else 585#endif 586#ifdef INET 587 { 588 hdrlen = sizeof(struct tcpiphdr); 589 ip = mtod(m, struct ip *); 590 th = (struct tcphdr *)(ip + 1); 591 tcpip_fillheaders(inp, ip, th); 592 } 593#endif 594 to.to_flags = 0; 595 596 /* 597 * Send a timestamp and echo-reply if both our side and our peer 598 * have sent timestamps in our SYN's and this is not a RST. 599 */ 600 if (tw->t_recent && flags == TH_ACK) { 601 to.to_flags |= TOF_TS; 602 to.to_tsval = tcp_ts_getticks() + tw->ts_offset; 603 to.to_tsecr = tw->t_recent; 604 } 605 optlen = tcp_addoptions(&to, (u_char *)(th + 1)); 606 607 m->m_len = hdrlen + optlen; 608 m->m_pkthdr.len = m->m_len; 609 610 KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small")); 611 612 th->th_seq = htonl(tw->snd_nxt); 613 th->th_ack = htonl(tw->rcv_nxt); 614 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 615 th->th_flags = flags; 616 th->th_win = htons(tw->last_win); 617 618 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 619#ifdef INET6 620 if (isipv6) { 621 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 622 th->th_sum = in6_cksum_pseudo(ip6, 623 sizeof(struct tcphdr) + optlen, IPPROTO_TCP, 0); 624 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 625 error = ip6_output(m, inp->in6p_outputopts, NULL, 626 (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp); 627 } 628#endif 629#if defined(INET6) && defined(INET) 630 else 631#endif 632#ifdef INET 633 { 634 m->m_pkthdr.csum_flags = CSUM_TCP; 635 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 636 htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP)); 637 ip->ip_len = htons(m->m_pkthdr.len); 638 if (V_path_mtu_discovery) 639 ip->ip_off |= htons(IP_DF); 640 error = ip_output(m, inp->inp_options, NULL, 641 ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 642 NULL, inp); 643 } 644#endif 645 if (flags & TH_ACK) 646 TCPSTAT_INC(tcps_sndacks); 647 else 648 TCPSTAT_INC(tcps_sndctrl); 649 TCPSTAT_INC(tcps_sndtotal); 650 return (error); 651} 652 653static void 654tcp_tw_2msl_reset(struct tcptw *tw, int rearm) 655{ 656 657 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 658 INP_WLOCK_ASSERT(tw->tw_inpcb); 659 660 TW_WLOCK(V_tw_lock); 661 if (rearm) 662 TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl); 663 tw->tw_time = ticks + 2 * tcp_msl; 664 TAILQ_INSERT_TAIL(&V_twq_2msl, tw, tw_2msl); 665 TW_WUNLOCK(V_tw_lock); 666} 667 668static void 669tcp_tw_2msl_stop(struct tcptw *tw, int reuse) 670{ 671 672 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 673 674 TW_WLOCK(V_tw_lock); 675 TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl); 676 crfree(tw->tw_cred); 677 tw->tw_cred = NULL; 678 TW_WUNLOCK(V_tw_lock); 679 680 if (!reuse) 681 tw_pcbrele(tw); 682} 683 684struct tcptw * 685tcp_tw_2msl_reuse(void) 686{ 687 struct tcptw *tw; 688 689 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 690 691 TW_WLOCK(V_tw_lock); 692 tw = TAILQ_FIRST(&V_twq_2msl); 693 if (tw == NULL) { 694 TW_WUNLOCK(V_tw_lock); 695 return NULL; 696 } 697 TW_WUNLOCK(V_tw_lock); 698 699 INP_WLOCK(tw->tw_inpcb); 700 tcp_twclose(tw, 1); 701 702 return (tw); 703} 704 705void 706tcp_tw_2msl_scan(void) 707{ 708 struct tcptw *tw; 709 710 for (;;) { 711 TW_RLOCK(V_tw_lock); 712 tw = TAILQ_FIRST(&V_twq_2msl); 713 if (tw == NULL || tw->tw_time - ticks > 0) { 714 TW_RUNLOCK(V_tw_lock); 715 break; 716 } 717 tw_pcbref(tw); 718 TW_RUNLOCK(V_tw_lock); 719 720 /* Close timewait state */ 721 if (INP_INFO_TRY_WLOCK(&V_tcbinfo)) { 722 if (tw_pcbrele(tw)) { 723 INP_INFO_WUNLOCK(&V_tcbinfo); 724 continue; 725 } 726 727 KASSERT(tw->tw_inpcb != NULL, 728 ("%s: tw->tw_inpcb == NULL", __func__)); 729 INP_WLOCK(tw->tw_inpcb); 730 tcp_twclose(tw, 0); 731 INP_INFO_WUNLOCK(&V_tcbinfo); 732 } else { 733 /* INP_INFO lock is busy; continue later. */ 734 tw_pcbrele(tw); 735 break; 736 } 737 } 738} 739