1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) B.A.T.M.A.N. contributors:
*
* Marek Lindner <marek.lindner@mailbox.org>
*
* License-Filename: LICENSES/preferred/GPL-2.0
*/
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <net/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
#include <netlink/netlink.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include "main.h"
#include "sys.h"
#include "functions.h"
#define IFACE_STATUS_LEN 256
static void interface_usage(void)
{
fprintf(stderr, "Usage: batctl [options] interface [parameters] [add|del iface(s)]\n");
fprintf(stderr, " batctl [options] interface [parameters] create [routing_algo|ra RA_NAME]\n");
fprintf(stderr, " batctl [options] interface [parameters] destroy\n");
fprintf(stderr, "parameters:\n");
fprintf(stderr, " \t -M disable automatic creation of batman-adv interface\n");
fprintf(stderr, " \t -h print this help\n");
}
static int get_iface_status_netlink_parse(struct nl_msg *msg, void *arg)
{
struct nlattr *attrs[NUM_BATADV_ATTR];
struct nlmsghdr *nlh = nlmsg_hdr(msg);
char *iface_status = arg;
struct genlmsghdr *ghdr;
if (!genlmsg_valid_hdr(nlh, 0))
return NL_OK;
ghdr = nlmsg_data(nlh);
if (ghdr->cmd != BATADV_CMD_GET_HARDIF)
return NL_OK;
if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0),
genlmsg_len(ghdr), batadv_netlink_policy))
return NL_OK;
if (attrs[BATADV_ATTR_ACTIVE])
strncpy(iface_status, "active\n", IFACE_STATUS_LEN);
else
strncpy(iface_status, "inactive\n", IFACE_STATUS_LEN);
iface_status[IFACE_STATUS_LEN - 1] = '\0';
return NL_OK;
}
static char *get_iface_status_netlink(struct state *state, unsigned int hardif,
char *iface_status)
{
char *ret_status = NULL;
struct nl_msg *msg;
int ret;
iface_status[0] = '\0';
nl_cb_set(state->cb, NL_CB_VALID, NL_CB_CUSTOM, get_iface_status_netlink_parse,
iface_status);
msg = nlmsg_alloc();
if (!msg)
return NULL;
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, state->batadv_family,
0, 0, BATADV_CMD_GET_HARDIF, 1);
nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, state->mesh_ifindex);
nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, hardif);
ret = nl_send_auto_complete(state->sock, msg);
if (ret < 0)
goto err_free_msg;
ret = nl_recvmsgs(state->sock, state->cb);
if (ret < 0)
goto err_free_msg;
nl_wait_for_ack(state->sock);
if (strlen(iface_status) > 0)
ret_status = iface_status;
err_free_msg:
nlmsg_free(msg);
return ret_status;
}
static struct nla_policy link_policy[IFLA_MAX + 1] = {
[IFLA_IFNAME] = { .type = NLA_STRING, .maxlen = IFNAMSIZ },
[IFLA_MASTER] = { .type = NLA_U32 },
};
static int print_interfaces_rtnl_parse(struct nl_msg *msg, void *arg)
{
char iface_status[IFACE_STATUS_LEN];
struct nlattr *attrs[IFLA_MAX + 1];
struct state *state = arg;
struct ifinfomsg *ifm;
unsigned int master;
const char *status;
char *ifname;
int ret;
ifm = nlmsg_data(nlmsg_hdr(msg));
ret = nlmsg_parse(nlmsg_hdr(msg), sizeof(*ifm), attrs, IFLA_MAX,
link_policy);
if (ret < 0)
goto err;
if (!attrs[IFLA_IFNAME])
goto err;
if (!attrs[IFLA_MASTER])
goto err;
ifname = nla_get_string(attrs[IFLA_IFNAME]);
master = nla_get_u32(attrs[IFLA_MASTER]);
/* required on older kernels which don't prefilter the results */
if (master != state->mesh_ifindex)
goto err;
status = get_iface_status_netlink(state, ifm->ifi_index, iface_status);
if (!status)
status = "<error reading status>\n";
printf("%s: %s", ifname, status);
err:
return NL_OK;
}
static int print_interfaces(struct state *state)
{
int ret;
if (!file_exists(module_ver_path)) {
fprintf(stderr, "Error - batman-adv module has not been loaded\n");
return EXIT_FAILURE;
}
/* duplicated code here from the main() because interface doesn't always
* need COMMAND_FLAG_MESH_IFACE and COMMAND_FLAG_NETLINK
*/
if (check_mesh_iface(state))
return EXIT_FAILURE;
ret = netlink_create(state);
if (ret < 0)
return EXIT_FAILURE;
query_rtnl_link(state->mesh_ifindex, print_interfaces_rtnl_parse,
state);
netlink_destroy(state);
return EXIT_SUCCESS;
}
struct count_interfaces_rtnl_arg {
int ifindex;
unsigned int count;
};
static int count_interfaces_rtnl_parse(struct nl_msg *msg, void *arg)
{
struct count_interfaces_rtnl_arg *count_arg = arg;
struct nlattr *attrs[IFLA_MAX + 1];
struct ifinfomsg *ifm;
int ret;
int master;
ifm = nlmsg_data(nlmsg_hdr(msg));
ret = nlmsg_parse(nlmsg_hdr(msg), sizeof(*ifm), attrs, IFLA_MAX,
link_policy);
if (ret < 0)
goto err;
if (!attrs[IFLA_IFNAME])
goto err;
if (!attrs[IFLA_MASTER])
goto err;
master = nla_get_u32(attrs[IFLA_MASTER]);
/* required on older kernels which don't prefilter the results */
if (master != count_arg->ifindex)
goto err;
count_arg->count++;
err:
return NL_OK;
}
static unsigned int count_interfaces(char *mesh_iface)
{
struct count_interfaces_rtnl_arg count_arg;
count_arg.count = 0;
count_arg.ifindex = if_nametoindex(mesh_iface);
if (!count_arg.ifindex)
return 0;
query_rtnl_link(count_arg.ifindex, count_interfaces_rtnl_parse,
&count_arg);
return count_arg.count;
}
struct interface_create_params {
const char *routing_algo;
};
static int
interface_parse_create_params(int argc, char **argv,
struct interface_create_params *create_params)
{
int pos = 1;
while (pos < argc) {
if (strcmp(argv[pos], "routing_algo") == 0 ||
strcmp(argv[pos], "ra") == 0) {
pos++;
if (pos >= argc) {
fprintf(stderr,
"Error - missing parameter for 'routing_algo'\n");
return -EINVAL;
}
create_params->routing_algo = argv[pos];
pos++;
} else {
fprintf(stderr,
"Error - unknown parameter '%s'\n",
argv[pos]);
return -EINVAL;
}
}
return 0;
}
static int create_interface(const char *mesh_iface,
const struct interface_create_params *create_param)
{
struct ifinfomsg rt_hdr = {
.ifi_family = IFLA_UNSPEC,
};
struct nlattr *linkinfo;
struct nlattr *linkdata;
struct nl_msg *msg;
int err = 0;
int ret;
msg = nlmsg_alloc_simple(RTM_NEWLINK,
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK);
if (!msg)
return -ENOMEM;
ret = nlmsg_append(msg, &rt_hdr, sizeof(rt_hdr), NLMSG_ALIGNTO);
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
ret = nla_put_string(msg, IFLA_IFNAME, mesh_iface);
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
linkinfo = nla_nest_start(msg, IFLA_LINKINFO);
if (!linkinfo) {
err = -ENOMEM;
goto err_free_msg;
}
ret = nla_put_string(msg, IFLA_INFO_KIND, "batadv");
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
linkdata = nla_nest_start(msg, IFLA_INFO_DATA);
if (!linkdata) {
err = -ENOMEM;
goto err_free_msg;
}
if (create_param->routing_algo) {
ret = nla_put_string(msg, IFLA_BATADV_ALGO_NAME,
create_param->routing_algo);
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
}
nla_nest_end(msg, linkdata);
nla_nest_end(msg, linkinfo);
err = netlink_simple_request(msg);
err_free_msg:
nlmsg_free(msg);
return err;
}
static int destroy_interface(const char *mesh_iface)
{
struct ifinfomsg rt_hdr = {
.ifi_family = IFLA_UNSPEC,
};
struct nl_msg *msg;
int err = 0;
int ret;
msg = nlmsg_alloc_simple(RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK);
if (!msg)
return -ENOMEM;
ret = nlmsg_append(msg, &rt_hdr, sizeof(rt_hdr), NLMSG_ALIGNTO);
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
ret = nla_put_string(msg, IFLA_IFNAME, mesh_iface);
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
err = netlink_simple_request(msg);
err_free_msg:
nlmsg_free(msg);
return err;
}
static int set_master_interface(const char *iface, unsigned int ifmaster)
{
struct ifinfomsg rt_hdr = {
.ifi_family = IFLA_UNSPEC,
};
struct nl_msg *msg;
int err = 0;
int ret;
msg = nlmsg_alloc_simple(RTM_SETLINK, NLM_F_REQUEST | NLM_F_ACK);
if (!msg)
return -ENOMEM;
ret = nlmsg_append(msg, &rt_hdr, sizeof(rt_hdr), NLMSG_ALIGNTO);
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
ret = nla_put_string(msg, IFLA_IFNAME, iface);
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
ret = nla_put_u32(msg, IFLA_MASTER, ifmaster);
if (ret < 0) {
err = -ENOMEM;
goto err_free_msg;
}
err = netlink_simple_request(msg);
err_free_msg:
nlmsg_free(msg);
return err;
}
static int interface(struct state *state, int argc, char **argv)
{
struct interface_create_params create_params = {};
bool manual_mode = false;
unsigned int ifmaster;
unsigned int ifindex;
unsigned int pre_cnt;
const char *long_op;
unsigned int cnt;
char **rest_argv;
int rest_argc;
int optchar;
int ret;
int i;
while ((optchar = getopt(argc, argv, "hM")) != -1) {
switch (optchar) {
case 'h':
interface_usage();
return EXIT_SUCCESS;
case 'M':
manual_mode = true;
break;
default:
interface_usage();
return EXIT_FAILURE;
}
}
rest_argc = argc - optind;
rest_argv = &argv[optind];
if (rest_argc == 0)
return print_interfaces(state);
if ((strcmp(rest_argv[0], "add") != 0) && (strcmp(rest_argv[0], "a") != 0) &&
(strcmp(rest_argv[0], "del") != 0) && (strcmp(rest_argv[0], "d") != 0) &&
(strcmp(rest_argv[0], "create") != 0) && (strcmp(rest_argv[0], "c") != 0) &&
(strcmp(rest_argv[0], "destroy") != 0) && (strcmp(rest_argv[0], "D") != 0)) {
fprintf(stderr, "Error - unknown argument specified: %s\n", rest_argv[0]);
interface_usage();
goto err;
}
if (strcmp(rest_argv[0], "destroy") == 0)
rest_argv[0][0] = 'D';
switch (rest_argv[0][0]) {
case 'a':
case 'd':
if (rest_argc == 1) {
fprintf(stderr,
"Error - missing interface name(s) after '%s'\n",
rest_argv[0]);
interface_usage();
goto err;
}
break;
case 'D':
if (rest_argc != 1) {
fprintf(stderr,
"Error - extra parameter after '%s'\n",
rest_argv[0]);
interface_usage();
goto err;
}
break;
case 'c':
ret = interface_parse_create_params(rest_argc, rest_argv,
&create_params);
if (ret) {
interface_usage();
goto err;
}
default:
break;
}
switch (rest_argv[0][0]) {
case 'c':
ret = create_interface(state->mesh_iface, &create_params);
if (ret < 0) {
fprintf(stderr,
"Error - failed to add create batman-adv interface: %s\n",
strerror(-ret));
goto err;
}
return EXIT_SUCCESS;
case 'D':
ret = destroy_interface(state->mesh_iface);
if (ret < 0) {
fprintf(stderr,
"Error - failed to destroy batman-adv interface: %s\n",
strerror(-ret));
goto err;
}
return EXIT_SUCCESS;
default:
break;
}
/* get index of batman-adv interface - or try to create it */
ifmaster = if_nametoindex(state->mesh_iface);
if (!manual_mode && !ifmaster && rest_argv[0][0] == 'a') {
ret = create_interface(state->mesh_iface, &create_params);
if (ret < 0 && ret != -EEXIST) {
fprintf(stderr,
"Error - failed to create batman-adv interface: %s\n",
strerror(-ret));
goto err;
}
ifmaster = if_nametoindex(state->mesh_iface);
}
if (!ifmaster) {
ret = -ENODEV;
fprintf(stderr,
"Error - failed to find batman-adv interface: %s\n",
strerror(-ret));
goto err;
}
/* make sure that batman-adv is loaded or was loaded by create_interface */
if (!file_exists(module_ver_path)) {
fprintf(stderr, "Error - batman-adv module has not been loaded\n");
goto err;
}
pre_cnt = count_interfaces(state->mesh_iface);
for (i = 1; i < rest_argc; i++) {
ifindex = if_nametoindex(rest_argv[i]);
if (!ifindex) {
fprintf(stderr, "Error - interface does not exist: %s\n", rest_argv[i]);
continue;
}
if (rest_argv[0][0] == 'a')
ifindex = ifmaster;
else
ifindex = 0;
ret = set_master_interface(rest_argv[i], ifindex);
if (ret < 0) {
if (rest_argv[0][0] == 'a')
long_op = "add";
else
long_op = "delete";
fprintf(stderr, "Error - failed to %s interface %s: %s\n",
long_op, rest_argv[i], strerror(-ret));
goto err;
}
}
/* check if there is no interface left and then destroy mesh_iface */
if (!manual_mode && rest_argv[0][0] == 'd') {
cnt = count_interfaces(state->mesh_iface);
if (cnt == 0 && pre_cnt > 0)
fprintf(stderr,
"Warning: %s has no interfaces and can be destroyed with: batctl meshif %s interface destroy\n",
state->mesh_iface, state->mesh_iface);
}
return EXIT_SUCCESS;
err:
return EXIT_FAILURE;
}
COMMAND(SUBCOMMAND_MIF, interface, "if", 0, NULL,
"[add|del iface(s)]\tdisplay or modify the interface settings");
|