HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/network/networkd-dhcp4.c

78,999 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: abf81103405ac09577598bbb04aa7dc856bbe8e1

Lines 1–1000 of 2,118

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <linux/if_arp.h>
#include <linux/pkt_sched.h>
#include <linux/rtnetlink.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
 
#include "sd-dhcp-protocol.h"
#include "sd-ipv4ll.h"
 
#include "alloc-util.h"
#include "conf-parser.h"
#include "device-private.h"
#include "dhcp-client-internal.h"
#include "errno-util.h"
#include "hostname-setup.h"
#include "networkd-address.h"
#include "networkd-dhcp-prefix-delegation.h"
#include "networkd-dhcp4.h"
#include "networkd-dhcp4-bus.h"
#include "networkd-ipv4ll.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-ntp.h"
#include "networkd-queue.h"
#include "networkd-route.h"
#include "networkd-setlink.h"
#include "networkd-state-file.h"
#include "networkd-wwan.h"
#include "parse-util.h"
#include "set.h"
#include "socket-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
 
void network_adjust_dhcp4(Network *network) {
        assert(network);
 
        if (!FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4))
                return;
 
        if (network->dhcp_use_gateway < 0)
                network->dhcp_use_gateway = network->dhcp_use_routes;
 
        /* RFC7844 section 3.: MAY contain the Client Identifier option
         * Section 3.5: clients MUST use client identifiers based solely on the link-layer address
         * NOTE: Using MAC, as it does not reveal extra information, and some servers might not answer
         * if this option is not sent */
        if (network->dhcp_anonymize &&
            network->dhcp_client_identifier >= 0 &&
            network->dhcp_client_identifier != DHCP_CLIENT_ID_MAC) {
                log_warning("%s: ClientIdentifier= is set, although Anonymize=yes. Using ClientIdentifier=mac.",
                            network->filename);
                network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC;
        }
 
        if (network->dhcp_client_identifier < 0)
                network->dhcp_client_identifier = network->dhcp_anonymize ? DHCP_CLIENT_ID_MAC : network->manager->dhcp_client_identifier;
 
        /* By default, RapidCommit= is enabled when Anonymize=no and neither AllowList= nor DenyList= is specified. */
        if (network->dhcp_use_rapid_commit < 0)
                network->dhcp_use_rapid_commit =
                        !network->dhcp_anonymize &&
                        set_isempty(network->dhcp_allow_listed_ip) &&
                        set_isempty(network->dhcp_deny_listed_ip);
}
 
static int dhcp4_prefix_covers(
                Link *link,
                const struct in_addr *in_prefix,
                uint8_t in_prefixlen) {
 
        struct in_addr prefix;
        uint8_t prefixlen;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(in_prefix);
 
        /* Return true if the input address or address range is in the assigned network.
         * E.g. if the DHCP server provides 192.168.0.100/24, then this returns true for the address or
         * address range in 192.168.0.0/24, and returns false otherwise. */
 
        r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen);
        if (r < 0)
                return r;
 
        return in4_addr_prefix_covers_full(&prefix, prefixlen, in_prefix, in_prefixlen);
}
 
static int dhcp4_get_router(Link *link, struct in_addr *ret) {
        const struct in_addr *routers;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(ret);
 
        r = sd_dhcp_lease_get_router(link->dhcp_lease, &routers);
        if (r < 0)
                return r;
 
        /* The router option may provide multiple routers, We only use the first non-null address. */
 
        FOREACH_ARRAY(router, routers, r) {
                if (in4_addr_is_null(router))
                        continue;
 
                *ret = *router;
                return 0;
        }
 
        return -ENODATA;
}
 
