From: Yu Watanabe Date: Sun, 26 Jan 2025 22:07:55 +0000 (+0900) Subject: network/dhcp4: Gateway=_dhcp4 also assign DHCP address as preferred source X-Git-Tag: v258-rc1~1419^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7befabafad9992744a186858b3a9c43aba9578a6;p=thirdparty%2Fsystemd.git network/dhcp4: Gateway=_dhcp4 also assign DHCP address as preferred source With the following, now preferred source address is set to the DHCP address. ==== [Route] Gatewa=_dhcp4 Table=100 ==== Before: ==== $ ip route show table 100 default default via 192.168.0.1 dev eth0 proto dhcp metric 1024 ==== After: ==== $ ip route show table 100 default default via 192.168.0.1 dev eth0 proto dhcp src 192.168.0.100 metric 1024 ==== To avoid the assignment, this also introduces PreferredSource=no. --- diff --git a/man/systemd.network.xml b/man/systemd.network.xml index 126accaca9b..186c0250b28 100644 --- a/man/systemd.network.xml +++ b/man/systemd.network.xml @@ -2010,8 +2010,10 @@ NFTSet=prefix:netdev:filter:eth_ipv4_prefix Gateway= Takes the gateway address or the special values _dhcp4 and - _ipv6ra. If _dhcp4 or _ipv6ra is - set, then the gateway address provided by DHCPv4 or IPv6 RA is used. + _ipv6ra. If _dhcp4 or _ipv6ra is set, then + the gateway address provided by DHCPv4 or IPv6 RA is used. When_dhcp4, the + acquired DHCPv4 address will be used as the preferred source address of the route, unless it is + explicitly configured in PreferredSource=. @@ -2117,10 +2119,12 @@ NFTSet=prefix:netdev:filter:eth_ipv4_prefix PreferredSource= - The preferred source address of the route. The address must be in the format described - in + The preferred source address of the route. Takes no or an address + in the format described in inet_pton3. - + If Gateway=_dhcp4 is specified, defaults to the acquired DHCPv4 address. + Otherwise, defaults to unset. The value no may be useful to configure a route + with Gateway=_dhcp4 without setting preferred source route address. diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index 949f08cf53f..926e769258b 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -681,6 +681,12 @@ static int dhcp4_request_semi_static_routes(Link *link) { route->nexthop.gw.in = gw; + if (!route->prefsrc_set) { + r = sd_dhcp_lease_get_address(link->dhcp_lease, &route->prefsrc.in); + if (r < 0) + return r; + } + r = dhcp4_request_prefix_route(link, route); if (r < 0) return r; diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c index ff93c1d27de..e41b4c31be1 100644 --- a/src/network/networkd-route.c +++ b/src/network/networkd-route.c @@ -1663,7 +1663,19 @@ static int config_parse_preferred_src( Route *route = ASSERT_PTR(userdata); int r; - assert(rvalue); + if (isempty(rvalue)) { + route->prefsrc_set = false; + route->prefsrc = IN_ADDR_NULL; + return 1; + } + + r = parse_boolean(rvalue); + if (r == 0) { + /* Accepts only no. That prohibits prefsrc set by DHCP lease. */ + route->prefsrc_set = true; + route->prefsrc = IN_ADDR_NULL; + return 1; + } if (route->family == AF_UNSPEC) r = in_addr_from_string_auto(rvalue, &route->family, &route->prefsrc); @@ -1672,6 +1684,7 @@ static int config_parse_preferred_src( if (r < 0) return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); + route->prefsrc_set = true; return 1; } diff --git a/src/network/networkd-route.h b/src/network/networkd-route.h index b9d2dbf9a6f..46bbe2899e0 100644 --- a/src/network/networkd-route.h +++ b/src/network/networkd-route.h @@ -71,6 +71,7 @@ struct Route { bool expiration_managed_by_kernel:1; /* RTA_CACHEINFO has nonzero rta_expires */ /* Only used by conf persers and route_section_verify(). */ + bool prefsrc_set:1; bool scope_set:1; bool table_set:1; bool priority_set:1;