1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <sys/stat.h>
5 
6 #include "sd-event.h"
7 #include "sd-netlink.h"
8 #include "sd-network.h"
9 
10 #include "hashmap.h"
11 #include "list.h"
12 #include "ordered-set.h"
13 #include "resolve-util.h"
14 #include "varlink.h"
15 
16 typedef struct Manager Manager;
17 
18 #include "resolved-dns-query.h"
19 #include "resolved-dns-search-domain.h"
20 #include "resolved-dns-stream.h"
21 #include "resolved-dns-stub.h"
22 #include "resolved-dns-trust-anchor.h"
23 #include "resolved-link.h"
24 #include "resolved-socket-graveyard.h"
25 
26 #define MANAGER_SEARCH_DOMAINS_MAX 256
27 #define MANAGER_DNS_SERVERS_MAX 256
28 
29 typedef struct EtcHosts {
30         Hashmap *by_address;
31         Hashmap *by_name;
32         Set *no_address;
33 } EtcHosts;
34 
35 struct Manager {
36         sd_event *event;
37 
38         ResolveSupport llmnr_support;
39         ResolveSupport mdns_support;
40         DnssecMode dnssec_mode;
41         DnsOverTlsMode dns_over_tls_mode;
42         DnsCacheMode enable_cache;
43         bool cache_from_localhost;
44         DnsStubListenerMode dns_stub_listener_mode;
45 
46 #if ENABLE_DNS_OVER_TLS
47         DnsTlsManagerData dnstls_data;
48 #endif
49 
50         /* Network */
51         Hashmap *links;
52 
53         sd_netlink *rtnl;
54         sd_event_source *rtnl_event_source;
55 
56         sd_network_monitor *network_monitor;
57         sd_event_source *network_event_source;
58 
59         /* DNS query management */
60         Hashmap *dns_transactions;
61         LIST_HEAD(DnsQuery, dns_queries);
62         unsigned n_dns_queries;
63         Hashmap *stub_queries_by_packet;
64 
65         LIST_HEAD(DnsStream, dns_streams);
66         unsigned n_dns_streams[_DNS_STREAM_TYPE_MAX];
67 
68         /* Unicast dns */
69         LIST_HEAD(DnsServer, dns_servers);
70         LIST_HEAD(DnsServer, fallback_dns_servers);
71         unsigned n_dns_servers; /* counts both main and fallback */
72         DnsServer *current_dns_server;
73 
74         LIST_HEAD(DnsSearchDomain, search_domains);
75         unsigned n_search_domains;
76 
77         bool need_builtin_fallbacks;
78         bool read_resolv_conf;
79         bool resolve_unicast_single_label;
80 
81         struct stat resolv_conf_stat;
82 
83         DnsTrustAnchor trust_anchor;
84 
85         LIST_HEAD(DnsScope, dns_scopes);
86         DnsScope *unicast_scope;
87 
88         /* LLMNR */
89         int llmnr_ipv4_udp_fd;
90         int llmnr_ipv6_udp_fd;
91         int llmnr_ipv4_tcp_fd;
92         int llmnr_ipv6_tcp_fd;
93 
94         sd_event_source *llmnr_ipv4_udp_event_source;
95         sd_event_source *llmnr_ipv6_udp_event_source;
96         sd_event_source *llmnr_ipv4_tcp_event_source;
97         sd_event_source *llmnr_ipv6_tcp_event_source;
98 
99         /* mDNS */
100         int mdns_ipv4_fd;
101         int mdns_ipv6_fd;
102         sd_event_source *mdns_ipv4_event_source;
103         sd_event_source *mdns_ipv6_event_source;
104 
105         /* DNS-SD */
106         Hashmap *dnssd_services;
107 
108         /* dbus */
109         sd_bus *bus;
110 
111         /* The hostname we publish on LLMNR and mDNS */
112         char *full_hostname;
113         char *llmnr_hostname;
114         char *mdns_hostname;
115         DnsResourceKey *llmnr_host_ipv4_key;
116         DnsResourceKey *llmnr_host_ipv6_key;
117         DnsResourceKey *mdns_host_ipv4_key;
118         DnsResourceKey *mdns_host_ipv6_key;
119 
120         /* Watch the system hostname */
121         int hostname_fd;
122         sd_event_source *hostname_event_source;
123 
124         sd_event_source *sigusr1_event_source;
125         sd_event_source *sigusr2_event_source;
126         sd_event_source *sigrtmin1_event_source;
127 
128         unsigned n_transactions_total;
129         unsigned n_dnssec_verdict[_DNSSEC_VERDICT_MAX];
130 
131         /* Data from /etc/hosts */
132         EtcHosts etc_hosts;
133         usec_t etc_hosts_last;
134         struct stat etc_hosts_stat;
135         bool read_etc_hosts;
136 
137         OrderedSet *dns_extra_stub_listeners;
138 
139         /* Local DNS stub on 127.0.0.53:53 */
140         sd_event_source *dns_stub_udp_event_source;
141         sd_event_source *dns_stub_tcp_event_source;
142 
143         /* Local DNS proxy stub on 127.0.0.54:53 */
144         sd_event_source *dns_proxy_stub_udp_event_source;
145         sd_event_source *dns_proxy_stub_tcp_event_source;
146 
147         Hashmap *polkit_registry;
148 
149         VarlinkServer *varlink_server;
150 
151         sd_event_source *clock_change_event_source;
152 
153         LIST_HEAD(SocketGraveyard, socket_graveyard);
154         SocketGraveyard *socket_graveyard_oldest;
155         size_t n_socket_graveyard;
156 };
157 
158 /* Manager */
159 
160 int manager_new(Manager **ret);
161 Manager* manager_free(Manager *m);
162 
163 int manager_start(Manager *m);
164 
165 uint32_t manager_find_mtu(Manager *m);
166 
167 int manager_write(Manager *m, int fd, DnsPacket *p);
168 int manager_send(Manager *m, int fd, int ifindex, int family, const union in_addr_union *destination, uint16_t port, const union in_addr_union *source, DnsPacket *p);
169 int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret);
170 
171 int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr);
172 LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr);
173 
174 void manager_refresh_rrs(Manager *m);
175 int manager_next_hostname(Manager *m);
176 
177 bool manager_packet_from_local_address(Manager *m, DnsPacket *p);
178 bool manager_packet_from_our_transaction(Manager *m, DnsPacket *p);
179 
180 DnsScope* manager_find_scope(Manager *m, DnsPacket *p);
181 
182 void manager_verify_all(Manager *m);
183 
184 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
185 
186 /* For some reason we need some extra cmsg space on some kernels/archs. One of those days we need to figure out why */
187 #define EXTRA_CMSG_SPACE 1024
188 
189 int manager_is_own_hostname(Manager *m, const char *name);
190 
191 int manager_compile_dns_servers(Manager *m, OrderedSet **servers);
192 int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route);
193 
194 DnssecMode manager_get_dnssec_mode(Manager *m);
195 bool manager_dnssec_supported(Manager *m);
196 
197 DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m);
198 
199 void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key);
200 
201 bool manager_routable(Manager *m);
202 
203 void manager_flush_caches(Manager *m, int log_level);
204 void manager_reset_server_features(Manager *m);
205 
206 void manager_cleanup_saved_user(Manager *m);
207 
208 bool manager_next_dnssd_names(Manager *m);
209 
210 bool manager_server_is_stub(Manager *m, DnsServer *s);
211 
212 int socket_disable_pmtud(int fd, int af);
213