networkd-wait-online-link.c revision d6731e4c7964ee2860d4f5abdb0b52acd7a66960
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen/***
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen This file is part of systemd.
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen Copyright 2014 Lennart Poettering
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen Copyright 2014 Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen systemd is free software; you can redistribute it and/or modify it
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen under the terms of the GNU Lesser General Public License as published by
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen (at your option) any later version.
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen systemd is distributed in the hope that it will be useful, but
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen Lesser General Public License for more details.
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen You should have received a copy of the GNU Lesser General Public License
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen***/
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen#include <net/if.h>
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen#include "sd-network.h"
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen#include "strv.h"
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen#include "networkd-wait-online-link.h"
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersenint link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen _cleanup_(link_freep) Link *l = NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen int r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(m);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(ifindex > 0);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = hashmap_ensure_allocated(&m->links, NULL, NULL);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = hashmap_ensure_allocated(&m->links_by_name,
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen string_hash_func, string_compare_func);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l = new0(Link, 1);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!l)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return -ENOMEM;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->manager = m;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->ifname = strdup(ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!l->ifname)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return -ENOMEM;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = hashmap_put(m->links_by_name, l->ifname, l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->ifindex = ifindex;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (ret)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen *ret = l;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l = NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return 0;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen}
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom GundersenLink *link_free(Link *l) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!l)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (l->manager) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen hashmap_remove(l->manager->links_by_name, l->ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen }
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen free(l->ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen free(l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen }
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersenint link_update_rtnl(Link *l, sd_rtnl_message *m) {
d243773263630f1cbc5868a12476b17c335abec8Tom Gundersen const char *ifname;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen int r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(l->manager);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(m);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = sd_rtnl_message_link_get_flags(m, &l->flags);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!streq(l->ifname, ifname)) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen char *new_ifname;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen new_ifname = strdup(ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!new_ifname)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return -ENOMEM;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen hashmap_remove(l->manager->links_by_name, l->ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen free(l->ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->ifname = new_ifname;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = hashmap_put(l->manager->links_by_name, l->ifname, l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen }
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return 0;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen}
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersenint link_update_monitor(Link *l) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen free(l->operational_state);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->operational_state = NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
d6731e4c7964ee2860d4f5abdb0b52acd7a66960Tom Gundersen sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen free(l->state);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->state = NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
d6731e4c7964ee2860d4f5abdb0b52acd7a66960Tom Gundersen sd_network_link_get_state(l->ifindex, &l->state);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return 0;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen}
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersenbool link_relevant(Link *l) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (l->flags & IFF_LOOPBACK)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return false;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return true;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen}