| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2009 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "nl-default.h" |
| 7 |
|
| 8 |
#include <linux/netlink.h> |
| 9 |
|
| 10 |
#include <netlink/cli/utils.h> |
| 11 |
#include <netlink/cli/neigh.h> |
| 12 |
#include <netlink/cli/link.h> |
| 13 |
|
| 14 |
static int quiet = 0; |
| 15 |
|
| 16 |
static void print_usage(void) |
| 17 |
{ |
| 18 |
printf( |
| 19 |
"Usage: nl-neigh-add [OPTION]... NEIGHBOUR\n" |
| 20 |
"\n" |
| 21 |
"Options\n" |
| 22 |
" --update-only Do not create neighbour, updates exclusively\n" |
| 23 |
" --create-only Do not update neighbour if it exists already.\n" |
| 24 |
" -q, --quiet Do not print informal notifications\n" |
| 25 |
" -h, --help Show this help\n" |
| 26 |
" -v, --version Show versioning information\n" |
| 27 |
"\n" |
| 28 |
"Neighbour Options\n" |
| 29 |
" -a, --addr=ADDR Destination address of neighbour\n" |
| 30 |
" -l, --lladdr=ADDR Link layer address of neighbour\n" |
| 31 |
" -d, --dev=DEV Device the neighbour is connected to\n" |
| 32 |
" --state=STATE Neighbour state, (default = permanent)\n" |
| 33 |
"\n" |
| 34 |
"Example\n" |
| 35 |
" nl-neigh-add --create-only --addr=10.0.0.1 --dev=eth0 \\\n" |
| 36 |
" --lladdr=AA:BB:CC:DD:EE:FF\n" |
| 37 |
); |
| 38 |
|
| 39 |
exit(0); |
| 40 |
} |
| 41 |
|
| 42 |
int main(int argc, char *argv[]) |
| 43 |
{ |
| 44 |
struct nl_sock *sock; |
| 45 |
struct rtnl_neigh *neigh; |
| 46 |
struct nl_cache *link_cache; |
| 47 |
struct nl_dump_params dp = { |
| 48 |
.dp_type = NL_DUMP_LINE, |
| 49 |
.dp_fd = stdout, |
| 50 |
}; |
| 51 |
int err, ok = 0, nlflags = NLM_F_REPLACE | NLM_F_CREATE; |
| 52 |
|
| 53 |
sock = nl_cli_alloc_socket(); |
| 54 |
nl_cli_connect(sock, NETLINK_ROUTE); |
| 55 |
link_cache = nl_cli_link_alloc_cache(sock); |
| 56 |
neigh = nl_cli_neigh_alloc(); |
| 57 |
|
| 58 |
for (;;) { |
| 59 |
int c, optidx = 0; |
| 60 |
enum { |
| 61 |
ARG_UPDATE_ONLY = 257, |
| 62 |
ARG_CREATE_ONLY = 258, |
| 63 |
ARG_STATE, |
| 64 |
}; |
| 65 |
static struct option long_opts[] = { |
| 66 |
{ "update-only", 0, 0, ARG_UPDATE_ONLY }, |
| 67 |
{ "create-only", 0, 0, ARG_CREATE_ONLY }, |
| 68 |
{ "quiet", 0, 0, 'q' }, |
| 69 |
{ "help", 0, 0, 'h' }, |
| 70 |
{ "version", 0, 0, 'v' }, |
| 71 |
{ "addr", 1, 0, 'a' }, |
| 72 |
{ "lladdr", 1, 0, 'l' }, |
| 73 |
{ "dev", 1, 0, 'd' }, |
| 74 |
{ "state", 1, 0, ARG_STATE }, |
| 75 |
{ 0, 0, 0, 0 } |
| 76 |
}; |
| 77 |
|
| 78 |
c = getopt_long(argc, argv, "qhva:l:d:", long_opts, &optidx); |
| 79 |
if (c == -1) |
| 80 |
break; |
| 81 |
|
| 82 |
switch (c) { |
| 83 |
case ARG_UPDATE_ONLY: nlflags &= ~NLM_F_CREATE; break; |
| 84 |
case ARG_CREATE_ONLY: nlflags |= NLM_F_EXCL; break; |
| 85 |
case 'q': quiet = 1; break; |
| 86 |
case 'h': print_usage(); break; |
| 87 |
case 'v': nl_cli_print_version(); break; |
| 88 |
case 'a': ok++; nl_cli_neigh_parse_dst(neigh, optarg); break; |
| 89 |
case 'l': nl_cli_neigh_parse_lladdr(neigh, optarg); break; |
| 90 |
case 'd': nl_cli_neigh_parse_dev(neigh, link_cache, optarg); break; |
| 91 |
case ARG_STATE: nl_cli_neigh_parse_state(neigh, optarg); break; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (!ok) |
| 96 |
print_usage(); |
| 97 |
|
| 98 |
if ((err = rtnl_neigh_add(sock, neigh, nlflags)) < 0) |
| 99 |
nl_cli_fatal(err, "Unable to add neighbour: %s", |
| 100 |
nl_geterror(err)); |
| 101 |
|
| 102 |
if (!quiet) { |
| 103 |
printf("Added "); |
| 104 |
nl_object_dump(OBJ_CAST(neigh), &dp); |
| 105 |
} |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |