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 "bus-wait-for-jobs.h"
30 #include "conf-files.h"
31 #include "device-util.h"
32 #include "edit-util.h"
34 #include "ether-addr-util.h"
35 #include "ethtool-util.h"
37 #include "format-table.h"
38 #include "format-util.h"
39 #include "geneve-util.h"
40 #include "glob-util.h"
41 #include "hwdb-util.h"
42 #include "ipvlan-util.h"
43 #include "local-addresses.h"
44 #include "locale-util.h"
45 #include "logs-show.h"
47 #include "macvlan-util.h"
48 #include "main-func.h"
49 #include "netif-util.h"
50 #include "netlink-util.h"
51 #include "network-internal.h"
52 #include "network-util.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"
60 #include "socket-netlink.h"
61 #include "socket-util.h"
62 #include "sort-util.h"
63 #include "sparse-endian.h"
64 #include "stdio-util.h"
65 #include "string-table.h"
66 #include "string-util.h"
69 #include "terminal-util.h"
73 #include "wifi-util.h"
75 /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */
76 #define NETDEV_KIND_MAX 64
78 /* use 128 kB for receive socket kernel queue, we shouldn't need more here */
79 #define RCVBUF_SIZE (128*1024)
81 static PagerFlags arg_pager_flags
= 0;
82 static bool arg_legend
= true;
83 static bool arg_no_reload
= false;
84 static bool arg_all
= false;
85 static bool arg_stats
= false;
86 static bool arg_full
= false;
87 static unsigned arg_lines
= 10;
88 static char *arg_drop_in
= NULL
;
89 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
91 STATIC_DESTRUCTOR_REGISTER(arg_drop_in
, freep
);
93 static int check_netns_match(sd_bus
*bus
) {
94 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
101 r
= bus_get_property_trivial(bus
, bus_network_mgr
, "NamespaceId", &error
, 't', &id
);
103 log_debug_errno(r
, "Failed to query network namespace of networkd, ignoring: %s", bus_error_message(&error
, r
));
107 log_debug("systemd-networkd.service not running in a network namespace (?), skipping netns check.");
111 if (stat("/proc/self/ns/net", &st
) < 0)
112 return log_error_errno(errno
, "Failed to determine our own network namespace ID: %m");
115 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
116 "networkctl must be invoked in same network namespace as systemd-networkd.service.");
121 static bool networkd_is_running(void) {
122 static int cached
= -1;
126 r
= access("/run/systemd/netif/state", F_OK
);
129 log_debug_errno(errno
,
130 "Failed to determine whether networkd is running, assuming it's not: %m");
140 static int acquire_bus(sd_bus
**ret
) {
141 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
146 r
= sd_bus_open_system(&bus
);
148 return log_error_errno(r
, "Failed to connect to system bus: %m");
150 if (networkd_is_running()) {
151 r
= check_netns_match(bus
);
155 log_warning("systemd-networkd is not running, output might be incomplete.");
157 *ret
= TAKE_PTR(bus
);
161 static int get_description(sd_bus
*bus
, JsonVariant
**ret
) {
162 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
163 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
170 r
= bus_call_method(bus
, bus_network_mgr
, "Describe", &error
, &reply
, NULL
);
172 return log_error_errno(r
, "Failed to get description: %s", bus_error_message(&error
, r
));
174 r
= sd_bus_message_read(reply
, "s", &text
);
176 return bus_log_parse_error(r
);
178 r
= json_parse(text
, 0, ret
, NULL
, NULL
);
180 return log_error_errno(r
, "Failed to parse JSON: %m");
185 static int dump_manager_description(sd_bus
*bus
) {
186 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
191 r
= get_description(bus
, &v
);
195 json_variant_dump(v
, arg_json_format_flags
, NULL
, NULL
);
199 static int dump_link_description(sd_bus
*bus
, char * const *patterns
) {
200 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
201 _cleanup_free_
bool *matched_patterns
= NULL
;
209 r
= get_description(bus
, &v
);
213 matched_patterns
= new0(bool, strv_length(patterns
));
214 if (!matched_patterns
)
217 JSON_VARIANT_ARRAY_FOREACH(i
, json_variant_by_key(v
, "Interfaces")) {
218 char ifindex_str
[DECIMAL_STR_MAX(int64_t)];
223 name
= json_variant_string(json_variant_by_key(i
, "Name"));
224 index
= json_variant_integer(json_variant_by_key(i
, "Index"));
225 xsprintf(ifindex_str
, "%" PRIi64
, index
);
227 if (!strv_fnmatch_full(patterns
, ifindex_str
, 0, &pos
) &&
228 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
232 JSON_VARIANT_ARRAY_FOREACH(a
, json_variant_by_key(i
, "AlternativeNames"))
233 if (strv_fnmatch_full(patterns
, json_variant_string(a
), 0, &pos
)) {
242 matched_patterns
[pos
] = true;
243 json_variant_dump(i
, arg_json_format_flags
, NULL
, NULL
);
247 /* Look if we matched all our arguments that are not globs. It is OK for a glob to match
248 * nothing, but not for an exact argument. */
249 for (size_t pos
= 0; pos
< strv_length(patterns
); pos
++) {
250 if (matched_patterns
[pos
])
253 if (string_is_glob(patterns
[pos
]))
254 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
257 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
258 "Interface \"%s\" not found.", patterns
[pos
]);
262 log_warning("No interfaces matched.");
267 static void operational_state_to_color(
273 if (STRPTR_IN_SET(state
, "routable", "enslaved") ||
274 (streq_ptr(name
, "lo") && streq_ptr(state
, "carrier"))) {
276 *on
= ansi_highlight_green();
278 *off
= ansi_normal();
279 } else if (streq_ptr(state
, "degraded")) {
281 *on
= ansi_highlight_yellow();
283 *off
= ansi_normal();
292 static void setup_state_to_color(const char *state
, const char **on
, const char **off
) {
293 if (streq_ptr(state
, "configured")) {
295 *on
= ansi_highlight_green();
297 *off
= ansi_normal();
298 } else if (streq_ptr(state
, "configuring")) {
300 *on
= ansi_highlight_yellow();
302 *off
= ansi_normal();
303 } else if (STRPTR_IN_SET(state
, "failed", "linger")) {
305 *on
= ansi_highlight_red();
307 *off
= ansi_normal();
316 static void online_state_to_color(const char *state
, const char **on
, const char **off
) {
317 if (streq_ptr(state
, "online")) {
319 *on
= ansi_highlight_green();
321 *off
= ansi_normal();
322 } else if (streq_ptr(state
, "partial")) {
324 *on
= ansi_highlight_yellow();
326 *off
= ansi_normal();
335 typedef struct VxLanInfo
{
342 union in_addr_union local
;
343 union in_addr_union group
;
356 typedef struct LinkInfo
{
357 char name
[IFNAMSIZ
+1];
359 sd_device
*sd_device
;
361 unsigned short iftype
;
362 struct hw_addr_data hw_address
;
363 struct hw_addr_data permanent_hw_address
;
370 uint8_t addr_gen_mode
;
372 char **alternative_names
;
375 struct rtnl_link_stats64 stats64
;
376 struct rtnl_link_stats stats
;
383 uint32_t forward_delay
;
386 uint32_t ageing_time
;
390 uint8_t mcast_igmp_version
;
394 VxLanInfo vxlan_info
;
407 uint16_t tunnel_port
;
410 union in_addr_union local
;
411 union in_addr_union remote
;
419 /* macvlan and macvtap info */
420 uint32_t macvlan_mode
;
423 uint16_t ipvlan_mode
;
424 uint16_t ipvlan_flags
;
433 enum nl80211_iftype wlan_iftype
;
435 struct ether_addr bssid
;
437 bool has_hw_address
:1;
438 bool has_permanent_hw_address
:1;
439 bool has_tx_queues
:1;
440 bool has_rx_queues
:1;
444 bool has_ethtool_link_info
:1;
445 bool has_wlan_link_info
:1;
446 bool has_tunnel_ipv4
:1;
447 bool has_ipv6_address_generation_mode
:1;
449 bool needs_freeing
:1;
452 static int link_info_compare(const LinkInfo
*a
, const LinkInfo
*b
) {
453 return CMP(a
->ifindex
, b
->ifindex
);
456 static LinkInfo
* link_info_array_free(LinkInfo
*array
) {
457 for (unsigned i
= 0; array
&& array
[i
].needs_freeing
; i
++) {
458 sd_device_unref(array
[i
].sd_device
);
459 free(array
[i
].netdev_kind
);
461 free(array
[i
].qdisc
);
462 strv_free(array
[i
].alternative_names
);
467 DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo
*, link_info_array_free
);
469 static int decode_netdev(sd_netlink_message
*m
, LinkInfo
*info
) {
475 r
= sd_netlink_message_enter_container(m
, IFLA_LINKINFO
);
479 r
= sd_netlink_message_read_string_strdup(m
, IFLA_INFO_KIND
, &info
->netdev_kind
);
481 (void) sd_netlink_message_exit_container(m
);
485 r
= sd_netlink_message_enter_container(m
, IFLA_INFO_DATA
);
489 if (streq(info
->netdev_kind
, "bridge")) {
490 (void) sd_netlink_message_read_u32(m
, IFLA_BR_FORWARD_DELAY
, &info
->forward_delay
);
491 (void) sd_netlink_message_read_u32(m
, IFLA_BR_HELLO_TIME
, &info
->hello_time
);
492 (void) sd_netlink_message_read_u32(m
, IFLA_BR_MAX_AGE
, &info
->max_age
);
493 (void) sd_netlink_message_read_u32(m
, IFLA_BR_AGEING_TIME
, &info
->ageing_time
);
494 (void) sd_netlink_message_read_u32(m
, IFLA_BR_STP_STATE
, &info
->stp_state
);
495 (void) sd_netlink_message_read_u32(m
, IFLA_BRPORT_COST
, &info
->cost
);
496 (void) sd_netlink_message_read_u16(m
, IFLA_BR_PRIORITY
, &info
->priority
);
497 (void) sd_netlink_message_read_u8(m
, IFLA_BR_MCAST_IGMP_VERSION
, &info
->mcast_igmp_version
);
498 (void) sd_netlink_message_read_u8(m
, IFLA_BRPORT_STATE
, &info
->port_state
);
499 } if (streq(info
->netdev_kind
, "bond")) {
500 (void) sd_netlink_message_read_u8(m
, IFLA_BOND_MODE
, &info
->mode
);
501 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_MIIMON
, &info
->miimon
);
502 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_DOWNDELAY
, &info
->downdelay
);
503 (void) sd_netlink_message_read_u32(m
, IFLA_BOND_UPDELAY
, &info
->updelay
);
504 } else if (streq(info
->netdev_kind
, "vxlan")) {
505 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_ID
, &info
->vxlan_info
.vni
);
507 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_GROUP
, &info
->vxlan_info
.group
.in
);
509 info
->vxlan_info
.group_family
= AF_INET
;
511 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_GROUP6
, &info
->vxlan_info
.group
.in6
);
513 info
->vxlan_info
.group_family
= AF_INET6
;
516 r
= sd_netlink_message_read_in_addr(m
, IFLA_VXLAN_LOCAL
, &info
->vxlan_info
.local
.in
);
518 info
->vxlan_info
.local_family
= AF_INET
;
520 r
= sd_netlink_message_read_in6_addr(m
, IFLA_VXLAN_LOCAL6
, &info
->vxlan_info
.local
.in6
);
522 info
->vxlan_info
.local_family
= AF_INET6
;
525 (void) sd_netlink_message_read_u32(m
, IFLA_VXLAN_LINK
, &info
->vxlan_info
.link
);
526 (void) sd_netlink_message_read_u16(m
, IFLA_VXLAN_PORT
, &info
->vxlan_info
.dest_port
);
527 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_PROXY
, &info
->vxlan_info
.proxy
);
528 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_LEARNING
, &info
->vxlan_info
.learning
);
529 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_RSC
, &info
->vxlan_info
.rsc
);
530 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L3MISS
, &info
->vxlan_info
.l3miss
);
531 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_L2MISS
, &info
->vxlan_info
.l2miss
);
532 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TOS
, &info
->vxlan_info
.tos
);
533 (void) sd_netlink_message_read_u8(m
, IFLA_VXLAN_TTL
, &info
->vxlan_info
.ttl
);
534 } else if (streq(info
->netdev_kind
, "vlan"))
535 (void) sd_netlink_message_read_u16(m
, IFLA_VLAN_ID
, &info
->vlan_id
);
536 else if (STR_IN_SET(info
->netdev_kind
, "ipip", "sit")) {
537 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_LOCAL
, &info
->local
.in
);
538 (void) sd_netlink_message_read_in_addr(m
, IFLA_IPTUN_REMOTE
, &info
->remote
.in
);
539 } else if (streq(info
->netdev_kind
, "geneve")) {
540 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_ID
, &info
->vni
);
542 r
= sd_netlink_message_read_in_addr(m
, IFLA_GENEVE_REMOTE
, &info
->remote
.in
);
544 info
->has_tunnel_ipv4
= true;
546 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GENEVE_REMOTE6
, &info
->remote
.in6
);
548 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL
, &info
->ttl
);
549 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TTL_INHERIT
, &info
->inherit
);
550 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_TOS
, &info
->tos
);
551 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_DF
, &info
->df
);
552 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_CSUM
, &info
->csum
);
553 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
, &info
->csum6_tx
);
554 (void) sd_netlink_message_read_u8(m
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
, &info
->csum6_rx
);
555 (void) sd_netlink_message_read_u16(m
, IFLA_GENEVE_PORT
, &info
->tunnel_port
);
556 (void) sd_netlink_message_read_u32(m
, IFLA_GENEVE_LABEL
, &info
->label
);
557 } else if (STR_IN_SET(info
->netdev_kind
, "gre", "gretap", "erspan")) {
558 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in
);
559 (void) sd_netlink_message_read_in_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in
);
560 } else if (STR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan")) {
561 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_LOCAL
, &info
->local
.in6
);
562 (void) sd_netlink_message_read_in6_addr(m
, IFLA_GRE_REMOTE
, &info
->remote
.in6
);
563 } else if (streq(info
->netdev_kind
, "vti")) {
564 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in
);
565 (void) sd_netlink_message_read_in_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in
);
566 } else if (streq(info
->netdev_kind
, "vti6")) {
567 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_LOCAL
, &info
->local
.in6
);
568 (void) sd_netlink_message_read_in6_addr(m
, IFLA_VTI_REMOTE
, &info
->remote
.in6
);
569 } else if (STR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap"))
570 (void) sd_netlink_message_read_u32(m
, IFLA_MACVLAN_MODE
, &info
->macvlan_mode
);
571 else if (streq(info
->netdev_kind
, "ipvlan")) {
572 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_MODE
, &info
->ipvlan_mode
);
573 (void) sd_netlink_message_read_u16(m
, IFLA_IPVLAN_FLAGS
, &info
->ipvlan_flags
);
576 (void) sd_netlink_message_exit_container(m
);
577 (void) sd_netlink_message_exit_container(m
);
582 static int decode_link(
583 sd_netlink_message
*m
,
585 char * const *patterns
,
586 bool matched_patterns
[]) {
588 _cleanup_strv_free_
char **altnames
= NULL
;
589 const char *name
, *qdisc
;
596 r
= sd_netlink_message_get_type(m
, &type
);
600 if (type
!= RTM_NEWLINK
)
603 r
= sd_rtnl_message_link_get_ifindex(m
, &ifindex
);
607 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &name
);
611 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &altnames
);
612 if (r
< 0 && r
!= -ENODATA
)
616 char str
[DECIMAL_STR_MAX(int)];
619 assert(matched_patterns
);
621 xsprintf(str
, "%i", ifindex
);
622 if (!strv_fnmatch_full(patterns
, str
, 0, &pos
) &&
623 !strv_fnmatch_full(patterns
, name
, 0, &pos
)) {
626 STRV_FOREACH(p
, altnames
)
627 if (strv_fnmatch_full(patterns
, *p
, 0, &pos
)) {
635 matched_patterns
[pos
] = true;
638 r
= sd_rtnl_message_link_get_type(m
, &info
->iftype
);
642 strscpy(info
->name
, sizeof info
->name
, name
);
643 info
->ifindex
= ifindex
;
644 info
->alternative_names
= TAKE_PTR(altnames
);
646 info
->has_hw_address
=
647 netlink_message_read_hw_addr(m
, IFLA_ADDRESS
, &info
->hw_address
) >= 0 &&
648 info
->hw_address
.length
> 0;
650 info
->has_permanent_hw_address
=
651 (netlink_message_read_hw_addr(m
, IFLA_PERM_ADDRESS
, &info
->permanent_hw_address
) >= 0 ||
652 ethtool_get_permanent_hw_addr(NULL
, info
->name
, &info
->permanent_hw_address
) >= 0) &&
653 !hw_addr_is_null(&info
->permanent_hw_address
) &&
654 !hw_addr_equal(&info
->permanent_hw_address
, &info
->hw_address
);
656 (void) sd_netlink_message_read_u32(m
, IFLA_MTU
, &info
->mtu
);
657 (void) sd_netlink_message_read_u32(m
, IFLA_MIN_MTU
, &info
->min_mtu
);
658 (void) sd_netlink_message_read_u32(m
, IFLA_MAX_MTU
, &info
->max_mtu
);
660 info
->has_rx_queues
=
661 sd_netlink_message_read_u32(m
, IFLA_NUM_RX_QUEUES
, &info
->rx_queues
) >= 0 &&
664 info
->has_tx_queues
=
665 sd_netlink_message_read_u32(m
, IFLA_NUM_TX_QUEUES
, &info
->tx_queues
) >= 0 &&
668 if (sd_netlink_message_read(m
, IFLA_STATS64
, sizeof info
->stats64
, &info
->stats64
) >= 0)
669 info
->has_stats64
= true;
670 else if (sd_netlink_message_read(m
, IFLA_STATS
, sizeof info
->stats
, &info
->stats
) >= 0)
671 info
->has_stats
= true;
673 r
= sd_netlink_message_read_string(m
, IFLA_QDISC
, &qdisc
);
675 info
->qdisc
= strdup(qdisc
);
680 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, &info
->master
);
682 r
= sd_netlink_message_enter_container(m
, IFLA_AF_SPEC
);
684 r
= sd_netlink_message_enter_container(m
, AF_INET6
);
686 r
= sd_netlink_message_read_u8(m
, IFLA_INET6_ADDR_GEN_MODE
, &info
->addr_gen_mode
);
687 if (r
>= 0 && IN_SET(info
->addr_gen_mode
,
688 IN6_ADDR_GEN_MODE_EUI64
,
689 IN6_ADDR_GEN_MODE_NONE
,
690 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
,
691 IN6_ADDR_GEN_MODE_RANDOM
))
692 info
->has_ipv6_address_generation_mode
= true;
694 (void) sd_netlink_message_exit_container(m
);
696 (void) sd_netlink_message_exit_container(m
);
700 (void) decode_netdev(m
, info
);
705 static int link_get_property(
707 const LinkInfo
*link
,
709 sd_bus_message
**reply
,
711 const char *propname
) {
713 _cleanup_free_
char *path
= NULL
;
714 char ifindex_str
[DECIMAL_STR_MAX(int)];
719 assert(link
->ifindex
>= 0);
725 xsprintf(ifindex_str
, "%i", link
->ifindex
);
727 r
= sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str
, &path
);
731 return sd_bus_get_property(bus
, "org.freedesktop.network1", path
, iface
, propname
, error
, reply
, "ss");
734 static int acquire_link_bitrates(sd_bus
*bus
, LinkInfo
*link
) {
735 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
736 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
742 r
= link_get_property(bus
, link
, &error
, &reply
, "org.freedesktop.network1.Link", "BitRates");
744 bool quiet
= sd_bus_error_has_names(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
,
745 BUS_ERROR_SPEED_METER_INACTIVE
);
747 return log_full_errno(quiet
? LOG_DEBUG
: LOG_WARNING
,
748 r
, "Failed to query link bit rates: %s", bus_error_message(&error
, r
));
751 r
= sd_bus_message_enter_container(reply
, 'v', "(tt)");
753 return bus_log_parse_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 *prefix
, char * const *l
) {
1123 if (strv_isempty(l
))
1126 r
= table_add_many(table
,
1128 TABLE_STRING
, prefix
,
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");
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
, 'v', "a(uayayayayt)");
1416 return bus_log_parse_error(r
);
1418 r
= sd_bus_message_enter_container(reply
, 'a', "(uayayayayt)");
1420 return bus_log_parse_error(r
);
1422 while ((r
= sd_bus_message_enter_container(reply
, 'r', "uayayayayt")) > 0) {
1423 _cleanup_free_
char *id
= NULL
, *ip
= NULL
;
1424 const void *client_id
, *addr
, *gtw
, *hwaddr
;
1425 size_t client_id_sz
, sz
;
1426 uint64_t expiration
;
1429 r
= sd_bus_message_read(reply
, "u", &family
);
1431 return bus_log_parse_error(r
);
1433 r
= sd_bus_message_read_array(reply
, 'y', &client_id
, &client_id_sz
);
1435 return bus_log_parse_error(r
);
1437 r
= sd_bus_message_read_array(reply
, 'y', &addr
, &sz
);
1438 if (r
< 0 || sz
!= 4)
1439 return bus_log_parse_error(r
);
1441 r
= sd_bus_message_read_array(reply
, 'y', >w
, &sz
);
1442 if (r
< 0 || sz
!= 4)
1443 return bus_log_parse_error(r
);
1445 r
= sd_bus_message_read_array(reply
, 'y', &hwaddr
, &sz
);
1447 return bus_log_parse_error(r
);
1449 r
= sd_bus_message_read_basic(reply
, 't', &expiration
);
1451 return bus_log_parse_error(r
);
1453 r
= sd_dhcp_client_id_to_string(client_id
, client_id_sz
, &id
);
1455 return bus_log_parse_error(r
);
1457 r
= in_addr_to_string(family
, addr
, &ip
);
1459 return bus_log_parse_error(r
);
1461 r
= strv_extendf(&buf
, "%s (to %s)", ip
, id
);
1465 r
= sd_bus_message_exit_container(reply
);
1467 return bus_log_parse_error(r
);
1470 return bus_log_parse_error(r
);
1472 r
= sd_bus_message_exit_container(reply
);
1474 return bus_log_parse_error(r
);
1476 r
= sd_bus_message_exit_container(reply
);
1478 return bus_log_parse_error(r
);
1480 if (strv_isempty(buf
)) {
1481 r
= strv_extendf(&buf
, "none");
1486 return dump_list(table
, prefix
, buf
);
1489 static int dump_ifindexes(Table
*table
, const char *prefix
, const int *ifindexes
) {
1495 if (!ifindexes
|| ifindexes
[0] <= 0)
1498 for (unsigned c
= 0; ifindexes
[c
] > 0; c
++) {
1499 r
= table_add_many(table
,
1501 TABLE_STRING
, c
== 0 ? prefix
: "",
1502 TABLE_IFINDEX
, ifindexes
[c
]);
1504 return table_log_add_error(r
);
1510 #define DUMP_STATS_ONE(name, val_name) \
1511 r = table_add_many(table, \
1513 TABLE_STRING, name ":"); \
1515 return table_log_add_error(r); \
1516 r = table_add_cell(table, NULL, \
1517 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
1518 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
1520 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_many(table
,
1561 TABLE_STRING
, field
);
1563 return table_log_add_error(r
);
1565 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s%s",
1566 HW_ADDR_TO_STR(addr
),
1567 description
? " (" : "",
1568 strempty(description
),
1569 description
? ")" : "");
1571 return table_log_add_error(r
);
1576 static OutputFlags
get_output_flags(void) {
1578 arg_all
* OUTPUT_SHOW_ALL
|
1579 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
1580 colors_enabled() * OUTPUT_COLOR
;
1583 static int show_logs(const LinkInfo
*info
) {
1584 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1590 r
= sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1592 return log_error_errno(r
, "Failed to open journal: %m");
1594 r
= add_match_this_boot(j
, NULL
);
1596 return log_error_errno(r
, "Failed to add boot matches: %m");
1599 char m1
[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)];
1600 const char *m2
, *m3
;
1603 xsprintf(m1
, "_KERNEL_DEVICE=n%i", info
->ifindex
);
1605 m2
= strjoina("INTERFACE=", info
->name
);
1607 m3
= strjoina("DEVICE=", info
->name
);
1610 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1611 (r
= sd_journal_add_disjunction(j
)) ||
1612 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1613 (r
= sd_journal_add_disjunction(j
)) ||
1614 (r
= sd_journal_add_match(j
, m3
, 0))
1617 return log_error_errno(r
, "Failed to add link matches: %m");
1619 r
= add_matches_for_unit(j
, "systemd-networkd.service");
1621 return log_error_errno(r
, "Failed to add unit matches: %m");
1623 r
= add_matches_for_unit(j
, "systemd-networkd-wait-online.service");
1625 return log_error_errno(r
, "Failed to add unit matches: %m");
1628 return show_journal(
1635 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
1639 static int table_add_string_line(Table
*table
, const char *key
, const char *value
) {
1648 r
= table_add_many(table
,
1651 TABLE_STRING
, value
);
1653 return table_log_add_error(r
);
1658 static int format_dropins(char **dropins
) {
1659 STRV_FOREACH(d
, dropins
) {
1660 _cleanup_free_
char *s
= NULL
;
1661 int glyph
= *(d
+ 1) == NULL
? SPECIAL_GLYPH_TREE_RIGHT
: SPECIAL_GLYPH_TREE_BRANCH
;
1663 s
= strjoin(special_glyph(glyph
), *d
);
1667 free_and_replace(*d
, s
);
1673 static int link_status_one(
1677 const LinkInfo
*info
) {
1679 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **sip
= NULL
, **search_domains
= NULL
,
1680 **route_domains
= NULL
, **link_dropins
= NULL
, **network_dropins
= NULL
;
1681 _cleanup_free_
char *t
= NULL
, *network
= NULL
, *iaid
= NULL
, *duid
= NULL
,
1682 *setup_state
= NULL
, *operational_state
= NULL
, *online_state
= NULL
, *activation_policy
= NULL
;
1683 const char *driver
= NULL
, *path
= NULL
, *vendor
= NULL
, *model
= NULL
, *link
= NULL
,
1684 *on_color_operational
, *off_color_operational
, *on_color_setup
, *off_color_setup
, *on_color_online
;
1685 _cleanup_free_
int *carrier_bound_to
= NULL
, *carrier_bound_by
= NULL
;
1686 _cleanup_(sd_dhcp_lease_unrefp
) sd_dhcp_lease
*lease
= NULL
;
1687 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1695 (void) sd_network_link_get_operational_state(info
->ifindex
, &operational_state
);
1696 operational_state_to_color(info
->name
, operational_state
, &on_color_operational
, &off_color_operational
);
1698 (void) sd_network_link_get_online_state(info
->ifindex
, &online_state
);
1699 online_state_to_color(online_state
, &on_color_online
, NULL
);
1701 (void) sd_network_link_get_setup_state(info
->ifindex
, &setup_state
);
1702 setup_state_to_color(setup_state
, &on_color_setup
, &off_color_setup
);
1704 (void) sd_network_link_get_dns(info
->ifindex
, &dns
);
1705 (void) sd_network_link_get_search_domains(info
->ifindex
, &search_domains
);
1706 (void) sd_network_link_get_route_domains(info
->ifindex
, &route_domains
);
1707 (void) sd_network_link_get_ntp(info
->ifindex
, &ntp
);
1708 (void) sd_network_link_get_sip(info
->ifindex
, &sip
);
1709 (void) sd_network_link_get_network_file(info
->ifindex
, &network
);
1710 (void) sd_network_link_get_network_file_dropins(info
->ifindex
, &network_dropins
);
1711 (void) sd_network_link_get_carrier_bound_to(info
->ifindex
, &carrier_bound_to
);
1712 (void) sd_network_link_get_carrier_bound_by(info
->ifindex
, &carrier_bound_by
);
1713 (void) sd_network_link_get_activation_policy(info
->ifindex
, &activation_policy
);
1715 if (info
->sd_device
) {
1718 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_LINK_FILE", &link
);
1720 if (sd_device_get_property_value(info
->sd_device
, "ID_NET_LINK_FILE_DROPINS", &joined
) >= 0) {
1721 r
= strv_split_full(&link_dropins
, joined
, ":", EXTRACT_CUNESCAPE
);
1726 (void) sd_device_get_property_value(info
->sd_device
, "ID_NET_DRIVER", &driver
);
1727 (void) sd_device_get_property_value(info
->sd_device
, "ID_PATH", &path
);
1729 if (sd_device_get_property_value(info
->sd_device
, "ID_VENDOR_FROM_DATABASE", &vendor
) < 0)
1730 (void) sd_device_get_property_value(info
->sd_device
, "ID_VENDOR", &vendor
);
1732 if (sd_device_get_property_value(info
->sd_device
, "ID_MODEL_FROM_DATABASE", &model
) < 0)
1733 (void) sd_device_get_property_value(info
->sd_device
, "ID_MODEL", &model
);
1736 r
= net_get_type_string(info
->sd_device
, info
->iftype
, &t
);
1740 char lease_file
[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)];
1741 xsprintf(lease_file
, "/run/systemd/netif/leases/%i", info
->ifindex
);
1743 (void) dhcp_lease_load(&lease
, lease_file
);
1745 r
= format_dropins(network_dropins
);
1749 if (strv_prepend(&network_dropins
, network
) < 0)
1752 r
= format_dropins(link_dropins
);
1756 if (strv_prepend(&link_dropins
, link
) < 0)
1759 table
= table_new("dot", "key", "value");
1764 table_set_width(table
, 0);
1766 assert_se(cell
= table_get_cell(table
, 0, 0));
1767 (void) table_set_ellipsize_percent(table
, cell
, 100);
1769 assert_se(cell
= table_get_cell(table
, 0, 1));
1770 (void) table_set_ellipsize_percent(table
, cell
, 100);
1772 table_set_header(table
, false);
1774 /* First line: circle, ifindex, ifname. */
1775 r
= table_add_many(table
,
1776 TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
),
1777 TABLE_SET_COLOR
, on_color_operational
);
1779 return table_log_add_error(r
);
1780 r
= table_add_cell_stringf(table
, &cell
, "%i: %s", info
->ifindex
, info
->name
);
1782 return table_log_add_error(r
);
1783 r
= table_add_many(table
, TABLE_EMPTY
);
1785 return table_log_add_error(r
);
1787 (void) table_set_align_percent(table
, cell
, 0);
1789 /* unit files and basic states. */
1790 r
= table_add_many(table
,
1792 TABLE_STRING
, "Link File:",
1793 TABLE_SET_ALIGN_PERCENT
, 100,
1794 TABLE_STRV
, link_dropins
?: STRV_MAKE("n/a"),
1796 TABLE_STRING
, "Network File:",
1797 TABLE_STRV
, network_dropins
?: STRV_MAKE("n/a"),
1799 TABLE_STRING
, "State:");
1801 return table_log_add_error(r
);
1803 r
= table_add_cell_stringf(table
, NULL
, "%s%s%s (%s%s%s)",
1804 on_color_operational
, strna(operational_state
), off_color_operational
,
1805 on_color_setup
, setup_state
?: "unmanaged", off_color_setup
);
1807 return table_log_add_error(r
);
1809 r
= table_add_many(table
,
1811 TABLE_STRING
, "Online state:",
1812 TABLE_STRING
, online_state
?: "unknown",
1813 TABLE_SET_COLOR
, on_color_online
);
1815 return table_log_add_error(r
);
1817 r
= table_add_string_line(table
, "Type:", t
);
1821 r
= table_add_string_line(table
, "Kind:", info
->netdev_kind
);
1825 r
= table_add_string_line(table
, "Path:", path
);
1829 r
= table_add_string_line(table
, "Driver:", driver
);
1833 r
= table_add_string_line(table
, "Vendor:", vendor
);
1837 r
= table_add_string_line(table
, "Model:", model
);
1841 strv_sort(info
->alternative_names
);
1842 r
= dump_list(table
, "Alternative Names:", info
->alternative_names
);
1846 if (info
->has_hw_address
) {
1847 r
= dump_hw_address(table
, hwdb
, "Hardware Address:", &info
->hw_address
);
1852 if (info
->has_permanent_hw_address
) {
1853 r
= dump_hw_address(table
, hwdb
, "Permanent Hardware Address:", &info
->permanent_hw_address
);
1858 if (info
->mtu
> 0) {
1859 char min_str
[DECIMAL_STR_MAX(uint32_t)], max_str
[DECIMAL_STR_MAX(uint32_t)];
1861 xsprintf(min_str
, "%" PRIu32
, info
->min_mtu
);
1862 xsprintf(max_str
, "%" PRIu32
, info
->max_mtu
);
1864 r
= table_add_many(table
,
1866 TABLE_STRING
, "MTU:");
1868 return table_log_add_error(r
);
1869 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"%s%s%s%s%s%s%s",
1871 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? " (" : "",
1872 info
->min_mtu
> 0 ? "min: " : "",
1873 info
->min_mtu
> 0 ? min_str
: "",
1874 info
->min_mtu
> 0 && info
->max_mtu
> 0 ? ", " : "",
1875 info
->max_mtu
> 0 ? "max: " : "",
1876 info
->max_mtu
> 0 ? max_str
: "",
1877 info
->min_mtu
> 0 || info
->max_mtu
> 0 ? ")" : "");
1879 return table_log_add_error(r
);
1882 r
= table_add_string_line(table
, "QDisc:", info
->qdisc
);
1886 if (info
->master
> 0) {
1887 r
= table_add_many(table
,
1889 TABLE_STRING
, "Master:",
1890 TABLE_IFINDEX
, info
->master
);
1892 return table_log_add_error(r
);
1895 if (info
->has_ipv6_address_generation_mode
) {
1896 static const struct {
1901 { "stable-privacy" },
1905 r
= table_add_many(table
,
1907 TABLE_STRING
, "IPv6 Address Generation Mode:",
1908 TABLE_STRING
, mode_table
[info
->addr_gen_mode
]);
1910 return table_log_add_error(r
);
1913 if (streq_ptr(info
->netdev_kind
, "bridge")) {
1914 r
= table_add_many(table
,
1916 TABLE_STRING
, "Forward Delay:",
1917 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->forward_delay
),
1919 TABLE_STRING
, "Hello Time:",
1920 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->hello_time
),
1922 TABLE_STRING
, "Max Age:",
1923 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->max_age
),
1925 TABLE_STRING
, "Ageing Time:",
1926 TABLE_TIMESPAN_MSEC
, jiffies_to_usec(info
->ageing_time
),
1928 TABLE_STRING
, "Priority:",
1929 TABLE_UINT16
, info
->priority
,
1931 TABLE_STRING
, "STP:",
1932 TABLE_BOOLEAN
, info
->stp_state
> 0,
1934 TABLE_STRING
, "Multicast IGMP Version:",
1935 TABLE_UINT8
, info
->mcast_igmp_version
,
1937 TABLE_STRING
, "Cost:",
1938 TABLE_UINT32
, info
->cost
);
1940 return table_log_add_error(r
);
1942 if (info
->port_state
<= BR_STATE_BLOCKING
) {
1943 r
= table_add_many(table
,
1945 TABLE_STRING
, "Port State:",
1946 TABLE_STRING
, bridge_state_to_string(info
->port_state
));
1948 return table_log_add_error(r
);
1951 } else if (streq_ptr(info
->netdev_kind
, "bond")) {
1952 r
= table_add_many(table
,
1954 TABLE_STRING
, "Mode:",
1955 TABLE_STRING
, bond_mode_to_string(info
->mode
),
1957 TABLE_STRING
, "Miimon:",
1958 TABLE_TIMESPAN_MSEC
, info
->miimon
* USEC_PER_MSEC
,
1960 TABLE_STRING
, "Updelay:",
1961 TABLE_TIMESPAN_MSEC
, info
->updelay
* USEC_PER_MSEC
,
1963 TABLE_STRING
, "Downdelay:",
1964 TABLE_TIMESPAN_MSEC
, info
->downdelay
* USEC_PER_MSEC
);
1966 return table_log_add_error(r
);
1968 } else if (streq_ptr(info
->netdev_kind
, "vxlan")) {
1969 char ttl
[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1971 if (info
->vxlan_info
.vni
> 0) {
1972 r
= table_add_many(table
,
1974 TABLE_STRING
, "VNI:",
1975 TABLE_UINT32
, info
->vxlan_info
.vni
);
1977 return table_log_add_error(r
);
1980 if (IN_SET(info
->vxlan_info
.group_family
, AF_INET
, AF_INET6
)) {
1983 r
= in_addr_is_multicast(info
->vxlan_info
.group_family
, &info
->vxlan_info
.group
);
1989 r
= table_add_many(table
,
1992 info
->vxlan_info
.group_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
,
1993 &info
->vxlan_info
.group
);
1995 return table_log_add_error(r
);
1998 if (IN_SET(info
->vxlan_info
.local_family
, AF_INET
, AF_INET6
)) {
1999 r
= table_add_many(table
,
2001 TABLE_STRING
, "Local:",
2002 info
->vxlan_info
.local_family
== AF_INET
? TABLE_IN_ADDR
: TABLE_IN6_ADDR
,
2003 &info
->vxlan_info
.local
);
2005 return table_log_add_error(r
);
2008 if (info
->vxlan_info
.dest_port
> 0) {
2009 r
= table_add_many(table
,
2011 TABLE_STRING
, "Destination Port:",
2012 TABLE_UINT16
, be16toh(info
->vxlan_info
.dest_port
));
2014 return table_log_add_error(r
);
2017 if (info
->vxlan_info
.link
> 0) {
2018 r
= table_add_many(table
,
2020 TABLE_STRING
, "Underlying Device:",
2021 TABLE_IFINDEX
, info
->vxlan_info
.link
);
2023 return table_log_add_error(r
);
2026 r
= table_add_many(table
,
2028 TABLE_STRING
, "Learning:",
2029 TABLE_BOOLEAN
, info
->vxlan_info
.learning
);
2031 return table_log_add_error(r
);
2033 r
= table_add_many(table
,
2035 TABLE_STRING
, "RSC:",
2036 TABLE_BOOLEAN
, info
->vxlan_info
.rsc
);
2038 return table_log_add_error(r
);
2040 r
= table_add_many(table
,
2042 TABLE_STRING
, "L3MISS:",
2043 TABLE_BOOLEAN
, info
->vxlan_info
.l3miss
);
2045 return table_log_add_error(r
);
2047 r
= table_add_many(table
,
2049 TABLE_STRING
, "L2MISS:",
2050 TABLE_BOOLEAN
, info
->vxlan_info
.l2miss
);
2052 return table_log_add_error(r
);
2054 if (info
->vxlan_info
.tos
> 1) {
2055 r
= table_add_many(table
,
2057 TABLE_STRING
, "TOS:",
2058 TABLE_UINT8
, info
->vxlan_info
.tos
);
2060 return table_log_add_error(r
);
2063 if (info
->vxlan_info
.ttl
> 0)
2064 xsprintf(ttl
, "%" PRIu8
, info
->vxlan_info
.ttl
);
2066 strcpy(ttl
, "auto");
2068 r
= table_add_many(table
,
2070 TABLE_STRING
, "TTL:",
2073 return table_log_add_error(r
);
2075 } else if (streq_ptr(info
->netdev_kind
, "vlan") && info
->vlan_id
> 0) {
2076 r
= table_add_many(table
,
2078 TABLE_STRING
, "VLan Id:",
2079 TABLE_UINT16
, info
->vlan_id
);
2081 return table_log_add_error(r
);
2083 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
2084 if (in_addr_is_set(AF_INET
, &info
->local
)) {
2085 r
= table_add_many(table
,
2087 TABLE_STRING
, "Local:",
2088 TABLE_IN_ADDR
, &info
->local
);
2090 return table_log_add_error(r
);
2093 if (in_addr_is_set(AF_INET
, &info
->remote
)) {
2094 r
= table_add_many(table
,
2096 TABLE_STRING
, "Remote:",
2097 TABLE_IN_ADDR
, &info
->remote
);
2099 return table_log_add_error(r
);
2102 } else if (STRPTR_IN_SET(info
->netdev_kind
, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
2103 if (in_addr_is_set(AF_INET6
, &info
->local
)) {
2104 r
= table_add_many(table
,
2106 TABLE_STRING
, "Local:",
2107 TABLE_IN6_ADDR
, &info
->local
);
2109 return table_log_add_error(r
);
2112 if (in_addr_is_set(AF_INET6
, &info
->remote
)) {
2113 r
= table_add_many(table
,
2115 TABLE_STRING
, "Remote:",
2116 TABLE_IN6_ADDR
, &info
->remote
);
2118 return table_log_add_error(r
);
2121 } else if (streq_ptr(info
->netdev_kind
, "geneve")) {
2122 r
= table_add_many(table
,
2124 TABLE_STRING
, "VNI:",
2125 TABLE_UINT32
, info
->vni
);
2127 return table_log_add_error(r
);
2129 if (info
->has_tunnel_ipv4
&& in_addr_is_set(AF_INET
, &info
->remote
)) {
2130 r
= table_add_many(table
,
2132 TABLE_STRING
, "Remote:",
2133 TABLE_IN_ADDR
, &info
->remote
);
2135 return table_log_add_error(r
);
2136 } else if (in_addr_is_set(AF_INET6
, &info
->remote
)) {
2137 r
= table_add_many(table
,
2139 TABLE_STRING
, "Remote:",
2140 TABLE_IN6_ADDR
, &info
->remote
);
2142 return table_log_add_error(r
);
2145 if (info
->ttl
> 0) {
2146 r
= table_add_many(table
,
2148 TABLE_STRING
, "TTL:",
2149 TABLE_UINT8
, info
->ttl
);
2151 return table_log_add_error(r
);
2154 if (info
->tos
> 0) {
2155 r
= table_add_many(table
,
2157 TABLE_STRING
, "TOS:",
2158 TABLE_UINT8
, info
->tos
);
2160 return table_log_add_error(r
);
2163 r
= table_add_many(table
,
2165 TABLE_STRING
, "Port:",
2166 TABLE_UINT16
, info
->tunnel_port
);
2168 return table_log_add_error(r
);
2170 r
= table_add_many(table
,
2172 TABLE_STRING
, "Inherit:",
2173 TABLE_STRING
, geneve_df_to_string(info
->inherit
));
2175 return table_log_add_error(r
);
2178 r
= table_add_many(table
,
2180 TABLE_STRING
, "IPDoNotFragment:",
2181 TABLE_UINT8
, info
->df
);
2183 return table_log_add_error(r
);
2186 r
= table_add_many(table
,
2188 TABLE_STRING
, "UDPChecksum:",
2189 TABLE_BOOLEAN
, info
->csum
);
2191 return table_log_add_error(r
);
2193 r
= table_add_many(table
,
2195 TABLE_STRING
, "UDP6ZeroChecksumTx:",
2196 TABLE_BOOLEAN
, info
->csum6_tx
);
2198 return table_log_add_error(r
);
2200 r
= table_add_many(table
,
2202 TABLE_STRING
, "UDP6ZeroChecksumRx:",
2203 TABLE_BOOLEAN
, info
->csum6_rx
);
2205 return table_log_add_error(r
);
2207 if (info
->label
> 0) {
2208 r
= table_add_many(table
,
2210 TABLE_STRING
, "FlowLabel:",
2211 TABLE_UINT32
, info
->label
);
2213 return table_log_add_error(r
);
2216 } else if (STRPTR_IN_SET(info
->netdev_kind
, "macvlan", "macvtap")) {
2217 r
= table_add_many(table
,
2219 TABLE_STRING
, "Mode:",
2220 TABLE_STRING
, macvlan_mode_to_string(info
->macvlan_mode
));
2222 return table_log_add_error(r
);
2224 } else if (streq_ptr(info
->netdev_kind
, "ipvlan")) {
2225 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
2227 if (info
->ipvlan_flags
& IPVLAN_F_PRIVATE
)
2228 p
= strdup("private");
2229 else if (info
->ipvlan_flags
& IPVLAN_F_VEPA
)
2232 p
= strdup("bridge");
2236 s
= strjoin(ipvlan_mode_to_string(info
->ipvlan_mode
), " (", p
, ")");
2240 r
= table_add_many(table
,
2242 TABLE_STRING
, "Mode:",
2245 return table_log_add_error(r
);
2248 if (info
->has_wlan_link_info
) {
2249 _cleanup_free_
char *esc
= NULL
;
2251 r
= table_add_many(table
,
2253 TABLE_STRING
, "WiFi access point:");
2255 return table_log_add_error(r
);
2258 esc
= cescape(info
->ssid
);
2260 r
= table_add_cell_stringf(table
, NULL
, "%s (%s)",
2262 ETHER_ADDR_TO_STR(&info
->bssid
));
2264 return table_log_add_error(r
);
2267 if (info
->has_bitrates
) {
2268 r
= table_add_many(table
,
2270 TABLE_STRING
, "Bit Rate (Tx/Rx):");
2272 return table_log_add_error(r
);
2273 r
= table_add_cell_stringf(table
, NULL
, "%sbps/%sbps",
2274 FORMAT_BYTES_FULL(info
->tx_bitrate
, 0),
2275 FORMAT_BYTES_FULL(info
->rx_bitrate
, 0));
2277 return table_log_add_error(r
);
2280 if (info
->has_tx_queues
|| info
->has_rx_queues
) {
2281 r
= table_add_many(table
,
2283 TABLE_STRING
, "Number of Queues (Tx/Rx):");
2285 return table_log_add_error(r
);
2286 r
= table_add_cell_stringf(table
, NULL
, "%" PRIu32
"/%" PRIu32
, info
->tx_queues
, info
->rx_queues
);
2288 return table_log_add_error(r
);
2291 if (info
->has_ethtool_link_info
) {
2292 if (IN_SET(info
->autonegotiation
, AUTONEG_DISABLE
, AUTONEG_ENABLE
)) {
2293 r
= table_add_many(table
,
2295 TABLE_STRING
, "Auto negotiation:",
2296 TABLE_BOOLEAN
, info
->autonegotiation
== AUTONEG_ENABLE
);
2298 return table_log_add_error(r
);
2301 if (info
->speed
> 0 && info
->speed
!= UINT64_MAX
) {
2302 r
= table_add_many(table
,
2304 TABLE_STRING
, "Speed:",
2305 TABLE_BPS
, info
->speed
);
2307 return table_log_add_error(r
);
2310 r
= table_add_string_line(table
, "Duplex:", duplex_to_string(info
->duplex
));
2314 r
= table_add_string_line(table
, "Port:", port_to_string(info
->port
));
2319 r
= dump_addresses(rtnl
, lease
, table
, info
->ifindex
);
2322 r
= dump_gateways(rtnl
, hwdb
, table
, info
->ifindex
);
2325 r
= dump_list(table
, "DNS:", dns
);
2328 r
= dump_list(table
, "Search Domains:", search_domains
);
2331 r
= dump_list(table
, "Route Domains:", route_domains
);
2334 r
= dump_list(table
, "NTP:", ntp
);
2337 r
= dump_list(table
, "SIP:", sip
);
2340 r
= dump_ifindexes(table
, "Carrier Bound To:", carrier_bound_to
);
2343 r
= dump_ifindexes(table
, "Carrier Bound By:", carrier_bound_by
);
2347 r
= table_add_string_line(table
, "Activation Policy:", activation_policy
);
2351 r
= sd_network_link_get_required_for_online(info
->ifindex
);
2353 r
= table_add_many(table
,
2355 TABLE_STRING
, "Required For Online:",
2358 return table_log_add_error(r
);
2362 const void *client_id
;
2363 size_t client_id_len
;
2366 r
= sd_dhcp_lease_get_timezone(lease
, &tz
);
2368 r
= table_add_many(table
,
2370 TABLE_STRING
, "Time Zone:",
2373 return table_log_add_error(r
);
2376 r
= sd_dhcp_lease_get_client_id(lease
, &client_id
, &client_id_len
);
2378 _cleanup_free_
char *id
= NULL
;
2380 r
= sd_dhcp_client_id_to_string(client_id
, client_id_len
, &id
);
2382 r
= table_add_many(table
,
2384 TABLE_STRING
, "DHCP4 Client ID:",
2387 return table_log_add_error(r
);
2392 r
= sd_network_link_get_dhcp6_client_iaid_string(info
->ifindex
, &iaid
);
2394 r
= table_add_many(table
,
2396 TABLE_STRING
, "DHCP6 Client IAID:",
2397 TABLE_STRING
, iaid
);
2399 return table_log_add_error(r
);
2402 r
= sd_network_link_get_dhcp6_client_duid_string(info
->ifindex
, &duid
);
2404 r
= table_add_many(table
,
2406 TABLE_STRING
, "DHCP6 Client DUID:",
2407 TABLE_STRING
, duid
);
2409 return table_log_add_error(r
);
2412 r
= dump_lldp_neighbors(table
, "Connected To:", info
->ifindex
);
2416 r
= dump_dhcp_leases(table
, "Offered DHCP leases:", bus
, info
);
2420 r
= dump_statistics(table
, info
);
2424 r
= table_print(table
, NULL
);
2426 return table_log_print_error(r
);
2428 return show_logs(info
);
2431 static int system_status(sd_netlink
*rtnl
, sd_hwdb
*hwdb
) {
2432 _cleanup_free_
char *operational_state
= NULL
, *online_state
= NULL
;
2433 _cleanup_strv_free_
char **dns
= NULL
, **ntp
= NULL
, **search_domains
= NULL
, **route_domains
= NULL
;
2434 const char *on_color_operational
, *on_color_online
;
2435 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2441 (void) sd_network_get_operational_state(&operational_state
);
2442 operational_state_to_color(NULL
, operational_state
, &on_color_operational
, NULL
);
2444 (void) sd_network_get_online_state(&online_state
);
2445 online_state_to_color(online_state
, &on_color_online
, NULL
);
2447 table
= table_new("dot", "key", "value");
2452 table_set_width(table
, 0);
2454 assert_se(cell
= table_get_cell(table
, 0, 0));
2455 (void) table_set_ellipsize_percent(table
, cell
, 100);
2457 assert_se(cell
= table_get_cell(table
, 0, 1));
2458 (void) table_set_align_percent(table
, cell
, 100);
2459 (void) table_set_ellipsize_percent(table
, cell
, 100);
2461 table_set_header(table
, false);
2463 r
= table_add_many(table
,
2464 TABLE_STRING
, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
),
2465 TABLE_SET_COLOR
, on_color_operational
,
2466 TABLE_STRING
, "State:",
2467 TABLE_STRING
, strna(operational_state
),
2468 TABLE_SET_COLOR
, on_color_operational
,
2470 TABLE_STRING
, "Online state:",
2471 TABLE_STRING
, online_state
?: "unknown",
2472 TABLE_SET_COLOR
, on_color_online
);
2474 return table_log_add_error(r
);
2476 r
= dump_addresses(rtnl
, NULL
, table
, 0);
2479 r
= dump_gateways(rtnl
, hwdb
, table
, 0);
2483 (void) sd_network_get_dns(&dns
);
2484 r
= dump_list(table
, "DNS:", dns
);
2488 (void) sd_network_get_search_domains(&search_domains
);
2489 r
= dump_list(table
, "Search Domains:", search_domains
);
2493 (void) sd_network_get_route_domains(&route_domains
);
2494 r
= dump_list(table
, "Route Domains:", route_domains
);
2498 (void) sd_network_get_ntp(&ntp
);
2499 r
= dump_list(table
, "NTP:", ntp
);
2503 r
= table_print(table
, NULL
);
2505 return table_log_print_error(r
);
2507 return show_logs(NULL
);
2510 static int link_status(int argc
, char *argv
[], void *userdata
) {
2511 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2512 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2513 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
2514 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2517 r
= acquire_bus(&bus
);
2521 if (arg_json_format_flags
!= JSON_FORMAT_OFF
) {
2522 if (arg_all
|| argc
<= 1)
2523 return dump_manager_description(bus
);
2525 return dump_link_description(bus
, strv_skip(argv
, 1));
2528 pager_open(arg_pager_flags
);
2530 r
= sd_netlink_open(&rtnl
);
2532 return log_error_errno(r
, "Failed to connect to netlink: %m");
2534 r
= sd_hwdb_new(&hwdb
);
2536 log_debug_errno(r
, "Failed to open hardware database: %m");
2539 c
= acquire_link_info(bus
, rtnl
, NULL
, &links
);
2541 return system_status(rtnl
, hwdb
);
2543 c
= acquire_link_info(bus
, rtnl
, argv
+ 1, &links
);
2547 for (int i
= 0; i
< c
; i
++) {
2549 fputc('\n', stdout
);
2551 link_status_one(bus
, rtnl
, hwdb
, links
+ i
);
2557 static char *lldp_capabilities_to_string(uint16_t x
) {
2558 static const char characters
[] = {
2559 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2564 ret
= new(char, ELEMENTSOF(characters
) + 1);
2568 for (i
= 0; i
< ELEMENTSOF(characters
); i
++)
2569 ret
[i
] = (x
& (1U << i
)) ? characters
[i
] : '.';
2575 static void lldp_capabilities_legend(uint16_t x
) {
2576 unsigned cols
= columns();
2577 static const char* const table
[] = {
2581 "w - WLAN Access Point",
2584 "d - DOCSIS cable device",
2586 "c - Customer VLAN",
2588 "m - Two-port MAC Relay (TPMR)",
2594 printf("\nCapability Flags:\n");
2595 for (unsigned w
= 0, i
= 0; i
< ELEMENTSOF(table
); i
++)
2596 if (x
& (1U << i
) || arg_all
) {
2599 newline
= w
+ strlen(table
[i
]) + (w
== 0 ? 0 : 2) > cols
;
2602 w
+= printf("%s%s%s", newline
? "\n" : "", w
== 0 ? "" : "; ", table
[i
]);
2607 static int link_lldp_status(int argc
, char *argv
[], void *userdata
) {
2608 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2609 _cleanup_(link_info_array_freep
) LinkInfo
*links
= NULL
;
2610 _cleanup_(table_unrefp
) Table
*table
= NULL
;
2615 r
= sd_netlink_open(&rtnl
);
2617 return log_error_errno(r
, "Failed to connect to netlink: %m");
2619 c
= acquire_link_info(NULL
, rtnl
, argc
> 1 ? argv
+ 1 : NULL
, &links
);
2623 pager_open(arg_pager_flags
);
2625 table
= table_new("link",
2630 "port-description");
2635 table_set_width(table
, 0);
2637 table_set_header(table
, arg_legend
);
2639 assert_se(cell
= table_get_cell(table
, 0, 3));
2640 table_set_minimum_width(table
, cell
, 11);
2641 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
2643 FOREACH_ARRAY(link
, links
, c
) {
2644 _cleanup_fclose_
FILE *f
= NULL
;
2646 r
= open_lldp_neighbors(link
->ifindex
, &f
);
2650 log_warning_errno(r
, "Failed to open LLDP data for %i, ignoring: %m", link
->ifindex
);
2655 const char *chassis_id
= NULL
, *port_id
= NULL
, *system_name
= NULL
, *port_description
= NULL
;
2656 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
2657 _cleanup_free_
char *capabilities
= NULL
;
2660 r
= next_lldp_neighbor(f
, &n
);
2662 log_warning_errno(r
, "Failed to read neighbor data: %m");
2668 (void) sd_lldp_neighbor_get_chassis_id_as_string(n
, &chassis_id
);
2669 (void) sd_lldp_neighbor_get_port_id_as_string(n
, &port_id
);
2670 (void) sd_lldp_neighbor_get_system_name(n
, &system_name
);
2671 (void) sd_lldp_neighbor_get_port_description(n
, &port_description
);
2673 if (sd_lldp_neighbor_get_enabled_capabilities(n
, &cc
) >= 0) {
2674 capabilities
= lldp_capabilities_to_string(cc
);
2678 r
= table_add_many(table
,
2679 TABLE_STRING
, link
->name
,
2680 TABLE_STRING
, chassis_id
,
2681 TABLE_STRING
, system_name
,
2682 TABLE_STRING
, capabilities
,
2683 TABLE_STRING
, port_id
,
2684 TABLE_STRING
, port_description
);
2686 return table_log_add_error(r
);
2692 r
= table_print(table
, NULL
);
2694 return table_log_print_error(r
);
2697 lldp_capabilities_legend(all
);
2698 printf("\n%i neighbors listed.\n", m
);
2704 static int link_delete_send_message(sd_netlink
*rtnl
, int index
) {
2705 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2711 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_DELLINK
, index
);
2713 return rtnl_log_create_error(r
);
2715 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2722 static int link_up_down_send_message(sd_netlink
*rtnl
, char *command
, int index
) {
2723 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2729 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_SETLINK
, index
);
2731 return rtnl_log_create_error(r
);
2733 if (streq(command
, "up"))
2734 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2736 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2738 return log_error_errno(r
, "Could not set link flags: %m");
2740 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
2747 static int link_up_down(int argc
, char *argv
[], void *userdata
) {
2748 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2749 _cleanup_set_free_ Set
*indexes
= NULL
;
2753 r
= sd_netlink_open(&rtnl
);
2755 return log_error_errno(r
, "Failed to connect to netlink: %m");
2757 indexes
= set_new(NULL
);
2761 for (int i
= 1; i
< argc
; i
++) {
2762 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2766 r
= set_put(indexes
, INT_TO_PTR(index
));
2771 SET_FOREACH(p
, indexes
) {
2772 index
= PTR_TO_INT(p
);
2773 r
= link_up_down_send_message(rtnl
, argv
[0], index
);
2775 return log_error_errno(r
, "Failed to bring %s interface %s: %m",
2776 argv
[0], FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2782 static int link_delete(int argc
, char *argv
[], void *userdata
) {
2783 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2784 _cleanup_set_free_ Set
*indexes
= NULL
;
2788 r
= sd_netlink_open(&rtnl
);
2790 return log_error_errno(r
, "Failed to connect to netlink: %m");
2792 indexes
= set_new(NULL
);
2796 for (int i
= 1; i
< argc
; i
++) {
2797 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2801 r
= set_put(indexes
, INT_TO_PTR(index
));
2806 SET_FOREACH(p
, indexes
) {
2807 index
= PTR_TO_INT(p
);
2808 r
= link_delete_send_message(rtnl
, index
);
2810 return log_error_errno(r
, "Failed to delete interface %s: %m",
2811 FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
));
2817 static int link_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2818 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2825 r
= bus_call_method(bus
, bus_network_mgr
, "RenewLink", &error
, NULL
, "i", index
);
2827 return log_error_errno(r
, "Failed to renew dynamic configuration of interface %s: %s",
2828 name
, bus_error_message(&error
, r
));
2833 static int link_renew(int argc
, char *argv
[], void *userdata
) {
2834 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2835 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2836 int index
, k
= 0, r
;
2838 r
= acquire_bus(&bus
);
2842 for (int i
= 1; i
< argc
; i
++) {
2843 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2847 r
= link_renew_one(bus
, index
, argv
[i
]);
2848 if (r
< 0 && k
>= 0)
2855 static int link_force_renew_one(sd_bus
*bus
, int index
, const char *name
) {
2856 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2863 r
= bus_call_method(bus
, bus_network_mgr
, "ForceRenewLink", &error
, NULL
, "i", index
);
2865 return log_error_errno(r
, "Failed to force renew dynamic configuration of interface %s: %s",
2866 name
, bus_error_message(&error
, r
));
2871 static int link_force_renew(int argc
, char *argv
[], void *userdata
) {
2872 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2873 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2876 r
= acquire_bus(&bus
);
2880 for (int i
= 1; i
< argc
; i
++) {
2881 int index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2885 r
= link_force_renew_one(bus
, index
, argv
[i
]);
2886 if (r
< 0 && k
>= 0)
2893 static int verb_reload(int argc
, char *argv
[], void *userdata
) {
2894 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2895 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2898 r
= acquire_bus(&bus
);
2902 r
= bus_call_method(bus
, bus_network_mgr
, "Reload", &error
, NULL
, NULL
);
2904 return log_error_errno(r
, "Failed to reload network settings: %s", bus_error_message(&error
, r
));
2909 static int verb_reconfigure(int argc
, char *argv
[], void *userdata
) {
2910 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2911 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2912 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
2913 _cleanup_set_free_ Set
*indexes
= NULL
;
2917 r
= acquire_bus(&bus
);
2921 indexes
= set_new(NULL
);
2925 for (int i
= 1; i
< argc
; i
++) {
2926 index
= rtnl_resolve_interface_or_warn(&rtnl
, argv
[i
]);
2930 r
= set_put(indexes
, INT_TO_PTR(index
));
2935 SET_FOREACH(p
, indexes
) {
2936 index
= PTR_TO_INT(p
);
2937 r
= bus_call_method(bus
, bus_network_mgr
, "ReconfigureLink", &error
, NULL
, "i", index
);
2939 return log_error_errno(r
, "Failed to reconfigure network interface %s: %s",
2940 FORMAT_IFNAME_FULL(index
, FORMAT_IFNAME_IFINDEX
),
2941 bus_error_message(&error
, r
));
2947 typedef enum ReloadFlags
{
2948 RELOAD_NETWORKD
= 1 << 0,
2949 RELOAD_UDEVD
= 1 << 1,
2952 static int get_config_files_by_name(const char *name
, char **ret_path
, char ***ret_dropins
) {
2953 _cleanup_free_
char *path
= NULL
;
2959 STRV_FOREACH(i
, NETWORK_DIRS
) {
2960 _cleanup_free_
char *p
= NULL
;
2962 p
= path_join(*i
, name
);
2966 r
= RET_NERRNO(access(p
, F_OK
));
2973 log_debug_errno(r
, "Failed to determine whether '%s' exists, ignoring: %m", p
);
2980 _cleanup_free_
char *dropin_dirname
= NULL
;
2982 dropin_dirname
= strjoin(name
, ".d");
2983 if (!dropin_dirname
)
2986 r
= conf_files_list_dropins(ret_dropins
, dropin_dirname
, /* root = */ NULL
, NETWORK_DIRS
);
2991 *ret_path
= TAKE_PTR(path
);
2996 static int get_dropin_by_name(
2998 char * const *dropins
,
3005 STRV_FOREACH(i
, dropins
)
3006 if (path_equal_filename(*i
, name
)) {
3007 _cleanup_free_
char *d
= NULL
;
3021 static int get_network_files_by_link(
3025 char ***ret_dropins
) {
3027 _cleanup_strv_free_
char **dropins
= NULL
;
3028 _cleanup_free_
char *path
= NULL
;
3034 assert(ret_dropins
);
3036 ifindex
= rtnl_resolve_interface_or_warn(rtnl
, link
);
3040 r
= sd_network_link_get_network_file(ifindex
, &path
);
3042 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
3043 "Link '%s' has no associated network file.", link
);
3045 return log_error_errno(r
, "Failed to get network file for link '%s': %m", link
);
3047 r
= sd_network_link_get_network_file_dropins(ifindex
, &dropins
);
3048 if (r
< 0 && r
!= -ENODATA
)
3049 return log_error_errno(r
, "Failed to get network drop-ins for link '%s': %m", link
);
3051 *ret_path
= TAKE_PTR(path
);
3052 *ret_dropins
= TAKE_PTR(dropins
);
3057 static int get_link_files_by_link(const char *link
, char **ret_path
, char ***ret_dropins
) {
3058 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3059 _cleanup_strv_free_
char **dropins_split
= NULL
;
3060 _cleanup_free_
char *p
= NULL
;
3061 const char *path
, *dropins
;
3066 assert(ret_dropins
);
3068 r
= sd_device_new_from_ifname(&device
, link
);
3070 return log_error_errno(r
, "Failed to create sd-device object for link '%s': %m", link
);
3072 r
= sd_device_get_property_value(device
, "ID_NET_LINK_FILE", &path
);
3074 return log_error_errno(r
, "Link '%s' has no associated link file.", link
);
3076 return log_error_errno(r
, "Failed to get link file for link '%s': %m", link
);
3078 r
= sd_device_get_property_value(device
, "ID_NET_LINK_FILE_DROPINS", &dropins
);
3079 if (r
< 0 && r
!= -ENOENT
)
3080 return log_error_errno(r
, "Failed to get link drop-ins for link '%s': %m", link
);
3082 r
= strv_split_full(&dropins_split
, dropins
, ":", EXTRACT_CUNESCAPE
);
3084 return log_error_errno(r
, "Failed to parse link drop-ins for link '%s': %m", link
);
3091 *ret_path
= TAKE_PTR(p
);
3092 *ret_dropins
= TAKE_PTR(dropins_split
);
3097 static int get_config_files_by_link_config(
3098 const char *link_config
,
3101 char ***ret_dropins
,
3102 ReloadFlags
*ret_reload
) {
3104 _cleanup_strv_free_
char **dropins
= NULL
, **link_config_split
= NULL
;
3105 _cleanup_free_
char *path
= NULL
;
3106 const char *ifname
, *type
;
3111 assert(link_config
);
3114 assert(ret_dropins
);
3116 link_config_split
= strv_split(link_config
, ":");
3117 if (!link_config_split
)
3120 n
= strv_length(link_config_split
);
3121 if (n
== 0 || isempty(link_config_split
[0]))
3122 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No link name is given.");
3124 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid link config '%s'.", link_config
);
3126 ifname
= link_config_split
[0];
3127 type
= n
== 2 ? link_config_split
[1] : "network";
3129 if (streq(type
, "network")) {
3130 if (!networkd_is_running())
3131 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
3132 "Cannot get network file for link if systemd-networkd is not running.");
3134 r
= get_network_files_by_link(rtnl
, ifname
, &path
, &dropins
);
3138 reload
= RELOAD_NETWORKD
;
3139 } else if (streq(type
, "link")) {
3140 r
= get_link_files_by_link(ifname
, &path
, &dropins
);
3144 reload
= RELOAD_UDEVD
;
3146 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3147 "Invalid config type '%s' for link '%s'.", type
, ifname
);
3149 *ret_path
= TAKE_PTR(path
);
3150 *ret_dropins
= TAKE_PTR(dropins
);
3153 *ret_reload
= reload
;
3158 static int add_config_to_edit(
3159 EditFileContext
*context
,
3161 char * const *dropins
) {
3163 _cleanup_free_
char *new_path
= NULL
, *dropin_path
= NULL
, *old_dropin
= NULL
;
3164 _cleanup_strv_free_
char **comment_paths
= NULL
;
3169 assert(!arg_drop_in
|| dropins
);
3171 if (path_startswith(path
, "/usr")) {
3172 _cleanup_free_
char *name
= NULL
;
3174 r
= path_extract_filename(path
, &name
);
3176 return log_error_errno(r
, "Failed to extract filename from '%s': %m", path
);
3178 new_path
= path_join(NETWORK_DIRS
[0], name
);
3184 return edit_files_add(context
, new_path
?: path
, path
, NULL
);
3186 r
= get_dropin_by_name(arg_drop_in
, dropins
, &old_dropin
);
3188 return log_error_errno(r
, "Failed to acquire drop-in '%s': %m", arg_drop_in
);
3190 if (r
> 0 && !path_startswith(old_dropin
, "/usr"))
3191 /* An existing drop-in is found and not in /usr/. Let's edit it directly. */
3192 dropin_path
= TAKE_PTR(old_dropin
);
3194 /* No drop-in was found or an existing drop-in resides in /usr/. Let's create
3195 * a new drop-in file. */
3196 dropin_path
= strjoin(new_path
?: path
, ".d/", arg_drop_in
);
3201 comment_paths
= strv_new(path
);
3205 r
= strv_extend_strv(&comment_paths
, dropins
, /* filter_duplicates = */ false);
3209 return edit_files_add(context
, dropin_path
, old_dropin
, comment_paths
);
3212 static int udevd_reload(sd_bus
*bus
) {
3213 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3214 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3215 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
3216 const char *job_path
;
3221 r
= bus_wait_for_jobs_new(bus
, &w
);
3223 return log_error_errno(r
, "Could not watch jobs: %m");
3225 r
= bus_call_method(bus
,
3231 "systemd-udevd.service",
3234 return log_error_errno(r
, "Failed to reload systemd-udevd: %s", bus_error_message(&error
, r
));
3236 r
= sd_bus_message_read(reply
, "o", &job_path
);
3238 return bus_log_parse_error(r
);
3240 r
= bus_wait_for_jobs_one(w
, job_path
, /* quiet = */ true, NULL
);
3241 if (r
== -ENOEXEC
) {
3242 log_debug("systemd-udevd is not running, skipping reload.");
3246 return log_error_errno(r
, "Failed to reload systemd-udevd: %m");
3251 static int verb_edit(int argc
, char *argv
[], void *userdata
) {
3252 _cleanup_(edit_file_context_done
) EditFileContext context
= {
3253 .marker_start
= DROPIN_MARKER_START
,
3254 .marker_end
= DROPIN_MARKER_END
,
3255 .remove_parent
= !!arg_drop_in
,
3257 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
3258 ReloadFlags reload
= 0;
3262 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Cannot edit network config files if not on a tty.");
3264 r
= mac_selinux_init();
3268 STRV_FOREACH(name
, strv_skip(argv
, 1)) {
3269 _cleanup_strv_free_
char **dropins
= NULL
;
3270 _cleanup_free_
char *path
= NULL
;
3271 const char *link_config
;
3273 link_config
= startswith(*name
, "@");
3277 r
= get_config_files_by_link_config(link_config
, &rtnl
, &path
, &dropins
, &flags
);
3283 r
= add_config_to_edit(&context
, path
, dropins
);
3290 if (ENDSWITH_SET(*name
, ".network", ".netdev"))
3291 reload
|= RELOAD_NETWORKD
;
3292 else if (endswith(*name
, ".link"))
3293 reload
|= RELOAD_UDEVD
;
3295 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid network config name '%s'.", *name
);
3297 r
= get_config_files_by_name(*name
, &path
, &dropins
);
3300 return log_error_errno(r
, "Cannot find network config '%s'.", *name
);
3302 log_debug("No existing network config '%s' found, creating a new file.", *name
);
3304 path
= path_join(NETWORK_DIRS
[0], *name
);
3308 r
= edit_files_add(&context
, path
, NULL
, NULL
);
3314 return log_error_errno(r
, "Failed to get the path of network config '%s': %m", *name
);
3316 r
= add_config_to_edit(&context
, path
, dropins
);
3321 r
= do_edit_files_and_install(&context
);
3328 if (!sd_booted() || running_in_chroot() > 0) {
3329 log_debug("System is not booted with systemd or is running in chroot, skipping reload.");
3333 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
3335 r
= sd_bus_open_system(&bus
);
3337 return log_error_errno(r
, "Failed to connect to system bus: %m");
3339 if (FLAGS_SET(reload
, RELOAD_UDEVD
)) {
3340 r
= udevd_reload(bus
);
3345 if (FLAGS_SET(reload
, RELOAD_NETWORKD
)) {
3346 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3348 if (!networkd_is_running()) {
3349 log_debug("systemd-networkd is not running, skipping reload.");
3353 r
= bus_call_method(bus
, bus_network_mgr
, "Reload", &error
, NULL
, NULL
);
3355 return log_error_errno(r
, "Failed to reload systemd-networkd: %s", bus_error_message(&error
, r
));
3361 static int verb_cat(int argc
, char *argv
[], void *userdata
) {
3362 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
3365 pager_open(arg_pager_flags
);
3367 STRV_FOREACH(name
, strv_skip(argv
, 1)) {
3368 _cleanup_strv_free_
char **dropins
= NULL
;
3369 _cleanup_free_
char *path
= NULL
;
3370 const char *link_config
;
3372 link_config
= startswith(*name
, "@");
3374 r
= get_config_files_by_link_config(link_config
, &rtnl
, &path
, &dropins
, /* ret_reload = */ NULL
);
3376 return ret
< 0 ? ret
: r
;
3378 r
= get_config_files_by_name(*name
, &path
, &dropins
);
3380 log_error_errno(r
, "Cannot find network config file '%s'.", *name
);
3381 ret
= ret
< 0 ? ret
: r
;
3385 log_error_errno(r
, "Failed to get the path of network config '%s': %m", *name
);
3386 return ret
< 0 ? ret
: r
;
3390 r
= cat_files(path
, dropins
, /* flags = */ 0);
3392 return ret
< 0 ? ret
: r
;
3398 static int help(void) {
3399 _cleanup_free_
char *link
= NULL
;
3402 r
= terminal_urlify_man("networkctl", "1", &link
);
3406 printf("%s [OPTIONS...] COMMAND\n\n"
3407 "%sQuery and control the networking subsystem.%s\n"
3409 " list [PATTERN...] List links\n"
3410 " status [PATTERN...] Show link status\n"
3411 " lldp [PATTERN...] Show LLDP neighbors\n"
3412 " label Show current address label entries in the kernel\n"
3413 " delete DEVICES... Delete virtual netdevs\n"
3414 " up DEVICES... Bring devices up\n"
3415 " down DEVICES... Bring devices down\n"
3416 " renew DEVICES... Renew dynamic configurations\n"
3417 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
3418 " reconfigure DEVICES... Reconfigure interfaces\n"
3419 " reload Reload .network and .netdev files\n"
3420 " edit FILES|DEVICES... Edit network configuration files\n"
3421 " cat FILES|DEVICES... Show network configuration files\n"
3423 " -h --help Show this help\n"
3424 " --version Show package version\n"
3425 " --no-pager Do not pipe output into a pager\n"
3426 " --no-legend Do not show the headers and footers\n"
3427 " -a --all Show status for all links\n"
3428 " -s --stats Show detailed link statistics\n"
3429 " -l --full Do not ellipsize output\n"
3430 " -n --lines=INTEGER Number of journal entries to show\n"
3431 " --json=pretty|short|off\n"
3432 " Generate JSON output\n"
3433 " --no-reload Do not reload systemd-networkd or systemd-udevd\n"
3434 " after editing network config\n"
3435 " --drop-in=NAME Edit specified drop-in instead of main config file\n"
3436 "\nSee the %s for details.\n",
3437 program_invocation_short_name
,
3445 static int parse_argv(int argc
, char *argv
[]) {
3447 ARG_VERSION
= 0x100,
3455 static const struct option options
[] = {
3456 { "help", no_argument
, NULL
, 'h' },
3457 { "version", no_argument
, NULL
, ARG_VERSION
},
3458 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
3459 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
3460 { "all", no_argument
, NULL
, 'a' },
3461 { "stats", no_argument
, NULL
, 's' },
3462 { "full", no_argument
, NULL
, 'l' },
3463 { "lines", required_argument
, NULL
, 'n' },
3464 { "json", required_argument
, NULL
, ARG_JSON
},
3465 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
3466 { "drop-in", required_argument
, NULL
, ARG_DROP_IN
},
3475 while ((c
= getopt_long(argc
, argv
, "hasln:", options
, NULL
)) >= 0) {
3486 arg_pager_flags
|= PAGER_DISABLE
;
3494 arg_no_reload
= true;
3498 if (isempty(optarg
))
3499 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Empty drop-in file name.");
3501 if (!endswith(optarg
, ".conf")) {
3504 conf
= strjoin(optarg
, ".conf");
3508 free_and_replace(arg_drop_in
, conf
);
3510 r
= free_and_strdup(&arg_drop_in
, optarg
);
3515 if (!filename_is_valid(arg_drop_in
))
3516 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3517 "Invalid drop-in file name '%s'.", arg_drop_in
);
3534 if (safe_atou(optarg
, &arg_lines
) < 0)
3535 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3536 "Failed to parse lines '%s'", optarg
);
3540 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3549 assert_not_reached();
3556 static int networkctl_main(int argc
, char *argv
[]) {
3557 static const Verb verbs
[] = {
3558 { "list", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
|VERB_ONLINE_ONLY
, list_links
},
3559 { "status", VERB_ANY
, VERB_ANY
, VERB_ONLINE_ONLY
, link_status
},
3560 { "lldp", VERB_ANY
, VERB_ANY
, 0, link_lldp_status
},
3561 { "label", 1, 1, 0, list_address_labels
},
3562 { "delete", 2, VERB_ANY
, 0, link_delete
},
3563 { "up", 2, VERB_ANY
, 0, link_up_down
},
3564 { "down", 2, VERB_ANY
, 0, link_up_down
},
3565 { "renew", 2, VERB_ANY
, VERB_ONLINE_ONLY
, link_renew
},
3566 { "forcerenew", 2, VERB_ANY
, VERB_ONLINE_ONLY
, link_force_renew
},
3567 { "reconfigure", 2, VERB_ANY
, VERB_ONLINE_ONLY
, verb_reconfigure
},
3568 { "reload", 1, 1, VERB_ONLINE_ONLY
, verb_reload
},
3569 { "edit", 2, VERB_ANY
, 0, verb_edit
},
3570 { "cat", 2, VERB_ANY
, 0, verb_cat
},
3574 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3577 static int run(int argc
, char* argv
[]) {
3582 r
= parse_argv(argc
, argv
);
3586 return networkctl_main(argc
, argv
);
3589 DEFINE_MAIN_FUNCTION(run
);