Skip to content

Commit

Permalink
fix: Allow address 0.0.0.0/0 or ::/0 for 'from'/'to' in routing rule …
Browse files Browse the repository at this point in the history
…validation

`from 0.0.0.0/0` means from all IPv4 addresses, `from ::/0` means from
all IPv6 addresses. In NM, if `from` property is not specified in a
routing rule, NM still appends `from 0.0.0.0/0` or `from ::/0` to the
rule. NM also allows to specify `to 0.0.0.0/0` or `to ::/0` in a
routing rule, but the connection profiles will only show the `from`
setting for the rule.

Signed-off-by: Wen Liang <[email protected]>
  • Loading branch information
liangwen12year committed Oct 20, 2023
1 parent a9f20cb commit 735943e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
8 changes: 6 additions & 2 deletions library/network_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,9 @@ def connection_create(self, connections, idx, connection_current=None):
routing_rule["dport"][0],
routing_rule["dport"][1],
)
if routing_rule["from"]:
# In NM, when user specifies `from 0.0.0.0/0`` or `from ::/0` in a
# routing rule, NM treats it as if the `from` setting is not specified.
if routing_rule["from"] and routing_rule["from"]["prefix"]:

Check warning on line 1248 in library/network_connections.py

View check run for this annotation

Codecov / codecov/patch

library/network_connections.py#L1248

Added line #L1248 was not covered by tests
NM.IPRoutingRule.set_from(
nm_routing_rule,
routing_rule["from"]["address"],
Expand Down Expand Up @@ -1274,7 +1276,9 @@ def connection_create(self, connections, idx, connection_current=None):
)
if routing_rule["table"]:
NM.IPRoutingRule.set_table(nm_routing_rule, routing_rule["table"])
if routing_rule["to"]:
# In NM, when user specifies `to 0.0.0.0/0`` or `to ::/0` in a
# routing rule, NM treats it as if the `to` setting is not specified.
if routing_rule["to"] and routing_rule["to"]["prefix"]:

Check warning on line 1281 in library/network_connections.py

View check run for this annotation

Codecov / codecov/patch

library/network_connections.py#L1281

Added line #L1281 was not covered by tests
NM.IPRoutingRule.set_to(
nm_routing_rule,
routing_rule["to"]["address"],
Expand Down
18 changes: 15 additions & 3 deletions module_utils/network_lsr/argument_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,15 +779,27 @@ def _validate_post(self, value, name, result):
name,
"missing 'table' for the routing rule",
)

if result["from"] is not None:
# `from 0.0.0.0/0` means from all IPv4 addresses
# `from ::/0` means from all IPv6 addresses
# In NM, if `from` property is not specified in a routing rule, NM
# still appends `from 0.0.0.0/0` or `from ::/0` to the rule
if result["from"] is not None and result["from"]["address"] not in [
"0.0.0.0",
"::",
]:
if result["from"]["prefix"] == 0:
raise ValidationError(
name,
"the prefix length for 'from' cannot be zero",
)

if result["to"] is not None:
# NM also allows to specify `to 0.0.0.0/0` or `to ::/0` in a routing
# rule, but the connection profiles will only show the `from` setting
# for the rule
if result["to"] is not None and result["to"]["address"] not in [
"0.0.0.0",
"::",
]:
if result["to"]["prefix"] == 0:
raise ValidationError(
name,
Expand Down
12 changes: 12 additions & 0 deletions tests/playbooks/tests_routing_rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
family: ipv4
oif: oiftest
table: 30400
- priority: 30403
from: 0.0.0.0/0
to: 0.0.0.0/0
table: 30400
- priority: 30600
to: 2001:db8::4/32
table: 30600
Expand All @@ -100,6 +104,10 @@
dport: 128 - 256
invert: true
table: 30600
- priority: 30602
from: ::/0
to: ::/0
table: 30600
- priority: 200
from: 198.51.100.56/26
table: custom
Expand Down Expand Up @@ -220,6 +228,8 @@
0.0.0.0/0 iif iiftest table 30400")
- connection_route_rule.stdout is search("priority 30402 from
0.0.0.0/0 oif oiftest table 30400")
- connection_route_rule.stdout is search("priority 30403 from
0.0.0.0/0 table 30400")
- connection_route_rule.stdout is search("priority 200 from
198.51.100.56/26 table 200")
msg: "the specified IPv4 routing rule was not configured in the
Expand All @@ -235,6 +245,8 @@
::/0 dport 128-256 table 30600") or
connection_route_rule6.stdout is search("not priority 30601 from
::/0 dport 128-256 table 30600")
- connection_route_rule6.stdout is search("priority 30602 from
::/0 table 30600")
msg: "the specified IPv6 routing rule was not configured in the
connection '{{ interface }}'"

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_network_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5050,6 +5050,14 @@ def test_routing_rule_validate_address_family(self):
self.validator.validate,
self.test_connections,
)
self.test_connections[0]["ip"]["routing_rule"][0]["from"] = "::/0"
self.test_connections[0]["ip"]["routing_rule"][0]["to"] = "::/0"
self.validator.validate(self.test_connections)

self.test_connections[0]["ip"]["routing_rule"][0]["family"] = "ipv4"
self.test_connections[0]["ip"]["routing_rule"][0]["from"] = "0.0.0.0/0"
self.test_connections[0]["ip"]["routing_rule"][0]["to"] = "0.0.0.0/0"
self.validator.validate(self.test_connections)

def test_routing_rule_missing_table(self):
"""
Expand Down

0 comments on commit 735943e

Please sign in to comment.