2b81e0ee3893e7eca71022c9bc7792caecd32ffe
[project/luci.git] / modules / luci-base / luasrc / sys / iptparser.lua
1 --[[
2
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>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17 local luci = {}
18 luci.util = require "luci.util"
19 luci.sys = require "luci.sys"
20 luci.ip = require "luci.ip"
21
22 local tonumber, ipairs, table = tonumber, ipairs, table
23
24 module("luci.sys.iptparser")
25
26 IptParser = luci.util.class()
27
28 function IptParser.__init__( self, family )
29 self._family = (tonumber(family) == 6) and 6 or 4
30 self._rules = { }
31 self._chains = { }
32
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"
37 else
38 self._nulladdr = "::/0"
39 self._tables = { "filter", "mangle", "raw" }
40 self._command = "ip6tables -t %s --line-numbers -nxvL"
41 end
42
43 self:_parse_rules()
44 end
45
46 -- search criteria as only argument. If args is nil or an empty table then all
47 -- rules will be returned.
48 --
49 -- The following keys in the args table are recognized:
50 -- <ul>
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
68 -- </ul>
69 -- The return value is a list of tables representing the matched rules.
70 -- Each rule table contains the following fields:
71 -- <ul>
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
90 -- </ul>
91 -- Example:
92 -- <pre>
93 -- ip = luci.sys.iptparser.IptParser()
94 -- result = ip.find( {
95 -- target="REJECT",
96 -- protocol="tcp",
97 -- options={ "reject-with", "tcp-reset" }
98 -- } )
99 -- </pre>
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 )
104
105 local args = args or { }
106 local rv = { }
107
108 args.source = args.source and self:_parse_addr(args.source)
109 args.destination = args.destination and self:_parse_addr(args.destination)
110
111 for i, rule in ipairs(self._rules) do
112 local match = true
113
114 -- match table
115 if not ( not args.table or args.table:lower() == rule.table ) then
116 match = false
117 end
118
119 -- match chain
120 if not ( match == true and (
121 not args.chain or args.chain == rule.chain
122 ) ) then
123 match = false
124 end
125
126 -- match target
127 if not ( match == true and (
128 not args.target or args.target == rule.target
129 ) ) then
130 match = false
131 end
132
133 -- match protocol
134 if not ( match == true and (
135 not args.protocol or rule.protocol == "all" or
136 args.protocol:lower() == rule.protocol
137 ) ) then
138 match = false
139 end
140
141 -- match source
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)
145 ) ) then
146 match = false
147 end
148
149 -- match destination
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)
153 ) ) then
154 match = false
155 end
156
157 -- match input interface
158 if not ( match == true and (
159 not args.inputif or rule.inputif == "*" or
160 args.inputif == rule.inputif
161 ) ) then
162 match = false
163 end
164
165 -- match output interface
166 if not ( match == true and (
167 not args.outputif or rule.outputif == "*" or
168 args.outputif == rule.outputif
169 ) ) then
170 match = false
171 end
172
173 -- match flags (the "opt" column)
174 if not ( match == true and (
175 not args.flags or rule.flags == args.flags
176 ) ) then
177 match = false
178 end
179
180 -- match specific options
181 if not ( match == true and (
182 not args.options or
183 self:_match_options( rule.options, args.options )
184 ) ) then
185 match = false
186 end
187
188 -- insert match
189 if match == true then
190 rv[#rv+1] = rule
191 end
192 end
193
194 return rv
195 end
196
197
198 -- through external commands.
199 function IptParser.resync( self )
200 self._rules = { }
201 self._chain = nil
202 self:_parse_rules()
203 end
204
205
206 function IptParser.tables( self )
207 return self._tables
208 end
209
210
211 function IptParser.chains( self, table )
212 local lookup = { }
213 local chains = { }
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
218 end
219 end
220 return chains
221 end
222
223
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]
227 end
228
229
230 function IptParser.is_custom_target( self, target )
231 for _, r in ipairs(self._rules) do
232 if r.chain == target then
233 return true
234 end
235 end
236 return false
237 end
238
239
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)
244 else
245 return luci.ip.IPv6(addr)
246 end
247 end
248
249 -- [internal] Parse iptables output from all tables.
250 function IptParser._parse_rules( self )
251
252 for i, tbl in ipairs(self._tables) do
253
254 self._chains[tbl] = { }
255
256 for i, rule in ipairs(luci.util.execl(self._command % tbl)) do
257
258 if rule:find( "^Chain " ) == 1 then
259
260 local crefs
261 local cname, cpol, cpkt, cbytes = rule:match(
262 "^Chain ([^%s]*) %(policy (%w+) " ..
263 "(%d+) packets, (%d+) bytes%)"
264 )
265
266 if not cname then
267 cname, crefs = rule:match(
268 "^Chain ([^%s]*) %((%d+) references%)"
269 )
270 end
271
272 self._chain = cname
273 self._chains[tbl][cname] = {
274 policy = cpol,
275 packets = tonumber(cpkt or 0),
276 bytes = tonumber(cbytes or 0),
277 references = tonumber(crefs or 0),
278 rules = { }
279 }
280
281 else
282 if rule:find("%d") == 1 then
283
284 local rule_parts = luci.util.split( rule, "%s+", nil, true )
285 local rule_details = { }
286
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)
290 end
291
292 -- ip6tables opt column is usually zero-width
293 if self._family == 6 then
294 table.insert(rule_parts, 6, "--")
295 end
296
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"] = { }
310
311 for i = 11, #rule_parts do
312 if #rule_parts[i] > 0 then
313 rule_details["options"][i-10] = rule_parts[i]
314 end
315 end
316
317 self._rules[#self._rules+1] = rule_details
318
319 self._chains[tbl][self._chain].rules[
320 #self._chains[tbl][self._chain].rules + 1
321 ] = rule_details
322 end
323 end
324 end
325 end
326
327 self._chain = nil
328 end
329
330
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 )
334
335 -- construct a hashtable of first options list to speed up lookups
336 local oh = { }
337 for i, opt in ipairs( o1 ) do oh[opt] = true end
338
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
343 if not oh[opt] then
344 return false
345 end
346 end
347
348 return true
349 end