libs/core: ignore sit0 in network model
[project/luci.git] / libs / core / luasrc / model / network.lua
index 858753f..28237e4 100644 (file)
@@ -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
 
@@ -154,15 +154,15 @@ 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("^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"
        )
 end
 
 
 function init(cursor)
-       uci_r = cursor or luci.model.uci.cursor()
-       uci_s = cursor:substate()
+       uci_r = cursor or uci_r or uci.cursor()
+       uci_s = uci_r:substate()
 
        ifs = { }
        brs = { }
@@ -275,26 +275,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 +290,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 +358,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 = { }
@@ -479,6 +481,7 @@ function network.ifname(self)
        elseif self:is_virtual() then
                return p .. "-" .. self.sid
        else
+               local num = { }
                local dev = self:_get("ifname") or
                        uci_r:get("network", self.sid, "ifname")
 
@@ -523,6 +526,15 @@ 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.is_bridge(self)
        return (self:type() == "bridge")
 end
@@ -530,7 +542,7 @@ end
 function network.is_virtual(self)
        local p = self:proto()
        return (
-               p == "3g" or p == "6in4" or p == "ppp" or
+               p == "3g" or p == "6in4" or p == "6to4" or p == "ppp" or
                p == "pppoe" or p == "pppoa"
        )
 end
@@ -609,7 +621,7 @@ function network.get_interfaces(self)
                ifaces = { interface(ifn) }
        else
                local nfs = { }
-               for ifn in self:_get("ifname"):gmatch("%S+") do
+               for ifn in utl.imatch(self:get("ifname")) do
                        ifn = ifn:match("[^:]+")
                        nfs[ifn] = interface(ifn)
                end
@@ -651,7 +663,7 @@ function network.contains_interface(self, ifname)
                ifn = self:proto() .. "-" .. self.sid
                return ifname == ifn
        else
-               for ifn in self:_get("ifname"):gmatch("%S+") do
+               for ifn in utl.imatch(self:get("ifname")) do
                        ifn = ifn:match("[^:]+")
                        if ifn == ifname then
                                return true
@@ -686,7 +698,7 @@ function interface.name(self)
 end
 
 function interface.mac(self)
-       return self.dev and self.dev or "00:00:00:00:00:00"
+       return self.dev and self.dev.macaddr or "00:00:00:00:00:00"
 end
 
 function interface.ipaddrs(self)
@@ -898,9 +910,9 @@ function wifidev.add_wifinet(self, options)
        options = options or { }
        options.device = self.sid
 
-       local wnet = uci_r:section("wifidev", "wifi-iface", nil, options)
+       local wnet = uci_r:section("wireless", "wifi-iface", nil, options)
        if wnet then
-               return wifinet(wnet)
+               return wifinet(wnet, options)
        end
 end
 
@@ -921,7 +933,7 @@ end
 
 
 wifinet = utl.class()
-function wifinet.__init__(self, net)
+function wifinet.__init__(self, net, data)
        self.sid = net
 
        local dev = uci_s:get("wireless", self.sid, "ifname")
@@ -940,8 +952,9 @@ function wifinet.__init__(self, net)
        end
 
        self.wdev   = dev
-       self.iwdata = uci_s:get_all("wireless", self.sid) or { }
        self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
+       self.iwdata = data or uci_s:get_all("wireless", self.sid) or
+               uci_r:get_all("wireless", self.sid) or { }
 end
 
 function wifinet.get(self, opt)
@@ -973,7 +986,11 @@ function wifinet.name(self)
 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)
@@ -1008,7 +1025,7 @@ function wifinet.active_ssid(self)
 end
 
 function wifinet.active_bssid(self)
-       return _stror(self.iwinfo.bssid, self.iwinfo.bssid) or "00:00:00:00:00:00"
+       return _stror(self.iwinfo.bssid, self.iwdata.bssid) or "00:00:00:00:00:00"
 end
 
 function wifinet.active_encryption(self)