Skip to contentSkip to conversation

Fix broken fragment/mssfix with NCP

PATCH Superseded Lev Stipakov 4 participants 11 messages Nov 12, 2018 – Nov 6, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix broken fragment/mssfix with NCP

From: Lev Stipakov <[email protected]...>

NCP negotiation replaces worst cast crypto overhead with actual one in data channel frame. That frame params are used by mssfix.

Fragment frame still contains worst case overhead. Because of that TCP packets are fragmented, since MSS value exceeds max fragment size.

Fix by replacing worst case crypto overhead with actual one for fragment frame, as it is done for data channel frame.

Trac #1140

---
 src/openvpn/forward.c |  1 +
 src/openvpn/init.c    | 12 +++++++++++-
 src/openvpn/openvpn.h |  1 +
 src/openvpn/push.c    |  9 ++++++++-
 src/openvpn/ssl.c     | 19 ++++++++++++++++++-
 src/openvpn/ssl.h     | 13 ++++++++-----
 6 files changed, 47 insertions(+), 8 deletions(-)
src/openvpn/forward.c +1 -0
index f8faa81..773f5d7 100644
@@ -1090,6 +1090,7 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
                 if (is_hard_reset(opcode, c->options.key_method))
                 {
                     c->c2.frame = c->c2.frame_initial;
+                    c->c2.frame_fragment = c->c2.frame_fragment_initial;
                 }
 
                 interval_action(&c->c2.tmp_int);
src/openvpn/init.c +11 -1
index 39e8ca5..db96353 100644
@@ -2300,9 +2300,18 @@ do_deferred_options(struct context *c, const unsigned int found)
         {
             tls_poor_mans_ncp(&c->options, c->c2.tls_multi->remote_ciphername);
         }
+        struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+        if (c->options.ce.fragment)
+        {
+            frame_fragment = &c->c2.frame_fragment;
+        }
+#endif
+
         /* Do not regenerate keys if server sends an extra push reply */
         if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
