| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "dns-packet.h" |
| 5 |
#include "list.h" |
| 6 |
#include "resolved-dnstls.h" |
| 7 |
#include "resolved-forward.h" |
| 8 |
#include "socket-util.h" |
| 9 |
#include "sparse-endian.h" |
| 10 |
#include "time-util.h" |
| 11 |
|
| 12 |
/* Various timeouts for establishing TCP connections. First the default timeout for that. */ |
| 13 |
#define DNS_STREAM_DEFAULT_TIMEOUT_USEC (10 * USEC_PER_SEC) |
| 14 |
|
| 15 |
/* In the DNS stub, be more friendly for incoming connections, than we are to ourselves for outgoing ones */ |
| 16 |
#define DNS_STREAM_STUB_TIMEOUT_USEC (30 * USEC_PER_SEC) |
| 17 |
|
| 18 |
/* In opportunistic TLS mode, lower timeouts */ |
| 19 |
#define DNS_STREAM_OPPORTUNISTIC_TLS_TIMEOUT_USEC (3 * USEC_PER_SEC) |
| 20 |
|
| 21 |
/* Once connections are established apply this timeout once nothing happens anymore */ |
| 22 |
#define DNS_STREAM_ESTABLISHED_TIMEOUT_USEC (10 * USEC_PER_SEC) |
| 23 |
|
| 24 |
typedef enum DnsStreamType { |
| 25 |
DNS_STREAM_LOOKUP, /* Outgoing connection to a classic DNS server */ |
| 26 |
DNS_STREAM_LLMNR_SEND, /* Outgoing LLMNR TCP lookup */ |
| 27 |
DNS_STREAM_LLMNR_RECV, /* Incoming LLMNR TCP lookup */ |
| 28 |
DNS_STREAM_STUB, /* Incoming DNS stub connection */ |
| 29 |
_DNS_STREAM_TYPE_MAX, |
| 30 |
_DNS_STREAM_TYPE_INVALID = -EINVAL, |
| 31 |
} DnsStreamType; |
| 32 |
|
| 33 |
#define DNS_STREAM_WRITE_TLS_DATA 1 |
| 34 |
|
| 35 |
/* Streams are used by three subsystems: |
| 36 |
* |
| 37 |
* 1. The normal transaction logic when doing a DNS or LLMNR lookup via TCP |
| 38 |
* 2. The LLMNR logic when accepting a TCP-based lookup |
| 39 |
* 3. The DNS stub logic when accepting a TCP-based lookup |
| 40 |
*/ |
| 41 |
|
| 42 |
typedef struct DnsStream { |
| 43 |
Manager *manager; |
| 44 |
unsigned n_ref; |
| 45 |
|
| 46 |
DnsStreamType type; |
| 47 |
DnsProtocol protocol; |
| 48 |
|
| 49 |
int fd; |
| 50 |
union sockaddr_union peer; |
| 51 |
socklen_t peer_salen; |
| 52 |
union sockaddr_union local; |
| 53 |
socklen_t local_salen; |
| 54 |
int ifindex; |
| 55 |
uint32_t ttl; |
| 56 |
bool identified; |
| 57 |
bool packet_received; /* At least one packet is received. Used by LLMNR. */ |
| 58 |
uint32_t requested_events; |
| 59 |
|
| 60 |
/* only when using TCP fast open */ |
| 61 |
union sockaddr_union tfo_address; |
| 62 |
socklen_t tfo_salen; |
| 63 |
|
| 64 |
#if ENABLE_DNS_OVER_TLS |
| 65 |
DnsTlsStreamData dnstls_data; |
| 66 |
uint32_t dnstls_events; |
| 67 |
#endif |
| 68 |
|
| 69 |
sd_event_source *io_event_source; |
| 70 |
sd_event_source *timeout_event_source; |
| 71 |
|
| 72 |
be16_t write_size, read_size; |
| 73 |
DnsPacket *write_packet, *read_packet; |
| 74 |
size_t n_written, n_read; |
| 75 |
OrderedSet *write_queue; |
| 76 |
|
| 77 |
int (*on_packet)(DnsStream *s, DnsPacket *p); |
| 78 |
int (*complete)(DnsStream *s, int error); |
| 79 |
|
| 80 |
LIST_HEAD(DnsTransaction, transactions); /* when used by the transaction logic */ |
| 81 |
DnsServer *server; /* when used by the transaction logic */ |
| 82 |
Set *queries; /* when used by the DNS stub logic */ |
| 83 |
|
| 84 |
/* used when DNS-over-TLS is enabled */ |
| 85 |
bool encrypted:1; |
| 86 |
|
| 87 |
DnsStubListenerExtra *stub_listener_extra; |
| 88 |
|
| 89 |
LIST_FIELDS(DnsStream, streams); |
| 90 |
} DnsStream; |
| 91 |
|
| 92 |
int dns_stream_new( |
| 93 |
Manager *m, |
| 94 |
DnsStream **ret, |
| 95 |
DnsStreamType type, |
| 96 |
DnsProtocol protocol, |
| 97 |
int fd, |
| 98 |
const union sockaddr_union *tfo_address, |
| 99 |
int (on_packet)(DnsStream*, DnsPacket*), |
| 100 |
int (complete)(DnsStream*, int), /* optional */ |
| 101 |
usec_t connect_timeout_usec); |
| 102 |
#if ENABLE_DNS_OVER_TLS |
| 103 |
int dns_stream_connect_tls(DnsStream *s, void *tls_session); |
| 104 |
#endif |
| 105 |
DECLARE_TRIVIAL_REF_UNREF_FUNC(DnsStream, dns_stream); |
| 106 |
|
| 107 |
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref); |
| 108 |
|
| 109 |
int dns_stream_write_packet(DnsStream *s, DnsPacket *p); |
| 110 |
ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags); |
| 111 |
|
| 112 |
static inline bool DNS_STREAM_QUEUED(DnsStream *s) { |
| 113 |
assert(s); |
| 114 |
|
| 115 |
if (s->fd < 0) /* already stopped? */ |
| 116 |
return false; |
| 117 |
|
| 118 |
return !!s->write_packet; |
| 119 |
} |
| 120 |
|
| 121 |
void dns_stream_detach(DnsStream *s); |
| 122 |
int dns_stream_disconnect_all(Manager *m); |