networkd-link.c revision 57bd6899b35b84d1f23809133ae45d29a87b2eaa
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek This file is part of systemd.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Copyright 2013 Tom Gundersen <teg@jklm.no>
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (at your option) any later version.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
2cf4172a71860c6e44edd27a3b68047ae062d7fcLennart Poetteringstatic int ipv4ll_address_update(Link *link, bool deprecate);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekstatic bool ipv4ll_is_bound(sd_ipv4ll *ll);
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmekstatic int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek _cleanup_link_free_ Link *link = NULL;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_type(message, &type);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &ifname);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek link->state = LINK_STATE_INITIALIZING;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = asprintf(&link->state_file, "/run/systemd/network/links/%"PRIu64,
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = hashmap_put(manager->links, &link->ifindex, link);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek sd_dhcp_client_unref(link->dhcp_client);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek sd_dhcp_lease_unref(link->dhcp_lease);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek hashmap_remove(link->manager->links, &link->ifindex);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek udev_device_unref(link->udev_device);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmekint link_get(Manager *m, int ifindex, Link **ret) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek link = hashmap_get(m->links, &ifindex_64);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmekstatic int link_enter_configured(Link *link) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert(link->state == LINK_STATE_SETTING_ROUTES);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek log_info_link(link, "link configured");
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek link->state = LINK_STATE_CONFIGURED;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidtstatic void link_enter_unmanaged(Link *link) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek log_info_link(link, "unmanaged");
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek link->state = LINK_STATE_UNMANAGED;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmekstatic void link_enter_failed(Link *link) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek log_warning_link(link, "failed");
36d4739a68c3edafe4d145d525a26de4ef0b8e5aZbigniew Jędrzejewski-Szmek link->state = LINK_STATE_FAILED;
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidtstatic int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert(link->route_messages > 0);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert(link->state == LINK_STATE_SETTING_ADDRESSES ||
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek link->state == LINK_STATE_SETTING_ROUTES ||
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek link->state == LINK_STATE_FAILED);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (r < 0 && r != -EEXIST)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not set route: %s",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek /* we might have received an old reply after moving back to SETTING_ADDRESSES,
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (link->route_messages == 0 && link->state == LINK_STATE_SETTING_ROUTES) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_debug_link(link, "routes set");
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekstatic int link_enter_set_routes(Link *link) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(link->state == LINK_STATE_SETTING_ADDRESSES);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek link->state = LINK_STATE_SETTING_ROUTES;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (!link->network->static_routes && !link->dhcp_lease &&
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false))
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return link_enter_configured(link);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_debug_link(link, "setting routes");
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek LIST_FOREACH(static_routes, rt, link->network->static_routes) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = route_configure(rt, link, &route_handler);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek "could not set routes: %s", strerror(-r));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (link->ipv4ll && !link->dhcp_lease) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route = NULL;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_warning_link(link, "IPV4LL error: no address: %s",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate route: %s",
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek r = route_configure(route, link, &route_handler);
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek "could not set routes: %s", strerror(-r));
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route = NULL;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route_gw = NULL;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no router: %s",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate route: %s",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = route_new_dynamic(&route_gw);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate route: %s",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek /* The dhcp netmask may mask out the gateway. Add an explicit
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * route for the gw host so that we can route no matter the
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * netmask or existing kernel route tables. */
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek route_gw->dst_addr.in = gateway;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek route_gw->scope = RT_SCOPE_LINK;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = route_configure(route_gw, link, &route_handler);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek "could not set host route: %s", strerror(-r));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = route_configure(route, link, &route_handler);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek "could not set routes: %s", strerror(-r));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekstatic int route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not drop route: %s",
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmekstatic int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(link->addr_messages > 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(link->state == LINK_STATE_SETTING_ADDRESSES || link->state == LINK_STATE_FAILED);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (r < 0 && r != -EEXIST)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt "MESSAGE=%s: could not set address: %s",
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_debug_link(link, "addresses set");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmekstatic int link_enter_set_addresses(Link *link) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(link->state != _LINK_STATE_INVALID);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek link->state = LINK_STATE_SETTING_ADDRESSES;
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek if (!link->network->static_addresses && !link->dhcp_lease &&
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false))
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek return link_enter_set_routes(link);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek log_debug_link(link, "setting addresses");
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek LIST_FOREACH(static_addresses, ad, link->network->static_addresses) {
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek r = address_configure(ad, link, &address_handler);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek "could not set addresses: %s", strerror(-r));
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek if (link->ipv4ll && !link->dhcp_lease) {
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek _cleanup_address_free_ Address *ll_addr = NULL;
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT) {
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek log_warning_link(link, "IPV4LL error: no address: %s",
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek r = address_new_dynamic(&ll_addr);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate address: %s", strerror(-r));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ll_addr->broadcast.s_addr = ll_addr->in_addr.in.s_addr | htonl(0xfffffffflu >> ll_addr->prefixlen);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = address_configure(ll_addr, link, &address_handler);
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek "could not set addresses: %s", strerror(-r));
5bc891206dd8eb4e4df58f502b0184b8426caf22Zbigniew Jędrzejewski-Szmek _cleanup_address_free_ Address *address = NULL;
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no address: %s",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt log_warning_link(link, "DHCP error: no netmask: %s",
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt prefixlen = net_netmask_to_prefixlen(&netmask);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek r = address_new_dynamic(&address);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate address: %s",
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek address->broadcast.s_addr = addr.s_addr | ~netmask.s_addr;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = address_configure(address, link, &address_handler);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek "could not set addresses: %s", strerror(-r));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmekstatic int address_update_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
30776485c5bc2d9c356e875f2aee874d22c393b7Zbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
30776485c5bc2d9c356e875f2aee874d22c393b7Zbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
30776485c5bc2d9c356e875f2aee874d22c393b7Zbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT)
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not update address: %s",
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmekstatic int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT)
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not drop address: %s",
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmekstatic int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek r = sd_bus_message_get_errno(m);
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek log_warning("Could not set hostname: %s", strerror(-r));
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmekstatic int set_hostname(sd_bus *bus, const char *hostname) {
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
dad29dff1925a114e20d4eb7b47fca23c4f25fd7Lennart Poettering log_debug("Setting transient hostname: '%s'", hostname);
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek if (!bus) { /* TODO: replace by assert when we can rely on kdbus */
1af719edc5958c01c19204fb68d6fc45c9eea85cZbigniew Jędrzejewski-Szmek log_info("Not connected to system bus, ignoring transient hostname.");
1af719edc5958c01c19204fb68d6fc45c9eea85cZbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_call(
dad29dff1925a114e20d4eb7b47fca23c4f25fd7Lennart Poettering "SetHostname");
dad29dff1925a114e20d4eb7b47fca23c4f25fd7Lennart Poettering r = sd_bus_message_append(m, "sb", hostname, false);
dad29dff1925a114e20d4eb7b47fca23c4f25fd7Lennart Poettering r = sd_bus_call_async(bus, m, set_hostname_handler, NULL, 0, NULL);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Could not set transient hostname: %s", strerror(-r));
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmekstatic int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_rtnl_message_get_errno(m);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_struct_link(LOG_WARNING, link,
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek "MESSAGE=%s: could not set MTU: %s",
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmekstatic int link_set_mtu(Link *link, uint32_t mtu) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_debug_link(link, "setting MTU: %" PRIu32, mtu);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not allocate RTM_SETLINK message");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error_link(link, "Could not append MTU: %s", strerror(-r));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek "Could not send rtnetlink message: %s", strerror(-r));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekstatic int dhcp_lease_lost(Link *link) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek _cleanup_address_free_ Address *address = NULL;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route_gw = NULL;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek _cleanup_route_free_ Route *route = NULL;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP lease lost");
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek r = address_new_dynamic(&address);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek prefixlen = net_netmask_to_prefixlen(&netmask);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek r = route_new_dynamic(&route_gw);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek route_gw->dst_addr.in = gateway;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek route_gw->scope = RT_SCOPE_LINK;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek route_drop(route_gw, link, &route_drop_handler);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek route_drop(route, link, &route_drop_handler);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek address_drop(address, link, &address_drop_handler);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (r >= 0 && link->original_mtu != mtu) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = link_set_mtu(link, link->original_mtu);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: could not reset MTU");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (link->network->dhcp_hostname) {
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = set_hostname(link->manager->bus, "");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_error("Failed to reset transient hostname");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmekstatic int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
dad29dff1925a114e20d4eb7b47fca23c4f25fd7Lennart Poettering r = sd_dhcp_client_get_lease(client, &lease);
dad29dff1925a114e20d4eb7b47fca23c4f25fd7Lennart Poettering log_warning_link(link, "DHCP error: no lease: %s",
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_address(lease, &address);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no address: %s",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_netmask(lease, &netmask);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no netmask: %s",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek prefixlen = net_netmask_to_prefixlen(&netmask);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_router(lease, &gateway);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_warning_link(link, "DHCP error: no router: %s",
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "ADDRESS=%u.%u.%u.%u",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek "GATEWAY=%u.%u.%u.%u",
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = manager_update_resolv_conf(link->manager);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_error("Failed to update resolv.conf");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_dhcp_lease_get_mtu(lease, &mtu);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error_link(link, "Failed to set MTU "
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (link->network->dhcp_hostname) {
4015ac5c321f34784f73bd91ae9d7855ed663e5eLennart Poettering r = sd_dhcp_lease_get_hostname(lease, &hostname);
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek r = set_hostname(link->manager->bus, hostname);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Failed to set transient hostname "
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmekstatic void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (link->state == LINK_STATE_FAILED)
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_debug_link(link, "IP address in use.");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (link->network->dhcp_critical) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_error_link(link, "DHCPv4 connection considered system critical, "
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "ignoring request to reconfigure it.");
36d4739a68c3edafe4d145d525a26de4ef0b8e5aZbigniew Jędrzejewski-Szmek if (event == DHCP_EVENT_IP_CHANGE) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = dhcp_lease_acquired(client, link);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (event == DHCP_EVENT_EXPIRED && link->network->ipv4ll) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (!sd_ipv4ll_is_running(link->ipv4ll))
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_ipv4ll_start(link->ipv4ll);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek else if (ipv4ll_is_bound(link->ipv4ll))
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = ipv4ll_address_update(link, false);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = dhcp_lease_acquired(client, link);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (ipv4ll_is_bound(link->ipv4ll))
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = ipv4ll_address_update(link, true);
strerror(-r));
NULL);
switch(event) {
case IPV4LL_EVENT_STOP:
case IPV4LL_EVENT_CONFLICT:
case IPV4LL_EVENT_BIND:
if (event < 0)
see Documentation/networking/operstates.txt in the kernel sources
if (carrier_gained) {
} else if (carrier_lost) {
r = sd_rtnl_message_get_errno(m);
NULL);
r = sd_rtnl_message_get_errno(m);
NULL);
Iterator i;
NULL);
NULL);
NULL);
NULL);
unsigned flags;
if (device)
if (r == -ENOENT) {
assert(m);
if (!device) {
return -errno;
unsigned flags;
char *ifname;
assert(m);
return -ENOMEM;
strerror(-r));
strerror(-r));
const char *state;
goto finish;
fprintf(f,
return -ENOMEM;
goto finish;
fflush(f);
r = -errno;