resolved-link.c revision 19b50b5ba7ee8c1bfb330377309e4bab7a7531d8
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
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
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
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/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <net/if.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "sd-network.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "strv.h"
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include "missing.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "resolved-link.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_new(Manager *m, Link **ret, int ifindex) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_(link_freep) Link *l = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(ifindex > 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = hashmap_ensure_allocated(&m->links, NULL, NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l = new0(Link, 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!l)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->ifindex = ifindex;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering l->llmnr_support = SUPPORT_YES;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->manager = m;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ret)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *ret = l;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringLink *link_free(Link *l) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!l)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while (l->addresses)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering link_address_free(l->addresses);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l->manager)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dns_scope_free(l->unicast_scope);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering dns_scope_free(l->llmnr_ipv4_scope);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering dns_scope_free(l->llmnr_ipv6_scope);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen while (l->dns_servers)
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen dns_server_free(l->dns_servers);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering}
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic void link_allocate_scopes(Link *l) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering int r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert(l);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen if (l->dns_servers) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (!l->unicast_scope) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering r = dns_scope_new(l->manager, &l->unicast_scope, l, DNS_PROTOCOL_DNS, AF_UNSPEC);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (r < 0)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_warning("Failed to allocate DNS scope: %s", strerror(-r));
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering } else
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering l->unicast_scope = dns_scope_free(l->unicast_scope);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering if (link_relevant(l, AF_INET) && l->llmnr_support != SUPPORT_NO && l->manager->llmnr_support != SUPPORT_NO) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (!l->llmnr_ipv4_scope) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (r < 0)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_warning("Failed to allocate LLMNR IPv4 scope: %s", strerror(-r));
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering } else
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering if (link_relevant(l, AF_INET6) && l->llmnr_support != SUPPORT_NO && l->manager->llmnr_support != SUPPORT_NO) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (!l->llmnr_ipv6_scope) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (r < 0)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_warning("Failed to allocate LLMNR IPv6 scope: %s", strerror(-r));
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering } else
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poetteringvoid link_add_rrs(Link *l, bool force_remove) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering LinkAddress *a;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering LIST_FOREACH(addresses, a, l->addresses)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering link_address_add_rrs(a, force_remove);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_update_rtnl(Link *l, sd_rtnl_message *m) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering const char *n = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_rtnl_message_link_get_flags(m, &l->flags);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
c5ed93163e6ef51a7462aa558a7e0912b17c4951Lennart Poettering sd_rtnl_message_read_u32(m, IFLA_MTU, &l->mtu);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (sd_rtnl_message_read_string(m, IFLA_IFNAME, &n) >= 0) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering strncpy(l->name, n, sizeof(l->name));
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering char_array_0(l->name);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering link_allocate_scopes(l);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering link_add_rrs(l, false);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersenstatic int link_update_dns_servers(Link *l) {
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen _cleanup_strv_free_ char **nameservers = NULL;
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen char **nameserver;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DnsServer *s, *nx;
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen r = sd_network_get_dns(l->ifindex, &nameservers);
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto clear;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering LIST_FOREACH(servers, s, l->dns_servers)
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering s->marked = true;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen STRV_FOREACH(nameserver, nameservers) {
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen union in_addr_union a;
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen int family;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen r = in_addr_from_string_auto(*nameserver, &family, &a);
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen if (r < 0)
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen goto clear;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen s = link_find_dns_server(l, family, &a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (s)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s->marked = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, family, &a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto clear;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen LIST_FOREACH_SAFE(servers, s, nx, l->dns_servers)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (s->marked)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dns_server_free(s);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringclear:
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen while (l->dns_servers)
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen dns_server_free(l->dns_servers);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poetteringstatic int link_update_llmnr_support(Link *l) {
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering _cleanup_free_ char *b = NULL;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering int r;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering assert(l);
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering r = sd_network_get_llmnr(l->ifindex, &b);
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering if (r < 0)
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering goto clear;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering r = parse_boolean(b);
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering if (r < 0) {
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering if (streq(b, "resolve"))
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering l->llmnr_support = SUPPORT_RESOLVE;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering else
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering goto clear;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering } else if (r > 0)
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering l->llmnr_support = SUPPORT_YES;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering else
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering l->llmnr_support = SUPPORT_NO;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering return 0;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poetteringclear:
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering l->llmnr_support = SUPPORT_YES;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering return r;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering}
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_update_monitor(Link *l) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen link_update_dns_servers(l);
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering link_update_llmnr_support(l);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering link_allocate_scopes(l);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering link_add_rrs(l, false);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringbool link_relevant(Link *l, int family) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering _cleanup_free_ char *state = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LinkAddress *a;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering /* A link is relevant if it isn't a loopback or pointopoint
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * device, has a link beat, can do multicast and has at least
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * one relevant IP address */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (l->flags & (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_DORMANT))
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering return false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if ((l->flags & (IFF_UP|IFF_LOWER_UP|IFF_MULTICAST)) != (IFF_UP|IFF_LOWER_UP|IFF_MULTICAST))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering sd_network_get_link_operational_state(l->ifindex, &state);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (state && !STR_IN_SET(state, "unknown", "degraded", "routable"))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FOREACH(addresses, a, l->addresses)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (a->family == family && link_address_relevant(a))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart PoetteringLinkAddress *link_find_address(Link *l, int family, const union in_addr_union *in_addr) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LinkAddress *a;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering LIST_FOREACH(addresses, a, l->addresses)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (a->family == family && in_addr_equal(family, &a->in_addr, in_addr))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return a;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart PoetteringDnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr) {
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen DnsServer *s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen LIST_FOREACH(servers, s, l->dns_servers)
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (s->family == family && in_addr_equal(family, &s->address, in_addr))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
2c27fbca2d88214bd305272308a370a962818f1eLennart PoetteringDnsServer* link_set_dns_server(Link *l, DnsServer *s) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert(l);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (l->current_dns_server == s)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering return s;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (s) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering _cleanup_free_ char *ip = NULL;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering in_addr_to_string(s->family, &s->address, &ip);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_info("Switching to DNS server %s for interface %s.", strna(ip), l->name);
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering }
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering l->current_dns_server = s;
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering if (l->unicast_scope)
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering dns_cache_flush(&l->unicast_scope->cache);
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering return s;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering}
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsServer *link_get_dns_server(Link *l) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!l->current_dns_server)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering link_set_dns_server(l, l->dns_servers);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return l->current_dns_server;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringvoid link_next_dns_server(Link *l) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!l->current_dns_server)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l->current_dns_server->servers_next) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering link_set_dns_server(l, l->current_dns_server->servers_next);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering link_set_dns_server(l, l->dns_servers);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LinkAddress *a;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(in_addr);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering a = new0(LinkAddress, 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!a)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering a->family = family;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering a->in_addr = *in_addr;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering a->link = l;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_PREPEND(addresses, l->addresses, a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ret)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *ret = a;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringLinkAddress *link_address_free(LinkAddress *a) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!a)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (a->link) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_REMOVE(addresses, a->link->addresses, a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (a->llmnr_address_rr) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (a->llmnr_ptr_rr) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering dns_resource_record_unref(a->llmnr_address_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering dns_resource_record_unref(a->llmnr_ptr_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poetteringvoid link_address_add_rrs(LinkAddress *a, bool force_remove) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(a);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->family == AF_INET) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (!force_remove &&
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering link_address_relevant(a) &&
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering a->link->llmnr_ipv4_scope &&
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering a->link->llmnr_support == SUPPORT_YES &&
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering a->link->manager->llmnr_support == SUPPORT_YES) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!a->link->manager->host_ipv4_key) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->link->manager->host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->hostname);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (!a->link->manager->host_ipv4_key) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = -ENOMEM;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto fail;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!a->llmnr_address_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_address_rr = dns_resource_record_new(a->link->manager->host_ipv4_key);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (!a->llmnr_address_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = -ENOMEM;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto fail;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_address_rr->a.in_addr = a->in_addr.in;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_address_rr->ttl = LLMNR_DEFAULT_TTL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (!a->llmnr_ptr_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->hostname);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (r < 0)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto fail;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->link->llmnr_ipv4_scope, a->llmnr_address_rr, true);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_warning("Failed tp add A record to LLMNR zone: %s", strerror(-r));
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->link->llmnr_ipv4_scope, a->llmnr_ptr_rr, false);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_warning("Failed tp add IPv6 PTR record to LLMNR zone: %s", strerror(-r));
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering } else {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->llmnr_address_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->link->llmnr_ipv4_scope)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_address_rr = dns_resource_record_unref(a->llmnr_address_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->llmnr_ptr_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->link->llmnr_ipv4_scope)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_ptr_rr = dns_resource_record_unref(a->llmnr_ptr_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->family == AF_INET6) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (!force_remove &&
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering link_address_relevant(a) &&
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering a->link->llmnr_ipv6_scope &&
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering a->link->llmnr_support == SUPPORT_YES &&
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering a->link->manager->llmnr_support == SUPPORT_YES) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!a->link->manager->host_ipv6_key) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->link->manager->host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->hostname);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (!a->link->manager->host_ipv6_key) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = -ENOMEM;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto fail;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!a->llmnr_address_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_address_rr = dns_resource_record_new(a->link->manager->host_ipv6_key);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (!a->llmnr_address_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = -ENOMEM;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto fail;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_address_rr->aaaa.in6_addr = a->in_addr.in6;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_address_rr->ttl = LLMNR_DEFAULT_TTL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (!a->llmnr_ptr_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->hostname);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (r < 0)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering goto fail;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->link->llmnr_ipv6_scope, a->llmnr_address_rr, true);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_warning("Failed to add AAAA record to LLMNR zone: %s", strerror(-r));
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->link->llmnr_ipv6_scope, a->llmnr_ptr_rr, false);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_warning("Failed to add IPv6 PTR record to LLMNR zone: %s", strerror(-r));
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering } else {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->llmnr_address_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->link->llmnr_ipv6_scope)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_address_rr = dns_resource_record_unref(a->llmnr_address_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->llmnr_ptr_rr) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (a->link->llmnr_ipv6_scope)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering a->llmnr_ptr_rr = dns_resource_record_unref(a->llmnr_ptr_rr);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringfail:
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_debug("Failed to update address RRs: %s", strerror(-r));
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_address_update_rtnl(LinkAddress *a, sd_rtnl_message *m) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_rtnl_message_addr_get_flags(m, &a->flags);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering sd_rtnl_message_addr_get_scope(m, &a->scope);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering link_allocate_scopes(a->link);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering link_add_rrs(a->link, false);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringbool link_address_relevant(LinkAddress *a) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (a->flags & IFA_F_DEPRECATED)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (IN_SET(a->scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}