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 bool add_opt
, /* add an OPT RR to this packet */
37 bool edns0_do
, /* set the EDNS0 DNSSEC OK bit */
38 bool ad
, /* set the DNSSEC authenticated data bit */
41 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
42 DnsResourceRecord
*rr
;
46 /* Note that we don't bother with any additional RRs, as this is stub is for local lookups only, and hence
47 * roundtrips aren't expensive. */
49 r
= dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0);
53 /* If the client didn't do EDNS, clamp the rcode to 4 bit */
54 if (!add_opt
&& rcode
> 0xF)
55 rcode
= DNS_RCODE_SERVFAIL
;
57 DNS_PACKET_HEADER(p
)->id
= id
;
58 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
69 r
= dns_packet_append_question(p
, q
);
72 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(dns_question_size(q
));
74 DNS_ANSWER_FOREACH(rr
, answer
) {
75 r
= dns_question_matches_rr(q
, rr
, NULL
);
81 r
= dns_question_matches_cname_or_dname(q
, rr
, NULL
);
89 r
= dns_packet_append_rr(p
, rr
, NULL
, NULL
);
95 DNS_PACKET_HEADER(p
)->ancount
= htobe16(c
);
98 r
= dns_packet_append_opt(p
, ADVERTISE_DATAGRAM_SIZE_MAX
, edns0_do
, rcode
, NULL
);
109 static void dns_stub_detach_stream(DnsStream
*s
) {
117 static int dns_stub_send(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, DnsPacket
*reply
) {
125 r
= dns_stream_write_packet(s
, reply
);
129 /* Truncate the message to the right size */
130 if (reply
->size
> DNS_PACKET_PAYLOAD_SIZE_MAX(p
)) {
131 dns_packet_truncate(reply
, DNS_PACKET_UNICAST_SIZE_MAX
);
132 DNS_PACKET_HEADER(reply
)->flags
= htobe16(be16toh(DNS_PACKET_HEADER(reply
)->flags
) | DNS_PACKET_FLAG_TC
);
135 fd
= manager_dns_stub_udp_fd(m
);
137 return log_debug_errno(fd
, "Failed to get reply socket: %m");
139 /* Note that it is essential here that we explicitly choose the source IP address for this packet. This
140 * is because otherwise the kernel will choose it automatically based on the routing table and will
141 * thus pick 127.0.0.1 rather than 127.0.0.53. */
143 r
= manager_send(m
, fd
, LOOPBACK_IFINDEX
, p
->family
, &p
->sender
, p
->sender_port
, &p
->destination
, reply
);
146 return log_debug_errno(r
, "Failed to send reply packet: %m");
151 static int dns_stub_send_failure(Manager
*m
, DnsStream
*s
, DnsPacket
*p
, int rcode
) {
152 _cleanup_(dns_packet_unrefp
) DnsPacket
*reply
= NULL
;
158 r
= dns_stub_make_reply_packet(DNS_PACKET_ID(p
), rcode
, p
->question
, NULL
, !!p
->opt
, DNS_PACKET_DO(p
), false, &reply
);
160 return log_debug_errno(r
, "Failed to build failure packet: %m");
162 return dns_stub_send(m
, s
, p
, reply
);
165 static void dns_stub_query_complete(DnsQuery
*q
) {
169 assert(q
->request_dns_packet
);
173 case DNS_TRANSACTION_SUCCESS
: {
174 _cleanup_(dns_packet_unrefp
) DnsPacket
*reply
= NULL
;
176 r
= dns_stub_make_reply_packet(
177 DNS_PACKET_ID(q
->request_dns_packet
),
181 !!q
->request_dns_packet
->opt
,
182 DNS_PACKET_DO(q
->request_dns_packet
),
183 DNS_PACKET_DO(q
->request_dns_packet
) && q
->answer_authenticated
,
186 log_debug_errno(r
, "Failed to build reply packet: %m");
190 (void) dns_stub_send(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, reply
);
194 case DNS_TRANSACTION_RCODE_FAILURE
:
195 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, q
->answer_rcode
);
198 case DNS_TRANSACTION_NOT_FOUND
:
199 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_NXDOMAIN
);
202 case DNS_TRANSACTION_TIMEOUT
:
203 case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
:
204 /* Propagate a timeout as a no packet, i.e. that the client also gets a timeout */
207 case DNS_TRANSACTION_NO_SERVERS
:
208 case DNS_TRANSACTION_INVALID_REPLY
:
209 case DNS_TRANSACTION_ERRNO
:
210 case DNS_TRANSACTION_ABORTED
:
211 case DNS_TRANSACTION_DNSSEC_FAILED
:
212 case DNS_TRANSACTION_NO_TRUST_ANCHOR
:
213 case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
:
214 case DNS_TRANSACTION_NETWORK_DOWN
:
215 (void) dns_stub_send_failure(q
->manager
, q
->request_dns_stream
, q
->request_dns_packet
, DNS_RCODE_SERVFAIL
);
218 case DNS_TRANSACTION_NULL
:
219 case DNS_TRANSACTION_PENDING
:
220 case DNS_TRANSACTION_VALIDATING
:
222 assert_not_reached("Impossible state");
225 /* If there's a packet to write set, let's leave the stream around */
226 if (q
->request_dns_stream
&& DNS_STREAM_QUEUED(q
->request_dns_stream
)) {
228 /* Detach the stream from our query (make it an orphan), but do not drop the reference to it. The
229 * default completion action of the stream will drop the reference. */
231 dns_stub_detach_stream(q
->request_dns_stream
);
232 q
->request_dns_stream
= NULL
;
238 static int dns_stub_stream_complete(DnsStream
*s
, int error
) {
241 log_debug_errno(error
, "DNS TCP connection terminated, destroying query: %m");
244 dns_query_free(s
->query
);
249 static void dns_stub_process_query(Manager
*m
, DnsStream
*s
, DnsPacket
*p
) {
255 assert(p
->protocol
== DNS_PROTOCOL_DNS
);
257 /* Takes ownership of the *s stream object */
259 if (in_addr_is_localhost(p
->family
, &p
->sender
) <= 0 ||
260 in_addr_is_localhost(p
->family
, &p
->destination
) <= 0) {
261 log_error("Got packet on unexpected IP range, refusing.");
262 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
);
266 r
= dns_packet_extract(p
);
268 log_debug_errno(r
, "Failed to extract resources from incoming packet, ignoring packet: %m");
269 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_FORMERR
);
273 if (!DNS_PACKET_VERSION_SUPPORTED(p
)) {
274 log_debug("Got EDNS OPT field with unsupported version number.");
275 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_BADVERS
);
279 if (dns_type_is_obsolete(p
->question
->keys
[0]->type
)) {
280 log_debug("Got message with obsolete key type, refusing.");
281 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
);
285 if (dns_type_is_zone_transer(p
->question
->keys
[0]->type
)) {
286 log_debug("Got request for zone transfer, refusing.");
287 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
);
291 if (!DNS_PACKET_RD(p
)) {
292 /* If the "rd" bit is off (i.e. recursion was not requested), then refuse operation */
293 log_debug("Got request with recursion disabled, refusing.");
294 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_REFUSED
);
298 if (DNS_PACKET_DO(p
) && DNS_PACKET_CD(p
)) {
299 log_debug("Got request with DNSSEC CD bit set, refusing.");
300 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_NOTIMP
);
304 r
= dns_query_new(m
, &q
, p
->question
, p
->question
, 0, SD_RESOLVED_PROTOCOLS_ALL
|SD_RESOLVED_NO_SEARCH
|SD_RESOLVED_NO_CNAME
);
306 log_error_errno(r
, "Failed to generate query object: %m");
307 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
);
311 /* Request that the TTL is corrected by the cached time for this lookup, so that we return vaguely useful TTLs */
314 q
->request_dns_packet
= dns_packet_ref(p
);
315 q
->request_dns_stream
= dns_stream_ref(s
); /* make sure the stream stays around until we can send a reply through it */
316 q
->complete
= dns_stub_query_complete
;
320 s
->complete
= dns_stub_stream_complete
;
326 log_error_errno(r
, "Failed to start query: %m");
327 dns_stub_send_failure(m
, s
, p
, DNS_RCODE_SERVFAIL
);
331 log_info("Processing query...");
335 if (s
&& DNS_STREAM_QUEUED(s
))
336 dns_stub_detach_stream(s
);
341 static int on_dns_stub_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
342 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
343 Manager
*m
= userdata
;
346 r
= manager_recv(m
, fd
, DNS_PROTOCOL_DNS
, &p
);
350 if (dns_packet_validate_query(p
) > 0) {
351 log_debug("Got DNS stub UDP query packet for id %u", DNS_PACKET_ID(p
));
353 dns_stub_process_query(m
, NULL
, p
);
355 log_debug("Invalid DNS stub UDP packet, ignoring.");
360 static int manager_dns_stub_udp_fd(Manager
*m
) {
361 static const int one
= 1;
362 union sockaddr_union sa
= {
363 .in
.sin_family
= AF_INET
,
364 .in
.sin_port
= htobe16(53),
365 .in
.sin_addr
.s_addr
= htobe32(INADDR_DNS_STUB
),
367 _cleanup_close_
int fd
= -1;
370 if (m
->dns_stub_udp_fd
>= 0)
371 return m
->dns_stub_udp_fd
;
373 fd
= socket(AF_INET
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
377 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
) < 0)
380 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &one
, sizeof one
) < 0)
383 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVTTL
, &one
, sizeof one
) < 0)
386 /* Make sure no traffic from outside the local host can leak to onto this socket */
387 if (setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, "lo", 3) < 0)
390 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
393 r
= sd_event_add_io(m
->event
, &m
->dns_stub_udp_event_source
, fd
, EPOLLIN
, on_dns_stub_packet
, m
);
397 (void) sd_event_source_set_description(m
->dns_stub_udp_event_source
, "dns-stub-udp");
398 m
->dns_stub_udp_fd
= fd
;
401 return m
->dns_stub_udp_fd
;
404 static int on_dns_stub_stream_packet(DnsStream
*s
) {
406 assert(s
->read_packet
);
408 if (dns_packet_validate_query(s
->read_packet
) > 0) {
409 log_debug("Got DNS stub TCP query packet for id %u", DNS_PACKET_ID(s
->read_packet
));
411 dns_stub_process_query(s
->manager
, s
, s
->read_packet
);
413 log_debug("Invalid DNS stub TCP packet, ignoring.");
415 /* Drop the reference to the stream. Either a query was created and added its own reference to the stream now,
416 * or that didn't happen in which case we want to free the stream */
422 static int on_dns_stub_stream(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
424 Manager
*m
= userdata
;
427 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
429 if (errno
== EAGAIN
|| errno
== EINTR
)
435 r
= dns_stream_new(m
, &stream
, DNS_PROTOCOL_DNS
, cfd
);
441 stream
->on_packet
= on_dns_stub_stream_packet
;
443 /* We let the reference to the stream dangling here, it will either be dropped by the default "complete" action
444 * of the stream, or by our packet callback, or when the manager is shut down. */
449 static int manager_dns_stub_tcp_fd(Manager
*m
) {
450 static const int one
= 1;
451 union sockaddr_union sa
= {
452 .in
.sin_family
= AF_INET
,
453 .in
.sin_addr
.s_addr
= htobe32(INADDR_DNS_STUB
),
454 .in
.sin_port
= htobe16(53),
456 _cleanup_close_
int fd
= -1;
459 if (m
->dns_stub_tcp_fd
>= 0)
460 return m
->dns_stub_tcp_fd
;
462 fd
= socket(AF_INET
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
466 if (setsockopt(fd
, IPPROTO_IP
, IP_TTL
, &one
, sizeof one
) < 0)
469 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
) < 0)
472 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &one
, sizeof one
) < 0)
475 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVTTL
, &one
, sizeof one
) < 0)
478 /* Make sure no traffic from outside the local host can leak to onto this socket */
479 if (setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, "lo", 3) < 0)
482 if (bind(fd
, &sa
.sa
, sizeof(sa
.in
)) < 0)
485 if (listen(fd
, SOMAXCONN
) < 0)
488 r
= sd_event_add_io(m
->event
, &m
->dns_stub_tcp_event_source
, fd
, EPOLLIN
, on_dns_stub_stream
, m
);
492 (void) sd_event_source_set_description(m
->dns_stub_tcp_event_source
, "dns-stub-tcp");
493 m
->dns_stub_tcp_fd
= fd
;
496 return m
->dns_stub_tcp_fd
;
499 int manager_dns_stub_start(Manager
*m
) {
500 const char *t
= "UDP";
505 if (IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_UDP
))
506 r
= manager_dns_stub_udp_fd(m
);
509 IN_SET(m
->dns_stub_listener_mode
, DNS_STUB_LISTENER_YES
, DNS_STUB_LISTENER_TCP
)) {
511 r
= manager_dns_stub_tcp_fd(m
);
514 if (IN_SET(r
, -EADDRINUSE
, -EPERM
)) {
515 if (r
== -EADDRINUSE
)
517 "Another process is already listening on %s socket 127.0.0.53:53.\n"
518 "Turning off local DNS stub support.", t
);
521 "Failed to listen on %s socket 127.0.0.53:53: %m.\n"
522 "Turning off local DNS stub support.", t
);
523 manager_dns_stub_stop(m
);
525 return log_error_errno(r
, "Failed to listen on %s socket 127.0.0.53:53: %m", t
);
530 void manager_dns_stub_stop(Manager
*m
) {
533 m
->dns_stub_udp_event_source
= sd_event_source_unref(m
->dns_stub_udp_event_source
);
534 m
->dns_stub_tcp_event_source
= sd_event_source_unref(m
->dns_stub_tcp_event_source
);
536 m
->dns_stub_udp_fd
= safe_close(m
->dns_stub_udp_fd
);
537 m
->dns_stub_tcp_fd
= safe_close(m
->dns_stub_tcp_fd
);