5050
5151
5252# get iptables rule dict for chain in table
53- def _iptables_get_rules (chain , table ):
53+ def _iptables_get_rules (command , chain , table ):
5454 # define list with rules
5555 rules = []
5656
5757 # run iptables, save output and split it by lines
58- iptables_command = f'iptables -vn -t { table } -L { chain } '
58+ iptables_command = f'{ command } -vn -t { table } -L { chain } '
5959 tmp = cmd (iptables_command , message = 'Failed to get flows list' )
6060 lines = tmp .splitlines ()
6161
@@ -67,9 +67,9 @@ def _iptables_get_rules(chain, table):
6767
6868 # Check that format is as expected
6969 if len (lines ) < 2 :
70- raise ConfigError (f'Unexpected output from iptables , too few lines' )
70+ raise ConfigError (f'Unexpected output from { command } , too few lines' )
7171 if not lines [0 ].startswith (f'Chain { chain } ' ):
72- raise ConfigError (f'Unexpected first line in output of iptables : "{ lines [0 ]} "' )
72+ raise ConfigError (f'Unexpected first line in output of { command } : "{ lines [0 ]} "' )
7373 columns = lines [1 ].split ();
7474
7575 # parse each line and add information to list
@@ -94,7 +94,7 @@ def _iptables_get_rules(chain, table):
9494 # return list with rules
9595 return rules
9696
97- def _iptables_config (configured_ifaces , direction , length = None ):
97+ def _iptables_config (command , configured_ifaces , direction ):
9898 # define list of nftables commands to modify settings
9999 iptables_commands = []
100100
@@ -113,7 +113,7 @@ def _iptables_config(configured_ifaces, direction, length=None):
113113 configured_ifaces_extended .append ({ 'iface' : iface })
114114
115115 # get currently configured interfaces with iptables rules
116- active_rules = _iptables_get_rules (iptables_chain , iptables_table )
116+ active_rules = _iptables_get_rules (command , iptables_chain , iptables_table )
117117
118118 # compare current active list with configured one and delete excessive interfaces, add missed
119119 active_ifaces = []
@@ -132,7 +132,7 @@ def _iptables_config(configured_ifaces, direction, length=None):
132132 # rulenums are not changed
133133 rulenums_delete .sort (reverse = True )
134134 for rulenum in rulenums_delete :
135- iptables_commands .append (f'iptables -t { iptables_table } -D { iptables_chain } { rulenum } ' )
135+ iptables_commands .append (f'{ command } -t { iptables_table } -D { iptables_chain } { rulenum } ' )
136136
137137 # do not create new rules for already configured interfaces
138138 for iface in active_ifaces :
@@ -144,13 +144,17 @@ def _iptables_config(configured_ifaces, direction, length=None):
144144 iface = iface_extended ['iface' ]
145145 iface_option = "o" if direction == "egress" else "i"
146146 #iptables -t raw -A PREROUTING -j NETFLOW -i eth0
147- rule_definition = f'iptables -t { iptables_table } -A { iptables_chain } -j NETFLOW -{ iface_option } { iface } '
147+ rule_definition = f'{ command } -t { iptables_table } -A { iptables_chain } -j NETFLOW -{ iface_option } { iface } '
148148 iptables_commands .append (rule_definition )
149149
150150 # change iptables
151151 for command in iptables_commands :
152152 cmd (command , raising = ConfigError )
153153
154+ def _iptables_config_v4_and_v6 (configured_ifaces , direction ):
155+ for command in 'iptables' , 'ip6tables' :
156+ _iptables_config (command , configured_ifaces , direction )
157+
154158
155159def get_config (config = None ):
156160 if config :
@@ -268,8 +272,8 @@ def apply(flow_config):
268272 # all iptables usage of ipt_NETFLOW
269273 # When flow_config is disabled everything should be cleaned-up too
270274 if need_reload or not flow_config :
271- _iptables_config ([], 'ingress' )
272- _iptables_config ([], 'egress' )
275+ _iptables_config_v4_and_v6 ([], 'ingress' )
276+ _iptables_config_v4_and_v6 ([], 'egress' )
273277
274278 # Stop flow-accounting module
275279 unload_kmod (module_name )
@@ -285,13 +289,13 @@ def apply(flow_config):
285289 # configure iptables for defined interfaces
286290 if 'interface' in flow_config ['netflow' ]:
287291 interfaces = flow_config ['netflow' ]['interface' ]
288- _iptables_config (interfaces , 'ingress' )
292+ _iptables_config_v4_and_v6 (interfaces , 'ingress' )
289293
290294 # configure egress the same way if configured otherwise remove it
291295 if 'enable_egress' in flow_config :
292- _iptables_config (interfaces , 'egress' )
296+ _iptables_config_v4_and_v6 (interfaces , 'egress' )
293297 else :
294- _iptables_config ([], 'egress' )
298+ _iptables_config_v4_and_v6 ([], 'egress' )
295299
296300
297301if __name__ == '__main__' :
0 commit comments