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