]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-synthesize.c
resolved: when synthesizing RR responses, own the name fully
[thirdparty/systemd.git] / src / resolve / resolved-dns-synthesize.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "hostname-util.h"
22 #include "local-addresses.h"
23 #include "resolved-dns-synthesize.h"
24
25 static int SYNTHESIZE_IFINDEX(int ifindex) {
26
27 /* When the caller asked for resolving on a specific
28 * interface, we synthesize the answer for that
29 * interface. However, if nothing specific was claimed and we
30 * only return localhost RRs, we synthesize the answer for
31 * localhost. */
32
33 if (ifindex > 0)
34 return ifindex;
35
36 return LOOPBACK_IFINDEX;
37 }
38
39 static int SYNTHESIZE_FAMILY(uint64_t flags) {
40
41 /* Picks an address family depending on set flags. This is
42 * purely for synthesized answers, where the family we return
43 * for the reply should match what was requested in the
44 * question, even though we are synthesizing the answer
45 * here. */
46
47 if (!(flags & SD_RESOLVED_DNS)) {
48 if (flags & (SD_RESOLVED_LLMNR_IPV4|SD_RESOLVED_MDNS_IPV4))
49 return AF_INET;
50 if (flags & (SD_RESOLVED_LLMNR_IPV6|SD_RESOLVED_MDNS_IPV6))
51 return AF_INET6;
52 }
53
54 return AF_UNSPEC;
55 }
56
57 static DnsProtocol SYNTHESIZE_PROTOCOL(uint64_t flags) {
58
59 /* Similar as SYNTHESIZE_FAMILY() but does this for the
60 * protocol. If resolving via DNS was requested, we claim it
61 * was DNS. Similar, if nothing specific was
62 * requested. However, if only resolving via LLMNR was
63 * requested we return that. */
64
65 if (flags & SD_RESOLVED_DNS)
66 return DNS_PROTOCOL_DNS;
67 if (flags & SD_RESOLVED_LLMNR)
68 return DNS_PROTOCOL_LLMNR;
69 if (flags & SD_RESOLVED_MDNS)
70 return DNS_PROTOCOL_MDNS;
71
72 return DNS_PROTOCOL_DNS;
73 }
74
75 static int synthesize_localhost_rr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) {
76 int r;
77
78 assert(m);
79 assert(key);
80 assert(answer);
81
82 r = dns_answer_reserve(answer, 2);
83 if (r < 0)
84 return r;
85
86 if (IN_SET(key->type, DNS_TYPE_A, DNS_TYPE_ANY)) {
87 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
88
89 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, DNS_RESOURCE_KEY_NAME(key));
90 if (!rr)
91 return -ENOMEM;
92
93 rr->a.in_addr.s_addr = htobe32(INADDR_LOOPBACK);
94
95 r = dns_answer_add(*answer, rr, SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
96 if (r < 0)
97 return r;
98 }
99
100 if (IN_SET(key->type, DNS_TYPE_AAAA, DNS_TYPE_ANY)) {
101 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
102
103 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, DNS_RESOURCE_KEY_NAME(key));
104 if (!rr)
105 return -ENOMEM;
106
107 rr->aaaa.in6_addr = in6addr_loopback;
108
109 r = dns_answer_add(*answer, rr, SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
110 if (r < 0)
111 return r;
112 }
113
114 return 0;
115 }
116
117 static int answer_add_ptr(DnsAnswer **answer, const char *from, const char *to, int ifindex, DnsAnswerFlags flags) {
118 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
119
120 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, from);
121 if (!rr)
122 return -ENOMEM;
123
124 rr->ptr.name = strdup(to);
125 if (!rr->ptr.name)
126 return -ENOMEM;
127
128 return dns_answer_add(*answer, rr, ifindex, flags);
129 }
130
131 static int synthesize_localhost_ptr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) {
132 int r;
133
134 assert(m);
135 assert(key);
136 assert(answer);
137
138 if (IN_SET(key->type, DNS_TYPE_PTR, DNS_TYPE_ANY)) {
139 r = dns_answer_reserve(answer, 1);
140 if (r < 0)
141 return r;
142
143 r = answer_add_ptr(answer, DNS_RESOURCE_KEY_NAME(key), "localhost", SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
144 if (r < 0)
145 return r;
146 }
147
148 return 0;
149 }
150
151 static int answer_add_addresses_rr(
152 DnsAnswer **answer,
153 const char *name,
154 struct local_address *addresses,
155 unsigned n_addresses) {
156
157 unsigned j;
158 int r;
159
160 assert(answer);
161 assert(name);
162
163 r = dns_answer_reserve(answer, n_addresses);
164 if (r < 0)
165 return r;
166
167 for (j = 0; j < n_addresses; j++) {
168 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
169
170 r = dns_resource_record_new_address(&rr, addresses[j].family, &addresses[j].address, name);
171 if (r < 0)
172 return r;
173
174 r = dns_answer_add(*answer, rr, addresses[j].ifindex, DNS_ANSWER_AUTHENTICATED);
175 if (r < 0)
176 return r;
177 }
178
179 return 0;
180 }
181
182 static int answer_add_addresses_ptr(
183 DnsAnswer **answer,
184 const char *name,
185 struct local_address *addresses,
186 unsigned n_addresses,
187 int af, const union in_addr_union *match) {
188
189 unsigned j;
190 int r;
191
192 assert(answer);
193 assert(name);
194
195 for (j = 0; j < n_addresses; j++) {
196 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
197
198 if (af != AF_UNSPEC) {
199
200 if (addresses[j].family != af)
201 continue;
202
203 if (match && !in_addr_equal(af, match, &addresses[j].address))
204 continue;
205 }
206
207 r = dns_answer_reserve(answer, 1);
208 if (r < 0)
209 return r;
210
211 r = dns_resource_record_new_reverse(&rr, addresses[j].family, &addresses[j].address, name);
212 if (r < 0)
213 return r;
214
215 r = dns_answer_add(*answer, rr, addresses[j].ifindex, DNS_ANSWER_AUTHENTICATED);
216 if (r < 0)
217 return r;
218 }
219
220 return 0;
221 }
222
223 static int synthesize_system_hostname_rr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) {
224 _cleanup_free_ struct local_address *addresses = NULL;
225 int n = 0, af;
226
227 assert(m);
228 assert(key);
229 assert(answer);
230
231 af = dns_type_to_af(key->type);
232 if (af >= 0) {
233 n = local_addresses(m->rtnl, ifindex, af, &addresses);
234 if (n < 0)
235 return n;
236
237 if (n == 0) {
238 struct local_address buffer[2];
239
240 /* If we have no local addresses then use ::1
241 * and 127.0.0.2 as local ones. */
242
243 if (af == AF_INET || af == AF_UNSPEC)
244 buffer[n++] = (struct local_address) {
245 .family = AF_INET,
246 .ifindex = SYNTHESIZE_IFINDEX(ifindex),
247 .address.in.s_addr = htobe32(0x7F000002),
248 };
249
250 if (af == AF_INET6 || af == AF_UNSPEC)
251 buffer[n++] = (struct local_address) {
252 .family = AF_INET6,
253 .ifindex = SYNTHESIZE_IFINDEX(ifindex),
254 .address.in6 = in6addr_loopback,
255 };
256
257 return answer_add_addresses_rr(answer, DNS_RESOURCE_KEY_NAME(key), buffer, n);
258 }
259 }
260
261 return answer_add_addresses_rr(answer, DNS_RESOURCE_KEY_NAME(key), addresses, n);
262 }
263
264 static int synthesize_system_hostname_ptr(Manager *m, int af, const union in_addr_union *address, int ifindex, DnsAnswer **answer) {
265 _cleanup_free_ struct local_address *addresses = NULL;
266 int n, r;
267
268 assert(m);
269 assert(address);
270 assert(answer);
271
272 if (af == AF_INET && address->in.s_addr == htobe32(0x7F000002)) {
273
274 /* Always map the IPv4 address 127.0.0.2 to the local
275 * hostname, in addition to "localhost": */
276
277 r = dns_answer_reserve(answer, 3);
278 if (r < 0)
279 return r;
280
281 r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", m->llmnr_hostname, SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
282 if (r < 0)
283 return r;
284
285 r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", m->mdns_hostname, SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
286 if (r < 0)
287 return r;
288
289 r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", "localhost", SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
290 if (r < 0)
291 return r;
292
293 return 0;
294 }
295
296 n = local_addresses(m->rtnl, ifindex, af, &addresses);
297 if (n < 0)
298 return n;
299
300 r = answer_add_addresses_ptr(answer, m->llmnr_hostname, addresses, n, af, address);
301 if (r < 0)
302 return r;
303
304 return answer_add_addresses_ptr(answer, m->mdns_hostname, addresses, n, af, address);
305 }
306
307 static int synthesize_gateway_rr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) {
308 _cleanup_free_ struct local_address *addresses = NULL;
309 int n = 0, af;
310
311 assert(m);
312 assert(key);
313 assert(answer);
314
315 af = dns_type_to_af(key->type);
316 if (af >= 0) {
317 n = local_gateways(m->rtnl, ifindex, af, &addresses);
318 if (n < 0)
319 return n;
320 }
321
322 return answer_add_addresses_rr(answer, DNS_RESOURCE_KEY_NAME(key), addresses, n);
323 }
324
325 static int synthesize_gateway_ptr(Manager *m, int af, const union in_addr_union *address, int ifindex, DnsAnswer **answer) {
326 _cleanup_free_ struct local_address *addresses = NULL;
327 int n;
328
329 assert(m);
330 assert(address);
331 assert(answer);
332
333 n = local_gateways(m->rtnl, ifindex, af, &addresses);
334 if (n < 0)
335 return n;
336
337 return answer_add_addresses_ptr(answer, "gateway", addresses, n, af, address);
338 }
339
340 int dns_synthesize_answer(
341 Manager *m,
342 DnsQuestion *q,
343 int ifindex,
344 uint64_t flags,
345 DnsAnswer **ret,
346 DnsProtocol *ret_protocol,
347 int *ret_family) {
348
349 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
350 DnsResourceKey *key;
351 bool found = false;
352 int r;
353
354 assert(m);
355 assert(q);
356
357 DNS_QUESTION_FOREACH(key, q) {
358 union in_addr_union address;
359 const char *name;
360 int af;
361
362 if (key->class != DNS_CLASS_IN &&
363 key->class != DNS_CLASS_ANY)
364 continue;
365
366 name = DNS_RESOURCE_KEY_NAME(key);
367
368 if (is_localhost(name)) {
369
370 r = synthesize_localhost_rr(m, key, ifindex, &answer);
371 if (r < 0)
372 return log_error_errno(r, "Failed to synthesize localhost RRs: %m");
373
374 } else if (manager_is_own_hostname(m, name)) {
375
376 r = synthesize_system_hostname_rr(m, key, ifindex, &answer);
377 if (r < 0)
378 return log_error_errno(r, "Failed to synthesize system hostname RRs: %m");
379
380 } else if (is_gateway_hostname(name)) {
381
382 r = synthesize_gateway_rr(m, key, ifindex, &answer);
383 if (r < 0)
384 return log_error_errno(r, "Failed to synthesize gateway RRs: %m");
385
386 } else if ((dns_name_endswith(name, "127.in-addr.arpa") > 0 && dns_name_equal(name, "2.0.0.127.in-addr.arpa") == 0) ||
387 dns_name_equal(name, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") > 0) {
388
389 r = synthesize_localhost_ptr(m, key, ifindex, &answer);
390 if (r < 0)
391 return log_error_errno(r, "Failed to synthesize localhost PTR RRs: %m");
392
393 } else if (dns_name_address(name, &af, &address) > 0) {
394
395 r = synthesize_system_hostname_ptr(m, af, &address, ifindex, &answer);
396 if (r < 0)
397 return log_error_errno(r, "Failed to synthesize system hostname PTR RR: %m");
398
399 r = synthesize_gateway_ptr(m, af, &address, ifindex, &answer);
400 if (r < 0)
401 return log_error_errno(r, "Failed to synthesize gateway hostname PTR RR: %m");
402 } else
403 continue;
404
405 found = true;
406 }
407
408 r = found;
409
410 if (ret) {
411 *ret = answer;
412 answer = NULL;
413 }
414
415 if (ret_protocol)
416 *ret_protocol = SYNTHESIZE_PROTOCOL(flags);
417
418 if (ret_family)
419 *ret_family = SYNTHESIZE_FAMILY(flags);
420
421 return r;
422 }