|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// Copyright (c) 2025 Christophe Fontaine |
| 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 <errno.h> |
| 17 | + |
| 18 | +static cmd_status_t ra_show(const struct gr_api_client *c, const struct ec_pnode *p) { |
| 19 | + struct libscols_table *table = scols_new_table(); |
| 20 | + struct gr_ip6_ra_show_resp *resp; |
| 21 | + struct gr_ip6_ra_show_req req; |
| 22 | + struct gr_iface iface; |
| 23 | + void *resp_ptr = NULL; |
| 24 | + |
| 25 | + if (!iface_from_name(c, arg_str(p, "IFACE"), &iface)) |
| 26 | + req.iface_id = iface.id; |
| 27 | + else |
| 28 | + req.iface_id = 0; |
| 29 | + |
| 30 | + if (gr_api_client_send_recv(c, GR_IP6_IFACE_RA_SHOW, sizeof(req), &req, &resp_ptr) < 0) |
| 31 | + return CMD_ERROR; |
| 32 | + resp = resp_ptr; |
| 33 | + |
| 34 | + scols_table_new_column(table, "IFACE", 0, 0); |
| 35 | + scols_table_new_column(table, "RA", 0, 0); |
| 36 | + scols_table_new_column(table, "interval", 0, 0); |
| 37 | + scols_table_new_column(table, "lifetime", 0, 0); |
| 38 | + scols_table_set_column_separator(table, " "); |
| 39 | + |
| 40 | + for (uint16_t i = 0; i < resp->n_ras; i++) { |
| 41 | + struct libscols_line *line = scols_table_new_line(table, NULL); |
| 42 | + if (iface_from_id(c, resp->ras[i].iface_id, &iface) == 0) |
| 43 | + scols_line_sprintf(line, 0, "%s", iface.name); |
| 44 | + else |
| 45 | + scols_line_sprintf(line, 0, "%u", resp->ras[i].iface_id); |
| 46 | + scols_line_sprintf(line, 1, "%u", resp->ras[i].enabled); |
| 47 | + scols_line_sprintf(line, 2, "%u", resp->ras[i].interval); |
| 48 | + scols_line_sprintf(line, 3, "%u", resp->ras[i].lifetime); |
| 49 | + } |
| 50 | + |
| 51 | + scols_print_table(table); |
| 52 | + scols_unref_table(table); |
| 53 | + free(resp_ptr); |
| 54 | + return CMD_SUCCESS; |
| 55 | +} |
| 56 | + |
| 57 | +static cmd_status_t ra_set(const struct gr_api_client *c, const struct ec_pnode *p) { |
| 58 | + struct gr_ip6_ra_set_req req = {0}; |
| 59 | + struct gr_iface iface; |
| 60 | + |
| 61 | + if (iface_from_name(c, arg_str(p, "IFACE"), &iface) < 0) |
| 62 | + return CMD_ERROR; |
| 63 | + |
| 64 | + req.iface_id = iface.id; |
| 65 | + if (!arg_u16(p, "IT", &req.interval)) |
| 66 | + req.set_interval = 1; |
| 67 | + |
| 68 | + if (!arg_u16(p, "LT", &req.lifetime)) |
| 69 | + req.set_lifetime = 1; |
| 70 | + |
| 71 | + if (gr_api_client_send_recv(c, GR_IP6_IFACE_RA_SET, sizeof(req), &req, NULL) < 0) |
| 72 | + return CMD_ERROR; |
| 73 | + return CMD_SUCCESS; |
| 74 | +} |
| 75 | + |
| 76 | +static cmd_status_t ra_clear(const struct gr_api_client *c, const struct ec_pnode *p) { |
| 77 | + struct gr_ip6_ra_clear_req req; |
| 78 | + struct gr_iface iface; |
| 79 | + |
| 80 | + if (iface_from_name(c, arg_str(p, "IFACE"), &iface) < 0) |
| 81 | + return CMD_ERROR; |
| 82 | + |
| 83 | + req.iface_id = iface.id; |
| 84 | + if (gr_api_client_send_recv(c, GR_IP6_IFACE_RA_CLEAR, sizeof(req), &req, NULL) < 0) |
| 85 | + return CMD_ERROR; |
| 86 | + return CMD_SUCCESS; |
| 87 | +} |
| 88 | + |
| 89 | +static int ctx_init(struct ec_node *root) { |
| 90 | + int ret; |
| 91 | + |
| 92 | + ret = CLI_COMMAND( |
| 93 | + IP6_SHOW_CTX(root), |
| 94 | + "router-advert [IFACE]", |
| 95 | + ra_show, |
| 96 | + "Show router advertisement configuration", |
| 97 | + with_help("Interface name.", ec_node_dyn("IFACE", complete_iface_names, NULL)) |
| 98 | + ); |
| 99 | + if (ret < 0) |
| 100 | + return ret; |
| 101 | + |
| 102 | + ret = CLI_COMMAND( |
| 103 | + IP6_SET_CTX(root), |
| 104 | + "router-advert IFACE [interval IT] [lifetime LT]", |
| 105 | + ra_set, |
| 106 | + "Set router advertisement parameters", |
| 107 | + with_help("Interface name.", ec_node_dyn("IFACE", complete_iface_names, NULL)), |
| 108 | + with_help("Interval", ec_node_uint("IT", 0, UINT16_MAX - 1, 10)), |
| 109 | + with_help("Life time", ec_node_uint("LT", 0, UINT16_MAX - 1, 10)) |
| 110 | + ); |
| 111 | + if (ret < 0) |
| 112 | + return ret; |
| 113 | + |
| 114 | + ret = CLI_COMMAND( |
| 115 | + IP6_CLEAR_CTX(root), |
| 116 | + "router-advert IFACE", |
| 117 | + ra_clear, |
| 118 | + "Disable router advertisement and reset parameters", |
| 119 | + with_help("Interface name.", ec_node_dyn("IFACE", complete_iface_names, NULL)) |
| 120 | + ); |
| 121 | + if (ret < 0) |
| 122 | + return ret; |
| 123 | + |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +static struct gr_cli_context ctx = { |
| 128 | + .name = "ipv6 router-advert", |
| 129 | + .init = ctx_init, |
| 130 | +}; |
| 131 | + |
| 132 | +static void __attribute__((constructor, used)) init(void) { |
| 133 | + register_context(&ctx); |
| 134 | +} |
0 commit comments