OpenVPN
test_probe.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 <stdarg.h>
30#include <stddef.h>
31#include <setjmp.h>
32#include <cmocka.h>
33
34#include "control_msg.h"
35#include "probe.h"
36#include "test_common.h"
37#include "test_probe.h"
38
39/* Write a probe request TLV and read it back; the fields must survive the
40 * round trip and the whole buffer must be consumed. */
41void
43{
44 struct gc_arena gc = gc_new();
45 struct buffer buf = alloc_buf_gc(128, &gc);
46
47 const struct probe_request in = {
48 .request_id = 0xdeadbeefcafef00dULL,
49 .timestamp = 0x0123456789abcdefULL,
51 };
52 assert_true(probe_request_write(&buf, &in));
53 assert_int_equal(BLEN(&buf), CTRL_MSG_TLV_HEADER_LEN + PROBE_REQUEST_LEN);
54
55 struct ctrl_msg_tlv_header hdr;
56 assert_true(ctrl_msg_tlv_peek_header(&buf, &hdr));
57 assert_int_equal(hdr.type, TLV_TYPE_PROBE_REQUEST);
58 assert_false(hdr.optional);
59 assert_int_equal(hdr.value_len, PROBE_REQUEST_LEN);
60
61 struct buffer value;
62 assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
63 assert_int_equal(BLEN(&value), PROBE_REQUEST_LEN);
64
65 struct probe_request out = { 0 };
66 assert_true(probe_request_read(&value, &out));
67 assert_true(in.request_id == out.request_id);
68 assert_true(in.timestamp == out.timestamp);
69 assert_int_equal(in.flags, out.flags);
70 /* the scan consumed header and value alike */
71 assert_int_equal(BLEN(&buf), 0);
72
73 gc_free(&gc);
74}
75
76/* The probe request wire format is locked to the field order of the spec
77 * (request_id, timestamp, flags), big-endian. */
78void
80{
81 struct gc_arena gc = gc_new();
82 struct buffer buf = alloc_buf_gc(128, &gc);
83
84 const struct probe_request in = {
85 .request_id = 0x1122334455667788ULL,
86 .timestamp = 0x0102030405060708ULL,
87 .flags = 0,
88 };
89 assert_true(probe_request_write(&buf, &in));
90
91 /* clang-format off */
92 const uint8_t expected[] = {
93 0x00, 0x02, /* TLV type 2, not optional */
94 0x00, 0x14, /* TLV value length = 20 */
95 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, /* request_id */
96 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, /* timestamp */
97 0x00, 0x00, 0x00, 0x00, /* flags */
98 };
99 /* clang-format on */
100 assert_int_equal(BLEN(&buf), sizeof(expected));
101 assert_memory_equal(BPTR(&buf), expected, sizeof(expected));
102
103 gc_free(&gc);
104}
105
106/* Write a probe reply TLV and read it back. */
107void
109{
110 struct gc_arena gc = gc_new();
111 struct buffer buf = alloc_buf_gc(128, &gc);
112
113 const struct probe_reply in = {
114 .request_id = 0x1122334455667788ULL,
115 .priority = 10,
116 .weight = 100,
117 .max_latency_diff = 25,
118 .connect_lifetime = 30,
120 };
121
122 assert_true(probe_reply_write(&buf, &in));
123 assert_int_equal(BLEN(&buf), CTRL_MSG_TLV_HEADER_LEN + PROBE_REPLY_LEN);
124
125 struct ctrl_msg_tlv_header hdr;
126 assert_true(ctrl_msg_tlv_peek_header(&buf, &hdr));
127 assert_int_equal(hdr.type, TLV_TYPE_PROBE_REPLY);
128 assert_false(hdr.optional);
129 assert_int_equal(hdr.value_len, PROBE_REPLY_LEN);
130
131 struct buffer value;
132 assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REPLY, &value));
133
134 struct probe_reply out = { 0 };
135 assert_true(probe_reply_read(&value, &out));
136 assert_true(in.request_id == out.request_id);
137 assert_int_equal(in.priority, out.priority);
138 assert_int_equal(in.weight, out.weight);
139 assert_int_equal(in.max_latency_diff, out.max_latency_diff);
140 assert_int_equal(in.connect_lifetime, out.connect_lifetime);
141 assert_int_equal(in.flags, out.flags);
142 assert_int_equal(BLEN(&buf), 0);
143
144 gc_free(&gc);
145}
146
147/* The probe reply wire format is locked to the field order of the spec
148 * (request_id, priority, weight, max_latency_diff, connect_lifetime, flags),
149 * big-endian. */
150void
152{
153 struct gc_arena gc = gc_new();
154 struct buffer buf = alloc_buf_gc(128, &gc);
155
156 const struct probe_reply in = {
157 .request_id = 0x1122334455667788ULL,
158 .priority = 10,
159 .weight = 100,
160 .max_latency_diff = 25,
161 .connect_lifetime = 30,
163 };
164
165 assert_true(probe_reply_write(&buf, &in));
166
167 /* clang-format off */
168 const uint8_t expected[] = {
169 0x00, 0x03, /* TLV type 3, not optional */
170 0x00, 0x12, /* TLV value length = 18 */
171 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, /* request_id */
172 0x00, 0x0a, /* priority = 10 */
173 0x00, 0x64, /* weight = 100 */
174 0x00, 0x19, /* max_latency_diff = 25 */
175 0x00, 0x1e, /* connect_lifetime = 30 */
176 0x00, 0x01, /* flags = RESEND_WKC */
177 };
178 /* clang-format on */
179 assert_int_equal(BLEN(&buf), sizeof(expected));
180 assert_memory_equal(BPTR(&buf), expected, sizeof(expected));
181
182 gc_free(&gc);
183}
184
185/* A TLV with a longer-than-known value must still parse: the known fields are
186 * read and the trailing bytes are skipped. Flag bits we do not know are kept
187 * rather than rejected, for the same forward compatibility. */
188void
190{
191 struct gc_arena gc = gc_new();
192 struct buffer buf = alloc_buf_gc(128, &gc);
193
194 const uint16_t extended_len = PROBE_REQUEST_LEN + 4;
195 assert_true(ctrl_msg_tlv_write_header(&buf, TLV_TYPE_PROBE_REQUEST, false, extended_len));
196 assert_true(buf_write_u64(&buf, 7)); /* request_id */
197 assert_true(buf_write_u64(&buf, 0xdeadbeefULL)); /* timestamp */
198 assert_true(buf_write_u32(&buf, 0x80000000)); /* flags, unknown bit set */
199 assert_true(buf_write_u32(&buf, 0x11223344)); /* unknown trailing field */
200
201 struct buffer value;
202 assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
203 assert_int_equal(BLEN(&value), extended_len);
204
205 struct probe_request out = { 0 };
206 assert_true(probe_request_read(&value, &out));
207 assert_true(out.request_id == 7);
208 assert_true(out.timestamp == 0xdeadbeefULL);
209 assert_true(out.flags == 0x80000000);
210 /* the unknown trailing field must have been consumed from the payload */
211 assert_int_equal(BLEN(&buf), 0);
212
213 gc_free(&gc);
214}
215
216/* A value shorter than the mandatory fields must be rejected, even when it
217 * holds enough bytes for some of the individual fields to read successfully. */
218void
220{
221 struct gc_arena gc = gc_new();
222 struct buffer buf = alloc_buf_gc(128, &gc);
223
224 /* 8 of the 20 mandatory value bytes: enough for the request_id alone */
225 assert_true(buf_write_u64(&buf, 0xdeadbeefULL));
226
227 struct probe_request out = { 0 };
228 assert_false(probe_request_read(&buf, &out));
229
230 gc_free(&gc);
231}
232
233/* The same for the reply, one u16 short of its 18 mandatory bytes. */
234void
236{
237 struct gc_arena gc = gc_new();
238 struct buffer buf = alloc_buf_gc(128, &gc);
239
240 assert_true(buf_write_u64(&buf, 1));
241 for (int i = 0; i < 4; i++)
242 {
243 assert_true(buf_write_u16(&buf, 0));
244 }
245
246 struct probe_reply out = { 0 };
247 assert_false(probe_reply_read(&buf, &out));
248
249 gc_free(&gc);
250}
251
252/* The flag combinations the protocol rules out must be rejected: the other
253 * addresses of a remote cannot be both the same server and distinct ones, and
254 * a shared connect_lifetime needs them to have been called equal. */
255void
257{
258 struct gc_arena gc = gc_new();
259 const uint16_t bad[] = {
263 };
264 const uint16_t good[] = {
265 0,
269 };
270
271 for (size_t i = 0; i < SIZE(bad) + SIZE(good); i++)
272 {
273 const bool expect_ok = i >= SIZE(bad);
274 const uint16_t flags = expect_ok ? good[i - SIZE(bad)] : bad[i];
275
276 struct buffer buf = alloc_buf_gc(128, &gc);
277 const struct probe_reply in = { .request_id = 1, .flags = flags };
278 assert_true(probe_reply_write(&buf, &in));
279
280 struct buffer value;
281 assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REPLY, &value));
282
283 struct probe_reply out = { 0 };
284 assert_int_equal(probe_reply_read(&value, &out), expect_ok);
285 }
286
287 gc_free(&gc);
288}
289
290/* A TLV header claiming more value bytes than the payload holds must be
291 * rejected by the scan rather than reported as found -- for the TLV being
292 * looked for as much as for one that would merely be skipped. */
293void
295{
296 struct gc_arena gc = gc_new();
297 struct buffer value;
298
299 /* the wanted TLV declares 20 value bytes, only 8 are present */
300 struct buffer buf = alloc_buf_gc(128, &gc);
302 assert_true(buf_write_u64(&buf, 0xdeadbeefULL));
303 assert_false(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
304
305 /* same defect on a TLV that would be skipped: the scan must not walk past
306 * the end of the payload looking for the next header */
307 struct buffer buf2 = alloc_buf_gc(128, &gc);
308 assert_true(ctrl_msg_tlv_write_header(&buf2, 0x7ff, false, 64));
309 assert_true(buf_write_u32(&buf2, 0));
310 assert_false(ctrl_msg_find_tlv(&buf2, TLV_TYPE_PROBE_REQUEST, &value));
311
312 gc_free(&gc);
313}
314
315/* Bytes left over after the last TLV that are too few for a header make the
316 * message malformed, even when the wanted TLV was already found. */
317void
319{
320 struct gc_arena gc = gc_new();
321 struct buffer value;
322
323 /* 1 to 3 bytes: less than a 4-byte TLV header */
324 for (int trailing = 1; trailing < CTRL_MSG_TLV_HEADER_LEN; trailing++)
325 {
326 struct buffer buf = alloc_buf_gc(128, &gc);
327 const struct probe_request in = { .request_id = 1, .timestamp = 1 };
328 assert_true(probe_request_write(&buf, &in));
329 for (int i = 0; i < trailing; i++)
330 {
331 assert_true(buf_write_u8(&buf, 0));
332 }
333 assert_false(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
334 }
335
336 gc_free(&gc);
337}
338
339/* An unknown TLV marked optional after the wanted one is skipped, and the
340 * wanted value is still the one returned. */
341void
343{
344 struct gc_arena gc = gc_new();
345 struct buffer buf = alloc_buf_gc(128, &gc);
346 struct buffer value;
347
348 const struct probe_request in = { .request_id = 1, .timestamp = 7 };
349 assert_true(probe_request_write(&buf, &in));
350 assert_true(ctrl_msg_tlv_write_header(&buf, 0x7ff, true, 4));
351 assert_true(buf_write_u32(&buf, 0));
352
353 assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
354 assert_int_equal(BLEN(&value), PROBE_REQUEST_LEN);
355 struct probe_request out = { 0 };
356 assert_true(probe_request_read(&value, &out));
357 assert_true(out.timestamp == 7);
358
359 gc_free(&gc);
360}
361
362/* A TLV we do not know that is not marked optional carries something its
363 * sender requires us to act on, so the scan rejects the message whether that
364 * TLV comes before or after the wanted one. */
365void
367{
368 struct gc_arena gc = gc_new();
369 struct buffer value;
370 const struct probe_request in = { .request_id = 1, .timestamp = 7 };
371
372 struct buffer before = alloc_buf_gc(128, &gc);
373 assert_true(ctrl_msg_tlv_write_header(&before, 0x7ff, false, 4));
374 assert_true(buf_write_u32(&before, 0xcafef00d));
375 assert_true(probe_request_write(&before, &in));
376
377 struct buffer after = alloc_buf_gc(128, &gc);
378 assert_true(probe_request_write(&after, &in));
379 assert_true(ctrl_msg_tlv_write_header(&after, 0x7ff, false, 4));
380 assert_true(buf_write_u32(&after, 0xcafef00d));
381
382 assert_false(ctrl_msg_find_tlv(&before, TLV_TYPE_PROBE_REQUEST, &value));
383 assert_false(ctrl_msg_find_tlv(&after, TLV_TYPE_PROBE_REQUEST, &value));
384
385 gc_free(&gc);
386}
387
388/* The wanted TLV may occur only once: a second one rejects the message. Repeats
389 * of an unknown TLV marked optional are skipped like any other. */
390void
392{
393 struct gc_arena gc = gc_new();
394 struct buffer value;
395 const struct probe_request in = { .request_id = 1 };
396
397 struct buffer buf = alloc_buf_gc(128, &gc);
398 assert_true(probe_request_write(&buf, &in));
399 assert_true(probe_request_write(&buf, &in));
400 assert_false(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
401
402 buf = alloc_buf_gc(128, &gc);
403 for (int i = 0; i < 2; i++)
404 {
405 assert_true(ctrl_msg_tlv_write_header(&buf, 0x7ff, true, 4));
406 assert_true(buf_write_u32(&buf, 0));
407 }
408 assert_true(probe_request_write(&buf, &in));
409 assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
410
411 gc_free(&gc);
412}
413
414/* An empty payload holds no TLV. */
415void
417{
418 struct gc_arena gc = gc_new();
419 struct buffer buf = alloc_buf_gc(16, &gc);
420 struct buffer value;
421
422 assert_false(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
423
424 gc_free(&gc);
425}
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
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Append a uint16_t to a buffer in network byte order.
Definition buffer.h:1302
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Append a uint32_t to a buffer in network byte order.
Definition buffer.h:1317
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1288
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
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
static bool buf_write_u64(struct buffer *dest, uint64_t data)
Append a uint64_t to a buffer in network byte order.
Definition buffer.h:1332
bool ctrl_msg_tlv_peek_header(const struct buffer *buf, struct ctrl_msg_tlv_header *hdr)
Read a TLV header from buf without consuming it.
Definition control_msg.c:60
bool ctrl_msg_tlv_write_header(struct buffer *buf, uint16_t type, bool optional, uint16_t value_len)
Write a TLV header (type + optional flag + value length) to buf.
Definition control_msg.c:32
bool ctrl_msg_find_tlv(struct buffer *payload, uint16_t wanted_type, struct buffer *value)
Scan payload for the TLV of type wanted_type, skipping any other TLV type that is marked optional.
Definition control_msg.c:96
TLV framing shared by the TLV-based control messages of the wire protocol.
#define CTRL_MSG_TLV_HEADER_LEN
Definition control_msg.h:48
#define TLV_TYPE_PROBE_REQUEST
Definition control_msg.h:44
#define TLV_TYPE_PROBE_REPLY
Definition control_msg.h:45
#define SIZE(x)
Definition basic.h:29
bool probe_reply_write(struct buffer *buf, const struct probe_reply *r)
Write a complete probe reply TLV, header and value, to buf.
Definition probe.c:58
bool probe_request_read(struct buffer *buf, struct probe_request *r)
Read a probe request TLV value from buf.
Definition probe.c:41
bool probe_request_write(struct buffer *buf, const struct probe_request *r)
Write a complete probe request TLV, header and value, to buf.
Definition probe.c:33
bool probe_reply_read(struct buffer *buf, struct probe_reply *r)
Read a probe reply TLV value from buf.
Definition probe.c:67
Encoding and decoding of the server probe TLVs.
#define PROBE_REPLY_FLAG_LIFETIME_SHARED
the connect_lifetime holds against those other addresses too, so a reply from one of them can skip th...
Definition probe.h:56
#define PROBE_REPLY_FLAG_ADDRS_EQUAL
the other addresses of this remote are the same server, no need to probe them
Definition probe.h:52
#define PROBE_REQUEST_FLAG_PROBE_ONLY
probe request flags.
Definition probe.h:46
#define PROBE_REPLY_LEN
request_id (u64), then priority, weight, max_latency_diff, connect_lifetime and flags (u16 each)
Definition probe.h:98
#define PROBE_REQUEST_LEN
request_id (u64), timestamp (u64), flags (u32)
Definition probe.h:94
#define PROBE_REPLY_FLAG_PROBE_MORE
the other addresses of this remote are distinct servers, so probe them too
Definition probe.h:58
#define PROBE_REPLY_FLAG_RESEND_WKC
complete the handshake with P_CONTROL_WKC_V1 rather than P_CONTROL_V1
Definition probe.h:50
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
The header every TLV carries: the 15-bit type and optional flag packed into the first 16-bit field,...
Definition control_msg.h:57
bool optional
value of the optional flag
Definition control_msg.h:59
uint16_t value_len
length of the value following the header
Definition control_msg.h:60
uint16_t type
the 15-bit TLV type
Definition control_msg.h:58
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
probe reply TLV, a server's answer to a probe request
Definition probe.h:74
uint16_t priority
DNS SRV style priority, lower is preferred.
Definition probe.h:76
uint16_t connect_lifetime
For how many seconds, counted from the moment the client sent its own probe request,...
Definition probe.h:84
uint16_t weight
DNS SRV style weight.
Definition probe.h:77
uint64_t request_id
echoes the request_id of the request
Definition probe.h:75
uint16_t max_latency_diff
candidate band in ms around the fastest server
Definition probe.h:78
uint16_t flags
PROBE_REPLY_FLAG_*.
Definition probe.h:85
probe request TLV, sent by a client to probe a server
Definition probe.h:62
uint32_t flags
PROBE_REQUEST_FLAG_*.
Definition probe.h:69
uint64_t timestamp
The client clock as a UNIX timestamp in seconds.
Definition probe.h:68
uint64_t request_id
identifies this probe, echoed in the reply
Definition probe.h:63
void test_find_tlv_rejects_unknown_mandatory(void **state)
Definition test_probe.c:366
void test_probe_request_wire_format(void **state)
Definition test_probe.c:79
void test_probe_reply_roundtrip(void **state)
Definition test_probe.c:108
void test_find_tlv_empty_payload(void **state)
Definition test_probe.c:416
void test_probe_reply_wire_format(void **state)
Definition test_probe.c:151
void test_find_tlv_rejects_duplicate(void **state)
Definition test_probe.c:391
void test_find_tlv_value_truncated(void **state)
Definition test_probe.c:294
void test_find_tlv_trailing_bytes(void **state)
Definition test_probe.c:318
void test_find_tlv_skips_optional_after_wanted(void **state)
Definition test_probe.c:342
void test_probe_reply_flags_contradict(void **state)
Definition test_probe.c:256
void test_probe_request_too_short(void **state)
Definition test_probe.c:219
void test_probe_request_forward_compat(void **state)
Definition test_probe.c:189
void test_probe_request_roundtrip(void **state)
Definition test_probe.c:42
void test_probe_reply_too_short(void **state)
Definition test_probe.c:235
struct gc_arena gc
Definition test_ssl.c:138