]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkctl.c
networkctl: load DHCPv4 lease file and use timezone data from the lease file
[thirdparty/systemd.git] / src / network / networkctl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
ee8c4568 2
ee8c4568 3#include <getopt.h>
d37b7627 4#include <linux/if_addrlabel.h>
1693a943 5#include <net/if.h>
3f6fd1ba 6#include <stdbool.h>
ca78ad1d
ZJS
7#include <sys/stat.h>
8#include <sys/types.h>
9#include <unistd.h>
12ef8fb6 10#include <linux/if_bridge.h>
dca35224 11#include <linux/if_tunnel.h>
ee8c4568 12
335dd8ba 13#include "sd-bus.h"
914d6c09 14#include "sd-device.h"
d9ce1c24 15#include "sd-dhcp-client.h"
3f6fd1ba 16#include "sd-hwdb.h"
34437b4f 17#include "sd-lldp.h"
3f6fd1ba
LP
18#include "sd-netlink.h"
19#include "sd-network.h"
ee8c4568 20
b5efdb8a 21#include "alloc-util.h"
b55818fd 22#include "bond-util.h"
335dd8ba
YW
23#include "bus-common-errors.h"
24#include "bus-error.h"
25#include "bus-util.h"
a8389a33 26#include "bridge-util.h"
914d6c09 27#include "device-util.h"
8d07de25 28#include "escape.h"
3f6fd1ba 29#include "ether-addr-util.h"
c967d2c7 30#include "ethtool-util.h"
34437b4f 31#include "fd-util.h"
ff7c88a2 32#include "format-table.h"
518a66ec 33#include "format-util.h"
e7b38d7d 34#include "geneve-util.h"
0ef84b80 35#include "glob-util.h"
81fd1dd3 36#include "hwdb-util.h"
851ef1ed 37#include "ipvlan-util.h"
ee8c4568 38#include "local-addresses.h"
8752c575 39#include "locale-util.h"
10c71c36 40#include "logs-show.h"
d37b7627 41#include "macro.h"
cf217a09 42#include "macvlan-util.h"
4e2ca442 43#include "main-func.h"
3f6fd1ba 44#include "netlink-util.h"
ef62949a 45#include "network-internal.h"
3f6fd1ba 46#include "pager.h"
6bedfcbb 47#include "parse-util.h"
294bf0c3 48#include "pretty-print.h"
9cd8c766 49#include "set.h"
d308bb99 50#include "socket-netlink.h"
db73295a 51#include "socket-util.h"
760877e9 52#include "sort-util.h"
34437b4f 53#include "sparse-endian.h"
d054f0a4 54#include "stdio-util.h"
8b43440b 55#include "string-table.h"
8752c575 56#include "string-util.h"
3f6fd1ba 57#include "strv.h"
2388b2f4 58#include "strxcpyx.h"
288a74cc 59#include "terminal-util.h"
10c71c36 60#include "unit-def.h"
3f6fd1ba 61#include "verbs.h"
8d07de25 62#include "wifi-util.h"
ee8c4568 63
c82d1bf2
SS
64/* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */
65#define NETDEV_KIND_MAX 64
66
8d07de25
ZJS
67/* use 128 kB for receive socket kernel queue, we shouldn't need more here */
68#define RCVBUF_SIZE (128*1024)
69
0221d68a 70static PagerFlags arg_pager_flags = 0;
ee8c4568 71static bool arg_legend = true;
9085f64a 72static bool arg_all = false;
a459b24f 73static bool arg_stats = false;
10c71c36
YW
74static bool arg_full = false;
75static unsigned arg_lines = 10;
ee8c4568 76
ceb366df 77static void operational_state_to_color(const char *name, const char *state, const char **on, const char **off) {
7e5a080a
LP
78 assert(on);
79 assert(off);
80
ceb366df
ZJS
81 if (STRPTR_IN_SET(state, "routable", "enslaved") ||
82 (streq_ptr(name, "lo") && streq_ptr(state, "carrier"))) {
7e5a080a
LP
83 *on = ansi_highlight_green();
84 *off = ansi_normal();
85 } else if (streq_ptr(state, "degraded")) {
86 *on = ansi_highlight_yellow();
87 *off = ansi_normal();
88 } else
89 *on = *off = "";
90}
91
92static void setup_state_to_color(const char *state, const char **on, const char **off) {
93 assert(on);
94 assert(off);
95
96 if (streq_ptr(state, "configured")) {
97 *on = ansi_highlight_green();
98 *off = ansi_normal();
99 } else if (streq_ptr(state, "configuring")) {
100 *on = ansi_highlight_yellow();
101 *off = ansi_normal();
1cf03a4f 102 } else if (STRPTR_IN_SET(state, "failed", "linger")) {
7e5a080a
LP
103 *on = ansi_highlight_red();
104 *off = ansi_normal();
105 } else
106 *on = *off = "";
107}
108
b24281aa
SS
109typedef struct VxLanInfo {
110 uint32_t vni;
111 uint32_t link;
112
113 int local_family;
114 int group_family;
115
116 union in_addr_union local;
117 union in_addr_union group;
118
119 uint16_t dest_port;
120
22ae6c7d
SS
121 uint8_t proxy;
122 uint8_t learning;
123 uint8_t inerit;
124 uint8_t rsc;
125 uint8_t l2miss;
126 uint8_t l3miss;
127 uint8_t tos;
128 uint8_t ttl;
b24281aa
SS
129} VxLanInfo;
130
6d0c65ff 131typedef struct LinkInfo {
e997c4b0 132 char name[IFNAMSIZ+1];
c82d1bf2 133 char netdev_kind[NETDEV_KIND_MAX];
172353b1 134 sd_device *sd_device;
6d0c65ff 135 int ifindex;
1c4a6088 136 unsigned short iftype;
b147503e 137 struct ether_addr mac_address;
caa8538a 138 struct ether_addr permanent_mac_address;
6cfef1b3 139 uint32_t master;
b147503e 140 uint32_t mtu;
2c73f59b
SS
141 uint32_t min_mtu;
142 uint32_t max_mtu;
0307afc6
SS
143 uint32_t tx_queues;
144 uint32_t rx_queues;
d69b62de 145 uint8_t addr_gen_mode;
e810df37 146 char *qdisc;
511070ee 147 char **alternative_names;
b147503e 148
a459b24f
YW
149 union {
150 struct rtnl_link_stats64 stats64;
151 struct rtnl_link_stats stats;
152 };
153
42a63431
YW
154 uint64_t tx_bitrate;
155 uint64_t rx_bitrate;
335dd8ba 156
c82d1bf2
SS
157 /* bridge info */
158 uint32_t forward_delay;
159 uint32_t hello_time;
160 uint32_t max_age;
161 uint32_t ageing_time;
162 uint32_t stp_state;
12ef8fb6 163 uint32_t cost;
c82d1bf2
SS
164 uint16_t priority;
165 uint8_t mcast_igmp_version;
12ef8fb6 166 uint8_t port_state;
c82d1bf2 167
b24281aa
SS
168 /* vxlan info */
169 VxLanInfo vxlan_info;
170
2b2a1ae6
SS
171 /* vlan info */
172 uint16_t vlan_id;
173
dca35224 174 /* tunnel info */
4e1a1991
SS
175 uint8_t ttl;
176 uint8_t tos;
e7b38d7d
SS
177 uint8_t inherit;
178 uint8_t df;
179 uint8_t csum;
180 uint8_t csum6_tx;
181 uint8_t csum6_rx;
4e1a1991
SS
182 uint16_t tunnel_port;
183 uint32_t vni;
e7b38d7d 184 uint32_t label;
dca35224
SS
185 union in_addr_union local;
186 union in_addr_union remote;
187
b1d6fe70
SS
188 /* bonding info */
189 uint8_t mode;
190 uint32_t miimon;
191 uint32_t updelay;
192 uint32_t downdelay;
193
cf217a09
SS
194 /* macvlan and macvtap info */
195 uint32_t macvlan_mode;
196
851ef1ed
SS
197 /* ipvlan info */
198 uint16_t ipvlan_mode;
199 uint16_t ipvlan_flags;
200
c967d2c7
YW
201 /* ethtool info */
202 int autonegotiation;
50299121 203 uint64_t speed;
c967d2c7
YW
204 Duplex duplex;
205 NetDevPort port;
206
8d07de25 207 /* wlan info */
78404d22 208 enum nl80211_iftype wlan_iftype;
8d07de25
ZJS
209 char *ssid;
210 struct ether_addr bssid;
211
b147503e 212 bool has_mac_address:1;
caa8538a 213 bool has_permanent_mac_address:1;
0307afc6
SS
214 bool has_tx_queues:1;
215 bool has_rx_queues:1;
a459b24f
YW
216 bool has_stats64:1;
217 bool has_stats:1;
335dd8ba 218 bool has_bitrates:1;
c967d2c7 219 bool has_ethtool_link_info:1;
8d07de25 220 bool has_wlan_link_info:1;
4e1a1991 221 bool has_tunnel_ipv4:1;
d69b62de 222 bool has_ipv6_address_generation_mode:1;
172353b1
ZJS
223
224 bool needs_freeing:1;
6d0c65ff
LP
225} LinkInfo;
226
93bab288
YW
227static int link_info_compare(const LinkInfo *a, const LinkInfo *b) {
228 return CMP(a->ifindex, b->ifindex);
6d0c65ff
LP
229}
230
172353b1
ZJS
231static const LinkInfo* link_info_array_free(LinkInfo *array) {
232 for (unsigned i = 0; array && array[i].needs_freeing; i++) {
233 sd_device_unref(array[i].sd_device);
8d07de25 234 free(array[i].ssid);
e810df37 235 free(array[i].qdisc);
511070ee 236 strv_free(array[i].alternative_names);
172353b1
ZJS
237 }
238
239 return mfree(array);
240}
241DEFINE_TRIVIAL_CLEANUP_FUNC(LinkInfo*, link_info_array_free);
242
c82d1bf2
SS
243static int decode_netdev(sd_netlink_message *m, LinkInfo *info) {
244 const char *received_kind;
245 int r;
246
247 assert(m);
248 assert(info);
249
250 r = sd_netlink_message_enter_container(m, IFLA_LINKINFO);
251 if (r < 0)
252 return r;
253
254 r = sd_netlink_message_read_string(m, IFLA_INFO_KIND, &received_kind);
255 if (r < 0)
256 return r;
257
258 r = sd_netlink_message_enter_container(m, IFLA_INFO_DATA);
259 if (r < 0)
260 return r;
261
262 if (streq(received_kind, "bridge")) {
263 (void) sd_netlink_message_read_u32(m, IFLA_BR_FORWARD_DELAY, &info->forward_delay);
264 (void) sd_netlink_message_read_u32(m, IFLA_BR_HELLO_TIME, &info->hello_time);
265 (void) sd_netlink_message_read_u32(m, IFLA_BR_MAX_AGE, &info->max_age);
266 (void) sd_netlink_message_read_u32(m, IFLA_BR_AGEING_TIME, &info->ageing_time);
267 (void) sd_netlink_message_read_u32(m, IFLA_BR_STP_STATE, &info->stp_state);
12ef8fb6 268 (void) sd_netlink_message_read_u32(m, IFLA_BRPORT_COST, &info->cost);
c82d1bf2
SS
269 (void) sd_netlink_message_read_u16(m, IFLA_BR_PRIORITY, &info->priority);
270 (void) sd_netlink_message_read_u8(m, IFLA_BR_MCAST_IGMP_VERSION, &info->mcast_igmp_version);
12ef8fb6 271 (void) sd_netlink_message_read_u8(m, IFLA_BRPORT_STATE, &info->port_state);
b1d6fe70
SS
272 } if (streq(received_kind, "bond")) {
273 (void) sd_netlink_message_read_u8(m, IFLA_BOND_MODE, &info->mode);
274 (void) sd_netlink_message_read_u32(m, IFLA_BOND_MIIMON, &info->miimon);
275 (void) sd_netlink_message_read_u32(m, IFLA_BOND_DOWNDELAY, &info->downdelay);
276 (void) sd_netlink_message_read_u32(m, IFLA_BOND_UPDELAY, &info->updelay);
b24281aa
SS
277 } else if (streq(received_kind, "vxlan")) {
278 (void) sd_netlink_message_read_u32(m, IFLA_VXLAN_ID, &info->vxlan_info.vni);
279
280 r = sd_netlink_message_read_in_addr(m, IFLA_VXLAN_GROUP, &info->vxlan_info.group.in);
281 if (r >= 0)
282 info->vxlan_info.group_family = AF_INET;
283 else {
284 r = sd_netlink_message_read_in6_addr(m, IFLA_VXLAN_GROUP6, &info->vxlan_info.group.in6);
285 if (r >= 0)
286 info->vxlan_info.group_family = AF_INET6;
287 }
288
289 r = sd_netlink_message_read_in_addr(m, IFLA_VXLAN_LOCAL, &info->vxlan_info.local.in);
290 if (r >= 0)
291 info->vxlan_info.local_family = AF_INET;
292 else {
293 r = sd_netlink_message_read_in6_addr(m, IFLA_VXLAN_LOCAL6, &info->vxlan_info.local.in6);
294 if (r >= 0)
295 info->vxlan_info.local_family = AF_INET6;
296 }
297
298 (void) sd_netlink_message_read_u32(m, IFLA_VXLAN_LINK, &info->vxlan_info.link);
299 (void) sd_netlink_message_read_u16(m, IFLA_VXLAN_PORT, &info->vxlan_info.dest_port);
22ae6c7d
SS
300 (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_PROXY, &info->vxlan_info.proxy);
301 (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_LEARNING, &info->vxlan_info.learning);
302 (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_RSC, &info->vxlan_info.rsc);
303 (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_L3MISS, &info->vxlan_info.l3miss);
304 (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_L2MISS, &info->vxlan_info.l2miss);
305 (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_TOS, &info->vxlan_info.tos);
306 (void) sd_netlink_message_read_u8(m, IFLA_VXLAN_TTL, &info->vxlan_info.ttl);
2b2a1ae6
SS
307 } else if (streq(received_kind, "vlan"))
308 (void) sd_netlink_message_read_u16(m, IFLA_VLAN_ID, &info->vlan_id);
dca35224
SS
309 else if (STR_IN_SET(received_kind, "ipip", "sit")) {
310 (void) sd_netlink_message_read_in_addr(m, IFLA_IPTUN_LOCAL, &info->local.in);
311 (void) sd_netlink_message_read_in_addr(m, IFLA_IPTUN_REMOTE, &info->remote.in);
4e1a1991
SS
312 } else if (streq(received_kind, "geneve")) {
313 (void) sd_netlink_message_read_u32(m, IFLA_GENEVE_ID, &info->vni);
314
315 r = sd_netlink_message_read_in_addr(m, IFLA_GENEVE_REMOTE, &info->remote.in);
316 if (r >= 0)
317 info->has_tunnel_ipv4 = true;
318 else
319 (void) sd_netlink_message_read_in6_addr(m, IFLA_GENEVE_REMOTE6, &info->remote.in6);
320
321 (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TTL, &info->ttl);
e7b38d7d 322 (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TTL_INHERIT, &info->inherit);
4e1a1991 323 (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_TOS, &info->tos);
e7b38d7d
SS
324 (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_DF, &info->df);
325 (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_CSUM, &info->csum);
326 (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, &info->csum6_tx);
327 (void) sd_netlink_message_read_u8(m, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, &info->csum6_rx);
4e1a1991 328 (void) sd_netlink_message_read_u16(m, IFLA_GENEVE_PORT, &info->tunnel_port);
e7b38d7d 329 (void) sd_netlink_message_read_u32(m, IFLA_GENEVE_LABEL, &info->label);
4d75ea1e
SS
330 } else if (STR_IN_SET(received_kind, "gre", "gretap", "erspan")) {
331 (void) sd_netlink_message_read_in_addr(m, IFLA_GRE_LOCAL, &info->local.in);
332 (void) sd_netlink_message_read_in_addr(m, IFLA_GRE_REMOTE, &info->remote.in);
ad760bc1
SS
333 } else if (STR_IN_SET(received_kind, "ip6gre", "ip6gretap", "ip6erspan")) {
334 (void) sd_netlink_message_read_in6_addr(m, IFLA_GRE_LOCAL, &info->local.in6);
335 (void) sd_netlink_message_read_in6_addr(m, IFLA_GRE_REMOTE, &info->remote.in6);
5712d689
SS
336 } else if (streq(received_kind, "vti")) {
337 (void) sd_netlink_message_read_in_addr(m, IFLA_VTI_LOCAL, &info->local.in);
338 (void) sd_netlink_message_read_in_addr(m, IFLA_VTI_REMOTE, &info->remote.in);
339 } else if (streq(received_kind, "vti6")) {
340 (void) sd_netlink_message_read_in6_addr(m, IFLA_VTI_LOCAL, &info->local.in6);
341 (void) sd_netlink_message_read_in6_addr(m, IFLA_VTI_REMOTE, &info->remote.in6);
cf217a09
SS
342 } else if (STR_IN_SET(received_kind, "macvlan", "macvtap"))
343 (void) sd_netlink_message_read_u32(m, IFLA_MACVLAN_MODE, &info->macvlan_mode);
851ef1ed
SS
344 else if (streq(received_kind, "ipvlan")) {
345 (void) sd_netlink_message_read_u16(m, IFLA_IPVLAN_MODE, &info->ipvlan_mode);
346 (void) sd_netlink_message_read_u16(m, IFLA_IPVLAN_FLAGS, &info->ipvlan_flags);
347 }
c82d1bf2
SS
348
349 strncpy(info->netdev_kind, received_kind, IFNAMSIZ);
350
351 (void) sd_netlink_message_exit_container(m);
352 (void) sd_netlink_message_exit_container(m);
353
354 return 0;
355}
356
0ef84b80 357static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns, bool matched_patterns[]) {
f7581ed6 358 _cleanup_strv_free_ char **altnames = NULL;
e810df37 359 const char *name, *qdisc;
a6962904 360 int ifindex, r;
c82d1bf2 361 uint16_t type;
6d0c65ff 362
e997c4b0
LP
363 assert(m);
364 assert(info);
6d0c65ff 365
e997c4b0
LP
366 r = sd_netlink_message_get_type(m, &type);
367 if (r < 0)
368 return r;
6d0c65ff 369
e997c4b0
LP
370 if (type != RTM_NEWLINK)
371 return 0;
6d0c65ff 372
a6962904 373 r = sd_rtnl_message_link_get_ifindex(m, &ifindex);
e997c4b0
LP
374 if (r < 0)
375 return r;
6d0c65ff 376
e997c4b0
LP
377 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &name);
378 if (r < 0)
379 return r;
6d0c65ff 380
f7581ed6
YW
381 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
382 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENODATA))
383 return r;
384
a6962904
YW
385 if (patterns) {
386 char str[DECIMAL_STR_MAX(int)];
0ef84b80
ZJS
387 size_t pos;
388
389 assert(matched_patterns);
a6962904
YW
390
391 xsprintf(str, "%i", ifindex);
0ef84b80
ZJS
392 if (!strv_fnmatch_full(patterns, str, 0, &pos) &&
393 !strv_fnmatch_full(patterns, name, 0, &pos)) {
f7581ed6
YW
394 bool match = false;
395 char **p;
396
397 STRV_FOREACH(p, altnames)
0ef84b80 398 if (strv_fnmatch_full(patterns, *p, 0, &pos)) {
f7581ed6
YW
399 match = true;
400 break;
401 }
402 if (!match)
403 return 0;
404 }
0ef84b80
ZJS
405
406 matched_patterns[pos] = true;
a6962904
YW
407 }
408
b147503e 409 r = sd_rtnl_message_link_get_type(m, &info->iftype);
e997c4b0
LP
410 if (r < 0)
411 return r;
6d0c65ff 412
2388b2f4 413 strscpy(info->name, sizeof info->name, name);
a6962904 414 info->ifindex = ifindex;
f7581ed6 415 info->alternative_names = TAKE_PTR(altnames);
b147503e
LP
416
417 info->has_mac_address =
418 sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &info->mac_address) >= 0 &&
72e551f4 419 memcmp(&info->mac_address, &ETHER_ADDR_NULL, sizeof(struct ether_addr)) != 0;
b147503e 420
caa8538a 421 info->has_permanent_mac_address =
6666c4fa 422 ethtool_get_permanent_macaddr(NULL, info->name, &info->permanent_mac_address) >= 0 &&
caa8538a
YW
423 memcmp(&info->permanent_mac_address, &ETHER_ADDR_NULL, sizeof(struct ether_addr)) != 0 &&
424 memcmp(&info->permanent_mac_address, &info->mac_address, sizeof(struct ether_addr)) != 0;
425
c06ff86e
YW
426 (void) sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu);
427 (void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu);
428 (void) sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu);
2c73f59b 429
0307afc6
SS
430 info->has_rx_queues =
431 sd_netlink_message_read_u32(m, IFLA_NUM_RX_QUEUES, &info->rx_queues) >= 0 &&
432 info->rx_queues > 0;
433
434 info->has_tx_queues =
435 sd_netlink_message_read_u32(m, IFLA_NUM_TX_QUEUES, &info->tx_queues) >= 0 &&
436 info->tx_queues > 0;
437
a459b24f
YW
438 if (sd_netlink_message_read(m, IFLA_STATS64, sizeof info->stats64, &info->stats64) >= 0)
439 info->has_stats64 = true;
440 else if (sd_netlink_message_read(m, IFLA_STATS, sizeof info->stats, &info->stats) >= 0)
441 info->has_stats = true;
442
e810df37
SS
443 r = sd_netlink_message_read_string(m, IFLA_QDISC, &qdisc);
444 if (r >= 0) {
445 info->qdisc = strdup(qdisc);
446 if (!info->qdisc)
447 return log_oom();
448 }
449
6cfef1b3
SS
450 (void) sd_netlink_message_read_u32(m, IFLA_MASTER, &info->master);
451
d69b62de
SS
452 r = sd_netlink_message_enter_container(m, IFLA_AF_SPEC);
453 if (r >= 0) {
454 r = sd_netlink_message_enter_container(m, AF_INET6);
455 if (r >= 0) {
456 r = sd_netlink_message_read_u8(m, IFLA_INET6_ADDR_GEN_MODE, &info->addr_gen_mode);
7f20a9e5
SS
457 if (r >= 0 && IN_SET(info->addr_gen_mode,
458 IN6_ADDR_GEN_MODE_EUI64,
459 IN6_ADDR_GEN_MODE_NONE,
460 IN6_ADDR_GEN_MODE_STABLE_PRIVACY,
461 IN6_ADDR_GEN_MODE_RANDOM))
d69b62de
SS
462 info->has_ipv6_address_generation_mode = true;
463
464 (void) sd_netlink_message_exit_container(m);
465 }
466 (void) sd_netlink_message_exit_container(m);
467 }
468
c82d1bf2
SS
469 /* fill kind info */
470 (void) decode_netdev(m, info);
471
e997c4b0
LP
472 return 1;
473}
474
090c923b
MAL
475static int link_get_property(
476 sd_bus *bus,
477 const LinkInfo *link,
478 sd_bus_error *error,
479 sd_bus_message **reply,
480 const char *iface,
481 const char *propname) {
335dd8ba
YW
482 _cleanup_free_ char *path = NULL, *ifindex_str = NULL;
483 int r;
484
485 if (asprintf(&ifindex_str, "%i", link->ifindex) < 0)
486 return -ENOMEM;
487
488 r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path);
489 if (r < 0)
490 return r;
491
090c923b 492 return sd_bus_call_method(
335dd8ba
YW
493 bus,
494 "org.freedesktop.network1",
495 path,
496 "org.freedesktop.DBus.Properties",
497 "Get",
090c923b
MAL
498 error,
499 reply,
335dd8ba 500 "ss",
090c923b
MAL
501 iface,
502 propname);
503}
504
505static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) {
506 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
507 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
508 int r;
509
510 r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.Link", "BitRates");
335dd8ba 511 if (r < 0) {
4023637a
ZJS
512 bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY) ||
513 sd_bus_error_has_name(&error, BUS_ERROR_SPEED_METER_INACTIVE);
8210a61a
ZJS
514
515 return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING,
516 r, "Failed to query link bit rates: %s", bus_error_message(&error, r));
335dd8ba
YW
517 }
518
42a63431 519 r = sd_bus_message_enter_container(reply, 'v', "(tt)");
335dd8ba
YW
520 if (r < 0)
521 return bus_log_parse_error(r);
522
42a63431 523 r = sd_bus_message_read(reply, "(tt)", &link->tx_bitrate, &link->rx_bitrate);
335dd8ba
YW
524 if (r < 0)
525 return bus_log_parse_error(r);
526
527 r = sd_bus_message_exit_container(reply);
528 if (r < 0)
529 return bus_log_parse_error(r);
530
e813de54 531 link->has_bitrates = link->tx_bitrate != UINT64_MAX && link->rx_bitrate != UINT64_MAX;
335dd8ba
YW
532
533 return 0;
534}
535
516e9c80
ZJS
536static void acquire_ether_link_info(int *fd, LinkInfo *link) {
537 if (ethtool_get_link_info(fd, link->name,
538 &link->autonegotiation,
539 &link->speed,
540 &link->duplex,
541 &link->port) >= 0)
542 link->has_ethtool_link_info = true;
543}
544
8d07de25
ZJS
545static void acquire_wlan_link_info(LinkInfo *link) {
546 _cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL;
547 const char *type = NULL;
78404d22 548 int r, k = 0;
8d07de25
ZJS
549
550 if (link->sd_device)
551 (void) sd_device_get_devtype(link->sd_device, &type);
552 if (!streq_ptr(type, "wlan"))
553 return;
554
555 r = sd_genl_socket_open(&genl);
556 if (r < 0) {
557 log_debug_errno(r, "Failed to open generic netlink socket: %m");
558 return;
559 }
560
561 (void) sd_netlink_inc_rcvbuf(genl, RCVBUF_SIZE);
562
78404d22 563 r = wifi_get_interface(genl, link->ifindex, &link->wlan_iftype, &link->ssid);
8d07de25
ZJS
564 if (r < 0)
565 log_debug_errno(r, "%s: failed to query ssid: %m", link->name);
566
33ebda2e 567 if (link->wlan_iftype == NL80211_IFTYPE_STATION) {
78404d22
YW
568 k = wifi_get_station(genl, link->ifindex, &link->bssid);
569 if (k < 0)
570 log_debug_errno(k, "%s: failed to query bssid: %m", link->name);
571 }
8d07de25
ZJS
572
573 link->has_wlan_link_info = r > 0 || k > 0;
574}
575
335dd8ba 576static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, LinkInfo **ret) {
4afd3348 577 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
172353b1 578 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
c967d2c7 579 _cleanup_close_ int fd = -1;
335dd8ba 580 size_t allocated = 0, c = 0, j;
7e5a080a 581 sd_netlink_message *i;
7d367b45
LP
582 int r;
583
b147503e 584 assert(rtnl);
7d367b45 585 assert(ret);
ee8c4568 586
ee8c4568
LP
587 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
588 if (r < 0)
589 return rtnl_log_create_error(r);
590
1c4baffc 591 r = sd_netlink_message_request_dump(req, true);
ee8c4568
LP
592 if (r < 0)
593 return rtnl_log_create_error(r);
594
1c4baffc 595 r = sd_netlink_call(rtnl, req, 0, &reply);
f647962d
MS
596 if (r < 0)
597 return log_error_errno(r, "Failed to enumerate links: %m");
ee8c4568 598
0ef84b80
ZJS
599 _cleanup_free_ bool *matched_patterns = NULL;
600 if (patterns) {
601 matched_patterns = new0(bool, strv_length(patterns));
602 if (!matched_patterns)
603 return log_oom();
604 }
605
7e5a080a 606 for (i = reply; i; i = sd_netlink_message_next(i)) {
172353b1 607 if (!GREEDY_REALLOC0(links, allocated, c + 2)) /* We keep one trailing one as marker */
7e5a080a 608 return -ENOMEM;
7d367b45 609
0ef84b80 610 r = decode_link(i, links + c, patterns, matched_patterns);
7e5a080a
LP
611 if (r < 0)
612 return r;
c967d2c7
YW
613 if (r == 0)
614 continue;
615
172353b1
ZJS
616 links[c].needs_freeing = true;
617
618 char devid[2 + DECIMAL_STR_MAX(int)];
619 xsprintf(devid, "n%i", links[c].ifindex);
620 (void) sd_device_new_from_device_id(&links[c].sd_device, devid);
621
516e9c80 622 acquire_ether_link_info(&fd, &links[c]);
8d07de25 623 acquire_wlan_link_info(&links[c]);
c967d2c7
YW
624
625 c++;
7e5a080a
LP
626 }
627
0ef84b80
ZJS
628 /* Look if we matched all our arguments that are not globs. It
629 * is OK for a glob to match nothing, but not for an exact argument. */
630 for (size_t pos = 0; pos < strv_length(patterns); pos++) {
631 if (matched_patterns[pos])
632 continue;
633
634 if (string_is_glob(patterns[pos]))
635 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
636 patterns[pos]);
637 else
638 return log_error_errno(SYNTHETIC_ERRNO(ENODEV),
639 "Interface \"%s\" not found.", patterns[pos]);
640 }
641
93bab288 642 typesafe_qsort(links, c, link_info_compare);
7e5a080a 643
335dd8ba
YW
644 if (bus)
645 for (j = 0; j < c; j++)
646 (void) acquire_link_bitrates(bus, links + j);
647
1cc6c93a 648 *ret = TAKE_PTR(links);
7e5a080a 649
0ef84b80
ZJS
650 if (patterns && c == 0)
651 log_warning("No interfaces matched.");
652
7e5a080a 653 return (int) c;
7d367b45 654}
ee8c4568 655
7d367b45 656static int list_links(int argc, char *argv[], void *userdata) {
b147503e 657 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
172353b1 658 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
ff7c88a2
YW
659 _cleanup_(table_unrefp) Table *table = NULL;
660 TableCell *cell;
b147503e
LP
661 int c, i, r;
662
663 r = sd_netlink_open(&rtnl);
664 if (r < 0)
665 return log_error_errno(r, "Failed to connect to netlink: %m");
7d367b45 666
335dd8ba 667 c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
6d0c65ff 668 if (c < 0)
7d367b45
LP
669 return c;
670
0221d68a 671 (void) pager_open(arg_pager_flags);
7d367b45 672
4252171a 673 table = table_new("idx", "link", "type", "operational", "setup");
ff7c88a2
YW
674 if (!table)
675 return log_oom();
676
a42d9490
YW
677 if (arg_full)
678 table_set_width(table, 0);
679
ff7c88a2
YW
680 table_set_header(table, arg_legend);
681
682 assert_se(cell = table_get_cell(table, 0, 0));
683 (void) table_set_minimum_width(table, cell, 3);
684 (void) table_set_weight(table, cell, 0);
81914d9f 685 (void) table_set_ellipsize_percent(table, cell, 100);
ff7c88a2
YW
686 (void) table_set_align_percent(table, cell, 100);
687
688 assert_se(cell = table_get_cell(table, 0, 1));
81914d9f 689 (void) table_set_ellipsize_percent(table, cell, 100);
6d0c65ff
LP
690
691 for (i = 0; i < c; i++) {
ab1525bc 692 _cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
d57c365b
LP
693 const char *on_color_operational, *off_color_operational,
694 *on_color_setup, *off_color_setup;
ee8c4568 695 _cleanup_free_ char *t = NULL;
ee8c4568 696
4abd866d 697 (void) sd_network_link_get_operational_state(links[i].ifindex, &operational_state);
ceb366df 698 operational_state_to_color(links[i].name, operational_state, &on_color_operational, &off_color_operational);
d57c365b 699
33d5013d
LP
700 r = sd_network_link_get_setup_state(links[i].ifindex, &setup_state);
701 if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */
702 setup_state = strdup("unmanaged");
d57c365b 703 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
ee8c4568 704
172353b1 705 t = link_get_type_string(links[i].iftype, links[i].sd_device);
ee8c4568 706
ff7c88a2 707 r = table_add_many(table,
8d0e0af2 708 TABLE_INT, links[i].ifindex,
ff7c88a2 709 TABLE_STRING, links[i].name,
8d0e0af2
YW
710 TABLE_STRING, strna(t),
711 TABLE_STRING, strna(operational_state),
712 TABLE_SET_COLOR, on_color_operational,
713 TABLE_STRING, strna(setup_state),
714 TABLE_SET_COLOR, on_color_setup);
ff7c88a2 715 if (r < 0)
bd17fa8c 716 return table_log_add_error(r);
ee8c4568
LP
717 }
718
ff7c88a2
YW
719 r = table_print(table, NULL);
720 if (r < 0)
721 return log_error_errno(r, "Failed to print table: %m");
722
ee8c4568 723 if (arg_legend)
6d0c65ff 724 printf("\n%i links listed.\n", c);
ee8c4568
LP
725
726 return 0;
727}
728
c09da729 729/* IEEE Organizationally Unique Identifier vendor string */
b147503e 730static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) {
81fd1dd3 731 const char *description;
fbd0b64f 732 char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc;
81fd1dd3
TG
733 int r;
734
735 assert(ret);
c09da729 736
888943fc
LP
737 if (!hwdb)
738 return -EINVAL;
739
81fd1dd3
TG
740 if (!mac)
741 return -EINVAL;
742
c09da729
TG
743 /* skip commonly misused 00:00:00 (Xerox) prefix */
744 if (memcmp(mac, "\0\0\0", 3) == 0)
745 return -EINVAL;
746
d054f0a4
DM
747 xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR,
748 ETHER_ADDR_FORMAT_VAL(*mac));
c09da729 749
81fd1dd3
TG
750 r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description);
751 if (r < 0)
752 return r;
c09da729 753
81fd1dd3
TG
754 desc = strdup(description);
755 if (!desc)
756 return -ENOMEM;
c09da729 757
81fd1dd3
TG
758 *ret = desc;
759
760 return 0;
c09da729
TG
761}
762
69fb1176 763static int get_gateway_description(
1c4baffc 764 sd_netlink *rtnl,
81fd1dd3 765 sd_hwdb *hwdb,
69fb1176
LP
766 int ifindex,
767 int family,
768 union in_addr_union *gateway,
769 char **gateway_description) {
4afd3348 770 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c4baffc 771 sd_netlink_message *m;
c09da729
TG
772 int r;
773
774 assert(rtnl);
775 assert(ifindex >= 0);
4c701096 776 assert(IN_SET(family, AF_INET, AF_INET6));
c09da729
TG
777 assert(gateway);
778 assert(gateway_description);
779
780 r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family);
781 if (r < 0)
782 return r;
783
1c4baffc 784 r = sd_netlink_message_request_dump(req, true);
c09da729
TG
785 if (r < 0)
786 return r;
787
1c4baffc 788 r = sd_netlink_call(rtnl, req, 0, &reply);
c09da729
TG
789 if (r < 0)
790 return r;
791
1c4baffc 792 for (m = reply; m; m = sd_netlink_message_next(m)) {
67a46833
YW
793 union in_addr_union gw = IN_ADDR_NULL;
794 struct ether_addr mac = ETHER_ADDR_NULL;
c09da729
TG
795 uint16_t type;
796 int ifi, fam;
797
1c4baffc 798 r = sd_netlink_message_get_errno(m);
c09da729
TG
799 if (r < 0) {
800 log_error_errno(r, "got error: %m");
801 continue;
802 }
803
1c4baffc 804 r = sd_netlink_message_get_type(m, &type);
c09da729
TG
805 if (r < 0) {
806 log_error_errno(r, "could not get type: %m");
807 continue;
808 }
809
810 if (type != RTM_NEWNEIGH) {
811 log_error("type is not RTM_NEWNEIGH");
812 continue;
813 }
814
815 r = sd_rtnl_message_neigh_get_family(m, &fam);
816 if (r < 0) {
817 log_error_errno(r, "could not get family: %m");
818 continue;
819 }
820
821 if (fam != family) {
822 log_error("family is not correct");
823 continue;
824 }
825
826 r = sd_rtnl_message_neigh_get_ifindex(m, &ifi);
827 if (r < 0) {
144232a8 828 log_error_errno(r, "could not get ifindex: %m");
c09da729
TG
829 continue;
830 }
831
832 if (ifindex > 0 && ifi != ifindex)
833 continue;
834
835 switch (fam) {
836 case AF_INET:
1c4baffc 837 r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in);
c09da729
TG
838 if (r < 0)
839 continue;
840
841 break;
842 case AF_INET6:
1c4baffc 843 r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6);
c09da729
TG
844 if (r < 0)
845 continue;
846
847 break;
848 default:
849 continue;
850 }
851
852 if (!in_addr_equal(fam, &gw, gateway))
853 continue;
854
49808e0e 855 r = sd_netlink_message_read(m, NDA_LLADDR, sizeof(mac), &mac);
c09da729
TG
856 if (r < 0)
857 continue;
858
859 r = ieee_oui(hwdb, &mac, gateway_description);
860 if (r < 0)
861 continue;
862
863 return 0;
864 }
865
866 return -ENODATA;
867}
868
536cdd07
YW
869static int dump_list(Table *table, const char *prefix, char * const *l) {
870 int r;
871
872 if (strv_isempty(l))
873 return 0;
874
875 r = table_add_many(table,
876 TABLE_EMPTY,
877 TABLE_STRING, prefix,
878 TABLE_STRV, l);
879 if (r < 0)
880 return table_log_add_error(r);
881
882 return 0;
883}
884
69fb1176 885static int dump_gateways(
1c4baffc 886 sd_netlink *rtnl,
81fd1dd3 887 sd_hwdb *hwdb,
98d5bef3 888 Table *table,
69fb1176 889 int ifindex) {
b6a3ca6d 890 _cleanup_free_ struct local_address *local = NULL;
536cdd07 891 _cleanup_strv_free_ char **buf = NULL;
b6a3ca6d 892 int r, n, i;
c09da729 893
837f57da 894 assert(rtnl);
98d5bef3 895 assert(table);
837f57da 896
b6a3ca6d 897 n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local);
536cdd07 898 if (n <= 0)
b6a3ca6d 899 return n;
c09da729 900
b6a3ca6d 901 for (i = 0; i < n; i++) {
98d5bef3 902 _cleanup_free_ char *gateway = NULL, *description = NULL, *with_description = NULL;
536cdd07 903 char name[IF_NAMESIZE+1];
c09da729 904
b6a3ca6d 905 r = in_addr_to_string(local[i].family, &local[i].address, &gateway);
c09da729 906 if (r < 0)
b6a3ca6d 907 return r;
c09da729 908
69fb1176 909 r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description);
c09da729 910 if (r < 0)
536cdd07 911 log_debug_errno(r, "Could not get description of gateway, ignoring: %m");
c09da729 912
98d5bef3
YW
913 if (description) {
914 with_description = strjoin(gateway, " (", description, ")");
915 if (!with_description)
536cdd07 916 return log_oom();
98d5bef3 917 }
1693a943 918
536cdd07
YW
919 /* Show interface name for the entry if we show entries for all interfaces */
920 r = strv_extendf(&buf, "%s%s%s",
921 with_description ?: gateway,
922 ifindex <= 0 ? " on " : "",
923 ifindex <= 0 ? format_ifname_full(local[i].ifindex, name, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
98d5bef3 924 if (r < 0)
536cdd07 925 return log_oom();
c09da729
TG
926 }
927
536cdd07 928 return dump_list(table, "Gateway:", buf);
c09da729
TG
929}
930
69fb1176 931static int dump_addresses(
1c4baffc 932 sd_netlink *rtnl,
98d5bef3 933 Table *table,
69fb1176
LP
934 int ifindex) {
935
ee8c4568 936 _cleanup_free_ struct local_address *local = NULL;
cdf01b36 937 _cleanup_free_ char *dhcp4_address = NULL;
536cdd07 938 _cleanup_strv_free_ char **buf = NULL;
ee8c4568
LP
939 int r, n, i;
940
837f57da 941 assert(rtnl);
98d5bef3 942 assert(table);
837f57da 943
1d050e1e 944 n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local);
536cdd07 945 if (n <= 0)
ee8c4568
LP
946 return n;
947
cdf01b36
SS
948 (void) sd_network_link_get_dhcp4_address(ifindex, &dhcp4_address);
949
ee8c4568
LP
950 for (i = 0; i < n; i++) {
951 _cleanup_free_ char *pretty = NULL;
536cdd07 952 char name[IF_NAMESIZE+1];
1693a943 953
98d5bef3
YW
954 r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
955 if (r < 0)
956 return r;
1693a943 957
cdf01b36
SS
958 if (dhcp4_address && streq(pretty, dhcp4_address)) {
959 _cleanup_free_ char *p = NULL;
960
961 p = pretty;
962 pretty = strjoin(pretty , " (DHCP4)");
963 if (!pretty)
964 return log_oom();
965 }
966
536cdd07
YW
967 r = strv_extendf(&buf, "%s%s%s",
968 pretty,
969 ifindex <= 0 ? " on " : "",
970 ifindex <= 0 ? format_ifname_full(local[i].ifindex, name, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
98d5bef3 971 if (r < 0)
536cdd07 972 return log_oom();
ee8c4568
LP
973 }
974
536cdd07 975 return dump_list(table, "Address:", buf);
ee8c4568
LP
976}
977
d37b7627
SS
978static int dump_address_labels(sd_netlink *rtnl) {
979 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
0232beed 980 _cleanup_(table_unrefp) Table *table = NULL;
d37b7627 981 sd_netlink_message *m;
0232beed 982 TableCell *cell;
d37b7627
SS
983 int r;
984
985 assert(rtnl);
986
987 r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6);
988 if (r < 0)
989 return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m");
990
991 r = sd_netlink_message_request_dump(req, true);
992 if (r < 0)
993 return r;
994
995 r = sd_netlink_call(rtnl, req, 0, &reply);
996 if (r < 0)
997 return r;
998
4252171a 999 table = table_new("label", "prefix/prefixlen");
0232beed 1000 if (!table)
bd17fa8c 1001 return log_oom();
0232beed 1002
a42d9490
YW
1003 if (arg_full)
1004 table_set_width(table, 0);
1005
ad5555b4 1006 r = table_set_sort(table, (size_t) 0, (size_t) SIZE_MAX);
0232beed
YW
1007 if (r < 0)
1008 return r;
1009
1010 assert_se(cell = table_get_cell(table, 0, 0));
1011 (void) table_set_align_percent(table, cell, 100);
81914d9f 1012 (void) table_set_ellipsize_percent(table, cell, 100);
0232beed
YW
1013
1014 assert_se(cell = table_get_cell(table, 0, 1));
1015 (void) table_set_align_percent(table, cell, 100);
d37b7627
SS
1016
1017 for (m = reply; m; m = sd_netlink_message_next(m)) {
1018 _cleanup_free_ char *pretty = NULL;
67a46833 1019 union in_addr_union prefix = IN_ADDR_NULL;
d37b7627
SS
1020 uint8_t prefixlen;
1021 uint32_t label;
1022
1023 r = sd_netlink_message_get_errno(m);
1024 if (r < 0) {
1025 log_error_errno(r, "got error: %m");
1026 continue;
1027 }
1028
1029 r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label);
1030 if (r < 0 && r != -ENODATA) {
1031 log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m");
1032 continue;
1033 }
1034
1035 r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix.in6);
1036 if (r < 0)
1037 continue;
1038
1039 r = in_addr_to_string(AF_INET6, &prefix, &pretty);
1040 if (r < 0)
1041 continue;
1042
1043 r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen);
1044 if (r < 0)
1045 continue;
1046
81914d9f 1047 r = table_add_cell(table, NULL, TABLE_UINT32, &label);
0232beed 1048 if (r < 0)
bd17fa8c 1049 return table_log_add_error(r);
0232beed 1050
8d0e0af2 1051 r = table_add_cell_stringf(table, NULL, "%s/%u", pretty, prefixlen);
0232beed 1052 if (r < 0)
bd17fa8c 1053 return table_log_add_error(r);
d37b7627
SS
1054 }
1055
bd17fa8c
YW
1056 r = table_print(table, NULL);
1057 if (r < 0)
1058 return log_error_errno(r, "Failed to print table: %m");
1059
1060 return 0;
d37b7627
SS
1061}
1062
1063static int list_address_labels(int argc, char *argv[], void *userdata) {
1064 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1065 int r;
1066
1067 r = sd_netlink_open(&rtnl);
1068 if (r < 0)
1069 return log_error_errno(r, "Failed to connect to netlink: %m");
1070
1071 dump_address_labels(rtnl);
1072
1073 return 0;
1074}
1075
837f57da
LP
1076static int open_lldp_neighbors(int ifindex, FILE **ret) {
1077 _cleanup_free_ char *p = NULL;
1078 FILE *f;
1079
1080 if (asprintf(&p, "/run/systemd/netif/lldp/%i", ifindex) < 0)
1081 return -ENOMEM;
1082
1083 f = fopen(p, "re");
1084 if (!f)
1085 return -errno;
1086
1087 *ret = f;
1088 return 0;
1089}
1090
1091static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) {
1092 _cleanup_free_ void *raw = NULL;
1093 size_t l;
1094 le64_t u;
1095 int r;
1096
1097 assert(f);
1098 assert(ret);
1099
1100 l = fread(&u, 1, sizeof(u), f);
1101 if (l == 0 && feof(f))
1102 return 0;
1103 if (l != sizeof(u))
1104 return -EBADMSG;
1105
d23c3e4c
FB
1106 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
1107 if (le64toh(u) >= 4096)
1108 return -EBADMSG;
1109
837f57da
LP
1110 raw = new(uint8_t, le64toh(u));
1111 if (!raw)
1112 return -ENOMEM;
1113
1114 if (fread(raw, 1, le64toh(u), f) != le64toh(u))
1115 return -EBADMSG;
1116
1117 r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u));
1118 if (r < 0)
1119 return r;
1120
1121 return 1;
1122}
1123
98d5bef3 1124static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) {
536cdd07 1125 _cleanup_strv_free_ char **buf = NULL;
837f57da 1126 _cleanup_fclose_ FILE *f = NULL;
536cdd07 1127 int r;
837f57da 1128
98d5bef3 1129 assert(table);
837f57da
LP
1130 assert(prefix);
1131 assert(ifindex > 0);
1132
1133 r = open_lldp_neighbors(ifindex, &f);
98d5bef3
YW
1134 if (r == -ENOENT)
1135 return 0;
837f57da
LP
1136 if (r < 0)
1137 return r;
1138
1139 for (;;) {
1140 const char *system_name = NULL, *port_id = NULL, *port_description = NULL;
1141 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
1142
1143 r = next_lldp_neighbor(f, &n);
1144 if (r < 0)
1145 return r;
1146 if (r == 0)
1147 break;
1148
837f57da
LP
1149 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
1150 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
1151 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
1152
536cdd07
YW
1153 r = strv_extendf(&buf, "%s on port %s%s%s%s",
1154 strna(system_name),
1155 strna(port_id),
1156 isempty(port_description) ? "" : " (",
1157 strempty(port_description),
1158 isempty(port_description) ? "" : ")");
98d5bef3 1159 if (r < 0)
536cdd07 1160 return log_oom();
837f57da
LP
1161 }
1162
536cdd07 1163 return dump_list(table, prefix, buf);
837f57da
LP
1164}
1165
d9ce1c24
MAL
1166static int dump_dhcp_leases(Table *table, const char *prefix, sd_bus *bus, const LinkInfo *link) {
1167 _cleanup_strv_free_ char **buf = NULL;
1168 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1169 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1170 int r;
1171
1172 r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.DHCPServer", "Leases");
1173 if (r < 0) {
1174 bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY);
1175
1176 log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING,
1177 r, "Failed to query link DHCP leases: %s", bus_error_message(&error, r));
1178 return 0;
1179 }
1180
1181 r = sd_bus_message_enter_container(reply, 'v', "a(uayayayayt)");
1182 if (r < 0)
1183 return bus_log_parse_error(r);
1184
1185 r = sd_bus_message_enter_container(reply, 'a', "(uayayayayt)");
1186 if (r < 0)
1187 return bus_log_parse_error(r);
1188
1189 while ((r = sd_bus_message_enter_container(reply, 'r', "uayayayayt")) > 0) {
1190 _cleanup_free_ char *id = NULL, *ip = NULL;
1191 const void *client_id, *addr, *gtw, *hwaddr;
1192 size_t client_id_sz, sz;
1193 uint64_t expiration;
1194 uint32_t family;
1195
1196 r = sd_bus_message_read(reply, "u", &family);
1197 if (r < 0)
1198 return bus_log_parse_error(r);
1199
1200 r = sd_bus_message_read_array(reply, 'y', &client_id, &client_id_sz);
1201 if (r < 0)
1202 return bus_log_parse_error(r);
1203
1204 r = sd_bus_message_read_array(reply, 'y', &addr, &sz);
1205 if (r < 0 || sz != 4)
1206 return bus_log_parse_error(r);
1207
1208 r = sd_bus_message_read_array(reply, 'y', &gtw, &sz);
1209 if (r < 0 || sz != 4)
1210 return bus_log_parse_error(r);
1211
1212 r = sd_bus_message_read_array(reply, 'y', &hwaddr, &sz);
1213 if (r < 0)
1214 return bus_log_parse_error(r);
1215
1216 r = sd_bus_message_read_basic(reply, 't', &expiration);
1217 if (r < 0)
1218 return bus_log_parse_error(r);
1219
1220 r = sd_dhcp_client_id_to_string(client_id, client_id_sz, &id);
1221 if (r < 0)
1222 return bus_log_parse_error(r);
1223
1224 r = in_addr_to_string(family, addr, &ip);
1225 if (r < 0)
1226 return bus_log_parse_error(r);
1227
1228 r = strv_extendf(&buf, "%s (to %s)", ip, id);
1229 if (r < 0)
1230 return log_oom();
1231
1232 r = sd_bus_message_exit_container(reply);
1233 if (r < 0)
1234 return bus_log_parse_error(r);
1235 }
1236 if (r < 0)
1237 return bus_log_parse_error(r);
1238
1239 r = sd_bus_message_exit_container(reply);
1240 if (r < 0)
1241 return bus_log_parse_error(r);
1242
1243 r = sd_bus_message_exit_container(reply);
1244 if (r < 0)
1245 return bus_log_parse_error(r);
1246
1247 if (strv_isempty(buf)) {
1248 r = strv_extendf(&buf, "none");
1249 if (r < 0)
1250 return log_oom();
1251 }
1252
1253 return dump_list(table, prefix, buf);
1254}
1255
98d5bef3 1256static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) {
b295beea 1257 unsigned c;
98d5bef3 1258 int r;
b295beea
LP
1259
1260 assert(prefix);
1261
1262 if (!ifindexes || ifindexes[0] <= 0)
98d5bef3 1263 return 0;
b295beea
LP
1264
1265 for (c = 0; ifindexes[c] > 0; c++) {
8d0e0af2
YW
1266 r = table_add_many(table,
1267 TABLE_EMPTY,
1268 TABLE_STRING, c == 0 ? prefix : "",
1269 TABLE_IFINDEX, ifindexes[c]);
98d5bef3 1270 if (r < 0)
bd17fa8c 1271 return table_log_add_error(r);
b295beea 1272 }
98d5bef3
YW
1273
1274 return 0;
b295beea
LP
1275}
1276
a459b24f 1277#define DUMP_STATS_ONE(name, val_name) \
8d0e0af2
YW
1278 r = table_add_many(table, \
1279 TABLE_EMPTY, \
1280 TABLE_STRING, name ":"); \
a459b24f 1281 if (r < 0) \
bd17fa8c 1282 return table_log_add_error(r); \
8d0e0af2
YW
1283 r = table_add_cell(table, NULL, \
1284 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
a459b24f
YW
1285 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
1286 if (r < 0) \
bd17fa8c 1287 return table_log_add_error(r);
a459b24f
YW
1288
1289static int dump_statistics(Table *table, const LinkInfo *info) {
1290 int r;
1291
1292 if (!arg_stats)
1293 return 0;
1294
1295 if (!info->has_stats64 && !info->has_stats)
1296 return 0;
1297
1298 DUMP_STATS_ONE("Rx Packets", rx_packets);
1299 DUMP_STATS_ONE("Tx Packets", tx_packets);
1300 DUMP_STATS_ONE("Rx Bytes", rx_bytes);
1301 DUMP_STATS_ONE("Tx Bytes", tx_bytes);
1302 DUMP_STATS_ONE("Rx Errors", rx_errors);
1303 DUMP_STATS_ONE("Tx Errors", tx_errors);
1304 DUMP_STATS_ONE("Rx Dropped", rx_dropped);
1305 DUMP_STATS_ONE("Tx Dropped", tx_dropped);
1306 DUMP_STATS_ONE("Multicast Packets", multicast);
1307 DUMP_STATS_ONE("Collisions", collisions);
1308
1309 return 0;
1310}
1311
10c71c36
YW
1312static OutputFlags get_output_flags(void) {
1313 return
1314 arg_all * OUTPUT_SHOW_ALL |
1315 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
1316 colors_enabled() * OUTPUT_COLOR;
1317}
1318
1319static int show_logs(const LinkInfo *info) {
1320 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1321 int r;
1322
1323 if (arg_lines == 0)
1324 return 0;
1325
1326 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
1327 if (r < 0)
1328 return log_error_errno(r, "Failed to open journal: %m");
1329
1330 r = add_match_this_boot(j, NULL);
1331 if (r < 0)
1332 return log_error_errno(r, "Failed to add boot matches: %m");
1333
1334 if (info) {
1335 char m1[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)];
1336 const char *m2, *m3;
1337
1338 /* kernel */
1339 xsprintf(m1, "_KERNEL_DEVICE=n%i", info->ifindex);
1340 /* networkd */
1341 m2 = strjoina("INTERFACE=", info->name);
1342 /* udevd */
1343 m3 = strjoina("DEVICE=", info->name);
1344
1345 (void)(
1346 (r = sd_journal_add_match(j, m1, 0)) ||
1347 (r = sd_journal_add_disjunction(j)) ||
1348 (r = sd_journal_add_match(j, m2, 0)) ||
1349 (r = sd_journal_add_disjunction(j)) ||
1350 (r = sd_journal_add_match(j, m3, 0))
1351 );
1352 if (r < 0)
1353 return log_error_errno(r, "Failed to add link matches: %m");
1354 } else {
1355 r = add_matches_for_unit(j, "systemd-networkd.service");
1356 if (r < 0)
1357 return log_error_errno(r, "Failed to add unit matches: %m");
1358
1359 r = add_matches_for_unit(j, "systemd-networkd-wait-online.service");
1360 if (r < 0)
1361 return log_error_errno(r, "Failed to add unit matches: %m");
1362 }
1363
1364 return show_journal(
1365 stdout,
1366 j,
1367 OUTPUT_SHORT,
1368 0,
1369 0,
1370 arg_lines,
1371 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
1372 NULL);
1373}
1374
69fb1176 1375static int link_status_one(
d9ce1c24 1376 sd_bus *bus,
1c4baffc 1377 sd_netlink *rtnl,
81fd1dd3 1378 sd_hwdb *hwdb,
b147503e
LP
1379 const LinkInfo *info) {
1380
2a71d57f 1381 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **sip = NULL, **search_domains = NULL, **route_domains = NULL;
35cab5f9
YW
1382 _cleanup_free_ char *t = NULL, *network = NULL, *client_id = NULL, *iaid = NULL, *duid = NULL,
1383 *setup_state = NULL, *operational_state = NULL, *lease_file = NULL;
1384 const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL,
1385 *on_color_operational, *off_color_operational, *on_color_setup, *off_color_setup;
b295beea 1386 _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
35cab5f9 1387 _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL;
98d5bef3
YW
1388 _cleanup_(table_unrefp) Table *table = NULL;
1389 TableCell *cell;
b147503e 1390 int r;
9085f64a
LP
1391
1392 assert(rtnl);
b147503e 1393 assert(info);
9085f64a 1394
b147503e 1395 (void) sd_network_link_get_operational_state(info->ifindex, &operational_state);
ceb366df 1396 operational_state_to_color(info->name, operational_state, &on_color_operational, &off_color_operational);
d57c365b 1397
33d5013d
LP
1398 r = sd_network_link_get_setup_state(info->ifindex, &setup_state);
1399 if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */
1400 setup_state = strdup("unmanaged");
d57c365b 1401 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
9085f64a 1402
b147503e
LP
1403 (void) sd_network_link_get_dns(info->ifindex, &dns);
1404 (void) sd_network_link_get_search_domains(info->ifindex, &search_domains);
1405 (void) sd_network_link_get_route_domains(info->ifindex, &route_domains);
1406 (void) sd_network_link_get_ntp(info->ifindex, &ntp);
1f807af6 1407 (void) sd_network_link_get_sip(info->ifindex, &sip);
9085f64a 1408
172353b1
ZJS
1409 if (info->sd_device) {
1410 (void) sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE", &link);
1411 (void) sd_device_get_property_value(info->sd_device, "ID_NET_DRIVER", &driver);
1412 (void) sd_device_get_property_value(info->sd_device, "ID_PATH", &path);
9085f64a 1413
172353b1
ZJS
1414 if (sd_device_get_property_value(info->sd_device, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
1415 (void) sd_device_get_property_value(info->sd_device, "ID_VENDOR", &vendor);
9085f64a 1416
172353b1
ZJS
1417 if (sd_device_get_property_value(info->sd_device, "ID_MODEL_FROM_DATABASE", &model) < 0)
1418 (void) sd_device_get_property_value(info->sd_device, "ID_MODEL", &model);
9085f64a
LP
1419 }
1420
172353b1 1421 t = link_get_type_string(info->iftype, info->sd_device);
b1acce80 1422
4abd866d 1423 (void) sd_network_link_get_network_file(info->ifindex, &network);
df3fb561 1424
4abd866d
LP
1425 (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
1426 (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
0d4ad91d 1427
35cab5f9
YW
1428 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", info->ifindex) < 0)
1429 return log_oom();
1430
1431 (void) dhcp_lease_load(&lease, lease_file);
1432
4252171a 1433 table = table_new("dot", "key", "value");
98d5bef3 1434 if (!table)
bd17fa8c 1435 return log_oom();
98d5bef3 1436
a42d9490
YW
1437 if (arg_full)
1438 table_set_width(table, 0);
1439
81914d9f
YW
1440 assert_se(cell = table_get_cell(table, 0, 0));
1441 (void) table_set_ellipsize_percent(table, cell, 100);
1442
1443 assert_se(cell = table_get_cell(table, 0, 1));
1444 (void) table_set_ellipsize_percent(table, cell, 100);
1445
98d5bef3
YW
1446 table_set_header(table, false);
1447
8d0e0af2
YW
1448 r = table_add_many(table,
1449 TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE),
1450 TABLE_SET_COLOR, on_color_operational);
98d5bef3 1451 if (r < 0)
bd17fa8c 1452 return table_log_add_error(r);
81914d9f 1453 r = table_add_cell_stringf(table, &cell, "%i: %s", info->ifindex, info->name);
98d5bef3 1454 if (r < 0)
bd17fa8c 1455 return table_log_add_error(r);
81914d9f 1456 (void) table_set_align_percent(table, cell, 0);
98d5bef3 1457
8d0e0af2
YW
1458 r = table_add_many(table,
1459 TABLE_EMPTY,
1460 TABLE_EMPTY,
1461 TABLE_STRING, "Link File:",
1462 TABLE_SET_ALIGN_PERCENT, 100,
1463 TABLE_STRING, strna(link),
1464 TABLE_EMPTY,
1465 TABLE_STRING, "Network File:",
1466 TABLE_STRING, strna(network),
1467 TABLE_EMPTY,
1468 TABLE_STRING, "Type:",
1469 TABLE_STRING, strna(t),
1470 TABLE_EMPTY,
1471 TABLE_STRING, "State:");
98d5bef3 1472 if (r < 0)
bd17fa8c 1473 return table_log_add_error(r);
98d5bef3 1474 r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)",
64e7ebde
YW
1475 on_color_operational, strna(operational_state), off_color_operational,
1476 on_color_setup, strna(setup_state), off_color_setup);
98d5bef3 1477 if (r < 0)
bd17fa8c 1478 return table_log_add_error(r);
98d5bef3 1479
536cdd07
YW
1480 r = dump_list(table, "Alternative Names:", info->alternative_names);
1481 if (r < 0)
1482 return r;
511070ee 1483
98d5bef3 1484 if (path) {
8d0e0af2
YW
1485 r = table_add_many(table,
1486 TABLE_EMPTY,
1487 TABLE_STRING, "Path:",
1488 TABLE_STRING, path);
98d5bef3 1489 if (r < 0)
bd17fa8c 1490 return table_log_add_error(r);
98d5bef3
YW
1491 }
1492 if (driver) {
8d0e0af2
YW
1493 r = table_add_many(table,
1494 TABLE_EMPTY,
1495 TABLE_STRING, "Driver:",
1496 TABLE_STRING, driver);
98d5bef3 1497 if (r < 0)
bd17fa8c 1498 return table_log_add_error(r);
98d5bef3
YW
1499 }
1500 if (vendor) {
8d0e0af2
YW
1501 r = table_add_many(table,
1502 TABLE_EMPTY,
1503 TABLE_STRING, "Vendor:",
1504 TABLE_STRING, vendor);
98d5bef3 1505 if (r < 0)
bd17fa8c 1506 return table_log_add_error(r);
98d5bef3
YW
1507 }
1508 if (model) {
8d0e0af2
YW
1509 r = table_add_many(table,
1510 TABLE_EMPTY,
1511 TABLE_STRING, "Model:",
1512 TABLE_STRING, model);
98d5bef3 1513 if (r < 0)
bd17fa8c 1514 return table_log_add_error(r);
98d5bef3 1515 }
9085f64a 1516
b147503e 1517 if (info->has_mac_address) {
888943fc 1518 _cleanup_free_ char *description = NULL;
db73295a 1519 char ea[ETHER_ADDR_TO_STRING_MAX];
888943fc 1520
4abd866d 1521 (void) ieee_oui(hwdb, &info->mac_address, &description);
888943fc 1522
8d0e0af2
YW
1523 r = table_add_many(table,
1524 TABLE_EMPTY,
1525 TABLE_STRING, "HW Address:");
98d5bef3 1526 if (r < 0)
bd17fa8c 1527 return table_log_add_error(r);
98d5bef3 1528 r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
64e7ebde
YW
1529 ether_addr_to_string(&info->mac_address, ea),
1530 description ? " (" : "",
1531 strempty(description),
1532 description ? ")" : "");
98d5bef3 1533 if (r < 0)
bd17fa8c 1534 return table_log_add_error(r);
db73295a 1535 }
9085f64a 1536
caa8538a
YW
1537 if (info->has_permanent_mac_address) {
1538 _cleanup_free_ char *description = NULL;
1539 char ea[ETHER_ADDR_TO_STRING_MAX];
1540
1541 (void) ieee_oui(hwdb, &info->permanent_mac_address, &description);
1542
1543 r = table_add_many(table,
1544 TABLE_EMPTY,
1545 TABLE_STRING, "HW Permanent Address:");
1546 if (r < 0)
bd17fa8c 1547 return table_log_add_error(r);
caa8538a
YW
1548 r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
1549 ether_addr_to_string(&info->permanent_mac_address, ea),
1550 description ? " (" : "",
1551 strempty(description),
1552 description ? ")" : "");
1553 if (r < 0)
bd17fa8c 1554 return table_log_add_error(r);
caa8538a
YW
1555 }
1556
c06ff86e
YW
1557 if (info->mtu > 0) {
1558 char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)];
1559
1560 xsprintf(min_str, "%" PRIu32, info->min_mtu);
1561 xsprintf(max_str, "%" PRIu32, info->max_mtu);
1562
8d0e0af2
YW
1563 r = table_add_many(table,
1564 TABLE_EMPTY,
1565 TABLE_STRING, "MTU:");
98d5bef3 1566 if (r < 0)
bd17fa8c 1567 return table_log_add_error(r);
c06ff86e
YW
1568 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s",
1569 info->mtu,
1570 info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "",
90e29fe1 1571 info->min_mtu > 0 ? "min: " : "",
c06ff86e
YW
1572 info->min_mtu > 0 ? min_str : "",
1573 info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "",
90e29fe1 1574 info->max_mtu > 0 ? "max: " : "",
c06ff86e
YW
1575 info->max_mtu > 0 ? max_str : "",
1576 info->min_mtu > 0 || info->max_mtu > 0 ? ")" : "");
98d5bef3 1577 if (r < 0)
e810df37
SS
1578 return table_log_add_error(r);
1579 }
1580
1581 if (info->qdisc) {
1582 r = table_add_many(table,
1583 TABLE_EMPTY,
1584 TABLE_STRING, "QDisc:",
1585 TABLE_STRING, info->qdisc);
1586 if (r < 0)
6cfef1b3
SS
1587 return table_log_add_error(r);
1588 }
1589
1590 if (info->master > 0) {
1591 r = table_add_many(table,
1592 TABLE_EMPTY,
1593 TABLE_STRING, "Master:",
1594 TABLE_IFINDEX, info->master);
1595 if (r < 0)
bd17fa8c 1596 return table_log_add_error(r);
98d5bef3 1597 }
8eb9058d 1598
d69b62de
SS
1599 if (info->has_ipv6_address_generation_mode) {
1600 static const struct {
1601 const char *mode;
1602 } mode_table[] = {
1603 { "eui64" },
1604 { "none" },
1605 { "stable-privacy" },
1606 { "random" },
1607 };
1608
1609 r = table_add_many(table,
1610 TABLE_EMPTY,
1611 TABLE_STRING, "IPv6 Address Generation Mode:",
1612 TABLE_STRING, mode_table[info->addr_gen_mode]);
1613 if (r < 0)
1614 return table_log_add_error(r);
1615 }
1616
c82d1bf2
SS
1617 if (streq_ptr(info->netdev_kind, "bridge")) {
1618 r = table_add_many(table,
1619 TABLE_EMPTY,
1620 TABLE_STRING, "Forward Delay:",
1621 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->forward_delay),
1622 TABLE_EMPTY,
1623 TABLE_STRING, "Hello Time:",
1624 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->hello_time),
1625 TABLE_EMPTY,
1626 TABLE_STRING, "Max Age:",
1627 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->max_age),
1628 TABLE_EMPTY,
1629 TABLE_STRING, "Ageing Time:",
1630 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->ageing_time),
1631 TABLE_EMPTY,
1632 TABLE_STRING, "Priority:",
1633 TABLE_UINT16, info->priority,
1634 TABLE_EMPTY,
1635 TABLE_STRING, "STP:",
1636 TABLE_BOOLEAN, info->stp_state > 0,
1637 TABLE_EMPTY,
1638 TABLE_STRING, "Multicast IGMP Version:",
12ef8fb6
SS
1639 TABLE_UINT8, info->mcast_igmp_version,
1640 TABLE_EMPTY,
1641 TABLE_STRING, "Cost:",
1642 TABLE_UINT32, info->cost);
c82d1bf2 1643 if (r < 0)
bd17fa8c 1644 return table_log_add_error(r);
b24281aa 1645
12ef8fb6 1646 if (info->port_state <= BR_STATE_BLOCKING) {
12ef8fb6
SS
1647 r = table_add_many(table,
1648 TABLE_EMPTY,
1649 TABLE_STRING, "Port State:",
a8389a33 1650 TABLE_STRING, bridge_state_to_string(info->port_state));
12ef8fb6 1651 }
b1d6fe70 1652 } else if (streq_ptr(info->netdev_kind, "bond")) {
b1d6fe70
SS
1653 r = table_add_many(table,
1654 TABLE_EMPTY,
1655 TABLE_STRING, "Mode:",
b55818fd 1656 TABLE_STRING, bond_mode_to_string(info->mode),
b1d6fe70
SS
1657 TABLE_EMPTY,
1658 TABLE_STRING, "Miimon:",
1659 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->miimon),
1660 TABLE_EMPTY,
1661 TABLE_STRING, "Updelay:",
1662 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->updelay),
1663 TABLE_EMPTY,
1664 TABLE_STRING, "Downdelay:",
1665 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->downdelay));
1666 if (r < 0)
1667 return table_log_add_error(r);
1668
b24281aa 1669 } else if (streq_ptr(info->netdev_kind, "vxlan")) {
22ae6c7d
SS
1670 char ttl[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1671
b24281aa
SS
1672 if (info->vxlan_info.vni > 0) {
1673 r = table_add_many(table,
1674 TABLE_EMPTY,
1675 TABLE_STRING, "VNI:",
1676 TABLE_UINT32, info->vxlan_info.vni);
1677 if (r < 0)
bd17fa8c 1678 return table_log_add_error(r);
b24281aa
SS
1679 }
1680
1681 if (IN_SET(info->vxlan_info.group_family, AF_INET, AF_INET6)) {
659f85a5
SS
1682 const char *p;
1683
1684 r = in_addr_is_multicast(info->vxlan_info.group_family, &info->vxlan_info.group);
1685 if (r <= 0)
1686 p = "Remote:";
1687 else
1688 p = "Group:";
1689
b24281aa
SS
1690 r = table_add_many(table,
1691 TABLE_EMPTY,
659f85a5 1692 TABLE_STRING, p,
b24281aa
SS
1693 info->vxlan_info.group_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR,
1694 &info->vxlan_info.group);
1695 if (r < 0)
bd17fa8c 1696 return table_log_add_error(r);
b24281aa
SS
1697 }
1698
1699 if (IN_SET(info->vxlan_info.local_family, AF_INET, AF_INET6)) {
1700 r = table_add_many(table,
1701 TABLE_EMPTY,
1702 TABLE_STRING, "Local:",
1703 info->vxlan_info.local_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR,
1704 &info->vxlan_info.local);
1705 if (r < 0)
bd17fa8c 1706 return table_log_add_error(r);
b24281aa
SS
1707 }
1708
1709 if (info->vxlan_info.dest_port > 0) {
1710 r = table_add_many(table,
1711 TABLE_EMPTY,
1712 TABLE_STRING, "Destination Port:",
1713 TABLE_UINT16, be16toh(info->vxlan_info.dest_port));
1714 if (r < 0)
bd17fa8c 1715 return table_log_add_error(r);
b24281aa
SS
1716 }
1717
1718 if (info->vxlan_info.link > 0) {
1719 r = table_add_many(table,
1720 TABLE_EMPTY,
1721 TABLE_STRING, "Underlying Device:",
1722 TABLE_IFINDEX, info->vxlan_info.link);
1723 if (r < 0)
bd17fa8c 1724 return table_log_add_error(r);
b24281aa 1725 }
22ae6c7d
SS
1726
1727 r = table_add_many(table,
1728 TABLE_EMPTY,
1729 TABLE_STRING, "Learning:",
1730 TABLE_BOOLEAN, info->vxlan_info.learning);
1731 if (r < 0)
1732 return table_log_add_error(r);
1733
1734 r = table_add_many(table,
1735 TABLE_EMPTY,
1736 TABLE_STRING, "RSC:",
1737 TABLE_BOOLEAN, info->vxlan_info.rsc);
1738 if (r < 0)
1739 return table_log_add_error(r);
1740
1741 r = table_add_many(table,
1742 TABLE_EMPTY,
1743 TABLE_STRING, "L3MISS:",
1744 TABLE_BOOLEAN, info->vxlan_info.l3miss);
1745 if (r < 0)
1746 return table_log_add_error(r);
1747
1748 r = table_add_many(table,
1749 TABLE_EMPTY,
1750 TABLE_STRING, "L2MISS:",
1751 TABLE_BOOLEAN, info->vxlan_info.l2miss);
1752 if (r < 0)
1753 return table_log_add_error(r);
1754
1755 if (info->vxlan_info.tos > 1) {
1756 r = table_add_many(table,
1757 TABLE_EMPTY,
1758 TABLE_STRING, "TOS:",
1759 TABLE_UINT8, info->vxlan_info.tos);
1760 if (r < 0)
1761 return table_log_add_error(r);
1762 }
1763
1764 if (info->vxlan_info.ttl > 0)
1765 xsprintf(ttl, "%" PRIu8, info->vxlan_info.ttl);
1766 else
1767 strcpy(ttl, "auto");
1768
1769 r = table_add_many(table,
1770 TABLE_EMPTY,
1771 TABLE_STRING, "TTL:",
1772 TABLE_STRING, ttl);
1773 if (r < 0)
1774 return table_log_add_error(r);
2b2a1ae6
SS
1775 } else if (streq_ptr(info->netdev_kind, "vlan") && info->vlan_id > 0) {
1776 r = table_add_many(table,
1777 TABLE_EMPTY,
1778 TABLE_STRING, "VLan Id:",
1779 TABLE_UINT16, info->vlan_id);
1780 if (r < 0)
1781 return table_log_add_error(r);
5712d689 1782 } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
dca35224
SS
1783 if (!in_addr_is_null(AF_INET, &info->local)) {
1784 r = table_add_many(table,
1785 TABLE_EMPTY,
1786 TABLE_STRING, "Local:",
1787 TABLE_IN_ADDR, &info->local);
1788 if (r < 0)
1789 return table_log_add_error(r);
1790 }
1791
1792 if (!in_addr_is_null(AF_INET, &info->remote)) {
1793 r = table_add_many(table,
1794 TABLE_EMPTY,
1795 TABLE_STRING, "Remote:",
1796 TABLE_IN_ADDR, &info->remote);
1797 if (r < 0)
1798 return table_log_add_error(r);
1799 }
5712d689 1800 } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
ad760bc1
SS
1801 if (!in_addr_is_null(AF_INET6, &info->local)) {
1802 r = table_add_many(table,
1803 TABLE_EMPTY,
1804 TABLE_STRING, "Local:",
1805 TABLE_IN6_ADDR, &info->local);
1806 if (r < 0)
1807 return table_log_add_error(r);
1808 }
1809
1810 if (!in_addr_is_null(AF_INET6, &info->remote)) {
1811 r = table_add_many(table,
1812 TABLE_EMPTY,
1813 TABLE_STRING, "Remote:",
1814 TABLE_IN6_ADDR, &info->remote);
1815 if (r < 0)
1816 return table_log_add_error(r);
1817 }
4e1a1991
SS
1818 } else if (streq_ptr(info->netdev_kind, "geneve")) {
1819 r = table_add_many(table,
1820 TABLE_EMPTY,
1821 TABLE_STRING, "VNI:",
1822 TABLE_UINT32, info->vni);
1823 if (r < 0)
1824 return table_log_add_error(r);
1825
1826 if (info->has_tunnel_ipv4 && !in_addr_is_null(AF_INET, &info->remote)) {
1827 r = table_add_many(table,
1828 TABLE_EMPTY,
1829 TABLE_STRING, "Remote:",
1830 TABLE_IN_ADDR, &info->remote);
1831 if (r < 0)
1832 return table_log_add_error(r);
1833 } else if (!in_addr_is_null(AF_INET6, &info->remote)) {
1834 r = table_add_many(table,
1835 TABLE_EMPTY,
1836 TABLE_STRING, "Remote:",
1837 TABLE_IN6_ADDR, &info->remote);
1838 if (r < 0)
1839 return table_log_add_error(r);
1840 }
1841
1842 if (info->ttl > 0) {
1843 r = table_add_many(table,
1844 TABLE_EMPTY,
1845 TABLE_STRING, "TTL:",
1846 TABLE_UINT8, info->ttl);
1847 if (r < 0)
1848 return table_log_add_error(r);
1849 }
1850
1851 if (info->tos > 0) {
1852 r = table_add_many(table,
1853 TABLE_EMPTY,
1854 TABLE_STRING, "TOS:",
1855 TABLE_UINT8, info->tos);
1856 if (r < 0)
1857 return table_log_add_error(r);
1858 }
1859
1860 r = table_add_many(table,
1861 TABLE_EMPTY,
1862 TABLE_STRING, "Port:",
1863 TABLE_UINT16, info->tunnel_port);
1864 if (r < 0)
1865 return table_log_add_error(r);
e7b38d7d
SS
1866
1867 r = table_add_many(table,
1868 TABLE_EMPTY,
1869 TABLE_STRING, "Inherit:",
1870 TABLE_STRING, geneve_df_to_string(info->inherit));
1871 if (r < 0)
1872 return table_log_add_error(r);
1873
1874 if (info->df > 0) {
1875 r = table_add_many(table,
1876 TABLE_EMPTY,
1877 TABLE_STRING, "IPDoNotFragment:",
1878 TABLE_UINT8, info->df);
1879 if (r < 0)
1880 return table_log_add_error(r);
1881 }
1882
1883 r = table_add_many(table,
1884 TABLE_EMPTY,
1885 TABLE_STRING, "UDPChecksum:",
1886 TABLE_BOOLEAN, info->csum);
1887 if (r < 0)
1888 return table_log_add_error(r);
1889
1890 r = table_add_many(table,
1891 TABLE_EMPTY,
1892 TABLE_STRING, "UDP6ZeroChecksumTx:",
1893 TABLE_BOOLEAN, info->csum6_tx);
1894 if (r < 0)
1895 return table_log_add_error(r);
1896
1897 r = table_add_many(table,
1898 TABLE_EMPTY,
1899 TABLE_STRING, "UDP6ZeroChecksumRx:",
1900 TABLE_BOOLEAN, info->csum6_rx);
1901 if (r < 0)
1902 return table_log_add_error(r);
1903
1904 if (info->label > 0) {
1905 r = table_add_many(table,
1906 TABLE_EMPTY,
1907 TABLE_STRING, "FlowLabel:",
1908 TABLE_UINT32, info->label);
1909 if (r < 0)
1910 return table_log_add_error(r);
1911 }
cf217a09
SS
1912 } else if (STRPTR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) {
1913 r = table_add_many(table,
1914 TABLE_EMPTY,
1915 TABLE_STRING, "Mode:",
1916 TABLE_STRING, macvlan_mode_to_string(info->macvlan_mode));
1917 if (r < 0)
1918 return table_log_add_error(r);
851ef1ed
SS
1919 } else if (streq_ptr(info->netdev_kind, "ipvlan")) {
1920 _cleanup_free_ char *p = NULL, *s = NULL;
1921
1922 if (info->ipvlan_flags & IPVLAN_F_PRIVATE)
1923 p = strdup("private");
1924 else if (info->ipvlan_flags & IPVLAN_F_VEPA)
1925 p = strdup("vepa");
1926 else
1927 p = strdup("bridge");
1928 if (!p)
1929 log_oom();
1930
1931 s = strjoin(ipvlan_mode_to_string(info->ipvlan_mode), " (", p, ")");
1932 if (!s)
1933 return log_oom();
1934
1935 r = table_add_many(table,
1936 TABLE_EMPTY,
1937 TABLE_STRING, "Mode:",
1938 TABLE_STRING, s);
1939 if (r < 0)
1940 return table_log_add_error(r);
c82d1bf2
SS
1941 }
1942
8d07de25
ZJS
1943 if (info->has_wlan_link_info) {
1944 _cleanup_free_ char *esc = NULL;
1945 char buf[ETHER_ADDR_TO_STRING_MAX];
1946
1947 r = table_add_many(table,
1948 TABLE_EMPTY,
1949 TABLE_STRING, "WiFi access point:");
1950 if (r < 0)
bd17fa8c 1951 return table_log_add_error(r);
8d07de25
ZJS
1952
1953 if (info->ssid)
1954 esc = cescape(info->ssid);
1955
1956 r = table_add_cell_stringf(table, NULL, "%s (%s)",
1957 strnull(esc),
1958 ether_addr_to_string(&info->bssid, buf));
1959 if (r < 0)
bd17fa8c 1960 return table_log_add_error(r);
8d07de25
ZJS
1961 }
1962
335dd8ba 1963 if (info->has_bitrates) {
42a63431 1964 char tx[FORMAT_BYTES_MAX], rx[FORMAT_BYTES_MAX];
335dd8ba 1965
8d0e0af2
YW
1966 r = table_add_many(table,
1967 TABLE_EMPTY,
1968 TABLE_STRING, "Bit Rate (Tx/Rx):");
335dd8ba 1969 if (r < 0)
bd17fa8c 1970 return table_log_add_error(r);
42a63431
YW
1971 r = table_add_cell_stringf(table, NULL, "%sbps/%sbps",
1972 format_bytes_full(tx, sizeof tx, info->tx_bitrate, 0),
1973 format_bytes_full(rx, sizeof rx, info->rx_bitrate, 0));
335dd8ba 1974 if (r < 0)
bd17fa8c 1975 return table_log_add_error(r);
335dd8ba
YW
1976 }
1977
98d5bef3 1978 if (info->has_tx_queues || info->has_rx_queues) {
8d0e0af2
YW
1979 r = table_add_many(table,
1980 TABLE_EMPTY,
1981 TABLE_STRING, "Queue Length (Tx/Rx):");
98d5bef3 1982 if (r < 0)
bd17fa8c 1983 return table_log_add_error(r);
98d5bef3
YW
1984 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues);
1985 if (r < 0)
bd17fa8c 1986 return table_log_add_error(r);
98d5bef3 1987 }
0d4ad91d 1988
c967d2c7
YW
1989 if (info->has_ethtool_link_info) {
1990 const char *duplex = duplex_to_string(info->duplex);
1991 const char *port = port_to_string(info->port);
1992
1993 if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) {
8d0e0af2
YW
1994 r = table_add_many(table,
1995 TABLE_EMPTY,
1996 TABLE_STRING, "Auto negotiation:",
1997 TABLE_BOOLEAN, info->autonegotiation == AUTONEG_ENABLE);
c967d2c7 1998 if (r < 0)
bd17fa8c 1999 return table_log_add_error(r);
c967d2c7
YW
2000 }
2001
2002 if (info->speed > 0) {
8d0e0af2
YW
2003 r = table_add_many(table,
2004 TABLE_EMPTY,
2005 TABLE_STRING, "Speed:",
50299121 2006 TABLE_BPS, info->speed);
c967d2c7 2007 if (r < 0)
bd17fa8c 2008 return table_log_add_error(r);
c967d2c7
YW
2009 }
2010
2011 if (duplex) {
8d0e0af2
YW
2012 r = table_add_many(table,
2013 TABLE_EMPTY,
2014 TABLE_STRING, "Duplex:",
2015 TABLE_STRING, duplex);
c967d2c7 2016 if (r < 0)
bd17fa8c 2017 return table_log_add_error(r);
c967d2c7
YW
2018 }
2019
2020 if (port) {
8d0e0af2
YW
2021 r = table_add_many(table,
2022 TABLE_EMPTY,
2023 TABLE_STRING, "Port:",
2024 TABLE_STRING, port);
c967d2c7 2025 if (r < 0)
bd17fa8c 2026 return table_log_add_error(r);
c967d2c7
YW
2027 }
2028 }
2029
98d5bef3
YW
2030 r = dump_addresses(rtnl, table, info->ifindex);
2031 if (r < 0)
2032 return r;
2033 r = dump_gateways(rtnl, hwdb, table, info->ifindex);
2034 if (r < 0)
2035 return r;
2036 r = dump_list(table, "DNS:", dns);
2037 if (r < 0)
2038 return r;
2039 r = dump_list(table, "Search Domains:", search_domains);
2040 if (r < 0)
2041 return r;
2042 r = dump_list(table, "Route Domains:", route_domains);
2043 if (r < 0)
2044 return r;
2045 r = dump_list(table, "NTP:", ntp);
1f807af6
SS
2046 if (r < 0)
2047 return r;
2048 r = dump_list(table, "SIP:", sip);
98d5bef3
YW
2049 if (r < 0)
2050 return r;
2051 r = dump_ifindexes(table, "Carrier Bound To:", carrier_bound_to);
2052 if (r < 0)
2053 return r;
2054 r = dump_ifindexes(table, "Carrier Bound By:", carrier_bound_by);
2055 if (r < 0)
2056 return r;
9085f64a 2057
35cab5f9
YW
2058 if (lease) {
2059 const char *tz;
2060
2061 r = sd_dhcp_lease_get_timezone(lease, &tz);
2062 if (r >= 0) {
2063 r = table_add_many(table,
2064 TABLE_EMPTY,
2065 TABLE_STRING, "Time Zone:",
2066 TABLE_STRING, tz);
2067 if (r < 0)
2068 return table_log_add_error(r);
2069 }
98d5bef3 2070 }
8eb9058d 2071
2153bbc8
SS
2072 r = sd_network_link_get_dhcp4_client_id_string(info->ifindex, &client_id);
2073 if (r >= 0) {
2074 r = table_add_many(table,
2075 TABLE_EMPTY,
2076 TABLE_STRING, "DHCP4 Client ID:",
2077 TABLE_STRING, client_id);
2078 if (r < 0)
2079 return table_log_add_error(r);
2080 }
2081
331ee15f
SS
2082 r = sd_network_link_get_dhcp6_client_iaid_string(info->ifindex, &iaid);
2083 if (r >= 0) {
2084 r = table_add_many(table,
2085 TABLE_EMPTY,
2086 TABLE_STRING, "DHCP6 Client IAID:",
2087 TABLE_STRING, iaid);
2088 if (r < 0)
2089 return table_log_add_error(r);
2090 }
2091
63911885
SS
2092 r = sd_network_link_get_dhcp6_client_duid_string(info->ifindex, &duid);
2093 if (r >= 0) {
2094 r = table_add_many(table,
2095 TABLE_EMPTY,
2096 TABLE_STRING, "DHCP6 Client DUID:",
2097 TABLE_STRING, duid);
2098 if (r < 0)
2099 return table_log_add_error(r);
2100 }
2101
98d5bef3
YW
2102 r = dump_lldp_neighbors(table, "Connected To:", info->ifindex);
2103 if (r < 0)
2104 return r;
837f57da 2105
d9ce1c24
MAL
2106 r = dump_dhcp_leases(table, "Offered DHCP leases:", bus, info);
2107 if (r < 0)
2108 return r;
2109
a459b24f
YW
2110 r = dump_statistics(table, info);
2111 if (r < 0)
2112 return r;
2113
10c71c36
YW
2114 r = table_print(table, NULL);
2115 if (r < 0)
bd17fa8c 2116 return log_error_errno(r, "Failed to print table: %m");
10c71c36
YW
2117
2118 return show_logs(info);
9085f64a
LP
2119}
2120
0070333f
LP
2121static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
2122 _cleanup_free_ char *operational_state = NULL;
2123 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
2124 const char *on_color_operational, *off_color_operational;
98d5bef3
YW
2125 _cleanup_(table_unrefp) Table *table = NULL;
2126 TableCell *cell;
2127 int r;
0070333f
LP
2128
2129 assert(rtnl);
2130
4abd866d 2131 (void) sd_network_get_operational_state(&operational_state);
ceb366df 2132 operational_state_to_color(NULL, operational_state, &on_color_operational, &off_color_operational);
0070333f 2133
4252171a 2134 table = table_new("dot", "key", "value");
98d5bef3 2135 if (!table)
bd17fa8c 2136 return log_oom();
98d5bef3 2137
a42d9490
YW
2138 if (arg_full)
2139 table_set_width(table, 0);
2140
81914d9f
YW
2141 assert_se(cell = table_get_cell(table, 0, 0));
2142 (void) table_set_ellipsize_percent(table, cell, 100);
2143
2144 assert_se(cell = table_get_cell(table, 0, 1));
2145 (void) table_set_align_percent(table, cell, 100);
2146 (void) table_set_ellipsize_percent(table, cell, 100);
2147
98d5bef3
YW
2148 table_set_header(table, false);
2149
8d0e0af2
YW
2150 r = table_add_many(table,
2151 TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE),
2152 TABLE_SET_COLOR, on_color_operational,
2153 TABLE_STRING, "State:",
2154 TABLE_STRING, strna(operational_state),
2155 TABLE_SET_COLOR, on_color_operational);
bd17fa8c
YW
2156 if (r < 0)
2157 return table_log_add_error(r);
0070333f 2158
98d5bef3
YW
2159 r = dump_addresses(rtnl, table, 0);
2160 if (r < 0)
2161 return r;
2162 r = dump_gateways(rtnl, hwdb, table, 0);
2163 if (r < 0)
2164 return r;
0070333f 2165
4abd866d 2166 (void) sd_network_get_dns(&dns);
98d5bef3
YW
2167 r = dump_list(table, "DNS:", dns);
2168 if (r < 0)
2169 return r;
0070333f 2170
4abd866d 2171 (void) sd_network_get_search_domains(&search_domains);
98d5bef3
YW
2172 r = dump_list(table, "Search Domains:", search_domains);
2173 if (r < 0)
2174 return r;
0070333f 2175
4abd866d 2176 (void) sd_network_get_route_domains(&route_domains);
98d5bef3
YW
2177 r = dump_list(table, "Route Domains:", route_domains);
2178 if (r < 0)
2179 return r;
0070333f 2180
4abd866d 2181 (void) sd_network_get_ntp(&ntp);
98d5bef3
YW
2182 r = dump_list(table, "NTP:", ntp);
2183 if (r < 0)
2184 return r;
0070333f 2185
10c71c36
YW
2186 r = table_print(table, NULL);
2187 if (r < 0)
bd17fa8c 2188 return log_error_errno(r, "Failed to print table: %m");
10c71c36
YW
2189
2190 return show_logs(NULL);
0070333f
LP
2191}
2192
266b5389 2193static int link_status(int argc, char *argv[], void *userdata) {
335dd8ba 2194 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
4afd3348 2195 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
b147503e 2196 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
172353b1 2197 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
b147503e 2198 int r, c, i;
ee8c4568 2199
0221d68a 2200 (void) pager_open(arg_pager_flags);
0070333f 2201
335dd8ba
YW
2202 r = sd_bus_open_system(&bus);
2203 if (r < 0)
2204 return log_error_errno(r, "Failed to connect system bus: %m");
2205
1c4baffc 2206 r = sd_netlink_open(&rtnl);
f647962d
MS
2207 if (r < 0)
2208 return log_error_errno(r, "Failed to connect to netlink: %m");
f7d68aa8 2209
81fd1dd3
TG
2210 r = sd_hwdb_new(&hwdb);
2211 if (r < 0)
2212 log_debug_errno(r, "Failed to open hardware database: %m");
69fb1176 2213
b147503e 2214 if (arg_all)
335dd8ba 2215 c = acquire_link_info(bus, rtnl, NULL, &links);
b147503e 2216 else if (argc <= 1)
0070333f 2217 return system_status(rtnl, hwdb);
b147503e 2218 else
335dd8ba 2219 c = acquire_link_info(bus, rtnl, argv + 1, &links);
b147503e
LP
2220 if (c < 0)
2221 return c;
ee8c4568 2222
b147503e
LP
2223 for (i = 0; i < c; i++) {
2224 if (i > 0)
2225 fputc('\n', stdout);
ee8c4568 2226
d9ce1c24 2227 link_status_one(bus, rtnl, hwdb, links + i);
ee8c4568
LP
2228 }
2229
2230 return 0;
2231}
2232
34437b4f
LP
2233static char *lldp_capabilities_to_string(uint16_t x) {
2234 static const char characters[] = {
2235 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2236 };
2237 char *ret;
2238 unsigned i;
49699bac 2239
34437b4f
LP
2240 ret = new(char, ELEMENTSOF(characters) + 1);
2241 if (!ret)
49699bac
SS
2242 return NULL;
2243
34437b4f
LP
2244 for (i = 0; i < ELEMENTSOF(characters); i++)
2245 ret[i] = (x & (1U << i)) ? characters[i] : '.';
49699bac 2246
34437b4f
LP
2247 ret[i] = 0;
2248 return ret;
49699bac
SS
2249}
2250
4c3160f1
ZJS
2251static void lldp_capabilities_legend(uint16_t x) {
2252 unsigned w, i, cols = columns();
404d53a9 2253 static const char* const table[] = {
4c3160f1
ZJS
2254 "o - Other",
2255 "p - Repeater",
2256 "b - Bridge",
2257 "w - WLAN Access Point",
2258 "r - Router",
2259 "t - Telephone",
2260 "d - DOCSIS cable device",
2261 "a - Station",
2262 "c - Customer VLAN",
2263 "s - Service VLAN",
2264 "m - Two-port MAC Relay (TPMR)",
2265 };
2266
2267 if (x == 0)
2268 return;
2269
2270 printf("\nCapability Flags:\n");
2271 for (w = 0, i = 0; i < ELEMENTSOF(table); i++)
2272 if (x & (1U << i) || arg_all) {
2273 bool newline;
2274
2275 newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols;
2276 if (newline)
2277 w = 0;
2278 w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]);
2279 }
2280 puts("");
2281}
2282
49699bac 2283static int link_lldp_status(int argc, char *argv[], void *userdata) {
b147503e 2284 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
172353b1 2285 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
658e9106 2286 _cleanup_(table_unrefp) Table *table = NULL;
b147503e 2287 int i, r, c, m = 0;
4c3160f1 2288 uint16_t all = 0;
658e9106 2289 TableCell *cell;
b147503e
LP
2290
2291 r = sd_netlink_open(&rtnl);
2292 if (r < 0)
2293 return log_error_errno(r, "Failed to connect to netlink: %m");
49699bac 2294
335dd8ba 2295 c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
49699bac 2296 if (c < 0)
7d367b45
LP
2297 return c;
2298
0221d68a 2299 (void) pager_open(arg_pager_flags);
49699bac 2300
4252171a
ZJS
2301 table = table_new("link",
2302 "chassis id",
2303 "system name",
2304 "caps",
2305 "port id",
2306 "port description");
658e9106 2307 if (!table)
bd17fa8c 2308 return log_oom();
658e9106 2309
a42d9490
YW
2310 if (arg_full)
2311 table_set_width(table, 0);
2312
658e9106
YW
2313 table_set_header(table, arg_legend);
2314
2315 assert_se(cell = table_get_cell(table, 0, 0));
2316 table_set_minimum_width(table, cell, 16);
2317
2318 assert_se(cell = table_get_cell(table, 0, 1));
2319 table_set_minimum_width(table, cell, 17);
2320
2321 assert_se(cell = table_get_cell(table, 0, 2));
2322 table_set_minimum_width(table, cell, 16);
2323
2324 assert_se(cell = table_get_cell(table, 0, 3));
2325 table_set_minimum_width(table, cell, 11);
2326
2327 assert_se(cell = table_get_cell(table, 0, 4));
2328 table_set_minimum_width(table, cell, 17);
2329
2330 assert_se(cell = table_get_cell(table, 0, 5));
2331 table_set_minimum_width(table, cell, 16);
49699bac 2332
b147503e 2333 for (i = 0; i < c; i++) {
34437b4f 2334 _cleanup_fclose_ FILE *f = NULL;
49699bac 2335
837f57da
LP
2336 r = open_lldp_neighbors(links[i].ifindex, &f);
2337 if (r == -ENOENT)
2338 continue;
2339 if (r < 0) {
2340 log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", links[i].ifindex);
34437b4f
LP
2341 continue;
2342 }
49699bac 2343
34437b4f 2344 for (;;) {
e2835111
YW
2345 _cleanup_free_ char *cid = NULL, *pid = NULL, *sname = NULL, *pdesc = NULL, *capabilities = NULL;
2346 const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL;
34437b4f 2347 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
34437b4f 2348 uint16_t cc;
49699bac 2349
837f57da 2350 r = next_lldp_neighbor(f, &n);
34437b4f 2351 if (r < 0) {
837f57da 2352 log_warning_errno(r, "Failed to read neighbor data: %m");
34437b4f 2353 break;
49699bac 2354 }
837f57da
LP
2355 if (r == 0)
2356 break;
34437b4f
LP
2357
2358 (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
2359 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
2360 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
2361 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
2362
d08191a2
LP
2363 if (chassis_id) {
2364 cid = ellipsize(chassis_id, 17, 100);
2365 if (cid)
2366 chassis_id = cid;
2367 }
2368
2369 if (port_id) {
2370 pid = ellipsize(port_id, 17, 100);
2371 if (pid)
2372 port_id = pid;
2373 }
2374
2375 if (system_name) {
2376 sname = ellipsize(system_name, 16, 100);
2377 if (sname)
2378 system_name = sname;
2379 }
2380
2381 if (port_description) {
2382 pdesc = ellipsize(port_description, 16, 100);
2383 if (pdesc)
2384 port_description = pdesc;
2385 }
2386
4c3160f1 2387 if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) {
34437b4f 2388 capabilities = lldp_capabilities_to_string(cc);
4c3160f1
ZJS
2389 all |= cc;
2390 }
34437b4f 2391
658e9106
YW
2392 r = table_add_many(table,
2393 TABLE_STRING, links[i].name,
2394 TABLE_STRING, strna(chassis_id),
2395 TABLE_STRING, strna(system_name),
2396 TABLE_STRING, strna(capabilities),
2397 TABLE_STRING, strna(port_id),
2398 TABLE_STRING, strna(port_description));
2399 if (r < 0)
bd17fa8c 2400 return table_log_add_error(r);
b147503e
LP
2401
2402 m++;
49699bac
SS
2403 }
2404 }
2405
658e9106
YW
2406 r = table_print(table, NULL);
2407 if (r < 0)
bd17fa8c 2408 return log_error_errno(r, "Failed to print table: %m");
658e9106 2409
4c3160f1
ZJS
2410 if (arg_legend) {
2411 lldp_capabilities_legend(all);
2412 printf("\n%i neighbors listed.\n", m);
2413 }
49699bac
SS
2414
2415 return 0;
2416}
2417
9cd8c766
SS
2418static int link_delete_send_message(sd_netlink *rtnl, int index) {
2419 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2420 int r;
2421
2422 assert(rtnl);
2423
2424 r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index);
2425 if (r < 0)
2426 return rtnl_log_create_error(r);
2427
2428 r = sd_netlink_call(rtnl, req, 0, NULL);
2429 if (r < 0)
2430 return r;
2431
2432 return 0;
2433}
2434
c30ffcee
SS
2435static int link_up_down_send_message(sd_netlink *rtnl, char *command, int index) {
2436 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2437 int r;
2438
2439 assert(rtnl);
2440
2441 r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, index);
2442 if (r < 0)
2443 return rtnl_log_create_error(r);
2444
2445 if (streq(command, "up"))
2446 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
2447 else
2448 r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP);
2449 if (r < 0)
2450 return log_error_errno(r, "Could not set link flags: %m");
2451
2452 r = sd_netlink_call(rtnl, req, 0, NULL);
2453 if (r < 0)
2454 return r;
2455
2456 return 0;
2457}
2458
2459static int link_up_down(int argc, char *argv[], void *userdata) {
2460 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2461 _cleanup_set_free_ Set *indexes = NULL;
2462 int index, r, i;
2463 Iterator j;
2464 void *p;
2465
2466 r = sd_netlink_open(&rtnl);
2467 if (r < 0)
2468 return log_error_errno(r, "Failed to connect to netlink: %m");
2469
2470 indexes = set_new(NULL);
2471 if (!indexes)
2472 return log_oom();
2473
2474 for (i = 1; i < argc; i++) {
2475 index = resolve_interface_or_warn(&rtnl, argv[i]);
2476 if (index < 0)
2477 return index;
2478
2479 r = set_put(indexes, INT_TO_PTR(index));
2480 if (r < 0)
2481 return log_oom();
2482 }
2483
2484 SET_FOREACH(p, indexes, j) {
2485 index = PTR_TO_INT(p);
2486 r = link_up_down_send_message(rtnl, argv[0], index);
2487 if (r < 0) {
2488 char ifname[IF_NAMESIZE + 1];
2489
2490 return log_error_errno(r, "Failed to %s interface %s: %m",
2491 argv[1], format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX));
2492 }
2493 }
2494
2495 return r;
2496}
2497
9cd8c766
SS
2498static int link_delete(int argc, char *argv[], void *userdata) {
2499 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2500 _cleanup_set_free_ Set *indexes = NULL;
9cd8c766
SS
2501 int index, r, i;
2502 Iterator j;
38b9af61 2503 void *p;
9cd8c766
SS
2504
2505 r = sd_netlink_open(&rtnl);
2506 if (r < 0)
2507 return log_error_errno(r, "Failed to connect to netlink: %m");
2508
2509 indexes = set_new(NULL);
2510 if (!indexes)
2511 return log_oom();
2512
2513 for (i = 1; i < argc; i++) {
d308bb99 2514 index = resolve_interface_or_warn(&rtnl, argv[i]);
231d9de1
ZJS
2515 if (index < 0)
2516 return index;
9cd8c766
SS
2517
2518 r = set_put(indexes, INT_TO_PTR(index));
2519 if (r < 0)
2520 return log_oom();
2521 }
2522
38b9af61 2523 SET_FOREACH(p, indexes, j) {
d56d6cb8
YW
2524 index = PTR_TO_INT(p);
2525 r = link_delete_send_message(rtnl, index);
9cd8c766 2526 if (r < 0) {
518a66ec
YW
2527 char ifname[IF_NAMESIZE + 1];
2528
e4857ee2
YW
2529 return log_error_errno(r, "Failed to delete interface %s: %m",
2530 format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX));
9cd8c766
SS
2531 }
2532 }
2533
2534 return r;
2535}
2536
308e7dfd
YW
2537static int link_renew_one(sd_bus *bus, int index, const char *name) {
2538 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2539 int r;
2540
8a048c8c 2541 r = bus_call_method(bus, bus_network_mgr, "RenewLink", &error, NULL, "i", index);
308e7dfd
YW
2542 if (r < 0)
2543 return log_error_errno(r, "Failed to renew dynamic configuration of interface %s: %s",
2544 name, bus_error_message(&error, r));
2545
2546 return 0;
2547}
2548
2549static int link_renew(int argc, char *argv[], void *userdata) {
2550 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
f7581ed6 2551 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
308e7dfd
YW
2552 int index, i, k = 0, r;
2553
2554 r = sd_bus_open_system(&bus);
2555 if (r < 0)
2556 return log_error_errno(r, "Failed to connect system bus: %m");
2557
2558 for (i = 1; i < argc; i++) {
d308bb99 2559 index = resolve_interface_or_warn(&rtnl, argv[i]);
231d9de1
ZJS
2560 if (index < 0)
2561 return index;
308e7dfd
YW
2562
2563 r = link_renew_one(bus, index, argv[i]);
2564 if (r < 0 && k >= 0)
2565 k = r;
2566 }
2567
2568 return k;
2569}
2570
3efdd6af
SS
2571static int link_force_renew_one(sd_bus *bus, int index, const char *name) {
2572 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2573 int r;
2574
8a048c8c 2575 r = bus_call_method(bus, bus_network_mgr, "ForceRenewLink", &error, NULL, "i", index);
3efdd6af
SS
2576 if (r < 0)
2577 return log_error_errno(r, "Failed to force renew dynamic configuration of interface %s: %s",
2578 name, bus_error_message(&error, r));
2579
2580 return 0;
2581}
2582
2583static int link_force_renew(int argc, char *argv[], void *userdata) {
2584 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2585 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2586 int index, i, k = 0, r;
2587
2588 r = sd_bus_open_system(&bus);
2589 if (r < 0)
2590 return log_error_errno(r, "Failed to connect system bus: %m");
2591
2592 for (i = 1; i < argc; i++) {
2593 index = resolve_interface_or_warn(&rtnl, argv[i]);
2594 if (index < 0)
2595 return index;
2596
2597 r = link_force_renew_one(bus, index, argv[i]);
2598 if (r < 0 && k >= 0)
2599 k = r;
2600 }
2601
2602 return k;
2603}
2604
a227674c
YW
2605static int verb_reload(int argc, char *argv[], void *userdata) {
2606 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2607 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2608 int r;
2609
2610 r = sd_bus_open_system(&bus);
2611 if (r < 0)
2612 return log_error_errno(r, "Failed to connect system bus: %m");
2613
8a048c8c 2614 r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
a227674c
YW
2615 if (r < 0)
2616 return log_error_errno(r, "Failed to reload network settings: %m");
2617
2618 return 0;
2619}
2620
8dc85c5e
YW
2621static int verb_reconfigure(int argc, char *argv[], void *userdata) {
2622 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2623 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
f7581ed6 2624 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
8dc85c5e
YW
2625 _cleanup_set_free_ Set *indexes = NULL;
2626 int index, i, r;
2627 Iterator j;
2628 void *p;
2629
2630 r = sd_bus_open_system(&bus);
2631 if (r < 0)
2632 return log_error_errno(r, "Failed to connect system bus: %m");
2633
2634 indexes = set_new(NULL);
2635 if (!indexes)
2636 return log_oom();
2637
2638 for (i = 1; i < argc; i++) {
d308bb99 2639 index = resolve_interface_or_warn(&rtnl, argv[i]);
231d9de1
ZJS
2640 if (index < 0)
2641 return index;
8dc85c5e
YW
2642
2643 r = set_put(indexes, INT_TO_PTR(index));
2644 if (r < 0)
2645 return log_oom();
2646 }
2647
2648 SET_FOREACH(p, indexes, j) {
2649 index = PTR_TO_INT(p);
8a048c8c 2650 r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index);
8dc85c5e
YW
2651 if (r < 0) {
2652 char ifname[IF_NAMESIZE + 1];
2653
117caf37
ZJS
2654 return log_error_errno(r, "Failed to reconfigure network interface %s: %m",
2655 format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX));
8dc85c5e
YW
2656 }
2657 }
2658
2659 return 0;
2660}
2661
37ec0fdd
LP
2662static int help(void) {
2663 _cleanup_free_ char *link = NULL;
2664 int r;
2665
2666 r = terminal_urlify_man("networkctl", "1", &link);
2667 if (r < 0)
2668 return log_oom();
2669
353b2baa
LP
2670 printf("%s [OPTIONS...] COMMAND\n\n"
2671 "%sQuery and control the networking subsystem.%s\n"
a459b24f 2672 "\nCommands:\n"
8dc85c5e
YW
2673 " list [PATTERN...] List links\n"
2674 " status [PATTERN...] Show link status\n"
2675 " lldp [PATTERN...] Show LLDP neighbors\n"
2676 " label Show current address label entries in the kernel\n"
2677 " delete DEVICES... Delete virtual netdevs\n"
c30ffcee
SS
2678 " up DEVICES... Bring devices up\n"
2679 " down DEVICES... Bring devices down\n"
8dc85c5e 2680 " renew DEVICES... Renew dynamic configurations\n"
3efdd6af 2681 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
8dc85c5e
YW
2682 " reconfigure DEVICES... Reconfigure interfaces\n"
2683 " reload Reload .network and .netdev files\n"
353b2baa 2684 "\nOptions:\n"
8dc85c5e
YW
2685 " -h --help Show this help\n"
2686 " --version Show package version\n"
2687 " --no-pager Do not pipe output into a pager\n"
2688 " --no-legend Do not show the headers and footers\n"
2689 " -a --all Show status for all links\n"
2690 " -s --stats Show detailed link statics\n"
10c71c36
YW
2691 " -l --full Do not ellipsize output\n"
2692 " -n --lines=INTEGER Number of journal entries to show\n"
37ec0fdd
LP
2693 "\nSee the %s for details.\n"
2694 , program_invocation_short_name
353b2baa 2695 , ansi_highlight()
ce2529b4 2696 , ansi_normal()
37ec0fdd
LP
2697 , link
2698 );
2699
2700 return 0;
ee8c4568
LP
2701}
2702
2703static int parse_argv(int argc, char *argv[]) {
2704
2705 enum {
2706 ARG_VERSION = 0x100,
2707 ARG_NO_PAGER,
2708 ARG_NO_LEGEND,
2709 };
2710
2711 static const struct option options[] = {
2712 { "help", no_argument, NULL, 'h' },
2713 { "version", no_argument, NULL, ARG_VERSION },
2714 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
2715 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
9085f64a 2716 { "all", no_argument, NULL, 'a' },
a459b24f 2717 { "stats", no_argument, NULL, 's' },
10c71c36
YW
2718 { "full", no_argument, NULL, 'l' },
2719 { "lines", required_argument, NULL, 'n' },
ee8c4568
LP
2720 {}
2721 };
2722
2723 int c;
2724
2725 assert(argc >= 0);
2726 assert(argv);
2727
10c71c36 2728 while ((c = getopt_long(argc, argv, "hasln:", options, NULL)) >= 0) {
ee8c4568
LP
2729
2730 switch (c) {
2731
2732 case 'h':
37ec0fdd 2733 return help();
ee8c4568
LP
2734
2735 case ARG_VERSION:
3f6fd1ba 2736 return version();
ee8c4568
LP
2737
2738 case ARG_NO_PAGER:
0221d68a 2739 arg_pager_flags |= PAGER_DISABLE;
ee8c4568
LP
2740 break;
2741
2742 case ARG_NO_LEGEND:
2743 arg_legend = false;
2744 break;
2745
9085f64a
LP
2746 case 'a':
2747 arg_all = true;
2748 break;
2749
a459b24f
YW
2750 case 's':
2751 arg_stats = true;
2752 break;
2753
10c71c36
YW
2754 case 'l':
2755 arg_full = true;
2756 break;
2757
2758 case 'n':
2759 if (safe_atou(optarg, &arg_lines) < 0)
2760 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2761 "Failed to parse lines '%s'", optarg);
2762 break;
2763
ee8c4568
LP
2764 case '?':
2765 return -EINVAL;
2766
2767 default:
2768 assert_not_reached("Unhandled option");
2769 }
2770 }
2771
2772 return 1;
2773}
2774
2775static int networkctl_main(int argc, char *argv[]) {
15c3626e 2776 static const Verb verbs[] = {
8dc85c5e
YW
2777 { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, list_links },
2778 { "status", VERB_ANY, VERB_ANY, 0, link_status },
2779 { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status },
2780 { "label", VERB_ANY, VERB_ANY, 0, list_address_labels },
2781 { "delete", 2, VERB_ANY, 0, link_delete },
c30ffcee
SS
2782 { "up", 2, VERB_ANY, 0, link_up_down },
2783 { "down", 2, VERB_ANY, 0, link_up_down },
8dc85c5e 2784 { "renew", 2, VERB_ANY, 0, link_renew },
3efdd6af 2785 { "forcerenew", 2, VERB_ANY, 0, link_force_renew },
8dc85c5e
YW
2786 { "reconfigure", 2, VERB_ANY, 0, verb_reconfigure },
2787 { "reload", 1, 1, 0, verb_reload },
266b5389 2788 {}
ee8c4568
LP
2789 };
2790
266b5389 2791 return dispatch_verb(argc, argv, verbs, NULL);
ee8c4568
LP
2792}
2793
58fb3678
LP
2794static void warn_networkd_missing(void) {
2795
2796 if (access("/run/systemd/netif/state", F_OK) >= 0)
2797 return;
2798
2799 fprintf(stderr, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
2800}
2801
4e2ca442 2802static int run(int argc, char* argv[]) {
ee8c4568
LP
2803 int r;
2804
1a043959 2805 log_show_color(true);
ee8c4568
LP
2806 log_parse_environment();
2807 log_open();
2808
2809 r = parse_argv(argc, argv);
2810 if (r <= 0)
4e2ca442 2811 return r;
ee8c4568 2812
58fb3678
LP
2813 warn_networkd_missing();
2814
4e2ca442 2815 return networkctl_main(argc, argv);
ee8c4568 2816}
4e2ca442
ZJS
2817
2818DEFINE_MAIN_FUNCTION(run);