Skip to contentSkip to conversation

Big buffer/frame refactoring patch set

PATCH Superseded Arne Schwabe 4 participants 66 messages Dec 7, 2021 – Dec 31, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Big buffer/frame refactoring patch set

This patchset refactores a lot of the buffer related code. It mainly uses the approach of simplifying the buffer sizes by always allocating buffers with worst case overhead instead of trying a save < 100 bytes and complicating the code.

It also moves to central functions that calculate the various overheads from the small little functions scattered all over the source code.

And last but not least, this patch set also corrects the MSS and fragment size calculation to take into account the real overhead that is present after NCP. It also adds a a 'mtu' keyword to both options so the desired real outer packet can be specified.

The patchset also deprecates link-mtu as this options is no longer working well as intended since neither encryption/authentication overhead is fixed (NCP) nor is the encapsulation overhead constant (IPv4 vs IPv6) and link-mtu basically assumes that the overhead is constant.

This patchset needs the "Cleanup crypto and use string isntead internal types" patch set applied first or will have conflicts.

Arne Schwabe (21):
Remove max_size from buffer_list_new
Deprecate link-mtu
Remove align_adjust frame code
Fix triggering assertion of ks->authticated after tls_deauthenticate Document frame related function and variables a bit more Remove post_open_mtu code
Add helper functions to calculate header/payload sizes Decouple MSS fix calculation from frame calculation Rework occ link-mtu calculation
Change buffer allocation calculation and checks to be more static Remove pointless do_init_frame_tls function
Fix datagram_overhead and assorted functions
Implement optional mtu parameter for mssfix
Add mtu paramter to --fragment and change fragment calculation Update fragment and mssfix related warnings
Remove link_mtu parameter when running up/down scripts Use new frame header methods to calculate OCC_MTU_LOAD payload size Remove extra_link from frame
Replace TUN_MTU_SIZE with frame->tun_mtu
Remove frame->link_mtu
Remove frame.extra_frame and frame.extra_buffer

 Changes.rst                            |   9 +
 doc/man-sections/link-options.rst      |  42 ++--
 src/openvpn/buffer.c                   |   7 +-
 src/openvpn/buffer.h                   |   4 +-
 src/openvpn/comp-lz4.c                 |   4 +-
 src/openvpn/comp.c                     |  15 --
 src/openvpn/comp.h                     |   4 -
 src/openvpn/crypto.c                   |  64 ++++--
 src/openvpn/crypto.h                   |  23 +-
 src/openvpn/forward.c                  |  32 +--
 src/openvpn/fragment.c                 |   9 +-
 src/openvpn/init.c                     | 302 +++++++++++--------------
 src/openvpn/init.h                     |   2 +-
 src/openvpn/lzo.c                      |   2 +-
 src/openvpn/manage.c                   |   4 +-
 src/openvpn/mss.c                      | 154 +++++++++++++
 src/openvpn/mss.h                      |  14 ++
 src/openvpn/mtu.c                      | 179 +++++++++------
 src/openvpn/mtu.h                      | 253 ++++++++++-----------
 src/openvpn/multi.c                    |   7 +-
 src/openvpn/multi.h                    |   2 +-
 src/openvpn/occ.c                      |  34 ++-
 src/openvpn/options.c                  |  82 ++-----
 src/openvpn/options.h                  |   7 +-
 src/openvpn/ping.c                     |   2 +-
 src/openvpn/proto.h                    |  11 -
 src/openvpn/reliable.c                 |   7 -
 src/openvpn/reliable.h                 |   3 -
 src/openvpn/socket.c                   |  34 +--
 src/openvpn/socket.h                   |  19 +-
 src/openvpn/socks.c                    |  11 +-
 src/openvpn/socks.h                    |   2 -
 src/openvpn/ssl.c                      |  99 ++++----
 src/openvpn/ssl.h                      |  10 +-
 src/openvpn/ssl_common.h               |   3 +-
 src/openvpn/tls_crypt.c                |  10 -
 src/openvpn/tls_crypt.h                |   5 -
 src/openvpn/tun.c                      |   1 -
 src/openvpn/tun.h                      |   4 -
 src/openvpn/win32.c                    |   2 +-
 tests/unit_tests/openvpn/Makefile.am   |   6 +-
 tests/unit_tests/openvpn/test_buffer.c |  22 +-
 tests/unit_tests/openvpn/test_crypto.c | 128 ++++++++++-
 tests/unit_tests/openvpn/test_misc.c   |   1 +
 44 files changed, 900 insertions(+), 735 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH 1/21 Remove max_size from buffer_list_new

This argument is never used apart from a unit test. Remove this argument as a small cleanup.

---
 src/openvpn/buffer.c                   |  7 +++----
 src/openvpn/buffer.h                   |  4 +---
 src/openvpn/manage.c                   |  4 ++--
 src/openvpn/ssl.c                      |  2 +-
 tests/unit_tests/openvpn/test_buffer.c | 22 ++++------------------
 5 files changed, 11 insertions(+), 28 deletions(-)
src/openvpn/buffer.c +3 -4
index 486a77548..e9afb6d6a 100644
@@ -1171,11 +1171,10 @@ valign4(const struct buffer *buf, const char *file, const int line)
  * struct buffer_list
  */
 struct buffer_list *
-buffer_list_new(const int max_size)
+buffer_list_new()
 {
     struct buffer_list *ret;
     ALLOC_OBJ_CLEAR(ret, struct buffer_list);
-    ret->max_size = max_size;
     ret->size = 0;
     return ret;
 }
@@ -1229,7 +1228,7 @@ struct buffer_entry *
 buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size)
 {
     struct buffer_entry *e = NULL;
-    if (data && (!ol->max_size || ol->size < ol->max_size))
+    if (data)
     {
         ALLOC_OBJ_CLEAR(e, struct buffer_entry);
 
@@ -1359,7 +1358,7 @@ buffer_list_file(const char *fn, int max_line_len)
         char *line = (char *) malloc(max_line_len);
         if (line)
         {
-            bl = buffer_list_new(0);
+            bl = buffer_list_new();
             while (fgets(line, max_line_len, fp) != NULL)
             {
                 buffer_list_push(bl, line);
src/openvpn/buffer.h +1 -3
index 8cc03c08f..619c3a95d 100644
@@ -1102,11 +1102,9 @@ struct buffer_list
 /**
  * Allocate an empty buffer list of capacity \c max_size.
  *
- * @param max_size  the capacity of the list to allocate
- *
  * @return the new list
  */
-struct buffer_list *buffer_list_new(const int max_size);
+struct buffer_list *buffer_list_new();
 
 /**
  * Frees a buffer list and all the buffers in it.
src/openvpn/manage.c +2 -2
index 28315b82a..1f408f0b5 100644
@@ -878,7 +878,7 @@ in_extra_reset(struct man_connection *mc, const int mode)
         }
         if (mode == IER_NEW)
         {
-            mc->in_extra = buffer_list_new(0);
+            mc->in_extra = buffer_list_new();
         }
     }
 }
@@ -2507,7 +2507,7 @@ man_connection_init(struct management *man)
          * command output from/to the socket.
          */
         man->connection.in = command_line_new(1024);
-        man->connection.out = buffer_list_new(0);
+        man->connection.out = buffer_list_new();
 
         /*
          * Initialize event set for standalone usage, when we are
src/openvpn/ssl.c +1 -1
index 0d811f24e..05096ee0a 100644
@@ -3989,7 +3989,7 @@ tls_send_payload(struct tls_multi *multi,
     {
         if (!ks->paybuf)
         {
-            ks->paybuf = buffer_list_new(0);
+            ks->paybuf = buffer_list_new();
         }
         buffer_list_push_data(ks->paybuf, data, (size_t)size);
         ret = true;
tests/unit_tests/openvpn/test_buffer.c +4 -18
index 5e854c22e..ac701669f 100644
@@ -67,18 +67,18 @@ static int
 test_buffer_list_setup(void **state)
 {
     struct test_buffer_list_aggregate_ctx *ctx  = calloc(1, sizeof(*ctx));
-    ctx->empty = buffer_list_new(0);
+    ctx->empty = buffer_list_new();
 
-    ctx->one_two_three = buffer_list_new(3);
+    ctx->one_two_three = buffer_list_new();
     buffer_list_push(ctx->one_two_three, teststr1);
     buffer_list_push(ctx->one_two_three, teststr2);
     buffer_list_push(ctx->one_two_three, teststr3);
 
-    ctx->zero_length_strings = buffer_list_new(2);
+    ctx->zero_length_strings = buffer_list_new();
     buffer_list_push(ctx->zero_length_strings, "");
     buffer_list_push(ctx->zero_length_strings, "");
 
-    ctx->empty_buffers = buffer_list_new(2);
+    ctx->empty_buffers = buffer_list_new();
     uint8_t data = 0;
     buffer_list_push_data(ctx->empty_buffers, &data, 0);
     buffer_list_push_data(ctx->empty_buffers, &data, 0);
@@ -100,17 +100,6 @@ test_buffer_list_teardown(void **state)
     return 0;
 }
 
-static void
-test_buffer_list_full(void **state)
-{
-    struct test_buffer_list_aggregate_ctx *ctx = *state;
-
-    /* list full */
-    assert_int_equal(ctx->one_two_three->size, 3);
-    buffer_list_push(ctx->one_two_three, teststr4);
-    assert_int_equal(ctx->one_two_three->size, 3);
-}
-
 static void
 test_buffer_list_aggregate_separator_empty(void **state)
 {
@@ -247,9 +236,6 @@ main(void)
 {
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(test_buffer_strprefix),
-        cmocka_unit_test_setup_teardown(test_buffer_list_full,
-                                        test_buffer_list_setup,
-                                        test_buffer_list_teardown),
         cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_empty,
                                         test_buffer_list_setup,
                                         test_buffer_list_teardown),
Signature · 1 line
2.33.0
Acked-by on v1 1/21
#2 lore raw

[Openvpn-devel] [PATCH applied] Re: Remove max_size from buffer_list_new

That was one of the easy ones :-) - verified that indeed, this option is never used in OpenVPN code.

I have client-side tested this ("make check"), just to be sure.

"void" has been added to prototype and function declaration...

Your patch has been applied to the master branch.

commit 5962f6267fcdf33f83f8de725751b5ef41e94f7c

Author: Arne Schwabe
Date:   Tue Dec 7 18:01:51 2021 +0100
     Remove max_size from buffer_list_new

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#3 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: Remove max_size from buffer_list_new

Hi,

Quoted 15 lines · Gert Doering

That was one of the easy ones :-) - verified that indeed, this option is never used in OpenVPN code.

I have client-side tested this ("make check"), just to be sure.

"void" has been added to prototype and function declaration...

Your patch has been applied to the master branch.

commit 5962f6267fcdf33f83f8de725751b5ef41e94f7c

Author: Arne Schwabe
Date:   Tue Dec 7 18:01:51 2021 +0100
Quoted 6 lines
     Remove max_size from buffer_list_new

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...

*sigh* one day I'll learn... mail-archive search engine takes a few hours before message IDs can be found. So usually when a patch is very fresh, I check the "URL:" that the script puts into the commit message, and adjust from the web archive.

Forgot for this one, but noticed before pushing - so, fixing the commit message with

URL: https://www.mail-archive.com/[email protected]/msg23329.html

which of course creates a new commit ID.

Thus:

commit 61d2f918d53c932acd1061b8523c54c18ebb8176 (HEAD -> master)

Author: Arne Schwabe <[email protected]...>
Date:   Tue Dec 7 18:01:51 2021 +0100
    Remove max_size from buffer_list_new

    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]/msg23329.html
    Signed-off-by: Gert Doering <[email protected]...>

Stupid toolchain...

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#4 lore raw

PATCH 2/21 Deprecate link-mtu

In reply to Arne Schwabe

This options might have been useful in the past but nowadays it has a very unclear semantics, so better remove/deprecate it.

---
 doc/man-sections/link-options.rst | 7 ++++++-
 src/openvpn/options.c             | 4 +---
 2 files changed, 7 insertions(+), 4 deletions(-)
doc/man-sections/link-options.rst +6 -1
index 32e72a1b7..b1ae4e75a 100644
@@ -82,10 +82,15 @@ the local and the remote host.
          ping-restart 60            # Argument: timeout
 
 --link-mtu n
-  Sets an upper bound on the size of UDP packets which are sent between
+  **DEPRECATED** Sets an upper bound on the size of UDP packets which are sent between
   OpenVPN peers. *It's best not to set this parameter unless you know what
   you're doing.*
 
+  Due to variable header size of IP header (20 bytes for IPv4 and 40 bytes
+  for IPv6) and dynamically negotiated data channel cipher, this option
+  is not reliable. It is recommended to set tun-mtu with enough headroom
+  instead.
+
 --local host
   Local host name or IP address for bind. If specified, OpenVPN will bind
   to this address only. If unspecified, OpenVPN will bind to all
src/openvpn/options.c +1 -3
index b840b767b..c1663b264 100644
@@ -2111,9 +2111,7 @@ options_postprocess_verify_ce(const struct options *options,
      */
     if (options->ce.tun_mtu_defined && options->ce.link_mtu_defined)
     {
-        msg(M_USAGE,
-            "only one of --tun-mtu or --link-mtu may be defined (note that "
-            "--ifconfig implies --link-mtu %d)", LINK_MTU_DEFAULT);
+        msg(M_USAGE, "only one of --tun-mtu or --link-mtu may be defined");
     }
 
     if (!proto_is_udp(ce->proto) && options->mtu_test)
Signature · 1 line
2.33.0
#5 lore raw

PATCH 3/21 Remove align_adjust frame code

In reply to Arne Schwabe

The align_adjust variable was only set to a non-zero value when no cipher was used for the data channel. Since we no longer want to optimise non encrypted data channel traffic, remove this optimisation and simplify the code.

---
 src/openvpn/crypto.c   |  4 ++--
 src/openvpn/forward.c  |  2 +-
 src/openvpn/fragment.c |  2 +-
 src/openvpn/init.c     | 13 -------------
 src/openvpn/mtu.c      |  9 ++-------
 src/openvpn/mtu.h      | 38 ++++++++------------------------------
 src/openvpn/socket.c   |  3 +--
 src/openvpn/win32.c    |  2 +-
 8 files changed, 16 insertions(+), 57 deletions(-)
src/openvpn/crypto.c +2 -2
index 36f880433..cd791ab8a 100644
@@ -370,7 +370,7 @@ openvpn_decrypt_aead(struct buffer *buf, struct buffer work,
 
     ASSERT(ad_start >= buf->data && ad_start <= BPTR(buf));
 
-    ASSERT(buf_init(&work, FRAME_HEADROOM_ADJ(frame, FRAME_HEADROOM_MARKER_DECRYPT)));
+    ASSERT(buf_init(&work, FRAME_HEADROOM(frame)));
 
     /* IV and Packet ID required for this mode */
     ASSERT(packet_id_initialized(&opt->packet_id));
@@ -533,7 +533,7 @@ openvpn_decrypt_v1(struct buffer *buf, struct buffer work,
             int outlen;
 
             /* initialize work buffer with FRAME_HEADROOM bytes of prepend capacity */
-            ASSERT(buf_init(&work, FRAME_HEADROOM_ADJ(frame, FRAME_HEADROOM_MARKER_DECRYPT)));
+            ASSERT(buf_init(&work, FRAME_HEADROOM(frame)));
 
             /* read the IV from the packet */
             if (buf->len < iv_size)
src/openvpn/forward.c +1 -1
index 41ef12e30..29efcd3b9 100644
@@ -803,7 +803,7 @@ read_incoming_link(struct context *c)
     perf_push(PERF_READ_IN_LINK);
 
     c->c2.buf = c->c2.buffers->read_link_buf;
-    ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM_ADJ(&c->c2.frame, FRAME_HEADROOM_MARKER_READ_LINK)));
+    ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
 
     status = link_socket_read(c->c2.link_socket,
                               &c->c2.buf,
src/openvpn/fragment.c +1 -1
index aba611fa0..6f8fb4476 100644
@@ -214,7 +214,7 @@ fragment_incoming(struct fragment_master *f, struct buffer *buf,
                 frag->defined = true;
                 frag->max_frag_size = size;
                 frag->map = 0;
-                ASSERT(buf_init(&frag->buf, FRAME_HEADROOM_ADJ(frame, FRAME_HEADROOM_MARKER_FRAGMENT)));
+                ASSERT(buf_init(&frag->buf, FRAME_HEADROOM(frame)));
             }
 
             /* copy the data to fragment buffer */
src/openvpn/init.c +0 -13
index f8a13fdfa..0009bcb72 100644
@@ -2461,19 +2461,6 @@ frame_finalize_options(struct context *c, const struct options *o)
         o = &c->options;
     }
 
-    /*
-     * Set adjustment factor for buffer alignment when no
-     * cipher is used.
-     */
-    if (!cipher_defined(c->c1.ks.key_type.cipher))
-    {
-        frame_align_to_extra_frame(&c->c2.frame);
-        frame_or_align_flags(&c->c2.frame,
-                             FRAME_HEADROOM_MARKER_FRAGMENT
-                             |FRAME_HEADROOM_MARKER_READ_LINK
-                             |FRAME_HEADROOM_MARKER_READ_STREAM);
-    }
-
     frame_add_to_extra_buffer(&c->c2.frame, PAYLOAD_ALIGN);
     frame_finalize(&c->c2.frame,
                    o->ce.link_mtu_defined,
src/openvpn/mtu.c +2 -7
index e4143e267..0ab716d7a 100644
@@ -42,12 +42,11 @@
 void
 alloc_buf_sock_tun(struct buffer *buf,
                    const struct frame *frame,
-                   const bool tuntap_buffer,
-                   const unsigned int align_mask)
+                   const bool tuntap_buffer)
 {
     /* allocate buffer for overlapped I/O */
     *buf = alloc_buf(BUF_SIZE(frame));
-    ASSERT(buf_init(buf, FRAME_HEADROOM_ADJ(frame, align_mask)));
+    ASSERT(buf_init(buf, FRAME_HEADROOM(frame)));
     buf->len = tuntap_buffer ? MAX_RW_SIZE_TUN(frame) : MAX_RW_SIZE_LINK(frame);
     ASSERT(buf_safe(buf, 0));
 }
@@ -153,10 +152,6 @@ frame_print(const struct frame *frame,
     buf_printf(&out, " EB:%d", frame->extra_buffer);
     buf_printf(&out, " ET:%d", frame->extra_tun);
     buf_printf(&out, " EL:%d", frame->extra_link);
-    if (frame->align_flags && frame->align_adjust)
-    {
-        buf_printf(&out, " AF:%u/%d", frame->align_flags, frame->align_adjust);
-    }
     buf_printf(&out, " ]");
 
     msg(level, "%s", out.data);
src/openvpn/mtu.h +8 -30
index 7b18b3621..72a9e515b 100644
@@ -121,17 +121,10 @@ struct frame {
 
     int extra_link;             /**< Maximum number of bytes in excess of
                                  *   external network interface's MTU that
-                                 *   might be read from or written to it. */
-
-    /*
-     * Alignment control
-     */
-#define FRAME_HEADROOM_MARKER_DECRYPT     (1<<0)
-#define FRAME_HEADROOM_MARKER_FRAGMENT    (1<<1)
-#define FRAME_HEADROOM_MARKER_READ_LINK   (1<<2)
-#define FRAME_HEADROOM_MARKER_READ_STREAM (1<<3)
-    unsigned int align_flags;
-    int align_adjust;
+                                 *   might be read from or written to it.
+                                 *
+                                 *   Used by peer-id (3) and
+                                 *   socks UDP (10) */
 };
 
 /* Forward declarations, to prevent includes */
@@ -184,8 +177,7 @@ struct options;
  * Control buffer headroom allocations to allow for efficient prepending.
  */
 #define FRAME_HEADROOM_BASE(f)     (TUN_LINK_DELTA(f) + (f)->extra_buffer + (f)->extra_link)
-#define FRAME_HEADROOM(f)          frame_headroom(f, 0)
-#define FRAME_HEADROOM_ADJ(f, fm)  frame_headroom(f, fm)
+#define FRAME_HEADROOM(f)          frame_headroom(f)
 
 /*
  * Max size of a buffer used to build a packet for output to
@@ -227,8 +219,7 @@ void frame_set_mtu_dynamic(struct frame *frame, int mtu, unsigned int flags);
  */
 void alloc_buf_sock_tun(struct buffer *buf,
                         const struct frame *frame,
-                        const bool tuntap_buffer,
-                        const unsigned int align_mask);
+                        const bool tuntap_buffer);
 
 /** Set the --mssfix option. */
 void frame_init_mssfix(struct frame *frame, const struct options *options);
@@ -252,11 +243,10 @@ const char *format_extended_socket_error(int fd, int *mtu, struct gc_arena *gc);
  * headroom and alignment issues.
  */
 static inline int
-frame_headroom(const struct frame *f, const unsigned int flag_mask)
+frame_headroom(const struct frame *f)
 {
     const int offset = FRAME_HEADROOM_BASE(f);
-    const int adjust = (flag_mask & f->align_flags) ? f->align_adjust : 0;
-    const int delta = ((PAYLOAD_ALIGN << 24) - (offset + adjust)) & (PAYLOAD_ALIGN - 1);
+    const int delta = ((PAYLOAD_ALIGN << 24) - offset) & (PAYLOAD_ALIGN - 1);
     return offset + delta;
 }
 
@@ -300,18 +290,6 @@ frame_add_to_extra_buffer(struct frame *frame, const int increment)
     frame->extra_buffer += increment;
 }
 
-static inline void
-frame_align_to_extra_frame(struct frame *frame)
-{
-    frame->align_adjust = frame->extra_frame + frame->extra_link;
-}
-
-static inline void
-frame_or_align_flags(struct frame *frame, const unsigned int flag_mask)
-{
-    frame->align_flags |= flag_mask;
-}
-
 static inline bool
 frame_defined(const struct frame *frame)
 {
src/openvpn/socket.c +1 -2
index 72062cd08..df7367469 100644
@@ -1645,8 +1645,7 @@ socket_frame_init(const struct frame *frame, struct link_socket *sock)
 #else
         alloc_buf_sock_tun(&sock->stream_buf_data,
                            frame,
-                           false,
-                           FRAME_HEADROOM_MARKER_READ_STREAM);
+                           false);
 
         stream_buf_init(&sock->stream_buf,
                         &sock->stream_buf_data,
src/openvpn/win32.c +1 -1
index fd1246cde..1dc1c5e77 100644
@@ -186,7 +186,7 @@ overlapped_io_init(struct overlapped_io *o,
     }
 
     /* allocate buffer for overlapped I/O */
-    alloc_buf_sock_tun(&o->buf_init, frame, tuntap_buffer, 0);
+    alloc_buf_sock_tun(&o->buf_init, frame, tuntap_buffer);
 }
 
 void
Signature · 1 line
2.33.0
Acked-by on v1 3/21
#6 lore raw

[Openvpn-devel] [PATCH applied] Re: Remove align_adjust frame code

Followed the twisty maze through the code, and I agree with the observation - align_adjust is effectively not used for "normal" data paths (not cipher none), so align_adjust + align_flags can go.

Lightly tested on client side. Including "cipher none" :-)

Your patch has been applied to the master branch.

commit 053de4db59a2d99eed8a07877ea321be4065ba26

Author: Arne Schwabe
Date:   Tue Dec 7 18:01:53 2021 +0100
     Remove align_adjust frame code

     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]/msg23331.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#7 lore raw

PATCH 4/21 Fix triggering assertion of ks->authticated after tls_deauthenticate

In reply to Arne Schwabe

When tls_deauthenticate is called (e.g. by management kicking of a client) the key auth state is changed to KS_AUTH_FALSE while the key state is still in S_GENERATED_KEYS. This triggers the assertion. Remove the assertions and instead check that the auth state is KS_AUTH_TRUE

---
 src/openvpn/ssl.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
