luci-base: fix Lua-side ip6hostid() datatype validation
authorJo-Philipp Wich <jo@mein.io>
Tue, 13 Feb 2018 13:29:21 +0000 (14:29 +0100)
committerJo-Philipp Wich <jo@mein.io>
Fri, 16 Feb 2018 16:05:48 +0000 (17:05 +0100)
A valid host ID as accepted by netifd must meet the following criteria:

 - Is either one of the two special "random" or "eui64" strings
 - Or is a valid IPv6 address according to inet_pton(AF_INET6)
 - Has the first 64 bit set to zero

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/luasrc/cbi/datatypes.lua

index df23aaf..a7e02f3 100644 (file)
@@ -169,8 +169,13 @@ function ipmask6(val)
 end
 
 function ip6hostid(val)
 end
 
 function ip6hostid(val)
-       if val and val:match("^[a-fA-F0-9:]+$") and (#val > 2) then
-               return (ip6addr("2001:db8:0:0" .. val) or ip6addr("2001:db8:0:0:" .. val))
+       if val == "eui64" or val == "random" then
+               return true
+       else
+               local addr = ip.IPv6(val)
+               if addr and addr:prefix() == 128 and addr:lower("::1:0:0:0:0") then
+                       return true
+               end
        end
 
        return false
        end
 
        return false