]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-stream.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
8 #include <netinet/tcp.h>
10 #include "alloc-util.h"
14 #include "resolved-dns-stream.h"
16 #define DNS_STREAM_TIMEOUT_USEC (10 * USEC_PER_SEC)
17 #define DNS_STREAMS_MAX 128
19 static void dns_stream_stop(DnsStream
*s
) {
22 s
->io_event_source
= sd_event_source_unref(s
->io_event_source
);
23 s
->timeout_event_source
= sd_event_source_unref(s
->timeout_event_source
);
24 s
->fd
= safe_close(s
->fd
);
27 static int dns_stream_update_io(DnsStream
*s
) {
32 if (s
->write_packet
&& s
->n_written
< sizeof(s
->write_size
) + s
->write_packet
->size
)
34 if (!s
->read_packet
|| s
->n_read
< sizeof(s
->read_size
) + s
->read_packet
->size
)
37 return sd_event_source_set_io_events(s
->io_event_source
, f
);
40 static int dns_stream_complete(DnsStream
*s
, int error
) {
46 s
->complete(s
, error
);
47 else /* the default action if no completion function is set is to close the stream */
53 static int dns_stream_identify(DnsStream
*s
) {
55 struct cmsghdr header
; /* For alignment */
56 uint8_t buffer
[CMSG_SPACE(MAXSIZE(struct in_pktinfo
, struct in6_pktinfo
))
57 + EXTRA_CMSG_SPACE
/* kernel appears to require extra space */];
59 struct msghdr mh
= {};
69 /* Query the local side */
70 s
->local_salen
= sizeof(s
->local
);
71 r
= getsockname(s
->fd
, &s
->local
.sa
, &s
->local_salen
);
74 if (s
->local
.sa
.sa_family
== AF_INET6
&& s
->ifindex
<= 0)
75 s
->ifindex
= s
->local
.in6
.sin6_scope_id
;
77 /* Query the remote side */
78 s
->peer_salen
= sizeof(s
->peer
);
79 r
= getpeername(s
->fd
, &s
->peer
.sa
, &s
->peer_salen
);
82 if (s
->peer
.sa
.sa_family
== AF_INET6
&& s
->ifindex
<= 0)
83 s
->ifindex
= s
->peer
.in6
.sin6_scope_id
;
85 /* Check consistency */
86 assert(s
->peer
.sa
.sa_family
== s
->local
.sa
.sa_family
);
87 assert(IN_SET(s
->peer
.sa
.sa_family
, AF_INET
, AF_INET6
));
89 /* Query connection meta information */
91 if (s
->peer
.sa
.sa_family
== AF_INET
) {
92 r
= getsockopt(s
->fd
, IPPROTO_IP
, IP_PKTOPTIONS
, &control
, &sl
);
95 } else if (s
->peer
.sa
.sa_family
== AF_INET6
) {
97 r
= getsockopt(s
->fd
, IPPROTO_IPV6
, IPV6_2292PKTOPTIONS
, &control
, &sl
);
101 return -EAFNOSUPPORT
;
103 mh
.msg_control
= &control
;
104 mh
.msg_controllen
= sl
;
106 CMSG_FOREACH(cmsg
, &mh
) {
108 if (cmsg
->cmsg_level
== IPPROTO_IPV6
) {
109 assert(s
->peer
.sa
.sa_family
== AF_INET6
);
111 switch (cmsg
->cmsg_type
) {
114 struct in6_pktinfo
*i
= (struct in6_pktinfo
*) CMSG_DATA(cmsg
);
117 s
->ifindex
= i
->ipi6_ifindex
;
122 s
->ttl
= *(int *) CMSG_DATA(cmsg
);
126 } else if (cmsg
->cmsg_level
== IPPROTO_IP
) {
127 assert(s
->peer
.sa
.sa_family
== AF_INET
);
129 switch (cmsg
->cmsg_type
) {
132 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
135 s
->ifindex
= i
->ipi_ifindex
;
140 s
->ttl
= *(int *) CMSG_DATA(cmsg
);
146 /* The Linux kernel sets the interface index to the loopback
147 * device if the connection came from the local host since it
148 * avoids the routing table in such a case. Let's unset the
149 * interface index in such a case. */
150 if (s
->ifindex
== LOOPBACK_IFINDEX
)
153 /* If we don't know the interface index still, we look for the
154 * first local interface with a matching address. Yuck! */
156 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
);
158 if (s
->protocol
== DNS_PROTOCOL_LLMNR
&& s
->ifindex
> 0) {
159 uint32_t ifindex
= htobe32(s
->ifindex
);
161 /* Make sure all packets for this connection are sent on the same interface */
162 if (s
->local
.sa
.sa_family
== AF_INET
) {
163 r
= setsockopt(s
->fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
));
165 log_debug_errno(errno
, "Failed to invoke IP_UNICAST_IF: %m");
166 } else if (s
->local
.sa
.sa_family
== AF_INET6
) {
167 r
= setsockopt(s
->fd
, IPPROTO_IPV6
, IPV6_UNICAST_IF
, &ifindex
, sizeof(ifindex
));
169 log_debug_errno(errno
, "Failed to invoke IPV6_UNICAST_IF: %m");
173 s
->identified
= true;
178 static int on_stream_timeout(sd_event_source
*es
, usec_t usec
, void *userdata
) {
179 DnsStream
*s
= userdata
;
183 return dns_stream_complete(s
, ETIMEDOUT
);
186 static int on_stream_io(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
187 DnsStream
*s
= userdata
;
192 r
= dns_stream_identify(s
);
194 return dns_stream_complete(s
, -r
);
196 if ((revents
& EPOLLOUT
) &&
198 s
->n_written
< sizeof(s
->write_size
) + s
->write_packet
->size
) {
203 iov
[0].iov_base
= &s
->write_size
;
204 iov
[0].iov_len
= sizeof(s
->write_size
);
205 iov
[1].iov_base
= DNS_PACKET_DATA(s
->write_packet
);
206 iov
[1].iov_len
= s
->write_packet
->size
;
208 IOVEC_INCREMENT(iov
, 2, s
->n_written
);
210 ss
= writev(fd
, iov
, 2);
212 if (!IN_SET(errno
, EINTR
, EAGAIN
))
213 return dns_stream_complete(s
, errno
);
217 /* Are we done? If so, disable the event source for EPOLLOUT */
218 if (s
->n_written
>= sizeof(s
->write_size
) + s
->write_packet
->size
) {
219 r
= dns_stream_update_io(s
);
221 return dns_stream_complete(s
, -r
);
225 if ((revents
& (EPOLLIN
|EPOLLHUP
|EPOLLRDHUP
)) &&
227 s
->n_read
< sizeof(s
->read_size
) + s
->read_packet
->size
)) {
229 if (s
->n_read
< sizeof(s
->read_size
)) {
232 ss
= read(fd
, (uint8_t*) &s
->read_size
+ s
->n_read
, sizeof(s
->read_size
) - s
->n_read
);
234 if (!IN_SET(errno
, EINTR
, EAGAIN
))
235 return dns_stream_complete(s
, errno
);
237 return dns_stream_complete(s
, ECONNRESET
);
242 if (s
->n_read
>= sizeof(s
->read_size
)) {
244 if (be16toh(s
->read_size
) < DNS_PACKET_HEADER_SIZE
)
245 return dns_stream_complete(s
, EBADMSG
);
247 if (s
->n_read
< sizeof(s
->read_size
) + be16toh(s
->read_size
)) {
250 if (!s
->read_packet
) {
251 r
= dns_packet_new(&s
->read_packet
, s
->protocol
, be16toh(s
->read_size
), DNS_PACKET_SIZE_MAX
);
253 return dns_stream_complete(s
, -r
);
255 s
->read_packet
->size
= be16toh(s
->read_size
);
256 s
->read_packet
->ipproto
= IPPROTO_TCP
;
257 s
->read_packet
->family
= s
->peer
.sa
.sa_family
;
258 s
->read_packet
->ttl
= s
->ttl
;
259 s
->read_packet
->ifindex
= s
->ifindex
;
261 if (s
->read_packet
->family
== AF_INET
) {
262 s
->read_packet
->sender
.in
= s
->peer
.in
.sin_addr
;
263 s
->read_packet
->sender_port
= be16toh(s
->peer
.in
.sin_port
);
264 s
->read_packet
->destination
.in
= s
->local
.in
.sin_addr
;
265 s
->read_packet
->destination_port
= be16toh(s
->local
.in
.sin_port
);
267 assert(s
->read_packet
->family
== AF_INET6
);
268 s
->read_packet
->sender
.in6
= s
->peer
.in6
.sin6_addr
;
269 s
->read_packet
->sender_port
= be16toh(s
->peer
.in6
.sin6_port
);
270 s
->read_packet
->destination
.in6
= s
->local
.in6
.sin6_addr
;
271 s
->read_packet
->destination_port
= be16toh(s
->local
.in6
.sin6_port
);
273 if (s
->read_packet
->ifindex
== 0)
274 s
->read_packet
->ifindex
= s
->peer
.in6
.sin6_scope_id
;
275 if (s
->read_packet
->ifindex
== 0)
276 s
->read_packet
->ifindex
= s
->local
.in6
.sin6_scope_id
;
281 (uint8_t*) DNS_PACKET_DATA(s
->read_packet
) + s
->n_read
- sizeof(s
->read_size
),
282 sizeof(s
->read_size
) + be16toh(s
->read_size
) - s
->n_read
);
284 if (!IN_SET(errno
, EINTR
, EAGAIN
))
285 return dns_stream_complete(s
, errno
);
287 return dns_stream_complete(s
, ECONNRESET
);
292 /* Are we done? If so, disable the event source for EPOLLIN */
293 if (s
->n_read
>= sizeof(s
->read_size
) + be16toh(s
->read_size
)) {
294 r
= dns_stream_update_io(s
);
296 return dns_stream_complete(s
, -r
);
298 /* If there's a packet handler
299 * installed, call that. Note that
300 * this is optional... */
302 return s
->on_packet(s
);
307 if ((s
->write_packet
&& s
->n_written
>= sizeof(s
->write_size
) + s
->write_packet
->size
) &&
308 (s
->read_packet
&& s
->n_read
>= sizeof(s
->read_size
) + s
->read_packet
->size
))
309 return dns_stream_complete(s
, 0);
314 DnsStream
*dns_stream_unref(DnsStream
*s
) {
318 assert(s
->n_ref
> 0);
327 LIST_REMOVE(streams
, s
->manager
->dns_streams
, s
);
328 s
->manager
->n_dns_streams
--;
331 dns_packet_unref(s
->write_packet
);
332 dns_packet_unref(s
->read_packet
);
337 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream
*, dns_stream_unref
);
339 DnsStream
*dns_stream_ref(DnsStream
*s
) {
343 assert(s
->n_ref
> 0);
349 int dns_stream_new(Manager
*m
, DnsStream
**ret
, DnsProtocol protocol
, int fd
) {
350 _cleanup_(dns_stream_unrefp
) DnsStream
*s
= NULL
;
356 if (m
->n_dns_streams
> DNS_STREAMS_MAX
)
359 s
= new0(DnsStream
, 1);
365 s
->protocol
= protocol
;
367 r
= sd_event_add_io(m
->event
, &s
->io_event_source
, fd
, EPOLLIN
, on_stream_io
, s
);
371 (void) sd_event_source_set_description(s
->io_event_source
, "dns-stream-io");
373 r
= sd_event_add_time(
375 &s
->timeout_event_source
,
376 clock_boottime_or_monotonic(),
377 now(clock_boottime_or_monotonic()) + DNS_STREAM_TIMEOUT_USEC
, 0,
378 on_stream_timeout
, s
);
382 (void) sd_event_source_set_description(s
->timeout_event_source
, "dns-stream-timeout");
384 LIST_PREPEND(streams
, m
->dns_streams
, s
);
394 int dns_stream_write_packet(DnsStream
*s
, DnsPacket
*p
) {
400 s
->write_packet
= dns_packet_ref(p
);
401 s
->write_size
= htobe16(p
->size
);
404 return dns_stream_update_io(s
);