src/openvpn/ssl.c +2 -3
index 05096ee0a..8cbb129d2 100644
@@ -3276,9 +3276,9 @@ handle_data_channel_packet(struct tls_multi *multi,
          * active side is the client which initiates connections).
          */
         if (ks->state >= S_GENERATED_KEYS && key_id == ks->key_id
+            && ks->authenticated == KS_AUTH_TRUE
             && (floated || link_socket_actual_match(from, &ks->remote_addr)))
         {
-            ASSERT(ks->authenticated == KS_AUTH_TRUE);
             if (!ks->crypto_options.key_ctx_bi.initialized)
             {
                 msg(D_MULTI_DROPPED,
@@ -3861,9 +3861,8 @@ struct key_state *tls_select_encryption_key(struct tls_multi *multi)
     for (int i = 0; i < KEY_SCAN_SIZE; ++i)
     {
         struct key_state *ks = get_key_scan(multi, i);
-        if (ks->state >= S_GENERATED_KEYS)
+        if (ks->state >= S_GENERATED_KEYS && ks->authenticated == KS_AUTH_TRUE)
         {
-            ASSERT(ks->authenticated == KS_AUTH_TRUE);
             ASSERT(ks->crypto_options.key_ctx_bi.initialized);
 
             if (!ks_select)
Signature · 1 line
2.33.0
Acked-by on v1 4/21
#8 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix triggering assertion of ks->authenticated after tls_deauthenticate

I have not tested the actuall ASSERT() crash, but went through the change asking myself "so, what will happen instead, now?"

- handle_data_channel_packet() will now ignore all keys that are no longer KS_AUTH_TRUE, so if there is no other key, it will end up in "TLS error: local/remote TLS keys are out of sync" and drop the incoming packet (buf->len = 0).

- tls_select_encryption_key() will also ignore those keys, and possibly return NULL. This is only called from tls_pre_encrypt(), which will log "TLS Warning: no data channel send key available" in this case, and drop the outgoing packlet (buf->len = 0).

Looking at tls_deauthenticate(), the problem described sounds real - this function sets all keys to KS_AUTH_FALSE, without changing ks->state, thus the next incoming or outgoing packet for that peer would trigger the ASSERT() (and this looks like could happen on regular renegotiation, like on a username or CN change, so this sounds like an actual DoS vector againt a "master" server...).

The patch should fix this, without introducing undesired new side effects (like, bypassing KS_AUTH_FALSE).

Subjected to a full set of server tests (passed).

Your patch has been applied to the master branch.

commit 75f9fb6f5b9bf57578086c3e42870bba4a914d7c

Author: Arne Schwabe
Date:   Tue Dec 7 18:01:54 2021 +0100
     Fix triggering assertion of ks->authenticated after tls_deauthenticate

     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]/msg23340.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#9 lore raw

PATCH 5/21 Document frame related function and variables a bit more

In reply to Arne Schwabe
---
 src/openvpn/mtu.h | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
src/openvpn/mtu.h +20 -2
index 72a9e515b..c1148c317 100644
@@ -28,7 +28,7 @@
 
 /*
  *
- * Packet maninipulation routes such as encrypt, decrypt, compress, decompress
+ * Packet manipulation routes such as encrypt, decrypt, compress, decompress
  * are passed a frame buffer that looks like this:
  *
  *    [extra_frame bytes] [mtu bytes] [extra_frame_bytes] [compression overflow bytes]
@@ -117,7 +117,12 @@ struct frame {
     int extra_tun;              /**< Maximum number of bytes in excess of
                                  *   the tun/tap MTU that might be read
                                  *   from or written to the virtual
-                                 *   tun/tap network interface. */
+                                 *   tun/tap network interface.
+                                 *
+                                 *   Only set with the option --tun-mtu-extra
+                                 *   which defaults to 0 for tun and 32
+                                 *   (\c TAP_MTU_EXTRA_DEFAULT) for tap.
+                                 *   */
 
     int extra_link;             /**< Maximum number of bytes in excess of
                                  *   external network interface's MTU that
@@ -177,11 +182,22 @@ struct options;
  * Control buffer headroom allocations to allow for efficient prepending.
  */
 #define FRAME_HEADROOM_BASE(f)     (TUN_LINK_DELTA(f) + (f)->extra_buffer + (f)->extra_link)
+/* Same as FRAME_HEADROOM_BASE but rounded up to next multiple of PAYLOAD_ALIGN */
 #define FRAME_HEADROOM(f)          frame_headroom(f)
 
 /*
  * Max size of a buffer used to build a packet for output to
  * the TCP/UDP port.
+ *
+ * the FRAME_HEADROOM_BASE(f) * 2 should not be necessary but it looks that at
+ * some point in the past we seem to have lost the information what parts of
+ * the extra space we need to have before the data and which we need after
+ * the data. So we ensure we have the FRAME_HEADROOM before and after the
+ * actual data.
+ *
+ * Most of our code only prepends headers but compression needs the extra bytes
+ * *after* the data as compressed data might end up larger than the original
+ * data (and max compression overhead is part of extra_buffer)
  */
 #define BUF_SIZE(f)              (TUN_MTU_SIZE(f) + FRAME_HEADROOM_BASE(f) * 2)
 
@@ -246,6 +262,8 @@ static inline int
 frame_headroom(const struct frame *f)
 {
     const int offset = FRAME_HEADROOM_BASE(f);
+    /* These two lines just pad offset to next multiple of PAYLOAD_ALIGN in
+     * a complicated and confusing way */
     const int delta = ((PAYLOAD_ALIGN << 24) - offset) & (PAYLOAD_ALIGN - 1);
     return offset + delta;
 }
Signature · 1 line
2.33.0
Acked-by on v1 5/21
#10 lore raw

Re: [Openvpn-devel] [PATCH 05/21] Document frame related function and variables a bit more

src/openvpn/mtu.h static inline int · line 267

Quoted 8 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:

src/openvpn/mtu.h +2 -0
@@ -246,6 +262,8 @@ static inline int
 frame_headroom(const struct frame *f)
 {
     const int offset = FRAME_HEADROOM_BASE(f);
+    /* These two lines just pad offset to next multiple of PAYLOAD_ALIGN in
+     * a complicated and confusing way */
     const int delta = ((PAYLOAD_ALIGN << 24) - offset) & (PAYLOAD_ALIGN - 1);

The 24 is completely arbitrary, right? As long as the result is always bigger than offset, you can use any other number.
I wonder whether it might be possible to replace it with something slightly less confusing (IMHO, anyway), like

delta = (PAYLOAD_ALIGN - (offset % PAYLOAD_ALIGN)) & (PAYLOAD_ALIGN - 1);

But my C-foo is not strong enough to judge potential corner cases.

Regards,
Frank

--
Frank Lichtenheld

#11 lore raw

[Openvpn-devel] [PATCH applied] Re: Document frame related function and variables a bit more

In reply to Arne Schwabe

Not tested in any way, as this is just comments, and Frank has reviewed them. I hope we'll see --tun-mtu-extra die later in this series... :-)

Your patch has been applied to the master branch.

commit 66c05aeabc7218e58266bd2bb39ddbd040030328

Author: Arne Schwabe
Date:   Tue Dec 7 18:01:55 2021 +0100
     Document frame related function and variables a bit more

     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]/msg23332.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#12 lore raw

PATCH 6/21 Remove post_open_mtu code

In reply to Arne Schwabe

This code is probably from a time when we could not set the MTU on the Windows tap6 driver. Nowadays we can set the MTU on this device, so this code is a noop now.

---
 src/openvpn/init.c | 10 ----------
 src/openvpn/ssl.c  |  2 +-
 src/openvpn/tun.c  |  1 -
 src/openvpn/tun.h  |  4 ----
 4 files changed, 1 insertion(+), 16 deletions(-)
src/openvpn/init.c +0 -10
index 0009bcb72..85d664ad6 100644
@@ -1809,16 +1809,6 @@ do_open_tun(struct context *c)
                  c->c1.tuntap, c->plugins, c->c2.es, &c->net_ctx);
     }
 
-    /*
-     * Did tun/tap driver give us an MTU?
-     */
-    if (c->c1.tuntap->post_open_mtu)
-    {
-        frame_set_mtu_dynamic(&c->c2.frame,
-                              c->c1.tuntap->post_open_mtu,
-                              SET_MTU_TUN | SET_MTU_UPPER_BOUND);
-    }
-
     ret = true;
     static_context = c;
 #ifndef TARGET_ANDROID
src/openvpn/ssl.c +1 -1
index 8cbb129d2..303e3fe8f 100644
@@ -1897,7 +1897,7 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
     /*
-     * mssfix uses data channel framing, which at this point contains
+     * mssfix uses data channel framing, which at this poipnt contains
      * actual overhead. Fragmentation logic uses frame_fragment, which
      * still contains worst case overhead. Replace it with actual overhead
      * to prevent unneeded fragmentation.
src/openvpn/tun.c +0 -1
index 75d5eaf7b..12bdd2005 100644
@@ -6071,7 +6071,6 @@ tuntap_get_mtu(struct tuntap *tt)
                         &mtu, sizeof(mtu),
                         &mtu, sizeof(mtu), &len, NULL))
     {
-        tt->post_open_mtu = (int)mtu;
         msg(D_MTU_INFO, "TAP-Windows MTU=%d", (int)mtu);
     }
 }
src/openvpn/tun.h +0 -4
index aa1e47b5a..d4657537c 100644
@@ -214,10 +214,6 @@ struct tuntap
 #endif
     /* used for printing status info only */
     unsigned int rwflags_debug;
-
-    /* Some TUN/TAP drivers like to be ioctled for mtu
-     * after open */
-    int post_open_mtu;
 };
 
 static inline bool
Signature · 1 line
2.33.0
#13 lore raw

Re: [Openvpn-devel] [PATCH 06/21] Remove post_open_mtu code

src/openvpn/ssl.c tls_session_update_crypto_params_do_work(struct tls_session *session, · line 1900

Quoted 13 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:

src/openvpn/ssl.c +1 -1
index 8cbb129d2..303e3fe8f 100644
@@ -1897,7 +1897,7 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
     /*
-     * mssfix uses data channel framing, which at this point contains
+     * mssfix uses data channel framing, which at this poipnt contains

Spurious change

Regards,
Frank
--
Frank Lichtenheld

#14 lore raw

Re: [Openvpn-devel] [PATCH 06/21] Remove post_open_mtu code

In reply to Arne Schwabe
Quoted 4 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:
This code is probably from a time when we could not set the MTU on the Windows tap6 driver. Nowadays we can set the MTU on this device, so this code is a noop now.

[...]

src/openvpn/tun.c tuntap_get_mtu(struct tuntap *tt) · line 6076

Quoted 12 lines
src/openvpn/tun.c +0 -1
index 75d5eaf7b..12bdd2005 100644
@@ -6071,7 +6071,6 @@ tuntap_get_mtu(struct tuntap *tt)
                         &mtu, sizeof(mtu),
                         &mtu, sizeof(mtu), &len, NULL))
     {
-        tt->post_open_mtu = (int)mtu;
         msg(D_MTU_INFO, "TAP-Windows MTU=%d", (int)mtu);
     }
 }

This change makes no sense to me. Why would you not remove the complete tuntap_get_mtu call? If the whole point was to get the MTU, and now it will be the one we set, why even to the ioctl at all?

Regards,
Frank
--
Frank Lichtenheld

#15 lore raw

Re: [Openvpn-devel] [PATCH 06/21] Remove post_open_mtu code

Am 10.12.21 um 17:17 schrieb Frank Lichtenheld:

Quoted 5 lines
Quoted 4 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:
This code is probably from a time when we could not set the MTU on the Windows tap6 driver. Nowadays we can set the MTU on this device, so this code is a noop now.

[...]

Quoted 14 lines

src/openvpn/tun.c tuntap_get_mtu(struct tuntap *tt) · line 6076

Quoted 12 lines
src/openvpn/tun.c +0 -1
index 75d5eaf7b..12bdd2005 100644
@@ -6071,7 +6071,6 @@ tuntap_get_mtu(struct tuntap *tt)
                          &mtu, sizeof(mtu),
                          &mtu, sizeof(mtu), &len, NULL))
      {
-        tt->post_open_mtu = (int)mtu;
          msg(D_MTU_INFO, "TAP-Windows MTU=%d", (int)mtu);
      }
  }

This change makes no sense to me. Why would you not remove the complete tuntap_get_mtu call? If the whole point was to get the MTU, and now it will be the one we set, why even to the ioctl at all?

I wanted to keep the message that is currently printed at verb 1. If we decide that the message can be removed I can remove the calling of the ioctl as well.

Arne

Acked-by on v1 6/21
#16 lore raw

[Openvpn-devel] [PATCH applied] Re: Remove post_open_mtu code

In reply to Arne Schwabe

I follow the reasoning - since we set the MTU now, we do not need to adjust the frame afterwards. Logging the MTU might be useful (Windows is weird, maybe it does not always "stick"...), so let's keep this in for the time being.

Spurious comment change in ssl.c removed from the patch.

I like the comment

/* Some TUN/TAP drivers like to be ioctled for mtu after open */

"like to be ioctled", hah :-) - but why this is there is lost lore from the long gone past...

I have test compiled this on mingw.

Your patch has been applied to the master branch.

commit c27868bfc2ad6ee21c28f4addb1c2fc003bfa61a

Author: Arne Schwabe
Date:   Tue Dec 7 18:01:56 2021 +0100
     Remove post_open_mtu code

     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]/msg23327.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#17 lore raw

PATCH 7/21 Add helper functions to calculate header/payload sizes

In reply to Arne Schwabe

These functions are intended to lay the groundwork to later replace the distributed frame calculations and centralise the calculation in one place.

---
 src/openvpn/crypto.c | 55 ++++++++++++++++++++++++++++++
 src/openvpn/crypto.h | 18 ++++++++++
 src/openvpn/mtu.c    | 80 ++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/mtu.h    | 54 ++++++++++++++++++++++++++++++
 4 files changed, 207 insertions(+)
src/openvpn/crypto.c +55 -0
index cd791ab8a..249c4212d 100644
@@ -667,6 +667,61 @@ openvpn_decrypt(struct buffer *buf, struct buffer work,
     return ret;
 }
 
+unsigned int
+calculate_crypto_overhead(const struct key_type *kt,
+                          bool packet_id,
+                          bool packet_id_long_form,
+                          unsigned int payload_size,
+                          bool occ)
+{
+    unsigned int crypto_overhead = 0;
+
+    /* No encryption */
+    if (packet_id)
+    {
+        crypto_overhead += packet_id_size(packet_id_long_form);
+    }
+
+    if (cipher_kt_mode_aead(kt->cipher))
+    {
+        /* For AEAD ciphers, we basically use a stream cipher/CTR for
+         * the encryption, so no overhead apart from the extra bytes
+         * we add */
+        crypto_overhead += cipher_kt_tag_size(kt->cipher);
+
+        if (occ)
+        {
+            /* the frame calculation of old clients adds these to the link-mtu
+             * even though they are not part of the actual packet */
+            crypto_overhead += cipher_kt_iv_size(kt->cipher);
+            crypto_overhead += cipher_kt_block_size(kt->cipher);
+        }
+    }
+    else
+    {
+        if (cipher_defined(kt->cipher))
+        {
+            /* CBC, OFB or CFB mode */
+            /* This is a worst case upper bound of needing to add
+             * a full extra block for padding when the payload
+             * is exactly a multiple of the block size */
+            if (occ || (cipher_kt_mode_cbc(kt->cipher) &&
+                (payload_size % cipher_kt_block_size(kt->cipher) == 0)))
+            {
+                crypto_overhead += cipher_kt_block_size(kt->cipher);
+            }
+            /* IV is always added (no-iv has been removed a while ago) */
+            crypto_overhead += cipher_kt_iv_size(kt->cipher);
+        }
+        if (md_defined(kt->digest))
+        {
+            crypto_overhead += md_kt_size(kt->digest);
+        }
+    }
+
+    return crypto_overhead;
+}
+
 void
 crypto_adjust_frame_parameters(struct frame *frame,
                                const struct key_type *kt,
src/openvpn/crypto.h +18 -0
index ad3543c1c..5a67b7ac1 100644
@@ -415,6 +415,24 @@ void crypto_adjust_frame_parameters(struct frame *frame,
                                     bool packet_id,
                                     bool packet_id_long_form);
 
+/** Calculate the maximum overhead that our encryption has
+ * on a packet. This does not include needed additional buffer size
+ *
+ * @param kt            Struct with the crypto algorithm to use
+ * @param packet_id     Whether packet_id is used
+ * @param packet_id_long_form Whether the packet id has the long form
+ * @param payload_size   payload size, only used if occ is false
+ * @param occ           if true calculates the overhead for crypto in the same
+ *                      incorrect way as all previous OpenVPN versions did, to
+ *                      end up with identical numbers for OCC compatibility
+ */
+unsigned int
+calculate_crypto_overhead(const struct key_type *kt,
+                           bool packet_id,
+                           bool packet_id_long_form,
+                           unsigned int payload_size,
+                           bool occ);
+
 /** Return the worst-case OpenVPN crypto overhead (in bytes) */
 unsigned int crypto_max_overhead(void);
 
src/openvpn/mtu.c +80 -0

92 lines not on this page · raw message

src/openvpn/mtu.h +54 -0

60 lines not on this page · raw message

Signature · 1 line
2.33.0
#18 lore raw

Re: [Openvpn-devel] [PATCH 07/21] Add helper functions to calculate header/payload sizes

Quoted 3 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:

[...]

Quoted 4 lines
src/openvpn/mtu.c +0 -0
index 0ab716d7a..25b943722 100644

[...]

Quoted 4 lines
src/openvpn/mtu.c +0 -0
@@ -51,6 +52,85 @@ alloc_buf_sock_tun(struct buffer *buf,
     ASSERT(buf_safe(buf, 0));
 }
 

src/openvpn/mtu.c alloc_buf_sock_tun(struct buffer *buf, · line 64

Quoted 10 lines
src/openvpn/mtu.c +10 -0
+size_t
+frame_calculate_protocol_header_size(const struct key_type *kt,
+                                     const struct options *options,
+                                     unsigned int payload_size,
+                                     bool occ)
+{
+    /* Sum of all the overhead that reduces the usable packet size */
+    size_t header_size = 0;
+
+    /* A socks proxy adds 10 byte of extra header to each packet */

This is only true since we do not support IPv6 UDP over socks. Would it be worth to point that out?

Quoted 10 lines
src/openvpn/mtu.c +10 -0
+    if (options->ce.socks_proxy_server && proto_is_udp(options->ce.proto))
+    {
+        header_size += 10;
+    }
+
+    /* TCP stream based packets have a 16 bit length field */
+    if (proto_is_tcp(options->ce.proto))
+    {
+        header_size += 2;
+    }

src/openvpn/mtu.c alloc_buf_sock_tun(struct buffer *buf, · line 84

Quoted 10 lines
src/openvpn/mtu.c +10 -0
+
+    /* Add the opcode and peerid */
+    header_size += options->use_peer_id ? 4 : 1;
+
+    /* Add the crypto overhead */
+    bool packet_id = options->replay;
+    bool packet_id_long_form = !tlsmode || cipher_kt_mode_ofb_cfb(kt->cipher);
+
+    /* For figuring out the crypto overhead, we need to use the real payload
+     * including all extra headers that also get encrypted */

That comment sounds like header size should somehow be a input into calculate_crypto_overhead, which it isn't. Is there an error here or do I just misunderstand the comment?

Quoted 26 lines
src/openvpn/mtu.c +26 -0
+    header_size += calculate_crypto_overhead(kt, packet_id,
+                                             packet_id_long_form,
+                                             payload_size, occ);
+    return header_size;
+}
+
+
+size_t
+frame_calculate_payload_overhead(const struct frame *frame,
+                                 const struct options *options,
+                                 bool extra_tun)
+{
+    size_t overhead = 0;
+
+    /* This is the overhead of tap device that is not included in the MTU itself
+     * i.e. Ethernet header that we still need to transmit as part of the
+     * payload*/
+    if (extra_tun)
+    {
+        overhead += frame->extra_tun;
+    }
+
+#if defined(USE_COMP)
+    /* v1 Compression schemes add 1 byte header. V2 only adds a header when it
+     * does not increase the packet length. We ignore the unlikely escaping
+     * for tap here */

src/openvpn/mtu.c alloc_buf_sock_tun(struct buffer *buf, · line 120

Quoted 10 lines
src/openvpn/mtu.c +10 -0
+    if (options->comp.alg == COMP_ALG_LZ4 || options->comp.alg == COMP_ALG_STUB
+        || options->comp.alg == COMP_ALG_LZO)
+    {
+        overhead += 1;
+    }
+#endif
+#if defined(ENABLE_FRAGMENT)
+    if (options->ce.fragment)
+    {
+        overhead += 4;

Every other statement has an explanation of the number, except this one. Maybe you could add one here as well?

Quoted 6 lines
src/openvpn/mtu.c +6 -0
+    }
+#endif
+    return overhead;
+}
+
+size_t

src/openvpn/mtu.c alloc_buf_sock_tun(struct buffer *buf, · line 136

Quoted 10 lines
src/openvpn/mtu.c +7 -0
+frame_calculate_payload_size(const struct frame *frame, const struct options *options)
+{
+    size_t payload_size = options->ce.tun_mtu;
+    payload_size += frame_calculate_payload_overhead(frame, options, true);
+    return payload_size;
+}
+
 void
 frame_finalize(struct frame *frame,
                bool link_mtu_defined,

Regards,
Frank

--
Frank Lichtenheld

#19 lore raw

PATCH 8/21 Decouple MSS fix calculation from frame calculation

In reply to Arne Schwabe

This consolidates the MSS fix calculation into a single function instead having it distributed all over the code. It also calculates the real wire overhead without extra sizes for buffer etc.

---
 src/openvpn/forward.c |  5 ++---
 src/openvpn/init.c    |  3 ++-
 src/openvpn/mss.c     | 40 ++++++++++++++++++++++++++++++++++++++++
 src/openvpn/mss.h     |  6 ++++++
 src/openvpn/mtu.c     |  9 ---------
 src/openvpn/mtu.h     | 10 ++++++----
 src/openvpn/proto.h   | 11 -----------
 src/openvpn/ssl.c     |  3 ++-
 8 files changed, 58 insertions(+), 29 deletions(-)
src/openvpn/forward.c +2 -3
index 29efcd3b9..f82386a1d 100644
@@ -1493,7 +1493,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
                 /* possibly alter the TCP MSS */
                 if (flags & PIP_MSSFIX)
                 {
-                    mss_fixup_ipv4(&ipbuf, MTU_TO_MSS(TUN_MTU_SIZE_DYNAMIC(&c->c2.frame)));
+                    mss_fixup_ipv4(&ipbuf, c->c2.frame.mss_fix);
                 }
 
                 /* possibly do NAT on packet */
@@ -1517,8 +1517,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
                 /* possibly alter the TCP MSS */
                 if (flags & PIP_MSSFIX)
                 {
-                    mss_fixup_ipv6(&ipbuf,
-                                   MTU_TO_MSS(TUN_MTU_SIZE_DYNAMIC(&c->c2.frame)));
+                    mss_fixup_ipv6(&ipbuf, c->c2.frame.mss_fix);
                 }
                 if (!(flags & PIP_OUTGOING) && (flags
                                                 &(PIPV6_IMCP_NOHOST_CLIENT | PIPV6_IMCP_NOHOST_SERVER)))
src/openvpn/init.c +2 -1
index 85d664ad6..b22ce60af 100644
@@ -53,6 +53,7 @@
 #include "tls_crypt.h"
 #include "forward.h"
 #include "auth_token.h"
+#include "mss.h"
 
 #include "memdbg.h"
 
@@ -4156,7 +4157,7 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
 #endif
 
     /* initialize dynamic MTU variable */
-    frame_init_mssfix(&c->c2.frame, &c->options);
+    frame_calculate_mssfix(&c->c2.frame, &c->c1.ks.key_type, &c->options);
 
     /* bind the TCP/UDP socket */
     if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
src/openvpn/mss.c +40 -0
index aa5b68ce9..56dea0292 100644
@@ -30,6 +30,8 @@
 #include "syshead.h"
 #include "error.h"
 #include "mss.h"
+#include "crypto.h"
+#include "ssl_common.h"
 #include "memdbg.h"
 
 /*
@@ -204,3 +206,41 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss)
         }
     }
 }
+
+void
+frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
+                       const struct options *options)
+{
+    if (options->ce.mssfix == 0)
+    {
+        return;
+    }
+
+    unsigned int payload_size;
+    unsigned int overhead;
+
+
+    payload_size = frame_calculate_payload_size(frame, options);
+
+    overhead = frame_calculate_protocol_header_size(kt, options,
+                                                    payload_size, false);
+
+    /* Calculate the number of bytes that the payload differs from the payload
+     * MTU. This are fragment/compression/ethernet headers */
+    unsigned payload_overhead = frame_calculate_payload_overhead(frame, options, true);
+
+    /* We are in a "liberal" position with respect to MSS,
+     * i.e. we assume that MSS can be calculated from MTU
+     * by subtracting out only the IP and TCP header sizes
+     * without options.
+     *
+     * (RFC 879, section 7). */
+
+    /* Add 20 bytes for the IPv4 header and TCP header of the payload,
+     * the mssfix routes will add 20 extra if payload is IPv6 */
+    overhead += 20 + 20;
+
+    /* Calculate the maximum MSS value from the max link layer size specified
+     * by ce.mssfix */
+    frame->mss_fix = options->ce.mssfix - overhead - payload_overhead;
+}
\ No newline at end of file
src/openvpn/mss.h +6 -0
index 41254e2a6..856f4c4e3 100644
@@ -26,6 +26,8 @@
 
 #include "proto.h"
 #include "error.h"
+#include "mtu.h"
+#include "ssl_common.h"
 
 void mss_fixup_ipv4(struct buffer *buf, int maxmss);
 
@@ -33,4 +35,8 @@ void mss_fixup_ipv6(struct buffer *buf, int maxmss);
 
 void mss_fixup_dowork(struct buffer *buf, uint16_t maxmss);
 
+/** Set the --mssfix option. */
+void frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
+                            const struct options *options);
+
 #endif
src/openvpn/mtu.c +0 -9
index 25b943722..e7ff477cd 100644
@@ -205,15 +205,6 @@ frame_subtract_extra(struct frame *frame, const struct frame *src)
     frame->extra_tun   += src->extra_frame;
 }
 
-void
-frame_init_mssfix(struct frame *frame, const struct options *options)
-{
-    if (options->ce.mssfix)
-    {
-        frame_set_mtu_dynamic(frame, options->ce.mssfix, SET_MTU_UPPER_BOUND);
-    }
-}
-
 void
 frame_print(const struct frame *frame,
             int level,
src/openvpn/mtu.h +6 -4
index 5ad0931fd..ae83d3e7a 100644
@@ -94,6 +94,12 @@ struct frame {
     int link_mtu;               /**< Maximum packet size to be sent over
                                  *   the external network interface. */
 
+    unsigned int mss_fix;      /**< The actual MSS value that should be
+                                 *   written to the payload packets. This
+                                 *   is the value for IPv4 TCP packets. For
+                                 *   IPv6 packets another 20 bytes must
+                                 *   be subtracted */
+
     int link_mtu_dynamic;       /**< Dynamic MTU value for the external
                                  *   network interface. */
 
@@ -152,7 +158,6 @@ struct options;
  * This is the size to "ifconfig" the tun or tap device.
  */
 #define TUN_MTU_SIZE(f)          ((f)->link_mtu - TUN_LINK_DELTA(f))
-#define TUN_MTU_SIZE_DYNAMIC(f)  ((f)->link_mtu_dynamic - TUN_LINK_DELTA(f))
 
 /*
  * This is the maximum packet size that we need to be able to
@@ -291,9 +296,6 @@ void alloc_buf_sock_tun(struct buffer *buf,
                         const struct frame *frame,
                         const bool tuntap_buffer);
 
-/** Set the --mssfix option. */
-void frame_init_mssfix(struct frame *frame, const struct options *options);
-
 /*
  * EXTENDED_SOCKET_ERROR_CAPABILITY functions -- print extra error info
  * on socket errors, such as PMTU size.  As of 2003.05.11, only works
src/openvpn/proto.h +0 -11
index f73e50c07..94010a98f 100644
@@ -247,17 +247,6 @@ struct ip_tcp_udp_hdr {
         acc -= (u32) >> 16;    \
 }
 
-/*
- * We are in a "liberal" position with respect to MSS,
- * i.e. we assume that MSS can be calculated from MTU
- * by subtracting out only the IP and TCP header sizes
- * without options.
- *
- * (RFC 879, section 7).
- */
-#define MTU_TO_MSS(mtu) (mtu - sizeof(struct openvpn_iphdr) \
-                         - sizeof(struct openvpn_tcphdr))
-
 /*
  * This returns an ip protocol version of packet inside tun
  * and offset of IP header (via parameter).
src/openvpn/ssl.c +2 -1
index 303e3fe8f..608b30110 100644
@@ -62,6 +62,7 @@
 #include "ssl_ncp.h"
 #include "ssl_util.h"
 #include "auth_token.h"
+#include "mss.h"
 
 #include "memdbg.h"
 
@@ -1893,7 +1894,7 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
                                    options->replay, packet_id_long_form);
     frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,
                    options->ce.tun_mtu_defined, options->ce.tun_mtu);
-    frame_init_mssfix(frame, options);
+    frame_calculate_mssfix(frame, &session->opt->key_type, options);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
     /*
Signature · 1 line
2.33.0
#20 lore raw

Re: [Openvpn-devel] [PATCH 08/21] Decouple MSS fix calculation from frame calculation

Quoted 3 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:

[...]

Quoted 38 lines
src/openvpn/mss.c +23 -0
index aa5b68ce9..56dea0292 100644
@@ -30,6 +30,8 @@
 #include "syshead.h"
 #include "error.h"
 #include "mss.h"
+#include "crypto.h"
+#include "ssl_common.h"
 #include "memdbg.h"
 
 /*
@@ -204,3 +206,41 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss)
         }
     }
 }
+
+void
+frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
+                       const struct options *options)
+{
+    if (options->ce.mssfix == 0)
+    {
+        return;
+    }
+
+    unsigned int payload_size;
+    unsigned int overhead;
+
+
+    payload_size = frame_calculate_payload_size(frame, options);
+
+    overhead = frame_calculate_protocol_header_size(kt, options,
+                                                    payload_size, false);
+
+    /* Calculate the number of bytes that the payload differs from the payload
+     * MTU. This are fragment/compression/ethernet headers */

src/openvpn/mss.c mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) · line 239

Quoted 10 lines
src/openvpn/mss.c +10 -0
+    unsigned payload_overhead = frame_calculate_payload_overhead(frame, options, true);
+
+    /* We are in a "liberal" position with respect to MSS,
+     * i.e. we assume that MSS can be calculated from MTU
+     * by subtracting out only the IP and TCP header sizes
+     * without options.
+     *
+     * (RFC 879, section 7). */
+
+    /* Add 20 bytes for the IPv4 header and TCP header of the payload,

"20 bytes each" would make the sentence much easier to parse, IMHO.

src/openvpn/mss.c mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) · line 246

Quoted 7 lines
src/openvpn/mss.c +7 -0
+     * the mssfix routes will add 20 extra if payload is IPv6 */
+    overhead += 20 + 20;
+
+    /* Calculate the maximum MSS value from the max link layer size specified
+     * by ce.mssfix */
+    frame->mss_fix = options->ce.mssfix - overhead - payload_overhead;
+}

Regards,
Frank
--
Frank Lichtenheld

#21 lore raw

Re: [Openvpn-devel] [PATCH 08/21] Decouple MSS fix calculation from frame calculation

In reply to Arne Schwabe
Quoted 2 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:

[...]

Quoted 4 lines
src/openvpn/mss.c +0 -0
index aa5b68ce9..56dea0292 100644

[...]

src/openvpn/mss.c mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) · line 217

Quoted 13 lines
src/openvpn/mss.c +9 -0
@@ -204,3 +206,41 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss)
         }
     }
 }
+
+void
+frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
+                       const struct options *options)
+{
+    if (options->ce.mssfix == 0)
+    {
+        return;
+    }

Wouldn't an ASSERT be better here? This essentially should never happen because ce.mssfix is initialized to MSSFIX_DEFAULT, anyway. If you handle this without an error here it looks like this will just explode later in mss_fixup_dowork. An ASSERT might be easier and cleaner.

Quoted 19 lines
src/openvpn/mss.c +19 -0
+    unsigned int payload_size;
+    unsigned int overhead;
+
+
+    payload_size = frame_calculate_payload_size(frame, options);
+
+    overhead = frame_calculate_protocol_header_size(kt, options,
+                                                    payload_size, false);
+
+    /* Calculate the number of bytes that the payload differs from the payload
+     * MTU. This are fragment/compression/ethernet headers */
+    unsigned payload_overhead = frame_calculate_payload_overhead(frame, options, true);
+
+    /* We are in a "liberal" position with respect to MSS,
+     * i.e. we assume that MSS can be calculated from MTU
+     * by subtracting out only the IP and TCP header sizes
+     * without options.
+     *
+     * (RFC 879, section 7). */

src/openvpn/mss.c mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) · line 246

Quoted 10 lines
src/openvpn/mss.c +9 -0
+
+    /* Add 20 bytes for the IPv4 header and TCP header of the payload,
+     * the mssfix routes will add 20 extra if payload is IPv6 */
+    overhead += 20 + 20;
+
+    /* Calculate the maximum MSS value from the max link layer size specified
+     * by ce.mssfix */
+    frame->mss_fix = options->ce.mssfix - overhead - payload_overhead;
+}
\ No newline at end of file

