]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkctl.c
Merge pull request #12705 from keszybz/varlink-json-fix-and-two-cleanups
[thirdparty/systemd.git] / src / network / networkctl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
ee8c4568 2
ee8c4568 3#include <getopt.h>
d37b7627 4#include <linux/if_addrlabel.h>
1693a943 5#include <net/if.h>
3f6fd1ba 6#include <stdbool.h>
ca78ad1d
ZJS
7#include <sys/stat.h>
8#include <sys/types.h>
9#include <unistd.h>
ee8c4568 10
914d6c09 11#include "sd-device.h"
3f6fd1ba 12#include "sd-hwdb.h"
34437b4f 13#include "sd-lldp.h"
3f6fd1ba
LP
14#include "sd-netlink.h"
15#include "sd-network.h"
ee8c4568 16
b5efdb8a 17#include "alloc-util.h"
3f6fd1ba 18#include "arphrd-list.h"
914d6c09 19#include "device-util.h"
3f6fd1ba 20#include "ether-addr-util.h"
34437b4f 21#include "fd-util.h"
ff7c88a2 22#include "format-table.h"
518a66ec 23#include "format-util.h"
81fd1dd3 24#include "hwdb-util.h"
ee8c4568 25#include "local-addresses.h"
8752c575 26#include "locale-util.h"
d37b7627 27#include "macro.h"
4e2ca442 28#include "main-func.h"
3f6fd1ba
LP
29#include "netlink-util.h"
30#include "pager.h"
6bedfcbb 31#include "parse-util.h"
294bf0c3 32#include "pretty-print.h"
9cd8c766 33#include "set.h"
db73295a 34#include "socket-util.h"
760877e9 35#include "sort-util.h"
34437b4f 36#include "sparse-endian.h"
d054f0a4 37#include "stdio-util.h"
8b43440b 38#include "string-table.h"
8752c575 39#include "string-util.h"
3f6fd1ba 40#include "strv.h"
2388b2f4 41#include "strxcpyx.h"
288a74cc 42#include "terminal-util.h"
3f6fd1ba 43#include "verbs.h"
ee8c4568 44
0221d68a 45static PagerFlags arg_pager_flags = 0;
ee8c4568 46static bool arg_legend = true;
9085f64a 47static bool arg_all = false;
ee8c4568 48
b55822c3 49static char *link_get_type_string(unsigned short iftype, sd_device *d) {
2c740afd 50 const char *t, *devtype;
ee8c4568
LP
51 char *p;
52
2c740afd
YW
53 if (d &&
54 sd_device_get_devtype(d, &devtype) >= 0 &&
55 !isempty(devtype))
56 return strdup(devtype);
ee8c4568
LP
57
58 t = arphrd_to_name(iftype);
b55822c3
JD
59 if (!t)
60 return NULL;
ee8c4568
LP
61
62 p = strdup(t);
63 if (!p)
b55822c3 64 return NULL;
ee8c4568
LP
65
66 ascii_strlower(p);
b55822c3 67 return p;
ee8c4568
LP
68}
69
7e5a080a
LP
70static void operational_state_to_color(const char *state, const char **on, const char **off) {
71 assert(on);
72 assert(off);
73
85323805 74 if (STRPTR_IN_SET(state, "routable", "enslaved")) {
7e5a080a
LP
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
84static 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();
1cf03a4f 94 } else if (STRPTR_IN_SET(state, "failed", "linger")) {
7e5a080a
LP
95 *on = ansi_highlight_red();
96 *off = ansi_normal();
97 } else
98 *on = *off = "";
99}
100
6d0c65ff 101typedef struct LinkInfo {
e997c4b0 102 char name[IFNAMSIZ+1];
6d0c65ff 103 int ifindex;
1c4a6088 104 unsigned short iftype;
b147503e
LP
105 struct ether_addr mac_address;
106 uint32_t mtu;
2c73f59b
SS
107 uint32_t min_mtu;
108 uint32_t max_mtu;
0307afc6
SS
109 uint32_t tx_queues;
110 uint32_t rx_queues;
b147503e
LP
111
112 bool has_mac_address:1;
113 bool has_mtu:1;
0307afc6
SS
114 bool has_tx_queues:1;
115 bool has_rx_queues:1;
6d0c65ff
LP
116} LinkInfo;
117
93bab288
YW
118static int link_info_compare(const LinkInfo *a, const LinkInfo *b) {
119 return CMP(a->ifindex, b->ifindex);
6d0c65ff
LP
120}
121
a6962904 122static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns) {
e997c4b0 123 const char *name;
e997c4b0 124 uint16_t type;
a6962904 125 int ifindex, r;
6d0c65ff 126
e997c4b0
LP
127 assert(m);
128 assert(info);
6d0c65ff 129
e997c4b0
LP
130 r = sd_netlink_message_get_type(m, &type);
131 if (r < 0)
132 return r;
6d0c65ff 133
e997c4b0
LP
134 if (type != RTM_NEWLINK)
135 return 0;
6d0c65ff 136
a6962904 137 r = sd_rtnl_message_link_get_ifindex(m, &ifindex);
e997c4b0
LP
138 if (r < 0)
139 return r;
6d0c65ff 140
e997c4b0
LP
141 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &name);
142 if (r < 0)
143 return r;
6d0c65ff 144
a6962904
YW
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
b147503e 154 r = sd_rtnl_message_link_get_type(m, &info->iftype);
e997c4b0
LP
155 if (r < 0)
156 return r;
6d0c65ff 157
2388b2f4 158 strscpy(info->name, sizeof info->name, name);
a6962904 159 info->ifindex = ifindex;
b147503e
LP
160
161 info->has_mac_address =
162 sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &info->mac_address) >= 0 &&
72e551f4 163 memcmp(&info->mac_address, &ETHER_ADDR_NULL, sizeof(struct ether_addr)) != 0;
b147503e
LP
164
165 info->has_mtu =
4015d106 166 sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu) >= 0 &&
b147503e 167 info->mtu > 0;
e997c4b0 168
98d5bef3
YW
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 }
2c73f59b 173
0307afc6
SS
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
e997c4b0
LP
182 return 1;
183}
184
a6962904 185static int acquire_link_info(sd_netlink *rtnl, char **patterns, LinkInfo **ret) {
4afd3348 186 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
7e5a080a
LP
187 _cleanup_free_ LinkInfo *links = NULL;
188 size_t allocated = 0, c = 0;
189 sd_netlink_message *i;
7d367b45
LP
190 int r;
191
b147503e 192 assert(rtnl);
7d367b45 193 assert(ret);
ee8c4568 194
ee8c4568
LP
195 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
196 if (r < 0)
197 return rtnl_log_create_error(r);
198
1c4baffc 199 r = sd_netlink_message_request_dump(req, true);
ee8c4568
LP
200 if (r < 0)
201 return rtnl_log_create_error(r);
202
1c4baffc 203 r = sd_netlink_call(rtnl, req, 0, &reply);
f647962d
MS
204 if (r < 0)
205 return log_error_errno(r, "Failed to enumerate links: %m");
ee8c4568 206
7e5a080a
LP
207 for (i = reply; i; i = sd_netlink_message_next(i)) {
208 if (!GREEDY_REALLOC(links, allocated, c+1))
209 return -ENOMEM;
7d367b45 210
a6962904 211 r = decode_link(i, links + c, patterns);
7e5a080a
LP
212 if (r < 0)
213 return r;
214 if (r > 0)
215 c++;
216 }
217
93bab288 218 typesafe_qsort(links, c, link_info_compare);
7e5a080a 219
1cc6c93a 220 *ret = TAKE_PTR(links);
7e5a080a
LP
221
222 return (int) c;
7d367b45 223}
ee8c4568 224
7d367b45 225static int list_links(int argc, char *argv[], void *userdata) {
b147503e 226 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
7d367b45 227 _cleanup_free_ LinkInfo *links = NULL;
ff7c88a2
YW
228 _cleanup_(table_unrefp) Table *table = NULL;
229 TableCell *cell;
b147503e
LP
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");
7d367b45 235
a6962904 236 c = acquire_link_info(rtnl, argc > 1 ? argv + 1 : NULL, &links);
6d0c65ff 237 if (c < 0)
7d367b45
LP
238 return c;
239
0221d68a 240 (void) pager_open(arg_pager_flags);
7d367b45 241
ff7c88a2
YW
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);
6d0c65ff
LP
265
266 for (i = 0; i < c; i++) {
ab1525bc 267 _cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
4afd3348 268 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
d57c365b
LP
269 const char *on_color_operational, *off_color_operational,
270 *on_color_setup, *off_color_setup;
7d6884b6 271 char devid[2 + DECIMAL_STR_MAX(int)];
ee8c4568 272 _cleanup_free_ char *t = NULL;
ee8c4568 273
4abd866d 274 (void) sd_network_link_get_operational_state(links[i].ifindex, &operational_state);
d57c365b
LP
275 operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
276
33d5013d
LP
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");
d57c365b 280 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
ee8c4568 281
691d4da9
LP
282 xsprintf(devid, "n%i", links[i].ifindex);
283 (void) sd_device_new_from_device_id(&d, devid);
ee8c4568 284
b55822c3 285 t = link_get_type_string(links[i].iftype, d);
ee8c4568 286
ff7c88a2
YW
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);
ee8c4568
LP
308 }
309
ff7c88a2
YW
310 r = table_print(table, NULL);
311 if (r < 0)
312 return log_error_errno(r, "Failed to print table: %m");
313
ee8c4568 314 if (arg_legend)
6d0c65ff 315 printf("\n%i links listed.\n", c);
ee8c4568
LP
316
317 return 0;
318}
319
c09da729 320/* IEEE Organizationally Unique Identifier vendor string */
b147503e 321static int ieee_oui(sd_hwdb *hwdb, const struct ether_addr *mac, char **ret) {
81fd1dd3 322 const char *description;
fbd0b64f 323 char modalias[STRLEN("OUI:XXYYXXYYXXYY") + 1], *desc;
81fd1dd3
TG
324 int r;
325
326 assert(ret);
c09da729 327
888943fc
LP
328 if (!hwdb)
329 return -EINVAL;
330
81fd1dd3
TG
331 if (!mac)
332 return -EINVAL;
333
c09da729
TG
334 /* skip commonly misused 00:00:00 (Xerox) prefix */
335 if (memcmp(mac, "\0\0\0", 3) == 0)
336 return -EINVAL;
337
d054f0a4
DM
338 xsprintf(modalias, "OUI:" ETHER_ADDR_FORMAT_STR,
339 ETHER_ADDR_FORMAT_VAL(*mac));
c09da729 340
81fd1dd3
TG
341 r = sd_hwdb_get(hwdb, modalias, "ID_OUI_FROM_DATABASE", &description);
342 if (r < 0)
343 return r;
c09da729 344
81fd1dd3
TG
345 desc = strdup(description);
346 if (!desc)
347 return -ENOMEM;
c09da729 348
81fd1dd3
TG
349 *ret = desc;
350
351 return 0;
c09da729
TG
352}
353
69fb1176 354static int get_gateway_description(
1c4baffc 355 sd_netlink *rtnl,
81fd1dd3 356 sd_hwdb *hwdb,
69fb1176
LP
357 int ifindex,
358 int family,
359 union in_addr_union *gateway,
360 char **gateway_description) {
4afd3348 361 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c4baffc 362 sd_netlink_message *m;
c09da729
TG
363 int r;
364
365 assert(rtnl);
366 assert(ifindex >= 0);
4c701096 367 assert(IN_SET(family, AF_INET, AF_INET6));
c09da729
TG
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
1c4baffc 375 r = sd_netlink_message_request_dump(req, true);
c09da729
TG
376 if (r < 0)
377 return r;
378
1c4baffc 379 r = sd_netlink_call(rtnl, req, 0, &reply);
c09da729
TG
380 if (r < 0)
381 return r;
382
1c4baffc 383 for (m = reply; m; m = sd_netlink_message_next(m)) {
67a46833
YW
384 union in_addr_union gw = IN_ADDR_NULL;
385 struct ether_addr mac = ETHER_ADDR_NULL;
c09da729
TG
386 uint16_t type;
387 int ifi, fam;
388
1c4baffc 389 r = sd_netlink_message_get_errno(m);
c09da729
TG
390 if (r < 0) {
391 log_error_errno(r, "got error: %m");
392 continue;
393 }
394
1c4baffc 395 r = sd_netlink_message_get_type(m, &type);
c09da729
TG
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) {
144232a8 419 log_error_errno(r, "could not get ifindex: %m");
c09da729
TG
420 continue;
421 }
422
423 if (ifindex > 0 && ifi != ifindex)
424 continue;
425
426 switch (fam) {
427 case AF_INET:
1c4baffc 428 r = sd_netlink_message_read_in_addr(m, NDA_DST, &gw.in);
c09da729
TG
429 if (r < 0)
430 continue;
431
432 break;
433 case AF_INET6:
1c4baffc 434 r = sd_netlink_message_read_in6_addr(m, NDA_DST, &gw.in6);
c09da729
TG
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
1c4baffc 446 r = sd_netlink_message_read_ether_addr(m, NDA_LLADDR, &mac);
c09da729
TG
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
69fb1176 460static int dump_gateways(
1c4baffc 461 sd_netlink *rtnl,
81fd1dd3 462 sd_hwdb *hwdb,
98d5bef3 463 Table *table,
69fb1176 464 int ifindex) {
b6a3ca6d
TG
465 _cleanup_free_ struct local_address *local = NULL;
466 int r, n, i;
c09da729 467
837f57da 468 assert(rtnl);
98d5bef3 469 assert(table);
837f57da 470
b6a3ca6d
TG
471 n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local);
472 if (n < 0)
473 return n;
c09da729 474
b6a3ca6d 475 for (i = 0; i < n; i++) {
98d5bef3
YW
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;
c09da729 485
b6a3ca6d 486 r = in_addr_to_string(local[i].family, &local[i].address, &gateway);
c09da729 487 if (r < 0)
b6a3ca6d 488 return r;
c09da729 489
69fb1176 490 r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description);
c09da729 491 if (r < 0)
b6a3ca6d 492 log_debug_errno(r, "Could not get description of gateway: %m");
c09da729 493
98d5bef3
YW
494 if (description) {
495 with_description = strjoin(gateway, " (", description, ")");
496 if (!with_description)
497 return -ENOMEM;
498 }
1693a943
LP
499
500 /* Show interface name for the entry if we show
501 * entries for all interfaces */
502 if (ifindex <= 0) {
518a66ec 503 char name[IF_NAMESIZE+1];
98d5bef3 504
518a66ec 505 if (format_ifname(local[i].ifindex, name))
98d5bef3
YW
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;
c09da729
TG
513 }
514
515 return 0;
516}
517
69fb1176 518static int dump_addresses(
1c4baffc 519 sd_netlink *rtnl,
98d5bef3 520 Table *table,
69fb1176
LP
521 int ifindex) {
522
ee8c4568
LP
523 _cleanup_free_ struct local_address *local = NULL;
524 int r, n, i;
525
837f57da 526 assert(rtnl);
98d5bef3 527 assert(table);
837f57da 528
1d050e1e 529 n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local);
ee8c4568
LP
530 if (n < 0)
531 return n;
532
533 for (i = 0; i < n; i++) {
534 _cleanup_free_ char *pretty = NULL;
535
98d5bef3 536 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
ee8c4568
LP
537 if (r < 0)
538 return r;
539
98d5bef3
YW
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;
1693a943 543
98d5bef3
YW
544 r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
545 if (r < 0)
546 return r;
1693a943 547
98d5bef3 548 if (ifindex <= 0) {
518a66ec 549 char name[IF_NAMESIZE+1];
98d5bef3 550
518a66ec 551 if (format_ifname(local[i].ifindex, name))
98d5bef3
YW
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;
ee8c4568
LP
559 }
560
561 return 0;
562}
563
d37b7627
SS
564static int dump_address_labels(sd_netlink *rtnl) {
565 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
0232beed 566 _cleanup_(table_unrefp) Table *table = NULL;
d37b7627 567 sd_netlink_message *m;
0232beed 568 TableCell *cell;
d37b7627
SS
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
0232beed
YW
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);
d37b7627
SS
598
599 for (m = reply; m; m = sd_netlink_message_next(m)) {
600 _cleanup_free_ char *pretty = NULL;
67a46833 601 union in_addr_union prefix = IN_ADDR_NULL;
d37b7627
SS
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
0232beed
YW
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);
d37b7627
SS
638 }
639
0232beed 640 return table_print(table, NULL);
d37b7627
SS
641}
642
643static 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
837f57da
LP
656static 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
671static 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
d23c3e4c
FB
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
837f57da
LP
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
98d5bef3 704static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) {
837f57da
LP
705 _cleanup_fclose_ FILE *f = NULL;
706 int r, c = 0;
707
98d5bef3 708 assert(table);
837f57da
LP
709 assert(prefix);
710 assert(ifindex > 0);
711
712 r = open_lldp_neighbors(ifindex, &f);
98d5bef3
YW
713 if (r == -ENOENT)
714 return 0;
837f57da
LP
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;
98d5bef3 721 _cleanup_free_ char *str = NULL;
837f57da
LP
722
723 r = next_lldp_neighbor(f, &n);
724 if (r < 0)
725 return r;
726 if (r == 0)
727 break;
728
98d5bef3
YW
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;
837f57da
LP
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
98d5bef3
YW
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;
837f57da 747
98d5bef3
YW
748 r = table_add_cell(table, NULL, TABLE_STRING, str);
749 if (r < 0)
750 return r;
837f57da
LP
751
752 c++;
753 }
754
755 return c;
756}
757
98d5bef3 758static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) {
b295beea 759 unsigned c;
98d5bef3 760 int r;
b295beea
LP
761
762 assert(prefix);
763
764 if (!ifindexes || ifindexes[0] <= 0)
98d5bef3 765 return 0;
b295beea
LP
766
767 for (c = 0; ifindexes[c] > 0; c++) {
98d5bef3
YW
768 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
769 if (r < 0)
770 return r;
b295beea 771
98d5bef3
YW
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;
b295beea 775
98d5bef3
YW
776 r = table_add_cell(table, NULL, TABLE_IFINDEX, ifindexes + c);
777 if (r < 0)
778 return r;
b295beea 779 }
98d5bef3
YW
780
781 return 0;
b295beea
LP
782}
783
98d5bef3 784static int dump_list(Table *table, const char *prefix, char **l) {
ee8c4568 785 char **i;
98d5bef3 786 int r;
ee8c4568 787
dce83649 788 if (strv_isempty(l))
98d5bef3 789 return 0;
dce83649 790
ee8c4568 791 STRV_FOREACH(i, l) {
98d5bef3
YW
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;
ee8c4568 803 }
98d5bef3
YW
804
805 return 0;
ee8c4568
LP
806}
807
69fb1176 808static int link_status_one(
1c4baffc 809 sd_netlink *rtnl,
81fd1dd3 810 sd_hwdb *hwdb,
b147503e
LP
811 const LinkInfo *info) {
812
3df9bec5 813 _cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
64d6c229 814 _cleanup_free_ char *setup_state = NULL, *operational_state = NULL, *tz = NULL;
4afd3348 815 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
9085f64a 816 char devid[2 + DECIMAL_STR_MAX(int)];
373d9f17 817 _cleanup_free_ char *t = NULL, *network = NULL;
af5effc4 818 const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL;
d57c365b 819 const char *on_color_operational, *off_color_operational,
98d5bef3 820 *on_color_setup, *off_color_setup;
b295beea 821 _cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
98d5bef3
YW
822 _cleanup_(table_unrefp) Table *table = NULL;
823 TableCell *cell;
b147503e 824 int r;
9085f64a
LP
825
826 assert(rtnl);
b147503e 827 assert(info);
9085f64a 828
b147503e 829 (void) sd_network_link_get_operational_state(info->ifindex, &operational_state);
d57c365b
LP
830 operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
831
33d5013d
LP
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");
d57c365b 835 setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);
9085f64a 836
b147503e
LP
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);
9085f64a 841
691d4da9 842 xsprintf(devid, "n%i", info->ifindex);
914d6c09 843
dce83649 844 (void) sd_device_new_from_device_id(&d, devid);
914d6c09 845
9085f64a 846 if (d) {
dce83649
LP
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);
9085f64a 850
2c740afd 851 if (sd_device_get_property_value(d, "ID_VENDOR_FROM_DATABASE", &vendor) < 0)
dce83649 852 (void) sd_device_get_property_value(d, "ID_VENDOR", &vendor);
9085f64a 853
2c740afd 854 if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) < 0)
dce83649 855 (void) sd_device_get_property_value(d, "ID_MODEL", &model);
9085f64a
LP
856 }
857
b55822c3 858 t = link_get_type_string(info->iftype, d);
b1acce80 859
4abd866d 860 (void) sd_network_link_get_network_file(info->ifindex, &network);
df3fb561 861
4abd866d
LP
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);
0d4ad91d 864
98d5bef3
YW
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 }
9085f64a 969
b147503e 970 if (info->has_mac_address) {
888943fc 971 _cleanup_free_ char *description = NULL;
db73295a 972 char ea[ETHER_ADDR_TO_STRING_MAX];
888943fc 973
4abd866d 974 (void) ieee_oui(hwdb, &info->mac_address, &description);
888943fc 975
98d5bef3
YW
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;
db73295a 989 }
9085f64a 990
98d5bef3
YW
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 }
8eb9058d 1003
98d5bef3
YW
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 }
0d4ad91d 1015
98d5bef3
YW
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;
9085f64a 1040
b147503e 1041 (void) sd_network_link_get_timezone(info->ifindex, &tz);
98d5bef3
YW
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 }
8eb9058d 1053
98d5bef3
YW
1054 r = dump_lldp_neighbors(table, "Connected To:", info->ifindex);
1055 if (r < 0)
1056 return r;
837f57da 1057
98d5bef3 1058 return table_print(table, NULL);
9085f64a
LP
1059}
1060
0070333f
LP
1061static 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;
98d5bef3
YW
1065 _cleanup_(table_unrefp) Table *table = NULL;
1066 TableCell *cell;
1067 int r;
0070333f
LP
1068
1069 assert(rtnl);
1070
4abd866d 1071 (void) sd_network_get_operational_state(&operational_state);
0070333f
LP
1072 operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
1073
98d5bef3
YW
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);
0070333f 1094
98d5bef3
YW
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;
0070333f 1101
4abd866d 1102 (void) sd_network_get_dns(&dns);
98d5bef3
YW
1103 r = dump_list(table, "DNS:", dns);
1104 if (r < 0)
1105 return r;
0070333f 1106
4abd866d 1107 (void) sd_network_get_search_domains(&search_domains);
98d5bef3
YW
1108 r = dump_list(table, "Search Domains:", search_domains);
1109 if (r < 0)
1110 return r;
0070333f 1111
4abd866d 1112 (void) sd_network_get_route_domains(&route_domains);
98d5bef3
YW
1113 r = dump_list(table, "Route Domains:", route_domains);
1114 if (r < 0)
1115 return r;
0070333f 1116
4abd866d 1117 (void) sd_network_get_ntp(&ntp);
98d5bef3
YW
1118 r = dump_list(table, "NTP:", ntp);
1119 if (r < 0)
1120 return r;
0070333f 1121
98d5bef3 1122 return table_print(table, NULL);
0070333f
LP
1123}
1124
266b5389 1125static int link_status(int argc, char *argv[], void *userdata) {
4afd3348 1126 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
b147503e
LP
1127 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
1128 _cleanup_free_ LinkInfo *links = NULL;
1129 int r, c, i;
ee8c4568 1130
0221d68a 1131 (void) pager_open(arg_pager_flags);
0070333f 1132
1c4baffc 1133 r = sd_netlink_open(&rtnl);
f647962d
MS
1134 if (r < 0)
1135 return log_error_errno(r, "Failed to connect to netlink: %m");
f7d68aa8 1136
81fd1dd3
TG
1137 r = sd_hwdb_new(&hwdb);
1138 if (r < 0)
1139 log_debug_errno(r, "Failed to open hardware database: %m");
69fb1176 1140
b147503e 1141 if (arg_all)
a6962904 1142 c = acquire_link_info(rtnl, NULL, &links);
b147503e 1143 else if (argc <= 1)
0070333f 1144 return system_status(rtnl, hwdb);
b147503e 1145 else
a6962904 1146 c = acquire_link_info(rtnl, argv + 1, &links);
b147503e
LP
1147 if (c < 0)
1148 return c;
ee8c4568 1149
b147503e
LP
1150 for (i = 0; i < c; i++) {
1151 if (i > 0)
1152 fputc('\n', stdout);
ee8c4568 1153
b147503e 1154 link_status_one(rtnl, hwdb, links + i);
ee8c4568
LP
1155 }
1156
1157 return 0;
1158}
1159
34437b4f
LP
1160static 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;
49699bac 1166
34437b4f
LP
1167 ret = new(char, ELEMENTSOF(characters) + 1);
1168 if (!ret)
49699bac
SS
1169 return NULL;
1170
34437b4f
LP
1171 for (i = 0; i < ELEMENTSOF(characters); i++)
1172 ret[i] = (x & (1U << i)) ? characters[i] : '.';
49699bac 1173
34437b4f
LP
1174 ret[i] = 0;
1175 return ret;
49699bac
SS
1176}
1177
4c3160f1
ZJS
1178static void lldp_capabilities_legend(uint16_t x) {
1179 unsigned w, i, cols = columns();
404d53a9 1180 static const char* const table[] = {
4c3160f1
ZJS
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
49699bac 1210static int link_lldp_status(int argc, char *argv[], void *userdata) {
b147503e 1211 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
49699bac 1212 _cleanup_free_ LinkInfo *links = NULL;
658e9106 1213 _cleanup_(table_unrefp) Table *table = NULL;
b147503e 1214 int i, r, c, m = 0;
4c3160f1 1215 uint16_t all = 0;
658e9106 1216 TableCell *cell;
b147503e
LP
1217
1218 r = sd_netlink_open(&rtnl);
1219 if (r < 0)
1220 return log_error_errno(r, "Failed to connect to netlink: %m");
49699bac 1221
a6962904 1222 c = acquire_link_info(rtnl, argc > 1 ? argv + 1 : NULL, &links);
49699bac 1223 if (c < 0)
7d367b45
LP
1224 return c;
1225
0221d68a 1226 (void) pager_open(arg_pager_flags);
49699bac 1227
658e9106
YW
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);
49699bac 1256
b147503e 1257 for (i = 0; i < c; i++) {
34437b4f 1258 _cleanup_fclose_ FILE *f = NULL;
49699bac 1259
837f57da
LP
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);
34437b4f
LP
1265 continue;
1266 }
49699bac 1267
34437b4f 1268 for (;;) {
e2835111
YW
1269 _cleanup_free_ char *cid = NULL, *pid = NULL, *sname = NULL, *pdesc = NULL, *capabilities = NULL;
1270 const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL;
34437b4f 1271 _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
34437b4f 1272 uint16_t cc;
49699bac 1273
837f57da 1274 r = next_lldp_neighbor(f, &n);
34437b4f 1275 if (r < 0) {
837f57da 1276 log_warning_errno(r, "Failed to read neighbor data: %m");
34437b4f 1277 break;
49699bac 1278 }
837f57da
LP
1279 if (r == 0)
1280 break;
34437b4f
LP
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
d08191a2
LP
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
4c3160f1 1311 if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) {
34437b4f 1312 capabilities = lldp_capabilities_to_string(cc);
4c3160f1
ZJS
1313 all |= cc;
1314 }
34437b4f 1315
658e9106
YW
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;
b147503e
LP
1325
1326 m++;
49699bac
SS
1327 }
1328 }
1329
658e9106
YW
1330 r = table_print(table, NULL);
1331 if (r < 0)
1332 return r;
1333
4c3160f1
ZJS
1334 if (arg_legend) {
1335 lldp_capabilities_legend(all);
1336 printf("\n%i neighbors listed.\n", m);
1337 }
49699bac
SS
1338
1339 return 0;
1340}
1341
9cd8c766
SS
1342static 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
1359static 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;
9cd8c766
SS
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++) {
4bddccc6
YW
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]);
9cd8c766
SS
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) {
518a66ec
YW
1386 char ifname[IF_NAMESIZE + 1];
1387
1388 if (format_ifname(index, ifname))
9cd8c766
SS
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
37ec0fdd
LP
1398static 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
ee8c4568
LP
1406 printf("%s [OPTIONS...]\n\n"
1407 "Query and control the networking subsystem.\n\n"
1408 " -h --help Show this help\n"
9085f64a
LP
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"
ee8c4568 1413 "Commands:\n"
a6962904
YW
1414 " list [PATTERN...] List links\n"
1415 " status [PATTERN...] Show link status\n"
1416 " lldp [PATTERN...] Show LLDP neighbors\n"
d37b7627 1417 " label Show current address label entries in the kernel\n"
9cd8c766 1418 " delete DEVICES Delete virtual netdevs\n"
37ec0fdd
LP
1419 "\nSee the %s for details.\n"
1420 , program_invocation_short_name
1421 , link
1422 );
1423
1424 return 0;
ee8c4568
LP
1425}
1426
1427static 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 },
9085f64a 1440 { "all", no_argument, NULL, 'a' },
ee8c4568
LP
1441 {}
1442 };
1443
1444 int c;
1445
1446 assert(argc >= 0);
1447 assert(argv);
1448
9085f64a 1449 while ((c = getopt_long(argc, argv, "ha", options, NULL)) >= 0) {
ee8c4568
LP
1450
1451 switch (c) {
1452
1453 case 'h':
37ec0fdd 1454 return help();
ee8c4568
LP
1455
1456 case ARG_VERSION:
3f6fd1ba 1457 return version();
ee8c4568
LP
1458
1459 case ARG_NO_PAGER:
0221d68a 1460 arg_pager_flags |= PAGER_DISABLE;
ee8c4568
LP
1461 break;
1462
1463 case ARG_NO_LEGEND:
1464 arg_legend = false;
1465 break;
1466
9085f64a
LP
1467 case 'a':
1468 arg_all = true;
1469 break;
1470
ee8c4568
LP
1471 case '?':
1472 return -EINVAL;
1473
1474 default:
1475 assert_not_reached("Unhandled option");
1476 }
1477 }
1478
1479 return 1;
1480}
1481
1482static int networkctl_main(int argc, char *argv[]) {
15c3626e
YW
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 },
9cd8c766 1488 { "delete", 2, VERB_ANY, 0, link_delete },
266b5389 1489 {}
ee8c4568
LP
1490 };
1491
266b5389 1492 return dispatch_verb(argc, argv, verbs, NULL);
ee8c4568
LP
1493}
1494
58fb3678
LP
1495static 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
4e2ca442 1503static int run(int argc, char* argv[]) {
ee8c4568
LP
1504 int r;
1505
1a043959 1506 log_show_color(true);
ee8c4568
LP
1507 log_parse_environment();
1508 log_open();
1509
1510 r = parse_argv(argc, argv);
1511 if (r <= 0)
4e2ca442 1512 return r;
ee8c4568 1513
58fb3678
LP
1514 warn_networkd_missing();
1515
4e2ca442 1516 return networkctl_main(argc, argv);
ee8c4568 1517}
4e2ca442
ZJS
1518
1519DEFINE_MAIN_FUNCTION(run);