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