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"
74 #include "wifi-util.h"
76 /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */
77 #define NETDEV_KIND_MAX 64
79 /* use 128 kB for receive socket kernel queue, we shouldn't need more here */
80 #define RCVBUF_SIZE (128*1024)
82 PagerFlags arg_pager_flags
= 0;
83 bool arg_legend
= true;
84 bool arg_no_reload
= false;
86 bool arg_stats
= false;
87 bool arg_full
= false;
88 bool arg_runtime
= false;
89 unsigned arg_lines
= 10;
90 char *arg_drop_in
= NULL
;
91 JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
93 STATIC_DESTRUCTOR_REGISTER(arg_drop_in
, freep
);
95 static int check_netns_match(sd_bus
*bus
) {
96 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
103 r
= bus_get_property_trivial(bus
, bus_network_mgr
, "NamespaceId", &error
, 't', &id
);
105 log_debug_errno(r
, "Failed to query network namespace of networkd, ignoring: %s", bus_error_message(&error
, r
));
109 log_debug("systemd-networkd.service not running in a network namespace (?), skipping netns check.");
113 if (stat("/proc/self/ns/net", &st
) < 0)
114 return log_error_errno(errno
, "Failed to determine our own network namespace ID: %m");
117 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
118 "networkctl must be invoked in same network namespace as systemd-networkd.service.");
123 bool networkd_is_running(void) {
124 static int cached
= -1;
128 r
= access("/run/systemd/netif/state", F_OK
);
131 log_debug_errno(errno
,
132 "Failed to determine whether networkd is running, assuming it's not: %m");
142 int acquire_bus(sd_bus
**ret
) {
143 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
148 r
= sd_bus_open_system(&bus
);
150 return log_error_errno(r
, "Failed to connect to system bus: %m");
152 if (networkd_is_running()) {
153 r
= check_netns_match(bus
);
157 log_warning("systemd-networkd is not running, output might be incomplete.");
159 *ret
= TAKE_PTR(bus
);
163 static int get_description(sd_bus
*bus
, JsonVariant
**ret
) {
164 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
165 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
172 r
= bus_call_method(bus
, bus_network_mgr
, "Describe", &error
, &reply
, NULL
);
174 return log_error_errno(r
, "Failed to get description: %s", bus_error_message(&error
, r
));
176 r
= sd_bus_message_read(reply
, "s", &text
);
178 return bus_log_parse_error(r
);
180 r
= json_parse(text
, 0, ret
, NULL
, NULL
);
182 return log_error_errno(r
, "Failed to parse JSON: %m");
187 static int dump_manager_description(sd_bus
*bus
) {
188 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
193 r
= get_description(bus
, &v
);
197 json_variant_dump(v
, arg_json_format_flags
, NULL
, NULL
);
201 static int dump_link_description(sd_bus
*bus
, char * const *patterns
) {
202 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
203 _cleanup_free_
bool *matched_patterns
= NULL
;
211 r
= get_description(bus
, &v
);
215 matched_patterns
= new0(bool, strv_length(patterns
));
216 if (!matched_patterns
)
219 JSON_VARIANT_ARRAY_FOREACH(i
, json_variant_by_key(v
, "Interfaces")) {
220 char ifindex_str
[DECIMAL_STR_MAX(int64_t)];
225 name
= json_variant_string(json_variant_by_key(i
, "Name"));
226 index
= json_variant_integer(json_variant_by_key(i
, "Index"));
227 xsprintf(ifindex_str
, "%" PRIi64
, index
);
229 if (!strv_fnmatch_full(patterns
, ifindex_str
, 0, &pos
) &&
230 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
234 JSON_VARIANT_ARRAY_FOREACH(a
, json_variant_by_key(i
, "AlternativeNames"))
235 if (strv_fnmatch_full(patterns
, json_variant_string(a
), 0, &pos
)) {
244 matched_patterns
[pos
] = true;
245 json_variant_dump(i
, arg_json_format_flags
, NULL
, NULL
);
249 /* Look if we matched all our arguments that are not globs. It is OK for a glob to match
250 * nothing, but not for an exact argument. */
251 for (size_t pos
= 0; pos
< strv_length(patterns
); pos
++) {
252 if (matched_patterns
[pos
])
255 if (string_is_glob(patterns
[pos
]))
256 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
259 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
260 "Interface \"%s\" not found.", patterns
[pos
]);
264 log_warning("No interfaces matched.");
269 static void operational_state_to_color(
275 if (STRPTR_IN_SET(state
, "routable", "enslaved") ||
276 (streq_ptr(name
, "lo") && streq_ptr(state
, "carrier"))) {
278 *on
= ansi_highlight_green();
280 *off
= ansi_normal();
281 } else if (streq_ptr(state
, "degraded")) {
283 *on
= ansi_highlight_yellow();
285 *off
= ansi_normal();
294 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
295 if (streq_ptr(state
, "configured")) {
297 *on
= ansi_highlight_green();
299 *off
= ansi_normal();
300 } else if (streq_ptr(state
, "configuring")) {
302 *on
= ansi_highlight_yellow();
304 *off
= ansi_normal();
305 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
307 *on
= ansi_highlight_red();
309 *off
= ansi_normal();
318 static void online_state_to_color(const char *state
, const char **on
, const char **off
) {
319 if (streq_ptr(state
, "online")) {
321 *on
= ansi_highlight_green();
323 *off
= ansi_normal();
324 } else if (streq_ptr(state
, "partial")) {
326 *on
= ansi_highlight_yellow();
328 *off
= ansi_normal();
337 typedef struct VxLanInfo
{
344 union in_addr_union local
;
345 union in_addr_union group
;
358 typedef struct LinkInfo
{
359 char name
[IFNAMSIZ
+1];
361 sd_device
*sd_device
;
363 unsigned short iftype
;
364 struct hw_addr_data hw_address
;
365 struct hw_addr_data permanent_hw_address
;
372 uint8_t addr_gen_mode
;
374 char **alternative_names
;
377 struct rtnl_link_stats64 stats64
;
378 struct rtnl_link_stats stats
;
385 uint32_t forward_delay
;
388 uint32_t ageing_time
;
392 uint8_t mcast_igmp_version
;
396 VxLanInfo vxlan_info
;
409 uint16_t tunnel_port
;
412 union in_addr_union local
;
413 union in_addr_union remote
;
421 /* macvlan and macvtap info */
422 uint32_t macvlan_mode
;
425 uint16_t ipvlan_mode
;
426 uint16_t ipvlan_flags
;
435 enum nl80211_iftype wlan_iftype
;
437 struct ether_addr bssid
;
439 bool has_hw_address
:1;
440 bool has_permanent_hw_address
:1;
441 bool has_tx_queues
:1;
442 bool has_rx_queues
:1;
446 bool has_ethtool_link_info
:1;
447 bool has_wlan_link_info
:1;
448 bool has_tunnel_ipv4
:1;
449 bool has_ipv6_address_generation_mode
:1;
451 bool needs_freeing
:1;
454 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
455 return CMP(a
->ifindex
, b
->ifindex
);
458 static LinkInfo
* link_info_array_free(LinkInfo
*array
) {
459 for (unsigned i
= 0; array
&& array
[i
].needs_freeing
; i
++) {
460 sd_device_unref(array
[i
].sd_device
);
461 free(array
[i
].netdev_kind
);
463 free(array
[i
].qdisc
);
464 strv_free(array
[i
].alternative_names
);
469 DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo
*, link_info_array_free
);
471 static int decode_netdev(sd_netlink_message
*m
, LinkInfo
*info
) {
477 r
= sd_netlink_message_enter_container(m
, IFLA_LINKINFO
);
481 r
= sd_netlink_message_read_string_strdup(m
, IFLA_INFO_KIND
, &info
->netdev_kind
);
483 (void) sd_netlink_message_exit_container(m
);
487 r
= sd_netlink_message_enter_container(m
, IFLA_INFO_DATA
);
491 if (streq(info
->netdev_kind
, "bridge")) {
492 (void) sd_netlink_message_read_u32(m
, IFLA_BR_FORWARD_DELAY
, &info
->forward_delay
);
493 (void) sd_netlink_message_read_u32(m
, IFLA_BR_HELLO_TIME
, &info
->hello_time
);
494 (void) sd_netlink_message_read_u32(m
, IFLA_BR_MAX_AGE
, &info
->max_age
);
495 (void) sd_netlink_message_read_u32(m
, IFLA_BR_AGEING_TIME
, &info
->ageing_time
);
496 (void) sd_netlink_message_read_u32(m
, IFLA_BR_STP_STATE
, &info
->stp_state
);
497 (void) sd_netlink_message_read_u32(m
, IFLA_BRPORT_COST
, &info
->cost
);
498 (void) sd_netlink_message_read_u16(m
, IFLA_BR_PRIORITY
, &info
->priority
);
499 (void) sd_netlink_message_read_u8(m
, IFLA_BR_MCAST_IGMP_VERSION
, &info
->mcast_igmp_version
);
500 (void) sd_netlink_message_read_u8(m
, IFLA_BRPORT_STATE
, &info
->port_state
);
501 } if (streq(info
->netdev_kind
, "bond")) {
502 (void) sd_netlink_message_read_u8(m
, IFLA_BOND_MODE
, &info
->mode
);
503 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_MIIMON
, &info
->miimon
);
504 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_DOWNDELAY
, &info
->downdelay
);
505 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_UPDELAY
, &info
->updelay
);
506 } else if (streq(info
->netdev_kind
, "vxlan")) {
507 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_ID
, &info
->vxlan_info
.vni
);
509 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_GROUP
, &info
->vxlan_info
.group
.in
);
511 info
->vxlan_info
.group_family
= AF_INET
;
513 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_GROUP6
, &info
->vxlan_info
.group
.in6
);
515 info
->vxlan_info
.group_family
= AF_INET6
;
518 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_LOCAL
, &info
->vxlan_info
.local
.in
);
520 info
->vxlan_info
.local_family
= AF_INET
;
522 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_LOCAL6
, &info
->vxlan_info
.local
.in6
);
524 info
->vxlan_info
.local_family
= AF_INET6
;
527 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_LINK
, &info
->vxlan_info
.link
);
528 (void) sd_netlink_message_read_u16(m
, IFLA_VXLAN_PORT
, &info
->vxlan_info
.dest_port
);
529 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_PROXY
, &info
->vxlan_info
.proxy
);
530 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_LEARNING
, &info
->vxlan_info
.learning
);
531 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_RSC
, &info
->vxlan_info
.rsc
);
532 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L3MISS
, &info
->vxlan_info
.l3miss
);
533 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L2MISS
, &info
->vxlan_info
.l2miss
);
534 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TOS
, &info
->vxlan_info
.tos
);
535 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TTL
, &info
->vxlan_info
.ttl
);
536 } else if (streq(info
->netdev_kind
, "vlan"))
537 (void) sd_netlink_message_read_u16(m
, IFLA_VLAN_ID
, &info
->vlan_id
);
538 else if (STR_IN_SET(info
->netdev_kind
, "ipip", "sit")) {
539 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_LOCAL
, &info
->local
.in
);
540 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_REMOTE
, &info
->remote
.in
);
541 } else if (streq(info
->netdev_kind
, "geneve")) {
542 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_ID
, &info
->vni
);
544 r
= sd_netlink_message_read_in_addr(m
, IFLA_GENEVE_REMOTE
, &info
->remote
.in
);
546 info
->has_tunnel_ipv4
= true;
548 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GENEVE_REMOTE6
, &info
->remote
.in6
);
550 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL
, &info
->ttl
);
551 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL_INHERIT
, &info
->inherit
);
552 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TOS
, &info
->tos
);
553 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_DF
, &info
->df
);
554 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_CSUM
, &info
->csum
);
555 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
, &info
->csum6_tx
);
556 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
, &info
->csum6_rx
);
557 (void) sd_netlink_message_read_u16(m
, IFLA_GENEVE_PORT
, &info
->tunnel_port
);
558 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_LABEL
, &info
->label
);
559 } else if (STR_IN_SET(info
->netdev_kind
, "gre", "gretap", "erspan")) {
560 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in
);
561 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in
);
562 } else if (STR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan")) {
563 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in6
);
564 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in6
);
565 } else if (streq(info
->netdev_kind
, "vti")) {
566 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in
);
567 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in
);
568 } else if (streq(info
->netdev_kind
, "vti6")) {
569 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in6
);
570 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in6
);
571 } else if (STR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap"))
572 (void) sd_netlink_message_read_u32(m
, IFLA_MACVLAN_MODE
, &info
->macvlan_mode
);
573 else if (streq(info
->netdev_kind
, "ipvlan")) {
574 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_MODE
, &info
->ipvlan_mode
);
575 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_FLAGS
, &info
->ipvlan_flags
);
578 (void) sd_netlink_message_exit_container(m
);
579 (void) sd_netlink_message_exit_container(m
);
584 static int decode_link(
585 sd_netlink_message
*m
,
587 char * const *patterns
,
588 bool matched_patterns
[]) {
590 _cleanup_strv_free_
char **altnames
= NULL
;
591 const char *name
, *qdisc
;
598 r
= sd_netlink_message_get_type(m
, &type
);
602 if (type
!= RTM_NEWLINK
)
605 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
609 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
613 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &altnames
);
614 if (r
< 0 && r
!= -ENODATA
)
618 char str
[DECIMAL_STR_MAX(int)];
621 assert(matched_patterns
);
623 xsprintf(str
, "%i", ifindex
);
624 if (!strv_fnmatch_full(patterns
, str
, 0, &pos
) &&
625 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
628 STRV_FOREACH(p
, altnames
)
629 if (strv_fnmatch_full(patterns
, *p
, 0, &pos
)) {
637 matched_patterns
[pos
] = true;
640 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
644 strscpy(info
->name
, sizeof info
->name
, name
);
645 info
->ifindex
= ifindex
;
646 info
->alternative_names
= TAKE_PTR(altnames
);
648 info
->has_hw_address
=
649 netlink_message_read_hw_addr(m
, IFLA_ADDRESS
, &info
->hw_address
) >= 0 &&
650 info
->hw_address
.length
> 0;
652 info
->has_permanent_hw_address
=
653 (netlink_message_read_hw_addr(m
, IFLA_PERM_ADDRESS
, &info
->permanent_hw_address
) >= 0 ||
654 ethtool_get_permanent_hw_addr(NULL
, info
->name
, &info
->permanent_hw_address
) >= 0) &&
655 !hw_addr_is_null(&info
->permanent_hw_address
) &&
656 !hw_addr_equal(&info
->permanent_hw_address
, &info
->hw_address
);
658 (void) sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
);
659 (void) sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
);
660 (void) sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
);
662 info
->has_rx_queues
=
663 sd_netlink_message_read_u32(m
, IFLA_NUM_RX_QUEUES
, &info
->rx_queues
) >= 0 &&
666 info
->has_tx_queues
=
667 sd_netlink_message_read_u32(m
, IFLA_NUM_TX_QUEUES
, &info
->tx_queues
) >= 0 &&
670 if (sd_netlink_message_read(m
, IFLA_STATS64
, sizeof info
->stats64
, &info
->stats64
) >= 0)
671 info
->has_stats64
= true;
672 else if (sd_netlink_message_read(m
, IFLA_STATS
, sizeof info
->stats
, &info
->stats
) >= 0)
673 info
->has_stats
= true;
675 r
= sd_netlink_message_read_string(m
, IFLA_QDISC
, &qdisc
);
677 info
->qdisc
= strdup(qdisc
);
682 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, &info
->master
);
684 r
= sd_netlink_message_enter_container(m
, IFLA_AF_SPEC
);
686 r
= sd_netlink_message_enter_container(m
, AF_INET6
);
688 r
= sd_netlink_message_read_u8(m
, IFLA_INET6_ADDR_GEN_MODE
, &info
->addr_gen_mode
);
689 if (r
>= 0 && IN_SET(info
->addr_gen_mode
,
690 IN6_ADDR_GEN_MODE_EUI64
,
691 IN6_ADDR_GEN_MODE_NONE
,
692 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
,
693 IN6_ADDR_GEN_MODE_RANDOM
))
694 info
->has_ipv6_address_generation_mode
= true;
696 (void) sd_netlink_message_exit_container(m
);
698 (void) sd_netlink_message_exit_container(m
);
702 (void) decode_netdev(m
, info
);
707 static int link_get_property(
709 const LinkInfo
*link
,
711 sd_bus_message
**reply
,
713 const char *propname
,
716 _cleanup_free_
char *path
= NULL
;
717 char ifindex_str
[DECIMAL_STR_MAX(int)];
722 assert(link
->ifindex
>= 0);
729 xsprintf(ifindex_str
, "%i", link
->ifindex
);
731 r
= sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str
, &path
);
735 return sd_bus_get_property(bus
, "org.freedesktop.network1", path
, iface
, propname
, error
, reply
, type
);
738 static int acquire_link_bitrates(sd_bus
*bus
, LinkInfo
*link
) {
739 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
740 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
746 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.Link", "BitRates", "(tt)");
748 bool quiet
= sd_bus_error_has_names(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
,
749 BUS_ERROR_SPEED_METER_INACTIVE
);
751 return log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
752 r
, "Failed to query link bit rates: %s", bus_error_message(&error
, r
));
755 r
= sd_bus_message_read(reply
, "(tt)", &link
->tx_bitrate
, &link
->rx_bitrate
);
757 return bus_log_parse_error(r
);
759 r
= sd_bus_message_exit_container(reply
);
761 return bus_log_parse_error(r
);
763 link
->has_bitrates
= link
->tx_bitrate
!= UINT64_MAX
&& link
->rx_bitrate
!= UINT64_MAX
;
768 static void acquire_ether_link_info(int *fd
, LinkInfo
*link
) {
772 if (ethtool_get_link_info(fd
,
774 &link
->autonegotiation
,
778 link
->has_ethtool_link_info
= true;
781 static void acquire_wlan_link_info(LinkInfo
*link
) {
782 _cleanup_(sd_netlink_unrefp
) sd_netlink
*genl
= NULL
;
783 const char *type
= NULL
;
789 (void) sd_device_get_devtype(link
->sd_device
, &type
);
790 if (!streq_ptr(type
, "wlan"))
793 r
= sd_genl_socket_open(&genl
);
795 log_debug_errno(r
, "Failed to open generic netlink socket: %m");
799 (void) sd_netlink_increase_rxbuf(genl
, RCVBUF_SIZE
);
801 r
= wifi_get_interface(genl
, link
->ifindex
, &link
->wlan_iftype
, &link
->ssid
);
803 log_debug_errno(r
, "%s: failed to query ssid: %m", link
->name
);
805 if (link
->wlan_iftype
== NL80211_IFTYPE_STATION
) {
806 k
= wifi_get_station(genl
, link
->ifindex
, &link
->bssid
);
808 log_debug_errno(k
, "%s: failed to query bssid: %m", link
->name
);
811 link
->has_wlan_link_info
= r
> 0 || k
> 0;
814 static int acquire_link_info(sd_bus
*bus
, sd_netlink
*rtnl
, char * const *patterns
, LinkInfo
**ret
) {
815 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
816 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
817 _cleanup_free_
bool *matched_patterns
= NULL
;
818 _cleanup_close_
int fd
= -EBADF
;
825 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
827 return rtnl_log_create_error(r
);
829 r
= sd_netlink_message_set_request_dump(req
, true);
831 return rtnl_log_create_error(r
);
833 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
835 return log_error_errno(r
, "Failed to enumerate links: %m");
838 matched_patterns
= new0(bool, strv_length(patterns
));
839 if (!matched_patterns
)
843 for (sd_netlink_message
*i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
844 if (!GREEDY_REALLOC0(links
, c
+ 2)) /* We keep one trailing one as marker */
847 r
= decode_link(i
, links
+ c
, patterns
, matched_patterns
);
853 links
[c
].needs_freeing
= true;
855 (void) sd_device_new_from_ifindex(&links
[c
].sd_device
, links
[c
].ifindex
);
857 acquire_ether_link_info(&fd
, &links
[c
]);
858 acquire_wlan_link_info(&links
[c
]);
863 /* Look if we matched all our arguments that are not globs. It
864 * is OK for a glob to match nothing, but not for an exact argument. */
865 for (size_t pos
= 0; pos
< strv_length(patterns
); pos
++) {
866 if (matched_patterns
[pos
])
869 if (string_is_glob(patterns
[pos
]))
870 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
873 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
874 "Interface \"%s\" not found.", patterns
[pos
]);
877 typesafe_qsort(links
, c
, link_info_compare
);
880 FOREACH_ARRAY(link
, links
, c
)
881 (void) acquire_link_bitrates(bus
, link
);
883 *ret
= TAKE_PTR(links
);
885 if (patterns
&& c
== 0)
886 log_warning("No interfaces matched.");
891 static int list_links(int argc
, char *argv
[], void *userdata
) {
892 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
893 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
894 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
895 _cleanup_(table_unrefp
) Table
*table
= NULL
;
899 r
= acquire_bus(&bus
);
903 if (arg_json_format_flags
!= JSON_FORMAT_OFF
) {
904 if (arg_all
|| argc
<= 1)
905 return dump_manager_description(bus
);
907 return dump_link_description(bus
, strv_skip(argv
, 1));
910 r
= sd_netlink_open(&rtnl
);
912 return log_error_errno(r
, "Failed to connect to netlink: %m");
914 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
918 pager_open(arg_pager_flags
);
920 table
= table_new("idx", "link", "type", "operational", "setup");
925 table_set_width(table
, 0);
927 table_set_header(table
, arg_legend
);
928 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
930 assert_se(cell
= table_get_cell(table
, 0, 0));
931 (void) table_set_minimum_width(table
, cell
, 3);
932 (void) table_set_weight(table
, cell
, 0);
933 (void) table_set_ellipsize_percent(table
, cell
, 100);
934 (void) table_set_align_percent(table
, cell
, 100);
936 assert_se(cell
= table_get_cell(table
, 0, 1));
937 (void) table_set_ellipsize_percent(table
, cell
, 100);
939 FOREACH_ARRAY(link
, links
, c
) {
940 _cleanup_free_
char *setup_state
= NULL
, *operational_state
= NULL
;
941 _cleanup_free_
char *t
= NULL
;
942 const char *on_color_operational
, *on_color_setup
;
944 (void) sd_network_link_get_operational_state(link
->ifindex
, &operational_state
);
945 operational_state_to_color(link
->name
, operational_state
, &on_color_operational
, NULL
);
947 (void) sd_network_link_get_setup_state(link
->ifindex
, &setup_state
);
948 setup_state_to_color(setup_state
, &on_color_setup
, NULL
);
950 r
= net_get_type_string(link
->sd_device
, link
->iftype
, &t
);
954 r
= table_add_many(table
,
955 TABLE_INT
, link
->ifindex
,
956 TABLE_STRING
, link
->name
,
958 TABLE_STRING
, operational_state
,
959 TABLE_SET_COLOR
, on_color_operational
,
960 TABLE_STRING
, setup_state
?: "unmanaged",
961 TABLE_SET_COLOR
, on_color_setup
);
963 return table_log_add_error(r
);
966 r
= table_print(table
, NULL
);
968 return table_log_print_error(r
);
971 printf("\n%i links listed.\n", c
);
976 /* IEEE Organizationally Unique Identifier vendor string */
977 static int ieee_oui(sd_hwdb
*hwdb
, const struct ether_addr
*mac
, char **ret
) {
978 _cleanup_free_
char *desc
= NULL
;
979 const char *description
;
980 char modalias
[STRLEN("OUI:XXYYXXYYXXYY") + 1];
988 /* skip commonly misused 00:00:00 (Xerox) prefix */
989 if (memcmp(mac
, "\0\0\0", 3) == 0)
992 xsprintf(modalias
, "OUI:" ETHER_ADDR_FORMAT_STR
, ETHER_ADDR_FORMAT_VAL(*mac
));
994 r
= sd_hwdb_get(hwdb
, modalias
, "ID_OUI_FROM_DATABASE", &description
);
998 desc
= strdup(description
);
1002 *ret
= TAKE_PTR(desc
);
1007 static int get_gateway_description(
1012 union in_addr_union
*gateway
,
1015 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1019 assert(ifindex
>= 0);
1020 assert(IN_SET(family
, AF_INET
, AF_INET6
));
1024 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_GETNEIGH
, ifindex
, family
);
1028 r
= sd_netlink_message_set_request_dump(req
, true);
1032 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1036 for (sd_netlink_message
*m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
1037 union in_addr_union gw
= IN_ADDR_NULL
;
1038 struct ether_addr mac
= ETHER_ADDR_NULL
;
1042 r
= sd_netlink_message_get_errno(m
);
1044 log_error_errno(r
, "Failed to get netlink message, ignoring: %m");
1048 r
= sd_netlink_message_get_type(m
, &type
);
1050 log_error_errno(r
, "Failed to get netlink message type, ignoring: %m");
1054 if (type
!= RTM_NEWNEIGH
) {
1055 log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1056 "Got unexpected netlink message type %u, ignoring",
1061 r
= sd_rtnl_message_neigh_get_family(m
, &fam
);
1063 log_error_errno(r
, "Failed to get rtnl family, ignoring: %m");
1067 if (fam
!= family
) {
1068 log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Got invalid rtnl family %d, ignoring", fam
);
1072 r
= sd_rtnl_message_neigh_get_ifindex(m
, &ifi
);
1074 log_error_errno(r
, "Failed to get rtnl ifindex, ignoring: %m");
1078 if (ifindex
> 0 && ifi
!= ifindex
)
1084 r
= sd_netlink_message_read_in_addr(m
, NDA_DST
, &gw
.in
);
1091 r
= sd_netlink_message_read_in6_addr(m
, NDA_DST
, &gw
.in6
);
1101 if (!in_addr_equal(fam
, &gw
, gateway
))
1104 r
= sd_netlink_message_read(m
, NDA_LLADDR
, sizeof(mac
), &mac
);
1108 r
= ieee_oui(hwdb
, &mac
, ret
);
1118 static int dump_list(Table
*table
, const char *key
, char * const *l
) {
1124 if (strv_isempty(l
))
1127 r
= table_add_many(table
,
1131 return table_log_add_error(r
);
1136 static int dump_gateways(sd_netlink
*rtnl
, sd_hwdb
*hwdb
, Table
*table
, int ifindex
) {
1137 _cleanup_free_
struct local_address
*local_addrs
= NULL
;
1138 _cleanup_strv_free_
char **buf
= NULL
;
1144 n
= local_gateways(rtnl
, ifindex
, AF_UNSPEC
, &local_addrs
);
1148 FOREACH_ARRAY(local
, local_addrs
, n
) {
1149 _cleanup_free_
char *description
= NULL
;
1151 r
= get_gateway_description(rtnl
, hwdb
, local
->ifindex
, local
->family
, &local
->address
, &description
);
1153 log_debug_errno(r
, "Could not get description of gateway, ignoring: %m");
1155 /* Show interface name for the entry if we show entries for all interfaces */
1156 r
= strv_extendf(&buf
, "%s%s%s%s%s%s",
1157 IN_ADDR_TO_STRING(local
->family
, &local
->address
),
1158 description
? " (" : "",
1159 strempty(description
),
1160 description
? ")" : "",
1161 ifindex
<= 0 ? " on " : "",
1162 ifindex
<= 0 ? FORMAT_IFNAME_FULL(local
->ifindex
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
) : "");
1167 return dump_list(table
, "Gateway", buf
);
1170 static int dump_addresses(
1172 sd_dhcp_lease
*lease
,
1176 _cleanup_free_
struct local_address
*local_addrs
= NULL
;
1177 _cleanup_strv_free_
char **buf
= NULL
;
1178 struct in_addr dhcp4_address
= {};
1184 n
= local_addresses(rtnl
, ifindex
, AF_UNSPEC
, &local_addrs
);
1189 (void) sd_dhcp_lease_get_address(lease
, &dhcp4_address
);
1191 FOREACH_ARRAY(local
, local_addrs
, n
) {
1192 struct in_addr server_address
;
1195 if (local
->family
== AF_INET
&& in4_addr_equal(&local
->address
.in
, &dhcp4_address
))
1196 dhcp4
= sd_dhcp_lease_get_server_identifier(lease
, &server_address
) >= 0;
1198 r
= strv_extendf(&buf
, "%s%s%s%s%s%s",
1199 IN_ADDR_TO_STRING(local
->family
, &local
->address
),
1200 dhcp4
? " (DHCP4 via " : "",
1201 dhcp4
? IN4_ADDR_TO_STRING(&server_address
) : "",
1203 ifindex
<= 0 ? " on " : "",
1204 ifindex
<= 0 ? FORMAT_IFNAME_FULL(local
->ifindex
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
) : "");
1209 return dump_list(table
, "Address", buf
);
1212 static int dump_address_labels(sd_netlink
*rtnl
) {
1213 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1214 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1220 r
= sd_rtnl_message_new_addrlabel(rtnl
, &req
, RTM_GETADDRLABEL
, 0, AF_INET6
);
1222 return log_error_errno(r
, "Could not allocate RTM_GETADDRLABEL message: %m");
1224 r
= sd_netlink_message_set_request_dump(req
, true);
1228 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1232 table
= table_new("label", "prefix/prefixlen");
1237 table_set_width(table
, 0);
1239 r
= table_set_sort(table
, (size_t) 0);
1243 assert_se(cell
= table_get_cell(table
, 0, 0));
1244 (void) table_set_align_percent(table
, cell
, 100);
1245 (void) table_set_ellipsize_percent(table
, cell
, 100);
1247 assert_se(cell
= table_get_cell(table
, 0, 1));
1248 (void) table_set_align_percent(table
, cell
, 100);
1250 for (sd_netlink_message
*m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
1251 struct in6_addr prefix
;
1255 r
= sd_netlink_message_get_errno(m
);
1257 log_error_errno(r
, "Failed to get netlink message, ignoring: %m");
1261 r
= sd_netlink_message_read_u32(m
, IFAL_LABEL
, &label
);
1262 if (r
< 0 && r
!= -ENODATA
) {
1263 log_error_errno(r
, "Could not read IFAL_LABEL, ignoring: %m");
1267 r
= sd_netlink_message_read_in6_addr(m
, IFAL_ADDRESS
, &prefix
);
1271 r
= sd_rtnl_message_addrlabel_get_prefixlen(m
, &prefixlen
);
1275 r
= table_add_cell(table
, NULL
, TABLE_UINT32
, &label
);
1277 return table_log_add_error(r
);
1279 r
= table_add_cell_stringf(table
, NULL
, "%s/%u", IN6_ADDR_TO_STRING(&prefix
), prefixlen
);
1281 return table_log_add_error(r
);
1284 r
= table_print(table
, NULL
);
1286 return table_log_print_error(r
);
1291 static int list_address_labels(int argc
, char *argv
[], void *userdata
) {
1292 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1295 r
= sd_netlink_open(&rtnl
);
1297 return log_error_errno(r
, "Failed to connect to netlink: %m");
1299 return dump_address_labels(rtnl
);
1302 static int open_lldp_neighbors(int ifindex
, FILE **ret
) {
1303 _cleanup_fclose_
FILE *f
= NULL
;
1304 char p
[STRLEN("/run/systemd/netif/lldp/") + DECIMAL_STR_MAX(int)];
1306 assert(ifindex
>= 0);
1309 xsprintf(p
, "/run/systemd/netif/lldp/%i", ifindex
);
1319 static int next_lldp_neighbor(FILE *f
, sd_lldp_neighbor
**ret
) {
1320 _cleanup_free_
void *raw
= NULL
;
1328 l
= fread(&u
, 1, sizeof(u
), f
);
1329 if (l
== 0 && feof(f
))
1334 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
1335 if (le64toh(u
) >= 4096)
1338 raw
= new(uint8_t, le64toh(u
));
1342 if (fread(raw
, 1, le64toh(u
), f
) != le64toh(u
))
1345 r
= sd_lldp_neighbor_from_raw(ret
, raw
, le64toh(u
));
1352 static int dump_lldp_neighbors(Table
*table
, const char *prefix
, int ifindex
) {
1353 _cleanup_strv_free_
char **buf
= NULL
;
1354 _cleanup_fclose_
FILE *f
= NULL
;
1359 assert(ifindex
> 0);
1361 r
= open_lldp_neighbors(ifindex
, &f
);
1368 const char *system_name
= NULL
, *port_id
= NULL
, *port_description
= NULL
;
1369 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
1371 r
= next_lldp_neighbor(f
, &n
);
1377 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
1378 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
1379 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
1381 r
= strv_extendf(&buf
, "%s on port %s%s%s%s",
1384 isempty(port_description
) ? "" : " (",
1385 strempty(port_description
),
1386 isempty(port_description
) ? "" : ")");
1391 return dump_list(table
, prefix
, buf
);
1394 static int dump_dhcp_leases(Table
*table
, const char *prefix
, sd_bus
*bus
, const LinkInfo
*link
) {
1395 _cleanup_strv_free_
char **buf
= NULL
;
1396 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1397 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1405 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.DHCPServer", "Leases", "a(uayayayayt)");
1407 bool quiet
= sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
);
1409 log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
1410 r
, "Failed to query link DHCP leases: %s", bus_error_message(&error
, r
));
1414 r
= sd_bus_message_enter_container(reply
, 'a', "(uayayayayt)");
1416 return bus_log_parse_error(r
);
1418 while ((r
= sd_bus_message_enter_container(reply
, 'r', "uayayayayt")) > 0) {
1419 _cleanup_free_
char *id
= NULL
, *ip
= NULL
;
1420 const void *client_id
, *addr
, *gtw
, *hwaddr
;
1421 size_t client_id_sz
, sz
;
1422 uint64_t expiration
;
1425 r
= sd_bus_message_read(reply
, "u", &family
);
1427 return bus_log_parse_error(r
);
1429 r
= sd_bus_message_read_array(reply
, 'y', &client_id
, &client_id_sz
);
1431 return bus_log_parse_error(r
);
1433 r
= sd_bus_message_read_array(reply
, 'y', &addr
, &sz
);
1434 if (r
< 0 || sz
!= 4)
1435 return bus_log_parse_error(r
);
1437 r
= sd_bus_message_read_array(reply
, 'y', >w
, &sz
);
1438 if (r
< 0 || sz
!= 4)
1439 return bus_log_parse_error(r
);
1441 r
= sd_bus_message_read_array(reply
, 'y', &hwaddr
, &sz
);
1443 return bus_log_parse_error(r
);
1445 r
= sd_bus_message_read_basic(reply
, 't', &expiration
);
1447 return bus_log_parse_error(r
);
1449 r
= sd_dhcp_client_id_to_string(client_id
, client_id_sz
, &id
);
1451 return bus_log_parse_error(r
);
1453 r
= in_addr_to_string(family
, addr
, &ip
);
1455 return bus_log_parse_error(r
);
1457 r
= strv_extendf(&buf
, "%s (to %s)", ip
, id
);
1461 r
= sd_bus_message_exit_container(reply
);
1463 return bus_log_parse_error(r
);
1466 return bus_log_parse_error(r
);
1468 r
= sd_bus_message_exit_container(reply
);
1470 return bus_log_parse_error(r
);
1472 r
= sd_bus_message_exit_container(reply
);
1474 return bus_log_parse_error(r
);
1476 if (strv_isempty(buf
)) {
1477 r
= strv_extendf(&buf
, "none");
1482 return dump_list(table
, prefix
, buf
);
1485 static int dump_ifindexes(Table
*table
, const char *prefix
, const int *ifindexes
) {
1494 for (unsigned c
= 0; ifindexes
[c
] > 0; c
++) {
1496 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, prefix
);
1498 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1500 return table_log_add_error(r
);
1502 r
= table_add_cell(table
, NULL
, TABLE_IFINDEX
, &ifindexes
[c
]);
1504 return table_log_add_error(r
);
1510 #define DUMP_STATS_ONE(name, val_name) \
1512 r = table_add_cell(table, NULL, TABLE_FIELD, name); \
1514 return table_log_add_error(r); \
1515 r = table_add_cell(table, NULL, \
1516 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
1517 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
1519 return table_log_add_error(r); \
1522 static int dump_statistics(Table
*table
, const LinkInfo
*info
) {
1531 if (!info
->has_stats64
&& !info
->has_stats
)
1534 DUMP_STATS_ONE("Rx Packets", rx_packets
);
1535 DUMP_STATS_ONE("Tx Packets", tx_packets
);
1536 DUMP_STATS_ONE("Rx Bytes", rx_bytes
);
1537 DUMP_STATS_ONE("Tx Bytes", tx_bytes
);
1538 DUMP_STATS_ONE("Rx Errors", rx_errors
);
1539 DUMP_STATS_ONE("Tx Errors", tx_errors
);
1540 DUMP_STATS_ONE("Rx Dropped", rx_dropped
);
1541 DUMP_STATS_ONE("Tx Dropped", tx_dropped
);
1542 DUMP_STATS_ONE("Multicast Packets", multicast
);
1543 DUMP_STATS_ONE("Collisions", collisions
);
1548 static int dump_hw_address(Table
*table
, sd_hwdb
*hwdb
, const char *field
, const struct hw_addr_data
*addr
) {
1549 _cleanup_free_
char *description
= NULL
;
1556 if (addr
->length
== ETH_ALEN
)
1557 (void) ieee_oui(hwdb
, &addr
->ether
, &description
);
1559 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, field
);
1561 return table_log_add_error(r
);
1563 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s%s",
1564 HW_ADDR_TO_STR(addr
),
1565 description
? " (" : "",
1566 strempty(description
),
1567 description
? ")" : "");
1569 return table_log_add_error(r
);
1574 static OutputFlags
get_output_flags(void) {
1576 arg_all
* OUTPUT_SHOW_ALL
|
1577 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
1578 colors_enabled() * OUTPUT_COLOR
;
1581 static int show_logs(const LinkInfo
*info
) {
1582 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1588 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1590 return log_error_errno(r
, "Failed to open journal: %m");
1592 r
= add_match_this_boot(j
, NULL
);
1594 return log_error_errno(r
, "Failed to add boot matches: %m");
1597 char m1
[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)];
1598 const char *m2
, *m3
;
1601 xsprintf(m1
, "_KERNEL_DEVICE=n%i", info
->ifindex
);
1603 m2
= strjoina("INTERFACE=", info
->name
);
1605 m3
= strjoina("DEVICE=", info
->name
);
1608 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1609 (r
= sd_journal_add_disjunction(j
)) ||
1610 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1611 (r
= sd_journal_add_disjunction(j
)) ||
1612 (r
= sd_journal_add_match(j
, m3
, 0))
1615 return log_error_errno(r
, "Failed to add link matches: %m");
1617 r
= add_matches_for_unit(j
, "systemd-networkd.service");
1619 return log_error_errno(r
, "Failed to add unit matches: %m");
1621 r
= add_matches_for_unit(j
, "systemd-networkd-wait-online.service");
1623 return log_error_errno(r
, "Failed to add unit matches: %m");
1626 return show_journal(
1633 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
1637 static int table_add_string_line(Table
*table
, const char *key
, const char *value
) {
1646 r
= table_add_many(table
,
1648 TABLE_STRING
, value
);
1650 return table_log_add_error(r
);
1655 static int format_dropins(char **dropins
) {
1656 STRV_FOREACH(d
, dropins
) {
1657 _cleanup_free_
char *s
= NULL
;
1658 int glyph
= *(d
+ 1) == NULL
? SPECIAL_GLYPH_TREE_RIGHT
: SPECIAL_GLYPH_TREE_BRANCH
;
1660 s
= strjoin(special_glyph(glyph
), *d
);
1664 free_and_replace(*d
, s
);
1670 static int link_status_one(
1674 const LinkInfo
*info
) {
1676 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **sip
= NULL
, **search_domains
= NULL
,
1677 **route_domains
= NULL
, **link_dropins
= NULL
, **network_dropins
= NULL
;
1678 _cleanup_free_
char *t
= NULL
, *network
= NULL
, *iaid
= NULL
, *duid
= NULL
, *captive_portal
= NULL
,
1679 *setup_state
= NULL
, *operational_state
= NULL
, *online_state
= NULL
, *activation_policy
= NULL
;
1680 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
,
1681 *on_color_operational
, *off_color_operational
, *on_color_setup
, *off_color_setup
, *on_color_online
;
1682 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
1683 _cleanup_(sd_dhcp_lease_unrefp
) sd_dhcp_lease
*lease
= NULL
;
1684 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1691 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
1692 operational_state_to_color(info
->name
, operational_state
, &on_color_operational
, &off_color_operational
);
1694 (void) sd_network_link_get_online_state(info
->ifindex
, &online_state
);
1695 online_state_to_color(online_state
, &on_color_online
, NULL
);
1697 (void) sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
1698 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
1700 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
1701 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
1702 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
1703 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
1704 (void) sd_network_link_get_sip(info
->ifindex
, &sip
);
1705 (void) sd_network_link_get_captive_portal(info
->ifindex
, &captive_portal
);
1706 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
1707 (void) sd_network_link_get_network_file_dropins(info
->ifindex
, &network_dropins
);
1708 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
1709 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
1710 (void) sd_network_link_get_activation_policy(info
->ifindex
, &activation_policy
);
1712 if (info
->sd_device
) {
1715 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_LINK_FILE", &link
);
1717 if (sd_device_get_property_value(info
->sd_device
, "ID_NET_LINK_FILE_DROPINS", &joined
) >= 0) {
1718 r
= strv_split_full(&link_dropins
, joined
, ":", EXTRACT_CUNESCAPE
);
1723 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_DRIVER", &driver
);
1724 (void) sd_device_get_property_value(info
->sd_device
, "ID_PATH", &path
);
1725 (void) device_get_vendor_string(info
->sd_device
, &vendor
);
1726 (void) device_get_model_string(info
->sd_device
, &model
);
1729 r
= net_get_type_string(info
->sd_device
, info
->iftype
, &t
);
1733 char lease_file
[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)];
1734 xsprintf(lease_file
, "/run/systemd/netif/leases/%i", info
->ifindex
);
1736 (void) dhcp_lease_load(&lease
, lease_file
);
1738 r
= format_dropins(network_dropins
);
1742 if (strv_prepend(&network_dropins
, network
) < 0)
1745 r
= format_dropins(link_dropins
);
1749 if (strv_prepend(&link_dropins
, link
) < 0)
1752 table
= table_new_vertical();
1757 table_set_width(table
, 0);
1759 /* unit files and basic states. */
1760 r
= table_add_many(table
,
1761 TABLE_FIELD
, "Link File",
1762 TABLE_STRV
, link_dropins
?: STRV_MAKE("n/a"),
1763 TABLE_FIELD
, "Network File",
1764 TABLE_STRV
, network_dropins
?: STRV_MAKE("n/a"),
1765 TABLE_FIELD
, "State");
1767 return table_log_add_error(r
);
1769 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s (%s%s%s)",
1770 on_color_operational
, strna(operational_state
), off_color_operational
,
1771 on_color_setup
, setup_state
?: "unmanaged", off_color_setup
);
1773 return table_log_add_error(r
);
1775 r
= table_add_many(table
,
1776 TABLE_FIELD
, "Online state",
1777 TABLE_STRING
, online_state
?: "unknown",
1778 TABLE_SET_COLOR
, on_color_online
);
1780 return table_log_add_error(r
);
1782 r
= table_add_string_line(table
, "Type", t
);
1786 r
= table_add_string_line(table
, "Kind", info
->netdev_kind
);
1790 r
= table_add_string_line(table
, "Path", path
);
1794 r
= table_add_string_line(table
, "Driver", driver
);
1798 r
= table_add_string_line(table
, "Vendor", vendor
);
1802 r
= table_add_string_line(table
, "Model", model
);
1806 strv_sort(info
->alternative_names
);
1807 r
= dump_list(table
, "Alternative Names", info
->alternative_names
);
1811 if (info
->has_hw_address
) {
1812 r
= dump_hw_address(table
, hwdb
, "Hardware Address", &info
->hw_address
);
1817 if (info
->has_permanent_hw_address
) {
1818 r
= dump_hw_address(table
, hwdb
, "Permanent Hardware Address", &info
->permanent_hw_address
);
1823 if (info
->mtu
> 0) {
1824 char min_str
[DECIMAL_STR_MAX(uint32_t)], max_str
[DECIMAL_STR_MAX(uint32_t)];
1826 xsprintf(min_str
, "%" PRIu32
, info
->min_mtu
);
1827 xsprintf(max_str
, "%" PRIu32
, info
->max_mtu
);
1829 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "MTU");
1831 return table_log_add_error(r
);
1833 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"%s%s%s%s%s%s%s",
1835 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? " (" : "",
1836 info
->min_mtu
> 0 ? "min: " : "",
1837 info
->min_mtu
> 0 ? min_str
: "",
1838 info
->min_mtu
> 0 && info
->max_mtu
> 0 ? ", " : "",
1839 info
->max_mtu
> 0 ? "max: " : "",
1840 info
->max_mtu
> 0 ? max_str
: "",
1841 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? ")" : "");
1843 return table_log_add_error(r
);
1846 r
= table_add_string_line(table
, "QDisc", info
->qdisc
);
1850 if (info
->master
> 0) {
1851 r
= table_add_many(table
,
1852 TABLE_FIELD
, "Master",
1853 TABLE_IFINDEX
, info
->master
);
1855 return table_log_add_error(r
);
1858 if (info
->has_ipv6_address_generation_mode
) {
1859 static const struct {
1864 { "stable-privacy" },
1868 r
= table_add_many(table
,
1869 TABLE_FIELD
, "IPv6 Address Generation Mode",
1870 TABLE_STRING
, mode_table
[info
->addr_gen_mode
]);
1872 return table_log_add_error(r
);
1875 if (streq_ptr(info
->netdev_kind
, "bridge")) {
1876 r
= table_add_many(table
,
1877 TABLE_FIELD
, "Forward Delay",
1878 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->forward_delay
),
1879 TABLE_FIELD
, "Hello Time",
1880 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->hello_time
),
1881 TABLE_FIELD
, "Max Age",
1882 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->max_age
),
1883 TABLE_FIELD
, "Ageing Time",
1884 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->ageing_time
),
1885 TABLE_FIELD
, "Priority",
1886 TABLE_UINT16
, info
->priority
,
1888 TABLE_BOOLEAN
, info
->stp_state
> 0,
1889 TABLE_FIELD
, "Multicast IGMP Version",
1890 TABLE_UINT8
, info
->mcast_igmp_version
,
1891 TABLE_FIELD
, "Cost",
1892 TABLE_UINT32
, info
->cost
);
1894 return table_log_add_error(r
);
1896 if (info
->port_state
<= BR_STATE_BLOCKING
) {
1897 r
= table_add_many(table
,
1898 TABLE_FIELD
, "Port State",
1899 TABLE_STRING
, bridge_state_to_string(info
->port_state
));
1901 return table_log_add_error(r
);
1904 } else if (streq_ptr(info
->netdev_kind
, "bond")) {
1905 r
= table_add_many(table
,
1906 TABLE_FIELD
, "Mode",
1907 TABLE_STRING
, bond_mode_to_string(info
->mode
),
1908 TABLE_FIELD
, "Miimon",
1909 TABLE_TIMESPAN_MSEC
, info
->miimon
* USEC_PER_MSEC
,
1910 TABLE_FIELD
, "Updelay",
1911 TABLE_TIMESPAN_MSEC
, info
->updelay
* USEC_PER_MSEC
,
1912 TABLE_FIELD
, "Downdelay",
1913 TABLE_TIMESPAN_MSEC
, info
->downdelay
* USEC_PER_MSEC
);
1915 return table_log_add_error(r
);
1917 } else if (streq_ptr(info
->netdev_kind
, "vxlan")) {
1918 char ttl
[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1920 if (info
->vxlan_info
.vni
> 0) {
1921 r
= table_add_many(table
,
1923 TABLE_UINT32
, info
->vxlan_info
.vni
);
1925 return table_log_add_error(r
);
1928 if (IN_SET(info
->vxlan_info
.group_family
, AF_INET
, AF_INET6
)) {
1931 r
= in_addr_is_multicast(info
->vxlan_info
.group_family
, &info
->vxlan_info
.group
);
1937 r
= table_add_many(table
,
1939 info
->vxlan_info
.group_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
, &info
->vxlan_info
.group
);
1941 return table_log_add_error(r
);
1944 if (IN_SET(info
->vxlan_info
.local_family
, AF_INET
, AF_INET6
)) {
1945 r
= table_add_many(table
,
1946 TABLE_FIELD
, "Local",
1947 info
->vxlan_info
.local_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
, &info
->vxlan_info
.local
);
1949 return table_log_add_error(r
);
1952 if (info
->vxlan_info
.dest_port
> 0) {
1953 r
= table_add_many(table
,
1954 TABLE_FIELD
, "Destination Port",
1955 TABLE_UINT16
, be16toh(info
->vxlan_info
.dest_port
));
1957 return table_log_add_error(r
);
1960 if (info
->vxlan_info
.link
> 0) {
1961 r
= table_add_many(table
,
1962 TABLE_FIELD
, "Underlying Device",
1963 TABLE_IFINDEX
, info
->vxlan_info
.link
);
1965 return table_log_add_error(r
);
1968 r
= table_add_many(table
,
1969 TABLE_FIELD
, "Learning",
1970 TABLE_BOOLEAN
, info
->vxlan_info
.learning
,
1972 TABLE_BOOLEAN
, info
->vxlan_info
.rsc
,
1973 TABLE_FIELD
, "L3MISS",
1974 TABLE_BOOLEAN
, info
->vxlan_info
.l3miss
,
1975 TABLE_FIELD
, "L2MISS",
1976 TABLE_BOOLEAN
, info
->vxlan_info
.l2miss
);
1978 return table_log_add_error(r
);
1980 if (info
->vxlan_info
.tos
> 1) {
1981 r
= table_add_many(table
,
1983 TABLE_UINT8
, info
->vxlan_info
.tos
);
1985 return table_log_add_error(r
);
1988 if (info
->vxlan_info
.ttl
> 0)
1989 xsprintf(ttl
, "%" PRIu8
, info
->vxlan_info
.ttl
);
1991 strcpy(ttl
, "auto");
1993 r
= table_add_many(table
,
1997 return table_log_add_error(r
);
1999 } else if (streq_ptr(info
->netdev_kind
, "vlan") && info
->vlan_id
> 0) {
2000 r
= table_add_many(table
,
2001 TABLE_FIELD
, "VLan Id",
2002 TABLE_UINT16
, info
->vlan_id
);
2004 return table_log_add_error(r
);
2006 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
2007 if (in_addr_is_set(AF_INET
, &info
->local
)) {
2008 r
= table_add_many(table
,
2009 TABLE_FIELD
, "Local",
2010 TABLE_IN_ADDR
, &info
->local
);
2012 return table_log_add_error(r
);
2015 if (in_addr_is_set(AF_INET
, &info
->remote
)) {
2016 r
= table_add_many(table
,
2017 TABLE_FIELD
, "Remote",
2018 TABLE_IN_ADDR
, &info
->remote
);
2020 return table_log_add_error(r
);
2023 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
2024 if (in_addr_is_set(AF_INET6
, &info
->local
)) {
2025 r
= table_add_many(table
,
2026 TABLE_FIELD
, "Local",
2027 TABLE_IN6_ADDR
, &info
->local
);
2029 return table_log_add_error(r
);
2032 if (in_addr_is_set(AF_INET6
, &info
->remote
)) {
2033 r
= table_add_many(table
,
2034 TABLE_FIELD
, "Remote",
2035 TABLE_IN6_ADDR
, &info
->remote
);
2037 return table_log_add_error(r
);
2040 } else if (streq_ptr(info
->netdev_kind
, "geneve")) {
2041 r
= table_add_many(table
,
2043 TABLE_UINT32
, info
->vni
);
2045 return table_log_add_error(r
);
2047 if (info
->has_tunnel_ipv4
&& in_addr_is_set(AF_INET
, &info
->remote
)) {
2048 r
= table_add_many(table
,
2049 TABLE_FIELD
, "Remote",
2050 TABLE_IN_ADDR
, &info
->remote
);
2052 return table_log_add_error(r
);
2053 } else if (in_addr_is_set(AF_INET6
, &info
->remote
)) {
2054 r
= table_add_many(table
,
2055 TABLE_FIELD
, "Remote",
2056 TABLE_IN6_ADDR
, &info
->remote
);
2058 return table_log_add_error(r
);
2061 if (info
->ttl
> 0) {
2062 r
= table_add_many(table
,
2064 TABLE_UINT8
, info
->ttl
);
2066 return table_log_add_error(r
);
2069 if (info
->tos
> 0) {
2070 r
= table_add_many(table
,
2072 TABLE_UINT8
, info
->tos
);
2074 return table_log_add_error(r
);
2077 r
= table_add_many(table
,
2078 TABLE_FIELD
, "Port",
2079 TABLE_UINT16
, info
->tunnel_port
,
2080 TABLE_FIELD
, "Inherit",
2081 TABLE_STRING
, geneve_df_to_string(info
->inherit
));
2083 return table_log_add_error(r
);
2086 r
= table_add_many(table
,
2087 TABLE_FIELD
, "IPDoNotFragment",
2088 TABLE_UINT8
, info
->df
);
2090 return table_log_add_error(r
);
2093 r
= table_add_many(table
,
2094 TABLE_FIELD
, "UDPChecksum",
2095 TABLE_BOOLEAN
, info
->csum
,
2096 TABLE_FIELD
, "UDP6ZeroChecksumTx",
2097 TABLE_BOOLEAN
, info
->csum6_tx
,
2098 TABLE_FIELD
, "UDP6ZeroChecksumRx",
2099 TABLE_BOOLEAN
, info
->csum6_rx
);
2101 return table_log_add_error(r
);
2103 if (info
->label
> 0) {
2104 r
= table_add_many(table
,
2105 TABLE_FIELD
, "FlowLabel",
2106 TABLE_UINT32
, info
->label
);
2108 return table_log_add_error(r
);
2111 } else if (STRPTR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap")) {
2112 r
= table_add_many(table
,
2113 TABLE_FIELD
, "Mode",
2114 TABLE_STRING
, macvlan_mode_to_string(info
->macvlan_mode
));
2116 return table_log_add_error(r
);
2118 } else if (streq_ptr(info
->netdev_kind
, "ipvlan")) {
2121 if (info
->ipvlan_flags
& IPVLAN_F_PRIVATE
)
2123 else if (info
->ipvlan_flags
& IPVLAN_F_VEPA
)
2128 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "Mode");
2130 return table_log_add_error(r
);
2132 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)",
2133 ipvlan_mode_to_string(info
->ipvlan_mode
), p
);
2135 return table_log_add_error(r
);
2138 if (info
->has_wlan_link_info
) {
2139 _cleanup_free_
char *esc
= NULL
;
2141 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "Wi-Fi access point");
2143 return table_log_add_error(r
);
2146 esc
= cescape(info
->ssid
);
2148 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)",
2150 ETHER_ADDR_TO_STR(&info
->bssid
));
2152 return table_log_add_error(r
);
2155 if (info
->has_bitrates
) {
2156 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "Bit Rate (Tx/Rx)");
2158 return table_log_add_error(r
);
2160 r
= table_add_cell_stringf(table
, NULL
, "%sbps/%sbps",
2161 FORMAT_BYTES_FULL(info
->tx_bitrate
, 0),
2162 FORMAT_BYTES_FULL(info
->rx_bitrate
, 0));
2164 return table_log_add_error(r
);
2167 if (info
->has_tx_queues
|| info
->has_rx_queues
) {
2168 r
= table_add_cell(table
, NULL
, TABLE_FIELD
, "Number of Queues (Tx/Rx)");
2170 return table_log_add_error(r
);
2172 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"/%" PRIu32
, info
->tx_queues
, info
->rx_queues
);
2174 return table_log_add_error(r
);
2177 if (info
->has_ethtool_link_info
) {
2178 if (IN_SET(info
->autonegotiation
, AUTONEG_DISABLE
, AUTONEG_ENABLE
)) {
2179 r
= table_add_many(table
,
2180 TABLE_FIELD
, "Auto negotiation",
2181 TABLE_BOOLEAN
, info
->autonegotiation
== AUTONEG_ENABLE
);
2183 return table_log_add_error(r
);
2186 if (info
->speed
> 0 && info
->speed
!= UINT64_MAX
) {
2187 r
= table_add_many(table
,
2188 TABLE_FIELD
, "Speed",
2189 TABLE_BPS
, info
->speed
);
2191 return table_log_add_error(r
);
2194 r
= table_add_string_line(table
, "Duplex", duplex_to_string(info
->duplex
));
2198 r
= table_add_string_line(table
, "Port", port_to_string(info
->port
));
2203 r
= dump_addresses(rtnl
, lease
, table
, info
->ifindex
);
2207 r
= dump_gateways(rtnl
, hwdb
, table
, info
->ifindex
);
2211 r
= dump_list(table
, "DNS", dns
);
2215 r
= dump_list(table
, "Search Domains", search_domains
);
2219 r
= dump_list(table
, "Route Domains", route_domains
);
2223 r
= dump_list(table
, "NTP", ntp
);
2227 r
= dump_list(table
, "SIP", sip
);
2231 r
= dump_ifindexes(table
, "Carrier Bound To", carrier_bound_to
);
2235 r
= dump_ifindexes(table
, "Carrier Bound By", carrier_bound_by
);
2239 r
= table_add_string_line(table
, "Activation Policy", activation_policy
);
2243 r
= sd_network_link_get_required_for_online(info
->ifindex
);
2245 r
= table_add_many(table
,
2246 TABLE_FIELD
, "Required For Online",
2249 return table_log_add_error(r
);
2252 if (captive_portal
) {
2253 r
= table_add_many(table
,
2254 TABLE_FIELD
, "Captive Portal",
2255 TABLE_STRING
, captive_portal
,
2256 TABLE_SET_URL
, captive_portal
);
2258 return table_log_add_error(r
);
2262 const void *client_id
;
2263 size_t client_id_len
;
2266 r
= sd_dhcp_lease_get_timezone(lease
, &tz
);
2268 r
= table_add_many(table
,
2269 TABLE_FIELD
, "Time Zone",
2272 return table_log_add_error(r
);
2275 r
= sd_dhcp_lease_get_client_id(lease
, &client_id
, &client_id_len
);
2277 _cleanup_free_
char *id
= NULL
;
2279 r
= sd_dhcp_client_id_to_string(client_id
, client_id_len
, &id
);
2281 r
= table_add_many(table
,
2282 TABLE_FIELD
, "DHCP4 Client ID",
2285 return table_log_add_error(r
);
2290 r
= sd_network_link_get_dhcp6_client_iaid_string(info
->ifindex
, &iaid
);
2292 r
= table_add_many(table
,
2293 TABLE_FIELD
, "DHCP6 Client IAID",
2294 TABLE_STRING
, iaid
);
2296 return table_log_add_error(r
);
2299 r
= sd_network_link_get_dhcp6_client_duid_string(info
->ifindex
, &duid
);
2301 r
= table_add_many(table
,
2302 TABLE_FIELD
, "DHCP6 Client DUID",
2303 TABLE_STRING
, duid
);
2305 return table_log_add_error(r
);
2308 r
= dump_lldp_neighbors(table
, "Connected To", info
->ifindex
);
2312 r
= dump_dhcp_leases(table
, "Offered DHCP leases", bus
, info
);
2316 r
= dump_statistics(table
, info
);
2320 /* First line: circle, ifindex, ifname. */
2321 printf("%s%s%s %d: %s\n",
2322 on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
,
2323 info
->ifindex
, info
->name
);
2325 r
= table_print(table
, NULL
);
2327 return table_log_print_error(r
);
2329 return show_logs(info
);
2332 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
2333 _cleanup_free_
char *operational_state
= NULL
, *online_state
= NULL
, *netifs_joined
= NULL
;
2334 _cleanup_strv_free_
char **netifs
= NULL
, **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
2335 const char *on_color_operational
, *off_color_operational
, *on_color_online
;
2336 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2341 (void) sd_network_get_operational_state(&operational_state
);
2342 operational_state_to_color(NULL
, operational_state
, &on_color_operational
, &off_color_operational
);
2344 (void) sd_network_get_online_state(&online_state
);
2345 online_state_to_color(online_state
, &on_color_online
, NULL
);
2347 table
= table_new_vertical();
2352 table_set_width(table
, 0);
2354 r
= get_files_in_directory("/run/systemd/netif/links/", &netifs
);
2355 if (r
< 0 && r
!= -ENOENT
)
2356 return log_error_errno(r
, "Failed to list network interfaces: %m");
2358 netifs_joined
= strv_join(netifs
, ", ");
2363 r
= table_add_many(table
,
2364 TABLE_FIELD
, "State",
2365 TABLE_STRING
, strna(operational_state
),
2366 TABLE_SET_COLOR
, on_color_operational
,
2367 TABLE_FIELD
, "Online state",
2368 TABLE_STRING
, online_state
?: "unknown",
2369 TABLE_SET_COLOR
, on_color_online
);
2371 return table_log_add_error(r
);
2373 r
= dump_addresses(rtnl
, NULL
, table
, 0);
2377 r
= dump_gateways(rtnl
, hwdb
, table
, 0);
2381 (void) sd_network_get_dns(&dns
);
2382 r
= dump_list(table
, "DNS", dns
);
2386 (void) sd_network_get_search_domains(&search_domains
);
2387 r
= dump_list(table
, "Search Domains", search_domains
);
2391 (void) sd_network_get_route_domains(&route_domains
);
2392 r
= dump_list(table
, "Route Domains", route_domains
);
2396 (void) sd_network_get_ntp(&ntp
);
2397 r
= dump_list(table
, "NTP", ntp
);
2401 printf("%s%s%s Interfaces: %s\n",
2402 on_color_operational
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
), off_color_operational
,
2403 strna(netifs_joined
));
2405 r
= table_print(table
, NULL
);
2407 return table_log_print_error(r
);
2409 return show_logs(NULL
);
2412 static int link_status(int argc
, char *argv
[], void *userdata
) {
2413 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2414 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2415 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
2416 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2419 r
= acquire_bus(&bus
);
2423 if (arg_json_format_flags
!= JSON_FORMAT_OFF
) {
2424 if (arg_all
|| argc
<= 1)
2425 return dump_manager_description(bus
);
2427 return dump_link_description(bus
, strv_skip(argv
, 1));
2430 pager_open(arg_pager_flags
);
2432 r
= sd_netlink_open(&rtnl
);
2434 return log_error_errno(r
, "Failed to connect to netlink: %m");
2436 r
= sd_hwdb_new(&hwdb
);
2438 log_debug_errno(r
, "Failed to open hardware database: %m");
2441 c
= acquire_link_info(bus
, rtnl
, NULL
, &links
);
2443 return system_status(rtnl
, hwdb
);
2445 c
= acquire_link_info(bus
, rtnl
, argv
+ 1, &links
);
2452 FOREACH_ARRAY(i
, links
, c
) {
2456 RET_GATHER(r
, link_status_one(bus
, rtnl
, hwdb
, i
));
2464 static char *lldp_capabilities_to_string(uint16_t x
) {
2465 static const char characters
[] = {
2466 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2471 ret
= new(char, ELEMENTSOF(characters
) + 1);
2475 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
2476 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
2482 static void lldp_capabilities_legend(uint16_t x
) {
2483 unsigned cols
= columns();
2484 static const char* const table
[] = {
2488 "w - WLAN Access Point",
2491 "d - DOCSIS cable device",
2493 "c - Customer VLAN",
2495 "m - Two-port MAC Relay (TPMR)",
2501 printf("\nCapability Flags:\n");
2502 for (unsigned w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
2503 if (x
& (1U << i
) || arg_all
) {
2506 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
2509 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
2514 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
2515 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2516 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2517 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2522 r
= sd_netlink_open(&rtnl
);
2524 return log_error_errno(r
, "Failed to connect to netlink: %m");
2526 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
2530 pager_open(arg_pager_flags
);
2532 table
= table_new("link",
2537 "port-description");
2542 table_set_width(table
, 0);
2544 table_set_header(table
, arg_legend
);
2546 assert_se(cell
= table_get_cell(table
, 0, 3));
2547 table_set_minimum_width(table
, cell
, 11);
2548 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
2550 FOREACH_ARRAY(link
, links
, c
) {
2551 _cleanup_fclose_
FILE *f
= NULL
;
2553 r
= open_lldp_neighbors(link
->ifindex
, &f
);
2557 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", link
->ifindex
);
2562 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
;
2563 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
2564 _cleanup_free_
char *capabilities
= NULL
;
2567 r
= next_lldp_neighbor(f
, &n
);
2569 log_warning_errno(r
, "Failed to read neighbor data: %m");
2575 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
2576 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
2577 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
2578 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
2580 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
2581 capabilities
= lldp_capabilities_to_string(cc
);
2585 r
= table_add_many(table
,
2586 TABLE_STRING
, link
->name
,
2587 TABLE_STRING
, chassis_id
,
2588 TABLE_STRING
, system_name
,
2589 TABLE_STRING
, capabilities
,
2590 TABLE_STRING
, port_id
,
2591 TABLE_STRING
, port_description
);
2593 return table_log_add_error(r
);
2599 r
= table_print(table
, NULL
);
2601 return table_log_print_error(r
);
2604 lldp_capabilities_legend(all
);
2605 printf("\n%i neighbors listed.\n", m
);
2611 static int link_delete_send_message(sd_netlink
*rtnl
, int index
) {
2612 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2618 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_DELLINK
, index
);
2620 return rtnl_log_create_error(r
);
2622 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2629 static int link_up_down_send_message(sd_netlink
*rtnl
, char *command
, int index
) {
2630 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2636 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_SETLINK
, index
);
2638 return rtnl_log_create_error(r
);
2640 if (streq(command
, "up"))
2641 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2643 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2645 return log_error_errno(r
, "Could not set link flags: %m");
2647 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2654 static int link_up_down(int argc
, char *argv
[], void *userdata
) {
2655 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2656 _cleanup_set_free_ Set
*indexes
= NULL
;
2660 r
= sd_netlink_open(&rtnl
);
2662 return log_error_errno(r
, "Failed to connect to netlink: %m");
2664 indexes
= set_new(NULL
);
2668 for (int i
= 1; i
< argc
; i
++) {
2669 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2673 r
= set_put(indexes
, INT_TO_PTR(index
));
2678 SET_FOREACH(p
, indexes
) {
2679 index
= PTR_TO_INT(p
);
2680 r
= link_up_down_send_message(rtnl
, argv
[0], index
);
2682 return log_error_errno(r
, "Failed to bring %s interface %s: %m",
2683 argv
[0], FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2689 static int link_delete(int argc
, char *argv
[], void *userdata
) {
2690 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2691 _cleanup_set_free_ Set
*indexes
= NULL
;
2695 r
= sd_netlink_open(&rtnl
);
2697 return log_error_errno(r
, "Failed to connect to netlink: %m");
2699 indexes
= set_new(NULL
);
2703 for (int i
= 1; i
< argc
; i
++) {
2704 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2708 r
= set_put(indexes
, INT_TO_PTR(index
));
2713 SET_FOREACH(p
, indexes
) {
2714 index
= PTR_TO_INT(p
);
2715 r
= link_delete_send_message(rtnl
, index
);
2717 return log_error_errno(r
, "Failed to delete interface %s: %m",
2718 FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2724 static int link_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2725 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2732 r
= bus_call_method(bus
, bus_network_mgr
, "RenewLink", &error
, NULL
, "i", index
);
2734 return log_error_errno(r
, "Failed to renew dynamic configuration of interface %s: %s",
2735 name
, bus_error_message(&error
, r
));
2740 static int link_renew(int argc
, char *argv
[], void *userdata
) {
2741 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2742 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2745 r
= acquire_bus(&bus
);
2751 for (int i
= 1; i
< argc
; i
++) {
2754 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2758 RET_GATHER(r
, link_renew_one(bus
, index
, argv
[i
]));
2764 static int link_force_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2765 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2772 r
= bus_call_method(bus
, bus_network_mgr
, "ForceRenewLink", &error
, NULL
, "i", index
);
2774 return log_error_errno(r
, "Failed to force renew dynamic configuration of interface %s: %s",
2775 name
, bus_error_message(&error
, r
));
2780 static int link_force_renew(int argc
, char *argv
[], void *userdata
) {
2781 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2782 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2785 r
= acquire_bus(&bus
);
2789 for (int i
= 1; i
< argc
; i
++) {
2790 int index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2794 r
= link_force_renew_one(bus
, index
, argv
[i
]);
2795 if (r
< 0 && k
>= 0)
2802 static int verb_reload(int argc
, char *argv
[], void *userdata
) {
2803 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2804 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2807 r
= acquire_bus(&bus
);
2811 r
= bus_call_method(bus
, bus_network_mgr
, "Reload", &error
, NULL
, NULL
);
2813 return log_error_errno(r
, "Failed to reload network settings: %s", bus_error_message(&error
, r
));
2818 static int verb_reconfigure(int argc
, char *argv
[], void *userdata
) {
2819 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2820 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2821 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2822 _cleanup_set_free_ Set
*indexes
= NULL
;
2826 r
= acquire_bus(&bus
);
2830 indexes
= set_new(NULL
);
2834 for (int i
= 1; i
< argc
; i
++) {
2835 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2839 r
= set_put(indexes
, INT_TO_PTR(index
));
2844 SET_FOREACH(p
, indexes
) {
2845 index
= PTR_TO_INT(p
);
2846 r
= bus_call_method(bus
, bus_network_mgr
, "ReconfigureLink", &error
, NULL
, "i", index
);
2848 return log_error_errno(r
, "Failed to reconfigure network interface %s: %s",
2849 FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
),
2850 bus_error_message(&error
, r
));
2856 static int help(void) {
2857 _cleanup_free_
char *link
= NULL
;
2860 r
= terminal_urlify_man("networkctl", "1", &link
);
2864 printf("%s [OPTIONS...] COMMAND\n\n"
2865 "%sQuery and control the networking subsystem.%s\n"
2867 " list [PATTERN...] List links\n"
2868 " status [PATTERN...] Show link status\n"
2869 " lldp [PATTERN...] Show LLDP neighbors\n"
2870 " label Show current address label entries in the kernel\n"
2871 " delete DEVICES... Delete virtual netdevs\n"
2872 " up DEVICES... Bring devices up\n"
2873 " down DEVICES... Bring devices down\n"
2874 " renew DEVICES... Renew dynamic configurations\n"
2875 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
2876 " reconfigure DEVICES... Reconfigure interfaces\n"
2877 " reload Reload .network and .netdev files\n"
2878 " edit FILES|DEVICES... Edit network configuration files\n"
2879 " cat FILES|DEVICES... Show network configuration files\n"
2880 " mask FILES... Mask network configuration files\n"
2881 " unmask FILES... Unmask network configuration files\n"
2883 " -h --help Show this help\n"
2884 " --version Show package version\n"
2885 " --no-pager Do not pipe output into a pager\n"
2886 " --no-legend Do not show the headers and footers\n"
2887 " -a --all Show status for all links\n"
2888 " -s --stats Show detailed link statistics\n"
2889 " -l --full Do not ellipsize output\n"
2890 " -n --lines=INTEGER Number of journal entries to show\n"
2891 " --json=pretty|short|off\n"
2892 " Generate JSON output\n"
2893 " --no-reload Do not reload systemd-networkd or systemd-udevd\n"
2894 " after editing network config\n"
2895 " --drop-in=NAME Edit specified drop-in instead of main config file\n"
2896 " --runtime Edit runtime config files\n"
2897 "\nSee the %s for details.\n",
2898 program_invocation_short_name
,
2906 static int parse_argv(int argc
, char *argv
[]) {
2908 ARG_VERSION
= 0x100,
2917 static const struct option options
[] = {
2918 { "help", no_argument
, NULL
, 'h' },
2919 { "version", no_argument
, NULL
, ARG_VERSION
},
2920 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2921 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2922 { "all", no_argument
, NULL
, 'a' },
2923 { "stats", no_argument
, NULL
, 's' },
2924 { "full", no_argument
, NULL
, 'l' },
2925 { "lines", required_argument
, NULL
, 'n' },
2926 { "json", required_argument
, NULL
, ARG_JSON
},
2927 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
2928 { "drop-in", required_argument
, NULL
, ARG_DROP_IN
},
2929 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
2938 while ((c
= getopt_long(argc
, argv
, "hasln:", options
, NULL
)) >= 0) {
2949 arg_pager_flags
|= PAGER_DISABLE
;
2957 arg_no_reload
= true;
2965 if (isempty(optarg
))
2966 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Empty drop-in file name.");
2968 if (!endswith(optarg
, ".conf")) {
2971 conf
= strjoin(optarg
, ".conf");
2975 free_and_replace(arg_drop_in
, conf
);
2977 r
= free_and_strdup(&arg_drop_in
, optarg
);
2982 if (!filename_is_valid(arg_drop_in
))
2983 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2984 "Invalid drop-in file name '%s'.", arg_drop_in
);
3001 if (safe_atou(optarg
, &arg_lines
) < 0)
3002 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3003 "Failed to parse lines '%s'", optarg
);
3007 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3016 assert_not_reached();
3023 static int networkctl_main(int argc
, char *argv
[]) {
3024 static const Verb verbs
[] = {
3025 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
|VERB_ONLINE_ONLY
, list_links
},
3026 { "status", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, link_status
},
3027 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
3028 { "label", 1, 1, 0, list_address_labels
},
3029 { "delete", 2, VERB_ANY
, 0, link_delete
},
3030 { "up", 2, VERB_ANY
, 0, link_up_down
},
3031 { "down", 2, VERB_ANY
, 0, link_up_down
},
3032 { "renew", 2, VERB_ANY
, VERB_ONLINE_ONLY
, link_renew
},
3033 { "forcerenew", 2, VERB_ANY
, VERB_ONLINE_ONLY
, link_force_renew
},
3034 { "reconfigure", 2, VERB_ANY
, VERB_ONLINE_ONLY
, verb_reconfigure
},
3035 { "reload", 1, 1, VERB_ONLINE_ONLY
, verb_reload
},
3036 { "edit", 2, VERB_ANY
, 0, verb_edit
},
3037 { "cat", 2, VERB_ANY
, 0, verb_cat
},
3038 { "mask", 2, VERB_ANY
, 0, verb_mask
},
3039 { "unmask", 2, VERB_ANY
, 0, verb_unmask
},
3043 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3046 static int run(int argc
, char* argv
[]) {
3053 r
= parse_argv(argc
, argv
);
3057 return networkctl_main(argc
, argv
);
3060 DEFINE_MAIN_FUNCTION(run
);