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