1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/if_addrlabel.h>
11 #include "sd-netlink.h"
12 #include "sd-network.h"
14 #include "alloc-util.h"
15 #include "arphrd-list.h"
16 #include "device-util.h"
17 #include "ether-addr-util.h"
19 #include "hwdb-util.h"
20 #include "local-addresses.h"
21 #include "locale-util.h"
23 #include "main-func.h"
24 #include "netlink-util.h"
26 #include "parse-util.h"
27 #include "pretty-print.h"
28 #include "socket-util.h"
29 #include "sparse-endian.h"
30 #include "stdio-util.h"
31 #include "string-table.h"
32 #include "string-util.h"
35 #include "terminal-util.h"
39 static PagerFlags arg_pager_flags
= 0;
40 static bool arg_legend
= true;
41 static bool arg_all
= false;
43 static char *link_get_type_string(unsigned short iftype
, sd_device
*d
) {
44 const char *t
, *devtype
;
48 sd_device_get_devtype(d
, &devtype
) >= 0 &&
50 return strdup(devtype
);
52 t
= arphrd_to_name(iftype
);
64 static void operational_state_to_color(const char *state
, const char **on
, const char **off
) {
68 if (streq_ptr(state
, "routable")) {
69 *on
= ansi_highlight_green();
71 } else if (streq_ptr(state
, "degraded")) {
72 *on
= ansi_highlight_yellow();
78 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
82 if (streq_ptr(state
, "configured")) {
83 *on
= ansi_highlight_green();
85 } else if (streq_ptr(state
, "configuring")) {
86 *on
= ansi_highlight_yellow();
88 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
89 *on
= ansi_highlight_red();
95 typedef struct LinkInfo
{
96 char name
[IFNAMSIZ
+1];
98 unsigned short iftype
;
99 struct ether_addr mac_address
;
102 bool has_mac_address
:1;
106 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
107 return CMP(a
->ifindex
, b
->ifindex
);
110 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
) {
118 r
= sd_netlink_message_get_type(m
, &type
);
122 if (type
!= RTM_NEWLINK
)
125 r
= sd_rtnl_message_link_get_ifindex(m
, &info
->ifindex
);
129 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
133 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
137 strscpy(info
->name
, sizeof info
->name
, name
);
139 info
->has_mac_address
=
140 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
141 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
144 sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
) &&
150 static int acquire_link_info_strv(sd_netlink
*rtnl
, char **l
, LinkInfo
**ret
) {
151 _cleanup_free_ LinkInfo
*links
= NULL
;
159 links
= new(LinkInfo
, strv_length(l
));
164 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
167 if (parse_ifindex(*i
, &ifindex
) >= 0)
168 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, ifindex
);
170 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
172 return rtnl_log_create_error(r
);
174 r
= sd_netlink_message_append_string(req
, IFLA_IFNAME
, *i
);
177 return rtnl_log_create_error(r
);
179 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
181 return log_error_errno(r
, "Failed to request link: %m");
183 r
= decode_link(reply
, links
+ c
);
190 typesafe_qsort(links
, c
, link_info_compare
);
192 *ret
= TAKE_PTR(links
);
197 static int acquire_link_info_all(sd_netlink
*rtnl
, LinkInfo
**ret
) {
198 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
199 _cleanup_free_ LinkInfo
*links
= NULL
;
200 size_t allocated
= 0, c
= 0;
201 sd_netlink_message
*i
;
207 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
209 return rtnl_log_create_error(r
);
211 r
= sd_netlink_message_request_dump(req
, true);
213 return rtnl_log_create_error(r
);
215 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
217 return log_error_errno(r
, "Failed to enumerate links: %m");
219 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
220 if (!GREEDY_REALLOC(links
, allocated
, c
+1))
223 r
= decode_link(i
, links
+ c
);
230 typesafe_qsort(links
, c
, link_info_compare
);
232 *ret
= TAKE_PTR(links
);
237 static int list_links(int argc
, char *argv
[], void *userdata
) {
238 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
239 _cleanup_free_ LinkInfo
*links
= NULL
;
242 r
= sd_netlink_open(&rtnl
);
244 return log_error_errno(r
, "Failed to connect to netlink: %m");
247 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
249 c
= acquire_link_info_all(rtnl
, &links
);
253 (void) pager_open(arg_pager_flags
);
256 printf("%3s %-16s %-18s %-11s %-10s\n",
263 for (i
= 0; i
< c
; i
++) {
264 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
265 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
266 const char *on_color_operational
, *off_color_operational
,
267 *on_color_setup
, *off_color_setup
;
268 char devid
[2 + DECIMAL_STR_MAX(int)];
269 _cleanup_free_
char *t
= NULL
;
271 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
272 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
274 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
275 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
276 setup_state
= strdup("unmanaged");
277 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
279 xsprintf(devid
, "n%i", links
[i
].ifindex
);
280 (void) sd_device_new_from_device_id(&d
, devid
);
282 t
= link_get_type_string(links
[i
].iftype
, d
);
284 printf("%3i %-16s %-18s %s%-11s%s %s%-10s%s\n",
285 links
[i
].ifindex
, links
[i
].name
, strna(t
),
286 on_color_operational
, strna(operational_state
), off_color_operational
,
287 on_color_setup
, strna(setup_state
), off_color_setup
);
291 printf("\n%i links listed.\n", c
);
296 /* IEEE Organizationally Unique Identifier vendor string */
297 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
298 const char *description
;
299 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
310 /* skip commonly misused 00:00:00 (Xerox) prefix */
311 if (memcmp(mac
, "\0\0\0", 3) == 0)
314 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
315 ETHER_ADDR_FORMAT_VAL(*mac
));
317 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
321 desc
= strdup(description
);
330 static int get_gateway_description(
335 union in_addr_union
*gateway
,
336 char **gateway_description
) {
337 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
338 sd_netlink_message
*m
;
342 assert(ifindex
>= 0);
343 assert(IN_SET(family
, AF_INET
, AF_INET6
));
345 assert(gateway_description
);
347 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
351 r
= sd_netlink_message_request_dump(req
, true);
355 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
359 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
360 union in_addr_union gw
= {};
361 struct ether_addr mac
= {};
365 r
= sd_netlink_message_get_errno(m
);
367 log_error_errno(r
, "got error: %m");
371 r
= sd_netlink_message_get_type(m
, &type
);
373 log_error_errno(r
, "could not get type: %m");
377 if (type
!= RTM_NEWNEIGH
) {
378 log_error("type is not RTM_NEWNEIGH");
382 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
384 log_error_errno(r
, "could not get family: %m");
389 log_error("family is not correct");
393 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
395 log_error_errno(r
, "could not get ifindex: %m");
399 if (ifindex
> 0 && ifi
!= ifindex
)
404 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
410 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
419 if (!in_addr_equal(fam
, &gw
, gateway
))
422 r
= sd_netlink_message_read_ether_addr(m
, NDA_LLADDR
, &mac
);
426 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
436 static int dump_gateways(
441 _cleanup_free_
struct local_address
*local
= NULL
;
447 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
451 for (i
= 0; i
< n
; i
++) {
452 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
;
454 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
458 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
460 log_debug_errno(r
, "Could not get description of gateway: %m");
463 (int) strlen(prefix
),
464 i
== 0 ? prefix
: "",
468 printf(" (%s)", description
);
470 /* Show interface name for the entry if we show
471 * entries for all interfaces */
473 char name
[IF_NAMESIZE
+1];
475 if (if_indextoname(local
[i
].ifindex
, name
)) {
476 fputs(" on ", stdout
);
479 printf(" on %%%i", local
[i
].ifindex
);
488 static int dump_addresses(
493 _cleanup_free_
struct local_address
*local
= NULL
;
499 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
503 for (i
= 0; i
< n
; i
++) {
504 _cleanup_free_
char *pretty
= NULL
;
506 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
511 (int) strlen(prefix
),
512 i
== 0 ? prefix
: "",
516 char name
[IF_NAMESIZE
+1];
518 if (if_indextoname(local
[i
].ifindex
, name
)) {
519 fputs(" on ", stdout
);
522 printf(" on %%%i", local
[i
].ifindex
);
531 static int dump_address_labels(sd_netlink
*rtnl
) {
532 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
533 sd_netlink_message
*m
;
538 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
540 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
542 r
= sd_netlink_message_request_dump(req
, true);
546 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
550 printf("%10s/%s %30s\n", "Prefix", "Prefixlen", "Label");
552 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
553 _cleanup_free_
char *pretty
= NULL
;
554 union in_addr_union prefix
= {};
558 r
= sd_netlink_message_get_errno(m
);
560 log_error_errno(r
, "got error: %m");
564 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
565 if (r
< 0 && r
!= -ENODATA
) {
566 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
570 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
574 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
578 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
582 printf("%10s/%-5u %30u\n", pretty
, prefixlen
, label
);
588 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
589 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
592 r
= sd_netlink_open(&rtnl
);
594 return log_error_errno(r
, "Failed to connect to netlink: %m");
596 dump_address_labels(rtnl
);
601 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
602 _cleanup_free_
char *p
= NULL
;
605 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
616 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
617 _cleanup_free_
void *raw
= NULL
;
625 l
= fread(&u
, 1, sizeof(u
), f
);
626 if (l
== 0 && feof(f
))
631 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
632 if (le64toh(u
) >= 4096)
635 raw
= new(uint8_t, le64toh(u
));
639 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
642 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
649 static int dump_lldp_neighbors(const char *prefix
, int ifindex
) {
650 _cleanup_fclose_
FILE *f
= NULL
;
656 r
= open_lldp_neighbors(ifindex
, &f
);
661 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
662 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
664 r
= next_lldp_neighbor(f
, &n
);
671 (int) strlen(prefix
),
672 c
== 0 ? prefix
: "");
674 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
675 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
676 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
678 printf("%s on port %s", strna(system_name
), strna(port_id
));
680 if (!isempty(port_description
))
681 printf(" (%s)", port_description
);
691 static void dump_ifindexes(const char *prefix
, const int *ifindexes
) {
696 if (!ifindexes
|| ifindexes
[0] <= 0)
699 for (c
= 0; ifindexes
[c
] > 0; c
++) {
700 char name
[IF_NAMESIZE
+1];
703 (int) strlen(prefix
),
704 c
== 0 ? prefix
: "");
706 if (if_indextoname(ifindexes
[c
], name
))
709 printf("%i", ifindexes
[c
]);
715 static void dump_list(const char *prefix
, char **l
) {
723 (int) strlen(prefix
),
724 i
== l
? prefix
: "",
729 static int link_status_one(
732 const LinkInfo
*info
) {
734 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
735 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
, *tz
= NULL
;
736 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
737 char devid
[2 + DECIMAL_STR_MAX(int)];
738 _cleanup_free_
char *t
= NULL
, *network
= NULL
;
739 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
;
740 const char *on_color_operational
, *off_color_operational
,
741 *on_color_setup
, *off_color_setup
;
742 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
748 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
749 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
751 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
752 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
753 setup_state
= strdup("unmanaged");
754 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
756 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
757 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
758 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
759 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
761 xsprintf(devid
, "n%i", info
->ifindex
);
763 (void) sd_device_new_from_device_id(&d
, devid
);
766 (void) sd_device_get_property_value(d
, "ID_NET_LINK_FILE", &link
);
767 (void) sd_device_get_property_value(d
, "ID_NET_DRIVER", &driver
);
768 (void) sd_device_get_property_value(d
, "ID_PATH", &path
);
770 if (sd_device_get_property_value(d
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
771 (void) sd_device_get_property_value(d
, "ID_VENDOR", &vendor
);
773 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
774 (void) sd_device_get_property_value(d
, "ID_MODEL", &model
);
777 t
= link_get_type_string(info
->iftype
, d
);
779 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
781 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
782 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
784 printf("%s%s%s %i: %s\n", on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
, info
->ifindex
, info
->name
);
786 printf(" Link File: %s\n"
787 " Network File: %s\n"
789 " State: %s%s%s (%s%s%s)\n",
793 on_color_operational
, strna(operational_state
), off_color_operational
,
794 on_color_setup
, strna(setup_state
), off_color_setup
);
797 printf(" Path: %s\n", path
);
799 printf(" Driver: %s\n", driver
);
801 printf(" Vendor: %s\n", vendor
);
803 printf(" Model: %s\n", model
);
805 if (info
->has_mac_address
) {
806 _cleanup_free_
char *description
= NULL
;
807 char ea
[ETHER_ADDR_TO_STRING_MAX
];
809 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
812 printf(" HW Address: %s (%s)\n", ether_addr_to_string(&info
->mac_address
, ea
), description
);
814 printf(" HW Address: %s\n", ether_addr_to_string(&info
->mac_address
, ea
));
818 printf(" MTU: %" PRIu32
"\n", info
->mtu
);
820 (void) dump_addresses(rtnl
, " Address: ", info
->ifindex
);
821 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", info
->ifindex
);
823 dump_list(" DNS: ", dns
);
824 dump_list(" Search Domains: ", search_domains
);
825 dump_list(" Route Domains: ", route_domains
);
827 dump_list(" NTP: ", ntp
);
829 dump_ifindexes("Carrier Bound To: ", carrier_bound_to
);
830 dump_ifindexes("Carrier Bound By: ", carrier_bound_by
);
832 (void) sd_network_link_get_timezone(info
->ifindex
, &tz
);
834 printf(" Time Zone: %s\n", tz
);
836 (void) dump_lldp_neighbors(" Connected To: ", info
->ifindex
);
841 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
842 _cleanup_free_
char *operational_state
= NULL
;
843 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
844 const char *on_color_operational
, *off_color_operational
;
848 (void) sd_network_get_operational_state(&operational_state
);
849 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
851 printf("%s%s%s State: %s%s%s\n",
852 on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
,
853 on_color_operational
, strna(operational_state
), off_color_operational
);
855 (void) dump_addresses(rtnl
, " Address: ", 0);
856 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", 0);
858 (void) sd_network_get_dns(&dns
);
859 dump_list(" DNS: ", dns
);
861 (void) sd_network_get_search_domains(&search_domains
);
862 dump_list("Search Domains: ", search_domains
);
864 (void) sd_network_get_route_domains(&route_domains
);
865 dump_list(" Route Domains: ", route_domains
);
867 (void) sd_network_get_ntp(&ntp
);
868 dump_list(" NTP: ", ntp
);
873 static int link_status(int argc
, char *argv
[], void *userdata
) {
874 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
875 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
876 _cleanup_free_ LinkInfo
*links
= NULL
;
879 (void) pager_open(arg_pager_flags
);
881 r
= sd_netlink_open(&rtnl
);
883 return log_error_errno(r
, "Failed to connect to netlink: %m");
885 r
= sd_hwdb_new(&hwdb
);
887 log_debug_errno(r
, "Failed to open hardware database: %m");
890 c
= acquire_link_info_all(rtnl
, &links
);
892 return system_status(rtnl
, hwdb
);
894 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
898 for (i
= 0; i
< c
; i
++) {
902 link_status_one(rtnl
, hwdb
, links
+ i
);
908 static char *lldp_capabilities_to_string(uint16_t x
) {
909 static const char characters
[] = {
910 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
915 ret
= new(char, ELEMENTSOF(characters
) + 1);
919 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
920 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
926 static void lldp_capabilities_legend(uint16_t x
) {
927 unsigned w
, i
, cols
= columns();
928 static const char* const table
[] = {
932 "w - WLAN Access Point",
935 "d - DOCSIS cable device",
939 "m - Two-port MAC Relay (TPMR)",
945 printf("\nCapability Flags:\n");
946 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
947 if (x
& (1U << i
) || arg_all
) {
950 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
953 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
958 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
959 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
960 _cleanup_free_ LinkInfo
*links
= NULL
;
964 r
= sd_netlink_open(&rtnl
);
966 return log_error_errno(r
, "Failed to connect to netlink: %m");
969 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
971 c
= acquire_link_info_all(rtnl
, &links
);
975 (void) pager_open(arg_pager_flags
);
978 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
986 for (i
= 0; i
< c
; i
++) {
987 _cleanup_fclose_
FILE *f
= NULL
;
989 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
993 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
998 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
;
999 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
, *capabilities
= NULL
;
1000 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1003 r
= next_lldp_neighbor(f
, &n
);
1005 log_warning_errno(r
, "Failed to read neighbor data: %m");
1011 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
1012 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1013 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1014 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1017 cid
= ellipsize(chassis_id
, 17, 100);
1023 pid
= ellipsize(port_id
, 17, 100);
1029 sname
= ellipsize(system_name
, 16, 100);
1031 system_name
= sname
;
1034 if (port_description
) {
1035 pdesc
= ellipsize(port_description
, 16, 100);
1037 port_description
= pdesc
;
1040 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
1041 capabilities
= lldp_capabilities_to_string(cc
);
1045 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
1049 strna(capabilities
),
1051 strna(port_description
));
1058 lldp_capabilities_legend(all
);
1059 printf("\n%i neighbors listed.\n", m
);
1065 static int help(void) {
1066 _cleanup_free_
char *link
= NULL
;
1069 r
= terminal_urlify_man("networkctl", "1", &link
);
1073 printf("%s [OPTIONS...]\n\n"
1074 "Query and control the networking subsystem.\n\n"
1075 " -h --help Show this help\n"
1076 " --version Show package version\n"
1077 " --no-pager Do not pipe output into a pager\n"
1078 " --no-legend Do not show the headers and footers\n"
1079 " -a --all Show status for all links\n\n"
1081 " list [LINK...] List links\n"
1082 " status [LINK...] Show link status\n"
1083 " lldp [LINK...] Show LLDP neighbors\n"
1084 " label Show current address label entries in the kernel\n"
1085 "\nSee the %s for details.\n"
1086 , program_invocation_short_name
1093 static int parse_argv(int argc
, char *argv
[]) {
1096 ARG_VERSION
= 0x100,
1101 static const struct option options
[] = {
1102 { "help", no_argument
, NULL
, 'h' },
1103 { "version", no_argument
, NULL
, ARG_VERSION
},
1104 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1105 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1106 { "all", no_argument
, NULL
, 'a' },
1115 while ((c
= getopt_long(argc
, argv
, "ha", options
, NULL
)) >= 0) {
1126 arg_pager_flags
|= PAGER_DISABLE
;
1141 assert_not_reached("Unhandled option");
1148 static int networkctl_main(int argc
, char *argv
[]) {
1149 static const Verb verbs
[] = {
1150 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
1151 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
1152 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
1153 { "label", VERB_ANY
, VERB_ANY
, 0, list_address_labels
},
1157 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1160 static void warn_networkd_missing(void) {
1162 if (access("/run/systemd/netif/state", F_OK
) >= 0)
1165 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1168 static int run(int argc
, char* argv
[]) {
1171 log_parse_environment();
1174 r
= parse_argv(argc
, argv
);
1178 warn_networkd_missing();
1180 return networkctl_main(argc
, argv
);
1183 DEFINE_MAIN_FUNCTION(run
);