blob: 2e1078d31bc122b44d4aef246a0a4d894c20f353 [file] [log] [blame]
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00004 *
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00006 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00007 * Bernhard Reutner-Fischer adjusted for busybox
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00008 */
Denys Vlasenkoa034cab2018-01-04 10:27:51 +01009//config:config TC
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "tc (8.3 kb)"
Denys Vlasenkoa034cab2018-01-04 10:27:51 +010011//config: default y
12//config: help
13//config: Show / manipulate traffic control settings
14//config:
15//config:config FEATURE_TC_INGRESS
16//config: bool "Enable ingress"
17//config: default y
18//config: depends on TC
Denys Vlasenko29e2c452016-11-23 09:51:33 +010019
Denys Vlasenkoa034cab2018-01-04 10:27:51 +010020//applet:IF_TC(APPLET(tc, BB_DIR_SBIN, BB_SUID_DROP))
21
22//kbuild:lib-$(CONFIG_TC) += tc.o
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000023
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage:#define tc_trivial_usage
25/* //usage: "[OPTIONS] OBJECT CMD [dev STRING]" */
26//usage: "OBJECT CMD [dev STRING]"
27//usage:#define tc_full_usage "\n\n"
Denys Vlasenko12389882017-01-21 14:27:07 +010028//usage: "OBJECT: qdisc|class|filter\n"
29//usage: "CMD: add|del|change|replace|show\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020030//usage: "\n"
Denys Vlasenko12389882017-01-21 14:27:07 +010031//usage: "qdisc [handle QHANDLE] [root|"IF_FEATURE_TC_INGRESS("ingress|")"parent CLASSID]\n"
32/* //usage: "[estimator INTERVAL TIME_CONSTANT]\n" */
33//usage: " [[QDISC_KIND] [help|OPTIONS]]\n"
34//usage: " QDISC_KIND := [p|b]fifo|tbf|prio|cbq|red|etc.\n"
35//usage: "qdisc show [dev STRING]"IF_FEATURE_TC_INGRESS(" [ingress]")"\n"
36//usage: "class [classid CLASSID] [root|parent CLASSID]\n"
37//usage: " [[QDISC_KIND] [help|OPTIONS] ]\n"
38//usage: "class show [ dev STRING ] [root|parent CLASSID]\n"
39//usage: "filter [pref PRIO] [protocol PROTO]\n"
40/* //usage: "\t[estimator INTERVAL TIME_CONSTANT]\n" */
41//usage: " [root|classid CLASSID] [handle FILTERID]\n"
42//usage: " [[FILTER_TYPE] [help|OPTIONS]]\n"
43//usage: "filter show [dev STRING] [root|parent CLASSID]"
Pere Orga5bc8c002011-04-11 03:29:49 +020044
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000045#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020046#include "common_bufsiz.h"
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000047
48#include "libiproute/utils.h"
49#include "libiproute/ip_common.h"
50#include "libiproute/rt_names.h"
51#include <linux/pkt_sched.h> /* for the TC_H_* macros */
52
Denys Vlasenkoa034cab2018-01-04 10:27:51 +010053/* This is the deprecated multiqueue interface */
54#ifndef TCA_PRIO_MAX
55enum
56{
57 TCA_PRIO_UNSPEC,
58 TCA_PRIO_MQ,
59 __TCA_PRIO_MAX
60};
61#define TCA_PRIO_MAX (__TCA_PRIO_MAX - 1)
62#endif
63
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000064#define parse_rtattr_nested(tb, max, rta) \
65 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
66
67/* nullifies tb on error */
68#define __parse_rtattr_nested_compat(tb, max, rta, len) \
Denys Vlasenko68ae5422018-02-08 08:42:37 +010069({ \
70 if ((RTA_PAYLOAD(rta) >= len) \
71 && (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) \
72 ) { \
73 rta = RTA_DATA(rta) + RTA_ALIGN(len); \
74 parse_rtattr_nested(tb, max, rta); \
75 } else \
76 memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
77})
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000078
79#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
Denys Vlasenko68ae5422018-02-08 08:42:37 +010080({ \
81 data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
82 __parse_rtattr_nested_compat(tb, max, rta, len); \
83})
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000084
85#define show_details (0) /* not implemented. Does anyone need it? */
86#define use_iec (0) /* not currently documented in the upstream manpage */
87
88
89struct globals {
90 int filter_ifindex;
Daniel Fandrich6295d272011-06-09 15:44:44 -070091 uint32_t filter_qdisc;
92 uint32_t filter_parent;
93 uint32_t filter_prio;
94 uint32_t filter_proto;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010095} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020096#define G (*(struct globals*)bb_common_bufsiz1)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000097#define filter_ifindex (G.filter_ifindex)
98#define filter_qdisc (G.filter_qdisc)
99#define filter_parent (G.filter_parent)
100#define filter_prio (G.filter_prio)
101#define filter_proto (G.filter_proto)
Denys Vlasenkoab3964d2015-10-13 14:50:20 +0200102#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200103 setup_common_bufsiz(); \
Denys Vlasenkoab3964d2015-10-13 14:50:20 +0200104 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
105} while (0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000106
107/* Allocates a buffer containing the name of a class id.
108 * The caller must free the returned memory. */
109static char* print_tc_classid(uint32_t cid)
110{
111#if 0 /* IMPOSSIBLE */
112 if (cid == TC_H_ROOT)
113 return xasprintf("root");
114 else
115#endif
116 if (cid == TC_H_UNSPEC)
117 return xasprintf("none");
118 else if (TC_H_MAJ(cid) == 0)
119 return xasprintf(":%x", TC_H_MIN(cid));
120 else if (TC_H_MIN(cid) == 0)
121 return xasprintf("%x:", TC_H_MAJ(cid)>>16);
122 else
123 return xasprintf("%x:%x", TC_H_MAJ(cid)>>16, TC_H_MIN(cid));
124}
125
126/* Get a qdisc handle. Return 0 on success, !0 otherwise. */
Denys Vlasenko8c317f02019-05-14 17:26:47 +0200127static int get_qdisc_handle(uint32_t *h, const char *str)
128{
Daniel Fandrich6295d272011-06-09 15:44:44 -0700129 uint32_t maj;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000130 char *p;
131
132 maj = TC_H_UNSPEC;
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100133 if (strcmp(str, "none") == 0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000134 goto ok;
135 maj = strtoul(str, &p, 16);
136 if (p == str)
137 return 1;
138 maj <<= 16;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200139 if (*p != ':' && *p != '\0')
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000140 return 1;
141 ok:
142 *h = maj;
143 return 0;
144}
145
146/* Get class ID. Return 0 on success, !0 otherwise. */
Denys Vlasenko8c317f02019-05-14 17:26:47 +0200147static int get_tc_classid(uint32_t *h, const char *str)
148{
Daniel Fandrich6295d272011-06-09 15:44:44 -0700149 uint32_t maj, min;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000150 char *p;
151
152 maj = TC_H_ROOT;
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100153 if (strcmp(str, "root") == 0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000154 goto ok;
155 maj = TC_H_UNSPEC;
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100156 if (strcmp(str, "none") == 0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000157 goto ok;
158 maj = strtoul(str, &p, 16);
159 if (p == str) {
160 if (*p != ':')
161 return 1;
162 maj = 0;
163 }
164 if (*p == ':') {
165 if (maj >= (1<<16))
166 return 1;
167 maj <<= 16;
168 str = p + 1;
169 min = strtoul(str, &p, 16);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200170//FIXME: check for "" too?
171 if (*p != '\0' || min >= (1<<16))
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000172 return 1;
173 maj |= min;
174 } else if (*p != 0)
175 return 1;
176 ok:
177 *h = maj;
178 return 0;
179}
180
181static void print_rate(char *buf, int len, uint32_t rate)
182{
183 double tmp = (double)rate*8;
184
185 if (use_iec) {
Denys Vlasenkob8781212015-05-24 18:01:53 +0200186 if (tmp >= 1000*1024*1024)
187 snprintf(buf, len, "%.0fMibit", tmp/(1024*1024));
188 else if (tmp >= 1000*1024)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000189 snprintf(buf, len, "%.0fKibit", tmp/1024);
190 else
191 snprintf(buf, len, "%.0fbit", tmp);
192 } else {
Denys Vlasenkob8781212015-05-24 18:01:53 +0200193 if (tmp >= 1000*1000000)
194 snprintf(buf, len, "%.0fMbit", tmp/1000000);
195 else if (tmp >= 1000*1000)
196 snprintf(buf, len, "%.0fKbit", tmp/1000);
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000197 else
198 snprintf(buf, len, "%.0fbit", tmp);
199 }
200}
201
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100202#if 0
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000203/* This is "pfifo_fast". */
204static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
205{
206 return 0;
207}
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100208#endif
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000209static int prio_print_opt(struct rtattr *opt)
210{
211 int i;
212 struct tc_prio_qopt *qopt;
213 struct rtattr *tb[TCA_PRIO_MAX+1];
214
215 if (opt == NULL)
216 return 0;
217 parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt));
218 if (tb == NULL)
219 return 0;
220 printf("bands %u priomap ", qopt->bands);
221 for (i=0; i<=TC_PRIO_MAX; i++)
222 printf(" %d", qopt->priomap[i]);
223
224 if (tb[TCA_PRIO_MQ])
225 printf(" multiqueue: o%s ",
226 *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
227
228 return 0;
229}
230
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100231#if 0
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000232/* Class Based Queue */
233static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
234{
235 return 0;
236}
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100237#endif
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000238static int cbq_print_opt(struct rtattr *opt)
239{
240 struct rtattr *tb[TCA_CBQ_MAX+1];
241 struct tc_ratespec *r = NULL;
242 struct tc_cbq_lssopt *lss = NULL;
243 struct tc_cbq_wrropt *wrr = NULL;
244 struct tc_cbq_fopt *fopt = NULL;
245 struct tc_cbq_ovl *ovl = NULL;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100246 const char *const error = "CBQ: too short %s opt";
247 char buf[64];
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000248
249 if (opt == NULL)
250 goto done;
251 parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
252
253 if (tb[TCA_CBQ_RATE]) {
254 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
255 bb_error_msg(error, "rate");
256 else
257 r = RTA_DATA(tb[TCA_CBQ_RATE]);
258 }
259 if (tb[TCA_CBQ_LSSOPT]) {
260 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
261 bb_error_msg(error, "lss");
262 else
263 lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
264 }
265 if (tb[TCA_CBQ_WRROPT]) {
266 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
267 bb_error_msg(error, "wrr");
268 else
269 wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
270 }
271 if (tb[TCA_CBQ_FOPT]) {
272 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
273 bb_error_msg(error, "fopt");
274 else
275 fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
276 }
277 if (tb[TCA_CBQ_OVL_STRATEGY]) {
278 if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl))
279 bb_error_msg("CBQ: too short overlimit strategy %u/%u",
280 (unsigned) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]),
281 (unsigned) sizeof(*ovl));
282 else
283 ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
284 }
285
286 if (r) {
287 print_rate(buf, sizeof(buf), r->rate);
288 printf("rate %s ", buf);
289 if (show_details) {
290 printf("cell %ub ", 1<<r->cell_log);
291 if (r->mpu)
292 printf("mpu %ub ", r->mpu);
293 if (r->overhead)
294 printf("overhead %ub ", r->overhead);
295 }
296 }
297 if (lss && lss->flags) {
298 bool comma = false;
299 bb_putchar('(');
300 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
301 printf("bounded");
302 comma = true;
303 }
304 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
305 if (comma)
306 bb_putchar(',');
307 printf("isolated");
308 }
309 printf(") ");
310 }
311 if (wrr) {
312 if (wrr->priority != TC_CBQ_MAXPRIO)
313 printf("prio %u", wrr->priority);
314 else
315 printf("prio no-transmit");
316 if (show_details) {
317 printf("/%u ", wrr->cpriority);
318 if (wrr->weight != 1) {
319 print_rate(buf, sizeof(buf), wrr->weight);
320 printf("weight %s ", buf);
321 }
322 if (wrr->allot)
323 printf("allot %ub ", wrr->allot);
324 }
325 }
326 done:
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000327 return 0;
328}
329
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100330static FAST_FUNC int print_qdisc(
331 const struct sockaddr_nl *who UNUSED_PARAM,
332 struct nlmsghdr *hdr,
333 void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000334{
335 struct tcmsg *msg = NLMSG_DATA(hdr);
336 int len = hdr->nlmsg_len;
337 struct rtattr * tb[TCA_MAX+1];
338 char *name;
339
340 if (hdr->nlmsg_type != RTM_NEWQDISC && hdr->nlmsg_type != RTM_DELQDISC) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000341 /* bb_error_msg("not a qdisc"); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000342 return 0; /* ??? mimic upstream; should perhaps return -1 */
343 }
344 len -= NLMSG_LENGTH(sizeof(*msg));
345 if (len < 0) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000346 /* bb_error_msg("wrong len %d", len); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000347 return -1;
348 }
349 /* not the desired interface? */
350 if (filter_ifindex && filter_ifindex != msg->tcm_ifindex)
351 return 0;
352 memset (tb, 0, sizeof(tb));
353 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
354 if (tb[TCA_KIND] == NULL) {
355 /* bb_error_msg("%s: NULL kind", "qdisc"); */
356 return -1;
357 }
358 if (hdr->nlmsg_type == RTM_DELQDISC)
359 printf("deleted ");
360 name = (char*)RTA_DATA(tb[TCA_KIND]);
361 printf("qdisc %s %x: ", name, msg->tcm_handle>>16);
362 if (filter_ifindex == 0)
363 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
364 if (msg->tcm_parent == TC_H_ROOT)
365 printf("root ");
366 else if (msg->tcm_parent) {
367 char *classid = print_tc_classid(msg->tcm_parent);
368 printf("parent %s ", classid);
369 if (ENABLE_FEATURE_CLEAN_UP)
370 free(classid);
371 }
372 if (msg->tcm_info != 1)
373 printf("refcnt %d ", msg->tcm_info);
374 if (tb[TCA_OPTIONS]) {
375 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
376 int qqq = index_in_strings(_q_, name);
377 if (qqq == 0) { /* pfifo_fast aka prio */
378 prio_print_opt(tb[TCA_OPTIONS]);
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200379 } else if (qqq == 1) { /* class based queuing */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000380 cbq_print_opt(tb[TCA_OPTIONS]);
381 } else
382 bb_error_msg("unknown %s", name);
383 }
384 bb_putchar('\n');
385 return 0;
386}
387
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100388static FAST_FUNC int print_class(
389 const struct sockaddr_nl *who UNUSED_PARAM,
390 struct nlmsghdr *hdr,
391 void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000392{
393 struct tcmsg *msg = NLMSG_DATA(hdr);
394 int len = hdr->nlmsg_len;
395 struct rtattr * tb[TCA_MAX+1];
396 char *name, *classid;
397
398 /*XXX Eventually factor out common code */
399
400 if (hdr->nlmsg_type != RTM_NEWTCLASS && hdr->nlmsg_type != RTM_DELTCLASS) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000401 /* bb_error_msg("not a class"); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000402 return 0; /* ??? mimic upstream; should perhaps return -1 */
403 }
404 len -= NLMSG_LENGTH(sizeof(*msg));
405 if (len < 0) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000406 /* bb_error_msg("wrong len %d", len); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000407 return -1;
408 }
409 /* not the desired interface? */
410 if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc))
411 return 0;
412 memset (tb, 0, sizeof(tb));
413 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
414 if (tb[TCA_KIND] == NULL) {
415 /* bb_error_msg("%s: NULL kind", "class"); */
416 return -1;
417 }
418 if (hdr->nlmsg_type == RTM_DELTCLASS)
419 printf("deleted ");
420
421 name = (char*)RTA_DATA(tb[TCA_KIND]);
422 classid = !msg->tcm_handle ? NULL : print_tc_classid(
423 filter_qdisc ? TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
424 printf ("class %s %s", name, classid);
425 if (ENABLE_FEATURE_CLEAN_UP)
426 free(classid);
427
428 if (filter_ifindex == 0)
429 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
430 if (msg->tcm_parent == TC_H_ROOT)
431 printf("root ");
432 else if (msg->tcm_parent) {
433 classid = print_tc_classid(filter_qdisc ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100434 TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000435 printf("parent %s ", classid);
436 if (ENABLE_FEATURE_CLEAN_UP)
437 free(classid);
438 }
439 if (msg->tcm_info)
440 printf("leaf %x ", msg->tcm_info >> 16);
441 /* Do that get_qdisc_kind(RTA_DATA(tb[TCA_KIND])). */
442 if (tb[TCA_OPTIONS]) {
443 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
444 int qqq = index_in_strings(_q_, name);
445 if (qqq == 0) { /* pfifo_fast aka prio */
446 /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200447 } else if (qqq == 1) { /* class based queuing */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000448 /* cbq_print_copt() is identical to cbq_print_opt(). */
449 cbq_print_opt(tb[TCA_OPTIONS]);
450 } else
451 bb_error_msg("unknown %s", name);
452 }
453 bb_putchar('\n');
454
455 return 0;
456}
457
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100458static FAST_FUNC int print_filter(
459 const struct sockaddr_nl *who UNUSED_PARAM,
460 struct nlmsghdr *hdr UNUSED_PARAM,
461 void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000462{
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000463 return 0;
464}
465
466int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
467int tc_main(int argc UNUSED_PARAM, char **argv)
468{
469 static const char objects[] ALIGN1 =
470 "qdisc\0""class\0""filter\0"
471 ;
472 enum { OBJ_qdisc = 0, OBJ_class, OBJ_filter };
473 static const char commands[] ALIGN1 =
474 "add\0""delete\0""change\0"
475 "link\0" /* only qdisc */
476 "replace\0"
477 "show\0""list\0"
478 ;
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100479 enum {
480 CMD_add = 0, CMD_del, CMD_change,
481 CMD_link,
482 CMD_replace,
483 CMD_show
484 };
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000485 static const char args[] ALIGN1 =
486 "dev\0" /* qdisc, class, filter */
487 "root\0" /* class, filter */
488 "parent\0" /* class, filter */
489 "qdisc\0" /* class */
490 "handle\0" /* change: qdisc, class(classid) list: filter */
491 "classid\0" /* change: for class use "handle" */
492 "preference\0""priority\0""protocol\0" /* filter */
493 ;
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100494 enum {
495 ARG_dev = 0,
496 ARG_root,
497 ARG_parent,
498 ARG_qdisc,
499 ARG_handle,
500 ARG_classid,
501 ARG_pref, ARG_prio, ARG_proto
502 };
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000503 struct rtnl_handle rth;
504 struct tcmsg msg;
505 int ret, obj, cmd, arg;
506 char *dev = NULL;
507
508 INIT_G();
509
510 if (!*++argv)
511 bb_show_usage();
512 xrtnl_open(&rth);
513 ret = EXIT_SUCCESS;
514
515 obj = index_in_substrings(objects, *argv++);
516
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200517 if (obj < 0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000518 bb_show_usage();
519 if (!*argv)
520 cmd = CMD_show; /* list is the default */
521 else {
522 cmd = index_in_substrings(commands, *argv);
523 if (cmd < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200524 invarg_1_to_2(*argv, argv[-1]);
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000525 argv++;
526 }
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100527
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000528 memset(&msg, 0, sizeof(msg));
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100529 if (AF_UNSPEC != 0)
530 msg.tcm_family = AF_UNSPEC;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000531 ll_init_map(&rth);
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100532
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000533 while (*argv) {
534 arg = index_in_substrings(args, *argv);
535 if (arg == ARG_dev) {
536 NEXT_ARG();
537 if (dev)
538 duparg2("dev", *argv);
539 dev = *argv++;
540 msg.tcm_ifindex = xll_name_to_index(dev);
541 if (cmd >= CMD_show)
542 filter_ifindex = msg.tcm_ifindex;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100543 } else
544 if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
545 || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
546 ) {
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000547 NEXT_ARG();
548 /* We don't care about duparg2("qdisc handle",*argv) for now */
549 if (get_qdisc_handle(&filter_qdisc, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200550 invarg_1_to_2(*argv, "qdisc");
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100551 } else
552 if (obj != OBJ_qdisc
553 && (arg == ARG_root
554 || arg == ARG_parent
555 || (obj == OBJ_filter && arg >= ARG_pref)
556 )
557 ) {
558 /* nothing */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000559 } else {
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200560 invarg_1_to_2(*argv, "command");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000561 }
562 NEXT_ARG();
563 if (arg == ARG_root) {
564 if (msg.tcm_parent)
565 duparg("parent", *argv);
566 msg.tcm_parent = TC_H_ROOT;
567 if (obj == OBJ_filter)
568 filter_parent = TC_H_ROOT;
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100569 } else
570 if (arg == ARG_parent) {
Daniel Fandrich6295d272011-06-09 15:44:44 -0700571 uint32_t handle;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000572 if (msg.tcm_parent)
573 duparg(*argv, "parent");
574 if (get_tc_classid(&handle, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200575 invarg_1_to_2(*argv, "parent");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000576 msg.tcm_parent = handle;
577 if (obj == OBJ_filter)
578 filter_parent = handle;
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100579 } else
580 if (arg == ARG_handle) { /* filter::list */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000581 if (msg.tcm_handle)
582 duparg(*argv, "handle");
583 /* reject LONG_MIN || LONG_MAX */
584 /* TODO: for fw
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100585 slash = strchr(handle, '/');
586 if (slash != NULL)
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100587 *slash = '\0';
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000588 */
Denis Vlasenko76140a72009-03-05 09:21:57 +0000589 msg.tcm_handle = get_u32(*argv, "handle");
Daniel Fandrich6295d272011-06-09 15:44:44 -0700590 /* if (slash) {if (get_u32(uint32_t &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100591 } else
592 if (arg == ARG_classid
593 && obj == OBJ_class
594 && cmd == CMD_change
595 ) {
596 /* TODO */
597 } else
598 if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000599 if (filter_prio)
600 duparg(*argv, "priority");
Denis Vlasenko76140a72009-03-05 09:21:57 +0000601 filter_prio = get_u32(*argv, "priority");
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100602 } else
603 if (arg == ARG_proto) { /* filter::list */
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200604 uint16_t tmp;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000605 if (filter_proto)
606 duparg(*argv, "protocol");
607 if (ll_proto_a2n(&tmp, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200608 invarg_1_to_2(*argv, "protocol");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000609 filter_proto = tmp;
610 }
611 }
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100612
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000613 if (cmd >= CMD_show) { /* show or list */
614 if (obj == OBJ_filter)
615 msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
616 if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
617 obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
618 &msg, sizeof(msg)) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100619 bb_simple_perror_msg_and_die("can't send dump request");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000620
621 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
622 obj == OBJ_class ? print_class : print_filter,
623 NULL);
624 }
625 if (ENABLE_FEATURE_CLEAN_UP) {
626 rtnl_close(&rth);
627 }
628 return ret;
629}