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