Regards,
--
Frank Lichtenheld

#22 lore raw

Re: [Openvpn-devel] [PATCH 08/21] Decouple MSS fix calculation from frame calculation

Am 13.12.21 um 13:35 schrieb Frank Lichtenheld:

Quoted 17 lines
Quoted 2 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:

[...]

Quoted 4 lines
src/openvpn/mss.c +0 -0
index aa5b68ce9..56dea0292 100644

[...]

Quoted 9 lines
src/openvpn/mss.c +5 -0
@@ -204,3 +206,41 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss)
          }
      }
  }
+
+void
+frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
+                       const struct options *options)
+{
Quoted 8 lines

src/openvpn/mss.c mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) · line 217

Quoted 4 lines
src/openvpn/mss.c +4 -0
+    if (options->ce.mssfix == 0)
+    {
+        return;
+    }

Wouldn't an ASSERT be better here? This essentially should never happen because ce.mssfix is initialized to MSSFIX_DEFAULT, anyway. If you handle this without an error here it looks like this will just explode later in mss_fixup_dowork. An ASSERT might be easier and cleaner.

--mssfix 0 can be used to disable MSSFIX but I broke that in this patch. So basically that test is testing if mssfix is enabled.

Arne

#23 lore raw

PATCH 9/21 Rework occ link-mtu calculation

In reply to Arne Schwabe

Use the functions that directly compute the link mtu instead relying on the frame logic.

---
 src/openvpn/mtu.c                      |  50 +++++++++-
 src/openvpn/mtu.h                      |  11 +++
 src/openvpn/options.c                  |  51 ----------
 tests/unit_tests/openvpn/Makefile.am   |   6 +-
 tests/unit_tests/openvpn/test_crypto.c | 128 ++++++++++++++++++++++++-
 tests/unit_tests/openvpn/test_misc.c   |   1 +
 6 files changed, 192 insertions(+), 55 deletions(-)
src/openvpn/mtu.c +49 -1
index e7ff477cd..c7f69bb2a 100644
@@ -61,6 +61,8 @@ frame_calculate_protocol_header_size(const struct key_type *kt,
     /* Sum of all the overhead that reduces the usable packet size */
     size_t header_size = 0;
 
+    bool tlsmode = options->tls_server || options->tls_client;
+
     /* A socks proxy adds 10 byte of extra header to each packet */
     if (options->ce.socks_proxy_server && proto_is_udp(options->ce.proto))
     {
@@ -74,7 +76,10 @@ frame_calculate_protocol_header_size(const struct key_type *kt,
     }
 
     /* Add the opcode and peerid */
-    header_size += options->use_peer_id ? 4 : 1;
+    if (tlsmode)
+    {
+        header_size += options->use_peer_id ? 4 : 1;
+    }
 
     /* Add the crypto overhead */
     bool packet_id = options->replay;
@@ -131,6 +136,49 @@ frame_calculate_payload_size(const struct frame *frame, const struct options *op
     return payload_size;
 }
 
+size_t
+calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
+{
+    unsigned int payload = frame_calculate_payload_size(frame, o);
+
+    /* neither --secret nor TLS mode */
+    if (!o->tls_client && !o->tls_server && !o->shared_secret_file)
+    {
+        return payload;
+    }
+
+    struct key_type occ_kt;
+
+    /* o->ciphername might be BF-CBC even though the underlying SSL library
+     * does not support it. For this reason we workaround this corner case
+     * by pretending to have no encryption enabled and by manually adding
+     * the required packet overhead to the MTU computation.
+     */
+    const char* ciphername = o->ciphername;
+
+    unsigned int overhead = 0;
+
+    if (strcmp(o->ciphername, "BF-CBC") == 0)
+    {
+        /* none has no overhead, so use this to later add only --auth
+         * overhead */
+
+        /* overhead of BF-CBC: 64 bit block size, 64 bit IV size */
+        overhead += 64/8 + 64/8;
+        /* set ciphername to none, so its size does get added in the
+         * fake_kt and the cipher is not tried to be resolved */
+        ciphername = "none";
+    }
+
+    /* We pass tlsmode always true here since as we do not need to check if
+     * the ciphers are actually valid for non tls in occ calucation */
+    init_key_type(&occ_kt, ciphername, o->authname, true, false);
+
+    overhead += frame_calculate_protocol_header_size(&occ_kt, o, 0, true);
+
+    return payload + overhead;
+}
+
 void
 frame_finalize(struct frame *frame,
                bool link_mtu_defined,
src/openvpn/mtu.h +11 -0
index ae83d3e7a..f60138607 100644
@@ -280,6 +280,17 @@ frame_calculate_protocol_header_size(const struct key_type *kt,
                                      unsigned int payload_size,
                                      bool occ);
 
+/**
+ * Calculate the link-mtu to advertise to our peer.  The actual value is not
+ * relevant, because we will possibly perform data channel cipher negotiation
+ * after this, but older clients will log warnings if we do not supply them the
+ * value they expect.  This assumes that the traditional cipher/auth directives
+ * in the config match the config of the peer.
+ */
+size_t
+calc_options_string_link_mtu(const struct options *options,
+                             const struct frame *frame);
+
 /*
  * frame_set_mtu_dynamic and flags
  */
src/openvpn/options.c +0 -51

57 lines not on this page · raw message

tests/unit_tests/openvpn/Makefile.am +4 -2
index 44b77cc5d..f681b353c 100644
@@ -46,7 +46,9 @@ crypto_testdriver_SOURCES = test_crypto.c mock_msg.c mock_msg.h \
 	$(openvpn_srcdir)/crypto_openssl.c \
 	$(openvpn_srcdir)/otime.c \
 	$(openvpn_srcdir)/packet_id.c \
-	$(openvpn_srcdir)/platform.c
+	$(openvpn_srcdir)/platform.c \
+	$(openvpn_srcdir)/mtu.c \
+	$(openvpn_srcdir)/mss.c
 
 packet_id_testdriver_CFLAGS  = @TEST_CFLAGS@ \
 	-I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir)
@@ -137,4 +139,4 @@ misc_testdriver_SOURCES = test_misc.c mock_msg.c \
     mock_get_random.c \
 	$(openvpn_srcdir)/buffer.c \
 	$(openvpn_srcdir)/ssl_util.c \
-	$(openvpn_srcdir)/platform.c
\ No newline at end of file
+	$(openvpn_srcdir)/platform.c
tests/unit_tests/openvpn/test_crypto.c +127 -1

146 lines not on this page · raw message

tests/unit_tests/openvpn/test_misc.c +1 -0
index 70f726d0f..867fa1bb5 100644
@@ -37,6 +37,7 @@
 #include <cmocka.h>
 
 #include "ssl_util.h"
+#include "options.h"
 
 static void
 test_compat_lzo_string(void **state)
Signature · 1 line
2.33.0
#24 lore raw

Re: [Openvpn-devel] [PATCH 09/21] Rework occ link-mtu calculation

Quoted 7 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:01 geschrieben:

Use the functions that directly compute the link mtu instead relying on the frame logic.

Is the assumption that the old and new version of the calc_options_string_link_mtu functions should return the same values for the same configuration? I tried to actually test that with the UT, but they work so differently that it is difficult to adapt the test. So before going further down that rabbit hole I wanted to ask whether there is any sense in that or whether it is okay if they differ in the value calculated.

Regards,
--
Frank Lichtenheld

#25 lore raw

Re: [Openvpn-devel] [PATCH 09/21] Rework occ link-mtu calculation

In reply to Arne Schwabe

Hi,

Quoted 2 lines · Arne Schwabe

Use the functions that directly compute the link mtu instead relying on the frame logic.

[..]

Quoted 12 lines
src/openvpn/mtu.c +2 -0
@@ -61,6 +61,8 @@ frame_calculate_protocol_header_size(const struct key_type *kt,
     /* Sum of all the overhead that reduces the usable packet size */
     size_t header_size = 0;
 
+    bool tlsmode = options->tls_server || options->tls_client;
+
     /* A socks proxy adds 10 byte of extra header to each packet */
     if (options->ce.socks_proxy_server && proto_is_udp(options->ce.proto))
     {

src/openvpn/mtu.c frame_calculate_protocol_header_size(const struct key_type *kt, · line 82

Quoted 8 lines
src/openvpn/mtu.c +4 -1
@@ -74,7 +76,10 @@ frame_calculate_protocol_header_size(const struct key_type *kt,
     }
 
     /* Add the opcode and peerid */
-    header_size += options->use_peer_id ? 4 : 1;
+    if (tlsmode)
+    {
+        header_size += options->use_peer_id ? 4 : 1;
+    }

We need a v2 of this - these two hunks are already in master.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#26 lore raw

PATCH 10/21 Change buffer allocation calculation and checks to be more static

In reply to Arne Schwabe

Currently we use half dynamic buffer sizes where we use have a fixed overhead for crypto (crypto_max_overhead) but use a dynamic overhead for the the other small header sizes.

---
 src/openvpn/comp-lz4.c   |   4 +-
 src/openvpn/crypto.c     |   4 +-
 src/openvpn/forward.c    |   8 +--
 src/openvpn/init.c       | 109 +++++++++++++++++++++++++++++++++++----
 src/openvpn/init.h       |   2 +-
 src/openvpn/lzo.c        |   2 +-
 src/openvpn/mss.c        |   4 +-
 src/openvpn/mtu.c        |   7 ++-
 src/openvpn/mtu.h        |  74 +++++++++++++-------------
 src/openvpn/multi.c      |   4 +-
 src/openvpn/multi.h      |   2 +-
 src/openvpn/occ.c        |   4 +-
 src/openvpn/options.c    |   2 +-
 src/openvpn/ping.c       |   2 +-
 src/openvpn/ssl.c        |  35 ++++++++++---
 src/openvpn/ssl_common.h |   3 +-
 16 files changed, 191 insertions(+), 75 deletions(-)
src/openvpn/comp-lz4.c +2 -2
index bceca5e2c..aa83ea80f 100644
@@ -213,7 +213,7 @@ lz4_decompress(struct buffer *buf, struct buffer work,
                struct compress_context *compctx,
                const struct frame *frame)
 {
-    size_t zlen_max = EXPANDED_SIZE(frame);
+    size_t zlen_max = frame->buf.payload_size;
     uint8_t c;          /* flag indicating whether or not our peer compressed */
 
     if (buf->len <= 0)
@@ -250,7 +250,7 @@ lz4v2_decompress(struct buffer *buf, struct buffer work,
                  struct compress_context *compctx,
                  const struct frame *frame)
 {
-    size_t zlen_max = EXPANDED_SIZE(frame);
+    size_t zlen_max = frame->buf.payload_size;
     uint8_t c;          /* flag indicating whether or not our peer compressed */
 
     if (buf->len <= 0)
src/openvpn/crypto.c +2 -2
index 249c4212d..b4b8ca54b 100644
@@ -1070,7 +1070,7 @@ test_crypto(struct crypto_options *co, struct frame *frame)
 {
     int i, j;
     struct gc_arena gc = gc_new();
-    struct buffer src = alloc_buf_gc(TUN_MTU_SIZE(frame), &gc);
+    struct buffer src = alloc_buf_gc(frame->buf.payload_size, &gc);
     struct buffer work = alloc_buf_gc(BUF_SIZE(frame), &gc);
     struct buffer encrypt_workspace = alloc_buf_gc(BUF_SIZE(frame), &gc);
     struct buffer decrypt_workspace = alloc_buf_gc(BUF_SIZE(frame), &gc);
@@ -1101,7 +1101,7 @@ test_crypto(struct crypto_options *co, struct frame *frame)
     }
 
     msg(M_INFO, "Entering " PACKAGE_NAME " crypto self-test mode.");
-    for (i = 1; i <= TUN_MTU_SIZE(frame); ++i)
+    for (i = 1; i <= frame->buf.payload_size; ++i)
     {
         update_time();
 
src/openvpn/forward.c +4 -4
index f82386a1d..c971c6bdb 100644
@@ -1119,8 +1119,8 @@ read_incoming_tun(struct context *c)
     }
 #else  /* ifdef _WIN32 */
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
-    ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
-    c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf), MAX_RW_SIZE_TUN(&c->c2.frame));
+    ASSERT(buf_safe(&c->c2.buf, c->c2.frame.buf.payload_size));
+    c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf), c->c2.frame.buf.payload_size);
 #endif /* ifdef _WIN32 */
 
 #ifdef PACKET_TRUNCATION_CHECK
@@ -1709,7 +1709,7 @@ process_outgoing_tun(struct context *c)
                       PIP_MSSFIX | PIPV4_EXTRACT_DHCP_ROUTER | PIPV4_CLIENT_NAT | PIP_OUTGOING,
                       &c->c2.to_tun);
 
-    if (c->c2.to_tun.len <= MAX_RW_SIZE_TUN(&c->c2.frame))
+    if (c->c2.to_tun.len <= c->c2.frame.buf.payload_size)
     {
         /*
          * Write to TUN/TAP device.
@@ -1769,7 +1769,7 @@ process_outgoing_tun(struct context *c)
          */
         msg(D_LINK_ERRORS, "tun packet too large on write (tried=%d,max=%d)",
             c->c2.to_tun.len,
-            MAX_RW_SIZE_TUN(&c->c2.frame));
+            c->c2.frame.buf.payload_size);
     }
 
     buf_reset(&c->c2.to_tun);
src/openvpn/init.c +98 -11

146 lines not on this page · raw message

src/openvpn/init.h +1 -1
index 52581f8ae..cc80fefee 100644
@@ -110,7 +110,7 @@ void inherit_context_top(struct context *dest,
 
 void close_context(struct context *c, int sig, unsigned int flags);
 
-struct context_buffers *init_context_buffers(const struct frame *frame);
+struct context_buffers *init_context_buffers(struct frame *frame);
 
 void free_context_buffers(struct context_buffers *b);
 
src/openvpn/lzo.c +1 -1
index 8d572684a..e7e89655f 100644
@@ -213,7 +213,7 @@ lzo_decompress(struct buffer *buf, struct buffer work,
                struct compress_context *compctx,
                const struct frame *frame)
 {
-    lzo_uint zlen = EXPANDED_SIZE(frame);
+    lzo_uint zlen = frame->buf.payload_size;
     int err;
     uint8_t c;          /* flag indicating whether or not our peer compressed */
 
src/openvpn/mss.c +2 -2
index 56dea0292..e4311c42a 100644
@@ -222,8 +222,8 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
 
     payload_size = frame_calculate_payload_size(frame, options);
 
-    overhead = frame_calculate_protocol_header_size(kt, options,
-                                                    payload_size, false);
+    overhead = frame_calculate_protocol_header_size(kt, options, payload_size,
+                                                    false);
 
     /* Calculate the number of bytes that the payload differs from the payload
      * MTU. This are fragment/compression/ethernet headers */
src/openvpn/mtu.c +6 -1
index c7f69bb2a..88a42a0c5 100644
@@ -48,7 +48,7 @@ alloc_buf_sock_tun(struct buffer *buf,
     /* allocate buffer for overlapped I/O */
     *buf = alloc_buf(BUF_SIZE(frame));
     ASSERT(buf_init(buf, FRAME_HEADROOM(frame)));
-    buf->len = tuntap_buffer ? MAX_RW_SIZE_TUN(frame) : MAX_RW_SIZE_LINK(frame);
+    buf->len = frame->buf.payload_size;
     ASSERT(buf_safe(buf, 0));
 }
 
@@ -265,6 +265,11 @@ frame_print(const struct frame *frame,
         buf_printf(&out, "%s ", prefix);
     }
     buf_printf(&out, "[");
+    buf_printf(&out, " mss_fix:%d", frame->mss_fix);
+    buf_printf(&out, " tun_mtu:%d", frame->tun_mtu);
+    buf_printf(&out, " headroom:%d", frame->buf.headroom);
+    buf_printf(&out, " payload:%d", frame->buf.payload_size);
+    buf_printf(&out, " tailroom:%d", frame->buf.tailroom);
     buf_printf(&out, " L:%d", frame->link_mtu);
     buf_printf(&out, " D:%d", frame->link_mtu_dynamic);
     buf_printf(&out, " EF:%d", frame->extra_frame);
src/openvpn/mtu.h +39 -35

121 lines not on this page · raw message

src/openvpn/multi.c +2 -2
index 103e882e3..e5ffebff2 100644
@@ -3495,7 +3495,7 @@ gremlin_flood_clients(struct multi_context *m)
         int i;
 
         ASSERT(buf_init(&buf, FRAME_HEADROOM(&m->top.c2.frame)));
-        parm.packet_size = min_int(parm.packet_size, MAX_RW_SIZE_TUN(&m->top.c2.frame));
+        parm.packet_size = min_int(parm.packet_size, m->top.c2.frame.buf.payload_size);
 
         msg(D_GREMLIN, "GREMLIN_FLOOD_CLIENTS: flooding clients with %d packets of size %d",
             parm.n_packets,
@@ -3557,7 +3557,7 @@ multi_process_per_second_timers_dowork(struct multi_context *m)
 }
 
 void
-multi_top_init(struct multi_context *m, const struct context *top)
+multi_top_init(struct multi_context *m, struct context *top)
 {
     inherit_context_top(&m->top, top);
     m->top.c2.buffers = init_context_buffers(&top->c2.frame);
src/openvpn/multi.h +1 -1
index 6e85c21c9..c2b085e32 100644
@@ -257,7 +257,7 @@ void multi_init(struct multi_context *m, struct context *t, bool tcp_mode);
 
 void multi_uninit(struct multi_context *m);
 
-void multi_top_init(struct multi_context *m, const struct context *top);
+void multi_top_init(struct multi_context *m, struct context *top);
 
 void multi_top_free(struct multi_context *m);
 
src/openvpn/occ.c +2 -2
index 610c05f5f..c4e7c1be2 100644
@@ -219,7 +219,7 @@ check_send_occ_msg_dowork(struct context *c)
 
     c->c2.buf = c->c2.buffers->aux_buf;
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
-    ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
+    ASSERT(buf_safe(&c->c2.buf, c->c2.frame.buf.payload_size));
     ASSERT(buf_write(&c->c2.buf, occ_magic, OCC_STRING_SIZE));
 
     switch (c->c2.occ_op)
@@ -319,7 +319,7 @@ check_send_occ_msg_dowork(struct context *c)
                  OCC_STRING_SIZE,
                  (int) sizeof(uint8_t),
                  EXTRA_FRAME(&c->c2.frame),
-                 MAX_RW_SIZE_TUN(&c->c2.frame),
+                 c->c2.frame.buf.payload_size,
                  BLEN(&c->c2.buf));
             doit = true;
         }
src/openvpn/options.c +1 -1
index 441855c7d..6dd573adb 100644
@@ -3832,7 +3832,7 @@ options_string(const struct options *o,
     buf_printf(&out, ",link-mtu %u",
                (unsigned int) calc_options_string_link_mtu(o, frame));
 
-    buf_printf(&out, ",tun-mtu %d", PAYLOAD_SIZE(frame));
+    buf_printf(&out, ",tun-mtu %d", frame->tun_mtu);
     buf_printf(&out, ",proto %s",  proto_remote(o->ce.proto, remote));
 
     bool p2p_nopull = o->mode == MODE_POINT_TO_POINT && !PULL_DEFINED(o);
src/openvpn/ping.c +1 -1
index 67bbca14d..a28f347f8 100644
@@ -80,7 +80,7 @@ check_ping_send_dowork(struct context *c)
 {
     c->c2.buf = c->c2.buffers->aux_buf;
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
-    ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
+    ASSERT(buf_safe(&c->c2.buf, c->c2.frame.buf.payload_size));
     ASSERT(buf_write(&c->c2.buf, ping_string, sizeof(ping_string)));
 
     /*
src/openvpn/ssl.c +27 -8
index 608b30110..41981d220 100644
@@ -332,6 +332,32 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame
     /* set dynamic link MTU to cap control channel packets at 1250 bytes */
     ASSERT(TUN_LINK_DELTA(frame) < min_int(frame->link_mtu, 1250));
     frame->link_mtu_dynamic = min_int(frame->link_mtu, 1250) - TUN_LINK_DELTA(frame);
+
+    /* calculate the maximum overhead that control channel frames may have */
+    int overhead = 0;
+
+    /* Socks */
+    overhead += 10;
+
+    /* tls-auth and tls-crypt */
+    overhead += max_int(tls_crypt_buf_overhead(),
+                        packet_id_size(true) + OPENVPN_MAX_HMAC_SIZE);
+
+    /* TCP length field and opcode */
+    overhead+= 3;
+
+    /* 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
+     * 1250 buffer size since a lot of code blindly assumes a large buffer
+     * (e.g. PUSH_BUNDLE_SIZE) and set frame->mtu_mtu as suggestion for the
+     * size */
+    frame->buf.payload_size = 1250 + overhead;
+
+    frame->buf.headroom = overhead;
+    frame->buf.tailroom = overhead;
+
+    frame->tun_mtu = min_int(data_channel_frame->tun_mtu, 1250);
 }
 
 void
@@ -1870,13 +1896,6 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
         msg(D_HANDSHAKE, "Data Channel: using negotiated cipher '%s'",
             options->ciphername);
     }
-    else
-    {
-        /* Very hacky workaround and quick fix for frame calculation
-         * different when adjusting frame size when the original and new cipher
-         * are identical to avoid a regression with client without NCP */
-        return tls_session_generate_data_channel_keys(session);
-    }
 
     init_key_type(&session->opt->key_type, options->ciphername,
                   options->authname, true, true);
@@ -2959,7 +2978,7 @@ tls_process(struct tls_multi *multi,
             buf = reliable_get_buf_output_sequenced(ks->send_reliable);
             if (buf)
             {
-                int status = key_state_read_ciphertext(&ks->ks_ssl, buf, PAYLOAD_SIZE_DYNAMIC(&multi->opt.frame));
+                int status = key_state_read_ciphertext(&ks->ks_ssl, buf, multi->opt.frame.tun_mtu);
                 if (status == -1)
                 {
                     msg(D_TLS_ERRORS,
src/openvpn/ssl_common.h +2 -1
index f851bd2b9..ada68b4b8 100644
@@ -221,8 +221,9 @@ struct key_state
     struct reliable *rec_reliable; /* order incoming ciphertext packets before we pass to TLS */
     struct reliable_ack *rec_ack; /* buffers all packet IDs we want to ACK back to sender */
 
+    /** Holds outgoing message for the control channel until ks->state reaches
+     * S_ACTIVE */
     struct buffer_list *paybuf;
-
     counter_type n_bytes;                /* how many bytes sent/recvd since last key exchange */
     counter_type n_packets;              /* how many packets sent/recvd since last key exchange */
 
Signature · 1 line
2.33.0
#27 lore raw

PATCH 11/21 Remove pointless do_init_frame_tls function

In reply to Arne Schwabe

This function is static and just calls another functions.

---
 src/openvpn/init.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
src/openvpn/init.c +1 -7
index 2ce963663..0287dda35 100644
@@ -3045,7 +3045,7 @@ do_init_crypto_tls(struct context *c, const unsigned int flags)
 }
 
 static void
-do_init_finalize_tls_frame(struct context *c)
+do_init_frame_tls(struct context *c)
 {
     if (c->c2.tls_multi)
     {
@@ -3302,12 +3302,6 @@ do_option_warnings(struct context *c)
     }
 }
 
-static void
-do_init_frame_tls(struct context *c)
-{
-    do_init_finalize_tls_frame(c);
-}
-
 struct context_buffers *
 init_context_buffers(struct frame *frame)
 {
Signature · 1 line
2.33.0
#29 lore raw

[Openvpn-devel] [PATCH applied] Re: Remove pointless do_init_frame_tls function

In reply to Arne Schwabe

Not tested beyond "make check", change is trivial enough.

Your patch has been applied to the master branch.

commit 2b6fcdc0280c9e56c4ce0f348cab7511b809b839

Author: Arne Schwabe
Date:   Tue Dec 7 18:02:01 2021 +0100
     Remove pointless do_init_frame_tls function

     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]/msg23337.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#30 lore raw

PATCH 12/21 Fix datagram_overhead and assorted functions

In reply to Arne Schwabe

This function is supposed to calculate the overhead of the protocol header (IP/IPv6 + TCP/UDP). But at some point the index that used to index the array proto_overhead and the associated PROTO_N went completely out of sync. This fixed the function and related caller to again calculate the overhead as intended.

Note that IPv6 mapped IPv4 addresses still have the wrong overhead calculated as they treated as IPv6 addresses (0:0:0:0:0:ffff::/96)

---
 src/openvpn/forward.c | 10 ++++++----
 src/openvpn/socket.c  | 16 +++-------------
 src/openvpn/socket.h  | 17 ++++++-----------
 3 files changed, 15 insertions(+), 28 deletions(-)
src/openvpn/forward.c +6 -4
index c971c6bdb..6de6b4d49 100644
@@ -480,10 +480,10 @@ check_fragment(struct context *c)
     struct link_socket_info *lsi = get_link_socket_info(c);
 
     /* OS MTU Hint? */
-    if (lsi->mtu_changed)
+    if (lsi->mtu_changed && lsi->lsa)
     {
         frame_adjust_path_mtu(&c->c2.frame_fragment, c->c2.link_socket->mtu,
-                              c->options.ce.proto);
+                              lsi->lsa->actual.dest.addr.sa.sa_family, lsi->proto);
         lsi->mtu_changed = false;
     }
 
@@ -1565,8 +1565,10 @@ process_outgoing_link(struct context *c)
              */
             if (c->options.shaper)
             {
-                shaper_wrote_bytes(&c->c2.shaper, BLEN(&c->c2.to_link)
-                                   + datagram_overhead(c->options.ce.proto));
+                int overhead = datagram_overhead(c->c2.to_link_addr->dest.addr.sa.sa_family,
+                                                 c->options.ce.proto);
+                shaper_wrote_bytes(&c->c2.shaper,
+                                   BLEN(&c->c2.to_link) + overhead);
             }
 
             /*
src/openvpn/socket.c +3 -13
index df7367469..93d2e61ec 100644
@@ -44,17 +44,6 @@
 
 #include "memdbg.h"
 
-const int proto_overhead[] = { /* indexed by PROTO_x */
-    0,
-    IPv4_UDP_HEADER_SIZE, /* IPv4 */
-    IPv4_TCP_HEADER_SIZE,
-    IPv4_TCP_HEADER_SIZE,
-    IPv6_UDP_HEADER_SIZE, /* IPv6 */
-    IPv6_TCP_HEADER_SIZE,
-    IPv6_TCP_HEADER_SIZE,
-    IPv6_TCP_HEADER_SIZE,
-};
-
 /*
  * Convert sockflags/getaddr_flags into getaddr_flags
  */
@@ -1660,9 +1649,10 @@ socket_frame_init(const struct frame *frame, struct link_socket *sock)
  * to us by the OS.
  */
 void
-frame_adjust_path_mtu(struct frame *frame, int pmtu, int proto)
+frame_adjust_path_mtu(struct frame *frame, int pmtu, sa_family_t af, int proto)
 {
-    frame_set_mtu_dynamic(frame, pmtu - datagram_overhead(proto), SET_MTU_UPPER_BOUND);
+    frame_set_mtu_dynamic(frame, pmtu - datagram_overhead(af, proto),
+                          SET_MTU_UPPER_BOUND);
 }
 
 static void
src/openvpn/socket.h +6 -11
index cc1e0c366..936ef2623 100644
@@ -300,7 +300,7 @@ void do_preresolve(struct context *c);
 
 void socket_adjust_frame_parameters(struct frame *frame, int proto);
 
-void frame_adjust_path_mtu(struct frame *frame, int pmtu, int proto);
+void frame_adjust_path_mtu(struct frame *frame, int pmtu, sa_family_t af, int proto);
 
 void link_socket_close(struct link_socket *sock);
 
@@ -579,18 +579,13 @@ const char *addr_family_name(int af);
 /*
  * Overhead added to packets by various protocols.
  */
-#define IPv4_UDP_HEADER_SIZE              28
-#define IPv4_TCP_HEADER_SIZE              40
-#define IPv6_UDP_HEADER_SIZE              48
-#define IPv6_TCP_HEADER_SIZE              60
-
-extern const int proto_overhead[];
-
 static inline int
-datagram_overhead(int proto)
+datagram_overhead(sa_family_t af, int proto)
 {
-    ASSERT(proto >= 0 && proto < PROTO_N);
-    return proto_overhead [proto];
+    int overhead = 0;
+    overhead += (proto == PROTO_UDP) ? 8 : 20;
+    overhead += (af == AF_INET) ? 20 : 40;
+    return overhead;
 }
 
 /*
Signature · 1 line
2.33.0
Acked-by on v1 12/21
#31 lore raw

Re: [Openvpn-devel] [PATCH 12/21] Fix datagram_overhead and assorted functions

Quoted 5 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:02 geschrieben:

This function is supposed to calculate the overhead of the protocol header (IP/IPv6 + TCP/UDP). But at some point the index that used

FWIW, that point was 30077d1f415b8dc9bd7c8a1ac6a7585136ac6261, AFAICT

commit 30077d1f415b8dc9bd7c8a1ac6a7585136ac6261

Author: Arne Schwabe <[email protected]...>
Date:   Sat Nov 23 20:57:04 2013 +0100

Split the PROTO_UDP_xx options into AF_INET/AF_INET6 and PROTO_TCP/PROTO_UDP part.

--
Frank Lichtenheld

#32 lore raw

PATCH 13/21 Implement optional mtu parameter for mssfix

In reply to Arne Schwabe

The current mssfix parameter is a bit as it needs manual calculation of the allowable packet size and also the resulting MSS value does not take into account if IPv4 or IPv6 is used on the outer tunnel. The mtu parameter fixes both of these problem by dynamically including the real overhead.

The syntax and naming of the parater is chosen for compatiblity with OpenVPN3.

---
 Changes.rst                       |  6 ++++++
 doc/man-sections/link-options.rst | 22 ++++++++++++++--------
 src/openvpn/init.c                |  7 ++++---
 src/openvpn/mss.c                 | 29 ++++++++++++++++++++++++++++-
 src/openvpn/mss.h                 |  3 ++-
 src/openvpn/multi.c               |  3 ++-
 src/openvpn/options.c             | 13 +++++++++++--
 src/openvpn/options.h             |  5 ++++-
 src/openvpn/ssl.c                 | 11 +++++++----
 src/openvpn/ssl.h                 |  5 ++++-
 10 files changed, 82 insertions(+), 22 deletions(-)
Changes.rst +6 -0
index b7d7f2054..cf6a2f86d 100644
@@ -62,6 +62,12 @@ Optional ciphers in ``--data-ciphers``
     Ciphers in ``--data-ciphers`` can now be prefixed with a ``?`` to mark
     those as optional and only use them if the SSL library supports them.
 
+
+Improved ``--mssfix`` calculation
+    The ``--mssfix`` option now allows an optional :code:`mtu` parameter to specify
+    that different overhead for IPv4/IPv6 should taken into account and the resulting
+    size is specified as the total size of the VPN packets including IP and UDP headers.
+
 Deprecated features
 -------------------
 ``inetd`` has been removed
doc/man-sections/link-options.rst +14 -8
index b1ae4e75a..f41c0c4f1 100644
@@ -110,19 +110,25 @@ the local and the remote host.
   (:code:`p2p`). OpenVPN 2.0 introduces a new mode (:code:`server`) which
   implements a multi-client server capability.
 
---mssfix max
+--mssfix max [mtu]
   Announce to TCP sessions running over the tunnel that they should limit
   their send packet sizes such that after OpenVPN has encapsulated them,
   the resulting UDP packet size that OpenVPN sends to its peer will not
   exceed ``max`` bytes. The default value is :code:`1450`.
 
-  The ``max`` parameter is interpreted in the same way as the
-  ``--link-mtu`` parameter, i.e. the UDP packet size after encapsulation
-  overhead has been added in, but not including the UDP header itself.
-  Resulting packet would be at most 28 bytes larger for IPv4 and 48 bytes
-  for IPv6 (20/40 bytes for IP header and 8 bytes for UDP header). Default
-  value of 1450 allows IPv4 packets to be transmitted over a link with MTU
-  1473 or higher without IP level fragmentation.
+  If the :code:`mtu` parameter is specified the ``max`` value is interpreted
+  as the resulting packet size of VPN packets including the IP and UDP header.
+  Use :code:`0` as max to use the default of :code:1450. Support for the
+  :code:`mtu` parameter was added with OpenVPN version 2.6.0.
+
+  If the :code:`mtu` parameter is not specified, the ``max`` parameter
+  is interpreted in the same way as the ``--link-mtu`` parameter, i.e.
+  the UDP packet size after encapsulation overhead has been added in, but
+  not including the UDP header itself. Resulting packet would be at most 28
+  bytes larger for IPv4 and 48 bytes for IPv6 (20/40 bytes for IP header and
+  8 bytes for UDP header). Default value of 1450 allows IPv4 packets to be
+  transmitted over a link with MTU 1473 or higher without IP level
+  fragmentation.
 
   The ``--mssfix`` option only makes sense when you are using the UDP
   protocol for OpenVPN peer-to-peer communication, i.e. ``--proto udp``.
src/openvpn/init.c +4 -3
index 0287dda35..b3653971c 100644
@@ -2207,7 +2207,7 @@ do_deferred_p2p_ncp(struct context *c)
 #endif
 
     if (!tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
-                                         frame_fragment))
+                                         frame_fragment, get_link_socket_info(c)))
     {
         msg(D_TLS_ERRORS, "ERROR: failed to set crypto cipher");
         return false;
@@ -2322,7 +2322,7 @@ do_deferred_options(struct context *c, const unsigned int found)
 
         struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
         if (!tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
-                                              frame_fragment))
+                                              frame_fragment, get_link_socket_info(c)))
         {
             msg(D_TLS_ERRORS, "OPTIONS ERROR: failed to import crypto options");
             return false;
@@ -4238,7 +4238,8 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
 #endif
 
     /* initialize dynamic MTU variable */
-    frame_calculate_mssfix(&c->c2.frame, &c->c1.ks.key_type, &c->options);
+    frame_calculate_mssfix(&c->c2.frame, &c->c1.ks.key_type, &c->options,
+                           get_link_socket_info(c));
 
     /* bind the TCP/UDP socket */
     if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
src/openvpn/mss.c +28 -1
index e4311c42a..b1f10d53e 100644
@@ -207,9 +207,30 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss)
     }
 }
 
