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
;
105 bool has_mac_address
:1;
109 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
110 return CMP(a
->ifindex
, b
->ifindex
);
113 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
, char **patterns
) {
121 r
= sd_netlink_message_get_type(m
, &type
);
125 if (type
!= RTM_NEWLINK
)
128 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
132 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
137 char str
[DECIMAL_STR_MAX(int)];
139 xsprintf(str
, "%i", ifindex
);
141 if (!strv_fnmatch(patterns
, str
, 0) && !strv_fnmatch(patterns
, name
, 0))
145 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
149 strscpy(info
->name
, sizeof info
->name
, name
);
150 info
->ifindex
= ifindex
;
152 info
->has_mac_address
=
153 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
154 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
157 sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
) &&
163 static int acquire_link_info(sd_netlink
*rtnl
, char **patterns
, LinkInfo
**ret
) {
164 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
165 _cleanup_free_ LinkInfo
*links
= NULL
;
166 size_t allocated
= 0, c
= 0;
167 sd_netlink_message
*i
;
173 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
175 return rtnl_log_create_error(r
);
177 r
= sd_netlink_message_request_dump(req
, true);
179 return rtnl_log_create_error(r
);
181 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
183 return log_error_errno(r
, "Failed to enumerate links: %m");
185 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
186 if (!GREEDY_REALLOC(links
, allocated
, c
+1))
189 r
= decode_link(i
, links
+ c
, patterns
);
196 typesafe_qsort(links
, c
, link_info_compare
);
198 *ret
= TAKE_PTR(links
);
203 static int list_links(int argc
, char *argv
[], void *userdata
) {
204 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
205 _cleanup_free_ LinkInfo
*links
= NULL
;
208 r
= sd_netlink_open(&rtnl
);
210 return log_error_errno(r
, "Failed to connect to netlink: %m");
212 c
= acquire_link_info(rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
216 (void) pager_open(arg_pager_flags
);
219 printf("%3s %-16s %-18s %-16s %-10s\n",
226 for (i
= 0; i
< c
; i
++) {
227 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
228 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
229 const char *on_color_operational
, *off_color_operational
,
230 *on_color_setup
, *off_color_setup
;
231 char devid
[2 + DECIMAL_STR_MAX(int)];
232 _cleanup_free_
char *t
= NULL
;
234 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
235 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
237 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
238 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
239 setup_state
= strdup("unmanaged");
240 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
242 xsprintf(devid
, "n%i", links
[i
].ifindex
);
243 (void) sd_device_new_from_device_id(&d
, devid
);
245 t
= link_get_type_string(links
[i
].iftype
, d
);
247 printf("%3i %-16s %-18s %s%-16s%s %s%-10s%s\n",
248 links
[i
].ifindex
, links
[i
].name
, strna(t
),
249 on_color_operational
, strna(operational_state
), off_color_operational
,
250 on_color_setup
, strna(setup_state
), off_color_setup
);
254 printf("\n%i links listed.\n", c
);
259 /* IEEE Organizationally Unique Identifier vendor string */
260 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
261 const char *description
;
262 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
273 /* skip commonly misused 00:00:00 (Xerox) prefix */
274 if (memcmp(mac
, "\0\0\0", 3) == 0)
277 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
278 ETHER_ADDR_FORMAT_VAL(*mac
));
280 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
284 desc
= strdup(description
);
293 static int get_gateway_description(
298 union in_addr_union
*gateway
,
299 char **gateway_description
) {
300 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
301 sd_netlink_message
*m
;
305 assert(ifindex
>= 0);
306 assert(IN_SET(family
, AF_INET
, AF_INET6
));
308 assert(gateway_description
);
310 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
314 r
= sd_netlink_message_request_dump(req
, true);
318 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
322 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
323 union in_addr_union gw
= IN_ADDR_NULL
;
324 struct ether_addr mac
= ETHER_ADDR_NULL
;
328 r
= sd_netlink_message_get_errno(m
);
330 log_error_errno(r
, "got error: %m");
334 r
= sd_netlink_message_get_type(m
, &type
);
336 log_error_errno(r
, "could not get type: %m");
340 if (type
!= RTM_NEWNEIGH
) {
341 log_error("type is not RTM_NEWNEIGH");
345 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
347 log_error_errno(r
, "could not get family: %m");
352 log_error("family is not correct");
356 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
358 log_error_errno(r
, "could not get ifindex: %m");
362 if (ifindex
> 0 && ifi
!= ifindex
)
367 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
373 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
382 if (!in_addr_equal(fam
, &gw
, gateway
))
385 r
= sd_netlink_message_read_ether_addr(m
, NDA_LLADDR
, &mac
);
389 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
399 static int dump_gateways(
404 _cleanup_free_
struct local_address
*local
= NULL
;
410 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
414 for (i
= 0; i
< n
; i
++) {
415 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
;
417 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
421 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
423 log_debug_errno(r
, "Could not get description of gateway: %m");
426 (int) strlen(prefix
),
427 i
== 0 ? prefix
: "",
431 printf(" (%s)", description
);
433 /* Show interface name for the entry if we show
434 * entries for all interfaces */
436 char name
[IF_NAMESIZE
+1];
438 if (if_indextoname(local
[i
].ifindex
, name
)) {
439 fputs(" on ", stdout
);
442 printf(" on %%%i", local
[i
].ifindex
);
451 static int dump_addresses(
456 _cleanup_free_
struct local_address
*local
= NULL
;
462 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
466 for (i
= 0; i
< n
; i
++) {
467 _cleanup_free_
char *pretty
= NULL
;
469 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
474 (int) strlen(prefix
),
475 i
== 0 ? prefix
: "",
479 char name
[IF_NAMESIZE
+1];
481 if (if_indextoname(local
[i
].ifindex
, name
)) {
482 fputs(" on ", stdout
);
485 printf(" on %%%i", local
[i
].ifindex
);
494 static int dump_address_labels(sd_netlink
*rtnl
) {
495 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
496 sd_netlink_message
*m
;
501 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
503 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
505 r
= sd_netlink_message_request_dump(req
, true);
509 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
513 printf("%10s/%s %30s\n", "Prefix", "Prefixlen", "Label");
515 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
516 _cleanup_free_
char *pretty
= NULL
;
517 union in_addr_union prefix
= IN_ADDR_NULL
;
521 r
= sd_netlink_message_get_errno(m
);
523 log_error_errno(r
, "got error: %m");
527 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
528 if (r
< 0 && r
!= -ENODATA
) {
529 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
533 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
537 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
541 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
545 printf("%10s/%-5u %30u\n", pretty
, prefixlen
, label
);
551 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
552 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
555 r
= sd_netlink_open(&rtnl
);
557 return log_error_errno(r
, "Failed to connect to netlink: %m");
559 dump_address_labels(rtnl
);
564 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
565 _cleanup_free_
char *p
= NULL
;
568 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
579 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
580 _cleanup_free_
void *raw
= NULL
;
588 l
= fread(&u
, 1, sizeof(u
), f
);
589 if (l
== 0 && feof(f
))
594 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
595 if (le64toh(u
) >= 4096)
598 raw
= new(uint8_t, le64toh(u
));
602 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
605 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
612 static int dump_lldp_neighbors(const char *prefix
, int ifindex
) {
613 _cleanup_fclose_
FILE *f
= NULL
;
619 r
= open_lldp_neighbors(ifindex
, &f
);
624 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
625 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
627 r
= next_lldp_neighbor(f
, &n
);
634 (int) strlen(prefix
),
635 c
== 0 ? prefix
: "");
637 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
638 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
639 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
641 printf("%s on port %s", strna(system_name
), strna(port_id
));
643 if (!isempty(port_description
))
644 printf(" (%s)", port_description
);
654 static void dump_ifindexes(const char *prefix
, const int *ifindexes
) {
659 if (!ifindexes
|| ifindexes
[0] <= 0)
662 for (c
= 0; ifindexes
[c
] > 0; c
++) {
663 char name
[IF_NAMESIZE
+1];
666 (int) strlen(prefix
),
667 c
== 0 ? prefix
: "");
669 if (if_indextoname(ifindexes
[c
], name
))
672 printf("%i", ifindexes
[c
]);
678 static void dump_list(const char *prefix
, char **l
) {
686 (int) strlen(prefix
),
687 i
== l
? prefix
: "",
692 static int link_status_one(
695 const LinkInfo
*info
) {
697 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
698 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
, *tz
= NULL
;
699 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
700 char devid
[2 + DECIMAL_STR_MAX(int)];
701 _cleanup_free_
char *t
= NULL
, *network
= NULL
;
702 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
;
703 const char *on_color_operational
, *off_color_operational
,
704 *on_color_setup
, *off_color_setup
;
705 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
711 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
712 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
714 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
715 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
716 setup_state
= strdup("unmanaged");
717 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
719 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
720 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
721 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
722 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
724 xsprintf(devid
, "n%i", info
->ifindex
);
726 (void) sd_device_new_from_device_id(&d
, devid
);
729 (void) sd_device_get_property_value(d
, "ID_NET_LINK_FILE", &link
);
730 (void) sd_device_get_property_value(d
, "ID_NET_DRIVER", &driver
);
731 (void) sd_device_get_property_value(d
, "ID_PATH", &path
);
733 if (sd_device_get_property_value(d
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
734 (void) sd_device_get_property_value(d
, "ID_VENDOR", &vendor
);
736 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
737 (void) sd_device_get_property_value(d
, "ID_MODEL", &model
);
740 t
= link_get_type_string(info
->iftype
, d
);
742 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
744 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
745 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
747 printf("%s%s%s %i: %s\n", on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
, info
->ifindex
, info
->name
);
749 printf(" Link File: %s\n"
750 " Network File: %s\n"
752 " State: %s%s%s (%s%s%s)\n",
756 on_color_operational
, strna(operational_state
), off_color_operational
,
757 on_color_setup
, strna(setup_state
), off_color_setup
);
760 printf(" Path: %s\n", path
);
762 printf(" Driver: %s\n", driver
);
764 printf(" Vendor: %s\n", vendor
);
766 printf(" Model: %s\n", model
);
768 if (info
->has_mac_address
) {
769 _cleanup_free_
char *description
= NULL
;
770 char ea
[ETHER_ADDR_TO_STRING_MAX
];
772 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
775 printf(" HW Address: %s (%s)\n", ether_addr_to_string(&info
->mac_address
, ea
), description
);
777 printf(" HW Address: %s\n", ether_addr_to_string(&info
->mac_address
, ea
));
781 printf(" MTU: %" PRIu32
"\n", info
->mtu
);
783 (void) dump_addresses(rtnl
, " Address: ", info
->ifindex
);
784 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", info
->ifindex
);
786 dump_list(" DNS: ", dns
);
787 dump_list(" Search Domains: ", search_domains
);
788 dump_list(" Route Domains: ", route_domains
);
790 dump_list(" NTP: ", ntp
);
792 dump_ifindexes("Carrier Bound To: ", carrier_bound_to
);
793 dump_ifindexes("Carrier Bound By: ", carrier_bound_by
);
795 (void) sd_network_link_get_timezone(info
->ifindex
, &tz
);
797 printf(" Time Zone: %s\n", tz
);
799 (void) dump_lldp_neighbors(" Connected To: ", info
->ifindex
);
804 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
805 _cleanup_free_
char *operational_state
= NULL
;
806 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
807 const char *on_color_operational
, *off_color_operational
;
811 (void) sd_network_get_operational_state(&operational_state
);
812 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
814 printf("%s%s%s State: %s%s%s\n",
815 on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
,
816 on_color_operational
, strna(operational_state
), off_color_operational
);
818 (void) dump_addresses(rtnl
, " Address: ", 0);
819 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", 0);
821 (void) sd_network_get_dns(&dns
);
822 dump_list(" DNS: ", dns
);
824 (void) sd_network_get_search_domains(&search_domains
);
825 dump_list("Search Domains: ", search_domains
);
827 (void) sd_network_get_route_domains(&route_domains
);
828 dump_list(" Route Domains: ", route_domains
);
830 (void) sd_network_get_ntp(&ntp
);
831 dump_list(" NTP: ", ntp
);
836 static int link_status(int argc
, char *argv
[], void *userdata
) {
837 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
838 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
839 _cleanup_free_ LinkInfo
*links
= NULL
;
842 (void) pager_open(arg_pager_flags
);
844 r
= sd_netlink_open(&rtnl
);
846 return log_error_errno(r
, "Failed to connect to netlink: %m");
848 r
= sd_hwdb_new(&hwdb
);
850 log_debug_errno(r
, "Failed to open hardware database: %m");
853 c
= acquire_link_info(rtnl
, NULL
, &links
);
855 return system_status(rtnl
, hwdb
);
857 c
= acquire_link_info(rtnl
, argv
+ 1, &links
);
861 for (i
= 0; i
< c
; i
++) {
865 link_status_one(rtnl
, hwdb
, links
+ i
);
871 static char *lldp_capabilities_to_string(uint16_t x
) {
872 static const char characters
[] = {
873 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
878 ret
= new(char, ELEMENTSOF(characters
) + 1);
882 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
883 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
889 static void lldp_capabilities_legend(uint16_t x
) {
890 unsigned w
, i
, cols
= columns();
891 static const char* const table
[] = {
895 "w - WLAN Access Point",
898 "d - DOCSIS cable device",
902 "m - Two-port MAC Relay (TPMR)",
908 printf("\nCapability Flags:\n");
909 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
910 if (x
& (1U << i
) || arg_all
) {
913 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
916 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
921 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
922 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
923 _cleanup_free_ LinkInfo
*links
= NULL
;
927 r
= sd_netlink_open(&rtnl
);
929 return log_error_errno(r
, "Failed to connect to netlink: %m");
931 c
= acquire_link_info(rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
935 (void) pager_open(arg_pager_flags
);
938 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
946 for (i
= 0; i
< c
; i
++) {
947 _cleanup_fclose_
FILE *f
= NULL
;
949 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
953 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
958 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
;
959 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
, *capabilities
= NULL
;
960 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
963 r
= next_lldp_neighbor(f
, &n
);
965 log_warning_errno(r
, "Failed to read neighbor data: %m");
971 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
972 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
973 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
974 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
977 cid
= ellipsize(chassis_id
, 17, 100);
983 pid
= ellipsize(port_id
, 17, 100);
989 sname
= ellipsize(system_name
, 16, 100);
994 if (port_description
) {
995 pdesc
= ellipsize(port_description
, 16, 100);
997 port_description
= pdesc
;
1000 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
1001 capabilities
= lldp_capabilities_to_string(cc
);
1005 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
1009 strna(capabilities
),
1011 strna(port_description
));
1018 lldp_capabilities_legend(all
);
1019 printf("\n%i neighbors listed.\n", m
);
1025 static int help(void) {
1026 _cleanup_free_
char *link
= NULL
;
1029 r
= terminal_urlify_man("networkctl", "1", &link
);
1033 printf("%s [OPTIONS...]\n\n"
1034 "Query and control the networking subsystem.\n\n"
1035 " -h --help Show this help\n"
1036 " --version Show package version\n"
1037 " --no-pager Do not pipe output into a pager\n"
1038 " --no-legend Do not show the headers and footers\n"
1039 " -a --all Show status for all links\n\n"
1041 " list [PATTERN...] List links\n"
1042 " status [PATTERN...] Show link status\n"
1043 " lldp [PATTERN...] Show LLDP neighbors\n"
1044 " label Show current address label entries in the kernel\n"
1045 "\nSee the %s for details.\n"
1046 , program_invocation_short_name
1053 static int parse_argv(int argc
, char *argv
[]) {
1056 ARG_VERSION
= 0x100,
1061 static const struct option options
[] = {
1062 { "help", no_argument
, NULL
, 'h' },
1063 { "version", no_argument
, NULL
, ARG_VERSION
},
1064 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1065 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1066 { "all", no_argument
, NULL
, 'a' },
1075 while ((c
= getopt_long(argc
, argv
, "ha", options
, NULL
)) >= 0) {
1086 arg_pager_flags
|= PAGER_DISABLE
;
1101 assert_not_reached("Unhandled option");
1108 static int networkctl_main(int argc
, char *argv
[]) {
1109 static const Verb verbs
[] = {
1110 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
1111 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
1112 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
1113 { "label", VERB_ANY
, VERB_ANY
, 0, list_address_labels
},
1117 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1120 static void warn_networkd_missing(void) {
1122 if (access("/run/systemd/netif/state", F_OK
) >= 0)
1125 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1128 static int run(int argc
, char* argv
[]) {
1131 log_show_color(true);
1132 log_parse_environment();
1135 r
= parse_argv(argc
, argv
);
1139 warn_networkd_missing();
1141 return networkctl_main(argc
, argv
);
1144 DEFINE_MAIN_FUNCTION(run
);