]>
Commit | Line | Data |
---|---|---|
839a4a20 LP |
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 | ||
dd0bc0f1 | 25 | int dns_synthesize_ifindex(int ifindex) { |
839a4a20 LP |
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 | ||
dd0bc0f1 | 39 | int dns_synthesize_family(uint64_t flags) { |
839a4a20 LP |
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 | ||
dd0bc0f1 | 57 | DnsProtocol dns_synthesize_protocol(uint64_t flags) { |
839a4a20 | 58 | |
dd0bc0f1 | 59 | /* Similar as dns_synthesize_family() but does this for the |
839a4a20 LP |
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 | ||
1c02e7ba | 89 | rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, dns_resource_key_name(key)); |
839a4a20 LP |
90 | if (!rr) |
91 | return -ENOMEM; | |
92 | ||
93 | rr->a.in_addr.s_addr = htobe32(INADDR_LOOPBACK); | |
94 | ||
dd0bc0f1 | 95 | r = dns_answer_add(*answer, rr, dns_synthesize_ifindex(ifindex), DNS_ANSWER_AUTHENTICATED); |
839a4a20 LP |
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 | ||
1c02e7ba | 103 | rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, dns_resource_key_name(key)); |
839a4a20 LP |
104 | if (!rr) |
105 | return -ENOMEM; | |
106 | ||
107 | rr->aaaa.in6_addr = in6addr_loopback; | |
108 | ||
dd0bc0f1 | 109 | r = dns_answer_add(*answer, rr, dns_synthesize_ifindex(ifindex), DNS_ANSWER_AUTHENTICATED); |
839a4a20 LP |
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 | ||
1c02e7ba | 143 | r = answer_add_ptr(answer, dns_resource_key_name(key), "localhost", dns_synthesize_ifindex(ifindex), DNS_ANSWER_AUTHENTICATED); |
839a4a20 LP |
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 | ||
2855b6c3 | 189 | bool added = false; |
839a4a20 LP |
190 | unsigned j; |
191 | int r; | |
192 | ||
193 | assert(answer); | |
194 | assert(name); | |
195 | ||
196 | for (j = 0; j < n_addresses; j++) { | |
197 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
198 | ||
199 | if (af != AF_UNSPEC) { | |
200 | ||
201 | if (addresses[j].family != af) | |
202 | continue; | |
203 | ||
204 | if (match && !in_addr_equal(af, match, &addresses[j].address)) | |
205 | continue; | |
206 | } | |
207 | ||
208 | r = dns_answer_reserve(answer, 1); | |
209 | if (r < 0) | |
210 | return r; | |
211 | ||
212 | r = dns_resource_record_new_reverse(&rr, addresses[j].family, &addresses[j].address, name); | |
213 | if (r < 0) | |
214 | return r; | |
215 | ||
216 | r = dns_answer_add(*answer, rr, addresses[j].ifindex, DNS_ANSWER_AUTHENTICATED); | |
217 | if (r < 0) | |
218 | return r; | |
2855b6c3 LP |
219 | |
220 | added = true; | |
839a4a20 LP |
221 | } |
222 | ||
2855b6c3 | 223 | return added; |
839a4a20 LP |
224 | } |
225 | ||
226 | static int synthesize_system_hostname_rr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) { | |
227 | _cleanup_free_ struct local_address *addresses = NULL; | |
228 | int n = 0, af; | |
229 | ||
230 | assert(m); | |
231 | assert(key); | |
232 | assert(answer); | |
233 | ||
234 | af = dns_type_to_af(key->type); | |
235 | if (af >= 0) { | |
236 | n = local_addresses(m->rtnl, ifindex, af, &addresses); | |
237 | if (n < 0) | |
238 | return n; | |
239 | ||
240 | if (n == 0) { | |
241 | struct local_address buffer[2]; | |
242 | ||
243 | /* If we have no local addresses then use ::1 | |
244 | * and 127.0.0.2 as local ones. */ | |
245 | ||
3742095b | 246 | if (IN_SET(af, AF_INET, AF_UNSPEC)) |
839a4a20 LP |
247 | buffer[n++] = (struct local_address) { |
248 | .family = AF_INET, | |
dd0bc0f1 | 249 | .ifindex = dns_synthesize_ifindex(ifindex), |
839a4a20 LP |
250 | .address.in.s_addr = htobe32(0x7F000002), |
251 | }; | |
252 | ||
3742095b | 253 | if (IN_SET(af, AF_INET6, AF_UNSPEC)) |
839a4a20 LP |
254 | buffer[n++] = (struct local_address) { |
255 | .family = AF_INET6, | |
dd0bc0f1 | 256 | .ifindex = dns_synthesize_ifindex(ifindex), |
839a4a20 LP |
257 | .address.in6 = in6addr_loopback, |
258 | }; | |
259 | ||
3742095b AR |
260 | return answer_add_addresses_rr(answer, |
261 | dns_resource_key_name(key), | |
262 | buffer, n); | |
839a4a20 LP |
263 | } |
264 | } | |
265 | ||
1c02e7ba | 266 | return answer_add_addresses_rr(answer, dns_resource_key_name(key), addresses, n); |
839a4a20 LP |
267 | } |
268 | ||
269 | static int synthesize_system_hostname_ptr(Manager *m, int af, const union in_addr_union *address, int ifindex, DnsAnswer **answer) { | |
270 | _cleanup_free_ struct local_address *addresses = NULL; | |
2855b6c3 | 271 | bool added = false; |
839a4a20 LP |
272 | int n, r; |
273 | ||
274 | assert(m); | |
275 | assert(address); | |
276 | assert(answer); | |
277 | ||
278 | if (af == AF_INET && address->in.s_addr == htobe32(0x7F000002)) { | |
279 | ||
a4f3375d | 280 | /* Always map the IPv4 address 127.0.0.2 to the local hostname, in addition to "localhost": */ |
839a4a20 | 281 | |
a4f3375d LP |
282 | r = dns_answer_reserve(answer, 4); |
283 | if (r < 0) | |
284 | return r; | |
285 | ||
286 | r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", m->full_hostname, dns_synthesize_ifindex(ifindex), DNS_ANSWER_AUTHENTICATED); | |
839a4a20 LP |
287 | if (r < 0) |
288 | return r; | |
289 | ||
dd0bc0f1 | 290 | r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", m->llmnr_hostname, dns_synthesize_ifindex(ifindex), DNS_ANSWER_AUTHENTICATED); |
839a4a20 LP |
291 | if (r < 0) |
292 | return r; | |
293 | ||
dd0bc0f1 | 294 | r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", m->mdns_hostname, dns_synthesize_ifindex(ifindex), DNS_ANSWER_AUTHENTICATED); |
839a4a20 LP |
295 | if (r < 0) |
296 | return r; | |
297 | ||
dd0bc0f1 | 298 | r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", "localhost", dns_synthesize_ifindex(ifindex), DNS_ANSWER_AUTHENTICATED); |
839a4a20 LP |
299 | if (r < 0) |
300 | return r; | |
301 | ||
2855b6c3 | 302 | return 1; |
839a4a20 LP |
303 | } |
304 | ||
305 | n = local_addresses(m->rtnl, ifindex, af, &addresses); | |
2855b6c3 | 306 | if (n <= 0) |
839a4a20 LP |
307 | return n; |
308 | ||
a4f3375d LP |
309 | r = answer_add_addresses_ptr(answer, m->full_hostname, addresses, n, af, address); |
310 | if (r < 0) | |
311 | return r; | |
312 | if (r > 0) | |
313 | added = true; | |
314 | ||
839a4a20 LP |
315 | r = answer_add_addresses_ptr(answer, m->llmnr_hostname, addresses, n, af, address); |
316 | if (r < 0) | |
317 | return r; | |
2855b6c3 LP |
318 | if (r > 0) |
319 | added = true; | |
320 | ||
321 | r = answer_add_addresses_ptr(answer, m->mdns_hostname, addresses, n, af, address); | |
322 | if (r < 0) | |
323 | return r; | |
324 | if (r > 0) | |
325 | added = true; | |
839a4a20 | 326 | |
2855b6c3 | 327 | return added; |
839a4a20 LP |
328 | } |
329 | ||
330 | static int synthesize_gateway_rr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) { | |
331 | _cleanup_free_ struct local_address *addresses = NULL; | |
acf06088 | 332 | int n = 0, af, r; |
839a4a20 LP |
333 | |
334 | assert(m); | |
335 | assert(key); | |
336 | assert(answer); | |
337 | ||
338 | af = dns_type_to_af(key->type); | |
339 | if (af >= 0) { | |
340 | n = local_gateways(m->rtnl, ifindex, af, &addresses); | |
acf06088 LP |
341 | if (n <= 0) |
342 | return n; /* < 0 means: error; == 0 means we have no gateway */ | |
839a4a20 LP |
343 | } |
344 | ||
acf06088 LP |
345 | r = answer_add_addresses_rr(answer, dns_resource_key_name(key), addresses, n); |
346 | if (r < 0) | |
347 | return r; | |
348 | ||
349 | return 1; /* > 0 means: we have some gateway */ | |
839a4a20 LP |
350 | } |
351 | ||
352 | static int synthesize_gateway_ptr(Manager *m, int af, const union in_addr_union *address, int ifindex, DnsAnswer **answer) { | |
353 | _cleanup_free_ struct local_address *addresses = NULL; | |
354 | int n; | |
355 | ||
356 | assert(m); | |
357 | assert(address); | |
358 | assert(answer); | |
359 | ||
360 | n = local_gateways(m->rtnl, ifindex, af, &addresses); | |
2855b6c3 | 361 | if (n <= 0) |
839a4a20 LP |
362 | return n; |
363 | ||
5248e7e1 | 364 | return answer_add_addresses_ptr(answer, "_gateway", addresses, n, af, address); |
839a4a20 LP |
365 | } |
366 | ||
367 | int dns_synthesize_answer( | |
368 | Manager *m, | |
369 | DnsQuestion *q, | |
370 | int ifindex, | |
dd0bc0f1 | 371 | DnsAnswer **ret) { |
839a4a20 LP |
372 | |
373 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; | |
374 | DnsResourceKey *key; | |
acf06088 | 375 | bool found = false, nxdomain = false; |
839a4a20 LP |
376 | int r; |
377 | ||
378 | assert(m); | |
379 | assert(q); | |
380 | ||
381 | DNS_QUESTION_FOREACH(key, q) { | |
382 | union in_addr_union address; | |
383 | const char *name; | |
384 | int af; | |
385 | ||
4c701096 | 386 | if (!IN_SET(key->class, DNS_CLASS_IN, DNS_CLASS_ANY)) |
839a4a20 LP |
387 | continue; |
388 | ||
1c02e7ba | 389 | name = dns_resource_key_name(key); |
839a4a20 LP |
390 | |
391 | if (is_localhost(name)) { | |
392 | ||
393 | r = synthesize_localhost_rr(m, key, ifindex, &answer); | |
394 | if (r < 0) | |
395 | return log_error_errno(r, "Failed to synthesize localhost RRs: %m"); | |
396 | ||
397 | } else if (manager_is_own_hostname(m, name)) { | |
398 | ||
399 | r = synthesize_system_hostname_rr(m, key, ifindex, &answer); | |
400 | if (r < 0) | |
401 | return log_error_errno(r, "Failed to synthesize system hostname RRs: %m"); | |
402 | ||
403 | } else if (is_gateway_hostname(name)) { | |
404 | ||
405 | r = synthesize_gateway_rr(m, key, ifindex, &answer); | |
406 | if (r < 0) | |
407 | return log_error_errno(r, "Failed to synthesize gateway RRs: %m"); | |
acf06088 LP |
408 | if (r == 0) { /* if we have no gateway return NXDOMAIN */ |
409 | nxdomain = true; | |
410 | continue; | |
411 | } | |
839a4a20 LP |
412 | |
413 | } else if ((dns_name_endswith(name, "127.in-addr.arpa") > 0 && dns_name_equal(name, "2.0.0.127.in-addr.arpa") == 0) || | |
414 | 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) { | |
415 | ||
416 | r = synthesize_localhost_ptr(m, key, ifindex, &answer); | |
417 | if (r < 0) | |
418 | return log_error_errno(r, "Failed to synthesize localhost PTR RRs: %m"); | |
419 | ||
420 | } else if (dns_name_address(name, &af, &address) > 0) { | |
2855b6c3 | 421 | int v, w; |
839a4a20 | 422 | |
2855b6c3 LP |
423 | v = synthesize_system_hostname_ptr(m, af, &address, ifindex, &answer); |
424 | if (v < 0) | |
839a4a20 LP |
425 | return log_error_errno(r, "Failed to synthesize system hostname PTR RR: %m"); |
426 | ||
2855b6c3 LP |
427 | w = synthesize_gateway_ptr(m, af, &address, ifindex, &answer); |
428 | if (w < 0) | |
839a4a20 | 429 | return log_error_errno(r, "Failed to synthesize gateway hostname PTR RR: %m"); |
2855b6c3 LP |
430 | |
431 | if (v == 0 && w == 0) /* This IP address is neither a local one nor a gateway */ | |
432 | continue; | |
433 | ||
528e685e LP |
434 | } else |
435 | continue; | |
436 | ||
437 | found = true; | |
839a4a20 LP |
438 | } |
439 | ||
acf06088 | 440 | if (found) { |
839a4a20 | 441 | |
acf06088 LP |
442 | if (ret) { |
443 | *ret = answer; | |
444 | answer = NULL; | |
445 | } | |
446 | ||
447 | return 1; | |
448 | } else if (nxdomain) | |
449 | return -ENXIO; | |
839a4a20 | 450 | |
acf06088 | 451 | return 0; |
839a4a20 | 452 | } |