Merge pull request #1096 from dibdot/app-adblock
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / interfaceconfig.lua
1 -- ------ extra functions ------ --
2
3 function interfaceCheck()
4         metricValue = ut.trim(sys.exec("uci -p /var/state get network." .. arg[1] .. ".metric"))
5         if metricValue == "" then -- no metric
6                 errorNoMetric = 1
7         else -- if metric exists create list of interface metrics to compare against for duplicates
8                 uci.cursor():foreach("mwan3", "interface",
9                         function (section)
10                                 local metricValue = ut.trim(sys.exec("uci -p /var/state get network." .. section[".name"] .. ".metric"))
11                                 metricList = metricList .. section[".name"] .. " " .. metricValue .. "\n"
12                         end
13                 )
14                 -- compare metric against list
15                 local metricDuplicateNumbers, metricDuplicates = sys.exec("echo '" .. metricList .. "' | awk '{print $2}' | uniq -d"), ""
16                 for line in metricDuplicateNumbers:gmatch("[^\r\n]+") do
17                         metricDuplicates = sys.exec("echo '" .. metricList .. "' | grep '" .. line .. "' | awk '{print $1}'")
18                         errorDuplicateMetricList = errorDuplicateMetricList .. metricDuplicates
19                 end
20                 if sys.exec("echo '" .. errorDuplicateMetricList .. "' | grep -w " .. arg[1]) ~= "" then
21                         errorDuplicateMetric = 1
22                 end
23         end
24         -- check if this interface has a higher reliability requirement than track IPs configured
25         local trackingNumber = tonumber(ut.trim(sys.exec("echo $(uci -p /var/state get mwan3." .. arg[1] .. ".track_ip) | wc -w")))
26         if trackingNumber > 0 then
27                 local reliabilityNumber = tonumber(ut.trim(sys.exec("uci -p /var/state get mwan3." .. arg[1] .. ".reliability")))
28                 if reliabilityNumber and reliabilityNumber > trackingNumber then
29                         errorReliability = 1
30                 end
31         end
32         -- check if any interfaces are not properly configured in /etc/config/network or have no default route in main routing table
33         if ut.trim(sys.exec("uci -p /var/state get network." .. arg[1])) == "interface" then
34                 local interfaceDevice = ut.trim(sys.exec("uci -p /var/state get network." .. arg[1] .. ".ifname"))
35                 if interfaceDevice == "uci: Entry not found" or interfaceDevice == "" then
36                         errorNetConfig = 1
37                         errorRoute = 1
38                 else
39                         local routeCheck = ut.trim(sys.exec("route -n | awk '{if ($8 == \"" .. interfaceDevice .. "\" && $1 == \"0.0.0.0\" && $3 == \"0.0.0.0\") print $1}'"))
40                         if routeCheck == "" then
41                                 errorRoute = 1
42                         end
43                 end
44         else
45                 errorNetConfig = 1
46                 errorRoute = 1
47         end
48 end
49
50 function interfaceWarnings() -- display warning messages at the top of the page
51         local warns, lineBreak = "", ""
52         if errorReliability == 1 then
53                 warns = "<font color=\"ff0000\"><strong>WARNING: this interface has a higher reliability requirement than there are tracking IP addresses!</strong></font>"
54                 lineBreak = "<br /><br />"
55         end
56         if errorRoute == 1 then
57                 warns = warns .. lineBreak .. "<font color=\"ff0000\"><strong>WARNING: this interface has no default route in the main routing table!</strong></font>"
58                 lineBreak = "<br /><br />"
59         end
60         if errorNetConfig == 1 then
61                 warns = warns .. lineBreak .. "<font color=\"ff0000\"><strong>WARNING: this interface is configured incorrectly or not at all in /etc/config/network!</strong></font>"
62                 lineBreak = "<br /><br />"
63         end
64         if errorNoMetric == 1 then
65                 warns = warns .. lineBreak .. "<font color=\"ff0000\"><strong>WARNING: this interface has no metric configured in /etc/config/network!</strong></font>"
66         elseif errorDuplicateMetric == 1 then
67                 warns = warns .. lineBreak .. "<font color=\"ff0000\"><strong>WARNING: this and other interfaces have duplicate metrics configured in /etc/config/network!</strong></font>"
68         end
69         return warns
70 end
71
72 -- ------ interface configuration ------ --
73
74 dsp = require "luci.dispatcher"
75 sys = require "luci.sys"
76 ut = require "luci.util"
77 arg[1] = arg[1] or ""
78
79 metricValue = ""
80 metricList = ""
81 errorDuplicateMetricList = ""
82 errorNoMetric = 0
83 errorDuplicateMetric = 0
84 errorRoute = 0
85 errorNetConfig = 0
86 errorReliability = 0
87 interfaceCheck()
88
89
90 m5 = Map("mwan3", translate("MWAN Interface Configuration - " .. arg[1]),
91         translate(interfaceWarnings()))
92         m5.redirect = dsp.build_url("admin", "network", "mwan", "configuration", "interface")
93
94
95 mwan_interface = m5:section(NamedSection, arg[1], "interface", "")
96         mwan_interface.addremove = false
97         mwan_interface.dynamic = false
98
99
100 enabled = mwan_interface:option(ListValue, "enabled", translate("Enabled"))
101         enabled.default = "1"
102         enabled:value("1", translate("Yes"))
103         enabled:value("0", translate("No"))
104
105 family = mwan_interface:option(ListValue, "family", translate("Internet Protocol"))
106         family.default = "ipv4"
107         family:value("ipv4", translate("IPv4"))
108         family:value("ipv6", translate("IPv6"))
109
110 track_ip = mwan_interface:option(DynamicList, "track_ip", translate("Tracking IP"),
111         translate("This IP address will be pinged to dermine if the link is up or down. Leave blank to assume interface is always online"))
112         track_ip.datatype = "ipaddr"
113
114 reliability = mwan_interface:option(Value, "reliability", translate("Tracking reliability"),
115         translate("Acceptable values: 1-100. This many Tracking IP addresses must respond for the link to be deemed up"))
116         reliability.datatype = "range(1, 100)"
117         reliability.default = "1"
118
119 count = mwan_interface:option(ListValue, "count", translate("Ping count"))
120         count.default = "1"
121         count:value("1")
122         count:value("2")
123         count:value("3")
124         count:value("4")
125         count:value("5")
126
127 size = mwan_interface:option(Value, "size", translate("Ping size"))
128         size.default = "56"
129         size:value("8")
130         size:value("24")
131         size:value("56")
132         size:value("120")
133         size:value("248")
134         size:value("504")
135         size:value("1016")
136         size:value("1472")
137         size:value("2040")
138         size.datatype = "range(1, 65507)"
139         size.rmempty = false
140         size.optional = false
141
142 timeout = mwan_interface:option(ListValue, "timeout", translate("Ping timeout"))
143         timeout.default = "2"
144         timeout:value("1", translate("1 second"))
145         timeout:value("2", translate("2 seconds"))
146         timeout:value("3", translate("3 seconds"))
147         timeout:value("4", translate("4 seconds"))
148         timeout:value("5", translate("5 seconds"))
149         timeout:value("6", translate("6 seconds"))
150         timeout:value("7", translate("7 seconds"))
151         timeout:value("8", translate("8 seconds"))
152         timeout:value("9", translate("9 seconds"))
153         timeout:value("10", translate("10 seconds"))
154
155 interval = mwan_interface:option(ListValue, "interval", translate("Ping interval"))
156         interval.default = "5"
157         interval:value("1", translate("1 second"))
158         interval:value("3", translate("3 seconds"))
159         interval:value("5", translate("5 seconds"))
160         interval:value("10", translate("10 seconds"))
161         interval:value("20", translate("20 seconds"))
162         interval:value("30", translate("30 seconds"))
163         interval:value("60", translate("1 minute"))
164         interval:value("300", translate("5 minutes"))
165         interval:value("600", translate("10 minutes"))
166         interval:value("900", translate("15 minutes"))
167         interval:value("1800", translate("30 minutes"))
168         interval:value("3600", translate("1 hour"))
169
170 down = mwan_interface:option(ListValue, "down", translate("Interface down"),
171         translate("Interface will be deemed down after this many failed ping tests"))
172         down.default = "3"
173         down:value("1")
174         down:value("2")
175         down:value("3")
176         down:value("4")
177         down:value("5")
178         down:value("6")
179         down:value("7")
180         down:value("8")
181         down:value("9")
182         down:value("10")
183
184 up = mwan_interface:option(ListValue, "up", translate("Interface up"),
185         translate("Downed interface will be deemed up after this many successful ping tests"))
186         up.default = "3"
187         up:value("1")
188         up:value("2")
189         up:value("3")
190         up:value("4")
191         up:value("5")
192         up:value("6")
193         up:value("7")
194         up:value("8")
195         up:value("9")
196         up:value("10")
197
198 flush = mwan_interface:option(ListValue, "flush_conntrack", translate("Flush conntrack table"),
199         translate("Flush global firewall conntrack table on interface events"))
200         flush.default = "never"
201         flush:value("ifup", translate("ifup"))
202         flush:value("ifdown", translate("ifdown"))
203         flush:value("never", translate("never"))
204         flush:value("always", translate("always"))
205
206 metric = mwan_interface:option(DummyValue, "metric", translate("Metric"),
207         translate("This displays the metric assigned to this interface in /etc/config/network"))
208         metric.rawhtml = true
209         function metric.cfgvalue(self, s)
210                 if errorNoMetric == 0 then
211                         return metricValue
212                 else
213                         return "&#8212;"
214                 end
215         end
216
217
218 return m5