2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010 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
16 local sys = require "luci.sys"
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 n:gmatch("%S+") 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(DynamicList, "src_mac", translate("Source MAC address"))
112 o.datatype = "neg(macaddr)"
113 o.placeholder = translate("any")
116 o = s:option(Value, "src_ip", translate("Source IP address"))
118 o.datatype = "neg(ipaddr)"
119 o.placeholder = translate("any")
122 o = s:option(Value, "src_port",
123 translate("Source port"),
124 translate("Match incoming traffic originating from the given source \
125 port or port range on the client host."))
127 o.datatype = "neg(portrange)"
128 o.placeholder = translate("any")
131 o = s:option(Value, "dest", translate("Destination zone"))
134 o.template = "cbi/firewall_zonelist"
137 o = s:option(Value, "dest_ip", translate("Destination IP address"))
138 o.datatype = "neg(ip4addr)"
140 for i, dataset in ipairs(luci.sys.net.arptable()) do
141 o:value(dataset["IP address"])
145 o = s:option(Value, "dest_port",
146 translate("Destination port"),
147 translate("Match forwarded traffic to the given destination port or \
151 o.placeholder = translate("any")
152 o.datatype = "portrange"
155 o = s:option(Value, "src_dip",
156 translate("SNAT IP address"),
157 translate("Rewrite matched traffic to the given address."))
159 o.datatype = "ip4addr"
161 for k, v in ipairs(nw:get_interfaces()) do
163 for k, a in ipairs(v:ipaddrs()) do
164 o:value(a:host():string(), '%s (%s)' %{
165 a:host():string(), v:shortname()
171 o = s:option(Value, "src_dport", translate("SNAT port"),
172 translate("Rewrite matched traffic to the given source port. May be \
173 left empty to only rewrite the IP address."))
174 o.datatype = "portrange"
176 o.placeholder = translate('Do not rewrite')
179 s:option(Value, "extra",
180 translate("Extra arguments"),
181 translate("Passes additional arguments to iptables. Use with care!"))
188 local name = m:get(arg[1], "name") or m:get(arg[1], "_name")
189 if not name or #name == 0 then
190 name = translate("(Unnamed Rule)")
193 m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
196 s = m:section(NamedSection, arg[1], "rule", "")
200 ft.opt_enabled(s, Button)
201 ft.opt_name(s, Value, translate("Name"))
204 o = s:option(ListValue, "family", translate("Restrict to address family"))
206 o:value("", translate("IPv4 and IPv6"))
207 o:value("ipv4", translate("IPv4 only"))
208 o:value("ipv6", translate("IPv6 only"))
211 o = s:option(Value, "proto", translate("Protocol"))
212 o:value("all", translate("Any"))
213 o:value("tcp udp", "TCP+UDP")
214 o:value("tcp", "TCP")
215 o:value("udp", "UDP")
216 o:value("icmp", "ICMP")
218 function o.cfgvalue(...)
219 local v = Value.cfgvalue(...)
220 if not v or v == "tcpudp" then
227 o = s:option(DynamicList, "icmp_type", translate("Match ICMP type"))
229 o:value("echo-reply")
230 o:value("destination-unreachable")
231 o:value("network-unreachable")
232 o:value("host-unreachable")
233 o:value("protocol-unreachable")
234 o:value("port-unreachable")
235 o:value("fragmentation-needed")
236 o:value("source-route-failed")
237 o:value("network-unknown")
238 o:value("host-unknown")
239 o:value("network-prohibited")
240 o:value("host-prohibited")
241 o:value("TOS-network-unreachable")
242 o:value("TOS-host-unreachable")
243 o:value("communication-prohibited")
244 o:value("host-precedence-violation")
245 o:value("precedence-cutoff")
246 o:value("source-quench")
248 o:value("network-redirect")
249 o:value("host-redirect")
250 o:value("TOS-network-redirect")
251 o:value("TOS-host-redirect")
252 o:value("echo-request")
253 o:value("router-advertisement")
254 o:value("router-solicitation")
255 o:value("time-exceeded")
256 o:value("ttl-zero-during-transit")
257 o:value("ttl-zero-during-reassembly")
258 o:value("parameter-problem")
259 o:value("ip-header-bad")
260 o:value("required-option-missing")
261 o:value("timestamp-request")
262 o:value("timestamp-reply")
263 o:value("address-mask-request")
264 o:value("address-mask-reply")
267 o = s:option(Value, "src", translate("Source zone"))
271 o.template = "cbi/firewall_zonelist"
274 o = s:option(Value, "src_mac", translate("Source MAC address"))
275 o.datatype = "list(macaddr)"
276 o.placeholder = translate("any")
279 o = s:option(Value, "src_ip", translate("Source address"))
280 o.datatype = "neg(ipaddr)"
281 o.placeholder = translate("any")
284 o = s:option(Value, "src_port", translate("Source port"))
285 o.datatype = "list(neg(portrange))"
286 o.placeholder = translate("any")
289 o = s:option(Value, "dest", translate("Destination zone"))
293 o.template = "cbi/firewall_zonelist"
296 o = s:option(Value, "dest_ip", translate("Destination address"))
297 o.datatype = "neg(ipaddr)"
298 o.placeholder = translate("any")
301 o = s:option(Value, "dest_port", translate("Destination port"))
302 o.datatype = "list(neg(portrange))"
303 o.placeholder = translate("any")
306 o = s:option(ListValue, "target", translate("Action"))
308 o:value("DROP", translate("drop"))
309 o:value("ACCEPT", translate("accept"))
310 o:value("REJECT", translate("reject"))
311 o:value("NOTRACK", translate("don't track"))
314 s:option(Value, "extra",
315 translate("Extra arguments"),
316 translate("Passes additional arguments to iptables. Use with care!"))