OpenVPN
mtu.h
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-2025 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#ifndef MTU_H
24#define MTU_H
25
26#include "buffer.h"
27
28/*
29 *
30 * Packet manipulation routes such as encrypt, decrypt, compress, decompress
31 * are passed a frame buffer that looks like this:
32 *
33 * [extra_frame bytes] [mtu bytes] [extra_frame_bytes] [compression overflow bytes]
34 * ^
35 * Pointer passed to function points here so that routine
36 * can make use of extra_frame bytes before pointer
37 * to prepend headers, etc.
38 *
39 * extra_frame bytes is large enough for all encryption related overhead.
40 *
41 * mtu bytes will be the MTU size set in the ifconfig statement that configures
42 * the TUN or TAP device such as:
43 *
44 * ifconfig $1 10.1.0.2 pointopoint 10.1.0.1 mtu 1450
45 *
46 * Compression overflow bytes is the worst-case size expansion that would be
47 * expected if we tried to compress mtu + extra_frame bytes of incompressible data.
48 */
49
50/*
51 * Standard ethernet MTU
52 */
53#define ETHERNET_MTU 1500
54
55/*
56 * It is a fatal error if mtu is less than
57 * this value for tun device.
58 */
59#define TUN_MTU_MIN 100
60
61/*
62 * Default MTU of network over which tunnel data will pass by TCP/UDP.
63 */
64#define LINK_MTU_DEFAULT 1500
65
66/*
67 * Default MTU of tunnel device.
68 */
69#define TUN_MTU_DEFAULT 1500
70
71/*
72 * Minimum maximum MTU
73 */
74#define TUN_MTU_MAX_MIN 1600
75
76/*
77 * MTU Defaults for TAP devices
78 */
79#define TAP_MTU_EXTRA_DEFAULT 32
80
81/*
82 * Default MSSFIX value, used for reducing TCP MTU size
83 */
84#define MSSFIX_DEFAULT 1492
85
86/*
87 * Default maximum size of control channel packets
88 */
89#define TLS_MTU_DEFAULT 1250
90
91/*
92 * Alignment of payload data such as IP packet or
93 * ethernet frame.
94 */
95#define PAYLOAD_ALIGN 4
96
97
98/**************************************************************************/
102struct frame
103{
104 struct
105 {
106 /* This struct holds all the information about the buffers that are
107 * allocated to match this frame */
123
124 uint16_t mss_fix;
160};
161
162/* Forward declarations, to prevent includes */
163struct options;
164
165/*
166 * Control buffer headroom allocations to allow for efficient prepending.
167 */
168
169/*
170 * Max size of a buffer used to build a packet for output to
171 * the TCP/UDP port or to read a packet from a tap/tun device.
172 *
173 * Most of our code only prepends headers but compression needs the extra bytes
174 * *after* the data as compressed data might end up larger than the original
175 * data. Also crypto needs an extra block for encryption. Therefore tailroom is
176 * larger than the headroom.
177 */
178#define BUF_SIZE(f) ((f)->buf.headroom + (f)->buf.payload_size + (f)->buf.tailroom)
179
180/*
181 * Function prototypes.
182 */
183
184void frame_print(const struct frame *frame, int level, const char *prefix);
185
186void set_mtu_discover_type(socket_descriptor_t sd, int mtu_type, sa_family_t proto_af);
187
188int translate_mtu_discover_type_name(const char *name);
189
190/* forward declaration of key_type */
191struct key_type;
192
202size_t frame_calculate_payload_size(const struct frame *frame, const struct options *options,
203 const struct key_type *kt);
204
221size_t frame_calculate_payload_overhead(size_t extra_tun, const struct options *options,
222 const struct key_type *kt);
223
224
240size_t frame_calculate_protocol_header_size(const struct key_type *kt,
241 const struct options *options, bool occ);
242
250size_t calc_options_string_link_mtu(const struct options *options, const struct frame *frame);
251
256unsigned int calc_packet_id_size_dc(const struct options *options, const struct key_type *kt);
257
258/*
259 * allocate a buffer for socket or tun layer
260 */
261void alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame);
262
263/*
264 * EXTENDED_SOCKET_ERROR_CAPABILITY functions -- print extra error info
265 * on socket errors, such as PMTU size. As of 2003.05.11, only works
266 * on Linux 2.4+.
267 */
268
269#if EXTENDED_SOCKET_ERROR_CAPABILITY
270
271void set_sock_extended_error_passing(int sd, sa_family_t proto_af);
272
273const char *format_extended_socket_error(int fd, int *mtu, struct gc_arena *gc);
274
275#endif
276
277#endif /* ifndef MTU_H */
void frame_print(const struct frame *frame, int level, const char *prefix)
Definition mtu.c:190
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:138
int translate_mtu_discover_type_name(const char *name)
Definition mtu.c:253
size_t calc_options_string_link_mtu(const struct options *options, const struct frame *frame)
Calculate the link-mtu to advertise to our peer.
Definition mtu.c:147
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:61
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:51
void alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame)
Definition mtu.c:41
void set_mtu_discover_type(socket_descriptor_t sd, int mtu_type, sa_family_t proto_af)
Definition mtu.c:218
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:98
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
Packet geometry parameters.
Definition mtu.h:103
int tun_mtu
the (user) configured tun-mtu.
Definition mtu.h:137
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:108
int tun_max_mtu
the maximum tun-mtu size the buffers are are sized for.
Definition mtu.h:147
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:151
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:114
uint16_t mss_fix
The actual MSS value that should be written to the payload packets.
Definition mtu.h:124
int max_fragment_size
The maximum size of a fragment.
Definition mtu.h:130
struct frame::@8 buf
int tailroom
the tailroom in the buffer.
Definition mtu.h:118
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
unsigned short sa_family_t
Definition syshead.h:396
SOCKET socket_descriptor_t
Definition syshead.h:440
struct gc_arena gc
Definition test_ssl.c:154