2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
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
11 http://www.apache.org/licenses/LICENSE-2.0
15 local sys = require "luci.sys"
16 local utl = require "luci.util"
17 local dsp = require "luci.dispatcher"
18 local nxo = require "nixio"
20 local ft = require "luci.tools.firewall"
21 local nw = require "luci.model.network"
27 translate("Firewall - Traffic Rules"),
28 translate("This page allows you to change advanced properties of the \
29 traffic rule entry, such as matched source and destination \
32 m.redirect = dsp.build_url("admin/network/firewall/rules")
36 local rule_type = m.uci:get("firewall", arg[1])
37 if rule_type == "redirect" and m:get(arg[1], "target") ~= "SNAT" then
42 luci.http.redirect(m.redirect)
48 elseif rule_type == "redirect" then
50 local name = m:get(arg[1], "name") or m:get(arg[1], "_name")
51 if not name or #name == 0 then
52 name = translate("(Unnamed SNAT)")
54 name = "SNAT %s" % name
57 m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
61 m.uci:foreach("firewall", "zone",
63 local n = s.network or s.name
66 for i in utl.imatch(n) do
75 s = m:section(NamedSection, arg[1], "redirect", "")
80 ft.opt_enabled(s, Button)
81 ft.opt_name(s, Value, translate("Name"))
84 o = s:option(Value, "proto",
85 translate("Protocol"),
86 translate("You may specify multiple by selecting \"-- custom --\" and \
87 then entering protocols separated by space."))
89 o:value("all", "All protocols")
90 o:value("tcp udp", "TCP+UDP")
93 o:value("icmp", "ICMP")
95 function o.cfgvalue(...)
96 local v = Value.cfgvalue(...)
97 if not v or v == "tcpudp" then
104 o = s:option(Value, "src", translate("Source zone"))
107 o.template = "cbi/firewall_zonelist"
110 o = s:option(Value, "src_ip", translate("Source IP address"))
112 o.datatype = "neg(ipaddr)"
113 o.placeholder = translate("any")
115 luci.sys.net.ipv4_hints(function(ip, name)
116 o:value(ip, "%s (%s)" %{ ip, name })
120 o = s:option(Value, "src_port",
121 translate("Source port"),
122 translate("Match incoming traffic originating from the given source \
123 port or port range on the client host."))
125 o.datatype = "neg(portrange)"
126 o.placeholder = translate("any")
129 o = s:option(Value, "dest", translate("Destination zone"))
132 o.template = "cbi/firewall_zonelist"
135 o = s:option(Value, "dest_ip", translate("Destination IP address"))
136 o.datatype = "neg(ip4addr)"
138 luci.sys.net.ipv4_hints(function(ip, name)
139 o:value(ip, "%s (%s)" %{ ip, name })
143 o = s:option(Value, "dest_port",
144 translate("Destination port"),
145 translate("Match forwarded traffic to the given destination port or \
149 o.placeholder = translate("any")
150 o.datatype = "neg(portrange)"
153 o = s:option(Value, "src_dip",
154 translate("SNAT IP address"),
155 translate("Rewrite matched traffic to the given address."))
157 o.datatype = "ip4addr"
159 for k, v in ipairs(nw:get_interfaces()) do
161 for k, a in ipairs(v:ipaddrs()) do
162 o:value(a:host():string(), '%s (%s)' %{
163 a:host():string(), v:shortname()
169 o = s:option(Value, "src_dport", translate("SNAT port"),
170 translate("Rewrite matched traffic to the given source port. May be \
171 left empty to only rewrite the IP address."))
172 o.datatype = "portrange"
174 o.placeholder = translate('Do not rewrite')
177 s:option(Value, "extra",
178 translate("Extra arguments"),
179 translate("Passes additional arguments to iptables. Use with care!"))
186 local name = m:get(arg[1], "name") or m:get(arg[1], "_name")
187 if not name or #name == 0 then
188 name = translate("(Unnamed Rule)")
191 m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
194 s = m:section(NamedSection, arg[1], "rule", "")
198 ft.opt_enabled(s, Button)
199 ft.opt_name(s, Value, translate("Name"))
202 o = s:option(ListValue, "family", translate("Restrict to address family"))
204 o:value("", translate("IPv4 and IPv6"))
205 o:value("ipv4", translate("IPv4 only"))
206 o:value("ipv6", translate("IPv6 only"))
209 o = s:option(Value, "proto", translate("Protocol"))
210 o:value("all", translate("Any"))
211 o:value("tcp udp", "TCP+UDP")
212 o:value("tcp", "TCP")
213 o:value("udp", "UDP")
214 o:value("icmp", "ICMP")
216 function o.cfgvalue(...)
217 local v = Value.cfgvalue(...)
218 if not v or v == "tcpudp" then
225 o = s:option(DynamicList, "icmp_type", translate("Match ICMP type"))
227 o:value("echo-reply")
228 o:value("destination-unreachable")
229 o:value("network-unreachable")
230 o:value("host-unreachable")
231 o:value("protocol-unreachable")
232 o:value("port-unreachable")
233 o:value("fragmentation-needed")
234 o:value("source-route-failed")
235 o:value("network-unknown")
236 o:value("host-unknown")
237 o:value("network-prohibited")
238 o:value("host-prohibited")
239 o:value("TOS-network-unreachable")
240 o:value("TOS-host-unreachable")
241 o:value("communication-prohibited")
242 o:value("host-precedence-violation")
243 o:value("precedence-cutoff")
244 o:value("source-quench")
246 o:value("network-redirect")
247 o:value("host-redirect")
248 o:value("TOS-network-redirect")
249 o:value("TOS-host-redirect")
250 o:value("echo-request")
251 o:value("router-advertisement")
252 o:value("router-solicitation")
253 o:value("time-exceeded")
254 o:value("ttl-zero-during-transit")
255 o:value("ttl-zero-during-reassembly")
256 o:value("parameter-problem")
257 o:value("ip-header-bad")
258 o:value("required-option-missing")
259 o:value("timestamp-request")
260 o:value("timestamp-reply")
261 o:value("address-mask-request")
262 o:value("address-mask-reply")
265 o = s:option(Value, "src", translate("Source zone"))
269 o.template = "cbi/firewall_zonelist"
272 o = s:option(Value, "src_mac", translate("Source MAC address"))
273 o.datatype = "list(macaddr)"
274 o.placeholder = translate("any")
276 luci.sys.net.mac_hints(function(mac, name)
277 o:value(mac, "%s (%s)" %{ mac, name })
281 o = s:option(Value, "src_ip", translate("Source address"))
282 o.datatype = "neg(ipaddr)"
283 o.placeholder = translate("any")
285 luci.sys.net.ipv4_hints(function(ip, name)
286 o:value(ip, "%s (%s)" %{ ip, name })
290 o = s:option(Value, "src_port", translate("Source port"))
291 o.datatype = "list(neg(portrange))"
292 o.placeholder = translate("any")
295 o = s:option(Value, "dest", translate("Destination zone"))
299 o.template = "cbi/firewall_zonelist"
302 o = s:option(Value, "dest_ip", translate("Destination address"))
303 o.datatype = "neg(ipaddr)"
304 o.placeholder = translate("any")
306 luci.sys.net.ipv4_hints(function(ip, name)
307 o:value(ip, "%s (%s)" %{ ip, name })
311 o = s:option(Value, "dest_port", translate("Destination port"))
312 o.datatype = "list(neg(portrange))"
313 o.placeholder = translate("any")
316 o = s:option(ListValue, "target", translate("Action"))
318 o:value("DROP", translate("drop"))
319 o:value("ACCEPT", translate("accept"))
320 o:value("REJECT", translate("reject"))
321 o:value("NOTRACK", translate("don't track"))
324 s:option(Value, "extra",
325 translate("Extra arguments"),
326 translate("Passes additional arguments to iptables. Use with care!"))