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