1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-19 22:50:17 +03:00

Merge pull request #12971 from yuwata/network-reassign-static-routes

network: reassign static routes and process reply address messages
This commit is contained in:
Yu Watanabe 2019-07-12 09:36:16 +09:00 committed by GitHub
commit 6fa0524133
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 100 additions and 20 deletions

View 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

View 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);

View 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) {

View 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;
}

View 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);

View File

@ -0,0 +1,8 @@
[Match]
Name=veth99
[Network]
IPv6AcceptRA=yes
[Route]
Destination=2600:0:0:1::/64

View File

@ -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()