]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_action.c
tc: m_action: Use C99 style initializers for struct req
[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 = malloc(sizeof(*a));
130 if (a) {
131 memset(a, 0, sizeof(*a));
132 strncpy(a->id, "noact", 15);
133 a->parse_aopt = parse_noaopt;
134 a->print_aopt = print_noaopt;
135 goto reg;
136 }
137 return a;
138 }
139
140 static int
141 new_cmd(char **argv)
142 {
143 if ((matches(*argv, "change") == 0) ||
144 (matches(*argv, "replace") == 0) ||
145 (matches(*argv, "delete") == 0) ||
146 (matches(*argv, "get") == 0) ||
147 (matches(*argv, "add") == 0))
148 return 1;
149
150 return 0;
151
152 }
153
154 int
155 parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
156 {
157 int argc = *argc_p;
158 char **argv = *argv_p;
159 struct rtattr *tail, *tail2;
160 char k[16];
161 int ok = 0;
162 int eap = 0; /* expect action parameters */
163
164 int ret = 0;
165 int prio = 0;
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, n);
220
221 if (ret < 0) {
222 fprintf(stderr, "bad action parsing\n");
223 goto bad_val;
224 }
225 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
226 ok++;
227 }
228
229 }
230
231 if (eap > 0) {
232 fprintf(stderr, "bad action empty %d\n", eap);
233 goto bad_val;
234 }
235
236 tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
237
238 done:
239 *argc_p = argc;
240 *argv_p = argv;
241 return 0;
242 bad_val:
243 /* no need to undo things, returning from here should
244 * cause enough pain */
245 fprintf(stderr, "parse_action: bad value (%d:%s)!\n", argc, *argv);
246 return -1;
247 }
248
249 static int
250 tc_print_one_action(FILE *f, struct rtattr *arg)
251 {
252
253 struct rtattr *tb[TCA_ACT_MAX + 1];
254 int err = 0;
255 struct action_util *a = NULL;
256
257 if (arg == NULL)
258 return -1;
259
260 parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
261
262 if (tb[TCA_ACT_KIND] == NULL) {
263 fprintf(stderr, "NULL Action!\n");
264 return -1;
265 }
266
267
268 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
269 if (a == NULL)
270 return err;
271
272 err = a->print_aopt(a, f, tb[TCA_ACT_OPTIONS]);
273
274 if (err < 0)
275 return err;
276
277 if (show_stats && tb[TCA_ACT_STATS]) {
278 fprintf(f, "\tAction statistics:\n");
279 print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
280 fprintf(f, "\n");
281 }
282
283 return 0;
284 }
285
286 static int
287 tc_print_action_flush(FILE *f, const struct rtattr *arg)
288 {
289
290 struct rtattr *tb[TCA_MAX + 1];
291 int err = 0;
292 struct action_util *a = NULL;
293 __u32 *delete_count = 0;
294
295 parse_rtattr_nested(tb, TCA_MAX, arg);
296
297 if (tb[TCA_KIND] == NULL) {
298 fprintf(stderr, "NULL Action!\n");
299 return -1;
300 }
301
302 a = get_action_kind(RTA_DATA(tb[TCA_KIND]));
303 if (a == NULL)
304 return err;
305
306 delete_count = RTA_DATA(tb[TCA_FCNT]);
307 fprintf(f, " %s (%d entries)\n", a->id, *delete_count);
308 tab_flush = 0;
309 return 0;
310 }
311
312 int
313 tc_print_action(FILE *f, const struct rtattr *arg)
314 {
315
316 int i;
317 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
318
319 if (arg == NULL)
320 return 0;
321
322 parse_rtattr_nested(tb, TCA_ACT_MAX_PRIO, arg);
323
324 if (tab_flush && NULL != tb[0] && NULL == tb[1])
325 return tc_print_action_flush(f, tb[0]);
326
327 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
328 if (tb[i]) {
329 fprintf(f, "\n\taction order %d: ", i);
330 if (tc_print_one_action(f, tb[i]) < 0) {
331 fprintf(f, "Error printing action\n");
332 }
333 }
334
335 }
336
337 return 0;
338 }
339
340 int print_action(const struct sockaddr_nl *who,
341 struct nlmsghdr *n,
342 void *arg)
343 {
344 FILE *fp = (FILE *)arg;
345 struct tcamsg *t = NLMSG_DATA(n);
346 int len = n->nlmsg_len;
347 struct rtattr *tb[TCAA_MAX+1];
348
349 len -= NLMSG_LENGTH(sizeof(*t));
350
351 if (len < 0) {
352 fprintf(stderr, "Wrong len %d\n", len);
353 return -1;
354 }
355
356 parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
357
358 if (tb[TCA_ACT_TAB] == NULL) {
359 if (n->nlmsg_type != RTM_GETACTION)
360 fprintf(stderr, "print_action: NULL kind\n");
361 return -1;
362 }
363
364 if (n->nlmsg_type == RTM_DELACTION) {
365 if (n->nlmsg_flags & NLM_F_ROOT) {
366 fprintf(fp, "Flushed table ");
367 tab_flush = 1;
368 } else {
369 fprintf(fp, "deleted action ");
370 }
371 }
372
373 if (n->nlmsg_type == RTM_NEWACTION)
374 fprintf(fp, "Added action ");
375 tc_print_action(fp, tb[TCA_ACT_TAB]);
376
377 return 0;
378 }
379
380 static int tc_action_gd(int cmd, unsigned int flags, int *argc_p, char ***argv_p)
381 {
382 char k[16];
383 struct action_util *a = NULL;
384 int argc = *argc_p;
385 char **argv = *argv_p;
386 int prio = 0;
387 int ret = 0;
388 __u32 i;
389 struct sockaddr_nl nladdr;
390 struct rtattr *tail;
391 struct rtattr *tail2;
392 struct nlmsghdr *ans = NULL;
393
394 struct {
395 struct nlmsghdr n;
396 struct tcamsg t;
397 char buf[MAX_MSG];
398 } req = {
399 .n = {
400 .nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
401 .nlmsg_flags = NLM_F_REQUEST | flags,
402 .nlmsg_type = cmd,
403 },
404 .t.tca_family = AF_UNSPEC,
405 .buf = { 0 }
406 };
407
408 memset(&nladdr, 0, sizeof(nladdr));
409 nladdr.nl_family = AF_NETLINK;
410
411 argc -= 1;
412 argv += 1;
413
414
415 tail = NLMSG_TAIL(&req.n);
416 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
417
418 while (argc > 0) {
419 if (strcmp(*argv, "action") == 0) {
420 argc--;
421 argv++;
422 continue;
423 } else if (strcmp(*argv, "help") == 0) {
424 return -1;
425 }
426
427 strncpy(k, *argv, sizeof(k) - 1);
428 a = get_action_kind(k);
429 if (a == NULL) {
430 fprintf(stderr, "Error: non existent action: %s\n", k);
431 ret = -1;
432 goto bad_val;
433 }
434 if (strcmp(a->id, k) != 0) {
435 fprintf(stderr, "Error: non existent action: %s\n", k);
436 ret = -1;
437 goto bad_val;
438 }
439
440 argc -= 1;
441 argv += 1;
442 if (argc <= 0) {
443 fprintf(stderr, "Error: no index specified action: %s\n", k);
444 ret = -1;
445 goto bad_val;
446 }
447
448 if (matches(*argv, "index") == 0) {
449 NEXT_ARG();
450 if (get_u32(&i, *argv, 10)) {
451 fprintf(stderr, "Illegal \"index\"\n");
452 ret = -1;
453 goto bad_val;
454 }
455 argc -= 1;
456 argv += 1;
457 } else {
458 fprintf(stderr, "Error: no index specified action: %s\n", k);
459 ret = -1;
460 goto bad_val;
461 }
462
463 tail2 = NLMSG_TAIL(&req.n);
464 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
465 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
466 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
467 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
468
469 }
470
471 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
472
473 req.n.nlmsg_seq = rth.dump = ++rth.seq;
474 if (cmd == RTM_GETACTION)
475 ans = &req.n;
476
477 if (rtnl_talk(&rth, &req.n, ans, MAX_MSG) < 0) {
478 fprintf(stderr, "We have an error talking to the kernel\n");
479 return 1;
480 }
481
482 if (ans && print_action(NULL, &req.n, (void *)stdout) < 0) {
483 fprintf(stderr, "Dump terminated\n");
484 return 1;
485 }
486
487 *argc_p = argc;
488 *argv_p = argv;
489 bad_val:
490 return ret;
491 }
492
493 static int tc_action_modify(int cmd, unsigned int flags, int *argc_p, char ***argv_p)
494 {
495 int argc = *argc_p;
496 char **argv = *argv_p;
497 int ret = 0;
498
499 struct rtattr *tail;
500 struct {
501 struct nlmsghdr n;
502 struct tcamsg t;
503 char buf[MAX_MSG];
504 } req = {
505 .n = {
506 .nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
507 .nlmsg_flags = NLM_F_REQUEST | flags,
508 .nlmsg_type = cmd,
509 },
510 .t.tca_family = AF_UNSPEC,
511 .buf = { 0 }
512 };
513
514 tail = NLMSG_TAIL(&req.n);
515 argc -= 1;
516 argv += 1;
517 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
518 fprintf(stderr, "Illegal \"action\"\n");
519 return -1;
520 }
521 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
522
523 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
524 fprintf(stderr, "We have an error talking to the kernel\n");
525 ret = -1;
526 }
527
528 *argc_p = argc;
529 *argv_p = argv;
530
531 return ret;
532 }
533
534 static int tc_act_list_or_flush(int argc, char **argv, int event)
535 {
536 int ret = 0, prio = 0, msg_size = 0;
537 char k[16];
538 struct rtattr *tail, *tail2;
539 struct action_util *a = NULL;
540 struct {
541 struct nlmsghdr n;
542 struct tcamsg t;
543 char buf[MAX_MSG];
544 } req = {
545 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg)),
546 .t.tca_family = AF_UNSPEC,
547 .buf = { 0 }
548 };
549
550 tail = NLMSG_TAIL(&req.n);
551 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
552 tail2 = NLMSG_TAIL(&req.n);
553
554 strncpy(k, *argv, sizeof(k) - 1);
555 #ifdef CONFIG_GACT
556 if (!gact_ld) {
557 get_action_kind("gact");
558 }
559 #endif
560 a = get_action_kind(k);
561 if (a == NULL) {
562 fprintf(stderr, "bad action %s\n", k);
563 goto bad_val;
564 }
565 if (strcmp(a->id, k) != 0) {
566 fprintf(stderr, "bad action %s\n", k);
567 goto bad_val;
568 }
569 strncpy(k, *argv, sizeof(k) - 1);
570
571 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
572 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
573 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
574 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
575
576 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
577
578 if (event == RTM_GETACTION) {
579 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
580 perror("Cannot send dump request");
581 return 1;
582 }
583 ret = rtnl_dump_filter(&rth, print_action, stdout);
584 }
585
586 if (event == RTM_DELACTION) {
587 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
588 req.n.nlmsg_type = RTM_DELACTION;
589 req.n.nlmsg_flags |= NLM_F_ROOT;
590 req.n.nlmsg_flags |= NLM_F_REQUEST;
591 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
592 fprintf(stderr, "We have an error flushing\n");
593 return 1;
594 }
595
596 }
597
598 bad_val:
599
600 return ret;
601 }
602
603 int do_action(int argc, char **argv)
604 {
605
606 int ret = 0;
607
608 while (argc > 0) {
609
610 if (matches(*argv, "add") == 0) {
611 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
612 } else if (matches(*argv, "change") == 0 ||
613 matches(*argv, "replace") == 0) {
614 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
615 } else if (matches(*argv, "delete") == 0) {
616 argc -= 1;
617 argv += 1;
618 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
619 } else if (matches(*argv, "get") == 0) {
620 argc -= 1;
621 argv += 1;
622 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
623 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
624 || matches(*argv, "lst") == 0) {
625 if (argc <= 2) {
626 act_usage();
627 return -1;
628 }
629 return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
630 } else if (matches(*argv, "flush") == 0) {
631 if (argc <= 2) {
632 act_usage();
633 return -1;
634 }
635 return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
636 } else if (matches(*argv, "help") == 0) {
637 act_usage();
638 return -1;
639 } else {
640
641 ret = -1;
642 }
643
644 if (ret < 0) {
645 fprintf(stderr, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv);
646 return -1;
647 }
648 }
649
650 return 0;
651 }