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