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