resolved-llmnr.c revision 35908b983545d540ff75d6a0f4fbac969463a250
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <resolv.h>
#include "fd-util.h"
#include "resolved-llmnr.h"
#include "resolved-manager.h"
void manager_llmnr_stop(Manager *m) {
assert(m);
}
int manager_llmnr_start(Manager *m) {
int r;
assert(m);
if (m->llmnr_support == RESOLVE_SUPPORT_NO)
return 0;
r = manager_llmnr_ipv4_udp_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
r = manager_llmnr_ipv4_tcp_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
if (socket_ipv6_is_supported()) {
r = manager_llmnr_ipv6_udp_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
r = manager_llmnr_ipv6_tcp_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
}
return 0;
log_warning("There appears to be another LLMNR responder running. Turning off LLMNR support.");
return 0;
}
DnsTransaction *t = NULL;
int r;
if (r <= 0)
return r;
scope = manager_find_scope(m, p);
if (!scope) {
log_warning("Got LLMNR UDP packet on unknown scope. Ignoring.");
return 0;
}
if (dns_packet_validate_reply(p) > 0) {
if (t)
} else if (dns_packet_validate_query(p) > 0) {
} else
log_debug("Invalid LLMNR UDP packet, ignoring.");
return 0;
}
int manager_llmnr_ipv4_udp_fd(Manager *m) {
union sockaddr_union sa = {
};
int r;
assert(m);
if (m->llmnr_ipv4_udp_fd >= 0)
return m->llmnr_ipv4_udp_fd;
if (m->llmnr_ipv4_udp_fd < 0)
return -errno;
/* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
/* Disable Don't-Fragment bit in the IP header */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv4_udp_event_source, m->llmnr_ipv4_udp_fd, EPOLLIN, on_llmnr_packet, m);
if (r < 0)
goto fail;
return m->llmnr_ipv4_udp_fd;
fail:
return r;
}
int manager_llmnr_ipv6_udp_fd(Manager *m) {
union sockaddr_union sa = {
};
int r;
assert(m);
if (m->llmnr_ipv6_udp_fd >= 0)
return m->llmnr_ipv6_udp_fd;
if (m->llmnr_ipv6_udp_fd < 0)
return -errno;
if (r < 0) {
r = -errno;
goto fail;
}
/* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv6_udp_event_source, m->llmnr_ipv6_udp_fd, EPOLLIN, on_llmnr_packet, m);
if (r < 0)
goto fail;
return m->llmnr_ipv6_udp_fd;
fail:
return r;
}
static int on_llmnr_stream_packet(DnsStream *s) {
assert(s);
if (!scope) {
log_warning("Got LLMNR TCP packet on unknown scope. Ignroing.");
return 0;
}
if (dns_packet_validate_query(s->read_packet) > 0) {
/* If no reply packet was set, we free the stream */
if (s->write_packet)
return 0;
} else
log_debug("Invalid LLMNR TCP packet.");
dns_stream_free(s);
return 0;
}
int cfd, r;
if (cfd < 0) {
return 0;
return -errno;
}
if (r < 0) {
return r;
}
return 0;
}
int manager_llmnr_ipv4_tcp_fd(Manager *m) {
union sockaddr_union sa = {
};
int r;
assert(m);
if (m->llmnr_ipv4_tcp_fd >= 0)
return m->llmnr_ipv4_tcp_fd;
if (m->llmnr_ipv4_tcp_fd < 0)
return -errno;
/* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
/* Disable Don't-Fragment bit in the IP header */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv4_tcp_event_source, m->llmnr_ipv4_tcp_fd, EPOLLIN, on_llmnr_stream, m);
if (r < 0)
goto fail;
return m->llmnr_ipv4_tcp_fd;
fail:
return r;
}
int manager_llmnr_ipv6_tcp_fd(Manager *m) {
union sockaddr_union sa = {
};
static const int one = 1;
int r;
assert(m);
if (m->llmnr_ipv6_tcp_fd >= 0)
return m->llmnr_ipv6_tcp_fd;
if (m->llmnr_ipv6_tcp_fd < 0)
return -errno;
/* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv6_tcp_event_source, m->llmnr_ipv6_tcp_fd, EPOLLIN, on_llmnr_stream, m);
if (r < 0)
goto fail;
return m->llmnr_ipv6_tcp_fd;
fail:
return r;
}