Merge pull request #3063 from TDT-AG/pr/20190908-luci-app-statistics
[oweals/luci.git] / modules / luci-base / luasrc / model / network.lua
index 78df72da2453d07e8416f32c99d6b6a8b7f49a1f..a36a23f321b25375cbb496c35133e6d65815b3d3 100644 (file)
@@ -622,6 +622,12 @@ function del_network(self, n)
                                        _uci:delete("wireless", s['.name'], "network")
                                end
                        end)
+
+               local ok, fw = pcall(require, "luci.model.firewall")
+               if ok then
+                       fw.init()
+                       fw:del_network(n)
+               end
        end
        return r
 end
@@ -813,6 +819,7 @@ function del_wifinet(self, net)
 end
 
 function get_status_by_route(self, addr, mask)
+       local route_statuses = { }
        local _, object
        for _, object in ipairs(utl.ubus()) do
                local net = object:match("^network%.interface%.(.+)")
@@ -822,12 +829,14 @@ function get_status_by_route(self, addr, mask)
                                local rt
                                for _, rt in ipairs(s.route) do
                                        if not rt.table and rt.target == addr and rt.mask == mask then
-                                               return net, s
+                                               route_statuses[net] = s
                                        end
                                end
                        end
                end
        end
+
+       return route_statuses
 end
 
 function get_status_by_address(self, addr)
@@ -852,28 +861,40 @@ function get_status_by_address(self, addr)
                                        end
                                end
                        end
+                       if s and s['ipv6-prefix-assignment'] then
+                               local a
+                               for _, a in ipairs(s['ipv6-prefix-assignment']) do
+                                       if a and a['local-address'] and a['local-address'].address == addr then
+                                               return net, s
+                                       end
+                               end
+                       end
                end
        end
 end
 
-function get_wannet(self)
-       local net, stat = self:get_status_by_route("0.0.0.0", 0)
-       return net and network(net, stat.proto)
-end
+function get_wan_networks(self)
+       local k, v
+       local wan_nets = { }
+       local route_statuses = self:get_status_by_route("0.0.0.0", 0)
 
-function get_wandev(self)
-       local _, stat = self:get_status_by_route("0.0.0.0", 0)
-       return stat and interface(stat.l3_device or stat.device)
-end
+       for k, v in pairs(route_statuses) do
+               wan_nets[#wan_nets+1] = network(k, v.proto)
+       end
 
-function get_wan6net(self)
-       local net, stat = self:get_status_by_route("::", 0)
-       return net and network(net, stat.proto)
+       return wan_nets
 end
 
-function get_wan6dev(self)
-       local _, stat = self:get_status_by_route("::", 0)
-       return stat and interface(stat.l3_device or stat.device)
+function get_wan6_networks(self)
+       local k, v
+       local wan6_nets = { }
+       local route_statuses = self:get_status_by_route("::", 0)
+
+       for k, v in pairs(route_statuses) do
+               wan6_nets[#wan6_nets+1] = network(k, v.proto)
+       end
+
+       return wan6_nets
 end
 
 function get_switch_topologies(self)
@@ -1144,6 +1165,10 @@ function protocol.is_dynamic(self)
        return (self:_ubus("dynamic") == true)
 end
 
+function protocol.is_auto(self)
+       return (self:_get("auto") ~= "0")
+end
+
 function protocol.is_alias(self)
        local ifn, parent = nil, nil
 
@@ -1348,7 +1373,9 @@ function interface.ip6addrs(self)
 end
 
 function interface.type(self)
-       if self.wif or _wifi_iface(self.ifname) then
+       if self.ifname and self.ifname:byte(1) == 64 then
+               return "alias"
+       elseif self.wif or _wifi_iface(self.ifname) then
                return "wifi"
        elseif _bridge[self.ifname] then
                return "bridge"
@@ -1385,7 +1412,9 @@ end
 
 function interface.get_type_i18n(self)
        local x = self:type()
-       if x == "wifi" then
+       if x == "alias" then
+               return lng.translate("Alias Interface")
+       elseif x == "wifi" then
                return lng.translate("Wireless Adapter")
        elseif x == "bridge" then
                return lng.translate("Bridge")
@@ -1438,7 +1467,11 @@ function interface.bridge_stp(self)
 end
 
 function interface.is_up(self)
-       return self:_ubus("up") or false
+       local up = self:_ubus("up")
+       if up == nil then
+               up = (self:type() == "alias")
+       end
+       return up or false
 end
 
 function interface.is_bridge(self)