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
, char ***argv_p
, int code
, struct nlmsghdr
*n
)
75 char **argv
= *argv_p
;
78 fprintf(stderr
, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au
->id
, *argv
);
80 fprintf(stderr
, "Unknown action \"%s\"\n", au
->id
);
85 static struct action_util
*get_action_kind(char *str
)
90 struct action_util
*a
;
95 for (a
= action_list
; a
; a
= a
->next
) {
96 if (strcmp(a
->id
, str
) == 0)
100 snprintf(buf
, sizeof(buf
), "%s/m_%s.so", get_tc_lib(), str
);
101 dlh
= dlopen(buf
, RTLD_LAZY
| RTLD_GLOBAL
);
105 dlh
= aBODY
= dlopen(NULL
, RTLD_LAZY
);
111 snprintf(buf
, sizeof(buf
), "%s_action_util", str
);
117 a
->next
= action_list
;
129 a
= calloc(1, sizeof(*a
));
131 strncpy(a
->id
, "noact", 15);
132 a
->parse_aopt
= parse_noaopt
;
133 a
->print_aopt
= print_noaopt
;
142 if ((matches(*argv
, "change") == 0) ||
143 (matches(*argv
, "replace") == 0) ||
144 (matches(*argv
, "delete") == 0) ||
145 (matches(*argv
, "get") == 0) ||
146 (matches(*argv
, "add") == 0))
153 int parse_action(int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
156 char **argv
= *argv_p
;
157 struct rtattr
*tail
, *tail2
;
161 int eap
= 0; /* expect action parameters */
165 unsigned char act_ck
[TC_COOKIE_MAX_SIZE
];
170 tail
= tail2
= NLMSG_TAIL(n
);
172 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
176 memset(k
, 0, sizeof(k
));
178 if (strcmp(*argv
, "action") == 0) {
184 get_action_kind("gact");
188 } else if (strcmp(*argv
, "flowid") == 0) {
190 } else if (strcmp(*argv
, "classid") == 0) {
192 } else if (strcmp(*argv
, "help") == 0) {
194 } else if (new_cmd(argv
)) {
197 struct action_util
*a
= NULL
;
199 strncpy(k
, *argv
, sizeof(k
) - 1);
202 a
= get_action_kind(k
);
215 tail
= NLMSG_TAIL(n
);
216 addattr_l(n
, MAX_MSG
, ++prio
, NULL
, 0);
217 addattr_l(n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
219 ret
= a
->parse_aopt(a
, &argc
, &argv
, TCA_ACT_OPTIONS
,
223 fprintf(stderr
, "bad action parsing\n");
227 if (*argv
&& strcmp(*argv
, "cookie") == 0) {
231 slen
= strlen(*argv
);
232 if (slen
> TC_COOKIE_MAX_SIZE
* 2) {
233 char cookie_err_m
[128];
235 snprintf(cookie_err_m
, 128,
236 "%zd Max allowed size %d",
237 slen
, TC_COOKIE_MAX_SIZE
*2);
238 invarg(cookie_err_m
, *argv
);
241 if (hex2mem(*argv
, act_ck
, slen
/ 2) < 0)
242 invarg("cookie must be a hex string\n",
245 act_ck_len
= slen
/ 2;
251 addattr_l(n
, MAX_MSG
, TCA_ACT_COOKIE
,
252 &act_ck
, act_ck_len
);
254 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
260 fprintf(stderr
, "bad action empty %d\n", eap
);
264 tail2
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail2
;
271 /* no need to undo things, returning from here should
272 * cause enough pain */
273 fprintf(stderr
, "parse_action: bad value (%d:%s)!\n", argc
, *argv
);
277 static int tc_print_one_action(FILE *f
, struct rtattr
*arg
)
280 struct rtattr
*tb
[TCA_ACT_MAX
+ 1];
282 struct action_util
*a
= NULL
;
287 parse_rtattr_nested(tb
, TCA_ACT_MAX
, arg
);
289 if (tb
[TCA_ACT_KIND
] == NULL
) {
290 fprintf(stderr
, "NULL Action!\n");
295 a
= get_action_kind(RTA_DATA(tb
[TCA_ACT_KIND
]));
299 err
= a
->print_aopt(a
, f
, tb
[TCA_ACT_OPTIONS
]);
304 if (show_stats
&& tb
[TCA_ACT_STATS
]) {
306 fprintf(f
, "\tAction statistics:\n");
307 print_tcstats2_attr(f
, tb
[TCA_ACT_STATS
], "\t", NULL
);
308 if (tb
[TCA_ACT_COOKIE
]) {
309 int strsz
= RTA_PAYLOAD(tb
[TCA_ACT_COOKIE
]);
310 char b1
[strsz
* 2 + 1];
312 fprintf(f
, "\n\tcookie len %d %s ", strsz
,
313 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
314 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 for (i
= 0; i
< tot_acts
; i
++) {
369 fprintf(f
, "\n\taction order %d: ", i
);
370 if (tc_print_one_action(f
, tb
[i
]) < 0) {
371 fprintf(f
, "Error printing action\n");
380 int print_action(const struct sockaddr_nl
*who
,
384 FILE *fp
= (FILE *)arg
;
385 struct tcamsg
*t
= NLMSG_DATA(n
);
386 int len
= n
->nlmsg_len
;
387 __u32
*tot_acts
= NULL
;
388 struct rtattr
*tb
[TCA_ROOT_MAX
+1];
390 len
-= NLMSG_LENGTH(sizeof(*t
));
393 fprintf(stderr
, "Wrong len %d\n", len
);
397 parse_rtattr(tb
, TCA_ROOT_MAX
, TA_RTA(t
), len
);
399 if (tb
[TCA_ROOT_COUNT
])
400 tot_acts
= RTA_DATA(tb
[TCA_ROOT_COUNT
]);
402 fprintf(fp
, "total acts %d\n", tot_acts
? *tot_acts
:0);
403 if (tb
[TCA_ACT_TAB
] == NULL
) {
404 if (n
->nlmsg_type
!= RTM_GETACTION
)
405 fprintf(stderr
, "print_action: NULL kind\n");
409 if (n
->nlmsg_type
== RTM_DELACTION
) {
410 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
411 fprintf(fp
, "Flushed table ");
414 fprintf(fp
, "Deleted action ");
418 if (n
->nlmsg_type
== RTM_NEWACTION
) {
419 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
420 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
421 fprintf(fp
, "Added action ");
422 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
423 fprintf(fp
, "Replaced action ");
428 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
433 static int tc_action_gd(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
436 struct action_util
*a
= NULL
;
438 char **argv
= *argv_p
;
443 struct rtattr
*tail2
;
444 struct nlmsghdr
*ans
= NULL
;
451 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
452 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
454 .t
.tca_family
= AF_UNSPEC
,
461 tail
= NLMSG_TAIL(&req
.n
);
462 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
465 if (strcmp(*argv
, "action") == 0) {
469 } else if (strcmp(*argv
, "help") == 0) {
473 strncpy(k
, *argv
, sizeof(k
) - 1);
474 a
= get_action_kind(k
);
476 fprintf(stderr
, "Error: non existent action: %s\n", k
);
480 if (strcmp(a
->id
, k
) != 0) {
481 fprintf(stderr
, "Error: non existent action: %s\n", k
);
489 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
494 if (matches(*argv
, "index") == 0) {
496 if (get_u32(&i
, *argv
, 10)) {
497 fprintf(stderr
, "Illegal \"index\"\n");
504 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
509 tail2
= NLMSG_TAIL(&req
.n
);
510 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
511 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
513 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
514 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
518 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
520 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
522 if (rtnl_talk(&rth
, &req
.n
, &ans
) < 0) {
523 fprintf(stderr
, "We have an error talking to the kernel\n");
527 if (cmd
== RTM_GETACTION
&& print_action(NULL
, ans
, stdout
) < 0) {
528 fprintf(stderr
, "Dump terminated\n");
540 static int tc_action_modify(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
543 char **argv
= *argv_p
;
550 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
551 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
553 .t
.tca_family
= AF_UNSPEC
,
555 struct rtattr
*tail
= NLMSG_TAIL(&req
.n
);
559 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
560 fprintf(stderr
, "Illegal \"action\"\n");
563 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
565 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
566 fprintf(stderr
, "We have an error talking to the kernel\n");
576 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
578 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
579 int ret
= 0, prio
= 0, msg_size
= 0;
580 struct action_util
*a
= NULL
;
581 struct nla_bitfield32 flag_select
= { 0 };
582 char **argv
= *argv_p
;
583 __u32 msec_since
= 0;
591 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
592 .t
.tca_family
= AF_UNSPEC
,
595 tail
= NLMSG_TAIL(&req
.n
);
596 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
597 tail2
= NLMSG_TAIL(&req
.n
);
599 strncpy(k
, *argv
, sizeof(k
) - 1);
602 get_action_kind("gact");
605 a
= get_action_kind(k
);
607 fprintf(stderr
, "bad action %s\n", k
);
610 if (strcmp(a
->id
, k
) != 0) {
611 fprintf(stderr
, "bad action %s\n", k
);
614 strncpy(k
, *argv
, sizeof(k
) - 1);
619 if (argc
&& (strcmp(*argv
, "since") == 0)) {
621 if (get_u32(&msec_since
, *argv
, 0))
622 invarg("dump time \"since\" is invalid", *argv
);
625 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
626 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
627 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
628 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
630 tail3
= NLMSG_TAIL(&req
.n
);
631 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
632 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
633 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
634 sizeof(struct nla_bitfield32
));
635 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
637 tail4
= NLMSG_TAIL(&req
.n
);
638 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
639 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
641 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
643 if (event
== RTM_GETACTION
) {
644 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
645 perror("Cannot send dump request");
648 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
651 if (event
== RTM_DELACTION
) {
652 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
653 req
.n
.nlmsg_type
= RTM_DELACTION
;
654 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
655 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
656 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
657 fprintf(stderr
, "We have an error flushing\n");
670 int do_action(int argc
, char **argv
)
677 if (matches(*argv
, "add") == 0) {
678 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
679 } else if (matches(*argv
, "change") == 0 ||
680 matches(*argv
, "replace") == 0) {
681 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
682 } else if (matches(*argv
, "delete") == 0) {
685 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
686 } else if (matches(*argv
, "get") == 0) {
689 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
690 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
691 || matches(*argv
, "lst") == 0) {
699 return tc_act_list_or_flush(&argc
, &argv
,
701 } else if (matches(*argv
, "flush") == 0) {
709 return tc_act_list_or_flush(&argc
, &argv
,
711 } else if (matches(*argv
, "help") == 0) {
715 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);