libs/core: more fixes in network model
[project/luci.git] / libs / core / luasrc / model / network.lua
index 128786d..5e7d7d6 100644 (file)
@@ -30,7 +30,7 @@ local uci = require "luci.model.uci"
 module "luci.model.network"
 
 
 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)
 
 function _list_del(c, s, o, r)
        local val = uci_r:get(c, s, o)
@@ -174,6 +174,7 @@ function init(cursor)
        ifs = { }
        brs = { }
        sws = { }
        ifs = { }
        brs = { }
        sws = { }
+       tns = { }
 
        -- read interface information
        local n, i
 
        -- read interface information
        local n, i
@@ -181,7 +182,11 @@ function init(cursor)
                local name = i.name:match("[^:]+")
                local prnt = name:match("^([^%.]+)%.")
 
                local name = i.name:match("[^:]+")
                local prnt = name:match("^([^%.]+)%.")
 
-               if _iface_virtual(name) or 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,
                        ifs[name] = ifs[name] or {
                                idx      = i.ifindex or n,
                                name     = name,
@@ -394,6 +399,24 @@ function get_interfaces(self)
                end
        end
 
                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
        for iface in utl.kspairs(nfs) do
                ifaces[#ifaces+1] = nfs[iface]
        end
@@ -656,6 +679,10 @@ function network.is_virtual(self)
        )
 end
 
        )
 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
 function network.is_empty(self)
        if self:is_virtual() then
                return false
@@ -679,7 +706,7 @@ function network.is_empty(self)
 end
 
 function network.add_interface(self, ifname)
 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
                if type(ifname) ~= "string" then
                        ifname = ifname:name()
                else
@@ -705,7 +732,7 @@ function network.add_interface(self, ifname)
 end
 
 function network.del_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
                if utl.instanceof(ifname, interface) then
                        ifname = ifname:name()
                else
@@ -721,18 +748,44 @@ function network.del_interface(self, ifname)
        end
 end
 
        end
 end
 
-function network.get_interfaces(self)
-       local ifaces = { }
-
-       local ifn
+function network.get_interface(self)
        if self:is_virtual() then
        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
        else
+               local ifn = nil
+               local num = { }
+               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.ifname or "%s.network%d" %{ s.device, num[s.device] }
+                                               return false
+                                       end
+                               end
+                       end)
+               return ifn and interface(ifn, self)
+       end
+end
+
+function network.get_interfaces(self)
+       if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
+               local ifaces = { }
+
+               local ifn
                local nfs = { }
                for ifn in utl.imatch(self:get("ifname")) do
                local nfs = { }
                for ifn in utl.imatch(self:get("ifname")) do
-                       ifn = ifn:match("[^:]+")
-                       nfs[ifn] = interface(ifn)
+                       ifn = ifn:match("^[^:/]+")
+                       nfs[ifn] = interface(ifn, self)
                end
 
                for ifn in utl.kspairs(nfs) do
                end
 
                for ifn in utl.kspairs(nfs) do
@@ -747,7 +800,7 @@ function network.get_interfaces(self)
                                        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] }
                                        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)
+                                               wfs[ifn] = interface(ifn, self)
                                        end
                                end
                        end)
                                        end
                                end
                        end)
@@ -755,9 +808,9 @@ function network.get_interfaces(self)
                for ifn in utl.kspairs(wfs) do
                        ifaces[#ifaces+1] = wfs[ifn]
                end
                for ifn in utl.kspairs(wfs) do
                        ifaces[#ifaces+1] = wfs[ifn]
                end
-       end
 
 
-       return ifaces
+               return ifaces
+       end
 end
 
 function network.contains_interface(self, ifname)
 end
 
 function network.contains_interface(self, ifname)
@@ -767,11 +820,12 @@ function network.contains_interface(self, ifname)
                ifname = ifname:match("[^%s:]+")
        end
 
                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
        else
+               local ifn
                for ifn in utl.imatch(self:get("ifname")) do
                        ifn = ifn:match("[^:]+")
                        if ifn == ifname then
                for ifn in utl.imatch(self:get("ifname")) do
                        ifn = ifn:match("[^:]+")
                        if ifn == ifname then
@@ -794,12 +848,13 @@ end
 
 
 interface = utl.class()
 
 
 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
 
        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)
 end
 
 function interface.name(self)
@@ -807,7 +862,7 @@ function interface.name(self)
 end
 
 function interface.mac(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)
 end
 
 function interface.ipaddrs(self)
@@ -823,7 +878,11 @@ function interface.type(self)
                return "wifi"
        elseif brs[self.ifname] then
                return "bridge"
                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"
                return "switch"
        else
                return "ethernet"
@@ -861,6 +920,10 @@ function interface.get_type_i18n(self)
                return i18n.translate("Bridge")
        elseif x == "switch" then
                return i18n.translate("Ethernet Switch")
                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
        else
                return i18n.translate("Ethernet Adapter")
        end
@@ -936,8 +999,10 @@ function interface.rx_packets(self)
 end
 
 function interface.get_network(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
        end
 
        if not self.network then
@@ -1121,6 +1186,10 @@ function wifinet.network(self)
        return uci_s:get("wifinet", self.sid, "network")
 end
 
        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.name(self)
        return self.sid
 end