summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-12-17 04:07:00 +0900
committerGitHub <noreply@github.com>2021-12-17 04:07:00 +0900
commit75e1378c0dffe10d5881b62e0580f3c2c8829a82 (patch)
tree25e9bcadf68d2eb2fcfcdec3b09dd3119954c70f
parentc6837e4e86d876cba3102ab1ba1e544237c3beb8 (diff)
parent2347b6b94e13c2a07f33e0c83f2612dfd4ff410f (diff)
downloadsystemd-75e1378c0dffe10d5881b62e0580f3c2c8829a82.tar.gz
Merge pull request #21736 from yuwata/network-cleanups-for-drop-foreign-configs
network: cleanups for dropping foreign configs
-rw-r--r--src/network/networkd-address.c22
-rw-r--r--src/network/networkd-route.c5
-rwxr-xr-xtest/test-network/systemd-networkd-tests.py9
3 files changed, 23 insertions, 13 deletions
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 7cd4d27c07..7df743efb5 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -728,6 +728,10 @@ bool link_address_is_dynamic(const Link *link, const Address *address) {
if (route->source != NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
+ /* The route is not assigned yet, or already removed. Ignoring. */
+ if (!route_exists(route))
+ continue;
+
if (route->protocol != RTPROT_DHCP)
continue;
@@ -840,6 +844,10 @@ int link_drop_foreign_addresses(Link *link) {
assert(link);
assert(link->network);
+ /* Keep all addresses when KeepConfiguration=yes. */
+ if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
+ return 0;
+
/* First, mark all addresses. */
SET_FOREACH(address, link->addresses) {
/* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */
@@ -854,10 +862,9 @@ int link_drop_foreign_addresses(Link *link) {
if (!address_exists(address))
continue;
- if (link_address_is_dynamic(link, address)) {
- if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
- continue;
- } else if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
+ /* link_address_is_dynamic() is slightly heavy. Let's call the function only when KeepConfiguration= is set. */
+ if (IN_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP, KEEP_CONFIGURATION_STATIC) &&
+ link_address_is_dynamic(link, address) == (link->network->keep_configuration == KEEP_CONFIGURATION_DHCP))
continue;
address_mark(address);
@@ -895,8 +902,11 @@ int link_drop_addresses(Link *link) {
if (!address_exists(address))
continue;
- /* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */
- if (address->family == AF_INET6 && in6_addr_is_link_local(&address->in_addr.in6))
+ /* Do not drop IPv6LL addresses assigned by the kernel here. They will be dropped in
+ * link_drop_ipv6ll_addresses() if IPv6LL addressing is disabled. */
+ if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN &&
+ address->family == AF_INET6 &&
+ in6_addr_is_link_local(&address->in_addr.in6))
continue;
k = address_remove(address);
diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c
index bfd849abb9..156476066a 100644
--- a/src/network/networkd-route.c
+++ b/src/network/networkd-route.c
@@ -893,6 +893,7 @@ int link_drop_foreign_routes(Link *link) {
assert(link);
assert(link->manager);
+ assert(link->network);
SET_FOREACH(route, link->routes) {
/* do not touch routes managed by the kernel */
@@ -907,11 +908,11 @@ int link_drop_foreign_routes(Link *link) {
if (!route_exists(route))
continue;
- if (route->protocol == RTPROT_STATIC && link->network &&
+ if (route->protocol == RTPROT_STATIC &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
continue;
- if (route->protocol == RTPROT_DHCP && link->network &&
+ if (route->protocol == RTPROT_DHCP &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
continue;
diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py
index 68cdad30ac..a55ac4de7e 100755
--- a/test/test-network/systemd-networkd-tests.py
+++ b/test/test-network/systemd-networkd-tests.py
@@ -4588,12 +4588,11 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
print(output)
self.assertRegex(output, r'192.168.5.*')
- output = check_output(*networkctl_cmd, '-n', '0', 'status', 'veth99', env=env)
- print(output)
- self.assertRegex(output, r'192.168.5.*')
+ with open(os.path.join(network_unit_file_path, 'dhcp-client-keep-configuration-dhcp.network'), mode='a') as f:
+ f.write('[Network]\nDHCP=no\n')
- start_networkd(3)
- self.wait_online(['veth-peer:routable'])
+ start_networkd()
+ self.wait_online(['veth99:routable', 'veth-peer:routable'])
print('Still the lease address should be kept after networkd restarted')
output = check_output('ip address show dev veth99 scope global')