3 Iptables parser and query library
4 (c) 2008-2009 Jo-Philipp Wich <jow@openwrt.org>
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 module("luci.sys.iptparser")
26 IptParser = luci.util.class()
28 function IptParser.__init__( self, family )
29 self._family = (tonumber(family) == 6) and 6 or 4
33 if self._family == 4 then
34 self._nulladdr = "0.0.0.0/0"
35 self._tables = { "filter", "nat", "mangle", "raw" }
36 self._command = "iptables -t %s --line-numbers -nxvL"
38 self._nulladdr = "::/0"
39 self._tables = { "filter", "mangle", "raw" }
40 self._command = "ip6tables -t %s --line-numbers -nxvL"
46 -- search criteria as only argument. If args is nil or an empty table then all
47 -- rules will be returned.
49 -- The following keys in the args table are recognized:
51 -- <li> table - Match rules that are located within the given table
52 -- <li> chain - Match rules that are located within the given chain
53 -- <li> target - Match rules with the given target
54 -- <li> protocol - Match rules that match the given protocol, rules with
55 -- protocol "all" are always matched
56 -- <li> source - Match rules with the given source, rules with source
57 -- "0.0.0.0/0" (::/0) are always matched
58 -- <li> destination - Match rules with the given destination, rules with
59 -- destination "0.0.0.0/0" (::/0) are always matched
60 -- <li> inputif - Match rules with the given input interface, rules
61 -- with input interface "*" (=all) are always matched
62 -- <li> outputif - Match rules with the given output interface, rules
63 -- with output interface "*" (=all) are always matched
64 -- <li> flags - Match rules that match the given flags, current
65 -- supported values are "-f" (--fragment)
66 -- and "!f" (! --fragment)
67 -- <li> options - Match rules containing all given options
69 -- The return value is a list of tables representing the matched rules.
70 -- Each rule table contains the following fields:
72 -- <li> index - The index number of the rule
73 -- <li> table - The table where the rule is located, can be one
74 -- of "filter", "nat" or "mangle"
75 -- <li> chain - The chain where the rule is located, e.g. "INPUT"
76 -- or "postrouting_wan"
77 -- <li> target - The rule target, e.g. "REJECT" or "DROP"
78 -- <li> protocol The matching protocols, e.g. "all" or "tcp"
79 -- <li> flags - Special rule options ("--", "-f" or "!f")
80 -- <li> inputif - Input interface of the rule, e.g. "eth0.0"
81 -- or "*" for all interfaces
82 -- <li> outputif - Output interface of the rule,e.g. "eth0.0"
83 -- or "*" for all interfaces
84 -- <li> source - The source ip range, e.g. "0.0.0.0/0" (::/0)
85 -- <li> destination - The destination ip range, e.g. "0.0.0.0/0" (::/0)
86 -- <li> options - A list of specific options of the rule,
87 -- e.g. { "reject-with", "tcp-reset" }
88 -- <li> packets - The number of packets matched by the rule
89 -- <li> bytes - The number of total bytes matched by the rule
93 -- ip = luci.sys.iptparser.IptParser()
94 -- result = ip.find( {
97 -- options={ "reject-with", "tcp-reset" }
100 -- This will match all rules with target "-j REJECT",
101 -- protocol "-p tcp" (or "-p all")
102 -- and the option "--reject-with tcp-reset".
103 function IptParser.find( self, args )
105 local args = args or { }
108 args.source = args.source and self:_parse_addr(args.source)
109 args.destination = args.destination and self:_parse_addr(args.destination)
111 for i, rule in ipairs(self._rules) do
115 if not ( not args.table or args.table:lower() == rule.table ) then
120 if not ( match == true and (
121 not args.chain or args.chain == rule.chain
127 if not ( match == true and (
128 not args.target or args.target == rule.target
134 if not ( match == true and (
135 not args.protocol or rule.protocol == "all" or
136 args.protocol:lower() == rule.protocol
142 if not ( match == true and (
143 not args.source or rule.source == self._nulladdr or
144 self:_parse_addr(rule.source):contains(args.source)
150 if not ( match == true and (
151 not args.destination or rule.destination == self._nulladdr or
152 self:_parse_addr(rule.destination):contains(args.destination)
157 -- match input interface
158 if not ( match == true and (
159 not args.inputif or rule.inputif == "*" or
160 args.inputif == rule.inputif
165 -- match output interface
166 if not ( match == true and (
167 not args.outputif or rule.outputif == "*" or
168 args.outputif == rule.outputif
173 -- match flags (the "opt" column)
174 if not ( match == true and (
175 not args.flags or rule.flags == args.flags
180 -- match specific options
181 if not ( match == true and (
183 self:_match_options( rule.options, args.options )
189 if match == true then
198 -- through external commands.
199 function IptParser.resync( self )
206 function IptParser.tables( self )
211 function IptParser.chains( self, table )
214 for _, r in ipairs(self:find({table=table})) do
215 if not lookup[r.chain] then
216 lookup[r.chain] = true
217 chains[#chains+1] = r.chain
224 -- and "rules". The "rules" field is a table of rule tables.
225 function IptParser.chain( self, table, chain )
226 return self._chains[table:lower()] and self._chains[table:lower()][chain]
230 function IptParser.is_custom_target( self, target )
231 for _, r in ipairs(self._rules) do
232 if r.chain == target then
240 -- [internal] Parse address according to family.
241 function IptParser._parse_addr( self, addr )
242 if self._family == 4 then
243 return luci.ip.IPv4(addr)
245 return luci.ip.IPv6(addr)
249 -- [internal] Parse iptables output from all tables.
250 function IptParser._parse_rules( self )
252 for i, tbl in ipairs(self._tables) do
254 self._chains[tbl] = { }
256 for i, rule in ipairs(luci.util.execl(self._command % tbl)) do
258 if rule:find( "^Chain " ) == 1 then
261 local cname, cpol, cpkt, cbytes = rule:match(
262 "^Chain ([^%s]*) %(policy (%w+) " ..
263 "(%d+) packets, (%d+) bytes%)"
267 cname, crefs = rule:match(
268 "^Chain ([^%s]*) %((%d+) references%)"
273 self._chains[tbl][cname] = {
275 packets = tonumber(cpkt or 0),
276 bytes = tonumber(cbytes or 0),
277 references = tonumber(crefs or 0),
282 if rule:find("%d") == 1 then
284 local rule_parts = luci.util.split( rule, "%s+", nil, true )
285 local rule_details = { }
287 -- cope with rules that have no target assigned
288 if rule:match("^%d+%s+%d+%s+%d+%s%s") then
289 table.insert(rule_parts, 4, nil)
292 -- ip6tables opt column is usually zero-width
293 if self._family == 6 then
294 table.insert(rule_parts, 6, "--")
297 rule_details["table"] = tbl
298 rule_details["chain"] = self._chain
299 rule_details["index"] = tonumber(rule_parts[1])
300 rule_details["packets"] = tonumber(rule_parts[2])
301 rule_details["bytes"] = tonumber(rule_parts[3])
302 rule_details["target"] = rule_parts[4]
303 rule_details["protocol"] = rule_parts[5]
304 rule_details["flags"] = rule_parts[6]
305 rule_details["inputif"] = rule_parts[7]
306 rule_details["outputif"] = rule_parts[8]
307 rule_details["source"] = rule_parts[9]
308 rule_details["destination"] = rule_parts[10]
309 rule_details["options"] = { }
311 for i = 11, #rule_parts do
312 if #rule_parts[i] > 0 then
313 rule_details["options"][i-10] = rule_parts[i]
317 self._rules[#self._rules+1] = rule_details
319 self._chains[tbl][self._chain].rules[
320 #self._chains[tbl][self._chain].rules + 1
331 -- [internal] Return true if optlist1 contains all elements of optlist 2.
332 -- Return false in all other cases.
333 function IptParser._match_options( self, o1, o2 )
335 -- construct a hashtable of first options list to speed up lookups
337 for i, opt in ipairs( o1 ) do oh[opt] = true end
339 -- iterate over second options list
340 -- each string in o2 must be also present in o1
341 -- if o2 contains a string which is not found in o1 then return false
342 for i, opt in ipairs( o2 ) do