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