]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-stub.c
Merge pull request #18701 from bugaevc/mdns-unicast
[thirdparty/systemd.git] / src / resolve / resolved-dns-stub.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if_arp.h>
4 #include <netinet/tcp.h>
5
6 #include "errno-util.h"
7 #include "fd-util.h"
8 #include "missing_network.h"
9 #include "missing_socket.h"
10 #include "resolved-dns-stub.h"
11 #include "socket-netlink.h"
12 #include "socket-util.h"
13 #include "stdio-util.h"
14 #include "string-table.h"
15
16 /* The MTU of the loopback device is 64K on Linux, advertise that as maximum datagram size, but subtract the Ethernet,
17 * IP and UDP header sizes */
18 #define ADVERTISE_DATAGRAM_SIZE_MAX (65536U-14U-20U-8U)
19
20 /* On the extra stubs, use a more conservative choice */
21 #define ADVERTISE_EXTRA_DATAGRAM_SIZE_MAX DNS_PACKET_UNICAST_SIZE_LARGE_MAX
22
23 static int manager_dns_stub_fd_extra(Manager *m, DnsStubListenerExtra *l, int type);
24
25 static void dns_stub_listener_extra_hash_func(const DnsStubListenerExtra *a, struct siphash *state) {
26 assert(a);
27
28 siphash24_compress(&a->mode, sizeof(a->mode), state);
29 siphash24_compress(&a->family, sizeof(a->family), state);
30 siphash24_compress(&a->address, FAMILY_ADDRESS_SIZE(a->family), state);
31 siphash24_compress(&a->port, sizeof(a->port), state);
32 }
33
34 static int dns_stub_listener_extra_compare_func(const DnsStubListenerExtra *a, const DnsStubListenerExtra *b) {
35 int r;
36
37 assert(a);
38 assert(b);
39
40 r = CMP(a->mode, b->mode);
41 if (r != 0)
42 return r;
43
44 r = CMP(a->family, b->family);
45 if (r != 0)
46 return r;
47
48 r = memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family));
49 if (r != 0)
50 return r;
51
52 return CMP(a->port, b->port);
53 }
54
55 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
56 dns_stub_listener_extra_hash_ops,
57 DnsStubListenerExtra,
58 dns_stub_listener_extra_hash_func,
59 dns_stub_listener_extra_compare_func,
60 dns_stub_listener_extra_free);
61
62 int dns_stub_listener_extra_new(
63 Manager *m,
64 DnsStubListenerExtra **ret) {
65
66 DnsStubListenerExtra *l;
67
68 l = new(DnsStubListenerExtra, 1);
69 if (!l)
70 return -ENOMEM;
71
72 *l = (DnsStubListenerExtra) {
73 .manager = m,
74 };
75
76 *ret = TAKE_PTR(l);
77 return 0;
78 }
79
80 DnsStubListenerExtra *dns_stub_listener_extra_free(DnsStubListenerExtra *p) {
81 if (!p)
82 return NULL;
83
84 p->udp_event_source = sd_event_source_disable_unref(p->udp_event_source);
85 p->tcp_event_source = sd_event_source_disable_unref(p->tcp_event_source);
86
87 hashmap_free(p->queries_by_packet);
88
89 return mfree(p);
90 }
91
92 static void stub_packet_hash_func(const DnsPacket *p, struct siphash *state) {
93 assert(p);
94
95 siphash24_compress(&p->protocol, sizeof(p->protocol), state);
96 siphash24_compress(&p->family, sizeof(p->family), state);
97 siphash24_compress(&p->sender, sizeof(p->sender), state);
98 siphash24_compress(&p->ipproto, sizeof(p->ipproto), state);
99 siphash24_compress(&p->sender_port, sizeof(p->sender_port), state);
100 siphash24_compress(DNS_PACKET_HEADER(p), sizeof(DnsPacketHeader), state);
101
102 /* We don't bother hashing the full packet here, just the header */
103 }
104
105 static int stub_packet_compare_func(const DnsPacket *x, const DnsPacket *y) {
106 int r;
107
108 r = CMP(x->protocol, y->protocol);
109 if (r != 0)
110 return r;
111
112 r = CMP(x->family, y->family);
113 if (r != 0)
114 return r;
115
116 r = memcmp(&x->sender, &y->sender, sizeof(x->sender));
117 if (r != 0)
118 return r;
119
120 r = CMP(x->ipproto, y->ipproto);
121 if (r != 0)
122 return r;
123
124 r = CMP(x->sender_port, y->sender_port);
125 if (r != 0)
126 return r;
127
128 return memcmp(DNS_PACKET_HEADER(x), DNS_PACKET_HEADER(y), sizeof(DnsPacketHeader));
129 }
130
131 DEFINE_HASH_OPS(stub_packet_hash_ops, DnsPacket, stub_packet_hash_func, stub_packet_compare_func);
132
133 static int reply_add_with_rrsig(
134 DnsAnswer **reply,
135 DnsResourceRecord *rr,
136 int ifindex,
137 DnsAnswerFlags flags,
138 DnsResourceRecord *rrsig,
139 bool with_rrsig) {
140 int r;
141
142 assert(reply);
143 assert(rr);
144
145 r = dns_answer_add_extend(reply, rr, ifindex, flags, rrsig);
146 if (r < 0)
147 return r;
148
149 if (with_rrsig && rrsig) {
150 r = dns_answer_add_extend(reply, rrsig, ifindex, flags, NULL);
151 if (r < 0)
152 return r;
153 }
154
155 return 0;
156 }
157
158 static int dns_stub_collect_answer_by_question(
159 DnsAnswer **reply,
160 DnsAnswer *answer,
161 DnsQuestion *question,
162 bool with_rrsig) { /* Add RRSIG RR matching each RR */
163
164 DnsAnswerItem *item;
165 int r;
166
167 assert(reply);
168
169 /* Copies all RRs from 'answer' into 'reply', if they match 'question'. */
170
171 DNS_ANSWER_FOREACH_ITEM(item, answer) {
172
173 /* We have a question, let's see if this RR matches it */
174 r = dns_question_matches_rr(question, item->rr, NULL);
175 if (r < 0)
176 return r;
177 if (!r) {
178 /* Maybe there's a CNAME/DNAME in here? If so, that's an answer too */
179 r = dns_question_matches_cname_or_dname(question, item->rr, NULL);
180 if (r < 0)
181 return r;
182 if (!r)
183 continue;
184 }
185
186 /* Mask the section info, we want the primary answers to always go without section
187 * info, so that it is added to the answer section when we synthesize a reply. */
188
189 r = reply_add_with_rrsig(
190 reply,
191 item->rr,
192 item->ifindex,
193 item->flags & ~DNS_ANSWER_MASK_SECTIONS,
194 item->rrsig,
195 with_rrsig);
196 if (r < 0)
197 return r;
198 }
199
200 return 0;
201 }
202
203 static int dns_stub_collect_answer_by_section(
204 DnsAnswer **reply,
205 DnsAnswer *answer,
206 DnsAnswerFlags section,
207 DnsAnswer *exclude1,
208 DnsAnswer *exclude2,
209 bool with_dnssec) { /* Include DNSSEC RRs. RRSIG, NSEC, … */
210
211 DnsAnswerItem *item;
212 int r;
213
214 assert(reply);
215
216 /* Copies all RRs from 'answer' into 'reply', if they originate from the specified section. Also,
217 * avoid any RRs listed in 'exclude'. */
218
219 DNS_ANSWER_FOREACH_ITEM(item, answer) {
220
221 if (dns_answer_contains(exclude1, item->rr) ||
222 dns_answer_contains(exclude2, item->rr))
223 continue;
224
225 if (!with_dnssec &&
226 dns_type_is_dnssec(item->rr->key->type))
227 continue;
228
229 if (((item->flags ^ section) & DNS_ANSWER_MASK_SECTIONS) != 0)
230 continue;
231
232 r = reply_add_with_rrsig(
233 reply,
234 item->rr,
235 item->ifindex,
236 item->flags,
237 item->rrsig,
238 with_dnssec);
239 if (r < 0)
240 return r;
241 }
242
243 return 0;
244 }
245
246 static int dns_stub_assign_sections(
247 DnsQuery *q,
248 DnsQuestion *question,
249 bool edns0_do) {
250
251 int r;
252
253 assert(q);
254 assert(question);
255
256 /* Let's assign the 'answer' RRs we collected to their respective sections in the reply datagram. We
257 * try to reproduce a section assignment similar to what the upstream DNS server responded to us. We
258 * use the DNS_ANSWER_SECTION_xyz flags to match things up, which is where the original upstream's
259 * packet section assignment is stored in the DnsAnswer object. Not all RRs in the 'answer' objects
260 * come with section information though (for example, because they were synthesized locally, and not
261 * from a DNS packet). To deal with that we extend the assignment logic a bit: anything from the
262 * 'answer' object that directly matches the original question is always put in the ANSWER section,
263 * regardless if it carries section info, or what that section info says. Then, anything from the
264 * 'answer' objects that is from the ANSWER or AUTHORITY sections, and wasn't already added to the
265 * ANSWER section is placed in the AUTHORITY section. Everything else from either object is added to
266 * the ADDITIONAL section. */
267
268 /* Include all RRs that directly answer the question in the answer section */
269 r = dns_stub_collect_answer_by_question(
270 &q->reply_answer,
271 q->answer,
272 question,
273 edns0_do);
274 if (r < 0)
275 return r;
276
277 /* Include all RRs that originate from the authority sections, and aren't already listed in the
278 * answer section, in the authority section */
279 r = dns_stub_collect_answer_by_section(
280 &q->reply_authoritative,
281 q->answer,
282 DNS_ANSWER_SECTION_AUTHORITY,
283 q->reply_answer, NULL,
284 edns0_do);
285 if (r < 0)
286 return r;
287
288 /* Include all RRs that originate from the answer or additional sections in the additional section
289 * (except if already listed in the other two sections). Also add all RRs with no section marking. */
290 r = dns_stub_collect_answer_by_section(
291 &q->reply_additional,
292 q->answer,
293 DNS_ANSWER_SECTION_ANSWER,
294 q->reply_answer, q->reply_authoritative,
295 edns0_do);
296 if (r < 0)
297 return r;
298 r = dns_stub_collect_answer_by_section(
299 &q->reply_additional,
300 q->answer,
301 DNS_ANSWER_SECTION_ADDITIONAL,
302 q->reply_answer, q->reply_authoritative,
303 edns0_do);
304 if (r < 0)
305 return r;
306 r = dns_stub_collect_answer_by_section(
307 &q->reply_additional,
308 q->answer,
309 0,
310 q->reply_answer, q->reply_authoritative,
311 edns0_do);
312 if (r < 0)
313 return r;
314
315 return 0;
316 }
317
318 static int dns_stub_make_reply_packet(
319 DnsPacket **ret,
320 size_t max_size,
321 DnsQuestion *q,
322 bool *ret_truncated) {
323
324 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
325 bool tc = false;
326 int r;
327
328 assert(ret);
329
330 r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, max_size);
331 if (r < 0)
332 return r;
333
334 r = dns_packet_append_question(p, q);
335 if (r == -EMSGSIZE)
336 tc = true;
337 else if (r < 0)
338 return r;
339
340 if (ret_truncated)
341 *ret_truncated = tc;
342 else if (tc)
343 return -EMSGSIZE;
344
345 DNS_PACKET_HEADER(p)->qdcount = htobe16(dns_question_size(q));
346
347 *ret = TAKE_PTR(p);
348 return 0;
349 }
350
351 static int dns_stub_add_reply_packet_body(
352 DnsPacket *p,
353 DnsAnswer *answer,
354 DnsAnswer *authoritative,
355 DnsAnswer *additional,
356 bool edns0_do, /* Client expects DNSSEC RRs? */
357 bool *truncated) {
358
359 unsigned n_answer = 0, n_authoritative = 0, n_additional = 0;
360 bool tc = false;
361 int r;
362
363 assert(p);
364
365 /* Add the three sections to the packet. If the answer section doesn't fit we'll signal that as
366 * truncation. If the authoritative section doesn't fit and we are in DNSSEC mode, also signal
367 * truncation. In all other cases where things don't fit don't signal truncation, as for those cases
368 * the dropped RRs should not be essential. */
369
370 r = dns_packet_append_answer(p, answer, &n_answer);
371 if (r == -EMSGSIZE)
372 tc = true;
373 else if (r < 0)
374 return r;
375 else {
376 r = dns_packet_append_answer(p, authoritative, &n_authoritative);
377 if (r == -EMSGSIZE) {
378 if (edns0_do)
379 tc = true;
380 } else if (r < 0)
381 return r;
382 else {
383 r = dns_packet_append_answer(p, additional, &n_additional);
384 if (r < 0 && r != -EMSGSIZE)
385 return r;
386 }
387 }
388
389 if (tc) {
390 if (!truncated)
391 return -EMSGSIZE;
392
393 *truncated = true;
394 }
395
396 DNS_PACKET_HEADER(p)->ancount = htobe16(n_answer);
397 DNS_PACKET_HEADER(p)->nscount = htobe16(n_authoritative);
398 DNS_PACKET_HEADER(p)->arcount = htobe16(n_additional);
399 return 0;
400 }
401
402 static const char *nsid_string(void) {
403 static char buffer[SD_ID128_STRING_MAX + STRLEN(".resolved.systemd.io")] = "";
404 sd_id128_t id;
405 int r;
406
407 /* Let's generate a string that we can use as RFC5001 NSID identifier. The string shall identify us
408 * as systemd-resolved, and return a different string for each resolved instance without leaking host
409 * identity. Hence let's use a fixed suffix that identifies resolved, and a prefix generated from the
410 * machine ID but from which the machine ID cannot be determined.
411 *
412 * Clients can use this to determine whether an answer is originating locally or is proxied from
413 * upstream. */
414
415 if (!isempty(buffer))
416 return buffer;
417
418 r = sd_id128_get_machine_app_specific(
419 SD_ID128_MAKE(ed,d3,12,5d,16,b9,41,f9,a1,49,5f,ab,15,62,ab,27),
420 &id);
421 if (r < 0) {
422 log_debug_errno(r, "Failed to determine machine ID, ignoring: %m");
423 return NULL;
424 }
425
426 xsprintf(buffer, SD_ID128_FORMAT_STR ".resolved.systemd.io", SD_ID128_FORMAT_VAL(id));
427 return buffer;
428 }
429
430 static int dns_stub_finish_reply_packet(
431 DnsPacket *p,
432 uint16_t id,
433 int rcode,
434 bool tc, /* set the Truncated bit? */
435 bool aa, /* set the Authoritative Answer bit? */
436 bool rd, /* set the Recursion Desired bit? */
437 bool add_opt, /* add an OPT RR to this packet? */
438 bool edns0_do, /* set the EDNS0 DNSSEC OK bit? */
439 bool ad, /* set the DNSSEC authenticated data bit? */
440 bool cd, /* set the DNSSEC checking disabled bit? */
441 uint16_t max_udp_size, /* The maximum UDP datagram size to advertise to clients */
442 bool nsid) { /* whether to add NSID */
443
444 int r;
445
446 assert(p);
447
448 if (add_opt) {
449 r = dns_packet_append_opt(p, max_udp_size, edns0_do, /* include_rfc6975 = */ false, nsid ? nsid_string() : NULL, rcode, NULL);
450 if (r == -EMSGSIZE) /* Hit the size limit? then indicate truncation */
451 tc = true;
452 else if (r < 0)
453 return r;
454 } else {
455 /* If the client can't to EDNS0, don't do DO either */
456 edns0_do = false;
457
458 /* If we don't do EDNS, clamp the rcode to 4 bit */
459 if (rcode > 0xF)
460 rcode = DNS_RCODE_SERVFAIL;
461 }
462
463 /* Don't set the CD bit unless DO is on, too */
464 if (!edns0_do)
465 cd = false;
466
467 /* Note that we allow the AD bit to be set even if client didn't signal DO, as per RFC 6840, section
468 * 5.7 */
469
470 DNS_PACKET_HEADER(p)->id = id;
471
472 DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
473 1 /* qr */,
474 0 /* opcode */,
475 aa /* aa */,
476 tc /* tc */,
477 rd /* rd */,
478 1 /* ra */,
479 ad /* ad */,
480 cd /* cd */,
481 rcode));
482
483 return 0;
484 }
485
486 static int dns_stub_send(
487 Manager *m,
488 DnsStubListenerExtra *l,
489 DnsStream *s,
490 DnsPacket *p,
491 DnsPacket *reply) {
492
493 int r;
494
495 assert(m);
496 assert(p);
497 assert(reply);
498
499 if (s)
500 r = dns_stream_write_packet(s, reply);
501 else
502 /* Note that it is essential here that we explicitly choose the source IP address for this packet. This
503 * is because otherwise the kernel will choose it automatically based on the routing table and will
504 * thus pick 127.0.0.1 rather than 127.0.0.53. */
505 r = manager_send(m,
506 manager_dns_stub_fd_extra(m, l, SOCK_DGRAM),
507 l ? p->ifindex : LOOPBACK_IFINDEX, /* force loopback iface if this is the main listener stub */
508 p->family, &p->sender, p->sender_port, &p->destination,
509 reply);
510 if (r < 0)
511 return log_debug_errno(r, "Failed to send reply packet: %m");
512
513 return 0;
514 }
515
516 static int dns_stub_reply_with_edns0_do(DnsQuery *q) {
517 assert(q);
518
519 /* Reply with DNSSEC DO set? Only if client supports it; and we did any DNSSEC verification
520 * ourselves, or consider the data fully authenticated because we generated it locally, or the client
521 * set cd */
522
523 return DNS_PACKET_DO(q->request_packet) &&
524 (q->answer_dnssec_result >= 0 || /* we did proper DNSSEC validation … */
525 dns_query_fully_authenticated(q) || /* … or we considered it authentic otherwise … */
526 DNS_PACKET_CD(q->request_packet)); /* … or client set CD */
527 }
528
529 static void dns_stub_suppress_duplicate_section_rrs(DnsQuery *q) {
530 /* If we follow a CNAME/DNAME chain we might end up populating our sections with redundant RRs
531 * because we built up the sections from multiple reply packets (one from each CNAME/DNAME chain
532 * element). E.g. it could be that an RR that was included in the first reply's additional section
533 * ends up being relevant as main answer in a subsequent reply in the chain. Let's clean this up, and
534 * remove everything in the "higher priority" sections from the "lower priority" sections.
535 *
536 * Note that this removal matches by RR keys instead of the full RRs. This is because RRsets should
537 * always end up in one section fully or not at all, but never be split among sections.
538 *
539 * Specifically: we remove ANSWER section RRs from the AUTHORITATIVE and ADDITIONAL sections, as well
540 * as AUTHORITATIVE section RRs from the ADDITIONAL section. */
541
542 dns_answer_remove_by_answer_keys(&q->reply_authoritative, q->reply_answer);
543 dns_answer_remove_by_answer_keys(&q->reply_additional, q->reply_answer);
544 dns_answer_remove_by_answer_keys(&q->reply_additional, q->reply_authoritative);
545 }
546
547 static int dns_stub_send_reply(
548 DnsQuery *q,
549 int rcode) {
550
551 _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL;
552 bool truncated, edns0_do;
553 int r;
554
555 assert(q);
556
557 edns0_do = dns_stub_reply_with_edns0_do(q); /* let's check if we shall reply with EDNS0 DO? */
558
559 r = dns_stub_make_reply_packet(
560 &reply,
561 DNS_PACKET_PAYLOAD_SIZE_MAX(q->request_packet),
562 q->request_packet->question,
563 &truncated);
564 if (r < 0)
565 return log_debug_errno(r, "Failed to build reply packet: %m");
566
567 dns_stub_suppress_duplicate_section_rrs(q);
568
569 r = dns_stub_add_reply_packet_body(
570 reply,
571 q->reply_answer,
572 q->reply_authoritative,
573 q->reply_additional,
574 edns0_do,
575 &truncated);
576 if (r < 0)
577 return log_debug_errno(r, "Failed to append reply packet body: %m");
578
579 r = dns_stub_finish_reply_packet(
580 reply,
581 DNS_PACKET_ID(q->request_packet),
582 rcode,
583 truncated,
584 dns_query_fully_authoritative(q),
585 DNS_PACKET_RD(q->request_packet),
586 !!q->request_packet->opt,
587 edns0_do,
588 DNS_PACKET_AD(q->request_packet) && dns_query_fully_authenticated(q),
589 DNS_PACKET_CD(q->request_packet),
590 q->stub_listener_extra ? ADVERTISE_EXTRA_DATAGRAM_SIZE_MAX : ADVERTISE_DATAGRAM_SIZE_MAX,
591 dns_packet_has_nsid_request(q->request_packet) > 0 && !q->stub_listener_extra);
592 if (r < 0)
593 return log_debug_errno(r, "Failed to build failure packet: %m");
594
595 return dns_stub_send(q->manager, q->stub_listener_extra, q->request_stream, q->request_packet, reply);
596 }
597
598 static int dns_stub_send_failure(
599 Manager *m,
600 DnsStubListenerExtra *l,
601 DnsStream *s,
602 DnsPacket *p,
603 int rcode,
604 bool authenticated) {
605
606 _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL;
607 bool truncated;
608 int r;
609
610 assert(m);
611 assert(p);
612
613 r = dns_stub_make_reply_packet(
614 &reply,
615 DNS_PACKET_PAYLOAD_SIZE_MAX(p),
616 p->question,
617 &truncated);
618 if (r < 0)
619 return log_debug_errno(r, "Failed to make failure packet: %m");
620
621 r = dns_stub_finish_reply_packet(
622 reply,
623 DNS_PACKET_ID(p),
624 rcode,
625 truncated,
626 false,
627 DNS_PACKET_RD(p),
628 !!p->opt,
629 DNS_PACKET_DO(p),
630 DNS_PACKET_AD(p) && authenticated,
631 DNS_PACKET_CD(p),
632 l ? ADVERTISE_EXTRA_DATAGRAM_SIZE_MAX : ADVERTISE_DATAGRAM_SIZE_MAX,
633 dns_packet_has_nsid_request(p) > 0 && !l);
634 if (r < 0)
635 return log_debug_errno(r, "Failed to build failure packet: %m");
636
637 return dns_stub_send(m, l, s, p, reply);
638 }
639
640 static int dns_stub_patch_bypass_reply_packet(
641 DnsPacket **ret, /* Where to place the patched packet */
642 DnsPacket *original, /* The packet to patch */
643 DnsPacket *request) { /* The packet the patched packet shall look like a reply to */
644 _cleanup_(dns_packet_unrefp) DnsPacket *c = NULL;
645 int r;
646
647 assert(ret);
648 assert(original);
649 assert(request);
650
651 r = dns_packet_dup(&c, original);
652 if (r < 0)
653 return r;
654
655 /* Extract the packet, so that we know where the OPT field is */
656 r = dns_packet_extract(c);
657 if (r < 0)
658 return r;
659
660 /* Copy over the original client request ID, so that we can make the upstream query look like our own reply. */
661 DNS_PACKET_HEADER(c)->id = DNS_PACKET_HEADER(request)->id;
662
663 /* Patch in our own maximum datagram size, if EDNS0 was on */
664 r = dns_packet_patch_max_udp_size(c, ADVERTISE_DATAGRAM_SIZE_MAX);
665 if (r < 0)
666 return r;
667
668 /* Lower all TTLs by the time passed since we received the datagram. */
669 if (timestamp_is_set(original->timestamp)) {
670 r = dns_packet_patch_ttls(c, original->timestamp);
671 if (r < 0)
672 return r;
673 }
674
675 /* Our upstream connection might have supported larger DNS requests than our downstream one, hence
676 * set the TC bit if our reply is larger than what the client supports, and truncate. */
677 if (c->size > DNS_PACKET_PAYLOAD_SIZE_MAX(request)) {
678 log_debug("Artificially truncating stub response, as advertised size of client is smaller than upstream one.");
679 dns_packet_truncate(c, DNS_PACKET_PAYLOAD_SIZE_MAX(request));
680 DNS_PACKET_HEADER(c)->flags = htobe16(be16toh(DNS_PACKET_HEADER(c)->flags) | DNS_PACKET_FLAG_TC);
681 }
682
683 *ret = TAKE_PTR(c);
684 return 0;
685 }
686
687 static void dns_stub_query_complete(DnsQuery *q) {
688 int r;
689
690 assert(q);
691 assert(q->request_packet);
692
693 if (q->question_bypass) {
694 /* This is a bypass reply. If so, let's propagate the upstream packet, if we have it and it
695 * is regular DNS. (We can't do this if the upstream packet is LLMNR or mDNS, since the
696 * packets are not 100% compatible.) */
697
698 if (q->answer_full_packet &&
699 q->answer_full_packet->protocol == DNS_PROTOCOL_DNS) {
700 _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL;
701
702 r = dns_stub_patch_bypass_reply_packet(&reply, q->answer_full_packet, q->request_packet);
703 if (r < 0)
704 log_debug_errno(r, "Failed to patch bypass reply packet: %m");
705 else
706 (void) dns_stub_send(q->manager, q->stub_listener_extra, q->request_stream, q->request_packet, reply);
707
708 dns_query_free(q);
709 return;
710 }
711 }
712
713 /* Take all data from the current reply, and merge it into the three reply sections we are building
714 * up. We do this before processing CNAME redirects, so that we gradually build up our sections, and
715 * and keep adding all RRs in the CNAME chain. */
716 r = dns_stub_assign_sections(
717 q,
718 dns_query_question_for_protocol(q, DNS_PROTOCOL_DNS),
719 dns_stub_reply_with_edns0_do(q));
720 if (r < 0) {
721 log_debug_errno(r, "Failed to assign sections: %m");
722 dns_query_free(q);
723 return;
724 }
725
726 switch (q->state) {
727
728 case DNS_TRANSACTION_SUCCESS: {
729 bool first = true;
730
731 for (;;) {
732 int cname_result;
733
734 cname_result = dns_query_process_cname_one(q);
735 if (cname_result == -ELOOP) { /* CNAME loop, let's send what we already have */
736 log_debug_errno(r, "Detected CNAME loop, returning what we already have.");
737 (void) dns_stub_send_reply(q, q->answer_rcode);
738 break;
739 }
740 if (cname_result < 0) {
741 log_debug_errno(cname_result, "Failed to process CNAME: %m");
742 break;
743 }
744
745 if (cname_result == DNS_QUERY_NOMATCH) {
746 /* This answer doesn't contain any RR that would answer our question
747 * positively, i.e. neither directly nor via CNAME. */
748
749 if (first) /* We never followed a CNAME and the answer doesn't match our
750 * question at all? Then this is final, the empty answer is the
751 * answer. */
752 break;
753
754 /* Otherwise, we already followed a CNAME once within this packet, and the
755 * packet doesn't answer our question. In that case let's restart the query,
756 * now with the redirected question. We'll */
757 r = dns_query_go(q);
758 if (r < 0)
759 log_debug_errno(r, "Failed to restart query: %m");
760
761 return;
762 }
763
764 r = dns_stub_assign_sections(
765 q,
766 dns_query_question_for_protocol(q, DNS_PROTOCOL_DNS),
767 dns_stub_reply_with_edns0_do(q));
768 if (r < 0) {
769 log_debug_errno(r, "Failed to assign sections: %m");
770 dns_query_free(q);
771 return;
772 }
773
774 if (cname_result == DNS_QUERY_MATCH) /* A match? Then we are done, let's return what we got */
775 break;
776
777 /* We followed a CNAME. and collected the RRs that answer the redirected question
778 * successfully. Let's not try to do this again. */
779 assert(cname_result == DNS_QUERY_CNAME);
780 first = false;
781 }
782
783 _fallthrough_;
784 }
785
786 case DNS_TRANSACTION_RCODE_FAILURE:
787 (void) dns_stub_send_reply(q, q->answer_rcode);
788 break;
789
790 case DNS_TRANSACTION_NOT_FOUND:
791 (void) dns_stub_send_reply(q, DNS_RCODE_NXDOMAIN);
792 break;
793
794 case DNS_TRANSACTION_TIMEOUT:
795 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
796 /* Propagate a timeout as a no packet, i.e. that the client also gets a timeout */
797 break;
798
799 case DNS_TRANSACTION_NO_SERVERS:
800 case DNS_TRANSACTION_INVALID_REPLY:
801 case DNS_TRANSACTION_ERRNO:
802 case DNS_TRANSACTION_ABORTED:
803 case DNS_TRANSACTION_DNSSEC_FAILED:
804 case DNS_TRANSACTION_NO_TRUST_ANCHOR:
805 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
806 case DNS_TRANSACTION_NETWORK_DOWN:
807 case DNS_TRANSACTION_NO_SOURCE:
808 case DNS_TRANSACTION_STUB_LOOP:
809 (void) dns_stub_send_reply(q, DNS_RCODE_SERVFAIL);
810 break;
811
812 case DNS_TRANSACTION_NULL:
813 case DNS_TRANSACTION_PENDING:
814 case DNS_TRANSACTION_VALIDATING:
815 default:
816 assert_not_reached("Impossible state");
817 }
818
819 dns_query_free(q);
820 }
821
822 static int dns_stub_stream_complete(DnsStream *s, int error) {
823 assert(s);
824
825 log_debug_errno(error, "DNS TCP connection terminated, destroying queries: %m");
826
827 for (;;) {
828 DnsQuery *q;
829
830 q = set_first(s->queries);
831 if (!q)
832 break;
833
834 dns_query_free(q);
835 }
836
837 /* This drops the implicit ref we keep around since it was allocated, as incoming stub connections
838 * should be kept as long as the client wants to. */
839 dns_stream_unref(s);
840 return 0;
841 }
842
843 static void dns_stub_process_query(Manager *m, DnsStubListenerExtra *l, DnsStream *s, DnsPacket *p) {
844 _cleanup_(dns_query_freep) DnsQuery *q = NULL;
845 Hashmap **queries_by_packet;
846 DnsQuery *existing;
847 int r;
848
849 assert(m);
850 assert(p);
851 assert(p->protocol == DNS_PROTOCOL_DNS);
852
853 if (!l && /* l == NULL if this is the main stub */
854 (in_addr_is_localhost(p->family, &p->sender) <= 0 ||
855 in_addr_is_localhost(p->family, &p->destination) <= 0)) {
856 log_warning("Got packet on unexpected (i.e. non-localhost) IP range, ignoring.");
857 return;
858 }
859
860 if (manager_packet_from_our_transaction(m, p)) {
861 log_debug("Got our own packet looped back, ignoring.");
862 return;
863 }
864
865 queries_by_packet = l ? &l->queries_by_packet : &m->stub_queries_by_packet;
866 existing = hashmap_get(*queries_by_packet, p);
867 if (existing && dns_packet_equal(existing->request_packet, p)) {
868 log_debug("Got repeat packet from client, ignoring.");
869 return;
870 }
871
872 r = dns_packet_extract(p);
873 if (r < 0) {
874 log_debug_errno(r, "Failed to extract resources from incoming packet, ignoring packet: %m");
875 dns_stub_send_failure(m, l, s, p, DNS_RCODE_FORMERR, false);
876 return;
877 }
878
879 if (!DNS_PACKET_VERSION_SUPPORTED(p)) {
880 log_debug("Got EDNS OPT field with unsupported version number.");
881 dns_stub_send_failure(m, l, s, p, DNS_RCODE_BADVERS, false);
882 return;
883 }
884
885 if (dns_type_is_obsolete(dns_question_first_key(p->question)->type)) {
886 log_debug("Got message with obsolete key type, refusing.");
887 dns_stub_send_failure(m, l, s, p, DNS_RCODE_REFUSED, false);
888 return;
889 }
890
891 if (dns_type_is_zone_transer(dns_question_first_key(p->question)->type)) {
892 log_debug("Got request for zone transfer, refusing.");
893 dns_stub_send_failure(m, l, s, p, DNS_RCODE_REFUSED, false);
894 return;
895 }
896
897 if (!DNS_PACKET_RD(p)) {
898 /* If the "rd" bit is off (i.e. recursion was not requested), then refuse operation */
899 log_debug("Got request with recursion disabled, refusing.");
900 dns_stub_send_failure(m, l, s, p, DNS_RCODE_REFUSED, false);
901 return;
902 }
903
904 r = hashmap_ensure_allocated(queries_by_packet, &stub_packet_hash_ops);
905 if (r < 0) {
906 log_oom();
907 return;
908 }
909
910 if (DNS_PACKET_DO(p) && DNS_PACKET_CD(p)) {
911 log_debug("Got request with DNSSEC checking disabled, enabling bypass logic.");
912
913 r = dns_query_new(m, &q, NULL, NULL, p, 0,
914 SD_RESOLVED_PROTOCOLS_ALL|
915 SD_RESOLVED_NO_CNAME|
916 SD_RESOLVED_NO_SEARCH|
917 SD_RESOLVED_NO_VALIDATE|
918 SD_RESOLVED_REQUIRE_PRIMARY|
919 SD_RESOLVED_CLAMP_TTL);
920 } else
921 r = dns_query_new(m, &q, p->question, p->question, NULL, 0,
922 SD_RESOLVED_PROTOCOLS_ALL|
923 SD_RESOLVED_NO_SEARCH|
924 (DNS_PACKET_DO(p) ? SD_RESOLVED_REQUIRE_PRIMARY : 0)|
925 SD_RESOLVED_CLAMP_TTL);
926 if (r < 0) {
927 log_error_errno(r, "Failed to generate query object: %m");
928 dns_stub_send_failure(m, l, s, p, DNS_RCODE_SERVFAIL, false);
929 return;
930 }
931
932 q->request_packet = dns_packet_ref(p);
933 q->request_stream = dns_stream_ref(s); /* make sure the stream stays around until we can send a reply through it */
934 q->stub_listener_extra = l;
935 q->complete = dns_stub_query_complete;
936
937 if (s) {
938 /* Remember which queries belong to this stream, so that we can cancel them when the stream
939 * is disconnected early */
940
941 r = set_ensure_put(&s->queries, NULL, q);
942 if (r < 0) {
943 log_oom();
944 return;
945 }
946 assert(r > 0);
947 }
948
949 /* Add the query to the hash table we use to determine repeat packets now. We don't care about
950 * failures here, since in the worst case we'll not recognize duplicate incoming requests, which
951 * isn't particularly bad. */
952 (void) hashmap_put(*queries_by_packet, q->request_packet, q);
953
954 r = dns_query_go(q);
955 if (r < 0) {
956 log_error_errno(r, "Failed to start query: %m");
957 dns_stub_send_failure(m, l, s, p, DNS_RCODE_SERVFAIL, false);
958 return;
959 }
960
961 log_debug("Processing query...");
962 TAKE_PTR(q);
963 }
964
965 static int on_dns_stub_packet_internal(sd_event_source *s, int fd, uint32_t revents, Manager *m, DnsStubListenerExtra *l) {
966 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
967 int r;
968
969 r = manager_recv(m, fd, DNS_PROTOCOL_DNS, &p);
970 if (r <= 0)
971 return r;
972
973 if (dns_packet_validate_query(p) > 0) {
974 log_debug("Got DNS stub UDP query packet for id %u", DNS_PACKET_ID(p));
975
976 dns_stub_process_query(m, l, NULL, p);
977 } else
978 log_debug("Invalid DNS stub UDP packet, ignoring.");
979
980 return 0;
981 }
982
983 static int on_dns_stub_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
984 return on_dns_stub_packet_internal(s, fd, revents, userdata, NULL);
985 }
986
987 static int on_dns_stub_packet_extra(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
988 DnsStubListenerExtra *l = userdata;
989
990 assert(l);
991
992 return on_dns_stub_packet_internal(s, fd, revents, l->manager, l);
993 }
994
995 static int on_dns_stub_stream_packet(DnsStream *s) {
996 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
997
998 assert(s);
999
1000 p = dns_stream_take_read_packet(s);
1001 assert(p);
1002
1003 if (dns_packet_validate_query(p) > 0) {
1004 log_debug("Got DNS stub TCP query packet for id %u", DNS_PACKET_ID(p));
1005
1006 dns_stub_process_query(s->manager, s->stub_listener_extra, s, p);
1007 } else
1008 log_debug("Invalid DNS stub TCP packet, ignoring.");
1009
1010 return 0;
1011 }
1012
1013 static int on_dns_stub_stream_internal(sd_event_source *s, int fd, uint32_t revents, Manager *m, DnsStubListenerExtra *l) {
1014 DnsStream *stream;
1015 int cfd, r;
1016
1017 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
1018 if (cfd < 0) {
1019 if (ERRNO_IS_ACCEPT_AGAIN(errno))
1020 return 0;
1021
1022 return -errno;
1023 }
1024
1025 r = dns_stream_new(m, &stream, DNS_STREAM_STUB, DNS_PROTOCOL_DNS, cfd, NULL);
1026 if (r < 0) {
1027 safe_close(cfd);
1028 return r;
1029 }
1030
1031 stream->stub_listener_extra = l;
1032 stream->on_packet = on_dns_stub_stream_packet;
1033 stream->complete = dns_stub_stream_complete;
1034
1035 /* We let the reference to the stream dangle here, it will be dropped later by the complete callback. */
1036
1037 return 0;
1038 }
1039
1040 static int on_dns_stub_stream(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
1041 return on_dns_stub_stream_internal(s, fd, revents, userdata, NULL);
1042 }
1043
1044 static int on_dns_stub_stream_extra(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
1045 DnsStubListenerExtra *l = userdata;
1046
1047 assert(l);
1048 return on_dns_stub_stream_internal(s, fd, revents, l->manager, l);
1049 }
1050
1051 static int set_dns_stub_common_socket_options(int fd, int family) {
1052 int r;
1053
1054 assert(fd >= 0);
1055 assert(IN_SET(family, AF_INET, AF_INET6));
1056
1057 r = setsockopt_int(fd, SOL_SOCKET, SO_REUSEADDR, true);
1058 if (r < 0)
1059 return r;
1060
1061 r = socket_set_recvpktinfo(fd, family, true);
1062 if (r < 0)
1063 return r;
1064
1065 r = socket_set_recvttl(fd, family, true);
1066 if (r < 0)
1067 return r;
1068
1069 return 0;
1070 }
1071
1072 static int set_dns_stub_common_tcp_socket_options(int fd) {
1073 int r;
1074
1075 assert(fd >= 0);
1076
1077 r = setsockopt_int(fd, IPPROTO_TCP, TCP_FASTOPEN, 5); /* Everybody appears to pick qlen=5, let's do the same here. */
1078 if (r < 0)
1079 log_debug_errno(r, "Failed to enable TCP_FASTOPEN on TCP listening socket, ignoring: %m");
1080
1081 r = setsockopt_int(fd, IPPROTO_TCP, TCP_NODELAY, true);
1082 if (r < 0)
1083 log_debug_errno(r, "Failed to enable TCP_NODELAY mode, ignoring: %m");
1084
1085 return 0;
1086 }
1087
1088 static int manager_dns_stub_fd(Manager *m, int type) {
1089 union sockaddr_union sa = {
1090 .in.sin_family = AF_INET,
1091 .in.sin_addr.s_addr = htobe32(INADDR_DNS_STUB),
1092 .in.sin_port = htobe16(53),
1093 };
1094 _cleanup_close_ int fd = -1;
1095 int r;
1096
1097 assert(IN_SET(type, SOCK_DGRAM, SOCK_STREAM));
1098
1099 sd_event_source **event_source = type == SOCK_DGRAM ? &m->dns_stub_udp_event_source : &m->dns_stub_tcp_event_source;
1100 if (*event_source)
1101 return sd_event_source_get_io_fd(*event_source);
1102
1103 fd = socket(AF_INET, type | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
1104 if (fd < 0)
1105 return -errno;
1106
1107 r = set_dns_stub_common_socket_options(fd, AF_INET);
1108 if (r < 0)
1109 return r;
1110
1111 if (type == SOCK_STREAM) {
1112 r = set_dns_stub_common_tcp_socket_options(fd);
1113 if (r < 0)
1114 return r;
1115 }
1116
1117 /* Make sure no traffic from outside the local host can leak to onto this socket */
1118 r = socket_bind_to_ifindex(fd, LOOPBACK_IFINDEX);
1119 if (r < 0)
1120 return r;
1121
1122 r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, 1);
1123 if (r < 0)
1124 return r;
1125
1126 if (bind(fd, &sa.sa, sizeof(sa.in)) < 0)
1127 return -errno;
1128
1129 if (type == SOCK_STREAM &&
1130 listen(fd, SOMAXCONN) < 0)
1131 return -errno;
1132
1133 r = sd_event_add_io(m->event, event_source, fd, EPOLLIN,
1134 type == SOCK_DGRAM ? on_dns_stub_packet : on_dns_stub_stream,
1135 m);
1136 if (r < 0)
1137 return r;
1138
1139 r = sd_event_source_set_io_fd_own(*event_source, true);
1140 if (r < 0)
1141 return r;
1142
1143 (void) sd_event_source_set_description(*event_source,
1144 type == SOCK_DGRAM ? "dns-stub-udp" : "dns-stub-tcp");
1145
1146 return TAKE_FD(fd);
1147 }
1148
1149 static int manager_dns_stub_fd_extra(Manager *m, DnsStubListenerExtra *l, int type) {
1150 _cleanup_free_ char *pretty = NULL;
1151 _cleanup_close_ int fd = -1;
1152 union sockaddr_union sa;
1153 int r;
1154
1155 assert(m);
1156 assert(IN_SET(type, SOCK_DGRAM, SOCK_STREAM));
1157
1158 if (!l)
1159 return manager_dns_stub_fd(m, type);
1160
1161 sd_event_source **event_source = type == SOCK_DGRAM ? &l->udp_event_source : &l->tcp_event_source;
1162 if (*event_source)
1163 return sd_event_source_get_io_fd(*event_source);
1164
1165 if (l->family == AF_INET)
1166 sa = (union sockaddr_union) {
1167 .in.sin_family = l->family,
1168 .in.sin_port = htobe16(dns_stub_listener_extra_port(l)),
1169 .in.sin_addr = l->address.in,
1170 };
1171 else
1172 sa = (union sockaddr_union) {
1173 .in6.sin6_family = l->family,
1174 .in6.sin6_port = htobe16(dns_stub_listener_extra_port(l)),
1175 .in6.sin6_addr = l->address.in6,
1176 };
1177
1178 fd = socket(l->family, type | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
1179 if (fd < 0) {
1180 r = -errno;
1181 goto fail;
1182 }
1183
1184 r = set_dns_stub_common_socket_options(fd, l->family);
1185 if (r < 0)
1186 goto fail;
1187
1188 if (type == SOCK_STREAM) {
1189 r = set_dns_stub_common_tcp_socket_options(fd);
1190 if (r < 0)
1191 goto fail;
1192 }
1193
1194 /* Do not set IP_TTL for extra DNS stub listeners, as the address may not be local and in that case
1195 * people may want ttl > 1. */
1196
1197 r = socket_set_freebind(fd, l->family, true);
1198 if (r < 0)
1199 goto fail;
1200
1201 if (type == SOCK_DGRAM) {
1202 r = socket_disable_pmtud(fd, l->family);
1203 if (r < 0)
1204 log_debug_errno(r, "Failed to disable UDP PMTUD, ignoring: %m");
1205
1206 r = socket_set_recvfragsize(fd, l->family, true);
1207 if (r < 0)
1208 log_debug_errno(r, "Failed to enable fragment size reception, ignoring: %m");
1209 }
1210
1211 if (bind(fd, &sa.sa, SOCKADDR_LEN(sa)) < 0) {
1212 r = -errno;
1213 goto fail;
1214 }
1215
1216 if (type == SOCK_STREAM &&
1217 listen(fd, SOMAXCONN) < 0) {
1218 r = -errno;
1219 goto fail;
1220 }
1221
1222 r = sd_event_add_io(m->event, event_source, fd, EPOLLIN,
1223 type == SOCK_DGRAM ? on_dns_stub_packet_extra : on_dns_stub_stream_extra,
1224 l);
1225 if (r < 0)
1226 goto fail;
1227
1228 r = sd_event_source_set_io_fd_own(*event_source, true);
1229 if (r < 0)
1230 goto fail;
1231
1232 (void) sd_event_source_set_description(*event_source,
1233 type == SOCK_DGRAM ? "dns-stub-udp-extra" : "dns-stub-tcp-extra");
1234
1235 if (DEBUG_LOGGING) {
1236 (void) in_addr_port_to_string(l->family, &l->address, l->port, &pretty);
1237 log_debug("Listening on %s socket %s.",
1238 type == SOCK_DGRAM ? "UDP" : "TCP",
1239 strnull(pretty));
1240 }
1241
1242 return TAKE_FD(fd);
1243
1244 fail:
1245 assert(r < 0);
1246 (void) in_addr_port_to_string(l->family, &l->address, l->port, &pretty);
1247 return log_warning_errno(r,
1248 r == -EADDRINUSE ? "Another process is already listening on %s socket %s: %m" :
1249 "Failed to listen on %s socket %s: %m",
1250 type == SOCK_DGRAM ? "UDP" : "TCP",
1251 strnull(pretty));
1252 }
1253
1254 int manager_dns_stub_start(Manager *m) {
1255 const char *t = "UDP";
1256 int r = 0;
1257
1258 assert(m);
1259
1260 if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_NO)
1261 log_debug("Not creating stub listener.");
1262 else
1263 log_debug("Creating stub listener using %s.",
1264 m->dns_stub_listener_mode == DNS_STUB_LISTENER_UDP ? "UDP" :
1265 m->dns_stub_listener_mode == DNS_STUB_LISTENER_TCP ? "TCP" :
1266 "UDP/TCP");
1267
1268 if (FLAGS_SET(m->dns_stub_listener_mode, DNS_STUB_LISTENER_UDP))
1269 r = manager_dns_stub_fd(m, SOCK_DGRAM);
1270
1271 if (r >= 0 &&
1272 FLAGS_SET(m->dns_stub_listener_mode, DNS_STUB_LISTENER_TCP)) {
1273 t = "TCP";
1274 r = manager_dns_stub_fd(m, SOCK_STREAM);
1275 }
1276
1277 if (IN_SET(r, -EADDRINUSE, -EPERM)) {
1278 log_warning_errno(r,
1279 r == -EADDRINUSE ? "Another process is already listening on %s socket 127.0.0.53:53.\n"
1280 "Turning off local DNS stub support." :
1281 "Failed to listen on %s socket 127.0.0.53:53: %m.\n"
1282 "Turning off local DNS stub support.",
1283 t);
1284 manager_dns_stub_stop(m);
1285 } else if (r < 0)
1286 return log_error_errno(r, "Failed to listen on %s socket 127.0.0.53:53: %m", t);
1287
1288 if (!ordered_set_isempty(m->dns_extra_stub_listeners)) {
1289 DnsStubListenerExtra *l;
1290
1291 log_debug("Creating extra stub listeners.");
1292
1293 ORDERED_SET_FOREACH(l, m->dns_extra_stub_listeners) {
1294 if (FLAGS_SET(l->mode, DNS_STUB_LISTENER_UDP))
1295 (void) manager_dns_stub_fd_extra(m, l, SOCK_DGRAM);
1296 if (FLAGS_SET(l->mode, DNS_STUB_LISTENER_TCP))
1297 (void) manager_dns_stub_fd_extra(m, l, SOCK_STREAM);
1298 }
1299 }
1300
1301 return 0;
1302 }
1303
1304 void manager_dns_stub_stop(Manager *m) {
1305 assert(m);
1306
1307 m->dns_stub_udp_event_source = sd_event_source_disable_unref(m->dns_stub_udp_event_source);
1308 m->dns_stub_tcp_event_source = sd_event_source_disable_unref(m->dns_stub_tcp_event_source);
1309 }
1310
1311 static const char* const dns_stub_listener_mode_table[_DNS_STUB_LISTENER_MODE_MAX] = {
1312 [DNS_STUB_LISTENER_NO] = "no",
1313 [DNS_STUB_LISTENER_UDP] = "udp",
1314 [DNS_STUB_LISTENER_TCP] = "tcp",
1315 [DNS_STUB_LISTENER_YES] = "yes",
1316 };
1317 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dns_stub_listener_mode, DnsStubListenerMode, DNS_STUB_LISTENER_YES);