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