| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2013 Intel Corporation. All rights reserved. |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <linux/if_infiniband.h> |
| 7 |
#include <net/if_arp.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "device-util.h" |
| 12 |
#include "dhcp-client-internal.h" |
| 13 |
#include "dhcp-client-send.h" |
| 14 |
#include "dhcp-lease-internal.h" |
| 15 |
#include "dns-domain.h" |
| 16 |
#include "errno-util.h" |
| 17 |
#include "event-util.h" |
| 18 |
#include "fd-util.h" |
| 19 |
#include "hostname-util.h" |
| 20 |
#include "iovec-util.h" |
| 21 |
#include "iovec-wrapper.h" |
| 22 |
#include "ip-util.h" |
| 23 |
#include "network-common.h" |
| 24 |
#include "random-util.h" |
| 25 |
#include "set.h" |
| 26 |
#include "socket-util.h" |
| 27 |
#include "string-util.h" |
| 28 |
#include "time-util.h" |
| 29 |
#include "tlv-util.h" |
| 30 |
#include "web-util.h" |
| 31 |
|
| 32 |
#define MAX_MAC_ADDR_LEN CONST_MAX(INFINIBAND_ALEN, ETH_ALEN) |
| 33 |
|
| 34 |
#define RESTART_AFTER_NAK_MIN_USEC (1 * USEC_PER_SEC) |
| 35 |
#define RESTART_AFTER_NAK_MAX_USEC (30 * USEC_PER_MINUTE) |
| 36 |
|
| 37 |
#define MAX_REQUEST_ATTEMPTS_ON_REBOOTING 2 |
| 38 |
#define MAX_REQUEST_ATTEMPTS 5 |
| 39 |
#define TRANSIENT_FAILURE_ATTEMPTS 3 /* Arbitrary limit: how many attempts are considered enough to report |
| 40 |
* transient failure. */ |
| 41 |
|
| 42 |
static const uint8_t default_req_opts[] = { |
| 43 |
SD_DHCP_OPTION_SUBNET_MASK, |
| 44 |
SD_DHCP_OPTION_ROUTER, |
| 45 |
SD_DHCP_OPTION_HOST_NAME, |
| 46 |
SD_DHCP_OPTION_DOMAIN_NAME, |
| 47 |
SD_DHCP_OPTION_DOMAIN_NAME_SERVER, |
| 48 |
}; |
| 49 |
|
| 50 |
static void client_stop(sd_dhcp_client *client, int error); |
| 51 |
static int client_restart(sd_dhcp_client *client); |
| 52 |
|
| 53 |
int dhcp_client_set_state_callback( |
| 54 |
sd_dhcp_client *client, |
| 55 |
sd_dhcp_client_callback_t cb, |
| 56 |
void *userdata) { |
| 57 |
|
| 58 |
assert_return(client, -EINVAL); |
| 59 |
|
| 60 |
client->state_callback = cb; |
| 61 |
client->state_userdata = userdata; |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
int sd_dhcp_client_set_callback( |
| 67 |
sd_dhcp_client *client, |
| 68 |
sd_dhcp_client_callback_t cb, |
| 69 |
void *userdata) { |
| 70 |
|
| 71 |
assert_return(client, -EINVAL); |
| 72 |
|
| 73 |
client->callback = cb; |
| 74 |
client->userdata = userdata; |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
int sd_dhcp_client_set_anonymize(sd_dhcp_client *client, int b) { |
| 80 |
assert_return(client, -EINVAL); |
| 81 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 82 |
|
| 83 |
client->anonymize = !!b; |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
int sd_dhcp_client_set_request_broadcast(sd_dhcp_client *client, int broadcast) { |
| 88 |
assert_return(client, -EINVAL); |
| 89 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 90 |
|
| 91 |
client->request_broadcast = broadcast; |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) { |
| 97 |
assert_return(client, -EINVAL); |
| 98 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 99 |
|
| 100 |
switch (option) { |
| 101 |
|
| 102 |
case SD_DHCP_OPTION_PAD: |
| 103 |
case SD_DHCP_OPTION_OVERLOAD: |
| 104 |
case SD_DHCP_OPTION_MESSAGE_TYPE: |
| 105 |
case SD_DHCP_OPTION_PARAMETER_REQUEST_LIST: |
| 106 |
case SD_DHCP_OPTION_END: |
| 107 |
return -EINVAL; |
| 108 |
|
| 109 |
default: |
| 110 |
; |
| 111 |
} |
| 112 |
|
| 113 |
return set_ensure_put(&client->req_opts, NULL, UINT8_TO_PTR(option)); |
| 114 |
} |
| 115 |
|
| 116 |
int sd_dhcp_client_set_request_address( |
| 117 |
sd_dhcp_client *client, |
| 118 |
const struct in_addr *last_addr) { |
| 119 |
|
| 120 |
assert_return(client, -EINVAL); |
| 121 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 122 |
|
| 123 |
if (last_addr) |
| 124 |
client->last_addr = last_addr->s_addr; |
| 125 |
else |
| 126 |
client->last_addr = INADDR_ANY; |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
int sd_dhcp_client_set_ifindex(sd_dhcp_client *client, int ifindex) { |
| 132 |
assert_return(client, -EINVAL); |
| 133 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 134 |
assert_return(ifindex > 0, -EINVAL); |
| 135 |
|
| 136 |
client->ifindex = ifindex; |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
int sd_dhcp_client_set_ifname(sd_dhcp_client *client, const char *ifname) { |
| 141 |
assert_return(client, -EINVAL); |
| 142 |
assert_return(ifname, -EINVAL); |
| 143 |
|
| 144 |
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) |
| 145 |
return -EINVAL; |
| 146 |
|
| 147 |
return free_and_strdup(&client->ifname, ifname); |
| 148 |
} |
| 149 |
|
| 150 |
int sd_dhcp_client_get_ifname(sd_dhcp_client *client, const char **ret) { |
| 151 |
int r; |
| 152 |
|
| 153 |
assert_return(client, -EINVAL); |
| 154 |
|
| 155 |
r = get_ifname(client->ifindex, &client->ifname); |
| 156 |
if (r < 0) |
| 157 |
return r; |
| 158 |
|
| 159 |
if (ret) |
| 160 |
*ret = client->ifname; |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
int sd_dhcp_client_set_mac( |
| 166 |
sd_dhcp_client *client, |
| 167 |
const uint8_t *hw_addr, |
| 168 |
const uint8_t *bcast_addr, |
| 169 |
size_t addr_len, |
| 170 |
uint16_t arp_type) { |
| 171 |
|
| 172 |
assert_return(client, -EINVAL); |
| 173 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 174 |
|
| 175 |
static const uint8_t default_eth_hwaddr[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 176 |
|
| 177 |
switch (arp_type) { |
| 178 |
case ARPHRD_ETHER: |
| 179 |
assert_return(addr_len == ETH_ALEN, -EINVAL); |
| 180 |
assert_return(hw_addr, -EINVAL); |
| 181 |
break; |
| 182 |
|
| 183 |
case ARPHRD_INFINIBAND: |
| 184 |
assert_return(addr_len == INFINIBAND_ALEN, -EINVAL); |
| 185 |
assert_return(hw_addr, -EINVAL); |
| 186 |
break; |
| 187 |
|
| 188 |
case ARPHRD_RAWIP: |
| 189 |
case ARPHRD_NONE: |
| 190 |
/* Linux cellular modem drivers (e.g. qmi_wwan) present a network interface of type |
| 191 |
* ARPHRD_RAWIP(519) or ARPHRD_NONE(65534) when in point-to-point mode, but these are not |
| 192 |
* valid DHCP hardware-type values. |
| 193 |
* |
| 194 |
* Apparently, it's best to just pretend that these are ethernet devices. Other approaches |
| 195 |
* have been tried, but resulted in incompatibilities with some server software. See |
| 196 |
* https://lore.kernel.org/netdev/[email protected]/ */ |
| 197 |
arp_type = ARPHRD_ETHER; |
| 198 |
if (addr_len == 0) { |
| 199 |
/* If the specified hardware address length is 0, always use the default ones. */ |
| 200 |
addr_len = ETH_ALEN; |
| 201 |
hw_addr = default_eth_hwaddr; |
| 202 |
bcast_addr = NULL; |
| 203 |
} else if (addr_len == ETH_ALEN) { |
| 204 |
/* If the specified hardware address length is ETH_ALEN, use the default ones when |
| 205 |
* unspecified. */ |
| 206 |
if (!hw_addr) |
| 207 |
hw_addr = default_eth_hwaddr; |
| 208 |
} else { |
| 209 |
/* Otherwise, user must specify valid addresses. */ |
| 210 |
assert_return(hw_addr, -EINVAL); |
| 211 |
assert_return(bcast_addr, -EINVAL); |
| 212 |
} |
| 213 |
break; |
| 214 |
|
| 215 |
default: |
| 216 |
return -EINVAL; |
| 217 |
} |
| 218 |
|
| 219 |
client->arp_type = arp_type; |
| 220 |
hw_addr_set(&client->hw_addr, hw_addr, addr_len); |
| 221 |
hw_addr_set(&client->bcast_addr, bcast_addr, bcast_addr ? addr_len : 0); |
| 222 |
return hw_addr_ensure_broadcast(&client->bcast_addr, arp_type); |
| 223 |
} |
| 224 |
|
| 225 |
int sd_dhcp_client_get_client_id(sd_dhcp_client *client, const sd_dhcp_client_id **ret) { |
| 226 |
assert_return(client, -EINVAL); |
| 227 |
assert_return(ret, -EINVAL); |
| 228 |
|
| 229 |
if (!sd_dhcp_client_id_is_set(&client->client_id)) |
| 230 |
return -ENODATA; |
| 231 |
|
| 232 |
*ret = &client->client_id; |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
int sd_dhcp_client_set_client_id( |
| 237 |
sd_dhcp_client *client, |
| 238 |
uint8_t type, |
| 239 |
const uint8_t *data, |
| 240 |
size_t data_len) { |
| 241 |
|
| 242 |
assert_return(client, -EINVAL); |
| 243 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 244 |
assert_return(data, -EINVAL); |
| 245 |
assert_return(client_id_data_size_is_valid(data_len), -EINVAL); |
| 246 |
|
| 247 |
/* For hardware types, log debug message about unexpected data length. |
| 248 |
* |
| 249 |
* Note that infiniband's INFINIBAND_ALEN is 20 bytes long, but only |
| 250 |
* the last 8 bytes of the address are stable and suitable to put into |
| 251 |
* the client-id. The caller is advised to account for that. */ |
| 252 |
if ((type == ARPHRD_ETHER && data_len != ETH_ALEN) || |
| 253 |
(type == ARPHRD_INFINIBAND && data_len != 8)) |
| 254 |
log_dhcp_client(client, |
| 255 |
"Changing client ID to hardware type %u with unexpected address length %zu", |
| 256 |
type, data_len); |
| 257 |
|
| 258 |
return sd_dhcp_client_id_set(&client->client_id, type, data, data_len); |
| 259 |
} |
| 260 |
|
| 261 |
static int dhcp_client_set_iaid_duid( |
| 262 |
sd_dhcp_client *client, |
| 263 |
bool iaid_set, |
| 264 |
uint32_t iaid, |
| 265 |
sd_dhcp_duid *duid) { |
| 266 |
|
| 267 |
int r; |
| 268 |
|
| 269 |
if (!iaid_set) { |
| 270 |
r = dhcp_identifier_set_iaid(client->dev, &client->hw_addr, |
| 271 |
/* legacy_unstable_byteorder= */ true, |
| 272 |
&iaid); |
| 273 |
if (r < 0) |
| 274 |
return r; |
| 275 |
|
| 276 |
iaid = be32toh(iaid); |
| 277 |
} |
| 278 |
|
| 279 |
return sd_dhcp_client_id_set_iaid_duid(&client->client_id, iaid, duid); |
| 280 |
} |
| 281 |
|
| 282 |
int sd_dhcp_client_set_iaid_duid_llt( |
| 283 |
sd_dhcp_client *client, |
| 284 |
bool iaid_set, |
| 285 |
uint32_t iaid, |
| 286 |
usec_t llt_time) { |
| 287 |
|
| 288 |
sd_dhcp_duid duid; |
| 289 |
int r; |
| 290 |
|
| 291 |
assert_return(client, -EINVAL); |
| 292 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 293 |
|
| 294 |
r = sd_dhcp_duid_set_llt(&duid, client->hw_addr.bytes, client->hw_addr.length, client->arp_type, llt_time); |
| 295 |
if (r < 0) |
| 296 |
return r; |
| 297 |
|
| 298 |
return dhcp_client_set_iaid_duid(client, iaid_set, iaid, &duid); |
| 299 |
} |
| 300 |
|
| 301 |
int sd_dhcp_client_set_iaid_duid_ll( |
| 302 |
sd_dhcp_client *client, |
| 303 |
bool iaid_set, |
| 304 |
uint32_t iaid) { |
| 305 |
|
| 306 |
sd_dhcp_duid duid; |
| 307 |
int r; |
| 308 |
|
| 309 |
assert_return(client, -EINVAL); |
| 310 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 311 |
|
| 312 |
r = sd_dhcp_duid_set_ll(&duid, client->hw_addr.bytes, client->hw_addr.length, client->arp_type); |
| 313 |
if (r < 0) |
| 314 |
return r; |
| 315 |
|
| 316 |
return dhcp_client_set_iaid_duid(client, iaid_set, iaid, &duid); |
| 317 |
} |
| 318 |
|
| 319 |
int sd_dhcp_client_set_iaid_duid_en( |
| 320 |
sd_dhcp_client *client, |
| 321 |
bool iaid_set, |
| 322 |
uint32_t iaid) { |
| 323 |
|
| 324 |
sd_dhcp_duid duid; |
| 325 |
int r; |
| 326 |
|
| 327 |
assert_return(client, -EINVAL); |
| 328 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 329 |
|
| 330 |
r = sd_dhcp_duid_set_en(&duid); |
| 331 |
if (r < 0) |
| 332 |
return r; |
| 333 |
|
| 334 |
return dhcp_client_set_iaid_duid(client, iaid_set, iaid, &duid); |
| 335 |
} |
| 336 |
|
| 337 |
int sd_dhcp_client_set_iaid_duid_uuid( |
| 338 |
sd_dhcp_client *client, |
| 339 |
bool iaid_set, |
| 340 |
uint32_t iaid) { |
| 341 |
|
| 342 |
sd_dhcp_duid duid; |
| 343 |
int r; |
| 344 |
|
| 345 |
assert_return(client, -EINVAL); |
| 346 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 347 |
|
| 348 |
r = sd_dhcp_duid_set_uuid(&duid); |
| 349 |
if (r < 0) |
| 350 |
return r; |
| 351 |
|
| 352 |
return dhcp_client_set_iaid_duid(client, iaid_set, iaid, &duid); |
| 353 |
} |
| 354 |
|
| 355 |
int sd_dhcp_client_set_iaid_duid_raw( |
| 356 |
sd_dhcp_client *client, |
| 357 |
bool iaid_set, |
| 358 |
uint32_t iaid, |
| 359 |
uint16_t duid_type, |
| 360 |
const uint8_t *duid_data, |
| 361 |
size_t duid_data_len) { |
| 362 |
|
| 363 |
sd_dhcp_duid duid; |
| 364 |
int r; |
| 365 |
|
| 366 |
assert_return(client, -EINVAL); |
| 367 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 368 |
assert_return(duid_data || duid_data_len == 0, -EINVAL); |
| 369 |
|
| 370 |
r = sd_dhcp_duid_set(&duid, duid_type, duid_data, duid_data_len); |
| 371 |
if (r < 0) |
| 372 |
return r; |
| 373 |
|
| 374 |
return dhcp_client_set_iaid_duid(client, iaid_set, iaid, &duid); |
| 375 |
} |
| 376 |
|
| 377 |
int sd_dhcp_client_set_rapid_commit(sd_dhcp_client *client, bool rapid_commit) { |
| 378 |
assert_return(client, -EINVAL); |
| 379 |
|
| 380 |
client->rapid_commit = !client->anonymize && rapid_commit; |
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 384 |
int sd_dhcp_client_set_hostname( |
| 385 |
sd_dhcp_client *client, |
| 386 |
const char *hostname) { |
| 387 |
|
| 388 |
assert_return(client, -EINVAL); |
| 389 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 390 |
|
| 391 |
/* Make sure hostnames qualify as DNS and as Linux hostnames */ |
| 392 |
if (hostname && |
| 393 |
!(hostname_is_valid(hostname, 0) && dns_name_is_valid(hostname) > 0)) |
| 394 |
return -EINVAL; |
| 395 |
|
| 396 |
return free_and_strdup(&client->hostname, hostname); |
| 397 |
} |
| 398 |
|
| 399 |
int sd_dhcp_client_set_vendor_class_identifier( |
| 400 |
sd_dhcp_client *client, |
| 401 |
const char *vci) { |
| 402 |
|
| 403 |
assert_return(client, -EINVAL); |
| 404 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 405 |
|
| 406 |
return free_and_strdup(&client->vendor_class_identifier, vci); |
| 407 |
} |
| 408 |
|
| 409 |
int sd_dhcp_client_set_mud_url( |
| 410 |
sd_dhcp_client *client, |
| 411 |
const char *mudurl) { |
| 412 |
|
| 413 |
assert_return(client, -EINVAL); |
| 414 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 415 |
assert_return(mudurl, -EINVAL); |
| 416 |
assert_return(strlen(mudurl) <= 255, -EINVAL); |
| 417 |
assert_return(http_url_is_valid(mudurl), -EINVAL); |
| 418 |
|
| 419 |
return free_and_strdup(&client->mudurl, mudurl); |
| 420 |
} |
| 421 |
|
| 422 |
int dhcp_client_set_user_class(sd_dhcp_client *client, const struct iovec_wrapper *user_class) { |
| 423 |
int r; |
| 424 |
|
| 425 |
assert_return(client, -EINVAL); |
| 426 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 427 |
|
| 428 |
if (iovw_isempty(user_class)) { |
| 429 |
iovw_done_free(&client->user_class); |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 434 |
FOREACH_ARRAY(iovec, user_class->iovec, user_class->count) { |
| 435 |
if (iovec->iov_len == 0 || iovec->iov_len > UINT8_MAX) |
| 436 |
return -EINVAL; |
| 437 |
|
| 438 |
r = iovw_extend_iov(&iovw, iovec); |
| 439 |
if (r < 0) |
| 440 |
return r; |
| 441 |
} |
| 442 |
|
| 443 |
iovw_done_free(&client->user_class); |
| 444 |
client->user_class = TAKE_STRUCT(iovw); |
| 445 |
return 0; |
| 446 |
} |
| 447 |
|
| 448 |
int sd_dhcp_client_set_client_port( |
| 449 |
sd_dhcp_client *client, |
| 450 |
uint16_t port) { |
| 451 |
|
| 452 |
assert_return(client, -EINVAL); |
| 453 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 454 |
|
| 455 |
client->port = port; |
| 456 |
|
| 457 |
return 0; |
| 458 |
} |
| 459 |
|
| 460 |
int sd_dhcp_client_set_port( |
| 461 |
sd_dhcp_client *client, |
| 462 |
uint16_t port) { |
| 463 |
|
| 464 |
assert_return(client, -EINVAL); |
| 465 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 466 |
|
| 467 |
client->server_port = port; |
| 468 |
|
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
int sd_dhcp_client_set_mtu(sd_dhcp_client *client, uint32_t mtu) { |
| 473 |
assert_return(client, -EINVAL); |
| 474 |
|
| 475 |
/* MTU may be changed by the acquired lease. Hence, we cannot require that the client is stopped here. |
| 476 |
* Please do not add assertion for !sd_dhcp_client_is_running(client) here. */ |
| 477 |
|
| 478 |
if (mtu < IPV4_MIN_MTU) |
| 479 |
return -ERANGE; |
| 480 |
|
| 481 |
client->mtu = mtu; |
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
int sd_dhcp_client_set_max_attempts(sd_dhcp_client *client, uint64_t max_attempts) { |
| 486 |
assert_return(client, -EINVAL); |
| 487 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 488 |
|
| 489 |
client->max_discover_attempts = max_attempts; |
| 490 |
|
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
int dhcp_client_set_extra_options(sd_dhcp_client *client, TLV *options) { |
| 495 |
assert(client); |
| 496 |
assert(!sd_dhcp_client_is_running(client)); |
| 497 |
|
| 498 |
return unref_and_replace_new_ref(client->extra_options, options, tlv_ref, tlv_unref); |
| 499 |
} |
| 500 |
|
| 501 |
int dhcp_client_set_vendor_options(sd_dhcp_client *client, TLV *options) { |
| 502 |
assert(client); |
| 503 |
assert(!sd_dhcp_client_is_running(client)); |
| 504 |
|
| 505 |
return unref_and_replace_new_ref(client->vendor_options, options, tlv_ref, tlv_unref); |
| 506 |
} |
| 507 |
|
| 508 |
int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) { |
| 509 |
assert_return(client, -EINVAL); |
| 510 |
|
| 511 |
if (!client->lease) |
| 512 |
return -EADDRNOTAVAIL; |
| 513 |
|
| 514 |
if (ret) |
| 515 |
*ret = client->lease; |
| 516 |
|
| 517 |
return 0; |
| 518 |
} |
| 519 |
|
| 520 |
int sd_dhcp_client_set_ip_service_type(sd_dhcp_client *client, uint8_t type) { |
| 521 |
assert_return(client, -EINVAL); |
| 522 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 523 |
|
| 524 |
client->ip_service_type = type; |
| 525 |
|
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
int sd_dhcp_client_set_socket_priority(sd_dhcp_client *client, int socket_priority) { |
| 530 |
assert_return(client, -EINVAL); |
| 531 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 532 |
|
| 533 |
client->socket_priority = socket_priority; |
| 534 |
|
| 535 |
return 0; |
| 536 |
} |
| 537 |
|
| 538 |
int sd_dhcp_client_set_fallback_lease_lifetime(sd_dhcp_client *client, uint64_t fallback_lease_lifetime) { |
| 539 |
assert_return(client, -EINVAL); |
| 540 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 541 |
assert_return(fallback_lease_lifetime > 0, -EINVAL); |
| 542 |
|
| 543 |
assert_cc(sizeof(usec_t) == sizeof(uint64_t)); |
| 544 |
client->fallback_lease_lifetime = fallback_lease_lifetime; |
| 545 |
|
| 546 |
return 0; |
| 547 |
} |
| 548 |
|
| 549 |
int sd_dhcp_client_set_bootp(sd_dhcp_client *client, int bootp) { |
| 550 |
assert_return(client, -EINVAL); |
| 551 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 552 |
|
| 553 |
client->bootp = bootp; |
| 554 |
|
| 555 |
return 0; |
| 556 |
} |
| 557 |
|
| 558 |
int sd_dhcp_client_set_send_release(sd_dhcp_client *client, int enable) { |
| 559 |
assert_return(client, -EINVAL); |
| 560 |
|
| 561 |
client->send_release = enable; |
| 562 |
|
| 563 |
return 0; |
| 564 |
} |
| 565 |
|
| 566 |
static void client_set_state(sd_dhcp_client *client, DHCPState state) { |
| 567 |
assert(client); |
| 568 |
|
| 569 |
if (client->state == state) |
| 570 |
return; |
| 571 |
|
| 572 |
log_dhcp_client(client, "State changed: %s -> %s", |
| 573 |
dhcp_state_to_string(client->state), dhcp_state_to_string(state)); |
| 574 |
|
| 575 |
client->state = state; |
| 576 |
|
| 577 |
switch (state) { |
| 578 |
case DHCP_STATE_STOPPED: |
| 579 |
case DHCP_STATE_BOUND: |
| 580 |
/* In these cases, the next DHCPDISCOVER message will be sent in a new cycle. |
| 581 |
* Hence, clear the counter for DHCPDISCOVER messages. */ |
| 582 |
client->discover_attempt = 0; |
| 583 |
break; |
| 584 |
|
| 585 |
case DHCP_STATE_REBOOTING: |
| 586 |
case DHCP_STATE_REQUESTING: |
| 587 |
case DHCP_STATE_RENEWING: |
| 588 |
case DHCP_STATE_REBINDING: |
| 589 |
/* In these cases, the next DHCPREQUEST message will be the first message in this new state. |
| 590 |
* Hence, clear the counter for DHCPREQUEST messages. */ |
| 591 |
client->request_attempt = 0; |
| 592 |
break; |
| 593 |
|
| 594 |
default: |
| 595 |
/* otherwise, do not reset the counters. */ |
| 596 |
; |
| 597 |
} |
| 598 |
|
| 599 |
// FIXME: If the state callback changes the state, we may not safely free/stop the client, and the |
| 600 |
// state machine diagram becomes needlessly complicated. Introduce a guard to avoid that. */ |
| 601 |
if (client->state_callback) |
| 602 |
client->state_callback(client, state, client->state_userdata); |
| 603 |
} |
| 604 |
|
| 605 |
int dhcp_client_get_state(sd_dhcp_client *client) { |
| 606 |
assert_return(client, -EINVAL); |
| 607 |
|
| 608 |
return client->state; |
| 609 |
} |
| 610 |
|
| 611 |
static int client_notify(sd_dhcp_client *client, int event) { |
| 612 |
assert(client); |
| 613 |
|
| 614 |
if (client->callback) |
| 615 |
return client->callback(client, event, client->userdata); |
| 616 |
|
| 617 |
return 0; |
| 618 |
} |
| 619 |
|
| 620 |
static void client_disable_event_sources(sd_dhcp_client *client) { |
| 621 |
assert(client); |
| 622 |
|
| 623 |
client->receive_message = sd_event_source_disable_unref(client->receive_message); |
| 624 |
|
| 625 |
(void) event_source_disable(client->timeout_resend); |
| 626 |
(void) event_source_disable(client->timeout_t1); |
| 627 |
(void) event_source_disable(client->timeout_t2); |
| 628 |
(void) event_source_disable(client->timeout_expire); |
| 629 |
} |
| 630 |
|
| 631 |
static void client_initialize(sd_dhcp_client *client) { |
| 632 |
assert(client); |
| 633 |
|
| 634 |
client_disable_event_sources(client); |
| 635 |
|
| 636 |
client_set_state(client, DHCP_STATE_STOPPED); |
| 637 |
client->xid = 0; |
| 638 |
|
| 639 |
client->lease = sd_dhcp_lease_unref(client->lease); |
| 640 |
} |
| 641 |
|
| 642 |
static void client_stop(sd_dhcp_client *client, int error) { |
| 643 |
assert(client); |
| 644 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 645 |
|
| 646 |
if (sd_dhcp_client_is_running(client)) { |
| 647 |
if (error < 0) |
| 648 |
log_dhcp_client_errno(client, error, "STOPPED: %m"); |
| 649 |
else if (error == SD_DHCP_CLIENT_EVENT_STOP) |
| 650 |
log_dhcp_client(client, "STOPPED"); |
| 651 |
else |
| 652 |
log_dhcp_client(client, "STOPPED: Unknown event"); |
| 653 |
|
| 654 |
client_notify(client, error); |
| 655 |
} else if (error < 0) { |
| 656 |
log_dhcp_client_errno(client, error, "FAILED: %m"); |
| 657 |
client_notify(client, error); |
| 658 |
} |
| 659 |
|
| 660 |
client_initialize(client); |
| 661 |
} |
| 662 |
|
| 663 |
/* RFC2131 section 4.1: |
| 664 |
* retransmission delays should include -1 to +1 sec of random 'fuzz'. */ |
| 665 |
#define RFC2131_RANDOM_FUZZ \ |
| 666 |
((int64_t)(random_u64() % (2 * USEC_PER_SEC)) - (int64_t)USEC_PER_SEC) |
| 667 |
|
| 668 |
/* RFC2131 section 4.1: |
| 669 |
* for retransmission delays, timeout should start at 4s then double |
| 670 |
* each attempt with max of 64s, with -1 to +1 sec of random 'fuzz' added. |
| 671 |
* This assumes the first call will be using attempt 1. */ |
| 672 |
static usec_t client_compute_request_timeout(uint64_t attempt) { |
| 673 |
usec_t timeout = (UINT64_C(1) << MIN(attempt + 1, UINT64_C(6))) * USEC_PER_SEC; |
| 674 |
return usec_sub_signed(timeout, RFC2131_RANDOM_FUZZ); |
| 675 |
} |
| 676 |
|
| 677 |
/* RFC2131 section 4.4.5: |
| 678 |
* the client SHOULD wait one-half of the remaining time until T2 (in RENEWING state) |
| 679 |
* and one-half of the remaining lease time (in REBINDING state), down to a minimum |
| 680 |
* of 60 seconds. |
| 681 |
* Note that while the default T1/T2 initial times do have random 'fuzz' applied, |
| 682 |
* the RFC sec 4.4.5 does not mention adding any fuzz to retries. */ |
| 683 |
static usec_t client_compute_reacquisition_timeout(usec_t now_usec, usec_t expire) { |
| 684 |
return MAX(usec_sub_unsigned(expire, now_usec) / 2, 60 * USEC_PER_SEC); |
| 685 |
} |
| 686 |
|
| 687 |
static int client_timeout_resend( |
| 688 |
sd_event_source *s, |
| 689 |
uint64_t usec, |
| 690 |
void *userdata) { |
| 691 |
|
| 692 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 693 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 694 |
usec_t time_now, next_timeout; |
| 695 |
int r; |
| 696 |
|
| 697 |
assert(s); |
| 698 |
assert(client->event); |
| 699 |
|
| 700 |
r = sd_event_now(client->event, CLOCK_BOOTTIME, &time_now); |
| 701 |
if (r < 0) |
| 702 |
goto error; |
| 703 |
|
| 704 |
switch (client->state) { |
| 705 |
|
| 706 |
case DHCP_STATE_RENEWING: |
| 707 |
next_timeout = client_compute_reacquisition_timeout(time_now, client->t2_time); |
| 708 |
break; |
| 709 |
|
| 710 |
case DHCP_STATE_REBINDING: |
| 711 |
next_timeout = client_compute_reacquisition_timeout(time_now, client->expire_time); |
| 712 |
break; |
| 713 |
|
| 714 |
case DHCP_STATE_INIT: |
| 715 |
client_set_state(client, DHCP_STATE_SELECTING); |
| 716 |
_fallthrough_; |
| 717 |
|
| 718 |
case DHCP_STATE_SELECTING: |
| 719 |
if (client->discover_attempt >= client->max_discover_attempts) { |
| 720 |
r = -ETIMEDOUT; |
| 721 |
goto error; |
| 722 |
} |
| 723 |
|
| 724 |
client->discover_attempt++; |
| 725 |
next_timeout = client_compute_request_timeout(client->discover_attempt); |
| 726 |
break; |
| 727 |
|
| 728 |
case DHCP_STATE_INIT_REBOOT: |
| 729 |
client_set_state(client, DHCP_STATE_REBOOTING); |
| 730 |
_fallthrough_; |
| 731 |
|
| 732 |
case DHCP_STATE_REBOOTING: |
| 733 |
/* There is nothing explicitly mentioned about retry interval on reboot. Let's reuse the same |
| 734 |
* algorithm as in the requesting state below, but slightly speed up for faster reboot. */ |
| 735 |
|
| 736 |
if (client->request_attempt >= MAX_REQUEST_ATTEMPTS_ON_REBOOTING) |
| 737 |
goto restart; |
| 738 |
|
| 739 |
client->request_attempt++; |
| 740 |
next_timeout = client_compute_request_timeout(client->request_attempt) / 4; |
| 741 |
break; |
| 742 |
|
| 743 |
case DHCP_STATE_REQUESTING: |
| 744 |
if (client->request_attempt >= MAX_REQUEST_ATTEMPTS) |
| 745 |
goto restart; |
| 746 |
|
| 747 |
client->request_attempt++; |
| 748 |
next_timeout = client_compute_request_timeout(client->request_attempt); |
| 749 |
break; |
| 750 |
|
| 751 |
default: |
| 752 |
assert_not_reached(); |
| 753 |
} |
| 754 |
|
| 755 |
r = event_reset_time_relative( |
| 756 |
client->event, &client->timeout_resend, |
| 757 |
CLOCK_BOOTTIME, next_timeout, 10 * USEC_PER_MSEC, |
| 758 |
client_timeout_resend, client, |
| 759 |
client->event_priority, "dhcp4-resend-timer", true); |
| 760 |
if (r < 0) |
| 761 |
goto error; |
| 762 |
|
| 763 |
switch (client->state) { |
| 764 |
case DHCP_STATE_SELECTING: |
| 765 |
r = dhcp_client_send_message(client, DHCP_DISCOVER); |
| 766 |
if (r < 0 && client->discover_attempt >= client->max_discover_attempts) |
| 767 |
goto error; |
| 768 |
|
| 769 |
if (client->discover_attempt >= TRANSIENT_FAILURE_ATTEMPTS) |
| 770 |
client_notify(client, SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE); |
| 771 |
break; |
| 772 |
|
| 773 |
case DHCP_STATE_REBOOTING: |
| 774 |
r = dhcp_client_send_message(client, DHCP_REQUEST); |
| 775 |
if (r < 0 && client->request_attempt >= MAX_REQUEST_ATTEMPTS_ON_REBOOTING) |
| 776 |
goto restart; |
| 777 |
break; |
| 778 |
|
| 779 |
case DHCP_STATE_REQUESTING: |
| 780 |
case DHCP_STATE_RENEWING: |
| 781 |
case DHCP_STATE_REBINDING: |
| 782 |
r = dhcp_client_send_message(client, DHCP_REQUEST); |
| 783 |
if (r < 0 && client->request_attempt >= MAX_REQUEST_ATTEMPTS) |
| 784 |
goto restart; |
| 785 |
break; |
| 786 |
|
| 787 |
default: |
| 788 |
assert_not_reached(); |
| 789 |
} |
| 790 |
|
| 791 |
return 0; |
| 792 |
|
| 793 |
restart: |
| 794 |
/* Avoid REQUEST infinite loop. Per RFC 2131 section 3.1.5: if the client receives |
| 795 |
neither a DHCPACK or a DHCPNAK message after employing the retransmission algorithm, |
| 796 |
the client reverts to INIT state and restarts the initialization process */ |
| 797 |
log_dhcp_client(client, "Max REQUEST attempts reached. Restarting..."); |
| 798 |
r = client_restart(client); |
| 799 |
if (r >= 0) |
| 800 |
return 0; |
| 801 |
|
| 802 |
error: |
| 803 |
client_stop(client, r); |
| 804 |
|
| 805 |
/* Errors were dealt with when stopping the client, don't spill |
| 806 |
errors into the event loop handler */ |
| 807 |
return 0; |
| 808 |
} |
| 809 |
|
| 810 |
static int client_initialize_time_events(sd_dhcp_client *client) { |
| 811 |
assert(client); |
| 812 |
assert(client->event); |
| 813 |
|
| 814 |
return event_reset_time_relative( |
| 815 |
client->event, |
| 816 |
&client->timeout_resend, |
| 817 |
CLOCK_BOOTTIME, |
| 818 |
client->start_delay, |
| 819 |
/* accuracy= */ 0, |
| 820 |
client_timeout_resend, |
| 821 |
client, |
| 822 |
client->event_priority, |
| 823 |
"dhcp4-resend-timer", |
| 824 |
/* force_reset= */ true); |
| 825 |
} |
| 826 |
|
| 827 |
static int client_start_delayed(sd_dhcp_client *client) { |
| 828 |
assert(client); |
| 829 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 830 |
|
| 831 |
client_disable_event_sources(client); |
| 832 |
client->lease = sd_dhcp_lease_unref(client->lease); |
| 833 |
|
| 834 |
client->xid = random_u32(); |
| 835 |
client->start_time = now(CLOCK_BOOTTIME); |
| 836 |
|
| 837 |
if (client->state != DHCP_STATE_INIT_REBOOT) |
| 838 |
client_set_state(client, DHCP_STATE_INIT); |
| 839 |
|
| 840 |
return client_initialize_time_events(client); |
| 841 |
} |
| 842 |
|
| 843 |
static int client_start(sd_dhcp_client *client) { |
| 844 |
assert(client); |
| 845 |
|
| 846 |
client->start_delay = 0; |
| 847 |
return client_start_delayed(client); |
| 848 |
} |
| 849 |
|
| 850 |
static int client_restart(sd_dhcp_client *client) { |
| 851 |
assert(client); |
| 852 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 853 |
|
| 854 |
/* This is called when we receive a DHCPNAK or could not receive any replies. */ |
| 855 |
|
| 856 |
/* First, if we have a bound lease, then notify it is expired. */ |
| 857 |
if (IN_SET(client->state, DHCP_STATE_BOUND, DHCP_STATE_RENEWING, DHCP_STATE_REBINDING)) { |
| 858 |
client_notify(client, SD_DHCP_CLIENT_EVENT_EXPIRED); |
| 859 |
|
| 860 |
if (client->state == DHCP_STATE_STOPPED) |
| 861 |
return 0; /* The notify callback stopped the client. */ |
| 862 |
} |
| 863 |
|
| 864 |
/* On reboot, DHCPNAK or no reply suggests that the network is changed or the address is already |
| 865 |
* used by another host. Let's restart the client immediately without any delay to speed up the |
| 866 |
* reboot process. */ |
| 867 |
if (client->state == DHCP_STATE_REBOOTING) |
| 868 |
return client_start(client); |
| 869 |
|
| 870 |
/* Otherwise, we should restart the client with a short delay. */ |
| 871 |
client->start_delay = CLAMP(client->start_delay * 2, |
| 872 |
RESTART_AFTER_NAK_MIN_USEC, RESTART_AFTER_NAK_MAX_USEC); |
| 873 |
|
| 874 |
log_dhcp_client(client, "REBOOT in %s", FORMAT_TIMESPAN(client->start_delay, USEC_PER_SEC)); |
| 875 |
return client_start_delayed(client); |
| 876 |
} |
| 877 |
|
| 878 |
static int client_timeout_expire(sd_event_source *s, uint64_t usec, void *userdata) { |
| 879 |
sd_dhcp_client *client = userdata; |
| 880 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 881 |
int r; |
| 882 |
|
| 883 |
log_dhcp_client(client, "EXPIRED"); |
| 884 |
|
| 885 |
client_notify(client, SD_DHCP_CLIENT_EVENT_EXPIRED); |
| 886 |
|
| 887 |
if (client->state == DHCP_STATE_STOPPED) |
| 888 |
return 0; /* The notify callback stopped the client. */ |
| 889 |
|
| 890 |
r = client_start(client); |
| 891 |
if (r < 0) |
| 892 |
client_stop(client, r); |
| 893 |
|
| 894 |
return 0; |
| 895 |
} |
| 896 |
|
| 897 |
static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) { |
| 898 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 899 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 900 |
|
| 901 |
/* Explicitly close the unicast socket opened during renewing. On success path, the socket will be |
| 902 |
* closed anyway on sending broadcast DHCPREQUEST, but let's explicitly close it here for failure |
| 903 |
* path to ignore all unicast replies from now on. */ |
| 904 |
client->receive_message = sd_event_source_disable_unref(client->receive_message); |
| 905 |
|
| 906 |
client_set_state(client, DHCP_STATE_REBINDING); |
| 907 |
|
| 908 |
return client_timeout_resend(s, usec, userdata); |
| 909 |
} |
| 910 |
|
| 911 |
static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata) { |
| 912 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 913 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 914 |
|
| 915 |
client_set_state(client, DHCP_STATE_RENEWING); |
| 916 |
|
| 917 |
return client_timeout_resend(s, usec, userdata); |
| 918 |
} |
| 919 |
|
| 920 |
static int client_enter_requesting(sd_dhcp_client *client) { |
| 921 |
assert(client); |
| 922 |
assert(client->lease); |
| 923 |
|
| 924 |
client_disable_event_sources(client); |
| 925 |
|
| 926 |
client_set_state(client, DHCP_STATE_REQUESTING); |
| 927 |
|
| 928 |
if (sd_dhcp_client_is_waiting_for_ipv6_connectivity(client)) { |
| 929 |
if (client->ipv6_acquired) { |
| 930 |
log_dhcp_client(client, |
| 931 |
"Received an OFFER with IPv6-only preferred option, and the host already acquired IPv6 connectivity, stopping DHCPv4 client."); |
| 932 |
return sd_dhcp_client_stop(client); |
| 933 |
} |
| 934 |
|
| 935 |
log_dhcp_client(client, |
| 936 |
"Received an OFFER with IPv6-only preferred option, delaying to send REQUEST with %s.", |
| 937 |
FORMAT_TIMESPAN(client->lease->ipv6_only_preferred_usec, USEC_PER_SEC)); |
| 938 |
} |
| 939 |
|
| 940 |
return event_reset_time_relative( |
| 941 |
client->event, |
| 942 |
&client->timeout_resend, |
| 943 |
CLOCK_BOOTTIME, |
| 944 |
client->lease->ipv6_only_preferred_usec, |
| 945 |
/* accuracy= */ 0, |
| 946 |
client_timeout_resend, |
| 947 |
client, |
| 948 |
client->event_priority, |
| 949 |
"dhcp4-resend-timer", |
| 950 |
/* force_reset= */ true); |
| 951 |
} |
| 952 |
|
| 953 |
static bool lease_equal(const sd_dhcp_lease *a, const sd_dhcp_lease *b) { |
| 954 |
if (a->address != b->address) |
| 955 |
return false; |
| 956 |
|
| 957 |
if (a->subnet_mask != b->subnet_mask) |
| 958 |
return false; |
| 959 |
|
| 960 |
if (a->router_size != b->router_size) |
| 961 |
return false; |
| 962 |
|
| 963 |
for (size_t i = 0; i < a->router_size; i++) |
| 964 |
if (a->router[i].s_addr != b->router[i].s_addr) |
| 965 |
return false; |
| 966 |
|
| 967 |
return true; |
| 968 |
} |
| 969 |
|
| 970 |
static int client_set_lease_timeouts(sd_dhcp_client *client) { |
| 971 |
usec_t time_now; |
| 972 |
int r; |
| 973 |
|
| 974 |
assert(client); |
| 975 |
assert(client->event); |
| 976 |
assert(client->lease); |
| 977 |
assert(client->lease->lifetime > 0); |
| 978 |
assert(triple_timestamp_is_set(&client->lease->timestamp)); |
| 979 |
|
| 980 |
/* don't set timers for infinite leases */ |
| 981 |
if (client->lease->lifetime == USEC_INFINITY) { |
| 982 |
(void) event_source_disable(client->timeout_t1); |
| 983 |
(void) event_source_disable(client->timeout_t2); |
| 984 |
(void) event_source_disable(client->timeout_expire); |
| 985 |
|
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
r = sd_event_now(client->event, CLOCK_BOOTTIME, &time_now); |
| 990 |
if (r < 0) |
| 991 |
return r; |
| 992 |
|
| 993 |
r = sd_dhcp_lease_get_lifetime_timestamp(client->lease, CLOCK_BOOTTIME, &client->expire_time); |
| 994 |
if (r < 0) |
| 995 |
return r; |
| 996 |
r = sd_dhcp_lease_get_t1_timestamp(client->lease, CLOCK_BOOTTIME, &client->t1_time); |
| 997 |
if (r < 0) |
| 998 |
return r; |
| 999 |
r = sd_dhcp_lease_get_t2_timestamp(client->lease, CLOCK_BOOTTIME, &client->t2_time); |
| 1000 |
if (r < 0) |
| 1001 |
return r; |
| 1002 |
|
| 1003 |
/* RFC2131 section 4.4.5: |
| 1004 |
* Times T1 and T2 SHOULD be chosen with some random "fuzz". |
| 1005 |
* Since the RFC doesn't specify here the exact 'fuzz' to use, |
| 1006 |
* we use the range from section 4.1: -1 to +1 sec. */ |
| 1007 |
client->t1_time = usec_sub_signed(client->t1_time, RFC2131_RANDOM_FUZZ); |
| 1008 |
client->t2_time = usec_sub_signed(client->t2_time, RFC2131_RANDOM_FUZZ); |
| 1009 |
|
| 1010 |
/* after fuzzing, ensure t2 is still >= t1 */ |
| 1011 |
client->t2_time = MAX(client->t1_time, client->t2_time); |
| 1012 |
|
| 1013 |
/* arm lifetime timeout */ |
| 1014 |
r = event_reset_time(client->event, &client->timeout_expire, |
| 1015 |
CLOCK_BOOTTIME, |
| 1016 |
client->expire_time, 10 * USEC_PER_MSEC, |
| 1017 |
client_timeout_expire, client, |
| 1018 |
client->event_priority, "dhcp4-lifetime", true); |
| 1019 |
if (r < 0) |
| 1020 |
return r; |
| 1021 |
|
| 1022 |
/* don't arm earlier timeouts if this has already expired */ |
| 1023 |
if (client->expire_time <= time_now) |
| 1024 |
return 0; |
| 1025 |
|
| 1026 |
log_dhcp_client(client, "lease expires in %s", |
| 1027 |
FORMAT_TIMESPAN(client->expire_time - time_now, USEC_PER_SEC)); |
| 1028 |
|
| 1029 |
/* arm T2 timeout */ |
| 1030 |
r = event_reset_time(client->event, &client->timeout_t2, |
| 1031 |
CLOCK_BOOTTIME, |
| 1032 |
client->t2_time, 10 * USEC_PER_MSEC, |
| 1033 |
client_timeout_t2, client, |
| 1034 |
client->event_priority, "dhcp4-t2-timeout", true); |
| 1035 |
if (r < 0) |
| 1036 |
return r; |
| 1037 |
|
| 1038 |
/* don't arm earlier timeout if this has already expired */ |
| 1039 |
if (client->t2_time <= time_now) |
| 1040 |
return 0; |
| 1041 |
|
| 1042 |
log_dhcp_client(client, "T2 expires in %s", |
| 1043 |
FORMAT_TIMESPAN(client->t2_time - time_now, USEC_PER_SEC)); |
| 1044 |
|
| 1045 |
/* arm T1 timeout */ |
| 1046 |
r = event_reset_time(client->event, &client->timeout_t1, |
| 1047 |
CLOCK_BOOTTIME, |
| 1048 |
client->t1_time, 10 * USEC_PER_MSEC, |
| 1049 |
client_timeout_t1, client, |
| 1050 |
client->event_priority, "dhcp4-t1-timer", true); |
| 1051 |
if (r < 0) |
| 1052 |
return r; |
| 1053 |
|
| 1054 |
if (client->t1_time > time_now) |
| 1055 |
log_dhcp_client(client, "T1 expires in %s", |
| 1056 |
FORMAT_TIMESPAN(client->t1_time - time_now, USEC_PER_SEC)); |
| 1057 |
|
| 1058 |
return 0; |
| 1059 |
} |
| 1060 |
|
| 1061 |
static int client_enter_bound(sd_dhcp_client *client, sd_dhcp_lease *lease) { |
| 1062 |
int r; |
| 1063 |
|
| 1064 |
assert(client); |
| 1065 |
assert(lease); |
| 1066 |
|
| 1067 |
int notify_event; |
| 1068 |
switch (client->state) { |
| 1069 |
case DHCP_STATE_SELECTING: |
| 1070 |
case DHCP_STATE_REQUESTING: |
| 1071 |
case DHCP_STATE_REBOOTING: |
| 1072 |
notify_event = SD_DHCP_CLIENT_EVENT_IP_ACQUIRE; |
| 1073 |
break; |
| 1074 |
case DHCP_STATE_RENEWING: |
| 1075 |
case DHCP_STATE_REBINDING: |
| 1076 |
assert(client->lease); |
| 1077 |
if (lease_equal(client->lease, lease)) |
| 1078 |
notify_event = SD_DHCP_CLIENT_EVENT_RENEW; |
| 1079 |
else |
| 1080 |
notify_event = SD_DHCP_CLIENT_EVENT_IP_CHANGE; |
| 1081 |
break; |
| 1082 |
default: |
| 1083 |
assert_not_reached(); |
| 1084 |
} |
| 1085 |
|
| 1086 |
unref_and_replace_new_ref(client->lease, lease, sd_dhcp_lease_ref, sd_dhcp_lease_unref); |
| 1087 |
|
| 1088 |
client_disable_event_sources(client); |
| 1089 |
|
| 1090 |
client->start_delay = 0; |
| 1091 |
|
| 1092 |
client_set_state(client, DHCP_STATE_BOUND); |
| 1093 |
|
| 1094 |
client->last_addr = client->lease->address; |
| 1095 |
|
| 1096 |
r = client_set_lease_timeouts(client); |
| 1097 |
if (r < 0) |
| 1098 |
return log_dhcp_client_errno(client, r, "Failed to set lease timeouts: %m"); |
| 1099 |
|
| 1100 |
client_notify(client, notify_event); |
| 1101 |
return 0; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static int client_handle_message(sd_dhcp_client *client, const struct iovec *iov, const triple_timestamp *timestamp) { |
| 1105 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 1106 |
int r; |
| 1107 |
|
| 1108 |
assert(client); |
| 1109 |
assert(iov); |
| 1110 |
|
| 1111 |
_cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL; |
| 1112 |
r = dhcp_client_parse_message(client, iov, &lease); |
| 1113 |
if (ERRNO_IS_NEG_RESOURCE(r)) |
| 1114 |
return r; |
| 1115 |
if (r < 0) |
| 1116 |
return 0; /* Ignore all parse errors. */ |
| 1117 |
|
| 1118 |
switch (r) { |
| 1119 |
|
| 1120 |
case DHCP_OFFER: |
| 1121 |
dhcp_lease_set_timestamp(lease, timestamp); |
| 1122 |
|
| 1123 |
unref_and_replace_new_ref(client->lease, lease, sd_dhcp_lease_ref, sd_dhcp_lease_unref); |
| 1124 |
if (client_notify(client, SD_DHCP_CLIENT_EVENT_SELECTING) < 0) |
| 1125 |
return 0; /* networkd refused the server, ignoring the message. */ |
| 1126 |
if (client->state == DHCP_STATE_STOPPED) |
| 1127 |
return 0; /* The notify callback stopped the client. */ |
| 1128 |
return client_enter_requesting(client); |
| 1129 |
|
| 1130 |
case DHCP_ACK: |
| 1131 |
dhcp_lease_set_timestamp(lease, timestamp); |
| 1132 |
return client_enter_bound(client, lease); |
| 1133 |
|
| 1134 |
case DHCP_NAK: |
| 1135 |
return client_restart(client); |
| 1136 |
|
| 1137 |
default: |
| 1138 |
assert_not_reached(); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
static int client_receive_message(sd_dhcp_client *client, int fd, bool raw) { |
| 1143 |
int r; |
| 1144 |
|
| 1145 |
assert(client); |
| 1146 |
assert(fd >= 0); |
| 1147 |
|
| 1148 |
ssize_t buflen = next_datagram_size_fd(fd); |
| 1149 |
if (ERRNO_IS_NEG_TRANSIENT(buflen) || ERRNO_IS_NEG_DISCONNECT(buflen)) |
| 1150 |
return 0; |
| 1151 |
if (buflen < 0) { |
| 1152 |
log_dhcp_client_errno(client, buflen, "Failed to determine datagram size to read, ignoring: %m"); |
| 1153 |
return 0; |
| 1154 |
} |
| 1155 |
|
| 1156 |
_cleanup_free_ void *buf = malloc0(buflen); |
| 1157 |
if (!buf) |
| 1158 |
return -ENOMEM; |
| 1159 |
|
| 1160 |
/* This needs to be initialized with zero. See #20741. |
| 1161 |
* The issue is fixed on glibc-2.35 (8fba672472ae0055387e9315fc2eddfa6775ca79). */ |
| 1162 |
CMSG_BUFFER_TYPE(CMSG_SPACE_TIMEVAL + |
| 1163 |
CMSG_SPACE(sizeof(struct tpacket_auxdata))) control = {}; |
| 1164 |
struct msghdr msg = { |
| 1165 |
.msg_iov = &IOVEC_MAKE(buf, buflen), |
| 1166 |
.msg_iovlen = 1, |
| 1167 |
.msg_control = &control, |
| 1168 |
.msg_controllen = sizeof(control), |
| 1169 |
}; |
| 1170 |
|
| 1171 |
ssize_t len = recvmsg_safe(fd, &msg, MSG_DONTWAIT); |
| 1172 |
if (ERRNO_IS_NEG_TRANSIENT(len) || ERRNO_IS_NEG_DISCONNECT(len)) |
| 1173 |
return 0; |
| 1174 |
if (len < 0) { |
| 1175 |
log_dhcp_client_errno(client, len, |
| 1176 |
"Could not receive message from %s socket, ignoring: %m", |
| 1177 |
raw ? "RAW" : "UDP"); |
| 1178 |
return 0; |
| 1179 |
} |
| 1180 |
|
| 1181 |
struct iovec payload = IOVEC_MAKE(buf, len); |
| 1182 |
if (raw) { |
| 1183 |
struct tpacket_auxdata *aux = CMSG_FIND_DATA(&msg, SOL_PACKET, PACKET_AUXDATA, struct tpacket_auxdata); |
| 1184 |
bool checksum = !aux || !(aux->tp_status & TP_STATUS_CSUMNOTREADY); |
| 1185 |
|
| 1186 |
if (udp_packet_verify(&payload, client->port, checksum, &payload) < 0) |
| 1187 |
return 0; |
| 1188 |
} |
| 1189 |
|
| 1190 |
log_dhcp_client(client, "Received message from %s socket, processing.", raw ? "RAW" : "UDP"); |
| 1191 |
r = client_handle_message(client, &payload, TRIPLE_TIMESTAMP_FROM_CMSG(&msg)); |
| 1192 |
if (r < 0) |
| 1193 |
client_stop(client, r); |
| 1194 |
|
| 1195 |
return 0; |
| 1196 |
} |
| 1197 |
|
| 1198 |
int client_receive_message_udp(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 1199 |
return client_receive_message(userdata, fd, /* raw= */ false); |
| 1200 |
} |
| 1201 |
|
| 1202 |
int client_receive_message_raw(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 1203 |
return client_receive_message(userdata, fd, /* raw= */ true); |
| 1204 |
} |
| 1205 |
|
| 1206 |
int sd_dhcp_client_send_renew(sd_dhcp_client *client) { |
| 1207 |
if (!sd_dhcp_client_is_running(client) || client->state != DHCP_STATE_BOUND || client->bootp) |
| 1208 |
return 0; /* do nothing */ |
| 1209 |
|
| 1210 |
client_set_state(client, DHCP_STATE_RENEWING); |
| 1211 |
|
| 1212 |
client->start_delay = 0; |
| 1213 |
return client_initialize_time_events(client); |
| 1214 |
} |
| 1215 |
|
| 1216 |
int sd_dhcp_client_is_running(sd_dhcp_client *client) { |
| 1217 |
if (!client) |
| 1218 |
return 0; |
| 1219 |
|
| 1220 |
return client->state != DHCP_STATE_STOPPED; |
| 1221 |
} |
| 1222 |
|
| 1223 |
int sd_dhcp_client_start(sd_dhcp_client *client) { |
| 1224 |
int r; |
| 1225 |
|
| 1226 |
assert_return(client, -EINVAL); |
| 1227 |
assert_return(client->event, -EINVAL); |
| 1228 |
assert_return(client->ifindex > 0, -EINVAL); |
| 1229 |
assert_return(!hw_addr_is_null(&client->bcast_addr), -EINVAL); |
| 1230 |
|
| 1231 |
/* If no client identifier exists, construct an RFC 4361-compliant one */ |
| 1232 |
if (!sd_dhcp_client_id_is_set(&client->client_id)) { |
| 1233 |
r = sd_dhcp_client_set_iaid_duid_en(client, /* iaid_set= */ false, /* iaid= */ 0); |
| 1234 |
if (r < 0) |
| 1235 |
return r; |
| 1236 |
} |
| 1237 |
|
| 1238 |
/* RFC7844 section 3.3: |
| 1239 |
SHOULD perform a complete four-way handshake, starting with a |
| 1240 |
DHCPDISCOVER, to obtain a new address lease. If the client can |
| 1241 |
ascertain that this is exactly the same network to which it was |
| 1242 |
previously connected, and if the link-layer address did not change, |
| 1243 |
the client MAY issue a DHCPREQUEST to try to reclaim the current |
| 1244 |
address. */ |
| 1245 |
if (client->last_addr && !client->anonymize && !client->bootp) |
| 1246 |
client_set_state(client, DHCP_STATE_INIT_REBOOT); |
| 1247 |
|
| 1248 |
/* We currently ignore: |
| 1249 |
* The client SHOULD wait a random time between one and ten seconds to desynchronize the use of |
| 1250 |
* DHCP at startup. */ |
| 1251 |
r = client_start(client); |
| 1252 |
if (r >= 0) |
| 1253 |
log_dhcp_client(client, "STARTED on ifindex %i", client->ifindex); |
| 1254 |
|
| 1255 |
return r; |
| 1256 |
} |
| 1257 |
|
| 1258 |
int sd_dhcp_client_send_decline(sd_dhcp_client *client) { |
| 1259 |
int r; |
| 1260 |
|
| 1261 |
if (!sd_dhcp_client_is_running(client) || !client->lease || client->bootp) |
| 1262 |
return 0; /* there is nothing to decline */ |
| 1263 |
|
| 1264 |
r = dhcp_client_send_message(client, DHCP_DECLINE); |
| 1265 |
if (r < 0) |
| 1266 |
return r; |
| 1267 |
|
| 1268 |
/* This function is mostly called when the acquired address conflicts with another host. |
| 1269 |
* Restarting the daemon to acquire another address. */ |
| 1270 |
return client_restart(client); |
| 1271 |
} |
| 1272 |
|
| 1273 |
static int client_send_release(sd_dhcp_client *client) { |
| 1274 |
assert(client); |
| 1275 |
|
| 1276 |
if (!client->send_release) |
| 1277 |
return 0; |
| 1278 |
|
| 1279 |
if (!sd_dhcp_client_is_running(client) || !client->lease || client->bootp) |
| 1280 |
return 0; /* there is nothing to release */ |
| 1281 |
|
| 1282 |
return dhcp_client_send_message(client, DHCP_RELEASE); |
| 1283 |
} |
| 1284 |
|
| 1285 |
int sd_dhcp_client_stop(sd_dhcp_client *client) { |
| 1286 |
if (!client) |
| 1287 |
return 0; |
| 1288 |
|
| 1289 |
DHCP_CLIENT_DONT_DESTROY(client); |
| 1290 |
|
| 1291 |
(void) client_send_release(client); |
| 1292 |
|
| 1293 |
client_stop(client, SD_DHCP_CLIENT_EVENT_STOP); |
| 1294 |
return 0; |
| 1295 |
} |
| 1296 |
|
| 1297 |
int sd_dhcp_client_is_waiting_for_ipv6_connectivity(sd_dhcp_client *client) { |
| 1298 |
/* Note that we intentionally do not implement the following behavior: |
| 1299 |
* |
| 1300 |
* RFC 8925, section 3.2: |
| 1301 |
* If the client is in the INIT-REBOOT state, it SHOULD stop the DHCPv4 configuration process or |
| 1302 |
* disable the IPv4 stack completely for V6ONLY_WAIT seconds or until the next network attachment |
| 1303 |
* event, whichever occurs first. |
| 1304 |
* |
| 1305 |
* Delaying the application of an acquired IPv4 address after DHCPACK introduces several issues: |
| 1306 |
* |
| 1307 |
* - If T1 is reached before the address is assigned to the interface, the client cannot send a |
| 1308 |
* unicast DHCPREQUEST during RENEWING. |
| 1309 |
* |
| 1310 |
* - If the client is stopped before the address is configured, it cannot send a DHCPRELEASE message, |
| 1311 |
* which also requires a valid source address. |
| 1312 |
* |
| 1313 |
* While these issues could be worked around, doing so would significantly complicate the |
| 1314 |
* implementation and violate assumptions in the DHCP state machine as defined in RFC 2131. |
| 1315 |
* |
| 1316 |
* Instead, we only honor the IPv6-Only Preferred delay (Option 108) in the REQUESTING state, i.e. |
| 1317 |
* before any DHCPREQUEST has been sent. */ |
| 1318 |
|
| 1319 |
return |
| 1320 |
client && |
| 1321 |
client->state == DHCP_STATE_REQUESTING && |
| 1322 |
client->request_attempt == 0 && |
| 1323 |
client->lease && |
| 1324 |
client->lease->ipv6_only_preferred_usec > 0; |
| 1325 |
} |
| 1326 |
|
| 1327 |
int sd_dhcp_client_set_ipv6_connectivity(sd_dhcp_client *client, int have) { |
| 1328 |
if (!client) |
| 1329 |
return 0; |
| 1330 |
|
| 1331 |
client->ipv6_acquired = have; |
| 1332 |
|
| 1333 |
if (have && sd_dhcp_client_is_waiting_for_ipv6_connectivity(client)) { |
| 1334 |
log_dhcp_client(client, |
| 1335 |
"Acquired IPv6 connectivity before sending REQUEST, stopping DHCPv4 client."); |
| 1336 |
return sd_dhcp_client_stop(client); |
| 1337 |
} |
| 1338 |
|
| 1339 |
return 0; |
| 1340 |
} |
| 1341 |
|
| 1342 |
int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event, int64_t priority) { |
| 1343 |
int r; |
| 1344 |
|
| 1345 |
assert_return(client, -EINVAL); |
| 1346 |
assert_return(!client->event, -EBUSY); |
| 1347 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 1348 |
|
| 1349 |
if (event) |
| 1350 |
client->event = sd_event_ref(event); |
| 1351 |
else { |
| 1352 |
r = sd_event_default(&client->event); |
| 1353 |
if (r < 0) |
| 1354 |
return r; |
| 1355 |
} |
| 1356 |
|
| 1357 |
client->event_priority = priority; |
| 1358 |
|
| 1359 |
return 0; |
| 1360 |
} |
| 1361 |
|
| 1362 |
int sd_dhcp_client_detach_event(sd_dhcp_client *client) { |
| 1363 |
assert_return(client, -EINVAL); |
| 1364 |
assert_return(!sd_dhcp_client_is_running(client), -EBUSY); |
| 1365 |
|
| 1366 |
client->event = sd_event_unref(client->event); |
| 1367 |
|
| 1368 |
return 0; |
| 1369 |
} |
| 1370 |
|
| 1371 |
sd_event* sd_dhcp_client_get_event(sd_dhcp_client *client) { |
| 1372 |
assert_return(client, NULL); |
| 1373 |
|
| 1374 |
return client->event; |
| 1375 |
} |
| 1376 |
|
| 1377 |
int sd_dhcp_client_attach_device(sd_dhcp_client *client, sd_device *dev) { |
| 1378 |
assert_return(client, -EINVAL); |
| 1379 |
|
| 1380 |
return device_unref_and_replace_new_ref(client->dev, dev); |
| 1381 |
} |
| 1382 |
|
| 1383 |
static sd_dhcp_client* dhcp_client_free(sd_dhcp_client *client) { |
| 1384 |
if (!client) |
| 1385 |
return NULL; |
| 1386 |
|
| 1387 |
log_dhcp_client(client, "FREE"); |
| 1388 |
|
| 1389 |
client_initialize(client); |
| 1390 |
|
| 1391 |
safe_close(client->socket_fd); |
| 1392 |
|
| 1393 |
sd_event_source_unref(client->timeout_resend); |
| 1394 |
sd_event_source_unref(client->timeout_t1); |
| 1395 |
sd_event_source_unref(client->timeout_t2); |
| 1396 |
sd_event_source_unref(client->timeout_expire); |
| 1397 |
|
| 1398 |
sd_dhcp_client_detach_event(client); |
| 1399 |
|
| 1400 |
sd_device_unref(client->dev); |
| 1401 |
|
| 1402 |
set_free(client->req_opts); |
| 1403 |
free(client->hostname); |
| 1404 |
free(client->vendor_class_identifier); |
| 1405 |
free(client->mudurl); |
| 1406 |
iovw_done_free(&client->user_class); |
| 1407 |
tlv_unref(client->extra_options); |
| 1408 |
tlv_unref(client->vendor_options); |
| 1409 |
free(client->ifname); |
| 1410 |
return mfree(client); |
| 1411 |
} |
| 1412 |
|
| 1413 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_client, sd_dhcp_client, dhcp_client_free); |
| 1414 |
|
| 1415 |
int sd_dhcp_client_new(sd_dhcp_client **ret) { |
| 1416 |
int r; |
| 1417 |
|
| 1418 |
assert_return(ret, -EINVAL); |
| 1419 |
|
| 1420 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = new(sd_dhcp_client, 1); |
| 1421 |
if (!client) |
| 1422 |
return -ENOMEM; |
| 1423 |
|
| 1424 |
*client = (sd_dhcp_client) { |
| 1425 |
.n_ref = 1, |
| 1426 |
.socket_fd = -EBADF, |
| 1427 |
.state = DHCP_STATE_STOPPED, |
| 1428 |
.ifindex = -1, |
| 1429 |
.port = DHCP_PORT_CLIENT, |
| 1430 |
.server_port = DHCP_PORT_SERVER, |
| 1431 |
.max_discover_attempts = UINT64_MAX, |
| 1432 |
.ip_service_type = IPTOS_CLASS_CS6, /* Defaults to CS6 (Internetwork Control). */ |
| 1433 |
.socket_priority = tos_to_priority(IPTOS_CLASS_CS6), |
| 1434 |
}; |
| 1435 |
|
| 1436 |
FOREACH_ELEMENT(opt, default_req_opts) { |
| 1437 |
r = sd_dhcp_client_set_request_option(client, *opt); |
| 1438 |
if (r < 0) |
| 1439 |
return r; |
| 1440 |
} |
| 1441 |
|
| 1442 |
*ret = TAKE_PTR(client); |
| 1443 |
|
| 1444 |
return 0; |
| 1445 |
} |