applications/luci-firewall: cope with both tables and strings when processing zone...
[project/luci.git] / applications / luci-firewall / luasrc / model / cbi / firewall / rule-details.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
6
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
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 ]]--
14
15 local sys = require "luci.sys"
16 local utl = require "luci.util"
17 local dsp = require "luci.dispatcher"
18 local nxo = require "nixio"
19
20 local ft = require "luci.tools.firewall"
21 local nw = require "luci.model.network"
22 local m, s, o, k, v
23
24 arg[1] = arg[1] or ""
25
26 m = Map("firewall",
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 \
30                            hosts."))
31
32 m.redirect = dsp.build_url("admin/network/firewall/rules")
33
34 nw.init(m.uci)
35
36 local rule_type = m.uci:get("firewall", arg[1])
37 if rule_type == "redirect" and m:get(arg[1], "target") ~= "SNAT" then
38         rule_type = nil
39 end
40
41 if not rule_type then
42         luci.http.redirect(m.redirect)
43         return
44
45 --
46 -- SNAT
47 --
48 elseif rule_type == "redirect" then
49
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)")
53         else
54                 name = "SNAT %s" % name
55         end
56
57         m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
58
59         local wan_zone = nil
60
61         m.uci:foreach("firewall", "zone",
62                 function(s)
63                         local n = s.network or s.name
64                         if n then
65                                 local i
66                                 for i in utl.imatch(n) do
67                                         if i == "wan" then
68                                                 wan_zone = s.name
69                                                 return false
70                                         end
71                                 end
72                         end
73                 end)
74
75         s = m:section(NamedSection, arg[1], "redirect", "")
76         s.anonymous = true
77         s.addremove = false
78
79
80         ft.opt_enabled(s, Button)
81         ft.opt_name(s, Value, translate("Name"))
82
83
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."))
88
89         o:value("all", "All protocols")
90         o:value("tcp udp", "TCP+UDP")
91         o:value("tcp", "TCP")
92         o:value("udp", "UDP")
93         o:value("icmp", "ICMP")
94
95         function o.cfgvalue(...)
96                 local v = Value.cfgvalue(...)
97                 if not v or v == "tcpudp" then
98                         return "tcp udp"
99                 end
100                 return v
101         end
102
103
104         o = s:option(Value, "src", translate("Source zone"))
105         o.nocreate = true
106         o.default = "wan"
107         o.template = "cbi/firewall_zonelist"
108
109
110         o = s:option(Value, "src_ip", translate("Source IP address"))
111         o.rmempty = true
112         o.datatype = "neg(ipaddr)"
113         o.placeholder = translate("any")
114
115         luci.sys.net.ipv4_hints(function(ip, name)
116                 o:value(ip, "%s (%s)" %{ ip, name })
117         end)
118
119
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."))
124         o.rmempty = true
125         o.datatype = "neg(portrange)"
126         o.placeholder = translate("any")
127
128
129         o = s:option(Value, "dest", translate("Destination zone"))
130         o.nocreate = true
131         o.default = "lan"
132         o.template = "cbi/firewall_zonelist"
133
134
135         o = s:option(Value, "dest_ip", translate("Destination IP address"))
136         o.datatype = "neg(ip4addr)"
137
138         luci.sys.net.ipv4_hints(function(ip, name)
139                 o:value(ip, "%s (%s)" %{ ip, name })
140         end)
141
142
143         o = s:option(Value, "dest_port",
144                 translate("Destination port"),
145                 translate("Match forwarded traffic to the given destination port or \
146                         port range."))
147
148         o.rmempty = true
149         o.placeholder = translate("any")
150         o.datatype = "neg(portrange)"
151
152
153         o = s:option(Value, "src_dip",
154                 translate("SNAT IP address"),
155                 translate("Rewrite matched traffic to the given address."))
156         o.rmempty = false
157         o.datatype = "ip4addr"
158
159         for k, v in ipairs(nw:get_interfaces()) do
160                 local a
161                 for k, a in ipairs(v:ipaddrs()) do
162                         o:value(a:host():string(), '%s (%s)' %{
163                                 a:host():string(), v:shortname()
164                         })
165                 end
166         end
167
168
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"
173         o.rmempty = true
174         o.placeholder = translate('Do not rewrite')
175
176
177         s:option(Value, "extra",
178                 translate("Extra arguments"),
179                 translate("Passes additional arguments to iptables. Use with care!"))
180
181
182 --
183 -- Rule
184 --
185 else
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)")
189         end
190
191         m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
192
193
194         s = m:section(NamedSection, arg[1], "rule", "")
195         s.anonymous = true
196         s.addremove = false
197
198         ft.opt_enabled(s, Button)
199         ft.opt_name(s, Value, translate("Name"))
200
201
202         o = s:option(ListValue, "family", translate("Restrict to address family"))
203         o.rmempty = true
204         o:value("", translate("IPv4 and IPv6"))
205         o:value("ipv4", translate("IPv4 only"))
206         o:value("ipv6", translate("IPv6 only"))
207
208
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")
215
216         function o.cfgvalue(...)
217                 local v = Value.cfgvalue(...)
218                 if not v or v == "tcpudp" then
219                         return "tcp udp"
220                 end
221                 return v
222         end
223
224
225         o = s:option(DynamicList, "icmp_type", translate("Match ICMP type"))
226         o:value("", "any")
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")
245         o:value("redirect")
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")
263
264
265         o = s:option(Value, "src", translate("Source zone"))
266         o.nocreate = true
267         o.allowany = true
268         o.default = "wan"
269         o.template = "cbi/firewall_zonelist"
270
271
272         o = s:option(Value, "src_mac", translate("Source MAC address"))
273         o.datatype = "list(macaddr)"
274         o.placeholder = translate("any")
275
276         luci.sys.net.mac_hints(function(mac, name)
277                 o:value(mac, "%s (%s)" %{ mac, name })
278         end)
279
280
281         o = s:option(Value, "src_ip", translate("Source address"))
282         o.datatype = "neg(ipaddr)"
283         o.placeholder = translate("any")
284
285         luci.sys.net.ipv4_hints(function(ip, name)
286                 o:value(ip, "%s (%s)" %{ ip, name })
287         end)
288
289
290         o = s:option(Value, "src_port", translate("Source port"))
291         o.datatype = "list(neg(portrange))"
292         o.placeholder = translate("any")
293
294
295         o = s:option(Value, "dest", translate("Destination zone"))
296         o.nocreate = true
297         o.allowany = true
298         o.allowlocal = true
299         o.template = "cbi/firewall_zonelist"
300
301
302         o = s:option(Value, "dest_ip", translate("Destination address"))
303         o.datatype = "neg(ipaddr)"
304         o.placeholder = translate("any")
305
306         luci.sys.net.ipv4_hints(function(ip, name)
307                 o:value(ip, "%s (%s)" %{ ip, name })
308         end)
309
310
311         o = s:option(Value, "dest_port", translate("Destination port"))
312         o.datatype = "list(neg(portrange))"
313         o.placeholder = translate("any")
314
315
316         o = s:option(ListValue, "target", translate("Action"))
317         o.default = "ACCEPT"
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"))
322
323
324         s:option(Value, "extra",
325                 translate("Extra arguments"),
326                 translate("Passes additional arguments to iptables. Use with care!"))
327 end
328
329 return m