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 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");
186 } else if (strcmp(*argv
, "flowid") == 0) {
188 } else if (strcmp(*argv
, "classid") == 0) {
190 } else if (strcmp(*argv
, "help") == 0) {
192 } else if (new_cmd(argv
)) {
195 struct action_util
*a
= NULL
;
197 if (!action_a2n(*argv
, NULL
, false))
198 strncpy(k
, "gact", sizeof(k
) - 1);
200 strncpy(k
, *argv
, sizeof(k
) - 1);
203 a
= get_action_kind(k
);
216 tail
= NLMSG_TAIL(n
);
217 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
218 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
220 ret
= a
->parse_aopt(a
, &argc
, &argv
, TCA_ACT_OPTIONS
,
224 fprintf(stderr
, "bad action parsing\n");
228 if (*argv
&& strcmp(*argv
, "cookie") == 0) {
232 slen
= strlen(*argv
);
233 if (slen
> TC_COOKIE_MAX_SIZE
* 2) {
234 char cookie_err_m
[128];
236 snprintf(cookie_err_m
, 128,
237 "%zd Max allowed size %d",
238 slen
, TC_COOKIE_MAX_SIZE
*2);
239 invarg(cookie_err_m
, *argv
);
242 if (hex2mem(*argv
, act_ck
, slen
/ 2) < 0)
243 invarg("cookie must be a hex string\n",
246 act_ck_len
= slen
/ 2;
252 addattr_l(n
, MAX_MSG
, TCA_ACT_COOKIE
,
253 &act_ck
, act_ck_len
);
255 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
261 fprintf(stderr
, "bad action empty %d\n", eap
);
265 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
272 /* no need to undo things, returning from here should
275 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n", argc
, *argv
);
279 static int tc_print_one_action(FILE *f
, struct rtattr
*arg
)
282 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
284 struct action_util
*a
= NULL
;
289 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
291 if (tb
[TCA_ACT_KIND
] == NULL
) {
292 fprintf(stderr
, "NULL Action!\n");
297 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
301 err
= a
->print_aopt(a
, f
, tb
[TCA_ACT_OPTIONS
]);
306 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
307 print_string(PRINT_FP
, NULL
, "\tAction statistics:\n", NULL
);
308 open_json_object("stats");
309 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
311 print_string(PRINT_FP
, NULL
, "\n", NULL
);
313 if (tb
[TCA_ACT_COOKIE
]) {
314 int strsz
= RTA_PAYLOAD(tb
[TCA_ACT_COOKIE
]);
315 char b1
[strsz
* 2 + 1];
317 print_string(PRINT_ANY
, "cookie", "\tcookie %s\n",
318 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
319 strsz
, b1
, sizeof(b1
)));
326 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
329 struct rtattr
*tb
[TCA_MAX
+ 1];
331 struct action_util
*a
= NULL
;
332 __u32
*delete_count
= 0;
334 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
336 if (tb
[TCA_KIND
] == NULL
) {
337 fprintf(stderr
, "NULL Action!\n");
341 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
345 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
346 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
352 tc_print_action(FILE *f
, const struct rtattr
*arg
, unsigned short tot_acts
)
361 tot_acts
= TCA_ACT_MAX_PRIO
;
363 struct rtattr
*tb
[tot_acts
+ 1];
365 parse_rtattr_nested(tb
, tot_acts
, arg
);
367 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1])
368 return tc_print_action_flush(f
, tb
[0]);
370 open_json_array(PRINT_JSON
, "actions");
371 for (i
= 0; i
< tot_acts
; i
++) {
373 open_json_object(NULL
);
374 print_uint(PRINT_ANY
, "order",
375 "\n\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 fprintf(fp
, "total acts %d\n", tot_acts
? *tot_acts
:0);
412 if (tb
[TCA_ACT_TAB
] == NULL
) {
413 if (n
->nlmsg_type
!= RTM_GETACTION
)
414 fprintf(stderr
, "print_action: NULL kind\n");
418 if (n
->nlmsg_type
== RTM_DELACTION
) {
419 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
420 fprintf(fp
, "Flushed table ");
423 fprintf(fp
, "Deleted action ");
427 if (n
->nlmsg_type
== RTM_NEWACTION
) {
428 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
429 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
430 fprintf(fp
, "Added action ");
431 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
432 fprintf(fp
, "Replaced action ");
437 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
442 static int tc_action_gd(int cmd
, unsigned int flags
,
443 int *argc_p
, char ***argv_p
)
445 char k
[FILTER_NAMESZ
];
446 struct action_util
*a
= NULL
;
448 char **argv
= *argv_p
;
453 struct rtattr
*tail2
;
454 struct nlmsghdr
*ans
= NULL
;
461 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
462 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
464 .t
.tca_family
= AF_UNSPEC
,
471 tail
= NLMSG_TAIL(&req
.n
);
472 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
475 if (strcmp(*argv
, "action") == 0) {
479 } else if (strcmp(*argv
, "help") == 0) {
483 strncpy(k
, *argv
, sizeof(k
) - 1);
484 a
= get_action_kind(k
);
486 fprintf(stderr
, "Error: non existent action: %s\n", k
);
490 if (strcmp(a
->id
, k
) != 0) {
491 fprintf(stderr
, "Error: non existent action: %s\n", k
);
500 "Error: no index specified action: %s\n", k
);
505 if (matches(*argv
, "index") == 0) {
507 if (get_u32(&i
, *argv
, 10)) {
508 fprintf(stderr
, "Illegal \"index\"\n");
516 "Error: no index specified action: %s\n", k
);
521 tail2
= NLMSG_TAIL(&req
.n
);
522 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
523 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
525 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
526 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
530 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
532 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
534 if (rtnl_talk(&rth
, &req
.n
, cmd
== RTM_DELACTION
? NULL
: &ans
) < 0) {
535 fprintf(stderr
, "We have an error talking to the kernel\n");
539 if (cmd
== RTM_GETACTION
&& print_action(NULL
, ans
, stdout
) < 0) {
540 fprintf(stderr
, "Dump terminated\n");
552 struct tc_action_req
{
558 static int tc_action_modify(int cmd
, unsigned int flags
,
559 int *argc_p
, char ***argv_p
,
560 void *buf
, size_t buflen
)
562 struct tc_action_req
*req
, action_req
;
563 char **argv
= *argv_p
;
571 if (buflen
< sizeof (struct tc_action_req
)) {
572 fprintf(stderr
, "buffer is too small: %zu\n", buflen
);
576 memset(&action_req
, 0, sizeof (struct tc_action_req
));
580 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
581 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
582 req
->n
.nlmsg_type
= cmd
;
583 req
->t
.tca_family
= AF_UNSPEC
;
584 tail
= NLMSG_TAIL(&req
->n
);
588 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
->n
)) {
589 fprintf(stderr
, "Illegal \"action\"\n");
592 tail
->rta_len
= (void *) NLMSG_TAIL(&req
->n
) - (void *) tail
;
600 iov
.iov_base
= &req
->n
;
601 iov
.iov_len
= req
->n
.nlmsg_len
;
602 if (rtnl_talk_iov(&rth
, &iov
, 1, NULL
) < 0) {
603 fprintf(stderr
, "We have an error talking to the kernel\n");
610 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
612 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
613 int ret
= 0, prio
= 0, msg_size
= 0;
614 struct action_util
*a
= NULL
;
615 struct nla_bitfield32 flag_select
= { 0 };
616 char **argv
= *argv_p
;
617 __u32 msec_since
= 0;
619 char k
[FILTER_NAMESZ
];
625 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
626 .t
.tca_family
= AF_UNSPEC
,
629 tail
= NLMSG_TAIL(&req
.n
);
630 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
631 tail2
= NLMSG_TAIL(&req
.n
);
633 strncpy(k
, *argv
, sizeof(k
) - 1);
636 get_action_kind("gact");
639 a
= get_action_kind(k
);
641 fprintf(stderr
, "bad action %s\n", k
);
644 if (strcmp(a
->id
, k
) != 0) {
645 fprintf(stderr
, "bad action %s\n", k
);
648 strncpy(k
, *argv
, sizeof(k
) - 1);
653 if (argc
&& (strcmp(*argv
, "since") == 0)) {
655 if (get_u32(&msec_since
, *argv
, 0))
656 invarg("dump time \"since\" is invalid", *argv
);
659 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
660 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
661 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
662 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
664 tail3
= NLMSG_TAIL(&req
.n
);
665 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
666 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
667 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
668 sizeof(struct nla_bitfield32
));
669 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
671 tail4
= NLMSG_TAIL(&req
.n
);
672 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
673 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
675 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
)
676 - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
678 if (event
== RTM_GETACTION
) {
679 if (rtnl_dump_request(&rth
, event
,
680 (void *)&req
.t
, msg_size
) < 0) {
681 perror("Cannot send dump request");
684 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
687 if (event
== RTM_DELACTION
) {
688 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
689 req
.n
.nlmsg_type
= RTM_DELACTION
;
690 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
691 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
692 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
693 fprintf(stderr
, "We have an error flushing\n");
706 int do_action(int argc
, char **argv
, void *buf
, size_t buflen
)
713 if (matches(*argv
, "add") == 0) {
714 ret
= tc_action_modify(RTM_NEWACTION
,
715 NLM_F_EXCL
| NLM_F_CREATE
,
716 &argc
, &argv
, buf
, buflen
);
717 } else if (matches(*argv
, "change") == 0 ||
718 matches(*argv
, "replace") == 0) {
719 ret
= tc_action_modify(RTM_NEWACTION
,
720 NLM_F_CREATE
| NLM_F_REPLACE
,
721 &argc
, &argv
, buf
, buflen
);
722 } else if (matches(*argv
, "delete") == 0) {
725 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
726 } else if (matches(*argv
, "get") == 0) {
729 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
730 } else if (matches(*argv
, "list") == 0 ||
731 matches(*argv
, "show") == 0 ||
732 matches(*argv
, "lst") == 0) {
740 return tc_act_list_or_flush(&argc
, &argv
,
742 } else if (matches(*argv
, "flush") == 0) {
750 return tc_act_list_or_flush(&argc
, &argv
,
752 } else if (matches(*argv
, "help") == 0) {
757 "Command \"%s\" is unknown, try \"tc actions help\".\n",