]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_action.c
tc/m_gact: Drop dead code
[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)
350 {
351
352 int i;
353 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
354
355 if (arg == NULL)
356 return 0;
357
358 parse_rtattr_nested(tb, TCA_ACT_MAX_PRIO, arg);
359
360 if (tab_flush && NULL != tb[0] && NULL == tb[1])
361 return tc_print_action_flush(f, tb[0]);
362
363 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
364 if (tb[i]) {
365 fprintf(f, "\n\taction order %d: ", i);
366 if (tc_print_one_action(f, tb[i]) < 0) {
367 fprintf(f, "Error printing action\n");
368 }
369 }
370
371 }
372
373 return 0;
374 }
375
376 int print_action(const struct sockaddr_nl *who,
377 struct nlmsghdr *n,
378 void *arg)
379 {
380 FILE *fp = (FILE *)arg;
381 struct tcamsg *t = NLMSG_DATA(n);
382 int len = n->nlmsg_len;
383 struct rtattr *tb[TCAA_MAX+1];
384
385 len -= NLMSG_LENGTH(sizeof(*t));
386
387 if (len < 0) {
388 fprintf(stderr, "Wrong len %d\n", len);
389 return -1;
390 }
391
392 parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
393
394 if (tb[TCA_ACT_TAB] == NULL) {
395 if (n->nlmsg_type != RTM_GETACTION)
396 fprintf(stderr, "print_action: NULL kind\n");
397 return -1;
398 }
399
400 if (n->nlmsg_type == RTM_DELACTION) {
401 if (n->nlmsg_flags & NLM_F_ROOT) {
402 fprintf(fp, "Flushed table ");
403 tab_flush = 1;
404 } else {
405 fprintf(fp, "Deleted action ");
406 }
407 }
408
409 if (n->nlmsg_type == RTM_NEWACTION) {
410 if ((n->nlmsg_flags & NLM_F_CREATE) &&
411 !(n->nlmsg_flags & NLM_F_REPLACE)) {
412 fprintf(fp, "Added action ");
413 } else if (n->nlmsg_flags & NLM_F_REPLACE) {
414 fprintf(fp, "Replaced action ");
415 }
416 }
417 tc_print_action(fp, tb[TCA_ACT_TAB]);
418
419 return 0;
420 }
421
422 static int tc_action_gd(int cmd, unsigned int flags, int *argc_p, char ***argv_p)
423 {
424 char k[16];
425 struct action_util *a = NULL;
426 int argc = *argc_p;
427 char **argv = *argv_p;
428 int prio = 0;
429 int ret = 0;
430 __u32 i;
431 struct rtattr *tail;
432 struct rtattr *tail2;
433 struct nlmsghdr *ans = NULL;
434
435 struct {
436 struct nlmsghdr n;
437 struct tcamsg t;
438 char buf[MAX_MSG];
439 } req = {
440 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
441 .n.nlmsg_flags = NLM_F_REQUEST | flags,
442 .n.nlmsg_type = cmd,
443 .t.tca_family = AF_UNSPEC,
444 };
445
446 argc -= 1;
447 argv += 1;
448
449
450 tail = NLMSG_TAIL(&req.n);
451 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
452
453 while (argc > 0) {
454 if (strcmp(*argv, "action") == 0) {
455 argc--;
456 argv++;
457 continue;
458 } else if (strcmp(*argv, "help") == 0) {
459 return -1;
460 }
461
462 strncpy(k, *argv, sizeof(k) - 1);
463 a = get_action_kind(k);
464 if (a == NULL) {
465 fprintf(stderr, "Error: non existent action: %s\n", k);
466 ret = -1;
467 goto bad_val;
468 }
469 if (strcmp(a->id, k) != 0) {
470 fprintf(stderr, "Error: non existent action: %s\n", k);
471 ret = -1;
472 goto bad_val;
473 }
474
475 argc -= 1;
476 argv += 1;
477 if (argc <= 0) {
478 fprintf(stderr, "Error: no index specified action: %s\n", k);
479 ret = -1;
480 goto bad_val;
481 }
482
483 if (matches(*argv, "index") == 0) {
484 NEXT_ARG();
485 if (get_u32(&i, *argv, 10)) {
486 fprintf(stderr, "Illegal \"index\"\n");
487 ret = -1;
488 goto bad_val;
489 }
490 argc -= 1;
491 argv += 1;
492 } else {
493 fprintf(stderr, "Error: no index specified action: %s\n", k);
494 ret = -1;
495 goto bad_val;
496 }
497
498 tail2 = NLMSG_TAIL(&req.n);
499 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
500 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
501 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
502 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
503
504 }
505
506 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
507
508 req.n.nlmsg_seq = rth.dump = ++rth.seq;
509 if (cmd == RTM_GETACTION)
510 ans = &req.n;
511
512 if (rtnl_talk(&rth, &req.n, ans, MAX_MSG) < 0) {
513 fprintf(stderr, "We have an error talking to the kernel\n");
514 return 1;
515 }
516
517 if (ans && print_action(NULL, &req.n, (void *)stdout) < 0) {
518 fprintf(stderr, "Dump terminated\n");
519 return 1;
520 }
521
522 *argc_p = argc;
523 *argv_p = argv;
524 bad_val:
525 return ret;
526 }
527
528 static int tc_action_modify(int cmd, unsigned int flags, int *argc_p, char ***argv_p)
529 {
530 int argc = *argc_p;
531 char **argv = *argv_p;
532 int ret = 0;
533 struct {
534 struct nlmsghdr n;
535 struct tcamsg t;
536 char buf[MAX_MSG];
537 } req = {
538 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
539 .n.nlmsg_flags = NLM_F_REQUEST | flags,
540 .n.nlmsg_type = cmd,
541 .t.tca_family = AF_UNSPEC,
542 };
543 struct rtattr *tail = NLMSG_TAIL(&req.n);
544
545 argc -= 1;
546 argv += 1;
547 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
548 fprintf(stderr, "Illegal \"action\"\n");
549 return -1;
550 }
551 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
552
553 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
554 fprintf(stderr, "We have an error talking to the kernel\n");
555 ret = -1;
556 }
557
558 *argc_p = argc;
559 *argv_p = argv;
560
561 return ret;
562 }
563
564 static int tc_act_list_or_flush(int argc, char **argv, int event)
565 {
566 int ret = 0, prio = 0, msg_size = 0;
567 char k[16];
568 struct rtattr *tail, *tail2;
569 struct action_util *a = NULL;
570 struct {
571 struct nlmsghdr n;
572 struct tcamsg t;
573 char buf[MAX_MSG];
574 } req = {
575 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
576 .t.tca_family = AF_UNSPEC,
577 };
578
579 tail = NLMSG_TAIL(&req.n);
580 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
581 tail2 = NLMSG_TAIL(&req.n);
582
583 strncpy(k, *argv, sizeof(k) - 1);
584 #ifdef CONFIG_GACT
585 if (!gact_ld) {
586 get_action_kind("gact");
587 }
588 #endif
589 a = get_action_kind(k);
590 if (a == NULL) {
591 fprintf(stderr, "bad action %s\n", k);
592 goto bad_val;
593 }
594 if (strcmp(a->id, k) != 0) {
595 fprintf(stderr, "bad action %s\n", k);
596 goto bad_val;
597 }
598 strncpy(k, *argv, sizeof(k) - 1);
599
600 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
601 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
602 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
603 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
604
605 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
606
607 if (event == RTM_GETACTION) {
608 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
609 perror("Cannot send dump request");
610 return 1;
611 }
612 ret = rtnl_dump_filter(&rth, print_action, stdout);
613 }
614
615 if (event == RTM_DELACTION) {
616 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
617 req.n.nlmsg_type = RTM_DELACTION;
618 req.n.nlmsg_flags |= NLM_F_ROOT;
619 req.n.nlmsg_flags |= NLM_F_REQUEST;
620 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
621 fprintf(stderr, "We have an error flushing\n");
622 return 1;
623 }
624
625 }
626
627 bad_val:
628
629 return ret;
630 }
631
632 int do_action(int argc, char **argv)
633 {
634
635 int ret = 0;
636
637 while (argc > 0) {
638
639 if (matches(*argv, "add") == 0) {
640 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
641 } else if (matches(*argv, "change") == 0 ||
642 matches(*argv, "replace") == 0) {
643 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
644 } else if (matches(*argv, "delete") == 0) {
645 argc -= 1;
646 argv += 1;
647 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
648 } else if (matches(*argv, "get") == 0) {
649 argc -= 1;
650 argv += 1;
651 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
652 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
653 || matches(*argv, "lst") == 0) {
654 if (argc <= 2) {
655 act_usage();
656 return -1;
657 }
658 return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
659 } else if (matches(*argv, "flush") == 0) {
660 if (argc <= 2) {
661 act_usage();
662 return -1;
663 }
664 return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
665 } else if (matches(*argv, "help") == 0) {
666 act_usage();
667 return -1;
668 } else {
669 fprintf(stderr, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv);
670 return -1;
671 }
672
673 if (ret < 0)
674 return -1;
675 }
676
677 return 0;
678 }