]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkctl.c
hibernate-resume: add resumeflags= kernel option
[thirdparty/systemd.git] / src / network / networkctl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <getopt.h>
4 #include <linux/if_addrlabel.h>
5 #include <net/if.h>
6 #include <stdbool.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 #include "sd-bus.h"
12 #include "sd-device.h"
13 #include "sd-hwdb.h"
14 #include "sd-lldp.h"
15 #include "sd-netlink.h"
16 #include "sd-network.h"
17
18 #include "alloc-util.h"
19 #include "arphrd-list.h"
20 #include "bus-common-errors.h"
21 #include "bus-error.h"
22 #include "bus-util.h"
23 #include "device-util.h"
24 #include "ether-addr-util.h"
25 #include "fd-util.h"
26 #include "format-table.h"
27 #include "format-util.h"
28 #include "hwdb-util.h"
29 #include "local-addresses.h"
30 #include "locale-util.h"
31 #include "macro.h"
32 #include "main-func.h"
33 #include "netlink-util.h"
34 #include "pager.h"
35 #include "parse-util.h"
36 #include "pretty-print.h"
37 #include "set.h"
38 #include "socket-util.h"
39 #include "sort-util.h"
40 #include "sparse-endian.h"
41 #include "stdio-util.h"
42 #include "string-table.h"
43 #include "string-util.h"
44 #include "strv.h"
45 #include "strxcpyx.h"
46 #include "terminal-util.h"
47 #include "verbs.h"
48
49 static PagerFlags arg_pager_flags = 0;
50 static bool arg_legend = true;
51 static bool arg_all = false;
52 static bool arg_stats = false;
53
54 static char *link_get_type_string(unsigned short iftype, sd_device *d) {
55 const char *t, *devtype;
56 char *p;
57
58 if (d &&
59 sd_device_get_devtype(d, &devtype) >= 0 &&
60 !isempty(devtype))
61 return strdup(devtype);
62
63 t = arphrd_to_name(iftype);
64 if (!t)
65 return NULL;
66
67 p = strdup(t);
68 if (!p)
69 return NULL;
70
71 ascii_strlower(p);
72 return p;
73 }
74
75 static void operational_state_to_color(const char *state, const char **on, const char **off) {
76 assert(on);
77 assert(off);
78
79 if (STRPTR_IN_SET(state, "routable", "enslaved")) {
80 *on = ansi_highlight_green();
81 *off = ansi_normal();
82 } else if (streq_ptr(state, "degraded")) {
83 *on = ansi_highlight_yellow();
84 *off = ansi_normal();
85 } else
86 *on = *off = "";
87 }
88
89 static void setup_state_to_color(const char *state, const char **on, const char **off) {
90 assert(on);
91 assert(off);
92
93 if (streq_ptr(state, "configured")) {
94 *on = ansi_highlight_green();
95 *off = ansi_normal();
96 } else if (streq_ptr(state, "configuring")) {
97 *on = ansi_highlight_yellow();
98 *off = ansi_normal();
99 } else if (STRPTR_IN_SET(state, "failed", "linger")) {
100 *on = ansi_highlight_red();
101 *off = ansi_normal();
102 } else
103 *on = *off = "";
104 }
105
106 typedef struct LinkInfo {
107 char name[IFNAMSIZ+1];
108 int ifindex;
109 unsigned short iftype;
110 struct ether_addr mac_address;
111 uint32_t mtu;
112 uint32_t min_mtu;
113 uint32_t max_mtu;
114 uint32_t tx_queues;
115 uint32_t rx_queues;
116
117 union {
118 struct rtnl_link_stats64 stats64;
119 struct rtnl_link_stats stats;
120 };
121
122 double tx_bitrate;
123 double rx_bitrate;
124
125 bool has_mac_address:1;
126 bool has_tx_queues:1;
127 bool has_rx_queues:1;
128 bool has_stats64:1;
129 bool has_stats:1;
130 bool has_bitrates:1;
131 } LinkInfo;
132
133 static int link_info_compare(const LinkInfo *a, const LinkInfo *b) {
134 return CMP(a->ifindex, b->ifindex);
135 }
136
137 static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns) {
138 const char *name;
139 uint16_t type;
140 int ifindex, r;
141
142 assert(m);
143 assert(info);
144
145 r = sd_netlink_message_get_type(m, &type);
146 if (r < 0)
147 return r;
148
149 if (type != RTM_NEWLINK)
150 return 0;
151
152 r = sd_rtnl_message_link_get_ifindex(m, &ifindex);
153 if (r < 0)
154 return r;
155
156 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &name);
157 if (r < 0)
158 return r;
159
160 if (patterns) {
161 char str[DECIMAL_STR_MAX(int)];
162
163 xsprintf(str, "%i", ifindex);
164
165 if (!strv_fnmatch(patterns, str, 0) && !strv_fnmatch(patterns, name, 0))
166 return 0;
167 }
168
169 r = sd_rtnl_message_link_get_type(m, &info->iftype);
170 if (r < 0)
171 return r;
172
173 strscpy(info->name, sizeof info->name, name);
174 info->ifindex = ifindex;
175
176 info->has_mac_address =
177 sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &info->mac_address) >= 0 &&
178 memcmp(&info->mac_address, &ETHER_ADDR_NULL, sizeof(struct ether_addr)) != 0;
179
180 (void) sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu);
181 (void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu);
182 (void) sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu);
183
184 info->has_rx_queues =
185 sd_netlink_message_read_u32(m, IFLA_NUM_RX_QUEUES, &info->rx_queues) >= 0 &&
186 info->rx_queues > 0;
187
188 info->has_tx_queues =
189 sd_netlink_message_read_u32(m, IFLA_NUM_TX_QUEUES, &info->tx_queues) >= 0 &&
190 info->tx_queues > 0;
191
192 if (sd_netlink_message_read(m, IFLA_STATS64, sizeof info->stats64, &info->stats64) >= 0)
193 info->has_stats64 = true;
194 else if (sd_netlink_message_read(m, IFLA_STATS, sizeof info->stats, &info->stats) >= 0)
195 info->has_stats = true;
196
197 return 1;
198 }
199
200 static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) {
201 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
202 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
203 _cleanup_free_ char *path = NULL, *ifindex_str = NULL;
204 int r;
205
206 if (asprintf(&ifindex_str, "%i", link->ifindex) < 0)
207 return -ENOMEM;
208
209 r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex_str, &path);
210 if (r < 0)
211 return r;
212
213 r = sd_bus_call_method(
214 bus,
215 "org.freedesktop.network1",
216 path,
217 "org.freedesktop.DBus.Properties",
218 "Get",
219 &error,
220 &reply,
221 "ss",
222 "org.freedesktop.network1.Link",
223 "BitRates");
224 if (r < 0) {
225 bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY) ||
226 sd_bus_error_has_name(&error, BUS_ERROR_SPEED_METER_INACTIVE);
227
228 return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING,
229 r, "Failed to query link bit rates: %s", bus_error_message(&error, r));
230 }
231
232 r = sd_bus_message_enter_container(reply, 'v', "(dd)");
233 if (r < 0)
234 return bus_log_parse_error(r);
235
236 r = sd_bus_message_read(reply, "(dd)", &link->tx_bitrate, &link->rx_bitrate);
237 if (r < 0)
238 return bus_log_parse_error(r);
239
240 r = sd_bus_message_exit_container(reply);
241 if (r < 0)
242 return bus_log_parse_error(r);
243
244 link->has_bitrates = link->tx_bitrate >= 0 && link->rx_bitrate >= 0;
245
246 return 0;
247 }
248
249 static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, LinkInfo **ret) {
250 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
251 _cleanup_free_ LinkInfo *links = NULL;
252 size_t allocated = 0, c = 0, j;
253 sd_netlink_message *i;
254 int r;
255
256 assert(rtnl);
257 assert(ret);
258
259 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
260 if (r < 0)
261 return rtnl_log_create_error(r);
262
263 r = sd_netlink_message_request_dump(req, true);
264 if (r < 0)
265 return rtnl_log_create_error(r);
266
267 r = sd_netlink_call(rtnl, req, 0, &reply);
268 if (r < 0)
269 return log_error_errno(r, "Failed to enumerate links: %m");
270
271 for (i = reply; i; i = sd_netlink_message_next(i)) {
272 if (!GREEDY_REALLOC(links, allocated, c+1))
273 return -ENOMEM;
274
275 r = decode_link(i, links + c, patterns);
276 if (r < 0)
277 return r;
278 if (r > 0)
279 c++;
280 }
281
282 typesafe_qsort(links, c, link_info_compare);
283
284 if (bus)
285 for (j = 0; j < c; j++)
286 (void) acquire_link_bitrates(bus, links + j);
287
288 *ret = TAKE_PTR(links);
289
290 return (int) c;
291 }
292
293 static int list_links(int argc, char *argv[], void *userdata) {
294 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
295 _cleanup_free_ LinkInfo *links = NULL;
296 _cleanup_(table_unrefp) Table *table = NULL;
297 TableCell *cell;
298 int c, i, r;
299
300 r = sd_netlink_open(&rtnl);
301 if (r < 0)
302 return log_error_errno(r, "Failed to connect to netlink: %m");
303
304 c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
305 if (c < 0)
306 return c;
307
308 (void) pager_open(arg_pager_flags);
309
310 table = table_new("IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");
311 if (!table)
312 return log_oom();
313
314 table_set_header(table, arg_legend);
315
316 assert_se(cell = table_get_cell(table, 0, 0));
317 (void) table_set_minimum_width(table, cell, 3);
318 (void) table_set_weight(table, cell, 0);
319 (void) table_set_ellipsize_percent(table, cell, 100);
320 (void) table_set_align_percent(table, cell, 100);
321
322 assert_se(cell = table_get_cell(table, 0, 1));
323 (void) table_set_ellipsize_percent(table, cell, 100);
324
325 for (i = 0; i < c; i++) {
326 _cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
327 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
328 const char *on_color_operational, *off_color_operational,
329 *on_color_setup, *off_color_setup;
330 char devid[2 + DECIMAL_STR_MAX(int)];
331 _cleanup_free_ char *t = NULL;
332
333 (void) sd_network_link_get_operational_state(links[i].ifindex, &operational_state);
334 operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
335
336 r = sd_network_link_get_setup_state(links[i].ifindex, &setup_state);
337 if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */
338 setup_state = strdup("unmanaged");
339 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
340
341 xsprintf(devid, "n%i", links[i].ifindex);
342 (void) sd_device_new_from_device_id(&d, devid);
343
344 t = link_get_type_string(links[i].iftype, d);
345
346 r = table_add_cell(table, NULL, TABLE_INT, &links[i].ifindex);
347 if (r < 0)
348 return r;
349
350 r = table_add_many(table,
351 TABLE_STRING, links[i].name,
352 TABLE_STRING, strna(t));
353 if (r < 0)
354 return r;
355
356 r = table_add_cell(table, &cell, TABLE_STRING, strna(operational_state));
357 if (r < 0)
358 return r;
359
360 (void) table_set_color(table, cell, on_color_operational);
361
362 r = table_add_cell(table, &cell, TABLE_STRING, strna(setup_state));
363 if (r < 0)
364 return r;
365
366 (void) table_set_color(table, cell, on_color_setup);
367 }
368
369 r = table_print(table, NULL);
370 if (r < 0)
371 return log_error_errno(r, "Failed to print table: %m");
372
373 if (arg_legend)
374 printf("\n%i links listed.\n", c);
375
376 return 0;
377 }
378
379 /* IEEE Organizationally Unique Identifier vendor string */
380 static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) {
381 const char *description;
382 char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc;
383 int r;
384
385 assert(ret);
386
387 if (!hwdb)
388 return -EINVAL;
389
390 if (!mac)
391 return -EINVAL;
392
393 /* skip commonly misused 00:00:00 (Xerox) prefix */
394 if (memcmp(mac, "\0\0\0", 3) == 0)
395 return -EINVAL;
396
397 xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR,
398 ETHER_ADDR_FORMAT_VAL(*mac));
399
400 r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description);
401 if (r < 0)
402 return r;
403
404 desc = strdup(description);
405 if (!desc)
406 return -ENOMEM;
407
408 *ret = desc;
409
410 return 0;
411 }
412
413 static int get_gateway_description(
414 sd_netlink *rtnl,
415 sd_hwdb *hwdb,
416 int ifindex,
417 int family,
418 union in_addr_union *gateway,
419 char **gateway_description) {
420 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
421 sd_netlink_message *m;
422 int r;
423
424 assert(rtnl);
425 assert(ifindex >= 0);
426 assert(IN_SET(family, AF_INET, AF_INET6));
427 assert(gateway);
428 assert(gateway_description);
429
430 r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family);
431 if (r < 0)
432 return r;
433
434 r = sd_netlink_message_request_dump(req, true);
435 if (r < 0)
436 return r;
437
438 r = sd_netlink_call(rtnl, req, 0, &reply);
439 if (r < 0)
440 return r;
441
442 for (m = reply; m; m = sd_netlink_message_next(m)) {
443 union in_addr_union gw = IN_ADDR_NULL;
444 struct ether_addr mac = ETHER_ADDR_NULL;
445 uint16_t type;
446 int ifi, fam;
447
448 r = sd_netlink_message_get_errno(m);
449 if (r < 0) {
450 log_error_errno(r, "got error: %m");
451 continue;
452 }
453
454 r = sd_netlink_message_get_type(m, &type);
455 if (r < 0) {
456 log_error_errno(r, "could not get type: %m");
457 continue;
458 }
459
460 if (type != RTM_NEWNEIGH) {
461 log_error("type is not RTM_NEWNEIGH");
462 continue;
463 }
464
465 r = sd_rtnl_message_neigh_get_family(m, &fam);
466 if (r < 0) {
467 log_error_errno(r, "could not get family: %m");
468 continue;
469 }
470
471 if (fam != family) {
472 log_error("family is not correct");
473 continue;
474 }
475
476 r = sd_rtnl_message_neigh_get_ifindex(m, &ifi);
477 if (r < 0) {
478 log_error_errno(r, "could not get ifindex: %m");
479 continue;
480 }
481
482 if (ifindex > 0 && ifi != ifindex)
483 continue;
484
485 switch (fam) {
486 case AF_INET:
487 r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in);
488 if (r < 0)
489 continue;
490
491 break;
492 case AF_INET6:
493 r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6);
494 if (r < 0)
495 continue;
496
497 break;
498 default:
499 continue;
500 }
501
502 if (!in_addr_equal(fam, &gw, gateway))
503 continue;
504
505 r = sd_netlink_message_read_ether_addr(m, NDA_LLADDR, &mac);
506 if (r < 0)
507 continue;
508
509 r = ieee_oui(hwdb, &mac, gateway_description);
510 if (r < 0)
511 continue;
512
513 return 0;
514 }
515
516 return -ENODATA;
517 }
518
519 static int dump_gateways(
520 sd_netlink *rtnl,
521 sd_hwdb *hwdb,
522 Table *table,
523 int ifindex) {
524 _cleanup_free_ struct local_address *local = NULL;
525 int r, n, i;
526
527 assert(rtnl);
528 assert(table);
529
530 n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local);
531 if (n < 0)
532 return n;
533
534 for (i = 0; i < n; i++) {
535 _cleanup_free_ char *gateway = NULL, *description = NULL, *with_description = NULL;
536
537 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
538 if (r < 0)
539 return r;
540
541 r = table_add_cell(table, NULL, TABLE_STRING, i == 0 ? "Gateway:" : "");
542 if (r < 0)
543 return r;
544
545 r = in_addr_to_string(local[i].family, &local[i].address, &gateway);
546 if (r < 0)
547 return r;
548
549 r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description);
550 if (r < 0)
551 log_debug_errno(r, "Could not get description of gateway: %m");
552
553 if (description) {
554 with_description = strjoin(gateway, " (", description, ")");
555 if (!with_description)
556 return -ENOMEM;
557 }
558
559 /* Show interface name for the entry if we show
560 * entries for all interfaces */
561 if (ifindex <= 0) {
562 char name[IF_NAMESIZE+1];
563
564 if (format_ifname(local[i].ifindex, name))
565 r = table_add_cell_stringf(table, NULL, "%s on %s", with_description ?: gateway, name);
566 else
567 r = table_add_cell_stringf(table, NULL, "%s on %%%i", with_description ?: gateway, local[i].ifindex);
568 } else
569 r = table_add_cell(table, NULL, TABLE_STRING, with_description ?: gateway);
570 if (r < 0)
571 return r;
572 }
573
574 return 0;
575 }
576
577 static int dump_addresses(
578 sd_netlink *rtnl,
579 Table *table,
580 int ifindex) {
581
582 _cleanup_free_ struct local_address *local = NULL;
583 int r, n, i;
584
585 assert(rtnl);
586 assert(table);
587
588 n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local);
589 if (n < 0)
590 return n;
591
592 for (i = 0; i < n; i++) {
593 _cleanup_free_ char *pretty = NULL;
594
595 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
596 if (r < 0)
597 return r;
598
599 r = table_add_cell(table, NULL, TABLE_STRING, i == 0 ? "Address:" : "");
600 if (r < 0)
601 return r;
602
603 r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
604 if (r < 0)
605 return r;
606
607 if (ifindex <= 0) {
608 char name[IF_NAMESIZE+1];
609
610 if (format_ifname(local[i].ifindex, name))
611 r = table_add_cell_stringf(table, NULL, "%s on %s", pretty, name);
612 else
613 r = table_add_cell_stringf(table, NULL, "%s on %%%i", pretty, local[i].ifindex);
614 } else
615 r = table_add_cell(table, NULL, TABLE_STRING, pretty);
616 if (r < 0)
617 return r;
618 }
619
620 return 0;
621 }
622
623 static int dump_address_labels(sd_netlink *rtnl) {
624 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
625 _cleanup_(table_unrefp) Table *table = NULL;
626 sd_netlink_message *m;
627 TableCell *cell;
628 int r;
629
630 assert(rtnl);
631
632 r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6);
633 if (r < 0)
634 return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m");
635
636 r = sd_netlink_message_request_dump(req, true);
637 if (r < 0)
638 return r;
639
640 r = sd_netlink_call(rtnl, req, 0, &reply);
641 if (r < 0)
642 return r;
643
644 table = table_new("Label", "Prefix/Prefixlen");
645 if (!table)
646 return -ENOMEM;
647
648 r = table_set_sort(table, 0, SIZE_MAX);
649 if (r < 0)
650 return r;
651
652 assert_se(cell = table_get_cell(table, 0, 0));
653 (void) table_set_align_percent(table, cell, 100);
654 (void) table_set_ellipsize_percent(table, cell, 100);
655
656 assert_se(cell = table_get_cell(table, 0, 1));
657 (void) table_set_align_percent(table, cell, 100);
658
659 for (m = reply; m; m = sd_netlink_message_next(m)) {
660 _cleanup_free_ char *pretty = NULL;
661 union in_addr_union prefix = IN_ADDR_NULL;
662 uint8_t prefixlen;
663 uint32_t label;
664
665 r = sd_netlink_message_get_errno(m);
666 if (r < 0) {
667 log_error_errno(r, "got error: %m");
668 continue;
669 }
670
671 r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label);
672 if (r < 0 && r != -ENODATA) {
673 log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m");
674 continue;
675 }
676
677 r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix.in6);
678 if (r < 0)
679 continue;
680
681 r = in_addr_to_string(AF_INET6, &prefix, &pretty);
682 if (r < 0)
683 continue;
684
685 r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen);
686 if (r < 0)
687 continue;
688
689 r = table_add_cell(table, NULL, TABLE_UINT32, &label);
690 if (r < 0)
691 return r;
692
693 r = table_add_cell_stringf(table, &cell, "%s/%u", pretty, prefixlen);
694 if (r < 0)
695 return r;
696 }
697
698 return table_print(table, NULL);
699 }
700
701 static int list_address_labels(int argc, char *argv[], void *userdata) {
702 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
703 int r;
704
705 r = sd_netlink_open(&rtnl);
706 if (r < 0)
707 return log_error_errno(r, "Failed to connect to netlink: %m");
708
709 dump_address_labels(rtnl);
710
711 return 0;
712 }
713
714 static int open_lldp_neighbors(int ifindex, FILE **ret) {
715 _cleanup_free_ char *p = NULL;
716 FILE *f;
717
718 if (asprintf(&p, "/run/systemd/netif/lldp/%i", ifindex) < 0)
719 return -ENOMEM;
720
721 f = fopen(p, "re");
722 if (!f)
723 return -errno;
724
725 *ret = f;
726 return 0;
727 }
728
729 static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) {
730 _cleanup_free_ void *raw = NULL;
731 size_t l;
732 le64_t u;
733 int r;
734
735 assert(f);
736 assert(ret);
737
738 l = fread(&u, 1, sizeof(u), f);
739 if (l == 0 && feof(f))
740 return 0;
741 if (l != sizeof(u))
742 return -EBADMSG;
743
744 /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
745 if (le64toh(u) >= 4096)
746 return -EBADMSG;
747
748 raw = new(uint8_t, le64toh(u));
749 if (!raw)
750 return -ENOMEM;
751
752 if (fread(raw, 1, le64toh(u), f) != le64toh(u))
753 return -EBADMSG;
754
755 r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u));
756 if (r < 0)
757 return r;
758
759 return 1;
760 }
761
762 static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) {
763 _cleanup_fclose_ FILE *f = NULL;
764 int r, c = 0;
765
766 assert(table);
767 assert(prefix);
768 assert(ifindex > 0);
769
770 r = open_lldp_neighbors(ifindex, &f);
771 if (r == -ENOENT)
772 return 0;
773 if (r < 0)
774 return r;
775
776 for (;;) {
777 const char *system_name = NULL, *port_id = NULL, *port_description = NULL;
778 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
779 _cleanup_free_ char *str = NULL;
780
781 r = next_lldp_neighbor(f, &n);
782 if (r < 0)
783 return r;
784 if (r == 0)
785 break;
786
787 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
788 if (r < 0)
789 return r;
790
791 r = table_add_cell(table, NULL, TABLE_STRING, c == 0 ? prefix : "");
792 if (r < 0)
793 return r;
794
795 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
796 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
797 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
798
799 if (asprintf(&str, "%s on port %s%s%s%s",
800 strna(system_name), strna(port_id),
801 isempty(port_description) ? "" : " (",
802 port_description,
803 isempty(port_description) ? "" : ")") < 0)
804 return -ENOMEM;
805
806 r = table_add_cell(table, NULL, TABLE_STRING, str);
807 if (r < 0)
808 return r;
809
810 c++;
811 }
812
813 return c;
814 }
815
816 static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) {
817 unsigned c;
818 int r;
819
820 assert(prefix);
821
822 if (!ifindexes || ifindexes[0] <= 0)
823 return 0;
824
825 for (c = 0; ifindexes[c] > 0; c++) {
826 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
827 if (r < 0)
828 return r;
829
830 r = table_add_cell(table, NULL, TABLE_STRING, c == 0 ? prefix : "");
831 if (r < 0)
832 return r;
833
834 r = table_add_cell(table, NULL, TABLE_IFINDEX, ifindexes + c);
835 if (r < 0)
836 return r;
837 }
838
839 return 0;
840 }
841
842 static int dump_list(Table *table, const char *prefix, char **l) {
843 char **i;
844 int r;
845
846 if (strv_isempty(l))
847 return 0;
848
849 STRV_FOREACH(i, l) {
850 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
851 if (r < 0)
852 return r;
853
854 r = table_add_cell(table, NULL, TABLE_STRING, i == l ? prefix : "");
855 if (r < 0)
856 return r;
857
858 r = table_add_cell(table, NULL, TABLE_STRING, *i);
859 if (r < 0)
860 return r;
861 }
862
863 return 0;
864 }
865
866 #define DUMP_STATS_ONE(name, val_name) \
867 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL); \
868 if (r < 0) \
869 return r; \
870 r = table_add_cell(table, NULL, TABLE_STRING, name ":"); \
871 if (r < 0) \
872 return r; \
873 r = table_add_cell(table, NULL, info->has_stats64 ? TABLE_UINT64 : TABLE_UINT32, \
874 info->has_stats64 ? (void*) &info->stats64.val_name : (void*) &info->stats.val_name); \
875 if (r < 0) \
876 return r;
877
878 static int dump_statistics(Table *table, const LinkInfo *info) {
879 int r;
880
881 if (!arg_stats)
882 return 0;
883
884 if (!info->has_stats64 && !info->has_stats)
885 return 0;
886
887 DUMP_STATS_ONE("Rx Packets", rx_packets);
888 DUMP_STATS_ONE("Tx Packets", tx_packets);
889 DUMP_STATS_ONE("Rx Bytes", rx_bytes);
890 DUMP_STATS_ONE("Tx Bytes", tx_bytes);
891 DUMP_STATS_ONE("Rx Errors", rx_errors);
892 DUMP_STATS_ONE("Tx Errors", tx_errors);
893 DUMP_STATS_ONE("Rx Dropped", rx_dropped);
894 DUMP_STATS_ONE("Tx Dropped", tx_dropped);
895 DUMP_STATS_ONE("Multicast Packets", multicast);
896 DUMP_STATS_ONE("Collisions", collisions);
897
898 return 0;
899 }
900
901 static const struct {
902 double val;
903 const char *str;
904 } prefix_table[] = {
905 { .val = 1e15, .str = "P" },
906 { .val = 1e12, .str = "T" },
907 { .val = 1e9, .str = "G" },
908 { .val = 1e6, .str = "M" },
909 { .val = 1e3, .str = "k" },
910 };
911
912 static void get_prefix(double val, double *ret_div, const char **ret_prefix) {
913 assert(ret_div);
914 assert(ret_prefix);
915
916 for (size_t i = 0; i < ELEMENTSOF(prefix_table); i++)
917 if (val > prefix_table[i].val) {
918 *ret_div = prefix_table[i].val;
919 *ret_prefix = prefix_table[i].str;
920 return;
921 }
922
923 *ret_div = 1;
924 *ret_prefix = NULL;
925 }
926
927 static int link_status_one(
928 sd_netlink *rtnl,
929 sd_hwdb *hwdb,
930 const LinkInfo *info) {
931
932 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
933 _cleanup_free_ char *setup_state = NULL, *operational_state = NULL, *tz = NULL;
934 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
935 char devid[2 + DECIMAL_STR_MAX(int)];
936 _cleanup_free_ char *t = NULL, *network = NULL;
937 const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL;
938 const char *on_color_operational, *off_color_operational,
939 *on_color_setup, *off_color_setup;
940 _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
941 _cleanup_(table_unrefp) Table *table = NULL;
942 TableCell *cell;
943 int r;
944
945 assert(rtnl);
946 assert(info);
947
948 (void) sd_network_link_get_operational_state(info->ifindex, &operational_state);
949 operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
950
951 r = sd_network_link_get_setup_state(info->ifindex, &setup_state);
952 if (r == -ENODATA) /* If there's no info available about this iface, it's unmanaged by networkd */
953 setup_state = strdup("unmanaged");
954 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
955
956 (void) sd_network_link_get_dns(info->ifindex, &dns);
957 (void) sd_network_link_get_search_domains(info->ifindex, &search_domains);
958 (void) sd_network_link_get_route_domains(info->ifindex, &route_domains);
959 (void) sd_network_link_get_ntp(info->ifindex, &ntp);
960
961 xsprintf(devid, "n%i", info->ifindex);
962
963 (void) sd_device_new_from_device_id(&d, devid);
964
965 if (d) {
966 (void) sd_device_get_property_value(d, "ID_NET_LINK_FILE", &link);
967 (void) sd_device_get_property_value(d, "ID_NET_DRIVER", &driver);
968 (void) sd_device_get_property_value(d, "ID_PATH", &path);
969
970 if (sd_device_get_property_value(d, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
971 (void) sd_device_get_property_value(d, "ID_VENDOR", &vendor);
972
973 if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) < 0)
974 (void) sd_device_get_property_value(d, "ID_MODEL", &model);
975 }
976
977 t = link_get_type_string(info->iftype, d);
978
979 (void) sd_network_link_get_network_file(info->ifindex, &network);
980
981 (void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
982 (void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
983
984 table = table_new("DOT", "KEY", "VALUE");
985 if (!table)
986 return -ENOMEM;
987
988 assert_se(cell = table_get_cell(table, 0, 0));
989 (void) table_set_ellipsize_percent(table, cell, 100);
990
991 assert_se(cell = table_get_cell(table, 0, 1));
992 (void) table_set_ellipsize_percent(table, cell, 100);
993
994 table_set_header(table, false);
995
996 r = table_add_cell(table, &cell, TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE));
997 if (r < 0)
998 return r;
999 (void) table_set_color(table, cell, on_color_operational);
1000 r = table_add_cell_stringf(table, &cell, "%i: %s", info->ifindex, info->name);
1001 if (r < 0)
1002 return r;
1003 (void) table_set_align_percent(table, cell, 0);
1004 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1005 if (r < 0)
1006 return r;
1007
1008 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1009 if (r < 0)
1010 return r;
1011 r = table_add_cell(table, &cell, TABLE_STRING, "Link File:");
1012 if (r < 0)
1013 return r;
1014 (void) table_set_align_percent(table, cell, 100);
1015 r = table_add_cell(table, NULL, TABLE_STRING, strna(link));
1016 if (r < 0)
1017 return r;
1018
1019 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1020 if (r < 0)
1021 return r;
1022 r = table_add_cell(table, NULL, TABLE_STRING, "Network File:");
1023 if (r < 0)
1024 return r;
1025 r = table_add_cell(table, NULL, TABLE_STRING, strna(network));
1026 if (r < 0)
1027 return r;
1028
1029 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1030 if (r < 0)
1031 return r;
1032 r = table_add_cell(table, NULL, TABLE_STRING, "Type:");
1033 if (r < 0)
1034 return r;
1035 r = table_add_cell(table, NULL, TABLE_STRING, strna(t));
1036 if (r < 0)
1037 return r;
1038
1039 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1040 if (r < 0)
1041 return r;
1042 r = table_add_cell(table, NULL, TABLE_STRING, "State:");
1043 if (r < 0)
1044 return r;
1045 r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)",
1046 on_color_operational, strna(operational_state), off_color_operational,
1047 on_color_setup, strna(setup_state), off_color_setup);
1048 if (r < 0)
1049 return r;
1050
1051 if (path) {
1052 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1053 if (r < 0)
1054 return r;
1055 r = table_add_cell(table, NULL, TABLE_STRING, "Path:");
1056 if (r < 0)
1057 return r;
1058 r = table_add_cell(table, NULL, TABLE_STRING, path);
1059 if (r < 0)
1060 return r;
1061 }
1062 if (driver) {
1063 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1064 if (r < 0)
1065 return r;
1066 r = table_add_cell(table, NULL, TABLE_STRING, "Driver:");
1067 if (r < 0)
1068 return r;
1069 r = table_add_cell(table, NULL, TABLE_STRING, driver);
1070 if (r < 0)
1071 return r;
1072 }
1073 if (vendor) {
1074 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1075 if (r < 0)
1076 return r;
1077 r = table_add_cell(table, NULL, TABLE_STRING, "Vendor:");
1078 if (r < 0)
1079 return r;
1080 r = table_add_cell(table, NULL, TABLE_STRING, vendor);
1081 if (r < 0)
1082 return r;
1083 }
1084 if (model) {
1085 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1086 if (r < 0)
1087 return r;
1088 r = table_add_cell(table, NULL, TABLE_STRING, "Model:");
1089 if (r < 0)
1090 return r;
1091 r = table_add_cell(table, NULL, TABLE_STRING, model);
1092 if (r < 0)
1093 return r;
1094 }
1095
1096 if (info->has_mac_address) {
1097 _cleanup_free_ char *description = NULL;
1098 char ea[ETHER_ADDR_TO_STRING_MAX];
1099
1100 (void) ieee_oui(hwdb, &info->mac_address, &description);
1101
1102 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1103 if (r < 0)
1104 return r;
1105 r = table_add_cell(table, NULL, TABLE_STRING, "HW Address:");
1106 if (r < 0)
1107 return r;
1108 r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
1109 ether_addr_to_string(&info->mac_address, ea),
1110 description ? " (" : "",
1111 description,
1112 description ? ")" : "");
1113 if (r < 0)
1114 return r;
1115 }
1116
1117 if (info->mtu > 0) {
1118 char min_str[DECIMAL_STR_MAX(uint32_t)], max_str[DECIMAL_STR_MAX(uint32_t)];
1119
1120 xsprintf(min_str, "%" PRIu32, info->min_mtu);
1121 xsprintf(max_str, "%" PRIu32, info->max_mtu);
1122
1123 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1124 if (r < 0)
1125 return r;
1126 r = table_add_cell(table, NULL, TABLE_STRING, "MTU:");
1127 if (r < 0)
1128 return r;
1129 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "%s%s%s%s%s%s%s",
1130 info->mtu,
1131 info->min_mtu > 0 || info->max_mtu > 0 ? " (" : "",
1132 info->min_mtu > 0 ? "min: " : "",
1133 info->min_mtu > 0 ? min_str : "",
1134 info->min_mtu > 0 && info->max_mtu > 0 ? ", " : "",
1135 info->max_mtu > 0 ? "max: " : "",
1136 info->max_mtu > 0 ? max_str : "",
1137 info->min_mtu > 0 || info->max_mtu > 0 ? ")" : "");
1138 if (r < 0)
1139 return r;
1140 }
1141
1142 if (info->has_bitrates) {
1143 const char *tx_prefix, *rx_prefix;
1144 double tx_div, rx_div;
1145
1146 get_prefix(info->tx_bitrate, &tx_div, &tx_prefix);
1147 get_prefix(info->rx_bitrate, &rx_div, &rx_prefix);
1148
1149 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1150 if (r < 0)
1151 return r;
1152 r = table_add_cell(table, NULL, TABLE_STRING, "Bit Rate (Tx/Rx):");
1153 if (r < 0)
1154 return r;
1155
1156 r = table_add_cell_stringf(table, NULL, "%.4g %sbps/%.4g %sbps",
1157 info->tx_bitrate / tx_div, strempty(tx_prefix),
1158 info->rx_bitrate / rx_div, strempty(rx_prefix));
1159 if (r < 0)
1160 return r;
1161 }
1162
1163 if (info->has_tx_queues || info->has_rx_queues) {
1164 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1165 if (r < 0)
1166 return r;
1167 r = table_add_cell(table, NULL, TABLE_STRING, "Queue Length (Tx/Rx):");
1168 if (r < 0)
1169 return r;
1170 r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues);
1171 if (r < 0)
1172 return r;
1173 }
1174
1175 r = dump_addresses(rtnl, table, info->ifindex);
1176 if (r < 0)
1177 return r;
1178 r = dump_gateways(rtnl, hwdb, table, info->ifindex);
1179 if (r < 0)
1180 return r;
1181 r = dump_list(table, "DNS:", dns);
1182 if (r < 0)
1183 return r;
1184 r = dump_list(table, "Search Domains:", search_domains);
1185 if (r < 0)
1186 return r;
1187 r = dump_list(table, "Route Domains:", route_domains);
1188 if (r < 0)
1189 return r;
1190 r = dump_list(table, "NTP:", ntp);
1191 if (r < 0)
1192 return r;
1193 r = dump_ifindexes(table, "Carrier Bound To:", carrier_bound_to);
1194 if (r < 0)
1195 return r;
1196 r = dump_ifindexes(table, "Carrier Bound By:", carrier_bound_by);
1197 if (r < 0)
1198 return r;
1199
1200 (void) sd_network_link_get_timezone(info->ifindex, &tz);
1201 if (tz) {
1202 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
1203 if (r < 0)
1204 return r;
1205 r = table_add_cell(table, NULL, TABLE_STRING, "Time Zone:");
1206 if (r < 0)
1207 return r;
1208 r = table_add_cell(table, NULL, TABLE_STRING, tz);
1209 if (r < 0)
1210 return r;
1211 }
1212
1213 r = dump_lldp_neighbors(table, "Connected To:", info->ifindex);
1214 if (r < 0)
1215 return r;
1216
1217 r = dump_statistics(table, info);
1218 if (r < 0)
1219 return r;
1220
1221 return table_print(table, NULL);
1222 }
1223
1224 static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
1225 _cleanup_free_ char *operational_state = NULL;
1226 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
1227 const char *on_color_operational, *off_color_operational;
1228 _cleanup_(table_unrefp) Table *table = NULL;
1229 TableCell *cell;
1230 int r;
1231
1232 assert(rtnl);
1233
1234 (void) sd_network_get_operational_state(&operational_state);
1235 operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
1236
1237 table = table_new("DOT", "KEY", "VALUE");
1238 if (!table)
1239 return -ENOMEM;
1240
1241 assert_se(cell = table_get_cell(table, 0, 0));
1242 (void) table_set_ellipsize_percent(table, cell, 100);
1243
1244 assert_se(cell = table_get_cell(table, 0, 1));
1245 (void) table_set_align_percent(table, cell, 100);
1246 (void) table_set_ellipsize_percent(table, cell, 100);
1247
1248 table_set_header(table, false);
1249
1250 r = table_add_cell(table, &cell, TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE));
1251 if (r < 0)
1252 return r;
1253 (void) table_set_color(table, cell, on_color_operational);
1254
1255 r = table_add_cell(table, NULL, TABLE_STRING, "State:");
1256 if (r < 0)
1257 return r;
1258
1259 r = table_add_cell(table, &cell, TABLE_STRING, strna(operational_state));
1260 if (r < 0)
1261 return r;
1262 (void) table_set_color(table, cell, on_color_operational);
1263
1264 r = dump_addresses(rtnl, table, 0);
1265 if (r < 0)
1266 return r;
1267 r = dump_gateways(rtnl, hwdb, table, 0);
1268 if (r < 0)
1269 return r;
1270
1271 (void) sd_network_get_dns(&dns);
1272 r = dump_list(table, "DNS:", dns);
1273 if (r < 0)
1274 return r;
1275
1276 (void) sd_network_get_search_domains(&search_domains);
1277 r = dump_list(table, "Search Domains:", search_domains);
1278 if (r < 0)
1279 return r;
1280
1281 (void) sd_network_get_route_domains(&route_domains);
1282 r = dump_list(table, "Route Domains:", route_domains);
1283 if (r < 0)
1284 return r;
1285
1286 (void) sd_network_get_ntp(&ntp);
1287 r = dump_list(table, "NTP:", ntp);
1288 if (r < 0)
1289 return r;
1290
1291 return table_print(table, NULL);
1292 }
1293
1294 static int link_status(int argc, char *argv[], void *userdata) {
1295 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1296 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1297 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
1298 _cleanup_free_ LinkInfo *links = NULL;
1299 int r, c, i;
1300
1301 (void) pager_open(arg_pager_flags);
1302
1303 r = sd_bus_open_system(&bus);
1304 if (r < 0)
1305 return log_error_errno(r, "Failed to connect system bus: %m");
1306
1307 r = sd_netlink_open(&rtnl);
1308 if (r < 0)
1309 return log_error_errno(r, "Failed to connect to netlink: %m");
1310
1311 r = sd_hwdb_new(&hwdb);
1312 if (r < 0)
1313 log_debug_errno(r, "Failed to open hardware database: %m");
1314
1315 if (arg_all)
1316 c = acquire_link_info(bus, rtnl, NULL, &links);
1317 else if (argc <= 1)
1318 return system_status(rtnl, hwdb);
1319 else
1320 c = acquire_link_info(bus, rtnl, argv + 1, &links);
1321 if (c < 0)
1322 return c;
1323
1324 for (i = 0; i < c; i++) {
1325 if (i > 0)
1326 fputc('\n', stdout);
1327
1328 link_status_one(rtnl, hwdb, links + i);
1329 }
1330
1331 return 0;
1332 }
1333
1334 static char *lldp_capabilities_to_string(uint16_t x) {
1335 static const char characters[] = {
1336 'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm',
1337 };
1338 char *ret;
1339 unsigned i;
1340
1341 ret = new(char, ELEMENTSOF(characters) + 1);
1342 if (!ret)
1343 return NULL;
1344
1345 for (i = 0; i < ELEMENTSOF(characters); i++)
1346 ret[i] = (x & (1U << i)) ? characters[i] : '.';
1347
1348 ret[i] = 0;
1349 return ret;
1350 }
1351
1352 static void lldp_capabilities_legend(uint16_t x) {
1353 unsigned w, i, cols = columns();
1354 static const char* const table[] = {
1355 "o - Other",
1356 "p - Repeater",
1357 "b - Bridge",
1358 "w - WLAN Access Point",
1359 "r - Router",
1360 "t - Telephone",
1361 "d - DOCSIS cable device",
1362 "a - Station",
1363 "c - Customer VLAN",
1364 "s - Service VLAN",
1365 "m - Two-port MAC Relay (TPMR)",
1366 };
1367
1368 if (x == 0)
1369 return;
1370
1371 printf("\nCapability Flags:\n");
1372 for (w = 0, i = 0; i < ELEMENTSOF(table); i++)
1373 if (x & (1U << i) || arg_all) {
1374 bool newline;
1375
1376 newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols;
1377 if (newline)
1378 w = 0;
1379 w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]);
1380 }
1381 puts("");
1382 }
1383
1384 static int link_lldp_status(int argc, char *argv[], void *userdata) {
1385 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1386 _cleanup_free_ LinkInfo *links = NULL;
1387 _cleanup_(table_unrefp) Table *table = NULL;
1388 int i, r, c, m = 0;
1389 uint16_t all = 0;
1390 TableCell *cell;
1391
1392 r = sd_netlink_open(&rtnl);
1393 if (r < 0)
1394 return log_error_errno(r, "Failed to connect to netlink: %m");
1395
1396 c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
1397 if (c < 0)
1398 return c;
1399
1400 (void) pager_open(arg_pager_flags);
1401
1402 table = table_new("LINK",
1403 "CHASSIS ID",
1404 "SYSTEM NAME",
1405 "CAPS",
1406 "PORT ID",
1407 "PORT DESCRIPTION");
1408 if (!table)
1409 return -ENOMEM;
1410
1411 table_set_header(table, arg_legend);
1412
1413 assert_se(cell = table_get_cell(table, 0, 0));
1414 table_set_minimum_width(table, cell, 16);
1415
1416 assert_se(cell = table_get_cell(table, 0, 1));
1417 table_set_minimum_width(table, cell, 17);
1418
1419 assert_se(cell = table_get_cell(table, 0, 2));
1420 table_set_minimum_width(table, cell, 16);
1421
1422 assert_se(cell = table_get_cell(table, 0, 3));
1423 table_set_minimum_width(table, cell, 11);
1424
1425 assert_se(cell = table_get_cell(table, 0, 4));
1426 table_set_minimum_width(table, cell, 17);
1427
1428 assert_se(cell = table_get_cell(table, 0, 5));
1429 table_set_minimum_width(table, cell, 16);
1430
1431 for (i = 0; i < c; i++) {
1432 _cleanup_fclose_ FILE *f = NULL;
1433
1434 r = open_lldp_neighbors(links[i].ifindex, &f);
1435 if (r == -ENOENT)
1436 continue;
1437 if (r < 0) {
1438 log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", links[i].ifindex);
1439 continue;
1440 }
1441
1442 for (;;) {
1443 _cleanup_free_ char *cid = NULL, *pid = NULL, *sname = NULL, *pdesc = NULL, *capabilities = NULL;
1444 const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL;
1445 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
1446 uint16_t cc;
1447
1448 r = next_lldp_neighbor(f, &n);
1449 if (r < 0) {
1450 log_warning_errno(r, "Failed to read neighbor data: %m");
1451 break;
1452 }
1453 if (r == 0)
1454 break;
1455
1456 (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
1457 (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
1458 (void) sd_lldp_neighbor_get_system_name(n, &system_name);
1459 (void) sd_lldp_neighbor_get_port_description(n, &port_description);
1460
1461 if (chassis_id) {
1462 cid = ellipsize(chassis_id, 17, 100);
1463 if (cid)
1464 chassis_id = cid;
1465 }
1466
1467 if (port_id) {
1468 pid = ellipsize(port_id, 17, 100);
1469 if (pid)
1470 port_id = pid;
1471 }
1472
1473 if (system_name) {
1474 sname = ellipsize(system_name, 16, 100);
1475 if (sname)
1476 system_name = sname;
1477 }
1478
1479 if (port_description) {
1480 pdesc = ellipsize(port_description, 16, 100);
1481 if (pdesc)
1482 port_description = pdesc;
1483 }
1484
1485 if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) {
1486 capabilities = lldp_capabilities_to_string(cc);
1487 all |= cc;
1488 }
1489
1490 r = table_add_many(table,
1491 TABLE_STRING, links[i].name,
1492 TABLE_STRING, strna(chassis_id),
1493 TABLE_STRING, strna(system_name),
1494 TABLE_STRING, strna(capabilities),
1495 TABLE_STRING, strna(port_id),
1496 TABLE_STRING, strna(port_description));
1497 if (r < 0)
1498 return r;
1499
1500 m++;
1501 }
1502 }
1503
1504 r = table_print(table, NULL);
1505 if (r < 0)
1506 return r;
1507
1508 if (arg_legend) {
1509 lldp_capabilities_legend(all);
1510 printf("\n%i neighbors listed.\n", m);
1511 }
1512
1513 return 0;
1514 }
1515
1516 static int link_delete_send_message(sd_netlink *rtnl, int index) {
1517 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1518 int r;
1519
1520 assert(rtnl);
1521
1522 r = sd_rtnl_message_new_link(rtnl, &req, RTM_DELLINK, index);
1523 if (r < 0)
1524 return rtnl_log_create_error(r);
1525
1526 r = sd_netlink_call(rtnl, req, 0, NULL);
1527 if (r < 0)
1528 return r;
1529
1530 return 0;
1531 }
1532
1533 static int link_delete(int argc, char *argv[], void *userdata) {
1534 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
1535 _cleanup_set_free_ Set *indexes = NULL;
1536 int index, r, i;
1537 Iterator j;
1538 void *p;
1539
1540 r = sd_netlink_open(&rtnl);
1541 if (r < 0)
1542 return log_error_errno(r, "Failed to connect to netlink: %m");
1543
1544 indexes = set_new(NULL);
1545 if (!indexes)
1546 return log_oom();
1547
1548 for (i = 1; i < argc; i++) {
1549 r = parse_ifindex_or_ifname(argv[i], &index);
1550 if (r < 0)
1551 return log_error_errno(r, "Failed to resolve interface %s", argv[i]);
1552
1553 r = set_put(indexes, INT_TO_PTR(index));
1554 if (r < 0)
1555 return log_oom();
1556 }
1557
1558 SET_FOREACH(p, indexes, j) {
1559 r = link_delete_send_message(rtnl, PTR_TO_INT(p));
1560 if (r < 0) {
1561 char ifname[IF_NAMESIZE + 1];
1562
1563 if (format_ifname(index, ifname))
1564 return log_error_errno(r, "Failed to delete interface %s: %m", ifname);
1565 else
1566 return log_error_errno(r, "Failed to delete interface %d: %m", index);
1567 }
1568 }
1569
1570 return r;
1571 }
1572
1573 static int help(void) {
1574 _cleanup_free_ char *link = NULL;
1575 int r;
1576
1577 r = terminal_urlify_man("networkctl", "1", &link);
1578 if (r < 0)
1579 return log_oom();
1580
1581 printf("%s [OPTIONS...]\n\n"
1582 "Query and control the networking subsystem.\n\n"
1583 " -h --help Show this help\n"
1584 " --version Show package version\n"
1585 " --no-pager Do not pipe output into a pager\n"
1586 " --no-legend Do not show the headers and footers\n"
1587 " -a --all Show status for all links\n"
1588 " -s --stats Show detailed link statics\n"
1589 "\nCommands:\n"
1590 " list [PATTERN...] List links\n"
1591 " status [PATTERN...] Show link status\n"
1592 " lldp [PATTERN...] Show LLDP neighbors\n"
1593 " label Show current address label entries in the kernel\n"
1594 " delete DEVICES Delete virtual netdevs\n"
1595 "\nSee the %s for details.\n"
1596 , program_invocation_short_name
1597 , link
1598 );
1599
1600 return 0;
1601 }
1602
1603 static int parse_argv(int argc, char *argv[]) {
1604
1605 enum {
1606 ARG_VERSION = 0x100,
1607 ARG_NO_PAGER,
1608 ARG_NO_LEGEND,
1609 };
1610
1611 static const struct option options[] = {
1612 { "help", no_argument, NULL, 'h' },
1613 { "version", no_argument, NULL, ARG_VERSION },
1614 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1615 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1616 { "all", no_argument, NULL, 'a' },
1617 { "stats", no_argument, NULL, 's' },
1618 {}
1619 };
1620
1621 int c;
1622
1623 assert(argc >= 0);
1624 assert(argv);
1625
1626 while ((c = getopt_long(argc, argv, "has", options, NULL)) >= 0) {
1627
1628 switch (c) {
1629
1630 case 'h':
1631 return help();
1632
1633 case ARG_VERSION:
1634 return version();
1635
1636 case ARG_NO_PAGER:
1637 arg_pager_flags |= PAGER_DISABLE;
1638 break;
1639
1640 case ARG_NO_LEGEND:
1641 arg_legend = false;
1642 break;
1643
1644 case 'a':
1645 arg_all = true;
1646 break;
1647
1648 case 's':
1649 arg_stats = true;
1650 break;
1651
1652 case '?':
1653 return -EINVAL;
1654
1655 default:
1656 assert_not_reached("Unhandled option");
1657 }
1658 }
1659
1660 return 1;
1661 }
1662
1663 static int networkctl_main(int argc, char *argv[]) {
1664 static const Verb verbs[] = {
1665 { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, list_links },
1666 { "status", VERB_ANY, VERB_ANY, 0, link_status },
1667 { "lldp", VERB_ANY, VERB_ANY, 0, link_lldp_status },
1668 { "label", VERB_ANY, VERB_ANY, 0, list_address_labels },
1669 { "delete", 2, VERB_ANY, 0, link_delete },
1670 {}
1671 };
1672
1673 return dispatch_verb(argc, argv, verbs, NULL);
1674 }
1675
1676 static void warn_networkd_missing(void) {
1677
1678 if (access("/run/systemd/netif/state", F_OK) >= 0)
1679 return;
1680
1681 fprintf(stderr, "WARNING: systemd-networkd is not running, output will be incomplete.\n\n");
1682 }
1683
1684 static int run(int argc, char* argv[]) {
1685 int r;
1686
1687 log_show_color(true);
1688 log_parse_environment();
1689 log_open();
1690
1691 r = parse_argv(argc, argv);
1692 if (r <= 0)
1693 return r;
1694
1695 warn_networkd_missing();
1696
1697 return networkctl_main(argc, argv);
1698 }
1699
1700 DEFINE_MAIN_FUNCTION(run);