Skip to contentSkip to conversation

Change FULL_SYNC macro to no_pending_reliable_packets function

PATCH In review Arne Schwabe 2 participants 4 messages Apr 26, 2022 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#1 lore raw

PATCH v2 1/1 Change FULL_SYNC macro to no_pending_reliable_packets function

This changes this macro to a better named inline function. This introduces a slight whitespace problem but the next refactoring will move the incorrectly intended block to its own function anyway.

---
 src/openvpn/ssl.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
src/openvpn/ssl.c +7 -4
index bad59f2a1..6c6648afa 100644
@@ -1774,8 +1774,11 @@ flush_payload_buffer(struct key_state *ks)
 }
 
 /* true if no in/out acknowledgements pending */
-#define FULL_SYNC \
-    (reliable_empty(ks->send_reliable) && reliable_ack_empty(ks->rec_ack))
+static bool
+no_pending_reliable_packets(struct key_state *ks)
+{
+    return (reliable_empty(ks->send_reliable) && reliable_ack_empty(ks->rec_ack));
+}
 
 /*
  * Move the active key to the lame duck key and reinitialize the
@@ -2518,7 +2521,7 @@ tls_process(struct tls_multi *multi,
         }
 
         /* Wait for Initial Handshake ACK */
-        if (ks->state == S_PRE_START && FULL_SYNC)
+        if (ks->state == S_PRE_START && no_pending_reliable_packets(ks))
         {
             ks->state = S_START;
             state_change = true;
@@ -2544,7 +2547,7 @@ tls_process(struct tls_multi *multi,
         if (((ks->state == S_GOT_KEY && !session->opt->server)
              || (ks->state == S_SENT_KEY && session->opt->server)))
         {
-            if (FULL_SYNC)
+            if (no_pending_reliable_packets(ks))
             {
                 ks->established = now;
                 dmsg(D_TLS_DEBUG_MED, "STATE S_ACTIVE");
Signature · 1 line
2.32.0 (Apple Git-132)
#2 lore raw

PATCH v2 1/1 Extract session_move_active into its own function

This makes the tls_process function smaller and easier to understand and this state easier to understand in its own function.

---
 src/openvpn/ssl.c | 92 ++++++++++++++++++++++++++---------------------
 1 file changed, 52 insertions(+), 40 deletions(-)
src/openvpn/ssl.c +52 -40
index 6c6648afa..c90113044 100644
@@ -2431,6 +2431,54 @@ session_move_pre_start(const struct tls_session *session,
     return true;
 
 }
+
+/**
+ * Moves the key to state to S_ACTIVE and also advances the multi_state state
+ * machine if this is the initial connection. 
+ */
+static void
+session_move_active(struct tls_multi *multi, struct tls_session *session,
+                    struct link_socket_info *to_link_socket_info,
+                    struct key_state *ks)
+{
+    dmsg(D_TLS_DEBUG_MED, "STATE S_ACTIVE");
+
+    ks->established = now;
+    if (check_debug_level(D_HANDSHAKE))
+    {
+        print_details(&ks->ks_ssl, "Control Channel:");
+    }
+    ks->state = S_ACTIVE;
+    /* Cancel negotiation timeout */
+    ks->must_negotiate = 0;
+    INCR_SUCCESS;
+
+    /* Set outgoing address for data channel packets */
+    link_socket_set_outgoing_addr(to_link_socket_info, &ks->remote_addr,
+                                  session->common_name, session->opt->es);
+
+    /* Check if we need to advance the tls_multi state machine */
+    if (multi->multi_state == CAS_NOT_CONNECTED)
+    {
+        if (session->opt->mode == MODE_SERVER)
+        {
+            /* On a server we continue with running connect scripts next */
+            multi->multi_state = CAS_WAITING_AUTH;
+        }
+        else
+        {
+            /* Skip the connect script related states */
+            multi->multi_state = CAS_WAITING_OPTIONS_IMPORT;
+        }
+    }
+
+    /* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */
+    flush_payload_buffer(ks);
+
+#ifdef MEASURE_TLS_HANDSHAKE_STATS
+    show_tls_performance_stats();
+#endif
+}
 /*
  * This is the primary routine for processing TLS stuff inside the
  * the main event loop.  When this routine exits
@@ -2545,47 +2593,11 @@ tls_process(struct tls_multi *multi,
 
         /* Wait for ACK */
         if (((ks->state == S_GOT_KEY && !session->opt->server)
-             || (ks->state == S_SENT_KEY && session->opt->server)))
+             || (ks->state == S_SENT_KEY && session->opt->server))
+             && no_pending_reliable_packets(ks))
         {
-            if (no_pending_reliable_packets(ks))
-            {
-                ks->established = now;
-                dmsg(D_TLS_DEBUG_MED, "STATE S_ACTIVE");
-                if (check_debug_level(D_HANDSHAKE))
-                {
-                    print_details(&ks->ks_ssl, "Control Channel:");
-                }
-                state_change = true;
-                ks->state = S_ACTIVE;
-                /* Cancel negotiation timeout */
-                ks->must_negotiate = 0;
-                INCR_SUCCESS;
-
-                /* Set outgoing address for data channel packets */
-                link_socket_set_outgoing_addr(to_link_socket_info, &ks->remote_addr, session->common_name, session->opt->es);
-
-                /* Check if we need to advance the tls_multi state machine */
-                if (multi->multi_state == CAS_NOT_CONNECTED)
-                {
-                    if (session->opt->mode == MODE_SERVER)
-                    {
-                        /* On a server we continue with running connect scripts next */
-                        multi->multi_state = CAS_WAITING_AUTH;
-                    }
-                    else
-                    {
-                        /* Skip the connect script related states */
-                        multi->multi_state = CAS_WAITING_OPTIONS_IMPORT;
-                    }
-                }
-
-                /* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */
-                flush_payload_buffer(ks);
-
-#ifdef MEASURE_TLS_HANDSHAKE_STATS
-                show_tls_performance_stats();
-#endif
-            }
+            session_move_active(multi, session, to_link_socket_info, ks);
+            state_change = true;
         }
 
         /* Reliable buffer to outgoing TCP/UDP (send up to CONTROL_SEND_ACK_MAX ACKs
Signature · 1 line
2.32.0 (Apple Git-132)
Acked-by on v2 1/1
#3 lore raw

[Openvpn-devel] [PATCH applied] Re: Extract session_move_active into its own function

This is the 2nd half of "patch 13 v2". It has a semi-ACK from Frank, and now a proper commit message as well :-)

Verified "it's only moved code" by hand (indent change), and lightly tested.

Whitespace error has been fixed by git, so the "whitespace fix" hunk coming up won't apply - but I can handle that :-)

Your patch has been applied to the master branch.

commit 4dbe508ab6d011d57f4acc71f3c0330deb852206

Author: Arne Schwabe
Date:   Tue Apr 26 15:23:24 2022 +0200
     Extract session_move_active into its own function

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

--
kind regards,

Gert Doering

Acked-by on v2 1/1
#4 lore raw
In reply to Arne Schwabe

As discussed on IRC, this is "13/28 v2, first half" (accidentially squashed).

Un-squashing these makes this one much clearer - only FULL_SYNC, and it does what it says on the lid.

Quick compile and client test passed.

Your patch has been applied to the master branch.

commit 04c3779922aed541755a20f415e7cb9d717ee7ed

Author: Arne Schwabe
Date:   Tue Apr 26 15:23:23 2022 +0200
     Change FULL_SYNC macro to no_pending_reliable_packets function

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

--
kind regards,

Gert Doering