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