tcp_timewait.c revision 190787
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 190787 2009-04-06 22:29:41Z zec $");
34172467Ssilby
35125680Sbms#include "opt_inet.h"
3654263Sshin#include "opt_inet6.h"
37101106Srwatson#include "opt_mac.h"
3829514Sjoerg#include "opt_tcpdebug.h"
3929514Sjoerg
401541Srgrimes#include <sys/param.h>
411541Srgrimes#include <sys/systm.h>
4250673Sjlemon#include <sys/callout.h>
4312172Sphk#include <sys/kernel.h>
4412172Sphk#include <sys/sysctl.h>
451541Srgrimes#include <sys/malloc.h>
461541Srgrimes#include <sys/mbuf.h>
47164033Srwatson#include <sys/priv.h>
4848758Sgreen#include <sys/proc.h>
491541Srgrimes#include <sys/socket.h>
501541Srgrimes#include <sys/socketvar.h>
511541Srgrimes#include <sys/protosw.h>
5275619Skris#include <sys/random.h>
53181803Sbz#include <sys/vimage.h>
5434923Sbde
5592760Sjeff#include <vm/uma.h>
561541Srgrimes
571541Srgrimes#include <net/route.h>
581541Srgrimes#include <net/if.h>
591541Srgrimes
601541Srgrimes#include <netinet/in.h>
611541Srgrimes#include <netinet/in_systm.h>
621541Srgrimes#include <netinet/ip.h>
6355679Sshin#ifdef INET6
6455679Sshin#include <netinet/ip6.h>
6555679Sshin#endif
661541Srgrimes#include <netinet/in_pcb.h>
6755679Sshin#ifdef INET6
6855679Sshin#include <netinet6/in6_pcb.h>
6955679Sshin#endif
707090Sbde#include <netinet/in_var.h>
711541Srgrimes#include <netinet/ip_var.h>
7255679Sshin#ifdef INET6
7355679Sshin#include <netinet6/ip6_var.h>
74148385Sume#include <netinet6/scope6_var.h>
75122922Sandre#include <netinet6/nd6.h>
7655679Sshin#endif
77145360Sandre#include <netinet/ip_icmp.h>
781541Srgrimes#include <netinet/tcp.h>
791541Srgrimes#include <netinet/tcp_fsm.h>
801541Srgrimes#include <netinet/tcp_seq.h>
811541Srgrimes#include <netinet/tcp_timer.h>
821541Srgrimes#include <netinet/tcp_var.h>
8355679Sshin#ifdef INET6
8455679Sshin#include <netinet6/tcp6_var.h>
8555679Sshin#endif
861541Srgrimes#include <netinet/tcpip.h>
876283Swollman#ifdef TCPDEBUG
886283Swollman#include <netinet/tcp_debug.h>
896283Swollman#endif
9055679Sshin#include <netinet6/ip6protosw.h>
91185571Sbz#include <netinet/vinet.h>
921541Srgrimes
9358698Sjlemon#include <machine/in_cksum.h>
9458698Sjlemon
95163606Srwatson#include <security/mac/mac_framework.h>
96163606Srwatson
97157478Sglebiusstatic int	maxtcptw;
98162767Ssilby
99169608Sandre/*
100169608Sandre * The timed wait queue contains references to each of the TCP sessions
101169608Sandre * currently in the TIME_WAIT state.  The queue pointers, including the
102169608Sandre * queue pointers in each tcptw structure, are protected using the global
103169608Sandre * tcbinfo lock, which must be held over queue iteration and modification.
104169608Sandre */
105185088Szec#ifdef VIMAGE_GLOBALS
106190787Szecstatic uma_zone_t tcptw_zone;
107169608Sandrestatic TAILQ_HEAD(, tcptw)	twq_2msl;
108185088Szecint	nolocaltimewait;
109185088Szec#endif
110169608Sandre
111169608Sandrestatic void	tcp_tw_2msl_reset(struct tcptw *, int);
112169608Sandrestatic void	tcp_tw_2msl_stop(struct tcptw *);
113169608Sandre
114162031Sglebiusstatic int
115162767Ssilbytcptw_auto_size(void)
116162767Ssilby{
117183550Szec	INIT_VNET_INET(curvnet);
118162767Ssilby	int halfrange;
119162767Ssilby
120162767Ssilby	/*
121162768Smaxim	 * Max out at half the ephemeral port range so that TIME_WAIT
122162768Smaxim	 * sockets don't tie up too many ephemeral ports.
123162768Smaxim	 */
124181803Sbz	if (V_ipport_lastauto > V_ipport_firstauto)
125181803Sbz		halfrange = (V_ipport_lastauto - V_ipport_firstauto) / 2;
126162767Ssilby	else
127181803Sbz		halfrange = (V_ipport_firstauto - V_ipport_lastauto) / 2;
128162767Ssilby	/* Protect against goofy port ranges smaller than 32. */
129162767Ssilby	return (imin(imax(halfrange, 32), maxsockets / 5));
130162767Ssilby}
131162767Ssilby
132162767Ssilbystatic int
133162031Sglebiussysctl_maxtcptw(SYSCTL_HANDLER_ARGS)
134162031Sglebius{
135162031Sglebius	int error, new;
136157478Sglebius
137162031Sglebius	if (maxtcptw == 0)
138162767Ssilby		new = tcptw_auto_size();
139162031Sglebius	else
140162031Sglebius		new = maxtcptw;
141170289Sdwmalone	error = sysctl_handle_int(oidp, &new, 0, req);
142162767Ssilby	if (error == 0 && req->newptr)
143162767Ssilby		if (new >= 32) {
144162031Sglebius			maxtcptw = new;
145190787Szec			uma_zone_set_max(V_tcptw_zone, maxtcptw);
146162767Ssilby		}
147162031Sglebius	return (error);
148162031Sglebius}
149169541Sandre
150162031SglebiusSYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxtcptw, CTLTYPE_INT|CTLFLAG_RW,
151162031Sglebius    &maxtcptw, 0, sysctl_maxtcptw, "IU",
152162031Sglebius    "Maximum number of compressed TCP TIME_WAIT entries");
153162031Sglebius
154185348SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, nolocaltimewait,
155185348Szec    CTLFLAG_RW, nolocaltimewait, 0,
156167721Sandre    "Do not create compressed TCP TIME_WAIT entries for local connections");
157162151Sglebius
158169541Sandrevoid
159169541Sandretcp_tw_zone_change(void)
160157927Sps{
161157927Sps
162162031Sglebius	if (maxtcptw == 0)
163190787Szec		uma_zone_set_max(V_tcptw_zone, tcptw_auto_size());
164157927Sps}
165157927Sps
1661541Srgrimesvoid
167169541Sandretcp_tw_init(void)
1681541Srgrimes{
169183550Szec	INIT_VNET_INET(curvnet);
170169454Srwatson
171190787Szec	V_tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw),
172162031Sglebius	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
173157478Sglebius	TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw);
174157478Sglebius	if (maxtcptw == 0)
175190787Szec		uma_zone_set_max(V_tcptw_zone, tcptw_auto_size());
176162031Sglebius	else
177190787Szec		uma_zone_set_max(V_tcptw_zone, maxtcptw);
178181803Sbz	TAILQ_INIT(&V_twq_2msl);
1791541Srgrimes}
1801541Srgrimes
1811541Srgrimes/*
182111145Sjlemon * Move a TCP connection into TIME_WAIT state.
183138410Srwatson *    tcbinfo is locked.
184111145Sjlemon *    inp is locked, and is unlocked before returning.
185111145Sjlemon */
186111145Sjlemonvoid
187157431Srwatsontcp_twstart(struct tcpcb *tp)
188111145Sjlemon{
189183550Szec#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
190183550Szec	INIT_VNET_INET(tp->t_vnet);
191183550Szec#endif
192111145Sjlemon	struct tcptw *tw;
193162151Sglebius	struct inpcb *inp = tp->t_inpcb;
194162111Sru	int acknow;
195111145Sjlemon	struct socket *so;
196111145Sjlemon
197181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);	/* tcp_tw_2msl_reset(). */
198178285Srwatson	INP_WLOCK_ASSERT(inp);
199138020Srwatson
200181803Sbz	if (V_nolocaltimewait && in_localip(inp->inp_faddr)) {
201162151Sglebius		tp = tcp_close(tp);
202162151Sglebius		if (tp != NULL)
203178285Srwatson			INP_WUNLOCK(inp);
204162151Sglebius		return;
205162151Sglebius	}
206162151Sglebius
207190787Szec	tw = uma_zalloc(V_tcptw_zone, M_NOWAIT);
208112009Sjlemon	if (tw == NULL) {
209169608Sandre		tw = tcp_tw_2msl_scan(1);
210112009Sjlemon		if (tw == NULL) {
211157376Srwatson			tp = tcp_close(tp);
212157376Srwatson			if (tp != NULL)
213178285Srwatson				INP_WUNLOCK(inp);
214112009Sjlemon			return;
215112009Sjlemon		}
216112009Sjlemon	}
217111145Sjlemon	tw->tw_inpcb = inp;
218111145Sjlemon
219111145Sjlemon	/*
220111145Sjlemon	 * Recover last window size sent.
221111145Sjlemon	 */
222111145Sjlemon	tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale;
223111145Sjlemon
224111145Sjlemon	/*
225111145Sjlemon	 * Set t_recent if timestamps are used on the connection.
226111145Sjlemon	 */
227133874Srwatson	if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
228169477Sandre	    (TF_REQ_TSTMP|TF_RCVD_TSTMP)) {
229111145Sjlemon		tw->t_recent = tp->ts_recent;
230169477Sandre		tw->ts_offset = tp->ts_offset;
231169477Sandre	} else {
232111145Sjlemon		tw->t_recent = 0;
233169477Sandre		tw->ts_offset = 0;
234169477Sandre	}
235111145Sjlemon
236111145Sjlemon	tw->snd_nxt = tp->snd_nxt;
237111145Sjlemon	tw->rcv_nxt = tp->rcv_nxt;
238121850Ssilby	tw->iss     = tp->iss;
239121884Ssilby	tw->irs     = tp->irs;
240111145Sjlemon	tw->t_starttime = tp->t_starttime;
241112009Sjlemon	tw->tw_time = 0;
242111145Sjlemon
243111145Sjlemon/* XXX
244111145Sjlemon * If this code will
245111145Sjlemon * be used for fin-wait-2 state also, then we may need
246111145Sjlemon * a ts_recent from the last segment.
247111145Sjlemon */
248137139Sandre	acknow = tp->t_flags & TF_ACKNOW;
249157376Srwatson
250157376Srwatson	/*
251157376Srwatson	 * First, discard tcpcb state, which includes stopping its timers and
252157376Srwatson	 * freeing it.  tcp_discardcb() used to also release the inpcb, but
253157376Srwatson	 * that work is now done in the caller.
254160925Srwatson	 *
255160925Srwatson	 * Note: soisdisconnected() call used to be made in tcp_discardcb(),
256160925Srwatson	 * and might not be needed here any longer.
257157376Srwatson	 */
258111145Sjlemon	tcp_discardcb(tp);
259111145Sjlemon	so = inp->inp_socket;
260160925Srwatson	soisdisconnected(so);
261169347Srwatson	tw->tw_cred = crhold(so->so_cred);
262130387Srwatson	SOCK_LOCK(so);
263111145Sjlemon	tw->tw_so_options = so->so_options;
264157376Srwatson	SOCK_UNLOCK(so);
265114794Srwatson	if (acknow)
266126351Srwatson		tcp_twrespond(tw, TH_ACK);
267157432Srwatson	inp->inp_ppcb = tw;
268189848Srwatson	inp->inp_flags |= INP_TIMEWAIT;
269169608Sandre	tcp_tw_2msl_reset(tw, 0);
270157376Srwatson
271157376Srwatson	/*
272157376Srwatson	 * If the inpcb owns the sole reference to the socket, then we can
273157376Srwatson	 * detach and free the socket as it is not needed in time wait.
274157376Srwatson	 */
275189848Srwatson	if (inp->inp_flags & INP_SOCKREF) {
276157376Srwatson		KASSERT(so->so_state & SS_PROTOREF,
277157376Srwatson		    ("tcp_twstart: !SS_PROTOREF"));
278189848Srwatson		inp->inp_flags &= ~INP_SOCKREF;
279178285Srwatson		INP_WUNLOCK(inp);
280157376Srwatson		ACCEPT_LOCK();
281157376Srwatson		SOCK_LOCK(so);
282157376Srwatson		so->so_state &= ~SS_PROTOREF;
283157376Srwatson		sofree(so);
284157376Srwatson	} else
285178285Srwatson		INP_WUNLOCK(inp);
286111145Sjlemon}
287111145Sjlemon
288162064Sglebius#if 0
289121850Ssilby/*
290121884Ssilby * The appromixate rate of ISN increase of Microsoft TCP stacks;
291121884Ssilby * the actual rate is slightly higher due to the addition of
292121884Ssilby * random positive increments.
293121884Ssilby *
294121884Ssilby * Most other new OSes use semi-randomized ISN values, so we
295121884Ssilby * do not need to worry about them.
296121884Ssilby */
297121884Ssilby#define MS_ISN_BYTES_PER_SECOND		250000
298121884Ssilby
299121884Ssilby/*
300121850Ssilby * Determine if the ISN we will generate has advanced beyond the last
301121850Ssilby * sequence number used by the previous connection.  If so, indicate
302121850Ssilby * that it is safe to recycle this tw socket by returning 1.
303121850Ssilby */
304121850Ssilbyint
305121850Ssilbytcp_twrecycleable(struct tcptw *tw)
306121850Ssilby{
307183550Szec	INIT_VNET_INET(curvnet);
308121884Ssilby	tcp_seq new_iss = tw->iss;
309121884Ssilby	tcp_seq new_irs = tw->irs;
310121850Ssilby
311181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
312121884Ssilby	new_iss += (ticks - tw->t_starttime) * (ISN_BYTES_PER_SECOND / hz);
313121884Ssilby	new_irs += (ticks - tw->t_starttime) * (MS_ISN_BYTES_PER_SECOND / hz);
314133874Srwatson
315121884Ssilby	if (SEQ_GT(new_iss, tw->snd_nxt) && SEQ_GT(new_irs, tw->rcv_nxt))
316139222Srwatson		return (1);
317121850Ssilby	else
318139222Srwatson		return (0);
319121850Ssilby}
320162064Sglebius#endif
321121850Ssilby
322169608Sandre/*
323169608Sandre * Returns 1 if the TIME_WAIT state was killed and we should start over,
324169608Sandre * looking for a pcb in the listen state.  Returns 0 otherwise.
325169608Sandre */
326169608Sandreint
327169608Sandretcp_twcheck(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th,
328169608Sandre    struct mbuf *m, int tlen)
329169608Sandre{
330183550Szec#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
331183550Szec	INIT_VNET_INET(curvnet);
332183550Szec#endif
333169608Sandre	struct tcptw *tw;
334169608Sandre	int thflags;
335169608Sandre	tcp_seq seq;
336169608Sandre
337169608Sandre	/* tcbinfo lock required for tcp_twclose(), tcp_tw_2msl_reset(). */
338181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
339178285Srwatson	INP_WLOCK_ASSERT(inp);
340169608Sandre
341169608Sandre	/*
342169608Sandre	 * XXXRW: Time wait state for inpcb has been recycled, but inpcb is
343169608Sandre	 * still present.  This is undesirable, but temporarily necessary
344169608Sandre	 * until we work out how to handle inpcb's who's timewait state has
345169608Sandre	 * been removed.
346169608Sandre	 */
347169608Sandre	tw = intotw(inp);
348169608Sandre	if (tw == NULL)
349169608Sandre		goto drop;
350169608Sandre
351169608Sandre	thflags = th->th_flags;
352169608Sandre
353169608Sandre	/*
354169608Sandre	 * NOTE: for FIN_WAIT_2 (to be added later),
355169608Sandre	 * must validate sequence number before accepting RST
356169608Sandre	 */
357169608Sandre
358169608Sandre	/*
359169608Sandre	 * If the segment contains RST:
360169608Sandre	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
361169608Sandre	 *      RFC 1337.
362169608Sandre	 */
363169608Sandre	if (thflags & TH_RST)
364169608Sandre		goto drop;
365169608Sandre
366169608Sandre#if 0
367169608Sandre/* PAWS not needed at the moment */
368169608Sandre	/*
369169608Sandre	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
370169608Sandre	 * and it's less than ts_recent, drop it.
371169608Sandre	 */
372169608Sandre	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
373169608Sandre	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
374169608Sandre		if ((thflags & TH_ACK) == 0)
375169608Sandre			goto drop;
376169608Sandre		goto ack;
377169608Sandre	}
378169608Sandre	/*
379169608Sandre	 * ts_recent is never updated because we never accept new segments.
380169608Sandre	 */
381169608Sandre#endif
382169608Sandre
383169608Sandre	/*
384169608Sandre	 * If a new connection request is received
385169608Sandre	 * while in TIME_WAIT, drop the old connection
386169608Sandre	 * and start over if the sequence numbers
387169608Sandre	 * are above the previous ones.
388169608Sandre	 */
389169608Sandre	if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
390169608Sandre		tcp_twclose(tw, 0);
391169608Sandre		return (1);
392169608Sandre	}
393169608Sandre
394169608Sandre	/*
395169608Sandre	 * Drop the the segment if it does not contain an ACK.
396169608Sandre	 */
397169608Sandre	if ((thflags & TH_ACK) == 0)
398169608Sandre		goto drop;
399169608Sandre
400169608Sandre	/*
401169608Sandre	 * Reset the 2MSL timer if this is a duplicate FIN.
402169608Sandre	 */
403169608Sandre	if (thflags & TH_FIN) {
404169608Sandre		seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
405169608Sandre		if (seq + 1 == tw->rcv_nxt)
406169608Sandre			tcp_tw_2msl_reset(tw, 1);
407169608Sandre	}
408169608Sandre
409169608Sandre	/*
410169608Sandre	 * Acknowledge the segment if it has data or is not a duplicate ACK.
411169608Sandre	 */
412169608Sandre	if (thflags != TH_ACK || tlen != 0 ||
413169608Sandre	    th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
414169608Sandre		tcp_twrespond(tw, TH_ACK);
415169608Sandredrop:
416178285Srwatson	INP_WUNLOCK(inp);
417169608Sandre	m_freem(m);
418169608Sandre	return (0);
419169608Sandre}
420169608Sandre
421157376Srwatsonvoid
422112009Sjlemontcp_twclose(struct tcptw *tw, int reuse)
423111145Sjlemon{
424183550Szec	INIT_VNET_INET(curvnet);
425157376Srwatson	struct socket *so;
426111145Sjlemon	struct inpcb *inp;
427111145Sjlemon
428157376Srwatson	/*
429157386Srwatson	 * At this point, we are in one of two situations:
430157376Srwatson	 *
431160549Srwatson	 * (1) We have no socket, just an inpcb<->twtcp pair.  We can free
432160549Srwatson	 *     all state.
433157386Srwatson	 *
434160549Srwatson	 * (2) We have a socket -- if we own a reference, release it and
435160549Srwatson	 *     notify the socket layer.
436157376Srwatson	 */
437111145Sjlemon	inp = tw->tw_inpcb;
438189848Srwatson	KASSERT((inp->inp_flags & INP_TIMEWAIT), ("tcp_twclose: !timewait"));
439157432Srwatson	KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw"));
440181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);	/* tcp_tw_2msl_stop(). */
441178285Srwatson	INP_WLOCK_ASSERT(inp);
442138020Srwatson
443111145Sjlemon	tw->tw_inpcb = NULL;
444169608Sandre	tcp_tw_2msl_stop(tw);
445111145Sjlemon	inp->inp_ppcb = NULL;
446158009Srwatson	in_pcbdrop(inp);
447157376Srwatson
448157376Srwatson	so = inp->inp_socket;
449157386Srwatson	if (so != NULL) {
450160549Srwatson		/*
451160549Srwatson		 * If there's a socket, handle two cases: first, we own a
452160549Srwatson		 * strong reference, which we will now release, or we don't
453160549Srwatson		 * in which case another reference exists (XXXRW: think
454160549Srwatson		 * about this more), and we don't need to take action.
455160549Srwatson		 */
456189848Srwatson		if (inp->inp_flags & INP_SOCKREF) {
457189848Srwatson			inp->inp_flags &= ~INP_SOCKREF;
458178285Srwatson			INP_WUNLOCK(inp);
459157386Srwatson			ACCEPT_LOCK();
460157386Srwatson			SOCK_LOCK(so);
461157386Srwatson			KASSERT(so->so_state & SS_PROTOREF,
462157386Srwatson			    ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF"));
463157386Srwatson			so->so_state &= ~SS_PROTOREF;
464157386Srwatson			sofree(so);
465157376Srwatson		} else {
466157386Srwatson			/*
467157386Srwatson			 * If we don't own the only reference, the socket and
468157386Srwatson			 * inpcb need to be left around to be handled by
469157386Srwatson			 * tcp_usr_detach() later.
470157386Srwatson			 */
471178285Srwatson			INP_WUNLOCK(inp);
472157376Srwatson		}
473185370Sbz	} else
474185370Sbz		in_pcbfree(inp);
475181803Sbz	V_tcpstat.tcps_closed++;
476126002Spjd	crfree(tw->tw_cred);
477126002Spjd	tw->tw_cred = NULL;
478112009Sjlemon	if (reuse)
479157376Srwatson		return;
480190787Szec	uma_zfree(V_tcptw_zone, tw);
481111145Sjlemon}
482111145Sjlemon
483111145Sjlemonint
484126351Srwatsontcp_twrespond(struct tcptw *tw, int flags)
485111145Sjlemon{
486183550Szec	INIT_VNET_INET(curvnet);
487111145Sjlemon	struct inpcb *inp = tw->tw_inpcb;
488111145Sjlemon	struct tcphdr *th;
489111145Sjlemon	struct mbuf *m;
490111145Sjlemon	struct ip *ip = NULL;
491111145Sjlemon	u_int hdrlen, optlen;
492111145Sjlemon	int error;
493168845Sandre	struct tcpopt to;
494111145Sjlemon#ifdef INET6
495111145Sjlemon	struct ip6_hdr *ip6 = NULL;
496186222Sbz	int isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6;
497111145Sjlemon#endif
498111145Sjlemon
499178285Srwatson	INP_WLOCK_ASSERT(inp);
500138020Srwatson
501151967Sandre	m = m_gethdr(M_DONTWAIT, MT_DATA);
502111145Sjlemon	if (m == NULL)
503111145Sjlemon		return (ENOBUFS);
504111145Sjlemon	m->m_data += max_linkhdr;
505111145Sjlemon
506114794Srwatson#ifdef MAC
507172930Srwatson	mac_inpcb_create_mbuf(inp, m);
508114794Srwatson#endif
509114794Srwatson
510111153Sjlemon#ifdef INET6
511111145Sjlemon	if (isipv6) {
512111145Sjlemon		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
513111145Sjlemon		ip6 = mtod(m, struct ip6_hdr *);
514111145Sjlemon		th = (struct tcphdr *)(ip6 + 1);
515111145Sjlemon		tcpip_fillheaders(inp, ip6, th);
516111153Sjlemon	} else
517111153Sjlemon#endif
518111153Sjlemon	{
519111145Sjlemon		hdrlen = sizeof(struct tcpiphdr);
520111145Sjlemon		ip = mtod(m, struct ip *);
521111145Sjlemon		th = (struct tcphdr *)(ip + 1);
522111145Sjlemon		tcpip_fillheaders(inp, ip, th);
523111145Sjlemon	}
524168845Sandre	to.to_flags = 0;
525133874Srwatson
526133874Srwatson	/*
527111145Sjlemon	 * Send a timestamp and echo-reply if both our side and our peer
528111145Sjlemon	 * have sent timestamps in our SYN's and this is not a RST.
529133874Srwatson	 */
530111145Sjlemon	if (tw->t_recent && flags == TH_ACK) {
531168845Sandre		to.to_flags |= TOF_TS;
532169477Sandre		to.to_tsval = ticks + tw->ts_offset;
533168845Sandre		to.to_tsecr = tw->t_recent;
534133874Srwatson	}
535168845Sandre	optlen = tcp_addoptions(&to, (u_char *)(th + 1));
536111145Sjlemon
537111145Sjlemon	m->m_len = hdrlen + optlen;
538111145Sjlemon	m->m_pkthdr.len = m->m_len;
539111145Sjlemon
540111145Sjlemon	KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small"));
541111145Sjlemon
542111145Sjlemon	th->th_seq = htonl(tw->snd_nxt);
543111145Sjlemon	th->th_ack = htonl(tw->rcv_nxt);
544111145Sjlemon	th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
545111145Sjlemon	th->th_flags = flags;
546111145Sjlemon	th->th_win = htons(tw->last_win);
547111145Sjlemon
548111153Sjlemon#ifdef INET6
549111145Sjlemon	if (isipv6) {
550111145Sjlemon		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
551111145Sjlemon		    sizeof(struct tcphdr) + optlen);
552122922Sandre		ip6->ip6_hlim = in6_selecthlim(inp, NULL);
553122922Sandre		error = ip6_output(m, inp->in6p_outputopts, NULL,
554111145Sjlemon		    (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp);
555111153Sjlemon	} else
556111153Sjlemon#endif
557111153Sjlemon	{
558111145Sjlemon		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
559133874Srwatson		    htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP));
560111145Sjlemon		m->m_pkthdr.csum_flags = CSUM_TCP;
561111145Sjlemon		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
562111145Sjlemon		ip->ip_len = m->m_pkthdr.len;
563181803Sbz		if (V_path_mtu_discovery)
564124248Sandre			ip->ip_off |= IP_DF;
565122922Sandre		error = ip_output(m, inp->inp_options, NULL,
566134793Sjmg		    ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0),
567134793Sjmg		    NULL, inp);
568111145Sjlemon	}
569111145Sjlemon	if (flags & TH_ACK)
570181803Sbz		V_tcpstat.tcps_sndacks++;
571111145Sjlemon	else
572181803Sbz		V_tcpstat.tcps_sndctrl++;
573181803Sbz	V_tcpstat.tcps_sndtotal++;
574111145Sjlemon	return (error);
575111145Sjlemon}
576169608Sandre
577169608Sandrestatic void
578169608Sandretcp_tw_2msl_reset(struct tcptw *tw, int rearm)
579169608Sandre{
580183550Szec	INIT_VNET_INET(curvnet);
581169608Sandre
582181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
583178285Srwatson	INP_WLOCK_ASSERT(tw->tw_inpcb);
584169608Sandre	if (rearm)
585181803Sbz		TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl);
586169608Sandre	tw->tw_time = ticks + 2 * tcp_msl;
587181803Sbz	TAILQ_INSERT_TAIL(&V_twq_2msl, tw, tw_2msl);
588169608Sandre}
589169608Sandre
590169608Sandrestatic void
591169608Sandretcp_tw_2msl_stop(struct tcptw *tw)
592169608Sandre{
593183550Szec	INIT_VNET_INET(curvnet);
594169608Sandre
595181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
596181803Sbz	TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl);
597169608Sandre}
598169608Sandre
599169608Sandrestruct tcptw *
600169608Sandretcp_tw_2msl_scan(int reuse)
601169608Sandre{
602183550Szec	INIT_VNET_INET(curvnet);
603169608Sandre	struct tcptw *tw;
604169608Sandre
605181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
606169608Sandre	for (;;) {
607181803Sbz		tw = TAILQ_FIRST(&V_twq_2msl);
608169608Sandre		if (tw == NULL || (!reuse && tw->tw_time > ticks))
609169608Sandre			break;
610178285Srwatson		INP_WLOCK(tw->tw_inpcb);
611169608Sandre		tcp_twclose(tw, reuse);
612169608Sandre		if (reuse)
613169608Sandre			return (tw);
614169608Sandre	}
615169608Sandre	return (NULL);
616169608Sandre}
617