xref: /freebsd/sys/netinet/tcp_timewait.c (revision 1d386b48)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
32  */
33 
34 #include <sys/cdefs.h>
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 #include "opt_ipsec.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/syslog.h>
51 #include <sys/protosw.h>
52 #include <sys/random.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_kdtrace.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_icmp.h>
68 #include <netinet/ip_var.h>
69 #ifdef INET6
70 #include <netinet/ip6.h>
71 #include <netinet6/in6_pcb.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/scope6_var.h>
74 #include <netinet6/nd6.h>
75 #endif
76 #include <netinet/tcp.h>
77 #include <netinet/tcp_fsm.h>
78 #include <netinet/tcp_seq.h>
79 #include <netinet/tcp_timer.h>
80 #include <netinet/tcp_var.h>
81 #include <netinet/tcpip.h>
82 
83 #include <netinet/udp.h>
84 #include <netinet/udp_var.h>
85 
86 #include <netipsec/ipsec_support.h>
87 
88 #include <machine/in_cksum.h>
89 
90 #include <security/mac/mac_framework.h>
91 
92 VNET_DEFINE_STATIC(bool, nolocaltimewait) = true;
93 #define	V_nolocaltimewait	VNET(nolocaltimewait)
94 SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, nolocaltimewait,
95     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nolocaltimewait), true,
96     "Do not create TCP TIME_WAIT state for local connections");
97 
98 /*
99  * Move a TCP connection into TIME_WAIT state.
100  *    inp is locked, and is unlocked before returning.
101  *
102  * This function used to free tcpcb and allocate a compressed TCP time-wait
103  * structure tcptw.  This served well for 20 years but is no longer relevant
104  * on modern machines in the modern internet.  However, the function remains
105  * so that TCP stacks require less modification and we don't burn the bridge
106  * to go back to using compressed time-wait.
107  */
108 void
109 tcp_twstart(struct tcpcb *tp)
110 {
111 	struct inpcb *inp = tptoinpcb(tp);
112 #ifdef INET6
113 	bool isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6;
114 #endif
115 
116 	NET_EPOCH_ASSERT();
117 	INP_WLOCK_ASSERT(inp);
118 
119 	/* A dropped inp should never transition to TIME_WAIT state. */
120 	KASSERT((inp->inp_flags & INP_DROPPED) == 0, ("tcp_twstart: "
121 	    "(inp->inp_flags & INP_DROPPED) != 0"));
122 
123 	tcp_state_change(tp, TCPS_TIME_WAIT);
124 	soisdisconnected(inp->inp_socket);
125 
126 	if (tp->t_flags & TF_ACKNOW)
127 		tcp_output(tp);
128 
129 	if (V_nolocaltimewait && (
130 #ifdef INET6
131 	    isipv6 ? in6_localaddr(&inp->in6p_faddr) :
132 #endif
133 #ifdef INET
134 	    in_localip(inp->inp_faddr)
135 #else
136 	    false
137 #endif
138 	    )) {
139 		if ((tp = tcp_close(tp)) != NULL)
140 			INP_WUNLOCK(inp);
141 		return;
142 	}
143 
144 	tcp_timer_activate(tp, TT_2MSL, 2 * V_tcp_msl);
145 	INP_WUNLOCK(inp);
146 }
147 
148 /*
149  * Returns true if the TIME_WAIT state was killed and we should start over,
150  * looking for a pcb in the listen state.  Otherwise returns false and frees
151  * the mbuf.
152  *
153  * For pure SYN-segments the PCB shall be read-locked and the tcpopt pointer
154  * may be NULL.  For the rest write-lock and valid tcpopt.
155  */
156 bool
157 tcp_twcheck(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th,
158     struct mbuf *m, int tlen)
159 {
160 	struct tcpcb *tp = intotcpcb(inp);
161 	char *s;
162 	int thflags;
163 	tcp_seq seq;
164 
165 	NET_EPOCH_ASSERT();
166 	INP_LOCK_ASSERT(inp);
167 
168 	thflags = tcp_get_flags(th);
169 #ifdef INVARIANTS
170 	if ((thflags & (TH_SYN | TH_ACK)) == TH_SYN)
171 		INP_RLOCK_ASSERT(inp);
172 	else {
173 		INP_WLOCK_ASSERT(inp);
174 		KASSERT(to != NULL,
175 		    ("%s: called without options on a non-SYN segment",
176 		    __func__));
177 	}
178 #endif
179 
180 	/*
181 	 * NOTE: for FIN_WAIT_2 (to be added later),
182 	 * must validate sequence number before accepting RST
183 	 */
184 
185 	/*
186 	 * If the segment contains RST:
187 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
188 	 *      RFC 1337.
189 	 */
190 	if (thflags & TH_RST)
191 		goto drop;
192 
193 #if 0
194 /* PAWS not needed at the moment */
195 	/*
196 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
197 	 * and it's less than ts_recent, drop it.
198 	 */
199 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
200 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
201 		if ((thflags & TH_ACK) == 0)
202 			goto drop;
203 		goto ack;
204 	}
205 	/*
206 	 * ts_recent is never updated because we never accept new segments.
207 	 */
208 #endif
209 
210 	/* Honor the drop_synfin sysctl variable. */
211 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
212 		if ((s = tcp_log_addrs(&inp->inp_inc, th, NULL, NULL))) {
213 			log(LOG_DEBUG, "%s; %s: "
214 			    "SYN|FIN segment ignored (based on "
215 			    "sysctl setting)\n", s, __func__);
216 			free(s, M_TCPLOG);
217 		}
218 		goto drop;
219 	}
220 
221 	/*
222 	 * If a new connection request is received
223 	 * while in TIME_WAIT, drop the old connection
224 	 * and start over if the sequence numbers
225 	 * are above the previous ones.
226 	 * Allow UDP port number changes in this case.
227 	 */
228 	if (((thflags & (TH_SYN | TH_ACK)) == TH_SYN) &&
229 	    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
230 		/*
231 		 * In case we can't upgrade our lock just pretend we have
232 		 * lost this packet.
233 		 */
234 		if (INP_TRY_UPGRADE(inp) == 0)
235 			goto drop;
236 		if ((tp = tcp_close(tp)) != NULL)
237 			INP_WUNLOCK(inp);
238 		TCPSTAT_INC(tcps_tw_recycles);
239 		return (true);
240 	}
241 
242 	/*
243 	 * Send RST if UDP port numbers don't match
244 	 */
245 	if (tp->t_port != m->m_pkthdr.tcp_tun_port) {
246 		if (tcp_get_flags(th) & TH_ACK) {
247 			tcp_respond(tp, mtod(m, void *), th, m,
248 			    (tcp_seq)0, th->th_ack, TH_RST);
249 		} else {
250 			if (tcp_get_flags(th) & TH_SYN)
251 				tlen++;
252 			if (tcp_get_flags(th) & TH_FIN)
253 				tlen++;
254 			tcp_respond(tp, mtod(m, void *), th, m,
255 			    th->th_seq+tlen, (tcp_seq)0, TH_RST|TH_ACK);
256 		}
257 		INP_UNLOCK(inp);
258 		TCPSTAT_INC(tcps_tw_resets);
259 		return (false);
260 	}
261 
262 	/*
263 	 * Drop the segment if it does not contain an ACK.
264 	 */
265 	if ((thflags & TH_ACK) == 0)
266 		goto drop;
267 
268 	INP_WLOCK_ASSERT(inp);
269 
270 	/*
271 	 * If timestamps were negotiated during SYN/ACK and a
272 	 * segment without a timestamp is received, silently drop
273 	 * the segment, unless the missing timestamps are tolerated.
274 	 * See section 3.2 of RFC 7323.
275 	 */
276 	if (((to->to_flags & TOF_TS) == 0) && (tp->ts_recent != 0) &&
277 	    (V_tcp_tolerate_missing_ts == 0)) {
278 		goto drop;
279 	}
280 
281 	/*
282 	 * Reset the 2MSL timer if this is a duplicate FIN.
283 	 */
284 	if (thflags & TH_FIN) {
285 		seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
286 		if (seq + 1 == tp->rcv_nxt)
287 			tcp_timer_activate(tp, TT_2MSL, 2 * V_tcp_msl);
288 	}
289 
290 	/*
291 	 * Acknowledge the segment if it has data or is not a duplicate ACK.
292 	 */
293 	if (thflags != TH_ACK || tlen != 0 ||
294 	    th->th_seq != tp->rcv_nxt || th->th_ack != tp->snd_nxt) {
295 		TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
296 		tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
297 		    tp->snd_nxt, TH_ACK);
298 		INP_UNLOCK(inp);
299 		TCPSTAT_INC(tcps_tw_responds);
300 		return (false);
301 	}
302 drop:
303 	TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
304 	INP_UNLOCK(inp);
305 	m_freem(m);
306 	return (false);
307 }
308