1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include "missing_network.h"
5 #include "resolved-dns-stub.h"
6 #include "socket-util.h"
8 /* The MTU of the loopback device is 64K on Linux, advertise that as maximum datagram size, but subtract the Ethernet,
9 * IP and UDP header sizes */
10 #define ADVERTISE_DATAGRAM_SIZE_MAX (65536U-14U-20U-8U)
12 static int manager_dns_stub_udp_fd(Manager
*m
);
13 static int manager_dns_stub_tcp_fd(Manager
*m
);
15 static int dns_stub_make_reply_packet(
20 bool *ret_truncated
) {
22 bool truncated
= false;
23 DnsResourceRecord
*rr
;
29 /* Note that we don't bother with any additional RRs, as this is stub is for local lookups only, and hence
30 * roundtrips aren't expensive. */
33 r
= dns_packet_new(p
, DNS_PROTOCOL_DNS
, 0, max_size
);
37 r
= dns_packet_append_question(*p
, q
);
41 DNS_PACKET_HEADER(*p
)->qdcount
= htobe16(dns_question_size(q
));
44 DNS_ANSWER_FOREACH(rr
, answer
) {
46 r
= dns_question_matches_rr(q
, rr
, NULL
);
52 r
= dns_question_matches_cname_or_dname(q
, rr
, NULL
);
60 r
= dns_packet_append_rr(*p
, rr
, 0, NULL
, NULL
);
72 *ret_truncated
= truncated
;
76 DNS_PACKET_HEADER(*p
)->ancount
= htobe16(be16toh(DNS_PACKET_HEADER(*p
)->ancount
) + c
);
81 static int dns_stub_finish_reply_packet(
85 bool tc
, /* set the Truncated bit? */
86 bool add_opt
, /* add an OPT RR to this packet? */
87 bool edns0_do
, /* set the EDNS0 DNSSEC OK bit? */
88 bool ad
) { /* set the DNSSEC authenticated data bit? */
95 r
= dns_packet_append_opt(p
, ADVERTISE_DATAGRAM_SIZE_MAX
, edns0_do
, rcode
, NULL
);
96 if (r
== -EMSGSIZE
) /* Hit the size limit? then indicate truncation */
102 /* If the client can't to EDNS0, don't do DO either */
105 /* If the client didn't do EDNS, clamp the rcode to 4 bit */
107 rcode
= DNS_RCODE_SERVFAIL
;
110 /* Don't set the AD bit unless DO is on, too */
114 DNS_PACKET_HEADER(p
)->id
= id
;
116 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
130 static int dns_stub_send(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, DnsPacket
*reply
) {
138 r
= dns_stream_write_packet(s
, reply
);
142 fd
= manager_dns_stub_udp_fd(m
);
144 return log_debug_errno(fd
, "Failed to get reply socket: %m");
146 /* Note that it is essential here that we explicitly choose the source IP address for this packet. This
147 * is because otherwise the kernel will choose it automatically based on the routing table and will
148 * thus pick 127.0.0.1 rather than 127.0.0.53. */
150 r
= manager_send(m
, fd
, LOOPBACK_IFINDEX
, p
->family
, &p
->sender
, p
->sender_port
, &p
->destination
, reply
);
153 return log_debug_errno(r
, "Failed to send reply packet: %m");
158 static int dns_stub_send_failure(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, int rcode
, bool authenticated
) {
159 _cleanup_(dns_packet_unrefp
) DnsPacket
*reply
= NULL
;
165 r
= dns_stub_make_reply_packet(&reply
, DNS_PACKET_PAYLOAD_SIZE_MAX(p
), p
->question
, NULL
, NULL
);
167 return log_debug_errno(r
, "Failed to make failure packet: %m");
169 r
= dns_stub_finish_reply_packet(reply
, DNS_PACKET_ID(p
), rcode
, false, !!p
->opt
, DNS_PACKET_DO(p
), authenticated
);
171 return log_debug_errno(r
, "Failed to build failure packet: %m");
173 return dns_stub_send(m
, s
, p
, reply
);
176 static void dns_stub_query_complete(DnsQuery
*q
) {
180 assert(q
->request_dns_packet
);
184 case DNS_TRANSACTION_SUCCESS
: {
187 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
);
189 log_debug_errno(r
, "Failed to build reply packet: %m");
194 r
= dns_query_process_cname(q
);
196 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_SERVFAIL
, false);
200 log_debug_errno(r
, "Failed to process CNAME: %m");
203 if (r
== DNS_QUERY_RESTARTED
)
207 r
= dns_stub_finish_reply_packet(
209 DNS_PACKET_ID(q
->request_dns_packet
),
212 !!q
->request_dns_packet
->opt
,
213 DNS_PACKET_DO(q
->request_dns_packet
),
214 dns_query_fully_authenticated(q
));
216 log_debug_errno(r
, "Failed to finish reply packet: %m");
220 (void) dns_stub_send(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->reply_dns_packet
);
224 case DNS_TRANSACTION_RCODE_FAILURE
:
225 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->answer_rcode
, dns_query_fully_authenticated(q
));
228 case DNS_TRANSACTION_NOT_FOUND
:
229 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_NXDOMAIN
, dns_query_fully_authenticated(q
));
232 case DNS_TRANSACTION_TIMEOUT
:
233 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
234 /* Propagate a timeout as a no packet, i.e. that the client also gets a timeout */
237 case DNS_TRANSACTION_NO_SERVERS
:
238 case DNS_TRANSACTION_INVALID_REPLY
:
239 case DNS_TRANSACTION_ERRNO
:
240 case DNS_TRANSACTION_ABORTED
:
241 case DNS_TRANSACTION_DNSSEC_FAILED
:
242 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
243 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
244 case DNS_TRANSACTION_NETWORK_DOWN
:
245 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_SERVFAIL
, false);
248 case DNS_TRANSACTION_NULL
:
249 case DNS_TRANSACTION_PENDING
:
250 case DNS_TRANSACTION_VALIDATING
:
252 assert_not_reached("Impossible state");
258 static int dns_stub_stream_complete(DnsStream
*s
, int error
) {
261 log_debug_errno(error
, "DNS TCP connection terminated, destroying queries: %m");
266 q
= set_first(s
->queries
);
273 /* This drops the implicit ref we keep around since it was allocated, as incoming stub connections
274 * should be kept as long as the client wants to. */
279 static void dns_stub_process_query(Manager
*m
, DnsStream
*s
, DnsPacket
*p
) {
285 assert(p
->protocol
== DNS_PROTOCOL_DNS
);
287 if (in_addr_is_localhost(p
->family
, &p
->sender
) <= 0 ||
288 in_addr_is_localhost(p
->family
, &p
->destination
) <= 0) {
289 log_error("Got packet on unexpected IP range, refusing.");
290 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
294 r
= dns_packet_extract(p
);
296 log_debug_errno(r
, "Failed to extract resources from incoming packet, ignoring packet: %m");
297 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_FORMERR
, false);
301 if (!DNS_PACKET_VERSION_SUPPORTED(p
)) {
302 log_debug("Got EDNS OPT field with unsupported version number.");
303 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_BADVERS
, false);
307 if (dns_type_is_obsolete(p
->question
->keys
[0]->type
)) {
308 log_debug("Got message with obsolete key type, refusing.");
309 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
313 if (dns_type_is_zone_transer(p
->question
->keys
[0]->type
)) {
314 log_debug("Got request for zone transfer, refusing.");
315 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
319 if (!DNS_PACKET_RD(p
)) {
320 /* If the "rd" bit is off (i.e. recursion was not requested), then refuse operation */
321 log_debug("Got request with recursion disabled, refusing.");
322 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_REFUSED
, false);
326 if (DNS_PACKET_DO(p
) && DNS_PACKET_CD(p
)) {
327 log_debug("Got request with DNSSEC CD bit set, refusing.");
328 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
, false);
332 r
= dns_query_new(m
, &q
, p
->question
, p
->question
, 0, SD_RESOLVED_PROTOCOLS_ALL
|SD_RESOLVED_NO_SEARCH
);
334 log_error_errno(r
, "Failed to generate query object: %m");
335 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
339 /* Request that the TTL is corrected by the cached time for this lookup, so that we return vaguely useful TTLs */
342 q
->request_dns_packet
= dns_packet_ref(p
);
343 q
->request_dns_stream
= dns_stream_ref(s
); /* make sure the stream stays around until we can send a reply through it */
344 q
->complete
= dns_stub_query_complete
;
347 /* Remember which queries belong to this stream, so that we can cancel them when the stream
348 * is disconnected early */
350 r
= set_ensure_allocated(&s
->queries
, &trivial_hash_ops
);
356 if (set_put(s
->queries
, q
) < 0) {
364 log_error_errno(r
, "Failed to start query: %m");
365 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
, false);
369 log_debug("Processing query...");
376 static int on_dns_stub_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
377 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
378 Manager
*m
= userdata
;
381 r
= manager_recv(m
, fd
, DNS_PROTOCOL_DNS
, &p
);
385 if (dns_packet_validate_query(p
) > 0) {
386 log_debug("Got DNS stub UDP query packet for id %u", DNS_PACKET_ID(p
));
388 dns_stub_process_query(m
, NULL
, p
);
390 log_debug("Invalid DNS stub UDP packet, ignoring.");
395 static int manager_dns_stub_udp_fd(Manager
*m
) {
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 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_REUSEADDR
, true);
415 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_PKTINFO
, true);
419 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_RECVTTL
, true);
423 /* Make sure no traffic from outside the local host can leak to onto this socket */
424 r
= socket_bind_to_ifindex(fd
, LOOPBACK_IFINDEX
);
428 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
431 r
= sd_event_add_io(m
->event
, &m
->dns_stub_udp_event_source
, fd
, EPOLLIN
, on_dns_stub_packet
, m
);
435 (void) sd_event_source_set_description(m
->dns_stub_udp_event_source
, "dns-stub-udp");
437 return m
->dns_stub_udp_fd
= TAKE_FD(fd
);
440 static int on_dns_stub_stream_packet(DnsStream
*s
) {
441 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
445 p
= dns_stream_take_read_packet(s
);
448 if (dns_packet_validate_query(p
) > 0) {
449 log_debug("Got DNS stub TCP query packet for id %u", DNS_PACKET_ID(p
));
451 dns_stub_process_query(s
->manager
, s
, p
);
453 log_debug("Invalid DNS stub TCP packet, ignoring.");
458 static int on_dns_stub_stream(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
460 Manager
*m
= userdata
;
463 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
465 if (IN_SET(errno
, EAGAIN
, EINTR
))
471 r
= dns_stream_new(m
, &stream
, DNS_STREAM_STUB
, DNS_PROTOCOL_DNS
, cfd
, NULL
);
477 stream
->on_packet
= on_dns_stub_stream_packet
;
478 stream
->complete
= dns_stub_stream_complete
;
480 /* We let the reference to the stream dangle here, it will be dropped later by the complete callback. */
485 static int manager_dns_stub_tcp_fd(Manager
*m
) {
486 union sockaddr_union sa
= {
487 .in
.sin_family
= AF_INET
,
488 .in
.sin_addr
.s_addr
= htobe32(INADDR_DNS_STUB
),
489 .in
.sin_port
= htobe16(53),
491 _cleanup_close_
int fd
= -1;
494 if (m
->dns_stub_tcp_fd
>= 0)
495 return m
->dns_stub_tcp_fd
;
497 fd
= socket(AF_INET
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
501 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_TTL
, true);
505 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_REUSEADDR
, true);
509 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_PKTINFO
, true);
513 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_RECVTTL
, true);
517 /* Make sure no traffic from outside the local host can leak to onto this socket */
518 r
= socket_bind_to_ifindex(fd
, LOOPBACK_IFINDEX
);
522 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
525 if (listen(fd
, SOMAXCONN
) < 0)
528 r
= sd_event_add_io(m
->event
, &m
->dns_stub_tcp_event_source
, fd
, EPOLLIN
, on_dns_stub_stream
, m
);
532 (void) sd_event_source_set_description(m
->dns_stub_tcp_event_source
, "dns-stub-tcp");
534 return m
->dns_stub_tcp_fd
= TAKE_FD(fd
);
537 int manager_dns_stub_start(Manager
*m
) {
538 const char *t
= "UDP";
543 if (m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_NO
)
544 log_debug("Not creating stub listener.");
546 log_debug("Creating stub listener using %s.",
547 m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_UDP
? "UDP" :
548 m
->dns_stub_listener_mode
== DNS_STUB_LISTENER_TCP
? "TCP" :
551 if (IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_UDP
))
552 r
= manager_dns_stub_udp_fd(m
);
555 IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_TCP
)) {
557 r
= manager_dns_stub_tcp_fd(m
);
560 if (IN_SET(r
, -EADDRINUSE
, -EPERM
)) {
561 if (r
== -EADDRINUSE
)
563 "Another process is already listening on %s socket 127.0.0.53:53.\n"
564 "Turning off local DNS stub support.", t
);
567 "Failed to listen on %s socket 127.0.0.53:53: %m.\n"
568 "Turning off local DNS stub support.", t
);
569 manager_dns_stub_stop(m
);
571 return log_error_errno(r
, "Failed to listen on %s socket 127.0.0.53:53: %m", t
);
576 void manager_dns_stub_stop(Manager
*m
) {
579 m
->dns_stub_udp_event_source
= sd_event_source_unref(m
->dns_stub_udp_event_source
);
580 m
->dns_stub_tcp_event_source
= sd_event_source_unref(m
->dns_stub_tcp_event_source
);
582 m
->dns_stub_udp_fd
= safe_close(m
->dns_stub_udp_fd
);
583 m
->dns_stub_tcp_fd
= safe_close(m
->dns_stub_tcp_fd
);