Debian Codegraph @

tc/tc_filter.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * tc_filter.c "tc filter".
4 *
5 * Authors: Alexey Kuznetsov, <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16 #include <linux/if_ether.h>
17
18 #include "rt_names.h"
19 #include "utils.h"
20 #include "tc_util.h"
21 #include "tc_common.h"
22
23 static void usage(void)
24 {
25 fprintf(stderr,
26 "Usage: tc filter [ add | del | change | replace | show ] [ dev STRING ]\n"
27 " tc filter [ add | del | change | replace | show ] [ block BLOCK_INDEX ]\n"
28 " tc filter get dev STRING parent CLASSID protocol PROTO handle FILTERID pref PRIO FILTER_TYPE\n"
29 " tc filter get block BLOCK_INDEX protocol PROTO handle FILTERID pref PRIO FILTER_TYPE\n"
30 " [ pref PRIO ] protocol PROTO [ chain CHAIN_INDEX ]\n"
31 " [ estimator INTERVAL TIME_CONSTANT ]\n"
32 " [ root | ingress | egress | parent CLASSID ]\n"
33 " [ handle FILTERID ] [ [ FILTER_TYPE ] [ help | OPTIONS ] ]\n"
34 "\n"
35 " tc filter show [ dev STRING ] [ root | ingress | egress | parent CLASSID ]\n"
36 " tc filter show [ block BLOCK_INDEX ]\n"
37 "Where:\n"
38 "FILTER_TYPE := { u32 | bpf | fw | route | etc. }\n"
39 "FILTERID := ... format depends on classifier, see there\n"
40 "OPTIONS := ... try tc filter add <desired FILTER_KIND> help\n");
41 }
42
43 static void chain_usage(void)
44 {
45 fprintf(stderr,
46 "Usage: tc chain [ add | del | get | show ] [ dev STRING ]\n"
47 " tc chain [ add | del | get | show ] [ block BLOCK_INDEX ] ]\n");
48 }
49
50 struct tc_filter_req {
51 struct nlmsghdr n;
52 struct tcmsg t;
53 char buf[MAX_MSG];
54 };
55
56 static int tc_filter_modify(int cmd, unsigned int flags, int argc, char **argv)
57 {
58 struct {
59 struct nlmsghdr n;
60 struct tcmsg t;
61 char buf[MAX_MSG];
62 } req = {
63 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
64 .n.nlmsg_flags = NLM_F_REQUEST | flags,
65 .n.nlmsg_type = cmd,
66 .t.tcm_family = AF_UNSPEC,
67 };
68 const struct filter_util *q = NULL;
69 __u32 prio = 0;
70 __u32 protocol = 0;
71 int protocol_set = 0;
72 __u32 block_index = 0;
73 __u32 chain_index = 0;
74 int chain_index_set = 0;
75 char *fhandle = NULL;
76 char d[IFNAMSIZ] = {};
77 char k[FILTER_NAMESZ] = {};
78 struct tc_estimator est = {};
79 int ret;
80
81 if (cmd == RTM_NEWTFILTER && flags & NLM_F_CREATE)
82 protocol = htons(ETH_P_ALL);
83
84 while (argc > 0) {
85 if (strcmp(*argv, "dev") == 0) {
86 NEXT_ARG();
87 if (d[0])
88 duparg("dev", *argv);
89 if (block_index) {
90 fprintf(stderr, "Error: \"dev\" and \"block\" are mutually exclusive\n");
91 return -1;
92 }
93 strncpy(d, *argv, sizeof(d)-1);
94 } else if (matches(*argv, "block") == 0) {
95 NEXT_ARG();
96 if (block_index)
97 duparg("block", *argv);
98 if (d[0]) {
99 fprintf(stderr, "Error: \"dev\" and \"block\" are mutually exclusive\n");
100 return -1;
101 }
102 if (get_u32(&block_index, *argv, 0) || !block_index)
103 invarg("invalid block index value", *argv);
104 } else if (strcmp(*argv, "root") == 0) {
105 if (req.t.tcm_parent) {
106 fprintf(stderr,
107 "Error: \"root\" is duplicate parent ID\n");
108 return -1;
109 }
110 req.t.tcm_parent = TC_H_ROOT;
111 } else if (strcmp(*argv, "ingress") == 0) {
112 if (req.t.tcm_parent) {
113 fprintf(stderr,
114 "Error: \"ingress\" is duplicate parent ID\n");
115 return -1;
116 }
117 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
118 TC_H_MIN_INGRESS);
119 } else if (strcmp(*argv, "egress") == 0) {
120 if (req.t.tcm_parent) {
121 fprintf(stderr,
122 "Error: \"egress\" is duplicate parent ID\n");
123 return -1;
124 }
125 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
126 TC_H_MIN_EGRESS);
127 } else if (strcmp(*argv, "parent") == 0) {
128 __u32 handle;
129
130 NEXT_ARG();
131 if (req.t.tcm_parent)
132 duparg("parent", *argv);
133 if (get_tc_classid(&handle, *argv))
134 invarg("Invalid parent ID", *argv);
135 req.t.tcm_parent = handle;
136 } else if (strcmp(*argv, "handle") == 0) {
137 NEXT_ARG();
138 if (fhandle)
139 duparg("handle", *argv);
140 fhandle = *argv;
141 } else if (matches(*argv, "preference") == 0 ||
142 matches(*argv, "priority") == 0) {
143 NEXT_ARG();
144 if (prio)
145 duparg("priority", *argv);
146 if (get_u32(&prio, *argv, 0) || prio > 0xFFFF)
147 invarg("invalid priority value", *argv);
148 } else if (matches(*argv, "protocol") == 0) {
149 __u16 id;
150
151 NEXT_ARG();
152 if (protocol_set)
153 duparg("protocol", *argv);
154 if (ll_proto_a2n(&id, *argv))
155 invarg("invalid protocol", *argv);
156 protocol = id;
157 protocol_set = 1;
158 } else if (matches(*argv, "chain") == 0) {
159 NEXT_ARG();
160 if (chain_index_set)
161 duparg("chain", *argv);
162 if (get_u32(&chain_index, *argv, 0))
163 invarg("invalid chain index value", *argv);
164 chain_index_set = 1;
165 } else if (matches(*argv, "estimator") == 0) {
166 if (parse_estimator(&argc, &argv, &est) < 0)
167 return -1;
168 } else if (matches(*argv, "help") == 0) {
169 usage();
170 return 0;
171 } else {
172 strncpy(k, *argv, sizeof(k)-1);
173
174 q = get_filter_kind(k);
175 argc--; argv++;
176 break;
177 }
178
179 argc--; argv++;
180 }
181
182 req.t.tcm_info = TC_H_MAKE(prio<<16, protocol);
183
184 if (chain_index_set)
185 addattr32(&req.n, sizeof(req), TCA_CHAIN, chain_index);
186
187 if (k[0])
188 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
189
190 if (d[0]) {
191 ll_init_map(&rth);
192
193 req.t.tcm_ifindex = ll_name_to_index(d);
194 if (req.t.tcm_ifindex == 0) {
195 fprintf(stderr, "Cannot find device \"%s\"\n", d);
196 return 1;
197 }
198 } else if (block_index) {
199 req.t.tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
200 req.t.tcm_block_index = block_index;
201 }
202
203 if (q) {
204 if (q->parse_fopt(q, fhandle, argc, argv, &req.n))
205 return 1;
206 } else {
207 if (fhandle) {
208 fprintf(stderr,
209 "Must specify filter type when using \"handle\"\n");
210 return -1;
211 }
212 if (argc) {
213 if (matches(*argv, "help") == 0)
214 usage();
215 fprintf(stderr,
216 "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n",
217 *argv);
218 return -1;
219 }
220 }
221
222 if (est.ewma_log)
223 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
224
225 if (echo_request)
226 ret = rtnl_echo_talk(&rth, &req.n, json, print_filter);
227 else
228 ret = rtnl_talk(&rth, &req.n, NULL);
229
230 if (ret < 0) {
231 fprintf(stderr, "We have an error talking to the kernel\n");
232 return 2;
233 }
234
235 return 0;
236 }
237
238 static __u32 filter_parent;
239 static int filter_ifindex;
240 static __u32 filter_prio;
241 static __u32 filter_protocol;
242 static __u32 filter_chain_index;
243 static int filter_chain_index_set;
244 static __u32 filter_block_index;
245 __u16 f_proto;
246
247 int print_filter(struct nlmsghdr *n, void *arg)
248 {
249 FILE *fp = (FILE *)arg;
250 struct tcmsg *t = NLMSG_DATA(n);
251 int len = n->nlmsg_len;
252 struct rtattr *tb[TCA_MAX+1];
253 const struct filter_util *q;
254 char abuf[256];
255
256 if (n->nlmsg_type != RTM_NEWTFILTER &&
257 n->nlmsg_type != RTM_GETTFILTER &&
258 n->nlmsg_type != RTM_DELTFILTER &&
259 n->nlmsg_type != RTM_NEWCHAIN &&
260 n->nlmsg_type != RTM_GETCHAIN &&
261 n->nlmsg_type != RTM_DELCHAIN) {
262 fprintf(stderr, "Not a filter(cmd %d)\n", n->nlmsg_type);
263 return 0;
264 }
265 len -= NLMSG_LENGTH(sizeof(*t));
266 if (len < 0) {
267 fprintf(stderr, "Wrong len %d\n", len);
268 return -1;
269 }
270
271 parse_rtattr_flags(tb, TCA_MAX, TCA_RTA(t), len, NLA_F_NESTED);
272
273 if (tb[TCA_KIND] == NULL && (n->nlmsg_type == RTM_NEWTFILTER ||
274 n->nlmsg_type == RTM_GETTFILTER ||
275 n->nlmsg_type == RTM_DELTFILTER)) {
276 fprintf(stderr, "print_filter: NULL kind\n");
277 return -1;
278 }
279
280 open_json_object(NULL);
281
282 if (n->nlmsg_type == RTM_DELTFILTER || n->nlmsg_type == RTM_DELCHAIN)
283 print_bool(PRINT_ANY, "deleted", "deleted ", true);
284
285 if ((n->nlmsg_type == RTM_NEWTFILTER ||
286 n->nlmsg_type == RTM_NEWCHAIN) &&
287 (n->nlmsg_flags & NLM_F_CREATE) &&
288 !(n->nlmsg_flags & NLM_F_EXCL))
289 print_bool(PRINT_ANY, "replaced", "replaced ", true);
290
291 if ((n->nlmsg_type == RTM_NEWTFILTER ||
292 n->nlmsg_type == RTM_NEWCHAIN) &&
293 (n->nlmsg_flags & NLM_F_CREATE) &&
294 (n->nlmsg_flags & NLM_F_EXCL))
295 print_bool(PRINT_ANY, "added", "added ", true);
296
297 if (n->nlmsg_type == RTM_NEWTFILTER ||
298 n->nlmsg_type == RTM_GETTFILTER ||
299 n->nlmsg_type == RTM_DELTFILTER)
300 print_string(PRINT_FP, NULL, "filter ", NULL);
301 else
302 print_string(PRINT_FP, NULL, "chain ", NULL);
303 if (t->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
304 if (!filter_block_index ||
305 filter_block_index != t->tcm_block_index)
306 print_uint(PRINT_ANY, "block", "block %u ",
307 t->tcm_block_index);
308 } else {
309 if (!filter_ifindex || filter_ifindex != t->tcm_ifindex)
310 print_devname(PRINT_ANY, t->tcm_ifindex);
311
312 if (!filter_parent || filter_parent != t->tcm_parent) {
313 if (t->tcm_parent == TC_H_ROOT)
314 print_bool(PRINT_ANY, "root", "root ", true);
315 else if (t->tcm_parent == TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS))
316 print_bool(PRINT_ANY, "ingress", "ingress ", true);
317 else if (t->tcm_parent == TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS))
318 print_bool(PRINT_ANY, "egress", "egress ", true);
319 else {
320 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
321 print_string(PRINT_ANY, "parent", "parent %s ", abuf);
322 }
323 }
324 }
325
326 if (t->tcm_info && (n->nlmsg_type == RTM_NEWTFILTER ||
327 n->nlmsg_type == RTM_DELTFILTER ||
328 n->nlmsg_type == RTM_GETTFILTER)) {
329 f_proto = TC_H_MIN(t->tcm_info);
330 __u32 prio = TC_H_MAJ(t->tcm_info)>>16;
331
332 if (!filter_protocol || filter_protocol != f_proto) {
333 if (f_proto) {
334 SPRINT_BUF(b1);
335 print_string(PRINT_ANY, "protocol",
336 "protocol %s ",
337 ll_proto_n2a(f_proto, b1, sizeof(b1)));
338 }
339 }
340 if (!filter_prio || filter_prio != prio) {
341 if (prio)
342 print_uint(PRINT_ANY, "pref", "pref %u ", prio);
343 }
344 }
345 if (tb[TCA_KIND])
346 print_string(PRINT_ANY, "kind", "%s ", rta_getattr_str(tb[TCA_KIND]));
347
348 if (tb[TCA_CHAIN]) {
349 __u32 chain_index = rta_getattr_u32(tb[TCA_CHAIN]);
350
351 if (!filter_chain_index_set ||
352 filter_chain_index != chain_index)
353 print_uint(PRINT_ANY, "chain", "chain %u ",
354 chain_index);
355 }
356
357 if (tb[TCA_KIND]) {
358 q = get_filter_kind(RTA_DATA(tb[TCA_KIND]));
359 if (tb[TCA_OPTIONS]) {
360 open_json_object("options");
361 if (q)
362 q->print_fopt(q, fp, tb[TCA_OPTIONS], t->tcm_handle);
363 else
364 fprintf(stderr, "cannot parse option parameters\n");
365 close_json_object();
366 }
367 }
368 print_nl();
369
370 if (show_stats && (tb[TCA_STATS] || tb[TCA_STATS2])) {
371 print_tcstats_attr(fp, tb, " ", NULL);
372 print_nl();
373 }
374
375 print_ext_msg(tb);
376 close_json_object();
377 fflush(fp);
378 return 0;
379 }
380
381 static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv)
382 {
383 struct {
384 struct nlmsghdr n;
385 struct tcmsg t;
386 char buf[MAX_MSG];
387 } req = {
388 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
389 /* NLM_F_ECHO is for backward compatibility. old kernels never
390 * respond without it and newer kernels will ignore it.
391 * In old kernels there is a side effect:
392 * In addition to a response to the GET you will receive an
393 * event (if you do tc mon).
394 */
395 .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ECHO | flags,
396 .n.nlmsg_type = cmd,
397 .t.tcm_parent = TC_H_UNSPEC,
398 .t.tcm_family = AF_UNSPEC,
399 };
400 struct nlmsghdr *answer;
401 const struct filter_util *q = NULL;
402 __u32 prio = 0;
403 __u32 protocol = 0;
404 int protocol_set = 0;
405 __u32 chain_index;
406 int chain_index_set = 0;
407 __u32 block_index = 0;
408 __u32 parent_handle = 0;
409 char *fhandle = NULL;
410 char d[IFNAMSIZ] = {};
411 char k[FILTER_NAMESZ] = {};
412
413 while (argc > 0) {
414 if (strcmp(*argv, "dev") == 0) {
415 NEXT_ARG();
416 if (d[0])
417 duparg("dev", *argv);
418 if (block_index) {
419 fprintf(stderr, "Error: \"dev\" and \"block\" are mutually exclusive\n");
420 return -1;
421 }
422 strncpy(d, *argv, sizeof(d)-1);
423 } else if (matches(*argv, "block") == 0) {
424 NEXT_ARG();
425 if (block_index)
426 duparg("block", *argv);
427 if (d[0]) {
428 fprintf(stderr, "Error: \"dev\" and \"block\" are mutually exclusive\n");
429 return -1;
430 }
431 if (get_u32(&block_index, *argv, 0) || !block_index)
432 invarg("invalid block index value", *argv);
433 } else if (strcmp(*argv, "root") == 0) {
434 if (req.t.tcm_parent) {
435 fprintf(stderr,
436 "Error: \"root\" is duplicate parent ID\n");
437 return -1;
438 }
439 req.t.tcm_parent = TC_H_ROOT;
440 } else if (strcmp(*argv, "ingress") == 0) {
441 if (req.t.tcm_parent) {
442 fprintf(stderr,
443 "Error: \"ingress\" is duplicate parent ID\n");
444 return -1;
445 }
446 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
447 TC_H_MIN_INGRESS);
448 } else if (strcmp(*argv, "egress") == 0) {
449 if (req.t.tcm_parent) {
450 fprintf(stderr,
451 "Error: \"egress\" is duplicate parent ID\n");
452 return -1;
453 }
454 req.t.tcm_parent = TC_H_MAKE(TC_H_CLSACT,
455 TC_H_MIN_EGRESS);
456 } else if (strcmp(*argv, "parent") == 0) {
457
458 NEXT_ARG();
459 if (req.t.tcm_parent)
460 duparg("parent", *argv);
461 if (get_tc_classid(&parent_handle, *argv))
462 invarg("Invalid parent ID", *argv);
463 req.t.tcm_parent = parent_handle;
464 } else if (strcmp(*argv, "handle") == 0) {
465 NEXT_ARG();
466 if (fhandle)
467 duparg("handle", *argv);
468 fhandle = *argv;
469 } else if (matches(*argv, "preference") == 0 ||
470 matches(*argv, "priority") == 0) {
471 NEXT_ARG();
472 if (prio)
473 duparg("priority", *argv);
474 if (get_u32(&prio, *argv, 0) || prio > 0xFFFF)
475 invarg("invalid priority value", *argv);
476 } else if (matches(*argv, "protocol") == 0) {
477 __u16 id;
478
479 NEXT_ARG();
480 if (protocol_set)
481 duparg("protocol", *argv);
482 if (ll_proto_a2n(&id, *argv))
483 invarg("invalid protocol", *argv);
484 protocol = id;
485 protocol_set = 1;
486 } else if (matches(*argv, "chain") == 0) {
487 NEXT_ARG();
488 if (chain_index_set)
489 duparg("chain", *argv);
490 if (get_u32(&chain_index, *argv, 0))
491 invarg("invalid chain index value", *argv);
492 chain_index_set = 1;
493 } else if (matches(*argv, "help") == 0) {
494 usage();
495 return 0;
496 } else {
497 if (!**argv)
498 invarg("invalid filter name", *argv);
499
500 strncpy(k, *argv, sizeof(k)-1);
501
502 q = get_filter_kind(k);
503 argc--; argv++;
504 break;
505 }
506
507 argc--; argv++;
508 }
509
510 if (cmd == RTM_GETTFILTER) {
511 if (!protocol_set) {
512 fprintf(stderr, "Must specify filter protocol\n");
513 return -1;
514 }
515
516 if (!prio) {
517 fprintf(stderr, "Must specify filter priority\n");
518 return -1;
519 }
520
521 req.t.tcm_info = TC_H_MAKE(prio<<16, protocol);
522 }
523
524 if (chain_index_set)
525 addattr32(&req.n, sizeof(req), TCA_CHAIN, chain_index);
526
527 if (req.t.tcm_parent == TC_H_UNSPEC) {
528 fprintf(stderr, "Must specify filter parent\n");
529 return -1;
530 }
531
532 if (cmd == RTM_GETTFILTER) {
533 if (k[0])
534 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
535 else {
536 fprintf(stderr, "Must specify filter type\n");
537 return -1;
538 }
539 }
540
541 if (d[0]) {
542 ll_init_map(&rth);
543
544 req.t.tcm_ifindex = ll_name_to_index(d);
545 if (!req.t.tcm_ifindex)
546 return -nodev(d);
547 filter_ifindex = req.t.tcm_ifindex;
548 } else if (block_index) {
549 req.t.tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
550 req.t.tcm_block_index = block_index;
551 filter_block_index = block_index;
552 } else {
553 fprintf(stderr, "Must specify netdevice \"dev\" or block index \"block\"\n");
554 return -1;
555 }
556
557 if (cmd == RTM_GETTFILTER &&
558 q->parse_fopt(q, fhandle, argc, argv, &req.n))
559 return 1;
560
561 if (!fhandle && cmd == RTM_GETTFILTER) {
562 fprintf(stderr, "Must specify filter \"handle\"\n");
563 return -1;
564 }
565
566 if (argc) {
567 if (matches(*argv, "help") == 0)
568 usage();
569 fprintf(stderr,
570 "Garbage instead of arguments \"%s ...\". Try \"tc filter help\".\n",
571 *argv);
572 return -1;
573 }
574
575 if (rtnl_talk(&rth, &req.n, &answer) < 0) {
576 fprintf(stderr, "We have an error talking to the kernel\n");
577 return 2;
578 }
579
580 new_json_obj(json);
581 print_filter(answer, (void *)stdout);
582 delete_json_obj();
583
584 free(answer);
585 return 0;
586 }
587
588 static int tc_filter_list(int cmd, int argc, char **argv)
589 {
590 struct {
591 struct nlmsghdr n;
592 struct tcmsg t;
593 char buf[MAX_MSG];
594 } req = {
595 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
596 .n.nlmsg_type = cmd,
597 .t.tcm_parent = TC_H_UNSPEC,
598 .t.tcm_family = AF_UNSPEC,
599 };
600 char d[IFNAMSIZ] = {};
601 __u32 prio = 0;
602 __u32 protocol = 0;
603 __u32 block_index = 0;
604 char *fhandle = NULL;
605
606 while (argc > 0) {
607 if (strcmp(*argv, "dev") == 0) {
608 NEXT_ARG();
609 if (d[0])
610 duparg("dev", *argv);
611 if (block_index) {
612 fprintf(stderr, "Error: \"dev\" cannot be used in the same time as \"block\"\n");
613 return -1;
614 }
615 strncpy(d, *argv, sizeof(d)-1);
616 } else if (matches(*argv, "block") == 0) {
617 NEXT_ARG();
618 if (block_index)
619 duparg("block", *argv);
620 if (d[0]) {
621 fprintf(stderr, "Error: \"block\" cannot be used in the same time as \"dev\"\n");
622 return -1;
623 }
624 if (get_u32(&block_index, *argv, 0) || !block_index)
625 invarg("invalid block index value", *argv);
626 } else if (strcmp(*argv, "root") == 0) {
627 if (req.t.tcm_parent) {
628 fprintf(stderr,
629 "Error: \"root\" is duplicate parent ID\n");
630 return -1;
631 }
632 filter_parent = req.t.tcm_parent = TC_H_ROOT;
633 } else if (strcmp(*argv, "ingress") == 0) {
634 if (req.t.tcm_parent) {
635 fprintf(stderr,
636 "Error: \"ingress\" is duplicate parent ID\n");
637 return -1;
638 }
639 filter_parent = TC_H_MAKE(TC_H_CLSACT,
640 TC_H_MIN_INGRESS);
641 req.t.tcm_parent = filter_parent;
642 } else if (strcmp(*argv, "egress") == 0) {
643 if (req.t.tcm_parent) {
644 fprintf(stderr,
645 "Error: \"egress\" is duplicate parent ID\n");
646 return -1;
647 }
648 filter_parent = TC_H_MAKE(TC_H_CLSACT,
649 TC_H_MIN_EGRESS);
650 req.t.tcm_parent = filter_parent;
651 } else if (strcmp(*argv, "parent") == 0) {
652 __u32 handle;
653
654 NEXT_ARG();
655 if (req.t.tcm_parent)
656 duparg("parent", *argv);
657 if (get_tc_classid(&handle, *argv))
658 invarg("invalid parent ID", *argv);
659 filter_parent = req.t.tcm_parent = handle;
660 } else if (strcmp(*argv, "handle") == 0) {
661 NEXT_ARG();
662 if (fhandle)
663 duparg("handle", *argv);
664 fhandle = *argv;
665 } else if (matches(*argv, "preference") == 0 ||
666 matches(*argv, "priority") == 0) {
667 NEXT_ARG();
668 if (prio)
669 duparg("priority", *argv);
670 if (get_u32(&prio, *argv, 0))
671 invarg("invalid preference", *argv);
672 filter_prio = prio;
673 } else if (matches(*argv, "protocol") == 0) {
674 __u16 res;
675
676 NEXT_ARG();
677 if (protocol)
678 duparg("protocol", *argv);
679 if (ll_proto_a2n(&res, *argv))
680 invarg("invalid protocol", *argv);
681 protocol = res;
682 filter_protocol = protocol;
683 } else if (matches(*argv, "chain") == 0) {
684 __u32 chain_index;
685
686 NEXT_ARG();
687 if (filter_chain_index_set)
688 duparg("chain", *argv);
689 if (get_u32(&chain_index, *argv, 0))
690 invarg("invalid chain index value", *argv);
691 filter_chain_index_set = 1;
692 filter_chain_index = chain_index;
693 } else if (matches(*argv, "help") == 0) {
694 usage();
695 } else {
696 fprintf(stderr,
697 " What is \"%s\"? Try \"tc filter help\"\n",
698 *argv);
699 return -1;
700 }
701
702 argc--; argv++;
703 }
704
705 req.t.tcm_info = TC_H_MAKE(prio<<16, protocol);
706
707 ll_init_map(&rth);
708
709 if (d[0]) {
710 req.t.tcm_ifindex = ll_name_to_index(d);
711 if (!req.t.tcm_ifindex)
712 return -nodev(d);
713 filter_ifindex = req.t.tcm_ifindex;
714 } else if (block_index) {
715 if (!tc_qdisc_block_exists(block_index)) {
716 fprintf(stderr, "Cannot find block \"%u\"\n", block_index);
717 return 1;
718 }
719 req.t.tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
720 req.t.tcm_block_index = block_index;
721 filter_block_index = block_index;
722 }
723
724 if (filter_chain_index_set)
725 addattr32(&req.n, sizeof(req), TCA_CHAIN, filter_chain_index);
726
727 if (brief) {
728 struct nla_bitfield32 flags = {
729 .value = TCA_DUMP_FLAGS_TERSE,
730 .selector = TCA_DUMP_FLAGS_TERSE
731 };
732
733 addattr_l(&req.n, MAX_MSG, TCA_DUMP_FLAGS, &flags, sizeof(flags));
734 }
735
736 if (rtnl_dump_request_n(&rth, &req.n) < 0) {
737 perror("Cannot send dump request");
738 return 1;
739 }
740
741 new_json_obj(json);
742 if (rtnl_dump_filter(&rth, print_filter, stdout) < 0) {
743 fprintf(stderr, "Dump terminated\n");
744 delete_json_obj();
745 return 1;
746 }
747 delete_json_obj();
748
749 return 0;
750 }
751
752 int do_filter(int argc, char **argv)
753 {
754 if (argc < 1)
755 return tc_filter_list(RTM_GETTFILTER, 0, NULL);
756 if (matches(*argv, "add") == 0)
757 return tc_filter_modify(RTM_NEWTFILTER, NLM_F_EXCL|NLM_F_CREATE,
758 argc-1, argv+1);
759 if (matches(*argv, "change") == 0)
760 return tc_filter_modify(RTM_NEWTFILTER, 0, argc-1, argv+1);
761 if (matches(*argv, "replace") == 0)
762 return tc_filter_modify(RTM_NEWTFILTER, NLM_F_CREATE, argc-1,
763 argv+1);
764 if (matches(*argv, "delete") == 0)
765 return tc_filter_modify(RTM_DELTFILTER, 0, argc-1, argv+1);
766 if (matches(*argv, "get") == 0)
767 return tc_filter_get(RTM_GETTFILTER, 0, argc-1, argv+1);
768 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
769 || matches(*argv, "lst") == 0)
770 return tc_filter_list(RTM_GETTFILTER, argc-1, argv+1);
771 if (matches(*argv, "help") == 0) {
772 usage();
773 return 0;
774 }
775 fprintf(stderr, "Command \"%s\" is unknown, try \"tc filter help\".\n",
776 *argv);
777 return -1;
778 }
779
780 int do_chain(int argc, char **argv)
781 {
782 if (argc < 1)
783 return tc_filter_list(RTM_GETCHAIN, 0, NULL);
784 if (matches(*argv, "add") == 0) {
785 return tc_filter_modify(RTM_NEWCHAIN, NLM_F_EXCL | NLM_F_CREATE,
786 argc - 1, argv + 1);
787 } else if (matches(*argv, "delete") == 0) {
788 return tc_filter_modify(RTM_DELCHAIN, 0,
789 argc - 1, argv + 1);
790 } else if (matches(*argv, "get") == 0) {
791 return tc_filter_get(RTM_GETCHAIN, 0,
792 argc - 1, argv + 1);
793 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 ||
794 matches(*argv, "lst") == 0) {
795 return tc_filter_list(RTM_GETCHAIN, argc - 1, argv + 1);
796 } else if (matches(*argv, "help") == 0) {
797 chain_usage();
798 return 0;
799 }
800 fprintf(stderr, "Command \"%s\" is unknown, try \"tc chain help\".\n",
801 *argv);
802 return -1;
803 }