libs/core: add luci.model.network.wifinet.id()
[project/luci.git] / libs / core / luasrc / model / network.lua
index bb98f82..56597a2 100644 (file)
@@ -17,8 +17,8 @@ limitations under the License.
 
 ]]--
 
-local type, pairs, ipairs, loadfile, table, tonumber, math, i18n
-       = type, pairs, ipairs, loadfile, table, tonumber, math, luci.i18n
+local type, next, pairs, ipairs, loadfile, table, tonumber, math, i18n
+       = type, next, pairs, ipairs, loadfile, table, tonumber, math, luci.i18n
 
 local nxo = require "nixio"
 local ipc = require "luci.ip"
@@ -102,7 +102,7 @@ function _set(c, s, o, v)
                if type(v) == "boolean" then v = v and "1" or "0" end
                return uci_r:set(c, s, o, v)
        else
-               return uci_r:del(c, s, o, v)
+               return uci_r:delete(c, s, o)
        end
 end
 
@@ -150,12 +150,19 @@ function _wifi_lookup(ifn)
        end
 end
 
+function _iface_virtual(x)
+       return (
+               x:match("^6in4-%w") or x:match("^6to4-%w") or x:match("^3g-%w") or
+               x:match("^ppp-%w") or x:match("^pppoe-%w") or x:match("^pppoa-%w") or
+               x:match("^relay-%w")
+       )
+end
+
 function _iface_ignore(x)
        return (
                x:match("^wmaster%d") or x:match("^wifi%d") or x:match("^hwsim%d") or
-               x:match("^imq%d") or x:match("^mon.wlan%d") or x:match("^6in4-%w") or
-               x:match("^3g-%w") or x:match("^ppp-%w") or x:match("^pppoe-%w") or
-               x:match("^pppoa-%w") or x == "lo"
+               x:match("^imq%d") or x:match("^mon.wlan%d") or
+               x == "sit0" or x == "lo" or _iface_virtual(x)
        )
 end
 
@@ -174,7 +181,7 @@ function init(cursor)
                local name = i.name:match("[^:]+")
                local prnt = name:match("^([^%.]+)%.")
 
