2 * src/nl-route-delete.c Delete Routes
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
9 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/route.h>
14 #include <netlink/cli/link.h>
16 static int interactive = 0, default_yes = 0, quiet = 0;
17 static int deleted = 0;
18 static struct nl_sock *sock;
20 static void print_version(void)
22 fprintf(stderr, "%s\n", LIBNL_STRING);
26 static void print_usage(void)
29 "Usage: nl-route-delete [OPTION]... [ROUTE]\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"
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"
44 " via=GATEWAY route via other node\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"
61 static void delete_cb(struct nl_object *obj, void *arg)
63 struct rtnl_route *route = (struct rtnl_route *) obj;
64 struct nl_dump_params params = {
65 .dp_type = NL_DUMP_LINE,
70 if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
73 if ((err = rtnl_route_delete(sock, route, 0)) < 0)
74 nl_cli_fatal(err, "Unable to delete route: %s", nl_geterror(err));
78 nl_object_dump(obj, ¶ms);
84 int main(int argc, char *argv[])
86 struct nl_cache *link_cache, *route_cache;
87 struct rtnl_route *route;
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();
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 },
131 c = getopt_long(argc, argv, "iqhvd:n:t:", long_opts, &optidx);
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;
156 if (nf == 0 && !interactive && !default_yes) {
157 fprintf(stderr, "You attempted to delete all routes in "
158 "non-interactive mode, aborting.\n");
162 nl_cache_foreach_filter(route_cache, OBJ_CAST(route), delete_cb, NULL);
165 printf("Deleted %d routes\n", deleted);