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 fprintf (stderr, "action usage improper\n");
43 static int print_noaopt(struct action_util *au, FILE *f, struct rtattr *opt)
45 if (opt && RTA_PAYLOAD(opt))
46 fprintf(f, "[Unknown action, optlen=%u] ",
47 (unsigned) RTA_PAYLOAD(opt));
51 static int parse_noaopt(struct action_util *au, int *argc_p, char ***argv_p, int code, struct nlmsghdr *n)
54 char **argv = *argv_p;
57 fprintf(stderr, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au->id, *argv);
59 fprintf(stderr, "Unknown action \"%s\"\n", au->id);
64 struct action_util *get_action_kind(char *str)
69 struct action_util *a;
74 for (a = action_list; a; a = a->next) {
75 if (strcmp(a->id, str) == 0)
79 snprintf(buf, sizeof(buf), "m_%s.so", str);
80 dlh = dlopen(buf, RTLD_LAZY);
84 dlh = aBODY = dlopen(NULL, RTLD_LAZY);
90 snprintf(buf, sizeof(buf), "%s_action_util", str);
96 a->next = action_list;
108 a = malloc(sizeof(*a));
110 memset(a, 0, sizeof(*a));
111 strncpy(a->id, "noact", 15);
112 a->parse_aopt = parse_noaopt;
113 a->print_aopt = print_noaopt;
122 if ((matches(*argv, "change") == 0) ||
123 (matches(*argv, "replace") == 0)||
124 (matches(*argv, "delete") == 0)||
125 (matches(*argv, "add") == 0))
133 parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
136 char **argv = *argv_p;
137 struct rtattr *tail, *tail2;
140 int eap = 0; /* expect action parameters */
148 tail = tail2 = NLMSG_TAIL(n);
150 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
154 memset(k, 0, sizeof (k));
156 if (strcmp(*argv, "action") == 0 ) {
162 get_action_kind("gact");
166 } else if (strcmp(*argv, "help") == 0) {
168 } else if (new_cmd(argv)) {
171 struct action_util *a = NULL;
172 strncpy(k, *argv, sizeof (k) - 1);
175 a = get_action_kind(k);
188 tail = NLMSG_TAIL(n);
189 addattr_l(n, MAX_MSG, ++prio, NULL, 0);
190 addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
192 ret = a->parse_aopt(a,&argc, &argv, TCA_ACT_OPTIONS, n);
195 fprintf(stderr,"bad action parsing\n");
198 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
205 fprintf(stderr,"bad action empty %d\n",eap);
209 tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
216 /* no need to undo things, returning from here should
217 * cause enough pain */
218 fprintf(stderr, "parse_action: bad value (%d:%s)!\n",argc,*argv);
223 tc_print_one_action(FILE * f, struct rtattr *arg)
226 struct rtattr *tb[TCA_ACT_MAX + 1];
228 struct action_util *a = NULL;
233 parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
234 if (tb[TCA_ACT_KIND] == NULL) {
235 fprintf(stderr, "NULL Action!\n");
240 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
245 fprintf(f," %s \n", a->id);
250 err = a->print_aopt(a,f,tb[TCA_ACT_OPTIONS]);
256 if (show_stats && tb[TCA_ACT_STATS]) {
257 fprintf(f, "\tAction statistics:\n");
258 print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
266 tc_print_action(FILE * f, const struct rtattr *arg)
270 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
275 parse_rtattr_nested(tb, TCA_ACT_MAX_PRIO, arg);
277 if (tab_flush && NULL != tb[0] && NULL == tb[1]) {
278 int ret = tc_print_one_action(f, tb[0]);
282 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
284 fprintf(f, "\n\taction order %d: ", i + batch_c);
285 if (0 > tc_print_one_action(f, tb[i])) {
286 fprintf(f, "Error printing action\n");
292 batch_c+=TCA_ACT_MAX_PRIO ;
296 static int do_print_action(const struct sockaddr_nl *who,
300 FILE *fp = (FILE*)arg;
301 struct tcamsg *t = NLMSG_DATA(n);
302 int len = n->nlmsg_len;
303 struct rtattr * tb[TCAA_MAX+1];
305 len -= NLMSG_LENGTH(sizeof(*t));
308 fprintf(stderr, "Wrong len %d\n", len);
312 parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
314 if (NULL == tb[TCA_ACT_TAB]) {
315 if (n->nlmsg_type != RTM_GETACTION)
316 fprintf(stderr, "do_print_action: NULL kind\n");
320 if (n->nlmsg_type == RTM_DELACTION) {
321 if (n->nlmsg_flags & NLM_F_ROOT) {
322 fprintf(fp, "Flushed table ");
325 fprintf(fp, "deleted action ");
329 if (n->nlmsg_type == RTM_NEWACTION)
330 fprintf(fp, "Added action ");
331 tc_print_action(fp, tb[TCA_ACT_TAB]);
336 int tc_action_gd(int cmd, unsigned flags, int *argc_p, char ***argv_p)
339 struct action_util *a = NULL;
341 char **argv = *argv_p;
345 struct sockaddr_nl nladdr;
347 struct rtattr *tail2;
348 struct nlmsghdr *ans = NULL;
356 req.t.tca_family = AF_UNSPEC;
358 memset(&req, 0, sizeof(req));
360 memset(&nladdr, 0, sizeof(nladdr));
361 nladdr.nl_family = AF_NETLINK;
363 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
364 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
365 req.n.nlmsg_type = cmd;
370 tail = NLMSG_TAIL(&req.n);
371 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
374 if (strcmp(*argv, "action") == 0 ) {
378 } else if (strcmp(*argv, "help") == 0) {
382 strncpy(k, *argv, sizeof (k) - 1);
383 a = get_action_kind(k);
385 fprintf(stderr, "Error: non existent action: %s\n",k);
389 if (strcmp(a->id, k) != 0) {
390 fprintf(stderr, "Error: non existent action: %s\n",k);
398 fprintf(stderr, "Error: no index specified action: %s\n",k);
403 if (matches(*argv, "index") == 0) {
405 if (get_u32(&i, *argv, 10)) {
406 fprintf(stderr, "Illegal \"index\"\n");
413 fprintf(stderr, "Error: no index specified action: %s\n",k);
418 tail2 = NLMSG_TAIL(&req.n);
419 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
420 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
421 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
422 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
426 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
428 req.n.nlmsg_seq = rth.dump = ++rth.seq;
429 if (cmd == RTM_GETACTION)
432 if (rtnl_talk(&rth, &req.n, 0, 0, ans, NULL, NULL) < 0) {
433 fprintf(stderr, "We have an error talking to the kernel\n");
437 if (ans && do_print_action(NULL, &req.n, (void*)stdout) < 0) {
438 fprintf(stderr, "Dump terminated\n");
448 int tc_action_modify(int cmd, unsigned flags, int *argc_p, char ***argv_p)
451 char **argv = *argv_p;
461 req.t.tca_family = AF_UNSPEC;
463 memset(&req, 0, sizeof(req));
465 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
466 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
467 req.n.nlmsg_type = cmd;
468 tail = NLMSG_TAIL(&req.n);
471 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
472 fprintf(stderr, "Illegal \"action\"\n");
475 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
477 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
478 fprintf(stderr, "We have an error talking to the kernel\n");
488 int tc_act_list_or_flush(int argc, char **argv, int event)
490 int ret = 0, prio = 0, msg_size = 0;
492 struct rtattr *tail,*tail2;
493 struct action_util *a = NULL;
500 req.t.tca_family = AF_UNSPEC;
502 memset(&req, 0, sizeof(req));
504 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
506 tail = NLMSG_TAIL(&req.n);
507 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
508 tail2 = NLMSG_TAIL(&req.n);
510 strncpy(k, *argv, sizeof (k) - 1);
513 get_action_kind("gact");
516 a = get_action_kind(k);
518 fprintf(stderr,"bad action %s\n",k);
521 if (strcmp(a->id, k) != 0) {
522 fprintf(stderr,"bad action %s\n",k);
525 strncpy(k, *argv, sizeof (k) - 1);
527 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
528 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
529 tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
530 tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
532 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
534 if (event == RTM_GETACTION) {
535 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
536 perror("Cannot send dump request");
539 ret = rtnl_dump_filter(&rth, do_print_action, stdout, NULL, NULL);
542 if (event == RTM_DELACTION) {
543 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
544 req.n.nlmsg_type = RTM_DELACTION;
545 req.n.nlmsg_flags |= NLM_F_ROOT;
546 req.n.nlmsg_flags |= NLM_F_REQUEST;
547 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
548 fprintf(stderr, "We have an error flushing\n");
559 int do_action(int argc, char **argv)
566 if (matches(*argv, "add") == 0) {
567 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
568 } else if (matches(*argv, "change") == 0 ||
569 matches(*argv, "replace") == 0) {
570 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
571 } else if (matches(*argv, "delete") == 0) {
574 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
575 } else if (matches(*argv, "get") == 0) {
578 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
579 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
580 || matches(*argv, "lst") == 0) {
585 return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
586 } else if (matches(*argv, "flush") == 0) {
591 return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
592 } else if (matches(*argv, "help") == 0) {
601 fprintf(stderr, "Command \"%s\" is unknown, try \"tc action help\".\n", *argv);