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