diff options
author | Jo-Philipp Wich <jo@mein.io> | 2018-03-12 16:12:18 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2018-03-12 16:12:18 +0100 |
commit | 28e3b328545529c19429ce88c7d1769e64e2de0f (patch) | |
tree | 3d7d85d7d4d1202199bad98df125f2af88b73c86 /modules/luci-base | |
parent | dfba318140e1a84359e221b7a9154337595dbded (diff) |
treewide: unify mac address handling
Use the new luci.ip MAC address facilities to parse and verify MAC addresses
in a common way, instead of relying on various ad-hoc solutions.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base')
-rw-r--r-- | modules/luci-base/luasrc/cbi/datatypes.lua | 18 | ||||
-rw-r--r-- | modules/luci-base/luasrc/model/network.lua | 5 | ||||
-rw-r--r-- | modules/luci-base/luasrc/sys.lua | 21 | ||||
-rw-r--r-- | modules/luci-base/luasrc/tools/status.lua | 15 |
4 files changed, 19 insertions, 40 deletions
diff --git a/modules/luci-base/luasrc/cbi/datatypes.lua b/modules/luci-base/luasrc/cbi/datatypes.lua index a7e02f350..55cdf8a74 100644 --- a/modules/luci-base/luasrc/cbi/datatypes.lua +++ b/modules/luci-base/luasrc/cbi/datatypes.lua @@ -196,23 +196,7 @@ function portrange(val) end function macaddr(val) - if val and val:match( - "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" .. - "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$" - ) then - local parts = util.split( val, ":" ) - - for i = 1,6 do - parts[i] = tonumber( parts[i], 16 ) - if parts[i] < 0 or parts[i] > 255 then - return false - end - end - - return true - end - - return false + return ip.checkmac(val) and true or false end function hostname(val) diff --git a/modules/luci-base/luasrc/model/network.lua b/modules/luci-base/luasrc/model/network.lua index c8ec5364e..056fc67b1 100644 --- a/modules/luci-base/luasrc/model/network.lua +++ b/modules/luci-base/luasrc/model/network.lua @@ -330,7 +330,7 @@ function init(cursor) if i.family == "packet" then _interfaces[name].flags = i.flags _interfaces[name].stats = i.data - _interfaces[name].macaddr = i.addr + _interfaces[name].macaddr = ipc.checkmac(i.addr) elseif i.family == "inet" then _interfaces[name].ipaddrs[#_interfaces[name].ipaddrs+1] = ipc.IPv4(i.addr, i.netmask) elseif i.family == "inet6" then @@ -1233,8 +1233,7 @@ function interface.name(self) end function interface.mac(self) - local mac = self:_ubus("macaddr") - return mac and mac:upper() + return ipc.checkmac(self:_ubus("macaddr")) end function interface.ipaddrs(self) diff --git a/modules/luci-base/luasrc/sys.lua b/modules/luci-base/luasrc/sys.lua index b00feda5a..12b20e4c3 100644 --- a/modules/luci-base/luasrc/sys.lua +++ b/modules/luci-base/luasrc/sys.lua @@ -138,20 +138,21 @@ local function _nethints(what, callback) luci.ip.neighbors(nil, function(neigh) if neigh.mac and neigh.family == 4 then - _add(what, neigh.mac:upper(), neigh.dest:string(), nil, nil) + _add(what, neigh.mac:string(), neigh.dest:string(), nil, nil) elseif neigh.mac and neigh.family == 6 then - _add(what, neigh.mac:upper(), nil, neigh.dest:string(), nil) + _add(what, neigh.mac:string(), nil, neigh.dest:string(), nil) end end) if fs.access("/etc/ethers") then for e in io.lines("/etc/ethers") do - mac, name = e:match("^([a-fA-F0-9:]+)%s+(%S+)") + mac, name = e:match("^([a-fA-F0-9:-]+)%s+(%S+)") + mac = luci.ip.checkmac(mac) if mac and name then - if luci.ip.IPv4(name) then - _add(what, mac:upper(), name, nil, nil) + if luci.ip.checkip4(name) then + _add(what, mac, name, nil, nil) else - _add(what, mac:upper(), nil, nil, name) + _add(what, mac, nil, nil, name) end end end @@ -162,8 +163,9 @@ local function _nethints(what, callback) if s.leasefile and fs.access(s.leasefile) then for e in io.lines(s.leasefile) do mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)") + mac = luci.ip.checkmac(mac) if mac and ip then - _add(what, mac:upper(), ip, nil, name ~= "*" and name) + _add(what, mac, ip, nil, name ~= "*" and name) end end end @@ -173,7 +175,10 @@ local function _nethints(what, callback) cur:foreach("dhcp", "host", function(s) for mac in luci.util.imatch(s.mac) do - _add(what, mac:upper(), s.ip, nil, s.name) + mac = luci.ip.checkmac(mac) + if mac then + _add(what, mac, s.ip, nil, s.name) + end end end) diff --git a/modules/luci-base/luasrc/tools/status.lua b/modules/luci-base/luasrc/tools/status.lua index 95ff46df1..501211181 100644 --- a/modules/luci-base/luasrc/tools/status.lua +++ b/modules/luci-base/luasrc/tools/status.lua @@ -4,6 +4,7 @@ module("luci.tools.status", package.seeall) local uci = require "luci.model.uci".cursor() +local ipc = require "luci.ip" local function dhcp_leases_common(family) local rv = { } @@ -31,7 +32,7 @@ local function dhcp_leases_common(family) if family == 4 and not ip:match(":") then rv[#rv+1] = { expires = (expire ~= 0) and os.difftime(expire, os.time()), - macaddr = mac, + macaddr = ipc.checkmac(mac) or "00:00:00:00:00:00", ipaddr = ip, hostname = (name ~= "*") and name } @@ -74,19 +75,9 @@ local function dhcp_leases_common(family) hostname = (name ~= "-") and name } elseif ip and iaid == "ipv4" and family == 4 then - local mac, mac1, mac2, mac3, mac4, mac5, mac6 - if duid and type(duid) == "string" then - mac1, mac2, mac3, mac4, mac5, mac6 = duid:match("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$") - end - if not (mac1 and mac2 and mac3 and mac4 and mac5 and mac6) then - mac = "FF:FF:FF:FF:FF:FF" - else - mac = mac1..":"..mac2..":"..mac3..":"..mac4..":"..mac5..":"..mac6 - end rv[#rv+1] = { expires = (expire >= 0) and os.difftime(expire, os.time()), - macaddr = duid, - macaddr = mac:lower(), + macaddr = ipc.checkmac(duid:gsub("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$", "%1:%2:%3:%4:%5:%6")) or "00:00:00:00:00:00", ipaddr = ip, hostname = (name ~= "-") and name } |