static int dhcp4_get_classless_static_or_static_routes(Link *link, sd_dhcp_route ***ret_routes, size_t *ret_num) {
        _cleanup_free_ sd_dhcp_route **routes = NULL;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
 
        /* If the DHCP server returns both a Classless Static Routes option and a Static Routes option,
         * the DHCP client MUST ignore the Static Routes option. */
 
        r = sd_dhcp_lease_get_classless_routes(link->dhcp_lease, &routes);
        if (r >= 0) {
                assert(r > 0);
                if (ret_routes)
                        *ret_routes = TAKE_PTR(routes);
                if (ret_num)
                        *ret_num = r;
                return 1; /* classless */
        } else if (r != -ENODATA)
                return r;
 
        r = sd_dhcp_lease_get_static_routes(link->dhcp_lease, &routes);
        if (r < 0)
                return r;
 
        assert(r > 0);
        if (ret_routes)
                *ret_routes = TAKE_PTR(routes);
        if (ret_num)
                *ret_num = r;
        return 0; /* static */
}
 
static int dhcp4_find_gateway_for_destination(
                Link *link,
                const struct in_addr *destination,
                uint8_t prefixlength,
                struct in_addr *ret) {
 
        _cleanup_free_ sd_dhcp_route **routes = NULL;
        size_t n_routes = 0;
        bool is_classless;
        uint8_t max_prefixlen = UINT8_MAX;
        struct in_addr gw;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(destination);
        assert(ret);
 
        /* This tries to find the most suitable gateway for an address or address range.
         * E.g. if the server provides the default gateway 192.168.0.1 and a classless static route for
         * 8.0.0.0/8 with gateway 192.168.0.2, then this returns 192.168.0.2 for 8.8.8.8/32, and 192.168.0.1
         * for 9.9.9.9/32. If the input address or address range is in the assigned network, then the null
         * address will be returned. */
 
        /* First, check with the assigned prefix, and if the destination is in the prefix, set the null
         * address for the gateway, and return it unless more suitable static route is found. */
        r = dhcp4_prefix_covers(link, destination, prefixlength);
        if (r < 0)
                return r;
        if (r > 0) {
                r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &max_prefixlen);
                if (r < 0)
                        return r;
 
                gw = (struct in_addr) {};
        }
 
        r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes);
        if (r < 0 && r != -ENODATA)
                return r;
        is_classless = r > 0;
 
        /* First, find most suitable gateway. */
        FOREACH_ARRAY(e, routes, n_routes) {
                struct in_addr dst;
                uint8_t len;
 
                r = sd_dhcp_route_get_destination(*e, &dst);
                if (r < 0)
                        return r;
 
                r = sd_dhcp_route_get_destination_prefix_length(*e, &len);
                if (r < 0)
                        return r;
 
                r = in4_addr_prefix_covers_full(&dst, len, destination, prefixlength);
                if (r < 0)
                        return r;
                if (r == 0)
                        continue;
 
                if (max_prefixlen != UINT8_MAX && max_prefixlen > len)
                        continue;
 
                r = sd_dhcp_route_get_gateway(*e, &gw);
                if (r < 0)
                        return r;
 
                max_prefixlen = len;
        }
 
        /* The destination is reachable. Note, the gateway address returned here may be NULL. */
        if (max_prefixlen != UINT8_MAX) {
                *ret = gw;
                return 0;
        }
 
        /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
         * a Router option, the DHCP client MUST ignore the Router option. */
        if (!is_classless) {
                /* No matching static route is found, and the destination is not in the acquired network,
                 * falling back to the Router option. */
                r = dhcp4_get_router(link, ret);
                if (r >= 0)
                        return 0;
                if (r != -ENODATA)
                        return r;
        }
 
        return -EHOSTUNREACH; /* Cannot reach the destination. */
}
 
