applications/luci-firewall: make rules and redirections sortable
[project/luci.git] / applications / luci-firewall / luasrc / model / cbi / luci_fw / zones.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 nw = require "luci.model.network"
16 local fw = require "luci.model.firewall"
17 local ds = require "luci.dispatcher"
18
19 local has_v2 = nixio.fs.access("/lib/firewall/fw.sh")
20
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."))
23
24 fw.init(m.uci)
25 nw.init(m.uci)
26
27 s = m:section(TypedSection, "defaults")
28 s.anonymous = true
29 s.addremove = false
30
31 s:tab("general", translate("General Settings"))
32 s:tab("custom", translate("Custom Rules"))
33
34
35 s:taboption("general", Flag, "syn_flood", translate("Enable SYN-flood protection"))
36
37 local di = s:taboption("general", Flag, "drop_invalid", translate("Drop invalid packets"))
38 di.rmempty = false
39 function di.cfgvalue(...)
40         return AbstractValue.cfgvalue(...) or "1"
41 end
42
43 p = {}
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"))
47
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"))
52 end
53
54 custom = s:taboption("custom", Value, "_custom",
55         translate("Custom Rules (/etc/firewall.user)"))
56
57 custom.template = "cbi/tvalue"
58 custom.rows = 20
59
60 function custom.cfgvalue(self, section)
61         return nixio.fs.readfile("/etc/firewall.user")
62 end
63
64 function custom.write(self, section, value)
65         value = value:gsub("\r\n?", "\n")
66         nixio.fs.writefile("/etc/firewall.user", value)
67 end
68
69
70 s = m:section(TypedSection, "zone", translate("Zones"))
71 s.template = "cbi/tblsection"
72 s.anonymous = true
73 s.addremove = true
74 s.extedit   = ds.build_url("admin", "network", "firewall", "zones", "%s")
75
76 function s.create(self)
77         local z = fw:new_zone()
78         if z then
79                 luci.http.redirect(
80                         ds.build_url("admin", "network", "firewall", "zones", z.sid)
81                 )
82         end
83 end
84
85 info = s:option(DummyValue, "_info", translate("Zone ⇒ Forwardings"))
86 info.template = "cbi/firewall_zoneforwards"
87 function info.cfgvalue(self, section)
88         return self.map:get(section, "name")
89 end
90
91 p = {}
92 p[1] = s:option(ListValue, "input", translate("Input"))
93 p[2] = s:option(ListValue, "output", translate("Output"))
94 p[3] = s:option(ListValue, "forward", translate("Forward"))
95
96 for i, v in ipairs(p) do
97         v:value("REJECT", translate("reject"))
98         v:value("DROP", translate("drop"))
99         v:value("ACCEPT", translate("accept"))
100 end
101
102 s:option(Flag, "masq", translate("Masquerading"))
103 s:option(Flag, "mtu_fix", translate("MSS clamping"))
104
105
106 local created = nil
107
108 --
109 -- Redirects
110 --
111
112 s = m:section(TypedSection, "redirect", translate("Redirections"))
113 s.template  = "cbi/tblsection"
114 s.addremove = true
115 s.anonymous = true
116 s.sortable  = true
117 s.extedit   = ds.build_url("admin", "network", "firewall", "redirect", "%s")
118
119 function s.create(self, section)
120         created = TypedSection.create(self, section)
121 end
122
123 function s.parse(self, ...)
124         TypedSection.parse(self, ...)
125         if created then
126                 m.uci:save("firewall")
127                 luci.http.redirect(ds.build_url(
128                         "admin", "network", "firewall", "redirect", created
129                 ))
130         end
131 end
132
133 name = s:option(DummyValue, "_name", translate("Name"))
134 function name.cfgvalue(self, s)
135         return self.map:get(s, "_name") or "-"
136 end
137
138 proto = s:option(DummyValue, "proto", translate("Protocol"))
139 function proto.cfgvalue(self, s)
140         local p = self.map:get(s, "proto")
141         if not p or p == "tcpudp" then
142                 return "TCP+UDP"
143         else
144                 return p:upper()
145         end
146 end
147
148 src = s:option(DummyValue, "src", translate("Source"))
149 function src.cfgvalue(self, s)
150         local rv = "%s:%s:%s" % {
151                 self.map:get(s, "src") or "*",
152                 self.map:get(s, "src_ip") or "0.0.0.0/0",
153                 self.map:get(s, "src_port") or "*"
154         }
155
156         local mac = self.map:get(s, "src_mac")
157         if mac then
158                 rv = rv .. ", MAC " .. mac
159         end
160
161         return rv
162 end
163
164 via = s:option(DummyValue, "via", translate("Via"))
165 function via.cfgvalue(self, s)
166         return "%s:%s:%s" % {
167                 translate("Device"),
168                 self.map:get(s, "src_dip") or "0.0.0.0/0",
169                 self.map:get(s, "src_dport") or "*"
170         }
171 end
172
173 dest = s:option(DummyValue, "dest", translate("Destination"))
174 function dest.cfgvalue(self, s)
175         return "%s:%s:%s" % {
176                 self.map:get(s, "dest") or "*",
177                 self.map:get(s, "dest_ip") or "0.0.0.0/0",
178                 self.map:get(s, "dest_port") or "*"
179         }
180 end
181
182 target = s:option(DummyValue, "target", translate("Action"))
183 function target.cfgvalue(self, s)
184         return self.map:get(s, "target") or "DNAT"
185 end
186
187
188 --
189 -- Rules
190 --
191
192 s = m:section(TypedSection, "rule", translate("Rules"))
193 s.addremove = true
194 s.anonymous = true
195 s.sortable  = true
196 s.template = "cbi/tblsection"
197 s.extedit   = ds.build_url("admin", "network", "firewall", "rule", "%s")
198 s.defaults.target = "ACCEPT"
199
200 function s.create(self, section)
201         local created = TypedSection.create(self, section)
202         m.uci:save("firewall")
203         luci.http.redirect(ds.build_url(
204                 "admin", "network", "firewall", "rule", created
205         ))
206         return
207 end
208
209 name = s:option(DummyValue, "_name", translate("Name"))
210 function name.cfgvalue(self, s)
211         return self.map:get(s, "_name") or "-"
212 end
213
214 if has_v2 then
215         family = s:option(DummyValue, "family", translate("Family"))
216         function family.cfgvalue(self, s)
217                 local f = self.map:get(s, "family")
218                 if f and f:match("4") then
219                         return translate("IPv4 only")
220                 elseif f and f:match("6") then
221                         return translate("IPv6 only")
222                 else
223                         return translate("IPv4 and IPv6")
224                 end
225         end
226 end
227
228 proto = s:option(DummyValue, "proto", translate("Protocol"))
229 function proto.cfgvalue(self, s)
230         local p = self.map:get(s, "proto")
231         local t = self.map:get(s, "icmp_type")
232         if p == "icmp" and t then
233                 return "ICMP (%s)" % t
234         elseif p == "tcpudp" or not p then
235                 return "TCP+UDP"
236         else
237                 return p:upper()
238         end
239 end
240
241 src = s:option(DummyValue, "src", translate("Source"))
242 function src.cfgvalue(self, s)
243         local rv = "%s:%s:%s" % {
244                 self.map:get(s, "src") or "*",
245                 self.map:get(s, "src_ip") or "0.0.0.0/0",
246                 self.map:get(s, "src_port") or "*"
247         }
248
249         local mac = self.map:get(s, "src_mac")
250         if mac then
251                 rv = rv .. ", MAC " .. mac
252         end
253
254         return rv
255 end
256
257 dest = s:option(DummyValue, "dest", translate("Destination"))
258 function dest.cfgvalue(self, s)
259         return "%s:%s:%s" % {
260                 self.map:get(s, "dest") or translate("Device"),
261                 self.map:get(s, "dest_ip") or "0.0.0.0/0",
262                 self.map:get(s, "dest_port") or "*"
263         }
264 end
265
266
267 s:option(DummyValue, "target", translate("Action"))
268
269 return m