applications/olsr: Validate input for lqmult, #654
[project/luci.git] / applications / luci-olsr / luasrc / model / cbi / olsr / olsrd.lua
index 9db56b5..8492bf6 100644 (file)
@@ -14,6 +14,11 @@ $Id$
 ]]--
 
 require("luci.tools.webadmin")
+local fs  = require "nixio.fs"
+local util = require "luci.util"
+local ip = require "luci.ip"
+
+local has_ipip  = fs.glob("/etc/modules.d/[0-9]*-ipip")()
 
 m = Map("olsrd", translate("OLSR Daemon"),
         translate("The OLSR daemon is an implementation of the Optimized Link State Routing protocol. "..
@@ -35,15 +40,21 @@ function m.on_parse()
        end
 end
 
+function write_float(self, section, value)
+    local n = tonumber(value)
+    if n ~= nil then
+        return Value.write(self, section, "%.1f" % n) 
+    end
+end
+
 s = m:section(TypedSection, "olsrd", translate("General settings"))
---s.dynamic = true
 s.anonymous = true
 
 s:tab("general",  translate("General Settings"))
 s:tab("lquality", translate("Link Quality Settings"))
+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."))
 s:tab("advanced", translate("Advanced Settings"))
 
-
 ipv = s:taboption("general", ListValue, "IpVersion", translate("Internet protocol"),
        translate("IP-version to use. If 6and4 is selected then one olsrd instance is started for each protocol."))
 ipv:value("4", "IPv4")
@@ -97,7 +108,7 @@ lqage:depends("LinkQualityLevel", "2")
 lqa = s:taboption("lquality", ListValue, "LinkQualityAlgorithm", translate("LQ algorithm"),
        translate("Link quality algorithm (only for lq level 2).<br />"..
        "<b>etx_float</b>: floating point ETX with exponential aging<br />"..
-       "<b>etx_fpm</b>  : same as ext_float, but with integer arithmetic<br />"..
+       "<b>etx_fpm</b>  : same as etx_float, but with integer arithmetic<br />"..
        "<b>etx_ff</b>   : ETX freifunk, an etx variant which use all OLSR traffic (instead of only hellos) for ETX calculation<br />"..
        "<b>etx_ffeth</b>: incompatible variant of etx_ff that allows ethernet links with ETX 0.1.<br />"..
        "Defaults to \"etx_ff\""))
@@ -122,6 +133,7 @@ hyst.enabled = "yes"
 hyst.disabled = "no"
 hyst:depends("LinkQualityLevel", "0")
 hyst.optional = true
+hyst.rmempty = true
 
 port = s:taboption("general", Value, "OlsrPort", translate("Port"),
         translate("The port OLSR uses. This should usually stay at the IANA assigned port 698. It can have a value between 1 and 65535."))
@@ -137,6 +149,59 @@ mainip.rmempty = true
 mainip.datatype = "ipaddr"
 mainip.placeholder = "0.0.0.0"
 
+sgw = s:taboption("smartgw", Flag, "SmartGateway", translate("Enable"), translate("Enable SmartGateway. If it is disabled, then " ..
+       "all other SmartGateway parameters are ignored. Default is \"no\"."))
+sgw.default="no"
+sgw.enabled="yes"
+sgw.disabled="no"
+sgw.rmempty = true
+
+sgwnat = s:taboption("smartgw", Flag, "SmartGatewayAllowNAT", translate("Allow gateways with NAT"), translate("Allow the selection of an outgoing ipv4 gateway with NAT"))
+sgwnat:depends("SmartGateway", "yes")
+sgwnat.default="yes"
+sgwnat.enabled="yes"
+sgwnat.disabled="no"
+sgwnat.optional = true
+sgwnat.rmempty = true
+
+sgwuplink = s:taboption("smartgw", ListValue, "SmartGatewayUplink", translate("Announce uplink"), translate("Which kind of uplink is exported to the other mesh nodes. " ..
+       "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\"."))
+sgwuplink:value("none")
+sgwuplink:value("ipv4")
+sgwuplink:value("ipv6")
+sgwuplink:value("both")
+sgwuplink:depends("SmartGateway", "yes")
+sgwuplink.default="both"
+sgwuplink.optional = true
+sgwuplink.rmempty = true
+
+sgwulnat = s:taboption("smartgw", Flag, "SmartGatewayUplinkNAT", translate("Uplink uses NAT"), translate("If this Node uses NAT for connections to the internet. " ..
+       "Default is \"yes\"."))
+sgwulnat:depends("SmartGatewayUplink", "ipv4")
+sgwulnat:depends("SmartGatewayUplink", "both")
+sgwulnat.default="yes"
+sgwulnat.enabled="yes"
+sgwulnat.disabled="no"
+sgwnat.optional = true
+sgwnat.rmempty = true
+
+sgwspeed = s:taboption("smartgw", Value, "SmartGatewaySpeed", translate("Speed of the uplink"), translate("Specifies the speed of "..
+       "the uplink in kilobits/s. First parameter is upstream, second parameter is downstream. Default is \"128 1024\"."))
+sgwspeed:depends("SmartGatewayUplink", "ipv4")
+sgwspeed:depends("SmartGatewayUplink", "ipv6")
+sgwspeed:depends("SmartGatewayUplink", "both")
+sgwspeed.optional = true
+sgwspeed.rmempty = true
+
+sgwprefix = s:taboption("smartgw", Value, "SmartGatewayPrefix", translate("IPv6-Prefix of the uplink"), translate("This can be used " ..
+       "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 " ..
+       "use the IPv6 gateway without any kind of address translation. The maximum prefix length is 64 bits. " ..
+       "Default is \"::/0\" (no prefix)."))
+sgwprefix:depends("SmartGatewayUplink", "ipv6")
+sgwprefix:depends("SmartGatewayUplink", "both")
+sgwprefix.optional = true
+sgwprefix.rmempty = true
+
 willingness = s:taboption("advanced", ListValue, "Willingness", translate("Willingness"),
                translate("The fixed willingness to use. If not set willingness will be calculated dynamically based on battery/power status. Default is \"3\"."))
 for i=0,7 do
@@ -157,8 +222,9 @@ end
 natthr:depends("LinkQualityAlgorithm", "etx_ff")
 natthr:depends("LinkQualityAlgorithm", "etx_float")
 natthr:depends("LinkQualityAlgorithm", "etx_fpm")
-natthr.default = 1
+natthr.default = "1.0"
 natthr.optional = true
+natthr.write = write_float
 
 
 i = m:section(TypedSection, "InterfaceDefaults", translate("Interfaces Defaults"))
@@ -189,7 +255,7 @@ weight.datatype = "uinteger"
 weight.placeholder = "0"
 
 lqmult = i:taboption("general", DynamicList, "LinkQualityMult", translate("LinkQuality Multiplicator"),
-       translate("Multiply routes with the factor given here. Allowed values are between 0.01 and 1. "..
+       translate("Multiply routes with the factor given here. Allowed values are between 0.01 and 1.0. "..
        "It is only used when LQ-Level is greater than 0. Examples:<br />"..
        "reduce LQ to 192.168.0.1 by half: 192.168.0.1 0.5<br />"..
        "reduce LQ to all nodes on this interface by 20%: default 0.8"))
@@ -198,6 +264,28 @@ lqmult.rmempty = true
 lqmult.cast = "table"
 lqmult.placeholder = "default 1.0"
 
+function lqmult.validate(self, value)
+       for _, v in pairs(value) do
+               if v ~= "" then
+                       local val = util.split(v, " ")
+                       local host = val[1]
+                       local mult = val[2]
+                       if not host or not mult then
+                               return nil, translate("LQMult requires two values (IP address or 'default' and multiplicator) seperated by space.")
+                       end
+                       if not (host == "default" or ip.IPv4(host) or ip.IPv6(host)) then
+                               return nil, translate("Can only be a valid IPv4 or IPv6 address or 'default'")
+                       end
+                       if not tonumber(mult) or tonumber(mult) > 1 or tonumber(mult) < 0.01 then
+                               return nil, translate("Invalid Value for LQMult-Value. Must be between 0.01 and 1.0.")
+                       end
+                       if not mult:match("[0-1]%.[0-9]+") then
+                               return nil, translate("Invalid Value for LQMult-Value. You must use a decimal number between 0.01 and 1.0 here.")
+                       end
+               end
+       end
+        return value
+end
 
 ip4b = i:taboption("addrs", Value, "Ip4Broadcast", translate("IPv4 broadcast"),
        translate("IPv4 broadcast address for outgoing OLSR packets. One useful example would be 255.255.255.255. "..
@@ -230,41 +318,49 @@ hi = i:taboption("timing", Value, "HelloInterval", translate("Hello interval"))
 hi.optional = true
 hi.datatype = "ufloat"
 hi.placeholder = "5.0"
+hi.write = write_float
 
 hv = i:taboption("timing", Value, "HelloValidityTime", translate("Hello validity time"))
 hv.optional = true
 hv.datatype = "ufloat"
 hv.placeholder = "40.0"
+hv.write = write_float
 
 ti = i:taboption("timing", Value, "TcInterval", translate("TC interval"))
 ti.optional = true
 ti.datatype = "ufloat"
 ti.placeholder = "2.0"
+ti.write = write_float
 
 tv = i:taboption("timing", Value, "TcValidityTime", translate("TC validity time"))
 tv.optional = true
 tv.datatype = "ufloat"
 tv.placeholder = "256.0"
+tv.write = write_float
 
 mi = i:taboption("timing", Value, "MidInterval", translate("MID interval"))
 mi.optional = true
 mi.datatype = "ufloat"
 mi.placeholder = "18.0"
+mi.write = write_float
 
 mv = i:taboption("timing", Value, "MidValidityTime", translate("MID validity time"))
 mv.optional = true
 mv.datatype = "ufloat"
 mv.placeholder = "324.0"
+mv.write = write_float
 
 ai = i:taboption("timing", Value, "HnaInterval", translate("HNA interval"))
 ai.optional = true
 ai.datatype = "ufloat"
 ai.placeholder = "18.0"
+ai.write = write_float
 
 av = i:taboption("timing", Value, "HnaValidityTime", translate("HNA validity time"))
 av.optional = true
 av.datatype = "ufloat"
 av.placeholder = "108.0"
+av.write = write_float
 
 
 ifs = m:section(TypedSection, "Interface", translate("Interfaces"))
@@ -273,6 +369,11 @@ ifs.anonymous = true
 ifs.extedit   = luci.dispatcher.build_url("admin/services/olsrd/iface/%s")
 ifs.template  = "cbi/tblsection"
 
+function ifs.create(...)
+       local sid = TypedSection.create(...)
+       luci.http.redirect(ifs.extedit % sid)
+end
+
 ign = ifs:option(Flag, "ignore", translate("Enable"))
 ign.enabled  = "0"
 ign.disabled = "1"
@@ -286,34 +387,34 @@ network.template = "cbi/network_netinfo"
 
 mode = ifs:option(DummyValue, "Mode", translate("Mode"))
 function mode.cfgvalue(...)
-       return Value.cfgvalue(...) or "mesh"
+       return Value.cfgvalue(...) or m.uci:get_first("olsrd", "InterfaceDefaults", "Mode", "mesh")
 end
 
 hello = ifs:option(DummyValue, "_hello", translate("Hello"))
 function hello.cfgvalue(self, section)
-       local i = tonumber(m.uci:get("olsrd", section, "HelloInterval"))     or 5
-       local v = tonumber(m.uci:get("olsrd", section, "HelloValidityTime")) or 40
+       local i = tonumber(m.uci:get("olsrd", section, "HelloInterval"))     or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "HelloInterval", 5))
+       local v = tonumber(m.uci:get("olsrd", section, "HelloValidityTime")) or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "HelloValidityTime", 40))
        return "%.01fs / %.01fs" %{ i, v }
 end
 
 tc = ifs:option(DummyValue, "_tc", translate("TC"))
 function tc.cfgvalue(self, section)
-       local i = tonumber(m.uci:get("olsrd", section, "TcInterval"))     or 2
-       local v = tonumber(m.uci:get("olsrd", section, "TcValidityTime")) or 256
+       local i = tonumber(m.uci:get("olsrd", section, "TcInterval"))     or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "TcInterval", 2))
+       local v = tonumber(m.uci:get("olsrd", section, "TcValidityTime")) or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "TcValidityTime", 256))
        return "%.01fs / %.01fs" %{ i, v }
 end
 
 mid = ifs:option(DummyValue, "_mid", translate("MID"))
 function mid.cfgvalue(self, section)
-       local i = tonumber(m.uci:get("olsrd", section, "MidInterval"))     or 18
-       local v = tonumber(m.uci:get("olsrd", section, "MidValidityTime")) or 324
+       local i = tonumber(m.uci:get("olsrd", section, "MidInterval"))     or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "MidInterval", 18))
+       local v = tonumber(m.uci:get("olsrd", section, "MidValidityTime")) or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "MidValidityTime", 324))
        return "%.01fs / %.01fs" %{ i, v }
 end
 
 hna = ifs:option(DummyValue, "_hna", translate("HNA"))
 function hna.cfgvalue(self, section)
-       local i = tonumber(m.uci:get("olsrd", section, "HnaInterval"))     or 18
-       local v = tonumber(m.uci:get("olsrd", section, "HnaValidityTime")) or 108
+       local i = tonumber(m.uci:get("olsrd", section, "HnaInterval"))     or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "HnaInterval", 18))
+       local v = tonumber(m.uci:get("olsrd", section, "HnaValidityTime")) or tonumber(m.uci:get_first("olsrd", "InterfaceDefaults", "HnaValidityTime", 108))
        return "%.01fs / %.01fs" %{ i, v }
 end