-               if not _iface_ignore(name) then
+               if _iface_virtual(name) or not _iface_ignore(name) then
                        ifs[name] = ifs[name] or {
                                idx      = i.ifindex or n,
                                name     = name,
@@ -242,10 +249,19 @@ function has_ipv6(self)
 end
 
 function add_network(self, n, options)
-       if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not self:get_network(n) then
+       local oldnet = self:get_network(n)
+       if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not oldnet then
                if uci_r:section("network", "interface", n, options) then
                        return network(n)
                end
+       elseif oldnet and oldnet:is_empty() then
+               if options then
+                       local k, v
+                       for k, v in pairs(options) do
+                               oldnet:set(k, v)
+                       end
+               end
+               return oldnet
        end
 end
 
@@ -275,26 +291,14 @@ end
 function del_network(self, n)
        local r = uci_r:delete("network", n)
        if r then
-               uci_r:foreach("network", "alias",
-                       function(s)
-                               if s.interface == n then
-                                       uci_r:delete("network", s['.name'])
-                               end
-                       end)
+               uci_r:delete_all("network", "alias",
+                       function(s) return (s.interface == n) end)
 
-               uci_r:foreach("network", "route",
-                       function(s)
-                               if s.interface == n then
-                                       uci_r:delete("network", s['.name'])
-                               end
-                       end)
+               uci_r:delete_all("network", "route",
+                       function(s) return (s.interface == n) end)
 
-               uci_r:foreach("network", "route6",
-                       function(s)
-                               if s.interface == n then
-                                       uci_r:delete("network", s['.name'])
-                               end
-                       end)
+               uci_r:delete_all("network", "route6",
+                       function(s) return (s.interface == n) end)
 
                uci_r:foreach("wireless", "wifi-iface",
                        function(s)
@@ -302,8 +306,6 @@ function del_network(self, n)
                                        uci_r:delete("wireless", s['.name'], "network")
                                end
                        end)
-
-               uci_r:delete("network", n)
        end
        return r
 end
@@ -372,14 +374,30 @@ end
 function get_interfaces(self)
        local iface
        local ifaces = { }
+       local seen = { }
+       local nfs = { }
 
        -- find normal interfaces
+       uci_r:foreach("network", "interface",
+               function(s)
+                       for iface in utl.imatch(s.ifname) do
+                               if not _iface_ignore(iface) and not _wifi_iface(iface) then
+                                       seen[iface] = true
+                                       nfs[iface] = interface(iface)
+                               end
+                       end
+               end)
+
        for iface in utl.kspairs(ifs) do
-               if not _iface_ignore(iface) and not _wifi_iface(iface) then
-                       ifaces[#ifaces+1] = interface(iface)
+               if not (seen[iface] or _iface_ignore(iface) or _wifi_iface(iface)) then
+                       nfs[iface] = interface(iface)
                end
        end
 
+       for iface in utl.kspairs(nfs) do
+               ifaces[#ifaces+1] = nfs[iface]
+       end
+
        -- find wifi interfaces
        local num = { }
        local wfs = { }
@@ -464,6 +482,24 @@ function network._get(self, opt)
        return v or ""
 end
 
+function network._ip(self, opt, family, list)
+       local ip = uci_s:get("network", self.sid, opt)
+       local fc = (family == 6) and ipc.IPv6 or ipc.IPv4
+       if ip or list then
+               if list then
+                       local l = { }
+                       for ip in utl.imatch(ip) do
+                               ip = fc(ip)
+                               if ip then l[#l+1] = ip:string() end
+                       end
+                       return l
+               else
+                       ip = fc(ip)
+                       return ip and ip:string()
+               end
+       end
+end
+
 function network.get(self, opt)
        return _get("network", self.sid, opt)
 end
@@ -476,13 +512,17 @@ function network.ifname(self)
        local p = self:proto()
        if self:is_bridge() then
                return "br-" .. self.sid
+       elseif self:proto() == "relay" then
+               return uci_s:get("network", self.sid, "up") == "1" and "lo" or nil
        elseif self:is_virtual() then
                return p .. "-" .. self.sid
        else
-               local dev = self:_get("ifname") or
-                       uci_r:get("network", self.sid, "ifname")
+               local num = { }
+               local dev = uci_r:get("network", self.sid, "ifname") or
+                       uci_s:get("network", self.sid, "ifname")
 
-               dev = dev and dev:match("%S+")
+               dev = (type(dev) == "table") and dev[1] or dev
+               dev = (dev ~= nil) and dev:match("%S+")
 
                if not dev then
                        uci_r:foreach("wireless", "wifi-iface",
@@ -504,11 +544,21 @@ function network.ifname(self)
 end
 
 function network.device(self)
-       local dev = self:_get("device")
+       local dev = uci_r:get("network", self.sid, "device") or
+               uci_s:get("network", self.sid, "device")
+
+       dev = (type(dev) == "table") and dev[1] or dev
+
        if not dev or dev:match("[^%w%-%.%s]") then
-               dev = uci_r:get("network", self.sid, "ifname")
+               dev = uci_r:get("network", self.sid, "ifname") or
+                       uci_s:get("network", self.sid, "ifname")
+
+               dev = (type(dev) == "table") and dev[1] or dev
+       end
+
+       for dev in utl.imatch(dev) do
+               return dev
        end
-       return dev
 end
 
 function network.proto(self)
@@ -523,6 +573,77 @@ function network.name(self)
        return self.sid
 end
 
+function network.uptime(self)
+       local cnt = tonumber(uci_s:get("network", self.sid, "connect_time"))
+       if cnt ~= nil then
+               return nxo.sysinfo().uptime - cnt
+       else
+               return 0
+       end
+end
+
+function network.expires(self)
+       local a = tonumber(uci_s:get("network", self.sid, "lease_acquired"))
+       local l = tonumber(uci_s:get("network", self.sid, "lease_lifetime"))
+       if a and l then
+               l = l - (nxo.sysinfo().uptime - a)
+               return l > 0 and l or 0
+       end
+       return -1
+end
+
+function network.metric(self)
+       return tonumber(uci_s:get("network", self.sid, "metric")) or 0
+end
+
+function network.ipaddr(self)
+       return self:_ip("ipaddr", 4)
+end
+
+function network.netmask(self)
+       return self:_ip("netmask", 4)
+end
+
+function network.gwaddr(self)
+       return self:_ip("gateway", 4)
+end
+
+function network.dnsaddrs(self)
+       return self:_ip("dns", 4, true)
+end
+
+function network.ip6addr(self)
+       local ip6 = self:_ip("ip6addr", 6)
+       if not ip6 then
+               local ifc = ifs[self:ifname()]
+               if ifc and ifc.ip6addrs then
+                       local a
+                       for _, a in ipairs(ifc.ip6addrs) do
+                               if not a:is6linklocal() then
+                                       ip6 = a:string()
+                                       break
+                               end
+                       end
+               end
+       end
+       return ip6
+end
+
+function network.gw6addr(self)
+       local ip6 = self:_ip("ip6gw", 6)
+       if not ip6 then
+               local dr6 = sys.net.defaultroute6()
+               if dr6 and dr6.device == self:ifname() then
+                       return dr6.nexthop:string()
+               end
+       end
+       return ip6
+end
+
+function network.dns6addrs(self)
+       return self:_ip("dns", 6, true)
+end
+
 function network.is_bridge(self)
        return (self:type() == "bridge")
 end
@@ -530,8 +651,8 @@ end
 function network.is_virtual(self)
        local p = self:proto()
        return (
-               p == "3g" or p == "6in4" or p == "ppp" or
-               p == "pppoe" or p == "pppoa"
+               p == "3g" or p == "6in4" or p == "6to4" or p == "ppp" or
+               p == "pppoe" or p == "pppoa" or p == "relay"
        )
 end
 
@@ -822,7 +943,9 @@ function interface.get_network(self)
        if not self.network then
                local net
                for _, net in ipairs(_M:get_networks()) do
-                       if net:contains_interface(self.ifname) then
+                       if net:contains_interface(self.ifname) or
+                          net:ifname() == self.ifname
+                       then
                                self.network = net
                                return net
                        end
@@ -839,7 +962,8 @@ end
 
 wifidev = utl.class()
 function wifidev.__init__(self, dev)
-       self.sid = dev
+       self.sid    = dev
+       self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
 end
 
 function wifidev.get(self, opt)
@@ -854,6 +978,33 @@ function wifidev.name(self)
        return self.sid
 end
 
+function wifidev.hwmodes(self)
+       local l = self.iwinfo.hwmodelist
+       if l and next(l) then
+               return l
+       else
+               return { b = true, g = true }
+       end
+end
+
+function wifidev.get_i18n(self)
+       local t = "Generic"
+       if self.iwinfo.type == "wl" then
+               t = "Broadcom"
+       elseif self.iwinfo.type == "madwifi" then
+               t = "Atheros"
+       end
+
+       local m = ""
+       local l = self:hwmodes()
+       if l.a then m = m .. "a" end
+       if l.b then m = m .. "b" end
+       if l.g then m = m .. "g" end
+       if l.n then m = m .. "n" end
+
+       return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
+end
+
 function wifidev.is_up(self)
        local up = false
 
@@ -924,21 +1075,22 @@ wifinet = utl.class()
 function wifinet.__init__(self, net, data)
        self.sid = net
 
-       local dev = uci_s:get("wireless", self.sid, "ifname")
-       if not dev then
-               local num = { }
-               uci_r:foreach("wireless", "wifi-iface",
-                       function(s)
-                               if s.device then
-                                       num[s.device] = num[s.device] and num[s.device] + 1 or 1
-                                       if s['.name'] == self.sid then
-                                               dev = "%s.network%d" %{ s.device, num[s.device] }
-                                               return false
-                                       end
+       local num = { }
+       local netid
+       uci_r:foreach("wireless", "wifi-iface",
+               function(s)
+                       if s.device then
+                               num[s.device] = num[s.device] and num[s.device] + 1 or 1
+                               if s['.name'] == self.sid then
+                                       netid = "%s.network%d" %{ s.device, num[s.device] }
+                                       return false
                                end
-                       end)
-       end
+                       end
+               end)
+
+       local dev = uci_s:get("wireless", self.sid, "ifname") or netid
 
+       self.netid  = netid
        self.wdev   = dev
        self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
        self.iwdata = data or uci_s:get_all("wireless", self.sid) or
@@ -969,12 +1121,20 @@ function wifinet.network(self)
        return uci_s:get("wifinet", self.sid, "network")
 end
 
+function wifinet.id(self)
+       return self.netid
+end
+
 function wifinet.name(self)
        return self.sid
 end
 
 function wifinet.ifname(self)
-       return self.iwinfo.ifname or self.wdev
+       local ifname = self.iwinfo.ifname
+       if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
+               ifname = self.wdev
+       end
+       return ifname
 end
 
 function wifinet.get_device(self)
@@ -990,7 +1150,7 @@ end
 function wifinet.active_mode(self)
        local m = _stror(self.iwinfo.mode, self.iwdata.mode) or "ap"
 
-       if     m == "ap"      then m = "AP"
+       if     m == "ap"      then m = "Master"
        elseif m == "sta"     then m = "Client"
        elseif m == "adhoc"   then m = "Ad-Hoc"
        elseif m == "mesh"    then m = "Mesh"
@@ -1048,6 +1208,14 @@ function wifinet.noise(self)
        return self.iwinfo.noise or 0
 end
 
+function wifinet.country(self)
+       return self.iwinfo.country or "00"
+end
+
+function wifinet.txpower(self)
+       return self.iwinfo.txpower or 0
+end
+
 function wifinet.signal_level(self, s, n)
        if self:active_bssid() ~= "00:00:00:00:00:00" then
                local signal = s or self:signal()
@@ -1092,8 +1260,7 @@ function wifinet.get_i18n(self)
 end
 
 function wifinet.adminlink(self)
-       return dsp.build_url("admin", "network", "wireless",
-               self.iwdata.device, self.wdev)
+       return dsp.build_url("admin", "network", "wireless", self.netid)
 end
 
 function wifinet.get_network(self)