OpenVPN
push_util.c
Go to the documentation of this file.
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include "push.h"
6#include "buffer.h"
7
8#ifdef ENABLE_MANAGEMENT
9#include "multi.h"
10#include "ssl_util.h"
11#endif
12
13int
14process_push_update(struct context *c, struct options *o, uint64_t permission_mask,
15 uint64_t *option_types_found, struct buffer *buf, bool msg_sender)
16{
17 int ret = PUSH_MSG_ERROR;
18 const int ch = buf_read_u8(buf);
19 if (ch == ',')
20 {
21 if (apply_push_options(c, o, buf, permission_mask, option_types_found, c->c2.es,
22 true))
23 {
24 switch (o->push_continuation)
25 {
26 case 0:
27 case 1:
28 ret = PUSH_MSG_UPDATE;
29 break;
30
31 case 2:
33 break;
34 }
35 }
36 else if (!msg_sender)
37 {
38 throw_signal_soft(SIGUSR1, "Offending option received from server");
39 }
40 }
41 else if (ch == '\0')
42 {
43 ret = PUSH_MSG_UPDATE;
44 }
45
46 return ret;
47}
48
49#ifdef ENABLE_MANAGEMENT
54static size_t
55find_first_comma_of_next_bundle(const char *str, size_t ix)
56{
57 while (ix > 0)
58 {
59 if (str[ix] == ',')
60 {
61 return ix;
62 }
63 ix--;
64 }
65 return 0;
66}
67
68/* Allocate memory and assemble the final message */
69static struct buffer
71{
72 size_t src_len = strlen(src);
73 size_t con_len = continuation ? strlen(continuation) : 0;
74 struct buffer buf = alloc_buf_gc(src_len + sizeof(push_update_cmd) + con_len + 2, gc);
75
76 buf_printf(&buf, "%s,%s%s", push_update_cmd, src, continuation ? continuation : "");
77
78 return buf;
79}
80
81/* Split the message (if necessary) and fill msgs with the message chunks.
82 * Return `false` on failure and `true` on success.
83 */
84static bool
85message_splitter(const char *s, struct buffer_list *msgs, struct gc_arena *gc, const size_t safe_cap)
86{
87 if (!s || !*s)
88 {
89 return false;
90 }
91
92 char *str = string_alloc(s, gc);
93
94 while (*str)
95 {
96 /* + ',' - '/0' */
97 if (strlen(str) > safe_cap)
98 {
100 if (!ci)
101 {
102 /* if no commas were found go to fail, do not send any message */
103 return false;
104 }
105 /* copy from current position to (ci - 1) */
106 str[ci] = '\0';
107 struct buffer tmp = forge_msg(str, ",push-continuation 2", gc);
109 str += ci + 1;
110 }
111 else
112 {
113 if (msgs->head)
114 {
115 struct buffer tmp = forge_msg(str, ",push-continuation 1", gc);
117 }
118 else
119 {
120 struct buffer tmp = forge_msg(str, NULL, gc);
122 }
123 break;
124 }
125 }
126 return true;
127}
128
129/* send the message(s) prepared to one single client */
130static bool
132{
133 if (!msgs->head)
134 {
135 return false;
136 }
137
138 uint64_t option_types_found = 0;
139 struct context *c = &mi->context;
140 struct options o;
141 CLEAR(o);
142
143 /* Set canary values to detect ifconfig options in push-update messages.
144 * These placeholder strings will be overwritten to NULL by the option
145 * parser if -ifconfig or -ifconfig-ipv6 options are present in the
146 * push-update.
147 */
148 const char *canary = "canary";
149 o.ifconfig_local = canary;
150 o.ifconfig_ipv6_local = canary;
151
152 struct buffer_entry *e = msgs->head;
153 while (e)
154 {
156 {
157 uninit_options(&o);
158 return false;
159 }
160
161 /* After sending the control message, we parse it, miming the behavior
162 * of `process_incoming_push_msg()` and we fill an empty `options` struct
163 * with the new options. If an `ifconfig_local` or `ifconfig_ipv6_local`
164 * options is found we update the vhash accordingly, so that the pushed
165 * ifconfig/ifconfig-ipv6 options can actually work.
166 * If we don't do that, packets arriving from the client with the
167 * new address will be rejected and packets for the new address
168 * will not be routed towards the client.
169 * Using `buf_string_compare_advance()` we mimic the behavior
170 * inside `process_incoming_push_msg()`. However, we don't need
171 * to check the return value here because we just want to `advance`,
172 * meaning we skip the `push_update_cmd' we added earlier.
173 * Also we need to make a temporary copy so we can buf_advance()
174 * without modifying original buffer.
175 */
176 struct buffer tmp_msg = e->buf;
179 if (process_push_update(c, &o, permission_mask, &option_types_found, &tmp_msg, true) == PUSH_MSG_ERROR)
180 {
181 msg(M_WARN, "Failed to process push update message sent to client ID: %u", c->c2.tls_multi->rx_peer_id);
182 }
183 e = e->next;
184 }
185
186 if (option_types_found & OPT_P_UP)
187 {
188 /* -ifconfig */
189 if (!o.ifconfig_local && mi->context.c2.push_ifconfig_defined)
190 {
191 unlearn_ifconfig(m, mi);
192 }
193 /* -ifconfig-ipv6 */
194 if (!o.ifconfig_ipv6_local && mi->context.c2.push_ifconfig_ipv6_defined)
195 {
197 }
198
199 if (o.ifconfig_local && !strcmp(o.ifconfig_local, canary))
200 {
201 o.ifconfig_local = NULL;
202 }
203 if (o.ifconfig_ipv6_local && !strcmp(o.ifconfig_ipv6_local, canary))
204 {
205 o.ifconfig_ipv6_local = NULL;
206 }
207
208 /* new ifconfig or new ifconfig-ipv6 */
209 update_vhash(m, mi, o.ifconfig_local, o.ifconfig_ipv6_local);
210 }
211
213 return true;
214}
215
216/* Return true if the client supports push-update */
217static bool
219{
221 const unsigned int iv_proto_peer = extract_iv_proto(mi->context.c2.tls_multi->peer_info);
223 {
224 return false;
225 }
226
227 return true;
228}
229
244static int
245send_push_update(struct multi_context *m, const void *target, const char *msg, const push_update_type type, const size_t push_bundle_size)
246{
247 if (dco_enabled(&m->top.options))
248 {
249 msg(M_WARN, "WARN: PUSH_UPDATE messages cannot currently be sent while DCO is enabled."
250 " To send a PUSH_UPDATE message, be sure to use the --disable-dco option.");
251 return 0;
252 }
253
254 if (!msg || !*msg || !m || (!target && type != UPT_BROADCAST))
255 {
256 return -EINVAL;
257 }
258
259 struct gc_arena gc = gc_new();
260 /* extra space for possible trailing ifconfig and push-continuation */
261 const size_t extra = 84 + sizeof(push_update_cmd);
262 /* push_bundle_size is the maximum size of a message, so if the message
263 * we want to send exceeds that size we have to split it into smaller messages */
264 ASSERT(push_bundle_size > extra);
265 const size_t safe_cap = push_bundle_size - extra;
266 struct buffer_list *msgs = buffer_list_new();
267
268 if (!message_splitter(msg, msgs, &gc, safe_cap))
269 {
270 buffer_list_free(msgs);
271 gc_free(&gc);
272 return -EINVAL;
273 }
274
275 if (type == UPT_BY_CID)
276 {
277 struct multi_instance *mi = lookup_by_cid(m, *((unsigned long *)target));
278
279 if (!mi)
280 {
281 buffer_list_free(msgs);
282 gc_free(&gc);
283 return -ENOENT;
284 }
285
286 if (!support_push_update(mi))
287 {
288 msg(M_CLIENT, "PUSH_UPDATE: not sending message to unsupported peer with ID: %u", mi->context.c2.tls_multi->rx_peer_id);
289 buffer_list_free(msgs);
290 gc_free(&gc);
291 return 0;
292 }
293
294 if (!mi->halt
295 && send_single_push_update(m, mi, msgs))
296 {
297 buffer_list_free(msgs);
298 gc_free(&gc);
299 return 1;
300 }
301 else
302 {
303 buffer_list_free(msgs);
304 gc_free(&gc);
305 return 0;
306 }
307 }
308
309 int count = 0;
310
311 for (uint32_t i = 0; i <= m->max_peerid; i++)
312 {
313 struct multi_instance *curr_mi = m->instances[i];
314
315 if (!curr_mi || curr_mi->halt || !support_push_update(curr_mi))
316 {
317 continue;
318 }
319
320 /* Type is UPT_BROADCAST so we update every client */
321 if (!send_single_push_update(m, curr_mi, msgs))
322 {
323 msg(M_CLIENT, "ERROR: Peer ID: %u has not been updated", curr_mi->context.c2.tls_multi->rx_peer_id);
324 continue;
325 }
326 count++;
327 }
328
329 buffer_list_free(msgs);
330 gc_free(&gc);
331 return count;
332}
333
334#define RETURN_UPDATE_STATUS(n_sent) \
335 do \
336 { \
337 if ((n_sent) > 0) \
338 { \
339 msg(M_CLIENT, "SUCCESS: %d client(s) updated", (n_sent)); \
340 return true; \
341 } \
342 else \
343 { \
344 msg(M_CLIENT, "ERROR: no client updated"); \
345 return false; \
346 } \
347 } while (0)
348
349bool
351{
352 int n_sent = send_push_update(arg, NULL, options, UPT_BROADCAST, PUSH_BUNDLE_SIZE);
353
354 RETURN_UPDATE_STATUS(n_sent);
355}
356
357bool
358management_callback_send_push_update_by_cid(void *arg, unsigned long cid, const char *options)
359{
360 int n_sent = send_push_update(arg, &cid, options, UPT_BY_CID, PUSH_BUNDLE_SIZE);
361
362 RETURN_UPDATE_STATUS(n_sent);
363}
364#endif /* ifdef ENABLE_MANAGEMENT */
bool buf_string_compare_advance(struct buffer *src, const char *match)
Compare the head of src with match and advance past it if equal.
Definition buffer.c:743
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition buffer.c:1185
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1194
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
void buffer_list_push(struct buffer_list *ol, const char *str)
Allocates and appends a new buffer containing str as data to ol.
Definition buffer.c:1225
Buffer management functions and garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static int buf_read_u8(struct buffer *buf)
Read and consume a uint8_t from the front of a buffer.
Definition buffer.h:1495
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
#define PUSH_BUNDLE_SIZE
Definition common.h:89
#define D_PUSH
Definition errlevel.h:81
bool send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel)
Definition forward.c:414
uint64_t pull_permission_mask(const struct context *c)
Definition init.c:2493
struct multi_instance * lookup_by_cid(struct multi_context *m, const unsigned long cid)
Definition multi.c:4004
void unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:4456
void unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:4444
void update_vhash(struct multi_context *m, struct multi_instance *mi, const char *new_ip, const char *new_ipv6)
Update the vhash with new IP/IPv6 addresses in the multi_context when a push-update message containin...
Definition multi.c:4478
Header file for server-mode related structures and functions.
#define CLEAR(x)
Definition basic.h:32
#define M_CLIENT
Definition error.h:108
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
void uninit_options(struct options *o)
Definition options.c:907
#define OPT_P_UP
Definition options.h:742
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:981
bool apply_push_options(struct context *c, struct options *options, struct buffer *buf, uint64_t permission_mask, uint64_t *option_types_found, struct env_set *es, bool is_update)
push_update_type
Definition push.h:47
@ UPT_BY_CID
Definition push.h:49
@ UPT_BROADCAST
Definition push.h:48
#define PUSH_MSG_ERROR
Definition push.h:28
#define PUSH_MSG_UPDATE
Definition push.h:35
#define push_update_cmd
Definition push.h:38
#define PUSH_MSG_CONTINUATION
Definition push.h:33
static bool message_splitter(const char *s, struct buffer_list *msgs, struct gc_arena *gc, const size_t safe_cap)
Definition push_util.c:85
int process_push_update(struct context *c, struct options *o, uint64_t permission_mask, uint64_t *option_types_found, struct buffer *buf, bool msg_sender)
Handles the receiving of a push-update message and applies updates to the specified options.
Definition push_util.c:14
static bool support_push_update(const struct multi_instance *mi)
Definition push_util.c:218
static struct buffer forge_msg(const char *src, const char *continuation, struct gc_arena *gc)
Definition push_util.c:70
static bool send_single_push_update(struct multi_context *m, struct multi_instance *mi, struct buffer_list *msgs)
Definition push_util.c:131
#define RETURN_UPDATE_STATUS(n_sent)
Definition push_util.c:334
static int send_push_update(struct multi_context *m, const void *target, const char *msg, const push_update_type type, const size_t push_bundle_size)
A function to send a PUSH_UPDATE control message from server to client(s).
Definition push_util.c:245
bool management_callback_send_push_update_by_cid(void *arg, unsigned long cid, const char *options)
Definition push_util.c:358
static size_t find_first_comma_of_next_bundle(const char *str, size_t ix)
Return index of last , or 0 if it didn't find any.
Definition push_util.c:55
bool management_callback_send_push_update_broadcast(void *arg, const char *options)
Definition push_util.c:350
void throw_signal_soft(const int signum, const char *signal_text)
Throw a soft global signal.
Definition sig.c:204
#define IV_PROTO_PUSH_UPDATE
Supports push-update.
Definition ssl.h:117
SSL utility functions.
static unsigned int extract_iv_proto(const char *peer_info)
Extracts the IV_PROTO variable and returns its value or 0 if it cannot be extracted.
Definition ssl_util.h:77
One node in a buffer_list linked list.
Definition buffer.h:2161
struct buffer buf
The buffer stored in this list node.
Definition buffer.h:2162
A singly-linked list of buffers, with head/tail pointers for O(1) push.
Definition buffer.h:2168
struct buffer_entry * head
Next item to pop/peek.
Definition buffer.h:2169
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
bool push_ifconfig_ipv6_defined
Definition openvpn.h:405
bool push_ifconfig_defined
Definition openvpn.h:399
struct env_set * es
Definition openvpn.h:394
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:300
Contains all state information for one tunnel.
Definition openvpn.h:445
struct context_2 c2
Level 2 context.
Definition openvpn.h:490
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Main OpenVPN server state structure.
Definition multi.h:170
uint32_t max_peerid
highest currently allocated peer-id and maximum allocated/valid index in instances
Definition multi.h:174
struct multi_instance ** instances
Array of multi_instances with the size of max_clients.
Definition multi.h:171
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:150
const char * ifconfig_ipv6_local
Definition options.h:329
const char * ifconfig_local
Definition options.h:327
int push_continuation
Definition options.h:558
char * peer_info
A multi-line string of general-purpose info received from peer over control channel.
Definition ssl_common.h:680
uint32_t rx_peer_id
Definition ssl_common.h:707
#define SIGUSR1
Definition syshead.h:57
struct gc_arena gc
Definition test_ssl.c:138