]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_action.c
First part of cleaning up the help output of actions.
[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 = 0 ; //fuckin backward compatibility
34 #endif
35 int batch_c = 0;
36 int tab_flush = 0;
37
38 void act_usage(void)
39 {
40 /*XXX: In the near future add a action->print_help to improve
41 * usability
42 * This would mean new tc will not be backward compatible
43 * with any action .so from the old days. But if someone really
44 * does that, they would know how to fix this ..
45 *
46 */
47 fprintf (stderr, "usage: tc actions <ACTSPECOP>*\n");
48 fprintf(stderr,
49 "Where: \tACTSPECOP := ACR | GD | FL\n"
50 "\tACR := add | change | replace <ACTSPEC>* \n"
51 "\tGD := get | delete | <ACTISPEC>*\n"
52 "\tFL := ls | list | flush | <ACTNAMESPEC>\n"
53 "\tACTNAMESPEC := action <ACTNAME>\n"
54 "\tACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
55 "\tACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
56 "\tINDEXSPEC := index <32 bit indexvalue>\n"
57 "\tACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
58 "\t\tExample ACTNAME is gact, mirred etc\n"
59 "\t\tEach action has its own parameters (ACTPARAMS)\n"
60 "\n");
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) 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 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), "m_%s.so", str);
101 dlh = dlopen(buf, RTLD_LAZY);
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 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, "add") == 0))
147 return 1;
148
149 return 0;
150
151 }
152
153 int
154 parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
155 {
156 int argc = *argc_p;
157 char **argv = *argv_p;
158 struct rtattr *tail, *tail2;
159 char k[16];
160 int ok = 0;
161 int eap = 0; /* expect action parameters */
162
163 int ret = 0;
164 int prio = 0;
165
166 if (argc <= 0)
167 return -1;
168
169 tail = tail2 = NLMSG_TAIL(n);
170
171 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
172
173 while (argc > 0) {
174
175 memset(k, 0, sizeof (k));
176
177 if (strcmp(*argv, "action") == 0 ) {
178 argc--;
179 argv++;
180 eap = 1;
181 #ifdef CONFIG_GACT
182 if (!gact_ld) {
183 get_action_kind("gact");
184 }
185 #endif
186 continue;
187 } else if (strcmp(*argv, "help") == 0) {
188 return -1;
189 } else if (new_cmd(argv)) {
190 goto done0;
191 } else {
192 struct action_util *a = NULL;
193 strncpy(k, *argv, sizeof (k) - 1);
194 eap = 0;
195 if (argc > 0 ) {
196 a = get_action_kind(k);
197 } else {
198 done0:
199 if (ok)
200 break;
201 else
202 goto done;
203 }
204
205 if (NULL == a) {
206 goto bad_val;
207 }
208
209 tail = NLMSG_TAIL(n);
210 addattr_l(n, MAX_MSG, ++prio, NULL, 0);
211 addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
212
213 ret = a->parse_aopt(a,&argc, &argv, TCA_ACT_OPTIONS, n);
214
215 if (ret < 0) {
216 fprintf(stderr,"bad action parsing\n");
217 goto bad_val;
218 }
219 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
220 ok++;
221 }
222
223 }
224
225 if (eap > 0) {
226 fprintf(stderr,"bad action empty %d\n",eap);
227 goto bad_val;
228 }
229
230 tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
231
232 done:
233 *argc_p = argc;
234 *argv_p = argv;
235 return 0;
236 bad_val:
237 /* no need to undo things, returning from here should
238 * cause enough pain */
239 fprintf(stderr, "parse_action: bad value (%d:%s)!\n",argc,*argv);
240 return -1;
241 }
242
243 int
244 tc_print_one_action(FILE * f, struct rtattr *arg)
245 {
246
247 struct rtattr *tb[TCA_ACT_MAX + 1];
248 int err = 0;
249 struct action_util *a = NULL;
250
251 if (arg == NULL)
252 return -1;
253
254 parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
255 if (tb[TCA_ACT_KIND] == NULL) {
256 fprintf(stderr, "NULL Action!\n");
257 return -1;
258 }
259
260
261 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
262 if (NULL == a)
263 return err;
264
265 if (tab_flush) {
266 fprintf(f," %s \n", a->id);
267 tab_flush = 0;
268 return 0;
269 }
270
271 err = a->print_aopt(a,f,tb[TCA_ACT_OPTIONS]);
272
273
274 if (0 > err)
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 int
287 tc_print_action(FILE * f, const struct rtattr *arg)
288 {
289
290 int i;
291 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
292
293 if (arg == NULL)
294 return 0;
295
296 parse_rtattr_nested(tb, TCA_ACT_MAX_PRIO, arg);
297
298 if (tab_flush && NULL != tb[0] && NULL == tb[1]) {
299 int ret = tc_print_one_action(f, tb[0]);
300 return ret;
301 }
302
303 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
304 if (tb[i]) {
305 fprintf(f, "\n\taction order %d: ", i + batch_c);
306 if (0 > tc_print_one_action(f, tb[i])) {
307 fprintf(f, "Error printing action\n");
308 }
309 }
310
311 }
312
313 batch_c+=TCA_ACT_MAX_PRIO ;
314 return 0;
315 }
316
317 int print_action(const struct sockaddr_nl *who,
318 struct nlmsghdr *n,
319 void *arg)
320 {
321 FILE *fp = (FILE*)arg;
322 struct tcamsg *t = NLMSG_DATA(n);
323 int len = n->nlmsg_len;
324 struct rtattr * tb[TCAA_MAX+1];
325
326 len -= NLMSG_LENGTH(sizeof(*t));
327
328 if (len < 0) {
329 fprintf(stderr, "Wrong len %d\n", len);
330 return -1;
331 }
332
333 parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
334
335 if (NULL == tb[TCA_ACT_TAB]) {
336 if (n->nlmsg_type != RTM_GETACTION)
337 fprintf(stderr, "print_action: NULL kind\n");
338 return -1;
339 }
340
341 if (n->nlmsg_type == RTM_DELACTION) {
342 if (n->nlmsg_flags & NLM_F_ROOT) {
343 fprintf(fp, "Flushed table ");
344 tab_flush = 1;
345 } else {
346 fprintf(fp, "deleted action ");
347 }
348 }
349
350 if (n->nlmsg_type == RTM_NEWACTION)
351 fprintf(fp, "Added action ");
352 tc_print_action(fp, tb[TCA_ACT_TAB]);
353
354 return 0;
355 }
356
357 int tc_action_gd(int cmd, unsigned flags, int *argc_p, char ***argv_p)
358 {
359 char k[16];
360 struct action_util *a = NULL;
361 int argc = *argc_p;
362 char **argv = *argv_p;
363 int prio = 0;
364 int ret = 0;
365 __u32 i;
366 struct sockaddr_nl nladdr;
367 struct rtattr *tail;
368 struct rtattr *tail2;
369 struct nlmsghdr *ans = NULL;
370
371 struct {
372 struct nlmsghdr n;
373 struct tcamsg t;
374 char buf[MAX_MSG];
375 } req;
376
377 req.t.tca_family = AF_UNSPEC;
378
379 memset(&req, 0, sizeof(req));
380
381 memset(&nladdr, 0, sizeof(nladdr));
382 nladdr.nl_family = AF_NETLINK;
383
384 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
385 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
386 req.n.nlmsg_type = cmd;
387 argc -=1;
388 argv +=1;
389
390
391 tail = NLMSG_TAIL(&req.n);
392 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
393
394 while (argc > 0) {
395 if (strcmp(*argv, "action") == 0 ) {
396 argc--;
397 argv++;
398 continue;
399 } else if (strcmp(*argv, "help") == 0) {
400 return -1;
401 }
402
403 strncpy(k, *argv, sizeof (k) - 1);
404 a = get_action_kind(k);
405 if (NULL == a) {
406 fprintf(stderr, "Error: non existent action: %s\n",k);
407 ret = -1;
408 goto bad_val;
409 }
410 if (strcmp(a->id, k) != 0) {
411 fprintf(stderr, "Error: non existent action: %s\n",k);
412 ret = -1;
413 goto bad_val;
414 }
415
416 argc -=1;
417 argv +=1;
418 if (argc <= 0) {
419 fprintf(stderr, "Error: no index specified action: %s\n",k);
420 ret = -1;
421 goto bad_val;
422 }
423
424 if (matches(*argv, "index") == 0) {
425 NEXT_ARG();
426 if (get_u32(&i, *argv, 10)) {
427 fprintf(stderr, "Illegal \"index\"\n");
428 ret = -1;
429 goto bad_val;
430 }
431 argc -=1;
432 argv +=1;
433 } else {
434 fprintf(stderr, "Error: no index specified action: %s\n",k);
435 ret = -1;
436 goto bad_val;
437 }
438
439 tail2 = NLMSG_TAIL(&req.n);
440 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
441 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
442 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
443 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
444
445 }
446
447 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
448
449 req.n.nlmsg_seq = rth.dump = ++rth.seq;
450 if (cmd == RTM_GETACTION)
451 ans = &req.n;
452
453 if (rtnl_talk(&rth, &req.n, 0, 0, ans, NULL, NULL) < 0) {
454 fprintf(stderr, "We have an error talking to the kernel\n");
455 return 1;
456 }
457
458 if (ans && print_action(NULL, &req.n, (void*)stdout) < 0) {
459 fprintf(stderr, "Dump terminated\n");
460 return 1;
461 }
462
463 *argc_p = argc;
464 *argv_p = argv;
465 bad_val:
466 return ret;
467 }
468
469 int tc_action_modify(int cmd, unsigned flags, int *argc_p, char ***argv_p)
470 {
471 int argc = *argc_p;
472 char **argv = *argv_p;
473 int ret = 0;
474
475 struct rtattr *tail;
476 struct {
477 struct nlmsghdr n;
478 struct tcamsg t;
479 char buf[MAX_MSG];
480 } req;
481
482 req.t.tca_family = AF_UNSPEC;
483
484 memset(&req, 0, sizeof(req));
485
486 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
487 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
488 req.n.nlmsg_type = cmd;
489 tail = NLMSG_TAIL(&req.n);
490 argc -=1;
491 argv +=1;
492 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
493 fprintf(stderr, "Illegal \"action\"\n");
494 return -1;
495 }
496 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
497
498 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
499 fprintf(stderr, "We have an error talking to the kernel\n");
500 ret = -1;
501 }
502
503 *argc_p = argc;
504 *argv_p = argv;
505
506 return ret;
507 }
508
509 int tc_act_list_or_flush(int argc, char **argv, int event)
510 {
511 int ret = 0, prio = 0, msg_size = 0;
512 char k[16];
513 struct rtattr *tail,*tail2;
514 struct action_util *a = NULL;
515 struct {
516 struct nlmsghdr n;
517 struct tcamsg t;
518 char buf[MAX_MSG];
519 } req;
520
521 req.t.tca_family = AF_UNSPEC;
522
523 memset(&req, 0, sizeof(req));
524
525 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
526
527 tail = NLMSG_TAIL(&req.n);
528 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
529 tail2 = NLMSG_TAIL(&req.n);
530
531 strncpy(k, *argv, sizeof (k) - 1);
532 #ifdef CONFIG_GACT
533 if (!gact_ld) {
534 get_action_kind("gact");
535 }
536 #endif
537 a = get_action_kind(k);
538 if (NULL == a) {
539 fprintf(stderr,"bad action %s\n",k);
540 goto bad_val;
541 }
542 if (strcmp(a->id, k) != 0) {
543 fprintf(stderr,"bad action %s\n",k);
544 goto bad_val;
545 }
546 strncpy(k, *argv, sizeof (k) - 1);
547
548 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
549 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
550 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
551 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
552
553 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
554
555 if (event == RTM_GETACTION) {
556 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
557 perror("Cannot send dump request");
558 return 1;
559 }
560 ret = rtnl_dump_filter(&rth, print_action, stdout, NULL, NULL);
561 }
562
563 if (event == RTM_DELACTION) {
564 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
565 req.n.nlmsg_type = RTM_DELACTION;
566 req.n.nlmsg_flags |= NLM_F_ROOT;
567 req.n.nlmsg_flags |= NLM_F_REQUEST;
568 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
569 fprintf(stderr, "We have an error flushing\n");
570 return 1;
571 }
572
573 }
574
575 bad_val:
576
577 return ret;
578 }
579
580 int do_action(int argc, char **argv)
581 {
582
583 int ret = 0;
584
585 while (argc > 0) {
586
587 if (matches(*argv, "add") == 0) {
588 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
589 } else if (matches(*argv, "change") == 0 ||
590 matches(*argv, "replace") == 0) {
591 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
592 } else if (matches(*argv, "delete") == 0) {
593 argc -=1;
594 argv +=1;
595 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
596 } else if (matches(*argv, "get") == 0) {
597 argc -=1;
598 argv +=1;
599 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
600 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
601 || matches(*argv, "lst") == 0) {
602 if (argc <= 2) {
603 act_usage();
604 return -1;
605 }
606 return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
607 } else if (matches(*argv, "flush") == 0) {
608 if (argc <= 2) {
609 act_usage();
610 return -1;
611 }
612 return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
613 } else if (matches(*argv, "help") == 0) {
614 act_usage();
615 return -1;
616 } else {
617
618 ret = -1;
619 }
620
621 if (ret < 0) {
622 fprintf(stderr, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv);
623 return -1;
624 }
625 }
626
627 return 0;
628 }
629