static int dhcp4_remove_address_and_routes(Link *link, bool only_marked) {
        Address *address;
        Route *route;
        int ret = 0;
 
        assert(link);
        assert(link->manager);
 
        SET_FOREACH(route, link->manager->routes) {
                if (route->source != NETWORK_CONFIG_SOURCE_DHCP4)
                        continue;
                if (route->nexthop.ifindex != link->ifindex)
                        continue;
                if (only_marked && !route_is_marked(route))
                        continue;
 
                RET_GATHER(ret, route_remove_and_cancel(route, link->manager));
        }
 
        SET_FOREACH(address, link->addresses) {
                if (address->source != NETWORK_CONFIG_SOURCE_DHCP4)
                        continue;
                if (only_marked && !address_is_marked(address))
                        continue;
 
                RET_GATHER(ret, address_remove_and_cancel(address, link));
        }
 
        if (!only_marked)
                /* Also drop pending requests when the lease is lost or the client is stopped. */
                RET_GATHER(ret, link_drop_requests(link, NETWORK_CONFIG_SOURCE_DHCP4));
 
        return ret;
}
 
static int dhcp4_address_get(Link *link, Address **ret) {
        Address *address;
 
        assert(link);
 
        SET_FOREACH(address, link->addresses) {
                if (address->source != NETWORK_CONFIG_SOURCE_DHCP4)
                        continue;
                if (address_is_marked(address))
                        continue;
 
                if (ret)
                        *ret = address;
                return 0;
        }
 
        return -ENOENT;
}
 
static int dhcp4_address_ready_callback(Address *address) {
        assert(address);
        assert(address->link);
 
        /* Do not call this again. */
        address->callback = NULL;
 
        return dhcp4_check_ready(address->link);
}
 
int dhcp4_check_ready(Link *link) {
        Address *address;
        int r;
 
        assert(link);
 
        if (link->dhcp4_messages > 0) {
                log_link_debug(link, "%s(): DHCPv4 address and routes are not set.", __func__);
                return 0;
        }
 
        if (dhcp4_address_get(link, &address) < 0) {
                log_link_debug(link, "%s(): DHCPv4 address is not set.", __func__);
                return 0;
        }
 
        if (!address_is_ready(address)) {
                log_link_debug(link, "%s(): DHCPv4 address is not ready.", __func__);
                address->callback = dhcp4_address_ready_callback;
                return 0;
        }
 
        link->dhcp4_configured = true;
        log_link_debug(link, "DHCPv4 address and routes set.");
 
        /* New address and routes are configured now. Let's release old lease. */
        r = dhcp4_remove_address_and_routes(link, /* only_marked= */ true);
        if (r < 0)
                return r;
 
        r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_DHCP4);
        if (r < 0)
                return r;
 
        r = sd_ipv4ll_stop(link->ipv4ll);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address: %m");
 
        link_check_ready(link);
        return 0;
}
 
static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) {
        int r;
 
        assert(req);
        assert(link);
        assert(route);
 
        r = route_configure_handler_internal(m, req, route);
        if (r <= 0)
                return r;
 
        r = dhcp4_check_ready(link);
        if (r < 0)
                link_enter_failed(link);
 
        return 1;
}
 
static int dhcp4_request_route(Route *route, Link *link) {
        struct in_addr server;
        Route *existing;
        int r;
 
        assert(route);
        assert(link);
        assert(link->manager);
        assert(link->network);
        assert(link->dhcp_lease);
 
        r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server);
        if (r < 0)
                return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
 
        route->source = NETWORK_CONFIG_SOURCE_DHCP4;
        route->provider.in = server;
        route->family = AF_INET;
        if (!route->protocol_set)
                route->protocol = RTPROT_DHCP;
        if (!route->priority_set)
                route->priority = link->network->dhcp_route_metric;
        if (!route->table_set)
                route->table = link_get_dhcp4_route_table(link);
        r = route_metric_set(&route->metric, RTAX_MTU, link->network->dhcp_route_mtu);
        if (r < 0)
                return r;
        r = route_metric_set(&route->metric, RTAX_INITCWND, link->network->dhcp_initial_congestion_window);
        if (r < 0)
                return r;
        r = route_metric_set(&route->metric, RTAX_INITRWND, link->network->dhcp_advertised_receive_window);
        if (r < 0)
                return r;
        r = route_metric_set(&route->metric, RTAX_QUICKACK, link->network->dhcp_quickack);
        if (r < 0)
                return r;
 
        r = route_adjust_nexthops(route, link);
        if (r < 0)
                return r;
 
        if (route_get(link->manager, route, &existing) < 0) /* This is a new route. */
                link->dhcp4_configured = false;
        else
                route_unmark(existing);
 
        return link_request_route(link, route, &link->dhcp4_messages, dhcp4_route_handler);
}
 
