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 "hwdb-util.h"
23 #include "local-addresses.h"
24 #include "locale-util.h"
26 #include "main-func.h"
27 #include "netlink-util.h"
29 #include "parse-util.h"
30 #include "pretty-print.h"
31 #include "socket-util.h"
32 #include "sort-util.h"
33 #include "sparse-endian.h"
34 #include "stdio-util.h"
35 #include "string-table.h"
36 #include "string-util.h"
39 #include "terminal-util.h"
42 static PagerFlags arg_pager_flags
= 0;
43 static bool arg_legend
= true;
44 static bool arg_all
= false;
46 static char *link_get_type_string(unsigned short iftype
, sd_device
*d
) {
47 const char *t
, *devtype
;
51 sd_device_get_devtype(d
, &devtype
) >= 0 &&
53 return strdup(devtype
);
55 t
= arphrd_to_name(iftype
);
67 static void operational_state_to_color(const char *state
, const char **on
, const char **off
) {
71 if (STRPTR_IN_SET(state
, "routable", "enslaved")) {
72 *on
= ansi_highlight_green();
74 } else if (streq_ptr(state
, "degraded")) {
75 *on
= ansi_highlight_yellow();
81 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
85 if (streq_ptr(state
, "configured")) {
86 *on
= ansi_highlight_green();
88 } else if (streq_ptr(state
, "configuring")) {
89 *on
= ansi_highlight_yellow();
91 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
92 *on
= ansi_highlight_red();
98 typedef struct LinkInfo
{
99 char name
[IFNAMSIZ
+1];
101 unsigned short iftype
;
102 struct ether_addr mac_address
;
107 bool has_mac_address
:1;
113 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
114 return CMP(a
->ifindex
, b
->ifindex
);
117 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
, char **patterns
) {
125 r
= sd_netlink_message_get_type(m
, &type
);
129 if (type
!= RTM_NEWLINK
)
132 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
136 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
141 char str
[DECIMAL_STR_MAX(int)];
143 xsprintf(str
, "%i", ifindex
);
145 if (!strv_fnmatch(patterns
, str
, 0) && !strv_fnmatch(patterns
, name
, 0))
149 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
153 strscpy(info
->name
, sizeof info
->name
, name
);
154 info
->ifindex
= ifindex
;
156 info
->has_mac_address
=
157 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
158 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
161 sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
) >= 0 &&
165 sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
) >= 0 &&
169 sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
) >= 0 &&
175 static int acquire_link_info(sd_netlink
*rtnl
, char **patterns
, LinkInfo
**ret
) {
176 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
177 _cleanup_free_ LinkInfo
*links
= NULL
;
178 size_t allocated
= 0, c
= 0;
179 sd_netlink_message
*i
;
185 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
187 return rtnl_log_create_error(r
);
189 r
= sd_netlink_message_request_dump(req
, true);
191 return rtnl_log_create_error(r
);
193 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
195 return log_error_errno(r
, "Failed to enumerate links: %m");
197 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
198 if (!GREEDY_REALLOC(links
, allocated
, c
+1))
201 r
= decode_link(i
, links
+ c
, patterns
);
208 typesafe_qsort(links
, c
, link_info_compare
);
210 *ret
= TAKE_PTR(links
);
215 static int list_links(int argc
, char *argv
[], void *userdata
) {
216 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
217 _cleanup_free_ LinkInfo
*links
= NULL
;
220 r
= sd_netlink_open(&rtnl
);
222 return log_error_errno(r
, "Failed to connect to netlink: %m");
224 c
= acquire_link_info(rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
228 (void) pager_open(arg_pager_flags
);
231 printf("%3s %-16s %-18s %-16s %-10s\n",
238 for (i
= 0; i
< c
; i
++) {
239 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
240 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
241 const char *on_color_operational
, *off_color_operational
,
242 *on_color_setup
, *off_color_setup
;
243 char devid
[2 + DECIMAL_STR_MAX(int)];
244 _cleanup_free_
char *t
= NULL
;
246 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
247 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
249 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
250 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
251 setup_state
= strdup("unmanaged");
252 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
254 xsprintf(devid
, "n%i", links
[i
].ifindex
);
255 (void) sd_device_new_from_device_id(&d
, devid
);
257 t
= link_get_type_string(links
[i
].iftype
, d
);
259 printf("%3i %-16s %-18s %s%-16s%s %s%-10s%s\n",
260 links
[i
].ifindex
, links
[i
].name
, strna(t
),
261 on_color_operational
, strna(operational_state
), off_color_operational
,
262 on_color_setup
, strna(setup_state
), off_color_setup
);
266 printf("\n%i links listed.\n", c
);
271 /* IEEE Organizationally Unique Identifier vendor string */
272 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
273 const char *description
;
274 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
285 /* skip commonly misused 00:00:00 (Xerox) prefix */
286 if (memcmp(mac
, "\0\0\0", 3) == 0)
289 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
290 ETHER_ADDR_FORMAT_VAL(*mac
));
292 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
296 desc
= strdup(description
);
305 static int get_gateway_description(
310 union in_addr_union
*gateway
,
311 char **gateway_description
) {
312 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
313 sd_netlink_message
*m
;
317 assert(ifindex
>= 0);
318 assert(IN_SET(family
, AF_INET
, AF_INET6
));
320 assert(gateway_description
);
322 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
326 r
= sd_netlink_message_request_dump(req
, true);
330 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
334 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
335 union in_addr_union gw
= IN_ADDR_NULL
;
336 struct ether_addr mac
= ETHER_ADDR_NULL
;
340 r
= sd_netlink_message_get_errno(m
);
342 log_error_errno(r
, "got error: %m");
346 r
= sd_netlink_message_get_type(m
, &type
);
348 log_error_errno(r
, "could not get type: %m");
352 if (type
!= RTM_NEWNEIGH
) {
353 log_error("type is not RTM_NEWNEIGH");
357 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
359 log_error_errno(r
, "could not get family: %m");
364 log_error("family is not correct");
368 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
370 log_error_errno(r
, "could not get ifindex: %m");
374 if (ifindex
> 0 && ifi
!= ifindex
)
379 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
385 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
394 if (!in_addr_equal(fam
, &gw
, gateway
))
397 r
= sd_netlink_message_read_ether_addr(m
, NDA_LLADDR
, &mac
);
401 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
411 static int dump_gateways(
416 _cleanup_free_
struct local_address
*local
= NULL
;
422 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
426 for (i
= 0; i
< n
; i
++) {
427 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
;
429 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
433 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
435 log_debug_errno(r
, "Could not get description of gateway: %m");
438 (int) strlen(prefix
),
439 i
== 0 ? prefix
: "",
443 printf(" (%s)", description
);
445 /* Show interface name for the entry if we show
446 * entries for all interfaces */
448 char name
[IF_NAMESIZE
+1];
450 if (if_indextoname(local
[i
].ifindex
, name
)) {
451 fputs(" on ", stdout
);
454 printf(" on %%%i", local
[i
].ifindex
);
463 static int dump_addresses(
468 _cleanup_free_
struct local_address
*local
= NULL
;
474 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
478 for (i
= 0; i
< n
; i
++) {
479 _cleanup_free_
char *pretty
= NULL
;
481 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
486 (int) strlen(prefix
),
487 i
== 0 ? prefix
: "",
491 char name
[IF_NAMESIZE
+1];
493 if (if_indextoname(local
[i
].ifindex
, name
)) {
494 fputs(" on ", stdout
);
497 printf(" on %%%i", local
[i
].ifindex
);
506 static int dump_address_labels(sd_netlink
*rtnl
) {
507 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
508 sd_netlink_message
*m
;
513 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
515 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
517 r
= sd_netlink_message_request_dump(req
, true);
521 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
525 printf("%10s/%s %30s\n", "Prefix", "Prefixlen", "Label");
527 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
528 _cleanup_free_
char *pretty
= NULL
;
529 union in_addr_union prefix
= IN_ADDR_NULL
;
533 r
= sd_netlink_message_get_errno(m
);
535 log_error_errno(r
, "got error: %m");
539 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
540 if (r
< 0 && r
!= -ENODATA
) {
541 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
545 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
549 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
553 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
557 printf("%10s/%-5u %30u\n", pretty
, prefixlen
, label
);
563 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
564 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
567 r
= sd_netlink_open(&rtnl
);
569 return log_error_errno(r
, "Failed to connect to netlink: %m");
571 dump_address_labels(rtnl
);
576 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
577 _cleanup_free_
char *p
= NULL
;
580 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
591 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
592 _cleanup_free_
void *raw
= NULL
;
600 l
= fread(&u
, 1, sizeof(u
), f
);
601 if (l
== 0 && feof(f
))
606 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
607 if (le64toh(u
) >= 4096)
610 raw
= new(uint8_t, le64toh(u
));
614 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
617 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
624 static int dump_lldp_neighbors(const char *prefix
, int ifindex
) {
625 _cleanup_fclose_
FILE *f
= NULL
;
631 r
= open_lldp_neighbors(ifindex
, &f
);
636 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
637 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
639 r
= next_lldp_neighbor(f
, &n
);
646 (int) strlen(prefix
),
647 c
== 0 ? prefix
: "");
649 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
650 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
651 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
653 printf("%s on port %s", strna(system_name
), strna(port_id
));
655 if (!isempty(port_description
))
656 printf(" (%s)", port_description
);
666 static void dump_ifindexes(const char *prefix
, const int *ifindexes
) {
671 if (!ifindexes
|| ifindexes
[0] <= 0)
674 for (c
= 0; ifindexes
[c
] > 0; c
++) {
675 char name
[IF_NAMESIZE
+1];
678 (int) strlen(prefix
),
679 c
== 0 ? prefix
: "");
681 if (if_indextoname(ifindexes
[c
], name
))
684 printf("%i", ifindexes
[c
]);
690 static void dump_list(const char *prefix
, char **l
) {
698 (int) strlen(prefix
),
699 i
== l
? prefix
: "",
704 static int link_status_one(
707 const LinkInfo
*info
) {
709 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
710 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
, *tz
= NULL
;
711 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
712 char devid
[2 + DECIMAL_STR_MAX(int)];
713 _cleanup_free_
char *t
= NULL
, *network
= NULL
;
714 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
;
715 const char *on_color_operational
, *off_color_operational
,
716 *on_color_setup
, *off_color_setup
;
717 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
723 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
724 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
726 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
727 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
728 setup_state
= strdup("unmanaged");
729 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
731 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
732 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
733 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
734 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
736 xsprintf(devid
, "n%i", info
->ifindex
);
738 (void) sd_device_new_from_device_id(&d
, devid
);
741 (void) sd_device_get_property_value(d
, "ID_NET_LINK_FILE", &link
);
742 (void) sd_device_get_property_value(d
, "ID_NET_DRIVER", &driver
);
743 (void) sd_device_get_property_value(d
, "ID_PATH", &path
);
745 if (sd_device_get_property_value(d
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
746 (void) sd_device_get_property_value(d
, "ID_VENDOR", &vendor
);
748 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
749 (void) sd_device_get_property_value(d
, "ID_MODEL", &model
);
752 t
= link_get_type_string(info
->iftype
, d
);
754 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
756 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
757 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
759 printf("%s%s%s %i: %s\n", on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
, info
->ifindex
, info
->name
);
761 printf(" Link File: %s\n"
762 " Network File: %s\n"
764 " State: %s%s%s (%s%s%s)\n",
768 on_color_operational
, strna(operational_state
), off_color_operational
,
769 on_color_setup
, strna(setup_state
), off_color_setup
);
772 printf(" Path: %s\n", path
);
774 printf(" Driver: %s\n", driver
);
776 printf(" Vendor: %s\n", vendor
);
778 printf(" Model: %s\n", model
);
780 if (info
->has_mac_address
) {
781 _cleanup_free_
char *description
= NULL
;
782 char ea
[ETHER_ADDR_TO_STRING_MAX
];
784 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
787 printf(" HW Address: %s (%s)\n", ether_addr_to_string(&info
->mac_address
, ea
), description
);
789 printf(" HW Address: %s\n", ether_addr_to_string(&info
->mac_address
, ea
));
793 printf(" MTU: %" PRIu32
"\n", info
->mtu
);
794 if (info
->has_min_mtu
)
795 printf(" Minimum MTU: %" PRIu32
"\n", info
->min_mtu
);
796 if (info
->has_max_mtu
)
797 printf(" Maximum MTU: %" PRIu32
"\n", info
->max_mtu
);
799 (void) dump_addresses(rtnl
, " Address: ", info
->ifindex
);
800 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", info
->ifindex
);
802 dump_list(" DNS: ", dns
);
803 dump_list(" Search Domains: ", search_domains
);
804 dump_list(" Route Domains: ", route_domains
);
806 dump_list(" NTP: ", ntp
);
808 dump_ifindexes("Carrier Bound To: ", carrier_bound_to
);
809 dump_ifindexes("Carrier Bound By: ", carrier_bound_by
);
811 (void) sd_network_link_get_timezone(info
->ifindex
, &tz
);
813 printf(" Time Zone: %s\n", tz
);
815 (void) dump_lldp_neighbors(" Connected To: ", info
->ifindex
);
820 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
821 _cleanup_free_
char *operational_state
= NULL
;
822 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
823 const char *on_color_operational
, *off_color_operational
;
827 (void) sd_network_get_operational_state(&operational_state
);
828 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
830 printf("%s%s%s State: %s%s%s\n",
831 on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
,
832 on_color_operational
, strna(operational_state
), off_color_operational
);
834 (void) dump_addresses(rtnl
, " Address: ", 0);
835 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", 0);
837 (void) sd_network_get_dns(&dns
);
838 dump_list(" DNS: ", dns
);
840 (void) sd_network_get_search_domains(&search_domains
);
841 dump_list("Search Domains: ", search_domains
);
843 (void) sd_network_get_route_domains(&route_domains
);
844 dump_list(" Route Domains: ", route_domains
);
846 (void) sd_network_get_ntp(&ntp
);
847 dump_list(" NTP: ", ntp
);
852 static int link_status(int argc
, char *argv
[], void *userdata
) {
853 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
854 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
855 _cleanup_free_ LinkInfo
*links
= NULL
;
858 (void) pager_open(arg_pager_flags
);
860 r
= sd_netlink_open(&rtnl
);
862 return log_error_errno(r
, "Failed to connect to netlink: %m");
864 r
= sd_hwdb_new(&hwdb
);
866 log_debug_errno(r
, "Failed to open hardware database: %m");
869 c
= acquire_link_info(rtnl
, NULL
, &links
);
871 return system_status(rtnl
, hwdb
);
873 c
= acquire_link_info(rtnl
, argv
+ 1, &links
);
877 for (i
= 0; i
< c
; i
++) {
881 link_status_one(rtnl
, hwdb
, links
+ i
);
887 static char *lldp_capabilities_to_string(uint16_t x
) {
888 static const char characters
[] = {
889 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
894 ret
= new(char, ELEMENTSOF(characters
) + 1);
898 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
899 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
905 static void lldp_capabilities_legend(uint16_t x
) {
906 unsigned w
, i
, cols
= columns();
907 static const char* const table
[] = {
911 "w - WLAN Access Point",
914 "d - DOCSIS cable device",
918 "m - Two-port MAC Relay (TPMR)",
924 printf("\nCapability Flags:\n");
925 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
926 if (x
& (1U << i
) || arg_all
) {
929 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
932 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
937 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
938 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
939 _cleanup_free_ LinkInfo
*links
= NULL
;
943 r
= sd_netlink_open(&rtnl
);
945 return log_error_errno(r
, "Failed to connect to netlink: %m");
947 c
= acquire_link_info(rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
951 (void) pager_open(arg_pager_flags
);
954 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
962 for (i
= 0; i
< c
; i
++) {
963 _cleanup_fclose_
FILE *f
= NULL
;
965 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
969 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
974 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
;
975 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
, *capabilities
= NULL
;
976 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
979 r
= next_lldp_neighbor(f
, &n
);
981 log_warning_errno(r
, "Failed to read neighbor data: %m");
987 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
988 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
989 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
990 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
993 cid
= ellipsize(chassis_id
, 17, 100);
999 pid
= ellipsize(port_id
, 17, 100);
1005 sname
= ellipsize(system_name
, 16, 100);
1007 system_name
= sname
;
1010 if (port_description
) {
1011 pdesc
= ellipsize(port_description
, 16, 100);
1013 port_description
= pdesc
;
1016 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
1017 capabilities
= lldp_capabilities_to_string(cc
);
1021 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
1025 strna(capabilities
),
1027 strna(port_description
));
1034 lldp_capabilities_legend(all
);
1035 printf("\n%i neighbors listed.\n", m
);
1041 static int help(void) {
1042 _cleanup_free_
char *link
= NULL
;
1045 r
= terminal_urlify_man("networkctl", "1", &link
);
1049 printf("%s [OPTIONS...]\n\n"
1050 "Query and control the networking subsystem.\n\n"
1051 " -h --help Show this help\n"
1052 " --version Show package version\n"
1053 " --no-pager Do not pipe output into a pager\n"
1054 " --no-legend Do not show the headers and footers\n"
1055 " -a --all Show status for all links\n\n"
1057 " list [PATTERN...] List links\n"
1058 " status [PATTERN...] Show link status\n"
1059 " lldp [PATTERN...] Show LLDP neighbors\n"
1060 " label Show current address label entries in the kernel\n"
1061 "\nSee the %s for details.\n"
1062 , program_invocation_short_name
1069 static int parse_argv(int argc
, char *argv
[]) {
1072 ARG_VERSION
= 0x100,
1077 static const struct option options
[] = {
1078 { "help", no_argument
, NULL
, 'h' },
1079 { "version", no_argument
, NULL
, ARG_VERSION
},
1080 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1081 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1082 { "all", no_argument
, NULL
, 'a' },
1091 while ((c
= getopt_long(argc
, argv
, "ha", options
, NULL
)) >= 0) {
1102 arg_pager_flags
|= PAGER_DISABLE
;
1117 assert_not_reached("Unhandled option");
1124 static int networkctl_main(int argc
, char *argv
[]) {
1125 static const Verb verbs
[] = {
1126 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
1127 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
1128 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
1129 { "label", VERB_ANY
, VERB_ANY
, 0, list_address_labels
},
1133 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1136 static void warn_networkd_missing(void) {
1138 if (access("/run/systemd/netif/state", F_OK
) >= 0)
1141 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1144 static int run(int argc
, char* argv
[]) {
1147 log_show_color(true);
1148 log_parse_environment();
1151 r
= parse_argv(argc
, argv
);
1155 warn_networkd_missing();
1157 return networkctl_main(argc
, argv
);
1160 DEFINE_MAIN_FUNCTION(run
);