1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/if_addrlabel.h>
12 #include "sd-device.h"
15 #include "sd-netlink.h"
16 #include "sd-network.h"
18 #include "alloc-util.h"
19 #include "arphrd-list.h"
20 #include "bus-common-errors.h"
21 #include "bus-error.h"
23 #include "device-util.h"
24 #include "ether-addr-util.h"
26 #include "format-table.h"
27 #include "format-util.h"
28 #include "hwdb-util.h"
29 #include "local-addresses.h"
30 #include "locale-util.h"
32 #include "main-func.h"
33 #include "netlink-util.h"
35 #include "parse-util.h"
36 #include "pretty-print.h"
38 #include "socket-util.h"
39 #include "sort-util.h"
40 #include "sparse-endian.h"
41 #include "stdio-util.h"
42 #include "string-table.h"
43 #include "string-util.h"
46 #include "terminal-util.h"
49 static PagerFlags arg_pager_flags
= 0;
50 static bool arg_legend
= true;
51 static bool arg_all
= false;
52 static bool arg_stats
= false;
54 static char *link_get_type_string(unsigned short iftype
, sd_device
*d
) {
55 const char *t
, *devtype
;
59 sd_device_get_devtype(d
, &devtype
) >= 0 &&
61 return strdup(devtype
);
63 t
= arphrd_to_name(iftype
);
75 static void operational_state_to_color(const char *state
, const char **on
, const char **off
) {
79 if (STRPTR_IN_SET(state
, "routable", "enslaved")) {
80 *on
= ansi_highlight_green();
82 } else if (streq_ptr(state
, "degraded")) {
83 *on
= ansi_highlight_yellow();
89 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
93 if (streq_ptr(state
, "configured")) {
94 *on
= ansi_highlight_green();
96 } else if (streq_ptr(state
, "configuring")) {
97 *on
= ansi_highlight_yellow();
99 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
100 *on
= ansi_highlight_red();
101 *off
= ansi_normal();
106 typedef struct LinkInfo
{
107 char name
[IFNAMSIZ
+1];
109 unsigned short iftype
;
110 struct ether_addr mac_address
;
118 struct rtnl_link_stats64 stats64
;
119 struct rtnl_link_stats stats
;
125 bool has_mac_address
:1;
126 bool has_tx_queues
:1;
127 bool has_rx_queues
:1;
133 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
134 return CMP(a
->ifindex
, b
->ifindex
);
137 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
, char **patterns
) {
145 r
= sd_netlink_message_get_type(m
, &type
);
149 if (type
!= RTM_NEWLINK
)
152 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
156 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
161 char str
[DECIMAL_STR_MAX(int)];
163 xsprintf(str
, "%i", ifindex
);
165 if (!strv_fnmatch(patterns
, str
, 0) && !strv_fnmatch(patterns
, name
, 0))
169 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
173 strscpy(info
->name
, sizeof info
->name
, name
);
174 info
->ifindex
= ifindex
;
176 info
->has_mac_address
=
177 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
178 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
180 (void) sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
);
181 (void) sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
);
182 (void) sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
);
184 info
->has_rx_queues
=
185 sd_netlink_message_read_u32(m
, IFLA_NUM_RX_QUEUES
, &info
->rx_queues
) >= 0 &&
188 info
->has_tx_queues
=
189 sd_netlink_message_read_u32(m
, IFLA_NUM_TX_QUEUES
, &info
->tx_queues
) >= 0 &&
192 if (sd_netlink_message_read(m
, IFLA_STATS64
, sizeof info
->stats64
, &info
->stats64
) >= 0)
193 info
->has_stats64
= true;
194 else if (sd_netlink_message_read(m
, IFLA_STATS
, sizeof info
->stats
, &info
->stats
) >= 0)
195 info
->has_stats
= true;
200 static int acquire_link_bitrates(sd_bus
*bus
, LinkInfo
*link
) {
201 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
202 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
203 _cleanup_free_
char *path
= NULL
, *ifindex_str
= NULL
;
206 if (asprintf(&ifindex_str
, "%i", link
->ifindex
) < 0)
209 r
= sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str
, &path
);
213 r
= sd_bus_call_method(
215 "org.freedesktop.network1",
217 "org.freedesktop.DBus.Properties",
222 "org.freedesktop.network1.Link",
225 if (sd_bus_error_has_name(&error
, BUS_ERROR_SPEED_METER_INACTIVE
))
227 return log_error_errno(r
, "%s", bus_error_message(&error
, r
));
230 r
= sd_bus_message_enter_container(reply
, 'v', "(dd)");
232 return bus_log_parse_error(r
);
234 r
= sd_bus_message_read(reply
, "(dd)", &link
->tx_bitrate
, &link
->rx_bitrate
);
236 return bus_log_parse_error(r
);
238 r
= sd_bus_message_exit_container(reply
);
240 return bus_log_parse_error(r
);
242 link
->has_bitrates
= link
->tx_bitrate
>= 0 && link
->rx_bitrate
>= 0;
247 static int acquire_link_info(sd_bus
*bus
, sd_netlink
*rtnl
, char **patterns
, LinkInfo
**ret
) {
248 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
249 _cleanup_free_ LinkInfo
*links
= NULL
;
250 size_t allocated
= 0, c
= 0, j
;
251 sd_netlink_message
*i
;
257 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
259 return rtnl_log_create_error(r
);
261 r
= sd_netlink_message_request_dump(req
, true);
263 return rtnl_log_create_error(r
);
265 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
267 return log_error_errno(r
, "Failed to enumerate links: %m");
269 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
270 if (!GREEDY_REALLOC(links
, allocated
, c
+1))
273 r
= decode_link(i
, links
+ c
, patterns
);
280 typesafe_qsort(links
, c
, link_info_compare
);
283 for (j
= 0; j
< c
; j
++)
284 (void) acquire_link_bitrates(bus
, links
+ j
);
286 *ret
= TAKE_PTR(links
);
291 static int list_links(int argc
, char *argv
[], void *userdata
) {
292 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
293 _cleanup_free_ LinkInfo
*links
= NULL
;
294 _cleanup_(table_unrefp
) Table
*table
= NULL
;
298 r
= sd_netlink_open(&rtnl
);
300 return log_error_errno(r
, "Failed to connect to netlink: %m");
302 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
306 (void) pager_open(arg_pager_flags
);
308 table
= table_new("IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");
312 table_set_header(table
, arg_legend
);
314 assert_se(cell
= table_get_cell(table
, 0, 0));
315 (void) table_set_minimum_width(table
, cell
, 3);
316 (void) table_set_weight(table
, cell
, 0);
317 (void) table_set_ellipsize_percent(table
, cell
, 100);
318 (void) table_set_align_percent(table
, cell
, 100);
320 assert_se(cell
= table_get_cell(table
, 0, 1));
321 (void) table_set_ellipsize_percent(table
, cell
, 100);
323 for (i
= 0; i
< c
; i
++) {
324 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
325 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
326 const char *on_color_operational
, *off_color_operational
,
327 *on_color_setup
, *off_color_setup
;
328 char devid
[2 + DECIMAL_STR_MAX(int)];
329 _cleanup_free_
char *t
= NULL
;
331 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
332 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
334 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
335 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
336 setup_state
= strdup("unmanaged");
337 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
339 xsprintf(devid
, "n%i", links
[i
].ifindex
);
340 (void) sd_device_new_from_device_id(&d
, devid
);
342 t
= link_get_type_string(links
[i
].iftype
, d
);
344 r
= table_add_cell(table
, NULL
, TABLE_INT
, &links
[i
].ifindex
);
348 r
= table_add_many(table
,
349 TABLE_STRING
, links
[i
].name
,
350 TABLE_STRING
, strna(t
));
354 r
= table_add_cell(table
, &cell
, TABLE_STRING
, strna(operational_state
));
358 (void) table_set_color(table
, cell
, on_color_operational
);
360 r
= table_add_cell(table
, &cell
, TABLE_STRING
, strna(setup_state
));
364 (void) table_set_color(table
, cell
, on_color_setup
);
367 r
= table_print(table
, NULL
);
369 return log_error_errno(r
, "Failed to print table: %m");
372 printf("\n%i links listed.\n", c
);
377 /* IEEE Organizationally Unique Identifier vendor string */
378 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
379 const char *description
;
380 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
391 /* skip commonly misused 00:00:00 (Xerox) prefix */
392 if (memcmp(mac
, "\0\0\0", 3) == 0)
395 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
396 ETHER_ADDR_FORMAT_VAL(*mac
));
398 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
402 desc
= strdup(description
);
411 static int get_gateway_description(
416 union in_addr_union
*gateway
,
417 char **gateway_description
) {
418 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
419 sd_netlink_message
*m
;
423 assert(ifindex
>= 0);
424 assert(IN_SET(family
, AF_INET
, AF_INET6
));
426 assert(gateway_description
);
428 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
432 r
= sd_netlink_message_request_dump(req
, true);
436 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
440 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
441 union in_addr_union gw
= IN_ADDR_NULL
;
442 struct ether_addr mac
= ETHER_ADDR_NULL
;
446 r
= sd_netlink_message_get_errno(m
);
448 log_error_errno(r
, "got error: %m");
452 r
= sd_netlink_message_get_type(m
, &type
);
454 log_error_errno(r
, "could not get type: %m");
458 if (type
!= RTM_NEWNEIGH
) {
459 log_error("type is not RTM_NEWNEIGH");
463 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
465 log_error_errno(r
, "could not get family: %m");
470 log_error("family is not correct");
474 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
476 log_error_errno(r
, "could not get ifindex: %m");
480 if (ifindex
> 0 && ifi
!= ifindex
)
485 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
491 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
500 if (!in_addr_equal(fam
, &gw
, gateway
))
503 r
= sd_netlink_message_read_ether_addr(m
, NDA_LLADDR
, &mac
);
507 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
517 static int dump_gateways(
522 _cleanup_free_
struct local_address
*local
= NULL
;
528 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
532 for (i
= 0; i
< n
; i
++) {
533 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
, *with_description
= NULL
;
535 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
539 r
= table_add_cell(table
, NULL
, TABLE_STRING
, i
== 0 ? "Gateway:" : "");
543 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
547 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
549 log_debug_errno(r
, "Could not get description of gateway: %m");
552 with_description
= strjoin(gateway
, " (", description
, ")");
553 if (!with_description
)
557 /* Show interface name for the entry if we show
558 * entries for all interfaces */
560 char name
[IF_NAMESIZE
+1];
562 if (format_ifname(local
[i
].ifindex
, name
))
563 r
= table_add_cell_stringf(table
, NULL
, "%s on %s", with_description
?: gateway
, name
);
565 r
= table_add_cell_stringf(table
, NULL
, "%s on %%%i", with_description
?: gateway
, local
[i
].ifindex
);
567 r
= table_add_cell(table
, NULL
, TABLE_STRING
, with_description
?: gateway
);
575 static int dump_addresses(
580 _cleanup_free_
struct local_address
*local
= NULL
;
586 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
590 for (i
= 0; i
< n
; i
++) {
591 _cleanup_free_
char *pretty
= NULL
;
593 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
597 r
= table_add_cell(table
, NULL
, TABLE_STRING
, i
== 0 ? "Address:" : "");
601 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
606 char name
[IF_NAMESIZE
+1];
608 if (format_ifname(local
[i
].ifindex
, name
))
609 r
= table_add_cell_stringf(table
, NULL
, "%s on %s", pretty
, name
);
611 r
= table_add_cell_stringf(table
, NULL
, "%s on %%%i", pretty
, local
[i
].ifindex
);
613 r
= table_add_cell(table
, NULL
, TABLE_STRING
, pretty
);
621 static int dump_address_labels(sd_netlink
*rtnl
) {
622 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
623 _cleanup_(table_unrefp
) Table
*table
= NULL
;
624 sd_netlink_message
*m
;
630 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
632 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
634 r
= sd_netlink_message_request_dump(req
, true);
638 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
642 table
= table_new("Label", "Prefix/Prefixlen");
646 r
= table_set_sort(table
, 0, SIZE_MAX
);
650 assert_se(cell
= table_get_cell(table
, 0, 0));
651 (void) table_set_align_percent(table
, cell
, 100);
652 (void) table_set_ellipsize_percent(table
, cell
, 100);
654 assert_se(cell
= table_get_cell(table
, 0, 1));
655 (void) table_set_align_percent(table
, cell
, 100);
657 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
658 _cleanup_free_
char *pretty
= NULL
;
659 union in_addr_union prefix
= IN_ADDR_NULL
;
663 r
= sd_netlink_message_get_errno(m
);
665 log_error_errno(r
, "got error: %m");
669 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
670 if (r
< 0 && r
!= -ENODATA
) {
671 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
675 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
679 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
683 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
687 r
= table_add_cell(table
, NULL
, TABLE_UINT32
, &label
);
691 r
= table_add_cell_stringf(table
, &cell
, "%s/%u", pretty
, prefixlen
);
696 return table_print(table
, NULL
);
699 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
700 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
703 r
= sd_netlink_open(&rtnl
);
705 return log_error_errno(r
, "Failed to connect to netlink: %m");
707 dump_address_labels(rtnl
);
712 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
713 _cleanup_free_
char *p
= NULL
;
716 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
727 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
728 _cleanup_free_
void *raw
= NULL
;
736 l
= fread(&u
, 1, sizeof(u
), f
);
737 if (l
== 0 && feof(f
))
742 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
743 if (le64toh(u
) >= 4096)
746 raw
= new(uint8_t, le64toh(u
));
750 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
753 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
760 static int dump_lldp_neighbors(Table
*table
, const char *prefix
, int ifindex
) {
761 _cleanup_fclose_
FILE *f
= NULL
;
768 r
= open_lldp_neighbors(ifindex
, &f
);
775 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
776 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
777 _cleanup_free_
char *str
= NULL
;
779 r
= next_lldp_neighbor(f
, &n
);
785 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
789 r
= table_add_cell(table
, NULL
, TABLE_STRING
, c
== 0 ? prefix
: "");
793 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
794 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
795 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
797 if (asprintf(&str
, "%s on port %s%s%s%s",
798 strna(system_name
), strna(port_id
),
799 isempty(port_description
) ? "" : " (",
801 isempty(port_description
) ? "" : ")") < 0)
804 r
= table_add_cell(table
, NULL
, TABLE_STRING
, str
);
814 static int dump_ifindexes(Table
*table
, const char *prefix
, const int *ifindexes
) {
820 if (!ifindexes
|| ifindexes
[0] <= 0)
823 for (c
= 0; ifindexes
[c
] > 0; c
++) {
824 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
828 r
= table_add_cell(table
, NULL
, TABLE_STRING
, c
== 0 ? prefix
: "");
832 r
= table_add_cell(table
, NULL
, TABLE_IFINDEX
, ifindexes
+ c
);
840 static int dump_list(Table
*table
, const char *prefix
, char **l
) {
848 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
852 r
= table_add_cell(table
, NULL
, TABLE_STRING
, i
== l
? prefix
: "");
856 r
= table_add_cell(table
, NULL
, TABLE_STRING
, *i
);
864 #define DUMP_STATS_ONE(name, val_name) \
865 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL); \
868 r = table_add_cell(table, NULL, TABLE_STRING, name ":"); \
871 r = table_add_cell(table, NULL, info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
872 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
876 static int dump_statistics(Table
*table
, const LinkInfo
*info
) {
882 if (!info
->has_stats64
&& !info
->has_stats
)
885 DUMP_STATS_ONE("Rx Packets", rx_packets
);
886 DUMP_STATS_ONE("Tx Packets", tx_packets
);
887 DUMP_STATS_ONE("Rx Bytes", rx_bytes
);
888 DUMP_STATS_ONE("Tx Bytes", tx_bytes
);
889 DUMP_STATS_ONE("Rx Errors", rx_errors
);
890 DUMP_STATS_ONE("Tx Errors", tx_errors
);
891 DUMP_STATS_ONE("Rx Dropped", rx_dropped
);
892 DUMP_STATS_ONE("Tx Dropped", tx_dropped
);
893 DUMP_STATS_ONE("Multicast Packets", multicast
);
894 DUMP_STATS_ONE("Collisions", collisions
);
899 static const struct {
903 { .val
= 1e15
, .str
= "P" },
904 { .val
= 1e12
, .str
= "T" },
905 { .val
= 1e9
, .str
= "G" },
906 { .val
= 1e6
, .str
= "M" },
907 { .val
= 1e3
, .str
= "k" },
910 static void get_prefix(double val
, double *ret_div
, const char **ret_prefix
) {
914 for (size_t i
= 0; i
< ELEMENTSOF(prefix_table
); i
++)
915 if (val
> prefix_table
[i
].val
) {
916 *ret_div
= prefix_table
[i
].val
;
917 *ret_prefix
= prefix_table
[i
].str
;
925 static int link_status_one(
928 const LinkInfo
*info
) {
930 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
931 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
, *tz
= NULL
;
932 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
933 char devid
[2 + DECIMAL_STR_MAX(int)];
934 _cleanup_free_
char *t
= NULL
, *network
= NULL
;
935 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
;
936 const char *on_color_operational
, *off_color_operational
,
937 *on_color_setup
, *off_color_setup
;
938 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
939 _cleanup_(table_unrefp
) Table
*table
= NULL
;
946 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
947 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
949 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
950 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
951 setup_state
= strdup("unmanaged");
952 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
954 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
955 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
956 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
957 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
959 xsprintf(devid
, "n%i", info
->ifindex
);
961 (void) sd_device_new_from_device_id(&d
, devid
);
964 (void) sd_device_get_property_value(d
, "ID_NET_LINK_FILE", &link
);
965 (void) sd_device_get_property_value(d
, "ID_NET_DRIVER", &driver
);
966 (void) sd_device_get_property_value(d
, "ID_PATH", &path
);
968 if (sd_device_get_property_value(d
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
969 (void) sd_device_get_property_value(d
, "ID_VENDOR", &vendor
);
971 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
972 (void) sd_device_get_property_value(d
, "ID_MODEL", &model
);
975 t
= link_get_type_string(info
->iftype
, d
);
977 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
979 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
980 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
982 table
= table_new("DOT", "KEY", "VALUE");
986 assert_se(cell
= table_get_cell(table
, 0, 0));
987 (void) table_set_ellipsize_percent(table
, cell
, 100);
989 assert_se(cell
= table_get_cell(table
, 0, 1));
990 (void) table_set_ellipsize_percent(table
, cell
, 100);
992 table_set_header(table
, false);
994 r
= table_add_cell(table
, &cell
, TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
));
997 (void) table_set_color(table
, cell
, on_color_operational
);
998 r
= table_add_cell_stringf(table
, &cell
, "%i: %s", info
->ifindex
, info
->name
);
1001 (void) table_set_align_percent(table
, cell
, 0);
1002 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1006 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1009 r
= table_add_cell(table
, &cell
, TABLE_STRING
, "Link File:");
1012 (void) table_set_align_percent(table
, cell
, 100);
1013 r
= table_add_cell(table
, NULL
, TABLE_STRING
, strna(link
));
1017 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1020 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Network File:");
1023 r
= table_add_cell(table
, NULL
, TABLE_STRING
, strna(network
));
1027 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1030 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Type:");
1033 r
= table_add_cell(table
, NULL
, TABLE_STRING
, strna(t
));
1037 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1040 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "State:");
1043 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s (%s%s%s)",
1044 on_color_operational
, strna(operational_state
), off_color_operational
,
1045 on_color_setup
, strna(setup_state
), off_color_setup
);
1050 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1053 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Path:");
1056 r
= table_add_cell(table
, NULL
, TABLE_STRING
, path
);
1061 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1064 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Driver:");
1067 r
= table_add_cell(table
, NULL
, TABLE_STRING
, driver
);
1072 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1075 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Vendor:");
1078 r
= table_add_cell(table
, NULL
, TABLE_STRING
, vendor
);
1083 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1086 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Model:");
1089 r
= table_add_cell(table
, NULL
, TABLE_STRING
, model
);
1094 if (info
->has_mac_address
) {
1095 _cleanup_free_
char *description
= NULL
;
1096 char ea
[ETHER_ADDR_TO_STRING_MAX
];
1098 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
1100 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1103 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "HW Address:");
1106 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s%s",
1107 ether_addr_to_string(&info
->mac_address
, ea
),
1108 description
? " (" : "",
1110 description
? ")" : "");
1115 if (info
->mtu
> 0) {
1116 char min_str
[DECIMAL_STR_MAX(uint32_t)], max_str
[DECIMAL_STR_MAX(uint32_t)];
1118 xsprintf(min_str
, "%" PRIu32
, info
->min_mtu
);
1119 xsprintf(max_str
, "%" PRIu32
, info
->max_mtu
);
1121 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1124 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "MTU:");
1127 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"%s%s%s%s%s%s%s",
1129 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? " (" : "",
1130 info
->min_mtu
> 0 ? "min: " : "",
1131 info
->min_mtu
> 0 ? min_str
: "",
1132 info
->min_mtu
> 0 && info
->max_mtu
> 0 ? ", " : "",
1133 info
->max_mtu
> 0 ? "max: " : "",
1134 info
->max_mtu
> 0 ? max_str
: "",
1135 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? ")" : "");
1140 if (info
->has_bitrates
) {
1141 const char *tx_prefix
, *rx_prefix
;
1142 double tx_div
, rx_div
;
1144 get_prefix(info
->tx_bitrate
, &tx_div
, &tx_prefix
);
1145 get_prefix(info
->rx_bitrate
, &rx_div
, &rx_prefix
);
1147 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1150 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Bit Rate (Tx/Rx):");
1154 r
= table_add_cell_stringf(table
, NULL
, "%.4g %sbps/%.4g %sbps",
1155 info
->tx_bitrate
/ tx_div
, strempty(tx_prefix
),
1156 info
->rx_bitrate
/ rx_div
, strempty(rx_prefix
));
1161 if (info
->has_tx_queues
|| info
->has_rx_queues
) {
1162 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1165 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Queue Length (Tx/Rx):");
1168 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"/%" PRIu32
, info
->tx_queues
, info
->rx_queues
);
1173 r
= dump_addresses(rtnl
, table
, info
->ifindex
);
1176 r
= dump_gateways(rtnl
, hwdb
, table
, info
->ifindex
);
1179 r
= dump_list(table
, "DNS:", dns
);
1182 r
= dump_list(table
, "Search Domains:", search_domains
);
1185 r
= dump_list(table
, "Route Domains:", route_domains
);
1188 r
= dump_list(table
, "NTP:", ntp
);
1191 r
= dump_ifindexes(table
, "Carrier Bound To:", carrier_bound_to
);
1194 r
= dump_ifindexes(table
, "Carrier Bound By:", carrier_bound_by
);
1198 (void) sd_network_link_get_timezone(info
->ifindex
, &tz
);
1200 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1203 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "Time Zone:");
1206 r
= table_add_cell(table
, NULL
, TABLE_STRING
, tz
);
1211 r
= dump_lldp_neighbors(table
, "Connected To:", info
->ifindex
);
1215 r
= dump_statistics(table
, info
);
1219 return table_print(table
, NULL
);
1222 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
1223 _cleanup_free_
char *operational_state
= NULL
;
1224 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
1225 const char *on_color_operational
, *off_color_operational
;
1226 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1232 (void) sd_network_get_operational_state(&operational_state
);
1233 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
1235 table
= table_new("DOT", "KEY", "VALUE");
1239 assert_se(cell
= table_get_cell(table
, 0, 0));
1240 (void) table_set_ellipsize_percent(table
, cell
, 100);
1242 assert_se(cell
= table_get_cell(table
, 0, 1));
1243 (void) table_set_align_percent(table
, cell
, 100);
1244 (void) table_set_ellipsize_percent(table
, cell
, 100);
1246 table_set_header(table
, false);
1248 r
= table_add_cell(table
, &cell
, TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
));
1251 (void) table_set_color(table
, cell
, on_color_operational
);
1253 r
= table_add_cell(table
, NULL
, TABLE_STRING
, "State:");
1257 r
= table_add_cell(table
, &cell
, TABLE_STRING
, strna(operational_state
));
1260 (void) table_set_color(table
, cell
, on_color_operational
);
1262 r
= dump_addresses(rtnl
, table
, 0);
1265 r
= dump_gateways(rtnl
, hwdb
, table
, 0);
1269 (void) sd_network_get_dns(&dns
);
1270 r
= dump_list(table
, "DNS:", dns
);
1274 (void) sd_network_get_search_domains(&search_domains
);
1275 r
= dump_list(table
, "Search Domains:", search_domains
);
1279 (void) sd_network_get_route_domains(&route_domains
);
1280 r
= dump_list(table
, "Route Domains:", route_domains
);
1284 (void) sd_network_get_ntp(&ntp
);
1285 r
= dump_list(table
, "NTP:", ntp
);
1289 return table_print(table
, NULL
);
1292 static int link_status(int argc
, char *argv
[], void *userdata
) {
1293 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1294 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1295 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
1296 _cleanup_free_ LinkInfo
*links
= NULL
;
1299 (void) pager_open(arg_pager_flags
);
1301 r
= sd_bus_open_system(&bus
);
1303 return log_error_errno(r
, "Failed to connect system bus: %m");
1305 r
= sd_netlink_open(&rtnl
);
1307 return log_error_errno(r
, "Failed to connect to netlink: %m");
1309 r
= sd_hwdb_new(&hwdb
);
1311 log_debug_errno(r
, "Failed to open hardware database: %m");
1314 c
= acquire_link_info(bus
, rtnl
, NULL
, &links
);
1316 return system_status(rtnl
, hwdb
);
1318 c
= acquire_link_info(bus
, rtnl
, argv
+ 1, &links
);
1322 for (i
= 0; i
< c
; i
++) {
1324 fputc('\n', stdout
);
1326 link_status_one(rtnl
, hwdb
, links
+ i
);
1332 static char *lldp_capabilities_to_string(uint16_t x
) {
1333 static const char characters
[] = {
1334 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
1339 ret
= new(char, ELEMENTSOF(characters
) + 1);
1343 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
1344 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
1350 static void lldp_capabilities_legend(uint16_t x
) {
1351 unsigned w
, i
, cols
= columns();
1352 static const char* const table
[] = {
1356 "w - WLAN Access Point",
1359 "d - DOCSIS cable device",
1361 "c - Customer VLAN",
1363 "m - Two-port MAC Relay (TPMR)",
1369 printf("\nCapability Flags:\n");
1370 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
1371 if (x
& (1U << i
) || arg_all
) {
1374 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
1377 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
1382 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
1383 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1384 _cleanup_free_ LinkInfo
*links
= NULL
;
1385 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1390 r
= sd_netlink_open(&rtnl
);
1392 return log_error_errno(r
, "Failed to connect to netlink: %m");
1394 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
1398 (void) pager_open(arg_pager_flags
);
1400 table
= table_new("LINK",
1405 "PORT DESCRIPTION");
1409 table_set_header(table
, arg_legend
);
1411 assert_se(cell
= table_get_cell(table
, 0, 0));
1412 table_set_minimum_width(table
, cell
, 16);
1414 assert_se(cell
= table_get_cell(table
, 0, 1));
1415 table_set_minimum_width(table
, cell
, 17);
1417 assert_se(cell
= table_get_cell(table
, 0, 2));
1418 table_set_minimum_width(table
, cell
, 16);
1420 assert_se(cell
= table_get_cell(table
, 0, 3));
1421 table_set_minimum_width(table
, cell
, 11);
1423 assert_se(cell
= table_get_cell(table
, 0, 4));
1424 table_set_minimum_width(table
, cell
, 17);
1426 assert_se(cell
= table_get_cell(table
, 0, 5));
1427 table_set_minimum_width(table
, cell
, 16);
1429 for (i
= 0; i
< c
; i
++) {
1430 _cleanup_fclose_
FILE *f
= NULL
;
1432 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
1436 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
1441 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
, *capabilities
= NULL
;
1442 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
;
1443 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1446 r
= next_lldp_neighbor(f
, &n
);
1448 log_warning_errno(r
, "Failed to read neighbor data: %m");
1454 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
1455 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1456 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1457 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1460 cid
= ellipsize(chassis_id
, 17, 100);
1466 pid
= ellipsize(port_id
, 17, 100);
1472 sname
= ellipsize(system_name
, 16, 100);
1474 system_name
= sname
;
1477 if (port_description
) {
1478 pdesc
= ellipsize(port_description
, 16, 100);
1480 port_description
= pdesc
;
1483 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
1484 capabilities
= lldp_capabilities_to_string(cc
);
1488 r
= table_add_many(table
,
1489 TABLE_STRING
, links
[i
].name
,
1490 TABLE_STRING
, strna(chassis_id
),
1491 TABLE_STRING
, strna(system_name
),
1492 TABLE_STRING
, strna(capabilities
),
1493 TABLE_STRING
, strna(port_id
),
1494 TABLE_STRING
, strna(port_description
));
1502 r
= table_print(table
, NULL
);
1507 lldp_capabilities_legend(all
);
1508 printf("\n%i neighbors listed.\n", m
);
1514 static int link_delete_send_message(sd_netlink
*rtnl
, int index
) {
1515 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1520 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_DELLINK
, index
);
1522 return rtnl_log_create_error(r
);
1524 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
1531 static int link_delete(int argc
, char *argv
[], void *userdata
) {
1532 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1533 _cleanup_set_free_ Set
*indexes
= NULL
;
1537 r
= sd_netlink_open(&rtnl
);
1539 return log_error_errno(r
, "Failed to connect to netlink: %m");
1541 indexes
= set_new(NULL
);
1545 for (i
= 1; i
< argc
; i
++) {
1546 r
= parse_ifindex_or_ifname(argv
[i
], &index
);
1548 return log_error_errno(r
, "Failed to resolve interface %s", argv
[i
]);
1550 r
= set_put(indexes
, INT_TO_PTR(index
));
1555 SET_FOREACH(index
, indexes
, j
) {
1556 r
= link_delete_send_message(rtnl
, index
);
1558 char ifname
[IF_NAMESIZE
+ 1];
1560 if (format_ifname(index
, ifname
))
1561 return log_error_errno(r
, "Failed to delete interface %s: %m", ifname
);
1563 return log_error_errno(r
, "Failed to delete interface %d: %m", index
);
1570 static int help(void) {
1571 _cleanup_free_
char *link
= NULL
;
1574 r
= terminal_urlify_man("networkctl", "1", &link
);
1578 printf("%s [OPTIONS...]\n\n"
1579 "Query and control the networking subsystem.\n\n"
1580 " -h --help Show this help\n"
1581 " --version Show package version\n"
1582 " --no-pager Do not pipe output into a pager\n"
1583 " --no-legend Do not show the headers and footers\n"
1584 " -a --all Show status for all links\n"
1585 " -s --stats Show detailed link statics\n"
1587 " list [PATTERN...] List links\n"
1588 " status [PATTERN...] Show link status\n"
1589 " lldp [PATTERN...] Show LLDP neighbors\n"
1590 " label Show current address label entries in the kernel\n"
1591 " delete DEVICES Delete virtual netdevs\n"
1592 "\nSee the %s for details.\n"
1593 , program_invocation_short_name
1600 static int parse_argv(int argc
, char *argv
[]) {
1603 ARG_VERSION
= 0x100,
1608 static const struct option options
[] = {
1609 { "help", no_argument
, NULL
, 'h' },
1610 { "version", no_argument
, NULL
, ARG_VERSION
},
1611 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1612 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1613 { "all", no_argument
, NULL
, 'a' },
1614 { "stats", no_argument
, NULL
, 's' },
1623 while ((c
= getopt_long(argc
, argv
, "has", options
, NULL
)) >= 0) {
1634 arg_pager_flags
|= PAGER_DISABLE
;
1653 assert_not_reached("Unhandled option");
1660 static int networkctl_main(int argc
, char *argv
[]) {
1661 static const Verb verbs
[] = {
1662 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
1663 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
1664 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
1665 { "label", VERB_ANY
, VERB_ANY
, 0, list_address_labels
},
1666 { "delete", 2, VERB_ANY
, 0, link_delete
},
1670 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1673 static void warn_networkd_missing(void) {
1675 if (access("/run/systemd/netif/state", F_OK
) >= 0)
1678 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1681 static int run(int argc
, char* argv
[]) {
1684 log_show_color(true);
1685 log_parse_environment();
1688 r
= parse_argv(argc
, argv
);
1692 warn_networkd_missing();
1694 return networkctl_main(argc
, argv
);
1697 DEFINE_MAIN_FUNCTION(run
);