1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
9 #include "resolved-dns-stub.h"
10 #include "socket-util.h"
12 /* The MTU of the loopback device is 64K on Linux, advertise that as maximum datagram size, but subtract the Ethernet,
13 * IP and UDP header sizes */
14 #define ADVERTISE_DATAGRAM_SIZE_MAX (65536U-14U-20U-8U)
16 static int manager_dns_stub_udp_fd(Manager
*m
);
17 static int manager_dns_stub_tcp_fd(Manager
*m
);
19 static int dns_stub_make_reply_packet(
24 bool *ret_truncated
) {
26 bool truncated
= false;
27 DnsResourceRecord
*rr
;
33 /* Note that we don't bother with any additional RRs, as this is stub is for local lookups only, and hence
34 * roundtrips aren't expensive. */
37 r
= dns_packet_new(p
, DNS_PROTOCOL_DNS
, 0, max_size
);
41 r
= dns_packet_append_question(*p
, q
);
45 DNS_PACKET_HEADER(*p
)->qdcount
= htobe16(dns_question_size(q
));
48 DNS_ANSWER_FOREACH(rr
, answer
) {
50 r
= dns_question_matches_rr(q
, rr
, NULL
);
56 r
= dns_question_matches_cname_or_dname(q
, rr
, NULL
);
64 r
= dns_packet_append_rr(*p
, rr
, 0, NULL
, NULL
);
76 *ret_truncated
= truncated
;
80 DNS_PACKET_HEADER(*p
)->ancount
= htobe16(be16toh(DNS_PACKET_HEADER(*p
)->ancount
) + c
);
85 static int dns_stub_finish_reply_packet(
89 bool tc
, /* set the Truncated bit? */
90 bool add_opt
, /* add an OPT RR to this packet? */
91 bool edns0_do
, /* set the EDNS0 DNSSEC OK bit? */
92 bool ad
) { /* set the DNSSEC authenticated data bit? */
99 /* If the client can't to EDNS0, don't do DO either */
102 /* If the client didn't do EDNS, clamp the rcode to 4 bit */
104 rcode
= DNS_RCODE_SERVFAIL
;
107 /* Don't set the AD bit unless DO is on, too */
111 DNS_PACKET_HEADER(p
)->id
= id
;
113 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
125 r
= dns_packet_append_opt(p
, ADVERTISE_DATAGRAM_SIZE_MAX
, edns0_do
, rcode
, NULL
);
133 static void dns_stub_detach_stream(DnsStream
*s
) {
141 static int dns_stub_send(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, DnsPacket
*reply
) {
149 r
= dns_stream_write_packet(s
, reply
);
153 fd
= manager_dns_stub_udp_fd(m
);
155 return log_debug_errno(fd
, "Failed to get reply socket: %m");
157 /* Note that it is essential here that we explicitly choose the source IP address for this packet. This
158 * is because otherwise the kernel will choose it automatically based on the routing table and will
159 * thus pick 127.0.0.1 rather than 127.0.0.53. */
161 r
= manager_send(m
, fd
, LOOPBACK_IFINDEX
, p
->family
, &p
->sender
, p
->sender_port
, &p
->destination
, reply
);
164 return log_debug_errno(r
, "Failed to send reply packet: %m");
169 static int dns_stub_send_failure(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, int rcode
, bool authenticated
) {
170 _cleanup_(dns_packet_unrefp
) DnsPacket
*reply
= NULL
;
176 r
= dns_stub_make_reply_packet(&reply
, DNS_PACKET_PAYLOAD_SIZE_MAX(p
), p
->question
, NULL
, NULL
);
178 return log_debug_errno(r
, "Failed to make failure packet: %m");
180 r
= dns_stub_finish_reply_packet(reply
, DNS_PACKET_ID(p
), rcode
, false, !!p
->opt
, DNS_PACKET_DO(p
), authenticated
);
182 return log_debug_errno(r
, "Failed to build failure packet: %m");
184 return dns_stub_send(m
, s
, p
, reply
);
187 static void dns_stub_query_complete(DnsQuery
*q
) {
191 assert(q
->request_dns_packet
);
195 case DNS_TRANSACTION_SUCCESS
: {
198 r
= dns_stub_make_reply_packet(&q
->reply_dns_packet
, DNS_PACKET_PAYLOAD_SIZE_MAX(q
->request_dns_packet
), q
->question_idna
, q
->answer
, &truncated
);
200 log_debug_errno(r
, "Failed to build reply packet: %m");
204 r
= dns_query_process_cname(q
);
206 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_SERVFAIL
, false);
210 log_debug_errno(r
, "Failed to process CNAME: %m");
213 if (r
== DNS_QUERY_RESTARTED
)
216 r
= dns_stub_finish_reply_packet(
218 DNS_PACKET_ID(q
->request_dns_packet
),
221 !!q
->request_dns_packet
->opt
,
222 DNS_PACKET_DO(q
->request_dns_packet
),
223 dns_query_fully_authenticated(q
));
225 log_debug_errno(r
, "Failed to finish reply packet: %m");
229 (void) dns_stub_send(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->reply_dns_packet
);
233 case DNS_TRANSACTION_RCODE_FAILURE
:
234 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->answer_rcode
, dns_query_fully_authenticated(q
));
237 case DNS_TRANSACTION_NOT_FOUND
:
238 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_NXDOMAIN
, dns_query_fully_authenticated(q
));
241 case DNS_TRANSACTION_TIMEOUT
:
242 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
243 /* Propagate a timeout as a no packet, i.e. that the client also gets a timeout */
246 case DNS_TRANSACTION_NO_SERVERS
:
247 case DNS_TRANSACTION_INVALID_REPLY
:
248 case DNS_TRANSACTION_ERRNO
:
249 case DNS_TRANSACTION_ABORTED
:
250 case DNS_TRANSACTION_DNSSEC_FAILED
:
251 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
252 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
253 case DNS_TRANSACTION_NETWORK_DOWN
:
254 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_SERVFAIL
, false);
257 case DNS_TRANSACTION_NULL
:
258 case DNS_TRANSACTION_PENDING
:
259 case DNS_TRANSACTION_VALIDATING
:
261 assert_not_reached("Impossible state");
264 /* If there's a packet to write set, let's leave the stream around */
265 if (q
->request_dns_stream
&& DNS_STREAM_QUEUED(q
->request_dns_stream
)) {
267 /* Detach the stream from our query (make it an orphan), but do not drop the reference to it. The
268 * default completion action of the stream will drop the reference. */
270 dns_stub_detach_stream(q
->request_dns_stream
);
271 q
->request_dns_stream
= NULL
;
277 static int dns_stub_stream_complete(DnsStream
*s
, int error
) {
280 log_debug_errno(error
, "DNS TCP connection terminated, destroying query: %m");
283 dns_query_free(s
->query
);
288 static void dns_stub_process_query(Manager
*m
, DnsStream
*s
, DnsPacket
*p
) {
294 assert(p
->protocol
== DNS_PROTOCOL_DNS
);
296 /* Takes ownership of the *s stream object */
298 if (in_addr_is_localhost(p
->family
, &p
->sender
) <= 0 ||
299 in_addr_is_localhost(p
->family
, &p
->destination
) <= 0) {
300 log_error("Got packet on unexpected IP range, refusing.");
301 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
305 r
= dns_packet_extract(p
);
307 log_debug_errno(r
, "Failed to extract resources from incoming packet, ignoring packet: %m");
308 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_FORMERR
, false);
312 if (!DNS_PACKET_VERSION_SUPPORTED(p
)) {
313 log_debug("Got EDNS OPT field with unsupported version number.");
314 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_BADVERS
, false);
318 if (dns_type_is_obsolete(p
->question
->keys
[0]->type
)) {
319 log_debug("Got message with obsolete key type, refusing.");
320 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
324 if (dns_type_is_zone_transer(p
->question
->keys
[0]->type
)) {
325 log_debug("Got request for zone transfer, refusing.");
326 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
330 if (!DNS_PACKET_RD(p
)) {
331 /* If the "rd" bit is off (i.e. recursion was not requested), then refuse operation */
332 log_debug("Got request with recursion disabled, refusing.");
333 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_REFUSED
, false);
337 if (DNS_PACKET_DO(p
) && DNS_PACKET_CD(p
)) {
338 log_debug("Got request with DNSSEC CD bit set, refusing.");
339 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
343 r
= dns_query_new(m
, &q
, p
->question
, p
->question
, 0, SD_RESOLVED_PROTOCOLS_ALL
|SD_RESOLVED_NO_SEARCH
);
345 log_error_errno(r
, "Failed to generate query object: %m");
346 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
350 /* Request that the TTL is corrected by the cached time for this lookup, so that we return vaguely useful TTLs */
353 q
->request_dns_packet
= dns_packet_ref(p
);
354 q
->request_dns_stream
= dns_stream_ref(s
); /* make sure the stream stays around until we can send a reply through it */
355 q
->complete
= dns_stub_query_complete
;
359 s
->complete
= dns_stub_stream_complete
;
365 log_error_errno(r
, "Failed to start query: %m");
366 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
370 log_debug("Processing query...");
374 if (s
&& DNS_STREAM_QUEUED(s
))
375 dns_stub_detach_stream(s
);
380 static int on_dns_stub_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
381 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
382 Manager
*m
= userdata
;
385 r
= manager_recv(m
, fd
, DNS_PROTOCOL_DNS
, &p
);
389 if (dns_packet_validate_query(p
) > 0) {
390 log_debug("Got DNS stub UDP query packet for id %u", DNS_PACKET_ID(p
));
392 dns_stub_process_query(m
, NULL
, p
);
394 log_debug("Invalid DNS stub UDP packet, ignoring.");
399 static int manager_dns_stub_udp_fd(Manager
*m
) {
400 static const int one
= 1;
401 union sockaddr_union sa
= {
402 .in
.sin_family
= AF_INET
,
403 .in
.sin_port
= htobe16(53),
404 .in
.sin_addr
.s_addr
= htobe32(INADDR_DNS_STUB
),
406 _cleanup_close_
int fd
= -1;
409 if (m
->dns_stub_udp_fd
>= 0)
410 return m
->dns_stub_udp_fd
;
412 fd
= socket(AF_INET
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
416 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
) < 0)
419 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &one
, sizeof one
) < 0)
422 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVTTL
, &one
, sizeof one
) < 0)
425 /* Make sure no traffic from outside the local host can leak to onto this socket */
426 if (setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, "lo", 3) < 0)
429 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
432 r
= sd_event_add_io(m
->event
, &m
->dns_stub_udp_event_source
, fd
, EPOLLIN
, on_dns_stub_packet
, m
);
436 (void) sd_event_source_set_description(m
->dns_stub_udp_event_source
, "dns-stub-udp");
438 return m
->dns_stub_udp_fd
= TAKE_FD(fd
);
441 static int on_dns_stub_stream_packet(DnsStream
*s
) {
443 assert(s
->read_packet
);
445 if (dns_packet_validate_query(s
->read_packet
) > 0) {
446 log_debug("Got DNS stub TCP query packet for id %u", DNS_PACKET_ID(s
->read_packet
));
448 dns_stub_process_query(s
->manager
, s
, s
->read_packet
);
450 log_debug("Invalid DNS stub TCP packet, ignoring.");
452 /* Drop the reference to the stream. Either a query was created and added its own reference to the stream now,
453 * or that didn't happen in which case we want to free the stream */
459 static int on_dns_stub_stream(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
461 Manager
*m
= userdata
;
464 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
466 if (IN_SET(errno
, EAGAIN
, EINTR
))
472 r
= dns_stream_new(m
, &stream
, DNS_PROTOCOL_DNS
, cfd
);
478 stream
->on_packet
= on_dns_stub_stream_packet
;
480 /* We let the reference to the stream dangling here, it will either be dropped by the default "complete" action
481 * of the stream, or by our packet callback, or when the manager is shut down. */
486 static int manager_dns_stub_tcp_fd(Manager
*m
) {
487 static const int one
= 1;
488 union sockaddr_union sa
= {
489 .in
.sin_family
= AF_INET
,
490 .in
.sin_addr
.s_addr
= htobe32(INADDR_DNS_STUB
),
491 .in
.sin_port
= htobe16(53),
493 _cleanup_close_
int fd
= -1;
496 if (m
->dns_stub_tcp_fd
>= 0)
497 return m
->dns_stub_tcp_fd
;
499 fd
= socket(AF_INET
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
503 if (setsockopt(fd
, IPPROTO_IP
, IP_TTL
, &one
, sizeof one
) < 0)
506 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
) < 0)
509 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &one
, sizeof one
) < 0)
512 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVTTL
, &one
, sizeof one
) < 0)
515 /* Make sure no traffic from outside the local host can leak to onto this socket */
516 if (setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, "lo", 3) < 0)
519 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
522 if (listen(fd
, SOMAXCONN
) < 0)
525 r
= sd_event_add_io(m
->event
, &m
->dns_stub_tcp_event_source
, fd
, EPOLLIN
, on_dns_stub_stream
, m
);
529 (void) sd_event_source_set_description(m
->dns_stub_tcp_event_source
, "dns-stub-tcp");
531 return m
->dns_stub_tcp_fd
= TAKE_FD(fd
);
534 int manager_dns_stub_start(Manager
*m
) {
535 const char *t
= "UDP";
540 if (m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_NO
)
541 log_debug("Not creating stub listener.");
543 log_debug("Creating stub listener using %s.",
544 m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_UDP
? "UDP" :
545 m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_TCP
? "TCP" :
548 if (IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_UDP
))
549 r
= manager_dns_stub_udp_fd(m
);
552 IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_TCP
)) {
554 r
= manager_dns_stub_tcp_fd(m
);
557 if (IN_SET(r
, -EADDRINUSE
, -EPERM
)) {
558 if (r
== -EADDRINUSE
)
560 "Another process is already listening on %s socket 127.0.0.53:53.\n"
561 "Turning off local DNS stub support.", t
);
564 "Failed to listen on %s socket 127.0.0.53:53: %m.\n"
565 "Turning off local DNS stub support.", t
);
566 manager_dns_stub_stop(m
);
568 return log_error_errno(r
, "Failed to listen on %s socket 127.0.0.53:53: %m", t
);
573 void manager_dns_stub_stop(Manager
*m
) {
576 m
->dns_stub_udp_event_source
= sd_event_source_unref(m
->dns_stub_udp_event_source
);
577 m
->dns_stub_tcp_event_source
= sd_event_source_unref(m
->dns_stub_tcp_event_source
);
579 m
->dns_stub_udp_fd
= safe_close(m
->dns_stub_udp_fd
);
580 m
->dns_stub_tcp_fd
= safe_close(m
->dns_stub_tcp_fd
);