1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include "resolved-dns-stub.h"
5 #include "socket-util.h"
7 /* The MTU of the loopback device is 64K on Linux, advertise that as maximum datagram size, but subtract the Ethernet,
8 * IP and UDP header sizes */
9 #define ADVERTISE_DATAGRAM_SIZE_MAX (65536U-14U-20U-8U)
11 static int manager_dns_stub_udp_fd(Manager
*m
);
12 static int manager_dns_stub_tcp_fd(Manager
*m
);
14 static int dns_stub_make_reply_packet(
19 bool *ret_truncated
) {
21 bool truncated
= false;
22 DnsResourceRecord
*rr
;
28 /* Note that we don't bother with any additional RRs, as this is stub is for local lookups only, and hence
29 * roundtrips aren't expensive. */
32 r
= dns_packet_new(p
, DNS_PROTOCOL_DNS
, 0, max_size
);
36 r
= dns_packet_append_question(*p
, q
);
40 DNS_PACKET_HEADER(*p
)->qdcount
= htobe16(dns_question_size(q
));
43 DNS_ANSWER_FOREACH(rr
, answer
) {
45 r
= dns_question_matches_rr(q
, rr
, NULL
);
51 r
= dns_question_matches_cname_or_dname(q
, rr
, NULL
);
59 r
= dns_packet_append_rr(*p
, rr
, 0, NULL
, NULL
);
71 *ret_truncated
= truncated
;
75 DNS_PACKET_HEADER(*p
)->ancount
= htobe16(be16toh(DNS_PACKET_HEADER(*p
)->ancount
) + c
);
80 static int dns_stub_finish_reply_packet(
84 bool tc
, /* set the Truncated bit? */
85 bool add_opt
, /* add an OPT RR to this packet? */
86 bool edns0_do
, /* set the EDNS0 DNSSEC OK bit? */
87 bool ad
) { /* set the DNSSEC authenticated data bit? */
94 /* If the client can't to EDNS0, don't do DO either */
97 /* If the client didn't do EDNS, clamp the rcode to 4 bit */
99 rcode
= DNS_RCODE_SERVFAIL
;
102 /* Don't set the AD bit unless DO is on, too */
106 DNS_PACKET_HEADER(p
)->id
= id
;
108 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
120 r
= dns_packet_append_opt(p
, ADVERTISE_DATAGRAM_SIZE_MAX
, edns0_do
, rcode
, NULL
);
128 static void dns_stub_detach_stream(DnsStream
*s
) {
136 static int dns_stub_send(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, DnsPacket
*reply
) {
144 r
= dns_stream_write_packet(s
, reply
);
148 fd
= manager_dns_stub_udp_fd(m
);
150 return log_debug_errno(fd
, "Failed to get reply socket: %m");
152 /* Note that it is essential here that we explicitly choose the source IP address for this packet. This
153 * is because otherwise the kernel will choose it automatically based on the routing table and will
154 * thus pick 127.0.0.1 rather than 127.0.0.53. */
156 r
= manager_send(m
, fd
, LOOPBACK_IFINDEX
, p
->family
, &p
->sender
, p
->sender_port
, &p
->destination
, reply
);
159 return log_debug_errno(r
, "Failed to send reply packet: %m");
164 static int dns_stub_send_failure(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, int rcode
, bool authenticated
) {
165 _cleanup_(dns_packet_unrefp
) DnsPacket
*reply
= NULL
;
171 r
= dns_stub_make_reply_packet(&reply
, DNS_PACKET_PAYLOAD_SIZE_MAX(p
), p
->question
, NULL
, NULL
);
173 return log_debug_errno(r
, "Failed to make failure packet: %m");
175 r
= dns_stub_finish_reply_packet(reply
, DNS_PACKET_ID(p
), rcode
, false, !!p
->opt
, DNS_PACKET_DO(p
), authenticated
);
177 return log_debug_errno(r
, "Failed to build failure packet: %m");
179 return dns_stub_send(m
, s
, p
, reply
);
182 static void dns_stub_query_complete(DnsQuery
*q
) {
186 assert(q
->request_dns_packet
);
190 case DNS_TRANSACTION_SUCCESS
: {
193 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
);
195 log_debug_errno(r
, "Failed to build reply packet: %m");
199 r
= dns_query_process_cname(q
);
201 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_SERVFAIL
, false);
205 log_debug_errno(r
, "Failed to process CNAME: %m");
208 if (r
== DNS_QUERY_RESTARTED
)
211 r
= dns_stub_finish_reply_packet(
213 DNS_PACKET_ID(q
->request_dns_packet
),
216 !!q
->request_dns_packet
->opt
,
217 DNS_PACKET_DO(q
->request_dns_packet
),
218 dns_query_fully_authenticated(q
));
220 log_debug_errno(r
, "Failed to finish reply packet: %m");
224 (void) dns_stub_send(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->reply_dns_packet
);
228 case DNS_TRANSACTION_RCODE_FAILURE
:
229 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->answer_rcode
, dns_query_fully_authenticated(q
));
232 case DNS_TRANSACTION_NOT_FOUND
:
233 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_NXDOMAIN
, dns_query_fully_authenticated(q
));
236 case DNS_TRANSACTION_TIMEOUT
:
237 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
238 /* Propagate a timeout as a no packet, i.e. that the client also gets a timeout */
241 case DNS_TRANSACTION_NO_SERVERS
:
242 case DNS_TRANSACTION_INVALID_REPLY
:
243 case DNS_TRANSACTION_ERRNO
:
244 case DNS_TRANSACTION_ABORTED
:
245 case DNS_TRANSACTION_DNSSEC_FAILED
:
246 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
247 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
248 case DNS_TRANSACTION_NETWORK_DOWN
:
249 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_SERVFAIL
, false);
252 case DNS_TRANSACTION_NULL
:
253 case DNS_TRANSACTION_PENDING
:
254 case DNS_TRANSACTION_VALIDATING
:
256 assert_not_reached("Impossible state");
259 /* If there's a packet to write set, let's leave the stream around */
260 if (q
->request_dns_stream
&& DNS_STREAM_QUEUED(q
->request_dns_stream
)) {
262 /* Detach the stream from our query (make it an orphan), but do not drop the reference to it. The
263 * default completion action of the stream will drop the reference. */
265 dns_stub_detach_stream(q
->request_dns_stream
);
266 q
->request_dns_stream
= NULL
;
272 static int dns_stub_stream_complete(DnsStream
*s
, int error
) {
275 log_debug_errno(error
, "DNS TCP connection terminated, destroying query: %m");
278 dns_query_free(s
->query
);
283 static void dns_stub_process_query(Manager
*m
, DnsStream
*s
, DnsPacket
*p
) {
289 assert(p
->protocol
== DNS_PROTOCOL_DNS
);
291 /* Takes ownership of the *s stream object */
293 if (in_addr_is_localhost(p
->family
, &p
->sender
) <= 0 ||
294 in_addr_is_localhost(p
->family
, &p
->destination
) <= 0) {
295 log_error("Got packet on unexpected IP range, refusing.");
296 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
300 r
= dns_packet_extract(p
);
302 log_debug_errno(r
, "Failed to extract resources from incoming packet, ignoring packet: %m");
303 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_FORMERR
, false);
307 if (!DNS_PACKET_VERSION_SUPPORTED(p
)) {
308 log_debug("Got EDNS OPT field with unsupported version number.");
309 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_BADVERS
, false);
313 if (dns_type_is_obsolete(p
->question
->keys
[0]->type
)) {
314 log_debug("Got message with obsolete key type, refusing.");
315 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
319 if (dns_type_is_zone_transer(p
->question
->keys
[0]->type
)) {
320 log_debug("Got request for zone transfer, refusing.");
321 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
325 if (!DNS_PACKET_RD(p
)) {
326 /* If the "rd" bit is off (i.e. recursion was not requested), then refuse operation */
327 log_debug("Got request with recursion disabled, refusing.");
328 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_REFUSED
, false);
332 if (DNS_PACKET_DO(p
) && DNS_PACKET_CD(p
)) {
333 log_debug("Got request with DNSSEC CD bit set, refusing.");
334 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
338 r
= dns_query_new(m
, &q
, p
->question
, p
->question
, 0, SD_RESOLVED_PROTOCOLS_ALL
|SD_RESOLVED_NO_SEARCH
);
340 log_error_errno(r
, "Failed to generate query object: %m");
341 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
345 /* Request that the TTL is corrected by the cached time for this lookup, so that we return vaguely useful TTLs */
348 q
->request_dns_packet
= dns_packet_ref(p
);
349 q
->request_dns_stream
= dns_stream_ref(s
); /* make sure the stream stays around until we can send a reply through it */
350 q
->complete
= dns_stub_query_complete
;
354 s
->complete
= dns_stub_stream_complete
;
360 log_error_errno(r
, "Failed to start query: %m");
361 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
365 log_debug("Processing query...");
369 if (s
&& DNS_STREAM_QUEUED(s
))
370 dns_stub_detach_stream(s
);
375 static int on_dns_stub_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
376 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
377 Manager
*m
= userdata
;
380 r
= manager_recv(m
, fd
, DNS_PROTOCOL_DNS
, &p
);
384 if (dns_packet_validate_query(p
) > 0) {
385 log_debug("Got DNS stub UDP query packet for id %u", DNS_PACKET_ID(p
));
387 dns_stub_process_query(m
, NULL
, p
);
389 log_debug("Invalid DNS stub UDP packet, ignoring.");
394 static int manager_dns_stub_udp_fd(Manager
*m
) {
395 static const int one
= 1;
396 union sockaddr_union sa
= {
397 .in
.sin_family
= AF_INET
,
398 .in
.sin_port
= htobe16(53),
399 .in
.sin_addr
.s_addr
= htobe32(INADDR_DNS_STUB
),
401 _cleanup_close_
int fd
= -1;
404 if (m
->dns_stub_udp_fd
>= 0)
405 return m
->dns_stub_udp_fd
;
407 fd
= socket(AF_INET
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
411 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
) < 0)
414 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &one
, sizeof one
) < 0)
417 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVTTL
, &one
, sizeof one
) < 0)
420 /* Make sure no traffic from outside the local host can leak to onto this socket */
421 if (setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, "lo", 3) < 0)
424 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
427 r
= sd_event_add_io(m
->event
, &m
->dns_stub_udp_event_source
, fd
, EPOLLIN
, on_dns_stub_packet
, m
);
431 (void) sd_event_source_set_description(m
->dns_stub_udp_event_source
, "dns-stub-udp");
433 return m
->dns_stub_udp_fd
= TAKE_FD(fd
);
436 static int on_dns_stub_stream_packet(DnsStream
*s
) {
438 assert(s
->read_packet
);
440 if (dns_packet_validate_query(s
->read_packet
) > 0) {
441 log_debug("Got DNS stub TCP query packet for id %u", DNS_PACKET_ID(s
->read_packet
));
443 dns_stub_process_query(s
->manager
, s
, s
->read_packet
);
445 log_debug("Invalid DNS stub TCP packet, ignoring.");
447 /* Drop the reference to the stream. Either a query was created and added its own reference to the stream now,
448 * or that didn't happen in which case we want to free the stream */
454 static int on_dns_stub_stream(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
456 Manager
*m
= userdata
;
459 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
461 if (IN_SET(errno
, EAGAIN
, EINTR
))
467 r
= dns_stream_new(m
, &stream
, DNS_PROTOCOL_DNS
, cfd
, NULL
);
473 stream
->on_packet
= on_dns_stub_stream_packet
;
475 /* We let the reference to the stream dangling here, it will either be dropped by the default "complete" action
476 * of the stream, or by our packet callback, or when the manager is shut down. */
481 static int manager_dns_stub_tcp_fd(Manager
*m
) {
482 static const int one
= 1;
483 union sockaddr_union sa
= {
484 .in
.sin_family
= AF_INET
,
485 .in
.sin_addr
.s_addr
= htobe32(INADDR_DNS_STUB
),
486 .in
.sin_port
= htobe16(53),
488 _cleanup_close_
int fd
= -1;
491 if (m
->dns_stub_tcp_fd
>= 0)
492 return m
->dns_stub_tcp_fd
;
494 fd
= socket(AF_INET
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
498 if (setsockopt(fd
, IPPROTO_IP
, IP_TTL
, &one
, sizeof one
) < 0)
501 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
) < 0)
504 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &one
, sizeof one
) < 0)
507 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVTTL
, &one
, sizeof one
) < 0)
510 /* Make sure no traffic from outside the local host can leak to onto this socket */
511 if (setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, "lo", 3) < 0)
514 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
517 if (listen(fd
, SOMAXCONN
) < 0)
520 r
= sd_event_add_io(m
->event
, &m
->dns_stub_tcp_event_source
, fd
, EPOLLIN
, on_dns_stub_stream
, m
);
524 (void) sd_event_source_set_description(m
->dns_stub_tcp_event_source
, "dns-stub-tcp");
526 return m
->dns_stub_tcp_fd
= TAKE_FD(fd
);
529 int manager_dns_stub_start(Manager
*m
) {
530 const char *t
= "UDP";
535 if (m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_NO
)
536 log_debug("Not creating stub listener.");
538 log_debug("Creating stub listener using %s.",
539 m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_UDP
? "UDP" :
540 m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_TCP
? "TCP" :
543 if (IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_UDP
))
544 r
= manager_dns_stub_udp_fd(m
);
547 IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_TCP
)) {
549 r
= manager_dns_stub_tcp_fd(m
);
552 if (IN_SET(r
, -EADDRINUSE
, -EPERM
)) {
553 if (r
== -EADDRINUSE
)
555 "Another process is already listening on %s socket 127.0.0.53:53.\n"
556 "Turning off local DNS stub support.", t
);
559 "Failed to listen on %s socket 127.0.0.53:53: %m.\n"
560 "Turning off local DNS stub support.", t
);
561 manager_dns_stub_stop(m
);
563 return log_error_errno(r
, "Failed to listen on %s socket 127.0.0.53:53: %m", t
);
568 void manager_dns_stub_stop(Manager
*m
) {
571 m
->dns_stub_udp_event_source
= sd_event_source_unref(m
->dns_stub_udp_event_source
);
572 m
->dns_stub_tcp_event_source
= sd_event_source_unref(m
->dns_stub_tcp_event_source
);
574 m
->dns_stub_udp_fd
= safe_close(m
->dns_stub_udp_fd
);
575 m
->dns_stub_tcp_fd
= safe_close(m
->dns_stub_tcp_fd
);