resolved-link.c revision 0b58db658b5c3f586ac3a837427f1f7fec2abb2e
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart 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 systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersenint link_new(Manager *m, Link **ret, int ifindex) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = hashmap_ensure_allocated(&m->links, NULL);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poetteringstatic void link_allocate_scopes(Link *l) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = dns_scope_new(l->manager, &l->unicast_scope, l, DNS_PROTOCOL_DNS, AF_UNSPEC);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_warning_errno(r, "Failed to allocate DNS scope: %m");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering l->unicast_scope = dns_scope_free(l->unicast_scope);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering l->manager->llmnr_support != SUPPORT_NO) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_warning_errno(r, "Failed to allocate LLMNR IPv4 scope: %m");
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering l->manager->llmnr_support != SUPPORT_NO &&
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning_errno(r, "Failed to allocate LLMNR IPv6 scope: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersenvoid link_add_rrs(Link *l, bool force_remove) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersenint link_update_rtnl(Link *l, sd_netlink_message *m) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen const char *n = NULL;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = sd_rtnl_message_link_get_flags(m, &l->flags);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sd_netlink_message_read_u32(m, IFLA_MTU, &l->mtu);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (sd_netlink_message_read_string(m, IFLA_IFNAME, &n) >= 0) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering _cleanup_strv_free_ char **nameservers = NULL;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = sd_network_link_get_dns(l->ifindex, &nameservers);
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering r = in_addr_from_string_auto(*nameserver, &family, &a);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, family, &a);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringstatic int link_update_llmnr_support(Link *l) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = sd_network_link_get_llmnr(l->ifindex, &b);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering } else if (r > 0)
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering l->unicast_scope->domains = strv_free(l->unicast_scope->domains);
b64513580ce627578351b76a502455e7bc62cae4Lennart 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
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering * one relevant IP address */
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering if (l->flags & (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_DORMANT))
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if ((l->flags & (IFF_UP|IFF_LOWER_UP|IFF_MULTICAST)) != (IFF_UP|IFF_LOWER_UP|IFF_MULTICAST))
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering sd_network_link_get_operational_state(l->ifindex, &state);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (state && !STR_IN_SET(state, "unknown", "degraded", "routable"))
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (a->family == family && link_address_relevant(a))
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom GundersenLinkAddress *link_find_address(Link *l, int family, const union in_addr_union *in_addr) {
efd46a696d31097c38f653b36921e00c4df62319Lennart Poettering if (a->family == family && in_addr_equal(family, &a->in_addr, in_addr))
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poetteringvoid link_flush_marked_dns_servers(Link *l) {
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering LIST_FOREACH_SAFE(servers, s, next, l->dns_servers) {
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart PoetteringDnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr) {
4e0b8b17a7465653f4e7b819dad5f8e30d64c0c4Tom Gundersen if (s->family == family && in_addr_equal(family, &s->address, in_addr))
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart PoetteringDnsServer* link_set_dns_server(Link *l, DnsServer *s) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering in_addr_to_string(s->family, &s->address, &ip);
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering log_info("Switching to DNS server %s for interface %s.", strna(ip), l->name);
b652d4a2099d1c167584dcc1d179d47c58dc38a2Lennart Poettering l->current_dns_server = dns_server_ref(s);
b64513580ce627578351b76a502455e7bc62cae4Lennart Poettering dns_cache_flush(&l->unicast_scope->cache);
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen /* Change to the next one, but make sure to follow the linked
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen * list only if this server is actually still linked. */
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen if (l->current_dns_server->linked && l->current_dns_server->servers_next) {
be808ea083fa07271116b4519c3c27fd20c5f077Tom Gundersen link_set_dns_server(l, l->current_dns_server->servers_next);
011842775f750711833526d5bba1b818713947f5Lennart Poetteringint link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr) {
011842775f750711833526d5bba1b818713947f5Lennart Poettering LIST_PREPEND(addresses, l->addresses, a);
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart PoetteringLinkAddress *link_address_free(LinkAddress *a) {
cc450722a02ab9c59bca1d9a5b5012f356336a8cLennart Poettering LIST_REMOVE(addresses, a->link->addresses, a);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
f4461e5641d53f27d6e76e0607bdaa9c0c58c1f6Lennart Poettering 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_address_rr);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
6a1a5eec43892dee3ff6e208bceb1931c25c782eLennart Poettering else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering dns_resource_record_unref(a->llmnr_address_rr);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering dns_resource_record_unref(a->llmnr_ptr_rr);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poetteringvoid link_address_add_rrs(LinkAddress *a, bool force_remove) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->link->manager->llmnr_support == SUPPORT_YES) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!a->link->manager->llmnr_host_ipv4_key) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->link->manager->llmnr_host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->llmnr_hostname);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering if (!a->link->manager->llmnr_host_ipv4_key) {
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->llmnr_address_rr = dns_resource_record_new(a->link->manager->llmnr_host_ipv4_key);
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 r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->llmnr_hostname);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->link->llmnr_ipv4_scope, a->llmnr_address_rr, true);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering log_warning_errno(r, "Failed to add A record to LLMNR zone: %m");
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->link->llmnr_ipv4_scope, a->llmnr_ptr_rr, false);
de54e62b4bd7856fb897c9a2ee93cc228adb2135Lennart Poettering log_warning_errno(r, "Failed to add IPv6 PTR record to LLMNR zone: %m");
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
6bb2c08597c999c429e889cd2403b2fef5f3e1a0Lennart Poettering a->llmnr_address_rr = dns_resource_record_unref(a->llmnr_address_rr);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering a->llmnr_ptr_rr = dns_resource_record_unref(a->llmnr_ptr_rr);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering a->link->manager->llmnr_support == SUPPORT_YES) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (!a->link->manager->llmnr_host_ipv6_key) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering a->link->manager->llmnr_host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->llmnr_hostname);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering if (!a->link->manager->llmnr_host_ipv6_key) {
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering a->llmnr_address_rr = dns_resource_record_new(a->link->manager->llmnr_host_ipv6_key);
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 r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->llmnr_hostname);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->link->llmnr_ipv6_scope, a->llmnr_address_rr, true);
6cb08a8930bdaca950b152b1e8b82466ed59511cLennart Poettering log_warning_errno(r, "Failed to add AAAA record to LLMNR zone: %m");
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->link->llmnr_ipv6_scope, a->llmnr_ptr_rr, false);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering log_warning_errno(r, "Failed to add IPv6 PTR record to LLMNR zone: %m");
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering a->llmnr_address_rr = dns_resource_record_unref(a->llmnr_address_rr);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
92ec902aad1ade7acbe50efd7b8ef87fbdc63af3Lennart Poettering a->llmnr_ptr_rr = dns_resource_record_unref(a->llmnr_ptr_rr);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen log_debug_errno(r, "Failed to update address RRs: %m");
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersenint link_address_update_rtnl(LinkAddress *a, sd_netlink_message *m) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering r = sd_rtnl_message_addr_get_flags(m, &a->flags);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering sd_rtnl_message_addr_get_scope(m, &a->scope);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (a->flags & (IFA_F_DEPRECATED|IFA_F_TENTATIVE))