1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/if_addrlabel.h>
11 #include "sd-device.h"
14 #include "sd-netlink.h"
15 #include "sd-network.h"
17 #include "alloc-util.h"
18 #include "arphrd-list.h"
19 #include "device-util.h"
20 #include "ether-addr-util.h"
22 #include "format-table.h"
23 #include "format-util.h"
24 #include "hwdb-util.h"
25 #include "local-addresses.h"
26 #include "locale-util.h"
28 #include "main-func.h"
29 #include "netlink-util.h"
31 #include "parse-util.h"
32 #include "pretty-print.h"
34 #include "socket-util.h"
35 #include "sort-util.h"
36 #include "sparse-endian.h"
37 #include "stdio-util.h"
38 #include "string-table.h"
39 #include "string-util.h"
42 #include "terminal-util.h"
45 static PagerFlags arg_pager_flags
= 0;
46 static bool arg_legend
= true;
47 static bool arg_all
= false;
49 static char *link_get_type_string(unsigned short iftype
, sd_device
*d
) {
50 const char *t
, *devtype
;
54 sd_device_get_devtype(d
, &devtype
) >= 0 &&
56 return strdup(devtype
);
58 t
= arphrd_to_name(iftype
);
70 static void operational_state_to_color(const char *state
, const char **on
, const char **off
) {
74 if (STRPTR_IN_SET(state
, "routable", "enslaved")) {
75 *on
= ansi_highlight_green();
77 } else if (streq_ptr(state
, "degraded")) {
78 *on
= ansi_highlight_yellow();
84 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
88 if (streq_ptr(state
, "configured")) {
89 *on
= ansi_highlight_green();
91 } else if (streq_ptr(state
, "configuring")) {
92 *on
= ansi_highlight_yellow();
94 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
95 *on
= ansi_highlight_red();
101 typedef struct LinkInfo
{
102 char name
[IFNAMSIZ
+1];
104 unsigned short iftype
;
105 struct ether_addr mac_address
;
112 bool has_mac_address
:1;
113 bool has_tx_queues
:1;
114 bool has_rx_queues
:1;
117 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
118 return CMP(a
->ifindex
, b
->ifindex
);
121 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
, char **patterns
) {
129 r
= sd_netlink_message_get_type(m
, &type
);
133 if (type
!= RTM_NEWLINK
)
136 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
140 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
145 char str
[DECIMAL_STR_MAX(int)];
147 xsprintf(str
, "%i", ifindex
);
149 if (!strv_fnmatch(patterns
, str
, 0) && !strv_fnmatch(patterns
, name
, 0))
153 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
157 strscpy(info
->name
, sizeof info
->name
, name
);
158 info
->ifindex
= ifindex
;
160 info
->has_mac_address
=
161 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
162 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
164 (void) sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
);
165 (void) sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
);
166 (void) sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
);
168 info
->has_rx_queues
=
169 sd_netlink_message_read_u32(m
, IFLA_NUM_RX_QUEUES
, &info
->rx_queues
) >= 0 &&
172 info
->has_tx_queues
=
173 sd_netlink_message_read_u32(m
, IFLA_NUM_TX_QUEUES
, &info
->tx_queues
) >= 0 &&
179 static int acquire_link_info(sd_netlink
*rtnl
, char **patterns
, LinkInfo
**ret
) {
180 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
181 _cleanup_free_ LinkInfo
*links
= NULL
;
182 size_t allocated
= 0, c
= 0;
183 sd_netlink_message
*i
;
189 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
191 return rtnl_log_create_error(r
);
193 r
= sd_netlink_message_request_dump(req
, true);
195 return rtnl_log_create_error(r
);
197 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
199 return log_error_errno(r
, "Failed to enumerate links: %m");
201 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
202 if (!GREEDY_REALLOC(links
, allocated
, c
+1))
205 r
= decode_link(i
, links
+ c
, patterns
);
212 typesafe_qsort(links
, c
, link_info_compare
);
214 *ret
= TAKE_PTR(links
);
219 static int list_links(int argc
, char *argv
[], void *userdata
) {
220 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
221 _cleanup_free_ LinkInfo
*links
= NULL
;
222 _cleanup_(table_unrefp
) Table
*table
= NULL
;
226 r
= sd_netlink_open(&rtnl
);
228 return log_error_errno(r
, "Failed to connect to netlink: %m");
230 c
= acquire_link_info(rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
234 (void) pager_open(arg_pager_flags
);
236 table
= table_new("IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");
240 table_set_header(table
, arg_legend
);
242 assert_se(cell
= table_get_cell(table
, 0, 0));
243 (void) table_set_minimum_width(table
, cell
, 3);
244 (void) table_set_weight(table
, cell
, 0);
245 (void) table_set_ellipsize_percent(table
, cell
, 0);
246 (void) table_set_align_percent(table
, cell
, 100);
248 assert_se(cell
= table_get_cell(table
, 0, 1));
249 (void) table_set_minimum_width(table
, cell
, 16);
251 assert_se(cell
= table_get_cell(table
, 0, 2));
252 (void) table_set_minimum_width(table
, cell
, 18);
254 assert_se(cell
= table_get_cell(table
, 0, 3));
255 (void) table_set_minimum_width(table
, cell
, 16);
257 assert_se(cell
= table_get_cell(table
, 0, 4));
258 (void) table_set_minimum_width(table
, cell
, 10);
260 for (i
= 0; i
< c
; i
++) {
261 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
262 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
263 const char *on_color_operational
, *off_color_operational
,
264 *on_color_setup
, *off_color_setup
;
265 char devid
[2 + DECIMAL_STR_MAX(int)];
266 _cleanup_free_
char *t
= NULL
;
268 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
269 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
271 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
272 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
273 setup_state
= strdup("unmanaged");
274 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
276 xsprintf(devid
, "n%i", links
[i
].ifindex
);
277 (void) sd_device_new_from_device_id(&d
, devid
);
279 t
= link_get_type_string(links
[i
].iftype
, d
);
281 r
= table_add_cell_full(table
, NULL
, TABLE_INT
, &links
[i
].ifindex
, SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
285 r
= table_add_many(table
,
286 TABLE_STRING
, links
[i
].name
,
287 TABLE_STRING
, strna(t
));
291 r
= table_add_cell(table
, &cell
, TABLE_STRING
, strna(operational_state
));
295 (void) table_set_color(table
, cell
, on_color_operational
);
297 r
= table_add_cell(table
, &cell
, TABLE_STRING
, strna(setup_state
));
301 (void) table_set_color(table
, cell
, on_color_setup
);
304 r
= table_print(table
, NULL
);
306 return log_error_errno(r
, "Failed to print table: %m");
309 printf("\n%i links listed.\n", c
);
314 /* IEEE Organizationally Unique Identifier vendor string */
315 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
316 const char *description
;
317 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
328 /* skip commonly misused 00:00:00 (Xerox) prefix */
329 if (memcmp(mac
, "\0\0\0", 3) == 0)
332 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
333 ETHER_ADDR_FORMAT_VAL(*mac
));
335 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
339 desc
= strdup(description
);
348 static int get_gateway_description(
353 union in_addr_union
*gateway
,
354 char **gateway_description
) {
355 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
356 sd_netlink_message
*m
;
360 assert(ifindex
>= 0);
361 assert(IN_SET(family
, AF_INET
, AF_INET6
));
363 assert(gateway_description
);
365 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
369 r
= sd_netlink_message_request_dump(req
, true);
373 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
377 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
378 union in_addr_union gw
= IN_ADDR_NULL
;
379 struct ether_addr mac
= ETHER_ADDR_NULL
;
383 r
= sd_netlink_message_get_errno(m
);
385 log_error_errno(r
, "got error: %m");
389 r
= sd_netlink_message_get_type(m
, &type
);
391 log_error_errno(r
, "could not get type: %m");
395 if (type
!= RTM_NEWNEIGH
) {
396 log_error("type is not RTM_NEWNEIGH");
400 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
402 log_error_errno(r
, "could not get family: %m");
407 log_error("family is not correct");
411 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
413 log_error_errno(r
, "could not get ifindex: %m");
417 if (ifindex
> 0 && ifi
!= ifindex
)
422 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
428 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
437 if (!in_addr_equal(fam
, &gw
, gateway
))
440 r
= sd_netlink_message_read_ether_addr(m
, NDA_LLADDR
, &mac
);
444 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
454 static int dump_gateways(
459 _cleanup_free_
struct local_address
*local
= NULL
;
465 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
469 for (i
= 0; i
< n
; i
++) {
470 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
, *with_description
= NULL
;
472 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
476 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, i
== 0 ? "Gateway:" : "", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
480 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
484 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
486 log_debug_errno(r
, "Could not get description of gateway: %m");
489 with_description
= strjoin(gateway
, " (", description
, ")");
490 if (!with_description
)
494 /* Show interface name for the entry if we show
495 * entries for all interfaces */
497 char name
[IF_NAMESIZE
+1];
499 if (format_ifname(local
[i
].ifindex
, name
))
500 r
= table_add_cell_stringf(table
, NULL
, "%s on %s", with_description
?: gateway
, name
);
502 r
= table_add_cell_stringf(table
, NULL
, "%s on %%%i", with_description
?: gateway
, local
[i
].ifindex
);
504 r
= table_add_cell(table
, NULL
, TABLE_STRING
, with_description
?: gateway
);
512 static int dump_addresses(
517 _cleanup_free_
struct local_address
*local
= NULL
;
523 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
527 for (i
= 0; i
< n
; i
++) {
528 _cleanup_free_
char *pretty
= NULL
;
530 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
534 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, i
== 0 ? "Address:" : "", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
538 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
543 char name
[IF_NAMESIZE
+1];
545 if (format_ifname(local
[i
].ifindex
, name
))
546 r
= table_add_cell_stringf(table
, NULL
, "%s on %s", pretty
, name
);
548 r
= table_add_cell_stringf(table
, NULL
, "%s on %%%i", pretty
, local
[i
].ifindex
);
550 r
= table_add_cell(table
, NULL
, TABLE_STRING
, pretty
);
558 static int dump_address_labels(sd_netlink
*rtnl
) {
559 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
560 _cleanup_(table_unrefp
) Table
*table
= NULL
;
561 sd_netlink_message
*m
;
567 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
569 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
571 r
= sd_netlink_message_request_dump(req
, true);
575 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
579 table
= table_new("Label", "Prefix/Prefixlen");
583 r
= table_set_sort(table
, 0, SIZE_MAX
);
587 assert_se(cell
= table_get_cell(table
, 0, 0));
588 (void) table_set_align_percent(table
, cell
, 100);
590 assert_se(cell
= table_get_cell(table
, 0, 1));
591 (void) table_set_align_percent(table
, cell
, 100);
593 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
594 _cleanup_free_
char *pretty
= NULL
;
595 union in_addr_union prefix
= IN_ADDR_NULL
;
599 r
= sd_netlink_message_get_errno(m
);
601 log_error_errno(r
, "got error: %m");
605 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
606 if (r
< 0 && r
!= -ENODATA
) {
607 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
611 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
615 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
619 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
623 r
= table_add_cell_full(table
, NULL
, TABLE_UINT32
, &label
, SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
627 r
= table_add_cell_stringf(table
, &cell
, "%s/%u", pretty
, prefixlen
);
631 (void) table_set_align_percent(table
, cell
, 100);
634 return table_print(table
, NULL
);
637 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
638 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
641 r
= sd_netlink_open(&rtnl
);
643 return log_error_errno(r
, "Failed to connect to netlink: %m");
645 dump_address_labels(rtnl
);
650 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
651 _cleanup_free_
char *p
= NULL
;
654 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
665 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
666 _cleanup_free_
void *raw
= NULL
;
674 l
= fread(&u
, 1, sizeof(u
), f
);
675 if (l
== 0 && feof(f
))
680 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
681 if (le64toh(u
) >= 4096)
684 raw
= new(uint8_t, le64toh(u
));
688 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
691 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
698 static int dump_lldp_neighbors(Table
*table
, const char *prefix
, int ifindex
) {
699 _cleanup_fclose_
FILE *f
= NULL
;
706 r
= open_lldp_neighbors(ifindex
, &f
);
713 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
714 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
715 _cleanup_free_
char *str
= NULL
;
717 r
= next_lldp_neighbor(f
, &n
);
723 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
727 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, c
== 0 ? prefix
: "", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
731 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
732 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
733 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
735 if (asprintf(&str
, "%s on port %s%s%s%s",
736 strna(system_name
), strna(port_id
),
737 isempty(port_description
) ? "" : " (",
739 isempty(port_description
) ? "" : ")") < 0)
742 r
= table_add_cell(table
, NULL
, TABLE_STRING
, str
);
752 static int dump_ifindexes(Table
*table
, const char *prefix
, const int *ifindexes
) {
758 if (!ifindexes
|| ifindexes
[0] <= 0)
761 for (c
= 0; ifindexes
[c
] > 0; c
++) {
762 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
766 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, c
== 0 ? prefix
: "", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
770 r
= table_add_cell(table
, NULL
, TABLE_IFINDEX
, ifindexes
+ c
);
778 static int dump_list(Table
*table
, const char *prefix
, char **l
) {
786 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
790 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, i
== l
? prefix
: "", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
794 r
= table_add_cell(table
, NULL
, TABLE_STRING
, *i
);
802 static int link_status_one(
805 const LinkInfo
*info
) {
807 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
808 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
, *tz
= NULL
;
809 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
810 char devid
[2 + DECIMAL_STR_MAX(int)];
811 _cleanup_free_
char *t
= NULL
, *network
= NULL
;
812 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
;
813 const char *on_color_operational
, *off_color_operational
,
814 *on_color_setup
, *off_color_setup
;
815 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
816 _cleanup_(table_unrefp
) Table
*table
= NULL
;
823 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
824 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
826 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
827 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
828 setup_state
= strdup("unmanaged");
829 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
831 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
832 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
833 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
834 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
836 xsprintf(devid
, "n%i", info
->ifindex
);
838 (void) sd_device_new_from_device_id(&d
, devid
);
841 (void) sd_device_get_property_value(d
, "ID_NET_LINK_FILE", &link
);
842 (void) sd_device_get_property_value(d
, "ID_NET_DRIVER", &driver
);
843 (void) sd_device_get_property_value(d
, "ID_PATH", &path
);
845 if (sd_device_get_property_value(d
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
846 (void) sd_device_get_property_value(d
, "ID_VENDOR", &vendor
);
848 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
849 (void) sd_device_get_property_value(d
, "ID_MODEL", &model
);
852 t
= link_get_type_string(info
->iftype
, d
);
854 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
856 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
857 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
859 table
= table_new("DOT", "KEY", "VALUE");
863 table_set_header(table
, false);
865 r
= table_add_cell(table
, &cell
, TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
));
868 (void) table_set_ellipsize_percent(table
, cell
, 0);
869 (void) table_set_color(table
, cell
, on_color_operational
);
870 r
= table_add_cell_stringf(table
, NULL
, "%i: %s", info
->ifindex
, info
->name
);
873 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
877 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
880 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Link File:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
883 r
= table_add_cell(table
, NULL
, TABLE_STRING
, strna(link
));
887 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
890 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Network File:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
893 r
= table_add_cell(table
, NULL
, TABLE_STRING
, strna(network
));
897 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
900 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Type:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
903 r
= table_add_cell(table
, NULL
, TABLE_STRING
, strna(t
));
907 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
910 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "State:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
913 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s (%s%s%s)",
914 on_color_operational
, strna(operational_state
), off_color_operational
,
915 on_color_setup
, strna(setup_state
), off_color_setup
);
920 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
923 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Path:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
926 r
= table_add_cell(table
, NULL
, TABLE_STRING
, path
);
931 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
934 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Driver:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
937 r
= table_add_cell(table
, NULL
, TABLE_STRING
, driver
);
942 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
945 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Vendor:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
948 r
= table_add_cell(table
, NULL
, TABLE_STRING
, vendor
);
953 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
956 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Model:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
959 r
= table_add_cell(table
, NULL
, TABLE_STRING
, model
);
964 if (info
->has_mac_address
) {
965 _cleanup_free_
char *description
= NULL
;
966 char ea
[ETHER_ADDR_TO_STRING_MAX
];
968 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
970 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
973 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "HW Address:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
976 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s%s",
977 ether_addr_to_string(&info
->mac_address
, ea
),
978 description
? " (" : "",
980 description
? ")" : "");
986 char min_str
[DECIMAL_STR_MAX(uint32_t)], max_str
[DECIMAL_STR_MAX(uint32_t)];
988 xsprintf(min_str
, "%" PRIu32
, info
->min_mtu
);
989 xsprintf(max_str
, "%" PRIu32
, info
->max_mtu
);
991 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
994 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "MTU:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
997 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"%s%s%s%s%s%s%s",
999 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? " (" : "",
1000 info
->min_mtu
> 0 ? "min: " : "",
1001 info
->min_mtu
> 0 ? min_str
: "",
1002 info
->min_mtu
> 0 && info
->max_mtu
> 0 ? ", " : "",
1003 info
->max_mtu
> 0 ? "max: " : "",
1004 info
->max_mtu
> 0 ? max_str
: "",
1005 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? ")" : "");
1010 if (info
->has_tx_queues
|| info
->has_rx_queues
) {
1011 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1014 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Queue Length (Tx/Rx):", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
1017 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"/%" PRIu32
, info
->tx_queues
, info
->rx_queues
);
1022 r
= dump_addresses(rtnl
, table
, info
->ifindex
);
1025 r
= dump_gateways(rtnl
, hwdb
, table
, info
->ifindex
);
1028 r
= dump_list(table
, "DNS:", dns
);
1031 r
= dump_list(table
, "Search Domains:", search_domains
);
1034 r
= dump_list(table
, "Route Domains:", route_domains
);
1037 r
= dump_list(table
, "NTP:", ntp
);
1040 r
= dump_ifindexes(table
, "Carrier Bound To:", carrier_bound_to
);
1043 r
= dump_ifindexes(table
, "Carrier Bound By:", carrier_bound_by
);
1047 (void) sd_network_link_get_timezone(info
->ifindex
, &tz
);
1049 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1052 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "Time Zone:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
1055 r
= table_add_cell(table
, NULL
, TABLE_STRING
, tz
);
1060 r
= dump_lldp_neighbors(table
, "Connected To:", info
->ifindex
);
1064 return table_print(table
, NULL
);
1067 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
1068 _cleanup_free_
char *operational_state
= NULL
;
1069 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
1070 const char *on_color_operational
, *off_color_operational
;
1071 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1077 (void) sd_network_get_operational_state(&operational_state
);
1078 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
1080 table
= table_new("DOT", "KEY", "VALUE");
1084 table_set_header(table
, false);
1086 r
= table_add_cell(table
, &cell
, TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
));
1089 (void) table_set_color(table
, cell
, on_color_operational
);
1090 (void) table_set_ellipsize_percent(table
, cell
, 0);
1092 r
= table_add_cell_full(table
, NULL
, TABLE_STRING
, "State:", SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
1096 r
= table_add_cell(table
, &cell
, TABLE_STRING
, strna(operational_state
));
1099 (void) table_set_color(table
, cell
, on_color_operational
);
1101 r
= dump_addresses(rtnl
, table
, 0);
1104 r
= dump_gateways(rtnl
, hwdb
, table
, 0);
1108 (void) sd_network_get_dns(&dns
);
1109 r
= dump_list(table
, "DNS:", dns
);
1113 (void) sd_network_get_search_domains(&search_domains
);
1114 r
= dump_list(table
, "Search Domains:", search_domains
);
1118 (void) sd_network_get_route_domains(&route_domains
);
1119 r
= dump_list(table
, "Route Domains:", route_domains
);
1123 (void) sd_network_get_ntp(&ntp
);
1124 r
= dump_list(table
, "NTP:", ntp
);
1128 return table_print(table
, NULL
);
1131 static int link_status(int argc
, char *argv
[], void *userdata
) {
1132 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1133 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
1134 _cleanup_free_ LinkInfo
*links
= NULL
;
1137 (void) pager_open(arg_pager_flags
);
1139 r
= sd_netlink_open(&rtnl
);
1141 return log_error_errno(r
, "Failed to connect to netlink: %m");
1143 r
= sd_hwdb_new(&hwdb
);
1145 log_debug_errno(r
, "Failed to open hardware database: %m");
1148 c
= acquire_link_info(rtnl
, NULL
, &links
);
1150 return system_status(rtnl
, hwdb
);
1152 c
= acquire_link_info(rtnl
, argv
+ 1, &links
);
1156 for (i
= 0; i
< c
; i
++) {
1158 fputc('\n', stdout
);
1160 link_status_one(rtnl
, hwdb
, links
+ i
);
1166 static char *lldp_capabilities_to_string(uint16_t x
) {
1167 static const char characters
[] = {
1168 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
1173 ret
= new(char, ELEMENTSOF(characters
) + 1);
1177 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
1178 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
1184 static void lldp_capabilities_legend(uint16_t x
) {
1185 unsigned w
, i
, cols
= columns();
1186 static const char* const table
[] = {
1190 "w - WLAN Access Point",
1193 "d - DOCSIS cable device",
1195 "c - Customer VLAN",
1197 "m - Two-port MAC Relay (TPMR)",
1203 printf("\nCapability Flags:\n");
1204 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
1205 if (x
& (1U << i
) || arg_all
) {
1208 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
1211 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
1216 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
1217 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1218 _cleanup_free_ LinkInfo
*links
= NULL
;
1219 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1224 r
= sd_netlink_open(&rtnl
);
1226 return log_error_errno(r
, "Failed to connect to netlink: %m");
1228 c
= acquire_link_info(rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
1232 (void) pager_open(arg_pager_flags
);
1234 table
= table_new("LINK",
1239 "PORT DESCRIPTION");
1243 table_set_header(table
, arg_legend
);
1245 assert_se(cell
= table_get_cell(table
, 0, 0));
1246 table_set_minimum_width(table
, cell
, 16);
1248 assert_se(cell
= table_get_cell(table
, 0, 1));
1249 table_set_minimum_width(table
, cell
, 17);
1251 assert_se(cell
= table_get_cell(table
, 0, 2));
1252 table_set_minimum_width(table
, cell
, 16);
1254 assert_se(cell
= table_get_cell(table
, 0, 3));
1255 table_set_minimum_width(table
, cell
, 11);
1257 assert_se(cell
= table_get_cell(table
, 0, 4));
1258 table_set_minimum_width(table
, cell
, 17);
1260 assert_se(cell
= table_get_cell(table
, 0, 5));
1261 table_set_minimum_width(table
, cell
, 16);
1263 for (i
= 0; i
< c
; i
++) {
1264 _cleanup_fclose_
FILE *f
= NULL
;
1266 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
1270 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
1275 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
, *capabilities
= NULL
;
1276 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
;
1277 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1280 r
= next_lldp_neighbor(f
, &n
);
1282 log_warning_errno(r
, "Failed to read neighbor data: %m");
1288 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
1289 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1290 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1291 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1294 cid
= ellipsize(chassis_id
, 17, 100);
1300 pid
= ellipsize(port_id
, 17, 100);
1306 sname
= ellipsize(system_name
, 16, 100);
1308 system_name
= sname
;
1311 if (port_description
) {
1312 pdesc
= ellipsize(port_description
, 16, 100);
1314 port_description
= pdesc
;
1317 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
1318 capabilities
= lldp_capabilities_to_string(cc
);
1322 r
= table_add_many(table
,
1323 TABLE_STRING
, links
[i
].name
,
1324 TABLE_STRING
, strna(chassis_id
),
1325 TABLE_STRING
, strna(system_name
),
1326 TABLE_STRING
, strna(capabilities
),
1327 TABLE_STRING
, strna(port_id
),
1328 TABLE_STRING
, strna(port_description
));
1336 r
= table_print(table
, NULL
);
1341 lldp_capabilities_legend(all
);
1342 printf("\n%i neighbors listed.\n", m
);
1348 static int link_delete_send_message(sd_netlink
*rtnl
, int index
) {
1349 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1354 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_DELLINK
, index
);
1356 return rtnl_log_create_error(r
);
1358 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
1365 static int link_delete(int argc
, char *argv
[], void *userdata
) {
1366 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1367 _cleanup_set_free_ Set
*indexes
= NULL
;
1371 r
= sd_netlink_open(&rtnl
);
1373 return log_error_errno(r
, "Failed to connect to netlink: %m");
1375 indexes
= set_new(NULL
);
1379 for (i
= 1; i
< argc
; i
++) {
1380 r
= parse_ifindex_or_ifname(argv
[i
], &index
);
1382 return log_error_errno(r
, "Failed to resolve interface %s", argv
[i
]);
1384 r
= set_put(indexes
, INT_TO_PTR(index
));
1389 SET_FOREACH(index
, indexes
, j
) {
1390 r
= link_delete_send_message(rtnl
, index
);
1392 char ifname
[IF_NAMESIZE
+ 1];
1394 if (format_ifname(index
, ifname
))
1395 return log_error_errno(r
, "Failed to delete interface %s: %m", ifname
);
1397 return log_error_errno(r
, "Failed to delete interface %d: %m", index
);
1404 static int help(void) {
1405 _cleanup_free_
char *link
= NULL
;
1408 r
= terminal_urlify_man("networkctl", "1", &link
);
1412 printf("%s [OPTIONS...]\n\n"
1413 "Query and control the networking subsystem.\n\n"
1414 " -h --help Show this help\n"
1415 " --version Show package version\n"
1416 " --no-pager Do not pipe output into a pager\n"
1417 " --no-legend Do not show the headers and footers\n"
1418 " -a --all Show status for all links\n\n"
1420 " list [PATTERN...] List links\n"
1421 " status [PATTERN...] Show link status\n"
1422 " lldp [PATTERN...] Show LLDP neighbors\n"
1423 " label Show current address label entries in the kernel\n"
1424 " delete DEVICES Delete virtual netdevs\n"
1425 "\nSee the %s for details.\n"
1426 , program_invocation_short_name
1433 static int parse_argv(int argc
, char *argv
[]) {
1436 ARG_VERSION
= 0x100,
1441 static const struct option options
[] = {
1442 { "help", no_argument
, NULL
, 'h' },
1443 { "version", no_argument
, NULL
, ARG_VERSION
},
1444 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1445 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1446 { "all", no_argument
, NULL
, 'a' },
1455 while ((c
= getopt_long(argc
, argv
, "ha", options
, NULL
)) >= 0) {
1466 arg_pager_flags
|= PAGER_DISABLE
;
1481 assert_not_reached("Unhandled option");
1488 static int networkctl_main(int argc
, char *argv
[]) {
1489 static const Verb verbs
[] = {
1490 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
1491 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
1492 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
1493 { "label", VERB_ANY
, VERB_ANY
, 0, list_address_labels
},
1494 { "delete", 2, VERB_ANY
, 0, link_delete
},
1498 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1501 static void warn_networkd_missing(void) {
1503 if (access("/run/systemd/netif/state", F_OK
) >= 0)
1506 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1509 static int run(int argc
, char* argv
[]) {
1512 log_show_color(true);
1513 log_parse_environment();
1516 r
= parse_argv(argc
, argv
);
1520 warn_networkd_missing();
1522 return networkctl_main(argc
, argv
);
1525 DEFINE_MAIN_FUNCTION(run
);