+static unsigned int
+get_ip_encap_overhead(const struct options *options,
+                      const struct link_socket_info *lsi)
+{
+    /* Add the overhead of the encapsulating IP packets */
+    sa_family_t af;
+    if (lsi->lsa)
+    {
+        af = lsi->lsa->actual.dest.addr.sa.sa_family;
+    }
+    else
+    {
+        /* In the early init before the connection is established or we
+         * are in listen mode we can only make an educated guess
+         * from the af of the connection entry */
+        af = options->ce.af;
+    }
+    return datagram_overhead(af, lsi->proto);
+}
+
 void
 frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
-                       const struct options *options)
+                       const struct options *options,
+                       struct link_socket_info *lsi)
 {
     if (options->ce.mssfix == 0)
     {
@@ -236,6 +257,12 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
      *
      * (RFC 879, section 7). */
 
+    if (options->ce.mssfix_encap)
+    {
+        /* Add the overhead of the encapsulating IP packets */
+        overhead += get_ip_encap_overhead(options, lsi);
+    }
+
     /* Add 20 bytes for the IPv4 header and TCP header of the payload,
      * the mssfix routes will add 20 extra if payload is IPv6 */
     overhead += 20 + 20;
src/openvpn/mss.h +2 -1
index 856f4c4e3..eecc79948 100644
@@ -37,6 +37,7 @@ void mss_fixup_dowork(struct buffer *buf, uint16_t maxmss);
 
 /** Set the --mssfix option. */
 void frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
-                            const struct options *options);
+                            const struct options *options,
+                            struct link_socket_info *lsi);
 
 #endif
src/openvpn/multi.c +2 -1
index e5ffebff2..67b7114ad 100644
@@ -2286,7 +2286,8 @@ multi_client_generate_tls_keys(struct context *c)
 #endif
     struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
     if (!tls_session_update_crypto_params(session, &c->options,
-                                          &c->c2.frame, frame_fragment))
+                                          &c->c2.frame, frame_fragment,
+                                          get_link_socket_info(c)))
     {
         msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
         register_signal(c, SIGUSR1, "process-push-msg-failed");
src/openvpn/options.c +11 -2
index 6dd573adb..8997fa988 100644
@@ -6771,18 +6771,27 @@ add_option(struct options *options,
         VERIFY_PERMISSION(OPT_P_GENERAL);
         script_security_set(atoi(p[1]));
     }
-    else if (streq(p[0], "mssfix") && !p[2])
+    else if (streq(p[0], "mssfix") && !p[3])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
         if (p[1])
         {
             options->ce.mssfix = positive_atoi(p[1]);
         }
-        else
+
+        if (!p[1] || options->ce.mssfix == 0)
         {
             options->ce.mssfix_default = true;
         }
 
+        if (p[2] && streq(p[2], "mtu"))
+        {
+            options->ce.mssfix_encap = true;
+        }
+        else if (p[2])
+        {
+            msg(msglevel, "Unknown parameter to --mssfix: %s", p[2]);
+        }
     }
     else if (streq(p[0], "disable-occ") && !p[1])
     {
src/openvpn/options.h +4 -1
index d4f41cd71..557edab9b 100644
@@ -126,7 +126,10 @@ struct connection_entry
 
     int fragment;        /* internal fragmentation size */
     int mssfix;          /* Upper bound on TCP MSS */
-    bool mssfix_default; /* true if --mssfix was supplied without a parameter */
+    bool mssfix_default; /* true if --mssfix was supplied without a parameter
+                          * or 0 was specified as MTU */
+    bool mssfix_encap;   /* true if --mssfix had the "mtu" parameter to include
+                          * overhead from IP and TCP/UDP encapsulation */
 
     int explicit_exit_notification; /* Explicitly tell peer when we are exiting via OCC_EXIT or [RESTART] message */
 
src/openvpn/ssl.c +7 -4
index 41981d220..c5b085646 100644
@@ -1883,7 +1883,8 @@ cleanup:
 bool
 tls_session_update_crypto_params_do_work(struct tls_session *session,
                                  struct options* options, struct frame *frame,
-                                 struct frame *frame_fragment)
+                                 struct frame *frame_fragment,
+                                 struct link_socket_info *lsi)
 {
     if (session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized)
     {
@@ -1913,7 +1914,7 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
                                    options->replay, packet_id_long_form);
     frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,
                    options->ce.tun_mtu_defined, options->ce.tun_mtu);
-    frame_calculate_mssfix(frame, &session->opt->key_type, options);
+    frame_calculate_mssfix(frame, &session->opt->key_type, options, lsi);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
     /*
@@ -1938,7 +1939,8 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
 bool
 tls_session_update_crypto_params(struct tls_session *session,
                                  struct options *options, struct frame *frame,
-                                 struct frame *frame_fragment)
+                                 struct frame *frame_fragment,
+                                 struct link_socket_info *lsi)
 {
 
     bool cipher_allowed_as_fallback = options->enable_ncp_fallback
@@ -1957,7 +1959,8 @@ tls_session_update_crypto_params(struct tls_session *session,
     /* Import crypto settings that might be set by pull/push */
     session->opt->crypto_flags |= options->data_channel_crypto_flags;
 
-    return tls_session_update_crypto_params_do_work(session, options, frame, frame_fragment);
+    return tls_session_update_crypto_params_do_work(session, options, frame,
+                                                    frame_fragment, lsi);
 }
 
 
src/openvpn/ssl.h +4 -1
index b14453fe2..e566acd81 100644
@@ -508,13 +508,16 @@ void tls_update_remote_addr(struct tls_multi *multi,
  * @param frame           The frame options for this session (frame overhead is
  *                        adjusted based on the selected cipher/auth).
  * @param frame_fragment  The fragment frame options.
+ * @param lsi             link socket info to adjust MTU related options
+ *                        depending on the current protocol
  *
  * @return true if updating succeeded or keys are already generated, false otherwise.
  */
 bool tls_session_update_crypto_params(struct tls_session *session,
                                       struct options *options,
                                       struct frame *frame,
-                                      struct frame *frame_fragment);
+                                      struct frame *frame_fragment,
+                                      struct link_socket_info *lsi);
 
 /*
  * inline functions
Signature · 1 line
2.33.0
#33 lore raw

Re: [Openvpn-devel] [PATCH 13/21] Implement optional mtu parameter for mssfix

Quoted 6 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:02 geschrieben:

The current mssfix parameter is a bit as it needs manual calculation of

missing word after "bit"?

Quoted 5 lines

the allowable packet size and also the resulting MSS value does not take into account if IPv4 or IPv6 is used on the outer tunnel. The mtu parameter fixes both of these problem by dynamically including the real overhead.

The syntax and naming of the parater is chosen for compatiblity with OpenVPN3.

typo "parameter"

doc/man-sections/link-options.rst the local and the remote host. · line 113

Quoted 10 lines
doc/man-sections/link-options.rst +1 -1
index b1ae4e75a..f41c0c4f1 100644
@@ -110,19 +110,25 @@ the local and the remote host.
   (:code:`p2p`). OpenVPN 2.0 introduces a new mode (:code:`server`) which
   implements a multi-client server capability.
 
---mssfix max
+--mssfix max [mtu]

If you want to denote optional paramters (which I think makes sense, but is not consistently done in the documentation so far), then I think it needs to be --mssfix [max [mtu]]

doc/man-sections/link-options.rst the local and the remote host. · line 118

Quoted 5 lines
doc/man-sections/link-options.rst +0 -0
   Announce to TCP sessions running over the tunnel that they should limit
   their send packet sizes such that after OpenVPN has encapsulated them,
   the resulting UDP packet size that OpenVPN sends to its peer will not
   exceed ``max`` bytes. The default value is :code:`1450`.
 

Regards,
Frank

--
Frank Lichtenheld

#34 lore raw

Re: [Openvpn-devel] [PATCH 13/21] Implement optional mtu parameter for mssfix

Quoted 10 lines

Frank Lichtenheld <[email protected]...> hat am 09.12.2021 14:49 geschrieben:

Quoted 9 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:02 geschrieben:

doc/man-sections/link-options.rst +0 -0
index b1ae4e75a..f41c0c4f1 100644
@@ -110,19 +110,25 @@ the local and the remote host.
   (:code:`p2p`). OpenVPN 2.0 introduces a new mode (:code:`server`) which
   implements a multi-client server capability.
 
Quoted 6 lines

doc/man-sections/link-options.rst the local and the remote host. · line 113

Quoted 2 lines
doc/man-sections/link-options.rst +1 -1
---mssfix max
+--mssfix max [mtu]

If you want to denote optional paramters (which I think makes sense, but is not consistently done in the documentation so far), then I think it needs to be --mssfix [max [mtu]]

It turns out that the reason we do not use [] in the option list is that it breaks the rst formatting. Option lists do not seem to have any support for formatting optional arguments (see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#option-lists).
I have verified that this visibly breaks the formatting, both for HTML and man page output.

This seems to be the reason that many options use a generic "args" and then specify the actual syntax in the description block instead.

Regards,
Frank
--
Frank Lichtenheld

#35 lore raw

PATCH 14/21 Add mtu paramter to --fragment and change fragment calculation

In reply to Arne Schwabe

Instead relying on the link_mtu_dynamic field and its calculation in the frame struct, add a new field max_fragment_size and add a calculation of it similar to mssfix.

Also whenever mssfix value is calculated, we also want to calculate the values for fragment as both options need to be calculated from the real overhead.

---
 Changes.rst                       |   9 +--
 doc/man-sections/link-options.rst |  13 ++--
 src/openvpn/forward.c             |   3 +-
 src/openvpn/fragment.c            |   4 +-
 src/openvpn/init.c                |  23 +++----
 src/openvpn/mss.c                 | 101 +++++++++++++++++++++++++++---
 src/openvpn/mss.h                 |  13 +++-
 src/openvpn/mtu.c                 |  66 +------------------
 src/openvpn/mtu.h                 |  22 +++----
 src/openvpn/options.c             |  12 +++-
 src/openvpn/options.h             |   2 +
 src/openvpn/socket.c              |  11 ----
 src/openvpn/socket.h              |   2 -
 src/openvpn/ssl.c                 |  20 ++----
 14 files changed, 157 insertions(+), 144 deletions(-)
Changes.rst +5 -4
index cf6a2f86d..c673196fa 100644
@@ -63,10 +63,11 @@ Optional ciphers in ``--data-ciphers``
     those as optional and only use them if the SSL library supports them.
 
 
-Improved ``--mssfix`` calculation
-    The ``--mssfix`` option now allows an optional :code:`mtu` parameter to specify
-    that different overhead for IPv4/IPv6 should taken into account and the resulting
-    size is specified as the total size of the VPN packets including IP and UDP headers.
+Improved ``--mssfix`` and ``--fragement`` calculation
+    The ``--mssfix`` and ``--fragment`` options now allow an optional :code:`mtu`
+    parameter to specify that different overhead for IPv4/IPv6 should taken into
+    account and the resulting size is specified as the total size of the VPN packets
+    including IP and UDP headers.
 
 Deprecated features
 -------------------
doc/man-sections/link-options.rst +9 -4
index f41c0c4f1..b71656e0b 100644
@@ -24,13 +24,18 @@ the local and the remote host.
   from any address, not only the address which was specified in the
   ``--remote`` option.
 
---fragment max
+--fragment max [mtu]
   Enable internal datagram fragmentation so that no UDP datagrams are sent
   which are larger than ``max`` bytes.
 
-  The ``max`` parameter is interpreted in the same way as the
-  ``--link-mtu`` parameter, i.e. the UDP packet size after encapsulation
-  overhead has been added in, but not including the UDP header itself.
+  If the :code:`mtu` parameter is present the ``max`` parameter is
+  interpreted to include IP and UDP encapsulation overhead. The
+  :code:`mtu` parameter is introduced in OpenVPN version 2.6.0.
+
+  If the :code:`mtu` parameter is absent, the ``max`` parameter is
+  interpreted in the same way as the ``--link-mtu`` parameter, i.e.
+  the UDP packet size after encapsulation overhead has been added in,
+  but not including the UDP header itself.
 
   The ``--fragment`` option only makes sense when you are using the UDP
   protocol (``--proto udp``).
src/openvpn/forward.c +1 -2
index 6de6b4d49..3f362e95d 100644
@@ -482,8 +482,7 @@ check_fragment(struct context *c)
     /* OS MTU Hint? */
     if (lsi->mtu_changed && lsi->lsa)
     {
-        frame_adjust_path_mtu(&c->c2.frame_fragment, c->c2.link_socket->mtu,
-                              lsi->lsa->actual.dest.addr.sa.sa_family, lsi->proto);
+        frame_adjust_path_mtu(c);
         lsi->mtu_changed = false;
     }
 
src/openvpn/fragment.c +2 -2
index 6f8fb4476..ce8cd3489 100644
@@ -335,12 +335,12 @@ fragment_outgoing(struct fragment_master *f, struct buffer *buf,
             msg(D_FRAG_ERRORS, "FRAG: outgoing buffer is not empty, len=[%d,%d]",
                 buf->len, f->outgoing.len);
         }
-        if (buf->len > PAYLOAD_SIZE_DYNAMIC(frame)) /* should we fragment? */
+        if (buf->len > frame->max_fragment_size) /* should we fragment? */
         {
             /*
              * Send the datagram as a series of 2 or more fragments.
              */
-            f->outgoing_frag_size = optimal_fragment_size(buf->len, PAYLOAD_SIZE_DYNAMIC(frame));
+            f->outgoing_frag_size = optimal_fragment_size(buf->len, frame->max_fragment_size);
             if (buf->len > f->outgoing_frag_size * MAX_FRAGS)
             {
                 FRAG_ERR("too many fragments would be required to send datagram");
src/openvpn/init.c +10 -13
index b3653971c..31ae250bc 100644
@@ -2535,14 +2535,6 @@ frame_finalize_options(struct context *c, const struct options *o)
     frame->buf.payload_size = payload_size;
     frame->buf.headroom = headroom;
     frame->buf.tailroom = tailroom;
-
-    /* Kept to still update/calculate the other fields for now */
-    frame_finalize(frame,
-                   o->ce.link_mtu_defined,
-                   o->ce.link_mtu,
-                   o->ce.tun_mtu_defined,
-                   o->ce.tun_mtu);
-
 }
 
 /*
@@ -3368,8 +3360,8 @@ static void
 do_init_fragment(struct context *c)
 {
     ASSERT(c->options.ce.fragment);
-    frame_set_mtu_dynamic(&c->c2.frame_fragment,
-                          c->options.ce.fragment, SET_MTU_UPPER_BOUND);
+    frame_calculate_dynamic(&c->c2.frame_fragment, &c->c1.ks.key_type,
+                            &c->options, get_link_socket_info(c));
     fragment_frame_init(c->c2.fragment, &c->c2.frame_fragment);
 }
 #endif
@@ -4237,9 +4229,9 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
     }
 #endif
 
-    /* initialize dynamic MTU variable */
-    frame_calculate_mssfix(&c->c2.frame, &c->c1.ks.key_type, &c->options,
-                           get_link_socket_info(c));
+    /* initialize dynamic MTU based options (fragment/mssfix) */
+    frame_calculate_dynamic(&c->c2.frame, &c->c1.ks.key_type, &c->options,
+                            get_link_socket_info(c));
 
     /* bind the TCP/UDP socket */
     if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
@@ -4291,6 +4283,11 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
         link_socket_init_phase2(c);
     }
 
+    /* Update dynamic frame calculation as exact transport socket information
+     * (IP vs IPv6) may be only available after socket phase2 has finished */
+    frame_calculate_dynamic(&c->c2.frame, &c->c1.ks.key_type, &c->options,
+                            get_link_socket_info(c));
+
     /*
      * Actually do UID/GID downgrade, and chroot, if requested.
      * May be delayed by --client, --pull, or --up-delay.
src/openvpn/mss.c +94 -7

121 lines not on this page · raw message

src/openvpn/mss.h +10 -3
index eecc79948..82e0c58f6 100644
@@ -36,8 +36,15 @@ void mss_fixup_ipv6(struct buffer *buf, int maxmss);
 void mss_fixup_dowork(struct buffer *buf, uint16_t maxmss);
 
 /** Set the --mssfix option. */
-void frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
-                            const struct options *options,
-                            struct link_socket_info *lsi);
+void frame_calculate_dynamic(struct frame *frame, struct key_type *kt,
+                             const struct options *options,
+                             struct link_socket_info *lsi);
+
+/**
+ * Checks and adjusts the fragment and mssfix value according to the
+ * discovered path mtu value
+ * @param c     context to adjust
+ */
+void frame_adjust_path_mtu(struct context *c);
 
 #endif
src/openvpn/mtu.c +3 -63

83 lines not on this page · raw message

src/openvpn/mtu.h +7 -15
index ace33a74a..06a00b5bb 100644
@@ -113,14 +113,18 @@ struct frame {
     int link_mtu;               /**< Maximum packet size to be sent over
                                  *   the external network interface. */
 
-    unsigned int mss_fix;      /**< The actual MSS value that should be
+    unsigned int mss_fix;       /**< The actual MSS value that should be
                                  *   written to the payload packets. This
                                  *   is the value for IPv4 TCP packets. For
                                  *   IPv6 packets another 20 bytes must
                                  *   be subtracted */
 
-    int link_mtu_dynamic;       /**< Dynamic MTU value for the external
-                                 *   network interface. */
+    int max_fragment_size;      /**< The maximum size of a fragment.
+                                 * Fragmentation is done on the unencrypted
+                                 * payload after (potential) compression. So
+                                 * this value specifies the maximum payload
+                                 * size that can be send in a single fragment
+                                 */
 
     int extra_frame;            /**< Maximum number of bytes that all
                                  *   processing steps together could add.
@@ -195,7 +199,6 @@ struct options;
  * a tap device ifconfiged to an MTU of 1200 might actually want
  * to return a packet size of 1214 on a read().
  */
-#define PAYLOAD_SIZE_DYNAMIC(f)  ((f)->link_mtu_dynamic - (f)->extra_frame)
 #define PAYLOAD_SIZE(f)          ((f)->buf.payload_size)
 
 /*
@@ -203,7 +206,6 @@ struct options;
  * overhead is added.
  */
 #define EXPANDED_SIZE(f)         ((f)->link_mtu)
-#define EXPANDED_SIZE_DYNAMIC(f) ((f)->link_mtu_dynamic)
 #define EXPANDED_SIZE_MIN(f)     (TUN_MTU_MIN + TUN_LINK_DELTA(f))
 
 /*
@@ -224,16 +226,6 @@ struct options;
 
 #define FRAME_HEADROOM(f)          ((f)->buf.headroom)
 
-/*
- * Function prototypes.
- */
-
-void frame_finalize(struct frame *frame,
-                    bool link_mtu_defined,
-                    int link_mtu,
-                    bool tun_mtu_defined,
-                    int tun_mtu);
-
 void frame_subtract_extra(struct frame *frame, const struct frame *src);
 
 void frame_print(const struct frame *frame,
src/openvpn/options.c +10 -2
index 8997fa988..e1792d510 100644
@@ -6115,11 +6115,19 @@ add_option(struct options *options,
         msg(msglevel, "--mtu-dynamic has been replaced by --fragment");
         goto err;
     }
-    else if (streq(p[0], "fragment") && p[1] && !p[2])
+    else if (streq(p[0], "fragment") && p[1] && !p[3])
     {
-/*      VERIFY_PERMISSION (OPT_P_MTU); */
         VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
         options->ce.fragment = positive_atoi(p[1]);
+
+        if (p[2] && streq(p[2], "mtu"))
+        {
+            options->ce.fragment_encap = true;
+        }
+        else if (p[2])
+        {
+            msg(msglevel, "Unknown parameter to --fragment: %s", p[2]);
+        }
     }
 #endif
     else if (streq(p[0], "mtu-disc") && p[1] && !p[2])
src/openvpn/options.h +2 -0
index 557edab9b..256318291 100644
@@ -125,6 +125,8 @@ struct connection_entry
     int mtu_discover_type; /* used if OS supports setting Path MTU discovery options on socket */
 
     int fragment;        /* internal fragmentation size */
+    bool fragment_encap; /* true if --fragment had the "mtu" parameter to
+                          * include overhead from IP and TCP/UDP encapsulation */
     int mssfix;          /* Upper bound on TCP MSS */
     bool mssfix_default; /* true if --mssfix was supplied without a parameter
                           * or 0 was specified as MTU */
src/openvpn/socket.c +0 -11
index 93d2e61ec..fe1dfb315 100644
@@ -1644,17 +1644,6 @@ socket_frame_init(const struct frame *frame, struct link_socket *sock)
     }
 }
 
