blob: ff8713b98e315ea6209fb3ab714a7b8fd5e81a8e [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * tc_filter.c "tc filter".
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000018#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include <linux/if_ether.h>
23
24#include "rt_names.h"
25#include "utils.h"
26#include "tc_util.h"
27#include "tc_common.h"
28
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000029static void usage(void)
30{
Stephen Hemmingerec2e0052016-10-12 15:21:13 -070031 fprintf(stderr,
32 "Usage: tc filter [ add | del | change | replace | show ] dev STRING\n"
33 "Usage: tc filter get dev STRING parent CLASSID protocol PROTO handle FILTERID pref PRIO FILTER_TYPE\n"
34 " [ pref PRIO ] protocol PROTO\n"
35 " [ estimator INTERVAL TIME_CONSTANT ]\n"
36 " [ root | ingress | egress | parent CLASSID ]\n"
37 " [ handle FILTERID ] [ [ FILTER_TYPE ] [ help | OPTIONS ] ]\n"
38 "\n"
39 " tc filter show [ dev STRING ] [ root | ingress | egress | parent CLASSID ]\n"
40 "Where:\n"
41 "FILTER_TYPE := { rsvp | u32 | bpf | fw | route | etc. }\n"
42 "FILTERID := ... format depends on classifier, see there\n"
43 "OPTIONS := ... try tc filter add <desired FILTER_KIND> help\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000044}
45
Stephen Hemminger32a121c2016-03-21 11:48:36 -070046static int tc_filter_modify(int cmd, unsigned int flags, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000047{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000048 struct {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070049 struct nlmsghdr n;
50 struct tcmsg t;
51 char buf[MAX_MSG];
Phil Sutterd17b1362016-07-18 16:48:42 +020052 } req = {
53 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
54 .n.nlmsg_flags = NLM_F_REQUEST | flags,
55 .n.nlmsg_type = cmd,
56 .t.tcm_family = AF_UNSPEC,
57 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000058 struct filter_util *q = NULL;
59 __u32 prio = 0;
Patrick McHardyae761062008-06-23 15:59:18 +020060 __u32 protocol = 0;
Patrick McHardy083a5f02008-03-26 07:40:03 +010061 int protocol_set = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062 char *fhandle = NULL;
Phil Sutterd17b1362016-07-18 16:48:42 +020063 char d[16] = {};
64 char k[16] = {};
65 struct tc_estimator est = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000066
Patrick McHardyae761062008-06-23 15:59:18 +020067 if (cmd == RTM_NEWTFILTER && flags & NLM_F_CREATE)
Florian Westphal05fb9182011-07-27 06:47:05 +000068 protocol = htons(ETH_P_ALL);
Patrick McHardyae761062008-06-23 15:59:18 +020069
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000070 while (argc > 0) {
71 if (strcmp(*argv, "dev") == 0) {
72 NEXT_ARG();
73 if (d[0])
74 duparg("dev", *argv);
75 strncpy(d, *argv, sizeof(d)-1);
76 } else if (strcmp(*argv, "root") == 0) {
77 if (req.t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -070078 fprintf(stderr,
79 "Error: \"root\" is duplicate parent ID\n");
osdl.net!shemminger024481b2005-03-14 19:02:41 +000080 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000081 }
82 req.t.tcm_parent = TC_H_ROOT;
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +010083 } else if (strcmp(*argv, "ingress") == 0) {
84 if (req.t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -070085 fprintf(stderr,
86 "Error: \"ingress\" is duplicate parent ID\n");
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +010087 return -1;
88 }
89 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
90 TC_H_MIN_INGRESS);
91 } else if (strcmp(*argv, "egress") == 0) {
92 if (req.t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -070093 fprintf(stderr,
94 "Error: \"egress\" is duplicate parent ID\n");
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +010095 return -1;
96 }
97 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
98 TC_H_MIN_EGRESS);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000099 } else if (strcmp(*argv, "parent") == 0) {
100 __u32 handle;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700101
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000102 NEXT_ARG();
103 if (req.t.tcm_parent)
104 duparg("parent", *argv);
105 if (get_tc_classid(&handle, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000106 invarg("Invalid parent ID", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000107 req.t.tcm_parent = handle;
108 } else if (strcmp(*argv, "handle") == 0) {
109 NEXT_ARG();
110 if (fhandle)
111 duparg("handle", *argv);
112 fhandle = *argv;
113 } else if (matches(*argv, "preference") == 0 ||
114 matches(*argv, "priority") == 0) {
115 NEXT_ARG();
116 if (prio)
117 duparg("priority", *argv);
Li Wei424adc12012-07-10 16:45:28 +0800118 if (get_u32(&prio, *argv, 0) || prio > 0xFFFF)
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000119 invarg("invalid priority value", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000120 } else if (matches(*argv, "protocol") == 0) {
121 __u16 id;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700122
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000123 NEXT_ARG();
Patrick McHardy083a5f02008-03-26 07:40:03 +0100124 if (protocol_set)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000125 duparg("protocol", *argv);
126 if (ll_proto_a2n(&id, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000127 invarg("invalid protocol", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000128 protocol = id;
Patrick McHardy083a5f02008-03-26 07:40:03 +0100129 protocol_set = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000130 } else if (matches(*argv, "estimator") == 0) {
131 if (parse_estimator(&argc, &argv, &est) < 0)
132 return -1;
133 } else if (matches(*argv, "help") == 0) {
134 usage();
Stephen Hemminger3a99df72008-10-13 07:00:48 -0700135 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000136 } else {
137 strncpy(k, *argv, sizeof(k)-1);
138
139 q = get_filter_kind(k);
140 argc--; argv++;
141 break;
142 }
143
144 argc--; argv++;
145 }
146
147 req.t.tcm_info = TC_H_MAKE(prio<<16, protocol);
148
149 if (k[0])
150 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
151
152 if (q) {
153 if (q->parse_fopt(q, fhandle, argc, argv, &req.n))
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000154 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000155 } else {
156 if (fhandle) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700157 fprintf(stderr,
158 "Must specify filter type when using \"handle\"\n");
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000159 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000160 }
161 if (argc) {
162 if (matches(*argv, "help") == 0)
163 usage();
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700164 fprintf(stderr,
165 "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n",
166 *argv);
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000167 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000168 }
169 }
170 if (est.ewma_log)
171 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
172
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000173
174 if (d[0]) {
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800175 ll_init_map(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000176
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700177 req.t.tcm_ifindex = ll_name_to_index(d);
178 if (req.t.tcm_ifindex == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000179 fprintf(stderr, "Cannot find device \"%s\"\n", d);
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000180 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000181 }
182 }
183
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700184 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000185 fprintf(stderr, "We have an error talking to the kernel\n");
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000186 return 2;
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000187 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000188
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000189 return 0;
190}
191
192static __u32 filter_parent;
193static int filter_ifindex;
194static __u32 filter_prio;
195static __u32 filter_protocol;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700196__u16 f_proto;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000197
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400198int print_filter(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000199{
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700200 FILE *fp = (FILE *)arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000201 struct tcmsg *t = NLMSG_DATA(n);
202 int len = n->nlmsg_len;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700203 struct rtattr *tb[TCA_MAX+1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000204 struct filter_util *q;
205 char abuf[256];
206
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400207 if (n->nlmsg_type != RTM_NEWTFILTER &&
208 n->nlmsg_type != RTM_GETTFILTER &&
209 n->nlmsg_type != RTM_DELTFILTER) {
210 fprintf(stderr, "Not a filter(cmd %d)\n", n->nlmsg_type);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000211 return 0;
212 }
213 len -= NLMSG_LENGTH(sizeof(*t));
214 if (len < 0) {
215 fprintf(stderr, "Wrong len %d\n", len);
216 return -1;
217 }
218
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000219 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
220
221 if (tb[TCA_KIND] == NULL) {
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000222 fprintf(stderr, "print_filter: NULL kind\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000223 return -1;
224 }
225
226 if (n->nlmsg_type == RTM_DELTFILTER)
227 fprintf(fp, "deleted ");
228
Roman Mashak98df0c82016-11-16 17:30:20 -0500229 if (n->nlmsg_type == RTM_NEWTFILTER &&
230 (n->nlmsg_flags & NLM_F_CREATE) &&
231 !(n->nlmsg_flags & NLM_F_EXCL))
232 fprintf(fp, "replaced ");
233
234 if (n->nlmsg_type == RTM_NEWTFILTER &&
235 (n->nlmsg_flags & NLM_F_CREATE) &&
236 (n->nlmsg_flags & NLM_F_EXCL))
237 fprintf(fp, "added ");
238
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000239 fprintf(fp, "filter ");
240 if (!filter_ifindex || filter_ifindex != t->tcm_ifindex)
241 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
242
243 if (!filter_parent || filter_parent != t->tcm_parent) {
244 if (t->tcm_parent == TC_H_ROOT)
245 fprintf(fp, "root ");
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +0100246 else if (t->tcm_parent == TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS))
247 fprintf(fp, "ingress ");
248 else if (t->tcm_parent == TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS))
249 fprintf(fp, "egress ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000250 else {
251 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
252 fprintf(fp, "parent %s ", abuf);
253 }
254 }
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +0100255
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000256 if (t->tcm_info) {
jamaleefcbc72008-04-20 10:47:48 -0400257 f_proto = TC_H_MIN(t->tcm_info);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000258 __u32 prio = TC_H_MAJ(t->tcm_info)>>16;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700259
jamaleefcbc72008-04-20 10:47:48 -0400260 if (!filter_protocol || filter_protocol != f_proto) {
261 if (f_proto) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000262 SPRINT_BUF(b1);
263 fprintf(fp, "protocol %s ",
jamaleefcbc72008-04-20 10:47:48 -0400264 ll_proto_n2a(f_proto, b1, sizeof(b1)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000265 }
266 }
267 if (!filter_prio || filter_prio != prio) {
268 if (prio)
269 fprintf(fp, "pref %u ", prio);
270 }
271 }
Stephen Hemmingerff247462012-04-10 08:47:55 -0700272 fprintf(fp, "%s ", rta_getattr_str(tb[TCA_KIND]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000273 q = get_filter_kind(RTA_DATA(tb[TCA_KIND]));
274 if (tb[TCA_OPTIONS]) {
275 if (q)
276 q->print_fopt(q, fp, tb[TCA_OPTIONS], t->tcm_handle);
277 else
278 fprintf(fp, "[cannot parse parameters]");
279 }
280 fprintf(fp, "\n");
281
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000282 if (show_stats && (tb[TCA_STATS] || tb[TCA_STATS2])) {
283 print_tcstats_attr(fp, tb, " ", NULL);
osdl.net!shemmingerde481782004-08-31 17:45:21 +0000284 fprintf(fp, "\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000285 }
osdl.net!shemmingerde481782004-08-31 17:45:21 +0000286
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000287 fflush(fp);
288 return 0;
289}
290
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400291static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv)
292{
293 struct {
294 struct nlmsghdr n;
295 struct tcmsg t;
296 char buf[MAX_MSG];
297 } req = {
298 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
299 /* NLM_F_ECHO is for backward compatibility. old kernels never
300 * respond without it and newer kernels will ignore it.
301 * In old kernels there is a side effect:
302 * In addition to a response to the GET you will receive an
303 * event (if you do tc mon).
304 */
305 .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ECHO | flags,
306 .n.nlmsg_type = cmd,
307 .t.tcm_parent = TC_H_UNSPEC,
308 .t.tcm_family = AF_UNSPEC,
309 };
310 struct filter_util *q = NULL;
311 __u32 prio = 0;
312 __u32 protocol = 0;
313 int protocol_set = 0;
314 __u32 parent_handle = 0;
315 char *fhandle = NULL;
316 char d[16] = {};
317 char k[16] = {};
318
319 while (argc > 0) {
320 if (strcmp(*argv, "dev") == 0) {
321 NEXT_ARG();
322 if (d[0])
323 duparg("dev", *argv);
324 strncpy(d, *argv, sizeof(d)-1);
325 } else if (strcmp(*argv, "root") == 0) {
326 if (req.t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700327 fprintf(stderr,
328 "Error: \"root\" is duplicate parent ID\n");
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400329 return -1;
330 }
331 req.t.tcm_parent = TC_H_ROOT;
332 } else if (strcmp(*argv, "ingress") == 0) {
333 if (req.t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700334 fprintf(stderr,
335 "Error: \"ingress\" is duplicate parent ID\n");
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400336 return -1;
337 }
338 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
339 TC_H_MIN_INGRESS);
340 } else if (strcmp(*argv, "egress") == 0) {
341 if (req.t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700342 fprintf(stderr,
343 "Error: \"egress\" is duplicate parent ID\n");
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400344 return -1;
345 }
346 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
347 TC_H_MIN_EGRESS);
348 } else if (strcmp(*argv, "parent") == 0) {
349
350 NEXT_ARG();
351 if (req.t.tcm_parent)
352 duparg("parent", *argv);
353 if (get_tc_classid(&parent_handle, *argv))
354 invarg("Invalid parent ID", *argv);
355 req.t.tcm_parent = parent_handle;
356 } else if (strcmp(*argv, "handle") == 0) {
357 NEXT_ARG();
358 if (fhandle)
359 duparg("handle", *argv);
360 fhandle = *argv;
361 } else if (matches(*argv, "preference") == 0 ||
362 matches(*argv, "priority") == 0) {
363 NEXT_ARG();
364 if (prio)
365 duparg("priority", *argv);
366 if (get_u32(&prio, *argv, 0) || prio > 0xFFFF)
367 invarg("invalid priority value", *argv);
368 } else if (matches(*argv, "protocol") == 0) {
369 __u16 id;
370
371 NEXT_ARG();
372 if (protocol_set)
373 duparg("protocol", *argv);
374 if (ll_proto_a2n(&id, *argv))
375 invarg("invalid protocol", *argv);
376 protocol = id;
377 protocol_set = 1;
378 } else if (matches(*argv, "help") == 0) {
379 usage();
380 return 0;
381 } else {
382 strncpy(k, *argv, sizeof(k)-1);
383
384 q = get_filter_kind(k);
385 argc--; argv++;
386 break;
387 }
388
389 argc--; argv++;
390 }
391
392 if (!protocol_set) {
393 fprintf(stderr, "Must specify filter protocol\n");
394 return -1;
395 }
396
397 if (!prio) {
398 fprintf(stderr, "Must specify filter priority\n");
399 return -1;
400 }
401
402 req.t.tcm_info = TC_H_MAKE(prio<<16, protocol);
403
404 if (req.t.tcm_parent == TC_H_UNSPEC) {
405 fprintf(stderr, "Must specify filter parent\n");
406 return -1;
407 }
408
409 if (k[0])
410 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
411 else {
412 fprintf(stderr, "Must specify filter type\n");
413 return -1;
414 }
415
416 if (q->parse_fopt(q, fhandle, argc, argv, &req.n))
417 return 1;
418
419
420 if (!fhandle) {
421 fprintf(stderr, "Must specify filter \"handle\"\n");
422 return -1;
423 }
424
425 if (argc) {
426 if (matches(*argv, "help") == 0)
427 usage();
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700428 fprintf(stderr,
429 "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n",
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400430 *argv);
431 return -1;
432 }
433
434 if (d[0]) {
435 ll_init_map(&rth);
436
437 req.t.tcm_ifindex = ll_name_to_index(d);
438 if (req.t.tcm_ifindex == 0) {
439 fprintf(stderr, "Cannot find device \"%s\"\n", d);
440 return 1;
441 }
442 filter_ifindex = req.t.tcm_ifindex;
443 } else {
444 fprintf(stderr, "Must specify netdevice \"dev\"\n");
445 return -1;
446 }
447
448 if (rtnl_talk(&rth, &req.n, &req.n, MAX_MSG) < 0) {
449 fprintf(stderr, "We have an error talking to the kernel\n");
450 return 2;
451 }
452
453 print_filter(NULL, &req.n, (void *)stdout);
454
455 return 0;
456}
457
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800458static int tc_filter_list(int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000459{
Phil Sutterd17b1362016-07-18 16:48:42 +0200460 struct tcmsg t = { .tcm_family = AF_UNSPEC };
461 char d[16] = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000462 __u32 prio = 0;
463 __u32 protocol = 0;
464 char *fhandle = NULL;
465
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000466 while (argc > 0) {
467 if (strcmp(*argv, "dev") == 0) {
468 NEXT_ARG();
469 if (d[0])
470 duparg("dev", *argv);
471 strncpy(d, *argv, sizeof(d)-1);
472 } else if (strcmp(*argv, "root") == 0) {
473 if (t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700474 fprintf(stderr,
475 "Error: \"root\" is duplicate parent ID\n");
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000476 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000477 }
478 filter_parent = t.tcm_parent = TC_H_ROOT;
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +0100479 } else if (strcmp(*argv, "ingress") == 0) {
480 if (t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700481 fprintf(stderr,
482 "Error: \"ingress\" is duplicate parent ID\n");
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +0100483 return -1;
484 }
485 filter_parent = TC_H_MAKE(TC_H_CLSACT,
486 TC_H_MIN_INGRESS);
487 t.tcm_parent = filter_parent;
488 } else if (strcmp(*argv, "egress") == 0) {
489 if (t.tcm_parent) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700490 fprintf(stderr,
491 "Error: \"egress\" is duplicate parent ID\n");
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +0100492 return -1;
493 }
494 filter_parent = TC_H_MAKE(TC_H_CLSACT,
495 TC_H_MIN_EGRESS);
496 t.tcm_parent = filter_parent;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000497 } else if (strcmp(*argv, "parent") == 0) {
498 __u32 handle;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700499
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000500 NEXT_ARG();
501 if (t.tcm_parent)
502 duparg("parent", *argv);
503 if (get_tc_classid(&handle, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000504 invarg("invalid parent ID", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000505 filter_parent = t.tcm_parent = handle;
506 } else if (strcmp(*argv, "handle") == 0) {
507 NEXT_ARG();
508 if (fhandle)
509 duparg("handle", *argv);
510 fhandle = *argv;
511 } else if (matches(*argv, "preference") == 0 ||
512 matches(*argv, "priority") == 0) {
513 NEXT_ARG();
514 if (prio)
515 duparg("priority", *argv);
516 if (get_u32(&prio, *argv, 0))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000517 invarg("invalid preference", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000518 filter_prio = prio;
519 } else if (matches(*argv, "protocol") == 0) {
520 __u16 res;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700521
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000522 NEXT_ARG();
523 if (protocol)
524 duparg("protocol", *argv);
525 if (ll_proto_a2n(&res, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000526 invarg("invalid protocol", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000527 protocol = res;
528 filter_protocol = protocol;
529 } else if (matches(*argv, "help") == 0) {
530 usage();
531 } else {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700532 fprintf(stderr,
533 " What is \"%s\"? Try \"tc filter help\"\n",
534 *argv);
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000535 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000536 }
537
538 argc--; argv++;
539 }
540
541 t.tcm_info = TC_H_MAKE(prio<<16, protocol);
542
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800543 ll_init_map(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000544
545 if (d[0]) {
Stephen Hemmingerec2e0052016-10-12 15:21:13 -0700546 t.tcm_ifindex = ll_name_to_index(d);
547 if (t.tcm_ifindex == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000548 fprintf(stderr, "Cannot find device \"%s\"\n", d);
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000549 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000550 }
551 filter_ifindex = t.tcm_ifindex;
552 }
553
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800554 if (rtnl_dump_request(&rth, RTM_GETTFILTER, &t, sizeof(t)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000555 perror("Cannot send dump request");
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000556 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000557 }
558
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800559 if (rtnl_dump_filter(&rth, print_filter, stdout) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000560 fprintf(stderr, "Dump terminated\n");
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000561 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000562 }
563
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000564 return 0;
565}
566
567int do_filter(int argc, char **argv)
568{
569 if (argc < 1)
570 return tc_filter_list(0, NULL);
571 if (matches(*argv, "add") == 0)
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400572 return tc_filter_modify(RTM_NEWTFILTER, NLM_F_EXCL|NLM_F_CREATE,
573 argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000574 if (matches(*argv, "change") == 0)
575 return tc_filter_modify(RTM_NEWTFILTER, 0, argc-1, argv+1);
576 if (matches(*argv, "replace") == 0)
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400577 return tc_filter_modify(RTM_NEWTFILTER, NLM_F_CREATE, argc-1,
578 argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000579 if (matches(*argv, "delete") == 0)
580 return tc_filter_modify(RTM_DELTFILTER, 0, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000581 if (matches(*argv, "get") == 0)
582 return tc_filter_get(RTM_GETTFILTER, 0, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000583 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
584 || matches(*argv, "lst") == 0)
585 return tc_filter_list(argc-1, argv+1);
Hasso Teppere5d179d2006-12-10 16:33:05 +0200586 if (matches(*argv, "help") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000587 usage();
Hasso Teppere5d179d2006-12-10 16:33:05 +0200588 return 0;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700589 }
Jamal Hadi Salim120f5562016-10-10 12:45:14 -0400590 fprintf(stderr, "Command \"%s\" is unknown, try \"tc filter help\".\n",
591 *argv);
osdl.net!shemminger024481b2005-03-14 19:02:41 +0000592 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000593}