2 * m_egress.c ingress/egress packet mirror/redir actions module
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)
11 * TODO: Add Ingress support
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
25 #include "tc_common.h"
26 #include <linux/tc_act/tc_mirred.h>
32 "Usage: mirred <DIRECTION> <ACTION> [index INDEX] <dev DEVICENAME>\n"
34 "\tDIRECTION := <ingress | egress>\n"
35 "\tACTION := <mirror | redirect>\n"
36 "\tINDEX is the specific policy instance id\n"
37 "\tDEVICENAME is the devicename\n");
47 static const char *mirred_n2a(int action
)
50 case TCA_EGRESS_REDIR
:
51 return "Egress Redirect";
52 case TCA_INGRESS_REDIR
:
53 return "Ingress Redirect";
54 case TCA_EGRESS_MIRROR
:
55 return "Egress Mirror";
56 case TCA_INGRESS_MIRROR
:
57 return "Ingress Mirror";
63 static const char *mirred_direction(int action
)
66 case TCA_EGRESS_REDIR
:
67 case TCA_EGRESS_MIRROR
:
69 case TCA_INGRESS_REDIR
:
70 case TCA_INGRESS_MIRROR
:
77 static const char *mirred_action(int action
)
80 case TCA_EGRESS_REDIR
:
81 case TCA_INGRESS_REDIR
:
83 case TCA_EGRESS_MIRROR
:
84 case TCA_INGRESS_MIRROR
:
92 parse_direction(struct action_util
*a
, int *argc_p
, char ***argv_p
,
93 int tca_id
, struct nlmsghdr
*n
)
97 char **argv
= *argv_p
;
98 int ok
= 0, iok
= 0, mirror
= 0, redir
= 0, ingress
= 0, egress
= 0;
99 struct tc_mirred p
= {};
101 char d
[IFNAMSIZ
] = {};
105 if (matches(*argv
, "action") == 0) {
107 } else if (!egress
&& matches(*argv
, "egress") == 0) {
111 "Can't have both egress and ingress\n");
117 } else if (!ingress
&& matches(*argv
, "ingress") == 0) {
121 "Can't have both ingress and egress\n");
129 if (matches(*argv
, "index") == 0) {
131 if (get_u32(&p
.index
, *argv
, 10)) {
132 fprintf(stderr
, "Illegal \"index\"\n");
143 "was expecting egress or ingress (%s)\n",
147 } else if (!mirror
&& matches(*argv
, "mirror") == 0) {
151 "Can't have both mirror and redir\n");
154 p
.eaction
= egress
? TCA_EGRESS_MIRROR
:
156 p
.action
= TC_ACT_PIPE
;
158 } else if (!redir
&& matches(*argv
, "redirect") == 0) {
162 "Can't have both mirror and redir\n");
165 p
.eaction
= egress
? TCA_EGRESS_REDIR
:
167 p
.action
= TC_ACT_STOLEN
;
169 } else if ((redir
|| mirror
) &&
170 matches(*argv
, "dev") == 0) {
173 duparg("dev", *argv
);
175 strncpy(d
, *argv
, sizeof(d
)-1);
195 idx
= ll_name_to_index(d
);
197 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
205 if (p
.eaction
== TCA_EGRESS_MIRROR
|| p
.eaction
== TCA_INGRESS_MIRROR
) {
206 parse_action_control(&argc
, &argv
, &p
.action
, false);
211 if (iok
&& matches(*argv
, "index") == 0) {
212 fprintf(stderr
, "mirred: Illegal double index\n");
216 if (matches(*argv
, "index") == 0) {
218 if (get_u32(&p
.index
, *argv
, 10)) {
220 "mirred: Illegal \"index\"\n");
228 tail
= NLMSG_TAIL(n
);
229 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
230 addattr_l(n
, MAX_MSG
, TCA_MIRRED_PARMS
, &p
, sizeof(p
));
231 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
240 parse_mirred(struct action_util
*a
, int *argc_p
, char ***argv_p
,
241 int tca_id
, struct nlmsghdr
*n
)
245 char **argv
= *argv_p
;
248 fprintf(stderr
, "mirred bad argument count %d\n", argc
);
252 if (matches(*argv
, "mirred") == 0) {
255 fprintf(stderr
, "mirred bad argument %s\n", *argv
);
260 if (matches(*argv
, "egress") == 0 || matches(*argv
, "ingress") == 0 ||
261 matches(*argv
, "index") == 0) {
262 int ret
= parse_direction(a
, &argc
, &argv
, tca_id
, n
);
270 } else if (matches(*argv
, "help") == 0) {
273 fprintf(stderr
, "mirred option not supported %s\n", *argv
);
281 print_mirred(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
284 struct rtattr
*tb
[TCA_MIRRED_MAX
+ 1];
290 parse_rtattr_nested(tb
, TCA_MIRRED_MAX
, arg
);
292 if (tb
[TCA_MIRRED_PARMS
] == NULL
) {
293 print_string(PRINT_FP
, NULL
, "%s", "[NULL mirred parameters]");
296 p
= RTA_DATA(tb
[TCA_MIRRED_PARMS
]);
298 dev
= ll_index_to_name(p
->ifindex
);
300 fprintf(stderr
, "Cannot find device %d\n", p
->ifindex
);
304 print_string(PRINT_ANY
, "kind", "%s ", "mirred");
305 print_string(PRINT_FP
, NULL
, "(%s", mirred_n2a(p
->eaction
));
306 print_string(PRINT_JSON
, "mirred_action", NULL
,
307 mirred_action(p
->eaction
));
308 print_string(PRINT_JSON
, "direction", NULL
,
309 mirred_direction(p
->eaction
));
310 print_string(PRINT_ANY
, "to_dev", " to device %s)", dev
);
311 print_action_control(f
, " ", p
->action
, "");
313 print_uint(PRINT_ANY
, "index", "\n \tindex %u", p
->index
);
314 print_int(PRINT_ANY
, "ref", " ref %d", p
->refcnt
);
315 print_int(PRINT_ANY
, "bind", " bind %d", p
->bindcnt
);
318 if (tb
[TCA_MIRRED_TM
]) {
319 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_MIRRED_TM
]);
324 print_string(PRINT_FP
, NULL
, "%s", "\n ");
328 struct action_util mirred_action_util
= {
330 .parse_aopt
= parse_mirred
,
331 .print_aopt
= print_mirred
,