-/*
- * Adjust frame structure based on a Path MTU value given
- * to us by the OS.
- */
-void
-frame_adjust_path_mtu(struct frame *frame, int pmtu, sa_family_t af, int proto)
-{
-    frame_set_mtu_dynamic(frame, pmtu - datagram_overhead(af, proto),
-                          SET_MTU_UPPER_BOUND);
-}
-
 static void
 resolve_bind_local(struct link_socket *sock, const sa_family_t af)
 {
src/openvpn/socket.h +0 -2
index 936ef2623..a43ed80b5 100644
@@ -300,8 +300,6 @@ void do_preresolve(struct context *c);
 
 void socket_adjust_frame_parameters(struct frame *frame, int proto);
 
-void frame_adjust_path_mtu(struct frame *frame, int pmtu, sa_family_t af, int proto);
-
 void link_socket_close(struct link_socket *sock);
 
 void sd_close(socket_descriptor_t *sd);
src/openvpn/ssl.c +4 -16
index c5b085646..79a5660bd 100644
@@ -331,7 +331,6 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame
 
     /* set dynamic link MTU to cap control channel packets at 1250 bytes */
     ASSERT(TUN_LINK_DELTA(frame) < min_int(frame->link_mtu, 1250));
-    frame->link_mtu_dynamic = min_int(frame->link_mtu, 1250) - TUN_LINK_DELTA(frame);
 
     /* calculate the maximum overhead that control channel frames may have */
     int overhead = 0;
@@ -1912,9 +1911,8 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
     frame_remove_from_extra_frame(frame, crypto_max_overhead());
     crypto_adjust_frame_parameters(frame, &session->opt->key_type,
                                    options->replay, packet_id_long_form);
-    frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,
-                   options->ce.tun_mtu_defined, options->ce.tun_mtu);
-    frame_calculate_mssfix(frame, &session->opt->key_type, options, lsi);
+    frame_calculate_dynamic(frame, &session->opt->key_type, options, lsi);
+
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
     /*
@@ -1929,7 +1927,7 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
         frame_remove_from_extra_frame(frame_fragment, crypto_max_overhead());
         crypto_adjust_frame_parameters(frame_fragment, &session->opt->key_type,
                                        options->replay, packet_id_long_form);
-        frame_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
+        frame_calculate_dynamic(frame_fragment, &session->opt->key_type, options, lsi);
         frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
     }
 
@@ -2982,6 +2980,7 @@ tls_process(struct tls_multi *multi,
             if (buf)
             {
                 int status = key_state_read_ciphertext(&ks->ks_ssl, buf, multi->opt.frame.tun_mtu);
+
                 if (status == -1)
                 {
                     msg(D_TLS_ERRORS,
@@ -3827,17 +3826,6 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,
         goto error;
     }
 
-    if (buf->len > EXPANDED_SIZE_DYNAMIC(&tas->frame))
-    {
-        dmsg(D_TLS_STATE_ERRORS,
-             "TLS State Error: Large packet (size %d) received from %s -- a packet no larger than %d bytes was expected",
-             buf->len,
-             print_link_socket_actual(from, &gc),
-             EXPANDED_SIZE_DYNAMIC(&tas->frame));
-        goto error;
-    }
-
-
     struct buffer newbuf = clone_buf(buf);
     struct tls_wrap_ctx tls_wrap_tmp = tas->tls_wrap;
 
Signature · 1 line
2.33.0
#36 lore raw

Re: [Openvpn-devel] [PATCH 14/21] Add mtu paramter to --fragment and change fragment calculation

Quoted 8 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:02 geschrieben:

Changes.rst +0 -0
index cf6a2f86d..c673196fa 100644

Changes.rst Optional ciphers in ``--data-ciphers`` · line 66

Quoted 8 lines
Changes.rst +1 -4
@@ -63,10 +63,11 @@ Optional ciphers in ``--data-ciphers``
     those as optional and only use them if the SSL library supports them.
 
 
-Improved ``--mssfix`` calculation
-    The ``--mssfix`` option now allows an optional :code:`mtu` parameter to specify
-    that different overhead for IPv4/IPv6 should taken into account and the resulting
-    size is specified as the total size of the VPN packets including IP and UDP headers.
+Improved ``--mssfix`` and ``--fragement`` calculation

Typo, should be "--fragment"

Changes.rst Optional ciphers in ``--data-ciphers`` · line 70

Quoted 4 lines
Changes.rst +4 -0
+    The ``--mssfix`` and ``--fragment`` options now allow an optional :code:`mtu`
+    parameter to specify that different overhead for IPv4/IPv6 should taken into
+    account and the resulting size is specified as the total size of the VPN packets
+    including IP and UDP headers.

Regards,
Frank

--
Frank Lichtenheld

#37 lore raw

Re: [Openvpn-devel] [PATCH 14/21] Add mtu paramter to --fragment and change fragment calculation

In reply to Arne Schwabe
Quoted 16 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:02 geschrieben:

doc/man-sections/link-options.rst +1 -2
index f41c0c4f1..b71656e0b 100644
@@ -24,13 +24,18 @@ the local and the remote host.
   from any address, not only the address which was specified in the
   ``--remote`` option.
 
---fragment max
+--fragment max [mtu]
   Enable internal datagram fragmentation so that no UDP datagrams are sent
   which are larger than ``max`` bytes.
 
-  The ``max`` parameter is interpreted in the same way as the

doc/man-sections/link-options.rst the local and the remote host. · line 38

Quoted 10 lines
doc/man-sections/link-options.rst +8 -2
-  ``--link-mtu`` parameter, i.e. the UDP packet size after encapsulation
-  overhead has been added in, but not including the UDP header itself.
+  If the :code:`mtu` parameter is present the ``max`` parameter is
+  interpreted to include IP and UDP encapsulation overhead. The
+  :code:`mtu` parameter is introduced in OpenVPN version 2.6.0.
+
+  If the :code:`mtu` parameter is absent, the ``max`` parameter is
+  interpreted in the same way as the ``--link-mtu`` parameter, i.e.
+  the UDP packet size after encapsulation overhead has been added in,
+  but not including the UDP header itself.

The same comment as for mssfix applies here: "--fragment max [mtu]" is not valid rst syntax. Even "--fragment max mtu" is invalid. You need to do something like

--fragment args
Valid syntaxes::

fragment max
fragment max mtu

Like we do for other parameters.

Regards,
--
Frank Lichtenheld

#38 lore raw

PATCH 15/21 Update fragment and mssfix related warnings

In reply to Arne Schwabe

The warning that fragment/mssfix needs also tun-mtu set to 1500 makes little sense. Remove it completely. Instead warn if there are incosistencies between --fragment and mssfix.

---
 src/openvpn/init.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
src/openvpn/init.c +10 -5
index 31ae250bc..4f08e560a 100644
@@ -3183,12 +3183,17 @@ do_init_frame(struct context *c)
 #endif
 
 #ifdef ENABLE_FRAGMENT
-    if ((c->options.ce.mssfix || c->options.ce.fragment)
-        && TUN_MTU_SIZE(&c->c2.frame_fragment) != ETHERNET_MTU)
+    if (c->options.ce.fragment > 0 && c->options.ce.mssfix > c->options.ce.fragment)
     {
-        msg(M_WARN,
-            "WARNING: normally if you use --mssfix and/or --fragment, you should also set --tun-mtu %d (currently it is %d)",
-            ETHERNET_MTU, TUN_MTU_SIZE(&c->c2.frame_fragment));
+        msg(M_WARN, "WARNING: if you use --mssfix and --fragment, you should "
+                    "set --fragment (%d) larger than --mssfix (%d)",
+                    c->options.ce.fragment, c->options.ce.mssfix);
+    }
+    if (c->options.ce.fragment > 0 && c->options.ce.mssfix > 0
+        && c->options.ce.fragment_encap != c->options.ce.mssfix_encap)
+    {
+        msg(M_WARN, "WARNING: if you use --mssfix and --fragment, you should "
+                    "use the mtu for both or none.");
     }
 #endif
 }
Signature · 1 line
2.33.0
Acked-by on v1 15/21
#39 lore raw

Re: [Openvpn-devel] [PATCH 15/21] Update fragment and mssfix related warnings

Quoted 15 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:02 geschrieben:

src/openvpn/init.c +1 -4
index 31ae250bc..4f08e560a 100644
@@ -3183,12 +3183,17 @@ do_init_frame(struct context *c)
 #endif
 
 #ifdef ENABLE_FRAGMENT
-    if ((c->options.ce.mssfix || c->options.ce.fragment)
-        && TUN_MTU_SIZE(&c->c2.frame_fragment) != ETHERNET_MTU)
+    if (c->options.ce.fragment > 0 && c->options.ce.mssfix > c->options.ce.fragment)
     {
-        msg(M_WARN,
-            "WARNING: normally if you use --mssfix and/or --fragment, you should also set --tun-mtu %d (currently it is %d)",

src/openvpn/init.c do_init_frame(struct context *c) · line 3196

Quoted 10 lines
src/openvpn/init.c +9 -1
-            ETHERNET_MTU, TUN_MTU_SIZE(&c->c2.frame_fragment));
+        msg(M_WARN, "WARNING: if you use --mssfix and --fragment, you should "
+                    "set --fragment (%d) larger than --mssfix (%d)",
+                    c->options.ce.fragment, c->options.ce.mssfix);
+    }
+    if (c->options.ce.fragment > 0 && c->options.ce.mssfix > 0
+        && c->options.ce.fragment_encap != c->options.ce.mssfix_encap)
+    {
+        msg(M_WARN, "WARNING: if you use --mssfix and --fragment, you should "
+                    "use the mtu for both or none.");

"the mtu parameter"? Or do you actually mean "the mtu" here?

Regards,
--
Frank Lichtenheld

#40 lore raw

PATCH 16/21 Remove link_mtu parameter when running up/down scripts

In reply to Arne Schwabe

The link mtu is no longer used and calculating a compatibility link MTU just for scripts makes little sense as well. Replace the parameter instead with a fixed paramter 0.

---
 Changes.rst        |  2 ++
 src/openvpn/init.c | 16 +++-------------
 2 files changed, 5 insertions(+), 13 deletions(-)
Changes.rst +2 -0
index c673196fa..3ae994022 100644
@@ -129,6 +129,8 @@ User-visible Changes
 - CHACHA20-POLY1305 is included in the default of ``--data-ciphers`` when available.
 - Option ``--prng`` is ignored as we rely on the SSL library random number generator.
 - 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.
 
 Overview of changes in 2.5
 ==========================
src/openvpn/init.c +3 -13
index 4f08e560a..72ffbfdfc 100644
@@ -113,7 +113,6 @@ run_up_down(const char *command,
 #endif
             const char *dev_type,
             int tun_mtu,
-            int link_mtu,
             const char *ifconfig_local,
             const char *ifconfig_remote,
             const char *context,
@@ -129,7 +128,6 @@ run_up_down(const char *command,
     }
     setenv_str(es, "script_context", context);
     setenv_int(es, "tun_mtu", tun_mtu);
-    setenv_int(es, "link_mtu", link_mtu);
     setenv_str(es, "dev", arg);
     if (dev_type)
     {
@@ -157,11 +155,8 @@ run_up_down(const char *command,
         struct argv argv = argv_new();
         ASSERT(arg);
         argv_printf(&argv,
-                    "%s %d %d %s %s %s",
-                    arg,
-                    tun_mtu, link_mtu,
-                    ifconfig_local, ifconfig_remote,
-                    context);
+                    "%s %d 0 %s %s %s",
+                    arg, tun_mtu, ifconfig_local, ifconfig_remote, context);
 
         if (plugin_call(plugins, plugin_type, &argv, NULL, es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
         {
@@ -177,7 +172,7 @@ run_up_down(const char *command,
         ASSERT(arg);
         setenv_str(es, "script_type", script_type);
         argv_parse_cmd(&argv, command);
-        argv_printf_cat(&argv, "%s %d %d %s %s %s", arg, tun_mtu, link_mtu,
+        argv_printf_cat(&argv, "%s %d 0 %s %s %s", arg, tun_mtu,
                         ifconfig_local, ifconfig_remote, context);
         argv_msg(M_INFO, &argv);
         openvpn_run_script(&argv, es, S_FATAL, "--up/--down");
@@ -1784,7 +1779,6 @@ do_open_tun(struct context *c)
 #endif
                 dev_type_string(c->options.dev, c->options.dev_type),
                 TUN_MTU_SIZE(&c->c2.frame),
-                EXPANDED_SIZE(&c->c2.frame),
                 print_in_addr_t(c->c1.tuntap->local, IA_EMPTY_IF_UNDEF, &gc),
                 print_in_addr_t(c->c1.tuntap->remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                 "init",
@@ -1834,7 +1828,6 @@ else
 #endif
                     dev_type_string(c->options.dev, c->options.dev_type),
                     TUN_MTU_SIZE(&c->c2.frame),
-                    EXPANDED_SIZE(&c->c2.frame),
                     print_in_addr_t(c->c1.tuntap->local, IA_EMPTY_IF_UNDEF, &gc),
                     print_in_addr_t(c->c1.tuntap->remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                     "restart",
@@ -1914,7 +1907,6 @@ do_close_tun(struct context *c, bool force)
 #endif
                             NULL,
                             TUN_MTU_SIZE(&c->c2.frame),
-                            EXPANDED_SIZE(&c->c2.frame),
                             print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
                             print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                             "init",
@@ -1945,7 +1937,6 @@ do_close_tun(struct context *c, bool force)
 #endif
                         NULL,
                         TUN_MTU_SIZE(&c->c2.frame),
-                        EXPANDED_SIZE(&c->c2.frame),
                         print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
                         print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                         "init",
@@ -1984,7 +1975,6 @@ do_close_tun(struct context *c, bool force)
 #endif
                             NULL,
                             TUN_MTU_SIZE(&c->c2.frame),
-                            EXPANDED_SIZE(&c->c2.frame),
                             print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
                             print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                             "restart",
Signature · 1 line
2.33.0
#41 lore raw

Re: [Openvpn-devel] [PATCH 16/21] Remove link_mtu parameter when running up/down scripts

NACK, missing update to script-options.rst.

Quoted 6 lines

Arne Schwabe <[email protected]...> hat am 07.12.2021 18:02 geschrieben:

The link mtu is no longer used and calculating a compatibility link MTU just for scripts makes little sense as well. Replace the parameter instead with a fixed paramter 0.

Regards,
--
Frank Lichtenheld

#42 lore raw

PATCH 17/21 Use new frame header methods to calculate OCC_MTU_LOAD payload size

In reply to Arne Schwabe
---
 src/openvpn/occ.c | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)
src/openvpn/occ.c +24 -8
index c4e7c1be2..05e2d4b85 100644
@@ -199,8 +199,12 @@ check_send_occ_load_test_dowork(struct context *c)
         if (entry->op >= 0)
         {
             c->c2.occ_op = entry->op;
-            c->c2.occ_mtu_load_size =
-                EXPANDED_SIZE(&c->c2.frame) + entry->delta;
+            size_t payload_size = frame_calculate_payload_size(&c->c2.frame,
+                                                               &c->options);
+            size_t header_size = frame_calculate_protocol_header_size(&c->c1.ks.key_type, &c->options,
+                                                                      payload_size, false);
+
+            c->c2.occ_mtu_load_size = payload_size + header_size;
         }
         else
         {
@@ -298,10 +302,21 @@ check_send_occ_msg_dowork(struct context *c)
             {
                 break;
             }
-            need_to_add = min_int(c->c2.occ_mtu_load_size, EXPANDED_SIZE(&c->c2.frame))
+            size_t proto_hdr, payload_hdr;
+
+            /* OCC message have comp/fragment headers but not ethernet headers */
+            payload_hdr = frame_calculate_payload_overhead(&c->c2.frame, &c->options,
+                                                           false);
+
+            /* Since we do not know the payload size we just pass 0 as size here */
+            proto_hdr = frame_calculate_protocol_header_size(&c->c1.ks.key_type,
+                                                             &c->options, 0, false);
+
+            need_to_add = min_int(c->c2.occ_mtu_load_size, c->c2.frame.buf.payload_size)
                           - OCC_STRING_SIZE
-                          - sizeof(uint8_t)
-                          - EXTRA_FRAME(&c->c2.frame);
+                          - sizeof(uint8_t)     /* occ opcode */
+                          - payload_hdr
+                          - proto_hdr;
 
             while (need_to_add > 0)
             {
@@ -314,12 +329,13 @@ check_send_occ_msg_dowork(struct context *c)
                 }
                 --need_to_add;
             }
-            dmsg(D_PACKET_CONTENT, "SENT OCC_MTU_LOAD min_int(%d-%d-%d-%d,%d) size=%d",
+            dmsg(D_PACKET_CONTENT, "SENT OCC_MTU_LOAD min_int(%d,%d)-%d-%d-%d-%d) size=%d",
                  c->c2.occ_mtu_load_size,
+                 c->c2.frame.buf.payload_size,
                  OCC_STRING_SIZE,
                  (int) sizeof(uint8_t),
-                 EXTRA_FRAME(&c->c2.frame),
-                 c->c2.frame.buf.payload_size,
+                 (int) payload_hdr,
+                 (int) proto_hdr,
                  BLEN(&c->c2.buf));
             doit = true;
         }
Signature · 1 line
2.33.0
#43 lore raw

PATCH 18/21 Remove extra_link from frame

In reply to Arne Schwabe

The previous commits removed any reads from this variable. So we can now safely remove it.

---
 src/openvpn/init.c  | 19 -------------------
 src/openvpn/mtu.c   |  1 -
 src/openvpn/mtu.h   | 13 -------------
 src/openvpn/socks.c | 11 +----------
 src/openvpn/socks.h |  2 --
 src/openvpn/ssl.c   |  1 -
 6 files changed, 1 insertion(+), 46 deletions(-)
src/openvpn/init.c +0 -19
index 72ffbfdfc..9d9dfe96a 100644
@@ -3099,14 +3099,6 @@ do_init_frame(struct context *c)
     }
 #endif /* USE_COMP */
 
-    /*
-     * Adjust frame size for UDP Socks support.
-     */
-    if (c->options.ce.socks_proxy_server)
-    {
-        socks_adjust_frame_parameters(&c->c2.frame, c->options.ce.proto);
-    }
-
     /*
      * Adjust frame size based on the --tun-mtu-extra parameter.
      */
@@ -3139,17 +3131,6 @@ do_init_frame(struct context *c)
 #endif
 #endif /* USE_COMP */
 
-    /* packets with peer-id (P_DATA_V2) need 3 extra bytes in frame (on client)
-     * and need link_mtu+3 bytes on socket reception (on server).
-     *
-     * accommodate receive path in f->extra_link, which has the side effect of
-     * also increasing send buffers (BUF_SIZE() macro), which need to be
-     * allocated big enough before receiving peer-id option from server.
-     *
-     * f->extra_frame is adjusted when peer-id option is push-received
-     */
-    frame_add_to_extra_link(&c->c2.frame, 3);
-
 #ifdef ENABLE_FRAGMENT
     /*
      * Set frame parameter for fragment code.  This is necessary because
src/openvpn/mtu.c +0 -1
index 62a66c8fd..9ca58c1f0 100644
@@ -215,7 +215,6 @@ frame_print(const struct frame *frame,
     buf_printf(&out, " EF:%d", frame->extra_frame);
     buf_printf(&out, " EB:%d", frame->extra_buffer);
     buf_printf(&out, " ET:%d", frame->extra_tun);
-    buf_printf(&out, " EL:%d", frame->extra_link);
     buf_printf(&out, " ]");
 
     msg(level, "%s", out.data);
src/openvpn/mtu.h +0 -13
index 06a00b5bb..1c479c9f5 100644
@@ -163,13 +163,6 @@ struct frame {
                                  *   which defaults to 0 for tun and 32
                                  *   (\c TAP_MTU_EXTRA_DEFAULT) for tap.
                                  *   */
-
-    int extra_link;             /**< Maximum number of bytes in excess of
-                                 *   external network interface's MTU that
-                                 *   might be read from or written to it.
-                                 *
-                                 *   Used by peer-id (3) and
-                                 *   socks UDP (10) */
 };
 
 /* Forward declarations, to prevent includes */
@@ -359,12 +352,6 @@ frame_add_to_extra_tun(struct frame *frame, const int increment)
     frame->extra_tun += increment;
 }
 
-static inline void
-frame_add_to_extra_link(struct frame *frame, const int increment)
-{
-    frame->extra_link += increment;
-}
-
 static inline void
 frame_add_to_extra_buffer(struct frame *frame, const int increment)
 {
src/openvpn/socks.c +1 -10
index 27a583313..6935e761a 100644
@@ -49,15 +49,6 @@
 
 #define UP_TYPE_SOCKS           "SOCKS Proxy"
 
-void
-socks_adjust_frame_parameters(struct frame *frame, int proto)
-{
-    if (proto == PROTO_UDP)
-    {
-        frame_add_to_extra_link(frame, 10);
-    }
-}
-
 struct socks_proxy_info *
 socks_proxy_new(const char *server,
                 const char *port,
@@ -610,7 +601,7 @@ socks_process_outgoing_udp(struct buffer *buf,
     /*
      * Get a 10 byte subset buffer prepended to buf --
      * we expect these bytes will be here because
-     * we allocated frame space in socks_adjust_frame_parameters.
+     * we always allocate space for these bytes
      */
     struct buffer head = buf_sub(buf, 10, true);
 
src/openvpn/socks.h +0 -2
index 9bda2e808..4ab30f55f 100644
@@ -42,8 +42,6 @@ struct socks_proxy_info {
     char authfile[256];
 };
 
-void socks_adjust_frame_parameters(struct frame *frame, int proto);
-
 struct socks_proxy_info *socks_proxy_new(const char *server,
                                          const char *port,
                                          const char *authfile);
src/openvpn/ssl.c +0 -1
index 79a5660bd..d55ffcdd2 100644
@@ -322,7 +322,6 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame
 
     /* inherit link MTU and extra_link from data channel */
     frame->link_mtu = data_channel_frame->link_mtu;
-    frame->extra_link = data_channel_frame->extra_link;
 
     /* set extra_frame */
     tls_adjust_frame_parameters(frame);
Signature · 1 line
2.33.0
#44 lore raw

PATCH 19/21 Replace TUN_MTU_SIZE with frame->tun_mtu

In reply to Arne Schwabe

This always uses the configured MTU size instead relying on the calculated MTU size.

---
 src/openvpn/forward.c |  2 +-
 src/openvpn/init.c    | 16 ++++++++--------
 src/openvpn/mtu.h     |  5 -----
 3 files changed, 9 insertions(+), 14 deletions(-)
src/openvpn/forward.c +1 -1
index 3f362e95d..5f8361d3e 100644
@@ -1379,7 +1379,7 @@ ipv6_send_icmp_unreachable(struct context *c, struct buffer *buf, bool client)
      * packet
      */
     int max_payload_size = min_int(MAX_ICMPV6LEN,
-                                   TUN_MTU_SIZE(&c->c2.frame) - icmpheader_len);
+                                   c->c2.frame.tun_mtu - icmpheader_len);
     int payload_len = min_int(max_payload_size, BLEN(&inputipbuf));
 
     pip6out.payload_len = htons(sizeof(struct openvpn_icmp6hdr) + payload_len);
src/openvpn/init.c +8 -8
index 9d9dfe96a..a8717c92a 100644
@@ -1735,7 +1735,7 @@ do_open_tun(struct context *c)
                                              c->options.dev_type,
                                              c->options.dev_node,
                                              &gc);
-        do_ifconfig(c->c1.tuntap, guess, TUN_MTU_SIZE(&c->c2.frame), c->c2.es,
+        do_ifconfig(c->c1.tuntap, guess, c->c2.frame.tun_mtu, c->c2.es,
                     &c->net_ctx);
     }
 
@@ -1766,7 +1766,7 @@ do_open_tun(struct context *c)
         && ifconfig_order() == IFCONFIG_AFTER_TUN_OPEN)
     {
         do_ifconfig(c->c1.tuntap, c->c1.tuntap->actual_name,
-                    TUN_MTU_SIZE(&c->c2.frame), c->c2.es, &c->net_ctx);
+                    c->c2.frame.tun_mtu, c->c2.es, &c->net_ctx);
     }
 
     /* run the up script */
@@ -1778,7 +1778,7 @@ do_open_tun(struct context *c)
                 c->c1.tuntap->adapter_index,
 #endif
                 dev_type_string(c->options.dev, c->options.dev_type),
-                TUN_MTU_SIZE(&c->c2.frame),
+                c->c2.frame.tun_mtu,
                 print_in_addr_t(c->c1.tuntap->local, IA_EMPTY_IF_UNDEF, &gc),
                 print_in_addr_t(c->c1.tuntap->remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                 "init",
@@ -1827,7 +1827,7 @@ else
                     c->c1.tuntap->adapter_index,
 #endif
                     dev_type_string(c->options.dev, c->options.dev_type),
-                    TUN_MTU_SIZE(&c->c2.frame),
+                    c->c2.frame.tun_mtu,
                     print_in_addr_t(c->c1.tuntap->local, IA_EMPTY_IF_UNDEF, &gc),
                     print_in_addr_t(c->c1.tuntap->remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                     "restart",
@@ -1906,7 +1906,7 @@ do_close_tun(struct context *c, bool force)
                             adapter_index,
 #endif
                             NULL,
-                            TUN_MTU_SIZE(&c->c2.frame),
+                            c->c2.frame.tun_mtu,
                             print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
                             print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                             "init",
@@ -1936,7 +1936,7 @@ do_close_tun(struct context *c, bool force)
                         adapter_index,
 #endif
                         NULL,
-                        TUN_MTU_SIZE(&c->c2.frame),
+                        c->c2.frame.tun_mtu,
                         print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
                         print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                         "init",
@@ -1974,7 +1974,7 @@ do_close_tun(struct context *c, bool force)
                             adapter_index,
 #endif
                             NULL,
-                            TUN_MTU_SIZE(&c->c2.frame),
+                            c->c2.frame.tun_mtu,
                             print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
                             print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc),
                             "restart",
@@ -2154,7 +2154,7 @@ void adjust_mtu_peerid(struct context *c)
     {
         msg(M_WARN, "OPTIONS IMPORT: WARNING: peer-id set, but link-mtu"
                     " fixed by config - reducing tun-mtu to %d, expect"
-                    " MTU problems", TUN_MTU_SIZE(&c->c2.frame));
+                    " MTU problems", c->c2.frame.tun_mtu);
     }
 }
 
src/openvpn/mtu.h +0 -5
index 1c479c9f5..b7c12b968 100644
@@ -181,11 +181,6 @@ struct options;
  */
 #define TUN_LINK_DELTA(f)        ((f)->extra_frame + (f)->extra_tun)
 
-/*
- * This is the size to "ifconfig" the tun or tap device.
- */
-#define TUN_MTU_SIZE(f)          ((f)->link_mtu - TUN_LINK_DELTA(f))
-
 /*
  * This is the maximum packet size that we need to be able to
  * read from or write to a tun or tap device.  For example,
Signature · 1 line
2.33.0
#45 lore raw

PATCH 20/21 Remove frame->link_mtu

In reply to Arne Schwabe
---
 src/openvpn/comp.c    |  8 --------
 src/openvpn/comp.h    |  2 --
 src/openvpn/forward.c |  4 ++--
 src/openvpn/init.c    | 31 +++----------------------------
 src/openvpn/mtu.c     |  1 -
 src/openvpn/mtu.h     | 16 ----------------
 src/openvpn/ssl.c     |  9 ---------
 7 files changed, 5 insertions(+), 66 deletions(-)
src/openvpn/comp.c +0 -8
index ad49b00b9..2d89e944d 100644
@@ -123,14 +123,6 @@ comp_add_to_extra_frame(struct frame *frame)
     frame_add_to_extra_frame(frame, COMP_PREFIX_LEN);
 }
 
-void
-comp_add_to_extra_buffer(struct frame *frame)
-{
-    /* Leave room for compression buffer to expand in worst case scenario
-     * where data is totally incompressible */
-    frame_add_to_extra_buffer(frame, COMP_EXTRA_BUFFER(EXPANDED_SIZE(frame)));
-}
-
 void
 comp_print_stats(const struct compress_context *compctx, struct status_output *so)
 {
src/openvpn/comp.h +0 -2
index 0d284e274..e42fc144f 100644
@@ -178,8 +178,6 @@ void comp_uninit(struct compress_context *compctx);
 
 void comp_add_to_extra_frame(struct frame *frame);
 
-void comp_add_to_extra_buffer(struct frame *frame);
-
 void comp_print_stats(const struct compress_context *compctx, struct status_output *so);
 
 void comp_generate_peer_info_string(const struct compress_options *opt, struct buffer *out);
src/openvpn/forward.c +2 -2
index 5f8361d3e..b6e9eabbb 100644
@@ -1544,7 +1544,7 @@ process_outgoing_link(struct context *c)
 
     perf_push(PERF_PROC_OUT_LINK);
 
-    if (c->c2.to_link.len > 0 && c->c2.to_link.len <= EXPANDED_SIZE(&c->c2.frame))
+    if (c->c2.to_link.len > 0 && c->c2.to_link.len <= c->c2.frame.buf.payload_size)
     {
         /*
          * Setup for call to send/sendto which will send
@@ -1672,7 +1672,7 @@ process_outgoing_link(struct context *c)
             msg(D_LINK_ERRORS, "TCP/UDP packet too large on write to %s (tried=%d,max=%d)",
                 print_link_socket_actual(c->c2.to_link_addr, &gc),
                 c->c2.to_link.len,
-                EXPANDED_SIZE(&c->c2.frame));
+                c->c2.frame.buf.payload_size);
         }
     }
 
src/openvpn/init.c +3 -28
index a8717c92a..abdf6aaf3 100644
@@ -2140,24 +2140,6 @@ pull_permission_mask(const struct context *c)
     return flags;
 }
 
-static
-void adjust_mtu_peerid(struct context *c)
-{
-    frame_add_to_extra_frame(&c->c2.frame, 3);     /* peer-id overhead */
-    if (!c->options.ce.link_mtu_defined)
-    {
-        frame_add_to_link_mtu(&c->c2.frame, 3);
-        msg(D_PUSH, "OPTIONS IMPORT: adjusting link_mtu to %d",
-            EXPANDED_SIZE(&c->c2.frame));
-    }
-    else
-    {
-        msg(M_WARN, "OPTIONS IMPORT: WARNING: peer-id set, but link-mtu"
-                    " fixed by config - reducing tun-mtu to %d, expect"
-                    " MTU problems", c->c2.frame.tun_mtu);
-    }
-}
-
 static bool
 do_deferred_p2p_ncp(struct context *c)
 {
@@ -2166,11 +2148,6 @@ do_deferred_p2p_ncp(struct context *c)
         return true;
     }
 
-    if (c->c2.tls_multi->use_peer_id)
-    {
-        adjust_mtu_peerid(c);
-    }
-
     struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
 
     const char *ncp_cipher = get_p2p_ncp_cipher(session, c->c2.tls_multi->peer_info,
@@ -2292,7 +2269,6 @@ do_deferred_options(struct context *c, const unsigned int found)
         msg(D_PUSH, "OPTIONS IMPORT: peer-id set");
         c->c2.tls_multi->use_peer_id = true;
         c->c2.tls_multi->peer_id = c->options.peer_id;
-        adjust_mtu_peerid(c);
     }
 
     /* process (potentially pushed) crypto options */
@@ -3032,8 +3008,8 @@ do_init_frame_tls(struct context *c)
     if (c->c2.tls_multi)
     {
         tls_multi_init_finalize(c->c2.tls_multi, &c->c2.frame);
-        ASSERT(EXPANDED_SIZE(&c->c2.tls_multi->opt.frame) <=
-               EXPANDED_SIZE(&c->c2.frame));
+        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,
                     "Control Channel MTU parms");
     }
