@@ -349,7 +349,7 @@ def _OptionsHandler(self, term):
349
349
else :
350
350
return ''
351
351
352
- def GroupExpressions (self , address_expr , pp_expr , options , verdict ):
352
+ def GroupExpressions (self , address_expr , pp_expr , options , verdict , comment ):
353
353
"""Combines all expressions with a verdict (decision).
354
354
355
355
The inputs are already pre-sanitized by RulesetGenerator. NFTables processes
@@ -362,11 +362,13 @@ def GroupExpressions(self, address_expr, pp_expr, options, verdict):
362
362
pp_expr: pre-processed list of nftables protocols and ports.
363
363
options: string value to append before verdict for NFT special options.
364
364
verdict: action to take on resulting final statement (allow/deny).
365
+ comment: term.comment string adhering to NFT limits.
365
366
366
367
Returns:
367
368
list of strings representing valid nftables statements.
368
369
"""
369
370
statement = []
371
+ statement_with_comment = []
370
372
if address_expr :
371
373
for addr in address_expr :
372
374
if pp_expr :
@@ -388,8 +390,13 @@ def GroupExpressions(self, address_expr, pp_expr, options, verdict):
388
390
statement .append (pstat + Add (options ) + Add (verdict ))
389
391
else :
390
392
# If no addresses or ports & protocol. Verdict only statement.
391
- statement .append ((Add (options ) + verdict ))
392
- return statement
393
+ statement .append ((Add (options ) + Add (verdict )))
394
+ # Handling of comments should always be done after verdict statement.
395
+ if comment != 'comment ' :
396
+ statement_with_comment .append (statement [0 ] + Add (comment ))
397
+ return statement_with_comment
398
+ else :
399
+ return statement
393
400
394
401
def _AddrStatement (self , address_family , src_addr , dst_addr ):
395
402
"""Builds an NFTables address statement.
@@ -455,12 +462,12 @@ def RulesetGenerator(self, term):
455
462
list of strings. Representing a ruleset for later formatting.
456
463
"""
457
464
term_ruleset = []
465
+ comment = 'comment '
458
466
459
467
# COMMENT handling.
460
468
if self .verbose :
461
- term_ruleset .append (
462
- 'comment ' + aclgenerator .TruncateWords (
463
- self .term .comment , Nftables .COMMENT_CHAR_LIMIT ))
469
+ comment += aclgenerator .TruncateWords (
470
+ self .term .comment , Nftables .COMMENT_CHAR_LIMIT )
464
471
# OPTIONS / LOGGING / COUNTERS
465
472
opt = self ._OptionsHandler (term )
466
473
# STATEMENT VERDICT / ACTION.
@@ -483,7 +490,7 @@ def RulesetGenerator(self, term):
483
490
484
491
# TODO: If verdict is not supported, drop nftable_rule for it.
485
492
nftable_rule = self .GroupExpressions (address_list , proto_and_ports , opt ,
486
- verdict )
493
+ verdict , comment )
487
494
term_ruleset .extend (nftable_rule )
488
495
return term_ruleset
489
496
@@ -795,20 +802,22 @@ def __str__(self):
795
802
# First time we comment it out so .nft file is human-readable.
796
803
nft_config .append (
797
804
TabSpacer (8 , '#' + ' ' .join (base_chain_dict [item ]['comment' ])))
798
- # Second time so 'nft list ruleset' keeps the comment in memory.
799
- nft_config .append (
800
- TabSpacer (
801
- 8 , 'comment ' +
802
- aclgenerator .TruncateWords (
803
- base_chain_dict [item ]['comment' ], self .COMMENT_CHAR_LIMIT )))
804
805
nft_config .append (
805
806
TabSpacer (
806
807
8 , 'type filter hook %s priority %s; policy %s;' %
807
808
(base_chain_dict [item ]['hook' ],
808
809
base_chain_dict [item ]['priority' ],
809
810
base_chain_dict [item ]['policy' ])))
810
- # stateful firewall: allow reply traffic.
811
- nft_config .append (TabSpacer (8 , 'ct state established,related accept' ))
811
+ # Add policy header comment after stateful firewall rule.
812
+ if base_chain_dict [item ]['comment' ]:
813
+ nft_config .append (TabSpacer (8 , 'ct state established,related accept'
814
+ + Add ('comment' ) +
815
+ Add (aclgenerator .TruncateWords (
816
+ base_chain_dict [item ]['comment' ],
817
+ self .COMMENT_CHAR_LIMIT ))))
818
+ else :
819
+ # stateful firewall: allows reply traffic.
820
+ nft_config .append (TabSpacer (8 , 'ct state established,related accept' ))
812
821
# Reference the child chains with jump.
813
822
for child_chain in base_chain_dict [item ]['rules' ][item ].keys ():
814
823
nft_config .append (TabSpacer (8 , 'jump %s' % child_chain ))
0 commit comments