@@ -153,9 +153,8 @@ def initialize_nftables() -> None:
153
153
are properly set up and, if missing, creates them. Additionally, it adds the necessary
154
154
custom chains and rules in the nftables configuration for network supervision.
155
155
156
- Chain aleph-vm-supervisor-nat are created and aleph-vm-supervisor-filter are created
157
- to contains our rules.
158
-
156
+ Chain aleph-vm-supervisor-nat, aleph-vm-supervisor-filter, and aleph-vm-supervisor-prerouting are created
157
+ to contains the rules.
159
158
"""
160
159
nft_ruleset = get_existing_nftables_ruleset ()
161
160
commands : list [dict ] = []
@@ -183,7 +182,21 @@ def initialize_nftables() -> None:
183
182
"hook" : "forward" ,
184
183
"prio" : 0 ,
185
184
}
186
- chain = default_base_chain_hook_forward if hook == "forward" else default_base_chain_hook_postrouting
185
+ default_base_chain_hook_prerouting = {
186
+ "family" : "ip" ,
187
+ "table" : "nat" ,
188
+ "name" : "PREROUTING" ,
189
+ "type" : "nat" ,
190
+ "hook" : "prerouting" ,
191
+ "prio" : - 100 ,
192
+ "policy" : "accept" ,
193
+ }
194
+ if hook == "forward" :
195
+ chain = default_base_chain_hook_forward
196
+ elif hook == "postrouting" :
197
+ chain = default_base_chain_hook_postrouting
198
+ elif hook == "prerouting" :
199
+ chain = default_base_chain_hook_prerouting
187
200
# Check if table exists, if not create it.
188
201
commands += add_entity_if_not_present (
189
202
nft_ruleset ,
@@ -270,6 +283,31 @@ def initialize_nftables() -> None:
270
283
},
271
284
)
272
285
286
+ # Add chain aleph-supervisor-prerouting
287
+ commands += add_entity_if_not_present (
288
+ nft_ruleset ,
289
+ {
290
+ "chain" : {
291
+ "family" : "ip" ,
292
+ "table" : base_chains ["prerouting" ]["table" ],
293
+ "name" : f"{ settings .NFTABLES_CHAIN_PREFIX } -supervisor-prerouting" ,
294
+ }
295
+ },
296
+ )
297
+
298
+ # Add jump to chain aleph-supervisor-prerouting
299
+ commands += add_entity_if_not_present (
300
+ nft_ruleset ,
301
+ {
302
+ "rule" : {
303
+ "family" : "ip" ,
304
+ "table" : base_chains ["prerouting" ]["table" ],
305
+ "chain" : base_chains ["prerouting" ]["name" ],
306
+ "expr" : [{"jump" : {"target" : f"{ settings .NFTABLES_CHAIN_PREFIX } -supervisor-prerouting" }}],
307
+ }
308
+ },
309
+ )
310
+
273
311
execute_json_nft_commands (commands )
274
312
275
313
@@ -278,6 +316,7 @@ def teardown_nftables() -> None:
278
316
logger .debug ("Tearing down nftables setup" )
279
317
remove_chain (f"{ settings .NFTABLES_CHAIN_PREFIX } -supervisor-nat" )
280
318
remove_chain (f"{ settings .NFTABLES_CHAIN_PREFIX } -supervisor-filter" )
319
+ remove_chain (f"{ settings .NFTABLES_CHAIN_PREFIX } -supervisor-prerouting" )
281
320
282
321
283
322
def remove_chain (name : str ) -> dict :
@@ -492,16 +531,17 @@ def add_port_redirect_rule(
492
531
Returns:
493
532
The exit code from executing the nftables commands
494
533
"""
495
- chain = add_or_get_prerouting_chain ()
496
- table = get_table_for_hook ("forward" )
534
+ chain_name = f"{ settings .NFTABLES_CHAIN_PREFIX } -supervisor-prerouting"
535
+ prerouting_table = get_table_for_hook ("prerouting" )
536
+ forward_table = get_table_for_hook ("forward" )
497
537
498
538
return ensure_entities (
499
539
[
500
540
{
501
541
"rule" : {
502
542
"family" : "ip" ,
503
- "table" : "nat" ,
504
- "chain" : chain [ "name" ] ,
543
+ "table" : prerouting_table ,
544
+ "chain" : chain_name ,
505
545
"expr" : [
506
546
{
507
547
"match" : {
@@ -527,7 +567,7 @@ def add_port_redirect_rule(
527
567
{
528
568
"rule" : {
529
569
"family" : "ip" ,
530
- "table" : table ,
570
+ "table" : forward_table ,
531
571
"chain" : f"{ settings .NFTABLES_CHAIN_PREFIX } -vm-filter-{ vm_id } " ,
532
572
"expr" : [
533
573
{
@@ -565,8 +605,8 @@ def remove_port_redirect_rule(interface: TapInterface, host_port: int, vm_port:
565
605
The exit code from executing the nftables commands
566
606
"""
567
607
nft_ruleset = get_existing_nftables_ruleset ()
568
- chain = add_or_get_prerouting_chain ()
569
- table = chain [ 'table' ]
608
+ chain_name = f" { settings . NFTABLES_CHAIN_PREFIX } -supervisor-prerouting"
609
+ prerouting_table = get_table_for_hook ( "prerouting" )
570
610
571
611
commands = []
572
612
@@ -575,8 +615,8 @@ def remove_port_redirect_rule(interface: TapInterface, host_port: int, vm_port:
575
615
isinstance (entry , dict )
576
616
and "rule" in entry
577
617
and entry ["rule" ].get ("family" ) == "ip"
578
- and entry ["rule" ].get ("table" ) == table
579
- and entry ["rule" ].get ("chain" ) == chain [ "name" ]
618
+ and entry ["rule" ].get ("table" ) == prerouting_table
619
+ and entry ["rule" ].get ("chain" ) == chain_name
580
620
and "expr" in entry ["rule" ]
581
621
):
582
622
expr = entry ["rule" ]["expr" ]
@@ -599,8 +639,8 @@ def remove_port_redirect_rule(interface: TapInterface, host_port: int, vm_port:
599
639
"delete" : {
600
640
"rule" : {
601
641
"family" : "ip" ,
602
- "table" : table ,
603
- "chain" : chain [ "name" ] ,
642
+ "table" : prerouting_table ,
643
+ "chain" : chain_name ,
604
644
"handle" : entry ["rule" ]["handle" ],
605
645
}
606
646
}
0 commit comments