resolved-link.c revision d5099efc47d4e6ac60816b5381a5f607ab03f06e
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
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <net/if.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include "sd-network.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "strv.h"
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen#include "missing.h"
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include "resolved-link.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersenint link_new(Manager *m, Link **ret, int ifindex) {
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen _cleanup_(link_freep) Link *l = NULL;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen int r;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert(m);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert(ifindex > 0);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen r = hashmap_ensure_allocated(&m->links, NULL);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen if (r < 0)
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return r;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l = new0(Link, 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!l)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->ifindex = ifindex;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering l->llmnr_support = SUPPORT_YES;
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering l->manager = m;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ret)
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering *ret = l;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering l = NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering return 0;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering}
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart PoetteringLink *link_free(Link *l) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (!l)
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering return NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while (l->addresses)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering link_address_free(l->addresses);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l->manager)
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering dns_scope_free(l->unicast_scope);
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering dns_scope_free(l->llmnr_ipv4_scope);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen dns_scope_free(l->llmnr_ipv6_scope);
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering while (l->dns_servers)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dns_server_free(l->dns_servers);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen free(l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringstatic void link_allocate_scopes(Link *l) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering int r;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering assert(l);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (l->dns_servers) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (!l->unicast_scope) {
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering r = dns_scope_new(l->manager, &l->unicast_scope, l, DNS_PROTOCOL_DNS, AF_UNSPEC);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (r < 0)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_warning("Failed to allocate DNS scope: %s", strerror(-r));
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering } else
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering l->unicast_scope = dns_scope_free(l->unicast_scope);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (link_relevant(l, AF_INET) &&
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering l->llmnr_support != SUPPORT_NO &&
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering l->manager->llmnr_support != SUPPORT_NO) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (!l->llmnr_ipv4_scope) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_warning("Failed to allocate LLMNR IPv4 scope: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering } else
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (link_relevant(l, AF_INET6) &&
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering l->llmnr_support != SUPPORT_NO &&
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering l->manager->llmnr_support != SUPPORT_NO &&
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering socket_ipv6_is_supported()) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (!l->llmnr_ipv6_scope) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to allocate LLMNR IPv6 scope: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringvoid link_add_rrs(Link *l, bool force_remove) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LinkAddress *a;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen LIST_FOREACH(addresses, a, l->addresses)
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering link_address_add_rrs(a, force_remove);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen}
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersenint link_update_rtnl(Link *l, sd_rtnl_message *m) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering const char *n = NULL;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen int r;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering assert(l);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(m);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen r = sd_rtnl_message_link_get_flags(m, &l->flags);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering sd_rtnl_message_read_u32(m, IFLA_MTU, &l->mtu);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (sd_rtnl_message_read_string(m, IFLA_IFNAME, &n) >= 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering strncpy(l->name, n, sizeof(l->name)-1);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering char_array_0(l->name);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering link_allocate_scopes(l);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen link_add_rrs(l, false);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 0;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic int link_update_dns_servers(Link *l) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering _cleanup_strv_free_ char **nameservers = NULL;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen char **nameserver;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering DnsServer *s, *nx;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering int r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(l);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = sd_network_link_get_dns(l->ifindex, &nameservers);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering goto clear;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering LIST_FOREACH(servers, s, l->dns_servers)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->marked = true;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering STRV_FOREACH(nameserver, nameservers) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering union in_addr_union a;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering int family;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = in_addr_from_string_auto(*nameserver, &family, &a);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering goto clear;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s = link_find_dns_server(l, family, &a);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (s)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->marked = false;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering else {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, family, &a);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering goto clear;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen LIST_FOREACH_SAFE(servers, s, nx, l->dns_servers)
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen if (s->marked)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering dns_server_free(s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return 0;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringclear:
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering while (l->dns_servers)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering dns_server_free(l->dns_servers);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return r;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringstatic int link_update_llmnr_support(Link *l) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering _cleanup_free_ char *b = NULL;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering int r;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(l);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = sd_network_link_get_llmnr(l->ifindex, &b);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering goto clear;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = parse_boolean(b);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (streq(b, "resolve"))
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering l->llmnr_support = SUPPORT_RESOLVE;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering else
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering goto clear;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering } else if (r > 0)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering l->llmnr_support = SUPPORT_YES;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering else
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering l->llmnr_support = SUPPORT_NO;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return 0;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringclear:
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering l->llmnr_support = SUPPORT_YES;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return r;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringstatic int link_update_domains(Link *l) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering int r;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen if (!l->unicast_scope)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen return 0;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering strv_free(l->unicast_scope->domains);
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering l->unicast_scope->domains = NULL;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = sd_network_link_get_domains(l->ifindex,
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering &l->unicast_scope->domains);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (r < 0)
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering return r;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return 0;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering}
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poetteringint link_update_monitor(Link *l) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering assert(l);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering link_update_dns_servers(l);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering link_update_llmnr_support(l);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering link_allocate_scopes(l);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering link_update_domains(l);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering link_add_rrs(l, false);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering return 0;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering}
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poetteringbool link_relevant(Link *l, int family) {
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering _cleanup_free_ char *state = NULL;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering LinkAddress *a;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering assert(l);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering /* A link is relevant if it isn't a loopback or pointopoint
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering * device, has a link beat, can do multicast and has at least
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering * one relevant IP address */
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (l->flags & (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_DORMANT))
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering return false;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if ((l->flags & (IFF_UP|IFF_LOWER_UP|IFF_MULTICAST)) != (IFF_UP|IFF_LOWER_UP|IFF_MULTICAST))
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering return false;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering sd_network_link_get_operational_state(l->ifindex, &state);
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering if (state && !STR_IN_SET(state, "unknown", "degraded", "routable"))
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering return false;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen LIST_FOREACH(addresses, a, l->addresses)
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen if (a->family == family && link_address_relevant(a))
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen return true;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen return false;
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen}
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom GundersenLinkAddress *link_find_address(Link *l, int family, const union in_addr_union *in_addr) {
efd46a696d31097c38f653b36921e00c4df62319Lennart Poettering LinkAddress *a;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen assert(l);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering LIST_FOREACH(addresses, a, l->addresses)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen if (a->family == family && in_addr_equal(family, &a->in_addr, in_addr))
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return a;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering return NULL;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering}
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart PoetteringDnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering DnsServer *s;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen assert(l);
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering LIST_FOREACH(servers, s, l->dns_servers)
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering if (s->family == family && in_addr_equal(family, &s->address, in_addr))
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering return s;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen return NULL;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen}
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom GundersenDnsServer* link_set_dns_server(Link *l, DnsServer *s) {
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen assert(l);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (l->current_dns_server == s)
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering return s;
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen if (s) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering _cleanup_free_ char *ip = NULL;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering in_addr_to_string(s->family, &s->address, &ip);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering log_info("Switching to DNS server %s for interface %s.", strna(ip), l->name);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering }
571370c1555d2aa697733479a50957aff024bbcbLennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering l->current_dns_server = s;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (l->unicast_scope)
571370c1555d2aa697733479a50957aff024bbcbLennart Poettering dns_cache_flush(&l->unicast_scope->cache);
571370c1555d2aa697733479a50957aff024bbcbLennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering return s;
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen}
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart PoetteringDnsServer *link_get_dns_server(Link *l) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering assert(l);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!l->current_dns_server)
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering link_set_dns_server(l, l->dns_servers);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering return l->current_dns_server;
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering}
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poetteringvoid link_next_dns_server(Link *l) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering assert(l);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!l->current_dns_server)
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering return;
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering if (l->current_dns_server->servers_next) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering link_set_dns_server(l, l->current_dns_server->servers_next);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering return;
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering }
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering link_set_dns_server(l, l->dns_servers);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering}
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poetteringint link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr) {
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering LinkAddress *a;
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert(l);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen assert(in_addr);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen a = new0(LinkAddress, 1);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen if (!a)
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return -ENOMEM;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen a->family = family;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen a->in_addr = *in_addr;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen a->link = l;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen LIST_PREPEND(addresses, l->addresses, a);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen if (ret)
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen *ret = a;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen return 0;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen}
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
011842775f750711833526d5bba1b818713947f5Lennart PoetteringLinkAddress *link_address_free(LinkAddress *a) {
011842775f750711833526d5bba1b818713947f5Lennart Poettering if (!a)
011842775f750711833526d5bba1b818713947f5Lennart Poettering return NULL;
011842775f750711833526d5bba1b818713947f5Lennart Poettering
011842775f750711833526d5bba1b818713947f5Lennart Poettering if (a->link) {
011842775f750711833526d5bba1b818713947f5Lennart Poettering LIST_REMOVE(addresses, a->link->addresses, a);
011842775f750711833526d5bba1b818713947f5Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (a->llmnr_address_rr) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
011842775f750711833526d5bba1b818713947f5Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
011842775f750711833526d5bba1b818713947f5Lennart Poettering else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
011842775f750711833526d5bba1b818713947f5Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering }
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen if (a->llmnr_ptr_rr) {
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
011842775f750711833526d5bba1b818713947f5Lennart Poettering }
011842775f750711833526d5bba1b818713947f5Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering dns_resource_record_unref(a->llmnr_address_rr);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering dns_resource_record_unref(a->llmnr_ptr_rr);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering free(a);
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering return NULL;
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering}
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersenvoid link_address_add_rrs(LinkAddress *a, bool force_remove) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering int r;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
6a1a5eec43892dee3ff6e208bceb1931c25c782eLennart Poettering assert(a);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (a->family == AF_INET) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!force_remove &&
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering link_address_relevant(a) &&
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->link->llmnr_ipv4_scope &&
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->link->llmnr_support == SUPPORT_YES &&
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->link->manager->llmnr_support == SUPPORT_YES) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!a->link->manager->host_ipv4_key) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->link->manager->host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->hostname);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!a->link->manager->host_ipv4_key) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = -ENOMEM;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering goto fail;
6a1a5eec43892dee3ff6e208bceb1931c25c782eLennart Poettering }
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!a->llmnr_address_rr) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering a->llmnr_address_rr = dns_resource_record_new(a->link->manager->host_ipv4_key);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!a->llmnr_address_rr) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = -ENOMEM;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering goto fail;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->llmnr_address_rr->a.in_addr = a->in_addr.in;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->llmnr_address_rr->ttl = LLMNR_DEFAULT_TTL;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!a->llmnr_ptr_rr) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->hostname);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (r < 0)
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering goto fail;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->link->llmnr_ipv4_scope, a->llmnr_address_rr, true);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (r < 0)
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering log_warning("Failed to add A record to LLMNR zone: %s", strerror(-r));
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->link->llmnr_ipv4_scope, a->llmnr_ptr_rr, false);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (r < 0)
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering log_warning("Failed to add IPv6 PTR record to LLMNR zone: %s", strerror(-r));
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering } else {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (a->llmnr_address_rr) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (a->link->llmnr_ipv4_scope)
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->llmnr_address_rr = dns_resource_record_unref(a->llmnr_address_rr);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (a->llmnr_ptr_rr) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (a->link->llmnr_ipv4_scope)
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->llmnr_ptr_rr = dns_resource_record_unref(a->llmnr_ptr_rr);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering }
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (a->family == AF_INET6) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!force_remove &&
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering link_address_relevant(a) &&
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering a->link->llmnr_ipv6_scope &&
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering a->link->llmnr_support == SUPPORT_YES &&
011842775f750711833526d5bba1b818713947f5Lennart Poettering a->link->manager->llmnr_support == SUPPORT_YES) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!a->link->manager->host_ipv6_key) {
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering a->link->manager->host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->hostname);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering if (!a->link->manager->host_ipv6_key) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering r = -ENOMEM;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen goto fail;
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen }
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering }
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen if (!a->llmnr_address_rr) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering a->llmnr_address_rr = dns_resource_record_new(a->link->manager->host_ipv6_key);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (!a->llmnr_address_rr) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering r = -ENOMEM;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering goto fail;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering }
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering a->llmnr_address_rr->aaaa.in6_addr = a->in_addr.in6;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering a->llmnr_address_rr->ttl = LLMNR_DEFAULT_TTL;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering }
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (!a->llmnr_ptr_rr) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->hostname);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (r < 0)
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering goto fail;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering }
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->link->llmnr_ipv6_scope, a->llmnr_address_rr, true);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (r < 0)
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering log_warning("Failed to add AAAA record to LLMNR zone: %s", strerror(-r));
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->link->llmnr_ipv6_scope, a->llmnr_ptr_rr, false);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (r < 0)
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering log_warning("Failed to add IPv6 PTR record to LLMNR zone: %s", strerror(-r));
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering } else {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (a->llmnr_address_rr) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (a->link->llmnr_ipv6_scope)
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering a->llmnr_address_rr = dns_resource_record_unref(a->llmnr_address_rr);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering }
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering if (a->llmnr_ptr_rr) {
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering if (a->link->llmnr_ipv6_scope)
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering a->llmnr_ptr_rr = dns_resource_record_unref(a->llmnr_ptr_rr);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering }
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering }
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering }
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering return;
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poetteringfail:
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering log_debug("Failed to update address RRs: %s", strerror(-r));
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering}
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poetteringint link_address_update_rtnl(LinkAddress *a, sd_rtnl_message *m) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering int r;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering assert(a);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering assert(m);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering r = sd_rtnl_message_addr_get_flags(m, &a->flags);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering if (r < 0)
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering return r;
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering sd_rtnl_message_addr_get_scope(m, &a->scope);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering link_allocate_scopes(a->link);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen link_add_rrs(a->link, false);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return 0;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen}
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersenbool link_address_relevant(LinkAddress *a) {
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen assert(a);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (a->flags & (IFA_F_DEPRECATED|IFA_F_TENTATIVE))
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt return false;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (IN_SET(a->scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE))
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return false;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return true;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering}
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering