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