| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <netinet/in.h> |
| 4 |
|
| 5 |
#include "sd-event.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "dns-answer.h" |
| 9 |
#include "dns-domain.h" |
| 10 |
#include "dns-packet.h" |
| 11 |
#include "dns-question.h" |
| 12 |
#include "dns-rr.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "log.h" |
| 15 |
#include "resolved-dns-scope.h" |
| 16 |
#include "resolved-dns-transaction.h" |
| 17 |
#include "resolved-link.h" |
| 18 |
#include "resolved-manager.h" |
| 19 |
#include "resolved-mdns.h" |
| 20 |
#include "sort-util.h" |
| 21 |
#include "time-util.h" |
| 22 |
|
| 23 |
#define CLEAR_CACHE_FLUSH(x) (~MDNS_RR_CACHE_FLUSH_OR_QU & (x)) |
| 24 |
|
| 25 |
void manager_mdns_stop(Manager *m) { |
| 26 |
assert(m); |
| 27 |
|
| 28 |
m->mdns_ipv4_event_source = sd_event_source_disable_unref(m->mdns_ipv4_event_source); |
| 29 |
m->mdns_ipv4_fd = safe_close(m->mdns_ipv4_fd); |
| 30 |
|
| 31 |
m->mdns_ipv6_event_source = sd_event_source_disable_unref(m->mdns_ipv6_event_source); |
| 32 |
m->mdns_ipv6_fd = safe_close(m->mdns_ipv6_fd); |
| 33 |
} |
| 34 |
|
| 35 |
void manager_mdns_maybe_stop(Manager *m) { |
| 36 |
assert(m); |
| 37 |
|
| 38 |
/* This stops mDNS only when no interface enables mDNS. */ |
| 39 |
|
| 40 |
Link *l; |
| 41 |
HASHMAP_FOREACH(l, m->links) |
| 42 |
if (link_get_mdns_support(l) != RESOLVE_SUPPORT_NO) |
| 43 |
return; |
| 44 |
|
| 45 |
manager_mdns_stop(m); |
| 46 |
} |
| 47 |
|
| 48 |
int manager_mdns_start(Manager *m) { |
| 49 |
int r; |
| 50 |
|
| 51 |
assert(m); |
| 52 |
|
| 53 |
if (m->mdns_support == RESOLVE_SUPPORT_NO) |
| 54 |
return 0; |
| 55 |
|
| 56 |
r = manager_mdns_ipv4_fd(m); |
| 57 |
if (r == -EADDRINUSE) |
| 58 |
goto eaddrinuse; |
| 59 |
if (r < 0) |
| 60 |
return r; |
| 61 |
|
| 62 |
if (socket_ipv6_is_enabled()) { |
| 63 |
r = manager_mdns_ipv6_fd(m); |
| 64 |
if (r == -EADDRINUSE) |
| 65 |
goto eaddrinuse; |
| 66 |
if (r < 0) |
| 67 |
return r; |
| 68 |
} |
| 69 |
|
| 70 |
return 0; |
| 71 |
|
| 72 |
eaddrinuse: |
| 73 |
log_warning("Another mDNS responder prohibits binding the socket to the same port. Turning off mDNS support."); |
| 74 |
m->mdns_support = RESOLVE_SUPPORT_NO; |
| 75 |
manager_mdns_stop(m); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static int mdns_rr_compare(DnsResourceRecord * const *a, DnsResourceRecord * const *b) { |
| 81 |
DnsResourceRecord *x = *(DnsResourceRecord **) a, *y = *(DnsResourceRecord **) b; |
| 82 |
size_t m; |
| 83 |
int r; |
| 84 |
|
| 85 |
assert(x); |
| 86 |
assert(y); |
| 87 |
|
| 88 |
r = CMP(CLEAR_CACHE_FLUSH(x->key->class), CLEAR_CACHE_FLUSH(y->key->class)); |
| 89 |
if (r != 0) |
| 90 |
return r; |
| 91 |
|
| 92 |
r = CMP(x->key->type, y->key->type); |
| 93 |
if (r != 0) |
| 94 |
return r; |
| 95 |
|
| 96 |
r = dns_resource_record_to_wire_format(x, false); |
| 97 |
if (r < 0) { |
| 98 |
log_warning_errno(r, "Can't wire-format RR: %m"); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
r = dns_resource_record_to_wire_format(y, false); |
| 103 |
if (r < 0) { |
| 104 |
log_warning_errno(r, "Can't wire-format RR: %m"); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
m = MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y)); |
| 109 |
|
| 110 |
r = memcmp(DNS_RESOURCE_RECORD_RDATA(x), DNS_RESOURCE_RECORD_RDATA(y), m); |
| 111 |
if (r != 0) |
| 112 |
return r; |
| 113 |
|
| 114 |
return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y)); |
| 115 |
} |
| 116 |
|
| 117 |
static int proposed_rrs_cmp(DnsResourceRecord **x, unsigned x_size, DnsResourceRecord **y, unsigned y_size) { |
| 118 |
unsigned m; |
| 119 |
int r; |
| 120 |
|
| 121 |
m = MIN(x_size, y_size); |
| 122 |
for (unsigned i = 0; i < m; i++) { |
| 123 |
r = mdns_rr_compare(&x[i], &y[i]); |
| 124 |
if (r != 0) |
| 125 |
return r; |
| 126 |
} |
| 127 |
|
| 128 |
return CMP(x_size, y_size); |
| 129 |
} |
| 130 |
|
| 131 |
static int mdns_packet_extract_matching_rrs(DnsPacket *p, DnsResourceKey *key, DnsResourceRecord ***ret_rrs) { |
| 132 |
_cleanup_free_ DnsResourceRecord **list = NULL; |
| 133 |
size_t i, n = 0, size = 0; |
| 134 |
DnsResourceRecord *rr; |
| 135 |
int r; |
| 136 |
|
| 137 |
assert(p); |
| 138 |
assert(key); |
| 139 |
assert(ret_rrs); |
| 140 |
assert_return(DNS_PACKET_NSCOUNT(p) > 0, -EINVAL); |
| 141 |
|
| 142 |
i = 0; |
| 143 |
DNS_ANSWER_FOREACH(rr, p->answer) { |
| 144 |
if (i >= DNS_PACKET_ANCOUNT(p) && i < DNS_PACKET_ANCOUNT(p) + DNS_PACKET_NSCOUNT(p)) { |
| 145 |
r = dns_resource_key_match_rr(key, rr, NULL); |
| 146 |
if (r < 0) |
| 147 |
return r; |
| 148 |
if (r > 0) |
| 149 |
size++; |
| 150 |
} |
| 151 |
i++; |
| 152 |
} |
| 153 |
|
| 154 |
if (size == 0) { |
| 155 |
*ret_rrs = NULL; |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
list = new(DnsResourceRecord *, size); |
| 160 |
if (!list) |
| 161 |
return -ENOMEM; |
| 162 |
|
| 163 |
i = 0; |
| 164 |
DNS_ANSWER_FOREACH(rr, p->answer) { |
| 165 |
if (i >= DNS_PACKET_ANCOUNT(p) && i < DNS_PACKET_ANCOUNT(p) + DNS_PACKET_NSCOUNT(p)) { |
| 166 |
r = dns_resource_key_match_rr(key, rr, NULL); |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
if (r > 0) |
| 170 |
list[n++] = rr; |
| 171 |
} |
| 172 |
i++; |
| 173 |
} |
| 174 |
|
| 175 |
assert(n == size); |
| 176 |
typesafe_qsort(list, size, mdns_rr_compare); |
| 177 |
|
| 178 |
*ret_rrs = TAKE_PTR(list); |
| 179 |
|
| 180 |
return size; |
| 181 |
} |
| 182 |
|
| 183 |
static int mdns_do_tiebreak(DnsResourceKey *key, DnsAnswer *answer, DnsPacket *p) { |
| 184 |
_cleanup_free_ DnsResourceRecord **our = NULL, **remote = NULL; |
| 185 |
DnsResourceRecord *rr; |
| 186 |
size_t i = 0, size; |
| 187 |
int r; |
| 188 |
|
| 189 |
size = dns_answer_size(answer); |
| 190 |
our = new(DnsResourceRecord *, size); |
| 191 |
if (!our) |
| 192 |
return -ENOMEM; |
| 193 |
|
| 194 |
DNS_ANSWER_FOREACH(rr, answer) |
| 195 |
our[i++] = rr; |
| 196 |
|
| 197 |
typesafe_qsort(our, size, mdns_rr_compare); |
| 198 |
|
| 199 |
r = mdns_packet_extract_matching_rrs(p, key, &remote); |
| 200 |
if (r < 0) |
| 201 |
return r; |
| 202 |
|
| 203 |
if (proposed_rrs_cmp(remote, r, our, size) > 0) |
| 204 |
return 1; |
| 205 |
|
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
static bool mdns_should_reply_using_unicast(DnsPacket *p) { |
| 210 |
DnsQuestionItem *item; |
| 211 |
|
| 212 |
/* Work out if we should respond using multicast or unicast. */ |
| 213 |
|
| 214 |
/* The query was a legacy "one-shot mDNS query", RFC 6762, sections 5.1 and 6.7 */ |
| 215 |
if (p->sender_port != MDNS_PORT) |
| 216 |
return true; |
| 217 |
|
| 218 |
/* The query was a "direct unicast query", RFC 6762, section 5.5 */ |
| 219 |
switch (p->family) { |
| 220 |
case AF_INET: |
| 221 |
if (!in4_addr_equal(&p->destination.in, &MDNS_MULTICAST_IPV4_ADDRESS)) |
| 222 |
return true; |
| 223 |
break; |
| 224 |
case AF_INET6: |
| 225 |
if (!in6_addr_equal(&p->destination.in6, &MDNS_MULTICAST_IPV6_ADDRESS)) |
| 226 |
return true; |
| 227 |
break; |
| 228 |
} |
| 229 |
|
| 230 |
/* All the questions in the query had a QU bit set, RFC 6762, section 5.4 */ |
| 231 |
DNS_QUESTION_FOREACH_ITEM(item, p->question) |
| 232 |
if (!FLAGS_SET(item->flags, DNS_QUESTION_WANTS_UNICAST_REPLY)) |
| 233 |
return false; |
| 234 |
|
| 235 |
return true; |
| 236 |
} |
| 237 |
|
| 238 |
static bool sender_on_local_subnet(DnsScope *s, DnsPacket *p) { |
| 239 |
int r; |
| 240 |
|
| 241 |
/* Check whether the sender is on a local subnet. */ |
| 242 |
|
| 243 |
if (!s->link) |
| 244 |
return false; |
| 245 |
|
| 246 |
LIST_FOREACH(addresses, a, s->link->addresses) { |
| 247 |
if (a->family != p->family) |
| 248 |
continue; |
| 249 |
if (a->prefixlen == UCHAR_MAX) /* don't know subnet mask */ |
| 250 |
continue; |
| 251 |
|
| 252 |
r = in_addr_prefix_covers(a->family, &a->in_addr, a->prefixlen, &p->sender); |
| 253 |
if (r < 0) |
| 254 |
log_debug_errno(r, "Failed to determine whether link address covers sender address: %m"); |
| 255 |
if (r > 0) |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
static int mdns_scope_process_query(DnsScope *s, DnsPacket *p) { |
| 263 |
_cleanup_(dns_answer_unrefp) DnsAnswer *full_answer = NULL; |
| 264 |
_cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL; |
| 265 |
DnsResourceKey *key = NULL; |
| 266 |
DnsResourceRecord *rr; |
| 267 |
bool tentative = false; |
| 268 |
bool legacy_query = p->sender_port != MDNS_PORT; |
| 269 |
bool unicast_reply; |
| 270 |
int r; |
| 271 |
|
| 272 |
assert(s); |
| 273 |
assert(p); |
| 274 |
|
| 275 |
r = dns_packet_extract(p); |
| 276 |
if (r < 0) |
| 277 |
return log_debug_errno(r, "Failed to extract resource records from incoming packet: %m"); |
| 278 |
|
| 279 |
/* TODO: Support Known-Answers only packets gracefully. */ |
| 280 |
if (dns_question_size(p->question) <= 0) |
| 281 |
return 0; |
| 282 |
|
| 283 |
unicast_reply = mdns_should_reply_using_unicast(p); |
| 284 |
if (unicast_reply && !sender_on_local_subnet(s, p)) { |
| 285 |
/* RFC 6762, section 5.5 recommends silently ignoring unicast queries |
| 286 |
* from senders outside the local network, so that we don't reveal our |
| 287 |
* internal network structure to outsiders. */ |
| 288 |
log_debug("Sender wants a unicast reply, but is not on a local subnet. Ignoring."); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
DNS_QUESTION_FOREACH(key, p->question) { |
| 293 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL; |
| 294 |
DnsAnswerItem *item; |
| 295 |
|
| 296 |
r = dns_zone_lookup(&s->zone, key, 0, &answer, &soa, &tentative); |
| 297 |
if (r < 0) |
| 298 |
return log_debug_errno(r, "Failed to look up key: %m"); |
| 299 |
|
| 300 |
if (tentative && DNS_PACKET_NSCOUNT(p) > 0) { |
| 301 |
/* |
| 302 |
* A race condition detected with the probe packet from |
| 303 |
* a remote host. |
| 304 |
* Do simultaneous probe tiebreaking as described in |
| 305 |
* RFC 6762, Section 8.2. In case we lost don't reply |
| 306 |
* the question and withdraw conflicting RRs. |
| 307 |
*/ |
| 308 |
r = mdns_do_tiebreak(key, answer, p); |
| 309 |
if (r < 0) |
| 310 |
return log_debug_errno(r, "Failed to do tiebreaking"); |
| 311 |
|
| 312 |
if (r > 0) { /* we lost */ |
| 313 |
DNS_ANSWER_FOREACH(rr, answer) { |
| 314 |
DnsZoneItem *i; |
| 315 |
|
| 316 |
i = dns_zone_get(&s->zone, rr); |
| 317 |
if (i) |
| 318 |
dns_zone_item_conflict(i); |
| 319 |
} |
| 320 |
|
| 321 |
continue; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if (dns_answer_isempty(answer)) |
| 326 |
continue; |
| 327 |
|
| 328 |
/* Copy answer items from full_answer to answer, tweaking them if needed. */ |
| 329 |
if (full_answer) { |
| 330 |
r = dns_answer_reserve(&full_answer, dns_answer_size(answer)); |
| 331 |
if (r < 0) |
| 332 |
return log_debug_errno(r, "Failed to reserve space in answer"); |
| 333 |
} else { |
| 334 |
full_answer = dns_answer_new(dns_answer_size(answer)); |
| 335 |
if (!full_answer) |
| 336 |
return log_oom(); |
| 337 |
} |
| 338 |
|
| 339 |
DNS_ANSWER_FOREACH_ITEM(item, answer) { |
| 340 |
DnsAnswerFlags flags = item->flags | DNS_ANSWER_REFUSE_TTL_NO_MATCH; |
| 341 |
/* The cache-flush bit must not be set in legacy unicast responses. |
| 342 |
* See section 6.7 of RFC 6762. */ |
| 343 |
if (legacy_query) |
| 344 |
flags &= ~DNS_ANSWER_CACHE_FLUSH; |
| 345 |
r = dns_answer_add(full_answer, item->rr, item->ifindex, flags, item->rrsig); |
| 346 |
if (r < 0) |
| 347 |
return log_debug_errno(r, "Failed to extend answer: %m"); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
if (dns_answer_isempty(full_answer)) |
| 352 |
return 0; |
| 353 |
|
| 354 |
r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, |
| 355 |
legacy_query ? p->question : NULL, full_answer, |
| 356 |
NULL, false, &reply); |
| 357 |
if (r < 0) |
| 358 |
return log_debug_errno(r, "Failed to build reply packet: %m"); |
| 359 |
|
| 360 |
if (!ratelimit_below(&s->ratelimit)) |
| 361 |
return 0; |
| 362 |
|
| 363 |
if (unicast_reply) { |
| 364 |
reply->destination = p->sender; |
| 365 |
reply->destination_port = p->sender_port; |
| 366 |
} |
| 367 |
r = dns_scope_emit_udp(s, -1, AF_UNSPEC, reply); |
| 368 |
if (r < 0) |
| 369 |
return log_debug_errno(r, "Failed to send reply packet: %m"); |
| 370 |
|
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
static int mdns_goodbye_callback(sd_event_source *s, uint64_t usec, void *userdata) { |
| 375 |
DnsScope *scope = userdata; |
| 376 |
int r; |
| 377 |
|
| 378 |
assert(s); |
| 379 |
assert(scope); |
| 380 |
|
| 381 |
scope->mdns_goodbye_event_source = sd_event_source_disable_unref(scope->mdns_goodbye_event_source); |
| 382 |
|
| 383 |
dns_cache_prune(&scope->cache); |
| 384 |
|
| 385 |
r = mdns_notify_browsers_goodbye(scope); |
| 386 |
if (r < 0) |
| 387 |
log_warning_errno(r, "mDNS: Failed to notify service subscribers of goodbyes, ignoring: %m"); |
| 388 |
|
| 389 |
if (dns_cache_expiry_in_one_second(&scope->cache, usec)) { |
| 390 |
r = sd_event_add_time_relative( |
| 391 |
scope->manager->event, |
| 392 |
&scope->mdns_goodbye_event_source, |
| 393 |
CLOCK_BOOTTIME, |
| 394 |
USEC_PER_SEC, |
| 395 |
/* accuracy= */ 0, |
| 396 |
mdns_goodbye_callback, |
| 397 |
scope); |
| 398 |
if (r < 0) |
| 399 |
return log_warning_errno(r, "mDNS: Failed to re-schedule goodbye callback, ignoring: %m"); |
| 400 |
} |
| 401 |
|
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
static int on_mdns_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 406 |
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; |
| 407 |
Manager *m = userdata; |
| 408 |
DnsScope *scope; |
| 409 |
int r; |
| 410 |
bool unsolicited_packet = true; |
| 411 |
|
| 412 |
r = manager_recv(m, fd, DNS_PROTOCOL_MDNS, &p); |
| 413 |
if (r <= 0) |
| 414 |
return r; |
| 415 |
|
| 416 |
/* Refuse traffic from the local host, to avoid query loops. However, allow legacy mDNS |
| 417 |
* unicast queries through anyway (we never send those ourselves, hence no risk). |
| 418 |
* i.e. check for the source port nr. */ |
| 419 |
if (p->sender_port == MDNS_PORT && manager_packet_from_local_address(m, p)) { |
| 420 |
log_debug("Got mDNS UDP packet from local host, ignoring."); |
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
scope = manager_find_scope(m, p); |
| 425 |
if (!scope) { |
| 426 |
log_debug("Got mDNS UDP packet on unknown scope. Ignoring."); |
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
if (dns_packet_validate_reply(p) > 0) { |
| 431 |
DnsResourceRecord *rr; |
| 432 |
|
| 433 |
/* RFC 6762 section 6: |
| 434 |
* The source UDP port in all Multicast DNS responses MUST be 5353 (the well-known port |
| 435 |
* assigned to mDNS). Multicast DNS implementations MUST silently ignore any Multicast DNS |
| 436 |
* responses they receive where the source UDP port is not 5353. */ |
| 437 |
if (p->sender_port != MDNS_PORT) { |
| 438 |
log_debug("Got mDNS reply from non-mDNS port %u (not %i), ignoring.", p->sender_port, MDNS_PORT); |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
log_debug("Got mDNS reply packet"); |
| 443 |
|
| 444 |
/* |
| 445 |
* mDNS is different from regular DNS and LLMNR with regard to handling responses. |
| 446 |
* While on other protocols, we can ignore every answer that doesn't match a question |
| 447 |
* we broadcast earlier, RFC6762, section 18.1 recommends looking at and caching all |
| 448 |
* incoming information, regardless of the DNS packet ID. |
| 449 |
* |
| 450 |
* Hence, extract the packet here, and try to find a transaction for answer the we got |
| 451 |
* and complete it. Also store the new information in scope's cache. |
| 452 |
*/ |
| 453 |
r = dns_packet_extract(p); |
| 454 |
if (r < 0) { |
| 455 |
log_debug("mDNS packet extraction failed."); |
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
dns_scope_check_conflicts(scope, p); |
| 460 |
|
| 461 |
DNS_ANSWER_FOREACH(rr, p->answer) { |
| 462 |
const char *name; |
| 463 |
|
| 464 |
name = dns_resource_key_name(rr->key); |
| 465 |
|
| 466 |
/* If the received reply packet contains ANY record that is not .local |
| 467 |
* or .in-addr.arpa or .ip6.arpa, we assume someone's playing tricks on |
| 468 |
* us and discard the packet completely. */ |
| 469 |
if (!(dns_name_endswith(name, "in-addr.arpa") > 0 || |
| 470 |
dns_name_endswith(name, "ip6.arpa") > 0 || |
| 471 |
dns_name_endswith(name, "local") > 0)) |
| 472 |
return 0; |
| 473 |
|
| 474 |
if (rr->ttl == 0) { |
| 475 |
log_debug("Got a goodbye packet"); |
| 476 |
/* See the section 10.1 of RFC6762 */ |
| 477 |
rr->ttl = 1; |
| 478 |
|
| 479 |
/* Look at the cache 1 second later and remove stale entries. |
| 480 |
* This is particularly useful to keep service browsers updated on service removal, |
| 481 |
* as there are no other reliable triggers to propagate that info. */ |
| 482 |
if (!scope->mdns_goodbye_event_source) { |
| 483 |
r = sd_event_add_time_relative( |
| 484 |
scope->manager->event, |
| 485 |
&scope->mdns_goodbye_event_source, |
| 486 |
CLOCK_BOOTTIME, |
| 487 |
USEC_PER_SEC, |
| 488 |
/* accuracy= */ 0, |
| 489 |
mdns_goodbye_callback, |
| 490 |
scope); |
| 491 |
if (r < 0) |
| 492 |
return r; |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
dns_cache_put( |
| 498 |
&scope->cache, |
| 499 |
scope->manager->enable_cache, |
| 500 |
DNS_PROTOCOL_MDNS, |
| 501 |
/* key= */ NULL, |
| 502 |
dns_packet_rcode(p), |
| 503 |
p->answer, |
| 504 |
/* full_packet= */ NULL, |
| 505 |
/* query_flags= */ false, |
| 506 |
_DNSSEC_RESULT_INVALID, |
| 507 |
/* nsec_ttl= */ UINT32_MAX, |
| 508 |
p->family, |
| 509 |
&p->sender, |
| 510 |
scope->manager->stale_retention_usec); |
| 511 |
|
| 512 |
for (bool match = true; match;) { |
| 513 |
match = false; |
| 514 |
LIST_FOREACH(transactions_by_scope, t, scope->transactions) { |
| 515 |
if (t->state != DNS_TRANSACTION_PENDING) |
| 516 |
continue; |
| 517 |
|
| 518 |
r = dns_answer_match_key(p->answer, dns_transaction_key(t), NULL); |
| 519 |
if (r <= 0) { |
| 520 |
if (r < 0) |
| 521 |
log_debug_errno(r, "Failed to match resource key, ignoring: %m"); |
| 522 |
continue; |
| 523 |
} |
| 524 |
|
| 525 |
unsolicited_packet = false; |
| 526 |
/* This packet matches the transaction, let's pass it on as reply */ |
| 527 |
dns_transaction_process_reply(t, p, false); |
| 528 |
|
| 529 |
/* The dns_transaction_process_reply() -> dns_transaction_complete() -> |
| 530 |
* dns_query_candidate_stop() may free multiple transactions. Hence, restart |
| 531 |
* the loop. */ |
| 532 |
match = true; |
| 533 |
break; |
| 534 |
} |
| 535 |
} |
| 536 |
/* Check if incoming packet key matches with active browse clients. If yes, update the same */ |
| 537 |
if (unsolicited_packet) |
| 538 |
mdns_notify_browsers_unsolicited_updates(m, p->answer, p->family); |
| 539 |
} else if (dns_packet_validate_query(p) > 0) { |
| 540 |
log_debug("Got mDNS query packet for id %u", DNS_PACKET_ID(p)); |
| 541 |
|
| 542 |
r = mdns_scope_process_query(scope, p); |
| 543 |
if (r < 0) { |
| 544 |
log_debug_errno(r, "mDNS query processing failed: %m"); |
| 545 |
return 0; |
| 546 |
} |
| 547 |
} else |
| 548 |
log_debug("Invalid mDNS UDP packet."); |
| 549 |
|
| 550 |
return 0; |
| 551 |
} |
| 552 |
|
| 553 |
int manager_mdns_ipv4_fd(Manager *m) { |
| 554 |
union sockaddr_union sa = { |
| 555 |
.in.sin_family = AF_INET, |
| 556 |
.in.sin_port = htobe16(MDNS_PORT), |
| 557 |
}; |
| 558 |
_cleanup_close_ int s = -EBADF; |
| 559 |
int r; |
| 560 |
|
| 561 |
assert(m); |
| 562 |
|
| 563 |
if (m->mdns_ipv4_fd >= 0) |
| 564 |
return m->mdns_ipv4_fd; |
| 565 |
|
| 566 |
s = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 567 |
if (s < 0) |
| 568 |
return log_error_errno(errno, "mDNS-IPv4: Failed to create socket: %m"); |
| 569 |
|
| 570 |
r = setsockopt_int(s, IPPROTO_IP, IP_TTL, 255); |
| 571 |
if (r < 0) |
| 572 |
return log_error_errno(r, "mDNS-IPv4: Failed to set IP_TTL: %m"); |
| 573 |
|
| 574 |
r = setsockopt_int(s, IPPROTO_IP, IP_MULTICAST_TTL, 255); |
| 575 |
if (r < 0) |
| 576 |
return log_error_errno(r, "mDNS-IPv4: Failed to set IP_MULTICAST_TTL: %m"); |
| 577 |
|
| 578 |
r = setsockopt_int(s, IPPROTO_IP, IP_MULTICAST_LOOP, true); |
| 579 |
if (r < 0) |
| 580 |
return log_error_errno(r, "mDNS-IPv4: Failed to set IP_MULTICAST_LOOP: %m"); |
| 581 |
|
| 582 |
r = setsockopt_int(s, IPPROTO_IP, IP_PKTINFO, true); |
| 583 |
if (r < 0) |
| 584 |
return log_error_errno(r, "mDNS-IPv4: Failed to set IP_PKTINFO: %m"); |
| 585 |
|
| 586 |
r = setsockopt_int(s, IPPROTO_IP, IP_RECVTTL, true); |
| 587 |
if (r < 0) |
| 588 |
return log_error_errno(r, "mDNS-IPv4: Failed to set IP_RECVTTL: %m"); |
| 589 |
|
| 590 |
/* Disable Don't-Fragment bit in the IP header */ |
| 591 |
r = setsockopt_int(s, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT); |
| 592 |
if (r < 0) |
| 593 |
return log_error_errno(r, "mDNS-IPv4: Failed to set IP_MTU_DISCOVER: %m"); |
| 594 |
|
| 595 |
/* See the section 15.1 of RFC6762 */ |
| 596 |
/* first try to bind without SO_REUSEADDR to detect another mDNS responder */ |
| 597 |
r = bind(s, &sa.sa, sizeof(sa.in)); |
| 598 |
if (r < 0) { |
| 599 |
if (errno != EADDRINUSE) |
| 600 |
return log_error_errno(errno, "mDNS-IPv4: Failed to bind socket: %m"); |
| 601 |
|
| 602 |
log_warning("mDNS-IPv4: There appears to be another mDNS responder running, or previously systemd-resolved crashed with some outstanding transfers."); |
| 603 |
|
| 604 |
/* try again with SO_REUSEADDR */ |
| 605 |
r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); |
| 606 |
if (r < 0) |
| 607 |
return log_error_errno(r, "mDNS-IPv4: Failed to set SO_REUSEADDR: %m"); |
| 608 |
|
| 609 |
r = bind(s, &sa.sa, sizeof(sa.in)); |
| 610 |
if (r < 0) |
| 611 |
return log_error_errno(errno, "mDNS-IPv4: Failed to bind socket: %m"); |
| 612 |
} else { |
| 613 |
/* enable SO_REUSEADDR for the case that the user really wants multiple mDNS responders */ |
| 614 |
r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); |
| 615 |
if (r < 0) |
| 616 |
return log_error_errno(r, "mDNS-IPv4: Failed to set SO_REUSEADDR: %m"); |
| 617 |
} |
| 618 |
|
| 619 |
r = sd_event_add_io(m->event, &m->mdns_ipv4_event_source, s, EPOLLIN, on_mdns_packet, m); |
| 620 |
if (r < 0) |
| 621 |
return log_error_errno(r, "mDNS-IPv4: Failed to create event source: %m"); |
| 622 |
|
| 623 |
(void) sd_event_source_set_description(m->mdns_ipv4_event_source, "mdns-ipv4"); |
| 624 |
|
| 625 |
return m->mdns_ipv4_fd = TAKE_FD(s); |
| 626 |
} |
| 627 |
|
| 628 |
int manager_mdns_ipv6_fd(Manager *m) { |
| 629 |
union sockaddr_union sa = { |
| 630 |
.in6.sin6_family = AF_INET6, |
| 631 |
.in6.sin6_port = htobe16(MDNS_PORT), |
| 632 |
}; |
| 633 |
_cleanup_close_ int s = -EBADF; |
| 634 |
int r; |
| 635 |
|
| 636 |
assert(m); |
| 637 |
|
| 638 |
if (m->mdns_ipv6_fd >= 0) |
| 639 |
return m->mdns_ipv6_fd; |
| 640 |
|
| 641 |
s = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 642 |
if (s < 0) |
| 643 |
return log_error_errno(errno, "mDNS-IPv6: Failed to create socket: %m"); |
| 644 |
|
| 645 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 255); |
| 646 |
if (r < 0) |
| 647 |
return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_UNICAST_HOPS: %m"); |
| 648 |
|
| 649 |
/* RFC 6762, section 11 recommends setting the TTL of UDP packets to 255. */ |
| 650 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255); |
| 651 |
if (r < 0) |
| 652 |
return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_MULTICAST_HOPS: %m"); |
| 653 |
|
| 654 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, true); |
| 655 |
if (r < 0) |
| 656 |
return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_MULTICAST_LOOP: %m"); |
| 657 |
|
| 658 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_V6ONLY, true); |
| 659 |
if (r < 0) |
| 660 |
return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_V6ONLY: %m"); |
| 661 |
|
| 662 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, true); |
| 663 |
if (r < 0) |
| 664 |
return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_RECVPKTINFO: %m"); |
| 665 |
|
| 666 |
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true); |
| 667 |
if (r < 0) |
| 668 |
return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_RECVHOPLIMIT: %m"); |
| 669 |
|
| 670 |
/* See the section 15.1 of RFC6762 */ |
| 671 |
/* first try to bind without SO_REUSEADDR to detect another mDNS responder */ |
| 672 |
r = bind(s, &sa.sa, sizeof(sa.in6)); |
| 673 |
if (r < 0) { |
| 674 |
if (errno != EADDRINUSE) |
| 675 |
return log_error_errno(errno, "mDNS-IPv6: Failed to bind socket: %m"); |
| 676 |
|
| 677 |
log_warning("mDNS-IPv6: There appears to be another mDNS responder running, or previously systemd-resolved crashed with some outstanding transfers."); |
| 678 |
|
| 679 |
/* try again with SO_REUSEADDR */ |
| 680 |
r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); |
| 681 |
if (r < 0) |
| 682 |
return log_error_errno(r, "mDNS-IPv6: Failed to set SO_REUSEADDR: %m"); |
| 683 |
|
| 684 |
r = bind(s, &sa.sa, sizeof(sa.in6)); |
| 685 |
if (r < 0) |
| 686 |
return log_error_errno(errno, "mDNS-IPv6: Failed to bind socket: %m"); |
| 687 |
} else { |
| 688 |
/* enable SO_REUSEADDR for the case that the user really wants multiple mDNS responders */ |
| 689 |
r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); |
| 690 |
if (r < 0) |
| 691 |
return log_error_errno(r, "mDNS-IPv6: Failed to set SO_REUSEADDR: %m"); |
| 692 |
} |
| 693 |
|
| 694 |
r = sd_event_add_io(m->event, &m->mdns_ipv6_event_source, s, EPOLLIN, on_mdns_packet, m); |
| 695 |
if (r < 0) |
| 696 |
return log_error_errno(r, "mDNS-IPv6: Failed to create event source: %m"); |
| 697 |
|
| 698 |
(void) sd_event_source_set_description(m->mdns_ipv6_event_source, "mdns-ipv6"); |
| 699 |
|
| 700 |
return m->mdns_ipv6_fd = TAKE_FD(s); |
| 701 |
} |