resolved-link.c revision 74b2466e14a1961bf3ac0e8a60cfaceec705bd59
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/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_new(Manager *m, Link **ret, int ifindex) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = hashmap_ensure_allocated(&m->links, NULL, NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_update_rtnl(Link *l, sd_rtnl_message *m) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_rtnl_message_link_get_flags(m, &l->flags);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int update_dhcp_dns_servers(Link *l) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ struct in_addr *nameservers = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_network_get_dhcp_lease(l->ifindex, &lease);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FOREACH(servers, s, l->dhcp_dns_servers)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_dhcp_lease_get_dns(lease, &nameservers, &n);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (i = 0; i < n; i++) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering union in_addr_union a = { .in = nameservers[i] };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s = link_find_dns_server(l, DNS_SERVER_DHCP, AF_INET, &a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_server_new(l->manager, NULL, DNS_SERVER_DHCP, l, AF_INET, &a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FOREACH_SAFE(servers, s, nx, l->dhcp_dns_servers)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int update_link_dns_servers(Link *l) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ struct in_addr *nameservers = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ struct in6_addr *nameservers6 = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FOREACH(servers, s, l->link_dns_servers)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_network_get_dns(l->ifindex, &nameservers, &n);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (i = 0; i < n; i++) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering union in_addr_union a = { .in = nameservers[i] };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s = link_find_dns_server(l, DNS_SERVER_LINK, AF_INET, &a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, AF_INET, &a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_network_get_dns6(l->ifindex, &nameservers6, &n);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (i = 0; i < n; i++) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering union in_addr_union a = { .in6 = nameservers6[i] };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s = link_find_dns_server(l, DNS_SERVER_LINK, AF_INET6, &a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, AF_INET6, &a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FOREACH_SAFE(servers, s, nx, l->link_dns_servers)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sd_network_get_link_operational_state(l->ifindex, &l->operational_state);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* A link is relevant if it isn't a loopback device and has at
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * least one relevant IP address */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l->operational_state && !STR_IN_SET(l->operational_state, "unknown", "degraded", "routable"))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringLinkAddress *link_find_address(Link *l, unsigned char family, union in_addr_union *in_addr) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FOREACH(addresses, a, l->addresses) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering in_addr_equal(family, &a->in_addr, in_addr))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsServer* link_find_dns_server(Link *l, DnsServerSource source, unsigned char family, union in_addr_union *in_addr) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering first = source == DNS_SERVER_DHCP ? l->dhcp_dns_servers : l->link_dns_servers;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering in_addr_equal(family, &s->address, in_addr))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->current_dns_server = l->link_dns_servers;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->current_dns_server = l->dhcp_dns_servers;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Switch to the next DNS server */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->current_dns_server = l->link_dns_servers;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->current_dns_server = l->dhcp_dns_servers;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l->current_dns_server->servers_next) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->current_dns_server = l->current_dns_server->servers_next;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l->current_dns_server->source == DNS_SERVER_LINK)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->current_dns_server = l->dhcp_dns_servers;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(l->current_dns_server->source == DNS_SERVER_DHCP);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l->current_dns_server = l->link_dns_servers;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_address_new(Link *l, LinkAddress **ret, unsigned char family, union in_addr_union *in_addr) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_PREPEND(addresses, l->addresses, a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringLinkAddress *link_address_free(LinkAddress *a) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_REMOVE(addresses, a->link->addresses, a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_address_update_rtnl(LinkAddress *a, sd_rtnl_message *m) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_rtnl_message_addr_get_flags(m, &a->flags);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_rtnl_message_addr_get_scope(m, &a->scope);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringbool link_address_relevant(LinkAddress *a) {