networkctl.c revision d9000fd3b3ad0728832007f56e9cafacfbbe54e7
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering This file is part of systemd.
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering Copyright 2014 Lennart Poettering
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering systemd is free software; you can redistribute it and/or modify it
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering under the terms of the GNU Lesser General Public License as published by
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering (at your option) any later version.
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering systemd is distributed in the hope that it will be useful, but
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering Lesser General Public License for more details.
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering You should have received a copy of the GNU Lesser General Public License
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic bool arg_no_pager = false;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic bool arg_legend = true;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic bool arg_all = false;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic void pager_open_if_enabled(void) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic int link_get_type_string(int iftype, struct udev_device *d, char **ret) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering const char *t;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering /* WLANs have iftype ARPHRD_ETHER, but we want
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering * to show a more useful type string for
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poetteringtypedef struct LinkInfo {
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poetteringstatic int link_info_compare(const void *a, const void *b) {
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering const LinkInfo *x = a, *y = b;
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poetteringstatic int decode_and_sort_links(sd_rtnl_message *m, LinkInfo **ret) {
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering for (i = m; i; i = sd_rtnl_message_next(i)) {
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_message_link_get_ifindex(i, &ifindex);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_message_read_string(i, IFLA_IFNAME, &name);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_message_link_get_type(i, &iftype);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering qsort_safe(links, c, sizeof(LinkInfo), link_info_compare);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering return (int) c;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic void operational_state_to_color(const char *state, const char **on, const char **off) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering } else if (streq_ptr(state, "degraded")) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic void setup_state_to_color(const char *state, const char **on, const char **off) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering } else if (streq_ptr(state, "configuring")) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering } else if (streq_ptr(state, "failed") || streq_ptr(state, "linger")) {
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poetteringstatic int list_links(int argc, char *argv[], void *userdata) {
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering _cleanup_udev_unref_ struct udev *udev = NULL;
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering return log_error_errno(r, "Failed to connect to netlink: %m");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering return log_error_errno(errno, "Failed to connect to udev: %m");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
df3fb561b2df486a495a5f0bcc83168bd1860533Lennart Poettering r = sd_rtnl_message_request_dump(req, true);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering return log_error_errno(r, "Failed to enumerate links: %m");
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering printf("%3s %-16s %-18s %-11s %-10s\n", "IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering c = decode_and_sort_links(reply, &links);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering for (i = 0; i < c; i++) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering const char *on_color_operational, *off_color_operational,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering sd_network_link_get_operational_state(links[i].ifindex, &operational_state);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering sd_network_link_get_setup_state(links[i].ifindex, &setup_state);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering d = udev_device_new_from_device_id(udev, devid);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering link_get_type_string(links[i].iftype, d, &t);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering printf("%3i %-16s %-18s %s%-11s%s %s%-10s%s\n",
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering links[i].ifindex, links[i].name, strna(t),
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering on_color_operational, strna(operational_state), off_color_operational,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering on_color_setup, strna(setup_state), off_color_setup);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering/* IEEE Organizationally Unique Identifier vendor string */
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic int ieee_oui(sd_hwdb *hwdb, struct ether_addr *mac, char **ret) {
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering char modalias[strlen("OUI:XXYYXXYYXXYY") + 1], *desc;
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering /* skip commonly misused 00:00:00 (Xerox) prefix */
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering snprintf(modalias, sizeof(modalias), "OUI:" ETHER_ADDR_FORMAT_STR, ETHER_ADDR_FORMAT_VAL(*mac));
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering assert(family == AF_INET || family == AF_INET6);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = sd_rtnl_message_request_dump(req, true);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering for (m = reply; m; m = sd_rtnl_message_next(m)) {
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering log_error_errno(r, "could not get type: %m");
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = sd_rtnl_message_neigh_get_family(m, &fam);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering log_error_errno(r, "could not get family: %m");
df3fb561b2df486a495a5f0bcc83168bd1860533Lennart Poettering r = sd_rtnl_message_neigh_get_ifindex(m, &ifi);
df3fb561b2df486a495a5f0bcc83168bd1860533Lennart Poettering log_error_errno(r, "could not get ifindex: %m");
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = sd_rtnl_message_read_in_addr(m, NDA_DST, &gw.in);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = sd_rtnl_message_read_in6_addr(m, NDA_DST, &gw.in6);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = sd_rtnl_message_read_ether_addr(m, NDA_LLADDR, &mac);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = ieee_oui(hwdb, &mac, gateway_description);
f7d68aa8c9430dfddee5f913f93b405a5d75723aLennart Poettering _cleanup_free_ struct local_address *local = NULL;
f7d68aa8c9430dfddee5f913f93b405a5d75723aLennart Poettering n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local);
f7d68aa8c9430dfddee5f913f93b405a5d75723aLennart Poettering for (i = 0; i < n; i++) {
f7d68aa8c9430dfddee5f913f93b405a5d75723aLennart Poettering _cleanup_free_ char *gateway = NULL, *description = NULL;
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = in_addr_to_string(local[i].family, &local[i].address, &gateway);
f7d68aa8c9430dfddee5f913f93b405a5d75723aLennart Poettering r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description);
03cc0fd1431b82e59c11ae12a274c1f2df23169dLennart Poettering log_debug_errno(r, "Could not get description of gateway: %m");
f7d68aa8c9430dfddee5f913f93b405a5d75723aLennart Poettering /* Show interface name for the entry if we show
f7d68aa8c9430dfddee5f913f93b405a5d75723aLennart Poettering * entries for all interfaces */
f7d68aa8c9430dfddee5f913f93b405a5d75723aLennart Poettering if (if_indextoname(local[i].ifindex, name)) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_free_ struct local_address *local = NULL;
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering for (i = 0; i < n; i++) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering if (if_indextoname(local[i].ifindex, name)) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringstatic void dump_list(const char *prefix, char **l) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering const char *name) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **domains = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_free_ char *t = NULL, *network = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering const char *on_color_operational, *off_color_operational,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering if (safe_atoi(name, &ifindex) >= 0 && ifindex > 0)
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, ifindex);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = sd_rtnl_message_append_string(req, IFLA_IFNAME, name);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering return log_error_errno(r, "Failed to query link: %m");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = sd_rtnl_message_link_get_ifindex(reply, &ifindex);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = sd_rtnl_message_read_string(reply, IFLA_IFNAME, &name);
9085f64a6694f2928c79fcce365edb1dca6937d4Lennart Poettering r = sd_rtnl_message_link_get_type(reply, &iftype);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering have_mac = sd_rtnl_message_read_ether_addr(reply, IFLA_ADDRESS, &e) >= 0;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering for (p = (uint8_t*) &e; p < (uint8_t*) &e + sizeof(e); p++)
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering if (*p != 0) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering sd_rtnl_message_read_u32(reply, IFLA_MTU, &mtu);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering sd_network_link_get_operational_state(ifindex, &operational_state);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering sd_network_link_get_setup_state(ifindex, &setup_state);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering sd_network_link_get_domains(ifindex, &domains);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = sd_network_link_get_wildcard_domain(ifindex);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering if (strv_consume(&domains, wildcard) < 0)
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering d = udev_device_new_from_device_id(udev, devid);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering link = udev_device_get_property_value(d, "ID_NET_LINK_FILE");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering driver = udev_device_get_property_value(d, "ID_NET_DRIVER");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering path = udev_device_get_property_value(d, "ID_PATH");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering vendor = udev_device_get_property_value(d, "ID_VENDOR_FROM_DATABASE");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering vendor = udev_device_get_property_value(d, "ID_VENDOR");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering model = udev_device_get_property_value(d, "ID_MODEL_FROM_DATABASE");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering model = udev_device_get_property_value(d, "ID_MODEL");
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering sd_network_link_get_network_file(ifindex, &network);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering printf("%s%s%s %i: %s\n", on_color_operational, draw_special_char(DRAW_BLACK_CIRCLE), off_color_operational, ifindex, name);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering "Network File: %s\n"
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering " State: %s%s%s (%s%s%s)\n",
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering on_color_operational, strna(operational_state), off_color_operational,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering on_color_setup, strna(setup_state), off_color_setup);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering printf(" HW Address: %s (%s)\n", ether_addr_to_string(&e, ea), description);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering printf(" HW Address: %s\n", ether_addr_to_string(&e, ea));
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering dump_addresses(rtnl, " Address: ", ifindex);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering dump_gateways(rtnl, hwdb, " Gateway: ", ifindex);
char **name;
if (!udev)
if (arg_all) {
return rtnl_log_create_error(r);
return rtnl_log_create_error(r);
return rtnl_log_parse_error(c);
char *capability;
return NULL;
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(t);
return NULL;
free(s);
capability = t;
s = NULL;
t = NULL;
return capability;
return rtnl_log_create_error(r);
return rtnl_log_create_error(r);
return rtnl_log_parse_error(c);
if (arg_legend)
return -ENOMEM;
STRV_FOREACH(s, l) {
return -ENOMEM;
if (!chassis)
return -ENOMEM;
if (!port)
return -ENOMEM;
r = safe_atollu(b, &x);
if (r < 0 || (usec_t) x != x)
if (x < time)
if (ttl >= 0) {
if (arg_legend) {
static void help(void) {
help();
case ARG_VERSION:
case ARG_NO_PAGER:
arg_no_pager = true;
case ARG_NO_LEGEND:
arg_legend = false;
arg_all = true;
return -EINVAL;
log_open();
goto finish;
pager_close();