tcp_timewait.c revision 185370
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 185370 2008-11-27 12:04:35Z bz $");
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>
911541Srgrimes
9258698Sjlemon#include <machine/in_cksum.h>
9358698Sjlemon
94163606Srwatson#include <security/mac/mac_framework.h>
95163606Srwatson
96162035Sglebiusstatic uma_zone_t tcptw_zone;
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
106169608Sandrestatic TAILQ_HEAD(, tcptw)	twq_2msl;
107185088Szecint	nolocaltimewait;
108185088Szec#endif
109169608Sandre
110169608Sandrestatic void	tcp_tw_2msl_reset(struct tcptw *, int);
111169608Sandrestatic void	tcp_tw_2msl_stop(struct tcptw *);
112169608Sandre
113162031Sglebiusstatic int
114162767Ssilbytcptw_auto_size(void)
115162767Ssilby{
116183550Szec	INIT_VNET_INET(curvnet);
117162767Ssilby	int halfrange;
118162767Ssilby
119162767Ssilby	/*
120162768Smaxim	 * Max out at half the ephemeral port range so that TIME_WAIT
121162768Smaxim	 * sockets don't tie up too many ephemeral ports.
122162768Smaxim	 */
123181803Sbz	if (V_ipport_lastauto > V_ipport_firstauto)
124181803Sbz		halfrange = (V_ipport_lastauto - V_ipport_firstauto) / 2;
125162767Ssilby	else
126181803Sbz		halfrange = (V_ipport_firstauto - V_ipport_lastauto) / 2;
127162767Ssilby	/* Protect against goofy port ranges smaller than 32. */
128162767Ssilby	return (imin(imax(halfrange, 32), maxsockets / 5));
129162767Ssilby}
130162767Ssilby
131162767Ssilbystatic int
132162031Sglebiussysctl_maxtcptw(SYSCTL_HANDLER_ARGS)
133162031Sglebius{
134162031Sglebius	int error, new;
135157478Sglebius
136162031Sglebius	if (maxtcptw == 0)
137162767Ssilby		new = tcptw_auto_size();
138162031Sglebius	else
139162031Sglebius		new = maxtcptw;
140170289Sdwmalone	error = sysctl_handle_int(oidp, &new, 0, req);
141162767Ssilby	if (error == 0 && req->newptr)
142162767Ssilby		if (new >= 32) {
143162031Sglebius			maxtcptw = new;
144162035Sglebius			uma_zone_set_max(tcptw_zone, maxtcptw);
145162767Ssilby		}
146162031Sglebius	return (error);
147162031Sglebius}
148169541Sandre
149162031SglebiusSYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxtcptw, CTLTYPE_INT|CTLFLAG_RW,
150162031Sglebius    &maxtcptw, 0, sysctl_maxtcptw, "IU",
151162031Sglebius    "Maximum number of compressed TCP TIME_WAIT entries");
152162031Sglebius
153185348SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, nolocaltimewait,
154185348Szec    CTLFLAG_RW, nolocaltimewait, 0,
155167721Sandre    "Do not create compressed TCP TIME_WAIT entries for local connections");
156162151Sglebius
157169541Sandrevoid
158169541Sandretcp_tw_zone_change(void)
159157927Sps{
160157927Sps
161162031Sglebius	if (maxtcptw == 0)
162162767Ssilby		uma_zone_set_max(tcptw_zone, tcptw_auto_size());
163157927Sps}
164157927Sps
1651541Srgrimesvoid
166169541Sandretcp_tw_init(void)
1671541Srgrimes{
168183550Szec	INIT_VNET_INET(curvnet);
169169454Srwatson
170162031Sglebius	tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw),
171162031Sglebius	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
172157478Sglebius	TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw);
173157478Sglebius	if (maxtcptw == 0)
174162767Ssilby		uma_zone_set_max(tcptw_zone, tcptw_auto_size());
175162031Sglebius	else
176162031Sglebius		uma_zone_set_max(tcptw_zone, maxtcptw);
177181803Sbz	TAILQ_INIT(&V_twq_2msl);
1781541Srgrimes}
1791541Srgrimes
1801541Srgrimes/*
181111145Sjlemon * Move a TCP connection into TIME_WAIT state.
182138410Srwatson *    tcbinfo is locked.
183111145Sjlemon *    inp is locked, and is unlocked before returning.
184111145Sjlemon */
185111145Sjlemonvoid
186157431Srwatsontcp_twstart(struct tcpcb *tp)
187111145Sjlemon{
188183550Szec#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
189183550Szec	INIT_VNET_INET(tp->t_vnet);
190183550Szec#endif
191111145Sjlemon	struct tcptw *tw;
192162151Sglebius	struct inpcb *inp = tp->t_inpcb;
193162111Sru	int acknow;
194111145Sjlemon	struct socket *so;
195111145Sjlemon
196181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);	/* tcp_tw_2msl_reset(). */
197178285Srwatson	INP_WLOCK_ASSERT(inp);
198138020Srwatson
199181803Sbz	if (V_nolocaltimewait && in_localip(inp->inp_faddr)) {
200162151Sglebius		tp = tcp_close(tp);
201162151Sglebius		if (tp != NULL)
202178285Srwatson			INP_WUNLOCK(inp);
203162151Sglebius		return;
204162151Sglebius	}
205162151Sglebius
206112009Sjlemon	tw = uma_zalloc(tcptw_zone, M_NOWAIT);
207112009Sjlemon	if (tw == NULL) {
208169608Sandre		tw = tcp_tw_2msl_scan(1);
209112009Sjlemon		if (tw == NULL) {
210157376Srwatson			tp = tcp_close(tp);
211157376Srwatson			if (tp != NULL)
212178285Srwatson				INP_WUNLOCK(inp);
213112009Sjlemon			return;
214112009Sjlemon		}
215112009Sjlemon	}
216111145Sjlemon	tw->tw_inpcb = inp;
217111145Sjlemon
218111145Sjlemon	/*
219111145Sjlemon	 * Recover last window size sent.
220111145Sjlemon	 */
221111145Sjlemon	tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale;
222111145Sjlemon
223111145Sjlemon	/*
224111145Sjlemon	 * Set t_recent if timestamps are used on the connection.
225111145Sjlemon	 */
226133874Srwatson	if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
227169477Sandre	    (TF_REQ_TSTMP|TF_RCVD_TSTMP)) {
228111145Sjlemon		tw->t_recent = tp->ts_recent;
229169477Sandre		tw->ts_offset = tp->ts_offset;
230169477Sandre	} else {
231111145Sjlemon		tw->t_recent = 0;
232169477Sandre		tw->ts_offset = 0;
233169477Sandre	}
234111145Sjlemon
235111145Sjlemon	tw->snd_nxt = tp->snd_nxt;
236111145Sjlemon	tw->rcv_nxt = tp->rcv_nxt;
237121850Ssilby	tw->iss     = tp->iss;
238121884Ssilby	tw->irs     = tp->irs;
239111145Sjlemon	tw->t_starttime = tp->t_starttime;
240112009Sjlemon	tw->tw_time = 0;
241111145Sjlemon
242111145Sjlemon/* XXX
243111145Sjlemon * If this code will
244111145Sjlemon * be used for fin-wait-2 state also, then we may need
245111145Sjlemon * a ts_recent from the last segment.
246111145Sjlemon */
247137139Sandre	acknow = tp->t_flags & TF_ACKNOW;
248157376Srwatson
249157376Srwatson	/*
250157376Srwatson	 * First, discard tcpcb state, which includes stopping its timers and
251157376Srwatson	 * freeing it.  tcp_discardcb() used to also release the inpcb, but
252157376Srwatson	 * that work is now done in the caller.
253160925Srwatson	 *
254160925Srwatson	 * Note: soisdisconnected() call used to be made in tcp_discardcb(),
255160925Srwatson	 * and might not be needed here any longer.
256157376Srwatson	 */
257111145Sjlemon	tcp_discardcb(tp);
258111145Sjlemon	so = inp->inp_socket;
259160925Srwatson	soisdisconnected(so);
260169347Srwatson	tw->tw_cred = crhold(so->so_cred);
261130387Srwatson	SOCK_LOCK(so);
262111145Sjlemon	tw->tw_so_options = so->so_options;
263157376Srwatson	SOCK_UNLOCK(so);
264114794Srwatson	if (acknow)
265126351Srwatson		tcp_twrespond(tw, TH_ACK);
266157432Srwatson	inp->inp_ppcb = tw;
267111145Sjlemon	inp->inp_vflag |= INP_TIMEWAIT;
268169608Sandre	tcp_tw_2msl_reset(tw, 0);
269157376Srwatson
270157376Srwatson	/*
271157376Srwatson	 * If the inpcb owns the sole reference to the socket, then we can
272157376Srwatson	 * detach and free the socket as it is not needed in time wait.
273157376Srwatson	 */
274157376Srwatson	if (inp->inp_vflag & INP_SOCKREF) {
275157376Srwatson		KASSERT(so->so_state & SS_PROTOREF,
276157376Srwatson		    ("tcp_twstart: !SS_PROTOREF"));
277157376Srwatson		inp->inp_vflag &= ~INP_SOCKREF;
278178285Srwatson		INP_WUNLOCK(inp);
279157376Srwatson		ACCEPT_LOCK();
280157376Srwatson		SOCK_LOCK(so);
281157376Srwatson		so->so_state &= ~SS_PROTOREF;
282157376Srwatson		sofree(so);
283157376Srwatson	} else
284178285Srwatson		INP_WUNLOCK(inp);
285111145Sjlemon}
286111145Sjlemon
287162064Sglebius#if 0
288121850Ssilby/*
289121884Ssilby * The appromixate rate of ISN increase of Microsoft TCP stacks;
290121884Ssilby * the actual rate is slightly higher due to the addition of
291121884Ssilby * random positive increments.
292121884Ssilby *
293121884Ssilby * Most other new OSes use semi-randomized ISN values, so we
294121884Ssilby * do not need to worry about them.
295121884Ssilby */
296121884Ssilby#define MS_ISN_BYTES_PER_SECOND		250000
297121884Ssilby
298121884Ssilby/*
299121850Ssilby * Determine if the ISN we will generate has advanced beyond the last
300121850Ssilby * sequence number used by the previous connection.  If so, indicate
301121850Ssilby * that it is safe to recycle this tw socket by returning 1.
302121850Ssilby */
303121850Ssilbyint
304121850Ssilbytcp_twrecycleable(struct tcptw *tw)
305121850Ssilby{
306183550Szec	INIT_VNET_INET(curvnet);
307121884Ssilby	tcp_seq new_iss = tw->iss;
308121884Ssilby	tcp_seq new_irs = tw->irs;
309121850Ssilby
310181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
311121884Ssilby	new_iss += (ticks - tw->t_starttime) * (ISN_BYTES_PER_SECOND / hz);
312121884Ssilby	new_irs += (ticks - tw->t_starttime) * (MS_ISN_BYTES_PER_SECOND / hz);
313133874Srwatson
314121884Ssilby	if (SEQ_GT(new_iss, tw->snd_nxt) && SEQ_GT(new_irs, tw->rcv_nxt))
315139222Srwatson		return (1);
316121850Ssilby	else
317139222Srwatson		return (0);
318121850Ssilby}
319162064Sglebius#endif
320121850Ssilby
321169608Sandre/*
322169608Sandre * Returns 1 if the TIME_WAIT state was killed and we should start over,
323169608Sandre * looking for a pcb in the listen state.  Returns 0 otherwise.
324169608Sandre */
325169608Sandreint
326169608Sandretcp_twcheck(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th,
327169608Sandre    struct mbuf *m, int tlen)
328169608Sandre{
329183550Szec#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
330183550Szec	INIT_VNET_INET(curvnet);
331183550Szec#endif
332169608Sandre	struct tcptw *tw;
333169608Sandre	int thflags;
334169608Sandre	tcp_seq seq;
335169608Sandre#ifdef INET6
336169608Sandre	int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
337169608Sandre#else
338169608Sandre	const int isipv6 = 0;
339169608Sandre#endif
340169608Sandre
341169608Sandre	/* tcbinfo lock required for tcp_twclose(), tcp_tw_2msl_reset(). */
342181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
343178285Srwatson	INP_WLOCK_ASSERT(inp);
344169608Sandre
345169608Sandre	/*
346169608Sandre	 * XXXRW: Time wait state for inpcb has been recycled, but inpcb is
347169608Sandre	 * still present.  This is undesirable, but temporarily necessary
348169608Sandre	 * until we work out how to handle inpcb's who's timewait state has
349169608Sandre	 * been removed.
350169608Sandre	 */
351169608Sandre	tw = intotw(inp);
352169608Sandre	if (tw == NULL)
353169608Sandre		goto drop;
354169608Sandre
355169608Sandre	thflags = th->th_flags;
356169608Sandre
357169608Sandre	/*
358169608Sandre	 * NOTE: for FIN_WAIT_2 (to be added later),
359169608Sandre	 * must validate sequence number before accepting RST
360169608Sandre	 */
361169608Sandre
362169608Sandre	/*
363169608Sandre	 * If the segment contains RST:
364169608Sandre	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
365169608Sandre	 *      RFC 1337.
366169608Sandre	 */
367169608Sandre	if (thflags & TH_RST)
368169608Sandre		goto drop;
369169608Sandre
370169608Sandre#if 0
371169608Sandre/* PAWS not needed at the moment */
372169608Sandre	/*
373169608Sandre	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
374169608Sandre	 * and it's less than ts_recent, drop it.
375169608Sandre	 */
376169608Sandre	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
377169608Sandre	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
378169608Sandre		if ((thflags & TH_ACK) == 0)
379169608Sandre			goto drop;
380169608Sandre		goto ack;
381169608Sandre	}
382169608Sandre	/*
383169608Sandre	 * ts_recent is never updated because we never accept new segments.
384169608Sandre	 */
385169608Sandre#endif
386169608Sandre
387169608Sandre	/*
388169608Sandre	 * If a new connection request is received
389169608Sandre	 * while in TIME_WAIT, drop the old connection
390169608Sandre	 * and start over if the sequence numbers
391169608Sandre	 * are above the previous ones.
392169608Sandre	 */
393169608Sandre	if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
394169608Sandre		tcp_twclose(tw, 0);
395169608Sandre		return (1);
396169608Sandre	}
397169608Sandre
398169608Sandre	/*
399169608Sandre	 * Drop the the segment if it does not contain an ACK.
400169608Sandre	 */
401169608Sandre	if ((thflags & TH_ACK) == 0)
402169608Sandre		goto drop;
403169608Sandre
404169608Sandre	/*
405169608Sandre	 * Reset the 2MSL timer if this is a duplicate FIN.
406169608Sandre	 */
407169608Sandre	if (thflags & TH_FIN) {
408169608Sandre		seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
409169608Sandre		if (seq + 1 == tw->rcv_nxt)
410169608Sandre			tcp_tw_2msl_reset(tw, 1);
411169608Sandre	}
412169608Sandre
413169608Sandre	/*
414169608Sandre	 * Acknowledge the segment if it has data or is not a duplicate ACK.
415169608Sandre	 */
416169608Sandre	if (thflags != TH_ACK || tlen != 0 ||
417169608Sandre	    th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
418169608Sandre		tcp_twrespond(tw, TH_ACK);
419169608Sandre	goto drop;
420169608Sandre
421169608Sandre	/*
422169608Sandre	 * Generate a RST, dropping incoming segment.
423169608Sandre	 * Make ACK acceptable to originator of segment.
424169608Sandre	 * Don't bother to respond if destination was broadcast/multicast.
425169608Sandre	 */
426169608Sandre	if (m->m_flags & (M_BCAST|M_MCAST))
427169608Sandre		goto drop;
428169608Sandre	if (isipv6) {
429169635Soleg#ifdef INET6
430169608Sandre		struct ip6_hdr *ip6;
431169608Sandre
432169608Sandre		/* IPv6 anycast check is done at tcp6_input() */
433169608Sandre		ip6 = mtod(m, struct ip6_hdr *);
434169608Sandre		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
435169608Sandre		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
436169608Sandre			goto drop;
437169635Soleg#endif
438169608Sandre	} else {
439169608Sandre		struct ip *ip;
440169608Sandre
441169608Sandre		ip = mtod(m, struct ip *);
442169608Sandre		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
443169608Sandre		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
444169608Sandre		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
445169608Sandre		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
446169608Sandre			goto drop;
447169608Sandre	}
448169608Sandre	if (thflags & TH_ACK) {
449169608Sandre		tcp_respond(NULL,
450169608Sandre		    mtod(m, void *), th, m, 0, th->th_ack, TH_RST);
451169608Sandre	} else {
452169608Sandre		seq = th->th_seq + (thflags & TH_SYN ? 1 : 0);
453169608Sandre		tcp_respond(NULL,
454169608Sandre		    mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK);
455169608Sandre	}
456178285Srwatson	INP_WUNLOCK(inp);
457169608Sandre	return (0);
458169608Sandre
459169608Sandredrop:
460178285Srwatson	INP_WUNLOCK(inp);
461169608Sandre	m_freem(m);
462169608Sandre	return (0);
463169608Sandre}
464169608Sandre
465157376Srwatsonvoid
466112009Sjlemontcp_twclose(struct tcptw *tw, int reuse)
467111145Sjlemon{
468183550Szec	INIT_VNET_INET(curvnet);
469157376Srwatson	struct socket *so;
470111145Sjlemon	struct inpcb *inp;
471111145Sjlemon
472157376Srwatson	/*
473157386Srwatson	 * At this point, we are in one of two situations:
474157376Srwatson	 *
475160549Srwatson	 * (1) We have no socket, just an inpcb<->twtcp pair.  We can free
476160549Srwatson	 *     all state.
477157386Srwatson	 *
478160549Srwatson	 * (2) We have a socket -- if we own a reference, release it and
479160549Srwatson	 *     notify the socket layer.
480157376Srwatson	 */
481111145Sjlemon	inp = tw->tw_inpcb;
482157376Srwatson	KASSERT((inp->inp_vflag & INP_TIMEWAIT), ("tcp_twclose: !timewait"));
483157432Srwatson	KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw"));
484181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);	/* tcp_tw_2msl_stop(). */
485178285Srwatson	INP_WLOCK_ASSERT(inp);
486138020Srwatson
487111145Sjlemon	tw->tw_inpcb = NULL;
488169608Sandre	tcp_tw_2msl_stop(tw);
489111145Sjlemon	inp->inp_ppcb = NULL;
490158009Srwatson	in_pcbdrop(inp);
491157376Srwatson
492157376Srwatson	so = inp->inp_socket;
493157386Srwatson	if (so != NULL) {
494160549Srwatson		/*
495160549Srwatson		 * If there's a socket, handle two cases: first, we own a
496160549Srwatson		 * strong reference, which we will now release, or we don't
497160549Srwatson		 * in which case another reference exists (XXXRW: think
498160549Srwatson		 * about this more), and we don't need to take action.
499160549Srwatson		 */
500157386Srwatson		if (inp->inp_vflag & INP_SOCKREF) {
501157386Srwatson			inp->inp_vflag &= ~INP_SOCKREF;
502178285Srwatson			INP_WUNLOCK(inp);
503157386Srwatson			ACCEPT_LOCK();
504157386Srwatson			SOCK_LOCK(so);
505157386Srwatson			KASSERT(so->so_state & SS_PROTOREF,
506157386Srwatson			    ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF"));
507157386Srwatson			so->so_state &= ~SS_PROTOREF;
508157386Srwatson			sofree(so);
509157376Srwatson		} else {
510157386Srwatson			/*
511157386Srwatson			 * If we don't own the only reference, the socket and
512157386Srwatson			 * inpcb need to be left around to be handled by
513157386Srwatson			 * tcp_usr_detach() later.
514157386Srwatson			 */
515178285Srwatson			INP_WUNLOCK(inp);
516157376Srwatson		}
517185370Sbz	} else
518185370Sbz		in_pcbfree(inp);
519181803Sbz	V_tcpstat.tcps_closed++;
520126002Spjd	crfree(tw->tw_cred);
521126002Spjd	tw->tw_cred = NULL;
522112009Sjlemon	if (reuse)
523157376Srwatson		return;
524112009Sjlemon	uma_zfree(tcptw_zone, tw);
525111145Sjlemon}
526111145Sjlemon
527111145Sjlemonint
528126351Srwatsontcp_twrespond(struct tcptw *tw, int flags)
529111145Sjlemon{
530183550Szec	INIT_VNET_INET(curvnet);
531111145Sjlemon	struct inpcb *inp = tw->tw_inpcb;
532111145Sjlemon	struct tcphdr *th;
533111145Sjlemon	struct mbuf *m;
534111145Sjlemon	struct ip *ip = NULL;
535111145Sjlemon	u_int hdrlen, optlen;
536111145Sjlemon	int error;
537168845Sandre	struct tcpopt to;
538111145Sjlemon#ifdef INET6
539111145Sjlemon	struct ip6_hdr *ip6 = NULL;
540111145Sjlemon	int isipv6 = inp->inp_inc.inc_isipv6;
541111145Sjlemon#endif
542111145Sjlemon
543178285Srwatson	INP_WLOCK_ASSERT(inp);
544138020Srwatson
545151967Sandre	m = m_gethdr(M_DONTWAIT, MT_DATA);
546111145Sjlemon	if (m == NULL)
547111145Sjlemon		return (ENOBUFS);
548111145Sjlemon	m->m_data += max_linkhdr;
549111145Sjlemon
550114794Srwatson#ifdef MAC
551172930Srwatson	mac_inpcb_create_mbuf(inp, m);
552114794Srwatson#endif
553114794Srwatson
554111153Sjlemon#ifdef INET6
555111145Sjlemon	if (isipv6) {
556111145Sjlemon		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
557111145Sjlemon		ip6 = mtod(m, struct ip6_hdr *);
558111145Sjlemon		th = (struct tcphdr *)(ip6 + 1);
559111145Sjlemon		tcpip_fillheaders(inp, ip6, th);
560111153Sjlemon	} else
561111153Sjlemon#endif
562111153Sjlemon	{
563111145Sjlemon		hdrlen = sizeof(struct tcpiphdr);
564111145Sjlemon		ip = mtod(m, struct ip *);
565111145Sjlemon		th = (struct tcphdr *)(ip + 1);
566111145Sjlemon		tcpip_fillheaders(inp, ip, th);
567111145Sjlemon	}
568168845Sandre	to.to_flags = 0;
569133874Srwatson
570133874Srwatson	/*
571111145Sjlemon	 * Send a timestamp and echo-reply if both our side and our peer
572111145Sjlemon	 * have sent timestamps in our SYN's and this is not a RST.
573133874Srwatson	 */
574111145Sjlemon	if (tw->t_recent && flags == TH_ACK) {
575168845Sandre		to.to_flags |= TOF_TS;
576169477Sandre		to.to_tsval = ticks + tw->ts_offset;
577168845Sandre		to.to_tsecr = tw->t_recent;
578133874Srwatson	}
579168845Sandre	optlen = tcp_addoptions(&to, (u_char *)(th + 1));
580111145Sjlemon
581111145Sjlemon	m->m_len = hdrlen + optlen;
582111145Sjlemon	m->m_pkthdr.len = m->m_len;
583111145Sjlemon
584111145Sjlemon	KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small"));
585111145Sjlemon
586111145Sjlemon	th->th_seq = htonl(tw->snd_nxt);
587111145Sjlemon	th->th_ack = htonl(tw->rcv_nxt);
588111145Sjlemon	th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
589111145Sjlemon	th->th_flags = flags;
590111145Sjlemon	th->th_win = htons(tw->last_win);
591111145Sjlemon
592111153Sjlemon#ifdef INET6
593111145Sjlemon	if (isipv6) {
594111145Sjlemon		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
595111145Sjlemon		    sizeof(struct tcphdr) + optlen);
596122922Sandre		ip6->ip6_hlim = in6_selecthlim(inp, NULL);
597122922Sandre		error = ip6_output(m, inp->in6p_outputopts, NULL,
598111145Sjlemon		    (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp);
599111153Sjlemon	} else
600111153Sjlemon#endif
601111153Sjlemon	{
602111145Sjlemon		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
603133874Srwatson		    htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP));
604111145Sjlemon		m->m_pkthdr.csum_flags = CSUM_TCP;
605111145Sjlemon		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
606111145Sjlemon		ip->ip_len = m->m_pkthdr.len;
607181803Sbz		if (V_path_mtu_discovery)
608124248Sandre			ip->ip_off |= IP_DF;
609122922Sandre		error = ip_output(m, inp->inp_options, NULL,
610134793Sjmg		    ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0),
611134793Sjmg		    NULL, inp);
612111145Sjlemon	}
613111145Sjlemon	if (flags & TH_ACK)
614181803Sbz		V_tcpstat.tcps_sndacks++;
615111145Sjlemon	else
616181803Sbz		V_tcpstat.tcps_sndctrl++;
617181803Sbz	V_tcpstat.tcps_sndtotal++;
618111145Sjlemon	return (error);
619111145Sjlemon}
620169608Sandre
621169608Sandrestatic void
622169608Sandretcp_tw_2msl_reset(struct tcptw *tw, int rearm)
623169608Sandre{
624183550Szec	INIT_VNET_INET(curvnet);
625169608Sandre
626181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
627178285Srwatson	INP_WLOCK_ASSERT(tw->tw_inpcb);
628169608Sandre	if (rearm)
629181803Sbz		TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl);
630169608Sandre	tw->tw_time = ticks + 2 * tcp_msl;
631181803Sbz	TAILQ_INSERT_TAIL(&V_twq_2msl, tw, tw_2msl);
632169608Sandre}
633169608Sandre
634169608Sandrestatic void
635169608Sandretcp_tw_2msl_stop(struct tcptw *tw)
636169608Sandre{
637183550Szec	INIT_VNET_INET(curvnet);
638169608Sandre
639181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
640181803Sbz	TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl);
641169608Sandre}
642169608Sandre
643169608Sandrestruct tcptw *
644169608Sandretcp_tw_2msl_scan(int reuse)
645169608Sandre{
646183550Szec	INIT_VNET_INET(curvnet);
647169608Sandre	struct tcptw *tw;
648169608Sandre
649181803Sbz	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
650169608Sandre	for (;;) {
651181803Sbz		tw = TAILQ_FIRST(&V_twq_2msl);
652169608Sandre		if (tw == NULL || (!reuse && tw->tw_time > ticks))
653169608Sandre			break;
654178285Srwatson		INP_WLOCK(tw->tw_inpcb);
655169608Sandre		tcp_twclose(tw, reuse);
656169608Sandre		if (reuse)
657169608Sandre			return (tw);
658169608Sandre	}
659169608Sandre	return (NULL);
660169608Sandre}
661