OpenVPN
mudp.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "multi.h"
30#include <inttypes.h>
31#include "forward.h"
32
33#include "memdbg.h"
34#include "ssl_pkt.h"
35
36#ifdef HAVE_SYS_INOTIFY_H
37#include <sys/inotify.h>
38#endif
39
55static void
56send_standalone_reply(struct multi_context *m, struct buffer *buf, const char *prefix,
57 const char *detail, struct link_socket *sock)
58{
59 struct context *c = &m->top;
60
61 /* dco-win server requires prepend with sockaddr, so preserve offset */
63 buf_copy(&c->c2.buffers->aux_buf, buf);
64
65 msg_set_prefix(prefix);
66 c->c2.to_link = c->c2.buffers->aux_buf;
67 c->c2.to_link_addr = &c->c2.from;
68 msg(D_MULTI_DEBUG, "%s", detail);
69 process_outgoing_link(c, sock);
70 c->c2.to_link.len = 0;
71 c->c2.to_link_addr = NULL;
72 msg_set_prefix(NULL);
73}
74
75static void
77 struct tls_auth_standalone *tas, struct session_id *sid,
78 bool request_resend_wkc, struct link_socket *sock)
79{
82 uint8_t header = 0 | (P_CONTROL_HARD_RESET_SERVER_V2 << P_OPCODE_SHIFT);
83 struct buffer buf = tls_reset_standalone(&state->tls_wrap_tmp, tas, sid,
84 &state->peer_session_id, header, request_resend_wkc);
85
86 send_standalone_reply(m, &buf, "Connection Attempt",
87 "Reset packet from client, sending HMAC based reset challenge", sock);
88}
89
90
91/* Returns true if this packet should create a new session */
92static bool
94 struct mroute_addr addr, struct link_socket *sock)
95{
96 ASSERT(m->top.c2.tls_auth_standalone);
97
99
101
102 verdict = tls_pre_decrypt_lite(tas, state, &m->top.c2.from, &m->top.c2.buf);
103
104 hmac_ctx_t *hmac = m->top.c2.session_id_hmac;
105 struct openvpn_sockaddr *from = &m->top.c2.from.dest;
106 int handwindow = m->top.options.handshake_window;
107
108 if (verdict == VERDICT_VALID_RESET_V3 || verdict == VERDICT_VALID_RESET_V2)
109 {
110 /* Check if we are still below our limit for sending out
111 * responses */
113 {
114 return false;
115 }
116 }
117
118 if (verdict == VERDICT_VALID_RESET_V3)
119 {
120 /* Extract the packet id to check if it has the special format that
121 * indicates early negotiation support */
122 struct packet_id_net pin;
123 struct buffer tmp = m->top.c2.buf;
125 ASSERT(packet_id_read(&pin, &tmp, true));
126
127 /* The most significant byte is 0x0f if early negotiation is supported */
129
130 /* All clients that support early negotiation and tls-crypt are assumed
131 * to also support resending the WKc in the 2nd packet */
133 {
134 /* Calculate the session ID HMAC for our reply and create reset packet */
135 struct session_id sid =
136 calculate_session_id_hmac(state->peer_session_id, from, hmac, handwindow, 0);
137 send_hmac_reset_packet(m, state, tas, &sid, true, sock);
138
139 return false;
140 }
141 else
142 {
143 /* For tls-crypt-v2 we need to keep the state of the first packet
144 * to store the unwrapped key if the client doesn't support resending
145 * the wrapped key. Unless the user specifically disallowed
146 * compatibility with such clients to avoid state exhaustion */
148 {
149 struct gc_arena gc = gc_new();
150 const char *peer = print_link_socket_actual(&m->top.c2.from, &gc);
152 "tls-crypt-v2 force-cookie is enabled, "
153 "ignoring connection attempt from old client (%s)",
154 peer);
155 gc_free(&gc);
156 return false;
157 }
158 else
159 {
160 return true;
161 }
162 }
163 }
164 else if (verdict == VERDICT_VALID_RESET_V2)
165 {
166 /* Calculate the session ID HMAC for our reply and create reset packet */
167 struct session_id sid =
168 calculate_session_id_hmac(state->peer_session_id, from, hmac, handwindow, 0);
169
170 send_hmac_reset_packet(m, state, tas, &sid, false, sock);
171
172 /* We have a reply do not create a new session */
173 return false;
174 }
175 else if (verdict == VERDICT_VALID_CONTROL_V1 || verdict == VERDICT_VALID_ACK_V1
176 || verdict == VERDICT_VALID_WKC_V1)
177 {
178 /* ACK_V1 contains the peer id (our id) while CONTROL_V1 can but does not
179 * need to contain the peer id */
180 struct gc_arena gc = gc_new();
181
182 bool pkt_is_ack = (verdict == VERDICT_VALID_ACK_V1);
183 bool ret = check_session_hmac_and_pkt_id(state, from, hmac, handwindow, pkt_is_ack);
184
185 const char *peer = print_link_socket_actual(&m->top.c2.from, &gc);
186 uint8_t pkt_firstbyte = *BPTR(&m->top.c2.buf);
187 int op = pkt_firstbyte >> P_OPCODE_SHIFT;
188
189 if (!ret)
190 {
191 msg(D_MULTI_MEDIUM, "Packet (%s) with invalid or missing SID from"
192 " %s or wrong packet id",
193 packet_opcode_name(op), peer);
194 }
195 else
196 {
198 "Valid packet (%s) with HMAC challenge from peer (%s), "
199 "accepting new connection.",
200 packet_opcode_name(op), peer);
201 }
202 gc_free(&gc);
203
204 return ret;
205 }
206
207 /* VERDICT_INVALID */
208 return false;
209}
210
218static struct multi_instance *
220 struct link_socket *sock,
221 struct mroute_addr *real,
222 const uint64_t hv,
223 struct hash_bucket *bucket)
224{
225 struct hash *hash = m->hash;
226 struct tls_pre_decrypt_state state = { 0 };
227 struct multi_instance *mi = NULL;
228 struct gc_arena gc = gc_new();
229
231 {
233 "MULTI: Connection attempt from %s ignored while server is "
234 "shutting down",
235 mroute_addr_print(real, &gc));
236 }
237 else if (do_pre_decrypt_check(m, &state, *real, sock))
238 {
239 /* This is an unknown session but with valid tls-auth/tls-crypt
240 * (or no auth at all). If this is the initial packet of a
241 * session, we just send a reply with a HMAC session id and
242 * do not generate a session slot */
243
245 {
246 /* a successful three-way handshake only counts against
247 * connect-freq but not against connect-freq-initial */
249
250 mi = multi_create_instance(m, real, sock);
251 if (mi)
252 {
253 hash_add_fast(hash, bucket, &mi->real, hv, mi);
254 mi->did_real_hash = true;
256
257 /* If we have a session id already, ensure that the
258 * state is using the same */
261 {
263 struct tls_session *session =
266 }
267 }
268 }
269 else
270 {
272 "MULTI: Connection from %s would exceed new connection frequency limit as controlled by --connect-freq",
273 mroute_addr_print(real, &gc));
274 }
275 }
277 gc_free(&gc);
278 return mi;
279}
280
286struct multi_instance *
287multi_get_create_instance_udp(struct multi_context *m, bool *floated, struct link_socket *sock)
288{
289 struct gc_arena gc = gc_new();
290 struct mroute_addr real = { 0 };
291 struct multi_instance *mi = NULL;
292 struct hash *hash = m->hash;
293 real.proto = sock->info.proto;
294
295 if (mroute_extract_openvpn_sockaddr(&real, &m->top.c2.from.dest, true) && m->top.c2.buf.len > 0)
296 {
297 struct hash_element *he;
298 const uint64_t hv = hash_value(hash, &real);
299 struct hash_bucket *bucket = hash_bucket(hash, hv);
300 uint8_t *ptr = BPTR(&m->top.c2.buf);
301 uint8_t op = ptr[0] >> P_OPCODE_SHIFT;
302 bool v2 = (op == P_DATA_V2) && (m->top.c2.buf.len >= (1 + 3));
303 bool peer_id_disabled = false;
304
305 /* make sure buffer has enough length to read opcode (1 byte) and peer-id (3 bytes) */
306 if (v2)
307 {
308 uint32_t peer_id = ((uint32_t)ptr[1] << 16) | ((uint32_t)ptr[2] << 8) | ((uint32_t)ptr[3]);
309 peer_id_disabled = (peer_id == MAX_PEER_ID);
310
311 if (!peer_id_disabled && (peer_id < m->max_clients) && m->instances[peer_id])
312 {
313 /* Floating on TCP will never be possible, so ensure we only process
314 * UDP clients */
315 if (m->instances[peer_id]->context.c2.link_sockets[0]->info.proto
316 == sock->info.proto)
317 {
318 mi = m->instances[peer_id];
319 *floated = !link_socket_actual_match(&mi->context.c2.from, &m->top.c2.from);
320
321 if (*floated)
322 {
323 /* reset prefix, since here we are not sure peer is the one it claims to be
324 */
326 msg(D_MULTI_MEDIUM, "Float requested for peer %" PRIu32 " to %s", peer_id,
327 mroute_addr_print(&real, &gc));
328 }
329 }
330 }
331 }
332 if (!v2 || peer_id_disabled)
333 {
334 he = hash_lookup_fast(hash, bucket, &real, hv);
335 if (he)
336 {
337 mi = (struct multi_instance *)he->value;
338 }
339 }
340
341 /* we have no existing multi instance for this connection */
342 if (!mi)
343 {
344 mi = handle_connection_attempt(m, sock, &real, hv, bucket);
345 }
346
347#ifdef ENABLE_DEBUG
349 {
350 const char *status = mi ? "[ok]" : "[failed]";
351
352 dmsg(D_MULTI_DEBUG, "GET INST BY REAL: %s %s", mroute_addr_print(&real, &gc), status);
353 }
354#endif
355 }
356
357 gc_free(&gc);
358 ASSERT(!(mi && mi->halt));
359 return mi;
360}
361
362/*
363 * Send a packet to UDP socket.
364 */
365static inline void
366multi_process_outgoing_link(struct multi_context *m, const unsigned int mpp_flags)
367{
369 if (mi)
370 {
371 multi_process_outgoing_link_dowork(m, mi, mpp_flags);
372 }
373}
374
375/*
376 * Process a UDP socket event.
377 */
378void
379multi_process_io_udp(struct multi_context *m, struct link_socket *sock, unsigned int rwflags)
380{
381 const unsigned int mpp_flags = (MPP_PRE_SELECT | MPP_CLOSE_ON_SIGNAL);
382
383 /* UDP port ready to accept write */
384 if (rwflags & SOCKET_WRITE)
385 {
386 multi_process_outgoing_link(m, mpp_flags);
387 }
388 /* Incoming data on UDP port */
389 else if (rwflags & SOCKET_READ)
390 {
391 read_incoming_link(&m->top, sock);
392 if (!IS_SIG(&m->top))
393 {
394 multi_process_incoming_link(m, NULL, mpp_flags, sock);
395 }
396 }
397}
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition buffer.h:713
#define BPTR(buf)
Definition buffer.h:124
static bool buf_advance(struct buffer *buf, ssize_t size)
Definition buffer.h:618
static void gc_free(struct gc_arena *a)
Definition buffer.h:1081
#define buf_init(buf, offset)
Definition buffer.h:211
static struct gc_arena gc_new(void)
Definition buffer.h:1073
#define CO_FORCE_TLSCRYPTV2_COOKIE
Bit-flag indicating that we do not allow clients that do not support resending the wrapped client key...
Definition crypto.h:367
#define D_MULTI_ERRORS
Definition errlevel.h:64
#define D_MULTI_MEDIUM
Definition errlevel.h:101
#define D_MULTI_DEBUG
Definition errlevel.h:126
#define SOCKET_READ
Definition event.h:60
#define SOCKET_WRITE
Definition event.h:61
Interface functions to the internal and external multiplexers.
#define TM_INITIAL
As yet un-trusted tls_session \ being negotiated.
Definition ssl_common.h:545
enum first_packet_verdict tls_pre_decrypt_lite(const struct tls_auth_standalone *tas, struct tls_pre_decrypt_state *state, const struct link_socket_actual *from, const struct buffer *buf)
Inspect an incoming packet for which no VPN tunnel is active, and determine whether a new VPN tunnel ...
Definition ssl_pkt.c:294
bool multi_process_incoming_link(struct multi_context *m, struct multi_instance *instance, const unsigned int mpp_flags, struct link_socket *sock)
Demultiplex and process a packet received over the external network interface.
Definition multi.c:3455
void process_outgoing_link(struct context *c, struct link_socket *sock)
Write a packet to the external network interface.
Definition forward.c:1746
void read_incoming_link(struct context *c, struct link_socket *sock)
Read a packet from the external network interface.
Definition forward.c:926
struct multi_instance * multi_get_create_instance_udp(struct multi_context *m, bool *floated, struct link_socket *sock)
Get a client instance based on real address.
Definition mudp.c:287
static SERVICE_STATUS status
Definition interactive.c:52
struct hash_element * hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv)
Definition list.c:79
static void hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv, void *value)
Definition list.h:146
static uint64_t hash_value(const struct hash *hash, const void *key)
Definition list.h:104
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition mroute.c:254
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition mroute.c:371
static struct multi_instance * handle_connection_attempt(struct multi_context *m, struct link_socket *sock, struct mroute_addr *real, const uint64_t hv, struct hash_bucket *bucket)
Handles a packet if no existing session exists for this incoming packet.
Definition mudp.c:219
static void send_standalone_reply(struct multi_context *m, struct buffer *buf, const char *prefix, const char *detail, struct link_socket *sock)
Send an already-built standalone control packet back to the peer that just contacted us (c2....
Definition mudp.c:56
static bool do_pre_decrypt_check(struct multi_context *m, struct tls_pre_decrypt_state *state, struct mroute_addr addr, struct link_socket *sock)
Definition mudp.c:93
static void send_hmac_reset_packet(struct multi_context *m, struct tls_pre_decrypt_state *state, struct tls_auth_standalone *tas, struct session_id *sid, bool request_resend_wkc, struct link_socket *sock)
Definition mudp.c:76
static void multi_process_outgoing_link(struct multi_context *m, const unsigned int mpp_flags)
Definition mudp.c:366
void multi_process_io_udp(struct multi_context *m, struct link_socket *sock, unsigned int rwflags)
Definition mudp.c:379
struct multi_instance * multi_create_instance(struct multi_context *m, const struct mroute_addr *real, struct link_socket *sock)
Definition multi.c:702
void ungenerate_prefix(struct multi_instance *mi)
Definition multi.c:465
void multi_assign_peer_id(struct multi_context *m, struct multi_instance *mi)
Assigns a peer-id to a a client and adds the instance to the the instances array of the multi_context...
Definition multi.c:4093
Header file for server-mode related structures and functions.
#define MPP_CLOSE_ON_SIGNAL
Definition multi.h:270
static struct multi_instance * multi_process_outgoing_link_pre(struct multi_context *m)
Definition multi.h:409
#define MPP_PRE_SELECT
Definition multi.h:269
static bool multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Definition multi.h:657
static bool check_debug_level(msglvl_t level)
Definition error.h:251
static void msg_set_prefix(const char *prefix)
Definition error.h:330
#define dmsg(flags,...)
Definition error.h:172
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
#define MAX_PEER_ID
Definition openvpn.h:550
bool frequency_limit_event_allowed(struct frequency_limit *f)
Definition otime.c:158
bool packet_id_read(struct packet_id_net *pin, struct buffer *buf, bool long_form)
Definition packet_id.c:319
static void reset_packet_id_send(struct packet_id_send *p)
Reset the current send packet id to its initial state.
Definition packet_id.h:312
void reflect_filter_rate_limit_decrease(struct initial_packet_rate_limit *irl)
decreases the counter of initial packets seen, so connections that successfully completed the three-w...
bool reflect_filter_rate_limit_check(struct initial_packet_rate_limit *irl)
checks if the connection is still allowed to connect under the rate limit.
static bool session_id_defined(const struct session_id *sid1)
Definition session_id.h:53
#define SID_SIZE
Definition session_id.h:44
#define IS_SIG(c)
Definition sig.h:47
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
static bool link_socket_actual_match(const struct link_socket_actual *a1, const struct link_socket_actual *a2)
bool session_skip_to_pre_start(struct tls_session *session, struct tls_pre_decrypt_state *state, struct link_socket_actual *from)
Definition ssl.c:2498
struct session_id calculate_session_id_hmac(struct session_id client_sid, const struct openvpn_sockaddr *from, hmac_ctx_t *hmac, int handwindow, int offset)
Calculates the HMAC based server session id based on a client session id and socket addr.
Definition ssl_pkt.c:460
void free_tls_pre_decrypt_state(struct tls_pre_decrypt_state *state)
Definition ssl_pkt.c:272
bool check_session_hmac_and_pkt_id(struct tls_pre_decrypt_state *state, const struct openvpn_sockaddr *from, hmac_ctx_t *hmac, int handwindow, bool pkt_is_ack)
Checks if a control packet has a correct HMAC server session id.
Definition ssl_pkt.c:500
struct buffer tls_reset_standalone(struct tls_wrap_ctx *ctx, struct tls_auth_standalone *tas, struct session_id *own_sid, struct session_id *remote_sid, uint8_t header, bool request_resend_wkc)
This function creates a reset packet using the information from the tls pre decrypt state.
Definition ssl_pkt.c:404
SSL control channel wrap/unwrap and decode functions.
#define P_DATA_V2
Definition ssl_pkt.h:48
#define P_OPCODE_SHIFT
Definition ssl_pkt.h:39
static const char * packet_opcode_name(int op)
Definition ssl_pkt.h:239
#define EARLY_NEG_MASK
Definition ssl_pkt.h:312
#define P_CONTROL_HARD_RESET_SERVER_V2
Definition ssl_pkt.h:52
first_packet_verdict
Definition ssl_pkt.h:85
@ VERDICT_VALID_ACK_V1
This packet is a valid ACK control packet from the peer, i.e.
Definition ssl_pkt.h:94
@ VERDICT_VALID_WKC_V1
The packet is a valid control packet with appended wrapped client key.
Definition ssl_pkt.h:96
@ VERDICT_VALID_RESET_V2
This packet is a valid reset packet from the peer (all but tls-crypt-v2)
Definition ssl_pkt.h:87
@ VERDICT_VALID_RESET_V3
This is a valid v3 reset (tls-crypt-v2)
Definition ssl_pkt.h:89
@ VERDICT_VALID_CONTROL_V1
This packet is a valid control packet from the peer.
Definition ssl_pkt.h:91
#define EARLY_NEG_START
Definition ssl_pkt.h:313
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
int offset
Offset in bytes of the actual content within the allocated memory.
Definition buffer.h:64
hmac_ctx_t * session_id_hmac
the HMAC we use to generate and verify our syn cookie like session ids from the server.
Definition openvpn.h:338
struct tls_auth_standalone * tls_auth_standalone
TLS state structure required for the initial authentication of a client's connection attempt.
Definition openvpn.h:326
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:323
struct link_socket_actual from
Definition openvpn.h:245
struct buffer to_link
Definition openvpn.h:377
struct link_socket ** link_sockets
Definition openvpn.h:237
struct link_socket_actual * to_link_addr
Definition openvpn.h:244
struct buffer buf
Definition openvpn.h:375
struct context_buffers * buffers
Definition openvpn.h:367
struct buffer aux_buf
Definition openvpn.h:97
Contains all state information for one tunnel.
Definition openvpn.h:471
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:472
unsigned int flags
Bit-flags determining behavior of security operation functions.
Definition crypto.h:386
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Definition crypto.h:333
int signal_received
Definition multi.h:62
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
void * value
Definition list.h:41
Definition list.h:53
uint8_t proto
Definition mroute.h:84
Main OpenVPN server state structure.
Definition multi.h:162
struct initial_packet_rate_limit * initial_rate_limiter
Definition multi.h:180
struct deferred_signal_schedule_entry deferred_shutdown_signal
Definition multi.h:214
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition multi.h:169
struct frequency_limit * new_connection_limiter
Definition multi.h:179
struct context top
Storage structure for process-wide configuration.
Definition multi.h:201
struct multi_instance ** instances
Array of multi_instances with the size of max_clients.
Definition multi.h:163
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct mroute_addr real
External network address of the remote peer.
Definition multi.h:121
bool did_real_hash
Definition multi.h:134
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:142
int handshake_window
Definition options.h:651
Data structure for describing the packet id that is received/send to the network.
Definition packet_id.h:191
struct packet_id_send send
Definition packet_id.h:200
struct packet_id_rec rec
Definition packet_id.h:201
struct tls_wrap_ctx tls_wrap
Definition ssl_pkt.h:79
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer.
Definition ssl_common.h:711
int n_sessions
Number of sessions negotiated thus far.
Definition ssl_common.h:630
struct that stores the temporary data for the tls lite decrypt functions
Definition ssl_pkt.h:106
struct session_id peer_session_id
Definition ssl_pkt.h:109
struct session_id server_session_id
Definition ssl_pkt.h:110
struct tls_wrap_ctx tls_wrap_tmp
Definition ssl_pkt.h:107
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:489
struct crypto_options opt
Crypto state.
Definition ssl_common.h:283
struct gc_arena gc
Definition test_ssl.c:133