]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkctl.c
Merge pull request #28445 from bluca/run_host_release
[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 const char *type) {
713
714 _cleanup_free_ char *path = NULL;
715 char ifindex_str[DECIMAL_STR_MAX(int)];
716 int r;
717
718 assert(bus);
719 assert(link);
720 assert(link->ifindex >= 0);
721 assert(error);
722 assert(reply);
723 assert(iface);
724 assert(propname);
725 assert(type);
726
727 xsprintf(ifindex_str, "%i", link->ifindex);
728
729 r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path);
730 if (r < 0)
731 return r;
732
733 return sd_bus_get_property(bus, "org.freedesktop.network1", path, iface, propname, error, reply, type);
734 }
735
736 static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) {
737 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
738 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
739 int r;
740
741 assert(bus);
742 assert(link);
743
744 r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.Link", "BitRates", "(tt)");
745 if (r < 0) {
746 bool quiet = sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY,
747 BUS_ERROR_SPEED_METER_INACTIVE);
748
749 return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING,
750 r, "Failed to query link bit rates: %s", bus_error_message(&error, r));
751 }
752
753 r = sd_bus_message_read(reply, "(tt)", &link->tx_bitrate, &link->rx_bitrate);
754 if (r < 0)
755 return bus_log_parse_error(r);
756
757 r = sd_bus_message_exit_container(reply);
758 if (r < 0)
759 return bus_log_parse_error(r);
760
761 link->has_bitrates = link->tx_bitrate != UINT64_MAX && link->rx_bitrate != UINT64_MAX;
762
763 return 0;
764 }
765
766 static void acquire_ether_link_info(int *fd, LinkInfo *link) {
767 assert(fd);
768 assert(link);
769
770 if (ethtool_get_link_info(fd,
771 link->name,
772 &link->autonegotiation,
773 &link->speed,
774 &link->duplex,
775 &link->port) >= 0)
776 link->has_ethtool_link_info = true;
777 }
778
779 static void acquire_wlan_link_info(LinkInfo *link) {
780 _cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL;
781 const char *type = NULL;
782 int r, k = 0;
783
784 assert(link);
785
786 if (link->sd_device)
787 (void) sd_device_get_devtype(link->sd_device, &type);
788 if (!streq_ptr(type, "wlan"))
789 return;
790
791 r = sd_genl_socket_open(&genl);
792 if (r < 0) {
793 log_debug_errno(r, "Failed to open generic netlink socket: %m");
794 return;
795 }
796
797 (void) sd_netlink_increase_rxbuf(genl, RCVBUF_SIZE);
798
799 r = wifi_get_interface(genl, link->ifindex, &link->wlan_iftype, &link->ssid);
800 if (r < 0)
801 log_debug_errno(r, "%s: failed to query ssid: %m", link->name);
802
803 if (link->wlan_iftype == NL80211_IFTYPE_STATION) {
804 k = wifi_get_station(genl, link->ifindex, &link->bssid);
805 if (k < 0)
806 log_debug_errno(k, "%s: failed to query bssid: %m", link->name);
807 }
808
809 link->has_wlan_link_info = r > 0 || k > 0;
810 }
811
812 static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char * const *patterns, LinkInfo **ret) {
813 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
814 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
815 _cleanup_free_ bool *matched_patterns = NULL;
816 _cleanup_close_ int fd = -EBADF;
817 size_t c = 0;
818 int r;
819
820 assert(rtnl);
821 assert(ret);
822
823 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
824 if (r < 0)
825 return rtnl_log_create_error(r);
826
827 r = sd_netlink_message_set_request_dump(req, true);
828 if (r < 0)
829 return rtnl_log_create_error(r);
830
831 r = sd_netlink_call(rtnl, req, 0, &reply);
832 if (r < 0)
833 return log_error_errno(r, "Failed to enumerate links: %m");
834
835 if (patterns) {
836 matched_patterns = new0(bool, strv_length(patterns));
837 if (!matched_patterns)
838 return log_oom();
839 }
840
841 for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) {
842 if (!GREEDY_REALLOC0(links, c + 2)) /* We keep one trailing one as marker */
843 return -ENOMEM;
844
845 r = decode_link(i, links + c, patterns, matched_patterns);
846 if (r < 0)
847 return r;
848 if (r == 0)
849 continue;
850
851 links[c].needs_freeing = true;
852
853 (void) sd_device_new_from_ifindex(&links[c].sd_device, links[c].ifindex);
854
855 acquire_ether_link_info(&fd, &links[c]);
856 acquire_wlan_link_info(&links[c]);
857
858 c++;
859 }
860
861 /* Look if we matched all our arguments that are not globs. It
862 * is OK for a glob to match nothing, but not for an exact argument. */
863 for (size_t pos = 0; pos < strv_length(patterns); pos++) {
864 if (matched_patterns[pos])
865 continue;
866
867 if (string_is_glob(patterns[pos]))
868 log_debug("Pattern \"%s\" doesn't match any interface, ignoring.",
869 patterns[pos]);
870 else
871 return log_error_errno(SYNTHETIC_ERRNO(ENODEV),
872 "Interface \"%s\" not found.", patterns[pos]);
873 }
874
875 typesafe_qsort(links, c, link_info_compare);
876
877 if (bus)
878 FOREACH_ARRAY(link, links, c)
879 (void) acquire_link_bitrates(bus, link);
880
881 *ret = TAKE_PTR(links);
882
883 if (patterns && c == 0)
884 log_warning("No interfaces matched.");
885
886 return (int) c;
887 }
888
889 static int list_links(int argc, char *argv[], void *userdata) {
890 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
891 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
892 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
893 _cleanup_(table_unrefp) Table *table = NULL;
894 TableCell *cell;
895 int c, r;
896
897 r = acquire_bus(&bus);
898 if (r < 0)
899 return r;
900
901 if (arg_json_format_flags != JSON_FORMAT_OFF) {
902 if (arg_all || argc <= 1)
903 return dump_manager_description(bus);
904 else
905 return dump_link_description(bus, strv_skip(argv, 1));
906 }
907
908 r = sd_netlink_open(&rtnl);
909 if (r < 0)
910 return log_error_errno(r, "Failed to connect to netlink: %m");
911
912 c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
913 if (c < 0)
914 return c;
915
916 pager_open(arg_pager_flags);
917
918 table = table_new("idx", "link", "type", "operational", "setup");
919 if (!table)
920 return log_oom();
921
922 if (arg_full)
923 table_set_width(table, 0);
924
925 table_set_header(table, arg_legend);
926 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
927
928 assert_se(cell = table_get_cell(table, 0, 0));
929 (void) table_set_minimum_width(table, cell, 3);
930 (void) table_set_weight(table, cell, 0);
931 (void) table_set_ellipsize_percent(table, cell, 100);
932 (void) table_set_align_percent(table, cell, 100);
933
934 assert_se(cell = table_get_cell(table, 0, 1));
935 (void) table_set_ellipsize_percent(table, cell, 100);
936
937 FOREACH_ARRAY(link, links, c) {
938 _cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
939 _cleanup_free_ char *t = NULL;
940 const char *on_color_operational, *on_color_setup;
941
942 (void) sd_network_link_get_operational_state(link->ifindex, &operational_state);
943 operational_state_to_color(link->name, operational_state, &on_color_operational, NULL);
944
945 (void) sd_network_link_get_setup_state(link->ifindex, &setup_state);
946 setup_state_to_color(setup_state, &on_color_setup, NULL);
947
948 r = net_get_type_string(link->sd_device, link->iftype, &t);
949 if (r == -ENOMEM)
950 return log_oom();
951
952 r = table_add_many(table,
953 TABLE_INT, link->ifindex,
954 TABLE_STRING, link->name,
955 TABLE_STRING, t,
956 TABLE_STRING, operational_state,
957 TABLE_SET_COLOR, on_color_operational,
958 TABLE_STRING, setup_state ?: "unmanaged",
959 TABLE_SET_COLOR, on_color_setup);
960 if (r < 0)
961 return table_log_add_error(r);
962 }
963
964 r = table_print(table, NULL);
965 if (r < 0)
966 return table_log_print_error(r);
967
968 if (arg_legend)
969 printf("\n%i links listed.\n", c);
970
971 return 0;
972 }
973
974 /* IEEE Organizationally Unique Identifier vendor string */
975 static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) {
976 _cleanup_free_ char *desc = NULL;
977 const char *description;
978 char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1];
979 int r;
980
981 assert(ret);
982
983 if (!hwdb || !mac)
984 return -EINVAL;
985
986 /* skip commonly misused 00:00:00 (Xerox) prefix */
987 if (memcmp(mac, "\0\0\0", 3) == 0)
988 return -EINVAL;
989
990 xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR, ETHER_ADDR_FORMAT_VAL(*mac));
991
992 r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description);
993 if (r < 0)
994 return r;
995
996 desc = strdup(description);
997 if (!desc)
998 return -ENOMEM;
999
1000 *ret = TAKE_PTR(desc);
1001
1002 return 0;
1003 }
1004
1005 static int get_gateway_description(
1006 sd_netlink *rtnl,
1007 sd_hwdb *hwdb,
1008 int ifindex,
1009 int family,
1010 union in_addr_union *gateway,
1011 char **ret) {
1012
1013 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1014 int r;
1015
1016 assert(rtnl);
1017 assert(ifindex >= 0);
1018 assert(IN_SET(family, AF_INET, AF_INET6));
1019 assert(gateway);
1020 assert(ret);
1021
1022 r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family);
1023 if (r < 0)
1024 return r;
1025
1026 r = sd_netlink_message_set_request_dump(req, true);
1027 if (r < 0)
1028 return r;
1029
1030 r = sd_netlink_call(rtnl, req, 0, &reply);
1031 if (r < 0)
1032 return r;
1033
1034 for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
1035 union in_addr_union gw = IN_ADDR_NULL;
1036 struct ether_addr mac = ETHER_ADDR_NULL;
1037 uint16_t type;
1038 int ifi, fam;
1039
1040 r = sd_netlink_message_get_errno(m);
1041 if (r < 0) {
1042 log_error_errno(r, "Failed to get netlink message, ignoring: %m");
1043 continue;
1044 }
1045
1046 r = sd_netlink_message_get_type(m, &type);
1047 if (r < 0) {
1048 log_error_errno(r, "Failed to get netlink message type, ignoring: %m");
1049 continue;
1050 }
1051
1052 if (type != RTM_NEWNEIGH) {
1053 log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1054 "Got unexpected netlink message type %u, ignoring",
1055 type);
1056 continue;
1057 }
1058
1059 r = sd_rtnl_message_neigh_get_family(m, &fam);
1060 if (r < 0) {
1061 log_error_errno(r, "Failed to get rtnl family, ignoring: %m");
1062 continue;
1063 }
1064
1065 if (fam != family) {
1066 log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Got invalid rtnl family %d, ignoring", fam);
1067 continue;
1068 }
1069
1070 r = sd_rtnl_message_neigh_get_ifindex(m, &ifi);
1071 if (r < 0) {
1072 log_error_errno(r, "Failed to get rtnl ifindex, ignoring: %m");
1073 continue;
1074 }
1075
1076 if (ifindex > 0 && ifi != ifindex)
1077 continue;
1078
1079 switch (fam) {
1080
1081 case AF_INET:
1082 r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in);
1083 if (r < 0)
1084 continue;
1085
1086 break;
1087
1088 case AF_INET6:
1089 r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6);
1090 if (r < 0)
1091 continue;
1092
1093 break;
1094
1095 default:
1096 continue;
1097 }
1098
1099 if (!in_addr_equal(fam, &gw, gateway))
1100 continue;
1101
1102 r = sd_netlink_message_read(m, NDA_LLADDR, sizeof(mac), &mac);
1103 if (r < 0)
1104 continue;
1105
1106 r = ieee_oui(hwdb, &mac, ret);
1107 if (r < 0)
1108 continue;
1109
1110 return 0;
1111 }
1112
1113 return -ENODATA;
1114 }
1115
1116 static int dump_list(Table *table, const char *prefix, char * const *l) {
1117 int r;
1118
1119 assert(table);
1120
1121 if (strv_isempty(l))
1122 return 0;
1123
1124 r = table_add_many(table,
1125 TABLE_EMPTY,
1126 TABLE_STRING, prefix,
1127 TABLE_STRV, l);
1128 if (r < 0)
1129 return table_log_add_error(r);
1130
1131 return 0;
1132 }
1133
1134 static int dump_gateways(sd_netlink *rtnl, sd_hwdb *hwdb, Table *table, int ifindex) {
1135 _cleanup_free_ struct local_address *local_addrs = NULL;
1136 _cleanup_strv_free_ char **buf = NULL;
1137 int r, n;
1138
1139 assert(rtnl);
1140 assert(table);
1141
1142 n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local_addrs);
1143 if (n <= 0)
1144 return n;
1145
1146 FOREACH_ARRAY(local, local_addrs, n) {
1147 _cleanup_free_ char *description = NULL;
1148
1149 r = get_gateway_description(rtnl, hwdb, local->ifindex, local->family, &local->address, &description);
1150 if (r < 0)
1151 log_debug_errno(r, "Could not get description of gateway, ignoring: %m");
1152
1153 /* Show interface name for the entry if we show entries for all interfaces */
1154 r = strv_extendf(&buf, "%s%s%s%s%s%s",
1155 IN_ADDR_TO_STRING(local->family, &local->address),
1156 description ? " (" : "",
1157 strempty(description),
1158 description ? ")" : "",
1159 ifindex <= 0 ? " on " : "",
1160 ifindex <= 0 ? FORMAT_IFNAME_FULL(local->ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
1161 if (r < 0)
1162 return log_oom();
1163 }
1164
1165 return dump_list(table, "Gateway:", buf);
1166 }
1167
1168 static int dump_addresses(
1169 sd_netlink *rtnl,
1170 sd_dhcp_lease *lease,
1171 Table *table,
1172 int ifindex) {
1173
1174 _cleanup_free_ struct local_address *local_addrs = NULL;
1175 _cleanup_strv_free_ char **buf = NULL;
1176 struct in_addr dhcp4_address = {};
1177 int r, n;
1178
1179 assert(rtnl);
1180 assert(table);
1181
1182 n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local_addrs);
1183 if (n <= 0)
1184 return n;
1185
1186 if (lease)
1187 (void) sd_dhcp_lease_get_address(lease, &dhcp4_address);
1188
1189 FOREACH_ARRAY(local, local_addrs, n) {
1190 struct in_addr server_address;
1191 bool dhcp4 = false;
1192
1193 if (local->family == AF_INET && in4_addr_equal(&local->address.in, &dhcp4_address))
1194 dhcp4 = sd_dhcp_lease_get_server_identifier(lease, &server_address) >= 0;
1195
1196 r = strv_extendf(&buf, "%s%s%s%s%s%s",
1197 IN_ADDR_TO_STRING(local->family, &local->address),
1198 dhcp4 ? " (DHCP4 via " : "",
1199 dhcp4 ? IN4_ADDR_TO_STRING(&server_address) : "",
1200 dhcp4 ? ")" : "",
1201 ifindex <= 0 ? " on " : "",
1202 ifindex <= 0 ? FORMAT_IFNAME_FULL(local->ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
1203 if (r < 0)
1204 return log_oom();
1205 }
1206
1207 return dump_list(table, "Address:", buf);
1208 }
1209
1210 static int dump_address_labels(sd_netlink *rtnl) {
1211 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1212 _cleanup_(table_unrefp) Table *table = NULL;
1213 TableCell *cell;
1214 int r;
1215
1216 assert(rtnl);
1217
1218 r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6);
1219 if (r < 0)
1220 return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m");
1221
1222 r = sd_netlink_message_set_request_dump(req, true);
1223 if (r < 0)
1224 return r;
1225
1226 r = sd_netlink_call(rtnl, req, 0, &reply);
1227 if (r < 0)
1228 return r;
1229
1230 table = table_new("label", "prefix/prefixlen");
1231 if (!table)
1232 return log_oom();
1233
1234 if (arg_full)
1235 table_set_width(table, 0);
1236
1237 r = table_set_sort(table, (size_t) 0);
1238 if (r < 0)
1239 return r;
1240
1241 assert_se(cell = table_get_cell(table, 0, 0));
1242 (void) table_set_align_percent(table, cell, 100);
1243 (void) table_set_ellipsize_percent(table, cell, 100);
1244
1245 assert_se(cell = table_get_cell(table, 0, 1));
1246 (void) table_set_align_percent(table, cell, 100);
1247
1248 for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
1249 struct in6_addr prefix;
1250 uint8_t prefixlen;
1251 uint32_t label;
1252
1253 r = sd_netlink_message_get_errno(m);
1254 if (r < 0) {
1255 log_error_errno(r, "Failed to get netlink message, ignoring: %m");
1256 continue;
1257 }
1258
1259 r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label);
1260 if (r < 0 && r != -ENODATA) {
1261 log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m");
1262 continue;
1263 }
1264
1265 r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix);
1266 if (r < 0)
1267 continue;
1268
1269 r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen);
1270 if (r < 0)
1271 continue;
1272
1273 r = table_add_cell(table, NULL, TABLE_UINT32, &label);
1274 if (r < 0)
1275 return table_log_add_error(r);
1276
1277 r = table_add_cell_stringf(table, NULL, "%s/%u", IN6_ADDR_TO_STRING(&prefix), prefixlen);
1278 if (r < 0)
1279 return table_log_add_error(r);
1280 }
1281
1282 r = table_print(table, NULL);
1283 if (r < 0)
1284 return table_log_print_error(r);
1285
1286 return 0;
1287 }
1288
1289 static int list_address_labels(int argc, char *argv[], void *userdata) {
1290 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1291 int r;
1292
1293 r = sd_netlink_open(&rtnl);
1294 if (r < 0)
1295 return log_error_errno(r, "Failed to connect to netlink: %m");
1296
1297 return dump_address_labels(rtnl);
1298 }
1299
1300 static int open_lldp_neighbors(int ifindex, FILE **ret) {
1301 _cleanup_fclose_ FILE *f = NULL;
1302 char p[STRLEN("/run/systemd/netif/lldp/") + DECIMAL_STR_MAX(int)];
1303
1304 assert(ifindex >= 0);
1305 assert(ret);
1306
1307 xsprintf(p, "/run/systemd/netif/lldp/%i", ifindex);
1308
1309 f = fopen(p, "re");
1310 if (!f)
1311 return -errno;
1312
1313 *ret = TAKE_PTR(f);
1314 return 0;
1315 }
1316
1317 static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) {
1318 _cleanup_free_ void *raw = NULL;
1319 size_t l;
1320 le64_t u;
1321 int r;
1322
1323 assert(f);
1324 assert(ret);
1325
1326 l = fread(&u, 1, sizeof(u), f);
1327 if (l == 0 && feof(f))
1328 return 0;
1329 if (l != sizeof(u))
1330 return -EBADMSG;
1331
1332 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
1333 if (le64toh(u) >= 4096)
1334 return -EBADMSG;
1335
1336 raw = new(uint8_t, le64toh(u));
1337 if (!raw)
1338 return -ENOMEM;
1339
1340 if (fread(raw, 1, le64toh(u), f) != le64toh(u))
1341 return -EBADMSG;
1342
1343 r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u));
1344 if (r < 0)
1345 return r;
1346
1347 return 1;
1348 }
1349
1350 static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) {
1351 _cleanup_strv_free_ char **buf = NULL;
1352 _cleanup_fclose_ FILE *f = NULL;
1353 int r;
1354
1355 assert(table);
1356 assert(prefix);
1357 assert(ifindex > 0);
1358
1359 r = open_lldp_neighbors(ifindex, &f);
1360 if (r == -ENOENT)
1361 return 0;
1362 if (r < 0)
1363 return r;
1364
1365 for (;;) {
1366 const char *system_name = NULL, *port_id = NULL, *port_description = NULL;
1367 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
1368
1369 r = next_lldp_neighbor(f, &n);
1370 if (r < 0)
1371 return r;
1372 if (r == 0)
1373 break;
1374
1375 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
1376 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
1377 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
1378
1379 r = strv_extendf(&buf, "%s on port %s%s%s%s",
1380 strna(system_name),
1381 strna(port_id),
1382 isempty(port_description) ? "" : " (",
1383 strempty(port_description),
1384 isempty(port_description) ? "" : ")");
1385 if (r < 0)
1386 return log_oom();
1387 }
1388
1389 return dump_list(table, prefix, buf);
1390 }
1391
1392 static int dump_dhcp_leases(Table *table, const char *prefix, sd_bus *bus, const LinkInfo *link) {
1393 _cleanup_strv_free_ char **buf = NULL;
1394 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1395 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1396 int r;
1397
1398 assert(table);
1399 assert(prefix);
1400 assert(bus);
1401 assert(link);
1402
1403 r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.DHCPServer", "Leases", "a(uayayayayt)");
1404 if (r < 0) {
1405 bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY);
1406
1407 log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING,
1408 r, "Failed to query link DHCP leases: %s", bus_error_message(&error, r));
1409 return 0;
1410 }
1411
1412 r = sd_bus_message_enter_container(reply, 'a', "(uayayayayt)");
1413 if (r < 0)
1414 return bus_log_parse_error(r);
1415
1416 while ((r = sd_bus_message_enter_container(reply, 'r', "uayayayayt")) > 0) {
1417 _cleanup_free_ char *id = NULL, *ip = NULL;
1418 const void *client_id, *addr, *gtw, *hwaddr;
1419 size_t client_id_sz, sz;
1420 uint64_t expiration;
1421 uint32_t family;
1422
1423 r = sd_bus_message_read(reply, "u", &family);
1424 if (r < 0)
1425 return bus_log_parse_error(r);
1426
1427 r = sd_bus_message_read_array(reply, 'y', &client_id, &client_id_sz);
1428 if (r < 0)
1429 return bus_log_parse_error(r);
1430
1431 r = sd_bus_message_read_array(reply, 'y', &addr, &sz);
1432 if (r < 0 || sz != 4)
1433 return bus_log_parse_error(r);
1434
1435 r = sd_bus_message_read_array(reply, 'y', &gtw, &sz);
1436 if (r < 0 || sz != 4)
1437 return bus_log_parse_error(r);
1438
1439 r = sd_bus_message_read_array(reply, 'y', &hwaddr, &sz);
1440 if (r < 0)
1441 return bus_log_parse_error(r);
1442
1443 r = sd_bus_message_read_basic(reply, 't', &expiration);
1444 if (r < 0)
1445 return bus_log_parse_error(r);
1446
1447 r = sd_dhcp_client_id_to_string(client_id, client_id_sz, &id);
1448 if (r < 0)
1449 return bus_log_parse_error(r);
1450
1451 r = in_addr_to_string(family, addr, &ip);
1452 if (r < 0)
1453 return bus_log_parse_error(r);
1454
1455 r = strv_extendf(&buf, "%s (to %s)", ip, id);
1456 if (r < 0)
1457 return log_oom();
1458
1459 r = sd_bus_message_exit_container(reply);
1460 if (r < 0)
1461 return bus_log_parse_error(r);
1462 }
1463 if (r < 0)
1464 return bus_log_parse_error(r);
1465
1466 r = sd_bus_message_exit_container(reply);
1467 if (r < 0)
1468 return bus_log_parse_error(r);
1469
1470 r = sd_bus_message_exit_container(reply);
1471 if (r < 0)
1472 return bus_log_parse_error(r);
1473
1474 if (strv_isempty(buf)) {
1475 r = strv_extendf(&buf, "none");
1476 if (r < 0)
1477 return log_oom();
1478 }
1479
1480 return dump_list(table, prefix, buf);
1481 }
1482
1483 static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) {
1484 int r;
1485
1486 assert(table);
1487 assert(prefix);
1488
1489 if (!ifindexes || ifindexes[0] <= 0)
1490 return 0;
1491
1492 for (unsigned c = 0; ifindexes[c] > 0; c++) {
1493 r = table_add_many(table,
1494 TABLE_EMPTY,
1495 TABLE_STRING, c == 0 ? prefix : "",
1496 TABLE_IFINDEX, ifindexes[c]);
1497 if (r < 0)
1498 return table_log_add_error(r);
1499 }
1500
1501 return 0;
1502 }
1503
1504 #define DUMP_STATS_ONE(name, val_name) \
1505 r = table_add_many(table, \
1506 TABLE_EMPTY, \
1507 TABLE_STRING, name ":"); \
1508 if (r < 0) \
1509 return table_log_add_error(r); \
1510 r = table_add_cell(table, NULL, \
1511 info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
1512 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
1513 if (r < 0) \
1514 return table_log_add_error(r);
1515
1516 static int dump_statistics(Table *table, const LinkInfo *info) {
1517 int r;
1518
1519 assert(table);
1520 assert(info);
1521
1522 if (!arg_stats)
1523 return 0;
1524
1525 if (!info->has_stats64 && !info->has_stats)
1526 return 0;
1527
1528 DUMP_STATS_ONE("Rx Packets", rx_packets);
1529 DUMP_STATS_ONE("Tx Packets", tx_packets);
1530 DUMP_STATS_ONE("Rx Bytes", rx_bytes);
1531 DUMP_STATS_ONE("Tx Bytes", tx_bytes);
1532 DUMP_STATS_ONE("Rx Errors", rx_errors);
1533 DUMP_STATS_ONE("Tx Errors", tx_errors);
1534 DUMP_STATS_ONE("Rx Dropped", rx_dropped);
1535 DUMP_STATS_ONE("Tx Dropped", tx_dropped);
1536 DUMP_STATS_ONE("Multicast Packets", multicast);
1537 DUMP_STATS_ONE("Collisions", collisions);
1538
1539 return 0;
1540 }
1541
1542 static int dump_hw_address(Table *table, sd_hwdb *hwdb, const char *field, const struct hw_addr_data *addr) {
1543 _cleanup_free_ char *description = NULL;
1544 int r;
1545
1546 assert(table);
1547 assert(field);
1548 assert(addr);
1549
1550 if (addr->length == ETH_ALEN)
1551 (void) ieee_oui(hwdb, &addr->ether, &description);
1552
1553 r = table_add_many(table,
1554 TABLE_EMPTY,
1555 TABLE_STRING, field);
1556 if (r < 0)
1557 return table_log_add_error(r);
1558
1559 r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
1560 HW_ADDR_TO_STR(addr),
1561 description ? " (" : "",
1562 strempty(description),
1563 description ? ")" : "");
1564 if (r < 0)
1565 return table_log_add_error(r);
1566
1567 return 0;
1568 }
1569
1570 static OutputFlags get_output_flags(void) {
1571 return
1572 arg_all * OUTPUT_SHOW_ALL |
1573 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
1574 colors_enabled() * OUTPUT_COLOR;
1575 }
1576
1577 static int show_logs(const LinkInfo *info) {
1578 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1579 int r;
1580
1581 if (arg_lines == 0)
1582 return 0;
1583
1584 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
1585 if (r < 0)
1586 return log_error_errno(r, "Failed to open journal: %m");
1587
1588 r = add_match_this_boot(j, NULL);
1589 if (r < 0)
1590 return log_error_errno(r, "Failed to add boot matches: %m");
1591
1592 if (info) {
1593 char m1[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)];
1594 const char *m2, *m3;
1595
1596 /* kernel */
1597 xsprintf(m1, "_KERNEL_DEVICE=n%i", info->ifindex);
1598 /* networkd */
1599 m2 = strjoina("INTERFACE=", info->name);
1600 /* udevd */
1601 m3 = strjoina("DEVICE=", info->name);
1602
1603 (void)(
1604 (r = sd_journal_add_match(j, m1, 0)) ||
1605 (r = sd_journal_add_disjunction(j)) ||
1606 (r = sd_journal_add_match(j, m2, 0)) ||
1607 (r = sd_journal_add_disjunction(j)) ||
1608 (r = sd_journal_add_match(j, m3, 0))
1609 );
1610 if (r < 0)
1611 return log_error_errno(r, "Failed to add link matches: %m");
1612 } else {
1613 r = add_matches_for_unit(j, "systemd-networkd.service");
1614 if (r < 0)
1615 return log_error_errno(r, "Failed to add unit matches: %m");
1616
1617 r = add_matches_for_unit(j, "systemd-networkd-wait-online.service");
1618 if (r < 0)
1619 return log_error_errno(r, "Failed to add unit matches: %m");
1620 }
1621
1622 return show_journal(
1623 stdout,
1624 j,
1625 OUTPUT_SHORT,
1626 0,
1627 0,
1628 arg_lines,
1629 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
1630 NULL);
1631 }
1632
1633 static int table_add_string_line(Table *table, const char *key, const char *value) {
1634 int r;
1635
1636 assert(table);
1637 assert(key);
1638
1639 if (isempty(value))
1640 return 0;
1641
1642 r = table_add_many(table,
1643 TABLE_EMPTY,
1644 TABLE_STRING, key,
1645 TABLE_STRING, value);
1646 if (r < 0)
1647 return table_log_add_error(r);
1648
1649 return 0;
1650 }
1651
1652 static int format_dropins(char **dropins) {
1653 STRV_FOREACH(d, dropins) {
1654 _cleanup_free_ char *s = NULL;
1655 int glyph = *(d + 1) == NULL ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH;
1656
1657 s = strjoin(special_glyph(glyph), *d);
1658 if (!s)
1659 return log_oom();
1660
1661 free_and_replace(*d, s);
1662 }
1663
1664 return 0;
1665 }
1666
1667 static int link_status_one(
1668 sd_bus *bus,
1669 sd_netlink *rtnl,
1670 sd_hwdb *hwdb,
1671 const LinkInfo *info) {
1672
1673 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **sip = NULL, **search_domains = NULL,
1674 **route_domains = NULL, **link_dropins = NULL, **network_dropins = NULL;
1675 _cleanup_free_ char *t = NULL, *network = NULL, *iaid = NULL, *duid = NULL, *captive_portal = NULL,
1676 *setup_state = NULL, *operational_state = NULL, *online_state = NULL, *activation_policy = NULL;
1677 const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL,
1678 *on_color_operational, *off_color_operational, *on_color_setup, *off_color_setup, *on_color_online;
1679 _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
1680 _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL;
1681 _cleanup_(table_unrefp) Table *table = NULL;
1682 TableCell *cell;
1683 int r;
1684
1685 assert(bus);
1686 assert(rtnl);
1687 assert(info);
1688
1689 (void) sd_network_link_get_operational_state(info->ifindex, &operational_state);
1690 operational_state_to_color(info->name, operational_state, &on_color_operational, &off_color_operational);
1691
1692 (void) sd_network_link_get_online_state(info->ifindex, &online_state);
1693 online_state_to_color(online_state, &on_color_online, NULL);
1694
1695 (void) sd_network_link_get_setup_state(info->ifindex, &setup_state);
1696 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
1697
1698 (void) sd_network_link_get_dns(info->ifindex, &dns);
1699 (void) sd_network_link_get_search_domains(info->ifindex, &search_domains);
1700 (void) sd_network_link_get_route_domains(info->ifindex, &route_domains);
1701 (void) sd_network_link_get_ntp(info->ifindex, &ntp);
1702 (void) sd_network_link_get_sip(info->ifindex, &sip);
1703 (void) sd_network_link_get_captive_portal(info->ifindex, &captive_portal);
1704 (void) sd_network_link_get_network_file(info->ifindex, &network);
1705 (void) sd_network_link_get_network_file_dropins(info->ifindex, &network_dropins);
1706 (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
1707 (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
1708 (void) sd_network_link_get_activation_policy(info->ifindex, &activation_policy);
1709
1710 if (info->sd_device) {
1711 const char *joined;
1712
1713 (void) sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE", &link);
1714
1715 if (sd_device_get_property_value(info->sd_device, "ID_NET_LINK_FILE_DROPINS", &joined) >= 0) {
1716 r = strv_split_full(&link_dropins, joined, ":", EXTRACT_CUNESCAPE);
1717 if (r < 0)
1718 return r;
1719 }
1720
1721 (void) sd_device_get_property_value(info->sd_device, "ID_NET_DRIVER", &driver);
1722 (void) sd_device_get_property_value(info->sd_device, "ID_PATH", &path);
1723
1724 if (sd_device_get_property_value(info->sd_device, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
1725 (void) sd_device_get_property_value(info->sd_device, "ID_VENDOR", &vendor);
1726
1727 if (sd_device_get_property_value(info->sd_device, "ID_MODEL_FROM_DATABASE", &model) < 0)
1728 (void) sd_device_get_property_value(info->sd_device, "ID_MODEL", &model);
1729 }
1730
1731 r = net_get_type_string(info->sd_device, info->iftype, &t);
1732 if (r == -ENOMEM)
1733 return log_oom();
1734
1735 char lease_file[STRLEN("/run/systemd/netif/leases/") + DECIMAL_STR_MAX(int)];
1736 xsprintf(lease_file, "/run/systemd/netif/leases/%i", info->ifindex);
1737
1738 (void) dhcp_lease_load(&lease, lease_file);
1739
1740 r = format_dropins(network_dropins);
1741 if (r < 0)
1742 return r;
1743
1744 if (strv_prepend(&network_dropins, network) < 0)
1745 return log_oom();
1746
1747 r = format_dropins(link_dropins);
1748 if (r < 0)
1749 return r;
1750
1751 if (strv_prepend(&link_dropins, link) < 0)
1752 return log_oom();
1753
1754 table = table_new("dot", "key", "value");
1755 if (!table)
1756 return log_oom();
1757
1758 if (arg_full)
1759 table_set_width(table, 0);
1760
1761 assert_se(cell = table_get_cell(table, 0, 0));
1762 (void) table_set_ellipsize_percent(table, cell, 100);
1763
1764 assert_se(cell = table_get_cell(table, 0, 1));
1765 (void) table_set_ellipsize_percent(table, cell, 100);
1766
1767 table_set_header(table, false);
1768
1769 /* First line: circle, ifindex, ifname. */
1770 r = table_add_many(table,
1771 TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE),
1772 TABLE_SET_COLOR, on_color_operational);
1773 if (r < 0)
1774 return table_log_add_error(r);
1775 r = table_add_cell_stringf(table, &cell, "%i: %s", info->ifindex, info->name);
1776 if (r < 0)
1777 return table_log_add_error(r);
1778 r = table_add_many(table, TABLE_EMPTY);
1779 if (r < 0)
1780 return table_log_add_error(r);
1781
1782 (void) table_set_align_percent(table, cell, 0);
1783
1784 /* unit files and basic states. */
1785 r = table_add_many(table,
1786 TABLE_EMPTY,
1787 TABLE_STRING, "Link File:",
1788 TABLE_SET_ALIGN_PERCENT, 100,
1789 TABLE_STRV, link_dropins ?: STRV_MAKE("n/a"),
1790 TABLE_EMPTY,
1791 TABLE_STRING, "Network File:",
1792 TABLE_STRV, network_dropins ?: STRV_MAKE("n/a"),
1793 TABLE_EMPTY,
1794 TABLE_STRING, "State:");
1795 if (r < 0)
1796 return table_log_add_error(r);
1797
1798 r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)",
1799 on_color_operational, strna(operational_state), off_color_operational,
1800 on_color_setup, setup_state ?: "unmanaged", off_color_setup);
1801 if (r < 0)
1802 return table_log_add_error(r);
1803
1804 r = table_add_many(table,
1805 TABLE_EMPTY,
1806 TABLE_STRING, "Online state:",
1807 TABLE_STRING, online_state ?: "unknown",
1808 TABLE_SET_COLOR, on_color_online);
1809 if (r < 0)
1810 return table_log_add_error(r);
1811
1812 r = table_add_string_line(table, "Type:", t);
1813 if (r < 0)
1814 return r;
1815
1816 r = table_add_string_line(table, "Kind:", info->netdev_kind);
1817 if (r < 0)
1818 return r;
1819
1820 r = table_add_string_line(table, "Path:", path);
1821 if (r < 0)
1822 return r;
1823
1824 r = table_add_string_line(table, "Driver:", driver);
1825 if (r < 0)
1826 return r;
1827
1828 r = table_add_string_line(table, "Vendor:", vendor);
1829 if (r < 0)
1830 return r;
1831
1832 r = table_add_string_line(table, "Model:", model);
1833 if (r < 0)
1834 return r;
1835
1836 strv_sort(info->alternative_names);
1837 r = dump_list(table, "Alternative Names:", info->alternative_names);
1838 if (r < 0)
1839 return r;
1840
1841 if (info->has_hw_address) {
1842 r = dump_hw_address(table, hwdb, "Hardware Address:", &info->hw_address);
1843 if (r < 0)
1844 return r;
1845 }
1846
1847 if (info->has_permanent_hw_address) {
1848 r = dump_hw_address(table, hwdb, "Permanent Hardware Address:", &info->permanent_hw_address);
1849 if (r < 0)
1850 return r;
1851 }
1852
1853 if (info->mtu > 0) {
1854 char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)];
1855
1856 xsprintf(min_str, "%" PRIu32, info->min_mtu);
1857 xsprintf(max_str, "%" PRIu32, info->max_mtu);
1858
1859 r = table_add_many(table,
1860 TABLE_EMPTY,
1861 TABLE_STRING, "MTU:");
1862 if (r < 0)
1863 return table_log_add_error(r);
1864 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s",
1865 info->mtu,
1866 info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "",
1867 info->min_mtu > 0 ? "min: " : "",
1868 info->min_mtu > 0 ? min_str : "",
1869 info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "",
1870 info->max_mtu > 0 ? "max: " : "",
1871 info->max_mtu > 0 ? max_str : "",
1872 info->min_mtu > 0 || info->max_mtu > 0 ? ")" : "");
1873 if (r < 0)
1874 return table_log_add_error(r);
1875 }
1876
1877 r = table_add_string_line(table, "QDisc:", info->qdisc);
1878 if (r < 0)
1879 return r;
1880
1881 if (info->master > 0) {
1882 r = table_add_many(table,
1883 TABLE_EMPTY,
1884 TABLE_STRING, "Master:",
1885 TABLE_IFINDEX, info->master);
1886 if (r < 0)
1887 return table_log_add_error(r);
1888 }
1889
1890 if (info->has_ipv6_address_generation_mode) {
1891 static const struct {
1892 const char *mode;
1893 } mode_table[] = {
1894 { "eui64" },
1895 { "none" },
1896 { "stable-privacy" },
1897 { "random" },
1898 };
1899
1900 r = table_add_many(table,
1901 TABLE_EMPTY,
1902 TABLE_STRING, "IPv6 Address Generation Mode:",
1903 TABLE_STRING, mode_table[info->addr_gen_mode]);
1904 if (r < 0)
1905 return table_log_add_error(r);
1906 }
1907
1908 if (streq_ptr(info->netdev_kind, "bridge")) {
1909 r = table_add_many(table,
1910 TABLE_EMPTY,
1911 TABLE_STRING, "Forward Delay:",
1912 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->forward_delay),
1913 TABLE_EMPTY,
1914 TABLE_STRING, "Hello Time:",
1915 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->hello_time),
1916 TABLE_EMPTY,
1917 TABLE_STRING, "Max Age:",
1918 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->max_age),
1919 TABLE_EMPTY,
1920 TABLE_STRING, "Ageing Time:",
1921 TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->ageing_time),
1922 TABLE_EMPTY,
1923 TABLE_STRING, "Priority:",
1924 TABLE_UINT16, info->priority,
1925 TABLE_EMPTY,
1926 TABLE_STRING, "STP:",
1927 TABLE_BOOLEAN, info->stp_state > 0,
1928 TABLE_EMPTY,
1929 TABLE_STRING, "Multicast IGMP Version:",
1930 TABLE_UINT8, info->mcast_igmp_version,
1931 TABLE_EMPTY,
1932 TABLE_STRING, "Cost:",
1933 TABLE_UINT32, info->cost);
1934 if (r < 0)
1935 return table_log_add_error(r);
1936
1937 if (info->port_state <= BR_STATE_BLOCKING) {
1938 r = table_add_many(table,
1939 TABLE_EMPTY,
1940 TABLE_STRING, "Port State:",
1941 TABLE_STRING, bridge_state_to_string(info->port_state));
1942 if (r < 0)
1943 return table_log_add_error(r);
1944 }
1945
1946 } else if (streq_ptr(info->netdev_kind, "bond")) {
1947 r = table_add_many(table,
1948 TABLE_EMPTY,
1949 TABLE_STRING, "Mode:",
1950 TABLE_STRING, bond_mode_to_string(info->mode),
1951 TABLE_EMPTY,
1952 TABLE_STRING, "Miimon:",
1953 TABLE_TIMESPAN_MSEC, info->miimon * USEC_PER_MSEC,
1954 TABLE_EMPTY,
1955 TABLE_STRING, "Updelay:",
1956 TABLE_TIMESPAN_MSEC, info->updelay * USEC_PER_MSEC,
1957 TABLE_EMPTY,
1958 TABLE_STRING, "Downdelay:",
1959 TABLE_TIMESPAN_MSEC, info->downdelay * USEC_PER_MSEC);
1960 if (r < 0)
1961 return table_log_add_error(r);
1962
1963 } else if (streq_ptr(info->netdev_kind, "vxlan")) {
1964 char ttl[CONST_MAX(STRLEN("auto") + 1, DECIMAL_STR_MAX(uint8_t))];
1965
1966 if (info->vxlan_info.vni > 0) {
1967 r = table_add_many(table,
1968 TABLE_EMPTY,
1969 TABLE_STRING, "VNI:",
1970 TABLE_UINT32, info->vxlan_info.vni);
1971 if (r < 0)
1972 return table_log_add_error(r);
1973 }
1974
1975 if (IN_SET(info->vxlan_info.group_family, AF_INET, AF_INET6)) {
1976 const char *p;
1977
1978 r = in_addr_is_multicast(info->vxlan_info.group_family, &info->vxlan_info.group);
1979 if (r <= 0)
1980 p = "Remote:";
1981 else
1982 p = "Group:";
1983
1984 r = table_add_many(table,
1985 TABLE_EMPTY,
1986 TABLE_STRING, p,
1987 info->vxlan_info.group_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR,
1988 &info->vxlan_info.group);
1989 if (r < 0)
1990 return table_log_add_error(r);
1991 }
1992
1993 if (IN_SET(info->vxlan_info.local_family, AF_INET, AF_INET6)) {
1994 r = table_add_many(table,
1995 TABLE_EMPTY,
1996 TABLE_STRING, "Local:",
1997 info->vxlan_info.local_family == AF_INET ? TABLE_IN_ADDR : TABLE_IN6_ADDR,
1998 &info->vxlan_info.local);
1999 if (r < 0)
2000 return table_log_add_error(r);
2001 }
2002
2003 if (info->vxlan_info.dest_port > 0) {
2004 r = table_add_many(table,
2005 TABLE_EMPTY,
2006 TABLE_STRING, "Destination Port:",
2007 TABLE_UINT16, be16toh(info->vxlan_info.dest_port));
2008 if (r < 0)
2009 return table_log_add_error(r);
2010 }
2011
2012 if (info->vxlan_info.link > 0) {
2013 r = table_add_many(table,
2014 TABLE_EMPTY,
2015 TABLE_STRING, "Underlying Device:",
2016 TABLE_IFINDEX, info->vxlan_info.link);
2017 if (r < 0)
2018 return table_log_add_error(r);
2019 }
2020
2021 r = table_add_many(table,
2022 TABLE_EMPTY,
2023 TABLE_STRING, "Learning:",
2024 TABLE_BOOLEAN, info->vxlan_info.learning);
2025 if (r < 0)
2026 return table_log_add_error(r);
2027
2028 r = table_add_many(table,
2029 TABLE_EMPTY,
2030 TABLE_STRING, "RSC:",
2031 TABLE_BOOLEAN, info->vxlan_info.rsc);
2032 if (r < 0)
2033 return table_log_add_error(r);
2034
2035 r = table_add_many(table,
2036 TABLE_EMPTY,
2037 TABLE_STRING, "L3MISS:",
2038 TABLE_BOOLEAN, info->vxlan_info.l3miss);
2039 if (r < 0)
2040 return table_log_add_error(r);
2041
2042 r = table_add_many(table,
2043 TABLE_EMPTY,
2044 TABLE_STRING, "L2MISS:",
2045 TABLE_BOOLEAN, info->vxlan_info.l2miss);
2046 if (r < 0)
2047 return table_log_add_error(r);
2048
2049 if (info->vxlan_info.tos > 1) {
2050 r = table_add_many(table,
2051 TABLE_EMPTY,
2052 TABLE_STRING, "TOS:",
2053 TABLE_UINT8, info->vxlan_info.tos);
2054 if (r < 0)
2055 return table_log_add_error(r);
2056 }
2057
2058 if (info->vxlan_info.ttl > 0)
2059 xsprintf(ttl, "%" PRIu8, info->vxlan_info.ttl);
2060 else
2061 strcpy(ttl, "auto");
2062
2063 r = table_add_many(table,
2064 TABLE_EMPTY,
2065 TABLE_STRING, "TTL:",
2066 TABLE_STRING, ttl);
2067 if (r < 0)
2068 return table_log_add_error(r);
2069
2070 } else if (streq_ptr(info->netdev_kind, "vlan") && info->vlan_id > 0) {
2071 r = table_add_many(table,
2072 TABLE_EMPTY,
2073 TABLE_STRING, "VLan Id:",
2074 TABLE_UINT16, info->vlan_id);
2075 if (r < 0)
2076 return table_log_add_error(r);
2077
2078 } else if (STRPTR_IN_SET(info->netdev_kind, "ipip", "sit", "gre", "gretap", "erspan", "vti")) {
2079 if (in_addr_is_set(AF_INET, &info->local)) {
2080 r = table_add_many(table,
2081 TABLE_EMPTY,
2082 TABLE_STRING, "Local:",
2083 TABLE_IN_ADDR, &info->local);
2084 if (r < 0)
2085 return table_log_add_error(r);
2086 }
2087
2088 if (in_addr_is_set(AF_INET, &info->remote)) {
2089 r = table_add_many(table,
2090 TABLE_EMPTY,
2091 TABLE_STRING, "Remote:",
2092 TABLE_IN_ADDR, &info->remote);
2093 if (r < 0)
2094 return table_log_add_error(r);
2095 }
2096
2097 } else if (STRPTR_IN_SET(info->netdev_kind, "ip6gre", "ip6gretap", "ip6erspan", "vti6")) {
2098 if (in_addr_is_set(AF_INET6, &info->local)) {
2099 r = table_add_many(table,
2100 TABLE_EMPTY,
2101 TABLE_STRING, "Local:",
2102 TABLE_IN6_ADDR, &info->local);
2103 if (r < 0)
2104 return table_log_add_error(r);
2105 }
2106
2107 if (in_addr_is_set(AF_INET6, &info->remote)) {
2108 r = table_add_many(table,
2109 TABLE_EMPTY,
2110 TABLE_STRING, "Remote:",
2111 TABLE_IN6_ADDR, &info->remote);
2112 if (r < 0)
2113 return table_log_add_error(r);
2114 }
2115
2116 } else if (streq_ptr(info->netdev_kind, "geneve")) {
2117 r = table_add_many(table,
2118 TABLE_EMPTY,
2119 TABLE_STRING, "VNI:",
2120 TABLE_UINT32, info->vni);
2121 if (r < 0)
2122 return table_log_add_error(r);
2123
2124 if (info->has_tunnel_ipv4 && in_addr_is_set(AF_INET, &info->remote)) {
2125 r = table_add_many(table,
2126 TABLE_EMPTY,
2127 TABLE_STRING, "Remote:",
2128 TABLE_IN_ADDR, &info->remote);
2129 if (r < 0)
2130 return table_log_add_error(r);
2131 } else if (in_addr_is_set(AF_INET6, &info->remote)) {
2132 r = table_add_many(table,
2133 TABLE_EMPTY,
2134 TABLE_STRING, "Remote:",
2135 TABLE_IN6_ADDR, &info->remote);
2136 if (r < 0)
2137 return table_log_add_error(r);
2138 }
2139
2140 if (info->ttl > 0) {
2141 r = table_add_many(table,
2142 TABLE_EMPTY,
2143 TABLE_STRING, "TTL:",
2144 TABLE_UINT8, info->ttl);
2145 if (r < 0)
2146 return table_log_add_error(r);
2147 }
2148
2149 if (info->tos > 0) {
2150 r = table_add_many(table,
2151 TABLE_EMPTY,
2152 TABLE_STRING, "TOS:",
2153 TABLE_UINT8, info->tos);
2154 if (r < 0)
2155 return table_log_add_error(r);
2156 }
2157
2158 r = table_add_many(table,
2159 TABLE_EMPTY,
2160 TABLE_STRING, "Port:",
2161 TABLE_UINT16, info->tunnel_port);
2162 if (r < 0)
2163 return table_log_add_error(r);
2164
2165 r = table_add_many(table,
2166 TABLE_EMPTY,
2167 TABLE_STRING, "Inherit:",
2168 TABLE_STRING, geneve_df_to_string(info->inherit));
2169 if (r < 0)
2170 return table_log_add_error(r);
2171
2172 if (info->df > 0) {
2173 r = table_add_many(table,
2174 TABLE_EMPTY,
2175 TABLE_STRING, "IPDoNotFragment:",
2176 TABLE_UINT8, info->df);
2177 if (r < 0)
2178 return table_log_add_error(r);
2179 }
2180
2181 r = table_add_many(table,
2182 TABLE_EMPTY,
2183 TABLE_STRING, "UDPChecksum:",
2184 TABLE_BOOLEAN, info->csum);
2185 if (r < 0)
2186 return table_log_add_error(r);
2187
2188 r = table_add_many(table,
2189 TABLE_EMPTY,
2190 TABLE_STRING, "UDP6ZeroChecksumTx:",
2191 TABLE_BOOLEAN, info->csum6_tx);
2192 if (r < 0)
2193 return table_log_add_error(r);
2194
2195 r = table_add_many(table,
2196 TABLE_EMPTY,
2197 TABLE_STRING, "UDP6ZeroChecksumRx:",
2198 TABLE_BOOLEAN, info->csum6_rx);
2199 if (r < 0)
2200 return table_log_add_error(r);
2201
2202 if (info->label > 0) {
2203 r = table_add_many(table,
2204 TABLE_EMPTY,
2205 TABLE_STRING, "FlowLabel:",
2206 TABLE_UINT32, info->label);
2207 if (r < 0)
2208 return table_log_add_error(r);
2209 }
2210
2211 } else if (STRPTR_IN_SET(info->netdev_kind, "macvlan", "macvtap")) {
2212 r = table_add_many(table,
2213 TABLE_EMPTY,
2214 TABLE_STRING, "Mode:",
2215 TABLE_STRING, macvlan_mode_to_string(info->macvlan_mode));
2216 if (r < 0)
2217 return table_log_add_error(r);
2218
2219 } else if (streq_ptr(info->netdev_kind, "ipvlan")) {
2220 _cleanup_free_ char *p = NULL, *s = NULL;
2221
2222 if (info->ipvlan_flags & IPVLAN_F_PRIVATE)
2223 p = strdup("private");
2224 else if (info->ipvlan_flags & IPVLAN_F_VEPA)
2225 p = strdup("vepa");
2226 else
2227 p = strdup("bridge");
2228 if (!p)
2229 log_oom();
2230
2231 s = strjoin(ipvlan_mode_to_string(info->ipvlan_mode), " (", p, ")");
2232 if (!s)
2233 return log_oom();
2234
2235 r = table_add_many(table,
2236 TABLE_EMPTY,
2237 TABLE_STRING, "Mode:",
2238 TABLE_STRING, s);
2239 if (r < 0)
2240 return table_log_add_error(r);
2241 }
2242
2243 if (info->has_wlan_link_info) {
2244 _cleanup_free_ char *esc = NULL;
2245
2246 r = table_add_many(table,
2247 TABLE_EMPTY,
2248 TABLE_STRING, "WiFi access point:");
2249 if (r < 0)
2250 return table_log_add_error(r);
2251
2252 if (info->ssid)
2253 esc = cescape(info->ssid);
2254
2255 r = table_add_cell_stringf(table, NULL, "%s (%s)",
2256 strnull(esc),
2257 ETHER_ADDR_TO_STR(&info->bssid));
2258 if (r < 0)
2259 return table_log_add_error(r);
2260 }
2261
2262 if (info->has_bitrates) {
2263 r = table_add_many(table,
2264 TABLE_EMPTY,
2265 TABLE_STRING, "Bit Rate (Tx/Rx):");
2266 if (r < 0)
2267 return table_log_add_error(r);
2268 r = table_add_cell_stringf(table, NULL, "%sbps/%sbps",
2269 FORMAT_BYTES_FULL(info->tx_bitrate, 0),
2270 FORMAT_BYTES_FULL(info->rx_bitrate, 0));
2271 if (r < 0)
2272 return table_log_add_error(r);
2273 }
2274
2275 if (info->has_tx_queues || info->has_rx_queues) {
2276 r = table_add_many(table,
2277 TABLE_EMPTY,
2278 TABLE_STRING, "Number of Queues (Tx/Rx):");
2279 if (r < 0)
2280 return table_log_add_error(r);
2281 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues);
2282 if (r < 0)
2283 return table_log_add_error(r);
2284 }
2285
2286 if (info->has_ethtool_link_info) {
2287 if (IN_SET(info->autonegotiation, AUTONEG_DISABLE, AUTONEG_ENABLE)) {
2288 r = table_add_many(table,
2289 TABLE_EMPTY,
2290 TABLE_STRING, "Auto negotiation:",
2291 TABLE_BOOLEAN, info->autonegotiation == AUTONEG_ENABLE);
2292 if (r < 0)
2293 return table_log_add_error(r);
2294 }
2295
2296 if (info->speed > 0 && info->speed != UINT64_MAX) {
2297 r = table_add_many(table,
2298 TABLE_EMPTY,
2299 TABLE_STRING, "Speed:",
2300 TABLE_BPS, info->speed);
2301 if (r < 0)
2302 return table_log_add_error(r);
2303 }
2304
2305 r = table_add_string_line(table, "Duplex:", duplex_to_string(info->duplex));
2306 if (r < 0)
2307 return r;
2308
2309 r = table_add_string_line(table, "Port:", port_to_string(info->port));
2310 if (r < 0)
2311 return r;
2312 }
2313
2314 r = dump_addresses(rtnl, lease, table, info->ifindex);
2315 if (r < 0)
2316 return r;
2317 r = dump_gateways(rtnl, hwdb, table, info->ifindex);
2318 if (r < 0)
2319 return r;
2320 r = dump_list(table, "DNS:", dns);
2321 if (r < 0)
2322 return r;
2323 r = dump_list(table, "Search Domains:", search_domains);
2324 if (r < 0)
2325 return r;
2326 r = dump_list(table, "Route Domains:", route_domains);
2327 if (r < 0)
2328 return r;
2329 r = dump_list(table, "NTP:", ntp);
2330 if (r < 0)
2331 return r;
2332 r = dump_list(table, "SIP:", sip);
2333 if (r < 0)
2334 return r;
2335 r = dump_ifindexes(table, "Carrier Bound To:", carrier_bound_to);
2336 if (r < 0)
2337 return r;
2338 r = dump_ifindexes(table, "Carrier Bound By:", carrier_bound_by);
2339 if (r < 0)
2340 return r;
2341
2342 r = table_add_string_line(table, "Activation Policy:", activation_policy);
2343 if (r < 0)
2344 return r;
2345
2346 r = sd_network_link_get_required_for_online(info->ifindex);
2347 if (r >= 0) {
2348 r = table_add_many(table,
2349 TABLE_EMPTY,
2350 TABLE_STRING, "Required For Online:",
2351 TABLE_BOOLEAN, r);
2352 if (r < 0)
2353 return table_log_add_error(r);
2354 }
2355
2356 if (captive_portal) {
2357 r = table_add_many(table,
2358 TABLE_EMPTY,
2359 TABLE_STRING, "Captive Portal:",
2360 TABLE_STRING, captive_portal,
2361 TABLE_SET_URL, captive_portal);
2362 if (r < 0)
2363 return table_log_add_error(r);
2364 }
2365
2366 if (lease) {
2367 const void *client_id;
2368 size_t client_id_len;
2369 const char *tz;
2370
2371 r = sd_dhcp_lease_get_timezone(lease, &tz);
2372 if (r >= 0) {
2373 r = table_add_many(table,
2374 TABLE_EMPTY,
2375 TABLE_STRING, "Time Zone:",
2376 TABLE_STRING, tz);
2377 if (r < 0)
2378 return table_log_add_error(r);
2379 }
2380
2381 r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len);
2382 if (r >= 0) {
2383 _cleanup_free_ char *id = NULL;
2384
2385 r = sd_dhcp_client_id_to_string(client_id, client_id_len, &id);
2386 if (r >= 0) {
2387 r = table_add_many(table,
2388 TABLE_EMPTY,
2389 TABLE_STRING, "DHCP4 Client ID:",
2390 TABLE_STRING, id);
2391 if (r < 0)
2392 return table_log_add_error(r);
2393 }
2394 }
2395 }
2396
2397 r = sd_network_link_get_dhcp6_client_iaid_string(info->ifindex, &iaid);
2398 if (r >= 0) {
2399 r = table_add_many(table,
2400 TABLE_EMPTY,
2401 TABLE_STRING, "DHCP6 Client IAID:",
2402 TABLE_STRING, iaid);
2403 if (r < 0)
2404 return table_log_add_error(r);
2405 }
2406
2407 r = sd_network_link_get_dhcp6_client_duid_string(info->ifindex, &duid);
2408 if (r >= 0) {
2409 r = table_add_many(table,
2410 TABLE_EMPTY,
2411 TABLE_STRING, "DHCP6 Client DUID:",
2412 TABLE_STRING, duid);
2413 if (r < 0)
2414 return table_log_add_error(r);
2415 }
2416
2417 r = dump_lldp_neighbors(table, "Connected To:", info->ifindex);
2418 if (r < 0)
2419 return r;
2420
2421 r = dump_dhcp_leases(table, "Offered DHCP leases:", bus, info);
2422 if (r < 0)
2423 return r;
2424
2425 r = dump_statistics(table, info);
2426 if (r < 0)
2427 return r;
2428
2429 r = table_print(table, NULL);
2430 if (r < 0)
2431 return table_log_print_error(r);
2432
2433 return show_logs(info);
2434 }
2435
2436 static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
2437 _cleanup_free_ char *operational_state = NULL, *online_state = NULL;
2438 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
2439 const char *on_color_operational, *on_color_online;
2440 _cleanup_(table_unrefp) Table *table = NULL;
2441 TableCell *cell;
2442 int r;
2443
2444 assert(rtnl);
2445
2446 (void) sd_network_get_operational_state(&operational_state);
2447 operational_state_to_color(NULL, operational_state, &on_color_operational, NULL);
2448
2449 (void) sd_network_get_online_state(&online_state);
2450 online_state_to_color(online_state, &on_color_online, NULL);
2451
2452 table = table_new("dot", "key", "value");
2453 if (!table)
2454 return log_oom();
2455
2456 if (arg_full)
2457 table_set_width(table, 0);
2458
2459 assert_se(cell = table_get_cell(table, 0, 0));
2460 (void) table_set_ellipsize_percent(table, cell, 100);
2461
2462 assert_se(cell = table_get_cell(table, 0, 1));
2463 (void) table_set_align_percent(table, cell, 100);
2464 (void) table_set_ellipsize_percent(table, cell, 100);
2465
2466 table_set_header(table, false);
2467
2468 r = table_add_many(table,
2469 TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE),
2470 TABLE_SET_COLOR, on_color_operational,
2471 TABLE_STRING, "State:",
2472 TABLE_STRING, strna(operational_state),
2473 TABLE_SET_COLOR, on_color_operational,
2474 TABLE_EMPTY,
2475 TABLE_STRING, "Online state:",
2476 TABLE_STRING, online_state ?: "unknown",
2477 TABLE_SET_COLOR, on_color_online);
2478 if (r < 0)
2479 return table_log_add_error(r);
2480
2481 r = dump_addresses(rtnl, NULL, table, 0);
2482 if (r < 0)
2483 return r;
2484 r = dump_gateways(rtnl, hwdb, table, 0);
2485 if (r < 0)
2486 return r;
2487
2488 (void) sd_network_get_dns(&dns);
2489 r = dump_list(table, "DNS:", dns);
2490 if (r < 0)
2491 return r;
2492
2493 (void) sd_network_get_search_domains(&search_domains);
2494 r = dump_list(table, "Search Domains:", search_domains);
2495 if (r < 0)
2496 return r;
2497
2498 (void) sd_network_get_route_domains(&route_domains);
2499 r = dump_list(table, "Route Domains:", route_domains);
2500 if (r < 0)
2501 return r;
2502
2503 (void) sd_network_get_ntp(&ntp);
2504 r = dump_list(table, "NTP:", ntp);
2505 if (r < 0)
2506 return r;
2507
2508 r = table_print(table, NULL);
2509 if (r < 0)
2510 return table_log_print_error(r);
2511
2512 return show_logs(NULL);
2513 }
2514
2515 static int link_status(int argc, char *argv[], void *userdata) {
2516 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2517 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2518 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
2519 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
2520 int r, c;
2521
2522 r = acquire_bus(&bus);
2523 if (r < 0)
2524 return r;
2525
2526 if (arg_json_format_flags != JSON_FORMAT_OFF) {
2527 if (arg_all || argc <= 1)
2528 return dump_manager_description(bus);
2529 else
2530 return dump_link_description(bus, strv_skip(argv, 1));
2531 }
2532
2533 pager_open(arg_pager_flags);
2534
2535 r = sd_netlink_open(&rtnl);
2536 if (r < 0)
2537 return log_error_errno(r, "Failed to connect to netlink: %m");
2538
2539 r = sd_hwdb_new(&hwdb);
2540 if (r < 0)
2541 log_debug_errno(r, "Failed to open hardware database: %m");
2542
2543 if (arg_all)
2544 c = acquire_link_info(bus, rtnl, NULL, &links);
2545 else if (argc <= 1)
2546 return system_status(rtnl, hwdb);
2547 else
2548 c = acquire_link_info(bus, rtnl, argv + 1, &links);
2549 if (c < 0)
2550 return c;
2551
2552 for (int i = 0; i < c; i++) {
2553 if (i > 0)
2554 fputc('\n', stdout);
2555
2556 link_status_one(bus, rtnl, hwdb, links + i);
2557 }
2558
2559 return 0;
2560 }
2561
2562 static char *lldp_capabilities_to_string(uint16_t x) {
2563 static const char characters[] = {
2564 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
2565 };
2566 char *ret;
2567 unsigned i;
2568
2569 ret = new(char, ELEMENTSOF(characters) + 1);
2570 if (!ret)
2571 return NULL;
2572
2573 for (i = 0; i < ELEMENTSOF(characters); i++)
2574 ret[i] = (x & (1U << i)) ? characters[i] : '.';
2575
2576 ret[i] = 0;
2577 return ret;
2578 }
2579
2580 static void lldp_capabilities_legend(uint16_t x) {
2581 unsigned cols = columns();
2582 static const char* const table[] = {
2583 "o - Other",
2584 "p - Repeater",
2585 "b - Bridge",
2586 "w - WLAN Access Point",
2587 "r - Router",
2588 "t - Telephone",
2589 "d - DOCSIS cable device",
2590 "a - Station",
2591 "c - Customer VLAN",
2592 "s - Service VLAN",
2593 "m - Two-port MAC Relay (TPMR)",
2594 };
2595
2596 if (x == 0)
2597 return;
2598
2599 printf("\nCapability Flags:\n");
2600 for (unsigned w = 0, i = 0; i < ELEMENTSOF(table); i++)
2601 if (x & (1U << i) || arg_all) {
2602 bool newline;
2603
2604 newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols;
2605 if (newline)
2606 w = 0;
2607 w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]);
2608 }
2609 puts("");
2610 }
2611
2612 static int link_lldp_status(int argc, char *argv[], void *userdata) {
2613 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2614 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
2615 _cleanup_(table_unrefp) Table *table = NULL;
2616 int r, c, m = 0;
2617 uint16_t all = 0;
2618 TableCell *cell;
2619
2620 r = sd_netlink_open(&rtnl);
2621 if (r < 0)
2622 return log_error_errno(r, "Failed to connect to netlink: %m");
2623
2624 c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
2625 if (c < 0)
2626 return c;
2627
2628 pager_open(arg_pager_flags);
2629
2630 table = table_new("link",
2631 "chassis-id",
2632 "system-name",
2633 "caps",
2634 "port-id",
2635 "port-description");
2636 if (!table)
2637 return log_oom();
2638
2639 if (arg_full)
2640 table_set_width(table, 0);
2641
2642 table_set_header(table, arg_legend);
2643
2644 assert_se(cell = table_get_cell(table, 0, 3));
2645 table_set_minimum_width(table, cell, 11);
2646 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
2647
2648 FOREACH_ARRAY(link, links, c) {
2649 _cleanup_fclose_ FILE *f = NULL;
2650
2651 r = open_lldp_neighbors(link->ifindex, &f);
2652 if (r == -ENOENT)
2653 continue;
2654 if (r < 0) {
2655 log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", link->ifindex);
2656 continue;
2657 }
2658
2659 for (;;) {
2660 const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL;
2661 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
2662 _cleanup_free_ char *capabilities = NULL;
2663 uint16_t cc;
2664
2665 r = next_lldp_neighbor(f, &n);
2666 if (r < 0) {
2667 log_warning_errno(r, "Failed to read neighbor data: %m");
2668 break;
2669 }
2670 if (r == 0)
2671 break;
2672
2673 (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
2674 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
2675 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
2676 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
2677
2678 if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) {
2679 capabilities = lldp_capabilities_to_string(cc);
2680 all |= cc;
2681 }
2682
2683 r = table_add_many(table,
2684 TABLE_STRING, link->name,
2685 TABLE_STRING, chassis_id,
2686 TABLE_STRING, system_name,
2687 TABLE_STRING, capabilities,
2688 TABLE_STRING, port_id,
2689 TABLE_STRING, port_description);
2690 if (r < 0)
2691 return table_log_add_error(r);
2692
2693 m++;
2694 }
2695 }
2696
2697 r = table_print(table, NULL);
2698 if (r < 0)
2699 return table_log_print_error(r);
2700
2701 if (arg_legend) {
2702 lldp_capabilities_legend(all);
2703 printf("\n%i neighbors listed.\n", m);
2704 }
2705
2706 return 0;
2707 }
2708
2709 static int link_delete_send_message(sd_netlink *rtnl, int index) {
2710 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2711 int r;
2712
2713 assert(rtnl);
2714 assert(index >= 0);
2715
2716 r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index);
2717 if (r < 0)
2718 return rtnl_log_create_error(r);
2719
2720 r = sd_netlink_call(rtnl, req, 0, NULL);
2721 if (r < 0)
2722 return r;
2723
2724 return 0;
2725 }
2726
2727 static int link_up_down_send_message(sd_netlink *rtnl, char *command, int index) {
2728 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2729 int r;
2730
2731 assert(rtnl);
2732 assert(index >= 0);
2733
2734 r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, index);
2735 if (r < 0)
2736 return rtnl_log_create_error(r);
2737
2738 if (streq(command, "up"))
2739 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
2740 else
2741 r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP);
2742 if (r < 0)
2743 return log_error_errno(r, "Could not set link flags: %m");
2744
2745 r = sd_netlink_call(rtnl, req, 0, NULL);
2746 if (r < 0)
2747 return r;
2748
2749 return 0;
2750 }
2751
2752 static int link_up_down(int argc, char *argv[], void *userdata) {
2753 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2754 _cleanup_set_free_ Set *indexes = NULL;
2755 int index, r;
2756 void *p;
2757
2758 r = sd_netlink_open(&rtnl);
2759 if (r < 0)
2760 return log_error_errno(r, "Failed to connect to netlink: %m");
2761
2762 indexes = set_new(NULL);
2763 if (!indexes)
2764 return log_oom();
2765
2766 for (int i = 1; i < argc; i++) {
2767 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2768 if (index < 0)
2769 return index;
2770
2771 r = set_put(indexes, INT_TO_PTR(index));
2772 if (r < 0)
2773 return log_oom();
2774 }
2775
2776 SET_FOREACH(p, indexes) {
2777 index = PTR_TO_INT(p);
2778 r = link_up_down_send_message(rtnl, argv[0], index);
2779 if (r < 0)
2780 return log_error_errno(r, "Failed to bring %s interface %s: %m",
2781 argv[0], FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX));
2782 }
2783
2784 return r;
2785 }
2786
2787 static int link_delete(int argc, char *argv[], void *userdata) {
2788 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2789 _cleanup_set_free_ Set *indexes = NULL;
2790 int index, r;
2791 void *p;
2792
2793 r = sd_netlink_open(&rtnl);
2794 if (r < 0)
2795 return log_error_errno(r, "Failed to connect to netlink: %m");
2796
2797 indexes = set_new(NULL);
2798 if (!indexes)
2799 return log_oom();
2800
2801 for (int i = 1; i < argc; i++) {
2802 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2803 if (index < 0)
2804 return index;
2805
2806 r = set_put(indexes, INT_TO_PTR(index));
2807 if (r < 0)
2808 return log_oom();
2809 }
2810
2811 SET_FOREACH(p, indexes) {
2812 index = PTR_TO_INT(p);
2813 r = link_delete_send_message(rtnl, index);
2814 if (r < 0)
2815 return log_error_errno(r, "Failed to delete interface %s: %m",
2816 FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX));
2817 }
2818
2819 return r;
2820 }
2821
2822 static int link_renew_one(sd_bus *bus, int index, const char *name) {
2823 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2824 int r;
2825
2826 assert(bus);
2827 assert(index >= 0);
2828 assert(name);
2829
2830 r = bus_call_method(bus, bus_network_mgr, "RenewLink", &error, NULL, "i", index);
2831 if (r < 0)
2832 return log_error_errno(r, "Failed to renew dynamic configuration of interface %s: %s",
2833 name, bus_error_message(&error, r));
2834
2835 return 0;
2836 }
2837
2838 static int link_renew(int argc, char *argv[], void *userdata) {
2839 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2840 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2841 int index, k = 0, r;
2842
2843 r = acquire_bus(&bus);
2844 if (r < 0)
2845 return r;
2846
2847 for (int i = 1; i < argc; i++) {
2848 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2849 if (index < 0)
2850 return index;
2851
2852 r = link_renew_one(bus, index, argv[i]);
2853 if (r < 0 && k >= 0)
2854 k = r;
2855 }
2856
2857 return k;
2858 }
2859
2860 static int link_force_renew_one(sd_bus *bus, int index, const char *name) {
2861 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2862 int r;
2863
2864 assert(bus);
2865 assert(index >= 0);
2866 assert(name);
2867
2868 r = bus_call_method(bus, bus_network_mgr, "ForceRenewLink", &error, NULL, "i", index);
2869 if (r < 0)
2870 return log_error_errno(r, "Failed to force renew dynamic configuration of interface %s: %s",
2871 name, bus_error_message(&error, r));
2872
2873 return 0;
2874 }
2875
2876 static int link_force_renew(int argc, char *argv[], void *userdata) {
2877 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2878 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2879 int k = 0, r;
2880
2881 r = acquire_bus(&bus);
2882 if (r < 0)
2883 return r;
2884
2885 for (int i = 1; i < argc; i++) {
2886 int index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2887 if (index < 0)
2888 return index;
2889
2890 r = link_force_renew_one(bus, index, argv[i]);
2891 if (r < 0 && k >= 0)
2892 k = r;
2893 }
2894
2895 return k;
2896 }
2897
2898 static int verb_reload(int argc, char *argv[], void *userdata) {
2899 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2900 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2901 int r;
2902
2903 r = acquire_bus(&bus);
2904 if (r < 0)
2905 return r;
2906
2907 r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
2908 if (r < 0)
2909 return log_error_errno(r, "Failed to reload network settings: %s", bus_error_message(&error, r));
2910
2911 return 0;
2912 }
2913
2914 static int verb_reconfigure(int argc, char *argv[], void *userdata) {
2915 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2916 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2917 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
2918 _cleanup_set_free_ Set *indexes = NULL;
2919 int index, r;
2920 void *p;
2921
2922 r = acquire_bus(&bus);
2923 if (r < 0)
2924 return r;
2925
2926 indexes = set_new(NULL);
2927 if (!indexes)
2928 return log_oom();
2929
2930 for (int i = 1; i < argc; i++) {
2931 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
2932 if (index < 0)
2933 return index;
2934
2935 r = set_put(indexes, INT_TO_PTR(index));
2936 if (r < 0)
2937 return log_oom();
2938 }
2939
2940 SET_FOREACH(p, indexes) {
2941 index = PTR_TO_INT(p);
2942 r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index);
2943 if (r < 0)
2944 return log_error_errno(r, "Failed to reconfigure network interface %s: %s",
2945 FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX),
2946 bus_error_message(&error, r));
2947 }
2948
2949 return 0;
2950 }
2951
2952 typedef enum ReloadFlags {
2953 RELOAD_NETWORKD = 1 << 0,
2954 RELOAD_UDEVD = 1 << 1,
2955 } ReloadFlags;
2956
2957 static int get_config_files_by_name(const char *name, char **ret_path, char ***ret_dropins) {
2958 _cleanup_free_ char *path = NULL;
2959 int r;
2960
2961 assert(name);
2962 assert(ret_path);
2963
2964 STRV_FOREACH(i, NETWORK_DIRS) {
2965 _cleanup_free_ char *p = NULL;
2966
2967 p = path_join(*i, name);
2968 if (!p)
2969 return -ENOMEM;
2970
2971 r = RET_NERRNO(access(p, F_OK));
2972 if (r >= 0) {
2973 path = TAKE_PTR(p);
2974 break;
2975 }
2976
2977 if (r != -ENOENT)
2978 log_debug_errno(r, "Failed to determine whether '%s' exists, ignoring: %m", p);
2979 }
2980
2981 if (!path)
2982 return -ENOENT;
2983
2984 if (ret_dropins) {
2985 _cleanup_free_ char *dropin_dirname = NULL;
2986
2987 dropin_dirname = strjoin(name, ".d");
2988 if (!dropin_dirname)
2989 return -ENOMEM;
2990
2991 r = conf_files_list_dropins(ret_dropins, dropin_dirname, /* root = */ NULL, NETWORK_DIRS);
2992 if (r < 0)
2993 return r;
2994 }
2995
2996 *ret_path = TAKE_PTR(path);
2997
2998 return 0;
2999 }
3000
3001 static int get_dropin_by_name(
3002 const char *name,
3003 char * const *dropins,
3004 char **ret) {
3005
3006 assert(name);
3007 assert(dropins);
3008 assert(ret);
3009
3010 STRV_FOREACH(i, dropins)
3011 if (path_equal_filename(*i, name)) {
3012 _cleanup_free_ char *d = NULL;
3013
3014 d = strdup(*i);
3015 if (!d)
3016 return -ENOMEM;
3017
3018 *ret = TAKE_PTR(d);
3019 return 1;
3020 }
3021
3022 *ret = NULL;
3023 return 0;
3024 }
3025
3026 static int get_network_files_by_link(
3027 sd_netlink **rtnl,
3028 const char *link,
3029 char **ret_path,
3030 char ***ret_dropins) {
3031
3032 _cleanup_strv_free_ char **dropins = NULL;
3033 _cleanup_free_ char *path = NULL;
3034 int r, ifindex;
3035
3036 assert(rtnl);
3037 assert(link);
3038 assert(ret_path);
3039 assert(ret_dropins);
3040
3041 ifindex = rtnl_resolve_interface_or_warn(rtnl, link);
3042 if (ifindex < 0)
3043 return ifindex;
3044
3045 r = sd_network_link_get_network_file(ifindex, &path);
3046 if (r == -ENODATA)
3047 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
3048 "Link '%s' has no associated network file.", link);
3049 if (r < 0)
3050 return log_error_errno(r, "Failed to get network file for link '%s': %m", link);
3051
3052 r = sd_network_link_get_network_file_dropins(ifindex, &dropins);
3053 if (r < 0 && r != -ENODATA)
3054 return log_error_errno(r, "Failed to get network drop-ins for link '%s': %m", link);
3055
3056 *ret_path = TAKE_PTR(path);
3057 *ret_dropins = TAKE_PTR(dropins);
3058
3059 return 0;
3060 }
3061
3062 static int get_link_files_by_link(const char *link, char **ret_path, char ***ret_dropins) {
3063 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
3064 _cleanup_strv_free_ char **dropins_split = NULL;
3065 _cleanup_free_ char *p = NULL;
3066 const char *path, *dropins;
3067 int r;
3068
3069 assert(link);
3070 assert(ret_path);
3071 assert(ret_dropins);
3072
3073 r = sd_device_new_from_ifname(&device, link);
3074 if (r < 0)
3075 return log_error_errno(r, "Failed to create sd-device object for link '%s': %m", link);
3076
3077 r = sd_device_get_property_value(device, "ID_NET_LINK_FILE", &path);
3078 if (r == -ENOENT)
3079 return log_error_errno(r, "Link '%s' has no associated link file.", link);
3080 if (r < 0)
3081 return log_error_errno(r, "Failed to get link file for link '%s': %m", link);
3082
3083 r = sd_device_get_property_value(device, "ID_NET_LINK_FILE_DROPINS", &dropins);
3084 if (r < 0 && r != -ENOENT)
3085 return log_error_errno(r, "Failed to get link drop-ins for link '%s': %m", link);
3086 if (r >= 0) {
3087 r = strv_split_full(&dropins_split, dropins, ":", EXTRACT_CUNESCAPE);
3088 if (r < 0)
3089 return log_error_errno(r, "Failed to parse link drop-ins for link '%s': %m", link);
3090 }
3091
3092 p = strdup(path);
3093 if (!p)
3094 return log_oom();
3095
3096 *ret_path = TAKE_PTR(p);
3097 *ret_dropins = TAKE_PTR(dropins_split);
3098
3099 return 0;
3100 }
3101
3102 static int get_config_files_by_link_config(
3103 const char *link_config,
3104 sd_netlink **rtnl,
3105 char **ret_path,
3106 char ***ret_dropins,
3107 ReloadFlags *ret_reload) {
3108
3109 _cleanup_strv_free_ char **dropins = NULL, **link_config_split = NULL;
3110 _cleanup_free_ char *path = NULL;
3111 const char *ifname, *type;
3112 ReloadFlags reload;
3113 size_t n;
3114 int r;
3115
3116 assert(link_config);
3117 assert(rtnl);
3118 assert(ret_path);
3119 assert(ret_dropins);
3120
3121 link_config_split = strv_split(link_config, ":");
3122 if (!link_config_split)
3123 return log_oom();
3124
3125 n = strv_length(link_config_split);
3126 if (n == 0 || isempty(link_config_split[0]))
3127 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No link name is given.");
3128 if (n > 2)
3129 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid link config '%s'.", link_config);
3130
3131 ifname = link_config_split[0];
3132 type = n == 2 ? link_config_split[1] : "network";
3133
3134 if (streq(type, "network")) {
3135 if (!networkd_is_running())
3136 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
3137 "Cannot get network file for link if systemd-networkd is not running.");
3138
3139 r = get_network_files_by_link(rtnl, ifname, &path, &dropins);
3140 if (r < 0)
3141 return r;
3142
3143 reload = RELOAD_NETWORKD;
3144 } else if (streq(type, "link")) {
3145 r = get_link_files_by_link(ifname, &path, &dropins);
3146 if (r < 0)
3147 return r;
3148
3149 reload = RELOAD_UDEVD;
3150 } else
3151 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3152 "Invalid config type '%s' for link '%s'.", type, ifname);
3153
3154 *ret_path = TAKE_PTR(path);
3155 *ret_dropins = TAKE_PTR(dropins);
3156
3157 if (ret_reload)
3158 *ret_reload = reload;
3159
3160 return 0;
3161 }
3162
3163 static int add_config_to_edit(
3164 EditFileContext *context,
3165 const char *path,
3166 char * const *dropins) {
3167
3168 _cleanup_free_ char *new_path = NULL, *dropin_path = NULL, *old_dropin = NULL;
3169 _cleanup_strv_free_ char **comment_paths = NULL;
3170 int r;
3171
3172 assert(context);
3173 assert(path);
3174 assert(!arg_drop_in || dropins);
3175
3176 if (path_startswith(path, "/usr")) {
3177 _cleanup_free_ char *name = NULL;
3178
3179 r = path_extract_filename(path, &name);
3180 if (r < 0)
3181 return log_error_errno(r, "Failed to extract filename from '%s': %m", path);
3182
3183 new_path = path_join(NETWORK_DIRS[0], name);
3184 if (!new_path)
3185 return log_oom();
3186 }
3187
3188 if (!arg_drop_in)
3189 return edit_files_add(context, new_path ?: path, path, NULL);
3190
3191 r = get_dropin_by_name(arg_drop_in, dropins, &old_dropin);
3192 if (r < 0)
3193 return log_error_errno(r, "Failed to acquire drop-in '%s': %m", arg_drop_in);
3194
3195 if (r > 0 && !path_startswith(old_dropin, "/usr"))
3196 /* An existing drop-in is found and not in /usr/. Let's edit it directly. */
3197 dropin_path = TAKE_PTR(old_dropin);
3198 else {
3199 /* No drop-in was found or an existing drop-in resides in /usr/. Let's create
3200 * a new drop-in file. */
3201 dropin_path = strjoin(new_path ?: path, ".d/", arg_drop_in);
3202 if (!dropin_path)
3203 return log_oom();
3204 }
3205
3206 comment_paths = strv_new(path);
3207 if (!comment_paths)
3208 return log_oom();
3209
3210 r = strv_extend_strv(&comment_paths, dropins, /* filter_duplicates = */ false);
3211 if (r < 0)
3212 return log_oom();
3213
3214 return edit_files_add(context, dropin_path, old_dropin, comment_paths);
3215 }
3216
3217 static int udevd_reload(sd_bus *bus) {
3218 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3219 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3220 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
3221 const char *job_path;
3222 int r;
3223
3224 assert(bus);
3225
3226 r = bus_wait_for_jobs_new(bus, &w);
3227 if (r < 0)
3228 return log_error_errno(r, "Could not watch jobs: %m");
3229
3230 r = bus_call_method(bus,
3231 bus_systemd_mgr,
3232 "ReloadUnit",
3233 &error,
3234 &reply,
3235 "ss",
3236 "systemd-udevd.service",
3237 "replace");
3238 if (r < 0)
3239 return log_error_errno(r, "Failed to reload systemd-udevd: %s", bus_error_message(&error, r));
3240
3241 r = sd_bus_message_read(reply, "o", &job_path);
3242 if (r < 0)
3243 return bus_log_parse_error(r);
3244
3245 r = bus_wait_for_jobs_one(w, job_path, /* quiet = */ true, NULL);
3246 if (r == -ENOEXEC) {
3247 log_debug("systemd-udevd is not running, skipping reload.");
3248 return 0;
3249 }
3250 if (r < 0)
3251 return log_error_errno(r, "Failed to reload systemd-udevd: %m");
3252
3253 return 1;
3254 }
3255
3256 static int verb_edit(int argc, char *argv[], void *userdata) {
3257 _cleanup_(edit_file_context_done) EditFileContext context = {
3258 .marker_start = DROPIN_MARKER_START,
3259 .marker_end = DROPIN_MARKER_END,
3260 .remove_parent = !!arg_drop_in,
3261 };
3262 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
3263 ReloadFlags reload = 0;
3264 int r;
3265
3266 if (!on_tty())
3267 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit network config files if not on a tty.");
3268
3269 r = mac_selinux_init();
3270 if (r < 0)
3271 return r;
3272
3273 STRV_FOREACH(name, strv_skip(argv, 1)) {
3274 _cleanup_strv_free_ char **dropins = NULL;
3275 _cleanup_free_ char *path = NULL;
3276 const char *link_config;
3277
3278 link_config = startswith(*name, "@");
3279 if (link_config) {
3280 ReloadFlags flags;
3281
3282 r = get_config_files_by_link_config(link_config, &rtnl, &path, &dropins, &flags);
3283 if (r < 0)
3284 return r;
3285
3286 reload |= flags;
3287
3288 r = add_config_to_edit(&context, path, dropins);
3289 if (r < 0)
3290 return r;
3291
3292 continue;
3293 }
3294
3295 if (ENDSWITH_SET(*name, ".network", ".netdev"))
3296 reload |= RELOAD_NETWORKD;
3297 else if (endswith(*name, ".link"))
3298 reload |= RELOAD_UDEVD;
3299 else
3300 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid network config name '%s'.", *name);
3301
3302 r = get_config_files_by_name(*name, &path, &dropins);
3303 if (r == -ENOENT) {
3304 if (arg_drop_in)
3305 return log_error_errno(r, "Cannot find network config '%s'.", *name);
3306
3307 log_debug("No existing network config '%s' found, creating a new file.", *name);
3308
3309 path = path_join(NETWORK_DIRS[0], *name);
3310 if (!path)
3311 return log_oom();
3312
3313 r = edit_files_add(&context, path, NULL, NULL);
3314 if (r < 0)
3315 return r;
3316 continue;
3317 }
3318 if (r < 0)
3319 return log_error_errno(r, "Failed to get the path of network config '%s': %m", *name);
3320
3321 r = add_config_to_edit(&context, path, dropins);
3322 if (r < 0)
3323 return r;
3324 }
3325
3326 r = do_edit_files_and_install(&context);
3327 if (r < 0)
3328 return r;
3329
3330 if (arg_no_reload)
3331 return 0;
3332
3333 if (!sd_booted() || running_in_chroot() > 0) {
3334 log_debug("System is not booted with systemd or is running in chroot, skipping reload.");
3335 return 0;
3336 }
3337
3338 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
3339
3340 r = sd_bus_open_system(&bus);
3341 if (r < 0)
3342 return log_error_errno(r, "Failed to connect to system bus: %m");
3343
3344 if (FLAGS_SET(reload, RELOAD_UDEVD)) {
3345 r = udevd_reload(bus);
3346 if (r < 0)
3347 return r;
3348 }
3349
3350 if (FLAGS_SET(reload, RELOAD_NETWORKD)) {
3351 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3352
3353 if (!networkd_is_running()) {
3354 log_debug("systemd-networkd is not running, skipping reload.");
3355 return 0;
3356 }
3357
3358 r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
3359 if (r < 0)
3360 return log_error_errno(r, "Failed to reload systemd-networkd: %s", bus_error_message(&error, r));
3361 }
3362
3363 return 0;
3364 }
3365
3366 static int verb_cat(int argc, char *argv[], void *userdata) {
3367 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
3368 int r, ret = 0;
3369
3370 pager_open(arg_pager_flags);
3371
3372 STRV_FOREACH(name, strv_skip(argv, 1)) {
3373 _cleanup_strv_free_ char **dropins = NULL;
3374 _cleanup_free_ char *path = NULL;
3375 const char *link_config;
3376
3377 link_config = startswith(*name, "@");
3378 if (link_config) {
3379 r = get_config_files_by_link_config(link_config, &rtnl, &path, &dropins, /* ret_reload = */ NULL);
3380 if (r < 0)
3381 return ret < 0 ? ret : r;
3382 } else {
3383 r = get_config_files_by_name(*name, &path, &dropins);
3384 if (r == -ENOENT) {
3385 log_error_errno(r, "Cannot find network config file '%s'.", *name);
3386 ret = ret < 0 ? ret : r;
3387 continue;
3388 }
3389 if (r < 0) {
3390 log_error_errno(r, "Failed to get the path of network config '%s': %m", *name);
3391 return ret < 0 ? ret : r;
3392 }
3393 }
3394
3395 r = cat_files(path, dropins, /* flags = */ 0);
3396 if (r < 0)
3397 return ret < 0 ? ret : r;
3398 }
3399
3400 return ret;
3401 }
3402
3403 static int help(void) {
3404 _cleanup_free_ char *link = NULL;
3405 int r;
3406
3407 r = terminal_urlify_man("networkctl", "1", &link);
3408 if (r < 0)
3409 return log_oom();
3410
3411 printf("%s [OPTIONS...] COMMAND\n\n"
3412 "%sQuery and control the networking subsystem.%s\n"
3413 "\nCommands:\n"
3414 " list [PATTERN...] List links\n"
3415 " status [PATTERN...] Show link status\n"
3416 " lldp [PATTERN...] Show LLDP neighbors\n"
3417 " label Show current address label entries in the kernel\n"
3418 " delete DEVICES... Delete virtual netdevs\n"
3419 " up DEVICES... Bring devices up\n"
3420 " down DEVICES... Bring devices down\n"
3421 " renew DEVICES... Renew dynamic configurations\n"
3422 " forcerenew DEVICES... Trigger DHCP reconfiguration of all connected clients\n"
3423 " reconfigure DEVICES... Reconfigure interfaces\n"
3424 " reload Reload .network and .netdev files\n"
3425 " edit FILES|DEVICES... Edit network configuration files\n"
3426 " cat FILES|DEVICES... Show network configuration files\n"
3427 "\nOptions:\n"
3428 " -h --help Show this help\n"
3429 " --version Show package version\n"
3430 " --no-pager Do not pipe output into a pager\n"
3431 " --no-legend Do not show the headers and footers\n"
3432 " -a --all Show status for all links\n"
3433 " -s --stats Show detailed link statistics\n"
3434 " -l --full Do not ellipsize output\n"
3435 " -n --lines=INTEGER Number of journal entries to show\n"
3436 " --json=pretty|short|off\n"
3437 " Generate JSON output\n"
3438 " --no-reload Do not reload systemd-networkd or systemd-udevd\n"
3439 " after editing network config\n"
3440 " --drop-in=NAME Edit specified drop-in instead of main config file\n"
3441 "\nSee the %s for details.\n",
3442 program_invocation_short_name,
3443 ansi_highlight(),
3444 ansi_normal(),
3445 link);
3446
3447 return 0;
3448 }
3449
3450 static int parse_argv(int argc, char *argv[]) {
3451 enum {
3452 ARG_VERSION = 0x100,
3453 ARG_NO_PAGER,
3454 ARG_NO_LEGEND,
3455 ARG_JSON,
3456 ARG_NO_RELOAD,
3457 ARG_DROP_IN,
3458 };
3459
3460 static const struct option options[] = {
3461 { "help", no_argument, NULL, 'h' },
3462 { "version", no_argument, NULL, ARG_VERSION },
3463 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
3464 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
3465 { "all", no_argument, NULL, 'a' },
3466 { "stats", no_argument, NULL, 's' },
3467 { "full", no_argument, NULL, 'l' },
3468 { "lines", required_argument, NULL, 'n' },
3469 { "json", required_argument, NULL, ARG_JSON },
3470 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3471 { "drop-in", required_argument, NULL, ARG_DROP_IN },
3472 {}
3473 };
3474
3475 int c, r;
3476
3477 assert(argc >= 0);
3478 assert(argv);
3479
3480 while ((c = getopt_long(argc, argv, "hasln:", options, NULL)) >= 0) {
3481
3482 switch (c) {
3483
3484 case 'h':
3485 return help();
3486
3487 case ARG_VERSION:
3488 return version();
3489
3490 case ARG_NO_PAGER:
3491 arg_pager_flags |= PAGER_DISABLE;
3492 break;
3493
3494 case ARG_NO_LEGEND:
3495 arg_legend = false;
3496 break;
3497
3498 case ARG_NO_RELOAD:
3499 arg_no_reload = true;
3500 break;
3501
3502 case ARG_DROP_IN:
3503 if (isempty(optarg))
3504 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty drop-in file name.");
3505
3506 if (!endswith(optarg, ".conf")) {
3507 char *conf;
3508
3509 conf = strjoin(optarg, ".conf");
3510 if (!conf)
3511 return log_oom();
3512
3513 free_and_replace(arg_drop_in, conf);
3514 } else {
3515 r = free_and_strdup(&arg_drop_in, optarg);
3516 if (r < 0)
3517 return log_oom();
3518 }
3519
3520 if (!filename_is_valid(arg_drop_in))
3521 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3522 "Invalid drop-in file name '%s'.", arg_drop_in);
3523
3524 break;
3525
3526 case 'a':
3527 arg_all = true;
3528 break;
3529
3530 case 's':
3531 arg_stats = true;
3532 break;
3533
3534 case 'l':
3535 arg_full = true;
3536 break;
3537
3538 case 'n':
3539 if (safe_atou(optarg, &arg_lines) < 0)
3540 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3541 "Failed to parse lines '%s'", optarg);
3542 break;
3543
3544 case ARG_JSON:
3545 r = parse_json_argument(optarg, &arg_json_format_flags);
3546 if (r <= 0)
3547 return r;
3548 break;
3549
3550 case '?':
3551 return -EINVAL;
3552
3553 default:
3554 assert_not_reached();
3555 }
3556 }
3557
3558 return 1;
3559 }
3560
3561 static int networkctl_main(int argc, char *argv[]) {
3562 static const Verb verbs[] = {
3563 { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_links },
3564 { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, link_status },
3565 { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status },
3566 { "label", 1, 1, 0, list_address_labels },
3567 { "delete", 2, VERB_ANY, 0, link_delete },
3568 { "up", 2, VERB_ANY, 0, link_up_down },
3569 { "down", 2, VERB_ANY, 0, link_up_down },
3570 { "renew", 2, VERB_ANY, VERB_ONLINE_ONLY, link_renew },
3571 { "forcerenew", 2, VERB_ANY, VERB_ONLINE_ONLY, link_force_renew },
3572 { "reconfigure", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_reconfigure },
3573 { "reload", 1, 1, VERB_ONLINE_ONLY, verb_reload },
3574 { "edit", 2, VERB_ANY, 0, verb_edit },
3575 { "cat", 2, VERB_ANY, 0, verb_cat },
3576 {}
3577 };
3578
3579 return dispatch_verb(argc, argv, verbs, NULL);
3580 }
3581
3582 static int run(int argc, char* argv[]) {
3583 int r;
3584
3585 log_setup();
3586
3587 r = parse_argv(argc, argv);
3588 if (r <= 0)
3589 return r;
3590
3591 return networkctl_main(argc, argv);
3592 }
3593
3594 DEFINE_MAIN_FUNCTION(run);