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 "netlink-util.h"
25 #include "parse-util.h"
26 #include "socket-util.h"
27 #include "sparse-endian.h"
28 #include "stdio-util.h"
29 #include "string-table.h"
30 #include "string-util.h"
33 #include "terminal-util.h"
37 static PagerFlags arg_pager_flags
= 0;
38 static bool arg_legend
= true;
39 static bool arg_all
= false;
41 static char *link_get_type_string(unsigned short iftype
, sd_device
*d
) {
42 const char *t
, *devtype
;
46 sd_device_get_devtype(d
, &devtype
) >= 0 &&
48 return strdup(devtype
);
50 t
= arphrd_to_name(iftype
);
62 static void operational_state_to_color(const char *state
, const char **on
, const char **off
) {
66 if (streq_ptr(state
, "routable")) {
67 *on
= ansi_highlight_green();
69 } else if (streq_ptr(state
, "degraded")) {
70 *on
= ansi_highlight_yellow();
76 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
80 if (streq_ptr(state
, "configured")) {
81 *on
= ansi_highlight_green();
83 } else if (streq_ptr(state
, "configuring")) {
84 *on
= ansi_highlight_yellow();
86 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
87 *on
= ansi_highlight_red();
93 typedef struct LinkInfo
{
94 char name
[IFNAMSIZ
+1];
96 unsigned short iftype
;
97 struct ether_addr mac_address
;
100 bool has_mac_address
:1;
104 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
105 return CMP(a
->ifindex
, b
->ifindex
);
108 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
) {
116 r
= sd_netlink_message_get_type(m
, &type
);
120 if (type
!= RTM_NEWLINK
)
123 r
= sd_rtnl_message_link_get_ifindex(m
, &info
->ifindex
);
127 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
131 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
135 strscpy(info
->name
, sizeof info
->name
, name
);
137 info
->has_mac_address
=
138 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
139 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
142 sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
) &&
148 static int acquire_link_info_strv(sd_netlink
*rtnl
, char **l
, LinkInfo
**ret
) {
149 _cleanup_free_ LinkInfo
*links
= NULL
;
157 links
= new(LinkInfo
, strv_length(l
));
162 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
165 if (parse_ifindex(*i
, &ifindex
) >= 0)
166 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, ifindex
);
168 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
170 return rtnl_log_create_error(r
);
172 r
= sd_netlink_message_append_string(req
, IFLA_IFNAME
, *i
);
175 return rtnl_log_create_error(r
);
177 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
179 return log_error_errno(r
, "Failed to request link: %m");
181 r
= decode_link(reply
, links
+ c
);
188 typesafe_qsort(links
, c
, link_info_compare
);
190 *ret
= TAKE_PTR(links
);
195 static int acquire_link_info_all(sd_netlink
*rtnl
, LinkInfo
**ret
) {
196 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
197 _cleanup_free_ LinkInfo
*links
= NULL
;
198 size_t allocated
= 0, c
= 0;
199 sd_netlink_message
*i
;
205 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
207 return rtnl_log_create_error(r
);
209 r
= sd_netlink_message_request_dump(req
, true);
211 return rtnl_log_create_error(r
);
213 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
215 return log_error_errno(r
, "Failed to enumerate links: %m");
217 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
218 if (!GREEDY_REALLOC(links
, allocated
, c
+1))
221 r
= decode_link(i
, links
+ c
);
228 typesafe_qsort(links
, c
, link_info_compare
);
230 *ret
= TAKE_PTR(links
);
235 static int list_links(int argc
, char *argv
[], void *userdata
) {
236 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
237 _cleanup_free_ LinkInfo
*links
= NULL
;
240 r
= sd_netlink_open(&rtnl
);
242 return log_error_errno(r
, "Failed to connect to netlink: %m");
245 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
247 c
= acquire_link_info_all(rtnl
, &links
);
251 (void) pager_open(arg_pager_flags
);
254 printf("%3s %-16s %-18s %-11s %-10s\n",
261 for (i
= 0; i
< c
; i
++) {
262 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
263 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
264 const char *on_color_operational
, *off_color_operational
,
265 *on_color_setup
, *off_color_setup
;
266 char devid
[2 + DECIMAL_STR_MAX(int)];
267 _cleanup_free_
char *t
= NULL
;
269 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
270 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
272 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
273 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
274 setup_state
= strdup("unmanaged");
275 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
277 xsprintf(devid
, "n%i", links
[i
].ifindex
);
278 (void) sd_device_new_from_device_id(&d
, devid
);
280 t
= link_get_type_string(links
[i
].iftype
, d
);
282 printf("%3i %-16s %-18s %s%-11s%s %s%-10s%s\n",
283 links
[i
].ifindex
, links
[i
].name
, strna(t
),
284 on_color_operational
, strna(operational_state
), off_color_operational
,
285 on_color_setup
, strna(setup_state
), off_color_setup
);
289 printf("\n%i links listed.\n", c
);
294 /* IEEE Organizationally Unique Identifier vendor string */
295 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
296 const char *description
;
297 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
308 /* skip commonly misused 00:00:00 (Xerox) prefix */
309 if (memcmp(mac
, "\0\0\0", 3) == 0)
312 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
313 ETHER_ADDR_FORMAT_VAL(*mac
));
315 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
319 desc
= strdup(description
);
328 static int get_gateway_description(
333 union in_addr_union
*gateway
,
334 char **gateway_description
) {
335 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
336 sd_netlink_message
*m
;
340 assert(ifindex
>= 0);
341 assert(IN_SET(family
, AF_INET
, AF_INET6
));
343 assert(gateway_description
);
345 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
349 r
= sd_netlink_message_request_dump(req
, true);
353 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
357 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
358 union in_addr_union gw
= {};
359 struct ether_addr mac
= {};
363 r
= sd_netlink_message_get_errno(m
);
365 log_error_errno(r
, "got error: %m");
369 r
= sd_netlink_message_get_type(m
, &type
);
371 log_error_errno(r
, "could not get type: %m");
375 if (type
!= RTM_NEWNEIGH
) {
376 log_error("type is not RTM_NEWNEIGH");
380 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
382 log_error_errno(r
, "could not get family: %m");
387 log_error("family is not correct");
391 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
393 log_error_errno(r
, "could not get ifindex: %m");
397 if (ifindex
> 0 && ifi
!= ifindex
)
402 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
408 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
417 if (!in_addr_equal(fam
, &gw
, gateway
))
420 r
= sd_netlink_message_read_ether_addr(m
, NDA_LLADDR
, &mac
);
424 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
434 static int dump_gateways(
439 _cleanup_free_
struct local_address
*local
= NULL
;
445 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
449 for (i
= 0; i
< n
; i
++) {
450 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
;
452 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
456 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
458 log_debug_errno(r
, "Could not get description of gateway: %m");
461 (int) strlen(prefix
),
462 i
== 0 ? prefix
: "",
466 printf(" (%s)", description
);
468 /* Show interface name for the entry if we show
469 * entries for all interfaces */
471 char name
[IF_NAMESIZE
+1];
473 if (if_indextoname(local
[i
].ifindex
, name
)) {
474 fputs(" on ", stdout
);
477 printf(" on %%%i", local
[i
].ifindex
);
486 static int dump_addresses(
491 _cleanup_free_
struct local_address
*local
= NULL
;
497 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
501 for (i
= 0; i
< n
; i
++) {
502 _cleanup_free_
char *pretty
= NULL
;
504 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
509 (int) strlen(prefix
),
510 i
== 0 ? prefix
: "",
514 char name
[IF_NAMESIZE
+1];
516 if (if_indextoname(local
[i
].ifindex
, name
)) {
517 fputs(" on ", stdout
);
520 printf(" on %%%i", local
[i
].ifindex
);
529 static int dump_address_labels(sd_netlink
*rtnl
) {
530 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
531 sd_netlink_message
*m
;
536 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
538 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
540 r
= sd_netlink_message_request_dump(req
, true);
544 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
548 printf("%10s/%s %30s\n", "Prefix", "Prefixlen", "Label");
550 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
551 _cleanup_free_
char *pretty
= NULL
;
552 union in_addr_union prefix
= {};
556 r
= sd_netlink_message_get_errno(m
);
558 log_error_errno(r
, "got error: %m");
562 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
563 if (r
< 0 && r
!= -ENODATA
) {
564 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
568 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
572 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
576 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
580 printf("%10s/%-5u %30u\n", pretty
, prefixlen
, label
);
586 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
587 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
590 r
= sd_netlink_open(&rtnl
);
592 return log_error_errno(r
, "Failed to connect to netlink: %m");
594 dump_address_labels(rtnl
);
599 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
600 _cleanup_free_
char *p
= NULL
;
603 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
614 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
615 _cleanup_free_
void *raw
= NULL
;
623 l
= fread(&u
, 1, sizeof(u
), f
);
624 if (l
== 0 && feof(f
))
629 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
630 if (le64toh(u
) >= 4096)
633 raw
= new(uint8_t, le64toh(u
));
637 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
640 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
647 static int dump_lldp_neighbors(const char *prefix
, int ifindex
) {
648 _cleanup_fclose_
FILE *f
= NULL
;
654 r
= open_lldp_neighbors(ifindex
, &f
);
659 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
660 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
662 r
= next_lldp_neighbor(f
, &n
);
669 (int) strlen(prefix
),
670 c
== 0 ? prefix
: "");
672 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
673 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
674 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
676 printf("%s on port %s", strna(system_name
), strna(port_id
));
678 if (!isempty(port_description
))
679 printf(" (%s)", port_description
);
689 static void dump_ifindexes(const char *prefix
, const int *ifindexes
) {
694 if (!ifindexes
|| ifindexes
[0] <= 0)
697 for (c
= 0; ifindexes
[c
] > 0; c
++) {
698 char name
[IF_NAMESIZE
+1];
701 (int) strlen(prefix
),
702 c
== 0 ? prefix
: "");
704 if (if_indextoname(ifindexes
[c
], name
))
707 printf("%i", ifindexes
[c
]);
713 static void dump_list(const char *prefix
, char **l
) {
721 (int) strlen(prefix
),
722 i
== l
? prefix
: "",
727 static int link_status_one(
730 const LinkInfo
*info
) {
732 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
733 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
, *tz
= NULL
;
734 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
735 char devid
[2 + DECIMAL_STR_MAX(int)];
736 _cleanup_free_
char *t
= NULL
, *network
= NULL
;
737 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
;
738 const char *on_color_operational
, *off_color_operational
,
739 *on_color_setup
, *off_color_setup
;
740 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
746 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
747 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
749 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
750 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
751 setup_state
= strdup("unmanaged");
752 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
754 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
755 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
756 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
757 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
759 xsprintf(devid
, "n%i", info
->ifindex
);
761 (void) sd_device_new_from_device_id(&d
, devid
);
764 (void) sd_device_get_property_value(d
, "ID_NET_LINK_FILE", &link
);
765 (void) sd_device_get_property_value(d
, "ID_NET_DRIVER", &driver
);
766 (void) sd_device_get_property_value(d
, "ID_PATH", &path
);
768 if (sd_device_get_property_value(d
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
769 (void) sd_device_get_property_value(d
, "ID_VENDOR", &vendor
);
771 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
772 (void) sd_device_get_property_value(d
, "ID_MODEL", &model
);
775 t
= link_get_type_string(info
->iftype
, d
);
777 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
779 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
780 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
782 printf("%s%s%s %i: %s\n", on_color_operational
, special_glyph(BLACK_CIRCLE
), off_color_operational
, info
->ifindex
, info
->name
);
784 printf(" Link File: %s\n"
785 " Network File: %s\n"
787 " State: %s%s%s (%s%s%s)\n",
791 on_color_operational
, strna(operational_state
), off_color_operational
,
792 on_color_setup
, strna(setup_state
), off_color_setup
);
795 printf(" Path: %s\n", path
);
797 printf(" Driver: %s\n", driver
);
799 printf(" Vendor: %s\n", vendor
);
801 printf(" Model: %s\n", model
);
803 if (info
->has_mac_address
) {
804 _cleanup_free_
char *description
= NULL
;
805 char ea
[ETHER_ADDR_TO_STRING_MAX
];
807 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
810 printf(" HW Address: %s (%s)\n", ether_addr_to_string(&info
->mac_address
, ea
), description
);
812 printf(" HW Address: %s\n", ether_addr_to_string(&info
->mac_address
, ea
));
816 printf(" MTU: %" PRIu32
"\n", info
->mtu
);
818 (void) dump_addresses(rtnl
, " Address: ", info
->ifindex
);
819 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", info
->ifindex
);
821 dump_list(" DNS: ", dns
);
822 dump_list(" Search Domains: ", search_domains
);
823 dump_list(" Route Domains: ", route_domains
);
825 dump_list(" NTP: ", ntp
);
827 dump_ifindexes("Carrier Bound To: ", carrier_bound_to
);
828 dump_ifindexes("Carrier Bound By: ", carrier_bound_by
);
830 (void) sd_network_link_get_timezone(info
->ifindex
, &tz
);
832 printf(" Time Zone: %s\n", tz
);
834 (void) dump_lldp_neighbors(" Connected To: ", info
->ifindex
);
839 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
840 _cleanup_free_
char *operational_state
= NULL
;
841 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
842 const char *on_color_operational
, *off_color_operational
;
846 (void) sd_network_get_operational_state(&operational_state
);
847 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
849 printf("%s%s%s State: %s%s%s\n",
850 on_color_operational
, special_glyph(BLACK_CIRCLE
), off_color_operational
,
851 on_color_operational
, strna(operational_state
), off_color_operational
);
853 (void) dump_addresses(rtnl
, " Address: ", 0);
854 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", 0);
856 (void) sd_network_get_dns(&dns
);
857 dump_list(" DNS: ", dns
);
859 (void) sd_network_get_search_domains(&search_domains
);
860 dump_list("Search Domains: ", search_domains
);
862 (void) sd_network_get_route_domains(&route_domains
);
863 dump_list(" Route Domains: ", route_domains
);
865 (void) sd_network_get_ntp(&ntp
);
866 dump_list(" NTP: ", ntp
);
871 static int link_status(int argc
, char *argv
[], void *userdata
) {
872 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
873 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
874 _cleanup_free_ LinkInfo
*links
= NULL
;
877 (void) pager_open(arg_pager_flags
);
879 r
= sd_netlink_open(&rtnl
);
881 return log_error_errno(r
, "Failed to connect to netlink: %m");
883 r
= sd_hwdb_new(&hwdb
);
885 log_debug_errno(r
, "Failed to open hardware database: %m");
888 c
= acquire_link_info_all(rtnl
, &links
);
890 return system_status(rtnl
, hwdb
);
892 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
896 for (i
= 0; i
< c
; i
++) {
900 link_status_one(rtnl
, hwdb
, links
+ i
);
906 static char *lldp_capabilities_to_string(uint16_t x
) {
907 static const char characters
[] = {
908 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
913 ret
= new(char, ELEMENTSOF(characters
) + 1);
917 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
918 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
924 static void lldp_capabilities_legend(uint16_t x
) {
925 unsigned w
, i
, cols
= columns();
926 static const char* const table
[] = {
930 "w - WLAN Access Point",
933 "d - DOCSIS cable device",
937 "m - Two-port MAC Relay (TPMR)",
943 printf("\nCapability Flags:\n");
944 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
945 if (x
& (1U << i
) || arg_all
) {
948 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
951 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
956 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
957 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
958 _cleanup_free_ LinkInfo
*links
= NULL
;
962 r
= sd_netlink_open(&rtnl
);
964 return log_error_errno(r
, "Failed to connect to netlink: %m");
967 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
969 c
= acquire_link_info_all(rtnl
, &links
);
973 (void) pager_open(arg_pager_flags
);
976 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
984 for (i
= 0; i
< c
; i
++) {
985 _cleanup_fclose_
FILE *f
= NULL
;
987 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
991 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
996 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
;
997 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
, *capabilities
= NULL
;
998 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1001 r
= next_lldp_neighbor(f
, &n
);
1003 log_warning_errno(r
, "Failed to read neighbor data: %m");
1009 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
1010 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1011 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1012 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1015 cid
= ellipsize(chassis_id
, 17, 100);
1021 pid
= ellipsize(port_id
, 17, 100);
1027 sname
= ellipsize(system_name
, 16, 100);
1029 system_name
= sname
;
1032 if (port_description
) {
1033 pdesc
= ellipsize(port_description
, 16, 100);
1035 port_description
= pdesc
;
1038 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
1039 capabilities
= lldp_capabilities_to_string(cc
);
1043 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
1047 strna(capabilities
),
1049 strna(port_description
));
1056 lldp_capabilities_legend(all
);
1057 printf("\n%i neighbors listed.\n", m
);
1063 static int help(void) {
1064 _cleanup_free_
char *link
= NULL
;
1067 r
= terminal_urlify_man("networkctl", "1", &link
);
1071 printf("%s [OPTIONS...]\n\n"
1072 "Query and control the networking subsystem.\n\n"
1073 " -h --help Show this help\n"
1074 " --version Show package version\n"
1075 " --no-pager Do not pipe output into a pager\n"
1076 " --no-legend Do not show the headers and footers\n"
1077 " -a --all Show status for all links\n\n"
1079 " list [LINK...] List links\n"
1080 " status [LINK...] Show link status\n"
1081 " lldp [LINK...] Show LLDP neighbors\n"
1082 " label Show current address label entries in the kernel\n"
1083 "\nSee the %s for details.\n"
1084 , program_invocation_short_name
1091 static int parse_argv(int argc
, char *argv
[]) {
1094 ARG_VERSION
= 0x100,
1099 static const struct option options
[] = {
1100 { "help", no_argument
, NULL
, 'h' },
1101 { "version", no_argument
, NULL
, ARG_VERSION
},
1102 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1103 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1104 { "all", no_argument
, NULL
, 'a' },
1113 while ((c
= getopt_long(argc
, argv
, "ha", options
, NULL
)) >= 0) {
1124 arg_pager_flags
|= PAGER_DISABLE
;
1139 assert_not_reached("Unhandled option");
1146 static int networkctl_main(int argc
, char *argv
[]) {
1147 static const Verb verbs
[] = {
1148 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
1149 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
1150 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
1151 { "label", VERB_ANY
, VERB_ANY
, 0, list_address_labels
},
1155 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1158 static void warn_networkd_missing(void) {
1160 if (access("/run/systemd/netif/state", F_OK
) >= 0)
1163 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1166 int main(int argc
, char* argv
[]) {
1169 log_parse_environment();
1172 r
= parse_argv(argc
, argv
);
1176 warn_networkd_missing();
1178 r
= networkctl_main(argc
, argv
);
1183 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;