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