summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-mwan3/luasrc/model/cbi/mwan/interface.lua
blob: c8c122ad48a795439848192fd2d3755423501f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
-- ------ extra functions ------ --

function interfaceCheck() -- find issues with too many interfaces, reliability and metric
	uci.cursor():foreach("mwan3", "interface",
		function (section)
			local interfaceName = section[".name"]
			interfaceNumber = interfaceNumber+1 -- count number of mwan interfaces configured
			-- create list of metrics for none and duplicate checking
			local metricValue = ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName .. ".metric"))
			if metricValue == "" then
				errorFound = 1
				errorNoMetricList = errorNoMetricList .. interfaceName .. " "
			else
				metricList = metricList .. interfaceName .. " " .. metricValue .. "\n"
			end
			-- check if any interfaces have a higher reliability requirement than tracking IPs configured
			local trackingNumber = tonumber(ut.trim(sys.exec("echo $(uci -p /var/state get mwan3." .. interfaceName .. ".track_ip) | wc -w")))
			if trackingNumber > 0 then
				local reliabilityNumber = tonumber(ut.trim(sys.exec("uci -p /var/state get mwan3." .. interfaceName .. ".reliability")))
				if reliabilityNumber and reliabilityNumber > trackingNumber then
					errorFound = 1
					errorReliabilityList = errorReliabilityList .. interfaceName .. " "
				end
			end
			-- check if any interfaces are not properly configured in /etc/config/network or have no default route in main routing table
			if ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName)) == "interface" then
				local interfaceDevice = ut.trim(sys.exec("uci -p /var/state get network." .. interfaceName .. ".ifname"))
				if interfaceDevice == "uci: Entry not found" or interfaceDevice == "" then
					errorFound = 1
					errorNetConfigList = errorNetConfigList .. interfaceName .. " "
					errorRouteList = errorRouteList .. interfaceName .. " "
				else
					local routeCheck = ut.trim(sys.exec("route -n | awk '{if ($8 == \"" .. interfaceDevice .. "\" && $1 == \"0.0.0.0\" && $3 == \"0.0.0.0\") print $1}'"))
					if routeCheck == "" then
						errorFound = 1
						errorRouteList = errorRouteList .. interfaceName .. " "
					end
				end
			else
				errorFound = 1
				errorNetConfigList = errorNetConfigList .. interfaceName .. " "
				errorRouteList = errorRouteList .. interfaceName .. " "
			end
		end
	)
	-- check if any interfaces have duplicate metrics
	local metricDuplicateNumbers = sys.exec("echo '" .. metricList .. "' | awk '{print $2}' | uniq -d")
	if metricDuplicateNumbers ~= "" then
		errorFound = 1
		local metricDuplicates = ""
		for line in metricDuplicateNumbers:gmatch("[^\r\n]+") do
			metricDuplicates = sys.exec("echo '" .. metricList .. "' | grep '" .. line .. "' | awk '{print $1}'")
			errorDuplicateMetricList = errorDuplicateMetricList .. metricDuplicates
		end
		errorDuplicateMetricList = sys.exec("echo '" .. errorDuplicateMetricList .. "' | tr '\n' ' '")
	end
end

function interfaceWarnings() -- display status and warning messages at the top of the page
	local warnings = ""
	if interfaceNumber <= 250 then
		warnings = "<strong>" .. translatef("There are currently %d of 250 supported interfaces configured", interfaceNumber) .. "</strong>"
	else
		warnings = "<font color=\"ff0000\"><strong>" .. translatef("WARNING: %d interfaces are configured exceeding the maximum of 250!", interfaceNumber) .. "</strong></font>"
	end
	if errorReliabilityList ~= " " then
		warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: some interfaces have a higher reliability requirement than there are tracking IP addresses!") .. "</strong></font>"
	end
	if errorRouteList ~= " " then
		warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: some interfaces have no default route in the main routing table!") .. "</strong></font>"
	end
	if errorNetConfigList ~= " " then
		warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: some interfaces are configured incorrectly or not at all in /etc/config/network!") .. "</strong></font>"
	end
	if errorNoMetricList ~= " " then
		warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: some interfaces have no metric configured in /etc/config/network!") .. "</strong></font>"
	end
	if errorDuplicateMetricList ~= " " then
		warnings = warnings .. "<br /><br /><font color=\"ff0000\"><strong>" .. translate("WARNING: some interfaces have duplicate metrics configured in /etc/config/network!") .. "</strong></font>"
	end
	return warnings
end

-- ------ interface configuration ------ --

dsp = require "luci.dispatcher"
sys = require "luci.sys"
ut = require "luci.util"

interfaceNumber = 0
metricList = ""
errorFound = 0
errorDuplicateMetricList = " "
errorNetConfigList = " "
errorNoMetricList = " "
errorReliabilityList = " "
errorRouteList = " "
interfaceCheck()


m5 = Map("mwan3", translate("MWAN Interface Configuration"),
	interfaceWarnings())
	m5:append(Template("mwan/config_css"))


