]> git.proxmox.com Git - systemd.git/blame - src/resolve/resolved-dns-packet.c
Merge tag 'upstream/229'
[systemd.git] / src / resolve / resolved-dns-packet.c
CommitLineData
5eef597e
MP
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
db2df898 20#include "alloc-util.h"
86f210e9 21#include "dns-domain.h"
5eef597e 22#include "resolved-dns-packet.h"
db2df898
MP
23#include "string-table.h"
24#include "strv.h"
25#include "unaligned.h"
26#include "utf8.h"
27#include "util.h"
5eef597e 28
4c89c718
MP
29#define EDNS0_OPT_DO (1<<15)
30
5eef597e
MP
31int dns_packet_new(DnsPacket **ret, DnsProtocol protocol, size_t mtu) {
32 DnsPacket *p;
33 size_t a;
34
35 assert(ret);
36
7035cd9e 37 if (mtu <= UDP_PACKET_HEADER_SIZE)
5eef597e
MP
38 a = DNS_PACKET_SIZE_START;
39 else
7035cd9e 40 a = mtu - UDP_PACKET_HEADER_SIZE;
5eef597e
MP
41
42 if (a < DNS_PACKET_HEADER_SIZE)
43 a = DNS_PACKET_HEADER_SIZE;
44
45 /* round up to next page size */
46 a = PAGE_ALIGN(ALIGN(sizeof(DnsPacket)) + a) - ALIGN(sizeof(DnsPacket));
47
48 /* make sure we never allocate more than useful */
49 if (a > DNS_PACKET_SIZE_MAX)
50 a = DNS_PACKET_SIZE_MAX;
51
52 p = malloc0(ALIGN(sizeof(DnsPacket)) + a);
53 if (!p)
54 return -ENOMEM;
55
56 p->size = p->rindex = DNS_PACKET_HEADER_SIZE;
57 p->allocated = a;
58 p->protocol = protocol;
4c89c718 59 p->opt_start = p->opt_size = (size_t) -1;
5eef597e
MP
60 p->n_ref = 1;
61
62 *ret = p;
63
64 return 0;
65}
66
4c89c718 67void dns_packet_set_flags(DnsPacket *p, bool dnssec_checking_disabled, bool truncated) {
5eef597e 68
4c89c718 69 DnsPacketHeader *h;
5eef597e 70
4c89c718 71 assert(p);
5eef597e
MP
72
73 h = DNS_PACKET_HEADER(p);
74
4c89c718
MP
75 switch(p->protocol) {
76 case DNS_PROTOCOL_LLMNR:
77 assert(!truncated);
78
5eef597e
MP
79 h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
80 0 /* opcode */,
81 0 /* c */,
82 0 /* tc */,
83 0 /* t */,
84 0 /* ra */,
85 0 /* ad */,
86 0 /* cd */,
87 0 /* rcode */));
4c89c718
MP
88 break;
89
90 case DNS_PROTOCOL_MDNS:
91 h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
92 0 /* opcode */,
93 0 /* aa */,
94 truncated /* tc */,
95 0 /* rd (ask for recursion) */,
96 0 /* ra */,
97 0 /* ad */,
98 0 /* cd */,
99 0 /* rcode */));
100 break;
101
102 default:
103 assert(!truncated);
104
5eef597e
MP
105 h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
106 0 /* opcode */,
107 0 /* aa */,
108 0 /* tc */,
109 1 /* rd (ask for recursion) */,
110 0 /* ra */,
111 0 /* ad */,
4c89c718 112 dnssec_checking_disabled /* cd */,
5eef597e 113 0 /* rcode */));
4c89c718
MP
114 }
115}
116
117int dns_packet_new_query(DnsPacket **ret, DnsProtocol protocol, size_t mtu, bool dnssec_checking_disabled) {
118 DnsPacket *p;
119 int r;
120
121 assert(ret);
122
123 r = dns_packet_new(&p, protocol, mtu);
124 if (r < 0)
125 return r;
126
127 /* Always set the TC bit to 0 initially.
128 * If there are multiple packets later, we'll update the bit shortly before sending.
129 */
130 dns_packet_set_flags(p, dnssec_checking_disabled, false);
5eef597e
MP
131
132 *ret = p;
133 return 0;
134}
135
136DnsPacket *dns_packet_ref(DnsPacket *p) {
137
138 if (!p)
139 return NULL;
140
4c89c718
MP
141 assert(!p->on_stack);
142
5eef597e
MP
143 assert(p->n_ref > 0);
144 p->n_ref++;
145 return p;
146}
147
148static void dns_packet_free(DnsPacket *p) {
149 char *s;
150
151 assert(p);
152
153 dns_question_unref(p->question);
154 dns_answer_unref(p->answer);
4c89c718 155 dns_resource_record_unref(p->opt);
5eef597e
MP
156
157 while ((s = hashmap_steal_first_key(p->names)))
158 free(s);
159 hashmap_free(p->names);
160
161 free(p->_data);
4c89c718
MP
162
163 if (!p->on_stack)
164 free(p);
5eef597e
MP
165}
166
167DnsPacket *dns_packet_unref(DnsPacket *p) {
168 if (!p)
169 return NULL;
170
171 assert(p->n_ref > 0);
172
4c89c718
MP
173 dns_packet_unref(p->more);
174
5eef597e
MP
175 if (p->n_ref == 1)
176 dns_packet_free(p);
177 else
178 p->n_ref--;
179
180 return NULL;
181}
182
183int dns_packet_validate(DnsPacket *p) {
184 assert(p);
185
186 if (p->size < DNS_PACKET_HEADER_SIZE)
187 return -EBADMSG;
188
189 if (p->size > DNS_PACKET_SIZE_MAX)
190 return -EBADMSG;
191
192 return 1;
193}
194
195int dns_packet_validate_reply(DnsPacket *p) {
196 int r;
197
198 assert(p);
199
200 r = dns_packet_validate(p);
201 if (r < 0)
202 return r;
203
204 if (DNS_PACKET_QR(p) != 1)
205 return 0;
206
207 if (DNS_PACKET_OPCODE(p) != 0)
208 return -EBADMSG;
209
7035cd9e 210 switch (p->protocol) {
4c89c718 211
7035cd9e
MP
212 case DNS_PROTOCOL_LLMNR:
213 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
214 if (DNS_PACKET_QDCOUNT(p) != 1)
215 return -EBADMSG;
216
217 break;
218
4c89c718
MP
219 case DNS_PROTOCOL_MDNS:
220 /* RFC 6762, Section 18 */
221 if (DNS_PACKET_RCODE(p) != 0)
222 return -EBADMSG;
223
224 break;
225
7035cd9e
MP
226 default:
227 break;
228 }
5eef597e
MP
229
230 return 1;
231}
232
233int dns_packet_validate_query(DnsPacket *p) {
234 int r;
235
236 assert(p);
237
238 r = dns_packet_validate(p);
239 if (r < 0)
240 return r;
241
242 if (DNS_PACKET_QR(p) != 0)
243 return 0;
244
245 if (DNS_PACKET_OPCODE(p) != 0)
246 return -EBADMSG;
247
248 if (DNS_PACKET_TC(p))
249 return -EBADMSG;
250
7035cd9e 251 switch (p->protocol) {
4c89c718 252
7035cd9e
MP
253 case DNS_PROTOCOL_LLMNR:
254 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
255 if (DNS_PACKET_QDCOUNT(p) != 1)
256 return -EBADMSG;
5eef597e 257
7035cd9e
MP
258 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
259 if (DNS_PACKET_ANCOUNT(p) > 0)
260 return -EBADMSG;
5eef597e 261
7035cd9e
MP
262 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
263 if (DNS_PACKET_NSCOUNT(p) > 0)
264 return -EBADMSG;
265
266 break;
267
4c89c718
MP
268 case DNS_PROTOCOL_MDNS:
269 /* RFC 6762, Section 18 */
270 if (DNS_PACKET_AA(p) != 0 ||
271 DNS_PACKET_RD(p) != 0 ||
272 DNS_PACKET_RA(p) != 0 ||
273 DNS_PACKET_AD(p) != 0 ||
274 DNS_PACKET_CD(p) != 0 ||
275 DNS_PACKET_RCODE(p) != 0)
276 return -EBADMSG;
277
278 break;
279
7035cd9e
MP
280 default:
281 break;
282 }
5eef597e
MP
283
284 return 1;
285}
286
287static int dns_packet_extend(DnsPacket *p, size_t add, void **ret, size_t *start) {
288 assert(p);
289
290 if (p->size + add > p->allocated) {
291 size_t a;
292
293 a = PAGE_ALIGN((p->size + add) * 2);
294 if (a > DNS_PACKET_SIZE_MAX)
295 a = DNS_PACKET_SIZE_MAX;
296
297 if (p->size + add > a)
298 return -EMSGSIZE;
299
300 if (p->_data) {
301 void *d;
302
303 d = realloc(p->_data, a);
304 if (!d)
305 return -ENOMEM;
306
307 p->_data = d;
308 } else {
309 p->_data = malloc(a);
310 if (!p->_data)
311 return -ENOMEM;
312
313 memcpy(p->_data, (uint8_t*) p + ALIGN(sizeof(DnsPacket)), p->size);
314 memzero((uint8_t*) p->_data + p->size, a - p->size);
315 }
316
317 p->allocated = a;
318 }
319
320 if (start)
321 *start = p->size;
322
323 if (ret)
324 *ret = (uint8_t*) DNS_PACKET_DATA(p) + p->size;
325
326 p->size += add;
327 return 0;
328}
329
4c89c718 330void dns_packet_truncate(DnsPacket *p, size_t sz) {
5eef597e
MP
331 Iterator i;
332 char *s;
333 void *n;
334
335 assert(p);
336
337 if (p->size <= sz)
338 return;
339
7035cd9e 340 HASHMAP_FOREACH_KEY(n, s, p->names, i) {
5eef597e
MP
341
342 if (PTR_TO_SIZE(n) < sz)
343 continue;
344
345 hashmap_remove(p->names, s);
346 free(s);
347 }
348
349 p->size = sz;
350}
351
352int dns_packet_append_blob(DnsPacket *p, const void *d, size_t l, size_t *start) {
353 void *q;
354 int r;
355
356 assert(p);
357
358 r = dns_packet_extend(p, l, &q, start);
359 if (r < 0)
360 return r;
361
362 memcpy(q, d, l);
363 return 0;
364}
365
366int dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start) {
367 void *d;
368 int r;
369
370 assert(p);
371
372 r = dns_packet_extend(p, sizeof(uint8_t), &d, start);
373 if (r < 0)
374 return r;
375
376 ((uint8_t*) d)[0] = v;
377
378 return 0;
379}
380
381int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start) {
382 void *d;
383 int r;
384
385 assert(p);
386
387 r = dns_packet_extend(p, sizeof(uint16_t), &d, start);
388 if (r < 0)
389 return r;
390
f47781d8 391 unaligned_write_be16(d, v);
5eef597e
MP
392
393 return 0;
394}
395
396int dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start) {
397 void *d;
398 int r;
399
400 assert(p);
401
402 r = dns_packet_extend(p, sizeof(uint32_t), &d, start);
403 if (r < 0)
404 return r;
405
f47781d8 406 unaligned_write_be32(d, v);
5eef597e
MP
407
408 return 0;
409}
410
411int dns_packet_append_string(DnsPacket *p, const char *s, size_t *start) {
4c89c718
MP
412 assert(p);
413 assert(s);
414
415 return dns_packet_append_raw_string(p, s, strlen(s), start);
416}
417
418int dns_packet_append_raw_string(DnsPacket *p, const void *s, size_t size, size_t *start) {
5eef597e 419 void *d;
5eef597e
MP
420 int r;
421
422 assert(p);
4c89c718 423 assert(s || size == 0);
5eef597e 424
4c89c718 425 if (size > 255)
5eef597e
MP
426 return -E2BIG;
427
4c89c718 428 r = dns_packet_extend(p, 1 + size, &d, start);
5eef597e
MP
429 if (r < 0)
430 return r;
431
4c89c718
MP
432 ((uint8_t*) d)[0] = (uint8_t) size;
433
434 if (size > 0)
435 memcpy(((uint8_t*) d) + 1, s, size);
5eef597e
MP
436
437 return 0;
438}
439
4c89c718
MP
440int dns_packet_append_label(DnsPacket *p, const char *d, size_t l, bool canonical_candidate, size_t *start) {
441 uint8_t *w;
5eef597e
MP
442 int r;
443
4c89c718
MP
444 /* Append a label to a packet. Optionally, does this in DNSSEC
445 * canonical form, if this label is marked as a candidate for
446 * it, and the canonical form logic is enabled for the
447 * packet */
448
5eef597e
MP
449 assert(p);
450 assert(d);
451
452 if (l > DNS_LABEL_MAX)
453 return -E2BIG;
454
4c89c718 455 r = dns_packet_extend(p, 1 + l, (void**) &w, start);
5eef597e
MP
456 if (r < 0)
457 return r;
458
4c89c718
MP
459 *(w++) = (uint8_t) l;
460
461 if (p->canonical_form && canonical_candidate) {
462 size_t i;
463
464 /* Generate in canonical form, as defined by DNSSEC
465 * RFC 4034, Section 6.2, i.e. all lower-case. */
466
467 for (i = 0; i < l; i++)
468 w[i] = (uint8_t) ascii_tolower(d[i]);
469 } else
470 /* Otherwise, just copy the string unaltered. This is
471 * essential for DNS-SD, where the casing of labels
472 * matters and needs to be retained. */
473 memcpy(w, d, l);
5eef597e
MP
474
475 return 0;
476}
477
13d276d0
MP
478int dns_packet_append_name(
479 DnsPacket *p,
480 const char *name,
481 bool allow_compression,
4c89c718 482 bool canonical_candidate,
13d276d0
MP
483 size_t *start) {
484
5eef597e
MP
485 size_t saved_size;
486 int r;
487
488 assert(p);
489 assert(name);
490
13d276d0
MP
491 if (p->refuse_compression)
492 allow_compression = false;
493
5eef597e
MP
494 saved_size = p->size;
495
4c89c718
MP
496 while (!dns_name_is_root(name)) {
497 const char *z = name;
5eef597e
MP
498 char label[DNS_LABEL_MAX];
499 size_t n = 0;
5eef597e
MP
500
501 if (allow_compression)
502 n = PTR_TO_SIZE(hashmap_get(p->names, name));
503 if (n > 0) {
504 assert(n < p->size);
505
506 if (n < 0x4000) {
507 r = dns_packet_append_uint16(p, 0xC000 | n, NULL);
508 if (r < 0)
509 goto fail;
510
511 goto done;
512 }
513 }
514
5eef597e
MP
515 r = dns_label_unescape(&name, label, sizeof(label));
516 if (r < 0)
517 goto fail;
518
4c89c718 519 r = dns_packet_append_label(p, label, r, canonical_candidate, &n);
5eef597e
MP
520 if (r < 0)
521 goto fail;
522
523 if (allow_compression) {
4c89c718
MP
524 _cleanup_free_ char *s = NULL;
525
526 s = strdup(z);
527 if (!s) {
528 r = -ENOMEM;
529 goto fail;
530 }
531
5eef597e
MP
532 r = hashmap_ensure_allocated(&p->names, &dns_name_hash_ops);
533 if (r < 0)
534 goto fail;
535
536 r = hashmap_put(p->names, s, SIZE_TO_PTR(n));
537 if (r < 0)
538 goto fail;
539
540 s = NULL;
541 }
542 }
543
544 r = dns_packet_append_uint8(p, 0, NULL);
545 if (r < 0)
546 return r;
547
548done:
549 if (start)
550 *start = saved_size;
551
552 return 0;
553
554fail:
555 dns_packet_truncate(p, saved_size);
556 return r;
557}
558
559int dns_packet_append_key(DnsPacket *p, const DnsResourceKey *k, size_t *start) {
560 size_t saved_size;
561 int r;
562
563 assert(p);
564 assert(k);
565
566 saved_size = p->size;
567
4c89c718 568 r = dns_packet_append_name(p, DNS_RESOURCE_KEY_NAME(k), true, true, NULL);
5eef597e
MP
569 if (r < 0)
570 goto fail;
571
572 r = dns_packet_append_uint16(p, k->type, NULL);
573 if (r < 0)
574 goto fail;
575
576 r = dns_packet_append_uint16(p, k->class, NULL);
577 if (r < 0)
578 goto fail;
579
580 if (start)
581 *start = saved_size;
582
583 return 0;
584
585fail:
586 dns_packet_truncate(p, saved_size);
587 return r;
588}
589
4c89c718 590static int dns_packet_append_type_window(DnsPacket *p, uint8_t window, uint8_t length, const uint8_t *types, size_t *start) {
7035cd9e
MP
591 size_t saved_size;
592 int r;
593
594 assert(p);
595 assert(types);
596 assert(length > 0);
597
598 saved_size = p->size;
599
600 r = dns_packet_append_uint8(p, window, NULL);
601 if (r < 0)
602 goto fail;
603
604 r = dns_packet_append_uint8(p, length, NULL);
605 if (r < 0)
606 goto fail;
607
608 r = dns_packet_append_blob(p, types, length, NULL);
609 if (r < 0)
610 goto fail;
611
612 if (start)
613 *start = saved_size;
614
615 return 0;
616fail:
617 dns_packet_truncate(p, saved_size);
618 return r;
619}
620
621static int dns_packet_append_types(DnsPacket *p, Bitmap *types, size_t *start) {
622 Iterator i;
623 uint8_t window = 0;
624 uint8_t entry = 0;
625 uint8_t bitmaps[32] = {};
626 unsigned n;
627 size_t saved_size;
628 int r;
629
630 assert(p);
7035cd9e
MP
631
632 saved_size = p->size;
633
634 BITMAP_FOREACH(n, types, i) {
635 assert(n <= 0xffff);
636
637 if ((n >> 8) != window && bitmaps[entry / 8] != 0) {
638 r = dns_packet_append_type_window(p, window, entry / 8 + 1, bitmaps, NULL);
639 if (r < 0)
640 goto fail;
641
642 zero(bitmaps);
643 }
644
645 window = n >> 8;
7035cd9e
MP
646 entry = n & 255;
647
648 bitmaps[entry / 8] |= 1 << (7 - (entry % 8));
649 }
650
4c89c718
MP
651 if (bitmaps[entry / 8] != 0) {
652 r = dns_packet_append_type_window(p, window, entry / 8 + 1, bitmaps, NULL);
653 if (r < 0)
654 goto fail;
655 }
656
657 if (start)
658 *start = saved_size;
659
660 return 0;
661fail:
662 dns_packet_truncate(p, saved_size);
663 return r;
664}
665
666/* Append the OPT pseudo-RR described in RFC6891 */
667int dns_packet_append_opt(DnsPacket *p, uint16_t max_udp_size, bool edns0_do, size_t *start) {
668 size_t saved_size;
669 int r;
670
671 assert(p);
672 /* we must never advertise supported packet size smaller than the legacy max */
673 assert(max_udp_size >= DNS_PACKET_UNICAST_SIZE_MAX);
674
675 if (p->opt_start != (size_t) -1)
676 return -EBUSY;
677
678 assert(p->opt_size == (size_t) -1);
679
680 saved_size = p->size;
681
682 /* empty name */
683 r = dns_packet_append_uint8(p, 0, NULL);
684 if (r < 0)
685 return r;
686
687 /* type */
688 r = dns_packet_append_uint16(p, DNS_TYPE_OPT, NULL);
689 if (r < 0)
690 goto fail;
691
692 /* maximum udp packet that can be received */
693 r = dns_packet_append_uint16(p, max_udp_size, NULL);
694 if (r < 0)
695 goto fail;
696
697 /* extended RCODE and VERSION */
698 r = dns_packet_append_uint16(p, 0, NULL);
699 if (r < 0)
700 goto fail;
701
702 /* flags: DNSSEC OK (DO), see RFC3225 */
703 r = dns_packet_append_uint16(p, edns0_do ? EDNS0_OPT_DO : 0, NULL);
7035cd9e
MP
704 if (r < 0)
705 goto fail;
706
4c89c718
MP
707 /* RDLENGTH */
708
709 if (edns0_do) {
710 /* If DO is on, also append RFC6975 Algorithm data */
711
712 static const uint8_t rfc6975[] = {
713
714 0, 5, /* OPTION_CODE: DAU */
715 0, 6, /* LIST_LENGTH */
716 DNSSEC_ALGORITHM_RSASHA1,
717 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1,
718 DNSSEC_ALGORITHM_RSASHA256,
719 DNSSEC_ALGORITHM_RSASHA512,
720 DNSSEC_ALGORITHM_ECDSAP256SHA256,
721 DNSSEC_ALGORITHM_ECDSAP384SHA384,
722
723 0, 6, /* OPTION_CODE: DHU */
724 0, 3, /* LIST_LENGTH */
725 DNSSEC_DIGEST_SHA1,
726 DNSSEC_DIGEST_SHA256,
727 DNSSEC_DIGEST_SHA384,
728
729 0, 7, /* OPTION_CODE: N3U */
730 0, 1, /* LIST_LENGTH */
731 NSEC3_ALGORITHM_SHA1,
732 };
733
734 r = dns_packet_append_uint16(p, sizeof(rfc6975), NULL);
735 if (r < 0)
736 goto fail;
737
738 r = dns_packet_append_blob(p, rfc6975, sizeof(rfc6975), NULL);
739 } else
740 r = dns_packet_append_uint16(p, 0, NULL);
741
742 if (r < 0)
743 goto fail;
744
745 DNS_PACKET_HEADER(p)->arcount = htobe16(DNS_PACKET_ARCOUNT(p) + 1);
746
747 p->opt_start = saved_size;
748 p->opt_size = p->size - saved_size;
749
7035cd9e
MP
750 if (start)
751 *start = saved_size;
752
753 return 0;
4c89c718 754
7035cd9e
MP
755fail:
756 dns_packet_truncate(p, saved_size);
757 return r;
758}
759
4c89c718
MP
760int dns_packet_truncate_opt(DnsPacket *p) {
761 assert(p);
762
763 if (p->opt_start == (size_t) -1) {
764 assert(p->opt_size == (size_t) -1);
765 return 0;
766 }
767
768 assert(p->opt_size != (size_t) -1);
769 assert(DNS_PACKET_ARCOUNT(p) > 0);
770
771 if (p->opt_start + p->opt_size != p->size)
772 return -EBUSY;
773
774 dns_packet_truncate(p, p->opt_start);
775 DNS_PACKET_HEADER(p)->arcount = htobe16(DNS_PACKET_ARCOUNT(p) - 1);
776 p->opt_start = p->opt_size = (size_t) -1;
777
778 return 1;
779}
780
781int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start, size_t *rdata_start) {
782 size_t saved_size, rdlength_offset, end, rdlength, rds;
5eef597e
MP
783 int r;
784
785 assert(p);
786 assert(rr);
787
788 saved_size = p->size;
789
790 r = dns_packet_append_key(p, rr->key, NULL);
791 if (r < 0)
792 goto fail;
793
794 r = dns_packet_append_uint32(p, rr->ttl, NULL);
795 if (r < 0)
796 goto fail;
797
798 /* Initially we write 0 here */
799 r = dns_packet_append_uint16(p, 0, &rdlength_offset);
800 if (r < 0)
801 goto fail;
802
4c89c718
MP
803 rds = p->size - saved_size;
804
5eef597e
MP
805 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
806
807 case DNS_TYPE_SRV:
808 r = dns_packet_append_uint16(p, rr->srv.priority, NULL);
809 if (r < 0)
810 goto fail;
811
812 r = dns_packet_append_uint16(p, rr->srv.weight, NULL);
813 if (r < 0)
814 goto fail;
815
816 r = dns_packet_append_uint16(p, rr->srv.port, NULL);
817 if (r < 0)
818 goto fail;
819
4c89c718 820 r = dns_packet_append_name(p, rr->srv.name, true, false, NULL);
5eef597e
MP
821 break;
822
823 case DNS_TYPE_PTR:
824 case DNS_TYPE_NS:
825 case DNS_TYPE_CNAME:
826 case DNS_TYPE_DNAME:
4c89c718 827 r = dns_packet_append_name(p, rr->ptr.name, true, false, NULL);
5eef597e
MP
828 break;
829
830 case DNS_TYPE_HINFO:
831 r = dns_packet_append_string(p, rr->hinfo.cpu, NULL);
832 if (r < 0)
833 goto fail;
834
835 r = dns_packet_append_string(p, rr->hinfo.os, NULL);
836 break;
837
838 case DNS_TYPE_SPF: /* exactly the same as TXT */
4c89c718 839 case DNS_TYPE_TXT:
5eef597e 840
4c89c718 841 if (!rr->txt.items) {
f47781d8
MP
842 /* RFC 6763, section 6.1 suggests to generate
843 * single empty string for an empty array. */
844
4c89c718 845 r = dns_packet_append_raw_string(p, NULL, 0, NULL);
5eef597e
MP
846 if (r < 0)
847 goto fail;
f47781d8 848 } else {
4c89c718
MP
849 DnsTxtItem *i;
850
851 LIST_FOREACH(items, i, rr->txt.items) {
852 r = dns_packet_append_raw_string(p, i->data, i->length, NULL);
f47781d8
MP
853 if (r < 0)
854 goto fail;
855 }
5eef597e
MP
856 }
857
858 r = 0;
859 break;
5eef597e
MP
860
861 case DNS_TYPE_A:
862 r = dns_packet_append_blob(p, &rr->a.in_addr, sizeof(struct in_addr), NULL);
863 break;
864
865 case DNS_TYPE_AAAA:
866 r = dns_packet_append_blob(p, &rr->aaaa.in6_addr, sizeof(struct in6_addr), NULL);
867 break;
868
869 case DNS_TYPE_SOA:
4c89c718 870 r = dns_packet_append_name(p, rr->soa.mname, true, false, NULL);
5eef597e
MP
871 if (r < 0)
872 goto fail;
873
4c89c718 874 r = dns_packet_append_name(p, rr->soa.rname, true, false, NULL);
5eef597e
MP
875 if (r < 0)
876 goto fail;
877
878 r = dns_packet_append_uint32(p, rr->soa.serial, NULL);
879 if (r < 0)
880 goto fail;
881
882 r = dns_packet_append_uint32(p, rr->soa.refresh, NULL);
883 if (r < 0)
884 goto fail;
885
886 r = dns_packet_append_uint32(p, rr->soa.retry, NULL);
887 if (r < 0)
888 goto fail;
889
890 r = dns_packet_append_uint32(p, rr->soa.expire, NULL);
891 if (r < 0)
892 goto fail;
893
894 r = dns_packet_append_uint32(p, rr->soa.minimum, NULL);
895 break;
896
897 case DNS_TYPE_MX:
898 r = dns_packet_append_uint16(p, rr->mx.priority, NULL);
899 if (r < 0)
900 goto fail;
901
4c89c718 902 r = dns_packet_append_name(p, rr->mx.exchange, true, false, NULL);
5eef597e
MP
903 break;
904
905 case DNS_TYPE_LOC:
906 r = dns_packet_append_uint8(p, rr->loc.version, NULL);
907 if (r < 0)
908 goto fail;
909
910 r = dns_packet_append_uint8(p, rr->loc.size, NULL);
911 if (r < 0)
912 goto fail;
913
914 r = dns_packet_append_uint8(p, rr->loc.horiz_pre, NULL);
915 if (r < 0)
916 goto fail;
917
918 r = dns_packet_append_uint8(p, rr->loc.vert_pre, NULL);
919 if (r < 0)
920 goto fail;
921
922 r = dns_packet_append_uint32(p, rr->loc.latitude, NULL);
923 if (r < 0)
924 goto fail;
925
926 r = dns_packet_append_uint32(p, rr->loc.longitude, NULL);
927 if (r < 0)
928 goto fail;
929
930 r = dns_packet_append_uint32(p, rr->loc.altitude, NULL);
931 break;
932
7035cd9e
MP
933 case DNS_TYPE_DS:
934 r = dns_packet_append_uint16(p, rr->ds.key_tag, NULL);
935 if (r < 0)
936 goto fail;
937
938 r = dns_packet_append_uint8(p, rr->ds.algorithm, NULL);
939 if (r < 0)
940 goto fail;
941
942 r = dns_packet_append_uint8(p, rr->ds.digest_type, NULL);
943 if (r < 0)
944 goto fail;
945
946 r = dns_packet_append_blob(p, rr->ds.digest, rr->ds.digest_size, NULL);
947 break;
948
5eef597e
MP
949 case DNS_TYPE_SSHFP:
950 r = dns_packet_append_uint8(p, rr->sshfp.algorithm, NULL);
951 if (r < 0)
952 goto fail;
953
954 r = dns_packet_append_uint8(p, rr->sshfp.fptype, NULL);
955 if (r < 0)
956 goto fail;
957
7035cd9e 958 r = dns_packet_append_blob(p, rr->sshfp.fingerprint, rr->sshfp.fingerprint_size, NULL);
5eef597e
MP
959 break;
960
961 case DNS_TYPE_DNSKEY:
4c89c718 962 r = dns_packet_append_uint16(p, rr->dnskey.flags, NULL);
5eef597e
MP
963 if (r < 0)
964 goto fail;
965
4c89c718 966 r = dns_packet_append_uint8(p, rr->dnskey.protocol, NULL);
5eef597e
MP
967 if (r < 0)
968 goto fail;
969
970 r = dns_packet_append_uint8(p, rr->dnskey.algorithm, NULL);
971 if (r < 0)
972 goto fail;
973
974 r = dns_packet_append_blob(p, rr->dnskey.key, rr->dnskey.key_size, NULL);
975 break;
976
977 case DNS_TYPE_RRSIG:
978 r = dns_packet_append_uint16(p, rr->rrsig.type_covered, NULL);
979 if (r < 0)
980 goto fail;
981
982 r = dns_packet_append_uint8(p, rr->rrsig.algorithm, NULL);
983 if (r < 0)
984 goto fail;
985
986 r = dns_packet_append_uint8(p, rr->rrsig.labels, NULL);
987 if (r < 0)
988 goto fail;
989
990 r = dns_packet_append_uint32(p, rr->rrsig.original_ttl, NULL);
991 if (r < 0)
992 goto fail;
993
994 r = dns_packet_append_uint32(p, rr->rrsig.expiration, NULL);
995 if (r < 0)
996 goto fail;
997
998 r = dns_packet_append_uint32(p, rr->rrsig.inception, NULL);
999 if (r < 0)
1000 goto fail;
1001
7035cd9e 1002 r = dns_packet_append_uint16(p, rr->rrsig.key_tag, NULL);
5eef597e
MP
1003 if (r < 0)
1004 goto fail;
1005
4c89c718 1006 r = dns_packet_append_name(p, rr->rrsig.signer, false, true, NULL);
5eef597e
MP
1007 if (r < 0)
1008 goto fail;
1009
1010 r = dns_packet_append_blob(p, rr->rrsig.signature, rr->rrsig.signature_size, NULL);
1011 break;
1012
7035cd9e 1013 case DNS_TYPE_NSEC:
4c89c718 1014 r = dns_packet_append_name(p, rr->nsec.next_domain_name, false, false, NULL);
7035cd9e
MP
1015 if (r < 0)
1016 goto fail;
1017
1018 r = dns_packet_append_types(p, rr->nsec.types, NULL);
1019 if (r < 0)
1020 goto fail;
1021
1022 break;
4c89c718 1023
7035cd9e
MP
1024 case DNS_TYPE_NSEC3:
1025 r = dns_packet_append_uint8(p, rr->nsec3.algorithm, NULL);
1026 if (r < 0)
1027 goto fail;
1028
1029 r = dns_packet_append_uint8(p, rr->nsec3.flags, NULL);
1030 if (r < 0)
1031 goto fail;
1032
1033 r = dns_packet_append_uint16(p, rr->nsec3.iterations, NULL);
1034 if (r < 0)
1035 goto fail;
1036
1037 r = dns_packet_append_uint8(p, rr->nsec3.salt_size, NULL);
1038 if (r < 0)
1039 goto fail;
1040
1041 r = dns_packet_append_blob(p, rr->nsec3.salt, rr->nsec3.salt_size, NULL);
1042 if (r < 0)
1043 goto fail;
1044
1045 r = dns_packet_append_uint8(p, rr->nsec3.next_hashed_name_size, NULL);
1046 if (r < 0)
1047 goto fail;
1048
1049 r = dns_packet_append_blob(p, rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, NULL);
1050 if (r < 0)
1051 goto fail;
1052
1053 r = dns_packet_append_types(p, rr->nsec3.types, NULL);
1054 if (r < 0)
1055 goto fail;
1056
1057 break;
4c89c718
MP
1058
1059 case DNS_TYPE_TLSA:
1060 r = dns_packet_append_uint8(p, rr->tlsa.cert_usage, NULL);
1061 if (r < 0)
1062 goto fail;
1063
1064 r = dns_packet_append_uint8(p, rr->tlsa.selector, NULL);
1065 if (r < 0)
1066 goto fail;
1067
1068 r = dns_packet_append_uint8(p, rr->tlsa.matching_type, NULL);
1069 if (r < 0)
1070 goto fail;
1071
1072 r = dns_packet_append_blob(p, rr->tlsa.data, rr->tlsa.data_size, NULL);
1073 break;
1074
1075 case DNS_TYPE_OPT:
1076 case DNS_TYPE_OPENPGPKEY:
5eef597e
MP
1077 case _DNS_TYPE_INVALID: /* unparseable */
1078 default:
1079
4c89c718 1080 r = dns_packet_append_blob(p, rr->generic.data, rr->generic.data_size, NULL);
5eef597e
MP
1081 break;
1082 }
1083 if (r < 0)
1084 goto fail;
1085
1086 /* Let's calculate the actual data size and update the field */
1087 rdlength = p->size - rdlength_offset - sizeof(uint16_t);
1088 if (rdlength > 0xFFFF) {
4c89c718 1089 r = -ENOSPC;
5eef597e
MP
1090 goto fail;
1091 }
1092
1093 end = p->size;
1094 p->size = rdlength_offset;
1095 r = dns_packet_append_uint16(p, rdlength, NULL);
1096 if (r < 0)
1097 goto fail;
1098 p->size = end;
1099
1100 if (start)
1101 *start = saved_size;
1102
4c89c718
MP
1103 if (rdata_start)
1104 *rdata_start = rds;
1105
5eef597e
MP
1106 return 0;
1107
1108fail:
1109 dns_packet_truncate(p, saved_size);
1110 return r;
1111}
1112
5eef597e
MP
1113int dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start) {
1114 assert(p);
1115
1116 if (p->rindex + sz > p->size)
1117 return -EMSGSIZE;
1118
1119 if (ret)
1120 *ret = (uint8_t*) DNS_PACKET_DATA(p) + p->rindex;
1121
1122 if (start)
1123 *start = p->rindex;
1124
1125 p->rindex += sz;
1126 return 0;
1127}
1128
1129void dns_packet_rewind(DnsPacket *p, size_t idx) {
1130 assert(p);
1131 assert(idx <= p->size);
1132 assert(idx >= DNS_PACKET_HEADER_SIZE);
1133
1134 p->rindex = idx;
1135}
1136
1137int dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start) {
1138 const void *q;
1139 int r;
1140
1141 assert(p);
1142 assert(d);
1143
1144 r = dns_packet_read(p, sz, &q, start);
1145 if (r < 0)
1146 return r;
1147
1148 memcpy(d, q, sz);
1149 return 0;
1150}
1151
7035cd9e
MP
1152static int dns_packet_read_memdup(
1153 DnsPacket *p, size_t size,
1154 void **ret, size_t *ret_size,
1155 size_t *ret_start) {
1156
1157 const void *src;
1158 size_t start;
1159 int r;
1160
1161 assert(p);
1162 assert(ret);
1163
1164 r = dns_packet_read(p, size, &src, &start);
1165 if (r < 0)
1166 return r;
1167
1168 if (size <= 0)
1169 *ret = NULL;
1170 else {
1171 void *copy;
1172
1173 copy = memdup(src, size);
1174 if (!copy)
1175 return -ENOMEM;
1176
1177 *ret = copy;
1178 }
1179
1180 if (ret_size)
1181 *ret_size = size;
1182 if (ret_start)
1183 *ret_start = start;
1184
1185 return 0;
1186}
1187
5eef597e
MP
1188int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start) {
1189 const void *d;
1190 int r;
1191
1192 assert(p);
1193
1194 r = dns_packet_read(p, sizeof(uint8_t), &d, start);
1195 if (r < 0)
1196 return r;
1197
1198 *ret = ((uint8_t*) d)[0];
1199 return 0;
1200}
1201
1202int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start) {
1203 const void *d;
1204 int r;
1205
1206 assert(p);
1207
1208 r = dns_packet_read(p, sizeof(uint16_t), &d, start);
1209 if (r < 0)
1210 return r;
1211
f47781d8
MP
1212 *ret = unaligned_read_be16(d);
1213
5eef597e
MP
1214 return 0;
1215}
1216
1217int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start) {
1218 const void *d;
1219 int r;
1220
1221 assert(p);
1222
1223 r = dns_packet_read(p, sizeof(uint32_t), &d, start);
1224 if (r < 0)
1225 return r;
1226
f47781d8 1227 *ret = unaligned_read_be32(d);
5eef597e
MP
1228
1229 return 0;
1230}
1231
1232int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start) {
1233 size_t saved_rindex;
1234 const void *d;
1235 char *t;
1236 uint8_t c;
1237 int r;
1238
1239 assert(p);
1240
1241 saved_rindex = p->rindex;
1242
1243 r = dns_packet_read_uint8(p, &c, NULL);
1244 if (r < 0)
1245 goto fail;
1246
1247 r = dns_packet_read(p, c, &d, NULL);
1248 if (r < 0)
1249 goto fail;
1250
1251 if (memchr(d, 0, c)) {
1252 r = -EBADMSG;
1253 goto fail;
1254 }
1255
1256 t = strndup(d, c);
1257 if (!t) {
1258 r = -ENOMEM;
1259 goto fail;
1260 }
1261
1262 if (!utf8_is_valid(t)) {
1263 free(t);
1264 r = -EBADMSG;
1265 goto fail;
1266 }
1267
1268 *ret = t;
1269
1270 if (start)
1271 *start = saved_rindex;
1272
1273 return 0;
1274
1275fail:
1276 dns_packet_rewind(p, saved_rindex);
1277 return r;
1278}
1279
4c89c718
MP
1280int dns_packet_read_raw_string(DnsPacket *p, const void **ret, size_t *size, size_t *start) {
1281 size_t saved_rindex;
1282 uint8_t c;
1283 int r;
1284
1285 assert(p);
1286
1287 saved_rindex = p->rindex;
1288
1289 r = dns_packet_read_uint8(p, &c, NULL);
1290 if (r < 0)
1291 goto fail;
1292
1293 r = dns_packet_read(p, c, ret, NULL);
1294 if (r < 0)
1295 goto fail;
1296
1297 if (size)
1298 *size = c;
1299 if (start)
1300 *start = saved_rindex;
1301
1302 return 0;
1303
1304fail:
1305 dns_packet_rewind(p, saved_rindex);
1306 return r;
1307}
1308
13d276d0
MP
1309int dns_packet_read_name(
1310 DnsPacket *p,
1311 char **_ret,
1312 bool allow_compression,
1313 size_t *start) {
1314
f47781d8 1315 size_t saved_rindex, after_rindex = 0, jump_barrier;
5eef597e
MP
1316 _cleanup_free_ char *ret = NULL;
1317 size_t n = 0, allocated = 0;
1318 bool first = true;
1319 int r;
1320
1321 assert(p);
1322 assert(_ret);
1323
13d276d0
MP
1324 if (p->refuse_compression)
1325 allow_compression = false;
1326
5eef597e 1327 saved_rindex = p->rindex;
f47781d8 1328 jump_barrier = p->rindex;
5eef597e
MP
1329
1330 for (;;) {
1331 uint8_t c, d;
1332
1333 r = dns_packet_read_uint8(p, &c, NULL);
1334 if (r < 0)
1335 goto fail;
1336
1337 if (c == 0)
1338 /* End of name */
1339 break;
1340 else if (c <= 63) {
5eef597e
MP
1341 const char *label;
1342
1343 /* Literal label */
1344 r = dns_packet_read(p, c, (const void**) &label, NULL);
1345 if (r < 0)
1346 goto fail;
1347
4c89c718 1348 if (!GREEDY_REALLOC(ret, allocated, n + !first + DNS_LABEL_ESCAPED_MAX)) {
5eef597e
MP
1349 r = -ENOMEM;
1350 goto fail;
1351 }
1352
4c89c718 1353 if (first)
5eef597e 1354 first = false;
4c89c718
MP
1355 else
1356 ret[n++] = '.';
1357
1358 r = dns_label_escape(label, c, ret + n, DNS_LABEL_ESCAPED_MAX);
1359 if (r < 0)
1360 goto fail;
5eef597e 1361
5eef597e
MP
1362 n += r;
1363 continue;
1364 } else if (allow_compression && (c & 0xc0) == 0xc0) {
1365 uint16_t ptr;
1366
1367 /* Pointer */
1368 r = dns_packet_read_uint8(p, &d, NULL);
1369 if (r < 0)
1370 goto fail;
1371
1372 ptr = (uint16_t) (c & ~0xc0) << 8 | (uint16_t) d;
f47781d8 1373 if (ptr < DNS_PACKET_HEADER_SIZE || ptr >= jump_barrier) {
5eef597e
MP
1374 r = -EBADMSG;
1375 goto fail;
1376 }
1377
1378 if (after_rindex == 0)
1379 after_rindex = p->rindex;
1380
e735f4d4 1381 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
f47781d8 1382 jump_barrier = ptr;
5eef597e 1383 p->rindex = ptr;
f47781d8
MP
1384 } else {
1385 r = -EBADMSG;
5eef597e 1386 goto fail;
f47781d8 1387 }
5eef597e
MP
1388 }
1389
1390 if (!GREEDY_REALLOC(ret, allocated, n + 1)) {
1391 r = -ENOMEM;
1392 goto fail;
1393 }
1394
1395 ret[n] = 0;
1396
1397 if (after_rindex != 0)
1398 p->rindex= after_rindex;
1399
1400 *_ret = ret;
1401 ret = NULL;
1402
1403 if (start)
1404 *start = saved_rindex;
1405
1406 return 0;
1407
1408fail:
1409 dns_packet_rewind(p, saved_rindex);
1410 return r;
1411}
1412
7035cd9e
MP
1413static int dns_packet_read_type_window(DnsPacket *p, Bitmap **types, size_t *start) {
1414 uint8_t window;
1415 uint8_t length;
1416 const uint8_t *bitmap;
1417 uint8_t bit = 0;
1418 unsigned i;
1419 bool found = false;
1420 size_t saved_rindex;
1421 int r;
1422
1423 assert(p);
1424 assert(types);
1425
1426 saved_rindex = p->rindex;
1427
1428 r = bitmap_ensure_allocated(types);
1429 if (r < 0)
1430 goto fail;
1431
1432 r = dns_packet_read_uint8(p, &window, NULL);
1433 if (r < 0)
1434 goto fail;
1435
1436 r = dns_packet_read_uint8(p, &length, NULL);
1437 if (r < 0)
1438 goto fail;
1439
1440 if (length == 0 || length > 32)
1441 return -EBADMSG;
1442
1443 r = dns_packet_read(p, length, (const void **)&bitmap, NULL);
1444 if (r < 0)
1445 goto fail;
1446
1447 for (i = 0; i < length; i++) {
1448 uint8_t bitmask = 1 << 7;
1449
1450 if (!bitmap[i]) {
1451 found = false;
1452 bit += 8;
1453 continue;
1454 }
1455
1456 found = true;
1457
1458 while (bitmask) {
1459 if (bitmap[i] & bitmask) {
1460 uint16_t n;
1461
1462 n = (uint16_t) window << 8 | (uint16_t) bit;
1463
1464 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1465 if (dns_type_is_pseudo(n))
1466 continue;
1467
1468 r = bitmap_set(*types, n);
1469 if (r < 0)
1470 goto fail;
1471 }
1472
1473 bit ++;
1474 bitmask >>= 1;
1475 }
1476 }
1477
1478 if (!found)
1479 return -EBADMSG;
1480
1481 if (start)
1482 *start = saved_rindex;
1483
1484 return 0;
1485fail:
1486 dns_packet_rewind(p, saved_rindex);
1487 return r;
1488}
1489
1490static int dns_packet_read_type_windows(DnsPacket *p, Bitmap **types, size_t size, size_t *start) {
1491 size_t saved_rindex;
1492 int r;
1493
1494 saved_rindex = p->rindex;
1495
1496 while (p->rindex < saved_rindex + size) {
1497 r = dns_packet_read_type_window(p, types, NULL);
1498 if (r < 0)
1499 goto fail;
1500
1501 /* don't read past end of current RR */
1502 if (p->rindex > saved_rindex + size) {
1503 r = -EBADMSG;
1504 goto fail;
1505 }
1506 }
1507
1508 if (p->rindex != saved_rindex + size) {
1509 r = -EBADMSG;
1510 goto fail;
1511 }
1512
1513 if (start)
1514 *start = saved_rindex;
1515
1516 return 0;
1517fail:
1518 dns_packet_rewind(p, saved_rindex);
1519 return r;
1520}
1521
4c89c718 1522int dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, bool *ret_cache_flush, size_t *start) {
5eef597e 1523 _cleanup_free_ char *name = NULL;
4c89c718 1524 bool cache_flush = false;
5eef597e
MP
1525 uint16_t class, type;
1526 DnsResourceKey *key;
1527 size_t saved_rindex;
1528 int r;
1529
1530 assert(p);
1531 assert(ret);
1532
1533 saved_rindex = p->rindex;
1534
1535 r = dns_packet_read_name(p, &name, true, NULL);
1536 if (r < 0)
1537 goto fail;
1538
1539 r = dns_packet_read_uint16(p, &type, NULL);
1540 if (r < 0)
1541 goto fail;
1542
1543 r = dns_packet_read_uint16(p, &class, NULL);
1544 if (r < 0)
1545 goto fail;
1546
4c89c718
MP
1547 if (p->protocol == DNS_PROTOCOL_MDNS) {
1548 /* See RFC6762, Section 10.2 */
1549
1550 if (type != DNS_TYPE_OPT && (class & MDNS_RR_CACHE_FLUSH)) {
1551 class &= ~MDNS_RR_CACHE_FLUSH;
1552 cache_flush = true;
1553 }
1554 }
1555
5eef597e
MP
1556 key = dns_resource_key_new_consume(class, type, name);
1557 if (!key) {
1558 r = -ENOMEM;
1559 goto fail;
1560 }
1561
1562 name = NULL;
1563 *ret = key;
1564
4c89c718
MP
1565 if (ret_cache_flush)
1566 *ret_cache_flush = cache_flush;
5eef597e
MP
1567 if (start)
1568 *start = saved_rindex;
1569
1570 return 0;
1571fail:
1572 dns_packet_rewind(p, saved_rindex);
1573 return r;
1574}
1575
5eef597e
MP
1576static bool loc_size_ok(uint8_t size) {
1577 uint8_t m = size >> 4, e = size & 0xF;
1578
1579 return m <= 9 && e <= 9 && (m > 0 || e == 0);
1580}
1581
4c89c718 1582int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, bool *ret_cache_flush, size_t *start) {
5eef597e
MP
1583 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1584 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1585 size_t saved_rindex, offset;
1586 uint16_t rdlength;
4c89c718 1587 bool cache_flush;
5eef597e
MP
1588 int r;
1589
1590 assert(p);
1591 assert(ret);
1592
1593 saved_rindex = p->rindex;
1594
4c89c718 1595 r = dns_packet_read_key(p, &key, &cache_flush, NULL);
5eef597e
MP
1596 if (r < 0)
1597 goto fail;
1598
4c89c718
MP
1599 if (!dns_class_is_valid_rr(key->class)||
1600 !dns_type_is_valid_rr(key->type)) {
5eef597e
MP
1601 r = -EBADMSG;
1602 goto fail;
1603 }
1604
1605 rr = dns_resource_record_new(key);
1606 if (!rr) {
1607 r = -ENOMEM;
1608 goto fail;
1609 }
1610
1611 r = dns_packet_read_uint32(p, &rr->ttl, NULL);
1612 if (r < 0)
1613 goto fail;
1614
4c89c718
MP
1615 /* RFC 2181, Section 8, suggests to
1616 * treat a TTL with the MSB set as a zero TTL. */
1617 if (rr->ttl & UINT32_C(0x80000000))
1618 rr->ttl = 0;
1619
5eef597e
MP
1620 r = dns_packet_read_uint16(p, &rdlength, NULL);
1621 if (r < 0)
1622 goto fail;
1623
1624 if (p->rindex + rdlength > p->size) {
1625 r = -EBADMSG;
1626 goto fail;
1627 }
1628
1629 offset = p->rindex;
1630
1631 switch (rr->key->type) {
1632
1633 case DNS_TYPE_SRV:
1634 r = dns_packet_read_uint16(p, &rr->srv.priority, NULL);
1635 if (r < 0)
1636 goto fail;
1637 r = dns_packet_read_uint16(p, &rr->srv.weight, NULL);
1638 if (r < 0)
1639 goto fail;
1640 r = dns_packet_read_uint16(p, &rr->srv.port, NULL);
1641 if (r < 0)
1642 goto fail;
1643 r = dns_packet_read_name(p, &rr->srv.name, true, NULL);
1644 break;
1645
1646 case DNS_TYPE_PTR:
1647 case DNS_TYPE_NS:
1648 case DNS_TYPE_CNAME:
1649 case DNS_TYPE_DNAME:
1650 r = dns_packet_read_name(p, &rr->ptr.name, true, NULL);
1651 break;
1652
1653 case DNS_TYPE_HINFO:
1654 r = dns_packet_read_string(p, &rr->hinfo.cpu, NULL);
1655 if (r < 0)
1656 goto fail;
1657
1658 r = dns_packet_read_string(p, &rr->hinfo.os, NULL);
1659 break;
1660
1661 case DNS_TYPE_SPF: /* exactly the same as TXT */
f47781d8
MP
1662 case DNS_TYPE_TXT:
1663 if (rdlength <= 0) {
4c89c718 1664 DnsTxtItem *i;
f47781d8
MP
1665 /* RFC 6763, section 6.1 suggests to treat
1666 * empty TXT RRs as equivalent to a TXT record
1667 * with a single empty string. */
5eef597e 1668
4c89c718
MP
1669 i = malloc0(offsetof(DnsTxtItem, data) + 1); /* for safety reasons we add an extra NUL byte */
1670 if (!i)
1671 return -ENOMEM;
1672
1673 rr->txt.items = i;
f47781d8 1674 } else {
4c89c718
MP
1675 DnsTxtItem *last = NULL;
1676
f47781d8 1677 while (p->rindex < offset + rdlength) {
4c89c718
MP
1678 DnsTxtItem *i;
1679 const void *data;
1680 size_t sz;
5eef597e 1681
4c89c718 1682 r = dns_packet_read_raw_string(p, &data, &sz, NULL);
f47781d8 1683 if (r < 0)
4c89c718 1684 return r;
f47781d8 1685
4c89c718
MP
1686 i = malloc0(offsetof(DnsTxtItem, data) + sz + 1); /* extra NUL byte at the end */
1687 if (!i)
1688 return -ENOMEM;
1689
1690 memcpy(i->data, data, sz);
1691 i->length = sz;
1692
1693 LIST_INSERT_AFTER(items, rr->txt.items, last, i);
1694 last = i;
f47781d8 1695 }
5eef597e
MP
1696 }
1697
1698 r = 0;
1699 break;
5eef597e
MP
1700
1701 case DNS_TYPE_A:
1702 r = dns_packet_read_blob(p, &rr->a.in_addr, sizeof(struct in_addr), NULL);
1703 break;
1704
1705 case DNS_TYPE_AAAA:
1706 r = dns_packet_read_blob(p, &rr->aaaa.in6_addr, sizeof(struct in6_addr), NULL);
1707 break;
1708
1709 case DNS_TYPE_SOA:
1710 r = dns_packet_read_name(p, &rr->soa.mname, true, NULL);
1711 if (r < 0)
1712 goto fail;
1713
1714 r = dns_packet_read_name(p, &rr->soa.rname, true, NULL);
1715 if (r < 0)
1716 goto fail;
1717
1718 r = dns_packet_read_uint32(p, &rr->soa.serial, NULL);
1719 if (r < 0)
1720 goto fail;
1721
1722 r = dns_packet_read_uint32(p, &rr->soa.refresh, NULL);
1723 if (r < 0)
1724 goto fail;
1725
1726 r = dns_packet_read_uint32(p, &rr->soa.retry, NULL);
1727 if (r < 0)
1728 goto fail;
1729
1730 r = dns_packet_read_uint32(p, &rr->soa.expire, NULL);
1731 if (r < 0)
1732 goto fail;
1733
1734 r = dns_packet_read_uint32(p, &rr->soa.minimum, NULL);
1735 break;
1736
1737 case DNS_TYPE_MX:
1738 r = dns_packet_read_uint16(p, &rr->mx.priority, NULL);
1739 if (r < 0)
1740 goto fail;
1741
1742 r = dns_packet_read_name(p, &rr->mx.exchange, true, NULL);
1743 break;
1744
1745 case DNS_TYPE_LOC: {
1746 uint8_t t;
1747 size_t pos;
1748
1749 r = dns_packet_read_uint8(p, &t, &pos);
1750 if (r < 0)
1751 goto fail;
1752
1753 if (t == 0) {
1754 rr->loc.version = t;
1755
1756 r = dns_packet_read_uint8(p, &rr->loc.size, NULL);
1757 if (r < 0)
1758 goto fail;
1759
1760 if (!loc_size_ok(rr->loc.size)) {
1761 r = -EBADMSG;
1762 goto fail;
1763 }
1764
1765 r = dns_packet_read_uint8(p, &rr->loc.horiz_pre, NULL);
1766 if (r < 0)
1767 goto fail;
1768
1769 if (!loc_size_ok(rr->loc.horiz_pre)) {
1770 r = -EBADMSG;
1771 goto fail;
1772 }
1773
1774 r = dns_packet_read_uint8(p, &rr->loc.vert_pre, NULL);
1775 if (r < 0)
1776 goto fail;
1777
1778 if (!loc_size_ok(rr->loc.vert_pre)) {
1779 r = -EBADMSG;
1780 goto fail;
1781 }
1782
1783 r = dns_packet_read_uint32(p, &rr->loc.latitude, NULL);
1784 if (r < 0)
1785 goto fail;
1786
1787 r = dns_packet_read_uint32(p, &rr->loc.longitude, NULL);
1788 if (r < 0)
1789 goto fail;
1790
1791 r = dns_packet_read_uint32(p, &rr->loc.altitude, NULL);
1792 if (r < 0)
1793 goto fail;
1794
1795 break;
1796 } else {
1797 dns_packet_rewind(p, pos);
1798 rr->unparseable = true;
1799 goto unparseable;
1800 }
1801 }
1802
7035cd9e
MP
1803 case DNS_TYPE_DS:
1804 r = dns_packet_read_uint16(p, &rr->ds.key_tag, NULL);
1805 if (r < 0)
1806 goto fail;
1807
1808 r = dns_packet_read_uint8(p, &rr->ds.algorithm, NULL);
1809 if (r < 0)
1810 goto fail;
1811
1812 r = dns_packet_read_uint8(p, &rr->ds.digest_type, NULL);
1813 if (r < 0)
1814 goto fail;
1815
1816 r = dns_packet_read_memdup(p, rdlength - 4,
1817 &rr->ds.digest, &rr->ds.digest_size,
1818 NULL);
1819 if (r < 0)
1820 goto fail;
1821
1822 if (rr->ds.digest_size <= 0) {
1823 /* the accepted size depends on the algorithm, but for now
1824 just ensure that the value is greater than zero */
1825 r = -EBADMSG;
1826 goto fail;
1827 }
1828
1829 break;
4c89c718 1830
5eef597e
MP
1831 case DNS_TYPE_SSHFP:
1832 r = dns_packet_read_uint8(p, &rr->sshfp.algorithm, NULL);
1833 if (r < 0)
1834 goto fail;
1835
1836 r = dns_packet_read_uint8(p, &rr->sshfp.fptype, NULL);
1837 if (r < 0)
1838 goto fail;
1839
7035cd9e
MP
1840 r = dns_packet_read_memdup(p, rdlength - 2,
1841 &rr->sshfp.fingerprint, &rr->sshfp.fingerprint_size,
1842 NULL);
1843
1844 if (rr->sshfp.fingerprint_size <= 0) {
1845 /* the accepted size depends on the algorithm, but for now
1846 just ensure that the value is greater than zero */
1847 r = -EBADMSG;
1848 goto fail;
1849 }
1850
5eef597e
MP
1851 break;
1852
4c89c718
MP
1853 case DNS_TYPE_DNSKEY:
1854 r = dns_packet_read_uint16(p, &rr->dnskey.flags, NULL);
5eef597e
MP
1855 if (r < 0)
1856 goto fail;
1857
4c89c718 1858 r = dns_packet_read_uint8(p, &rr->dnskey.protocol, NULL);
5eef597e
MP
1859 if (r < 0)
1860 goto fail;
1861
5eef597e
MP
1862 r = dns_packet_read_uint8(p, &rr->dnskey.algorithm, NULL);
1863 if (r < 0)
1864 goto fail;
1865
7035cd9e
MP
1866 r = dns_packet_read_memdup(p, rdlength - 4,
1867 &rr->dnskey.key, &rr->dnskey.key_size,
1868 NULL);
1869
1870 if (rr->dnskey.key_size <= 0) {
1871 /* the accepted size depends on the algorithm, but for now
1872 just ensure that the value is greater than zero */
1873 r = -EBADMSG;
1874 goto fail;
1875 }
1876
5eef597e 1877 break;
5eef597e
MP
1878
1879 case DNS_TYPE_RRSIG:
1880 r = dns_packet_read_uint16(p, &rr->rrsig.type_covered, NULL);
1881 if (r < 0)
1882 goto fail;
1883
1884 r = dns_packet_read_uint8(p, &rr->rrsig.algorithm, NULL);
1885 if (r < 0)
1886 goto fail;
1887
1888 r = dns_packet_read_uint8(p, &rr->rrsig.labels, NULL);
1889 if (r < 0)
1890 goto fail;
1891
1892 r = dns_packet_read_uint32(p, &rr->rrsig.original_ttl, NULL);
1893 if (r < 0)
1894 goto fail;
1895
1896 r = dns_packet_read_uint32(p, &rr->rrsig.expiration, NULL);
1897 if (r < 0)
1898 goto fail;
1899
1900 r = dns_packet_read_uint32(p, &rr->rrsig.inception, NULL);
1901 if (r < 0)
1902 goto fail;
1903
1904 r = dns_packet_read_uint16(p, &rr->rrsig.key_tag, NULL);
1905 if (r < 0)
1906 goto fail;
1907
1908 r = dns_packet_read_name(p, &rr->rrsig.signer, false, NULL);
1909 if (r < 0)
1910 goto fail;
1911
7035cd9e
MP
1912 r = dns_packet_read_memdup(p, offset + rdlength - p->rindex,
1913 &rr->rrsig.signature, &rr->rrsig.signature_size,
1914 NULL);
1915
1916 if (rr->rrsig.signature_size <= 0) {
1917 /* the accepted size depends on the algorithm, but for now
1918 just ensure that the value is greater than zero */
1919 r = -EBADMSG;
1920 goto fail;
1921 }
1922
5eef597e
MP
1923 break;
1924
4c89c718
MP
1925 case DNS_TYPE_NSEC: {
1926
1927 /*
1928 * RFC6762, section 18.14 explictly states mDNS should use name compression.
1929 * This contradicts RFC3845, section 2.1.1
1930 */
1931
1932 bool allow_compressed = p->protocol == DNS_PROTOCOL_MDNS;
1933
1934 r = dns_packet_read_name(p, &rr->nsec.next_domain_name, allow_compressed, NULL);
5eef597e
MP
1935 if (r < 0)
1936 goto fail;
1937
7035cd9e
MP
1938 r = dns_packet_read_type_windows(p, &rr->nsec.types, offset + rdlength - p->rindex, NULL);
1939 if (r < 0)
1940 goto fail;
1941
4c89c718
MP
1942 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
1943 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
1944 * without the NSEC bit set. */
5eef597e 1945
7035cd9e 1946 break;
4c89c718 1947 }
7035cd9e
MP
1948 case DNS_TYPE_NSEC3: {
1949 uint8_t size;
1950
1951 r = dns_packet_read_uint8(p, &rr->nsec3.algorithm, NULL);
1952 if (r < 0)
1953 goto fail;
1954
1955 r = dns_packet_read_uint8(p, &rr->nsec3.flags, NULL);
1956 if (r < 0)
1957 goto fail;
1958
1959 r = dns_packet_read_uint16(p, &rr->nsec3.iterations, NULL);
1960 if (r < 0)
1961 goto fail;
1962
1963 /* this may be zero */
1964 r = dns_packet_read_uint8(p, &size, NULL);
1965 if (r < 0)
1966 goto fail;
1967
1968 r = dns_packet_read_memdup(p, size, &rr->nsec3.salt, &rr->nsec3.salt_size, NULL);
1969 if (r < 0)
1970 goto fail;
1971
1972 r = dns_packet_read_uint8(p, &size, NULL);
1973 if (r < 0)
1974 goto fail;
1975
1976 if (size <= 0) {
1977 r = -EBADMSG;
1978 goto fail;
1979 }
1980
1981 r = dns_packet_read_memdup(p, size, &rr->nsec3.next_hashed_name, &rr->nsec3.next_hashed_name_size, NULL);
1982 if (r < 0)
1983 goto fail;
1984
13d276d0 1985 r = dns_packet_read_type_windows(p, &rr->nsec3.types, offset + rdlength - p->rindex, NULL);
7035cd9e
MP
1986 if (r < 0)
1987 goto fail;
1988
1989 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
1990
1991 break;
1992 }
4c89c718
MP
1993
1994 case DNS_TYPE_TLSA:
1995 r = dns_packet_read_uint8(p, &rr->tlsa.cert_usage, NULL);
1996 if (r < 0)
1997 goto fail;
1998
1999 r = dns_packet_read_uint8(p, &rr->tlsa.selector, NULL);
2000 if (r < 0)
2001 goto fail;
2002
2003 r = dns_packet_read_uint8(p, &rr->tlsa.matching_type, NULL);
2004 if (r < 0)
2005 goto fail;
2006
2007 r = dns_packet_read_memdup(p, rdlength - 3,
2008 &rr->tlsa.data, &rr->tlsa.data_size,
2009 NULL);
2010 if (rr->tlsa.data_size <= 0) {
2011 /* the accepted size depends on the algorithm, but for now
2012 just ensure that the value is greater than zero */
2013 r = -EBADMSG;
2014 goto fail;
2015 }
2016
2017 break;
2018
2019 case DNS_TYPE_OPT: /* we only care about the header of OPT for now. */
2020 case DNS_TYPE_OPENPGPKEY:
7035cd9e
MP
2021 default:
2022 unparseable:
4c89c718 2023 r = dns_packet_read_memdup(p, rdlength, &rr->generic.data, &rr->generic.data_size, NULL);
7035cd9e
MP
2024 if (r < 0)
2025 goto fail;
5eef597e
MP
2026 break;
2027 }
2028 if (r < 0)
2029 goto fail;
2030 if (p->rindex != offset + rdlength) {
2031 r = -EBADMSG;
2032 goto fail;
2033 }
2034
2035 *ret = rr;
2036 rr = NULL;
2037
4c89c718
MP
2038 if (ret_cache_flush)
2039 *ret_cache_flush = cache_flush;
5eef597e
MP
2040 if (start)
2041 *start = saved_rindex;
2042
2043 return 0;
2044fail:
2045 dns_packet_rewind(p, saved_rindex);
2046 return r;
2047}
2048
4c89c718
MP
2049static bool opt_is_good(DnsResourceRecord *rr, bool *rfc6975) {
2050 const uint8_t* p;
2051 bool found_dau_dhu_n3u = false;
2052 size_t l;
2053
2054 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2055 * a reply). */
2056
2057 assert(rr);
2058 assert(rr->key->type == DNS_TYPE_OPT);
2059
2060 /* Check that the version is 0 */
2061 if (((rr->ttl >> 16) & UINT32_C(0xFF)) != 0)
2062 return false;
2063
2064 p = rr->opt.data;
2065 l = rr->opt.data_size;
2066 while (l > 0) {
2067 uint16_t option_code, option_length;
2068
2069 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2070 if (l < 4U)
2071 return false;
2072
2073 option_code = unaligned_read_be16(p);
2074 option_length = unaligned_read_be16(p + 2);
2075
2076 if (l < option_length + 4U)
2077 return false;
2078
2079 /* RFC 6975 DAU, DHU or N3U fields found. */
2080 if (IN_SET(option_code, 5, 6, 7))
2081 found_dau_dhu_n3u = true;
2082
2083 p += option_length + 4U;
2084 l -= option_length + 4U;
2085 }
2086
2087 *rfc6975 = found_dau_dhu_n3u;
2088 return true;
2089}
2090
5eef597e
MP
2091int dns_packet_extract(DnsPacket *p) {
2092 _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
2093 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
2094 size_t saved_rindex;
2095 unsigned n, i;
2096 int r;
2097
2098 if (p->extracted)
2099 return 0;
2100
2101 saved_rindex = p->rindex;
2102 dns_packet_rewind(p, DNS_PACKET_HEADER_SIZE);
2103
2104 n = DNS_PACKET_QDCOUNT(p);
2105 if (n > 0) {
2106 question = dns_question_new(n);
2107 if (!question) {
2108 r = -ENOMEM;
2109 goto finish;
2110 }
2111
2112 for (i = 0; i < n; i++) {
2113 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
4c89c718 2114 bool cache_flush;
5eef597e 2115
4c89c718 2116 r = dns_packet_read_key(p, &key, &cache_flush, NULL);
5eef597e
MP
2117 if (r < 0)
2118 goto finish;
2119
4c89c718
MP
2120 if (cache_flush) {
2121 r = -EBADMSG;
2122 goto finish;
2123 }
2124
2125 if (!dns_type_is_valid_query(key->type)) {
2126 r = -EBADMSG;
2127 goto finish;
2128 }
2129
5eef597e
MP
2130 r = dns_question_add(question, key);
2131 if (r < 0)
2132 goto finish;
2133 }
2134 }
2135
2136 n = DNS_PACKET_RRCOUNT(p);
2137 if (n > 0) {
4c89c718
MP
2138 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *previous = NULL;
2139 bool bad_opt = false;
2140
5eef597e
MP
2141 answer = dns_answer_new(n);
2142 if (!answer) {
2143 r = -ENOMEM;
2144 goto finish;
2145 }
2146
2147 for (i = 0; i < n; i++) {
2148 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
4c89c718 2149 bool cache_flush;
5eef597e 2150
4c89c718 2151 r = dns_packet_read_rr(p, &rr, &cache_flush, NULL);
5eef597e
MP
2152 if (r < 0)
2153 goto finish;
2154
4c89c718
MP
2155 /* Try to reduce memory usage a bit */
2156 if (previous)
2157 dns_resource_key_reduce(&rr->key, &previous->key);
2158
2159 if (rr->key->type == DNS_TYPE_OPT) {
2160 bool has_rfc6975;
2161
2162 if (p->opt || bad_opt) {
2163 /* Multiple OPT RRs? if so, let's ignore all, because there's something wrong
2164 * with the server, and if one is valid we wouldn't know which one. */
2165 log_debug("Multiple OPT RRs detected, ignoring all.");
2166 bad_opt = true;
2167 continue;
2168 }
2169
2170 if (!dns_name_is_root(DNS_RESOURCE_KEY_NAME(rr->key))) {
2171 /* If the OPT RR is not owned by the root domain, then it is bad, let's ignore
2172 * it. */
2173 log_debug("OPT RR is not owned by root domain, ignoring.");
2174 bad_opt = true;
2175 continue;
2176 }
2177
2178 if (i < DNS_PACKET_ANCOUNT(p) + DNS_PACKET_NSCOUNT(p)) {
2179 /* OPT RR is in the wrong section? Some Belkin routers do this. This is a hint
2180 * the EDNS implementation is borked, like the Belkin one is, hence ignore
2181 * it. */
2182 log_debug("OPT RR in wrong section, ignoring.");
2183 bad_opt = true;
2184 continue;
2185 }
2186
2187 if (!opt_is_good(rr, &has_rfc6975)) {
2188 log_debug("Malformed OPT RR, ignoring.");
2189 bad_opt = true;
2190 continue;
2191 }
2192
2193 if (has_rfc6975) {
2194 /* If the OPT RR contains RFC6975 algorithm data, then this is indication that
2195 * the server just copied the OPT it got from us (which contained that data)
2196 * back into the reply. If so, then it doesn't properly support EDNS, as
2197 * RFC6975 makes it very clear that the algorithm data should only be contained
2198 * in questions, never in replies. Crappy Belkin routers copy the OPT data for
2199 * example, hence let's detect this so that we downgrade early. */
2200 log_debug("OPT RR contained RFC6975 data, ignoring.");
2201 bad_opt = true;
2202 continue;
2203 }
2204
2205 p->opt = dns_resource_record_ref(rr);
2206 } else {
2207
2208 /* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
2209 * cached. Hence mark only those RRs as cacheable by default, but not the ones from the
2210 * Additional or Authority sections. */
2211
2212 r = dns_answer_add(answer, rr, p->ifindex,
2213 (i < DNS_PACKET_ANCOUNT(p) ? DNS_ANSWER_CACHEABLE : 0) |
2214 (p->protocol == DNS_PROTOCOL_MDNS && !cache_flush ? DNS_ANSWER_SHARED_OWNER : 0));
2215 if (r < 0)
2216 goto finish;
2217 }
2218
2219 /* Remember this RR, so that we potentically can merge it's ->key object with the next RR. Note
2220 * that we only do this if we actually decided to keep the RR around. */
2221 dns_resource_record_unref(previous);
2222 previous = dns_resource_record_ref(rr);
5eef597e 2223 }
4c89c718
MP
2224
2225 if (bad_opt)
2226 p->opt = dns_resource_record_unref(p->opt);
5eef597e
MP
2227 }
2228
2229 p->question = question;
2230 question = NULL;
2231
2232 p->answer = answer;
2233 answer = NULL;
2234
2235 p->extracted = true;
2236
2237 r = 0;
2238
2239finish:
2240 p->rindex = saved_rindex;
2241 return r;
2242}
2243
4c89c718
MP
2244int dns_packet_is_reply_for(DnsPacket *p, const DnsResourceKey *key) {
2245 int r;
2246
2247 assert(p);
2248 assert(key);
2249
2250 /* Checks if the specified packet is a reply for the specified
2251 * key and the specified key is the only one in the question
2252 * section. */
2253
2254 if (DNS_PACKET_QR(p) != 1)
2255 return 0;
2256
2257 /* Let's unpack the packet, if that hasn't happened yet. */
2258 r = dns_packet_extract(p);
2259 if (r < 0)
2260 return r;
2261
2262 if (p->question->n_keys != 1)
2263 return 0;
2264
2265 return dns_resource_key_equal(p->question->keys[0], key);
2266}
2267
5eef597e
MP
2268static const char* const dns_rcode_table[_DNS_RCODE_MAX_DEFINED] = {
2269 [DNS_RCODE_SUCCESS] = "SUCCESS",
2270 [DNS_RCODE_FORMERR] = "FORMERR",
2271 [DNS_RCODE_SERVFAIL] = "SERVFAIL",
2272 [DNS_RCODE_NXDOMAIN] = "NXDOMAIN",
2273 [DNS_RCODE_NOTIMP] = "NOTIMP",
2274 [DNS_RCODE_REFUSED] = "REFUSED",
2275 [DNS_RCODE_YXDOMAIN] = "YXDOMAIN",
2276 [DNS_RCODE_YXRRSET] = "YRRSET",
2277 [DNS_RCODE_NXRRSET] = "NXRRSET",
2278 [DNS_RCODE_NOTAUTH] = "NOTAUTH",
2279 [DNS_RCODE_NOTZONE] = "NOTZONE",
2280 [DNS_RCODE_BADVERS] = "BADVERS",
2281 [DNS_RCODE_BADKEY] = "BADKEY",
2282 [DNS_RCODE_BADTIME] = "BADTIME",
2283 [DNS_RCODE_BADMODE] = "BADMODE",
2284 [DNS_RCODE_BADNAME] = "BADNAME",
2285 [DNS_RCODE_BADALG] = "BADALG",
2286 [DNS_RCODE_BADTRUNC] = "BADTRUNC",
2287};
2288DEFINE_STRING_TABLE_LOOKUP(dns_rcode, int);
2289
2290static const char* const dns_protocol_table[_DNS_PROTOCOL_MAX] = {
2291 [DNS_PROTOCOL_DNS] = "dns",
2292 [DNS_PROTOCOL_MDNS] = "mdns",
2293 [DNS_PROTOCOL_LLMNR] = "llmnr",
2294};
2295DEFINE_STRING_TABLE_LOOKUP(dns_protocol, DnsProtocol);