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
; /* f*ckin backward compatibility */
37 static void act_usage(void)
39 /*XXX: In the near future add a action->print_help to improve
41 * This would mean new tc will not be backward compatible
42 * with any action .so from the old days. But if someone really
43 * does that, they would know how to fix this ..
46 fprintf(stderr
, "usage: tc actions <ACTSPECOP>*\n");
48 "Where: \tACTSPECOP := ACR | GD | FL\n"
49 "\tACR := add | change | replace <ACTSPEC>*\n"
50 "\tGD := get | delete | <ACTISPEC>*\n"
51 "\tFL := ls | list | flush | <ACTNAMESPEC>\n"
52 "\tACTNAMESPEC := action <ACTNAME>\n"
53 "\tACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
54 "\tACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
55 "\tINDEXSPEC := index <32 bit indexvalue>\n"
56 "\tACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
57 "\t\tExample ACTNAME is gact, mirred, bpf, etc\n"
58 "\t\tEach action has its own parameters (ACTPARAMS)\n"
64 static int print_noaopt(struct action_util
*au
, FILE *f
, struct rtattr
*opt
)
66 if (opt
&& RTA_PAYLOAD(opt
))
67 fprintf(f
, "[Unknown action, optlen=%u] ",
68 (unsigned int) RTA_PAYLOAD(opt
));
72 static int parse_noaopt(struct action_util
*au
, int *argc_p
,
73 char ***argv_p
, int code
, struct nlmsghdr
*n
)
76 char **argv
= *argv_p
;
80 "Unknown action \"%s\", hence option \"%s\" is unparsable\n",
83 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
88 static struct action_util
*get_action_kind(char *str
)
93 struct action_util
*a
;
98 for (a
= action_list
; a
; a
= a
->next
) {
99 if (strcmp(a
->id
, str
) == 0)
103 snprintf(buf
, sizeof(buf
), "%s/m_%s.so", get_tc_lib(), str
);
104 dlh
= dlopen(buf
, RTLD_LAZY
| RTLD_GLOBAL
);
108 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
114 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
120 a
->next
= action_list
;
132 a
= calloc(1, sizeof(*a
));
134 strncpy(a
->id
, "noact", 15);
135 a
->parse_aopt
= parse_noaopt
;
136 a
->print_aopt
= print_noaopt
;
145 return (matches(*argv
, "change") == 0) ||
146 (matches(*argv
, "replace") == 0) ||
147 (matches(*argv
, "delete") == 0) ||
148 (matches(*argv
, "get") == 0) ||
149 (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 tail2
= addattr_nest(n
, MAX_MSG
, tca_id
);
173 memset(k
, 0, sizeof(k
));
175 if (strcmp(*argv
, "action") == 0) {
181 get_action_kind("gact");
184 } else if (strcmp(*argv
, "flowid") == 0) {
186 } else if (strcmp(*argv
, "classid") == 0) {
188 } else if (strcmp(*argv
, "help") == 0) {
190 } else if (new_cmd(argv
)) {
193 struct action_util
*a
= NULL
;
195 if (!action_a2n(*argv
, NULL
, false))
196 strncpy(k
, "gact", sizeof(k
) - 1);
198 strncpy(k
, *argv
, sizeof(k
) - 1);
201 a
= get_action_kind(k
);
214 tail
= addattr_nest(n
, MAX_MSG
, ++prio
);
215 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
217 ret
= a
->parse_aopt(a
, &argc
, &argv
, TCA_ACT_OPTIONS
,
221 fprintf(stderr
, "bad action parsing\n");
225 if (*argv
&& strcmp(*argv
, "cookie") == 0) {
229 slen
= strlen(*argv
);
230 if (slen
> TC_COOKIE_MAX_SIZE
* 2) {
231 char cookie_err_m
[128];
233 snprintf(cookie_err_m
, 128,
234 "%zd Max allowed size %d",
235 slen
, TC_COOKIE_MAX_SIZE
*2);
236 invarg(cookie_err_m
, *argv
);
239 if (hex2mem(*argv
, act_ck
, slen
/ 2) < 0)
240 invarg("cookie must be a hex string\n",
243 act_ck_len
= slen
/ 2;
249 addattr_l(n
, MAX_MSG
, TCA_ACT_COOKIE
,
250 &act_ck
, act_ck_len
);
252 addattr_nest_end(n
, tail
);
258 fprintf(stderr
, "bad action empty %d\n", eap
);
262 addattr_nest_end(n
, tail2
);
269 /* no need to undo things, returning from here should
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:", NULL
);
305 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
306 open_json_object("stats");
307 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
309 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
311 if (tb
[TCA_ACT_COOKIE
]) {
312 int strsz
= RTA_PAYLOAD(tb
[TCA_ACT_COOKIE
]);
313 char b1
[strsz
* 2 + 1];
315 print_string(PRINT_ANY
, "cookie", "\tcookie %s",
316 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
317 strsz
, b1
, sizeof(b1
)));
318 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
325 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
328 struct rtattr
*tb
[TCA_MAX
+ 1];
330 struct action_util
*a
= NULL
;
331 __u32
*delete_count
= 0;
333 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
335 if (tb
[TCA_KIND
] == NULL
) {
336 fprintf(stderr
, "NULL Action!\n");
340 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
344 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
345 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
351 tc_print_action(FILE *f
, const struct rtattr
*arg
, unsigned short tot_acts
)
360 tot_acts
= TCA_ACT_MAX_PRIO
;
362 struct rtattr
*tb
[tot_acts
+ 1];
364 parse_rtattr_nested(tb
, tot_acts
, arg
);
366 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1])
367 return tc_print_action_flush(f
, tb
[0]);
369 open_json_array(PRINT_JSON
, "actions");
370 for (i
= 0; i
<= tot_acts
; i
++) {
372 open_json_object(NULL
);
373 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
374 print_uint(PRINT_ANY
, "order",
375 "\taction order %u: ", i
);
376 if (tc_print_one_action(f
, tb
[i
]) < 0) {
377 print_string(PRINT_FP
, NULL
,
378 "Error printing action\n", NULL
);
384 close_json_array(PRINT_JSON
, NULL
);
389 int print_action(const struct sockaddr_nl
*who
,
393 FILE *fp
= (FILE *)arg
;
394 struct tcamsg
*t
= NLMSG_DATA(n
);
395 int len
= n
->nlmsg_len
;
396 __u32
*tot_acts
= NULL
;
397 struct rtattr
*tb
[TCA_ROOT_MAX
+1];
399 len
-= NLMSG_LENGTH(sizeof(*t
));
402 fprintf(stderr
, "Wrong len %d\n", len
);
406 parse_rtattr(tb
, TCA_ROOT_MAX
, TA_RTA(t
), len
);
408 if (tb
[TCA_ROOT_COUNT
])
409 tot_acts
= RTA_DATA(tb
[TCA_ROOT_COUNT
]);
411 open_json_object(NULL
);
412 print_uint(PRINT_ANY
, "total acts", "total acts %u",
413 tot_acts
? *tot_acts
: 0);
414 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
416 if (tb
[TCA_ACT_TAB
] == NULL
) {
417 if (n
->nlmsg_type
!= RTM_GETACTION
)
418 fprintf(stderr
, "print_action: NULL kind\n");
422 if (n
->nlmsg_type
== RTM_DELACTION
) {
423 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
424 fprintf(fp
, "Flushed table ");
427 fprintf(fp
, "Deleted action ");
431 if (n
->nlmsg_type
== RTM_NEWACTION
) {
432 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
433 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
434 fprintf(fp
, "Added action ");
435 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
436 fprintf(fp
, "Replaced action ");
440 open_json_object(NULL
);
441 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
447 static int tc_action_gd(int cmd
, unsigned int flags
,
448 int *argc_p
, char ***argv_p
)
450 char k
[FILTER_NAMESZ
];
451 struct action_util
*a
= NULL
;
453 char **argv
= *argv_p
;
458 struct rtattr
*tail2
;
459 struct nlmsghdr
*ans
= NULL
;
466 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
467 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
469 .t
.tca_family
= AF_UNSPEC
,
476 tail
= addattr_nest(&req
.n
, MAX_MSG
, TCA_ACT_TAB
);
479 if (strcmp(*argv
, "action") == 0) {
483 } else if (strcmp(*argv
, "help") == 0) {
487 strncpy(k
, *argv
, sizeof(k
) - 1);
488 a
= get_action_kind(k
);
490 fprintf(stderr
, "Error: non existent action: %s\n", k
);
494 if (strcmp(a
->id
, k
) != 0) {
495 fprintf(stderr
, "Error: non existent action: %s\n", k
);
504 "Error: no index specified action: %s\n", k
);
509 if (matches(*argv
, "index") == 0) {
511 if (get_u32(&i
, *argv
, 10)) {
512 fprintf(stderr
, "Illegal \"index\"\n");
520 "Error: no index specified action: %s\n", k
);
525 tail2
= addattr_nest(&req
.n
, MAX_MSG
, ++prio
);
526 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
528 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
529 addattr_nest_end(&req
.n
, tail2
);
533 addattr_nest_end(&req
.n
, tail
);
535 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
537 if (rtnl_talk(&rth
, &req
.n
, cmd
== RTM_DELACTION
? NULL
: &ans
) < 0) {
538 fprintf(stderr
, "We have an error talking to the kernel\n");
542 if (cmd
== RTM_GETACTION
) {
544 ret
= print_action(NULL
, ans
, stdout
);
546 fprintf(stderr
, "Dump terminated\n");
561 struct tc_action_req
{
567 static int tc_action_modify(int cmd
, unsigned int flags
,
568 int *argc_p
, char ***argv_p
,
569 void *buf
, size_t buflen
)
571 struct tc_action_req
*req
, action_req
;
572 char **argv
= *argv_p
;
580 if (buflen
< sizeof (struct tc_action_req
)) {
581 fprintf(stderr
, "buffer is too small: %zu\n", buflen
);
585 memset(&action_req
, 0, sizeof (struct tc_action_req
));
589 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
590 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
591 req
->n
.nlmsg_type
= cmd
;
592 req
->t
.tca_family
= AF_UNSPEC
;
593 tail
= NLMSG_TAIL(&req
->n
);
597 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
->n
)) {
598 fprintf(stderr
, "Illegal \"action\"\n");
601 tail
->rta_len
= (void *) NLMSG_TAIL(&req
->n
) - (void *) tail
;
609 iov
.iov_base
= &req
->n
;
610 iov
.iov_len
= req
->n
.nlmsg_len
;
611 if (rtnl_talk_iov(&rth
, &iov
, 1, NULL
) < 0) {
612 fprintf(stderr
, "We have an error talking to the kernel\n");
619 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
621 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
622 int ret
= 0, prio
= 0, msg_size
= 0;
623 struct action_util
*a
= NULL
;
624 struct nla_bitfield32 flag_select
= { 0 };
625 char **argv
= *argv_p
;
626 __u32 msec_since
= 0;
628 char k
[FILTER_NAMESZ
];
634 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
635 .t
.tca_family
= AF_UNSPEC
,
638 tail
= addattr_nest(&req
.n
, MAX_MSG
, TCA_ACT_TAB
);
639 tail2
= NLMSG_TAIL(&req
.n
);
641 strncpy(k
, *argv
, sizeof(k
) - 1);
644 get_action_kind("gact");
647 a
= get_action_kind(k
);
649 fprintf(stderr
, "bad action %s\n", k
);
652 if (strcmp(a
->id
, k
) != 0) {
653 fprintf(stderr
, "bad action %s\n", k
);
656 strncpy(k
, *argv
, sizeof(k
) - 1);
661 if (argc
&& (strcmp(*argv
, "since") == 0)) {
663 if (get_u32(&msec_since
, *argv
, 0))
664 invarg("dump time \"since\" is invalid", *argv
);
667 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
668 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
669 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
670 addattr_nest_end(&req
.n
, tail
);
672 tail3
= NLMSG_TAIL(&req
.n
);
673 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
674 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
675 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
676 sizeof(struct nla_bitfield32
));
677 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
679 tail4
= NLMSG_TAIL(&req
.n
);
680 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
681 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
683 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
)
684 - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
686 if (event
== RTM_GETACTION
) {
687 if (rtnl_dump_request(&rth
, event
,
688 (void *)&req
.t
, msg_size
) < 0) {
689 perror("Cannot send dump request");
693 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
697 if (event
== RTM_DELACTION
) {
698 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
699 req
.n
.nlmsg_type
= RTM_DELACTION
;
700 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
701 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
702 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
703 fprintf(stderr
, "We have an error flushing\n");
716 int do_action(int argc
, char **argv
, void *buf
, size_t buflen
)
723 if (matches(*argv
, "add") == 0) {
724 ret
= tc_action_modify(RTM_NEWACTION
,
725 NLM_F_EXCL
| NLM_F_CREATE
,
726 &argc
, &argv
, buf
, buflen
);
727 } else if (matches(*argv
, "change") == 0 ||
728 matches(*argv
, "replace") == 0) {
729 ret
= tc_action_modify(RTM_NEWACTION
,
730 NLM_F_CREATE
| NLM_F_REPLACE
,
731 &argc
, &argv
, buf
, buflen
);
732 } else if (matches(*argv
, "delete") == 0) {
735 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
736 } else if (matches(*argv
, "get") == 0) {
739 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
740 } else if (matches(*argv
, "list") == 0 ||
741 matches(*argv
, "show") == 0 ||
742 matches(*argv
, "lst") == 0) {
750 return tc_act_list_or_flush(&argc
, &argv
,
752 } else if (matches(*argv
, "flush") == 0) {
760 return tc_act_list_or_flush(&argc
, &argv
,
762 } else if (matches(*argv
, "help") == 0) {
767 "Command \"%s\" is unknown, try \"tc actions help\".\n",