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