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