| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2014-2015 Intel Corporation. All rights reserved. |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <linux/if_arp.h> |
| 7 |
#include <linux/if_infiniband.h> |
| 8 |
|
| 9 |
#include "sd-dhcp6-client.h" |
| 10 |
#include "sd-dhcp6-option.h" |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "device-util.h" |
| 14 |
#include "dhcp-duid-internal.h" |
| 15 |
#include "dhcp6-client-internal.h" |
| 16 |
#include "dhcp6-internal.h" |
| 17 |
#include "dhcp6-lease-internal.h" |
| 18 |
#include "dns-domain.h" |
| 19 |
#include "errno-util.h" |
| 20 |
#include "event-util.h" |
| 21 |
#include "fd-util.h" |
| 22 |
#include "hostname-util.h" |
| 23 |
#include "in-addr-util.h" |
| 24 |
#include "iovec-util.h" |
| 25 |
#include "ordered-set.h" |
| 26 |
#include "random-util.h" |
| 27 |
#include "socket-util.h" |
| 28 |
#include "sort-util.h" |
| 29 |
#include "string-util.h" |
| 30 |
#include "strv.h" |
| 31 |
#include "web-util.h" |
| 32 |
|
| 33 |
#define DHCP6_CLIENT_DONT_DESTROY(client) \ |
| 34 |
_cleanup_(sd_dhcp6_client_unrefp) _unused_ sd_dhcp6_client *_dont_destroy_##client = sd_dhcp6_client_ref(client) |
| 35 |
|
| 36 |
static int client_start_transaction(sd_dhcp6_client *client, DHCP6State state); |
| 37 |
|
| 38 |
int sd_dhcp6_client_set_callback( |
| 39 |
sd_dhcp6_client *client, |
| 40 |
sd_dhcp6_client_callback_t cb, |
| 41 |
void *userdata) { |
| 42 |
|
| 43 |
assert_return(client, -EINVAL); |
| 44 |
|
| 45 |
client->callback = cb; |
| 46 |
client->userdata = userdata; |
| 47 |
|
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
int dhcp6_client_set_state_callback( |
| 52 |
sd_dhcp6_client *client, |
| 53 |
sd_dhcp6_client_callback_t cb, |
| 54 |
void *userdata) { |
| 55 |
|
| 56 |
assert_return(client, -EINVAL); |
| 57 |
|
| 58 |
client->state_callback = cb; |
| 59 |
client->state_userdata = userdata; |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
int sd_dhcp6_client_set_ifindex(sd_dhcp6_client *client, int ifindex) { |
| 65 |
assert_return(client, -EINVAL); |
| 66 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 67 |
assert_return(ifindex > 0, -EINVAL); |
| 68 |
|
| 69 |
client->ifindex = ifindex; |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
int sd_dhcp6_client_set_ifname(sd_dhcp6_client *client, const char *ifname) { |
| 74 |
assert_return(client, -EINVAL); |
| 75 |
assert_return(ifname, -EINVAL); |
| 76 |
|
| 77 |
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) |
| 78 |
return -EINVAL; |
| 79 |
|
| 80 |
return free_and_strdup(&client->ifname, ifname); |
| 81 |
} |
| 82 |
|
| 83 |
int sd_dhcp6_client_get_ifname(sd_dhcp6_client *client, const char **ret) { |
| 84 |
int r; |
| 85 |
|
| 86 |
assert_return(client, -EINVAL); |
| 87 |
|
| 88 |
r = get_ifname(client->ifindex, &client->ifname); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
|
| 92 |
if (ret) |
| 93 |
*ret = client->ifname; |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
int sd_dhcp6_client_set_local_address( |
| 99 |
sd_dhcp6_client *client, |
| 100 |
const struct in6_addr *local_address) { |
| 101 |
|
| 102 |
assert_return(client, -EINVAL); |
| 103 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 104 |
assert_return(local_address, -EINVAL); |
| 105 |
assert_return(in6_addr_is_link_local(local_address) > 0, -EINVAL); |
| 106 |
|
| 107 |
client->local_address = *local_address; |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
int sd_dhcp6_client_set_mac( |
| 113 |
sd_dhcp6_client *client, |
| 114 |
const uint8_t *addr, |
| 115 |
size_t addr_len, |
| 116 |
uint16_t arp_type) { |
| 117 |
|
| 118 |
assert_return(client, -EINVAL); |
| 119 |
assert_return(addr, -EINVAL); |
| 120 |
assert_return(addr_len <= sizeof(client->hw_addr.bytes), -EINVAL); |
| 121 |
|
| 122 |
/* Unlike the other setters, it is OK to set a new MAC address while the client is running, |
| 123 |
* as the MAC address is used only when setting DUID or IAID. */ |
| 124 |
|
| 125 |
if (arp_type == ARPHRD_ETHER) |
| 126 |
assert_return(addr_len == ETH_ALEN, -EINVAL); |
| 127 |
else if (arp_type == ARPHRD_INFINIBAND) |
| 128 |
assert_return(addr_len == INFINIBAND_ALEN, -EINVAL); |
| 129 |
else { |
| 130 |
client->arp_type = ARPHRD_NONE; |
| 131 |
client->hw_addr.length = 0; |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
client->arp_type = arp_type; |
| 136 |
hw_addr_set(&client->hw_addr, addr, addr_len); |
| 137 |
|
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
int sd_dhcp6_client_set_prefix_delegation_hint( |
| 142 |
sd_dhcp6_client *client, |
| 143 |
uint8_t prefixlen, |
| 144 |
const struct in6_addr *pd_prefix) { |
| 145 |
|
| 146 |
_cleanup_free_ DHCP6Address *prefix = NULL; |
| 147 |
|
| 148 |
assert_return(client, -EINVAL); |
| 149 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 150 |
|
| 151 |
if (!pd_prefix) { |
| 152 |
/* clear previous assignments. */ |
| 153 |
dhcp6_ia_clear_addresses(&client->ia_pd); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
assert_return(prefixlen > 0 && prefixlen <= 128, -EINVAL); |
| 158 |
|
| 159 |
prefix = new(DHCP6Address, 1); |
| 160 |
if (!prefix) |
| 161 |
return -ENOMEM; |
| 162 |
|
| 163 |
*prefix = (DHCP6Address) { |
| 164 |
.iapdprefix.address = *pd_prefix, |
| 165 |
.iapdprefix.prefixlen = prefixlen, |
| 166 |
}; |
| 167 |
|
| 168 |
LIST_PREPEND(addresses, client->ia_pd.addresses, TAKE_PTR(prefix)); |
| 169 |
return 1; |
| 170 |
} |
| 171 |
|
| 172 |
int sd_dhcp6_client_add_vendor_option(sd_dhcp6_client *client, sd_dhcp6_option *v) { |
| 173 |
int r; |
| 174 |
|
| 175 |
assert_return(client, -EINVAL); |
| 176 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 177 |
|
| 178 |
if (!v) { |
| 179 |
/* Clear the previous assignments. */ |
| 180 |
ordered_set_clear(client->vendor_options); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
r = ordered_set_ensure_put(&client->vendor_options, &dhcp6_option_hash_ops, v); |
| 185 |
if (r <= 0) |
| 186 |
return r; |
| 187 |
|
| 188 |
sd_dhcp6_option_ref(v); |
| 189 |
|
| 190 |
return r; |
| 191 |
} |
| 192 |
|
| 193 |
static int client_ensure_duid(sd_dhcp6_client *client) { |
| 194 |
assert(client); |
| 195 |
|
| 196 |
if (sd_dhcp_duid_is_set(&client->duid)) |
| 197 |
return 0; |
| 198 |
|
| 199 |
return sd_dhcp6_client_set_duid_en(client); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Sets DUID. If duid is non-null, the DUID is set to duid_type + duid |
| 204 |
* without further modification. Otherwise, if duid_type is supported, DUID |
| 205 |
* is set based on that type. Otherwise, an error is returned. |
| 206 |
*/ |
| 207 |
int sd_dhcp6_client_set_duid_llt(sd_dhcp6_client *client, uint64_t llt_time) { |
| 208 |
int r; |
| 209 |
|
| 210 |
assert_return(client, -EINVAL); |
| 211 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 212 |
|
| 213 |
r = sd_dhcp_duid_set_llt(&client->duid, client->hw_addr.bytes, client->hw_addr.length, client->arp_type, llt_time); |
| 214 |
if (r < 0) |
| 215 |
return log_dhcp6_client_errno(client, r, "Failed to set DUID-LLT: %m"); |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
int sd_dhcp6_client_set_duid_ll(sd_dhcp6_client *client) { |
| 221 |
int r; |
| 222 |
|
| 223 |
assert_return(client, -EINVAL); |
| 224 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 225 |
|
| 226 |
r = sd_dhcp_duid_set_ll(&client->duid, client->hw_addr.bytes, client->hw_addr.length, client->arp_type); |
| 227 |
if (r < 0) |
| 228 |
return log_dhcp6_client_errno(client, r, "Failed to set DUID-LL: %m"); |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
int sd_dhcp6_client_set_duid_en(sd_dhcp6_client *client) { |
| 234 |
int r; |
| 235 |
|
| 236 |
assert_return(client, -EINVAL); |
| 237 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 238 |
|
| 239 |
r = sd_dhcp_duid_set_en(&client->duid); |
| 240 |
if (r < 0) |
| 241 |
return log_dhcp6_client_errno(client, r, "Failed to set DUID-EN: %m"); |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
int sd_dhcp6_client_set_duid_uuid(sd_dhcp6_client *client) { |
| 247 |
int r; |
| 248 |
|
| 249 |
assert_return(client, -EINVAL); |
| 250 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 251 |
|
| 252 |
r = sd_dhcp_duid_set_uuid(&client->duid); |
| 253 |
if (r < 0) |
| 254 |
return log_dhcp6_client_errno(client, r, "Failed to set DUID-UUID: %m"); |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
int sd_dhcp6_client_set_duid_raw(sd_dhcp6_client *client, uint16_t duid_type, const uint8_t *duid, size_t duid_len) { |
| 260 |
int r; |
| 261 |
|
| 262 |
assert_return(client, -EINVAL); |
| 263 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 264 |
assert_return(duid || duid_len == 0, -EINVAL); |
| 265 |
|
| 266 |
r = sd_dhcp_duid_set(&client->duid, duid_type, duid, duid_len); |
| 267 |
if (r < 0) |
| 268 |
return log_dhcp6_client_errno(client, r, "Failed to set DUID: %m"); |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
int sd_dhcp6_client_set_duid(sd_dhcp6_client *client, const sd_dhcp_duid *duid) { |
| 274 |
assert_return(client, -EINVAL); |
| 275 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 276 |
assert_return(sd_dhcp_duid_is_set(duid), -EINVAL); |
| 277 |
|
| 278 |
client->duid = *duid; |
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
int sd_dhcp6_client_get_duid(sd_dhcp6_client *client, const sd_dhcp_duid **ret) { |
| 283 |
assert_return(client, -EINVAL); |
| 284 |
assert_return(ret, -EINVAL); |
| 285 |
|
| 286 |
if (!sd_dhcp_duid_is_set(&client->duid)) |
| 287 |
return -ENODATA; |
| 288 |
|
| 289 |
*ret = &client->duid; |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
int sd_dhcp6_client_get_duid_as_string(sd_dhcp6_client *client, char **ret) { |
| 294 |
assert_return(client, -EINVAL); |
| 295 |
assert_return(ret, -EINVAL); |
| 296 |
|
| 297 |
if (!sd_dhcp_duid_is_set(&client->duid)) |
| 298 |
return -ENODATA; |
| 299 |
|
| 300 |
return sd_dhcp_duid_to_string(&client->duid, ret); |
| 301 |
} |
| 302 |
|
| 303 |
int sd_dhcp6_client_set_iaid(sd_dhcp6_client *client, uint32_t iaid) { |
| 304 |
assert_return(client, -EINVAL); |
| 305 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 306 |
|
| 307 |
client->ia_na.header.id = htobe32(iaid); |
| 308 |
client->ia_pd.header.id = htobe32(iaid); |
| 309 |
client->iaid_set = true; |
| 310 |
|
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
static int client_ensure_iaid(sd_dhcp6_client *client) { |
| 315 |
int r; |
| 316 |
uint32_t iaid; |
| 317 |
|
| 318 |
assert(client); |
| 319 |
|
| 320 |
if (client->iaid_set) |
| 321 |
return 0; |
| 322 |
|
| 323 |
r = dhcp_identifier_set_iaid(client->dev, &client->hw_addr, |
| 324 |
/* legacy_unstable_byteorder= */ true, |
| 325 |
&iaid); |
| 326 |
if (r < 0) |
| 327 |
return r; |
| 328 |
|
| 329 |
client->ia_na.header.id = iaid; |
| 330 |
client->ia_pd.header.id = iaid; |
| 331 |
client->iaid_set = true; |
| 332 |
|
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
int sd_dhcp6_client_get_iaid(sd_dhcp6_client *client, uint32_t *iaid) { |
| 337 |
assert_return(client, -EINVAL); |
| 338 |
assert_return(iaid, -EINVAL); |
| 339 |
|
| 340 |
if (!client->iaid_set) |
| 341 |
return -ENODATA; |
| 342 |
|
| 343 |
*iaid = be32toh(client->ia_na.header.id); |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
int sd_dhcp6_client_set_fqdn( |
| 349 |
sd_dhcp6_client *client, |
| 350 |
const char *fqdn) { |
| 351 |
|
| 352 |
assert_return(client, -EINVAL); |
| 353 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 354 |
|
| 355 |
/* Make sure FQDN qualifies as DNS and as Linux hostname */ |
| 356 |
if (fqdn && |
| 357 |
!(hostname_is_valid(fqdn, 0) && dns_name_is_valid(fqdn) > 0)) |
| 358 |
return -EINVAL; |
| 359 |
|
| 360 |
return free_and_strdup(&client->fqdn, fqdn); |
| 361 |
} |
| 362 |
|
| 363 |
int sd_dhcp6_client_set_information_request(sd_dhcp6_client *client, int enabled) { |
| 364 |
assert_return(client, -EINVAL); |
| 365 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 366 |
|
| 367 |
client->information_request = enabled; |
| 368 |
|
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
int sd_dhcp6_client_get_information_request(sd_dhcp6_client *client, int *enabled) { |
| 373 |
assert_return(client, -EINVAL); |
| 374 |
assert_return(enabled, -EINVAL); |
| 375 |
|
| 376 |
*enabled = client->information_request; |
| 377 |
|
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
static int be16_compare_func(const be16_t *a, const be16_t *b) { |
| 382 |
assert(a); |
| 383 |
assert(b); |
| 384 |
|
| 385 |
return CMP(be16toh(*a), be16toh(*b)); |
| 386 |
} |
| 387 |
|
| 388 |
int sd_dhcp6_client_set_request_option(sd_dhcp6_client *client, uint16_t option) { |
| 389 |
be16_t opt; |
| 390 |
|
| 391 |
assert_return(client, -EINVAL); |
| 392 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 393 |
|
| 394 |
if (!dhcp6_option_can_request(option)) |
| 395 |
return -EINVAL; |
| 396 |
|
| 397 |
opt = htobe16(option); |
| 398 |
if (typesafe_bsearch(&opt, client->req_opts, client->n_req_opts, be16_compare_func)) |
| 399 |
return -EEXIST; |
| 400 |
|
| 401 |
if (!GREEDY_REALLOC(client->req_opts, client->n_req_opts + 1)) |
| 402 |
return -ENOMEM; |
| 403 |
|
| 404 |
client->req_opts[client->n_req_opts++] = opt; |
| 405 |
|
| 406 |
/* Sort immediately to make the above binary search will work for the next time. */ |
| 407 |
typesafe_qsort(client->req_opts, client->n_req_opts, be16_compare_func); |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
int sd_dhcp6_client_set_request_mud_url(sd_dhcp6_client *client, const char *mudurl) { |
| 412 |
assert_return(client, -EINVAL); |
| 413 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 414 |
assert_return(mudurl, -EINVAL); |
| 415 |
assert_return(strlen(mudurl) <= UINT8_MAX, -EINVAL); |
| 416 |
assert_return(http_url_is_valid(mudurl), -EINVAL); |
| 417 |
|
| 418 |
return free_and_strdup(&client->mudurl, mudurl); |
| 419 |
} |
| 420 |
|
| 421 |
int sd_dhcp6_client_set_request_user_class(sd_dhcp6_client *client, char * const *user_class) { |
| 422 |
char **s; |
| 423 |
|
| 424 |
assert_return(client, -EINVAL); |
| 425 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 426 |
assert_return(!strv_isempty(user_class), -EINVAL); |
| 427 |
|
| 428 |
STRV_FOREACH(p, user_class) { |
| 429 |
size_t len = strlen(*p); |
| 430 |
|
| 431 |
if (len > UINT16_MAX || len == 0) |
| 432 |
return -EINVAL; |
| 433 |
} |
| 434 |
|
| 435 |
s = strv_copy(user_class); |
| 436 |
if (!s) |
| 437 |
return -ENOMEM; |
| 438 |
|
| 439 |
return strv_free_and_replace(client->user_class, s); |
| 440 |
} |
| 441 |
|
| 442 |
int sd_dhcp6_client_set_request_vendor_class(sd_dhcp6_client *client, char * const *vendor_class) { |
| 443 |
char **s; |
| 444 |
|
| 445 |
assert_return(client, -EINVAL); |
| 446 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 447 |
assert_return(!strv_isempty(vendor_class), -EINVAL); |
| 448 |
|
| 449 |
STRV_FOREACH(p, vendor_class) { |
| 450 |
size_t len = strlen(*p); |
| 451 |
|
| 452 |
if (len > UINT16_MAX || len == 0) |
| 453 |
return -EINVAL; |
| 454 |
} |
| 455 |
|
| 456 |
s = strv_copy(vendor_class); |
| 457 |
if (!s) |
| 458 |
return -ENOMEM; |
| 459 |
|
| 460 |
return strv_free_and_replace(client->vendor_class, s); |
| 461 |
} |
| 462 |
|
| 463 |
int sd_dhcp6_client_get_prefix_delegation(sd_dhcp6_client *client, int *delegation) { |
| 464 |
assert_return(client, -EINVAL); |
| 465 |
assert_return(delegation, -EINVAL); |
| 466 |
|
| 467 |
*delegation = FLAGS_SET(client->request_ia, DHCP6_REQUEST_IA_PD); |
| 468 |
|
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
int sd_dhcp6_client_set_prefix_delegation(sd_dhcp6_client *client, int delegation) { |
| 473 |
assert_return(client, -EINVAL); |
| 474 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 475 |
|
| 476 |
SET_FLAG(client->request_ia, DHCP6_REQUEST_IA_PD, delegation); |
| 477 |
|
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
int sd_dhcp6_client_get_address_request(sd_dhcp6_client *client, int *request) { |
| 482 |
assert_return(client, -EINVAL); |
| 483 |
assert_return(request, -EINVAL); |
| 484 |
|
| 485 |
*request = FLAGS_SET(client->request_ia, DHCP6_REQUEST_IA_NA); |
| 486 |
|
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
int sd_dhcp6_client_set_address_request(sd_dhcp6_client *client, int request) { |
| 491 |
assert_return(client, -EINVAL); |
| 492 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 493 |
|
| 494 |
SET_FLAG(client->request_ia, DHCP6_REQUEST_IA_NA, request); |
| 495 |
|
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
int dhcp6_client_set_transaction_id(sd_dhcp6_client *client, uint32_t transaction_id) { |
| 500 |
assert(client); |
| 501 |
assert_se(network_test_mode_enabled()); |
| 502 |
|
| 503 |
/* This is for tests or fuzzers. */ |
| 504 |
|
| 505 |
client->transaction_id = transaction_id & htobe32(0x00ffffff); |
| 506 |
|
| 507 |
return 0; |
| 508 |
} |
| 509 |
|
| 510 |
int sd_dhcp6_client_set_rapid_commit(sd_dhcp6_client *client, int enable) { |
| 511 |
assert_return(client, -EINVAL); |
| 512 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 513 |
|
| 514 |
client->rapid_commit = enable; |
| 515 |
return 0; |
| 516 |
} |
| 517 |
|
| 518 |
int sd_dhcp6_client_set_send_release(sd_dhcp6_client *client, int enable) { |
| 519 |
assert_return(client, -EINVAL); |
| 520 |
|
| 521 |
client->send_release = enable; |
| 522 |
return 0; |
| 523 |
} |
| 524 |
|
| 525 |
int sd_dhcp6_client_get_lease(sd_dhcp6_client *client, sd_dhcp6_lease **ret) { |
| 526 |
assert_return(client, -EINVAL); |
| 527 |
|
| 528 |
if (!client->lease) |
| 529 |
return -ENOMSG; |
| 530 |
|
| 531 |
if (ret) |
| 532 |
*ret = client->lease; |
| 533 |
|
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
int sd_dhcp6_client_add_option(sd_dhcp6_client *client, sd_dhcp6_option *v) { |
| 538 |
int r; |
| 539 |
|
| 540 |
assert_return(client, -EINVAL); |
| 541 |
assert_return(v, -EINVAL); |
| 542 |
|
| 543 |
r = ordered_hashmap_ensure_put(&client->extra_options, &dhcp6_option_hash_ops, UINT_TO_PTR(v->option), v); |
| 544 |
if (r <= 0) |
| 545 |
return r; |
| 546 |
|
| 547 |
sd_dhcp6_option_ref(v); |
| 548 |
return r; |
| 549 |
} |
| 550 |
|
| 551 |
static void client_set_state(sd_dhcp6_client *client, DHCP6State state) { |
| 552 |
assert(client); |
| 553 |
|
| 554 |
if (client->state == state) |
| 555 |
return; |
| 556 |
|
| 557 |
log_dhcp6_client(client, "State changed: %s -> %s", |
| 558 |
dhcp6_state_to_string(client->state), dhcp6_state_to_string(state)); |
| 559 |
|
| 560 |
client->state = state; |
| 561 |
|
| 562 |
if (client->state_callback) |
| 563 |
client->state_callback(client, state, client->state_userdata); |
| 564 |
} |
| 565 |
|
| 566 |
int dhcp6_client_get_state(sd_dhcp6_client *client) { |
| 567 |
assert_return(client, -EINVAL); |
| 568 |
|
| 569 |
return client->state; |
| 570 |
} |
| 571 |
|
| 572 |
static void client_notify(sd_dhcp6_client *client, int event) { |
| 573 |
assert(client); |
| 574 |
|
| 575 |
if (client->callback) |
| 576 |
client->callback(client, event, client->userdata); |
| 577 |
} |
| 578 |
|
| 579 |
static void client_cleanup(sd_dhcp6_client *client) { |
| 580 |
assert(client); |
| 581 |
|
| 582 |
client->lease = sd_dhcp6_lease_unref(client->lease); |
| 583 |
|
| 584 |
/* Reset IRT here. Otherwise, we cannot restart the client in the information requesting mode, |
| 585 |
* even though the lease is freed below. */ |
| 586 |
client->information_request_time_usec = 0; |
| 587 |
client->information_refresh_time_usec = 0; |
| 588 |
|
| 589 |
(void) event_source_disable(client->receive_message); |
| 590 |
(void) event_source_disable(client->timeout_resend); |
| 591 |
(void) event_source_disable(client->timeout_expire); |
| 592 |
(void) event_source_disable(client->timeout_t1); |
| 593 |
(void) event_source_disable(client->timeout_t2); |
| 594 |
|
| 595 |
client_set_state(client, DHCP6_STATE_STOPPED); |
| 596 |
} |
| 597 |
|
| 598 |
static void client_stop(sd_dhcp6_client *client, int error) { |
| 599 |
DHCP6_CLIENT_DONT_DESTROY(client); |
| 600 |
|
| 601 |
assert(client); |
| 602 |
|
| 603 |
client_notify(client, error); |
| 604 |
|
| 605 |
client_cleanup(client); |
| 606 |
} |
| 607 |
|
| 608 |
static int client_append_common_options_in_managed_mode( |
| 609 |
sd_dhcp6_client *client, |
| 610 |
uint8_t **buf, |
| 611 |
size_t *offset, |
| 612 |
const DHCP6IA *ia_na, |
| 613 |
const DHCP6IA *ia_pd) { |
| 614 |
|
| 615 |
int r; |
| 616 |
|
| 617 |
assert(client); |
| 618 |
assert(IN_SET(client->state, |
| 619 |
DHCP6_STATE_SOLICITATION, |
| 620 |
DHCP6_STATE_REQUEST, |
| 621 |
DHCP6_STATE_RENEW, |
| 622 |
DHCP6_STATE_REBIND, |
| 623 |
DHCP6_STATE_STOPPING)); |
| 624 |
assert(buf); |
| 625 |
assert(*buf); |
| 626 |
assert(offset); |
| 627 |
|
| 628 |
if (FLAGS_SET(client->request_ia, DHCP6_REQUEST_IA_NA) && ia_na) { |
| 629 |
r = dhcp6_option_append_ia(buf, offset, ia_na); |
| 630 |
if (r < 0) |
| 631 |
return r; |
| 632 |
} |
| 633 |
|
| 634 |
if (FLAGS_SET(client->request_ia, DHCP6_REQUEST_IA_PD) && ia_pd) { |
| 635 |
r = dhcp6_option_append_ia(buf, offset, ia_pd); |
| 636 |
if (r < 0) |
| 637 |
return r; |
| 638 |
} |
| 639 |
|
| 640 |
if (client->state != DHCP6_STATE_STOPPING) { |
| 641 |
r = dhcp6_option_append_fqdn(buf, offset, client->fqdn); |
| 642 |
if (r < 0) |
| 643 |
return r; |
| 644 |
} |
| 645 |
|
| 646 |
r = dhcp6_option_append_user_class(buf, offset, client->user_class); |
| 647 |
if (r < 0) |
| 648 |
return r; |
| 649 |
|
| 650 |
r = dhcp6_option_append_vendor_class(buf, offset, client->vendor_class); |
| 651 |
if (r < 0) |
| 652 |
return r; |
| 653 |
|
| 654 |
r = dhcp6_option_append_vendor_option(buf, offset, client->vendor_options); |
| 655 |
if (r < 0) |
| 656 |
return r; |
| 657 |
|
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
static DHCP6MessageType client_message_type_from_state(sd_dhcp6_client *client) { |
| 662 |
assert(client); |
| 663 |
|
| 664 |
switch (client->state) { |
| 665 |
case DHCP6_STATE_INFORMATION_REQUEST: |
| 666 |
return DHCP6_MESSAGE_INFORMATION_REQUEST; |
| 667 |
case DHCP6_STATE_SOLICITATION: |
| 668 |
return DHCP6_MESSAGE_SOLICIT; |
| 669 |
case DHCP6_STATE_REQUEST: |
| 670 |
return DHCP6_MESSAGE_REQUEST; |
| 671 |
case DHCP6_STATE_RENEW: |
| 672 |
return DHCP6_MESSAGE_RENEW; |
| 673 |
case DHCP6_STATE_REBIND: |
| 674 |
return DHCP6_MESSAGE_REBIND; |
| 675 |
case DHCP6_STATE_STOPPING: |
| 676 |
return DHCP6_MESSAGE_RELEASE; |
| 677 |
default: |
| 678 |
assert_not_reached(); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
static int client_append_oro(sd_dhcp6_client *client, uint8_t **buf, size_t *offset) { |
| 683 |
_cleanup_free_ be16_t *p = NULL; |
| 684 |
be16_t *req_opts; |
| 685 |
size_t n; |
| 686 |
|
| 687 |
assert(client); |
| 688 |
assert(buf); |
| 689 |
assert(*buf); |
| 690 |
assert(offset); |
| 691 |
|
| 692 |
switch (client->state) { |
| 693 |
case DHCP6_STATE_INFORMATION_REQUEST: |
| 694 |
n = client->n_req_opts; |
| 695 |
p = new(be16_t, n + 2); |
| 696 |
if (!p) |
| 697 |
return -ENOMEM; |
| 698 |
|
| 699 |
memcpy_safe(p, client->req_opts, n * sizeof(be16_t)); |
| 700 |
p[n++] = htobe16(SD_DHCP6_OPTION_INFORMATION_REFRESH_TIME); /* RFC 8415 section 21.23 */ |
| 701 |
p[n++] = htobe16(SD_DHCP6_OPTION_INF_MAX_RT); /* RFC 8415 section 21.25 */ |
| 702 |
|
| 703 |
typesafe_qsort(p, n, be16_compare_func); |
| 704 |
req_opts = p; |
| 705 |
break; |
| 706 |
|
| 707 |
case DHCP6_STATE_SOLICITATION: |
| 708 |
n = client->n_req_opts; |
| 709 |
p = new(be16_t, n + 1); |
| 710 |
if (!p) |
| 711 |
return -ENOMEM; |
| 712 |
|
| 713 |
memcpy_safe(p, client->req_opts, n * sizeof(be16_t)); |
| 714 |
p[n++] = htobe16(SD_DHCP6_OPTION_SOL_MAX_RT); /* RFC 8415 section 21.24 */ |
| 715 |
|
| 716 |
typesafe_qsort(p, n, be16_compare_func); |
| 717 |
req_opts = p; |
| 718 |
break; |
| 719 |
|
| 720 |
case DHCP6_STATE_STOPPING: |
| 721 |
return 0; |
| 722 |
|
| 723 |
default: |
| 724 |
n = client->n_req_opts; |
| 725 |
req_opts = client->req_opts; |
| 726 |
} |
| 727 |
|
| 728 |
if (n == 0) |
| 729 |
return 0; |
| 730 |
|
| 731 |
return dhcp6_option_append(buf, offset, SD_DHCP6_OPTION_ORO, n * sizeof(be16_t), req_opts); |
| 732 |
} |
| 733 |
|
| 734 |
static int client_append_mudurl(sd_dhcp6_client *client, uint8_t **buf, size_t *offset) { |
| 735 |
assert(client); |
| 736 |
assert(buf); |
| 737 |
assert(*buf); |
| 738 |
assert(offset); |
| 739 |
|
| 740 |
if (!client->mudurl) |
| 741 |
return 0; |
| 742 |
|
| 743 |
if (client->state == DHCP6_STATE_STOPPING) |
| 744 |
return 0; |
| 745 |
|
| 746 |
return dhcp6_option_append(buf, offset, SD_DHCP6_OPTION_MUD_URL_V6, |
| 747 |
strlen(client->mudurl), client->mudurl); |
| 748 |
} |
| 749 |
|
| 750 |
int dhcp6_client_send_message(sd_dhcp6_client *client) { |
| 751 |
_cleanup_free_ uint8_t *buf = NULL; |
| 752 |
struct sd_dhcp6_option *j; |
| 753 |
usec_t elapsed_usec, time_now; |
| 754 |
be16_t elapsed_time; |
| 755 |
DHCP6Message *message; |
| 756 |
size_t offset; |
| 757 |
int r; |
| 758 |
|
| 759 |
assert(client); |
| 760 |
assert(client->event); |
| 761 |
|
| 762 |
r = sd_event_now(client->event, CLOCK_BOOTTIME, &time_now); |
| 763 |
if (r < 0) |
| 764 |
return r; |
| 765 |
|
| 766 |
if (!GREEDY_REALLOC0(buf, offsetof(DHCP6Message, options))) |
| 767 |
return -ENOMEM; |
| 768 |
|
| 769 |
message = (DHCP6Message*) buf; |
| 770 |
message->transaction_id = client->transaction_id; |
| 771 |
message->type = client_message_type_from_state(client); |
| 772 |
offset = offsetof(DHCP6Message, options); |
| 773 |
|
| 774 |
switch (client->state) { |
| 775 |
case DHCP6_STATE_INFORMATION_REQUEST: |
| 776 |
break; |
| 777 |
|
| 778 |
case DHCP6_STATE_SOLICITATION: |
| 779 |
if (client->rapid_commit) { |
| 780 |
r = dhcp6_option_append(&buf, &offset, SD_DHCP6_OPTION_RAPID_COMMIT, 0, NULL); |
| 781 |
if (r < 0) |
| 782 |
return r; |
| 783 |
} |
| 784 |
|
| 785 |
r = client_append_common_options_in_managed_mode(client, &buf, &offset, |
| 786 |
&client->ia_na, &client->ia_pd); |
| 787 |
if (r < 0) |
| 788 |
return r; |
| 789 |
break; |
| 790 |
|
| 791 |
case DHCP6_STATE_REQUEST: |
| 792 |
case DHCP6_STATE_RENEW: |
| 793 |
case DHCP6_STATE_STOPPING: |
| 794 |
r = dhcp6_option_append(&buf, &offset, SD_DHCP6_OPTION_SERVERID, |
| 795 |
client->lease->serverid_len, |
| 796 |
client->lease->serverid); |
| 797 |
if (r < 0) |
| 798 |
return r; |
| 799 |
|
| 800 |
_fallthrough_; |
| 801 |
case DHCP6_STATE_REBIND: |
| 802 |
|
| 803 |
assert(client->lease); |
| 804 |
|
| 805 |
r = client_append_common_options_in_managed_mode(client, &buf, &offset, |
| 806 |
client->lease->ia_na, client->lease->ia_pd); |
| 807 |
if (r < 0) |
| 808 |
return r; |
| 809 |
break; |
| 810 |
|
| 811 |
case DHCP6_STATE_BOUND: |
| 812 |
case DHCP6_STATE_STOPPED: |
| 813 |
default: |
| 814 |
assert_not_reached(); |
| 815 |
} |
| 816 |
|
| 817 |
r = client_append_mudurl(client, &buf, &offset); |
| 818 |
if (r < 0) |
| 819 |
return r; |
| 820 |
|
| 821 |
r = client_append_oro(client, &buf, &offset); |
| 822 |
if (r < 0) |
| 823 |
return r; |
| 824 |
|
| 825 |
assert(sd_dhcp_duid_is_set(&client->duid)); |
| 826 |
r = dhcp6_option_append(&buf, &offset, SD_DHCP6_OPTION_CLIENTID, |
| 827 |
client->duid.size, &client->duid.duid); |
| 828 |
if (r < 0) |
| 829 |
return r; |
| 830 |
|
| 831 |
ORDERED_HASHMAP_FOREACH(j, client->extra_options) { |
| 832 |
r = dhcp6_option_append(&buf, &offset, j->option, j->length, j->data); |
| 833 |
if (r < 0) |
| 834 |
return r; |
| 835 |
} |
| 836 |
|
| 837 |
/* RFC 8415 Section 21.9. |
| 838 |
* A client MUST include an Elapsed Time option in messages to indicate how long the client has |
| 839 |
* been trying to complete a DHCP message exchange. */ |
| 840 |
elapsed_usec = MIN(usec_sub_unsigned(time_now, client->transaction_start) / USEC_PER_MSEC / 10, (usec_t) UINT16_MAX); |
| 841 |
elapsed_time = htobe16(elapsed_usec); |
| 842 |
r = dhcp6_option_append(&buf, &offset, SD_DHCP6_OPTION_ELAPSED_TIME, sizeof(elapsed_time), &elapsed_time); |
| 843 |
if (r < 0) |
| 844 |
return r; |
| 845 |
|
| 846 |
r = dhcp6_network_send_udp_socket(client->fd, &IN6_ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS, buf, offset); |
| 847 |
if (r < 0) |
| 848 |
return r; |
| 849 |
|
| 850 |
log_dhcp6_client(client, "Sent %s", |
| 851 |
dhcp6_message_type_to_string(client_message_type_from_state(client))); |
| 852 |
return 0; |
| 853 |
} |
| 854 |
|
| 855 |
static usec_t client_timeout_compute_random(usec_t val) { |
| 856 |
return usec_sub_unsigned(val, random_u64_range(val / 10)); |
| 857 |
} |
| 858 |
|
| 859 |
static int client_timeout_resend(sd_event_source *s, uint64_t usec, void *userdata) { |
| 860 |
sd_dhcp6_client *client = ASSERT_PTR(userdata); |
| 861 |
usec_t init_retransmit_time, max_retransmit_time; |
| 862 |
int r; |
| 863 |
|
| 864 |
assert(client->event); |
| 865 |
|
| 866 |
switch (client->state) { |
| 867 |
case DHCP6_STATE_INFORMATION_REQUEST: |
| 868 |
init_retransmit_time = DHCP6_INF_TIMEOUT; |
| 869 |
max_retransmit_time = DHCP6_INF_MAX_RT; |
| 870 |
break; |
| 871 |
|
| 872 |
case DHCP6_STATE_SOLICITATION: |
| 873 |
|
| 874 |
if (client->retransmit_count > 0 && client->lease) { |
| 875 |
(void) client_start_transaction(client, DHCP6_STATE_REQUEST); |
| 876 |
return 0; |
| 877 |
} |
| 878 |
|
| 879 |
init_retransmit_time = DHCP6_SOL_TIMEOUT; |
| 880 |
max_retransmit_time = DHCP6_SOL_MAX_RT; |
| 881 |
break; |
| 882 |
|
| 883 |
case DHCP6_STATE_REQUEST: |
| 884 |
|
| 885 |
if (client->retransmit_count >= DHCP6_REQ_MAX_RC) { |
| 886 |
client_stop(client, SD_DHCP6_CLIENT_EVENT_RETRANS_MAX); |
| 887 |
return 0; |
| 888 |
} |
| 889 |
|
| 890 |
init_retransmit_time = DHCP6_REQ_TIMEOUT; |
| 891 |
max_retransmit_time = DHCP6_REQ_MAX_RT; |
| 892 |
break; |
| 893 |
|
| 894 |
case DHCP6_STATE_RENEW: |
| 895 |
init_retransmit_time = DHCP6_REN_TIMEOUT; |
| 896 |
max_retransmit_time = DHCP6_REN_MAX_RT; |
| 897 |
|
| 898 |
/* RFC 3315, section 18.1.3. says max retransmit duration will |
| 899 |
be the remaining time until T2. Instead of setting MRD, |
| 900 |
wait for T2 to trigger with the same end result */ |
| 901 |
break; |
| 902 |
|
| 903 |
case DHCP6_STATE_REBIND: |
| 904 |
init_retransmit_time = DHCP6_REB_TIMEOUT; |
| 905 |
max_retransmit_time = DHCP6_REB_MAX_RT; |
| 906 |
|
| 907 |
/* Also, instead of setting MRD, the expire timer is already set in client_enter_bound_state(). */ |
| 908 |
break; |
| 909 |
|
| 910 |
case DHCP6_STATE_STOPPED: |
| 911 |
case DHCP6_STATE_STOPPING: |
| 912 |
case DHCP6_STATE_BOUND: |
| 913 |
default: |
| 914 |
assert_not_reached(); |
| 915 |
} |
| 916 |
|
| 917 |
r = dhcp6_client_send_message(client); |
| 918 |
if (r >= 0) |
| 919 |
client->retransmit_count++; |
| 920 |
|
| 921 |
if (client->retransmit_time == 0) { |
| 922 |
client->retransmit_time = client_timeout_compute_random(init_retransmit_time); |
| 923 |
|
| 924 |
if (client->state == DHCP6_STATE_SOLICITATION) |
| 925 |
client->retransmit_time += init_retransmit_time / 10; |
| 926 |
|
| 927 |
} else if (client->retransmit_time > max_retransmit_time / 2) |
| 928 |
client->retransmit_time = client_timeout_compute_random(max_retransmit_time); |
| 929 |
else |
| 930 |
client->retransmit_time += client_timeout_compute_random(client->retransmit_time); |
| 931 |
|
| 932 |
log_dhcp6_client(client, "Next retransmission in %s", |
| 933 |
FORMAT_TIMESPAN(client->retransmit_time, USEC_PER_SEC)); |
| 934 |
|
| 935 |
r = event_reset_time_relative(client->event, &client->timeout_resend, |
| 936 |
CLOCK_BOOTTIME, |
| 937 |
client->retransmit_time, 10 * USEC_PER_MSEC, |
| 938 |
client_timeout_resend, client, |
| 939 |
client->event_priority, "dhcp6-resend-timer", true); |
| 940 |
if (r < 0) |
| 941 |
client_stop(client, r); |
| 942 |
|
| 943 |
return 0; |
| 944 |
} |
| 945 |
|
| 946 |
static int client_start_transaction(sd_dhcp6_client *client, DHCP6State state) { |
| 947 |
int r; |
| 948 |
|
| 949 |
assert(client); |
| 950 |
assert(client->event); |
| 951 |
|
| 952 |
switch (state) { |
| 953 |
case DHCP6_STATE_INFORMATION_REQUEST: |
| 954 |
case DHCP6_STATE_SOLICITATION: |
| 955 |
assert(client->state == DHCP6_STATE_STOPPED); |
| 956 |
break; |
| 957 |
case DHCP6_STATE_REQUEST: |
| 958 |
assert(client->state == DHCP6_STATE_SOLICITATION); |
| 959 |
break; |
| 960 |
case DHCP6_STATE_RENEW: |
| 961 |
assert(client->state == DHCP6_STATE_BOUND); |
| 962 |
break; |
| 963 |
case DHCP6_STATE_REBIND: |
| 964 |
assert(IN_SET(client->state, DHCP6_STATE_BOUND, DHCP6_STATE_RENEW)); |
| 965 |
break; |
| 966 |
case DHCP6_STATE_STOPPED: |
| 967 |
case DHCP6_STATE_STOPPING: |
| 968 |
case DHCP6_STATE_BOUND: |
| 969 |
default: |
| 970 |
assert_not_reached(); |
| 971 |
} |
| 972 |
|
| 973 |
client_set_state(client, state); |
| 974 |
|
| 975 |
client->retransmit_time = 0; |
| 976 |
client->retransmit_count = 0; |
| 977 |
client->transaction_id = random_u32() & htobe32(0x00ffffff); |
| 978 |
|
| 979 |
r = sd_event_now(client->event, CLOCK_BOOTTIME, &client->transaction_start); |
| 980 |
if (r < 0) |
| 981 |
goto error; |
| 982 |
|
| 983 |
r = event_reset_time(client->event, &client->timeout_resend, |
| 984 |
CLOCK_BOOTTIME, |
| 985 |
0, 0, |
| 986 |
client_timeout_resend, client, |
| 987 |
client->event_priority, "dhcp6-resend-timeout", true); |
| 988 |
if (r < 0) |
| 989 |
goto error; |
| 990 |
|
| 991 |
r = sd_event_source_set_enabled(client->receive_message, SD_EVENT_ON); |
| 992 |
if (r < 0) |
| 993 |
goto error; |
| 994 |
|
| 995 |
return 0; |
| 996 |
|
| 997 |
error: |
| 998 |
client_stop(client, r); |
| 999 |
return r; |
| 1000 |
} |
| 1001 |
|
| 1002 |
static int client_timeout_expire(sd_event_source *s, uint64_t usec, void *userdata) { |
| 1003 |
sd_dhcp6_client *client = ASSERT_PTR(userdata); |
| 1004 |
DHCP6_CLIENT_DONT_DESTROY(client); |
| 1005 |
DHCP6State state; |
| 1006 |
|
| 1007 |
(void) event_source_disable(client->timeout_expire); |
| 1008 |
(void) event_source_disable(client->timeout_t2); |
| 1009 |
(void) event_source_disable(client->timeout_t1); |
| 1010 |
|
| 1011 |
state = client->state; |
| 1012 |
|
| 1013 |
client_stop(client, SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE); |
| 1014 |
|
| 1015 |
/* RFC 3315, section 18.1.4., says that "...the client may choose to |
| 1016 |
use a Solicit message to locate a new DHCP server..." */ |
| 1017 |
if (state == DHCP6_STATE_REBIND) |
| 1018 |
(void) client_start_transaction(client, DHCP6_STATE_SOLICITATION); |
| 1019 |
|
| 1020 |
return 0; |
| 1021 |
} |
| 1022 |
|
| 1023 |
static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) { |
| 1024 |
sd_dhcp6_client *client = ASSERT_PTR(userdata); |
| 1025 |
|
| 1026 |
(void) event_source_disable(client->timeout_t2); |
| 1027 |
(void) event_source_disable(client->timeout_t1); |
| 1028 |
|
| 1029 |
log_dhcp6_client(client, "Timeout T2"); |
| 1030 |
|
| 1031 |
(void) client_start_transaction(client, DHCP6_STATE_REBIND); |
| 1032 |
|
| 1033 |
return 0; |
| 1034 |
} |
| 1035 |
|
| 1036 |
static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata) { |
| 1037 |
sd_dhcp6_client *client = ASSERT_PTR(userdata); |
| 1038 |
|
| 1039 |
(void) event_source_disable(client->timeout_t1); |
| 1040 |
|
| 1041 |
log_dhcp6_client(client, "Timeout T1"); |
| 1042 |
|
| 1043 |
(void) client_start_transaction(client, DHCP6_STATE_RENEW); |
| 1044 |
|
| 1045 |
return 0; |
| 1046 |
} |
| 1047 |
|
| 1048 |
static int client_enter_bound_state(sd_dhcp6_client *client) { |
| 1049 |
usec_t lifetime_t1, lifetime_t2, lifetime_valid; |
| 1050 |
int r; |
| 1051 |
|
| 1052 |
assert(client); |
| 1053 |
assert(client->lease); |
| 1054 |
assert(IN_SET(client->state, |
| 1055 |
DHCP6_STATE_SOLICITATION, |
| 1056 |
DHCP6_STATE_REQUEST, |
| 1057 |
DHCP6_STATE_RENEW, |
| 1058 |
DHCP6_STATE_REBIND)); |
| 1059 |
|
| 1060 |
(void) event_source_disable(client->receive_message); |
| 1061 |
(void) event_source_disable(client->timeout_resend); |
| 1062 |
|
| 1063 |
r = sd_dhcp6_lease_get_t1(client->lease, &lifetime_t1); |
| 1064 |
if (r < 0) |
| 1065 |
goto error; |
| 1066 |
|
| 1067 |
r = sd_dhcp6_lease_get_t2(client->lease, &lifetime_t2); |
| 1068 |
if (r < 0) |
| 1069 |
goto error; |
| 1070 |
|
| 1071 |
r = sd_dhcp6_lease_get_valid_lifetime(client->lease, &lifetime_valid); |
| 1072 |
if (r < 0) |
| 1073 |
goto error; |
| 1074 |
|
| 1075 |
lifetime_t2 = client_timeout_compute_random(lifetime_t2); |
| 1076 |
lifetime_t1 = client_timeout_compute_random(MIN(lifetime_t1, lifetime_t2)); |
| 1077 |
|
| 1078 |
if (lifetime_t1 == USEC_INFINITY) { |
| 1079 |
log_dhcp6_client(client, "Infinite T1"); |
| 1080 |
event_source_disable(client->timeout_t1); |
| 1081 |
} else { |
| 1082 |
log_dhcp6_client(client, "T1 expires in %s", FORMAT_TIMESPAN(lifetime_t1, USEC_PER_SEC)); |
| 1083 |
r = event_reset_time_relative(client->event, &client->timeout_t1, |
| 1084 |
CLOCK_BOOTTIME, |
| 1085 |
lifetime_t1, 10 * USEC_PER_SEC, |
| 1086 |
client_timeout_t1, client, |
| 1087 |
client->event_priority, "dhcp6-t1-timeout", true); |
| 1088 |
if (r < 0) |
| 1089 |
goto error; |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (lifetime_t2 == USEC_INFINITY) { |
| 1093 |
log_dhcp6_client(client, "Infinite T2"); |
| 1094 |
event_source_disable(client->timeout_t2); |
| 1095 |
} else { |
| 1096 |
log_dhcp6_client(client, "T2 expires in %s", FORMAT_TIMESPAN(lifetime_t2, USEC_PER_SEC)); |
| 1097 |
r = event_reset_time_relative(client->event, &client->timeout_t2, |
| 1098 |
CLOCK_BOOTTIME, |
| 1099 |
lifetime_t2, 10 * USEC_PER_SEC, |
| 1100 |
client_timeout_t2, client, |
| 1101 |
client->event_priority, "dhcp6-t2-timeout", true); |
| 1102 |
if (r < 0) |
| 1103 |
goto error; |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (lifetime_valid == USEC_INFINITY) { |
| 1107 |
log_dhcp6_client(client, "Infinite valid lifetime"); |
| 1108 |
event_source_disable(client->timeout_expire); |
| 1109 |
} else { |
| 1110 |
log_dhcp6_client(client, "Valid lifetime expires in %s", FORMAT_TIMESPAN(lifetime_valid, USEC_PER_SEC)); |
| 1111 |
|
| 1112 |
r = event_reset_time_relative(client->event, &client->timeout_expire, |
| 1113 |
CLOCK_BOOTTIME, |
| 1114 |
lifetime_valid, USEC_PER_SEC, |
| 1115 |
client_timeout_expire, client, |
| 1116 |
client->event_priority, "dhcp6-lease-expire", true); |
| 1117 |
if (r < 0) |
| 1118 |
goto error; |
| 1119 |
} |
| 1120 |
|
| 1121 |
client_set_state(client, DHCP6_STATE_BOUND); |
| 1122 |
client_notify(client, SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE); |
| 1123 |
return 0; |
| 1124 |
|
| 1125 |
error: |
| 1126 |
client_stop(client, r); |
| 1127 |
return r; |
| 1128 |
} |
| 1129 |
|
| 1130 |
static int log_invalid_message_type(sd_dhcp6_client *client, const DHCP6Message *message) { |
| 1131 |
const char *type_str; |
| 1132 |
|
| 1133 |
assert(client); |
| 1134 |
assert(message); |
| 1135 |
|
| 1136 |
type_str = dhcp6_message_type_to_string(message->type); |
| 1137 |
if (type_str) |
| 1138 |
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), |
| 1139 |
"Received unexpected %s message, ignoring.", type_str); |
| 1140 |
else |
| 1141 |
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), |
| 1142 |
"Received unsupported message type %u, ignoring.", message->type); |
| 1143 |
} |
| 1144 |
|
| 1145 |
static int client_process_information( |
| 1146 |
sd_dhcp6_client *client, |
| 1147 |
DHCP6Message *message, |
| 1148 |
size_t len, |
| 1149 |
const triple_timestamp *timestamp, |
| 1150 |
const struct in6_addr *server_address) { |
| 1151 |
|
| 1152 |
_cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL; |
| 1153 |
int r; |
| 1154 |
|
| 1155 |
assert(client); |
| 1156 |
assert(message); |
| 1157 |
|
| 1158 |
if (message->type != DHCP6_MESSAGE_REPLY) |
| 1159 |
return log_invalid_message_type(client, message); |
| 1160 |
|
| 1161 |
r = dhcp6_lease_new_from_message(client, message, len, timestamp, server_address, &lease); |
| 1162 |
if (r < 0) |
| 1163 |
return log_dhcp6_client_errno(client, r, "Failed to process received reply message, ignoring: %m"); |
| 1164 |
|
| 1165 |
log_dhcp6_client(client, "Processed %s message", dhcp6_message_type_to_string(message->type)); |
| 1166 |
|
| 1167 |
sd_dhcp6_lease_unref(client->lease); |
| 1168 |
client->lease = TAKE_PTR(lease); |
| 1169 |
|
| 1170 |
/* Do not call client_stop() here, as it frees the acquired lease. */ |
| 1171 |
(void) event_source_disable(client->receive_message); |
| 1172 |
(void) event_source_disable(client->timeout_resend); |
| 1173 |
client_set_state(client, DHCP6_STATE_STOPPED); |
| 1174 |
|
| 1175 |
client_notify(client, SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST); |
| 1176 |
return 0; |
| 1177 |
} |
| 1178 |
|
| 1179 |
static int client_process_reply( |
| 1180 |
sd_dhcp6_client *client, |
| 1181 |
DHCP6Message *message, |
| 1182 |
size_t len, |
| 1183 |
const triple_timestamp *timestamp, |
| 1184 |
const struct in6_addr *server_address) { |
| 1185 |
|
| 1186 |
_cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL; |
| 1187 |
int r; |
| 1188 |
|
| 1189 |
assert(client); |
| 1190 |
assert(message); |
| 1191 |
|
| 1192 |
if (message->type != DHCP6_MESSAGE_REPLY) |
| 1193 |
return log_invalid_message_type(client, message); |
| 1194 |
|
| 1195 |
r = dhcp6_lease_new_from_message(client, message, len, timestamp, server_address, &lease); |
| 1196 |
if (r == -EADDRNOTAVAIL) { |
| 1197 |
|
| 1198 |
/* If NoBinding status code is received, we cannot request the address anymore. |
| 1199 |
* Let's restart transaction from the beginning. */ |
| 1200 |
|
| 1201 |
if (client->state == DHCP6_STATE_REQUEST) |
| 1202 |
/* The lease is not acquired yet, hence it is not necessary to notify the restart. */ |
| 1203 |
client_cleanup(client); |
| 1204 |
else |
| 1205 |
/* We need to notify the previous lease was expired. */ |
| 1206 |
client_stop(client, SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE); |
| 1207 |
|
| 1208 |
return client_start_transaction(client, DHCP6_STATE_SOLICITATION); |
| 1209 |
} |
| 1210 |
if (r < 0) |
| 1211 |
return log_dhcp6_client_errno(client, r, "Failed to process received reply message, ignoring: %m"); |
| 1212 |
|
| 1213 |
log_dhcp6_client(client, "Processed %s message", dhcp6_message_type_to_string(message->type)); |
| 1214 |
|
| 1215 |
sd_dhcp6_lease_unref(client->lease); |
| 1216 |
client->lease = TAKE_PTR(lease); |
| 1217 |
|
| 1218 |
return client_enter_bound_state(client); |
| 1219 |
} |
| 1220 |
|
| 1221 |
static int client_process_advertise_or_rapid_commit_reply( |
| 1222 |
sd_dhcp6_client *client, |
| 1223 |
DHCP6Message *message, |
| 1224 |
size_t len, |
| 1225 |
const triple_timestamp *timestamp, |
| 1226 |
const struct in6_addr *server_address) { |
| 1227 |
|
| 1228 |
_cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL; |
| 1229 |
uint8_t pref_advertise, pref_lease = 0; |
| 1230 |
int r; |
| 1231 |
|
| 1232 |
assert(client); |
| 1233 |
assert(message); |
| 1234 |
|
| 1235 |
if (!IN_SET(message->type, DHCP6_MESSAGE_ADVERTISE, DHCP6_MESSAGE_REPLY)) |
| 1236 |
return log_invalid_message_type(client, message); |
| 1237 |
|
| 1238 |
r = dhcp6_lease_new_from_message(client, message, len, timestamp, server_address, &lease); |
| 1239 |
if (r < 0) |
| 1240 |
return log_dhcp6_client_errno(client, r, "Failed to process received %s message, ignoring: %m", |
| 1241 |
dhcp6_message_type_to_string(message->type)); |
| 1242 |
|
| 1243 |
if (message->type == DHCP6_MESSAGE_REPLY) { |
| 1244 |
bool rapid_commit; |
| 1245 |
|
| 1246 |
if (!client->rapid_commit) |
| 1247 |
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), |
| 1248 |
"Received unexpected reply message, even we sent a solicit message without the rapid commit option, ignoring."); |
| 1249 |
|
| 1250 |
r = dhcp6_lease_get_rapid_commit(lease, &rapid_commit); |
| 1251 |
if (r < 0) |
| 1252 |
return r; |
| 1253 |
|
| 1254 |
if (!rapid_commit) |
| 1255 |
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), |
| 1256 |
"Received reply message without rapid commit flag, ignoring."); |
| 1257 |
|
| 1258 |
log_dhcp6_client(client, "Processed %s message", dhcp6_message_type_to_string(message->type)); |
| 1259 |
|
| 1260 |
sd_dhcp6_lease_unref(client->lease); |
| 1261 |
client->lease = TAKE_PTR(lease); |
| 1262 |
|
| 1263 |
return client_enter_bound_state(client); |
| 1264 |
} |
| 1265 |
|
| 1266 |
r = dhcp6_lease_get_preference(lease, &pref_advertise); |
| 1267 |
if (r < 0) |
| 1268 |
return r; |
| 1269 |
|
| 1270 |
if (client->lease) { |
| 1271 |
r = dhcp6_lease_get_preference(client->lease, &pref_lease); |
| 1272 |
if (r < 0) |
| 1273 |
return r; |
| 1274 |
} |
| 1275 |
|
| 1276 |
log_dhcp6_client(client, "Processed %s message", dhcp6_message_type_to_string(message->type)); |
| 1277 |
|
| 1278 |
if (!client->lease || pref_advertise > pref_lease) { |
| 1279 |
/* If this is the first advertise message or has higher preference, then save the lease. */ |
| 1280 |
sd_dhcp6_lease_unref(client->lease); |
| 1281 |
client->lease = TAKE_PTR(lease); |
| 1282 |
} |
| 1283 |
|
| 1284 |
if (pref_advertise == 255 || client->retransmit_count > 1) |
| 1285 |
(void) client_start_transaction(client, DHCP6_STATE_REQUEST); |
| 1286 |
|
| 1287 |
return 0; |
| 1288 |
} |
| 1289 |
|
| 1290 |
static int client_receive_message( |
| 1291 |
sd_event_source *s, |
| 1292 |
int fd, uint32_t |
| 1293 |
revents, |
| 1294 |
void *userdata) { |
| 1295 |
|
| 1296 |
sd_dhcp6_client *client = ASSERT_PTR(userdata); |
| 1297 |
DHCP6_CLIENT_DONT_DESTROY(client); |
| 1298 |
/* This needs to be initialized with zero. See #20741. |
| 1299 |
* The issue is fixed on glibc-2.35 (8fba672472ae0055387e9315fc2eddfa6775ca79). */ |
| 1300 |
CMSG_BUFFER_TYPE(CMSG_SPACE_TIMEVAL) control = {}; |
| 1301 |
struct iovec iov; |
| 1302 |
union sockaddr_union sa = {}; |
| 1303 |
struct msghdr msg = { |
| 1304 |
.msg_name = &sa.sa, |
| 1305 |
.msg_namelen = sizeof(sa), |
| 1306 |
.msg_iov = &iov, |
| 1307 |
.msg_iovlen = 1, |
| 1308 |
.msg_control = &control, |
| 1309 |
.msg_controllen = sizeof(control), |
| 1310 |
}; |
| 1311 |
triple_timestamp t; |
| 1312 |
_cleanup_free_ DHCP6Message *message = NULL; |
| 1313 |
struct in6_addr *server_address = NULL; |
| 1314 |
ssize_t buflen, len; |
| 1315 |
|
| 1316 |
buflen = next_datagram_size_fd(fd); |
| 1317 |
if (ERRNO_IS_NEG_TRANSIENT(buflen) || ERRNO_IS_NEG_DISCONNECT(buflen)) |
| 1318 |
return 0; |
| 1319 |
if (buflen < 0) { |
| 1320 |
log_dhcp6_client_errno(client, buflen, "Failed to determine datagram size to read, ignoring: %m"); |
| 1321 |
return 0; |
| 1322 |
} |
| 1323 |
|
| 1324 |
message = malloc(buflen); |
| 1325 |
if (!message) |
| 1326 |
return -ENOMEM; |
| 1327 |
|
| 1328 |
iov = IOVEC_MAKE(message, buflen); |
| 1329 |
|
| 1330 |
len = recvmsg_safe(fd, &msg, MSG_DONTWAIT); |
| 1331 |
if (ERRNO_IS_NEG_TRANSIENT(len) || ERRNO_IS_NEG_DISCONNECT(len)) |
| 1332 |
return 0; |
| 1333 |
if (len < 0) { |
| 1334 |
log_dhcp6_client_errno(client, len, "Could not receive message from UDP socket, ignoring: %m"); |
| 1335 |
return 0; |
| 1336 |
} |
| 1337 |
if ((size_t) len < sizeof(DHCP6Message)) { |
| 1338 |
log_dhcp6_client(client, "Too small to be DHCPv6 message: ignoring"); |
| 1339 |
return 0; |
| 1340 |
} |
| 1341 |
|
| 1342 |
/* msg_namelen == 0 happens when running the test-suite over a socketpair */ |
| 1343 |
if (msg.msg_namelen > 0) { |
| 1344 |
if (msg.msg_namelen != sizeof(struct sockaddr_in6) || sa.in6.sin6_family != AF_INET6) { |
| 1345 |
log_dhcp6_client(client, "Received message from invalid source, ignoring."); |
| 1346 |
return 0; |
| 1347 |
} |
| 1348 |
|
| 1349 |
server_address = &sa.in6.sin6_addr; |
| 1350 |
} |
| 1351 |
|
| 1352 |
triple_timestamp_from_cmsg(&t, &msg); |
| 1353 |
|
| 1354 |
if (client->transaction_id != (message->transaction_id & htobe32(0x00ffffff))) |
| 1355 |
return 0; |
| 1356 |
|
| 1357 |
switch (client->state) { |
| 1358 |
case DHCP6_STATE_INFORMATION_REQUEST: |
| 1359 |
if (client_process_information(client, message, len, &t, server_address) < 0) |
| 1360 |
return 0; |
| 1361 |
break; |
| 1362 |
|
| 1363 |
case DHCP6_STATE_SOLICITATION: |
| 1364 |
if (client_process_advertise_or_rapid_commit_reply(client, message, len, &t, server_address) < 0) |
| 1365 |
return 0; |
| 1366 |
break; |
| 1367 |
|
| 1368 |
case DHCP6_STATE_REQUEST: |
| 1369 |
case DHCP6_STATE_RENEW: |
| 1370 |
case DHCP6_STATE_REBIND: |
| 1371 |
if (client_process_reply(client, message, len, &t, server_address) < 0) |
| 1372 |
return 0; |
| 1373 |
break; |
| 1374 |
|
| 1375 |
case DHCP6_STATE_BOUND: |
| 1376 |
case DHCP6_STATE_STOPPED: |
| 1377 |
case DHCP6_STATE_STOPPING: |
| 1378 |
default: |
| 1379 |
assert_not_reached(); |
| 1380 |
} |
| 1381 |
|
| 1382 |
return 0; |
| 1383 |
} |
| 1384 |
|
| 1385 |
static int client_send_release(sd_dhcp6_client *client) { |
| 1386 |
sd_dhcp6_lease *lease; |
| 1387 |
|
| 1388 |
assert(client); |
| 1389 |
|
| 1390 |
if (!client->send_release) |
| 1391 |
return 0; |
| 1392 |
|
| 1393 |
if (sd_dhcp6_client_get_lease(client, &lease) < 0) |
| 1394 |
return 0; |
| 1395 |
|
| 1396 |
if (!lease->ia_na && !lease->ia_pd) |
| 1397 |
return 0; |
| 1398 |
|
| 1399 |
client_set_state(client, DHCP6_STATE_STOPPING); |
| 1400 |
return dhcp6_client_send_message(client); |
| 1401 |
} |
| 1402 |
|
| 1403 |
int sd_dhcp6_client_stop(sd_dhcp6_client *client) { |
| 1404 |
int r; |
| 1405 |
|
| 1406 |
if (!client) |
| 1407 |
return 0; |
| 1408 |
|
| 1409 |
/* Intentionally ignoring failure to send DHCP6 release. The DHCPv6 client |
| 1410 |
* engine is about to release its UDP socket unconditionally. */ |
| 1411 |
r = client_send_release(client); |
| 1412 |
if (r < 0) |
| 1413 |
log_dhcp6_client_errno(client, r, |
| 1414 |
"Failed to send DHCPv6 release message, ignoring: %m"); |
| 1415 |
|
| 1416 |
client_stop(client, SD_DHCP6_CLIENT_EVENT_STOP); |
| 1417 |
|
| 1418 |
client->receive_message = sd_event_source_unref(client->receive_message); |
| 1419 |
client->fd = safe_close(client->fd); |
| 1420 |
|
| 1421 |
return 0; |
| 1422 |
} |
| 1423 |
|
| 1424 |
int sd_dhcp6_client_is_running(sd_dhcp6_client *client) { |
| 1425 |
if (!client) |
| 1426 |
return false; |
| 1427 |
|
| 1428 |
return client->state != DHCP6_STATE_STOPPED; |
| 1429 |
} |
| 1430 |
|
| 1431 |
int sd_dhcp6_client_start(sd_dhcp6_client *client) { |
| 1432 |
DHCP6State state = DHCP6_STATE_SOLICITATION; |
| 1433 |
int r; |
| 1434 |
|
| 1435 |
assert_return(client, -EINVAL); |
| 1436 |
assert_return(client->event, -EINVAL); |
| 1437 |
assert_return(client->ifindex > 0, -EINVAL); |
| 1438 |
assert_return(in6_addr_is_link_local(&client->local_address) > 0, -EINVAL); |
| 1439 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 1440 |
assert_return(client->information_request || client->request_ia != 0, -EINVAL); |
| 1441 |
|
| 1442 |
/* Even if the client is in the STOPPED state, the lease acquired in the previous information |
| 1443 |
* request may be stored. */ |
| 1444 |
client->lease = sd_dhcp6_lease_unref(client->lease); |
| 1445 |
|
| 1446 |
r = client_ensure_iaid(client); |
| 1447 |
if (r < 0) |
| 1448 |
return r; |
| 1449 |
|
| 1450 |
r = client_ensure_duid(client); |
| 1451 |
if (r < 0) |
| 1452 |
return r; |
| 1453 |
|
| 1454 |
if (client->fd < 0) { |
| 1455 |
r = dhcp6_network_bind_udp_socket(client->ifindex, &client->local_address); |
| 1456 |
if (r < 0) |
| 1457 |
return log_dhcp6_client_errno(client, r, |
| 1458 |
"Failed to bind to UDP socket at address %s: %m", |
| 1459 |
IN6_ADDR_TO_STRING(&client->local_address)); |
| 1460 |
|
| 1461 |
client->fd = r; |
| 1462 |
} |
| 1463 |
|
| 1464 |
if (!client->receive_message) { |
| 1465 |
_cleanup_(sd_event_source_disable_unrefp) sd_event_source *s = NULL; |
| 1466 |
|
| 1467 |
r = sd_event_add_io(client->event, &s, client->fd, EPOLLIN, client_receive_message, client); |
| 1468 |
if (r < 0) |
| 1469 |
return r; |
| 1470 |
|
| 1471 |
r = sd_event_source_set_priority(s, client->event_priority); |
| 1472 |
if (r < 0) |
| 1473 |
return r; |
| 1474 |
|
| 1475 |
r = sd_event_source_set_description(s, "dhcp6-receive-message"); |
| 1476 |
if (r < 0) |
| 1477 |
return r; |
| 1478 |
|
| 1479 |
client->receive_message = TAKE_PTR(s); |
| 1480 |
} |
| 1481 |
|
| 1482 |
if (client->information_request) { |
| 1483 |
usec_t t = now(CLOCK_MONOTONIC); |
| 1484 |
|
| 1485 |
if (t < usec_add(client->information_request_time_usec, client->information_refresh_time_usec)) |
| 1486 |
return 0; |
| 1487 |
|
| 1488 |
client->information_request_time_usec = t; |
| 1489 |
state = DHCP6_STATE_INFORMATION_REQUEST; |
| 1490 |
} |
| 1491 |
|
| 1492 |
log_dhcp6_client(client, "Starting in %s mode", |
| 1493 |
client->information_request ? "Information request" : "Solicit"); |
| 1494 |
|
| 1495 |
return client_start_transaction(client, state); |
| 1496 |
} |
| 1497 |
|
| 1498 |
int sd_dhcp6_client_attach_event(sd_dhcp6_client *client, sd_event *event, int64_t priority) { |
| 1499 |
int r; |
| 1500 |
|
| 1501 |
assert_return(client, -EINVAL); |
| 1502 |
assert_return(!client->event, -EBUSY); |
| 1503 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 1504 |
|
| 1505 |
if (event) |
| 1506 |
client->event = sd_event_ref(event); |
| 1507 |
else { |
| 1508 |
r = sd_event_default(&client->event); |
| 1509 |
if (r < 0) |
| 1510 |
return r; |
| 1511 |
} |
| 1512 |
|
| 1513 |
client->event_priority = priority; |
| 1514 |
|
| 1515 |
return 0; |
| 1516 |
} |
| 1517 |
|
| 1518 |
int sd_dhcp6_client_detach_event(sd_dhcp6_client *client) { |
| 1519 |
assert_return(client, -EINVAL); |
| 1520 |
assert_return(!sd_dhcp6_client_is_running(client), -EBUSY); |
| 1521 |
|
| 1522 |
client->event = sd_event_unref(client->event); |
| 1523 |
|
| 1524 |
return 0; |
| 1525 |
} |
| 1526 |
|
| 1527 |
sd_event *sd_dhcp6_client_get_event(sd_dhcp6_client *client) { |
| 1528 |
assert_return(client, NULL); |
| 1529 |
|
| 1530 |
return client->event; |
| 1531 |
} |
| 1532 |
|
| 1533 |
int sd_dhcp6_client_attach_device(sd_dhcp6_client *client, sd_device *dev) { |
| 1534 |
assert_return(client, -EINVAL); |
| 1535 |
|
| 1536 |
return device_unref_and_replace_new_ref(client->dev, dev); |
| 1537 |
} |
| 1538 |
|
| 1539 |
static sd_dhcp6_client *dhcp6_client_free(sd_dhcp6_client *client) { |
| 1540 |
if (!client) |
| 1541 |
return NULL; |
| 1542 |
|
| 1543 |
sd_dhcp6_lease_unref(client->lease); |
| 1544 |
|
| 1545 |
sd_event_source_disable_unref(client->receive_message); |
| 1546 |
sd_event_source_disable_unref(client->timeout_resend); |
| 1547 |
sd_event_source_disable_unref(client->timeout_expire); |
| 1548 |
sd_event_source_disable_unref(client->timeout_t1); |
| 1549 |
sd_event_source_disable_unref(client->timeout_t2); |
| 1550 |
sd_event_unref(client->event); |
| 1551 |
|
| 1552 |
client->fd = safe_close(client->fd); |
| 1553 |
|
| 1554 |
sd_device_unref(client->dev); |
| 1555 |
|
| 1556 |
free(client->req_opts); |
| 1557 |
free(client->fqdn); |
| 1558 |
free(client->mudurl); |
| 1559 |
dhcp6_ia_clear_addresses(&client->ia_pd); |
| 1560 |
ordered_hashmap_free(client->extra_options); |
| 1561 |
ordered_set_free(client->vendor_options); |
| 1562 |
strv_free(client->user_class); |
| 1563 |
strv_free(client->vendor_class); |
| 1564 |
free(client->ifname); |
| 1565 |
|
| 1566 |
return mfree(client); |
| 1567 |
} |
| 1568 |
|
| 1569 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp6_client, sd_dhcp6_client, dhcp6_client_free); |
| 1570 |
|
| 1571 |
int sd_dhcp6_client_new(sd_dhcp6_client **ret) { |
| 1572 |
_cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL; |
| 1573 |
|
| 1574 |
assert_return(ret, -EINVAL); |
| 1575 |
|
| 1576 |
client = new(sd_dhcp6_client, 1); |
| 1577 |
if (!client) |
| 1578 |
return -ENOMEM; |
| 1579 |
|
| 1580 |
*client = (sd_dhcp6_client) { |
| 1581 |
.n_ref = 1, |
| 1582 |
.ia_na.type = SD_DHCP6_OPTION_IA_NA, |
| 1583 |
.ia_pd.type = SD_DHCP6_OPTION_IA_PD, |
| 1584 |
.ifindex = -1, |
| 1585 |
.request_ia = DHCP6_REQUEST_IA_NA | DHCP6_REQUEST_IA_PD, |
| 1586 |
.fd = -EBADF, |
| 1587 |
.rapid_commit = true, |
| 1588 |
}; |
| 1589 |
|
| 1590 |
*ret = TAKE_PTR(client); |
| 1591 |
|
| 1592 |
return 0; |
| 1593 |
} |