2 This file is part of systemd.
4 Copyright 2016 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "resolved-dns-stub.h"
22 #include "socket-util.h"
24 /* The MTU of the loopback device is 64K on Linux, advertise that as maximum datagram size, but subtract the Ethernet,
25 * IP and UDP header sizes */
26 #define ADVERTISE_DATAGRAM_SIZE_MAX (65536U-14U-20U-8U)
28 static int manager_dns_stub_udp_fd(Manager
*m
);
29 static int manager_dns_stub_tcp_fd(Manager
*m
);
31 static int dns_stub_make_reply_packet(
36 DnsResourceRecord
*rr
;
42 /* Note that we don't bother with any additional RRs, as this is stub is for local lookups only, and hence
43 * roundtrips aren't expensive. */
46 r
= dns_packet_new(p
, DNS_PROTOCOL_DNS
, 0);
50 r
= dns_packet_append_question(*p
, q
);
54 DNS_PACKET_HEADER(*p
)->qdcount
= htobe16(dns_question_size(q
));
57 DNS_ANSWER_FOREACH(rr
, answer
) {
59 r
= dns_question_matches_rr(q
, rr
, NULL
);
65 r
= dns_question_matches_cname_or_dname(q
, rr
, NULL
);
73 r
= dns_packet_append_rr(*p
, rr
, NULL
, NULL
);
80 DNS_PACKET_HEADER(*p
)->ancount
= htobe16(be16toh(DNS_PACKET_HEADER(*p
)->ancount
) + c
);
85 static int dns_stub_finish_reply_packet(
89 bool add_opt
, /* add an OPT RR to this packet? */
90 bool edns0_do
, /* set the EDNS0 DNSSEC OK bit? */
91 bool ad
) { /* set the DNSSEC authenticated data bit? */
97 /* If the client didn't do EDNS, clamp the rcode to 4 bit */
98 if (!add_opt
&& rcode
> 0xF)
99 rcode
= DNS_RCODE_SERVFAIL
;
101 DNS_PACKET_HEADER(p
)->id
= id
;
103 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
115 r
= dns_packet_append_opt(p
, ADVERTISE_DATAGRAM_SIZE_MAX
, edns0_do
, rcode
, NULL
);
123 static void dns_stub_detach_stream(DnsStream
*s
) {
131 static int dns_stub_send(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, DnsPacket
*reply
) {
139 r
= dns_stream_write_packet(s
, reply
);
143 /* Truncate the message to the right size */
144 if (reply
->size
> DNS_PACKET_PAYLOAD_SIZE_MAX(p
)) {
145 dns_packet_truncate(reply
, DNS_PACKET_UNICAST_SIZE_MAX
);
146 DNS_PACKET_HEADER(reply
)->flags
= htobe16(be16toh(DNS_PACKET_HEADER(reply
)->flags
) | DNS_PACKET_FLAG_TC
);
149 fd
= manager_dns_stub_udp_fd(m
);
151 return log_debug_errno(fd
, "Failed to get reply socket: %m");
153 /* Note that it is essential here that we explicitly choose the source IP address for this packet. This
154 * is because otherwise the kernel will choose it automatically based on the routing table and will
155 * thus pick 127.0.0.1 rather than 127.0.0.53. */
157 r
= manager_send(m
, fd
, LOOPBACK_IFINDEX
, p
->family
, &p
->sender
, p
->sender_port
, &p
->destination
, reply
);
160 return log_debug_errno(r
, "Failed to send reply packet: %m");
165 static int dns_stub_send_failure(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, int rcode
) {
166 _cleanup_(dns_packet_unrefp
) DnsPacket
*reply
= NULL
;
172 r
= dns_stub_make_reply_packet(&reply
, p
->question
, NULL
);
174 return log_debug_errno(r
, "Failed to make failure packet: %m");
176 r
= dns_stub_finish_reply_packet(reply
, DNS_PACKET_ID(p
), rcode
, !!p
->opt
, DNS_PACKET_DO(p
), false);
178 return log_debug_errno(r
, "Failed to build failure packet: %m");
180 return dns_stub_send(m
, s
, p
, reply
);
183 static void dns_stub_query_complete(DnsQuery
*q
) {
187 assert(q
->request_dns_packet
);
191 case DNS_TRANSACTION_SUCCESS
:
193 r
= dns_stub_make_reply_packet(&q
->reply_dns_packet
, q
->question_idna
, q
->answer
);
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
);
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
),
215 !!q
->request_dns_packet
->opt
,
216 DNS_PACKET_DO(q
->request_dns_packet
),
217 DNS_PACKET_DO(q
->request_dns_packet
) && dns_query_fully_authenticated(q
));
219 log_debug_errno(r
, "Failed to finish reply packet: %m");
223 (void) dns_stub_send(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->reply_dns_packet
);
226 case DNS_TRANSACTION_RCODE_FAILURE
:
227 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->answer_rcode
);
230 case DNS_TRANSACTION_NOT_FOUND
:
231 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_NXDOMAIN
);
234 case DNS_TRANSACTION_TIMEOUT
:
235 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
236 /* Propagate a timeout as a no packet, i.e. that the client also gets a timeout */
239 case DNS_TRANSACTION_NO_SERVERS
:
240 case DNS_TRANSACTION_INVALID_REPLY
:
241 case DNS_TRANSACTION_ERRNO
:
242 case DNS_TRANSACTION_ABORTED
:
243 case DNS_TRANSACTION_DNSSEC_FAILED
:
244 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
245 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
246 case DNS_TRANSACTION_NETWORK_DOWN
:
247 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_SERVFAIL
);
250 case DNS_TRANSACTION_NULL
:
251 case DNS_TRANSACTION_PENDING
:
252 case DNS_TRANSACTION_VALIDATING
:
254 assert_not_reached("Impossible state");
257 /* If there's a packet to write set, let's leave the stream around */
258 if (q
->request_dns_stream
&& DNS_STREAM_QUEUED(q
->request_dns_stream
)) {
260 /* Detach the stream from our query (make it an orphan), but do not drop the reference to it. The
261 * default completion action of the stream will drop the reference. */
263 dns_stub_detach_stream(q
->request_dns_stream
);
264 q
->request_dns_stream
= NULL
;
270 static int dns_stub_stream_complete(DnsStream
*s
, int error
) {
273 log_debug_errno(error
, "DNS TCP connection terminated, destroying query: %m");
276 dns_query_free(s
->query
);
281 static void dns_stub_process_query(Manager
*m
, DnsStream
*s
, DnsPacket
*p
) {
287 assert(p
->protocol
== DNS_PROTOCOL_DNS
);
289 /* Takes ownership of the *s stream object */
291 if (in_addr_is_localhost(p
->family
, &p
->sender
) <= 0 ||
292 in_addr_is_localhost(p
->family
, &p
->destination
) <= 0) {
293 log_error("Got packet on unexpected IP range, refusing.");
294 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
);
298 r
= dns_packet_extract(p
);
300 log_debug_errno(r
, "Failed to extract resources from incoming packet, ignoring packet: %m");
301 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_FORMERR
);
305 if (!DNS_PACKET_VERSION_SUPPORTED(p
)) {
306 log_debug("Got EDNS OPT field with unsupported version number.");
307 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_BADVERS
);
311 if (dns_type_is_obsolete(p
->question
->keys
[0]->type
)) {
312 log_debug("Got message with obsolete key type, refusing.");
313 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
);
317 if (dns_type_is_zone_transer(p
->question
->keys
[0]->type
)) {
318 log_debug("Got request for zone transfer, refusing.");
319 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
);
323 if (!DNS_PACKET_RD(p
)) {
324 /* If the "rd" bit is off (i.e. recursion was not requested), then refuse operation */
325 log_debug("Got request with recursion disabled, refusing.");
326 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_REFUSED
);
330 if (DNS_PACKET_DO(p
) && DNS_PACKET_CD(p
)) {
331 log_debug("Got request with DNSSEC CD bit set, refusing.");
332 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
);
336 r
= dns_query_new(m
, &q
, p
->question
, p
->question
, 0, SD_RESOLVED_PROTOCOLS_ALL
|SD_RESOLVED_NO_SEARCH
);
338 log_error_errno(r
, "Failed to generate query object: %m");
339 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
);
343 /* Request that the TTL is corrected by the cached time for this lookup, so that we return vaguely useful TTLs */
346 q
->request_dns_packet
= dns_packet_ref(p
);
347 q
->request_dns_stream
= dns_stream_ref(s
); /* make sure the stream stays around until we can send a reply through it */
348 q
->complete
= dns_stub_query_complete
;
352 s
->complete
= dns_stub_stream_complete
;
358 log_error_errno(r
, "Failed to start query: %m");
359 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
);
363 log_debug("Processing query...");
367 if (s
&& DNS_STREAM_QUEUED(s
))
368 dns_stub_detach_stream(s
);
373 static int on_dns_stub_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
374 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
375 Manager
*m
= userdata
;
378 r
= manager_recv(m
, fd
, DNS_PROTOCOL_DNS
, &p
);
382 if (dns_packet_validate_query(p
) > 0) {
383 log_debug("Got DNS stub UDP query packet for id %u", DNS_PACKET_ID(p
));
385 dns_stub_process_query(m
, NULL
, p
);
387 log_debug("Invalid DNS stub UDP packet, ignoring.");
392 static int manager_dns_stub_udp_fd(Manager
*m
) {
393 static const int one
= 1;
394 union sockaddr_union sa
= {
395 .in
.sin_family
= AF_INET
,
396 .in
.sin_port
= htobe16(53),
397 .in
.sin_addr
.s_addr
= htobe32(INADDR_DNS_STUB
),
399 _cleanup_close_
int fd
= -1;
402 if (m
->dns_stub_udp_fd
>= 0)
403 return m
->dns_stub_udp_fd
;
405 fd
= socket(AF_INET
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
409 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
) < 0)
412 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &one
, sizeof one
) < 0)
415 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVTTL
, &one
, sizeof one
) < 0)
418 /* Make sure no traffic from outside the local host can leak to onto this socket */
419 if (setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, "lo", 3) < 0)
422 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
425 r
= sd_event_add_io(m
->event
, &m
->dns_stub_udp_event_source
, fd
, EPOLLIN
, on_dns_stub_packet
, m
);
429 (void) sd_event_source_set_description(m
->dns_stub_udp_event_source
, "dns-stub-udp");
430 m
->dns_stub_udp_fd
= fd
;
433 return m
->dns_stub_udp_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 (errno
== EAGAIN
|| errno
== EINTR
)
467 r
= dns_stream_new(m
, &stream
, DNS_PROTOCOL_DNS
, cfd
);
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");
525 m
->dns_stub_tcp_fd
= fd
;
528 return m
->dns_stub_tcp_fd
;
531 int manager_dns_stub_start(Manager
*m
) {
532 const char *t
= "UDP";
537 if (IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_UDP
))
538 r
= manager_dns_stub_udp_fd(m
);
541 IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_TCP
)) {
543 r
= manager_dns_stub_tcp_fd(m
);
546 if (IN_SET(r
, -EADDRINUSE
, -EPERM
)) {
547 if (r
== -EADDRINUSE
)
549 "Another process is already listening on %s socket 127.0.0.53:53.\n"
550 "Turning off local DNS stub support.", t
);
553 "Failed to listen on %s socket 127.0.0.53:53: %m.\n"
554 "Turning off local DNS stub support.", t
);
555 manager_dns_stub_stop(m
);
557 return log_error_errno(r
, "Failed to listen on %s socket 127.0.0.53:53: %m", t
);
562 void manager_dns_stub_stop(Manager
*m
) {
565 m
->dns_stub_udp_event_source
= sd_event_source_unref(m
->dns_stub_udp_event_source
);
566 m
->dns_stub_tcp_event_source
= sd_event_source_unref(m
->dns_stub_tcp_event_source
);
568 m
->dns_stub_udp_fd
= safe_close(m
->dns_stub_udp_fd
);
569 m
->dns_stub_tcp_fd
= safe_close(m
->dns_stub_tcp_fd
);