luci-base: break circular luci.config <> luci.model.uci dependency
[project/luci.git] / modules / luci-base / luasrc / model / network.lua
index 48a0339..0f58c16 100644 (file)
@@ -113,6 +113,51 @@ local function _wifi_state()
        return _ubuswificache
 end
 
+local function _wifi_state_by_sid(sid)
+       local t1, n1 = _uci:get("wireless", sid)
+       if t1 == "wifi-iface" and n1 ~= nil then
+               local radioname, radiostate
+               for radioname, radiostate in pairs(_wifi_state()) do
+                       if type(radiostate) == "table" and
+                          type(radiostate.interfaces) == "table"
+                       then
+                               local netidx, netstate
+                               for netidx, netstate in ipairs(radiostate.interfaces) do
+                                       if type(netstate) == "table" and
+                                          type(netstate.section) == "string"
+                                       then
+                                               local t2, n2 = _uci:get("wireless", netstate.section)
+                                               if t1 == t2 and n1 == n2 then
+                                                       return radioname, radiostate, netstate
+                                               end
+                                       end
+                               end
+                       end
+               end
+       end
+end
+
+local function _wifi_state_by_ifname(ifname)
+       if type(ifname) == "string" then
+               local radioname, radiostate
+               for radioname, radiostate in pairs(_wifi_state()) do
+                       if type(radiostate) == "table" and
+                          type(radiostate.interfaces) == "table"
+                       then
+                               local netidx, netstate
+                               for netidx, netstate in ipairs(radiostate.interfaces) do
+                                       if type(netstate) == "table" and
+                                          type(netstate.ifname) == "string" and
+                                          netstate.ifname == ifname
+                                       then
+                                               return radioname, radiostate, netstate
+                                       end
+                               end
+                       end
+               end
+       end
+end
+
 function _wifi_iface(x)
        local _, p
        for _, p in ipairs(IFACE_PATTERNS_WIRELESS) do
@@ -230,51 +275,6 @@ local function _wifi_netid_by_netname(name)
        return netid
 end
 
-local function _wifi_state_by_sid(sid)
-       local t1, n1 = _uci:get("wireless", sid)
-       if t1 == "wifi-iface" and n1 ~= nil then
-               local radioname, radiostate
-               for radioname, radiostate in pairs(_wifi_state()) do
-                       if type(radiostate) == "table" and
-                          type(radiostate.interfaces) == "table"
-                       then
-                               local netidx, netstate
-                               for netidx, netstate in ipairs(radiostate.interfaces) do
-                                       if type(netstate) == "table" and
-                                          type(netstate.section) == "string"
-                                       then
-                                               local t2, n2 = _uci:get("wireless", netstate.section)
-                                               if t1 == t2 and n1 == n2 then
-                                                       return radioname, radiostate, netstate
-                                               end
-                                       end
-                               end
-                       end
-               end
-       end
-end
-
-local function _wifi_state_by_ifname(ifname)
-       if type(ifname) == "string" then
-               local radioname, radiostate
-               for radioname, radiostate in pairs(_wifi_state()) do
-                       if type(radiostate) == "table" and
-                          type(radiostate.interfaces) == "table"
-                       then
-                               local netidx, netstate
-                               for netidx, netstate in ipairs(radiostate.interfaces) do
-                                       if type(netstate) == "table" and
-                                          type(netstate.ifname) == "string" and
-                                          netstate.ifname == ifname
-                                       then
-                                               return radioname, radiostate, netstate
-                                       end
-                               end
-                       end
-               end
-       end
-end
-
 function _iface_virtual(x)
        local _, p
        for _, p in ipairs(IFACE_PATTERNS_VIRTUAL) do
@@ -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
@@ -543,6 +543,9 @@ end
 function del_network(self, n)
        local r = _uci:delete("network", n)
        if r then
+               _uci:delete_all("luci", "ifstate",
+                       function(s) return (s.interface == n) end)
+
                _uci:delete_all("network", "alias",
                        function(s) return (s.interface == n) end)
 
@@ -626,7 +629,7 @@ function get_interface(self, i)
        if _interfaces[i] or _wifi_iface(i) then
                return interface(i)
        else
-               local netid = _wifi_netid_by_netname(i)
+               local netid = _wifi_netid_by_sid(i)
                return netid and interface(netid)
        end
 end
@@ -998,7 +1001,15 @@ function protocol.ip6addrs(self)
 
        if type(addrs) == "table" then
                for n, addr in ipairs(addrs) do
-                       rv[#rv+1] = "%s1/%d" %{ addr.address, addr.mask }
+                       if type(addr["local-address"]) == "table" and
+                          type(addr["local-address"].mask) == "number" and
+                          type(addr["local-address"].address) == "string"
+                       then
+                               rv[#rv+1] = "%s/%d" %{
+                                       addr["local-address"].address,
+                                       addr["local-address"].mask
+                               }
+                       end
                end
        end
 
@@ -1222,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)
@@ -1418,7 +1428,7 @@ function wifidev.hwmodes(self)
 end
 
 function wifidev.get_i18n(self)
-       local t = "Generic"
+       local t = self.iwinfo.hardware_name or "Generic"
        if self.iwinfo.type == "wl" then
                t = "Broadcom"
        end