2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/if_addrlabel.h>
25 #include "sd-device.h"
28 #include "sd-netlink.h"
29 #include "sd-network.h"
31 #include "alloc-util.h"
32 #include "arphrd-list.h"
33 #include "device-util.h"
34 #include "ether-addr-util.h"
36 #include "hwdb-util.h"
37 #include "local-addresses.h"
38 #include "locale-util.h"
40 #include "netlink-util.h"
42 #include "parse-util.h"
43 #include "socket-util.h"
44 #include "sparse-endian.h"
45 #include "stdio-util.h"
46 #include "string-table.h"
47 #include "string-util.h"
50 #include "terminal-util.h"
54 static bool arg_no_pager
= false;
55 static bool arg_legend
= true;
56 static bool arg_all
= false;
58 static int link_get_type_string(unsigned short iftype
, sd_device
*d
, char **ret
) {
64 if (iftype
== ARPHRD_ETHER
&& d
) {
65 const char *devtype
= NULL
, *id
= NULL
;
66 /* WLANs have iftype ARPHRD_ETHER, but we want
67 * to show a more useful type string for
70 (void) sd_device_get_devtype(d
, &devtype
);
72 if (streq_ptr(devtype
, "wlan"))
74 else if (streq_ptr(devtype
, "wwan"))
87 t
= arphrd_to_name(iftype
);
103 static void operational_state_to_color(const char *state
, const char **on
, const char **off
) {
107 if (streq_ptr(state
, "routable")) {
108 *on
= ansi_highlight_green();
109 *off
= ansi_normal();
110 } else if (streq_ptr(state
, "degraded")) {
111 *on
= ansi_highlight_yellow();
112 *off
= ansi_normal();
117 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
121 if (streq_ptr(state
, "configured")) {
122 *on
= ansi_highlight_green();
123 *off
= ansi_normal();
124 } else if (streq_ptr(state
, "configuring")) {
125 *on
= ansi_highlight_yellow();
126 *off
= ansi_normal();
127 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
128 *on
= ansi_highlight_red();
129 *off
= ansi_normal();
134 typedef struct LinkInfo
{
135 char name
[IFNAMSIZ
+1];
137 unsigned short iftype
;
138 struct ether_addr mac_address
;
141 bool has_mac_address
:1;
145 static int link_info_compare(const void *a
, const void *b
) {
146 const LinkInfo
*x
= a
, *y
= b
;
148 return x
->ifindex
- y
->ifindex
;
151 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
) {
159 r
= sd_netlink_message_get_type(m
, &type
);
163 if (type
!= RTM_NEWLINK
)
166 r
= sd_rtnl_message_link_get_ifindex(m
, &info
->ifindex
);
170 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
174 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
178 strscpy(info
->name
, sizeof info
->name
, name
);
180 info
->has_mac_address
=
181 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
182 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
185 sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
) &&
191 static int acquire_link_info_strv(sd_netlink
*rtnl
, char **l
, LinkInfo
**ret
) {
192 _cleanup_free_ LinkInfo
*links
= NULL
;
200 links
= new(LinkInfo
, strv_length(l
));
205 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
208 if (parse_ifindex(*i
, &ifindex
) >= 0)
209 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, ifindex
);
211 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
213 return rtnl_log_create_error(r
);
215 r
= sd_netlink_message_append_string(req
, IFLA_IFNAME
, *i
);
218 return rtnl_log_create_error(r
);
220 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
222 return log_error_errno(r
, "Failed to request link: %m");
224 r
= decode_link(reply
, links
+ c
);
231 qsort_safe(links
, c
, sizeof(LinkInfo
), link_info_compare
);
239 static int acquire_link_info_all(sd_netlink
*rtnl
, LinkInfo
**ret
) {
240 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
241 _cleanup_free_ LinkInfo
*links
= NULL
;
242 size_t allocated
= 0, c
= 0;
243 sd_netlink_message
*i
;
249 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
251 return rtnl_log_create_error(r
);
253 r
= sd_netlink_message_request_dump(req
, true);
255 return rtnl_log_create_error(r
);
257 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
259 return log_error_errno(r
, "Failed to enumerate links: %m");
261 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
262 if (!GREEDY_REALLOC(links
, allocated
, c
+1))
265 r
= decode_link(i
, links
+ c
);
272 qsort_safe(links
, c
, sizeof(LinkInfo
), link_info_compare
);
280 static int list_links(int argc
, char *argv
[], void *userdata
) {
281 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
282 _cleanup_free_ LinkInfo
*links
= NULL
;
285 r
= sd_netlink_open(&rtnl
);
287 return log_error_errno(r
, "Failed to connect to netlink: %m");
290 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
292 c
= acquire_link_info_all(rtnl
, &links
);
296 pager_open(arg_no_pager
, false);
299 printf("%3s %-16s %-18s %-11s %-10s\n",
306 for (i
= 0; i
< c
; i
++) {
307 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
308 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
309 const char *on_color_operational
, *off_color_operational
,
310 *on_color_setup
, *off_color_setup
;
311 char devid
[2 + DECIMAL_STR_MAX(int)];
312 _cleanup_free_
char *t
= NULL
;
314 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
315 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
317 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
318 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
319 setup_state
= strdup("unmanaged");
320 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
322 xsprintf(devid
, "n%i", links
[i
].ifindex
);
323 (void) sd_device_new_from_device_id(&d
, devid
);
325 (void) link_get_type_string(links
[i
].iftype
, d
, &t
);
327 printf("%3i %-16s %-18s %s%-11s%s %s%-10s%s\n",
328 links
[i
].ifindex
, links
[i
].name
, strna(t
),
329 on_color_operational
, strna(operational_state
), off_color_operational
,
330 on_color_setup
, strna(setup_state
), off_color_setup
);
334 printf("\n%i links listed.\n", c
);
339 /* IEEE Organizationally Unique Identifier vendor string */
340 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
341 const char *description
;
342 char modalias
[strlen("OUI:XXYYXXYYXXYY") + 1], *desc
;
353 /* skip commonly misused 00:00:00 (Xerox) prefix */
354 if (memcmp(mac
, "\0\0\0", 3) == 0)
357 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
358 ETHER_ADDR_FORMAT_VAL(*mac
));
360 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
364 desc
= strdup(description
);
373 static int get_gateway_description(
378 union in_addr_union
*gateway
,
379 char **gateway_description
) {
380 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
381 sd_netlink_message
*m
;
385 assert(ifindex
>= 0);
386 assert(IN_SET(family
, AF_INET
, AF_INET6
));
388 assert(gateway_description
);
390 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
394 r
= sd_netlink_message_request_dump(req
, true);
398 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
402 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
403 union in_addr_union gw
= {};
404 struct ether_addr mac
= {};
408 r
= sd_netlink_message_get_errno(m
);
410 log_error_errno(r
, "got error: %m");
414 r
= sd_netlink_message_get_type(m
, &type
);
416 log_error_errno(r
, "could not get type: %m");
420 if (type
!= RTM_NEWNEIGH
) {
421 log_error("type is not RTM_NEWNEIGH");
425 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
427 log_error_errno(r
, "could not get family: %m");
432 log_error("family is not correct");
436 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
438 log_error_errno(r
, "could not get ifindex: %m");
442 if (ifindex
> 0 && ifi
!= ifindex
)
447 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
453 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
462 if (!in_addr_equal(fam
, &gw
, gateway
))
465 r
= sd_netlink_message_read_ether_addr(m
, NDA_LLADDR
, &mac
);
469 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
479 static int dump_gateways(
484 _cleanup_free_
struct local_address
*local
= NULL
;
490 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
494 for (i
= 0; i
< n
; i
++) {
495 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
;
497 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
501 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
503 log_debug_errno(r
, "Could not get description of gateway: %m");
506 (int) strlen(prefix
),
507 i
== 0 ? prefix
: "",
511 printf(" (%s)", description
);
513 /* Show interface name for the entry if we show
514 * entries for all interfaces */
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_addresses(
536 _cleanup_free_
struct local_address
*local
= NULL
;
542 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
546 for (i
= 0; i
< n
; i
++) {
547 _cleanup_free_
char *pretty
= NULL
;
549 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
554 (int) strlen(prefix
),
555 i
== 0 ? prefix
: "",
559 char name
[IF_NAMESIZE
+1];
561 if (if_indextoname(local
[i
].ifindex
, name
)) {
562 fputs(" on ", stdout
);
565 printf(" on %%%i", local
[i
].ifindex
);
574 static int dump_address_labels(sd_netlink
*rtnl
) {
575 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
576 sd_netlink_message
*m
;
581 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
583 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
585 r
= sd_netlink_message_request_dump(req
, true);
589 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
593 printf("%10s/%s %30s\n", "Prefix", "Prefixlen", "Label");
595 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
596 _cleanup_free_
char *pretty
= NULL
;
597 union in_addr_union prefix
= {};
601 r
= sd_netlink_message_get_errno(m
);
603 log_error_errno(r
, "got error: %m");
607 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
608 if (r
< 0 && r
!= -ENODATA
) {
609 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
613 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
617 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
621 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
625 printf("%10s/%-5u %30u\n", pretty
, prefixlen
, label
);
631 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
632 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
635 r
= sd_netlink_open(&rtnl
);
637 return log_error_errno(r
, "Failed to connect to netlink: %m");
639 dump_address_labels(rtnl
);
644 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
645 _cleanup_free_
char *p
= NULL
;
648 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
659 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
660 _cleanup_free_
void *raw
= NULL
;
668 l
= fread(&u
, 1, sizeof(u
), f
);
669 if (l
== 0 && feof(f
))
674 raw
= new(uint8_t, le64toh(u
));
678 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
681 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
688 static int dump_lldp_neighbors(const char *prefix
, int ifindex
) {
689 _cleanup_fclose_
FILE *f
= NULL
;
695 r
= open_lldp_neighbors(ifindex
, &f
);
700 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
701 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
703 r
= next_lldp_neighbor(f
, &n
);
710 (int) strlen(prefix
),
711 c
== 0 ? prefix
: "");
713 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
714 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
715 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
717 printf("%s on port %s", strna(system_name
), strna(port_id
));
719 if (!isempty(port_description
))
720 printf(" (%s)", port_description
);
730 static void dump_ifindexes(const char *prefix
, const int *ifindexes
) {
735 if (!ifindexes
|| ifindexes
[0] <= 0)
738 for (c
= 0; ifindexes
[c
] > 0; c
++) {
739 char name
[IF_NAMESIZE
+1];
742 (int) strlen(prefix
),
743 c
== 0 ? prefix
: "");
745 if (if_indextoname(ifindexes
[c
], name
))
748 printf("%i", ifindexes
[c
]);
754 static void dump_list(const char *prefix
, char **l
) {
762 (int) strlen(prefix
),
763 i
== l
? prefix
: "",
768 static int link_status_one(
771 const LinkInfo
*info
) {
773 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
774 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
, *tz
= NULL
;
775 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
776 char devid
[2 + DECIMAL_STR_MAX(int)];
777 _cleanup_free_
char *t
= NULL
, *network
= NULL
;
778 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
;
779 const char *on_color_operational
, *off_color_operational
,
780 *on_color_setup
, *off_color_setup
;
781 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
787 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
788 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
790 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
791 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
792 setup_state
= strdup("unmanaged");
793 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
795 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
796 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
797 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
798 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
800 xsprintf(devid
, "n%i", info
->ifindex
);
802 (void) sd_device_new_from_device_id(&d
, devid
);
805 (void) sd_device_get_property_value(d
, "ID_NET_LINK_FILE", &link
);
806 (void) sd_device_get_property_value(d
, "ID_NET_DRIVER", &driver
);
807 (void) sd_device_get_property_value(d
, "ID_PATH", &path
);
809 r
= sd_device_get_property_value(d
, "ID_VENDOR_FROM_DATABASE", &vendor
);
811 (void) sd_device_get_property_value(d
, "ID_VENDOR", &vendor
);
813 r
= sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
);
815 (void) sd_device_get_property_value(d
, "ID_MODEL", &model
);
818 (void) link_get_type_string(info
->iftype
, d
, &t
);
820 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
822 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
823 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
825 printf("%s%s%s %i: %s\n", on_color_operational
, special_glyph(BLACK_CIRCLE
), off_color_operational
, info
->ifindex
, info
->name
);
827 printf(" Link File: %s\n"
828 " Network File: %s\n"
830 " State: %s%s%s (%s%s%s)\n",
834 on_color_operational
, strna(operational_state
), off_color_operational
,
835 on_color_setup
, strna(setup_state
), off_color_setup
);
838 printf(" Path: %s\n", path
);
840 printf(" Driver: %s\n", driver
);
842 printf(" Vendor: %s\n", vendor
);
844 printf(" Model: %s\n", model
);
846 if (info
->has_mac_address
) {
847 _cleanup_free_
char *description
= NULL
;
848 char ea
[ETHER_ADDR_TO_STRING_MAX
];
850 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
853 printf(" HW Address: %s (%s)\n", ether_addr_to_string(&info
->mac_address
, ea
), description
);
855 printf(" HW Address: %s\n", ether_addr_to_string(&info
->mac_address
, ea
));
859 printf(" MTU: %u\n", info
->mtu
);
861 (void) dump_addresses(rtnl
, " Address: ", info
->ifindex
);
862 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", info
->ifindex
);
864 dump_list(" DNS: ", dns
);
865 dump_list(" Search Domains: ", search_domains
);
866 dump_list(" Route Domains: ", route_domains
);
868 dump_list(" NTP: ", ntp
);
870 dump_ifindexes("Carrier Bound To: ", carrier_bound_to
);
871 dump_ifindexes("Carrier Bound By: ", carrier_bound_by
);
873 (void) sd_network_link_get_timezone(info
->ifindex
, &tz
);
875 printf(" Time Zone: %s\n", tz
);
877 (void) dump_lldp_neighbors(" Connected To: ", info
->ifindex
);
882 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
883 _cleanup_free_
char *operational_state
= NULL
;
884 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
885 const char *on_color_operational
, *off_color_operational
;
889 (void) sd_network_get_operational_state(&operational_state
);
890 operational_state_to_color(operational_state
, &on_color_operational
, &off_color_operational
);
892 printf("%s%s%s State: %s%s%s\n",
893 on_color_operational
, special_glyph(BLACK_CIRCLE
), off_color_operational
,
894 on_color_operational
, strna(operational_state
), off_color_operational
);
896 (void) dump_addresses(rtnl
, " Address: ", 0);
897 (void) dump_gateways(rtnl
, hwdb
, " Gateway: ", 0);
899 (void) sd_network_get_dns(&dns
);
900 dump_list(" DNS: ", dns
);
902 (void) sd_network_get_search_domains(&search_domains
);
903 dump_list("Search Domains: ", search_domains
);
905 (void) sd_network_get_route_domains(&route_domains
);
906 dump_list(" Route Domains: ", route_domains
);
908 (void) sd_network_get_ntp(&ntp
);
909 dump_list(" NTP: ", ntp
);
914 static int link_status(int argc
, char *argv
[], void *userdata
) {
915 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
916 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
917 _cleanup_free_ LinkInfo
*links
= NULL
;
920 pager_open(arg_no_pager
, false);
922 r
= sd_netlink_open(&rtnl
);
924 return log_error_errno(r
, "Failed to connect to netlink: %m");
926 r
= sd_hwdb_new(&hwdb
);
928 log_debug_errno(r
, "Failed to open hardware database: %m");
931 c
= acquire_link_info_all(rtnl
, &links
);
933 return system_status(rtnl
, hwdb
);
935 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
939 for (i
= 0; i
< c
; i
++) {
943 link_status_one(rtnl
, hwdb
, links
+ i
);
949 static char *lldp_capabilities_to_string(uint16_t x
) {
950 static const char characters
[] = {
951 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
956 ret
= new(char, ELEMENTSOF(characters
) + 1);
960 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
961 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
967 static void lldp_capabilities_legend(uint16_t x
) {
968 unsigned w
, i
, cols
= columns();
969 static const char* const table
[] = {
973 "w - WLAN Access Point",
976 "d - DOCSIS cable device",
980 "m - Two-port MAC Relay (TPMR)",
986 printf("\nCapability Flags:\n");
987 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
988 if (x
& (1U << i
) || arg_all
) {
991 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
994 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
999 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
1000 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1001 _cleanup_free_ LinkInfo
*links
= NULL
;
1005 r
= sd_netlink_open(&rtnl
);
1007 return log_error_errno(r
, "Failed to connect to netlink: %m");
1010 c
= acquire_link_info_strv(rtnl
, argv
+ 1, &links
);
1012 c
= acquire_link_info_all(rtnl
, &links
);
1016 pager_open(arg_no_pager
, false);
1019 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
1025 "PORT DESCRIPTION");
1027 for (i
= 0; i
< c
; i
++) {
1028 _cleanup_fclose_
FILE *f
= NULL
;
1030 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
1034 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
1039 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
;
1040 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
, *capabilities
= NULL
;
1041 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1044 r
= next_lldp_neighbor(f
, &n
);
1046 log_warning_errno(r
, "Failed to read neighbor data: %m");
1052 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
1053 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1054 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1055 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1058 cid
= ellipsize(chassis_id
, 17, 100);
1064 pid
= ellipsize(port_id
, 17, 100);
1070 sname
= ellipsize(system_name
, 16, 100);
1072 system_name
= sname
;
1075 if (port_description
) {
1076 pdesc
= ellipsize(port_description
, 16, 100);
1078 port_description
= pdesc
;
1081 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
1082 capabilities
= lldp_capabilities_to_string(cc
);
1086 printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
1090 strna(capabilities
),
1092 strna(port_description
));
1099 lldp_capabilities_legend(all
);
1100 printf("\n%i neighbors listed.\n", m
);
1106 static void help(void) {
1107 printf("%s [OPTIONS...]\n\n"
1108 "Query and control the networking subsystem.\n\n"
1109 " -h --help Show this help\n"
1110 " --version Show package version\n"
1111 " --no-pager Do not pipe output into a pager\n"
1112 " --no-legend Do not show the headers and footers\n"
1113 " -a --all Show status for all links\n\n"
1115 " list [LINK...] List links\n"
1116 " status [LINK...] Show link status\n"
1117 " lldp [LINK...] Show LLDP neighbors\n"
1118 " label Show current address label entries in the kernel\n"
1119 , program_invocation_short_name
);
1122 static int parse_argv(int argc
, char *argv
[]) {
1125 ARG_VERSION
= 0x100,
1130 static const struct option options
[] = {
1131 { "help", no_argument
, NULL
, 'h' },
1132 { "version", no_argument
, NULL
, ARG_VERSION
},
1133 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1134 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1135 { "all", no_argument
, NULL
, 'a' },
1144 while ((c
= getopt_long(argc
, argv
, "ha", options
, NULL
)) >= 0) {
1156 arg_no_pager
= true;
1171 assert_not_reached("Unhandled option");
1178 static int networkctl_main(int argc
, char *argv
[]) {
1179 const Verb verbs
[] = {
1180 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
1181 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
1182 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
1183 { "label", VERB_ANY
, VERB_ANY
, 0, list_address_labels
},
1187 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1190 static void warn_networkd_missing(void) {
1192 if (access("/run/systemd/netif/state", F_OK
) >= 0)
1195 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1198 int main(int argc
, char* argv
[]) {
1201 log_parse_environment();
1204 r
= parse_argv(argc
, argv
);
1208 warn_networkd_missing();
1210 r
= networkctl_main(argc
, argv
);
1215 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;