1 -- Copyright 2011-2012 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.tools.firewall", package.seeall)
6 local ut = require "luci.util"
7 local ip = require "luci.ip"
8 local nx = require "nixio"
10 local translate, translatef = luci.i18n.translate, luci.i18n.translatef
13 return tostring(translate(...))
17 if type(x) == "string" then
18 local v, neg = x:gsub("^ *! *", "")
20 return v, "%s " % _("not")
31 local l = { _("MAC"), " " }
32 for m in ut.imatch(x) do
34 l[#l+1] = "<var>%s%s</var>" %{ n, m }
42 return table.concat(l, "")
47 function fmt_port(x, d)
50 local l = { _("port"), " " }
51 for p in ut.imatch(x) do
53 local a, b = p:match("(%d+)%D+(%d+)")
56 l[#l+1] = "<var>%s%d-%d</var>" %{ n, a, b }
58 l[#l+1] = "<var>%s%d</var>" %{ n, p }
67 return table.concat(l, "")
70 return d and "<var>%s</var>" % d
75 local l = { _("IP"), " " }
77 for v in ut.imatch(x) do
79 a, m = v:match("(%S+)/(%d+%.%S+)")
81 a = a:match(":") and ip.IPv6(a, m) or ip.IPv4(a, m)
82 if a and (a:is6() and a:prefix() < 128 or a:prefix() < 32) then
84 l[#l+1] = "<var title='%s - %s'>%s%s</var>" %{
90 l[#l+1] = "<var>%s%s</var>" %{
102 return table.concat(l, "")
105 return d and "<var>%s</var>" % d
108 function fmt_zone(x, d)
110 return "<var>%s</var>" % _("any zone")
111 elseif x and #x > 0 then
112 return "<var>%s</var>" % x
114 return "<var>%s</var>" % d
118 function fmt_icmp_type(x)
121 local l = { _("type"), " " }
122 for v in ut.imatch(x) do
124 l[#l+1] = "<var>%s%s</var>" %{ n, v }
132 return table.concat(l, "")
137 function fmt_proto(x, icmp_types)
141 local t = fmt_icmp_type(icmp_types)
142 for v in ut.imatch(x) do
144 if v == "tcpudp" then
149 elseif v ~= "all" then
150 local p = nx.getproto(v)
153 if (p.proto == 1 or p.proto == 58) and t then
154 l[#l+1] = translatef(
156 n, p.aliases[1] or p.name, t
161 p.aliases[1] or p.name
170 return table.concat(l, "")
175 function fmt_limit(limit, burst)
176 burst = tonumber(burst)
177 if limit and #limit > 0 then
178 local l, u = limit:match("(%d+)/(%w+)")
179 l = tonumber(l or limit)
182 if u:match("^s") then
184 elseif u:match("^m") then
186 elseif u:match("^h") then
188 elseif u:match("^d") then
191 if burst and burst > 0 then
192 return translatef("<var>%d</var> pkts. per <var>%s</var>, \
193 burst <var>%d</var> pkts.", l, u, burst)
195 return translatef("<var>%d</var> pkts. per <var>%s</var>", l, u)
201 function fmt_target(x, src, dest)
202 if not src or #src == 0 then
203 if x == "ACCEPT" then
204 return _("Accept output")
205 elseif x == "REJECT" then
206 return _("Refuse output")
207 elseif x == "NOTRACK" then
208 return _("Do not track output")
209 else --if x == "DROP" then
210 return _("Discard output")
212 elseif dest and #dest > 0 then
213 if x == "ACCEPT" then
214 return _("Accept forward")
215 elseif x == "REJECT" then
216 return _("Refuse forward")
217 elseif x == "NOTRACK" then
218 return _("Do not track forward")
219 else --if x == "DROP" then
220 return _("Discard forward")
223 if x == "ACCEPT" then
224 return _("Accept input")
225 elseif x == "REJECT" then
226 return _("Refuse input")
227 elseif x == "NOTRACK" then
228 return _("Do not track input")
229 else --if x == "DROP" then
230 return _("Discard input")
236 function opt_enabled(s, t, ...)
237 if t == luci.cbi.Button then
238 local o = s:option(t, "__enabled")
239 function o.render(self, section)
240 if self.map:get(section, "enabled") ~= "0" then
241 self.title = _("Rule is enabled")
242 self.inputtitle = _("Disable")
243 self.inputstyle = "reset"
245 self.title = _("Rule is disabled")
246 self.inputtitle = _("Enable")
247 self.inputstyle = "apply"
249 t.render(self, section)
251 function o.write(self, section, value)
252 if self.map:get(section, "enabled") ~= "0" then
253 self.map:set(section, "enabled", "0")
255 self.map:del(section, "enabled")
260 local o = s:option(t, "enabled", ...)
266 function opt_name(s, t, ...)
267 local o = s:option(t, "name", ...)
269 function o.cfgvalue(self, section)
270 return self.map:get(section, "name") or
271 self.map:get(section, "_name") or "-"
274 function o.write(self, section, value)
276 self.map:set(section, "name", value)
277 self.map:del(section, "_name")
283 function o.remove(self, section)
284 self.map:del(section, "name")
285 self.map:del(section, "_name")