networkd-link.c revision 628706137efbca8aaf753ccd063e5abf7e31aed5
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Tom Gundersen <teg@jklm.no>
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 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 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/>.
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersenint link_new(Manager *manager, struct udev_device *device, Link **ret) {
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen const char *mac;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering link->ifindex = udev_device_get_ifindex(device);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen mac = udev_device_get_sysattr_value(device, "address");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memcpy(&link->mac, mac_addr, sizeof(struct ether_addr));
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering ifname = udev_device_get_sysname(device);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = hashmap_put(manager->links, &link->ifindex, link);
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering hashmap_remove(link->manager->links, &link->ifindex);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_add(Manager *m, struct udev_device *device, Link **ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering devtype = udev_device_get_devtype(device);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (r < 0 && r != -ENOENT)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return r == -ENOENT ? 0 : r;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic int link_enter_configured(Link *link) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(link->state == LINK_STATE_SETTING_ROUTES);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic void link_enter_failed(Link *link) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringstatic int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(link->state == LINK_STATE_SETTING_ADDRESSES ||
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering link->state == LINK_STATE_SETTING_ROUTES ||
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (r < 0 && r != -EEXIST)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering log_warning_link(link, "could not set route: %s", strerror(-r));
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 Poetteringstatic int link_enter_set_routes(Link *link) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(link->state == LINK_STATE_SETTING_ADDRESSES);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (!link->network->static_routes && !link->dhcp_route)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_FOREACH(static_routes, route, link->network->static_routes) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering r = route_configure(route, link, &route_handler);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen r = route_configure(link->dhcp_route, link, &route_handler);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poetteringstatic int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering assert(link->state == LINK_STATE_SETTING_ADDRESSES || link->state == LINK_STATE_FAILED);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (r < 0 && r != -EEXIST)
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt "MESSAGE=%s: could not set address: %s",
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering "ERRNO=%d", -r,
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poetteringstatic int link_enter_set_addresses(Link *link) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(link->state != _LINK_STATE_INVALID);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering link->state = LINK_STATE_SETTING_ADDRESSES;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (!link->network->static_addresses && !link->dhcp_address)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_link_debug(link, "setting addresses");
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering LIST_FOREACH(static_addresses, address, link->network->static_addresses) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering r = address_configure(address, link, &address_handler);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering "could not set addresses: %s", strerror(-r));
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering r = address_configure(link->dhcp_address, link, &address_handler);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering "could not set addresses: %s", strerror(-r));
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poetteringstatic int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (r < 0 && r != -EEXIST)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_warning_link(link, "could not drop address: %s", strerror(-r));
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poetteringstatic void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering log_warning_link(link, "DHCP error: %s", strerror(-event));
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_debug(link, "IP address in use.");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_EXPIRED ||
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering address_drop(link->dhcp_address, link, address_drop_handler);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = sd_dhcp_client_get_address(client, &address);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_warning(link, "DHCP error: no address");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = sd_dhcp_client_get_netmask(client, &netmask);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_warning(link, "DHCP error: no netmask");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering prefixlen = sd_dhcp_client_prefixlen(&netmask);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_warning(link, "DHCP error: no prefixlen");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering r = sd_dhcp_client_get_router(client, &gateway);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_link_warning(link, "DHCP error: no router");
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_IP_ACQUIRE) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering _cleanup_address_free_ Address *addr = NULL;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering "ADDRESS=%u.%u.%u.%u",
NULL);
return -ENOMEM;
r = sd_rtnl_message_get_errno(m);
r = sd_rtnl_message_get_errno(m);
NULL);
NULL);
NULL);
NULL);
r = sd_rtnl_message_get_errno(m);
unsigned flags;
assert(m);