-            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame))
+            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
+                                                 frame_fragment))
         {
             msg(D_TLS_ERRORS, "OPTIONS ERROR: failed to import crypto options");
             return false;
@@ -3082,6 +3091,7 @@ do_init_frame(struct context *c)
      */
     c->c2.frame_fragment = c->c2.frame;
     frame_subtract_extra(&c->c2.frame_fragment, &c->c2.frame_fragment_omit);
+    c->c2.frame_fragment_initial = c->c2.frame_fragment;
 #endif
 
 #if defined(ENABLE_FRAGMENT) && defined(ENABLE_OCC)
src/openvpn/openvpn.h +1 -0
index d11f61d..a5d250f 100644
@@ -265,6 +265,7 @@ struct context_2
     /* Object to handle advanced MTU negotiation and datagram fragmentation */
     struct fragment_master *fragment;
     struct frame frame_fragment;
+    struct frame frame_fragment_initial;
     struct frame frame_fragment_omit;
 #endif
 
src/openvpn/push.c +8 -1
index 72f0996..a44646a 100644
@@ -274,11 +274,18 @@ incoming_push_message(struct context *c, const struct buffer *buffer)
     {
         if (c->options.mode == MODE_SERVER)
         {
+            struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+            if (c->options.ce.fragment)
+            {
+                frame_fragment = &c->c2.frame_fragment;
+            }
+#endif
             struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
             /* Do not regenerate keys if client send a second push request */
             if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
                 && !tls_session_update_crypto_params(session, &c->options,
-                                                     &c->c2.frame))
+                                                     &c->c2.frame, frame_fragment))
             {
                 msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
                 goto error;
src/openvpn/ssl.c +18 -1
index 74b88ce..8942f71 100644
@@ -1986,7 +1986,8 @@ cleanup:
 
 bool
 tls_session_update_crypto_params(struct tls_session *session,
-                                 struct options *options, struct frame *frame)
+                                 struct options *options, struct frame *frame,
+                                 struct frame *frame_fragment)
 {
     if (!session->opt->server
         && 0 != strcmp(options->ciphername, session->opt->config_ciphername)
@@ -2030,6 +2031,22 @@ tls_session_update_crypto_params(struct tls_session *session,
     frame_init_mssfix(frame, options);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
+    /*
+     * mssfix uses data channel framing, which at this point contains
+     * actual overhead. Fragmentation logic uses frame_fragment, which
+     * still contains worst case overhead. Replace it with actual overhead
+     * to prevent unneeded fragmentation.
+     */
+
+    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_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
+        frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
+    }
+
     return tls_session_generate_data_channel_keys(session);
 }
 
src/openvpn/ssl.h +8 -5
index ac6fef7..4802e62 100644
@@ -474,15 +474,18 @@ void tls_update_remote_addr(struct tls_multi *multi,
  * Update TLS session crypto parameters (cipher and auth) and derive data
  * channel keys based on the supplied options.
  *
- * @param session       The TLS session to update.
- * @param options       The options to use when updating session.
- * @param frame         The frame options for this session (frame overhead is
- *                      adjusted based on the selected cipher/auth).
+ * @param session         The TLS session to update.
+ * @param options         The options to use when updating session.
+ * @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.
  *
  * @return true if updating succeeded, false otherwise.
  */
 bool tls_session_update_crypto_params(struct tls_session *session,
-                                      struct options *options, struct frame *frame);
+                                      struct options *options,
+                                      struct frame *frame,
+                                      struct frame *frame_fragment);
 
 /**
  * "Poor man's NCP": Use peer cipher if it is an allowed (NCP) cipher.
Signature · 1 line
2.7.4
#1 lore raw

Hi,

Quoted 10 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

NCP negotiation replaces worst cast crypto overhead with actual one in data channel frame. That frame params are used by mssfix.

Fragment frame still contains worst case overhead. Because of that TCP packets are fragmented, since MSS value exceeds max fragment size.

Quoted 5 lines

Fix by replacing worst case crypto overhead with actual one for fragment frame, as it is done for data channel frame.

Trac #1140

Thanks for digging into this!

Quoted 15 lines
---
 src/openvpn/forward.c |  1 +
 src/openvpn/init.c    | 12 +++++++++++-
 src/openvpn/openvpn.h |  1 +
 src/openvpn/push.c    |  9 ++++++++-
 src/openvpn/ssl.c     | 19 ++++++++++++++++++-
 src/openvpn/ssl.h     | 13 ++++++++-----
 6 files changed, 47 insertions(+), 8 deletions(-)
src/openvpn/forward.c +0 -0
index f8faa81..773f5d7 100644

src/openvpn/forward.c process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo · line 1093

Quoted 4 lines
src/openvpn/forward.c +1 -0
@@ -1090,6 +1090,7 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
                 if (is_hard_reset(opcode, c->options.key_method))
                 {
                     c->c2.frame = c->c2.frame_initial;
+                    c->c2.frame_fragment = c->c2.frame_fragment_initial;

This breaks --disable-fragment builds:

src/openvpn/forward.c:1096:27: error: no member named 'frame_fragment' in 'struct context_2'

                    c->c2.frame_fragment = c->c2.frame_fragment_initial;
                    ~~~~~ ^
src/openvpn/forward.c:1096:50: error: no member named
'frame_fragment_initial' in 'struct context_2'
                    c->c2.frame_fragment = c->c2.frame_fragment_initial;
                                           ~~~~~ ^

2 errors generated.
Makefile:676: recipe for target 'forward.o' failed

Quoted 127 lines
src/openvpn/forward.c +0 -0
                 }
 
                 interval_action(&c->c2.tmp_int);
src/openvpn/init.c +11 -1
index 39e8ca5..db96353 100644
@@ -2300,9 +2300,18 @@ do_deferred_options(struct context *c, const unsigned int found)
         {
             tls_poor_mans_ncp(&c->options, c->c2.tls_multi->remote_ciphername);
         }
+        struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+        if (c->options.ce.fragment)
+        {
+            frame_fragment = &c->c2.frame_fragment;
+        }
+#endif
+
         /* Do not regenerate keys if server sends an extra push reply */
         if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
-            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame))
+            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
+                                                 frame_fragment))
         {
             msg(D_TLS_ERRORS, "OPTIONS ERROR: failed to import crypto options");
             return false;
@@ -3082,6 +3091,7 @@ do_init_frame(struct context *c)
      */
     c->c2.frame_fragment = c->c2.frame;
     frame_subtract_extra(&c->c2.frame_fragment, &c->c2.frame_fragment_omit);
+    c->c2.frame_fragment_initial = c->c2.frame_fragment;
 #endif
 
 #if defined(ENABLE_FRAGMENT) && defined(ENABLE_OCC)
src/openvpn/openvpn.h +1 -0
index d11f61d..a5d250f 100644
@@ -265,6 +265,7 @@ struct context_2
     /* Object to handle advanced MTU negotiation and datagram fragmentation */
     struct fragment_master *fragment;
     struct frame frame_fragment;
+    struct frame frame_fragment_initial;
     struct frame frame_fragment_omit;
 #endif
 
src/openvpn/push.c +8 -1
index 72f0996..a44646a 100644
@@ -274,11 +274,18 @@ incoming_push_message(struct context *c, const struct buffer *buffer)
     {
         if (c->options.mode == MODE_SERVER)
         {
+            struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+            if (c->options.ce.fragment)
+            {
+                frame_fragment = &c->c2.frame_fragment;
+            }
+#endif
             struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
             /* Do not regenerate keys if client send a second push request */
             if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
                 && !tls_session_update_crypto_params(session, &c->options,
-                                                     &c->c2.frame))
+                                                     &c->c2.frame, frame_fragment))
             {
                 msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
                 goto error;
src/openvpn/ssl.c +18 -1
index 74b88ce..8942f71 100644
@@ -1986,7 +1986,8 @@ cleanup:
 
 bool
 tls_session_update_crypto_params(struct tls_session *session,
-                                 struct options *options, struct frame *frame)
+                                 struct options *options, struct frame *frame,
+                                 struct frame *frame_fragment)
 {
     if (!session->opt->server
         && 0 != strcmp(options->ciphername, session->opt->config_ciphername)
@@ -2030,6 +2031,22 @@ tls_session_update_crypto_params(struct tls_session *session,
     frame_init_mssfix(frame, options);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
+    /*
+     * mssfix uses data channel framing, which at this point contains
+     * actual overhead. Fragmentation logic uses frame_fragment, which
+     * still contains worst case overhead. Replace it with actual overhead
+     * to prevent unneeded fragmentation.
+     */
+
+    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_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
+        frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
+    }
+
     return tls_session_generate_data_channel_keys(session);
 }
 
src/openvpn/ssl.h +5 -4
index ac6fef7..4802e62 100644
@@ -474,15 +474,18 @@ void tls_update_remote_addr(struct tls_multi *multi,
  * Update TLS session crypto parameters (cipher and auth) and derive data
  * channel keys based on the supplied options.
  *
- * @param session       The TLS session to update.
- * @param options       The options to use when updating session.
- * @param frame         The frame options for this session (frame overhead is
- *                      adjusted based on the selected cipher/auth).
+ * @param session         The TLS session to update.
+ * @param options         The options to use when updating session.
+ * @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.
  *

src/openvpn/ssl.h void tls_update_remote_addr(struct tls_multi *multi, · line 491

Quoted 10 lines
src/openvpn/ssl.h +3 -1
  * @return true if updating succeeded, false otherwise.
  */
 bool tls_session_update_crypto_params(struct tls_session *session,
-                                      struct options *options, struct frame *frame);
+                                      struct options *options,
+                                      struct frame *frame,
+                                      struct frame *frame_fragment);
 
 /**
  * "Poor man's NCP": Use peer cipher if it is an allowed (NCP) cipher.

Otherwise this patch looks good to me. I recall finding weird behaviour when I tested this patch earlier, but can't reproduce it anymore based on my notes from then. I tried (large) pings and TCP transfers, using --fragment 500 and 1000 and inspected the packet sizes using tcpdump. Both for client-to-server and server-to-client traffic. Although --fragment for non-TCP traffic still seems to behave suboptimal, this patch does improve behaviour for TCP streams. Since that is probably the majority or traffic where people need performance, this is very useful.

Once the --disable-fragment builds have been fixed, I have no objections to merging that patch. But I would prefer to have one of the networking guys to take a good look at this too before merging.

-Steffan

#2 lore raw

PATCH v2 1/1 Fix broken fragment/mssfix with NCP

From: Lev Stipakov <[email protected]...>

NCP negotiation replaces worst cast crypto overhead with actual one in data channel frame. That frame params are used by mssfix.

Fragment frame still contains worst case overhead. Because of that TCP packets are fragmented, since MSS value exceeds max fragment size.

Fix by replacing worst case crypto overhead with actual one for fragment frame, as it is done for data channel frame.

Trac #1140

---

v2: fix --disable-fragment build

 src/openvpn/forward.c |  3 +++
 src/openvpn/init.c    | 12 +++++++++++-
 src/openvpn/openvpn.h |  1 +
 src/openvpn/push.c    |  9 ++++++++-
 src/openvpn/ssl.c     | 19 ++++++++++++++++++-
 src/openvpn/ssl.h     | 13 ++++++++-----
 6 files changed, 49 insertions(+), 8 deletions(-)
src/openvpn/forward.c +3 -0
index 4076f64..0dd599a 100644
@@ -1093,6 +1093,9 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
                 if (is_hard_reset(opcode, c->options.key_method))
                 {
                     c->c2.frame = c->c2.frame_initial;
+#ifdef ENABLE_FRAGMENT
+                    c->c2.frame_fragment = c->c2.frame_fragment_initial;
+#endif
                 }
 
                 interval_action(&c->c2.tmp_int);
src/openvpn/init.c +11 -1
index 560d87d..b1a91af 100644
@@ -2298,9 +2298,18 @@ do_deferred_options(struct context *c, const unsigned int found)
         {
             tls_poor_mans_ncp(&c->options, c->c2.tls_multi->remote_ciphername);
         }
+        struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+        if (c->options.ce.fragment)
+        {
+            frame_fragment = &c->c2.frame_fragment;
+        }
+#endif
+
         /* Do not regenerate keys if server sends an extra push reply */
         if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
-            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame))
+            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
+                                                 frame_fragment))
         {
             msg(D_TLS_ERRORS, "OPTIONS ERROR: failed to import crypto options");
             return false;
@@ -3082,6 +3091,7 @@ do_init_frame(struct context *c)
      */
     c->c2.frame_fragment = c->c2.frame;
     frame_subtract_extra(&c->c2.frame_fragment, &c->c2.frame_fragment_omit);
+    c->c2.frame_fragment_initial = c->c2.frame_fragment;
 #endif
 
 #if defined(ENABLE_FRAGMENT) && defined(ENABLE_OCC)
src/openvpn/openvpn.h +1 -0
index d11f61d..a5d250f 100644
@@ -265,6 +265,7 @@ struct context_2
     /* Object to handle advanced MTU negotiation and datagram fragmentation */
     struct fragment_master *fragment;
     struct frame frame_fragment;
+    struct frame frame_fragment_initial;
     struct frame frame_fragment_omit;
 #endif
 
src/openvpn/push.c +8 -1
index 8befc6f..248c2e4 100644
@@ -287,11 +287,18 @@ incoming_push_message(struct context *c, const struct buffer *buffer)
     {
         if (c->options.mode == MODE_SERVER)
         {
+            struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+            if (c->options.ce.fragment)
+            {
+                frame_fragment = &c->c2.frame_fragment;
+            }
+#endif
             struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
             /* Do not regenerate keys if client send a second push request */
             if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
                 && !tls_session_update_crypto_params(session, &c->options,
-                                                     &c->c2.frame))
+                                                     &c->c2.frame, frame_fragment))
             {
                 msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
                 goto error;
src/openvpn/ssl.c +18 -1
index e9927eb..ea9fd5f 100644
@@ -1990,7 +1990,8 @@ cleanup:
 
 bool
 tls_session_update_crypto_params(struct tls_session *session,
-                                 struct options *options, struct frame *frame)
+                                 struct options *options, struct frame *frame,
+                                 struct frame *frame_fragment)
 {
     if (!session->opt->server
         && 0 != strcmp(options->ciphername, session->opt->config_ciphername)
@@ -2034,6 +2035,22 @@ tls_session_update_crypto_params(struct tls_session *session,
     frame_init_mssfix(frame, options);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
+    /*
+     * mssfix uses data channel framing, which at this point contains
+     * actual overhead. Fragmentation logic uses frame_fragment, which
+     * still contains worst case overhead. Replace it with actual overhead
+     * to prevent unneeded fragmentation.
+     */
+
+    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_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
+        frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
+    }
+
     return tls_session_generate_data_channel_keys(session);
 }
 
src/openvpn/ssl.h +8 -5
index eafb235..a6b18d0 100644
@@ -476,15 +476,18 @@ void tls_update_remote_addr(struct tls_multi *multi,
  * Update TLS session crypto parameters (cipher and auth) and derive data
  * channel keys based on the supplied options.
  *
- * @param session       The TLS session to update.
- * @param options       The options to use when updating session.
- * @param frame         The frame options for this session (frame overhead is
- *                      adjusted based on the selected cipher/auth).
+ * @param session         The TLS session to update.
+ * @param options         The options to use when updating session.
+ * @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.
  *
  * @return true if updating succeeded, false otherwise.
  */
 bool tls_session_update_crypto_params(struct tls_session *session,
-                                      struct options *options, struct frame *frame);
+                                      struct options *options,
+                                      struct frame *frame,
+                                      struct frame *frame_fragment);
 
 /**
  * "Poor man's NCP": Use peer cipher if it is an allowed (NCP) cipher.
Signature · 1 line
2.7.4
Acked-by on v2 1/1
#3 lore raw

Am 21.01.19 um 21:04 schrieb Lev Stipakov:

Quoted 6 lines

From: Lev Stipakov <[email protected]...>

NCP negotiation replaces worst cast crypto overhead with actual one in data channel frame. That frame params are used by mssfix.

Quoted 7 lines

Fragment frame still contains worst case overhead. Because of that TCP packets are fragmented, since MSS value exceeds max fragment size.

Fix by replacing worst case crypto overhead with actual one for fragment frame, as it is done for data channel frame.

The change looks good and is tested.

#4 lore raw
Quoted 11 lines · Arne Schwabe

Am 21.01.19 um 21:04 schrieb Lev Stipakov:

Quoted 10 lines

From: Lev Stipakov <[email protected]...>

NCP negotiation replaces worst cast crypto overhead with actual one in data channel frame. That frame params are used by mssfix.

Fragment frame still contains worst case overhead. Because of that TCP packets are fragmented, since MSS value exceeds max fragment size.

Quoted 7 lines
Quoted 3 lines

Fix by replacing worst case crypto overhead with actual one for fragment frame, as it is done for data channel frame.

The change looks good and is tested.

Since Arne agrees, I'm completely convinced now too:

#5 lore raw
In reply to Lev Stipakov

Your patch has been applied to the master branch.

I have not tried to understand the actual change, but the general explanation makes sense and with two ACKs already... I did run t_client tests, though :-) (and there is a fragment test).

I did try to apply it to 2.4, because it's a bugfix (so "can I have it for release/2.4, please!") and while it *applied* fine, it did not compile :-( - the function signature for crypto_adjust_frame_parameters() is different in 2.4 - "use_iv" still exists. Can you have a look, please, and send a 2.4 version?

commit d22ba6b2c551fa83d23b5cf668e08a08fde446bc (master)

Author: Lev Stipakov
Date:   Mon Jan 21 22:04:54 2019 +0200
     Fix broken fragment/mssfix with NCP

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Acked-by: Steffan Karger <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg18135.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#6 lore raw

PATCH v2 1/1 Fix broken fragment/mssfix with NCP

From: Lev Stipakov <[email protected]...>

This is the 2.4 backport of master patch (commit d22ba6b).

NCP negotiation replaces worst cast crypto overhead with actual one in data channel frame. That frame params are used by mssfix.

Fragment frame still contains worst case overhead. Because of that TCP packets are fragmented, since MSS value exceeds max fragment size.

Fix by replacing worst case crypto overhead with actual one for fragment frame, as it is done for data channel frame.

Trac #1140

---
v2: fix --disable-fragment build

 src/openvpn/forward.c |  3 +++
 src/openvpn/init.c    | 12 +++++++++++-
 src/openvpn/openvpn.h |  1 +
 src/openvpn/push.c    |  9 ++++++++-
 src/openvpn/ssl.c     | 19 ++++++++++++++++++-
 src/openvpn/ssl.h     | 13 ++++++++-----
 6 files changed, 49 insertions(+), 8 deletions(-)
src/openvpn/forward.c +3 -0
index 65f790f..84bb584 100644
@@ -873,6 +873,9 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
                 if (is_hard_reset(opcode, c->options.key_method))
                 {
                     c->c2.frame = c->c2.frame_initial;
+#ifdef ENABLE_FRAGMENT
+                    c->c2.frame_fragment = c->c2.frame_fragment_initial;
+#endif
                 }
 
                 interval_action(&c->c2.tmp_int);
src/openvpn/init.c +11 -1
index d3785ca..37b832a 100644
@@ -2294,9 +2294,18 @@ do_deferred_options(struct context *c, const unsigned int found)
         {
             tls_poor_mans_ncp(&c->options, c->c2.tls_multi->remote_ciphername);
         }
+        struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+        if (c->options.ce.fragment)
+        {
+            frame_fragment = &c->c2.frame_fragment;
+        }
+#endif
+
         /* Do not regenerate keys if server sends an extra push reply */
         if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
-            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame))
+            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
+                                                 frame_fragment))
         {
             msg(D_TLS_ERRORS, "OPTIONS ERROR: failed to import crypto options");
             return false;
@@ -3035,6 +3044,7 @@ do_init_frame(struct context *c)
      */
     c->c2.frame_fragment = c->c2.frame;
     frame_subtract_extra(&c->c2.frame_fragment, &c->c2.frame_fragment_omit);
+    c->c2.frame_fragment_initial = c->c2.frame_fragment;
 #endif
 
 #if defined(ENABLE_FRAGMENT) && defined(ENABLE_OCC)
src/openvpn/openvpn.h +1 -0
index 7736183..ed7975c 100644
@@ -269,6 +269,7 @@ struct context_2
     /* Object to handle advanced MTU negotiation and datagram fragmentation */
     struct fragment_master *fragment;
     struct frame frame_fragment;
+    struct frame frame_fragment_initial;
     struct frame frame_fragment_omit;
 #endif
 
src/openvpn/push.c +8 -1
index dd5bd41..ba2fbe4 100644
@@ -287,11 +287,18 @@ incoming_push_message(struct context *c, const struct buffer *buffer)
     {
         if (c->options.mode == MODE_SERVER)
         {
+            struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+            if (c->options.ce.fragment)
+            {
+                frame_fragment = &c->c2.frame_fragment;
+            }
+#endif
             struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
             /* Do not regenerate keys if client send a second push request */
             if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
                 && !tls_session_update_crypto_params(session, &c->options,
-                                                     &c->c2.frame))
+                                                     &c->c2.frame, frame_fragment))
             {
                 msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
                 goto error;
src/openvpn/ssl.c +18 -1
index 9696e9b..7dcd962 100644
@@ -1962,7 +1962,8 @@ cleanup:
 
 bool
 tls_session_update_crypto_params(struct tls_session *session,
-                                 struct options *options, struct frame *frame)
+                                 struct options *options, struct frame *frame,
+                                 struct frame *frame_fragment)
 {
     if (!session->opt->server
         && 0 != strcmp(options->ciphername, session->opt->config_ciphername)
@@ -2006,6 +2007,22 @@ tls_session_update_crypto_params(struct tls_session *session,
     frame_init_mssfix(frame, options);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
+    /*
+     * mssfix uses data channel framing, which at this point contains
+     * actual overhead. Fragmentation logic uses frame_fragment, which
+     * still contains worst case overhead. Replace it with actual overhead
+     * to prevent unneeded fragmentation.
+     */
+
+    if (frame_fragment)
+    {
+        frame_remove_from_extra_frame(frame_fragment, crypto_max_overhead());
+        crypto_adjust_frame_parameters(frame_fragment, &session->opt->key_type,
+                                       options->use_iv, options->replay, packet_id_long_form);
+        frame_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
+        frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
+    }
+
     return tls_session_generate_data_channel_keys(session);
 }
 
src/openvpn/ssl.h +8 -5
index 8066789..6672d43 100644
@@ -475,15 +475,18 @@ void tls_update_remote_addr(struct tls_multi *multi,
  * Update TLS session crypto parameters (cipher and auth) and derive data
  * channel keys based on the supplied options.
  *
- * @param session       The TLS session to update.
- * @param options       The options to use when updating session.
- * @param frame         The frame options for this session (frame overhead is
- *                      adjusted based on the selected cipher/auth).
+ * @param session         The TLS session to update.
+ * @param options         The options to use when updating session.
+ * @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.
  *
  * @return true if updating succeeded, false otherwise.
  */
 bool tls_session_update_crypto_params(struct tls_session *session,
-                                      struct options *options, struct frame *frame);
+                                      struct options *options,
+                                      struct frame *frame,
+                                      struct frame *frame_fragment);
 
 /**
  * "Poor man's NCP": Use peer cipher if it is an allowed (NCP) cipher.
Signature · 1 line
2.7.4
#7 lore raw

Hi,

Quoted 8 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

This is the 2.4 backport of master patch (commit d22ba6b).

NCP negotiation replaces worst cast crypto overhead with actual one in data channel frame. That frame params are used by mssfix.

Quoted 7 lines

Fragment frame still contains worst case overhead. Because of that TCP packets are fragmented, since MSS value exceeds max fragment size.

Fix by replacing worst case crypto overhead with actual one for fragment frame, as it is done for data channel frame.

Thanks for the backport.

Unfortunately, I can not reproduce the problem *without* the patch, so I wonder if the issue got fixed by something else as side-effect?

If I do what you describe in the ticket:

run

openvpn --fragment 1300 --mssfix

and grep out the MSS lines, I get (for IPv4, IPv6 numbers are -20 bytes):

with NCP: MSS: 1460 -> 1203
--ncp-disable: MSS: 1284 -> 1211

... and the very same numbers from master...

OpenVPN versions tested:

OpenVPN 2.4.7 [git:release/2.4/416532f8e4125adb+]
OpenVPN 2.5_git [git:master/b5fe104ddbbdf59b+]

So, I'm wondering how to proceed here...

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]...
#8 lore raw

Hi,

Quoted 9 lines

with NCP: MSS: 1460 -> 1203
--ncp-disable: MSS: 1284 -> 1211

... and the very same numbers from master...

So, I'm wondering how to proceed here...

As it turns out, the fix is correct but explanation is not quite clear.

The problem is not in incorrect MSS value (it is correct), but inside openvpn's fragmentation logic, which decides "to fragment or not to fragment".

Without this patch, fragmentation logic incorrectly uses max crypto overhead when calculating packet size. It exceeds fragment size and openvpn peforms fragmentation:

Quoted 1 line

sudo tcpdump port 1194

13:59:06.956394 IP server.fi.openvpn > nat2.panoulu.net.openvpn: UDP, length 652
13:59:06.956489 IP server.fi.openvpn > nat2.panoulu.net.openvpn: UDP, length 648

With this patch, an actual crypto overhead is used and no unnecessary fragmentation is performed:

Quoted 1 line

sudo tcpdump port 1194

13:58:08.685915 IP server.fi.openvpn > nat2.panoulu.net.openvpn: UDP, length 1272
13:58:08.686007 IP server.fi.openvpn > nat2.panoulu.net.openvpn: UDP, length 1272

I will send v3 with a better explanation.

Signature · 1 line
-Lev
#9 lore raw

PATCH v3 1/1 Fix broken fragmentation logic when using NCP

From: Lev Stipakov <[email protected]...>

This is the 2.4 backport of master patch (commit d22ba6b).

NCP negotiation replaces worst case crypto overhead with actual one in data channel frame. That frame params are used by mssfix. Fragment frame still contains worst case overhead.

Without this patch, fragmentation logic incorrectly uses max crypto overhead when calculating packet size. It exceeds fragment size and openvpn peforms fragmentation:

Quoted 1 line

sudo tcpdump port 1194

13:59:06.956394 IP server.fi.openvpn > nat2.panoulu.net.openvpn: UDP, length 652 13:59:06.956489 IP server.fi.openvpn > nat2.panoulu.net.openvpn: UDP, length 648

This patch fixes fragmentation calculation by setting actual crypto overhead, and no unnecessary fragmentation is performed:

Quoted 1 line

sudo tcpdump port 1194

13:58:08.685915 IP server.fi.openvpn > nat2.panoulu.net.openvpn: UDP, length 1272 13:58:08.686007 IP server.fi.openvpn > nat2.panoulu.net.openvpn: UDP, length 1272

Trac #1140

---

v3:

Refine a bit misleading commit message. Before one might think that
problem is in incorrect MSS value being set - it is not. The problem is in fragment calculation.

v2:

    Fix --disable-fragment build
 src/openvpn/forward.c |  3 +++
 src/openvpn/init.c    | 12 +++++++++++-
 src/openvpn/openvpn.h |  1 +
 src/openvpn/push.c    |  9 ++++++++-
 src/openvpn/ssl.c     | 19 ++++++++++++++++++-
 src/openvpn/ssl.h     | 13 ++++++++-----
 6 files changed, 49 insertions(+), 8 deletions(-)
src/openvpn/forward.c +3 -0
index 65f790f..84bb584 100644
@@ -873,6 +873,9 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
                 if (is_hard_reset(opcode, c->options.key_method))
                 {
                     c->c2.frame = c->c2.frame_initial;
+#ifdef ENABLE_FRAGMENT
+                    c->c2.frame_fragment = c->c2.frame_fragment_initial;
+#endif
                 }
 
                 interval_action(&c->c2.tmp_int);
src/openvpn/init.c +11 -1
index d3785ca..37b832a 100644
@@ -2294,9 +2294,18 @@ do_deferred_options(struct context *c, const unsigned int found)
         {
             tls_poor_mans_ncp(&c->options, c->c2.tls_multi->remote_ciphername);
         }
+        struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+        if (c->options.ce.fragment)
+        {
+            frame_fragment = &c->c2.frame_fragment;
+        }
+#endif
+
         /* Do not regenerate keys if server sends an extra push reply */
         if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
-            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame))
+            && !tls_session_update_crypto_params(session, &c->options, &c->c2.frame,
+                                                 frame_fragment))
         {
             msg(D_TLS_ERRORS, "OPTIONS ERROR: failed to import crypto options");
             return false;
@@ -3035,6 +3044,7 @@ do_init_frame(struct context *c)
      */
     c->c2.frame_fragment = c->c2.frame;
     frame_subtract_extra(&c->c2.frame_fragment, &c->c2.frame_fragment_omit);
+    c->c2.frame_fragment_initial = c->c2.frame_fragment;
 #endif
 
 #if defined(ENABLE_FRAGMENT) && defined(ENABLE_OCC)
src/openvpn/openvpn.h +1 -0
index 7736183..ed7975c 100644
@@ -269,6 +269,7 @@ struct context_2
     /* Object to handle advanced MTU negotiation and datagram fragmentation */
     struct fragment_master *fragment;
     struct frame frame_fragment;
+    struct frame frame_fragment_initial;
     struct frame frame_fragment_omit;
 #endif
 
src/openvpn/push.c +8 -1
index dd5bd41..ba2fbe4 100644
@@ -287,11 +287,18 @@ incoming_push_message(struct context *c, const struct buffer *buffer)
     {
         if (c->options.mode == MODE_SERVER)
         {
+            struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+            if (c->options.ce.fragment)
+            {
+                frame_fragment = &c->c2.frame_fragment;
+            }
+#endif
             struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
             /* Do not regenerate keys if client send a second push request */
             if (!session->key[KS_PRIMARY].crypto_options.key_ctx_bi.initialized
                 && !tls_session_update_crypto_params(session, &c->options,
-                                                     &c->c2.frame))
+                                                     &c->c2.frame, frame_fragment))
             {
                 msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
                 goto error;
src/openvpn/ssl.c +18 -1
index 9696e9b..7dcd962 100644
@@ -1962,7 +1962,8 @@ cleanup:
 
 bool
 tls_session_update_crypto_params(struct tls_session *session,
-                                 struct options *options, struct frame *frame)
+                                 struct options *options, struct frame *frame,
+                                 struct frame *frame_fragment)
 {
     if (!session->opt->server
         && 0 != strcmp(options->ciphername, session->opt->config_ciphername)
@@ -2006,6 +2007,22 @@ tls_session_update_crypto_params(struct tls_session *session,
     frame_init_mssfix(frame, options);
     frame_print(frame, D_MTU_INFO, "Data Channel MTU parms");
 
+    /*
+     * mssfix uses data channel framing, which at this point contains
+     * actual overhead. Fragmentation logic uses frame_fragment, which
+     * still contains worst case overhead. Replace it with actual overhead
+     * to prevent unneeded fragmentation.
+     */
+
+    if (frame_fragment)
+    {
+        frame_remove_from_extra_frame(frame_fragment, crypto_max_overhead());
+        crypto_adjust_frame_parameters(frame_fragment, &session->opt->key_type,
+                                       options->use_iv, options->replay, packet_id_long_form);
+        frame_set_mtu_dynamic(frame_fragment, options->ce.fragment, SET_MTU_UPPER_BOUND);
+        frame_print(frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
+    }
+
     return tls_session_generate_data_channel_keys(session);
 }
 
src/openvpn/ssl.h +8 -5
index 8066789..6672d43 100644
@@ -475,15 +475,18 @@ void tls_update_remote_addr(struct tls_multi *multi,
  * Update TLS session crypto parameters (cipher and auth) and derive data
  * channel keys based on the supplied options.
  *
- * @param session       The TLS session to update.
- * @param options       The options to use when updating session.
- * @param frame         The frame options for this session (frame overhead is
- *                      adjusted based on the selected cipher/auth).
+ * @param session         The TLS session to update.
+ * @param options         The options to use when updating session.
+ * @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.
  *
  * @return true if updating succeeded, false otherwise.
  */
 bool tls_session_update_crypto_params(struct tls_session *session,
-                                      struct options *options, struct frame *frame);
+                                      struct options *options,
+                                      struct frame *frame,
+                                      struct frame *frame_fragment);
 
 /**
  * "Poor man's NCP": Use peer cipher if it is an allowed (NCP) cipher.
Signature · 1 line
2.7.4
Acked-by on v3 1/1
#10 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix broken fragmentation logic when using NCP

Thanks for clarifying what the actual problem is, so we have it nicely in the logs. I have diff'ed this patch vs. d22ba6b2c551f, and the only difference is related to "use_iv" which got kicked out from master.

So I'm mostly relying on the ACks from Arne and Steffan and not doing any review myself :-) - I have done a full client test, of course.

Your patch has been applied to the release/2.4 branch.

commit 3bd91cd0e68762b861c57cf37f144d8a11704e9d

Author: Lev Stipakov
Date:   Wed Oct 30 14:44:59 2019 +0200
     Fix broken fragmentation logic when using NCP

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg18975.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering