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