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
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
27 #include "tc_common.h"
30 static struct action_util
*action_list
;
32 int gact_ld
; /* f*ckin backward compatibility */
36 static void act_usage(void)
38 /*XXX: In the near future add a action->print_help to improve
40 * This would mean new tc will not be backward compatible
41 * with any action .so from the old days. But if someone really
42 * does that, they would know how to fix this ..
45 fprintf(stderr
, "usage: tc actions <ACTSPECOP>*\n");
47 "Where: \tACTSPECOP := ACR | GD | FL\n"
48 "\tACR := add | change | replace <ACTSPEC>*\n"
49 "\tGD := get | delete | <ACTISPEC>*\n"
50 "\tFL := ls | list | flush | <ACTNAMESPEC>\n"
51 "\tACTNAMESPEC := action <ACTNAME>\n"
52 "\tACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
53 "\tACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
54 "\tINDEXSPEC := index <32 bit indexvalue>\n"
55 "\tACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
56 "\t\tExample ACTNAME is gact, mirred, bpf, etc\n"
57 "\t\tEach action has its own parameters (ACTPARAMS)\n"
63 static int print_noaopt(struct action_util
*au
, FILE *f
, struct rtattr
*opt
)
65 if (opt
&& RTA_PAYLOAD(opt
))
66 fprintf(f
, "[Unknown action, optlen=%u] ",
67 (unsigned int) RTA_PAYLOAD(opt
));
71 static int parse_noaopt(struct action_util
*au
, int *argc_p
, char ***argv_p
, int code
, struct nlmsghdr
*n
)
74 char **argv
= *argv_p
;
77 fprintf(stderr
, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au
->id
, *argv
);
79 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
84 static struct action_util
*get_action_kind(char *str
)
89 struct action_util
*a
;
94 for (a
= action_list
; a
; a
= a
->next
) {
95 if (strcmp(a
->id
, str
) == 0)
99 snprintf(buf
, sizeof(buf
), "%s/m_%s.so", get_tc_lib(), str
);
100 dlh
= dlopen(buf
, RTLD_LAZY
| RTLD_GLOBAL
);
104 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
110 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
116 a
->next
= action_list
;
128 a
= calloc(1, sizeof(*a
));
130 strncpy(a
->id
, "noact", 15);
131 a
->parse_aopt
= parse_noaopt
;
132 a
->print_aopt
= print_noaopt
;
141 if ((matches(*argv
, "change") == 0) ||
142 (matches(*argv
, "replace") == 0) ||
143 (matches(*argv
, "delete") == 0) ||
144 (matches(*argv
, "get") == 0) ||
145 (matches(*argv
, "add") == 0))
152 int parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
155 char **argv
= *argv_p
;
156 struct rtattr
*tail
, *tail2
;
160 int eap
= 0; /* expect action parameters */
164 unsigned char act_ck
[TC_COOKIE_MAX_SIZE
];
169 tail
= tail2
= NLMSG_TAIL(n
);
171 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
175 memset(k
, 0, sizeof(k
));
177 if (strcmp(*argv
, "action") == 0) {
183 get_action_kind("gact");
187 } else if (strcmp(*argv
, "flowid") == 0) {
189 } else if (strcmp(*argv
, "classid") == 0) {
191 } else if (strcmp(*argv
, "help") == 0) {
193 } else if (new_cmd(argv
)) {
196 struct action_util
*a
= NULL
;
198 strncpy(k
, *argv
, sizeof(k
) - 1);
201 a
= get_action_kind(k
);
214 tail
= NLMSG_TAIL(n
);
215 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
216 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
218 ret
= a
->parse_aopt(a
, &argc
, &argv
, TCA_ACT_OPTIONS
,
222 fprintf(stderr
, "bad action parsing\n");
226 if (*argv
&& strcmp(*argv
, "cookie") == 0) {
230 slen
= strlen(*argv
);
231 if (slen
> TC_COOKIE_MAX_SIZE
* 2) {
232 char cookie_err_m
[128];
234 snprintf(cookie_err_m
, 128,
235 "%zd Max allowed size %d",
236 slen
, TC_COOKIE_MAX_SIZE
*2);
237 invarg(cookie_err_m
, *argv
);
240 if (hex2mem(*argv
, act_ck
, slen
/ 2) < 0)
241 invarg("cookie must be a hex string\n",
244 act_ck_len
= slen
/ 2;
250 addattr_l(n
, MAX_MSG
, TCA_ACT_COOKIE
,
251 &act_ck
, act_ck_len
);
253 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
259 fprintf(stderr
, "bad action empty %d\n", eap
);
263 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
270 /* no need to undo things, returning from here should
271 * cause enough pain */
272 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n", argc
, *argv
);
276 static int tc_print_one_action(FILE *f
, struct rtattr
*arg
)
279 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
281 struct action_util
*a
= NULL
;
286 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
288 if (tb
[TCA_ACT_KIND
] == NULL
) {
289 fprintf(stderr
, "NULL Action!\n");
294 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
298 err
= a
->print_aopt(a
, f
, tb
[TCA_ACT_OPTIONS
]);
303 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
305 fprintf(f
, "\tAction statistics:\n");
306 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
307 if (tb
[TCA_ACT_COOKIE
]) {
308 int strsz
= RTA_PAYLOAD(tb
[TCA_ACT_COOKIE
]);
309 char b1
[strsz
* 2 + 1];
311 fprintf(f
, "\n\tcookie len %d %s ", strsz
,
312 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
313 strsz
, b1
, sizeof(b1
)));
322 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
325 struct rtattr
*tb
[TCA_MAX
+ 1];
327 struct action_util
*a
= NULL
;
328 __u32
*delete_count
= 0;
330 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
332 if (tb
[TCA_KIND
] == NULL
) {
333 fprintf(stderr
, "NULL Action!\n");
337 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
341 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
342 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
348 tc_print_action(FILE *f
, const struct rtattr
*arg
, unsigned short tot_acts
)
357 tot_acts
= TCA_ACT_MAX_PRIO
;
359 struct rtattr
*tb
[tot_acts
+ 1];
361 parse_rtattr_nested(tb
, tot_acts
, arg
);
363 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1])
364 return tc_print_action_flush(f
, tb
[0]);
366 for (i
= 0; i
< tot_acts
; i
++) {
368 fprintf(f
, "\n\taction order %d: ", i
);
369 if (tc_print_one_action(f
, tb
[i
]) < 0) {
370 fprintf(f
, "Error printing action\n");
379 int print_action(const struct sockaddr_nl
*who
,
383 FILE *fp
= (FILE *)arg
;
384 struct tcamsg
*t
= NLMSG_DATA(n
);
385 int len
= n
->nlmsg_len
;
386 __u32
*tot_acts
= NULL
;
387 struct rtattr
*tb
[TCA_ROOT_MAX
+1];
389 len
-= NLMSG_LENGTH(sizeof(*t
));
392 fprintf(stderr
, "Wrong len %d\n", len
);
396 parse_rtattr(tb
, TCA_ROOT_MAX
, TA_RTA(t
), len
);
398 if (tb
[TCA_ROOT_COUNT
])
399 tot_acts
= RTA_DATA(tb
[TCA_ROOT_COUNT
]);
401 fprintf(fp
, "total acts %d\n", tot_acts
? *tot_acts
:0);
402 if (tb
[TCA_ACT_TAB
] == NULL
) {
403 if (n
->nlmsg_type
!= RTM_GETACTION
)
404 fprintf(stderr
, "print_action: NULL kind\n");
408 if (n
->nlmsg_type
== RTM_DELACTION
) {
409 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
410 fprintf(fp
, "Flushed table ");
413 fprintf(fp
, "Deleted action ");
417 if (n
->nlmsg_type
== RTM_NEWACTION
) {
418 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
419 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
420 fprintf(fp
, "Added action ");
421 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
422 fprintf(fp
, "Replaced action ");
427 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
432 static int tc_action_gd(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
435 struct action_util
*a
= NULL
;
437 char **argv
= *argv_p
;
442 struct rtattr
*tail2
;
443 struct nlmsghdr
*ans
= NULL
;
450 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
451 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
453 .t
.tca_family
= AF_UNSPEC
,
460 tail
= NLMSG_TAIL(&req
.n
);
461 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
464 if (strcmp(*argv
, "action") == 0) {
468 } else if (strcmp(*argv
, "help") == 0) {
472 strncpy(k
, *argv
, sizeof(k
) - 1);
473 a
= get_action_kind(k
);
475 fprintf(stderr
, "Error: non existent action: %s\n", k
);
479 if (strcmp(a
->id
, k
) != 0) {
480 fprintf(stderr
, "Error: non existent action: %s\n", k
);
488 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
493 if (matches(*argv
, "index") == 0) {
495 if (get_u32(&i
, *argv
, 10)) {
496 fprintf(stderr
, "Illegal \"index\"\n");
503 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
508 tail2
= NLMSG_TAIL(&req
.n
);
509 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
510 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
512 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
513 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
517 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
519 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
521 if (rtnl_talk(&rth
, &req
.n
, &ans
) < 0) {
522 fprintf(stderr
, "We have an error talking to the kernel\n");
526 if (cmd
== RTM_GETACTION
&& print_action(NULL
, ans
, stdout
) < 0) {
527 fprintf(stderr
, "Dump terminated\n");
539 static int tc_action_modify(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
542 char **argv
= *argv_p
;
549 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
550 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
552 .t
.tca_family
= AF_UNSPEC
,
554 struct rtattr
*tail
= NLMSG_TAIL(&req
.n
);
558 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
559 fprintf(stderr
, "Illegal \"action\"\n");
562 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
564 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
565 fprintf(stderr
, "We have an error talking to the kernel\n");
575 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
577 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
578 int ret
= 0, prio
= 0, msg_size
= 0;
579 struct action_util
*a
= NULL
;
580 struct nla_bitfield32 flag_select
= { 0 };
581 char **argv
= *argv_p
;
582 __u32 msec_since
= 0;
590 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
591 .t
.tca_family
= AF_UNSPEC
,
594 tail
= NLMSG_TAIL(&req
.n
);
595 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
596 tail2
= NLMSG_TAIL(&req
.n
);
598 strncpy(k
, *argv
, sizeof(k
) - 1);
601 get_action_kind("gact");
604 a
= get_action_kind(k
);
606 fprintf(stderr
, "bad action %s\n", k
);
609 if (strcmp(a
->id
, k
) != 0) {
610 fprintf(stderr
, "bad action %s\n", k
);
613 strncpy(k
, *argv
, sizeof(k
) - 1);
618 if (argc
&& (strcmp(*argv
, "since") == 0)) {
620 if (get_u32(&msec_since
, *argv
, 0))
621 invarg("dump time \"since\" is invalid", *argv
);
624 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
625 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
626 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
627 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
629 tail3
= NLMSG_TAIL(&req
.n
);
630 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
631 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
632 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
633 sizeof(struct nla_bitfield32
));
634 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
636 tail4
= NLMSG_TAIL(&req
.n
);
637 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
638 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
640 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
642 if (event
== RTM_GETACTION
) {
643 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
644 perror("Cannot send dump request");
647 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
650 if (event
== RTM_DELACTION
) {
651 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
652 req
.n
.nlmsg_type
= RTM_DELACTION
;
653 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
654 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
655 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
656 fprintf(stderr
, "We have an error flushing\n");
669 int do_action(int argc
, char **argv
)
676 if (matches(*argv
, "add") == 0) {
677 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
678 } else if (matches(*argv
, "change") == 0 ||
679 matches(*argv
, "replace") == 0) {
680 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
681 } else if (matches(*argv
, "delete") == 0) {
684 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
685 } else if (matches(*argv
, "get") == 0) {
688 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
689 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
690 || matches(*argv
, "lst") == 0) {
698 return tc_act_list_or_flush(&argc
, &argv
,
700 } else if (matches(*argv
, "flush") == 0) {
708 return tc_act_list_or_flush(&argc
, &argv
,
710 } else if (matches(*argv
, "help") == 0) {
714 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);