| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <net/if_arp.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "dhcp-client-id-internal.h" |
| 7 |
#include "dhcp-message.h" |
| 8 |
#include "dhcp-route.h" |
| 9 |
#include "dns-def.h" |
| 10 |
#include "dns-domain.h" |
| 11 |
#include "dns-resolver-internal.h" |
| 12 |
#include "errno-util.h" |
| 13 |
#include "ether-addr-util.h" |
| 14 |
#include "hostname-util.h" |
| 15 |
#include "in-addr-util.h" |
| 16 |
#include "iovec-util.h" |
| 17 |
#include "iovec-wrapper.h" |
| 18 |
#include "ip-util.h" |
| 19 |
#include "json-util.h" |
| 20 |
#include "network-common.h" |
| 21 |
#include "set.h" |
| 22 |
#include "socket-util.h" |
| 23 |
#include "sort-util.h" |
| 24 |
#include "string-util.h" |
| 25 |
#include "unaligned.h" |
| 26 |
|
| 27 |
static sd_dhcp_message* dhcp_message_free(sd_dhcp_message *message) { |
| 28 |
if (!message) |
| 29 |
return NULL; |
| 30 |
|
| 31 |
tlv_done(&message->options); |
| 32 |
return mfree(message); |
| 33 |
} |
| 34 |
|
| 35 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_message, sd_dhcp_message, dhcp_message_free); |
| 36 |
|
| 37 |
int dhcp_message_new(sd_dhcp_message **ret) { |
| 38 |
assert(ret); |
| 39 |
|
| 40 |
sd_dhcp_message *message = new(sd_dhcp_message, 1); |
| 41 |
if (!message) |
| 42 |
return -ENOMEM; |
| 43 |
|
| 44 |
*message = (sd_dhcp_message) { |
| 45 |
.n_ref = 1, |
| 46 |
.options = TLV_INIT(TLV_DHCP4), |
| 47 |
}; |
| 48 |
|
| 49 |
*ret = TAKE_PTR(message); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
int dhcp_message_init_header( |
| 54 |
sd_dhcp_message *message, |
| 55 |
uint8_t op, |
| 56 |
uint32_t xid, |
| 57 |
uint16_t arp_type, |
| 58 |
const struct hw_addr_data *hw_addr) { |
| 59 |
|
| 60 |
assert(message); |
| 61 |
assert(IN_SET(op, BOOTREQUEST, BOOTREPLY)); |
| 62 |
|
| 63 |
/* RFC 2131 section 4.1.1: |
| 64 |
* The client MUST include its hardware address in the ’chaddr’ field, if necessary for delivery of |
| 65 |
* DHCP reply messages. |
| 66 |
* |
| 67 |
* RFC 4390 section 2.1: |
| 68 |
* A DHCP client, when working over an IPoIB interface, MUST follow the following rules: |
| 69 |
* "htype" (hardware address type) MUST be 32 [ARPPARAM]. |
| 70 |
* "hlen" (hardware address length) MUST be 0. |
| 71 |
* "chaddr" (client hardware address) field MUST be zeroed. |
| 72 |
* |
| 73 |
* Note, the maximum hardware address length (HW_ADDR_MAX_SIZE) is 32, but the size of the chaddr |
| 74 |
* field is 16. |
| 75 |
* |
| 76 |
* Also, ARP type is 2 bytes, but the htype field is 1 byte. */ |
| 77 |
|
| 78 |
if (arp_type == ARPHRD_INFINIBAND) |
| 79 |
hw_addr = NULL; |
| 80 |
|
| 81 |
if (hw_addr && hw_addr->length > sizeof_field(DHCPMessageHeader, chaddr)) |
| 82 |
return -EINVAL; |
| 83 |
|
| 84 |
message->header = (DHCPMessageHeader) { |
| 85 |
.op = op, |
| 86 |
.htype = arp_type <= UINT8_MAX ? arp_type : 0, |
| 87 |
.hlen = hw_addr ? hw_addr->length : 0, |
| 88 |
.xid = htobe32(xid), |
| 89 |
.magic = htobe32(DHCP_MAGIC_COOKIE), |
| 90 |
}; |
| 91 |
|
| 92 |
if (hw_addr) |
| 93 |
memcpy_safe(message->header.chaddr, hw_addr->bytes, hw_addr->length); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
void dhcp_message_set_broadcast_flag(sd_dhcp_message *message, bool b) { |
| 98 |
assert(message); |
| 99 |
|
| 100 |
SET_FLAG(message->header.flags, htobe16(0x8000), b); |
| 101 |
} |
| 102 |
|
| 103 |
bool dhcp_message_has_broadcast_flag(sd_dhcp_message *message) { |
| 104 |
assert(message); |
| 105 |
|
| 106 |
return FLAGS_SET(message->header.flags, htobe16(0x8000)); |
| 107 |
} |
| 108 |
|
| 109 |
int dhcp_message_get_hw_addr(sd_dhcp_message *message, struct hw_addr_data *ret) { |
| 110 |
assert(message); |
| 111 |
assert(ret); |
| 112 |
|
| 113 |
if (message->header.hlen > sizeof_field(DHCPMessageHeader, chaddr)) |
| 114 |
return -EBADMSG; |
| 115 |
|
| 116 |
ret->length = message->header.hlen; |
| 117 |
memcpy_safe(ret->bytes, message->header.chaddr, message->header.hlen); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
bool dhcp_message_has_option(sd_dhcp_message *message, uint8_t code) { |
| 122 |
assert(message); |
| 123 |
return tlv_contains(&message->options, code); |
| 124 |
} |
| 125 |
|
| 126 |
void dhcp_message_remove_option(sd_dhcp_message *message, uint8_t code) { |
| 127 |
assert(message); |
| 128 |
tlv_remove(&message->options, code); |
| 129 |
} |
| 130 |
|
| 131 |
int dhcp_message_append_option(sd_dhcp_message *message, uint8_t code, size_t length, const void *data) { |
| 132 |
assert(message); |
| 133 |
return tlv_append(&message->options, code, length, data); |
| 134 |
} |
| 135 |
|
| 136 |
int dhcp_message_append_option_tlv(sd_dhcp_message *message, const TLV *tlv) { |
| 137 |
assert(message); |
| 138 |
return tlv_append_tlv(&message->options, tlv); |
| 139 |
} |
| 140 |
|
| 141 |
int dhcp_message_append_option_flag(sd_dhcp_message *message, uint8_t code) { |
| 142 |
assert(message); |
| 143 |
|
| 144 |
if (dhcp_message_has_option(message, code)) |
| 145 |
return -EEXIST; |
| 146 |
|
| 147 |
return dhcp_message_append_option(message, code, /* length= */ 0, /* data= */ NULL); |
| 148 |
} |
| 149 |
|
| 150 |
int dhcp_message_append_option_u8(sd_dhcp_message *message, uint8_t code, uint8_t data) { |
| 151 |
assert(message); |
| 152 |
|
| 153 |
if (dhcp_message_has_option(message, code)) |
| 154 |
return -EEXIST; |
| 155 |
|
| 156 |
return dhcp_message_append_option(message, code, sizeof(uint8_t), &data); |
| 157 |
} |
| 158 |
|
| 159 |
int dhcp_message_append_option_u16(sd_dhcp_message *message, uint8_t code, uint16_t data) { |
| 160 |
assert(message); |
| 161 |
|
| 162 |
if (dhcp_message_has_option(message, code)) |
| 163 |
return -EEXIST; |
| 164 |
|
| 165 |
be16_t b = htobe16(data); |
| 166 |
return dhcp_message_append_option(message, code, sizeof(be16_t), &b); |
| 167 |
} |
| 168 |
|
| 169 |
int dhcp_message_append_option_be32(sd_dhcp_message *message, uint8_t code, be32_t data) { |
| 170 |
assert(message); |
| 171 |
|
| 172 |
if (dhcp_message_has_option(message, code)) |
| 173 |
return -EEXIST; |
| 174 |
|
| 175 |
return dhcp_message_append_option(message, code, sizeof(be32_t), &data); |
| 176 |
} |
| 177 |
|
| 178 |
int dhcp_message_append_option_sec(sd_dhcp_message *message, uint8_t code, usec_t usec) { |
| 179 |
assert(message); |
| 180 |
return dhcp_message_append_option_be32(message, code, usec_to_be32_sec(usec)); |
| 181 |
} |
| 182 |
|
| 183 |
int dhcp_message_append_option_address(sd_dhcp_message *message, uint8_t code, const struct in_addr *addr) { |
| 184 |
assert(message); |
| 185 |
assert(addr); |
| 186 |
return dhcp_message_append_option_be32(message, code, addr->s_addr); |
| 187 |
} |
| 188 |
|
| 189 |
int dhcp_message_append_option_addresses(sd_dhcp_message *message, uint8_t code, size_t n_addr, const struct in_addr *addr) { |
| 190 |
assert(message); |
| 191 |
assert(n_addr == 0 || addr); |
| 192 |
|
| 193 |
if (n_addr == 0) |
| 194 |
return 0; |
| 195 |
|
| 196 |
if (size_multiply_overflow(sizeof(struct in_addr), n_addr)) |
| 197 |
return -ENOBUFS; |
| 198 |
|
| 199 |
if (code == SD_DHCP_OPTION_SIP_SERVER) { |
| 200 |
if (dhcp_message_has_option(message, SD_DHCP_OPTION_SIP_SERVER)) |
| 201 |
return -EEXIST; |
| 202 |
|
| 203 |
size_t len = size_add(1, sizeof(struct in_addr) * n_addr); |
| 204 |
if (len == SIZE_MAX) |
| 205 |
return -ENOBUFS; |
| 206 |
|
| 207 |
_cleanup_free_ uint8_t *buf = new(uint8_t, len); |
| 208 |
if (!buf) |
| 209 |
return -ENOMEM; |
| 210 |
|
| 211 |
buf[0] = 1; /* 'enc' field, 0: domains, 1: addresses */ |
| 212 |
memcpy(buf + 1, addr, sizeof(struct in_addr) * n_addr); |
| 213 |
|
| 214 |
return dhcp_message_append_option(message, code, len, buf); |
| 215 |
} |
| 216 |
|
| 217 |
return dhcp_message_append_option(message, code, sizeof(struct in_addr) * n_addr, addr); |
| 218 |
} |
| 219 |
|
| 220 |
int dhcp_message_append_option_string(sd_dhcp_message *message, uint8_t code, const char *data) { |
| 221 |
assert(message); |
| 222 |
|
| 223 |
if (isempty(data)) |
| 224 |
return 0; |
| 225 |
|
| 226 |
if (!string_is_safe(data, STRING_ALLOW_BACKSLASHES | STRING_ALLOW_QUOTES | STRING_ALLOW_GLOBS)) |
| 227 |
return -EINVAL; |
| 228 |
|
| 229 |
if (dhcp_message_has_option(message, code)) |
| 230 |
return -EEXIST; |
| 231 |
|
| 232 |
return dhcp_message_append_option(message, code, strlen(data), data); |
| 233 |
} |
| 234 |
|
| 235 |
static int dhcp_message_append_option_static_routes(sd_dhcp_message *message, size_t n_routes, const sd_dhcp_route *routes) { |
| 236 |
int r; |
| 237 |
|
| 238 |
assert(message); |
| 239 |
assert(routes || n_routes == 0); |
| 240 |
|
| 241 |
if (n_routes == 0) |
| 242 |
return 0; |
| 243 |
|
| 244 |
if (size_multiply_overflow(2 * sizeof(struct in_addr), n_routes)) |
| 245 |
return -ENOBUFS; |
| 246 |
|
| 247 |
_cleanup_free_ struct in_addr *buf = new(struct in_addr, 2 * n_routes); |
| 248 |
if (!buf) |
| 249 |
return -ENOMEM; |
| 250 |
|
| 251 |
size_t count = 0; |
| 252 |
FOREACH_ARRAY(route, routes, n_routes) { |
| 253 |
uint8_t prefixlen; |
| 254 |
r = in4_addr_default_prefixlen(&route->dst_addr, &prefixlen); |
| 255 |
if (r < 0) |
| 256 |
return r; |
| 257 |
|
| 258 |
if (prefixlen != route->dst_prefixlen) |
| 259 |
return -EINVAL; |
| 260 |
|
| 261 |
struct in_addr dst = route->dst_addr; |
| 262 |
(void) in4_addr_mask(&dst, prefixlen); |
| 263 |
|
| 264 |
buf[count++] = dst; |
| 265 |
buf[count++] = route->gw_addr; |
| 266 |
} |
| 267 |
|
| 268 |
assert(count == 2 * n_routes); |
| 269 |
|
| 270 |
return dhcp_message_append_option_addresses(message, SD_DHCP_OPTION_STATIC_ROUTE, 2 * n_routes, buf); |
| 271 |
} |
| 272 |
|
| 273 |
static int dhcp_message_append_option_classless_static_routes(sd_dhcp_message *message, uint8_t code, size_t n_routes, const sd_dhcp_route *routes) { |
| 274 |
assert(message); |
| 275 |
assert(routes || n_routes == 0); |
| 276 |
assert(IN_SET(code, |
| 277 |
SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE, |
| 278 |
SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE)); |
| 279 |
|
| 280 |
if (n_routes == 0) |
| 281 |
return 0; |
| 282 |
|
| 283 |
if (size_multiply_overflow(1 + 2 * sizeof(struct in_addr), n_routes)) |
| 284 |
return -ENOBUFS; |
| 285 |
|
| 286 |
_cleanup_free_ uint8_t *buf = new(uint8_t, (1 + 2 * sizeof(struct in_addr)) * n_routes); |
| 287 |
if (!buf) |
| 288 |
return -ENOMEM; |
| 289 |
|
| 290 |
uint8_t *p = buf; |
| 291 |
FOREACH_ARRAY(route, routes, n_routes) { |
| 292 |
if (route->dst_prefixlen > sizeof(struct in_addr) * 8) |
| 293 |
return -EINVAL; |
| 294 |
|
| 295 |
*p++ = route->dst_prefixlen; |
| 296 |
struct in_addr dst = route->dst_addr; |
| 297 |
(void) in4_addr_mask(&dst, route->dst_prefixlen); |
| 298 |
p = mempcpy(p, &dst, DIV_ROUND_UP(route->dst_prefixlen, 8)); |
| 299 |
p = mempcpy(p, &route->gw_addr, sizeof(struct in_addr)); |
| 300 |
} |
| 301 |
|
| 302 |
return dhcp_message_append_option(message, code, p - buf, buf); |
| 303 |
} |
| 304 |
|
| 305 |
int dhcp_message_append_option_routes(sd_dhcp_message *message, uint8_t code, size_t n_routes, const sd_dhcp_route *routes) { |
| 306 |
switch (code) { |
| 307 |
case SD_DHCP_OPTION_STATIC_ROUTE: |
| 308 |
return dhcp_message_append_option_static_routes(message, n_routes, routes); |
| 309 |
case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: |
| 310 |
case SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE: |
| 311 |
return dhcp_message_append_option_classless_static_routes(message, code, n_routes, routes); |
| 312 |
default: |
| 313 |
return -EINVAL; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
int dhcp_message_append_option_6rd( |
| 318 |
sd_dhcp_message *message, |
| 319 |
uint8_t ipv4masklen, |
| 320 |
uint8_t prefixlen, |
| 321 |
const struct in6_addr *prefix, |
| 322 |
size_t n_br_addresses, |
| 323 |
const struct in_addr *br_addresses) { |
| 324 |
|
| 325 |
assert(message); |
| 326 |
assert(prefix); |
| 327 |
assert(n_br_addresses == 0 || br_addresses); |
| 328 |
|
| 329 |
/* See RFC 5969 Section 7.1.1 and dhcp_message_get_option_6rd() below. */ |
| 330 |
|
| 331 |
if (dhcp_message_has_option(message, SD_DHCP_OPTION_6RD)) |
| 332 |
return -EEXIST; |
| 333 |
|
| 334 |
if (ipv4masklen > 32) |
| 335 |
return -EINVAL; |
| 336 |
|
| 337 |
if (32 - ipv4masklen + prefixlen > 128) |
| 338 |
return -EINVAL; |
| 339 |
|
| 340 |
if (n_br_addresses == 0) |
| 341 |
return -EINVAL; |
| 342 |
|
| 343 |
if (size_multiply_overflow(sizeof(struct in_addr), n_br_addresses)) |
| 344 |
return -ENOBUFS; |
| 345 |
|
| 346 |
size_t buflen = size_add(2 + sizeof(struct in6_addr), sizeof(struct in_addr) * n_br_addresses); |
| 347 |
if (buflen == SIZE_MAX) |
| 348 |
return -ENOBUFS; |
| 349 |
|
| 350 |
_cleanup_free_ uint8_t *buf = new(uint8_t, buflen); |
| 351 |
if (!buf) |
| 352 |
return -ENOMEM; |
| 353 |
|
| 354 |
uint8_t *p = buf; |
| 355 |
*p++ = ipv4masklen; |
| 356 |
*p++ = prefixlen; |
| 357 |
|
| 358 |
struct in6_addr masked = *prefix; |
| 359 |
(void) in6_addr_mask(&masked, prefixlen); |
| 360 |
p = mempcpy(p, &masked, sizeof(struct in6_addr)); |
| 361 |
|
| 362 |
memcpy(p, br_addresses, n_br_addresses * sizeof(struct in_addr)); |
| 363 |
|
| 364 |
return dhcp_message_append_option(message, SD_DHCP_OPTION_6RD, buflen, buf); |
| 365 |
} |
| 366 |
|
| 367 |
int dhcp_message_append_option_client_id(sd_dhcp_message *message, const sd_dhcp_client_id *id) { |
| 368 |
assert(message); |
| 369 |
assert(id); |
| 370 |
|
| 371 |
if (!sd_dhcp_client_id_is_set(id)) |
| 372 |
return -EINVAL; |
| 373 |
|
| 374 |
if (dhcp_message_has_option(message, SD_DHCP_OPTION_CLIENT_IDENTIFIER)) |
| 375 |
return -EEXIST; |
| 376 |
|
| 377 |
return dhcp_message_append_option(message, SD_DHCP_OPTION_CLIENT_IDENTIFIER, id->size, id->raw); |
| 378 |
} |
| 379 |
|
| 380 |
static int cmp_uint8(const uint8_t *a, const uint8_t *b) { |
| 381 |
assert(a); |
| 382 |
assert(b); |
| 383 |
|
| 384 |
return CMP(*a, *b); |
| 385 |
} |
| 386 |
|
| 387 |
int dhcp_message_append_option_parameter_request_list(sd_dhcp_message *message, Set *prl) { |
| 388 |
assert(message); |
| 389 |
|
| 390 |
size_t len = set_size(prl); |
| 391 |
if (len == 0) |
| 392 |
return 0; |
| 393 |
|
| 394 |
_cleanup_free_ uint8_t *buf = new(uint8_t, len); |
| 395 |
if (!buf) |
| 396 |
return -ENOMEM; |
| 397 |
|
| 398 |
uint8_t *p = buf; |
| 399 |
void *q; |
| 400 |
SET_FOREACH(q, prl) |
| 401 |
*p++ = PTR_TO_UINT8(q); |
| 402 |
|
| 403 |
/* Sort the options to make the message reproducible. */ |
| 404 |
typesafe_qsort(buf, len, cmp_uint8); |
| 405 |
|
| 406 |
return dhcp_message_append_option(message, SD_DHCP_OPTION_PARAMETER_REQUEST_LIST, len, buf); |
| 407 |
} |
| 408 |
|
| 409 |
static int dhcp_message_append_option_fqdn(sd_dhcp_message *message, uint8_t flags, bool is_client, const char *fqdn) { |
| 410 |
int r; |
| 411 |
|
| 412 |
assert(message); |
| 413 |
assert(fqdn); |
| 414 |
|
| 415 |
/* FIXME: Allow long fqdn, as now we support long option. */ |
| 416 |
uint8_t buf[3 + DHCP_MAX_FQDN_LENGTH]; |
| 417 |
|
| 418 |
/* RFC 4702 section 2.1 |
| 419 |
* The "E" bit indicates the encoding of the Domain Name field. 1 indicates canonical wire format, |
| 420 |
* without compression. This encoding SHOULD be used by clients and MUST be supported by servers. |
| 421 |
* A server MUST use the same encoding as that used by the client. A server that does not support |
| 422 |
* the deprecated ASCII encoding MUST ignore Client FQDN options that use that encoding. |
| 423 |
* |
| 424 |
* Here, we unconditionally set the 'E' flag. Hence, sd_dhcp_server must ignore the option if a |
| 425 |
* client does not set the 'E' flag in the request. */ |
| 426 |
buf[0] = flags | DHCP_FQDN_FLAG_E; |
| 427 |
|
| 428 |
/* RFC 4702 section 2.2 |
| 429 |
* The two 1-octet RCODE1 and RCODE2 fields are deprecated. A client SHOULD set these to 0 when |
| 430 |
* sending the option and SHOULD ignore them on receipt. A server SHOULD set these to 255 when |
| 431 |
* sending the option and MUST ignore them on receipt. */ |
| 432 |
buf[1] = is_client ? 0 : 255; |
| 433 |
buf[2] = is_client ? 0 : 255; |
| 434 |
|
| 435 |
r = dns_name_to_wire_format(fqdn, buf + 3, sizeof(buf) - 3, false); |
| 436 |
if (r <= 0) |
| 437 |
return r; |
| 438 |
|
| 439 |
return dhcp_message_append_option(message, SD_DHCP_OPTION_FQDN, 3 + r, buf); |
| 440 |
} |
| 441 |
|
| 442 |
int dhcp_message_append_option_hostname(sd_dhcp_message *message, uint8_t flags, bool is_client, const char *hostname) { |
| 443 |
assert(message); |
| 444 |
|
| 445 |
/* Hostname (12) or FQDN (81) |
| 446 |
* |
| 447 |
* RFC 4702 section 3.1 |
| 448 |
* clients that send the Client FQDN option in their messages MUST NOT also send the Host Name option. */ |
| 449 |
|
| 450 |
if (isempty(hostname)) |
| 451 |
return 0; |
| 452 |
|
| 453 |
if (dhcp_message_has_option(message, SD_DHCP_OPTION_HOST_NAME)) |
| 454 |
return -EEXIST; |
| 455 |
|
| 456 |
if (dhcp_message_has_option(message, SD_DHCP_OPTION_FQDN)) |
| 457 |
return -EEXIST; |
| 458 |
|
| 459 |
if (dns_name_is_single_label(hostname)) |
| 460 |
return dhcp_message_append_option_string(message, SD_DHCP_OPTION_HOST_NAME, hostname); |
| 461 |
|
| 462 |
return dhcp_message_append_option_fqdn(message, flags, is_client, hostname); |
| 463 |
} |
| 464 |
|
| 465 |
int dhcp_message_append_option_sub_tlv(sd_dhcp_message *message, uint8_t code, const TLV *tlv) { |
| 466 |
int r; |
| 467 |
|
| 468 |
assert(message); |
| 469 |
|
| 470 |
if (tlv_isempty(tlv)) |
| 471 |
return 0; |
| 472 |
|
| 473 |
if (dhcp_message_has_option(message, code)) |
| 474 |
return -EEXIST; |
| 475 |
|
| 476 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 477 |
r = tlv_build(tlv, &iov); |
| 478 |
if (r < 0) |
| 479 |
return r; |
| 480 |
|
| 481 |
return dhcp_message_append_option(message, code, iov.iov_len, iov.iov_base); |
| 482 |
} |
| 483 |
|
| 484 |
int dhcp_message_append_option_length_prefixed_data( |
| 485 |
sd_dhcp_message *message, |
| 486 |
uint8_t code, |
| 487 |
size_t length_size, |
| 488 |
const struct iovec_wrapper *iovw) { |
| 489 |
|
| 490 |
int r; |
| 491 |
|
| 492 |
assert(message); |
| 493 |
|
| 494 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 495 |
r = iovw_merge(iovw, length_size, &iov); |
| 496 |
if (r < 0) |
| 497 |
return r; |
| 498 |
|
| 499 |
if (!iovec_is_set(&iov)) |
| 500 |
return 0; |
| 501 |
|
| 502 |
return dhcp_message_append_option(message, code, iov.iov_len, iov.iov_base); |
| 503 |
} |
| 504 |
|
| 505 |
int dhcp_message_get_option(sd_dhcp_message *message, uint8_t code, size_t length, void *ret) { |
| 506 |
int r; |
| 507 |
|
| 508 |
assert(message); |
| 509 |
|
| 510 |
struct iovec iov; |
| 511 |
r = tlv_get_full(&message->options, code, length, ret ? &iov : NULL); |
| 512 |
if (r < 0) |
| 513 |
return r; |
| 514 |
|
| 515 |
if (ret) |
| 516 |
memcpy_safe(ret, iov.iov_base, iov.iov_len); |
| 517 |
return 0; |
| 518 |
} |
| 519 |
|
| 520 |
int dhcp_message_get_option_alloc(sd_dhcp_message *message, uint8_t code, struct iovec *ret) { |
| 521 |
assert(message); |
| 522 |
return tlv_get_alloc(&message->options, code, ret); |
| 523 |
} |
| 524 |
|
| 525 |
int dhcp_message_get_option_flag(sd_dhcp_message *message, uint8_t code) { |
| 526 |
assert(message); |
| 527 |
return dhcp_message_get_option(message, code, /* length= */ 0, /* ret= */ NULL); |
| 528 |
} |
| 529 |
|
| 530 |
int dhcp_message_get_option_u8(sd_dhcp_message *message, uint8_t code, uint8_t *ret) { |
| 531 |
assert(message); |
| 532 |
return dhcp_message_get_option(message, code, sizeof(uint8_t), ret); |
| 533 |
} |
| 534 |
|
| 535 |
int dhcp_message_get_option_u16(sd_dhcp_message *message, uint8_t code, uint16_t *ret) { |
| 536 |
be16_t b; |
| 537 |
int r; |
| 538 |
|
| 539 |
assert(message); |
| 540 |
|
| 541 |
r = dhcp_message_get_option(message, code, sizeof(be16_t), ret ? &b : NULL); |
| 542 |
if (r < 0) |
| 543 |
return r; |
| 544 |
|
| 545 |
if (ret) |
| 546 |
*ret = be16toh(b); |
| 547 |
return 0; |
| 548 |
} |
| 549 |
|
| 550 |
int dhcp_message_get_option_be32(sd_dhcp_message *message, uint8_t code, be32_t *ret) { |
| 551 |
assert(message); |
| 552 |
return dhcp_message_get_option(message, code, sizeof(be32_t), ret); |
| 553 |
} |
| 554 |
|
| 555 |
int dhcp_message_get_option_sec(sd_dhcp_message *message, uint8_t code, bool max_as_infinity, usec_t *ret) { |
| 556 |
int r; |
| 557 |
|
| 558 |
assert(message); |
| 559 |
|
| 560 |
be32_t t; |
| 561 |
r = dhcp_message_get_option_be32(message, code, &t); |
| 562 |
if (r < 0) |
| 563 |
return r; |
| 564 |
|
| 565 |
if (ret) |
| 566 |
*ret = be32_sec_to_usec(t, max_as_infinity); |
| 567 |
return 0; |
| 568 |
} |
| 569 |
|
| 570 |
int dhcp_message_get_option_address(sd_dhcp_message *message, uint8_t code, struct in_addr *ret) { |
| 571 |
assert(message); |
| 572 |
return dhcp_message_get_option_be32(message, code, ret ? &ret->s_addr : NULL); |
| 573 |
} |
| 574 |
|
| 575 |
int dhcp_message_get_option_addresses(sd_dhcp_message *message, uint8_t code, size_t *ret_n_addr, struct in_addr **ret_addr) { |
| 576 |
int r; |
| 577 |
|
| 578 |
assert(message); |
| 579 |
assert(ret_n_addr || !ret_addr); |
| 580 |
|
| 581 |
_cleanup_(iovec_done) struct iovec iov_free = {}; |
| 582 |
r = dhcp_message_get_option_alloc(message, code, &iov_free); |
| 583 |
if (r < 0) |
| 584 |
return r; |
| 585 |
|
| 586 |
struct iovec iov = iov_free; |
| 587 |
if (code == SD_DHCP_OPTION_SIP_SERVER) { |
| 588 |
if (!iovec_is_set(&iov)) |
| 589 |
return -EBADMSG; |
| 590 |
|
| 591 |
if (*(uint8_t*) iov.iov_base != 1) /* 'enc' field, 0: domains, 1: addresses */ |
| 592 |
return -ENODATA; |
| 593 |
|
| 594 |
iovec_inc(&iov, 1); |
| 595 |
} |
| 596 |
|
| 597 |
if (iov.iov_len % sizeof(struct in_addr) != 0) |
| 598 |
return -EBADMSG; |
| 599 |
|
| 600 |
size_t n = iov.iov_len / sizeof(struct in_addr); |
| 601 |
if (n == 0) |
| 602 |
return -ENODATA; |
| 603 |
|
| 604 |
if (ret_addr) { |
| 605 |
if (code == SD_DHCP_OPTION_SIP_SERVER) { |
| 606 |
struct in_addr *addr = newdup(struct in_addr, iov.iov_base, n); |
| 607 |
if (!addr) |
| 608 |
return -ENOMEM; |
| 609 |
*ret_addr = addr; |
| 610 |
} else { |
| 611 |
*ret_addr = iov.iov_base; |
| 612 |
TAKE_STRUCT(iov_free); |
| 613 |
} |
| 614 |
} |
| 615 |
if (ret_n_addr) |
| 616 |
*ret_n_addr = n; |
| 617 |
return 0; |
| 618 |
} |
| 619 |
|
| 620 |
int dhcp_message_get_option_string(sd_dhcp_message *message, uint8_t code, char **ret) { |
| 621 |
int r; |
| 622 |
|
| 623 |
assert(message); |
| 624 |
|
| 625 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 626 |
r = dhcp_message_get_option_alloc(message, code, &iov); |
| 627 |
if (r < 0) |
| 628 |
return r; |
| 629 |
|
| 630 |
if (!iovec_is_set(&iov)) |
| 631 |
return -ENODATA; |
| 632 |
|
| 633 |
/* Allow NUL at the end for buggy DHCP servers, but refuse intermediate NUL. */ |
| 634 |
if (memchr(iov.iov_base, 0, iov.iov_len - 1)) |
| 635 |
return -EBADMSG; |
| 636 |
|
| 637 |
/* Note, dhcp_message_get_option_alloc() -> tlv_get_alloc() allocates an extra byte to make |
| 638 |
* iov.iov_base can be handled as a NUL-terminated string. Hence, we can directly pass it to |
| 639 |
* isempty() and string_is_safe(). */ |
| 640 |
|
| 641 |
if (isempty(iov.iov_base)) |
| 642 |
return -ENODATA; |
| 643 |
|
| 644 |
if (!string_is_safe(iov.iov_base, STRING_ALLOW_BACKSLASHES | STRING_ALLOW_QUOTES | STRING_ALLOW_GLOBS)) |
| 645 |
return -EBADMSG; |
| 646 |
|
| 647 |
if (ret) |
| 648 |
*ret = TAKE_PTR(iov.iov_base); |
| 649 |
return 0; |
| 650 |
} |
| 651 |
|
| 652 |
static int dhcp_message_get_option_static_routes(sd_dhcp_message *message, size_t *ret_n_routes, sd_dhcp_route **ret_routes) { |
| 653 |
int r; |
| 654 |
|
| 655 |
assert(message); |
| 656 |
|
| 657 |
size_t n; |
| 658 |
_cleanup_free_ struct in_addr *addrs = NULL; |
| 659 |
r = dhcp_message_get_option_addresses(message, SD_DHCP_OPTION_STATIC_ROUTE, &n, &addrs); |
| 660 |
if (r < 0) |
| 661 |
return r; |
| 662 |
|
| 663 |
if (n % 2 != 0) |
| 664 |
return -EBADMSG; |
| 665 |
|
| 666 |
_cleanup_free_ sd_dhcp_route *routes = NULL; |
| 667 |
size_t n_routes = 0; |
| 668 |
|
| 669 |
for (size_t i = 0; i < n; i += 2) { |
| 670 |
struct in_addr dst = addrs[i]; |
| 671 |
|
| 672 |
uint8_t prefixlen; |
| 673 |
if (in4_addr_default_prefixlen(&dst, &prefixlen) < 0) |
| 674 |
continue; |
| 675 |
|
| 676 |
(void) in4_addr_mask(&dst, prefixlen); |
| 677 |
|
| 678 |
/* RFC 2132 section 5.8: |
| 679 |
* The default route (0.0.0.0) is an illegal destination for a static route.*/ |
| 680 |
if (in4_addr_is_null(&dst)) |
| 681 |
continue; |
| 682 |
|
| 683 |
if (!ret_routes) { |
| 684 |
n_routes++; |
| 685 |
continue; |
| 686 |
} |
| 687 |
|
| 688 |
if (!GREEDY_REALLOC(routes, n_routes + 1)) |
| 689 |
return -ENOMEM; |
| 690 |
|
| 691 |
routes[n_routes++] = (struct sd_dhcp_route) { |
| 692 |
.dst_addr = dst, |
| 693 |
.gw_addr = addrs[i + 1], |
| 694 |
.dst_prefixlen = prefixlen, |
| 695 |
}; |
| 696 |
} |
| 697 |
|
| 698 |
if (n_routes == 0) |
| 699 |
return -ENODATA; |
| 700 |
|
| 701 |
if (ret_routes) |
| 702 |
*ret_routes = TAKE_PTR(routes); |
| 703 |
if (ret_n_routes) |
| 704 |
*ret_n_routes = n_routes; |
| 705 |
return 0; |
| 706 |
} |
| 707 |
|
| 708 |
static int dhcp_message_get_option_classless_static_routes(sd_dhcp_message *message, uint8_t code, size_t *ret_n_routes, sd_dhcp_route **ret_routes) { |
| 709 |
int r; |
| 710 |
|
| 711 |
assert(message); |
| 712 |
assert(IN_SET(code, |
| 713 |
SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE, |
| 714 |
SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE)); |
| 715 |
|
| 716 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 717 |
r = dhcp_message_get_option_alloc(message, code, &iov); |
| 718 |
if (r < 0) |
| 719 |
return r; |
| 720 |
|
| 721 |
_cleanup_free_ sd_dhcp_route *routes = NULL; |
| 722 |
size_t n_routes = 0; |
| 723 |
|
| 724 |
for (struct iovec i = iov; iovec_is_set(&i);) { |
| 725 |
uint8_t prefixlen = *(uint8_t*) i.iov_base; |
| 726 |
iovec_inc(&i, 1); |
| 727 |
|
| 728 |
if (prefixlen > 32) |
| 729 |
return -EBADMSG; |
| 730 |
|
| 731 |
size_t n = DIV_ROUND_UP(prefixlen, 8); |
| 732 |
if (n > i.iov_len) |
| 733 |
return -EBADMSG; |
| 734 |
|
| 735 |
struct in_addr dst = {}; |
| 736 |
memcpy_safe(&dst, i.iov_base, n); |
| 737 |
(void) in4_addr_mask(&dst, prefixlen); |
| 738 |
iovec_inc(&i, n); |
| 739 |
|
| 740 |
if (i.iov_len < sizeof(struct in_addr)) |
| 741 |
return -EBADMSG; |
| 742 |
|
| 743 |
struct in_addr gw; |
| 744 |
memcpy(&gw, i.iov_base, sizeof(struct in_addr)); |
| 745 |
iovec_inc(&i, sizeof(struct in_addr)); |
| 746 |
|
| 747 |
if (!ret_routes) { |
| 748 |
n_routes++; |
| 749 |
continue; |
| 750 |
} |
| 751 |
|
| 752 |
if (!GREEDY_REALLOC(routes, n_routes + 1)) |
| 753 |
return -ENOMEM; |
| 754 |
|
| 755 |
routes[n_routes++] = (struct sd_dhcp_route) { |
| 756 |
.dst_addr = dst, |
| 757 |
.gw_addr = gw, |
| 758 |
.dst_prefixlen = prefixlen, |
| 759 |
}; |
| 760 |
} |
| 761 |
|
| 762 |
if (n_routes == 0) |
| 763 |
return -ENODATA; |
| 764 |
|
| 765 |
if (ret_routes) |
| 766 |
*ret_routes = TAKE_PTR(routes); |
| 767 |
if (ret_n_routes) |
| 768 |
*ret_n_routes = n_routes; |
| 769 |
return 0; |
| 770 |
} |
| 771 |
|
| 772 |
int dhcp_message_get_option_routes(sd_dhcp_message *message, uint8_t code, size_t *ret_n_routes, sd_dhcp_route **ret_routes) { |
| 773 |
switch (code) { |
| 774 |
case SD_DHCP_OPTION_STATIC_ROUTE: |
| 775 |
return dhcp_message_get_option_static_routes(message, ret_n_routes, ret_routes); |
| 776 |
case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: |
| 777 |
case SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE: |
| 778 |
return dhcp_message_get_option_classless_static_routes(message, code, ret_n_routes, ret_routes); |
| 779 |
default: |
| 780 |
return -EINVAL; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
int dhcp_message_get_option_6rd( |
| 785 |
sd_dhcp_message *message, |
| 786 |
uint8_t *ret_ipv4masklen, |
| 787 |
uint8_t *ret_prefixlen, |
| 788 |
struct in6_addr *ret_prefix, |
| 789 |
size_t *ret_n_br_addresses, |
| 790 |
struct in_addr **ret_br_addresses) { |
| 791 |
|
| 792 |
int r; |
| 793 |
|
| 794 |
assert(message); |
| 795 |
assert(ret_n_br_addresses || !ret_br_addresses); |
| 796 |
|
| 797 |
/* See RFC 5969 Section 7.1.1 */ |
| 798 |
|
| 799 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 800 |
r = dhcp_message_get_option_alloc(message, SD_DHCP_OPTION_6RD, &iov); |
| 801 |
if (r < 0) |
| 802 |
return r; |
| 803 |
|
| 804 |
/* option-length: The length of the DHCP option in octets (22 octets with one BR IPv4 address). */ |
| 805 |
if (iov.iov_len < 2 + sizeof(struct in6_addr) + sizeof(struct in_addr) || |
| 806 |
(iov.iov_len - 2 - sizeof(struct in6_addr)) % sizeof(struct in_addr) != 0) |
| 807 |
return -EBADMSG; |
| 808 |
|
| 809 |
size_t n_br_addresses = (iov.iov_len - 2 - sizeof(struct in6_addr)) / sizeof(struct in_addr); |
| 810 |
assert(n_br_addresses > 0); /* We have already checked that in the above. */ |
| 811 |
|
| 812 |
const uint8_t *p = iov.iov_base; |
| 813 |
|
| 814 |
/* IPv4MaskLen: The number of high-order bits that are identical across all CE IPv4 addresses |
| 815 |
* within a given 6rd domain. This may be any value between 0 and 32. Any value |
| 816 |
* greater than 32 is invalid. */ |
| 817 |
uint8_t ipv4masklen = *p++; |
| 818 |
if (ipv4masklen > 32) |
| 819 |
return -EBADMSG; |
| 820 |
|
| 821 |
/* 6rdPrefixLen: The IPv6 prefix length of the SP's 6rd IPv6 prefix in number of bits. For the |
| 822 |
* purpose of bounds checking by DHCP option processing, the sum of |
| 823 |
* (32 - IPv4MaskLen) + 6rdPrefixLen MUST be less than or equal to 128. */ |
| 824 |
uint8_t prefixlen = *p++; |
| 825 |
if (32 - ipv4masklen + prefixlen > 128) |
| 826 |
return -EBADMSG; |
| 827 |
|
| 828 |
/* 6rdPrefix: The service provider's 6rd IPv6 prefix represented as a 16-octet IPv6 address. |
| 829 |
* The bits in the prefix after the 6rdPrefixlen number of bits are reserved and |
| 830 |
* MUST be initialized to zero by the sender and ignored by the receiver. */ |
| 831 |
struct in6_addr prefix; |
| 832 |
memcpy(&prefix, p, sizeof(struct in6_addr)); |
| 833 |
(void) in6_addr_mask(&prefix, prefixlen); |
| 834 |
p += sizeof(struct in6_addr); |
| 835 |
|
| 836 |
/* 6rdBRIPv4Address: One or more IPv4 addresses of the 6rd Border Relays for a given 6rd domain. */ |
| 837 |
if (ret_br_addresses) { |
| 838 |
struct in_addr *br_addresses = newdup(struct in_addr, p, n_br_addresses); |
| 839 |
if (!br_addresses) |
| 840 |
return -ENOMEM; |
| 841 |
|
| 842 |
*ret_br_addresses = br_addresses; |
| 843 |
} |
| 844 |
|
| 845 |
if (ret_ipv4masklen) |
| 846 |
*ret_ipv4masklen = ipv4masklen; |
| 847 |
if (ret_prefixlen) |
| 848 |
*ret_prefixlen = prefixlen; |
| 849 |
if (ret_prefix) |
| 850 |
*ret_prefix = prefix; |
| 851 |
if (ret_n_br_addresses) |
| 852 |
*ret_n_br_addresses = n_br_addresses; |
| 853 |
return 0; |
| 854 |
} |
| 855 |
|
| 856 |
int dhcp_message_get_option_client_id(sd_dhcp_message *message, sd_dhcp_client_id *ret) { |
| 857 |
int r; |
| 858 |
|
| 859 |
assert(message); |
| 860 |
assert(ret); |
| 861 |
|
| 862 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 863 |
r = dhcp_message_get_option_alloc(message, SD_DHCP_OPTION_CLIENT_IDENTIFIER, &iov); |
| 864 |
if (r < 0) |
| 865 |
return r; |
| 866 |
|
| 867 |
return sd_dhcp_client_id_set_raw(ret, iov.iov_base, iov.iov_len); |
| 868 |
} |
| 869 |
|
| 870 |
int dhcp_message_get_option_parameter_request_list(sd_dhcp_message *message, Set **ret) { |
| 871 |
int r; |
| 872 |
|
| 873 |
assert(message); |
| 874 |
|
| 875 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 876 |
r = dhcp_message_get_option_alloc(message, SD_DHCP_OPTION_PARAMETER_REQUEST_LIST, &iov); |
| 877 |
if (r < 0) |
| 878 |
return r; |
| 879 |
|
| 880 |
if (!iovec_is_set(&iov)) |
| 881 |
return -ENODATA; |
| 882 |
|
| 883 |
if (!ret) |
| 884 |
return 0; |
| 885 |
|
| 886 |
_cleanup_set_free_ Set *prl = NULL; |
| 887 |
for (struct iovec i = iov; iovec_is_set(&i); iovec_inc(&i, 1)) { |
| 888 |
r = set_ensure_put(&prl, /* hash_ops= */ NULL, UINT8_TO_PTR(*(uint8_t*) i.iov_base)); |
| 889 |
if (r < 0) |
| 890 |
return r; |
| 891 |
} |
| 892 |
|
| 893 |
*ret = TAKE_PTR(prl); |
| 894 |
return 0; |
| 895 |
} |
| 896 |
|
| 897 |
static int normalize_dns_name(const char *name, char **ret) { |
| 898 |
int r; |
| 899 |
|
| 900 |
assert(name); |
| 901 |
|
| 902 |
_cleanup_free_ char *normalized = NULL; |
| 903 |
r = dns_name_normalize(name, /* flags= */ 0, &normalized); |
| 904 |
if (r < 0) |
| 905 |
return r; |
| 906 |
|
| 907 |
if (is_localhost(normalized)) |
| 908 |
return -EINVAL; |
| 909 |
|
| 910 |
if (dns_name_is_root(normalized)) |
| 911 |
return -EINVAL; |
| 912 |
|
| 913 |
if (ret) |
| 914 |
*ret = TAKE_PTR(normalized); |
| 915 |
return 0; |
| 916 |
} |
| 917 |
|
| 918 |
int dhcp_message_get_option_fqdn(sd_dhcp_message *message, uint8_t *ret_flags, char **ret_fqdn) { |
| 919 |
int r; |
| 920 |
|
| 921 |
assert(message); |
| 922 |
|
| 923 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 924 |
r = dhcp_message_get_option_alloc(message, SD_DHCP_OPTION_FQDN, &iov); |
| 925 |
if (r < 0) |
| 926 |
return r; |
| 927 |
|
| 928 |
if (iov.iov_len <= 3) |
| 929 |
return -EBADMSG; |
| 930 |
|
| 931 |
uint8_t flags = *(uint8_t*) iov.iov_base; |
| 932 |
if (!FLAGS_SET(flags, DHCP_FQDN_FLAG_E)) |
| 933 |
return -EOPNOTSUPP; |
| 934 |
|
| 935 |
struct iovec i; |
| 936 |
iovec_shift(&iov, 3, &i); |
| 937 |
|
| 938 |
_cleanup_free_ char *name = NULL; |
| 939 |
const uint8_t *p = i.iov_base; |
| 940 |
r = dns_name_from_wire_format(&p, &i.iov_len, &name); |
| 941 |
if (r < 0) |
| 942 |
return r; |
| 943 |
if (i.iov_len > 0) /* trailing garbage? */ |
| 944 |
return -EBADMSG; |
| 945 |
|
| 946 |
if (isempty(name)) |
| 947 |
return -ENODATA; |
| 948 |
|
| 949 |
if (!string_is_safe(name, /* flags= */ 0)) |
| 950 |
return -EBADMSG; |
| 951 |
|
| 952 |
_cleanup_free_ char *normalized = NULL; |
| 953 |
r = normalize_dns_name(name, &normalized); |
| 954 |
if (r < 0) |
| 955 |
return r; |
| 956 |
|
| 957 |
if (ret_flags) |
| 958 |
*ret_flags = flags; |
| 959 |
if (ret_fqdn) |
| 960 |
*ret_fqdn = TAKE_PTR(normalized); |
| 961 |
return 0; |
| 962 |
} |
| 963 |
|
| 964 |
int dhcp_message_get_option_dns_name(sd_dhcp_message *message, uint8_t code, char **ret) { |
| 965 |
int r; |
| 966 |
|
| 967 |
assert(message); |
| 968 |
|
| 969 |
/* Mainly for Host Name or Domain Name options. */ |
| 970 |
|
| 971 |
_cleanup_free_ char *name = NULL; |
| 972 |
r = dhcp_message_get_option_string(message, code, &name); |
| 973 |
if (r < 0) |
| 974 |
return r; |
| 975 |
|
| 976 |
_cleanup_free_ char *normalized = NULL; |
| 977 |
r = normalize_dns_name(name, &normalized); |
| 978 |
if (r < 0) |
| 979 |
return r; |
| 980 |
|
| 981 |
if (ret) |
| 982 |
*ret = TAKE_PTR(normalized); |
| 983 |
return 0; |
| 984 |
} |
| 985 |
|
| 986 |
int dhcp_message_get_option_hostname(sd_dhcp_message *message, char **ret) { |
| 987 |
int r; |
| 988 |
|
| 989 |
assert(message); |
| 990 |
|
| 991 |
/* FQDN option always takes precedence. */ |
| 992 |
r = dhcp_message_get_option_fqdn(message, /* ret_flags= */ NULL, ret); |
| 993 |
if (ERRNO_IS_NEG_RESOURCE(r)) |
| 994 |
return r; |
| 995 |
if (r >= 0) |
| 996 |
return 0; |
| 997 |
|
| 998 |
/* Then, fall back to Host Name option. */ |
| 999 |
return dhcp_message_get_option_dns_name(message, SD_DHCP_OPTION_HOST_NAME, ret); |
| 1000 |
} |
| 1001 |
|
| 1002 |
int dhcp_message_get_option_domains(sd_dhcp_message *message, uint8_t code, char ***ret) { |
| 1003 |
int r; |
| 1004 |
|
| 1005 |
assert(message); |
| 1006 |
|
| 1007 |
/* This is mostly for SD_DHCP_OPTION_DOMAIN_SEARCH and SD_DHCP_OPTION_SIP_SERVER. */ |
| 1008 |
|
| 1009 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 1010 |
r = dhcp_message_get_option_alloc(message, code, &iov); |
| 1011 |
if (r < 0) |
| 1012 |
return r; |
| 1013 |
|
| 1014 |
const uint8_t *buf = iov.iov_base; |
| 1015 |
size_t len = iov.iov_len; |
| 1016 |
|
| 1017 |
if (code == SD_DHCP_OPTION_SIP_SERVER) { |
| 1018 |
if (len == 0) |
| 1019 |
return -EBADMSG; |
| 1020 |
|
| 1021 |
if (buf[0] != 0) /* 'enc' field, 0: domains, 1: addresses */ |
| 1022 |
return -ENODATA; |
| 1023 |
|
| 1024 |
len--; |
| 1025 |
buf++; |
| 1026 |
} |
| 1027 |
|
| 1028 |
_cleanup_strv_free_ char **names = NULL; |
| 1029 |
size_t n_names = 0; |
| 1030 |
|
| 1031 |
_cleanup_free_ char *name = NULL; |
| 1032 |
size_t n = 0; |
| 1033 |
|
| 1034 |
for (size_t pos = 0, jump_barrier = 0, next_chunk = 0; pos < len;) { |
| 1035 |
uint8_t c = buf[pos++]; |
| 1036 |
|
| 1037 |
if (c == 0) { |
| 1038 |
/* End of name */ |
| 1039 |
|
| 1040 |
if (!string_is_safe(name, /* flags= */ 0)) |
| 1041 |
return -EBADMSG; |
| 1042 |
|
| 1043 |
_cleanup_free_ char *normalized = NULL; |
| 1044 |
r = normalize_dns_name(name, &normalized); |
| 1045 |
if (r < 0) |
| 1046 |
return r; |
| 1047 |
|
| 1048 |
r = strv_consume_with_size(&names, &n_names, TAKE_PTR(normalized)); |
| 1049 |
if (r < 0) |
| 1050 |
return r; |
| 1051 |
|
| 1052 |
if (next_chunk != 0) |
| 1053 |
pos = next_chunk; |
| 1054 |
|
| 1055 |
next_chunk = 0; |
| 1056 |
jump_barrier = pos; |
| 1057 |
|
| 1058 |
name = mfree(name); |
| 1059 |
n = 0; |
| 1060 |
|
| 1061 |
} else if (c <= 63) { |
| 1062 |
/* Literal label */ |
| 1063 |
|
| 1064 |
const char *label = (const char*) (buf + pos); |
| 1065 |
pos += c; |
| 1066 |
|
| 1067 |
if (pos >= len) |
| 1068 |
return -EBADMSG; |
| 1069 |
|
| 1070 |
if (!GREEDY_REALLOC(name, n + 1 + DNS_LABEL_ESCAPED_MAX)) |
| 1071 |
return -ENOMEM; |
| 1072 |
|
| 1073 |
if (n != 0) |
| 1074 |
name[n++] = '.'; |
| 1075 |
|
| 1076 |
r = dns_label_escape(label, c, name + n, DNS_LABEL_ESCAPED_MAX); |
| 1077 |
if (r < 0) |
| 1078 |
return r; |
| 1079 |
|
| 1080 |
n += r; |
| 1081 |
|
| 1082 |
} else if (FLAGS_SET(c, 0xc0)) { |
| 1083 |
/* Pointer */ |
| 1084 |
|
| 1085 |
if (pos >= len) /* pointer is 2 bytes, hence we need to read at least one more byte. */ |
| 1086 |
return -EBADMSG; |
| 1087 |
|
| 1088 |
/* Save the current location so we don't end up re-parsing what's parsed so far. */ |
| 1089 |
if (next_chunk == 0) |
| 1090 |
next_chunk = pos + 1; |
| 1091 |
|
| 1092 |
pos = ((size_t) (c & ~0xc0) << 8) | ((size_t) buf[pos]); |
| 1093 |
|
| 1094 |
/* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */ |
| 1095 |
if (pos >= jump_barrier) |
| 1096 |
return -EBADMSG; |
| 1097 |
|
| 1098 |
jump_barrier = pos; |
| 1099 |
|
| 1100 |
} else |
| 1101 |
return -EBADMSG; |
| 1102 |
} |
| 1103 |
|
| 1104 |
if (!isempty(name)) /* trailing garbage?? Should not happen, but for safety. */ |
| 1105 |
return -EBADMSG; |
| 1106 |
|
| 1107 |
if (strv_isempty(names)) |
| 1108 |
return -EBADMSG; |
| 1109 |
|
| 1110 |
if (ret) |
| 1111 |
*ret = TAKE_PTR(names); |
| 1112 |
return 0; |
| 1113 |
} |
| 1114 |
|
| 1115 |
int dhcp_message_get_option_sub_tlv(sd_dhcp_message *message, uint8_t code, TLVFlag flags, TLV **ret) { |
| 1116 |
int r; |
| 1117 |
|
| 1118 |
assert(message); |
| 1119 |
assert(!FLAGS_SET(flags, TLV_TEMPORARY)); |
| 1120 |
|
| 1121 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 1122 |
r = dhcp_message_get_option_alloc(message, code, &iov); |
| 1123 |
if (r < 0) |
| 1124 |
return r; |
| 1125 |
|
| 1126 |
_cleanup_(tlv_unrefp) TLV *tlv = tlv_new(flags); |
| 1127 |
if (!tlv) |
| 1128 |
return -ENOMEM; |
| 1129 |
|
| 1130 |
r = tlv_parse(tlv, &iov); |
| 1131 |
if (r < 0) |
| 1132 |
return r; |
| 1133 |
|
| 1134 |
if (tlv_isempty(tlv)) |
| 1135 |
return -ENODATA; |
| 1136 |
|
| 1137 |
if (ret) |
| 1138 |
*ret = TAKE_PTR(tlv); |
| 1139 |
return 0; |
| 1140 |
} |
| 1141 |
|
| 1142 |
int dhcp_message_get_option_length_prefixed_data( |
| 1143 |
sd_dhcp_message *message, |
| 1144 |
uint8_t code, |
| 1145 |
size_t length_size, |
| 1146 |
struct iovec_wrapper *ret) { |
| 1147 |
|
| 1148 |
int r; |
| 1149 |
|
| 1150 |
assert(message); |
| 1151 |
|
| 1152 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 1153 |
r = dhcp_message_get_option_alloc(message, code, &iov); |
| 1154 |
if (r < 0) |
| 1155 |
return r; |
| 1156 |
|
| 1157 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 1158 |
r = iovec_split(&iov, length_size, &iovw); |
| 1159 |
if (r < 0) |
| 1160 |
return r; |
| 1161 |
|
| 1162 |
if (iovw_isempty(&iovw)) |
| 1163 |
return -ENODATA; |
| 1164 |
|
| 1165 |
if (ret) |
| 1166 |
*ret = TAKE_STRUCT(iovw); |
| 1167 |
return 0; |
| 1168 |
} |
| 1169 |
|
| 1170 |
static int parse_dnr_one(const struct iovec *iov, sd_dns_resolver *ret) { |
| 1171 |
int r; |
| 1172 |
|
| 1173 |
assert(iovec_is_set(iov)); |
| 1174 |
assert(ret); |
| 1175 |
|
| 1176 |
_cleanup_(sd_dns_resolver_done) sd_dns_resolver resolver = {}; |
| 1177 |
struct iovec i = *iov; |
| 1178 |
|
| 1179 |
/* service priority */ |
| 1180 |
if (i.iov_len < sizeof(be16_t)) |
| 1181 |
return -EBADMSG; |
| 1182 |
|
| 1183 |
resolver.priority = unaligned_read_be16(i.iov_base); |
| 1184 |
iovec_inc(&i, sizeof(be16_t)); |
| 1185 |
|
| 1186 |
/* RFC 9460 section 2.4.1: |
| 1187 |
* When SvcPriority is 0, the SVCB record is in AliasMode. |
| 1188 |
* |
| 1189 |
* We do not support the alias mode. But the entry itself is not invalid. */ |
| 1190 |
if (resolver.priority == 0) { |
| 1191 |
*ret = (sd_dns_resolver) {}; |
| 1192 |
return 0; |
| 1193 |
} |
| 1194 |
|
| 1195 |
/* authentication domain name */ |
| 1196 |
if (!iovec_is_set(&i)) |
| 1197 |
return -EBADMSG; |
| 1198 |
|
| 1199 |
size_t name_len = *(uint8_t*) i.iov_base; |
| 1200 |
iovec_inc(&i, 1); |
| 1201 |
if (i.iov_len < name_len) |
| 1202 |
return -EBADMSG; |
| 1203 |
|
| 1204 |
const uint8_t *name_buf = i.iov_base; |
| 1205 |
iovec_inc(&i, name_len); |
| 1206 |
|
| 1207 |
r = dns_name_from_wire_format(&name_buf, &name_len, &resolver.auth_name); |
| 1208 |
if (r < 0) |
| 1209 |
return r; |
| 1210 |
if (r == 0 || name_len != 0) |
| 1211 |
return -EBADMSG; |
| 1212 |
|
| 1213 |
r = dns_name_is_valid_ldh(resolver.auth_name); |
| 1214 |
if (r < 0) |
| 1215 |
return r; |
| 1216 |
if (r == 0) |
| 1217 |
return -EBADMSG; |
| 1218 |
|
| 1219 |
if (dns_name_is_root(resolver.auth_name)) |
| 1220 |
return -EBADMSG; |
| 1221 |
|
| 1222 |
/* RFC9463 section 3.1.6: In ADN-only mode, server omits everything after the ADN. |
| 1223 |
* |
| 1224 |
* We don't support these, but they are not invalid. */ |
| 1225 |
if (!iovec_is_set(&i)) { |
| 1226 |
*ret = (sd_dns_resolver) {}; |
| 1227 |
return 0; |
| 1228 |
} |
| 1229 |
|
| 1230 |
/* IPv4 addresses */ |
| 1231 |
size_t n = *(uint8_t*) i.iov_base; |
| 1232 |
iovec_inc(&i, 1); |
| 1233 |
|
| 1234 |
if (n % sizeof(struct in_addr) != 0) |
| 1235 |
return -EBADMSG; |
| 1236 |
|
| 1237 |
n /= sizeof(struct in_addr); |
| 1238 |
|
| 1239 |
/* RFC9463 section 3.1.8: option MUST include at least one valid IP addr */ |
| 1240 |
if (n == 0) |
| 1241 |
return -EBADMSG; |
| 1242 |
|
| 1243 |
resolver.family = AF_INET; |
| 1244 |
resolver.n_addrs = n; |
| 1245 |
resolver.addrs = new(union in_addr_union, n); |
| 1246 |
if (!resolver.addrs) |
| 1247 |
return -ENOMEM; |
| 1248 |
|
| 1249 |
for (size_t j = 0; j < n; j++) { |
| 1250 |
if (i.iov_len < sizeof(struct in_addr)) |
| 1251 |
return -EBADMSG; |
| 1252 |
|
| 1253 |
struct in_addr a; |
| 1254 |
memcpy(&a, i.iov_base, sizeof(struct in_addr)); |
| 1255 |
iovec_inc(&i, sizeof(struct in_addr)); |
| 1256 |
|
| 1257 |
/* RFC9463 section 5.2: client MUST discard multicast and host loopback addresses */ |
| 1258 |
if (in4_addr_is_multicast(&a) || in4_addr_is_localhost(&a)) |
| 1259 |
return -EBADMSG; |
| 1260 |
|
| 1261 |
resolver.addrs[j] = (union in_addr_union) { .in = a }; |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* service params */ |
| 1265 |
r = dnr_parse_svc_params(i.iov_base, i.iov_len, &resolver); |
| 1266 |
if (r < 0) |
| 1267 |
return r; |
| 1268 |
if (r == 0) { |
| 1269 |
/* We can't use this record, but it is not invalid. */ |
| 1270 |
*ret = (sd_dns_resolver) {}; |
| 1271 |
return 0; |
| 1272 |
} |
| 1273 |
|
| 1274 |
*ret = TAKE_STRUCT(resolver); |
| 1275 |
return 1; |
| 1276 |
} |
| 1277 |
|
| 1278 |
int dhcp_message_get_option_dnr(sd_dhcp_message *message, size_t *ret_n_resolvers, sd_dns_resolver **ret_resolvers) { |
| 1279 |
int r; |
| 1280 |
|
| 1281 |
assert(message); |
| 1282 |
assert(ret_n_resolvers || !ret_resolvers); |
| 1283 |
|
| 1284 |
/* See RFC 9463 section 5.1 */ |
| 1285 |
|
| 1286 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 1287 |
r = dhcp_message_get_option_length_prefixed_data(message, SD_DHCP_OPTION_V4_DNR, /* length_size= */ 2, &iovw); |
| 1288 |
if (r < 0) |
| 1289 |
return r; |
| 1290 |
|
| 1291 |
sd_dns_resolver *resolvers = NULL; |
| 1292 |
size_t n_resolvers = 0; |
| 1293 |
CLEANUP_ARRAY(resolvers, n_resolvers, dns_resolver_free_array); |
| 1294 |
FOREACH_ARRAY(i, iovw.iovec, iovw.count) { |
| 1295 |
_cleanup_(sd_dns_resolver_done) sd_dns_resolver dnr = {}; |
| 1296 |
r = parse_dnr_one(i, &dnr); |
| 1297 |
if (r < 0) |
| 1298 |
return r; |
| 1299 |
if (r == 0) |
| 1300 |
continue; |
| 1301 |
|
| 1302 |
if (!ret_resolvers) { |
| 1303 |
n_resolvers++; |
| 1304 |
continue; |
| 1305 |
} |
| 1306 |
|
| 1307 |
if (!GREEDY_REALLOC(resolvers, n_resolvers + 1)) |
| 1308 |
return -ENOMEM; |
| 1309 |
|
| 1310 |
resolvers[n_resolvers++] = TAKE_STRUCT(dnr); |
| 1311 |
} |
| 1312 |
|
| 1313 |
if (n_resolvers == 0) /* no supported resolver */ |
| 1314 |
return -ENODATA; |
| 1315 |
|
| 1316 |
if (ret_resolvers) { |
| 1317 |
/* Sort the resolvers with their priorities. */ |
| 1318 |
typesafe_qsort(resolvers, n_resolvers, dns_resolver_prio_compare); |
| 1319 |
*ret_resolvers = TAKE_PTR(resolvers); |
| 1320 |
} |
| 1321 |
if (ret_n_resolvers) |
| 1322 |
*ret_n_resolvers = n_resolvers; |
| 1323 |
|
| 1324 |
return 0; |
| 1325 |
} |
| 1326 |
|
| 1327 |
static int dhcp_message_verify_header( |
| 1328 |
const struct iovec *iov, |
| 1329 |
uint8_t op, |
| 1330 |
const uint32_t *xid, |
| 1331 |
uint16_t arp_type, |
| 1332 |
const struct hw_addr_data *hw_addr) { |
| 1333 |
|
| 1334 |
assert(iov); |
| 1335 |
assert(iovec_is_valid(iov)); |
| 1336 |
assert(IN_SET(op, 0, BOOTREQUEST, BOOTREPLY)); /* when 0, both BOOTREQUEST and BOOTREPLY are accepted */ |
| 1337 |
|
| 1338 |
POINTER_MAY_BE_NULL(xid); |
| 1339 |
POINTER_MAY_BE_NULL(hw_addr); |
| 1340 |
|
| 1341 |
if (iov->iov_len < sizeof(DHCPMessageHeader)) |
| 1342 |
return -EBADMSG; |
| 1343 |
|
| 1344 |
/* DHCP travels over UDP, so anything larger than the maximum UDP payload cannot be a valid |
| 1345 |
* message and would also be impossible to rebuild as a UDP packet. */ |
| 1346 |
if (iov->iov_len > UDP_PAYLOAD_MAX_SIZE) |
| 1347 |
return -EBADMSG; |
| 1348 |
|
| 1349 |
const DHCPMessageHeader *header = iov->iov_base; |
| 1350 |
|
| 1351 |
if (!IN_SET(header->op, BOOTREQUEST, BOOTREPLY)) |
| 1352 |
return -EBADMSG; |
| 1353 |
if (op != 0 && header->op != op) |
| 1354 |
return -EBADMSG; |
| 1355 |
|
| 1356 |
if (xid && be32toh(header->xid) != *xid) |
| 1357 |
return -EBADMSG; |
| 1358 |
|
| 1359 |
if (arp_type <= UINT8_MAX && header->htype != arp_type) |
| 1360 |
return -EBADMSG; |
| 1361 |
|
| 1362 |
if (header->hlen > sizeof_field(DHCPMessageHeader, chaddr)) |
| 1363 |
return -EBADMSG; |
| 1364 |
|
| 1365 |
if (hw_addr && memcmp_nn(header->chaddr, header->hlen, hw_addr->bytes, hw_addr->length) != 0) |
| 1366 |
return -EBADMSG; |
| 1367 |
|
| 1368 |
return 0; |
| 1369 |
} |
| 1370 |
|
| 1371 |
int dhcp_message_parse( |
| 1372 |
const struct iovec *iov, |
| 1373 |
uint8_t op, |
| 1374 |
const uint32_t *xid, |
| 1375 |
uint16_t arp_type, |
| 1376 |
const struct hw_addr_data *hw_addr, |
| 1377 |
sd_dhcp_message **ret) { |
| 1378 |
|
| 1379 |
int r; |
| 1380 |
|
| 1381 |
assert(iov); |
| 1382 |
assert(ret); |
| 1383 |
|
| 1384 |
r = dhcp_message_verify_header(iov, op, xid, arp_type, hw_addr); |
| 1385 |
if (r < 0) |
| 1386 |
return r; |
| 1387 |
|
| 1388 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *message = NULL; |
| 1389 |
r = dhcp_message_new(&message); |
| 1390 |
if (r < 0) |
| 1391 |
return r; |
| 1392 |
|
| 1393 |
memcpy(&message->header, iov->iov_base, sizeof(DHCPMessageHeader)); |
| 1394 |
|
| 1395 |
if (be32toh(message->header.magic) != DHCP_MAGIC_COOKIE) { |
| 1396 |
/* Should be BOOTP, and no options. */ |
| 1397 |
*ret = TAKE_PTR(message); |
| 1398 |
return 0; |
| 1399 |
} |
| 1400 |
|
| 1401 |
/* In the BOOTP protocol (RFC 951), the vendor field (magic + options) is 64 bytes, but here we do |
| 1402 |
* not check the length, and support all DHCP options even if we are running as BOOTP client. */ |
| 1403 |
|
| 1404 |
r = tlv_parse(&message->options, &IOVEC_SHIFT(iov, sizeof(DHCPMessageHeader))); |
| 1405 |
if (r < 0) |
| 1406 |
return r; |
| 1407 |
|
| 1408 |
/* Parse SD_DHCP_OPTION_OVERLOAD (52) to determine if we should parse sname and/or file. */ |
| 1409 |
uint8_t overload = DHCP_OVERLOAD_NONE; |
| 1410 |
(void) dhcp_message_get_option_u8(message, SD_DHCP_OPTION_OVERLOAD, &overload); |
| 1411 |
|
| 1412 |
if (FLAGS_SET(overload, DHCP_OVERLOAD_FILE)) { |
| 1413 |
r = tlv_parse(&message->options, &IOVEC_MAKE(message->header.file, sizeof(message->header.file))); |
| 1414 |
if (r < 0) |
| 1415 |
return r; |
| 1416 |
|
| 1417 |
/* The content of the overloaded field has been merged into options. Clear it so that the |
| 1418 |
* field is not re-parsed (which would duplicate the options) and not re-emitted verbatim |
| 1419 |
* by dhcp_message_build(), ensuring parse/build is idempotent. */ |
| 1420 |
memzero(message->header.file, sizeof(message->header.file)); |
| 1421 |
} |
| 1422 |
|
| 1423 |
if (FLAGS_SET(overload, DHCP_OVERLOAD_SNAME)) { |
| 1424 |
r = tlv_parse(&message->options, &IOVEC_MAKE(message->header.sname, sizeof(message->header.sname))); |
| 1425 |
if (r < 0) |
| 1426 |
return r; |
| 1427 |
|
| 1428 |
memzero(message->header.sname, sizeof(message->header.sname)); |
| 1429 |
} |
| 1430 |
|
| 1431 |
*ret = TAKE_PTR(message); |
| 1432 |
return 0; |
| 1433 |
} |
| 1434 |
|
| 1435 |
size_t dhcp_message_payload_size(sd_dhcp_message *message) { |
| 1436 |
assert(message); |
| 1437 |
|
| 1438 |
return MAX(size_add(sizeof(DHCPMessageHeader), tlv_size(&message->options)), BOOTP_MESSAGE_SIZE); |
| 1439 |
} |
| 1440 |
|
| 1441 |
size_t dhcp_message_packet_size(sd_dhcp_message *message) { |
| 1442 |
assert(message); |
| 1443 |
|
| 1444 |
return size_add(sizeof(struct iphdr) + sizeof(struct udphdr), dhcp_message_payload_size(message)); |
| 1445 |
} |
| 1446 |
|
| 1447 |
int dhcp_message_build(sd_dhcp_message *message, struct iovec_wrapper *ret) { |
| 1448 |
int r; |
| 1449 |
|
| 1450 |
assert(message); |
| 1451 |
assert(ret); |
| 1452 |
|
| 1453 |
size_t size = size_add(sizeof(DHCPMessageHeader), tlv_size(&message->options)); |
| 1454 |
if (size > UDP_PAYLOAD_MAX_SIZE) |
| 1455 |
return -E2BIG; |
| 1456 |
|
| 1457 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 1458 |
r = iovw_extend(&iovw, &message->header, sizeof(DHCPMessageHeader)); |
| 1459 |
if (r < 0) |
| 1460 |
return r; |
| 1461 |
|
| 1462 |
struct iovec options; |
| 1463 |
r = tlv_build(&message->options, &options); |
| 1464 |
if (r < 0) |
| 1465 |
return r; |
| 1466 |
|
| 1467 |
r = iovw_consume_iov(&iovw, &options); |
| 1468 |
if (r < 0) |
| 1469 |
return r; |
| 1470 |
|
| 1471 |
/* For compatibility with other implementations and network appliances, the message size should be at |
| 1472 |
* least 300 bytes, which is the size of BOOTP message. */ |
| 1473 |
size_t padding_size = LESS_BY(BOOTP_MESSAGE_SIZE, size); |
| 1474 |
if (padding_size > 0) { |
| 1475 |
uint8_t *padding = new0(uint8_t, padding_size); |
| 1476 |
if (!padding) |
| 1477 |
return -ENOMEM; |
| 1478 |
|
| 1479 |
r = iovw_consume(&iovw, padding, padding_size); |
| 1480 |
if (r < 0) |
| 1481 |
return r; |
| 1482 |
} |
| 1483 |
|
| 1484 |
*ret = TAKE_STRUCT(iovw); |
| 1485 |
return 0; |
| 1486 |
} |
| 1487 |
|
| 1488 |
int dhcp_message_build_json(sd_dhcp_message *message, sd_json_variant **ret) { |
| 1489 |
int r; |
| 1490 |
|
| 1491 |
assert(message); |
| 1492 |
assert(message->header.hlen <= sizeof(message->header.chaddr)); |
| 1493 |
assert(ret); |
| 1494 |
|
| 1495 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1496 |
r = sd_json_buildo( |
| 1497 |
&v, |
| 1498 |
SD_JSON_BUILD_PAIR_UNSIGNED("op", message->header.op), |
| 1499 |
SD_JSON_BUILD_PAIR_UNSIGNED("htype", message->header.htype), |
| 1500 |
SD_JSON_BUILD_PAIR_UNSIGNED("hops", message->header.hops), |
| 1501 |
SD_JSON_BUILD_PAIR_UNSIGNED("xid", be32toh(message->header.xid)), |
| 1502 |
SD_JSON_BUILD_PAIR_UNSIGNED("secs", be16toh(message->header.secs)), |
| 1503 |
SD_JSON_BUILD_PAIR_UNSIGNED("flags", be16toh(message->header.flags)), |
| 1504 |
JSON_BUILD_PAIR_HEX_NON_EMPTY("ciaddr", &message->header.ciaddr, sizeof(message->header.ciaddr)), |
| 1505 |
JSON_BUILD_PAIR_HEX_NON_EMPTY("yiaddr", &message->header.yiaddr, sizeof(message->header.yiaddr)), |
| 1506 |
JSON_BUILD_PAIR_HEX_NON_EMPTY("siaddr", &message->header.siaddr, sizeof(message->header.siaddr)), |
| 1507 |
JSON_BUILD_PAIR_HEX_NON_EMPTY("giaddr", &message->header.giaddr, sizeof(message->header.giaddr)), |
| 1508 |
JSON_BUILD_PAIR_HEX_NON_EMPTY("chaddr", message->header.chaddr, message->header.hlen)); |
| 1509 |
if (r < 0) |
| 1510 |
return r; |
| 1511 |
|
| 1512 |
uint8_t overload = DHCP_OVERLOAD_NONE; |
| 1513 |
(void) dhcp_message_get_option_u8(message, SD_DHCP_OPTION_OVERLOAD, &overload); |
| 1514 |
|
| 1515 |
if (!FLAGS_SET(overload, DHCP_OVERLOAD_SNAME) && !eqzero(message->header.sname)) { |
| 1516 |
r = sd_json_variant_merge_objectbo( |
| 1517 |
&v, |
| 1518 |
JSON_BUILD_PAIR_HEX_NON_EMPTY("sname", message->header.sname, sizeof(message->header.sname))); |
| 1519 |
if (r < 0) |
| 1520 |
return r; |
| 1521 |
} |
| 1522 |
|
| 1523 |
if (!FLAGS_SET(overload, DHCP_OVERLOAD_FILE) && !eqzero(message->header.file)) { |
| 1524 |
r = sd_json_variant_merge_objectbo( |
| 1525 |
&v, |
| 1526 |
JSON_BUILD_PAIR_HEX_NON_EMPTY("file", message->header.file, sizeof(message->header.file))); |
| 1527 |
if (r < 0) |
| 1528 |
return r; |
| 1529 |
} |
| 1530 |
|
| 1531 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; |
| 1532 |
r = tlv_build_json(&message->options, &w); |
| 1533 |
if (r < 0) |
| 1534 |
return r; |
| 1535 |
|
| 1536 |
r = sd_json_variant_merge_objectbo( |
| 1537 |
&v, |
| 1538 |
JSON_BUILD_PAIR_VARIANT_NON_NULL("options", w)); |
| 1539 |
if (r < 0) |
| 1540 |
return r; |
| 1541 |
|
| 1542 |
*ret = TAKE_PTR(v); |
| 1543 |
return 0; |
| 1544 |
} |
| 1545 |
|
| 1546 |
typedef struct MessageParam { |
| 1547 |
uint8_t op; |
| 1548 |
uint8_t htype; |
| 1549 |
uint8_t hops; |
| 1550 |
uint32_t xid; |
| 1551 |
uint16_t secs; |
| 1552 |
uint16_t flags; |
| 1553 |
struct iovec ciaddr; |
| 1554 |
struct iovec yiaddr; |
| 1555 |
struct iovec siaddr; |
| 1556 |
struct iovec giaddr; |
| 1557 |
struct iovec chaddr; |
| 1558 |
struct iovec sname; |
| 1559 |
struct iovec file; |
| 1560 |
TLV *options; |
| 1561 |
} MessageParam; |
| 1562 |
|
| 1563 |
static void message_param_done(MessageParam *p) { |
| 1564 |
assert(p); |
| 1565 |
|
| 1566 |
iovec_done(&p->ciaddr); |
| 1567 |
iovec_done(&p->yiaddr); |
| 1568 |
iovec_done(&p->siaddr); |
| 1569 |
iovec_done(&p->giaddr); |
| 1570 |
iovec_done(&p->chaddr); |
| 1571 |
iovec_done(&p->sname); |
| 1572 |
iovec_done(&p->file); |
| 1573 |
tlv_unref(p->options); |
| 1574 |
} |
| 1575 |
|
| 1576 |
static int dispatch_options(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { |
| 1577 |
TLV **options = ASSERT_PTR(userdata); |
| 1578 |
int r; |
| 1579 |
|
| 1580 |
if (*options) |
| 1581 |
return -EINVAL; /* multiple options field? */ |
| 1582 |
|
| 1583 |
_cleanup_(tlv_unrefp) TLV *tlv = tlv_new(TLV_DHCP4); |
| 1584 |
if (!tlv) |
| 1585 |
return -ENOMEM; |
| 1586 |
|
| 1587 |
r = tlv_parse_json(tlv, v); |
| 1588 |
if (r < 0) |
| 1589 |
return r; |
| 1590 |
|
| 1591 |
*options = TAKE_PTR(tlv); |
| 1592 |
return 0; |
| 1593 |
} |
| 1594 |
|
| 1595 |
int dhcp_message_parse_json(sd_json_variant *v, sd_dhcp_message **ret) { |
| 1596 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 1597 |
{ "op", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(MessageParam, op), SD_JSON_MANDATORY }, |
| 1598 |
{ "htype", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(MessageParam, htype), 0 }, |
| 1599 |
{ "hops", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(MessageParam, hops), 0 }, |
| 1600 |
{ "xid", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(MessageParam, xid), 0 }, |
| 1601 |
{ "secs", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(MessageParam, secs), 0 }, |
| 1602 |
{ "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(MessageParam, flags), 0 }, |
| 1603 |
{ "ciaddr", SD_JSON_VARIANT_STRING, json_dispatch_unhex_iovec, offsetof(MessageParam, ciaddr), 0 }, |
| 1604 |
{ "yiaddr", SD_JSON_VARIANT_STRING, json_dispatch_unhex_iovec, offsetof(MessageParam, yiaddr), 0 }, |
| 1605 |
{ "siaddr", SD_JSON_VARIANT_STRING, json_dispatch_unhex_iovec, offsetof(MessageParam, siaddr), 0 }, |
| 1606 |
{ "giaddr", SD_JSON_VARIANT_STRING, json_dispatch_unhex_iovec, offsetof(MessageParam, giaddr), 0 }, |
| 1607 |
{ "chaddr", SD_JSON_VARIANT_STRING, json_dispatch_unhex_iovec, offsetof(MessageParam, chaddr), 0 }, |
| 1608 |
{ "sname", SD_JSON_VARIANT_STRING, json_dispatch_unhex_iovec, offsetof(MessageParam, sname), 0 }, |
| 1609 |
{ "file", SD_JSON_VARIANT_STRING, json_dispatch_unhex_iovec, offsetof(MessageParam, file), 0 }, |
| 1610 |
{ "options", SD_JSON_VARIANT_ARRAY, dispatch_options, offsetof(MessageParam, options), 0 }, |
| 1611 |
{}, |
| 1612 |
}; |
| 1613 |
|
| 1614 |
int r; |
| 1615 |
|
| 1616 |
assert(v); |
| 1617 |
assert(ret); |
| 1618 |
|
| 1619 |
_cleanup_(message_param_done) MessageParam p = {}; |
| 1620 |
r = sd_json_dispatch(v, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p); |
| 1621 |
if (r < 0) |
| 1622 |
return r; |
| 1623 |
|
| 1624 |
if (!IN_SET(p.op, BOOTREQUEST, BOOTREPLY)) |
| 1625 |
return -EINVAL; |
| 1626 |
if (!iovec_is_valid(&p.ciaddr) || !IN_SET(p.ciaddr.iov_len, 0, sizeof_field(sd_dhcp_message, header.ciaddr))) |
| 1627 |
return -EINVAL; |
| 1628 |
if (!iovec_is_valid(&p.yiaddr) || !IN_SET(p.yiaddr.iov_len, 0, sizeof_field(sd_dhcp_message, header.yiaddr))) |
| 1629 |
return -EINVAL; |
| 1630 |
if (!iovec_is_valid(&p.siaddr) || !IN_SET(p.siaddr.iov_len, 0, sizeof_field(sd_dhcp_message, header.siaddr))) |
| 1631 |
return -EINVAL; |
| 1632 |
if (!iovec_is_valid(&p.giaddr) || !IN_SET(p.giaddr.iov_len, 0, sizeof_field(sd_dhcp_message, header.giaddr))) |
| 1633 |
return -EINVAL; |
| 1634 |
if (!iovec_is_valid(&p.chaddr) || p.chaddr.iov_len > sizeof_field(sd_dhcp_message, header.chaddr)) |
| 1635 |
return -EINVAL; |
| 1636 |
if (!iovec_is_valid(&p.sname) || p.sname.iov_len > sizeof_field(sd_dhcp_message, header.sname)) |
| 1637 |
return -EINVAL; |
| 1638 |
if (!iovec_is_valid(&p.file) || p.file.iov_len > sizeof_field(sd_dhcp_message, header.file)) |
| 1639 |
return -EINVAL; |
| 1640 |
|
| 1641 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *message = NULL; |
| 1642 |
r = dhcp_message_new(&message); |
| 1643 |
if (r < 0) |
| 1644 |
return r; |
| 1645 |
|
| 1646 |
message->header = (DHCPMessageHeader) { |
| 1647 |
.op = p.op, |
| 1648 |
.htype = p.htype, |
| 1649 |
.hlen = p.chaddr.iov_len, |
| 1650 |
.hops = p.hops, |
| 1651 |
.xid = htobe32(p.xid), |
| 1652 |
.secs = htobe16(p.secs), |
| 1653 |
.flags = htobe16(p.flags), |
| 1654 |
.magic = htobe32(DHCP_MAGIC_COOKIE), |
| 1655 |
}; |
| 1656 |
|
| 1657 |
memcpy_safe(&message->header.ciaddr, p.ciaddr.iov_base, p.ciaddr.iov_len); |
| 1658 |
memcpy_safe(&message->header.yiaddr, p.yiaddr.iov_base, p.yiaddr.iov_len); |
| 1659 |
memcpy_safe(&message->header.siaddr, p.siaddr.iov_base, p.siaddr.iov_len); |
| 1660 |
memcpy_safe(&message->header.giaddr, p.giaddr.iov_base, p.giaddr.iov_len); |
| 1661 |
memcpy_safe(message->header.chaddr, p.chaddr.iov_base, p.chaddr.iov_len); |
| 1662 |
memcpy_safe(message->header.sname, p.sname.iov_base, p.sname.iov_len); |
| 1663 |
memcpy_safe(message->header.file, p.file.iov_base, p.file.iov_len); |
| 1664 |
if (p.options) { |
| 1665 |
message->options = TAKE_STRUCT(*p.options); |
| 1666 |
p.options = mfree(p.options); |
| 1667 |
} |
| 1668 |
|
| 1669 |
*ret = TAKE_PTR(message); |
| 1670 |
return 0; |
| 1671 |
} |
| 1672 |
|
| 1673 |
int dhcp_message_send_udp( |
| 1674 |
sd_dhcp_message *message, |
| 1675 |
int fd, |
| 1676 |
be32_t src_addr, |
| 1677 |
be32_t dst_addr, |
| 1678 |
uint16_t dst_port) { |
| 1679 |
|
| 1680 |
int r; |
| 1681 |
|
| 1682 |
assert(message); |
| 1683 |
assert(fd >= 0); |
| 1684 |
|
| 1685 |
_cleanup_(iovw_done_free) struct iovec_wrapper payload = {}; |
| 1686 |
r = dhcp_message_build(message, &payload); |
| 1687 |
if (r < 0) |
| 1688 |
return r; |
| 1689 |
|
| 1690 |
union sockaddr_union sa = { |
| 1691 |
.in.sin_family = AF_INET, |
| 1692 |
.in.sin_port = htobe16(dst_port), |
| 1693 |
.in.sin_addr.s_addr = dst_addr, |
| 1694 |
}; |
| 1695 |
|
| 1696 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in_pktinfo))) control = {}; |
| 1697 |
|
| 1698 |
struct msghdr msg = { |
| 1699 |
.msg_name = &sa, |
| 1700 |
.msg_namelen = sizeof(sa.in), |
| 1701 |
.msg_iov = payload.iovec, |
| 1702 |
.msg_iovlen = payload.count, |
| 1703 |
}; |
| 1704 |
|
| 1705 |
if (src_addr != INADDR_ANY) { |
| 1706 |
msg.msg_control = &control; |
| 1707 |
msg.msg_controllen = sizeof(control); |
| 1708 |
|
| 1709 |
struct cmsghdr *cmsg = ASSERT_PTR(CMSG_FIRSTHDR(&msg)); |
| 1710 |
cmsg->cmsg_level = IPPROTO_IP; |
| 1711 |
cmsg->cmsg_type = IP_PKTINFO; |
| 1712 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
| 1713 |
|
| 1714 |
struct in_pktinfo *pktinfo = ASSERT_PTR(CMSG_TYPED_DATA(cmsg, struct in_pktinfo)); |
| 1715 |
pktinfo->ipi_spec_dst.s_addr = src_addr; |
| 1716 |
} |
| 1717 |
|
| 1718 |
if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0) |
| 1719 |
return -errno; |
| 1720 |
|
| 1721 |
return 0; |
| 1722 |
} |
| 1723 |
|
| 1724 |
int dhcp_message_send_raw( |
| 1725 |
sd_dhcp_message *message, |
| 1726 |
int fd, |
| 1727 |
int ifindex, |
| 1728 |
be32_t src_addr, |
| 1729 |
uint16_t src_port, |
| 1730 |
const struct hw_addr_data *dst_hw_addr, |
| 1731 |
be32_t dst_addr, |
| 1732 |
uint16_t dst_port, |
| 1733 |
int ip_service_type) { |
| 1734 |
|
| 1735 |
int r; |
| 1736 |
|
| 1737 |
assert(message); |
| 1738 |
assert(fd >= 0); |
| 1739 |
assert(ifindex > 0); |
| 1740 |
assert(dst_hw_addr); |
| 1741 |
|
| 1742 |
_cleanup_(iovw_done_free) struct iovec_wrapper payload = {}; |
| 1743 |
r = dhcp_message_build(message, &payload); |
| 1744 |
if (r < 0) |
| 1745 |
return r; |
| 1746 |
|
| 1747 |
struct iphdr ip; |
| 1748 |
struct udphdr udp; |
| 1749 |
r = udp_packet_build( |
| 1750 |
src_addr, |
| 1751 |
src_port, |
| 1752 |
dst_addr, |
| 1753 |
dst_port, |
| 1754 |
ip_service_type, |
| 1755 |
&payload, |
| 1756 |
&ip, |
| 1757 |
&udp); |
| 1758 |
if (r < 0) |
| 1759 |
return r; |
| 1760 |
|
| 1761 |
_cleanup_(iovw_done) struct iovec_wrapper iovw = {}; |
| 1762 |
r = iovw_put(&iovw, &ip, sizeof(struct iphdr)); |
| 1763 |
if (r < 0) |
| 1764 |
return r; |
| 1765 |
|
| 1766 |
r = iovw_put(&iovw, &udp, sizeof(struct udphdr)); |
| 1767 |
if (r < 0) |
| 1768 |
return r; |
| 1769 |
|
| 1770 |
r = iovw_put_iovw(&iovw, &payload); |
| 1771 |
if (r < 0) |
| 1772 |
return r; |
| 1773 |
|
| 1774 |
union sockaddr_union sa = { |
| 1775 |
.ll.sll_family = AF_PACKET, |
| 1776 |
.ll.sll_protocol = htobe16(ETH_P_IP), |
| 1777 |
.ll.sll_ifindex = ifindex, |
| 1778 |
.ll.sll_halen = dst_hw_addr->length, |
| 1779 |
}; |
| 1780 |
|
| 1781 |
memcpy_safe(sa.ll.sll_addr, dst_hw_addr->bytes, dst_hw_addr->length); |
| 1782 |
|
| 1783 |
struct msghdr mh = { |
| 1784 |
.msg_name = &sa.sa, |
| 1785 |
.msg_namelen = sockaddr_ll_len(&sa.ll), |
| 1786 |
.msg_iov = iovw.iovec, |
| 1787 |
.msg_iovlen = iovw.count, |
| 1788 |
}; |
| 1789 |
|
| 1790 |
if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0) |
| 1791 |
return -errno; |
| 1792 |
|
| 1793 |
return 0; |
| 1794 |
} |