networkd-link.c revision 628706137efbca8aaf753ccd063e5abf7e31aed5
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 2013 Tom Gundersen <teg@jklm.no>
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 <netinet/ether.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <linux/if.h>
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "networkd.h"
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include "libudev-private.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "util.h"
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersenint link_new(Manager *manager, struct udev_device *device, Link **ret) {
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen _cleanup_link_free_ Link *link = NULL;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen const char *mac;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct ether_addr *mac_addr;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *ifname;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(device);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering assert(ret);
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering link = new0(Link, 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!link)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering link->manager = manager;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering link->state = _LINK_STATE_INVALID;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering link->ifindex = udev_device_get_ifindex(device);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (link->ifindex <= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen mac = udev_device_get_sysattr_value(device, "address");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (mac) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering mac_addr = ether_aton(mac);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (mac_addr)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memcpy(&link->mac, mac_addr, sizeof(struct ether_addr));
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering ifname = udev_device_get_sysname(device);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link->ifname = strdup(ifname);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = hashmap_put(manager->links, &link->ifindex, link);
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen if (r < 0)
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen return r;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering *ret = link;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringvoid link_free(Link *link) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (!link)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering return;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(link->manager);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (link->dhcp)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering sd_dhcp_client_free(link->dhcp);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering route_free(link->dhcp_route);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering link->dhcp_route = NULL;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering address_free(link->dhcp_address);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering link->dhcp_address = NULL;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering hashmap_remove(link->manager->links, &link->ifindex);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering free(link->ifname);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(link);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_add(Manager *m, struct udev_device *device, Link **ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Link *link;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Network *network;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint64_t ifindex;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *devtype;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert(m);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert(device);
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen ifindex = udev_device_get_ifindex(device);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen link = hashmap_get(m->links, &ifindex);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen if (link) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering *ret = link;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen return -EEXIST;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen }
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = link_new(m, device, &link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0)
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen return r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering *ret = link;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering devtype = udev_device_get_devtype(device);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (streq_ptr(devtype, "bridge")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = bridge_set_link(m, link);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (r < 0 && r != -ENOENT)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen r = network_get(m, device, &network);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return r == -ENOENT ? 0 : r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen r = network_apply(m, network, link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (r < 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return r;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 0;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic int link_enter_configured(Link *link) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(link->state == LINK_STATE_SETTING_ROUTES);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_link_info(link, "link configured");
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering link->state = LINK_STATE_CONFIGURED;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 0;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic void link_enter_failed(Link *link) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_link_warning(link, "failed");
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering link->state = LINK_STATE_FAILED;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering Link *link = userdata;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering int r;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert(link->route_messages > 0);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(link->state == LINK_STATE_SETTING_ADDRESSES ||
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link->state == LINK_STATE_SETTING_ROUTES ||
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link->state == LINK_STATE_FAILED);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link->route_messages --;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (link->state == LINK_STATE_FAILED)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return 1;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = sd_rtnl_message_get_errno(m);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0 && r != -EEXIST)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_warning_link(link, "could not set route: %s", strerror(-r));
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering /* we might have received an old reply after moving back to SETTING_ADDRESSES,
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering * ignore it */
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (link->route_messages == 0 && link->state == LINK_STATE_SETTING_ROUTES) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_link_debug(link, "routes set");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link_enter_configured(link);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return 1;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringstatic int link_enter_set_routes(Link *link) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering Route *route;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering int r;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(link);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(link->network);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(link->state == LINK_STATE_SETTING_ADDRESSES);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link->state = LINK_STATE_SETTING_ROUTES;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (!link->network->static_routes && !link->dhcp_route)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return link_enter_configured(link);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_link_debug(link, "setting routes");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_FOREACH(static_routes, route, link->network->static_routes) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = route_configure(route, link, &route_handler);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_warning_link(link,
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen "could not set routes: %s", strerror(-r));
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen link_enter_failed(link);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen return r;
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering }
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering link->route_messages ++;
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering }
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen if (link->dhcp_route) {
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen r = route_configure(link->dhcp_route, link, &route_handler);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen if (r < 0) {
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen log_warning_link(link,
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen "could not set routes: %s", strerror(-r));
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering link_enter_failed(link);
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering return r;
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering }
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen link->route_messages ++;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen }
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return 0;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering}
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poetteringstatic int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen Link *link = userdata;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen int r;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering assert(m);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt assert(link);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering assert(link->ifname);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering assert(link->addr_messages > 0);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering assert(link->state == LINK_STATE_SETTING_ADDRESSES || link->state == LINK_STATE_FAILED);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering link->addr_messages --;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (link->state == LINK_STATE_FAILED)
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return 1;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt r = sd_rtnl_message_get_errno(m);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (r < 0 && r != -EEXIST)
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt log_struct_link(LOG_ERR, link,
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt "MESSAGE=%s: could not set address: %s",
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt link->ifname, strerror(-r),
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering "ERRNO=%d", -r,
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering NULL);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (link->addr_messages == 0) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_link_debug(link, "addresses set");
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering link_enter_set_routes(link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 1;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poetteringstatic int link_enter_set_addresses(Link *link) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering Address *address;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering int r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(link->network);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(link->state != _LINK_STATE_INVALID);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering link->state = LINK_STATE_SETTING_ADDRESSES;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (!link->network->static_addresses && !link->dhcp_address)
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering return link_enter_set_routes(link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_link_debug(link, "setting addresses");
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering LIST_FOREACH(static_addresses, address, link->network->static_addresses) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = address_configure(address, link, &address_handler);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering if (r < 0) {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering log_warning_link(link,
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering "could not set addresses: %s", strerror(-r));
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering link_enter_failed(link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return r;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering }
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering link->addr_messages ++;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (link->dhcp_address) {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering r = address_configure(link->dhcp_address, link, &address_handler);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering if (r < 0) {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering log_warning_link(link,
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering "could not set addresses: %s", strerror(-r));
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering link_enter_failed(link);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering return r;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering }
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering link->addr_messages ++;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering }
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering return 0;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poetteringstatic int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering Link *link = userdata;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering int r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(m);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(link);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(link->ifname);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (link->state == LINK_STATE_FAILED)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return 1;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = sd_rtnl_message_get_errno(m);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0 && r != -EEXIST)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_warning_link(link, "could not drop address: %s", strerror(-r));
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return 1;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poetteringstatic void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering Link *link = userdata;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering struct in_addr address;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering struct in_addr netmask;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering struct in_addr gateway;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering int prefixlen;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering int r;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(link);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (link->state == LINK_STATE_FAILED)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (event < 0) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_warning_link(link, "DHCP error: %s", strerror(-event));
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link_enter_failed(link);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (event == DHCP_EVENT_NO_LEASE)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_debug(link, "IP address in use.");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_EXPIRED ||
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering event == DHCP_EVENT_STOP) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (link->dhcp_address) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering address_drop(link->dhcp_address, link, address_drop_handler);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering address_free(link->dhcp_address);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link->dhcp_address = NULL;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (link->dhcp_route) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering route_free(link->dhcp_route);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link->dhcp_route = NULL;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = sd_dhcp_client_get_address(client, &address);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_warning(link, "DHCP error: no address");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link_enter_failed(link);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = sd_dhcp_client_get_netmask(client, &netmask);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_warning(link, "DHCP error: no netmask");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link_enter_failed(link);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering prefixlen = sd_dhcp_client_prefixlen(&netmask);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (prefixlen < 0) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_warning(link, "DHCP error: no prefixlen");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link_enter_failed(link);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = sd_dhcp_client_get_router(client, &gateway);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_warning(link, "DHCP error: no router");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link_enter_failed(link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_IP_ACQUIRE) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering _cleanup_address_free_ Address *addr = NULL;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering _cleanup_route_free_ Route *rt = NULL;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_struct_link(LOG_INFO, link,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering link->ifname,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering ADDRESS_FMT_VAL(address),
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering prefixlen,
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering ADDRESS_FMT_VAL(gateway),
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering "ADDRESS=%u.%u.%u.%u",
ADDRESS_FMT_VAL(address),
"PREFIXLEN=%u",
prefixlen,
"GATEWAY=%u.%u.%u.%u",
ADDRESS_FMT_VAL(gateway),
NULL);
r = address_new_dynamic(&addr);
if (r < 0) {
log_link_error(link, "Could not allocate address");
link_enter_failed(link);
return;
}
addr->family = AF_INET;
addr->in_addr.in = address;
addr->prefixlen = prefixlen;
addr->netmask = netmask;
r = route_new_dynamic(&rt);
if (r < 0) {
log_link_error(link, "Could not allocate route");
link_enter_failed(link);
return;
}
rt->family = AF_INET;
rt->in_addr.in = gateway;
link->dhcp_address = addr;
link->dhcp_route = rt;
addr = NULL;
rt = NULL;
link_enter_set_addresses(link);
}
return;
}
static int link_acquire_conf(Link *link) {
int r;
assert(link);
assert(link->network);
assert(link->network->dhcp);
assert(link->manager);
assert(link->manager->event);
if (!link->dhcp) {
link->dhcp = sd_dhcp_client_new(link->manager->event);
if (!link->dhcp)
return -ENOMEM;
r = sd_dhcp_client_set_index(link->dhcp, link->ifindex);
if (r < 0)
return r;
r = sd_dhcp_client_set_mac(link->dhcp, &link->mac);
if (r < 0)
return r;
r = sd_dhcp_client_set_callback(link->dhcp, dhcp_handler, link);
if (r < 0)
return r;
}
r = sd_dhcp_client_start(link->dhcp);
if (r < 0)
return r;
return 0;
}
static int link_update_flags(Link *link, unsigned flags) {
int r;
assert(link);
assert(link->network);
if (link->state == LINK_STATE_FAILED)
return 0;
if (link->flags == flags) {
log_debug_link(link, "link status unchanged: %#x", flags);
return 0;
}
if ((link->flags & IFF_UP) != (flags & IFF_UP))
log_info_link(link,
"power %s", flags & IFF_UP ? "on": "off");
if ((link->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
if (flags & IFF_LOWER_UP) {
log_link_info(link, "carrier on");
if (link->network->dhcp) {
r = link_acquire_conf(link);
if (r < 0) {
link_enter_failed(link);
return r;
}
}
} else {
log_link_info(link, "carrier off");
if (link->network->dhcp) {
r = sd_dhcp_client_stop(link->dhcp);
if (r < 0) {
link_enter_failed(link);
return r;
}
}
}
}
log_debug_link(link,
"link status updated: %#x -> %#x", link->flags, flags);
link->flags = flags;
return 0;
}
static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
Link *link = userdata;
int r;
assert(link);
if (link->state == LINK_STATE_FAILED)
return 1;
r = sd_rtnl_message_get_errno(m);
if (r < 0) {
log_warning_link(link,
"could not bring up interface: %s", strerror(-r));
link_enter_failed(link);
}
link_update_flags(link, link->flags | IFF_UP);
return 1;
}
static int link_up(Link *link) {
_cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
int r;
assert(link);
assert(link->manager);
assert(link->manager->rtnl);
log_link_debug(link, "bringing link up");
r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, &req);
if (r < 0) {
log_link_error(link, "Could not allocate RTM_SETLINK message");
return r;
}
r = sd_rtnl_message_link_set_flags(req, IFF_UP);
if (r < 0) {
log_error_link(link, "Could not set link flags: %s", strerror(-r));
return r;
}
r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL);
if (r < 0) {
log_error_link(link,
"Could not send rtnetlink message: %s", strerror(-r));
return r;
}
return 0;
}
static int link_bridge_joined(Link *link) {
int r;
assert(link);
assert(link->state == LINK_STATE_JOINING_BRIDGE);
assert(link->network);
r = link_up(link);
if (r < 0) {
link_enter_failed(link);
return r;
}
if (!link->network->dhcp)
return link_enter_set_addresses(link);
return 0;
}
static int bridge_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
Link *link = userdata;
int r;
assert(link);
assert(link->state == LINK_STATE_JOINING_BRIDGE || link->state == LINK_STATE_FAILED);
assert(link->network);
if (link->state == LINK_STATE_FAILED)
return 1;
r = sd_rtnl_message_get_errno(m);
if (r < 0) {
log_struct_link(LOG_ERR, link,
"MESSAGE=%s: could not join bridge '%s': %s",
link->ifname, link->network->bridge->name, strerror(-r),
BRIDGE(link->network->bridge),
NULL);
link_enter_failed(link);
return 1;
}
log_struct_link(LOG_DEBUG, link,
"MESSAGE=%s: joined bridge '%s'",
link->network->bridge->name,
BRIDGE(link->network->bridge),
NULL);
link_bridge_joined(link);
return 1;
}
static int link_enter_join_bridge(Link *link) {
int r;
assert(link);
assert(link->network);
assert(link->state == _LINK_STATE_INVALID);
link->state = LINK_STATE_JOINING_BRIDGE;
if (!link->network->bridge)
return link_bridge_joined(link);
log_struct_link(LOG_DEBUG, link,
"MESSAGE=%s: joining bridge '%s'",
link->network->bridge->name,
BRIDGE(link->network->bridge),
NULL);
log_link_debug(link, "joining bridge");
r = bridge_join(link->network->bridge, link, &bridge_handler);
if (r < 0) {
log_struct_link(LOG_WARNING, link,
"MESSAGE=%s: could not join bridge '%s': %s",
link->network->bridge->name, strerror(-r),
BRIDGE(link->network->bridge),
NULL);
link_enter_failed(link);
return r;
}
return 0;
}
static int link_get_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
Link *link = userdata;
int r;
assert(link);
if (link->state == LINK_STATE_FAILED)
return 1;
r = sd_rtnl_message_get_errno(m);
if (r < 0) {
log_warning_link(link, "could not get state: %s", strerror(-r));
link_enter_failed(link);
}
log_link_debug(link, "got link state");
link_update(link, m);
return 1;
}
static int link_get(Link *link) {
_cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
int r;
assert(link);
assert(link->manager);
assert(link->manager->rtnl);
log_link_debug(link, "requesting link status");
r = sd_rtnl_message_link_new(RTM_GETLINK, link->ifindex, &req);
if (r < 0) {
log_link_error(link, "Could not allocate RTM_GETLINK message");
return r;
}
r = sd_rtnl_call_async(link->manager->rtnl, req, link_get_handler, link, 0, NULL);
if (r < 0) {
log_error_link(link,
"Could not send rtnetlink message: %s", strerror(-r));
return r;
}
return 0;
}
int link_configure(Link *link) {
int r;
assert(link);
assert(link->network);
assert(link->state == _LINK_STATE_INVALID);
r = link_get(link);
if (r < 0) {
link_enter_failed(link);
return r;
}
return link_enter_join_bridge(link);
}
int link_update(Link *link, sd_rtnl_message *m) {
unsigned flags;
int r;
assert(link);
assert(m);
if (link->state == LINK_STATE_FAILED)
return 0;
r = sd_rtnl_message_link_get_flags(m, &flags);
if (r < 0) {
log_link_warning(link, "could not get link flags");
return r;
}
return link_update_flags(link, flags);
}