3 Iptables parser and query library
4 (c) 2008-2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008-2009 Steven Barth <steven@midlink.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
18 luci.util = require "luci.util"
19 luci.sys = require "luci.sys"
20 luci.ip = require "luci.ip"
22 local tonumber, ipairs, table = tonumber, ipairs, table
24 --- LuCI iptables parser and query library
26 module("luci.sys.iptparser")
28 --- Create a new iptables parser object.
31 -- @return IptParser instance
32 IptParser = luci.util.class()
34 function IptParser.__init__( self, ... )
40 --- Find all firewall rules that match the given criteria. Expects a table with
41 -- search criteria as only argument. If args is nil or an empty table then all
42 -- rules will be returned.
44 -- The following keys in the args table are recognized:
46 -- <li> table - Match rules that are located within the given table
47 -- <li> chain - Match rules that are located within the given chain
48 -- <li> target - Match rules with the given target
49 -- <li> protocol - Match rules that match the given protocol, rules with
50 -- protocol "all" are always matched
51 -- <li> source - Match rules with the given source, rules with source
52 -- "0.0.0.0/0" are always matched
53 -- <li> destination - Match rules with the given destination, rules with
54 -- destination "0.0.0.0/0" are always matched
55 -- <li> inputif - Match rules with the given input interface, rules
56 -- with input interface "*" (=all) are always matched
57 -- <li> outputif - Match rules with the given output interface, rules
58 -- with output interface "*" (=all) are always matched
59 -- <li> flags - Match rules that match the given flags, current
60 -- supported values are "-f" (--fragment)
61 -- and "!f" (! --fragment)
62 -- <li> options - Match rules containing all given options
64 -- The return value is a list of tables representing the matched rules.
65 -- Each rule table contains the following fields:
67 -- <li> index - The index number of the rule
68 -- <li> table - The table where the rule is located, can be one
69 -- of "filter", "nat" or "mangle"
70 -- <li> chain - The chain where the rule is located, e.g. "INPUT"
71 -- or "postrouting_wan"
72 -- <li> target - The rule target, e.g. "REJECT" or "DROP"
73 -- <li> protocol The matching protocols, e.g. "all" or "tcp"
74 -- <li> flags - Special rule options ("--", "-f" or "!f")
75 -- <li> inputif - Input interface of the rule, e.g. "eth0.0"
76 -- or "*" for all interfaces
77 -- <li> outputif - Output interface of the rule,e.g. "eth0.0"
78 -- or "*" for all interfaces
79 -- <li> source - The source ip range, e.g. "0.0.0.0/0"
80 -- <li> destination - The destination ip range, e.g. "0.0.0.0/0"
81 -- <li> options - A list of specific options of the rule,
82 -- e.g. { "reject-with", "tcp-reset" }
83 -- <li> packets - The number of packets matched by the rule
84 -- <li> bytes - The number of total bytes matched by the rule
88 -- ip = luci.sys.iptparser.IptParser()
89 -- result = ip.find( {
92 -- options={ "reject-with", "tcp-reset" }
95 -- This will match all rules with target "-j REJECT",
96 -- protocol "-p tcp" (or "-p all")
97 -- and the option "--reject-with tcp-reset".
98 -- @params args Table containing the search arguments (optional)
99 -- @return Table of matching rule tables
100 function IptParser.find( self, args )
102 local args = args or { }
105 args.source = args.source and luci.ip.IPv4(args.source)
106 args.destination = args.destination and luci.ip.IPv4(args.destination)
108 for i, rule in ipairs(self._rules) do
112 if not ( not args.table or args.table:lower() == rule.table ) then
117 if not ( match == true and (
118 not args.chain or args.chain == rule.chain
124 if not ( match == true and (
125 not args.target or args.target == rule.target
131 if not ( match == true and (
132 not args.protocol or rule.protocol == "all" or
133 args.protocol:lower() == rule.protocol
139 if not ( match == true and (
140 not args.source or rule.source == "0.0.0.0/0" or
141 luci.ip.IPv4(rule.source):contains(args.source)
147 if not ( match == true and (
148 not args.destination or rule.destination == "0.0.0.0/0" or
149 luci.ip.IPv4(rule.destination):contains(args.destination)
154 -- match input interface
155 if not ( match == true and (
156 not args.inputif or rule.inputif == "*" or
157 args.inputif == rule.inputif
162 -- match output interface
163 if not ( match == true and (
164 not args.outputif or rule.outputif == "*" or
165 args.outputif == rule.outputif
170 -- match flags (the "opt" column)
171 if not ( match == true and (
172 not args.flags or rule.flags == args.flags
177 -- match specific options
178 if not ( match == true and (
180 self:_match_options( rule.options, args.options )
186 if match == true then
195 --- Rebuild the internal lookup table, for example when rules have changed
196 -- through external commands.
198 function IptParser.resync( self )
205 --- Find the names of all chains within the given table name.
206 -- @param table String containing the table name
207 -- @return Table of chain names in the order they occur.
208 function IptParser.chains( self, table )
211 for _, r in ipairs(self:find({table=table})) do
212 if not lookup[r.chain] then
213 lookup[r.chain] = true
214 chains[#chains+1] = r.chain
221 --- Return the given firewall chain within the given table name.
222 -- @param table String containing the table name
223 -- @param chain String containing the chain name
224 -- @return Table containing the fields "policy", "packets", "bytes"
225 -- and "rules". The "rules" field is a table of rule tables.
226 function IptParser.chain( self, table, chain )
227 return self._chains[table:lower()] and self._chains[table:lower()][chain]
231 --- Test whether the given target points to a custom chain.
232 -- @param target String containing the target action
233 -- @return Boolean indicating whether target is a custom chain.
234 function IptParser.is_custom_target( self, target )
235 for _, r in ipairs(self._rules) do
236 if r.chain == target then
244 -- [internal] Parse iptables output from all tables.
245 function IptParser._parse_rules( self )
247 for i, tbl in ipairs({ "filter", "nat", "mangle" }) do
249 self._chains[tbl] = { }
251 for i, rule in ipairs(luci.util.execl("iptables -t " .. tbl .. " --line-numbers -nxvL")) do
253 if rule:find( "Chain " ) == 1 then
256 local cname, cpol, cpkt, cbytes = rule:match(
257 "Chain ([^%s]*) %(policy (%w+) " ..
258 "(%d+) packets, (%d+) bytes%)"
262 cname, crefs = rule:match(
263 "Chain ([^%s]*) %((%d+) references%)"
268 self._chains[tbl][cname] = {
270 packets = tonumber(cpkt or 0),
271 bytes = tonumber(cbytes or 0),
272 references = tonumber(crefs or 0),
277 if rule:find("%d") == 1 then
279 local rule_parts = luci.util.split( rule, "%s+", nil, true )
280 local rule_details = { }
282 -- cope with rules that have no target assigned
283 if rule:match("^%d+%s+%d+%s+%d+%s%s") then
284 table.insert(rule_parts, 4, nil)
287 rule_details["table"] = tbl
288 rule_details["chain"] = self._chain
289 rule_details["index"] = tonumber(rule_parts[1])
290 rule_details["packets"] = tonumber(rule_parts[2])
291 rule_details["bytes"] = tonumber(rule_parts[3])
292 rule_details["target"] = rule_parts[4]
293 rule_details["protocol"] = rule_parts[5]
294 rule_details["flags"] = rule_parts[6]
295 rule_details["inputif"] = rule_parts[7]
296 rule_details["outputif"] = rule_parts[8]
297 rule_details["source"] = rule_parts[9]
298 rule_details["destination"] = rule_parts[10]
299 rule_details["options"] = { }
301 for i = 11, #rule_parts - 1 do
302 rule_details["options"][i-10] = rule_parts[i]
305 self._rules[#self._rules+1] = rule_details
307 self._chains[tbl][self._chain].rules[
308 #self._chains[tbl][self._chain].rules + 1
319 -- [internal] Return true if optlist1 contains all elements of optlist 2.
320 -- Return false in all other cases.
321 function IptParser._match_options( self, o1, o2 )
323 -- construct a hashtable of first options list to speed up lookups
325 for i, opt in ipairs( o1 ) do oh[opt] = true end
327 -- iterate over second options list
328 -- each string in o2 must be also present in o1
329 -- if o2 contains a string which is not found in o1 then return false
330 for i, opt in ipairs( o2 ) do