From: Yu Watanabe Date: Mon, 24 Jul 2023 14:34:18 +0000 (+0900) Subject: network/dhcp4: always honor specified gateway address X-Git-Tag: v255-rc1~900^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4001653ddb2067978af3c0aff59e41019559cd57;p=thirdparty%2Fsystemd.git network/dhcp4: always honor specified gateway address Follow-up for 77451f654a89d822cd288883edfac315949d1cb6. Now, gateway for routes to DNS or NTP servers should be correctly picked, hence it is not necessary to adjust the gateway address in dhcp4_request_route_auto() again. Also, similar for classless static routes, let's always honor gateway address specified in (non-classless) static routes. --- diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index ca93c6773f0..dedc0a00151 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -454,12 +454,10 @@ static int dhcp4_request_route_to_gateway(Link *link, const struct in_addr *gw) static int dhcp4_request_route_auto( Route *in, Link *link, - const struct in_addr *gw, - bool force_use_gw) { + const struct in_addr *gw) { _cleanup_(route_freep) Route *route = in; - struct in_addr address, prefix; - uint8_t prefixlen; + struct in_addr address; int r; assert(route); @@ -471,10 +469,6 @@ static int dhcp4_request_route_auto( if (r < 0) return r; - r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen); - if (r < 0) - return r; - if (in4_addr_is_localhost(&route->dst.in)) { if (in4_addr_is_set(gw)) log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is localhost, " @@ -497,25 +491,23 @@ static int dhcp4_request_route_auto( route->gw = IN_ADDR_NULL; route->prefsrc.in = address; - } else if (!force_use_gw && - dhcp4_prefix_covers(link, &route->dst.in, route->dst_prefixlen) > 0) { - if (in4_addr_is_set(gw)) - log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is in the assigned network " - IPV4_ADDRESS_FMT_STR"/%u, ignoring gateway address "IPV4_ADDRESS_FMT_STR, - IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, - IPV4_ADDRESS_FMT_VAL(prefix), prefixlen, - IPV4_ADDRESS_FMT_VAL(*gw)); + } else if (in4_addr_is_null(gw)) { + r = dhcp4_prefix_covers(link, &route->dst.in, route->dst_prefixlen); + if (r < 0) + return r; + if (r == 0 && DEBUG_LOGGING) { + struct in_addr prefix; + uint8_t prefixlen; - route->scope = RT_SCOPE_LINK; - route->gw_family = AF_UNSPEC; - route->gw = IN_ADDR_NULL; - route->prefsrc.in = address; + r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen); + if (r < 0) + return r; - } else if (in4_addr_is_null(gw)) { - log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is not in the assigned network " - IPV4_ADDRESS_FMT_STR"/%u, but no gateway is specified, using 'link' scope.", - IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, - IPV4_ADDRESS_FMT_VAL(prefix), prefixlen); + log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is not in the assigned network " + IPV4_ADDRESS_FMT_STR"/%u, but no gateway is specified, using 'link' scope.", + IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, + IPV4_ADDRESS_FMT_VAL(prefix), prefixlen); + } route->scope = RT_SCOPE_LINK; route->gw_family = AF_UNSPEC; @@ -621,9 +613,7 @@ static int dhcp4_request_static_routes(Link *link, struct in_addr *ret_default_g in4_addr_is_null(&default_gw)) default_gw = gw; - /* Do not ignore the gateway given by the classless route option even if the destination is - * in the same network. See issue #28280. */ - r = dhcp4_request_route_auto(TAKE_PTR(route), link, &gw, /* force_use_gw = */ is_classless); + r = dhcp4_request_route_auto(TAKE_PTR(route), link, &gw); if (r < 0) return r; } @@ -770,7 +760,7 @@ static int dhcp4_request_routes_to_servers( route->dst.in = *dst; route->dst_prefixlen = 32; - r = dhcp4_request_route_auto(TAKE_PTR(route), link, &gw, /* force_use_gw = */ false); + r = dhcp4_request_route_auto(TAKE_PTR(route), link, &gw); if (r < 0) return r; } diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index f2e2279124c..766abab819b 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -4993,7 +4993,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): additional_options = [ '--dhcp-option=option:dns-server,192.168.5.10,8.8.8.8', '--dhcp-option=option:ntp-server,192.168.5.11,9.9.9.9', - '--dhcp-option=option:static-route,192.168.5.100,192.168.5.2,8.8.8.8,192.168.5.3' + '--dhcp-option=option:static-route,192.168.6.100,192.168.5.2,8.8.8.8,192.168.5.3' ] if classless: additional_options += [ @@ -5014,16 +5014,18 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): self.assertRegex(output, r'192.168.5.4 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') self.assertRegex(output, r'192.168.5.5 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') else: - self.assertRegex(output, r'192.168.5.0/24 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') + self.assertRegex(output, r'192.168.6.0/24 via 192.168.5.2 proto dhcp src 192.168.5.[0-9]* metric 1024') self.assertRegex(output, r'8.0.0.0/8 via 192.168.5.3 proto dhcp src 192.168.5.[0-9]* metric 1024') + self.assertRegex(output, r'192.168.5.2 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') self.assertRegex(output, r'192.168.5.3 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') else: self.assertNotRegex(output, r'default via 192.168.5.4 proto dhcp src 192.168.5.[0-9]* metric 1024') self.assertNotRegex(output, r'8.0.0.0/8 via 192.168.5.5 proto dhcp src 192.168.5.[0-9]* metric 1024') self.assertNotRegex(output, r'192.168.5.4 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') self.assertNotRegex(output, r'192.168.5.5 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') - self.assertNotRegex(output, r'192.168.5.0/24 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') + self.assertNotRegex(output, r'192.168.6.0/24 via 192.168.5.2 proto dhcp src 192.168.5.[0-9]* metric 1024') self.assertNotRegex(output, r'8.0.0.0/8 via 192.168.5.3 proto dhcp src 192.168.5.[0-9]* metric 1024') + self.assertNotRegex(output, r'192.168.5.2 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') self.assertNotRegex(output, r'192.168.5.3 proto dhcp scope link src 192.168.5.[0-9]* metric 1024') # Check UseGateway=