2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2019-05-09 02:33:45 +02:00
|
|
|
#include <netinet/in.h>
|
2020-03-02 21:44:01 +01:00
|
|
|
#include <netinet/ip.h>
|
2014-08-08 12:12:17 +02:00
|
|
|
#include <linux/if.h>
|
2019-07-24 11:22:43 +02:00
|
|
|
#include <linux/if_arp.h>
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-03-30 16:43:28 +02:00
|
|
|
#include "escape.h"
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2019-09-26 20:06:02 +02:00
|
|
|
#include "dhcp-client-internal.h"
|
2015-05-18 17:10:07 +02:00
|
|
|
#include "hostname-util.h"
|
2017-11-24 21:03:05 +01:00
|
|
|
#include "parse-util.h"
|
2014-08-08 12:12:17 +02:00
|
|
|
#include "network-internal.h"
|
2020-10-02 07:01:59 +02:00
|
|
|
#include "networkd-address.h"
|
2019-06-29 20:57:47 +02:00
|
|
|
#include "networkd-dhcp4.h"
|
2016-11-13 04:59:06 +01:00
|
|
|
#include "networkd-link.h"
|
|
|
|
#include "networkd-manager.h"
|
|
|
|
#include "networkd-network.h"
|
2019-06-29 20:57:47 +02:00
|
|
|
#include "string-table.h"
|
2020-10-28 16:20:14 +01:00
|
|
|
#include "strv.h"
|
2017-11-24 21:03:05 +01:00
|
|
|
#include "sysctl-util.h"
|
2020-03-30 16:43:28 +02:00
|
|
|
#include "web-util.h"
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-07-14 18:30:09 +02:00
|
|
|
static int dhcp4_update_address(Link *link, bool announce);
|
|
|
|
static int dhcp4_remove_all(Link *link);
|
2019-06-07 08:31:07 +02:00
|
|
|
|
2020-08-28 06:45:42 +02:00
|
|
|
static int dhcp4_release_old_lease(Link *link) {
|
2020-07-21 16:06:51 +02:00
|
|
|
Route *route;
|
|
|
|
int k, r = 0;
|
2019-06-07 08:31:07 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
assert(link);
|
2019-06-07 08:31:07 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
if (!link->dhcp_address_old && set_isempty(link->dhcp_routes_old))
|
|
|
|
return 0;
|
2019-06-07 08:31:07 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
log_link_debug(link, "Removing old DHCPv4 address and routes.");
|
2019-06-07 08:31:07 +02:00
|
|
|
|
|
|
|
link_dirty(link);
|
2020-07-21 16:06:51 +02:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(route, link->dhcp_routes_old) {
|
2020-10-07 02:41:52 +02:00
|
|
|
k = route_remove(route, NULL, link, NULL);
|
2020-07-21 16:06:51 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link->dhcp_address_old) {
|
|
|
|
k = address_remove(link->dhcp_address_old, link, NULL);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2019-06-07 08:31:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-20 16:19:25 +01:00
|
|
|
static void dhcp4_check_ready(Link *link) {
|
2020-07-21 16:06:51 +02:00
|
|
|
int r;
|
|
|
|
|
2020-07-16 03:31:49 +02:00
|
|
|
if (link->network->dhcp_send_decline && !link->dhcp4_address_bind)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (link->dhcp4_messages > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
link->dhcp4_configured = true;
|
2020-07-21 16:06:51 +02:00
|
|
|
|
2020-07-16 03:31:49 +02:00
|
|
|
/* New address and routes are configured now. Let's release old lease. */
|
2020-08-28 06:45:42 +02:00
|
|
|
r = dhcp4_release_old_lease(link);
|
2020-07-21 16:06:51 +02:00
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 05:28:47 +01:00
|
|
|
r = sd_ipv4ll_stop(link->ipv4ll);
|
|
|
|
if (r < 0)
|
|
|
|
log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address, ignoring: %m");
|
|
|
|
|
2020-07-16 03:31:49 +02:00
|
|
|
link_check_ready(link);
|
2019-12-20 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
2018-11-28 21:06:52 +01:00
|
|
|
static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
2014-08-08 12:12:17 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
2015-11-03 13:01:43 +01:00
|
|
|
assert(link->dhcp4_messages > 0);
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
link->dhcp4_messages--;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2019-06-07 08:04:11 +02:00
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
2019-07-22 04:25:31 +02:00
|
|
|
if (r == -ENETUNREACH && !link->dhcp4_route_retrying) {
|
|
|
|
|
|
|
|
/* It seems kernel does not support that the prefix route cannot be configured with
|
|
|
|
* route table. Let's once drop the config and reconfigure them later. */
|
|
|
|
|
2020-02-09 15:44:11 +01:00
|
|
|
log_link_message_debug_errno(link, m, r, "Could not set DHCPv4 route, retrying later");
|
2019-07-22 04:25:31 +02:00
|
|
|
link->dhcp4_route_failed = true;
|
|
|
|
link->manager->dhcp4_prefix_root_cannot_set_table = true;
|
|
|
|
} else if (r < 0 && r != -EEXIST) {
|
2020-02-09 15:44:11 +01:00
|
|
|
log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 route");
|
2014-08-08 12:12:17 +02:00
|
|
|
link_enter_failed(link);
|
2019-06-07 08:04:11 +02:00
|
|
|
return 1;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 03:31:49 +02:00
|
|
|
if (link->dhcp4_messages == 0 && link->dhcp4_route_failed) {
|
|
|
|
link->dhcp4_route_failed = false;
|
|
|
|
link->dhcp4_route_retrying = true;
|
2019-07-22 04:25:31 +02:00
|
|
|
|
2020-07-16 03:31:49 +02:00
|
|
|
r = dhcp4_remove_all(link);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 03:31:49 +02:00
|
|
|
dhcp4_check_ready(link);
|
|
|
|
|
2014-08-08 12:12:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-20 13:05:18 +02:00
|
|
|
static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) {
|
|
|
|
assert(route);
|
|
|
|
assert(self_addr);
|
|
|
|
|
2019-07-06 04:17:48 +02:00
|
|
|
if (in4_addr_is_localhost(&route->dst.in) ||
|
|
|
|
(!in4_addr_is_null(self_addr) && in4_addr_equal(&route->dst.in, self_addr)))
|
2017-05-20 13:05:18 +02:00
|
|
|
return RT_SCOPE_HOST;
|
|
|
|
else if (in4_addr_is_null(&route->gw.in))
|
|
|
|
return RT_SCOPE_LINK;
|
|
|
|
else
|
|
|
|
return RT_SCOPE_UNIVERSE;
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:32:36 +01:00
|
|
|
static bool link_prefixroute(Link *link) {
|
|
|
|
return !link->network->dhcp_route_table_set ||
|
|
|
|
link->network->dhcp_route_table == RT_TABLE_MAIN ||
|
|
|
|
link->manager->dhcp4_prefix_root_cannot_set_table;
|
2019-07-22 04:22:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
static int dhcp_route_configure(Route *route, Link *link) {
|
|
|
|
Route *ret;
|
2019-07-16 19:14:27 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(route);
|
|
|
|
assert(link);
|
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
r = route_configure(route, link, dhcp4_route_handler, &ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to set DHCPv4 route: %m");
|
2019-07-16 19:14:27 +02:00
|
|
|
|
|
|
|
link->dhcp4_messages++;
|
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
r = set_ensure_put(&link->dhcp_routes, &route_hash_ops, ret);
|
2019-07-16 19:14:27 +02:00
|
|
|
if (r < 0)
|
2020-07-21 16:06:51 +02:00
|
|
|
return log_link_error_errno(link, r, "Failed to store DHCPv4 route: %m");
|
|
|
|
|
|
|
|
(void) set_remove(link->dhcp_routes_old, ret);
|
2019-07-16 19:14:27 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:30:56 +02:00
|
|
|
static int link_set_dns_routes(Link *link, const struct in_addr *address) {
|
|
|
|
const struct in_addr *dns;
|
|
|
|
uint32_t table;
|
|
|
|
int i, n, r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp_lease);
|
|
|
|
assert(link->network);
|
|
|
|
|
2019-07-16 19:47:20 +02:00
|
|
|
if (!link->network->dhcp_use_dns ||
|
|
|
|
!link->network->dhcp_routes_to_dns)
|
2019-07-16 19:30:56 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
n = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
|
|
|
|
if (IN_SET(n, 0, -ENODATA))
|
|
|
|
return 0;
|
|
|
|
if (n < 0)
|
|
|
|
return log_link_warning_errno(link, n, "DHCP error: could not get DNS servers: %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)
|
2020-07-14 18:30:09 +02:00
|
|
|
return log_link_error_errno(link, r, "Could not allocate route: %m");
|
2019-07-16 19:30:56 +02:00
|
|
|
|
|
|
|
/* Set routes to DNS servers. */
|
|
|
|
|
|
|
|
route->family = AF_INET;
|
|
|
|
route->dst.in = dns[i];
|
|
|
|
route->dst_prefixlen = 32;
|
|
|
|
route->prefsrc.in = *address;
|
|
|
|
route->scope = RT_SCOPE_LINK;
|
|
|
|
route->protocol = RTPROT_DHCP;
|
|
|
|
route->priority = link->network->dhcp_route_metric;
|
|
|
|
route->table = table;
|
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
r = dhcp_route_configure(route, link);
|
2019-07-16 19:30:56 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not set route to DNS server: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:46:44 +02:00
|
|
|
static int dhcp_prefix_route_from_lease(
|
|
|
|
const sd_dhcp_lease *lease,
|
|
|
|
uint32_t table,
|
|
|
|
const struct in_addr *address,
|
|
|
|
Route **ret_route) {
|
|
|
|
|
|
|
|
Route *route;
|
|
|
|
struct in_addr netmask;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = sd_dhcp_lease_get_netmask((sd_dhcp_lease*) lease, &netmask);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = route_new(&route);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
route->family = AF_INET;
|
|
|
|
route->dst.in.s_addr = address->s_addr & netmask.s_addr;
|
|
|
|
route->dst_prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
|
|
|
|
route->prefsrc.in = *address;
|
|
|
|
route->scope = RT_SCOPE_LINK;
|
|
|
|
route->protocol = RTPROT_DHCP;
|
|
|
|
route->table = table;
|
|
|
|
*ret_route = route;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-08 12:12:17 +02:00
|
|
|
static int link_set_dhcp_routes(Link *link) {
|
2016-01-20 14:44:14 +01:00
|
|
|
_cleanup_free_ sd_dhcp_route **static_routes = NULL;
|
2018-01-20 00:42:45 +01:00
|
|
|
bool classless_route = false, static_route = false;
|
2018-12-14 11:10:57 +01:00
|
|
|
struct in_addr address;
|
2017-09-07 11:08:39 +02:00
|
|
|
uint32_t table;
|
2020-07-21 16:06:51 +02:00
|
|
|
Route *rt;
|
2020-08-26 15:31:01 +02:00
|
|
|
int r, n;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
assert(link);
|
2017-08-31 10:58:39 +02:00
|
|
|
|
|
|
|
if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!link->network) /* link went down while we configured the IP addresses? */
|
|
|
|
return 0;
|
2016-04-21 02:36:33 +02:00
|
|
|
|
2019-07-16 06:02:38 +02:00
|
|
|
if (!link_has_carrier(link) && !link->network->configure_without_carrier)
|
|
|
|
/* During configuring addresses, the link lost its carrier. As networkd is dropping
|
|
|
|
* the addresses now, let's not configure the routes either. */
|
|
|
|
return 0;
|
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
while ((rt = set_steal_first(link->dhcp_routes))) {
|
|
|
|
r = set_ensure_put(&link->dhcp_routes_old, &route_hash_ops, rt);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store old DHCPv4 route: %m");
|
|
|
|
}
|
2019-07-16 19:14:27 +02:00
|
|
|
|
2019-02-12 04:49:45 +01:00
|
|
|
table = link_get_dhcp_route_table(link);
|
2017-09-07 11:08:39 +02:00
|
|
|
|
2017-05-24 14:05:49 +02:00
|
|
|
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
|
|
|
|
|
2019-12-07 16:32:36 +01:00
|
|
|
if (!link_prefixroute(link)) {
|
2019-07-22 04:22:28 +02:00
|
|
|
_cleanup_(route_freep) Route *prefix_route = NULL;
|
|
|
|
|
2019-10-03 15:46:44 +02:00
|
|
|
r = dhcp_prefix_route_from_lease(link->dhcp_lease, table, &address, &prefix_route);
|
2019-07-22 04:22:28 +02:00
|
|
|
if (r < 0)
|
2020-07-14 18:30:09 +02:00
|
|
|
return log_link_error_errno(link, r, "Could not create prefix route: %m");
|
2019-07-22 04:22:28 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
r = dhcp_route_configure(prefix_route, link);
|
2019-07-22 04:22:28 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not set prefix route: %m");
|
|
|
|
}
|
|
|
|
|
2017-11-22 07:43:55 +01:00
|
|
|
n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
|
2018-06-25 14:04:22 +02:00
|
|
|
if (n == -ENODATA)
|
|
|
|
log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m");
|
|
|
|
else if (n < 0)
|
2020-08-26 15:31:01 +02:00
|
|
|
return log_link_error_errno(link, n, "DHCP: could not get routes: %m");
|
2017-11-22 07:43:55 +01:00
|
|
|
|
2020-08-26 15:31:01 +02:00
|
|
|
for (int i = 0; i < n; i++) {
|
2018-11-27 12:18:29 +01:00
|
|
|
switch (sd_dhcp_route_get_option(static_routes[i])) {
|
|
|
|
case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE:
|
2018-01-20 00:42:45 +01:00
|
|
|
classless_route = true;
|
2018-11-27 12:18:29 +01:00
|
|
|
break;
|
|
|
|
case SD_DHCP_OPTION_STATIC_ROUTE:
|
2018-01-20 00:42:45 +01:00
|
|
|
static_route = true;
|
2018-11-27 12:18:29 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-01-20 00:42:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 05:12:39 +01:00
|
|
|
if (link->network->dhcp_use_routes) {
|
2020-08-26 15:31:01 +02:00
|
|
|
/* 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. */
|
|
|
|
if (classless_route && static_route)
|
|
|
|
log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option");
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
2020-03-01 05:12:39 +01:00
|
|
|
_cleanup_(route_freep) Route *route = NULL;
|
|
|
|
|
|
|
|
if (classless_route &&
|
|
|
|
sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = route_new(&route);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not allocate route: %m");
|
|
|
|
|
|
|
|
route->family = AF_INET;
|
|
|
|
route->protocol = RTPROT_DHCP;
|
2020-10-07 03:04:13 +02:00
|
|
|
route->gw_family = AF_INET;
|
2020-03-01 05:12:39 +01:00
|
|
|
assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0);
|
|
|
|
assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0);
|
|
|
|
assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0);
|
|
|
|
route->priority = link->network->dhcp_route_metric;
|
|
|
|
route->table = table;
|
|
|
|
route->mtu = link->network->dhcp_route_mtu;
|
|
|
|
route->scope = route_scope_from_address(route, &address);
|
|
|
|
if (IN_SET(route->scope, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE))
|
|
|
|
route->prefsrc.in = address;
|
|
|
|
|
|
|
|
if (set_contains(link->dhcp_routes, route))
|
|
|
|
continue;
|
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
r = dhcp_route_configure(route, link);
|
2020-03-01 05:12:39 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not set route: %m");
|
|
|
|
}
|
2017-11-22 07:43:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 00:05:14 +02:00
|
|
|
if (link->network->dhcp_use_gateway) {
|
2020-08-26 15:31:01 +02:00
|
|
|
const struct in_addr *router;
|
|
|
|
|
2020-04-16 00:05:14 +02:00
|
|
|
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)
|
2020-08-26 15:31:01 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m");
|
2020-04-16 00:05:14 +02:00
|
|
|
else if (in4_addr_is_null(&router[0]))
|
|
|
|
log_link_info(link, "DHCP: Received gateway is null.");
|
2020-08-26 15:31:01 +02:00
|
|
|
else if (classless_route)
|
|
|
|
/* 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. */
|
|
|
|
log_link_warning(link, "Classless static routes received from DHCP server: ignoring router option");
|
|
|
|
else {
|
2020-04-16 00:05:14 +02:00
|
|
|
_cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
|
|
|
|
|
|
|
|
r = route_new(&route_gw);
|
|
|
|
if (r < 0)
|
2020-07-14 18:30:09 +02:00
|
|
|
return log_link_error_errno(link, r, "Could not allocate route: %m");
|
2020-04-16 00:05:14 +02:00
|
|
|
|
|
|
|
/* The dhcp netmask may mask out the gateway. Add an explicit
|
|
|
|
* 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 = 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;
|
|
|
|
route_gw->mtu = link->network->dhcp_route_mtu;
|
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
r = dhcp_route_configure(route_gw, link);
|
2020-04-16 00:05:14 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not set host route: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-04-16 00:05:14 +02:00
|
|
|
r = route_new(&route);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not allocate route: %m");
|
2019-02-11 13:25:13 +01:00
|
|
|
|
2020-04-16 00:05:14 +02:00
|
|
|
route->family = AF_INET;
|
2020-10-07 03:04:13 +02:00
|
|
|
route->gw_family = AF_INET;
|
2020-04-16 00:05:14 +02:00
|
|
|
route->gw.in = router[0];
|
|
|
|
route->prefsrc.in = address;
|
|
|
|
route->protocol = RTPROT_DHCP;
|
|
|
|
route->priority = link->network->dhcp_route_metric;
|
|
|
|
route->table = table;
|
|
|
|
route->mtu = link->network->dhcp_route_mtu;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
r = dhcp_route_configure(route, link);
|
2020-04-16 00:05:14 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not set router: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-10-02 03:41:55 +02:00
|
|
|
HASHMAP_FOREACH(rt, link->network->routes_by_section) {
|
2020-10-12 07:44:04 +02:00
|
|
|
if (!rt->gateway_from_dhcp_or_ra)
|
2020-08-26 15:31:01 +02:00
|
|
|
continue;
|
2020-01-07 06:43:09 +01:00
|
|
|
|
2020-10-12 08:12:55 +02:00
|
|
|
if (rt->gw_family != AF_INET)
|
2020-08-26 15:31:01 +02:00
|
|
|
continue;
|
2020-01-07 06:43:09 +01:00
|
|
|
|
2020-08-26 15:31:01 +02:00
|
|
|
rt->gw.in = router[0];
|
2020-10-12 08:52:02 +02:00
|
|
|
if (!rt->protocol_set)
|
|
|
|
rt->protocol = RTPROT_DHCP;
|
|
|
|
if (!rt->priority_set)
|
|
|
|
rt->priority = link->network->dhcp_route_metric;
|
|
|
|
if (!rt->table_set)
|
|
|
|
rt->table = table;
|
|
|
|
if (rt->mtu == 0)
|
|
|
|
rt->mtu = link->network->dhcp_route_mtu;
|
2020-01-07 06:43:09 +01:00
|
|
|
|
2020-08-26 15:31:01 +02:00
|
|
|
r = dhcp_route_configure(rt, link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not set gateway: %m");
|
|
|
|
}
|
2020-04-16 00:05:14 +02:00
|
|
|
}
|
2020-01-07 06:43:09 +01:00
|
|
|
}
|
|
|
|
|
2019-07-16 19:30:56 +02:00
|
|
|
return link_set_dns_routes(link, &address);
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 09:47:47 +02:00
|
|
|
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);
|
2020-08-18 12:21:25 +02:00
|
|
|
if (r == -ENODATA)
|
|
|
|
return 0;
|
2019-05-24 09:47:47 +02:00
|
|
|
if (r < 0)
|
2020-08-18 12:21:25 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP error: failed to get MTU from lease: %m");
|
2019-05-24 09:47:47 +02:00
|
|
|
|
|
|
|
if (link->original_mtu == mtu)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = link_set_mtu(link, link->original_mtu);
|
2020-08-18 12:21:25 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2019-05-24 09:47:47 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-14 18:30:09 +02:00
|
|
|
static int dhcp4_remove_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp4_remove_messages > 0);
|
|
|
|
|
|
|
|
link->dhcp4_remove_messages--;
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
r = sd_netlink_message_get_errno(m);
|
|
|
|
if (r < 0 && r != -ESRCH)
|
|
|
|
log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 route, ignoring");
|
|
|
|
|
|
|
|
if (link->dhcp4_remove_messages == 0) {
|
|
|
|
r = dhcp4_update_address(link, false);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp4_remove_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp4_remove_messages > 0);
|
|
|
|
|
|
|
|
link->dhcp4_remove_messages--;
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
r = sd_netlink_message_get_errno(m);
|
|
|
|
if (r < 0 && r != -EADDRNOTAVAIL)
|
|
|
|
log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 address, ignoring");
|
|
|
|
else
|
|
|
|
(void) manager_rtnl_process_address(rtnl, m, link->manager);
|
|
|
|
|
|
|
|
if (link->dhcp4_remove_messages == 0) {
|
|
|
|
r = dhcp4_update_address(link, false);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp4_remove_all(Link *link) {
|
2020-07-21 16:06:51 +02:00
|
|
|
Route *route;
|
|
|
|
int k, r = 0;
|
2020-07-14 18:30:09 +02:00
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(route, link->dhcp_routes) {
|
2020-10-07 02:41:52 +02:00
|
|
|
k = route_remove(route, NULL, link, dhcp4_remove_route_handler);
|
2020-07-21 16:06:51 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
else
|
|
|
|
link->dhcp4_remove_messages++;
|
|
|
|
}
|
2020-07-14 18:30:09 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
if (link->dhcp_address) {
|
|
|
|
k = address_remove(link->dhcp_address, link, dhcp4_remove_address_handler);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
else
|
|
|
|
link->dhcp4_remove_messages++;
|
|
|
|
}
|
2020-07-14 18:30:09 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
return r;
|
2020-07-14 18:30:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 09:47:47 +02:00
|
|
|
static int dhcp_lease_lost(Link *link) {
|
2020-07-30 05:45:36 +02:00
|
|
|
int k, r = 0;
|
2019-05-24 09:47:47 +02:00
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp_lease);
|
|
|
|
|
2019-08-31 20:49:17 +02:00
|
|
|
log_link_info(link, "DHCP lease lost");
|
2019-05-24 09:47:47 +02:00
|
|
|
|
|
|
|
link->dhcp4_configured = false;
|
|
|
|
|
2020-07-16 03:12:18 +02:00
|
|
|
/* dhcp_lease_lost() may be called during renewing IP address. */
|
2020-08-28 06:45:42 +02:00
|
|
|
k = dhcp4_release_old_lease(link);
|
2020-07-21 16:06:51 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
2020-07-16 03:12:18 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
k = dhcp4_remove_all(link);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
2020-07-14 18:30:09 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
k = dhcp_reset_mtu(link);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
2020-07-14 18:30:09 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
k = dhcp_reset_hostname(link);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
2019-05-24 09:47:47 +02:00
|
|
|
|
2014-08-08 12:12:17 +02:00
|
|
|
link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
|
2015-11-18 14:21:06 +01:00
|
|
|
link_dirty(link);
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-10-15 00:38:45 +02:00
|
|
|
(void) sd_ipv4acd_stop(link->dhcp_acd);
|
2020-10-14 08:26:19 +02:00
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
return r;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2019-12-20 14:13:18 +01:00
|
|
|
static void dhcp_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
|
|
|
|
_cleanup_free_ char *pretty = NULL;
|
|
|
|
union in_addr_union address = {};
|
|
|
|
Link *link;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(acd);
|
|
|
|
assert(userdata);
|
|
|
|
|
|
|
|
link = userdata;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case SD_IPV4ACD_EVENT_STOP:
|
|
|
|
log_link_debug(link, "Stopping ACD client for DHCP4...");
|
|
|
|
return;
|
|
|
|
|
|
|
|
case SD_IPV4ACD_EVENT_BIND:
|
|
|
|
if (DEBUG_LOGGING) {
|
|
|
|
(void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
|
|
|
|
(void) in_addr_to_string(AF_INET, &address, &pretty);
|
|
|
|
log_link_debug(link, "Successfully claimed DHCP4 address %s", strna(pretty));
|
|
|
|
}
|
2020-07-16 03:31:49 +02:00
|
|
|
link->dhcp4_address_bind = true;
|
2019-12-20 16:19:25 +01:00
|
|
|
dhcp4_check_ready(link);
|
2019-12-20 14:13:18 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SD_IPV4ACD_EVENT_CONFLICT:
|
|
|
|
(void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in);
|
|
|
|
(void) in_addr_to_string(AF_INET, &address, &pretty);
|
|
|
|
log_link_warning(link, "DAD conflict. Dropping DHCP4 address %s", strna(pretty));
|
|
|
|
|
2020-07-16 03:43:49 +02:00
|
|
|
r = sd_dhcp_client_send_decline(link->dhcp_client);
|
|
|
|
if (r < 0)
|
|
|
|
log_link_warning_errno(link, r, "Failed to send DHCP DECLINE, ignoring: %m");
|
2019-12-20 14:13:18 +01:00
|
|
|
|
|
|
|
if (link->dhcp_lease) {
|
|
|
|
r = dhcp_lease_lost(link);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(link);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Invalid IPv4ACD event.");
|
|
|
|
}
|
|
|
|
|
2020-09-04 01:54:21 +02:00
|
|
|
(void) sd_ipv4acd_stop(acd);
|
2019-12-20 14:13:18 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
static int dhcp4_configure_dad(Link *link) {
|
2019-12-20 14:13:18 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
2020-10-14 08:22:56 +02:00
|
|
|
assert(link->manager);
|
|
|
|
assert(link->network);
|
2019-12-20 14:13:18 +01:00
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
if (!link->network->dhcp_send_decline)
|
|
|
|
return 0;
|
2019-12-20 14:13:18 +01:00
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
if (!link->dhcp_acd) {
|
|
|
|
r = sd_ipv4acd_new(&link->dhcp_acd);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-12-20 14:13:18 +01:00
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
r = sd_ipv4acd_attach_event(link->dhcp_acd, link->manager->event, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2019-12-20 14:13:18 +01:00
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
r = sd_ipv4acd_set_ifindex(link->dhcp_acd, link->ifindex);
|
2019-12-20 14:13:18 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-26 18:07:49 +01:00
|
|
|
r = sd_ipv4acd_set_mac(link->dhcp_acd, &link->hw_addr.addr.ether);
|
2019-12-20 14:13:18 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-14 08:29:47 +02:00
|
|
|
static int dhcp4_dad_update_mac(Link *link) {
|
|
|
|
bool running;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (!link->dhcp_acd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
running = sd_ipv4acd_is_running(link->dhcp_acd);
|
|
|
|
|
|
|
|
r = sd_ipv4acd_stop(link->dhcp_acd);
|
2019-12-20 14:13:18 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-26 18:07:49 +01:00
|
|
|
r = sd_ipv4acd_set_mac(link->dhcp_acd, &link->hw_addr.addr.ether);
|
2019-12-20 14:13:18 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-14 08:29:47 +02:00
|
|
|
if (running) {
|
|
|
|
r = sd_ipv4acd_start(link->dhcp_acd, true);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-12-20 14:13:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-16 03:31:49 +02:00
|
|
|
static int dhcp4_start_acd(Link *link) {
|
|
|
|
union in_addr_union addr;
|
2020-08-27 23:54:11 +02:00
|
|
|
struct in_addr old;
|
2020-07-16 03:31:49 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!link->network->dhcp_send_decline)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!link->dhcp_lease)
|
|
|
|
return 0;
|
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
(void) sd_ipv4acd_stop(link->dhcp_acd);
|
2020-08-27 23:38:43 +02:00
|
|
|
|
2020-07-16 03:31:49 +02:00
|
|
|
link->dhcp4_address_bind = false;
|
|
|
|
|
|
|
|
r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr.in);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
r = sd_ipv4acd_get_address(link->dhcp_acd, &old);
|
2020-08-27 23:54:11 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
r = sd_ipv4acd_set_address(link->dhcp_acd, &addr.in);
|
2020-07-16 03:31:49 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
r = sd_ipv4acd_set_callback(link->dhcp_acd, dhcp_address_on_acd, link);
|
2020-07-16 03:31:49 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (DEBUG_LOGGING) {
|
|
|
|
_cleanup_free_ char *pretty = NULL;
|
|
|
|
|
|
|
|
(void) in_addr_to_string(AF_INET, &addr, &pretty);
|
|
|
|
log_link_debug(link, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty));
|
|
|
|
}
|
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
r = sd_ipv4acd_start(link->dhcp_acd, !in4_addr_equal(&addr.in, &old));
|
2020-07-16 03:31:49 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-28 06:45:42 +02:00
|
|
|
static int dhcp4_address_ready_callback(Address *address) {
|
|
|
|
Link *link;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(address);
|
|
|
|
|
|
|
|
link = address->link;
|
|
|
|
|
|
|
|
/* Do not call this again. */
|
|
|
|
address->callback = NULL;
|
|
|
|
|
|
|
|
r = link_set_dhcp_routes(link);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* Reconfigure static routes as kernel may remove some routes when lease expires. */
|
2020-10-02 02:27:28 +02:00
|
|
|
r = link_set_routes(link);
|
2020-08-28 06:45:42 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = dhcp4_start_acd(link);
|
|
|
|
if (r < 0)
|
2020-09-14 15:32:37 +02:00
|
|
|
return log_link_error_errno(link, r, "Failed to start IPv4ACD for DHCP4 address: %m");
|
2020-08-28 06:45:42 +02:00
|
|
|
|
|
|
|
dhcp4_check_ready(link);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-28 21:06:52 +01:00
|
|
|
static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
2014-08-08 12:12:17 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
2019-06-07 08:04:11 +02:00
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0 && r != -EEXIST) {
|
2019-11-30 07:54:07 +01:00
|
|
|
log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 address");
|
2014-08-08 12:12:17 +02:00
|
|
|
link_enter_failed(link);
|
2019-06-07 08:04:11 +02:00
|
|
|
return 1;
|
2019-07-14 17:35:49 +02:00
|
|
|
} else if (r >= 0)
|
|
|
|
(void) manager_rtnl_process_address(rtnl, m, link->manager);
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-08-28 06:45:42 +02:00
|
|
|
if (address_is_ready(link->dhcp_address)) {
|
|
|
|
r = dhcp4_address_ready_callback(link->dhcp_address);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
link->dhcp_address->callback = dhcp4_address_ready_callback;
|
2019-12-20 14:13:18 +01:00
|
|
|
|
2014-08-08 12:12:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-14 18:30:09 +02:00
|
|
|
static int dhcp4_update_address(Link *link, bool announce) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(address_freep) Address *addr = NULL;
|
2020-07-14 18:30:09 +02:00
|
|
|
uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
|
|
|
|
struct in_addr address, netmask;
|
2014-08-08 12:12:17 +02:00
|
|
|
unsigned prefixlen;
|
2020-07-21 16:06:51 +02:00
|
|
|
Address *ret;
|
2014-08-08 12:12:17 +02:00
|
|
|
int r;
|
|
|
|
|
2020-07-14 18:30:09 +02:00
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
|
|
|
|
|
|
|
if (!link->dhcp_lease)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
link_set_state(link, LINK_STATE_CONFIGURING);
|
|
|
|
link->dhcp4_configured = false;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-10-02 02:27:28 +02:00
|
|
|
/* address_handler calls link_set_routes() and link_set_nexthop(). Before they are called, the
|
|
|
|
* related flags must be cleared. Otherwise, the link becomes configured state before routes
|
|
|
|
* are configured. */
|
2020-07-12 20:12:11 +02:00
|
|
|
link->static_routes_configured = false;
|
|
|
|
link->static_nexthops_configured = false;
|
|
|
|
|
2020-07-14 18:30:09 +02:00
|
|
|
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_netmask(link->dhcp_lease, &netmask);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
|
|
|
|
|
|
|
|
if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
|
|
|
|
r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
|
|
|
|
|
|
|
|
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_null(&router[0]))
|
|
|
|
log_struct(LOG_INFO,
|
|
|
|
LOG_LINK_INTERFACE(link),
|
2020-10-07 13:42:54 +02:00
|
|
|
LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u via "IPV4_ADDRESS_FMT_STR,
|
|
|
|
IPV4_ADDRESS_FMT_VAL(address),
|
2020-07-14 18:30:09 +02:00
|
|
|
prefixlen,
|
2020-10-07 13:42:54 +02:00
|
|
|
IPV4_ADDRESS_FMT_VAL(router[0])),
|
|
|
|
"ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
|
2020-07-14 18:30:09 +02:00
|
|
|
"PREFIXLEN=%u", prefixlen,
|
2020-10-07 13:42:54 +02:00
|
|
|
"GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0]));
|
2020-07-14 18:30:09 +02:00
|
|
|
else
|
|
|
|
log_struct(LOG_INFO,
|
|
|
|
LOG_LINK_INTERFACE(link),
|
2020-10-07 13:42:54 +02:00
|
|
|
LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u",
|
|
|
|
IPV4_ADDRESS_FMT_VAL(address),
|
2020-07-14 18:30:09 +02:00
|
|
|
prefixlen),
|
2020-10-07 13:42:54 +02:00
|
|
|
"ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
|
2020-07-14 18:30:09 +02:00
|
|
|
"PREFIXLEN=%u", prefixlen);
|
|
|
|
}
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2015-09-21 15:53:40 +02:00
|
|
|
r = address_new(&addr);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0)
|
2020-07-14 18:30:09 +02:00
|
|
|
return log_oom();
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
addr->family = AF_INET;
|
2020-07-14 18:30:09 +02:00
|
|
|
addr->in_addr.in.s_addr = address.s_addr;
|
2014-08-08 12:12:17 +02:00
|
|
|
addr->cinfo.ifa_prefered = lifetime;
|
|
|
|
addr->cinfo.ifa_valid = lifetime;
|
|
|
|
addr->prefixlen = prefixlen;
|
2020-12-02 11:19:06 +01:00
|
|
|
if (prefixlen <= 30)
|
|
|
|
addr->broadcast.s_addr = address.s_addr | ~netmask.s_addr;
|
2020-10-04 01:27:14 +02:00
|
|
|
SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !link_prefixroute(link));
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2015-10-01 17:31:14 +02:00
|
|
|
/* allow reusing an existing address and simply update its lifetime
|
|
|
|
* in case it already exists */
|
2020-07-21 16:06:51 +02:00
|
|
|
r = address_configure(addr, link, dhcp4_address_handler, true, &ret);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0)
|
2020-07-21 16:06:51 +02:00
|
|
|
return log_link_error_errno(link, r, "Failed to set DHCPv4 address: %m");
|
|
|
|
|
|
|
|
if (!address_equal(link->dhcp_address, ret))
|
|
|
|
link->dhcp_address_old = link->dhcp_address;
|
|
|
|
link->dhcp_address = ret;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
|
|
|
|
sd_dhcp_lease *lease;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(client);
|
|
|
|
|
|
|
|
r = sd_dhcp_client_get_lease(client, &lease);
|
2015-08-27 13:04:33 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
sd_dhcp_lease_unref(link->dhcp_lease);
|
2015-08-26 12:30:56 +02:00
|
|
|
link->dhcp_lease = sd_dhcp_lease_ref(lease);
|
2015-11-18 14:21:06 +01:00
|
|
|
link_dirty(link);
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-07-14 18:30:09 +02:00
|
|
|
return dhcp4_update_address(link, false);
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
|
|
|
|
sd_dhcp_lease *lease;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(client);
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
r = sd_dhcp_client_get_lease(client, &lease);
|
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
|
|
|
if (r < 0)
|
2015-08-27 13:04:33 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP error: No lease: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-07-14 18:30:09 +02:00
|
|
|
sd_dhcp_lease_unref(link->dhcp_lease);
|
2015-08-26 12:30:56 +02:00
|
|
|
link->dhcp_lease = sd_dhcp_lease_ref(lease);
|
2015-11-18 14:21:06 +01:00
|
|
|
link_dirty(link);
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2016-01-25 21:47:02 +01:00
|
|
|
if (link->network->dhcp_use_mtu) {
|
2014-08-08 12:12:17 +02:00
|
|
|
uint16_t mtu;
|
|
|
|
|
|
|
|
r = sd_dhcp_lease_get_mtu(lease, &mtu);
|
|
|
|
if (r >= 0) {
|
2019-05-15 09:02:06 +02:00
|
|
|
r = link_set_mtu(link, mtu);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0)
|
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
|
|
|
log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 21:47:02 +01:00
|
|
|
if (link->network->dhcp_use_hostname) {
|
2017-12-13 18:00:46 +01:00
|
|
|
const char *dhcpname = NULL;
|
|
|
|
_cleanup_free_ char *hostname = NULL;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2016-01-25 21:47:02 +01:00
|
|
|
if (link->network->dhcp_hostname)
|
2017-12-13 18:00:46 +01:00
|
|
|
dhcpname = link->network->dhcp_hostname;
|
2015-08-27 12:53:43 +02:00
|
|
|
else
|
2017-12-13 18:00:46 +01:00
|
|
|
(void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
|
|
|
|
|
|
|
|
if (dhcpname) {
|
|
|
|
r = shorten_overlong(dhcpname, &hostname);
|
|
|
|
if (r < 0)
|
|
|
|
log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
|
|
|
|
if (r == 1)
|
2019-04-27 02:22:40 +02:00
|
|
|
log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
|
2017-12-13 18:00:46 +01:00
|
|
|
}
|
2015-07-10 12:12:27 +02:00
|
|
|
|
2015-08-27 12:53:43 +02:00
|
|
|
if (hostname) {
|
2016-11-21 23:15:41 +01:00
|
|
|
r = manager_set_hostname(link->manager, hostname);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0)
|
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
|
|
|
log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 21:47:02 +01:00
|
|
|
if (link->network->dhcp_use_timezone) {
|
2015-08-27 02:12:27 +02:00
|
|
|
const char *tz = NULL;
|
|
|
|
|
|
|
|
(void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
|
|
|
|
|
|
|
|
if (tz) {
|
2016-11-21 23:15:41 +01:00
|
|
|
r = manager_set_timezone(link->manager, tz);
|
2015-08-27 02:12:27 +02:00
|
|
|
if (r < 0)
|
|
|
|
log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 18:30:09 +02:00
|
|
|
if (link->dhcp4_remove_messages == 0) {
|
|
|
|
r = dhcp4_update_address(link, true);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
} else
|
|
|
|
log_link_debug(link,
|
|
|
|
"The link has previously assigned DHCPv4 address or routes. "
|
|
|
|
"The newly assigned address and routes will set up after old ones are removed.");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-01 08:43:23 +02:00
|
|
|
|
2019-06-07 08:31:07 +02:00
|
|
|
static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = dhcp_lease_acquired(client, link);
|
2020-07-21 16:06:51 +02:00
|
|
|
if (r < 0)
|
2019-06-07 08:31:07 +02:00
|
|
|
(void) dhcp_lease_lost(link);
|
|
|
|
|
2020-07-21 16:06:51 +02:00
|
|
|
return r;
|
2019-06-07 08:31:07 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 08:31:16 +02:00
|
|
|
static int dhcp_server_is_deny_listed(Link *link, sd_dhcp_client *client) {
|
2019-05-01 00:47:41 +02:00
|
|
|
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)
|
2020-07-07 11:51:29 +02:00
|
|
|
return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
|
2019-05-01 00:47:41 +02:00
|
|
|
|
2020-06-23 08:31:16 +02:00
|
|
|
if (set_contains(link->network->dhcp_deny_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
|
2019-05-01 00:47:41 +02:00
|
|
|
log_struct(LOG_DEBUG,
|
|
|
|
LOG_LINK_INTERFACE(link),
|
2020-10-07 13:42:54 +02:00
|
|
|
LOG_LINK_MESSAGE(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in deny-list, ignoring offer",
|
|
|
|
IPV4_ADDRESS_FMT_VAL(addr)));
|
2019-05-01 00:47:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-26 05:28:52 +02:00
|
|
|
static int dhcp_server_is_allow_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)
|
2020-07-07 11:51:29 +02:00
|
|
|
return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
|
2020-06-26 05:28:52 +02:00
|
|
|
|
|
|
|
if (set_contains(link->network->dhcp_allow_listed_ip, UINT32_TO_PTR(addr.s_addr))) {
|
|
|
|
log_struct(LOG_DEBUG,
|
|
|
|
LOG_LINK_INTERFACE(link),
|
2020-10-07 13:42:54 +02:00
|
|
|
LOG_LINK_MESSAGE(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in allow-list, accepting offer",
|
|
|
|
IPV4_ADDRESS_FMT_VAL(addr)));
|
2020-06-26 05:28:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:47:41 +02:00
|
|
|
static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
|
2014-08-08 12:12:17 +02:00
|
|
|
Link *link = userdata;
|
2019-05-10 16:31:20 +02:00
|
|
|
int r;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
|
|
|
assert(link->manager);
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
2019-05-01 00:47:41 +02:00
|
|
|
return 0;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
switch (event) {
|
2015-09-22 14:46:21 +02:00
|
|
|
case SD_DHCP_CLIENT_EVENT_STOP:
|
2020-11-23 05:44:29 +01:00
|
|
|
if (link->ipv4ll) {
|
2019-05-01 08:43:23 +02:00
|
|
|
log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
|
|
|
|
|
|
|
|
r = sd_ipv4ll_start(link->ipv4ll);
|
2019-05-01 00:47:41 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
|
2019-05-01 08:43:23 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 05:31:13 +02:00
|
|
|
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
|
2019-05-27 22:45:10 +02:00
|
|
|
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link->dhcp_lease) {
|
2020-07-16 03:43:49 +02:00
|
|
|
if (link->network->dhcp_send_release) {
|
|
|
|
r = sd_dhcp_client_send_release(client);
|
|
|
|
if (r < 0)
|
|
|
|
log_link_warning_errno(link, r, "Failed to send DHCP RELEASE, ignoring: %m");
|
|
|
|
}
|
2019-10-02 10:29:41 +02:00
|
|
|
|
2019-05-27 22:45:10 +02:00
|
|
|
r = dhcp_lease_lost(link);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2019-05-01 08:43:23 +02:00
|
|
|
case SD_DHCP_CLIENT_EVENT_EXPIRED:
|
2019-06-03 05:31:13 +02:00
|
|
|
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
|
2019-05-27 22:45:10 +02:00
|
|
|
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
|
2019-05-01 00:47:41 +02:00
|
|
|
return 0;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (link->dhcp_lease) {
|
|
|
|
r = dhcp_lease_lost(link);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
2019-05-01 00:47:41 +02:00
|
|
|
return r;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:31:07 +02:00
|
|
|
break;
|
|
|
|
case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
|
|
|
|
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
|
|
|
|
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = dhcp_lease_ip_change(client, link);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return r;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2015-09-22 14:46:21 +02:00
|
|
|
case SD_DHCP_CLIENT_EVENT_RENEW:
|
2014-08-08 12:12:17 +02:00
|
|
|
r = dhcp_lease_renew(client, link);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
2019-05-01 00:47:41 +02:00
|
|
|
return r;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
break;
|
2015-09-22 14:46:21 +02:00
|
|
|
case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
|
2014-08-08 12:12:17 +02:00
|
|
|
r = dhcp_lease_acquired(client, link);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
2019-05-01 00:47:41 +02:00
|
|
|
return r;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-05-01 00:47:41 +02:00
|
|
|
case SD_DHCP_CLIENT_EVENT_SELECTING:
|
2020-06-26 05:28:52 +02:00
|
|
|
if (!set_isempty(link->network->dhcp_allow_listed_ip)) {
|
|
|
|
r = dhcp_server_is_allow_listed(link, client);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
return -ENOMSG;
|
|
|
|
} else {
|
|
|
|
r = dhcp_server_is_deny_listed(link, client);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r != 0)
|
|
|
|
return -ENOMSG;
|
|
|
|
}
|
2019-05-01 00:47:41 +02:00
|
|
|
break;
|
2020-10-08 20:14:51 +02:00
|
|
|
|
|
|
|
case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE:
|
2020-11-23 05:44:29 +01:00
|
|
|
if (link->ipv4ll && !sd_ipv4ll_is_running(link->ipv4ll)) {
|
|
|
|
log_link_debug(link, "Problems acquiring DHCP lease, acquiring IPv4 link-local address");
|
2020-10-08 20:14:51 +02:00
|
|
|
|
2020-11-23 05:44:29 +01:00
|
|
|
r = sd_ipv4ll_start(link->ipv4ll);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
|
2020-10-08 20:14:51 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-08-08 12:12:17 +02:00
|
|
|
default:
|
|
|
|
if (event < 0)
|
2015-08-27 13:04:33 +02:00
|
|
|
log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
else
|
2015-08-27 13:04:33 +02:00
|
|
|
log_link_warning(link, "DHCP unknown event: %i", event);
|
2014-08-08 12:12:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:47:41 +02:00
|
|
|
return 0;
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 13:02:57 +01:00
|
|
|
static int dhcp4_set_hostname(Link *link) {
|
|
|
|
_cleanup_free_ char *hostname = NULL;
|
|
|
|
const char *hn;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (!link->network->dhcp_send_hostname)
|
|
|
|
hn = NULL;
|
|
|
|
else if (link->network->dhcp_hostname)
|
|
|
|
hn = link->network->dhcp_hostname;
|
|
|
|
else {
|
|
|
|
r = gethostname_strict(&hostname);
|
|
|
|
if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to get hostname: %m");
|
2016-11-18 13:02:57 +01:00
|
|
|
|
|
|
|
hn = hostname;
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:31:10 +02:00
|
|
|
r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
|
|
|
|
if (r == -EINVAL && hostname)
|
|
|
|
/* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
|
2020-10-28 07:18:23 +01:00
|
|
|
log_link_debug_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
|
2018-08-02 09:31:10 +02:00
|
|
|
else if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
|
2017-11-24 21:03:05 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:29:57 +02:00
|
|
|
static int dhcp4_set_client_identifier(Link *link) {
|
2018-08-06 10:33:12 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
|
|
|
assert(link->dhcp_client);
|
|
|
|
|
|
|
|
switch (link->network->dhcp_client_identifier) {
|
|
|
|
case DHCP_CLIENT_ID_DUID: {
|
2018-08-07 06:57:48 +02:00
|
|
|
/* If configured, apply user specified DUID and IAID */
|
2018-08-06 10:33:12 +02:00
|
|
|
const DUID *duid = link_get_duid(link);
|
|
|
|
|
2018-08-07 06:57:48 +02:00
|
|
|
if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
|
|
|
|
r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
|
2018-11-23 22:19:26 +01:00
|
|
|
link->network->iaid_set,
|
2018-08-07 06:57:48 +02:00
|
|
|
link->network->iaid,
|
|
|
|
duid->llt_time);
|
|
|
|
else
|
|
|
|
r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
|
2018-11-23 22:19:26 +01:00
|
|
|
link->network->iaid_set,
|
2018-08-07 06:57:48 +02:00
|
|
|
link->network->iaid,
|
|
|
|
duid->type,
|
|
|
|
duid->raw_data_len > 0 ? duid->raw_data : NULL,
|
|
|
|
duid->raw_data_len);
|
2018-08-06 10:33:12 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m");
|
2018-08-06 10:33:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DHCP_CLIENT_ID_DUID_ONLY: {
|
|
|
|
/* If configured, apply user specified DUID */
|
|
|
|
const DUID *duid = link_get_duid(link);
|
|
|
|
|
2018-08-07 06:57:48 +02:00
|
|
|
if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
|
|
|
|
r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
|
2018-12-22 23:59:54 +01:00
|
|
|
duid->llt_time);
|
2018-08-07 06:57:48 +02:00
|
|
|
else
|
|
|
|
r = sd_dhcp_client_set_duid(link->dhcp_client,
|
|
|
|
duid->type,
|
|
|
|
duid->raw_data_len > 0 ? duid->raw_data : NULL,
|
|
|
|
duid->raw_data_len);
|
2018-08-06 10:33:12 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
|
2018-08-06 10:33:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-10-26 14:09:13 +01:00
|
|
|
case DHCP_CLIENT_ID_MAC: {
|
|
|
|
const uint8_t *hw_addr = link->hw_addr.addr.bytes;
|
|
|
|
size_t hw_addr_len = link->hw_addr.length;
|
|
|
|
|
|
|
|
if (link->iftype == ARPHRD_INFINIBAND && hw_addr_len == INFINIBAND_ALEN) {
|
|
|
|
/* set_client_id expects only last 8 bytes of an IB address */
|
|
|
|
hw_addr += INFINIBAND_ALEN - 8;
|
|
|
|
hw_addr_len -= INFINIBAND_ALEN - 8;
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:33:12 +02:00
|
|
|
r = sd_dhcp_client_set_client_id(link->dhcp_client,
|
2020-10-26 14:09:13 +01:00
|
|
|
link->iftype,
|
|
|
|
hw_addr,
|
|
|
|
hw_addr_len);
|
2018-08-06 10:33:12 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
|
2018-08-06 10:33:12 +02:00
|
|
|
break;
|
2020-10-26 14:09:13 +01:00
|
|
|
}
|
2018-08-06 10:33:12 +02:00
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown client identifier type.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:16:22 +01:00
|
|
|
static int dhcp4_set_request_address(Link *link) {
|
|
|
|
Address *a;
|
2020-10-02 11:07:32 +02:00
|
|
|
|
|
|
|
assert(link);
|
2020-10-28 06:16:22 +01:00
|
|
|
assert(link->network);
|
|
|
|
assert(link->dhcp_client);
|
2020-10-02 11:07:32 +02:00
|
|
|
|
2020-10-28 06:16:22 +01:00
|
|
|
if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
|
2020-10-02 11:07:32 +02:00
|
|
|
return 0;
|
|
|
|
|
2020-10-28 06:16:22 +01:00
|
|
|
SET_FOREACH(a, link->addresses_foreign) {
|
|
|
|
if (a->family != AF_INET)
|
|
|
|
continue;
|
|
|
|
if (link_address_is_dynamic(link, a))
|
|
|
|
break;
|
|
|
|
}
|
2020-10-02 11:07:32 +02:00
|
|
|
|
2020-10-28 06:16:22 +01:00
|
|
|
if (!a)
|
|
|
|
return 0;
|
2020-10-02 11:07:32 +02:00
|
|
|
|
2020-12-02 08:20:52 +01:00
|
|
|
log_link_debug(link, "DHCP4 CLIENT: requesting " IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(a->in_addr.in));
|
|
|
|
|
2020-10-28 06:16:22 +01:00
|
|
|
return sd_dhcp_client_set_request_address(link->dhcp_client, &a->in_addr.in);
|
2020-10-02 11:07:32 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 12:12:17 +02:00
|
|
|
int dhcp4_configure(Link *link) {
|
2019-09-26 20:06:02 +02:00
|
|
|
sd_dhcp_option *send_option;
|
2019-09-17 14:49:22 +02:00
|
|
|
void *request_options;
|
2014-08-08 12:12:17 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
2020-10-04 00:37:22 +02:00
|
|
|
|
|
|
|
if (!link_dhcp4_enabled(link))
|
|
|
|
return 0;
|
|
|
|
|
2020-10-28 06:22:09 +01:00
|
|
|
if (!link->dhcp_client) {
|
|
|
|
r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to allocate DHCP4 client: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-10-28 06:22:09 +01:00
|
|
|
r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to attach event to DHCP4 client: %m");
|
2020-10-28 06:22:09 +01:00
|
|
|
}
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2014-10-08 21:15:45 +02:00
|
|
|
r = sd_dhcp_client_set_mac(link->dhcp_client,
|
2020-10-26 18:07:49 +01:00
|
|
|
link->hw_addr.addr.bytes,
|
2020-10-26 14:09:13 +01:00
|
|
|
link->bcast_addr.length > 0 ? link->bcast_addr.addr.bytes : NULL,
|
|
|
|
link->hw_addr.length, link->iftype);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2016-05-23 16:13:18 +02:00
|
|
|
r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-10-28 07:18:23 +01:00
|
|
|
r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link->network->dhcp_broadcast);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
|
2020-10-28 07:18:23 +01:00
|
|
|
if (link->mtu > 0) {
|
2014-08-08 12:12:17 +02:00
|
|
|
r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2016-01-25 21:47:02 +01:00
|
|
|
if (link->network->dhcp_use_mtu) {
|
2020-10-28 07:18:23 +01:00
|
|
|
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_INTERFACE_MTU);
|
2015-08-27 01:18:10 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 19:19:51 +02:00
|
|
|
/* NOTE: even if this variable is called "use", it also "sends" PRL
|
|
|
|
* options, maybe there should be a different configuration variable
|
|
|
|
* to send or not route options?. */
|
2017-11-10 16:03:43 +01:00
|
|
|
/* NOTE: when using Anonymize=yes, routes PRL options are sent
|
|
|
|
* by default, so they don't need to be added here. */
|
2017-08-03 19:19:51 +02:00
|
|
|
if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
|
2020-10-28 07:18:23 +01:00
|
|
|
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_STATIC_ROUTE);
|
2014-08-08 12:12:17 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
|
2018-08-01 03:35:17 +02:00
|
|
|
|
2020-10-28 07:18:23 +01:00
|
|
|
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
|
2015-08-06 00:31:09 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-14 03:29:40 +02:00
|
|
|
if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO && !link->network->dhcp_anonymize) {
|
|
|
|
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m");
|
2019-07-14 03:29:40 +02:00
|
|
|
}
|
|
|
|
|
2017-09-05 12:26:32 +02:00
|
|
|
if (link->network->dhcp_use_ntp) {
|
|
|
|
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
|
2017-09-05 12:26:32 +02:00
|
|
|
}
|
2015-08-27 01:14:20 +02:00
|
|
|
|
2019-09-18 15:22:47 +02:00
|
|
|
if (link->network->dhcp_use_sip) {
|
|
|
|
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m");
|
2019-09-18 15:22:47 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 10:10:50 +02:00
|
|
|
if (link->network->dhcp_use_timezone) {
|
|
|
|
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
|
2017-09-06 10:10:50 +02:00
|
|
|
}
|
2015-08-26 19:19:32 +02:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(request_options, link->network->dhcp_request_options) {
|
2019-09-17 14:49:22 +02:00
|
|
|
uint32_t option = PTR_TO_UINT32(request_options);
|
|
|
|
|
|
|
|
r = sd_dhcp_client_set_request_option(link->dhcp_client, option);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option);
|
2019-09-17 14:49:22 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) {
|
2020-02-28 19:28:49 +01:00
|
|
|
r = sd_dhcp_client_add_option(link->dhcp_client, send_option);
|
|
|
|
if (r == -EEXIST)
|
|
|
|
continue;
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
|
2020-02-28 19:28:49 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) {
|
2020-02-28 19:28:49 +01:00
|
|
|
r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option);
|
|
|
|
if (r == -EEXIST)
|
|
|
|
continue;
|
2019-09-26 20:06:02 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
|
2019-09-26 20:06:02 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 13:02:57 +01:00
|
|
|
r = dhcp4_set_hostname(link);
|
|
|
|
if (r < 0)
|
2018-08-02 09:31:10 +02:00
|
|
|
return r;
|
2014-08-08 12:12:17 +02:00
|
|
|
|
|
|
|
if (link->network->dhcp_vendor_class_identifier) {
|
|
|
|
r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
|
|
|
|
link->network->dhcp_vendor_class_identifier);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 16:43:28 +02:00
|
|
|
if (link->network->dhcp_mudurl) {
|
2020-10-28 07:18:23 +01:00
|
|
|
r = sd_dhcp_client_set_mud_url(link->dhcp_client, link->network->dhcp_mudurl);
|
2020-03-30 16:43:28 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set MUD URL: %m");
|
2020-03-30 16:43:28 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 14:21:02 +02:00
|
|
|
if (link->network->dhcp_user_class) {
|
|
|
|
r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
|
2018-05-07 14:21:02 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 07:18:23 +01:00
|
|
|
if (link->network->dhcp_client_port > 0) {
|
2016-11-11 00:34:19 +01:00
|
|
|
r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
|
2016-11-11 00:34:19 +01:00
|
|
|
}
|
|
|
|
|
2019-05-04 13:40:54 +02:00
|
|
|
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)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m");
|
2019-05-04 13:40:54 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 07:56:38 +02:00
|
|
|
if (link->network->dhcp_ip_service_type > 0) {
|
|
|
|
r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->dhcp_ip_service_type);
|
2019-09-23 13:25:21 +02:00
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set IP service type: %m");
|
2019-09-23 13:25:21 +02:00
|
|
|
}
|
2019-12-20 14:13:18 +01:00
|
|
|
|
2020-05-20 06:23:36 +02:00
|
|
|
if (link->network->dhcp_fallback_lease_lifetime > 0) {
|
|
|
|
r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
|
2020-05-20 06:23:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:16:22 +01:00
|
|
|
r = dhcp4_set_request_address(link);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set initial DHCPv4 address: %m");
|
2020-10-28 06:16:22 +01:00
|
|
|
|
2020-10-14 08:22:56 +02:00
|
|
|
r = dhcp4_configure_dad(link);
|
|
|
|
if (r < 0)
|
2020-10-28 07:18:23 +01:00
|
|
|
return log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to configure service type: %m");
|
2019-12-20 14:13:18 +01:00
|
|
|
|
|
|
|
return dhcp4_set_client_identifier(link);
|
2014-08-08 12:12:17 +02:00
|
|
|
}
|
2019-06-29 20:57:47 +02:00
|
|
|
|
2020-10-02 13:29:57 +02:00
|
|
|
int dhcp4_update_mac(Link *link) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (!link->dhcp_client)
|
|
|
|
return 0;
|
|
|
|
|
2020-10-26 14:09:13 +01:00
|
|
|
r = sd_dhcp_client_set_mac(link->dhcp_client, link->hw_addr.addr.bytes,
|
|
|
|
link->bcast_addr.length > 0 ? link->bcast_addr.addr.bytes : NULL,
|
|
|
|
link->hw_addr.length, link->iftype);
|
2020-10-02 13:29:57 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = dhcp4_set_client_identifier(link);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-14 08:29:47 +02:00
|
|
|
r = dhcp4_dad_update_mac(link);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-02 13:29:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-29 20:57:47 +02:00
|
|
|
int config_parse_dhcp_max_attempts(
|
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
Network *network = data;
|
|
|
|
uint64_t a;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(network);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
|
|
|
|
if (isempty(rvalue)) {
|
|
|
|
network->dhcp_max_attempts = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streq(rvalue, "infinity")) {
|
|
|
|
network->dhcp_max_attempts = (uint64_t) -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = safe_atou64(rvalue, &a);
|
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
2019-06-29 20:57:47 +02:00
|
|
|
"Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a == 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
2019-06-29 20:57:47 +02:00
|
|
|
"%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
network->dhcp_max_attempts = a;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-26 05:28:52 +02:00
|
|
|
int config_parse_dhcp_acl_ip_address(
|
2019-06-29 20:57:47 +02:00
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
Network *network = data;
|
2020-06-26 05:28:52 +02:00
|
|
|
Set **acl;
|
2019-06-29 20:57:47 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
2020-06-26 05:28:52 +02:00
|
|
|
acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip;
|
|
|
|
|
2019-06-29 20:57:47 +02:00
|
|
|
if (isempty(rvalue)) {
|
2020-06-26 05:28:52 +02:00
|
|
|
*acl = set_free(*acl);
|
2019-06-29 20:57:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:12:29 +02:00
|
|
|
for (const char *p = rvalue;;) {
|
2019-06-29 20:57:47 +02:00
|
|
|
_cleanup_free_ char *n = NULL;
|
|
|
|
union in_addr_union ip;
|
|
|
|
|
|
|
|
r = extract_first_word(&p, &n, NULL, 0);
|
2020-07-16 07:48:08 +02:00
|
|
|
if (r == -ENOMEM)
|
|
|
|
return log_oom();
|
2019-06-29 20:57:47 +02:00
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
2020-06-26 05:28:52 +02:00
|
|
|
"Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
|
|
|
|
lvalue, rvalue);
|
2019-06-29 20:57:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (r == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = in_addr_from_string(AF_INET, n, &ip);
|
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
2020-06-26 05:28:52 +02:00
|
|
|
"DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n);
|
2019-06-29 20:57:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-26 05:28:52 +02:00
|
|
|
r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr));
|
2019-06-29 20:57:47 +02:00
|
|
|
if (r < 0)
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
2020-06-26 05:28:52 +02:00
|
|
|
"Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n);
|
2019-06-29 20:57:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 21:44:01 +01:00
|
|
|
int config_parse_dhcp_ip_service_type(
|
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
|
|
|
|
if (streq(rvalue, "CS4"))
|
|
|
|
*((int *)data) = IPTOS_CLASS_CS4;
|
|
|
|
else if (streq(rvalue, "CS6"))
|
|
|
|
*((int *)data) = IPTOS_CLASS_CS6;
|
|
|
|
else
|
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
|
|
|
"Failed to parse IPServiceType type '%s', ignoring.", rvalue);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:43:28 +02:00
|
|
|
int config_parse_dhcp_mud_url(
|
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *unescaped = NULL;
|
|
|
|
Network *network = data;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
|
|
|
|
if (isempty(rvalue)) {
|
|
|
|
network->dhcp_mudurl = mfree(network->dhcp_mudurl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = cunescape(rvalue, 0, &unescaped);
|
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
2020-03-30 16:43:28 +02:00
|
|
|
"Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
2020-03-30 16:43:28 +02:00
|
|
|
"Failed to parse MUD URL '%s', ignoring: %m", rvalue);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return free_and_strdup_warn(&network->dhcp_mudurl, unescaped);
|
|
|
|
}
|
|
|
|
|
2020-05-20 06:23:36 +02:00
|
|
|
int config_parse_dhcp_fallback_lease_lifetime(const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
Network *network = userdata;
|
2020-05-21 08:17:45 +02:00
|
|
|
uint32_t k;
|
2020-05-20 06:23:36 +02:00
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(section);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
if (isempty(rvalue)) {
|
|
|
|
network->dhcp_fallback_lease_lifetime = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We accept only "forever" or "infinity". */
|
|
|
|
if (STR_IN_SET(rvalue, "forever", "infinity"))
|
|
|
|
k = CACHE_INFO_INFINITY_LIFE_TIME;
|
|
|
|
else {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
2020-05-20 06:23:36 +02:00
|
|
|
"Invalid LeaseLifetime= value, ignoring: %s", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
network->dhcp_fallback_lease_lifetime = k;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-29 20:57:47 +02:00
|
|
|
static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
|
|
|
|
[DHCP_CLIENT_ID_MAC] = "mac",
|
|
|
|
[DHCP_CLIENT_ID_DUID] = "duid",
|
|
|
|
[DHCP_CLIENT_ID_DUID_ONLY] = "duid-only",
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
|
|
|
|
DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier,
|
|
|
|
"Failed to parse client identifier type");
|