1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/addr.h>
8 #include <netlink/cli/link.h>
9
10 #include <linux/netlink.h>
11
12 static struct nl_sock *sock;
13 static int interactive = 0, default_yes = 0, quiet = 0;
14 static int deleted = 0;
15
print_usage(void)16 static void print_usage(void)
17 {
18 printf(
19 "Usage: nl-addr-delete [OPTION]... [ADDRESS]\n"
20 "\n"
21 "Options\n"
22 " -i, --interactive Run interactively.\n"
23 " --yes Set default answer to yes.\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 "Address Options\n"
29 " -a, --local=ADDR Local address.\n"
30 " -d, --dev=DEV Associated network device.\n"
31 " --family=FAMILY Family of local address.\n"
32 " --label=STRING Address label (IPv4).\n"
33 " --peer=ADDR Peer address (IPv4).\n"
34 " --scope=SCOPE Address scope (IPv4).\n"
35 " --broadcast=ADDR Broadcast address of network (IPv4).\n"
36 " --valid-lifetime=TS Valid lifetime before route expires (IPv6).\n"
37 " --preferred=TIME Preferred lifetime (IPv6).\n"
38 " --valid=TIME Valid lifetime (IPv6).\n"
39 );
40
41 exit(0);
42 }
43
delete_cb(struct nl_object * obj,void * arg)44 static void delete_cb(struct nl_object *obj, void *arg)
45 {
46 struct rtnl_addr *addr = nl_object_priv(obj);
47 struct nl_dump_params params = {
48 .dp_type = NL_DUMP_LINE,
49 .dp_fd = stdout,
50 };
51 int err;
52
53 if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
54 return;
55
56 if ((err = rtnl_addr_delete(sock, addr, 0)) < 0)
57 nl_cli_fatal(err, "Unable to delete address: %s\n",
58 nl_geterror(err));
59
60 if (!quiet) {
61 printf("Deleted ");
62 nl_object_dump(obj, ¶ms);
63 }
64
65 deleted++;
66 }
67
main(int argc,char * argv[])68 int main(int argc, char *argv[])
69 {
70 struct rtnl_addr *addr;
71 struct nl_cache *link_cache, *addr_cache;
72
73 sock = nl_cli_alloc_socket();
74 nl_cli_connect(sock, NETLINK_ROUTE);
75 link_cache = nl_cli_link_alloc_cache(sock);
76 addr_cache = nl_cli_addr_alloc_cache(sock);
77 addr = nl_cli_addr_alloc();
78
79 for (;;) {
80 int c, optidx = 0;
81 enum {
82 ARG_FAMILY = 257,
83 ARG_LABEL = 258,
84 ARG_YES,
85 ARG_PEER,
86 ARG_SCOPE,
87 ARG_BROADCAST,
88 ARG_PREFERRED,
89 ARG_VALID,
90 };
91 static struct option long_opts[] = {
92 { "interactive", 0, 0, 'i' },
93 { "yes", 0, 0, ARG_YES },
94 { "quiet", 0, 0, 'q' },
95 { "help", 0, 0, 'h' },
96 { "version", 0, 0, 'v' },
97 { "local", 1, 0, 'a' },
98 { "dev", 1, 0, 'd' },
99 { "family", 1, 0, ARG_FAMILY },
100 { "label", 1, 0, ARG_LABEL },
101 { "peer", 1, 0, ARG_PEER },
102 { "scope", 1, 0, ARG_SCOPE },
103 { "broadcast", 1, 0, ARG_BROADCAST },
104 { "preferred", 1, 0, ARG_PREFERRED },
105 { "valid", 1, 0, ARG_VALID },
106 { 0, 0, 0, 0 }
107 };
108
109 c = getopt_long(argc, argv, "iqhva:d:", long_opts, &optidx);
110 if (c == -1)
111 break;
112
113 switch (c) {
114 case 'i': interactive = 1; break;
115 case ARG_YES: default_yes = 1; break;
116 case 'q': quiet = 1; break;
117 case 'h': print_usage(); break;
118 case 'v': nl_cli_print_version(); break;
119 case 'a': nl_cli_addr_parse_local(addr, optarg); break;
120 case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
121 case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
122 case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
123 case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
124 case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
125 case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
126 case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
127 case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
128 }
129 }
130
131 nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), delete_cb, NULL);
132
133 if (!quiet)
134 printf("Deleted %d addresses\n", deleted);
135
136 return 0;
137 }
138