resolved-link.c revision 19b50b5ba7ee8c1bfb330377309e4bab7a7531d8
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/***
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen This file is part of systemd.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Copyright 2014 Lennart Poettering
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is free software; you can redistribute it and/or modify it
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen under the terms of the GNU Lesser General Public License as published by
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen the Free Software Foundation; either version 2.1 of the License, or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen (at your option) any later version.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is distributed in the hope that it will be useful, but
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen WITHOUT ANY WARRANTY; without even the implied warranty of
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Lesser General Public License for more details.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen You should have received a copy of the GNU Lesser General Public License
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen***/
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <net/if.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include "sd-network.h"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include "strv.h"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include "missing.h"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include "resolved-link.h"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint link_new(Manager *m, Link **ret, int ifindex) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_(link_freep) Link *l = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(ifindex > 0);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = hashmap_ensure_allocated(&m->links, NULL, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l = new0(Link, 1);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!l)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return -ENOMEM;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->ifindex = ifindex;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->llmnr_support = SUPPORT_YES;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->manager = m;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (ret)
d5d8429a12c4b1ef0dcd226c0904f00f4fa4898aLennart Poettering *ret = l;
d5d8429a12c4b1ef0dcd226c0904f00f4fa4898aLennart Poettering l = NULL;
d5d8429a12c4b1ef0dcd226c0904f00f4fa4898aLennart Poettering
d5d8429a12c4b1ef0dcd226c0904f00f4fa4898aLennart Poettering return 0;
d5d8429a12c4b1ef0dcd226c0904f00f4fa4898aLennart Poettering}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel AndersenLink *link_free(Link *l) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!l)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen while (l->addresses)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_address_free(l->addresses);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (l->manager)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_scope_free(l->unicast_scope);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_scope_free(l->llmnr_ipv4_scope);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_scope_free(l->llmnr_ipv6_scope);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen while (l->dns_servers)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_server_free(l->dns_servers);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen free(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return NULL;
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void link_allocate_scopes(Link *l) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (l->dns_servers) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!l->unicast_scope) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = dns_scope_new(l->manager, &l->unicast_scope, l, DNS_PROTOCOL_DNS, AF_UNSPEC);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_warning("Failed to allocate DNS scope: %s", strerror(-r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->unicast_scope = dns_scope_free(l->unicast_scope);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (link_relevant(l, AF_INET) && l->llmnr_support != SUPPORT_NO && l->manager->llmnr_support != SUPPORT_NO) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!l->llmnr_ipv4_scope) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_warning("Failed to allocate LLMNR IPv4 scope: %s", strerror(-r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (link_relevant(l, AF_INET6) && l->llmnr_support != SUPPORT_NO && l->manager->llmnr_support != SUPPORT_NO) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!l->llmnr_ipv6_scope) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_warning("Failed to allocate LLMNR IPv6 scope: %s", strerror(-r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
b7e7184634d573fb73143210962acce205f37f61Michael Bieblvoid link_add_rrs(Link *l, bool force_remove) {
b7e7184634d573fb73143210962acce205f37f61Michael Biebl LinkAddress *a;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Biebl LIST_FOREACH(addresses, a, l->addresses)
b7e7184634d573fb73143210962acce205f37f61Michael Biebl link_address_add_rrs(a, force_remove);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl}
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Bieblint link_update_rtnl(Link *l, sd_rtnl_message *m) {
b7e7184634d573fb73143210962acce205f37f61Michael Biebl const char *n = NULL;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl int r;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Biebl assert(l);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl assert(m);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Biebl r = sd_rtnl_message_link_get_flags(m, &l->flags);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl if (r < 0)
b7e7184634d573fb73143210962acce205f37f61Michael Biebl return r;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Biebl sd_rtnl_message_read_u32(m, IFLA_MTU, &l->mtu);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Biebl if (sd_rtnl_message_read_string(m, IFLA_IFNAME, &n) >= 0) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen strncpy(l->name, n, sizeof(l->name));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char_array_0(l->name);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
91e7bad45dced1cb2dfaac79337bb08d6e2b74a9Andreas Henriksson
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_allocate_scopes(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_add_rrs(l, false);
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int link_update_dns_servers(Link *l) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_strv_free_ char **nameservers = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char **nameserver;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen DnsServer *s, *nx;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen r = sd_network_get_dns(l->ifindex, &nameservers);
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen if (r < 0)
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen goto clear;
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(servers, s, l->dns_servers)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen s->marked = true;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen STRV_FOREACH(nameserver, nameservers) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen union in_addr_union a;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int family;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = in_addr_from_string_auto(*nameserver, &family, &a);
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt if (r < 0)
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt goto clear;
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt
9993ef2e9817b35b1d467707bef12b2a140b62dcLennart Poettering s = link_find_dns_server(l, family, &a);
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt if (s)
9993ef2e9817b35b1d467707bef12b2a140b62dcLennart Poettering s->marked = false;
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt else {
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, family, &a);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt goto clear;
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH_SAFE(servers, s, nx, l->dns_servers)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (s->marked)
aad0a2c80097926757d4385e5f5492082d47f006Zbigniew Jędrzejewski-Szmek dns_server_free(s);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenclear:
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen while (l->dns_servers)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_server_free(l->dns_servers);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return r;
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen}
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int link_update_llmnr_support(Link *l) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *b = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_network_get_llmnr(l->ifindex, &b);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto clear;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = parse_boolean(b);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (streq(b, "resolve"))
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->llmnr_support = SUPPORT_RESOLVE;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen else
d171ed1c50ba64928b7fb30ee2ae729fdfe0826bThomas Hindoe Paaboel Andersen goto clear;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else if (r > 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->llmnr_support = SUPPORT_YES;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen else
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen l->llmnr_support = SUPPORT_NO;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenclear:
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen l->llmnr_support = SUPPORT_YES;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
31938a8560a664c32a9d72f1fc2d4347b232e6e9Michal Schmidt
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint link_update_monitor(Link *l) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_update_dns_servers(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_update_llmnr_support(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_allocate_scopes(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_add_rrs(l, false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenbool link_relevant(Link *l, int family) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *state = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LinkAddress *a;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* A link is relevant if it isn't a loopback or pointopoint
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * device, has a link beat, can do multicast and has at least
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * one relevant IP address */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (l->flags & (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_DORMANT))
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if ((l->flags & (IFF_UP|IFF_LOWER_UP|IFF_MULTICAST)) != (IFF_UP|IFF_LOWER_UP|IFF_MULTICAST))
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen sd_network_get_link_operational_state(l->ifindex, &state);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (state && !STR_IN_SET(state, "unknown", "degraded", "routable"))
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(addresses, a, l->addresses)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->family == family && link_address_relevant(a))
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return true;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel AndersenLinkAddress *link_find_address(Link *l, int family, const union in_addr_union *in_addr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LinkAddress *a;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(addresses, a, l->addresses)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->family == family && in_addr_equal(family, &a->in_addr, in_addr))
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return a;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
4e4885553447f6f4c014bfa3e5b5837a76a0e612Lennart PoetteringDnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen DnsServer *s;
4e4885553447f6f4c014bfa3e5b5837a76a0e612Lennart Poettering
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(servers, s, l->dns_servers)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (s->family == family && in_addr_equal(family, &s->address, in_addr))
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return s;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel AndersenDnsServer* link_set_dns_server(Link *l, DnsServer *s) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (l->current_dns_server == s)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return s;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (s) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *ip = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen in_addr_to_string(s->family, &s->address, &ip);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_info("Switching to DNS server %s for interface %s.", strna(ip), l->name);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt l->current_dns_server = s;
4e4885553447f6f4c014bfa3e5b5837a76a0e612Lennart Poettering
40780877c19ef408da8ab21f4156cfc153f94b5cMartin Pitt if (l->unicast_scope)
3315f085178f46155fda345d9526c09083b45946Lennart Poettering dns_cache_flush(&l->unicast_scope->cache);
4e4885553447f6f4c014bfa3e5b5837a76a0e612Lennart Poettering
3315f085178f46155fda345d9526c09083b45946Lennart Poettering return s;
3315f085178f46155fda345d9526c09083b45946Lennart Poettering}
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel AndersenDnsServer *link_get_dns_server(Link *l) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!l->current_dns_server)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_set_dns_server(l, l->dns_servers);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return l->current_dns_server;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
3315f085178f46155fda345d9526c09083b45946Lennart Poetteringvoid link_next_dns_server(Link *l) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!l->current_dns_server)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (l->current_dns_server->servers_next) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_set_dns_server(l, l->current_dns_server->servers_next);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_set_dns_server(l, l->dns_servers);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LinkAddress *a;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(l);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(in_addr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a = new0(LinkAddress, 1);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return -ENOMEM;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->family = family;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->in_addr = *in_addr;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link = l;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_PREPEND(addresses, l->addresses, a);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (ret)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen *ret = a;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
77354c7e6f096a447245a8781c1eaa4acbe67089Martin PittLinkAddress *link_address_free(LinkAddress *a) {
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt if (!a)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->link) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_REMOVE(addresses, a->link->addresses, a);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->llmnr_address_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->llmnr_ptr_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_resource_record_unref(a->llmnr_address_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_resource_record_unref(a->llmnr_ptr_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen free(a);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenvoid link_address_add_rrs(LinkAddress *a, bool force_remove) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(a);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->family == AF_INET) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!force_remove &&
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_address_relevant(a) &&
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link->llmnr_ipv4_scope &&
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link->llmnr_support == SUPPORT_YES &&
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link->manager->llmnr_support == SUPPORT_YES) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->link->manager->host_ipv4_key) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link->manager->host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->hostname);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->link->manager->host_ipv4_key) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = -ENOMEM;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto fail;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->llmnr_address_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_address_rr = dns_resource_record_new(a->link->manager->host_ipv4_key);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->llmnr_address_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = -ENOMEM;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto fail;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_address_rr->a.in_addr = a->in_addr.in;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_address_rr->ttl = LLMNR_DEFAULT_TTL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->llmnr_ptr_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->hostname);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto fail;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->link->llmnr_ipv4_scope, a->llmnr_address_rr, true);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_warning("Failed tp add A record to LLMNR zone: %s", strerror(-r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->link->llmnr_ipv4_scope, a->llmnr_ptr_rr, false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_warning("Failed tp add IPv6 PTR record to LLMNR zone: %s", strerror(-r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->llmnr_address_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->link->llmnr_ipv4_scope)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_address_rr = dns_resource_record_unref(a->llmnr_address_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->llmnr_ptr_rr) {
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if (a->link->llmnr_ipv4_scope)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_ptr_rr = dns_resource_record_unref(a->llmnr_ptr_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->family == AF_INET6) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!force_remove &&
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_address_relevant(a) &&
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link->llmnr_ipv6_scope &&
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link->llmnr_support == SUPPORT_YES &&
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link->manager->llmnr_support == SUPPORT_YES) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->link->manager->host_ipv6_key) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->link->manager->host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->hostname);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->link->manager->host_ipv6_key) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = -ENOMEM;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto fail;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->llmnr_address_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_address_rr = dns_resource_record_new(a->link->manager->host_ipv6_key);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->llmnr_address_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = -ENOMEM;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto fail;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_address_rr->aaaa.in6_addr = a->in_addr.in6;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_address_rr->ttl = LLMNR_DEFAULT_TTL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!a->llmnr_ptr_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->hostname);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto fail;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->link->llmnr_ipv6_scope, a->llmnr_address_rr, true);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_warning("Failed to add AAAA record to LLMNR zone: %s", strerror(-r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->link->llmnr_ipv6_scope, a->llmnr_ptr_rr, false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_warning("Failed to add IPv6 PTR record to LLMNR zone: %s", strerror(-r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->llmnr_address_rr) {
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek if (a->link->llmnr_ipv6_scope)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_address_rr = dns_resource_record_unref(a->llmnr_address_rr);
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->llmnr_ptr_rr) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (a->link->llmnr_ipv6_scope)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen a->llmnr_ptr_rr = dns_resource_record_unref(a->llmnr_ptr_rr);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
b7e7184634d573fb73143210962acce205f37f61Michael Biebl return;
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt
b7e7184634d573fb73143210962acce205f37f61Michael Bieblfail:
b7e7184634d573fb73143210962acce205f37f61Michael Biebl log_debug("Failed to update address RRs: %s", strerror(-r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint link_address_update_rtnl(LinkAddress *a, sd_rtnl_message *m) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(a);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_rtnl_message_addr_get_flags(m, &a->flags);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return r;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen sd_rtnl_message_addr_get_scope(m, &a->scope);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_allocate_scopes(a->link);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen link_add_rrs(a->link, false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
805b573fad06b845502e76f3db3a0efa7583149dLukas Nykrynbool link_address_relevant(LinkAddress *a) {
805b573fad06b845502e76f3db3a0efa7583149dLukas Nykryn assert(a);
805b573fad06b845502e76f3db3a0efa7583149dLukas Nykryn
805b573fad06b845502e76f3db3a0efa7583149dLukas Nykryn if (a->flags & IFA_F_DEPRECATED)
805b573fad06b845502e76f3db3a0efa7583149dLukas Nykryn return false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (IN_SET(a->scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE))
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return false;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return true;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen