1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-route-delete.c Delete Routes
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/route.h>
15 #include <netlink/cli/link.h>
16
17 #include <linux/netlink.h>
18
19 static int interactive = 0, default_yes = 0, quiet = 0;
20 static int deleted = 0;
21 static struct nl_sock *sock;
22
print_version(void)23 static void print_version(void)
24 {
25 fprintf(stderr, "%s\n", LIBNL_STRING);
26 exit(0);
27 }
28
print_usage(void)29 static void print_usage(void)
30 {
31 printf(
32 "Usage: nl-route-delete [OPTION]... [ROUTE]\n"
33 "\n"
34 "Options\n"
35 " -i, --interactive Run interactively\n"
36 " --yes Set default answer to yes\n"
37 " -q, --quiet Do not print informal notifications\n"
38 " -h, --help Show this help\n"
39 " -v, --version Show versioning information\n"
40 "\n"
41 "Route Options\n"
42 " -d, --dst=ADDR destination prefix, e.g. 10.10.0.0/16\n"
43 " -n, --nexthop=NH nexthop configuration:\n"
44 " dev=DEV route via device\n"
45 " weight=WEIGHT weight of nexthop\n"
46 " flags=FLAGS\n"
47 " via=GATEWAY route via other node\n"
48 " realms=REALMS\n"
49 " e.g. dev=eth0,via=192.168.1.12\n"
50 " -t, --table=TABLE Routing table\n"
51 " --family=FAMILY Address family\n"
52 " --src=ADDR Source prefix\n"
53 " --iif=DEV Incomming interface\n"
54 " --pref-src=ADDR Preferred source address\n"
55 " --metrics=OPTS Metrics configurations\n"
56 " --priority=NUM Priotity\n"
57 " --scope=SCOPE Scope\n"
58 " --protocol=PROTO Protocol\n"
59 " --type=TYPE { unicast | local | broadcast | multicast }\n"
60 );
61 exit(0);
62 }
63
delete_cb(struct nl_object * obj,void * arg)64 static void delete_cb(struct nl_object *obj, void *arg)
65 {
66 struct rtnl_route *route = (struct rtnl_route *) obj;
67 struct nl_dump_params params = {
68 .dp_type = NL_DUMP_LINE,
69 .dp_fd = stdout,
70 };
71 int err;
72
73 if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
74 return;
75
76 if ((err = rtnl_route_delete(sock, route, 0)) < 0)
77 nl_cli_fatal(err, "Unable to delete route: %s", nl_geterror(err));
78
79 if (!quiet) {
80 printf("Deleted ");
81 nl_object_dump(obj, ¶ms);
82 }
83
84 deleted++;
85 }
86
main(int argc,char * argv[])87 int main(int argc, char *argv[])
88 {
89 struct nl_cache *link_cache, *route_cache;
90 struct rtnl_route *route;
91 int nf = 0;
92
93 sock = nl_cli_alloc_socket();
94 nl_cli_connect(sock, NETLINK_ROUTE);
95 link_cache = nl_cli_link_alloc_cache(sock);
96 route_cache = nl_cli_route_alloc_cache(sock, 0);
97 route = nl_cli_route_alloc();
98
99 for (;;) {
100 int c, optidx = 0;
101 enum {
102 ARG_FAMILY = 257,
103 ARG_SRC = 258,
104 ARG_IIF,
105 ARG_PREF_SRC,
106 ARG_METRICS,
107 ARG_PRIORITY,
108 ARG_SCOPE,
109 ARG_PROTOCOL,
110 ARG_TYPE,
111 ARG_YES,
112 };
113 static struct option long_opts[] = {
114 { "interactive", 0, 0, 'i' },
115 { "yes", 0, 0, ARG_YES },
116 { "quiet", 0, 0, 'q' },
117 { "help", 0, 0, 'h' },
118 { "version", 0, 0, 'v' },
119 { "dst", 1, 0, 'd' },
120 { "nexthop", 1, 0, 'n' },
121 { "table", 1, 0, 't' },
122 { "family", 1, 0, ARG_FAMILY },
123 { "src", 1, 0, ARG_SRC },
124 { "iif", 1, 0, ARG_IIF },
125 { "pref-src", 1, 0, ARG_PREF_SRC },
126 { "metrics", 1, 0, ARG_METRICS },
127 { "priority", 1, 0, ARG_PRIORITY },
128 { "scope", 1, 0, ARG_SCOPE },
129 { "protocol", 1, 0, ARG_PROTOCOL },
130 { "type", 1, 0, ARG_TYPE },
131 { 0, 0, 0, 0 }
132 };
133
134 c = getopt_long(argc, argv, "iqhvd:n:t:", long_opts, &optidx);
135 if (c == -1)
136 break;
137
138 switch (c) {
139 case 'i': interactive = 1; break;
140 case ARG_YES: default_yes = 1; break;
141 case 'q': quiet = 1; break;
142 case 'h': print_usage(); break;
143 case 'v': print_version(); break;
144 case 'd': nf++; nl_cli_route_parse_dst(route, optarg); break;
145 case 'n': nf++; nl_cli_route_parse_nexthop(route, optarg, link_cache); break;
146 case 't': nf++; nl_cli_route_parse_table(route, optarg); break;
147 case ARG_FAMILY: nf++; nl_cli_route_parse_family(route, optarg); break;
148 case ARG_SRC: nf++; nl_cli_route_parse_src(route, optarg); break;
149 case ARG_IIF: nf++; nl_cli_route_parse_iif(route, optarg, link_cache); break;
150 case ARG_PREF_SRC: nf++; nl_cli_route_parse_pref_src(route, optarg); break;
151 case ARG_METRICS: nf++; nl_cli_route_parse_metric(route, optarg); break;
152 case ARG_PRIORITY: nf++; nl_cli_route_parse_prio(route, optarg); break;
153 case ARG_SCOPE: nf++; nl_cli_route_parse_scope(route, optarg); break;
154 case ARG_PROTOCOL: nf++; nl_cli_route_parse_protocol(route, optarg); break;
155 case ARG_TYPE: nf++; nl_cli_route_parse_type(route, optarg); break;
156 }
157 }
158
159 if (nf == 0 && !interactive && !default_yes) {
160 fprintf(stderr, "You attempted to delete all routes in "
161 "non-interactive mode, aborting.\n");
162 exit(0);
163 }
164
165 nl_cache_foreach_filter(route_cache, OBJ_CAST(route), delete_cb, NULL);
166
167 if (!quiet)
168 printf("Deleted %d routes\n", deleted);
169
170 return 0;
171 }
172