1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <linux/if_addrlabel.h>
11 #include <linux/if_bridge.h>
12 #include <linux/if_tunnel.h>
15 #include "sd-device.h"
16 #include "sd-dhcp-client.h"
19 #include "sd-netlink.h"
20 #include "sd-network.h"
22 #include "alloc-util.h"
23 #include "bond-util.h"
24 #include "bridge-util.h"
25 #include "bus-common-errors.h"
26 #include "bus-error.h"
27 #include "bus-locator.h"
28 #include "device-util.h"
30 #include "ether-addr-util.h"
31 #include "ethtool-util.h"
33 #include "format-table.h"
34 #include "format-util.h"
35 #include "geneve-util.h"
36 #include "glob-util.h"
37 #include "hwdb-util.h"
38 #include "ipvlan-util.h"
39 #include "local-addresses.h"
40 #include "locale-util.h"
41 #include "logs-show.h"
43 #include "macvlan-util.h"
44 #include "main-func.h"
45 #include "netlink-util.h"
46 #include "network-internal.h"
48 #include "parse-util.h"
49 #include "pretty-print.h"
51 #include "socket-netlink.h"
52 #include "socket-util.h"
53 #include "sort-util.h"
54 #include "sparse-endian.h"
55 #include "stdio-util.h"
56 #include "string-table.h"
57 #include "string-util.h"
60 #include "terminal-util.h"
63 #include "wifi-util.h"
65 /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */
66 #define NETDEV_KIND_MAX 64
68 /* use 128 kB for receive socket kernel queue, we shouldn't need more here */
69 #define RCVBUF_SIZE (128*1024)
71 static PagerFlags arg_pager_flags
= 0;
72 static bool arg_legend
= true;
73 static bool arg_all
= false;
74 static bool arg_stats
= false;
75 static bool arg_full
= false;
76 static unsigned arg_lines
= 10;
78 static void operational_state_to_color(const char *name
, const char *state
, const char **on
, const char **off
) {
82 if (STRPTR_IN_SET(state
, "routable", "enslaved") ||
83 (streq_ptr(name
, "lo") && streq_ptr(state
, "carrier"))) {
84 *on
= ansi_highlight_green();
86 } else if (streq_ptr(state
, "degraded")) {
87 *on
= ansi_highlight_yellow();
93 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
97 if (streq_ptr(state
, "configured")) {
98 *on
= ansi_highlight_green();
100 } else if (streq_ptr(state
, "configuring")) {
101 *on
= ansi_highlight_yellow();
102 *off
= ansi_normal();
103 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
104 *on
= ansi_highlight_red();
105 *off
= ansi_normal();
110 typedef struct VxLanInfo
{
117 union in_addr_union local
;
118 union in_addr_union group
;
132 typedef struct LinkInfo
{
133 char name
[IFNAMSIZ
+1];
134 char netdev_kind
[NETDEV_KIND_MAX
];
135 sd_device
*sd_device
;
137 unsigned short iftype
;
138 struct ether_addr mac_address
;
139 struct ether_addr permanent_mac_address
;
146 uint8_t addr_gen_mode
;
148 char **alternative_names
;
151 struct rtnl_link_stats64 stats64
;
152 struct rtnl_link_stats stats
;
159 uint32_t forward_delay
;
162 uint32_t ageing_time
;
166 uint8_t mcast_igmp_version
;
170 VxLanInfo vxlan_info
;
183 uint16_t tunnel_port
;
186 union in_addr_union local
;
187 union in_addr_union remote
;
195 /* macvlan and macvtap info */
196 uint32_t macvlan_mode
;
199 uint16_t ipvlan_mode
;
200 uint16_t ipvlan_flags
;
209 enum nl80211_iftype wlan_iftype
;
211 struct ether_addr bssid
;
213 bool has_mac_address
:1;
214 bool has_permanent_mac_address
:1;
215 bool has_tx_queues
:1;
216 bool has_rx_queues
:1;
220 bool has_ethtool_link_info
:1;
221 bool has_wlan_link_info
:1;
222 bool has_tunnel_ipv4
:1;
223 bool has_ipv6_address_generation_mode
:1;
225 bool needs_freeing
:1;
228 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
229 return CMP(a
->ifindex
, b
->ifindex
);
232 static const LinkInfo
* link_info_array_free(LinkInfo
*array
) {
233 for (unsigned i
= 0; array
&& array
[i
].needs_freeing
; i
++) {
234 sd_device_unref(array
[i
].sd_device
);
236 free(array
[i
].qdisc
);
237 strv_free(array
[i
].alternative_names
);
242 DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo
*, link_info_array_free
);
244 static int decode_netdev(sd_netlink_message
*m
, LinkInfo
*info
) {
245 const char *received_kind
;
251 r
= sd_netlink_message_enter_container(m
, IFLA_LINKINFO
);
255 r
= sd_netlink_message_read_string(m
, IFLA_INFO_KIND
, &received_kind
);
259 r
= sd_netlink_message_enter_container(m
, IFLA_INFO_DATA
);
263 if (streq(received_kind
, "bridge")) {
264 (void) sd_netlink_message_read_u32(m
, IFLA_BR_FORWARD_DELAY
, &info
->forward_delay
);
265 (void) sd_netlink_message_read_u32(m
, IFLA_BR_HELLO_TIME
, &info
->hello_time
);
266 (void) sd_netlink_message_read_u32(m
, IFLA_BR_MAX_AGE
, &info
->max_age
);
267 (void) sd_netlink_message_read_u32(m
, IFLA_BR_AGEING_TIME
, &info
->ageing_time
);
268 (void) sd_netlink_message_read_u32(m
, IFLA_BR_STP_STATE
, &info
->stp_state
);
269 (void) sd_netlink_message_read_u32(m
, IFLA_BRPORT_COST
, &info
->cost
);
270 (void) sd_netlink_message_read_u16(m
, IFLA_BR_PRIORITY
, &info
->priority
);
271 (void) sd_netlink_message_read_u8(m
, IFLA_BR_MCAST_IGMP_VERSION
, &info
->mcast_igmp_version
);
272 (void) sd_netlink_message_read_u8(m
, IFLA_BRPORT_STATE
, &info
->port_state
);
273 } if (streq(received_kind
, "bond")) {
274 (void) sd_netlink_message_read_u8(m
, IFLA_BOND_MODE
, &info
->mode
);
275 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_MIIMON
, &info
->miimon
);
276 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_DOWNDELAY
, &info
->downdelay
);
277 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_UPDELAY
, &info
->updelay
);
278 } else if (streq(received_kind
, "vxlan")) {
279 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_ID
, &info
->vxlan_info
.vni
);
281 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_GROUP
, &info
->vxlan_info
.group
.in
);
283 info
->vxlan_info
.group_family
= AF_INET
;
285 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_GROUP6
, &info
->vxlan_info
.group
.in6
);
287 info
->vxlan_info
.group_family
= AF_INET6
;
290 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_LOCAL
, &info
->vxlan_info
.local
.in
);
292 info
->vxlan_info
.local_family
= AF_INET
;
294 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_LOCAL6
, &info
->vxlan_info
.local
.in6
);
296 info
->vxlan_info
.local_family
= AF_INET6
;
299 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_LINK
, &info
->vxlan_info
.link
);
300 (void) sd_netlink_message_read_u16(m
, IFLA_VXLAN_PORT
, &info
->vxlan_info
.dest_port
);
301 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_PROXY
, &info
->vxlan_info
.proxy
);
302 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_LEARNING
, &info
->vxlan_info
.learning
);
303 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_RSC
, &info
->vxlan_info
.rsc
);
304 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L3MISS
, &info
->vxlan_info
.l3miss
);
305 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L2MISS
, &info
->vxlan_info
.l2miss
);
306 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TOS
, &info
->vxlan_info
.tos
);
307 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TTL
, &info
->vxlan_info
.ttl
);
308 } else if (streq(received_kind
, "vlan"))
309 (void) sd_netlink_message_read_u16(m
, IFLA_VLAN_ID
, &info
->vlan_id
);
310 else if (STR_IN_SET(received_kind
, "ipip", "sit")) {
311 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_LOCAL
, &info
->local
.in
);
312 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_REMOTE
, &info
->remote
.in
);
313 } else if (streq(received_kind
, "geneve")) {
314 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_ID
, &info
->vni
);
316 r
= sd_netlink_message_read_in_addr(m
, IFLA_GENEVE_REMOTE
, &info
->remote
.in
);
318 info
->has_tunnel_ipv4
= true;
320 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GENEVE_REMOTE6
, &info
->remote
.in6
);
322 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL
, &info
->ttl
);
323 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL_INHERIT
, &info
->inherit
);
324 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TOS
, &info
->tos
);
325 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_DF
, &info
->df
);
326 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_CSUM
, &info
->csum
);
327 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
, &info
->csum6_tx
);
328 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
, &info
->csum6_rx
);
329 (void) sd_netlink_message_read_u16(m
, IFLA_GENEVE_PORT
, &info
->tunnel_port
);
330 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_LABEL
, &info
->label
);
331 } else if (STR_IN_SET(received_kind
, "gre", "gretap", "erspan")) {
332 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in
);
333 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in
);
334 } else if (STR_IN_SET(received_kind
, "ip6gre", "ip6gretap", "ip6erspan")) {
335 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in6
);
336 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in6
);
337 } else if (streq(received_kind
, "vti")) {
338 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in
);
339 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in
);
340 } else if (streq(received_kind
, "vti6")) {
341 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in6
);
342 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in6
);
343 } else if (STR_IN_SET(received_kind
, "macvlan", "macvtap"))
344 (void) sd_netlink_message_read_u32(m
, IFLA_MACVLAN_MODE
, &info
->macvlan_mode
);
345 else if (streq(received_kind
, "ipvlan")) {
346 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_MODE
, &info
->ipvlan_mode
);
347 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_FLAGS
, &info
->ipvlan_flags
);
350 strncpy(info
->netdev_kind
, received_kind
, IFNAMSIZ
);
352 (void) sd_netlink_message_exit_container(m
);
353 (void) sd_netlink_message_exit_container(m
);
358 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
, char **patterns
, bool matched_patterns
[]) {
359 _cleanup_strv_free_
char **altnames
= NULL
;
360 const char *name
, *qdisc
;
367 r
= sd_netlink_message_get_type(m
, &type
);
371 if (type
!= RTM_NEWLINK
)
374 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
378 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
382 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &altnames
);
383 if (r
< 0 && r
!= -ENODATA
)
387 char str
[DECIMAL_STR_MAX(int)];
390 assert(matched_patterns
);
392 xsprintf(str
, "%i", ifindex
);
393 if (!strv_fnmatch_full(patterns
, str
, 0, &pos
) &&
394 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
398 STRV_FOREACH(p
, altnames
)
399 if (strv_fnmatch_full(patterns
, *p
, 0, &pos
)) {
407 matched_patterns
[pos
] = true;
410 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
414 strscpy(info
->name
, sizeof info
->name
, name
);
415 info
->ifindex
= ifindex
;
416 info
->alternative_names
= TAKE_PTR(altnames
);
418 info
->has_mac_address
=
419 sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &info
->mac_address
) >= 0 &&
420 memcmp(&info
->mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0;
422 info
->has_permanent_mac_address
=
423 ethtool_get_permanent_macaddr(NULL
, info
->name
, &info
->permanent_mac_address
) >= 0 &&
424 memcmp(&info
->permanent_mac_address
, ÐER_ADDR_NULL
, sizeof(struct ether_addr
)) != 0 &&
425 memcmp(&info
->permanent_mac_address
, &info
->mac_address
, sizeof(struct ether_addr
)) != 0;
427 (void) sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
);
428 (void) sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
);
429 (void) sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
);
431 info
->has_rx_queues
=
432 sd_netlink_message_read_u32(m
, IFLA_NUM_RX_QUEUES
, &info
->rx_queues
) >= 0 &&
435 info
->has_tx_queues
=
436 sd_netlink_message_read_u32(m
, IFLA_NUM_TX_QUEUES
, &info
->tx_queues
) >= 0 &&
439 if (sd_netlink_message_read(m
, IFLA_STATS64
, sizeof info
->stats64
, &info
->stats64
) >= 0)
440 info
->has_stats64
= true;
441 else if (sd_netlink_message_read(m
, IFLA_STATS
, sizeof info
->stats
, &info
->stats
) >= 0)
442 info
->has_stats
= true;
444 r
= sd_netlink_message_read_string(m
, IFLA_QDISC
, &qdisc
);
446 info
->qdisc
= strdup(qdisc
);
451 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, &info
->master
);
453 r
= sd_netlink_message_enter_container(m
, IFLA_AF_SPEC
);
455 r
= sd_netlink_message_enter_container(m
, AF_INET6
);
457 r
= sd_netlink_message_read_u8(m
, IFLA_INET6_ADDR_GEN_MODE
, &info
->addr_gen_mode
);
458 if (r
>= 0 && IN_SET(info
->addr_gen_mode
,
459 IN6_ADDR_GEN_MODE_EUI64
,
460 IN6_ADDR_GEN_MODE_NONE
,
461 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
,
462 IN6_ADDR_GEN_MODE_RANDOM
))
463 info
->has_ipv6_address_generation_mode
= true;
465 (void) sd_netlink_message_exit_container(m
);
467 (void) sd_netlink_message_exit_container(m
);
471 (void) decode_netdev(m
, info
);
476 static int link_get_property(
478 const LinkInfo
*link
,
480 sd_bus_message
**reply
,
482 const char *propname
) {
483 _cleanup_free_
char *path
= NULL
, *ifindex_str
= NULL
;
486 if (asprintf(&ifindex_str
, "%i", link
->ifindex
) < 0)
489 r
= sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str
, &path
);
493 return sd_bus_call_method(
495 "org.freedesktop.network1",
497 "org.freedesktop.DBus.Properties",
506 static int acquire_link_bitrates(sd_bus
*bus
, LinkInfo
*link
) {
507 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
508 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
511 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.Link", "BitRates");
513 bool quiet
= sd_bus_error_has_names(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
,
514 BUS_ERROR_SPEED_METER_INACTIVE
);
516 return log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
517 r
, "Failed to query link bit rates: %s", bus_error_message(&error
, r
));
520 r
= sd_bus_message_enter_container(reply
, 'v', "(tt)");
522 return bus_log_parse_error(r
);
524 r
= sd_bus_message_read(reply
, "(tt)", &link
->tx_bitrate
, &link
->rx_bitrate
);
526 return bus_log_parse_error(r
);
528 r
= sd_bus_message_exit_container(reply
);
530 return bus_log_parse_error(r
);
532 link
->has_bitrates
= link
->tx_bitrate
!= UINT64_MAX
&& link
->rx_bitrate
!= UINT64_MAX
;
537 static void acquire_ether_link_info(int *fd
, LinkInfo
*link
) {
538 if (ethtool_get_link_info(fd
, link
->name
,
539 &link
->autonegotiation
,
543 link
->has_ethtool_link_info
= true;
546 static void acquire_wlan_link_info(LinkInfo
*link
) {
547 _cleanup_(sd_netlink_unrefp
) sd_netlink
*genl
= NULL
;
548 const char *type
= NULL
;
552 (void) sd_device_get_devtype(link
->sd_device
, &type
);
553 if (!streq_ptr(type
, "wlan"))
556 r
= sd_genl_socket_open(&genl
);
558 log_debug_errno(r
, "Failed to open generic netlink socket: %m");
562 (void) sd_netlink_inc_rcvbuf(genl
, RCVBUF_SIZE
);
564 r
= wifi_get_interface(genl
, link
->ifindex
, &link
->wlan_iftype
, &link
->ssid
);
566 log_debug_errno(r
, "%s: failed to query ssid: %m", link
->name
);
568 if (link
->wlan_iftype
== NL80211_IFTYPE_STATION
) {
569 k
= wifi_get_station(genl
, link
->ifindex
, &link
->bssid
);
571 log_debug_errno(k
, "%s: failed to query bssid: %m", link
->name
);
574 link
->has_wlan_link_info
= r
> 0 || k
> 0;
577 static int acquire_link_info(sd_bus
*bus
, sd_netlink
*rtnl
, char **patterns
, LinkInfo
**ret
) {
578 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
579 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
580 _cleanup_close_
int fd
= -1;
581 size_t allocated
= 0, c
= 0, j
;
582 sd_netlink_message
*i
;
588 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
590 return rtnl_log_create_error(r
);
592 r
= sd_netlink_message_request_dump(req
, true);
594 return rtnl_log_create_error(r
);
596 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
598 return log_error_errno(r
, "Failed to enumerate links: %m");
600 _cleanup_free_
bool *matched_patterns
= NULL
;
602 matched_patterns
= new0(bool, strv_length(patterns
));
603 if (!matched_patterns
)
607 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
608 if (!GREEDY_REALLOC0(links
, allocated
, c
+ 2)) /* We keep one trailing one as marker */
611 r
= decode_link(i
, links
+ c
, patterns
, matched_patterns
);
617 links
[c
].needs_freeing
= true;
619 char devid
[2 + DECIMAL_STR_MAX(int)];
620 xsprintf(devid
, "n%i", links
[c
].ifindex
);
621 (void) sd_device_new_from_device_id(&links
[c
].sd_device
, devid
);
623 acquire_ether_link_info(&fd
, &links
[c
]);
624 acquire_wlan_link_info(&links
[c
]);
629 /* Look if we matched all our arguments that are not globs. It
630 * is OK for a glob to match nothing, but not for an exact argument. */
631 for (size_t pos
= 0; pos
< strv_length(patterns
); pos
++) {
632 if (matched_patterns
[pos
])
635 if (string_is_glob(patterns
[pos
]))
636 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
639 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
640 "Interface \"%s\" not found.", patterns
[pos
]);
643 typesafe_qsort(links
, c
, link_info_compare
);
646 for (j
= 0; j
< c
; j
++)
647 (void) acquire_link_bitrates(bus
, links
+ j
);
649 *ret
= TAKE_PTR(links
);
651 if (patterns
&& c
== 0)
652 log_warning("No interfaces matched.");
657 static int list_links(int argc
, char *argv
[], void *userdata
) {
658 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
659 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
660 _cleanup_(table_unrefp
) Table
*table
= NULL
;
664 r
= sd_netlink_open(&rtnl
);
666 return log_error_errno(r
, "Failed to connect to netlink: %m");
668 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
672 (void) pager_open(arg_pager_flags
);
674 table
= table_new("idx", "link", "type", "operational", "setup");
679 table_set_width(table
, 0);
681 table_set_header(table
, arg_legend
);
683 assert_se(cell
= table_get_cell(table
, 0, 0));
684 (void) table_set_minimum_width(table
, cell
, 3);
685 (void) table_set_weight(table
, cell
, 0);
686 (void) table_set_ellipsize_percent(table
, cell
, 100);
687 (void) table_set_align_percent(table
, cell
, 100);
689 assert_se(cell
= table_get_cell(table
, 0, 1));
690 (void) table_set_ellipsize_percent(table
, cell
, 100);
692 for (i
= 0; i
< c
; i
++) {
693 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
694 const char *on_color_operational
, *off_color_operational
,
695 *on_color_setup
, *off_color_setup
;
696 _cleanup_free_
char *t
= NULL
;
698 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
699 operational_state_to_color(links
[i
].name
, operational_state
, &on_color_operational
, &off_color_operational
);
701 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
702 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
703 setup_state
= strdup("unmanaged");
704 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
706 t
= link_get_type_string(links
[i
].iftype
, links
[i
].sd_device
);
708 r
= table_add_many(table
,
709 TABLE_INT
, links
[i
].ifindex
,
710 TABLE_STRING
, links
[i
].name
,
711 TABLE_STRING
, strna(t
),
712 TABLE_STRING
, strna(operational_state
),
713 TABLE_SET_COLOR
, on_color_operational
,
714 TABLE_STRING
, strna(setup_state
),
715 TABLE_SET_COLOR
, on_color_setup
);
717 return table_log_add_error(r
);
720 r
= table_print(table
, NULL
);
722 return table_log_print_error(r
);
725 printf("\n%i links listed.\n", c
);
730 /* IEEE Organizationally Unique Identifier vendor string */
731 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
732 const char *description
;
733 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
744 /* skip commonly misused 00:00:00 (Xerox) prefix */
745 if (memcmp(mac
, "\0\0\0", 3) == 0)
748 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
749 ETHER_ADDR_FORMAT_VAL(*mac
));
751 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
755 desc
= strdup(description
);
764 static int get_gateway_description(
769 union in_addr_union
*gateway
,
770 char **gateway_description
) {
771 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
772 sd_netlink_message
*m
;
776 assert(ifindex
>= 0);
777 assert(IN_SET(family
, AF_INET
, AF_INET6
));
779 assert(gateway_description
);
781 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
785 r
= sd_netlink_message_request_dump(req
, true);
789 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
793 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
794 union in_addr_union gw
= IN_ADDR_NULL
;
795 struct ether_addr mac
= ETHER_ADDR_NULL
;
799 r
= sd_netlink_message_get_errno(m
);
801 log_error_errno(r
, "got error: %m");
805 r
= sd_netlink_message_get_type(m
, &type
);
807 log_error_errno(r
, "could not get type: %m");
811 if (type
!= RTM_NEWNEIGH
) {
812 log_error("type is not RTM_NEWNEIGH");
816 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
818 log_error_errno(r
, "could not get family: %m");
823 log_error("family is not correct");
827 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
829 log_error_errno(r
, "could not get ifindex: %m");
833 if (ifindex
> 0 && ifi
!= ifindex
)
838 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
844 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
853 if (!in_addr_equal(fam
, &gw
, gateway
))
856 r
= sd_netlink_message_read(m
, NDA_LLADDR
, sizeof(mac
), &mac
);
860 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
870 static int dump_list(Table
*table
, const char *prefix
, char * const *l
) {
876 r
= table_add_many(table
,
878 TABLE_STRING
, prefix
,
881 return table_log_add_error(r
);
886 static int dump_gateways(
891 _cleanup_free_
struct local_address
*local
= NULL
;
892 _cleanup_strv_free_
char **buf
= NULL
;
898 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
902 for (i
= 0; i
< n
; i
++) {
903 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
, *with_description
= NULL
;
904 char name
[IF_NAMESIZE
+1];
906 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
910 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
912 log_debug_errno(r
, "Could not get description of gateway, ignoring: %m");
915 with_description
= strjoin(gateway
, " (", description
, ")");
916 if (!with_description
)
920 /* Show interface name for the entry if we show entries for all interfaces */
921 r
= strv_extendf(&buf
, "%s%s%s",
922 with_description
?: gateway
,
923 ifindex
<= 0 ? " on " : "",
924 ifindex
<= 0 ? format_ifname_full(local
[i
].ifindex
, name
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
) : "");
929 return dump_list(table
, "Gateway:", buf
);
932 static int dump_addresses(
934 sd_dhcp_lease
*lease
,
938 _cleanup_free_
struct local_address
*local
= NULL
;
939 _cleanup_strv_free_
char **buf
= NULL
;
940 struct in_addr dhcp4_address
= {};
946 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
951 (void) sd_dhcp_lease_get_address(lease
, &dhcp4_address
);
953 for (i
= 0; i
< n
; i
++) {
954 _cleanup_free_
char *pretty
= NULL
;
955 char name
[IF_NAMESIZE
+1];
957 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
961 if (local
[i
].family
== AF_INET
&& in4_addr_equal(&local
[i
].address
.in
, &dhcp4_address
)) {
962 struct in_addr server_address
;
963 char *p
, s
[INET_ADDRSTRLEN
];
965 r
= sd_dhcp_lease_get_server_identifier(lease
, &server_address
);
966 if (r
>= 0 && inet_ntop(AF_INET
, &server_address
, s
, sizeof(s
)))
967 p
= strjoin(pretty
, " (DHCP4 via ", s
, ")");
969 p
= strjoin(pretty
, " (DHCP4)");
973 free_and_replace(pretty
, p
);
976 r
= strv_extendf(&buf
, "%s%s%s",
978 ifindex
<= 0 ? " on " : "",
979 ifindex
<= 0 ? format_ifname_full(local
[i
].ifindex
, name
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
) : "");
984 return dump_list(table
, "Address:", buf
);
987 static int dump_address_labels(sd_netlink
*rtnl
) {
988 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
989 _cleanup_(table_unrefp
) Table
*table
= NULL
;
990 sd_netlink_message
*m
;
996 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
998 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
1000 r
= sd_netlink_message_request_dump(req
, true);
1004 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1008 table
= table_new("label", "prefix/prefixlen");
1013 table_set_width(table
, 0);
1015 r
= table_set_sort(table
, (size_t) 0, (size_t) SIZE_MAX
);
1019 assert_se(cell
= table_get_cell(table
, 0, 0));
1020 (void) table_set_align_percent(table
, cell
, 100);
1021 (void) table_set_ellipsize_percent(table
, cell
, 100);
1023 assert_se(cell
= table_get_cell(table
, 0, 1));
1024 (void) table_set_align_percent(table
, cell
, 100);
1026 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
1027 _cleanup_free_
char *pretty
= NULL
;
1028 union in_addr_union prefix
= IN_ADDR_NULL
;
1032 r
= sd_netlink_message_get_errno(m
);
1034 log_error_errno(r
, "got error: %m");
1038 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
1039 if (r
< 0 && r
!= -ENODATA
) {
1040 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
1044 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
1048 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
1052 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
1056 r
= table_add_cell(table
, NULL
, TABLE_UINT32
, &label
);
1058 return table_log_add_error(r
);
1060 r
= table_add_cell_stringf(table
, NULL
, "%s/%u", pretty
, prefixlen
);
1062 return table_log_add_error(r
);
1065 r
= table_print(table
, NULL
);
1067 return table_log_print_error(r
);
1072 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
1073 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1076 r
= sd_netlink_open(&rtnl
);
1078 return log_error_errno(r
, "Failed to connect to netlink: %m");
1080 dump_address_labels(rtnl
);
1085 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
1086 _cleanup_free_
char *p
= NULL
;
1089 if (asprintf(&p
, "/run/systemd/netif/lldp/%i", ifindex
) < 0)
1100 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
1101 _cleanup_free_
void *raw
= NULL
;
1109 l
= fread(&u
, 1, sizeof(u
), f
);
1110 if (l
== 0 && feof(f
))
1115 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
1116 if (le64toh(u
) >= 4096)
1119 raw
= new(uint8_t, le64toh(u
));
1123 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
1126 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
1133 static int dump_lldp_neighbors(Table
*table
, const char *prefix
, int ifindex
) {
1134 _cleanup_strv_free_
char **buf
= NULL
;
1135 _cleanup_fclose_
FILE *f
= NULL
;
1140 assert(ifindex
> 0);
1142 r
= open_lldp_neighbors(ifindex
, &f
);
1149 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
1150 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1152 r
= next_lldp_neighbor(f
, &n
);
1158 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1159 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1160 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1162 r
= strv_extendf(&buf
, "%s on port %s%s%s%s",
1165 isempty(port_description
) ? "" : " (",
1166 strempty(port_description
),
1167 isempty(port_description
) ? "" : ")");
1172 return dump_list(table
, prefix
, buf
);
1175 static int dump_dhcp_leases(Table
*table
, const char *prefix
, sd_bus
*bus
, const LinkInfo
*link
) {
1176 _cleanup_strv_free_
char **buf
= NULL
;
1177 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1178 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1181 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.DHCPServer", "Leases");
1183 bool quiet
= sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
);
1185 log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
1186 r
, "Failed to query link DHCP leases: %s", bus_error_message(&error
, r
));
1190 r
= sd_bus_message_enter_container(reply
, 'v', "a(uayayayayt)");
1192 return bus_log_parse_error(r
);
1194 r
= sd_bus_message_enter_container(reply
, 'a', "(uayayayayt)");
1196 return bus_log_parse_error(r
);
1198 while ((r
= sd_bus_message_enter_container(reply
, 'r', "uayayayayt")) > 0) {
1199 _cleanup_free_
char *id
= NULL
, *ip
= NULL
;
1200 const void *client_id
, *addr
, *gtw
, *hwaddr
;
1201 size_t client_id_sz
, sz
;
1202 uint64_t expiration
;
1205 r
= sd_bus_message_read(reply
, "u", &family
);
1207 return bus_log_parse_error(r
);
1209 r
= sd_bus_message_read_array(reply
, 'y', &client_id
, &client_id_sz
);
1211 return bus_log_parse_error(r
);
1213 r
= sd_bus_message_read_array(reply
, 'y', &addr
, &sz
);
1214 if (r
< 0 || sz
!= 4)
1215 return bus_log_parse_error(r
);
1217 r
= sd_bus_message_read_array(reply
, 'y', >w
, &sz
);
1218 if (r
< 0 || sz
!= 4)
1219 return bus_log_parse_error(r
);
1221 r
= sd_bus_message_read_array(reply
, 'y', &hwaddr
, &sz
);
1223 return bus_log_parse_error(r
);
1225 r
= sd_bus_message_read_basic(reply
, 't', &expiration
);
1227 return bus_log_parse_error(r
);
1229 r
= sd_dhcp_client_id_to_string(client_id
, client_id_sz
, &id
);
1231 return bus_log_parse_error(r
);
1233 r
= in_addr_to_string(family
, addr
, &ip
);
1235 return bus_log_parse_error(r
);
1237 r
= strv_extendf(&buf
, "%s (to %s)", ip
, id
);
1241 r
= sd_bus_message_exit_container(reply
);
1243 return bus_log_parse_error(r
);
1246 return bus_log_parse_error(r
);
1248 r
= sd_bus_message_exit_container(reply
);
1250 return bus_log_parse_error(r
);
1252 r
= sd_bus_message_exit_container(reply
);
1254 return bus_log_parse_error(r
);
1256 if (strv_isempty(buf
)) {
1257 r
= strv_extendf(&buf
, "none");
1262 return dump_list(table
, prefix
, buf
);
1265 static int dump_ifindexes(Table
*table
, const char *prefix
, const int *ifindexes
) {
1271 if (!ifindexes
|| ifindexes
[0] <= 0)
1274 for (c
= 0; ifindexes
[c
] > 0; c
++) {
1275 r
= table_add_many(table
,
1277 TABLE_STRING
, c
== 0 ? prefix
: "",
1278 TABLE_IFINDEX
, ifindexes
[c
]);
1280 return table_log_add_error(r
);
1286 #define DUMP_STATS_ONE(name, val_name) \
1287 r = table_add_many(table, \
1289 TABLE_STRING, name ":"); \
1291 return table_log_add_error(r); \
1292 r = table_add_cell(table, NULL, \
1293 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
1294 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
1296 return table_log_add_error(r);
1298 static int dump_statistics(Table
*table
, const LinkInfo
*info
) {
1304 if (!info
->has_stats64
&& !info
->has_stats
)
1307 DUMP_STATS_ONE("Rx Packets", rx_packets
);
1308 DUMP_STATS_ONE("Tx Packets", tx_packets
);
1309 DUMP_STATS_ONE("Rx Bytes", rx_bytes
);
1310 DUMP_STATS_ONE("Tx Bytes", tx_bytes
);
1311 DUMP_STATS_ONE("Rx Errors", rx_errors
);
1312 DUMP_STATS_ONE("Tx Errors", tx_errors
);
1313 DUMP_STATS_ONE("Rx Dropped", rx_dropped
);
1314 DUMP_STATS_ONE("Tx Dropped", tx_dropped
);
1315 DUMP_STATS_ONE("Multicast Packets", multicast
);
1316 DUMP_STATS_ONE("Collisions", collisions
);
1321 static OutputFlags
get_output_flags(void) {
1323 arg_all
* OUTPUT_SHOW_ALL
|
1324 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
1325 colors_enabled() * OUTPUT_COLOR
;
1328 static int show_logs(const LinkInfo
*info
) {
1329 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1335 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1337 return log_error_errno(r
, "Failed to open journal: %m");
1339 r
= add_match_this_boot(j
, NULL
);
1341 return log_error_errno(r
, "Failed to add boot matches: %m");
1344 char m1
[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)];
1345 const char *m2
, *m3
;
1348 xsprintf(m1
, "_KERNEL_DEVICE=n%i", info
->ifindex
);
1350 m2
= strjoina("INTERFACE=", info
->name
);
1352 m3
= strjoina("DEVICE=", info
->name
);
1355 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1356 (r
= sd_journal_add_disjunction(j
)) ||
1357 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1358 (r
= sd_journal_add_disjunction(j
)) ||
1359 (r
= sd_journal_add_match(j
, m3
, 0))
1362 return log_error_errno(r
, "Failed to add link matches: %m");
1364 r
= add_matches_for_unit(j
, "systemd-networkd.service");
1366 return log_error_errno(r
, "Failed to add unit matches: %m");
1368 r
= add_matches_for_unit(j
, "systemd-networkd-wait-online.service");
1370 return log_error_errno(r
, "Failed to add unit matches: %m");
1373 return show_journal(
1380 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
1384 static int link_status_one(
1388 const LinkInfo
*info
) {
1390 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **sip
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
1391 _cleanup_free_
char *t
= NULL
, *network
= NULL
, *iaid
= NULL
, *duid
= NULL
,
1392 *setup_state
= NULL
, *operational_state
= NULL
, *lease_file
= NULL
;
1393 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
,
1394 *on_color_operational
, *off_color_operational
, *on_color_setup
, *off_color_setup
;
1395 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
1396 _cleanup_(sd_dhcp_lease_unrefp
) sd_dhcp_lease
*lease
= NULL
;
1397 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1404 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
1405 operational_state_to_color(info
->name
, operational_state
, &on_color_operational
, &off_color_operational
);
1407 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
1408 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
1409 setup_state
= strdup("unmanaged");
1410 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
1412 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
1413 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
1414 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
1415 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
1416 (void) sd_network_link_get_sip(info
->ifindex
, &sip
);
1418 if (info
->sd_device
) {
1419 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_LINK_FILE", &link
);
1420 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_DRIVER", &driver
);
1421 (void) sd_device_get_property_value(info
->sd_device
, "ID_PATH", &path
);
1423 if (sd_device_get_property_value(info
->sd_device
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
1424 (void) sd_device_get_property_value(info
->sd_device
, "ID_VENDOR", &vendor
);
1426 if (sd_device_get_property_value(info
->sd_device
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
1427 (void) sd_device_get_property_value(info
->sd_device
, "ID_MODEL", &model
);
1430 t
= link_get_type_string(info
->iftype
, info
->sd_device
);
1432 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
1434 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
1435 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
1437 if (asprintf(&lease_file
, "/run/systemd/netif/leases/%d", info
->ifindex
) < 0)
1440 (void) dhcp_lease_load(&lease
, lease_file
);
1442 table
= table_new("dot", "key", "value");
1447 table_set_width(table
, 0);
1449 assert_se(cell
= table_get_cell(table
, 0, 0));
1450 (void) table_set_ellipsize_percent(table
, cell
, 100);
1452 assert_se(cell
= table_get_cell(table
, 0, 1));
1453 (void) table_set_ellipsize_percent(table
, cell
, 100);
1455 table_set_header(table
, false);
1457 r
= table_add_many(table
,
1458 TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
),
1459 TABLE_SET_COLOR
, on_color_operational
);
1461 return table_log_add_error(r
);
1462 r
= table_add_cell_stringf(table
, &cell
, "%i: %s", info
->ifindex
, info
->name
);
1464 return table_log_add_error(r
);
1465 (void) table_set_align_percent(table
, cell
, 0);
1467 r
= table_add_many(table
,
1470 TABLE_STRING
, "Link File:",
1471 TABLE_SET_ALIGN_PERCENT
, 100,
1472 TABLE_STRING
, strna(link
),
1474 TABLE_STRING
, "Network File:",
1475 TABLE_STRING
, strna(network
),
1477 TABLE_STRING
, "Type:",
1478 TABLE_STRING
, strna(t
),
1480 TABLE_STRING
, "State:");
1482 return table_log_add_error(r
);
1483 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s (%s%s%s)",
1484 on_color_operational
, strna(operational_state
), off_color_operational
,
1485 on_color_setup
, strna(setup_state
), off_color_setup
);
1487 return table_log_add_error(r
);
1489 strv_sort(info
->alternative_names
);
1490 r
= dump_list(table
, "Alternative Names:", info
->alternative_names
);
1495 r
= table_add_many(table
,
1497 TABLE_STRING
, "Path:",
1498 TABLE_STRING
, path
);
1500 return table_log_add_error(r
);
1503 r
= table_add_many(table
,
1505 TABLE_STRING
, "Driver:",
1506 TABLE_STRING
, driver
);
1508 return table_log_add_error(r
);
1511 r
= table_add_many(table
,
1513 TABLE_STRING
, "Vendor:",
1514 TABLE_STRING
, vendor
);
1516 return table_log_add_error(r
);
1519 r
= table_add_many(table
,
1521 TABLE_STRING
, "Model:",
1522 TABLE_STRING
, model
);
1524 return table_log_add_error(r
);
1527 if (info
->has_mac_address
) {
1528 _cleanup_free_
char *description
= NULL
;
1529 char ea
[ETHER_ADDR_TO_STRING_MAX
];
1531 (void) ieee_oui(hwdb
, &info
->mac_address
, &description
);
1533 r
= table_add_many(table
,
1535 TABLE_STRING
, "HW Address:");
1537 return table_log_add_error(r
);
1538 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s%s",
1539 ether_addr_to_string(&info
->mac_address
, ea
),
1540 description
? " (" : "",
1541 strempty(description
),
1542 description
? ")" : "");
1544 return table_log_add_error(r
);
1547 if (info
->has_permanent_mac_address
) {
1548 _cleanup_free_
char *description
= NULL
;
1549 char ea
[ETHER_ADDR_TO_STRING_MAX
];
1551 (void) ieee_oui(hwdb
, &info
->permanent_mac_address
, &description
);
1553 r
= table_add_many(table
,
1555 TABLE_STRING
, "HW Permanent Address:");
1557 return table_log_add_error(r
);
1558 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s%s",
1559 ether_addr_to_string(&info
->permanent_mac_address
, ea
),
1560 description
? " (" : "",
1561 strempty(description
),
1562 description
? ")" : "");
1564 return table_log_add_error(r
);
1567 if (info
->mtu
> 0) {
1568 char min_str
[DECIMAL_STR_MAX(uint32_t)], max_str
[DECIMAL_STR_MAX(uint32_t)];
1570 xsprintf(min_str
, "%" PRIu32
, info
->min_mtu
);
1571 xsprintf(max_str
, "%" PRIu32
, info
->max_mtu
);
1573 r
= table_add_many(table
,
1575 TABLE_STRING
, "MTU:");
1577 return table_log_add_error(r
);
1578 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"%s%s%s%s%s%s%s",
1580 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? " (" : "",
1581 info
->min_mtu
> 0 ? "min: " : "",
1582 info
->min_mtu
> 0 ? min_str
: "",
1583 info
->min_mtu
> 0 && info
->max_mtu
> 0 ? ", " : "",
1584 info
->max_mtu
> 0 ? "max: " : "",
1585 info
->max_mtu
> 0 ? max_str
: "",
1586 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? ")" : "");
1588 return table_log_add_error(r
);
1592 r
= table_add_many(table
,
1594 TABLE_STRING
, "QDisc:",
1595 TABLE_STRING
, info
->qdisc
);
1597 return table_log_add_error(r
);
1600 if (info
->master
> 0) {
1601 r
= table_add_many(table
,
1603 TABLE_STRING
, "Master:",
1604 TABLE_IFINDEX
, info
->master
);
1606 return table_log_add_error(r
);
1609 if (info
->has_ipv6_address_generation_mode
) {
1610 static const struct {
1615 { "stable-privacy" },
1619 r
= table_add_many(table
,
1621 TABLE_STRING
, "IPv6 Address Generation Mode:",
1622 TABLE_STRING
, mode_table
[info
->addr_gen_mode
]);
1624 return table_log_add_error(r
);
1627 if (streq_ptr(info
->netdev_kind
, "bridge")) {
1628 r
= table_add_many(table
,
1630 TABLE_STRING
, "Forward Delay:",
1631 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->forward_delay
),
1633 TABLE_STRING
, "Hello Time:",
1634 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->hello_time
),
1636 TABLE_STRING
, "Max Age:",
1637 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->max_age
),
1639 TABLE_STRING
, "Ageing Time:",
1640 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->ageing_time
),
1642 TABLE_STRING
, "Priority:",
1643 TABLE_UINT16
, info
->priority
,
1645 TABLE_STRING
, "STP:",
1646 TABLE_BOOLEAN
, info
->stp_state
> 0,
1648 TABLE_STRING
, "Multicast IGMP Version:",
1649 TABLE_UINT8
, info
->mcast_igmp_version
,
1651 TABLE_STRING
, "Cost:",
1652 TABLE_UINT32
, info
->cost
);
1654 return table_log_add_error(r
);
1656 if (info
->port_state
<= BR_STATE_BLOCKING
) {
1657 r
= table_add_many(table
,
1659 TABLE_STRING
, "Port State:",
1660 TABLE_STRING
, bridge_state_to_string(info
->port_state
));
1662 } else if (streq_ptr(info
->netdev_kind
, "bond")) {
1663 r
= table_add_many(table
,
1665 TABLE_STRING
, "Mode:",
1666 TABLE_STRING
, bond_mode_to_string(info
->mode
),
1668 TABLE_STRING
, "Miimon:",
1669 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->miimon
),
1671 TABLE_STRING
, "Updelay:",
1672 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->updelay
),
1674 TABLE_STRING
, "Downdelay:",
1675 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->downdelay
));
1677 return table_log_add_error(r
);
1679 } else if (streq_ptr(info
->netdev_kind
, "vxlan")) {
1680 char ttl
[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1682 if (info
->vxlan_info
.vni
> 0) {
1683 r
= table_add_many(table
,
1685 TABLE_STRING
, "VNI:",
1686 TABLE_UINT32
, info
->vxlan_info
.vni
);
1688 return table_log_add_error(r
);
1691 if (IN_SET(info
->vxlan_info
.group_family
, AF_INET
, AF_INET6
)) {
1694 r
= in_addr_is_multicast(info
->vxlan_info
.group_family
, &info
->vxlan_info
.group
);
1700 r
= table_add_many(table
,
1703 info
->vxlan_info
.group_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
,
1704 &info
->vxlan_info
.group
);
1706 return table_log_add_error(r
);
1709 if (IN_SET(info
->vxlan_info
.local_family
, AF_INET
, AF_INET6
)) {
1710 r
= table_add_many(table
,
1712 TABLE_STRING
, "Local:",
1713 info
->vxlan_info
.local_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
,
1714 &info
->vxlan_info
.local
);
1716 return table_log_add_error(r
);
1719 if (info
->vxlan_info
.dest_port
> 0) {
1720 r
= table_add_many(table
,
1722 TABLE_STRING
, "Destination Port:",
1723 TABLE_UINT16
, be16toh(info
->vxlan_info
.dest_port
));
1725 return table_log_add_error(r
);
1728 if (info
->vxlan_info
.link
> 0) {
1729 r
= table_add_many(table
,
1731 TABLE_STRING
, "Underlying Device:",
1732 TABLE_IFINDEX
, info
->vxlan_info
.link
);
1734 return table_log_add_error(r
);
1737 r
= table_add_many(table
,
1739 TABLE_STRING
, "Learning:",
1740 TABLE_BOOLEAN
, info
->vxlan_info
.learning
);
1742 return table_log_add_error(r
);
1744 r
= table_add_many(table
,
1746 TABLE_STRING
, "RSC:",
1747 TABLE_BOOLEAN
, info
->vxlan_info
.rsc
);
1749 return table_log_add_error(r
);
1751 r
= table_add_many(table
,
1753 TABLE_STRING
, "L3MISS:",
1754 TABLE_BOOLEAN
, info
->vxlan_info
.l3miss
);
1756 return table_log_add_error(r
);
1758 r
= table_add_many(table
,
1760 TABLE_STRING
, "L2MISS:",
1761 TABLE_BOOLEAN
, info
->vxlan_info
.l2miss
);
1763 return table_log_add_error(r
);
1765 if (info
->vxlan_info
.tos
> 1) {
1766 r
= table_add_many(table
,
1768 TABLE_STRING
, "TOS:",
1769 TABLE_UINT8
, info
->vxlan_info
.tos
);
1771 return table_log_add_error(r
);
1774 if (info
->vxlan_info
.ttl
> 0)
1775 xsprintf(ttl
, "%" PRIu8
, info
->vxlan_info
.ttl
);
1777 strcpy(ttl
, "auto");
1779 r
= table_add_many(table
,
1781 TABLE_STRING
, "TTL:",
1784 return table_log_add_error(r
);
1785 } else if (streq_ptr(info
->netdev_kind
, "vlan") && info
->vlan_id
> 0) {
1786 r
= table_add_many(table
,
1788 TABLE_STRING
, "VLan Id:",
1789 TABLE_UINT16
, info
->vlan_id
);
1791 return table_log_add_error(r
);
1792 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
1793 if (!in_addr_is_null(AF_INET
, &info
->local
)) {
1794 r
= table_add_many(table
,
1796 TABLE_STRING
, "Local:",
1797 TABLE_IN_ADDR
, &info
->local
);
1799 return table_log_add_error(r
);
1802 if (!in_addr_is_null(AF_INET
, &info
->remote
)) {
1803 r
= table_add_many(table
,
1805 TABLE_STRING
, "Remote:",
1806 TABLE_IN_ADDR
, &info
->remote
);
1808 return table_log_add_error(r
);
1810 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
1811 if (!in_addr_is_null(AF_INET6
, &info
->local
)) {
1812 r
= table_add_many(table
,
1814 TABLE_STRING
, "Local:",
1815 TABLE_IN6_ADDR
, &info
->local
);
1817 return table_log_add_error(r
);
1820 if (!in_addr_is_null(AF_INET6
, &info
->remote
)) {
1821 r
= table_add_many(table
,
1823 TABLE_STRING
, "Remote:",
1824 TABLE_IN6_ADDR
, &info
->remote
);
1826 return table_log_add_error(r
);
1828 } else if (streq_ptr(info
->netdev_kind
, "geneve")) {
1829 r
= table_add_many(table
,
1831 TABLE_STRING
, "VNI:",
1832 TABLE_UINT32
, info
->vni
);
1834 return table_log_add_error(r
);
1836 if (info
->has_tunnel_ipv4
&& !in_addr_is_null(AF_INET
, &info
->remote
)) {
1837 r
= table_add_many(table
,
1839 TABLE_STRING
, "Remote:",
1840 TABLE_IN_ADDR
, &info
->remote
);
1842 return table_log_add_error(r
);
1843 } else if (!in_addr_is_null(AF_INET6
, &info
->remote
)) {
1844 r
= table_add_many(table
,
1846 TABLE_STRING
, "Remote:",
1847 TABLE_IN6_ADDR
, &info
->remote
);
1849 return table_log_add_error(r
);
1852 if (info
->ttl
> 0) {
1853 r
= table_add_many(table
,
1855 TABLE_STRING
, "TTL:",
1856 TABLE_UINT8
, info
->ttl
);
1858 return table_log_add_error(r
);
1861 if (info
->tos
> 0) {
1862 r
= table_add_many(table
,
1864 TABLE_STRING
, "TOS:",
1865 TABLE_UINT8
, info
->tos
);
1867 return table_log_add_error(r
);
1870 r
= table_add_many(table
,
1872 TABLE_STRING
, "Port:",
1873 TABLE_UINT16
, info
->tunnel_port
);
1875 return table_log_add_error(r
);
1877 r
= table_add_many(table
,
1879 TABLE_STRING
, "Inherit:",
1880 TABLE_STRING
, geneve_df_to_string(info
->inherit
));
1882 return table_log_add_error(r
);
1885 r
= table_add_many(table
,
1887 TABLE_STRING
, "IPDoNotFragment:",
1888 TABLE_UINT8
, info
->df
);
1890 return table_log_add_error(r
);
1893 r
= table_add_many(table
,
1895 TABLE_STRING
, "UDPChecksum:",
1896 TABLE_BOOLEAN
, info
->csum
);
1898 return table_log_add_error(r
);
1900 r
= table_add_many(table
,
1902 TABLE_STRING
, "UDP6ZeroChecksumTx:",
1903 TABLE_BOOLEAN
, info
->csum6_tx
);
1905 return table_log_add_error(r
);
1907 r
= table_add_many(table
,
1909 TABLE_STRING
, "UDP6ZeroChecksumRx:",
1910 TABLE_BOOLEAN
, info
->csum6_rx
);
1912 return table_log_add_error(r
);
1914 if (info
->label
> 0) {
1915 r
= table_add_many(table
,
1917 TABLE_STRING
, "FlowLabel:",
1918 TABLE_UINT32
, info
->label
);
1920 return table_log_add_error(r
);
1922 } else if (STRPTR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap")) {
1923 r
= table_add_many(table
,
1925 TABLE_STRING
, "Mode:",
1926 TABLE_STRING
, macvlan_mode_to_string(info
->macvlan_mode
));
1928 return table_log_add_error(r
);
1929 } else if (streq_ptr(info
->netdev_kind
, "ipvlan")) {
1930 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
1932 if (info
->ipvlan_flags
& IPVLAN_F_PRIVATE
)
1933 p
= strdup("private");
1934 else if (info
->ipvlan_flags
& IPVLAN_F_VEPA
)
1937 p
= strdup("bridge");
1941 s
= strjoin(ipvlan_mode_to_string(info
->ipvlan_mode
), " (", p
, ")");
1945 r
= table_add_many(table
,
1947 TABLE_STRING
, "Mode:",
1950 return table_log_add_error(r
);
1953 if (info
->has_wlan_link_info
) {
1954 _cleanup_free_
char *esc
= NULL
;
1955 char buf
[ETHER_ADDR_TO_STRING_MAX
];
1957 r
= table_add_many(table
,
1959 TABLE_STRING
, "WiFi access point:");
1961 return table_log_add_error(r
);
1964 esc
= cescape(info
->ssid
);
1966 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)",
1968 ether_addr_to_string(&info
->bssid
, buf
));
1970 return table_log_add_error(r
);
1973 if (info
->has_bitrates
) {
1974 char tx
[FORMAT_BYTES_MAX
], rx
[FORMAT_BYTES_MAX
];
1976 r
= table_add_many(table
,
1978 TABLE_STRING
, "Bit Rate (Tx/Rx):");
1980 return table_log_add_error(r
);
1981 r
= table_add_cell_stringf(table
, NULL
, "%sbps/%sbps",
1982 format_bytes_full(tx
, sizeof tx
, info
->tx_bitrate
, 0),
1983 format_bytes_full(rx
, sizeof rx
, info
->rx_bitrate
, 0));
1985 return table_log_add_error(r
);
1988 if (info
->has_tx_queues
|| info
->has_rx_queues
) {
1989 r
= table_add_many(table
,
1991 TABLE_STRING
, "Queue Length (Tx/Rx):");
1993 return table_log_add_error(r
);
1994 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"/%" PRIu32
, info
->tx_queues
, info
->rx_queues
);
1996 return table_log_add_error(r
);
1999 if (info
->has_ethtool_link_info
) {
2000 const char *duplex
= duplex_to_string(info
->duplex
);
2001 const char *port
= port_to_string(info
->port
);
2003 if (IN_SET(info
->autonegotiation
, AUTONEG_DISABLE
, AUTONEG_ENABLE
)) {
2004 r
= table_add_many(table
,
2006 TABLE_STRING
, "Auto negotiation:",
2007 TABLE_BOOLEAN
, info
->autonegotiation
== AUTONEG_ENABLE
);
2009 return table_log_add_error(r
);
2012 if (info
->speed
> 0) {
2013 r
= table_add_many(table
,
2015 TABLE_STRING
, "Speed:",
2016 TABLE_BPS
, info
->speed
);
2018 return table_log_add_error(r
);
2022 r
= table_add_many(table
,
2024 TABLE_STRING
, "Duplex:",
2025 TABLE_STRING
, duplex
);
2027 return table_log_add_error(r
);
2031 r
= table_add_many(table
,
2033 TABLE_STRING
, "Port:",
2034 TABLE_STRING
, port
);
2036 return table_log_add_error(r
);
2040 r
= dump_addresses(rtnl
, lease
, table
, info
->ifindex
);
2043 r
= dump_gateways(rtnl
, hwdb
, table
, info
->ifindex
);
2046 r
= dump_list(table
, "DNS:", dns
);
2049 r
= dump_list(table
, "Search Domains:", search_domains
);
2052 r
= dump_list(table
, "Route Domains:", route_domains
);
2055 r
= dump_list(table
, "NTP:", ntp
);
2058 r
= dump_list(table
, "SIP:", sip
);
2061 r
= dump_ifindexes(table
, "Carrier Bound To:", carrier_bound_to
);
2064 r
= dump_ifindexes(table
, "Carrier Bound By:", carrier_bound_by
);
2069 const void *client_id
;
2070 size_t client_id_len
;
2073 r
= sd_dhcp_lease_get_timezone(lease
, &tz
);
2075 r
= table_add_many(table
,
2077 TABLE_STRING
, "Time Zone:",
2080 return table_log_add_error(r
);
2083 r
= sd_dhcp_lease_get_client_id(lease
, &client_id
, &client_id_len
);
2085 _cleanup_free_
char *id
= NULL
;
2087 r
= sd_dhcp_client_id_to_string(client_id
, client_id_len
, &id
);
2089 r
= table_add_many(table
,
2091 TABLE_STRING
, "DHCP4 Client ID:",
2094 return table_log_add_error(r
);
2099 r
= sd_network_link_get_dhcp6_client_iaid_string(info
->ifindex
, &iaid
);
2101 r
= table_add_many(table
,
2103 TABLE_STRING
, "DHCP6 Client IAID:",
2104 TABLE_STRING
, iaid
);
2106 return table_log_add_error(r
);
2109 r
= sd_network_link_get_dhcp6_client_duid_string(info
->ifindex
, &duid
);
2111 r
= table_add_many(table
,
2113 TABLE_STRING
, "DHCP6 Client DUID:",
2114 TABLE_STRING
, duid
);
2116 return table_log_add_error(r
);
2119 r
= dump_lldp_neighbors(table
, "Connected To:", info
->ifindex
);
2123 r
= dump_dhcp_leases(table
, "Offered DHCP leases:", bus
, info
);
2127 r
= dump_statistics(table
, info
);
2131 r
= table_print(table
, NULL
);
2133 return table_log_print_error(r
);
2135 return show_logs(info
);
2138 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
2139 _cleanup_free_
char *operational_state
= NULL
;
2140 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
2141 const char *on_color_operational
, *off_color_operational
;
2142 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2148 (void) sd_network_get_operational_state(&operational_state
);
2149 operational_state_to_color(NULL
, operational_state
, &on_color_operational
, &off_color_operational
);
2151 table
= table_new("dot", "key", "value");
2156 table_set_width(table
, 0);
2158 assert_se(cell
= table_get_cell(table
, 0, 0));
2159 (void) table_set_ellipsize_percent(table
, cell
, 100);
2161 assert_se(cell
= table_get_cell(table
, 0, 1));
2162 (void) table_set_align_percent(table
, cell
, 100);
2163 (void) table_set_ellipsize_percent(table
, cell
, 100);
2165 table_set_header(table
, false);
2167 r
= table_add_many(table
,
2168 TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
),
2169 TABLE_SET_COLOR
, on_color_operational
,
2170 TABLE_STRING
, "State:",
2171 TABLE_STRING
, strna(operational_state
),
2172 TABLE_SET_COLOR
, on_color_operational
);
2174 return table_log_add_error(r
);
2176 r
= dump_addresses(rtnl
, NULL
, table
, 0);
2179 r
= dump_gateways(rtnl
, hwdb
, table
, 0);
2183 (void) sd_network_get_dns(&dns
);
2184 r
= dump_list(table
, "DNS:", dns
);
2188 (void) sd_network_get_search_domains(&search_domains
);
2189 r
= dump_list(table
, "Search Domains:", search_domains
);
2193 (void) sd_network_get_route_domains(&route_domains
);
2194 r
= dump_list(table
, "Route Domains:", route_domains
);
2198 (void) sd_network_get_ntp(&ntp
);
2199 r
= dump_list(table
, "NTP:", ntp
);
2203 r
= table_print(table
, NULL
);
2205 return table_log_print_error(r
);
2207 return show_logs(NULL
);
2210 static int link_status(int argc
, char *argv
[], void *userdata
) {
2211 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2212 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2213 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
2214 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2217 (void) pager_open(arg_pager_flags
);
2219 r
= sd_bus_open_system(&bus
);
2221 return log_error_errno(r
, "Failed to connect system bus: %m");
2223 r
= sd_netlink_open(&rtnl
);
2225 return log_error_errno(r
, "Failed to connect to netlink: %m");
2227 r
= sd_hwdb_new(&hwdb
);
2229 log_debug_errno(r
, "Failed to open hardware database: %m");
2232 c
= acquire_link_info(bus
, rtnl
, NULL
, &links
);
2234 return system_status(rtnl
, hwdb
);
2236 c
= acquire_link_info(bus
, rtnl
, argv
+ 1, &links
);
2240 for (i
= 0; i
< c
; i
++) {
2242 fputc('\n', stdout
);
2244 link_status_one(bus
, rtnl
, hwdb
, links
+ i
);
2250 static char *lldp_capabilities_to_string(uint16_t x
) {
2251 static const char characters
[] = {
2252 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2257 ret
= new(char, ELEMENTSOF(characters
) + 1);
2261 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
2262 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
2268 static void lldp_capabilities_legend(uint16_t x
) {
2269 unsigned w
, i
, cols
= columns();
2270 static const char* const table
[] = {
2274 "w - WLAN Access Point",
2277 "d - DOCSIS cable device",
2279 "c - Customer VLAN",
2281 "m - Two-port MAC Relay (TPMR)",
2287 printf("\nCapability Flags:\n");
2288 for (w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
2289 if (x
& (1U << i
) || arg_all
) {
2292 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
2295 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
2300 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
2301 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2302 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2303 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2308 r
= sd_netlink_open(&rtnl
);
2310 return log_error_errno(r
, "Failed to connect to netlink: %m");
2312 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
2316 (void) pager_open(arg_pager_flags
);
2318 table
= table_new("link",
2323 "port description");
2328 table_set_width(table
, 0);
2330 table_set_header(table
, arg_legend
);
2332 assert_se(cell
= table_get_cell(table
, 0, 0));
2333 table_set_minimum_width(table
, cell
, 16);
2335 assert_se(cell
= table_get_cell(table
, 0, 1));
2336 table_set_minimum_width(table
, cell
, 17);
2338 assert_se(cell
= table_get_cell(table
, 0, 2));
2339 table_set_minimum_width(table
, cell
, 16);
2341 assert_se(cell
= table_get_cell(table
, 0, 3));
2342 table_set_minimum_width(table
, cell
, 11);
2344 assert_se(cell
= table_get_cell(table
, 0, 4));
2345 table_set_minimum_width(table
, cell
, 17);
2347 assert_se(cell
= table_get_cell(table
, 0, 5));
2348 table_set_minimum_width(table
, cell
, 16);
2350 for (i
= 0; i
< c
; i
++) {
2351 _cleanup_fclose_
FILE *f
= NULL
;
2353 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
2357 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
2362 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
, *capabilities
= NULL
;
2363 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
;
2364 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
2367 r
= next_lldp_neighbor(f
, &n
);
2369 log_warning_errno(r
, "Failed to read neighbor data: %m");
2375 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
2376 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
2377 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
2378 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
2381 cid
= ellipsize(chassis_id
, 17, 100);
2387 pid
= ellipsize(port_id
, 17, 100);
2393 sname
= ellipsize(system_name
, 16, 100);
2395 system_name
= sname
;
2398 if (port_description
) {
2399 pdesc
= ellipsize(port_description
, 16, 100);
2401 port_description
= pdesc
;
2404 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
2405 capabilities
= lldp_capabilities_to_string(cc
);
2409 r
= table_add_many(table
,
2410 TABLE_STRING
, links
[i
].name
,
2411 TABLE_STRING
, strna(chassis_id
),
2412 TABLE_STRING
, strna(system_name
),
2413 TABLE_STRING
, strna(capabilities
),
2414 TABLE_STRING
, strna(port_id
),
2415 TABLE_STRING
, strna(port_description
));
2417 return table_log_add_error(r
);
2423 r
= table_print(table
, NULL
);
2425 return table_log_print_error(r
);
2428 lldp_capabilities_legend(all
);
2429 printf("\n%i neighbors listed.\n", m
);
2435 static int link_delete_send_message(sd_netlink
*rtnl
, int index
) {
2436 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2441 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_DELLINK
, index
);
2443 return rtnl_log_create_error(r
);
2445 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2452 static int link_up_down_send_message(sd_netlink
*rtnl
, char *command
, int index
) {
2453 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2458 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_SETLINK
, index
);
2460 return rtnl_log_create_error(r
);
2462 if (streq(command
, "up"))
2463 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2465 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2467 return log_error_errno(r
, "Could not set link flags: %m");
2469 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2476 static int link_up_down(int argc
, char *argv
[], void *userdata
) {
2477 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2478 _cleanup_set_free_ Set
*indexes
= NULL
;
2482 r
= sd_netlink_open(&rtnl
);
2484 return log_error_errno(r
, "Failed to connect to netlink: %m");
2486 indexes
= set_new(NULL
);
2490 for (i
= 1; i
< argc
; i
++) {
2491 index
= resolve_interface_or_warn(&rtnl
, argv
[i
]);
2495 r
= set_put(indexes
, INT_TO_PTR(index
));
2500 SET_FOREACH(p
, indexes
) {
2501 index
= PTR_TO_INT(p
);
2502 r
= link_up_down_send_message(rtnl
, argv
[0], index
);
2504 char ifname
[IF_NAMESIZE
+ 1];
2506 return log_error_errno(r
, "Failed to %s interface %s: %m",
2507 argv
[1], format_ifname_full(index
, ifname
, FORMAT_IFNAME_IFINDEX
));
2514 static int link_delete(int argc
, char *argv
[], void *userdata
) {
2515 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2516 _cleanup_set_free_ Set
*indexes
= NULL
;
2520 r
= sd_netlink_open(&rtnl
);
2522 return log_error_errno(r
, "Failed to connect to netlink: %m");
2524 indexes
= set_new(NULL
);
2528 for (i
= 1; i
< argc
; i
++) {
2529 index
= resolve_interface_or_warn(&rtnl
, argv
[i
]);
2533 r
= set_put(indexes
, INT_TO_PTR(index
));
2538 SET_FOREACH(p
, indexes
) {
2539 index
= PTR_TO_INT(p
);
2540 r
= link_delete_send_message(rtnl
, index
);
2542 char ifname
[IF_NAMESIZE
+ 1];
2544 return log_error_errno(r
, "Failed to delete interface %s: %m",
2545 format_ifname_full(index
, ifname
, FORMAT_IFNAME_IFINDEX
));
2552 static int link_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2553 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2556 r
= bus_call_method(bus
, bus_network_mgr
, "RenewLink", &error
, NULL
, "i", index
);
2558 return log_error_errno(r
, "Failed to renew dynamic configuration of interface %s: %s",
2559 name
, bus_error_message(&error
, r
));
2564 static int link_renew(int argc
, char *argv
[], void *userdata
) {
2565 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2566 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2567 int index
, i
, k
= 0, r
;
2569 r
= sd_bus_open_system(&bus
);
2571 return log_error_errno(r
, "Failed to connect system bus: %m");
2573 for (i
= 1; i
< argc
; i
++) {
2574 index
= resolve_interface_or_warn(&rtnl
, argv
[i
]);
2578 r
= link_renew_one(bus
, index
, argv
[i
]);
2579 if (r
< 0 && k
>= 0)
2586 static int link_force_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2587 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2590 r
= bus_call_method(bus
, bus_network_mgr
, "ForceRenewLink", &error
, NULL
, "i", index
);
2592 return log_error_errno(r
, "Failed to force renew dynamic configuration of interface %s: %s",
2593 name
, bus_error_message(&error
, r
));
2598 static int link_force_renew(int argc
, char *argv
[], void *userdata
) {
2599 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2600 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2601 int index
, i
, k
= 0, r
;
2603 r
= sd_bus_open_system(&bus
);
2605 return log_error_errno(r
, "Failed to connect system bus: %m");
2607 for (i
= 1; i
< argc
; i
++) {
2608 index
= resolve_interface_or_warn(&rtnl
, argv
[i
]);
2612 r
= link_force_renew_one(bus
, index
, argv
[i
]);
2613 if (r
< 0 && k
>= 0)
2620 static int verb_reload(int argc
, char *argv
[], void *userdata
) {
2621 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2622 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2625 r
= sd_bus_open_system(&bus
);
2627 return log_error_errno(r
, "Failed to connect system bus: %m");
2629 r
= bus_call_method(bus
, bus_network_mgr
, "Reload", &error
, NULL
, NULL
);
2631 return log_error_errno(r
, "Failed to reload network settings: %m");
2636 static int verb_reconfigure(int argc
, char *argv
[], void *userdata
) {
2637 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2638 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2639 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2640 _cleanup_set_free_ Set
*indexes
= NULL
;
2644 r
= sd_bus_open_system(&bus
);
2646 return log_error_errno(r
, "Failed to connect system bus: %m");
2648 indexes
= set_new(NULL
);
2652 for (i
= 1; i
< argc
; i
++) {
2653 index
= resolve_interface_or_warn(&rtnl
, argv
[i
]);
2657 r
= set_put(indexes
, INT_TO_PTR(index
));
2662 SET_FOREACH(p
, indexes
) {
2663 index
= PTR_TO_INT(p
);
2664 r
= bus_call_method(bus
, bus_network_mgr
, "ReconfigureLink", &error
, NULL
, "i", index
);
2666 char ifname
[IF_NAMESIZE
+ 1];
2668 return log_error_errno(r
, "Failed to reconfigure network interface %s: %m",
2669 format_ifname_full(index
, ifname
, FORMAT_IFNAME_IFINDEX
));
2676 static int help(void) {
2677 _cleanup_free_
char *link
= NULL
;
2680 r
= terminal_urlify_man("networkctl", "1", &link
);
2684 printf("%s [OPTIONS...] COMMAND\n\n"
2685 "%sQuery and control the networking subsystem.%s\n"
2687 " list [PATTERN...] List links\n"
2688 " status [PATTERN...] Show link status\n"
2689 " lldp [PATTERN...] Show LLDP neighbors\n"
2690 " label Show current address label entries in the kernel\n"
2691 " delete DEVICES... Delete virtual netdevs\n"
2692 " up DEVICES... Bring devices up\n"
2693 " down DEVICES... Bring devices down\n"
2694 " renew DEVICES... Renew dynamic configurations\n"
2695 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
2696 " reconfigure DEVICES... Reconfigure interfaces\n"
2697 " reload Reload .network and .netdev files\n"
2699 " -h --help Show this help\n"
2700 " --version Show package version\n"
2701 " --no-pager Do not pipe output into a pager\n"
2702 " --no-legend Do not show the headers and footers\n"
2703 " -a --all Show status for all links\n"
2704 " -s --stats Show detailed link statics\n"
2705 " -l --full Do not ellipsize output\n"
2706 " -n --lines=INTEGER Number of journal entries to show\n"
2707 "\nSee the %s for details.\n"
2708 , program_invocation_short_name
2717 static int parse_argv(int argc
, char *argv
[]) {
2720 ARG_VERSION
= 0x100,
2725 static const struct option options
[] = {
2726 { "help", no_argument
, NULL
, 'h' },
2727 { "version", no_argument
, NULL
, ARG_VERSION
},
2728 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2729 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2730 { "all", no_argument
, NULL
, 'a' },
2731 { "stats", no_argument
, NULL
, 's' },
2732 { "full", no_argument
, NULL
, 'l' },
2733 { "lines", required_argument
, NULL
, 'n' },
2742 while ((c
= getopt_long(argc
, argv
, "hasln:", options
, NULL
)) >= 0) {
2753 arg_pager_flags
|= PAGER_DISABLE
;
2773 if (safe_atou(optarg
, &arg_lines
) < 0)
2774 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2775 "Failed to parse lines '%s'", optarg
);
2782 assert_not_reached("Unhandled option");
2789 static int networkctl_main(int argc
, char *argv
[]) {
2790 static const Verb verbs
[] = {
2791 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
2792 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
2793 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
2794 { "label", 1, 1, 0, list_address_labels
},
2795 { "delete", 2, VERB_ANY
, 0, link_delete
},
2796 { "up", 2, VERB_ANY
, 0, link_up_down
},
2797 { "down", 2, VERB_ANY
, 0, link_up_down
},
2798 { "renew", 2, VERB_ANY
, 0, link_renew
},
2799 { "forcerenew", 2, VERB_ANY
, 0, link_force_renew
},
2800 { "reconfigure", 2, VERB_ANY
, 0, verb_reconfigure
},
2801 { "reload", 1, 1, 0, verb_reload
},
2805 return dispatch_verb(argc
, argv
, verbs
, NULL
);
2808 static void warn_networkd_missing(void) {
2810 if (access("/run/systemd/netif/state", F_OK
) >= 0)
2813 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
2816 static int run(int argc
, char* argv
[]) {
2821 r
= parse_argv(argc
, argv
);
2825 warn_networkd_missing();
2827 return networkctl_main(argc
, argv
);
2830 DEFINE_MAIN_FUNCTION(run
);