| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_arp.h> |
| 4 |
#include <linux/rtnetlink.h> |
| 5 |
#include <netinet/in.h> |
| 6 |
|
| 7 |
#include "sd-bus.h" |
| 8 |
#include "sd-dhcp6-lease.h" |
| 9 |
#include "sd-dhcp6-option.h" |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "bus-error.h" |
| 13 |
#include "bus-locator.h" |
| 14 |
#include "dhcp6-option.h" |
| 15 |
#include "escape.h" |
| 16 |
#include "extract-word.h" |
| 17 |
#include "hexdecoct.h" |
| 18 |
#include "in-addr-prefix-util.h" |
| 19 |
#include "iovec-util.h" |
| 20 |
#include "networkd-dhcp-common.h" |
| 21 |
#include "networkd-dhcp-prefix-delegation.h" |
| 22 |
#include "networkd-ipv6ll.h" |
| 23 |
#include "networkd-link.h" |
| 24 |
#include "networkd-manager.h" |
| 25 |
#include "networkd-network.h" |
| 26 |
#include "networkd-route-util.h" |
| 27 |
#include "networkd-util.h" |
| 28 |
#include "parse-util.h" |
| 29 |
#include "set.h" |
| 30 |
#include "socket-util.h" |
| 31 |
#include "string-table.h" |
| 32 |
#include "string-util.h" |
| 33 |
#include "strv.h" |
| 34 |
#include "vrf.h" |
| 35 |
|
| 36 |
static uint32_t link_get_vrf_table(Link *link) { |
| 37 |
assert(link); |
| 38 |
assert(link->network); |
| 39 |
|
| 40 |
return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN; |
| 41 |
} |
| 42 |
|
| 43 |
uint32_t link_get_dhcp4_route_table(Link *link) { |
| 44 |
assert(link); |
| 45 |
assert(link->network); |
| 46 |
|
| 47 |
/* When the interface is part of an VRF use the VRFs routing table, unless |
| 48 |
* another table is explicitly specified. */ |
| 49 |
|
| 50 |
if (link->network->dhcp_route_table_set) |
| 51 |
return link->network->dhcp_route_table; |
| 52 |
return link_get_vrf_table(link); |
| 53 |
} |
| 54 |
|
| 55 |
uint32_t link_get_ndisc_route_table(Link *link) { |
| 56 |
assert(link); |
| 57 |
assert(link->network); |
| 58 |
|
| 59 |
if (link->network->ndisc_route_table_set) |
| 60 |
return link->network->ndisc_route_table; |
| 61 |
return link_get_vrf_table(link); |
| 62 |
} |
| 63 |
|
| 64 |
uint32_t link_get_dhcp6_route_table(Link *link) { |
| 65 |
assert(link); |
| 66 |
assert(link->network); |
| 67 |
|
| 68 |
if (link->network->dhcp6_route_table_set) |
| 69 |
return link->network->dhcp6_route_table; |
| 70 |
return link_get_vrf_table(link); |
| 71 |
} |
| 72 |
|
| 73 |
bool link_dhcp_enabled(Link *link, int family) { |
| 74 |
assert(link); |
| 75 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 76 |
|
| 77 |
/* Currently, sd-dhcp-client supports only ethernet and infiniband. |
| 78 |
* (ARMHRD_RAWIP and ARMHRD_NONE are typically wwan modems and will be |
| 79 |
* treated as ethernet devices.) */ |
| 80 |
if (family == AF_INET && !IN_SET(link->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND, ARPHRD_RAWIP, ARPHRD_NONE)) |
| 81 |
return false; |
| 82 |
|
| 83 |
if (family == AF_INET6 && !socket_ipv6_is_supported()) |
| 84 |
return false; |
| 85 |
|
| 86 |
if (link->flags & IFF_LOOPBACK) |
| 87 |
return false; |
| 88 |
|
| 89 |
if (link->iftype == ARPHRD_CAN) |
| 90 |
return false; |
| 91 |
|
| 92 |
if (!link->network) |
| 93 |
return false; |
| 94 |
|
| 95 |
return link->network->dhcp & AF_TO_ADDRESS_FAMILY(family); |
| 96 |
} |
| 97 |
|
| 98 |
void network_adjust_dhcp(Network *network) { |
| 99 |
assert(network); |
| 100 |
assert(network->dhcp >= 0); |
| 101 |
|
| 102 |
if (network->dhcp == ADDRESS_FAMILY_NO) |
| 103 |
return; |
| 104 |
|
| 105 |
/* Bonding slave does not support addressing. */ |
| 106 |
if (network->bond) { |
| 107 |
log_warning("%s: Cannot enable DHCP= when Bond= is specified, disabling DHCP=.", |
| 108 |
network->filename); |
| 109 |
network->dhcp = ADDRESS_FAMILY_NO; |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6) && |
| 114 |
!network_has_static_ipv6ll_address(network) && |
| 115 |
FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV6)) { |
| 116 |
log_warning("%s: DHCPv6 client is enabled but IPv6 link-local addressing is disabled. " |
| 117 |
"Disabling DHCPv6 client.", network->filename); |
| 118 |
SET_FLAG(network->dhcp, ADDRESS_FAMILY_IPV6, false); |
| 119 |
} |
| 120 |
|
| 121 |
network_adjust_dhcp4(network); |
| 122 |
network_adjust_dhcp_prefix_delegation(network); |
| 123 |
} |
| 124 |
|
| 125 |
static bool duid_needs_product_uuid(const DUID *duid) { |
| 126 |
assert(duid); |
| 127 |
|
| 128 |
return duid->type == DUID_TYPE_UUID && duid->raw_data_len == 0; |
| 129 |
} |
| 130 |
|
| 131 |
static const struct DUID fallback_duid = { .type = DUID_TYPE_EN }; |
| 132 |
|
| 133 |
const DUID *link_get_duid(Link *link, int family) { |
| 134 |
const DUID *duid; |
| 135 |
|
| 136 |
assert(link); |
| 137 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 138 |
|
| 139 |
if (link->network) { |
| 140 |
duid = family == AF_INET ? &link->network->dhcp_duid : &link->network->dhcp6_duid; |
| 141 |
if (duid->type != _DUID_TYPE_INVALID) { |
| 142 |
if (duid_needs_product_uuid(duid)) |
| 143 |
return &link->manager->duid_product_uuid; |
| 144 |
else |
| 145 |
return duid; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
duid = family == AF_INET ? &link->manager->dhcp_duid : &link->manager->dhcp6_duid; |
| 150 |
if (link->hw_addr.length == 0 && IN_SET(duid->type, DUID_TYPE_LLT, DUID_TYPE_LL)) |
| 151 |
/* Fallback to DUID that works without MAC address. |
| 152 |
* This is useful for tunnel devices without MAC address. */ |
| 153 |
return &fallback_duid; |
| 154 |
|
| 155 |
return duid; |
| 156 |
} |
| 157 |
|
| 158 |
static int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
| 159 |
Manager *manager = ASSERT_PTR(userdata); |
| 160 |
const sd_bus_error *e; |
| 161 |
const void *a; |
| 162 |
size_t sz; |
| 163 |
int r; |
| 164 |
|
| 165 |
assert(m); |
| 166 |
|
| 167 |
/* To avoid calling GetProductUUID() bus method so frequently, set the flag below |
| 168 |
* even if the method fails. */ |
| 169 |
manager->has_product_uuid = true; |
| 170 |
|
| 171 |
e = sd_bus_message_get_error(m); |
| 172 |
if (e) { |
| 173 |
r = sd_bus_error_get_errno(e); |
| 174 |
log_warning_errno(r, "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s", |
| 175 |
bus_error_message(e, r)); |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
r = sd_bus_message_read_array(m, 'y', &a, &sz); |
| 180 |
if (r < 0) { |
| 181 |
log_warning_errno(r, "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m"); |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
if (sz != sizeof(sd_id128_t)) { |
| 186 |
log_warning("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID."); |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
log_debug("Successfully obtained product UUID"); |
| 191 |
|
| 192 |
memcpy(&manager->duid_product_uuid.raw_data, a, sz); |
| 193 |
manager->duid_product_uuid.raw_data_len = sz; |
| 194 |
|
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
int manager_request_product_uuid(Manager *m) { |
| 199 |
static bool bus_method_is_called = false; |
| 200 |
int r; |
| 201 |
|
| 202 |
assert(m); |
| 203 |
|
| 204 |
if (bus_method_is_called) |
| 205 |
return 0; |
| 206 |
|
| 207 |
if (sd_bus_is_ready(m->bus) <= 0 && !m->product_uuid_requested) { |
| 208 |
log_debug("Not connected to system bus, requesting product UUID later."); |
| 209 |
m->product_uuid_requested = true; |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
m->product_uuid_requested = false; |
| 214 |
|
| 215 |
r = bus_call_method_async( |
| 216 |
m->bus, |
| 217 |
NULL, |
| 218 |
bus_hostname, |
| 219 |
"GetProductUUID", |
| 220 |
get_product_uuid_handler, |
| 221 |
m, |
| 222 |
"b", |
| 223 |
false); |
| 224 |
if (r < 0) |
| 225 |
return log_warning_errno(r, "Failed to get product UUID: %m"); |
| 226 |
|
| 227 |
log_debug("Requesting product UUID."); |
| 228 |
|
| 229 |
bus_method_is_called = true; |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
int dhcp_configure_duid(Link *link, const DUID *duid) { |
| 235 |
Manager *m; |
| 236 |
int r; |
| 237 |
|
| 238 |
assert(link); |
| 239 |
assert(link->manager); |
| 240 |
assert(duid); |
| 241 |
|
| 242 |
m = link->manager; |
| 243 |
|
| 244 |
if (!duid_needs_product_uuid(duid)) |
| 245 |
return 1; |
| 246 |
|
| 247 |
if (m->has_product_uuid) |
| 248 |
return 1; |
| 249 |
|
| 250 |
r = manager_request_product_uuid(m); |
| 251 |
if (r < 0) { |
| 252 |
log_link_warning_errno(link, r, |
| 253 |
"Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m"); |
| 254 |
|
| 255 |
m->has_product_uuid = true; /* Do not request UUID again on failure. */ |
| 256 |
return 1; |
| 257 |
} |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
bool address_is_filtered(int family, const union in_addr_union *address, uint8_t prefixlen, Set *allow_list, Set *deny_list) { |
| 263 |
struct in_addr_prefix *p; |
| 264 |
|
| 265 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 266 |
assert(address); |
| 267 |
|
| 268 |
if (allow_list) { |
| 269 |
SET_FOREACH(p, allow_list) |
| 270 |
if (p->family == family && |
| 271 |
p->prefixlen <= prefixlen && |
| 272 |
in_addr_prefix_covers(family, &p->address, p->prefixlen, address) > 0) |
| 273 |
return false; |
| 274 |
|
| 275 |
return true; |
| 276 |
} |
| 277 |
|
| 278 |
SET_FOREACH(p, deny_list) |
| 279 |
if (p->family == family && |
| 280 |
in_addr_prefix_intersect(family, &p->address, p->prefixlen, address, prefixlen) > 0) |
| 281 |
return true; |
| 282 |
|
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
int link_get_captive_portal(Link *link, const char **ret) { |
| 287 |
const char *dhcp4_cp = NULL, *dhcp6_cp = NULL, *ndisc_cp = NULL; |
| 288 |
int r; |
| 289 |
|
| 290 |
assert(link); |
| 291 |
assert(ret); |
| 292 |
|
| 293 |
if (!link->network) { |
| 294 |
*ret = NULL; |
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
if (link->network->dhcp_use_captive_portal && link->dhcp_lease) { |
| 299 |
r = sd_dhcp_lease_get_captive_portal(link->dhcp_lease, &dhcp4_cp); |
| 300 |
if (r < 0 && r != -ENODATA) |
| 301 |
return r; |
| 302 |
} |
| 303 |
|
| 304 |
if (link->network->dhcp6_use_captive_portal && link->dhcp6_lease) { |
| 305 |
r = sd_dhcp6_lease_get_captive_portal(link->dhcp6_lease, &dhcp6_cp); |
| 306 |
if (r < 0 && r != -ENODATA) |
| 307 |
return r; |
| 308 |
} |
| 309 |
|
| 310 |
if (link->network->ndisc_use_captive_portal) { |
| 311 |
NDiscCaptivePortal *cp; |
| 312 |
usec_t usec = 0; |
| 313 |
|
| 314 |
/* Use the captive portal with the longest lifetime. */ |
| 315 |
|
| 316 |
SET_FOREACH(cp, link->ndisc_captive_portals) { |
| 317 |
if (cp->lifetime_usec < usec) |
| 318 |
continue; |
| 319 |
|
| 320 |
ndisc_cp = cp->captive_portal; |
| 321 |
usec = cp->lifetime_usec; |
| 322 |
} |
| 323 |
|
| 324 |
if (set_size(link->ndisc_captive_portals) > 1) |
| 325 |
log_link_debug(link, "Multiple captive portals obtained by IPv6RA, using \"%s\" and ignoring others.", |
| 326 |
ndisc_cp); |
| 327 |
} |
| 328 |
|
| 329 |
if (dhcp4_cp) { |
| 330 |
if (dhcp6_cp && !streq(dhcp4_cp, dhcp6_cp)) |
| 331 |
log_link_debug(link, "DHCPv6 captive portal (%s) does not match DHCPv4 (%s), ignoring DHCPv6 captive portal.", |
| 332 |
dhcp6_cp, dhcp4_cp); |
| 333 |
|
| 334 |
if (ndisc_cp && !streq(dhcp4_cp, ndisc_cp)) |
| 335 |
log_link_debug(link, "IPv6RA captive portal (%s) does not match DHCPv4 (%s), ignoring IPv6RA captive portal.", |
| 336 |
ndisc_cp, dhcp4_cp); |
| 337 |
|
| 338 |
*ret = dhcp4_cp; |
| 339 |
return 1; |
| 340 |
} |
| 341 |
|
| 342 |
if (dhcp6_cp) { |
| 343 |
if (ndisc_cp && !streq(dhcp6_cp, ndisc_cp)) |
| 344 |
log_link_debug(link, "IPv6RA captive portal (%s) does not match DHCPv6 (%s), ignoring IPv6RA captive portal.", |
| 345 |
ndisc_cp, dhcp6_cp); |
| 346 |
|
| 347 |
*ret = dhcp6_cp; |
| 348 |
return 1; |
| 349 |
} |
| 350 |
|
| 351 |
*ret = ndisc_cp; |
| 352 |
return !!ndisc_cp; |
| 353 |
} |
| 354 |
|
| 355 |
int config_parse_dhcp( |
| 356 |
const char *unit, |
| 357 |
const char *filename, |
| 358 |
unsigned line, |
| 359 |
const char *section, |
| 360 |
unsigned section_line, |
| 361 |
const char *lvalue, |
| 362 |
int ltype, |
| 363 |
const char *rvalue, |
| 364 |
void *data, |
| 365 |
void *userdata) { |
| 366 |
|
| 367 |
AddressFamily *dhcp = data, s; |
| 368 |
|
| 369 |
assert(filename); |
| 370 |
assert(lvalue); |
| 371 |
assert(rvalue); |
| 372 |
assert(data); |
| 373 |
|
| 374 |
/* Note that this is mostly like |
| 375 |
* config_parse_address_family(), except that it |
| 376 |
* understands some old names for the enum values */ |
| 377 |
|
| 378 |
s = address_family_from_string(rvalue); |
| 379 |
if (s < 0) { |
| 380 |
|
| 381 |
/* Previously, we had a slightly different enum here, |
| 382 |
* support its values for compatibility. */ |
| 383 |
|
| 384 |
s = dhcp_deprecated_address_family_from_string(rvalue); |
| 385 |
if (s < 0) { |
| 386 |
log_syntax(unit, LOG_WARNING, filename, line, s, |
| 387 |
"Failed to parse DHCP option, ignoring: %s", rvalue); |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 392 |
"DHCP=%s is deprecated, please use DHCP=%s instead.", |
| 393 |
rvalue, address_family_to_string(s)); |
| 394 |
} |
| 395 |
|
| 396 |
*dhcp = s; |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
int config_parse_dhcp_route_metric( |
| 401 |
const char *unit, |
| 402 |
const char *filename, |
| 403 |
unsigned line, |
| 404 |
const char *section, |
| 405 |
unsigned section_line, |
| 406 |
const char *lvalue, |
| 407 |
int ltype, |
| 408 |
const char *rvalue, |
| 409 |
void *data, |
| 410 |
void *userdata) { |
| 411 |
|
| 412 |
Network *network = userdata; |
| 413 |
uint32_t metric; |
| 414 |
int r; |
| 415 |
|
| 416 |
assert(filename); |
| 417 |
assert(lvalue); |
| 418 |
assert(IN_SET(ltype, AF_UNSPEC, AF_INET)); |
| 419 |
assert(rvalue); |
| 420 |
assert(data); |
| 421 |
|
| 422 |
r = safe_atou32(rvalue, &metric); |
| 423 |
if (r < 0) { |
| 424 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 425 |
"Failed to parse RouteMetric=%s, ignoring assignment: %m", rvalue); |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
switch (ltype) { |
| 430 |
case AF_INET: |
| 431 |
network->dhcp_route_metric = metric; |
| 432 |
network->dhcp_route_metric_set = true; |
| 433 |
break; |
| 434 |
case AF_UNSPEC: |
| 435 |
/* For backward compatibility. */ |
| 436 |
if (!network->dhcp_route_metric_set) |
| 437 |
network->dhcp_route_metric = metric; |
| 438 |
if (!network->ndisc_route_metric_set) { |
| 439 |
network->ndisc_route_metric_high = metric; |
| 440 |
network->ndisc_route_metric_medium = metric; |
| 441 |
network->ndisc_route_metric_low = metric; |
| 442 |
} |
| 443 |
break; |
| 444 |
default: |
| 445 |
assert_not_reached(); |
| 446 |
} |
| 447 |
|
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
int config_parse_ndisc_route_metric( |
| 452 |
const char *unit, |
| 453 |
const char *filename, |
| 454 |
unsigned line, |
| 455 |
const char *section, |
| 456 |
unsigned section_line, |
| 457 |
const char *lvalue, |
| 458 |
int ltype, |
| 459 |
const char *rvalue, |
| 460 |
void *data, |
| 461 |
void *userdata) { |
| 462 |
|
| 463 |
Network *network = ASSERT_PTR(userdata); |
| 464 |
uint32_t metric_high, metric_medium, metric_low; |
| 465 |
int r, s, t; |
| 466 |
|
| 467 |
assert(filename); |
| 468 |
assert(rvalue); |
| 469 |
|
| 470 |
if (safe_atou32(rvalue, &metric_low) >= 0) |
| 471 |
metric_high = metric_medium = metric_low; |
| 472 |
else { |
| 473 |
_cleanup_free_ char *high = NULL, *medium = NULL, *low = NULL; |
| 474 |
const char *p = rvalue; |
| 475 |
|
| 476 |
r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &high, &medium, &low); |
| 477 |
if (r == -ENOMEM) |
| 478 |
return log_oom(); |
| 479 |
if (r != 3 || !isempty(p)) { |
| 480 |
log_syntax(unit, LOG_WARNING, filename, line, r < 0 ? r : 0, |
| 481 |
"Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue); |
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
r = safe_atou32(high, &metric_high); |
| 486 |
s = safe_atou32(medium, &metric_medium); |
| 487 |
t = safe_atou32(low, &metric_low); |
| 488 |
if (r < 0 || s < 0 || t < 0) { |
| 489 |
log_syntax(unit, LOG_WARNING, filename, line, r < 0 ? r : s < 0 ? s : t, |
| 490 |
"Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue); |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
if (metric_high >= metric_medium || metric_medium >= metric_low) { |
| 495 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 496 |
"Invalid RouteTable=%s, ignoring assignment: %m", rvalue); |
| 497 |
return 0; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
network->ndisc_route_metric_high = metric_high; |
| 502 |
network->ndisc_route_metric_medium = metric_medium; |
| 503 |
network->ndisc_route_metric_low = metric_low; |
| 504 |
network->ndisc_route_metric_set = true; |
| 505 |
|
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
int config_parse_dhcp_send_hostname( |
| 510 |
const char *unit, |
| 511 |
const char *filename, |
| 512 |
unsigned line, |
| 513 |
const char *section, |
| 514 |
unsigned section_line, |
| 515 |
const char *lvalue, |
| 516 |
int ltype, |
| 517 |
const char *rvalue, |
| 518 |
void *data, |
| 519 |
void *userdata) { |
| 520 |
|
| 521 |
Network *network = userdata; |
| 522 |
int r; |
| 523 |
|
| 524 |
assert(filename); |
| 525 |
assert(lvalue); |
| 526 |
assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6)); |
| 527 |
assert(rvalue); |
| 528 |
assert(data); |
| 529 |
|
| 530 |
r = parse_boolean(rvalue); |
| 531 |
if (r < 0) { |
| 532 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 533 |
"Failed to parse SendHostname=%s, ignoring assignment: %m", rvalue); |
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
switch (ltype) { |
| 538 |
case AF_INET: |
| 539 |
network->dhcp_send_hostname = r; |
| 540 |
network->dhcp_send_hostname_set = true; |
| 541 |
break; |
| 542 |
case AF_INET6: |
| 543 |
network->dhcp6_send_hostname = r; |
| 544 |
network->dhcp6_send_hostname_set = true; |
| 545 |
break; |
| 546 |
case AF_UNSPEC: |
| 547 |
/* For backward compatibility. */ |
| 548 |
if (!network->dhcp_send_hostname_set) |
| 549 |
network->dhcp_send_hostname = r; |
| 550 |
if (!network->dhcp6_send_hostname_set) |
| 551 |
network->dhcp6_send_hostname = r; |
| 552 |
break; |
| 553 |
default: |
| 554 |
assert_not_reached(); |
| 555 |
} |
| 556 |
|
| 557 |
return 0; |
| 558 |
} |
| 559 |
|
| 560 |
int config_parse_dhcp_or_ra_route_table( |
| 561 |
const char *unit, |
| 562 |
const char *filename, |
| 563 |
unsigned line, |
| 564 |
const char *section, |
| 565 |
unsigned section_line, |
| 566 |
const char *lvalue, |
| 567 |
int ltype, |
| 568 |
const char *rvalue, |
| 569 |
void *data, |
| 570 |
void *userdata) { |
| 571 |
|
| 572 |
Network *network = ASSERT_PTR(userdata); |
| 573 |
uint32_t rt; |
| 574 |
int r; |
| 575 |
|
| 576 |
assert(filename); |
| 577 |
assert(lvalue); |
| 578 |
assert(IN_SET(ltype, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6, NETWORK_CONFIG_SOURCE_NDISC)); |
| 579 |
assert(rvalue); |
| 580 |
|
| 581 |
r = manager_get_route_table_from_string(network->manager, rvalue, &rt); |
| 582 |
if (r < 0) { |
| 583 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 584 |
"Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue); |
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
switch (ltype) { |
| 589 |
case NETWORK_CONFIG_SOURCE_DHCP4: |
| 590 |
network->dhcp_route_table = rt; |
| 591 |
network->dhcp_route_table_set = true; |
| 592 |
break; |
| 593 |
case NETWORK_CONFIG_SOURCE_DHCP6: |
| 594 |
network->dhcp6_route_table = rt; |
| 595 |
network->dhcp6_route_table_set = true; |
| 596 |
break; |
| 597 |
case NETWORK_CONFIG_SOURCE_NDISC: |
| 598 |
network->ndisc_route_table = rt; |
| 599 |
network->ndisc_route_table_set = true; |
| 600 |
break; |
| 601 |
default: |
| 602 |
assert_not_reached(); |
| 603 |
} |
| 604 |
|
| 605 |
return 0; |
| 606 |
} |
| 607 |
|
| 608 |
int config_parse_iaid( |
| 609 |
const char *unit, |
| 610 |
const char *filename, |
| 611 |
unsigned line, |
| 612 |
const char *section, |
| 613 |
unsigned section_line, |
| 614 |
const char *lvalue, |
| 615 |
int ltype, |
| 616 |
const char *rvalue, |
| 617 |
void *data, |
| 618 |
void *userdata) { |
| 619 |
|
| 620 |
Network *network = ASSERT_PTR(userdata); |
| 621 |
uint32_t iaid; |
| 622 |
int r; |
| 623 |
|
| 624 |
assert(filename); |
| 625 |
assert(lvalue); |
| 626 |
assert(rvalue); |
| 627 |
assert(IN_SET(ltype, AF_INET, AF_INET6)); |
| 628 |
|
| 629 |
r = safe_atou32(rvalue, &iaid); |
| 630 |
if (r < 0) { |
| 631 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 632 |
"Unable to read IAID, ignoring assignment: %s", rvalue); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
if (ltype == AF_INET) { |
| 637 |
network->dhcp_iaid = iaid; |
| 638 |
network->dhcp_iaid_set = true; |
| 639 |
if (!network->dhcp6_iaid_set_explicitly) { |
| 640 |
/* Backward compatibility. Previously, IAID is shared by DHCPv4 and DHCPv6. |
| 641 |
* If DHCPv6 IAID is not specified explicitly, then use DHCPv4 IAID for DHCPv6. */ |
| 642 |
network->dhcp6_iaid = iaid; |
| 643 |
network->dhcp6_iaid_set = true; |
| 644 |
} |
| 645 |
} else { |
| 646 |
assert(ltype == AF_INET6); |
| 647 |
network->dhcp6_iaid = iaid; |
| 648 |
network->dhcp6_iaid_set = true; |
| 649 |
network->dhcp6_iaid_set_explicitly = true; |
| 650 |
} |
| 651 |
|
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
int config_parse_dhcp4_user_class( |
| 656 |
const char *unit, |
| 657 |
const char *filename, |
| 658 |
unsigned line, |
| 659 |
const char *section, |
| 660 |
unsigned section_line, |
| 661 |
const char *lvalue, |
| 662 |
int ltype, |
| 663 |
const char *rvalue, |
| 664 |
void *data, |
| 665 |
void *userdata) { |
| 666 |
|
| 667 |
struct iovec_wrapper *iovw = ASSERT_PTR(data); |
| 668 |
int r; |
| 669 |
|
| 670 |
assert(lvalue); |
| 671 |
assert(rvalue); |
| 672 |
|
| 673 |
if (isempty(rvalue)) { |
| 674 |
iovw_done_free(iovw); |
| 675 |
return 0; |
| 676 |
} |
| 677 |
|
| 678 |
for (const char *p = rvalue;;) { |
| 679 |
_cleanup_free_ char *w = NULL; |
| 680 |
|
| 681 |
r = extract_first_word(&p, &w, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE); |
| 682 |
if (r < 0) |
| 683 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 684 |
if (r == 0) |
| 685 |
return 0; |
| 686 |
|
| 687 |
size_t len = strlen(w); |
| 688 |
if (len > UINT8_MAX || len == 0) { |
| 689 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 690 |
"The length of the %s entry '%s' is not in the range 1…255, ignoring.", lvalue, w); |
| 691 |
continue; |
| 692 |
} |
| 693 |
|
| 694 |
r = iovw_consume(iovw, TAKE_PTR(w), len); |
| 695 |
if (r < 0) |
| 696 |
return log_oom(); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
int config_parse_dhcp6_user_or_vendor_class( |
| 701 |
const char *unit, |
| 702 |
const char *filename, |
| 703 |
unsigned line, |
| 704 |
const char *section, |
| 705 |
unsigned section_line, |
| 706 |
const char *lvalue, |
| 707 |
int ltype, |
| 708 |
const char *rvalue, |
| 709 |
void *data, |
| 710 |
void *userdata) { |
| 711 |
|
| 712 |
char ***l = ASSERT_PTR(data); |
| 713 |
int r; |
| 714 |
|
| 715 |
assert(lvalue); |
| 716 |
assert(rvalue); |
| 717 |
|
| 718 |
if (isempty(rvalue)) { |
| 719 |
*l = strv_free(*l); |
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
for (const char *p = rvalue;;) { |
| 724 |
_cleanup_free_ char *w = NULL; |
| 725 |
|
| 726 |
r = extract_first_word(&p, &w, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE); |
| 727 |
if (r < 0) |
| 728 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 729 |
if (r == 0) |
| 730 |
return 0; |
| 731 |
|
| 732 |
size_t len = strlen(w); |
| 733 |
if (len > UINT16_MAX || len == 0) { |
| 734 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 735 |
"The length of the %s entry '%s' is not in the range 1…65535, ignoring.", lvalue, w); |
| 736 |
continue; |
| 737 |
} |
| 738 |
|
| 739 |
r = strv_consume(l, TAKE_PTR(w)); |
| 740 |
if (r < 0) |
| 741 |
return log_oom(); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
int config_parse_dhcp6_send_option( |
| 746 |
const char *unit, |
| 747 |
const char *filename, |
| 748 |
unsigned line, |
| 749 |
const char *section, |
| 750 |
unsigned section_line, |
| 751 |
const char *lvalue, |
| 752 |
int ltype, |
| 753 |
const char *rvalue, |
| 754 |
void *data, |
| 755 |
void *userdata) { |
| 756 |
|
| 757 |
_cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *opt6 = NULL; |
| 758 |
_unused_ _cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *old6 = NULL; |
| 759 |
uint32_t uint32_data, enterprise_identifier = 0; |
| 760 |
_cleanup_free_ char *word = NULL, *q = NULL; |
| 761 |
OrderedHashmap **dhcp6_options = ASSERT_PTR(data); |
| 762 |
uint16_t u16, uint16_data; |
| 763 |
union in_addr_union addr; |
| 764 |
DHCPOptionDataType type; |
| 765 |
uint8_t uint8_data; |
| 766 |
const void *udata; |
| 767 |
const char *p; |
| 768 |
ssize_t sz; |
| 769 |
int r; |
| 770 |
|
| 771 |
assert(filename); |
| 772 |
assert(lvalue); |
| 773 |
assert(rvalue); |
| 774 |
|
| 775 |
if (isempty(rvalue)) { |
| 776 |
*dhcp6_options = ordered_hashmap_free(*dhcp6_options); |
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
p = rvalue; |
| 781 |
if (streq(lvalue, "SendVendorOption")) { |
| 782 |
r = extract_first_word(&p, &word, ":", 0); |
| 783 |
if (r == -ENOMEM) |
| 784 |
return log_oom(); |
| 785 |
if (r <= 0 || isempty(p)) { |
| 786 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 787 |
"Invalid DHCP option, ignoring assignment: %s", rvalue); |
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
r = safe_atou32(word, &enterprise_identifier); |
| 792 |
if (r < 0) { |
| 793 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 794 |
"Failed to parse DHCPv6 enterprise identifier data, ignoring assignment: %s", p); |
| 795 |
return 0; |
| 796 |
} |
| 797 |
word = mfree(word); |
| 798 |
} |
| 799 |
|
| 800 |
r = extract_first_word(&p, &word, ":", 0); |
| 801 |
if (r == -ENOMEM) |
| 802 |
return log_oom(); |
| 803 |
if (r <= 0 || isempty(p)) { |
| 804 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 805 |
"Invalid DHCP option, ignoring assignment: %s", rvalue); |
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
r = safe_atou16(word, &u16); |
| 810 |
if (r < 0) { |
| 811 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 812 |
"Invalid DHCP option, ignoring assignment: %s", rvalue); |
| 813 |
return 0; |
| 814 |
} |
| 815 |
if (u16 < 1 || u16 >= UINT16_MAX) { |
| 816 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 817 |
"Invalid DHCP option, valid range is 1-65535, ignoring assignment: %s", rvalue); |
| 818 |
return 0; |
| 819 |
} |
| 820 |
|
| 821 |
word = mfree(word); |
| 822 |
r = extract_first_word(&p, &word, ":", 0); |
| 823 |
if (r == -ENOMEM) |
| 824 |
return log_oom(); |
| 825 |
if (r <= 0 || isempty(p)) { |
| 826 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 827 |
"Invalid DHCP option, ignoring assignment: %s", rvalue); |
| 828 |
return 0; |
| 829 |
} |
| 830 |
|
| 831 |
type = dhcp_option_data_type_from_string(word); |
| 832 |
if (type < 0) { |
| 833 |
log_syntax(unit, LOG_WARNING, filename, line, type, |
| 834 |
"Invalid DHCP option data type, ignoring assignment: %s", p); |
| 835 |
return 0; |
| 836 |
} |
| 837 |
|
| 838 |
switch (type) { |
| 839 |
case DHCP_OPTION_DATA_UINT8:{ |
| 840 |
r = safe_atou8(p, &uint8_data); |
| 841 |
if (r < 0) { |
| 842 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 843 |
"Failed to parse DHCP uint8 data, ignoring assignment: %s", p); |
| 844 |
return 0; |
| 845 |
} |
| 846 |
|
| 847 |
udata = &uint8_data; |
| 848 |
sz = sizeof(uint8_t); |
| 849 |
break; |
| 850 |
} |
| 851 |
case DHCP_OPTION_DATA_UINT16:{ |
| 852 |
uint16_t k; |
| 853 |
|
| 854 |
r = safe_atou16(p, &k); |
| 855 |
if (r < 0) { |
| 856 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 857 |
"Failed to parse DHCP uint16 data, ignoring assignment: %s", p); |
| 858 |
return 0; |
| 859 |
} |
| 860 |
|
| 861 |
uint16_data = htobe16(k); |
| 862 |
udata = &uint16_data; |
| 863 |
sz = sizeof(uint16_t); |
| 864 |
break; |
| 865 |
} |
| 866 |
case DHCP_OPTION_DATA_UINT32: { |
| 867 |
uint32_t k; |
| 868 |
|
| 869 |
r = safe_atou32(p, &k); |
| 870 |
if (r < 0) { |
| 871 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 872 |
"Failed to parse DHCP uint32 data, ignoring assignment: %s", p); |
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
uint32_data = htobe32(k); |
| 877 |
udata = &uint32_data; |
| 878 |
sz = sizeof(uint32_t); |
| 879 |
|
| 880 |
break; |
| 881 |
} |
| 882 |
case DHCP_OPTION_DATA_IPV4ADDRESS: { |
| 883 |
r = in_addr_from_string(AF_INET, p, &addr); |
| 884 |
if (r < 0) { |
| 885 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 886 |
"Failed to parse DHCP ipv4address data, ignoring assignment: %s", p); |
| 887 |
return 0; |
| 888 |
} |
| 889 |
|
| 890 |
udata = &addr.in; |
| 891 |
sz = sizeof(addr.in.s_addr); |
| 892 |
break; |
| 893 |
} |
| 894 |
case DHCP_OPTION_DATA_IPV6ADDRESS: { |
| 895 |
r = in_addr_from_string(AF_INET6, p, &addr); |
| 896 |
if (r < 0) { |
| 897 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 898 |
"Failed to parse DHCP ipv6address data, ignoring assignment: %s", p); |
| 899 |
return 0; |
| 900 |
} |
| 901 |
|
| 902 |
udata = &addr.in6; |
| 903 |
sz = sizeof(addr.in6.s6_addr); |
| 904 |
break; |
| 905 |
} |
| 906 |
case DHCP_OPTION_DATA_STRING: |
| 907 |
sz = cunescape(p, UNESCAPE_ACCEPT_NUL, &q); |
| 908 |
if (sz < 0) { |
| 909 |
log_syntax(unit, LOG_WARNING, filename, line, sz, |
| 910 |
"Failed to decode DHCP option data, ignoring assignment: %s", p); |
| 911 |
return 0; |
| 912 |
} |
| 913 |
|
| 914 |
udata = q; |
| 915 |
break; |
| 916 |
default: |
| 917 |
return -EINVAL; |
| 918 |
} |
| 919 |
|
| 920 |
r = sd_dhcp6_option_new(u16, udata, sz, enterprise_identifier, &opt6); |
| 921 |
if (r < 0) { |
| 922 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 923 |
"Failed to store DHCP option '%s', ignoring assignment: %m", rvalue); |
| 924 |
return 0; |
| 925 |
} |
| 926 |
|
| 927 |
r = ordered_hashmap_ensure_allocated(dhcp6_options, &dhcp6_option_hash_ops); |
| 928 |
if (r < 0) |
| 929 |
return log_oom(); |
| 930 |
|
| 931 |
/* Overwrite existing option */ |
| 932 |
old6 = ordered_hashmap_get(*dhcp6_options, UINT_TO_PTR(u16)); |
| 933 |
r = ordered_hashmap_replace(*dhcp6_options, UINT_TO_PTR(u16), opt6); |
| 934 |
if (r < 0) { |
| 935 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 936 |
"Failed to store DHCP option '%s', ignoring assignment: %m", rvalue); |
| 937 |
return 0; |
| 938 |
} |
| 939 |
TAKE_PTR(opt6); |
| 940 |
|
| 941 |
return 0; |
| 942 |
} |
| 943 |
|
| 944 |
int config_parse_dhcp_option( |
| 945 |
const char *unit, |
| 946 |
const char *filename, |
| 947 |
unsigned line, |
| 948 |
const char *section, |
| 949 |
unsigned section_line, |
| 950 |
const char *lvalue, |
| 951 |
int ltype, |
| 952 |
const char *rvalue, |
| 953 |
void *data, |
| 954 |
void *userdata) { |
| 955 |
|
| 956 |
struct iovec *iov = ASSERT_PTR(data); |
| 957 |
bool check_length = ltype; |
| 958 |
int r; |
| 959 |
|
| 960 |
if (isempty(rvalue)) { |
| 961 |
iovec_done(iov); |
| 962 |
return 0; |
| 963 |
} |
| 964 |
|
| 965 |
_cleanup_free_ char *word = NULL; |
| 966 |
const char *p = rvalue; |
| 967 |
r = extract_first_word(&p, &word, ":", 0); |
| 968 |
if (r == -ENOMEM) |
| 969 |
return log_oom(); |
| 970 |
if (r <= 0 || isempty(p)) |
| 971 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 972 |
|
| 973 |
DHCPOptionDataType type = dhcp_option_data_type_from_string(word); |
| 974 |
if (type < 0) |
| 975 |
return log_syntax_parse_error(unit, filename, line, type, lvalue, rvalue); |
| 976 |
|
| 977 |
switch (type) { |
| 978 |
case DHCP_OPTION_DATA_UINT8:{ |
| 979 |
uint8_t u; |
| 980 |
|
| 981 |
r = safe_atou8(p, &u); |
| 982 |
if (r < 0) |
| 983 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 984 |
|
| 985 |
r = iovec_done_and_memdup(iov, &IOVEC_MAKE(&u, sizeof(u))); |
| 986 |
if (r < 0) |
| 987 |
return log_oom(); |
| 988 |
|
| 989 |
return 1; |
| 990 |
} |
| 991 |
case DHCP_OPTION_DATA_UINT16:{ |
| 992 |
uint16_t u; |
| 993 |
|
| 994 |
r = safe_atou16(p, &u); |
| 995 |
if (r < 0) |
| 996 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 997 |
|
| 998 |
u = htobe16(u); |
| 999 |
r = iovec_done_and_memdup(iov, &IOVEC_MAKE(&u, sizeof(u))); |
| 1000 |
if (r < 0) |
| 1001 |
return log_oom(); |
| 1002 |
|
| 1003 |
return 1; |
| 1004 |
} |
| 1005 |
case DHCP_OPTION_DATA_UINT32: { |
| 1006 |
uint32_t u; |
| 1007 |
|
| 1008 |
r = safe_atou32(p, &u); |
| 1009 |
if (r < 0) |
| 1010 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1011 |
|
| 1012 |
u = htobe32(u); |
| 1013 |
r = iovec_done_and_memdup(iov, &IOVEC_MAKE(&u, sizeof(u))); |
| 1014 |
if (r < 0) |
| 1015 |
return log_oom(); |
| 1016 |
|
| 1017 |
return 1; |
| 1018 |
} |
| 1019 |
case DHCP_OPTION_DATA_IPV4ADDRESS: { |
| 1020 |
union in_addr_union a; |
| 1021 |
|
| 1022 |
r = in_addr_from_string(AF_INET, p, &a); |
| 1023 |
if (r < 0) |
| 1024 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1025 |
|
| 1026 |
r = iovec_done_and_memdup(iov, &IOVEC_MAKE(&a.in, sizeof(a.in))); |
| 1027 |
if (r < 0) |
| 1028 |
return log_oom(); |
| 1029 |
|
| 1030 |
return 1; |
| 1031 |
} |
| 1032 |
case DHCP_OPTION_DATA_IPV6ADDRESS: { |
| 1033 |
union in_addr_union a; |
| 1034 |
|
| 1035 |
r = in_addr_from_string(AF_INET6, p, &a); |
| 1036 |
if (r < 0) |
| 1037 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1038 |
|
| 1039 |
r = iovec_done_and_memdup(iov, &IOVEC_MAKE(&a.in6, sizeof(a.in6))); |
| 1040 |
if (r < 0) |
| 1041 |
return log_oom(); |
| 1042 |
|
| 1043 |
return 1; |
| 1044 |
} |
| 1045 |
case DHCP_OPTION_DATA_STRING: { |
| 1046 |
_cleanup_free_ char *s = NULL; |
| 1047 |
ssize_t sz = cunescape(p, UNESCAPE_ACCEPT_NUL, &s); |
| 1048 |
if (sz < 0) |
| 1049 |
return log_syntax_parse_error(unit, filename, line, sz, lvalue, rvalue); |
| 1050 |
if (check_length && sz > UINT8_MAX) |
| 1051 |
return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue); |
| 1052 |
|
| 1053 |
iovec_done(iov); |
| 1054 |
*iov = IOVEC_MAKE(TAKE_PTR(s), sz); |
| 1055 |
return 1; |
| 1056 |
} |
| 1057 |
default: |
| 1058 |
return -EINVAL; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
int config_parse_dhcp_option_tlv( |
| 1063 |
const char *unit, |
| 1064 |
const char *filename, |
| 1065 |
unsigned line, |
| 1066 |
const char *section, |
| 1067 |
unsigned section_line, |
| 1068 |
const char *lvalue, |
| 1069 |
int ltype, |
| 1070 |
const char *rvalue, |
| 1071 |
void *data, |
| 1072 |
void *userdata) { |
| 1073 |
|
| 1074 |
TLV *options = ASSERT_PTR(data); |
| 1075 |
int r; |
| 1076 |
|
| 1077 |
if (isempty(rvalue)) { |
| 1078 |
tlv_done(options); |
| 1079 |
return 0; |
| 1080 |
} |
| 1081 |
|
| 1082 |
_cleanup_free_ char *word = NULL; |
| 1083 |
const char *p = rvalue; |
| 1084 |
r = extract_first_word(&p, &word, ":", 0); |
| 1085 |
if (r == -ENOMEM) |
| 1086 |
return log_oom(); |
| 1087 |
if (r <= 0 || isempty(p)) |
| 1088 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1089 |
|
| 1090 |
uint8_t code; |
| 1091 |
r = safe_atou8(word, &code); |
| 1092 |
if (r < 0) |
| 1093 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1094 |
if (code < 1 || code >= UINT8_MAX) |
| 1095 |
return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue); |
| 1096 |
|
| 1097 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 1098 |
r = config_parse_dhcp_option(unit, filename, line, section, section_line, lvalue, ltype, p, &iov, userdata); |
| 1099 |
if (r <= 0) |
| 1100 |
return r; |
| 1101 |
|
| 1102 |
r = tlv_append_iov(options, code, &iov); |
| 1103 |
if (r < 0) |
| 1104 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1105 |
"Failed to store '%s=%s', ignoring assignment: %m", lvalue, rvalue); |
| 1106 |
|
| 1107 |
return 0; |
| 1108 |
} |
| 1109 |
|
| 1110 |
int config_parse_dhcp_request_options( |
| 1111 |
const char *unit, |
| 1112 |
const char *filename, |
| 1113 |
unsigned line, |
| 1114 |
const char *section, |
| 1115 |
unsigned section_line, |
| 1116 |
const char *lvalue, |
| 1117 |
int ltype, |
| 1118 |
const char *rvalue, |
| 1119 |
void *data, |
| 1120 |
void *userdata) { |
| 1121 |
|
| 1122 |
Network *network = userdata; |
| 1123 |
int r; |
| 1124 |
|
| 1125 |
assert(filename); |
| 1126 |
assert(lvalue); |
| 1127 |
assert(rvalue); |
| 1128 |
assert(data); |
| 1129 |
|
| 1130 |
if (isempty(rvalue)) { |
| 1131 |
if (ltype == AF_INET) |
| 1132 |
network->dhcp_request_options = set_free(network->dhcp_request_options); |
| 1133 |
else |
| 1134 |
network->dhcp6_request_options = set_free(network->dhcp6_request_options); |
| 1135 |
|
| 1136 |
return 0; |
| 1137 |
} |
| 1138 |
|
| 1139 |
for (const char *p = rvalue;;) { |
| 1140 |
_cleanup_free_ char *n = NULL; |
| 1141 |
uint32_t i; |
| 1142 |
|
| 1143 |
r = extract_first_word(&p, &n, NULL, 0); |
| 1144 |
if (r == -ENOMEM) |
| 1145 |
return log_oom(); |
| 1146 |
if (r < 0) { |
| 1147 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1148 |
"Failed to parse DHCP request option, ignoring assignment: %s", |
| 1149 |
rvalue); |
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
if (r == 0) |
| 1153 |
return 0; |
| 1154 |
|
| 1155 |
r = safe_atou32(n, &i); |
| 1156 |
if (r < 0) { |
| 1157 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1158 |
"DHCP request option is invalid, ignoring assignment: %s", n); |
| 1159 |
continue; |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (i < 1 || i >= UINT8_MAX) { |
| 1163 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1164 |
"DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n); |
| 1165 |
continue; |
| 1166 |
} |
| 1167 |
|
| 1168 |
r = set_ensure_put(ltype == AF_INET ? &network->dhcp_request_options : &network->dhcp6_request_options, |
| 1169 |
NULL, UINT32_TO_PTR(i)); |
| 1170 |
if (r < 0) |
| 1171 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1172 |
"Failed to store DHCP request option '%s', ignoring assignment: %m", n); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
static const char * const dhcp_option_data_type_table[_DHCP_OPTION_DATA_MAX] = { |
| 1177 |
[DHCP_OPTION_DATA_UINT8] = "uint8", |
| 1178 |
[DHCP_OPTION_DATA_UINT16] = "uint16", |
| 1179 |
[DHCP_OPTION_DATA_UINT32] = "uint32", |
| 1180 |
[DHCP_OPTION_DATA_STRING] = "string", |
| 1181 |
[DHCP_OPTION_DATA_IPV4ADDRESS] = "ipv4address", |
| 1182 |
[DHCP_OPTION_DATA_IPV6ADDRESS] = "ipv6address", |
| 1183 |
}; |
| 1184 |
|
| 1185 |
DEFINE_STRING_TABLE_LOOKUP(dhcp_option_data_type, DHCPOptionDataType); |
| 1186 |
|
| 1187 |
static const char* const duid_type_table[_DUID_TYPE_MAX] = { |
| 1188 |
[DUID_TYPE_LLT] = "link-layer-time", |
| 1189 |
[DUID_TYPE_EN] = "vendor", |
| 1190 |
[DUID_TYPE_LL] = "link-layer", |
| 1191 |
[DUID_TYPE_UUID] = "uuid", |
| 1192 |
}; |
| 1193 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type, DUIDType); |
| 1194 |
|
| 1195 |
int config_parse_duid_type( |
| 1196 |
const char *unit, |
| 1197 |
const char *filename, |
| 1198 |
unsigned line, |
| 1199 |
const char *section, |
| 1200 |
unsigned section_line, |
| 1201 |
const char *lvalue, |
| 1202 |
int ltype, |
| 1203 |
const char *rvalue, |
| 1204 |
void *data, |
| 1205 |
void *userdata) { |
| 1206 |
|
| 1207 |
_cleanup_free_ char *type_string = NULL; |
| 1208 |
const char *p = ASSERT_PTR(rvalue); |
| 1209 |
bool force = ltype; |
| 1210 |
DUID *duid = ASSERT_PTR(data); |
| 1211 |
DUIDType type; |
| 1212 |
int r; |
| 1213 |
|
| 1214 |
assert(filename); |
| 1215 |
assert(lvalue); |
| 1216 |
|
| 1217 |
if (!force && duid->set) |
| 1218 |
return 0; |
| 1219 |
|
| 1220 |
r = extract_first_word(&p, &type_string, ":", 0); |
| 1221 |
if (r == -ENOMEM) |
| 1222 |
return log_oom(); |
| 1223 |
if (r < 0) { |
| 1224 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1225 |
"Invalid syntax, ignoring: %s", rvalue); |
| 1226 |
return 0; |
| 1227 |
} |
| 1228 |
if (r == 0) { |
| 1229 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1230 |
"Failed to extract DUID type from '%s', ignoring.", rvalue); |
| 1231 |
return 0; |
| 1232 |
} |
| 1233 |
|
| 1234 |
type = duid_type_from_string(type_string); |
| 1235 |
if (type < 0) { |
| 1236 |
uint16_t t; |
| 1237 |
|
| 1238 |
r = safe_atou16(type_string, &t); |
| 1239 |
if (r < 0) { |
| 1240 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1241 |
"Failed to parse DUID type '%s', ignoring.", type_string); |
| 1242 |
return 0; |
| 1243 |
} |
| 1244 |
|
| 1245 |
type = t; |
| 1246 |
assert(type == t); /* Check if type can store uint16_t. */ |
| 1247 |
} |
| 1248 |
|
| 1249 |
if (!isempty(p)) { |
| 1250 |
usec_t u; |
| 1251 |
|
| 1252 |
if (type != DUID_TYPE_LLT) { |
| 1253 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1254 |
"Invalid syntax, ignoring: %s", rvalue); |
| 1255 |
return 0; |
| 1256 |
} |
| 1257 |
|
| 1258 |
r = parse_timestamp(p, &u); |
| 1259 |
if (r < 0) { |
| 1260 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1261 |
"Failed to parse timestamp, ignoring: %s", p); |
| 1262 |
return 0; |
| 1263 |
} |
| 1264 |
|
| 1265 |
duid->llt_time = u; |
| 1266 |
} |
| 1267 |
|
| 1268 |
duid->type = type; |
| 1269 |
duid->set = force; |
| 1270 |
|
| 1271 |
return 0; |
| 1272 |
} |
| 1273 |
|
| 1274 |
int config_parse_manager_duid_type( |
| 1275 |
const char *unit, |
| 1276 |
const char *filename, |
| 1277 |
unsigned line, |
| 1278 |
const char *section, |
| 1279 |
unsigned section_line, |
| 1280 |
const char *lvalue, |
| 1281 |
int ltype, |
| 1282 |
const char *rvalue, |
| 1283 |
void *data, |
| 1284 |
void *userdata) { |
| 1285 |
|
| 1286 |
Manager *manager = ASSERT_PTR(userdata); |
| 1287 |
int r; |
| 1288 |
|
| 1289 |
/* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */ |
| 1290 |
|
| 1291 |
r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager); |
| 1292 |
if (r < 0) |
| 1293 |
return r; |
| 1294 |
|
| 1295 |
return config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp6_duid, manager); |
| 1296 |
} |
| 1297 |
|
| 1298 |
int config_parse_network_duid_type( |
| 1299 |
const char *unit, |
| 1300 |
const char *filename, |
| 1301 |
unsigned line, |
| 1302 |
const char *section, |
| 1303 |
unsigned section_line, |
| 1304 |
const char *lvalue, |
| 1305 |
int ltype, |
| 1306 |
const char *rvalue, |
| 1307 |
void *data, |
| 1308 |
void *userdata) { |
| 1309 |
|
| 1310 |
Network *network = ASSERT_PTR(userdata); |
| 1311 |
int r; |
| 1312 |
|
| 1313 |
r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network); |
| 1314 |
if (r < 0) |
| 1315 |
return r; |
| 1316 |
|
| 1317 |
/* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */ |
| 1318 |
return config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network); |
| 1319 |
} |
| 1320 |
|
| 1321 |
int config_parse_duid_rawdata( |
| 1322 |
const char *unit, |
| 1323 |
const char *filename, |
| 1324 |
unsigned line, |
| 1325 |
const char *section, |
| 1326 |
unsigned section_line, |
| 1327 |
const char *lvalue, |
| 1328 |
int ltype, |
| 1329 |
const char *rvalue, |
| 1330 |
void *data, |
| 1331 |
void *userdata) { |
| 1332 |
|
| 1333 |
uint8_t raw_data[MAX_DUID_DATA_LEN]; |
| 1334 |
unsigned count = 0; |
| 1335 |
bool force = ltype; |
| 1336 |
DUID *duid = ASSERT_PTR(data); |
| 1337 |
|
| 1338 |
assert(filename); |
| 1339 |
assert(lvalue); |
| 1340 |
assert(rvalue); |
| 1341 |
|
| 1342 |
if (!force && duid->set) |
| 1343 |
return 0; |
| 1344 |
|
| 1345 |
/* RawData contains DUID in format "NN:NN:NN..." */ |
| 1346 |
for (const char *p = rvalue;;) { |
| 1347 |
int n1, n2, len, r; |
| 1348 |
uint32_t byte; |
| 1349 |
_cleanup_free_ char *cbyte = NULL; |
| 1350 |
|
| 1351 |
r = extract_first_word(&p, &cbyte, ":", 0); |
| 1352 |
if (r == -ENOMEM) |
| 1353 |
return log_oom(); |
| 1354 |
if (r < 0) { |
| 1355 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to read DUID, ignoring assignment: %s.", rvalue); |
| 1356 |
return 0; |
| 1357 |
} |
| 1358 |
if (r == 0) |
| 1359 |
break; |
| 1360 |
|
| 1361 |
if (count >= MAX_DUID_DATA_LEN) { |
| 1362 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue); |
| 1363 |
return 0; |
| 1364 |
} |
| 1365 |
|
| 1366 |
len = strlen(cbyte); |
| 1367 |
if (!IN_SET(len, 1, 2)) { |
| 1368 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid length - DUID byte: %s, ignoring assignment: %s.", cbyte, rvalue); |
| 1369 |
return 0; |
| 1370 |
} |
| 1371 |
n1 = unhexchar(cbyte[0]); |
| 1372 |
if (len == 2) |
| 1373 |
n2 = unhexchar(cbyte[1]); |
| 1374 |
else |
| 1375 |
n2 = 0; |
| 1376 |
|
| 1377 |
if (n1 < 0 || n2 < 0) { |
| 1378 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid DUID byte: %s. Ignoring assignment: %s.", cbyte, rvalue); |
| 1379 |
return 0; |
| 1380 |
} |
| 1381 |
|
| 1382 |
byte = ((uint8_t) n1 << (4 * (len-1))) | (uint8_t) n2; |
| 1383 |
raw_data[count++] = byte; |
| 1384 |
} |
| 1385 |
|
| 1386 |
assert_cc(sizeof(raw_data) == sizeof(duid->raw_data)); |
| 1387 |
memcpy(duid->raw_data, raw_data, count); |
| 1388 |
duid->raw_data_len = count; |
| 1389 |
duid->set = force; |
| 1390 |
|
| 1391 |
return 0; |
| 1392 |
} |
| 1393 |
|
| 1394 |
int config_parse_manager_duid_rawdata( |
| 1395 |
const char *unit, |
| 1396 |
const char *filename, |
| 1397 |
unsigned line, |
| 1398 |
const char *section, |
| 1399 |
unsigned section_line, |
| 1400 |
const char *lvalue, |
| 1401 |
int ltype, |
| 1402 |
const char *rvalue, |
| 1403 |
void *data, |
| 1404 |
void *userdata) { |
| 1405 |
|
| 1406 |
Manager *manager = ASSERT_PTR(userdata); |
| 1407 |
int r; |
| 1408 |
|
| 1409 |
/* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */ |
| 1410 |
|
| 1411 |
r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager); |
| 1412 |
if (r < 0) |
| 1413 |
return r; |
| 1414 |
|
| 1415 |
return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp6_duid, manager); |
| 1416 |
} |
| 1417 |
|
| 1418 |
int config_parse_network_duid_rawdata( |
| 1419 |
const char *unit, |
| 1420 |
const char *filename, |
| 1421 |
unsigned line, |
| 1422 |
const char *section, |
| 1423 |
unsigned section_line, |
| 1424 |
const char *lvalue, |
| 1425 |
int ltype, |
| 1426 |
const char *rvalue, |
| 1427 |
void *data, |
| 1428 |
void *userdata) { |
| 1429 |
|
| 1430 |
Network *network = ASSERT_PTR(userdata); |
| 1431 |
int r; |
| 1432 |
|
| 1433 |
r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network); |
| 1434 |
if (r < 0) |
| 1435 |
return r; |
| 1436 |
|
| 1437 |
/* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */ |
| 1438 |
return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network); |
| 1439 |
} |
| 1440 |
|
| 1441 |
int config_parse_uplink( |
| 1442 |
const char *unit, |
| 1443 |
const char *filename, |
| 1444 |
unsigned line, |
| 1445 |
const char *section, |
| 1446 |
unsigned section_line, |
| 1447 |
const char *lvalue, |
| 1448 |
int ltype, |
| 1449 |
const char *rvalue, |
| 1450 |
void *data, |
| 1451 |
void *userdata) { |
| 1452 |
|
| 1453 |
Network *network = ASSERT_PTR(userdata); |
| 1454 |
bool accept_none = true; |
| 1455 |
int *index, r; |
| 1456 |
char **name; |
| 1457 |
|
| 1458 |
assert(filename); |
| 1459 |
assert(section); |
| 1460 |
assert(lvalue); |
| 1461 |
assert(rvalue); |
| 1462 |
|
| 1463 |
if (streq(section, "DHCPServer")) { |
| 1464 |
index = &network->dhcp_server_uplink_index; |
| 1465 |
name = &network->dhcp_server_uplink_name; |
| 1466 |
} else if (streq(section, "IPv6SendRA")) { |
| 1467 |
index = &network->router_uplink_index; |
| 1468 |
name = &network->router_uplink_name; |
| 1469 |
} else if (STR_IN_SET(section, "DHCPv6PrefixDelegation", "DHCPPrefixDelegation")) { |
| 1470 |
index = &network->dhcp_pd_uplink_index; |
| 1471 |
name = &network->dhcp_pd_uplink_name; |
| 1472 |
accept_none = false; |
| 1473 |
} else |
| 1474 |
assert_not_reached(); |
| 1475 |
|
| 1476 |
if (isempty(rvalue) || streq(rvalue, ":auto")) { |
| 1477 |
*index = UPLINK_INDEX_AUTO; |
| 1478 |
*name = mfree(*name); |
| 1479 |
return 0; |
| 1480 |
} |
| 1481 |
|
| 1482 |
if (accept_none && streq(rvalue, ":none")) { |
| 1483 |
*index = UPLINK_INDEX_NONE; |
| 1484 |
*name = mfree(*name); |
| 1485 |
return 0; |
| 1486 |
} |
| 1487 |
|
| 1488 |
if (!accept_none && streq(rvalue, ":self")) { |
| 1489 |
*index = UPLINK_INDEX_SELF; |
| 1490 |
*name = mfree(*name); |
| 1491 |
return 0; |
| 1492 |
} |
| 1493 |
|
| 1494 |
r = parse_ifindex(rvalue); |
| 1495 |
if (r > 0) { |
| 1496 |
*index = r; |
| 1497 |
*name = mfree(*name); |
| 1498 |
return 0; |
| 1499 |
} |
| 1500 |
|
| 1501 |
if (!ifname_valid_full(rvalue, IFNAME_VALID_ALTERNATIVE)) { |
| 1502 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1503 |
"Invalid interface name in %s=, ignoring assignment: %s", lvalue, rvalue); |
| 1504 |
return 0; |
| 1505 |
} |
| 1506 |
|
| 1507 |
/* The interface name will be resolved later. */ |
| 1508 |
r = free_and_strdup_warn(name, rvalue); |
| 1509 |
if (r < 0) |
| 1510 |
return r; |
| 1511 |
|
| 1512 |
/* Note, if uplink_name is set, then uplink_index will be ignored. So, the below does not mean |
| 1513 |
* an uplink interface will be selected automatically. */ |
| 1514 |
*index = UPLINK_INDEX_AUTO; |
| 1515 |
return 0; |
| 1516 |
} |