290096ba44243ce44563ec05216b8119b002a4c3
[project/luci.git] / applications / luci-firewall / luasrc / model / cbi / firewall / rules.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local ds = require "luci.dispatcher"
16 local ft = require "luci.tools.firewall"
17
18 m = Map("firewall",
19         translate("Firewall - Traffic Rules"),
20         translate("Traffic rules define policies for packets traveling between \
21                 different zones, for example to reject traffic between certain hosts \
22                 or to open WAN ports on the router."))
23
24 --
25 -- Rules
26 --
27
28 s = m:section(TypedSection, "rule", translate("Traffic Rules"))
29 s.addremove = true
30 s.anonymous = true
31 s.sortable  = true
32 s.template = "cbi/tblsection"
33 s.extedit   = ds.build_url("admin/network/firewall/rules/%s")
34 s.defaults.target = "ACCEPT"
35 s.template_addremove = "firewall/cbi_addrule"
36
37
38 function s.create(self, section)
39         created = TypedSection.create(self, section)
40 end
41
42 function s.parse(self, ...)
43         TypedSection.parse(self, ...)
44
45         local i_n = m:formvalue("_newopen.name")
46         local i_p = m:formvalue("_newopen.proto")
47         local i_e = m:formvalue("_newopen.extport")
48         local i_x = m:formvalue("_newopen.submit")
49
50         local f_n = m:formvalue("_newfwd.name")
51         local f_s = m:formvalue("_newfwd.src")
52         local f_d = m:formvalue("_newfwd.dest")
53         local f_x = m:formvalue("_newfwd.submit")
54
55         if i_x then
56                 created = TypedSection.create(self, section)
57
58                 self.map:set(created, "target",    "ACCEPT")
59                 self.map:set(created, "src",       "wan")
60                 self.map:set(created, "proto",     (i_p ~= "other") and i_p or "all")
61                 self.map:set(created, "dest_port", i_e)
62                 self.map:set(created, "name",      i_n)
63
64                 if i_p ~= "other" and i_e and #i_e > 0 then
65                         created = nil
66                 end
67
68         elseif f_x then
69                 created = TypedSection.create(self, section)
70
71                 self.map:set(created, "target", "ACCEPT")
72                 self.map:set(created, "src",    f_s)
73                 self.map:set(created, "dest",   f_d)
74                 self.map:set(created, "name",   f_n)
75         end
76
77         if created then
78                 m.uci:save("firewall")
79                 luci.http.redirect(ds.build_url(
80                         "admin/network/firewall/rules", created
81                 ))
82         end
83 end
84
85 ft.opt_name(s, DummyValue, translate("Name"))
86
87 family = s:option(DummyValue, "family", translate("Family"))
88 function family.cfgvalue(self, s)
89         local f = self.map:get(s, "family")
90         if f and f:match("4") then
91                 return translate("IPv4")
92         elseif f and f:match("6") then
93                 return translate("IPv6")
94         else
95                 return translate("Any")
96         end
97 end
98
99 proto = s:option(DummyValue, "proto", translate("Protocol"))
100 proto.rawhtml = true
101 proto.width   = "20%"
102 function proto.cfgvalue(self, s)
103         return ft.fmt_proto(self.map:get(s, "proto"), self.map:get(s, "icmp_type"))
104                 or "TCP+UDP"
105 end
106
107 src = s:option(DummyValue, "src", translate("Source"))
108 src.rawhtml = true
109 src.width   = "20%"
110 function src.cfgvalue(self, s)
111         local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
112         local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
113         local p = ft.fmt_port(self.map:get(s, "src_port"))
114         local m = ft.fmt_mac(self.map:get(s, "src_mac"))
115
116         if p and m then
117                 return translatef("From %s in %s with source %s and %s", a, z, p, m)
118         elseif p or m then
119                 return translatef("From %s in %s with source %s", a, z, p or m)
120         else
121                 return translatef("From %s in %s", a, z)
122         end
123 end
124
125 dest = s:option(DummyValue, "dest", translate("Destination"))
126 dest.rawhtml = true
127 dest.width   = "20%"
128 function dest.cfgvalue(self, s)
129         local z = ft.fmt_zone(self.map:get(s, "dest"))
130         local p = ft.fmt_port(self.map:get(s, "dest_port"))
131
132         -- Forward
133         if z then
134                 local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
135                 if p then
136                         return translatef("To %s, %s in %s", a, p, z)
137                 else
138                         return translatef("To %s in %s", a, z)
139                 end
140
141         -- Input
142         else
143                 local a = ft.fmt_ip(self.map:get(s, "dest_ip"),
144                         translate("any router IP"))
145
146                 if p then
147                         return translatef("To %s at %s on <var>this device</var>", a, p)
148                 else
149                         return translatef("To %s on <var>this device</var>", a)
150                 end
151         end
152 end
153
154
155 target = s:option(DummyValue, "target", translate("Action"))
156 target.rawhtml = true
157 target.width   = "20%"
158 function target.cfgvalue(self, s)
159         local t = ft.fmt_target(self.map:get(s, "target"), self.map:get(s, "dest"))
160         local l = ft.fmt_limit(self.map:get(s, "limit"),
161                 self.map:get(s, "limit_burst"))
162
163         if l then
164                 return translatef("<var>%s</var> and limit to %s", t, l)
165         else
166                 return "<var>%s</var>" % t
167         end
168 end
169
170 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
171
172
173 --
174 -- SNAT
175 --
176
177 s = m:section(TypedSection, "redirect",
178         translate("Source NAT"),
179         translate("Source NAT is a specific form of masquerading which allows \
180                 fine grained control over the source IP used for outgoing traffic, \
181                 for example to map multiple WAN addresses to internal subnets."))
182 s.template  = "cbi/tblsection"
183 s.addremove = true
184 s.anonymous = true
185 s.sortable  = true
186 s.extedit   = ds.build_url("admin/network/firewall/rules/%s")
187 s.template_addremove = "firewall/cbi_addsnat"
188
189 function s.create(self, section)
190         created = TypedSection.create(self, section)
191 end
192
193 function s.parse(self, ...)
194         TypedSection.parse(self, ...)
195
196         local n = m:formvalue("_newsnat.name")
197         local s = m:formvalue("_newsnat.src")
198         local d = m:formvalue("_newsnat.dest")
199         local a = m:formvalue("_newsnat.dip")
200         local p = m:formvalue("_newsnat.dport")
201         local x = m:formvalue("_newsnat.submit")
202
203         if x and a and #a > 0 then
204                 created = TypedSection.create(self, section)
205
206                 self.map:set(created, "target",    "SNAT")
207                 self.map:set(created, "src",       s)
208                 self.map:set(created, "dest",      d)
209                 self.map:set(created, "proto",     "all")
210                 self.map:set(created, "src_dip",   a)
211                 self.map:set(created, "src_dport", p)
212                 self.map:set(created, "name",      n)
213         end
214
215         if created then
216                 m.uci:save("firewall")
217                 luci.http.redirect(ds.build_url(
218                         "admin/network/firewall/rules", created
219                 ))
220         end
221 end
222
223 function s.filter(self, sid)
224         return (self.map:get(sid, "target") == "SNAT")
225 end
226
227 ft.opt_name(s, DummyValue, translate("Name"))
228
229 proto = s:option(DummyValue, "proto", translate("Protocol"))
230 proto.rawhtml = true
231 function proto.cfgvalue(self, s)
232         return ft.fmt_proto(self.map:get(s, "proto")) or "TCP+UDP"
233 end
234
235
236 src = s:option(DummyValue, "src", translate("Source"))
237 src.rawhtml = true
238 src.width   = "20%"
239 function src.cfgvalue(self, s)
240         local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
241         local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
242         local p = ft.fmt_port(self.map:get(s, "src_port"))
243         local m = ft.fmt_mac(self.map:get(s, "src_mac"))
244
245         if p and m then
246                 return translatef("From %s in %s with source %s and %s", a, z, p, m)
247         elseif p or m then
248                 return translatef("From %s in %s with source %s", a, z, p or m)
249         else
250                 return translatef("From %s in %s", a, z)
251         end
252 end
253
254 dest = s:option(DummyValue, "dest", translate("Destination"))
255 dest.rawhtml = true
256 dest.width   = "30%"
257 function dest.cfgvalue(self, s)
258         local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
259         local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
260         local p = ft.fmt_port(self.map:get(s, "dest_port")) or
261                 ft.fmt_port(self.map:get(s, "src_dport"))
262
263         if p then
264                 return translatef("To %s, %s in %s", a, p, z)
265         else
266                 return translatef("To %s in %s", a, z)
267         end
268 end
269
270 snat = s:option(DummyValue, "via", translate("SNAT"))
271 snat.rawhtml = true
272 snat.width   = "20%"
273 function snat.cfgvalue(self, s)
274         local a = ft.fmt_ip(self.map:get(s, "src_dip"))
275         local p = ft.fmt_port(self.map:get(s, "src_dport"))
276
277         if a and p then
278                 return translatef("Rewrite to source %s, %s", a, p)
279         else
280                 return translatef("Rewrite to source %s", a or p)
281         end
282 end
283
284 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
285
286
287 return m