static bool prefixroute_by_kernel(Link *link) {
        return !link->network->dhcp_route_table_set ||
                link->network->dhcp_route_table == RT_TABLE_MAIN;
}
 
static int dhcp4_request_prefix_route(Link *link, Route *rt) {
        _cleanup_(route_unrefp) Route *route = NULL;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
 
        if (prefixroute_by_kernel(link) && (!rt || !rt->table_set || rt->table == RT_TABLE_MAIN))
                /* The prefix route in the main table will be created by the kernel. See dhcp4_update_address(). */
                return 0;
 
        r = route_new(&route);
        if (r < 0)
                return r;
 
        route->scope = RT_SCOPE_LINK;
        if (rt) {
                route->table_set = rt->table_set;
                route->table = rt->table;
        }
 
        r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &route->dst.in, &route->dst_prefixlen);
        if (r < 0)
                return r;
 
        r = sd_dhcp_lease_get_address(link->dhcp_lease, &route->prefsrc.in);
        if (r < 0)
                return r;
 
        return dhcp4_request_route(route, link);
}
 
static int dhcp4_request_route_to_gateway(Link *link, const Route *rt) {
        _cleanup_(route_unrefp) Route *route = NULL;
        struct in_addr address;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(rt);
 
        if (in_addr_is_set(rt->nexthop.family, &rt->nexthop.gw) <= 0)
                return 0;
 
        assert(rt->nexthop.family == AF_INET);
 
        r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
        if (r < 0)
                return r;
 
        r = route_new(&route);
        if (r < 0)
                return r;
 
        route->dst.in = rt->nexthop.gw.in;
        route->dst_prefixlen = 32;
        route->prefsrc.in = address;
        route->scope = RT_SCOPE_LINK;
        route->table = rt->table;
        route->table_set = rt->table_set;
 
        return dhcp4_request_route(route, link);
}
 
static int dhcp4_request_route_auto(
                Route *route,
                Link *link,
                const struct in_addr *gw) {
 
        struct in_addr address;
        int r;
 
        assert(route);
        assert(link);
        assert(link->dhcp_lease);
        assert(gw);
 
        r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
        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, "
                                       "ignoring gateway address "IPV4_ADDRESS_FMT_STR,
                                       IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw));
 
                route->scope = RT_SCOPE_HOST;
                route->nexthop.family = AF_UNSPEC;
                route->nexthop.gw = IN_ADDR_NULL;
                route->prefsrc = IN_ADDR_NULL;
 
        } else if (route->dst_prefixlen == 32 && in4_addr_equal(&route->dst.in, &address)) {
                /* The destination is masked, hence a larger network also compares equal when the acquired
                 * address has no host bits set, e.g. the lower address of a /31. See RFC 3021. */
                if (in4_addr_is_set(gw))
                        log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is equivalent to the acquired address, "
                                       "ignoring gateway address "IPV4_ADDRESS_FMT_STR,
                                       IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw));
 
                route->scope = RT_SCOPE_HOST;
                route->nexthop.family = AF_UNSPEC;
                route->nexthop.gw = IN_ADDR_NULL;
                route->prefsrc.in = address;
 
        } 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;
 
                        r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen);
                        if (r < 0)
                                return r;
 
                        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->nexthop.family = AF_UNSPEC;
                route->nexthop.gw = IN_ADDR_NULL;
                route->prefsrc.in = address;
 
        } else {
                route->scope = RT_SCOPE_UNIVERSE;
                route->nexthop.family = AF_INET;
                route->nexthop.gw.in = *gw;
                route->prefsrc.in = address;
 
                r = dhcp4_request_route_to_gateway(link, route);
                if (r < 0)
                        return r;
        }
 
        return dhcp4_request_route(route, link);
}
 
