libs/core: allow resolving of virtual interfaces via get_interface(), should fix...
[project/luci.git] / libs / core / luasrc / model / network.lua
index 68ad04f..0d3a759 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"
@@ -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("^6to4-%w") or x:match("^3g-%w") or x:match("^ppp-%w") or
-               x:match("^pppoe-%w") or x:match("^pppoa-%w") or x == "sit0" 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,
@@ -475,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
@@ -487,6 +512,8 @@ 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
@@ -529,7 +556,9 @@ function network.device(self)
                dev = (type(dev) == "table") and dev[1] or dev
        end
 
-       return dev
+       for dev in utl.imatch(dev) do
+               return dev
+       end
 end
 
 function network.proto(self)
@@ -553,6 +582,68 @@ function network.uptime(self)
        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
@@ -561,7 +652,7 @@ function network.is_virtual(self)
        local p = self:proto()
        return (
                p == "3g" or p == "6in4" or p == "6to4" or p == "ppp" or
-               p == "pppoe" or p == "pppoa"
+               p == "pppoe" or p == "pppoa" or p == "relay"
        )
 end
 
@@ -871,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)
@@ -886,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