1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/tcp.h>
6 #include "alloc-util.h"
9 #include "missing_network.h"
10 #include "resolved-dns-stream.h"
11 #include "resolved-manager.h"
13 #define DNS_STREAM_TIMEOUT_USEC (10 * USEC_PER_SEC)
14 #define DNS_STREAMS_MAX 128
16 #define DNS_QUERIES_PER_STREAM 32
18 static void dns_stream_stop(DnsStream
*s
) {
21 s
->io_event_source
= sd_event_source_unref(s
->io_event_source
);
22 s
->timeout_event_source
= sd_event_source_unref(s
->timeout_event_source
);
23 s
->fd
= safe_close(s
->fd
);
25 /* Disconnect us from the server object if we are now not usable anymore */
29 static int dns_stream_update_io(DnsStream
*s
) {
34 if (s
->write_packet
&& s
->n_written
< sizeof(s
->write_size
) + s
->write_packet
->size
)
36 else if (!ordered_set_isempty(s
->write_queue
)) {
37 dns_packet_unref(s
->write_packet
);
38 s
->write_packet
= ordered_set_steal_first(s
->write_queue
);
39 s
->write_size
= htobe16(s
->write_packet
->size
);
44 /* Let's read a packet if we haven't queued any yet. Except if we already hit a limit of parallel
45 * queries for this connection. */
46 if ((!s
->read_packet
|| s
->n_read
< sizeof(s
->read_size
) + s
->read_packet
->size
) &&
47 set_size(s
->queries
) < DNS_QUERIES_PER_STREAM
)
50 #if ENABLE_DNS_OVER_TLS
51 /* For handshake and clean closing purposes, TLS can override requested events */
52 if (s
->dnstls_events
!= 0)
56 return sd_event_source_set_io_events(s
->io_event_source
, f
);
59 static int dns_stream_complete(DnsStream
*s
, int error
) {
60 _cleanup_(dns_stream_unrefp
) _unused_ DnsStream
*ref
= dns_stream_ref(s
); /* Protect stream while we process it */
65 /* Error is > 0 when the connection failed for some reason in the network stack. It's == 0 if we sent
66 * and received exactly one packet each (in the LLMNR client case). */
68 #if ENABLE_DNS_OVER_TLS
72 r
= dnstls_stream_shutdown(s
, error
);
82 s
->complete(s
, error
);
83 else /* the default action if no completion function is set is to close the stream */
89 static int dns_stream_identify(DnsStream
*s
) {
90 CMSG_BUFFER_TYPE(CMSG_SPACE(MAXSIZE(struct in_pktinfo
, struct in6_pktinfo
))
91 + CMSG_SPACE(int) + /* for the TTL */
92 + EXTRA_CMSG_SPACE
/* kernel appears to require extra space */) control
;
93 struct msghdr mh
= {};
103 /* Query the local side */
104 s
->local_salen
= sizeof(s
->local
);
105 r
= getsockname(s
->fd
, &s
->local
.sa
, &s
->local_salen
);
108 if (s
->local
.sa
.sa_family
== AF_INET6
&& s
->ifindex
<= 0)
109 s
->ifindex
= s
->local
.in6
.sin6_scope_id
;
111 /* Query the remote side */
112 s
->peer_salen
= sizeof(s
->peer
);
113 r
= getpeername(s
->fd
, &s
->peer
.sa
, &s
->peer_salen
);
116 if (s
->peer
.sa
.sa_family
== AF_INET6
&& s
->ifindex
<= 0)
117 s
->ifindex
= s
->peer
.in6
.sin6_scope_id
;
119 /* Check consistency */
120 assert(s
->peer
.sa
.sa_family
== s
->local
.sa
.sa_family
);
121 assert(IN_SET(s
->peer
.sa
.sa_family
, AF_INET
, AF_INET6
));
123 /* Query connection meta information */
124 sl
= sizeof(control
);
125 if (s
->peer
.sa
.sa_family
== AF_INET
) {
126 r
= getsockopt(s
->fd
, IPPROTO_IP
, IP_PKTOPTIONS
, &control
, &sl
);
129 } else if (s
->peer
.sa
.sa_family
== AF_INET6
) {
131 r
= getsockopt(s
->fd
, IPPROTO_IPV6
, IPV6_2292PKTOPTIONS
, &control
, &sl
);
135 return -EAFNOSUPPORT
;
137 mh
.msg_control
= &control
;
138 mh
.msg_controllen
= sl
;
140 CMSG_FOREACH(cmsg
, &mh
) {
142 if (cmsg
->cmsg_level
== IPPROTO_IPV6
) {
143 assert(s
->peer
.sa
.sa_family
== AF_INET6
);
145 switch (cmsg
->cmsg_type
) {
148 struct in6_pktinfo
*i
= (struct in6_pktinfo
*) CMSG_DATA(cmsg
);
151 s
->ifindex
= i
->ipi6_ifindex
;
156 s
->ttl
= *(int *) CMSG_DATA(cmsg
);
160 } else if (cmsg
->cmsg_level
== IPPROTO_IP
) {
161 assert(s
->peer
.sa
.sa_family
== AF_INET
);
163 switch (cmsg
->cmsg_type
) {
166 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
169 s
->ifindex
= i
->ipi_ifindex
;
174 s
->ttl
= *(int *) CMSG_DATA(cmsg
);
180 /* The Linux kernel sets the interface index to the loopback
181 * device if the connection came from the local host since it
182 * avoids the routing table in such a case. Let's unset the
183 * interface index in such a case. */
184 if (s
->ifindex
== LOOPBACK_IFINDEX
)
187 /* If we don't know the interface index still, we look for the
188 * first local interface with a matching address. Yuck! */
190 s
->ifindex
= manager_find_ifindex(s
->manager
, s
->local
.sa
.sa_family
, s
->local
.sa
.sa_family
== AF_INET
? (union in_addr_union
*) &s
->local
.in
.sin_addr
: (union in_addr_union
*) &s
->local
.in6
.sin6_addr
);
192 if (s
->protocol
== DNS_PROTOCOL_LLMNR
&& s
->ifindex
> 0) {
193 /* Make sure all packets for this connection are sent on the same interface */
194 r
= socket_set_unicast_if(s
->fd
, s
->local
.sa
.sa_family
, s
->ifindex
);
196 log_debug_errno(errno
, "Failed to invoke IP_UNICAST_IF/IPV6_UNICAST_IF: %m");
199 s
->identified
= true;
204 ssize_t
dns_stream_writev(DnsStream
*s
, const struct iovec
*iov
, size_t iovcnt
, int flags
) {
210 #if ENABLE_DNS_OVER_TLS
211 if (s
->encrypted
&& !(flags
& DNS_STREAM_WRITE_TLS_DATA
)) {
216 for (i
= 0; i
< iovcnt
; i
++) {
217 ss
= dnstls_stream_write(s
, iov
[i
].iov_base
, iov
[i
].iov_len
);
222 if (ss
!= (ssize_t
) iov
[i
].iov_len
)
227 if (s
->tfo_salen
> 0) {
228 struct msghdr hdr
= {
229 .msg_iov
= (struct iovec
*) iov
,
230 .msg_iovlen
= iovcnt
,
231 .msg_name
= &s
->tfo_address
.sa
,
232 .msg_namelen
= s
->tfo_salen
235 m
= sendmsg(s
->fd
, &hdr
, MSG_FASTOPEN
);
237 if (errno
== EOPNOTSUPP
) {
239 if (connect(s
->fd
, &s
->tfo_address
.sa
, s
->tfo_salen
) < 0)
244 if (errno
== EINPROGRESS
)
249 s
->tfo_salen
= 0; /* connection is made */
251 m
= writev(s
->fd
, iov
, iovcnt
);
259 static ssize_t
dns_stream_read(DnsStream
*s
, void *buf
, size_t count
) {
262 #if ENABLE_DNS_OVER_TLS
264 ss
= dnstls_stream_read(s
, buf
, count
);
268 ss
= read(s
->fd
, buf
, count
);
276 static int on_stream_timeout(sd_event_source
*es
, usec_t usec
, void *userdata
) {
277 DnsStream
*s
= userdata
;
281 return dns_stream_complete(s
, ETIMEDOUT
);
284 static int on_stream_io(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
285 _cleanup_(dns_stream_unrefp
) DnsStream
*s
= dns_stream_ref(userdata
); /* Protect stream while we process it */
286 bool progressed
= false;
291 #if ENABLE_DNS_OVER_TLS
293 r
= dnstls_stream_on_io(s
, revents
);
294 if (r
== DNSTLS_STREAM_CLOSED
)
297 return dns_stream_update_io(s
);
299 return dns_stream_complete(s
, -r
);
301 r
= dns_stream_update_io(s
);
307 /* only identify after connecting */
308 if (s
->tfo_salen
== 0) {
309 r
= dns_stream_identify(s
);
311 return dns_stream_complete(s
, -r
);
314 if ((revents
& EPOLLOUT
) &&
316 s
->n_written
< sizeof(s
->write_size
) + s
->write_packet
->size
) {
321 iov
[0] = IOVEC_MAKE(&s
->write_size
, sizeof(s
->write_size
));
322 iov
[1] = IOVEC_MAKE(DNS_PACKET_DATA(s
->write_packet
), s
->write_packet
->size
);
324 IOVEC_INCREMENT(iov
, 2, s
->n_written
);
326 ss
= dns_stream_writev(s
, iov
, 2, 0);
328 if (!IN_SET(-ss
, EINTR
, EAGAIN
))
329 return dns_stream_complete(s
, -ss
);
335 /* Are we done? If so, disable the event source for EPOLLOUT */
336 if (s
->n_written
>= sizeof(s
->write_size
) + s
->write_packet
->size
) {
337 r
= dns_stream_update_io(s
);
339 return dns_stream_complete(s
, -r
);
343 if ((revents
& (EPOLLIN
|EPOLLHUP
|EPOLLRDHUP
)) &&
345 s
->n_read
< sizeof(s
->read_size
) + s
->read_packet
->size
)) {
347 if (s
->n_read
< sizeof(s
->read_size
)) {
350 ss
= dns_stream_read(s
, (uint8_t*) &s
->read_size
+ s
->n_read
, sizeof(s
->read_size
) - s
->n_read
);
352 if (!IN_SET(-ss
, EINTR
, EAGAIN
))
353 return dns_stream_complete(s
, -ss
);
355 return dns_stream_complete(s
, ECONNRESET
);
362 if (s
->n_read
>= sizeof(s
->read_size
)) {
364 if (be16toh(s
->read_size
) < DNS_PACKET_HEADER_SIZE
)
365 return dns_stream_complete(s
, EBADMSG
);
367 if (s
->n_read
< sizeof(s
->read_size
) + be16toh(s
->read_size
)) {
370 if (!s
->read_packet
) {
371 r
= dns_packet_new(&s
->read_packet
, s
->protocol
, be16toh(s
->read_size
), DNS_PACKET_SIZE_MAX
);
373 return dns_stream_complete(s
, -r
);
375 s
->read_packet
->size
= be16toh(s
->read_size
);
376 s
->read_packet
->ipproto
= IPPROTO_TCP
;
377 s
->read_packet
->family
= s
->peer
.sa
.sa_family
;
378 s
->read_packet
->ttl
= s
->ttl
;
379 s
->read_packet
->ifindex
= s
->ifindex
;
381 if (s
->read_packet
->family
== AF_INET
) {
382 s
->read_packet
->sender
.in
= s
->peer
.in
.sin_addr
;
383 s
->read_packet
->sender_port
= be16toh(s
->peer
.in
.sin_port
);
384 s
->read_packet
->destination
.in
= s
->local
.in
.sin_addr
;
385 s
->read_packet
->destination_port
= be16toh(s
->local
.in
.sin_port
);
387 assert(s
->read_packet
->family
== AF_INET6
);
388 s
->read_packet
->sender
.in6
= s
->peer
.in6
.sin6_addr
;
389 s
->read_packet
->sender_port
= be16toh(s
->peer
.in6
.sin6_port
);
390 s
->read_packet
->destination
.in6
= s
->local
.in6
.sin6_addr
;
391 s
->read_packet
->destination_port
= be16toh(s
->local
.in6
.sin6_port
);
393 if (s
->read_packet
->ifindex
== 0)
394 s
->read_packet
->ifindex
= s
->peer
.in6
.sin6_scope_id
;
395 if (s
->read_packet
->ifindex
== 0)
396 s
->read_packet
->ifindex
= s
->local
.in6
.sin6_scope_id
;
400 ss
= dns_stream_read(s
,
401 (uint8_t*) DNS_PACKET_DATA(s
->read_packet
) + s
->n_read
- sizeof(s
->read_size
),
402 sizeof(s
->read_size
) + be16toh(s
->read_size
) - s
->n_read
);
404 if (!IN_SET(-ss
, EINTR
, EAGAIN
))
405 return dns_stream_complete(s
, -ss
);
407 return dns_stream_complete(s
, ECONNRESET
);
412 /* Are we done? If so, disable the event source for EPOLLIN */
413 if (s
->n_read
>= sizeof(s
->read_size
) + be16toh(s
->read_size
)) {
414 /* If there's a packet handler
415 * installed, call that. Note that
416 * this is optional... */
423 r
= dns_stream_update_io(s
);
425 return dns_stream_complete(s
, -r
);
430 /* Call "complete" callback if finished reading and writing one packet, and there's nothing else left
432 if (s
->type
== DNS_STREAM_LLMNR_SEND
&&
433 (s
->write_packet
&& s
->n_written
>= sizeof(s
->write_size
) + s
->write_packet
->size
) &&
434 ordered_set_isempty(s
->write_queue
) &&
435 (s
->read_packet
&& s
->n_read
>= sizeof(s
->read_size
) + s
->read_packet
->size
))
436 return dns_stream_complete(s
, 0);
438 /* If we did something, let's restart the timeout event source */
439 if (progressed
&& s
->timeout_event_source
) {
440 r
= sd_event_source_set_time_relative(s
->timeout_event_source
, DNS_STREAM_TIMEOUT_USEC
);
442 log_warning_errno(errno
, "Couldn't restart TCP connection timeout, ignoring: %m");
448 static DnsStream
*dns_stream_free(DnsStream
*s
) {
456 LIST_REMOVE(streams
, s
->manager
->dns_streams
, s
);
457 s
->manager
->n_dns_streams
[s
->type
]--;
460 #if ENABLE_DNS_OVER_TLS
462 dnstls_stream_free(s
);
465 ORDERED_SET_FOREACH(p
, s
->write_queue
)
466 dns_packet_unref(ordered_set_remove(s
->write_queue
, p
));
468 dns_packet_unref(s
->write_packet
);
469 dns_packet_unref(s
->read_packet
);
470 dns_server_unref(s
->server
);
472 ordered_set_free(s
->write_queue
);
477 DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsStream
, dns_stream
, dns_stream_free
);
483 DnsProtocol protocol
,
485 const union sockaddr_union
*tfo_address
) {
487 _cleanup_(dns_stream_unrefp
) DnsStream
*s
= NULL
;
493 assert(type
< _DNS_STREAM_TYPE_MAX
);
494 assert(protocol
>= 0);
495 assert(protocol
< _DNS_PROTOCOL_MAX
);
498 if (m
->n_dns_streams
[type
] > DNS_STREAMS_MAX
)
501 s
= new(DnsStream
, 1);
508 .protocol
= protocol
,
512 r
= ordered_set_ensure_allocated(&s
->write_queue
, &dns_packet_hash_ops
);
516 r
= sd_event_add_io(m
->event
, &s
->io_event_source
, fd
, EPOLLIN
, on_stream_io
, s
);
520 (void) sd_event_source_set_description(s
->io_event_source
, "dns-stream-io");
522 r
= sd_event_add_time_relative(
524 &s
->timeout_event_source
,
525 clock_boottime_or_monotonic(),
526 DNS_STREAM_TIMEOUT_USEC
, 0,
527 on_stream_timeout
, s
);
531 (void) sd_event_source_set_description(s
->timeout_event_source
, "dns-stream-timeout");
533 LIST_PREPEND(streams
, m
->dns_streams
, s
);
534 m
->n_dns_streams
[type
]++;
540 s
->tfo_address
= *tfo_address
;
541 s
->tfo_salen
= tfo_address
->sa
.sa_family
== AF_INET6
? sizeof(tfo_address
->in6
) : sizeof(tfo_address
->in
);
549 int dns_stream_write_packet(DnsStream
*s
, DnsPacket
*p
) {
555 r
= ordered_set_put(s
->write_queue
, p
);
561 return dns_stream_update_io(s
);
564 DnsPacket
*dns_stream_take_read_packet(DnsStream
*s
) {
570 if (s
->n_read
< sizeof(s
->read_size
))
573 if (s
->n_read
< sizeof(s
->read_size
) + be16toh(s
->read_size
))
577 return TAKE_PTR(s
->read_packet
);
580 void dns_stream_detach(DnsStream
*s
) {
586 if (s
->server
->stream
!= s
)
589 dns_server_unref_stream(s
->server
);