]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkctl.c
Merge pull request #29427 from ddstreet/cryptenroll_specify_handle_index
[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 "bus-wait-for-jobs.h"
30 #include "conf-files.h"
31 #include "device-util.h"
32 #include "edit-util.h"
33 #include "escape.h"
34 #include "ether-addr-util.h"
35 #include "ethtool-util.h"
36 #include "fd-util.h"
37 #include "format-table.h"
38 #include "format-util.h"
39 #include "fs-util.h"
40 #include "geneve-util.h"
41 #include "glob-util.h"
42 #include "hwdb-util.h"
43 #include "ipvlan-util.h"
44 #include "local-addresses.h"
45 #include "locale-util.h"
46 #include "logs-show.h"
47 #include "macro.h"
48 #include "macvlan-util.h"
49 #include "main-func.h"
50 #include "netif-util.h"
51 #include "netlink-util.h"
52 #include "network-internal.h"
53 #include "network-util.h"
54 #include "pager.h"
55 #include "parse-argument.h"
56 #include "parse-util.h"
57 #include "path-lookup.h"
58 #include "path-util.h"
59 #include "pretty-print.h"
60 #include "set.h"
61 #include "sigbus.h"
62 #include "socket-netlink.h"
63 #include "socket-util.h"
64 #include "sort-util.h"
65 #include "sparse-endian.h"
66 #include "stdio-util.h"
67 #include "string-table.h"
68 #include "string-util.h"
69 #include "strv.h"
70 #include "strxcpyx.h"
71 #include "terminal-util.h"
72 #include "unit-def.h"
73 #include "verbs.h"
74 #include "virt.h"
75 #include "wifi-util.h"
76
77 /* Kernel defines MODULE_NAME_LEN as 64 - sizeof(unsigned long). So, 64 is enough. */
78 #define NETDEV_KIND_MAX 64
79
80 /* use 128 kB for receive socket kernel queue, we shouldn't need more here */
81 #define RCVBUF_SIZE (128*1024)
82
83 static PagerFlags arg_pager_flags = 0;
84 static bool arg_legend = true;
85 static bool arg_no_reload = false;
86 static bool arg_all = false;
87 static bool arg_stats = false;
88 static bool arg_full = false;
89 static unsigned arg_lines = 10;
90 static char *arg_drop_in = NULL;
91 static 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 static 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 static 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
1726 if (sd_device_get_property_value(info->sd_device, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
1727 (void) sd_device_get_property_value(info->sd_device, "ID_VENDOR", &vendor);
1728
1729 if (sd_device_get_property_value(info->sd_device, "ID_MODEL_FROM_DATABASE", &model) < 0)
1730 (void) sd_device_get_property_value(info->sd_device, "ID_MODEL", &model);
1731 }
1732
1733 r = net_get_type_string(info->sd_device, info->iftype, &t);
1734 if (r == -ENOMEM)
1735 return log_oom();
1736
1737 char lease_file[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)];
1738 xsprintf(lease_file, "/run/systemd/netif/leases/%i", info->ifindex);
1739
1740 (void) dhcp_lease_load(&lease, lease_file);
1741
1742 r = format_dropins(network_dropins);
1743 if (r < 0)
1744 return r;
1745
1746 if (strv_prepend(&network_dropins, network) < 0)
1747 return log_oom();
1748
1749 r = format_dropins(link_dropins);
1750 if (r < 0)
1751 return r;
1752
1753 if (strv_prepend(&link_dropins, link) < 0)
1754 return log_oom();
1755
1756 table = table_new_vertical();
1757 if (!table)
1758 return log_oom();
1759
1760 if (arg_full)
1761 table_set_width(table, 0);
1762
1763 /* unit files and basic states. */
1764 r = table_add_many(table,
1765 TABLE_FIELD, "Link File",
1766 TABLE_STRV, link_dropins ?: STRV_MAKE("n/a"),
1767 TABLE_FIELD, "Network File",
1768 TABLE_STRV, network_dropins ?: STRV_MAKE("n/a"),
1769 TABLE_FIELD, "State");
1770 if (r < 0)
1771 return table_log_add_error(r);
1772
1773 r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)",
1774 on_color_operational, strna(operational_state), off_color_operational,
1775 on_color_setup, setup_state ?: "unmanaged", off_color_setup);
1776 if (r < 0)
1777 return table_log_add_error(r);
1778
1779 r = table_add_many(table,
1780 TABLE_FIELD, "Online state",
1781 TABLE_STRING, online_state ?: "unknown",
1782 TABLE_SET_COLOR, on_color_online);
1783 if (r < 0)
1784 return table_log_add_error(r);
1785
1786 r = table_add_string_line(table, "Type", t);
1787 if (r < 0)
1788 return r;
1789
1790 r = table_add_string_line(table, "Kind", info->netdev_kind);
1791 if (r < 0)
1792 return r;
1793
1794 r = table_add_string_line(table, "Path", path);
1795 if (r < 0)
1796 return r;
1797
1798 r = table_add_string_line(table, "Driver", driver);
1799 if (r < 0)
1800 return r;
1801
1802 r = table_add_string_line(table, "Vendor", vendor);
1803 if (r < 0)
1804 return r;
1805
1806 r = table_add_string_line(table, "Model", model);
1807 if (r < 0)
1808 return r;
1809
1810 strv_sort(info->alternative_names);
1811 r = dump_list(table, "Alternative Names", info->alternative_names);
1812 if (r < 0)
1813 return r;
1814
1815 if (info->has_hw_address) {
1816 r = dump_hw_address(table, hwdb, "Hardware Address", &info->hw_address);
1817 if (r < 0)
1818 return r;
1819 }
1820
1821 if (info->has_permanent_hw_address) {
1822 r = dump_hw_address(table, hwdb, "Permanent Hardware Address", &info->permanent_hw_address);
1823 if (r < 0)
1824 return r;
1825 }
1826
1827 if (info->mtu > 0) {
1828 char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)];
1829
1830 xsprintf(min_str, "%" PRIu32, info->min_mtu);
1831 xsprintf(max_str, "%" PRIu32, info->max_mtu);
1832
1833 r = table_add_cell(table, NULL, TABLE_FIELD, "MTU");
1834 if (r < 0)
1835 return table_log_add_error(r);
1836
1837 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s",
1838 info->mtu,
1839 info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "",
1840 info->min_mtu > 0 ? "min: " : "",
1841 info->min_mtu > 0 ? min_str : "",
1842 info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "",
1843 info->max_mtu > 0 ? "max: " : "",
1844 info->max_mtu > 0 ? max_str : "",
1845 info->min_mtu > 0 || info->max_mtu > 0 ? ")" : "");
1846 if (r < 0)
1847 return table_log_add_error(r);
1848 }
1849
1850 r = table_add_string_line(table, "QDisc", info->qdisc);
1851 if (r < 0)
1852 return r;
1853
1854 if (info->master > 0) {
1855 r = table_add_many(table,
1856 TABLE_FIELD, "Master",
1857 TABLE_IFINDEX, info->master);
1858 if (r < 0)
1859 return table_log_add_error(r);
1860 }
1861
1862 if (info->has_ipv6_address_generation_mode) {
1863 static const struct {
1864 const char *mode;
1865 } mode_table[] = {
1866 { "eui64" },
1867 { "none" },
1868 { "stable-privacy" },
1869 { "random" },
1870 };
1871
1872 r = table_add_many(table,
1873 TABLE_FIELD, "IPv6 Address Generation Mode",
1874 TABLE_STRING, mode_table[info->addr_gen_mode]);
1875 if (r < 0)
1876 return table_log_add_error(r);
1877 }
1878
1879 if (streq_ptr(info->netdev_kind, "bridge")) {
1880 r = table_add_many(table,
1881 TABLE_FIELD, "Forward Delay",
1882 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->forward_delay),
1883 TABLE_FIELD, "Hello Time",
1884 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->hello_time),
1885 TABLE_FIELD, "Max Age",
1886 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->max_age),
1887 TABLE_FIELD, "Ageing Time",
1888 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->ageing_time),
1889 TABLE_FIELD, "Priority",
1890 TABLE_UINT16, info->priority,
1891 TABLE_FIELD, "STP",
1892 TABLE_BOOLEAN, info->stp_state > 0,
1893 TABLE_FIELD, "Multicast IGMP Version",
1894 TABLE_UINT8, info->mcast_igmp_version,
1895 TABLE_FIELD, "Cost",
1896 TABLE_UINT32, info->cost);
1897 if (r < 0)
1898 return table_log_add_error(r);
1899
1900 if (info->port_state <= BR_STATE_BLOCKING) {
1901 r = table_add_many(table,
1902 TABLE_FIELD, "Port State",
1903 TABLE_STRING, bridge_state_to_string(info->port_state));
1904 if (r < 0)
1905 return table_log_add_error(r);
1906 }
1907
1908 } else if (streq_ptr(info->netdev_kind, "bond")) {
1909 r = table_add_many(table,
1910 TABLE_FIELD, "Mode",
1911 TABLE_STRING, bond_mode_to_string(info->mode),
1912 TABLE_FIELD, "Miimon",
1913 TABLE_TIMESPAN_MSEC, info->miimon * USEC_PER_MSEC,
1914 TABLE_FIELD, "Updelay",
1915 TABLE_TIMESPAN_MSEC, info->updelay * USEC_PER_MSEC,
1916 TABLE_FIELD, "Downdelay",
1917 TABLE_TIMESPAN_MSEC, info->downdelay * USEC_PER_MSEC);
1918 if (r < 0)
1919 return table_log_add_error(r);
1920
1921 } else if (streq_ptr(info->netdev_kind, "vxlan")) {
1922 char ttl[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1923
1924 if (info->vxlan_info.vni > 0) {
1925 r = table_add_many(table,
1926 TABLE_FIELD, "VNI",
1927 TABLE_UINT32, info->vxlan_info.vni);
1928 if (r < 0)
1929 return table_log_add_error(r);
1930 }
1931
1932 if (IN_SET(info->vxlan_info.group_family, AF_INET, AF_INET6)) {
1933 const char *p;
1934
1935 r = in_addr_is_multicast(info->vxlan_info.group_family, &info->vxlan_info.group);
1936 if (r <= 0)
1937 p = "Remote";
1938 else
1939 p = "Group";
1940
1941 r = table_add_many(table,
1942 TABLE_FIELD, p,
1943 info->vxlan_info.group_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, &info->vxlan_info.group);
1944 if (r < 0)
1945 return table_log_add_error(r);
1946 }
1947
1948 if (IN_SET(info->vxlan_info.local_family, AF_INET, AF_INET6)) {
1949 r = table_add_many(table,
1950 TABLE_FIELD, "Local",
1951 info->vxlan_info.local_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR, &info->vxlan_info.local);
1952 if (r < 0)
1953 return table_log_add_error(r);
1954 }
1955
1956 if (info->vxlan_info.dest_port > 0) {
1957 r = table_add_many(table,
1958 TABLE_FIELD, "Destination Port",
1959 TABLE_UINT16, be16toh(info->vxlan_info.dest_port));
1960 if (r < 0)
1961 return table_log_add_error(r);
1962 }
1963
1964 if (info->vxlan_info.link > 0) {
1965 r = table_add_many(table,
1966 TABLE_FIELD, "Underlying Device",
1967 TABLE_IFINDEX, info->vxlan_info.link);
1968 if (r < 0)
1969 return table_log_add_error(r);
1970 }
1971
1972 r = table_add_many(table,
1973 TABLE_FIELD, "Learning",
1974 TABLE_BOOLEAN, info->vxlan_info.learning,
1975 TABLE_FIELD, "RSC",
1976 TABLE_BOOLEAN, info->vxlan_info.rsc,
1977 TABLE_FIELD, "L3MISS",
1978 TABLE_BOOLEAN, info->vxlan_info.l3miss,
1979 TABLE_FIELD, "L2MISS",
1980 TABLE_BOOLEAN, info->vxlan_info.l2miss);
1981 if (r < 0)
1982 return table_log_add_error(r);
1983
1984 if (info->vxlan_info.tos > 1) {
1985 r = table_add_many(table,
1986 TABLE_FIELD, "TOS",
1987 TABLE_UINT8, info->vxlan_info.tos);
1988 if (r < 0)
1989 return table_log_add_error(r);
1990 }
1991
1992 if (info->vxlan_info.ttl > 0)
1993 xsprintf(ttl, "%" PRIu8, info->vxlan_info.ttl);
1994 else
1995 strcpy(ttl, "auto");
1996
1997 r = table_add_many(table,
1998 TABLE_FIELD, "TTL",
1999 TABLE_STRING, ttl);
2000 if (r < 0)
2001 return table_log_add_error(r);
2002
2003 } else if (streq_ptr(info->netdev_kind, "vlan") && info->vlan_id > 0) {
2004 r = table_add_many(table,
2005 TABLE_FIELD, "VLan Id",
2006 TABLE_UINT16, info->vlan_id);
2007 if (r < 0)
2008 return table_log_add_error(r);
2009
2010 } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
2011 if (in_addr_is_set(AF_INET, &info->local)) {
2012 r = table_add_many(table,
2013 TABLE_FIELD, "Local",
2014 TABLE_IN_ADDR, &info->local);
2015 if (r < 0)
2016 return table_log_add_error(r);
2017 }
2018
2019 if (in_addr_is_set(AF_INET, &info->remote)) {
2020 r = table_add_many(table,
2021 TABLE_FIELD, "Remote",
2022 TABLE_IN_ADDR, &info->remote);
2023 if (r < 0)
2024 return table_log_add_error(r);
2025 }
2026
2027 } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
2028 if (in_addr_is_set(AF_INET6, &info->local)) {
2029 r = table_add_many(table,
2030 TABLE_FIELD, "Local",
2031 TABLE_IN6_ADDR, &info->local);
2032 if (r < 0)
2033 return table_log_add_error(r);
2034 }
2035
2036 if (in_addr_is_set(AF_INET6, &info->remote)) {
2037 r = table_add_many(table,
2038 TABLE_FIELD, "Remote",
2039 TABLE_IN6_ADDR, &info->remote);
2040 if (r < 0)
2041 return table_log_add_error(r);
2042 }
2043
2044 } else if (streq_ptr(info->netdev_kind, "geneve")) {
2045 r = table_add_many(table,
2046 TABLE_FIELD, "VNI",
2047 TABLE_UINT32, info->vni);
2048 if (r < 0)
2049 return table_log_add_error(r);
2050
2051 if (info->has_tunnel_ipv4 && in_addr_is_set(AF_INET, &info->remote)) {
2052 r = table_add_many(table,
2053 TABLE_FIELD, "Remote",
2054 TABLE_IN_ADDR, &info->remote);
2055 if (r < 0)
2056 return table_log_add_error(r);
2057 } else if (in_addr_is_set(AF_INET6, &info->remote)) {
2058 r = table_add_many(table,
2059 TABLE_FIELD, "Remote",
2060 TABLE_IN6_ADDR, &info->remote);
2061 if (r < 0)
2062 return table_log_add_error(r);
2063 }
2064
2065 if (info->ttl > 0) {
2066 r = table_add_many(table,
2067 TABLE_FIELD, "TTL",
2068 TABLE_UINT8, info->ttl);
2069 if (r < 0)
2070 return table_log_add_error(r);
2071 }
2072
2073 if (info->tos > 0) {
2074 r = table_add_many(table,
2075 TABLE_FIELD, "TOS",
2076 TABLE_UINT8, info->tos);
2077 if (r < 0)
2078 return table_log_add_error(r);
2079 }
2080
2081 r = table_add_many(table,
2082 TABLE_FIELD, "Port",
2083 TABLE_UINT16, info->tunnel_port,
2084 TABLE_FIELD, "Inherit",
2085 TABLE_STRING, geneve_df_to_string(info->inherit));
2086 if (r < 0)
2087 return table_log_add_error(r);
2088
2089 if (info->df > 0) {
2090 r = table_add_many(table,
2091 TABLE_FIELD, "IPDoNotFragment",
2092 TABLE_UINT8, info->df);
2093 if (r < 0)
2094 return table_log_add_error(r);
2095 }
2096
2097 r = table_add_many(table,
2098 TABLE_FIELD, "UDPChecksum",
2099 TABLE_BOOLEAN, info->csum,
2100 TABLE_FIELD, "UDP6ZeroChecksumTx",
2101 TABLE_BOOLEAN, info->csum6_tx,
2102 TABLE_FIELD, "UDP6ZeroChecksumRx",
2103 TABLE_BOOLEAN, info->csum6_rx);
2104 if (r < 0)
2105 return table_log_add_error(r);
2106
2107 if (info->label > 0) {
2108 r = table_add_many(table,
2109 TABLE_FIELD, "FlowLabel",
2110 TABLE_UINT32, info->label);
2111 if (r < 0)
2112 return table_log_add_error(r);
2113 }
2114
2115 } else if (STRPTR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) {
2116 r = table_add_many(table,
2117 TABLE_FIELD, "Mode",
2118 TABLE_STRING, macvlan_mode_to_string(info->macvlan_mode));
2119 if (r < 0)
2120 return table_log_add_error(r);
2121
2122 } else if (streq_ptr(info->netdev_kind, "ipvlan")) {
2123 const char *p;
2124
2125 if (info->ipvlan_flags & IPVLAN_F_PRIVATE)
2126 p = "private";
2127 else if (info->ipvlan_flags & IPVLAN_F_VEPA)
2128 p = "vepa";
2129 else
2130 p = "bridge";
2131
2132 r = table_add_cell(table, NULL, TABLE_FIELD, "Mode");
2133 if (r < 0)
2134 return table_log_add_error(r);
2135
2136 r = table_add_cell_stringf(table, NULL, "%s (%s)",
2137 ipvlan_mode_to_string(info->ipvlan_mode), p);
2138 if (r < 0)
2139 return table_log_add_error(r);
2140 }
2141
2142 if (info->has_wlan_link_info) {
2143 _cleanup_free_ char *esc = NULL;
2144
2145 r = table_add_cell(table, NULL, TABLE_FIELD, "Wi-Fi access point");
2146 if (r < 0)
2147 return table_log_add_error(r);
2148
2149 if (info->ssid)
2150 esc = cescape(info->ssid);
2151
2152 r = table_add_cell_stringf(table, NULL, "%s (%s)",
2153 strnull(esc),
2154 ETHER_ADDR_TO_STR(&info->bssid));
2155 if (r < 0)
2156 return table_log_add_error(r);
2157 }
2158
2159 if (info->has_bitrates) {
2160 r = table_add_cell(table, NULL, TABLE_FIELD, "Bit Rate (Tx/Rx)");
2161 if (r < 0)
2162 return table_log_add_error(r);
2163
2164 r = table_add_cell_stringf(table, NULL, "%sbps/%sbps",
2165 FORMAT_BYTES_FULL(info->tx_bitrate, 0),
2166 FORMAT_BYTES_FULL(info->rx_bitrate, 0));
2167 if (r < 0)
2168 return table_log_add_error(r);
2169 }
2170
2171 if (info->has_tx_queues || info->has_rx_queues) {
2172 r = table_add_cell(table, NULL, TABLE_FIELD, "Number of Queues (Tx/Rx)");
2173 if (r < 0)
2174 return table_log_add_error(r);
2175
2176 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues);
2177 if (r < 0)
2178 return table_log_add_error(r);
2179 }
2180
2181 if (info->has_ethtool_link_info) {
2182 if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) {
2183 r = table_add_many(table,
2184 TABLE_FIELD, "Auto negotiation",
2185 TABLE_BOOLEAN, info->autonegotiation == AUTONEG_ENABLE);
2186 if (r < 0)
2187 return table_log_add_error(r);
2188 }
2189
2190 if (info->speed > 0 && info->speed != UINT64_MAX) {
2191 r = table_add_many(table,
2192 TABLE_FIELD, "Speed",
2193 TABLE_BPS, info->speed);
2194 if (r < 0)
2195 return table_log_add_error(r);
2196 }
2197
2198 r = table_add_string_line(table, "Duplex", duplex_to_string(info->duplex));
2199 if (r < 0)
2200 return r;
2201
2202 r = table_add_string_line(table, "Port", port_to_string(info->port));
2203 if (r < 0)
2204 return r;
2205 }
2206
2207 r = dump_addresses(rtnl, lease, table, info->ifindex);
2208 if (r < 0)
2209 return r;
2210
2211 r = dump_gateways(rtnl, hwdb, table, info->ifindex);
2212 if (r < 0)
2213 return r;
2214
2215 r = dump_list(table, "DNS", dns);
2216 if (r < 0)
2217 return r;
2218
2219 r = dump_list(table, "Search Domains", search_domains);
2220 if (r < 0)
2221 return r;
2222
2223 r = dump_list(table, "Route Domains", route_domains);
2224 if (r < 0)
2225 return r;
2226
2227 r = dump_list(table, "NTP", ntp);
2228 if (r < 0)
2229 return r;
2230
2231 r = dump_list(table, "SIP", sip);
2232 if (r < 0)
2233 return r;
2234
2235 r = dump_ifindexes(table, "Carrier Bound To", carrier_bound_to);
2236 if (r < 0)
2237 return r;
2238
2239 r = dump_ifindexes(table, "Carrier Bound By", carrier_bound_by);
2240 if (r < 0)
2241 return r;
2242
2243 r = table_add_string_line(table, "Activation Policy", activation_policy);
2244 if (r < 0)
2245 return r;
2246
2247 r = sd_network_link_get_required_for_online(info->ifindex);
2248 if (r >= 0) {
2249 r = table_add_many(table,
2250 TABLE_FIELD, "Required For Online",
2251 TABLE_BOOLEAN, r);
2252 if (r < 0)
2253 return table_log_add_error(r);
2254 }
2255
2256 if (captive_portal) {
2257 r = table_add_many(table,
2258 TABLE_FIELD, "Captive Portal",
2259 TABLE_STRING, captive_portal,
2260 TABLE_SET_URL, captive_portal);
2261 if (r < 0)
2262 return table_log_add_error(r);
2263 }
2264
2265 if (lease) {
2266 const void *client_id;
2267 size_t client_id_len;
2268 const char *tz;
2269
2270 r = sd_dhcp_lease_get_timezone(lease, &tz);
2271 if (r >= 0) {
2272 r = table_add_many(table,
2273 TABLE_FIELD, "Time Zone",
2274 TABLE_STRING, tz);
2275 if (r < 0)
2276 return table_log_add_error(r);
2277 }
2278
2279 r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len);
2280 if (r >= 0) {
2281 _cleanup_free_ char *id = NULL;
2282
2283 r = sd_dhcp_client_id_to_string(client_id, client_id_len, &id);
2284 if (r >= 0) {
2285 r = table_add_many(table,
2286 TABLE_FIELD, "DHCP4 Client ID",
2287 TABLE_STRING, id);
2288 if (r < 0)
2289 return table_log_add_error(r);
2290 }
2291 }
2292 }
2293
2294 r = sd_network_link_get_dhcp6_client_iaid_string(info->ifindex, &iaid);
2295 if (r >= 0) {
2296 r = table_add_many(table,
2297 TABLE_FIELD, "DHCP6 Client IAID",
2298 TABLE_STRING, iaid);
2299 if (r < 0)
2300 return table_log_add_error(r);
2301 }
2302
2303 r = sd_network_link_get_dhcp6_client_duid_string(info->ifindex, &duid);
2304 if (r >= 0) {
2305 r = table_add_many(table,
2306 TABLE_FIELD, "DHCP6 Client DUID",
2307 TABLE_STRING, duid);
2308 if (r < 0)
2309 return table_log_add_error(r);
2310 }
2311
2312 r = dump_lldp_neighbors(table, "Connected To", info->ifindex);
2313 if (r < 0)
2314 return r;
2315
2316 r = dump_dhcp_leases(table, "Offered DHCP leases", bus, info);
2317 if (r < 0)
2318 return r;
2319
2320 r = dump_statistics(table, info);
2321 if (r < 0)
2322 return r;
2323
2324 /* First line: circle, ifindex, ifname. */
2325 printf("%s%s%s %d: %s\n",
2326 on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational,
2327 info->ifindex, info->name);
2328
2329 r = table_print(table, NULL);
2330 if (r < 0)
2331 return table_log_print_error(r);
2332
2333 return show_logs(info);
2334 }
2335
2336 static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
2337 _cleanup_free_ char *operational_state = NULL, *online_state = NULL, *netifs_joined = NULL;
2338 _cleanup_strv_free_ char **netifs = NULL, **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
2339 const char *on_color_operational, *off_color_operational, *on_color_online;
2340 _cleanup_(table_unrefp) Table *table = NULL;
2341 int r;
2342
2343 assert(rtnl);
2344
2345 (void) sd_network_get_operational_state(&operational_state);
2346 operational_state_to_color(NULL, operational_state, &on_color_operational, &off_color_operational);
2347
2348 (void) sd_network_get_online_state(&online_state);
2349 online_state_to_color(online_state, &on_color_online, NULL);
2350
2351 table = table_new_vertical();
2352 if (!table)
2353 return log_oom();
2354
2355 if (arg_full)
2356 table_set_width(table, 0);
2357
2358 r = get_files_in_directory("/run/systemd/netif/links/", &netifs);
2359 if (r < 0 && r != -ENOENT)
2360 return log_error_errno(r, "Failed to list network interfaces: %m");
2361 else if (r > 0) {
2362 netifs_joined = strv_join(netifs, ", ");
2363 if (!netifs_joined)
2364 return log_oom();
2365 }
2366
2367 r = table_add_many(table,
2368 TABLE_FIELD, "State",
2369 TABLE_STRING, strna(operational_state),
2370 TABLE_SET_COLOR, on_color_operational,
2371 TABLE_FIELD, "Online state",
2372 TABLE_STRING, online_state ?: "unknown",
2373 TABLE_SET_COLOR, on_color_online);
2374 if (r < 0)
2375 return table_log_add_error(r);
2376
2377 r = dump_addresses(rtnl, NULL, table, 0);
2378 if (r < 0)
2379 return r;
2380
2381 r = dump_gateways(rtnl, hwdb, table, 0);
2382 if (r < 0)
2383 return r;
2384
2385 (void) sd_network_get_dns(&dns);
2386 r = dump_list(table, "DNS", dns);
2387 if (r < 0)
2388 return r;
2389
2390 (void) sd_network_get_search_domains(&search_domains);
2391 r = dump_list(table, "Search Domains", search_domains);
2392 if (r < 0)
2393 return r;
2394
2395 (void) sd_network_get_route_domains(&route_domains);
2396 r = dump_list(table, "Route Domains", route_domains);
2397 if (r < 0)
2398 return r;
2399
2400 (void) sd_network_get_ntp(&ntp);
2401 r = dump_list(table, "NTP", ntp);
2402 if (r < 0)
2403 return r;
2404
2405 printf("%s%s%s Interfaces: %s\n",
2406 on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational,
2407 strna(netifs_joined));
2408
2409 r = table_print(table, NULL);
2410 if (r < 0)
2411 return table_log_print_error(r);
2412
2413 return show_logs(NULL);
2414 }
2415
2416 static int link_status(int argc, char *argv[], void *userdata) {
2417 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2418 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2419 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
2420 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
2421 int r, c;
2422
2423 r = acquire_bus(&bus);
2424 if (r < 0)
2425 return r;
2426
2427 if (arg_json_format_flags != JSON_FORMAT_OFF) {
2428 if (arg_all || argc <= 1)
2429 return dump_manager_description(bus);
2430 else
2431 return dump_link_description(bus, strv_skip(argv, 1));
2432 }
2433
2434 pager_open(arg_pager_flags);
2435
2436 r = sd_netlink_open(&rtnl);
2437 if (r < 0)
2438 return log_error_errno(r, "Failed to connect to netlink: %m");
2439
2440 r = sd_hwdb_new(&hwdb);
2441 if (r < 0)
2442 log_debug_errno(r, "Failed to open hardware database: %m");
2443
2444 if (arg_all)
2445 c = acquire_link_info(bus, rtnl, NULL, &links);
2446 else if (argc <= 1)
2447 return system_status(rtnl, hwdb);
2448 else
2449 c = acquire_link_info(bus, rtnl, argv + 1, &links);
2450 if (c < 0)
2451 return c;
2452
2453 r = 0;
2454
2455 bool first = true;
2456 FOREACH_ARRAY(i, links, c) {
2457 if (!first)
2458 putchar('\n');
2459
2460 RET_GATHER(r, link_status_one(bus, rtnl, hwdb, i));
2461
2462 first = false;
2463 }
2464
2465 return r;
2466 }
2467
2468 static char *lldp_capabilities_to_string(uint16_t x) {
2469 static const char characters[] = {
2470 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2471 };
2472 char *ret;
2473 unsigned i;
2474
2475 ret = new(char, ELEMENTSOF(characters) + 1);
2476 if (!ret)
2477 return NULL;
2478
2479 for (i = 0; i < ELEMENTSOF(characters); i++)
2480 ret[i] = (x & (1U << i)) ? characters[i] : '.';
2481
2482 ret[i] = 0;
2483 return ret;
2484 }
2485
2486 static void lldp_capabilities_legend(uint16_t x) {
2487 unsigned cols = columns();
2488 static const char* const table[] = {
2489 "o - Other",
2490 "p - Repeater",
2491 "b - Bridge",
2492 "w - WLAN Access Point",
2493 "r - Router",
2494 "t - Telephone",
2495 "d - DOCSIS cable device",
2496 "a - Station",
2497 "c - Customer VLAN",
2498 "s - Service VLAN",
2499 "m - Two-port MAC Relay (TPMR)",
2500 };
2501
2502 if (x == 0)
2503 return;
2504
2505 printf("\nCapability Flags:\n");
2506 for (unsigned w = 0, i = 0; i < ELEMENTSOF(table); i++)
2507 if (x & (1U << i) || arg_all) {
2508 bool newline;
2509
2510 newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols;
2511 if (newline)
2512 w = 0;
2513 w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]);
2514 }
2515 puts("");
2516 }
2517
2518 static int link_lldp_status(int argc, char *argv[], void *userdata) {
2519 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2520 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
2521 _cleanup_(table_unrefp) Table *table = NULL;
2522 int r, c, m = 0;
2523 uint16_t all = 0;
2524 TableCell *cell;
2525
2526 r = sd_netlink_open(&rtnl);
2527 if (r < 0)
2528 return log_error_errno(r, "Failed to connect to netlink: %m");
2529
2530 c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
2531 if (c < 0)
2532 return c;
2533
2534 pager_open(arg_pager_flags);
2535
2536 table = table_new("link",
2537 "chassis-id",
2538 "system-name",
2539 "caps",
2540 "port-id",
2541 "port-description");
2542 if (!table)
2543 return log_oom();
2544
2545 if (arg_full)
2546 table_set_width(table, 0);
2547
2548 table_set_header(table, arg_legend);
2549
2550 assert_se(cell = table_get_cell(table, 0, 3));
2551 table_set_minimum_width(table, cell, 11);
2552 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
2553
2554 FOREACH_ARRAY(link, links, c) {
2555 _cleanup_fclose_ FILE *f = NULL;
2556
2557 r = open_lldp_neighbors(link->ifindex, &f);
2558 if (r == -ENOENT)
2559 continue;
2560 if (r < 0) {
2561 log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", link->ifindex);
2562 continue;
2563 }
2564
2565 for (;;) {
2566 const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL;
2567 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
2568 _cleanup_free_ char *capabilities = NULL;
2569 uint16_t cc;
2570
2571 r = next_lldp_neighbor(f, &n);
2572 if (r < 0) {
2573 log_warning_errno(r, "Failed to read neighbor data: %m");
2574 break;
2575 }
2576 if (r == 0)
2577 break;
2578
2579 (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
2580 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
2581 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
2582 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
2583
2584 if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) {
2585 capabilities = lldp_capabilities_to_string(cc);
2586 all |= cc;
2587 }
2588
2589 r = table_add_many(table,
2590 TABLE_STRING, link->name,
2591 TABLE_STRING, chassis_id,
2592 TABLE_STRING, system_name,
2593 TABLE_STRING, capabilities,
2594 TABLE_STRING, port_id,
2595 TABLE_STRING, port_description);
2596 if (r < 0)
2597 return table_log_add_error(r);
2598
2599 m++;
2600 }
2601 }
2602
2603 r = table_print(table, NULL);
2604 if (r < 0)
2605 return table_log_print_error(r);
2606
2607 if (arg_legend) {
2608 lldp_capabilities_legend(all);
2609 printf("\n%i neighbors listed.\n", m);
2610 }
2611
2612 return 0;
2613 }
2614
2615 static int link_delete_send_message(sd_netlink *rtnl, int index) {
2616 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2617 int r;
2618
2619 assert(rtnl);
2620 assert(index >= 0);
2621
2622 r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index);
2623 if (r < 0)
2624 return rtnl_log_create_error(r);
2625
2626 r = sd_netlink_call(rtnl, req, 0, NULL);
2627 if (r < 0)
2628 return r;
2629
2630 return 0;
2631 }
2632
2633 static int link_up_down_send_message(sd_netlink *rtnl, char *command, int index) {
2634 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2635 int r;
2636
2637 assert(rtnl);
2638 assert(index >= 0);
2639
2640 r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, index);
2641 if (r < 0)
2642 return rtnl_log_create_error(r);
2643
2644 if (streq(command, "up"))
2645 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
2646 else
2647 r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP);
2648 if (r < 0)
2649 return log_error_errno(r, "Could not set link flags: %m");
2650
2651 r = sd_netlink_call(rtnl, req, 0, NULL);
2652 if (r < 0)
2653 return r;
2654
2655 return 0;
2656 }
2657
2658 static int link_up_down(int argc, char *argv[], void *userdata) {
2659 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2660 _cleanup_set_free_ Set *indexes = NULL;
2661 int index, r;
2662 void *p;
2663
2664 r = sd_netlink_open(&rtnl);
2665 if (r < 0)
2666 return log_error_errno(r, "Failed to connect to netlink: %m");
2667
2668 indexes = set_new(NULL);
2669 if (!indexes)
2670 return log_oom();
2671
2672 for (int i = 1; i < argc; i++) {
2673 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2674 if (index < 0)
2675 return index;
2676
2677 r = set_put(indexes, INT_TO_PTR(index));
2678 if (r < 0)
2679 return log_oom();
2680 }
2681
2682 SET_FOREACH(p, indexes) {
2683 index = PTR_TO_INT(p);
2684 r = link_up_down_send_message(rtnl, argv[0], index);
2685 if (r < 0)
2686 return log_error_errno(r, "Failed to bring %s interface %s: %m",
2687 argv[0], FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX));
2688 }
2689
2690 return r;
2691 }
2692
2693 static int link_delete(int argc, char *argv[], void *userdata) {
2694 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2695 _cleanup_set_free_ Set *indexes = NULL;
2696 int index, r;
2697 void *p;
2698
2699 r = sd_netlink_open(&rtnl);
2700 if (r < 0)
2701 return log_error_errno(r, "Failed to connect to netlink: %m");
2702
2703 indexes = set_new(NULL);
2704 if (!indexes)
2705 return log_oom();
2706
2707 for (int i = 1; i < argc; i++) {
2708 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2709 if (index < 0)
2710 return index;
2711
2712 r = set_put(indexes, INT_TO_PTR(index));
2713 if (r < 0)
2714 return log_oom();
2715 }
2716
2717 SET_FOREACH(p, indexes) {
2718 index = PTR_TO_INT(p);
2719 r = link_delete_send_message(rtnl, index);
2720 if (r < 0)
2721 return log_error_errno(r, "Failed to delete interface %s: %m",
2722 FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX));
2723 }
2724
2725 return r;
2726 }
2727
2728 static int link_renew_one(sd_bus *bus, int index, const char *name) {
2729 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2730 int r;
2731
2732 assert(bus);
2733 assert(index >= 0);
2734 assert(name);
2735
2736 r = bus_call_method(bus, bus_network_mgr, "RenewLink", &error, NULL, "i", index);
2737 if (r < 0)
2738 return log_error_errno(r, "Failed to renew dynamic configuration of interface %s: %s",
2739 name, bus_error_message(&error, r));
2740
2741 return 0;
2742 }
2743
2744 static int link_renew(int argc, char *argv[], void *userdata) {
2745 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2746 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2747 int index, k = 0, r;
2748
2749 r = acquire_bus(&bus);
2750 if (r < 0)
2751 return r;
2752
2753 for (int i = 1; i < argc; i++) {
2754 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2755 if (index < 0)
2756 return index;
2757
2758 r = link_renew_one(bus, index, argv[i]);
2759 if (r < 0 && k >= 0)
2760 k = r;
2761 }
2762
2763 return k;
2764 }
2765
2766 static int link_force_renew_one(sd_bus *bus, int index, const char *name) {
2767 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2768 int r;
2769
2770 assert(bus);
2771 assert(index >= 0);
2772 assert(name);
2773
2774 r = bus_call_method(bus, bus_network_mgr, "ForceRenewLink", &error, NULL, "i", index);
2775 if (r < 0)
2776 return log_error_errno(r, "Failed to force renew dynamic configuration of interface %s: %s",
2777 name, bus_error_message(&error, r));
2778
2779 return 0;
2780 }
2781
2782 static int link_force_renew(int argc, char *argv[], void *userdata) {
2783 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2784 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2785 int k = 0, r;
2786
2787 r = acquire_bus(&bus);
2788 if (r < 0)
2789 return r;
2790
2791 for (int i = 1; i < argc; i++) {
2792 int index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2793 if (index < 0)
2794 return index;
2795
2796 r = link_force_renew_one(bus, index, argv[i]);
2797 if (r < 0 && k >= 0)
2798 k = r;
2799 }
2800
2801 return k;
2802 }
2803
2804 static int verb_reload(int argc, char *argv[], void *userdata) {
2805 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2806 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2807 int r;
2808
2809 r = acquire_bus(&bus);
2810 if (r < 0)
2811 return r;
2812
2813 r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
2814 if (r < 0)
2815 return log_error_errno(r, "Failed to reload network settings: %s", bus_error_message(&error, r));
2816
2817 return 0;
2818 }
2819
2820 static int verb_reconfigure(int argc, char *argv[], void *userdata) {
2821 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2822 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2823 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2824 _cleanup_set_free_ Set *indexes = NULL;
2825 int index, r;
2826 void *p;
2827
2828 r = acquire_bus(&bus);
2829 if (r < 0)
2830 return r;
2831
2832 indexes = set_new(NULL);
2833 if (!indexes)
2834 return log_oom();
2835
2836 for (int i = 1; i < argc; i++) {
2837 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2838 if (index < 0)
2839 return index;
2840
2841 r = set_put(indexes, INT_TO_PTR(index));
2842 if (r < 0)
2843 return log_oom();
2844 }
2845
2846 SET_FOREACH(p, indexes) {
2847 index = PTR_TO_INT(p);
2848 r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index);
2849 if (r < 0)
2850 return log_error_errno(r, "Failed to reconfigure network interface %s: %s",
2851 FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX),
2852 bus_error_message(&error, r));
2853 }
2854
2855 return 0;
2856 }
2857
2858 typedef enum ReloadFlags {
2859 RELOAD_NETWORKD = 1 << 0,
2860 RELOAD_UDEVD = 1 << 1,
2861 } ReloadFlags;
2862
2863 static int get_config_files_by_name(const char *name, char **ret_path, char ***ret_dropins) {
2864 _cleanup_free_ char *path = NULL;
2865 int r;
2866
2867 assert(name);
2868 assert(ret_path);
2869
2870 STRV_FOREACH(i, NETWORK_DIRS) {
2871 _cleanup_free_ char *p = NULL;
2872
2873 p = path_join(*i, name);
2874 if (!p)
2875 return -ENOMEM;
2876
2877 r = RET_NERRNO(access(p, F_OK));
2878 if (r >= 0) {
2879 path = TAKE_PTR(p);
2880 break;
2881 }
2882
2883 if (r != -ENOENT)
2884 log_debug_errno(r, "Failed to determine whether '%s' exists, ignoring: %m", p);
2885 }
2886
2887 if (!path)
2888 return -ENOENT;
2889
2890 if (ret_dropins) {
2891 _cleanup_free_ char *dropin_dirname = NULL;
2892
2893 dropin_dirname = strjoin(name, ".d");
2894 if (!dropin_dirname)
2895 return -ENOMEM;
2896
2897 r = conf_files_list_dropins(ret_dropins, dropin_dirname, /* root = */ NULL, NETWORK_DIRS);
2898 if (r < 0)
2899 return r;
2900 }
2901
2902 *ret_path = TAKE_PTR(path);
2903
2904 return 0;
2905 }
2906
2907 static int get_dropin_by_name(
2908 const char *name,
2909 char * const *dropins,
2910 char **ret) {
2911
2912 assert(name);
2913 assert(dropins);
2914 assert(ret);
2915
2916 STRV_FOREACH(i, dropins)
2917 if (path_equal_filename(*i, name)) {
2918 _cleanup_free_ char *d = NULL;
2919
2920 d = strdup(*i);
2921 if (!d)
2922 return -ENOMEM;
2923
2924 *ret = TAKE_PTR(d);
2925 return 1;
2926 }
2927
2928 *ret = NULL;
2929 return 0;
2930 }
2931
2932 static int get_network_files_by_link(
2933 sd_netlink **rtnl,
2934 const char *link,
2935 char **ret_path,
2936 char ***ret_dropins) {
2937
2938 _cleanup_strv_free_ char **dropins = NULL;
2939 _cleanup_free_ char *path = NULL;
2940 int r, ifindex;
2941
2942 assert(rtnl);
2943 assert(link);
2944 assert(ret_path);
2945 assert(ret_dropins);
2946
2947 ifindex = rtnl_resolve_interface_or_warn(rtnl, link);
2948 if (ifindex < 0)
2949 return ifindex;
2950
2951 r = sd_network_link_get_network_file(ifindex, &path);
2952 if (r == -ENODATA)
2953 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
2954 "Link '%s' has no associated network file.", link);
2955 if (r < 0)
2956 return log_error_errno(r, "Failed to get network file for link '%s': %m", link);
2957
2958 r = sd_network_link_get_network_file_dropins(ifindex, &dropins);
2959 if (r < 0 && r != -ENODATA)
2960 return log_error_errno(r, "Failed to get network drop-ins for link '%s': %m", link);
2961
2962 *ret_path = TAKE_PTR(path);
2963 *ret_dropins = TAKE_PTR(dropins);
2964
2965 return 0;
2966 }
2967
2968 static int get_link_files_by_link(const char *link, char **ret_path, char ***ret_dropins) {
2969 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
2970 _cleanup_strv_free_ char **dropins_split = NULL;
2971 _cleanup_free_ char *p = NULL;
2972 const char *path, *dropins;
2973 int r;
2974
2975 assert(link);
2976 assert(ret_path);
2977 assert(ret_dropins);
2978
2979 r = sd_device_new_from_ifname(&device, link);
2980 if (r < 0)
2981 return log_error_errno(r, "Failed to create sd-device object for link '%s': %m", link);
2982
2983 r = sd_device_get_property_value(device, "ID_NET_LINK_FILE", &path);
2984 if (r == -ENOENT)
2985 return log_error_errno(r, "Link '%s' has no associated link file.", link);
2986 if (r < 0)
2987 return log_error_errno(r, "Failed to get link file for link '%s': %m", link);
2988
2989 r = sd_device_get_property_value(device, "ID_NET_LINK_FILE_DROPINS", &dropins);
2990 if (r < 0 && r != -ENOENT)
2991 return log_error_errno(r, "Failed to get link drop-ins for link '%s': %m", link);
2992 if (r >= 0) {
2993 r = strv_split_full(&dropins_split, dropins, ":", EXTRACT_CUNESCAPE);
2994 if (r < 0)
2995 return log_error_errno(r, "Failed to parse link drop-ins for link '%s': %m", link);
2996 }
2997
2998 p = strdup(path);
2999 if (!p)
3000 return log_oom();
3001
3002 *ret_path = TAKE_PTR(p);
3003 *ret_dropins = TAKE_PTR(dropins_split);
3004
3005 return 0;
3006 }
3007
3008 static int get_config_files_by_link_config(
3009 const char *link_config,
3010 sd_netlink **rtnl,
3011 char **ret_path,
3012 char ***ret_dropins,
3013 ReloadFlags *ret_reload) {
3014
3015 _cleanup_strv_free_ char **dropins = NULL, **link_config_split = NULL;
3016 _cleanup_free_ char *path = NULL;
3017 const char *ifname, *type;
3018 ReloadFlags reload;
3019 size_t n;
3020 int r;
3021
3022 assert(link_config);
3023 assert(rtnl);
3024 assert(ret_path);
3025 assert(ret_dropins);
3026
3027 link_config_split = strv_split(link_config, ":");
3028 if (!link_config_split)
3029 return log_oom();
3030
3031 n = strv_length(link_config_split);
3032 if (n == 0 || isempty(link_config_split[0]))
3033 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No link name is given.");
3034 if (n > 2)
3035 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid link config '%s'.", link_config);
3036
3037 ifname = link_config_split[0];
3038 type = n == 2 ? link_config_split[1] : "network";
3039
3040 if (streq(type, "network")) {
3041 if (!networkd_is_running())
3042 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
3043 "Cannot get network file for link if systemd-networkd is not running.");
3044
3045 r = get_network_files_by_link(rtnl, ifname, &path, &dropins);
3046 if (r < 0)
3047 return r;
3048
3049 reload = RELOAD_NETWORKD;
3050 } else if (streq(type, "link")) {
3051 r = get_link_files_by_link(ifname, &path, &dropins);
3052 if (r < 0)
3053 return r;
3054
3055 reload = RELOAD_UDEVD;
3056 } else
3057 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3058 "Invalid config type '%s' for link '%s'.", type, ifname);
3059
3060 *ret_path = TAKE_PTR(path);
3061 *ret_dropins = TAKE_PTR(dropins);
3062
3063 if (ret_reload)
3064 *ret_reload = reload;
3065
3066 return 0;
3067 }
3068
3069 static int add_config_to_edit(
3070 EditFileContext *context,
3071 const char *path,
3072 char * const *dropins) {
3073
3074 _cleanup_free_ char *new_path = NULL, *dropin_path = NULL, *old_dropin = NULL;
3075 _cleanup_strv_free_ char **comment_paths = NULL;
3076 int r;
3077
3078 assert(context);
3079 assert(path);
3080 assert(!arg_drop_in || dropins);
3081
3082 if (path_startswith(path, "/usr")) {
3083 _cleanup_free_ char *name = NULL;
3084
3085 r = path_extract_filename(path, &name);
3086 if (r < 0)
3087 return log_error_errno(r, "Failed to extract filename from '%s': %m", path);
3088
3089 new_path = path_join(NETWORK_DIRS[0], name);
3090 if (!new_path)
3091 return log_oom();
3092 }
3093
3094 if (!arg_drop_in)
3095 return edit_files_add(context, new_path ?: path, path, NULL);
3096
3097 r = get_dropin_by_name(arg_drop_in, dropins, &old_dropin);
3098 if (r < 0)
3099 return log_error_errno(r, "Failed to acquire drop-in '%s': %m", arg_drop_in);
3100
3101 if (r > 0 && !path_startswith(old_dropin, "/usr"))
3102 /* An existing drop-in is found and not in /usr/. Let's edit it directly. */
3103 dropin_path = TAKE_PTR(old_dropin);
3104 else {
3105 /* No drop-in was found or an existing drop-in resides in /usr/. Let's create
3106 * a new drop-in file. */
3107 dropin_path = strjoin(new_path ?: path, ".d/", arg_drop_in);
3108 if (!dropin_path)
3109 return log_oom();
3110 }
3111
3112 comment_paths = strv_new(path);
3113 if (!comment_paths)
3114 return log_oom();
3115
3116 r = strv_extend_strv(&comment_paths, dropins, /* filter_duplicates = */ false);
3117 if (r < 0)
3118 return log_oom();
3119
3120 return edit_files_add(context, dropin_path, old_dropin, comment_paths);
3121 }
3122
3123 static int udevd_reload(sd_bus *bus) {
3124 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3125 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3126 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
3127 const char *job_path;
3128 int r;
3129
3130 assert(bus);
3131
3132 r = bus_wait_for_jobs_new(bus, &w);
3133 if (r < 0)
3134 return log_error_errno(r, "Could not watch jobs: %m");
3135
3136 r = bus_call_method(bus,
3137 bus_systemd_mgr,
3138 "ReloadUnit",
3139 &error,
3140 &reply,
3141 "ss",
3142 "systemd-udevd.service",
3143 "replace");
3144 if (r < 0)
3145 return log_error_errno(r, "Failed to reload systemd-udevd: %s", bus_error_message(&error, r));
3146
3147 r = sd_bus_message_read(reply, "o", &job_path);
3148 if (r < 0)
3149 return bus_log_parse_error(r);
3150
3151 r = bus_wait_for_jobs_one(w, job_path, /* quiet = */ true, NULL);
3152 if (r == -ENOEXEC) {
3153 log_debug("systemd-udevd is not running, skipping reload.");
3154 return 0;
3155 }
3156 if (r < 0)
3157 return log_error_errno(r, "Failed to reload systemd-udevd: %m");
3158
3159 return 1;
3160 }
3161
3162 static int verb_edit(int argc, char *argv[], void *userdata) {
3163 _cleanup_(edit_file_context_done) EditFileContext context = {
3164 .marker_start = DROPIN_MARKER_START,
3165 .marker_end = DROPIN_MARKER_END,
3166 .remove_parent = !!arg_drop_in,
3167 };
3168 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
3169 ReloadFlags reload = 0;
3170 int r;
3171
3172 if (!on_tty())
3173 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit network config files if not on a tty.");
3174
3175 r = mac_selinux_init();
3176 if (r < 0)
3177 return r;
3178
3179 STRV_FOREACH(name, strv_skip(argv, 1)) {
3180 _cleanup_strv_free_ char **dropins = NULL;
3181 _cleanup_free_ char *path = NULL;
3182 const char *link_config;
3183
3184 link_config = startswith(*name, "@");
3185 if (link_config) {
3186 ReloadFlags flags;
3187
3188 r = get_config_files_by_link_config(link_config, &rtnl, &path, &dropins, &flags);
3189 if (r < 0)
3190 return r;
3191
3192 reload |= flags;
3193
3194 r = add_config_to_edit(&context, path, dropins);
3195 if (r < 0)
3196 return r;
3197
3198 continue;
3199 }
3200
3201 if (ENDSWITH_SET(*name, ".network", ".netdev"))
3202 reload |= RELOAD_NETWORKD;
3203 else if (endswith(*name, ".link"))
3204 reload |= RELOAD_UDEVD;
3205 else
3206 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid network config name '%s'.", *name);
3207
3208 r = get_config_files_by_name(*name, &path, &dropins);
3209 if (r == -ENOENT) {
3210 if (arg_drop_in)
3211 return log_error_errno(r, "Cannot find network config '%s'.", *name);
3212
3213 log_debug("No existing network config '%s' found, creating a new file.", *name);
3214
3215 path = path_join(NETWORK_DIRS[0], *name);
3216 if (!path)
3217 return log_oom();
3218
3219 r = edit_files_add(&context, path, NULL, NULL);
3220 if (r < 0)
3221 return r;
3222 continue;
3223 }
3224 if (r < 0)
3225 return log_error_errno(r, "Failed to get the path of network config '%s': %m", *name);
3226
3227 r = add_config_to_edit(&context, path, dropins);
3228 if (r < 0)
3229 return r;
3230 }
3231
3232 r = do_edit_files_and_install(&context);
3233 if (r < 0)
3234 return r;
3235
3236 if (arg_no_reload)
3237 return 0;
3238
3239 if (!sd_booted() || running_in_chroot() > 0) {
3240 log_debug("System is not booted with systemd or is running in chroot, skipping reload.");
3241 return 0;
3242 }
3243
3244 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
3245
3246 r = sd_bus_open_system(&bus);
3247 if (r < 0)
3248 return log_error_errno(r, "Failed to connect to system bus: %m");
3249
3250 if (FLAGS_SET(reload, RELOAD_UDEVD)) {
3251 r = udevd_reload(bus);
3252 if (r < 0)
3253 return r;
3254 }
3255
3256 if (FLAGS_SET(reload, RELOAD_NETWORKD)) {
3257 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3258
3259 if (!networkd_is_running()) {
3260 log_debug("systemd-networkd is not running, skipping reload.");
3261 return 0;
3262 }
3263
3264 r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
3265 if (r < 0)
3266 return log_error_errno(r, "Failed to reload systemd-networkd: %s", bus_error_message(&error, r));
3267 }
3268
3269 return 0;
3270 }
3271
3272 static int verb_cat(int argc, char *argv[], void *userdata) {
3273 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
3274 int r, ret = 0;
3275
3276 pager_open(arg_pager_flags);
3277
3278 STRV_FOREACH(name, strv_skip(argv, 1)) {
3279 _cleanup_strv_free_ char **dropins = NULL;
3280 _cleanup_free_ char *path = NULL;
3281 const char *link_config;
3282
3283 link_config = startswith(*name, "@");
3284 if (link_config) {
3285 r = get_config_files_by_link_config(link_config, &rtnl, &path, &dropins, /* ret_reload = */ NULL);
3286 if (r < 0)
3287 return ret < 0 ? ret : r;
3288 } else {
3289 r = get_config_files_by_name(*name, &path, &dropins);
3290 if (r == -ENOENT) {
3291 log_error_errno(r, "Cannot find network config file '%s'.", *name);
3292 ret = ret < 0 ? ret : r;
3293 continue;
3294 }
3295 if (r < 0) {
3296 log_error_errno(r, "Failed to get the path of network config '%s': %m", *name);
3297 return ret < 0 ? ret : r;
3298 }
3299 }
3300
3301 r = cat_files(path, dropins, /* flags = */ 0);
3302 if (r < 0)
3303 return ret < 0 ? ret : r;
3304 }
3305
3306 return ret;
3307 }
3308
3309 static int help(void) {
3310 _cleanup_free_ char *link = NULL;
3311 int r;
3312
3313 r = terminal_urlify_man("networkctl", "1", &link);
3314 if (r < 0)
3315 return log_oom();
3316
3317 printf("%s [OPTIONS...] COMMAND\n\n"
3318 "%sQuery and control the networking subsystem.%s\n"
3319 "\nCommands:\n"
3320 " list [PATTERN...] List links\n"
3321 " status [PATTERN...] Show link status\n"
3322 " lldp [PATTERN...] Show LLDP neighbors\n"
3323 " label Show current address label entries in the kernel\n"
3324 " delete DEVICES... Delete virtual netdevs\n"
3325 " up DEVICES... Bring devices up\n"
3326 " down DEVICES... Bring devices down\n"
3327 " renew DEVICES... Renew dynamic configurations\n"
3328 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
3329 " reconfigure DEVICES... Reconfigure interfaces\n"
3330 " reload Reload .network and .netdev files\n"
3331 " edit FILES|DEVICES... Edit network configuration files\n"
3332 " cat FILES|DEVICES... Show network configuration files\n"
3333 "\nOptions:\n"
3334 " -h --help Show this help\n"
3335 " --version Show package version\n"
3336 " --no-pager Do not pipe output into a pager\n"
3337 " --no-legend Do not show the headers and footers\n"
3338 " -a --all Show status for all links\n"
3339 " -s --stats Show detailed link statistics\n"
3340 " -l --full Do not ellipsize output\n"
3341 " -n --lines=INTEGER Number of journal entries to show\n"
3342 " --json=pretty|short|off\n"
3343 " Generate JSON output\n"
3344 " --no-reload Do not reload systemd-networkd or systemd-udevd\n"
3345 " after editing network config\n"
3346 " --drop-in=NAME Edit specified drop-in instead of main config file\n"
3347 "\nSee the %s for details.\n",
3348 program_invocation_short_name,
3349 ansi_highlight(),
3350 ansi_normal(),
3351 link);
3352
3353 return 0;
3354 }
3355
3356 static int parse_argv(int argc, char *argv[]) {
3357 enum {
3358 ARG_VERSION = 0x100,
3359 ARG_NO_PAGER,
3360 ARG_NO_LEGEND,
3361 ARG_JSON,
3362 ARG_NO_RELOAD,
3363 ARG_DROP_IN,
3364 };
3365
3366 static const struct option options[] = {
3367 { "help", no_argument, NULL, 'h' },
3368 { "version", no_argument, NULL, ARG_VERSION },
3369 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
3370 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
3371 { "all", no_argument, NULL, 'a' },
3372 { "stats", no_argument, NULL, 's' },
3373 { "full", no_argument, NULL, 'l' },
3374 { "lines", required_argument, NULL, 'n' },
3375 { "json", required_argument, NULL, ARG_JSON },
3376 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3377 { "drop-in", required_argument, NULL, ARG_DROP_IN },
3378 {}
3379 };
3380
3381 int c, r;
3382
3383 assert(argc >= 0);
3384 assert(argv);
3385
3386 while ((c = getopt_long(argc, argv, "hasln:", options, NULL)) >= 0) {
3387
3388 switch (c) {
3389
3390 case 'h':
3391 return help();
3392
3393 case ARG_VERSION:
3394 return version();
3395
3396 case ARG_NO_PAGER:
3397 arg_pager_flags |= PAGER_DISABLE;
3398 break;
3399
3400 case ARG_NO_LEGEND:
3401 arg_legend = false;
3402 break;
3403
3404 case ARG_NO_RELOAD:
3405 arg_no_reload = true;
3406 break;
3407
3408 case ARG_DROP_IN:
3409 if (isempty(optarg))
3410 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty drop-in file name.");
3411
3412 if (!endswith(optarg, ".conf")) {
3413 char *conf;
3414
3415 conf = strjoin(optarg, ".conf");
3416 if (!conf)
3417 return log_oom();
3418
3419 free_and_replace(arg_drop_in, conf);
3420 } else {
3421 r = free_and_strdup(&arg_drop_in, optarg);
3422 if (r < 0)
3423 return log_oom();
3424 }
3425
3426 if (!filename_is_valid(arg_drop_in))
3427 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3428 "Invalid drop-in file name '%s'.", arg_drop_in);
3429
3430 break;
3431
3432 case 'a':
3433 arg_all = true;
3434 break;
3435
3436 case 's':
3437 arg_stats = true;
3438 break;
3439
3440 case 'l':
3441 arg_full = true;
3442 break;
3443
3444 case 'n':
3445 if (safe_atou(optarg, &arg_lines) < 0)
3446 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3447 "Failed to parse lines '%s'", optarg);
3448 break;
3449
3450 case ARG_JSON:
3451 r = parse_json_argument(optarg, &arg_json_format_flags);
3452 if (r <= 0)
3453 return r;
3454 break;
3455
3456 case '?':
3457 return -EINVAL;
3458
3459 default:
3460 assert_not_reached();
3461 }
3462 }
3463
3464 return 1;
3465 }
3466
3467 static int networkctl_main(int argc, char *argv[]) {
3468 static const Verb verbs[] = {
3469 { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_links },
3470 { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, link_status },
3471 { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status },
3472 { "label", 1, 1, 0, list_address_labels },
3473 { "delete", 2, VERB_ANY, 0, link_delete },
3474 { "up", 2, VERB_ANY, 0, link_up_down },
3475 { "down", 2, VERB_ANY, 0, link_up_down },
3476 { "renew", 2, VERB_ANY, VERB_ONLINE_ONLY, link_renew },
3477 { "forcerenew", 2, VERB_ANY, VERB_ONLINE_ONLY, link_force_renew },
3478 { "reconfigure", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_reconfigure },
3479 { "reload", 1, 1, VERB_ONLINE_ONLY, verb_reload },
3480 { "edit", 2, VERB_ANY, 0, verb_edit },
3481 { "cat", 2, VERB_ANY, 0, verb_cat },
3482 {}
3483 };
3484
3485 return dispatch_verb(argc, argv, verbs, NULL);
3486 }
3487
3488 static int run(int argc, char* argv[]) {
3489 int r;
3490
3491 log_setup();
3492
3493 sigbus_install();
3494
3495 r = parse_argv(argc, argv);
3496 if (r <= 0)
3497 return r;
3498
3499 return networkctl_main(argc, argv);
3500 }
3501
3502 DEFINE_MAIN_FUNCTION(run);