dhcpv6: change dhcpv6 message type check in relay
authorPaul Donald <[email protected]>
Fri, 3 Oct 2025 12:59:55 +0000 (14:59 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Sun, 19 Oct 2025 17:33:20 +0000 (19:33 +0200)
When compiled, the switch instruction jump table O(1) is more efficient
than multiple logical comparisons in the if block (4-5 checks).

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

index b92790e6bb9e40e40903fba7b68da43221e839f4..abb0f362b4b9e96232dbd89b09b3dca94844e8e5 100644 (file)
@@ -895,11 +895,29 @@ static void relay_client_request(struct sockaddr_in6 *source,
        struct odhcpd_ipaddr *ip;
        struct sockaddr_in6 s;
 
-       if (h->msg_type == DHCPV6_MSG_RELAY_REPL ||
-           h->msg_type == DHCPV6_MSG_RECONFIGURE ||
-           h->msg_type == DHCPV6_MSG_REPLY ||
-           h->msg_type == DHCPV6_MSG_ADVERTISE)
-               return; /* Invalid message types for client */
+       switch (h->msg_type) {
+       /* Valid message types from 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:
+       case DHCPV6_MSG_DHCPV4_QUERY:
+               break;
+       /* Invalid message types from clients i.e. server messages */
+       case DHCPV6_MSG_ADVERTISE:
+       case DHCPV6_MSG_REPLY:
+       case DHCPV6_MSG_RECONFIGURE:
+       case DHCPV6_MSG_RELAY_REPL:
+       case DHCPV6_MSG_DHCPV4_RESPONSE:
+               return;
+       default:
+               break;
+       }
 
        debug("Got a DHCPv6-request on %s", iface->name);