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