]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_action.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / tc / m_action.c
1 /*
2 * m_action.c Action Management
3 *
4 * This program is free software; you can distribute 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: J Hadi Salim (hadi@cyberus.ca)
10 *
11 * TODO:
12 * - parse to be passed a filedescriptor for logging purposes
13 *
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <fcntl.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <string.h>
25 #include <dlfcn.h>
26
27 #include "utils.h"
28 #include "tc_common.h"
29 #include "tc_util.h"
30
31 static struct action_util *action_list;
32 #ifdef CONFIG_GACT
33 int gact_ld; /* f*ckin backward compatibility */
34 #endif
35 int tab_flush;
36
37 static void act_usage(void)
38 {
39 /*XXX: In the near future add a action->print_help to improve
40 * usability
41 * This would mean new tc will not be backward compatible
42 * with any action .so from the old days. But if someone really
43 * does that, they would know how to fix this ..
44 *
45 */
46 fprintf(stderr, "usage: tc actions <ACTSPECOP>*\n");
47 fprintf(stderr,
48 "Where: \tACTSPECOP := ACR | GD | FL\n"
49 "\tACR := add | change | replace <ACTSPEC>*\n"
50 "\tGD := get | delete | <ACTISPEC>*\n"
51 "\tFL := ls | list | flush | <ACTNAMESPEC>\n"
52 "\tACTNAMESPEC := action <ACTNAME>\n"
53 "\tACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
54 "\tACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
55 "\tINDEXSPEC := index <32 bit indexvalue>\n"
56 "\tACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
57 "\t\tExample ACTNAME is gact, mirred, bpf, etc\n"
58 "\t\tEach action has its own parameters (ACTPARAMS)\n"
59 "\n");
60
61 exit(-1);
62 }
63
64 static int print_noaopt(struct action_util *au, FILE *f, struct rtattr *opt)
65 {
66 if (opt && RTA_PAYLOAD(opt))
67 fprintf(f, "[Unknown action, optlen=%u] ",
68 (unsigned int) RTA_PAYLOAD(opt));
69 return 0;
70 }
71
72 static int parse_noaopt(struct action_util *au, int *argc_p, char ***argv_p, int code, struct nlmsghdr *n)
73 {
74 int argc = *argc_p;
75 char **argv = *argv_p;
76
77 if (argc) {
78 fprintf(stderr, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au->id, *argv);
79 } else {
80 fprintf(stderr, "Unknown action \"%s\"\n", au->id);
81 }
82 return -1;
83 }
84
85 static struct action_util *get_action_kind(char *str)
86 {
87 static void *aBODY;
88 void *dlh;
89 char buf[256];
90 struct action_util *a;
91 #ifdef CONFIG_GACT
92 int looked4gact = 0;
93 restart_s:
94 #endif
95 for (a = action_list; a; a = a->next) {
96 if (strcmp(a->id, str) == 0)
97 return a;
98 }
99
100 snprintf(buf, sizeof(buf), "%s/m_%s.so", get_tc_lib(), str);
101 dlh = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
102 if (dlh == NULL) {
103 dlh = aBODY;
104 if (dlh == NULL) {
105 dlh = aBODY = dlopen(NULL, RTLD_LAZY);
106 if (dlh == NULL)
107 goto noexist;
108 }
109 }
110
111 snprintf(buf, sizeof(buf), "%s_action_util", str);
112 a = dlsym(dlh, buf);
113 if (a == NULL)
114 goto noexist;
115
116 reg:
117 a->next = action_list;
118 action_list = a;
119 return a;
120
121 noexist:
122 #ifdef CONFIG_GACT
123 if (!looked4gact) {
124 looked4gact = 1;
125 strcpy(str, "gact");
126 goto restart_s;
127 }
128 #endif
129 a = calloc(1, sizeof(*a));
130 if (a) {
131 strncpy(a->id, "noact", 15);
132 a->parse_aopt = parse_noaopt;
133 a->print_aopt = print_noaopt;
134 goto reg;
135 }
136 return a;
137 }
138
139 static int
140 new_cmd(char **argv)
141 {
142 if ((matches(*argv, "change") == 0) ||
143 (matches(*argv, "replace") == 0) ||
144 (matches(*argv, "delete") == 0) ||
145 (matches(*argv, "get") == 0) ||
146 (matches(*argv, "add") == 0))
147 return 1;
148
149 return 0;
150
151 }
152
153 int parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
154 {
155 int argc = *argc_p;
156 char **argv = *argv_p;
157 struct rtattr *tail, *tail2;
158 char k[16];
159 int act_ck_len = 0;
160 int ok = 0;
161 int eap = 0; /* expect action parameters */
162
163 int ret = 0;
164 int prio = 0;
165 unsigned char act_ck[TC_COOKIE_MAX_SIZE];
166
167 if (argc <= 0)
168 return -1;
169
170 tail = tail2 = NLMSG_TAIL(n);
171
172 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
173
174 while (argc > 0) {
175
176 memset(k, 0, sizeof(k));
177
178 if (strcmp(*argv, "action") == 0) {
179 argc--;
180 argv++;
181 eap = 1;
182 #ifdef CONFIG_GACT
183 if (!gact_ld) {
184 get_action_kind("gact");
185 }
186 #endif
187 continue;
188 } else if (strcmp(*argv, "flowid") == 0) {
189 break;
190 } else if (strcmp(*argv, "classid") == 0) {
191 break;
192 } else if (strcmp(*argv, "help") == 0) {
193 return -1;
194 } else if (new_cmd(argv)) {
195 goto done0;
196 } else {
197 struct action_util *a = NULL;
198
199 strncpy(k, *argv, sizeof(k) - 1);
200 eap = 0;
201 if (argc > 0) {
202 a = get_action_kind(k);
203 } else {
204 done0:
205 if (ok)
206 break;
207 else
208 goto done;
209 }
210
211 if (a == NULL) {
212 goto bad_val;
213 }
214
215 tail = NLMSG_TAIL(n);
216 addattr_l(n, MAX_MSG, ++prio, NULL, 0);
217 addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
218
219 ret = a->parse_aopt(a, &argc, &argv, TCA_ACT_OPTIONS,
220 n);
221
222 if (ret < 0) {
223 fprintf(stderr, "bad action parsing\n");
224 goto bad_val;
225 }
226
227 if (*argv && strcmp(*argv, "cookie") == 0) {
228 size_t slen;
229
230 NEXT_ARG();
231 slen = strlen(*argv);
232 if (slen > TC_COOKIE_MAX_SIZE * 2) {
233 char cookie_err_m[128];
234
235 snprintf(cookie_err_m, 128,
236 "%zd Max allowed size %d",
237 slen, TC_COOKIE_MAX_SIZE*2);
238 invarg(cookie_err_m, *argv);
239 }
240
241 if (hex2mem(*argv, act_ck, slen / 2) < 0)
242 invarg("cookie must be a hex string\n",
243 *argv);
244
245 act_ck_len = slen;
246 argc--;
247 argv++;
248 }
249
250 if (act_ck_len)
251 addattr_l(n, MAX_MSG, TCA_ACT_COOKIE,
252 &act_ck, act_ck_len);
253
254 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
255 ok++;
256 }
257 }
258
259 if (eap > 0) {
260 fprintf(stderr, "bad action empty %d\n", eap);
261 goto bad_val;
262 }
263
264 tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
265
266 done:
267 *argc_p = argc;
268 *argv_p = argv;
269 return 0;
270 bad_val:
271 /* no need to undo things, returning from here should
272 * cause enough pain */
273 fprintf(stderr, "parse_action: bad value (%d:%s)!\n", argc, *argv);
274 return -1;
275 }
276
277 static int tc_print_one_action(FILE *f, struct rtattr *arg)
278 {
279
280 struct rtattr *tb[TCA_ACT_MAX + 1];
281 int err = 0;
282 struct action_util *a = NULL;
283
284 if (arg == NULL)
285 return -1;
286
287 parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
288
289 if (tb[TCA_ACT_KIND] == NULL) {
290 fprintf(stderr, "NULL Action!\n");
291 return -1;
292 }
293
294
295 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
296 if (a == NULL)
297 return err;
298
299 err = a->print_aopt(a, f, tb[TCA_ACT_OPTIONS]);
300
301 if (err < 0)
302 return err;
303
304 if (show_stats && tb[TCA_ACT_STATS]) {
305
306 fprintf(f, "\tAction statistics:\n");
307 print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
308 if (tb[TCA_ACT_COOKIE]) {
309 int strsz = RTA_PAYLOAD(tb[TCA_ACT_COOKIE]);
310 char b1[strsz+1];
311
312 fprintf(f, "\n\tcookie len %d %s ", strsz,
313 hexstring_n2a(RTA_DATA(tb[TCA_ACT_COOKIE]),
314 strsz, b1, sizeof(b1)));
315 }
316 fprintf(f, "\n");
317 }
318
319 return 0;
320 }
321
322 static int
323 tc_print_action_flush(FILE *f, const struct rtattr *arg)
324 {
325
326 struct rtattr *tb[TCA_MAX + 1];
327 int err = 0;
328 struct action_util *a = NULL;
329 __u32 *delete_count = 0;
330
331 parse_rtattr_nested(tb, TCA_MAX, arg);
332
333 if (tb[TCA_KIND] == NULL) {
334 fprintf(stderr, "NULL Action!\n");
335 return -1;
336 }
337
338 a = get_action_kind(RTA_DATA(tb[TCA_KIND]));
339 if (a == NULL)
340 return err;
341
342 delete_count = RTA_DATA(tb[TCA_FCNT]);
343 fprintf(f, " %s (%d entries)\n", a->id, *delete_count);
344 tab_flush = 0;
345 return 0;
346 }
347
348 int
349 tc_print_action(FILE *f, const struct rtattr *arg, unsigned short tot_acts)
350 {
351
352 int i;
353
354 if (arg == NULL)
355 return 0;
356
357 if (!tot_acts)
358 tot_acts = TCA_ACT_MAX_PRIO;
359
360 struct rtattr *tb[tot_acts + 1];
361
362 parse_rtattr_nested(tb, tot_acts, arg);
363
364 if (tab_flush && NULL != tb[0] && NULL == tb[1])
365 return tc_print_action_flush(f, tb[0]);
366
367 for (i = 0; i < tot_acts; i++) {
368 if (tb[i]) {
369 fprintf(f, "\n\taction order %d: ", i);
370 if (tc_print_one_action(f, tb[i]) < 0) {
371 fprintf(f, "Error printing action\n");
372 }
373 }
374
375 }
376
377 return 0;
378 }
379
380 int print_action(const struct sockaddr_nl *who,
381 struct nlmsghdr *n,
382 void *arg)
383 {
384 FILE *fp = (FILE *)arg;
385 struct tcamsg *t = NLMSG_DATA(n);
386 int len = n->nlmsg_len;
387 __u32 *tot_acts = NULL;
388 struct rtattr *tb[TCA_ROOT_MAX+1];
389
390 len -= NLMSG_LENGTH(sizeof(*t));
391
392 if (len < 0) {
393 fprintf(stderr, "Wrong len %d\n", len);
394 return -1;
395 }
396
397 parse_rtattr(tb, TCA_ROOT_MAX, TA_RTA(t), len);
398
399 if (tb[TCA_ROOT_COUNT])
400 tot_acts = RTA_DATA(tb[TCA_ROOT_COUNT]);
401
402 fprintf(fp, "total acts %d\n", tot_acts ? *tot_acts:0);
403 if (tb[TCA_ACT_TAB] == NULL) {
404 if (n->nlmsg_type != RTM_GETACTION)
405 fprintf(stderr, "print_action: NULL kind\n");
406 return -1;
407 }
408
409 if (n->nlmsg_type == RTM_DELACTION) {
410 if (n->nlmsg_flags & NLM_F_ROOT) {
411 fprintf(fp, "Flushed table ");
412 tab_flush = 1;
413 } else {
414 fprintf(fp, "Deleted action ");
415 }
416 }
417
418 if (n->nlmsg_type == RTM_NEWACTION) {
419 if ((n->nlmsg_flags & NLM_F_CREATE) &&
420 !(n->nlmsg_flags & NLM_F_REPLACE)) {
421 fprintf(fp, "Added action ");
422 } else if (n->nlmsg_flags & NLM_F_REPLACE) {
423 fprintf(fp, "Replaced action ");
424 }
425 }
426
427
428 tc_print_action(fp, tb[TCA_ACT_TAB], tot_acts ? *tot_acts:0);
429
430 return 0;
431 }
432
433 static int tc_action_gd(int cmd, unsigned int flags, int *argc_p, char ***argv_p)
434 {
435 char k[16];
436 struct action_util *a = NULL;
437 int argc = *argc_p;
438 char **argv = *argv_p;
439 int prio = 0;
440 int ret = 0;
441 __u32 i = 0;
442 struct rtattr *tail;
443 struct rtattr *tail2;
444 struct nlmsghdr *ans = NULL;
445
446 struct {
447 struct nlmsghdr n;
448 struct tcamsg t;
449 char buf[MAX_MSG];
450 } req = {
451 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
452 .n.nlmsg_flags = NLM_F_REQUEST | flags,
453 .n.nlmsg_type = cmd,
454 .t.tca_family = AF_UNSPEC,
455 };
456
457 argc -= 1;
458 argv += 1;
459
460
461 tail = NLMSG_TAIL(&req.n);
462 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
463
464 while (argc > 0) {
465 if (strcmp(*argv, "action") == 0) {
466 argc--;
467 argv++;
468 continue;
469 } else if (strcmp(*argv, "help") == 0) {
470 return -1;
471 }
472
473 strncpy(k, *argv, sizeof(k) - 1);
474 a = get_action_kind(k);
475 if (a == NULL) {
476 fprintf(stderr, "Error: non existent action: %s\n", k);
477 ret = -1;
478 goto bad_val;
479 }
480 if (strcmp(a->id, k) != 0) {
481 fprintf(stderr, "Error: non existent action: %s\n", k);
482 ret = -1;
483 goto bad_val;
484 }
485
486 argc -= 1;
487 argv += 1;
488 if (argc <= 0) {
489 fprintf(stderr, "Error: no index specified action: %s\n", k);
490 ret = -1;
491 goto bad_val;
492 }
493
494 if (matches(*argv, "index") == 0) {
495 NEXT_ARG();
496 if (get_u32(&i, *argv, 10)) {
497 fprintf(stderr, "Illegal \"index\"\n");
498 ret = -1;
499 goto bad_val;
500 }
501 argc -= 1;
502 argv += 1;
503 } else {
504 fprintf(stderr, "Error: no index specified action: %s\n", k);
505 ret = -1;
506 goto bad_val;
507 }
508
509 tail2 = NLMSG_TAIL(&req.n);
510 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
511 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
512 if (i > 0)
513 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
514 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
515
516 }
517
518 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
519
520 req.n.nlmsg_seq = rth.dump = ++rth.seq;
521 if (cmd == RTM_GETACTION)
522 ans = &req.n;
523
524 if (rtnl_talk(&rth, &req.n, ans, MAX_MSG) < 0) {
525 fprintf(stderr, "We have an error talking to the kernel\n");
526 return 1;
527 }
528
529 if (ans && print_action(NULL, &req.n, (void *)stdout) < 0) {
530 fprintf(stderr, "Dump terminated\n");
531 return 1;
532 }
533
534 *argc_p = argc;
535 *argv_p = argv;
536 bad_val:
537 return ret;
538 }
539
540 static int tc_action_modify(int cmd, unsigned int flags, int *argc_p, char ***argv_p)
541 {
542 int argc = *argc_p;
543 char **argv = *argv_p;
544 int ret = 0;
545 struct {
546 struct nlmsghdr n;
547 struct tcamsg t;
548 char buf[MAX_MSG];
549 } req = {
550 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
551 .n.nlmsg_flags = NLM_F_REQUEST | flags,
552 .n.nlmsg_type = cmd,
553 .t.tca_family = AF_UNSPEC,
554 };
555 struct rtattr *tail = NLMSG_TAIL(&req.n);
556
557 argc -= 1;
558 argv += 1;
559 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
560 fprintf(stderr, "Illegal \"action\"\n");
561 return -1;
562 }
563 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
564
565 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
566 fprintf(stderr, "We have an error talking to the kernel\n");
567 ret = -1;
568 }
569
570 *argc_p = argc;
571 *argv_p = argv;
572
573 return ret;
574 }
575
576 static int tc_act_list_or_flush(int *argc_p, char ***argv_p, int event)
577 {
578 struct rtattr *tail, *tail2, *tail3, *tail4;
579 int ret = 0, prio = 0, msg_size = 0;
580 struct action_util *a = NULL;
581 struct nla_bitfield32 flag_select = { 0 };
582 char **argv = *argv_p;
583 __u32 msec_since = 0;
584 int argc = *argc_p;
585 char k[16];
586 struct {
587 struct nlmsghdr n;
588 struct tcamsg t;
589 char buf[MAX_MSG];
590 } req = {
591 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
592 .t.tca_family = AF_UNSPEC,
593 };
594
595 tail = NLMSG_TAIL(&req.n);
596 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
597 tail2 = NLMSG_TAIL(&req.n);
598
599 strncpy(k, *argv, sizeof(k) - 1);
600 #ifdef CONFIG_GACT
601 if (!gact_ld) {
602 get_action_kind("gact");
603 }
604 #endif
605 a = get_action_kind(k);
606 if (a == NULL) {
607 fprintf(stderr, "bad action %s\n", k);
608 goto bad_val;
609 }
610 if (strcmp(a->id, k) != 0) {
611 fprintf(stderr, "bad action %s\n", k);
612 goto bad_val;
613 }
614 strncpy(k, *argv, sizeof(k) - 1);
615
616 argc -= 1;
617 argv += 1;
618
619 if (argc && (strcmp(*argv, "since") == 0)) {
620 NEXT_ARG();
621 if (get_u32(&msec_since, *argv, 0))
622 invarg("dump time \"since\" is invalid", *argv);
623 }
624
625 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
626 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
627 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
628 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
629
630 tail3 = NLMSG_TAIL(&req.n);
631 flag_select.value |= TCA_FLAG_LARGE_DUMP_ON;
632 flag_select.selector |= TCA_FLAG_LARGE_DUMP_ON;
633 addattr_l(&req.n, MAX_MSG, TCA_ROOT_FLAGS, &flag_select,
634 sizeof(struct nla_bitfield32));
635 tail3->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail3;
636 if (msec_since) {
637 tail4 = NLMSG_TAIL(&req.n);
638 addattr32(&req.n, MAX_MSG, TCA_ROOT_TIME_DELTA, msec_since);
639 tail4->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail4;
640 }
641 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
642
643 if (event == RTM_GETACTION) {
644 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
645 perror("Cannot send dump request");
646 return 1;
647 }
648 ret = rtnl_dump_filter(&rth, print_action, stdout);
649 }
650
651 if (event == RTM_DELACTION) {
652 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
653 req.n.nlmsg_type = RTM_DELACTION;
654 req.n.nlmsg_flags |= NLM_F_ROOT;
655 req.n.nlmsg_flags |= NLM_F_REQUEST;
656 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
657 fprintf(stderr, "We have an error flushing\n");
658 return 1;
659 }
660
661 }
662
663 bad_val:
664
665 *argc_p = argc;
666 *argv_p = argv;
667 return ret;
668 }
669
670 int do_action(int argc, char **argv)
671 {
672
673 int ret = 0;
674
675 while (argc > 0) {
676
677 if (matches(*argv, "add") == 0) {
678 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
679 } else if (matches(*argv, "change") == 0 ||
680 matches(*argv, "replace") == 0) {
681 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
682 } else if (matches(*argv, "delete") == 0) {
683 argc -= 1;
684 argv += 1;
685 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
686 } else if (matches(*argv, "get") == 0) {
687 argc -= 1;
688 argv += 1;
689 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
690 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
691 || matches(*argv, "lst") == 0) {
692 if (argc <= 2) {
693 act_usage();
694 return -1;
695 }
696
697 argc -= 2;
698 argv += 2;
699 return tc_act_list_or_flush(&argc, &argv,
700 RTM_GETACTION);
701 } else if (matches(*argv, "flush") == 0) {
702 if (argc <= 2) {
703 act_usage();
704 return -1;
705 }
706
707 argc -= 2;
708 argv += 2;
709 return tc_act_list_or_flush(&argc, &argv,
710 RTM_DELACTION);
711 } else if (matches(*argv, "help") == 0) {
712 act_usage();
713 return -1;
714 } else {
715 fprintf(stderr, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv);
716 return -1;
717 }
718
719 if (ret < 0)
720 return -1;
721 }
722
723 return 0;
724 }