Ensure that control channel packet are respecting maximum packet size
PATCH v4 1/3 Ensure that control channel packet are respecting maximum packet size
This ensures that control packets are actually smaller than the maximum control channel packet size.
Since OpenVPN will consider a control message packet complete when the TLS record is complete, we have to ensure that the SSL library will still write one record, so the receiving side will only be able to get/read the control message content when a TLS record is complete. To achieve this goal, this commit does:
- Split one read from TLS library into multiple control channel packets, splitting one TLS record into multiple control packets.
- increase allowed number of outstanding packets to 6 from 4 on the sender side. This is still okay with older implementations as receivers will have room for 8.
- calculate the overhead for control channel message to allow staying below that threshold.
- remove maxlen from key_state_read_ciphertext and related functions as we now always allow control channel messages to be up to TLS_CHANNEL_BUF_SIZE in size and no longer limit this by the mtu of control packets as the implemented splitting will take care of larger payloads from the SSL library
Note this commit has a warning message that references max-packet-size that is introduced in the next commit. In this commit the packet size is not yet configurable.
Patch v2: avoid assertion about to large buffer by sticking to 1250 max
control size in this commit and leaving larger sizes for the --max-packet-size commit. Also fix
various other small problems and grammar fixes.
Patch v3: grammar fixes
Patch v4: adjust tls-mtu to max-packet-size in message.
--- src/openvpn/reliable.c | 55 ++++++++++---- src/openvpn/reliable.h | 32 +++++++- src/openvpn/ssl.c | 155 ++++++++++++++++++++++++++++++++------ src/openvpn/ssl_backend.h | 8 +- src/openvpn/ssl_mbedtls.c | 14 +--- src/openvpn/ssl_openssl.c | 16 ++-- src/openvpn/ssl_pkt.h | 4 +- 7 files changed, 215 insertions(+), 69 deletions(-)
src/openvpn/reliable.c +39 -16
@@ -41,6 +41,14 @@#include "memdbg.h" +/* calculates test - base while allowing for base or test wraparound. test is +* assumed to be higher than base */ +static inline packet_id_type +subtract_pid (const packet_id_type test,const packet_id_type base) +{ +return test - base; +} +/* * verify that test - base < extent while allowing for base or test wraparound */
@@ -49,22 +57,7 @@ reliable_pid_in_range1(const packet_id_type test, const packet_id_type base, const unsigned int extent) { - if (test >= base) - { - if (test - base < extent) - { -return true; - } - } - else - { -if ((test+0x80000000u ) - (base+0x80000000u ) < extent) - { -return true; - } - } - -return false; + return subtract_pid(test, base) < extent; }/*
@@ -498,6 +491,36 @@ reliable_get_buf(struct reliable *rel)return NULL ; } +int +reliable_get_num_output_sequenced_available (struct reliable *rel) +{ +struct gc_arena gc =gc_new (); +packet_id_type min_id =0 ; +bool min_id_defined = false; + +/* find minimum active packet_id */ +for (int i =0 ; i < rel->size; ++i) + { +const struct reliable_entry *e = &rel->array[i]; +if (e->active) + { +if (!min_id_defined ||reliable_pid_min (e->packet_id, min_id)) + { + min_id_defined = true; + min_id = e->packet_id; + } + } + } + +int ret = rel->size; +if (min_id_defined) + { + ret -=subtract_pid (rel->packet_id, min_id); + } +gc_free (&gc); +return ret; +} +/* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel)
src/openvpn/reliable.h +30 -2
@@ -46,7 +46,7 @@* be stored in one \c reliable_ack * structure. */ -#define RELIABLE_CAPACITY 8/**< The maximum number of packets that +#define RELIABLE_CAPACITY 12/**< The maximum number of packets that * the reliability layer for one VPN * tunnel in one direction can store. */
@@ -93,7 +93,7 @@ struct reliableint size;interval_t initial_timeout;packet_id_type packet_id; -int offset; +int offset;/**< Offset of the bufs in the reliable_entry array */ bool hold;/* don't xmit until reliable_schedule_now is called */ struct reliable_entry array[RELIABLE_CAPACITY ]; };
@@ -178,6 +178,20 @@ reliable_ack_empty(struct reliable_ack *ack)return !ack->len; } +/** +* Returns the number of packets that need to be acked. +* +* @param ack The acknowledgment structure to check. +* +* @returns the number of outstanding acks +*/ +static inline int +reliable_ack_outstanding (struct reliable_ack *ack) +{ +return ack->len; +} + +/** * Write a packet ID acknowledgment record to a buffer. *
@@ -385,6 +399,20 @@ void reliable_mark_deleted(struct reliable *rel, struct buffer *buf);*/ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel); + +/** +* Counts the number of free buffers in output that can be potentially used +* for sending +* +* @param rel The reliable structure in which to search for a free +* entry. +* +* @return the number of buffer that are available for sending without +* breaking ack sequence +* */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel); +/** * Mark the reliable entry associated with the given buffer as * active outgoing.
src/openvpn/ssl.c +134 -21
194 lines not on this page · raw message
src/openvpn/ssl_backend.h +3 -5
@@ -461,7 +461,6 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* @param ks_ssl - The security parameter state for this %key * session. * @param buf - A buffer in which to store the ciphertext. -* @param maxlen - The maximum number of bytes to extract. * * @return The return value indicates whether the data was successfully * processed:
@@ -470,8 +469,8 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf); +/** @} name Functions for packets to be sent to a remote OpenVPN peer */
@@ -517,8 +516,7 @@ int key_state_write_ciphertext(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf);/** @} name Functions for packets received from a remote OpenVPN peer */
src/openvpn/ssl_mbedtls.c +2 -12
@@ -1285,8 +1285,7 @@ key_state_write_plaintext_const(struct key_state_ssl *ks, const uint8_t *data, i }int -key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1304,10 +1303,6 @@ key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =endless_buf_read (&ks->bio_ctx->out,BPTR (buf), len);
@@ -1388,8 +1383,7 @@ key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1407,10 +1401,6 @@ key_state_read_plaintext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =mbedtls_ssl_read (ks->ctx,BPTR (buf), len);
src/openvpn/ssl_openssl.c +5 -11
@@ -1871,7 +1871,7 @@ bio_write_post(const int status, struct buffer *buf)* Read from an OpenSSL BIO in non-blocking mode. */ static int -bio_read (BIO *bio,struct buffer *buf,int maxlen,const char *desc) +bio_read (BIO *bio,struct buffer *buf,const char *desc) {int i;int ret =0 ;
@@ -1882,10 +1882,6 @@ bio_read(BIO *bio, struct buffer *buf, int maxlen, const char *desc)else {int len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - }/* * BIO_read brackets most of the serious RSA
@@ -2012,15 +2008,14 @@ key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *dat }int -key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_CIPHERTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ct_out, buf, maxlen,"tls_read_ciphertext" ); + ret =bio_read (ks_ssl->ct_out, buf,"tls_read_ciphertext" );perf_pop ();return ret;
@@ -2042,15 +2037,14 @@ key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_PLAINTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ssl_bio, buf, maxlen,"tls_read_plaintext" ); + ret =bio_read (ks_ssl->ssl_bio, buf,"tls_read_plaintext" );perf_pop ();return ret;
src/openvpn/ssl_pkt.h +2 -2
@@ -67,8 +67,8 @@/* * Define number of buffers for send and receive in the reliability layer. */ -#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ -#define TLS_RELIABLE_N_REC_BUFFERS 8 +#define TLS_RELIABLE_N_SEND_BUFFERS 6/* also window size for reliability layer */ +#define TLS_RELIABLE_N_REC_BUFFERS 12/* * Used in --mode server mode to check tls-auth signature on initial
Signature · 1 line
2.37.0 (Apple Git-136)
PATCH v4 2/3 Allow setting control channel packet size with max-packet-size
Currently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Instead we decouple this and introduce max-packet-size. Control packet size defaults to 1250 if max-packet-size is not set.
Patch v2: rebase on latest patch set
Patch v3: Introduce TLS_CHANNEL_MTU_MIN define and give explaination
of its value.
Patch v4: introduce max-packet-size instead of tls-mtu
--- Changes.rst | 9 +++++++++ doc/man-sections/link-options.rst | 16 ++++++++++++++++ src/openvpn/common.h | 10 ++++++++++ src/openvpn/init.c | 8 ++++++-- src/openvpn/mtu.h | 5 +++++ src/openvpn/options.c | 21 +++++++++++++++++++++ src/openvpn/options.h | 1 + src/openvpn/ssl.c | 26 ++++++++++++++------------ src/openvpn/ssl.h | 8 +++----- 9 files changed, 85 insertions(+), 19 deletions(-)
Changes.rst +9 -0
@@ -88,6 +88,12 @@ Data channel offloading with ovpn-dco which brings DATA_V2 packet support. +Improved control channel packet size control (``max-packet-size``) + The size of control channel is no longer tied to + ``--link-mtu``/``--tun-mtu`` and can be set using ``--max-packet-size``. + Setting the size to small sizes no longer breaks the OpenVPN protocol in + certain situations. + Deprecated features ------------------- ``inetd`` has been removed
@@ -150,6 +156,9 @@ User-visible Changes - Option ``--nobind`` is default when ``--client`` or ``--pull`` is used in the configuration - :code:`link_mtu` parameter is removed from environment or replaced with 0 when scripts are called with parameters. This parameter is unreliable and no longer internally calculated. +- control channel packet maximum size is no longer influenced by ``--link-mtu``/``--tun-mtu`` + and must be set by ``--max-packet-size`` now. + Overview of changes in 2.5 ==========================
doc/man-sections/link-options.rst +16 -0
@@ -454,3 +454,19 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--max-packet-size size + This option will instruct OpenVPN to try to limit the maximum on-write packet + size by restricting the control channel packet size and setting ``--mssfix``. + + OpenVPN will try to keep its control channel messages below this size but + due to some constraints in the protocol this is not always possible. If the + option is not set, the control packet maximum size defaults to 1250. + The control channel packet size will be restricted to values between + 512 and 2048. The maximum packet size includes encapsulation overhead like + UDP and IP. + + For ``--mssfix`` it will expand to: + :: + + mssfix size mtu
src/openvpn/common.h +10 -0
@@ -68,6 +68,16 @@ typedef unsigned long ptr_type;*/ #define TLS_CHANNEL_BUF_SIZE 2048 +/* TLS control buffer minimum size, this size is not actually inherent to +* the protocol but. Our current sending window is 6 and the receive window +* is 8 or 12 depending on the OpenVPN version. We need to be able to send +* a TLS record of size TLS_CHANNEL_BUF_SIZE. Splitting this into more than +* 6 packets (with overhead) would complicate our sending logic a lot more, so +* we settle here for a "round" number that allow with overhead of ~100 bytes +* to be larger than TLS_CHANNEL_BUF_SIZE. E.g. 6x ~400 > 2048. +* */ +#define TLS_CHANNEL_MTU_MIN 512 +/* * This parameter controls the maximum size of a bundle * of pushed options.
src/openvpn/init.c +6 -2
@@ -2627,6 +2627,10 @@ frame_finalize_options(struct context *c, const struct options *o)* space */ size_t payload_size =max_int (1500 , frame->tun_mtu); +/* we need to be also large enough to hold larger control channel packets +* if configured */ + payload_size =max_int (payload_size, o->ce.tls_mtu); +/* The extra tun needs to be added to the payload size */ if (o->ce.tun_mtu_defined) {
@@ -3172,7 +3176,7 @@ do_init_frame_tls(struct context *c) {if (c->c2.tls_multi) { -tls_multi_init_finalize (c->c2.tls_multi, &c->c2.frame); +tls_multi_init_finalize (c->c2.tls_multi, c->options.ce.tls_mtu);ASSERT (c->c2.tls_multi->opt.frame.buf.payload_size <= c->c2.frame.buf.payload_size); frame_print(&c->c2.tls_multi->opt.frame, D_MTU_INFO,
@@ -3180,7 +3184,7 @@ do_init_frame_tls(struct context *c) }if (c->c2.tls_auth_standalone) { -tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame); +tls_init_control_channel_frame_parameters (&c->c2.tls_auth_standalone->frame, c->options.ce.tls_mtu);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc);
src/openvpn/mtu.h +5 -0
@@ -79,6 +79,11 @@*/ #define MSSFIX_DEFAULT 1492 +/* +* Default maximum size of control channel packets +*/ +#define TLS_MTU_DEFAULT 1250 +/* * Alignment of payload data such as IP packet or * ethernet frame.
src/openvpn/options.c +21 -0
@@ -825,6 +825,7 @@ init_options(struct options *o, const bool init_gc) o->ce.bind_local = true; o->ce.tun_mtu =TUN_MTU_DEFAULT ; o->ce.link_mtu =LINK_MTU_DEFAULT ; + o->ce.tls_mtu =TLS_MTU_DEFAULT ; o->ce.mtu_discover_type =-1 ; o->ce.mssfix =0 ; o->ce.mssfix_default = true;
@@ -1711,6 +1712,7 @@ show_connection_entry(const struct connection_entry *o)SHOW_BOOL (link_mtu_defined);SHOW_INT (tun_mtu_extra);SHOW_BOOL (tun_mtu_extra_defined); +SHOW_INT (tls_mtu);SHOW_INT (mtu_discover_type);
@@ -6442,6 +6444,25 @@ add_option(struct options *options, options->ce.tun_mtu_extra =positive_atoi (p[1 ]); options->ce.tun_mtu_extra_defined = true; } +else if (streq (p[0 ],"max-packet-size" ) && p[1 ] && !p[2 ]) + { +VERIFY_PERMISSION (OPT_P_MTU |OPT_P_CONNECTION ); +int maxmtu =positive_atoi (p[1 ]); + options->ce.tls_mtu =constrain_int (maxmtu,TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE ); + +if (maxmtu <TLS_CHANNEL_MTU_MIN || maxmtu >TLS_CHANNEL_BUF_SIZE ) + { +msg (M_WARN ,"max-packet-size value outside of allowed control " +"channel packet size (%d to %d), will use %d " +"instead" ,TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE , + options->ce.tls_mtu); + } + +/* also set mssfix maxmtu mtu */ + options->ce.mssfix = maxmtu; + options->ce.mssfix_default = false; + options->ce.mssfix_encap = true; + }#ifdef ENABLE_FRAGMENT else if (streq(p[0], "mtu-dynamic")) {
src/openvpn/options.h +1 -0
@@ -123,6 +123,7 @@ struct connection_entrybool tun_mtu_extra_defined;int link_mtu;/* MTU of device over which tunnel packets pass via TCP/UDP */ bool link_mtu_defined;/* true if user overriding parm with command line option */ +int tls_mtu;/* Maximum MTU for the control channel messages */ /* Advanced MTU negotiation and datagram fragmentation options */ int mtu_discover_type;/* used if OS supports setting Path MTU discovery options on socket */
src/openvpn/ssl.c +14 -12
@@ -297,8 +297,7 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes) }void -tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame) +tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu) {/* * frame->extra_frame is already initialized with tls_auth buffer requirements,
@@ -323,18 +322,21 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame/* Previous OpenVPN version calculated the maximum size and buffer of a * control frame depending on the overhead of the data channel frame -* overhead and limited its maximum size to 1250. We always allocate the -* TLS_CHANNEL_BUF_SIZE buffer size since a lot of code blindly assumes -* a large buffer (e.g. PUSH_BUNDLE_SIZE) and also our peer might have -* a higher size configured and we still want to be able to receive the -* packets. frame->mtu_mtu is set as suggestion for the maximum packet -* size */ - frame->buf.payload_size =1250 + overhead; +* overhead and limited its maximum size to 1250. Since control frames +* also need to fit into data channel buffer we have the same +* default of 1500 + 100 as data channel buffers have. Increasing +* control channel mtu beyond this limit also increases the data channel +* buffers */ + frame->buf.payload_size =max_int (1500 , tls_mtu) +100 ; frame->buf.headroom = overhead; frame->buf.tailroom = overhead; - frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); + frame->tun_mtu = tls_mtu; + +/* Ensure the tun-mtu stays in a valid range */ + frame->tun_mtu =min_int (frame->tun_mtu,TLS_CHANNEL_BUF_SIZE ); + frame->tun_mtu =max_int (frame->tun_mtu,TLS_CHANNEL_MTU_MIN ); }/**
@@ -1300,9 +1302,9 @@ tls_multi_init(struct tls_options *tls_options) }void -tls_multi_init_finalize (struct tls_multi *multi,const struct frame *frame) +tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu) { -tls_init_control_channel_frame_parameters (frame, &multi->opt.frame); +tls_init_control_channel_frame_parameters (&multi->opt.frame, tls_mtu);/* initialize the active and untrusted sessions */ tls_session_init (multi, &multi->session[TM_ACTIVE ]);
src/openvpn/ssl.h +3 -5
@@ -166,10 +166,9 @@ struct tls_multi *tls_multi_init(struct tls_options *tls_options);* * @param multi - The \c tls_multi structure of which to finalize * initialization. -* @param frame - The data channel's \c frame structure. +* @param tls_mtu - maximum allowed size for control channel packets */ -void tls_multi_init_finalize (struct tls_multi *multi, -const struct frame *frame); +void tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu);/* * Initialize a standalone tls-auth verification object.
@@ -181,8 +180,7 @@ struct tls_auth_standalone *tls_auth_standalone_init(struct tls_options *tls_opt* Setups the control channel frame size parameters from the data channel * parameters */ -void tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame); +void tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu);/* * Set local and remote option compatibility strings.
Signature · 1 line
2.37.0 (Apple Git-136)
PATCH v4 3/3 Add unit test for reliable_get_num_output_sequenced_available
In reply to Arne SchwabePatch v4: rebase
--- tests/unit_tests/openvpn/Makefile.am | 5 +- tests/unit_tests/openvpn/mock_get_random.c | 10 ++++ tests/unit_tests/openvpn/test_packet_id.c | 55 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-)
tests/unit_tests/openvpn/Makefile.am +4 -1
@@ -61,7 +61,10 @@ packet_id_testdriver_SOURCES = test_packet_id.c mock_msg.c mock_msg.h \ $(openvpn_srcdir)/buffer.c \ $(openvpn_srcdir)/otime.c \ $(openvpn_srcdir)/packet_id.c \ - $(openvpn_srcdir)/platform.c + $(openvpn_srcdir)/platform.c \ + $(openvpn_srcdir)/reliable.c \ + $(openvpn_srcdir)/session_id.c + pkt_testdriver_CFLAGS = @TEST_CFLAGS@ \ -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir)
tests/unit_tests/openvpn/mock_get_random.c +10 -0
@@ -26,6 +26,7 @@#include <stdio.h> #include <stdlib.h> #include <setjmp.h> +#include <stdint.h> #include <cmocka.h> unsigned long
@@ -34,3 +35,12 @@ get_random(void)/* rand() is not very random, but it's C99 and this is just for testing */ return rand (); } + +void +prng_bytes (uint8_t *output,int len) +{ +for (int i =0 ; i < len; i++) + { + output[i] =rand (); + } +}
tests/unit_tests/openvpn/test_packet_id.c +55 -0
73 lines not on this page · raw message
Signature · 1 line
2.37.0 (Apple Git-136)
Quoted 27 lines · Arne Schwabe
This ensures that control packets are actually smaller than the maximum control channel packet size.
Since OpenVPN will consider a control message packet complete when the TLS record is complete, we have to ensure that the SSL library will still write one record, so the receiving side will only be able to get/read the control message content when a TLS record is complete. To achieve this goal, this commit does:
- Split one read from TLS library into multiple control channel packets, splitting one TLS record into multiple control packets.
- increase allowed number of outstanding packets to 6 from 4 on the sender side. This is still okay with older implementations as receivers will have room for 8.
- calculate the overhead for control channel message to allow staying below that threshold.
- remove maxlen fromkey_state_read_ciphertextand related functions as we now always allow control channel messages to be up toTLS_CHANNEL_BUF_SIZEin size and no longer limit this by the mtu of control packets as the implemented splitting will take care of larger payloads from the SSL libraryNote this commit has a warning message that references max-packet-size that is introduced in the next commit. In this commit the packet size is not yet configurable.
Quoted 6 lines
Patch v2: avoid assertion about to large buffer by sticking to 1250 max
control size in this commit and leaving larger sizes for the --max-packet-size commit. Also fix
various other small problems and grammar fixes.
Patch v3: grammar fixes
Patch v4: adjust tls-mtu to max-packet-size in message.
Reread the code. Did some client-side tests with small MTUs. Didn't find any issues. One remaining typo found, see below.
I think it would actually have been possible to split this up in even smaller patches (e.g. removing the maxlen parameter), but probably not worth doing that now on a v4.
Quoted 4 lines
src/openvpn/ssl.c +0
-0
[...]
Quoted 66 lines
src/openvpn/ssl.c +62
-0@@ -2675,6 +2718,92 @@ read_incoming_tls_plaintext(struct key_state *ks, struct buffer *buf,return true; } +static bool +write_outgoing_tls_ciphertext (struct tls_session *session,bool *state_change) +{ +struct key_state *ks = &session->key[KS_PRIMARY ]; + +int rel_avail =reliable_get_num_output_sequenced_available (ks->send_reliable); +if (rel_avail ==0 ) + { +return true; + } + +/* We need to determine how much space is actually available in the control +* channel frame */ + +int max_pkt_len =min_int (TLS_CHANNEL_BUF_SIZE , session->opt->frame.tun_mtu); + + +/* Subtract overhead */ + max_pkt_len -=calc_control_channel_frame_overhead (session); + +/* calculate total available length for outgoing tls ciphertext */ +int maxlen = max_pkt_len * rel_avail; + +/* Is first packet one that will have a WKC appended? */ +if (control_packet_needs_wkc (ks)) + { + maxlen -=buf_len (session->tls_wrap.tls_crypt_v2_wkc); + } + +/* Not enough space available to send a full control channel packet */ +if (maxlen <TLS_CHANNEL_BUF_SIZE ) + { +if (rel_avail ==TLS_RELIABLE_N_SEND_BUFFERS ) + { +msg (D_TLS_ERRORS ,"--max-packet-size setting too low. Unable to " +"send control channel message." ); + } +msg (D_REL_LOW ,"Reliable: Send queue full, postponing TLS send" ); +return true; + } + +/* This seems a bit wasteful to allocate every time */ +struct gc_arena gc =gc_new (); +struct buffer tmp =alloc_buf_gc (TLS_CHANNEL_BUF_SIZE , &gc); + +int status =key_state_read_ciphertext (&ks->ks_ssl, &tmp); + +if (status ==-1 ) + { +msg (D_TLS_ERRORS , +"TLS Error: Ciphertext -> reliable TCP/UDP transport read error" ); +gc_free (&gc); +return false; + } +if (status ==1 ) + { +/* Split the TLS ciphertext (TLS record) into multiple small packets +* that respect tls_mtu */ +while (tmp.len) + { +int len = max_pkt_len; +int opcode =P_CONTROL_V1 ;
src/openvpn/ssl.c read_incoming_tls_plaintext(struct key_state *ks, struct buffer *buf, · line 2792
Quoted 10 lines
src/openvpn/ssl.c +10
-0+if (control_packet_needs_wkc (ks)) + { + opcode =P_CONTROL_WKC_V1 ; + len =max_int (0 , len -buf_len (session->tls_wrap.tls_crypt_v2_wkc)); + } +/* do not send more than available */ + len =min_int (len, tmp.len); + +struct buffer *buf =reliable_get_buf_output_sequenced (ks->send_reliable); +/* we assert here since we checked for its availibility before */
"availability"
Quoted 5 lines
src/openvpn/ssl.c +5
-0+ASSERT (buf); +buf_copy_n (buf, &tmp, len); + +reliable_mark_active_outgoing (ks->send_reliable, buf, opcode); +INCR_GENERATED ;
src/openvpn/ssl.c read_incoming_tls_plaintext(struct key_state *ks, struct buffer *buf, · line 2807
Quoted 10 lines
src/openvpn/ssl.c +9
-0+ *state_change = true; + } +dmsg (D_TLS_DEBUG ,"Outgoing Ciphertext -> Reliable" ); + } + +gc_free (&gc); +return true; +} +
Regards,
Signature · 1 line
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v4 3/3] Add unit test for reliable_get_num_output_sequenced_available
In reply to Arne SchwabeAlready acked in [email protected]...
Quoted 139 lines · Arne Schwabe
Patch v4: rebase
Signed-off-by Arne Schwabe <[email protected]...>--- tests/unit_tests/openvpn/Makefile.am | 5 +- tests/unit_tests/openvpn/mock_get_random.c | 10 ++++ tests/unit_tests/openvpn/test_packet_id.c | 55 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-)tests/unit_tests/openvpn/Makefile.am +4
-1@@ -61,7 +61,10 @@ packet_id_testdriver_SOURCES = test_packet_id.c mock_msg.c mock_msg.h \ $(openvpn_srcdir)/buffer.c \ $(openvpn_srcdir)/otime.c \ $(openvpn_srcdir)/packet_id.c \ - $(openvpn_srcdir)/platform.c + $(openvpn_srcdir)/platform.c \ + $(openvpn_srcdir)/reliable.c \ + $(openvpn_srcdir)/session_id.c + pkt_testdriver_CFLAGS = @TEST_CFLAGS@ \ -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir)tests/unit_tests/openvpn/mock_get_random.c +10
-0@@ -26,6 +26,7 @@#include <stdio.h> #include <stdlib.h> #include <setjmp.h> +#include <stdint.h> #include <cmocka.h> unsigned long @@ -34,3 +35,12 @@ get_random(void)/* rand() is not very random, but it's C99 and this is just for testing */ return rand (); } + +void +prng_bytes (uint8_t *output,int len) +{ +for (int i =0 ; i < len; i++) + { + output[i] =rand (); + } +}tests/unit_tests/openvpn/test_packet_id.c +55
-0@@ -36,6 +36,7 @@#include <cmocka.h> #include "packet_id.h" +#include "reliable.h" #include "mock_msg.h" @@ -156,6 +157,59 @@ test_packet_id_write_long_wrap(void **state)assert_true (data->test_buf_data.buf_time ==htonl (now)); } +static void +test_get_num_output_sequenced_available (void **state) +{ + +struct reliable *rel =malloc (sizeof (struct reliable )); +reliable_init (rel,100 ,50 ,8 , false); + + rel->array[5 ].active = true; + rel->array[5 ].packet_id =100 ; + + rel->packet_id =103 ; + +assert_int_equal (5 ,reliable_get_num_output_sequenced_available (rel)); + + rel->array[6 ].active = true; + rel->array[6 ].packet_id =97 ; +assert_int_equal (2 ,reliable_get_num_output_sequenced_available (rel)); + +/* test ids close to int/unsigned int barrier */ + + rel->array[5 ].active = true; + rel->array[5 ].packet_id = (0x80000000u -3 ); + rel->array[6 ].active = false; + rel->packet_id = (0x80000000u -1 ); + +assert_int_equal (6 ,reliable_get_num_output_sequenced_available (rel)); + + rel->array[5 ].active = true; + rel->array[5 ].packet_id = (0x80000000u -3 ); + rel->packet_id =0x80000001u ; + +assert_int_equal (4 ,reliable_get_num_output_sequenced_available (rel)); + + +/* test wrapping */ + rel->array[5 ].active = true; + rel->array[5 ].packet_id = (0xffffffffu -3 ); + rel->array[6 ].active = false; + rel->packet_id = (0xffffffffu -1 ); + +assert_int_equal (6 ,reliable_get_num_output_sequenced_available (rel)); + + rel->array[2 ].packet_id =0 ; + rel->array[2 ].active = true; + +assert_int_equal (6 ,reliable_get_num_output_sequenced_available (rel)); + + rel->packet_id =3 ; +assert_int_equal (1 ,reliable_get_num_output_sequenced_available (rel)); + +reliable_free (rel); +} +int main (void ) {@@ -178,6 +232,7 @@ main(void)cmocka_unit_test_setup_teardown (test_packet_id_write_long_wrap, test_packet_id_write_setup, test_packet_id_write_teardown), + cmocka_unit_test(test_get_num_output_sequenced_available) };return cmocka_run_group_tests_name ("packet_id tests" , tests,NULL ,NULL );Signature · 8 lines
2.37.0 (Apple Git-136) _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Signature · 1 line
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v4 2/3] Allow setting control channel packet size with max-packet-size
In reply to Arne SchwabeQuoted 5 lines · Arne Schwabe
Currently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Quoted 7 lines
Instead we decouple this and introduce max-packet-size. Control packet size defaults to 1250 if max-packet-size is not set.
Patch v2: rebase on latest patch set
Patch v3: IntroduceTLS_CHANNEL_MTU_MINdefine and give explaination
of its value.
Patch v4: introduce max-packet-size instead of tls-mtu
In general I think this change really could use more documentation.
E.g. so we imply --mssfix, this should be documented under --mssfix. Also we should look at all existing examples with --mssfix/--fragment/--tun-mtu and check whether they should be changed to use different options. E.g. how does --max-packet-size interact with the argument-less --mssfix? That should be mentioned.
At the moment this patch adds an option but gives the user no indication when and why to use it compared to all other MTU-related options.
Some other small suggestions below.
Changes.rst Data channel offloading with ovpn-dco · line 95
Quoted 13 lines
Changes.rst +5
-0@@ -88,6 +88,12 @@ Data channel offloading with ovpn-dco which brings DATA_V2 packet support. +Improved control channel packet size control (``max-packet-size``) + The size of control channel is no longer tied to + ``--link-mtu``/``--tun-mtu`` and can be set using ``--max-packet-size``. + Setting the size to small sizes no longer breaks the OpenVPN protocol in + certain situations.
This should mention the implied --mssfix. This way it sounds like it only affects control channel.
Quoted 28 lines
Changes.rst +3
-0Deprecated features ------------------- ``inetd`` has been removed@@ -150,6 +156,9 @@ User-visible Changes - Option ``--nobind`` is default when ``--client`` or ``--pull`` is used in the configuration - :code:`link_mtu` parameter is removed from environment or replaced with 0 when scripts are called with parameters. This parameter is unreliable and no longer internally calculated. +- control channel packet maximum size is no longer influenced by ``--link-mtu``/``--tun-mtu`` + and must be set by ``--max-packet-size`` now. + Overview of changes in 2.5 ==========================doc/man-sections/link-options.rst +7
-0@@ -454,3 +454,19 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--max-packet-size size + This option will instruct OpenVPN to try to limit the maximum on-write packet + size by restricting the control channel packet size and setting ``--mssfix``. + + OpenVPN will try to keep its control channel messages below this size but + due to some constraints in the protocol this is not always possible. If the
doc/man-sections/link-options.rst the local and the remote host. · line 472
Quoted 10 lines
doc/man-sections/link-options.rst +9
-0+ option is not set, the control packet maximum size defaults to 1250. + The control channel packet size will be restricted to values between + 512 and 2048. The maximum packet size includes encapsulation overhead like + UDP and IP. + + For ``--mssfix`` it will expand to: + :: + + mssfix size mtu
Missing newline
Quoted 5 lines
src/openvpn/common.h +0
-0
src/openvpn/common.h typedef unsigned long ptr_type; · line 77
Quoted 10 lines
src/openvpn/common.h +7
-0@@ -68,6 +68,16 @@ typedef unsigned long ptr_type;*/ #define TLS_CHANNEL_BUF_SIZE 2048 +/* TLS control buffer minimum size, this size is not actually inherent to +* the protocol but. Our current sending window is 6 and the receive window +* is 8 or 12 depending on the OpenVPN version. We need to be able to send +* a TLS record of size TLS_CHANNEL_BUF_SIZE. Splitting this into more than +* 6 packets (with overhead) would complicate our sending logic a lot more, so +* we settle here for a "round" number that allow with overhead of ~100 bytes +* to be larger than TLS_CHANNEL_BUF_SIZE. E.g. 6x ~400 > 2048.
That sentence is difficult to parse, specifically "so
we settle here for a "round" number that allow with overhead of ~100 bytes to be larger than TLS_CHANNEL_BUF_SIZE". I'm not completely sure what you want to say so I find it difficult to make recommendations.
Quoted 6 lines
src/openvpn/common.h +3
-0+* */ +#define TLS_CHANNEL_MTU_MIN 512 +/* * This parameter controls the maximum size of a bundle * of pushed options.
[...]
Quoted 26 lines
src/openvpn/options.c +4
-0@@ -825,6 +825,7 @@ init_options(struct options *o, const bool init_gc) o->ce.bind_local = true; o->ce.tun_mtu =TUN_MTU_DEFAULT ; o->ce.link_mtu =LINK_MTU_DEFAULT ; + o->ce.tls_mtu =TLS_MTU_DEFAULT ; o->ce.mtu_discover_type =-1 ; o->ce.mssfix =0 ; o->ce.mssfix_default = true;@@ -1711,6 +1712,7 @@ show_connection_entry(const struct connection_entry *o)SHOW_BOOL (link_mtu_defined);SHOW_INT (tun_mtu_extra);SHOW_BOOL (tun_mtu_extra_defined); +SHOW_INT (tls_mtu);SHOW_INT (mtu_discover_type);@@ -6442,6 +6444,25 @@ add_option(struct options *options, options->ce.tun_mtu_extra =positive_atoi (p[1 ]); options->ce.tun_mtu_extra_defined = true; } +else if (streq (p[0 ],"max-packet-size" ) && p[1 ] && !p[2 ]) + {
src/openvpn/options.c add_option(struct options *options, · line 6458
Quoted 10 lines
src/openvpn/options.c +10
-0+VERIFY_PERMISSION (OPT_P_MTU |OPT_P_CONNECTION ); +int maxmtu =positive_atoi (p[1 ]); + options->ce.tls_mtu =constrain_int (maxmtu,TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE ); + +if (maxmtu <TLS_CHANNEL_MTU_MIN || maxmtu >TLS_CHANNEL_BUF_SIZE ) + { +msg (M_WARN ,"max-packet-size value outside of allowed control " +"channel packet size (%d to %d), will use %d " +"instead" ,TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE , + options->ce.tls_mtu);
Would add '.' after instead. Also we might want to add "Note: " prefix to be more consistent with other similar messages?
src/openvpn/options.c add_option(struct options *options, · line 6468
Quoted 10 lines
src/openvpn/options.c +7
-0+ } + +/* also set mssfix maxmtu mtu */ + options->ce.mssfix = maxmtu; + options->ce.mssfix_default = false; + options->ce.mssfix_encap = true; + }#ifdef ENABLE_FRAGMENT else if (streq(p[0], "mtu-dynamic")) {
[...]
regards,
Signature · 1 line
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v4 3/3] Add unit test for reliable_get_num_output_sequenced_available
In reply to Frank LichtenheldQuoted 4 lines · Frank Lichtenheld
Already acked in [email protected]...
Note: this patch does not depend on 2/3, only on 1/3.
Regards,
Signature · 1 line
Frank Lichtenheld
PATCH v5 2/3 Allow setting control channel packet size with max-packet-size
In reply to Arne SchwabeCurrently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Instead we decouple this and introduce max-packet-size. Control packet size defaults to 1250 if max-packet-size is not set.
Patch v2: rebase on latest patch set
Patch v3: Introduce TLS_CHANNEL_MTU_MIN define and give explaination
of its value.
Patch v4: introduce max-packet-size instead of tls-mtu Patch v5: improve documentation
--- Changes.rst | 11 +++++++++- doc/man-sections/link-options.rst | 34 ++++++++++++++++++++++++++++--- src/openvpn/common.h | 13 ++++++++++++ src/openvpn/init.c | 8 ++++++-- src/openvpn/mtu.h | 5 +++++ src/openvpn/options.c | 21 +++++++++++++++++++ src/openvpn/options.h | 1 + src/openvpn/ssl.c | 26 ++++++++++++----------- src/openvpn/ssl.h | 8 +++----- 9 files changed, 104 insertions(+), 23 deletions(-)
Changes.rst +10 -1
@@ -100,6 +100,13 @@ Inline auth username and password http-proxy-user-pass too. +Improved control channel packet size control (``max-packet-size``) + The size of control channel is no longer tied to + ``--link-mtu``/``--tun-mtu`` and can be set using ``--max-packet-size``. + Setting the size to small sizes no longer breaks the OpenVPN protocol in + certain situations. ``max-packet-size`` will also set ``mssfix`` to try + to limit data-channel packets as well. + Deprecated features ------------------- ``inetd`` has been removed
@@ -162,7 +169,9 @@ User-visible Changes - Option ``--nobind`` is default when ``--client`` or ``--pull`` is used in the configuration - :code:`link_mtu` parameter is removed from environment or replaced with 0 when scripts are called with parameters. This parameter is unreliable and no longer internally calculated. - +- control channel packet maximum size is no longer influenced by + ``--link-mtu``/``--tun-mtu`` and must be set by ``--max-packet-size`` now. + The default is 1250 for the control channel size. - In point-to-point OpenVPN setups (no ``--server``), using ``--explict-exit-notiy`` on one end would terminate the other side at session end. This is considered a no longer useful default and has
doc/man-sections/link-options.rst +31 -3
@@ -172,9 +172,9 @@ the local and the remote host. the first place, and if big packets come through anyhow (from protocols other than TCP), ``--fragment`` will internally fragment them. - Both ``--fragment`` and ``--mssfix`` are designed to work around cases - where Path MTU discovery is broken on the network path between OpenVPN - peers. + ``max-packet-size``, ``--fragment`` and ``--mssfix`` are designed to work + around cases where Path MTU discovery is broken on the network path + between OpenVPN peers. The usual symptom of such a breakdown is an OpenVPN connection which successfully starts, but then stalls during active usage.
@@ -189,6 +189,10 @@ the local and the remote host. --tun-mtu 1500 --fragment 1300 --mssfix + If the ``max-packet-size size`` option is used in the configuration + it will also act as if ``mssfix size mtu`` was specified in the + configuration. + --mtu-disc type Should we do Path MTU discovery on TCP/UDP channel? Only supported on OSes such as Linux that supports the necessary system call to set.
@@ -465,3 +469,27 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--max-packet-size size + This option will instruct OpenVPN to try to limit the maximum on-write packet + size by restricting the control channel packet size and setting ``--mssfix``. + + OpenVPN will try to keep its control channel messages below this size but + due to some constraints in the protocol this is not always possible. If the + option is not set, the control packet maximum size defaults to 1250. + The control channel packet size will be restricted to values between + 512 and 2048. The maximum packet size includes encapsulation overhead like + UDP and IP. + + In terms of ``--mssfix`` it will expand to: + :: + + mssfix size mtu + + If you need to set ``--mssfix`` for data channel and control channel maximum + packet size independently, use ``--max-packet-size`` first, followed by a + ``--mssfix`` in the configuration. + + In general the default size of 1250 should work almost universally apart + from specific corner cases, especially since IPv6 requires a MTU of 1280 + or larger.
src/openvpn/common.h +13 -0
@@ -68,6 +68,19 @@ typedef unsigned long ptr_type;*/ #define TLS_CHANNEL_BUF_SIZE 2048 +/* TLS control buffer minimum size. This size is not actually inherent to +* the OpenVPN protocol. But with our current sending window being 6 and the +* receive window being 8 or 12 depending on the OpenVPN version, the biggest +* payload we can send is 6 * min_size. And we need to support to send payloads +* of TLS_CHANNEL_BUF_SIZE. Splitting this into more than +* 6 packets (with overhead) would complicate our sending logic a lot more. +* Diving TLS_CHANNEL_BUF_SIZE (2048) by 6 gets us ~342 byte. Allowing for +* ~100 bytes of overhead (in OpenVPN headers + IP headers) and rounding +* up to the next "nice" number gives use 512. +* +* */ +#define TLS_CHANNEL_MTU_MIN 512 +/* * This parameter controls the maximum size of a bundle * of pushed options.
src/openvpn/init.c +6 -2
@@ -2651,6 +2651,10 @@ frame_finalize_options(struct context *c, const struct options *o)* space */ size_t payload_size =max_int (1500 , frame->tun_mtu); +/* we need to be also large enough to hold larger control channel packets +* if configured */ + payload_size =max_int (payload_size, o->ce.tls_mtu); +/* The extra tun needs to be added to the payload size */ if (o->ce.tun_mtu_defined) {
@@ -3198,7 +3202,7 @@ do_init_frame_tls(struct context *c) {if (c->c2.tls_multi) { -tls_multi_init_finalize (c->c2.tls_multi, &c->c2.frame); +tls_multi_init_finalize (c->c2.tls_multi, c->options.ce.tls_mtu);ASSERT (c->c2.tls_multi->opt.frame.buf.payload_size <= c->c2.frame.buf.payload_size); frame_print(&c->c2.tls_multi->opt.frame, D_MTU_INFO,
@@ -3206,7 +3210,7 @@ do_init_frame_tls(struct context *c) }if (c->c2.tls_auth_standalone) { -tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame); +tls_init_control_channel_frame_parameters (&c->c2.tls_auth_standalone->frame, c->options.ce.tls_mtu);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc);
src/openvpn/mtu.h +5 -0
@@ -79,6 +79,11 @@*/ #define MSSFIX_DEFAULT 1492 +/* +* Default maximum size of control channel packets +*/ +#define TLS_MTU_DEFAULT 1250 +/* * Alignment of payload data such as IP packet or * ethernet frame.
src/openvpn/options.c +21 -0
@@ -826,6 +826,7 @@ init_options(struct options *o, const bool init_gc) o->ce.bind_local = true; o->ce.tun_mtu =TUN_MTU_DEFAULT ; o->ce.link_mtu =LINK_MTU_DEFAULT ; + o->ce.tls_mtu =TLS_MTU_DEFAULT ; o->ce.mtu_discover_type =-1 ; o->ce.mssfix =0 ; o->ce.mssfix_default = true;
@@ -1712,6 +1713,7 @@ show_connection_entry(const struct connection_entry *o)SHOW_BOOL (link_mtu_defined);SHOW_INT (tun_mtu_extra);SHOW_BOOL (tun_mtu_extra_defined); +SHOW_INT (tls_mtu);SHOW_INT (mtu_discover_type);
@@ -6457,6 +6459,25 @@ add_option(struct options *options, options->ce.tun_mtu_extra =positive_atoi (p[1 ]); options->ce.tun_mtu_extra_defined = true; } +else if (streq (p[0 ],"max-packet-size" ) && p[1 ] && !p[2 ]) + { +VERIFY_PERMISSION (OPT_P_MTU |OPT_P_CONNECTION ); +int maxmtu =positive_atoi (p[1 ]); + options->ce.tls_mtu =constrain_int (maxmtu,TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE ); + +if (maxmtu <TLS_CHANNEL_MTU_MIN || maxmtu >TLS_CHANNEL_BUF_SIZE ) + { +msg (M_WARN ,"Note: max-packet-size value outside of allowed " +"control channel packet size (%d to %d), will use %d " +"instead." ,TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE , + options->ce.tls_mtu); + } + +/* also set mssfix maxmtu mtu */ + options->ce.mssfix = maxmtu; + options->ce.mssfix_default = false; + options->ce.mssfix_encap = true; + }#ifdef ENABLE_FRAGMENT else if (streq(p[0], "mtu-dynamic")) {
src/openvpn/options.h +1 -0
@@ -123,6 +123,7 @@ struct connection_entrybool tun_mtu_extra_defined;int link_mtu;/* MTU of device over which tunnel packets pass via TCP/UDP */ bool link_mtu_defined;/* true if user overriding parm with command line option */ +int tls_mtu;/* Maximum MTU for the control channel messages */ /* Advanced MTU negotiation and datagram fragmentation options */ int mtu_discover_type;/* used if OS supports setting Path MTU discovery options on socket */
src/openvpn/ssl.c +14 -12
@@ -297,8 +297,7 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes) }void -tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame) +tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu) {/* * frame->extra_frame is already initialized with tls_auth buffer requirements,
@@ -323,18 +322,21 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame/* Previous OpenVPN version calculated the maximum size and buffer of a * control frame depending on the overhead of the data channel frame -* overhead and limited its maximum size to 1250. We always allocate the -* TLS_CHANNEL_BUF_SIZE buffer size since a lot of code blindly assumes -* a large buffer (e.g. PUSH_BUNDLE_SIZE) and also our peer might have -* a higher size configured and we still want to be able to receive the -* packets. frame->mtu_mtu is set as suggestion for the maximum packet -* size */ - frame->buf.payload_size =1250 + overhead; +* overhead and limited its maximum size to 1250. Since control frames +* also need to fit into data channel buffer we have the same +* default of 1500 + 100 as data channel buffers have. Increasing +* control channel mtu beyond this limit also increases the data channel +* buffers */ + frame->buf.payload_size =max_int (1500 , tls_mtu) +100 ; frame->buf.headroom = overhead; frame->buf.tailroom = overhead; - frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); + frame->tun_mtu = tls_mtu; + +/* Ensure the tun-mtu stays in a valid range */ + frame->tun_mtu =min_int (frame->tun_mtu,TLS_CHANNEL_BUF_SIZE ); + frame->tun_mtu =max_int (frame->tun_mtu,TLS_CHANNEL_MTU_MIN ); }/**
@@ -1314,9 +1316,9 @@ tls_multi_init(struct tls_options *tls_options) }void -tls_multi_init_finalize (struct tls_multi *multi,const struct frame *frame) +tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu) { -tls_init_control_channel_frame_parameters (frame, &multi->opt.frame); +tls_init_control_channel_frame_parameters (&multi->opt.frame, tls_mtu);/* initialize the active and untrusted sessions */ tls_session_init (multi, &multi->session[TM_ACTIVE ]);
src/openvpn/ssl.h +3 -5
@@ -166,10 +166,9 @@ struct tls_multi *tls_multi_init(struct tls_options *tls_options);* * @param multi - The \c tls_multi structure of which to finalize * initialization. -* @param frame - The data channel's \c frame structure. +* @param tls_mtu - maximum allowed size for control channel packets */ -void tls_multi_init_finalize (struct tls_multi *multi, -const struct frame *frame); +void tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu);/* * Initialize a standalone tls-auth verification object.
@@ -181,8 +180,7 @@ struct tls_auth_standalone *tls_auth_standalone_init(struct tls_options *tls_opt* Setups the control channel frame size parameters from the data channel * parameters */ -void tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame); +void tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu);/* * Set local and remote option compatibility strings.
Signature · 1 line
2.37.0 (Apple Git-136)
Re: [Openvpn-devel] [PATCH v5 2/3] Allow setting control channel packet size with max-packet-size
Quoted 5 lines · Arne Schwabe
Currently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Quoted 8 lines
Instead we decouple this and introduce max-packet-size. Control packet size defaults to 1250 if max-packet-size is not set.
Patch v2: rebase on latest patch set
Patch v3: IntroduceTLS_CHANNEL_MTU_MINdefine and give explaination
of its value.
Patch v4: introduce max-packet-size instead of tls-mtu Patch v5: improve documentation
Thanks. Documentation is much improved now, indeed. There is always more that could be improved but let's not do that in this patch.
Two tiny nit-picks noted below which could be applied on merge, or not.
doc/man-sections/link-options.rst the local and the remote host. · line 175
Quoted 12 lines
doc/man-sections/link-options.rst +1
-3@@ -172,9 +172,9 @@ the local and the remote host. the first place, and if big packets come through anyhow (from protocols other than TCP), ``--fragment`` will internally fragment them. - Both ``--fragment`` and ``--mssfix`` are designed to work around cases - where Path MTU discovery is broken on the network path between OpenVPN - peers. + ``max-packet-size``, ``--fragment`` and ``--mssfix`` are designed to work
I'm a big fan of the Oxford comma, so "``--fragment``, ". But I'm not sure what we usually do.
Quoted 51 lines
doc/man-sections/link-options.rst +30
-0+ around cases where Path MTU discovery is broken on the network path + between OpenVPN peers. The usual symptom of such a breakdown is an OpenVPN connection which successfully starts, but then stalls during active usage.@@ -189,6 +189,10 @@ the local and the remote host. --tun-mtu 1500 --fragment 1300 --mssfix + If the ``max-packet-size size`` option is used in the configuration + it will also act as if ``mssfix size mtu`` was specified in the + configuration. + --mtu-disc type Should we do Path MTU discovery on TCP/UDP channel? Only supported on OSes such as Linux that supports the necessary system call to set.@@ -465,3 +469,27 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--max-packet-size size + This option will instruct OpenVPN to try to limit the maximum on-write packet + size by restricting the control channel packet size and setting ``--mssfix``. + + OpenVPN will try to keep its control channel messages below this size but + due to some constraints in the protocol this is not always possible. If the + option is not set, the control packet maximum size defaults to 1250. + The control channel packet size will be restricted to values between + 512 and 2048. The maximum packet size includes encapsulation overhead like + UDP and IP. + + In terms of ``--mssfix`` it will expand to: + :: + + mssfix size mtu + + If you need to set ``--mssfix`` for data channel and control channel maximum + packet size independently, use ``--max-packet-size`` first, followed by a + ``--mssfix`` in the configuration. + + In general the default size of 1250 should work almost universally apart + from specific corner cases, especially since IPv6 requires a MTU of 1280 + or larger.src/openvpn/common.h +0
-0@@ -68,6 +68,19 @@ typedef unsigned long ptr_type;*/ #define TLS_CHANNEL_BUF_SIZE 2048
src/openvpn/common.h typedef unsigned long ptr_type; · line 79
Quoted 10 lines
src/openvpn/common.h +9
-0+/* TLS control buffer minimum size. This size is not actually inherent to +* the OpenVPN protocol. But with our current sending window being 6 and the +* receive window being 8 or 12 depending on the OpenVPN version, the biggest +* payload we can send is 6 * min_size. And we need to support to send payloads +* of TLS_CHANNEL_BUF_SIZE. Splitting this into more than +* 6 packets (with overhead) would complicate our sending logic a lot more. +* Diving TLS_CHANNEL_BUF_SIZE (2048) by 6 gets us ~342 byte. Allowing for +* ~100 bytes of overhead (in OpenVPN headers + IP headers) and rounding +* up to the next "nice" number gives use 512.
"use" -> "us"
src/openvpn/common.h typedef unsigned long ptr_type; · line 86
Quoted 7 lines
src/openvpn/common.h +4
-0+* +* */ +#define TLS_CHANNEL_MTU_MIN 512 +/* * This parameter controls the maximum size of a bundle * of pushed options.
Regards,
Signature · 1 line
Frank Lichtenheld
Re: [Openvpn-devel] [PATCH v5 2/3] Allow setting control channel packet size with max-packet-size
In reply to Arne Schwabe-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Hi,
Plus one more typo.
Sent with Proton Mail secure email.
------- Original Message -------
On Thursday, October 20th, 2022 at 11:05, Arne Schwabe <[email protected]...> wrote:
<snip>
Quoted 8 lines
src/openvpn/common.h +0
-0@@ -68,6 +68,19 @@ typedef unsigned long ptr_type;/
#defineTLS_CHANNEL_BUF_SIZE2048
Quoted 7 lines
+/ TLS control buffer minimum size. This size is not actually inherent to
+ * the OpenVPN protocol. But with our current sending window being 6 and the
+ * receive window being 8 or 12 depending on the OpenVPN version, the biggest
+ * payload we can send is 6 *min_size. And we need to support to send payloads
+ * ofTLS_CHANNEL_BUF_SIZE. Splitting this into more than
+ * 6 packets (with overhead) would complicate our sending logic a lot more.
+ * DivingTLS_CHANNEL_BUF_SIZE(2048) by 6 gets us ~342 byte. Allowing for
Diving -> Dividing
Quoted 4 lines
+ * ~100 bytes of overhead (in OpenVPN headers + IP headers) and rounding
+ * up to the next "nice" number gives use 512.
+ *
+ * /
-----BEGIN PGP SIGNATURE-----
Version: ProtonMail
wsBzBAEBCAAGBQJjUS1fACEJEE+XnPZrkLidFiEECbw9RGejjXJ5xVVVT5ec 9muQuJ2mxAf/afXklJAMoPqLoelxi8mF7hq97b3Eky6iB+zqiGXhOxBjVOKC F6BpNk2uxCUkNSU9FPaLDTMurKuGe6p5+YSdHQh13EzZkx/vehBce4/+OWZ5 nInvafaUbtAI0LqHZvcNhjB2LQcci2MUyw6duok1V43LRdYFZ0ohk4/o+HZ7 6vrij4xNLO1BHhc91CKS0Gm9ZierXPHS8vmAc6ssrhhhq8eFetVq58S7dhyQ ISx8xg20MnqjynmDjpOVgbxKW00+OBTK5NWGLEXd5effQjgdz4qEv5MBoS4V sNdvCBw4tU/GrZdYlBihcQf2h6tgGJ0DxEu4qiF0Tg1h81rrEHSahw== =8tPU
-----END PGP SIGNATURE-----
PATCH v6 1/2 Refactor/optimise code sending TLS control channel messages
In reply to Arne SchwabeThis commit originally tried to solve a problem that the SSL library might split up a control frame into multiple TLS records when doing multiple reads. However, this does not seem to be actually the case.
OpenVPN will consider a control message packet complete
when the TLS record is complete, we have to ensure that the SSL library will still write one record, so the receiving side will only be able to get/read the control message content when a TLS record is complete.
To improve handling of large control channel messages, this commit does:
- Split one read from TLS library into multiple control channel packets, splitting one TLS record into multiple control packets.
- increase allowed number of outstanding packets to 6 from 4 on the sender side. This is still okay with older implementations as receivers will have room for 8. This allows transmitting larger control message more quickly.
- take the wrapped key length into account when sending packets This is especially important for the tls-crypt-v2 P_CONTROL_WKC_V1 message
- calculate the overhead for control channel message to allow staying below that threshold.
- remove maxlen from key_state_read_ciphertext and related functions. We now always give the function a correctly sized buffer.
If we end up needing to send a packet larger than max-packet-size, we warn about it but still do it as it might still work, while refusing to send will never work.
Patch v2: avoid assertion about to large buffer by sticking to 1250 max
control size in this commit and leaving larger sizes for the --max-packet-size commit. Also fix
various other small problems and grammar fixes.
Patch v3: grammar fixes
Patch v4: adjust tls-mtu to max-packet-size in message. Patch v6: no longer make the assumption that multiple reads from the SSL
library split a control frame into multiple TLS records.
--- src/openvpn/reliable.c | 55 ++++++++++---- src/openvpn/reliable.h | 32 +++++++- src/openvpn/ssl.c | 156 +++++++++++++++++++++++++++++++++----- src/openvpn/ssl_backend.h | 8 +- src/openvpn/ssl_mbedtls.c | 14 +--- src/openvpn/ssl_openssl.c | 16 ++-- src/openvpn/ssl_pkt.h | 4 +- 7 files changed, 216 insertions(+), 69 deletions(-)
src/openvpn/reliable.c +39 -16
@@ -41,6 +41,14 @@#include "memdbg.h" +/* calculates test - base while allowing for base or test wraparound. test is +* assumed to be higher than base */ +static inline packet_id_type +subtract_pid (const packet_id_type test,const packet_id_type base) +{ +return test - base; +} +/* * verify that test - base < extent while allowing for base or test wraparound */
@@ -49,22 +57,7 @@ reliable_pid_in_range1(const packet_id_type test, const packet_id_type base, const unsigned int extent) { - if (test >= base) - { - if (test - base < extent) - { -return true; - } - } - else - { -if ((test+0x80000000u ) - (base+0x80000000u ) < extent) - { -return true; - } - } - -return false; + return subtract_pid(test, base) < extent; }/*
@@ -498,6 +491,36 @@ reliable_get_buf(struct reliable *rel)return NULL ; } +int +reliable_get_num_output_sequenced_available (struct reliable *rel) +{ +struct gc_arena gc =gc_new (); +packet_id_type min_id =0 ; +bool min_id_defined = false; + +/* find minimum active packet_id */ +for (int i =0 ; i < rel->size; ++i) + { +const struct reliable_entry *e = &rel->array[i]; +if (e->active) + { +if (!min_id_defined ||reliable_pid_min (e->packet_id, min_id)) + { + min_id_defined = true; + min_id = e->packet_id; + } + } + } + +int ret = rel->size; +if (min_id_defined) + { + ret -=subtract_pid (rel->packet_id, min_id); + } +gc_free (&gc); +return ret; +} +/* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel)
src/openvpn/reliable.h +30 -2
@@ -46,7 +46,7 @@* be stored in one \c reliable_ack * structure. */ -#define RELIABLE_CAPACITY 8/**< The maximum number of packets that +#define RELIABLE_CAPACITY 12/**< The maximum number of packets that * the reliability layer for one VPN * tunnel in one direction can store. */
@@ -93,7 +93,7 @@ struct reliableint size;interval_t initial_timeout;packet_id_type packet_id; -int offset; +int offset;/**< Offset of the bufs in the reliable_entry array */ bool hold;/* don't xmit until reliable_schedule_now is called */ struct reliable_entry array[RELIABLE_CAPACITY ]; };
@@ -178,6 +178,20 @@ reliable_ack_empty(struct reliable_ack *ack)return !ack->len; } +/** +* Returns the number of packets that need to be acked. +* +* @param ack The acknowledgment structure to check. +* +* @returns the number of outstanding acks +*/ +static inline int +reliable_ack_outstanding (struct reliable_ack *ack) +{ +return ack->len; +} + +/** * Write a packet ID acknowledgment record to a buffer. *
@@ -385,6 +399,20 @@ void reliable_mark_deleted(struct reliable *rel, struct buffer *buf);*/ struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel); + +/** +* Counts the number of free buffers in output that can be potentially used +* for sending +* +* @param rel The reliable structure in which to search for a free +* entry. +* +* @return the number of buffer that are available for sending without +* breaking ack sequence +* */ +int +reliable_get_num_output_sequenced_available (struct reliable *rel); +/** * Mark the reliable entry associated with the given buffer as * active outgoing.
src/openvpn/ssl.c +135 -21
195 lines not on this page · raw message
src/openvpn/ssl_backend.h +3 -5
@@ -461,7 +461,6 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* @param ks_ssl - The security parameter state for this %key * session. * @param buf - A buffer in which to store the ciphertext. -* @param maxlen - The maximum number of bytes to extract. * * @return The return value indicates whether the data was successfully * processed:
@@ -470,8 +469,8 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf); +/** @} name Functions for packets to be sent to a remote OpenVPN peer */
@@ -517,8 +516,7 @@ int key_state_write_ciphertext(struct key_state_ssl *ks_ssl,* later to retry. * - \c -1: An error occurred. */ -int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen); +int key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf);/** @} name Functions for packets received from a remote OpenVPN peer */
src/openvpn/ssl_mbedtls.c +2 -12
@@ -1285,8 +1285,7 @@ key_state_write_plaintext_const(struct key_state_ssl *ks, const uint8_t *data, i }int -key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1304,10 +1303,6 @@ key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =endless_buf_read (&ks->bio_ctx->out,BPTR (buf), len);
@@ -1388,8 +1383,7 @@ key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks,struct buffer *buf) {int retval =0 ;int len =0 ;
@@ -1407,10 +1401,6 @@ key_state_read_plaintext(struct key_state_ssl *ks, struct buffer *buf, } len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - } retval =mbedtls_ssl_read (ks->ctx,BPTR (buf), len);
src/openvpn/ssl_openssl.c +5 -11
@@ -1871,7 +1871,7 @@ bio_write_post(const int status, struct buffer *buf)* Read from an OpenSSL BIO in non-blocking mode. */ static int -bio_read (BIO *bio,struct buffer *buf,int maxlen,const char *desc) +bio_read (BIO *bio,struct buffer *buf,const char *desc) {int i;int ret =0 ;
@@ -1882,10 +1882,6 @@ bio_read(BIO *bio, struct buffer *buf, int maxlen, const char *desc)else {int len =buf_forward_capacity (buf); -if (maxlen < len) - { - len = maxlen; - }/* * BIO_read brackets most of the serious RSA
@@ -2012,15 +2008,14 @@ key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *dat }int -key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_ciphertext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_CIPHERTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ct_out, buf, maxlen,"tls_read_ciphertext" ); + ret =bio_read (ks_ssl->ct_out, buf,"tls_read_ciphertext" );perf_pop ();return ret;
@@ -2042,15 +2037,14 @@ key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf) }int -key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf, -int maxlen) +key_state_read_plaintext (struct key_state_ssl *ks_ssl,struct buffer *buf) {int ret =0 ;perf_push (PERF_BIO_READ_PLAINTEXT );ASSERT (NULL != ks_ssl); - ret =bio_read (ks_ssl->ssl_bio, buf, maxlen,"tls_read_plaintext" ); + ret =bio_read (ks_ssl->ssl_bio, buf,"tls_read_plaintext" );perf_pop ();return ret;
src/openvpn/ssl_pkt.h +2 -2
@@ -67,8 +67,8 @@/* * Define number of buffers for send and receive in the reliability layer. */ -#define TLS_RELIABLE_N_SEND_BUFFERS 4/* also window size for reliability layer */ -#define TLS_RELIABLE_N_REC_BUFFERS 8 +#define TLS_RELIABLE_N_SEND_BUFFERS 6/* also window size for reliability layer */ +#define TLS_RELIABLE_N_REC_BUFFERS 12/* * Used in --mode server mode to check tls-auth signature on initial
Signature · 1 line
2.37.0 (Apple Git-136)
PATCH v6 2/2 Allow setting control channel packet size with max-packet-size
Currently control packet size is controlled by tun-mtu in a very non-obvious way since the control overhead is not taken into account and control channel packet will end up with a different size than data channel packet.
Instead we decouple this and introduce max-packet-size. Control packet size defaults to 1250 if max-packet-size is not set.
Patch v2: rebase on latest patch set
Patch v3: Introduce TLS_CHANNEL_MTU_MIN define and give explaination
of its value.
Patch v4: introduce max-packet-size instead of tls-mtu Patch v5: improve documentation
Patch v6: Rebase, lower lower limit, add warning message for
when wrapped tls-crypt-v2 keys will ignore max-packet-size
--- Changes.rst | 11 +++++++++- doc/man-sections/link-options.rst | 34 ++++++++++++++++++++++++++++--- src/openvpn/common.h | 13 ++++++++++++ src/openvpn/init.c | 23 +++++++++++++++++++-- src/openvpn/mtu.h | 5 +++++ src/openvpn/options.c | 21 +++++++++++++++++++ src/openvpn/options.h | 1 + src/openvpn/ssl.c | 26 ++++++++++++----------- src/openvpn/ssl.h | 8 +++----- 9 files changed, 119 insertions(+), 23 deletions(-)
Changes.rst +10 -1
@@ -100,6 +100,13 @@ Inline auth username and password http-proxy-user-pass too. +Improved control channel packet size control (``max-packet-size``) + The size of control channel is no longer tied to + ``--link-mtu``/``--tun-mtu`` and can be set using ``--max-packet-size``. + Sending large control channel frames is also optimised by allowing 6 + outstanding packets instead of just 4. ``max-packet-size`` will also set + ``mssfix`` to try to limit data-channel packets as well. + Deprecated features ------------------- ``inetd`` has been removed
@@ -162,7 +169,9 @@ User-visible Changes - Option ``--nobind`` is default when ``--client`` or ``--pull`` is used in the configuration - :code:`link_mtu` parameter is removed from environment or replaced with 0 when scripts are called with parameters. This parameter is unreliable and no longer internally calculated. - +- control channel packet maximum size is no longer influenced by + ``--link-mtu``/``--tun-mtu`` and must be set by ``--max-packet-size`` now. + The default is 1250 for the control channel size. - In point-to-point OpenVPN setups (no ``--server``), using ``--explict-exit-notiy`` on one end would terminate the other side at session end. This is considered a no longer useful default and has
doc/man-sections/link-options.rst +31 -3
@@ -172,9 +172,9 @@ the local and the remote host. the first place, and if big packets come through anyhow (from protocols other than TCP), ``--fragment`` will internally fragment them. - Both ``--fragment`` and ``--mssfix`` are designed to work around cases - where Path MTU discovery is broken on the network path between OpenVPN - peers. + ``max-packet-size``, ``--fragment`` and ``--mssfix`` are designed to work + around cases where Path MTU discovery is broken on the network path + between OpenVPN peers. The usual symptom of such a breakdown is an OpenVPN connection which successfully starts, but then stalls during active usage.
@@ -189,6 +189,10 @@ the local and the remote host. --tun-mtu 1500 --fragment 1300 --mssfix + If the ``max-packet-size size`` option is used in the configuration + it will also act as if ``mssfix size mtu`` was specified in the + configuration. + --mtu-disc type Should we do Path MTU discovery on TCP/UDP channel? Only supported on OSes such as Linux that supports the necessary system call to set.
@@ -465,3 +469,27 @@ the local and the remote host. if mode server: socket-flags TCP_NODELAY push "socket-flags TCP_NODELAY" + +--max-packet-size size + This option will instruct OpenVPN to try to limit the maximum on-write packet + size by restricting the control channel packet size and setting ``--mssfix``. + + OpenVPN will try to keep its control channel messages below this size but + due to some constraints in the protocol this is not always possible. If the + option is not set, the control packet maximum size defaults to 1250. + The control channel packet size will be restricted to values between + 512 and 2048. The maximum packet size includes encapsulation overhead like + UDP and IP. + + In terms of ``--mssfix`` it will expand to: + :: + + mssfix size mtu + + If you need to set ``--mssfix`` for data channel and control channel maximum + packet size independently, use ``--max-packet-size`` first, followed by a + ``--mssfix`` in the configuration. + + In general the default size of 1250 should work almost universally apart + from specific corner cases, especially since IPv6 requires a MTU of 1280 + or larger.
src/openvpn/common.h +13 -0
@@ -68,6 +68,19 @@ typedef unsigned long ptr_type;*/ #define TLS_CHANNEL_BUF_SIZE 2048 +/* TLS control buffer minimum size +* +* A control frame might have IPv6 header (40 byte), +* UDP (8 byte), opcode (1), session id (8), +* ACK array with 4 ACKs in non-ACK_V1 packets (25 bytes) +* tls-crypt(56) or tls-auth(up to 72). To allow secure +* renegotiation (dynamic tls-crypt), we set this minimum +* to 154, which only allows 16 byte of payload and should +* be considered an absolute minimum and not a good value to +* set +*/ +#define TLS_CHANNEL_MTU_MIN 154 +/* * This parameter controls the maximum size of a bundle * of pushed options.
src/openvpn/init.c +21 -2
@@ -2640,6 +2640,10 @@ frame_finalize_options(struct context *c, const struct options *o)* space */ size_t payload_size =max_int (1500 , frame->tun_mtu); +/* we need to be also large enough to hold larger control channel packets +* if configured */ + payload_size =max_int (payload_size, o->ce.tls_mtu); +/* The extra tun needs to be added to the payload size */ if (o->ce.tun_mtu_defined) {
@@ -2840,6 +2844,21 @@ do_init_tls_wrap_key(struct context *c) options->ce.tls_crypt_v2_file, options->ce.tls_crypt_v2_file_inline); } +/* We have to ensure that the loaded tls-crypt key is small enough +* to fit into the initial hard reset v3 packet */ +int wkc_len =buf_len (&c->c1.ks.tls_crypt_v2_wkc); + +/* empty ACK/message id, tls-crypt, Opcode, udp , ipv6 */ +int required_size =5 + wkc_len +tls_crypt_buf_overhead () +1 +8 +40 ; + +if (required_size > c->options.ce.tls_mtu) + { +msg (M_WARN ,"ERROR: tls-crypt-v2 client key too large to work with " +"requested --max-packet-size %d, requires at least " +"--max-packet-size %d. Packets will ignore requested " +"maximum packet size" , c->options.ce.tls_mtu, + required_size); + } }
@@ -3187,7 +3206,7 @@ do_init_frame_tls(struct context *c) {if (c->c2.tls_multi) { -tls_multi_init_finalize (c->c2.tls_multi, &c->c2.frame); +tls_multi_init_finalize (c->c2.tls_multi, c->options.ce.tls_mtu);ASSERT (c->c2.tls_multi->opt.frame.buf.payload_size <= c->c2.frame.buf.payload_size); frame_print(&c->c2.tls_multi->opt.frame, D_MTU_INFO,
@@ -3195,7 +3214,7 @@ do_init_frame_tls(struct context *c) }if (c->c2.tls_auth_standalone) { -tls_init_control_channel_frame_parameters (&c->c2.frame, &c->c2.tls_auth_standalone->frame); +tls_init_control_channel_frame_parameters (&c->c2.tls_auth_standalone->frame, c->options.ce.tls_mtu);frame_print (&c->c2.tls_auth_standalone->frame,D_MTU_INFO ,"TLS-Auth MTU parms" ); c->c2.tls_auth_standalone->tls_wrap.work =alloc_buf_gc (BUF_SIZE (&c->c2.frame), &c->c2.gc);
src/openvpn/mtu.h +5 -0
@@ -79,6 +79,11 @@*/ #define MSSFIX_DEFAULT 1492 +/* +* Default maximum size of control channel packets +*/ +#define TLS_MTU_DEFAULT 1250 +/* * Alignment of payload data such as IP packet or * ethernet frame.
src/openvpn/options.c +21 -0
@@ -826,6 +826,7 @@ init_options(struct options *o, const bool init_gc) o->ce.bind_local = true; o->ce.tun_mtu =TUN_MTU_DEFAULT ; o->ce.link_mtu =LINK_MTU_DEFAULT ; + o->ce.tls_mtu =TLS_MTU_DEFAULT ; o->ce.mtu_discover_type =-1 ; o->ce.mssfix =0 ; o->ce.mssfix_default = true;
@@ -1712,6 +1713,7 @@ show_connection_entry(const struct connection_entry *o)SHOW_BOOL (link_mtu_defined);SHOW_INT (tun_mtu_extra);SHOW_BOOL (tun_mtu_extra_defined); +SHOW_INT (tls_mtu);SHOW_INT (mtu_discover_type);
@@ -6457,6 +6459,25 @@ add_option(struct options *options, options->ce.tun_mtu_extra =positive_atoi (p[1 ]); options->ce.tun_mtu_extra_defined = true; } +else if (streq (p[0 ],"max-packet-size" ) && p[1 ] && !p[2 ]) + { +VERIFY_PERMISSION (OPT_P_MTU |OPT_P_CONNECTION ); +int maxmtu =positive_atoi (p[1 ]); + options->ce.tls_mtu =constrain_int (maxmtu,TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE ); + +if (maxmtu <TLS_CHANNEL_MTU_MIN || maxmtu >TLS_CHANNEL_BUF_SIZE ) + { +msg (M_WARN ,"Note: max-packet-size value outside of allowed " +"control channel packet size (%d to %d), will use %d " +"instead." ,TLS_CHANNEL_MTU_MIN ,TLS_CHANNEL_BUF_SIZE , + options->ce.tls_mtu); + } + +/* also set mssfix maxmtu mtu */ + options->ce.mssfix = maxmtu; + options->ce.mssfix_default = false; + options->ce.mssfix_encap = true; + }#ifdef ENABLE_FRAGMENT else if (streq(p[0], "mtu-dynamic")) {
src/openvpn/options.h +1 -0
@@ -123,6 +123,7 @@ struct connection_entrybool tun_mtu_extra_defined;int link_mtu;/* MTU of device over which tunnel packets pass via TCP/UDP */ bool link_mtu_defined;/* true if user overriding parm with command line option */ +int tls_mtu;/* Maximum MTU for the control channel messages */ /* Advanced MTU negotiation and datagram fragmentation options */ int mtu_discover_type;/* used if OS supports setting Path MTU discovery options on socket */
src/openvpn/ssl.c +14 -12
@@ -297,8 +297,7 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes) }void -tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame) +tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu) {/* * frame->extra_frame is already initialized with tls_auth buffer requirements,
@@ -323,18 +322,21 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame/* Previous OpenVPN version calculated the maximum size and buffer of a * control frame depending on the overhead of the data channel frame -* overhead and limited its maximum size to 1250. We always allocate the -* TLS_CHANNEL_BUF_SIZE buffer size since a lot of code blindly assumes -* a large buffer (e.g. PUSH_BUNDLE_SIZE) and also our peer might have -* a higher size configured and we still want to be able to receive the -* packets. frame->mtu_mtu is set as suggestion for the maximum packet -* size */ - frame->buf.payload_size =1250 + overhead; +* overhead and limited its maximum size to 1250. Since control frames +* also need to fit into data channel buffer we have the same +* default of 1500 + 100 as data channel buffers have. Increasing +* control channel mtu beyond this limit also increases the data channel +* buffers */ + frame->buf.payload_size =max_int (1500 , tls_mtu) +100 ; frame->buf.headroom = overhead; frame->buf.tailroom = overhead; - frame->tun_mtu =min_int (data_channel_frame->tun_mtu,1250 ); + frame->tun_mtu = tls_mtu; + +/* Ensure the tun-mtu stays in a valid range */ + frame->tun_mtu =min_int (frame->tun_mtu,TLS_CHANNEL_BUF_SIZE ); + frame->tun_mtu =max_int (frame->tun_mtu,TLS_CHANNEL_MTU_MIN ); }/**
@@ -1315,9 +1317,9 @@ tls_multi_init(struct tls_options *tls_options) }void -tls_multi_init_finalize (struct tls_multi *multi,const struct frame *frame) +tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu) { -tls_init_control_channel_frame_parameters (frame, &multi->opt.frame); +tls_init_control_channel_frame_parameters (&multi->opt.frame, tls_mtu);/* initialize the active and untrusted sessions */ tls_session_init (multi, &multi->session[TM_ACTIVE ]);
src/openvpn/ssl.h +3 -5
@@ -166,10 +166,9 @@ struct tls_multi *tls_multi_init(struct tls_options *tls_options);* * @param multi - The \c tls_multi structure of which to finalize * initialization. -* @param frame - The data channel's \c frame structure. +* @param tls_mtu - maximum allowed size for control channel packets */ -void tls_multi_init_finalize (struct tls_multi *multi, -const struct frame *frame); +void tls_multi_init_finalize (struct tls_multi *multi,int tls_mtu);/* * Initialize a standalone tls-auth verification object.
@@ -181,8 +180,7 @@ struct tls_auth_standalone *tls_auth_standalone_init(struct tls_options *tls_opt* Setups the control channel frame size parameters from the data channel * parameters */ -void tls_init_control_channel_frame_parameters (const struct frame *data_channel_frame, -struct frame *frame); +void tls_init_control_channel_frame_parameters (struct frame *frame,int tls_mtu);/* * Set local and remote option compatibility strings.
Signature · 1 line
2.37.0 (Apple Git-136)
[Openvpn-devel] [PATCH applied] Re: Refactor/optimise code sending TLS control channel messages
In reply to Arne SchwabeThis is a slightly complicated ACK - this is a respin of 1/3 v4 with the "Ensure that control channel packet are respecting maximum packet size" patch, which has an ACK from Frank Lichtenheld. During testing of v4 I noticed that it it brings a regression for "very small packets" (--link-mtu 440 and below) - not that these are overly useful, but if it worked before, why shouldn't it stop afterwards? So I started a discussion on IRC which ended here.
Most of this patch is identical to "1/3 v4", except for a typo fix :-), avoiding an integer underrun ("tmp.len > 0" now), removing the 2 byte TCP length from "overhead", using the actual AF_ instead of always going for AF_INET, and most important, the "maxlen < TLS_CHANNEL_BUF_SIZE" that broke "very small packets" in v4 got changed to "for really really small packets, we might exceed the MTU", but this is in the order of "16 bytes payload". Better die trying than not try at all...
The "if (maxlen < 16)" bit looks a bit strange, setting maxlen to 16 (okay) and max_pkt_len to TLS_CHANNEL_BUF_SIZE - if we get there with maxlen = 16, max_pkt_len will also be something like "16" (assuming rel_avail == 1 and no WKC) or maybe "32", but nowhere near 1250... OTOH it is of no real significance, as "maxlen" controls how many bytes we'll ever request as payload - so, 16+WKC, well below 1250.
I am a bit concerned that the inner loop in write_outgoing_tls_ciphertext() could end up as an endless loop, if "max_pkt_len" ends up being <= "buf_len(session->tls_wrap.tls_crypt_v2_wkc))", so the max_int() ends up with "len == 0", buf_copy_n() copying nothing, and the loop repeating "send packets with a WKC and nothing else", forever.
OTOH, this needs a very small MTU to start with, which is not possible to trigger with this patch alone (--link-mtu below 125 is refused with "TUN MTU value (nn) must be at least 100" error). But I'd still like either confirmation that I have overlooked something, or maybe adjust the max_int() to "1, ..." so it's clear we'll always handle 1 byte per loop, whatever happens...
Another observation: getting rid of all the "if (maxlen<len)" checks in the BIO path leads to extra alloc_buf_gc() calls to convey the "maxlen" by means of the "tmp" buf. Not sure if this is a good tradeoff... more alloc()/free() against a few less integer compares... (but then, this is control channel, which is not the most critical fast path).
There are a few other bits in here that need work, like a comment talking about "frame->mtu_mtu" and "TLS_CHANNEL_BUF_SIZE" while the code does "1250" magic numbers - but these get fixed in 2/2.
This all said, I did my best to break master + this patch, and couldn't find a way - so, merged. I tested server side (without any special MTU settings) and client side with --link-mtu $various, comparing the UDP output I saw in tcpdump. We're still exceeding the --link-mtu value for some cases (--link-mtu 125 -> UDP payload 121 bytes for a few packets, and "72" for very many of them, both not really "125"
- and I wonder where the 72 is coming from, leaving lots of space unused. With --link-mtu 200, the maximum I saw is 171 bytes, so at least this patch tends to underflow available MTU (and same thing for 300/271, 400/371). Will come back to this with 2/2 v6 :-)
Your patch has been applied to the master branch.
commit 4e9e25a9e547ab6e1f71003947a2d186dc231cb6
Author: Arne Schwabe Date: Fri Nov 4 13:56:54 2022 +0100
Refactor/optimise code sending TLS control channel messages
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg25478.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Add unit test for reliable_get_num_output_sequenced_available
In reply to Arne SchwabeThis adds the unit test for the previously introduced new function - and applies nicely on "v6 1/2". Tested on FreeBSD and Linux, and "it passes".
..
[ RUN ] test_get_num_output_sequenced_available
[ OK ] test_get_num_output_sequenced_available
[==========] 7 test(s) run.
[ PASSED ] 7 test(s).
PASS: packet_id_testdriver
Since this got an ACK from Frank, I did not spend much stare-at-code to verify that it actually tests something useful - I *have* found a tab error in tests/unit_tests/openvpn/Makefile.am, though, and fixed on the fly :-)
Your patch has been applied to the master branch.
commit a5a30ec311ce9d0dbcd6162daab97a225189d570
Author: Arne Schwabe Date: Wed Sep 21 12:49:30 2022 +0200
Add unit test for reliable_get_num_output_sequenced_available
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg25292.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: Allow setting control channel packet size with max-packet-size
In reply to Arne SchwabeStared-at-code, discussed with Arne on IRC. There's a few things one could have done differently (like, using constrain_int() inside tls_init_control_channel_frame_parameters()), but this is all minor.
Tested regular t_client and server side tests (which are not excercising the new option) - all good.
Tested with the new option, to see what I could break (no tls-auth or tls-crypt in use for this test)
- not specified -> maximum TLS packet size is 1250 byte (1264 eth len)
- --max-packet-size 100
-> tells me: "Note: max-packet-size value outside of allowed control
channel packet size (154 to 2048), will use 154 instead." and
maximum TLS packet size observed is 154 byte, with a single packet of 169 byte
- --max-packet-size 500
-> most packets are 488 bytes, one is 500 bytes
- --max-packet-size 3000
-> it complains to me, "will use 2048 instead". I observe outside- fragmented packets, with an inner UDP length of 2020, and a total combined length of both fragments of 2056 bytes (second IP header adds more bytes outside OpenVPN Control).
This test case actually fails to establish a TLS session(!) - the server I connect to has the same code, but no --max-packet option in its config, so it does not want to see these packets:
Nov 6 09:41:39 gentoo tun-udp-p2mp-topology-subnet[15790]: 194.97.140.21:40355 Incoming control channel packet too big, dropping.
... this is not surprising, tbh, as we allocate "1500+100" by default (so --max-packet-size 1600 *does* work, two ip fragments, being properly reassembled and handled by the server)
- results are consistent for IPv4 and IPv6 (= UDP payload is smaller on --proto udp6, total packet size is the same)
- mssfix also seems to do what it says on the lid
09:10:42.860856 IP 10.194.0.1.774 > 10.194.3.9.37123: Flags [S.], seq 2812517040, ack 1148395195, win 65535, options [mss 32,nop,wscale 6,sackOK,TS val 574950381 ecr 3397293635], length 0
(for a --max-packet-size 154 session)
- if I poke the bear with --max-packet-size 100, *TLS* MTU will be ugprade to 154 (as the warning says), but --mssfix will be broken, as it installs the "not constrained_int()" value to ce.mssfix
+ int maxmtu = positive_atoi(p[1]);
+ options->ce.tls_mtu = constrain_int(maxmtu, TLS_CHANNEL_MTU_MIN, TLS_CH
...
+ options->ce.mssfix = maxmtu;
(this will result in a mssfix value <= 0, so "no mssfix activity")
- --maxpacket-size 113 over udp6 will result in
09:14:47.172084 IP 10.194.0.1.774 > 10.194.3.9.50937: Flags [S.], seq 3614955968, ack 1939523050, win 65535, options [mss 0,nop,wscale 6,sackOK,TS val 371801058 ecr 3794849425], length 0
... which the sending stack seems to treat with "no idea what the other end wants, I ignore this". 114 leads to "mss 1", which also is not handled by the other end as "only 1 byte segments", but I see 204 bytes
- so FreeBSD seems to set that as minimum MSS.
I think a followup patch should maybe use "ce.tls_mtu" (constrained) and not the raw argument here. Less confusion.
- for --max-packet-size 500 ("reasonable values"), implccit mssfix mtu works as expected.
- --max-packet-size 145 + tls-auth --> works
- --max-packet-size 145 + tls-crypt --> works, though only 138 bytes
- --max-packet-size 145 + tls-crypt-v2 --> works, though only 138 bytes
the last 3 have been added to the "server test / master t_client.rc" testbed.
I have added the oxford comma, as requested, and also a "--" before "max-packet-size" in that line. The other two wording fixes are not needed as that comment got completely rewritten. Found another typo ("udp , ipv6").
Also, I have updated the man page to show "154" as the correct minimum in v6, it was still talking about 512 (why are we limiting the maximum to 2048, btw?).
Your patch has been applied to the master branch.
commit 5f6ea5975927627680c21c10670ccb8503f18249
Author: Arne Schwabe Date: Fri Nov 4 13:56:55 2022 +0100
Allow setting control channel packet size with max-packet-size
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg25477.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering