]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #12971 from yuwata/network-reassign-static-routes
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 12 Jul 2019 00:36:16 +0000 (09:36 +0900)
committerGitHub <noreply@github.com>
Fri, 12 Jul 2019 00:36:16 +0000 (09:36 +0900)
network: reassign static routes and process reply address messages

src/basic/in-addr-util.c
src/basic/in-addr-util.h
src/network/networkd-dhcp4.c
src/network/networkd-dhcp6.c
src/network/networkd-ndisc.c
test/test-network/conf/dhcp-client-reassign-static-routes-ipv4.network [moved from test/test-network/conf/dhcp-client-use-routes-no.network with 100% similarity]
test/test-network/conf/dhcp-client-reassign-static-routes-ipv6.network [new file with mode: 0644]
test/test-network/systemd-networkd-tests.py

index 7a5384bab3c69be16ecf75f745c72637a8812228..06b92db579fda40e2a728eab9a963d32f41e37b5 100644 (file)
@@ -92,12 +92,19 @@ int in_addr_is_localhost(int family, const union in_addr_union *u) {
         return -EAFNOSUPPORT;
 }
 
+bool in4_addr_equal(const struct in_addr *a, const struct in_addr *b) {
+        assert(a);
+        assert(b);
+
+        return a->s_addr == b->s_addr;
+}
+
 int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
         assert(a);
         assert(b);
 
         if (family == AF_INET)
-                return a->in.s_addr == b->in.s_addr;
+                return in4_addr_equal(&a->in, &b->in);
 
         if (family == AF_INET6)
                 return
index 2ca7f4b32fc88b5d8a9ecd2674661839821f4015..28afc7d86cfd91485d6f1fba8c871830d22434d6 100644 (file)
@@ -32,6 +32,7 @@ int in_addr_is_localhost(int family, const union in_addr_union *u);
 
 bool in4_addr_is_non_local(const struct in_addr *a);
 
+bool in4_addr_equal(const struct in_addr *a, const struct in_addr *b);
 int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b);
 int in_addr_prefix_intersect(int family, const union in_addr_union *a, unsigned aprefixlen, const union in_addr_union *b, unsigned bprefixlen);
 int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen);
index c0d776f464b75b5b541e944e9fd06f90229dfa38..7ed91cebd12e6518cf9c972c4f1760b9054eeb4a 100644 (file)
@@ -75,8 +75,8 @@ static int route_scope_from_address(const Route *route, const struct in_addr *se
         assert(route);
         assert(self_addr);
 
-        if (in_addr_is_localhost(AF_INET, &route->dst) ||
-            (self_addr->s_addr && route->dst.in.s_addr == self_addr->s_addr))
+        if (in4_addr_is_localhost(&route->dst.in) ||
+            (!in4_addr_is_null(self_addr) && in4_addr_equal(&route->dst.in, self_addr)))
                 return RT_SCOPE_HOST;
         else if (in4_addr_is_null(&route->gw.in))
                 return RT_SCOPE_LINK;
@@ -153,8 +153,8 @@ static int link_set_dhcp_routes(Link *link) {
                 r = route_configure(route, link, dhcp4_route_handler);
                 if (r < 0)
                         return log_link_error_errno(link, r, "Could not set host route: %m");
-
-                link->dhcp4_messages++;
+                if (r > 0)
+                        link->dhcp4_messages++;
         }
 
         r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
@@ -192,8 +192,8 @@ static int link_set_dhcp_routes(Link *link) {
                 r = route_configure(route_gw, link, dhcp4_route_handler);
                 if (r < 0)
                         return log_link_error_errno(link, r, "Could not set host route: %m");
-
-                link->dhcp4_messages++;
+                if (r > 0)
+                        link->dhcp4_messages++;
 
                 r = route_new(&route);
                 if (r < 0)
@@ -209,8 +209,8 @@ static int link_set_dhcp_routes(Link *link) {
                 r = route_configure(route, link, dhcp4_route_handler);
                 if (r < 0)
                         return log_link_error_errno(link, r, "Could not set routes: %m");
-
-                link->dhcp4_messages++;
+                if (r > 0)
+                        link->dhcp4_messages++;
         }
 
         return 0;
@@ -460,8 +460,8 @@ static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *
                 link_enter_failed(link);
                 return 1;
         }
-
-        manager_rtnl_process_address(rtnl, m, link->manager);
+        if (r >= 0)
+                manager_rtnl_process_address(rtnl, m, link->manager);
 
         r = link_set_dhcp_routes(link);
         if (r < 0) {
index 286b9071ae7c91e6678afd58ce23faf7d482df94..0a73ffc848d911668cd2f758afca33617181703e 100644 (file)
@@ -408,10 +408,13 @@ static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *
                 log_link_error_errno(link, r, "Could not set DHCPv6 address: %m");
 
                 link_enter_failed(link);
-
-        } else if (r >= 0)
+                return 1;
+        }
+        if (r >= 0)
                 manager_rtnl_process_address(rtnl, m, link->manager);
 
+        link_request_set_routes(link);
+
         return 1;
 }
 
index 78f89e026e4f035e35398145472abbcfb35c0a4f..39b66d8f6d7a24ed5fa70d3692d0303290b3e482 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "missing_network.h"
 #include "networkd-dhcp6.h"
+#include "networkd-manager.h"
 #include "networkd-ndisc.h"
 #include "networkd-route.h"
 #include "strv.h"
@@ -18,7 +19,7 @@
 #define NDISC_RDNSS_MAX 64U
 #define NDISC_PREFIX_LFT_MIN 7200U
 
-static int ndisc_netlink_message_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
+static int ndisc_netlink_route_message_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
         int r;
 
         assert(link);
@@ -32,6 +33,30 @@ static int ndisc_netlink_message_handler(sd_netlink *rtnl, sd_netlink_message *m
 
         if (link->ndisc_messages == 0) {
                 link->ndisc_configured = true;
+                link_request_set_routes(link);
+                link_check_ready(link);
+        }
+
+        return 1;
+}
+
+static int ndisc_netlink_address_message_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
+        int r;
+
+        assert(link);
+        assert(link->ndisc_messages > 0);
+
+        link->ndisc_messages--;
+
+        r = sd_netlink_message_get_errno(m);
+        if (r < 0 && r != -EEXIST)
+                log_link_error_errno(link, r, "Could not set NDisc route or address: %m");
+        else if (r >= 0)
+                manager_rtnl_process_address(rtnl, m, link->manager);
+
+        if (link->ndisc_messages == 0) {
+                link->ndisc_configured = true;
+                link_request_set_routes(link);
                 link_check_ready(link);
         }
 
@@ -116,7 +141,7 @@ static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
         route->lifetime = time_now + lifetime * USEC_PER_SEC;
         route->mtu = mtu;
 
-        r = route_configure(route, link, ndisc_netlink_message_handler);
+        r = route_configure(route, link, ndisc_netlink_route_message_handler);
         if (r < 0) {
                 log_link_warning_errno(link, r, "Could not set default route: %m");
                 link_enter_failed(link);
@@ -204,7 +229,7 @@ static int ndisc_router_process_autonomous_prefix(Link *link, sd_ndisc_router *r
         if (address->cinfo.ifa_valid == 0)
                 return 0;
 
-        r = address_configure(address, link, ndisc_netlink_message_handler, true);
+        r = address_configure(address, link, ndisc_netlink_address_message_handler, true);
         if (r < 0) {
                 log_link_warning_errno(link, r, "Could not set SLAAC address: %m");
                 link_enter_failed(link);
@@ -254,7 +279,7 @@ static int ndisc_router_process_onlink_prefix(Link *link, sd_ndisc_router *rt) {
         if (r < 0)
                 return log_link_error_errno(link, r, "Failed to get prefix address: %m");
 
-        r = route_configure(route, link, ndisc_netlink_message_handler);
+        r = route_configure(route, link, ndisc_netlink_route_message_handler);
         if (r < 0) {
                 log_link_warning_errno(link, r, "Could not set prefix route: %m");
                 link_enter_failed(link);
@@ -315,7 +340,7 @@ static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt) {
         if (r < 0)
                 return log_link_error_errno(link, r, "Failed to get route address: %m");
 
-        r = route_configure(route, link, ndisc_netlink_message_handler);
+        r = route_configure(route, link, ndisc_netlink_route_message_handler);
         if (r < 0) {
                 log_link_warning_errno(link, r, "Could not set additional route: %m");
                 link_enter_failed(link);
diff --git a/test/test-network/conf/dhcp-client-reassign-static-routes-ipv6.network b/test/test-network/conf/dhcp-client-reassign-static-routes-ipv6.network
new file mode 100644 (file)
index 0000000..c662e99
--- /dev/null
@@ -0,0 +1,8 @@
+[Match]
+Name=veth99
+
+[Network]
+IPv6AcceptRA=yes
+
+[Route]
+Destination=2600:0:0:1::/64
index 272d5453fe768246ac6959decf6b2624cb8b888d..399824af5c4f17603be98684c85451acbd00921f 100755 (executable)
@@ -2202,6 +2202,8 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
         'dhcp-client-keep-configuration-dhcp-on-stop.network',
         'dhcp-client-keep-configuration-dhcp.network',
         'dhcp-client-listen-port.network',
+        'dhcp-client-reassign-static-routes-ipv4.network',
+        'dhcp-client-reassign-static-routes-ipv6.network',
         'dhcp-client-route-metric.network',
         'dhcp-client-route-table.network',
         'dhcp-client-use-dns-ipv4-and-ra.network',
@@ -2406,9 +2408,9 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
         print(output)
         self.assertRegex(output, 'metric 24')
 
-    def test_dhcp_client_use_routes_no(self):
+    def test_dhcp_client_reassign_static_routes_ipv4(self):
         copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network',
-                                        'dhcp-client-use-routes-no.network')
+                                        'dhcp-client-reassign-static-routes-ipv4.network')
         start_networkd()
         wait_online(['veth-peer:carrier'])
         start_dnsmasq(lease_time='2m')
@@ -2425,6 +2427,9 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
         self.assertRegex(output, r'192.168.6.0/24 proto static')
         self.assertRegex(output, r'192.168.7.0/24 proto static')
 
+        stop_dnsmasq(dnsmasq_pid_file)
+        start_dnsmasq(ipv4_range='192.168.5.210,192.168.5.220', lease_time='2m')
+
         # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120
         print('Wait for the dynamic address to be renewed')
         time.sleep(125)
@@ -2438,6 +2443,37 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
         self.assertRegex(output, r'192.168.6.0/24 proto static')
         self.assertRegex(output, r'192.168.7.0/24 proto static')
 
+    def test_dhcp_client_reassign_static_routes_ipv6(self):
+        copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network',
+                                        'dhcp-client-reassign-static-routes-ipv6.network')
+        start_networkd()
+        wait_online(['veth-peer:carrier'])
+        start_dnsmasq(lease_time='2m')
+        wait_online(['veth99:routable', 'veth-peer:routable'])
+
+        output = check_output('ip address show dev veth99 scope global')
+        print(output)
+        self.assertRegex(output, r'inet6 2600::[0-9a-f]*/128 scope global (?:noprefixroute dynamic|dynamic noprefixroute)')
+
+        output = check_output('ip -6 route show dev veth99')
+        print(output)
+        self.assertRegex(output, r'2600::/64 proto ra metric 1024')
+        self.assertRegex(output, r'2600:0:0:1::/64 proto static metric 1024 pref medium')
+
+        stop_dnsmasq(dnsmasq_pid_file)
+        start_dnsmasq(ipv6_range='2600::30,2600::40', lease_time='2m')
+
+        # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120
+        print('Wait for the dynamic address to be renewed')
+        time.sleep(125)
+
+        wait_online(['veth99:routable'])
+
+        output = check_output('ip -6 route show dev veth99')
+        print(output)
+        self.assertRegex(output, r'2600::/64 proto ra metric 1024')
+        self.assertRegex(output, r'2600:0:0:1::/64 proto static metric 1024 pref medium')
+
     def test_dhcp_keep_configuration_dhcp(self):
         copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-v4-server-veth-peer.network', 'dhcp-client-keep-configuration-dhcp.network')
         start_networkd()