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