[Openvpn-devel,v2] clinat: locate the TCP/UDP header via the IP header length

Message ID 20261129171625.15648-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v2] clinat: locate the TCP/UDP header via the IP header length |

Commit Message

Gert Doering Nov. 29, 2026, 5:16 p.m. UTC
  From: Razvan Cojocaru <razvanc@mailbox.org>

client_nat_transform() adjusted the TCP/UDP checksum at a fixed offset
of 20 bytes. With IP options this rewrote the UDP destination port or
the TCP data offset and flags instead, and for non-first fragments it
modified payload bytes. Use the header length from the IP header and
skip the transport checksum for non-first fragments, as mss_fixup_ipv4()
does.

Change-Id: Ibd316e5c3e6a0fb600ec4817aa079554464fd942
Signed-off-by: Razvan Cojocaru <razvanc@mailbox.org>
Acked-by: Antonio Quartulli <antonio@mandelbit.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2208
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2208
This mail reflects revision 2 of this Change.

Acked-by according to Gerrit (reflected above):
Antonio Quartulli <antonio@mandelbit.com>
  

Patch

diff --git a/src/openvpn/clinat.c b/src/openvpn/clinat.c
index fc4acef..a046324 100644
--- a/src/openvpn/clinat.c
+++ b/src/openvpn/clinat.c
@@ -247,32 +247,45 @@ 
 
         ADJUST_CHECKSUM(accumulate, h->ip.check);
 
+        /* The TCP/UDP header follows any IP options, and only the first
+         * fragment carries it. */
+        const int hlen = OPENVPN_IPH_GET_LEN(h->ip.version_len);
+        struct buffer l4 = *ipbuf;
+        if ((ntohs(h->ip.frag_off) & OPENVPN_IP_OFFMASK) || hlen < (int)sizeof(struct openvpn_iphdr)
+            || !buf_advance(&l4, hlen))
+        {
+            return;
+        }
+
         if (h->ip.protocol == OPENVPN_IPPROTO_TCP)
         {
-            if (BLENZ(ipbuf) >= sizeof(struct openvpn_iphdr) + sizeof(struct openvpn_tcphdr))
+            if (BLENZ(&l4) >= sizeof(struct openvpn_tcphdr))
             {
-                ADJUST_CHECKSUM(accumulate, h->u.tcp.check);
+                struct openvpn_tcphdr *tcp = (struct openvpn_tcphdr *)BPTR(&l4);
+                ADJUST_CHECKSUM(accumulate, tcp->check);
             }
         }
         else if (h->ip.protocol == OPENVPN_IPPROTO_UDP)
         {
-            if (BLENZ(ipbuf) >= sizeof(struct openvpn_iphdr) + sizeof(struct openvpn_udphdr))
+            if (BLENZ(&l4) >= sizeof(struct openvpn_udphdr))
             {
+                struct openvpn_udphdr *udp = (struct openvpn_udphdr *)BPTR(&l4);
+
                 /* RFC 768: a UDP checksum of 0 means "no checksum computed".
                  * Do not run the incremental adjustment over a non-checksum,
                  * or we will write a bogus non-zero value into the field.
                  */
-                if (h->u.udp.check)
+                if (udp->check)
                 {
-                    ADJUST_CHECKSUM(accumulate, h->u.udp.check);
+                    ADJUST_CHECKSUM(accumulate, udp->check);
 
-                    if (!h->u.udp.check)
+                    if (!udp->check)
                     {
                         /* RFC 768: a computed checksum of 0 must be transmitted
                          * as 0xFFFF (one-complement), because 0 is reserved for
                          * "no checksum computed"
                          */
-                        h->u.udp.check = 0xFFFF;
+                        udp->check = 0xFFFF;
                     }
                 }
             }