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 "hwdb-util.h"
24 #include "local-addresses.h"
25 #include "locale-util.h"
27 #include "main-func.h"
28 #include "netlink-util.h"
30 #include "parse-util.h"
31 #include "pretty-print.h"
33 #include "socket-util.h"
34 #include "sort-util.h"
35 #include "sparse-endian.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "string-util.h"
41 #include "terminal-util.h"
44 static PagerFlags arg_pager_flags
= 0;
45 static bool arg_legend
= true;
46 static bool arg_all
= false;
48 static char *link_get_type_string(unsigned short iftype
, sd_device
*d
) {
49 const char *t
, *devtype
;
53 sd_device_get_devtype(d
, &devtype
) >= 0 &&
55 return strdup(devtype
);
57 t
= arphrd_to_name(iftype
);
69 static void operational_state_to_color(const char *state
, const char **on
, const char **off
) {
73 if (STRPTR_IN_SET(state
, "routable", "enslaved")) {
74 *on
= ansi_highlight_green();
76 } else if (streq_ptr(state
, "degraded")) {
77 *on
= ansi_highlight_yellow();
83 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
87 if (streq_ptr(state
, "configured")) {
88 *on
= ansi_highlight_green();
90 } else if (streq_ptr(state
, "configuring")) {
91 *on
= ansi_highlight_yellow();
93 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
94 *on
= ansi_highlight_red();
100 typedef struct LinkInfo
{
101 char name
[IFNAMSIZ
+1];
103 unsigned short iftype
;
104 struct ether_addr mac_address
;
111 bool has_mac_address
:1;
115 bool has_tx_queues
:1;
116 bool has_rx_queues
:1;
119 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
120 return CMP(a
->ifindex
, b
->ifindex
);
123 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
, char **patterns
) {
131 r
= sd_netlink_message_get_type(m
, &type
);
135 if (type
!= RTM_NEWLINK
)
138 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
142 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
147 char str
[DECIMAL_STR_MAX(int)];
149 xsprintf(str
, "%i", ifindex
);
151 if (!strv_fnmatch(patterns
, str
, 0) && !strv_fnmatch(patterns
, name
, 0))
155 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
159 strscpy(info
->name
, sizeof info
->name
, name
);
160 info
->ifindex
= ifindex
;
162 info
->has_mac_address
=
163 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
164 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
167 sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
) >= 0 &&
171 sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
) >= 0 &&
175 sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
) >= 0 &&
178 info
->has_rx_queues
=
179 sd_netlink_message_read_u32(m
, IFLA_NUM_RX_QUEUES
, &info
->rx_queues
) >= 0 &&
182 info
->has_tx_queues
=
183 sd_netlink_message_read_u32(m
, IFLA_NUM_TX_QUEUES
, &info
->tx_queues
) >= 0 &&
189 static int acquire_link_info(sd_netlink
*rtnl
, char **patterns
, LinkInfo
**ret
) {
190 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
191 _cleanup_free_ LinkInfo
*links
= NULL
;
192 size_t allocated
= 0, c
= 0;
193 sd_netlink_message
*i
;
199 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
201 return rtnl_log_create_error(r
);
203 r
= sd_netlink_message_request_dump(req
, true);
205 return rtnl_log_create_error(r
);
207 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
209 return log_error_errno(r
, "Failed to enumerate links: %m");
211 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
212 if (!GREEDY_REALLOC(links
, allocated
, c
+1))
215 r
= decode_link(i
, links
+ c
, patterns
);
222 typesafe_qsort(links
, c
, link_info_compare
);
224 *ret
= TAKE_PTR(links
);
229 static int list_links(int argc
, char *argv
[], void *userdata
) {
230 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
231 _cleanup_free_ LinkInfo
*links
= NULL
;
232 _cleanup_(table_unrefp
) Table
*table
= NULL
;
236 r
= sd_netlink_open(&rtnl
);
238 return log_error_errno(r
, "Failed to connect to netlink: %m");
240 c
= acquire_link_info(rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
244 (void) pager_open(arg_pager_flags
);
246 table
= table_new("IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");
250 table_set_header(table
, arg_legend
);
252 assert_se(cell
= table_get_cell(table
, 0, 0));
253 (void) table_set_minimum_width(table
, cell
, 3);
254 (void) table_set_weight(table
, cell
, 0);
255 (void) table_set_ellipsize_percent(table
, cell
, 0);
256 (void) table_set_align_percent(table
, cell
, 100);
258 assert_se(cell
= table_get_cell(table
, 0, 1));
259 (void) table_set_minimum_width(table
, cell
, 16);
261 assert_se(cell
= table_get_cell(table
, 0, 2));
262 (void) table_set_minimum_width(table
, cell
, 18);
264 assert_se(cell
= table_get_cell(table
, 0, 3));
265 (void) table_set_minimum_width(table
, cell
, 16);
267 assert_se(cell
= table_get_cell(table
, 0, 4));
268 (void) table_set_minimum_width(table
, cell
, 10);
270 for (i
= 0; i
< c
; i
++) {
271 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
272 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
273 const char *on_color_operational
, *off_color_operational
,
274 *on_color_setup
, *off_color_setup
;
275 char devid
[2 + DECIMAL_STR_MAX(int)];
276 _cleanup_free_
char *t
= NULL
;
278 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
279 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
281 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
282 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
283 setup_state
= strdup("unmanaged");
284 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
286 xsprintf(devid
, "n%i", links
[i
].ifindex
);
287 (void) sd_device_new_from_device_id(&d
, devid
);
289 t
= link_get_type_string(links
[i
].iftype
, d
);
291 r
= table_add_cell_full(table
, NULL
, TABLE_INT
, &links
[i
].ifindex
, SIZE_MAX
, SIZE_MAX
, 0, 100, 0);
295 r
= table_add_many(table
,
296 TABLE_STRING
, links
[i
].name
,
297 TABLE_STRING
, strna(t
));
301 r
= table_add_cell(table
, &cell
, TABLE_STRING
, strna(operational_state
));
305 (void) table_set_color(table
, cell
, on_color_operational
);
307 r
= table_add_cell(table
, &cell
, TABLE_STRING
, strna(setup_state
));
311 (void) table_set_color(table
, cell
, on_color_setup
);
314 r
= table_print(table
, NULL
);
316 return log_error_errno(r
, "Failed to print table: %m");
319 printf("\n%i links listed.\n", c
);
324 /* IEEE Organizationally Unique Identifier vendor string */
325 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
326 const char *description
;
327 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
338 /* skip commonly misused 00:00:00 (Xerox) prefix */
339 if (memcmp(mac
, "\0\0\0", 3) == 0)
342 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
343 ETHER_ADDR_FORMAT_VAL(*mac
));
345 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
349 desc
= strdup(description
);
358 static int get_gateway_description(
363 union in_addr_union
*gateway
,
364 char **gateway_description
) {
365 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
366 sd_netlink_message
*m
;
370 assert(ifindex
>= 0);
371 assert(IN_SET(family
, AF_INET
, AF_INET6
));
373 assert(gateway_description
);
375 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
379 r
= sd_netlink_message_request_dump(req
, true);
383 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
387 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
388 union in_addr_union gw
= IN_ADDR_NULL
;
389 struct ether_addr mac
= ETHER_ADDR_NULL
;
393 r
= sd_netlink_message_get_errno(m
);
395 log_error_errno(r
, "got error: %m");
399 r
= sd_netlink_message_get_type(m
, &type
);
401 log_error_errno(r
, "could not get type: %m");
405 if (type
!= RTM_NEWNEIGH
) {
406 log_error("type is not RTM_NEWNEIGH");
410 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
412 log_error_errno(r
, "could not get family: %m");
417 log_error("family is not correct");
421 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
423 log_error_errno(r
, "could not get ifindex: %m");
427 if (ifindex
> 0 && ifi
!= ifindex
)
432 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
438 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
447 if (!in_addr_equal(fam
, &gw
, gateway
))
450 r
= sd_netlink_message_read_ether_addr(m
, NDA_LLADDR
, &mac
);
454 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
464 static int dump_gateways(
469 _cleanup_free_
struct local_address
*local
= NULL
;
475 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
479 for (i
= 0; i
< n
; i
++) {
480 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
;
482 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
486 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
488 log_debug_errno(r
, "Could not get description of gateway: %m");
491 (int) strlen(prefix
),
492 i
== 0 ? prefix
: "",
496 printf(" (%s)", description
);
498 /* Show interface name for the entry if we show
499 * entries for all interfaces */
501 char name
[IF_NAMESIZE
+1];
503 if (if_indextoname(local
[i
].ifindex
, name
)) {
504 fputs(" on ", stdout
);
507 printf(" on %%%i", local
[i
].ifindex
);
516 static int dump_addresses(
521 _cleanup_free_
struct local_address
*local
= NULL
;
527 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
531 for (i
= 0; i
< n
; i
++) {
532 _cleanup_free_
char *pretty
= NULL
;
534 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
539 (int) strlen(prefix
),
540 i
== 0 ? prefix
: "",
544 char name
[IF_NAMESIZE
+1];
546 if (if_indextoname(local
[i
].ifindex
, name
)) {
547 fputs(" on ", stdout
);
550 printf(" on %%%i", local
[i
].ifindex
);
559 static int dump_address_labels(sd_netlink
*rtnl
) {
560 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
561 sd_netlink_message
*m
;
566 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
568 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
570 r
= sd_netlink_message_request_dump(req
, true);
574 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
578 printf("%10s/%s %30s\n", "Prefix", "Prefixlen", "Label");
580 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
581 _cleanup_free_
char *pretty
= NULL
;
582 union in_addr_union prefix
= IN_ADDR_NULL
;
586 r
= sd_netlink_message_get_errno(m
);
588 log_error_errno(r
, "got error: %m");
592 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
593 if (r
< 0 && r
!= -ENODATA
) {
594 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
598 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
602 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
606 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
610 printf("%10s/%-5u %30u\n", pretty
, prefixlen
, label
);
616 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
617 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
620 r
= sd_netlink_open(&rtnl
);
622 return log_error_errno(r
, "Failed to connect to netlink: %m");
624 dump_address_labels(rtnl
);
629 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
630 _cleanup_free_
char *p
= NULL
;
633 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
644 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
645 _cleanup_free_
void *raw
= NULL
;
653 l
= fread(&u
, 1, sizeof(u
), f
);
654 if (l
== 0 && feof(f
))
659 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
660 if (le64toh(u
) >= 4096)
663 raw
= new(uint8_t, le64toh(u
));
667 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
670 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
677 static int dump_lldp_neighbors(const char *prefix
, int ifindex
) {
678 _cleanup_fclose_
FILE *f
= NULL
;
684 r
= open_lldp_neighbors(ifindex
, &f
);
689 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
690 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
692 r
= next_lldp_neighbor(f
, &n
);
699 (int) strlen(prefix
),
700 c
== 0 ? prefix
: "");
702 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
703 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
704 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
706 printf("%s on port %s", strna(system_name
), strna(port_id
));
708 if (!isempty(port_description
))
709 printf(" (%s)", port_description
);
719 static void dump_ifindexes(const char *prefix
, const int *ifindexes
) {
724 if (!ifindexes
|| ifindexes
[0] <= 0)
727 for (c
= 0; ifindexes
[c
] > 0; c
++) {
728 char name
[IF_NAMESIZE
+1];
731 (int) strlen(prefix
),
732 c
== 0 ? prefix
: "");
734 if (if_indextoname(ifindexes
[c
], name
))
737 printf("%i", ifindexes
[c
]);
743 static void dump_list(const char *prefix
, char **l
) {
751 (int) strlen(prefix
),
752 i
== l
? prefix
: "",
757 static int link_status_one(
760 const LinkInfo
*info
) {
762 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
763 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
, *tz
= NULL
;
764 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
765 char devid
[2 + DECIMAL_STR_MAX(int)];
766 _cleanup_free_
char *t
= NULL
, *network
= NULL
;
767 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
;
768 const char *on_color_operational
, *off_color_operational
,
769 *on_color_setup
, *off_color_setup
;
770 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
776 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
777 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
779 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
780 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
781 setup_state
= strdup("unmanaged");
782 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
784 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
785 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
786 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
787 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
789 xsprintf(devid
, "n%i", info
->ifindex
);
791 (void) sd_device_new_from_device_id(&d
, devid
);
794 (void) sd_device_get_property_value(d
, "ID_NET_LINK_FILE", &link
);
795 (void) sd_device_get_property_value(d
, "ID_NET_DRIVER", &driver
);
796 (void) sd_device_get_property_value(d
, "ID_PATH", &path
);
798 if (sd_device_get_property_value(d
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
799 (void) sd_device_get_property_value(d
, "ID_VENDOR", &vendor
);
801 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
802 (void) sd_device_get_property_value(d
, "ID_MODEL", &model
);
805 t
= link_get_type_string(info
->iftype
, d
);
807 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
809 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
810 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
812 printf("%s%s%s %i: %s\n", on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
, info
->ifindex
, info
->name
);
814 printf(" Link File: %s\n"
815 " Network File: %s\n"
817 " State: %s%s%s (%s%s%s)\n",
821 on_color_operational
, strna(operational_state
), off_color_operational
,
822 on_color_setup
, strna(setup_state
), off_color_setup
);
825 printf(" Path: %s\n", path
);
827 printf(" Driver: %s\n", driver
);
829 printf(" Vendor: %s\n", vendor
);
831 printf(" Model: %s\n", model
);
833 if (info
->has_mac_address
) {
834 _cleanup_free_
char *description
= NULL
;
835 char ea
[ETHER_ADDR_TO_STRING_MAX
];
837 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
840 printf(" HW Address: %s (%s)\n", ether_addr_to_string(&info
->mac_address
, ea
), description
);
842 printf(" HW Address: %s\n", ether_addr_to_string(&info
->mac_address
, ea
));
846 printf(" MTU: %" PRIu32
"\n", info
->mtu
);
847 if (info
->has_min_mtu
)
848 printf(" Minimum MTU: %" PRIu32
"\n", info
->min_mtu
);
849 if (info
->has_max_mtu
)
850 printf(" Maximum MTU: %" PRIu32
"\n", info
->max_mtu
);
852 if (info
->has_tx_queues
)
853 printf("Transmit Queue Length: %" PRIu32
"\n", info
->tx_queues
);
854 if (info
->has_rx_queues
)
855 printf(" Receive Queue Length: %" PRIu32
"\n", info
->rx_queues
);
857 (void) dump_addresses(rtnl
, " Address: ", info
->ifindex
);
858 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", info
->ifindex
);
860 dump_list(" DNS: ", dns
);
861 dump_list(" Search Domains: ", search_domains
);
862 dump_list(" Route Domains: ", route_domains
);
864 dump_list(" NTP: ", ntp
);
866 dump_ifindexes("Carrier Bound To: ", carrier_bound_to
);
867 dump_ifindexes("Carrier Bound By: ", carrier_bound_by
);
869 (void) sd_network_link_get_timezone(info
->ifindex
, &tz
);
871 printf(" Time Zone: %s\n", tz
);
873 (void) dump_lldp_neighbors(" Connected To: ", info
->ifindex
);
878 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
879 _cleanup_free_
char *operational_state
= NULL
;
880 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
881 const char *on_color_operational
, *off_color_operational
;
885 (void) sd_network_get_operational_state(&operational_state
);
886 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
888 printf("%s%s%s State: %s%s%s\n",
889 on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
,
890 on_color_operational
, strna(operational_state
), off_color_operational
);
892 (void) dump_addresses(rtnl
, " Address: ", 0);
893 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", 0);
895 (void) sd_network_get_dns(&dns
);
896 dump_list(" DNS: ", dns
);
898 (void) sd_network_get_search_domains(&search_domains
);
899 dump_list("Search Domains: ", search_domains
);
901 (void) sd_network_get_route_domains(&route_domains
);
902 dump_list(" Route Domains: ", route_domains
);
904 (void) sd_network_get_ntp(&ntp
);
905 dump_list(" NTP: ", ntp
);
910 static int link_status(int argc
, char *argv
[], void *userdata
) {
911 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
912 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
913 _cleanup_free_ LinkInfo
*links
= NULL
;
916 (void) pager_open(arg_pager_flags
);
918 r
= sd_netlink_open(&rtnl
);
920 return log_error_errno(r
, "Failed to connect to netlink: %m");
922 r
= sd_hwdb_new(&hwdb
);
924 log_debug_errno(r
, "Failed to open hardware database: %m");
927 c
= acquire_link_info(rtnl
, NULL
, &links
);
929 return system_status(rtnl
, hwdb
);
931 c
= acquire_link_info(rtnl
, argv
+ 1, &links
);
935 for (i
= 0; i
< c
; i
++) {
939 link_status_one(rtnl
, hwdb
, links
+ i
);
945 static char *lldp_capabilities_to_string(uint16_t x
) {
946 static const char characters
[] = {
947 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
952 ret
= new(char, ELEMENTSOF(characters
) + 1);
956 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
957 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
963 static void lldp_capabilities_legend(uint16_t x
) {
964 unsigned w
, i
, cols
= columns();
965 static const char* const table
[] = {
969 "w - WLAN Access Point",
972 "d - DOCSIS cable device",
976 "m - Two-port MAC Relay (TPMR)",
982 printf("\nCapability Flags:\n");
983 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
984 if (x
& (1U << i
) || arg_all
) {
987 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
990 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
995 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
996 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
997 _cleanup_free_ LinkInfo
*links
= NULL
;
1001 r
= sd_netlink_open(&rtnl
);
1003 return log_error_errno(r
, "Failed to connect to netlink: %m");
1005 c
= acquire_link_info(rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
1009 (void) pager_open(arg_pager_flags
);
1012 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
1018 "PORT DESCRIPTION");
1020 for (i
= 0; i
< c
; i
++) {
1021 _cleanup_fclose_
FILE *f
= NULL
;
1023 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
1027 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
1032 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
;
1033 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
, *capabilities
= NULL
;
1034 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1037 r
= next_lldp_neighbor(f
, &n
);
1039 log_warning_errno(r
, "Failed to read neighbor data: %m");
1045 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
1046 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1047 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1048 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1051 cid
= ellipsize(chassis_id
, 17, 100);
1057 pid
= ellipsize(port_id
, 17, 100);
1063 sname
= ellipsize(system_name
, 16, 100);
1065 system_name
= sname
;
1068 if (port_description
) {
1069 pdesc
= ellipsize(port_description
, 16, 100);
1071 port_description
= pdesc
;
1074 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
1075 capabilities
= lldp_capabilities_to_string(cc
);
1079 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
1083 strna(capabilities
),
1085 strna(port_description
));
1092 lldp_capabilities_legend(all
);
1093 printf("\n%i neighbors listed.\n", m
);
1099 static int link_delete_send_message(sd_netlink
*rtnl
, int index
) {
1100 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1105 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_DELLINK
, index
);
1107 return rtnl_log_create_error(r
);
1109 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
1116 static int link_delete(int argc
, char *argv
[], void *userdata
) {
1117 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1118 _cleanup_set_free_ Set
*indexes
= NULL
;
1119 char ifname
[IFNAMSIZ
] = "";
1123 r
= sd_netlink_open(&rtnl
);
1125 return log_error_errno(r
, "Failed to connect to netlink: %m");
1127 indexes
= set_new(NULL
);
1131 for (i
= 1; i
< argc
; i
++) {
1132 r
= parse_ifindex_or_ifname(argv
[i
], &index
);
1134 return log_error_errno(r
, "Failed to resolve interface %s", argv
[i
]);
1136 r
= set_put(indexes
, INT_TO_PTR(index
));
1141 SET_FOREACH(index
, indexes
, j
) {
1142 r
= link_delete_send_message(rtnl
, index
);
1144 if (if_indextoname(index
, ifname
))
1145 return log_error_errno(r
, "Failed to delete interface %s: %m", ifname
);
1147 return log_error_errno(r
, "Failed to delete interface %d: %m", index
);
1154 static int help(void) {
1155 _cleanup_free_
char *link
= NULL
;
1158 r
= terminal_urlify_man("networkctl", "1", &link
);
1162 printf("%s [OPTIONS...]\n\n"
1163 "Query and control the networking subsystem.\n\n"
1164 " -h --help Show this help\n"
1165 " --version Show package version\n"
1166 " --no-pager Do not pipe output into a pager\n"
1167 " --no-legend Do not show the headers and footers\n"
1168 " -a --all Show status for all links\n\n"
1170 " list [PATTERN...] List links\n"
1171 " status [PATTERN...] Show link status\n"
1172 " lldp [PATTERN...] Show LLDP neighbors\n"
1173 " label Show current address label entries in the kernel\n"
1174 " delete DEVICES Delete virtual netdevs\n"
1175 "\nSee the %s for details.\n"
1176 , program_invocation_short_name
1183 static int parse_argv(int argc
, char *argv
[]) {
1186 ARG_VERSION
= 0x100,
1191 static const struct option options
[] = {
1192 { "help", no_argument
, NULL
, 'h' },
1193 { "version", no_argument
, NULL
, ARG_VERSION
},
1194 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1195 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1196 { "all", no_argument
, NULL
, 'a' },
1205 while ((c
= getopt_long(argc
, argv
, "ha", options
, NULL
)) >= 0) {
1216 arg_pager_flags
|= PAGER_DISABLE
;
1231 assert_not_reached("Unhandled option");
1238 static int networkctl_main(int argc
, char *argv
[]) {
1239 static const Verb verbs
[] = {
1240 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
1241 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
1242 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
1243 { "label", VERB_ANY
, VERB_ANY
, 0, list_address_labels
},
1244 { "delete", 2, VERB_ANY
, 0, link_delete
},
1248 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1251 static void warn_networkd_missing(void) {
1253 if (access("/run/systemd/netif/state", F_OK
) >= 0)
1256 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1259 static int run(int argc
, char* argv
[]) {
1262 log_show_color(true);
1263 log_parse_environment();
1266 r
= parse_argv(argc
, argv
);
1270 warn_networkd_missing();
1272 return networkctl_main(argc
, argv
);
1275 DEFINE_MAIN_FUNCTION(run
);