]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkctl.c
dhcp: fix entry name in parsing 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,
d41fa6ee 933 sd_dhcp_lease *lease,
98d5bef3 934 Table *table,
69fb1176
LP
935 int ifindex) {
936
ee8c4568 937 _cleanup_free_ struct local_address *local = NULL;
536cdd07 938 _cleanup_strv_free_ char **buf = NULL;
d41fa6ee 939 struct in_addr dhcp4_address = {};
ee8c4568
LP
940 int r, n, i;
941
837f57da 942 assert(rtnl);
98d5bef3 943 assert(table);
837f57da 944
1d050e1e 945 n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local);
536cdd07 946 if (n <= 0)
ee8c4568
LP
947 return n;
948
d41fa6ee
YW
949 if (lease)
950 (void) sd_dhcp_lease_get_address(lease, &dhcp4_address);
cdf01b36 951
ee8c4568
LP
952 for (i = 0; i < n; i++) {
953 _cleanup_free_ char *pretty = NULL;
536cdd07 954 char name[IF_NAMESIZE+1];
1693a943 955
98d5bef3
YW
956 r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
957 if (r < 0)
958 return r;
1693a943 959
d41fa6ee 960 if (local[i].family == AF_INET && in4_addr_equal(&local[i].address.in, &dhcp4_address)) {
cdf01b36
SS
961 _cleanup_free_ char *p = NULL;
962
963 p = pretty;
964 pretty = strjoin(pretty , " (DHCP4)");
965 if (!pretty)
966 return log_oom();
967 }
968
536cdd07
YW
969 r = strv_extendf(&buf, "%s%s%s",
970 pretty,
971 ifindex <= 0 ? " on " : "",
972 ifindex <= 0 ? format_ifname_full(local[i].ifindex, name, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
98d5bef3 973 if (r < 0)
536cdd07 974 return log_oom();
ee8c4568
LP
975 }
976
536cdd07 977 return dump_list(table, "Address:", buf);
ee8c4568
LP
978}
979
d37b7627
SS
980static int dump_address_labels(sd_netlink *rtnl) {
981 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
0232beed 982 _cleanup_(table_unrefp) Table *table = NULL;
d37b7627 983 sd_netlink_message *m;
0232beed 984 TableCell *cell;
d37b7627
SS
985 int r;
986
987 assert(rtnl);
988
989 r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6);
990 if (r < 0)
991 return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m");
992
993 r = sd_netlink_message_request_dump(req, true);
994 if (r < 0)
995 return r;
996
997 r = sd_netlink_call(rtnl, req, 0, &reply);
998 if (r < 0)
999 return r;
1000
4252171a 1001 table = table_new("label", "prefix/prefixlen");
0232beed 1002 if (!table)
bd17fa8c 1003 return log_oom();
0232beed 1004
a42d9490
YW
1005 if (arg_full)
1006 table_set_width(table, 0);
1007
ad5555b4 1008 r = table_set_sort(table, (size_t) 0, (size_t) SIZE_MAX);
0232beed
YW
1009 if (r < 0)
1010 return r;
1011
1012 assert_se(cell = table_get_cell(table, 0, 0));
1013 (void) table_set_align_percent(table, cell, 100);
81914d9f 1014 (void) table_set_ellipsize_percent(table, cell, 100);
0232beed
YW
1015
1016 assert_se(cell = table_get_cell(table, 0, 1));
1017 (void) table_set_align_percent(table, cell, 100);
d37b7627
SS
1018
1019 for (m = reply; m; m = sd_netlink_message_next(m)) {
1020 _cleanup_free_ char *pretty = NULL;
67a46833 1021 union in_addr_union prefix = IN_ADDR_NULL;
d37b7627
SS
1022 uint8_t prefixlen;
1023 uint32_t label;
1024
1025 r = sd_netlink_message_get_errno(m);
1026 if (r < 0) {
1027 log_error_errno(r, "got error: %m");
1028 continue;
1029 }
1030
1031 r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label);
1032 if (r < 0 && r != -ENODATA) {
1033 log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m");
1034 continue;
1035 }
1036
1037 r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix.in6);
1038 if (r < 0)
1039 continue;
1040
1041 r = in_addr_to_string(AF_INET6, &prefix, &pretty);
1042 if (r < 0)
1043 continue;
1044
1045 r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen);
1046 if (r < 0)
1047 continue;
1048
81914d9f 1049 r = table_add_cell(table, NULL, TABLE_UINT32, &label);
0232beed 1050 if (r < 0)
bd17fa8c 1051 return table_log_add_error(r);
0232beed 1052
8d0e0af2 1053 r = table_add_cell_stringf(table, NULL, "%s/%u", pretty, prefixlen);
0232beed 1054 if (r < 0)
bd17fa8c 1055 return table_log_add_error(r);
d37b7627
SS
1056 }
1057
bd17fa8c
YW
1058 r = table_print(table, NULL);
1059 if (r < 0)
1060 return log_error_errno(r, "Failed to print table: %m");
1061
1062 return 0;
d37b7627
SS
1063}
1064
1065static int list_address_labels(int argc, char *argv[], void *userdata) {
1066 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1067 int r;
1068
1069 r = sd_netlink_open(&rtnl);
1070 if (r < 0)
1071 return log_error_errno(r, "Failed to connect to netlink: %m");
1072
1073 dump_address_labels(rtnl);
1074
1075 return 0;
1076}
1077
837f57da
LP
1078static int open_lldp_neighbors(int ifindex, FILE **ret) {
1079 _cleanup_free_ char *p = NULL;
1080 FILE *f;
1081
1082 if (asprintf(&p, "/run/systemd/netif/lldp/%i", ifindex) < 0)
1083 return -ENOMEM;
1084
1085 f = fopen(p, "re");
1086 if (!f)
1087 return -errno;
1088
1089 *ret = f;
1090 return 0;
1091}
1092
1093static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) {
1094 _cleanup_free_ void *raw = NULL;
1095 size_t l;
1096 le64_t u;
1097 int r;
1098
1099 assert(f);
1100 assert(ret);
1101
1102 l = fread(&u, 1, sizeof(u), f);
1103 if (l == 0 && feof(f))
1104 return 0;
1105 if (l != sizeof(u))
1106 return -EBADMSG;
1107
d23c3e4c
FB
1108 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
1109 if (le64toh(u) >= 4096)
1110 return -EBADMSG;
1111
837f57da
LP
1112 raw = new(uint8_t, le64toh(u));
1113 if (!raw)
1114 return -ENOMEM;
1115
1116 if (fread(raw, 1, le64toh(u), f) != le64toh(u))
1117 return -EBADMSG;
1118
1119 r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u));
1120 if (r < 0)
1121 return r;
1122
1123 return 1;
1124}
1125
98d5bef3 1126static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) {
536cdd07 1127 _cleanup_strv_free_ char **buf = NULL;
837f57da 1128 _cleanup_fclose_ FILE *f = NULL;
536cdd07 1129 int r;
837f57da 1130
98d5bef3 1131 assert(table);
837f57da
LP
1132 assert(prefix);
1133 assert(ifindex > 0);
1134
1135 r = open_lldp_neighbors(ifindex, &f);
98d5bef3
YW
1136 if (r == -ENOENT)
1137 return 0;
837f57da
LP
1138 if (r < 0)
1139 return r;
1140
1141 for (;;) {
1142 const char *system_name = NULL, *port_id = NULL, *port_description = NULL;
1143 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
1144
1145 r = next_lldp_neighbor(f, &n);
1146 if (r < 0)
1147 return r;
1148 if (r == 0)
1149 break;
1150
837f57da
LP
1151 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
1152 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
1153 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
1154
536cdd07
YW
1155 r = strv_extendf(&buf, "%s on port %s%s%s%s",
1156 strna(system_name),
1157 strna(port_id),
1158 isempty(port_description) ? "" : " (",
1159 strempty(port_description),
1160 isempty(port_description) ? "" : ")");
98d5bef3 1161 if (r < 0)
536cdd07 1162 return log_oom();
837f57da
LP
1163 }
1164
536cdd07 1165 return dump_list(table, prefix, buf);
837f57da
LP
1166}
1167
d9ce1c24
MAL
1168static int dump_dhcp_leases(Table *table, const char *prefix, sd_bus *bus, const LinkInfo *link) {
1169 _cleanup_strv_free_ char **buf = NULL;
1170 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1171 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1172 int r;
1173
1174 r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.DHCPServer", "Leases");
1175 if (r < 0) {
1176 bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY);
1177
1178 log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING,
1179 r, "Failed to query link DHCP leases: %s", bus_error_message(&error, r));
1180 return 0;
1181 }
1182
1183 r = sd_bus_message_enter_container(reply, 'v', "a(uayayayayt)");
1184 if (r < 0)
1185 return bus_log_parse_error(r);
1186
1187 r = sd_bus_message_enter_container(reply, 'a', "(uayayayayt)");
1188 if (r < 0)
1189 return bus_log_parse_error(r);
1190
1191 while ((r = sd_bus_message_enter_container(reply, 'r', "uayayayayt")) > 0) {
1192 _cleanup_free_ char *id = NULL, *ip = NULL;
1193 const void *client_id, *addr, *gtw, *hwaddr;
1194 size_t client_id_sz, sz;
1195 uint64_t expiration;
1196 uint32_t family;
1197
1198 r = sd_bus_message_read(reply, "u", &family);
1199 if (r < 0)
1200 return bus_log_parse_error(r);
1201
1202 r = sd_bus_message_read_array(reply, 'y', &client_id, &client_id_sz);
1203 if (r < 0)
1204 return bus_log_parse_error(r);
1205
1206 r = sd_bus_message_read_array(reply, 'y', &addr, &sz);
1207 if (r < 0 || sz != 4)
1208 return bus_log_parse_error(r);
1209
1210 r = sd_bus_message_read_array(reply, 'y', &gtw, &sz);
1211 if (r < 0 || sz != 4)
1212 return bus_log_parse_error(r);
1213
1214 r = sd_bus_message_read_array(reply, 'y', &hwaddr, &sz);
1215 if (r < 0)
1216 return bus_log_parse_error(r);
1217
1218 r = sd_bus_message_read_basic(reply, 't', &expiration);
1219 if (r < 0)
1220 return bus_log_parse_error(r);
1221
1222 r = sd_dhcp_client_id_to_string(client_id, client_id_sz, &id);
1223 if (r < 0)
1224 return bus_log_parse_error(r);
1225
1226 r = in_addr_to_string(family, addr, &ip);
1227 if (r < 0)
1228 return bus_log_parse_error(r);
1229
1230 r = strv_extendf(&buf, "%s (to %s)", ip, id);
1231 if (r < 0)
1232 return log_oom();
1233
1234 r = sd_bus_message_exit_container(reply);
1235 if (r < 0)
1236 return bus_log_parse_error(r);
1237 }
1238 if (r < 0)
1239 return bus_log_parse_error(r);
1240
1241 r = sd_bus_message_exit_container(reply);
1242 if (r < 0)
1243 return bus_log_parse_error(r);
1244
1245 r = sd_bus_message_exit_container(reply);
1246 if (r < 0)
1247 return bus_log_parse_error(r);
1248
1249 if (strv_isempty(buf)) {
1250 r = strv_extendf(&buf, "none");
1251 if (r < 0)
1252 return log_oom();
1253 }
1254
1255 return dump_list(table, prefix, buf);
1256}
1257
98d5bef3 1258static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) {
b295beea 1259 unsigned c;
98d5bef3 1260 int r;
b295beea
LP
1261
1262 assert(prefix);
1263
1264 if (!ifindexes || ifindexes[0] <= 0)
98d5bef3 1265 return 0;
b295beea
LP
1266
1267 for (c = 0; ifindexes[c] > 0; c++) {
8d0e0af2
YW
1268 r = table_add_many(table,
1269 TABLE_EMPTY,
1270 TABLE_STRING, c == 0 ? prefix : "",
1271 TABLE_IFINDEX, ifindexes[c]);
98d5bef3 1272 if (r < 0)
bd17fa8c 1273 return table_log_add_error(r);
b295beea 1274 }
98d5bef3
YW
1275
1276 return 0;
b295beea
LP
1277}
1278
a459b24f 1279#define DUMP_STATS_ONE(name, val_name) \
8d0e0af2
YW
1280 r = table_add_many(table, \
1281 TABLE_EMPTY, \
1282 TABLE_STRING, name ":"); \
a459b24f 1283 if (r < 0) \
bd17fa8c 1284 return table_log_add_error(r); \
8d0e0af2
YW
1285 r = table_add_cell(table, NULL, \
1286 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
a459b24f
YW
1287 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
1288 if (r < 0) \
bd17fa8c 1289 return table_log_add_error(r);
a459b24f
YW
1290
1291static int dump_statistics(Table *table, const LinkInfo *info) {
1292 int r;
1293
1294 if (!arg_stats)
1295 return 0;
1296
1297 if (!info->has_stats64 && !info->has_stats)
1298 return 0;
1299
1300 DUMP_STATS_ONE("Rx Packets", rx_packets);
1301 DUMP_STATS_ONE("Tx Packets", tx_packets);
1302 DUMP_STATS_ONE("Rx Bytes", rx_bytes);
1303 DUMP_STATS_ONE("Tx Bytes", tx_bytes);
1304 DUMP_STATS_ONE("Rx Errors", rx_errors);
1305 DUMP_STATS_ONE("Tx Errors", tx_errors);
1306 DUMP_STATS_ONE("Rx Dropped", rx_dropped);
1307 DUMP_STATS_ONE("Tx Dropped", tx_dropped);
1308 DUMP_STATS_ONE("Multicast Packets", multicast);
1309 DUMP_STATS_ONE("Collisions", collisions);
1310
1311 return 0;
1312}
1313
10c71c36
YW
1314static OutputFlags get_output_flags(void) {
1315 return
1316 arg_all * OUTPUT_SHOW_ALL |
1317 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
1318 colors_enabled() * OUTPUT_COLOR;
1319}
1320
1321static int show_logs(const LinkInfo *info) {
1322 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1323 int r;
1324
1325 if (arg_lines == 0)
1326 return 0;
1327
1328 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
1329 if (r < 0)
1330 return log_error_errno(r, "Failed to open journal: %m");
1331
1332 r = add_match_this_boot(j, NULL);
1333 if (r < 0)
1334 return log_error_errno(r, "Failed to add boot matches: %m");
1335
1336 if (info) {
1337 char m1[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)];
1338 const char *m2, *m3;
1339
1340 /* kernel */
1341 xsprintf(m1, "_KERNEL_DEVICE=n%i", info->ifindex);
1342 /* networkd */
1343 m2 = strjoina("INTERFACE=", info->name);
1344 /* udevd */
1345 m3 = strjoina("DEVICE=", info->name);
1346
1347 (void)(
1348 (r = sd_journal_add_match(j, m1, 0)) ||
1349 (r = sd_journal_add_disjunction(j)) ||
1350 (r = sd_journal_add_match(j, m2, 0)) ||
1351 (r = sd_journal_add_disjunction(j)) ||
1352 (r = sd_journal_add_match(j, m3, 0))
1353 );
1354 if (r < 0)
1355 return log_error_errno(r, "Failed to add link matches: %m");
1356 } else {
1357 r = add_matches_for_unit(j, "systemd-networkd.service");
1358 if (r < 0)
1359 return log_error_errno(r, "Failed to add unit matches: %m");
1360
1361 r = add_matches_for_unit(j, "systemd-networkd-wait-online.service");
1362 if (r < 0)
1363 return log_error_errno(r, "Failed to add unit matches: %m");
1364 }
1365
1366 return show_journal(
1367 stdout,
1368 j,
1369 OUTPUT_SHORT,
1370 0,
1371 0,
1372 arg_lines,
1373 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
1374 NULL);
1375}
1376
69fb1176 1377static int link_status_one(
d9ce1c24 1378 sd_bus *bus,
1c4baffc 1379 sd_netlink *rtnl,
81fd1dd3 1380 sd_hwdb *hwdb,
b147503e
LP
1381 const LinkInfo *info) {
1382
2a71d57f 1383 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **sip = NULL, **search_domains = NULL, **route_domains = NULL;
862e7108 1384 _cleanup_free_ char *t = NULL, *network = NULL, *iaid = NULL, *duid = NULL,
35cab5f9
YW
1385 *setup_state = NULL, *operational_state = NULL, *lease_file = NULL;
1386 const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL,
1387 *on_color_operational, *off_color_operational, *on_color_setup, *off_color_setup;
b295beea 1388 _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
35cab5f9 1389 _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL;
98d5bef3
YW
1390 _cleanup_(table_unrefp) Table *table = NULL;
1391 TableCell *cell;
b147503e 1392 int r;
9085f64a
LP
1393
1394 assert(rtnl);
b147503e 1395 assert(info);
9085f64a 1396
b147503e 1397 (void) sd_network_link_get_operational_state(info->ifindex, &operational_state);
ceb366df 1398 operational_state_to_color(info->name, operational_state, &on_color_operational, &off_color_operational);
d57c365b 1399
33d5013d
LP
1400 r = sd_network_link_get_setup_state(info->ifindex, &setup_state);
1401 if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */
1402 setup_state = strdup("unmanaged");
d57c365b 1403 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
9085f64a 1404
b147503e
LP
1405 (void) sd_network_link_get_dns(info->ifindex, &dns);
1406 (void) sd_network_link_get_search_domains(info->ifindex, &search_domains);
1407 (void) sd_network_link_get_route_domains(info->ifindex, &route_domains);
1408 (void) sd_network_link_get_ntp(info->ifindex, &ntp);
1f807af6 1409 (void) sd_network_link_get_sip(info->ifindex, &sip);
9085f64a 1410
172353b1
ZJS
1411 if (info->sd_device) {
1412 (void) sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE", &link);
1413 (void) sd_device_get_property_value(info->sd_device, "ID_NET_DRIVER", &driver);
1414 (void) sd_device_get_property_value(info->sd_device, "ID_PATH", &path);
9085f64a 1415
172353b1
ZJS
1416 if (sd_device_get_property_value(info->sd_device, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
1417 (void) sd_device_get_property_value(info->sd_device, "ID_VENDOR", &vendor);
9085f64a 1418
172353b1
ZJS
1419 if (sd_device_get_property_value(info->sd_device, "ID_MODEL_FROM_DATABASE", &model) < 0)
1420 (void) sd_device_get_property_value(info->sd_device, "ID_MODEL", &model);
9085f64a
LP
1421 }
1422
172353b1 1423 t = link_get_type_string(info->iftype, info->sd_device);
b1acce80 1424
4abd866d 1425 (void) sd_network_link_get_network_file(info->ifindex, &network);
df3fb561 1426
4abd866d
LP
1427 (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
1428 (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
0d4ad91d 1429
35cab5f9
YW
1430 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", info->ifindex) < 0)
1431 return log_oom();
1432
1433 (void) dhcp_lease_load(&lease, lease_file);
1434
4252171a 1435 table = table_new("dot", "key", "value");
98d5bef3 1436 if (!table)
bd17fa8c 1437 return log_oom();
98d5bef3 1438
a42d9490
YW
1439 if (arg_full)
1440 table_set_width(table, 0);
1441
81914d9f
YW
1442 assert_se(cell = table_get_cell(table, 0, 0));
1443 (void) table_set_ellipsize_percent(table, cell, 100);
1444
1445 assert_se(cell = table_get_cell(table, 0, 1));
1446 (void) table_set_ellipsize_percent(table, cell, 100);
1447
98d5bef3
YW
1448 table_set_header(table, false);
1449
8d0e0af2
YW
1450 r = table_add_many(table,
1451 TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE),
1452 TABLE_SET_COLOR, on_color_operational);
98d5bef3 1453 if (r < 0)
bd17fa8c 1454 return table_log_add_error(r);
81914d9f 1455 r = table_add_cell_stringf(table, &cell, "%i: %s", info->ifindex, info->name);
98d5bef3 1456 if (r < 0)
bd17fa8c 1457 return table_log_add_error(r);
81914d9f 1458 (void) table_set_align_percent(table, cell, 0);
98d5bef3 1459
8d0e0af2
YW
1460 r = table_add_many(table,
1461 TABLE_EMPTY,
1462 TABLE_EMPTY,
1463 TABLE_STRING, "Link File:",
1464 TABLE_SET_ALIGN_PERCENT, 100,
1465 TABLE_STRING, strna(link),
1466 TABLE_EMPTY,
1467 TABLE_STRING, "Network File:",
1468 TABLE_STRING, strna(network),
1469 TABLE_EMPTY,
1470 TABLE_STRING, "Type:",
1471 TABLE_STRING, strna(t),
1472 TABLE_EMPTY,
1473 TABLE_STRING, "State:");
98d5bef3 1474 if (r < 0)
bd17fa8c 1475 return table_log_add_error(r);
98d5bef3 1476 r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)",
64e7ebde
YW
1477 on_color_operational, strna(operational_state), off_color_operational,
1478 on_color_setup, strna(setup_state), off_color_setup);
98d5bef3 1479 if (r < 0)
bd17fa8c 1480 return table_log_add_error(r);
98d5bef3 1481
536cdd07
YW
1482 r = dump_list(table, "Alternative Names:", info->alternative_names);
1483 if (r < 0)
1484 return r;
511070ee 1485
98d5bef3 1486 if (path) {
8d0e0af2
YW
1487 r = table_add_many(table,
1488 TABLE_EMPTY,
1489 TABLE_STRING, "Path:",
1490 TABLE_STRING, path);
98d5bef3 1491 if (r < 0)
bd17fa8c 1492 return table_log_add_error(r);
98d5bef3
YW
1493 }
1494 if (driver) {
8d0e0af2
YW
1495 r = table_add_many(table,
1496 TABLE_EMPTY,
1497 TABLE_STRING, "Driver:",
1498 TABLE_STRING, driver);
98d5bef3 1499 if (r < 0)
bd17fa8c 1500 return table_log_add_error(r);
98d5bef3
YW
1501 }
1502 if (vendor) {
8d0e0af2
YW
1503 r = table_add_many(table,
1504 TABLE_EMPTY,
1505 TABLE_STRING, "Vendor:",
1506 TABLE_STRING, vendor);
98d5bef3 1507 if (r < 0)
bd17fa8c 1508 return table_log_add_error(r);
98d5bef3
YW
1509 }
1510 if (model) {
8d0e0af2
YW
1511 r = table_add_many(table,
1512 TABLE_EMPTY,
1513 TABLE_STRING, "Model:",
1514 TABLE_STRING, model);
98d5bef3 1515 if (r < 0)
bd17fa8c 1516 return table_log_add_error(r);
98d5bef3 1517 }
9085f64a 1518
b147503e 1519 if (info->has_mac_address) {
888943fc 1520 _cleanup_free_ char *description = NULL;
db73295a 1521 char ea[ETHER_ADDR_TO_STRING_MAX];
888943fc 1522
4abd866d 1523 (void) ieee_oui(hwdb, &info->mac_address, &description);
888943fc 1524
8d0e0af2
YW
1525 r = table_add_many(table,
1526 TABLE_EMPTY,
1527 TABLE_STRING, "HW Address:");
98d5bef3 1528 if (r < 0)
bd17fa8c 1529 return table_log_add_error(r);
98d5bef3 1530 r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
64e7ebde
YW
1531 ether_addr_to_string(&info->mac_address, ea),
1532 description ? " (" : "",
1533 strempty(description),
1534 description ? ")" : "");
98d5bef3 1535 if (r < 0)
bd17fa8c 1536 return table_log_add_error(r);
db73295a 1537 }
9085f64a 1538
caa8538a
YW
1539 if (info->has_permanent_mac_address) {
1540 _cleanup_free_ char *description = NULL;
1541 char ea[ETHER_ADDR_TO_STRING_MAX];
1542
1543 (void) ieee_oui(hwdb, &info->permanent_mac_address, &description);
1544
1545 r = table_add_many(table,
1546 TABLE_EMPTY,
1547 TABLE_STRING, "HW Permanent Address:");
1548 if (r < 0)
bd17fa8c 1549 return table_log_add_error(r);
caa8538a
YW
1550 r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
1551 ether_addr_to_string(&info->permanent_mac_address, ea),
1552 description ? " (" : "",
1553 strempty(description),
1554 description ? ")" : "");
1555 if (r < 0)
bd17fa8c 1556 return table_log_add_error(r);
caa8538a
YW
1557 }
1558
c06ff86e
YW
1559 if (info->mtu > 0) {
1560 char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)];
1561
1562 xsprintf(min_str, "%" PRIu32, info->min_mtu);
1563 xsprintf(max_str, "%" PRIu32, info->max_mtu);
1564
8d0e0af2
YW
1565 r = table_add_many(table,
1566 TABLE_EMPTY,
1567 TABLE_STRING, "MTU:");
98d5bef3 1568 if (r < 0)
bd17fa8c 1569 return table_log_add_error(r);
c06ff86e
YW
1570 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s",
1571 info->mtu,
1572 info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "",
90e29fe1 1573 info->min_mtu > 0 ? "min: " : "",
c06ff86e
YW
1574 info->min_mtu > 0 ? min_str : "",
1575 info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "",
90e29fe1 1576 info->max_mtu > 0 ? "max: " : "",
c06ff86e
YW
1577 info->max_mtu > 0 ? max_str : "",
1578 info->min_mtu > 0 || info->max_mtu > 0 ? ")" : "");
98d5bef3 1579 if (r < 0)
e810df37
SS
1580 return table_log_add_error(r);
1581 }
1582
1583 if (info->qdisc) {
1584 r = table_add_many(table,
1585 TABLE_EMPTY,
1586 TABLE_STRING, "QDisc:",
1587 TABLE_STRING, info->qdisc);
1588 if (r < 0)
6cfef1b3
SS
1589 return table_log_add_error(r);
1590 }
1591
1592 if (info->master > 0) {
1593 r = table_add_many(table,
1594 TABLE_EMPTY,
1595 TABLE_STRING, "Master:",
1596 TABLE_IFINDEX, info->master);
1597 if (r < 0)
bd17fa8c 1598 return table_log_add_error(r);
98d5bef3 1599 }
8eb9058d 1600
d69b62de
SS
1601 if (info->has_ipv6_address_generation_mode) {
1602 static const struct {
1603 const char *mode;
1604 } mode_table[] = {
1605 { "eui64" },
1606 { "none" },
1607 { "stable-privacy" },
1608 { "random" },
1609 };
1610
1611 r = table_add_many(table,
1612 TABLE_EMPTY,
1613 TABLE_STRING, "IPv6 Address Generation Mode:",
1614 TABLE_STRING, mode_table[info->addr_gen_mode]);
1615 if (r < 0)
1616 return table_log_add_error(r);
1617 }
1618
c82d1bf2
SS
1619 if (streq_ptr(info->netdev_kind, "bridge")) {
1620 r = table_add_many(table,
1621 TABLE_EMPTY,
1622 TABLE_STRING, "Forward Delay:",
1623 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->forward_delay),
1624 TABLE_EMPTY,
1625 TABLE_STRING, "Hello Time:",
1626 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->hello_time),
1627 TABLE_EMPTY,
1628 TABLE_STRING, "Max Age:",
1629 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->max_age),
1630 TABLE_EMPTY,
1631 TABLE_STRING, "Ageing Time:",
1632 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->ageing_time),
1633 TABLE_EMPTY,
1634 TABLE_STRING, "Priority:",
1635 TABLE_UINT16, info->priority,
1636 TABLE_EMPTY,
1637 TABLE_STRING, "STP:",
1638 TABLE_BOOLEAN, info->stp_state > 0,
1639 TABLE_EMPTY,
1640 TABLE_STRING, "Multicast IGMP Version:",
12ef8fb6
SS
1641 TABLE_UINT8, info->mcast_igmp_version,
1642 TABLE_EMPTY,
1643 TABLE_STRING, "Cost:",
1644 TABLE_UINT32, info->cost);
c82d1bf2 1645 if (r < 0)
bd17fa8c 1646 return table_log_add_error(r);
b24281aa 1647
12ef8fb6 1648 if (info->port_state <= BR_STATE_BLOCKING) {
12ef8fb6
SS
1649 r = table_add_many(table,
1650 TABLE_EMPTY,
1651 TABLE_STRING, "Port State:",
a8389a33 1652 TABLE_STRING, bridge_state_to_string(info->port_state));
12ef8fb6 1653 }
b1d6fe70 1654 } else if (streq_ptr(info->netdev_kind, "bond")) {
b1d6fe70
SS
1655 r = table_add_many(table,
1656 TABLE_EMPTY,
1657 TABLE_STRING, "Mode:",
b55818fd 1658 TABLE_STRING, bond_mode_to_string(info->mode),
b1d6fe70
SS
1659 TABLE_EMPTY,
1660 TABLE_STRING, "Miimon:",
1661 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->miimon),
1662 TABLE_EMPTY,
1663 TABLE_STRING, "Updelay:",
1664 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->updelay),
1665 TABLE_EMPTY,
1666 TABLE_STRING, "Downdelay:",
1667 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->downdelay));
1668 if (r < 0)
1669 return table_log_add_error(r);
1670
b24281aa 1671 } else if (streq_ptr(info->netdev_kind, "vxlan")) {
22ae6c7d
SS
1672 char ttl[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1673
b24281aa
SS
1674 if (info->vxlan_info.vni > 0) {
1675 r = table_add_many(table,
1676 TABLE_EMPTY,
1677 TABLE_STRING, "VNI:",
1678 TABLE_UINT32, info->vxlan_info.vni);
1679 if (r < 0)
bd17fa8c 1680 return table_log_add_error(r);
b24281aa
SS
1681 }
1682
1683 if (IN_SET(info->vxlan_info.group_family, AF_INET, AF_INET6)) {
659f85a5
SS
1684 const char *p;
1685
1686 r = in_addr_is_multicast(info->vxlan_info.group_family, &info->vxlan_info.group);
1687 if (r <= 0)
1688 p = "Remote:";
1689 else
1690 p = "Group:";
1691
b24281aa
SS
1692 r = table_add_many(table,
1693 TABLE_EMPTY,
659f85a5 1694 TABLE_STRING, p,
b24281aa
SS
1695 info->vxlan_info.group_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR,
1696 &info->vxlan_info.group);
1697 if (r < 0)
bd17fa8c 1698 return table_log_add_error(r);
b24281aa
SS
1699 }
1700
1701 if (IN_SET(info->vxlan_info.local_family, AF_INET, AF_INET6)) {
1702 r = table_add_many(table,
1703 TABLE_EMPTY,
1704 TABLE_STRING, "Local:",
1705 info->vxlan_info.local_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR,
1706 &info->vxlan_info.local);
1707 if (r < 0)
bd17fa8c 1708 return table_log_add_error(r);
b24281aa
SS
1709 }
1710
1711 if (info->vxlan_info.dest_port > 0) {
1712 r = table_add_many(table,
1713 TABLE_EMPTY,
1714 TABLE_STRING, "Destination Port:",
1715 TABLE_UINT16, be16toh(info->vxlan_info.dest_port));
1716 if (r < 0)
bd17fa8c 1717 return table_log_add_error(r);
b24281aa
SS
1718 }
1719
1720 if (info->vxlan_info.link > 0) {
1721 r = table_add_many(table,
1722 TABLE_EMPTY,
1723 TABLE_STRING, "Underlying Device:",
1724 TABLE_IFINDEX, info->vxlan_info.link);
1725 if (r < 0)
bd17fa8c 1726 return table_log_add_error(r);
b24281aa 1727 }
22ae6c7d
SS
1728
1729 r = table_add_many(table,
1730 TABLE_EMPTY,
1731 TABLE_STRING, "Learning:",
1732 TABLE_BOOLEAN, info->vxlan_info.learning);
1733 if (r < 0)
1734 return table_log_add_error(r);
1735
1736 r = table_add_many(table,
1737 TABLE_EMPTY,
1738 TABLE_STRING, "RSC:",
1739 TABLE_BOOLEAN, info->vxlan_info.rsc);
1740 if (r < 0)
1741 return table_log_add_error(r);
1742
1743 r = table_add_many(table,
1744 TABLE_EMPTY,
1745 TABLE_STRING, "L3MISS:",
1746 TABLE_BOOLEAN, info->vxlan_info.l3miss);
1747 if (r < 0)
1748 return table_log_add_error(r);
1749
1750 r = table_add_many(table,
1751 TABLE_EMPTY,
1752 TABLE_STRING, "L2MISS:",
1753 TABLE_BOOLEAN, info->vxlan_info.l2miss);
1754 if (r < 0)
1755 return table_log_add_error(r);
1756
1757 if (info->vxlan_info.tos > 1) {
1758 r = table_add_many(table,
1759 TABLE_EMPTY,
1760 TABLE_STRING, "TOS:",
1761 TABLE_UINT8, info->vxlan_info.tos);
1762 if (r < 0)
1763 return table_log_add_error(r);
1764 }
1765
1766 if (info->vxlan_info.ttl > 0)
1767 xsprintf(ttl, "%" PRIu8, info->vxlan_info.ttl);
1768 else
1769 strcpy(ttl, "auto");
1770
1771 r = table_add_many(table,
1772 TABLE_EMPTY,
1773 TABLE_STRING, "TTL:",
1774 TABLE_STRING, ttl);
1775 if (r < 0)
1776 return table_log_add_error(r);
2b2a1ae6
SS
1777 } else if (streq_ptr(info->netdev_kind, "vlan") && info->vlan_id > 0) {
1778 r = table_add_many(table,
1779 TABLE_EMPTY,
1780 TABLE_STRING, "VLan Id:",
1781 TABLE_UINT16, info->vlan_id);
1782 if (r < 0)
1783 return table_log_add_error(r);
5712d689 1784 } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
dca35224
SS
1785 if (!in_addr_is_null(AF_INET, &info->local)) {
1786 r = table_add_many(table,
1787 TABLE_EMPTY,
1788 TABLE_STRING, "Local:",
1789 TABLE_IN_ADDR, &info->local);
1790 if (r < 0)
1791 return table_log_add_error(r);
1792 }
1793
1794 if (!in_addr_is_null(AF_INET, &info->remote)) {
1795 r = table_add_many(table,
1796 TABLE_EMPTY,
1797 TABLE_STRING, "Remote:",
1798 TABLE_IN_ADDR, &info->remote);
1799 if (r < 0)
1800 return table_log_add_error(r);
1801 }
5712d689 1802 } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
ad760bc1
SS
1803 if (!in_addr_is_null(AF_INET6, &info->local)) {
1804 r = table_add_many(table,
1805 TABLE_EMPTY,
1806 TABLE_STRING, "Local:",
1807 TABLE_IN6_ADDR, &info->local);
1808 if (r < 0)
1809 return table_log_add_error(r);
1810 }
1811
1812 if (!in_addr_is_null(AF_INET6, &info->remote)) {
1813 r = table_add_many(table,
1814 TABLE_EMPTY,
1815 TABLE_STRING, "Remote:",
1816 TABLE_IN6_ADDR, &info->remote);
1817 if (r < 0)
1818 return table_log_add_error(r);
1819 }
4e1a1991
SS
1820 } else if (streq_ptr(info->netdev_kind, "geneve")) {
1821 r = table_add_many(table,
1822 TABLE_EMPTY,
1823 TABLE_STRING, "VNI:",
1824 TABLE_UINT32, info->vni);
1825 if (r < 0)
1826 return table_log_add_error(r);
1827
1828 if (info->has_tunnel_ipv4 && !in_addr_is_null(AF_INET, &info->remote)) {
1829 r = table_add_many(table,
1830 TABLE_EMPTY,
1831 TABLE_STRING, "Remote:",
1832 TABLE_IN_ADDR, &info->remote);
1833 if (r < 0)
1834 return table_log_add_error(r);
1835 } else if (!in_addr_is_null(AF_INET6, &info->remote)) {
1836 r = table_add_many(table,
1837 TABLE_EMPTY,
1838 TABLE_STRING, "Remote:",
1839 TABLE_IN6_ADDR, &info->remote);
1840 if (r < 0)
1841 return table_log_add_error(r);
1842 }
1843
1844 if (info->ttl > 0) {
1845 r = table_add_many(table,
1846 TABLE_EMPTY,
1847 TABLE_STRING, "TTL:",
1848 TABLE_UINT8, info->ttl);
1849 if (r < 0)
1850 return table_log_add_error(r);
1851 }
1852
1853 if (info->tos > 0) {
1854 r = table_add_many(table,
1855 TABLE_EMPTY,
1856 TABLE_STRING, "TOS:",
1857 TABLE_UINT8, info->tos);
1858 if (r < 0)
1859 return table_log_add_error(r);
1860 }
1861
1862 r = table_add_many(table,
1863 TABLE_EMPTY,
1864 TABLE_STRING, "Port:",
1865 TABLE_UINT16, info->tunnel_port);
1866 if (r < 0)
1867 return table_log_add_error(r);
e7b38d7d
SS
1868
1869 r = table_add_many(table,
1870 TABLE_EMPTY,
1871 TABLE_STRING, "Inherit:",
1872 TABLE_STRING, geneve_df_to_string(info->inherit));
1873 if (r < 0)
1874 return table_log_add_error(r);
1875
1876 if (info->df > 0) {
1877 r = table_add_many(table,
1878 TABLE_EMPTY,
1879 TABLE_STRING, "IPDoNotFragment:",
1880 TABLE_UINT8, info->df);
1881 if (r < 0)
1882 return table_log_add_error(r);
1883 }
1884
1885 r = table_add_many(table,
1886 TABLE_EMPTY,
1887 TABLE_STRING, "UDPChecksum:",
1888 TABLE_BOOLEAN, info->csum);
1889 if (r < 0)
1890 return table_log_add_error(r);
1891
1892 r = table_add_many(table,
1893 TABLE_EMPTY,
1894 TABLE_STRING, "UDP6ZeroChecksumTx:",
1895 TABLE_BOOLEAN, info->csum6_tx);
1896 if (r < 0)
1897 return table_log_add_error(r);
1898
1899 r = table_add_many(table,
1900 TABLE_EMPTY,
1901 TABLE_STRING, "UDP6ZeroChecksumRx:",
1902 TABLE_BOOLEAN, info->csum6_rx);
1903 if (r < 0)
1904 return table_log_add_error(r);
1905
1906 if (info->label > 0) {
1907 r = table_add_many(table,
1908 TABLE_EMPTY,
1909 TABLE_STRING, "FlowLabel:",
1910 TABLE_UINT32, info->label);
1911 if (r < 0)
1912 return table_log_add_error(r);
1913 }
cf217a09
SS
1914 } else if (STRPTR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) {
1915 r = table_add_many(table,
1916 TABLE_EMPTY,
1917 TABLE_STRING, "Mode:",
1918 TABLE_STRING, macvlan_mode_to_string(info->macvlan_mode));
1919 if (r < 0)
1920 return table_log_add_error(r);
851ef1ed
SS
1921 } else if (streq_ptr(info->netdev_kind, "ipvlan")) {
1922 _cleanup_free_ char *p = NULL, *s = NULL;
1923
1924 if (info->ipvlan_flags & IPVLAN_F_PRIVATE)
1925 p = strdup("private");
1926 else if (info->ipvlan_flags & IPVLAN_F_VEPA)
1927 p = strdup("vepa");
1928 else
1929 p = strdup("bridge");
1930 if (!p)
1931 log_oom();
1932
1933 s = strjoin(ipvlan_mode_to_string(info->ipvlan_mode), " (", p, ")");
1934 if (!s)
1935 return log_oom();
1936
1937 r = table_add_many(table,
1938 TABLE_EMPTY,
1939 TABLE_STRING, "Mode:",
1940 TABLE_STRING, s);
1941 if (r < 0)
1942 return table_log_add_error(r);
c82d1bf2
SS
1943 }
1944
8d07de25
ZJS
1945 if (info->has_wlan_link_info) {
1946 _cleanup_free_ char *esc = NULL;
1947 char buf[ETHER_ADDR_TO_STRING_MAX];
1948
1949 r = table_add_many(table,
1950 TABLE_EMPTY,
1951 TABLE_STRING, "WiFi access point:");
1952 if (r < 0)
bd17fa8c 1953 return table_log_add_error(r);
8d07de25
ZJS
1954
1955 if (info->ssid)
1956 esc = cescape(info->ssid);
1957
1958 r = table_add_cell_stringf(table, NULL, "%s (%s)",
1959 strnull(esc),
1960 ether_addr_to_string(&info->bssid, buf));
1961 if (r < 0)
bd17fa8c 1962 return table_log_add_error(r);
8d07de25
ZJS
1963 }
1964
335dd8ba 1965 if (info->has_bitrates) {
42a63431 1966 char tx[FORMAT_BYTES_MAX], rx[FORMAT_BYTES_MAX];
335dd8ba 1967
8d0e0af2
YW
1968 r = table_add_many(table,
1969 TABLE_EMPTY,
1970 TABLE_STRING, "Bit Rate (Tx/Rx):");
335dd8ba 1971 if (r < 0)
bd17fa8c 1972 return table_log_add_error(r);
42a63431
YW
1973 r = table_add_cell_stringf(table, NULL, "%sbps/%sbps",
1974 format_bytes_full(tx, sizeof tx, info->tx_bitrate, 0),
1975 format_bytes_full(rx, sizeof rx, info->rx_bitrate, 0));
335dd8ba 1976 if (r < 0)
bd17fa8c 1977 return table_log_add_error(r);
335dd8ba
YW
1978 }
1979
98d5bef3 1980 if (info->has_tx_queues || info->has_rx_queues) {
8d0e0af2
YW
1981 r = table_add_many(table,
1982 TABLE_EMPTY,
1983 TABLE_STRING, "Queue Length (Tx/Rx):");
98d5bef3 1984 if (r < 0)
bd17fa8c 1985 return table_log_add_error(r);
98d5bef3
YW
1986 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues);
1987 if (r < 0)
bd17fa8c 1988 return table_log_add_error(r);
98d5bef3 1989 }
0d4ad91d 1990
c967d2c7
YW
1991 if (info->has_ethtool_link_info) {
1992 const char *duplex = duplex_to_string(info->duplex);
1993 const char *port = port_to_string(info->port);
1994
1995 if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) {
8d0e0af2
YW
1996 r = table_add_many(table,
1997 TABLE_EMPTY,
1998 TABLE_STRING, "Auto negotiation:",
1999 TABLE_BOOLEAN, info->autonegotiation == AUTONEG_ENABLE);
c967d2c7 2000 if (r < 0)
bd17fa8c 2001 return table_log_add_error(r);
c967d2c7
YW
2002 }
2003
2004 if (info->speed > 0) {
8d0e0af2
YW
2005 r = table_add_many(table,
2006 TABLE_EMPTY,
2007 TABLE_STRING, "Speed:",
50299121 2008 TABLE_BPS, info->speed);
c967d2c7 2009 if (r < 0)
bd17fa8c 2010 return table_log_add_error(r);
c967d2c7
YW
2011 }
2012
2013 if (duplex) {
8d0e0af2
YW
2014 r = table_add_many(table,
2015 TABLE_EMPTY,
2016 TABLE_STRING, "Duplex:",
2017 TABLE_STRING, duplex);
c967d2c7 2018 if (r < 0)
bd17fa8c 2019 return table_log_add_error(r);
c967d2c7
YW
2020 }
2021
2022 if (port) {
8d0e0af2
YW
2023 r = table_add_many(table,
2024 TABLE_EMPTY,
2025 TABLE_STRING, "Port:",
2026 TABLE_STRING, port);
c967d2c7 2027 if (r < 0)
bd17fa8c 2028 return table_log_add_error(r);
c967d2c7
YW
2029 }
2030 }
2031
d41fa6ee 2032 r = dump_addresses(rtnl, lease, table, info->ifindex);
98d5bef3
YW
2033 if (r < 0)
2034 return r;
2035 r = dump_gateways(rtnl, hwdb, table, info->ifindex);
2036 if (r < 0)
2037 return r;
2038 r = dump_list(table, "DNS:", dns);
2039 if (r < 0)
2040 return r;
2041 r = dump_list(table, "Search Domains:", search_domains);
2042 if (r < 0)
2043 return r;
2044 r = dump_list(table, "Route Domains:", route_domains);
2045 if (r < 0)
2046 return r;
2047 r = dump_list(table, "NTP:", ntp);
1f807af6
SS
2048 if (r < 0)
2049 return r;
2050 r = dump_list(table, "SIP:", sip);
98d5bef3
YW
2051 if (r < 0)
2052 return r;
2053 r = dump_ifindexes(table, "Carrier Bound To:", carrier_bound_to);
2054 if (r < 0)
2055 return r;
2056 r = dump_ifindexes(table, "Carrier Bound By:", carrier_bound_by);
2057 if (r < 0)
2058 return r;
9085f64a 2059
35cab5f9 2060 if (lease) {
862e7108
YW
2061 const void *client_id;
2062 size_t client_id_len;
35cab5f9
YW
2063 const char *tz;
2064
2065 r = sd_dhcp_lease_get_timezone(lease, &tz);
2066 if (r >= 0) {
2067 r = table_add_many(table,
2068 TABLE_EMPTY,
2069 TABLE_STRING, "Time Zone:",
2070 TABLE_STRING, tz);
2071 if (r < 0)
2072 return table_log_add_error(r);
2073 }
8eb9058d 2074
862e7108
YW
2075 r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len);
2076 if (r >= 0) {
2077 _cleanup_free_ char *id = NULL;
2078
2079 r = sd_dhcp_client_id_to_string(client_id, client_id_len, &id);
2080 if (r >= 0) {
2081 r = table_add_many(table,
2082 TABLE_EMPTY,
2083 TABLE_STRING, "DHCP4 Client ID:",
2084 TABLE_STRING, id);
2085 if (r < 0)
2086 return table_log_add_error(r);
2087 }
2088 }
2153bbc8
SS
2089 }
2090
331ee15f
SS
2091 r = sd_network_link_get_dhcp6_client_iaid_string(info->ifindex, &iaid);
2092 if (r >= 0) {
2093 r = table_add_many(table,
2094 TABLE_EMPTY,
2095 TABLE_STRING, "DHCP6 Client IAID:",
2096 TABLE_STRING, iaid);
2097 if (r < 0)
2098 return table_log_add_error(r);
2099 }
2100
63911885
SS
2101 r = sd_network_link_get_dhcp6_client_duid_string(info->ifindex, &duid);
2102 if (r >= 0) {
2103 r = table_add_many(table,
2104 TABLE_EMPTY,
2105 TABLE_STRING, "DHCP6 Client DUID:",
2106 TABLE_STRING, duid);
2107 if (r < 0)
2108 return table_log_add_error(r);
2109 }
2110
98d5bef3
YW
2111 r = dump_lldp_neighbors(table, "Connected To:", info->ifindex);
2112 if (r < 0)
2113 return r;
837f57da 2114
d9ce1c24
MAL
2115 r = dump_dhcp_leases(table, "Offered DHCP leases:", bus, info);
2116 if (r < 0)
2117 return r;
2118
a459b24f
YW
2119 r = dump_statistics(table, info);
2120 if (r < 0)
2121 return r;
2122
10c71c36
YW
2123 r = table_print(table, NULL);
2124 if (r < 0)
bd17fa8c 2125 return log_error_errno(r, "Failed to print table: %m");
10c71c36
YW
2126
2127 return show_logs(info);
9085f64a
LP
2128}
2129
0070333f
LP
2130static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
2131 _cleanup_free_ char *operational_state = NULL;
2132 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
2133 const char *on_color_operational, *off_color_operational;
98d5bef3
YW
2134 _cleanup_(table_unrefp) Table *table = NULL;
2135 TableCell *cell;
2136 int r;
0070333f
LP
2137
2138 assert(rtnl);
2139
4abd866d 2140 (void) sd_network_get_operational_state(&operational_state);
ceb366df 2141 operational_state_to_color(NULL, operational_state, &on_color_operational, &off_color_operational);
0070333f 2142
4252171a 2143 table = table_new("dot", "key", "value");
98d5bef3 2144 if (!table)
bd17fa8c 2145 return log_oom();
98d5bef3 2146
a42d9490
YW
2147 if (arg_full)
2148 table_set_width(table, 0);
2149
81914d9f
YW
2150 assert_se(cell = table_get_cell(table, 0, 0));
2151 (void) table_set_ellipsize_percent(table, cell, 100);
2152
2153 assert_se(cell = table_get_cell(table, 0, 1));
2154 (void) table_set_align_percent(table, cell, 100);
2155 (void) table_set_ellipsize_percent(table, cell, 100);
2156
98d5bef3
YW
2157 table_set_header(table, false);
2158
8d0e0af2
YW
2159 r = table_add_many(table,
2160 TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE),
2161 TABLE_SET_COLOR, on_color_operational,
2162 TABLE_STRING, "State:",
2163 TABLE_STRING, strna(operational_state),
2164 TABLE_SET_COLOR, on_color_operational);
bd17fa8c
YW
2165 if (r < 0)
2166 return table_log_add_error(r);
0070333f 2167
d41fa6ee 2168 r = dump_addresses(rtnl, NULL, table, 0);
98d5bef3
YW
2169 if (r < 0)
2170 return r;
2171 r = dump_gateways(rtnl, hwdb, table, 0);
2172 if (r < 0)
2173 return r;
0070333f 2174
4abd866d 2175 (void) sd_network_get_dns(&dns);
98d5bef3
YW
2176 r = dump_list(table, "DNS:", dns);
2177 if (r < 0)
2178 return r;
0070333f 2179
4abd866d 2180 (void) sd_network_get_search_domains(&search_domains);
98d5bef3
YW
2181 r = dump_list(table, "Search Domains:", search_domains);
2182 if (r < 0)
2183 return r;
0070333f 2184
4abd866d 2185 (void) sd_network_get_route_domains(&route_domains);
98d5bef3
YW
2186 r = dump_list(table, "Route Domains:", route_domains);
2187 if (r < 0)
2188 return r;
0070333f 2189
4abd866d 2190 (void) sd_network_get_ntp(&ntp);
98d5bef3
YW
2191 r = dump_list(table, "NTP:", ntp);
2192 if (r < 0)
2193 return r;
0070333f 2194
10c71c36
YW
2195 r = table_print(table, NULL);
2196 if (r < 0)
bd17fa8c 2197 return log_error_errno(r, "Failed to print table: %m");
10c71c36
YW
2198
2199 return show_logs(NULL);
0070333f
LP
2200}
2201
266b5389 2202static int link_status(int argc, char *argv[], void *userdata) {
335dd8ba 2203 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
4afd3348 2204 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
b147503e 2205 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
172353b1 2206 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
b147503e 2207 int r, c, i;
ee8c4568 2208
0221d68a 2209 (void) pager_open(arg_pager_flags);
0070333f 2210
335dd8ba
YW
2211 r = sd_bus_open_system(&bus);
2212 if (r < 0)
2213 return log_error_errno(r, "Failed to connect system bus: %m");
2214
1c4baffc 2215 r = sd_netlink_open(&rtnl);
f647962d
MS
2216 if (r < 0)
2217 return log_error_errno(r, "Failed to connect to netlink: %m");
f7d68aa8 2218
81fd1dd3
TG
2219 r = sd_hwdb_new(&hwdb);
2220 if (r < 0)
2221 log_debug_errno(r, "Failed to open hardware database: %m");
69fb1176 2222
b147503e 2223 if (arg_all)
335dd8ba 2224 c = acquire_link_info(bus, rtnl, NULL, &links);
b147503e 2225 else if (argc <= 1)
0070333f 2226 return system_status(rtnl, hwdb);
b147503e 2227 else
335dd8ba 2228 c = acquire_link_info(bus, rtnl, argv + 1, &links);
b147503e
LP
2229 if (c < 0)
2230 return c;
ee8c4568 2231
b147503e
LP
2232 for (i = 0; i < c; i++) {
2233 if (i > 0)
2234 fputc('\n', stdout);
ee8c4568 2235
d9ce1c24 2236 link_status_one(bus, rtnl, hwdb, links + i);
ee8c4568
LP
2237 }
2238
2239 return 0;
2240}
2241
34437b4f
LP
2242static char *lldp_capabilities_to_string(uint16_t x) {
2243 static const char characters[] = {
2244 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2245 };
2246 char *ret;
2247 unsigned i;
49699bac 2248
34437b4f
LP
2249 ret = new(char, ELEMENTSOF(characters) + 1);
2250 if (!ret)
49699bac
SS
2251 return NULL;
2252
34437b4f
LP
2253 for (i = 0; i < ELEMENTSOF(characters); i++)
2254 ret[i] = (x & (1U << i)) ? characters[i] : '.';
49699bac 2255
34437b4f
LP
2256 ret[i] = 0;
2257 return ret;
49699bac
SS
2258}
2259
4c3160f1
ZJS
2260static void lldp_capabilities_legend(uint16_t x) {
2261 unsigned w, i, cols = columns();
404d53a9 2262 static const char* const table[] = {
4c3160f1
ZJS
2263 "o - Other",
2264 "p - Repeater",
2265 "b - Bridge",
2266 "w - WLAN Access Point",
2267 "r - Router",
2268 "t - Telephone",
2269 "d - DOCSIS cable device",
2270 "a - Station",
2271 "c - Customer VLAN",
2272 "s - Service VLAN",
2273 "m - Two-port MAC Relay (TPMR)",
2274 };
2275
2276 if (x == 0)
2277 return;
2278
2279 printf("\nCapability Flags:\n");
2280 for (w = 0, i = 0; i < ELEMENTSOF(table); i++)
2281 if (x & (1U << i) || arg_all) {
2282 bool newline;
2283
2284 newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols;
2285 if (newline)
2286 w = 0;
2287 w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]);
2288 }
2289 puts("");
2290}
2291
49699bac 2292static int link_lldp_status(int argc, char *argv[], void *userdata) {
b147503e 2293 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
172353b1 2294 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
658e9106 2295 _cleanup_(table_unrefp) Table *table = NULL;
b147503e 2296 int i, r, c, m = 0;
4c3160f1 2297 uint16_t all = 0;
658e9106 2298 TableCell *cell;
b147503e
LP
2299
2300 r = sd_netlink_open(&rtnl);
2301 if (r < 0)
2302 return log_error_errno(r, "Failed to connect to netlink: %m");
49699bac 2303
335dd8ba 2304 c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
49699bac 2305 if (c < 0)
7d367b45
LP
2306 return c;
2307
0221d68a 2308 (void) pager_open(arg_pager_flags);
49699bac 2309
4252171a
ZJS
2310 table = table_new("link",
2311 "chassis id",
2312 "system name",
2313 "caps",
2314 "port id",
2315 "port description");
658e9106 2316 if (!table)
bd17fa8c 2317 return log_oom();
658e9106 2318
a42d9490
YW
2319 if (arg_full)
2320 table_set_width(table, 0);
2321
658e9106
YW
2322 table_set_header(table, arg_legend);
2323
2324 assert_se(cell = table_get_cell(table, 0, 0));
2325 table_set_minimum_width(table, cell, 16);
2326
2327 assert_se(cell = table_get_cell(table, 0, 1));
2328 table_set_minimum_width(table, cell, 17);
2329
2330 assert_se(cell = table_get_cell(table, 0, 2));
2331 table_set_minimum_width(table, cell, 16);
2332
2333 assert_se(cell = table_get_cell(table, 0, 3));
2334 table_set_minimum_width(table, cell, 11);
2335
2336 assert_se(cell = table_get_cell(table, 0, 4));
2337 table_set_minimum_width(table, cell, 17);
2338
2339 assert_se(cell = table_get_cell(table, 0, 5));
2340 table_set_minimum_width(table, cell, 16);
49699bac 2341
b147503e 2342 for (i = 0; i < c; i++) {
34437b4f 2343 _cleanup_fclose_ FILE *f = NULL;
49699bac 2344
837f57da
LP
2345 r = open_lldp_neighbors(links[i].ifindex, &f);
2346 if (r == -ENOENT)
2347 continue;
2348 if (r < 0) {
2349 log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", links[i].ifindex);
34437b4f
LP
2350 continue;
2351 }
49699bac 2352
34437b4f 2353 for (;;) {
e2835111
YW
2354 _cleanup_free_ char *cid = NULL, *pid = NULL, *sname = NULL, *pdesc = NULL, *capabilities = NULL;
2355 const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL;
34437b4f 2356 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
34437b4f 2357 uint16_t cc;
49699bac 2358
837f57da 2359 r = next_lldp_neighbor(f, &n);
34437b4f 2360 if (r < 0) {
837f57da 2361 log_warning_errno(r, "Failed to read neighbor data: %m");
34437b4f 2362 break;
49699bac 2363 }
837f57da
LP
2364 if (r == 0)
2365 break;
34437b4f
LP
2366
2367 (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
2368 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
2369 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
2370 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
2371
d08191a2
LP
2372 if (chassis_id) {
2373 cid = ellipsize(chassis_id, 17, 100);
2374 if (cid)
2375 chassis_id = cid;
2376 }
2377
2378 if (port_id) {
2379 pid = ellipsize(port_id, 17, 100);
2380 if (pid)
2381 port_id = pid;
2382 }
2383
2384 if (system_name) {
2385 sname = ellipsize(system_name, 16, 100);
2386 if (sname)
2387 system_name = sname;
2388 }
2389
2390 if (port_description) {
2391 pdesc = ellipsize(port_description, 16, 100);
2392 if (pdesc)
2393 port_description = pdesc;
2394 }
2395
4c3160f1 2396 if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) {
34437b4f 2397 capabilities = lldp_capabilities_to_string(cc);
4c3160f1
ZJS
2398 all |= cc;
2399 }
34437b4f 2400
658e9106
YW
2401 r = table_add_many(table,
2402 TABLE_STRING, links[i].name,
2403 TABLE_STRING, strna(chassis_id),
2404 TABLE_STRING, strna(system_name),
2405 TABLE_STRING, strna(capabilities),
2406 TABLE_STRING, strna(port_id),
2407 TABLE_STRING, strna(port_description));
2408 if (r < 0)
bd17fa8c 2409 return table_log_add_error(r);
b147503e
LP
2410
2411 m++;
49699bac
SS
2412 }
2413 }
2414
658e9106
YW
2415 r = table_print(table, NULL);
2416 if (r < 0)
bd17fa8c 2417 return log_error_errno(r, "Failed to print table: %m");
658e9106 2418
4c3160f1
ZJS
2419 if (arg_legend) {
2420 lldp_capabilities_legend(all);
2421 printf("\n%i neighbors listed.\n", m);
2422 }
49699bac
SS
2423
2424 return 0;
2425}
2426
9cd8c766
SS
2427static int link_delete_send_message(sd_netlink *rtnl, int index) {
2428 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2429 int r;
2430
2431 assert(rtnl);
2432
2433 r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index);
2434 if (r < 0)
2435 return rtnl_log_create_error(r);
2436
2437 r = sd_netlink_call(rtnl, req, 0, NULL);
2438 if (r < 0)
2439 return r;
2440
2441 return 0;
2442}
2443
c30ffcee
SS
2444static int link_up_down_send_message(sd_netlink *rtnl, char *command, int index) {
2445 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2446 int r;
2447
2448 assert(rtnl);
2449
2450 r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, index);
2451 if (r < 0)
2452 return rtnl_log_create_error(r);
2453
2454 if (streq(command, "up"))
2455 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
2456 else
2457 r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP);
2458 if (r < 0)
2459 return log_error_errno(r, "Could not set link flags: %m");
2460
2461 r = sd_netlink_call(rtnl, req, 0, NULL);
2462 if (r < 0)
2463 return r;
2464
2465 return 0;
2466}
2467
2468static int link_up_down(int argc, char *argv[], void *userdata) {
2469 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2470 _cleanup_set_free_ Set *indexes = NULL;
2471 int index, r, i;
2472 Iterator j;
2473 void *p;
2474
2475 r = sd_netlink_open(&rtnl);
2476 if (r < 0)
2477 return log_error_errno(r, "Failed to connect to netlink: %m");
2478
2479 indexes = set_new(NULL);
2480 if (!indexes)
2481 return log_oom();
2482
2483 for (i = 1; i < argc; i++) {
2484 index = resolve_interface_or_warn(&rtnl, argv[i]);
2485 if (index < 0)
2486 return index;
2487
2488 r = set_put(indexes, INT_TO_PTR(index));
2489 if (r < 0)
2490 return log_oom();
2491 }
2492
2493 SET_FOREACH(p, indexes, j) {
2494 index = PTR_TO_INT(p);
2495 r = link_up_down_send_message(rtnl, argv[0], index);
2496 if (r < 0) {
2497 char ifname[IF_NAMESIZE + 1];
2498
2499 return log_error_errno(r, "Failed to %s interface %s: %m",
2500 argv[1], format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX));
2501 }
2502 }
2503
2504 return r;
2505}
2506
9cd8c766
SS
2507static int link_delete(int argc, char *argv[], void *userdata) {
2508 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2509 _cleanup_set_free_ Set *indexes = NULL;
9cd8c766
SS
2510 int index, r, i;
2511 Iterator j;
38b9af61 2512 void *p;
9cd8c766
SS
2513
2514 r = sd_netlink_open(&rtnl);
2515 if (r < 0)
2516 return log_error_errno(r, "Failed to connect to netlink: %m");
2517
2518 indexes = set_new(NULL);
2519 if (!indexes)
2520 return log_oom();
2521
2522 for (i = 1; i < argc; i++) {
d308bb99 2523 index = resolve_interface_or_warn(&rtnl, argv[i]);
231d9de1
ZJS
2524 if (index < 0)
2525 return index;
9cd8c766
SS
2526
2527 r = set_put(indexes, INT_TO_PTR(index));
2528 if (r < 0)
2529 return log_oom();
2530 }
2531
38b9af61 2532 SET_FOREACH(p, indexes, j) {
d56d6cb8
YW
2533 index = PTR_TO_INT(p);
2534 r = link_delete_send_message(rtnl, index);
9cd8c766 2535 if (r < 0) {
518a66ec
YW
2536 char ifname[IF_NAMESIZE + 1];
2537
e4857ee2
YW
2538 return log_error_errno(r, "Failed to delete interface %s: %m",
2539 format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX));
9cd8c766
SS
2540 }
2541 }
2542
2543 return r;
2544}
2545
308e7dfd
YW
2546static int link_renew_one(sd_bus *bus, int index, const char *name) {
2547 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2548 int r;
2549
8a048c8c 2550 r = bus_call_method(bus, bus_network_mgr, "RenewLink", &error, NULL, "i", index);
308e7dfd
YW
2551 if (r < 0)
2552 return log_error_errno(r, "Failed to renew dynamic configuration of interface %s: %s",
2553 name, bus_error_message(&error, r));
2554
2555 return 0;
2556}
2557
2558static int link_renew(int argc, char *argv[], void *userdata) {
2559 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
f7581ed6 2560 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
308e7dfd
YW
2561 int index, i, k = 0, r;
2562
2563 r = sd_bus_open_system(&bus);
2564 if (r < 0)
2565 return log_error_errno(r, "Failed to connect system bus: %m");
2566
2567 for (i = 1; i < argc; i++) {
d308bb99 2568 index = resolve_interface_or_warn(&rtnl, argv[i]);
231d9de1
ZJS
2569 if (index < 0)
2570 return index;
308e7dfd
YW
2571
2572 r = link_renew_one(bus, index, argv[i]);
2573 if (r < 0 && k >= 0)
2574 k = r;
2575 }
2576
2577 return k;
2578}
2579
3efdd6af
SS
2580static int link_force_renew_one(sd_bus *bus, int index, const char *name) {
2581 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2582 int r;
2583
8a048c8c 2584 r = bus_call_method(bus, bus_network_mgr, "ForceRenewLink", &error, NULL, "i", index);
3efdd6af
SS
2585 if (r < 0)
2586 return log_error_errno(r, "Failed to force renew dynamic configuration of interface %s: %s",
2587 name, bus_error_message(&error, r));
2588
2589 return 0;
2590}
2591
2592static int link_force_renew(int argc, char *argv[], void *userdata) {
2593 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2594 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2595 int index, i, k = 0, r;
2596
2597 r = sd_bus_open_system(&bus);
2598 if (r < 0)
2599 return log_error_errno(r, "Failed to connect system bus: %m");
2600
2601 for (i = 1; i < argc; i++) {
2602 index = resolve_interface_or_warn(&rtnl, argv[i]);
2603 if (index < 0)
2604 return index;
2605
2606 r = link_force_renew_one(bus, index, argv[i]);
2607 if (r < 0 && k >= 0)
2608 k = r;
2609 }
2610
2611 return k;
2612}
2613
a227674c
YW
2614static int verb_reload(int argc, char *argv[], void *userdata) {
2615 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2616 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2617 int r;
2618
2619 r = sd_bus_open_system(&bus);
2620 if (r < 0)
2621 return log_error_errno(r, "Failed to connect system bus: %m");
2622
8a048c8c 2623 r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
a227674c
YW
2624 if (r < 0)
2625 return log_error_errno(r, "Failed to reload network settings: %m");
2626
2627 return 0;
2628}
2629
8dc85c5e
YW
2630static int verb_reconfigure(int argc, char *argv[], void *userdata) {
2631 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2632 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
f7581ed6 2633 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
8dc85c5e
YW
2634 _cleanup_set_free_ Set *indexes = NULL;
2635 int index, i, r;
2636 Iterator j;
2637 void *p;
2638
2639 r = sd_bus_open_system(&bus);
2640 if (r < 0)
2641 return log_error_errno(r, "Failed to connect system bus: %m");
2642
2643 indexes = set_new(NULL);
2644 if (!indexes)
2645 return log_oom();
2646
2647 for (i = 1; i < argc; i++) {
d308bb99 2648 index = resolve_interface_or_warn(&rtnl, argv[i]);
231d9de1
ZJS
2649 if (index < 0)
2650 return index;
8dc85c5e
YW
2651
2652 r = set_put(indexes, INT_TO_PTR(index));
2653 if (r < 0)
2654 return log_oom();
2655 }
2656
2657 SET_FOREACH(p, indexes, j) {
2658 index = PTR_TO_INT(p);
8a048c8c 2659 r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index);
8dc85c5e
YW
2660 if (r < 0) {
2661 char ifname[IF_NAMESIZE + 1];
2662
117caf37
ZJS
2663 return log_error_errno(r, "Failed to reconfigure network interface %s: %m",
2664 format_ifname_full(index, ifname, FORMAT_IFNAME_IFINDEX));
8dc85c5e
YW
2665 }
2666 }
2667
2668 return 0;
2669}
2670
37ec0fdd
LP
2671static int help(void) {
2672 _cleanup_free_ char *link = NULL;
2673 int r;
2674
2675 r = terminal_urlify_man("networkctl", "1", &link);
2676 if (r < 0)
2677 return log_oom();
2678
353b2baa
LP
2679 printf("%s [OPTIONS...] COMMAND\n\n"
2680 "%sQuery and control the networking subsystem.%s\n"
a459b24f 2681 "\nCommands:\n"
8dc85c5e
YW
2682 " list [PATTERN...] List links\n"
2683 " status [PATTERN...] Show link status\n"
2684 " lldp [PATTERN...] Show LLDP neighbors\n"
2685 " label Show current address label entries in the kernel\n"
2686 " delete DEVICES... Delete virtual netdevs\n"
c30ffcee
SS
2687 " up DEVICES... Bring devices up\n"
2688 " down DEVICES... Bring devices down\n"
8dc85c5e 2689 " renew DEVICES... Renew dynamic configurations\n"
3efdd6af 2690 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
8dc85c5e
YW
2691 " reconfigure DEVICES... Reconfigure interfaces\n"
2692 " reload Reload .network and .netdev files\n"
353b2baa 2693 "\nOptions:\n"
8dc85c5e
YW
2694 " -h --help Show this help\n"
2695 " --version Show package version\n"
2696 " --no-pager Do not pipe output into a pager\n"
2697 " --no-legend Do not show the headers and footers\n"
2698 " -a --all Show status for all links\n"
2699 " -s --stats Show detailed link statics\n"
10c71c36
YW
2700 " -l --full Do not ellipsize output\n"
2701 " -n --lines=INTEGER Number of journal entries to show\n"
37ec0fdd
LP
2702 "\nSee the %s for details.\n"
2703 , program_invocation_short_name
353b2baa 2704 , ansi_highlight()
ce2529b4 2705 , ansi_normal()
37ec0fdd
LP
2706 , link
2707 );
2708
2709 return 0;
ee8c4568
LP
2710}
2711
2712static int parse_argv(int argc, char *argv[]) {
2713
2714 enum {
2715 ARG_VERSION = 0x100,
2716 ARG_NO_PAGER,
2717 ARG_NO_LEGEND,
2718 };
2719
2720 static const struct option options[] = {
2721 { "help", no_argument, NULL, 'h' },
2722 { "version", no_argument, NULL, ARG_VERSION },
2723 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
2724 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
9085f64a 2725 { "all", no_argument, NULL, 'a' },
a459b24f 2726 { "stats", no_argument, NULL, 's' },
10c71c36
YW
2727 { "full", no_argument, NULL, 'l' },
2728 { "lines", required_argument, NULL, 'n' },
ee8c4568
LP
2729 {}
2730 };
2731
2732 int c;
2733
2734 assert(argc >= 0);
2735 assert(argv);
2736
10c71c36 2737 while ((c = getopt_long(argc, argv, "hasln:", options, NULL)) >= 0) {
ee8c4568
LP
2738
2739 switch (c) {
2740
2741 case 'h':
37ec0fdd 2742 return help();
ee8c4568
LP
2743
2744 case ARG_VERSION:
3f6fd1ba 2745 return version();
ee8c4568
LP
2746
2747 case ARG_NO_PAGER:
0221d68a 2748 arg_pager_flags |= PAGER_DISABLE;
ee8c4568
LP
2749 break;
2750
2751 case ARG_NO_LEGEND:
2752 arg_legend = false;
2753 break;
2754
9085f64a
LP
2755 case 'a':
2756 arg_all = true;
2757 break;
2758
a459b24f
YW
2759 case 's':
2760 arg_stats = true;
2761 break;
2762
10c71c36
YW
2763 case 'l':
2764 arg_full = true;
2765 break;
2766
2767 case 'n':
2768 if (safe_atou(optarg, &arg_lines) < 0)
2769 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2770 "Failed to parse lines '%s'", optarg);
2771 break;
2772
ee8c4568
LP
2773 case '?':
2774 return -EINVAL;
2775
2776 default:
2777 assert_not_reached("Unhandled option");
2778 }
2779 }
2780
2781 return 1;
2782}
2783
2784static int networkctl_main(int argc, char *argv[]) {
15c3626e 2785 static const Verb verbs[] = {
8dc85c5e
YW
2786 { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, list_links },
2787 { "status", VERB_ANY, VERB_ANY, 0, link_status },
2788 { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status },
2789 { "label", VERB_ANY, VERB_ANY, 0, list_address_labels },
2790 { "delete", 2, VERB_ANY, 0, link_delete },
c30ffcee
SS
2791 { "up", 2, VERB_ANY, 0, link_up_down },
2792 { "down", 2, VERB_ANY, 0, link_up_down },
8dc85c5e 2793 { "renew", 2, VERB_ANY, 0, link_renew },
3efdd6af 2794 { "forcerenew", 2, VERB_ANY, 0, link_force_renew },
8dc85c5e
YW
2795 { "reconfigure", 2, VERB_ANY, 0, verb_reconfigure },
2796 { "reload", 1, 1, 0, verb_reload },
266b5389 2797 {}
ee8c4568
LP
2798 };
2799
266b5389 2800 return dispatch_verb(argc, argv, verbs, NULL);
ee8c4568
LP
2801}
2802
58fb3678
LP
2803static void warn_networkd_missing(void) {
2804
2805 if (access("/run/systemd/netif/state", F_OK) >= 0)
2806 return;
2807
2808 fprintf(stderr, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
2809}
2810
4e2ca442 2811static int run(int argc, char* argv[]) {
ee8c4568
LP
2812 int r;
2813
1a043959 2814 log_show_color(true);
ee8c4568
LP
2815 log_parse_environment();
2816 log_open();
2817
2818 r = parse_argv(argc, argv);
2819 if (r <= 0)
4e2ca442 2820 return r;
ee8c4568 2821
58fb3678
LP
2822 warn_networkd_missing();
2823
4e2ca442 2824 return networkctl_main(argc, argv);
ee8c4568 2825}
4e2ca442
ZJS
2826
2827DEFINE_MAIN_FUNCTION(run);