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 dsp = require "luci.dispatcher"
17 local nxo = require "nixio"
19 local ft = require "luci.tools.firewall"
20 local nw = require "luci.model.network"
26 translate("Firewall - Traffic Rules"),
27 translate("This page allows you to change advanced properties of the \
28 traffic rule entry, such as matched source and destination \
31 m.redirect = dsp.build_url("admin/network/firewall/rules")
35 local rule_type = m.uci:get("firewall", arg[1])
36 if rule_type == "redirect" and m:get(arg[1], "target") ~= "SNAT" then
41 luci.http.redirect(m.redirect)
47 elseif rule_type == "redirect" then
49 local name = m:get(arg[1], "name") or m:get(arg[1], "_name")
50 if not name or #name == 0 then
51 name = translate("(Unnamed SNAT)")
53 name = "SNAT %s" % name
56 m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
60 m.uci:foreach("firewall", "zone",
62 local n = s.network or s.name
65 for i in n:gmatch("%S+") do
74 s = m:section(NamedSection, arg[1], "redirect", "")
79 ft.opt_enabled(s, Button)
80 ft.opt_name(s, Value, translate("Name"))
83 o = s:option(Value, "proto",
84 translate("Protocol"),
85 translate("You may specify multiple by selecting \"-- custom --\" and \
86 then entering protocols separated by space."))
88 o:value("all", "All protocols")
89 o:value("tcp udp", "TCP+UDP")
92 o:value("icmp", "ICMP")
94 function o.cfgvalue(...)
95 local v = Value.cfgvalue(...)
96 if not v or v == "tcpudp" then
103 o = s:option(Value, "src", translate("Source zone"))
106 o.template = "cbi/firewall_zonelist"
109 o = s:option(Value, "src_ip", translate("Source IP address"))
111 o.datatype = "neg(ipaddr)"
112 o.placeholder = translate("any")
114 luci.sys.net.ipv4_hints(function(ip, name)
115 o:value(ip, "%s (%s)" %{ ip, name })
119 o = s:option(Value, "src_port",
120 translate("Source port"),
121 translate("Match incoming traffic originating from the given source \
122 port or port range on the client host."))
124 o.datatype = "neg(portrange)"
125 o.placeholder = translate("any")
128 o = s:option(Value, "dest", translate("Destination zone"))
131 o.template = "cbi/firewall_zonelist"
134 o = s:option(Value, "dest_ip", translate("Destination IP address"))
135 o.datatype = "neg(ip4addr)"
137 luci.sys.net.ipv4_hints(function(ip, name)
138 o:value(ip, "%s (%s)" %{ ip, name })
142 o = s:option(Value, "dest_port",
143 translate("Destination port"),
144 translate("Match forwarded traffic to the given destination port or \
148 o.placeholder = translate("any")
149 o.datatype = "neg(portrange)"
152 o = s:option(Value, "src_dip",
153 translate("SNAT IP address"),
154 translate("Rewrite matched traffic to the given address."))
156 o.datatype = "ip4addr"
158 for k, v in ipairs(nw:get_interfaces()) do
160 for k, a in ipairs(v:ipaddrs()) do
161 o:value(a:host():string(), '%s (%s)' %{
162 a:host():string(), v:shortname()
168 o = s:option(Value, "src_dport", translate("SNAT port"),
169 translate("Rewrite matched traffic to the given source port. May be \
170 left empty to only rewrite the IP address."))
171 o.datatype = "portrange"
173 o.placeholder = translate('Do not rewrite')
176 s:option(Value, "extra",
177 translate("Extra arguments"),
178 translate("Passes additional arguments to iptables. Use with care!"))
185 local name = m:get(arg[1], "name") or m:get(arg[1], "_name")
186 if not name or #name == 0 then
187 name = translate("(Unnamed Rule)")
190 m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
193 s = m:section(NamedSection, arg[1], "rule", "")
197 ft.opt_enabled(s, Button)
198 ft.opt_name(s, Value, translate("Name"))
201 o = s:option(ListValue, "family", translate("Restrict to address family"))
203 o:value("", translate("IPv4 and IPv6"))
204 o:value("ipv4", translate("IPv4 only"))
205 o:value("ipv6", translate("IPv6 only"))
208 o = s:option(Value, "proto", translate("Protocol"))
209 o:value("all", translate("Any"))
210 o:value("tcp udp", "TCP+UDP")
211 o:value("tcp", "TCP")
212 o:value("udp", "UDP")
213 o:value("icmp", "ICMP")
215 function o.cfgvalue(...)
216 local v = Value.cfgvalue(...)
217 if not v or v == "tcpudp" then
224 o = s:option(DynamicList, "icmp_type", translate("Match ICMP type"))
226 o:value("echo-reply")
227 o:value("destination-unreachable")
228 o:value("network-unreachable")
229 o:value("host-unreachable")
230 o:value("protocol-unreachable")
231 o:value("port-unreachable")
232 o:value("fragmentation-needed")
233 o:value("source-route-failed")
234 o:value("network-unknown")
235 o:value("host-unknown")
236 o:value("network-prohibited")
237 o:value("host-prohibited")
238 o:value("TOS-network-unreachable")
239 o:value("TOS-host-unreachable")
240 o:value("communication-prohibited")
241 o:value("host-precedence-violation")
242 o:value("precedence-cutoff")
243 o:value("source-quench")
245 o:value("network-redirect")
246 o:value("host-redirect")
247 o:value("TOS-network-redirect")
248 o:value("TOS-host-redirect")
249 o:value("echo-request")
250 o:value("router-advertisement")
251 o:value("router-solicitation")
252 o:value("time-exceeded")
253 o:value("ttl-zero-during-transit")
254 o:value("ttl-zero-during-reassembly")
255 o:value("parameter-problem")
256 o:value("ip-header-bad")
257 o:value("required-option-missing")
258 o:value("timestamp-request")
259 o:value("timestamp-reply")
260 o:value("address-mask-request")
261 o:value("address-mask-reply")
264 o = s:option(Value, "src", translate("Source zone"))
268 o.template = "cbi/firewall_zonelist"
271 o = s:option(Value, "src_mac", translate("Source MAC address"))
272 o.datatype = "list(macaddr)"
273 o.placeholder = translate("any")
275 luci.sys.net.mac_hints(function(mac, name)
276 o:value(mac, "%s (%s)" %{ mac, name })
280 o = s:option(Value, "src_ip", translate("Source address"))
281 o.datatype = "neg(ipaddr)"
282 o.placeholder = translate("any")
284 luci.sys.net.ipv4_hints(function(ip, name)
285 o:value(ip, "%s (%s)" %{ ip, name })
289 o = s:option(Value, "src_port", translate("Source port"))
290 o.datatype = "list(neg(portrange))"
291 o.placeholder = translate("any")
294 o = s:option(Value, "dest", translate("Destination zone"))
298 o.template = "cbi/firewall_zonelist"
301 o = s:option(Value, "dest_ip", translate("Destination address"))
302 o.datatype = "neg(ipaddr)"
303 o.placeholder = translate("any")
305 luci.sys.net.ipv4_hints(function(ip, name)
306 o:value(ip, "%s (%s)" %{ ip, name })
310 o = s:option(Value, "dest_port", translate("Destination port"))
311 o.datatype = "list(neg(portrange))"
312 o.placeholder = translate("any")
315 o = s:option(ListValue, "target", translate("Action"))
317 o:value("DROP", translate("drop"))
318 o:value("ACCEPT", translate("accept"))
319 o:value("REJECT", translate("reject"))
320 o:value("NOTRACK", translate("don't track"))
323 s:option(Value, "extra",
324 translate("Extra arguments"),
325 translate("Passes additional arguments to iptables. Use with care!"))