/* SPDX-License-Identifier: LGPL-2.1+ */
-#include <netinet/ether.h>
+#include <netinet/in.h>
#include <linux/if.h>
#include "alloc-util.h"
#include "hostname-util.h"
#include "parse-util.h"
-#include "netdev/vrf.h"
#include "network-internal.h"
#include "networkd-link.h"
#include "networkd-manager.h"
static int link_set_dhcp_routes(Link *link) {
_cleanup_free_ sd_dhcp_route **static_routes = NULL;
bool classless_route = false, static_route = false;
- struct in_addr gateway, address;
+ const struct in_addr *router;
+ struct in_addr address;
int r, n, i;
uint32_t table;
if (!link->network->dhcp_use_routes)
return 0;
- /* When the interface is part of an VRF use the VRFs routing table, unless
- * there is a another table specified. */
- table = link->network->dhcp_route_table;
- if (!link->network->dhcp_route_table_set && link->network->vrf != NULL)
- table = VRF(link->network->vrf)->table;
+ table = link_get_dhcp_route_table(link);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
link->dhcp4_messages++;
}
- r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
- if (r == -ENODATA)
- log_link_info_errno(link, r, "DHCP: No gateway received from DHCP server: %m");
+ r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
+ if (IN_SET(r, 0, -ENODATA))
+ log_link_info(link, "DHCP: No gateway received from DHCP server.");
else if (r < 0)
log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m");
+ else if (in4_addr_is_null(&router[0]))
+ log_link_info(link, "DHCP: Received gateway is null.");
/* 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 (classless_route && static_route)
log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option");
- if (r >= 0 && !classless_route) {
- _cleanup_(route_freep) Route *route = NULL;
- _cleanup_(route_freep) Route *route_gw = NULL;
-
- r = route_new(&route);
- if (r < 0)
- return log_link_error_errno(link, r, "Could not allocate route: %m");
-
- route->protocol = RTPROT_DHCP;
+ if (r > 0 && !classless_route && !in4_addr_is_null(&router[0])) {
+ _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
r = route_new(&route_gw);
if (r < 0)
* route for the gw host so that we can route no matter the
* netmask or existing kernel route tables. */
route_gw->family = AF_INET;
- route_gw->dst.in = gateway;
+ route_gw->dst.in = router[0];
route_gw->dst_prefixlen = 32;
route_gw->prefsrc.in = address;
route_gw->scope = RT_SCOPE_LINK;
link->dhcp4_messages++;
+ r = route_new(&route);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Could not allocate route: %m");
+
route->family = AF_INET;
- route->gw.in = gateway;
+ route->gw.in = router[0];
route->prefsrc.in = address;
+ route->protocol = RTPROT_DHCP;
route->priority = link->network->dhcp_route_metric;
route->table = table;
return 0;
}
-static int dhcp_lease_lost(Link *link) {
- _cleanup_(address_freep) Address *address = NULL;
- struct in_addr addr;
+static int dhcp_remove_routes(Link *link, struct in_addr *address) {
+ _cleanup_free_ sd_dhcp_route **routes = NULL;
+ uint32_t table;
+ int n, i, r;
+
+ assert(link);
+ assert(address);
+
+ if (!link->network->dhcp_use_routes)
+ return 0;
+
+ n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes);
+ if (IN_SET(n, 0, -ENODATA)) {
+ log_link_debug(link, "DHCP: No routes received from DHCP server: %m");
+ return 0;
+ } else if (n < 0)
+ return log_link_error_errno(link, n, "DHCP error: could not get routes: %m");
+
+ table = link_get_dhcp_route_table(link);
+
+ for (i = 0; i < n; i++) {
+ _cleanup_(route_freep) Route *route = NULL;
+
+ r = route_new(&route);
+ if (r < 0)
+ return log_oom();
+
+ route->family = AF_INET;
+ assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0);
+ assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0);
+ assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0);
+ route->priority = link->network->dhcp_route_metric;
+ route->table = table;
+ route->scope = route_scope_from_address(route, address);
+
+ (void) route_remove(route, link, NULL);
+ }
+
+ return n;
+}
+
+static int dhcp_remove_router(Link *link, struct in_addr *address) {
+ _cleanup_(route_freep) Route *route_gw = NULL, *route = NULL;
+ const struct in_addr *router;
+ uint32_t table;
+ int r;
+
+ assert(link);
+ assert(address);
+
+ if (!link->network->dhcp_use_routes)
+ return 0;
+
+ r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
+ if (IN_SET(r, 0, -ENODATA)) {
+ log_link_debug(link, "DHCP: No gateway received from DHCP server.");
+ return 0;
+ } else if (r < 0)
+ return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m");
+ else if (in4_addr_is_null(&router[0])) {
+ log_link_info(link, "DHCP: Received gateway is null, ignoring.");
+ return 0;
+ }
+
+ table = link_get_dhcp_route_table(link);
+
+ r = route_new(&route_gw);
+ if (r < 0)
+ return log_oom();
+
+ route_gw->family = AF_INET;
+ route_gw->dst.in = router[0];
+ route_gw->dst_prefixlen = 32;
+ route_gw->prefsrc.in = *address;
+ route_gw->scope = RT_SCOPE_LINK;
+ route_gw->protocol = RTPROT_DHCP;
+ route_gw->priority = link->network->dhcp_route_metric;
+ route_gw->table = table;
+
+ (void) route_remove(route_gw, link, NULL);
+
+ r = route_new(&route);
+ if (r < 0)
+ return log_oom();
+
+ route->family = AF_INET;
+ route->gw.in = router[0];
+ route->prefsrc.in = *address;
+ route->protocol = RTPROT_DHCP;
+ route->priority = link->network->dhcp_route_metric;
+ route->table = table;
+
+ (void) route_remove(route, link, NULL);
+
+ return 0;
+}
+
+static int dhcp_remove_address(Link *link, struct in_addr *address) {
+ _cleanup_(address_freep) Address *a = NULL;
struct in_addr netmask;
- struct in_addr gateway;
- unsigned prefixlen = 0;
int r;
assert(link);
- assert(link->dhcp_lease);
+ assert(address);
- log_link_warning(link, "DHCP lease lost");
+ if (in4_addr_is_null(address))
+ return 0;
- if (link->network->dhcp_use_routes) {
- _cleanup_free_ sd_dhcp_route **routes = NULL;
- int n, i;
+ r = address_new(&a);
+ if (r < 0)
+ return log_oom();
- n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes);
- if (n >= 0) {
- for (i = 0; i < n; i++) {
- _cleanup_(route_freep) Route *route = NULL;
+ a->family = AF_INET;
+ a->in_addr.in = *address;
- r = route_new(&route);
- if (r >= 0) {
- route->family = AF_INET;
- assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0);
- assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0);
- assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0);
+ if (sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask) >= 0)
+ a->prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
- route_remove(route, link, NULL);
- }
- }
- }
+ (void) address_remove(a, link, NULL);
+
+ return 0;
+}
+
+static int dhcp_reset_mtu(Link *link) {
+ uint16_t mtu;
+ int r;
+
+ assert(link);
+
+ if (!link->network->dhcp_use_mtu)
+ return 0;
+
+ r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
+ if (r < 0)
+ return r;
+
+ if (link->original_mtu == mtu)
+ return 0;
+
+ r = link_set_mtu(link, link->original_mtu);
+ if (r < 0) {
+ log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m");
+ link_enter_failed(link);
+ return r;
}
- r = address_new(&address);
- if (r >= 0) {
- r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
- if (r >= 0) {
- _cleanup_(route_freep) Route *route_gw = NULL;
- _cleanup_(route_freep) Route *route = NULL;
+ return 0;
+}
- r = route_new(&route_gw);
- if (r >= 0) {
- route_gw->family = AF_INET;
- route_gw->dst.in = gateway;
- route_gw->dst_prefixlen = 32;
- route_gw->scope = RT_SCOPE_LINK;
+static int dhcp_reset_hostname(Link *link) {
+ const char *hostname;
+ int r;
- route_remove(route_gw, link, NULL);
- }
+ assert(link);
- r = route_new(&route);
- if (r >= 0) {
- route->family = AF_INET;
- route->gw.in = gateway;
+ if (!link->network->dhcp_use_hostname)
+ return 0;
- route_remove(route, link, NULL);
- }
- }
+ hostname = link->network->dhcp_hostname;
+ if (!hostname)
+ (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
- r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
- if (r >= 0) {
- r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
- if (r >= 0)
- prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
+ if (!hostname)
+ return 0;
- address->family = AF_INET;
- address->in_addr.in = addr;
- address->prefixlen = prefixlen;
+ /* 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");
- address_remove(address, link, NULL);
- }
- }
+ return 0;
+}
- if (link->network->dhcp_use_mtu) {
- uint16_t mtu;
+static int dhcp_lease_lost(Link *link) {
+ struct in_addr address = {};
- r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
- if (r >= 0 && link->original_mtu != mtu) {
- r = link_set_mtu(link, link->original_mtu);
- if (r < 0) {
- log_link_warning(link,
- "DHCP error: could not reset MTU");
- link_enter_failed(link);
- return r;
- }
- }
- }
+ assert(link);
+ assert(link->dhcp_lease);
- if (link->network->dhcp_use_hostname) {
- const char *hostname = NULL;
+ log_link_warning(link, "DHCP lease lost");
- if (link->network->dhcp_hostname)
- hostname = link->network->dhcp_hostname;
- else
- (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
+ link->dhcp4_configured = false;
- if (hostname) {
- /* If a hostname was set due to the lease, then unset it now. */
- r = manager_set_hostname(link->manager, NULL);
- if (r < 0)
- log_link_warning_errno(link, r, "Failed to reset transient hostname: %m");
- }
- }
+ (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address);
+ (void) dhcp_remove_routes(link, &address);
+ (void) dhcp_remove_router(link, &address);
+ (void) dhcp_remove_address(link, &address);
+ (void) dhcp_reset_mtu(link);
+ (void) dhcp_reset_hostname(link);
link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
link_dirty(link);
- link->dhcp4_configured = false;
return 0;
}
link_set_dhcp_routes(link);
+ /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */
+ link_request_set_routes(link);
+
if (link->dhcp4_messages == 0) {
link->dhcp4_configured = true;
link_check_ready(link);
}
static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
+ const struct in_addr *router;
sd_dhcp_lease *lease;
struct in_addr address;
struct in_addr netmask;
- struct in_addr gateway;
unsigned prefixlen;
uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
int r;
assert(client);
assert(link);
+ link->dhcp4_configured = false;
+
r = sd_dhcp_client_get_lease(client, &lease);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: No lease: %m");
prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
- r = sd_dhcp_lease_get_router(lease, &gateway);
+ r = sd_dhcp_lease_get_router(lease, &router);
if (r < 0 && r != -ENODATA)
return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
- if (r >= 0)
+ if (r > 0 && !in4_addr_is_null(&router[0]))
log_struct(LOG_INFO,
LOG_LINK_INTERFACE(link),
LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
ADDRESS_FMT_VAL(address),
prefixlen,
- ADDRESS_FMT_VAL(gateway)),
+ ADDRESS_FMT_VAL(router[0])),
"ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
"PREFIXLEN=%u", prefixlen,
- "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(gateway));
+ "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
else
log_struct(LOG_INFO,
LOG_LINK_INTERFACE(link),
if (r < 0)
log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
if (r == 1)
- log_link_notice(link, "Overlong DCHP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
+ log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
}
if (hostname) {
return 0;
}
-static void dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
+
+static int dhcp_server_is_black_listed(Link *link, sd_dhcp_client *client) {
+ sd_dhcp_lease *lease;
+ struct in_addr addr;
+ int r;
+
+ assert(link);
+ assert(link->network);
+ assert(client);
+
+ r = sd_dhcp_client_get_lease(client, &lease);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
+
+ r = sd_dhcp_lease_get_server_identifier(lease, &addr);
+ if (r < 0)
+ return log_link_debug_errno(link, r, "Failed to get DHCP server ip address: %m");
+
+ if (set_contains(link->network->dhcp_black_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
+ log_struct(LOG_DEBUG,
+ LOG_LINK_INTERFACE(link),
+ LOG_LINK_MESSAGE(link, "DHCPv4 ip '%u.%u.%u.%u' found in black listed ip addresses, ignoring offer",
+ ADDRESS_FMT_VAL(addr)));
+ return true;
+ }
+
+ return false;
+}
+
+static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
Link *link = userdata;
- int r = 0;
+ int r;
assert(link);
assert(link->network);
assert(link->manager);
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
- return;
+ return 0;
switch (event) {
- case SD_DHCP_CLIENT_EVENT_EXPIRED:
case SD_DHCP_CLIENT_EVENT_STOP:
+
+ if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4)) {
+ assert(link->ipv4ll);
+
+ log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
+
+ r = sd_ipv4ll_start(link->ipv4ll);
+ if (r < 0)
+ return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
+ }
+
+ if (link->network->dhcp_critical) {
+ log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
+ return 0;
+ }
+
+ if (link->network->dhcp_send_release)
+ (void) sd_dhcp_client_send_release(client);
+
+ if (link->dhcp_lease) {
+ r = dhcp_lease_lost(link);
+ if (r < 0) {
+ link_enter_failed(link);
+ return r;
+ }
+ }
+
+ break;
+ case SD_DHCP_CLIENT_EVENT_EXPIRED:
case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
+
if (link->network->dhcp_critical) {
- log_link_error(link, "DHCPv4 connection considered system critical, ignoring request to reconfigure it.");
- return;
+ log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
+ return 0;
}
if (link->dhcp_lease) {
r = dhcp_lease_lost(link);
if (r < 0) {
link_enter_failed(link);
- return;
+ return r;
}
}
r = dhcp_lease_acquired(client, link);
if (r < 0) {
link_enter_failed(link);
- return;
+ return r;
}
}
r = dhcp_lease_renew(client, link);
if (r < 0) {
link_enter_failed(link);
- return;
+ return r;
}
break;
case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
r = dhcp_lease_acquired(client, link);
if (r < 0) {
link_enter_failed(link);
- return;
+ return r;
}
break;
+ case SD_DHCP_CLIENT_EVENT_SELECTING:
+ r = dhcp_server_is_black_listed(link, client);
+ if (r < 0)
+ return r;
+ if (r != 0)
+ return -ENOMSG;
+ break;
default:
if (event < 0)
log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
break;
}
- return;
+ return 0;
}
static int dhcp4_set_hostname(Link *link) {
if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
- duid->llt_time);
+ duid->llt_time);
else
r = sd_dhcp_client_set_duid(link->dhcp_client,
duid->type,
return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
}
+ if (link->network->dhcp_max_attempts > 0) {
+ r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
+ if (r < 0)
+ return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
+ }
+
return dhcp4_set_client_identifier(link);
}