Skip to content

Commit 3584770

Browse files
olethanhnesitor
authored andcommitted
New chain aleph-supervisor-prerouting to contain the redirect rule
1 parent 124aeea commit 3584770

File tree

2 files changed

+579
-15
lines changed

2 files changed

+579
-15
lines changed

src/aleph/vm/network/firewall.py

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,8 @@ def initialize_nftables() -> None:
153153
are properly set up and, if missing, creates them. Additionally, it adds the necessary
154154
custom chains and rules in the nftables configuration for network supervision.
155155
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.
159158
"""
160159
nft_ruleset = get_existing_nftables_ruleset()
161160
commands: list[dict] = []
@@ -183,7 +182,21 @@ def initialize_nftables() -> None:
183182
"hook": "forward",
184183
"prio": 0,
185184
}
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
187200
# Check if table exists, if not create it.
188201
commands += add_entity_if_not_present(
189202
nft_ruleset,
@@ -270,6 +283,31 @@ def initialize_nftables() -> None:
270283
},
271284
)
272285

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+
273311
execute_json_nft_commands(commands)
274312

275313

@@ -278,6 +316,7 @@ def teardown_nftables() -> None:
278316
logger.debug("Tearing down nftables setup")
279317
remove_chain(f"{settings.NFTABLES_CHAIN_PREFIX}-supervisor-nat")
280318
remove_chain(f"{settings.NFTABLES_CHAIN_PREFIX}-supervisor-filter")
319+
remove_chain(f"{settings.NFTABLES_CHAIN_PREFIX}-supervisor-prerouting")
281320

282321

283322
def remove_chain(name: str) -> dict:
@@ -492,16 +531,17 @@ def add_port_redirect_rule(
492531
Returns:
493532
The exit code from executing the nftables commands
494533
"""
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")
497537

498538
return ensure_entities(
499539
[
500540
{
501541
"rule": {
502542
"family": "ip",
503-
"table": "nat",
504-
"chain": chain["name"],
543+
"table": prerouting_table,
544+
"chain": chain_name,
505545
"expr": [
506546
{
507547
"match": {
@@ -527,7 +567,7 @@ def add_port_redirect_rule(
527567
{
528568
"rule": {
529569
"family": "ip",
530-
"table": table,
570+
"table": forward_table,
531571
"chain": f"{settings.NFTABLES_CHAIN_PREFIX}-vm-filter-{vm_id}",
532572
"expr": [
533573
{
@@ -565,8 +605,8 @@ def remove_port_redirect_rule(interface: TapInterface, host_port: int, vm_port:
565605
The exit code from executing the nftables commands
566606
"""
567607
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")
570610

571611
commands = []
572612

@@ -575,8 +615,8 @@ def remove_port_redirect_rule(interface: TapInterface, host_port: int, vm_port:
575615
isinstance(entry, dict)
576616
and "rule" in entry
577617
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
580620
and "expr" in entry["rule"]
581621
):
582622
expr = entry["rule"]["expr"]
@@ -599,8 +639,8 @@ def remove_port_redirect_rule(interface: TapInterface, host_port: int, vm_port:
599639
"delete": {
600640
"rule": {
601641
"family": "ip",
602-
"table": table,
603-
"chain": chain["name"],
642+
"table": prerouting_table,
643+
"chain": chain_name,
604644
"handle": entry["rule"]["handle"],
605645
}
606646
}

0 commit comments

Comments
 (0)