applications/olsr: Validate input for lqmult, #654
[project/luci.git] / applications / luci-olsr / luasrc / model / cbi / olsr / olsrd.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 require("luci.tools.webadmin")
17 local fs  = require "nixio.fs"
18 local util = require "luci.util"
19 local ip = require "luci.ip"
20
21 local has_ipip  = fs.glob("/etc/modules.d/[0-9]*-ipip")()
22
23 m = Map("olsrd", translate("OLSR Daemon"),
24         translate("The OLSR daemon is an implementation of the Optimized Link State Routing protocol. "..
25         "As such it allows mesh routing for any network equipment. "..
26         "It runs on any wifi card that supports ad-hoc mode and of course on any ethernet device. "..
27         "Visit <a href='http://www.olsr.org'>olsrd.org</a> for help and documentation."))
28
29 function m.on_parse()
30         local has_defaults = false
31
32         m.uci:foreach("olsrd", "InterfaceDefaults",
33                 function(s)
34                         has_defaults = true
35                         return false
36                 end)
37
38         if not has_defaults then
39                 m.uci:section("olsrd", "InterfaceDefaults")
40         end
41 end
42
43 function write_float(self, section, value)
44     local n = tonumber(value)
45     if n ~= nil then
46         return Value.write(self, section, "%.1f" % n) 
47     end
48 end
49
50 s = m:section(TypedSection, "olsrd", translate("General settings"))
51 s.anonymous = true
52
53 s:tab("general",  translate("General Settings"))
54 s:tab("lquality", translate("Link Quality Settings"))
55 s:tab("smartgw", translate("SmartGW"), not has_ipip and translate("Warning: kmod-ipip is not installed. Without kmod-ipip SmartGateway will not work, please install it."))
56 s:tab("advanced", translate("Advanced Settings"))
57
58 ipv = s:taboption("general", ListValue, "IpVersion", translate("Internet protocol"),
59         translate("IP-version to use. If 6and4 is selected then one olsrd instance is started for each protocol."))
60 ipv:value("4", "IPv4")
61 ipv:value("6", "IPv6")
62 ipv:value("6and4", "6and4")
63
64
65 poll = s:taboption("advanced", Value, "Pollrate", translate("Pollrate"),
66         translate("Polling rate for OLSR sockets in seconds. Default is 0.05."))
67 poll.optional = true
68 poll.datatype = "ufloat"
69 poll.placeholder = "0.05"
70
71 nicc = s:taboption("advanced", Value, "NicChgsPollInt", translate("Nic changes poll interval"),
72         translate("Interval to poll network interfaces for configuration changes (in seconds). Default is \"2.5\"."))
73 nicc.optional = true
74 nicc.datatype = "ufloat"
75 nicc.placeholder = "2.5"
76
77 tos = s:taboption("advanced", Value, "TosValue", translate("TOS value"),
78         translate("Type of service value for the IP header of control traffic. Default is \"16\"."))
79 tos.optional = true
80 tos.datatype = "uinteger"
81 tos.placeholder = "16"
82
83 fib = s:taboption("general", ListValue, "FIBMetric", translate("FIB metric"),
84         translate ("FIBMetric controls the metric value of the host-routes OLSRd sets. "..
85         "\"flat\" means that the metric value is always 2. This is the preferred value "..
86         "because it helps the linux kernel routing to clean up older routes. "..
87         "\"correct\" uses the hopcount as the metric value. "..
88         "\"approx\" use the hopcount as the metric value too, but does only update the hopcount if the nexthop changes too. "..
89         "Default is \"flat\"."))
90 fib:value("flat")
91 fib:value("correct")
92 fib:value("approx")
93
94 lql = s:taboption("lquality", ListValue, "LinkQualityLevel", translate("LQ level"),
95         translate("Link quality level switch between hopcount and cost-based (mostly ETX) routing.<br />"..
96         "<b>0</b> = do not use link quality<br />"..
97         "<b>2</b> = use link quality for MPR selection and routing<br />"..
98         "Default is \"2\""))
99 lql:value("2")
100 lql:value("0")
101
102 lqage = s:taboption("lquality", Value, "LinkQualityAging", translate("LQ aging"),
103         translate("Link quality aging factor (only for lq level 2). Tuning parameter for etx_float and etx_fpm, smaller values "..
104         "mean slower changes of ETX value. (allowed values are between 0.01 and 1.0)"))
105 lqage.optional = true
106 lqage:depends("LinkQualityLevel", "2")
107
108 lqa = s:taboption("lquality", ListValue, "LinkQualityAlgorithm", translate("LQ algorithm"),
109         translate("Link quality algorithm (only for lq level 2).<br />"..
110         "<b>etx_float</b>: floating point ETX with exponential aging<br />"..
111         "<b>etx_fpm</b>  : same as etx_float, but with integer arithmetic<br />"..
112         "<b>etx_ff</b>   : ETX freifunk, an etx variant which use all OLSR traffic (instead of only hellos) for ETX calculation<br />"..
113         "<b>etx_ffeth</b>: incompatible variant of etx_ff that allows ethernet links with ETX 0.1.<br />"..
114         "Defaults to \"etx_ff\""))
115 lqa.optional = true
116 lqa:value("etx_ff")
117 lqa:value("etx_fpm")
118 lqa:value("etx_float")
119 lqa:value("etx_ffeth")
120 lqa:depends("LinkQualityLevel", "2")
121 lqa.optional = true
122
123 lqfish = s:taboption("lquality", Flag, "LinkQualityFishEye", translate("LQ fisheye"),
124         translate("Fisheye mechanism for TCs (checked means on). Default is \"on\""))
125 lqfish.default = "1"
126 lqfish.optional = true
127
128 hyst = s:taboption("lquality", Flag, "UseHysteresis", translate("Use hysteresis"),
129         translate("Hysteresis for link sensing (only for hopcount metric). Hysteresis adds more robustness to the link sensing "..
130         "but delays neighbor registration. Defaults is \"yes\""))
131 hyst.default = "yes"
132 hyst.enabled = "yes"
133 hyst.disabled = "no"
134 hyst:depends("LinkQualityLevel", "0")
135 hyst.optional = true
136 hyst.rmempty = true
137
138 port = s:taboption("general", Value, "OlsrPort", translate("Port"),
139         translate("The port OLSR uses. This should usually stay at the IANA assigned port 698. It can have a value between 1 and 65535."))
140 port.optional = true
141 port.default = "698"
142 port.rmempty = true
143
144 mainip = s:taboption("general", Value, "MainIp", translate("Main IP"),
145         translate("Sets the main IP (originator ip) of the router. This IP will NEVER change during the uptime of olsrd. "..
146         "Default is 0.0.0.0, which triggers usage of the IP of the first interface."))
147 mainip.optional = true
148 mainip.rmempty = true
149 mainip.datatype = "ipaddr"
150 mainip.placeholder = "0.0.0.0"
151
152 sgw = s:taboption("smartgw", Flag, "SmartGateway", translate("Enable"), translate("Enable SmartGateway. If it is disabled, then " ..
153         "all other SmartGateway parameters are ignored. Default is \"no\"."))
154 sgw.default="no"
155 sgw.enabled="yes"
156 sgw.disabled="no"
157 sgw.rmempty = true
158
159 sgwnat = s:taboption("smartgw", Flag, "SmartGatewayAllowNAT", translate("Allow gateways with NAT"), translate("Allow the selection of an outgoing ipv4 gateway with NAT"))
160 sgwnat:depends("SmartGateway", "yes")
161 sgwnat.default="yes"
162 sgwnat.enabled="yes"
163 sgwnat.disabled="no"
164 sgwnat.optional = true
165 sgwnat.rmempty = true
166
167 sgwuplink = s:taboption("smartgw", ListValue, "SmartGatewayUplink", translate("Announce uplink"), translate("Which kind of uplink is exported to the other mesh nodes. " ..
168         "An uplink is detected by looking for a local HNA of 0.0.0.0/0, ::ffff:0:0/96 or 2000::/3. Default setting is \"both\"."))
169 sgwuplink:value("none")
170 sgwuplink:value("ipv4")
171 sgwuplink:value("ipv6")
172 sgwuplink:value("both")
173 sgwuplink:depends("SmartGateway", "yes")
174 sgwuplink.default="both"
175 sgwuplink.optional = true
176 sgwuplink.rmempty = true
177
178 sgwulnat = s:taboption("smartgw", Flag, "SmartGatewayUplinkNAT", translate("Uplink uses NAT"), translate("If this Node uses NAT for connections to the internet. " ..
179         "Default is \"yes\"."))
180 sgwulnat:depends("SmartGatewayUplink", "ipv4")
181 sgwulnat:depends("SmartGatewayUplink", "both")
182 sgwulnat.default="yes"
183 sgwulnat.enabled="yes"
184 sgwulnat.disabled="no"
185 sgwnat.optional = true
186 sgwnat.rmempty = true
187
188 sgwspeed = s:taboption("smartgw", Value, "SmartGatewaySpeed", translate("Speed of the uplink"), translate("Specifies the speed of "..
189         "the uplink in kilobits/s. First parameter is upstream, second parameter is downstream. Default is \"128 1024\"."))
190 sgwspeed:depends("SmartGatewayUplink", "ipv4")
191 sgwspeed:depends("SmartGatewayUplink", "ipv6")
192 sgwspeed:depends("SmartGatewayUplink", "both")
193 sgwspeed.optional = true
194 sgwspeed.rmempty = true
195
196 sgwprefix = s:taboption("smartgw", Value, "SmartGatewayPrefix", translate("IPv6-Prefix of the uplink"), translate("This can be used " ..
197         "to signal the external IPv6 prefix of the uplink to the clients. This might allow a client to change it's local IPv6 address to " ..
198         "use the IPv6 gateway without any kind of address translation. The maximum prefix length is 64 bits. " ..
199         "Default is \"::/0\" (no prefix)."))
200 sgwprefix:depends("SmartGatewayUplink", "ipv6")
201 sgwprefix:depends("SmartGatewayUplink", "both")
202 sgwprefix.optional = true
203 sgwprefix.rmempty = true
204
205 willingness = s:taboption("advanced", ListValue, "Willingness", translate("Willingness"),
206                 translate("The fixed willingness to use. If not set willingness will be calculated dynamically based on battery/power status. Default is \"3\"."))
207 for i=0,7 do
208         willingness:value(i)
209 end
210 willingness.optional = true
211 willingness.default = "3"
212
213 natthr = s:taboption("advanced", Value, "NatThreshold", translate("NAT threshold"),
214         translate("If the route to the current gateway is to be changed, the ETX value of this gateway is "..
215         "multiplied with this value before it is compared to the new one. "..
216         "The parameter can be a value between 0.1 and 1.0, but should be close to 1.0 if changed.<br />"..
217         "<b>WARNING:</b> This parameter should not be used together with the etx_ffeth metric!<br />"..
218         "Defaults to \"1.0\"."))
219 for i=1,0.1,-0.1 do
220         natthr:value(i)
221 end
222 natthr:depends("LinkQualityAlgorithm", "etx_ff")
223 natthr:depends("LinkQualityAlgorithm", "etx_float")
224 natthr:depends("LinkQualityAlgorithm", "etx_fpm")
225 natthr.default = "1.0"
226 natthr.optional = true
227 natthr.write = write_float
228
229
230 i = m:section(TypedSection, "InterfaceDefaults", translate("Interfaces Defaults"))
231 i.anonymous = true
232 i.addremove = false
233
234 i:tab("general", translate("General Settings"))
235 i:tab("addrs",   translate("IP Addresses"))
236 i:tab("timing",  translate("Timing and Validity"))
237
238 mode = i:taboption("general", ListValue, "Mode", translate("Mode"),
239         translate("Interface Mode is used to prevent unnecessary packet forwarding on switched ethernet interfaces. "..
240         "valid Modes are \"mesh\" and \"ether\". Default is \"mesh\"."))
241 mode:value("mesh")
242 mode:value("ether")
243 mode.optional = true
244 mode.rmempty = true
245
246
247 weight = i:taboption("general", Value, "Weight", translate("Weight"),
248         translate("When multiple links exist between hosts the weight of interface is used to determine the link to use. "..
249         "Normally the weight is automatically calculated by olsrd based on the characteristics of the interface, "..
250         "but here you can specify a fixed value. Olsrd will choose links with the lowest value.<br />"..
251         "<b>Note:</b> Interface weight is used only when LinkQualityLevel is set to 0. "..
252         "For any other value of LinkQualityLevel, the interface ETX value is used instead."))
253 weight.optional = true
254 weight.datatype = "uinteger"
255 weight.placeholder = "0"
256
257 lqmult = i:taboption("general", DynamicList, "LinkQualityMult", translate("LinkQuality Multiplicator"),
258         translate("Multiply routes with the factor given here. Allowed values are between 0.01 and 1.0. "..
259         "It is only used when LQ-Level is greater than 0. Examples:<br />"..
260         "reduce LQ to 192.168.0.1 by half: 192.168.0.1 0.5<br />"..
261         "reduce LQ to all nodes on this interface by 20%: default 0.8"))
262 lqmult.optional = true
263 lqmult.rmempty = true
264 lqmult.cast = "table"
265 lqmult.placeholder = "default 1.0"
266
267 function lqmult.validate(self, value)
268         for _, v in pairs(value) do
269                 if v ~= "" then
270                         local val = util.split(v, " ")
271                         local host = val[1]
272                         local mult = val[2]
273                         if not host or not mult then
274                                 return nil, translate("LQMult requires two values (IP address or 'default' and multiplicator) seperated by space.")
275                         end
276                         if not (host == "default" or ip.IPv4(host) or ip.IPv6(host)) then
277                                 return nil, translate("Can only be a valid IPv4 or IPv6 address or 'default'")
278                         end
279                         if not tonumber(mult) or tonumber(mult) > 1 or tonumber(mult) < 0.01 then
280                                 return nil, translate("Invalid Value for LQMult-Value. Must be between 0.01 and 1.0.")
281                         end
282                         if not mult:match("[0-1]%.[0-9]+") then
283                                 return nil, translate("Invalid Value for LQMult-Value. You must use a decimal number between 0.01 and 1.0 here.")
284                         end
285                 end
286         end
287         return value
288 end
289
290 ip4b = i:taboption("addrs", Value, "Ip4Broadcast", translate("IPv4 broadcast"),
291         translate("IPv4 broadcast address for outgoing OLSR packets. One useful example would be 255.255.255.255. "..
292         "Default is \"0.0.0.0\", which triggers the usage of the interface broadcast IP."))
293 ip4b.optional = true
294 ip4b.datatype = "ip4addr"
295 ip4b.placeholder = "0.0.0.0"
296
297 ip6m = i:taboption("addrs", Value, "IPv6Multicast", translate("IPv6 multicast"),
298         translate("IPv6 multicast address. Default is \"FF02::6D\", the manet-router linklocal multicast."))
299 ip6m.optional = true
300 ip6m.datatype = "ip6addr"
301 ip6m.placeholder = "FF02::6D"
302
303 ip4s = i:taboption("addrs", Value, "IPv4Src", translate("IPv4 source"),
304         translate("IPv4 src address for outgoing OLSR packages. Default is \"0.0.0.0\", which triggers usage of the interface IP."))
305 ip4s.optional = true
306 ip4s.datatype = "ip4addr"
307 ip4s.placeholder = "0.0.0.0"
308
309 ip6s = i:taboption("addrs", Value, "IPv6Src", translate("IPv6 source"),
310         translate("IPv6 src prefix. OLSRd will choose one of the interface IPs which matches the prefix of this parameter. "..
311         "Default is \"0::/0\", which triggers the usage of a not-linklocal interface IP."))
312 ip6s.optional = true
313 ip6s.datatype = "ip6addr"
314 ip6s.placeholder = "0::/0"
315
316
317 hi = i:taboption("timing", Value, "HelloInterval", translate("Hello interval"))
318 hi.optional = true
319 hi.datatype = "ufloat"
320 hi.placeholder = "5.0"
321 hi.write = write_float
322
323 hv = i:taboption("timing", Value, "HelloValidityTime", translate("Hello validity time"))
324 hv.optional = true
325 hv.datatype = "ufloat"
326 hv.placeholder = "40.0"
327 hv.write = write_float
328
329 ti = i:taboption("timing", Value, "TcInterval", translate("TC interval"))
330 ti.optional = true
331 ti.datatype = "ufloat"
332 ti.placeholder = "2.0"
333 ti.write = write_float
334
335 tv = i:taboption("timing", Value, "TcValidityTime", translate("TC validity time"))
336 tv.optional = true
337 tv.datatype = "ufloat"
338 tv.placeholder = "256.0"
339 tv.write = write_float
340
341 mi = i:taboption("timing", Value, "MidInterval", translate("MID interval"))
342 mi.optional = true
343 mi.datatype = "ufloat"
344 mi.placeholder = "18.0"
345 mi.write = write_float
346
347 mv = i:taboption("timing", Value, "MidValidityTime", translate("MID validity time"))
348 mv.optional = true
349 mv.datatype = "ufloat"
350 mv.placeholder = "324.0"
351 mv.write = write_float
352
353 ai = i:taboption("timing", Value, "HnaInterval", translate("HNA interval"))
354 ai.optional = true
355 ai.datatype = "ufloat"
356 ai.placeholder = "18.0"
357 ai.write = write_float
358
359 av = i:taboption("timing", Value, "HnaValidityTime", translate("HNA validity time"))
360 av.optional = true
361 av.datatype = "ufloat"
362 av.placeholder = "108.0"
363 av.write = write_float
364
365
366 ifs = m:section(TypedSection, "Interface", translate("Interfaces"))
367 ifs.addremove = true
368 ifs.anonymous = true
369 ifs.extedit   = luci.dispatcher.build_url("admin/services/olsrd/iface/%s")
370 ifs.template  = "cbi/tblsection"
371
372 function ifs.create(...)
373         local sid = TypedSection.create(...)
374         luci.http.redirect(ifs.extedit % sid)
375 end
376
377 ign = ifs:option(Flag, "ignore", translate("Enable"))
378 ign.enabled  = "0"
379 ign.disabled = "1"
380 ign.rmempty = false
381 function ign.cfgvalue(self, section)
382         return Flag.cfgvalue(self, section) or "0"
383 end
384
385 network = ifs:option(DummyValue, "interface", translate("Network"))
386 network.template = "cbi/network_netinfo"
387
388 mode = ifs:option(DummyValue, "Mode", translate("Mode"))
389 function mode.cfgvalue(...)
390         return Value.cfgvalue(...) or m.uci:get_first("olsrd", "InterfaceDefaults", "Mode", "mesh")
391 end
392
393 hello = ifs:option(DummyValue, "_hello", translate("Hello"))
394 function hello.cfgvalue(self, section)
395         local i = tonumber(m.uci:get("olsrd", section, "HelloInterval"))     or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "HelloInterval", 5))
396         local v = tonumber(m.uci:get("olsrd", section, "HelloValidityTime")) or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "HelloValidityTime", 40))
397         return "%.01fs / %.01fs" %{ i, v }
398 end
399
400 tc = ifs:option(DummyValue, "_tc", translate("TC"))
401 function tc.cfgvalue(self, section)
402         local i = tonumber(m.uci:get("olsrd", section, "TcInterval"))     or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "TcInterval", 2))
403         local v = tonumber(m.uci:get("olsrd", section, "TcValidityTime")) or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "TcValidityTime", 256))
404         return "%.01fs / %.01fs" %{ i, v }
405 end
406
407 mid = ifs:option(DummyValue, "_mid", translate("MID"))
408 function mid.cfgvalue(self, section)
409         local i = tonumber(m.uci:get("olsrd", section, "MidInterval"))     or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "MidInterval", 18))
410         local v = tonumber(m.uci:get("olsrd", section, "MidValidityTime")) or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "MidValidityTime", 324))
411         return "%.01fs / %.01fs" %{ i, v }
412 end
413
414 hna = ifs:option(DummyValue, "_hna", translate("HNA"))
415 function hna.cfgvalue(self, section)
416         local i = tonumber(m.uci:get("olsrd", section, "HnaInterval"))     or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "HnaInterval", 18))
417         local v = tonumber(m.uci:get("olsrd", section, "HnaValidityTime")) or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "HnaValidityTime", 108))
418         return "%.01fs / %.01fs" %{ i, v }
419 end
420
421 return m