networkd-link.c revision fecc80c1ba2eed9dadb9a10c15508c356bcc5fc1
/*-*- 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 <unistd.h>
#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 "conf-parser.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)
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;
}
}
int r = 0, k;
return 0;
if (k < 0) {
r = k;
}
}
if (k < 0) {
r = k;
}
}
if (k < 0) {
r = k;
}
}
if (link->dhcp6_client) {
if (k < 0) {
r = k;
}
}
if (k < 0) {
r = k;
}
}
return r;
}
return;
}
/* The the first statically configured address if there is any */
continue;
continue;
return address;
}
/* If that didn't work, find a suitable address we got from the pool */
continue;
return address;
}
return NULL;
}
int r;
struct in_addr pool_start;
if (!address) {
return 0;
}
if (r < 0)
return r;
/* offer 32 addresses starting from the address following the server address */
&pool_start, 32);
if (r < 0)
return r;
/* TODO:
r = sd_dhcp_server_set_router(link->dhcp_server,
&main_address->in_addr.in);
if (r < 0)
return r;
r = sd_dhcp_server_set_prefixlen(link->dhcp_server,
main_address->prefixlen);
if (r < 0)
return r;
*/
if (r < 0) {
"instance: %s", strerror(-r));
return 0;
}
}
return 0;
}
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;
}
struct sd_dhcp_route *static_routes;
int r;
unsigned i;
if (r < 0) {
if (r != -ENOENT)
return r;
}
for (i = 0; i < static_routes_size; i++) {
r = route_new_dynamic(&route);
if (r < 0) {
strerror(-r));
return r;
}
if (r < 0) {
"could not set host route: %s", strerror(-r));
return r;
}
link->route_messages ++;
}
return 0;
}
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) {
strerror(-r));
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;
}
&lifetime);
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;
}
/* use update rather than configure so that we will update the lifetime
of an existing address if it has already been configured */
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;
}
return 0;
}
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;
unsigned i;
int r;
struct sd_dhcp_route *routes;
if (r >= 0) {
for (i = 0; i < routes_size; i++) {
r = route_new_dynamic(&route);
if (r >= 0) {
}
}
}
}
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;
}
int r;
if (r < 0) {
strerror(-r));
return r;
}
return 0;
}
unsigned prefixlen;
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) {
strerror(-r));
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)
}
}
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_RENEW:
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;
}
}
return;
switch(event) {
case DHCP6_EVENT_STOP:
case DHCP6_EVENT_RETRANS_MAX:
case DHCP6_EVENT_IP_ACQUIRE:
break;
default:
if (event < 0)
else
event);
return;
}
}
int r;
return;
switch(event) {
return;
break;
default:
if (event < 0)
else
event);
return;
}
if (link->dhcp6_client)
return;
if (r < 0)
return;
if (r < 0) {
return;
}
if (r < 0) {
return;
}
if (r < 0) {
return;
}
link);
if (r < 0) {
return;
}
if (r < 0)
}
int r;
if (r < 0) {
"link-local address");
return r;
}
}
if (r < 0) {
"lease");
return r;
}
}
if (r < 0) {
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);
}
int r;
return 1;
r = sd_rtnl_message_get_errno(m);
if (r < 0 && r != -EEXIST) {
"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;
}
}
"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;
}
if (r < 0)
return r;
if (r < 0)
return r;
}
if (!hostname)
return -ENOMEM;
if (!is_localhost(hostname)) {
if (r < 0)
return r;
}
}
}
if (r < 0)
return r;
if (r < 0)
return r;
}
if (r < 0)
return r;
NULL, 0);
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 1;
if (r == -ENOENT) {
return 1;
} else if (r < 0)
return r;
if (r < 0)
return r;
r = link_configure(link);
if (r < 0)
return r;
return 1;
}
int r;
return 0;
/* udev has initialized the link, but we don't know if we have yet processed
the NEWLINK messages with the latest state. Do a GETLINK, when it returns
we know that the pending NEWLINKs have already been processed and that we
are up-to-date */
if (r < 0)
return r;
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;
}
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;
} else {
/* we are calling a callback directly, so must take a ref */
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;
}
}
if (link->dhcp6_client) {
if (r < 0) {
strerror(-r));
return r;
}
}
}
}
return link_update_flags(link, m);
}
assert(f);
if (!address)
return;
char buf[INET6_ADDRSTRLEN];
}
fputs("\n", f);
}
/* if we have carrier, check what addresses we have */
}
if (scope < RT_SCOPE_SITE)
/* universally accessible addresses found */
else if (scope < RT_SCOPE_HOST)
/* only link or site local addresses found */
else
/* no useful addresses found */
} else
}
const char *admin_state, *oper_state;
int r;
if (r < 0)
return r;
return 0;
}
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;
fprintf(f,
"DHCP_LEASE=%s\n"
"DHCP_USE_DNS=%s\n"
"DHCP_USE_NTP=%s\n",
} 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",
};
static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
[LINK_OPERSTATE_UNKNOWN] = "unknown",
[LINK_OPERSTATE_DORMANT] = "dormant",
[LINK_OPERSTATE_CARRIER] = "carrier",
[LINK_OPERSTATE_DEGRADED] = "degraded",
[LINK_OPERSTATE_ROUTABLE] = "routable",
};
static const char* const dhcp_support_table[_DHCP_SUPPORT_MAX] = {
[DHCP_SUPPORT_NONE] = "none",
[DHCP_SUPPORT_BOTH] = "both",
[DHCP_SUPPORT_V4] = "v4",
[DHCP_SUPPORT_V6] = "v6",
};
int config_parse_dhcp(
const char* unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int k;
/* Our enum shall be a superset of booleans, hence first try
* to parse as boolean, and then as enum */
k = parse_boolean(rvalue);
if (k > 0)
else if (k == 0)
else {
DHCPSupport s;
if (s < 0){
return 0;
}
*dhcp = s;
}
return 0;
}