Debian Codegraph @

src/libsystemd-network/sd-dns-resolver.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "dns-packet.h"
5 #include "dns-resolver-internal.h"
6 #include "errno-util.h"
7 #include "siphash24.h"
8 #include "socket-netlink.h"
9 #include "string-util.h"
10 #include "strv.h"
11 #include "unaligned.h"
12
13 void sd_dns_resolver_done(sd_dns_resolver *res) {
14 assert(res);
15
16 res->auth_name = mfree(res->auth_name);
17 res->addrs = mfree(res->addrs);
18 res->dohpath = mfree(res->dohpath);
19 }
20
21 sd_dns_resolver *sd_dns_resolver_unref(sd_dns_resolver *res) {
22 if (!res)
23 return NULL;
24
25 sd_dns_resolver_done(res);
26 return mfree(res);
27 }
28
29 DEFINE_ARRAY_FREE_FUNC(dns_resolver_free_array, sd_dns_resolver, sd_dns_resolver_done);
30
31 int dns_resolver_prio_compare(const sd_dns_resolver *a, const sd_dns_resolver *b) {
32 return CMP(ASSERT_PTR(a)->priority, ASSERT_PTR(b)->priority);
33 }
34
35 int sd_dns_resolver_get_priority(sd_dns_resolver *res, uint16_t *ret_priority) {
36 assert_return(res, -EINVAL);
37 assert_return(ret_priority, -EINVAL);
38
39 *ret_priority = res->priority;
40 return 0;
41 }
42
43 int sd_dns_resolver_get_adn(sd_dns_resolver *res, const char **ret_adn) {
44 assert_return(res, -EINVAL);
45 assert_return(ret_adn, -EINVAL);
46
47 /* Without adn only Do53 can be supported */
48 if (!res->auth_name)
49 return -ENODATA;
50
51 *ret_adn = res->auth_name;
52 return 0;
53 }
54
55 int sd_dns_resolver_get_inet_addresses(sd_dns_resolver *res, const struct in_addr **ret_addrs, size_t
56 *ret_n_addrs) {
57 assert_return(res, -EINVAL);
58 assert_return(ret_addrs, -EINVAL);
59 assert_return(ret_n_addrs, -EINVAL);
60 assert_return(res->family == AF_INET, -EINVAL);
61
62 /* ADN-only mode has no addrs */
63 if (res->n_addrs == 0)
64 return -ENODATA;
65
66 struct in_addr *addrs = new(struct in_addr, res->n_addrs);
67 if (!addrs)
68 return -ENOMEM;
69
70 for (size_t i = 0; i < res->n_addrs; i++)
71 addrs[i] = res->addrs[i].in;
72 *ret_addrs = addrs;
73 *ret_n_addrs = res->n_addrs;
74
75 return 0;
76 }
77
78 int sd_dns_resolver_get_inet6_addresses(sd_dns_resolver *res, const struct in6_addr **ret_addrs, size_t
79 *ret_n_addrs) {
80 assert_return(res, -EINVAL);
81 assert_return(ret_addrs, -EINVAL);
82 assert_return(ret_n_addrs, -EINVAL);
83 assert_return(res->family == AF_INET6, -EINVAL);
84
85 /* ADN-only mode has no addrs */
86 if (res->n_addrs == 0)
87 return -ENODATA;
88
89 struct in6_addr *addrs = new(struct in6_addr, res->n_addrs);
90 if (!addrs)
91 return -ENOMEM;
92
93 for (size_t i = 0; i < res->n_addrs; i++)
94 addrs[i] = res->addrs[i].in6;
95 *ret_addrs = addrs;
96 *ret_n_addrs = res->n_addrs;
97
98 return 0;
99 }
100
101 int sd_dns_resolver_get_alpn(sd_dns_resolver *res, sd_dns_alpn_flags *ret_alpn) {
102 assert_return(res, -EINVAL);
103 assert_return(ret_alpn, -EINVAL);
104
105 /* ADN-only mode has no transports */
106 if (!res->transports)
107 return -ENODATA;
108
109 *ret_alpn = res->transports;
110 return 0;
111 }
112
113 int sd_dns_resolver_get_port(sd_dns_resolver *res, uint16_t *ret_port) {
114 assert_return(res, -EINVAL);
115 assert_return(ret_port, -EINVAL);
116
117 /* port = 0 is the default port */
118 *ret_port = res->port;
119 return 0;
120 }
121
122 int sd_dns_resolver_get_dohpath(sd_dns_resolver *res, const char **ret_dohpath) {
123 assert_return(res, -EINVAL);
124 assert_return(ret_dohpath, -EINVAL);
125
126 /* only present in DoH resolvers */
127 if (!res->dohpath)
128 return -ENODATA;
129
130 *ret_dohpath = res->dohpath;
131 return 0;
132 }
133
134 void siphash24_compress_resolver(const sd_dns_resolver *res, struct siphash *state) {
135 assert(res);
136
137 siphash24_compress_typesafe(res->priority, state);
138 siphash24_compress_typesafe(res->transports, state);
139 siphash24_compress_typesafe(res->port, state);
140
141 siphash24_compress_string(res->auth_name, state);
142 siphash24_compress_string(res->dohpath, state);
143
144 siphash24_compress_typesafe(res->family, state);
145 FOREACH_ARRAY(addr, res->addrs, res->n_addrs)
146 siphash24_compress_typesafe(*addr, state);
147 }
148
149 int dns_resolver_transports_to_strv(sd_dns_alpn_flags transports, char ***ret) {
150 _cleanup_strv_free_ char **ans = NULL;
151
152 assert(ret);
153
154 if (FLAGS_SET(transports, SD_DNS_ALPN_DO53)) {
155 /* Do53 has no ALPN, this flag is only for our own usage. */
156 }
157
158 if (FLAGS_SET(transports, SD_DNS_ALPN_HTTP_2_TLS))
159 if (strv_extend(&ans, "h2") < 0)
160 return -ENOMEM;
161 if (FLAGS_SET(transports, SD_DNS_ALPN_HTTP_3))
162 if (strv_extend(&ans, "h3") < 0)
163 return -ENOMEM;
164 if (FLAGS_SET(transports, SD_DNS_ALPN_DOT))
165 if (strv_extend(&ans, "dot") < 0)
166 return -ENOMEM;
167 if (FLAGS_SET(transports, SD_DNS_ALPN_DOQ))
168 if (strv_extend(&ans, "doq") < 0)
169 return -ENOMEM;
170
171 *ret = TAKE_PTR(ans);
172 return 0;
173 }
174
175 int dnr_parse_svc_params(const uint8_t *option, size_t len, sd_dns_resolver *resolver) {
176 size_t offset = 0;
177 int r;
178
179 assert(option || len == 0);
180 assert(resolver);
181
182 sd_dns_alpn_flags transports = 0;
183 uint16_t port = 0;
184 _cleanup_free_ char *dohpath = NULL;
185 bool alpn = false;
186
187 uint16_t lastkey = 0;
188 while (offset < len) {
189 if (offset + 4 > len)
190 return -EBADMSG;
191
192 uint16_t key = unaligned_read_be16(&option[offset]);
193 offset += 2;
194
195 /* RFC9460 § 2.2 SvcParam MUST appear in strictly increasing numeric order */
196 if (lastkey >= key)
197 return -EBADMSG;
198 lastkey = key;
199
200 uint16_t plen = unaligned_read_be16(&option[offset]);
201 offset += 2;
202 if (offset + plen > len)
203 return -EBADMSG;
204
205 switch (key) {
206 /* Mandatory keys must be understood by the client, otherwise the record should be discarded.
207 * Automatic mandatory keys must not appear in the mandatory parameter, so these are all
208 * supplementary. We don't understand any supplementary keys, so if the mandatory parameter
209 * is present, we cannot use this record. */
210 case DNS_SVC_PARAM_KEY_MANDATORY:
211 if (plen > 0)
212 return -EBADMSG;
213 break;
214
215 case DNS_SVC_PARAM_KEY_ALPN:
216 if (plen == 0)
217 return 0;
218 alpn = true; /* alpn is required. Record that the requirement is met. */
219
220 size_t poff = offset;
221 size_t pend = offset + plen;
222 while (poff < pend) {
223 uint8_t alen = option[poff++];
224 if (poff + alen > len)
225 return -EBADMSG;
226 if (memcmp_nn(&option[poff], alen, "dot", STRLEN("dot")) == 0)
227 transports |= SD_DNS_ALPN_DOT;
228 if (memcmp_nn(&option[poff], alen, "h2", STRLEN("h2")) == 0)
229 transports |= SD_DNS_ALPN_HTTP_2_TLS;
230 if (memcmp_nn(&option[poff], alen, "h3", STRLEN("h3")) == 0)
231 transports |= SD_DNS_ALPN_HTTP_3;
232 if (memcmp_nn(&option[poff], alen, "doq", STRLEN("doq")) == 0)
233 transports |= SD_DNS_ALPN_DOQ;
234 poff += alen;
235 }
236 if (poff != pend)
237 return -EBADMSG;
238 break;
239
240 case DNS_SVC_PARAM_KEY_PORT:
241 if (plen != sizeof(uint16_t))
242 return -EBADMSG;
243 port = unaligned_read_be16(&option[offset]);
244 /* Server should indicate default port by omitting this param */
245 if (port == 0)
246 return -EBADMSG;
247 break;
248
249 /* RFC9463 § 5.1 service params MUST NOT include ipv4hint/ipv6hint */
250 case DNS_SVC_PARAM_KEY_IPV4HINT:
251 case DNS_SVC_PARAM_KEY_IPV6HINT:
252 return -EBADMSG;
253
254 case DNS_SVC_PARAM_KEY_DOHPATH:
255 r = make_cstring(&option[offset], plen,
256 MAKE_CSTRING_REFUSE_TRAILING_NUL, &dohpath);
257 if (ERRNO_IS_NEG_RESOURCE(r))
258 return r;
259 if (r < 0)
260 return -EBADMSG;
261 /* dohpath is a RFC6750 URI template. We don't parse these, but at least check the
262 * charset is reasonable. */
263 if (!in_charset(dohpath, URI_VALID "{}"))
264 return -EBADMSG;
265 break;
266
267 default:
268 ;
269 }
270 offset += plen;
271 }
272 if (offset != len)
273 return -EBADMSG;
274
275 /* DNR cannot be used without alpn */
276 if (!alpn)
277 return -EBADMSG;
278
279 /* RFC9461 § 5: If the [SvcParam] indicates support for HTTP, "dohpath" MUST be present. */
280 if (!dohpath && (FLAGS_SET(transports, SD_DNS_ALPN_HTTP_2_TLS) ||
281 FLAGS_SET(transports, SD_DNS_ALPN_HTTP_3)))
282 return -EBADMSG;
283
284 /* No useful transports */
285 if (!transports)
286 return 0;
287
288 resolver->transports = transports;
289 resolver->port = port;
290 free_and_replace(resolver->dohpath, dohpath);
291 return transports;
292 }
293
294 int dns_resolvers_to_dot_addrs(const sd_dns_resolver *resolvers, size_t n_resolvers,
295 struct in_addr_full ***ret_addrs, size_t *ret_n_addrs) {
296 assert(ret_addrs);
297 assert(ret_n_addrs);
298 assert(resolvers || n_resolvers == 0);
299
300 struct in_addr_full **addrs = NULL;
301 size_t n = 0;
302 CLEANUP_ARRAY(addrs, n, in_addr_full_free_array);
303
304 FOREACH_ARRAY(res, resolvers, n_resolvers) {
305 if (!FLAGS_SET(res->transports, SD_DNS_ALPN_DOT))
306 continue;
307
308 FOREACH_ARRAY(i, res->addrs, res->n_addrs) {
309 _cleanup_(in_addr_full_freep) struct in_addr_full *addr = NULL;
310 int r;
311
312 addr = new0(struct in_addr_full, 1);
313 if (!addr)
314 return -ENOMEM;
315 if (!GREEDY_REALLOC(addrs, n+1))
316 return -ENOMEM;
317
318 r = free_and_strdup(&addr->server_name, res->auth_name);
319 if (r < 0)
320 return r;
321 addr->family = res->family;
322 addr->port = res->port;
323 addr->address = *i;
324
325 addrs[n++] = TAKE_PTR(addr);
326 }
327 }
328
329 *ret_addrs = TAKE_PTR(addrs);
330 *ret_n_addrs = n;
331 return n;
332 }
333
334 int dns_resolvers_to_dot_strv(const sd_dns_resolver *resolvers, size_t n_resolvers, char ***ret_names) {
335 assert(ret_names);
336 int r;
337
338 _cleanup_strv_free_ char **names = NULL;
339 size_t len = 0;
340
341 struct in_addr_full **addrs = NULL;
342 size_t n = 0;
343 CLEANUP_ARRAY(addrs, n, in_addr_full_free_array);
344
345 r = dns_resolvers_to_dot_addrs(resolvers, n_resolvers, &addrs, &n);
346 if (r < 0)
347 return r;
348
349 FOREACH_ARRAY(addr, addrs, n) {
350 const char *name = in_addr_full_to_string(*addr);
351 if (!name)
352 return -ENOMEM;
353 r = strv_extend_with_size(&names, &len, name);
354 if (r < 0)
355 return r;
356 }
357
358 *ret_names = TAKE_PTR(names);
359 return len;
360 }