network/route: improve Gateway=_dhcp4 handling (#36183)

- Also configures route to the gateway and prefix route in the specified
table, if necessary.
- Also set preferred source address of the route.

Closes #36168.
This commit is contained in:
Luca Boccassi
2025-02-05 12:19:01 +00:00
committed by GitHub
8 changed files with 122 additions and 48 deletions
@@ -43,5 +43,9 @@ Destination=192.168.7.0/24
[Route]
Gateway=_dhcp4
Destination=10.0.0.0/8
Table=211
Destination=192.0.2.0/24
[Route]
Gateway=_dhcp4
Destination=198.51.100.0/24
Table=212
+21 -2
View File
@@ -7263,7 +7263,14 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
self.assertRegex(output, f'192.168.5.1 proto dhcp scope link src {address1} metric 24')
self.assertRegex(output, f'192.168.5.6 proto dhcp scope link src {address1} metric 24')
self.assertRegex(output, f'192.168.5.7 proto dhcp scope link src {address1} metric 24')
self.assertIn('10.0.0.0/8 via 192.168.5.1 proto dhcp', output)
self.assertRegex(output, f'192.0.2.0/24 via 192.168.5.1 proto dhcp src {address1}')
print('## ip route show table 212 dev veth99')
output = check_output('ip route show table 212 dev veth99')
print(output)
self.assertRegex(output, f'192.168.5.0/24 proto dhcp scope link src {address1} metric 24')
self.assertRegex(output, f'192.168.5.1 proto dhcp scope link src {address1} metric 24')
self.assertRegex(output, f'198.51.100.0/24 via 192.168.5.1 proto dhcp src {address1}')
print('## link state file')
output = read_link_state_file('veth99')
@@ -7363,7 +7370,14 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
self.assertNotIn('192.168.5.6', output)
self.assertRegex(output, f'192.168.5.7 proto dhcp scope link src {address2} metric 24')
self.assertRegex(output, f'192.168.5.8 proto dhcp scope link src {address2} metric 24')
self.assertIn('10.0.0.0/8 via 192.168.5.1 proto dhcp', output)
self.assertRegex(output, f'192.0.2.0/24 via 192.168.5.1 proto dhcp src {address2}')
print('## ip route show table 212 dev veth99')
output = check_output('ip route show table 212 dev veth99')
print(output)
self.assertRegex(output, f'192.168.5.0/24 proto dhcp scope link src {address2} metric 24')
self.assertRegex(output, f'192.168.5.1 proto dhcp scope link src {address2} metric 24')
self.assertRegex(output, f'198.51.100.0/24 via 192.168.5.1 proto dhcp src {address2}')
print('## link state file')
output = read_link_state_file('veth99')
@@ -7434,6 +7448,11 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
print(output)
self.assertNotIn(f'{address2}', output)
print('## ip route show table 212 dev veth99')
output = check_output('ip route show table 212 dev veth99')
print(output)
self.assertNotIn(f'{address2}', output)
self.teardown_nftset('addr4', 'network4', 'ifindex')
def test_dhcp_client_ipv4_dbus_status(self):