2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
15 local ds = require "luci.dispatcher"
16 local ft = require "luci.tools.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."))
28 s = m:section(TypedSection, "rule", translate("Traffic Rules"))
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"
38 function s.create(self, section)
39 created = TypedSection.create(self, section)
42 function s.parse(self, ...)
43 TypedSection.parse(self, ...)
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")
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")
56 created = TypedSection.create(self, section)
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)
64 if i_p ~= "other" and i_e and #i_e > 0 then
69 created = TypedSection.create(self, section)
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)
78 m.uci:save("firewall")
79 luci.http.redirect(ds.build_url(
80 "admin/network/firewall/rules", created
85 ft.opt_name(s, DummyValue, translate("Name"))
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")
95 return translate("Any")
99 proto = s:option(DummyValue, "proto", translate("Protocol"))
102 function proto.cfgvalue(self, s)
103 return ft.fmt_proto(self.map:get(s, "proto"), self.map:get(s, "icmp_type"))
107 src = s:option(DummyValue, "src", translate("Source"))
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"))
117 return translatef("From %s in %s with source %s and %s", a, z, p, m)
119 return translatef("From %s in %s with source %s", a, z, p or m)
121 return translatef("From %s in %s", a, z)
125 dest = s:option(DummyValue, "dest", translate("Destination"))
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"))
134 local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
136 return translatef("To %s, %s in %s", a, p, z)
138 return translatef("To %s in %s", a, z)
143 local a = ft.fmt_ip(self.map:get(s, "dest_ip"),
144 translate("any router IP"))
147 return translatef("To %s at %s on <var>this device</var>", a, p)
149 return translatef("To %s on <var>this device</var>", a)
155 target = s:option(DummyValue, "target", translate("Action"))
156 target.rawhtml = true
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"))
164 return translatef("<var>%s</var> and limit to %s", t, l)
166 return "<var>%s</var>" % t
170 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
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"
186 s.extedit = ds.build_url("admin/network/firewall/rules/%s")
187 s.template_addremove = "firewall/cbi_addsnat"
189 function s.create(self, section)
190 created = TypedSection.create(self, section)
193 function s.parse(self, ...)
194 TypedSection.parse(self, ...)
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")
203 if x and a and #a > 0 then
204 created = TypedSection.create(self, section)
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)
216 m.uci:save("firewall")
217 luci.http.redirect(ds.build_url(
218 "admin/network/firewall/rules", created
223 function s.filter(self, sid)
224 return (self.map:get(sid, "target") == "SNAT")
227 ft.opt_name(s, DummyValue, translate("Name"))
229 proto = s:option(DummyValue, "proto", translate("Protocol"))
231 function proto.cfgvalue(self, s)
232 return ft.fmt_proto(self.map:get(s, "proto")) or "TCP+UDP"
236 src = s:option(DummyValue, "src", translate("Source"))
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"))
246 return translatef("From %s in %s with source %s and %s", a, z, p, m)
248 return translatef("From %s in %s with source %s", a, z, p or m)
250 return translatef("From %s in %s", a, z)
254 dest = s:option(DummyValue, "dest", translate("Destination"))
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"))
264 return translatef("To %s, %s in %s", a, p, z)
266 return translatef("To %s in %s", a, z)
270 snat = s:option(DummyValue, "via", translate("SNAT"))
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"))
278 return translatef("Rewrite to source %s, %s", a, p)
280 return translatef("Rewrite to source %s", a or p)
284 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"