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
32 #include <sys/cdefs.h>
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/callout.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/priv.h>
45 #include <sys/proc.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/syslog.h>
49 #include <sys/protosw.h>
50 #include <sys/random.h>
51
52 #include <vm/uma.h>
53
54 #include <net/route.h>
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/vnet.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_kdtrace.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_icmp.h>
66 #include <netinet/ip_var.h>
67 #ifdef INET6
68 #include <netinet/ip6.h>
69 #include <netinet6/in6_pcb.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet6/scope6_var.h>
72 #include <netinet6/nd6.h>
73 #endif
74 #include <netinet/tcp.h>
75 #include <netinet/tcp_fsm.h>
76 #include <netinet/tcp_seq.h>
77 #include <netinet/tcp_timer.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/tcpip.h>
80
81 #include <netinet/udp.h>
82 #include <netinet/udp_var.h>
83
84 #include <netipsec/ipsec_support.h>
85
86 #include <machine/in_cksum.h>
87
88 #include <security/mac/mac_framework.h>
89
90 VNET_DEFINE_STATIC(bool, nolocaltimewait) = false;
91 #define V_nolocaltimewait VNET(nolocaltimewait)
92
93 static int
sysctl_net_inet_tcp_nolocaltimewait(SYSCTL_HANDLER_ARGS)94 sysctl_net_inet_tcp_nolocaltimewait(SYSCTL_HANDLER_ARGS)
95 {
96 int error;
97 bool new;
98
99 new = V_nolocaltimewait;
100 error = sysctl_handle_bool(oidp, &new, 0, req);
101 if (error == 0 && req->newptr) {
102 V_nolocaltimewait = new;
103 gone_in(16, "net.inet.tcp.nolocaltimewait is obsolete."
104 " Use net.inet.tcp.local_msl instead.\n");
105 }
106 return (error);
107 }
108
109 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, nolocaltimewait,
110 CTLFLAG_VNET | CTLFLAG_RW | CTLTYPE_U8,
111 &VNET_NAME(nolocaltimewait), 0, sysctl_net_inet_tcp_nolocaltimewait, "CU",
112 "Do not create TCP TIME_WAIT state for local connections");
113
114 static u_int
tcp_eff_msl(struct tcpcb * tp)115 tcp_eff_msl(struct tcpcb *tp)
116 {
117 struct inpcb *inp = tptoinpcb(tp);
118 #ifdef INET6
119 bool isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6;
120 #endif
121
122 if (
123 #ifdef INET6
124 isipv6 ? in6_localip(&inp->in6p_faddr) :
125 #endif
126 #ifdef INET
127 in_localip(inp->inp_faddr))
128 #else
129 false)
130 #endif
131 return (V_tcp_msl_local);
132 else
133 return (V_tcp_msl);
134 }
135
136 /*
137 * Move a TCP connection into TIME_WAIT state.
138 * inp is locked, and is unlocked before returning.
139 *
140 * This function used to free tcpcb and allocate a compressed TCP time-wait
141 * structure tcptw. This served well for 20 years but is no longer relevant
142 * on modern machines in the modern internet. However, the function remains
143 * so that TCP stacks require less modification and we don't burn the bridge
144 * to go back to using compressed time-wait.
145 */
146 void
tcp_twstart(struct tcpcb * tp)147 tcp_twstart(struct tcpcb *tp)
148 {
149 struct inpcb *inp = tptoinpcb(tp);
150 #ifdef INET6
151 bool isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6;
152 #endif
153
154 NET_EPOCH_ASSERT();
155 INP_WLOCK_ASSERT(inp);
156
157 /* A dropped inp should never transition to TIME_WAIT state. */
158 KASSERT((inp->inp_flags & INP_DROPPED) == 0, ("tcp_twstart: "
159 "(inp->inp_flags & INP_DROPPED) != 0"));
160
161 tcp_state_change(tp, TCPS_TIME_WAIT);
162 tcp_free_sackholes(tp);
163 soisdisconnected(inp->inp_socket);
164
165 if (tp->t_flags & TF_ACKNOW)
166 (void) tcp_output(tp);
167
168 if (V_nolocaltimewait && (
169 #ifdef INET6
170 isipv6 ? in6_localip(&inp->in6p_faddr) :
171 #endif
172 #ifdef INET
173 in_localip(inp->inp_faddr)
174 #else
175 false
176 #endif
177 )) {
178 if ((tp = tcp_close(tp)) != NULL)
179 INP_WUNLOCK(inp);
180 return;
181 }
182
183 tcp_timer_activate(tp, TT_2MSL, 2 * tcp_eff_msl(tp));
184 INP_WUNLOCK(inp);
185 }
186
187 /*
188 * Returns true if the TIME_WAIT state was killed and we should start over,
189 * looking for a pcb in the listen state. Otherwise returns false and frees
190 * the mbuf.
191 *
192 * For pure SYN-segments the PCB shall be read-locked and the tcpopt pointer
193 * may be NULL. For the rest write-lock and valid tcpopt.
194 */
195 bool
tcp_twcheck(struct inpcb * inp,struct tcpopt * to,struct tcphdr * th,struct mbuf * m,int tlen)196 tcp_twcheck(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th,
197 struct mbuf *m, int tlen)
198 {
199 struct tcpcb *tp = intotcpcb(inp);
200 char *s;
201 int thflags;
202 tcp_seq seq;
203
204 NET_EPOCH_ASSERT();
205 INP_LOCK_ASSERT(inp);
206
207 thflags = tcp_get_flags(th);
208 #ifdef INVARIANTS
209 if ((thflags & (TH_SYN | TH_ACK)) == TH_SYN)
210 INP_RLOCK_ASSERT(inp);
211 else {
212 INP_WLOCK_ASSERT(inp);
213 KASSERT(to != NULL,
214 ("%s: called without options on a non-SYN segment",
215 __func__));
216 }
217 #endif
218
219 /*
220 * NOTE: for FIN_WAIT_2 (to be added later),
221 * must validate sequence number before accepting RST
222 */
223
224 /*
225 * If the segment contains RST:
226 * Drop the segment - see Stevens, vol. 2, p. 964 and
227 * RFC 1337.
228 */
229 if (thflags & TH_RST)
230 goto drop;
231
232 #if 0
233 /* PAWS not needed at the moment */
234 /*
235 * RFC 1323 PAWS: If we have a timestamp reply on this segment
236 * and it's less than ts_recent, drop it.
237 */
238 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
239 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
240 if ((thflags & TH_ACK) == 0)
241 goto drop;
242 goto ack;
243 }
244 /*
245 * ts_recent is never updated because we never accept new segments.
246 */
247 #endif
248
249 /* Honor the drop_synfin sysctl variable. */
250 if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
251 if ((s = tcp_log_addrs(&inp->inp_inc, th, NULL, NULL))) {
252 log(LOG_DEBUG, "%s; %s: "
253 "SYN|FIN segment ignored (based on "
254 "sysctl setting)\n", s, __func__);
255 free(s, M_TCPLOG);
256 }
257 goto drop;
258 }
259
260 /*
261 * If a new connection request is received
262 * while in TIME_WAIT, drop the old connection
263 * and start over if the sequence numbers
264 * are above the previous ones.
265 * Allow UDP port number changes in this case.
266 */
267 if (((thflags & (TH_SYN | TH_ACK)) == TH_SYN) &&
268 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
269 /*
270 * In case we can't upgrade our lock just pretend we have
271 * lost this packet.
272 */
273 if (INP_TRY_UPGRADE(inp) == 0)
274 goto drop;
275 if ((tp = tcp_close(tp)) != NULL)
276 INP_WUNLOCK(inp);
277 TCPSTAT_INC(tcps_tw_recycles);
278 return (true);
279 }
280
281 /*
282 * Send RST if UDP port numbers don't match
283 */
284 if (tp->t_port != m->m_pkthdr.tcp_tun_port) {
285 if (tcp_get_flags(th) & TH_ACK) {
286 tcp_respond(tp, mtod(m, void *), th, m,
287 (tcp_seq)0, th->th_ack, TH_RST);
288 } else {
289 if (tcp_get_flags(th) & TH_SYN)
290 tlen++;
291 if (tcp_get_flags(th) & TH_FIN)
292 tlen++;
293 tcp_respond(tp, mtod(m, void *), th, m,
294 th->th_seq+tlen, (tcp_seq)0, TH_RST|TH_ACK);
295 }
296 INP_UNLOCK(inp);
297 TCPSTAT_INC(tcps_tw_resets);
298 return (false);
299 }
300
301 /*
302 * Drop the segment if it does not contain an ACK.
303 */
304 if ((thflags & TH_ACK) == 0)
305 goto drop;
306
307 INP_WLOCK_ASSERT(inp);
308
309 /*
310 * If timestamps were negotiated during SYN/ACK and a
311 * segment without a timestamp is received, silently drop
312 * the segment, unless the missing timestamps are tolerated.
313 * See section 3.2 of RFC 7323.
314 */
315 if (((to->to_flags & TOF_TS) == 0) && (tp->ts_recent != 0) &&
316 (V_tcp_tolerate_missing_ts == 0)) {
317 goto drop;
318 }
319
320 /*
321 * Reset the 2MSL timer if this is a duplicate FIN.
322 */
323 if (thflags & TH_FIN) {
324 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
325 if (seq + 1 == tp->rcv_nxt)
326 tcp_timer_activate(tp, TT_2MSL, 2 * tcp_eff_msl(tp));
327 }
328
329 /*
330 * Acknowledge the segment if it has data or is not a duplicate ACK.
331 */
332 if (thflags != TH_ACK || tlen != 0 ||
333 th->th_seq != tp->rcv_nxt || th->th_ack != tp->snd_nxt) {
334 TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
335 tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
336 tp->snd_nxt, TH_ACK);
337 INP_UNLOCK(inp);
338 TCPSTAT_INC(tcps_tw_responds);
339 return (false);
340 }
341 drop:
342 TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
343 INP_UNLOCK(inp);
344 m_freem(m);
345 return (false);
346 }
347