applications/luci-firewall: complete rework firewall ui
[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 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 $Id$
14 ]]--
15
16 local sys = require "luci.sys"
17 local dsp = require "luci.dispatcher"
18 local nxo = require "nixio"
19
20 local nw = require "luci.model.network"
21 local m, s, o, k, v
22
23 arg[1] = arg[1] or ""
24
25 m = Map("firewall",
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 \
29                            hosts."))
30
31 m.redirect = dsp.build_url("admin/network/firewall/rules")
32
33 nw.init(m.uci)
34
35 local rule_type = m.uci:get("firewall", arg[1])
36 if rule_type == "redirect" and m:get(arg[1], "target") ~= "SNAT" then
37         rule_type = nil
38 end
39
40 if not rule_type then
41         luci.http.redirect(m.redirect)
42         return
43
44 --
45 -- SNAT
46 --
47 elseif rule_type == "redirect" then
48
49         local name = m:get(arg[1], "_name")
50         if not name or #name == 0 then
51                 name = translate("(Unnamed SNAT)")
52         else
53                 name = "SNAT %s" % name
54         end
55
56         m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name }
57
58         local wan_zone = nil
59
60         m.uci:foreach("firewall", "zone",
61                 function(s)
62                         local n = s.network or s.name
63                         if n then
64                                 local i
65                                 for i in n:gmatch("%S+") do
66                                         if i == "wan" then
67                                                 wan_zone = s.name
68                                                 return false
69                                         end
70                                 end
71                         end
72                 end)
73
74         s = m:section(NamedSection, arg[1], "redirect", "")
75         s.anonymous = true
76         s.addremove = false
77
78
79         o = s:option(Value, "_name", translate("Name"))
80         o.rmempty = true
81         o.size = 10
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(DynamicList, "src_mac", translate("Source MAC address"))
111         o.rmempty = true
112         o.datatype = "neg(macaddr)"
113         o.placeholder = translate("any")
114
115
116         o = s:option(Value, "src_ip", translate("Source IP address"))
117         o.rmempty = true
118         o.datatype = "neg(ip4addr)"
119         o.placeholder = translate("any")
120
121
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."))
126         o.rmempty = true
127         o.datatype = "neg(portrange)"
128         o.placeholder = translate("any")
129
130
131         o = s:option(Value, "dest", translate("Destination zone"))
132         o.nocreate = true
133         o.default = "lan"
134         o.template = "cbi/firewall_zonelist"
135
136
137         o = s:option(Value, "dest_ip", translate("Destination IP address"))
138         o.datatype = "neg(ip4addr)"
139
140         for i, dataset in ipairs(luci.sys.net.arptable()) do
141                 o:value(dataset["IP address"])
142         end
143
144
145         o = s:option(Value, "dest_port",
146                 translate("Destination port"),
147                 translate("Match forwarded traffic to the given destination port or \
148                         port range."))
149
150         o.rmempty = true
151         o.placeholder = translate("any")
152         o.datatype = "portrange"
153
154
155         o = s:option(Value, "src_dip",
156                 translate("SNAT IP address"),
157                 translate("Rewrite matched traffic to the given address."))
158         o.rmempty = false
159         o.datatype = "ip4addr"
160
161         for k, v in ipairs(nw:get_interfaces()) do
162                 local a
163                 for k, a in ipairs(v:ipaddrs()) do
164                         o:value(a:host():string(), '%s (%s)' %{
165                                 a:host():string(), v:shortname()
166                         })
167                 end
168         end
169
170
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"
175         o.rmempty = true
176         o.placeholder = translate('Do not rewrite')
177
178
179 --
180 -- Rule
181 --
182 else
183         s = m:section(NamedSection, arg[1], "rule", "")
184         s.anonymous = true
185         s.addremove = false
186
187         s:option(Value, "_name", translate("Name").." "..translate("(optional)"))
188
189
190         o = s:option(ListValue, "family", translate("Restrict to address family"))
191         o.rmempty = true
192         o:value("", translate("IPv4 and IPv6"))
193         o:value("ipv4", translate("IPv4 only"))
194         o:value("ipv6", translate("IPv6 only"))
195
196
197         o = s:option(Value, "proto", translate("Protocol"))
198         o:value("all", translate("Any"))
199         o:value("tcp udp", "TCP+UDP")
200         o:value("tcp", "TCP")
201         o:value("udp", "UDP")
202         o:value("icmp", "ICMP")
203
204         function o.cfgvalue(...)
205                 local v = Value.cfgvalue(...)
206                 if not v or v == "tcpudp" then
207                         return "tcp udp"
208                 end
209                 return v
210         end
211
212
213         o = s:option(DynamicList, "icmp_type", translate("Match ICMP type"))
214         o:value("", "any")
215         o:value("echo-reply")
216         o:value("destination-unreachable")
217         o:value("network-unreachable")
218         o:value("host-unreachable")
219         o:value("protocol-unreachable")
220         o:value("port-unreachable")
221         o:value("fragmentation-needed")
222         o:value("source-route-failed")
223         o:value("network-unknown")
224         o:value("host-unknown")
225         o:value("network-prohibited")
226         o:value("host-prohibited")
227         o:value("TOS-network-unreachable")
228         o:value("TOS-host-unreachable")
229         o:value("communication-prohibited")
230         o:value("host-precedence-violation")
231         o:value("precedence-cutoff")
232         o:value("source-quench")
233         o:value("redirect")
234         o:value("network-redirect")
235         o:value("host-redirect")
236         o:value("TOS-network-redirect")
237         o:value("TOS-host-redirect")
238         o:value("echo-request")
239         o:value("router-advertisement")
240         o:value("router-solicitation")
241         o:value("time-exceeded")
242         o:value("ttl-zero-during-transit")
243         o:value("ttl-zero-during-reassembly")
244         o:value("parameter-problem")
245         o:value("ip-header-bad")
246         o:value("required-option-missing")
247         o:value("timestamp-request")
248         o:value("timestamp-reply")
249         o:value("address-mask-request")
250         o:value("address-mask-reply")
251
252
253         o = s:option(Value, "src", translate("Source zone"))
254         o.nocreate = true
255         o.allowany = true
256         o.default = "wan"
257         o.template = "cbi/firewall_zonelist"
258
259
260         o = s:option(Value, "src_mac", translate("Source MAC address"))
261         o.datatype = "list(macaddr)"
262         o.placeholder = translate("any")
263
264
265         o = s:option(Value, "src_ip", translate("Source address"))
266         o.datatype = "neg(ipaddr)"
267         o.placeholder = translate("any")
268
269
270         o = s:option(Value, "src_port", translate("Source port"))
271         o.datatype = "list(neg,portrange)"
272         o.placeholder = translate("any")
273
274
275         o = s:option(Value, "dest", translate("Destination zone"))
276         o.nocreate = true
277         o.allowany = true
278         o.allowlocal = true
279         o.template = "cbi/firewall_zonelist"
280
281
282         o = s:option(Value, "dest_ip", translate("Destination address"))
283         o.datatype = "neg(ipaddr)"
284         o.placeholder = translate("any")
285
286
287         o = s:option(Value, "dest_port", translate("Destination port"))
288         o.datatype = "list(neg,portrange)"
289         o.placeholder = translate("any")
290
291
292         o = s:option(ListValue, "target", translate("Action"))
293         o.default = "ACCEPT"
294         o:value("DROP", translate("drop"))
295         o:value("ACCEPT", translate("accept"))
296         o:value("REJECT", translate("reject"))
297         o:value("NOTRACK", translate("don't track"))
298 end
299
300 return m