tc: less ugly check for bad sizeof(G)
[oweals/busybox.git] / networking / tc.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4  *
5  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  *
7  * Bernhard Reutner-Fischer adjusted for busybox
8  */
9
10 #include "libbb.h"
11
12 #include "libiproute/utils.h"
13 #include "libiproute/ip_common.h"
14 #include "libiproute/rt_names.h"
15 #include <linux/pkt_sched.h> /* for the TC_H_* macros */
16
17 #define parse_rtattr_nested(tb, max, rta) \
18         (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
19
20 /* nullifies tb on error */
21 #define __parse_rtattr_nested_compat(tb, max, rta, len) \
22         ({if ((RTA_PAYLOAD(rta) >= len) && \
23                  (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr))) { \
24                         rta = RTA_DATA(rta) + RTA_ALIGN(len); \
25                         parse_rtattr_nested(tb, max, rta); \
26           } else \
27                         memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
28         })
29
30 #define parse_rtattr_nested_compat(tb, max, rta, data, len) \
31         ({data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
32         __parse_rtattr_nested_compat(tb, max, rta, len); })
33
34 #define show_details (0) /* not implemented. Does anyone need it? */
35 #define use_iec (0) /* not currently documented in the upstream manpage */
36
37
38 struct globals {
39         int filter_ifindex;
40         __u32 filter_qdisc;
41         __u32 filter_parent;
42         __u32 filter_prio;
43         __u32 filter_proto;
44 } FIX_ALIASING;
45 #define G (*(struct globals*)&bb_common_bufsiz1)
46 struct BUG_G_too_big {
47         char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
48 };
49 #define filter_ifindex (G.filter_ifindex)
50 #define filter_qdisc (G.filter_qdisc)
51 #define filter_parent (G.filter_parent)
52 #define filter_prio (G.filter_prio)
53 #define filter_proto (G.filter_proto)
54 #define INIT_G() do { } while (0)
55
56 /* Allocates a buffer containing the name of a class id.
57  * The caller must free the returned memory.  */
58 static char* print_tc_classid(uint32_t cid)
59 {
60 #if 0 /* IMPOSSIBLE */
61         if (cid == TC_H_ROOT)
62                 return xasprintf("root");
63         else
64 #endif
65         if (cid == TC_H_UNSPEC)
66                 return xasprintf("none");
67         else if (TC_H_MAJ(cid) == 0)
68                 return xasprintf(":%x", TC_H_MIN(cid));
69         else if (TC_H_MIN(cid) == 0)
70                 return xasprintf("%x:", TC_H_MAJ(cid)>>16);
71         else
72                 return xasprintf("%x:%x", TC_H_MAJ(cid)>>16, TC_H_MIN(cid));
73 }
74
75 /* Get a qdisc handle.  Return 0 on success, !0 otherwise.  */
76 static int get_qdisc_handle(__u32 *h, const char *str) {
77         __u32 maj;
78         char *p;
79
80         maj = TC_H_UNSPEC;
81         if (!strcmp(str, "none"))
82                 goto ok;
83         maj = strtoul(str, &p, 16);
84         if (p == str)
85                 return 1;
86         maj <<= 16;
87         if (*p != ':' && *p != '\0')
88                 return 1;
89  ok:
90         *h = maj;
91         return 0;
92 }
93
94 /* Get class ID.  Return 0 on success, !0 otherwise.  */
95 static int get_tc_classid(__u32 *h, const char *str) {
96         __u32 maj, min;
97         char *p;
98
99         maj = TC_H_ROOT;
100         if (!strcmp(str, "root"))
101                 goto ok;
102         maj = TC_H_UNSPEC;
103         if (!strcmp(str, "none"))
104                 goto ok;
105         maj = strtoul(str, &p, 16);
106         if (p == str) {
107                 if (*p != ':')
108                         return 1;
109                 maj = 0;
110         }
111         if (*p == ':') {
112                 if (maj >= (1<<16))
113                         return 1;
114                 maj <<= 16;
115                 str = p + 1;
116                 min = strtoul(str, &p, 16);
117 //FIXME: check for "" too?
118                 if (*p != '\0' || min >= (1<<16))
119                         return 1;
120                 maj |= min;
121         } else if (*p != 0)
122                 return 1;
123  ok:
124         *h = maj;
125         return 0;
126 }
127
128 static void print_rate(char *buf, int len, uint32_t rate)
129 {
130         double tmp = (double)rate*8;
131
132         if (use_iec) {
133                 if (tmp >= 1000.0*1024.0*1024.0)
134                         snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0);
135                 else if (tmp >= 1000.0*1024)
136                         snprintf(buf, len, "%.0fKibit", tmp/1024);
137                 else
138                         snprintf(buf, len, "%.0fbit", tmp);
139         } else {
140                 if (tmp >= 1000.0*1000000.0)
141                         snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
142                 else if (tmp >= 1000.0 * 1000.0)
143                         snprintf(buf, len, "%.0fKbit", tmp/1000.0);
144                 else
145                         snprintf(buf, len, "%.0fbit",  tmp);
146         }
147 }
148
149 /* This is "pfifo_fast".  */
150 static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
151 {
152         return 0;
153 }
154 static int prio_print_opt(struct rtattr *opt)
155 {
156         int i;
157         struct tc_prio_qopt *qopt;
158         struct rtattr *tb[TCA_PRIO_MAX+1];
159
160         if (opt == NULL)
161                 return 0;
162         parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt));
163         if (tb == NULL)
164                 return 0;
165         printf("bands %u priomap ", qopt->bands);
166         for (i=0; i<=TC_PRIO_MAX; i++)
167                 printf(" %d", qopt->priomap[i]);
168
169         if (tb[TCA_PRIO_MQ])
170                 printf(" multiqueue: o%s ",
171                     *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
172
173         return 0;
174 }
175
176 /* Class Based Queue */
177 static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
178 {
179         return 0;
180 }
181 static int cbq_print_opt(struct rtattr *opt)
182 {
183         struct rtattr *tb[TCA_CBQ_MAX+1];
184         struct tc_ratespec *r = NULL;
185         struct tc_cbq_lssopt *lss = NULL;
186         struct tc_cbq_wrropt *wrr = NULL;
187         struct tc_cbq_fopt *fopt = NULL;
188         struct tc_cbq_ovl *ovl = NULL;
189         const char *const error = "CBQ: too short %s opt";
190         char buf[64];
191
192         if (opt == NULL)
193                 goto done;
194         parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
195
196         if (tb[TCA_CBQ_RATE]) {
197                 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
198                         bb_error_msg(error, "rate");
199                 else
200                         r = RTA_DATA(tb[TCA_CBQ_RATE]);
201         }
202         if (tb[TCA_CBQ_LSSOPT]) {
203                 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
204                         bb_error_msg(error, "lss");
205                 else
206                         lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
207         }
208         if (tb[TCA_CBQ_WRROPT]) {
209                 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
210                         bb_error_msg(error, "wrr");
211                 else
212                         wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
213         }
214         if (tb[TCA_CBQ_FOPT]) {
215                 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
216                         bb_error_msg(error, "fopt");
217                 else
218                         fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
219         }
220         if (tb[TCA_CBQ_OVL_STRATEGY]) {
221                 if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl))
222                         bb_error_msg("CBQ: too short overlimit strategy %u/%u",
223                                 (unsigned) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]),
224                                 (unsigned) sizeof(*ovl));
225                 else
226                         ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
227         }
228
229         if (r) {
230                 print_rate(buf, sizeof(buf), r->rate);
231                 printf("rate %s ", buf);
232                 if (show_details) {
233                         printf("cell %ub ", 1<<r->cell_log);
234                         if (r->mpu)
235                                 printf("mpu %ub ", r->mpu);
236                         if (r->overhead)
237                                 printf("overhead %ub ", r->overhead);
238                 }
239         }
240         if (lss && lss->flags) {
241                 bool comma = false;
242                 bb_putchar('(');
243                 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
244                         printf("bounded");
245                         comma = true;
246                 }
247                 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
248                         if (comma)
249                                 bb_putchar(',');
250                         printf("isolated");
251                 }
252                 printf(") ");
253         }
254         if (wrr) {
255                 if (wrr->priority != TC_CBQ_MAXPRIO)
256                         printf("prio %u", wrr->priority);
257                 else
258                         printf("prio no-transmit");
259                 if (show_details) {
260                         printf("/%u ", wrr->cpriority);
261                         if (wrr->weight != 1) {
262                                 print_rate(buf, sizeof(buf), wrr->weight);
263                                 printf("weight %s ", buf);
264                         }
265                         if (wrr->allot)
266                                 printf("allot %ub ", wrr->allot);
267                 }
268         }
269  done:
270         return 0;
271 }
272
273 static int print_qdisc(const struct sockaddr_nl *who UNUSED_PARAM,
274                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
275 {
276         struct tcmsg *msg = NLMSG_DATA(hdr);
277         int len = hdr->nlmsg_len;
278         struct rtattr * tb[TCA_MAX+1];
279         char *name;
280
281         if (hdr->nlmsg_type != RTM_NEWQDISC && hdr->nlmsg_type != RTM_DELQDISC) {
282                 /* bb_error_msg("not a qdisc"); */
283                 return 0; /* ??? mimic upstream; should perhaps return -1 */
284         }
285         len -= NLMSG_LENGTH(sizeof(*msg));
286         if (len < 0) {
287                 /* bb_error_msg("wrong len %d", len); */
288                 return -1;
289         }
290         /* not the desired interface? */
291         if (filter_ifindex && filter_ifindex != msg->tcm_ifindex)
292                 return 0;
293         memset (tb, 0, sizeof(tb));
294         parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
295         if (tb[TCA_KIND] == NULL) {
296                 /* bb_error_msg("%s: NULL kind", "qdisc"); */
297                 return -1;
298         }
299         if (hdr->nlmsg_type == RTM_DELQDISC)
300                 printf("deleted ");
301         name = (char*)RTA_DATA(tb[TCA_KIND]);
302         printf("qdisc %s %x: ", name, msg->tcm_handle>>16);
303         if (filter_ifindex == 0)
304                 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
305         if (msg->tcm_parent == TC_H_ROOT)
306                 printf("root ");
307         else if (msg->tcm_parent) {
308                 char *classid = print_tc_classid(msg->tcm_parent);
309                 printf("parent %s ", classid);
310                 if (ENABLE_FEATURE_CLEAN_UP)
311                         free(classid);
312         }
313         if (msg->tcm_info != 1)
314                 printf("refcnt %d ", msg->tcm_info);
315         if (tb[TCA_OPTIONS]) {
316                 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
317                 int qqq = index_in_strings(_q_, name);
318                 if (qqq == 0) { /* pfifo_fast aka prio */
319                         prio_print_opt(tb[TCA_OPTIONS]);
320                 } else if (qqq == 1) { /* class based queuing */
321                         cbq_print_opt(tb[TCA_OPTIONS]);
322                 } else
323                         bb_error_msg("unknown %s", name);
324         }
325         bb_putchar('\n');
326         return 0;
327 }
328
329 static int print_class(const struct sockaddr_nl *who UNUSED_PARAM,
330                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
331 {
332         struct tcmsg *msg = NLMSG_DATA(hdr);
333         int len = hdr->nlmsg_len;
334         struct rtattr * tb[TCA_MAX+1];
335         char *name, *classid;
336
337         /*XXX Eventually factor out common code */
338
339         if (hdr->nlmsg_type != RTM_NEWTCLASS && hdr->nlmsg_type != RTM_DELTCLASS) {
340                 /* bb_error_msg("not a class"); */
341                 return 0; /* ??? mimic upstream; should perhaps return -1 */
342         }
343         len -= NLMSG_LENGTH(sizeof(*msg));
344         if (len < 0) {
345                 /* bb_error_msg("wrong len %d", len); */
346                 return -1;
347         }
348         /* not the desired interface? */
349         if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc))
350                 return 0;
351         memset (tb, 0, sizeof(tb));
352         parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
353         if (tb[TCA_KIND] == NULL) {
354                 /* bb_error_msg("%s: NULL kind", "class"); */
355                 return -1;
356         }
357         if (hdr->nlmsg_type == RTM_DELTCLASS)
358                 printf("deleted ");
359
360         name = (char*)RTA_DATA(tb[TCA_KIND]);
361         classid = !msg->tcm_handle ? NULL : print_tc_classid(
362                                 filter_qdisc ? TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
363         printf ("class %s %s", name, classid);
364         if (ENABLE_FEATURE_CLEAN_UP)
365                 free(classid);
366
367         if (filter_ifindex == 0)
368                 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
369         if (msg->tcm_parent == TC_H_ROOT)
370                 printf("root ");
371         else if (msg->tcm_parent) {
372                 classid = print_tc_classid(filter_qdisc ?
373                                                                    TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
374                 printf("parent %s ", classid);
375                 if (ENABLE_FEATURE_CLEAN_UP)
376                         free(classid);
377         }
378         if (msg->tcm_info)
379                 printf("leaf %x ", msg->tcm_info >> 16);
380         /* Do that get_qdisc_kind(RTA_DATA(tb[TCA_KIND])).  */
381         if (tb[TCA_OPTIONS]) {
382                 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
383                 int qqq = index_in_strings(_q_, name);
384                 if (qqq == 0) { /* pfifo_fast aka prio */
385                         /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/
386                 } else if (qqq == 1) { /* class based queuing */
387                         /* cbq_print_copt() is identical to cbq_print_opt(). */
388                         cbq_print_opt(tb[TCA_OPTIONS]);
389                 } else
390                         bb_error_msg("unknown %s", name);
391         }
392         bb_putchar('\n');
393
394         return 0;
395 }
396
397 static int print_filter(const struct sockaddr_nl *who UNUSED_PARAM,
398                                                 struct nlmsghdr *hdr, void *arg UNUSED_PARAM)
399 {
400         struct tcmsg *msg = NLMSG_DATA(hdr);
401         int len = hdr->nlmsg_len;
402         struct rtattr * tb[TCA_MAX+1];
403         return 0;
404 }
405
406 int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
407 int tc_main(int argc UNUSED_PARAM, char **argv)
408 {
409         static const char objects[] ALIGN1 =
410                 "qdisc\0""class\0""filter\0"
411                 ;
412         enum { OBJ_qdisc = 0, OBJ_class, OBJ_filter };
413         static const char commands[] ALIGN1 =
414                 "add\0""delete\0""change\0"
415                 "link\0" /* only qdisc */
416                 "replace\0"
417                 "show\0""list\0"
418                 ;
419         static const char args[] ALIGN1 =
420                 "dev\0" /* qdisc, class, filter */
421                 "root\0" /* class, filter */
422                 "parent\0" /* class, filter */
423                 "qdisc\0" /* class */
424                 "handle\0" /* change: qdisc, class(classid) list: filter */
425                 "classid\0" /* change: for class use "handle" */
426                 "preference\0""priority\0""protocol\0" /* filter */
427                 ;
428         enum { CMD_add = 0, CMD_del, CMD_change, CMD_link, CMD_replace, CMD_show };
429         enum { ARG_dev = 0, ARG_root, ARG_parent, ARG_qdisc,
430                         ARG_handle, ARG_classid, ARG_pref, ARG_prio, ARG_proto};
431         struct rtnl_handle rth;
432         struct tcmsg msg;
433         int ret, obj, cmd, arg;
434         char *dev = NULL;
435
436         INIT_G();
437
438         if (!*++argv)
439                 bb_show_usage();
440         xrtnl_open(&rth);
441         ret = EXIT_SUCCESS;
442
443         obj = index_in_substrings(objects, *argv++);
444
445         if (obj < OBJ_qdisc)
446                 bb_show_usage();
447         if (!*argv)
448                 cmd = CMD_show; /* list is the default */
449         else {
450                 cmd = index_in_substrings(commands, *argv);
451                 if (cmd < 0)
452                         bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
453                 argv++;
454         }
455         memset(&msg, 0, sizeof(msg));
456         msg.tcm_family = AF_UNSPEC;
457         ll_init_map(&rth);
458         while (*argv) {
459                 arg = index_in_substrings(args, *argv);
460                 if (arg == ARG_dev) {
461                         NEXT_ARG();
462                         if (dev)
463                                 duparg2("dev", *argv);
464                         dev = *argv++;
465                         msg.tcm_ifindex = xll_name_to_index(dev);
466                         if (cmd >= CMD_show)
467                                 filter_ifindex = msg.tcm_ifindex;
468                 } else
469                 if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
470                  || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
471                 ) {
472                         NEXT_ARG();
473                         /* We don't care about duparg2("qdisc handle",*argv) for now */
474                         if (get_qdisc_handle(&filter_qdisc, *argv))
475                                 invarg(*argv, "qdisc");
476                 } else
477                 if (obj != OBJ_qdisc
478                  && (arg == ARG_root
479                     || arg == ARG_parent
480                     || (obj == OBJ_filter && arg >= ARG_pref)
481                     )
482                 ) {
483                         /* nothing */
484                 } else {
485                         invarg(*argv, "command");
486                 }
487                 NEXT_ARG();
488                 if (arg == ARG_root) {
489                         if (msg.tcm_parent)
490                                 duparg("parent", *argv);
491                         msg.tcm_parent = TC_H_ROOT;
492                         if (obj == OBJ_filter)
493                                 filter_parent = TC_H_ROOT;
494                 } else if (arg == ARG_parent) {
495                         __u32 handle;
496                         if (msg.tcm_parent)
497                                 duparg(*argv, "parent");
498                         if (get_tc_classid(&handle, *argv))
499                                 invarg(*argv, "parent");
500                         msg.tcm_parent = handle;
501                         if (obj == OBJ_filter)
502                                 filter_parent = handle;
503                 } else if (arg == ARG_handle) { /* filter::list */
504                         if (msg.tcm_handle)
505                                 duparg(*argv, "handle");
506                         /* reject LONG_MIN || LONG_MAX */
507                         /* TODO: for fw
508                            if ((slash = strchr(handle, '/')) != NULL)
509                                    *slash = '\0';
510                          */
511                         msg.tcm_handle = get_u32(*argv, "handle");
512                         /* if (slash) {if (get_u32(__u32 &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
513                 } else if (arg == ARG_classid && obj == OBJ_class && cmd == CMD_change){
514                 } else if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
515                         if (filter_prio)
516                                 duparg(*argv, "priority");
517                         filter_prio = get_u32(*argv, "priority");
518                 } else if (arg == ARG_proto) { /* filter::list */
519                         uint16_t tmp;
520                         if (filter_proto)
521                                 duparg(*argv, "protocol");
522                         if (ll_proto_a2n(&tmp, *argv))
523                                 invarg(*argv, "protocol");
524                         filter_proto = tmp;
525                 }
526         }
527         if (cmd >= CMD_show) { /* show or list */
528                 if (obj == OBJ_filter)
529                         msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
530                 if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
531                                                 obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
532                                                 &msg, sizeof(msg)) < 0)
533                         bb_simple_perror_msg_and_die("can't send dump request");
534
535                 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
536                                                 obj == OBJ_class ? print_class : print_filter,
537                                                 NULL);
538         }
539         if (ENABLE_FEATURE_CLEAN_UP) {
540                 rtnl_close(&rth);
541         }
542         return ret;
543 }