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