OpenVPN
mtu.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 "common.h"
30#include "buffer.h"
31#include "error.h"
32#include "integer.h"
33#include "mtu.h"
34#include "options.h"
35#include "crypto.h"
36
37#include "memdbg.h"
38#include "ssl_common.h"
39
40#if defined(__GNUC__) || defined(__clang__)
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Wsign-conversion"
43#endif
44
45/* allocate a buffer for socket or tun layer */
46void
47alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame)
48{
49 /* allocate buffer for overlapped I/O */
50 *buf = alloc_buf(BUF_SIZE(frame));
52 buf->len = frame->buf.payload_size;
53 ASSERT(buf_safe(buf, 0));
54}
55
56unsigned int
57calc_packet_id_size_dc(const struct options *options, const struct key_type *kt)
58{
60
61 /* epoch format uses a 64-bit packet id: 16 bit epoch + 48 bit per-epoch counter */
62 if (epoch)
63 {
64 return sizeof(uint64_t);
65 }
66
67 bool packet_id_long_form = cipher_kt_mode_ofb_cfb(kt->cipher);
68
69 return packet_id_size(packet_id_long_form);
70}
71
72size_t
74 bool occ)
75{
76 /* Sum of all the overhead that reduces the usable packet size */
77 size_t header_size = 0;
78
79 /* A socks proxy adds extra header to each packet
80 * (we only support Socks with IPv4, this value is different for IPv6) */
82 {
83 header_size += SOCKS_UDPv4_HEADROOM;
84 }
85
86 /* TCP stream based packets have a 16 bit length field */
88 {
89 header_size += 2;
90 }
91
92 /* Add the opcode and peerid */
93 header_size += options->use_peer_id ? 4 : 1;
94
95 unsigned int pkt_id_size = calc_packet_id_size_dc(options, kt);
96
97 /* For figuring out the crypto overhead, we need the size of the payload
98 * including all headers that also get encrypted as part of the payload */
99 header_size += calculate_crypto_overhead(kt, pkt_id_size, occ);
100 return header_size;
101}
102
103
104size_t
105frame_calculate_payload_overhead(size_t extra_tun, const struct options *options,
106 const struct key_type *kt)
107{
108 size_t overhead = 0;
109
110 /* This is the overhead of tap device that is not included in the MTU itself
111 * i.e. Ethernet header that we still need to transmit as part of the
112 * payload, this is set to 0 by caller if not applicable */
113 overhead += extra_tun;
114
115#if defined(USE_COMP)
116 /* v1 Compression schemes add 1 byte header. V2 only adds a header when it
117 * does not increase the packet length. We ignore the unlikely escaping
118 * for tap here */
121 {
122 overhead += 1;
123 }
124#endif
125#if defined(ENABLE_FRAGMENT)
126 /* Add the size of the fragment header (uint32_t) */
127 if (options->ce.fragment)
128 {
129 overhead += 4;
130 }
131#endif
132
133 if (cipher_kt_mode_cbc(kt->cipher))
134 {
135 /* The packet id is part of the plain text payload instead of the
136 * cleartext protocol header and needs to be included in the payload
137 * overhead instead of the protocol header */
138 overhead += calc_packet_id_size_dc(options, kt);
139 }
140
141 return overhead;
142}
143
144size_t
146 const struct key_type *kt)
147{
148 size_t payload_size = options->ce.tun_mtu;
150 return payload_size;
151}
152
153size_t
154calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
155{
156 struct key_type occ_kt;
157
158 /* o->ciphername might be BF-CBC even though the underlying SSL library
159 * does not support it. For this reason we workaround this corner case
160 * by pretending to have no encryption enabled and by manually adding
161 * the required packet overhead to the MTU computation.
162 */
163 const char *ciphername = o->ciphername;
164
165 size_t overhead = 0;
166
167 if (strcmp(o->ciphername, "BF-CBC") == 0)
168 {
169 /* none has no overhead, so use this to later add only --auth
170 * overhead */
171
172 /* overhead of BF-CBC: 64 bit block size, 64 bit IV size */
173 overhead += 64 / 8 + 64 / 8;
174 /* set ciphername to none, so its size does get added in the
175 * fake_kt and the cipher is not tried to be resolved */
176 ciphername = "none";
177 }
178
179 init_key_type(&occ_kt, ciphername, o->authname, false);
180
181 size_t payload = frame_calculate_payload_size(frame, o, &occ_kt);
182 overhead += frame_calculate_protocol_header_size(&occ_kt, o, true);
183
184 return payload + overhead;
185}
186
187void
188frame_print(const struct frame *frame, msglvl_t msglevel, const char *prefix)
189{
190 struct gc_arena gc = gc_new();
191 struct buffer out = alloc_buf_gc(256, &gc);
192 if (prefix)
193 {
194 buf_printf(&out, "%s ", prefix);
195 }
196 buf_printf(&out, "[");
197 buf_printf(&out, " mss_fix:%" PRIu16, frame->mss_fix);
198#ifdef ENABLE_FRAGMENT
199 buf_printf(&out, " max_frag:%d", frame->max_fragment_size);
200#endif
201 buf_printf(&out, " tun_mtu:%d", frame->tun_mtu);
202 buf_printf(&out, " tun_max_mtu:%d", frame->tun_max_mtu);
203 buf_printf(&out, " headroom:%d", frame->buf.headroom);
204 buf_printf(&out, " payload:%d", frame->buf.payload_size);
205 buf_printf(&out, " tailroom:%d", frame->buf.tailroom);
206 buf_printf(&out, " ET:%d", frame->extra_tun);
207 buf_printf(&out, " ]");
208
209 msg(msglevel, "%s", out.data);
210 gc_free(&gc);
211}
212
213#define MTUDISC_NOT_SUPPORTED_MSG "--mtu-disc is not supported on this OS"
214
215void
217{
218 if (mtu_type >= 0)
219 {
220 switch (proto_af)
221 {
222#if defined(IP_MTU_DISCOVER)
223 case AF_INET:
224 if (setsockopt(sd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&mtu_type,
225 sizeof(mtu_type)))
226 {
227 msg(M_ERR, "Error setting IP_MTU_DISCOVER type=%d on TCP/UDP socket", mtu_type);
228 }
229 break;
230
231#endif
232#if defined(IPV6_MTU_DISCOVER)
233 case AF_INET6:
234 if (setsockopt(sd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&mtu_type,
235 sizeof(mtu_type)))
236 {
237 msg(M_ERR, "Error setting IPV6_MTU_DISCOVER type=%d on TCP6/UDP6 socket",
238 mtu_type);
239 }
240 break;
241
242#endif
243 default:
245 break;
246 }
247 }
248}
249
250int
252{
253#if defined(IP_PMTUDISC_DONT) && defined(IP_PMTUDISC_WANT) && defined(IP_PMTUDISC_DO)
254 if (!strcmp(name, "yes"))
255 {
256 return IP_PMTUDISC_DO;
257 }
258 if (!strcmp(name, "maybe"))
259 {
260 return IP_PMTUDISC_WANT;
261 }
262 if (!strcmp(name, "no"))
263 {
264 return IP_PMTUDISC_DONT;
265 }
266 msg(M_FATAL, "invalid --mtu-disc type: '%s' -- valid types are 'yes', 'maybe', or 'no'", name);
267#else
269#endif
270 return -1; /* NOTREACHED */
271}
272
273#if EXTENDED_SOCKET_ERROR_CAPABILITY
274
275#include <linux/errqueue.h>
276
277struct probehdr
278{
279 uint32_t ttl;
280 struct timeval tv;
281};
282
283const char *
284format_extended_socket_error(int fd, int *mtu, struct gc_arena *gc)
285{
286 struct probehdr rcvbuf;
287 struct iovec iov;
288 struct msghdr msg;
289 struct cmsghdr *cmsg;
290 struct sock_extended_err *e;
291 struct sockaddr_storage addr;
292 struct buffer out = alloc_buf_gc(256, gc);
293 char *cbuf = (char *)gc_malloc(256, false, gc);
294
295 *mtu = 0;
296
297 while (true)
298 {
299 memset(&rcvbuf, -1, sizeof(rcvbuf));
300 iov.iov_base = &rcvbuf;
301 iov.iov_len = sizeof(rcvbuf);
302 msg.msg_name = (uint8_t *)&addr;
303 msg.msg_namelen = sizeof(addr);
304 msg.msg_iov = &iov;
305 msg.msg_iovlen = 1;
306 msg.msg_flags = 0;
307 msg.msg_control = cbuf;
308 msg.msg_controllen = 256; /* size of cbuf */
309
310 ssize_t res = recvmsg(fd, &msg, MSG_ERRQUEUE);
311 if (res < 0)
312 {
313 goto exit;
314 }
315
316 e = NULL;
317
318 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
319 {
320 if (cmsg->cmsg_level == SOL_IP)
321 {
322 if (cmsg->cmsg_type == IP_RECVERR)
323 {
324 e = (struct sock_extended_err *)CMSG_DATA(cmsg);
325 }
326 else
327 {
328 buf_printf(&out, "CMSG=%d|", cmsg->cmsg_type);
329 }
330 }
331 else if (cmsg->cmsg_level == IPPROTO_IPV6)
332 {
333 if (cmsg->cmsg_type == IPV6_RECVERR)
334 {
335 e = (struct sock_extended_err *)CMSG_DATA(cmsg);
336 }
337 else
338 {
339 buf_printf(&out, "CMSG=%d|", cmsg->cmsg_type);
340 }
341 }
342 }
343 if (e == NULL)
344 {
345 buf_printf(&out, "NO-INFO|");
346 goto exit;
347 }
348
349 switch (e->ee_errno)
350 {
351 case ETIMEDOUT:
352 buf_printf(&out, "ETIMEDOUT|");
353 break;
354
355 case EMSGSIZE:
356 buf_printf(&out, "EMSGSIZE Path-MTU=%d|", e->ee_info);
357 *mtu = e->ee_info;
358 break;
359
360 case ECONNREFUSED:
361 buf_printf(&out, "ECONNREFUSED|");
362 break;
363
364 case EPROTO:
365 buf_printf(&out, "EPROTO|");
366 break;
367
368 case EHOSTUNREACH:
369 buf_printf(&out, "EHOSTUNREACH|");
370 break;
371
372 case ENETUNREACH:
373 buf_printf(&out, "ENETUNREACH|");
374 break;
375
376 case EACCES:
377 buf_printf(&out, "EACCES|");
378 break;
379
380 default:
381 buf_printf(&out, "UNKNOWN|");
382 break;
383 }
384 }
385
386exit:
387 buf_rmtail(&out, '|');
388 return BSTR(&out);
389}
390
391#if defined(__GNUC__) || defined(__clang__)
392#pragma GCC diagnostic pop
393#endif
394
395void
396set_sock_extended_error_passing(int sd, sa_family_t proto_af)
397{
398 int on = 1;
399 /* see "man 7 ip" (on Linux)
400 * this works on IPv4 and IPv6(-dual-stack) sockets (v4-mapped)
401 */
402 if (setsockopt(sd, SOL_IP, IP_RECVERR, (void *)&on, sizeof(on)) != 0)
403 {
405 "Note: enable extended error passing on TCP/UDP socket failed (IP_RECVERR)");
406 }
407 /* see "man 7 ipv6" (on Linux)
408 * this only works on IPv6 sockets
409 */
410 if (proto_af == AF_INET6
411 && setsockopt(sd, IPPROTO_IPV6, IPV6_RECVERR, (void *)&on, sizeof(on)) != 0)
412 {
414 "Note: enable extended error passing on TCP/UDP socket failed (IPV6_RECVERR)");
415 }
416}
417
418#endif /* if EXTENDED_SOCKET_ERROR_CAPABILITY */
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:321
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
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
void buf_rmtail(struct buffer *buf, uint8_t remove)
Remove all occurrences of a specific byte from the end of a buffer.
Definition buffer.c:494
Buffer management functions and garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
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 void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define buf_init(buf, offset)
Definition buffer.h:364
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
#define COMP_ALG_LZ4
LZ4 algorithm.
Definition comp.h:59
#define COMP_ALG_STUB
support compression command byte and framing without actual compression
Definition comp.h:56
#define COMP_ALG_LZO
LZO algorithm.
Definition comp.h:57
void init_key_type(struct key_type *kt, const char *ciphername, const char *authname, bool warn)
Initialize a key_type structure with.
Definition crypto.c:872
size_t calculate_crypto_overhead(const struct key_type *kt, unsigned int pkt_id_size, bool occ)
Calculate the maximum overhead that our encryption has on a packet.
Definition crypto.c:800
Data Channel Cryptography Module.
#define CO_EPOCH_DATA_KEY_FORMAT
Bit-flag indicating the epoch the data format.
Definition crypto.h:367
bool cipher_kt_mode_cbc(const char *ciphername)
Check if the supplied cipher is a supported CBC mode cipher.
bool cipher_kt_mode_ofb_cfb(const char *ciphername)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
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
#define MTUDISC_NOT_SUPPORTED_MSG
Definition mtu.c:213
int translate_mtu_discover_type_name(const char *name)
Definition mtu.c:251
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
unsigned int calc_packet_id_size_dc(const struct options *options, const struct key_type *kt)
Return the size of the packet ID size that is currently in use by cipher and options for the data cha...
Definition mtu.c:57
void alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame)
Definition mtu.c:47
void frame_print(const struct frame *frame, msglvl_t msglevel, const char *prefix)
Definition mtu.c:188
size_t calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
Calculate the link-mtu to advertise to our peer.
Definition mtu.c:154
void set_mtu_discover_type(socket_descriptor_t sd, int mtu_type, sa_family_t proto_af)
Definition mtu.c:216
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 BUF_SIZE(f)
Definition mtu.h:188
#define SOCKS_UDPv4_HEADROOM
Definition mtu.h:106
#define M_FATAL
Definition error.h:90
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
static unsigned int packet_id_size(bool long_form)
Definition packet_id.h:253
static bool proto_is_udp(int proto)
Returns if the protocol being used is UDP.
static bool proto_is_tcp(int proto)
returns if the proto is a TCP variant (tcp-server, tcp-client or tcp)
Control Channel Common Data Structures.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:78
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
const char * socks_proxy_server
Definition options.h:125
int fragment
Definition options.h:143
int proto
Definition options.h:111
int tun_mtu
Definition options.h:129
Packet geometry parameters.
Definition mtu.h:113
int tun_mtu
the (user) configured tun-mtu.
Definition mtu.h:147
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 tun_max_mtu
the maximum tun-mtu size the buffers are are sized for.
Definition mtu.h:157
int extra_tun
Maximum number of bytes in excess of the tun/tap MTU that might be read from or written to the virtua...
Definition mtu.h:161
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
uint16_t mss_fix
The actual MSS value that should be written to the payload packets.
Definition mtu.h:134
int max_fragment_size
The maximum size of a fragment.
Definition mtu.h:140
int tailroom
the tailroom in the buffer.
Definition mtu.h:128
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
const char * cipher
const name of the cipher
Definition crypto.h:135
struct compress_options comp
Definition options.h:410
unsigned int imported_protocol_flags
Definition options.h:735
bool use_peer_id
Whether the data channel uses the DATA_V2 header (peer-id).
Definition options.h:703
const char * authname
Definition options.h:579
struct connection_entry ce
Definition options.h:294
const char * ciphername
Definition options.h:573
unsigned short sa_family_t
Definition syshead.h:405
#define SOL_IP
Definition syshead.h:398
SOCKET socket_descriptor_t
Definition syshead.h:441
struct gc_arena gc
Definition test_ssl.c:138