networkd-link.c revision 56cd007ab83749b85670c9c7f560e083980c2ff4
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2013 Tom Gundersen <teg@jklm.no>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int ipv4ll_address_update(Link *link, bool deprecate);
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poetteringstatic bool ipv4ll_is_bound(sd_ipv4ll *ll);
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poetteringint link_new(Manager *manager, struct udev_device *device, Link **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_link_free_ Link *link = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->state = _LINK_STATE_INVALID;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->ifindex = udev_device_get_ifindex(device);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = asprintf(&link->state_file, "/run/systemd/network/links/%"PRIu64,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ifname = udev_device_get_sysname(device);
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering r = hashmap_put(manager->links, &link->ifindex, link);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->udev_device = udev_device_ref(device);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_dhcp_client_unref(link->dhcp_client);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek hashmap_remove(link->manager->links, &link->ifindex);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_unref(link->udev_device);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint link_get(Manager *m, int ifindex, Link **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link = hashmap_get(m->links, &ifindex_64);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int link_enter_configured(Link *link) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(link->state == LINK_STATE_SETTING_ROUTES);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info_link(link, "link configured");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->state = LINK_STATE_CONFIGURED;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void link_enter_failed(Link *link) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "failed");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->state = LINK_STATE_FAILED;
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimerstatic int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(link->route_messages > 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(link->state == LINK_STATE_SETTING_ADDRESSES ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->state == LINK_STATE_SETTING_ROUTES ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->state == LINK_STATE_FAILED);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && r != -EEXIST)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not set route: %s",
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek /* we might have received an old reply after moving back to SETTING_ADDRESSES,
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek if (link->route_messages == 0 && link->state == LINK_STATE_SETTING_ROUTES) {
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek log_debug_link(link, "routes set");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int link_enter_set_routes(Link *link) {
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering assert(link->state == LINK_STATE_SETTING_ADDRESSES);
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering if (!link->network->static_routes && !link->dhcp_lease &&
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false))
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return link_enter_configured(link);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek log_debug_link(link, "setting routes");
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek LIST_FOREACH(static_routes, rt, link->network->static_routes) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek r = route_configure(rt, link, &route_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "could not set routes: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (link->ipv4ll && !link->dhcp_lease) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (r < 0 && r != -ENOENT) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "IPV4LL error: no address: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate route: %s",
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt r = route_configure(route, link, &route_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "could not set routes: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route_gw = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no router: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate route: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = route_new_dynamic(&route_gw);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate route: %s",
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering /* The dhcp netmask may mask out the gateway. Add an explicit
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * route for the gw host so that we can route no matter the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * netmask or existing kernel route tables. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek route_gw->dst_addr.in = gateway;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek route_gw->scope = RT_SCOPE_LINK;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = route_configure(route_gw, link, &route_handler);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "could not set host route: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = route_configure(route, link, &route_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "could not set routes: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not drop route: %s",
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekstatic int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(link->addr_messages > 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(link->state == LINK_STATE_SETTING_ADDRESSES || link->state == LINK_STATE_FAILED);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && r != -EEXIST)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not set address: %s",
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt "ERRNO=%d", -r,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_link(link, "addresses set");
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic int link_enter_set_addresses(Link *link) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert(link->state != _LINK_STATE_INVALID);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (!link->network->static_addresses && !link->dhcp_lease &&
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false))
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering log_debug_link(link, "setting addresses");
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering LIST_FOREACH(static_addresses, ad, link->network->static_addresses) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt r = address_configure(ad, link, &address_handler);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "could not set addresses: %s", strerror(-r));
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering _cleanup_address_free_ Address *ll_addr = NULL;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek log_warning_link(link, "IPV4LL error: no address: %s",
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = address_new_dynamic(&ll_addr);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate address: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ll_addr->broadcast.s_addr = ll_addr->in_addr.in.s_addr | htonl(0xfffffffflu >> ll_addr->prefixlen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = address_configure(ll_addr, link, &address_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "could not set addresses: %s", strerror(-r));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering _cleanup_address_free_ Address *address = NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_warning_link(link, "DHCP error: no address: %s",
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_warning_link(link, "DHCP error: no netmask: %s",
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering prefixlen = net_netmask_to_prefixlen(&netmask);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_error_link(link, "Could not allocate address: %s",
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering address->broadcast.s_addr = addr.s_addr | ~netmask.s_addr;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = address_configure(address, link, &address_handler);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "could not set addresses: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int address_update_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not update address: %s",
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poetteringstatic int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not drop address: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_bus_message_get_errno(m);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Could not set hostname: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int set_hostname(sd_bus *bus, const char *hostname) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Setting transient hostname: '%s'", hostname);
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering if (!bus) { /* TODO: replace by assert when we can rely on kdbus */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("Not connected to system bus, ignoring transient hostname.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_call(
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering r = sd_bus_message_append(m, "sb", hostname, false);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_bus_call_async(bus, m, set_hostname_handler, NULL, 0, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Could not set transient hostname: %s", strerror(-r));
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poetteringstatic int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not set MTU: %s",
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering "ERRNO=%d", -r,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int link_set_mtu(Link *link, uint32_t mtu) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_link(link, "setting MTU: %" PRIu32, mtu);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate RTM_SETLINK message");
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not append MTU: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Could not send rtnetlink message: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int dhcp_lease_lost(Link *link) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_address_free_ Address *address = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route_gw = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route = NULL;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering log_warning_link(link, "DHCP lease lost");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = address_new_dynamic(&address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering prefixlen = net_netmask_to_prefixlen(&netmask);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek route_gw->dst_addr.in = gateway;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek route_gw->scope = RT_SCOPE_LINK;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek route_drop(route_gw, link, &route_drop_handler);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek route_drop(route, link, &route_drop_handler);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering address_drop(address, link, &address_drop_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (r >= 0 && link->original_mtu != mtu) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = link_set_mtu(link, link->original_mtu);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering log_warning_link(link, "DHCP error: could not reset MTU");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering r = set_hostname(link->manager->bus, "");
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering log_error("Failed to reset transient hostname");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = sd_dhcp_client_get_lease(client, &lease);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no lease: %s",
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = sd_dhcp_lease_get_address(lease, &address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no address: %s",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_netmask(lease, &netmask);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no netmask: %s",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek prefixlen = net_netmask_to_prefixlen(&netmask);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_router(lease, &gateway);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no router: %s",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "ADDRESS=%u.%u.%u.%u",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "GATEWAY=%u.%u.%u.%u",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = manager_update_resolv_conf(link->manager);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Failed to update resolv.conf");
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_mtu(lease, &mtu);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error_link(link, "Failed to set MTU "
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (link->network->dhcp_hostname) {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering r = sd_dhcp_lease_get_hostname(lease, &hostname);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = set_hostname(link->manager->bus, hostname);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Failed to set transient hostname "
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poetteringstatic void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_link(link, "IP address in use.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (link->network->dhcp_critical) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "DHCPv4 connection considered system critical, "
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "ignoring request to reconfigure it.");
7517e17443225fafea86b21e42a36af69feb1dbcTorstein Husebø if (event == DHCP_EVENT_EXPIRED && link->network->ipv4ll) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dhcp_lease_acquired(client, link);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (ipv4ll_is_bound(link->ipv4ll))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = ipv4ll_address_update(link, true);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_stop(link->ipv4ll);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: %s", strerror(-event));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP unknown event: %d", event);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int ipv4ll_address_update(Link *link, bool deprecate) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_address_free_ Address *address = NULL;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering log_debug_link(link, "IPv4 link-local %s %u.%u.%u.%u",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = address_new_dynamic(&address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate address: %s", strerror(-r));
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther address->cinfo.ifa_prefered = deprecate ? 0 : CACHE_INFO_INFINITY_LIFE_TIME;
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther address->broadcast.s_addr = address->in_addr.in.s_addr | htonl(0xfffffffflu >> address->prefixlen);
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther address_update(address, link, &address_update_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int ipv4ll_address_lost(Link *link) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_address_free_ Address *address = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_link(link, "IPv4 link-local release %u.%u.%u.%u",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = address_new_dynamic(&address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate address: %s", strerror(-r));
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek address_drop(address, link, &address_drop_handler);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering log_error_link(link, "Could not allocate route: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek route_drop(route, link, &route_drop_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic bool ipv4ll_is_bound(sd_ipv4ll *ll) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_get_address(ll, &addr);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_get_address(ll, &address);
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn "MESSAGE=%s: IPv4 link-local address %u.%u.%u.%u",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata){
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = ipv4ll_address_claimed(ll, link);
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering log_warning_link(link, "IPv4 link-local error: %s", strerror(-event));
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering log_warning_link(link, "IPv4 link-local unknown event: %d", event);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_link(link, "acquiring IPv4 link-local address");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_start(link->ipv4ll);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_link(link, "acquiring DHCPv4 lease");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_dhcp_client_start(link->dhcp_client);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int link_update_flags(Link *link, unsigned flags) {
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt log_debug_link(link, "link status updated: %#.8x -> %#.8x",
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering if ((link->flags & IFF_UP) != (flags & IFF_UP))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "link is %s", flags & IFF_UP ? "up": "down");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((link->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info_link(link, "carrier on");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (link->network->dhcp || link->network->ipv4ll) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "Could not acquire configuration: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info_link(link, "carrier off");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_dhcp_client_stop(link->dhcp_client);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r));
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering log_warning_link(link, "Could not stop IPv4 link-local: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering link_update_flags(link, link->flags | IFF_UP);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not bring up interface: %s",
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering "ERRNO=%d", -r,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering log_debug_link(link, "bringing link up");
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering log_error_link(link, "Could not allocate RTM_SETLINK message");
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering log_error_link(link, "Could not set link flags: %s", strerror(-r));
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering "Could not send rtnetlink message: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int link_enslaved(Link *link) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(link->state == LINK_STATE_ENSLAVING);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!link->network->dhcp && !link->network->ipv4ll)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(link->state == LINK_STATE_ENSLAVING || link->state == LINK_STATE_FAILED);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering "MESSAGE=%s: could not enslave: %s",
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering "ERRNO=%d", -r,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic int link_enter_enslave(Link *link) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(link->state == _LINK_STATE_INVALID);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!link->network->bridge && !link->network->bond &&
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering hashmap_isempty(link->network->macvlans))
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering "MESSAGE=%s: enslaving by '%s'",
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering link->ifname, link->network->bridge->name,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = netdev_enslave(link->network->bridge, link, &enslave_handler);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering "MESSAGE=%s: could not enslave by '%s': %s",
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering link->ifname, link->network->bridge->name, strerror(-r),
3bfd4e0c6341b0ef946d2198f089743fa99e0a97WaLyong Cho "MESSAGE=%s: enslaving by '%s'",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->ifname, link->network->bond->name,
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = netdev_enslave(link->network->bond, link, &enslave_handler);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering "MESSAGE=%s: could not enslave by '%s': %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->ifname, link->network->bond->name, strerror(-r),
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering HASHMAP_FOREACH(vlan, link->network->vlans, i) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "MESSAGE=%s: enslaving by '%s'",
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering link->ifname, vlan->name, NETDEV(vlan), NULL);
4daf54a851e4fb7ed1a13c3117bba12528fd2c7fZbigniew Jędrzejewski-Szmek r = netdev_enslave(vlan, link, &enslave_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not enslave by '%s': %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->ifname, vlan->name, strerror(-r),
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering HASHMAP_FOREACH(macvlan, link->network->macvlans, i) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering "MESSAGE=%s: enslaving by '%s'",
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering link->ifname, macvlan->name, NETDEV(macvlan), NULL);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = netdev_enslave(macvlan, link, &enslave_handler);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering "MESSAGE=%s: could not enslave by '%s': %s",
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering link->ifname, macvlan->name, strerror(-r),
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmekstatic int link_getlink_handler(sd_rtnl *rtnl, sd_rtnl_message *m,
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not get state: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int link_getlink(Link *link) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen log_debug_link(link, "requesting link status");
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen log_error_link(link, "Could not allocate RTM_GETLINK message");
b2fadec6048adb3596f2633cb7fe7a49f5937a18Zbigniew Jędrzejewski-Szmek r = sd_rtnl_call_async(link->manager->rtnl, req, link_getlink_handler,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Could not send rtnetlink message: %s", strerror(-r));
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(link->state == _LINK_STATE_INVALID);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poetteringint link_add(Manager *m, struct udev_device *device, Link **ret) {
ca2670162464b98f44d3f30a1d8b47b02609784cMichał Bartoszkiewicz return r == -ENOENT ? 0 : r;
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering r = net_get_unique_predictable_data(link->udev_device, seed);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = sd_ipv4ll_set_callback(link->ipv4ll, ipv4ll_handler, link);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = sd_dhcp_client_new(&link->dhcp_client);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp_handler, link);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt r = sd_dhcp_client_set_request_option(link->dhcp_client, 26);
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidtint link_update(Link *link, sd_rtnl_message *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (link->network->dhcp && link->network->dhcp_mtu &&
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_read_u16(m, IFLA_MTU, &link->original_mtu);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_link(link, "saved original MTU: %"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk log_debug_link(link, "Could not get MAC address: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_link(link, "MAC address: "
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "Could not update MAC "
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "address in IPv4LL client: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt log_warning_link(link, "Could not update MAC "
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "address in DHCP client: %s",
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt r = sd_rtnl_message_link_get_flags(m, &flags);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_link(link, "Could not get link flags");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *temp_path = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_fclose_ FILE *f = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek state = link_state_to_string(link->state);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = fopen_temporary(link->state_file, &f, &temp_path);
03ee5c38cb0da193dd08733fb4c0c2809cee6a99Lennart Poettering "# This is private data. Do not parse.\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *lease_file = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = asprintf(&lease_file, "/run/systemd/network/leases/%"PRIu64,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dhcp_lease_save(link->dhcp_lease, lease_file);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering fprintf(f, "DHCP_LEASE=%s\n", lease_file);
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering if (ferror(f) || rename(temp_path, link->state_file) < 0) {
13790add4bf648fed816361794d8277a75253410Lennart Poettering log_error("Failed to save link data %s: %s", link->state_file, strerror(-r));
13790add4bf648fed816361794d8277a75253410Lennart Poetteringstatic const char* const link_state_table[_LINK_STATE_MAX] = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek [LINK_STATE_ENSLAVING] = "configuring",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek [LINK_STATE_SETTING_ADDRESSES] = "configuring",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek [LINK_STATE_SETTING_ROUTES] = "configuring",