Spaces:
Running
Running
fix: match IPv6 control packets through extension headers
Browse files
src/lumi_eggcracker/offline_boundary.py
CHANGED
|
@@ -338,7 +338,7 @@ def _nft_program(table: str) -> str:
|
|
| 338 |
f"add chain inet {table} {OUTPUT_CHAIN} {{ type filter hook output priority 0; policy drop; }}",
|
| 339 |
f"add rule inet {table} {INPUT_CHAIN} iifname \"lo\" accept",
|
| 340 |
f"add rule inet {table} {OUTPUT_CHAIN} oifname \"lo\" accept",
|
| 341 |
-
f"add rule inet {table} {OUTPUT_CHAIN}
|
| 342 |
f"add rule inet {table} {OUTPUT_CHAIN} counter name {COUNTER_NAME} drop",
|
| 343 |
"",
|
| 344 |
)
|
|
@@ -577,29 +577,22 @@ class OfflineBoundary:
|
|
| 577 |
expr = rule["expr"]
|
| 578 |
if len(expr) == 2:
|
| 579 |
first, second = expr
|
| 580 |
-
elif len(expr) ==
|
| 581 |
-
first, second, third
|
| 582 |
first_match = first.get("match") if isinstance(first, dict) else None
|
| 583 |
-
second_match = second.get("match") if isinstance(second, dict) else None
|
| 584 |
first_left = first_match.get("left") if isinstance(first_match, dict) else None
|
| 585 |
-
second_left = second_match.get("left") if isinstance(second_match, dict) else None
|
| 586 |
first_payload = first_left.get("payload") if isinstance(first_left, dict) else None
|
| 587 |
-
|
| 588 |
-
right = second_match.get("right") if isinstance(second_match, dict) else None
|
| 589 |
right_set = right.get("set") if isinstance(right, dict) else None
|
| 590 |
if (
|
| 591 |
not isinstance(first_match, dict)
|
| 592 |
or first_match.get("op") != "=="
|
| 593 |
-
or first_payload != {"field": "
|
| 594 |
-
or first_match.get("right") != "ipv6-icmp"
|
| 595 |
-
or not isinstance(second_match, dict)
|
| 596 |
-
or second_match.get("op") != "=="
|
| 597 |
-
or second_payload != {"field": "type", "protocol": "icmpv6"}
|
| 598 |
or not isinstance(right_set, list)
|
| 599 |
or set(right_set) != CONTROL_ICMPV6_TYPES
|
| 600 |
or len(right_set) != len(CONTROL_ICMPV6_TYPES)
|
| 601 |
-
or
|
| 602 |
-
or
|
| 603 |
):
|
| 604 |
raise JsonInputError("offline boundary control rule is invalid")
|
| 605 |
control_rules += 1
|
|
|
|
| 338 |
f"add chain inet {table} {OUTPUT_CHAIN} {{ type filter hook output priority 0; policy drop; }}",
|
| 339 |
f"add rule inet {table} {INPUT_CHAIN} iifname \"lo\" accept",
|
| 340 |
f"add rule inet {table} {OUTPUT_CHAIN} oifname \"lo\" accept",
|
| 341 |
+
f"add rule inet {table} {OUTPUT_CHAIN} icmpv6 type {{ 130, 131, 132, 133, 134, 135, 136, 137, 143 }} counter name {CONTROL_COUNTER_NAME} drop",
|
| 342 |
f"add rule inet {table} {OUTPUT_CHAIN} counter name {COUNTER_NAME} drop",
|
| 343 |
"",
|
| 344 |
)
|
|
|
|
| 577 |
expr = rule["expr"]
|
| 578 |
if len(expr) == 2:
|
| 579 |
first, second = expr
|
| 580 |
+
elif len(expr) == 3 and chain == self.identity.output_chain:
|
| 581 |
+
first, second, third = expr
|
| 582 |
first_match = first.get("match") if isinstance(first, dict) else None
|
|
|
|
| 583 |
first_left = first_match.get("left") if isinstance(first_match, dict) else None
|
|
|
|
| 584 |
first_payload = first_left.get("payload") if isinstance(first_left, dict) else None
|
| 585 |
+
right = first_match.get("right") if isinstance(first_match, dict) else None
|
|
|
|
| 586 |
right_set = right.get("set") if isinstance(right, dict) else None
|
| 587 |
if (
|
| 588 |
not isinstance(first_match, dict)
|
| 589 |
or first_match.get("op") != "=="
|
| 590 |
+
or first_payload != {"field": "type", "protocol": "icmpv6"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
or not isinstance(right_set, list)
|
| 592 |
or set(right_set) != CONTROL_ICMPV6_TYPES
|
| 593 |
or len(right_set) != len(CONTROL_ICMPV6_TYPES)
|
| 594 |
+
or second != {"counter": self.identity.control_counter}
|
| 595 |
+
or third != {"drop": None}
|
| 596 |
):
|
| 597 |
raise JsonInputError("offline boundary control rule is invalid")
|
| 598 |
control_rules += 1
|
tests/test_offline_boundary.py
CHANGED
|
@@ -153,18 +153,6 @@ class OfflineBoundaryTests(unittest.TestCase):
|
|
| 153 |
"table": value.table,
|
| 154 |
"chain": value.output_chain,
|
| 155 |
"expr": [
|
| 156 |
-
{
|
| 157 |
-
"match": {
|
| 158 |
-
"op": "==",
|
| 159 |
-
"left": {
|
| 160 |
-
"payload": {
|
| 161 |
-
"field": "nexthdr",
|
| 162 |
-
"protocol": "ip6",
|
| 163 |
-
}
|
| 164 |
-
},
|
| 165 |
-
"right": "ipv6-icmp",
|
| 166 |
-
}
|
| 167 |
-
},
|
| 168 |
{
|
| 169 |
"match": {
|
| 170 |
"op": "==",
|
|
|
|
| 153 |
"table": value.table,
|
| 154 |
"chain": value.output_chain,
|
| 155 |
"expr": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
{
|
| 157 |
"match": {
|
| 158 |
"op": "==",
|