1
0

iptparser.lua 9.8 KB

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