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"
26 #include "bus-common-errors.h"
27 #include "bus-error.h"
28 #include "bus-locator.h"
29 #include "device-util.h"
31 #include "ether-addr-util.h"
32 #include "ethtool-util.h"
34 #include "format-table.h"
35 #include "format-util.h"
37 #include "geneve-util.h"
38 #include "glob-util.h"
39 #include "hwdb-util.h"
40 #include "ipvlan-util.h"
41 #include "local-addresses.h"
42 #include "locale-util.h"
43 #include "logs-show.h"
45 #include "macvlan-util.h"
46 #include "main-func.h"
47 #include "netif-util.h"
48 #include "netlink-util.h"
49 #include "network-internal.h"
50 #include "network-util.h"
51 #include "networkctl.h"
52 #include "networkctl-config-file.h"
54 #include "parse-argument.h"
55 #include "parse-util.h"
56 #include "path-lookup.h"
57 #include "path-util.h"
58 #include "pretty-print.h"
61 #include "socket-netlink.h"
62 #include "socket-util.h"
63 #include "sort-util.h"
64 #include "sparse-endian.h"
65 #include "stdio-util.h"
66 #include "string-table.h"
67 #include "string-util.h"
70 #include "terminal-util.h"
71 #include "udev-util.h"
75 #include "wifi-util.h"
77 /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */
78 #define NETDEV_KIND_MAX 64
80 /* use 128 kB for receive socket kernel queue, we shouldn't need more here */
81 #define RCVBUF_SIZE (128*1024)
83 PagerFlags arg_pager_flags
= 0;
84 bool arg_legend
= true;
85 bool arg_no_reload
= false;
87 bool arg_stats
= false;
88 bool arg_full
= false;
89 bool arg_runtime
= false;
90 unsigned arg_lines
= 10;
91 char *arg_drop_in
= NULL
;
92 JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
94 STATIC_DESTRUCTOR_REGISTER(arg_drop_in
, freep
);
96 static int check_netns_match(void) {
99 JsonVariant
*reply
= NULL
;
100 _cleanup_(varlink_unrefp
) Varlink
*vl
= NULL
;
103 r
= varlink_connect_address(&vl
, "/run/systemd/netif/io.systemd.Network");
105 return log_error_errno(r
, "Failed to connect to network service /run/systemd/netif/io.systemd.Network: %m");
107 r
= varlink_call_and_log(vl
, "io.systemd.Network.GetNamespaceId", /* parameters= */ NULL
, &reply
);
111 static const JsonDispatch dispatch_table
[] = {
112 { "NamespaceId", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, 0, JSON_MANDATORY
},
116 r
= json_dispatch(reply
, dispatch_table
, JSON_LOG
|JSON_ALLOW_EXTENSIONS
, &id
);
121 log_debug("systemd-networkd.service not running in a network namespace (?), skipping netns check.");
125 if (stat("/proc/self/ns/net", &st
) < 0)
126 return log_error_errno(errno
, "Failed to determine our own network namespace ID: %m");
129 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
130 "networkctl must be invoked in same network namespace as systemd-networkd.service.");
135 bool networkd_is_running(void) {
136 static int cached
= -1;
140 r
= access("/run/systemd/netif/state", F_OK
);
143 log_debug_errno(errno
,
144 "Failed to determine whether networkd is running, assuming it's not: %m");
154 int acquire_bus(sd_bus
**ret
) {
155 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
160 r
= sd_bus_open_system(&bus
);
162 return log_error_errno(r
, "Failed to connect to system bus: %m");
164 if (networkd_is_running()) {
165 r
= check_netns_match();
169 log_warning("systemd-networkd is not running, output might be incomplete.");
171 *ret
= TAKE_PTR(bus
);
175 static int get_description(sd_bus
*bus
, JsonVariant
**ret
) {
176 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
177 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
184 r
= bus_call_method(bus
, bus_network_mgr
, "Describe", &error
, &reply
, NULL
);
186 return log_error_errno(r
, "Failed to get description: %s", bus_error_message(&error
, r
));
188 r
= sd_bus_message_read(reply
, "s", &text
);
190 return bus_log_parse_error(r
);
192 r
= json_parse(text
, 0, ret
, NULL
, NULL
);
194 return log_error_errno(r
, "Failed to parse JSON: %m");
199 static int dump_manager_description(sd_bus
*bus
) {
200 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
205 r
= get_description(bus
, &v
);
209 json_variant_dump(v
, arg_json_format_flags
, NULL
, NULL
);
213 static int dump_link_description(sd_bus
*bus
, char * const *patterns
) {
214 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
215 _cleanup_free_
bool *matched_patterns
= NULL
;
223 r
= get_description(bus
, &v
);
227 matched_patterns
= new0(bool, strv_length(patterns
));
228 if (!matched_patterns
)
231 JSON_VARIANT_ARRAY_FOREACH(i
, json_variant_by_key(v
, "Interfaces")) {
232 char ifindex_str
[DECIMAL_STR_MAX(int64_t)];
237 name
= json_variant_string(json_variant_by_key(i
, "Name"));
238 index
= json_variant_integer(json_variant_by_key(i
, "Index"));
239 xsprintf(ifindex_str
, "%" PRIi64
, index
);
241 if (!strv_fnmatch_full(patterns
, ifindex_str
, 0, &pos
) &&
242 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
246 JSON_VARIANT_ARRAY_FOREACH(a
, json_variant_by_key(i
, "AlternativeNames"))
247 if (strv_fnmatch_full(patterns
, json_variant_string(a
), 0, &pos
)) {
256 matched_patterns
[pos
] = true;
257 json_variant_dump(i
, arg_json_format_flags
, NULL
, NULL
);
261 /* Look if we matched all our arguments that are not globs. It is OK for a glob to match
262 * nothing, but not for an exact argument. */
263 for (size_t pos
= 0; pos
< strv_length(patterns
); pos
++) {
264 if (matched_patterns
[pos
])
267 if (string_is_glob(patterns
[pos
]))
268 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
271 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
272 "Interface \"%s\" not found.", patterns
[pos
]);
276 log_warning("No interfaces matched.");
281 static void operational_state_to_color(
287 if (STRPTR_IN_SET(state
, "routable", "enslaved") ||
288 (streq_ptr(name
, "lo") && streq_ptr(state
, "carrier"))) {
290 *on
= ansi_highlight_green();
292 *off
= ansi_normal();
293 } else if (streq_ptr(state
, "degraded")) {
295 *on
= ansi_highlight_yellow();
297 *off
= ansi_normal();
306 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
307 if (streq_ptr(state
, "configured")) {
309 *on
= ansi_highlight_green();
311 *off
= ansi_normal();
312 } else if (streq_ptr(state
, "configuring")) {
314 *on
= ansi_highlight_yellow();
316 *off
= ansi_normal();
317 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
319 *on
= ansi_highlight_red();
321 *off
= ansi_normal();
330 static void online_state_to_color(const char *state
, const char **on
, const char **off
) {
331 if (streq_ptr(state
, "online")) {
333 *on
= ansi_highlight_green();
335 *off
= ansi_normal();
336 } else if (streq_ptr(state
, "partial")) {
338 *on
= ansi_highlight_yellow();
340 *off
= ansi_normal();
349 typedef struct VxLanInfo
{
356 union in_addr_union local
;
357 union in_addr_union group
;
370 typedef struct LinkInfo
{
371 char name
[IFNAMSIZ
+1];
373 sd_device
*sd_device
;
375 unsigned short iftype
;
376 struct hw_addr_data hw_address
;
377 struct hw_addr_data permanent_hw_address
;
384 uint8_t addr_gen_mode
;
386 char **alternative_names
;
389 struct rtnl_link_stats64 stats64
;
390 struct rtnl_link_stats stats
;
397 uint32_t forward_delay
;
400 uint32_t ageing_time
;
404 uint8_t mcast_igmp_version
;
408 VxLanInfo vxlan_info
;
421 uint16_t tunnel_port
;
424 union in_addr_union local
;
425 union in_addr_union remote
;
433 /* macvlan and macvtap info */
434 uint32_t macvlan_mode
;
437 uint16_t ipvlan_mode
;
438 uint16_t ipvlan_flags
;
447 enum nl80211_iftype wlan_iftype
;
449 struct ether_addr bssid
;
451 bool has_hw_address
:1;
452 bool has_permanent_hw_address
:1;
453 bool has_tx_queues
:1;
454 bool has_rx_queues
:1;
458 bool has_ethtool_link_info
:1;
459 bool has_wlan_link_info
:1;
460 bool has_tunnel_ipv4
:1;
461 bool has_ipv6_address_generation_mode
:1;
463 bool needs_freeing
:1;
466 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
467 return CMP(a
->ifindex
, b
->ifindex
);
470 static LinkInfo
* link_info_array_free(LinkInfo
*array
) {
471 for (unsigned i
= 0; array
&& array
[i
].needs_freeing
; i
++) {
472 sd_device_unref(array
[i
].sd_device
);
473 free(array
[i
].netdev_kind
);
475 free(array
[i
].qdisc
);
476 strv_free(array
[i
].alternative_names
);
481 DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo
*, link_info_array_free
);
483 static int decode_netdev(sd_netlink_message
*m
, LinkInfo
*info
) {
489 r
= sd_netlink_message_enter_container(m
, IFLA_LINKINFO
);
493 r
= sd_netlink_message_read_string_strdup(m
, IFLA_INFO_KIND
, &info
->netdev_kind
);
495 (void) sd_netlink_message_exit_container(m
);
499 r
= sd_netlink_message_enter_container(m
, IFLA_INFO_DATA
);
503 if (streq(info
->netdev_kind
, "bridge")) {
504 (void) sd_netlink_message_read_u32(m
, IFLA_BR_FORWARD_DELAY
, &info
->forward_delay
);
505 (void) sd_netlink_message_read_u32(m
, IFLA_BR_HELLO_TIME
, &info
->hello_time
);
506 (void) sd_netlink_message_read_u32(m
, IFLA_BR_MAX_AGE
, &info
->max_age
);
507 (void) sd_netlink_message_read_u32(m
, IFLA_BR_AGEING_TIME
, &info
->ageing_time
);
508 (void) sd_netlink_message_read_u32(m
, IFLA_BR_STP_STATE
, &info
->stp_state
);
509 (void) sd_netlink_message_read_u32(m
, IFLA_BRPORT_COST
, &info
->cost
);
510 (void) sd_netlink_message_read_u16(m
, IFLA_BR_PRIORITY
, &info
->priority
);
511 (void) sd_netlink_message_read_u8(m
, IFLA_BR_MCAST_IGMP_VERSION
, &info
->mcast_igmp_version
);
512 (void) sd_netlink_message_read_u8(m
, IFLA_BRPORT_STATE
, &info
->port_state
);
513 } if (streq(info
->netdev_kind
, "bond")) {
514 (void) sd_netlink_message_read_u8(m
, IFLA_BOND_MODE
, &info
->mode
);
515 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_MIIMON
, &info
->miimon
);
516 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_DOWNDELAY
, &info
->downdelay
);
517 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_UPDELAY
, &info
->updelay
);
518 } else if (streq(info
->netdev_kind
, "vxlan")) {
519 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_ID
, &info
->vxlan_info
.vni
);
521 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_GROUP
, &info
->vxlan_info
.group
.in
);
523 info
->vxlan_info
.group_family
= AF_INET
;
525 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_GROUP6
, &info
->vxlan_info
.group
.in6
);
527 info
->vxlan_info
.group_family
= AF_INET6
;
530 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_LOCAL
, &info
->vxlan_info
.local
.in
);
532 info
->vxlan_info
.local_family
= AF_INET
;
534 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_LOCAL6
, &info
->vxlan_info
.local
.in6
);
536 info
->vxlan_info
.local_family
= AF_INET6
;
539 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_LINK
, &info
->vxlan_info
.link
);
540 (void) sd_netlink_message_read_u16(m
, IFLA_VXLAN_PORT
, &info
->vxlan_info
.dest_port
);
541 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_PROXY
, &info
->vxlan_info
.proxy
);
542 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_LEARNING
, &info
->vxlan_info
.learning
);
543 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_RSC
, &info
->vxlan_info
.rsc
);
544 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L3MISS
, &info
->vxlan_info
.l3miss
);
545 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L2MISS
, &info
->vxlan_info
.l2miss
);
546 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TOS
, &info
->vxlan_info
.tos
);
547 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TTL
, &info
->vxlan_info
.ttl
);
548 } else if (streq(info
->netdev_kind
, "vlan"))
549 (void) sd_netlink_message_read_u16(m
, IFLA_VLAN_ID
, &info
->vlan_id
);
550 else if (STR_IN_SET(info
->netdev_kind
, "ipip", "sit")) {
551 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_LOCAL
, &info
->local
.in
);
552 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_REMOTE
, &info
->remote
.in
);
553 } else if (streq(info
->netdev_kind
, "geneve")) {
554 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_ID
, &info
->vni
);
556 r
= sd_netlink_message_read_in_addr(m
, IFLA_GENEVE_REMOTE
, &info
->remote
.in
);
558 info
->has_tunnel_ipv4
= true;
560 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GENEVE_REMOTE6
, &info
->remote
.in6
);
562 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL
, &info
->ttl
);
563 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL_INHERIT
, &info
->inherit
);
564 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TOS
, &info
->tos
);
565 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_DF
, &info
->df
);
566 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_CSUM
, &info
->csum
);
567 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
, &info
->csum6_tx
);
568 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
, &info
->csum6_rx
);
569 (void) sd_netlink_message_read_u16(m
, IFLA_GENEVE_PORT
, &info
->tunnel_port
);
570 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_LABEL
, &info
->label
);
571 } else if (STR_IN_SET(info
->netdev_kind
, "gre", "gretap", "erspan")) {
572 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in
);
573 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in
);
574 } else if (STR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan")) {
575 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in6
);
576 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in6
);
577 } else if (streq(info
->netdev_kind
, "vti")) {
578 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in
);
579 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in
);
580 } else if (streq(info
->netdev_kind
, "vti6")) {
581 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in6
);
582 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in6
);
583 } else if (STR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap"))
584 (void) sd_netlink_message_read_u32(m
, IFLA_MACVLAN_MODE
, &info
->macvlan_mode
);
585 else if (streq(info
->netdev_kind
, "ipvlan")) {
586 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_MODE
, &info
->ipvlan_mode
);
587 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_FLAGS
, &info
->ipvlan_flags
);
590 (void) sd_netlink_message_exit_container(m
);
591 (void) sd_netlink_message_exit_container(m
);
596 static int decode_link(
597 sd_netlink_message
*m
,
599 char * const *patterns
,
600 bool matched_patterns
[]) {
602 _cleanup_strv_free_
char **altnames
= NULL
;
603 const char *name
, *qdisc
;
610 r
= sd_netlink_message_get_type(m
, &type
);
614 if (type
!= RTM_NEWLINK
)
617 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
621 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
625 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &altnames
);
626 if (r
< 0 && r
!= -ENODATA
)
630 char str
[DECIMAL_STR_MAX(int)];
633 assert(matched_patterns
);
635 xsprintf(str
, "%i", ifindex
);
636 if (!strv_fnmatch_full(patterns
, str
, 0, &pos
) &&
637 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
640 STRV_FOREACH(p
, altnames
)
641 if (strv_fnmatch_full(patterns
, *p
, 0, &pos
)) {
649 matched_patterns
[pos
] = true;
652 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
656 strscpy(info
->name
, sizeof info
->name
, name
);
657 info
->ifindex
= ifindex
;
658 info
->alternative_names
= TAKE_PTR(altnames
);
660 info
->has_hw_address
=
661 netlink_message_read_hw_addr(m
, IFLA_ADDRESS
, &info
->hw_address
) >= 0 &&
662 info
->hw_address
.length
> 0;
664 info
->has_permanent_hw_address
=
665 (netlink_message_read_hw_addr(m
, IFLA_PERM_ADDRESS
, &info
->permanent_hw_address
) >= 0 ||
666 ethtool_get_permanent_hw_addr(NULL
, info
->name
, &info
->permanent_hw_address
) >= 0) &&
667 !hw_addr_is_null(&info
->permanent_hw_address
) &&
668 !hw_addr_equal(&info
->permanent_hw_address
, &info
->hw_address
);
670 (void) sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
);
671 (void) sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
);
672 (void) sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
);
674 info
->has_rx_queues
=
675 sd_netlink_message_read_u32(m
, IFLA_NUM_RX_QUEUES
, &info
->rx_queues
) >= 0 &&
678 info
->has_tx_queues
=
679 sd_netlink_message_read_u32(m
, IFLA_NUM_TX_QUEUES
, &info
->tx_queues
) >= 0 &&
682 if (sd_netlink_message_read(m
, IFLA_STATS64
, sizeof info
->stats64
, &info
->stats64
) >= 0)
683 info
->has_stats64
= true;
684 else if (sd_netlink_message_read(m
, IFLA_STATS
, sizeof info
->stats
, &info
->stats
) >= 0)
685 info
->has_stats
= true;
687 r
= sd_netlink_message_read_string(m
, IFLA_QDISC
, &qdisc
);
689 info
->qdisc
= strdup(qdisc
);
694 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, &info
->master
);
696 r
= sd_netlink_message_enter_container(m
, IFLA_AF_SPEC
);
698 r
= sd_netlink_message_enter_container(m
, AF_INET6
);
700 r
= sd_netlink_message_read_u8(m
, IFLA_INET6_ADDR_GEN_MODE
, &info
->addr_gen_mode
);
701 if (r
>= 0 && IN_SET(info
->addr_gen_mode
,
702 IN6_ADDR_GEN_MODE_EUI64
,
703 IN6_ADDR_GEN_MODE_NONE
,
704 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
,
705 IN6_ADDR_GEN_MODE_RANDOM
))
706 info
->has_ipv6_address_generation_mode
= true;
708 (void) sd_netlink_message_exit_container(m
);
710 (void) sd_netlink_message_exit_container(m
);
714 (void) decode_netdev(m
, info
);
719 static int link_get_property(
721 const LinkInfo
*link
,
723 sd_bus_message
**reply
,
725 const char *propname
,
728 _cleanup_free_
char *path
= NULL
;
729 char ifindex_str
[DECIMAL_STR_MAX(int)];
734 assert(link
->ifindex
>= 0);
741 xsprintf(ifindex_str
, "%i", link
->ifindex
);
743 r
= sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str
, &path
);
747 return sd_bus_get_property(bus
, "org.freedesktop.network1", path
, iface
, propname
, error
, reply
, type
);
750 static int acquire_link_bitrates(sd_bus
*bus
, LinkInfo
*link
) {
751 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
752 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
758 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.Link", "BitRates", "(tt)");
760 bool quiet
= sd_bus_error_has_names(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
,
761 BUS_ERROR_SPEED_METER_INACTIVE
);
763 return log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
764 r
, "Failed to query link bit rates: %s", bus_error_message(&error
, r
));
767 r
= sd_bus_message_read(reply
, "(tt)", &link
->tx_bitrate
, &link
->rx_bitrate
);
769 return bus_log_parse_error(r
);
771 r
= sd_bus_message_exit_container(reply
);
773 return bus_log_parse_error(r
);
775 link
->has_bitrates
= link
->tx_bitrate
!= UINT64_MAX
&& link
->rx_bitrate
!= UINT64_MAX
;
780 static void acquire_ether_link_info(int *fd
, LinkInfo
*link
) {
784 if (ethtool_get_link_info(fd
,
786 &link
->autonegotiation
,
790 link
->has_ethtool_link_info
= true;
793 static void acquire_wlan_link_info(LinkInfo
*link
) {
794 _cleanup_(sd_netlink_unrefp
) sd_netlink
*genl
= NULL
;
799 if (!link
->sd_device
)
802 if (!device_is_devtype(link
->sd_device
, "wlan"))
805 r
= sd_genl_socket_open(&genl
);
807 log_debug_errno(r
, "Failed to open generic netlink socket: %m");
811 (void) sd_netlink_increase_rxbuf(genl
, RCVBUF_SIZE
);
813 r
= wifi_get_interface(genl
, link
->ifindex
, &link
->wlan_iftype
, &link
->ssid
);
815 log_debug_errno(r
, "%s: failed to query ssid: %m", link
->name
);
817 if (link
->wlan_iftype
== NL80211_IFTYPE_STATION
) {
818 k
= wifi_get_station(genl
, link
->ifindex
, &link
->bssid
);
820 log_debug_errno(k
, "%s: failed to query bssid: %m", link
->name
);
823 link
->has_wlan_link_info
= r
> 0 || k
> 0;
826 static int acquire_link_info(sd_bus
*bus
, sd_netlink
*rtnl
, char * const *patterns
, LinkInfo
**ret
) {
827 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
828 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
829 _cleanup_free_
bool *matched_patterns
= NULL
;
830 _cleanup_close_
int fd
= -EBADF
;
837 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
839 return rtnl_log_create_error(r
);
841 r
= sd_netlink_message_set_request_dump(req
, true);
843 return rtnl_log_create_error(r
);
845 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
847 return log_error_errno(r
, "Failed to enumerate links: %m");
850 matched_patterns
= new0(bool, strv_length(patterns
));
851 if (!matched_patterns
)
855 for (sd_netlink_message
*i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
856 if (!GREEDY_REALLOC0(links
, c
+ 2)) /* We keep one trailing one as marker */
859 r
= decode_link(i
, links
+ c
, patterns
, matched_patterns
);
865 links
[c
].needs_freeing
= true;
867 (void) sd_device_new_from_ifindex(&links
[c
].sd_device
, links
[c
].ifindex
);
869 acquire_ether_link_info(&fd
, &links
[c
]);
870 acquire_wlan_link_info(&links
[c
]);
875 /* Look if we matched all our arguments that are not globs. It
876 * is OK for a glob to match nothing, but not for an exact argument. */
877 for (size_t pos
= 0; pos
< strv_length(patterns
); pos
++) {
878 if (matched_patterns
[pos
])
881 if (string_is_glob(patterns
[pos
]))
882 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
885 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
886 "Interface \"%s\" not found.", patterns
[pos
]);
889 typesafe_qsort(links
, c
, link_info_compare
);
892 FOREACH_ARRAY(link
, links
, c
)
893 (void) acquire_link_bitrates(bus
, link
);
895 *ret
= TAKE_PTR(links
);
897 if (patterns
&& c
== 0)
898 log_warning("No interfaces matched.");
903 static int list_links(int argc
, char *argv
[], void *userdata
) {
904 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
905 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
906 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
907 _cleanup_(table_unrefp
) Table
*table
= NULL
;
911 r
= acquire_bus(&bus
);
915 if (arg_json_format_flags
!= JSON_FORMAT_OFF
) {
916 if (arg_all
|| argc
<= 1)
917 return dump_manager_description(bus
);
919 return dump_link_description(bus
, strv_skip(argv
, 1));
922 r
= sd_netlink_open(&rtnl
);
924 return log_error_errno(r
, "Failed to connect to netlink: %m");
926 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
930 pager_open(arg_pager_flags
);
932 table
= table_new("idx", "link", "type", "operational", "setup");
937 table_set_width(table
, 0);
939 table_set_header(table
, arg_legend
);
940 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
942 assert_se(cell
= table_get_cell(table
, 0, 0));
943 (void) table_set_minimum_width(table
, cell
, 3);
944 (void) table_set_weight(table
, cell
, 0);
945 (void) table_set_ellipsize_percent(table
, cell
, 100);
946 (void) table_set_align_percent(table
, cell
, 100);
948 assert_se(cell
= table_get_cell(table
, 0, 1));
949 (void) table_set_ellipsize_percent(table
, cell
, 100);
951 FOREACH_ARRAY(link
, links
, c
) {
952 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
953 _cleanup_free_
char *t
= NULL
;
954 const char *on_color_operational
, *on_color_setup
;
956 (void) sd_network_link_get_operational_state(link
->ifindex
, &operational_state
);
957 operational_state_to_color(link
->name
, operational_state
, &on_color_operational
, NULL
);
959 (void) sd_network_link_get_setup_state(link
->ifindex
, &setup_state
);
960 setup_state_to_color(setup_state
, &on_color_setup
, NULL
);
962 r
= net_get_type_string(link
->sd_device
, link
->iftype
, &t
);
966 r
= table_add_many(table
,
967 TABLE_INT
, link
->ifindex
,
968 TABLE_STRING
, link
->name
,
970 TABLE_STRING
, operational_state
,
971 TABLE_SET_COLOR
, on_color_operational
,
972 TABLE_STRING
, setup_state
?: "unmanaged",
973 TABLE_SET_COLOR
, on_color_setup
);
975 return table_log_add_error(r
);
978 r
= table_print(table
, NULL
);
980 return table_log_print_error(r
);
983 printf("\n%i links listed.\n", c
);
988 /* IEEE Organizationally Unique Identifier vendor string */
989 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
990 _cleanup_free_
char *desc
= NULL
;
991 const char *description
;
992 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1];
1000 /* skip commonly misused 00:00:00 (Xerox) prefix */
1001 if (memcmp(mac
, "\0\0\0", 3) == 0)
1004 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
, ETHER_ADDR_FORMAT_VAL(*mac
));
1006 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
1010 desc
= strdup(description
);
1014 *ret
= TAKE_PTR(desc
);
1019 static int get_gateway_description(
1024 union in_addr_union
*gateway
,
1027 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1031 assert(ifindex
>= 0);
1032 assert(IN_SET(family
, AF_INET
, AF_INET6
));
1036 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
1040 r
= sd_netlink_message_set_request_dump(req
, true);
1044 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1048 for (sd_netlink_message
*m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
1049 union in_addr_union gw
= IN_ADDR_NULL
;
1050 struct ether_addr mac
= ETHER_ADDR_NULL
;
1054 r
= sd_netlink_message_get_errno(m
);
1056 log_error_errno(r
, "Failed to get netlink message, ignoring: %m");
1060 r
= sd_netlink_message_get_type(m
, &type
);
1062 log_error_errno(r
, "Failed to get netlink message type, ignoring: %m");
1066 if (type
!= RTM_NEWNEIGH
) {
1067 log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1068 "Got unexpected netlink message type %u, ignoring",
1073 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
1075 log_error_errno(r
, "Failed to get rtnl family, ignoring: %m");
1079 if (fam
!= family
) {
1080 log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Got invalid rtnl family %d, ignoring", fam
);
1084 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
1086 log_error_errno(r
, "Failed to get rtnl ifindex, ignoring: %m");
1090 if (ifindex
> 0 && ifi
!= ifindex
)
1096 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
1103 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
1113 if (!in_addr_equal(fam
, &gw
, gateway
))
1116 r
= sd_netlink_message_read(m
, NDA_LLADDR
, sizeof(mac
), &mac
);
1120 r
= ieee_oui(hwdb
, &mac
, ret
);
1130 static int dump_list(Table
*table
, const char *key
, char * const *l
) {
1136 if (strv_isempty(l
))
1139 r
= table_add_many(table
,
1143 return table_log_add_error(r
);
1148 static int dump_gateways(sd_netlink
*rtnl
, sd_hwdb
*hwdb
, Table
*table
, int ifindex
) {
1149 _cleanup_free_
struct local_address
*local_addrs
= NULL
;
1150 _cleanup_strv_free_
char **buf
= NULL
;
1156 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local_addrs
);
1160 FOREACH_ARRAY(local
, local_addrs
, n
) {
1161 _cleanup_free_
char *description
= NULL
;
1163 r
= get_gateway_description(rtnl
, hwdb
, local
->ifindex
, local
->family
, &local
->address
, &description
);
1165 log_debug_errno(r
, "Could not get description of gateway, ignoring: %m");
1167 /* Show interface name for the entry if we show entries for all interfaces */
1168 r
= strv_extendf(&buf
, "%s%s%s%s%s%s",
1169 IN_ADDR_TO_STRING(local
->family
, &local
->address
),
1170 description
? " (" : "",
1171 strempty(description
),
1172 description
? ")" : "",
1173 ifindex
<= 0 ? " on " : "",
1174 ifindex
<= 0 ? FORMAT_IFNAME_FULL(local
->ifindex
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
) : "");
1179 return dump_list(table
, "Gateway", buf
);
1182 static int dump_addresses(
1184 sd_dhcp_lease
*lease
,
1188 _cleanup_free_
struct local_address
*local_addrs
= NULL
;
1189 _cleanup_strv_free_
char **buf
= NULL
;
1190 struct in_addr dhcp4_address
= {};
1196 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local_addrs
);
1201 (void) sd_dhcp_lease_get_address(lease
, &dhcp4_address
);
1203 FOREACH_ARRAY(local
, local_addrs
, n
) {
1204 struct in_addr server_address
;
1207 if (local
->family
== AF_INET
&& in4_addr_equal(&local
->address
.in
, &dhcp4_address
))
1208 dhcp4
= sd_dhcp_lease_get_server_identifier(lease
, &server_address
) >= 0;
1210 r
= strv_extendf(&buf
, "%s%s%s%s%s%s",
1211 IN_ADDR_TO_STRING(local
->family
, &local
->address
),
1212 dhcp4
? " (DHCP4 via " : "",
1213 dhcp4
? IN4_ADDR_TO_STRING(&server_address
) : "",
1215 ifindex
<= 0 ? " on " : "",
1216 ifindex
<= 0 ? FORMAT_IFNAME_FULL(local
->ifindex
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
) : "");
1221 return dump_list(table
, "Address", buf
);
1224 static int dump_address_labels(sd_netlink
*rtnl
) {
1225 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1226 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1232 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
1234 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
1236 r
= sd_netlink_message_set_request_dump(req
, true);
1240 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1244 table
= table_new("label", "prefix/prefixlen");
1249 table_set_width(table
, 0);
1251 r
= table_set_sort(table
, (size_t) 0);
1255 assert_se(cell
= table_get_cell(table
, 0, 0));
1256 (void) table_set_align_percent(table
, cell
, 100);
1257 (void) table_set_ellipsize_percent(table
, cell
, 100);
1259 assert_se(cell
= table_get_cell(table
, 0, 1));
1260 (void) table_set_align_percent(table
, cell
, 100);
1262 for (sd_netlink_message
*m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
1263 struct in6_addr prefix
;
1267 r
= sd_netlink_message_get_errno(m
);
1269 log_error_errno(r
, "Failed to get netlink message, ignoring: %m");
1273 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
1274 if (r
< 0 && r
!= -ENODATA
) {
1275 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
1279 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
);
1283 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
1287 r
= table_add_cell(table
, NULL
, TABLE_UINT32
, &label
);
1289 return table_log_add_error(r
);
1291 r
= table_add_cell_stringf(table
, NULL
, "%s/%u", IN6_ADDR_TO_STRING(&prefix
), prefixlen
);
1293 return table_log_add_error(r
);
1296 r
= table_print(table
, NULL
);
1298 return table_log_print_error(r
);
1303 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
1304 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1307 r
= sd_netlink_open(&rtnl
);
1309 return log_error_errno(r
, "Failed to connect to netlink: %m");
1311 return dump_address_labels(rtnl
);
1314 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
1315 _cleanup_fclose_
FILE *f
= NULL
;
1316 char p
[STRLEN("/run/systemd/netif/lldp/") + DECIMAL_STR_MAX(int)];
1318 assert(ifindex
>= 0);
1321 xsprintf(p
, "/run/systemd/netif/lldp/%i", ifindex
);
1331 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
1332 _cleanup_free_
void *raw
= NULL
;
1340 l
= fread(&u
, 1, sizeof(u
), f
);
1341 if (l
== 0 && feof(f
))
1346 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
1347 if (le64toh(u
) >= 4096)
1350 raw
= new(uint8_t, le64toh(u
));
1354 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
1357 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
1364 static int dump_lldp_neighbors(Table
*table
, const char *prefix
, int ifindex
) {
1365 _cleanup_strv_free_
char **buf
= NULL
;
1366 _cleanup_fclose_
FILE *f
= NULL
;
1371 assert(ifindex
> 0);
1373 r
= open_lldp_neighbors(ifindex
, &f
);
1380 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
1381 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1383 r
= next_lldp_neighbor(f
, &n
);
1389 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1390 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1391 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1393 r
= strv_extendf(&buf
, "%s on port %s%s%s%s",
1396 isempty(port_description
) ? "" : " (",
1397 strempty(port_description
),
1398 isempty(port_description
) ? "" : ")");
1403 return dump_list(table
, prefix
, buf
);
1406 static int dump_dhcp_leases(Table
*table
, const char *prefix
, sd_bus
*bus
, const LinkInfo
*link
) {
1407 _cleanup_strv_free_
char **buf
= NULL
;
1408 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1409 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1417 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.DHCPServer", "Leases", "a(uayayayayt)");
1419 bool quiet
= sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
);
1421 log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
1422 r
, "Failed to query link DHCP leases: %s", bus_error_message(&error
, r
));
1426 r
= sd_bus_message_enter_container(reply
, 'a', "(uayayayayt)");
1428 return bus_log_parse_error(r
);
1430 while ((r
= sd_bus_message_enter_container(reply
, 'r', "uayayayayt")) > 0) {
1431 _cleanup_free_
char *id
= NULL
, *ip
= NULL
;
1432 const void *client_id
, *addr
, *gtw
, *hwaddr
;
1433 size_t client_id_sz
, sz
;
1434 uint64_t expiration
;
1437 r
= sd_bus_message_read(reply
, "u", &family
);
1439 return bus_log_parse_error(r
);
1441 r
= sd_bus_message_read_array(reply
, 'y', &client_id
, &client_id_sz
);
1443 return bus_log_parse_error(r
);
1445 r
= sd_bus_message_read_array(reply
, 'y', &addr
, &sz
);
1446 if (r
< 0 || sz
!= 4)
1447 return bus_log_parse_error(r
);
1449 r
= sd_bus_message_read_array(reply
, 'y', >w
, &sz
);
1450 if (r
< 0 || sz
!= 4)
1451 return bus_log_parse_error(r
);
1453 r
= sd_bus_message_read_array(reply
, 'y', &hwaddr
, &sz
);
1455 return bus_log_parse_error(r
);
1457 r
= sd_bus_message_read_basic(reply
, 't', &expiration
);
1459 return bus_log_parse_error(r
);
1461 r
= sd_dhcp_client_id_to_string_from_raw(client_id
, client_id_sz
, &id
);
1463 return bus_log_parse_error(r
);
1465 r
= in_addr_to_string(family
, addr
, &ip
);
1467 return bus_log_parse_error(r
);
1469 r
= strv_extendf(&buf
, "%s (to %s)", ip
, id
);
1473 r
= sd_bus_message_exit_container(reply
);
1475 return bus_log_parse_error(r
);
1478 return bus_log_parse_error(r
);
1480 r
= sd_bus_message_exit_container(reply
);
1482 return bus_log_parse_error(r
);
1484 r
= sd_bus_message_exit_container(reply
);
1486 return bus_log_parse_error(r
);
1488 if (strv_isempty(buf
)) {
1489 r
= strv_extendf(&buf
, "none");
1494 return dump_list(table
, prefix
, buf
);
1497 static int dump_ifindexes(Table
*table
, const char *prefix
, const int *ifindexes
) {
1506 for (unsigned c
= 0; ifindexes
[c
] > 0; c
++) {
1508 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, prefix
);
1510 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1512 return table_log_add_error(r
);
1514 r
= table_add_cell(table
, NULL
, TABLE_IFINDEX
, &ifindexes
[c
]);
1516 return table_log_add_error(r
);
1522 #define DUMP_STATS_ONE(name, val_name) \
1524 r = table_add_cell(table, NULL, TABLE_FIELD, name); \
1526 return table_log_add_error(r); \
1527 r = table_add_cell(table, NULL, \
1528 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
1529 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
1531 return table_log_add_error(r); \
1534 static int dump_statistics(Table
*table
, const LinkInfo
*info
) {
1543 if (!info
->has_stats64
&& !info
->has_stats
)
1546 DUMP_STATS_ONE("Rx Packets", rx_packets
);
1547 DUMP_STATS_ONE("Tx Packets", tx_packets
);
1548 DUMP_STATS_ONE("Rx Bytes", rx_bytes
);
1549 DUMP_STATS_ONE("Tx Bytes", tx_bytes
);
1550 DUMP_STATS_ONE("Rx Errors", rx_errors
);
1551 DUMP_STATS_ONE("Tx Errors", tx_errors
);
1552 DUMP_STATS_ONE("Rx Dropped", rx_dropped
);
1553 DUMP_STATS_ONE("Tx Dropped", tx_dropped
);
1554 DUMP_STATS_ONE("Multicast Packets", multicast
);
1555 DUMP_STATS_ONE("Collisions", collisions
);
1560 static int dump_hw_address(Table
*table
, sd_hwdb
*hwdb
, const char *field
, const struct hw_addr_data
*addr
) {
1561 _cleanup_free_
char *description
= NULL
;
1568 if (addr
->length
== ETH_ALEN
)
1569 (void) ieee_oui(hwdb
, &addr
->ether
, &description
);
1571 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, field
);
1573 return table_log_add_error(r
);
1575 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s%s",
1576 HW_ADDR_TO_STR(addr
),
1577 description
? " (" : "",
1578 strempty(description
),
1579 description
? ")" : "");
1581 return table_log_add_error(r
);
1586 static OutputFlags
get_output_flags(void) {
1588 arg_all
* OUTPUT_SHOW_ALL
|
1589 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
1590 colors_enabled() * OUTPUT_COLOR
;
1593 static int show_logs(const LinkInfo
*info
) {
1594 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1600 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
| SD_JOURNAL_ASSUME_IMMUTABLE
);
1602 return log_error_errno(r
, "Failed to open journal: %m");
1604 r
= add_match_this_boot(j
, NULL
);
1606 return log_error_errno(r
, "Failed to add boot matches: %m");
1609 char m1
[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)];
1610 const char *m2
, *m3
;
1613 xsprintf(m1
, "_KERNEL_DEVICE=n%i", info
->ifindex
);
1615 m2
= strjoina("INTERFACE=", info
->name
);
1617 m3
= strjoina("DEVICE=", info
->name
);
1620 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1621 (r
= sd_journal_add_disjunction(j
)) ||
1622 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1623 (r
= sd_journal_add_disjunction(j
)) ||
1624 (r
= sd_journal_add_match(j
, m3
, 0))
1627 return log_error_errno(r
, "Failed to add link matches: %m");
1629 r
= add_matches_for_unit(j
, "systemd-networkd.service");
1631 return log_error_errno(r
, "Failed to add unit matches: %m");
1633 r
= add_matches_for_unit(j
, "systemd-networkd-wait-online.service");
1635 return log_error_errno(r
, "Failed to add unit matches: %m");
1638 return show_journal(
1645 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
1649 static int table_add_string_line(Table
*table
, const char *key
, const char *value
) {
1658 r
= table_add_many(table
,
1660 TABLE_STRING
, value
);
1662 return table_log_add_error(r
);
1667 static int format_dropins(char **dropins
) {
1668 STRV_FOREACH(d
, dropins
) {
1669 _cleanup_free_
char *s
= NULL
;
1670 int glyph
= *(d
+ 1) == NULL
? SPECIAL_GLYPH_TREE_RIGHT
: SPECIAL_GLYPH_TREE_BRANCH
;
1672 s
= strjoin(special_glyph(glyph
), *d
);
1676 free_and_replace(*d
, s
);
1682 static int link_status_one(
1686 const LinkInfo
*info
) {
1688 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **sip
= NULL
, **search_domains
= NULL
,
1689 **route_domains
= NULL
, **link_dropins
= NULL
, **network_dropins
= NULL
;
1690 _cleanup_free_
char *t
= NULL
, *network
= NULL
, *iaid
= NULL
, *duid
= NULL
, *captive_portal
= NULL
,
1691 *setup_state
= NULL
, *operational_state
= NULL
, *online_state
= NULL
, *activation_policy
= NULL
;
1692 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
,
1693 *on_color_operational
, *off_color_operational
, *on_color_setup
, *off_color_setup
, *on_color_online
;
1694 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
1695 _cleanup_(sd_dhcp_lease_unrefp
) sd_dhcp_lease
*lease
= NULL
;
1696 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1703 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
1704 operational_state_to_color(info
->name
, operational_state
, &on_color_operational
, &off_color_operational
);
1706 (void) sd_network_link_get_online_state(info
->ifindex
, &online_state
);
1707 online_state_to_color(online_state
, &on_color_online
, NULL
);
1709 (void) sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
1710 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
1712 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
1713 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
1714 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
1715 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
1716 (void) sd_network_link_get_sip(info
->ifindex
, &sip
);
1717 (void) sd_network_link_get_captive_portal(info
->ifindex
, &captive_portal
);
1718 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
1719 (void) sd_network_link_get_network_file_dropins(info
->ifindex
, &network_dropins
);
1720 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
1721 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
1722 (void) sd_network_link_get_activation_policy(info
->ifindex
, &activation_policy
);
1724 if (info
->sd_device
) {
1727 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_LINK_FILE", &link
);
1729 if (sd_device_get_property_value(info
->sd_device
, "ID_NET_LINK_FILE_DROPINS", &joined
) >= 0) {
1730 r
= strv_split_full(&link_dropins
, joined
, ":", EXTRACT_CUNESCAPE
);
1735 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_DRIVER", &driver
);
1736 (void) sd_device_get_property_value(info
->sd_device
, "ID_PATH", &path
);
1737 (void) device_get_vendor_string(info
->sd_device
, &vendor
);
1738 (void) device_get_model_string(info
->sd_device
, &model
);
1741 r
= net_get_type_string(info
->sd_device
, info
->iftype
, &t
);
1745 char lease_file
[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)];
1746 xsprintf(lease_file
, "/run/systemd/netif/leases/%i", info
->ifindex
);
1748 (void) dhcp_lease_load(&lease
, lease_file
);
1750 r
= format_dropins(network_dropins
);
1754 if (strv_prepend(&network_dropins
, network
) < 0)
1757 r
= format_dropins(link_dropins
);
1761 if (strv_prepend(&link_dropins
, link
) < 0)
1764 table
= table_new_vertical();
1769 table_set_width(table
, 0);
1771 /* unit files and basic states. */
1772 r
= table_add_many(table
,
1773 TABLE_FIELD
, "Link File",
1774 TABLE_STRV
, link_dropins
?: STRV_MAKE("n/a"),
1775 TABLE_FIELD
, "Network File",
1776 TABLE_STRV
, network_dropins
?: STRV_MAKE("n/a"),
1777 TABLE_FIELD
, "State");
1779 return table_log_add_error(r
);
1781 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s (%s%s%s)",
1782 on_color_operational
, strna(operational_state
), off_color_operational
,
1783 on_color_setup
, setup_state
?: "unmanaged", off_color_setup
);
1785 return table_log_add_error(r
);
1787 r
= table_add_many(table
,
1788 TABLE_FIELD
, "Online state",
1789 TABLE_STRING
, online_state
?: "unknown",
1790 TABLE_SET_COLOR
, on_color_online
);
1792 return table_log_add_error(r
);
1794 r
= table_add_string_line(table
, "Type", t
);
1798 r
= table_add_string_line(table
, "Kind", info
->netdev_kind
);
1802 r
= table_add_string_line(table
, "Path", path
);
1806 r
= table_add_string_line(table
, "Driver", driver
);
1810 r
= table_add_string_line(table
, "Vendor", vendor
);
1814 r
= table_add_string_line(table
, "Model", model
);
1818 strv_sort(info
->alternative_names
);
1819 r
= dump_list(table
, "Alternative Names", info
->alternative_names
);
1823 if (info
->has_hw_address
) {
1824 r
= dump_hw_address(table
, hwdb
, "Hardware Address", &info
->hw_address
);
1829 if (info
->has_permanent_hw_address
) {
1830 r
= dump_hw_address(table
, hwdb
, "Permanent Hardware Address", &info
->permanent_hw_address
);
1835 if (info
->mtu
> 0) {
1836 char min_str
[DECIMAL_STR_MAX(uint32_t)], max_str
[DECIMAL_STR_MAX(uint32_t)];
1838 xsprintf(min_str
, "%" PRIu32
, info
->min_mtu
);
1839 xsprintf(max_str
, "%" PRIu32
, info
->max_mtu
);
1841 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "MTU");
1843 return table_log_add_error(r
);
1845 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"%s%s%s%s%s%s%s",
1847 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? " (" : "",
1848 info
->min_mtu
> 0 ? "min: " : "",
1849 info
->min_mtu
> 0 ? min_str
: "",
1850 info
->min_mtu
> 0 && info
->max_mtu
> 0 ? ", " : "",
1851 info
->max_mtu
> 0 ? "max: " : "",
1852 info
->max_mtu
> 0 ? max_str
: "",
1853 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? ")" : "");
1855 return table_log_add_error(r
);
1858 r
= table_add_string_line(table
, "QDisc", info
->qdisc
);
1862 if (info
->master
> 0) {
1863 r
= table_add_many(table
,
1864 TABLE_FIELD
, "Master",
1865 TABLE_IFINDEX
, info
->master
);
1867 return table_log_add_error(r
);
1870 if (info
->has_ipv6_address_generation_mode
) {
1871 static const struct {
1876 { "stable-privacy" },
1880 r
= table_add_many(table
,
1881 TABLE_FIELD
, "IPv6 Address Generation Mode",
1882 TABLE_STRING
, mode_table
[info
->addr_gen_mode
]);
1884 return table_log_add_error(r
);
1887 if (streq_ptr(info
->netdev_kind
, "bridge")) {
1888 r
= table_add_many(table
,
1889 TABLE_FIELD
, "Forward Delay",
1890 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->forward_delay
),
1891 TABLE_FIELD
, "Hello Time",
1892 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->hello_time
),
1893 TABLE_FIELD
, "Max Age",
1894 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->max_age
),
1895 TABLE_FIELD
, "Ageing Time",
1896 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->ageing_time
),
1897 TABLE_FIELD
, "Priority",
1898 TABLE_UINT16
, info
->priority
,
1900 TABLE_BOOLEAN
, info
->stp_state
> 0,
1901 TABLE_FIELD
, "Multicast IGMP Version",
1902 TABLE_UINT8
, info
->mcast_igmp_version
,
1903 TABLE_FIELD
, "Cost",
1904 TABLE_UINT32
, info
->cost
);
1906 return table_log_add_error(r
);
1908 if (info
->port_state
<= BR_STATE_BLOCKING
) {
1909 r
= table_add_many(table
,
1910 TABLE_FIELD
, "Port State",
1911 TABLE_STRING
, bridge_state_to_string(info
->port_state
));
1913 return table_log_add_error(r
);
1916 } else if (streq_ptr(info
->netdev_kind
, "bond")) {
1917 r
= table_add_many(table
,
1918 TABLE_FIELD
, "Mode",
1919 TABLE_STRING
, bond_mode_to_string(info
->mode
),
1920 TABLE_FIELD
, "Miimon",
1921 TABLE_TIMESPAN_MSEC
, info
->miimon
* USEC_PER_MSEC
,
1922 TABLE_FIELD
, "Updelay",
1923 TABLE_TIMESPAN_MSEC
, info
->updelay
* USEC_PER_MSEC
,
1924 TABLE_FIELD
, "Downdelay",
1925 TABLE_TIMESPAN_MSEC
, info
->downdelay
* USEC_PER_MSEC
);
1927 return table_log_add_error(r
);
1929 } else if (streq_ptr(info
->netdev_kind
, "vxlan")) {
1930 char ttl
[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1932 if (info
->vxlan_info
.vni
> 0) {
1933 r
= table_add_many(table
,
1935 TABLE_UINT32
, info
->vxlan_info
.vni
);
1937 return table_log_add_error(r
);
1940 if (IN_SET(info
->vxlan_info
.group_family
, AF_INET
, AF_INET6
)) {
1943 r
= in_addr_is_multicast(info
->vxlan_info
.group_family
, &info
->vxlan_info
.group
);
1949 r
= table_add_many(table
,
1951 info
->vxlan_info
.group_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
, &info
->vxlan_info
.group
);
1953 return table_log_add_error(r
);
1956 if (IN_SET(info
->vxlan_info
.local_family
, AF_INET
, AF_INET6
)) {
1957 r
= table_add_many(table
,
1958 TABLE_FIELD
, "Local",
1959 info
->vxlan_info
.local_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
, &info
->vxlan_info
.local
);
1961 return table_log_add_error(r
);
1964 if (info
->vxlan_info
.dest_port
> 0) {
1965 r
= table_add_many(table
,
1966 TABLE_FIELD
, "Destination Port",
1967 TABLE_UINT16
, be16toh(info
->vxlan_info
.dest_port
));
1969 return table_log_add_error(r
);
1972 if (info
->vxlan_info
.link
> 0) {
1973 r
= table_add_many(table
,
1974 TABLE_FIELD
, "Underlying Device",
1975 TABLE_IFINDEX
, info
->vxlan_info
.link
);
1977 return table_log_add_error(r
);
1980 r
= table_add_many(table
,
1981 TABLE_FIELD
, "Learning",
1982 TABLE_BOOLEAN
, info
->vxlan_info
.learning
,
1984 TABLE_BOOLEAN
, info
->vxlan_info
.rsc
,
1985 TABLE_FIELD
, "L3MISS",
1986 TABLE_BOOLEAN
, info
->vxlan_info
.l3miss
,
1987 TABLE_FIELD
, "L2MISS",
1988 TABLE_BOOLEAN
, info
->vxlan_info
.l2miss
);
1990 return table_log_add_error(r
);
1992 if (info
->vxlan_info
.tos
> 1) {
1993 r
= table_add_many(table
,
1995 TABLE_UINT8
, info
->vxlan_info
.tos
);
1997 return table_log_add_error(r
);
2000 if (info
->vxlan_info
.ttl
> 0)
2001 xsprintf(ttl
, "%" PRIu8
, info
->vxlan_info
.ttl
);
2003 strcpy(ttl
, "auto");
2005 r
= table_add_many(table
,
2009 return table_log_add_error(r
);
2011 } else if (streq_ptr(info
->netdev_kind
, "vlan") && info
->vlan_id
> 0) {
2012 r
= table_add_many(table
,
2013 TABLE_FIELD
, "VLan Id",
2014 TABLE_UINT16
, info
->vlan_id
);
2016 return table_log_add_error(r
);
2018 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
2019 if (in_addr_is_set(AF_INET
, &info
->local
)) {
2020 r
= table_add_many(table
,
2021 TABLE_FIELD
, "Local",
2022 TABLE_IN_ADDR
, &info
->local
);
2024 return table_log_add_error(r
);
2027 if (in_addr_is_set(AF_INET
, &info
->remote
)) {
2028 r
= table_add_many(table
,
2029 TABLE_FIELD
, "Remote",
2030 TABLE_IN_ADDR
, &info
->remote
);
2032 return table_log_add_error(r
);
2035 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
2036 if (in_addr_is_set(AF_INET6
, &info
->local
)) {
2037 r
= table_add_many(table
,
2038 TABLE_FIELD
, "Local",
2039 TABLE_IN6_ADDR
, &info
->local
);
2041 return table_log_add_error(r
);
2044 if (in_addr_is_set(AF_INET6
, &info
->remote
)) {
2045 r
= table_add_many(table
,
2046 TABLE_FIELD
, "Remote",
2047 TABLE_IN6_ADDR
, &info
->remote
);
2049 return table_log_add_error(r
);
2052 } else if (streq_ptr(info
->netdev_kind
, "geneve")) {
2053 r
= table_add_many(table
,
2055 TABLE_UINT32
, info
->vni
);
2057 return table_log_add_error(r
);
2059 if (info
->has_tunnel_ipv4
&& in_addr_is_set(AF_INET
, &info
->remote
)) {
2060 r
= table_add_many(table
,
2061 TABLE_FIELD
, "Remote",
2062 TABLE_IN_ADDR
, &info
->remote
);
2064 return table_log_add_error(r
);
2065 } else if (in_addr_is_set(AF_INET6
, &info
->remote
)) {
2066 r
= table_add_many(table
,
2067 TABLE_FIELD
, "Remote",
2068 TABLE_IN6_ADDR
, &info
->remote
);
2070 return table_log_add_error(r
);
2073 if (info
->ttl
> 0) {
2074 r
= table_add_many(table
,
2076 TABLE_UINT8
, info
->ttl
);
2078 return table_log_add_error(r
);
2081 if (info
->tos
> 0) {
2082 r
= table_add_many(table
,
2084 TABLE_UINT8
, info
->tos
);
2086 return table_log_add_error(r
);
2089 r
= table_add_many(table
,
2090 TABLE_FIELD
, "Port",
2091 TABLE_UINT16
, info
->tunnel_port
,
2092 TABLE_FIELD
, "Inherit",
2093 TABLE_STRING
, geneve_df_to_string(info
->inherit
));
2095 return table_log_add_error(r
);
2098 r
= table_add_many(table
,
2099 TABLE_FIELD
, "IPDoNotFragment",
2100 TABLE_UINT8
, info
->df
);
2102 return table_log_add_error(r
);
2105 r
= table_add_many(table
,
2106 TABLE_FIELD
, "UDPChecksum",
2107 TABLE_BOOLEAN
, info
->csum
,
2108 TABLE_FIELD
, "UDP6ZeroChecksumTx",
2109 TABLE_BOOLEAN
, info
->csum6_tx
,
2110 TABLE_FIELD
, "UDP6ZeroChecksumRx",
2111 TABLE_BOOLEAN
, info
->csum6_rx
);
2113 return table_log_add_error(r
);
2115 if (info
->label
> 0) {
2116 r
= table_add_many(table
,
2117 TABLE_FIELD
, "FlowLabel",
2118 TABLE_UINT32
, info
->label
);
2120 return table_log_add_error(r
);
2123 } else if (STRPTR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap")) {
2124 r
= table_add_many(table
,
2125 TABLE_FIELD
, "Mode",
2126 TABLE_STRING
, macvlan_mode_to_string(info
->macvlan_mode
));
2128 return table_log_add_error(r
);
2130 } else if (streq_ptr(info
->netdev_kind
, "ipvlan")) {
2133 if (info
->ipvlan_flags
& IPVLAN_F_PRIVATE
)
2135 else if (info
->ipvlan_flags
& IPVLAN_F_VEPA
)
2140 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "Mode");
2142 return table_log_add_error(r
);
2144 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)",
2145 ipvlan_mode_to_string(info
->ipvlan_mode
), p
);
2147 return table_log_add_error(r
);
2150 if (info
->has_wlan_link_info
) {
2151 _cleanup_free_
char *esc
= NULL
;
2153 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "Wi-Fi access point");
2155 return table_log_add_error(r
);
2158 esc
= cescape(info
->ssid
);
2160 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)",
2162 ETHER_ADDR_TO_STR(&info
->bssid
));
2164 return table_log_add_error(r
);
2167 if (info
->has_bitrates
) {
2168 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "Bit Rate (Tx/Rx)");
2170 return table_log_add_error(r
);
2172 r
= table_add_cell_stringf(table
, NULL
, "%sbps/%sbps",
2173 FORMAT_BYTES_FULL(info
->tx_bitrate
, 0),
2174 FORMAT_BYTES_FULL(info
->rx_bitrate
, 0));
2176 return table_log_add_error(r
);
2179 if (info
->has_tx_queues
|| info
->has_rx_queues
) {
2180 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "Number of Queues (Tx/Rx)");
2182 return table_log_add_error(r
);
2184 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"/%" PRIu32
, info
->tx_queues
, info
->rx_queues
);
2186 return table_log_add_error(r
);
2189 if (info
->has_ethtool_link_info
) {
2190 if (IN_SET(info
->autonegotiation
, AUTONEG_DISABLE
, AUTONEG_ENABLE
)) {
2191 r
= table_add_many(table
,
2192 TABLE_FIELD
, "Auto negotiation",
2193 TABLE_BOOLEAN
, info
->autonegotiation
== AUTONEG_ENABLE
);
2195 return table_log_add_error(r
);
2198 if (info
->speed
> 0 && info
->speed
!= UINT64_MAX
) {
2199 r
= table_add_many(table
,
2200 TABLE_FIELD
, "Speed",
2201 TABLE_BPS
, info
->speed
);
2203 return table_log_add_error(r
);
2206 r
= table_add_string_line(table
, "Duplex", duplex_to_string(info
->duplex
));
2210 r
= table_add_string_line(table
, "Port", port_to_string(info
->port
));
2215 r
= dump_addresses(rtnl
, lease
, table
, info
->ifindex
);
2219 r
= dump_gateways(rtnl
, hwdb
, table
, info
->ifindex
);
2223 r
= dump_list(table
, "DNS", dns
);
2227 r
= dump_list(table
, "Search Domains", search_domains
);
2231 r
= dump_list(table
, "Route Domains", route_domains
);
2235 r
= dump_list(table
, "NTP", ntp
);
2239 r
= dump_list(table
, "SIP", sip
);
2243 r
= dump_ifindexes(table
, "Carrier Bound To", carrier_bound_to
);
2247 r
= dump_ifindexes(table
, "Carrier Bound By", carrier_bound_by
);
2251 r
= table_add_string_line(table
, "Activation Policy", activation_policy
);
2255 r
= sd_network_link_get_required_for_online(info
->ifindex
);
2257 r
= table_add_many(table
,
2258 TABLE_FIELD
, "Required For Online",
2261 return table_log_add_error(r
);
2264 if (captive_portal
) {
2265 r
= table_add_many(table
,
2266 TABLE_FIELD
, "Captive Portal",
2267 TABLE_STRING
, captive_portal
,
2268 TABLE_SET_URL
, captive_portal
);
2270 return table_log_add_error(r
);
2274 const sd_dhcp_client_id
*client_id
;
2277 r
= sd_dhcp_lease_get_timezone(lease
, &tz
);
2279 r
= table_add_many(table
,
2280 TABLE_FIELD
, "Time Zone",
2283 return table_log_add_error(r
);
2286 r
= sd_dhcp_lease_get_client_id(lease
, &client_id
);
2288 _cleanup_free_
char *id
= NULL
;
2290 r
= sd_dhcp_client_id_to_string(client_id
, &id
);
2292 r
= table_add_many(table
,
2293 TABLE_FIELD
, "DHCP4 Client ID",
2296 return table_log_add_error(r
);
2301 r
= sd_network_link_get_dhcp6_client_iaid_string(info
->ifindex
, &iaid
);
2303 r
= table_add_many(table
,
2304 TABLE_FIELD
, "DHCP6 Client IAID",
2305 TABLE_STRING
, iaid
);
2307 return table_log_add_error(r
);
2310 r
= sd_network_link_get_dhcp6_client_duid_string(info
->ifindex
, &duid
);
2312 r
= table_add_many(table
,
2313 TABLE_FIELD
, "DHCP6 Client DUID",
2314 TABLE_STRING
, duid
);
2316 return table_log_add_error(r
);
2319 r
= dump_lldp_neighbors(table
, "Connected To", info
->ifindex
);
2323 r
= dump_dhcp_leases(table
, "Offered DHCP leases", bus
, info
);
2327 r
= dump_statistics(table
, info
);
2331 /* First line: circle, ifindex, ifname. */
2332 printf("%s%s%s %d: %s\n",
2333 on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
,
2334 info
->ifindex
, info
->name
);
2336 r
= table_print(table
, NULL
);
2338 return table_log_print_error(r
);
2340 return show_logs(info
);
2343 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
2344 _cleanup_free_
char *operational_state
= NULL
, *online_state
= NULL
, *netifs_joined
= NULL
;
2345 _cleanup_strv_free_
char **netifs
= NULL
, **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
2346 const char *on_color_operational
, *off_color_operational
, *on_color_online
;
2347 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2352 (void) sd_network_get_operational_state(&operational_state
);
2353 operational_state_to_color(NULL
, operational_state
, &on_color_operational
, &off_color_operational
);
2355 (void) sd_network_get_online_state(&online_state
);
2356 online_state_to_color(online_state
, &on_color_online
, NULL
);
2358 table
= table_new_vertical();
2363 table_set_width(table
, 0);
2365 r
= get_files_in_directory("/run/systemd/netif/links/", &netifs
);
2366 if (r
< 0 && r
!= -ENOENT
)
2367 return log_error_errno(r
, "Failed to list network interfaces: %m");
2369 netifs_joined
= strv_join(netifs
, ", ");
2374 r
= table_add_many(table
,
2375 TABLE_FIELD
, "State",
2376 TABLE_STRING
, strna(operational_state
),
2377 TABLE_SET_COLOR
, on_color_operational
,
2378 TABLE_FIELD
, "Online state",
2379 TABLE_STRING
, online_state
?: "unknown",
2380 TABLE_SET_COLOR
, on_color_online
);
2382 return table_log_add_error(r
);
2384 r
= dump_addresses(rtnl
, NULL
, table
, 0);
2388 r
= dump_gateways(rtnl
, hwdb
, table
, 0);
2392 (void) sd_network_get_dns(&dns
);
2393 r
= dump_list(table
, "DNS", dns
);
2397 (void) sd_network_get_search_domains(&search_domains
);
2398 r
= dump_list(table
, "Search Domains", search_domains
);
2402 (void) sd_network_get_route_domains(&route_domains
);
2403 r
= dump_list(table
, "Route Domains", route_domains
);
2407 (void) sd_network_get_ntp(&ntp
);
2408 r
= dump_list(table
, "NTP", ntp
);
2412 printf("%s%s%s Interfaces: %s\n",
2413 on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
,
2414 strna(netifs_joined
));
2416 r
= table_print(table
, NULL
);
2418 return table_log_print_error(r
);
2420 return show_logs(NULL
);
2423 static int link_status(int argc
, char *argv
[], void *userdata
) {
2424 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2425 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2426 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
2427 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2430 r
= acquire_bus(&bus
);
2434 if (arg_json_format_flags
!= JSON_FORMAT_OFF
) {
2435 if (arg_all
|| argc
<= 1)
2436 return dump_manager_description(bus
);
2438 return dump_link_description(bus
, strv_skip(argv
, 1));
2441 pager_open(arg_pager_flags
);
2443 r
= sd_netlink_open(&rtnl
);
2445 return log_error_errno(r
, "Failed to connect to netlink: %m");
2447 r
= sd_hwdb_new(&hwdb
);
2449 log_debug_errno(r
, "Failed to open hardware database: %m");
2452 c
= acquire_link_info(bus
, rtnl
, NULL
, &links
);
2454 return system_status(rtnl
, hwdb
);
2456 c
= acquire_link_info(bus
, rtnl
, argv
+ 1, &links
);
2463 FOREACH_ARRAY(i
, links
, c
) {
2467 RET_GATHER(r
, link_status_one(bus
, rtnl
, hwdb
, i
));
2475 static char *lldp_capabilities_to_string(uint16_t x
) {
2476 static const char characters
[] = {
2477 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2482 ret
= new(char, ELEMENTSOF(characters
) + 1);
2486 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
2487 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
2493 static void lldp_capabilities_legend(uint16_t x
) {
2494 unsigned cols
= columns();
2495 static const char* const table
[] = {
2499 "w - WLAN Access Point",
2502 "d - DOCSIS cable device",
2504 "c - Customer VLAN",
2506 "m - Two-port MAC Relay (TPMR)",
2512 printf("\nCapability Flags:\n");
2513 for (unsigned w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
2514 if (x
& (1U << i
) || arg_all
) {
2517 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
2520 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
2525 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
2526 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2527 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2528 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2533 r
= sd_netlink_open(&rtnl
);
2535 return log_error_errno(r
, "Failed to connect to netlink: %m");
2537 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
2541 pager_open(arg_pager_flags
);
2543 table
= table_new("link",
2548 "port-description");
2553 table_set_width(table
, 0);
2555 table_set_header(table
, arg_legend
);
2557 assert_se(cell
= table_get_cell(table
, 0, 3));
2558 table_set_minimum_width(table
, cell
, 11);
2559 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
2561 FOREACH_ARRAY(link
, links
, c
) {
2562 _cleanup_fclose_
FILE *f
= NULL
;
2564 r
= open_lldp_neighbors(link
->ifindex
, &f
);
2568 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", link
->ifindex
);
2573 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
;
2574 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
2575 _cleanup_free_
char *capabilities
= NULL
;
2578 r
= next_lldp_neighbor(f
, &n
);
2580 log_warning_errno(r
, "Failed to read neighbor data: %m");
2586 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
2587 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
2588 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
2589 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
2591 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
2592 capabilities
= lldp_capabilities_to_string(cc
);
2596 r
= table_add_many(table
,
2597 TABLE_STRING
, link
->name
,
2598 TABLE_STRING
, chassis_id
,
2599 TABLE_STRING
, system_name
,
2600 TABLE_STRING
, capabilities
,
2601 TABLE_STRING
, port_id
,
2602 TABLE_STRING
, port_description
);
2604 return table_log_add_error(r
);
2610 r
= table_print(table
, NULL
);
2612 return table_log_print_error(r
);
2615 lldp_capabilities_legend(all
);
2616 printf("\n%i neighbors listed.\n", m
);
2622 static int link_delete_send_message(sd_netlink
*rtnl
, int index
) {
2623 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2629 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_DELLINK
, index
);
2631 return rtnl_log_create_error(r
);
2633 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2640 static int link_up_down_send_message(sd_netlink
*rtnl
, char *command
, int index
) {
2641 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2647 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_SETLINK
, index
);
2649 return rtnl_log_create_error(r
);
2651 if (streq(command
, "up"))
2652 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2654 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2656 return log_error_errno(r
, "Could not set link flags: %m");
2658 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2665 static int link_up_down(int argc
, char *argv
[], void *userdata
) {
2666 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2667 _cleanup_set_free_ Set
*indexes
= NULL
;
2671 r
= sd_netlink_open(&rtnl
);
2673 return log_error_errno(r
, "Failed to connect to netlink: %m");
2675 indexes
= set_new(NULL
);
2679 for (int i
= 1; i
< argc
; i
++) {
2680 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2684 r
= set_put(indexes
, INT_TO_PTR(index
));
2689 SET_FOREACH(p
, indexes
) {
2690 index
= PTR_TO_INT(p
);
2691 r
= link_up_down_send_message(rtnl
, argv
[0], index
);
2693 return log_error_errno(r
, "Failed to bring %s interface %s: %m",
2694 argv
[0], FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2700 static int link_delete(int argc
, char *argv
[], void *userdata
) {
2701 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2702 _cleanup_set_free_ Set
*indexes
= NULL
;
2706 r
= sd_netlink_open(&rtnl
);
2708 return log_error_errno(r
, "Failed to connect to netlink: %m");
2710 indexes
= set_new(NULL
);
2714 for (int i
= 1; i
< argc
; i
++) {
2715 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2719 r
= set_put(indexes
, INT_TO_PTR(index
));
2724 SET_FOREACH(p
, indexes
) {
2725 index
= PTR_TO_INT(p
);
2726 r
= link_delete_send_message(rtnl
, index
);
2728 return log_error_errno(r
, "Failed to delete interface %s: %m",
2729 FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2735 static int link_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2736 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2743 r
= bus_call_method(bus
, bus_network_mgr
, "RenewLink", &error
, NULL
, "i", index
);
2745 return log_error_errno(r
, "Failed to renew dynamic configuration of interface %s: %s",
2746 name
, bus_error_message(&error
, r
));
2751 static int link_renew(int argc
, char *argv
[], void *userdata
) {
2752 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2753 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2756 r
= acquire_bus(&bus
);
2762 for (int i
= 1; i
< argc
; i
++) {
2765 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2769 RET_GATHER(r
, link_renew_one(bus
, index
, argv
[i
]));
2775 static int link_force_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2776 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2783 r
= bus_call_method(bus
, bus_network_mgr
, "ForceRenewLink", &error
, NULL
, "i", index
);
2785 return log_error_errno(r
, "Failed to force renew dynamic configuration of interface %s: %s",
2786 name
, bus_error_message(&error
, r
));
2791 static int link_force_renew(int argc
, char *argv
[], void *userdata
) {
2792 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2793 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2796 r
= acquire_bus(&bus
);
2800 for (int i
= 1; i
< argc
; i
++) {
2801 int index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2805 r
= link_force_renew_one(bus
, index
, argv
[i
]);
2806 if (r
< 0 && k
>= 0)
2813 static int verb_reload(int argc
, char *argv
[], void *userdata
) {
2814 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2815 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2818 r
= acquire_bus(&bus
);
2822 r
= bus_call_method(bus
, bus_network_mgr
, "Reload", &error
, NULL
, NULL
);
2824 return log_error_errno(r
, "Failed to reload network settings: %s", bus_error_message(&error
, r
));
2829 static int verb_reconfigure(int argc
, char *argv
[], void *userdata
) {
2830 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2831 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2832 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2833 _cleanup_set_free_ Set
*indexes
= NULL
;
2837 r
= acquire_bus(&bus
);
2841 indexes
= set_new(NULL
);
2845 for (int i
= 1; i
< argc
; i
++) {
2846 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2850 r
= set_put(indexes
, INT_TO_PTR(index
));
2855 SET_FOREACH(p
, indexes
) {
2856 index
= PTR_TO_INT(p
);
2857 r
= bus_call_method(bus
, bus_network_mgr
, "ReconfigureLink", &error
, NULL
, "i", index
);
2859 return log_error_errno(r
, "Failed to reconfigure network interface %s: %s",
2860 FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
),
2861 bus_error_message(&error
, r
));
2867 static int help(void) {
2868 _cleanup_free_
char *link
= NULL
;
2871 r
= terminal_urlify_man("networkctl", "1", &link
);
2875 printf("%s [OPTIONS...] COMMAND\n\n"
2876 "%sQuery and control the networking subsystem.%s\n"
2878 " list [PATTERN...] List links\n"
2879 " status [PATTERN...] Show link status\n"
2880 " lldp [PATTERN...] Show LLDP neighbors\n"
2881 " label Show current address label entries in the kernel\n"
2882 " delete DEVICES... Delete virtual netdevs\n"
2883 " up DEVICES... Bring devices up\n"
2884 " down DEVICES... Bring devices down\n"
2885 " renew DEVICES... Renew dynamic configurations\n"
2886 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
2887 " reconfigure DEVICES... Reconfigure interfaces\n"
2888 " reload Reload .network and .netdev files\n"
2889 " edit FILES|DEVICES... Edit network configuration files\n"
2890 " cat FILES|DEVICES... Show network configuration files\n"
2891 " mask FILES... Mask network configuration files\n"
2892 " unmask FILES... Unmask network configuration files\n"
2894 " -h --help Show this help\n"
2895 " --version Show package version\n"
2896 " --no-pager Do not pipe output into a pager\n"
2897 " --no-legend Do not show the headers and footers\n"
2898 " -a --all Show status for all links\n"
2899 " -s --stats Show detailed link statistics\n"
2900 " -l --full Do not ellipsize output\n"
2901 " -n --lines=INTEGER Number of journal entries to show\n"
2902 " --json=pretty|short|off\n"
2903 " Generate JSON output\n"
2904 " --no-reload Do not reload systemd-networkd or systemd-udevd\n"
2905 " after editing network config\n"
2906 " --drop-in=NAME Edit specified drop-in instead of main config file\n"
2907 " --runtime Edit runtime config files\n"
2908 "\nSee the %s for details.\n",
2909 program_invocation_short_name
,
2917 static int parse_argv(int argc
, char *argv
[]) {
2919 ARG_VERSION
= 0x100,
2928 static const struct option options
[] = {
2929 { "help", no_argument
, NULL
, 'h' },
2930 { "version", no_argument
, NULL
, ARG_VERSION
},
2931 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2932 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2933 { "all", no_argument
, NULL
, 'a' },
2934 { "stats", no_argument
, NULL
, 's' },
2935 { "full", no_argument
, NULL
, 'l' },
2936 { "lines", required_argument
, NULL
, 'n' },
2937 { "json", required_argument
, NULL
, ARG_JSON
},
2938 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
2939 { "drop-in", required_argument
, NULL
, ARG_DROP_IN
},
2940 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
2949 while ((c
= getopt_long(argc
, argv
, "hasln:", options
, NULL
)) >= 0) {
2960 arg_pager_flags
|= PAGER_DISABLE
;
2968 arg_no_reload
= true;
2976 if (isempty(optarg
))
2977 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Empty drop-in file name.");
2979 if (!endswith(optarg
, ".conf")) {
2982 conf
= strjoin(optarg
, ".conf");
2986 free_and_replace(arg_drop_in
, conf
);
2988 r
= free_and_strdup(&arg_drop_in
, optarg
);
2993 if (!filename_is_valid(arg_drop_in
))
2994 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2995 "Invalid drop-in file name '%s'.", arg_drop_in
);
3012 if (safe_atou(optarg
, &arg_lines
) < 0)
3013 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3014 "Failed to parse lines '%s'", optarg
);
3018 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3027 assert_not_reached();
3034 static int networkctl_main(int argc
, char *argv
[]) {
3035 static const Verb verbs
[] = {
3036 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
|VERB_ONLINE_ONLY
, list_links
},
3037 { "status", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, link_status
},
3038 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
3039 { "label", 1, 1, 0, list_address_labels
},
3040 { "delete", 2, VERB_ANY
, 0, link_delete
},
3041 { "up", 2, VERB_ANY
, 0, link_up_down
},
3042 { "down", 2, VERB_ANY
, 0, link_up_down
},
3043 { "renew", 2, VERB_ANY
, VERB_ONLINE_ONLY
, link_renew
},
3044 { "forcerenew", 2, VERB_ANY
, VERB_ONLINE_ONLY
, link_force_renew
},
3045 { "reconfigure", 2, VERB_ANY
, VERB_ONLINE_ONLY
, verb_reconfigure
},
3046 { "reload", 1, 1, VERB_ONLINE_ONLY
, verb_reload
},
3047 { "edit", 2, VERB_ANY
, 0, verb_edit
},
3048 { "cat", 2, VERB_ANY
, 0, verb_cat
},
3049 { "mask", 2, VERB_ANY
, 0, verb_mask
},
3050 { "unmask", 2, VERB_ANY
, 0, verb_unmask
},
3054 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3057 static int run(int argc
, char* argv
[]) {
3064 r
= parse_argv(argc
, argv
);
3068 return networkctl_main(argc
, argv
);
3071 DEFINE_MAIN_FUNCTION(run
);