1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2008-2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/cls.h>
8 #include <netlink/cli/link.h>
9
10 #include <linux/netlink.h>
11
12 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
13 static struct nl_sock *sock;
14
print_usage(void)15 static void print_usage(void)
16 {
17 printf(
18 "Usage: nl-cls-delete [OPTION]... [class]\n"
19 "\n"
20 "OPTIONS\n"
21 " --interactive Run interactively.\n"
22 " --yes Set default answer to yes.\n"
23 " -q, --quiet Do not print informal notifications.\n"
24 " -h, --help Show this help text and exit.\n"
25 " -v, --version Show versioning information and exit.\n"
26 "\n"
27 " -d, --dev=DEV Device the classifer is attached to.\n"
28 " -p, --parent=ID Identifier of parent qdisc/class.\n"
29 " -i, --id=ID Identifier\n"
30 " -k, --kind=NAME Kind of classifier (e.g. basic, u32, fw)\n"
31 " --proto=PROTO Protocol to match (default: all)\n"
32 " --prio=PRIO Priority (default: 0)\n"
33 "\n"
34 "EXAMPLE\n"
35 " # Delete all classifiers on eth0 attached to parent q_root:\n"
36 " $ nl-cls-delete --dev eth0 --parent q_root:\n"
37 "\n"
38 );
39
40 exit(0);
41 }
42
delete_cb(struct nl_object * obj,void * arg)43 static void delete_cb(struct nl_object *obj, void *arg)
44 {
45 struct rtnl_cls *cls = nl_object_priv(obj);
46 struct nl_dump_params params = {
47 .dp_type = NL_DUMP_LINE,
48 .dp_fd = stdout,
49 };
50 int err;
51
52 if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
53 return;
54
55 if ((err = rtnl_cls_delete(sock, cls, 0)) < 0)
56 nl_cli_fatal(err, "Unable to delete classifier: %s\n",
57 nl_geterror(err));
58
59 if (!quiet) {
60 printf("Deleted ");
61 nl_object_dump(obj, ¶ms);
62 }
63
64 deleted++;
65 }
66
__delete_link(int ifindex,struct rtnl_cls * filter)67 static void __delete_link(int ifindex, struct rtnl_cls *filter)
68 {
69 struct nl_cache *cache;
70 uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter);
71
72 cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
73 nl_cache_foreach_filter(cache, OBJ_CAST(filter), delete_cb, NULL);
74 nl_cache_free(cache);
75 }
76
delete_link(struct nl_object * obj,void * arg)77 static void delete_link(struct nl_object *obj, void *arg)
78 {
79 struct rtnl_link *link = nl_object_priv(obj);
80
81 __delete_link(rtnl_link_get_ifindex(link), arg);
82 }
83
main(int argc,char * argv[])84 int main(int argc, char *argv[])
85 {
86 struct rtnl_cls *cls;
87 struct rtnl_tc *tc;
88 struct nl_cache *link_cache;
89 int ifindex;
90
91 sock = nl_cli_alloc_socket();
92 nl_cli_connect(sock, NETLINK_ROUTE);
93 link_cache = nl_cli_link_alloc_cache(sock);
94 cls = nl_cli_cls_alloc();
95 tc = (struct rtnl_tc *) cls;
96
97 for (;;) {
98 int c, optidx = 0;
99 enum {
100 ARG_YES = 257,
101 ARG_INTERACTIVE = 258,
102 ARG_PROTO,
103 ARG_PRIO,
104 };
105 static struct option long_opts[] = {
106 { "interactive", 0, 0, ARG_INTERACTIVE },
107 { "yes", 0, 0, ARG_YES },
108 { "quiet", 0, 0, 'q' },
109 { "help", 0, 0, 'h' },
110 { "version", 0, 0, 'v' },
111 { "dev", 1, 0, 'd' },
112 { "parent", 1, 0, 'p' },
113 { "id", 1, 0, 'i' },
114 { "kind", 1, 0, 'k' },
115 { "proto", 1, 0, ARG_PROTO },
116 { "prio", 1, 0, ARG_PRIO },
117 { 0, 0, 0, 0 }
118 };
119
120 c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
121 if (c == -1)
122 break;
123
124 switch (c) {
125 case '?': nl_cli_fatal(EINVAL, "Invalid options");
126 case ARG_INTERACTIVE: interactive = 1; break;
127 case ARG_YES: default_yes = 1; break;
128 case 'q': quiet = 1; break;
129 case 'h': print_usage(); break;
130 case 'v': nl_cli_print_version(); break;
131 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
132 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
133 case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
134 case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
135 case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
136 case ARG_PRIO:
137 rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
138 break;
139 }
140 }
141
142 if ((ifindex = rtnl_tc_get_ifindex(tc)))
143 __delete_link(ifindex, cls);
144 else
145 nl_cache_foreach(link_cache, delete_link, cls);
146
147 if (!quiet)
148 printf("Deleted %d classs\n", deleted);
149
150 return 0;
151 }
152