Calculate IPv6 netmask correctly.
authorAlan T. DeKok <aland@freeradius.org>
Sat, 6 Mar 2010 10:03:33 +0000 (11:03 +0100)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 6 Mar 2010 10:04:13 +0000 (11:04 +0100)
Closes bug #69

src/main/client.c

index d378d4b..7df88d3 100644 (file)
@@ -233,15 +233,29 @@ static int client_sane(RADCLIENT *client)
                               sizeof(client->ipaddr.ipaddr.ip6addr));
 
                } else if (client->prefix < 128) {
-                       int i;
                        uint32_t mask, *addr;
 
                        addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
-
-                       for (i = client->prefix; i < 128; i += 32) {
-                               mask = ~0;
-                               mask <<= ((128 - i) & 0x1f);
-                               addr[i / 32] &= mask;
+                       mask = ~ ((uint32_t) 0);
+                       mask <<= (32 - (client->prefix & 0x1f));
+                       mask = htonl(mask);
+
+                       switch ((client->prefix - 1) >> 5) {
+                       case 0:
+                               addr[0] &= mask;
+                               mask = 0;
+                               /* FALL-THROUGH */
+                       case 1:
+                               addr[1] &= mask;
+                               mask = 0;
+                               /* FALL-THROUGH */
+                       case 2:
+                               addr[2] &= mask;
+                               mask = 0;
+                               /* FALL-THROUGH */
+                       case 3:
+                               addr[3] &= mask;
+                         break;
                        }
                }
                break;