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 fprintf(f
, "\tAction statistics:\n");
305 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
, "\tcookie %s\n",
313 hexstring_n2a(RTA_DATA(tb
[TCA_ACT_COOKIE
]),
314 strsz
, b1
, sizeof(b1
)));
321 tc_print_action_flush(FILE *f
, const struct rtattr
*arg
)
324 struct rtattr
*tb
[TCA_MAX
+ 1];
326 struct action_util
*a
= NULL
;
327 __u32
*delete_count
= 0;
329 parse_rtattr_nested(tb
, TCA_MAX
, arg
);
331 if (tb
[TCA_KIND
] == NULL
) {
332 fprintf(stderr
, "NULL Action!\n");
336 a
= get_action_kind(RTA_DATA(tb
[TCA_KIND
]));
340 delete_count
= RTA_DATA(tb
[TCA_FCNT
]);
341 fprintf(f
, " %s (%d entries)\n", a
->id
, *delete_count
);
347 tc_print_action(FILE *f
, const struct rtattr
*arg
, unsigned short tot_acts
)
356 tot_acts
= TCA_ACT_MAX_PRIO
;
358 struct rtattr
*tb
[tot_acts
+ 1];
360 parse_rtattr_nested(tb
, tot_acts
, arg
);
362 if (tab_flush
&& NULL
!= tb
[0] && NULL
== tb
[1])
363 return tc_print_action_flush(f
, tb
[0]);
365 for (i
= 0; i
< tot_acts
; i
++) {
367 fprintf(f
, "\n\taction order %d: ", i
);
368 if (tc_print_one_action(f
, tb
[i
]) < 0) {
369 fprintf(f
, "Error printing action\n");
378 int print_action(const struct sockaddr_nl
*who
,
382 FILE *fp
= (FILE *)arg
;
383 struct tcamsg
*t
= NLMSG_DATA(n
);
384 int len
= n
->nlmsg_len
;
385 __u32
*tot_acts
= NULL
;
386 struct rtattr
*tb
[TCA_ROOT_MAX
+1];
388 len
-= NLMSG_LENGTH(sizeof(*t
));
391 fprintf(stderr
, "Wrong len %d\n", len
);
395 parse_rtattr(tb
, TCA_ROOT_MAX
, TA_RTA(t
), len
);
397 if (tb
[TCA_ROOT_COUNT
])
398 tot_acts
= RTA_DATA(tb
[TCA_ROOT_COUNT
]);
400 fprintf(fp
, "total acts %d\n", tot_acts
? *tot_acts
:0);
401 if (tb
[TCA_ACT_TAB
] == NULL
) {
402 if (n
->nlmsg_type
!= RTM_GETACTION
)
403 fprintf(stderr
, "print_action: NULL kind\n");
407 if (n
->nlmsg_type
== RTM_DELACTION
) {
408 if (n
->nlmsg_flags
& NLM_F_ROOT
) {
409 fprintf(fp
, "Flushed table ");
412 fprintf(fp
, "Deleted action ");
416 if (n
->nlmsg_type
== RTM_NEWACTION
) {
417 if ((n
->nlmsg_flags
& NLM_F_CREATE
) &&
418 !(n
->nlmsg_flags
& NLM_F_REPLACE
)) {
419 fprintf(fp
, "Added action ");
420 } else if (n
->nlmsg_flags
& NLM_F_REPLACE
) {
421 fprintf(fp
, "Replaced action ");
426 tc_print_action(fp
, tb
[TCA_ACT_TAB
], tot_acts
? *tot_acts
:0);
431 static int tc_action_gd(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
433 char k
[FILTER_NAMESZ
];
434 struct action_util
*a
= NULL
;
436 char **argv
= *argv_p
;
441 struct rtattr
*tail2
;
442 struct nlmsghdr
*ans
= NULL
;
449 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
450 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
452 .t
.tca_family
= AF_UNSPEC
,
459 tail
= NLMSG_TAIL(&req
.n
);
460 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
463 if (strcmp(*argv
, "action") == 0) {
467 } else if (strcmp(*argv
, "help") == 0) {
471 strncpy(k
, *argv
, sizeof(k
) - 1);
472 a
= get_action_kind(k
);
474 fprintf(stderr
, "Error: non existent action: %s\n", k
);
478 if (strcmp(a
->id
, k
) != 0) {
479 fprintf(stderr
, "Error: non existent action: %s\n", k
);
487 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
492 if (matches(*argv
, "index") == 0) {
494 if (get_u32(&i
, *argv
, 10)) {
495 fprintf(stderr
, "Illegal \"index\"\n");
502 fprintf(stderr
, "Error: no index specified action: %s\n", k
);
507 tail2
= NLMSG_TAIL(&req
.n
);
508 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
509 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
511 addattr32(&req
.n
, MAX_MSG
, TCA_ACT_INDEX
, i
);
512 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
516 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
518 req
.n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
;
520 if (rtnl_talk(&rth
, &req
.n
, &ans
) < 0) {
521 fprintf(stderr
, "We have an error talking to the kernel\n");
525 if (cmd
== RTM_GETACTION
&& print_action(NULL
, ans
, stdout
) < 0) {
526 fprintf(stderr
, "Dump terminated\n");
538 static int tc_action_modify(int cmd
, unsigned int flags
, int *argc_p
, char ***argv_p
)
541 char **argv
= *argv_p
;
548 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
549 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
551 .t
.tca_family
= AF_UNSPEC
,
553 struct rtattr
*tail
= NLMSG_TAIL(&req
.n
);
557 if (parse_action(&argc
, &argv
, TCA_ACT_TAB
, &req
.n
)) {
558 fprintf(stderr
, "Illegal \"action\"\n");
561 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
563 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
564 fprintf(stderr
, "We have an error talking to the kernel\n");
574 static int tc_act_list_or_flush(int *argc_p
, char ***argv_p
, int event
)
576 struct rtattr
*tail
, *tail2
, *tail3
, *tail4
;
577 int ret
= 0, prio
= 0, msg_size
= 0;
578 struct action_util
*a
= NULL
;
579 struct nla_bitfield32 flag_select
= { 0 };
580 char **argv
= *argv_p
;
581 __u32 msec_since
= 0;
583 char k
[FILTER_NAMESZ
];
589 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcamsg
)),
590 .t
.tca_family
= AF_UNSPEC
,
593 tail
= NLMSG_TAIL(&req
.n
);
594 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_TAB
, NULL
, 0);
595 tail2
= NLMSG_TAIL(&req
.n
);
597 strncpy(k
, *argv
, sizeof(k
) - 1);
600 get_action_kind("gact");
603 a
= get_action_kind(k
);
605 fprintf(stderr
, "bad action %s\n", k
);
608 if (strcmp(a
->id
, k
) != 0) {
609 fprintf(stderr
, "bad action %s\n", k
);
612 strncpy(k
, *argv
, sizeof(k
) - 1);
617 if (argc
&& (strcmp(*argv
, "since") == 0)) {
619 if (get_u32(&msec_since
, *argv
, 0))
620 invarg("dump time \"since\" is invalid", *argv
);
623 addattr_l(&req
.n
, MAX_MSG
, ++prio
, NULL
, 0);
624 addattr_l(&req
.n
, MAX_MSG
, TCA_ACT_KIND
, k
, strlen(k
) + 1);
625 tail2
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail2
;
626 tail
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail
;
628 tail3
= NLMSG_TAIL(&req
.n
);
629 flag_select
.value
|= TCA_FLAG_LARGE_DUMP_ON
;
630 flag_select
.selector
|= TCA_FLAG_LARGE_DUMP_ON
;
631 addattr_l(&req
.n
, MAX_MSG
, TCA_ROOT_FLAGS
, &flag_select
,
632 sizeof(struct nla_bitfield32
));
633 tail3
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail3
;
635 tail4
= NLMSG_TAIL(&req
.n
);
636 addattr32(&req
.n
, MAX_MSG
, TCA_ROOT_TIME_DELTA
, msec_since
);
637 tail4
->rta_len
= (void *) NLMSG_TAIL(&req
.n
) - (void *) tail4
;
639 msg_size
= NLMSG_ALIGN(req
.n
.nlmsg_len
) - NLMSG_ALIGN(sizeof(struct nlmsghdr
));
641 if (event
== RTM_GETACTION
) {
642 if (rtnl_dump_request(&rth
, event
, (void *)&req
.t
, msg_size
) < 0) {
643 perror("Cannot send dump request");
646 ret
= rtnl_dump_filter(&rth
, print_action
, stdout
);
649 if (event
== RTM_DELACTION
) {
650 req
.n
.nlmsg_len
= NLMSG_ALIGN(req
.n
.nlmsg_len
);
651 req
.n
.nlmsg_type
= RTM_DELACTION
;
652 req
.n
.nlmsg_flags
|= NLM_F_ROOT
;
653 req
.n
.nlmsg_flags
|= NLM_F_REQUEST
;
654 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0) {
655 fprintf(stderr
, "We have an error flushing\n");
668 int do_action(int argc
, char **argv
)
675 if (matches(*argv
, "add") == 0) {
676 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_EXCL
|NLM_F_CREATE
, &argc
, &argv
);
677 } else if (matches(*argv
, "change") == 0 ||
678 matches(*argv
, "replace") == 0) {
679 ret
= tc_action_modify(RTM_NEWACTION
, NLM_F_CREATE
|NLM_F_REPLACE
, &argc
, &argv
);
680 } else if (matches(*argv
, "delete") == 0) {
683 ret
= tc_action_gd(RTM_DELACTION
, 0, &argc
, &argv
);
684 } else if (matches(*argv
, "get") == 0) {
687 ret
= tc_action_gd(RTM_GETACTION
, 0, &argc
, &argv
);
688 } else if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
689 || matches(*argv
, "lst") == 0) {
697 return tc_act_list_or_flush(&argc
, &argv
,
699 } else if (matches(*argv
, "flush") == 0) {
707 return tc_act_list_or_flush(&argc
, &argv
,
709 } else if (matches(*argv
, "help") == 0) {
713 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv
);