#define ADVERTISE_EXTRA_DATAGRAM_SIZE_MAX DNS_PACKET_UNICAST_SIZE_LARGE_MAX
static int manager_dns_stub_fd_extra(Manager *m, DnsStubListenerExtra *l, int type);
+static int manager_dns_stub_fd(Manager *m, int family, const union in_addr_union *listen_address, int type);
static void dns_stub_listener_extra_hash_func(const DnsStubListenerExtra *a, struct siphash *state) {
assert(a);
DnsQuestion *question,
bool with_rrsig) { /* Add RRSIG RR matching each RR */
- _cleanup_(dns_resource_key_unrefp) DnsResourceKey *redirected_key = NULL;
- unsigned n_cname_redirects = 0;
DnsAnswerItem *item;
int r;
assert(reply);
- /* Copies all RRs from 'answer' into 'reply', if they match 'question'. There might be direct and
- * indirect matches (i.e. via CNAME/DNAME). If they have an indirect one, remember where we need to
- * go, and restart the loop */
+ /* Copies all RRs from 'answer' into 'reply', if they match 'question'. */
- for (;;) {
- _cleanup_(dns_resource_key_unrefp) DnsResourceKey *next_redirected_key = NULL;
-
- DNS_ANSWER_FOREACH_ITEM(item, answer) {
- DnsResourceKey *k = NULL;
-
- if (redirected_key) {
- /* There was a redirect in this packet, let's collect all matching RRs for the redirect */
- r = dns_resource_key_match_rr(redirected_key, item->rr, NULL);
- if (r < 0)
- return r;
-
- k = redirected_key;
- } else if (question) {
- /* We have a question, let's see if this RR matches it */
- r = dns_question_matches_rr(question, item->rr, NULL);
- if (r < 0)
- return r;
-
- k = question->keys[0];
- } else
- r = 1; /* No question, everything matches */
-
- if (r == 0) {
- _cleanup_free_ char *target = NULL;
-
- /* OK, so the RR doesn't directly match. Let's see if the RR is a matching
- * CNAME or DNAME */
-
- assert(k);
-
- r = dns_resource_record_get_cname_target(k, item->rr, &target);
- if (r == -EUNATCH)
- continue; /* Not a CNAME/DNAME or doesn't match */
- if (r < 0)
- return r;
-
- /* Oh, wow, this is a redirect. Let's remember where this points, and store
- * it in 'next_redirected_key'. Once we finished iterating through the rest
- * of the RR's we'll start again, with the redirected RR key. */
-
- n_cname_redirects++;
- if (n_cname_redirects > CNAME_REDIRECT_MAX) /* don't loop forever */
- return -ELOOP;
-
- dns_resource_key_unref(next_redirected_key);
-
- /* There can only be one CNAME per name, hence no point in storing more than one here */
- next_redirected_key = dns_resource_key_new(k->class, k->type, target);
- if (!next_redirected_key)
- return -ENOMEM;
- }
-
- /* Mask the section info, we want the primary answers to always go without section info, so
- * that it is added to the answer section when we synthesize a reply. */
+ DNS_ANSWER_FOREACH_ITEM(item, answer) {
- r = reply_add_with_rrsig(
- reply,
- item->rr,
- item->ifindex,
- item->flags & ~DNS_ANSWER_MASK_SECTIONS,
- item->rrsig,
- with_rrsig);
+ /* We have a question, let's see if this RR matches it */
+ r = dns_question_matches_rr(question, item->rr, NULL);
+ if (r < 0)
+ return r;
+ if (!r) {
+ /* Maybe there's a CNAME/DNAME in here? If so, that's an answer too */
+ r = dns_question_matches_cname_or_dname(question, item->rr, NULL);
if (r < 0)
return r;
+ if (!r)
+ continue;
}
- if (!next_redirected_key)
- break;
+ /* Mask the section info, we want the primary answers to always go without section
+ * info, so that it is added to the answer section when we synthesize a reply. */
- dns_resource_key_unref(redirected_key);
- redirected_key = TAKE_PTR(next_redirected_key);
+ r = reply_add_with_rrsig(
+ reply,
+ item->rr,
+ item->ifindex,
+ item->flags & ~DNS_ANSWER_MASK_SECTIONS,
+ item->rrsig,
+ with_rrsig);
+ if (r < 0)
+ return r;
}
return 0;
dns_type_is_dnssec(item->rr->key->type))
continue;
- if (((item->flags ^ section) & (DNS_ANSWER_SECTION_ANSWER|DNS_ANSWER_SECTION_AUTHORITY|DNS_ANSWER_SECTION_ADDITIONAL)) != 0)
+ if (((item->flags ^ section) & DNS_ANSWER_MASK_SECTIONS) != 0)
continue;
r = reply_add_with_rrsig(
if (r < 0)
return r;
- /* Include all RRs that originate from the answer or authority sections, and aren't listed in the
+ /* Include all RRs that originate from the authority sections, and aren't already listed in the
* answer section, in the authority section */
r = dns_stub_collect_answer_by_section(
&q->reply_authoritative,
q->answer,
- DNS_ANSWER_SECTION_ANSWER,
+ DNS_ANSWER_SECTION_AUTHORITY,
q->reply_answer, NULL,
edns0_do);
if (r < 0)
return r;
+
+ /* Include all RRs that originate from the answer or additional sections in the additional section
+ * (except if already listed in the other two sections). Also add all RRs with no section marking. */
r = dns_stub_collect_answer_by_section(
- &q->reply_authoritative,
+ &q->reply_additional,
q->answer,
- DNS_ANSWER_SECTION_AUTHORITY,
- q->reply_answer, NULL,
+ DNS_ANSWER_SECTION_ANSWER,
+ q->reply_answer, q->reply_authoritative,
edns0_do);
if (r < 0)
return r;
-
- /* Include all RRs that originate from the additional sections in the additional section (except if
- * already listed in the other two sections). Also add all RRs with no section marking. */
r = dns_stub_collect_answer_by_section(
&q->reply_additional,
q->answer,
int rcode,
bool tc, /* set the Truncated bit? */
bool aa, /* set the Authoritative Answer bit? */
+ bool rd, /* set the Recursion Desired bit? */
bool add_opt, /* add an OPT RR to this packet? */
bool edns0_do, /* set the EDNS0 DNSSEC OK bit? */
bool ad, /* set the DNSSEC authenticated data bit? */
0 /* opcode */,
aa /* aa */,
tc /* tc */,
- 1 /* rd */,
+ rd /* rd */,
1 /* ra */,
ad /* ad */,
cd /* cd */,
return 0;
}
+static bool address_is_proxy(int family, const union in_addr_union *a) {
+ assert(a);
+
+ /* Returns true if the specified address is the DNS "proxy" stub, i.e. where we unconditionally enable bypass mode */
+
+ if (family != AF_INET)
+ return false;
+
+ return be32toh(a->in.s_addr) == INADDR_DNS_PROXY_STUB;
+}
+
+static int find_socket_fd(
+ Manager *m,
+ DnsStubListenerExtra *l,
+ int family,
+ const union in_addr_union *listen_address,
+ int type) {
+
+ assert(m);
+
+ /* Finds the right socket to use for sending. If we know the extra listener, otherwise go via the
+ * address to send from */
+ if (l)
+ return manager_dns_stub_fd_extra(m, l, type);
+
+ return manager_dns_stub_fd(m, family, listen_address, type);
+}
+
static int dns_stub_send(
Manager *m,
DnsStubListenerExtra *l,
if (s)
r = dns_stream_write_packet(s, reply);
- else
- /* Note that it is essential here that we explicitly choose the source IP address for this packet. This
- * is because otherwise the kernel will choose it automatically based on the routing table and will
- * thus pick 127.0.0.1 rather than 127.0.0.53. */
+ else {
+ int fd, ifindex;
+
+ fd = find_socket_fd(m, l, p->family, &p->destination, SOCK_DGRAM);
+ if (fd < 0)
+ return fd;
+
+ if (address_is_proxy(p->family, &p->destination))
+ /* Force loopback iface if this is the loopback proxy stub
+ * and ifindex was normalized to 0 by manager_recv(). */
+ ifindex = p->ifindex ?: LOOPBACK_IFINDEX;
+ else
+ /* Force loopback iface if this is the main listener stub. */
+ ifindex = l ? p->ifindex : LOOPBACK_IFINDEX;
+
+ /* Note that it is essential here that we explicitly choose the source IP address for this
+ * packet. This is because otherwise the kernel will choose it automatically based on the
+ * routing table and will thus pick 127.0.0.1 rather than 127.0.0.53/54. */
r = manager_send(m,
- manager_dns_stub_fd_extra(m, l, SOCK_DGRAM),
- l ? p->ifindex : LOOPBACK_IFINDEX, /* force loopback iface if this is the main listener stub */
+ fd,
+ ifindex,
p->family, &p->sender, p->sender_port, &p->destination,
reply);
+ }
if (r < 0)
return log_debug_errno(r, "Failed to send reply packet: %m");
DNS_PACKET_CD(q->request_packet)); /* … or client set CD */
}
+static void dns_stub_suppress_duplicate_section_rrs(DnsQuery *q) {
+ /* If we follow a CNAME/DNAME chain we might end up populating our sections with redundant RRs
+ * because we built up the sections from multiple reply packets (one from each CNAME/DNAME chain
+ * element). E.g. it could be that an RR that was included in the first reply's additional section
+ * ends up being relevant as main answer in a subsequent reply in the chain. Let's clean this up, and
+ * remove everything in the "higher priority" sections from the "lower priority" sections.
+ *
+ * Note that this removal matches by RR keys instead of the full RRs. This is because RRsets should
+ * always end up in one section fully or not at all, but never be split among sections.
+ *
+ * Specifically: we remove ANSWER section RRs from the AUTHORITATIVE and ADDITIONAL sections, as well
+ * as AUTHORITATIVE section RRs from the ADDITIONAL section. */
+
+ dns_answer_remove_by_answer_keys(&q->reply_authoritative, q->reply_answer);
+ dns_answer_remove_by_answer_keys(&q->reply_additional, q->reply_answer);
+ dns_answer_remove_by_answer_keys(&q->reply_additional, q->reply_authoritative);
+}
+
static int dns_stub_send_reply(
DnsQuery *q,
int rcode) {
edns0_do = dns_stub_reply_with_edns0_do(q); /* let's check if we shall reply with EDNS0 DO? */
- r = dns_stub_assign_sections(
- q,
- q->request_packet->question,
- edns0_do);
- if (r < 0)
- return log_debug_errno(r, "Failed to assign sections: %m");
-
r = dns_stub_make_reply_packet(
&reply,
DNS_PACKET_PAYLOAD_SIZE_MAX(q->request_packet),
if (r < 0)
return log_debug_errno(r, "Failed to build reply packet: %m");
+ dns_stub_suppress_duplicate_section_rrs(q);
+
r = dns_stub_add_reply_packet_body(
reply,
q->reply_answer,
DNS_PACKET_ID(q->request_packet),
rcode,
truncated,
- dns_query_fully_synthetic(q),
+ dns_query_fully_authoritative(q),
+ DNS_PACKET_RD(q->request_packet),
!!q->request_packet->opt,
edns0_do,
- DNS_PACKET_AD(q->request_packet) && dns_query_fully_authenticated(q),
+ (DNS_PACKET_AD(q->request_packet) || DNS_PACKET_DO(q->request_packet)) && dns_query_fully_authenticated(q),
DNS_PACKET_CD(q->request_packet),
q->stub_listener_extra ? ADVERTISE_EXTRA_DATAGRAM_SIZE_MAX : ADVERTISE_DATAGRAM_SIZE_MAX,
dns_packet_has_nsid_request(q->request_packet) > 0 && !q->stub_listener_extra);
rcode,
truncated,
false,
+ DNS_PACKET_RD(p),
!!p->opt,
DNS_PACKET_DO(p),
- DNS_PACKET_AD(p) && authenticated,
+ (DNS_PACKET_AD(p) || DNS_PACKET_DO(p)) && authenticated,
DNS_PACKET_CD(p),
l ? ADVERTISE_EXTRA_DATAGRAM_SIZE_MAX : ADVERTISE_DATAGRAM_SIZE_MAX,
dns_packet_has_nsid_request(p) > 0 && !l);
return 0;
}
-static void dns_stub_query_complete(DnsQuery *q) {
+static void dns_stub_query_complete(DnsQuery *query) {
+ _cleanup_(dns_query_freep) DnsQuery *q = query;
int r;
assert(q);
else
(void) dns_stub_send(q->manager, q->stub_listener_extra, q->request_stream, q->request_packet, reply);
- dns_query_free(q);
return;
}
}
- /* Note that we don't bother with following CNAMEs here. We propagate the authoritative/additional
- * sections from the upstream answer however, hence if the upstream server collected that information
- * already we don't have to collect it ourselves anymore. */
+ /* Take all data from the current reply, and merge it into the three reply sections we are building
+ * up. We do this before processing CNAME redirects, so that we gradually build up our sections, and
+ * and keep adding all RRs in the CNAME chain. */
+ r = dns_stub_assign_sections(
+ q,
+ dns_query_question_for_protocol(q, DNS_PROTOCOL_DNS),
+ dns_stub_reply_with_edns0_do(q));
+ if (r < 0)
+ return (void) log_debug_errno(r, "Failed to assign sections: %m");
switch (q->state) {
- case DNS_TRANSACTION_SUCCESS:
+ case DNS_TRANSACTION_SUCCESS: {
+ bool first = true;
+
+ for (;;) {
+ int cname_result;
+
+ cname_result = dns_query_process_cname_one(q);
+ if (cname_result == -ELOOP) { /* CNAME loop, let's send what we already have */
+ log_debug_errno(r, "Detected CNAME loop, returning what we already have.");
+ (void) dns_stub_send_reply(q, q->answer_rcode);
+ break;
+ }
+ if (cname_result < 0) {
+ log_debug_errno(cname_result, "Failed to process CNAME: %m");
+ break;
+ }
+
+ if (cname_result == DNS_QUERY_NOMATCH) {
+ /* This answer doesn't contain any RR that would answer our question
+ * positively, i.e. neither directly nor via CNAME. */
+
+ if (first) /* We never followed a CNAME and the answer doesn't match our
+ * question at all? Then this is final, the empty answer is the
+ * answer. */
+ break;
+
+ /* Otherwise, we already followed a CNAME once within this packet, and the
+ * packet doesn't answer our question. In that case let's restart the query,
+ * now with the redirected question. We'll */
+ r = dns_query_go(q);
+ if (r < 0)
+ return (void) log_debug_errno(r, "Failed to restart query: %m");
+
+ TAKE_PTR(q);
+ return;
+ }
+
+ r = dns_stub_assign_sections(
+ q,
+ dns_query_question_for_protocol(q, DNS_PROTOCOL_DNS),
+ dns_stub_reply_with_edns0_do(q));
+ if (r < 0)
+ return (void) log_debug_errno(r, "Failed to assign sections: %m");
+
+ if (cname_result == DNS_QUERY_MATCH) /* A match? Then we are done, let's return what we got */
+ break;
+
+ /* We followed a CNAME. and collected the RRs that answer the redirected question
+ * successfully. Let's not try to do this again. */
+ assert(cname_result == DNS_QUERY_CNAME);
+ first = false;
+ }
+
+ _fallthrough_;
+ }
+
case DNS_TRANSACTION_RCODE_FAILURE:
(void) dns_stub_send_reply(q, q->answer_rcode);
break;
case DNS_TRANSACTION_PENDING:
case DNS_TRANSACTION_VALIDATING:
default:
- assert_not_reached("Impossible state");
+ assert_not_reached();
}
-
- dns_query_free(q);
}
static int dns_stub_stream_complete(DnsStream *s, int error) {
}
static void dns_stub_process_query(Manager *m, DnsStubListenerExtra *l, DnsStream *s, DnsPacket *p) {
+ uint64_t protocol_flags = SD_RESOLVED_PROTOCOLS_ALL;
_cleanup_(dns_query_freep) DnsQuery *q = NULL;
Hashmap **queries_by_packet;
DnsQuery *existing;
+ bool bypass = false;
int r;
assert(m);
assert(p->protocol == DNS_PROTOCOL_DNS);
if (!l && /* l == NULL if this is the main stub */
+ !address_is_proxy(p->family, &p->destination) && /* don't restrict needlessly for 127.0.0.54 */
(in_addr_is_localhost(p->family, &p->sender) <= 0 ||
in_addr_is_localhost(p->family, &p->destination) <= 0)) {
log_warning("Got packet on unexpected (i.e. non-localhost) IP range, ignoring.");
return;
}
- if (dns_type_is_obsolete(p->question->keys[0]->type)) {
+ if (dns_type_is_obsolete(dns_question_first_key(p->question)->type)) {
log_debug("Got message with obsolete key type, refusing.");
dns_stub_send_failure(m, l, s, p, DNS_RCODE_REFUSED, false);
return;
}
- if (dns_type_is_zone_transer(p->question->keys[0]->type)) {
+ if (dns_type_is_zone_transer(dns_question_first_key(p->question)->type)) {
log_debug("Got request for zone transfer, refusing.");
dns_stub_send_failure(m, l, s, p, DNS_RCODE_REFUSED, false);
return;
return;
}
- if (DNS_PACKET_DO(p) && DNS_PACKET_CD(p)) {
+ if (address_is_proxy(p->family, &p->destination)) {
+ _cleanup_free_ char *dipa = NULL;
+
+ r = in_addr_to_string(p->family, &p->destination, &dipa);
+ if (r < 0)
+ return (void) log_error_errno(r, "Failed to format destination address: %m");
+
+ log_debug("Got request to DNS proxy address 127.0.0.54, enabling bypass logic.");
+ bypass = true;
+ protocol_flags = SD_RESOLVED_DNS|SD_RESOLVED_NO_ZONE; /* Turn off mDNS/LLMNR for proxy stub. */
+ } else if ((DNS_PACKET_DO(p) && DNS_PACKET_CD(p))) {
log_debug("Got request with DNSSEC checking disabled, enabling bypass logic.");
+ bypass = true;
+ }
+ if (bypass)
r = dns_query_new(m, &q, NULL, NULL, p, 0,
- SD_RESOLVED_PROTOCOLS_ALL|
+ protocol_flags|
SD_RESOLVED_NO_CNAME|
SD_RESOLVED_NO_SEARCH|
SD_RESOLVED_NO_VALIDATE|
SD_RESOLVED_REQUIRE_PRIMARY|
SD_RESOLVED_CLAMP_TTL);
- } else
+ else
r = dns_query_new(m, &q, p->question, p->question, NULL, 0,
- SD_RESOLVED_PROTOCOLS_ALL|
+ protocol_flags|
SD_RESOLVED_NO_SEARCH|
- SD_RESOLVED_NO_CNAME|
(DNS_PACKET_DO(p) ? SD_RESOLVED_REQUIRE_PRIMARY : 0)|
SD_RESOLVED_CLAMP_TTL);
if (r < 0) {
}
static int on_dns_stub_packet_extra(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- DnsStubListenerExtra *l = userdata;
-
- assert(l);
+ DnsStubListenerExtra *l = ASSERT_PTR(userdata);
return on_dns_stub_packet_internal(s, fd, revents, l->manager, l);
}
-static int on_dns_stub_stream_packet(DnsStream *s) {
- _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
-
+static int on_dns_stub_stream_packet(DnsStream *s, DnsPacket *p) {
assert(s);
-
- p = dns_stream_take_read_packet(s);
+ assert(s->manager);
assert(p);
if (dns_packet_validate_query(p) > 0) {
return -errno;
}
- r = dns_stream_new(m, &stream, DNS_STREAM_STUB, DNS_PROTOCOL_DNS, cfd, NULL);
+ r = dns_stream_new(m, &stream, DNS_STREAM_STUB, DNS_PROTOCOL_DNS, cfd, NULL,
+ on_dns_stub_stream_packet, dns_stub_stream_complete, DNS_STREAM_STUB_TIMEOUT_USEC);
if (r < 0) {
safe_close(cfd);
return r;
}
stream->stub_listener_extra = l;
- stream->on_packet = on_dns_stub_stream_packet;
- stream->complete = dns_stub_stream_complete;
/* We let the reference to the stream dangle here, it will be dropped later by the complete callback. */
}
static int on_dns_stub_stream_extra(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- DnsStubListenerExtra *l = userdata;
+ DnsStubListenerExtra *l = ASSERT_PTR(userdata);
- assert(l);
return on_dns_stub_stream_internal(s, fd, revents, l->manager, l);
}
return 0;
}
-static int manager_dns_stub_fd(Manager *m, int type) {
- union sockaddr_union sa = {
- .in.sin_family = AF_INET,
- .in.sin_addr.s_addr = htobe32(INADDR_DNS_STUB),
- .in.sin_port = htobe16(53),
- };
- _cleanup_close_ int fd = -1;
+static int manager_dns_stub_fd(
+ Manager *m,
+ int family,
+ const union in_addr_union *listen_addr,
+ int type) {
+
+ sd_event_source **event_source;
+ _cleanup_close_ int fd = -EBADF;
+ union sockaddr_union sa;
int r;
- assert(IN_SET(type, SOCK_DGRAM, SOCK_STREAM));
+ assert(m);
+ assert(listen_addr);
+
+ if (type == SOCK_DGRAM)
+ event_source = address_is_proxy(family, listen_addr) ? &m->dns_proxy_stub_udp_event_source : &m->dns_stub_udp_event_source;
+ else if (type == SOCK_STREAM)
+ event_source = address_is_proxy(family, listen_addr) ? &m->dns_proxy_stub_tcp_event_source : &m->dns_stub_tcp_event_source;
+ else
+ return -EPROTONOSUPPORT;
- sd_event_source **event_source = type == SOCK_DGRAM ? &m->dns_stub_udp_event_source : &m->dns_stub_tcp_event_source;
if (*event_source)
return sd_event_source_get_io_fd(*event_source);
- fd = socket(AF_INET, type | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ fd = socket(family, type | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (fd < 0)
return -errno;
- r = set_dns_stub_common_socket_options(fd, AF_INET);
+ r = set_dns_stub_common_socket_options(fd, family);
if (r < 0)
return r;
return r;
}
- /* Make sure no traffic from outside the local host can leak to onto this socket */
- r = socket_bind_to_ifindex(fd, LOOPBACK_IFINDEX);
- if (r < 0)
- return r;
+ /* Set slightly different socket options for the non-proxy and the proxy binding. The former we want
+ * to be accessible only from the local host, for the latter it's OK if people use NAT redirects or
+ * so to redirect external traffic to it. */
+
+ if (!address_is_proxy(family, listen_addr)) {
+ /* Make sure no traffic from outside the local host can leak to onto this socket */
+ r = socket_bind_to_ifindex(fd, LOOPBACK_IFINDEX);
+ if (r < 0)
+ return r;
- r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, 1);
+ r = socket_set_ttl(fd, family, 1);
+ if (r < 0)
+ return r;
+ } else if (type == SOCK_DGRAM) {
+ /* Turn off Path MTU Discovery for UDP, for security reasons. See socket_disable_pmtud() for
+ * a longer discussion. (We only do this for sockets that are potentially externally
+ * accessible, i.e. the proxy stub one. For the non-proxy one we instead set the TTL to 1,
+ * see above, so that packets don't get routed at all.) */
+ r = socket_disable_pmtud(fd, family);
+ if (r < 0)
+ log_debug_errno(r, "Failed to disable UDP PMTUD, ignoring: %m");
+
+ r = socket_set_recvfragsize(fd, family, true);
+ if (r < 0)
+ log_debug_errno(r, "Failed to enable fragment size reception, ignoring: %m");
+ }
+
+ r = sockaddr_set_in_addr(&sa, family, listen_addr, 53);
if (r < 0)
return r;
static int manager_dns_stub_fd_extra(Manager *m, DnsStubListenerExtra *l, int type) {
_cleanup_free_ char *pretty = NULL;
- _cleanup_close_ int fd = -1;
+ _cleanup_close_ int fd = -EBADF;
union sockaddr_union sa;
int r;
assert(m);
+ assert(l);
assert(IN_SET(type, SOCK_DGRAM, SOCK_STREAM));
- if (!l)
- return manager_dns_stub_fd(m, type);
-
sd_event_source **event_source = type == SOCK_DGRAM ? &l->udp_event_source : &l->tcp_event_source;
if (*event_source)
return sd_event_source_get_io_fd(*event_source);
log_debug_errno(r, "Failed to enable fragment size reception, ignoring: %m");
}
- if (bind(fd, &sa.sa, SOCKADDR_LEN(sa)) < 0) {
- r = -errno;
+ r = RET_NERRNO(bind(fd, &sa.sa, SOCKADDR_LEN(sa)));
+ if (r < 0)
goto fail;
- }
if (type == SOCK_STREAM &&
listen(fd, SOMAXCONN) < 0) {
}
int manager_dns_stub_start(Manager *m) {
- const char *t = "UDP";
- int r = 0;
+ int r;
assert(m);
if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_NO)
log_debug("Not creating stub listener.");
- else
+ else {
+ static const struct {
+ uint32_t addr;
+ int socket_type;
+ } stub_sockets[] = {
+ { INADDR_DNS_STUB, SOCK_DGRAM },
+ { INADDR_DNS_STUB, SOCK_STREAM },
+ { INADDR_DNS_PROXY_STUB, SOCK_DGRAM },
+ { INADDR_DNS_PROXY_STUB, SOCK_STREAM },
+ };
+
log_debug("Creating stub listener using %s.",
m->dns_stub_listener_mode == DNS_STUB_LISTENER_UDP ? "UDP" :
m->dns_stub_listener_mode == DNS_STUB_LISTENER_TCP ? "TCP" :
"UDP/TCP");
- if (FLAGS_SET(m->dns_stub_listener_mode, DNS_STUB_LISTENER_UDP))
- r = manager_dns_stub_fd(m, SOCK_DGRAM);
-
- if (r >= 0 &&
- FLAGS_SET(m->dns_stub_listener_mode, DNS_STUB_LISTENER_TCP)) {
- t = "TCP";
- r = manager_dns_stub_fd(m, SOCK_STREAM);
+ for (size_t i = 0; i < ELEMENTSOF(stub_sockets); i++) {
+ union in_addr_union a = {
+ .in.s_addr = htobe32(stub_sockets[i].addr),
+ };
+
+ if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_UDP && stub_sockets[i].socket_type == SOCK_STREAM)
+ continue;
+ if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_TCP && stub_sockets[i].socket_type == SOCK_DGRAM)
+ continue;
+
+ r = manager_dns_stub_fd(m, AF_INET, &a, stub_sockets[i].socket_type);
+ if (r < 0) {
+ _cleanup_free_ char *busy_socket = NULL;
+
+ if (asprintf(&busy_socket,
+ "%s socket " IPV4_ADDRESS_FMT_STR ":53",
+ stub_sockets[i].socket_type == SOCK_DGRAM ? "UDP" : "TCP",
+ IPV4_ADDRESS_FMT_VAL(a.in)) < 0)
+ return log_oom();
+
+ if (IN_SET(r, -EADDRINUSE, -EPERM)) {
+ log_warning_errno(r,
+ r == -EADDRINUSE ? "Another process is already listening on %s.\n"
+ "Turning off local DNS stub support." :
+ "Failed to listen on %s: %m.\n"
+ "Turning off local DNS stub support.",
+ busy_socket);
+ manager_dns_stub_stop(m);
+ break;
+ }
+
+ return log_error_errno(r, "Failed to listen on %s: %m", busy_socket);
+ }
+ }
}
- if (IN_SET(r, -EADDRINUSE, -EPERM)) {
- log_warning_errno(r,
- r == -EADDRINUSE ? "Another process is already listening on %s socket 127.0.0.53:53.\n"
- "Turning off local DNS stub support." :
- "Failed to listen on %s socket 127.0.0.53:53: %m.\n"
- "Turning off local DNS stub support.",
- t);
- manager_dns_stub_stop(m);
- } else if (r < 0)
- return log_error_errno(r, "Failed to listen on %s socket 127.0.0.53:53: %m", t);
-
if (!ordered_set_isempty(m->dns_extra_stub_listeners)) {
DnsStubListenerExtra *l;
m->dns_stub_udp_event_source = sd_event_source_disable_unref(m->dns_stub_udp_event_source);
m->dns_stub_tcp_event_source = sd_event_source_disable_unref(m->dns_stub_tcp_event_source);
+ m->dns_proxy_stub_udp_event_source = sd_event_source_disable_unref(m->dns_proxy_stub_udp_event_source);
+ m->dns_proxy_stub_tcp_event_source = sd_event_source_disable_unref(m->dns_proxy_stub_tcp_event_source);
}
static const char* const dns_stub_listener_mode_table[_DNS_STUB_LISTENER_MODE_MAX] = {