| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_addrlabel.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "format-table.h" |
| 8 |
#include "in-addr-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "networkctl.h" |
| 11 |
#include "networkctl-address-label.h" |
| 12 |
|
| 13 |
static int dump_address_labels(sd_netlink *rtnl) { |
| 14 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
| 15 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 16 |
TableCell *cell; |
| 17 |
int r; |
| 18 |
|
| 19 |
assert(rtnl); |
| 20 |
|
| 21 |
r = sd_rtnl_message_new_addrlabel(rtnl, &req, RTM_GETADDRLABEL, 0, AF_INET6); |
| 22 |
if (r < 0) |
| 23 |
return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m"); |
| 24 |
|
| 25 |
r = sd_netlink_message_set_request_dump(req, true); |
| 26 |
if (r < 0) |
| 27 |
return r; |
| 28 |
|
| 29 |
r = sd_netlink_call(rtnl, req, 0, &reply); |
| 30 |
if (r < 0) |
| 31 |
return r; |
| 32 |
|
| 33 |
table = table_new("label", "prefix/prefixlen"); |
| 34 |
if (!table) |
| 35 |
return log_oom(); |
| 36 |
|
| 37 |
if (arg_full) |
| 38 |
table_set_width(table, 0); |
| 39 |
|
| 40 |
r = table_set_sort(table, (size_t) 0); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
assert_se(cell = table_get_cell(table, 0, 0)); |
| 45 |
(void) table_set_align_percent(table, cell, 100); |
| 46 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 47 |
|
| 48 |
assert_se(cell = table_get_cell(table, 0, 1)); |
| 49 |
(void) table_set_align_percent(table, cell, 100); |
| 50 |
|
| 51 |
for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) { |
| 52 |
struct in6_addr prefix; |
| 53 |
uint8_t prefixlen; |
| 54 |
uint32_t label; |
| 55 |
|
| 56 |
r = sd_netlink_message_get_errno(m); |
| 57 |
if (r < 0) { |
| 58 |
log_error_errno(r, "Failed to get netlink message, ignoring: %m"); |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
r = sd_netlink_message_read_u32(m, IFAL_LABEL, &label); |
| 63 |
if (r < 0 && r != -ENODATA) { |
| 64 |
log_error_errno(r, "Could not read IFAL_LABEL, ignoring: %m"); |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix); |
| 69 |
if (r < 0) |
| 70 |
continue; |
| 71 |
|
| 72 |
r = sd_rtnl_message_addrlabel_get_prefixlen(m, &prefixlen); |
| 73 |
if (r < 0) |
| 74 |
continue; |
| 75 |
|
| 76 |
r = table_add_cell(table, NULL, TABLE_UINT32, &label); |
| 77 |
if (r < 0) |
| 78 |
return table_log_add_error(r); |
| 79 |
|
| 80 |
r = table_add_cell_stringf(table, NULL, "%s/%u", IN6_ADDR_TO_STRING(&prefix), prefixlen); |
| 81 |
if (r < 0) |
| 82 |
return table_log_add_error(r); |
| 83 |
} |
| 84 |
|
| 85 |
return table_print_or_warn(table); |
| 86 |
} |
| 87 |
|
| 88 |
int verb_list_address_labels(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 89 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 90 |
int r; |
| 91 |
|
| 92 |
r = sd_netlink_open(&rtnl); |
| 93 |
if (r < 0) |
| 94 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 95 |
|
| 96 |
return dump_address_labels(rtnl); |
| 97 |
} |