networkd-link.c revision 428fd0a75873d63b9d567f9db7871551d1ee0d62
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "networkd.h"
#include "libudev-private.h"
#include "udev-util.h"
#include "util.h"
#include "virt.h"
#include "bus-util.h"
#include "network-internal.h"
#include "network-util.h"
#include "dhcp-lease-internal.h"
char *ifname;
int r, ifindex;
if (r < 0)
return r;
else if (type != RTM_NEWLINK)
return -EINVAL;
if (r < 0)
return r;
else if (ifindex <= 0)
return -EINVAL;
if (r < 0)
return r;
if (!link)
return -ENOMEM;
return -ENOMEM;
if (r < 0)
return -ENOMEM;
if (r < 0)
return -ENOMEM;
if (r < 0)
return r;
return 0;
}
if (!link)
return;
}
}
return NULL;
}
if (link)
return link;
}
assert(m);
if (!link)
return -ENODEV;
return 0;
}
return;
return;
}
return 0;
}
}
int r = 0, k;
return 0;
if (k < 0) {
r = k;
}
}
if (k < 0) {
r = k;
}
}
return r;
}
return;
}
int r;
link->route_messages --;
return 1;
}
r = sd_rtnl_message_get_errno(m);
if (r < 0 && r != -EEXIST)
"MESSAGE=%*s: could not set route: %s",
"ERRNO=%d", -r,
NULL);
/* we might have received an old reply after moving back to SETTING_ADDRESSES,
* ignore it */
}
return 1;
}
int r;
return link_enter_configured(link);
if (r < 0) {
"could not set routes: %s", strerror(-r));
return r;
}
link->route_messages ++;
}
if (r < 0 && r != -ENOENT) {
strerror(-r));
return r;
}
if (r != -ENOENT) {
r = route_new_dynamic(&route);
if (r < 0) {
strerror(-r));
return r;
}
if (r < 0) {
"could not set routes: %s", strerror(-r));
return r;
}
link->route_messages ++;
}
}
if (link->dhcp_lease) {
if (r < 0 && r != -ENOENT) {
return r;
}
if (r >= 0) {
r = route_new_dynamic(&route);
if (r < 0) {
strerror(-r));
return r;
}
r = route_new_dynamic(&route_gw);
if (r < 0) {
strerror(-r));
return r;
}
/* The dhcp netmask may mask out the gateway. Add an explicit
* route for the gw host so that we can route no matter the
* netmask or existing kernel route tables. */
if (r < 0) {
"could not set host route: %s", strerror(-r));
return r;
}
link->route_messages ++;
if (r < 0) {
"could not set routes: %s", strerror(-r));
return r;
}
link->route_messages ++;
}
}
if (link->route_messages == 0) {
}
return 0;
}
int r;
assert(m);
return 1;
}
r = sd_rtnl_message_get_errno(m);
if (r < 0 && r != -ESRCH)
"MESSAGE=%*s: could not drop route: %s",
"ERRNO=%d", -r,
NULL);
return 0;
}
int r;
assert(m);
link->addr_messages --;
return 1;
}
r = sd_rtnl_message_get_errno(m);
if (r < 0 && r != -EEXIST)
"MESSAGE=%*s: could not set address: %s",
"ERRNO=%d", -r,
NULL);
if (link->addr_messages == 0) {
}
return 1;
}
int r;
return link_enter_set_routes(link);
if (r < 0) {
"could not set addresses: %s", strerror(-r));
return r;
}
link->addr_messages ++;
}
if (r < 0 && r != -ENOENT) {
strerror(-r));
return r;
}
if (r != -ENOENT) {
r = address_new_dynamic(&ll_addr);
if (r < 0) {
return r;
}
if (r < 0) {
"could not set addresses: %s", strerror(-r));
return r;
}
link->addr_messages ++;
}
}
if (link->dhcp_lease) {
unsigned prefixlen;
if (r < 0) {
strerror(-r));
return r;
}
if (r < 0) {
strerror(-r));
return r;
}
r = address_new_dynamic(&address);
if (r < 0) {
strerror(-r));
return r;
}
if (r < 0) {
"could not set addresses: %s", strerror(-r));
return r;
}
link->addr_messages ++;
}
return 0;
}
int r;
assert(m);
return 1;
}
r = sd_rtnl_message_get_errno(m);
if (r < 0 && r != -ENOENT)
"MESSAGE=%*s: could not update address: %s",
"ERRNO=%d", -r,
NULL);
return 0;
}
int r;
assert(m);
return 1;
}
r = sd_rtnl_message_get_errno(m);
if (r < 0 && r != -EADDRNOTAVAIL)
"MESSAGE=%*s: could not drop address: %s",
"ERRNO=%d", -r,
NULL);
return 0;
}
static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
int r;
return 1;
}
r = sd_bus_message_get_errno(m);
if (r < 0)
return 1;
}
int r = 0;
return 0;
}
&m,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"SetHostname");
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
}
int r;
assert(m);
return 1;
}
r = sd_rtnl_message_get_errno(m);
if (r < 0)
"MESSAGE=%s: could not set MTU: %s",
"ERRNO=%d", -r,
NULL);
return 1;
}
int r;
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
if (r < 0) {
"Could not send rtnetlink message: %s", strerror(-r));
return r;
}
return 0;
}
unsigned prefixlen;
int r;
r = address_new_dynamic(&address);
if (r >= 0) {
if (r >= 0) {
r = route_new_dynamic(&route_gw);
if (r >= 0) {
}
r = route_new_dynamic(&route);
if (r >= 0) {
}
}
}
if (r < 0) {
return r;
}
}
}
if (r >= 0 && hostname) {
if (r < 0)
}
}
return 0;
}
unsigned prefixlen;
struct in_addr *nameservers;
int r;
if (r < 0) {
strerror(-r));
return r;
}
if (r < 0) {
strerror(-r));
return r;
}
if (r < 0) {
strerror(-r));
return r;
}
if (r < 0 && r != -ENOENT) {
return r;
}
if (r >= 0)
"MESSAGE=%*s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
"ADDRESS=%u.%u.%u.%u",
"PREFIXLEN=%u",
"GATEWAY=%u.%u.%u.%u",
NULL);
else
"MESSAGE=%*s: DHCPv4 address %u.%u.%u.%u/%u",
"ADDRESS=%u.%u.%u.%u",
"PREFIXLEN=%u",
NULL);
if (r >= 0) {
if (r < 0)
}
}
if (r >= 0) {
if (r < 0)
}
}
const char *hostname;
if (r >= 0) {
if (r < 0)
"to '%s'", hostname);
}
}
return 0;
}
int r = 0;
return;
switch (event) {
case DHCP_EVENT_NO_LEASE:
break;
case DHCP_EVENT_EXPIRED:
case DHCP_EVENT_STOP:
case DHCP_EVENT_IP_CHANGE:
"ignoring request to reconfigure it.");
return;
}
if (link->dhcp_lease) {
r = dhcp_lease_lost(link);
if (r < 0) {
return;
}
}
if (event == DHCP_EVENT_IP_CHANGE) {
if (r < 0) {
return;
}
}
r = ipv4ll_address_update(link, false);
if (r < 0) {
return;
}
}
break;
case DHCP_EVENT_IP_ACQUIRE:
if (r < 0) {
return;
}
r = ipv4ll_address_update(link, true);
else
if (r < 0) {
return;
}
}
break;
default:
if (event < 0)
else
break;
}
return;
}
int r;
if (r >= 0) {
r = address_new_dynamic(&address);
if (r < 0) {
return r;
}
}
return 0;
}
int r;
if (r >= 0) {
r = address_new_dynamic(&address);
if (r < 0) {
return r;
}
r = route_new_dynamic(&route);
if (r < 0) {
strerror(-r));
return r;
}
}
return 0;
}
int r;
if (r < 0)
return false;
return true;
}
int r;
if (r < 0)
return r;
"MESSAGE=%*s: IPv4 link-local address %u.%u.%u.%u",
NULL);
return 0;
}
int r;
return;
switch(event) {
case IPV4LL_EVENT_STOP:
case IPV4LL_EVENT_CONFLICT:
r = ipv4ll_address_lost(link);
if (r < 0) {
return;
}
break;
case IPV4LL_EVENT_BIND:
if (r < 0) {
return;
}
break;
default:
if (event < 0)
else
break;
}
}
int r;
if (r < 0) {
"link-local address");
return r;
}
}
if (r < 0) {
"lease");
return r;
}
}
return 0;
}
/* see Documentation/networking/operstates.txt in the kernel sources */
if (operstate == IF_OPER_UP)
return true;
if (operstate == IF_OPER_UNKNOWN)
/* operstate may not be implemented, so fall back to flags */
return true;
return false;
}
: "")
bool carrier_gained = false, carrier_lost = false;
int r;
r = sd_rtnl_message_link_get_flags(m, &flags);
if (r < 0) {
return r;
}
if (r < 0)
/* if we got a message without operstate, take it to mean
the state was unchanged */
return 0;
/* link flags are currently at most 18 bits, let's align to printing 20 */
if (unknown_flags_added)
}
return 0;
if (carrier_gained) {
r = link_acquire_conf(link);
if (r < 0) {
return r;
}
}
} else if (carrier_lost) {
r = link_stop_clients(link);
if (r < 0) {
return r;
}
}
return 0;
}
int r;
return 1;
}
r = sd_rtnl_message_get_errno(m);
if (r < 0) {
/* we warn but don't fail the link, as it may
be brought up later */
"MESSAGE=%*s: could not bring up interface: %s",
"ERRNO=%d", -r,
NULL);
}
return 1;
}
int r;
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
if (r < 0) {
"Could not send rtnetlink message: %s", strerror(-r));
return r;
}
return 0;
}
int r;
if (r < 0) {
return r;
}
}
return link_enter_set_addresses(link);
return 0;
}
int r;
return 1;
}
r = sd_rtnl_message_get_errno(m);
if (r < 0) {
"MESSAGE=%*s: could not enslave: %s",
"ERRNO=%d", -r,
NULL);
return 1;
}
return 1;
}
Iterator i;
int r;
return link_enslaved(link);
"MESSAGE=%*s: enslaving by '%s'",
NULL);
if (r < 0) {
"MESSAGE=%*s: could not enslave by '%s': %s",
NULL);
return r;
}
}
"MESSAGE=%*s: enslaving by '%s'",
NULL);
if (r < 0) {
"MESSAGE=%*s: could not enslave by '%s': %s",
NULL);
return r;
}
}
"MESSAGE=%*s: enslaving by '%s'",
NULL);
if (r < 0) {
"MESSAGE=%*s: could not enslave by '%s': %s",
NULL);
return r;
}
}
"MESSAGE=%*s: enslaving by '%s'",
if (r < 0) {
"MESSAGE=%*s: could not enslave by '%s': %s",
return r;
}
}
"MESSAGE=%*s: enslaving by '%s'",
if (r < 0) {
"MESSAGE=%*s: could not enslave by '%s': %s",
return r;
}
}
return 0;
}
int r;
if (r < 0)
return r;
if (link->udev_device) {
if (r >= 0) {
if (r < 0)
return r;
}
}
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
}
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
}
}
r = link_acquire_conf(link);
if (r < 0)
return r;
}
return link_enter_enslave(link);
}
int r;
return 0;
if (device)
if (r == -ENOENT) {
return 0;
} else if (r < 0)
return r;
if (r < 0)
return r;
r = link_configure(link);
if (r < 0)
return r;
return 0;
}
char buf[INET6_ADDRSTRLEN];
bool address_dropped = false;
int r, ifindex;
assert(m);
if (r < 0) {
log_warning("rtnl: could not get message type");
return 0;
}
if (r < 0 || ifindex <= 0) {
log_warning("rtnl: received address message without valid ifindex, ignoring");
return 0;
} else {
if (r < 0 || !link) {
log_warning("rtnl: received address for a nonexistent link, ignoring");
return 0;
}
}
r = address_new_dynamic(&address);
if (r < 0)
return 0;
return 0;
}
if (r < 0) {
return 0;
}
case AF_INET:
if (r < 0) {
return 0;
}
break;
case AF_INET6:
if (r < 0) {
return 0;
}
break;
default:
assert_not_reached("invalid address family");
}
return 0;
}
address_dropped = true;
break;
}
}
switch (type) {
case RTM_NEWADDR:
if (!address_dropped)
break;
case RTM_DELADDR:
if (address_dropped)
break;
default:
assert_not_reached("Received invalid RTNL message type");
}
return 1;
}
int r;
assert(m);
for (; m; m = sd_rtnl_message_next(m)) {
r = sd_rtnl_message_get_errno(m);
if (r < 0) {
continue;
}
if (r < 0)
}
return 1;
}
int r;
assert(m);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (detect_container(NULL) <= 0) {
/* not in a container, udev will be around */
if (!device) {
return -errno;
}
if (udev_device_get_is_initialized(device) <= 0) {
/* not yet ready */
return 0;
}
}
if (r < 0)
return r;
return 0;
}
struct ether_addr mac;
char *ifname;
int r;
assert(m);
}
return -ENOMEM;
}
if (!link->original_mtu) {
if (r >= 0)
}
/* The kernel may broadcast NEWLINK messages without the MAC address
set, simply ignore them. */
if (r >= 0) {
"%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
mac.ether_addr_octet[0],
if (r < 0) {
"address in IPv4LL client: %s",
strerror(-r));
return r;
}
}
if (link->dhcp_client) {
if (r < 0) {
"address in DHCP client: %s",
strerror(-r));
return r;
}
}
}
}
return link_update_flags(link, m);
}
int r;
if (r < 0)
return r;
return 0;
}
oper_state = "dormant";
oper_state = "carrier";
if (r < 0)
goto finish;
fprintf(f,
"# This is private data. Do not parse.\n"
"ADMIN_STATE=%s\n"
"OPER_STATE=%s\n"
"FLAGS=%u\n",
if (link->dhcp_lease) {
if (r < 0)
goto finish;
} else
fflush(f);
r = -errno;
}
if (r < 0)
return r;
}
static const char* const link_state_table[_LINK_STATE_MAX] = {
[LINK_STATE_INITIALIZING] = "initializing",
[LINK_STATE_ENSLAVING] = "configuring",
[LINK_STATE_SETTING_ADDRESSES] = "configuring",
[LINK_STATE_SETTING_ROUTES] = "configuring",
[LINK_STATE_CONFIGURED] = "configured",
[LINK_STATE_UNMANAGED] = "unmanaged",
[LINK_STATE_FAILED] = "failed",
[LINK_STATE_LINGER] = "linger",
};