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