[Openvpn-devel,v2] dhcp: locate the UDP header via the IP header length

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

Commit Message

Gert Doering Nov. 26, 2026, 11:58 a.m. UTC
  From: Razvan Cojocaru <razvanc@mailbox.org>

dhcp_extract_router_msg() assumed a 20-byte IP header, so DHCP replies
carrying IP options were not recognised. Use the IHL to find the UDP
header and skip fragments, whose UDP checksum covers data we don't
have. Drop struct dhcp_full, no longer used.

Change-Id: I3630ac3defbb64e252ac3009fdd784153367e4fd
Signed-off-by: Razvan Cojocaru <razvanc@mailbox.org>
Acked-by: Lev Stipakov <lstipakov@gmail.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2209
---

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/+/2209
This mail reflects revision 2 of this Change.

Acked-by according to Gerrit (reflected above):
Lev Stipakov <lstipakov@gmail.com>
  

Patch

diff --git a/src/openvpn/dhcp.c b/src/openvpn/dhcp.c
index 525d567..8c9e520 100644
--- a/src/openvpn/dhcp.c
+++ b/src/openvpn/dhcp.c
@@ -152,27 +152,49 @@ 
 in_addr_t
 dhcp_extract_router_msg(struct buffer *ipbuf)
 {
-    struct dhcp_full *df = (struct dhcp_full *)BPTR(ipbuf);
-    const int optlen =
-        BLEN(ipbuf)
-        - (int)(sizeof(struct openvpn_iphdr) + sizeof(struct openvpn_udphdr) + sizeof(struct dhcp));
-
-    if (optlen >= 0 && df->ip.protocol == OPENVPN_IPPROTO_UDP
-        && df->udp.source == htons(BOOTPS_PORT) && df->udp.dest == htons(BOOTPC_PORT)
-        && df->dhcp.op == BOOTREPLY)
+    if (BLENZ(ipbuf) < sizeof(struct openvpn_iphdr))
     {
-        const int message_type = get_dhcp_message_type(&df->dhcp, optlen);
+        return 0;
+    }
+
+    const struct openvpn_iphdr *ip = (const struct openvpn_iphdr *)BPTR(ipbuf);
+    const int hlen = OPENVPN_IPH_GET_LEN(ip->version_len);
+
+    /* Skip fragments: later ones carry no UDP header, and the checksum
+     * covers the whole datagram. */
+    if (ntohs(ip->frag_off) & (OPENVPN_IP_MF | OPENVPN_IP_OFFMASK))
+    {
+        return 0;
+    }
+
+    /* The UDP header follows any IP options. */
+    struct buffer udpbuf = *ipbuf;
+    if (hlen < (int)sizeof(struct openvpn_iphdr) || !buf_advance(&udpbuf, hlen))
+    {
+        return 0;
+    }
+
+    struct openvpn_udphdr *udp = (struct openvpn_udphdr *)BPTR(&udpbuf);
+    struct dhcp *dhcp = (struct dhcp *)(udp + 1);
+    const int optlen =
+        BLEN(&udpbuf) - (int)(sizeof(struct openvpn_udphdr) + sizeof(struct dhcp));
+
+    if (optlen >= 0 && ip->protocol == OPENVPN_IPPROTO_UDP
+        && udp->source == htons(BOOTPS_PORT) && udp->dest == htons(BOOTPC_PORT)
+        && dhcp->op == BOOTREPLY)
+    {
+        const int message_type = get_dhcp_message_type(dhcp, optlen);
         if (message_type == DHCPACK || message_type == DHCPOFFER)
         {
             /* get the router IP address while padding out all DHCP router options */
-            const in_addr_t ret = do_extract(&df->dhcp, optlen);
+            const in_addr_t ret = do_extract(dhcp, optlen);
 
             /* recompute the UDP checksum */
-            df->udp.check = 0;
-            df->udp.check = htons(ip_checksum(
-                AF_INET, (uint8_t *)&df->udp,
+            udp->check = 0;
+            udp->check = htons(ip_checksum(
+                AF_INET, (uint8_t *)udp,
                 sizeof(struct openvpn_udphdr) + sizeof(struct dhcp) + optlen,
-                (uint8_t *)&df->ip.saddr, (uint8_t *)&df->ip.daddr, OPENVPN_IPPROTO_UDP));
+                (const uint8_t *)&ip->saddr, (const uint8_t *)&ip->daddr, OPENVPN_IPPROTO_UDP));
 
             /* only return the extracted Router address if DHCPACK */
             if (message_type == DHCPACK)
diff --git a/src/openvpn/dhcp.h b/src/openvpn/dhcp.h
index 38ebf8d..8517b92 100644
--- a/src/openvpn/dhcp.h
+++ b/src/openvpn/dhcp.h
@@ -80,15 +80,6 @@ 
     uint32_t magic;     /* must be 0x63825363 (network order) */
 };
 
-struct dhcp_full
-{
-    struct openvpn_iphdr ip;
-    struct openvpn_udphdr udp;
-    struct dhcp dhcp;
-#define DHCP_OPTIONS_BUFFER_SIZE 256
-    uint8_t options[DHCP_OPTIONS_BUFFER_SIZE];
-};
-
 #pragma pack()
 
 in_addr_t dhcp_extract_router_msg(struct buffer *ipbuf);
diff --git a/src/openvpn/proto.h b/src/openvpn/proto.h
index 14f9264..0841ad5 100644
--- a/src/openvpn/proto.h
+++ b/src/openvpn/proto.h
@@ -96,6 +96,7 @@ 
     uint16_t tot_len;
     uint16_t id;
 
+#define OPENVPN_IP_MF      0x2000
 #define OPENVPN_IP_OFFMASK 0x1fff
     uint16_t frag_off;