2 * m_action.c Action Management
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.
9 * Authors: J Hadi Salim (hadi@cyberus.ca)
12 * - parse to be passed a filedescriptor for logging purposes
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
28 #include "tc_common.h"
31 static struct action_util * action_list;
33 int gact_ld = 0 ; //fuckin backward compatibility
40 /*XXX: In the near future add a action->print_help to improve
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 ..
47 fprintf (stderr, "usage: tc actions <ACTSPECOP>*\n");
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"
65 static int print_noaopt(struct action_util *au, FILE *f, struct rtattr *opt)
67 if (opt && RTA_PAYLOAD(opt))
68 fprintf(f, "[Unknown action, optlen=%u] ",
69 (unsigned) RTA_PAYLOAD(opt));
73 static int parse_noaopt(struct action_util *au, int *argc_p, char ***argv_p, int code, struct nlmsghdr *n)
76 char **argv = *argv_p;
79 fprintf(stderr, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au->id, *argv);
81 fprintf(stderr, "Unknown action \"%s\"\n", au->id);
86 struct action_util *get_action_kind(char *str)
91 struct action_util *a;
96 for (a = action_list; a; a = a->next) {
97 if (strcmp(a->id, str) == 0)
101 snprintf(buf, sizeof(buf), "%s/m_%s.so", get_tc_lib(), str);
102 dlh = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
106 dlh = aBODY = dlopen(NULL, RTLD_LAZY);
112 snprintf(buf, sizeof(buf), "%s_action_util", str);
118 a->next = action_list;
130 a = malloc(sizeof(*a));
132 memset(a, 0, sizeof(*a));
133 strncpy(a->id, "noact", 15);
134 a->parse_aopt = parse_noaopt;
135 a->print_aopt = print_noaopt;
144 if ((matches(*argv, "change") == 0) ||
145 (matches(*argv, "replace") == 0)||
146 (matches(*argv, "delete") == 0)||
147 (matches(*argv, "add") == 0))
155 parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
158 char **argv = *argv_p;
159 struct rtattr *tail, *tail2;
162 int eap = 0; /* expect action parameters */
170 tail = tail2 = NLMSG_TAIL(n);
172 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
176 memset(k, 0, sizeof (k));
178 if (strcmp(*argv, "action") == 0 ) {
184 get_action_kind("gact");
188 } else if (strcmp(*argv, "help") == 0) {
190 } else if (new_cmd(argv)) {
193 struct action_util *a = NULL;
194 strncpy(k, *argv, sizeof (k) - 1);
197 a = get_action_kind(k);
210 tail = NLMSG_TAIL(n);
211 addattr_l(n, MAX_MSG, ++prio, NULL, 0);
212 addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
214 ret = a->parse_aopt(a,&argc, &argv, TCA_ACT_OPTIONS, n);
217 fprintf(stderr,"bad action parsing\n");
220 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
227 fprintf(stderr,"bad action empty %d\n",eap);
231 tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
238 /* no need to undo things, returning from here should
239 * cause enough pain */
240 fprintf(stderr, "parse_action: bad value (%d:%s)!\n",argc,*argv);
245 tc_print_one_action(FILE * f, struct rtattr *arg)
248 struct rtattr *tb[TCA_ACT_MAX + 1];
250 struct action_util *a = NULL;
255 parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
256 if (tb[TCA_ACT_KIND] == NULL) {
257 fprintf(stderr, "NULL Action!\n");
262 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
267 fprintf(f," %s \n", a->id);
272 err = a->print_aopt(a,f,tb[TCA_ACT_OPTIONS]);
278 if (show_stats && tb[TCA_ACT_STATS]) {
279 fprintf(f, "\tAction statistics:\n");
280 print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
288 tc_print_action(FILE * f, const struct rtattr *arg)
292 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
297 parse_rtattr_nested(tb, TCA_ACT_MAX_PRIO, arg);
299 if (tab_flush && NULL != tb[0] && NULL == tb[1]) {
300 int ret = tc_print_one_action(f, tb[0]);
304 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
306 fprintf(f, "\n\taction order %d: ", i + batch_c);
307 if (0 > tc_print_one_action(f, tb[i])) {
308 fprintf(f, "Error printing action\n");
314 batch_c+=TCA_ACT_MAX_PRIO ;
318 int print_action(const struct sockaddr_nl *who,
322 FILE *fp = (FILE*)arg;
323 struct tcamsg *t = NLMSG_DATA(n);
324 int len = n->nlmsg_len;
325 struct rtattr * tb[TCAA_MAX+1];
327 len -= NLMSG_LENGTH(sizeof(*t));
330 fprintf(stderr, "Wrong len %d\n", len);
334 parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
336 if (NULL == tb[TCA_ACT_TAB]) {
337 if (n->nlmsg_type != RTM_GETACTION)
338 fprintf(stderr, "print_action: NULL kind\n");
342 if (n->nlmsg_type == RTM_DELACTION) {
343 if (n->nlmsg_flags & NLM_F_ROOT) {
344 fprintf(fp, "Flushed table ");
347 fprintf(fp, "deleted action ");
351 if (n->nlmsg_type == RTM_NEWACTION)
352 fprintf(fp, "Added action ");
353 tc_print_action(fp, tb[TCA_ACT_TAB]);
358 int tc_action_gd(int cmd, unsigned flags, int *argc_p, char ***argv_p)
361 struct action_util *a = NULL;
363 char **argv = *argv_p;
367 struct sockaddr_nl nladdr;
369 struct rtattr *tail2;
370 struct nlmsghdr *ans = NULL;
378 req.t.tca_family = AF_UNSPEC;
380 memset(&req, 0, sizeof(req));
382 memset(&nladdr, 0, sizeof(nladdr));
383 nladdr.nl_family = AF_NETLINK;
385 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
386 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
387 req.n.nlmsg_type = cmd;
392 tail = NLMSG_TAIL(&req.n);
393 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
396 if (strcmp(*argv, "action") == 0 ) {
400 } else if (strcmp(*argv, "help") == 0) {
404 strncpy(k, *argv, sizeof (k) - 1);
405 a = get_action_kind(k);
407 fprintf(stderr, "Error: non existent action: %s\n",k);
411 if (strcmp(a->id, k) != 0) {
412 fprintf(stderr, "Error: non existent action: %s\n",k);
420 fprintf(stderr, "Error: no index specified action: %s\n",k);
425 if (matches(*argv, "index") == 0) {
427 if (get_u32(&i, *argv, 10)) {
428 fprintf(stderr, "Illegal \"index\"\n");
435 fprintf(stderr, "Error: no index specified action: %s\n",k);
440 tail2 = NLMSG_TAIL(&req.n);
441 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
442 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
443 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
444 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
448 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
450 req.n.nlmsg_seq = rth.dump = ++rth.seq;
451 if (cmd == RTM_GETACTION)
454 if (rtnl_talk(&rth, &req.n, 0, 0, ans, NULL, NULL) < 0) {
455 fprintf(stderr, "We have an error talking to the kernel\n");
459 if (ans && print_action(NULL, &req.n, (void*)stdout) < 0) {
460 fprintf(stderr, "Dump terminated\n");
470 int tc_action_modify(int cmd, unsigned flags, int *argc_p, char ***argv_p)
473 char **argv = *argv_p;
483 req.t.tca_family = AF_UNSPEC;
485 memset(&req, 0, sizeof(req));
487 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
488 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
489 req.n.nlmsg_type = cmd;
490 tail = NLMSG_TAIL(&req.n);
493 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
494 fprintf(stderr, "Illegal \"action\"\n");
497 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
499 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
500 fprintf(stderr, "We have an error talking to the kernel\n");
510 int tc_act_list_or_flush(int argc, char **argv, int event)
512 int ret = 0, prio = 0, msg_size = 0;
514 struct rtattr *tail,*tail2;
515 struct action_util *a = NULL;
522 req.t.tca_family = AF_UNSPEC;
524 memset(&req, 0, sizeof(req));
526 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
528 tail = NLMSG_TAIL(&req.n);
529 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
530 tail2 = NLMSG_TAIL(&req.n);
532 strncpy(k, *argv, sizeof (k) - 1);
535 get_action_kind("gact");
538 a = get_action_kind(k);
540 fprintf(stderr,"bad action %s\n",k);
543 if (strcmp(a->id, k) != 0) {
544 fprintf(stderr,"bad action %s\n",k);
547 strncpy(k, *argv, sizeof (k) - 1);
549 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
550 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
551 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
552 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
554 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
556 if (event == RTM_GETACTION) {
557 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
558 perror("Cannot send dump request");
561 ret = rtnl_dump_filter(&rth, print_action, stdout, NULL, NULL);
564 if (event == RTM_DELACTION) {
565 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
566 req.n.nlmsg_type = RTM_DELACTION;
567 req.n.nlmsg_flags |= NLM_F_ROOT;
568 req.n.nlmsg_flags |= NLM_F_REQUEST;
569 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
570 fprintf(stderr, "We have an error flushing\n");
581 int do_action(int argc, char **argv)
588 if (matches(*argv, "add") == 0) {
589 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
590 } else if (matches(*argv, "change") == 0 ||
591 matches(*argv, "replace") == 0) {
592 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
593 } else if (matches(*argv, "delete") == 0) {
596 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
597 } else if (matches(*argv, "get") == 0) {
600 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
601 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
602 || matches(*argv, "lst") == 0) {
607 return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
608 } else if (matches(*argv, "flush") == 0) {
613 return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
614 } else if (matches(*argv, "help") == 0) {
623 fprintf(stderr, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv);