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 nw = require "luci.model.network"
16 local fw = require "luci.model.firewall"
17 local ds = require "luci.dispatcher"
19 local has_v2 = nixio.fs.access("/lib/firewall/fw.sh")
21 require("luci.tools.webadmin")
22 m = Map("firewall", translate("Firewall"), translate("The firewall creates zones over your network interfaces to control network traffic flow."))
27 s = m:section(TypedSection, "defaults")
31 s:tab("general", translate("General Settings"))
32 s:tab("custom", translate("Custom Rules"))
35 s:taboption("general", Flag, "syn_flood", translate("Enable SYN-flood protection"))
37 local di = s:taboption("general", Flag, "drop_invalid", translate("Drop invalid packets"))
39 function di.cfgvalue(...)
40 return AbstractValue.cfgvalue(...) or "1"
44 p[1] = s:taboption("general", ListValue, "input", translate("Input"))
45 p[2] = s:taboption("general", ListValue, "output", translate("Output"))
46 p[3] = s:taboption("general", ListValue, "forward", translate("Forward"))
48 for i, v in ipairs(p) do
49 v:value("REJECT", translate("reject"))
50 v:value("DROP", translate("drop"))
51 v:value("ACCEPT", translate("accept"))
54 custom = s:taboption("custom", Value, "_custom",
55 translate("Custom Rules (/etc/firewall.user)"))
57 custom.template = "cbi/tvalue"
60 function custom.cfgvalue(self, section)
61 return nixio.fs.readfile("/etc/firewall.user")
64 function custom.write(self, section, value)
65 nixio.fs.writefile("/etc/firewall.user", value)
69 s = m:section(TypedSection, "zone", translate("Zones"))
70 s.template = "cbi/tblsection"
73 s.extedit = ds.build_url("admin", "network", "firewall", "zones", "%s")
75 function s.create(self)
76 local z = fw:new_zone()
79 ds.build_url("admin", "network", "firewall", "zones", z.sid)
84 info = s:option(DummyValue, "_info", translate("Zone ⇒ Forwardings"))
85 info.template = "cbi/firewall_zoneforwards"
86 function info.cfgvalue(self, section)
87 return self.map:get(section, "name")
91 p[1] = s:option(ListValue, "input", translate("Input"))
92 p[2] = s:option(ListValue, "output", translate("Output"))
93 p[3] = s:option(ListValue, "forward", translate("Forward"))
95 for i, v in ipairs(p) do
96 v:value("REJECT", translate("reject"))
97 v:value("DROP", translate("drop"))
98 v:value("ACCEPT", translate("accept"))
101 s:option(Flag, "masq", translate("Masquerading"))
102 s:option(Flag, "mtu_fix", translate("MSS clamping"))
111 s = m:section(TypedSection, "redirect", translate("Redirections"))
112 s.template = "cbi/tblsection"
115 s.extedit = ds.build_url("admin", "network", "firewall", "redirect", "%s")
117 function s.create(self, section)
118 created = TypedSection.create(self, section)
121 function s.parse(self, ...)
122 TypedSection.parse(self, ...)
124 m.uci:save("firewall")
125 luci.http.redirect(ds.build_url(
126 "admin", "network", "firewall", "redirect", created
131 name = s:option(DummyValue, "_name", translate("Name"))
132 function name.cfgvalue(self, s)
133 return self.map:get(s, "_name") or "-"
136 proto = s:option(DummyValue, "proto", translate("Protocol"))
137 function proto.cfgvalue(self, s)
138 local p = self.map:get(s, "proto")
139 if not p or p == "tcpudp" then
146 src = s:option(DummyValue, "src", translate("Source"))
147 function src.cfgvalue(self, s)
148 local rv = "%s:%s:%s" % {
149 self.map:get(s, "src") or "*",
150 self.map:get(s, "src_ip") or "0.0.0.0/0",
151 self.map:get(s, "src_port") or "*"
154 local mac = self.map:get(s, "src_mac")
156 rv = rv .. ", MAC " .. mac
162 via = s:option(DummyValue, "via", translate("Via"))
163 function via.cfgvalue(self, s)
164 return "%s:%s:%s" % {
166 self.map:get(s, "src_dip") or "0.0.0.0/0",
167 self.map:get(s, "src_dport") or "*"
171 dest = s:option(DummyValue, "dest", translate("Destination"))
172 function dest.cfgvalue(self, s)
173 return "%s:%s:%s" % {
174 self.map:get(s, "dest") or "*",
175 self.map:get(s, "dest_ip") or "0.0.0.0/0",
176 self.map:get(s, "dest_port") or "*"
180 target = s:option(DummyValue, "target", translate("Action"))
181 function target.cfgvalue(self, s)
182 return self.map:get(s, "target") or "DNAT"
190 s = m:section(TypedSection, "rule", translate("Rules"))
193 s.template = "cbi/tblsection"
194 s.extedit = ds.build_url("admin", "network", "firewall", "rule", "%s")
195 s.defaults.target = "ACCEPT"
197 function s.create(self, section)
198 local created = TypedSection.create(self, section)
199 m.uci:save("firewall")
200 luci.http.redirect(ds.build_url(
201 "admin", "network", "firewall", "rule", created
206 name = s:option(DummyValue, "_name", translate("Name"))
207 function name.cfgvalue(self, s)
208 return self.map:get(s, "_name") or "-"
212 family = s:option(DummyValue, "family", translate("Family"))
213 function family.cfgvalue(self, s)
214 local f = self.map:get(s, "family")
215 if f and f:match("4") then
216 return translate("IPv4 only")
217 elseif f and f:match("6") then
218 return translate("IPv6 only")
220 return translate("IPv4 and IPv6")
225 proto = s:option(DummyValue, "proto", translate("Protocol"))
226 function proto.cfgvalue(self, s)
227 local p = self.map:get(s, "proto")
228 local t = self.map:get(s, "icmp_type")
229 if p == "icmp" and t then
230 return "ICMP (%s)" % t
231 elseif p == "tcpudp" or not p then
238 src = s:option(DummyValue, "src", translate("Source"))
239 function src.cfgvalue(self, s)
240 local rv = "%s:%s:%s" % {
241 self.map:get(s, "src") or "*",
242 self.map:get(s, "src_ip") or "0.0.0.0/0",
243 self.map:get(s, "src_port") or "*"
246 local mac = self.map:get(s, "src_mac")
248 rv = rv .. ", MAC " .. mac
254 dest = s:option(DummyValue, "dest", translate("Destination"))
255 function dest.cfgvalue(self, s)
256 return "%s:%s:%s" % {
257 self.map:get(s, "dest") or translate("Device"),
258 self.map:get(s, "dest_ip") or "0.0.0.0/0",
259 self.map:get(s, "dest_port") or "*"
264 s:option(DummyValue, "target", translate("Action"))