iptparser.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. --[[
  2. Iptables parser and query library
  3. (c) 2008-2009 Jo-Philipp Wich <jow@openwrt.org>
  4. (c) 2008-2009 Steven Barth <steven@midlink.org>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. $Id$
  10. ]]--
  11. local luci = {}
  12. luci.util = require "luci.util"
  13. luci.sys = require "luci.sys"
  14. luci.ip = require "luci.ip"
  15. local tonumber, ipairs, table = tonumber, ipairs, table
  16. module("luci.sys.iptparser")
  17. IptParser = luci.util.class()
  18. function IptParser.__init__( self, family )
  19. self._family = (tonumber(family) == 6) and 6 or 4
  20. self._rules = { }
  21. self._chains = { }
  22. if self._family == 4 then
  23. self._nulladdr = "0.0.0.0/0"
  24. self._tables = { "filter", "nat", "mangle", "raw" }
  25. self._command = "iptables -t %s --line-numbers -nxvL"
  26. else
  27. self._nulladdr = "::/0"
  28. self._tables = { "filter", "mangle", "raw" }
  29. self._command = "ip6tables -t %s --line-numbers -nxvL"
  30. end
  31. self:_parse_rules()
  32. end
  33. -- search criteria as only argument. If args is nil or an empty table then all
  34. -- rules will be returned.
  35. --
  36. -- The following keys in the args table are recognized:
  37. -- <ul>
  38. -- <li> table - Match rules that are located within the given table
  39. -- <li> chain - Match rules that are located within the given chain
  40. -- <li> target - Match rules with the given target
  41. -- <li> protocol - Match rules that match the given protocol, rules with
  42. -- protocol "all" are always matched
  43. -- <li> source - Match rules with the given source, rules with source
  44. -- "0.0.0.0/0" (::/0) are always matched
  45. -- <li> destination - Match rules with the given destination, rules with
  46. -- destination "0.0.0.0/0" (::/0) are always matched
  47. -- <li> inputif - Match rules with the given input interface, rules
  48. -- with input interface "*" (=all) are always matched
  49. -- <li> outputif - Match rules with the given output interface, rules
  50. -- with output interface "*" (=all) are always matched
  51. -- <li> flags - Match rules that match the given flags, current
  52. -- supported values are "-f" (--fragment)
  53. -- and "!f" (! --fragment)
  54. -- <li> options - Match rules containing all given options
  55. -- </ul>
  56. -- The return value is a list of tables representing the matched rules.
  57. -- Each rule table contains the following fields:
  58. -- <ul>
  59. -- <li> index - The index number of the rule
  60. -- <li> table - The table where the rule is located, can be one
  61. -- of "filter", "nat" or "mangle"
  62. -- <li> chain - The chain where the rule is located, e.g. "INPUT"
  63. -- or "postrouting_wan"
  64. -- <li> target - The rule target, e.g. "REJECT" or "DROP"
  65. -- <li> protocol The matching protocols, e.g. "all" or "tcp"
  66. -- <li> flags - Special rule options ("--", "-f" or "!f")
  67. -- <li> inputif - Input interface of the rule, e.g. "eth0.0"
  68. -- or "*" for all interfaces
  69. -- <li> outputif - Output interface of the rule,e.g. "eth0.0"
  70. -- or "*" for all interfaces
  71. -- <li> source - The source ip range, e.g. "0.0.0.0/0" (::/0)
  72. -- <li> destination - The destination ip range, e.g. "0.0.0.0/0" (::/0)
  73. -- <li> options - A list of specific options of the rule,
  74. -- e.g. { "reject-with", "tcp-reset" }
  75. -- <li> packets - The number of packets matched by the rule
  76. -- <li> bytes - The number of total bytes matched by the rule
  77. -- </ul>
  78. -- Example:
  79. -- <pre>
  80. -- ip = luci.sys.iptparser.IptParser()
  81. -- result = ip.find( {
  82. -- target="REJECT",
  83. -- protocol="tcp",
  84. -- options={ "reject-with", "tcp-reset" }
  85. -- } )
  86. -- </pre>
  87. -- This will match all rules with target "-j REJECT",
  88. -- protocol "-p tcp" (or "-p all")
  89. -- and the option "--reject-with tcp-reset".
  90. function IptParser.find( self, args )
  91. local args = args or { }
  92. local rv = { }
  93. args.source = args.source and self:_parse_addr(args.source)
  94. args.destination = args.destination and self:_parse_addr(args.destination)
  95. for i, rule in ipairs(self._rules) do
  96. local match = true
  97. -- match table
  98. if not ( not args.table or args.table:lower() == rule.table ) then
  99. match = false
  100. end
  101. -- match chain
  102. if not ( match == true and (
  103. not args.chain or args.chain == rule.chain
  104. ) ) then
  105. match = false
  106. end
  107. -- match target
  108. if not ( match == true and (
  109. not args.target or args.target == rule.target
  110. ) ) then
  111. match = false
  112. end
  113. -- match protocol
  114. if not ( match == true and (
  115. not args.protocol or rule.protocol == "all" or
  116. args.protocol:lower() == rule.protocol
  117. ) ) then
  118. match = false
  119. end
  120. -- match source
  121. if not ( match == true and (
  122. not args.source or rule.source == self._nulladdr or
  123. self:_parse_addr(rule.source):contains(args.source)
  124. ) ) then
  125. match = false
  126. end
  127. -- match destination
  128. if not ( match == true and (
  129. not args.destination or rule.destination == self._nulladdr or
  130. self:_parse_addr(rule.destination):contains(args.destination)
  131. ) ) then
  132. match = false
  133. end
  134. -- match input interface
  135. if not ( match == true and (
  136. not args.inputif or rule.inputif == "*" or
  137. args.inputif == rule.inputif
  138. ) ) then
  139. match = false
  140. end
  141. -- match output interface
  142. if not ( match == true and (
  143. not args.outputif or rule.outputif == "*" or
  144. args.outputif == rule.outputif
  145. ) ) then
  146. match = false
  147. end
  148. -- match flags (the "opt" column)
  149. if not ( match == true and (
  150. not args.flags or rule.flags == args.flags
  151. ) ) then
  152. match = false
  153. end
  154. -- match specific options
  155. if not ( match == true and (
  156. not args.options or
  157. self:_match_options( rule.options, args.options )
  158. ) ) then
  159. match = false
  160. end
  161. -- insert match
  162. if match == true then
  163. rv[#rv+1] = rule
  164. end
  165. end
  166. return rv
  167. end
  168. -- through external commands.
  169. function IptParser.resync( self )
  170. self._rules = { }
  171. self._chain = nil
  172. self:_parse_rules()
  173. end
  174. function IptParser.tables( self )
  175. return self._tables
  176. end
  177. function IptParser.chains( self, table )
  178. local lookup = { }
  179. local chains = { }
  180. for _, r in ipairs(self:find({table=table})) do
  181. if not lookup[r.chain] then
  182. lookup[r.chain] = true
  183. chains[#chains+1] = r.chain
  184. end
  185. end
  186. return chains
  187. end
  188. -- and "rules". The "rules" field is a table of rule tables.
  189. function IptParser.chain( self, table, chain )
  190. return self._chains[table:lower()] and self._chains[table:lower()][chain]
  191. end
  192. function IptParser.is_custom_target( self, target )
  193. for _, r in ipairs(self._rules) do
  194. if r.chain == target then
  195. return true
  196. end
  197. end
  198. return false
  199. end
  200. -- [internal] Parse address according to family.
  201. function IptParser._parse_addr( self, addr )
  202. if self._family == 4 then
  203. return luci.ip.IPv4(addr)
  204. else
  205. return luci.ip.IPv6(addr)
  206. end
  207. end
  208. -- [internal] Parse iptables output from all tables.
  209. function IptParser._parse_rules( self )
  210. for i, tbl in ipairs(self._tables) do
  211. self._chains[tbl] = { }
  212. for i, rule in ipairs(luci.util.execl(self._command % tbl)) do
  213. if rule:find( "^Chain " ) == 1 then
  214. local crefs
  215. local cname, cpol, cpkt, cbytes = rule:match(
  216. "^Chain ([^%s]*) %(policy (%w+) " ..
  217. "(%d+) packets, (%d+) bytes%)"
  218. )
  219. if not cname then
  220. cname, crefs = rule:match(
  221. "^Chain ([^%s]*) %((%d+) references%)"
  222. )
  223. end
  224. self._chain = cname
  225. self._chains[tbl][cname] = {
  226. policy = cpol,
  227. packets = tonumber(cpkt or 0),
  228. bytes = tonumber(cbytes or 0),
  229. references = tonumber(crefs or 0),
  230. rules = { }
  231. }
  232. else
  233. if rule:find("%d") == 1 then
  234. local rule_parts = luci.util.split( rule, "%s+", nil, true )
  235. local rule_details = { }
  236. -- cope with rules that have no target assigned
  237. if rule:match("^%d+%s+%d+%s+%d+%s%s") then
  238. table.insert(rule_parts, 4, nil)
  239. end
  240. -- ip6tables opt column is usually zero-width
  241. if self._family == 6 then
  242. table.insert(rule_parts, 6, "--")
  243. end
  244. rule_details["table"] = tbl
  245. rule_details["chain"] = self._chain
  246. rule_details["index"] = tonumber(rule_parts[1])
  247. rule_details["packets"] = tonumber(rule_parts[2])
  248. rule_details["bytes"] = tonumber(rule_parts[3])
  249. rule_details["target"] = rule_parts[4]
  250. rule_details["protocol"] = rule_parts[5]
  251. rule_details["flags"] = rule_parts[6]
  252. rule_details["inputif"] = rule_parts[7]
  253. rule_details["outputif"] = rule_parts[8]
  254. rule_details["source"] = rule_parts[9]
  255. rule_details["destination"] = rule_parts[10]
  256. rule_details["options"] = { }
  257. for i = 11, #rule_parts do
  258. if #rule_parts[i] > 0 then
  259. rule_details["options"][i-10] = rule_parts[i]
  260. end
  261. end
  262. self._rules[#self._rules+1] = rule_details
  263. self._chains[tbl][self._chain].rules[
  264. #self._chains[tbl][self._chain].rules + 1
  265. ] = rule_details
  266. end
  267. end
  268. end
  269. end
  270. self._chain = nil
  271. end
  272. -- [internal] Return true if optlist1 contains all elements of optlist 2.
  273. -- Return false in all other cases.
  274. function IptParser._match_options( self, o1, o2 )
  275. -- construct a hashtable of first options list to speed up lookups
  276. local oh = { }
  277. for i, opt in ipairs( o1 ) do oh[opt] = true end
  278. -- iterate over second options list
  279. -- each string in o2 must be also present in o1
  280. -- if o2 contains a string which is not found in o1 then return false
  281. for i, opt in ipairs( o2 ) do
  282. if not oh[opt] then
  283. return false
  284. end
  285. end
  286. return true
  287. end