/* SPDX-License-Identifier: LGPL-2.1+ */
#include <netinet/in.h>
+#include <netinet/ip.h>
#include <linux/if.h>
#include <linux/if_arp.h>
+#include "escape.h"
#include "alloc-util.h"
#include "dhcp-client-internal.h"
#include "hostname-util.h"
#include "string-table.h"
#include "string-util.h"
#include "sysctl-util.h"
+#include "web-util.h"
-static int dhcp_remove_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all);
-static int dhcp_remove_router(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all);
-static int dhcp_remove_dns_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all);
+static int dhcp_remove_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all, link_netlink_message_handler_t callback);
+static int dhcp_remove_router(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all, link_netlink_message_handler_t callback);
+static int dhcp_remove_dns_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all, link_netlink_message_handler_t callback);
static int dhcp_remove_address(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, link_netlink_message_handler_t callback);
-static int dhcp_remove_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link);
-static int dhcp_lease_renew(sd_dhcp_client *client, Link *link);
+static int dhcp4_update_address(Link *link, bool announce);
+static int dhcp4_remove_all(Link *link);
void dhcp4_release_old_lease(Link *link) {
struct in_addr address = {}, address_old = {};
(void) sd_dhcp_lease_get_address(link->dhcp_lease_old, &address_old);
(void) sd_dhcp_lease_get_address(link->dhcp_lease, &address);
- (void) dhcp_remove_routes(link, link->dhcp_lease_old, &address_old, false);
- (void) dhcp_remove_router(link, link->dhcp_lease_old, &address_old, false);
- (void) dhcp_remove_dns_routes(link, link->dhcp_lease_old, &address_old, false);
+ (void) dhcp_remove_routes(link, link->dhcp_lease_old, &address_old, false, NULL);
+ (void) dhcp_remove_router(link, link->dhcp_lease_old, &address_old, false, NULL);
+ (void) dhcp_remove_dns_routes(link, link->dhcp_lease_old, &address_old, false, NULL);
if (!in4_addr_equal(&address_old, &address))
(void) dhcp_remove_address(link, link->dhcp_lease_old, &address_old, NULL);
/* 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. */
- log_link_message_debug_errno(link, m, r, "Could not set DHCPv4 route, retrying later: %m");
+ log_link_message_debug_errno(link, m, r, "Could not set DHCPv4 route, retrying later");
link->dhcp4_route_failed = true;
link->manager->dhcp4_prefix_root_cannot_set_table = true;
} else if (r < 0 && r != -EEXIST) {
- log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 route: %m");
+ log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 route");
link_enter_failed(link);
return 1;
}
if (link->dhcp4_messages == 0) {
if (link->dhcp4_route_failed) {
- struct in_addr address = {};
-
link->dhcp4_route_failed = false;
link->dhcp4_route_retrying = true;
- (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address);
- (void) dhcp_remove_routes(link, link->dhcp_lease, &address, true);
- (void) dhcp_remove_router(link, link->dhcp_lease, &address, true);
- (void) dhcp_remove_dns_routes(link, link->dhcp_lease, &address, true);
- (void) dhcp_remove_address(link, link->dhcp_lease, &address, dhcp_remove_address_handler);
-
+ r = dhcp4_remove_all(link);
+ if (r < 0)
+ link_enter_failed(link);
return 1;
}
if (!link->network->dhcp_send_decline)
r = route_new(&route);
if (r < 0)
- return log_link_error_errno(link, r, "Could not allocate route: %m");
+ return log_link_error_errno(link, r, "Could not allocate route: %m");
/* Set routes to DNS servers. */
if (!link->network) /* link went down while we configured the IP addresses? */
return 0;
- if (!link->network->dhcp_use_routes)
- return 0;
-
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. */
r = dhcp_prefix_route_from_lease(link->dhcp_lease, table, &address, &prefix_route);
if (r < 0)
- return log_link_error_errno(link, r, "Could not create prefix route: %m");
+ return log_link_error_errno(link, r, "Could not create prefix route: %m");
r = dhcp_route_configure(&prefix_route, link);
if (r < 0)
}
}
- for (i = 0; i < n; i++) {
- _cleanup_(route_freep) Route *route = NULL;
+ if (link->network->dhcp_use_routes) {
+ for (i = 0; i < n; i++) {
+ _cleanup_(route_freep) Route *route = NULL;
- /* 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 &&
- sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
- continue;
+ /* 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 &&
+ 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");
+ 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;
+ 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;
+
+ r = dhcp_route_configure(&route, link);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Could not set route: %m");
+ }
+ }
- route->family = AF_INET;
- route->protocol = RTPROT_DHCP;
- 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 (link->network->dhcp_use_gateway) {
+ 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.");
- if (set_contains(link->dhcp_routes, route))
- continue;
+ /* 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");
- r = dhcp_route_configure(&route, link);
- if (r < 0)
- return log_link_error_errno(link, r, "Could not set route: %m");
- }
+ if (r > 0 && !classless_route && !in4_addr_is_null(&router[0])) {
+ _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
- 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.");
+ r = route_new(&route_gw);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Could not allocate route: %m");
+
+ /* 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;
+
+ r = dhcp_route_configure(&route_gw, link);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Could not set host route: %m");
- /* 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");
+ r = route_new(&route);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Could not allocate route: %m");
- if (r > 0 && !classless_route && !in4_addr_is_null(&router[0])) {
- _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
+ 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;
+ route->mtu = link->network->dhcp_route_mtu;
- r = route_new(&route_gw);
- if (r < 0)
- return log_link_error_errno(link, r, "Could not allocate route: %m");
-
- /* 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;
-
- r = dhcp_route_configure(&route_gw, link);
- if (r < 0)
- return log_link_error_errno(link, r, "Could not set host route: %m");
+ r = dhcp_route_configure(&route, link);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Could not set router: %m");
+ }
- r = route_new(&route);
- if (r < 0)
- return log_link_error_errno(link, r, "Could not allocate route: %m");
+ Route *rt;
+ LIST_FOREACH(routes, rt, link->network->static_routes) {
+ if (!rt->gateway_from_dhcp)
+ continue;
- 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;
- route->mtu = link->network->dhcp_route_mtu;
+ if (rt->family != AF_INET)
+ continue;
- r = dhcp_route_configure(&route, link);
- if (r < 0)
- return log_link_error_errno(link, r, "Could not set router: %m");
+ rt->gw.in = router[0];
+
+ r = route_configure(rt, link, dhcp4_route_handler);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Could not set gateway: %m");
+ if (r > 0)
+ link->dhcp4_messages++;
+ }
}
- Route *rt;
- LIST_FOREACH(routes, rt, link->network->static_routes) {
- if (!rt->gateway_from_dhcp)
- continue;
+ return link_set_dns_routes(link, &address);
+}
- if (rt->family != AF_INET)
- continue;
+static int dhcp_route_remove(Route *route, Link *link, link_netlink_message_handler_t callback) {
+ int r;
- rt->gw.in = router[0];
+ r = route_remove(route, link, callback);
+ if (r < 0)
+ return r;
- r = route_configure(rt, link, dhcp4_route_handler);
- if (r < 0)
- return log_link_error_errno(link, r, "Could not set gateway: %m");
- if (r > 0)
- link->dhcp4_messages++;
- }
+ if (callback)
+ link->dhcp4_remove_messages++;
- return link_set_dns_routes(link, &address);
+ return 0;
}
-static int dhcp_remove_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all) {
+static int dhcp_remove_routes(
+ Link *link,
+ sd_dhcp_lease *lease,
+ const struct in_addr *address,
+ bool remove_all,
+ link_netlink_message_handler_t callback) {
+
_cleanup_free_ sd_dhcp_route **routes = NULL;
uint32_t table;
int n, i, r;
if (!remove_all && set_contains(link->dhcp_routes, route))
continue;
- (void) route_remove(route, link, NULL);
+ r = dhcp_route_remove(route, link, callback);
+ if (r < 0)
+ return r;
}
return n;
}
-static int dhcp_remove_router(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all) {
+static int dhcp_remove_router(
+ Link *link,
+ sd_dhcp_lease *lease,
+ const struct in_addr *address,
+ bool remove_all,
+ link_netlink_message_handler_t callback) {
+
_cleanup_(route_freep) Route *route_gw = NULL, *route = NULL;
const struct in_addr *router;
uint32_t table;
assert(link);
assert(address);
- if (!link->network->dhcp_use_routes)
+ if (!link->network->dhcp_use_gateway)
return 0;
r = sd_dhcp_lease_get_router(lease, &router);
route_gw->priority = link->network->dhcp_route_metric;
route_gw->table = table;
- if (remove_all || !set_contains(link->dhcp_routes, route_gw))
- (void) route_remove(route_gw, link, NULL);
+ if (remove_all || !set_contains(link->dhcp_routes, route_gw)) {
+ r = dhcp_route_remove(route_gw, link, callback);
+ if (r < 0)
+ return r;
+ }
r = route_new(&route);
if (r < 0)
route->priority = link->network->dhcp_route_metric;
route->table = table;
- if (remove_all || !set_contains(link->dhcp_routes, route))
- (void) route_remove(route, link, NULL);
+ if (remove_all || !set_contains(link->dhcp_routes, route)) {
+ r = dhcp_route_remove(route, link, callback);
+ if (r < 0)
+ return r;
+ }
Route *rt;
LIST_FOREACH(routes, rt, link->network->static_routes) {
if (!remove_all && in4_addr_equal(router, &rt->gw.in))
continue;
- (void) route_remove(rt, link, NULL);
+ r = dhcp_route_remove(rt, link, callback);
+ if (r < 0)
+ return r;
}
return 0;
}
-static int dhcp_remove_dns_routes(Link *link, sd_dhcp_lease *lease, const struct in_addr *address, bool remove_all) {
+static int dhcp_remove_dns_routes(
+ Link *link,
+ sd_dhcp_lease *lease,
+ const struct in_addr *address,
+ bool remove_all,
+ link_netlink_message_handler_t callback) {
+
const struct in_addr *dns;
uint32_t table;
int i, n, r;
r = route_new(&route);
if (r < 0)
- return log_link_error_errno(link, r, "Could not allocate route: %m");
+ return log_link_error_errno(link, r, "Could not allocate route: %m");
route->family = AF_INET;
route->dst.in = dns[i];
if (!remove_all && set_contains(link->dhcp_routes, route))
continue;
- (void) route_remove(route, link, NULL);
+ r = dhcp_route_remove(route, link, callback);
+ if (r < 0)
+ return r;
}
if (!link_prefixroute(link)) {
r = dhcp_prefix_route_from_lease(lease, table, address, &prefix_route);
if (r < 0)
- return log_link_warning_errno(link, r, "Could not delete prefix route: %m");
+ return log_link_warning_errno(link, r, "Could not create prefix route: %m");
- if (remove_all || !set_contains(link->dhcp_routes, prefix_route))
- (void) route_remove(prefix_route, link, NULL);
+ if (remove_all || !set_contains(link->dhcp_routes, prefix_route)) {
+ r = dhcp_route_remove(prefix_route, link, callback);
+ if (r < 0)
+ return r;
+ }
}
return 0;
}
-static int dhcp_remove_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
- int r;
-
- assert(link);
-
- /* This is only used when retrying to assign the address received from DHCPv4 server.
- * See dhcp4_route_handler(). */
-
- if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
- return 1;
-
- r = sd_netlink_message_get_errno(m);
- if (r < 0)
- log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 address, ignoring");
- else
- (void) manager_rtnl_process_address(rtnl, m, link->manager);
-
- (void) dhcp_lease_renew(link->dhcp_client, link);
- return 1;
-}
-
static int dhcp_remove_address(
Link *link, sd_dhcp_lease *lease,
const struct in_addr *address,
if (sd_dhcp_lease_get_netmask(lease, &netmask) >= 0)
a->prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
- (void) address_remove(a, link, callback);
+ r = address_remove(a, link, callback);
+ if (r < 0)
+ return r;
+
+ if (callback)
+ link->dhcp4_remove_messages++;
return 0;
}
return 0;
}
+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) {
+ struct in_addr address;
+ int r;
+
+ assert(link);
+ assert(link->dhcp_lease);
+
+ r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
+ if (r < 0)
+ return log_link_error_errno(link, r, "Failed to get DHCPv4 address: %m");
+
+ r = dhcp_remove_routes(link, link->dhcp_lease, &address, true, dhcp4_remove_route_handler);
+ if (r < 0)
+ return r;
+
+ r = dhcp_remove_router(link, link->dhcp_lease, &address, true, dhcp4_remove_route_handler);
+ if (r < 0)
+ return r;
+
+ r = dhcp_remove_dns_routes(link, link->dhcp_lease, &address, true, dhcp4_remove_route_handler);
+ if (r < 0)
+ return r;
+
+ r = dhcp_remove_address(link, link->dhcp_lease, &address, dhcp4_remove_address_handler);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
static int dhcp_lease_lost(Link *link) {
- struct in_addr address = {};
+ int r;
assert(link);
assert(link->dhcp_lease);
link->dhcp4_configured = false;
- (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address);
- (void) dhcp_remove_routes(link, link->dhcp_lease, &address, true);
- (void) dhcp_remove_router(link, link->dhcp_lease, &address, true);
- (void) dhcp_remove_dns_routes(link, link->dhcp_lease, &address, true);
- (void) dhcp_remove_address(link, link->dhcp_lease, &address, NULL);
- (void) dhcp_reset_mtu(link);
- (void) dhcp_reset_hostname(link);
+ r = dhcp4_remove_all(link);
+ if (r < 0)
+ return r;
+
+ r = dhcp_reset_mtu(link);
+ if (r < 0)
+ return r;
+
+ r = dhcp_reset_hostname(link);
+ if (r < 0)
+ return r;
link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
link_dirty(link);
return 1;
}
-static int dhcp4_update_address(Link *link,
- struct in_addr *address,
- struct in_addr *netmask,
- uint32_t lifetime) {
+static int dhcp4_update_address(Link *link, bool announce) {
_cleanup_(address_freep) Address *addr = NULL;
+ uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
+ struct in_addr address, netmask;
unsigned prefixlen;
int r;
- assert(address);
- assert(netmask);
- assert(lifetime);
+ assert(link);
+ assert(link->network);
+
+ if (!link->dhcp_lease)
+ return 0;
+
+ link_set_state(link, LINK_STATE_CONFIGURING);
+ link->dhcp4_configured = false;
+
+ /* address_handler calls link_request_set_routes() and link_request_set_nexthop(). Before they
+ * are called, the related flags must be cleared. Otherwise, the link becomes configured state
+ * before routes are configured. */
+ link->static_routes_configured = false;
+ link->static_nexthops_configured = false;
- prefixlen = in4_addr_netmask_to_prefixlen(netmask);
+ 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),
+ LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
+ ADDRESS_FMT_VAL(address),
+ prefixlen,
+ 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(router[0]));
+ else
+ log_struct(LOG_INFO,
+ LOG_LINK_INTERFACE(link),
+ LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
+ ADDRESS_FMT_VAL(address),
+ prefixlen),
+ "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
+ "PREFIXLEN=%u", prefixlen);
+ }
r = address_new(&addr);
if (r < 0)
- return r;
+ return log_oom();
addr->family = AF_INET;
- addr->in_addr.in.s_addr = address->s_addr;
+ addr->in_addr.in.s_addr = address.s_addr;
addr->cinfo.ifa_prefered = lifetime;
addr->cinfo.ifa_valid = lifetime;
addr->prefixlen = prefixlen;
- addr->broadcast.s_addr = address->s_addr | ~netmask->s_addr;
+ addr->broadcast.s_addr = address.s_addr | ~netmask.s_addr;
addr->prefix_route = link_prefixroute(link);
/* allow reusing an existing address and simply update its lifetime
static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
sd_dhcp_lease *lease;
- struct in_addr address;
- struct in_addr netmask;
- uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
int r;
assert(link);
assert(client);
- assert(link->network);
r = sd_dhcp_client_get_lease(client, &lease);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
sd_dhcp_lease_unref(link->dhcp_lease);
- link->dhcp4_configured = false;
link->dhcp_lease = sd_dhcp_lease_ref(lease);
link_dirty(link);
- r = sd_dhcp_lease_get_address(lease, &address);
- if (r < 0)
- return log_link_warning_errno(link, r, "DHCP error: no address: %m");
-
- r = sd_dhcp_lease_get_netmask(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");
- }
-
- r = dhcp4_update_address(link, &address, &netmask, lifetime);
- if (r < 0)
- return log_link_warning_errno(link, r, "Could not update IP address: %m");
-
- return 0;
+ return dhcp4_update_address(link, false);
}
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;
- 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");
- r = sd_dhcp_lease_get_address(lease, &address);
- if (r < 0)
- return log_link_error_errno(link, r, "DHCP error: No address: %m");
-
- r = sd_dhcp_lease_get_netmask(lease, &netmask);
- if (r < 0)
- return log_link_error_errno(link, r, "DHCP error: No netmask: %m");
-
- prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
-
- if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
- r = sd_dhcp_lease_get_lifetime(lease, &lifetime);
- if (r < 0)
- return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
- }
-
- 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 && !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(router[0])),
- "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
- "PREFIXLEN=%u", prefixlen,
- "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
- else
- log_struct(LOG_INFO,
- LOG_LINK_INTERFACE(link),
- LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u",
- ADDRESS_FMT_VAL(address),
- prefixlen),
- "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
- "PREFIXLEN=%u", prefixlen);
-
+ sd_dhcp_lease_unref(link->dhcp_lease);
link->dhcp_lease = sd_dhcp_lease_ref(lease);
link_dirty(link);
}
}
- r = dhcp4_update_address(link, &address, &netmask, lifetime);
- if (r < 0)
- return log_link_warning_errno(link, r, "Could not update IP address: %m");
+ 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.");
return 0;
}
link->dhcp_lease_old = TAKE_PTR(link->dhcp_lease);
- /* On ip address change, to keep the connectability, we would like to assign new address and
+ /* On IP address change, to keep the connectability, we would like to assign new address and
* routes, and then release old lease. There are two possible success paths:
*
* 1. new address and routes are configured.
return 0;
}
-static int dhcp_server_is_black_listed(Link *link, sd_dhcp_client *client) {
+static int dhcp_server_is_deny_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_deny_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 deny-listed IP addresses, ignoring offer",
+ ADDRESS_FMT_VAL(addr)));
+ return true;
+ }
+
+ return false;
+}
+
+static int dhcp_server_is_allow_listed(Link *link, sd_dhcp_client *client) {
sd_dhcp_lease *lease;
struct in_addr addr;
int r;
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");
+ 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))) {
+ if (set_contains(link->network->dhcp_allow_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",
+ LOG_LINK_MESSAGE(link, "DHCPv4 IP '%u.%u.%u.%u' found in allow-listed IP addresses, accepting offer",
ADDRESS_FMT_VAL(addr)));
return true;
}
}
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;
-
+ 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;
+ }
break;
default:
if (event < 0)
return log_oom();
if (r < 0)
return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
- }
- r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
- if (r < 0)
- return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
+ r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
+ if (r < 0)
+ return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
+ }
r = sd_dhcp_client_set_mac(link->dhcp_client,
(const uint8_t *) &link->mac,
}
ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options, i) {
- r = sd_dhcp_client_set_dhcp_option(link->dhcp_client, send_option);
+ r = sd_dhcp_client_add_option(link->dhcp_client, send_option);
+ if (r == -EEXIST)
+ continue;
+ if (r < 0)
+ return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
+ }
+
+ ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options, i) {
+ r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option);
+ if (r == -EEXIST)
+ continue;
if (r < 0)
return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m");
}
return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
}
+ if (link->network->dhcp_mudurl) {
+ r = sd_dhcp_client_set_mud_url(link->dhcp_client,
+ link->network->dhcp_mudurl);
+ if (r < 0)
+ return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MUD URL: %m");
+ }
+
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)
if (link->network->ip_service_type > 0) {
r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->ip_service_type);
if (r < 0)
- return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ip service type: %m");
+ return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IP service type: %m");
+ }
+
+ 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)
+ return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed set to lease lifetime: %m");
}
if (link->network->dhcp_send_decline) {
return 0;
}
-int config_parse_dhcp_black_listed_ip_address(
+int config_parse_dhcp_acl_ip_address(
const char *unit,
const char *filename,
unsigned line,
void *userdata) {
Network *network = data;
- const char *p;
+ Set **acl;
int r;
assert(filename);
assert(rvalue);
assert(data);
+ acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip;
+
if (isempty(rvalue)) {
- network->dhcp_black_listed_ip = set_free(network->dhcp_black_listed_ip);
+ *acl = set_free(*acl);
return 0;
}
- for (p = rvalue;;) {
+ for (const char *p = rvalue;;) {
_cleanup_free_ char *n = NULL;
union in_addr_union ip;
r = extract_first_word(&p, &n, NULL, 0);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r,
- "Failed to parse DHCP black listed ip address, ignoring assignment: %s",
- rvalue);
+ "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s",
+ lvalue, rvalue);
return 0;
}
if (r == 0)
r = in_addr_from_string(AF_INET, n, &ip);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r,
- "DHCP black listed ip address is invalid, ignoring assignment: %s", n);
+ "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n);
continue;
}
- r = set_ensure_allocated(&network->dhcp_black_listed_ip, NULL);
- if (r < 0)
- return log_oom();
-
- r = set_put(network->dhcp_black_listed_ip, UINT32_TO_PTR(ip.in.s_addr));
+ r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr));
if (r < 0)
log_syntax(unit, LOG_ERR, filename, line, r,
- "Failed to store DHCP black listed ip address '%s', ignoring assignment: %m", n);
+ "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n);
}
return 0;
}
-int config_parse_dhcp_user_class(
+int config_parse_dhcp_ip_service_type(
const char *unit,
const char *filename,
unsigned line,
void *data,
void *userdata) {
- char ***l = data;
- int r;
-
- assert(l);
+ assert(filename);
assert(lvalue);
assert(rvalue);
- if (isempty(rvalue)) {
- *l = strv_free(*l);
- return 0;
- }
-
- for (;;) {
- _cleanup_free_ char *w = NULL;
-
- r = extract_first_word(&rvalue, &w, NULL, 0);
- if (r == -ENOMEM)
- return log_oom();
- if (r < 0) {
- log_syntax(unit, LOG_ERR, filename, line, r,
- "Failed to split user classes option, ignoring: %s", rvalue);
- break;
- }
- if (r == 0)
- break;
-
- if (strlen(w) > 255) {
- log_syntax(unit, LOG_ERR, filename, line, 0,
- "%s length is not in the range 1-255, ignoring.", w);
- continue;
- }
-
- r = strv_push(l, w);
- if (r < 0)
- return log_oom();
-
- w = NULL;
- }
+ 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;
}
-int config_parse_dhcp_request_options(
+int config_parse_dhcp_mud_url(
const char *unit,
const char *filename,
unsigned line,
void *data,
void *userdata) {
+ _cleanup_free_ char *unescaped = NULL;
Network *network = data;
- const char *p;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
- assert(data);
if (isempty(rvalue)) {
- network->dhcp_request_options = set_free(network->dhcp_request_options);
+ network->dhcp_mudurl = mfree(network->dhcp_mudurl);
return 0;
}
- for (p = rvalue;;) {
- _cleanup_free_ char *n = NULL;
- uint32_t i;
+ r = cunescape(rvalue, 0, &unescaped);
+ if (r < 0) {
+ log_syntax(unit, LOG_ERR, filename, line, r,
+ "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
+ return 0;
+ }
- r = extract_first_word(&p, &n, NULL, 0);
- if (r < 0) {
- log_syntax(unit, LOG_ERR, filename, line, r,
- "Failed to parse DHCP request option, ignoring assignment: %s",
- rvalue);
- return 0;
- }
- if (r == 0)
- return 0;
+ if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
+ log_syntax(unit, LOG_ERR, filename, line, 0,
+ "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
- r = safe_atou32(n, &i);
- if (r < 0) {
- log_syntax(unit, LOG_ERR, filename, line, r,
- "DHCP request option is invalid, ignoring assignment: %s", n);
- continue;
- }
+ return 0;
+ }
- if (i < 1 || i >= 255) {
- log_syntax(unit, LOG_ERR, filename, line, r,
- "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n);
- continue;
- }
+ return free_and_strdup_warn(&network->dhcp_mudurl, unescaped);
+}
- r = set_ensure_allocated(&network->dhcp_request_options, NULL);
- if (r < 0)
- return log_oom();
+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;
+ uint32_t k;
- r = set_put(network->dhcp_request_options, UINT32_TO_PTR(i));
- if (r < 0)
- log_syntax(unit, LOG_ERR, filename, line, r,
- "Failed to store DHCP request option '%s', ignoring assignment: %m", n);
+ 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 {
+ log_syntax(unit, LOG_ERR, filename, line, 0,
+ "Invalid LeaseLifetime= value, ignoring: %s", rvalue);
+ return 0;
+ }
+
+ network->dhcp_fallback_lease_lifetime = k;
+
return 0;
}