@@ -3125,9 +3101,8 @@ do_init_frame(struct context *c)
      * Modify frame parameters if compression is compiled in.
      * Should be called after frame_finalize_options.
      */
-    comp_add_to_extra_buffer(&c->c2.frame);
 #ifdef ENABLE_FRAGMENT
-    comp_add_to_extra_buffer(&c->c2.frame_fragment_omit); /* omit compression frame delta from final frame_fragment */
+    /*TODO:frame comp_add_to_extra_buffer(&c->c2.frame_fragment_omit);  omit compression frame delta from final frame_fragment */
 #endif
 #endif /* USE_COMP */
 
src/openvpn/mtu.c +0 -1
index 9ca58c1f0..eb823165a 100644
@@ -211,7 +211,6 @@ frame_print(const struct frame *frame,
     buf_printf(&out, " headroom:%d", frame->buf.headroom);
     buf_printf(&out, " payload:%d", frame->buf.payload_size);
     buf_printf(&out, " tailroom:%d", frame->buf.tailroom);
-    buf_printf(&out, " L:%d", frame->link_mtu);
     buf_printf(&out, " EF:%d", frame->extra_frame);
     buf_printf(&out, " EB:%d", frame->extra_buffer);
     buf_printf(&out, " ET:%d", frame->extra_tun);
src/openvpn/mtu.h +0 -16
index b7c12b968..72cf80917 100644
@@ -110,9 +110,6 @@ struct frame {
                                   *  decryption/encryption or compression. */
     } buf;
 
-    int link_mtu;               /**< Maximum packet size to be sent over
-                                 *   the external network interface. */
-
     unsigned int mss_fix;       /**< The actual MSS value that should be
                                  *   written to the payload packets. This
                                  *   is the value for IPv4 TCP packets. For
@@ -189,13 +186,6 @@ struct options;
  */
 #define PAYLOAD_SIZE(f)          ((f)->buf.payload_size)
 
-/*
- * Max size of a payload packet after encryption, compression, etc.
- * overhead is added.
- */
-#define EXPANDED_SIZE(f)         ((f)->link_mtu)
-#define EXPANDED_SIZE_MIN(f)     (TUN_MTU_MIN + TUN_LINK_DELTA(f))
-
 /*
  * Control buffer headroom allocations to allow for efficient prepending.
  */
@@ -323,12 +313,6 @@ const char *format_extended_socket_error(int fd, int *mtu, struct gc_arena *gc);
  * frame member adjustment functions
  */
 
-static inline void
-frame_add_to_link_mtu(struct frame *frame, const int increment)
-{
-    frame->link_mtu += increment;
-}
-
 static inline void
 frame_add_to_extra_frame(struct frame *frame, const unsigned int increment)
 {
src/openvpn/ssl.c +0 -9
index d55ffcdd2..bb1ff04cc 100644
@@ -320,17 +320,11 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame
      * if --tls-auth is enabled.
      */
 
-    /* inherit link MTU and extra_link from data channel */
-    frame->link_mtu = data_channel_frame->link_mtu;
-
     /* set extra_frame */
     tls_adjust_frame_parameters(frame);
     reliable_ack_adjust_frame_parameters(frame, CONTROL_SEND_ACK_MAX);
     frame_add_to_extra_frame(frame, SID_SIZE + sizeof(packet_id_type));
 
-    /* set dynamic link MTU to cap control channel packets at 1250 bytes */
-    ASSERT(TUN_LINK_DELTA(frame) < min_int(frame->link_mtu, 1250));
-
     /* calculate the maximum overhead that control channel frames may have */
     int overhead = 0;
 
@@ -1923,9 +1917,6 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
 
     if (frame_fragment)
     {
-        frame_remove_from_extra_frame(frame_fragment, crypto_max_overhead());
-        crypto_adjust_frame_parameters(frame_fragment, &session->opt->key_type,
-                                       options->replay, packet_id_long_form);
         frame_calculate_dynamic(frame_fragment, &session->opt->key_type, options, lsi);
         frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
     }
Signature · 1 line
2.33.0
#46 lore raw

PATCH 21/21 Remove frame.extra_frame and frame.extra_buffer

In reply to Arne Schwabe
---
 src/openvpn/comp.c      |  7 ------
 src/openvpn/comp.h      |  2 --
 src/openvpn/crypto.c    | 37 ---------------------------
 src/openvpn/crypto.h    |  7 ------
 src/openvpn/fragment.c  |  3 ---
 src/openvpn/init.c      | 56 -----------------------------------------
 src/openvpn/mtu.c       | 14 -----------
 src/openvpn/mtu.h       | 42 ++-----------------------------
 src/openvpn/reliable.c  |  7 ------
 src/openvpn/reliable.h  |  3 ---
 src/openvpn/socket.c    | 10 --------
 src/openvpn/socket.h    |  2 --
 src/openvpn/ssl.c       | 21 ----------------
 src/openvpn/ssl.h       |  5 ----
 src/openvpn/tls_crypt.c | 10 --------
 src/openvpn/tls_crypt.h |  5 ----
 16 files changed, 2 insertions(+), 229 deletions(-)
src/openvpn/comp.c +0 -7
index 2d89e944d..33bf21a7a 100644
@@ -116,13 +116,6 @@ comp_uninit(struct compress_context *compctx)
     }
 }
 
-void
-comp_add_to_extra_frame(struct frame *frame)
-{
-    /* Leave room for our one-byte compressed/didn't-compress prefix byte. */
-    frame_add_to_extra_frame(frame, COMP_PREFIX_LEN);
-}
-
 void
 comp_print_stats(const struct compress_context *compctx, struct status_output *so)
 {
src/openvpn/comp.h +0 -2
index e42fc144f..d059d6cd3 100644
@@ -176,8 +176,6 @@ struct compress_context *comp_init(const struct compress_options *opt);
 
 void comp_uninit(struct compress_context *compctx);
 
-void comp_add_to_extra_frame(struct frame *frame);
-
 void comp_print_stats(const struct compress_context *compctx, struct status_output *so);
 
 void comp_generate_peer_info_string(const struct compress_options *opt, struct buffer *out);
src/openvpn/crypto.c +0 -37
index b4b8ca54b..f4f23427b 100644
@@ -722,43 +722,6 @@ calculate_crypto_overhead(const struct key_type *kt,
     return crypto_overhead;
 }
 
-void
-crypto_adjust_frame_parameters(struct frame *frame,
-                               const struct key_type *kt,
-                               bool packet_id,
-                               bool packet_id_long_form)
-{
-    unsigned int crypto_overhead = 0;
-
-    if (packet_id)
-    {
-        crypto_overhead += packet_id_size(packet_id_long_form);
-    }
-
-    if (cipher_defined(kt->cipher))
-    {
-        crypto_overhead += cipher_kt_iv_size(kt->cipher);
-
-        if (cipher_kt_mode_aead(kt->cipher))
-        {
-            crypto_overhead += cipher_kt_tag_size(kt->cipher);
-        }
-
-        /* extra block required by cipher_ctx_update() */
-        crypto_overhead += cipher_kt_block_size(kt->cipher);
-    }
-
-    if (md_defined(kt->digest))
-    {
-        crypto_overhead += md_kt_size(kt->digest);
-    }
-
-    frame_add_to_extra_frame(frame, crypto_overhead);
-
-    msg(D_MTU_DEBUG, "%s: Adjusting frame parameters for crypto by %u bytes",
-        __func__, crypto_overhead);
-}
-
 unsigned int
 crypto_max_overhead(void)
 {
src/openvpn/crypto.h +0 -7
index 5a67b7ac1..b039c3b6b 100644
@@ -408,13 +408,6 @@ bool crypto_check_replay(struct crypto_options *opt,
                          const struct packet_id_net *pin, const char *error_prefix,
                          struct gc_arena *gc);
 
-
-/** Calculate crypto overhead and adjust frame to account for that */
-void crypto_adjust_frame_parameters(struct frame *frame,
-                                    const struct key_type *kt,
-                                    bool packet_id,
-                                    bool packet_id_long_form);
-
 /** Calculate the maximum overhead that our encryption has
  * on a packet. This does not include needed additional buffer size
  *
src/openvpn/fragment.c +0 -3
index ce8cd3489..eb90dcacb 100644
@@ -96,9 +96,6 @@ fragment_init(struct frame *frame)
      * fragment_master assume an initial CLEAR */
     ALLOC_OBJ_CLEAR(ret, struct fragment_master);
 
-    /* add in the size of our contribution to the expanded frame size */
-    frame_add_to_extra_frame(frame, sizeof(fragment_header_type));
-
     /*
      * Outgoing sequence ID is randomized to reduce
      * the probability of sequence number collisions
src/openvpn/init.c +0 -56

110 lines not on this page · raw message

src/openvpn/mtu.c +0 -14
index eb823165a..3783e5315 100644
@@ -179,18 +179,6 @@ calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
     return payload + overhead;
 }
 
-/*
- * Move extra_frame octets into extra_tun.  Used by fragmenting code
- * to adjust frame relative to its position in the buffer processing
- * queue.
- */
-void
-frame_subtract_extra(struct frame *frame, const struct frame *src)
-{
-    frame->extra_frame -= src->extra_frame;
-    frame->extra_tun   += src->extra_frame;
-}
-
 void
 frame_print(const struct frame *frame,
             int level,
@@ -211,8 +199,6 @@ frame_print(const struct frame *frame,
     buf_printf(&out, " headroom:%d", frame->buf.headroom);
     buf_printf(&out, " payload:%d", frame->buf.payload_size);
     buf_printf(&out, " tailroom:%d", frame->buf.tailroom);
-    buf_printf(&out, " EF:%d", frame->extra_frame);
-    buf_printf(&out, " EB:%d", frame->extra_buffer);
     buf_printf(&out, " ET:%d", frame->extra_tun);
     buf_printf(&out, " ]");
 
src/openvpn/mtu.h +2 -40

77 lines not on this page · raw message

src/openvpn/reliable.c +0 -7
index 08c9ab192..6f9971010 100644
@@ -253,13 +253,6 @@ error:
     return false;
 }
 
-/* add to extra_frame the maximum number of bytes we will need for reliable_ack_write */
-void
-reliable_ack_adjust_frame_parameters(struct frame *frame, int max)
-{
-    frame_add_to_extra_frame(frame, ACK_SIZE(max));
-}
-
 /* print a reliable ACK record coming off the wire */
 const char *
 reliable_ack_print(struct buffer *buf, bool verbose, struct gc_arena *gc)
src/openvpn/reliable.h +0 -3
index 693abb3c7..cbd9cc8f1 100644
@@ -207,9 +207,6 @@ void reliable_init(struct reliable *rel, int buf_size, int offset, int array_siz
  */
 void reliable_free(struct reliable *rel);
 
-/* add to extra_frame the maximum number of bytes we will need for reliable_ack_write */
-void reliable_ack_adjust_frame_parameters(struct frame *frame, int max);
-
 /** @} name Functions for initialization and cleanup */
 
 
src/openvpn/socket.c +0 -10
index fe1dfb315..93b857f01 100644
@@ -2285,16 +2285,6 @@ link_socket_close(struct link_socket *sock)
     }
 }
 
-/* for stream protocols, allow for packet length prefix */
-void
-socket_adjust_frame_parameters(struct frame *frame, int proto)
-{
-    if (link_socket_proto_connection_oriented(proto))
-    {
-        frame_add_to_extra_frame(frame, sizeof(packet_size_type));
-    }
-}
-
 void
 setenv_trusted(struct env_set *es, const struct link_socket_info *info)
 {
src/openvpn/socket.h +0 -2
index a43ed80b5..2ad0e1b33 100644
@@ -298,8 +298,6 @@ void link_socket_init_phase2(struct context *c);
 
 void do_preresolve(struct context *c);
 
-void socket_adjust_frame_parameters(struct frame *frame, int proto);
-
 void link_socket_close(struct link_socket *sock);
 
 void sd_close(socket_descriptor_t *sd);
src/openvpn/ssl.c +0 -21
index bb1ff04cc..4012ebf15 100644
@@ -295,18 +295,6 @@ tls_limit_reneg_bytes(const char *ciphername, int *reneg_bytes)
     }
 }
 
-/*
- * Max number of bytes we will add
- * for data structures common to both
- * data and control channel packets.
- * (opcode only).
- */
-void
-tls_adjust_frame_parameters(struct frame *frame)
-{
-    frame_add_to_extra_frame(frame, 1); /* space for opcode */
-}
-
 /*
  * Max number of bytes we will add
  * to control channel packet.
@@ -320,11 +308,6 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame
      * if --tls-auth is enabled.
      */
 
-    /* set extra_frame */
-    tls_adjust_frame_parameters(frame);
-    reliable_ack_adjust_frame_parameters(frame, CONTROL_SEND_ACK_MAX);
-    frame_add_to_extra_frame(frame, SID_SIZE + sizeof(packet_id_type));
-
     /* calculate the maximum overhead that control channel frames may have */
     int overhead = 0;
 
@@ -1900,10 +1883,6 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
         session->opt->crypto_flags |= CO_PACKET_ID_LONG_FORM;
     }
 
-    /* Update frame parameters: undo worst-case overhead, add actual overhead */
-    frame_remove_from_extra_frame(frame, crypto_max_overhead());
-    crypto_adjust_frame_parameters(frame, &session->opt->key_type,
-                                   options->replay, packet_id_long_form);
     frame_calculate_dynamic(frame, &session->opt->key_type, options, lsi);
 
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
src/openvpn/ssl.h +0 -5
index e566acd81..5e1c7a2a2 100644
@@ -471,11 +471,6 @@ void ssl_put_auth_challenge(const char *cr_str);
 
 #endif
 
-/*
- * Reserve any extra space required on frames.
- */
-void tls_adjust_frame_parameters(struct frame *frame);
-
 /*
  * Send a payload over the TLS control channel
  */
src/openvpn/tls_crypt.c +0 -10
index 543e2afd0..26f8b8ddf 100644
@@ -89,16 +89,6 @@ tls_crypt_init_key(struct key_ctx_bi *key, const char *key_file,
                             "Control Channel Encryption", "tls-crypt");
 }
 
-void
-tls_crypt_adjust_frame_parameters(struct frame *frame)
-{
-    frame_add_to_extra_frame(frame, tls_crypt_buf_overhead());
-
-    msg(D_MTU_DEBUG, "%s: Adjusting frame parameters for tls-crypt by %i bytes",
-        __func__, tls_crypt_buf_overhead());
-}
-
-
 bool
 tls_crypt_wrap(const struct buffer *src, struct buffer *dst,
                struct crypto_options *opt)
src/openvpn/tls_crypt.h +0 -5
index 81d0a10ee..928ff5475 100644
@@ -123,11 +123,6 @@ void tls_crypt_init_key(struct key_ctx_bi *key, const char *key_file,
  */
 int tls_crypt_buf_overhead(void);
 
-/**
- * Adjust frame parameters for --tls-crypt overhead.
- */
-void tls_crypt_adjust_frame_parameters(struct frame *frame);
-
 /**
  * Wrap a control channel packet (both authenticates and encrypts the data).
  *
Signature · 1 line
2.33.0
#47 lore raw

PATCH v2 7/21 Add helper functions to calculate header/payload sizes

In reply to Arne Schwabe

These functions are intended to lay the groundwork to later replace the distributed frame calculations and centralise the calculation in one place.

Patch v2: clarify that the socks comments is assuming IPv4

---
 src/openvpn/crypto.c | 55 ++++++++++++++++++++++++++++
 src/openvpn/crypto.h | 18 ++++++++++
 src/openvpn/mtu.c    | 86 ++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/mtu.h    | 54 ++++++++++++++++++++++++++++
 4 files changed, 213 insertions(+)
src/openvpn/crypto.c +55 -0
index dc7ba5422..a58a48007 100644
@@ -667,6 +667,61 @@ openvpn_decrypt(struct buffer *buf, struct buffer work,
     return ret;
 }
 
+unsigned int
+calculate_crypto_overhead(const struct key_type *kt,
+                          bool packet_id,
+                          bool packet_id_long_form,
+                          unsigned int payload_size,
+                          bool occ)
+{
+    unsigned int crypto_overhead = 0;
+
+    /* No encryption */
+    if (packet_id)
+    {
+        crypto_overhead += packet_id_size(packet_id_long_form);
+    }
+
+    if (cipher_kt_mode_aead(kt->cipher))
+    {
+        /* For AEAD ciphers, we basically use a stream cipher/CTR for
+         * the encryption, so no overhead apart from the extra bytes
+         * we add */
+        crypto_overhead += cipher_kt_tag_size(kt->cipher);
+
+        if (occ)
+        {
+            /* the frame calculation of old clients adds these to the link-mtu
+             * even though they are not part of the actual packet */
+            crypto_overhead += cipher_kt_iv_size(kt->cipher);
+            crypto_overhead += cipher_kt_block_size(kt->cipher);
+        }
+    }
+    else
+    {
+        if (cipher_defined(kt->cipher))
+        {
+            /* CBC, OFB or CFB mode */
+            /* This is a worst case upper bound of needing to add
+             * a full extra block for padding when the payload
+             * is exactly a multiple of the block size */
+            if (occ || (cipher_kt_mode_cbc(kt->cipher) &&
+                (payload_size % cipher_kt_block_size(kt->cipher) == 0)))
+            {
+                crypto_overhead += cipher_kt_block_size(kt->cipher);
+            }
+            /* IV is always added (no-iv has been removed a while ago) */
+            crypto_overhead += cipher_kt_iv_size(kt->cipher);
+        }
+        if (md_defined(kt->digest))
+        {
+            crypto_overhead += md_kt_size(kt->digest);
+        }
+    }
+
+    return crypto_overhead;
+}
+
 void
 crypto_adjust_frame_parameters(struct frame *frame,
                                const struct key_type *kt,
src/openvpn/crypto.h +18 -0
index ad3543c1c..5a67b7ac1 100644
@@ -415,6 +415,24 @@ void crypto_adjust_frame_parameters(struct frame *frame,
                                     bool packet_id,
                                     bool packet_id_long_form);
 
+/** Calculate the maximum overhead that our encryption has
+ * on a packet. This does not include needed additional buffer size
+ *
+ * @param kt            Struct with the crypto algorithm to use
+ * @param packet_id     Whether packet_id is used
+ * @param packet_id_long_form Whether the packet id has the long form
+ * @param payload_size   payload size, only used if occ is false
+ * @param occ           if true calculates the overhead for crypto in the same
+ *                      incorrect way as all previous OpenVPN versions did, to
+ *                      end up with identical numbers for OCC compatibility
+ */
+unsigned int
+calculate_crypto_overhead(const struct key_type *kt,
+                           bool packet_id,
+                           bool packet_id_long_form,
+                           unsigned int payload_size,
+                           bool occ);
+
 /** Return the worst-case OpenVPN crypto overhead (in bytes) */
 unsigned int crypto_max_overhead(void);
 
src/openvpn/mtu.c +86 -0

98 lines not on this page · raw message

src/openvpn/mtu.h +54 -0

60 lines not on this page · raw message

Signature · 1 line
2.33.0
#48 lore raw

Re: [Openvpn-devel] [PATCH v2 07/21] Add helper functions to calculate header/payload sizes

Quoted 10 lines

Arne Schwabe <[email protected]...> hat am 14.12.2021 16:09 geschrieben:

These functions are intended to lay the groundwork to later replace the distributed frame calculations and centralise the calculation in one place.

Patch v2: clarify that the socks comments is assuming IPv4

You addressed by first question, but not my others.

[...]

Quoted 4 lines
src/openvpn/mtu.c +0 -0
index 0ab716d7a..0da1dadfa 100644

[...]

Quoted 33 lines
src/openvpn/mtu.c +29 -0
@@ -51,6 +52,91 @@ alloc_buf_sock_tun(struct buffer *buf,
     ASSERT(buf_safe(buf, 0));
 }
 
+size_t
+frame_calculate_protocol_header_size(const struct key_type *kt,
+                                     const struct options *options,
+                                     unsigned int payload_size,
+                                     bool occ)
+{
+    /* Sum of all the overhead that reduces the usable packet size */
+    size_t header_size = 0;
+
+    bool tlsmode = options->tls_server || options->tls_client;
+
+    /* A socks proxy adds 10 byte of extra header to each packet
+     * (we only support Socks with IPv4, this value is different for IPv6) */
+    if (options->ce.socks_proxy_server && proto_is_udp(options->ce.proto))
+    {
+        header_size += 10;
+    }
+
+    /* TCP stream based packets have a 16 bit length field */
+    if (proto_is_tcp(options->ce.proto))
+    {
+        header_size += 2;
+    }
+
+    /* Add the opcode and peerid */
+    if (tlsmode)
+    {
+        header_size += options->use_peer_id ? 4 : 1;
+    }

src/openvpn/mtu.c alloc_buf_sock_tun(struct buffer *buf, · line 93

Quoted 10 lines
src/openvpn/mtu.c +10 -0
+
+    /* Add the crypto overhead */
+    bool packet_id = options->replay;
+    bool packet_id_long_form = !tlsmode || cipher_kt_mode_ofb_cfb(kt->cipher);
+
+    /* For figuring out the crypto overhead, we need to use the real payload
+     * including all extra headers that also get encrypted */
+    header_size += calculate_crypto_overhead(kt, packet_id,
+                                             packet_id_long_form,
+                                             payload_size, occ);

My question was why you mention "extra headers" here but that doesn't seem to be reflected in the code itself. After looking further into the code it seems to me that you allude here to the frame_caculate_payload_overhead function? But you don't use it here, you assume that the caller of this function already called it. So maybe instead of putting this comment here it would be better to put that into the documentation of this function in the header?

Regards,
--
Frank Lichtenheld

#49 lore raw

Re: [Openvpn-devel] [PATCH v2 07/21] Add helper functions to calculate header/payload sizes

Quoted 8 lines

src/openvpn/mtu.c alloc_buf_sock_tun(struct buffer *buf, · line 93

Quoted 7 lines
src/openvpn/mtu.c +6 -0
+
+    /* For figuring out the crypto overhead, we need to use the real payload
+     * including all extra headers that also get encrypted */
+    header_size += calculate_crypto_overhead(kt, packet_id,
+                                             packet_id_long_form,
+                                             payload_size, occ);
Quoted 5 lines

My question was why you mention "extra headers" here but that doesn't seem to be reflected in the code itself. After looking further into the code it seems to me that you allude here to the frame_caculate_payload_overhead function? But you don't use it here, you assume that the caller of this function already called it. So maybe instead of putting this comment here it would be better to put that into the documentation of this function in the header?

Ah sorry, I overlooked that. A better comment would be:

/* For figuring out the crypto overhead, we need to use the real
payload size including the extra headers that also get encrypted as part of the payload like compression and fragmentation header */

Anr

#50 lore raw

PATCH v2 7/21 Add helper functions to calculate header/payload sizes

In reply to Arne Schwabe

These functions are intended to lay the groundwork to later replace the distributed frame calculations and centralise the calculation in one place.

Patch v2: clarify that the socks comments is assuming IPv4 and improve other
comments

---
 src/openvpn/crypto.c | 55 ++++++++++++++++++++++++++++
 src/openvpn/crypto.h | 18 +++++++++
 src/openvpn/mtu.c    | 87 ++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/mtu.h    | 54 +++++++++++++++++++++++++++
 4 files changed, 214 insertions(+)
src/openvpn/crypto.c +55 -0
index dc7ba542..5626e2b6 100644
@@ -667,6 +667,61 @@ openvpn_decrypt(struct buffer *buf, struct buffer work,
     return ret;
 }
 
+unsigned int
+calculate_crypto_overhead(const struct key_type *kt,
+                          bool packet_id,
+                          bool packet_id_long_form,
+                          unsigned int payload_size,
+                          bool occ)
+{
+    unsigned int crypto_overhead = 0;
+
+    /* We always have a packet id, no matter if encrypted or unencrypted */
+    if (packet_id)
+    {
+        crypto_overhead += packet_id_size(packet_id_long_form);
+    }
+
+    if (cipher_kt_mode_aead(kt->cipher))
+    {
+        /* For AEAD ciphers, we basically use a stream cipher/CTR for
+         * the encryption, so no overhead apart from the extra bytes
+         * we add */
+        crypto_overhead += cipher_kt_tag_size(kt->cipher);
+
+        if (occ)
+        {
+            /* the frame calculation of old clients adds these to the link-mtu
+             * even though they are not part of the actual packet */
+            crypto_overhead += cipher_kt_iv_size(kt->cipher);
+            crypto_overhead += cipher_kt_block_size(kt->cipher);
+        }
+    }
+    else
+    {
+        if (cipher_defined(kt->cipher))
+        {
+            /* CBC, OFB or CFB mode */
+            /* This is a worst case upper bound of needing to add
+             * a full extra block for padding when the payload
+             * is exactly a multiple of the block size */
+            if (occ || (cipher_kt_mode_cbc(kt->cipher) &&
+                (payload_size % cipher_kt_block_size(kt->cipher) == 0)))
+            {
+                crypto_overhead += cipher_kt_block_size(kt->cipher);
+            }
+            /* IV is always added (no-iv has been removed a while ago) */
+            crypto_overhead += cipher_kt_iv_size(kt->cipher);
+        }
+        if (md_defined(kt->digest))
+        {
+            crypto_overhead += md_kt_size(kt->digest);
+        }
+    }
+
+    return crypto_overhead;
+}
+
 void
 crypto_adjust_frame_parameters(struct frame *frame,
                                const struct key_type *kt,
src/openvpn/crypto.h +18 -0
index ad3543c1..5a67b7ac 100644
@@ -415,6 +415,24 @@ void crypto_adjust_frame_parameters(struct frame *frame,
                                     bool packet_id,
                                     bool packet_id_long_form);
 
+/** Calculate the maximum overhead that our encryption has
+ * on a packet. This does not include needed additional buffer size
+ *
+ * @param kt            Struct with the crypto algorithm to use
+ * @param packet_id     Whether packet_id is used
+ * @param packet_id_long_form Whether the packet id has the long form
+ * @param payload_size   payload size, only used if occ is false
+ * @param occ           if true calculates the overhead for crypto in the same
+ *                      incorrect way as all previous OpenVPN versions did, to
+ *                      end up with identical numbers for OCC compatibility
+ */
+unsigned int
+calculate_crypto_overhead(const struct key_type *kt,
+                           bool packet_id,
+                           bool packet_id_long_form,
+                           unsigned int payload_size,
+                           bool occ);
+
 /** Return the worst-case OpenVPN crypto overhead (in bytes) */
 unsigned int crypto_max_overhead(void);
 
src/openvpn/mtu.c +87 -0

99 lines not on this page · raw message

src/openvpn/mtu.h +54 -0

60 lines not on this page · raw message

Signature · 1 line
2.33.0
Acked-by on v2 7/21
#51 lore raw

[Openvpn-devel] [PATCH applied] Re: Add helper functions to calculate header/payload sizes

With the latest comment changes (v2/2) and wiscii's grammar fix these code parts are now much easier to understand.

There is not much to actually test yet, as the new functions are not called. I have visually compared with crypto_adjust_frame_parameters() and "it seems to do the smae thing" (if(occ)).

The comments in mtu.h are helpful to understand the terminology used regarding "header" and "payload (including compression *header*").

Your patch has been applied to the master branch.

commit de018b5e93d6a85f39e5e8ecd3c986320fde8a2f

Author: Arne Schwabe
Date:   Wed Dec 29 17:34:45 2021 +0100
     Add helper functions to calculate header/payload sizes

     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]/msg23476.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#52 lore raw

PATCH v2 8/21 Decouple MSS fix calculation from frame calculation

In reply to Arne Schwabe

This consolidates the MSS fix calculation into a single function instead having it distributed all over the code. It also calculates the real wire overhead without extra sizes for buffer etc.

Patch v2: improve comment

---
 src/openvpn/forward.c |  5 ++---
 src/openvpn/init.c    |  3 ++-
 src/openvpn/mss.c     | 40 ++++++++++++++++++++++++++++++++++++++++
 src/openvpn/mss.h     |  6 ++++++
 src/openvpn/mtu.c     |  9 ---------
 src/openvpn/mtu.h     | 10 ++++++----
 src/openvpn/proto.h   | 11 -----------
 src/openvpn/ssl.c     |  3 ++-
 8 files changed, 58 insertions(+), 29 deletions(-)
src/openvpn/forward.c +2 -3
index 29efcd3b9..f82386a1d 100644
@@ -1493,7 +1493,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
                 /* possibly alter the TCP MSS */
                 if (flags & PIP_MSSFIX)
                 {
-                    mss_fixup_ipv4(&ipbuf, MTU_TO_MSS(TUN_MTU_SIZE_DYNAMIC(&c->c2.frame)));
+                    mss_fixup_ipv4(&ipbuf, c->c2.frame.mss_fix);
                 }
 
                 /* possibly do NAT on packet */
@@ -1517,8 +1517,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
                 /* possibly alter the TCP MSS */
                 if (flags & PIP_MSSFIX)
                 {
-                    mss_fixup_ipv6(&ipbuf,
-                                   MTU_TO_MSS(TUN_MTU_SIZE_DYNAMIC(&c->c2.frame)));
+                    mss_fixup_ipv6(&ipbuf, c->c2.frame.mss_fix);
                 }
                 if (!(flags & PIP_OUTGOING) && (flags
                                                 &(PIPV6_IMCP_NOHOST_CLIENT | PIPV6_IMCP_NOHOST_SERVER)))
src/openvpn/init.c +2 -1
index eb2678116..462edc01e 100644
@@ -53,6 +53,7 @@
 #include "tls_crypt.h"
 #include "forward.h"
 #include "auth_token.h"
+#include "mss.h"
 
 #include "memdbg.h"
 
@@ -4156,7 +4157,7 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
 #endif
 
     /* initialize dynamic MTU variable */
-    frame_init_mssfix(&c->c2.frame, &c->options);
+    frame_calculate_mssfix(&c->c2.frame, &c->c1.ks.key_type, &c->options);
 
     /* bind the TCP/UDP socket */
     if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
src/openvpn/mss.c +40 -0
index aa5b68ce9..d2842aac5 100644
@@ -30,6 +30,8 @@
 #include "syshead.h"
 #include "error.h"
 #include "mss.h"
+#include "crypto.h"
+#include "ssl_common.h"
 #include "memdbg.h"
 
 /*
@@ -204,3 +206,41 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss)
         }
     }
 }
+
+void
+frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
+                       const struct options *options)
+{
+    if (options->ce.mssfix == 0)
+    {
+        return;
+    }
+
+    unsigned int payload_size;
+    unsigned int overhead;
+
+
+    payload_size = frame_calculate_payload_size(frame, options);
+
+    overhead = frame_calculate_protocol_header_size(kt, options,
+                                                    payload_size, false);
+
+    /* Calculate the number of bytes that the payload differs from the payload
+     * MTU. This are fragment/compression/ethernet headers */
+    unsigned payload_overhead = frame_calculate_payload_overhead(frame, options, true);
+
+    /* We are in a "liberal" position with respect to MSS,
+     * i.e. we assume that MSS can be calculated from MTU
+     * by subtracting out only the IP and TCP header sizes
+     * without options.
+     *
+     * (RFC 879, section 7). */
+
+    /* Add 20 bytes for the IPv4 header and 20 byte for the TCP header of the
+     * payload, the mssfix method will add 20 extra if payload is IPv6 */
+    overhead += 20 + 20;
+
+    /* Calculate the maximum MSS value from the max link layer size specified
+     * by ce.mssfix */
+    frame->mss_fix = options->ce.mssfix - overhead - payload_overhead;
+}
\ No newline at end of file
src/openvpn/mss.h +6 -0
index 41254e2a6..856f4c4e3 100644
@@ -26,6 +26,8 @@
 
 #include "proto.h"
 #include "error.h"
+#include "mtu.h"
+#include "ssl_common.h"
 
 void mss_fixup_ipv4(struct buffer *buf, int maxmss);
 
@@ -33,4 +35,8 @@ void mss_fixup_ipv6(struct buffer *buf, int maxmss);
 
 void mss_fixup_dowork(struct buffer *buf, uint16_t maxmss);
 
+/** Set the --mssfix option. */
+void frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
+                            const struct options *options);
+
 #endif
src/openvpn/mtu.c +0 -9
index 0da1dadfa..7e5c3c24d 100644
@@ -211,15 +211,6 @@ frame_subtract_extra(struct frame *frame, const struct frame *src)
     frame->extra_tun   += src->extra_frame;
 }
 
