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