libs/core: add "tunnel" interface type to network model, various optimizations
[project/luci.git] / libs / core / luasrc / model / network.lua
index 07bae9a8ae54fc94903d8770a03a129f39ee82dc..26344c2cc1aae84526fe0b350f9d089f96501164 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"
@@ -30,7 +30,7 @@ local uci = require "luci.model.uci"
 module "luci.model.network"
 
 
-local ifs, brs, sws, uci_r, uci_s
+local ifs, brs, sws, tns, uci_r, uci_s
 
 function _list_del(c, s, o, r)
        local val = uci_r:get(c, s, o)
@@ -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
 
@@ -167,6 +174,7 @@ function init(cursor)
        ifs = { }
        brs = { }
        sws = { }
+       tns = { }
 
        -- read interface information
        local n, i
@@ -174,7 +182,11 @@ function init(cursor)
                local name = i.name:match("[^:]+")
                local prnt = name:match("^([^%.]+)%.")
 
-               if not _iface_ignore(name) then
+               if _iface_virtual(name) then
+                       tns[name] = true
+               end
+
+               if tns[name] or not _iface_ignore(name) then
                        ifs[name] = ifs[name] or {
                                idx      = i.ifindex or n,
                                name     = name,
@@ -387,6 +399,24 @@ function get_interfaces(self)
                end
        end
 
+       -- find vlan interfaces
+       uci_r:foreach("network", "switch_vlan",
+               function(s)
+                       local base = s.device or "-"
+                       if not base:match("^eth%d") then
+                               base = "eth0"
+                       end
+
+                       local vid = tonumber(s.vid or s.vlan)
+                       if vid ~= nil and vid >= 0 and vid <= 4095 then
+                               local iface = "%s.%d" %{ base, vid }
+                               if not seen[iface] then
+                                       seen[iface] = true
+                                       nfs[iface] = interface(iface)
+                               end
+                       end
+               end)
+
        for iface in utl.kspairs(nfs) do
                ifaces[#ifaces+1] = nfs[iface]
        end
@@ -478,7 +508,7 @@ 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 then
+       if ip or list then
                if list then
                        local l = { }
                        for ip in utl.imatch(ip) do
@@ -505,6 +535,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
@@ -547,7 +579,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)
@@ -605,11 +639,10 @@ function network.ip6addr(self)
        local ip6 = self:_ip("ip6addr", 6)
        if not ip6 then
                local ifc = ifs[self:ifname()]
-               local llr = ipc.IPv6("fe80::/10")
                if ifc and ifc.ip6addrs then
                        local a
                        for _, a in ipairs(ifc.ip6addrs) do
-                               if not llr:contains(a) then
+                               if not a:is6linklocal() then
                                        ip6 = a:string()
                                        break
                                end
@@ -642,10 +675,14 @@ 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
 
+function network.is_floating(self)
+       return (self:is_virtual() and self:proto() ~= "pppoe")
+end
+
 function network.is_empty(self)
        if self:is_virtual() then
                return false
@@ -669,7 +706,7 @@ function network.is_empty(self)
 end
 
 function network.add_interface(self, ifname)
-       if not self:is_virtual() then
+       if not self:is_floating() then
                if type(ifname) ~= "string" then
                        ifname = ifname:name()
                else
@@ -695,7 +732,7 @@ function network.add_interface(self, ifname)
 end
 
 function network.del_interface(self, ifname)
-       if not self:is_virtual() then
+       if not self:is_floating() then
                if utl.instanceof(ifname, interface) then
                        ifname = ifname:name()
                else
@@ -711,40 +748,69 @@ function network.del_interface(self, ifname)
        end
 end
 
-function network.get_interfaces(self)
-       local ifaces = { }
-
-       local ifn
+function network.get_interface(self)
        if self:is_virtual() then
-               ifn = self:proto() .. "-" .. self.sid
-               ifaces = { interface(ifn) }
+               tns[self:proto() .. "-" .. self.sid] = true
+               return interface(self:proto() .. "-" .. self.sid, self)
+       elseif self:is_bridge() then
+               brs["br-" .. self.sid] = true
+               return interface("br-" .. self.sid, self)
        else
-               local nfs = { }
-               for ifn in utl.imatch(self:get("ifname")) do
-                       ifn = ifn:match("[^:]+")
-                       nfs[ifn] = interface(ifn)
-               end
-
-               for ifn in utl.kspairs(nfs) do
-                       ifaces[#ifaces+1] = nfs[ifn]
-               end
-
+               local ifn = nil
                local num = { }
-               local wfs = { }
-               uci_r:foreach("wireless", "wifi-iface",
+               for ifn in utl.imatch(uci_s:get("network", self.sid, "ifname")) do
+                       ifn = ifn:match("^[^:/]+")
+                       return ifn and interface(ifn, self)
+               end
+               ifn = nil
+               uci_s: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.network == self.sid then
-                                               ifn = "%s.network%d" %{ s.device, num[s.device] }
-                                               wfs[ifn] = interface(ifn)
+                                               ifn = s.ifname or "%s.network%d" %{ s.device, num[s.device] }
+                                               return false
                                        end
                                end
                        end)
+               return ifn and interface(ifn, self)
+       end
+end
 
-               for ifn in utl.kspairs(wfs) do
-                       ifaces[#ifaces+1] = wfs[ifn]
-               end
+function network.get_interfaces(self)
+       local ifaces = { }
+
+       local ifn
+       local nfs = { }
+       for ifn in utl.imatch(self:get("ifname")) do
+               ifn = ifn:match("^[^:/]+")
+               nfs[ifn] = interface(ifn, self)
+       end
+
+       for ifn in utl.kspairs(nfs) do
+               ifaces[#ifaces+1] = nfs[ifn]
+       end
+
+       local num = { }
+       local wfs = { }
+       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.network == self.sid then
+                                       ifn = "%s.network%d" %{ s.device, num[s.device] }
+                                       wfs[ifn] = interface(ifn, self)
+                               end
+                       end
+               end)
+
+       for ifn in utl.kspairs(wfs) do
+               ifaces[#ifaces+1] = wfs[ifn]
+
+               -- only bridges may cover more than one interface
+               --if not self:is_bridge() then
+               --      break
+               --end
        end
 
        return ifaces
@@ -757,11 +823,12 @@ function network.contains_interface(self, ifname)
                ifname = ifname:match("[^%s:]+")
        end
 
-       local ifn
-       if self:is_virtual() then
-               ifn = self:proto() .. "-" .. self.sid
-               return ifname == ifn
+       if self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
+               return true
+       elseif self:is_bridge() and "br-" .. self.sid == ifname then
+               return true
        else
+               local ifn
                for ifn in utl.imatch(self:get("ifname")) do
                        ifn = ifn:match("[^:]+")
                        if ifn == ifname then
@@ -784,12 +851,13 @@ end
 
 
 interface = utl.class()
-function interface.__init__(self, ifname)
+function interface.__init__(self, ifname, network)
        local wif = _wifi_lookup(ifname)
        if wif then self.wif = wifinet(wif) end
 
-       self.ifname = self.ifname or ifname
-       self.dev    = ifs[self.ifname]
+       self.ifname  = self.ifname or ifname
+       self.dev     = ifs[self.ifname]
+       self.network = network
 end
 
 function interface.name(self)
@@ -797,7 +865,7 @@ function interface.name(self)
 end
 
 function interface.mac(self)
-       return self.dev and self.dev.macaddr or "00:00:00:00:00:00"
+       return (self.dev and self.dev.macaddr or "00:00:00:00:00:00"):upper()
 end
 
 function interface.ipaddrs(self)
@@ -813,7 +881,11 @@ function interface.type(self)
                return "wifi"
        elseif brs[self.ifname] then
                return "bridge"
-       elseif sws[self.ifname] or self.ifname:match("%.") then
+       elseif tns[self.ifname] then
+               return "tunnel"
+       elseif self.ifname:match("%.") then
+               return "vlan"
+       elseif sws[self.ifname] then
                return "switch"
        else
                return "ethernet"
@@ -851,6 +923,10 @@ function interface.get_type_i18n(self)
                return i18n.translate("Bridge")
        elseif x == "switch" then
                return i18n.translate("Ethernet Switch")
+       elseif x == "vlan" then
+               return i18n.translate("VLAN Interface")
+       elseif x == "tunnel" then
+               return i18n.translate("Tunnel Interface")
        else
                return i18n.translate("Ethernet Adapter")
        end
@@ -926,8 +1002,10 @@ function interface.rx_packets(self)
 end
 
 function interface.get_network(self)
-       if self.dev and self.dev.network then
-               self.network = _M:get_network(self.dev.network)
+       if not self.network then
+               if self.dev and self.dev.network then
+                       self.network = _M:get_network(self.dev.network)
+               end
        end
 
        if not self.network then
@@ -952,7 +1030,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)
@@ -967,6 +1046,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
 
@@ -1083,6 +1189,10 @@ 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
@@ -1166,6 +1276,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()