resolved-manager.c revision 4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Tom Gundersen <teg@jklm.no>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#define SEND_TIMEOUT_USEC (200 * USEC_PER_MSEC)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int manager_process_link(sd_rtnl *rtnl, sd_rtnl_message *mm, void *userdata) {
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen l = hashmap_get(m->links, INT_TO_PTR(ifindex));
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering log_debug("Found new link %i/%s", ifindex, l->name);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen log_debug("Removing link %i/%s", l->ifindex, l->name);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_warning("Failed to process RTNL link message: %s", strerror(-r));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int manager_process_address(sd_rtnl *rtnl, sd_rtnl_message *mm, void *userdata) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_addr_get_ifindex(mm, &ifindex);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering l = hashmap_get(m->links, INT_TO_PTR(ifindex));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_addr_get_family(mm, &family);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_read_in_addr(mm, IFA_LOCAL, &address.in);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_read_in_addr(mm, IFA_ADDRESS, &address.in);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_read_in6_addr(mm, IFA_LOCAL, &address.in6);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_message_read_in6_addr(mm, IFA_ADDRESS, &address.in6);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering a = link_find_address(l, family, &address);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = link_address_new(l, &a, family, &address);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_warning("Failed to process RTNL address message: %s", strerror(-r));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int manager_rtnl_listen(Manager *m) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering /* First, subscibe to interfaces coming and going */
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, manager_process_link, m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, manager_process_link, m);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, manager_process_address, m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, manager_process_address, m);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering /* Then, enumerate all links */
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_message_request_dump(req, true);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_call(m->rtnl, req, 0, &reply);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering for (i = reply; i; i = sd_rtnl_message_next(i)) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering /* Finally, enumerate all addresses, too */
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_rtnl_message_request_dump(req, true);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering for (i = reply; i; i = sd_rtnl_message_next(i)) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_network_monitor_flush(m->network_monitor);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt log_warning("Failed to update monitor information for %i: %s", l->ifindex, strerror(-r));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Could not update resolv.conf: %s", strerror(-r));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int manager_network_monitor_listen(Manager *m) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_network_monitor_new(&m->network_monitor, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fd = sd_network_monitor_get_fd(m->network_monitor);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering events = sd_network_monitor_get_events(m->network_monitor);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_event_add_io(m->event, &m->network_event_source, fd, events, &on_network_event, m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int determine_hostname(char **ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *h = NULL, *n = NULL;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_error("System hostname is not UTF-8 clean.");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_error("System hostname '%s' cannot be normalized.", h);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0; /* ignore invalid hostnames */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_info("System hostname changed to '%s'.", h);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmekstatic int manager_watch_hostname(Manager *m) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering m->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY|O_NOCTTY);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_warning("Failed to watch hostname: %m");
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering m->hostname_fd = safe_close(m->hostname_fd);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek log_error("Failed to add hostname event source: %s", strerror(-r));
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_info("Defaulting to hostname 'linux'.");
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_info("Using system hostname '%s'.", m->hostname);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek _cleanup_(manager_freep) Manager *m = NULL;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen m->llmnr_ipv4_udp_fd = m->llmnr_ipv6_udp_fd = -1;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen m->llmnr_ipv4_tcp_fd = m->llmnr_ipv6_tcp_fd = -1;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = manager_parse_dns_server(m, DNS_SERVER_FALLBACK, DNS_SERVERS);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_event_default(&m->event);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering dns_server_free(m->fallback_dns_servers);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering sd_event_source_unref(m->network_event_source);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering sd_network_monitor_unref(m->network_monitor);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering sd_event_source_unref(m->dns_ipv4_event_source);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_source_unref(m->dns_ipv6_event_source);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_source_unref(m->llmnr_ipv4_udp_event_source);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_source_unref(m->llmnr_ipv6_udp_event_source);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sd_event_source_unref(m->llmnr_ipv4_tcp_event_source);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering sd_event_source_unref(m->llmnr_ipv6_tcp_event_source);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering sd_event_source_unref(m->bus_retry_event_source);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering dns_resource_key_unref(m->host_ipv4_key);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering dns_resource_key_unref(m->host_ipv6_key);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering sd_event_source_unref(m->hostname_event_source);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void write_resolve_conf_server(DnsServer *s, FILE *f, unsigned *count) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = in_addr_to_string(s->family, &s->address, &t);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Invalid DNS address. Ignoring: %s", strerror(-r));
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering fputs("# Too many DNS servers configured, the following entries may be ignored\n", f);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringint manager_write_resolv_conf(Manager *m) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering static const char path[] = "/run/systemd/resolve/resolv.conf";
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = fopen_temporary(path, &f, &temp_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "# Third party programs must not access this file directly, but\n"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "# only through the symlink at /etc/resolv.conf. To manage\n"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "# resolv.conf(5) in a different way, replace the symlink by a\n"
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen "# static file or a different symlink.\n\n", f);
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen LIST_FOREACH(servers, s, m->fallback_dns_servers)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (rename(temp_path, path) < 0) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekint manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek struct cmsghdr header; /* For alignment */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen uint8_t buffer[CMSG_SPACE(MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = dns_packet_new(&p, protocol, ms);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek iov.iov_base = DNS_PACKET_DATA(p);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek mh.msg_controllen = sizeof(control);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (errno == EAGAIN || errno == EINTR)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(!(mh.msg_flags & MSG_CTRUNC));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(!(mh.msg_flags & MSG_TRUNC));
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering /* The Linux kernel sets the interface index to the loopback
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * device if the packet came from the local host since it
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * avoids the routing table in such a case. Let's unset the
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * interface index in such a case. */
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (p->ifindex > 0 && manager_ifindex_is_loopback(m, p->ifindex) != 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering /* If we don't know the interface index still, we look for the
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * first local interface with a matching address. Yuck! */
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering p->ifindex = manager_find_ifindex(m, p->family, &p->destination);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic int on_dns_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = manager_recv(m, fd, DNS_PROTOCOL_DNS, &p);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (dns_packet_validate_reply(p) > 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p)));
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering m->dns_ipv4_fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering r = setsockopt(m->dns_ipv4_fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_event_add_io(m->event, &m->dns_ipv4_event_source, m->dns_ipv4_fd, EPOLLIN, on_dns_packet, m);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek m->dns_ipv4_fd = safe_close(m->dns_ipv4_fd);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmekint manager_dns_ipv6_fd(Manager *m) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering m->dns_ipv6_fd = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = setsockopt(m->dns_ipv6_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_event_add_io(m->event, &m->dns_ipv6_event_source, m->dns_ipv6_fd, EPOLLIN, on_dns_packet, m);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering m->dns_ipv6_fd = safe_close(m->dns_ipv6_fd);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int sendmsg_loop(int fd, struct msghdr *mh, int flags) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic int manager_ipv4_send(Manager *m, int fd, int ifindex, const struct in_addr *addr, uint16_t port, DnsPacket *p) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek struct cmsghdr header; /* For alignment */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering mh.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering pi = (struct in_pktinfo*) CMSG_DATA(cmsg);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return sendmsg_loop(fd, &mh, 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmekstatic int manager_ipv6_send(Manager *m, int fd, int ifindex, const struct in6_addr *addr, uint16_t port, DnsPacket *p) {
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering struct cmsghdr header; /* For alignment */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))];
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek iov.iov_base = DNS_PACKET_DATA(p);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek sa.in6.sin6_port = htobe16(port),
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek mh.msg_namelen = sizeof(sa.in6);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek mh.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo));
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek cmsg->cmsg_level = IPPROTO_IPV6;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek pi = (struct in6_pktinfo*) CMSG_DATA(cmsg);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekint manager_send(Manager *m, int fd, int ifindex, int family, const union in_addr_union *addr, uint16_t port, DnsPacket *p) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_debug("Sending %s packet with id %u on interface %i/%s", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p), ifindex, af_to_name(family));
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return manager_ipv4_send(m, fd, ifindex, &addr->in, port, p);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return manager_ipv6_send(m, fd, ifindex, &addr->in6, port, p);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenbool manager_known_dns_server(Manager *m, int family, const union in_addr_union *in_addr) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (s->family == family && in_addr_equal(family, &s->address, in_addr))
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen LIST_FOREACH(servers, s, m->fallback_dns_servers)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (s->family == family && in_addr_equal(family, &s->address, in_addr))
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return false;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenstatic DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen in_addr_to_string(s->family, &s->address, &ip);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_info("Switching to system DNS server %s.", strna(ip));
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-SzmekDnsServer *manager_get_dns_server(Manager *m) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* No DNS servers configured, let's see if there are
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering * any on any links. If not, we use the fallback
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering manager_set_dns_server(m, m->fallback_dns_servers);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringvoid manager_next_dns_server(Manager *m) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* If there's currently no DNS server set, then the next
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering * manager_get_dns_server() will find one */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* Change to the next one */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (m->current_dns_server->servers_next) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering manager_set_dns_server(m, m->current_dns_server->servers_next);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering /* If there was no next one, then start from the beginning of
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (m->current_dns_server->type == DNS_SERVER_FALLBACK)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering manager_set_dns_server(m, m->fallback_dns_servers);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering manager_set_dns_server(m, m->dns_servers);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering /* If we don't know on which link a DNS packet would be
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * delivered, let's find the largest MTU that works on all
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * interfaces we know of */
if (dns_packet_validate_reply(p) > 0) {
} else if (dns_packet_validate_query(p) > 0) {
Link *l;
if (scope)
assert(m);
if (m->llmnr_ipv4_udp_fd >= 0)
return m->llmnr_ipv4_udp_fd;
if (m->llmnr_ipv4_udp_fd < 0)
return -errno;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
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);
goto fail;
return m->llmnr_ipv4_udp_fd;
fail:
assert(m);
if (m->llmnr_ipv6_udp_fd >= 0)
return m->llmnr_ipv6_udp_fd;
if (m->llmnr_ipv6_udp_fd < 0)
return -errno;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
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);
r = -errno;
goto fail;
return m->llmnr_ipv6_udp_fd;
fail:
assert(s);
Link *l;
if (scope) {
if (s->write_packet)
dns_stream_free(s);
int cfd, r;
if (cfd < 0) {
return -errno;
assert(m);
if (m->llmnr_ipv4_tcp_fd >= 0)
return m->llmnr_ipv4_tcp_fd;
if (m->llmnr_ipv4_tcp_fd < 0)
return -errno;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
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);
goto fail;
return m->llmnr_ipv4_tcp_fd;
fail:
assert(m);
if (m->llmnr_ipv6_tcp_fd >= 0)
return m->llmnr_ipv6_tcp_fd;
if (m->llmnr_ipv6_tcp_fd < 0)
return -errno;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
r = -errno;
goto fail;
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);
r = -errno;
goto fail;
return m->llmnr_ipv6_tcp_fd;
fail:
Link *l;
assert(m);
if (ifindex <= 0)
return -EINVAL;
LinkAddress *a;
assert(m);
Iterator i;
Link *l;
assert(m);
link_add_rrs(l, true);
link_add_rrs(l, false);
uint64_t u;
assert(m);
assert(p);
while (p > m->hostname) {
if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0)
return -ENOMEM;
m->hostname = h;
LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) {
Iterator i;
Link *l;
assert(m);
LinkAddress *a;
return NULL;
assert(m);
assert(p);