Attention is currently required from: Arne Schwabe.

Hello Arne Schwabe,

I'd like you to do a code review.
Please visit

    https://gerrit.openvpn.net/c/openvpn/+/2209?usp=email

to review the following change.


Change subject: dhcp: locate the UDP header via the IP header length
......................................................................

dhcp: locate the UDP header via the IP header length

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 <[email protected]>
---
M src/openvpn/dhcp.c
M src/openvpn/dhcp.h
M src/openvpn/proto.h
3 files changed, 32 insertions(+), 23 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/09/2209/1

diff --git a/src/openvpn/dhcp.c b/src/openvpn/dhcp.c
index 525d567..cbbf0d7 100644
--- a/src/openvpn/dhcp.c
+++ b/src/openvpn/dhcp.c
@@ -152,27 +152,44 @@
 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);
+
+    /* The UDP header follows any IP options. Skip fragments: later ones
+     * carry no UDP header, and the checksum covers the whole datagram. */
+    struct buffer udpbuf = *ipbuf;
+    if ((ntohs(ip->frag_off) & (OPENVPN_IP_MF | OPENVPN_IP_OFFMASK))
+        || 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;


--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2209?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I3630ac3defbb64e252ac3009fdd784153367e4fd
Gerrit-Change-Number: 2209
Gerrit-PatchSet: 1
Gerrit-Owner: Răzvan Cojocaru <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to