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