| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "bitmap.h" |
| 5 |
#include "dns-answer.h" |
| 6 |
#include "dns-domain.h" |
| 7 |
#include "dns-packet.h" |
| 8 |
#include "dns-question.h" |
| 9 |
#include "dns-rr.h" |
| 10 |
#include "escape.h" |
| 11 |
#include "log.h" |
| 12 |
#include "memory-util.h" |
| 13 |
#include "set.h" |
| 14 |
#include "siphash24.h" |
| 15 |
#include "stdio-util.h" |
| 16 |
#include "string-table.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "time-util.h" |
| 19 |
#include "unaligned.h" |
| 20 |
#include "utf8.h" |
| 21 |
|
| 22 |
#define EDNS0_OPT_DO (1<<15) |
| 23 |
|
| 24 |
assert_cc(DNS_PACKET_SIZE_START > DNS_PACKET_HEADER_SIZE); |
| 25 |
|
| 26 |
typedef struct DnsPacketRewinder { |
| 27 |
DnsPacket *packet; |
| 28 |
size_t saved_rindex; |
| 29 |
} DnsPacketRewinder; |
| 30 |
|
| 31 |
static void rewind_dns_packet(DnsPacketRewinder *rewinder) { |
| 32 |
if (rewinder->packet) |
| 33 |
dns_packet_rewind(rewinder->packet, rewinder->saved_rindex); |
| 34 |
} |
| 35 |
|
| 36 |
#define REWINDER_INIT(p) { \ |
| 37 |
.packet = (p), \ |
| 38 |
.saved_rindex = (p)->rindex, \ |
| 39 |
} |
| 40 |
#define CANCEL_REWINDER(rewinder) do { (rewinder).packet = NULL; } while (0) |
| 41 |
|
| 42 |
uint16_t dns_packet_rcode(DnsPacket *p) { |
| 43 |
uint16_t rcode; |
| 44 |
|
| 45 |
assert(p); |
| 46 |
|
| 47 |
if (p->opt) |
| 48 |
rcode = (uint16_t) ((p->opt->ttl >> 20) & 0xFF0); |
| 49 |
else |
| 50 |
rcode = 0; |
| 51 |
|
| 52 |
return rcode | (be16toh(DNS_PACKET_HEADER(p)->flags) & 0xF); |
| 53 |
}; |
| 54 |
|
| 55 |
uint16_t dns_packet_payload_size_max(DnsPacket *p) { |
| 56 |
assert(p); |
| 57 |
|
| 58 |
/* Returns the advertised maximum size for replies, or the DNS default if there's nothing defined. */ |
| 59 |
|
| 60 |
if (p->ipproto == IPPROTO_TCP) /* we ignore EDNS(0) size data on TCP, like everybody else */ |
| 61 |
return DNS_PACKET_SIZE_MAX; |
| 62 |
|
| 63 |
if (p->opt) |
| 64 |
return MAX(DNS_PACKET_UNICAST_SIZE_MAX, p->opt->key->class); |
| 65 |
|
| 66 |
return DNS_PACKET_UNICAST_SIZE_MAX; |
| 67 |
} |
| 68 |
|
| 69 |
bool dns_packet_do(DnsPacket *p) { |
| 70 |
assert(p); |
| 71 |
|
| 72 |
if (!p->opt) |
| 73 |
return false; |
| 74 |
|
| 75 |
return !!(p->opt->ttl & (1U << 15)); |
| 76 |
} |
| 77 |
|
| 78 |
bool dns_packet_version_supported(DnsPacket *p) { |
| 79 |
assert(p); |
| 80 |
|
| 81 |
/* Returns true if this packet is in a version we support. Which means either non-EDNS or EDNS(0), but not EDNS |
| 82 |
* of any newer versions */ |
| 83 |
|
| 84 |
if (!p->opt) |
| 85 |
return true; |
| 86 |
|
| 87 |
return DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(p->opt); |
| 88 |
} |
| 89 |
|
| 90 |
int dns_packet_new( |
| 91 |
DnsPacket **ret, |
| 92 |
DnsProtocol protocol, |
| 93 |
size_t min_alloc_dsize, |
| 94 |
size_t max_size) { |
| 95 |
|
| 96 |
DnsPacket *p; |
| 97 |
size_t a; |
| 98 |
|
| 99 |
assert(ret); |
| 100 |
assert(max_size >= DNS_PACKET_HEADER_SIZE); |
| 101 |
|
| 102 |
if (max_size > DNS_PACKET_SIZE_MAX) |
| 103 |
max_size = DNS_PACKET_SIZE_MAX; |
| 104 |
|
| 105 |
/* The caller may not check what is going to be truly allocated, so do not allow to |
| 106 |
* allocate a DNS packet bigger than DNS_PACKET_SIZE_MAX. |
| 107 |
*/ |
| 108 |
if (min_alloc_dsize > DNS_PACKET_SIZE_MAX) |
| 109 |
return log_error_errno(SYNTHETIC_ERRNO(EFBIG), |
| 110 |
"Requested packet data size too big: %zu", |
| 111 |
min_alloc_dsize); |
| 112 |
|
| 113 |
/* When dns_packet_new() is called with min_alloc_dsize == 0, allocate more than the |
| 114 |
* absolute minimum (which is the dns packet header size), to avoid |
| 115 |
* resizing immediately again after appending the first data to the packet. |
| 116 |
*/ |
| 117 |
if (min_alloc_dsize < DNS_PACKET_HEADER_SIZE) |
| 118 |
a = DNS_PACKET_SIZE_START; |
| 119 |
else |
| 120 |
a = min_alloc_dsize; |
| 121 |
|
| 122 |
/* round up to next page size */ |
| 123 |
/* Silence static analyzers */ |
| 124 |
assert(a <= SIZE_MAX - ALIGN(sizeof(DnsPacket))); |
| 125 |
a = PAGE_ALIGN(ALIGN(sizeof(DnsPacket)) + a) - ALIGN(sizeof(DnsPacket)); |
| 126 |
|
| 127 |
/* make sure we never allocate more than useful */ |
| 128 |
if (a > max_size) |
| 129 |
a = max_size; |
| 130 |
|
| 131 |
p = malloc0(ALIGN(sizeof(DnsPacket)) + a); |
| 132 |
if (!p) |
| 133 |
return -ENOMEM; |
| 134 |
|
| 135 |
*p = (DnsPacket) { |
| 136 |
.n_ref = 1, |
| 137 |
.protocol = protocol, |
| 138 |
.size = DNS_PACKET_HEADER_SIZE, |
| 139 |
.rindex = DNS_PACKET_HEADER_SIZE, |
| 140 |
.allocated = a, |
| 141 |
.max_size = max_size, |
| 142 |
.opt_start = SIZE_MAX, |
| 143 |
.opt_size = SIZE_MAX, |
| 144 |
}; |
| 145 |
|
| 146 |
*ret = p; |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
void dns_packet_set_flags(DnsPacket *p, bool dnssec_checking_disabled, bool truncated) { |
| 152 |
|
| 153 |
DnsPacketHeader *h; |
| 154 |
|
| 155 |
assert(p); |
| 156 |
|
| 157 |
h = DNS_PACKET_HEADER(p); |
| 158 |
|
| 159 |
switch (p->protocol) { |
| 160 |
case DNS_PROTOCOL_LLMNR: |
| 161 |
assert(!truncated); |
| 162 |
|
| 163 |
h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */, |
| 164 |
0 /* opcode */, |
| 165 |
0 /* c */, |
| 166 |
0 /* tc */, |
| 167 |
0 /* t */, |
| 168 |
0 /* ra */, |
| 169 |
0 /* ad */, |
| 170 |
0 /* cd */, |
| 171 |
0 /* rcode */)); |
| 172 |
break; |
| 173 |
|
| 174 |
case DNS_PROTOCOL_MDNS: |
| 175 |
h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */, |
| 176 |
0 /* opcode */, |
| 177 |
0 /* aa */, |
| 178 |
truncated /* tc */, |
| 179 |
0 /* rd (ask for recursion) */, |
| 180 |
0 /* ra */, |
| 181 |
0 /* ad */, |
| 182 |
0 /* cd */, |
| 183 |
0 /* rcode */)); |
| 184 |
break; |
| 185 |
|
| 186 |
default: |
| 187 |
assert(!truncated); |
| 188 |
|
| 189 |
h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */, |
| 190 |
0 /* opcode */, |
| 191 |
0 /* aa */, |
| 192 |
0 /* tc */, |
| 193 |
1 /* rd (ask for recursion) */, |
| 194 |
0 /* ra */, |
| 195 |
0 /* ad */, |
| 196 |
dnssec_checking_disabled /* cd */, |
| 197 |
0 /* rcode */)); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
int dns_packet_new_query(DnsPacket **ret, DnsProtocol protocol, size_t min_alloc_dsize, bool dnssec_checking_disabled) { |
| 202 |
DnsPacket *p; |
| 203 |
int r; |
| 204 |
|
| 205 |
assert(ret); |
| 206 |
|
| 207 |
r = dns_packet_new(&p, protocol, min_alloc_dsize, DNS_PACKET_SIZE_MAX); |
| 208 |
if (r < 0) |
| 209 |
return r; |
| 210 |
|
| 211 |
/* Always set the TC bit to 0 initially. |
| 212 |
* If there are multiple packets later, we'll update the bit shortly before sending. |
| 213 |
*/ |
| 214 |
dns_packet_set_flags(p, dnssec_checking_disabled, false); |
| 215 |
|
| 216 |
*ret = p; |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
int dns_packet_dup(DnsPacket **ret, DnsPacket *p) { |
| 221 |
DnsPacket *c; |
| 222 |
int r; |
| 223 |
|
| 224 |
assert(ret); |
| 225 |
assert(p); |
| 226 |
|
| 227 |
r = dns_packet_validate(p); |
| 228 |
if (r < 0) |
| 229 |
return r; |
| 230 |
|
| 231 |
/* Silence static analyzers */ |
| 232 |
assert(p->size <= SIZE_MAX - ALIGN(sizeof(DnsPacket))); |
| 233 |
c = malloc(ALIGN(sizeof(DnsPacket)) + p->size); |
| 234 |
if (!c) |
| 235 |
return -ENOMEM; |
| 236 |
|
| 237 |
*c = (DnsPacket) { |
| 238 |
.n_ref = 1, |
| 239 |
.protocol = p->protocol, |
| 240 |
.size = p->size, |
| 241 |
.rindex = DNS_PACKET_HEADER_SIZE, |
| 242 |
.allocated = p->size, |
| 243 |
.max_size = p->max_size, |
| 244 |
.opt_start = SIZE_MAX, |
| 245 |
.opt_size = SIZE_MAX, |
| 246 |
}; |
| 247 |
|
| 248 |
memcpy(DNS_PACKET_DATA(c), DNS_PACKET_DATA(p), p->size); |
| 249 |
|
| 250 |
*ret = c; |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
DnsPacket *dns_packet_ref(DnsPacket *p) { |
| 255 |
|
| 256 |
if (!p) |
| 257 |
return NULL; |
| 258 |
|
| 259 |
assert(!p->on_stack); |
| 260 |
|
| 261 |
assert(p->n_ref > 0); |
| 262 |
p->n_ref++; |
| 263 |
return p; |
| 264 |
} |
| 265 |
|
| 266 |
static void dns_packet_free(DnsPacket *p) { |
| 267 |
char *s; |
| 268 |
|
| 269 |
assert(p); |
| 270 |
|
| 271 |
dns_question_unref(p->question); |
| 272 |
dns_answer_unref(p->answer); |
| 273 |
dns_resource_record_unref(p->opt); |
| 274 |
|
| 275 |
while ((s = hashmap_steal_first_key(p->names))) |
| 276 |
free(s); |
| 277 |
hashmap_free(p->names); |
| 278 |
|
| 279 |
free(p->_data); |
| 280 |
|
| 281 |
if (!p->on_stack) |
| 282 |
free(p); |
| 283 |
} |
| 284 |
|
| 285 |
DnsPacket *dns_packet_unref(DnsPacket *p) { |
| 286 |
if (!p) |
| 287 |
return NULL; |
| 288 |
|
| 289 |
assert(p->n_ref > 0); |
| 290 |
|
| 291 |
if (p->n_ref == 1) { |
| 292 |
dns_packet_unref(p->more); |
| 293 |
dns_packet_free(p); |
| 294 |
} else |
| 295 |
p->n_ref--; |
| 296 |
|
| 297 |
return NULL; |
| 298 |
} |
| 299 |
|
| 300 |
int dns_packet_validate(DnsPacket *p) { |
| 301 |
assert(p); |
| 302 |
|
| 303 |
if (p->size < DNS_PACKET_HEADER_SIZE) |
| 304 |
return -EBADMSG; |
| 305 |
|
| 306 |
if (p->size > DNS_PACKET_SIZE_MAX) |
| 307 |
return -EBADMSG; |
| 308 |
|
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
int dns_packet_validate_reply(DnsPacket *p) { |
| 313 |
int r; |
| 314 |
|
| 315 |
assert(p); |
| 316 |
|
| 317 |
r = dns_packet_validate(p); |
| 318 |
if (r < 0) |
| 319 |
return r; |
| 320 |
|
| 321 |
if (DNS_PACKET_QR(p) != 1) |
| 322 |
return 0; |
| 323 |
|
| 324 |
if (DNS_PACKET_OPCODE(p) != 0) |
| 325 |
return -EBADMSG; |
| 326 |
|
| 327 |
switch (p->protocol) { |
| 328 |
|
| 329 |
case DNS_PROTOCOL_LLMNR: |
| 330 |
/* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */ |
| 331 |
if (DNS_PACKET_QDCOUNT(p) != 1) |
| 332 |
return -EBADMSG; |
| 333 |
|
| 334 |
break; |
| 335 |
|
| 336 |
case DNS_PROTOCOL_MDNS: |
| 337 |
/* RFC 6762, Section 18 */ |
| 338 |
if (dns_packet_rcode(p) != 0) |
| 339 |
return -EBADMSG; |
| 340 |
|
| 341 |
break; |
| 342 |
|
| 343 |
default: |
| 344 |
; |
| 345 |
} |
| 346 |
|
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
int dns_packet_validate_query(DnsPacket *p) { |
| 351 |
int r; |
| 352 |
|
| 353 |
assert(p); |
| 354 |
|
| 355 |
r = dns_packet_validate(p); |
| 356 |
if (r < 0) |
| 357 |
return r; |
| 358 |
|
| 359 |
if (DNS_PACKET_QR(p) != 0) |
| 360 |
return 0; |
| 361 |
|
| 362 |
if (DNS_PACKET_OPCODE(p) != 0) |
| 363 |
return -EBADMSG; |
| 364 |
|
| 365 |
switch (p->protocol) { |
| 366 |
|
| 367 |
case DNS_PROTOCOL_DNS: |
| 368 |
if (DNS_PACKET_TC(p)) |
| 369 |
return -EBADMSG; |
| 370 |
|
| 371 |
if (DNS_PACKET_QDCOUNT(p) != 1) |
| 372 |
return -EBADMSG; |
| 373 |
|
| 374 |
if (DNS_PACKET_ANCOUNT(p) > 0) |
| 375 |
return -EBADMSG; |
| 376 |
|
| 377 |
/* Note, in most cases, DNS query packet does not have authority section. But some query |
| 378 |
* types, e.g. IXFR, have Authority sections. Hence, unlike the check for LLMNR, we do not |
| 379 |
* check DNS_PACKET_NSCOUNT(p) here. */ |
| 380 |
break; |
| 381 |
|
| 382 |
case DNS_PROTOCOL_LLMNR: |
| 383 |
if (DNS_PACKET_TC(p)) |
| 384 |
return -EBADMSG; |
| 385 |
|
| 386 |
/* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */ |
| 387 |
if (DNS_PACKET_QDCOUNT(p) != 1) |
| 388 |
return -EBADMSG; |
| 389 |
|
| 390 |
/* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */ |
| 391 |
if (DNS_PACKET_ANCOUNT(p) > 0) |
| 392 |
return -EBADMSG; |
| 393 |
|
| 394 |
/* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */ |
| 395 |
if (DNS_PACKET_NSCOUNT(p) > 0) |
| 396 |
return -EBADMSG; |
| 397 |
|
| 398 |
break; |
| 399 |
|
| 400 |
case DNS_PROTOCOL_MDNS: |
| 401 |
/* Note, mDNS query may have truncation flag. So, unlike the check for DNS and LLMNR, |
| 402 |
* we do not check DNS_PACKET_TC(p) here. */ |
| 403 |
|
| 404 |
/* RFC 6762, Section 18 specifies that messages with non-zero RCODE |
| 405 |
* must be silently ignored, and that we must ignore the values of |
| 406 |
* AA, RD, RA, AD, and CD bits. */ |
| 407 |
if (dns_packet_rcode(p) != 0) |
| 408 |
return -EBADMSG; |
| 409 |
|
| 410 |
break; |
| 411 |
|
| 412 |
default: |
| 413 |
; |
| 414 |
} |
| 415 |
|
| 416 |
return 1; |
| 417 |
} |
| 418 |
|
| 419 |
static int dns_packet_extend(DnsPacket *p, size_t add, void **ret, size_t *start) { |
| 420 |
assert(p); |
| 421 |
|
| 422 |
if (p->size + add > p->allocated) { |
| 423 |
size_t a, ms; |
| 424 |
|
| 425 |
a = PAGE_ALIGN((p->size + add) * 2); |
| 426 |
|
| 427 |
ms = dns_packet_size_max(p); |
| 428 |
if (a > ms) |
| 429 |
a = ms; |
| 430 |
|
| 431 |
if (p->size + add > a) |
| 432 |
return -EMSGSIZE; |
| 433 |
|
| 434 |
if (p->_data) { |
| 435 |
void *d; |
| 436 |
|
| 437 |
d = realloc(p->_data, a); |
| 438 |
if (!d) |
| 439 |
return -ENOMEM; |
| 440 |
|
| 441 |
p->_data = d; |
| 442 |
} else { |
| 443 |
p->_data = malloc(a); |
| 444 |
if (!p->_data) |
| 445 |
return -ENOMEM; |
| 446 |
|
| 447 |
memcpy(p->_data, (uint8_t*) p + ALIGN(sizeof(DnsPacket)), p->size); |
| 448 |
memzero((uint8_t*) p->_data + p->size, a - p->size); |
| 449 |
} |
| 450 |
|
| 451 |
p->allocated = a; |
| 452 |
} |
| 453 |
|
| 454 |
if (start) |
| 455 |
*start = p->size; |
| 456 |
|
| 457 |
if (ret) |
| 458 |
*ret = DNS_PACKET_DATA(p) + p->size; |
| 459 |
|
| 460 |
p->size += add; |
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
void dns_packet_truncate(DnsPacket *p, size_t sz) { |
| 465 |
char *s; |
| 466 |
void *n; |
| 467 |
|
| 468 |
assert(p); |
| 469 |
|
| 470 |
if (p->size <= sz) |
| 471 |
return; |
| 472 |
|
| 473 |
HASHMAP_FOREACH_KEY(n, s, p->names) { |
| 474 |
|
| 475 |
if (PTR_TO_SIZE(n) < sz) |
| 476 |
continue; |
| 477 |
|
| 478 |
hashmap_remove(p->names, s); |
| 479 |
free(s); |
| 480 |
} |
| 481 |
|
| 482 |
p->size = sz; |
| 483 |
} |
| 484 |
|
| 485 |
int dns_packet_append_blob(DnsPacket *p, const void *d, size_t l, size_t *start) { |
| 486 |
void *q; |
| 487 |
int r; |
| 488 |
|
| 489 |
assert(p); |
| 490 |
|
| 491 |
r = dns_packet_extend(p, l, &q, start); |
| 492 |
if (r < 0) |
| 493 |
return r; |
| 494 |
|
| 495 |
memcpy_safe(q, d, l); |
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
int dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start) { |
| 500 |
void *d; |
| 501 |
int r; |
| 502 |
|
| 503 |
assert(p); |
| 504 |
|
| 505 |
r = dns_packet_extend(p, sizeof(uint8_t), &d, start); |
| 506 |
if (r < 0) |
| 507 |
return r; |
| 508 |
|
| 509 |
((uint8_t*) d)[0] = v; |
| 510 |
|
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start) { |
| 515 |
void *d; |
| 516 |
int r; |
| 517 |
|
| 518 |
assert(p); |
| 519 |
|
| 520 |
r = dns_packet_extend(p, sizeof(uint16_t), &d, start); |
| 521 |
if (r < 0) |
| 522 |
return r; |
| 523 |
|
| 524 |
unaligned_write_be16(d, v); |
| 525 |
|
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
int dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start) { |
| 530 |
void *d; |
| 531 |
int r; |
| 532 |
|
| 533 |
assert(p); |
| 534 |
|
| 535 |
r = dns_packet_extend(p, sizeof(uint32_t), &d, start); |
| 536 |
if (r < 0) |
| 537 |
return r; |
| 538 |
|
| 539 |
unaligned_write_be32(d, v); |
| 540 |
|
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
int dns_packet_append_string(DnsPacket *p, const char *s, size_t *start) { |
| 545 |
assert(p); |
| 546 |
assert(s); |
| 547 |
|
| 548 |
return dns_packet_append_raw_string(p, s, strlen(s), start); |
| 549 |
} |
| 550 |
|
| 551 |
int dns_packet_append_raw_string(DnsPacket *p, const void *s, size_t size, size_t *start) { |
| 552 |
void *d; |
| 553 |
int r; |
| 554 |
|
| 555 |
assert(p); |
| 556 |
assert(s || size == 0); |
| 557 |
|
| 558 |
if (size > 255) |
| 559 |
return -E2BIG; |
| 560 |
|
| 561 |
r = dns_packet_extend(p, 1 + size, &d, start); |
| 562 |
if (r < 0) |
| 563 |
return r; |
| 564 |
|
| 565 |
((uint8_t*) d)[0] = (uint8_t) size; |
| 566 |
|
| 567 |
memcpy_safe(((uint8_t*) d) + 1, s, size); |
| 568 |
|
| 569 |
return 0; |
| 570 |
} |
| 571 |
|
| 572 |
int dns_packet_append_label(DnsPacket *p, const char *d, size_t l, bool canonical_candidate, size_t *start) { |
| 573 |
uint8_t *w; |
| 574 |
int r; |
| 575 |
|
| 576 |
/* Append a label to a packet. Optionally, does this in DNSSEC |
| 577 |
* canonical form, if this label is marked as a candidate for |
| 578 |
* it, and the canonical form logic is enabled for the |
| 579 |
* packet */ |
| 580 |
|
| 581 |
assert(p); |
| 582 |
assert(d); |
| 583 |
|
| 584 |
if (l > DNS_LABEL_MAX) |
| 585 |
return -E2BIG; |
| 586 |
|
| 587 |
r = dns_packet_extend(p, 1 + l, (void**) &w, start); |
| 588 |
if (r < 0) |
| 589 |
return r; |
| 590 |
|
| 591 |
*(w++) = (uint8_t) l; |
| 592 |
|
| 593 |
if (p->canonical_form && canonical_candidate) |
| 594 |
/* Generate in canonical form, as defined by DNSSEC |
| 595 |
* RFC 4034, Section 6.2, i.e. all lower-case. */ |
| 596 |
for (size_t i = 0; i < l; i++) |
| 597 |
w[i] = (uint8_t) ascii_tolower(d[i]); |
| 598 |
else |
| 599 |
/* Otherwise, just copy the string unaltered. This is |
| 600 |
* essential for DNS-SD, where the casing of labels |
| 601 |
* matters and needs to be retained. */ |
| 602 |
memcpy(w, d, l); |
| 603 |
|
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
int dns_packet_append_name( |
| 608 |
DnsPacket *p, |
| 609 |
const char *name, |
| 610 |
bool allow_compression, |
| 611 |
bool canonical_candidate, |
| 612 |
size_t *start) { |
| 613 |
|
| 614 |
_cleanup_free_ char **added_entries = NULL; /* doesn't own the strings! this is just regular pointer array, not a NULL-terminated strv! */ |
| 615 |
size_t n_added_entries = 0, saved_size; |
| 616 |
int r; |
| 617 |
|
| 618 |
assert(p); |
| 619 |
assert(name); |
| 620 |
|
| 621 |
r = dns_name_is_valid(name); |
| 622 |
if (r < 0) |
| 623 |
return r; |
| 624 |
if (r == 0) |
| 625 |
return -EINVAL; |
| 626 |
|
| 627 |
if (p->refuse_compression) |
| 628 |
allow_compression = false; |
| 629 |
|
| 630 |
saved_size = p->size; |
| 631 |
|
| 632 |
while (!dns_name_is_root(name)) { |
| 633 |
const char *z = name; |
| 634 |
char label[DNS_LABEL_MAX+1]; |
| 635 |
size_t n = 0; |
| 636 |
|
| 637 |
if (allow_compression) |
| 638 |
n = PTR_TO_SIZE(hashmap_get(p->names, name)); |
| 639 |
if (n > 0) { |
| 640 |
assert(n < p->size); |
| 641 |
|
| 642 |
if (n < 0x4000) { |
| 643 |
r = dns_packet_append_uint16(p, 0xC000 | n, NULL); |
| 644 |
if (r < 0) |
| 645 |
goto fail; |
| 646 |
|
| 647 |
goto done; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
r = dns_label_unescape(&name, label, sizeof label, 0); |
| 652 |
if (r < 0) |
| 653 |
goto fail; |
| 654 |
|
| 655 |
r = dns_packet_append_label(p, label, r, canonical_candidate, &n); |
| 656 |
if (r < 0) |
| 657 |
goto fail; |
| 658 |
|
| 659 |
if (allow_compression) { |
| 660 |
_cleanup_free_ char *s = NULL; |
| 661 |
|
| 662 |
if (!GREEDY_REALLOC(added_entries, n_added_entries + 1)) { |
| 663 |
r = -ENOMEM; |
| 664 |
goto fail; |
| 665 |
} |
| 666 |
|
| 667 |
s = strdup(z); |
| 668 |
if (!s) { |
| 669 |
r = -ENOMEM; |
| 670 |
goto fail; |
| 671 |
} |
| 672 |
|
| 673 |
r = hashmap_ensure_put(&p->names, &dns_name_hash_ops, s, SIZE_TO_PTR(n)); |
| 674 |
if (r < 0) |
| 675 |
goto fail; |
| 676 |
|
| 677 |
/* Keep track of the entries we just added (note that the string is owned by the hashtable, not this array!) */ |
| 678 |
added_entries[n_added_entries++] = TAKE_PTR(s); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
r = dns_packet_append_uint8(p, 0, NULL); |
| 683 |
if (r < 0) |
| 684 |
return r; |
| 685 |
|
| 686 |
done: |
| 687 |
if (start) |
| 688 |
*start = saved_size; |
| 689 |
|
| 690 |
return 0; |
| 691 |
|
| 692 |
fail: |
| 693 |
/* Remove all label compression names we added again */ |
| 694 |
FOREACH_ARRAY(s, added_entries, n_added_entries) { |
| 695 |
hashmap_remove(p->names, *s); |
| 696 |
free(*s); |
| 697 |
} |
| 698 |
|
| 699 |
dns_packet_truncate(p, saved_size); |
| 700 |
return r; |
| 701 |
} |
| 702 |
|
| 703 |
int dns_packet_append_key(DnsPacket *p, const DnsResourceKey *k, const DnsAnswerFlags flags, size_t *start) { |
| 704 |
size_t saved_size; |
| 705 |
uint16_t class; |
| 706 |
int r; |
| 707 |
|
| 708 |
assert(p); |
| 709 |
assert(k); |
| 710 |
|
| 711 |
saved_size = p->size; |
| 712 |
|
| 713 |
r = dns_packet_append_name(p, dns_resource_key_name(k), true, true, NULL); |
| 714 |
if (r < 0) |
| 715 |
goto fail; |
| 716 |
|
| 717 |
r = dns_packet_append_uint16(p, k->type, NULL); |
| 718 |
if (r < 0) |
| 719 |
goto fail; |
| 720 |
|
| 721 |
class = flags & DNS_ANSWER_CACHE_FLUSH ? k->class | MDNS_RR_CACHE_FLUSH_OR_QU : k->class; |
| 722 |
r = dns_packet_append_uint16(p, class, NULL); |
| 723 |
if (r < 0) |
| 724 |
goto fail; |
| 725 |
|
| 726 |
if (start) |
| 727 |
*start = saved_size; |
| 728 |
|
| 729 |
return 0; |
| 730 |
|
| 731 |
fail: |
| 732 |
dns_packet_truncate(p, saved_size); |
| 733 |
return r; |
| 734 |
} |
| 735 |
|
| 736 |
static int dns_packet_append_type_window(DnsPacket *p, uint8_t window, uint8_t length, const uint8_t *types, size_t *start) { |
| 737 |
size_t saved_size; |
| 738 |
int r; |
| 739 |
|
| 740 |
assert(p); |
| 741 |
assert(types); |
| 742 |
assert(length > 0); |
| 743 |
|
| 744 |
saved_size = p->size; |
| 745 |
|
| 746 |
r = dns_packet_append_uint8(p, window, NULL); |
| 747 |
if (r < 0) |
| 748 |
goto fail; |
| 749 |
|
| 750 |
r = dns_packet_append_uint8(p, length, NULL); |
| 751 |
if (r < 0) |
| 752 |
goto fail; |
| 753 |
|
| 754 |
r = dns_packet_append_blob(p, types, length, NULL); |
| 755 |
if (r < 0) |
| 756 |
goto fail; |
| 757 |
|
| 758 |
if (start) |
| 759 |
*start = saved_size; |
| 760 |
|
| 761 |
return 0; |
| 762 |
fail: |
| 763 |
dns_packet_truncate(p, saved_size); |
| 764 |
return r; |
| 765 |
} |
| 766 |
|
| 767 |
static int dns_packet_append_types(DnsPacket *p, Bitmap *types, size_t *start) { |
| 768 |
uint8_t window = 0; |
| 769 |
uint8_t entry = 0; |
| 770 |
uint8_t bitmaps[32] = {}; |
| 771 |
unsigned n; |
| 772 |
size_t saved_size; |
| 773 |
int r; |
| 774 |
|
| 775 |
assert(p); |
| 776 |
|
| 777 |
saved_size = p->size; |
| 778 |
|
| 779 |
BITMAP_FOREACH(n, types) { |
| 780 |
assert(n <= 0xffff); |
| 781 |
|
| 782 |
if ((n >> 8) != window && bitmaps[entry / 8] != 0) { |
| 783 |
r = dns_packet_append_type_window(p, window, entry / 8 + 1, bitmaps, NULL); |
| 784 |
if (r < 0) |
| 785 |
goto fail; |
| 786 |
|
| 787 |
zero(bitmaps); |
| 788 |
} |
| 789 |
|
| 790 |
window = n >> 8; |
| 791 |
entry = n & 255; |
| 792 |
|
| 793 |
bitmaps[entry / 8] |= 1 << (7 - (entry % 8)); |
| 794 |
} |
| 795 |
|
| 796 |
if (bitmaps[entry / 8] != 0) { |
| 797 |
r = dns_packet_append_type_window(p, window, entry / 8 + 1, bitmaps, NULL); |
| 798 |
if (r < 0) |
| 799 |
goto fail; |
| 800 |
} |
| 801 |
|
| 802 |
if (start) |
| 803 |
*start = saved_size; |
| 804 |
|
| 805 |
return 0; |
| 806 |
fail: |
| 807 |
dns_packet_truncate(p, saved_size); |
| 808 |
return r; |
| 809 |
} |
| 810 |
|
| 811 |
/* Append the OPT pseudo-RR described in RFC6891 */ |
| 812 |
int dns_packet_append_opt( |
| 813 |
DnsPacket *p, |
| 814 |
uint16_t max_udp_size, |
| 815 |
bool edns0_do, |
| 816 |
bool include_rfc6975, |
| 817 |
const char *nsid, |
| 818 |
int rcode, |
| 819 |
size_t *ret_start) { |
| 820 |
|
| 821 |
size_t saved_size; |
| 822 |
int r; |
| 823 |
|
| 824 |
assert(p); |
| 825 |
/* we must never advertise supported packet size smaller than the legacy max */ |
| 826 |
assert(max_udp_size >= DNS_PACKET_UNICAST_SIZE_MAX); |
| 827 |
assert(rcode >= 0); |
| 828 |
assert(rcode <= _DNS_RCODE_MAX); |
| 829 |
|
| 830 |
if (p->opt_start != SIZE_MAX) |
| 831 |
return -EBUSY; |
| 832 |
|
| 833 |
assert(p->opt_size == SIZE_MAX); |
| 834 |
|
| 835 |
saved_size = p->size; |
| 836 |
|
| 837 |
/* empty name */ |
| 838 |
r = dns_packet_append_uint8(p, 0, NULL); |
| 839 |
if (r < 0) |
| 840 |
return r; |
| 841 |
|
| 842 |
/* type */ |
| 843 |
r = dns_packet_append_uint16(p, DNS_TYPE_OPT, NULL); |
| 844 |
if (r < 0) |
| 845 |
goto fail; |
| 846 |
|
| 847 |
/* class: maximum udp packet that can be received */ |
| 848 |
r = dns_packet_append_uint16(p, max_udp_size, NULL); |
| 849 |
if (r < 0) |
| 850 |
goto fail; |
| 851 |
|
| 852 |
/* extended RCODE and VERSION */ |
| 853 |
r = dns_packet_append_uint16(p, ((uint16_t) rcode & 0x0FF0) << 4, NULL); |
| 854 |
if (r < 0) |
| 855 |
goto fail; |
| 856 |
|
| 857 |
/* flags: DNSSEC OK (DO), see RFC3225 */ |
| 858 |
r = dns_packet_append_uint16(p, edns0_do ? EDNS0_OPT_DO : 0, NULL); |
| 859 |
if (r < 0) |
| 860 |
goto fail; |
| 861 |
|
| 862 |
if (edns0_do && include_rfc6975) { |
| 863 |
/* If DO is on and this is requested, also append RFC6975 Algorithm data. This is supposed to |
| 864 |
* be done on queries, not on replies, hencer callers should turn this off when finishing off |
| 865 |
* replies. */ |
| 866 |
|
| 867 |
static const uint8_t rfc6975[] = { |
| 868 |
|
| 869 |
0, DNS_EDNS_OPT_DAU, /* OPTION_CODE */ |
| 870 |
#if HAVE_OPENSSL |
| 871 |
0, 7, /* LIST_LENGTH */ |
| 872 |
#else |
| 873 |
0, 6, /* LIST_LENGTH */ |
| 874 |
#endif |
| 875 |
DNSSEC_ALGORITHM_RSASHA1, |
| 876 |
DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1, |
| 877 |
DNSSEC_ALGORITHM_RSASHA256, |
| 878 |
DNSSEC_ALGORITHM_RSASHA512, |
| 879 |
DNSSEC_ALGORITHM_ECDSAP256SHA256, |
| 880 |
DNSSEC_ALGORITHM_ECDSAP384SHA384, |
| 881 |
#if HAVE_OPENSSL |
| 882 |
DNSSEC_ALGORITHM_ED25519, |
| 883 |
#endif |
| 884 |
|
| 885 |
0, DNS_EDNS_OPT_DHU, /* OPTION_CODE */ |
| 886 |
0, 3, /* LIST_LENGTH */ |
| 887 |
DNSSEC_DIGEST_SHA1, |
| 888 |
DNSSEC_DIGEST_SHA256, |
| 889 |
DNSSEC_DIGEST_SHA384, |
| 890 |
|
| 891 |
0, DNS_EDNS_OPT_N3U, /* OPTION_CODE */ |
| 892 |
0, 1, /* LIST_LENGTH */ |
| 893 |
NSEC3_ALGORITHM_SHA1, |
| 894 |
}; |
| 895 |
|
| 896 |
r = dns_packet_append_uint16(p, sizeof(rfc6975), NULL); /* RDLENGTH */ |
| 897 |
if (r < 0) |
| 898 |
goto fail; |
| 899 |
|
| 900 |
r = dns_packet_append_blob(p, rfc6975, sizeof(rfc6975), NULL); /* the payload, as defined above */ |
| 901 |
|
| 902 |
} else if (nsid) { |
| 903 |
|
| 904 |
if (strlen(nsid) > UINT16_MAX - 4) { |
| 905 |
r = -E2BIG; |
| 906 |
goto fail; |
| 907 |
} |
| 908 |
|
| 909 |
r = dns_packet_append_uint16(p, 4 + strlen(nsid), NULL); /* RDLENGTH */ |
| 910 |
if (r < 0) |
| 911 |
goto fail; |
| 912 |
|
| 913 |
r = dns_packet_append_uint16(p, 3, NULL); /* OPTION-CODE: NSID */ |
| 914 |
if (r < 0) |
| 915 |
goto fail; |
| 916 |
|
| 917 |
r = dns_packet_append_uint16(p, strlen(nsid), NULL); /* OPTION-LENGTH */ |
| 918 |
if (r < 0) |
| 919 |
goto fail; |
| 920 |
|
| 921 |
r = dns_packet_append_blob(p, nsid, strlen(nsid), NULL); |
| 922 |
} else |
| 923 |
r = dns_packet_append_uint16(p, 0, NULL); |
| 924 |
if (r < 0) |
| 925 |
goto fail; |
| 926 |
|
| 927 |
DNS_PACKET_HEADER(p)->arcount = htobe16(DNS_PACKET_ARCOUNT(p) + 1); |
| 928 |
|
| 929 |
p->opt_start = saved_size; |
| 930 |
p->opt_size = p->size - saved_size; |
| 931 |
|
| 932 |
if (ret_start) |
| 933 |
*ret_start = saved_size; |
| 934 |
|
| 935 |
return 0; |
| 936 |
|
| 937 |
fail: |
| 938 |
dns_packet_truncate(p, saved_size); |
| 939 |
return r; |
| 940 |
} |
| 941 |
|
| 942 |
int dns_packet_truncate_opt(DnsPacket *p) { |
| 943 |
assert(p); |
| 944 |
|
| 945 |
if (p->opt_start == SIZE_MAX) { |
| 946 |
assert(p->opt_size == SIZE_MAX); |
| 947 |
return 0; |
| 948 |
} |
| 949 |
|
| 950 |
assert(p->opt_size != SIZE_MAX); |
| 951 |
assert(DNS_PACKET_ARCOUNT(p) > 0); |
| 952 |
|
| 953 |
if (p->opt_start + p->opt_size != p->size) |
| 954 |
return -EBUSY; |
| 955 |
|
| 956 |
dns_packet_truncate(p, p->opt_start); |
| 957 |
DNS_PACKET_HEADER(p)->arcount = htobe16(DNS_PACKET_ARCOUNT(p) - 1); |
| 958 |
p->opt_start = p->opt_size = SIZE_MAX; |
| 959 |
|
| 960 |
return 1; |
| 961 |
} |
| 962 |
|
| 963 |
int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, const DnsAnswerFlags flags, size_t *start, size_t *rdata_start) { |
| 964 |
|
| 965 |
size_t saved_size, rdlength_offset, end, rdlength, rds; |
| 966 |
uint32_t ttl; |
| 967 |
int r; |
| 968 |
|
| 969 |
assert(p); |
| 970 |
assert(rr); |
| 971 |
|
| 972 |
saved_size = p->size; |
| 973 |
|
| 974 |
r = dns_packet_append_key(p, rr->key, flags, NULL); |
| 975 |
if (r < 0) |
| 976 |
goto fail; |
| 977 |
|
| 978 |
ttl = flags & DNS_ANSWER_GOODBYE ? 0 : rr->ttl; |
| 979 |
r = dns_packet_append_uint32(p, ttl, NULL); |
| 980 |
if (r < 0) |
| 981 |
goto fail; |
| 982 |
|
| 983 |
/* Initially we write 0 here */ |
| 984 |
r = dns_packet_append_uint16(p, 0, &rdlength_offset); |
| 985 |
if (r < 0) |
| 986 |
goto fail; |
| 987 |
|
| 988 |
rds = p->size - saved_size; |
| 989 |
|
| 990 |
switch (rr->unparsable ? _DNS_TYPE_INVALID : rr->key->type) { |
| 991 |
|
| 992 |
case DNS_TYPE_SRV: |
| 993 |
r = dns_packet_append_uint16(p, rr->srv.priority, NULL); |
| 994 |
if (r < 0) |
| 995 |
goto fail; |
| 996 |
|
| 997 |
r = dns_packet_append_uint16(p, rr->srv.weight, NULL); |
| 998 |
if (r < 0) |
| 999 |
goto fail; |
| 1000 |
|
| 1001 |
r = dns_packet_append_uint16(p, rr->srv.port, NULL); |
| 1002 |
if (r < 0) |
| 1003 |
goto fail; |
| 1004 |
|
| 1005 |
/* RFC 2782 states "Unless and until permitted by future standards action, name compression |
| 1006 |
* is not to be used for this field." Hence we turn off compression here. */ |
| 1007 |
r = dns_packet_append_name(p, rr->srv.name, /* allow_compression= */ false, /* canonical_candidate= */ true, NULL); |
| 1008 |
break; |
| 1009 |
|
| 1010 |
case DNS_TYPE_PTR: |
| 1011 |
case DNS_TYPE_NS: |
| 1012 |
case DNS_TYPE_CNAME: |
| 1013 |
case DNS_TYPE_DNAME: |
| 1014 |
r = dns_packet_append_name(p, rr->ptr.name, true, true, NULL); |
| 1015 |
break; |
| 1016 |
|
| 1017 |
case DNS_TYPE_HINFO: |
| 1018 |
r = dns_packet_append_string(p, rr->hinfo.cpu, NULL); |
| 1019 |
if (r < 0) |
| 1020 |
goto fail; |
| 1021 |
|
| 1022 |
r = dns_packet_append_string(p, rr->hinfo.os, NULL); |
| 1023 |
break; |
| 1024 |
|
| 1025 |
case DNS_TYPE_SPF: /* exactly the same as TXT */ |
| 1026 |
case DNS_TYPE_TXT: |
| 1027 |
|
| 1028 |
if (!rr->txt.items) { |
| 1029 |
/* RFC 6763, section 6.1 suggests to generate |
| 1030 |
* single empty string for an empty array. */ |
| 1031 |
|
| 1032 |
r = dns_packet_append_raw_string(p, NULL, 0, NULL); |
| 1033 |
if (r < 0) |
| 1034 |
goto fail; |
| 1035 |
} else |
| 1036 |
LIST_FOREACH(items, i, rr->txt.items) { |
| 1037 |
r = dns_packet_append_raw_string(p, i->data, i->length, NULL); |
| 1038 |
if (r < 0) |
| 1039 |
goto fail; |
| 1040 |
} |
| 1041 |
|
| 1042 |
r = 0; |
| 1043 |
break; |
| 1044 |
|
| 1045 |
case DNS_TYPE_A: |
| 1046 |
r = dns_packet_append_blob(p, &rr->a.in_addr, sizeof(struct in_addr), NULL); |
| 1047 |
break; |
| 1048 |
|
| 1049 |
case DNS_TYPE_AAAA: |
| 1050 |
r = dns_packet_append_blob(p, &rr->aaaa.in6_addr, sizeof(struct in6_addr), NULL); |
| 1051 |
break; |
| 1052 |
|
| 1053 |
case DNS_TYPE_SOA: |
| 1054 |
r = dns_packet_append_name(p, rr->soa.mname, true, true, NULL); |
| 1055 |
if (r < 0) |
| 1056 |
goto fail; |
| 1057 |
|
| 1058 |
r = dns_packet_append_name(p, rr->soa.rname, true, true, NULL); |
| 1059 |
if (r < 0) |
| 1060 |
goto fail; |
| 1061 |
|
| 1062 |
r = dns_packet_append_uint32(p, rr->soa.serial, NULL); |
| 1063 |
if (r < 0) |
| 1064 |
goto fail; |
| 1065 |
|
| 1066 |
r = dns_packet_append_uint32(p, rr->soa.refresh, NULL); |
| 1067 |
if (r < 0) |
| 1068 |
goto fail; |
| 1069 |
|
| 1070 |
r = dns_packet_append_uint32(p, rr->soa.retry, NULL); |
| 1071 |
if (r < 0) |
| 1072 |
goto fail; |
| 1073 |
|
| 1074 |
r = dns_packet_append_uint32(p, rr->soa.expire, NULL); |
| 1075 |
if (r < 0) |
| 1076 |
goto fail; |
| 1077 |
|
| 1078 |
r = dns_packet_append_uint32(p, rr->soa.minimum, NULL); |
| 1079 |
break; |
| 1080 |
|
| 1081 |
case DNS_TYPE_MX: |
| 1082 |
r = dns_packet_append_uint16(p, rr->mx.priority, NULL); |
| 1083 |
if (r < 0) |
| 1084 |
goto fail; |
| 1085 |
|
| 1086 |
r = dns_packet_append_name(p, rr->mx.exchange, true, true, NULL); |
| 1087 |
break; |
| 1088 |
|
| 1089 |
case DNS_TYPE_LOC: |
| 1090 |
r = dns_packet_append_uint8(p, rr->loc.version, NULL); |
| 1091 |
if (r < 0) |
| 1092 |
goto fail; |
| 1093 |
|
| 1094 |
r = dns_packet_append_uint8(p, rr->loc.size, NULL); |
| 1095 |
if (r < 0) |
| 1096 |
goto fail; |
| 1097 |
|
| 1098 |
r = dns_packet_append_uint8(p, rr->loc.horiz_pre, NULL); |
| 1099 |
if (r < 0) |
| 1100 |
goto fail; |
| 1101 |
|
| 1102 |
r = dns_packet_append_uint8(p, rr->loc.vert_pre, NULL); |
| 1103 |
if (r < 0) |
| 1104 |
goto fail; |
| 1105 |
|
| 1106 |
r = dns_packet_append_uint32(p, rr->loc.latitude, NULL); |
| 1107 |
if (r < 0) |
| 1108 |
goto fail; |
| 1109 |
|
| 1110 |
r = dns_packet_append_uint32(p, rr->loc.longitude, NULL); |
| 1111 |
if (r < 0) |
| 1112 |
goto fail; |
| 1113 |
|
| 1114 |
r = dns_packet_append_uint32(p, rr->loc.altitude, NULL); |
| 1115 |
break; |
| 1116 |
|
| 1117 |
case DNS_TYPE_DS: |
| 1118 |
r = dns_packet_append_uint16(p, rr->ds.key_tag, NULL); |
| 1119 |
if (r < 0) |
| 1120 |
goto fail; |
| 1121 |
|
| 1122 |
r = dns_packet_append_uint8(p, rr->ds.algorithm, NULL); |
| 1123 |
if (r < 0) |
| 1124 |
goto fail; |
| 1125 |
|
| 1126 |
r = dns_packet_append_uint8(p, rr->ds.digest_type, NULL); |
| 1127 |
if (r < 0) |
| 1128 |
goto fail; |
| 1129 |
|
| 1130 |
r = dns_packet_append_blob(p, rr->ds.digest, rr->ds.digest_size, NULL); |
| 1131 |
break; |
| 1132 |
|
| 1133 |
case DNS_TYPE_SSHFP: |
| 1134 |
r = dns_packet_append_uint8(p, rr->sshfp.algorithm, NULL); |
| 1135 |
if (r < 0) |
| 1136 |
goto fail; |
| 1137 |
|
| 1138 |
r = dns_packet_append_uint8(p, rr->sshfp.fptype, NULL); |
| 1139 |
if (r < 0) |
| 1140 |
goto fail; |
| 1141 |
|
| 1142 |
r = dns_packet_append_blob(p, rr->sshfp.fingerprint, rr->sshfp.fingerprint_size, NULL); |
| 1143 |
break; |
| 1144 |
|
| 1145 |
case DNS_TYPE_DNSKEY: |
| 1146 |
r = dns_packet_append_uint16(p, rr->dnskey.flags, NULL); |
| 1147 |
if (r < 0) |
| 1148 |
goto fail; |
| 1149 |
|
| 1150 |
r = dns_packet_append_uint8(p, rr->dnskey.protocol, NULL); |
| 1151 |
if (r < 0) |
| 1152 |
goto fail; |
| 1153 |
|
| 1154 |
r = dns_packet_append_uint8(p, rr->dnskey.algorithm, NULL); |
| 1155 |
if (r < 0) |
| 1156 |
goto fail; |
| 1157 |
|
| 1158 |
r = dns_packet_append_blob(p, rr->dnskey.key, rr->dnskey.key_size, NULL); |
| 1159 |
break; |
| 1160 |
|
| 1161 |
case DNS_TYPE_RRSIG: |
| 1162 |
r = dns_packet_append_uint16(p, rr->rrsig.type_covered, NULL); |
| 1163 |
if (r < 0) |
| 1164 |
goto fail; |
| 1165 |
|
| 1166 |
r = dns_packet_append_uint8(p, rr->rrsig.algorithm, NULL); |
| 1167 |
if (r < 0) |
| 1168 |
goto fail; |
| 1169 |
|
| 1170 |
r = dns_packet_append_uint8(p, rr->rrsig.labels, NULL); |
| 1171 |
if (r < 0) |
| 1172 |
goto fail; |
| 1173 |
|
| 1174 |
r = dns_packet_append_uint32(p, rr->rrsig.original_ttl, NULL); |
| 1175 |
if (r < 0) |
| 1176 |
goto fail; |
| 1177 |
|
| 1178 |
r = dns_packet_append_uint32(p, rr->rrsig.expiration, NULL); |
| 1179 |
if (r < 0) |
| 1180 |
goto fail; |
| 1181 |
|
| 1182 |
r = dns_packet_append_uint32(p, rr->rrsig.inception, NULL); |
| 1183 |
if (r < 0) |
| 1184 |
goto fail; |
| 1185 |
|
| 1186 |
r = dns_packet_append_uint16(p, rr->rrsig.key_tag, NULL); |
| 1187 |
if (r < 0) |
| 1188 |
goto fail; |
| 1189 |
|
| 1190 |
r = dns_packet_append_name(p, rr->rrsig.signer, false, true, NULL); |
| 1191 |
if (r < 0) |
| 1192 |
goto fail; |
| 1193 |
|
| 1194 |
r = dns_packet_append_blob(p, rr->rrsig.signature, rr->rrsig.signature_size, NULL); |
| 1195 |
break; |
| 1196 |
|
| 1197 |
case DNS_TYPE_NSEC: |
| 1198 |
r = dns_packet_append_name(p, rr->nsec.next_domain_name, false, false, NULL); |
| 1199 |
if (r < 0) |
| 1200 |
goto fail; |
| 1201 |
|
| 1202 |
r = dns_packet_append_types(p, rr->nsec.types, NULL); |
| 1203 |
if (r < 0) |
| 1204 |
goto fail; |
| 1205 |
|
| 1206 |
break; |
| 1207 |
|
| 1208 |
case DNS_TYPE_NSEC3: |
| 1209 |
r = dns_packet_append_uint8(p, rr->nsec3.algorithm, NULL); |
| 1210 |
if (r < 0) |
| 1211 |
goto fail; |
| 1212 |
|
| 1213 |
r = dns_packet_append_uint8(p, rr->nsec3.flags, NULL); |
| 1214 |
if (r < 0) |
| 1215 |
goto fail; |
| 1216 |
|
| 1217 |
r = dns_packet_append_uint16(p, rr->nsec3.iterations, NULL); |
| 1218 |
if (r < 0) |
| 1219 |
goto fail; |
| 1220 |
|
| 1221 |
r = dns_packet_append_uint8(p, rr->nsec3.salt_size, NULL); |
| 1222 |
if (r < 0) |
| 1223 |
goto fail; |
| 1224 |
|
| 1225 |
r = dns_packet_append_blob(p, rr->nsec3.salt, rr->nsec3.salt_size, NULL); |
| 1226 |
if (r < 0) |
| 1227 |
goto fail; |
| 1228 |
|
| 1229 |
r = dns_packet_append_uint8(p, rr->nsec3.next_hashed_name_size, NULL); |
| 1230 |
if (r < 0) |
| 1231 |
goto fail; |
| 1232 |
|
| 1233 |
r = dns_packet_append_blob(p, rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, NULL); |
| 1234 |
if (r < 0) |
| 1235 |
goto fail; |
| 1236 |
|
| 1237 |
r = dns_packet_append_types(p, rr->nsec3.types, NULL); |
| 1238 |
if (r < 0) |
| 1239 |
goto fail; |
| 1240 |
|
| 1241 |
break; |
| 1242 |
|
| 1243 |
case DNS_TYPE_TLSA: |
| 1244 |
r = dns_packet_append_uint8(p, rr->tlsa.cert_usage, NULL); |
| 1245 |
if (r < 0) |
| 1246 |
goto fail; |
| 1247 |
|
| 1248 |
r = dns_packet_append_uint8(p, rr->tlsa.selector, NULL); |
| 1249 |
if (r < 0) |
| 1250 |
goto fail; |
| 1251 |
|
| 1252 |
r = dns_packet_append_uint8(p, rr->tlsa.matching_type, NULL); |
| 1253 |
if (r < 0) |
| 1254 |
goto fail; |
| 1255 |
|
| 1256 |
r = dns_packet_append_blob(p, rr->tlsa.data, rr->tlsa.data_size, NULL); |
| 1257 |
break; |
| 1258 |
|
| 1259 |
case DNS_TYPE_SVCB: |
| 1260 |
case DNS_TYPE_HTTPS: |
| 1261 |
r = dns_packet_append_uint16(p, rr->svcb.priority, NULL); |
| 1262 |
if (r < 0) |
| 1263 |
goto fail; |
| 1264 |
|
| 1265 |
r = dns_packet_append_name(p, rr->svcb.target_name, false, false, NULL); |
| 1266 |
if (r < 0) |
| 1267 |
goto fail; |
| 1268 |
|
| 1269 |
LIST_FOREACH(params, i, rr->svcb.params) { |
| 1270 |
r = dns_packet_append_uint16(p, i->key, NULL); |
| 1271 |
if (r < 0) |
| 1272 |
goto fail; |
| 1273 |
|
| 1274 |
r = dns_packet_append_uint16(p, i->length, NULL); |
| 1275 |
if (r < 0) |
| 1276 |
goto fail; |
| 1277 |
|
| 1278 |
r = dns_packet_append_blob(p, i->value, i->length, NULL); |
| 1279 |
if (r < 0) |
| 1280 |
goto fail; |
| 1281 |
} |
| 1282 |
break; |
| 1283 |
|
| 1284 |
case DNS_TYPE_CAA: |
| 1285 |
r = dns_packet_append_uint8(p, rr->caa.flags, NULL); |
| 1286 |
if (r < 0) |
| 1287 |
goto fail; |
| 1288 |
|
| 1289 |
r = dns_packet_append_string(p, rr->caa.tag, NULL); |
| 1290 |
if (r < 0) |
| 1291 |
goto fail; |
| 1292 |
|
| 1293 |
r = dns_packet_append_blob(p, rr->caa.value, rr->caa.value_size, NULL); |
| 1294 |
break; |
| 1295 |
|
| 1296 |
case DNS_TYPE_NAPTR: |
| 1297 |
r = dns_packet_append_uint16(p, rr->naptr.order, NULL); |
| 1298 |
if (r < 0) |
| 1299 |
goto fail; |
| 1300 |
|
| 1301 |
r = dns_packet_append_uint16(p, rr->naptr.preference, NULL); |
| 1302 |
if (r < 0) |
| 1303 |
goto fail; |
| 1304 |
|
| 1305 |
r = dns_packet_append_string(p, rr->naptr.flags, NULL); |
| 1306 |
if (r < 0) |
| 1307 |
goto fail; |
| 1308 |
|
| 1309 |
r = dns_packet_append_string(p, rr->naptr.services, NULL); |
| 1310 |
if (r < 0) |
| 1311 |
goto fail; |
| 1312 |
|
| 1313 |
r = dns_packet_append_string(p, rr->naptr.regexp, NULL); |
| 1314 |
if (r < 0) |
| 1315 |
goto fail; |
| 1316 |
|
| 1317 |
r = dns_packet_append_name(p, rr->naptr.replacement, /* allow_compression= */ false, /* canonical_candidate= */ true, NULL); |
| 1318 |
break; |
| 1319 |
|
| 1320 |
case DNS_TYPE_OPT: |
| 1321 |
case DNS_TYPE_OPENPGPKEY: |
| 1322 |
case _DNS_TYPE_INVALID: /* unparsable */ |
| 1323 |
default: |
| 1324 |
r = dns_packet_append_blob(p, rr->generic.data, rr->generic.data_size, NULL); |
| 1325 |
} |
| 1326 |
if (r < 0) |
| 1327 |
goto fail; |
| 1328 |
|
| 1329 |
/* Let's calculate the actual data size and update the field */ |
| 1330 |
rdlength = p->size - rdlength_offset - sizeof(uint16_t); |
| 1331 |
if (rdlength > 0xFFFF) { |
| 1332 |
r = -ENOSPC; |
| 1333 |
goto fail; |
| 1334 |
} |
| 1335 |
|
| 1336 |
end = p->size; |
| 1337 |
p->size = rdlength_offset; |
| 1338 |
r = dns_packet_append_uint16(p, rdlength, NULL); |
| 1339 |
if (r < 0) |
| 1340 |
goto fail; |
| 1341 |
p->size = end; |
| 1342 |
|
| 1343 |
if (start) |
| 1344 |
*start = saved_size; |
| 1345 |
|
| 1346 |
if (rdata_start) |
| 1347 |
*rdata_start = rds; |
| 1348 |
|
| 1349 |
return 0; |
| 1350 |
|
| 1351 |
fail: |
| 1352 |
dns_packet_truncate(p, saved_size); |
| 1353 |
return r; |
| 1354 |
} |
| 1355 |
|
| 1356 |
int dns_packet_append_question(DnsPacket *p, DnsQuestion *q) { |
| 1357 |
DnsResourceKey *key; |
| 1358 |
int r; |
| 1359 |
|
| 1360 |
assert(p); |
| 1361 |
|
| 1362 |
DNS_QUESTION_FOREACH(key, q) { |
| 1363 |
r = dns_packet_append_key(p, key, 0, NULL); |
| 1364 |
if (r < 0) |
| 1365 |
return r; |
| 1366 |
} |
| 1367 |
|
| 1368 |
return 0; |
| 1369 |
} |
| 1370 |
|
| 1371 |
int dns_packet_append_answer(DnsPacket *p, DnsAnswer *a, unsigned *completed) { |
| 1372 |
DnsResourceRecord *rr; |
| 1373 |
DnsAnswerFlags flags; |
| 1374 |
int r; |
| 1375 |
|
| 1376 |
assert(p); |
| 1377 |
|
| 1378 |
DNS_ANSWER_FOREACH_FLAGS(rr, flags, a) { |
| 1379 |
r = dns_packet_append_rr(p, rr, flags, NULL, NULL); |
| 1380 |
if (r < 0) |
| 1381 |
return r; |
| 1382 |
|
| 1383 |
if (completed) |
| 1384 |
(*completed)++; |
| 1385 |
} |
| 1386 |
|
| 1387 |
return 0; |
| 1388 |
} |
| 1389 |
|
| 1390 |
int dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start) { |
| 1391 |
assert(p); |
| 1392 |
assert(p->rindex <= p->size); |
| 1393 |
|
| 1394 |
if (sz > p->size - p->rindex) |
| 1395 |
return -EMSGSIZE; |
| 1396 |
|
| 1397 |
if (ret) |
| 1398 |
*ret = DNS_PACKET_DATA(p) + p->rindex; |
| 1399 |
|
| 1400 |
if (start) |
| 1401 |
*start = p->rindex; |
| 1402 |
|
| 1403 |
p->rindex += sz; |
| 1404 |
return 0; |
| 1405 |
} |
| 1406 |
|
| 1407 |
void dns_packet_rewind(DnsPacket *p, size_t idx) { |
| 1408 |
assert(p); |
| 1409 |
assert(idx <= p->size); |
| 1410 |
assert(idx >= DNS_PACKET_HEADER_SIZE); |
| 1411 |
|
| 1412 |
p->rindex = idx; |
| 1413 |
} |
| 1414 |
|
| 1415 |
int dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start) { |
| 1416 |
const void *q; |
| 1417 |
int r; |
| 1418 |
|
| 1419 |
assert(p); |
| 1420 |
assert(d); |
| 1421 |
|
| 1422 |
r = dns_packet_read(p, sz, &q, start); |
| 1423 |
if (r < 0) |
| 1424 |
return r; |
| 1425 |
|
| 1426 |
memcpy(d, q, sz); |
| 1427 |
return 0; |
| 1428 |
} |
| 1429 |
|
| 1430 |
static int dns_packet_read_memdup( |
| 1431 |
DnsPacket *p, size_t size, |
| 1432 |
void **ret, size_t *ret_size, |
| 1433 |
size_t *ret_start) { |
| 1434 |
|
| 1435 |
const void *src; |
| 1436 |
size_t start; |
| 1437 |
int r; |
| 1438 |
|
| 1439 |
assert(p); |
| 1440 |
assert(ret); |
| 1441 |
|
| 1442 |
r = dns_packet_read(p, size, &src, &start); |
| 1443 |
if (r < 0) |
| 1444 |
return r; |
| 1445 |
|
| 1446 |
if (size <= 0) |
| 1447 |
*ret = NULL; |
| 1448 |
else { |
| 1449 |
void *copy; |
| 1450 |
|
| 1451 |
copy = memdup(src, size); |
| 1452 |
if (!copy) |
| 1453 |
return -ENOMEM; |
| 1454 |
|
| 1455 |
*ret = copy; |
| 1456 |
} |
| 1457 |
|
| 1458 |
if (ret_size) |
| 1459 |
*ret_size = size; |
| 1460 |
if (ret_start) |
| 1461 |
*ret_start = start; |
| 1462 |
|
| 1463 |
return 0; |
| 1464 |
} |
| 1465 |
|
| 1466 |
int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start) { |
| 1467 |
const void *d; |
| 1468 |
int r; |
| 1469 |
|
| 1470 |
assert(p); |
| 1471 |
|
| 1472 |
r = dns_packet_read(p, sizeof(uint8_t), &d, start); |
| 1473 |
if (r < 0) |
| 1474 |
return r; |
| 1475 |
|
| 1476 |
if (ret) |
| 1477 |
*ret = ((uint8_t*) d)[0]; |
| 1478 |
|
| 1479 |
return 0; |
| 1480 |
} |
| 1481 |
|
| 1482 |
int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start) { |
| 1483 |
const void *d; |
| 1484 |
int r; |
| 1485 |
|
| 1486 |
assert(p); |
| 1487 |
|
| 1488 |
r = dns_packet_read(p, sizeof(uint16_t), &d, start); |
| 1489 |
if (r < 0) |
| 1490 |
return r; |
| 1491 |
|
| 1492 |
if (ret) |
| 1493 |
*ret = unaligned_read_be16(d); |
| 1494 |
|
| 1495 |
return 0; |
| 1496 |
} |
| 1497 |
|
| 1498 |
int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start) { |
| 1499 |
const void *d; |
| 1500 |
int r; |
| 1501 |
|
| 1502 |
assert(p); |
| 1503 |
|
| 1504 |
r = dns_packet_read(p, sizeof(uint32_t), &d, start); |
| 1505 |
if (r < 0) |
| 1506 |
return r; |
| 1507 |
|
| 1508 |
if (ret) |
| 1509 |
*ret = unaligned_read_be32(d); |
| 1510 |
|
| 1511 |
return 0; |
| 1512 |
} |
| 1513 |
|
| 1514 |
int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start) { |
| 1515 |
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 1516 |
_cleanup_free_ char *t = NULL; |
| 1517 |
const void *d; |
| 1518 |
uint8_t c; |
| 1519 |
int r; |
| 1520 |
|
| 1521 |
assert(p); |
| 1522 |
assert(ret); |
| 1523 |
|
| 1524 |
r = dns_packet_read_uint8(p, &c, NULL); |
| 1525 |
if (r < 0) |
| 1526 |
return r; |
| 1527 |
|
| 1528 |
r = dns_packet_read(p, c, &d, NULL); |
| 1529 |
if (r < 0) |
| 1530 |
return r; |
| 1531 |
|
| 1532 |
r = make_cstring(d, c, MAKE_CSTRING_REFUSE_TRAILING_NUL, &t); |
| 1533 |
if (r < 0) |
| 1534 |
return r; |
| 1535 |
|
| 1536 |
if (!utf8_is_valid(t)) |
| 1537 |
return -EBADMSG; |
| 1538 |
|
| 1539 |
*ret = TAKE_PTR(t); |
| 1540 |
|
| 1541 |
if (start) |
| 1542 |
*start = rewinder.saved_rindex; |
| 1543 |
CANCEL_REWINDER(rewinder); |
| 1544 |
|
| 1545 |
return 0; |
| 1546 |
} |
| 1547 |
|
| 1548 |
int dns_packet_read_raw_string(DnsPacket *p, const void **ret, size_t *size, size_t *start) { |
| 1549 |
assert(p); |
| 1550 |
|
| 1551 |
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 1552 |
uint8_t c; |
| 1553 |
int r; |
| 1554 |
|
| 1555 |
r = dns_packet_read_uint8(p, &c, NULL); |
| 1556 |
if (r < 0) |
| 1557 |
return r; |
| 1558 |
|
| 1559 |
r = dns_packet_read(p, c, ret, NULL); |
| 1560 |
if (r < 0) |
| 1561 |
return r; |
| 1562 |
|
| 1563 |
if (size) |
| 1564 |
*size = c; |
| 1565 |
if (start) |
| 1566 |
*start = rewinder.saved_rindex; |
| 1567 |
CANCEL_REWINDER(rewinder); |
| 1568 |
|
| 1569 |
return 0; |
| 1570 |
} |
| 1571 |
|
| 1572 |
int dns_packet_read_name( |
| 1573 |
DnsPacket *p, |
| 1574 |
char **ret, |
| 1575 |
bool allow_compression, |
| 1576 |
size_t *ret_start) { |
| 1577 |
|
| 1578 |
assert(p); |
| 1579 |
|
| 1580 |
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 1581 |
size_t after_rindex = 0, jump_barrier = p->rindex; |
| 1582 |
_cleanup_free_ char *name = NULL; |
| 1583 |
bool first = true; |
| 1584 |
size_t n = 0, m = 0; |
| 1585 |
int r; |
| 1586 |
|
| 1587 |
if (p->refuse_compression) |
| 1588 |
allow_compression = false; |
| 1589 |
|
| 1590 |
for (;;) { |
| 1591 |
uint8_t c, d; |
| 1592 |
|
| 1593 |
r = dns_packet_read_uint8(p, &c, NULL); |
| 1594 |
if (r < 0) |
| 1595 |
return r; |
| 1596 |
|
| 1597 |
if (c == 0) |
| 1598 |
/* End of name */ |
| 1599 |
break; |
| 1600 |
else if (c <= 63) { |
| 1601 |
const char *label; |
| 1602 |
|
| 1603 |
/* Literal label */ |
| 1604 |
r = dns_packet_read(p, c, (const void**) &label, NULL); |
| 1605 |
if (r < 0) |
| 1606 |
return r; |
| 1607 |
|
| 1608 |
if (!GREEDY_REALLOC(name, n + !first + DNS_LABEL_ESCAPED_MAX)) |
| 1609 |
return -ENOMEM; |
| 1610 |
|
| 1611 |
if (first) |
| 1612 |
first = false; |
| 1613 |
else { |
| 1614 |
name[n++] = '.'; |
| 1615 |
m++; |
| 1616 |
} |
| 1617 |
|
| 1618 |
r = dns_label_escape(label, c, name + n, DNS_LABEL_ESCAPED_MAX); |
| 1619 |
if (r < 0) |
| 1620 |
return r; |
| 1621 |
|
| 1622 |
n += r; |
| 1623 |
m += c; |
| 1624 |
|
| 1625 |
if (m > DNS_HOSTNAME_MAX) |
| 1626 |
return -EBADMSG; |
| 1627 |
|
| 1628 |
continue; |
| 1629 |
} else if (allow_compression && FLAGS_SET(c, 0xc0)) { |
| 1630 |
uint16_t ptr; |
| 1631 |
|
| 1632 |
/* Pointer */ |
| 1633 |
r = dns_packet_read_uint8(p, &d, NULL); |
| 1634 |
if (r < 0) |
| 1635 |
return r; |
| 1636 |
|
| 1637 |
ptr = (uint16_t) (c & ~0xc0) << 8 | (uint16_t) d; |
| 1638 |
if (ptr < DNS_PACKET_HEADER_SIZE || ptr >= jump_barrier) |
| 1639 |
return -EBADMSG; |
| 1640 |
|
| 1641 |
if (after_rindex == 0) |
| 1642 |
after_rindex = p->rindex; |
| 1643 |
|
| 1644 |
/* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */ |
| 1645 |
jump_barrier = ptr; |
| 1646 |
p->rindex = ptr; |
| 1647 |
} else |
| 1648 |
return -EBADMSG; |
| 1649 |
} |
| 1650 |
|
| 1651 |
if (!GREEDY_REALLOC(name, n + 1)) |
| 1652 |
return -ENOMEM; |
| 1653 |
|
| 1654 |
name[n] = 0; |
| 1655 |
|
| 1656 |
if (after_rindex != 0) |
| 1657 |
p->rindex= after_rindex; |
| 1658 |
|
| 1659 |
if (ret) |
| 1660 |
*ret = TAKE_PTR(name); |
| 1661 |
if (ret_start) |
| 1662 |
*ret_start = rewinder.saved_rindex; |
| 1663 |
|
| 1664 |
CANCEL_REWINDER(rewinder); |
| 1665 |
|
| 1666 |
return 0; |
| 1667 |
} |
| 1668 |
|
| 1669 |
static int dns_packet_read_type_window(DnsPacket *p, Bitmap **types, size_t *start) { |
| 1670 |
assert(p); |
| 1671 |
assert(types); |
| 1672 |
|
| 1673 |
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 1674 |
uint8_t window, length; |
| 1675 |
const uint8_t *bitmap; |
| 1676 |
uint8_t bit = 0; |
| 1677 |
bool found = false; |
| 1678 |
int r; |
| 1679 |
|
| 1680 |
r = bitmap_ensure_allocated(types); |
| 1681 |
if (r < 0) |
| 1682 |
return r; |
| 1683 |
|
| 1684 |
r = dns_packet_read_uint8(p, &window, NULL); |
| 1685 |
if (r < 0) |
| 1686 |
return r; |
| 1687 |
|
| 1688 |
r = dns_packet_read_uint8(p, &length, NULL); |
| 1689 |
if (r < 0) |
| 1690 |
return r; |
| 1691 |
|
| 1692 |
if (length == 0 || length > 32) |
| 1693 |
return -EBADMSG; |
| 1694 |
|
| 1695 |
r = dns_packet_read(p, length, (const void **)&bitmap, NULL); |
| 1696 |
if (r < 0) |
| 1697 |
return r; |
| 1698 |
|
| 1699 |
for (uint8_t i = 0; i < length; i++) { |
| 1700 |
uint8_t bitmask = 1 << 7; |
| 1701 |
|
| 1702 |
if (!bitmap[i]) { |
| 1703 |
found = false; |
| 1704 |
bit += 8; |
| 1705 |
continue; |
| 1706 |
} |
| 1707 |
|
| 1708 |
found = true; |
| 1709 |
|
| 1710 |
for (; bitmask; bit++, bitmask >>= 1) |
| 1711 |
if (bitmap[i] & bitmask) { |
| 1712 |
uint16_t n; |
| 1713 |
|
| 1714 |
n = (uint16_t) window << 8 | (uint16_t) bit; |
| 1715 |
|
| 1716 |
/* Ignore pseudo-types. see RFC4034 section 4.1.2 */ |
| 1717 |
if (dns_type_is_pseudo(n)) |
| 1718 |
continue; |
| 1719 |
|
| 1720 |
r = bitmap_set(*types, n); |
| 1721 |
if (r < 0) |
| 1722 |
return r; |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (!found) |
| 1727 |
return -EBADMSG; |
| 1728 |
|
| 1729 |
if (start) |
| 1730 |
*start = rewinder.saved_rindex; |
| 1731 |
CANCEL_REWINDER(rewinder); |
| 1732 |
|
| 1733 |
return 0; |
| 1734 |
} |
| 1735 |
|
| 1736 |
static int dns_packet_read_type_windows(DnsPacket *p, Bitmap **types, size_t size, size_t *start) { |
| 1737 |
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 1738 |
int r; |
| 1739 |
|
| 1740 |
while (p->rindex - rewinder.saved_rindex < size) { |
| 1741 |
r = dns_packet_read_type_window(p, types, NULL); |
| 1742 |
if (r < 0) |
| 1743 |
return r; |
| 1744 |
|
| 1745 |
assert(p->rindex >= rewinder.saved_rindex); |
| 1746 |
|
| 1747 |
/* don't read past end of current RR */ |
| 1748 |
if (p->rindex - rewinder.saved_rindex > size) |
| 1749 |
return -EBADMSG; |
| 1750 |
} |
| 1751 |
|
| 1752 |
if (p->rindex - rewinder.saved_rindex != size) |
| 1753 |
return -EBADMSG; |
| 1754 |
|
| 1755 |
if (start) |
| 1756 |
*start = rewinder.saved_rindex; |
| 1757 |
CANCEL_REWINDER(rewinder); |
| 1758 |
|
| 1759 |
return 0; |
| 1760 |
} |
| 1761 |
|
| 1762 |
int dns_packet_read_key( |
| 1763 |
DnsPacket *p, |
| 1764 |
DnsResourceKey **ret, |
| 1765 |
bool *ret_cache_flush_or_qu, |
| 1766 |
size_t *ret_start) { |
| 1767 |
|
| 1768 |
assert(p); |
| 1769 |
|
| 1770 |
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 1771 |
_cleanup_free_ char *name = NULL; |
| 1772 |
bool cache_flush_or_qu = false; |
| 1773 |
uint16_t class, type; |
| 1774 |
int r; |
| 1775 |
|
| 1776 |
r = dns_packet_read_name(p, &name, true, NULL); |
| 1777 |
if (r < 0) |
| 1778 |
return r; |
| 1779 |
|
| 1780 |
r = dns_packet_read_uint16(p, &type, NULL); |
| 1781 |
if (r < 0) |
| 1782 |
return r; |
| 1783 |
|
| 1784 |
r = dns_packet_read_uint16(p, &class, NULL); |
| 1785 |
if (r < 0) |
| 1786 |
return r; |
| 1787 |
|
| 1788 |
if (p->protocol == DNS_PROTOCOL_MDNS) { |
| 1789 |
/* See RFC6762, sections 5.4 and 10.2 */ |
| 1790 |
|
| 1791 |
if (type != DNS_TYPE_OPT && (class & MDNS_RR_CACHE_FLUSH_OR_QU)) { |
| 1792 |
class &= ~MDNS_RR_CACHE_FLUSH_OR_QU; |
| 1793 |
cache_flush_or_qu = true; |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
if (ret) { |
| 1798 |
DnsResourceKey *key; |
| 1799 |
|
| 1800 |
key = dns_resource_key_new_consume(class, type, name); |
| 1801 |
if (!key) |
| 1802 |
return -ENOMEM; |
| 1803 |
|
| 1804 |
TAKE_PTR(name); |
| 1805 |
*ret = key; |
| 1806 |
} |
| 1807 |
|
| 1808 |
if (ret_cache_flush_or_qu) |
| 1809 |
*ret_cache_flush_or_qu = cache_flush_or_qu; |
| 1810 |
if (ret_start) |
| 1811 |
*ret_start = rewinder.saved_rindex; |
| 1812 |
|
| 1813 |
CANCEL_REWINDER(rewinder); |
| 1814 |
return 0; |
| 1815 |
} |
| 1816 |
|
| 1817 |
static bool loc_size_ok(uint8_t size) { |
| 1818 |
uint8_t m = size >> 4, e = size & 0xF; |
| 1819 |
|
| 1820 |
return m <= 9 && e <= 9 && (m > 0 || e == 0); |
| 1821 |
} |
| 1822 |
|
| 1823 |
static bool dns_svc_param_is_valid(DnsSvcParam *i) { |
| 1824 |
if (!i) |
| 1825 |
return false; |
| 1826 |
|
| 1827 |
switch (i->key) { |
| 1828 |
/* RFC 9460, section 7.1.1: alpn-ids must exactly fill SvcParamValue */ |
| 1829 |
case DNS_SVC_PARAM_KEY_ALPN: { |
| 1830 |
size_t sz = 0; |
| 1831 |
if (i->length <= 0) |
| 1832 |
return false; |
| 1833 |
while (sz < i->length) |
| 1834 |
sz += 1 + i->value[sz]; /* N.B. will not overflow */ |
| 1835 |
return sz == i->length; |
| 1836 |
} |
| 1837 |
|
| 1838 |
/* RFC 9460, section 7.1.1: value must be empty */ |
| 1839 |
case DNS_SVC_PARAM_KEY_NO_DEFAULT_ALPN: |
| 1840 |
return i->length == 0; |
| 1841 |
|
| 1842 |
/* RFC 9460, section 7.2 */ |
| 1843 |
case DNS_SVC_PARAM_KEY_PORT: |
| 1844 |
return i->length == 2; |
| 1845 |
|
| 1846 |
/* RFC 9460, section 7.3: addrs must exactly fill SvcParamValue */ |
| 1847 |
case DNS_SVC_PARAM_KEY_IPV4HINT: |
| 1848 |
return i->length > 0 && i->length % (sizeof (struct in_addr)) == 0; |
| 1849 |
case DNS_SVC_PARAM_KEY_IPV6HINT: |
| 1850 |
return i->length > 0 && i->length % (sizeof (struct in6_addr)) == 0; |
| 1851 |
|
| 1852 |
/* Otherwise, permit any value */ |
| 1853 |
default: |
| 1854 |
return true; |
| 1855 |
} |
| 1856 |
} |
| 1857 |
|
| 1858 |
int dns_packet_read_rr( |
| 1859 |
DnsPacket *p, |
| 1860 |
DnsResourceRecord **ret, |
| 1861 |
bool *ret_cache_flush, |
| 1862 |
size_t *ret_start) { |
| 1863 |
|
| 1864 |
assert(p); |
| 1865 |
|
| 1866 |
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 1867 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 1868 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 1869 |
size_t offset; |
| 1870 |
uint16_t rdlength; |
| 1871 |
bool cache_flush; |
| 1872 |
int r; |
| 1873 |
|
| 1874 |
r = dns_packet_read_key(p, &key, &cache_flush, NULL); |
| 1875 |
if (r < 0) |
| 1876 |
return r; |
| 1877 |
|
| 1878 |
if (!dns_class_is_valid_rr(key->class) || !dns_type_is_valid_rr(key->type)) |
| 1879 |
return -EBADMSG; |
| 1880 |
|
| 1881 |
rr = dns_resource_record_new(key); |
| 1882 |
if (!rr) |
| 1883 |
return -ENOMEM; |
| 1884 |
|
| 1885 |
r = dns_packet_read_uint32(p, &rr->ttl, NULL); |
| 1886 |
if (r < 0) |
| 1887 |
return r; |
| 1888 |
|
| 1889 |
/* RFC 2181, Section 8, suggests to treat a TTL with the MSB set as a zero TTL. We avoid doing this |
| 1890 |
* for OPT records so that all 8 bits of the extended RCODE may be used. */ |
| 1891 |
if (key->type != DNS_TYPE_OPT && rr->ttl & UINT32_C(0x80000000)) |
| 1892 |
rr->ttl = 0; |
| 1893 |
|
| 1894 |
r = dns_packet_read_uint16(p, &rdlength, NULL); |
| 1895 |
if (r < 0) |
| 1896 |
return r; |
| 1897 |
|
| 1898 |
if (rdlength > p->size - p->rindex) |
| 1899 |
return -EBADMSG; |
| 1900 |
|
| 1901 |
offset = p->rindex; |
| 1902 |
|
| 1903 |
switch (rr->key->type) { |
| 1904 |
|
| 1905 |
case DNS_TYPE_SRV: |
| 1906 |
r = dns_packet_read_uint16(p, &rr->srv.priority, NULL); |
| 1907 |
if (r < 0) |
| 1908 |
return r; |
| 1909 |
r = dns_packet_read_uint16(p, &rr->srv.weight, NULL); |
| 1910 |
if (r < 0) |
| 1911 |
return r; |
| 1912 |
r = dns_packet_read_uint16(p, &rr->srv.port, NULL); |
| 1913 |
if (r < 0) |
| 1914 |
return r; |
| 1915 |
|
| 1916 |
/* RFC 2782 states "Unless and until permitted by future standards action, name compression |
| 1917 |
* is not to be used for this field." Nonetheless, we support it here, in the interest of |
| 1918 |
* increasing compatibility with implementations that do not implement this correctly. After |
| 1919 |
* all we didn't do this right once upon a time ourselves (see |
| 1920 |
* https://github.com/systemd/systemd/issues/9793). */ |
| 1921 |
r = dns_packet_read_name(p, &rr->srv.name, /* allow_compression= */ true, NULL); |
| 1922 |
break; |
| 1923 |
|
| 1924 |
case DNS_TYPE_PTR: |
| 1925 |
case DNS_TYPE_NS: |
| 1926 |
case DNS_TYPE_CNAME: |
| 1927 |
case DNS_TYPE_DNAME: |
| 1928 |
r = dns_packet_read_name(p, &rr->ptr.name, true, NULL); |
| 1929 |
break; |
| 1930 |
|
| 1931 |
case DNS_TYPE_HINFO: |
| 1932 |
r = dns_packet_read_string(p, &rr->hinfo.cpu, NULL); |
| 1933 |
if (r < 0) |
| 1934 |
return r; |
| 1935 |
|
| 1936 |
r = dns_packet_read_string(p, &rr->hinfo.os, NULL); |
| 1937 |
break; |
| 1938 |
|
| 1939 |
case DNS_TYPE_SPF: /* exactly the same as TXT */ |
| 1940 |
case DNS_TYPE_TXT: |
| 1941 |
if (rdlength <= 0) { |
| 1942 |
r = dns_txt_item_new_empty(&rr->txt.items); |
| 1943 |
if (r < 0) |
| 1944 |
return r; |
| 1945 |
} else { |
| 1946 |
DnsTxtItem *last = NULL; |
| 1947 |
|
| 1948 |
while (p->rindex - offset < rdlength) { |
| 1949 |
DnsTxtItem *i; |
| 1950 |
const void *data; |
| 1951 |
size_t sz; |
| 1952 |
|
| 1953 |
r = dns_packet_read_raw_string(p, &data, &sz, NULL); |
| 1954 |
if (r < 0) |
| 1955 |
return r; |
| 1956 |
|
| 1957 |
i = malloc0(offsetof(DnsTxtItem, data) + sz + 1); /* extra NUL byte at the end */ |
| 1958 |
if (!i) |
| 1959 |
return -ENOMEM; |
| 1960 |
|
| 1961 |
memcpy(i->data, data, sz); |
| 1962 |
i->length = sz; |
| 1963 |
|
| 1964 |
LIST_INSERT_AFTER(items, rr->txt.items, last, i); |
| 1965 |
last = i; |
| 1966 |
} |
| 1967 |
} |
| 1968 |
|
| 1969 |
r = 0; |
| 1970 |
break; |
| 1971 |
|
| 1972 |
case DNS_TYPE_A: |
| 1973 |
r = dns_packet_read_blob(p, &rr->a.in_addr, sizeof(struct in_addr), NULL); |
| 1974 |
break; |
| 1975 |
|
| 1976 |
case DNS_TYPE_AAAA: |
| 1977 |
r = dns_packet_read_blob(p, &rr->aaaa.in6_addr, sizeof(struct in6_addr), NULL); |
| 1978 |
break; |
| 1979 |
|
| 1980 |
case DNS_TYPE_SOA: |
| 1981 |
r = dns_packet_read_name(p, &rr->soa.mname, true, NULL); |
| 1982 |
if (r < 0) |
| 1983 |
return r; |
| 1984 |
|
| 1985 |
r = dns_packet_read_name(p, &rr->soa.rname, true, NULL); |
| 1986 |
if (r < 0) |
| 1987 |
return r; |
| 1988 |
|
| 1989 |
r = dns_packet_read_uint32(p, &rr->soa.serial, NULL); |
| 1990 |
if (r < 0) |
| 1991 |
return r; |
| 1992 |
|
| 1993 |
r = dns_packet_read_uint32(p, &rr->soa.refresh, NULL); |
| 1994 |
if (r < 0) |
| 1995 |
return r; |
| 1996 |
|
| 1997 |
r = dns_packet_read_uint32(p, &rr->soa.retry, NULL); |
| 1998 |
if (r < 0) |
| 1999 |
return r; |
| 2000 |
|
| 2001 |
r = dns_packet_read_uint32(p, &rr->soa.expire, NULL); |
| 2002 |
if (r < 0) |
| 2003 |
return r; |
| 2004 |
|
| 2005 |
r = dns_packet_read_uint32(p, &rr->soa.minimum, NULL); |
| 2006 |
break; |
| 2007 |
|
| 2008 |
case DNS_TYPE_MX: |
| 2009 |
r = dns_packet_read_uint16(p, &rr->mx.priority, NULL); |
| 2010 |
if (r < 0) |
| 2011 |
return r; |
| 2012 |
|
| 2013 |
r = dns_packet_read_name(p, &rr->mx.exchange, true, NULL); |
| 2014 |
break; |
| 2015 |
|
| 2016 |
case DNS_TYPE_LOC: { |
| 2017 |
uint8_t t; |
| 2018 |
size_t pos; |
| 2019 |
|
| 2020 |
r = dns_packet_read_uint8(p, &t, &pos); |
| 2021 |
if (r < 0) |
| 2022 |
return r; |
| 2023 |
|
| 2024 |
if (t == 0) { |
| 2025 |
rr->loc.version = t; |
| 2026 |
|
| 2027 |
r = dns_packet_read_uint8(p, &rr->loc.size, NULL); |
| 2028 |
if (r < 0) |
| 2029 |
return r; |
| 2030 |
|
| 2031 |
if (!loc_size_ok(rr->loc.size)) |
| 2032 |
return -EBADMSG; |
| 2033 |
|
| 2034 |
r = dns_packet_read_uint8(p, &rr->loc.horiz_pre, NULL); |
| 2035 |
if (r < 0) |
| 2036 |
return r; |
| 2037 |
|
| 2038 |
if (!loc_size_ok(rr->loc.horiz_pre)) |
| 2039 |
return -EBADMSG; |
| 2040 |
|
| 2041 |
r = dns_packet_read_uint8(p, &rr->loc.vert_pre, NULL); |
| 2042 |
if (r < 0) |
| 2043 |
return r; |
| 2044 |
|
| 2045 |
if (!loc_size_ok(rr->loc.vert_pre)) |
| 2046 |
return -EBADMSG; |
| 2047 |
|
| 2048 |
r = dns_packet_read_uint32(p, &rr->loc.latitude, NULL); |
| 2049 |
if (r < 0) |
| 2050 |
return r; |
| 2051 |
|
| 2052 |
r = dns_packet_read_uint32(p, &rr->loc.longitude, NULL); |
| 2053 |
if (r < 0) |
| 2054 |
return r; |
| 2055 |
|
| 2056 |
r = dns_packet_read_uint32(p, &rr->loc.altitude, NULL); |
| 2057 |
if (r < 0) |
| 2058 |
return r; |
| 2059 |
|
| 2060 |
break; |
| 2061 |
} else { |
| 2062 |
dns_packet_rewind(p, pos); |
| 2063 |
rr->unparsable = true; |
| 2064 |
goto unparsable; |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
case DNS_TYPE_DS: |
| 2069 |
r = dns_packet_read_uint16(p, &rr->ds.key_tag, NULL); |
| 2070 |
if (r < 0) |
| 2071 |
return r; |
| 2072 |
|
| 2073 |
r = dns_packet_read_uint8(p, &rr->ds.algorithm, NULL); |
| 2074 |
if (r < 0) |
| 2075 |
return r; |
| 2076 |
|
| 2077 |
r = dns_packet_read_uint8(p, &rr->ds.digest_type, NULL); |
| 2078 |
if (r < 0) |
| 2079 |
return r; |
| 2080 |
|
| 2081 |
if (rdlength < 4) |
| 2082 |
return -EBADMSG; |
| 2083 |
|
| 2084 |
r = dns_packet_read_memdup(p, rdlength - 4, |
| 2085 |
&rr->ds.digest, &rr->ds.digest_size, |
| 2086 |
NULL); |
| 2087 |
if (r < 0) |
| 2088 |
return r; |
| 2089 |
|
| 2090 |
if (rr->ds.digest_size <= 0) |
| 2091 |
/* the accepted size depends on the algorithm, but for now |
| 2092 |
just ensure that the value is greater than zero */ |
| 2093 |
return -EBADMSG; |
| 2094 |
|
| 2095 |
break; |
| 2096 |
|
| 2097 |
case DNS_TYPE_SSHFP: |
| 2098 |
r = dns_packet_read_uint8(p, &rr->sshfp.algorithm, NULL); |
| 2099 |
if (r < 0) |
| 2100 |
return r; |
| 2101 |
|
| 2102 |
r = dns_packet_read_uint8(p, &rr->sshfp.fptype, NULL); |
| 2103 |
if (r < 0) |
| 2104 |
return r; |
| 2105 |
|
| 2106 |
if (rdlength < 2) |
| 2107 |
return -EBADMSG; |
| 2108 |
|
| 2109 |
r = dns_packet_read_memdup(p, rdlength - 2, |
| 2110 |
&rr->sshfp.fingerprint, &rr->sshfp.fingerprint_size, |
| 2111 |
NULL); |
| 2112 |
|
| 2113 |
if (rr->sshfp.fingerprint_size <= 0) |
| 2114 |
/* the accepted size depends on the algorithm, but for now |
| 2115 |
just ensure that the value is greater than zero */ |
| 2116 |
return -EBADMSG; |
| 2117 |
|
| 2118 |
break; |
| 2119 |
|
| 2120 |
case DNS_TYPE_DNSKEY: |
| 2121 |
r = dns_packet_read_uint16(p, &rr->dnskey.flags, NULL); |
| 2122 |
if (r < 0) |
| 2123 |
return r; |
| 2124 |
|
| 2125 |
r = dns_packet_read_uint8(p, &rr->dnskey.protocol, NULL); |
| 2126 |
if (r < 0) |
| 2127 |
return r; |
| 2128 |
|
| 2129 |
r = dns_packet_read_uint8(p, &rr->dnskey.algorithm, NULL); |
| 2130 |
if (r < 0) |
| 2131 |
return r; |
| 2132 |
|
| 2133 |
if (rdlength < 4) |
| 2134 |
return -EBADMSG; |
| 2135 |
|
| 2136 |
r = dns_packet_read_memdup(p, rdlength - 4, |
| 2137 |
&rr->dnskey.key, &rr->dnskey.key_size, |
| 2138 |
NULL); |
| 2139 |
|
| 2140 |
if (rr->dnskey.key_size <= 0) |
| 2141 |
/* the accepted size depends on the algorithm, but for now |
| 2142 |
just ensure that the value is greater than zero */ |
| 2143 |
return -EBADMSG; |
| 2144 |
|
| 2145 |
break; |
| 2146 |
|
| 2147 |
case DNS_TYPE_RRSIG: |
| 2148 |
r = dns_packet_read_uint16(p, &rr->rrsig.type_covered, NULL); |
| 2149 |
if (r < 0) |
| 2150 |
return r; |
| 2151 |
|
| 2152 |
r = dns_packet_read_uint8(p, &rr->rrsig.algorithm, NULL); |
| 2153 |
if (r < 0) |
| 2154 |
return r; |
| 2155 |
|
| 2156 |
r = dns_packet_read_uint8(p, &rr->rrsig.labels, NULL); |
| 2157 |
if (r < 0) |
| 2158 |
return r; |
| 2159 |
|
| 2160 |
r = dns_packet_read_uint32(p, &rr->rrsig.original_ttl, NULL); |
| 2161 |
if (r < 0) |
| 2162 |
return r; |
| 2163 |
|
| 2164 |
r = dns_packet_read_uint32(p, &rr->rrsig.expiration, NULL); |
| 2165 |
if (r < 0) |
| 2166 |
return r; |
| 2167 |
|
| 2168 |
r = dns_packet_read_uint32(p, &rr->rrsig.inception, NULL); |
| 2169 |
if (r < 0) |
| 2170 |
return r; |
| 2171 |
|
| 2172 |
r = dns_packet_read_uint16(p, &rr->rrsig.key_tag, NULL); |
| 2173 |
if (r < 0) |
| 2174 |
return r; |
| 2175 |
|
| 2176 |
r = dns_packet_read_name(p, &rr->rrsig.signer, false, NULL); |
| 2177 |
if (r < 0) |
| 2178 |
return r; |
| 2179 |
|
| 2180 |
if (rdlength < p->rindex - offset) |
| 2181 |
return -EBADMSG; |
| 2182 |
|
| 2183 |
r = dns_packet_read_memdup(p, offset + rdlength - p->rindex, |
| 2184 |
&rr->rrsig.signature, &rr->rrsig.signature_size, |
| 2185 |
NULL); |
| 2186 |
|
| 2187 |
if (rr->rrsig.signature_size <= 0) |
| 2188 |
/* the accepted size depends on the algorithm, but for now |
| 2189 |
just ensure that the value is greater than zero */ |
| 2190 |
return -EBADMSG; |
| 2191 |
|
| 2192 |
break; |
| 2193 |
|
| 2194 |
case DNS_TYPE_NSEC: { |
| 2195 |
|
| 2196 |
/* |
| 2197 |
* RFC6762, section 18.14 explicitly states mDNS should use name compression. |
| 2198 |
* This contradicts RFC3845, section 2.1.1 |
| 2199 |
*/ |
| 2200 |
|
| 2201 |
bool allow_compressed = p->protocol == DNS_PROTOCOL_MDNS; |
| 2202 |
|
| 2203 |
r = dns_packet_read_name(p, &rr->nsec.next_domain_name, allow_compressed, NULL); |
| 2204 |
if (r < 0) |
| 2205 |
return r; |
| 2206 |
|
| 2207 |
if (rdlength < p->rindex - offset) |
| 2208 |
return -EBADMSG; |
| 2209 |
|
| 2210 |
r = dns_packet_read_type_windows(p, &rr->nsec.types, offset + rdlength - p->rindex, NULL); |
| 2211 |
|
| 2212 |
/* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself |
| 2213 |
* is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records |
| 2214 |
* without the NSEC bit set. */ |
| 2215 |
|
| 2216 |
break; |
| 2217 |
} |
| 2218 |
case DNS_TYPE_NSEC3: { |
| 2219 |
uint8_t size; |
| 2220 |
|
| 2221 |
r = dns_packet_read_uint8(p, &rr->nsec3.algorithm, NULL); |
| 2222 |
if (r < 0) |
| 2223 |
return r; |
| 2224 |
|
| 2225 |
r = dns_packet_read_uint8(p, &rr->nsec3.flags, NULL); |
| 2226 |
if (r < 0) |
| 2227 |
return r; |
| 2228 |
|
| 2229 |
r = dns_packet_read_uint16(p, &rr->nsec3.iterations, NULL); |
| 2230 |
if (r < 0) |
| 2231 |
return r; |
| 2232 |
|
| 2233 |
/* this may be zero */ |
| 2234 |
r = dns_packet_read_uint8(p, &size, NULL); |
| 2235 |
if (r < 0) |
| 2236 |
return r; |
| 2237 |
|
| 2238 |
r = dns_packet_read_memdup(p, size, &rr->nsec3.salt, &rr->nsec3.salt_size, NULL); |
| 2239 |
if (r < 0) |
| 2240 |
return r; |
| 2241 |
|
| 2242 |
r = dns_packet_read_uint8(p, &size, NULL); |
| 2243 |
if (r < 0) |
| 2244 |
return r; |
| 2245 |
|
| 2246 |
if (size <= 0) |
| 2247 |
return -EBADMSG; |
| 2248 |
|
| 2249 |
r = dns_packet_read_memdup(p, size, |
| 2250 |
&rr->nsec3.next_hashed_name, &rr->nsec3.next_hashed_name_size, |
| 2251 |
NULL); |
| 2252 |
if (r < 0) |
| 2253 |
return r; |
| 2254 |
|
| 2255 |
if (rdlength < p->rindex - offset) |
| 2256 |
return -EBADMSG; |
| 2257 |
|
| 2258 |
r = dns_packet_read_type_windows(p, &rr->nsec3.types, offset + rdlength - p->rindex, NULL); |
| 2259 |
|
| 2260 |
/* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */ |
| 2261 |
|
| 2262 |
break; |
| 2263 |
} |
| 2264 |
|
| 2265 |
case DNS_TYPE_TLSA: |
| 2266 |
r = dns_packet_read_uint8(p, &rr->tlsa.cert_usage, NULL); |
| 2267 |
if (r < 0) |
| 2268 |
return r; |
| 2269 |
|
| 2270 |
r = dns_packet_read_uint8(p, &rr->tlsa.selector, NULL); |
| 2271 |
if (r < 0) |
| 2272 |
return r; |
| 2273 |
|
| 2274 |
r = dns_packet_read_uint8(p, &rr->tlsa.matching_type, NULL); |
| 2275 |
if (r < 0) |
| 2276 |
return r; |
| 2277 |
|
| 2278 |
if (rdlength < 3) |
| 2279 |
return -EBADMSG; |
| 2280 |
|
| 2281 |
r = dns_packet_read_memdup(p, rdlength - 3, |
| 2282 |
&rr->tlsa.data, &rr->tlsa.data_size, |
| 2283 |
NULL); |
| 2284 |
|
| 2285 |
if (rr->tlsa.data_size <= 0) |
| 2286 |
/* the accepted size depends on the algorithm, but for now |
| 2287 |
just ensure that the value is greater than zero */ |
| 2288 |
return -EBADMSG; |
| 2289 |
|
| 2290 |
break; |
| 2291 |
|
| 2292 |
case DNS_TYPE_SVCB: |
| 2293 |
case DNS_TYPE_HTTPS: |
| 2294 |
r = dns_packet_read_uint16(p, &rr->svcb.priority, NULL); |
| 2295 |
if (r < 0) |
| 2296 |
return r; |
| 2297 |
|
| 2298 |
r = dns_packet_read_name(p, &rr->svcb.target_name, false /* uncompressed */, NULL); |
| 2299 |
if (r < 0) |
| 2300 |
return r; |
| 2301 |
|
| 2302 |
DnsSvcParam *last = NULL; |
| 2303 |
while (p->rindex - offset < rdlength) { |
| 2304 |
_cleanup_free_ DnsSvcParam *i = NULL; |
| 2305 |
uint16_t svc_param_key; |
| 2306 |
uint16_t sz; |
| 2307 |
|
| 2308 |
r = dns_packet_read_uint16(p, &svc_param_key, NULL); |
| 2309 |
if (r < 0) |
| 2310 |
return r; |
| 2311 |
/* RFC 9460, section 2.2 says we must consider an RR malformed if SvcParamKeys are |
| 2312 |
* not in strictly increasing order */ |
| 2313 |
if (last && last->key >= svc_param_key) |
| 2314 |
return -EBADMSG; |
| 2315 |
|
| 2316 |
r = dns_packet_read_uint16(p, &sz, NULL); |
| 2317 |
if (r < 0) |
| 2318 |
return r; |
| 2319 |
|
| 2320 |
i = malloc0(offsetof(DnsSvcParam, value) + sz); |
| 2321 |
if (!i) |
| 2322 |
return -ENOMEM; |
| 2323 |
|
| 2324 |
i->key = svc_param_key; |
| 2325 |
i->length = sz; |
| 2326 |
r = dns_packet_read_blob(p, &i->value, sz, NULL); |
| 2327 |
if (r < 0) |
| 2328 |
return r; |
| 2329 |
if (!dns_svc_param_is_valid(i)) |
| 2330 |
return -EBADMSG; |
| 2331 |
|
| 2332 |
LIST_INSERT_AFTER(params, rr->svcb.params, last, i); |
| 2333 |
last = TAKE_PTR(i); |
| 2334 |
} |
| 2335 |
|
| 2336 |
break; |
| 2337 |
|
| 2338 |
case DNS_TYPE_CAA: |
| 2339 |
r = dns_packet_read_uint8(p, &rr->caa.flags, NULL); |
| 2340 |
if (r < 0) |
| 2341 |
return r; |
| 2342 |
|
| 2343 |
r = dns_packet_read_string(p, &rr->caa.tag, NULL); |
| 2344 |
if (r < 0) |
| 2345 |
return r; |
| 2346 |
|
| 2347 |
if (rdlength < p->rindex - offset) |
| 2348 |
return -EBADMSG; |
| 2349 |
|
| 2350 |
r = dns_packet_read_memdup(p, |
| 2351 |
rdlength + offset - p->rindex, |
| 2352 |
&rr->caa.value, &rr->caa.value_size, NULL); |
| 2353 |
|
| 2354 |
break; |
| 2355 |
|
| 2356 |
case DNS_TYPE_NAPTR: |
| 2357 |
r = dns_packet_read_uint16(p, &rr->naptr.order, NULL); |
| 2358 |
if (r < 0) |
| 2359 |
return r; |
| 2360 |
|
| 2361 |
r = dns_packet_read_uint16(p, &rr->naptr.preference, NULL); |
| 2362 |
if (r < 0) |
| 2363 |
return r; |
| 2364 |
|
| 2365 |
r = dns_packet_read_string(p, &rr->naptr.flags, NULL); |
| 2366 |
if (r < 0) |
| 2367 |
return r; |
| 2368 |
|
| 2369 |
r = dns_packet_read_string(p, &rr->naptr.services, NULL); |
| 2370 |
if (r < 0) |
| 2371 |
return r; |
| 2372 |
|
| 2373 |
r = dns_packet_read_string(p, &rr->naptr.regexp, NULL); |
| 2374 |
if (r < 0) |
| 2375 |
return r; |
| 2376 |
|
| 2377 |
r = dns_packet_read_name(p, &rr->naptr.replacement, /* allow_compression= */ false, NULL); |
| 2378 |
break; |
| 2379 |
|
| 2380 |
case DNS_TYPE_OPT: /* we only care about the header of OPT for now. */ |
| 2381 |
case DNS_TYPE_OPENPGPKEY: |
| 2382 |
default: |
| 2383 |
unparsable: |
| 2384 |
r = dns_packet_read_memdup(p, rdlength, &rr->generic.data, &rr->generic.data_size, NULL); |
| 2385 |
} |
| 2386 |
if (r < 0) |
| 2387 |
return r; |
| 2388 |
if (p->rindex - offset != rdlength) |
| 2389 |
return -EBADMSG; |
| 2390 |
|
| 2391 |
if (ret) |
| 2392 |
*ret = TAKE_PTR(rr); |
| 2393 |
if (ret_cache_flush) |
| 2394 |
*ret_cache_flush = cache_flush; |
| 2395 |
if (ret_start) |
| 2396 |
*ret_start = rewinder.saved_rindex; |
| 2397 |
|
| 2398 |
CANCEL_REWINDER(rewinder); |
| 2399 |
return 0; |
| 2400 |
} |
| 2401 |
|
| 2402 |
static bool opt_is_good(DnsResourceRecord *rr, bool *rfc6975) { |
| 2403 |
const uint8_t* p; |
| 2404 |
bool found_dau_dhu_n3u = false; |
| 2405 |
size_t l; |
| 2406 |
|
| 2407 |
/* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in |
| 2408 |
* a reply). */ |
| 2409 |
|
| 2410 |
assert(rr); |
| 2411 |
assert(rfc6975); |
| 2412 |
assert(rr->key->type == DNS_TYPE_OPT); |
| 2413 |
|
| 2414 |
/* Check that the version is 0 */ |
| 2415 |
if (((rr->ttl >> 16) & UINT32_C(0xFF)) != 0) { |
| 2416 |
*rfc6975 = false; |
| 2417 |
return true; /* if it's not version 0, it's OK, but we will ignore the OPT field contents */ |
| 2418 |
} |
| 2419 |
|
| 2420 |
p = rr->opt.data; |
| 2421 |
l = rr->opt.data_size; |
| 2422 |
while (l > 0) { |
| 2423 |
uint16_t option_code, option_length; |
| 2424 |
|
| 2425 |
/* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */ |
| 2426 |
if (l < 4U) |
| 2427 |
return false; |
| 2428 |
|
| 2429 |
option_code = unaligned_read_be16(p); |
| 2430 |
option_length = unaligned_read_be16(p + 2); |
| 2431 |
|
| 2432 |
if (l < option_length + 4U) |
| 2433 |
return false; |
| 2434 |
|
| 2435 |
/* RFC 6975 DAU, DHU or N3U fields found. */ |
| 2436 |
if (IN_SET(option_code, DNS_EDNS_OPT_DAU, DNS_EDNS_OPT_DHU, DNS_EDNS_OPT_N3U)) |
| 2437 |
found_dau_dhu_n3u = true; |
| 2438 |
|
| 2439 |
p += option_length + 4U; |
| 2440 |
l -= option_length + 4U; |
| 2441 |
} |
| 2442 |
|
| 2443 |
*rfc6975 = found_dau_dhu_n3u; |
| 2444 |
return true; |
| 2445 |
} |
| 2446 |
|
| 2447 |
static int dns_packet_extract_question(DnsPacket *p, DnsQuestion **ret_question) { |
| 2448 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 2449 |
unsigned n, prealloc; |
| 2450 |
int r; |
| 2451 |
|
| 2452 |
assert(ret_question); |
| 2453 |
|
| 2454 |
n = DNS_PACKET_QDCOUNT(p); |
| 2455 |
if (n == 0) { |
| 2456 |
*ret_question = NULL; |
| 2457 |
return 0; |
| 2458 |
} |
| 2459 |
|
| 2460 |
/* Calculate the maximum number of potential questions the remaining packet data can actually |
| 2461 |
* contain: p->size - p->rindex are the remaining unread bytes in the packet, and 5U is the minimum |
| 2462 |
* size of each question - 1 (QNAME) + 2 (QTYPE) + 2 (QCLASS). */ |
| 2463 |
prealloc = (p->size - p->rindex) / 5U; |
| 2464 |
if (prealloc == 0) |
| 2465 |
/* QDCOUNT > 0 but there's not enough space left for a single question. */ |
| 2466 |
return -EMSGSIZE; |
| 2467 |
|
| 2468 |
question = dns_question_new(n); |
| 2469 |
if (!question) |
| 2470 |
return -ENOMEM; |
| 2471 |
|
| 2472 |
_cleanup_set_free_ Set *keys = NULL; /* references to keys are kept by Question */ |
| 2473 |
|
| 2474 |
keys = set_new(&dns_resource_key_hash_ops); |
| 2475 |
if (!keys) |
| 2476 |
return log_oom(); |
| 2477 |
|
| 2478 |
/* Pre-allocate the question hashmap, but cap the pre-allocation to a number of questions the |
| 2479 |
* packet can realistically contain. That is, pick the minimal value from the claimed number |
| 2480 |
* of questions (n) and a maximum number of potential questions the remaining packet data can |
| 2481 |
* actually contain, see above. |
| 2482 |
* |
| 2483 |
* Note for the multiplication: higher multipliers give slightly higher efficiency through |
| 2484 |
* hash collisions, but the gains quickly drop off after 2. */ |
| 2485 |
r = set_reserve(keys, MIN(n, prealloc) * 2); |
| 2486 |
if (r < 0) |
| 2487 |
return r; |
| 2488 |
|
| 2489 |
for (unsigned i = 0; i < n; i++) { |
| 2490 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 2491 |
bool qu; |
| 2492 |
|
| 2493 |
r = dns_packet_read_key(p, &key, &qu, NULL); |
| 2494 |
if (r < 0) |
| 2495 |
return r; |
| 2496 |
|
| 2497 |
if (!dns_type_is_valid_query(key->type)) |
| 2498 |
return -EBADMSG; |
| 2499 |
|
| 2500 |
r = set_put(keys, key); |
| 2501 |
if (r < 0) |
| 2502 |
return r; |
| 2503 |
if (r == 0) |
| 2504 |
/* Already in the Question, let's skip */ |
| 2505 |
continue; |
| 2506 |
|
| 2507 |
r = dns_question_add_raw(question, key, qu ? DNS_QUESTION_WANTS_UNICAST_REPLY : 0); |
| 2508 |
if (r < 0) |
| 2509 |
return r; |
| 2510 |
} |
| 2511 |
|
| 2512 |
*ret_question = TAKE_PTR(question); |
| 2513 |
|
| 2514 |
return 0; |
| 2515 |
} |
| 2516 |
|
| 2517 |
static int dns_packet_extract_answer(DnsPacket *p, DnsAnswer **ret_answer) { |
| 2518 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 2519 |
unsigned n, prealloc; |
| 2520 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *previous = NULL; |
| 2521 |
bool bad_opt = false; |
| 2522 |
int r; |
| 2523 |
|
| 2524 |
assert(ret_answer); |
| 2525 |
|
| 2526 |
n = DNS_PACKET_RRCOUNT(p); |
| 2527 |
if (n == 0) { |
| 2528 |
*ret_answer = NULL; |
| 2529 |
return 0; |
| 2530 |
} |
| 2531 |
|
| 2532 |
/* Calculate the maximum number of potential RRs the remaining packet data can actually contain: |
| 2533 |
* p->size - p->rindex are the remaining unread bytes in the packet, and the 11U is the minimum size |
| 2534 |
* of each RR - 1 (NAME) + 2 (TYPE) + 2 (CLASS) + 4 (TTL) + 2 (RDLENGTH). */ |
| 2535 |
prealloc = (p->size - p->rindex) / 11U; |
| 2536 |
if (prealloc == 0) |
| 2537 |
/* RRCOUNT > 0 but there's not enough space left for a single RR. */ |
| 2538 |
return -EMSGSIZE; |
| 2539 |
|
| 2540 |
/* Pre-allocate the answer hashmap, but cap the pre-allocation to a number of RRs the packet can |
| 2541 |
* realistically contain. That is, pick the minimal value from the claimed number of RRs (n) and a |
| 2542 |
* maximum number of potential RRs the remaining packet data can actually contain, see above. */ |
| 2543 |
answer = dns_answer_new(MIN(n, prealloc)); |
| 2544 |
if (!answer) |
| 2545 |
return -ENOMEM; |
| 2546 |
|
| 2547 |
for (unsigned i = 0; i < n; i++) { |
| 2548 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 2549 |
bool cache_flush = false; |
| 2550 |
size_t start; |
| 2551 |
|
| 2552 |
if (p->rindex == p->size && p->opt) { |
| 2553 |
/* If we reached the end of the packet already, but there are still more RRs |
| 2554 |
* declared, then that's a corrupt packet. Let's accept the packet anyway, since it's |
| 2555 |
* apparently a common bug in routers. Let's however suppress OPT support in this |
| 2556 |
* case, so that we force the rest of the logic into lowest DNS baseline support. Or |
| 2557 |
* to say this differently: if the DNS server doesn't even get the RR counts right, |
| 2558 |
* it's highly unlikely it gets EDNS right. */ |
| 2559 |
log_debug("More resource records declared in packet than included, suppressing OPT."); |
| 2560 |
bad_opt = true; |
| 2561 |
break; |
| 2562 |
} |
| 2563 |
|
| 2564 |
r = dns_packet_read_rr(p, &rr, &cache_flush, &start); |
| 2565 |
if (r < 0) |
| 2566 |
return r; |
| 2567 |
|
| 2568 |
/* Try to reduce memory usage a bit */ |
| 2569 |
if (previous) |
| 2570 |
dns_resource_key_reduce(&rr->key, &previous->key); |
| 2571 |
|
| 2572 |
if (rr->key->type == DNS_TYPE_OPT) { |
| 2573 |
bool has_rfc6975; |
| 2574 |
|
| 2575 |
if (p->opt || bad_opt) { |
| 2576 |
/* Multiple OPT RRs? if so, let's ignore all, because there's |
| 2577 |
* something wrong with the server, and if one is valid we wouldn't |
| 2578 |
* know which one. */ |
| 2579 |
log_debug("Multiple OPT RRs detected, ignoring all."); |
| 2580 |
bad_opt = true; |
| 2581 |
continue; |
| 2582 |
} |
| 2583 |
|
| 2584 |
if (!dns_name_is_root(dns_resource_key_name(rr->key))) { |
| 2585 |
/* If the OPT RR is not owned by the root domain, then it is bad, |
| 2586 |
* let's ignore it. */ |
| 2587 |
log_debug("OPT RR is not owned by root domain, ignoring."); |
| 2588 |
bad_opt = true; |
| 2589 |
continue; |
| 2590 |
} |
| 2591 |
|
| 2592 |
if (i < DNS_PACKET_ANCOUNT(p) + DNS_PACKET_NSCOUNT(p)) { |
| 2593 |
/* OPT RR is in the wrong section? Some Belkin routers do this. This |
| 2594 |
* is a hint the EDNS implementation is borked, like the Belkin one |
| 2595 |
* is, hence ignore it. */ |
| 2596 |
log_debug("OPT RR in wrong section, ignoring."); |
| 2597 |
bad_opt = true; |
| 2598 |
continue; |
| 2599 |
} |
| 2600 |
|
| 2601 |
if (!opt_is_good(rr, &has_rfc6975)) { |
| 2602 |
log_debug("Malformed OPT RR, ignoring."); |
| 2603 |
bad_opt = true; |
| 2604 |
continue; |
| 2605 |
} |
| 2606 |
|
| 2607 |
if (DNS_PACKET_QR(p)) { |
| 2608 |
/* Additional checks for responses */ |
| 2609 |
|
| 2610 |
if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr)) |
| 2611 |
/* If this is a reply and we don't know the EDNS version |
| 2612 |
* then something is weird... */ |
| 2613 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2614 |
"EDNS version newer that our request, bad server."); |
| 2615 |
|
| 2616 |
if (has_rfc6975) { |
| 2617 |
/* If the OPT RR contains RFC6975 algorithm data, then this |
| 2618 |
* is indication that the server just copied the OPT it got |
| 2619 |
* from us (which contained that data) back into the reply. |
| 2620 |
* If so, then it doesn't properly support EDNS, as RFC6975 |
| 2621 |
* makes it very clear that the algorithm data should only |
| 2622 |
* be contained in questions, never in replies. Crappy |
| 2623 |
* Belkin routers copy the OPT data for example, hence let's |
| 2624 |
* detect this so that we downgrade early. */ |
| 2625 |
log_debug("OPT RR contains RFC6975 data, ignoring."); |
| 2626 |
bad_opt = true; |
| 2627 |
continue; |
| 2628 |
} |
| 2629 |
} |
| 2630 |
|
| 2631 |
p->opt = dns_resource_record_ref(rr); |
| 2632 |
p->opt_start = start; |
| 2633 |
assert(p->rindex >= start); |
| 2634 |
p->opt_size = p->rindex - start; |
| 2635 |
} else { |
| 2636 |
DnsAnswerFlags flags = 0; |
| 2637 |
|
| 2638 |
if (p->protocol == DNS_PROTOCOL_MDNS) { |
| 2639 |
flags |= DNS_ANSWER_REFUSE_TTL_NO_MATCH; |
| 2640 |
if (!cache_flush) |
| 2641 |
flags |= DNS_ANSWER_SHARED_OWNER; |
| 2642 |
} |
| 2643 |
|
| 2644 |
/* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be |
| 2645 |
* cached. Hence mark only those RRs as cacheable by default, but not the ones from |
| 2646 |
* the Additional or Authority sections. |
| 2647 |
* This restriction does not apply to mDNS records (RFC 6762). */ |
| 2648 |
if (i < DNS_PACKET_ANCOUNT(p)) |
| 2649 |
flags |= DNS_ANSWER_CACHEABLE|DNS_ANSWER_SECTION_ANSWER; |
| 2650 |
else if (i < DNS_PACKET_ANCOUNT(p) + DNS_PACKET_NSCOUNT(p)) |
| 2651 |
flags |= DNS_ANSWER_SECTION_AUTHORITY; |
| 2652 |
else { |
| 2653 |
flags |= DNS_ANSWER_SECTION_ADDITIONAL; |
| 2654 |
if (p->protocol == DNS_PROTOCOL_MDNS) |
| 2655 |
flags |= DNS_ANSWER_CACHEABLE; |
| 2656 |
} |
| 2657 |
|
| 2658 |
r = dns_answer_add(answer, rr, p->ifindex, flags, NULL); |
| 2659 |
if (r < 0) |
| 2660 |
return r; |
| 2661 |
} |
| 2662 |
|
| 2663 |
/* Remember this RR, so that we can potentially merge its ->key object with the |
| 2664 |
* next RR. Note that we only do this if we actually decided to keep the RR around. |
| 2665 |
*/ |
| 2666 |
DNS_RR_REPLACE(previous, dns_resource_record_ref(rr)); |
| 2667 |
} |
| 2668 |
|
| 2669 |
if (bad_opt) { |
| 2670 |
p->opt = dns_resource_record_unref(p->opt); |
| 2671 |
p->opt_start = p->opt_size = SIZE_MAX; |
| 2672 |
} |
| 2673 |
|
| 2674 |
*ret_answer = TAKE_PTR(answer); |
| 2675 |
|
| 2676 |
return 0; |
| 2677 |
} |
| 2678 |
|
| 2679 |
int dns_packet_extract(DnsPacket *p) { |
| 2680 |
assert(p); |
| 2681 |
|
| 2682 |
if (p->extracted) |
| 2683 |
return 0; |
| 2684 |
|
| 2685 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 2686 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 2687 |
_unused_ _cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 2688 |
int r; |
| 2689 |
|
| 2690 |
dns_packet_rewind(p, DNS_PACKET_HEADER_SIZE); |
| 2691 |
|
| 2692 |
r = dns_packet_extract_question(p, &question); |
| 2693 |
if (r < 0) |
| 2694 |
return r; |
| 2695 |
|
| 2696 |
r = dns_packet_extract_answer(p, &answer); |
| 2697 |
if (r < 0) |
| 2698 |
return r; |
| 2699 |
|
| 2700 |
if (p->rindex < p->size) { |
| 2701 |
log_debug("Trailing garbage in packet, suppressing OPT."); |
| 2702 |
p->opt = dns_resource_record_unref(p->opt); |
| 2703 |
p->opt_start = p->opt_size = SIZE_MAX; |
| 2704 |
} |
| 2705 |
|
| 2706 |
p->question = TAKE_PTR(question); |
| 2707 |
p->answer = TAKE_PTR(answer); |
| 2708 |
p->extracted = true; |
| 2709 |
|
| 2710 |
/* no CANCEL, always rewind */ |
| 2711 |
return 0; |
| 2712 |
} |
| 2713 |
|
| 2714 |
int dns_packet_is_reply_for(DnsPacket *p, const DnsResourceKey *key) { |
| 2715 |
int r; |
| 2716 |
|
| 2717 |
assert(p); |
| 2718 |
assert(key); |
| 2719 |
|
| 2720 |
/* Checks if the specified packet is a reply for the specified |
| 2721 |
* key and the specified key is the only one in the question |
| 2722 |
* section. */ |
| 2723 |
|
| 2724 |
if (DNS_PACKET_QR(p) != 1) |
| 2725 |
return 0; |
| 2726 |
|
| 2727 |
/* Let's unpack the packet, if that hasn't happened yet. */ |
| 2728 |
r = dns_packet_extract(p); |
| 2729 |
if (r < 0) |
| 2730 |
return r; |
| 2731 |
|
| 2732 |
if (!p->question) |
| 2733 |
return 0; |
| 2734 |
|
| 2735 |
if (p->question->n_keys != 1) |
| 2736 |
return 0; |
| 2737 |
|
| 2738 |
return dns_resource_key_equal(dns_question_first_key(p->question), key); |
| 2739 |
} |
| 2740 |
|
| 2741 |
int dns_packet_patch_max_udp_size(DnsPacket *p, uint16_t max_udp_size) { |
| 2742 |
assert(p); |
| 2743 |
assert(max_udp_size >= DNS_PACKET_UNICAST_SIZE_MAX); |
| 2744 |
|
| 2745 |
if (p->opt_start == SIZE_MAX) /* No OPT section, nothing to patch */ |
| 2746 |
return 0; |
| 2747 |
|
| 2748 |
assert(p->opt_size != SIZE_MAX); |
| 2749 |
assert(p->opt_size >= 5); |
| 2750 |
|
| 2751 |
unaligned_write_be16(DNS_PACKET_DATA(p) + p->opt_start + 3, max_udp_size); |
| 2752 |
return 1; |
| 2753 |
} |
| 2754 |
|
| 2755 |
static int patch_rr(DnsPacket *p, usec_t age) { |
| 2756 |
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 2757 |
size_t ttl_index; |
| 2758 |
uint32_t ttl; |
| 2759 |
uint16_t type, rdlength; |
| 2760 |
int r; |
| 2761 |
|
| 2762 |
/* Patches the RR at the current rindex, subtracts the specified time from the TTL */ |
| 2763 |
|
| 2764 |
r = dns_packet_read_name(p, NULL, true, NULL); |
| 2765 |
if (r < 0) |
| 2766 |
return r; |
| 2767 |
|
| 2768 |
r = dns_packet_read_uint16(p, &type, NULL); |
| 2769 |
if (r < 0) |
| 2770 |
return r; |
| 2771 |
|
| 2772 |
r = dns_packet_read_uint16(p, NULL, NULL); |
| 2773 |
if (r < 0) |
| 2774 |
return r; |
| 2775 |
|
| 2776 |
r = dns_packet_read_uint32(p, &ttl, &ttl_index); |
| 2777 |
if (r < 0) |
| 2778 |
return r; |
| 2779 |
|
| 2780 |
if (type != DNS_TYPE_OPT) { /* The TTL of the OPT field is not actually a TTL, skip it */ |
| 2781 |
ttl = LESS_BY(ttl * USEC_PER_SEC, age) / USEC_PER_SEC; |
| 2782 |
unaligned_write_be32(DNS_PACKET_DATA(p) + ttl_index, ttl); |
| 2783 |
} |
| 2784 |
|
| 2785 |
r = dns_packet_read_uint16(p, &rdlength, NULL); |
| 2786 |
if (r < 0) |
| 2787 |
return r; |
| 2788 |
|
| 2789 |
r = dns_packet_read(p, rdlength, NULL, NULL); |
| 2790 |
if (r < 0) |
| 2791 |
return r; |
| 2792 |
|
| 2793 |
CANCEL_REWINDER(rewinder); |
| 2794 |
return 0; |
| 2795 |
} |
| 2796 |
|
| 2797 |
int dns_packet_patch_ttls(DnsPacket *p, usec_t timestamp) { |
| 2798 |
assert(p); |
| 2799 |
assert(timestamp_is_set(timestamp)); |
| 2800 |
|
| 2801 |
/* Adjusts all TTLs in the packet by subtracting the time difference between now and the specified timestamp */ |
| 2802 |
|
| 2803 |
_unused_ _cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p); |
| 2804 |
unsigned n; |
| 2805 |
usec_t k; |
| 2806 |
int r; |
| 2807 |
|
| 2808 |
k = now(CLOCK_BOOTTIME); |
| 2809 |
assert(k >= timestamp); |
| 2810 |
k -= timestamp; |
| 2811 |
|
| 2812 |
dns_packet_rewind(p, DNS_PACKET_HEADER_SIZE); |
| 2813 |
|
| 2814 |
n = DNS_PACKET_QDCOUNT(p); |
| 2815 |
for (unsigned i = 0; i < n; i++) { |
| 2816 |
r = dns_packet_read_key(p, NULL, NULL, NULL); |
| 2817 |
if (r < 0) |
| 2818 |
return r; |
| 2819 |
} |
| 2820 |
|
| 2821 |
n = DNS_PACKET_RRCOUNT(p); |
| 2822 |
for (unsigned i = 0; i < n; i++) { |
| 2823 |
|
| 2824 |
/* DNS servers suck, hence the RR count is in many servers off. If we reached the end |
| 2825 |
* prematurely, accept that, exit early */ |
| 2826 |
if (p->rindex == p->size) |
| 2827 |
break; |
| 2828 |
|
| 2829 |
r = patch_rr(p, k); |
| 2830 |
if (r < 0) |
| 2831 |
return r; |
| 2832 |
} |
| 2833 |
|
| 2834 |
return 0; |
| 2835 |
} |
| 2836 |
|
| 2837 |
static void dns_packet_hash_func(const DnsPacket *s, struct siphash *state) { |
| 2838 |
assert(s); |
| 2839 |
|
| 2840 |
siphash24_compress_typesafe(s->size, state); |
| 2841 |
siphash24_compress(DNS_PACKET_DATA((DnsPacket*) s), s->size, state); |
| 2842 |
} |
| 2843 |
|
| 2844 |
static int dns_packet_compare_func(const DnsPacket *x, const DnsPacket *y) { |
| 2845 |
int r; |
| 2846 |
|
| 2847 |
r = CMP(x->size, y->size); |
| 2848 |
if (r != 0) |
| 2849 |
return r; |
| 2850 |
|
| 2851 |
return memcmp(DNS_PACKET_DATA((DnsPacket*) x), DNS_PACKET_DATA((DnsPacket*) y), x->size); |
| 2852 |
} |
| 2853 |
|
| 2854 |
DEFINE_HASH_OPS(dns_packet_hash_ops, DnsPacket, dns_packet_hash_func, dns_packet_compare_func); |
| 2855 |
|
| 2856 |
bool dns_packet_equal(const DnsPacket *a, const DnsPacket *b) { |
| 2857 |
return dns_packet_compare_func(a, b) == 0; |
| 2858 |
} |
| 2859 |
|
| 2860 |
int dns_packet_ede_rcode(DnsPacket *p, int *ret_ede_rcode, char **ret_ede_msg) { |
| 2861 |
const uint8_t *d; |
| 2862 |
size_t l; |
| 2863 |
int r; |
| 2864 |
|
| 2865 |
assert(p); |
| 2866 |
|
| 2867 |
if (!p->opt) |
| 2868 |
return -ENOENT; |
| 2869 |
|
| 2870 |
d = p->opt->opt.data; |
| 2871 |
l = p->opt->opt.data_size; |
| 2872 |
|
| 2873 |
while (l > 0) { |
| 2874 |
uint16_t code, length; |
| 2875 |
|
| 2876 |
if (l < 4U) |
| 2877 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2878 |
"EDNS0 variable part has invalid size."); |
| 2879 |
|
| 2880 |
code = unaligned_read_be16(d); |
| 2881 |
length = unaligned_read_be16(d + 2); |
| 2882 |
|
| 2883 |
if (l < 4U + length) |
| 2884 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2885 |
"Truncated option in EDNS0 variable part."); |
| 2886 |
|
| 2887 |
if (code == DNS_EDNS_OPT_EXT_ERROR) { |
| 2888 |
_cleanup_free_ char *msg = NULL; |
| 2889 |
|
| 2890 |
if (length < 2U) |
| 2891 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2892 |
"EDNS0 truncated EDE info code."); |
| 2893 |
|
| 2894 |
r = make_cstring(d + 6, length - 2U, MAKE_CSTRING_ALLOW_TRAILING_NUL, &msg); |
| 2895 |
if (r < 0) |
| 2896 |
return log_debug_errno(r, "Invalid EDE text in opt."); |
| 2897 |
|
| 2898 |
if (ret_ede_msg) { |
| 2899 |
if (!utf8_is_valid(msg)) { |
| 2900 |
_cleanup_free_ char *msg_escaped = NULL; |
| 2901 |
|
| 2902 |
msg_escaped = cescape(msg); |
| 2903 |
if (!msg_escaped) |
| 2904 |
return log_oom_debug(); |
| 2905 |
|
| 2906 |
*ret_ede_msg = TAKE_PTR(msg_escaped); |
| 2907 |
} else |
| 2908 |
*ret_ede_msg = TAKE_PTR(msg); |
| 2909 |
} |
| 2910 |
|
| 2911 |
if (ret_ede_rcode) |
| 2912 |
*ret_ede_rcode = unaligned_read_be16(d + 4); |
| 2913 |
|
| 2914 |
return 0; |
| 2915 |
} |
| 2916 |
|
| 2917 |
d += 4U + length; |
| 2918 |
l -= 4U + length; |
| 2919 |
} |
| 2920 |
|
| 2921 |
return -ENOENT; |
| 2922 |
} |
| 2923 |
|
| 2924 |
bool dns_ede_rcode_is_dnssec(int ede_rcode) { |
| 2925 |
return IN_SET(ede_rcode, |
| 2926 |
DNS_EDE_RCODE_UNSUPPORTED_DNSKEY_ALG, |
| 2927 |
DNS_EDE_RCODE_UNSUPPORTED_DS_DIGEST, |
| 2928 |
DNS_EDE_RCODE_DNSSEC_INDETERMINATE, |
| 2929 |
DNS_EDE_RCODE_DNSSEC_BOGUS, |
| 2930 |
DNS_EDE_RCODE_SIG_EXPIRED, |
| 2931 |
DNS_EDE_RCODE_SIG_NOT_YET_VALID, |
| 2932 |
DNS_EDE_RCODE_DNSKEY_MISSING, |
| 2933 |
DNS_EDE_RCODE_RRSIG_MISSING, |
| 2934 |
DNS_EDE_RCODE_NO_ZONE_KEY_BIT, |
| 2935 |
DNS_EDE_RCODE_NSEC_MISSING |
| 2936 |
); |
| 2937 |
} |
| 2938 |
|
| 2939 |
int dns_packet_has_nsid_request(DnsPacket *p) { |
| 2940 |
bool has_nsid = false; |
| 2941 |
const uint8_t *d; |
| 2942 |
size_t l; |
| 2943 |
|
| 2944 |
assert(p); |
| 2945 |
|
| 2946 |
if (!p->opt) |
| 2947 |
return false; |
| 2948 |
|
| 2949 |
d = p->opt->opt.data; |
| 2950 |
l = p->opt->opt.data_size; |
| 2951 |
|
| 2952 |
while (l > 0) { |
| 2953 |
uint16_t code, length; |
| 2954 |
|
| 2955 |
if (l < 4U) |
| 2956 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2957 |
"EDNS0 variable part has invalid size."); |
| 2958 |
|
| 2959 |
code = unaligned_read_be16(d); |
| 2960 |
length = unaligned_read_be16(d + 2); |
| 2961 |
|
| 2962 |
if (l < 4U + length) |
| 2963 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2964 |
"Truncated option in EDNS0 variable part."); |
| 2965 |
|
| 2966 |
if (code == DNS_EDNS_OPT_NSID) { |
| 2967 |
if (has_nsid) |
| 2968 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2969 |
"Duplicate NSID option in EDNS0 variable part."); |
| 2970 |
|
| 2971 |
if (length != 0) |
| 2972 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2973 |
"Non-empty NSID option in DNS request."); |
| 2974 |
|
| 2975 |
has_nsid = true; |
| 2976 |
} |
| 2977 |
|
| 2978 |
d += 4U + length; |
| 2979 |
l -= 4U + length; |
| 2980 |
} |
| 2981 |
|
| 2982 |
return has_nsid; |
| 2983 |
} |
| 2984 |
|
| 2985 |
size_t dns_packet_size_unfragmented(DnsPacket *p) { |
| 2986 |
assert(p); |
| 2987 |
|
| 2988 |
if (p->fragsize == 0) /* Wasn't fragmented */ |
| 2989 |
return p->size; |
| 2990 |
|
| 2991 |
/* The fragment size (p->fragsize) covers the whole (fragmented) IP packet, while the regular packet |
| 2992 |
* size (p->size) only covers the DNS part. Thus, subtract the UDP header from the largest fragment |
| 2993 |
* size, in order to determine which size of DNS packet would have gone through without |
| 2994 |
* fragmenting. */ |
| 2995 |
|
| 2996 |
return LESS_BY(p->fragsize, udp_header_size(p->family)); |
| 2997 |
} |
| 2998 |
|
| 2999 |
static const char* const dns_svc_param_key_table[_DNS_SVC_PARAM_KEY_MAX_DEFINED] = { |
| 3000 |
[DNS_SVC_PARAM_KEY_MANDATORY] = "mandatory", |
| 3001 |
[DNS_SVC_PARAM_KEY_ALPN] = "alpn", |
| 3002 |
[DNS_SVC_PARAM_KEY_NO_DEFAULT_ALPN] = "no-default-alpn", |
| 3003 |
[DNS_SVC_PARAM_KEY_PORT] = "port", |
| 3004 |
[DNS_SVC_PARAM_KEY_IPV4HINT] = "ipv4hint", |
| 3005 |
[DNS_SVC_PARAM_KEY_ECH] = "ech", |
| 3006 |
[DNS_SVC_PARAM_KEY_IPV6HINT] = "ipv6hint", |
| 3007 |
[DNS_SVC_PARAM_KEY_DOHPATH] = "dohpath", |
| 3008 |
[DNS_SVC_PARAM_KEY_OHTTP] = "ohttp", |
| 3009 |
}; |
| 3010 |
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(dns_svc_param_key, int); |
| 3011 |
|
| 3012 |
const char* format_dns_svc_param_key(uint16_t i, char buf[static DECIMAL_STR_MAX(uint16_t)+3]) { |
| 3013 |
const char *p = dns_svc_param_key_to_string(i); |
| 3014 |
if (p) |
| 3015 |
return p; |
| 3016 |
|
| 3017 |
return snprintf_ok(buf, DECIMAL_STR_MAX(uint16_t)+3, "key%i", i); |
| 3018 |
} |
| 3019 |
|
| 3020 |
static const char* const dns_rcode_table[_DNS_RCODE_MAX_DEFINED] = { |
| 3021 |
[DNS_RCODE_SUCCESS] = "SUCCESS", |
| 3022 |
[DNS_RCODE_FORMERR] = "FORMERR", |
| 3023 |
[DNS_RCODE_SERVFAIL] = "SERVFAIL", |
| 3024 |
[DNS_RCODE_NXDOMAIN] = "NXDOMAIN", |
| 3025 |
[DNS_RCODE_NOTIMP] = "NOTIMP", |
| 3026 |
[DNS_RCODE_REFUSED] = "REFUSED", |
| 3027 |
[DNS_RCODE_YXDOMAIN] = "YXDOMAIN", |
| 3028 |
[DNS_RCODE_YXRRSET] = "YRRSET", |
| 3029 |
[DNS_RCODE_NXRRSET] = "NXRRSET", |
| 3030 |
[DNS_RCODE_NOTAUTH] = "NOTAUTH", |
| 3031 |
[DNS_RCODE_NOTZONE] = "NOTZONE", |
| 3032 |
[DNS_RCODE_DSOTYPENI] = "DSOTYPENI", |
| 3033 |
[DNS_RCODE_BADVERS] = "BADVERS", |
| 3034 |
[DNS_RCODE_BADKEY] = "BADKEY", |
| 3035 |
[DNS_RCODE_BADTIME] = "BADTIME", |
| 3036 |
[DNS_RCODE_BADMODE] = "BADMODE", |
| 3037 |
[DNS_RCODE_BADNAME] = "BADNAME", |
| 3038 |
[DNS_RCODE_BADALG] = "BADALG", |
| 3039 |
[DNS_RCODE_BADTRUNC] = "BADTRUNC", |
| 3040 |
[DNS_RCODE_BADCOOKIE] = "BADCOOKIE", |
| 3041 |
}; |
| 3042 |
DEFINE_STRING_TABLE_LOOKUP(dns_rcode, int); |
| 3043 |
|
| 3044 |
const char* format_dns_rcode(int i, char buf[static DECIMAL_STR_MAX(int)]) { |
| 3045 |
const char *p = dns_rcode_to_string(i); |
| 3046 |
if (p) |
| 3047 |
return p; |
| 3048 |
|
| 3049 |
return snprintf_ok(buf, DECIMAL_STR_MAX(int), "%i", i); |
| 3050 |
} |
| 3051 |
|
| 3052 |
static const char* const dns_ede_rcode_table[_DNS_EDE_RCODE_MAX_DEFINED] = { |
| 3053 |
[DNS_EDE_RCODE_OTHER] = "Other", |
| 3054 |
[DNS_EDE_RCODE_UNSUPPORTED_DNSKEY_ALG] = "Unsupported DNSKEY Algorithm", |
| 3055 |
[DNS_EDE_RCODE_UNSUPPORTED_DS_DIGEST] = "Unsupported DS Digest Type", |
| 3056 |
[DNS_EDE_RCODE_STALE_ANSWER] = "Stale Answer", |
| 3057 |
[DNS_EDE_RCODE_FORGED_ANSWER] = "Forged Answer", |
| 3058 |
[DNS_EDE_RCODE_DNSSEC_INDETERMINATE] = "DNSSEC Indeterminate", |
| 3059 |
[DNS_EDE_RCODE_DNSSEC_BOGUS] = "DNSSEC Bogus", |
| 3060 |
[DNS_EDE_RCODE_SIG_EXPIRED] = "Signature Expired", |
| 3061 |
[DNS_EDE_RCODE_SIG_NOT_YET_VALID] = "Signature Not Yet Valid", |
| 3062 |
[DNS_EDE_RCODE_DNSKEY_MISSING] = "DNSKEY Missing", |
| 3063 |
[DNS_EDE_RCODE_RRSIG_MISSING] = "RRSIG Missing", |
| 3064 |
[DNS_EDE_RCODE_NO_ZONE_KEY_BIT] = "No Zone Key Bit Set", |
| 3065 |
[DNS_EDE_RCODE_NSEC_MISSING] = "NSEC Missing", |
| 3066 |
[DNS_EDE_RCODE_CACHED_ERROR] = "Cached Error", |
| 3067 |
[DNS_EDE_RCODE_NOT_READY] = "Not Ready", |
| 3068 |
[DNS_EDE_RCODE_BLOCKED] = "Blocked", |
| 3069 |
[DNS_EDE_RCODE_CENSORED] = "Censored", |
| 3070 |
[DNS_EDE_RCODE_FILTERED] = "Filtered", |
| 3071 |
[DNS_EDE_RCODE_PROHIBITED] = "Prohibited", |
| 3072 |
[DNS_EDE_RCODE_STALE_NXDOMAIN_ANSWER] = "Stale NXDOMAIN Answer", |
| 3073 |
[DNS_EDE_RCODE_NOT_AUTHORITATIVE] = "Not Authoritative", |
| 3074 |
[DNS_EDE_RCODE_NOT_SUPPORTED] = "Not Supported", |
| 3075 |
[DNS_EDE_RCODE_UNREACH_AUTHORITY] = "No Reachable Authority", |
| 3076 |
[DNS_EDE_RCODE_NET_ERROR] = "Network Error", |
| 3077 |
[DNS_EDE_RCODE_INVALID_DATA] = "Invalid Data", |
| 3078 |
[DNS_EDE_RCODE_SIG_NEVER] = "Signature Never Valid", |
| 3079 |
[DNS_EDE_RCODE_TOO_EARLY] = "Too Early", |
| 3080 |
[DNS_EDE_RCODE_UNSUPPORTED_NSEC3_ITER] = "Unsupported NSEC3 Iterations", |
| 3081 |
[DNS_EDE_RCODE_TRANSPORT_POLICY] = "Impossible Transport Policy", |
| 3082 |
[DNS_EDE_RCODE_SYNTHESIZED] = "Synthesized", |
| 3083 |
}; |
| 3084 |
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(dns_ede_rcode, int); |
| 3085 |
|
| 3086 |
const char* format_dns_ede_rcode(int i, char buf[static DECIMAL_STR_MAX(int)]) { |
| 3087 |
const char *p = dns_ede_rcode_to_string(i); |
| 3088 |
if (p) |
| 3089 |
return p; |
| 3090 |
|
| 3091 |
return snprintf_ok(buf, DECIMAL_STR_MAX(int), "%i", i); |
| 3092 |
} |
| 3093 |
|
| 3094 |
static const char* const dns_protocol_table[_DNS_PROTOCOL_MAX] = { |
| 3095 |
[DNS_PROTOCOL_DNS] = "dns", |
| 3096 |
[DNS_PROTOCOL_MDNS] = "mdns", |
| 3097 |
[DNS_PROTOCOL_LLMNR] = "llmnr", |
| 3098 |
}; |
| 3099 |
DEFINE_STRING_TABLE_LOOKUP(dns_protocol, DnsProtocol); |