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