tcp_timewait.c revision 169482
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 * $FreeBSD: head/sys/netinet/tcp_timewait.c 169482 2007-05-11 21:17:53Z andre $ 31 */ 32 33#include "opt_compat.h" 34#include "opt_inet.h" 35#include "opt_inet6.h" 36#include "opt_ipsec.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#ifdef INET6 48#include <sys/domain.h> 49#endif 50#include <sys/priv.h> 51#include <sys/proc.h> 52#include <sys/socket.h> 53#include <sys/socketvar.h> 54#include <sys/protosw.h> 55#include <sys/random.h> 56 57#include <vm/uma.h> 58 59#include <net/route.h> 60#include <net/if.h> 61 62#include <netinet/in.h> 63#include <netinet/in_systm.h> 64#include <netinet/ip.h> 65#ifdef INET6 66#include <netinet/ip6.h> 67#endif 68#include <netinet/in_pcb.h> 69#ifdef INET6 70#include <netinet6/in6_pcb.h> 71#endif 72#include <netinet/in_var.h> 73#include <netinet/ip_var.h> 74#ifdef INET6 75#include <netinet6/ip6_var.h> 76#include <netinet6/scope6_var.h> 77#include <netinet6/nd6.h> 78#endif 79#include <netinet/ip_icmp.h> 80#include <netinet/tcp.h> 81#include <netinet/tcp_fsm.h> 82#include <netinet/tcp_seq.h> 83#include <netinet/tcp_timer.h> 84#include <netinet/tcp_var.h> 85#ifdef INET6 86#include <netinet6/tcp6_var.h> 87#endif 88#include <netinet/tcpip.h> 89#ifdef TCPDEBUG 90#include <netinet/tcp_debug.h> 91#endif 92#include <netinet6/ip6protosw.h> 93 94#ifdef IPSEC 95#include <netinet6/ipsec.h> 96#ifdef INET6 97#include <netinet6/ipsec6.h> 98#endif 99#include <netkey/key.h> 100#endif /*IPSEC*/ 101 102#ifdef FAST_IPSEC 103#include <netipsec/ipsec.h> 104#include <netipsec/xform.h> 105#ifdef INET6 106#include <netipsec/ipsec6.h> 107#endif 108#include <netipsec/key.h> 109#define IPSEC 110#endif /*FAST_IPSEC*/ 111 112#include <machine/in_cksum.h> 113#include <sys/md5.h> 114 115#include <security/mac/mac_framework.h> 116 117static uma_zone_t tcptw_zone; 118static int maxtcptw; 119 120static int 121tcptw_auto_size(void) 122{ 123 int halfrange; 124 125 /* 126 * Max out at half the ephemeral port range so that TIME_WAIT 127 * sockets don't tie up too many ephemeral ports. 128 */ 129 if (ipport_lastauto > ipport_firstauto) 130 halfrange = (ipport_lastauto - ipport_firstauto) / 2; 131 else 132 halfrange = (ipport_firstauto - ipport_lastauto) / 2; 133 /* Protect against goofy port ranges smaller than 32. */ 134 return (imin(imax(halfrange, 32), maxsockets / 5)); 135} 136 137static int 138sysctl_maxtcptw(SYSCTL_HANDLER_ARGS) 139{ 140 int error, new; 141 142 if (maxtcptw == 0) 143 new = tcptw_auto_size(); 144 else 145 new = maxtcptw; 146 error = sysctl_handle_int(oidp, &new, sizeof(int), req); 147 if (error == 0 && req->newptr) 148 if (new >= 32) { 149 maxtcptw = new; 150 uma_zone_set_max(tcptw_zone, maxtcptw); 151 } 152 return (error); 153} 154SYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxtcptw, CTLTYPE_INT|CTLFLAG_RW, 155 &maxtcptw, 0, sysctl_maxtcptw, "IU", 156 "Maximum number of compressed TCP TIME_WAIT entries"); 157 158static int nolocaltimewait = 0; 159SYSCTL_INT(_net_inet_tcp, OID_AUTO, nolocaltimewait, CTLFLAG_RW, 160 &nolocaltimewait, 0, 161 "Do not create compressed TCP TIME_WAIT entries for local connections"); 162 163/* 164 * TCP initialization. 165 */ 166static void 167tcp_zone_change(void *tag) 168{ 169 170 if (maxtcptw == 0) 171 uma_zone_set_max(tcptw_zone, tcptw_auto_size()); 172} 173 174void 175tcp_init(void) 176{ 177 178 tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw), 179 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 180 TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw); 181 if (maxtcptw == 0) 182 uma_zone_set_max(tcptw_zone, tcptw_auto_size()); 183 else 184 uma_zone_set_max(tcptw_zone, maxtcptw); 185} 186 187/* 188 * Move a TCP connection into TIME_WAIT state. 189 * tcbinfo is locked. 190 * inp is locked, and is unlocked before returning. 191 */ 192void 193tcp_twstart(struct tcpcb *tp) 194{ 195 struct tcptw *tw; 196 struct inpcb *inp = tp->t_inpcb; 197 int acknow; 198 struct socket *so; 199 200 INP_INFO_WLOCK_ASSERT(&tcbinfo); /* tcp_timer_2msl_reset(). */ 201 INP_LOCK_ASSERT(inp); 202 203 if (nolocaltimewait && in_localip(inp->inp_faddr)) { 204 tp = tcp_close(tp); 205 if (tp != NULL) 206 INP_UNLOCK(inp); 207 return; 208 } 209 210 tw = uma_zalloc(tcptw_zone, M_NOWAIT); 211 if (tw == NULL) { 212 tw = tcp_timer_2msl_tw(1); 213 if (tw == NULL) { 214 tp = tcp_close(tp); 215 if (tp != NULL) 216 INP_UNLOCK(inp); 217 return; 218 } 219 } 220 tw->tw_inpcb = inp; 221 222 /* 223 * Recover last window size sent. 224 */ 225 tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale; 226 227 /* 228 * Set t_recent if timestamps are used on the connection. 229 */ 230 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 231 (TF_REQ_TSTMP|TF_RCVD_TSTMP)) { 232 tw->t_recent = tp->ts_recent; 233 tw->ts_offset = tp->ts_offset; 234 } else { 235 tw->t_recent = 0; 236 tw->ts_offset = 0; 237 } 238 239 tw->snd_nxt = tp->snd_nxt; 240 tw->rcv_nxt = tp->rcv_nxt; 241 tw->iss = tp->iss; 242 tw->irs = tp->irs; 243 tw->t_starttime = tp->t_starttime; 244 tw->tw_time = 0; 245 246/* XXX 247 * If this code will 248 * be used for fin-wait-2 state also, then we may need 249 * a ts_recent from the last segment. 250 */ 251 acknow = tp->t_flags & TF_ACKNOW; 252 253 /* 254 * First, discard tcpcb state, which includes stopping its timers and 255 * freeing it. tcp_discardcb() used to also release the inpcb, but 256 * that work is now done in the caller. 257 * 258 * Note: soisdisconnected() call used to be made in tcp_discardcb(), 259 * and might not be needed here any longer. 260 */ 261 tcp_discardcb(tp); 262 so = inp->inp_socket; 263 soisdisconnected(so); 264 tw->tw_cred = crhold(so->so_cred); 265 SOCK_LOCK(so); 266 tw->tw_so_options = so->so_options; 267 SOCK_UNLOCK(so); 268 if (acknow) 269 tcp_twrespond(tw, TH_ACK); 270 inp->inp_ppcb = tw; 271 inp->inp_vflag |= INP_TIMEWAIT; 272 tcp_timer_2msl_reset(tw, 0); 273 274 /* 275 * If the inpcb owns the sole reference to the socket, then we can 276 * detach and free the socket as it is not needed in time wait. 277 */ 278 if (inp->inp_vflag & INP_SOCKREF) { 279 KASSERT(so->so_state & SS_PROTOREF, 280 ("tcp_twstart: !SS_PROTOREF")); 281 inp->inp_vflag &= ~INP_SOCKREF; 282 INP_UNLOCK(inp); 283 ACCEPT_LOCK(); 284 SOCK_LOCK(so); 285 so->so_state &= ~SS_PROTOREF; 286 sofree(so); 287 } else 288 INP_UNLOCK(inp); 289} 290 291#if 0 292/* 293 * The appromixate rate of ISN increase of Microsoft TCP stacks; 294 * the actual rate is slightly higher due to the addition of 295 * random positive increments. 296 * 297 * Most other new OSes use semi-randomized ISN values, so we 298 * do not need to worry about them. 299 */ 300#define MS_ISN_BYTES_PER_SECOND 250000 301 302/* 303 * Determine if the ISN we will generate has advanced beyond the last 304 * sequence number used by the previous connection. If so, indicate 305 * that it is safe to recycle this tw socket by returning 1. 306 */ 307int 308tcp_twrecycleable(struct tcptw *tw) 309{ 310 tcp_seq new_iss = tw->iss; 311 tcp_seq new_irs = tw->irs; 312 313 INP_INFO_WLOCK_ASSERT(&tcbinfo); 314 new_iss += (ticks - tw->t_starttime) * (ISN_BYTES_PER_SECOND / hz); 315 new_irs += (ticks - tw->t_starttime) * (MS_ISN_BYTES_PER_SECOND / hz); 316 317 if (SEQ_GT(new_iss, tw->snd_nxt) && SEQ_GT(new_irs, tw->rcv_nxt)) 318 return (1); 319 else 320 return (0); 321} 322#endif 323 324void 325tcp_twclose(struct tcptw *tw, int reuse) 326{ 327 struct socket *so; 328 struct inpcb *inp; 329 330 /* 331 * At this point, we are in one of two situations: 332 * 333 * (1) We have no socket, just an inpcb<->twtcp pair. We can free 334 * all state. 335 * 336 * (2) We have a socket -- if we own a reference, release it and 337 * notify the socket layer. 338 */ 339 inp = tw->tw_inpcb; 340 KASSERT((inp->inp_vflag & INP_TIMEWAIT), ("tcp_twclose: !timewait")); 341 KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw")); 342 INP_INFO_WLOCK_ASSERT(&tcbinfo); /* tcp_timer_2msl_stop(). */ 343 INP_LOCK_ASSERT(inp); 344 345 tw->tw_inpcb = NULL; 346 tcp_timer_2msl_stop(tw); 347 inp->inp_ppcb = NULL; 348 in_pcbdrop(inp); 349 350 so = inp->inp_socket; 351 if (so != NULL) { 352 /* 353 * If there's a socket, handle two cases: first, we own a 354 * strong reference, which we will now release, or we don't 355 * in which case another reference exists (XXXRW: think 356 * about this more), and we don't need to take action. 357 */ 358 if (inp->inp_vflag & INP_SOCKREF) { 359 inp->inp_vflag &= ~INP_SOCKREF; 360 INP_UNLOCK(inp); 361 ACCEPT_LOCK(); 362 SOCK_LOCK(so); 363 KASSERT(so->so_state & SS_PROTOREF, 364 ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF")); 365 so->so_state &= ~SS_PROTOREF; 366 sofree(so); 367 } else { 368 /* 369 * If we don't own the only reference, the socket and 370 * inpcb need to be left around to be handled by 371 * tcp_usr_detach() later. 372 */ 373 INP_UNLOCK(inp); 374 } 375 } else { 376#ifdef INET6 377 if (inp->inp_vflag & INP_IPV6PROTO) 378 in6_pcbfree(inp); 379 else 380#endif 381 in_pcbfree(inp); 382 } 383 tcpstat.tcps_closed++; 384 crfree(tw->tw_cred); 385 tw->tw_cred = NULL; 386 if (reuse) 387 return; 388 uma_zfree(tcptw_zone, tw); 389} 390 391int 392tcp_twrespond(struct tcptw *tw, int flags) 393{ 394 struct inpcb *inp = tw->tw_inpcb; 395 struct tcphdr *th; 396 struct mbuf *m; 397 struct ip *ip = NULL; 398 u_int hdrlen, optlen; 399 int error; 400 struct tcpopt to; 401#ifdef INET6 402 struct ip6_hdr *ip6 = NULL; 403 int isipv6 = inp->inp_inc.inc_isipv6; 404#endif 405 406 INP_LOCK_ASSERT(inp); 407 408 m = m_gethdr(M_DONTWAIT, MT_DATA); 409 if (m == NULL) 410 return (ENOBUFS); 411 m->m_data += max_linkhdr; 412 413#ifdef MAC 414 mac_create_mbuf_from_inpcb(inp, m); 415#endif 416 417#ifdef INET6 418 if (isipv6) { 419 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 420 ip6 = mtod(m, struct ip6_hdr *); 421 th = (struct tcphdr *)(ip6 + 1); 422 tcpip_fillheaders(inp, ip6, th); 423 } else 424#endif 425 { 426 hdrlen = sizeof(struct tcpiphdr); 427 ip = mtod(m, struct ip *); 428 th = (struct tcphdr *)(ip + 1); 429 tcpip_fillheaders(inp, ip, th); 430 } 431 to.to_flags = 0; 432 433 /* 434 * Send a timestamp and echo-reply if both our side and our peer 435 * have sent timestamps in our SYN's and this is not a RST. 436 */ 437 if (tw->t_recent && flags == TH_ACK) { 438 to.to_flags |= TOF_TS; 439 to.to_tsval = ticks + tw->ts_offset; 440 to.to_tsecr = tw->t_recent; 441 } 442 optlen = tcp_addoptions(&to, (u_char *)(th + 1)); 443 444 m->m_len = hdrlen + optlen; 445 m->m_pkthdr.len = m->m_len; 446 447 KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small")); 448 449 th->th_seq = htonl(tw->snd_nxt); 450 th->th_ack = htonl(tw->rcv_nxt); 451 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 452 th->th_flags = flags; 453 th->th_win = htons(tw->last_win); 454 455#ifdef INET6 456 if (isipv6) { 457 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 458 sizeof(struct tcphdr) + optlen); 459 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 460 error = ip6_output(m, inp->in6p_outputopts, NULL, 461 (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp); 462 } else 463#endif 464 { 465 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 466 htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP)); 467 m->m_pkthdr.csum_flags = CSUM_TCP; 468 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 469 ip->ip_len = m->m_pkthdr.len; 470 if (path_mtu_discovery) 471 ip->ip_off |= IP_DF; 472 error = ip_output(m, inp->inp_options, NULL, 473 ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 474 NULL, inp); 475 } 476 if (flags & TH_ACK) 477 tcpstat.tcps_sndacks++; 478 else 479 tcpstat.tcps_sndctrl++; 480 tcpstat.tcps_sndtotal++; 481 return (error); 482} 483