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>
33 static struct action_util * action_list;
35 int gact_ld = 0 ; //fuckin backward compatibility
42 fprintf (stderr, "action usage improper\n");
45 static int print_noaopt(struct action_util *au, FILE *f, struct rtattr *opt)
47 if (opt && RTA_PAYLOAD(opt))
48 fprintf(f, "[Unknown action, optlen=%u] ", RTA_PAYLOAD(opt));
52 static int parse_noaopt(struct action_util *au, int *argc_p, char ***argv_p, int code, struct nlmsghdr *n)
55 char **argv = *argv_p;
58 fprintf(stderr, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au->id, *argv);
60 fprintf(stderr, "Unknown action \"%s\"\n", au->id);
65 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 */
149 (struct rtattr *) (((void *) n) + NLMSG_ALIGN(n->nlmsg_len));
151 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
155 memset(k, 0, sizeof (k));
157 if (strcmp(*argv, "action") == 0 ) {
163 get_action_kind("gact");
167 } else if (strcmp(*argv, "help") == 0) {
169 } else if (new_cmd(argv)) {
172 struct action_util *a = NULL;
173 strncpy(k, *argv, sizeof (k) - 1);
176 a = get_action_kind(k);
190 (struct rtattr *) (((void *) n) +
191 NLMSG_ALIGN(n->nlmsg_len));
192 addattr_l(n, MAX_MSG, ++prio, NULL, 0);
193 addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
195 ret = a->parse_aopt(a,&argc, &argv, TCA_ACT_OPTIONS, n);
198 fprintf(stderr,"bad action parsing\n");
202 (((void *) n) + NLMSG_ALIGN(n->nlmsg_len)) -
210 fprintf(stderr,"bad action empty %d\n",eap);
215 (((void *) n) + NLMSG_ALIGN(n->nlmsg_len)) - (void *) tail2;
222 /* no need to undo things, returning from here should
223 * cause enough pain */
224 fprintf(stderr, "parse_action: bad value (%d:%s)!\n",argc,*argv);
229 tc_print_one_action(FILE * f, struct rtattr *arg)
232 struct rtattr *tb[TCA_ACT_MAX + 1];
234 struct action_util *a = NULL;
239 memset(tb, 0, sizeof (tb));
240 parse_rtattr(tb, TCA_ACT_MAX, RTA_DATA(arg), RTA_PAYLOAD(arg));
241 if (tb[TCA_ACT_KIND] == NULL) {
242 fprintf(stderr, "NULL Action!\n");
247 a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
252 fprintf(f," %s \n", a->id);
257 err = a->print_aopt(a,f,tb[TCA_ACT_OPTIONS]);
263 if (show_stats && tb[TCA_STATS]) {
265 print_tcstats_attr(f, tb[TCA_STATS]);
273 tc_print_action(FILE * f, const struct rtattr *arg)
277 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
282 memset(tb, 0, sizeof (tb));
283 parse_rtattr(tb, TCA_ACT_MAX_PRIO, RTA_DATA(arg), RTA_PAYLOAD(arg));
285 if (tab_flush && NULL != tb[0] && NULL == tb[1]) {
286 int ret = tc_print_one_action(f, tb[0]);
290 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
292 fprintf(f, "\n\taction order %d: ", i + batch_c);
293 if (0 > tc_print_one_action(f, tb[i])) {
294 fprintf(f, "Error printing action\n");
300 batch_c+=TCA_ACT_MAX_PRIO ;
304 static int do_print_action(const struct sockaddr_nl *who,
305 const struct nlmsghdr *n,
308 FILE *fp = (FILE*)arg;
309 struct tcamsg *t = NLMSG_DATA(n);
310 int len = n->nlmsg_len;
311 struct rtattr * tb[TCAA_MAX+1];
313 len -= NLMSG_LENGTH(sizeof(*t));
316 fprintf(stderr, "Wrong len %d\n", len);
320 memset(tb, 0, sizeof(tb));
321 parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
323 if (NULL == tb[TCA_ACT_TAB]) {
324 if (n->nlmsg_type != RTM_GETACTION)
325 fprintf(stderr, "do_print_action: NULL kind\n");
329 if (n->nlmsg_type == RTM_DELACTION) {
330 if (n->nlmsg_flags & NLM_F_ROOT) {
331 fprintf(fp, "Flushed table ");
334 fprintf(fp, "deleted action ");
338 if (n->nlmsg_type == RTM_NEWACTION)
339 fprintf(fp, "Added action ");
340 tc_print_action(fp, tb[TCA_ACT_TAB]);
345 int tc_action_gd(int cmd, unsigned flags, int *argc_p, char ***argv_p)
348 struct action_util *a = NULL;
350 char **argv = *argv_p;
354 struct rtnl_handle rth;
355 struct sockaddr_nl nladdr;
357 struct rtattr *tail2;
358 struct nlmsghdr *ans = NULL;
366 req.t.tca_family = AF_UNSPEC;
368 memset(&req, 0, sizeof(req));
370 memset(&nladdr, 0, sizeof(nladdr));
371 nladdr.nl_family = AF_NETLINK;
373 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
374 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
375 req.n.nlmsg_type = cmd;
380 tail = (struct rtattr*)(((void*)&req.n)+NLMSG_ALIGN(req.n.nlmsg_len));
382 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
385 if (strcmp(*argv, "action") == 0 ) {
389 } else if (strcmp(*argv, "help") == 0) {
393 strncpy(k, *argv, sizeof (k) - 1);
394 a = get_action_kind(k);
396 fprintf(stderr, "Error: non existent action: %s\n",k);
400 if (strcmp(a->id, k) != 0) {
401 fprintf(stderr, "Error: non existent action: %s\n",k);
409 fprintf(stderr, "Error: no index specified action: %s\n",k);
414 if (matches(*argv, "index") == 0) {
416 if (get_u32(&i, *argv, 10)) {
417 fprintf(stderr, "Illegal \"index\"\n");
424 fprintf(stderr, "Error: no index specified action: %s\n",k);
430 (struct rtattr *) (((void *) &req.n) + NLMSG_ALIGN(req.n.nlmsg_len));
431 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
432 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
433 addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
435 (((void *) &req.n) + NLMSG_ALIGN(req.n.nlmsg_len)) - (void *) tail2;
439 tail->rta_len = (((void*)&req.n)+ NLMSG_ALIGN(req.n.nlmsg_len)) - (void*)tail;
441 if (rtnl_open(&rth, 0) < 0) {
442 fprintf(stderr, "Cannot open rtnetlink\n");
446 req.n.nlmsg_seq = rth.dump = ++rth.seq;
447 if (cmd == RTM_GETACTION)
449 if (rtnl_talk(&rth, &req.n, 0, 0, ans, NULL, NULL) < 0) {
450 fprintf(stderr, "We have an error talking to the kernel\n");
455 if (ans && do_print_action(NULL, &req.n, (void*)stdout) < 0) {
456 fprintf(stderr, "Dump terminated\n");
468 int tc_action_modify(int cmd, unsigned flags, int *argc_p, char ***argv_p)
471 char **argv = *argv_p;
474 struct rtnl_handle rth;
482 req.t.tca_family = AF_UNSPEC;
484 memset(&req, 0, sizeof(req));
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 = (struct rtattr*)(((void*)&req.n)+NLMSG_ALIGN(req.n.nlmsg_len));
492 if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
493 fprintf(stderr, "Illegal \"action\"\n");
496 tail->rta_len = (((void*)&req.n)+req.n.nlmsg_len) - (void*)tail;
498 if (rtnl_open(&rth, 0) < 0) {
499 fprintf(stderr, "Cannot open rtnetlink\n");
504 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
505 fprintf(stderr, "We have an error talking to the kernel\n");
515 int tc_act_list_or_flush(int argc, char **argv, int event)
517 int ret = 0, prio = 0, msg_size = 0;
519 struct rtnl_handle rth;
520 struct rtattr *tail,*tail2;
521 struct action_util *a = NULL;
528 req.t.tca_family = AF_UNSPEC;
530 memset(&req, 0, sizeof(req));
532 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
534 tail = (struct rtattr*)(((void*)&req.n)+NLMSG_ALIGN(req.n.nlmsg_len));
536 addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
538 (struct rtattr *) (((void *) &req.n) + NLMSG_ALIGN(req.n.nlmsg_len));
540 strncpy(k, *argv, sizeof (k) - 1);
543 get_action_kind("gact");
546 a = get_action_kind(k);
548 fprintf(stderr,"bad action %s\n",k);
551 if (strcmp(a->id, k) != 0) {
552 fprintf(stderr,"bad action %s\n",k);
555 strncpy(k, *argv, sizeof (k) - 1);
557 addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
558 addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
560 (((void *) &req.n) + NLMSG_ALIGN(req.n.nlmsg_len)) - (void *) tail2;
561 tail->rta_len = (((void*)&req.n)+NLMSG_ALIGN(req.n.nlmsg_len)) - (void*)tail;
564 if (rtnl_open(&rth, 0) < 0) {
565 fprintf(stderr, "Cannot open rtnetlink\n");
569 msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
571 if (event == RTM_GETACTION) {
572 if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
573 perror("Cannot send dump request");
576 ret = rtnl_dump_filter(&rth, do_print_action, stdout, NULL, NULL);
579 if (event == RTM_DELACTION) {
580 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
581 req.n.nlmsg_type = RTM_DELACTION;
582 req.n.nlmsg_flags |= NLM_F_ROOT;
583 req.n.nlmsg_flags |= NLM_F_REQUEST;
584 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
585 fprintf(stderr, "We have an error flushing\n");
598 int do_action(int argc, char **argv)
605 if (matches(*argv, "add") == 0) {
606 ret = tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
607 } else if (matches(*argv, "change") == 0 ||
608 matches(*argv, "replace") == 0) {
609 ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
610 } else if (matches(*argv, "delete") == 0) {
613 ret = tc_action_gd(RTM_DELACTION, 0, &argc, &argv);
614 } else if (matches(*argv, "get") == 0) {
617 ret = tc_action_gd(RTM_GETACTION, 0, &argc, &argv);
618 } else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
619 || matches(*argv, "lst") == 0) {
624 return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
625 } else if (matches(*argv, "flush") == 0) {
630 return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
631 } else if (matches(*argv, "help") == 0) {
640 fprintf(stderr, "Command \"%s\" is unknown, try \"tc action help\".\n", *argv);