Changeset 6825

Show
Ignore:
Timestamp:
01/29/11 19:34:00 (2 years ago)
Author:
jow
Message:

libs/sys: add ip6tables support to luci.sys.iptparser

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/sys/luasrc/sys/iptparser.lua

    r4780 r6825  
    2929-- @class   function 
    3030-- @name    IptParser 
     31-- @param   family  Number specifying the address family. 4 for IPv4, 6 for IPv6 
    3132-- @return  IptParser instance 
    3233IptParser = luci.util.class() 
    3334 
    34 function IptParser.__init__( self, ... ) 
     35function IptParser.__init__( self, family ) 
     36    self._family = (tonumber(family) == 6) and 6 or 4 
    3537    self._rules  = { } 
    3638    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 
    3750    self:_parse_rules() 
    3851end 
     
    5063--                      protocol "all" are always matched 
    5164--  <li> source      - Match rules with the given source, rules with source 
    52 --                      "0.0.0.0/0" are always matched 
     65--                      "0.0.0.0/0" (::/0) are always matched 
    5366--  <li> destination - Match rules with the given destination, rules with 
    54 --                      destination "0.0.0.0/0" are always matched 
     67--                      destination "0.0.0.0/0" (::/0) are always matched 
    5568--  <li> inputif     - Match rules with the given input interface, rules 
    5669--                      with input  interface "*" (=all) are always matched 
     
    7790--  <li> outputif    - Output interface of the rule,e.g. "eth0.0" 
    7891--                      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) 
    8194--  <li> options     - A list of specific options of the rule, 
    8295--                      e.g. { "reject-with", "tcp-reset" } 
     
    103116    local rv   = { } 
    104117 
    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) 
    107120 
    108121    for i, rule in ipairs(self._rules) do 
     
    138151        -- match source 
    139152        if not ( match == true and ( 
    140             not args.source or rule.source == "0.0.0.0/0" or 
    141             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) 
    142155        ) ) then 
    143156            match = false 
     
    146159        -- match destination 
    147160        if not ( match == true and ( 
    148             not args.destination or rule.destination == "0.0.0.0/0" or 
    149             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) 
    150163        ) ) then 
    151164            match = false 
     
    242255 
    243256 
     257-- [internal] Parse address according to family. 
     258function 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 
     264end 
     265 
    244266-- [internal] Parse iptables output from all tables. 
    245267function IptParser._parse_rules( self ) 
    246268 
    247     for i, tbl in ipairs({ "filter", "nat", "mangle" }) do 
     269    for i, tbl in ipairs(self._tables) do 
    248270 
    249271        self._chains[tbl] = { } 
    250272 
    251         for i, rule in ipairs(luci.util.execl("iptables -t " .. tbl .. " --line-numbers -nxvL")) do 
    252  
    253             if rule:find( "Chain " ) == 1 then 
     273        for i, rule in ipairs(luci.util.execl(self._command % tbl)) do 
     274 
     275            if rule:find( "^Chain " ) == 1 then 
    254276 
    255277                local crefs 
    256278                local cname, cpol, cpkt, cbytes = rule:match( 
    257                     "Chain ([^%s]*) %(policy (%w+) " .. 
     279                    "^Chain ([^%s]*) %(policy (%w+) " .. 
    258280                    "(%d+) packets, (%d+) bytes%)" 
    259281                ) 
     
    261283                if not cname then 
    262284                    cname, crefs = rule:match( 
    263                         "Chain ([^%s]*) %((%d+) references%)" 
     285                        "^Chain ([^%s]*) %((%d+) references%)" 
    264286                    ) 
    265287                end