-void
-frame_init_mssfix(struct frame *frame, const struct options *options)
-{
-    if (options->ce.mssfix)
-    {
-        frame_set_mtu_dynamic(frame, options->ce.mssfix, SET_MTU_UPPER_BOUND);
-    }
-}
-
 void
 frame_print(const struct frame *frame,
             int level,
src/openvpn/mtu.h +6 -4
index 5ad0931fd..ae83d3e7a 100644
@@ -94,6 +94,12 @@ struct frame {
     int link_mtu;               /**< Maximum packet size to be sent over
                                  *   the external network interface. */
 
+    unsigned int mss_fix;      /**< The actual MSS value that should be
+                                 *   written to the payload packets. This
+                                 *   is the value for IPv4 TCP packets. For
+                                 *   IPv6 packets another 20 bytes must
+                                 *   be subtracted */
+
     int link_mtu_dynamic;       /**< Dynamic MTU value for the external
                                  *   network interface. */
 
@@ -152,7 +158,6 @@ struct options;
  * This is the size to "ifconfig" the tun or tap device.
  */
 #define TUN_MTU_SIZE(f)          ((f)->link_mtu - TUN_LINK_DELTA(f))
-#define TUN_MTU_SIZE_DYNAMIC(f)  ((f)->link_mtu_dynamic - TUN_LINK_DELTA(f))
 
 /*
  * This is the maximum packet size that we need to be able to
@@ -291,9 +296,6 @@ void alloc_buf_sock_tun(struct buffer *buf,
                         const struct frame *frame,
                         const bool tuntap_buffer);
 
-/** Set the --mssfix option. */
-void frame_init_mssfix(struct frame *frame, const struct options *options);
-
 /*
  * EXTENDED_SOCKET_ERROR_CAPABILITY functions -- print extra error info
  * on socket errors, such as PMTU size.  As of 2003.05.11, only works
src/openvpn/proto.h +0 -11
index f73e50c07..94010a98f 100644
@@ -247,17 +247,6 @@ struct ip_tcp_udp_hdr {
         acc -= (u32) >> 16;    \
 }
 
-/*
- * We are in a "liberal" position with respect to MSS,
- * i.e. we assume that MSS can be calculated from MTU
- * by subtracting out only the IP and TCP header sizes
- * without options.
- *
- * (RFC 879, section 7).
- */
-#define MTU_TO_MSS(mtu) (mtu - sizeof(struct openvpn_iphdr) \
-                         - sizeof(struct openvpn_tcphdr))
-
 /*
  * This returns an ip protocol version of packet inside tun
  * and offset of IP header (via parameter).
src/openvpn/ssl.c +2 -1
index 8cbb129d2..96c78199a 100644
@@ -62,6 +62,7 @@
 #include "ssl_ncp.h"
 #include "ssl_util.h"
 #include "auth_token.h"
+#include "mss.h"
 
 #include "memdbg.h"
 
@@ -1893,7 +1894,7 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
                                    options->replay, packet_id_long_form);
     frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,
                    options->ce.tun_mtu_defined, options->ce.tun_mtu);
-    frame_init_mssfix(frame, options);
+    frame_calculate_mssfix(frame, &session->opt->key_type, options);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
     /*
Signature · 1 line
2.33.0
#53 lore raw

Re: [Openvpn-devel] [PATCH v2 08/21] Decouple MSS fix calculation from frame calculation

Quoted 28 lines

Arne Schwabe <[email protected]...> hat am 14.12.2021 16:09 geschrieben:

This consolidates the MSS fix calculation into a single function instead having it distributed all over the code. It also calculates the real wire overhead without extra sizes for buffer etc.

Patch v2: improve comment

---
 src/openvpn/forward.c |  5 ++---
 src/openvpn/init.c    |  3 ++-
 src/openvpn/mss.c     | 40 ++++++++++++++++++++++++++++++++++++++++
 src/openvpn/mss.h     |  6 ++++++
 src/openvpn/mtu.c     |  9 ---------
 src/openvpn/mtu.h     | 10 ++++++----
 src/openvpn/proto.h   | 11 -----------
 src/openvpn/ssl.c     |  3 ++-
 8 files changed, 58 insertions(+), 29 deletions(-)
src/openvpn/forward.c +0 -0
index 29efcd3b9..f82386a1d 100644

src/openvpn/forward.c process_ip_header(struct context *c, unsigned int flags, struct buffer *buf) · line 1496

Quoted 5 lines
src/openvpn/forward.c +1 -1
@@ -1493,7 +1493,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
                 /* possibly alter the TCP MSS */
                 if (flags & PIP_MSSFIX)
                 {
-                    mss_fixup_ipv4(&ipbuf, MTU_TO_MSS(TUN_MTU_SIZE_DYNAMIC(&c->c2.frame)));
+                    mss_fixup_ipv4(&ipbuf, c->c2.frame.mss_fix);

I still think this will badly explode in the ce.mssfix == 0 code path. In that case frame.mss_fix == 0 AFAICT and I see no handling of that possibility in mss_fixup_ipv4/6.

Regards,
--
Frank Lichtenheld

#54 lore raw

Re: [Openvpn-devel] [PATCH v2 08/21] Decouple MSS fix calculation from frame calculation

Am 14.12.21 um 18:10 schrieb Frank Lichtenheld:

Quoted 23 lines
Quoted 23 lines

Arne Schwabe <[email protected]...> hat am 14.12.2021 16:09 geschrieben:

This consolidates the MSS fix calculation into a single function instead having it distributed all over the code. It also calculates the real wire overhead without extra sizes for buffer etc.

Patch v2: improve comment

---
  src/openvpn/forward.c |  5 ++---
  src/openvpn/init.c    |  3 ++-
  src/openvpn/mss.c     | 40 ++++++++++++++++++++++++++++++++++++++++
  src/openvpn/mss.h     |  6 ++++++
  src/openvpn/mtu.c     |  9 ---------
  src/openvpn/mtu.h     | 10 ++++++----
  src/openvpn/proto.h   | 11 -----------
  src/openvpn/ssl.c     |  3 ++-
  8 files changed, 58 insertions(+), 29 deletions(-)
Quoted 13 lines

src/openvpn/forward.c process_ip_header(struct context *c, unsigned int flags, struct buffer *buf) · line 1496

Quoted 10 lines
src/openvpn/forward.c +1 -1
index 29efcd3b9..f82386a1d 100644
@@ -1493,7 +1493,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
                  /* possibly alter the TCP MSS */
                  if (flags & PIP_MSSFIX)
                  {
-                    mss_fixup_ipv4(&ipbuf, MTU_TO_MSS(TUN_MTU_SIZE_DYNAMIC(&c->c2.frame)));
+                    mss_fixup_ipv4(&ipbuf, c->c2.frame.mss_fix);

I still think this will badly explode in the ce.mssfix == 0 code path. In that case frame.mss_fix == 0 AFAICT and I see no handling of that possibility in mss_fixup_ipv4/6.

I won't. This is part of the older parts of OpenVPN that are more obscure. You overlooking the implicit assumption that PIP_MSSFIX is only set if also c->c2.frame.mssfix is != 0

See the top of the function. If ce.mssfix is 0 then you never have the PIP_MSSFIX flag in the flags:

void
process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)

{
    if (!c->options.ce.mssfix)
    {
        flags &= ~PIP_MSSFIX;
    }

Arne

#55 lore raw

Re: [Openvpn-devel] [PATCH v2 08/21] Decouple MSS fix calculation from frame calculation

Quoted 21 lines

Arne Schwabe <[email protected]...> hat am 14.12.2021 23:38 geschrieben:
Am 14.12.21 um 18:10 schrieb Frank Lichtenheld:

Quoted 16 lines

src/openvpn/forward.c process_ip_header(struct context *c, unsigned int flags, struct buffer *buf) · line 1496

Quoted 13 lines

Arne Schwabe <[email protected]...> hat am 14.12.2021 16:09 geschrieben:

src/openvpn/forward.c +1 -1
index 29efcd3b9..f82386a1d 100644
@@ -1493,7 +1493,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
                  /* possibly alter the TCP MSS */
                  if (flags & PIP_MSSFIX)
                  {
-                    mss_fixup_ipv4(&ipbuf, MTU_TO_MSS(TUN_MTU_SIZE_DYNAMIC(&c->c2.frame)));
+                    mss_fixup_ipv4(&ipbuf, c->c2.frame.mss_fix);

I still think this will badly explode in the ce.mssfix == 0 code path. In that case frame.mss_fix == 0 AFAICT and I see no handling of that possibility in mss_fixup_ipv4/6.

Quoted 6 lines

I won't. This is part of the older parts of OpenVPN that are more obscure. You overlooking the implicit assumption that PIP_MSSFIX is only set if also c->c2.frame.mssfix is != 0

See the top of the function. If ce.mssfix is 0 then you never have the PIP_MSSFIX flag in the flags:

You're right. Should've not only looked at mss_fixup_*, but also at process_ip_header...

Regards,
--
Frank Lichtenheld

#56 lore raw

Re: [Openvpn-devel] [PATCH v2 08/21] Decouple MSS fix calculation from frame calculation

I'm pretty sure that this patch is correct if 07/21 is correct.

Quoted 36 lines

Frank Lichtenheld <[email protected]...> hat am 15.12.2021 10:58 geschrieben:

Quoted 19 lines

Arne Schwabe <[email protected]...> hat am 14.12.2021 23:38 geschrieben:
Am 14.12.21 um 18:10 schrieb Frank Lichtenheld:

Quoted 16 lines

src/openvpn/forward.c process_ip_header(struct context *c, unsigned int flags, struct buffer *buf) · line 1496

Quoted 13 lines

Arne Schwabe <[email protected]...> hat am 14.12.2021 16:09 geschrieben:

src/openvpn/forward.c +1 -1
index 29efcd3b9..f82386a1d 100644
@@ -1493,7 +1493,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
                  /* possibly alter the TCP MSS */
                  if (flags & PIP_MSSFIX)
                  {
-                    mss_fixup_ipv4(&ipbuf, MTU_TO_MSS(TUN_MTU_SIZE_DYNAMIC(&c->c2.frame)));
+                    mss_fixup_ipv4(&ipbuf, c->c2.frame.mss_fix);

I still think this will badly explode in the ce.mssfix == 0 code path. In that case frame.mss_fix == 0 AFAICT and I see no handling of that possibility in mss_fixup_ipv4/6.

Quoted 6 lines

I won't. This is part of the older parts of OpenVPN that are more obscure. You overlooking the implicit assumption that PIP_MSSFIX is only set if also c->c2.frame.mssfix is != 0

See the top of the function. If ce.mssfix is 0 then you never have the PIP_MSSFIX flag in the flags:

You're right. Should've not only looked at mss_fixup_*, but also at process_ip_header...

Regards,
--
Frank Lichtenheld

Quoted 4 lines

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

--
Frank Lichtenheld

#57 lore raw

[Openvpn-devel] [PATCH applied] Re: Decouple MSS fix calculation from frame calculation

In reply to Arne Schwabe

I've stared at the code (nice, things get simpler :-) ) and done a few tests (v4 over v4, v4 over v6, ...) with "--mssfix 1000" and looked at the resulting MSS values. These are way different from "master without this" - but arguably, closer to reality than what we had before.

Old: BF-CBC, --mssfix 1000 -> MSS 804 / 784

     AES256GCM, --mssfix 1000 -> MSS 856 / 876
New: BF-CBC,    --mssfix 1000 -> MSS 892 / 872
     AES256GCM, --mssfix 1000 -> MSS 904 / 884 

I have not done more sophisticated counting on whether the new values are *correct*, but tcpdump claims they are:

- "AES256GCM, mssfix 1000", "ssh -4 $remote ls -lR"

16:55:40.171255 IP 194.97.140.11.51194 > 193.149.48.178.50377: UDP, length 1000 16:55:40.171267 IP 194.97.140.11.51194 > 193.149.48.178.50377: UDP, length 1000 16:55:40.171270 IP 194.97.140.11.51194 > 193.149.48.178.50377: UDP, length 1000

- "AES256GCM, mssfix 1000", "ssh -6 $remote ls -lR"

16:57:04.871238 IP 194.97.140.11.51194 > 193.149.48.178.50377: UDP, length 1000

- IPv6 transport is a also correct (which surprises me a bit, I have not seen a reference to the underlying protocol, which changes the "headroom" we have). At least, sometimes... with AES256GCM:

16:58:31.681280 IP6 2001:608:0:814::f000:11.51194 > 2001:608:4::ce:c0f.65297: UDP, length 1000 16:58:31.681289 IP6 2001:608:0:814::f000:11.51194 > 2001:608:4::ce:c0f.65297: UDP, length 1000

- over IPv6 transport, with BF, max packets are off by 8 bytes

17:13:54.561605 IP6 2001:608:0:814::f000:11.51194 > 2001:608:4::ce:c0f.28958: UDP, length 1008 17:13:54.561760 IP6 2001:608:0:814::f000:11.51194 > 2001:608:4::ce:c0f.28958: UDP, length 1008

(which is closer to what I'd expect, but still weird, why not 1020?
... my machines do weird stuff here...)

That said, the code has been subjected to t_client tests as well, which were uneventful (there is nothing that excercises TCP in there), as expected.

The new frame_calculate_mssfix() looks a bit confused regarding "do we declare + initialize variables in one or two steps", and "do we call it 'unsigned' or 'unsigned int'". But this can be fixed in a followup cleanup patch ;-)

Not sure I understand what the old code does here, looking at "tun_mtu_dynamic" - this looks like "adjust to discovered MTU", but the documentation of --mssfix does not speak about this ... so it will be interesting to revisit this (documentation + option) eventually and see if/how we want to consider MTU discovery.

Your patch has been applied to the master branch.

commit d4458eed0c3cf582f787893f033a19cb4629cd76

Author: Arne Schwabe
Date:   Tue Dec 14 16:09:01 2021 +0100
     Decouple MSS fix calculation from frame calculation

     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]/msg23423.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#58 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: Decouple MSS fix calculation from frame calculation

Am 30.12.21 um 17:38 schrieb Gert Doering:

Quoted 31 lines

I've stared at the code (nice, things get simpler :-) ) and done a few tests (v4 over v4, v4 over v6, ...) with "--mssfix 1000" and looked at the resulting MSS values. These are way different from "master without this" - but arguably, closer to reality than what we had before.

Old: BF-CBC, --mssfix 1000 -> MSS 804 / 784

      AES256GCM, --mssfix 1000 -> MSS 856 / 876
New: BF-CBC,    --mssfix 1000 -> MSS 892 / 872
      AES256GCM, --mssfix 1000 -> MSS 904 / 884

I have not done more sophisticated counting on whether the new values are *correct*, but tcpdump claims they are:

- "AES256GCM, mssfix 1000", "ssh -4 $remote ls -lR"

16:55:40.171255 IP 194.97.140.11.51194 > 193.149.48.178.50377: UDP, length 1000 16:55:40.171267 IP 194.97.140.11.51194 > 193.149.48.178.50377: UDP, length 1000 16:55:40.171270 IP 194.97.140.11.51194 > 193.149.48.178.50377: UDP, length 1000

- "AES256GCM, mssfix 1000", "ssh -6 $remote ls -lR"

16:57:04.871238 IP 194.97.140.11.51194 > 193.149.48.178.50377: UDP, length 1000

- IPv6 transport is a also correct (which surprises me a bit, I have not seen a reference to the underlying protocol, which changes the "headroom" we have). At least, sometimes... with AES256GCM:

16:58:31.681280 IP6 2001:608:0:814::f000:11.51194 > 2001:608:4::ce:c0f.65297: UDP, length 1000 16:58:31.681289 IP6 2001:608:0:814::f000:11.51194 > 2001:608:4::ce:c0f.65297: UDP, length 1000

Quoted 7 lines

- over IPv6 transport, with BF, max packets are off by 8 bytes

17:13:54.561605 IP6 2001:608:0:814::f000:11.51194 > 2001:608:4::ce:c0f.28958: UDP, length 1008 17:13:54.561760 IP6 2001:608:0:814::f000:11.51194 > 2001:608:4::ce:c0f.28958: UDP, length 1008

(which is closer to what I'd expect, but still weird, why not 1020?
... my machines do weird stuff here...)

That BF-CBC seems have an extra 8 bytes that I somehow missed. CBC is a odd since it always gives you a multiple of the blocksize (64 bit or 8 byte) and if you evenly divide by the blocksize you get an extra block just for the padding. I need to reinvestigate that code and send a fixup patch for it.

Quoted 8 lines

That said, the code has been subjected to t_client tests as well, which were uneventful (there is nothing that excercises TCP in there), as expected.

The new frame_calculate_mssfix() looks a bit confused regarding "do we declare + initialize variables in one or two steps", and "do we call it 'unsigned' or 'unsigned int'". But this can be fixed in a followup cleanup patch ;-)

Some of those are due trying to not to break the 80 char limit which they would do if we declare them just in one line.

Quoted 5 lines

Not sure I understand what the old code does here, looking at "tun_mtu_dynamic" - this looks like "adjust to discovered MTU", but the documentation of --mssfix does not speak about this ... so it will be interesting to revisit this (documentation + option) eventually and see if/how we want to consider MTU discovery.

MTU discovery is only enabled if we also have fragment enabled and then it affects both fragment and mss since they both use the mtu_dynamic variable.

Arne

#59 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: Decouple MSS fix calculation from frame calculation

Hi,

Quoted 5 lines · Arne Schwabe

That BF-CBC seems have an extra 8 bytes that I somehow missed. CBC is a odd since it always gives you a multiple of the blocksize (64 bit or 8 byte) and if you evenly divide by the blocksize you get an extra block just for the padding. I need to reinvestigate that code and send a fixup patch for it.

You probably know this, but for clarity: this is how CBC padding works, not just for BF. It is easier to trigger with BF though, because of the smaller (64-bit) block, compared to AES (128-bit block).

-Steffan

#60 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: Decouple MSS fix calculation from frame calculation

Hi,

Quoted 10 lines · Steffan Karger
Quoted 5 lines · Arne Schwabe

That BF-CBC seems have an extra 8 bytes that I somehow missed. CBC is a odd since it always gives you a multiple of the blocksize (64 bit or 8 byte) and if you evenly divide by the blocksize you get an extra block just for the padding. I need to reinvestigate that code and send a fixup patch for it.

You probably know this, but for clarity: this is how CBC padding works, not just for BF. It is easier to trigger with BF though, because of the smaller (64-bit) block, compared to AES (128-bit block).

The comment in the code acknowledges this :-) - but the math seems to be not quite right.

We've tested with a few different --mssfix values and BF-CBC + AES-CBC and packets (UDP payload) are consistently up to 8 bytes larger than ordered...

18:34 <@plaisthos> I think I am missing the rounding up to blocksize step 18:35 <@plaisthos> I basically handle the corner that you do NOT round up and
get an extra block but I completely forgot the rounding up for all other values
18:40 <@plaisthos> I will look into that CBC thing later, that needs more
testing than just writing a small quick fix

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#61 lore raw

PATCH v2 9/21 Rework occ link-mtu calculation

In reply to Arne Schwabe

Use the functions that directly compute the link mtu instead relying on the frame logic.

Patch V2: rebase on master

---
 src/openvpn/mtu.c                      |  43 +++++++++
 src/openvpn/mtu.h                      |  11 +++
 src/openvpn/options.c                  |  51 ----------
 tests/unit_tests/openvpn/Makefile.am   |   6 +-
 tests/unit_tests/openvpn/test_crypto.c | 128 ++++++++++++++++++++++++-
 tests/unit_tests/openvpn/test_misc.c   |   1 +
 6 files changed, 186 insertions(+), 54 deletions(-)
src/openvpn/mtu.c +43 -0
index 662984f6..dac0c1de 100644
@@ -138,6 +138,49 @@ frame_calculate_payload_size(const struct frame *frame, const struct options *op
     return payload_size;
 }
 
+size_t
+calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
+{
+    unsigned int payload = frame_calculate_payload_size(frame, o);
+
+    /* neither --secret nor TLS mode */
+    if (!o->tls_client && !o->tls_server && !o->shared_secret_file)
+    {
+        return payload;
+    }
+
+    struct key_type occ_kt;
+
+    /* o->ciphername might be BF-CBC even though the underlying SSL library
+     * does not support it. For this reason we workaround this corner case
+     * by pretending to have no encryption enabled and by manually adding
+     * the required packet overhead to the MTU computation.
+     */
+    const char* ciphername = o->ciphername;
+
+    unsigned int overhead = 0;
+
+    if (strcmp(o->ciphername, "BF-CBC") == 0)
+    {
+        /* none has no overhead, so use this to later add only --auth
+         * overhead */
+
+        /* overhead of BF-CBC: 64 bit block size, 64 bit IV size */
+        overhead += 64/8 + 64/8;
+        /* set ciphername to none, so its size does get added in the
+         * fake_kt and the cipher is not tried to be resolved */
+        ciphername = "none";
+    }
+
+    /* We pass tlsmode always true here since as we do not need to check if
+     * the ciphers are actually valid for non tls in occ calucation */
+    init_key_type(&occ_kt, ciphername, o->authname, true, false);
+
+    overhead += frame_calculate_protocol_header_size(&occ_kt, o, 0, true);
+
+    return payload + overhead;
+}
+
 void
 frame_finalize(struct frame *frame,
                bool link_mtu_defined,
src/openvpn/mtu.h +11 -0
index ae83d3e7..f6013860 100644
@@ -280,6 +280,17 @@ frame_calculate_protocol_header_size(const struct key_type *kt,
                                      unsigned int payload_size,
                                      bool occ);
 
+/**
+ * Calculate the link-mtu to advertise to our peer.  The actual value is not
+ * relevant, because we will possibly perform data channel cipher negotiation
+ * after this, but older clients will log warnings if we do not supply them the
+ * value they expect.  This assumes that the traditional cipher/auth directives
+ * in the config match the config of the peer.
+ */
+size_t
+calc_options_string_link_mtu(const struct options *options,
+                             const struct frame *frame);
+
 /*
  * frame_set_mtu_dynamic and flags
  */
src/openvpn/options.c +0 -51

57 lines not on this page · raw message

tests/unit_tests/openvpn/Makefile.am +4 -2
index 44b77cc5..f681b353 100644
@@ -46,7 +46,9 @@ crypto_testdriver_SOURCES = test_crypto.c mock_msg.c mock_msg.h \
 	$(openvpn_srcdir)/crypto_openssl.c \
 	$(openvpn_srcdir)/otime.c \
 	$(openvpn_srcdir)/packet_id.c \
-	$(openvpn_srcdir)/platform.c
+	$(openvpn_srcdir)/platform.c \
+	$(openvpn_srcdir)/mtu.c \
+	$(openvpn_srcdir)/mss.c
 
 packet_id_testdriver_CFLAGS  = @TEST_CFLAGS@ \
 	-I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir)
@@ -137,4 +139,4 @@ misc_testdriver_SOURCES = test_misc.c mock_msg.c \
     mock_get_random.c \
 	$(openvpn_srcdir)/buffer.c \
 	$(openvpn_srcdir)/ssl_util.c \
-	$(openvpn_srcdir)/platform.c
\ No newline at end of file
+	$(openvpn_srcdir)/platform.c
tests/unit_tests/openvpn/test_crypto.c +127 -1

146 lines not on this page · raw message

tests/unit_tests/openvpn/test_misc.c +1 -0
index 70f726d0..867fa1bb 100644
@@ -37,6 +37,7 @@
 #include <cmocka.h>
 
 #include "ssl_util.h"
+#include "options.h"
 
 static void
 test_compat_lzo_string(void **state)
Signature · 1 line
2.33.0
Acked-by on v2 9/21
#62 lore raw

[Openvpn-devel] [PATCH applied] Re: Rework occ link-mtu calculation

Code looks reasonably, and comes with an extensive test suite - so I did not test "does it configure the same link-mtu for all possible combinations as the code before that". I did test some basic stuff (TLS/BF-CBC/SHA1, TLS/AES-256-GCM, TLS/none/none) and it did produce "different values". BF-CBC matched what the server wanted, with the same config.

I have left out the hunk that adds "option.h" to test_misc.c - that looks stray, no change to that test otherwise (maybe it's needed in a later patch, then I'll notice and re-add it). test_crypto.c also has this added, and *there* it's definitely needed :-)

As mentioned on IRC, we're losing a few msg(D_MTU_DEBUG, ...) in this process - do we want to bring them back at some point?

Your patch has been applied to the master branch.

commit 01b7cd44669896d91696e936cc38a2f57bc6081e

Author: Arne Schwabe
Date:   Thu Dec 30 18:21:36 2021 +0100
     Rework occ link-mtu calculation

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#63 lore raw

PATCH v2 13/21 Implement optional mtu parameter for mssfix

In reply to Arne Schwabe

The current mssfix parameter is a bit as it needs manual calculation of the allowable packet size and also the resulting MSS value does not take into account if IPv4 or IPv6 is used on the outer tunnel. The mtu parameter fixes both of these problem by dynamically including the real overhead.

The syntax and naming of the parater is chosen for compatiblity with OpenVPN3.

Patch V2: document mssfix 0 disabling mssfix, fix rst syntax

---
 Changes.rst                       |  6 +++++
 doc/man-sections/link-options.rst | 38 +++++++++++++++++++++++--------
 src/openvpn/init.c                |  7 +++---
 src/openvpn/mss.c                 | 29 ++++++++++++++++++++++-
 src/openvpn/mss.h                 |  3 ++-
 src/openvpn/multi.c               |  3 ++-
 src/openvpn/options.c             | 13 +++++++++--
 src/openvpn/options.h             |  5 +++-
 src/openvpn/ssl.c                 | 11 +++++----
 src/openvpn/ssl.h                 |  5 +++-
 10 files changed, 96 insertions(+), 24 deletions(-)
Changes.rst +6 -0
index b7d7f2054..cf6a2f86d 100644
@@ -62,6 +62,12 @@ Optional ciphers in ``--data-ciphers``
     Ciphers in ``--data-ciphers`` can now be prefixed with a ``?`` to mark
     those as optional and only use them if the SSL library supports them.
 
+
+Improved ``--mssfix`` calculation
+    The ``--mssfix`` option now allows an optional :code:`mtu` parameter to specify
+    that different overhead for IPv4/IPv6 should taken into account and the resulting
+    size is specified as the total size of the VPN packets including IP and UDP headers.
+
 Deprecated features
 -------------------
 ``inetd`` has been removed
doc/man-sections/link-options.rst +28 -10
index b1ae4e75a..01bc910f3 100644
@@ -110,19 +110,37 @@ the local and the remote host.
   (:code:`p2p`). OpenVPN 2.0 introduces a new mode (:code:`server`) which
   implements a multi-client server capability.
 
---mssfix max
+--mssfix args
+
+  Valid syntax:
+  ::
+
+     mssfix max [mtu]
+
+     mssfix
+
   Announce to TCP sessions running over the tunnel that they should limit
   their send packet sizes such that after OpenVPN has encapsulated them,
   the resulting UDP packet size that OpenVPN sends to its peer will not
-  exceed ``max`` bytes. The default value is :code:`1450`.
-
-  The ``max`` parameter is interpreted in the same way as the
-  ``--link-mtu`` parameter, i.e. the UDP packet size after encapsulation
-  overhead has been added in, but not including the UDP header itself.
-  Resulting packet would be at most 28 bytes larger for IPv4 and 48 bytes
-  for IPv6 (20/40 bytes for IP header and 8 bytes for UDP header). Default
-  value of 1450 allows IPv4 packets to be transmitted over a link with MTU
-  1473 or higher without IP level fragmentation.
+  exceed ``max`` bytes. The default value is :code:`1450`. Use :code:`0`
+  as max to disable mssfix.
+
+  If the :code:`mtu` parameter is specified the ``max`` value is interpreted
+  as the resulting packet size of VPN packets including the IP and UDP header.
+  Support for the :code:`mtu` parameter was added with OpenVPN version 2.6.0.
+
+  If the :code:`mtu` parameter is not specified, the ``max`` parameter
+  is interpreted in the same way as the ``--link-mtu`` parameter, i.e.
+  the UDP packet size after encapsulation overhead has been added in, but
+  not including the UDP header itself. Resulting packet would be at most 28
+  bytes larger for IPv4 and 48 bytes for IPv6 (20/40 bytes for IP header and
+  8 bytes for UDP header). Default value of 1450 allows IPv4 packets to be
+  transmitted over a link with MTU 1473 or higher without IP level
+  fragmentation.
+
+  if ``--mssfix`` is specified is specified without any parameter it
+  inherits the parameters of ``--fragment`` if specified or uses the
+  default for ``--mssfix`` otherwise.
 
   The ``--mssfix`` option only makes sense when you are using the UDP
   protocol for OpenVPN peer-to-peer communication, i.e. ``--proto udp``.
src/openvpn/init.c +4 -3
index 9dc38ed20..bd58e4665 100644
@@ -2207,7 +2207,7 @@ do_deferred_p2p_ncp(struct context *c)
 #endif
 
     if (!tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
-                                         frame_fragment))
+                                         frame_fragment, get_link_socket_info(c)))
     {
         msg(D_TLS_ERRORS, "ERROR: failed to set crypto cipher");
         return false;
@@ -2322,7 +2322,7 @@ do_deferred_options(struct context *c, const unsigned int found)
 
         struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
         if (!tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
-                                              frame_fragment))
+                                              frame_fragment, get_link_socket_info(c)))
         {
             msg(D_TLS_ERRORS, "OPTIONS ERROR: failed to import crypto options");
             return false;
@@ -4238,7 +4238,8 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
 #endif
 
     /* initialize dynamic MTU variable */
-    frame_calculate_mssfix(&c->c2.frame, &c->c1.ks.key_type, &c->options);
+    frame_calculate_mssfix(&c->c2.frame, &c->c1.ks.key_type, &c->options,
+                           get_link_socket_info(c));
 
     /* bind the TCP/UDP socket */
     if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
src/openvpn/mss.c +28 -1
index 906d64eac..b2fe3e56e 100644
@@ -207,9 +207,30 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss)
     }
 }
 
