Changeset 6825
- Timestamp:
- 01/29/11 19:34:00 (2 years ago)
- Files:
-
- 1 modified
-
luci/trunk/libs/sys/luasrc/sys/iptparser.lua (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
luci/trunk/libs/sys/luasrc/sys/iptparser.lua
r4780 r6825 29 29 -- @class function 30 30 -- @name IptParser 31 -- @param family Number specifying the address family. 4 for IPv4, 6 for IPv6 31 32 -- @return IptParser instance 32 33 IptParser = luci.util.class() 33 34 34 function IptParser.__init__( self, ... ) 35 function IptParser.__init__( self, family ) 36 self._family = (tonumber(family) == 6) and 6 or 4 35 37 self._rules = { } 36 38 self._chains = { } 39 40 if self._family == 4 then 41 self._nulladdr = "0.0.0.0/0" 42 self._tables = { "filter", "nat", "mangle", "raw" } 43 self._command = "iptables -t %s --line-numbers -nxvL" 44 else 45 self._nulladdr = "::/0" 46 self._tables = { "filter", "mangle", "raw" } 47 self._command = "ip6tables -t %s --line-numbers -nxvL" 48 end 49 37 50 self:_parse_rules() 38 51 end … … 50 63 -- protocol "all" are always matched 51 64 -- <li> source - Match rules with the given source, rules with source 52 -- "0.0.0.0/0" are always matched65 -- "0.0.0.0/0" (::/0) are always matched 53 66 -- <li> destination - Match rules with the given destination, rules with 54 -- destination "0.0.0.0/0" are always matched67 -- destination "0.0.0.0/0" (::/0) are always matched 55 68 -- <li> inputif - Match rules with the given input interface, rules 56 69 -- with input interface "*" (=all) are always matched … … 77 90 -- <li> outputif - Output interface of the rule,e.g. "eth0.0" 78 91 -- 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" 92 -- <li> source - The source ip range, e.g. "0.0.0.0/0" (::/0) 93 -- <li> destination - The destination ip range, e.g. "0.0.0.0/0" (::/0) 81 94 -- <li> options - A list of specific options of the rule, 82 95 -- e.g. { "reject-with", "tcp-reset" } … … 103 116 local rv = { } 104 117 105 args.source = args.source and luci.ip.IPv4(args.source)106 args.destination = args.destination and luci.ip.IPv4(args.destination)118 args.source = args.source and self:_parse_addr(args.source) 119 args.destination = args.destination and self:_parse_addr(args.destination) 107 120 108 121 for i, rule in ipairs(self._rules) do … … 138 151 -- match source 139 152 if not ( match == true and ( 140 not args.source or rule.source == "0.0.0.0/0"or141 luci.ip.IPv4(rule.source):contains(args.source)153 not args.source or rule.source == self._nulladdr or 154 self:_parse_addr(rule.source):contains(args.source) 142 155 ) ) then 143 156 match = false … … 146 159 -- match destination 147 160 if not ( match == true and ( 148 not args.destination or rule.destination == "0.0.0.0/0"or149 luci.ip.IPv4(rule.destination):contains(args.destination)161 not args.destination or rule.destination == self._nulladdr or 162 self:_parse_addr(rule.destination):contains(args.destination) 150 163 ) ) then 151 164 match = false … … 242 255 243 256 257 -- [internal] Parse address according to family. 258 function IptParser._parse_addr( self, addr ) 259 if self._family == 4 then 260 return luci.ip.IPv4(addr) 261 else 262 return luci.ip.IPv6(addr) 263 end 264 end 265 244 266 -- [internal] Parse iptables output from all tables. 245 267 function IptParser._parse_rules( self ) 246 268 247 for i, tbl in ipairs( { "filter", "nat", "mangle" }) do269 for i, tbl in ipairs(self._tables) do 248 270 249 271 self._chains[tbl] = { } 250 272 251 for i, rule in ipairs(luci.util.execl( "iptables -t " .. tbl .. " --line-numbers -nxvL")) do252 253 if rule:find( " Chain " ) == 1 then273 for i, rule in ipairs(luci.util.execl(self._command % tbl)) do 274 275 if rule:find( "^Chain " ) == 1 then 254 276 255 277 local crefs 256 278 local cname, cpol, cpkt, cbytes = rule:match( 257 " Chain ([^%s]*) %(policy (%w+) " ..279 "^Chain ([^%s]*) %(policy (%w+) " .. 258 280 "(%d+) packets, (%d+) bytes%)" 259 281 ) … … 261 283 if not cname then 262 284 cname, crefs = rule:match( 263 " Chain ([^%s]*) %((%d+) references%)"285 "^Chain ([^%s]*) %((%d+) references%)" 264 286 ) 265 287 end
