#include "resolved-dns-domain.h"
#include "resolved-dns-scope.h"
+#define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
+#define MULTICAST_RATELIMIT_BURST 1000
+
int dns_scope_new(Manager *m, DnsScope **ret, Link *l, DnsProtocol protocol, int family) {
DnsScope *s;
log_debug("New scope on link %s, protocol %s, family %s", l ? l->name : "*", dns_protocol_to_string(protocol), family == AF_UNSPEC ? "*" : af_to_name(family));
+ /* Enforce ratelimiting for the multicast protocols */
+ RATELIMIT_INIT(s->ratelimit, MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST);
+
*ret = s;
return 0;
}
return NULL;
}
-DnsServer *dns_scope_get_server(DnsScope *s) {
+DnsServer *dns_scope_get_dns_server(DnsScope *s) {
assert(s);
if (s->protocol != DNS_PROTOCOL_DNS)
if (DNS_PACKET_QDCOUNT(p) > 1)
return -ENOTSUP;
- srv = dns_scope_get_server(s);
+ srv = dns_scope_get_dns_server(s);
if (!srv)
return -ESRCH;
if (DNS_PACKET_QDCOUNT(p) > 1)
return -ENOTSUP;
+ if (!ratelimit_test(&s->ratelimit))
+ return -EBUSY;
+
family = s->family;
port = 5355;
if (family == AF_UNSPEC) {
DnsServer *srv;
- srv = dns_scope_get_server(s);
+ srv = dns_scope_get_dns_server(s);
if (!srv)
return -ESRCH;
if (fd < 0)
return fd;
+ /* Always first try to drop membership before we add
+ * one. This is necessary on some devices, such as
+ * veth. */
+ if (b)
+ setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn));
+
if (setsockopt(fd, IPPROTO_IP, b ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)) < 0)
return -errno;
if (fd < 0)
return fd;
+ if (b)
+ setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
+
if (setsockopt(fd, IPPROTO_IPV6, b ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
return -errno;
} else
assert(s);
- if (q->n_keys <= 0 && answer->n_rrs <= 0 && soa->n_rrs <= 0)
+ if ((!q || q->n_keys <= 0)
+ && (!answer || answer->n_rrs <= 0)
+ && (!soa || soa->n_rrs <= 0))
return -EINVAL;
r = dns_packet_new(&p, s->protocol, 0);
if (stream)
r = dns_stream_write_packet(stream, reply);
else {
+ if (!ratelimit_test(&s->ratelimit))
+ return;
+
if (p->family == AF_INET)
fd = manager_llmnr_ipv4_udp_fd(s->manager);
else if (p->family == AF_INET6)
}
}
-DnsTransaction *dns_scope_find_transaction(DnsScope *scope, DnsQuestion *question) {
+DnsTransaction *dns_scope_find_transaction(DnsScope *scope, DnsQuestion *question, bool cache_ok) {
DnsTransaction *t;
assert(scope);
/* Try to find an ongoing transaction that is a equal or a
* superset of the specified question */
- LIST_FOREACH(transactions_by_scope, t, scope->transactions)
+ LIST_FOREACH(transactions_by_scope, t, scope->transactions) {
+
+ /* Refuse reusing transactions that completed based on
+ * cached data instead of a real packet, if that's
+ * requested. */
+ if (!cache_ok &&
+ IN_SET(t->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_FAILURE) &&
+ !t->received)
+ continue;
+
if (dns_question_is_superset(t->question, question) > 0)
return t;
+ }
return NULL;
}