| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_arp.h> |
| 4 |
#include <linux/pkt_sched.h> |
| 5 |
#include <linux/rtnetlink.h> |
| 6 |
#include <netinet/in.h> |
| 7 |
#include <netinet/ip.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#include "sd-dhcp-protocol.h" |
| 11 |
#include "sd-ipv4ll.h" |
| 12 |
|
| 13 |
#include "alloc-util.h" |
| 14 |
#include "conf-parser.h" |
| 15 |
#include "device-private.h" |
| 16 |
#include "dhcp-client-internal.h" |
| 17 |
#include "errno-util.h" |
| 18 |
#include "hostname-setup.h" |
| 19 |
#include "networkd-address.h" |
| 20 |
#include "networkd-dhcp-prefix-delegation.h" |
| 21 |
#include "networkd-dhcp4.h" |
| 22 |
#include "networkd-dhcp4-bus.h" |
| 23 |
#include "networkd-ipv4ll.h" |
| 24 |
#include "networkd-link.h" |
| 25 |
#include "networkd-manager.h" |
| 26 |
#include "networkd-network.h" |
| 27 |
#include "networkd-nexthop.h" |
| 28 |
#include "networkd-ntp.h" |
| 29 |
#include "networkd-queue.h" |
| 30 |
#include "networkd-route.h" |
| 31 |
#include "networkd-setlink.h" |
| 32 |
#include "networkd-state-file.h" |
| 33 |
#include "networkd-wwan.h" |
| 34 |
#include "parse-util.h" |
| 35 |
#include "set.h" |
| 36 |
#include "socket-util.h" |
| 37 |
#include "string-table.h" |
| 38 |
#include "string-util.h" |
| 39 |
#include "strv.h" |
| 40 |
|
| 41 |
void network_adjust_dhcp4(Network *network) { |
| 42 |
assert(network); |
| 43 |
|
| 44 |
if (!FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4)) |
| 45 |
return; |
| 46 |
|
| 47 |
if (network->dhcp_use_gateway < 0) |
| 48 |
network->dhcp_use_gateway = network->dhcp_use_routes; |
| 49 |
|
| 50 |
/* RFC7844 section 3.: MAY contain the Client Identifier option |
| 51 |
* Section 3.5: clients MUST use client identifiers based solely on the link-layer address |
| 52 |
* NOTE: Using MAC, as it does not reveal extra information, and some servers might not answer |
| 53 |
* if this option is not sent */ |
| 54 |
if (network->dhcp_anonymize && |
| 55 |
network->dhcp_client_identifier >= 0 && |
| 56 |
network->dhcp_client_identifier != DHCP_CLIENT_ID_MAC) { |
| 57 |
log_warning("%s: ClientIdentifier= is set, although Anonymize=yes. Using ClientIdentifier=mac.", |
| 58 |
network->filename); |
| 59 |
network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC; |
| 60 |
} |
| 61 |
|
| 62 |
if (network->dhcp_client_identifier < 0) |
| 63 |
network->dhcp_client_identifier = network->dhcp_anonymize ? DHCP_CLIENT_ID_MAC : network->manager->dhcp_client_identifier; |
| 64 |
|
| 65 |
/* By default, RapidCommit= is enabled when Anonymize=no and neither AllowList= nor DenyList= is specified. */ |
| 66 |
if (network->dhcp_use_rapid_commit < 0) |
| 67 |
network->dhcp_use_rapid_commit = |
| 68 |
!network->dhcp_anonymize && |
| 69 |
set_isempty(network->dhcp_allow_listed_ip) && |
| 70 |
set_isempty(network->dhcp_deny_listed_ip); |
| 71 |
} |
| 72 |
|
| 73 |
static int dhcp4_prefix_covers( |
| 74 |
Link *link, |
| 75 |
const struct in_addr *in_prefix, |
| 76 |
uint8_t in_prefixlen) { |
| 77 |
|
| 78 |
struct in_addr prefix; |
| 79 |
uint8_t prefixlen; |
| 80 |
int r; |
| 81 |
|
| 82 |
assert(link); |
| 83 |
assert(link->dhcp_lease); |
| 84 |
assert(in_prefix); |
| 85 |
|
| 86 |
/* Return true if the input address or address range is in the assigned network. |
| 87 |
* E.g. if the DHCP server provides 192.168.0.100/24, then this returns true for the address or |
| 88 |
* address range in 192.168.0.0/24, and returns false otherwise. */ |
| 89 |
|
| 90 |
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen); |
| 91 |
if (r < 0) |
| 92 |
return r; |
| 93 |
|
| 94 |
return in4_addr_prefix_covers_full(&prefix, prefixlen, in_prefix, in_prefixlen); |
| 95 |
} |
| 96 |
|
| 97 |
static int dhcp4_get_router(Link *link, struct in_addr *ret) { |
| 98 |
const struct in_addr *routers; |
| 99 |
int r; |
| 100 |
|
| 101 |
assert(link); |
| 102 |
assert(link->dhcp_lease); |
| 103 |
assert(ret); |
| 104 |
|
| 105 |
r = sd_dhcp_lease_get_router(link->dhcp_lease, &routers); |
| 106 |
if (r < 0) |
| 107 |
return r; |
| 108 |
|
| 109 |
/* The router option may provide multiple routers, We only use the first non-null address. */ |
| 110 |
|
| 111 |
FOREACH_ARRAY(router, routers, r) { |
| 112 |
if (in4_addr_is_null(router)) |
| 113 |
continue; |
| 114 |
|
| 115 |
*ret = *router; |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
return -ENODATA; |
| 120 |
} |
| 121 |
|
| 122 |
static int dhcp4_get_classless_static_or_static_routes(Link *link, sd_dhcp_route ***ret_routes, size_t *ret_num) { |
| 123 |
_cleanup_free_ sd_dhcp_route **routes = NULL; |
| 124 |
int r; |
| 125 |
|
| 126 |
assert(link); |
| 127 |
assert(link->dhcp_lease); |
| 128 |
|
| 129 |
/* If the DHCP server returns both a Classless Static Routes option and a Static Routes option, |
| 130 |
* the DHCP client MUST ignore the Static Routes option. */ |
| 131 |
|
| 132 |
r = sd_dhcp_lease_get_classless_routes(link->dhcp_lease, &routes); |
| 133 |
if (r >= 0) { |
| 134 |
assert(r > 0); |
| 135 |
if (ret_routes) |
| 136 |
*ret_routes = TAKE_PTR(routes); |
| 137 |
if (ret_num) |
| 138 |
*ret_num = r; |
| 139 |
return 1; /* classless */ |
| 140 |
} else if (r != -ENODATA) |
| 141 |
return r; |
| 142 |
|
| 143 |
r = sd_dhcp_lease_get_static_routes(link->dhcp_lease, &routes); |
| 144 |
if (r < 0) |
| 145 |
return r; |
| 146 |
|
| 147 |
assert(r > 0); |
| 148 |
if (ret_routes) |
| 149 |
*ret_routes = TAKE_PTR(routes); |
| 150 |
if (ret_num) |
| 151 |
*ret_num = r; |
| 152 |
return 0; /* static */ |
| 153 |
} |
| 154 |
|
| 155 |
static int dhcp4_find_gateway_for_destination( |
| 156 |
Link *link, |
| 157 |
const struct in_addr *destination, |
| 158 |
uint8_t prefixlength, |
| 159 |
struct in_addr *ret) { |
| 160 |
|
| 161 |
_cleanup_free_ sd_dhcp_route **routes = NULL; |
| 162 |
size_t n_routes = 0; |
| 163 |
bool is_classless; |
| 164 |
uint8_t max_prefixlen = UINT8_MAX; |
| 165 |
struct in_addr gw; |
| 166 |
int r; |
| 167 |
|
| 168 |
assert(link); |
| 169 |
assert(link->dhcp_lease); |
| 170 |
assert(destination); |
| 171 |
assert(ret); |
| 172 |
|
| 173 |
/* This tries to find the most suitable gateway for an address or address range. |
| 174 |
* E.g. if the server provides the default gateway 192.168.0.1 and a classless static route for |
| 175 |
* 8.0.0.0/8 with gateway 192.168.0.2, then this returns 192.168.0.2 for 8.8.8.8/32, and 192.168.0.1 |
| 176 |
* for 9.9.9.9/32. If the input address or address range is in the assigned network, then the null |
| 177 |
* address will be returned. */ |
| 178 |
|
| 179 |
/* First, check with the assigned prefix, and if the destination is in the prefix, set the null |
| 180 |
* address for the gateway, and return it unless more suitable static route is found. */ |
| 181 |
r = dhcp4_prefix_covers(link, destination, prefixlength); |
| 182 |
if (r < 0) |
| 183 |
return r; |
| 184 |
if (r > 0) { |
| 185 |
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &max_prefixlen); |
| 186 |
if (r < 0) |
| 187 |
return r; |
| 188 |
|
| 189 |
gw = (struct in_addr) {}; |
| 190 |
} |
| 191 |
|
| 192 |
r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes); |
| 193 |
if (r < 0 && r != -ENODATA) |
| 194 |
return r; |
| 195 |
is_classless = r > 0; |
| 196 |
|
| 197 |
/* First, find most suitable gateway. */ |
| 198 |
FOREACH_ARRAY(e, routes, n_routes) { |
| 199 |
struct in_addr dst; |
| 200 |
uint8_t len; |
| 201 |
|
| 202 |
r = sd_dhcp_route_get_destination(*e, &dst); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
|
| 206 |
r = sd_dhcp_route_get_destination_prefix_length(*e, &len); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
r = in4_addr_prefix_covers_full(&dst, len, destination, prefixlength); |
| 211 |
if (r < 0) |
| 212 |
return r; |
| 213 |
if (r == 0) |
| 214 |
continue; |
| 215 |
|
| 216 |
if (max_prefixlen != UINT8_MAX && max_prefixlen > len) |
| 217 |
continue; |
| 218 |
|
| 219 |
r = sd_dhcp_route_get_gateway(*e, &gw); |
| 220 |
if (r < 0) |
| 221 |
return r; |
| 222 |
|
| 223 |
max_prefixlen = len; |
| 224 |
} |
| 225 |
|
| 226 |
/* The destination is reachable. Note, the gateway address returned here may be NULL. */ |
| 227 |
if (max_prefixlen != UINT8_MAX) { |
| 228 |
*ret = gw; |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
/* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and |
| 233 |
* a Router option, the DHCP client MUST ignore the Router option. */ |
| 234 |
if (!is_classless) { |
| 235 |
/* No matching static route is found, and the destination is not in the acquired network, |
| 236 |
* falling back to the Router option. */ |
| 237 |
r = dhcp4_get_router(link, ret); |
| 238 |
if (r >= 0) |
| 239 |
return 0; |
| 240 |
if (r != -ENODATA) |
| 241 |
return r; |
| 242 |
} |
| 243 |
|
| 244 |
return -EHOSTUNREACH; /* Cannot reach the destination. */ |
| 245 |
} |
| 246 |
|
| 247 |
static int dhcp4_remove_address_and_routes(Link *link, bool only_marked) { |
| 248 |
Address *address; |
| 249 |
Route *route; |
| 250 |
int ret = 0; |
| 251 |
|
| 252 |
assert(link); |
| 253 |
assert(link->manager); |
| 254 |
|
| 255 |
SET_FOREACH(route, link->manager->routes) { |
| 256 |
if (route->source != NETWORK_CONFIG_SOURCE_DHCP4) |
| 257 |
continue; |
| 258 |
if (route->nexthop.ifindex != link->ifindex) |
| 259 |
continue; |
| 260 |
if (only_marked && !route_is_marked(route)) |
| 261 |
continue; |
| 262 |
|
| 263 |
RET_GATHER(ret, route_remove_and_cancel(route, link->manager)); |
| 264 |
} |
| 265 |
|
| 266 |
SET_FOREACH(address, link->addresses) { |
| 267 |
if (address->source != NETWORK_CONFIG_SOURCE_DHCP4) |
| 268 |
continue; |
| 269 |
if (only_marked && !address_is_marked(address)) |
| 270 |
continue; |
| 271 |
|
| 272 |
RET_GATHER(ret, address_remove_and_cancel(address, link)); |
| 273 |
} |
| 274 |
|
| 275 |
return ret; |
| 276 |
} |
| 277 |
|
| 278 |
static int dhcp4_address_get(Link *link, Address **ret) { |
| 279 |
Address *address; |
| 280 |
|
| 281 |
assert(link); |
| 282 |
|
| 283 |
SET_FOREACH(address, link->addresses) { |
| 284 |
if (address->source != NETWORK_CONFIG_SOURCE_DHCP4) |
| 285 |
continue; |
| 286 |
if (address_is_marked(address)) |
| 287 |
continue; |
| 288 |
|
| 289 |
if (ret) |
| 290 |
*ret = address; |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
return -ENOENT; |
| 295 |
} |
| 296 |
|
| 297 |
static int dhcp4_address_ready_callback(Address *address) { |
| 298 |
assert(address); |
| 299 |
assert(address->link); |
| 300 |
|
| 301 |
/* Do not call this again. */ |
| 302 |
address->callback = NULL; |
| 303 |
|
| 304 |
return dhcp4_check_ready(address->link); |
| 305 |
} |
| 306 |
|
| 307 |
int dhcp4_check_ready(Link *link) { |
| 308 |
Address *address; |
| 309 |
int r; |
| 310 |
|
| 311 |
assert(link); |
| 312 |
|
| 313 |
if (link->dhcp4_messages > 0) { |
| 314 |
log_link_debug(link, "%s(): DHCPv4 address and routes are not set.", __func__); |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
if (dhcp4_address_get(link, &address) < 0) { |
| 319 |
log_link_debug(link, "%s(): DHCPv4 address is not set.", __func__); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
if (!address_is_ready(address)) { |
| 324 |
log_link_debug(link, "%s(): DHCPv4 address is not ready.", __func__); |
| 325 |
address->callback = dhcp4_address_ready_callback; |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
link->dhcp4_configured = true; |
| 330 |
log_link_debug(link, "DHCPv4 address and routes set."); |
| 331 |
|
| 332 |
/* New address and routes are configured now. Let's release old lease. */ |
| 333 |
r = dhcp4_remove_address_and_routes(link, /* only_marked= */ true); |
| 334 |
if (r < 0) |
| 335 |
return r; |
| 336 |
|
| 337 |
r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_DHCP4); |
| 338 |
if (r < 0) |
| 339 |
return r; |
| 340 |
|
| 341 |
r = sd_ipv4ll_stop(link->ipv4ll); |
| 342 |
if (r < 0) |
| 343 |
return log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address: %m"); |
| 344 |
|
| 345 |
link_check_ready(link); |
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) { |
| 350 |
int r; |
| 351 |
|
| 352 |
assert(req); |
| 353 |
assert(link); |
| 354 |
assert(route); |
| 355 |
|
| 356 |
r = route_configure_handler_internal(m, req, route); |
| 357 |
if (r <= 0) |
| 358 |
return r; |
| 359 |
|
| 360 |
r = dhcp4_check_ready(link); |
| 361 |
if (r < 0) |
| 362 |
link_enter_failed(link); |
| 363 |
|
| 364 |
return 1; |
| 365 |
} |
| 366 |
|
| 367 |
static int dhcp4_request_route(Route *route, Link *link) { |
| 368 |
struct in_addr server; |
| 369 |
Route *existing; |
| 370 |
int r; |
| 371 |
|
| 372 |
assert(route); |
| 373 |
assert(link); |
| 374 |
assert(link->manager); |
| 375 |
assert(link->network); |
| 376 |
assert(link->dhcp_lease); |
| 377 |
|
| 378 |
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server); |
| 379 |
if (r < 0) |
| 380 |
return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); |
| 381 |
|
| 382 |
route->source = NETWORK_CONFIG_SOURCE_DHCP4; |
| 383 |
route->provider.in = server; |
| 384 |
route->family = AF_INET; |
| 385 |
if (!route->protocol_set) |
| 386 |
route->protocol = RTPROT_DHCP; |
| 387 |
if (!route->priority_set) |
| 388 |
route->priority = link->network->dhcp_route_metric; |
| 389 |
if (!route->table_set) |
| 390 |
route->table = link_get_dhcp4_route_table(link); |
| 391 |
r = route_metric_set(&route->metric, RTAX_MTU, link->network->dhcp_route_mtu); |
| 392 |
if (r < 0) |
| 393 |
return r; |
| 394 |
r = route_metric_set(&route->metric, RTAX_INITCWND, link->network->dhcp_initial_congestion_window); |
| 395 |
if (r < 0) |
| 396 |
return r; |
| 397 |
r = route_metric_set(&route->metric, RTAX_INITRWND, link->network->dhcp_advertised_receive_window); |
| 398 |
if (r < 0) |
| 399 |
return r; |
| 400 |
r = route_metric_set(&route->metric, RTAX_QUICKACK, link->network->dhcp_quickack); |
| 401 |
if (r < 0) |
| 402 |
return r; |
| 403 |
|
| 404 |
r = route_adjust_nexthops(route, link); |
| 405 |
if (r < 0) |
| 406 |
return r; |
| 407 |
|
| 408 |
if (route_get(link->manager, route, &existing) < 0) /* This is a new route. */ |
| 409 |
link->dhcp4_configured = false; |
| 410 |
else |
| 411 |
route_unmark(existing); |
| 412 |
|
| 413 |
return link_request_route(link, route, &link->dhcp4_messages, dhcp4_route_handler); |
| 414 |
} |
| 415 |
|
| 416 |
static bool prefixroute_by_kernel(Link *link) { |
| 417 |
return !link->network->dhcp_route_table_set || |
| 418 |
link->network->dhcp_route_table == RT_TABLE_MAIN; |
| 419 |
} |
| 420 |
|
| 421 |
static int dhcp4_request_prefix_route(Link *link, Route *rt) { |
| 422 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 423 |
int r; |
| 424 |
|
| 425 |
assert(link); |
| 426 |
assert(link->dhcp_lease); |
| 427 |
|
| 428 |
if (prefixroute_by_kernel(link) && (!rt || !rt->table_set || rt->table == RT_TABLE_MAIN)) |
| 429 |
/* The prefix route in the main table will be created by the kernel. See dhcp4_update_address(). */ |
| 430 |
return 0; |
| 431 |
|
| 432 |
r = route_new(&route); |
| 433 |
if (r < 0) |
| 434 |
return r; |
| 435 |
|
| 436 |
route->scope = RT_SCOPE_LINK; |
| 437 |
if (rt) { |
| 438 |
route->table_set = rt->table_set; |
| 439 |
route->table = rt->table; |
| 440 |
} |
| 441 |
|
| 442 |
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &route->dst.in, &route->dst_prefixlen); |
| 443 |
if (r < 0) |
| 444 |
return r; |
| 445 |
|
| 446 |
r = sd_dhcp_lease_get_address(link->dhcp_lease, &route->prefsrc.in); |
| 447 |
if (r < 0) |
| 448 |
return r; |
| 449 |
|
| 450 |
return dhcp4_request_route(route, link); |
| 451 |
} |
| 452 |
|
| 453 |
static int dhcp4_request_route_to_gateway(Link *link, const Route *rt) { |
| 454 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 455 |
struct in_addr address; |
| 456 |
int r; |
| 457 |
|
| 458 |
assert(link); |
| 459 |
assert(link->dhcp_lease); |
| 460 |
assert(rt); |
| 461 |
|
| 462 |
if (in_addr_is_set(rt->nexthop.family, &rt->nexthop.gw) <= 0) |
| 463 |
return 0; |
| 464 |
|
| 465 |
assert(rt->nexthop.family == AF_INET); |
| 466 |
|
| 467 |
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
| 468 |
if (r < 0) |
| 469 |
return r; |
| 470 |
|
| 471 |
r = route_new(&route); |
| 472 |
if (r < 0) |
| 473 |
return r; |
| 474 |
|
| 475 |
route->dst.in = rt->nexthop.gw.in; |
| 476 |
route->dst_prefixlen = 32; |
| 477 |
route->prefsrc.in = address; |
| 478 |
route->scope = RT_SCOPE_LINK; |
| 479 |
route->table = rt->table; |
| 480 |
route->table_set = rt->table_set; |
| 481 |
|
| 482 |
return dhcp4_request_route(route, link); |
| 483 |
} |
| 484 |
|
| 485 |
static int dhcp4_request_route_auto( |
| 486 |
Route *route, |
| 487 |
Link *link, |
| 488 |
const struct in_addr *gw) { |
| 489 |
|
| 490 |
struct in_addr address; |
| 491 |
int r; |
| 492 |
|
| 493 |
assert(route); |
| 494 |
assert(link); |
| 495 |
assert(link->dhcp_lease); |
| 496 |
assert(gw); |
| 497 |
|
| 498 |
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
| 499 |
if (r < 0) |
| 500 |
return r; |
| 501 |
|
| 502 |
if (in4_addr_is_localhost(&route->dst.in)) { |
| 503 |
if (in4_addr_is_set(gw)) |
| 504 |
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is localhost, " |
| 505 |
"ignoring gateway address "IPV4_ADDRESS_FMT_STR, |
| 506 |
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw)); |
| 507 |
|
| 508 |
route->scope = RT_SCOPE_HOST; |
| 509 |
route->nexthop.family = AF_UNSPEC; |
| 510 |
route->nexthop.gw = IN_ADDR_NULL; |
| 511 |
route->prefsrc = IN_ADDR_NULL; |
| 512 |
|
| 513 |
} else if (in4_addr_equal(&route->dst.in, &address)) { |
| 514 |
if (in4_addr_is_set(gw)) |
| 515 |
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is equivalent to the acquired address, " |
| 516 |
"ignoring gateway address "IPV4_ADDRESS_FMT_STR, |
| 517 |
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw)); |
| 518 |
|
| 519 |
route->scope = RT_SCOPE_HOST; |
| 520 |
route->nexthop.family = AF_UNSPEC; |
| 521 |
route->nexthop.gw = IN_ADDR_NULL; |
| 522 |
route->prefsrc.in = address; |
| 523 |
|
| 524 |
} else if (in4_addr_is_null(gw)) { |
| 525 |
r = dhcp4_prefix_covers(link, &route->dst.in, route->dst_prefixlen); |
| 526 |
if (r < 0) |
| 527 |
return r; |
| 528 |
if (r == 0 && DEBUG_LOGGING) { |
| 529 |
struct in_addr prefix; |
| 530 |
uint8_t prefixlen; |
| 531 |
|
| 532 |
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen); |
| 533 |
if (r < 0) |
| 534 |
return r; |
| 535 |
|
| 536 |
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is not in the assigned network " |
| 537 |
IPV4_ADDRESS_FMT_STR"/%u, but no gateway is specified, using 'link' scope.", |
| 538 |
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, |
| 539 |
IPV4_ADDRESS_FMT_VAL(prefix), prefixlen); |
| 540 |
} |
| 541 |
|
| 542 |
route->scope = RT_SCOPE_LINK; |
| 543 |
route->nexthop.family = AF_UNSPEC; |
| 544 |
route->nexthop.gw = IN_ADDR_NULL; |
| 545 |
route->prefsrc.in = address; |
| 546 |
|
| 547 |
} else { |
| 548 |
route->scope = RT_SCOPE_UNIVERSE; |
| 549 |
route->nexthop.family = AF_INET; |
| 550 |
route->nexthop.gw.in = *gw; |
| 551 |
route->prefsrc.in = address; |
| 552 |
|
| 553 |
r = dhcp4_request_route_to_gateway(link, route); |
| 554 |
if (r < 0) |
| 555 |
return r; |
| 556 |
} |
| 557 |
|
| 558 |
return dhcp4_request_route(route, link); |
| 559 |
} |
| 560 |
|
| 561 |
static int dhcp4_request_classless_static_or_static_routes(Link *link) { |
| 562 |
_cleanup_free_ sd_dhcp_route **routes = NULL; |
| 563 |
size_t n_routes; |
| 564 |
int r; |
| 565 |
|
| 566 |
assert(link); |
| 567 |
assert(link->dhcp_lease); |
| 568 |
|
| 569 |
if (!link->network->dhcp_use_routes) |
| 570 |
return 0; |
| 571 |
|
| 572 |
r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes); |
| 573 |
if (r == -ENODATA) |
| 574 |
return 0; |
| 575 |
if (r < 0) |
| 576 |
return r; |
| 577 |
|
| 578 |
FOREACH_ARRAY(e, routes, n_routes) { |
| 579 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 580 |
struct in_addr gw; |
| 581 |
|
| 582 |
r = route_new(&route); |
| 583 |
if (r < 0) |
| 584 |
return r; |
| 585 |
|
| 586 |
r = sd_dhcp_route_get_gateway(*e, &gw); |
| 587 |
if (r < 0) |
| 588 |
return r; |
| 589 |
|
| 590 |
r = sd_dhcp_route_get_destination(*e, &route->dst.in); |
| 591 |
if (r < 0) |
| 592 |
return r; |
| 593 |
|
| 594 |
r = sd_dhcp_route_get_destination_prefix_length(*e, &route->dst_prefixlen); |
| 595 |
if (r < 0) |
| 596 |
return r; |
| 597 |
|
| 598 |
r = dhcp4_request_route_auto(route, link, &gw); |
| 599 |
if (r < 0) |
| 600 |
return r; |
| 601 |
} |
| 602 |
|
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
static int dhcp4_request_default_gateway(Link *link) { |
| 607 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 608 |
struct in_addr address, router; |
| 609 |
int r; |
| 610 |
|
| 611 |
assert(link); |
| 612 |
assert(link->dhcp_lease); |
| 613 |
|
| 614 |
if (!link->network->dhcp_use_gateway) |
| 615 |
return 0; |
| 616 |
|
| 617 |
/* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and |
| 618 |
* a Router option, the DHCP client MUST ignore the Router option. */ |
| 619 |
if (link->network->dhcp_use_routes && |
| 620 |
dhcp4_get_classless_static_or_static_routes(link, NULL, NULL) > 0) |
| 621 |
return 0; |
| 622 |
|
| 623 |
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
| 624 |
if (r < 0) |
| 625 |
return r; |
| 626 |
|
| 627 |
r = dhcp4_get_router(link, &router); |
| 628 |
if (r == -ENODATA) { |
| 629 |
log_link_debug(link, "DHCP: No valid router address received from DHCP server."); |
| 630 |
return 0; |
| 631 |
} |
| 632 |
if (r < 0) |
| 633 |
return r; |
| 634 |
|
| 635 |
r = route_new(&route); |
| 636 |
if (r < 0) |
| 637 |
return r; |
| 638 |
|
| 639 |
/* Next, add a default gateway. */ |
| 640 |
route->nexthop.family = AF_INET; |
| 641 |
route->nexthop.gw.in = router; |
| 642 |
route->prefsrc.in = address; |
| 643 |
|
| 644 |
/* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host |
| 645 |
* so that we can route no matter the netmask or existing kernel route tables. */ |
| 646 |
r = dhcp4_request_route_to_gateway(link, route); |
| 647 |
if (r < 0) |
| 648 |
return r; |
| 649 |
|
| 650 |
return dhcp4_request_route(route, link); |
| 651 |
} |
| 652 |
|
| 653 |
static int dhcp4_request_semi_static_routes(Link *link) { |
| 654 |
Route *rt; |
| 655 |
int r; |
| 656 |
|
| 657 |
assert(link); |
| 658 |
assert(link->dhcp_lease); |
| 659 |
assert(link->network); |
| 660 |
|
| 661 |
HASHMAP_FOREACH(rt, link->network->routes_by_section) { |
| 662 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 663 |
struct in_addr gw; |
| 664 |
|
| 665 |
if (rt->source != NETWORK_CONFIG_SOURCE_DHCP4) |
| 666 |
continue; |
| 667 |
|
| 668 |
assert(rt->family == AF_INET); |
| 669 |
assert(rt->nexthop.family == AF_INET); |
| 670 |
|
| 671 |
r = dhcp4_find_gateway_for_destination(link, &rt->dst.in, rt->dst_prefixlen, &gw); |
| 672 |
if (r == -EHOSTUNREACH) { |
| 673 |
log_link_debug_errno(link, r, "DHCP: Cannot find suitable gateway for destination %s of semi-static route, ignoring: %m", |
| 674 |
IN4_ADDR_PREFIX_TO_STRING(&rt->dst.in, rt->dst_prefixlen)); |
| 675 |
continue; |
| 676 |
} |
| 677 |
if (r < 0) |
| 678 |
return r; |
| 679 |
|
| 680 |
if (in4_addr_is_null(&gw)) { |
| 681 |
log_link_debug(link, "DHCP: Destination %s of semi-static route is in the acquired network, skipping configuration.", |
| 682 |
IN4_ADDR_PREFIX_TO_STRING(&rt->dst.in, rt->dst_prefixlen)); |
| 683 |
continue; |
| 684 |
} |
| 685 |
|
| 686 |
r = route_dup(rt, NULL, &route); |
| 687 |
if (r < 0) |
| 688 |
return r; |
| 689 |
|
| 690 |
route->nexthop.gw.in = gw; |
| 691 |
|
| 692 |
if (!route->prefsrc_set) { |
| 693 |
r = sd_dhcp_lease_get_address(link->dhcp_lease, &route->prefsrc.in); |
| 694 |
if (r < 0) |
| 695 |
return r; |
| 696 |
} |
| 697 |
|
| 698 |
r = dhcp4_request_prefix_route(link, route); |
| 699 |
if (r < 0) |
| 700 |
return r; |
| 701 |
|
| 702 |
r = dhcp4_request_route_to_gateway(link, route); |
| 703 |
if (r < 0) |
| 704 |
return r; |
| 705 |
|
| 706 |
r = dhcp4_request_route(route, link); |
| 707 |
if (r < 0) |
| 708 |
return r; |
| 709 |
} |
| 710 |
|
| 711 |
return 0; |
| 712 |
} |
| 713 |
|
| 714 |
static int dhcp4_request_routes_to_servers( |
| 715 |
Link *link, |
| 716 |
const struct in_addr *servers, |
| 717 |
size_t n_servers) { |
| 718 |
|
| 719 |
int r; |
| 720 |
|
| 721 |
assert(link); |
| 722 |
assert(link->dhcp_lease); |
| 723 |
assert(link->network); |
| 724 |
assert(servers || n_servers == 0); |
| 725 |
|
| 726 |
FOREACH_ARRAY(dst, servers, n_servers) { |
| 727 |
_cleanup_(route_unrefp) Route *route = NULL; |
| 728 |
struct in_addr gw; |
| 729 |
|
| 730 |
if (in4_addr_is_null(dst)) |
| 731 |
continue; |
| 732 |
|
| 733 |
r = dhcp4_find_gateway_for_destination(link, dst, 32, &gw); |
| 734 |
if (r == -EHOSTUNREACH) { |
| 735 |
log_link_debug_errno(link, r, "DHCP: Cannot find suitable gateway for destination %s, ignoring: %m", |
| 736 |
IN4_ADDR_PREFIX_TO_STRING(dst, 32)); |
| 737 |
continue; |
| 738 |
} |
| 739 |
if (r < 0) |
| 740 |
return r; |
| 741 |
|
| 742 |
r = route_new(&route); |
| 743 |
if (r < 0) |
| 744 |
return r; |
| 745 |
|
| 746 |
route->dst.in = *dst; |
| 747 |
route->dst_prefixlen = 32; |
| 748 |
|
| 749 |
r = dhcp4_request_route_auto(route, link, &gw); |
| 750 |
if (r < 0) |
| 751 |
return r; |
| 752 |
} |
| 753 |
|
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
static int dhcp4_request_routes_to_dns(Link *link) { |
| 758 |
const struct in_addr *dns; |
| 759 |
int r; |
| 760 |
|
| 761 |
assert(link); |
| 762 |
assert(link->dhcp_lease); |
| 763 |
assert(link->network); |
| 764 |
|
| 765 |
if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4) || |
| 766 |
!link->network->dhcp_routes_to_dns) |
| 767 |
return 0; |
| 768 |
|
| 769 |
r = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns); |
| 770 |
if (IN_SET(r, 0, -ENODATA)) |
| 771 |
return 0; |
| 772 |
if (r < 0) |
| 773 |
return r; |
| 774 |
|
| 775 |
return dhcp4_request_routes_to_servers(link, dns, r); |
| 776 |
} |
| 777 |
|
| 778 |
static int dhcp4_request_routes_to_ntp(Link *link) { |
| 779 |
const struct in_addr *ntp; |
| 780 |
int r; |
| 781 |
|
| 782 |
assert(link); |
| 783 |
assert(link->dhcp_lease); |
| 784 |
assert(link->network); |
| 785 |
|
| 786 |
if (!link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4) || |
| 787 |
!link->network->dhcp_routes_to_ntp) |
| 788 |
return 0; |
| 789 |
|
| 790 |
r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp); |
| 791 |
if (IN_SET(r, 0, -ENODATA)) |
| 792 |
return 0; |
| 793 |
if (r < 0) |
| 794 |
return r; |
| 795 |
|
| 796 |
return dhcp4_request_routes_to_servers(link, ntp, r); |
| 797 |
} |
| 798 |
|
| 799 |
static int dhcp4_request_routes(Link *link) { |
| 800 |
int r; |
| 801 |
|
| 802 |
assert(link); |
| 803 |
assert(link->dhcp_lease); |
| 804 |
|
| 805 |
r = dhcp4_request_prefix_route(link, /* rt= */ NULL); |
| 806 |
if (r < 0) |
| 807 |
return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m"); |
| 808 |
|
| 809 |
r = dhcp4_request_default_gateway(link); |
| 810 |
if (r < 0) |
| 811 |
return log_link_error_errno(link, r, "DHCP error: Could not request default gateway: %m"); |
| 812 |
|
| 813 |
r = dhcp4_request_classless_static_or_static_routes(link); |
| 814 |
if (r < 0) |
| 815 |
return log_link_error_errno(link, r, "DHCP error: Could not request static routes: %m"); |
| 816 |
|
| 817 |
r = dhcp4_request_semi_static_routes(link); |
| 818 |
if (r < 0) |
| 819 |
return log_link_error_errno(link, r, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m"); |
| 820 |
|
| 821 |
r = dhcp4_request_routes_to_dns(link); |
| 822 |
if (r < 0) |
| 823 |
return log_link_error_errno(link, r, "DHCP error: Could not request routes to DNS servers: %m"); |
| 824 |
|
| 825 |
r = dhcp4_request_routes_to_ntp(link); |
| 826 |
if (r < 0) |
| 827 |
return log_link_error_errno(link, r, "DHCP error: Could not request routes to NTP servers: %m"); |
| 828 |
|
| 829 |
return 0; |
| 830 |
} |
| 831 |
|
| 832 |
static int dhcp_reset_mtu(Link *link) { |
| 833 |
int r; |
| 834 |
|
| 835 |
assert(link); |
| 836 |
|
| 837 |
if (!link->network->dhcp_use_mtu) |
| 838 |
return 0; |
| 839 |
|
| 840 |
r = link_request_to_set_mtu(link, link->original_mtu); |
| 841 |
if (r < 0) |
| 842 |
return log_link_error_errno(link, r, "DHCP error: Could not queue request to reset MTU: %m"); |
| 843 |
|
| 844 |
return 0; |
| 845 |
} |
| 846 |
|
| 847 |
static int dhcp_reset_hostname(Link *link) { |
| 848 |
const char *hostname; |
| 849 |
int r; |
| 850 |
|
| 851 |
assert(link); |
| 852 |
|
| 853 |
if (!link->network->dhcp_use_hostname) |
| 854 |
return 0; |
| 855 |
|
| 856 |
hostname = link->network->dhcp_hostname; |
| 857 |
if (!hostname) |
| 858 |
(void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); |
| 859 |
|
| 860 |
if (!hostname) |
| 861 |
return 0; |
| 862 |
|
| 863 |
/* If a hostname was set due to the lease, then unset it now. */ |
| 864 |
r = manager_set_hostname(link->manager, NULL); |
| 865 |
if (r < 0) |
| 866 |
return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m"); |
| 867 |
|
| 868 |
return 0; |
| 869 |
} |
| 870 |
|
| 871 |
int dhcp4_lease_lost(Link *link) { |
| 872 |
int r = 0; |
| 873 |
|
| 874 |
assert(link); |
| 875 |
assert(link->dhcp_lease); |
| 876 |
assert(link->network); |
| 877 |
|
| 878 |
log_link_info(link, "DHCP lease lost"); |
| 879 |
|
| 880 |
link->dhcp4_configured = false; |
| 881 |
|
| 882 |
if (link->network->dhcp_use_6rd && |
| 883 |
sd_dhcp_lease_has_6rd(link->dhcp_lease)) |
| 884 |
dhcp4_pd_prefix_lost(link); |
| 885 |
|
| 886 |
RET_GATHER(r, dhcp4_remove_address_and_routes(link, /* only_marked= */ false)); |
| 887 |
RET_GATHER(r, dhcp_reset_mtu(link)); |
| 888 |
RET_GATHER(r, dhcp_reset_hostname(link)); |
| 889 |
|
| 890 |
link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); |
| 891 |
link_dirty(link); |
| 892 |
|
| 893 |
/* If one of the above failed. Do not request nexthops and routes. */ |
| 894 |
if (r < 0) |
| 895 |
return r; |
| 896 |
|
| 897 |
r = link_request_static_nexthops(link, true); |
| 898 |
if (r < 0) |
| 899 |
return r; |
| 900 |
|
| 901 |
return link_request_static_routes(link, true); |
| 902 |
} |
| 903 |
|
| 904 |
static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) { |
| 905 |
int r; |
| 906 |
|
| 907 |
assert(link); |
| 908 |
assert(address); |
| 909 |
|
| 910 |
r = address_configure_handler_internal(m, link, address); |
| 911 |
if (r <= 0) |
| 912 |
return r; |
| 913 |
|
| 914 |
r = dhcp4_check_ready(link); |
| 915 |
if (r < 0) |
| 916 |
link_enter_failed(link); |
| 917 |
|
| 918 |
return 1; |
| 919 |
} |
| 920 |
|
| 921 |
static int dhcp4_request_address(Link *link, bool announce) { |
| 922 |
_cleanup_(address_unrefp) Address *addr = NULL; |
| 923 |
struct in_addr address, server; |
| 924 |
uint8_t prefixlen; |
| 925 |
Address *existing; |
| 926 |
usec_t lifetime_usec; |
| 927 |
int r; |
| 928 |
|
| 929 |
assert(link); |
| 930 |
assert(link->manager); |
| 931 |
assert(link->network); |
| 932 |
assert(link->dhcp_lease); |
| 933 |
|
| 934 |
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
| 935 |
if (r < 0) |
| 936 |
return log_link_warning_errno(link, r, "DHCP error: no address: %m"); |
| 937 |
|
| 938 |
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &prefixlen); |
| 939 |
if (r < 0) |
| 940 |
return log_link_warning_errno(link, r, "DHCP error: no netmask: %m"); |
| 941 |
|
| 942 |
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server); |
| 943 |
if (r < 0) |
| 944 |
return log_link_debug_errno(link, r, "DHCP error: failed to get DHCP server IP address: %m"); |
| 945 |
|
| 946 |
if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { |
| 947 |
r = sd_dhcp_lease_get_lifetime_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &lifetime_usec); |
| 948 |
if (r < 0) |
| 949 |
return log_link_warning_errno(link, r, "DHCP error: failed to get lifetime: %m"); |
| 950 |
} else |
| 951 |
lifetime_usec = USEC_INFINITY; |
| 952 |
|
| 953 |
if (announce) { |
| 954 |
const struct in_addr *router; |
| 955 |
|
| 956 |
r = sd_dhcp_lease_get_router(link->dhcp_lease, &router); |
| 957 |
if (r < 0 && r != -ENODATA) |
| 958 |
return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); |
| 959 |
|
| 960 |
if (r > 0 && in4_addr_is_set(&router[0])) |
| 961 |
log_struct(LOG_INFO, |
| 962 |
LOG_LINK_INTERFACE(link), |
| 963 |
LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u, gateway "IPV4_ADDRESS_FMT_STR" acquired from "IPV4_ADDRESS_FMT_STR, |
| 964 |
IPV4_ADDRESS_FMT_VAL(address), |
| 965 |
prefixlen, |
| 966 |
IPV4_ADDRESS_FMT_VAL(router[0]), |
| 967 |
IPV4_ADDRESS_FMT_VAL(server)), |
| 968 |
LOG_ITEM("ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address)), |
| 969 |
LOG_ITEM("PREFIXLEN=%u", prefixlen), |
| 970 |
LOG_ITEM("GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0]))); |
| 971 |
else |
| 972 |
log_struct(LOG_INFO, |
| 973 |
LOG_LINK_INTERFACE(link), |
| 974 |
LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u acquired from "IPV4_ADDRESS_FMT_STR, |
| 975 |
IPV4_ADDRESS_FMT_VAL(address), |
| 976 |
prefixlen, |
| 977 |
IPV4_ADDRESS_FMT_VAL(server)), |
| 978 |
LOG_ITEM("ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address)), |
| 979 |
LOG_ITEM("PREFIXLEN=%u", prefixlen)); |
| 980 |
} |
| 981 |
|
| 982 |
r = address_new(&addr); |
| 983 |
if (r < 0) |
| 984 |
return log_oom(); |
| 985 |
|
| 986 |
addr->source = NETWORK_CONFIG_SOURCE_DHCP4; |
| 987 |
addr->provider.in = server; |
| 988 |
addr->family = AF_INET; |
| 989 |
addr->in_addr.in.s_addr = address.s_addr; |
| 990 |
addr->lifetime_preferred_usec = lifetime_usec; |
| 991 |
addr->lifetime_valid_usec = lifetime_usec; |
| 992 |
addr->prefixlen = prefixlen; |
| 993 |
r = sd_dhcp_lease_get_broadcast(link->dhcp_lease, &addr->broadcast); |
| 994 |
if (r < 0 && r != -ENODATA) |
| 995 |
return log_link_warning_errno(link, r, "DHCP: failed to get broadcast address: %m"); |
| 996 |
SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !prefixroute_by_kernel(link)); |
| 997 |
addr->route_metric = link->network->dhcp_route_metric; |
| 998 |
addr->duplicate_address_detection = link->network->dhcp_send_decline ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_NO; |
| 999 |
|
| 1000 |
r = free_and_strdup_warn(&addr->label, link->network->dhcp_label); |
| 1001 |
if (r < 0) |
| 1002 |
return r; |
| 1003 |
|
| 1004 |
r = free_and_strdup_warn(&addr->netlabel, link->network->dhcp_netlabel); |
| 1005 |
if (r < 0) |
| 1006 |
return r; |
| 1007 |
|
| 1008 |
if (address_get(link, addr, &existing) < 0) /* The address is new. */ |
| 1009 |
link->dhcp4_configured = false; |
| 1010 |
else |
| 1011 |
address_unmark(existing); |
| 1012 |
|
| 1013 |
r = link_request_address(link, addr, &link->dhcp4_messages, |
| 1014 |
dhcp4_address_handler, NULL); |
| 1015 |
if (r < 0) |
| 1016 |
return log_link_error_errno(link, r, "Failed to request DHCPv4 address: %m"); |
| 1017 |
|
| 1018 |
return 0; |
| 1019 |
} |
| 1020 |
|
| 1021 |
static int dhcp4_request_address_and_routes(Link *link, bool announce) { |
| 1022 |
int r; |
| 1023 |
|
| 1024 |
assert(link); |
| 1025 |
|
| 1026 |
link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP4); |
| 1027 |
manager_mark_routes(link->manager, link, NETWORK_CONFIG_SOURCE_DHCP4); |
| 1028 |
|
| 1029 |
r = dhcp4_request_address(link, announce); |
| 1030 |
if (r < 0) |
| 1031 |
return r; |
| 1032 |
|
| 1033 |
r = dhcp4_request_routes(link); |
| 1034 |
if (r < 0) |
| 1035 |
return r; |
| 1036 |
|
| 1037 |
if (!link->dhcp4_configured) { |
| 1038 |
link_set_state(link, LINK_STATE_CONFIGURING); |
| 1039 |
link_check_ready(link); |
| 1040 |
} |
| 1041 |
|
| 1042 |
return 0; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { |
| 1046 |
_cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *old_lease = NULL; |
| 1047 |
sd_dhcp_lease *lease; |
| 1048 |
int r; |
| 1049 |
|
| 1050 |
assert(link); |
| 1051 |
assert(link->network); |
| 1052 |
assert(client); |
| 1053 |
|
| 1054 |
r = sd_dhcp_client_get_lease(client, &lease); |
| 1055 |
if (r < 0) |
| 1056 |
return log_link_warning_errno(link, r, "DHCP error: no lease: %m"); |
| 1057 |
|
| 1058 |
old_lease = TAKE_PTR(link->dhcp_lease); |
| 1059 |
link->dhcp_lease = sd_dhcp_lease_ref(lease); |
| 1060 |
link_dirty(link); |
| 1061 |
|
| 1062 |
if (link->network->dhcp_use_6rd) { |
| 1063 |
if (sd_dhcp_lease_has_6rd(link->dhcp_lease)) { |
| 1064 |
r = dhcp4_pd_prefix_acquired(link); |
| 1065 |
if (r < 0) |
| 1066 |
return log_link_warning_errno(link, r, "Failed to process 6rd option: %m"); |
| 1067 |
} else if (sd_dhcp_lease_has_6rd(old_lease)) |
| 1068 |
dhcp4_pd_prefix_lost(link); |
| 1069 |
} |
| 1070 |
|
| 1071 |
return dhcp4_request_address_and_routes(link, false); |
| 1072 |
} |
| 1073 |
|
| 1074 |
static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { |
| 1075 |
sd_dhcp_lease *lease; |
| 1076 |
int r; |
| 1077 |
|
| 1078 |
assert(client); |
| 1079 |
assert(link); |
| 1080 |
|
| 1081 |
r = sd_dhcp_client_get_lease(client, &lease); |
| 1082 |
if (r < 0) |
| 1083 |
return log_link_error_errno(link, r, "DHCP error: No lease: %m"); |
| 1084 |
|
| 1085 |
sd_dhcp_lease_unref(link->dhcp_lease); |
| 1086 |
link->dhcp_lease = sd_dhcp_lease_ref(lease); |
| 1087 |
link_dirty(link); |
| 1088 |
|
| 1089 |
if (link->network->dhcp_use_mtu) { |
| 1090 |
uint16_t mtu; |
| 1091 |
|
| 1092 |
r = sd_dhcp_lease_get_mtu(lease, &mtu); |
| 1093 |
if (r >= 0) { |
| 1094 |
r = link_request_to_set_mtu(link, mtu); |
| 1095 |
if (r < 0) |
| 1096 |
log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
if (link->network->dhcp_use_hostname) { |
| 1101 |
const char *dhcpname = NULL; |
| 1102 |
_cleanup_free_ char *hostname = NULL; |
| 1103 |
|
| 1104 |
if (link->network->dhcp_hostname) |
| 1105 |
dhcpname = link->network->dhcp_hostname; |
| 1106 |
else |
| 1107 |
(void) sd_dhcp_lease_get_hostname(lease, &dhcpname); |
| 1108 |
|
| 1109 |
if (dhcpname) { |
| 1110 |
r = shorten_overlong(dhcpname, &hostname); |
| 1111 |
if (r < 0) |
| 1112 |
log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname); |
| 1113 |
if (r == 1) |
| 1114 |
log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname); |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (hostname) { |
| 1118 |
r = manager_set_hostname(link->manager, hostname); |
| 1119 |
if (r < 0) |
| 1120 |
log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
if (link->network->dhcp_use_timezone) { |
| 1125 |
const char *tz = NULL; |
| 1126 |
|
| 1127 |
(void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
| 1128 |
|
| 1129 |
if (tz) { |
| 1130 |
r = manager_set_timezone(link->manager, tz); |
| 1131 |
if (r < 0) |
| 1132 |
log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
if (link->network->dhcp_use_6rd && |
| 1137 |
sd_dhcp_lease_has_6rd(link->dhcp_lease)) { |
| 1138 |
r = dhcp4_pd_prefix_acquired(link); |
| 1139 |
if (r < 0) |
| 1140 |
return log_link_warning_errno(link, r, "Failed to process 6rd option: %m"); |
| 1141 |
} |
| 1142 |
|
| 1143 |
return dhcp4_request_address_and_routes(link, true); |
| 1144 |
} |
| 1145 |
|
| 1146 |
static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) { |
| 1147 |
int r; |
| 1148 |
|
| 1149 |
r = dhcp_lease_acquired(client, link); |
| 1150 |
if (r < 0) |
| 1151 |
(void) dhcp4_lease_lost(link); |
| 1152 |
|
| 1153 |
return r; |
| 1154 |
} |
| 1155 |
|
| 1156 |
static int dhcp_server_is_filtered(Link *link, sd_dhcp_client *client) { |
| 1157 |
sd_dhcp_lease *lease; |
| 1158 |
struct in_addr addr; |
| 1159 |
int r; |
| 1160 |
|
| 1161 |
assert(link); |
| 1162 |
assert(link->network); |
| 1163 |
assert(client); |
| 1164 |
|
| 1165 |
r = sd_dhcp_client_get_lease(client, &lease); |
| 1166 |
if (r < 0) |
| 1167 |
return log_link_error_errno(link, r, "Failed to get DHCP lease: %m"); |
| 1168 |
|
| 1169 |
r = sd_dhcp_lease_get_server_identifier(lease, &addr); |
| 1170 |
if (r < 0) |
| 1171 |
return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); |
| 1172 |
|
| 1173 |
if (in4_address_is_filtered(&addr, link->network->dhcp_allow_listed_ip, link->network->dhcp_deny_listed_ip)) { |
| 1174 |
if (link->network->dhcp_allow_listed_ip) |
| 1175 |
log_link_debug(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" not found in allow-list, ignoring offer.", |
| 1176 |
IPV4_ADDRESS_FMT_VAL(addr)); |
| 1177 |
else |
| 1178 |
log_link_debug(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in deny-list, ignoring offer.", |
| 1179 |
IPV4_ADDRESS_FMT_VAL(addr)); |
| 1180 |
|
| 1181 |
return true; |
| 1182 |
} |
| 1183 |
|
| 1184 |
return false; |
| 1185 |
} |
| 1186 |
|
| 1187 |
static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 1188 |
Link *link = ASSERT_PTR(userdata); |
| 1189 |
int r; |
| 1190 |
|
| 1191 |
assert(link->network); |
| 1192 |
assert(link->manager); |
| 1193 |
|
| 1194 |
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
| 1195 |
return 0; |
| 1196 |
|
| 1197 |
switch (event) { |
| 1198 |
case SD_DHCP_CLIENT_EVENT_STOP: |
| 1199 |
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { |
| 1200 |
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
| 1201 |
return 0; |
| 1202 |
} |
| 1203 |
|
| 1204 |
r = ipv4ll_start(link); |
| 1205 |
if (r < 0) |
| 1206 |
return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); |
| 1207 |
if (r > 0) |
| 1208 |
log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address."); |
| 1209 |
|
| 1210 |
if (link->dhcp_lease) { |
| 1211 |
r = dhcp4_lease_lost(link); |
| 1212 |
if (r < 0) { |
| 1213 |
link_enter_failed(link); |
| 1214 |
return r; |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
break; |
| 1219 |
case SD_DHCP_CLIENT_EVENT_EXPIRED: |
| 1220 |
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { |
| 1221 |
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
| 1222 |
return 0; |
| 1223 |
} |
| 1224 |
|
| 1225 |
if (link->dhcp_lease) { |
| 1226 |
r = dhcp4_lease_lost(link); |
| 1227 |
if (r < 0) { |
| 1228 |
link_enter_failed(link); |
| 1229 |
return r; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
break; |
| 1234 |
case SD_DHCP_CLIENT_EVENT_IP_CHANGE: |
| 1235 |
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { |
| 1236 |
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
| 1237 |
return 0; |
| 1238 |
} |
| 1239 |
|
| 1240 |
r = dhcp_lease_ip_change(client, link); |
| 1241 |
if (r < 0) { |
| 1242 |
link_enter_failed(link); |
| 1243 |
return r; |
| 1244 |
} |
| 1245 |
|
| 1246 |
break; |
| 1247 |
case SD_DHCP_CLIENT_EVENT_RENEW: |
| 1248 |
r = dhcp_lease_renew(client, link); |
| 1249 |
if (r < 0) { |
| 1250 |
link_enter_failed(link); |
| 1251 |
return r; |
| 1252 |
} |
| 1253 |
break; |
| 1254 |
case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE: |
| 1255 |
r = dhcp_lease_acquired(client, link); |
| 1256 |
if (r < 0) { |
| 1257 |
link_enter_failed(link); |
| 1258 |
return r; |
| 1259 |
} |
| 1260 |
break; |
| 1261 |
case SD_DHCP_CLIENT_EVENT_SELECTING: |
| 1262 |
r = dhcp_server_is_filtered(link, client); |
| 1263 |
if (r < 0) { |
| 1264 |
link_enter_failed(link); |
| 1265 |
return r; |
| 1266 |
} |
| 1267 |
if (r > 0) |
| 1268 |
return -ENOMSG; |
| 1269 |
break; |
| 1270 |
|
| 1271 |
case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE: |
| 1272 |
r = ipv4ll_start(link); |
| 1273 |
if (r < 0) |
| 1274 |
return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); |
| 1275 |
if (r > 0) |
| 1276 |
log_link_debug(link, "Problems acquiring DHCP lease, acquiring IPv4 link-local address"); |
| 1277 |
break; |
| 1278 |
|
| 1279 |
default: |
| 1280 |
if (event < 0) |
| 1281 |
log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); |
| 1282 |
else |
| 1283 |
log_link_warning(link, "DHCP unknown event: %i", event); |
| 1284 |
} |
| 1285 |
|
| 1286 |
return 0; |
| 1287 |
} |
| 1288 |
|
| 1289 |
static int dhcp4_set_hostname(Link *link) { |
| 1290 |
_cleanup_free_ char *hostname = NULL; |
| 1291 |
const char *hn; |
| 1292 |
int r; |
| 1293 |
|
| 1294 |
assert(link); |
| 1295 |
|
| 1296 |
if (!link->network->dhcp_send_hostname) |
| 1297 |
hn = NULL; |
| 1298 |
else if (link->network->dhcp_hostname) |
| 1299 |
hn = link->network->dhcp_hostname; |
| 1300 |
else { |
| 1301 |
r = gethostname_strict(&hostname); |
| 1302 |
if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */ |
| 1303 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to get hostname: %m"); |
| 1304 |
|
| 1305 |
hn = hostname; |
| 1306 |
} |
| 1307 |
|
| 1308 |
r = sd_dhcp_client_set_hostname(link->dhcp_client, hn); |
| 1309 |
if (r == -EINVAL && hostname) |
| 1310 |
/* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */ |
| 1311 |
log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m"); |
| 1312 |
else if (r < 0) |
| 1313 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname: %m"); |
| 1314 |
|
| 1315 |
return 0; |
| 1316 |
} |
| 1317 |
|
| 1318 |
static int dhcp4_set_client_identifier(Link *link) { |
| 1319 |
int r; |
| 1320 |
|
| 1321 |
assert(link); |
| 1322 |
assert(link->network); |
| 1323 |
assert(link->dhcp_client); |
| 1324 |
|
| 1325 |
switch (link->network->dhcp_client_identifier) { |
| 1326 |
case DHCP_CLIENT_ID_DUID: { |
| 1327 |
/* If configured, apply user specified DUID and IAID */ |
| 1328 |
const DUID *duid = link_get_dhcp4_duid(link); |
| 1329 |
|
| 1330 |
if (duid->raw_data_len == 0) |
| 1331 |
switch (duid->type) { |
| 1332 |
case DUID_TYPE_LLT: |
| 1333 |
r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client, |
| 1334 |
link->network->dhcp_iaid_set, |
| 1335 |
link->network->dhcp_iaid, |
| 1336 |
duid->llt_time); |
| 1337 |
break; |
| 1338 |
case DUID_TYPE_LL: |
| 1339 |
r = sd_dhcp_client_set_iaid_duid_ll(link->dhcp_client, |
| 1340 |
link->network->dhcp_iaid_set, |
| 1341 |
link->network->dhcp_iaid); |
| 1342 |
break; |
| 1343 |
case DUID_TYPE_EN: |
| 1344 |
r = sd_dhcp_client_set_iaid_duid_en(link->dhcp_client, |
| 1345 |
link->network->dhcp_iaid_set, |
| 1346 |
link->network->dhcp_iaid); |
| 1347 |
break; |
| 1348 |
case DUID_TYPE_UUID: |
| 1349 |
r = sd_dhcp_client_set_iaid_duid_uuid(link->dhcp_client, |
| 1350 |
link->network->dhcp_iaid_set, |
| 1351 |
link->network->dhcp_iaid); |
| 1352 |
break; |
| 1353 |
default: |
| 1354 |
r = sd_dhcp_client_set_iaid_duid_raw(link->dhcp_client, |
| 1355 |
link->network->dhcp_iaid_set, |
| 1356 |
link->network->dhcp_iaid, |
| 1357 |
duid->type, NULL, 0); |
| 1358 |
} |
| 1359 |
else |
| 1360 |
r = sd_dhcp_client_set_iaid_duid_raw(link->dhcp_client, |
| 1361 |
link->network->dhcp_iaid_set, |
| 1362 |
link->network->dhcp_iaid, |
| 1363 |
duid->type, duid->raw_data, duid->raw_data_len); |
| 1364 |
if (r < 0) |
| 1365 |
return r; |
| 1366 |
break; |
| 1367 |
} |
| 1368 |
case DHCP_CLIENT_ID_MAC: { |
| 1369 |
const uint8_t *hw_addr = link->hw_addr.bytes; |
| 1370 |
size_t hw_addr_len = link->hw_addr.length; |
| 1371 |
|
| 1372 |
if (link->iftype == ARPHRD_INFINIBAND && hw_addr_len == INFINIBAND_ALEN) { |
| 1373 |
/* set_client_id expects only last 8 bytes of an IB address */ |
| 1374 |
hw_addr += INFINIBAND_ALEN - 8; |
| 1375 |
hw_addr_len -= INFINIBAND_ALEN - 8; |
| 1376 |
} |
| 1377 |
|
| 1378 |
r = sd_dhcp_client_set_client_id(link->dhcp_client, |
| 1379 |
link->iftype, |
| 1380 |
hw_addr, |
| 1381 |
hw_addr_len); |
| 1382 |
if (r < 0) |
| 1383 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set client ID: %m"); |
| 1384 |
break; |
| 1385 |
} |
| 1386 |
default: |
| 1387 |
assert_not_reached(); |
| 1388 |
} |
| 1389 |
|
| 1390 |
return 0; |
| 1391 |
} |
| 1392 |
|
| 1393 |
static int dhcp4_set_request_address(Link *link) { |
| 1394 |
assert(link); |
| 1395 |
assert(link->network); |
| 1396 |
assert(link->dhcp_client); |
| 1397 |
|
| 1398 |
/* 1. Use already assigned address. */ |
| 1399 |
Address *a; |
| 1400 |
SET_FOREACH(a, link->addresses) { |
| 1401 |
if (a->source != NETWORK_CONFIG_SOURCE_DHCP4) |
| 1402 |
continue; |
| 1403 |
|
| 1404 |
assert(a->family == AF_INET); |
| 1405 |
|
| 1406 |
log_link_debug(link, "DHCPv4 CLIENT: requesting previously acquired address %s.", |
| 1407 |
IN4_ADDR_TO_STRING(&a->in_addr.in)); |
| 1408 |
return sd_dhcp_client_set_request_address(link->dhcp_client, &a->in_addr.in); |
| 1409 |
} |
| 1410 |
|
| 1411 |
/* 2. If no address is assigned yet, use explicitly configured address. */ |
| 1412 |
if (in4_addr_is_set(&link->network->dhcp_request_address)) { |
| 1413 |
log_link_debug(link, "DHCPv4 CLIENT: requesting address %s specified by RequestAddress=.", |
| 1414 |
IN4_ADDR_TO_STRING(&link->network->dhcp_request_address)); |
| 1415 |
return sd_dhcp_client_set_request_address(link->dhcp_client, &link->network->dhcp_request_address); |
| 1416 |
} |
| 1417 |
|
| 1418 |
/* 3. If KeepConfiguration=dynamic, use a foreign dynamic address. */ |
| 1419 |
if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) |
| 1420 |
return 0; |
| 1421 |
|
| 1422 |
SET_FOREACH(a, link->addresses) { |
| 1423 |
if (a->source != NETWORK_CONFIG_SOURCE_FOREIGN) |
| 1424 |
continue; |
| 1425 |
if (a->family != AF_INET) |
| 1426 |
continue; |
| 1427 |
if (!link_address_is_dynamic(link, a)) |
| 1428 |
continue; |
| 1429 |
|
| 1430 |
log_link_debug(link, "DHCPv4 CLIENT: requesting foreign dynamic address %s.", |
| 1431 |
IN4_ADDR_TO_STRING(&a->in_addr.in)); |
| 1432 |
return sd_dhcp_client_set_request_address(link->dhcp_client, &a->in_addr.in); |
| 1433 |
} |
| 1434 |
|
| 1435 |
return 0; |
| 1436 |
} |
| 1437 |
|
| 1438 |
static bool link_needs_dhcp_broadcast(Link *link) { |
| 1439 |
int r; |
| 1440 |
|
| 1441 |
assert(link); |
| 1442 |
assert(link->network); |
| 1443 |
|
| 1444 |
/* Return the setting in DHCP[4].RequestBroadcast if specified. Otherwise return the device property |
| 1445 |
* ID_NET_DHCP_BROADCAST setting, which may be set for interfaces requiring that the DHCPOFFER |
| 1446 |
* message is being broadcast because they can't handle unicast messages while not fully configured. |
| 1447 |
* If neither is set or a failure occurs, return false, which is the default for this flag. */ |
| 1448 |
|
| 1449 |
r = link->network->dhcp_broadcast; |
| 1450 |
if (r >= 0) |
| 1451 |
return r; |
| 1452 |
|
| 1453 |
if (!link->dev) |
| 1454 |
return false; |
| 1455 |
|
| 1456 |
r = device_get_property_bool(link->dev, "ID_NET_DHCP_BROADCAST"); |
| 1457 |
if (r < 0) { |
| 1458 |
if (r != -ENOENT) |
| 1459 |
log_link_warning_errno(link, r, "DHCPv4 CLIENT: Failed to get or parse ID_NET_DHCP_BROADCAST, ignoring: %m"); |
| 1460 |
|
| 1461 |
return false; |
| 1462 |
} |
| 1463 |
|
| 1464 |
log_link_debug(link, "DHCPv4 CLIENT: Detected ID_NET_DHCP_BROADCAST='%d'.", r); |
| 1465 |
return r; |
| 1466 |
} |
| 1467 |
|
| 1468 |
static bool link_dhcp4_ipv6_only_mode(Link *link) { |
| 1469 |
assert(link); |
| 1470 |
assert(link->network); |
| 1471 |
|
| 1472 |
/* If it is explicitly specified, then honor the setting. */ |
| 1473 |
if (link->network->dhcp_ipv6_only_mode >= 0) |
| 1474 |
return link->network->dhcp_ipv6_only_mode; |
| 1475 |
|
| 1476 |
/* Defaults to false, until we support 464XLAT. See issue #30891. */ |
| 1477 |
return false; |
| 1478 |
} |
| 1479 |
|
| 1480 |
static int dhcp4_configure(Link *link) { |
| 1481 |
void *request_options; |
| 1482 |
int r; |
| 1483 |
|
| 1484 |
assert(link); |
| 1485 |
assert(link->network); |
| 1486 |
|
| 1487 |
if (link->dhcp_client) |
| 1488 |
return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "DHCPv4 client is already configured."); |
| 1489 |
|
| 1490 |
r = sd_dhcp_client_new(&link->dhcp_client); |
| 1491 |
if (r < 0) |
| 1492 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to allocate DHCPv4 client: %m"); |
| 1493 |
|
| 1494 |
r = sd_dhcp_client_set_anonymize(link->dhcp_client, link->network->dhcp_anonymize); |
| 1495 |
if (r < 0) |
| 1496 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to anonymize requests: %m"); |
| 1497 |
|
| 1498 |
r = sd_dhcp_client_set_bootp(link->dhcp_client, link->network->dhcp_use_bootp); |
| 1499 |
if (r < 0) |
| 1500 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to %s BOOTP: %m", |
| 1501 |
enable_disable(link->network->dhcp_use_bootp)); |
| 1502 |
|
| 1503 |
r = sd_dhcp_client_set_send_release(link->dhcp_client, link->network->dhcp_send_release); |
| 1504 |
if (r < 0) |
| 1505 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to %s sending release message on stop: %m", |
| 1506 |
enable_disable(link->network->dhcp_send_release)); |
| 1507 |
|
| 1508 |
r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0); |
| 1509 |
if (r < 0) |
| 1510 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to attach event to DHCPv4 client: %m"); |
| 1511 |
|
| 1512 |
r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev); |
| 1513 |
if (r < 0) |
| 1514 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to attach device: %m"); |
| 1515 |
|
| 1516 |
r = sd_dhcp_client_set_rapid_commit(link->dhcp_client, link->network->dhcp_use_rapid_commit); |
| 1517 |
if (r < 0) |
| 1518 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set rapid commit: %m"); |
| 1519 |
|
| 1520 |
r = sd_dhcp_client_set_mac(link->dhcp_client, |
| 1521 |
link->hw_addr.bytes, |
| 1522 |
link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL, |
| 1523 |
link->hw_addr.length, link->iftype); |
| 1524 |
if (r < 0) |
| 1525 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MAC address: %m"); |
| 1526 |
|
| 1527 |
r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex); |
| 1528 |
if (r < 0) |
| 1529 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set ifindex: %m"); |
| 1530 |
|
| 1531 |
r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link); |
| 1532 |
if (r < 0) |
| 1533 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set callback: %m"); |
| 1534 |
|
| 1535 |
r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link_needs_dhcp_broadcast(link)); |
| 1536 |
if (r < 0) |
| 1537 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for broadcast: %m"); |
| 1538 |
|
| 1539 |
r = dhcp_client_set_state_callback(link->dhcp_client, dhcp_client_callback_bus, link); |
| 1540 |
if (r < 0) |
| 1541 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set state change callback: %m"); |
| 1542 |
|
| 1543 |
if (link->mtu > 0) { |
| 1544 |
r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); |
| 1545 |
if (r < 0) |
| 1546 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MTU: %m"); |
| 1547 |
} |
| 1548 |
|
| 1549 |
if (!link->network->dhcp_anonymize) { |
| 1550 |
r = dhcp4_set_request_address(link); |
| 1551 |
if (r < 0) |
| 1552 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set initial DHCPv4 address: %m"); |
| 1553 |
|
| 1554 |
if (link->network->dhcp_use_mtu) { |
| 1555 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_MTU_INTERFACE); |
| 1556 |
if (r < 0) |
| 1557 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for MTU: %m"); |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (link->network->dhcp_use_routes) { |
| 1561 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_STATIC_ROUTE); |
| 1562 |
if (r < 0) |
| 1563 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for static route: %m"); |
| 1564 |
|
| 1565 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE); |
| 1566 |
if (r < 0) |
| 1567 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for classless static route: %m"); |
| 1568 |
} |
| 1569 |
|
| 1570 |
if (link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP4) > 0) { |
| 1571 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH); |
| 1572 |
if (r < 0) |
| 1573 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for domain search list: %m"); |
| 1574 |
} |
| 1575 |
|
| 1576 |
if (link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) { |
| 1577 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER); |
| 1578 |
if (r < 0) |
| 1579 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for NTP server: %m"); |
| 1580 |
} |
| 1581 |
|
| 1582 |
if (link->network->dhcp_use_sip) { |
| 1583 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER); |
| 1584 |
if (r < 0) |
| 1585 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for SIP server: %m"); |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP4)) { |
| 1589 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_V4_DNR); |
| 1590 |
if (r < 0) |
| 1591 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for DNR: %m"); |
| 1592 |
} |
| 1593 |
|
| 1594 |
if (link->network->dhcp_use_captive_portal) { |
| 1595 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DHCP_CAPTIVE_PORTAL); |
| 1596 |
if (r < 0) |
| 1597 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for captive portal: %m"); |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (link->network->dhcp_use_timezone) { |
| 1601 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_TZDB_TIMEZONE); |
| 1602 |
if (r < 0) |
| 1603 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for timezone: %m"); |
| 1604 |
} |
| 1605 |
|
| 1606 |
if (link->network->dhcp_use_6rd) { |
| 1607 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_6RD); |
| 1608 |
if (r < 0) |
| 1609 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for 6rd: %m"); |
| 1610 |
} |
| 1611 |
|
| 1612 |
if (link_dhcp4_ipv6_only_mode(link)) { |
| 1613 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED); |
| 1614 |
if (r < 0) |
| 1615 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for IPv6-only preferred option: %m"); |
| 1616 |
} |
| 1617 |
|
| 1618 |
SET_FOREACH(request_options, link->network->dhcp_request_options) { |
| 1619 |
uint32_t option = PTR_TO_UINT32(request_options); |
| 1620 |
|
| 1621 |
r = sd_dhcp_client_set_request_option(link->dhcp_client, option); |
| 1622 |
if (r < 0) |
| 1623 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for '%u': %m", option); |
| 1624 |
} |
| 1625 |
|
| 1626 |
r = dhcp_client_set_extra_options(link->dhcp_client, &link->network->dhcp_extra_options); |
| 1627 |
if (r < 0) |
| 1628 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set extra options: %m"); |
| 1629 |
|
| 1630 |
r = dhcp_client_set_vendor_options(link->dhcp_client, &link->network->dhcp_vendor_options); |
| 1631 |
if (r < 0) |
| 1632 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set vendor options: %m"); |
| 1633 |
|
| 1634 |
r = dhcp4_set_hostname(link); |
| 1635 |
if (r < 0) |
| 1636 |
return r; |
| 1637 |
|
| 1638 |
if (link->network->dhcp_vendor_class_identifier) { |
| 1639 |
r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client, |
| 1640 |
link->network->dhcp_vendor_class_identifier); |
| 1641 |
if (r < 0) |
| 1642 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set vendor class identifier: %m"); |
| 1643 |
} |
| 1644 |
|
| 1645 |
if (link->network->dhcp_mudurl) { |
| 1646 |
r = sd_dhcp_client_set_mud_url(link->dhcp_client, link->network->dhcp_mudurl); |
| 1647 |
if (r < 0) |
| 1648 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MUD URL: %m"); |
| 1649 |
} |
| 1650 |
|
| 1651 |
r = dhcp_client_set_user_class(link->dhcp_client, &link->network->dhcp_user_class); |
| 1652 |
if (r < 0) |
| 1653 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set user class: %m"); |
| 1654 |
} |
| 1655 |
|
| 1656 |
if (link->network->dhcp_client_port > 0) { |
| 1657 |
r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port); |
| 1658 |
if (r < 0) |
| 1659 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set listen port: %m"); |
| 1660 |
} |
| 1661 |
if (link->network->dhcp_port > 0) { |
| 1662 |
r = sd_dhcp_client_set_port(link->dhcp_client, link->network->dhcp_port); |
| 1663 |
if (r < 0) |
| 1664 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set server port: %m"); |
| 1665 |
} |
| 1666 |
|
| 1667 |
if (link->network->dhcp_max_attempts > 0) { |
| 1668 |
r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts); |
| 1669 |
if (r < 0) |
| 1670 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set max attempts: %m"); |
| 1671 |
} |
| 1672 |
|
| 1673 |
if (link->network->dhcp_ip_service_type >= 0) { |
| 1674 |
assert(link->network->dhcp_ip_service_type <= UINT8_MAX); |
| 1675 |
r = sd_dhcp_client_set_ip_service_type(link->dhcp_client, link->network->dhcp_ip_service_type); |
| 1676 |
if (r < 0) |
| 1677 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set IP service type: %m"); |
| 1678 |
} |
| 1679 |
|
| 1680 |
if (link->network->dhcp_socket_priority >= 0) { |
| 1681 |
r = sd_dhcp_client_set_socket_priority(link->dhcp_client, link->network->dhcp_socket_priority); |
| 1682 |
if (r < 0) |
| 1683 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set socket priority: %m"); |
| 1684 |
} |
| 1685 |
|
| 1686 |
if (link->network->dhcp_fallback_lease_lifetime_usec > 0) { |
| 1687 |
r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime_usec); |
| 1688 |
if (r < 0) |
| 1689 |
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set fallback lease lifetime: %m"); |
| 1690 |
} |
| 1691 |
|
| 1692 |
return dhcp4_set_client_identifier(link); |
| 1693 |
} |
| 1694 |
|
| 1695 |
int dhcp4_update_mac(Link *link) { |
| 1696 |
bool restart; |
| 1697 |
int r; |
| 1698 |
|
| 1699 |
assert(link); |
| 1700 |
|
| 1701 |
if (!link->dhcp_client) |
| 1702 |
return 0; |
| 1703 |
|
| 1704 |
restart = sd_dhcp_client_is_running(link->dhcp_client); |
| 1705 |
|
| 1706 |
r = sd_dhcp_client_stop(link->dhcp_client); |
| 1707 |
if (r < 0) |
| 1708 |
return r; |
| 1709 |
|
| 1710 |
r = sd_dhcp_client_set_mac(link->dhcp_client, |
| 1711 |
link->hw_addr.bytes, |
| 1712 |
link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL, |
| 1713 |
link->hw_addr.length, link->iftype); |
| 1714 |
if (r < 0) |
| 1715 |
return r; |
| 1716 |
|
| 1717 |
r = dhcp4_set_client_identifier(link); |
| 1718 |
if (r < 0) |
| 1719 |
return r; |
| 1720 |
|
| 1721 |
if (restart) { |
| 1722 |
r = dhcp4_start(link); |
| 1723 |
if (r < 0) |
| 1724 |
return r; |
| 1725 |
} |
| 1726 |
|
| 1727 |
return 0; |
| 1728 |
} |
| 1729 |
|
| 1730 |
int dhcp4_update_ipv6_connectivity(Link *link) { |
| 1731 |
int r; |
| 1732 |
|
| 1733 |
assert(link); |
| 1734 |
|
| 1735 |
if (!link->network) |
| 1736 |
return 0; |
| 1737 |
|
| 1738 |
if (!link->network->dhcp_ipv6_only_mode) |
| 1739 |
return 0; |
| 1740 |
|
| 1741 |
if (!link->dhcp_client) |
| 1742 |
return 0; |
| 1743 |
|
| 1744 |
bool have = link_has_ipv6_connectivity(link); |
| 1745 |
r = sd_dhcp_client_set_ipv6_connectivity(link->dhcp_client, have); |
| 1746 |
if (r < 0) |
| 1747 |
return r; |
| 1748 |
|
| 1749 |
/* If we do not have IPv6 connectivity, and the client has been already stopped or not started yet, |
| 1750 |
* let's start the client if possible. */ |
| 1751 |
if (!have && !sd_dhcp_client_is_running(link->dhcp_client)) { |
| 1752 |
r = dhcp4_start_full(link, /* set_ipv6_connectivity= */ false); |
| 1753 |
if (r < 0) |
| 1754 |
return r; |
| 1755 |
} |
| 1756 |
|
| 1757 |
return 0; |
| 1758 |
} |
| 1759 |
|
| 1760 |
int dhcp4_start_full(Link *link, bool set_ipv6_connectivity) { |
| 1761 |
int r; |
| 1762 |
|
| 1763 |
assert(link); |
| 1764 |
assert(link->manager); |
| 1765 |
assert(link->network); |
| 1766 |
|
| 1767 |
/* On stopping/restarting networkd, we may drop IPv6 connectivity (which depends on KeepConfiguration= |
| 1768 |
* setting). Do not (re)start DHCPv4 client in that case. See issue #39299. */ |
| 1769 |
if (link->manager->state != MANAGER_RUNNING) |
| 1770 |
return 0; |
| 1771 |
|
| 1772 |
if (!link->dhcp_client) |
| 1773 |
return 0; |
| 1774 |
|
| 1775 |
if (link_dhcp_enabled_by_bearer(link, AF_INET) == 0) |
| 1776 |
return 0; |
| 1777 |
|
| 1778 |
if (!link_has_carrier(link)) |
| 1779 |
return 0; |
| 1780 |
|
| 1781 |
if (sd_dhcp_client_is_running(link->dhcp_client) > 0) |
| 1782 |
return 0; |
| 1783 |
|
| 1784 |
r = sd_dhcp_client_start(link->dhcp_client); |
| 1785 |
if (r < 0) |
| 1786 |
return r; |
| 1787 |
|
| 1788 |
if (set_ipv6_connectivity) { |
| 1789 |
r = dhcp4_update_ipv6_connectivity(link); |
| 1790 |
if (r < 0) |
| 1791 |
return r; |
| 1792 |
} |
| 1793 |
|
| 1794 |
return 1; |
| 1795 |
} |
| 1796 |
|
| 1797 |
int dhcp4_renew(Link *link) { |
| 1798 |
assert(link); |
| 1799 |
|
| 1800 |
if (!link->dhcp_client) |
| 1801 |
return 0; |
| 1802 |
|
| 1803 |
/* The DHCPv4 client may have been stopped by the IPv6 only mode. Let's unconditionally restart the |
| 1804 |
* client if it is not running. */ |
| 1805 |
if (!sd_dhcp_client_is_running(link->dhcp_client)) |
| 1806 |
return dhcp4_start(link); |
| 1807 |
|
| 1808 |
/* The client may be waiting for IPv6 connectivity. Let's restart the client in that case. */ |
| 1809 |
if (sd_dhcp_client_is_waiting_for_ipv6_connectivity(link->dhcp_client)) { |
| 1810 |
sd_dhcp_client_stop(link->dhcp_client); |
| 1811 |
return dhcp4_start(link); |
| 1812 |
} |
| 1813 |
|
| 1814 |
/* Otherwise, send a RENEW command. */ |
| 1815 |
return sd_dhcp_client_send_renew(link->dhcp_client); |
| 1816 |
} |
| 1817 |
|
| 1818 |
static int dhcp4_configure_duid(Link *link) { |
| 1819 |
assert(link); |
| 1820 |
assert(link->network); |
| 1821 |
|
| 1822 |
if (link->network->dhcp_client_identifier != DHCP_CLIENT_ID_DUID) |
| 1823 |
return 1; |
| 1824 |
|
| 1825 |
return dhcp_configure_duid(link, link_get_dhcp4_duid(link)); |
| 1826 |
} |
| 1827 |
|
| 1828 |
static int dhcp4_process_request(Request *req, Link *link, void *userdata) { |
| 1829 |
int r; |
| 1830 |
|
| 1831 |
assert(link); |
| 1832 |
|
| 1833 |
if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false)) |
| 1834 |
return 0; |
| 1835 |
|
| 1836 |
r = dhcp4_configure_duid(link); |
| 1837 |
if (r <= 0) |
| 1838 |
return r; |
| 1839 |
|
| 1840 |
r = dhcp4_configure(link); |
| 1841 |
if (r < 0) |
| 1842 |
return log_link_warning_errno(link, r, "Failed to configure DHCPv4 client: %m"); |
| 1843 |
|
| 1844 |
r = dhcp4_start(link); |
| 1845 |
if (r < 0) |
| 1846 |
return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m"); |
| 1847 |
|
| 1848 |
log_link_debug(link, "DHCPv4 client is configured%s.", |
| 1849 |
r > 0 ? ", acquiring DHCPv4 lease" : ""); |
| 1850 |
return 1; |
| 1851 |
} |
| 1852 |
|
| 1853 |
int link_request_dhcp4_client(Link *link) { |
| 1854 |
int r; |
| 1855 |
|
| 1856 |
assert(link); |
| 1857 |
|
| 1858 |
if (!link_dhcp4_enabled(link)) |
| 1859 |
return 0; |
| 1860 |
|
| 1861 |
if (link->dhcp_client) |
| 1862 |
return 0; |
| 1863 |
|
| 1864 |
r = link_queue_request(link, REQUEST_TYPE_DHCP4_CLIENT, dhcp4_process_request, NULL); |
| 1865 |
if (r < 0) |
| 1866 |
return log_link_warning_errno(link, r, "Failed to request configuring of the DHCPv4 client: %m"); |
| 1867 |
|
| 1868 |
log_link_debug(link, "Requested configuring of the DHCPv4 client."); |
| 1869 |
return 0; |
| 1870 |
} |
| 1871 |
|
| 1872 |
static bool link_should_drop_dhcp4_config(Link *link, Network *network) { |
| 1873 |
assert(link); |
| 1874 |
assert(link->network); |
| 1875 |
|
| 1876 |
if (!link_dhcp4_enabled(link)) |
| 1877 |
/* DHCP client is now disabled. */ |
| 1878 |
return true; |
| 1879 |
|
| 1880 |
if (link->dhcp_client && link->network->dhcp_use_bootp && |
| 1881 |
network && !network->dhcp_use_bootp && network->dhcp_send_release) |
| 1882 |
/* The client was enabled as a DHCP client and sending release message is requested, and now |
| 1883 |
* the client is enabled as a BOOTP client. In this case, we need to release the previous |
| 1884 |
* lease, and hence all DHCPv4 configurations (address, routes, DNS servers, and so on) needs |
| 1885 |
* to be dropped. */ |
| 1886 |
return true; |
| 1887 |
|
| 1888 |
return false; |
| 1889 |
} |
| 1890 |
|
| 1891 |
int link_drop_dhcp4_config(Link *link, Network *network) { |
| 1892 |
int ret = 0; |
| 1893 |
|
| 1894 |
assert(link); |
| 1895 |
assert(link->network); |
| 1896 |
|
| 1897 |
if (link->network == network) |
| 1898 |
return 0; /* .network file is unchanged. It is not necessary to reconfigure the client. */ |
| 1899 |
|
| 1900 |
if (link_should_drop_dhcp4_config(link, network)) { |
| 1901 |
/* Stop the client if it is running and drop the lease. */ |
| 1902 |
ret = sd_dhcp_client_stop(link->dhcp_client); |
| 1903 |
|
| 1904 |
/* Also explicitly drop DHCPv4 address and routes. Why? This is for the case when the DHCPv4 |
| 1905 |
* client was enabled on the previous invocation of networkd, but when it is restarted, a new |
| 1906 |
* .network file may match to the interface, and DHCPv4 client may be disabled. In that case, |
| 1907 |
* the DHCPv4 client is not running, hence sd_dhcp_client_stop() in the above does nothing. */ |
| 1908 |
RET_GATHER(ret, dhcp4_remove_address_and_routes(link, /* only_marked= */ false)); |
| 1909 |
} |
| 1910 |
|
| 1911 |
/* Even if the client is currently enabled and also enabled in the new .network file, detailed |
| 1912 |
* settings for the client may be different. Let's unref() the client. But do not unref() the lease. |
| 1913 |
* it will be unref()ed later when a new lease is acquired. */ |
| 1914 |
link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client); |
| 1915 |
return ret; |
| 1916 |
} |
| 1917 |
|
| 1918 |
int config_parse_dhcp_max_attempts( |
| 1919 |
const char *unit, |
| 1920 |
const char *filename, |
| 1921 |
unsigned line, |
| 1922 |
const char *section, |
| 1923 |
unsigned section_line, |
| 1924 |
const char *lvalue, |
| 1925 |
int ltype, |
| 1926 |
const char *rvalue, |
| 1927 |
void *data, |
| 1928 |
void *userdata) { |
| 1929 |
|
| 1930 |
Network *network = ASSERT_PTR(data); |
| 1931 |
uint64_t a; |
| 1932 |
int r; |
| 1933 |
|
| 1934 |
assert(lvalue); |
| 1935 |
assert(rvalue); |
| 1936 |
|
| 1937 |
if (isempty(rvalue)) { |
| 1938 |
network->dhcp_max_attempts = 0; |
| 1939 |
return 0; |
| 1940 |
} |
| 1941 |
|
| 1942 |
if (streq(rvalue, "infinity")) { |
| 1943 |
network->dhcp_max_attempts = UINT64_MAX; |
| 1944 |
return 0; |
| 1945 |
} |
| 1946 |
|
| 1947 |
r = safe_atou64(rvalue, &a); |
| 1948 |
if (r < 0) { |
| 1949 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1950 |
"Failed to parse DHCP maximum attempts, ignoring: %s", rvalue); |
| 1951 |
return 0; |
| 1952 |
} |
| 1953 |
|
| 1954 |
if (a == 0) { |
| 1955 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1956 |
"%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue); |
| 1957 |
return 0; |
| 1958 |
} |
| 1959 |
|
| 1960 |
network->dhcp_max_attempts = a; |
| 1961 |
|
| 1962 |
return 0; |
| 1963 |
} |
| 1964 |
|
| 1965 |
int config_parse_dhcp_ip_service_type( |
| 1966 |
const char *unit, |
| 1967 |
const char *filename, |
| 1968 |
unsigned line, |
| 1969 |
const char *section, |
| 1970 |
unsigned section_line, |
| 1971 |
const char *lvalue, |
| 1972 |
int ltype, |
| 1973 |
const char *rvalue, |
| 1974 |
void *data, |
| 1975 |
void *userdata) { |
| 1976 |
|
| 1977 |
int *tos = ASSERT_PTR(data); |
| 1978 |
|
| 1979 |
assert(filename); |
| 1980 |
assert(lvalue); |
| 1981 |
assert(rvalue); |
| 1982 |
|
| 1983 |
if (isempty(rvalue)) |
| 1984 |
*tos = -1; /* use sd_dhcp_client's default (currently, CS6). */ |
| 1985 |
else if (streq(rvalue, "none")) |
| 1986 |
*tos = 0; |
| 1987 |
else if (streq(rvalue, "CS4")) |
| 1988 |
*tos = IPTOS_CLASS_CS4; |
| 1989 |
else if (streq(rvalue, "CS6")) |
| 1990 |
*tos = IPTOS_CLASS_CS6; |
| 1991 |
else |
| 1992 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1993 |
"Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue); |
| 1994 |
|
| 1995 |
return 0; |
| 1996 |
} |
| 1997 |
|
| 1998 |
int config_parse_dhcp_socket_priority( |
| 1999 |
const char *unit, |
| 2000 |
const char *filename, |
| 2001 |
unsigned line, |
| 2002 |
const char *section, |
| 2003 |
unsigned section_line, |
| 2004 |
const char *lvalue, |
| 2005 |
int ltype, |
| 2006 |
const char *rvalue, |
| 2007 |
void *data, |
| 2008 |
void *userdata) { |
| 2009 |
|
| 2010 |
int r, a, *priority = ASSERT_PTR(data); |
| 2011 |
|
| 2012 |
assert(lvalue); |
| 2013 |
assert(rvalue); |
| 2014 |
|
| 2015 |
if (isempty(rvalue)) { |
| 2016 |
*priority = -1; |
| 2017 |
return 0; |
| 2018 |
} |
| 2019 |
|
| 2020 |
r = safe_atoi(rvalue, &a); |
| 2021 |
if (r < 0) { |
| 2022 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2023 |
"Failed to parse socket priority, ignoring: %s", rvalue); |
| 2024 |
return 0; |
| 2025 |
} |
| 2026 |
if (a < 0 || a > TC_PRIO_MAX) { |
| 2027 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2028 |
"Invalid socket priority, must be in 0…%i, ignoring: %i", TC_PRIO_MAX, a); |
| 2029 |
return 0; |
| 2030 |
} |
| 2031 |
|
| 2032 |
*priority = a; |
| 2033 |
return 0; |
| 2034 |
} |
| 2035 |
|
| 2036 |
int config_parse_dhcp_fallback_lease_lifetime( |
| 2037 |
const char *unit, |
| 2038 |
const char *filename, |
| 2039 |
unsigned line, |
| 2040 |
const char *section, |
| 2041 |
unsigned section_line, |
| 2042 |
const char *lvalue, |
| 2043 |
int ltype, |
| 2044 |
const char *rvalue, |
| 2045 |
void *data, |
| 2046 |
void *userdata) { |
| 2047 |
|
| 2048 |
Network *network = userdata; |
| 2049 |
|
| 2050 |
assert(filename); |
| 2051 |
assert(section); |
| 2052 |
assert(lvalue); |
| 2053 |
assert(rvalue); |
| 2054 |
assert(data); |
| 2055 |
|
| 2056 |
if (isempty(rvalue)) { |
| 2057 |
network->dhcp_fallback_lease_lifetime_usec = 0; |
| 2058 |
return 0; |
| 2059 |
} |
| 2060 |
|
| 2061 |
/* We accept only "forever" or "infinity". */ |
| 2062 |
if (!STR_IN_SET(rvalue, "forever", "infinity")) { |
| 2063 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2064 |
"Invalid LeaseLifetime= value, ignoring: %s", rvalue); |
| 2065 |
return 0; |
| 2066 |
} |
| 2067 |
|
| 2068 |
network->dhcp_fallback_lease_lifetime_usec = USEC_INFINITY; |
| 2069 |
|
| 2070 |
return 0; |
| 2071 |
} |
| 2072 |
|
| 2073 |
int config_parse_dhcp_label( |
| 2074 |
const char *unit, |
| 2075 |
const char *filename, |
| 2076 |
unsigned line, |
| 2077 |
const char *section, |
| 2078 |
unsigned section_line, |
| 2079 |
const char *lvalue, |
| 2080 |
int ltype, |
| 2081 |
const char *rvalue, |
| 2082 |
void *data, |
| 2083 |
void *userdata) { |
| 2084 |
|
| 2085 |
char **label = ASSERT_PTR(data); |
| 2086 |
|
| 2087 |
assert(filename); |
| 2088 |
assert(lvalue); |
| 2089 |
assert(rvalue); |
| 2090 |
|
| 2091 |
if (isempty(rvalue)) { |
| 2092 |
*label = mfree(*label); |
| 2093 |
return 0; |
| 2094 |
} |
| 2095 |
|
| 2096 |
if (!address_label_valid(rvalue)) { |
| 2097 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2098 |
"Address label is too long or invalid, ignoring assignment: %s", rvalue); |
| 2099 |
return 0; |
| 2100 |
} |
| 2101 |
|
| 2102 |
return free_and_strdup_warn(label, rvalue); |
| 2103 |
} |
| 2104 |
|
| 2105 |
static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = { |
| 2106 |
[DHCP_CLIENT_ID_MAC] = "mac", |
| 2107 |
[DHCP_CLIENT_ID_DUID] = "duid", |
| 2108 |
}; |
| 2109 |
|
| 2110 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier); |
| 2111 |
DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier); |