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