1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
18 #include "sd-lldp-rx.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 "netif-util.h"
46 #include "netlink-util.h"
47 #include "network-internal.h"
48 #include "network-util.h"
50 #include "parse-argument.h"
51 #include "parse-util.h"
52 #include "pretty-print.h"
54 #include "socket-netlink.h"
55 #include "socket-util.h"
56 #include "sort-util.h"
57 #include "sparse-endian.h"
58 #include "stdio-util.h"
59 #include "string-table.h"
60 #include "string-util.h"
63 #include "terminal-util.h"
66 #include "wifi-util.h"
68 /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */
69 #define NETDEV_KIND_MAX 64
71 /* use 128 kB for receive socket kernel queue, we shouldn't need more here */
72 #define RCVBUF_SIZE (128*1024)
74 static PagerFlags arg_pager_flags
= 0;
75 static bool arg_legend
= true;
76 static bool arg_all
= false;
77 static bool arg_stats
= false;
78 static bool arg_full
= false;
79 static unsigned arg_lines
= 10;
80 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
82 static int get_description(sd_bus
*bus
, JsonVariant
**ret
) {
83 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
84 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
85 const char *text
= NULL
;
88 r
= bus_call_method(bus
, bus_network_mgr
, "Describe", &error
, &reply
, NULL
);
90 return log_error_errno(r
, "Failed to get description: %s", bus_error_message(&error
, r
));
92 r
= sd_bus_message_read(reply
, "s", &text
);
94 return bus_log_parse_error(r
);
96 r
= json_parse(text
, 0, ret
, NULL
, NULL
);
98 return log_error_errno(r
, "Failed to parse JSON: %m");
103 static int dump_manager_description(sd_bus
*bus
) {
104 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
107 r
= get_description(bus
, &v
);
111 json_variant_dump(v
, arg_json_format_flags
, NULL
, NULL
);
115 static int dump_link_description(sd_bus
*bus
, char **patterns
) {
116 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
117 _cleanup_free_
bool *matched_patterns
= NULL
;
122 r
= get_description(bus
, &v
);
126 matched_patterns
= new0(bool, strv_length(patterns
));
127 if (!matched_patterns
)
130 JSON_VARIANT_ARRAY_FOREACH(i
, json_variant_by_key(v
, "Interfaces")) {
131 char ifindex_str
[DECIMAL_STR_MAX(int64_t)];
136 name
= json_variant_string(json_variant_by_key(i
, "Name"));
137 index
= json_variant_integer(json_variant_by_key(i
, "Index"));
138 xsprintf(ifindex_str
, "%ji", index
);
140 if (!strv_fnmatch_full(patterns
, ifindex_str
, 0, &pos
) &&
141 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
145 JSON_VARIANT_ARRAY_FOREACH(a
, json_variant_by_key(i
, "AlternativeNames"))
146 if (strv_fnmatch_full(patterns
, json_variant_string(a
), 0, &pos
)) {
155 matched_patterns
[pos
] = true;
156 json_variant_dump(i
, arg_json_format_flags
, NULL
, NULL
);
160 /* Look if we matched all our arguments that are not globs. It is OK for a glob to match
161 * nothing, but not for an exact argument. */
162 for (size_t pos
= 0; pos
< strv_length(patterns
); pos
++) {
163 if (matched_patterns
[pos
])
166 if (string_is_glob(patterns
[pos
]))
167 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
170 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
171 "Interface \"%s\" not found.", patterns
[pos
]);
175 log_warning("No interfaces matched.");
180 static void operational_state_to_color(const char *name
, const char *state
, const char **on
, const char **off
) {
181 if (STRPTR_IN_SET(state
, "routable", "enslaved") ||
182 (streq_ptr(name
, "lo") && streq_ptr(state
, "carrier"))) {
184 *on
= ansi_highlight_green();
186 *off
= ansi_normal();
187 } else if (streq_ptr(state
, "degraded")) {
189 *on
= ansi_highlight_yellow();
191 *off
= ansi_normal();
200 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
201 if (streq_ptr(state
, "configured")) {
203 *on
= ansi_highlight_green();
205 *off
= ansi_normal();
206 } else if (streq_ptr(state
, "configuring")) {
208 *on
= ansi_highlight_yellow();
210 *off
= ansi_normal();
211 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
213 *on
= ansi_highlight_red();
215 *off
= ansi_normal();
224 static void online_state_to_color(const char *state
, const char **on
, const char **off
) {
225 if (streq_ptr(state
, "online")) {
227 *on
= ansi_highlight_green();
229 *off
= ansi_normal();
230 } else if (streq_ptr(state
, "partial")) {
232 *on
= ansi_highlight_yellow();
234 *off
= ansi_normal();
243 typedef struct VxLanInfo
{
250 union in_addr_union local
;
251 union in_addr_union group
;
264 typedef struct LinkInfo
{
265 char name
[IFNAMSIZ
+1];
267 sd_device
*sd_device
;
269 unsigned short iftype
;
270 struct hw_addr_data hw_address
;
271 struct hw_addr_data permanent_hw_address
;
278 uint8_t addr_gen_mode
;
280 char **alternative_names
;
283 struct rtnl_link_stats64 stats64
;
284 struct rtnl_link_stats stats
;
291 uint32_t forward_delay
;
294 uint32_t ageing_time
;
298 uint8_t mcast_igmp_version
;
302 VxLanInfo vxlan_info
;
315 uint16_t tunnel_port
;
318 union in_addr_union local
;
319 union in_addr_union remote
;
327 /* macvlan and macvtap info */
328 uint32_t macvlan_mode
;
331 uint16_t ipvlan_mode
;
332 uint16_t ipvlan_flags
;
341 enum nl80211_iftype wlan_iftype
;
343 struct ether_addr bssid
;
345 bool has_hw_address
:1;
346 bool has_permanent_hw_address
:1;
347 bool has_tx_queues
:1;
348 bool has_rx_queues
:1;
352 bool has_ethtool_link_info
:1;
353 bool has_wlan_link_info
:1;
354 bool has_tunnel_ipv4
:1;
355 bool has_ipv6_address_generation_mode
:1;
357 bool needs_freeing
:1;
360 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
361 return CMP(a
->ifindex
, b
->ifindex
);
364 static LinkInfo
* link_info_array_free(LinkInfo
*array
) {
365 for (unsigned i
= 0; array
&& array
[i
].needs_freeing
; i
++) {
366 sd_device_unref(array
[i
].sd_device
);
367 free(array
[i
].netdev_kind
);
369 free(array
[i
].qdisc
);
370 strv_free(array
[i
].alternative_names
);
375 DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo
*, link_info_array_free
);
377 static int decode_netdev(sd_netlink_message
*m
, LinkInfo
*info
) {
383 r
= sd_netlink_message_enter_container(m
, IFLA_LINKINFO
);
387 r
= sd_netlink_message_read_string_strdup(m
, IFLA_INFO_KIND
, &info
->netdev_kind
);
389 (void) sd_netlink_message_exit_container(m
);
393 r
= sd_netlink_message_enter_container(m
, IFLA_INFO_DATA
);
397 if (streq(info
->netdev_kind
, "bridge")) {
398 (void) sd_netlink_message_read_u32(m
, IFLA_BR_FORWARD_DELAY
, &info
->forward_delay
);
399 (void) sd_netlink_message_read_u32(m
, IFLA_BR_HELLO_TIME
, &info
->hello_time
);
400 (void) sd_netlink_message_read_u32(m
, IFLA_BR_MAX_AGE
, &info
->max_age
);
401 (void) sd_netlink_message_read_u32(m
, IFLA_BR_AGEING_TIME
, &info
->ageing_time
);
402 (void) sd_netlink_message_read_u32(m
, IFLA_BR_STP_STATE
, &info
->stp_state
);
403 (void) sd_netlink_message_read_u32(m
, IFLA_BRPORT_COST
, &info
->cost
);
404 (void) sd_netlink_message_read_u16(m
, IFLA_BR_PRIORITY
, &info
->priority
);
405 (void) sd_netlink_message_read_u8(m
, IFLA_BR_MCAST_IGMP_VERSION
, &info
->mcast_igmp_version
);
406 (void) sd_netlink_message_read_u8(m
, IFLA_BRPORT_STATE
, &info
->port_state
);
407 } if (streq(info
->netdev_kind
, "bond")) {
408 (void) sd_netlink_message_read_u8(m
, IFLA_BOND_MODE
, &info
->mode
);
409 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_MIIMON
, &info
->miimon
);
410 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_DOWNDELAY
, &info
->downdelay
);
411 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_UPDELAY
, &info
->updelay
);
412 } else if (streq(info
->netdev_kind
, "vxlan")) {
413 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_ID
, &info
->vxlan_info
.vni
);
415 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_GROUP
, &info
->vxlan_info
.group
.in
);
417 info
->vxlan_info
.group_family
= AF_INET
;
419 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_GROUP6
, &info
->vxlan_info
.group
.in6
);
421 info
->vxlan_info
.group_family
= AF_INET6
;
424 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_LOCAL
, &info
->vxlan_info
.local
.in
);
426 info
->vxlan_info
.local_family
= AF_INET
;
428 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_LOCAL6
, &info
->vxlan_info
.local
.in6
);
430 info
->vxlan_info
.local_family
= AF_INET6
;
433 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_LINK
, &info
->vxlan_info
.link
);
434 (void) sd_netlink_message_read_u16(m
, IFLA_VXLAN_PORT
, &info
->vxlan_info
.dest_port
);
435 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_PROXY
, &info
->vxlan_info
.proxy
);
436 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_LEARNING
, &info
->vxlan_info
.learning
);
437 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_RSC
, &info
->vxlan_info
.rsc
);
438 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L3MISS
, &info
->vxlan_info
.l3miss
);
439 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L2MISS
, &info
->vxlan_info
.l2miss
);
440 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TOS
, &info
->vxlan_info
.tos
);
441 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TTL
, &info
->vxlan_info
.ttl
);
442 } else if (streq(info
->netdev_kind
, "vlan"))
443 (void) sd_netlink_message_read_u16(m
, IFLA_VLAN_ID
, &info
->vlan_id
);
444 else if (STR_IN_SET(info
->netdev_kind
, "ipip", "sit")) {
445 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_LOCAL
, &info
->local
.in
);
446 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_REMOTE
, &info
->remote
.in
);
447 } else if (streq(info
->netdev_kind
, "geneve")) {
448 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_ID
, &info
->vni
);
450 r
= sd_netlink_message_read_in_addr(m
, IFLA_GENEVE_REMOTE
, &info
->remote
.in
);
452 info
->has_tunnel_ipv4
= true;
454 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GENEVE_REMOTE6
, &info
->remote
.in6
);
456 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL
, &info
->ttl
);
457 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL_INHERIT
, &info
->inherit
);
458 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TOS
, &info
->tos
);
459 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_DF
, &info
->df
);
460 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_CSUM
, &info
->csum
);
461 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
, &info
->csum6_tx
);
462 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
, &info
->csum6_rx
);
463 (void) sd_netlink_message_read_u16(m
, IFLA_GENEVE_PORT
, &info
->tunnel_port
);
464 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_LABEL
, &info
->label
);
465 } else if (STR_IN_SET(info
->netdev_kind
, "gre", "gretap", "erspan")) {
466 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in
);
467 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in
);
468 } else if (STR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan")) {
469 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in6
);
470 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in6
);
471 } else if (streq(info
->netdev_kind
, "vti")) {
472 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in
);
473 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in
);
474 } else if (streq(info
->netdev_kind
, "vti6")) {
475 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in6
);
476 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in6
);
477 } else if (STR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap"))
478 (void) sd_netlink_message_read_u32(m
, IFLA_MACVLAN_MODE
, &info
->macvlan_mode
);
479 else if (streq(info
->netdev_kind
, "ipvlan")) {
480 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_MODE
, &info
->ipvlan_mode
);
481 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_FLAGS
, &info
->ipvlan_flags
);
484 (void) sd_netlink_message_exit_container(m
);
485 (void) sd_netlink_message_exit_container(m
);
490 static int decode_link(sd_netlink_message
*m
, LinkInfo
*info
, char **patterns
, bool matched_patterns
[]) {
491 _cleanup_strv_free_
char **altnames
= NULL
;
492 const char *name
, *qdisc
;
499 r
= sd_netlink_message_get_type(m
, &type
);
503 if (type
!= RTM_NEWLINK
)
506 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
510 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
514 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &altnames
);
515 if (r
< 0 && r
!= -ENODATA
)
519 char str
[DECIMAL_STR_MAX(int)];
522 assert(matched_patterns
);
524 xsprintf(str
, "%i", ifindex
);
525 if (!strv_fnmatch_full(patterns
, str
, 0, &pos
) &&
526 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
529 STRV_FOREACH(p
, altnames
)
530 if (strv_fnmatch_full(patterns
, *p
, 0, &pos
)) {
538 matched_patterns
[pos
] = true;
541 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
545 strscpy(info
->name
, sizeof info
->name
, name
);
546 info
->ifindex
= ifindex
;
547 info
->alternative_names
= TAKE_PTR(altnames
);
549 info
->has_hw_address
=
550 netlink_message_read_hw_addr(m
, IFLA_ADDRESS
, &info
->hw_address
) >= 0 &&
551 info
->hw_address
.length
> 0;
553 info
->has_permanent_hw_address
=
554 (netlink_message_read_hw_addr(m
, IFLA_PERM_ADDRESS
, &info
->permanent_hw_address
) >= 0 ||
555 ethtool_get_permanent_hw_addr(NULL
, info
->name
, &info
->permanent_hw_address
) >= 0) &&
556 !hw_addr_is_null(&info
->permanent_hw_address
) &&
557 !hw_addr_equal(&info
->permanent_hw_address
, &info
->hw_address
);
559 (void) sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
);
560 (void) sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
);
561 (void) sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
);
563 info
->has_rx_queues
=
564 sd_netlink_message_read_u32(m
, IFLA_NUM_RX_QUEUES
, &info
->rx_queues
) >= 0 &&
567 info
->has_tx_queues
=
568 sd_netlink_message_read_u32(m
, IFLA_NUM_TX_QUEUES
, &info
->tx_queues
) >= 0 &&
571 if (sd_netlink_message_read(m
, IFLA_STATS64
, sizeof info
->stats64
, &info
->stats64
) >= 0)
572 info
->has_stats64
= true;
573 else if (sd_netlink_message_read(m
, IFLA_STATS
, sizeof info
->stats
, &info
->stats
) >= 0)
574 info
->has_stats
= true;
576 r
= sd_netlink_message_read_string(m
, IFLA_QDISC
, &qdisc
);
578 info
->qdisc
= strdup(qdisc
);
583 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, &info
->master
);
585 r
= sd_netlink_message_enter_container(m
, IFLA_AF_SPEC
);
587 r
= sd_netlink_message_enter_container(m
, AF_INET6
);
589 r
= sd_netlink_message_read_u8(m
, IFLA_INET6_ADDR_GEN_MODE
, &info
->addr_gen_mode
);
590 if (r
>= 0 && IN_SET(info
->addr_gen_mode
,
591 IN6_ADDR_GEN_MODE_EUI64
,
592 IN6_ADDR_GEN_MODE_NONE
,
593 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
,
594 IN6_ADDR_GEN_MODE_RANDOM
))
595 info
->has_ipv6_address_generation_mode
= true;
597 (void) sd_netlink_message_exit_container(m
);
599 (void) sd_netlink_message_exit_container(m
);
603 (void) decode_netdev(m
, info
);
608 static int link_get_property(
610 const LinkInfo
*link
,
612 sd_bus_message
**reply
,
614 const char *propname
) {
616 char ifindex_str
[DECIMAL_STR_MAX(int)];
617 _cleanup_free_
char *path
= NULL
;
620 xsprintf(ifindex_str
, "%i", link
->ifindex
);
622 r
= sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str
, &path
);
626 return sd_bus_call_method(
628 "org.freedesktop.network1",
630 "org.freedesktop.DBus.Properties",
639 static int acquire_link_bitrates(sd_bus
*bus
, LinkInfo
*link
) {
640 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
641 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
644 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.Link", "BitRates");
646 bool quiet
= sd_bus_error_has_names(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
,
647 BUS_ERROR_SPEED_METER_INACTIVE
);
649 return log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
650 r
, "Failed to query link bit rates: %s", bus_error_message(&error
, r
));
653 r
= sd_bus_message_enter_container(reply
, 'v', "(tt)");
655 return bus_log_parse_error(r
);
657 r
= sd_bus_message_read(reply
, "(tt)", &link
->tx_bitrate
, &link
->rx_bitrate
);
659 return bus_log_parse_error(r
);
661 r
= sd_bus_message_exit_container(reply
);
663 return bus_log_parse_error(r
);
665 link
->has_bitrates
= link
->tx_bitrate
!= UINT64_MAX
&& link
->rx_bitrate
!= UINT64_MAX
;
670 static void acquire_ether_link_info(int *fd
, LinkInfo
*link
) {
671 if (ethtool_get_link_info(fd
, link
->name
,
672 &link
->autonegotiation
,
676 link
->has_ethtool_link_info
= true;
679 static void acquire_wlan_link_info(LinkInfo
*link
) {
680 _cleanup_(sd_netlink_unrefp
) sd_netlink
*genl
= NULL
;
681 const char *type
= NULL
;
685 (void) sd_device_get_devtype(link
->sd_device
, &type
);
686 if (!streq_ptr(type
, "wlan"))
689 r
= sd_genl_socket_open(&genl
);
691 log_debug_errno(r
, "Failed to open generic netlink socket: %m");
695 (void) sd_netlink_inc_rcvbuf(genl
, RCVBUF_SIZE
);
697 r
= wifi_get_interface(genl
, link
->ifindex
, &link
->wlan_iftype
, &link
->ssid
);
699 log_debug_errno(r
, "%s: failed to query ssid: %m", link
->name
);
701 if (link
->wlan_iftype
== NL80211_IFTYPE_STATION
) {
702 k
= wifi_get_station(genl
, link
->ifindex
, &link
->bssid
);
704 log_debug_errno(k
, "%s: failed to query bssid: %m", link
->name
);
707 link
->has_wlan_link_info
= r
> 0 || k
> 0;
710 static int acquire_link_info(sd_bus
*bus
, sd_netlink
*rtnl
, char **patterns
, LinkInfo
**ret
) {
711 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
712 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
713 _cleanup_close_
int fd
= -1;
720 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
722 return rtnl_log_create_error(r
);
724 r
= sd_netlink_message_request_dump(req
, true);
726 return rtnl_log_create_error(r
);
728 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
730 return log_error_errno(r
, "Failed to enumerate links: %m");
732 _cleanup_free_
bool *matched_patterns
= NULL
;
734 matched_patterns
= new0(bool, strv_length(patterns
));
735 if (!matched_patterns
)
739 for (sd_netlink_message
*i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
740 if (!GREEDY_REALLOC0(links
, c
+ 2)) /* We keep one trailing one as marker */
743 r
= decode_link(i
, links
+ c
, patterns
, matched_patterns
);
749 links
[c
].needs_freeing
= true;
751 (void) sd_device_new_from_ifindex(&links
[c
].sd_device
, links
[c
].ifindex
);
753 acquire_ether_link_info(&fd
, &links
[c
]);
754 acquire_wlan_link_info(&links
[c
]);
759 /* Look if we matched all our arguments that are not globs. It
760 * is OK for a glob to match nothing, but not for an exact argument. */
761 for (size_t pos
= 0; pos
< strv_length(patterns
); pos
++) {
762 if (matched_patterns
[pos
])
765 if (string_is_glob(patterns
[pos
]))
766 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
769 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
770 "Interface \"%s\" not found.", patterns
[pos
]);
773 typesafe_qsort(links
, c
, link_info_compare
);
776 for (size_t j
= 0; j
< c
; j
++)
777 (void) acquire_link_bitrates(bus
, links
+ j
);
779 *ret
= TAKE_PTR(links
);
781 if (patterns
&& c
== 0)
782 log_warning("No interfaces matched.");
787 static int list_links(int argc
, char *argv
[], void *userdata
) {
788 sd_bus
*bus
= ASSERT_PTR(userdata
);
789 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
790 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
791 _cleanup_(table_unrefp
) Table
*table
= NULL
;
795 if (arg_json_format_flags
!= JSON_FORMAT_OFF
) {
796 if (arg_all
|| argc
<= 1)
797 return dump_manager_description(bus
);
799 return dump_link_description(bus
, strv_skip(argv
, 1));
802 r
= sd_netlink_open(&rtnl
);
804 return log_error_errno(r
, "Failed to connect to netlink: %m");
806 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
810 pager_open(arg_pager_flags
);
812 table
= table_new("idx", "link", "type", "operational", "setup");
817 table_set_width(table
, 0);
819 table_set_header(table
, arg_legend
);
820 if (table_set_empty_string(table
, "n/a") < 0)
823 assert_se(cell
= table_get_cell(table
, 0, 0));
824 (void) table_set_minimum_width(table
, cell
, 3);
825 (void) table_set_weight(table
, cell
, 0);
826 (void) table_set_ellipsize_percent(table
, cell
, 100);
827 (void) table_set_align_percent(table
, cell
, 100);
829 assert_se(cell
= table_get_cell(table
, 0, 1));
830 (void) table_set_ellipsize_percent(table
, cell
, 100);
832 for (int i
= 0; i
< c
; i
++) {
833 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
834 const char *on_color_operational
, *on_color_setup
;
835 _cleanup_free_
char *t
= NULL
;
837 (void) sd_network_link_get_operational_state(links
[i
].ifindex
, &operational_state
);
838 operational_state_to_color(links
[i
].name
, operational_state
, &on_color_operational
, NULL
);
840 r
= sd_network_link_get_setup_state(links
[i
].ifindex
, &setup_state
);
841 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
842 setup_state
= strdup("unmanaged");
843 setup_state_to_color(setup_state
, &on_color_setup
, NULL
);
845 r
= net_get_type_string(links
[i
].sd_device
, links
[i
].iftype
, &t
);
849 r
= table_add_many(table
,
850 TABLE_INT
, links
[i
].ifindex
,
851 TABLE_STRING
, links
[i
].name
,
853 TABLE_STRING
, operational_state
,
854 TABLE_SET_COLOR
, on_color_operational
,
855 TABLE_STRING
, setup_state
,
856 TABLE_SET_COLOR
, on_color_setup
);
858 return table_log_add_error(r
);
861 r
= table_print(table
, NULL
);
863 return table_log_print_error(r
);
866 printf("\n%i links listed.\n", c
);
871 /* IEEE Organizationally Unique Identifier vendor string */
872 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
873 const char *description
;
874 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc
;
885 /* skip commonly misused 00:00:00 (Xerox) prefix */
886 if (memcmp(mac
, "\0\0\0", 3) == 0)
889 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
,
890 ETHER_ADDR_FORMAT_VAL(*mac
));
892 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
896 desc
= strdup(description
);
905 static int get_gateway_description(
910 union in_addr_union
*gateway
,
911 char **gateway_description
) {
912 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
916 assert(ifindex
>= 0);
917 assert(IN_SET(family
, AF_INET
, AF_INET6
));
919 assert(gateway_description
);
921 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
925 r
= sd_netlink_message_request_dump(req
, true);
929 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
933 for (sd_netlink_message
*m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
934 union in_addr_union gw
= IN_ADDR_NULL
;
935 struct ether_addr mac
= ETHER_ADDR_NULL
;
939 r
= sd_netlink_message_get_errno(m
);
941 log_error_errno(r
, "got error: %m");
945 r
= sd_netlink_message_get_type(m
, &type
);
947 log_error_errno(r
, "could not get type: %m");
951 if (type
!= RTM_NEWNEIGH
) {
952 log_error("type is not RTM_NEWNEIGH");
956 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
958 log_error_errno(r
, "could not get family: %m");
963 log_error("family is not correct");
967 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
969 log_error_errno(r
, "could not get ifindex: %m");
973 if (ifindex
> 0 && ifi
!= ifindex
)
978 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
984 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
993 if (!in_addr_equal(fam
, &gw
, gateway
))
996 r
= sd_netlink_message_read(m
, NDA_LLADDR
, sizeof(mac
), &mac
);
1000 r
= ieee_oui(hwdb
, &mac
, gateway_description
);
1010 static int dump_list(Table
*table
, const char *prefix
, char * const *l
) {
1013 if (strv_isempty(l
))
1016 r
= table_add_many(table
,
1018 TABLE_STRING
, prefix
,
1021 return table_log_add_error(r
);
1026 static int dump_gateways(
1031 _cleanup_free_
struct local_address
*local
= NULL
;
1032 _cleanup_strv_free_
char **buf
= NULL
;
1038 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local
);
1042 for (int i
= 0; i
< n
; i
++) {
1043 _cleanup_free_
char *gateway
= NULL
, *description
= NULL
;
1045 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &gateway
);
1049 r
= get_gateway_description(rtnl
, hwdb
, local
[i
].ifindex
, local
[i
].family
, &local
[i
].address
, &description
);
1051 log_debug_errno(r
, "Could not get description of gateway, ignoring: %m");
1054 if (!strextend(&gateway
, " (", description
, ")"))
1058 /* Show interface name for the entry if we show entries for all interfaces */
1059 r
= strv_extendf(&buf
, "%s%s%s",
1061 ifindex
<= 0 ? " on " : "",
1062 ifindex
<= 0 ? FORMAT_IFNAME_FULL(local
[i
].ifindex
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
) : "");
1067 return dump_list(table
, "Gateway:", buf
);
1070 static int dump_addresses(
1072 sd_dhcp_lease
*lease
,
1076 _cleanup_free_
struct local_address
*local
= NULL
;
1077 _cleanup_strv_free_
char **buf
= NULL
;
1078 struct in_addr dhcp4_address
= {};
1084 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local
);
1089 (void) sd_dhcp_lease_get_address(lease
, &dhcp4_address
);
1091 for (int i
= 0; i
< n
; i
++) {
1092 _cleanup_free_
char *pretty
= NULL
;
1094 r
= in_addr_to_string(local
[i
].family
, &local
[i
].address
, &pretty
);
1098 if (local
[i
].family
== AF_INET
&& in4_addr_equal(&local
[i
].address
.in
, &dhcp4_address
)) {
1099 struct in_addr server_address
;
1100 char *p
, s
[INET_ADDRSTRLEN
];
1102 r
= sd_dhcp_lease_get_server_identifier(lease
, &server_address
);
1103 if (r
>= 0 && inet_ntop(AF_INET
, &server_address
, s
, sizeof(s
)))
1104 p
= strjoin(pretty
, " (DHCP4 via ", s
, ")");
1106 p
= strjoin(pretty
, " (DHCP4)");
1110 free_and_replace(pretty
, p
);
1113 r
= strv_extendf(&buf
, "%s%s%s",
1115 ifindex
<= 0 ? " on " : "",
1116 ifindex
<= 0 ? FORMAT_IFNAME_FULL(local
[i
].ifindex
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
) : "");
1121 return dump_list(table
, "Address:", buf
);
1124 static int dump_address_labels(sd_netlink
*rtnl
) {
1125 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1126 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1132 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
1134 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
1136 r
= sd_netlink_message_request_dump(req
, true);
1140 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1144 table
= table_new("label", "prefix/prefixlen");
1149 table_set_width(table
, 0);
1151 r
= table_set_sort(table
, (size_t) 0);
1155 assert_se(cell
= table_get_cell(table
, 0, 0));
1156 (void) table_set_align_percent(table
, cell
, 100);
1157 (void) table_set_ellipsize_percent(table
, cell
, 100);
1159 assert_se(cell
= table_get_cell(table
, 0, 1));
1160 (void) table_set_align_percent(table
, cell
, 100);
1162 for (sd_netlink_message
*m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
1163 _cleanup_free_
char *pretty
= NULL
;
1164 union in_addr_union prefix
= IN_ADDR_NULL
;
1168 r
= sd_netlink_message_get_errno(m
);
1170 log_error_errno(r
, "got error: %m");
1174 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
1175 if (r
< 0 && r
!= -ENODATA
) {
1176 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
1180 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
.in6
);
1184 r
= in_addr_to_string(AF_INET6
, &prefix
, &pretty
);
1188 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
1192 r
= table_add_cell(table
, NULL
, TABLE_UINT32
, &label
);
1194 return table_log_add_error(r
);
1196 r
= table_add_cell_stringf(table
, NULL
, "%s/%u", pretty
, prefixlen
);
1198 return table_log_add_error(r
);
1201 r
= table_print(table
, NULL
);
1203 return table_log_print_error(r
);
1208 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
1209 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1212 r
= sd_netlink_open(&rtnl
);
1214 return log_error_errno(r
, "Failed to connect to netlink: %m");
1216 dump_address_labels(rtnl
);
1221 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
1222 char p
[STRLEN("/run/systemd/netif/lldp/") + DECIMAL_STR_MAX(int)];
1225 xsprintf(p
, "/run/systemd/netif/lldp/%i", ifindex
);
1234 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
1235 _cleanup_free_
void *raw
= NULL
;
1243 l
= fread(&u
, 1, sizeof(u
), f
);
1244 if (l
== 0 && feof(f
))
1249 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
1250 if (le64toh(u
) >= 4096)
1253 raw
= new(uint8_t, le64toh(u
));
1257 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
1260 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
1267 static int dump_lldp_neighbors(Table
*table
, const char *prefix
, int ifindex
) {
1268 _cleanup_strv_free_
char **buf
= NULL
;
1269 _cleanup_fclose_
FILE *f
= NULL
;
1274 assert(ifindex
> 0);
1276 r
= open_lldp_neighbors(ifindex
, &f
);
1283 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
1284 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1286 r
= next_lldp_neighbor(f
, &n
);
1292 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1293 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1294 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1296 r
= strv_extendf(&buf
, "%s on port %s%s%s%s",
1299 isempty(port_description
) ? "" : " (",
1300 strempty(port_description
),
1301 isempty(port_description
) ? "" : ")");
1306 return dump_list(table
, prefix
, buf
);
1309 static int dump_dhcp_leases(Table
*table
, const char *prefix
, sd_bus
*bus
, const LinkInfo
*link
) {
1310 _cleanup_strv_free_
char **buf
= NULL
;
1311 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1312 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1315 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.DHCPServer", "Leases");
1317 bool quiet
= sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
);
1319 log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
1320 r
, "Failed to query link DHCP leases: %s", bus_error_message(&error
, r
));
1324 r
= sd_bus_message_enter_container(reply
, 'v', "a(uayayayayt)");
1326 return bus_log_parse_error(r
);
1328 r
= sd_bus_message_enter_container(reply
, 'a', "(uayayayayt)");
1330 return bus_log_parse_error(r
);
1332 while ((r
= sd_bus_message_enter_container(reply
, 'r', "uayayayayt")) > 0) {
1333 _cleanup_free_
char *id
= NULL
, *ip
= NULL
;
1334 const void *client_id
, *addr
, *gtw
, *hwaddr
;
1335 size_t client_id_sz
, sz
;
1336 uint64_t expiration
;
1339 r
= sd_bus_message_read(reply
, "u", &family
);
1341 return bus_log_parse_error(r
);
1343 r
= sd_bus_message_read_array(reply
, 'y', &client_id
, &client_id_sz
);
1345 return bus_log_parse_error(r
);
1347 r
= sd_bus_message_read_array(reply
, 'y', &addr
, &sz
);
1348 if (r
< 0 || sz
!= 4)
1349 return bus_log_parse_error(r
);
1351 r
= sd_bus_message_read_array(reply
, 'y', >w
, &sz
);
1352 if (r
< 0 || sz
!= 4)
1353 return bus_log_parse_error(r
);
1355 r
= sd_bus_message_read_array(reply
, 'y', &hwaddr
, &sz
);
1357 return bus_log_parse_error(r
);
1359 r
= sd_bus_message_read_basic(reply
, 't', &expiration
);
1361 return bus_log_parse_error(r
);
1363 r
= sd_dhcp_client_id_to_string(client_id
, client_id_sz
, &id
);
1365 return bus_log_parse_error(r
);
1367 r
= in_addr_to_string(family
, addr
, &ip
);
1369 return bus_log_parse_error(r
);
1371 r
= strv_extendf(&buf
, "%s (to %s)", ip
, id
);
1375 r
= sd_bus_message_exit_container(reply
);
1377 return bus_log_parse_error(r
);
1380 return bus_log_parse_error(r
);
1382 r
= sd_bus_message_exit_container(reply
);
1384 return bus_log_parse_error(r
);
1386 r
= sd_bus_message_exit_container(reply
);
1388 return bus_log_parse_error(r
);
1390 if (strv_isempty(buf
)) {
1391 r
= strv_extendf(&buf
, "none");
1396 return dump_list(table
, prefix
, buf
);
1399 static int dump_ifindexes(Table
*table
, const char *prefix
, const int *ifindexes
) {
1404 if (!ifindexes
|| ifindexes
[0] <= 0)
1407 for (unsigned c
= 0; ifindexes
[c
] > 0; c
++) {
1408 r
= table_add_many(table
,
1410 TABLE_STRING
, c
== 0 ? prefix
: "",
1411 TABLE_IFINDEX
, ifindexes
[c
]);
1413 return table_log_add_error(r
);
1419 #define DUMP_STATS_ONE(name, val_name) \
1420 r = table_add_many(table, \
1422 TABLE_STRING, name ":"); \
1424 return table_log_add_error(r); \
1425 r = table_add_cell(table, NULL, \
1426 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
1427 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
1429 return table_log_add_error(r);
1431 static int dump_statistics(Table
*table
, const LinkInfo
*info
) {
1437 if (!info
->has_stats64
&& !info
->has_stats
)
1440 DUMP_STATS_ONE("Rx Packets", rx_packets
);
1441 DUMP_STATS_ONE("Tx Packets", tx_packets
);
1442 DUMP_STATS_ONE("Rx Bytes", rx_bytes
);
1443 DUMP_STATS_ONE("Tx Bytes", tx_bytes
);
1444 DUMP_STATS_ONE("Rx Errors", rx_errors
);
1445 DUMP_STATS_ONE("Tx Errors", tx_errors
);
1446 DUMP_STATS_ONE("Rx Dropped", rx_dropped
);
1447 DUMP_STATS_ONE("Tx Dropped", tx_dropped
);
1448 DUMP_STATS_ONE("Multicast Packets", multicast
);
1449 DUMP_STATS_ONE("Collisions", collisions
);
1454 static int dump_hw_address(Table
*table
, sd_hwdb
*hwdb
, const char *field
, const struct hw_addr_data
*addr
) {
1455 _cleanup_free_
char *description
= NULL
;
1462 if (addr
->length
== ETH_ALEN
)
1463 (void) ieee_oui(hwdb
, &addr
->ether
, &description
);
1465 r
= table_add_many(table
,
1467 TABLE_STRING
, field
);
1469 return table_log_add_error(r
);
1471 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s%s",
1472 HW_ADDR_TO_STR(addr
),
1473 description
? " (" : "",
1474 strempty(description
),
1475 description
? ")" : "");
1477 return table_log_add_error(r
);
1482 static OutputFlags
get_output_flags(void) {
1484 arg_all
* OUTPUT_SHOW_ALL
|
1485 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
1486 colors_enabled() * OUTPUT_COLOR
;
1489 static int show_logs(const LinkInfo
*info
) {
1490 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1496 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1498 return log_error_errno(r
, "Failed to open journal: %m");
1500 r
= add_match_this_boot(j
, NULL
);
1502 return log_error_errno(r
, "Failed to add boot matches: %m");
1505 char m1
[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)];
1506 const char *m2
, *m3
;
1509 xsprintf(m1
, "_KERNEL_DEVICE=n%i", info
->ifindex
);
1511 m2
= strjoina("INTERFACE=", info
->name
);
1513 m3
= strjoina("DEVICE=", info
->name
);
1516 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1517 (r
= sd_journal_add_disjunction(j
)) ||
1518 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1519 (r
= sd_journal_add_disjunction(j
)) ||
1520 (r
= sd_journal_add_match(j
, m3
, 0))
1523 return log_error_errno(r
, "Failed to add link matches: %m");
1525 r
= add_matches_for_unit(j
, "systemd-networkd.service");
1527 return log_error_errno(r
, "Failed to add unit matches: %m");
1529 r
= add_matches_for_unit(j
, "systemd-networkd-wait-online.service");
1531 return log_error_errno(r
, "Failed to add unit matches: %m");
1534 return show_journal(
1541 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
1545 static int link_status_one(
1549 const LinkInfo
*info
) {
1551 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **sip
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
1552 _cleanup_free_
char *t
= NULL
, *network
= NULL
, *iaid
= NULL
, *duid
= NULL
,
1553 *setup_state
= NULL
, *operational_state
= NULL
, *online_state
= NULL
, *activation_policy
= NULL
;
1554 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
,
1555 *on_color_operational
, *off_color_operational
, *on_color_setup
, *off_color_setup
, *on_color_online
;
1556 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
1557 _cleanup_(sd_dhcp_lease_unrefp
) sd_dhcp_lease
*lease
= NULL
;
1558 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1565 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
1566 operational_state_to_color(info
->name
, operational_state
, &on_color_operational
, &off_color_operational
);
1568 (void) sd_network_link_get_online_state(info
->ifindex
, &online_state
);
1569 online_state_to_color(online_state
, &on_color_online
, NULL
);
1571 r
= sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
1572 if (r
== -ENODATA
) /* If there's no info available about this iface, it's unmanaged by networkd */
1573 setup_state
= strdup("unmanaged");
1574 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
1576 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
1577 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
1578 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
1579 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
1580 (void) sd_network_link_get_sip(info
->ifindex
, &sip
);
1582 if (info
->sd_device
) {
1583 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_LINK_FILE", &link
);
1584 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_DRIVER", &driver
);
1585 (void) sd_device_get_property_value(info
->sd_device
, "ID_PATH", &path
);
1587 if (sd_device_get_property_value(info
->sd_device
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
1588 (void) sd_device_get_property_value(info
->sd_device
, "ID_VENDOR", &vendor
);
1590 if (sd_device_get_property_value(info
->sd_device
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
1591 (void) sd_device_get_property_value(info
->sd_device
, "ID_MODEL", &model
);
1594 r
= net_get_type_string(info
->sd_device
, info
->iftype
, &t
);
1598 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
1600 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
1601 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
1603 char lease_file
[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)];
1604 xsprintf(lease_file
, "/run/systemd/netif/leases/%i", info
->ifindex
);
1606 (void) dhcp_lease_load(&lease
, lease_file
);
1608 table
= table_new("dot", "key", "value");
1613 table_set_width(table
, 0);
1615 assert_se(cell
= table_get_cell(table
, 0, 0));
1616 (void) table_set_ellipsize_percent(table
, cell
, 100);
1618 assert_se(cell
= table_get_cell(table
, 0, 1));
1619 (void) table_set_ellipsize_percent(table
, cell
, 100);
1621 table_set_header(table
, false);
1623 r
= table_add_many(table
,
1624 TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
),
1625 TABLE_SET_COLOR
, on_color_operational
);
1627 return table_log_add_error(r
);
1628 r
= table_add_cell_stringf(table
, &cell
, "%i: %s", info
->ifindex
, info
->name
);
1630 return table_log_add_error(r
);
1631 (void) table_set_align_percent(table
, cell
, 0);
1633 r
= table_add_many(table
,
1636 TABLE_STRING
, "Link File:",
1637 TABLE_SET_ALIGN_PERCENT
, 100,
1638 TABLE_STRING
, strna(link
),
1640 TABLE_STRING
, "Network File:",
1641 TABLE_STRING
, strna(network
),
1643 TABLE_STRING
, "Type:",
1644 TABLE_STRING
, strna(t
),
1646 TABLE_STRING
, "Kind:",
1647 TABLE_STRING
, strna(info
->netdev_kind
),
1649 TABLE_STRING
, "State:");
1651 return table_log_add_error(r
);
1652 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s (%s%s%s)",
1653 on_color_operational
, strna(operational_state
), off_color_operational
,
1654 on_color_setup
, strna(setup_state
), off_color_setup
);
1656 return table_log_add_error(r
);
1658 r
= table_add_many(table
,
1660 TABLE_STRING
, "Online state:",
1661 TABLE_STRING
, online_state
?: "unknown",
1662 TABLE_SET_COLOR
, on_color_online
);
1664 return table_log_add_error(r
);
1666 strv_sort(info
->alternative_names
);
1667 r
= dump_list(table
, "Alternative Names:", info
->alternative_names
);
1672 r
= table_add_many(table
,
1674 TABLE_STRING
, "Path:",
1675 TABLE_STRING
, path
);
1677 return table_log_add_error(r
);
1680 r
= table_add_many(table
,
1682 TABLE_STRING
, "Driver:",
1683 TABLE_STRING
, driver
);
1685 return table_log_add_error(r
);
1688 r
= table_add_many(table
,
1690 TABLE_STRING
, "Vendor:",
1691 TABLE_STRING
, vendor
);
1693 return table_log_add_error(r
);
1696 r
= table_add_many(table
,
1698 TABLE_STRING
, "Model:",
1699 TABLE_STRING
, model
);
1701 return table_log_add_error(r
);
1704 if (info
->has_hw_address
) {
1705 r
= dump_hw_address(table
, hwdb
, "Hardware Address:", &info
->hw_address
);
1710 if (info
->has_permanent_hw_address
) {
1711 r
= dump_hw_address(table
, hwdb
, "Permanent Hardware Address:", &info
->permanent_hw_address
);
1716 if (info
->mtu
> 0) {
1717 char min_str
[DECIMAL_STR_MAX(uint32_t)], max_str
[DECIMAL_STR_MAX(uint32_t)];
1719 xsprintf(min_str
, "%" PRIu32
, info
->min_mtu
);
1720 xsprintf(max_str
, "%" PRIu32
, info
->max_mtu
);
1722 r
= table_add_many(table
,
1724 TABLE_STRING
, "MTU:");
1726 return table_log_add_error(r
);
1727 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"%s%s%s%s%s%s%s",
1729 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? " (" : "",
1730 info
->min_mtu
> 0 ? "min: " : "",
1731 info
->min_mtu
> 0 ? min_str
: "",
1732 info
->min_mtu
> 0 && info
->max_mtu
> 0 ? ", " : "",
1733 info
->max_mtu
> 0 ? "max: " : "",
1734 info
->max_mtu
> 0 ? max_str
: "",
1735 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? ")" : "");
1737 return table_log_add_error(r
);
1741 r
= table_add_many(table
,
1743 TABLE_STRING
, "QDisc:",
1744 TABLE_STRING
, info
->qdisc
);
1746 return table_log_add_error(r
);
1749 if (info
->master
> 0) {
1750 r
= table_add_many(table
,
1752 TABLE_STRING
, "Master:",
1753 TABLE_IFINDEX
, info
->master
);
1755 return table_log_add_error(r
);
1758 if (info
->has_ipv6_address_generation_mode
) {
1759 static const struct {
1764 { "stable-privacy" },
1768 r
= table_add_many(table
,
1770 TABLE_STRING
, "IPv6 Address Generation Mode:",
1771 TABLE_STRING
, mode_table
[info
->addr_gen_mode
]);
1773 return table_log_add_error(r
);
1776 if (streq_ptr(info
->netdev_kind
, "bridge")) {
1777 r
= table_add_many(table
,
1779 TABLE_STRING
, "Forward Delay:",
1780 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->forward_delay
),
1782 TABLE_STRING
, "Hello Time:",
1783 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->hello_time
),
1785 TABLE_STRING
, "Max Age:",
1786 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->max_age
),
1788 TABLE_STRING
, "Ageing Time:",
1789 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->ageing_time
),
1791 TABLE_STRING
, "Priority:",
1792 TABLE_UINT16
, info
->priority
,
1794 TABLE_STRING
, "STP:",
1795 TABLE_BOOLEAN
, info
->stp_state
> 0,
1797 TABLE_STRING
, "Multicast IGMP Version:",
1798 TABLE_UINT8
, info
->mcast_igmp_version
,
1800 TABLE_STRING
, "Cost:",
1801 TABLE_UINT32
, info
->cost
);
1803 return table_log_add_error(r
);
1805 if (info
->port_state
<= BR_STATE_BLOCKING
)
1806 r
= table_add_many(table
,
1808 TABLE_STRING
, "Port State:",
1809 TABLE_STRING
, bridge_state_to_string(info
->port_state
));
1810 } else if (streq_ptr(info
->netdev_kind
, "bond")) {
1811 r
= table_add_many(table
,
1813 TABLE_STRING
, "Mode:",
1814 TABLE_STRING
, bond_mode_to_string(info
->mode
),
1816 TABLE_STRING
, "Miimon:",
1817 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->miimon
),
1819 TABLE_STRING
, "Updelay:",
1820 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->updelay
),
1822 TABLE_STRING
, "Downdelay:",
1823 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->downdelay
));
1825 return table_log_add_error(r
);
1827 } else if (streq_ptr(info
->netdev_kind
, "vxlan")) {
1828 char ttl
[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1830 if (info
->vxlan_info
.vni
> 0) {
1831 r
= table_add_many(table
,
1833 TABLE_STRING
, "VNI:",
1834 TABLE_UINT32
, info
->vxlan_info
.vni
);
1836 return table_log_add_error(r
);
1839 if (IN_SET(info
->vxlan_info
.group_family
, AF_INET
, AF_INET6
)) {
1842 r
= in_addr_is_multicast(info
->vxlan_info
.group_family
, &info
->vxlan_info
.group
);
1848 r
= table_add_many(table
,
1851 info
->vxlan_info
.group_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
,
1852 &info
->vxlan_info
.group
);
1854 return table_log_add_error(r
);
1857 if (IN_SET(info
->vxlan_info
.local_family
, AF_INET
, AF_INET6
)) {
1858 r
= table_add_many(table
,
1860 TABLE_STRING
, "Local:",
1861 info
->vxlan_info
.local_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
,
1862 &info
->vxlan_info
.local
);
1864 return table_log_add_error(r
);
1867 if (info
->vxlan_info
.dest_port
> 0) {
1868 r
= table_add_many(table
,
1870 TABLE_STRING
, "Destination Port:",
1871 TABLE_UINT16
, be16toh(info
->vxlan_info
.dest_port
));
1873 return table_log_add_error(r
);
1876 if (info
->vxlan_info
.link
> 0) {
1877 r
= table_add_many(table
,
1879 TABLE_STRING
, "Underlying Device:",
1880 TABLE_IFINDEX
, info
->vxlan_info
.link
);
1882 return table_log_add_error(r
);
1885 r
= table_add_many(table
,
1887 TABLE_STRING
, "Learning:",
1888 TABLE_BOOLEAN
, info
->vxlan_info
.learning
);
1890 return table_log_add_error(r
);
1892 r
= table_add_many(table
,
1894 TABLE_STRING
, "RSC:",
1895 TABLE_BOOLEAN
, info
->vxlan_info
.rsc
);
1897 return table_log_add_error(r
);
1899 r
= table_add_many(table
,
1901 TABLE_STRING
, "L3MISS:",
1902 TABLE_BOOLEAN
, info
->vxlan_info
.l3miss
);
1904 return table_log_add_error(r
);
1906 r
= table_add_many(table
,
1908 TABLE_STRING
, "L2MISS:",
1909 TABLE_BOOLEAN
, info
->vxlan_info
.l2miss
);
1911 return table_log_add_error(r
);
1913 if (info
->vxlan_info
.tos
> 1) {
1914 r
= table_add_many(table
,
1916 TABLE_STRING
, "TOS:",
1917 TABLE_UINT8
, info
->vxlan_info
.tos
);
1919 return table_log_add_error(r
);
1922 if (info
->vxlan_info
.ttl
> 0)
1923 xsprintf(ttl
, "%" PRIu8
, info
->vxlan_info
.ttl
);
1925 strcpy(ttl
, "auto");
1927 r
= table_add_many(table
,
1929 TABLE_STRING
, "TTL:",
1932 return table_log_add_error(r
);
1933 } else if (streq_ptr(info
->netdev_kind
, "vlan") && info
->vlan_id
> 0) {
1934 r
= table_add_many(table
,
1936 TABLE_STRING
, "VLan Id:",
1937 TABLE_UINT16
, info
->vlan_id
);
1939 return table_log_add_error(r
);
1940 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
1941 if (in_addr_is_set(AF_INET
, &info
->local
)) {
1942 r
= table_add_many(table
,
1944 TABLE_STRING
, "Local:",
1945 TABLE_IN_ADDR
, &info
->local
);
1947 return table_log_add_error(r
);
1950 if (in_addr_is_set(AF_INET
, &info
->remote
)) {
1951 r
= table_add_many(table
,
1953 TABLE_STRING
, "Remote:",
1954 TABLE_IN_ADDR
, &info
->remote
);
1956 return table_log_add_error(r
);
1958 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
1959 if (in_addr_is_set(AF_INET6
, &info
->local
)) {
1960 r
= table_add_many(table
,
1962 TABLE_STRING
, "Local:",
1963 TABLE_IN6_ADDR
, &info
->local
);
1965 return table_log_add_error(r
);
1968 if (in_addr_is_set(AF_INET6
, &info
->remote
)) {
1969 r
= table_add_many(table
,
1971 TABLE_STRING
, "Remote:",
1972 TABLE_IN6_ADDR
, &info
->remote
);
1974 return table_log_add_error(r
);
1976 } else if (streq_ptr(info
->netdev_kind
, "geneve")) {
1977 r
= table_add_many(table
,
1979 TABLE_STRING
, "VNI:",
1980 TABLE_UINT32
, info
->vni
);
1982 return table_log_add_error(r
);
1984 if (info
->has_tunnel_ipv4
&& in_addr_is_set(AF_INET
, &info
->remote
)) {
1985 r
= table_add_many(table
,
1987 TABLE_STRING
, "Remote:",
1988 TABLE_IN_ADDR
, &info
->remote
);
1990 return table_log_add_error(r
);
1991 } else if (in_addr_is_set(AF_INET6
, &info
->remote
)) {
1992 r
= table_add_many(table
,
1994 TABLE_STRING
, "Remote:",
1995 TABLE_IN6_ADDR
, &info
->remote
);
1997 return table_log_add_error(r
);
2000 if (info
->ttl
> 0) {
2001 r
= table_add_many(table
,
2003 TABLE_STRING
, "TTL:",
2004 TABLE_UINT8
, info
->ttl
);
2006 return table_log_add_error(r
);
2009 if (info
->tos
> 0) {
2010 r
= table_add_many(table
,
2012 TABLE_STRING
, "TOS:",
2013 TABLE_UINT8
, info
->tos
);
2015 return table_log_add_error(r
);
2018 r
= table_add_many(table
,
2020 TABLE_STRING
, "Port:",
2021 TABLE_UINT16
, info
->tunnel_port
);
2023 return table_log_add_error(r
);
2025 r
= table_add_many(table
,
2027 TABLE_STRING
, "Inherit:",
2028 TABLE_STRING
, geneve_df_to_string(info
->inherit
));
2030 return table_log_add_error(r
);
2033 r
= table_add_many(table
,
2035 TABLE_STRING
, "IPDoNotFragment:",
2036 TABLE_UINT8
, info
->df
);
2038 return table_log_add_error(r
);
2041 r
= table_add_many(table
,
2043 TABLE_STRING
, "UDPChecksum:",
2044 TABLE_BOOLEAN
, info
->csum
);
2046 return table_log_add_error(r
);
2048 r
= table_add_many(table
,
2050 TABLE_STRING
, "UDP6ZeroChecksumTx:",
2051 TABLE_BOOLEAN
, info
->csum6_tx
);
2053 return table_log_add_error(r
);
2055 r
= table_add_many(table
,
2057 TABLE_STRING
, "UDP6ZeroChecksumRx:",
2058 TABLE_BOOLEAN
, info
->csum6_rx
);
2060 return table_log_add_error(r
);
2062 if (info
->label
> 0) {
2063 r
= table_add_many(table
,
2065 TABLE_STRING
, "FlowLabel:",
2066 TABLE_UINT32
, info
->label
);
2068 return table_log_add_error(r
);
2070 } else if (STRPTR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap")) {
2071 r
= table_add_many(table
,
2073 TABLE_STRING
, "Mode:",
2074 TABLE_STRING
, macvlan_mode_to_string(info
->macvlan_mode
));
2076 return table_log_add_error(r
);
2077 } else if (streq_ptr(info
->netdev_kind
, "ipvlan")) {
2078 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
2080 if (info
->ipvlan_flags
& IPVLAN_F_PRIVATE
)
2081 p
= strdup("private");
2082 else if (info
->ipvlan_flags
& IPVLAN_F_VEPA
)
2085 p
= strdup("bridge");
2089 s
= strjoin(ipvlan_mode_to_string(info
->ipvlan_mode
), " (", p
, ")");
2093 r
= table_add_many(table
,
2095 TABLE_STRING
, "Mode:",
2098 return table_log_add_error(r
);
2101 if (info
->has_wlan_link_info
) {
2102 _cleanup_free_
char *esc
= NULL
;
2104 r
= table_add_many(table
,
2106 TABLE_STRING
, "WiFi access point:");
2108 return table_log_add_error(r
);
2111 esc
= cescape(info
->ssid
);
2113 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)",
2115 ETHER_ADDR_TO_STR(&info
->bssid
));
2117 return table_log_add_error(r
);
2120 if (info
->has_bitrates
) {
2121 r
= table_add_many(table
,
2123 TABLE_STRING
, "Bit Rate (Tx/Rx):");
2125 return table_log_add_error(r
);
2126 r
= table_add_cell_stringf(table
, NULL
, "%sbps/%sbps",
2127 FORMAT_BYTES_FULL(info
->tx_bitrate
, 0),
2128 FORMAT_BYTES_FULL(info
->rx_bitrate
, 0));
2130 return table_log_add_error(r
);
2133 if (info
->has_tx_queues
|| info
->has_rx_queues
) {
2134 r
= table_add_many(table
,
2136 TABLE_STRING
, "Queue Length (Tx/Rx):");
2138 return table_log_add_error(r
);
2139 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"/%" PRIu32
, info
->tx_queues
, info
->rx_queues
);
2141 return table_log_add_error(r
);
2144 if (info
->has_ethtool_link_info
) {
2145 const char *duplex
= duplex_to_string(info
->duplex
);
2146 const char *port
= port_to_string(info
->port
);
2148 if (IN_SET(info
->autonegotiation
, AUTONEG_DISABLE
, AUTONEG_ENABLE
)) {
2149 r
= table_add_many(table
,
2151 TABLE_STRING
, "Auto negotiation:",
2152 TABLE_BOOLEAN
, info
->autonegotiation
== AUTONEG_ENABLE
);
2154 return table_log_add_error(r
);
2157 if (info
->speed
> 0) {
2158 r
= table_add_many(table
,
2160 TABLE_STRING
, "Speed:",
2161 TABLE_BPS
, info
->speed
);
2163 return table_log_add_error(r
);
2167 r
= table_add_many(table
,
2169 TABLE_STRING
, "Duplex:",
2170 TABLE_STRING
, duplex
);
2172 return table_log_add_error(r
);
2176 r
= table_add_many(table
,
2178 TABLE_STRING
, "Port:",
2179 TABLE_STRING
, port
);
2181 return table_log_add_error(r
);
2185 r
= dump_addresses(rtnl
, lease
, table
, info
->ifindex
);
2188 r
= dump_gateways(rtnl
, hwdb
, table
, info
->ifindex
);
2191 r
= dump_list(table
, "DNS:", dns
);
2194 r
= dump_list(table
, "Search Domains:", search_domains
);
2197 r
= dump_list(table
, "Route Domains:", route_domains
);
2200 r
= dump_list(table
, "NTP:", ntp
);
2203 r
= dump_list(table
, "SIP:", sip
);
2206 r
= dump_ifindexes(table
, "Carrier Bound To:", carrier_bound_to
);
2209 r
= dump_ifindexes(table
, "Carrier Bound By:", carrier_bound_by
);
2213 r
= sd_network_link_get_activation_policy(info
->ifindex
, &activation_policy
);
2215 r
= table_add_many(table
,
2217 TABLE_STRING
, "Activation Policy:",
2218 TABLE_STRING
, activation_policy
);
2220 return table_log_add_error(r
);
2223 r
= sd_network_link_get_required_for_online(info
->ifindex
);
2225 r
= table_add_many(table
,
2227 TABLE_STRING
, "Required For Online:",
2230 return table_log_add_error(r
);
2234 const void *client_id
;
2235 size_t client_id_len
;
2238 r
= sd_dhcp_lease_get_timezone(lease
, &tz
);
2240 r
= table_add_many(table
,
2242 TABLE_STRING
, "Time Zone:",
2245 return table_log_add_error(r
);
2248 r
= sd_dhcp_lease_get_client_id(lease
, &client_id
, &client_id_len
);
2250 _cleanup_free_
char *id
= NULL
;
2252 r
= sd_dhcp_client_id_to_string(client_id
, client_id_len
, &id
);
2254 r
= table_add_many(table
,
2256 TABLE_STRING
, "DHCP4 Client ID:",
2259 return table_log_add_error(r
);
2264 r
= sd_network_link_get_dhcp6_client_iaid_string(info
->ifindex
, &iaid
);
2266 r
= table_add_many(table
,
2268 TABLE_STRING
, "DHCP6 Client IAID:",
2269 TABLE_STRING
, iaid
);
2271 return table_log_add_error(r
);
2274 r
= sd_network_link_get_dhcp6_client_duid_string(info
->ifindex
, &duid
);
2276 r
= table_add_many(table
,
2278 TABLE_STRING
, "DHCP6 Client DUID:",
2279 TABLE_STRING
, duid
);
2281 return table_log_add_error(r
);
2284 r
= dump_lldp_neighbors(table
, "Connected To:", info
->ifindex
);
2288 r
= dump_dhcp_leases(table
, "Offered DHCP leases:", bus
, info
);
2292 r
= dump_statistics(table
, info
);
2296 r
= table_print(table
, NULL
);
2298 return table_log_print_error(r
);
2300 return show_logs(info
);
2303 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
2304 _cleanup_free_
char *operational_state
= NULL
, *online_state
= NULL
;
2305 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
2306 const char *on_color_operational
, *on_color_online
;
2307 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2313 (void) sd_network_get_operational_state(&operational_state
);
2314 operational_state_to_color(NULL
, operational_state
, &on_color_operational
, NULL
);
2316 (void) sd_network_get_online_state(&online_state
);
2317 online_state_to_color(online_state
, &on_color_online
, NULL
);
2319 table
= table_new("dot", "key", "value");
2324 table_set_width(table
, 0);
2326 assert_se(cell
= table_get_cell(table
, 0, 0));
2327 (void) table_set_ellipsize_percent(table
, cell
, 100);
2329 assert_se(cell
= table_get_cell(table
, 0, 1));
2330 (void) table_set_align_percent(table
, cell
, 100);
2331 (void) table_set_ellipsize_percent(table
, cell
, 100);
2333 table_set_header(table
, false);
2335 r
= table_add_many(table
,
2336 TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
),
2337 TABLE_SET_COLOR
, on_color_operational
,
2338 TABLE_STRING
, "State:",
2339 TABLE_STRING
, strna(operational_state
),
2340 TABLE_SET_COLOR
, on_color_operational
,
2342 TABLE_STRING
, "Online state:",
2343 TABLE_STRING
, online_state
?: "unknown",
2344 TABLE_SET_COLOR
, on_color_online
);
2346 return table_log_add_error(r
);
2348 r
= dump_addresses(rtnl
, NULL
, table
, 0);
2351 r
= dump_gateways(rtnl
, hwdb
, table
, 0);
2355 (void) sd_network_get_dns(&dns
);
2356 r
= dump_list(table
, "DNS:", dns
);
2360 (void) sd_network_get_search_domains(&search_domains
);
2361 r
= dump_list(table
, "Search Domains:", search_domains
);
2365 (void) sd_network_get_route_domains(&route_domains
);
2366 r
= dump_list(table
, "Route Domains:", route_domains
);
2370 (void) sd_network_get_ntp(&ntp
);
2371 r
= dump_list(table
, "NTP:", ntp
);
2375 r
= table_print(table
, NULL
);
2377 return table_log_print_error(r
);
2379 return show_logs(NULL
);
2382 static int link_status(int argc
, char *argv
[], void *userdata
) {
2383 sd_bus
*bus
= ASSERT_PTR(userdata
);
2384 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2385 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
2386 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2389 if (arg_json_format_flags
!= JSON_FORMAT_OFF
) {
2390 if (arg_all
|| argc
<= 1)
2391 return dump_manager_description(bus
);
2393 return dump_link_description(bus
, strv_skip(argv
, 1));
2396 pager_open(arg_pager_flags
);
2398 r
= sd_netlink_open(&rtnl
);
2400 return log_error_errno(r
, "Failed to connect to netlink: %m");
2402 r
= sd_hwdb_new(&hwdb
);
2404 log_debug_errno(r
, "Failed to open hardware database: %m");
2407 c
= acquire_link_info(bus
, rtnl
, NULL
, &links
);
2409 return system_status(rtnl
, hwdb
);
2411 c
= acquire_link_info(bus
, rtnl
, argv
+ 1, &links
);
2415 for (int i
= 0; i
< c
; i
++) {
2417 fputc('\n', stdout
);
2419 link_status_one(bus
, rtnl
, hwdb
, links
+ i
);
2425 static char *lldp_capabilities_to_string(uint16_t x
) {
2426 static const char characters
[] = {
2427 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2432 ret
= new(char, ELEMENTSOF(characters
) + 1);
2436 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
2437 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
2443 static void lldp_capabilities_legend(uint16_t x
) {
2444 unsigned cols
= columns();
2445 static const char* const table
[] = {
2449 "w - WLAN Access Point",
2452 "d - DOCSIS cable device",
2454 "c - Customer VLAN",
2456 "m - Two-port MAC Relay (TPMR)",
2462 printf("\nCapability Flags:\n");
2463 for (unsigned w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
2464 if (x
& (1U << i
) || arg_all
) {
2467 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
2470 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
2475 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
2476 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2477 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2478 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2483 r
= sd_netlink_open(&rtnl
);
2485 return log_error_errno(r
, "Failed to connect to netlink: %m");
2487 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
2491 pager_open(arg_pager_flags
);
2493 table
= table_new("link",
2498 "port description");
2503 table_set_width(table
, 0);
2505 table_set_header(table
, arg_legend
);
2507 assert_se(cell
= table_get_cell(table
, 0, 0));
2508 table_set_minimum_width(table
, cell
, 16);
2510 assert_se(cell
= table_get_cell(table
, 0, 1));
2511 table_set_minimum_width(table
, cell
, 17);
2513 assert_se(cell
= table_get_cell(table
, 0, 2));
2514 table_set_minimum_width(table
, cell
, 16);
2516 assert_se(cell
= table_get_cell(table
, 0, 3));
2517 table_set_minimum_width(table
, cell
, 11);
2519 assert_se(cell
= table_get_cell(table
, 0, 4));
2520 table_set_minimum_width(table
, cell
, 17);
2522 assert_se(cell
= table_get_cell(table
, 0, 5));
2523 table_set_minimum_width(table
, cell
, 16);
2525 for (int i
= 0; i
< c
; i
++) {
2526 _cleanup_fclose_
FILE *f
= NULL
;
2528 r
= open_lldp_neighbors(links
[i
].ifindex
, &f
);
2532 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", links
[i
].ifindex
);
2537 _cleanup_free_
char *cid
= NULL
, *pid
= NULL
, *sname
= NULL
, *pdesc
= NULL
, *capabilities
= NULL
;
2538 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
;
2539 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
2542 r
= next_lldp_neighbor(f
, &n
);
2544 log_warning_errno(r
, "Failed to read neighbor data: %m");
2550 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
2551 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
2552 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
2553 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
2556 cid
= ellipsize(chassis_id
, 17, 100);
2562 pid
= ellipsize(port_id
, 17, 100);
2568 sname
= ellipsize(system_name
, 16, 100);
2570 system_name
= sname
;
2573 if (port_description
) {
2574 pdesc
= ellipsize(port_description
, 16, 100);
2576 port_description
= pdesc
;
2579 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
2580 capabilities
= lldp_capabilities_to_string(cc
);
2584 r
= table_add_many(table
,
2585 TABLE_STRING
, links
[i
].name
,
2586 TABLE_STRING
, strna(chassis_id
),
2587 TABLE_STRING
, strna(system_name
),
2588 TABLE_STRING
, strna(capabilities
),
2589 TABLE_STRING
, strna(port_id
),
2590 TABLE_STRING
, strna(port_description
));
2592 return table_log_add_error(r
);
2598 r
= table_print(table
, NULL
);
2600 return table_log_print_error(r
);
2603 lldp_capabilities_legend(all
);
2604 printf("\n%i neighbors listed.\n", m
);
2610 static int link_delete_send_message(sd_netlink
*rtnl
, int index
) {
2611 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2616 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_DELLINK
, index
);
2618 return rtnl_log_create_error(r
);
2620 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2627 static int link_up_down_send_message(sd_netlink
*rtnl
, char *command
, int index
) {
2628 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2633 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_SETLINK
, index
);
2635 return rtnl_log_create_error(r
);
2637 if (streq(command
, "up"))
2638 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2640 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2642 return log_error_errno(r
, "Could not set link flags: %m");
2644 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2651 static int link_up_down(int argc
, char *argv
[], void *userdata
) {
2652 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2653 _cleanup_set_free_ Set
*indexes
= NULL
;
2657 r
= sd_netlink_open(&rtnl
);
2659 return log_error_errno(r
, "Failed to connect to netlink: %m");
2661 indexes
= set_new(NULL
);
2665 for (int i
= 1; i
< argc
; i
++) {
2666 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2670 r
= set_put(indexes
, INT_TO_PTR(index
));
2675 SET_FOREACH(p
, indexes
) {
2676 index
= PTR_TO_INT(p
);
2677 r
= link_up_down_send_message(rtnl
, argv
[0], index
);
2679 return log_error_errno(r
, "Failed to bring %s interface %s: %m",
2680 argv
[0], FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2686 static int link_delete(int argc
, char *argv
[], void *userdata
) {
2687 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2688 _cleanup_set_free_ Set
*indexes
= NULL
;
2692 r
= sd_netlink_open(&rtnl
);
2694 return log_error_errno(r
, "Failed to connect to netlink: %m");
2696 indexes
= set_new(NULL
);
2700 for (int i
= 1; i
< argc
; i
++) {
2701 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2705 r
= set_put(indexes
, INT_TO_PTR(index
));
2710 SET_FOREACH(p
, indexes
) {
2711 index
= PTR_TO_INT(p
);
2712 r
= link_delete_send_message(rtnl
, index
);
2714 return log_error_errno(r
, "Failed to delete interface %s: %m",
2715 FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2721 static int link_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2722 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2725 r
= bus_call_method(bus
, bus_network_mgr
, "RenewLink", &error
, NULL
, "i", index
);
2727 return log_error_errno(r
, "Failed to renew dynamic configuration of interface %s: %s",
2728 name
, bus_error_message(&error
, r
));
2733 static int link_renew(int argc
, char *argv
[], void *userdata
) {
2734 sd_bus
*bus
= ASSERT_PTR(userdata
);
2735 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2736 int index
, k
= 0, r
;
2738 for (int i
= 1; i
< argc
; i
++) {
2739 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2743 r
= link_renew_one(bus
, index
, argv
[i
]);
2744 if (r
< 0 && k
>= 0)
2751 static int link_force_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2752 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2755 r
= bus_call_method(bus
, bus_network_mgr
, "ForceRenewLink", &error
, NULL
, "i", index
);
2757 return log_error_errno(r
, "Failed to force renew dynamic configuration of interface %s: %s",
2758 name
, bus_error_message(&error
, r
));
2763 static int link_force_renew(int argc
, char *argv
[], void *userdata
) {
2764 sd_bus
*bus
= ASSERT_PTR(userdata
);
2765 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2768 for (int i
= 1; i
< argc
; i
++) {
2769 int index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2773 r
= link_force_renew_one(bus
, index
, argv
[i
]);
2774 if (r
< 0 && k
>= 0)
2781 static int verb_reload(int argc
, char *argv
[], void *userdata
) {
2782 sd_bus
*bus
= ASSERT_PTR(userdata
);
2783 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2786 r
= bus_call_method(bus
, bus_network_mgr
, "Reload", &error
, NULL
, NULL
);
2788 return log_error_errno(r
, "Failed to reload network settings: %m");
2793 static int verb_reconfigure(int argc
, char *argv
[], void *userdata
) {
2794 sd_bus
*bus
= ASSERT_PTR(userdata
);
2795 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2796 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2797 _cleanup_set_free_ Set
*indexes
= NULL
;
2801 indexes
= set_new(NULL
);
2805 for (int i
= 1; i
< argc
; i
++) {
2806 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2810 r
= set_put(indexes
, INT_TO_PTR(index
));
2815 SET_FOREACH(p
, indexes
) {
2816 index
= PTR_TO_INT(p
);
2817 r
= bus_call_method(bus
, bus_network_mgr
, "ReconfigureLink", &error
, NULL
, "i", index
);
2819 return log_error_errno(r
, "Failed to reconfigure network interface %s: %m",
2820 FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2826 static int help(void) {
2827 _cleanup_free_
char *link
= NULL
;
2830 r
= terminal_urlify_man("networkctl", "1", &link
);
2834 printf("%s [OPTIONS...] COMMAND\n\n"
2835 "%sQuery and control the networking subsystem.%s\n"
2837 " list [PATTERN...] List links\n"
2838 " status [PATTERN...] Show link status\n"
2839 " lldp [PATTERN...] Show LLDP neighbors\n"
2840 " label Show current address label entries in the kernel\n"
2841 " delete DEVICES... Delete virtual netdevs\n"
2842 " up DEVICES... Bring devices up\n"
2843 " down DEVICES... Bring devices down\n"
2844 " renew DEVICES... Renew dynamic configurations\n"
2845 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
2846 " reconfigure DEVICES... Reconfigure interfaces\n"
2847 " reload Reload .network and .netdev files\n"
2849 " -h --help Show this help\n"
2850 " --version Show package version\n"
2851 " --no-pager Do not pipe output into a pager\n"
2852 " --no-legend Do not show the headers and footers\n"
2853 " -a --all Show status for all links\n"
2854 " -s --stats Show detailed link statistics\n"
2855 " -l --full Do not ellipsize output\n"
2856 " -n --lines=INTEGER Number of journal entries to show\n"
2857 " --json=pretty|short|off\n"
2858 " Generate JSON output\n"
2859 "\nSee the %s for details.\n",
2860 program_invocation_short_name
,
2868 static int parse_argv(int argc
, char *argv
[]) {
2871 ARG_VERSION
= 0x100,
2877 static const struct option options
[] = {
2878 { "help", no_argument
, NULL
, 'h' },
2879 { "version", no_argument
, NULL
, ARG_VERSION
},
2880 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2881 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2882 { "all", no_argument
, NULL
, 'a' },
2883 { "stats", no_argument
, NULL
, 's' },
2884 { "full", no_argument
, NULL
, 'l' },
2885 { "lines", required_argument
, NULL
, 'n' },
2886 { "json", required_argument
, NULL
, ARG_JSON
},
2895 while ((c
= getopt_long(argc
, argv
, "hasln:", options
, NULL
)) >= 0) {
2906 arg_pager_flags
|= PAGER_DISABLE
;
2926 if (safe_atou(optarg
, &arg_lines
) < 0)
2927 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2928 "Failed to parse lines '%s'", optarg
);
2932 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
2941 assert_not_reached();
2948 static int networkctl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
2949 static const Verb verbs
[] = {
2950 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_links
},
2951 { "status", VERB_ANY
, VERB_ANY
, 0, link_status
},
2952 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
2953 { "label", 1, 1, 0, list_address_labels
},
2954 { "delete", 2, VERB_ANY
, 0, link_delete
},
2955 { "up", 2, VERB_ANY
, 0, link_up_down
},
2956 { "down", 2, VERB_ANY
, 0, link_up_down
},
2957 { "renew", 2, VERB_ANY
, 0, link_renew
},
2958 { "forcerenew", 2, VERB_ANY
, 0, link_force_renew
},
2959 { "reconfigure", 2, VERB_ANY
, 0, verb_reconfigure
},
2960 { "reload", 1, 1, 0, verb_reload
},
2964 return dispatch_verb(argc
, argv
, verbs
, bus
);
2967 static int check_netns_match(sd_bus
*bus
) {
2968 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2973 r
= sd_bus_get_property_trivial(
2975 "org.freedesktop.network1",
2976 "/org/freedesktop/network1",
2977 "org.freedesktop.network1.Manager",
2983 log_debug_errno(r
, "Failed to query network namespace of networkd, ignoring: %s", bus_error_message(&error
, r
));
2987 log_debug("systemd-networkd.service not running in a network namespace (?), skipping netns check.");
2991 if (stat("/proc/self/ns/net", &st
) < 0)
2992 return log_error_errno(r
, "Failed to determine our own network namespace ID: %m");
2994 if (id
!= st
.st_ino
)
2995 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
2996 "networkctl must be invoked in same network namespace as systemd-networkd.service.");
3001 static void warn_networkd_missing(void) {
3003 if (access("/run/systemd/netif/state", F_OK
) >= 0)
3006 fprintf(stderr
, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
3009 static int run(int argc
, char* argv
[]) {
3010 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
3015 r
= parse_argv(argc
, argv
);
3019 r
= sd_bus_open_system(&bus
);
3021 return log_error_errno(r
, "Failed to connect system bus: %m");
3023 r
= check_netns_match(bus
);
3027 warn_networkd_missing();
3029 return networkctl_main(bus
, argc
, argv
);
3032 DEFINE_MAIN_FUNCTION(run
);