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, default_yes = 0, deleted = 0, interactive = 0;
15 static struct nl_sock *sock;
16
print_usage(void)17 static void print_usage(void)
18 {
19 printf(
20 "Usage: nl-neigh-delete [OPTION]... [NEIGHBOUR]\n"
21 "\n"
22 "Options\n"
23 " -i, --interactive Run interactively\n"
24 " --yes Set default answer to yes\n"
25 " -q, --quiet Do not print informal notifications\n"
26 " -h, --help Show this help\n"
27 " -v, --version Show versioning information\n"
28 "\n"
29 "Neighbour Options\n"
30 " -a, --addr=ADDR Destination address of neighbour\n"
31 " -l, --lladdr=ADDR Link layer address of neighbour\n"
32 " -d, --dev=DEV Device the neighbour is connected to\n"
33 " --family=FAMILY Destination address family\n"
34 " --state=STATE Neighbour state, (default = permanent)\n"
35 );
36
37 exit(0);
38 }
39
delete_cb(struct nl_object * obj,void * arg)40 static void delete_cb(struct nl_object *obj, void *arg)
41 {
42 struct rtnl_neigh *neigh = nl_object_priv(obj);
43 struct nl_dump_params params = {
44 .dp_type = NL_DUMP_LINE,
45 .dp_fd = stdout,
46 };
47 int err;
48
49 if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
50 return;
51
52 if ((err = rtnl_neigh_delete(sock, neigh, 0)) < 0)
53 nl_cli_fatal(err, "Unable to delete neighbour: %s\n",
54 nl_geterror(err));
55
56 if (!quiet) {
57 printf("Deleted ");
58 nl_object_dump(obj, ¶ms);
59 }
60
61 deleted++;
62 }
63
main(int argc,char * argv[])64 int main(int argc, char *argv[])
65 {
66 struct rtnl_neigh *neigh;
67 struct nl_cache *link_cache, *neigh_cache;
68
69 sock = nl_cli_alloc_socket();
70 nl_cli_connect(sock, NETLINK_ROUTE);
71 link_cache = nl_cli_link_alloc_cache(sock);
72 neigh_cache = nl_cli_neigh_alloc_cache(sock);
73 neigh = nl_cli_neigh_alloc();
74
75 for (;;) {
76 int c, optidx = 0;
77 enum {
78 ARG_FAMILY = 257,
79 ARG_STATE = 258,
80 ARG_YES,
81 };
82 static struct option long_opts[] = {
83 { "interactive", 0, 0, 'i' },
84 { "yes", 0, 0, ARG_YES },
85 { "quiet", 0, 0, 'q' },
86 { "help", 0, 0, 'h' },
87 { "version", 0, 0, 'v' },
88 { "addr", 1, 0, 'a' },
89 { "lladdr", 1, 0, 'l' },
90 { "dev", 1, 0, 'd' },
91 { "family", 1, 0, ARG_FAMILY },
92 { "state", 1, 0, ARG_STATE },
93 { 0, 0, 0, 0 }
94 };
95
96 c = getopt_long(argc, argv, "qhva:l:d:", long_opts, &optidx);
97 if (c == -1)
98 break;
99
100 switch (c) {
101 case 'i': interactive = 1; break;
102 case ARG_YES: default_yes = 1; break;
103 case 'q': quiet = 1; break;
104 case 'h': print_usage(); break;
105 case 'v': nl_cli_print_version(); break;
106 case 'a': nl_cli_neigh_parse_dst(neigh, optarg); break;
107 case 'l': nl_cli_neigh_parse_lladdr(neigh, optarg); break;
108 case 'd': nl_cli_neigh_parse_dev(neigh, link_cache, optarg); break;
109 case ARG_FAMILY: nl_cli_neigh_parse_family(neigh, optarg); break;
110 case ARG_STATE: nl_cli_neigh_parse_state(neigh, optarg); break;
111 }
112 }
113
114 nl_cache_foreach_filter(neigh_cache, OBJ_CAST(neigh), delete_cb, NULL);
115
116 if (!quiet)
117 printf("Deleted %d neighbours\n", deleted);
118
119 return 0;
120 }
121