OpenVPN
occ.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 "occ.h"
30#include "forward.h"
31#include "memdbg.h"
32#include "options_string.h"
33
34/*
35 * This random string identifies an OpenVPN
36 * Configuration Control packet.
37 * It should be of sufficient length and randomness
38 * so as not to collide with other tunnel data.
39 *
40 * The OCC protocol is as follows:
41 *
42 * occ_magic -- (16 octets)
43 *
44 * type [OCC_REQUEST | OCC_REPLY] (1 octet)
45 * null terminated options string if OCC_REPLY (variable)
46 *
47 * When encryption is used, the OCC packet
48 * is encapsulated within the encrypted
49 * envelope.
50 *
51 * OCC_STRING_SIZE must be set to sizeof (occ_magic)
52 */
53
54const uint8_t occ_magic[] = { 0x28, 0x7f, 0x34, 0x6b, 0xd4, 0xef, 0x7a, 0x81,
55 0x2d, 0x56, 0xb8, 0xd3, 0xaf, 0xc5, 0x45, 0x9c };
56
57static const struct mtu_load_test mtu_load_test_sequence[] = {
58
59 { OCC_MTU_LOAD_REQUEST, -1000 },
60 { OCC_MTU_LOAD, -1000 },
61 { OCC_MTU_LOAD_REQUEST, -1000 },
62 { OCC_MTU_LOAD, -1000 },
63 { OCC_MTU_LOAD_REQUEST, -1000 },
64 { OCC_MTU_LOAD, -1000 },
65
66 { OCC_MTU_LOAD_REQUEST, -750 },
67 { OCC_MTU_LOAD, -750 },
68 { OCC_MTU_LOAD_REQUEST, -750 },
69 { OCC_MTU_LOAD, -750 },
70 { OCC_MTU_LOAD_REQUEST, -750 },
71 { OCC_MTU_LOAD, -750 },
72
73 { OCC_MTU_LOAD_REQUEST, -500 },
74 { OCC_MTU_LOAD, -500 },
75 { OCC_MTU_LOAD_REQUEST, -500 },
76 { OCC_MTU_LOAD, -500 },
77 { OCC_MTU_LOAD_REQUEST, -500 },
78 { OCC_MTU_LOAD, -500 },
79
80 { OCC_MTU_LOAD_REQUEST, -400 },
81 { OCC_MTU_LOAD, -400 },
82 { OCC_MTU_LOAD_REQUEST, -400 },
83 { OCC_MTU_LOAD, -400 },
84 { OCC_MTU_LOAD_REQUEST, -400 },
85 { OCC_MTU_LOAD, -400 },
86
87 { OCC_MTU_LOAD_REQUEST, -300 },
88 { OCC_MTU_LOAD, -300 },
89 { OCC_MTU_LOAD_REQUEST, -300 },
90 { OCC_MTU_LOAD, -300 },
91 { OCC_MTU_LOAD_REQUEST, -300 },
92 { OCC_MTU_LOAD, -300 },
93
94 { OCC_MTU_LOAD_REQUEST, -200 },
95 { OCC_MTU_LOAD, -200 },
96 { OCC_MTU_LOAD_REQUEST, -200 },
97 { OCC_MTU_LOAD, -200 },
98 { OCC_MTU_LOAD_REQUEST, -200 },
99 { OCC_MTU_LOAD, -200 },
100
101 { OCC_MTU_LOAD_REQUEST, -150 },
102 { OCC_MTU_LOAD, -150 },
103 { OCC_MTU_LOAD_REQUEST, -150 },
104 { OCC_MTU_LOAD, -150 },
105 { OCC_MTU_LOAD_REQUEST, -150 },
106 { OCC_MTU_LOAD, -150 },
107
108 { OCC_MTU_LOAD_REQUEST, -100 },
109 { OCC_MTU_LOAD, -100 },
110 { OCC_MTU_LOAD_REQUEST, -100 },
111 { OCC_MTU_LOAD, -100 },
112 { OCC_MTU_LOAD_REQUEST, -100 },
113 { OCC_MTU_LOAD, -100 },
114
115 { OCC_MTU_LOAD_REQUEST, -50 },
116 { OCC_MTU_LOAD, -50 },
117 { OCC_MTU_LOAD_REQUEST, -50 },
118 { OCC_MTU_LOAD, -50 },
119 { OCC_MTU_LOAD_REQUEST, -50 },
120 { OCC_MTU_LOAD, -50 },
121
123 { OCC_MTU_LOAD, 0 },
125 { OCC_MTU_LOAD, 0 },
127 { OCC_MTU_LOAD, 0 },
128
129 { OCC_MTU_REQUEST, 0 },
130 { OCC_MTU_REQUEST, 0 },
131 { OCC_MTU_REQUEST, 0 },
132 { OCC_MTU_REQUEST, 0 },
133 { OCC_MTU_REQUEST, 0 },
134 { OCC_MTU_REQUEST, 0 },
135 { OCC_MTU_REQUEST, 0 },
136 { OCC_MTU_REQUEST, 0 },
137 { OCC_MTU_REQUEST, 0 },
138 { OCC_MTU_REQUEST, 0 },
139
140 { -1, 0 }
141};
142
143void
145{
146 if (++c->c2.occ_n_tries >= OCC_N_TRIES)
147 {
148 if (c->options.ce.remote)
149 {
150 /*
151 * No OCC_REPLY from peer after repeated attempts.
152 * Give up.
153 */
155 "NOTE: failed to obtain options consistency info from peer -- "
156 "this could occur if the remote peer is running a version of " PACKAGE_NAME
157 " before 1.5-beta8 or if there is a network connectivity problem, and will not necessarily prevent " PACKAGE_NAME
158 " from running (" counter_format " bytes received from peer, " counter_format
159 " bytes authenticated data channel traffic) -- you can disable the options consistency "
160 "check with --disable-occ.",
162 }
164 }
165 else
166 {
167 c->c2.occ_op = OCC_REQUEST;
168
169 /*
170 * If we don't hear back from peer, send another
171 * OCC_REQUEST in OCC_INTERVAL_SECONDS.
172 */
174 }
175}
176
177#if defined(__GNUC__) || defined(__clang__)
178#pragma GCC diagnostic push
179#pragma GCC diagnostic ignored "-Wconversion"
180#pragma GCC diagnostic ignored "-Wsign-conversion"
181#endif
182
183void
185{
187 {
188 const struct mtu_load_test *entry;
189
190 if (!c->c2.occ_mtu_load_n_tries)
191 {
192 msg(M_INFO,
193 "NOTE: Beginning empirical MTU test -- results should be available in 3 to 4 minutes.");
194 }
195
197 if (entry->op >= 0)
198 {
199 c->c2.occ_op = entry->op;
200 size_t payload_size =
202 size_t header_size =
204
205 c->c2.occ_mtu_load_size = payload_size + header_size;
206 }
207 else
208 {
209 msg(M_INFO, "NOTE: failed to empirically measure MTU (requires " PACKAGE_NAME
210 " 1.5 or higher at other end of connection).");
213 }
214 }
215}
216
217void
219{
220 bool doit = false;
221
222 c->c2.buf = c->c2.buffers->aux_buf;
226
227 switch (c->c2.occ_op)
228 {
229 case OCC_REQUEST:
230 if (!buf_write_u8(&c->c2.buf, OCC_REQUEST))
231 {
232 break;
233 }
234 dmsg(D_PACKET_CONTENT, "SENT OCC_REQUEST");
235 doit = true;
236 break;
237
238 case OCC_REPLY:
239 if (!c->c2.options_string_local)
240 {
241 break;
242 }
243 if (!buf_write_u8(&c->c2.buf, OCC_REPLY))
244 {
245 break;
246 }
248 strlen(c->c2.options_string_local) + 1))
249 {
250 break;
251 }
252 dmsg(D_PACKET_CONTENT, "SENT OCC_REPLY");
253 doit = true;
254 break;
255
256 case OCC_MTU_REQUEST:
258 {
259 break;
260 }
261 dmsg(D_PACKET_CONTENT, "SENT OCC_MTU_REQUEST");
262 doit = true;
263 break;
264
265 case OCC_MTU_REPLY:
266 if (!buf_write_u8(&c->c2.buf, OCC_MTU_REPLY))
267 {
268 break;
269 }
271 {
272 break;
273 }
275 {
276 break;
277 }
278 dmsg(D_PACKET_CONTENT, "SENT OCC_MTU_REPLY");
279 doit = true;
280 break;
281
284 {
285 break;
286 }
288 {
289 break;
290 }
291 dmsg(D_PACKET_CONTENT, "SENT OCC_MTU_LOAD_REQUEST");
292 doit = true;
293 break;
294
295 case OCC_MTU_LOAD:
296 {
297 int need_to_add;
298
299 if (!buf_write_u8(&c->c2.buf, OCC_MTU_LOAD))
300 {
301 break;
302 }
303 size_t proto_hdr, payload_hdr;
304 const struct key_type *kt = &c->c1.ks.key_type;
305
306 /* OCC message have comp/fragment headers but not ethernet headers */
307 payload_hdr = frame_calculate_payload_overhead(0, &c->options, kt);
308
309 /* Since we do not know the payload size we just pass 0 as size here */
310 proto_hdr = frame_calculate_protocol_header_size(kt, &c->options, false);
311
312 need_to_add = min_int(c->c2.occ_mtu_load_size, c->c2.frame.buf.payload_size)
313 - OCC_STRING_SIZE - sizeof(uint8_t) /* occ opcode */
314 - payload_hdr - proto_hdr;
315
316 while (need_to_add > 0)
317 {
318 /*
319 * Fill the load test packet with pseudo-random bytes.
320 */
321 if (!buf_write_u8(&c->c2.buf, get_random() & 0xFF))
322 {
323 break;
324 }
325 --need_to_add;
326 }
327 dmsg(D_PACKET_CONTENT, "SENT OCC_MTU_LOAD min_int(%d,%d)-%d-%d-%d-%d) size=%d",
329 (int)sizeof(uint8_t), (int)payload_hdr, (int)proto_hdr, BLEN(&c->c2.buf));
330 doit = true;
331 }
332 break;
333
334 case OCC_EXIT:
335 if (!buf_write_u8(&c->c2.buf, OCC_EXIT))
336 {
337 break;
338 }
339 dmsg(D_PACKET_CONTENT, "SENT OCC_EXIT");
340 doit = true;
341 break;
342 }
343
344 if (doit)
345 {
346 /*
347 * We will treat the packet like any other outgoing packet,
348 * compress, encrypt, sign, etc.
349 */
350 encrypt_sign(c, true);
351 }
352
353 c->c2.occ_op = -1;
354}
355
356void
358{
360 switch (buf_read_u8(&c->c2.buf))
361 {
362 case OCC_REQUEST:
363 dmsg(D_PACKET_CONTENT, "RECEIVED OCC_REQUEST");
364 c->c2.occ_op = OCC_REPLY;
365 break;
366
367 case OCC_MTU_REQUEST:
368 dmsg(D_PACKET_CONTENT, "RECEIVED OCC_MTU_REQUEST");
370 break;
371
373 dmsg(D_PACKET_CONTENT, "RECEIVED OCC_MTU_LOAD_REQUEST");
375 if (c->c2.occ_mtu_load_size >= 0)
376 {
378 }
379 break;
380
381 case OCC_REPLY:
382 dmsg(D_PACKET_CONTENT, "RECEIVED OCC_REPLY");
383 if (c->options.occ && !TLS_MODE(c) && c->c2.options_string_remote)
384 {
386 c->c2.buf.len))
387 {
389 c->c2.buf.len);
390 }
391 }
393 break;
394
395 case OCC_MTU_REPLY:
396 dmsg(D_PACKET_CONTENT, "RECEIVED OCC_MTU_REPLY");
399 if (c->options.mtu_test && c->c2.max_recv_size_remote > 0
400 && c->c2.max_send_size_remote > 0)
401 {
402 msg(M_INFO,
403 "NOTE: Empirical MTU test completed [Tried,Actual] local->remote=[%d,%d] remote->local=[%d,%d]",
410 {
411 msg(M_INFO,
412 "NOTE: This connection is unable to accommodate a UDP packet size of %d. Consider using --fragment or --mssfix options as a workaround.",
414 }
415 }
417 break;
418
419 case OCC_EXIT:
420 dmsg(D_STREAM_ERRORS, "OCC exit message received by peer");
421 register_signal(c->sig, SIGUSR1, "remote-exit");
422 break;
423 }
424 c->c2.buf.len = 0; /* don't pass packet on */
425}
426
427#if defined(__GNUC__) || defined(__clang__)
428#pragma GCC diagnostic pop
429#endif
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 int buf_read_u16(struct buffer *buf)
Read and consume a uint16_t from the front of a buffer.
Definition buffer.h:1516
static bool buf_safe(const struct buffer *buf, size_t len)
Check whether len bytes can be appended to a buffer.
Definition buffer.h:994
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
Definition buffer.h:1165
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1288
static int buf_read_u8(struct buffer *buf)
Read and consume a uint8_t from the front of a buffer.
Definition buffer.h:1495
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define buf_init(buf, offset)
Definition buffer.h:364
#define counter_format
Definition common.h:32
int64_t get_random(void)
an analogue to the random() function, but use prng_bytes and also int64_t instead of long to avoid LL...
Definition crypto.c:1703
#define D_SHOW_OCC
Definition errlevel.h:147
#define D_PACKET_CONTENT
Definition errlevel.h:164
#define D_STREAM_ERRORS
Definition errlevel.h:62
#define M_INFO
Definition errlevel.h:54
Interface functions to the internal and external multiplexers.
static bool connection_established(struct context *c)
Definition forward.h:398
void encrypt_sign(struct context *c, bool comp_frag)
Process a data channel packet that will be sent through a VPN tunnel.
Definition forward.c:654
static int min_int(int x, int y)
Definition integer.h:105
static void event_timeout_reset(struct event_timeout *et)
Resets a timer.
Definition interval.h:187
static void event_timeout_clear(struct event_timeout *et)
Clears the timeout and reset all values to 0.
Definition interval.h:153
size_t frame_calculate_payload_size(const struct frame *frame, const struct options *options, const struct key_type *kt)
Calculates the size of the payload according to tun-mtu and tap overhead.
Definition mtu.c:145
size_t frame_calculate_protocol_header_size(const struct key_type *kt, const struct options *options, bool occ)
Calculates the size of the OpenVPN protocol header.
Definition mtu.c:73
size_t frame_calculate_payload_overhead(size_t extra_tun, const struct options *options, const struct key_type *kt)
Calculates the size of the payload overhead according to tun-mtu and tap overhead.
Definition mtu.c:105
#define TUN_MTU_MIN
Definition mtu.h:59
void process_received_occ_msg(struct context *c)
Definition occ.c:357
void check_send_occ_req_dowork(struct context *c)
Definition occ.c:144
void check_send_occ_msg_dowork(struct context *c)
Definition occ.c:218
const uint8_t occ_magic[]
Definition occ.c:54
void check_send_occ_load_test_dowork(struct context *c)
Definition occ.c:184
static const struct mtu_load_test mtu_load_test_sequence[]
Definition occ.c:57
#define OCC_N_TRIES
Definition occ.h:46
#define OCC_REQUEST
Definition occ.h:35
#define OCC_REPLY
Definition occ.h:36
#define OCC_MTU_REPLY
Definition occ.h:55
#define OCC_STRING_SIZE
Definition occ.h:29
#define OCC_MTU_LOAD_REQUEST
Definition occ.h:51
#define OCC_MTU_REQUEST
Definition occ.h:53
#define OCC_EXIT
Definition occ.h:66
#define OCC_MTU_LOAD
Definition occ.h:52
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define TLS_MODE(c)
Definition openvpn.h:517
bool options_cmp_equal_safe(char *actual, const char *expected, size_t actual_n)
void options_warning_safe(char *actual, const char *expected, size_t actual_n)
void register_signal(struct signal_info *si, int signum, const char *signal_text)
Register a soft signal in the signal_info struct si respecting priority.
Definition sig.c:228
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP).
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
const char * remote
Definition options.h:116
int fragment
Definition options.h:143
int proto
Definition options.h:111
struct key_schedule ks
Definition openvpn.h:150
counter_type link_read_bytes
Definition openvpn.h:249
char * options_string_local
Definition openvpn.h:273
struct event_timeout occ_mtu_load_test_interval
Definition openvpn.h:294
int max_recv_size_local
Definition openvpn.h:285
int occ_mtu_load_size
Definition openvpn.h:292
int max_recv_size_remote
Definition openvpn.h:286
char * options_string_remote
Definition openvpn.h:274
int occ_mtu_load_n_tries
Definition openvpn.h:295
int max_send_size_local
Definition openvpn.h:287
int occ_op
Definition openvpn.h:276
struct frame frame
Definition openvpn.h:231
struct event_timeout occ_interval
Definition openvpn.h:278
counter_type link_read_bytes_auth
Definition openvpn.h:251
struct buffer buf
Definition openvpn.h:349
struct context_buffers * buffers
Definition openvpn.h:341
int max_send_size_remote
Definition openvpn.h:288
int occ_n_tries
Definition openvpn.h:277
struct buffer aux_buf
Definition openvpn.h:83
Contains all state information for one tunnel.
Definition openvpn.h:455
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:476
struct context_2 c2
Level 2 context.
Definition openvpn.h:492
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:456
struct context_1 c1
Level 1 context.
Definition openvpn.h:491
struct frame::@074234134026241341315172337061247271261307273127 buf
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:118
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:124
struct key_type key_type
Definition openvpn.h:58
int op
Definition occ.h:74
struct connection_entry ce
Definition options.h:298
bool mtu_test
Definition options.h:342
bool occ
Definition options.h:447
#define SIGUSR1
Definition syshead.h:57