X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-link.c;h=f1c2889ba4992cca66bb28c922705a53c529b9e7;hb=5972fe953ec56c77936a1e612ca87d8a0e6c0c64;hp=3c683a0ce0fa0ec8bafa2eabd98cb4130236f650;hpb=628706137efbca8aaf753ccd063e5abf7e31aed5;p=elogind.git diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 3c683a0ce..f1c2889ba 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -25,6 +25,7 @@ #include "networkd.h" #include "libudev-private.h" #include "util.h" +#include "bus-util.h" int link_new(Manager *manager, struct udev_device *device, Link **ret) { _cleanup_link_free_ Link *link = NULL; @@ -134,7 +135,7 @@ static int link_enter_configured(Link *link) { assert(link); assert(link->state == LINK_STATE_SETTING_ROUTES); - log_link_info(link, "link configured"); + log_info_link(link, "link configured"); link->state = LINK_STATE_CONFIGURED; @@ -144,7 +145,7 @@ static int link_enter_configured(Link *link) { static void link_enter_failed(Link *link) { assert(link); - log_link_warning(link, "failed"); + log_warning_link(link, "failed"); link->state = LINK_STATE_FAILED; } @@ -170,7 +171,7 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { /* we might have received an old reply after moving back to SETTING_ADDRESSES, * ignore it */ if (link->route_messages == 0 && link->state == LINK_STATE_SETTING_ROUTES) { - log_link_debug(link, "routes set"); + log_debug_link(link, "routes set"); link_enter_configured(link); } @@ -190,7 +191,7 @@ static int link_enter_set_routes(Link *link) { if (!link->network->static_routes && !link->dhcp_route) return link_enter_configured(link); - log_link_debug(link, "setting routes"); + log_debug_link(link, "setting routes"); LIST_FOREACH(static_routes, route, link->network->static_routes) { r = route_configure(route, link, &route_handler); @@ -243,7 +244,7 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { NULL); if (link->addr_messages == 0) { - log_link_debug(link, "addresses set"); + log_debug_link(link, "addresses set"); link_enter_set_routes(link); } @@ -263,7 +264,7 @@ static int link_enter_set_addresses(Link *link) { if (!link->network->static_addresses && !link->dhcp_address) return link_enter_set_routes(link); - log_link_debug(link, "setting addresses"); + log_debug_link(link, "setting addresses"); LIST_FOREACH(static_addresses, address, link->network->static_addresses) { r = address_configure(address, link, &address_handler); @@ -310,6 +311,100 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat return 1; } +static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { + int r; + + r = sd_bus_message_get_errno(m); + if (r < 0) + log_warning("Could not set hostname: %s", strerror(-r)); + + return 1; +} + +static int set_hostname(sd_bus *bus, const char *hostname) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + int r = 0; + + assert(hostname); + + log_debug("Setting transient hostname: '%s'", hostname); + + if (!bus) { /* TODO: replace by assert when we can rely on kdbus */ + log_info("Not connected to system bus, ignoring transient hostname."); + return 0; + } + + r = sd_bus_message_new_method_call( + bus, + "org.freedesktop.hostname1", + "/org/freedesktop/hostname1", + "org.freedesktop.hostname1", + "SetHostname", + &m); + if (r < 0) + return r; + + r = sd_bus_message_append(m, "sb", hostname, false); + if (r < 0) + return r; + + r = sd_bus_call_async(bus, m, set_hostname_handler, NULL, 0, NULL); + if (r < 0) + log_error("Could not set transient hostname: %s", strerror(-r)); + + return r; +} + +static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { + Link *link = userdata; + int r; + + assert(m); + assert(link); + assert(link->ifname); + + if (link->state == LINK_STATE_FAILED) + return 1; + + r = sd_rtnl_message_get_errno(m); + if (r < 0 && r != -EEXIST) + log_warning_link(link, "Could not set MTU: %s", strerror(-r)); + + return 1; +} + +static int link_set_mtu(Link *link, uint32_t mtu) { + _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL; + int r; + + assert(link); + assert(link->manager); + assert(link->manager->rtnl); + + log_debug_link(link, "setting MTU: %" PRIu32, mtu); + + r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, &req); + if (r < 0) { + log_error_link(link, "Could not allocate RTM_SETLINK message"); + return r; + } + + r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu); + if (r < 0) { + log_error_link(link, "Could not append MTU: %s", strerror(-r)); + return r; + } + + r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL); + if (r < 0) { + log_error_link(link, + "Could not send rtnetlink message: %s", strerror(-r)); + return r; + } + + return 0; +} + static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { Link *link = userdata; struct in_addr address; @@ -319,6 +414,8 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { int r; assert(link); + assert(link->network); + assert(link->manager); if (link->state == LINK_STATE_FAILED) return; @@ -330,7 +427,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { } if (event == DHCP_EVENT_NO_LEASE) - log_link_debug(link, "IP address in use."); + log_debug_link(link, "IP address in use."); if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_EXPIRED || event == DHCP_EVENT_STOP) { @@ -345,32 +442,52 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { route_free(link->dhcp_route); link->dhcp_route = NULL; } + + if (link->network->dhcp_mtu) { + uint16_t mtu; + + r = sd_dhcp_client_get_mtu(client, &mtu); + if (r >= 0 && link->original_mtu != mtu) { + r = link_set_mtu(link, link->original_mtu); + if (r < 0) { + log_warning_link(link, "DHCP error: could not reset MTU"); + link_enter_failed(link); + return; + } + } + } + + if (link->network->dhcp_hostname) { + r = set_hostname(link->manager->bus, ""); + if (r < 0) + log_error("Failed to reset transient hostname"); + } } r = sd_dhcp_client_get_address(client, &address); if (r < 0) { - log_link_warning(link, "DHCP error: no address"); + log_warning_link(link, "DHCP error: no address"); link_enter_failed(link); return; } r = sd_dhcp_client_get_netmask(client, &netmask); if (r < 0) { - log_link_warning(link, "DHCP error: no netmask"); + log_warning_link(link, "DHCP error: no netmask"); link_enter_failed(link); return; } prefixlen = sd_dhcp_client_prefixlen(&netmask); if (prefixlen < 0) { - log_link_warning(link, "DHCP error: no prefixlen"); + log_warning_link(link, "DHCP error: no prefixlen"); link_enter_failed(link); return; } r = sd_dhcp_client_get_router(client, &gateway); if (r < 0) { - log_link_warning(link, "DHCP error: no router"); + log_warning_link(link, "DHCP error: no router"); link_enter_failed(link); return; } @@ -378,6 +495,8 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_IP_ACQUIRE) { _cleanup_address_free_ Address *addr = NULL; _cleanup_route_free_ Route *rt = NULL; + struct in_addr *nameservers; + size_t nameservers_size; log_struct_link(LOG_INFO, link, "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", @@ -395,7 +514,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { r = address_new_dynamic(&addr); if (r < 0) { - log_link_error(link, "Could not allocate address"); + log_error_link(link, "Could not allocate address"); link_enter_failed(link); return; } @@ -407,7 +526,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { r = route_new_dynamic(&rt); if (r < 0) { - log_link_error(link, "Could not allocate route"); + log_error_link(link, "Could not allocate route"); link_enter_failed(link); return; } @@ -420,6 +539,39 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { addr = NULL; rt = NULL; + if (link->network->dhcp_dns) { + r = sd_dhcp_client_get_dns(client, &nameservers, &nameservers_size); + if (r >= 0) { + r = manager_update_resolv_conf(link->manager); + if (r < 0) + log_error("Failed to update resolv.conf"); + } + } + + if (link->network->dhcp_mtu) { + uint16_t mtu; + + r = sd_dhcp_client_get_mtu(client, &mtu); + if (r >= 0) { + r = link_set_mtu(link, mtu); + if (r < 0) + log_error_link(link, "Failed to set MTU " + "to %" PRIu16, mtu); + } + } + + if (link->network->dhcp_hostname) { + const char *hostname; + + r = sd_dhcp_client_get_hostname(client, &hostname); + if (r >= 0) { + r = set_hostname(link->manager->bus, hostname); + if (r < 0) + log_error("Failed to set transient hostname " + "to '%s'", hostname); + } + } + link_enter_set_addresses(link); } @@ -436,9 +588,13 @@ static int link_acquire_conf(Link *link) { 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_new(&link->dhcp); + if (r < 0) + return r; + + r = sd_dhcp_client_attach_event(link->dhcp, NULL, 0); + if (r < 0) + return r; r = sd_dhcp_client_set_index(link->dhcp, link->ifindex); if (r < 0) @@ -451,8 +607,16 @@ static int link_acquire_conf(Link *link) { r = sd_dhcp_client_set_callback(link->dhcp, dhcp_handler, link); if (r < 0) return r; + + if (link->network->dhcp_mtu) { + r = sd_dhcp_client_set_request_option(link->dhcp, 26); + if (r < 0) + return r; + } } + log_debug_link(link, "acquiring DHCPv4 lease"); + r = sd_dhcp_client_start(link->dhcp); if (r < 0) return r; @@ -470,31 +634,33 @@ static int link_update_flags(Link *link, unsigned flags) { return 0; if (link->flags == flags) { - log_debug_link(link, "link status unchanged: %#x", flags); + log_debug_link(link, "link status unchanged: %#.8x", flags); return 0; } if ((link->flags & IFF_UP) != (flags & IFF_UP)) log_info_link(link, - "power %s", flags & IFF_UP ? "on": "off"); + "link is %s", flags & IFF_UP ? "up": "down"); if ((link->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) { if (flags & IFF_LOWER_UP) { - log_link_info(link, "carrier on"); + log_info_link(link, "carrier on"); if (link->network->dhcp) { r = link_acquire_conf(link); if (r < 0) { + log_warning_link(link, "Could not acquire DHCPv4 lease: %s", strerror(-r)); link_enter_failed(link); return r; } } } else { - log_link_info(link, "carrier off"); + log_info_link(link, "carrier off"); if (link->network->dhcp) { r = sd_dhcp_client_stop(link->dhcp); if (r < 0) { + log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r)); link_enter_failed(link); return r; } @@ -503,7 +669,7 @@ static int link_update_flags(Link *link, unsigned flags) { } log_debug_link(link, - "link status updated: %#x -> %#x", link->flags, flags); + "link status updated: %#.8x -> %#.8x", link->flags, flags); link->flags = flags; @@ -539,11 +705,11 @@ static int link_up(Link *link) { assert(link->manager); assert(link->manager->rtnl); - log_link_debug(link, "bringing link up"); + log_debug_link(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"); + log_error_link(link, "Could not allocate RTM_SETLINK message"); return r; } @@ -609,6 +775,7 @@ static int bridge_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { link->network->bridge->name, BRIDGE(link->network->bridge), NULL); + link_bridge_joined(link); return 1; @@ -631,7 +798,7 @@ static int link_enter_join_bridge(Link *link) { link->network->bridge->name, BRIDGE(link->network->bridge), NULL); - log_link_debug(link, "joining bridge"); + log_debug_link(link, "joining bridge"); r = bridge_join(link->network->bridge, link, &bridge_handler); if (r < 0) { @@ -662,7 +829,7 @@ static int link_get_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { link_enter_failed(link); } - log_link_debug(link, "got link state"); + log_debug_link(link, "got link state"); link_update(link, m); @@ -677,11 +844,11 @@ static int link_get(Link *link) { assert(link->manager); assert(link->manager->rtnl); - log_link_debug(link, "requesting link status"); + log_debug_link(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"); + log_error_link(link, "Could not allocate RTM_GETLINK message"); return r; } @@ -713,6 +880,8 @@ int link_configure(Link *link) { int link_update(Link *link, sd_rtnl_message *m) { unsigned flags; + void *data; + uint16_t type; int r; assert(link); @@ -723,9 +892,18 @@ int link_update(Link *link, sd_rtnl_message *m) { r = sd_rtnl_message_link_get_flags(m, &flags); if (r < 0) { - log_link_warning(link, "could not get link flags"); + log_warning_link(link, "Could not get link flags"); return r; } + while (sd_rtnl_message_read(m, &type, &data) > 0) { + if (type == IFLA_MTU && link->network->dhcp && + link->network->dhcp_mtu && !link->original_mtu) { + link->original_mtu = *(uint16_t *) data; + log_debug_link(link, "saved original MTU: %" PRIu16, + link->original_mtu); + } + } + return link_update_flags(link, flags); }