dhcpv6: replace hash_ifname() with MD5 implementation
authorÁlvaro Fernández Rojas <noltari@gmail.com>
Tue, 13 Jan 2026 17:35:56 +0000 (18:35 +0100)
committerÁlvaro Fernández Rojas <noltari@gmail.com>
Sat, 24 Jan 2026 20:42:42 +0000 (21:42 +0100)
Replace hash_ifname() with new implementation based on MD5 hash.

This implementation generates the same output as:
https://github.com/openwrt/openwrt/commit/e1f2b666ff94f2b8a50ca000d69f5b5f0b89a27c

Link: https://github.com/openwrt/odhcp6c/pull/150
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
src/dhcpv6.c

index 49636821c21b7ec85ecd11930c52b58ff64c2915..05d55d34f3f2cee1f6d7d740273064509c78475a 100644 (file)
@@ -544,10 +544,21 @@ void dhcpv6_reset_stats(void)
        memset(&dhcpv6_stats, 0, sizeof(dhcpv6_stats));
 }
 
-uint32_t hash_ifname(const char *s) {
-       uint32_t h = 0;
-       while (*s) h = h * 31 + *s++;
-       return h;
+static uint32_t dhcpv6_generate_iface_iaid(const char *ifname) {
+       uint8_t hash[16] = {0};
+       uint32_t iaid;
+       md5_ctx_t md5;
+
+       md5_begin(&md5);
+       md5_hash(ifname, strlen(ifname), &md5);
+       md5_end(hash, &md5);
+
+       iaid = hash[0] << 24;
+       iaid |= hash[1] << 16;
+       iaid |= hash[2] << 8;
+       iaid |= hash[3];
+
+       return iaid;
 }
 
 int init_dhcpv6(const char *ifname)
@@ -574,7 +585,7 @@ int init_dhcpv6(const char *ifname)
        if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0)
                goto failure;
 
-       ifname_hash_iaid = hash_ifname(ifname);
+       ifname_hash_iaid = dhcpv6_generate_iface_iaid(ifname);
 
        ifindex = ifr.ifr_ifindex;