|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// Copyright (c) 2024 Robin Jarry |
| 3 | + |
| 4 | +#include "ip.h" |
| 5 | + |
| 6 | +#include <gr_api.h> |
| 7 | +#include <gr_cli.h> |
| 8 | +#include <gr_cli_iface.h> |
| 9 | +#include <gr_ip6.h> |
| 10 | +#include <gr_net_types.h> |
| 11 | +#include <gr_table.h> |
| 12 | + |
| 13 | +#include <ecoli.h> |
| 14 | +#include <libsmartcols.h> |
| 15 | + |
| 16 | +#include <stdint.h> |
| 17 | + |
| 18 | +static cmd_status_t addr_add(const struct gr_api_client *c, const struct ec_pnode *p) { |
| 19 | + struct gr_ip6_addr_add_req req = {.exist_ok = true}; |
| 20 | + struct gr_iface iface; |
| 21 | + |
| 22 | + if (ip6_net_parse(arg_str(p, "IP6_NET"), &req.addr.addr, false) < 0) |
| 23 | + return CMD_ERROR; |
| 24 | + if (iface_from_name(c, arg_str(p, "IFACE"), &iface) < 0) |
| 25 | + return CMD_ERROR; |
| 26 | + req.addr.iface_id = iface.id; |
| 27 | + |
| 28 | + if (gr_api_client_send_recv(c, GR_IP6_ADDR_ADD, sizeof(req), &req, NULL) < 0) |
| 29 | + return CMD_ERROR; |
| 30 | + |
| 31 | + return CMD_SUCCESS; |
| 32 | +} |
| 33 | + |
| 34 | +static cmd_status_t addr_del(const struct gr_api_client *c, const struct ec_pnode *p) { |
| 35 | + struct gr_ip6_addr_del_req req = {.missing_ok = true}; |
| 36 | + struct gr_iface iface; |
| 37 | + |
| 38 | + if (ip6_net_parse(arg_str(p, "IP6_NET"), &req.addr.addr, false) < 0) |
| 39 | + return CMD_ERROR; |
| 40 | + if (iface_from_name(c, arg_str(p, "IFACE"), &iface) < 0) |
| 41 | + return CMD_ERROR; |
| 42 | + req.addr.iface_id = iface.id; |
| 43 | + |
| 44 | + if (gr_api_client_send_recv(c, GR_IP6_ADDR_DEL, sizeof(req), &req, NULL) < 0) |
| 45 | + return CMD_ERROR; |
| 46 | + |
| 47 | + return CMD_SUCCESS; |
| 48 | +} |
| 49 | + |
| 50 | +static cmd_status_t addr_list(const struct gr_api_client *c, const struct ec_pnode *p) { |
| 51 | + struct libscols_table *table = scols_new_table(); |
| 52 | + const struct gr_ip6_addr_list_resp *resp; |
| 53 | + struct gr_ip6_addr_list_req req = {0}; |
| 54 | + struct gr_iface iface; |
| 55 | + |
| 56 | + void *resp_ptr = NULL; |
| 57 | + char buf[BUFSIZ]; |
| 58 | + |
| 59 | + (void)p; |
| 60 | + |
| 61 | + if (table == NULL) |
| 62 | + return CMD_ERROR; |
| 63 | + |
| 64 | + if (arg_u16(p, "VRF", &req.vrf_id) < 0 && errno != ENOENT) { |
| 65 | + scols_unref_table(table); |
| 66 | + return CMD_ERROR; |
| 67 | + } |
| 68 | + |
| 69 | + if (gr_api_client_send_recv(c, GR_IP6_ADDR_LIST, sizeof(req), &req, &resp_ptr) < 0) { |
| 70 | + scols_unref_table(table); |
| 71 | + return CMD_ERROR; |
| 72 | + } |
| 73 | + |
| 74 | + resp = resp_ptr; |
| 75 | + |
| 76 | + scols_table_new_column(table, "IFACE", 0, 0); |
| 77 | + scols_table_new_column(table, "ADDRESS", 0, 0); |
| 78 | + scols_table_set_column_separator(table, " "); |
| 79 | + |
| 80 | + for (size_t i = 0; i < resp->n_addrs; i++) { |
| 81 | + struct libscols_line *line = scols_table_new_line(table, NULL); |
| 82 | + const struct gr_ip6_ifaddr *addr = &resp->addrs[i]; |
| 83 | + ip6_net_format(&addr->addr, buf, sizeof(buf)); |
| 84 | + if (iface_from_id(c, addr->iface_id, &iface) == 0) |
| 85 | + scols_line_sprintf(line, 0, "%s", iface.name); |
| 86 | + else |
| 87 | + scols_line_sprintf(line, 0, "%u", addr->iface_id); |
| 88 | + scols_line_sprintf(line, 1, "%s", buf); |
| 89 | + } |
| 90 | + |
| 91 | + scols_print_table(table); |
| 92 | + scols_unref_table(table); |
| 93 | + free(resp_ptr); |
| 94 | + |
| 95 | + return CMD_SUCCESS; |
| 96 | +} |
| 97 | + |
| 98 | +static int ctx_init(struct ec_node *root) { |
| 99 | + int ret; |
| 100 | + |
| 101 | + ret = CLI_COMMAND( |
| 102 | + IP6_ADD_CTX(root), |
| 103 | + "address IP6_NET iface IFACE", |
| 104 | + addr_add, |
| 105 | + "Add an IPv6 address to an interface.", |
| 106 | + with_help("IPv6 address with prefix length.", ec_node_re("IP6_NET", IPV6_NET_RE)), |
| 107 | + with_help("Interface name.", ec_node_dyn("IFACE", complete_iface_names, NULL)) |
| 108 | + ); |
| 109 | + if (ret < 0) |
| 110 | + return ret; |
| 111 | + ret = CLI_COMMAND( |
| 112 | + IP6_DEL_CTX(root), |
| 113 | + "address IP6_NET iface IFACE", |
| 114 | + addr_del, |
| 115 | + "Remove an IPv6 address from an interface.", |
| 116 | + with_help("IPv6 address with prefix length.", ec_node_re("IP6_NET", IPV6_NET_RE)), |
| 117 | + with_help("Interface name.", ec_node_dyn("IFACE", complete_iface_names, NULL)) |
| 118 | + ); |
| 119 | + if (ret < 0) |
| 120 | + return ret; |
| 121 | + ret = CLI_COMMAND( |
| 122 | + IP6_SHOW_CTX(root), |
| 123 | + "address [vrf VRF]", |
| 124 | + addr_list, |
| 125 | + "Display all IPv6 addresses.", |
| 126 | + with_help("L3 addressing domain ID.", ec_node_uint("VRF", 0, UINT16_MAX - 1, 10)) |
| 127 | + ); |
| 128 | + if (ret < 0) |
| 129 | + return ret; |
| 130 | + |
| 131 | + return 0; |
| 132 | +} |
| 133 | + |
| 134 | +static struct gr_cli_context ctx = { |
| 135 | + .name = "ipv6 address", |
| 136 | + .init = ctx_init, |
| 137 | +}; |
| 138 | + |
| 139 | +static void __attribute__((constructor, used)) init(void) { |
| 140 | + register_context(&ctx); |
| 141 | +} |
0 commit comments