+static unsigned int
+get_ip_encap_overhead(const struct options *options,
+                      const struct link_socket_info *lsi)
+{
+    /* Add the overhead of the encapsulating IP packets */
+    sa_family_t af;
+    if (lsi->lsa)
+    {
+        af = lsi->lsa->actual.dest.addr.sa.sa_family;
+    }
+    else
+    {
+        /* In the early init before the connection is established or we
+         * are in listen mode we can only make an educated guess
+         * from the af of the connection entry */
+        af = options->ce.af;
+    }
+    return datagram_overhead(af, lsi->proto);
+}
+
 void
 frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
-                       const struct options *options)
+                       const struct options *options,
+                       struct link_socket_info *lsi)
 {
     if (options->ce.mssfix == 0)
     {
@@ -236,6 +257,12 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
      *
      * (RFC 879, section 7). */
 
+    if (options->ce.mssfix_encap)
+    {
+        /* Add the overhead of the encapsulating IP packets */
+        overhead += get_ip_encap_overhead(options, lsi);
+    }
+
     /* Add 20 bytes for the IPv4 header and 20 byte for the TCP header of the
      * payload, the mssfix method will add 20 extra if payload is IPv6 */
     overhead += 20 + 20;
src/openvpn/mss.h +2 -1
index 856f4c4e3..eecc79948 100644
@@ -37,6 +37,7 @@ void mss_fixup_dowork(struct buffer *buf, uint16_t maxmss);
 
 /** Set the --mssfix option. */
 void frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
-                            const struct options *options);
+                            const struct options *options,
+                            struct link_socket_info *lsi);
 
 #endif
src/openvpn/multi.c +2 -1
index e5ffebff2..67b7114ad 100644
@@ -2286,7 +2286,8 @@ multi_client_generate_tls_keys(struct context *c)
 #endif
     struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
     if (!tls_session_update_crypto_params(session, &c->options,
-                                          &c->c2.frame, frame_fragment))
+                                          &c->c2.frame, frame_fragment,
+                                          get_link_socket_info(c)))
     {
         msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
         register_signal(c, SIGUSR1, "process-push-msg-failed");
src/openvpn/options.c +11 -2
index 6dd573adb..9b968586a 100644
@@ -6771,18 +6771,27 @@ add_option(struct options *options,
         VERIFY_PERMISSION(OPT_P_GENERAL);
         script_security_set(atoi(p[1]));
     }
-    else if (streq(p[0], "mssfix") && !p[2])
+    else if (streq(p[0], "mssfix") && !p[3])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
         if (p[1])
         {
             options->ce.mssfix = positive_atoi(p[1]);
         }
-        else
+
+        if (!p[1])
         {
             options->ce.mssfix_default = true;
         }
 
+        if (p[2] && streq(p[2], "mtu"))
+        {
+            options->ce.mssfix_encap = true;
+        }
+        else if (p[2])
+        {
+            msg(msglevel, "Unknown parameter to --mssfix: %s", p[2]);
+        }
     }
     else if (streq(p[0], "disable-occ") && !p[1])
     {
src/openvpn/options.h +4 -1
index d4f41cd71..557edab9b 100644
@@ -126,7 +126,10 @@ struct connection_entry
 
     int fragment;        /* internal fragmentation size */
     int mssfix;          /* Upper bound on TCP MSS */
-    bool mssfix_default; /* true if --mssfix was supplied without a parameter */
+    bool mssfix_default; /* true if --mssfix was supplied without a parameter
+                          * or 0 was specified as MTU */
+    bool mssfix_encap;   /* true if --mssfix had the "mtu" parameter to include
+                          * overhead from IP and TCP/UDP encapsulation */
 
     int explicit_exit_notification; /* Explicitly tell peer when we are exiting via OCC_EXIT or [RESTART] message */
 
src/openvpn/ssl.c +7 -4
index b815cdfc1..d6b91efc0 100644
@@ -1883,7 +1883,8 @@ cleanup:
 bool
 tls_session_update_crypto_params_do_work(struct tls_session *session,
                                  struct options* options, struct frame *frame,
-                                 struct frame *frame_fragment)
+                                 struct frame *frame_fragment,
+                                 struct link_socket_info *lsi)
 {
     if (session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized)
     {
@@ -1913,7 +1914,7 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
                                    options->replay, packet_id_long_form);
     frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,
                    options->ce.tun_mtu_defined, options->ce.tun_mtu);
-    frame_calculate_mssfix(frame, &session->opt->key_type, options);
+    frame_calculate_mssfix(frame, &session->opt->key_type, options, lsi);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
     /*
@@ -1938,7 +1939,8 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
 bool
 tls_session_update_crypto_params(struct tls_session *session,
                                  struct options *options, struct frame *frame,
-                                 struct frame *frame_fragment)
+                                 struct frame *frame_fragment,
+                                 struct link_socket_info *lsi)
 {
 
     bool cipher_allowed_as_fallback = options->enable_ncp_fallback
@@ -1957,7 +1959,8 @@ tls_session_update_crypto_params(struct tls_session *session,
     /* Import crypto settings that might be set by pull/push */
     session->opt->crypto_flags |= options->data_channel_crypto_flags;
 
-    return tls_session_update_crypto_params_do_work(session, options, frame, frame_fragment);
+    return tls_session_update_crypto_params_do_work(session, options, frame,
+                                                    frame_fragment, lsi);
 }
 
 
src/openvpn/ssl.h +4 -1
index b14453fe2..e566acd81 100644
@@ -508,13 +508,16 @@ void tls_update_remote_addr(struct tls_multi *multi,
  * @param frame           The frame options for this session (frame overhead is
  *                        adjusted based on the selected cipher/auth).
  * @param frame_fragment  The fragment frame options.
+ * @param lsi             link socket info to adjust MTU related options
+ *                        depending on the current protocol
  *
  * @return true if updating succeeded or keys are already generated, false otherwise.
  */
 bool tls_session_update_crypto_params(struct tls_session *session,
                                       struct options *options,
                                       struct frame *frame,
-                                      struct frame *frame_fragment);
+                                      struct frame *frame_fragment,
+                                      struct link_socket_info *lsi);
 
 /*
  * inline functions
Signature · 1 line
2.33.0
#64 lore raw

PATCH v2 14/21 Add mtu paramter to --fragment and change fragment calculation

In reply to Arne Schwabe

Instead relying on the link_mtu_dynamic field and its calculation in the frame struct, add a new field max_fragment_size and add a calculation of it similar to mssfix.

Also whenever mssfix value is calculated, we also want to calculate the values for fragment as both options need to be calculated from the real overhead.

Patch v2: Fix syntax in rst man page

---
 Changes.rst                       |   9 +--
 doc/man-sections/link-options.rst |  20 ++++--
 src/openvpn/forward.c             |   3 +-
 src/openvpn/fragment.c            |   4 +-
 src/openvpn/init.c                |  23 +++----
 src/openvpn/mss.c                 | 101 +++++++++++++++++++++++++++---
 src/openvpn/mss.h                 |  13 +++-
 src/openvpn/mtu.c                 |  66 +------------------
 src/openvpn/mtu.h                 |  22 +++----
 src/openvpn/options.c             |  12 +++-
 src/openvpn/options.h             |   2 +
 src/openvpn/socket.c              |  11 ----
 src/openvpn/socket.h              |   2 -
 src/openvpn/ssl.c                 |  20 ++----
 14 files changed, 164 insertions(+), 144 deletions(-)
Changes.rst +5 -4
index cf6a2f86d..1ca53dee8 100644
@@ -63,10 +63,11 @@ Optional ciphers in ``--data-ciphers``
     those as optional and only use them if the SSL library supports them.
 
 
-Improved ``--mssfix`` calculation
-    The ``--mssfix`` option now allows an optional :code:`mtu` parameter to specify
-    that different overhead for IPv4/IPv6 should taken into account and the resulting
-    size is specified as the total size of the VPN packets including IP and UDP headers.
+Improved ``--mssfix`` and ``--fragment`` calculation
+    The ``--mssfix`` and ``--fragment`` options now allow an optional :code:`mtu`
+    parameter to specify that different overhead for IPv4/IPv6 should taken into
+    account and the resulting size is specified as the total size of the VPN packets
+    including IP and UDP headers.
 
 Deprecated features
 -------------------
doc/man-sections/link-options.rst +16 -4
index 01bc910f3..94453de0e 100644
@@ -24,13 +24,25 @@ the local and the remote host.
   from any address, not only the address which was specified in the
   ``--remote`` option.
 
---fragment max
+--fragment args
+
+  Valid syntax:
+  ::
+
+     fragment max
+     fragment max mtu
+
   Enable internal datagram fragmentation so that no UDP datagrams are sent
   which are larger than ``max`` bytes.
 
-  The ``max`` parameter is interpreted in the same way as the
-  ``--link-mtu`` parameter, i.e. the UDP packet size after encapsulation
-  overhead has been added in, but not including the UDP header itself.
+  If the :code:`mtu` parameter is present the ``max`` parameter is
+  interpreted to include IP and UDP encapsulation overhead. The
+  :code:`mtu` parameter is introduced in OpenVPN version 2.6.0.
+
+  If the :code:`mtu` parameter is absent, the ``max`` parameter is
+  interpreted in the same way as the ``--link-mtu`` parameter, i.e.
+  the UDP packet size after encapsulation overhead has been added in,
+  but not including the UDP header itself.
 
   The ``--fragment`` option only makes sense when you are using the UDP
   protocol (``--proto udp``).
src/openvpn/forward.c +1 -2
index 6de6b4d49..3f362e95d 100644
@@ -482,8 +482,7 @@ check_fragment(struct context *c)
     /* OS MTU Hint? */
     if (lsi->mtu_changed && lsi->lsa)
     {
-        frame_adjust_path_mtu(&c->c2.frame_fragment, c->c2.link_socket->mtu,
-                              lsi->lsa->actual.dest.addr.sa.sa_family, lsi->proto);
+        frame_adjust_path_mtu(c);
         lsi->mtu_changed = false;
     }
 
src/openvpn/fragment.c +2 -2
index 6f8fb4476..ce8cd3489 100644
@@ -335,12 +335,12 @@ fragment_outgoing(struct fragment_master *f, struct buffer *buf,
             msg(D_FRAG_ERRORS, "FRAG: outgoing buffer is not empty, len=[%d,%d]",
                 buf->len, f->outgoing.len);
         }
-        if (buf->len > PAYLOAD_SIZE_DYNAMIC(frame)) /* should we fragment? */
+        if (buf->len > frame->max_fragment_size) /* should we fragment? */
         {
             /*
              * Send the datagram as a series of 2 or more fragments.
              */
-            f->outgoing_frag_size = optimal_fragment_size(buf->len, PAYLOAD_SIZE_DYNAMIC(frame));
+            f->outgoing_frag_size = optimal_fragment_size(buf->len, frame->max_fragment_size);
             if (buf->len > f->outgoing_frag_size * MAX_FRAGS)
             {
                 FRAG_ERR("too many fragments would be required to send datagram");
src/openvpn/init.c +10 -13
index bd58e4665..647bbbcdf 100644
@@ -2535,14 +2535,6 @@ frame_finalize_options(struct context *c, const struct options *o)
     frame->buf.payload_size = payload_size;
     frame->buf.headroom = headroom;
     frame->buf.tailroom = tailroom;
-
-    /* Kept to still update/calculate the other fields for now */
-    frame_finalize(frame,
-                   o->ce.link_mtu_defined,
-                   o->ce.link_mtu,
-                   o->ce.tun_mtu_defined,
-                   o->ce.tun_mtu);
-
 }
 
 /*
@@ -3368,8 +3360,8 @@ static void
 do_init_fragment(struct context *c)
 {
     ASSERT(c->options.ce.fragment);
-    frame_set_mtu_dynamic(&c->c2.frame_fragment,
-                          c->options.ce.fragment, SET_MTU_UPPER_BOUND);
+    frame_calculate_dynamic(&c->c2.frame_fragment, &c->c1.ks.key_type,
+                            &c->options, get_link_socket_info(c));
     fragment_frame_init(c->c2.fragment, &c->c2.frame_fragment);
 }
 #endif
@@ -4237,9 +4229,9 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
     }
 #endif
 
-    /* initialize dynamic MTU variable */
-    frame_calculate_mssfix(&c->c2.frame, &c->c1.ks.key_type, &c->options,
-                           get_link_socket_info(c));
+    /* initialize dynamic MTU based options (fragment/mssfix) */
+    frame_calculate_dynamic(&c->c2.frame, &c->c1.ks.key_type, &c->options,
+                            get_link_socket_info(c));
 
     /* bind the TCP/UDP socket */
     if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
@@ -4291,6 +4283,11 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
         link_socket_init_phase2(c);
     }
 
+    /* Update dynamic frame calculation as exact transport socket information
+     * (IP vs IPv6) may be only available after socket phase2 has finished */
+    frame_calculate_dynamic(&c->c2.frame, &c->c1.ks.key_type, &c->options,
+                            get_link_socket_info(c));
+
     /*
      * Actually do UID/GID downgrade, and chroot, if requested.
      * May be delayed by --client, --pull, or --up-delay.
src/openvpn/mss.c +94 -7

121 lines not on this page · raw message

src/openvpn/mss.h +10 -3
index eecc79948..82e0c58f6 100644
@@ -36,8 +36,15 @@ void mss_fixup_ipv6(struct buffer *buf, int maxmss);
 void mss_fixup_dowork(struct buffer *buf, uint16_t maxmss);
 
 /** Set the --mssfix option. */
-void frame_calculate_mssfix(struct frame *frame, struct key_type *kt,
-                            const struct options *options,
-                            struct link_socket_info *lsi);
+void frame_calculate_dynamic(struct frame *frame, struct key_type *kt,
+                             const struct options *options,
+                             struct link_socket_info *lsi);
+
+/**
+ * Checks and adjusts the fragment and mssfix value according to the
+ * discovered path mtu value
+ * @param c     context to adjust
+ */
+void frame_adjust_path_mtu(struct context *c);
 
 #endif
src/openvpn/mtu.c +3 -63

83 lines not on this page · raw message

src/openvpn/mtu.h +7 -15
index ace33a74a..06a00b5bb 100644
@@ -113,14 +113,18 @@ struct frame {
     int link_mtu;               /**< Maximum packet size to be sent over
                                  *   the external network interface. */
 
-    unsigned int mss_fix;      /**< The actual MSS value that should be
+    unsigned int mss_fix;       /**< The actual MSS value that should be
                                  *   written to the payload packets. This
                                  *   is the value for IPv4 TCP packets. For
                                  *   IPv6 packets another 20 bytes must
                                  *   be subtracted */
 
-    int link_mtu_dynamic;       /**< Dynamic MTU value for the external
-                                 *   network interface. */
+    int max_fragment_size;      /**< The maximum size of a fragment.
+                                 * Fragmentation is done on the unencrypted
+                                 * payload after (potential) compression. So
+                                 * this value specifies the maximum payload
+                                 * size that can be send in a single fragment
+                                 */
 
     int extra_frame;            /**< Maximum number of bytes that all
                                  *   processing steps together could add.
@@ -195,7 +199,6 @@ struct options;
  * a tap device ifconfiged to an MTU of 1200 might actually want
  * to return a packet size of 1214 on a read().
  */
-#define PAYLOAD_SIZE_DYNAMIC(f)  ((f)->link_mtu_dynamic - (f)->extra_frame)
 #define PAYLOAD_SIZE(f)          ((f)->buf.payload_size)
 
 /*
@@ -203,7 +206,6 @@ struct options;
  * overhead is added.
  */
 #define EXPANDED_SIZE(f)         ((f)->link_mtu)
-#define EXPANDED_SIZE_DYNAMIC(f) ((f)->link_mtu_dynamic)
 #define EXPANDED_SIZE_MIN(f)     (TUN_MTU_MIN + TUN_LINK_DELTA(f))
 
 /*
@@ -224,16 +226,6 @@ struct options;
 
 #define FRAME_HEADROOM(f)          ((f)->buf.headroom)
 
-/*
- * Function prototypes.
- */
-
-void frame_finalize(struct frame *frame,
-                    bool link_mtu_defined,
-                    int link_mtu,
-                    bool tun_mtu_defined,
-                    int tun_mtu);
-
 void frame_subtract_extra(struct frame *frame, const struct frame *src);
 
 void frame_print(const struct frame *frame,
src/openvpn/options.c +10 -2
index 9b968586a..50e7fdfd1 100644
@@ -6115,11 +6115,19 @@ add_option(struct options *options,
         msg(msglevel, "--mtu-dynamic has been replaced by --fragment");
         goto err;
     }
-    else if (streq(p[0], "fragment") && p[1] && !p[2])
+    else if (streq(p[0], "fragment") && p[1] && !p[3])
     {
-/*      VERIFY_PERMISSION (OPT_P_MTU); */
         VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
         options->ce.fragment = positive_atoi(p[1]);
+
+        if (p[2] && streq(p[2], "mtu"))
+        {
+            options->ce.fragment_encap = true;
+        }
+        else if (p[2])
+        {
+            msg(msglevel, "Unknown parameter to --fragment: %s", p[2]);
+        }
     }
 #endif
     else if (streq(p[0], "mtu-disc") && p[1] && !p[2])
src/openvpn/options.h +2 -0
index 557edab9b..256318291 100644
@@ -125,6 +125,8 @@ struct connection_entry
     int mtu_discover_type; /* used if OS supports setting Path MTU discovery options on socket */
 
     int fragment;        /* internal fragmentation size */
+    bool fragment_encap; /* true if --fragment had the "mtu" parameter to
+                          * include overhead from IP and TCP/UDP encapsulation */
     int mssfix;          /* Upper bound on TCP MSS */
     bool mssfix_default; /* true if --mssfix was supplied without a parameter
                           * or 0 was specified as MTU */
src/openvpn/socket.c +0 -11
index 93d2e61ec..fe1dfb315 100644
@@ -1644,17 +1644,6 @@ socket_frame_init(const struct frame *frame, struct link_socket *sock)
     }
 }
 
-/*
- * Adjust frame structure based on a Path MTU value given
- * to us by the OS.
- */
-void
-frame_adjust_path_mtu(struct frame *frame, int pmtu, sa_family_t af, int proto)
-{
-    frame_set_mtu_dynamic(frame, pmtu - datagram_overhead(af, proto),
-                          SET_MTU_UPPER_BOUND);
-}
-
 static void
 resolve_bind_local(struct link_socket *sock, const sa_family_t af)
 {
src/openvpn/socket.h +0 -2
index 936ef2623..a43ed80b5 100644
@@ -300,8 +300,6 @@ void do_preresolve(struct context *c);
 
 void socket_adjust_frame_parameters(struct frame *frame, int proto);
 
-void frame_adjust_path_mtu(struct frame *frame, int pmtu, sa_family_t af, int proto);
-
 void link_socket_close(struct link_socket *sock);
 
 void sd_close(socket_descriptor_t *sd);
src/openvpn/ssl.c +4 -16
index d6b91efc0..789835454 100644
@@ -331,7 +331,6 @@ tls_init_control_channel_frame_parameters(const struct frame *data_channel_frame
 
     /* set dynamic link MTU to cap control channel packets at 1250 bytes */
     ASSERT(TUN_LINK_DELTA(frame) < min_int(frame->link_mtu, 1250));
-    frame->link_mtu_dynamic = min_int(frame->link_mtu, 1250) - TUN_LINK_DELTA(frame);
 
     /* calculate the maximum overhead that control channel frames may have */
     int overhead = 0;
@@ -1912,9 +1911,8 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
     frame_remove_from_extra_frame(frame, crypto_max_overhead());
     crypto_adjust_frame_parameters(frame, &session->opt->key_type,
                                    options->replay, packet_id_long_form);
-    frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,
-                   options->ce.tun_mtu_defined, options->ce.tun_mtu);
-    frame_calculate_mssfix(frame, &session->opt->key_type, options, lsi);
+    frame_calculate_dynamic(frame, &session->opt->key_type, options, lsi);
+
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
     /*
@@ -1929,7 +1927,7 @@ tls_session_update_crypto_params_do_work(struct tls_session *session,
         frame_remove_from_extra_frame(frame_fragment, crypto_max_overhead());
         crypto_adjust_frame_parameters(frame_fragment, &session->opt->key_type,
                                        options->replay, packet_id_long_form);
-        frame_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
+        frame_calculate_dynamic(frame_fragment, &session->opt->key_type, options, lsi);
         frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
     }
 
@@ -2982,6 +2980,7 @@ tls_process(struct tls_multi *multi,
             if (buf)
             {
                 int status = key_state_read_ciphertext(&ks->ks_ssl, buf, multi->opt.frame.tun_mtu);
+
                 if (status == -1)
                 {
                     msg(D_TLS_ERRORS,
@@ -3827,17 +3826,6 @@ tls_pre_decrypt_lite(const struct tls_auth_standalone *tas,
         goto error;
     }
 
-    if (buf->len > EXPANDED_SIZE_DYNAMIC(&tas->frame))
-    {
-        dmsg(D_TLS_STATE_ERRORS,
-             "TLS State Error: Large packet (size %d) received from %s -- a packet no larger than %d bytes was expected",
-             buf->len,
-             print_link_socket_actual(from, &gc),
-             EXPANDED_SIZE_DYNAMIC(&tas->frame));
-        goto error;
-    }
-
-
     struct buffer newbuf = clone_buf(buf);
     struct tls_wrap_ctx tls_wrap_tmp = tas->tls_wrap;
 
Signature · 1 line
2.33.0
#65 lore raw

PATCH v2 15/21 Update fragment and mssfix related warnings

In reply to Arne Schwabe

The warning that fragment/mssfix needs also tun-mtu set to 1500 makes little sense. Remove it completely. Instead warn if there are incosistencies between --fragment and mssfix.

Patch v2: clarify the mssfix and fragment mtu warning message

---
 src/openvpn/init.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
src/openvpn/init.c +10 -5
index 647bbbcdf..6a384381c 100644
@@ -3183,12 +3183,17 @@ do_init_frame(struct context *c)
 #endif
 
 #ifdef ENABLE_FRAGMENT
-    if ((c->options.ce.mssfix || c->options.ce.fragment)
-        && TUN_MTU_SIZE(&c->c2.frame_fragment) != ETHERNET_MTU)
+    if (c->options.ce.fragment > 0 && c->options.ce.mssfix > c->options.ce.fragment)
     {
-        msg(M_WARN,
-            "WARNING: normally if you use --mssfix and/or --fragment, you should also set --tun-mtu %d (currently it is %d)",
-            ETHERNET_MTU, TUN_MTU_SIZE(&c->c2.frame_fragment));
+        msg(M_WARN, "WARNING: if you use --mssfix and --fragment, you should "
+                    "set --fragment (%d) larger than --mssfix (%d)",
+                    c->options.ce.fragment, c->options.ce.mssfix);
+    }
+    if (c->options.ce.fragment > 0 && c->options.ce.mssfix > 0
+        && c->options.ce.fragment_encap != c->options.ce.mssfix_encap)
+    {
+        msg(M_WARN, "WARNING: if you use --mssfix and --fragment, you should "
+                    "use the \"mtu\" flag for both or none of of them.");
     }
 #endif
 }
Signature · 1 line
2.33.0