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