1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2012 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4 local dsp = require "luci.dispatcher"
5 local ft = require "luci.tools.firewall"
6 local nw = require "luci.model.network"
12 translate("Firewall - Traffic Rules"),
13 translate("This page allows you to change advanced properties of the \
14 traffic rule entry, such as matched source and destination \
17 m.redirect = dsp.build_url("admin/network/firewall/rules")
21 local rule_type = m.uci:get("firewall", arg[1])
22 if rule_type == "redirect" and m:get(arg[1], "target") ~= "SNAT" then
27 luci.http.redirect(m.redirect)
33 elseif rule_type == "redirect" then
35 local name = m:get(arg[1], "name") or m:get(arg[1], "_name")
36 if not name or #name == 0 then
37 name = translate("(Unnamed SNAT)")
39 name = "SNAT %s" % name
42 m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
44 s = m:section(NamedSection, arg[1], "redirect", "")
49 ft.opt_enabled(s, Button)
50 ft.opt_name(s, Value, translate("Name"))
53 o = s:option(Value, "proto",
54 translate("Protocol"),
55 translate("You may specify multiple by selecting \"-- custom --\" and \
56 then entering protocols separated by space."))
58 o:value("all", "All protocols")
59 o:value("tcp udp", "TCP+UDP")
62 o:value("icmp", "ICMP")
64 function o.cfgvalue(...)
65 local v = Value.cfgvalue(...)
66 if not v or v == "tcpudp" then
73 o = s:option(Value, "src", translate("Source zone"))
76 o.template = "cbi/firewall_zonelist"
79 o = s:option(Value, "src_ip", translate("Source IP address"))
81 o.datatype = "neg(ipmask4)"
82 o.placeholder = translate("any")
84 luci.sys.net.ipv4_hints(function(ip, name)
85 o:value(ip, "%s (%s)" %{ ip, name })
89 o = s:option(Value, "src_port",
90 translate("Source port"),
91 translate("Match incoming traffic originating from the given source \
92 port or port range on the client host."))
94 o.datatype = "neg(portrange)"
95 o.placeholder = translate("any")
97 o:depends("proto", "tcp")
98 o:depends("proto", "udp")
99 o:depends("proto", "tcp udp")
100 o:depends("proto", "tcpudp")
102 o = s:option(Value, "dest", translate("Destination zone"))
105 o.template = "cbi/firewall_zonelist"
108 o = s:option(Value, "dest_ip", translate("Destination IP address"))
109 o.datatype = "neg(ipmask4)"
111 luci.sys.net.ipv4_hints(function(ip, name)
112 o:value(ip, "%s (%s)" %{ ip, name })
116 o = s:option(Value, "dest_port",
117 translate("Destination port"),
118 translate("Match forwarded traffic to the given destination port or \
122 o.placeholder = translate("any")
123 o.datatype = "neg(portrange)"
125 o:depends("proto", "tcp")
126 o:depends("proto", "udp")
127 o:depends("proto", "tcp udp")
128 o:depends("proto", "tcpudp")
130 o = s:option(Value, "src_dip",
131 translate("SNAT IP address"),
132 translate("Rewrite matched traffic to the given address."))
134 o.datatype = "ip4addr"
136 for _, v in ipairs(nw:get_interfaces()) do
138 for _, a in ipairs(v:ipaddrs()) do
139 o:value(a:host():string(), '%s (%s)' %{
140 a:host():string(), v:shortname()
146 o = s:option(Value, "src_dport", translate("SNAT port"),
147 translate("Rewrite matched traffic to the given source port. May be \
148 left empty to only rewrite the IP address."))
149 o.datatype = "portrange"
151 o.placeholder = translate('Do not rewrite')
153 o:depends("proto", "tcp")
154 o:depends("proto", "udp")
155 o:depends("proto", "tcp udp")
156 o:depends("proto", "tcpudp")
158 s:option(Value, "extra",
159 translate("Extra arguments"),
160 translate("Passes additional arguments to iptables. Use with care!"))
167 local name = m:get(arg[1], "name") or m:get(arg[1], "_name")
168 if not name or #name == 0 then
169 name = translate("(Unnamed Rule)")
172 m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
175 s = m:section(NamedSection, arg[1], "rule", "")
179 ft.opt_enabled(s, Button)
180 ft.opt_name(s, Value, translate("Name"))
183 o = s:option(ListValue, "family", translate("Restrict to address family"))
185 o:value("", translate("IPv4 and IPv6"))
186 o:value("ipv4", translate("IPv4 only"))
187 o:value("ipv6", translate("IPv6 only"))
190 o = s:option(Value, "proto", translate("Protocol"))
191 o:value("all", translate("Any"))
192 o:value("tcp udp", "TCP+UDP")
193 o:value("tcp", "TCP")
194 o:value("udp", "UDP")
195 o:value("icmp", "ICMP")
197 function o.cfgvalue(...)
198 local v = Value.cfgvalue(...)
199 if not v or v == "tcpudp" then
206 o = s:option(DropDown, "icmp_type", translate("Match ICMP type"))
214 o:value("echo-reply")
215 o:value("destination-unreachable")
216 o:value("network-unreachable")
217 o:value("host-unreachable")
218 o:value("protocol-unreachable")
219 o:value("port-unreachable")
220 o:value("fragmentation-needed")
221 o:value("source-route-failed")
222 o:value("network-unknown")
223 o:value("host-unknown")
224 o:value("network-prohibited")
225 o:value("host-prohibited")
226 o:value("TOS-network-unreachable")
227 o:value("TOS-host-unreachable")
228 o:value("communication-prohibited")
229 o:value("host-precedence-violation")
230 o:value("precedence-cutoff")
231 o:value("source-quench")
233 o:value("network-redirect")
234 o:value("host-redirect")
235 o:value("TOS-network-redirect")
236 o:value("TOS-host-redirect")
237 o:value("echo-request")
238 o:value("router-advertisement")
239 o:value("router-solicitation")
240 o:value("time-exceeded")
241 o:value("ttl-zero-during-transit")
242 o:value("ttl-zero-during-reassembly")
243 o:value("parameter-problem")
244 o:value("ip-header-bad")
245 o:value("required-option-missing")
246 o:value("timestamp-request")
247 o:value("timestamp-reply")
248 o:value("address-mask-request")
249 o:value("address-mask-reply")
251 o:depends("proto", "icmp")
254 o = s:option(Value, "src", translate("Source zone"))
258 o.template = "cbi/firewall_zonelist"
261 o = s:option(Value, "src_mac", translate("Source MAC address"))
262 o.datatype = "list(macaddr)"
263 o.placeholder = translate("any")
265 luci.sys.net.mac_hints(function(mac, name)
266 o:value(mac, "%s (%s)" %{ mac, name })
270 o = s:option(Value, "src_ip", translate("Source address"))
271 o.datatype = "list(neg(ipmask))"
272 o.placeholder = translate("any")
274 luci.sys.net.ipv4_hints(function(ip, name)
275 o:value(ip, "%s (%s)" %{ ip, name })
279 o = s:option(Value, "src_port", translate("Source port"))
280 o.datatype = "list(neg(portrange))"
281 o.placeholder = translate("any")
283 o:depends("proto", "tcp")
284 o:depends("proto", "udp")
285 o:depends("proto", "tcp udp")
286 o:depends("proto", "tcpudp")
288 o = s:option(Value, "dest_local", translate("Output zone"))
291 o.template = "cbi/firewall_zonelist"
295 o = s:option(Value, "dest_remote", translate("Destination zone"))
299 o.template = "cbi/firewall_zonelist"
301 o:depends({["src"] = "", ["!reverse"] = true})
304 o = s:option(Value, "dest_ip", translate("Destination address"))
305 o.datatype = "list(neg(ipmask))"
306 o.placeholder = translate("any")
308 luci.sys.net.ipv4_hints(function(ip, name)
309 o:value(ip, "%s (%s)" %{ ip, name })
313 o = s:option(Value, "dest_port", translate("Destination port"))
314 o.datatype = "list(neg(portrange))"
315 o.placeholder = translate("any")
317 o:depends("proto", "tcp")
318 o:depends("proto", "udp")
319 o:depends("proto", "tcp udp")
320 o:depends("proto", "tcpudp")
322 o = s:option(ListValue, "target", translate("Action"))
324 o:value("DROP", translate("drop"))
325 o:value("ACCEPT", translate("accept"))
326 o:value("REJECT", translate("reject"))
327 o:value("NOTRACK", translate("don't track"))
330 s:option(Value, "extra",
331 translate("Extra arguments"),
332 translate("Passes additional arguments to iptables. Use with care!"))
335 o = s:option(DropDown, "weekdays", translate("Week Days"))
338 o:value("Sun", translate("Sunday"))
339 o:value("Mon", translate("Monday"))
340 o:value("Tue", translate("Tuesday"))
341 o:value("Wed", translate("Wednesday"))
342 o:value("Thu", translate("Thursday"))
343 o:value("Fri", translate("Friday"))
344 o:value("Sat", translate("Saturday"))
346 o = s:option(DropDown, "monthdays", translate("Month Days"))
350 o:value(translate(i))
353 o = s:option(Value, "start_time", translate("Start Time (hh:mm:ss)"))
354 o.datatype = "timehhmmss"
355 o = s:option(Value, "stop_time", translate("Stop Time (hh:mm:ss)"))
356 o.datatype = "timehhmmss"
357 o = s:option(Value, "start_date", translate("Start Date (yyyy-mm-dd)"))
358 o.datatype = "dateyyyymmdd"
359 o = s:option(Value, "stop_date", translate("Stop Date (yyyy-mm-dd)"))
360 o.datatype = "dateyyyymmdd"
362 o = s:option(Flag, "utc_time", translate("Time in UTC"))
363 o.default = o.disabled