static int dhcp4_request_classless_static_or_static_routes(Link *link) {
        _cleanup_free_ sd_dhcp_route **routes = NULL;
        size_t n_routes;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
 
        if (!link->network->dhcp_use_routes)
                return 0;
 
        r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes);
        if (r == -ENODATA)
                return 0;
        if (r < 0)
                return r;
 
        FOREACH_ARRAY(e, routes, n_routes) {
                _cleanup_(route_unrefp) Route *route = NULL;
                struct in_addr gw;
 
                r = route_new(&route);
                if (r < 0)
                        return r;
 
                r = sd_dhcp_route_get_gateway(*e, &gw);
                if (r < 0)
                        return r;
 
                r = sd_dhcp_route_get_destination(*e, &route->dst.in);
                if (r < 0)
                        return r;
 
                r = sd_dhcp_route_get_destination_prefix_length(*e, &route->dst_prefixlen);
                if (r < 0)
                        return r;
 
                r = dhcp4_request_route_auto(route, link, &gw);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int dhcp4_request_default_gateway(Link *link) {
        _cleanup_(route_unrefp) Route *route = NULL;
        struct in_addr address, router;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
 
        if (!link->network->dhcp_use_gateway)
                return 0;
 
        /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
         * a Router option, the DHCP client MUST ignore the Router option. */
        if (link->network->dhcp_use_routes &&
            dhcp4_get_classless_static_or_static_routes(link, NULL, NULL) > 0)
                return 0;
 
        r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
        if (r < 0)
                return r;
 
        r = dhcp4_get_router(link, &router);
        if (r == -ENODATA) {
                log_link_debug(link, "DHCP: No valid router address received from DHCP server.");
                return 0;
        }
        if (r < 0)
                return r;
 
        r = route_new(&route);
        if (r < 0)
                return r;
 
        /* Next, add a default gateway. */
        route->nexthop.family = AF_INET;
        route->nexthop.gw.in = router;
        route->prefsrc.in = address;
 
        /* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host
         * so that we can route no matter the netmask or existing kernel route tables. */
        r = dhcp4_request_route_to_gateway(link, route);
        if (r < 0)
                return r;
 
        return dhcp4_request_route(route, link);
}
 
static int dhcp4_request_semi_static_routes(Link *link) {
        Route *rt;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(link->network);
 
        HASHMAP_FOREACH(rt, link->network->routes_by_section) {
                _cleanup_(route_unrefp) Route *route = NULL;
                struct in_addr gw;
 
                if (rt->source != NETWORK_CONFIG_SOURCE_DHCP4)
                        continue;
 
                assert(rt->family == AF_INET);
                assert(rt->nexthop.family == AF_INET);
 
                r = dhcp4_find_gateway_for_destination(link, &rt->dst.in, rt->dst_prefixlen, &gw);
                if (r == -EHOSTUNREACH) {
                        log_link_debug_errno(link, r, "DHCP: Cannot find suitable gateway for destination %s of semi-static route, ignoring: %m",
                                             IN4_ADDR_PREFIX_TO_STRING(&rt->dst.in, rt->dst_prefixlen));
                        continue;
                }
                if (r < 0)
                        return r;
 
                if (in4_addr_is_null(&gw)) {
                        log_link_debug(link, "DHCP: Destination %s of semi-static route is in the acquired network, skipping configuration.",
                                       IN4_ADDR_PREFIX_TO_STRING(&rt->dst.in, rt->dst_prefixlen));
                        continue;
                }
 
                r = route_dup(rt, NULL, &route);
                if (r < 0)
                        return r;
 
                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;
 
                r = dhcp4_request_route_to_gateway(link, route);
                if (r < 0)
                        return r;
 
                r = dhcp4_request_route(route, link);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int dhcp4_request_routes_to_servers(
                Link *link,
                const struct in_addr *servers,
                size_t n_servers) {
 
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(link->network);
        assert(servers || n_servers == 0);
 
        FOREACH_ARRAY(dst, servers, n_servers) {
                _cleanup_(route_unrefp) Route *route = NULL;
                struct in_addr gw;
 
                if (in4_addr_is_null(dst))
                        continue;
 
                r = dhcp4_find_gateway_for_destination(link, dst, 32, &gw);
                if (r == -EHOSTUNREACH) {
                        log_link_debug_errno(link, r, "DHCP: Cannot find suitable gateway for destination %s, ignoring: %m",
                                             IN4_ADDR_PREFIX_TO_STRING(dst, 32));
                        continue;
                }
                if (r < 0)
                        return r;
 
                r = route_new(&route);
                if (r < 0)
                        return r;
 
                route->dst.in = *dst;
                route->dst_prefixlen = 32;
 
                r = dhcp4_request_route_auto(route, link, &gw);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int dhcp4_request_routes_to_dns(Link *link) {
        const struct in_addr *dns;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(link->network);
 
        if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4) ||
            !link->network->dhcp_routes_to_dns)
                return 0;
 
        r = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
        if (IN_SET(r, 0, -ENODATA))
                return 0;
        if (r < 0)
                return r;
 
        return dhcp4_request_routes_to_servers(link, dns, r);
}
 
static int dhcp4_request_routes_to_ntp(Link *link) {
        const struct in_addr *ntp;
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(link->network);
 
        if (!link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4) ||
            !link->network->dhcp_routes_to_ntp)
                return 0;
 
        r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp);
        if (IN_SET(r, 0, -ENODATA))
                return 0;
        if (r < 0)
                return r;
 
        return dhcp4_request_routes_to_servers(link, ntp, r);
}
 
static int dhcp4_request_routes(Link *link) {
        int r;
 
        assert(link);
        assert(link->dhcp_lease);
 
        r = dhcp4_request_prefix_route(link, /* rt= */ NULL);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m");
 
        r = dhcp4_request_default_gateway(link);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP error: Could not request default gateway: %m");
 
        r = dhcp4_request_classless_static_or_static_routes(link);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP error: Could not request static routes: %m");
 
        r = dhcp4_request_semi_static_routes(link);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m");
 
        r = dhcp4_request_routes_to_dns(link);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP error: Could not request routes to DNS servers: %m");
 
        r = dhcp4_request_routes_to_ntp(link);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP error: Could not request routes to NTP servers: %m");
 
        return 0;
}
 
static int dhcp_reset_mtu(Link *link) {
        int r;
 
        assert(link);
 
        if (!link->network->dhcp_use_mtu)
                return 0;
 
        r = link_request_to_set_mtu(link, link->original_mtu);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP error: Could not queue request to reset MTU: %m");
 
        return 0;
}
 
static int dhcp_reset_hostname(Link *link) {
        const char *hostname;
        int r;
 
        assert(link);
 
        if (!link->network->dhcp_use_hostname)
                return 0;
 
        hostname = link->network->dhcp_hostname;
        if (!hostname)
                (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
 
        if (!hostname)
                return 0;
 
        /* If a hostname was set due to the lease, then unset it now. */
        r = manager_set_hostname(link->manager, NULL);
        if (r < 0)
                return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
 
        return 0;
}
 
int dhcp4_lease_lost(Link *link) {
        int r = 0;
 
        assert(link);
        assert(link->dhcp_lease);
        assert(link->network);
 
        log_link_info(link, "DHCP lease lost");
 
        link->dhcp4_configured = false;
 
        if (link->network->dhcp_use_6rd &&
            sd_dhcp_lease_has_6rd(link->dhcp_lease))
                dhcp4_pd_prefix_lost(link);
 
        RET_GATHER(r, dhcp4_remove_address_and_routes(link, /* only_marked= */ false));
        RET_GATHER(r, dhcp_reset_mtu(link));
        RET_GATHER(r, dhcp_reset_hostname(link));
 
        link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
        link_dirty(link);
 
        /* If one of the above failed. Do not request nexthops and routes. */
        if (r < 0)
                return r;
 
        r = link_request_static_nexthops(link, true);
        if (r < 0)
                return r;
 
        return link_request_static_routes(link, true);
}
 
static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
        int r;
 
        assert(link);
        assert(address);
 
        r = address_configure_handler_internal(m, link, address);
        if (r <= 0)
                return r;
 
        r = dhcp4_check_ready(link);
        if (r < 0)
                link_enter_failed(link);
 
        return 1;
}
 
static int dhcp4_request_address(Link *link, bool announce) {
        _cleanup_(address_unrefp) Address *addr = NULL;
        struct in_addr address, server;
        uint8_t prefixlen;
        Address *existing;
        usec_t lifetime_usec;
        int r;
 
        assert(link);
        assert(link->manager);
        assert(link->network);
        assert(link->dhcp_lease);
 
        r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
        if (r < 0)
                return log_link_warning_errno(link, r, "DHCP error: no address: %m");
 
        r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &prefixlen);
        if (r < 0)
                return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
 
        r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server);
        if (r < 0)
                return log_link_debug_errno(link, r, "DHCP error: failed to get DHCP server IP address: %m");
 
        if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) {
                r = sd_dhcp_lease_get_lifetime_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &lifetime_usec);
                if (r < 0)
                        return log_link_warning_errno(link, r, "DHCP error: failed to get lifetime: %m");
        } else
                lifetime_usec = USEC_INFINITY;
 
        if (announce) {
                const struct in_addr *router;
 
                r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
                if (r < 0 && r != -ENODATA)
                        return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
 
                if (r > 0 && in4_addr_is_set(&router[0]))
                        log_struct(LOG_INFO,
                                   LOG_LINK_INTERFACE(link),
                                   LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u, gateway "IPV4_ADDRESS_FMT_STR" acquired from "IPV4_ADDRESS_FMT_STR,
                                                    IPV4_ADDRESS_FMT_VAL(address),
                                                    prefixlen,
                                                    IPV4_ADDRESS_FMT_VAL(router[0]),
                                                    IPV4_ADDRESS_FMT_VAL(server)),
                                   LOG_ITEM("ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address)),
                                   LOG_ITEM("PREFIXLEN=%u", prefixlen),
                                   LOG_ITEM("GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0])));
                else
                        log_struct(LOG_INFO,
                                   LOG_LINK_INTERFACE(link),
                                   LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u acquired from "IPV4_ADDRESS_FMT_STR,
                                                    IPV4_ADDRESS_FMT_VAL(address),
                                                    prefixlen,
                                                    IPV4_ADDRESS_FMT_VAL(server)),
                                   LOG_ITEM("ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address)),
                                   LOG_ITEM("PREFIXLEN=%u", prefixlen));
        }
 
        r = address_new(&addr);
        if (r < 0)
                return log_oom();
 
        addr->source = NETWORK_CONFIG_SOURCE_DHCP4;
        addr->provider.in = server;
        addr->family = AF_INET;
        addr->in_addr.in.s_addr = address.s_addr;
        addr->lifetime_preferred_usec = lifetime_usec;
        addr->lifetime_valid_usec = lifetime_usec;
        addr->prefixlen = prefixlen;
        r = sd_dhcp_lease_get_broadcast(link->dhcp_lease, &addr->broadcast);
        if (r < 0 && r != -ENODATA)