resolved-link.c revision 8bea3d6f887e336295a9cc24169f329c05f7383f
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2014 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/* RFC 4795 Section 2.8. suggests a TTL of 30s by default */
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic void link_address_add_rrs(LinkAddress *a);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringint link_new(Manager *m, Link **ret, int ifindex) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_ensure_allocated(&m->links, NULL, NULL);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void link_allocate_scopes(Link *l) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = dns_scope_new(l->manager, &l->unicast_scope, l, DNS_PROTOCOL_DNS, AF_UNSPEC);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen log_warning("Failed to allocate DNS scope: %s", strerror(-r));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen l->unicast_scope = dns_scope_free(l->unicast_scope);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (link_relevant(l, AF_INET) && (l->flags & IFF_MULTICAST) && l->manager->use_llmnr) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_warning("Failed to allocate LLMNR IPv4 scope: %s", strerror(-r));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (link_relevant(l, AF_INET6) && (l->flags & IFF_MULTICAST) && l->manager->use_llmnr) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_warning("Failed to allocate LLMNR IPv6 scope: %s", strerror(-r));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint link_update_rtnl(Link *l, sd_rtnl_message *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *n = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_rtnl_message_link_get_flags(m, &l->flags);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering sd_rtnl_message_read_u32(m, IFLA_MTU, &l->mtu);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (sd_rtnl_message_read_string(m, IFLA_IFNAME, &n) >= 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int link_update_dns_servers(Link *l) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering _cleanup_strv_free_ char **nameservers = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_network_get_dns(l->ifindex, &nameservers);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = in_addr_from_string_auto(*nameserver, &family, &a);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = dns_server_new(l->manager, NULL, l, family, &a);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering LIST_FOREACH_SAFE(servers, s, nx, l->dns_servers)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering /* A link is relevant if it isn't a loopback device and has at
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering * least one relevant IP address */
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering sd_network_get_link_operational_state(l->ifindex, &state);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (state && !STR_IN_SET(state, "unknown", "degraded", "routable"))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (a->family == family && link_address_relevant(a))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart PoetteringLinkAddress *link_find_address(Link *l, int family, const union in_addr_union *in_addr) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (a->family == family && in_addr_equal(family, &a->in_addr, in_addr))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart PoetteringDnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (s->family == family && in_addr_equal(family, &s->address, in_addr))
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* Switch to the next DNS server */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (l->current_dns_server->servers_next) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering l->current_dns_server = l->current_dns_server->servers_next;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poetteringint link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr) {
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering LIST_PREPEND(addresses, l->addresses, a);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart PoetteringLinkAddress *link_address_free(LinkAddress *a) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering LIST_REMOVE(addresses, a->link->addresses, a);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering dns_resource_record_unref(a->llmnr_address_rr);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering else if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering dns_resource_record_unref(a->llmnr_ptr_rr);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic void link_address_add_rrs(LinkAddress *a) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering if (a->family == AF_INET && a->link->llmnr_ipv4_scope) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering a->link->manager->host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->hostname);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering a->llmnr_address_rr = dns_resource_record_new(a->link->manager->host_ipv4_key);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering a->llmnr_address_rr->a.in_addr = a->in_addr.in;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering a->llmnr_address_rr->ttl = LLMNR_DEFAULT_TTL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->hostname);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_address_rr);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv4_scope->zone, a->llmnr_ptr_rr);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (a->family == AF_INET6 && a->link->llmnr_ipv6_scope) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering a->link->manager->host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->hostname);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering a->llmnr_address_rr = dns_resource_record_new(a->link->manager->host_ipv6_key);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering a->llmnr_address_rr->aaaa.in6_addr = a->in_addr.in6;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering a->llmnr_address_rr->ttl = LLMNR_DEFAULT_TTL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = dns_resource_record_new_reverse(&a->llmnr_ptr_rr, a->family, &a->in_addr, a->link->manager->hostname);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering a->llmnr_ptr_rr->ttl = LLMNR_DEFAULT_TTL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = dns_zone_put(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_address_rr);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers dns_zone_remove_rr(&a->link->llmnr_ipv6_scope->zone, a->llmnr_ptr_rr);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_debug("Failed to update address RRs: %s", strerror(-r));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenint link_address_update_rtnl(LinkAddress *a, sd_rtnl_message *m) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering r = sd_rtnl_message_addr_get_flags(m, &a->flags);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_rtnl_message_addr_get_scope(m, &a->scope);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringbool link_address_relevant(LinkAddress *a) {