dhcpv6: move dhcpv6 message type check for early exit
authorPaul Donald <[email protected]>
Sun, 19 Oct 2025 17:30:38 +0000 (19:30 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Sun, 19 Oct 2025 17:32:56 +0000 (19:32 +0200)
Avoid doing a bunch of work if the message type we received is not a
client type and does not warrant a reply. Exit early.

Clarify and comment message type handling.

Signed-off-by: Paul Donald <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/279
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/dhcpv6.c
src/dhcpv6.h

index 73ce0b3d1c8920c93d08b48f35f6db842bf2c268..b92790e6bb9e40e40903fba7b68da43221e839f4 100644 (file)
@@ -318,6 +318,36 @@ static void handle_client_request(void *addr, void *data, size_t len,
        if (len < sizeof(*hdr))
                return;
 
+       switch (hdr->msg_type) {
+       /* Valid message types for clients */
+       case DHCPV6_MSG_SOLICIT:
+       case DHCPV6_MSG_REQUEST:
+       case DHCPV6_MSG_CONFIRM:
+       case DHCPV6_MSG_RENEW:
+       case DHCPV6_MSG_REBIND:
+       case DHCPV6_MSG_RELEASE:
+       case DHCPV6_MSG_DECLINE:
+       case DHCPV6_MSG_INFORMATION_REQUEST:
+       case DHCPV6_MSG_RELAY_FORW:
+#ifdef DHCPV4_SUPPORT
+       /* if we include DHCPV4 support, handle this message type */
+       case DHCPV6_MSG_DHCPV4_QUERY:
+#endif
+               break;
+       /* Invalid message types for clients i.e. server messages */
+       case DHCPV6_MSG_ADVERTISE:
+       case DHCPV6_MSG_REPLY:
+       case DHCPV6_MSG_RECONFIGURE:
+       case DHCPV6_MSG_RELAY_REPL:
+#ifndef DHCPV4_SUPPORT
+       /* if we omit DHCPV4 support, ignore this client message type */
+       case DHCPV6_MSG_DHCPV4_QUERY:
+#endif
+       case DHCPV6_MSG_DHCPV4_RESPONSE:
+       default:
+               return;
+       }
+
        debug("Got a DHCPv6-request on %s", iface->name);
 
        /* Construct reply message */
@@ -566,32 +596,6 @@ static void handle_client_request(void *addr, void *data, size_t len,
        if (hdr->msg_type == DHCPV6_MSG_RELAY_FORW)
                handle_nested_message(data, len, &hdr, &opts, &opts_end, iov);
 
-       switch (hdr->msg_type) {
-       case DHCPV6_MSG_SOLICIT:
-       case DHCPV6_MSG_REQUEST:
-       case DHCPV6_MSG_CONFIRM:
-       case DHCPV6_MSG_RENEW:
-       case DHCPV6_MSG_REBIND:
-       case DHCPV6_MSG_RELEASE:
-       case DHCPV6_MSG_DECLINE:
-       case DHCPV6_MSG_INFORMATION_REQUEST:
-       case DHCPV6_MSG_RELAY_FORW:
-#ifdef DHCPV4_SUPPORT
-       case DHCPV6_MSG_DHCPV4_QUERY:
-#endif
-               break; /* Valid message types for clients */
-       case DHCPV6_MSG_ADVERTISE:
-       case DHCPV6_MSG_REPLY:
-       case DHCPV6_MSG_RECONFIGURE:
-       case DHCPV6_MSG_RELAY_REPL:
-       case DHCPV6_MSG_DHCPV4_RESPONSE:
-#ifndef DHCPV4_SUPPORT
-       case DHCPV6_MSG_DHCPV4_QUERY:
-#endif
-       default:
-               return; /* Invalid message types for clients */
-       }
-
        if (!IN6_IS_ADDR_MULTICAST((struct in6_addr *)dest_addr) && iov[IOV_NESTED].iov_len == 0 &&
            (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_CONFIRM ||
             hdr->msg_type == DHCPV6_MSG_REBIND || hdr->msg_type == DHCPV6_MSG_INFORMATION_REQUEST))
index a3944539af02603513f68f13326f68f3e1e72ce2..4220fe2b757cbe7cb59d9c1123c7ab28b8501007 100644 (file)
@@ -22,6 +22,7 @@
 #define DHCPV6_CLIENT_PORT 546
 #define DHCPV6_SERVER_PORT 547
 
+/* RFC8415 */
 #define DHCPV6_MSG_SOLICIT 1
 #define DHCPV6_MSG_ADVERTISE 2
 #define DHCPV6_MSG_REQUEST 3
@@ -35,6 +36,7 @@
 #define DHCPV6_MSG_INFORMATION_REQUEST 11
 #define DHCPV6_MSG_RELAY_FORW 12
 #define DHCPV6_MSG_RELAY_REPL 13
+/* RFC7341 */
 #define DHCPV6_MSG_DHCPV4_QUERY 20
 #define DHCPV6_MSG_DHCPV4_RESPONSE 21