libnl: /data/src/libnl3/src/nl-route-delete.c Source File

libnl 3.10.0

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