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
;
157 char k
[FILTER_NAMESZ
];
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
]) {
304 print_string(PRINT_FP
, NULL
, "\tAction statistics:\n", NULL
);
305 open_json_object("stats");
306 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
308 print_string(PRINT_FP
, NULL
, "\n", NULL
);
310 if (tb
[TCA_ACT_COOKIE
]) {
311 int strsz
= RTA_PAYLOAD(tb
[TCA_ACT_COOKIE
]);
312 char b1
[strsz
* 2 + 1];
314 print_string(PRINT_ANY
, "cookie", "\tcookie %s\n",
315 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
316 strsz
, b1
, sizeof(b1
)));
323 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
326 struct rtattr
*tb
[TCA_MAX
+ 1];
328 struct action_util
*a
= NULL
;
329 __u32
*delete_count
= 0;
331 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
333 if (tb
[TCA_KIND
] == NULL
) {
334 fprintf(stderr
, "NULL Action!\n");
338 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
342 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
343 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
349 tc_print_action(FILE *f
, const struct rtattr
*arg
, unsigned short tot_acts
)
358 tot_acts
= TCA_ACT_MAX_PRIO
;
360 struct rtattr
*tb
[tot_acts
+ 1];
362 parse_rtattr_nested(tb
, tot_acts
, arg
);
364 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1])
365 return tc_print_action_flush(f
, tb
[0]);
367 open_json_array(PRINT_JSON
, "actions");
368 for (i
= 0; i
< tot_acts
; i
++) {
370 open_json_object(NULL
);
371 print_uint(PRINT_ANY
, "order",
372 "\n\taction order %u: ", i
);
373 if (tc_print_one_action(f
, tb
[i
]) < 0) {
374 print_string(PRINT_FP
, NULL
,
375 "Error printing action\n", NULL
);
386 int print_action(const struct sockaddr_nl
*who
,
390 FILE *fp
= (FILE *)arg
;
391 struct tcamsg
*t
= NLMSG_DATA(n
);
392 int len
= n
->nlmsg_len
;
393 __u32
*tot_acts
= NULL
;
394 struct rtattr
*tb
[TCA_ROOT_MAX
+1];
396 len
-= NLMSG_LENGTH(sizeof(*t
));
399 fprintf(stderr
, "Wrong len %d\n", len
);
403 parse_rtattr(tb
, TCA_ROOT_MAX
, TA_RTA(t
), len
);
405 if (tb
[TCA_ROOT_COUNT
])
406 tot_acts
= RTA_DATA(tb
[TCA_ROOT_COUNT
]);
408 fprintf(fp
, "total acts %d\n", tot_acts
? *tot_acts
:0);
409 if (tb
[TCA_ACT_TAB
] == NULL
) {
410 if (n
->nlmsg_type
!= RTM_GETACTION
)
411 fprintf(stderr
, "print_action: NULL kind\n");
415 if (n
->nlmsg_type
== RTM_DELACTION
) {
416 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
417 fprintf(fp
, "Flushed table ");
420 fprintf(fp
, "Deleted action ");
424 if (n
->nlmsg_type
== RTM_NEWACTION
) {
425 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
426 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
427 fprintf(fp
, "Added action ");
428 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
429 fprintf(fp
, "Replaced action ");
434 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
439 static int tc_action_gd(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
441 char k
[FILTER_NAMESZ
];
442 struct action_util
*a
= NULL
;
444 char **argv
= *argv_p
;
449 struct rtattr
*tail2
;
450 struct nlmsghdr
*ans
= NULL
;
457 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
458 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
460 .t
.tca_family
= AF_UNSPEC
,
467 tail
= NLMSG_TAIL(&req
.n
);
468 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
471 if (strcmp(*argv
, "action") == 0) {
475 } else if (strcmp(*argv
, "help") == 0) {
479 strncpy(k
, *argv
, sizeof(k
) - 1);
480 a
= get_action_kind(k
);
482 fprintf(stderr
, "Error: non existent action: %s\n", k
);
486 if (strcmp(a
->id
, k
) != 0) {
487 fprintf(stderr
, "Error: non existent action: %s\n", k
);
495 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
500 if (matches(*argv
, "index") == 0) {
502 if (get_u32(&i
, *argv
, 10)) {
503 fprintf(stderr
, "Illegal \"index\"\n");
510 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
515 tail2
= NLMSG_TAIL(&req
.n
);
516 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
517 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
519 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
520 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
524 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
526 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
528 if (rtnl_talk(&rth
, &req
.n
, &ans
) < 0) {
529 fprintf(stderr
, "We have an error talking to the kernel\n");
533 if (cmd
== RTM_GETACTION
&& print_action(NULL
, ans
, stdout
) < 0) {
534 fprintf(stderr
, "Dump terminated\n");
546 static int tc_action_modify(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
549 char **argv
= *argv_p
;
556 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
557 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
559 .t
.tca_family
= AF_UNSPEC
,
561 struct rtattr
*tail
= NLMSG_TAIL(&req
.n
);
565 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
566 fprintf(stderr
, "Illegal \"action\"\n");
569 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
571 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
572 fprintf(stderr
, "We have an error talking to the kernel\n");
582 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
584 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
585 int ret
= 0, prio
= 0, msg_size
= 0;
586 struct action_util
*a
= NULL
;
587 struct nla_bitfield32 flag_select
= { 0 };
588 char **argv
= *argv_p
;
589 __u32 msec_since
= 0;
591 char k
[FILTER_NAMESZ
];
597 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
598 .t
.tca_family
= AF_UNSPEC
,
601 tail
= NLMSG_TAIL(&req
.n
);
602 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
603 tail2
= NLMSG_TAIL(&req
.n
);
605 strncpy(k
, *argv
, sizeof(k
) - 1);
608 get_action_kind("gact");
611 a
= get_action_kind(k
);
613 fprintf(stderr
, "bad action %s\n", k
);
616 if (strcmp(a
->id
, k
) != 0) {
617 fprintf(stderr
, "bad action %s\n", k
);
620 strncpy(k
, *argv
, sizeof(k
) - 1);
625 if (argc
&& (strcmp(*argv
, "since") == 0)) {
627 if (get_u32(&msec_since
, *argv
, 0))
628 invarg("dump time \"since\" is invalid", *argv
);
631 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
632 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
633 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
634 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
636 tail3
= NLMSG_TAIL(&req
.n
);
637 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
638 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
639 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
640 sizeof(struct nla_bitfield32
));
641 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
643 tail4
= NLMSG_TAIL(&req
.n
);
644 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
645 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
647 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
649 if (event
== RTM_GETACTION
) {
650 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
651 perror("Cannot send dump request");
654 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
657 if (event
== RTM_DELACTION
) {
658 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
659 req
.n
.nlmsg_type
= RTM_DELACTION
;
660 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
661 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
662 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
663 fprintf(stderr
, "We have an error flushing\n");
676 int do_action(int argc
, char **argv
)
683 if (matches(*argv
, "add") == 0) {
684 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
685 } else if (matches(*argv
, "change") == 0 ||
686 matches(*argv
, "replace") == 0) {
687 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
688 } else if (matches(*argv
, "delete") == 0) {
691 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
692 } else if (matches(*argv
, "get") == 0) {
695 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
696 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
697 || matches(*argv
, "lst") == 0) {
705 return tc_act_list_or_flush(&argc
, &argv
,
707 } else if (matches(*argv
, "flush") == 0) {
715 return tc_act_list_or_flush(&argc
, &argv
,
717 } else if (matches(*argv
, "help") == 0) {
721 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);