tcp_timewait.c revision 236170
1139823Simp/*- 211150Swollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 2911150Swollman * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 301541Srgrimes */ 311541Srgrimes 32172467Ssilby#include <sys/cdefs.h> 33172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/tcp_timewait.c 236170 2012-05-28 09:30:13Z bz $"); 34172467Ssilby 35125680Sbms#include "opt_inet.h" 3654263Sshin#include "opt_inet6.h" 3729514Sjoerg#include "opt_tcpdebug.h" 3829514Sjoerg 391541Srgrimes#include <sys/param.h> 401541Srgrimes#include <sys/systm.h> 4150673Sjlemon#include <sys/callout.h> 4212172Sphk#include <sys/kernel.h> 4312172Sphk#include <sys/sysctl.h> 441541Srgrimes#include <sys/malloc.h> 451541Srgrimes#include <sys/mbuf.h> 46164033Srwatson#include <sys/priv.h> 4748758Sgreen#include <sys/proc.h> 481541Srgrimes#include <sys/socket.h> 491541Srgrimes#include <sys/socketvar.h> 501541Srgrimes#include <sys/protosw.h> 5175619Skris#include <sys/random.h> 5234923Sbde 5392760Sjeff#include <vm/uma.h> 541541Srgrimes 551541Srgrimes#include <net/route.h> 561541Srgrimes#include <net/if.h> 57196019Srwatson#include <net/vnet.h> 581541Srgrimes 591541Srgrimes#include <netinet/in.h> 60221250Sbz#include <netinet/in_pcb.h> 611541Srgrimes#include <netinet/in_systm.h> 62221250Sbz#include <netinet/in_var.h> 631541Srgrimes#include <netinet/ip.h> 64221250Sbz#include <netinet/ip_icmp.h> 65221250Sbz#include <netinet/ip_var.h> 6655679Sshin#ifdef INET6 6755679Sshin#include <netinet/ip6.h> 6855679Sshin#include <netinet6/in6_pcb.h> 6955679Sshin#include <netinet6/ip6_var.h> 70148385Sume#include <netinet6/scope6_var.h> 71122922Sandre#include <netinet6/nd6.h> 7255679Sshin#endif 731541Srgrimes#include <netinet/tcp.h> 741541Srgrimes#include <netinet/tcp_fsm.h> 751541Srgrimes#include <netinet/tcp_seq.h> 761541Srgrimes#include <netinet/tcp_timer.h> 771541Srgrimes#include <netinet/tcp_var.h> 7855679Sshin#ifdef INET6 7955679Sshin#include <netinet6/tcp6_var.h> 8055679Sshin#endif 811541Srgrimes#include <netinet/tcpip.h> 826283Swollman#ifdef TCPDEBUG 836283Swollman#include <netinet/tcp_debug.h> 846283Swollman#endif 85221250Sbz#ifdef INET6 8655679Sshin#include <netinet6/ip6protosw.h> 87221250Sbz#endif 881541Srgrimes 8958698Sjlemon#include <machine/in_cksum.h> 9058698Sjlemon 91163606Srwatson#include <security/mac/mac_framework.h> 92163606Srwatson 93215701Sdimstatic VNET_DEFINE(uma_zone_t, tcptw_zone); 94207369Sbz#define V_tcptw_zone VNET(tcptw_zone) 95157478Sglebiusstatic int maxtcptw; 96162767Ssilby 97169608Sandre/* 98169608Sandre * The timed wait queue contains references to each of the TCP sessions 99169608Sandre * currently in the TIME_WAIT state. The queue pointers, including the 100169608Sandre * queue pointers in each tcptw structure, are protected using the global 101169608Sandre * tcbinfo lock, which must be held over queue iteration and modification. 102169608Sandre */ 103215701Sdimstatic VNET_DEFINE(TAILQ_HEAD(, tcptw), twq_2msl); 104195727Srwatson#define V_twq_2msl VNET(twq_2msl) 105195699Srwatson 106169608Sandrestatic void tcp_tw_2msl_reset(struct tcptw *, int); 107169608Sandrestatic void tcp_tw_2msl_stop(struct tcptw *); 108169608Sandre 109162031Sglebiusstatic int 110162767Ssilbytcptw_auto_size(void) 111162767Ssilby{ 112162767Ssilby int halfrange; 113162767Ssilby 114162767Ssilby /* 115162768Smaxim * Max out at half the ephemeral port range so that TIME_WAIT 116162768Smaxim * sockets don't tie up too many ephemeral ports. 117162768Smaxim */ 118181803Sbz if (V_ipport_lastauto > V_ipport_firstauto) 119181803Sbz halfrange = (V_ipport_lastauto - V_ipport_firstauto) / 2; 120162767Ssilby else 121181803Sbz halfrange = (V_ipport_firstauto - V_ipport_lastauto) / 2; 122162767Ssilby /* Protect against goofy port ranges smaller than 32. */ 123162767Ssilby return (imin(imax(halfrange, 32), maxsockets / 5)); 124162767Ssilby} 125162767Ssilby 126162767Ssilbystatic int 127162031Sglebiussysctl_maxtcptw(SYSCTL_HANDLER_ARGS) 128162031Sglebius{ 129162031Sglebius int error, new; 130157478Sglebius 131162031Sglebius if (maxtcptw == 0) 132162767Ssilby new = tcptw_auto_size(); 133162031Sglebius else 134162031Sglebius new = maxtcptw; 135170289Sdwmalone error = sysctl_handle_int(oidp, &new, 0, req); 136162767Ssilby if (error == 0 && req->newptr) 137162767Ssilby if (new >= 32) { 138162031Sglebius maxtcptw = new; 139190787Szec uma_zone_set_max(V_tcptw_zone, maxtcptw); 140162767Ssilby } 141162031Sglebius return (error); 142162031Sglebius} 143169541Sandre 144162031SglebiusSYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxtcptw, CTLTYPE_INT|CTLFLAG_RW, 145162031Sglebius &maxtcptw, 0, sysctl_maxtcptw, "IU", 146162031Sglebius "Maximum number of compressed TCP TIME_WAIT entries"); 147162031Sglebius 148207369SbzVNET_DEFINE(int, nolocaltimewait) = 0; 149207369Sbz#define V_nolocaltimewait VNET(nolocaltimewait) 150195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, nolocaltimewait, CTLFLAG_RW, 151195699Srwatson &VNET_NAME(nolocaltimewait), 0, 152167721Sandre "Do not create compressed TCP TIME_WAIT entries for local connections"); 153162151Sglebius 154169541Sandrevoid 155169541Sandretcp_tw_zone_change(void) 156157927Sps{ 157157927Sps 158162031Sglebius if (maxtcptw == 0) 159190787Szec uma_zone_set_max(V_tcptw_zone, tcptw_auto_size()); 160157927Sps} 161157927Sps 1621541Srgrimesvoid 163169541Sandretcp_tw_init(void) 1641541Srgrimes{ 165169454Srwatson 166190787Szec V_tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw), 167162031Sglebius NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 168157478Sglebius TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw); 169157478Sglebius if (maxtcptw == 0) 170190787Szec uma_zone_set_max(V_tcptw_zone, tcptw_auto_size()); 171162031Sglebius else 172190787Szec uma_zone_set_max(V_tcptw_zone, maxtcptw); 173181803Sbz TAILQ_INIT(&V_twq_2msl); 1741541Srgrimes} 1751541Srgrimes 176193731Szec#ifdef VIMAGE 177193731Szecvoid 178193731Szectcp_tw_destroy(void) 179193731Szec{ 180193731Szec struct tcptw *tw; 181193731Szec 182193731Szec INP_INFO_WLOCK(&V_tcbinfo); 183193731Szec while((tw = TAILQ_FIRST(&V_twq_2msl)) != NULL) 184193731Szec tcp_twclose(tw, 0); 185193731Szec INP_INFO_WUNLOCK(&V_tcbinfo); 186204838Sbz 187204838Sbz uma_zdestroy(V_tcptw_zone); 188193731Szec} 189193731Szec#endif 190193731Szec 1911541Srgrimes/* 192111145Sjlemon * Move a TCP connection into TIME_WAIT state. 193138410Srwatson * tcbinfo is locked. 194111145Sjlemon * inp is locked, and is unlocked before returning. 195111145Sjlemon */ 196111145Sjlemonvoid 197157431Srwatsontcp_twstart(struct tcpcb *tp) 198111145Sjlemon{ 199111145Sjlemon struct tcptw *tw; 200162151Sglebius struct inpcb *inp = tp->t_inpcb; 201162111Sru int acknow; 202111145Sjlemon struct socket *so; 203221250Sbz#ifdef INET6 204221250Sbz int isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6; 205221250Sbz#endif 206111145Sjlemon 207181803Sbz INP_INFO_WLOCK_ASSERT(&V_tcbinfo); /* tcp_tw_2msl_reset(). */ 208178285Srwatson INP_WLOCK_ASSERT(inp); 209138020Srwatson 210221250Sbz if (V_nolocaltimewait) { 211221250Sbz int error = 0; 212221250Sbz#ifdef INET6 213221250Sbz if (isipv6) 214221250Sbz error = in6_localaddr(&inp->in6p_faddr); 215221250Sbz#endif 216221250Sbz#if defined(INET6) && defined(INET) 217221250Sbz else 218221250Sbz#endif 219221250Sbz#ifdef INET 220221250Sbz error = in_localip(inp->inp_faddr); 221221250Sbz#endif 222221250Sbz if (error) { 223221250Sbz tp = tcp_close(tp); 224221250Sbz if (tp != NULL) 225221250Sbz INP_WUNLOCK(inp); 226221250Sbz return; 227221250Sbz } 228162151Sglebius } 229162151Sglebius 230190787Szec tw = uma_zalloc(V_tcptw_zone, M_NOWAIT); 231112009Sjlemon if (tw == NULL) { 232169608Sandre tw = tcp_tw_2msl_scan(1); 233112009Sjlemon if (tw == NULL) { 234157376Srwatson tp = tcp_close(tp); 235157376Srwatson if (tp != NULL) 236178285Srwatson INP_WUNLOCK(inp); 237112009Sjlemon return; 238112009Sjlemon } 239112009Sjlemon } 240111145Sjlemon tw->tw_inpcb = inp; 241111145Sjlemon 242111145Sjlemon /* 243111145Sjlemon * Recover last window size sent. 244111145Sjlemon */ 245229700Sjhb if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 246229665Sjhb tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale; 247229665Sjhb else 248229665Sjhb tw->last_win = 0; 249111145Sjlemon 250111145Sjlemon /* 251111145Sjlemon * Set t_recent if timestamps are used on the connection. 252111145Sjlemon */ 253133874Srwatson if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 254169477Sandre (TF_REQ_TSTMP|TF_RCVD_TSTMP)) { 255111145Sjlemon tw->t_recent = tp->ts_recent; 256169477Sandre tw->ts_offset = tp->ts_offset; 257169477Sandre } else { 258111145Sjlemon tw->t_recent = 0; 259169477Sandre tw->ts_offset = 0; 260169477Sandre } 261111145Sjlemon 262111145Sjlemon tw->snd_nxt = tp->snd_nxt; 263111145Sjlemon tw->rcv_nxt = tp->rcv_nxt; 264121850Ssilby tw->iss = tp->iss; 265121884Ssilby tw->irs = tp->irs; 266111145Sjlemon tw->t_starttime = tp->t_starttime; 267112009Sjlemon tw->tw_time = 0; 268111145Sjlemon 269111145Sjlemon/* XXX 270111145Sjlemon * If this code will 271111145Sjlemon * be used for fin-wait-2 state also, then we may need 272111145Sjlemon * a ts_recent from the last segment. 273111145Sjlemon */ 274137139Sandre acknow = tp->t_flags & TF_ACKNOW; 275157376Srwatson 276157376Srwatson /* 277157376Srwatson * First, discard tcpcb state, which includes stopping its timers and 278157376Srwatson * freeing it. tcp_discardcb() used to also release the inpcb, but 279157376Srwatson * that work is now done in the caller. 280160925Srwatson * 281160925Srwatson * Note: soisdisconnected() call used to be made in tcp_discardcb(), 282160925Srwatson * and might not be needed here any longer. 283157376Srwatson */ 284111145Sjlemon tcp_discardcb(tp); 285111145Sjlemon so = inp->inp_socket; 286160925Srwatson soisdisconnected(so); 287169347Srwatson tw->tw_cred = crhold(so->so_cred); 288130387Srwatson SOCK_LOCK(so); 289111145Sjlemon tw->tw_so_options = so->so_options; 290157376Srwatson SOCK_UNLOCK(so); 291114794Srwatson if (acknow) 292126351Srwatson tcp_twrespond(tw, TH_ACK); 293157432Srwatson inp->inp_ppcb = tw; 294189848Srwatson inp->inp_flags |= INP_TIMEWAIT; 295169608Sandre tcp_tw_2msl_reset(tw, 0); 296157376Srwatson 297157376Srwatson /* 298157376Srwatson * If the inpcb owns the sole reference to the socket, then we can 299157376Srwatson * detach and free the socket as it is not needed in time wait. 300157376Srwatson */ 301189848Srwatson if (inp->inp_flags & INP_SOCKREF) { 302157376Srwatson KASSERT(so->so_state & SS_PROTOREF, 303157376Srwatson ("tcp_twstart: !SS_PROTOREF")); 304189848Srwatson inp->inp_flags &= ~INP_SOCKREF; 305178285Srwatson INP_WUNLOCK(inp); 306157376Srwatson ACCEPT_LOCK(); 307157376Srwatson SOCK_LOCK(so); 308157376Srwatson so->so_state &= ~SS_PROTOREF; 309157376Srwatson sofree(so); 310157376Srwatson } else 311178285Srwatson INP_WUNLOCK(inp); 312111145Sjlemon} 313111145Sjlemon 314162064Sglebius#if 0 315121850Ssilby/* 316121884Ssilby * The appromixate rate of ISN increase of Microsoft TCP stacks; 317121884Ssilby * the actual rate is slightly higher due to the addition of 318121884Ssilby * random positive increments. 319121884Ssilby * 320121884Ssilby * Most other new OSes use semi-randomized ISN values, so we 321121884Ssilby * do not need to worry about them. 322121884Ssilby */ 323121884Ssilby#define MS_ISN_BYTES_PER_SECOND 250000 324121884Ssilby 325121884Ssilby/* 326121850Ssilby * Determine if the ISN we will generate has advanced beyond the last 327121850Ssilby * sequence number used by the previous connection. If so, indicate 328121850Ssilby * that it is safe to recycle this tw socket by returning 1. 329121850Ssilby */ 330121850Ssilbyint 331121850Ssilbytcp_twrecycleable(struct tcptw *tw) 332121850Ssilby{ 333121884Ssilby tcp_seq new_iss = tw->iss; 334121884Ssilby tcp_seq new_irs = tw->irs; 335121850Ssilby 336181803Sbz INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 337121884Ssilby new_iss += (ticks - tw->t_starttime) * (ISN_BYTES_PER_SECOND / hz); 338121884Ssilby new_irs += (ticks - tw->t_starttime) * (MS_ISN_BYTES_PER_SECOND / hz); 339133874Srwatson 340121884Ssilby if (SEQ_GT(new_iss, tw->snd_nxt) && SEQ_GT(new_irs, tw->rcv_nxt)) 341139222Srwatson return (1); 342121850Ssilby else 343139222Srwatson return (0); 344121850Ssilby} 345162064Sglebius#endif 346121850Ssilby 347169608Sandre/* 348169608Sandre * Returns 1 if the TIME_WAIT state was killed and we should start over, 349169608Sandre * looking for a pcb in the listen state. Returns 0 otherwise. 350169608Sandre */ 351169608Sandreint 352169608Sandretcp_twcheck(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th, 353169608Sandre struct mbuf *m, int tlen) 354169608Sandre{ 355169608Sandre struct tcptw *tw; 356169608Sandre int thflags; 357169608Sandre tcp_seq seq; 358169608Sandre 359169608Sandre /* tcbinfo lock required for tcp_twclose(), tcp_tw_2msl_reset(). */ 360181803Sbz INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 361178285Srwatson INP_WLOCK_ASSERT(inp); 362169608Sandre 363169608Sandre /* 364169608Sandre * XXXRW: Time wait state for inpcb has been recycled, but inpcb is 365169608Sandre * still present. This is undesirable, but temporarily necessary 366169608Sandre * until we work out how to handle inpcb's who's timewait state has 367169608Sandre * been removed. 368169608Sandre */ 369169608Sandre tw = intotw(inp); 370169608Sandre if (tw == NULL) 371169608Sandre goto drop; 372169608Sandre 373169608Sandre thflags = th->th_flags; 374169608Sandre 375169608Sandre /* 376169608Sandre * NOTE: for FIN_WAIT_2 (to be added later), 377169608Sandre * must validate sequence number before accepting RST 378169608Sandre */ 379169608Sandre 380169608Sandre /* 381169608Sandre * If the segment contains RST: 382169608Sandre * Drop the segment - see Stevens, vol. 2, p. 964 and 383169608Sandre * RFC 1337. 384169608Sandre */ 385169608Sandre if (thflags & TH_RST) 386169608Sandre goto drop; 387169608Sandre 388169608Sandre#if 0 389169608Sandre/* PAWS not needed at the moment */ 390169608Sandre /* 391169608Sandre * RFC 1323 PAWS: If we have a timestamp reply on this segment 392169608Sandre * and it's less than ts_recent, drop it. 393169608Sandre */ 394169608Sandre if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 395169608Sandre TSTMP_LT(to.to_tsval, tp->ts_recent)) { 396169608Sandre if ((thflags & TH_ACK) == 0) 397169608Sandre goto drop; 398169608Sandre goto ack; 399169608Sandre } 400169608Sandre /* 401169608Sandre * ts_recent is never updated because we never accept new segments. 402169608Sandre */ 403169608Sandre#endif 404169608Sandre 405169608Sandre /* 406169608Sandre * If a new connection request is received 407169608Sandre * while in TIME_WAIT, drop the old connection 408169608Sandre * and start over if the sequence numbers 409169608Sandre * are above the previous ones. 410169608Sandre */ 411169608Sandre if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 412169608Sandre tcp_twclose(tw, 0); 413169608Sandre return (1); 414169608Sandre } 415169608Sandre 416169608Sandre /* 417218909Sbrucec * Drop the segment if it does not contain an ACK. 418169608Sandre */ 419169608Sandre if ((thflags & TH_ACK) == 0) 420169608Sandre goto drop; 421169608Sandre 422169608Sandre /* 423169608Sandre * Reset the 2MSL timer if this is a duplicate FIN. 424169608Sandre */ 425169608Sandre if (thflags & TH_FIN) { 426169608Sandre seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 427169608Sandre if (seq + 1 == tw->rcv_nxt) 428169608Sandre tcp_tw_2msl_reset(tw, 1); 429169608Sandre } 430169608Sandre 431169608Sandre /* 432169608Sandre * Acknowledge the segment if it has data or is not a duplicate ACK. 433169608Sandre */ 434169608Sandre if (thflags != TH_ACK || tlen != 0 || 435169608Sandre th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 436169608Sandre tcp_twrespond(tw, TH_ACK); 437169608Sandredrop: 438178285Srwatson INP_WUNLOCK(inp); 439169608Sandre m_freem(m); 440169608Sandre return (0); 441169608Sandre} 442169608Sandre 443157376Srwatsonvoid 444112009Sjlemontcp_twclose(struct tcptw *tw, int reuse) 445111145Sjlemon{ 446157376Srwatson struct socket *so; 447111145Sjlemon struct inpcb *inp; 448111145Sjlemon 449157376Srwatson /* 450157386Srwatson * At this point, we are in one of two situations: 451157376Srwatson * 452160549Srwatson * (1) We have no socket, just an inpcb<->twtcp pair. We can free 453160549Srwatson * all state. 454157386Srwatson * 455160549Srwatson * (2) We have a socket -- if we own a reference, release it and 456160549Srwatson * notify the socket layer. 457157376Srwatson */ 458111145Sjlemon inp = tw->tw_inpcb; 459189848Srwatson KASSERT((inp->inp_flags & INP_TIMEWAIT), ("tcp_twclose: !timewait")); 460157432Srwatson KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw")); 461181803Sbz INP_INFO_WLOCK_ASSERT(&V_tcbinfo); /* tcp_tw_2msl_stop(). */ 462178285Srwatson INP_WLOCK_ASSERT(inp); 463138020Srwatson 464111145Sjlemon tw->tw_inpcb = NULL; 465169608Sandre tcp_tw_2msl_stop(tw); 466111145Sjlemon inp->inp_ppcb = NULL; 467158009Srwatson in_pcbdrop(inp); 468157376Srwatson 469157376Srwatson so = inp->inp_socket; 470157386Srwatson if (so != NULL) { 471160549Srwatson /* 472160549Srwatson * If there's a socket, handle two cases: first, we own a 473160549Srwatson * strong reference, which we will now release, or we don't 474160549Srwatson * in which case another reference exists (XXXRW: think 475160549Srwatson * about this more), and we don't need to take action. 476160549Srwatson */ 477189848Srwatson if (inp->inp_flags & INP_SOCKREF) { 478189848Srwatson inp->inp_flags &= ~INP_SOCKREF; 479178285Srwatson INP_WUNLOCK(inp); 480157386Srwatson ACCEPT_LOCK(); 481157386Srwatson SOCK_LOCK(so); 482157386Srwatson KASSERT(so->so_state & SS_PROTOREF, 483157386Srwatson ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF")); 484157386Srwatson so->so_state &= ~SS_PROTOREF; 485157386Srwatson sofree(so); 486157376Srwatson } else { 487157386Srwatson /* 488157386Srwatson * If we don't own the only reference, the socket and 489157386Srwatson * inpcb need to be left around to be handled by 490157386Srwatson * tcp_usr_detach() later. 491157386Srwatson */ 492178285Srwatson INP_WUNLOCK(inp); 493157376Srwatson } 494185370Sbz } else 495185370Sbz in_pcbfree(inp); 496190948Srwatson TCPSTAT_INC(tcps_closed); 497126002Spjd crfree(tw->tw_cred); 498126002Spjd tw->tw_cred = NULL; 499112009Sjlemon if (reuse) 500157376Srwatson return; 501190787Szec uma_zfree(V_tcptw_zone, tw); 502111145Sjlemon} 503111145Sjlemon 504111145Sjlemonint 505126351Srwatsontcp_twrespond(struct tcptw *tw, int flags) 506111145Sjlemon{ 507111145Sjlemon struct inpcb *inp = tw->tw_inpcb; 508221250Sbz#if defined(INET6) || defined(INET) 509221250Sbz struct tcphdr *th = NULL; 510221250Sbz#endif 511111145Sjlemon struct mbuf *m; 512221250Sbz#ifdef INET 513111145Sjlemon struct ip *ip = NULL; 514221250Sbz#endif 515111145Sjlemon u_int hdrlen, optlen; 516221250Sbz int error = 0; /* Keep compiler happy */ 517168845Sandre struct tcpopt to; 518111145Sjlemon#ifdef INET6 519111145Sjlemon struct ip6_hdr *ip6 = NULL; 520186222Sbz int isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6; 521111145Sjlemon#endif 522111145Sjlemon 523178285Srwatson INP_WLOCK_ASSERT(inp); 524138020Srwatson 525151967Sandre m = m_gethdr(M_DONTWAIT, MT_DATA); 526111145Sjlemon if (m == NULL) 527111145Sjlemon return (ENOBUFS); 528111145Sjlemon m->m_data += max_linkhdr; 529111145Sjlemon 530114794Srwatson#ifdef MAC 531172930Srwatson mac_inpcb_create_mbuf(inp, m); 532114794Srwatson#endif 533114794Srwatson 534111153Sjlemon#ifdef INET6 535111145Sjlemon if (isipv6) { 536111145Sjlemon hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 537111145Sjlemon ip6 = mtod(m, struct ip6_hdr *); 538111145Sjlemon th = (struct tcphdr *)(ip6 + 1); 539111145Sjlemon tcpip_fillheaders(inp, ip6, th); 540221250Sbz } 541111153Sjlemon#endif 542221250Sbz#if defined(INET6) && defined(INET) 543221250Sbz else 544221250Sbz#endif 545221250Sbz#ifdef INET 546111153Sjlemon { 547111145Sjlemon hdrlen = sizeof(struct tcpiphdr); 548111145Sjlemon ip = mtod(m, struct ip *); 549111145Sjlemon th = (struct tcphdr *)(ip + 1); 550111145Sjlemon tcpip_fillheaders(inp, ip, th); 551111145Sjlemon } 552221250Sbz#endif 553168845Sandre to.to_flags = 0; 554133874Srwatson 555133874Srwatson /* 556111145Sjlemon * Send a timestamp and echo-reply if both our side and our peer 557111145Sjlemon * have sent timestamps in our SYN's and this is not a RST. 558133874Srwatson */ 559111145Sjlemon if (tw->t_recent && flags == TH_ACK) { 560168845Sandre to.to_flags |= TOF_TS; 561231767Sbz to.to_tsval = tcp_ts_getticks() + tw->ts_offset; 562168845Sandre to.to_tsecr = tw->t_recent; 563133874Srwatson } 564168845Sandre optlen = tcp_addoptions(&to, (u_char *)(th + 1)); 565111145Sjlemon 566111145Sjlemon m->m_len = hdrlen + optlen; 567111145Sjlemon m->m_pkthdr.len = m->m_len; 568111145Sjlemon 569111145Sjlemon KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small")); 570111145Sjlemon 571111145Sjlemon th->th_seq = htonl(tw->snd_nxt); 572111145Sjlemon th->th_ack = htonl(tw->rcv_nxt); 573111145Sjlemon th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 574111145Sjlemon th->th_flags = flags; 575111145Sjlemon th->th_win = htons(tw->last_win); 576111145Sjlemon 577235961Sbz m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 578111153Sjlemon#ifdef INET6 579111145Sjlemon if (isipv6) { 580236170Sbz m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 581235961Sbz th->th_sum = in6_cksum_pseudo(ip6, 582235961Sbz sizeof(struct tcphdr) + optlen, IPPROTO_TCP, 0); 583122922Sandre ip6->ip6_hlim = in6_selecthlim(inp, NULL); 584122922Sandre error = ip6_output(m, inp->in6p_outputopts, NULL, 585111145Sjlemon (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp); 586221250Sbz } 587111153Sjlemon#endif 588221250Sbz#if defined(INET6) && defined(INET) 589221250Sbz else 590221250Sbz#endif 591221250Sbz#ifdef INET 592111153Sjlemon { 593236170Sbz m->m_pkthdr.csum_flags = CSUM_TCP; 594111145Sjlemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 595133874Srwatson htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP)); 596111145Sjlemon ip->ip_len = m->m_pkthdr.len; 597181803Sbz if (V_path_mtu_discovery) 598124248Sandre ip->ip_off |= IP_DF; 599122922Sandre error = ip_output(m, inp->inp_options, NULL, 600134793Sjmg ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 601134793Sjmg NULL, inp); 602111145Sjlemon } 603221250Sbz#endif 604111145Sjlemon if (flags & TH_ACK) 605190948Srwatson TCPSTAT_INC(tcps_sndacks); 606111145Sjlemon else 607190948Srwatson TCPSTAT_INC(tcps_sndctrl); 608190948Srwatson TCPSTAT_INC(tcps_sndtotal); 609111145Sjlemon return (error); 610111145Sjlemon} 611169608Sandre 612169608Sandrestatic void 613169608Sandretcp_tw_2msl_reset(struct tcptw *tw, int rearm) 614169608Sandre{ 615169608Sandre 616181803Sbz INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 617178285Srwatson INP_WLOCK_ASSERT(tw->tw_inpcb); 618169608Sandre if (rearm) 619181803Sbz TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl); 620169608Sandre tw->tw_time = ticks + 2 * tcp_msl; 621181803Sbz TAILQ_INSERT_TAIL(&V_twq_2msl, tw, tw_2msl); 622169608Sandre} 623169608Sandre 624169608Sandrestatic void 625169608Sandretcp_tw_2msl_stop(struct tcptw *tw) 626169608Sandre{ 627169608Sandre 628181803Sbz INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 629181803Sbz TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl); 630169608Sandre} 631169608Sandre 632169608Sandrestruct tcptw * 633169608Sandretcp_tw_2msl_scan(int reuse) 634169608Sandre{ 635169608Sandre struct tcptw *tw; 636169608Sandre 637181803Sbz INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 638169608Sandre for (;;) { 639181803Sbz tw = TAILQ_FIRST(&V_twq_2msl); 640196410Speter if (tw == NULL || (!reuse && (tw->tw_time - ticks) > 0)) 641169608Sandre break; 642178285Srwatson INP_WLOCK(tw->tw_inpcb); 643169608Sandre tcp_twclose(tw, reuse); 644169608Sandre if (reuse) 645169608Sandre return (tw); 646169608Sandre } 647169608Sandre return (NULL); 648169608Sandre} 649