mwan_interface = m5:section(TypedSection, "interface", translate("Interfaces"),
	translate("MWAN supports up to 250 physical and/or logical interfaces<br />" ..
	"MWAN requires that all interfaces have a unique metric configured in /etc/config/network<br />" ..
	"Names must match the interface name found in /etc/config/network (see advanced tab)<br />" ..
	"Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
	"Interfaces may not share the same name as configured members, policies or rules"))
	mwan_interface.addremove = true
	mwan_interface.dynamic = false
	mwan_interface.sectionhead = translate("Interface")
	mwan_interface.sortable = false
	mwan_interface.template = "cbi/tblsection"
	mwan_interface.extedit = dsp.build_url("admin", "network", "mwan", "configuration", "interface", "%s")
	function mwan_interface.create(self, section)
		TypedSection.create(self, section)
		m5.uci:save("mwan3")
		luci.http.redirect(dsp.build_url("admin", "network", "mwan", "configuration", "interface", section))
	end


enabled = mwan_interface:option(DummyValue, "enabled", translate("Enabled"))
	enabled.rawhtml = true
	function enabled.cfgvalue(self, s)
		if self.map:get(s, "enabled") == "1" then
			return "Yes"
		else
			return "No"
		end
	end

track_ip = mwan_interface:option(DummyValue, "track_ip", translate("Tracking IP"))
	track_ip.rawhtml = true
	function track_ip.cfgvalue(self, s)
		tracked = self.map:get(s, "track_ip")
		if tracked then
			local ipList = ""
			for k,v in pairs(tracked) do
				ipList = ipList .. v .. "<br />"
			end
			return ipList
		else
			return "&#8212;"
		end
	end

track_method = mwan_interface:option(DummyValue, "track_method", translate("Tracking method"))
	track_method.rawhtml = true
	function track_method.cfgvalue(self, s)
		if tracked then
			return self.map:get(s, "track_method") or "&#8212;"
		else
			return "&#8212;"
		end
	end

reliability = mwan_interface:option(DummyValue, "reliability", translate("Tracking reliability"))
	reliability.rawhtml = true
	function reliability.cfgvalue(self, s)
		if tracked then
			return self.map:get(s, "reliability") or "&#8212;"
		else
			return "&#8212;"
		end
	end

count = mwan_interface:option(DummyValue, "count", translate("Ping count"))
	count.rawhtml = true
	function count.cfgvalue(self, s)
		if tracked then
			return self.map:get(s, "count") or "&#8212;"
		else
			return "&#8212;"
		end
	end

timeout = mwan_interface:option(DummyValue, "timeout", translate("Ping timeout"))
	timeout.rawhtml = true
	function timeout.cfgvalue(self, s)
		if tracked then
			local timeoutValue = self.map:get(s, "timeout")
			if timeoutValue then
				return timeoutValue .. "s"
			else
				return "&#8212;"
			end
		else
			return "&#8212;"
		end
	end

interval = mwan_interface:option(DummyValue, "interval", translate("Ping interval"))
	interval.rawhtml = true
	function interval.cfgvalue(self, s)
		if tracked then
			local intervalValue = self.map:get(s, "interval")
			if intervalValue then
				return intervalValue .. "s"
			else
				return "&#8212;"
			end
		else
			return "&#8212;"
		end
	end

down = mwan_interface:option(DummyValue, "down", translate("Interface down"))
	down.rawhtml = true
	function down.cfgvalue(self, s)
		if tracked then
			return self.map:get(s, "down") or "&#8212;"
		else
			return "&#8212;"
		end
	end

up = mwan_interface:option(DummyValue, "up", translate("Interface up"))
	up.rawhtml = true
	function up.cfgvalue(self, s)
		if tracked then
			return self.map:get(s, "up") or "&#8212;"
		else
			return "&#8212;"
		end
	end

metric = mwan_interface:option(DummyValue, "metric", translate("Metric"))
	metric.rawhtml = true
	function metric.cfgvalue(self, s)
		local metricValue = sys.exec("uci -p /var/state get network." .. s .. ".metric")
		if metricValue ~= "" then
			return metricValue
		else
			return "&#8212;"
		end
	end

errors = mwan_interface:option(DummyValue, "errors", translate("Errors"))
	errors.rawhtml = true
	function errors.cfgvalue(self, s)
		if errorFound == 1 then
			local mouseOver, lineBreak = "", ""
			if string.find(errorReliabilityList, " " .. s .. " ") then
				mouseOver = "Higher reliability requirement than there are tracking IP addresses"
				lineBreak = "&#10;&#10;"
			end
			if string.find(errorRouteList, " " .. s .. " ") then
				mouseOver = mouseOver .. lineBreak .. "No default route in the main routing table"
				lineBreak = "&#10;&#10;"
			end
			if string.find(errorNetConfigList, " " .. s .. " ") then
				mouseOver = mouseOver .. lineBreak .. "Configured incorrectly or not at all in /etc/config/network"
				lineBreak = "&#10;&#10;"
			end
			if string.find(errorNoMetricList, " " .. s .. " ") then
				mouseOver = mouseOver .. lineBreak .. "No metric configured in /etc/config/network"
				lineBreak = "&#10;&#10;"
			end
			if string.find(errorDuplicateMetricList, " " .. s .. " ") then
				mouseOver = mouseOver .. lineBreak .. "Duplicate metric configured in /etc/config/network"
			end
			if mouseOver == "" then
				return ""
			else
				return "<span title=\"" .. mouseOver .. "\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
			end
		else
			return ""
		end
	end


return m5