resolved-link.c revision 6073b6f26ab9fc6bf335faa7073ec443eef093fd
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"
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
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(ifindex > 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering r = hashmap_ensure_allocated(&m->links, NULL, NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering return r;
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l = new0(Link, 1);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (!l)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering l->ifindex = ifindex;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (r < 0)
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering return r;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering l->manager = m;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (ret)
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering *ret = l;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering l = NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering return 0;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringLink *link_free(Link *l) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!l)
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen return NULL;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering while (l->addresses)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering link_address_free(l->addresses);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen if (l->manager)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering dns_scope_free(l->unicast_scope);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering dns_scope_free(l->llmnr_ipv4_scope);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering dns_scope_free(l->llmnr_ipv6_scope);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering while (l->dns_servers)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering dns_server_free(l->dns_servers);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering free(l);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering return NULL;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart 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) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering r = dns_scope_new(l->manager, &l->unicast_scope, l, DNS_PROTOCOL_DNS, AF_UNSPEC);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to allocate DNS scope: %s", strerror(-r));
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering l->unicast_scope = dns_scope_free(l->unicast_scope);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (link_relevant(l, AF_INET) && l->manager->use_llmnr) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (!l->llmnr_ipv4_scope) {
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering if (r < 0)
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering log_warning("Failed to allocate LLMNR IPv4 scope: %s", strerror(-r));
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (link_relevant(l, AF_INET6) && l->manager->use_llmnr) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!l->llmnr_ipv6_scope) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to allocate LLMNR IPv6 scope: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen}
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersenint link_update_rtnl(Link *l, sd_rtnl_message *m) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen const char *n = NULL;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen int r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert(l);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert(m);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = sd_rtnl_message_link_get_flags(m, &l->flags);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0)
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen return r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart 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));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char_array_0(l->name);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering link_allocate_scopes(l);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 0;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic int link_update_dns_servers(Link *l) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering _cleanup_free_ struct in_addr *nameservers = NULL;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen _cleanup_free_ struct in6_addr *nameservers6 = NULL;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering DnsServer *s, *nx;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering int r, n, i;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(l);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering LIST_FOREACH(servers, s, l->dns_servers)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->marked = true;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering n = sd_network_get_dns(l->ifindex, &nameservers);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (n < 0) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = n;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering goto clear;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering for (i = 0; i < n; i++) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering union in_addr_union a = { .in = nameservers[i] };
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s = link_find_dns_server(l, AF_INET, &a);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering if (s)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->marked = false;
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering else {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = dns_server_new(l->manager, NULL, l, AF_INET, &a);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering goto clear;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering n = sd_network_get_dns6(l->ifindex, &nameservers6);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (n < 0) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = n;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering goto clear;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen for (i = 0; i < n; i++) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen union in_addr_union a = { .in6 = nameservers6[i] };
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering s = link_find_dns_server(l, AF_INET6, &a);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (s)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering s->marked = false;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering else {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = dns_server_new(l->manager, NULL, l, AF_INET6, &a);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering goto clear;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_FOREACH_SAFE(servers, s, nx, l->dns_servers)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering 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 Poetteringint link_update_monitor(Link *l) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(l);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link_update_dns_servers(l);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link_allocate_scopes(l);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return 0;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringbool link_relevant(Link *l, int family) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering _cleanup_free_ char *state = NULL;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LinkAddress *a;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(l);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering /* A link is relevant if it isn't a loopback device and has at
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering * least one relevant IP address */
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen if (l->flags & IFF_LOOPBACK)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen return false;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering sd_network_get_link_operational_state(l->ifindex, &state);
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering if (state && !STR_IN_SET(state, "unknown", "degraded", "routable"))
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering return false;
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering LIST_FOREACH(addresses, a, l->addresses)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen if (a->family == family && link_address_relevant(a))
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen return true;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen return false;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen}
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
84129d46cd6e95e142973da93aede4c7433c9600Lennart PoetteringLinkAddress *link_find_address(Link *l, int family, union in_addr_union *in_addr) {
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering LinkAddress *a;
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen assert(l);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen LIST_FOREACH(addresses, a, l->addresses)
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (a->family == family && in_addr_equal(family, &a->in_addr, in_addr))
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return a;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return NULL;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen}
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart PoetteringDnsServer* link_find_dns_server(Link *l, int family, union in_addr_union *in_addr) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering DnsServer *s;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering assert(l);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering LIST_FOREACH(servers, s, l->dns_servers)
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (s->family == family && in_addr_equal(family, &s->address, in_addr))
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return s;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return NULL;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering}
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal SchmidtDnsServer *link_get_dns_server(Link *l) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt assert(l);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (!l->current_dns_server)
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt l->current_dns_server = l->dns_servers;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering return l->current_dns_server;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poetteringvoid link_next_dns_server(Link *l) {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering assert(l);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering /* Switch to the next DNS server */
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering if (!l->current_dns_server) {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering l->current_dns_server = l->dns_servers;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (l->current_dns_server)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering }
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering if (!l->current_dns_server)
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering return;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering if (l->current_dns_server->servers_next) {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering l->current_dns_server = l->current_dns_server->servers_next;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering return;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering l->current_dns_server = l->dns_servers;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering}
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poetteringint link_address_new(Link *l, LinkAddress **ret, int family, union in_addr_union *in_addr) {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering LinkAddress *a;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering assert(l);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering assert(in_addr);
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering a = new0(LinkAddress, 1);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering if (!a)
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering return -ENOMEM;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering a->family = family;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering a->in_addr = *in_addr;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering a->link = l;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering LIST_PREPEND(addresses, l->addresses, a);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering if (ret)
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering *ret = a;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering return 0;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart PoetteringLinkAddress *link_address_free(LinkAddress *a) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (!a)
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering return NULL;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (a->link)
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering LIST_REMOVE(addresses, a->link->addresses, a);
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering free(a);
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering return NULL;
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering}
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poetteringint link_address_update_rtnl(LinkAddress *a, sd_rtnl_message *m) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering int r;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(a);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(m);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = sd_rtnl_message_addr_get_flags(m, &a->flags);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return r;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering sd_rtnl_message_addr_get_scope(m, &a->scope);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link_allocate_scopes(a->link);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return 0;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringbool link_address_relevant(LinkAddress *a) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(a);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (a->flags & IFA_F_DEPRECATED)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return false;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (IN_SET(a->scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE))
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return false;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return true;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering