From 9d36a7b5184f93680575fa02564c48463b23c17e Mon Sep 17 00:00:00 2001
From: Konstantin Kuzov <master.nosferatu@gmail.com>
Date: Fri, 31 Aug 2018 10:35:05 +0300
Subject: [PATCH] add support for new add dependency functionality

Additionally add ability to set multicast querier on bridge.
Also add ability to set checkup_interval for l2tp.

Signed-off-by: Konstantin Kuzov <master.nosferatu@gmail.com>
---
 modules/luci-base/luasrc/model/network.lua    |  4 +++
 .../model/cbi/admin_network/iface_add.lua     | 27 ++++++++++++++
 .../luasrc/model/cbi/admin_network/ifaces.lua | 36 +++++++++++++++++--
 .../model/cbi/admin_network/proto_l2tp.lua    |  4 +++
 .../model/cbi/admin_network/proto_pptp.lua    |  4 +++
 .../luasrc/model/network/proto_ppp.lua        |  4 +++
 6 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/modules/luci-base/luasrc/model/network.lua b/modules/luci-base/luasrc/model/network.lua
index cce559a..9a76b00 100644
--- a/modules/luci-base/luasrc/model/network.lua
+++ b/modules/luci-base/luasrc/model/network.lua
@@ -1158,6 +1158,10 @@ function protocol.is_alias(self)
 	return parent
 end
 
+function protocol.is_l3_tunnel(self)
+	return false
+end
+
 function protocol.is_empty(self)
 	if self:is_floating() then
 		return false
diff --git a/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/iface_add.lua b/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/iface_add.lua
index ca66e9f..6cee273 100644
--- a/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/iface_add.lua
+++ b/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/iface_add.lua
@@ -47,6 +47,16 @@ mifname.template  = "cbi/network_ifacelist"
 mifname.nobridges = true
 
 
+snoroute = m:field(Flag, "_noroute", translate("Disable automatic route assignment"),
+	translate("If checked, no route to server is configured"))
+
+
+sifroute = m:field(Value, "_ifroute", translate("Route through this interface"),
+	translate("If interface is unspecified then default route is used"))
+
+sifroute.widget = "radio"
+sifroute.template = "cbi/network_netlist"
+
 local _, p
 for _, p in ipairs(nw:get_protocols()) do
 	if p:is_installed() then
@@ -56,6 +66,10 @@ for _, p in ipairs(nw:get_protocols()) do
 			sifname:depends({ _bridge = "",  _netproto = p:proto()})
 			mifname:depends({ _bridge = "1", _netproto = p:proto()})
 		end
+		if p:is_l3_tunnel() then
+			snoroute:depends({ _bridge = "", _netproto = p:proto()})
+			sifroute:depends({ _bridge = "", _noroute = false,  _netproto = p:proto()})
+		end
 	end
 end
 
@@ -86,11 +100,24 @@ function newproto.write(self, section, value)
 		local net = nw:add_network(name, { proto = value, type = br })
 		if net then
 			local ifn
+			local noroute = (snoroute:formvalue(section) == "1")
 			for ifn in utl.imatch(
 				br and mifname:formvalue(section) or sifname:formvalue(section)
 			) do
 				net:add_interface(ifn)
 			end
+
+			net:set("route_disable", noroute)
+			for ifn in utl.imatch(
+				sifroute:formvalue(section)
+			) do
+				local val = sifroute:formvalue(section)
+				if val == "-" then
+					val = nil
+				end
+				net:set("route_interface", val)
+			end
+
 			nw:save("network")
 			nw:save("wireless")
 		end
diff --git a/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/ifaces.lua b/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/ifaces.lua
index 06bce01..977bdad 100644
--- a/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/ifaces.lua
+++ b/modules/luci-mod-admin-full/luasrc/model/cbi/admin_network/ifaces.lua
@@ -79,7 +79,6 @@ local function backup_ifnames(is_bridge)
 	end
 end
 
-
 -- redirect to overview page if network does not exist anymore (e.g. after a revert)
 if not net then
 	luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
@@ -149,7 +148,9 @@ if m:formvalue("cbid.network.%s._switch" % net:name()) then
 		for k, v in pairs(m:get(net:name())) do
 			if k:sub(1,1) ~= "." and
 			   k ~= "type" and
-			   k ~= "ifname"
+			   k ~= "ifname" and
+			   k ~= "route_interface" and
+			   k ~= "route_disable"
 			then
 				m:del(net:name(), k)
 			end
@@ -261,7 +262,7 @@ force_link.default = (net:proto() == "static") and force_link.enabled or force_l
 
 
 if not net:is_virtual() then
-	br = s:taboption("physical", Flag, "type", translate("Bridge interfaces"), translate("creates a bridge over specified interface(s)"))
+	br = s:taboption("physical", Flag, "type", translate("Bridge interfaces"), translate("Creates a bridge over specified interface(s)"))
 	br.enabled = "bridge"
 	br.rmempty = true
 	br:depends("proto", "static")
@@ -277,6 +278,11 @@ if not net:is_virtual() then
 		translate("Enables IGMP snooping on this bridge"))
 	igmp:depends("type", "bridge")
 	igmp.rmempty = true
+
+	mcquerier = s:taboption("physical", Flag, "multicast_querier", translate("Enable Multicast Querier"),
+		translate("Enables the Multicast Querier on this bridge"))
+	mcquerier:depends("type", "bridge")
+	mcquerier.rmempty = true
 end
 
 
@@ -337,6 +343,30 @@ if not net:is_floating() then
 	end
 end
 
+if net:is_l3_tunnel() then
+	ifnoroute = s:taboption("physical", Flag, "route_disable", translate("Disable automatic route assignment"),
+		translate("If checked, no route to server is configured"))
+	ifnoroute:depends("type", "")
+	ifnoroute.rmempty = false
+	ifroute = s:taboption("physical", Value, "route_interface", translate("Interface"))
+	ifroute.template = "cbi/network_netlist"
+	ifroute.widget = "radio"
+	ifroute.rmempty = true
+	ifroute.network = arg[1]
+	ifroute:depends("route_disable", false)
+
+	function ifroute.cfgvalue(self, s)
+		return net:get("route_interface")
+	end
+
+	function ifroute.write(self, s, val)
+		if val == "-" then
+			val = nil
+		end
+		net:set("route_interface", val)
+	end
+end
+
 
 if not net:is_virtual() then
 	ifname_multi = s:taboption("physical", Value, "ifname_multi", translate("Interface"))
diff --git a/protocols/luci-proto-ppp/luasrc/model/cbi/admin_network/proto_l2tp.lua b/protocols/luci-proto-ppp/luasrc/model/cbi/admin_network/proto_l2tp.lua
index 604f019..3dcd653 100644
--- a/protocols/luci-proto-ppp/luasrc/model/cbi/admin_network/proto_l2tp.lua
+++ b/protocols/luci-proto-ppp/luasrc/model/cbi/admin_network/proto_l2tp.lua
@@ -59,3 +59,7 @@ dns.cast     = "string"
 mtu = section:taboption("advanced", Value, "mtu", translate("Override MTU"))
 mtu.placeholder = "1500"
 mtu.datatype    = "max(9200)"
+
+cui = section:taboption("advanced", Value, "checkup_interval", translate("Autoreconnect Interval"))
+cui.placeholder = "30"
+cui.datatype    = "min(5)"
diff --git a/protocols/luci-proto-ppp/luasrc/model/cbi/admin_network/proto_pptp.lua b/protocols/luci-proto-ppp/luasrc/model/cbi/admin_network/proto_pptp.lua
index 6a828ef..2b000b1 100644
--- a/protocols/luci-proto-ppp/luasrc/model/cbi/admin_network/proto_pptp.lua
+++ b/protocols/luci-proto-ppp/luasrc/model/cbi/admin_network/proto_pptp.lua
@@ -104,3 +104,7 @@ demand.datatype    = "uinteger"
 mtu = section:taboption("advanced", Value, "mtu", translate("Override MTU"))
 mtu.placeholder = "1500"
 mtu.datatype    = "max(9200)"
+
+cui = section:taboption("advanced", Value, "checkup_interval", translate("Autoreconnect Interval"))
+cui.placeholder = "30"
+cui.datatype    = "min(5)"
diff --git a/protocols/luci-proto-ppp/luasrc/model/network/proto_ppp.lua b/protocols/luci-proto-ppp/luasrc/model/network/proto_ppp.lua
index 3afb7de..b8d7041 100644
--- a/protocols/luci-proto-ppp/luasrc/model/network/proto_ppp.lua
+++ b/protocols/luci-proto-ppp/luasrc/model/network/proto_ppp.lua
@@ -64,6 +64,10 @@ for _, p in ipairs({"ppp", "pptp", "pppoe", "pppoa", "l2tp", "pppossh"}) do
 		return (p ~= "pppoe")
 	end
 
+	function proto.is_l3_tunnel(self)
+		return (p == "l2tp" or p == "pptp")
+	end
+
 	function proto.is_virtual(self)
 		return true
 	end
-- 
2.17.0

