]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-dns-stream.c
resolved: only call complete() with zero argument in LLMNR client cases
[thirdparty/systemd.git] / src / resolve / resolved-dns-stream.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
623a4c97
LP
2
3#include <netinet/tcp.h>
4
b5efdb8a 5#include "alloc-util.h"
3ffd4af2 6#include "fd-util.h"
afc5dbf3 7#include "io-util.h"
623a4c97
LP
8#include "missing.h"
9#include "resolved-dns-stream.h"
10
11#define DNS_STREAM_TIMEOUT_USEC (10 * USEC_PER_SEC)
12#define DNS_STREAMS_MAX 128
13
14static void dns_stream_stop(DnsStream *s) {
15 assert(s);
16
17 s->io_event_source = sd_event_source_unref(s->io_event_source);
18 s->timeout_event_source = sd_event_source_unref(s->timeout_event_source);
19 s->fd = safe_close(s->fd);
808089ae
LP
20
21 /* Disconnect us from the server object if we are now not usable anymore */
22 dns_stream_detach(s);
623a4c97
LP
23}
24
25static int dns_stream_update_io(DnsStream *s) {
26 int f = 0;
27
28 assert(s);
29
30 if (s->write_packet && s->n_written < sizeof(s->write_size) + s->write_packet->size)
31 f |= EPOLLOUT;
98767d75
IT
32 else if (!ordered_set_isempty(s->write_queue)) {
33 dns_packet_unref(s->write_packet);
34 s->write_packet = ordered_set_steal_first(s->write_queue);
35 s->write_size = htobe16(s->write_packet->size);
36 s->n_written = 0;
37 f |= EPOLLOUT;
38 }
623a4c97
LP
39 if (!s->read_packet || s->n_read < sizeof(s->read_size) + s->read_packet->size)
40 f |= EPOLLIN;
41
ba6aaf57
IT
42#if ENABLE_DNS_OVER_TLS
43 /* For handshake and clean closing purposes, TLS can override requested events */
57bdb749 44 if (s->dnstls_events != 0)
ba6aaf57
IT
45 f = s->dnstls_events;
46#endif
47
623a4c97
LP
48 return sd_event_source_set_io_events(s->io_event_source, f);
49}
50
b914e211 51static int dns_stream_complete(DnsStream *s, int error) {
d973d94d
LP
52 _cleanup_(dns_stream_unrefp) _unused_ DnsStream *ref = dns_stream_ref(s); /* Protect stream while we process it */
53
623a4c97
LP
54 assert(s);
55
56ddbf10 56#if ENABLE_DNS_OVER_TLS
6016fcb0 57 if (s->encrypted) {
5d67a7ae
IT
58 int r;
59
6016fcb0
IT
60 r = dnstls_stream_shutdown(s, error);
61 if (r != -EAGAIN)
5d67a7ae
IT
62 dns_stream_stop(s);
63 } else
64#endif
65 dns_stream_stop(s);
623a4c97 66
7172e4ee
LP
67 dns_stream_detach(s);
68
623a4c97
LP
69 if (s->complete)
70 s->complete(s, error);
b30bf55d
LP
71 else /* the default action if no completion function is set is to close the stream */
72 dns_stream_unref(s);
623a4c97
LP
73
74 return 0;
75}
76
b914e211
LP
77static int dns_stream_identify(DnsStream *s) {
78 union {
79 struct cmsghdr header; /* For alignment */
40a1eebd 80 uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
b914e211
LP
81 + EXTRA_CMSG_SPACE /* kernel appears to require extra space */];
82 } control;
83 struct msghdr mh = {};
84 struct cmsghdr *cmsg;
85 socklen_t sl;
86 int r;
87
88 assert(s);
89
90 if (s->identified)
91 return 0;
92
93 /* Query the local side */
94 s->local_salen = sizeof(s->local);
95 r = getsockname(s->fd, &s->local.sa, &s->local_salen);
96 if (r < 0)
97 return -errno;
98 if (s->local.sa.sa_family == AF_INET6 && s->ifindex <= 0)
99 s->ifindex = s->local.in6.sin6_scope_id;
100
101 /* Query the remote side */
102 s->peer_salen = sizeof(s->peer);
103 r = getpeername(s->fd, &s->peer.sa, &s->peer_salen);
104 if (r < 0)
105 return -errno;
106 if (s->peer.sa.sa_family == AF_INET6 && s->ifindex <= 0)
107 s->ifindex = s->peer.in6.sin6_scope_id;
108
109 /* Check consistency */
110 assert(s->peer.sa.sa_family == s->local.sa.sa_family);
111 assert(IN_SET(s->peer.sa.sa_family, AF_INET, AF_INET6));
112
113 /* Query connection meta information */
114 sl = sizeof(control);
115 if (s->peer.sa.sa_family == AF_INET) {
116 r = getsockopt(s->fd, IPPROTO_IP, IP_PKTOPTIONS, &control, &sl);
117 if (r < 0)
118 return -errno;
119 } else if (s->peer.sa.sa_family == AF_INET6) {
120
121 r = getsockopt(s->fd, IPPROTO_IPV6, IPV6_2292PKTOPTIONS, &control, &sl);
122 if (r < 0)
123 return -errno;
124 } else
125 return -EAFNOSUPPORT;
126
127 mh.msg_control = &control;
128 mh.msg_controllen = sl;
2a1288ff
LP
129
130 CMSG_FOREACH(cmsg, &mh) {
b914e211
LP
131
132 if (cmsg->cmsg_level == IPPROTO_IPV6) {
133 assert(s->peer.sa.sa_family == AF_INET6);
134
135 switch (cmsg->cmsg_type) {
136
137 case IPV6_PKTINFO: {
138 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
139
140 if (s->ifindex <= 0)
141 s->ifindex = i->ipi6_ifindex;
142 break;
143 }
144
145 case IPV6_HOPLIMIT:
146 s->ttl = *(int *) CMSG_DATA(cmsg);
147 break;
148 }
149
150 } else if (cmsg->cmsg_level == IPPROTO_IP) {
151 assert(s->peer.sa.sa_family == AF_INET);
152
153 switch (cmsg->cmsg_type) {
154
155 case IP_PKTINFO: {
156 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
157
158 if (s->ifindex <= 0)
159 s->ifindex = i->ipi_ifindex;
160 break;
161 }
162
163 case IP_TTL:
164 s->ttl = *(int *) CMSG_DATA(cmsg);
165 break;
166 }
167 }
168 }
169
170 /* The Linux kernel sets the interface index to the loopback
171 * device if the connection came from the local host since it
172 * avoids the routing table in such a case. Let's unset the
173 * interface index in such a case. */
a5f03596 174 if (s->ifindex == LOOPBACK_IFINDEX)
b914e211
LP
175 s->ifindex = 0;
176
177 /* If we don't know the interface index still, we look for the
178 * first local interface with a matching address. Yuck! */
179 if (s->ifindex <= 0)
180 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);
181
182 if (s->protocol == DNS_PROTOCOL_LLMNR && s->ifindex > 0) {
183 uint32_t ifindex = htobe32(s->ifindex);
184
185 /* Make sure all packets for this connection are sent on the same interface */
186 if (s->local.sa.sa_family == AF_INET) {
187 r = setsockopt(s->fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex));
188 if (r < 0)
56f64d95 189 log_debug_errno(errno, "Failed to invoke IP_UNICAST_IF: %m");
b914e211
LP
190 } else if (s->local.sa.sa_family == AF_INET6) {
191 r = setsockopt(s->fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex, sizeof(ifindex));
192 if (r < 0)
56f64d95 193 log_debug_errno(errno, "Failed to invoke IPV6_UNICAST_IF: %m");
b914e211
LP
194 }
195 }
196
197 s->identified = true;
198
199 return 0;
200}
201
6016fcb0 202ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags) {
e6dc5556 203 ssize_t m;
91ccab1e
IT
204
205 assert(s);
206 assert(iov);
207
56ddbf10 208#if ENABLE_DNS_OVER_TLS
6016fcb0 209 if (s->encrypted && !(flags & DNS_STREAM_WRITE_TLS_DATA)) {
5d67a7ae
IT
210 ssize_t ss;
211 size_t i;
212
e6dc5556 213 m = 0;
5d67a7ae 214 for (i = 0; i < iovcnt; i++) {
6016fcb0
IT
215 ss = dnstls_stream_write(s, iov[i].iov_base, iov[i].iov_len);
216 if (ss < 0)
217 return ss;
5d67a7ae 218
e6dc5556 219 m += ss;
5d67a7ae
IT
220 if (ss != (ssize_t) iov[i].iov_len)
221 continue;
222 }
223 } else
224#endif
91ccab1e
IT
225 if (s->tfo_salen > 0) {
226 struct msghdr hdr = {
227 .msg_iov = (struct iovec*) iov,
228 .msg_iovlen = iovcnt,
229 .msg_name = &s->tfo_address.sa,
230 .msg_namelen = s->tfo_salen
231 };
232
e6dc5556
LP
233 m = sendmsg(s->fd, &hdr, MSG_FASTOPEN);
234 if (m < 0) {
91ccab1e
IT
235 if (errno == EOPNOTSUPP) {
236 s->tfo_salen = 0;
e6dc5556 237 if (connect(s->fd, &s->tfo_address.sa, s->tfo_salen) < 0)
91ccab1e
IT
238 return -errno;
239
e6dc5556
LP
240 return -EAGAIN;
241 }
242 if (errno == EINPROGRESS)
243 return -EAGAIN;
244
245 return -errno;
91ccab1e
IT
246 } else
247 s->tfo_salen = 0; /* connection is made */
f6c9c5f8 248 } else {
e6dc5556
LP
249 m = writev(s->fd, iov, iovcnt);
250 if (m < 0)
251 return -errno;
f6c9c5f8 252 }
91ccab1e 253
e6dc5556 254 return m;
91ccab1e
IT
255}
256
5d67a7ae
IT
257static ssize_t dns_stream_read(DnsStream *s, void *buf, size_t count) {
258 ssize_t ss;
259
56ddbf10 260#if ENABLE_DNS_OVER_TLS
6016fcb0
IT
261 if (s->encrypted)
262 ss = dnstls_stream_read(s, buf, count);
263 else
5d67a7ae 264#endif
f6c9c5f8 265 {
5d67a7ae 266 ss = read(s->fd, buf, count);
f6c9c5f8 267 if (ss < 0)
94fdb4d9 268 return -errno;
f6c9c5f8 269 }
5d67a7ae
IT
270
271 return ss;
272}
273
623a4c97
LP
274static int on_stream_timeout(sd_event_source *es, usec_t usec, void *userdata) {
275 DnsStream *s = userdata;
276
277 assert(s);
278
b914e211 279 return dns_stream_complete(s, ETIMEDOUT);
623a4c97
LP
280}
281
282static int on_stream_io(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
d973d94d 283 _cleanup_(dns_stream_unrefp) DnsStream *s = dns_stream_ref(userdata); /* Protect stream while we process it */
5971dffd 284 bool progressed = false;
623a4c97
LP
285 int r;
286
287 assert(s);
288
56ddbf10 289#if ENABLE_DNS_OVER_TLS
6016fcb0 290 if (s->encrypted) {
04c4d919 291 r = dnstls_stream_on_io(s, revents);
ba6aaf57 292 if (r == DNSTLS_STREAM_CLOSED)
6016fcb0 293 return 0;
b2cf6704 294 if (r == -EAGAIN)
ba6aaf57 295 return dns_stream_update_io(s);
b2cf6704 296 if (r < 0)
6016fcb0 297 return dns_stream_complete(s, -r);
b2cf6704
LP
298
299 r = dns_stream_update_io(s);
300 if (r < 0)
301 return r;
5d67a7ae
IT
302 }
303#endif
304
91ccab1e
IT
305 /* only identify after connecting */
306 if (s->tfo_salen == 0) {
307 r = dns_stream_identify(s);
308 if (r < 0)
309 return dns_stream_complete(s, -r);
310 }
b914e211 311
623a4c97
LP
312 if ((revents & EPOLLOUT) &&
313 s->write_packet &&
314 s->n_written < sizeof(s->write_size) + s->write_packet->size) {
315
316 struct iovec iov[2];
317 ssize_t ss;
318
5cfa2c3d
LP
319 iov[0] = IOVEC_MAKE(&s->write_size, sizeof(s->write_size));
320 iov[1] = IOVEC_MAKE(DNS_PACKET_DATA(s->write_packet), s->write_packet->size);
623a4c97
LP
321
322 IOVEC_INCREMENT(iov, 2, s->n_written);
323
5d67a7ae 324 ss = dns_stream_writev(s, iov, 2, 0);
623a4c97 325 if (ss < 0) {
f6c9c5f8
IT
326 if (!IN_SET(-ss, EINTR, EAGAIN))
327 return dns_stream_complete(s, -ss);
5971dffd
LP
328 } else {
329 progressed = true;
623a4c97 330 s->n_written += ss;
5971dffd 331 }
623a4c97
LP
332
333 /* Are we done? If so, disable the event source for EPOLLOUT */
334 if (s->n_written >= sizeof(s->write_size) + s->write_packet->size) {
335 r = dns_stream_update_io(s);
336 if (r < 0)
b914e211 337 return dns_stream_complete(s, -r);
623a4c97
LP
338 }
339 }
340
341 if ((revents & (EPOLLIN|EPOLLHUP|EPOLLRDHUP)) &&
342 (!s->read_packet ||
343 s->n_read < sizeof(s->read_size) + s->read_packet->size)) {
344
345 if (s->n_read < sizeof(s->read_size)) {
346 ssize_t ss;
347
5d67a7ae 348 ss = dns_stream_read(s, (uint8_t*) &s->read_size + s->n_read, sizeof(s->read_size) - s->n_read);
623a4c97 349 if (ss < 0) {
f6c9c5f8
IT
350 if (!IN_SET(-ss, EINTR, EAGAIN))
351 return dns_stream_complete(s, -ss);
623a4c97 352 } else if (ss == 0)
b914e211 353 return dns_stream_complete(s, ECONNRESET);
5971dffd
LP
354 else {
355 progressed = true;
623a4c97 356 s->n_read += ss;
5971dffd 357 }
623a4c97
LP
358 }
359
360 if (s->n_read >= sizeof(s->read_size)) {
361
362 if (be16toh(s->read_size) < DNS_PACKET_HEADER_SIZE)
b914e211 363 return dns_stream_complete(s, EBADMSG);
623a4c97
LP
364
365 if (s->n_read < sizeof(s->read_size) + be16toh(s->read_size)) {
366 ssize_t ss;
367
368 if (!s->read_packet) {
51027656 369 r = dns_packet_new(&s->read_packet, s->protocol, be16toh(s->read_size), DNS_PACKET_SIZE_MAX);
623a4c97 370 if (r < 0)
b914e211 371 return dns_stream_complete(s, -r);
623a4c97
LP
372
373 s->read_packet->size = be16toh(s->read_size);
374 s->read_packet->ipproto = IPPROTO_TCP;
375 s->read_packet->family = s->peer.sa.sa_family;
376 s->read_packet->ttl = s->ttl;
377 s->read_packet->ifindex = s->ifindex;
378
379 if (s->read_packet->family == AF_INET) {
380 s->read_packet->sender.in = s->peer.in.sin_addr;
381 s->read_packet->sender_port = be16toh(s->peer.in.sin_port);
382 s->read_packet->destination.in = s->local.in.sin_addr;
383 s->read_packet->destination_port = be16toh(s->local.in.sin_port);
384 } else {
385 assert(s->read_packet->family == AF_INET6);
386 s->read_packet->sender.in6 = s->peer.in6.sin6_addr;
387 s->read_packet->sender_port = be16toh(s->peer.in6.sin6_port);
388 s->read_packet->destination.in6 = s->local.in6.sin6_addr;
389 s->read_packet->destination_port = be16toh(s->local.in6.sin6_port);
390
391 if (s->read_packet->ifindex == 0)
392 s->read_packet->ifindex = s->peer.in6.sin6_scope_id;
393 if (s->read_packet->ifindex == 0)
394 s->read_packet->ifindex = s->local.in6.sin6_scope_id;
395 }
396 }
397
5d67a7ae 398 ss = dns_stream_read(s,
623a4c97
LP
399 (uint8_t*) DNS_PACKET_DATA(s->read_packet) + s->n_read - sizeof(s->read_size),
400 sizeof(s->read_size) + be16toh(s->read_size) - s->n_read);
401 if (ss < 0) {
99521cab
YW
402 if (!IN_SET(-ss, EINTR, EAGAIN))
403 return dns_stream_complete(s, -ss);
623a4c97 404 } else if (ss == 0)
b914e211 405 return dns_stream_complete(s, ECONNRESET);
623a4c97
LP
406 else
407 s->n_read += ss;
408 }
409
410 /* Are we done? If so, disable the event source for EPOLLIN */
411 if (s->n_read >= sizeof(s->read_size) + be16toh(s->read_size)) {
623a4c97
LP
412 /* If there's a packet handler
413 * installed, call that. Note that
414 * this is optional... */
98767d75
IT
415 if (s->on_packet) {
416 r = s->on_packet(s);
417 if (r < 0)
418 return r;
419 }
420
421 r = dns_stream_update_io(s);
422 if (r < 0)
423 return dns_stream_complete(s, -r);
623a4c97
LP
424 }
425 }
426 }
427
9c9e0170
LP
428 /* Call "complete" callback if finished reading and writing one packet, and there's nothing else left
429 * to write. */
430 if (s->type == DNS_STREAM_LLMNR_SEND &&
431 (s->write_packet && s->n_written >= sizeof(s->write_size) + s->write_packet->size) &&
432 ordered_set_isempty(s->write_queue) &&
623a4c97 433 (s->read_packet && s->n_read >= sizeof(s->read_size) + s->read_packet->size))
b914e211 434 return dns_stream_complete(s, 0);
623a4c97 435
5971dffd
LP
436 /* If we did something, let's restart the timeout event source */
437 if (progressed && s->timeout_event_source) {
438 r = sd_event_source_set_time(s->timeout_event_source, now(clock_boottime_or_monotonic()) + DNS_STREAM_TIMEOUT_USEC);
439 if (r < 0)
440 log_warning_errno(errno, "Couldn't restart TCP connection timeout, ignoring: %m");
441 }
442
623a4c97
LP
443 return 0;
444}
445
8301aa0b 446static DnsStream *dns_stream_free(DnsStream *s) {
98767d75
IT
447 DnsPacket *p;
448 Iterator i;
449
8301aa0b 450 assert(s);
b30bf55d 451
623a4c97
LP
452 dns_stream_stop(s);
453
454 if (s->manager) {
455 LIST_REMOVE(streams, s->manager->dns_streams, s);
652ba568 456 s->manager->n_dns_streams[s->type]--;
623a4c97
LP
457 }
458
56ddbf10 459#if ENABLE_DNS_OVER_TLS
6016fcb0
IT
460 if (s->encrypted)
461 dnstls_stream_free(s);
5d67a7ae
IT
462#endif
463
98767d75
IT
464 ORDERED_SET_FOREACH(p, s->write_queue, i)
465 dns_packet_unref(ordered_set_remove(s->write_queue, p));
466
623a4c97
LP
467 dns_packet_unref(s->write_packet);
468 dns_packet_unref(s->read_packet);
98767d75
IT
469 dns_server_unref(s->server);
470
471 ordered_set_free(s->write_queue);
623a4c97 472
6b430fdb 473 return mfree(s);
623a4c97
LP
474}
475
8301aa0b 476DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsStream, dns_stream, dns_stream_free);
623a4c97 477
b27a32a0
LP
478int dns_stream_new(
479 Manager *m,
480 DnsStream **ret,
652ba568 481 DnsStreamType type,
b27a32a0
LP
482 DnsProtocol protocol,
483 int fd,
484 const union sockaddr_union *tfo_address) {
485
b30bf55d 486 _cleanup_(dns_stream_unrefp) DnsStream *s = NULL;
623a4c97
LP
487 int r;
488
489 assert(m);
499aa1d3 490 assert(ret);
652ba568
LP
491 assert(type >= 0);
492 assert(type < _DNS_STREAM_TYPE_MAX);
493 assert(protocol >= 0);
494 assert(protocol < _DNS_PROTOCOL_MAX);
623a4c97
LP
495 assert(fd >= 0);
496
652ba568 497 if (m->n_dns_streams[type] > DNS_STREAMS_MAX)
623a4c97
LP
498 return -EBUSY;
499
898892e8 500 s = new(DnsStream, 1);
623a4c97
LP
501 if (!s)
502 return -ENOMEM;
503
898892e8
LP
504 *s = (DnsStream) {
505 .n_ref = 1,
506 .fd = -1,
507 .protocol = protocol,
508 };
509
98767d75
IT
510 r = ordered_set_ensure_allocated(&s->write_queue, &dns_packet_hash_ops);
511 if (r < 0)
512 return r;
513
623a4c97
LP
514 r = sd_event_add_io(m->event, &s->io_event_source, fd, EPOLLIN, on_stream_io, s);
515 if (r < 0)
516 return r;
517
aa4a9deb
LP
518 (void) sd_event_source_set_description(s->io_event_source, "dns-stream-io");
519
9a015429
LP
520 r = sd_event_add_time(
521 m->event,
522 &s->timeout_event_source,
523 clock_boottime_or_monotonic(),
524 now(clock_boottime_or_monotonic()) + DNS_STREAM_TIMEOUT_USEC, 0,
525 on_stream_timeout, s);
623a4c97
LP
526 if (r < 0)
527 return r;
528
aa4a9deb
LP
529 (void) sd_event_source_set_description(s->timeout_event_source, "dns-stream-timeout");
530
623a4c97 531 LIST_PREPEND(streams, m->dns_streams, s);
652ba568 532 m->n_dns_streams[type]++;
623a4c97 533 s->manager = m;
08e254c8 534
623a4c97 535 s->fd = fd;
08e254c8 536
91ccab1e
IT
537 if (tfo_address) {
538 s->tfo_address = *tfo_address;
539 s->tfo_salen = tfo_address->sa.sa_family == AF_INET6 ? sizeof(tfo_address->in6) : sizeof(tfo_address->in);
540 }
541
1cc6c93a 542 *ret = TAKE_PTR(s);
623a4c97
LP
543
544 return 0;
545}
546
547int dns_stream_write_packet(DnsStream *s, DnsPacket *p) {
98767d75
IT
548 int r;
549
623a4c97 550 assert(s);
499aa1d3 551 assert(p);
623a4c97 552
98767d75
IT
553 r = ordered_set_put(s->write_queue, p);
554 if (r < 0)
555 return r;
623a4c97 556
98767d75 557 dns_packet_ref(p);
623a4c97
LP
558
559 return dns_stream_update_io(s);
560}
aa337a5e
LP
561
562DnsPacket *dns_stream_take_read_packet(DnsStream *s) {
563 assert(s);
564
565 if (!s->read_packet)
566 return NULL;
567
568 if (s->n_read < sizeof(s->read_size))
569 return NULL;
570
571 if (s->n_read < sizeof(s->read_size) + be16toh(s->read_size))
572 return NULL;
573
574 s->n_read = 0;
575 return TAKE_PTR(s->read_packet);
576}
808089ae
LP
577
578void dns_stream_detach(DnsStream *s) {
579 assert(s);
580
581 if (!s->server)
582 return;
583
584 if (s->server->stream != s)
585 return;
586
587 dns_server_unref_stream(s->server);
588}