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
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
26 #include "tc_common.h"
27 #include <linux/tc_act/tc_mirred.h>
32 fprintf(stderr
, "Usage: mirred <DIRECTION> <ACTION> [index INDEX] <dev DEVICENAME>\n");
33 fprintf(stderr
, "where:\n");
34 fprintf(stderr
, "\tDIRECTION := <ingress | egress>\n");
35 fprintf(stderr
, "\tACTION := <mirror | redirect>\n");
36 fprintf(stderr
, "\tINDEX is the specific policy instance id\n");
37 fprintf(stderr
, "\tDEVICENAME is the devicename\n");
48 static const char *mirred_n2a(int action
)
51 case TCA_EGRESS_REDIR
:
52 return "Egress Redirect";
53 case TCA_INGRESS_REDIR
:
54 return "Ingress Redirect";
55 case TCA_EGRESS_MIRROR
:
56 return "Egress Mirror";
57 case TCA_INGRESS_MIRROR
:
58 return "Ingress Mirror";
65 parse_direction(struct action_util
*a
, int *argc_p
, char ***argv_p
,
66 int tca_id
, struct nlmsghdr
*n
)
70 char **argv
= *argv_p
;
71 int ok
= 0, iok
= 0, mirror
= 0, redir
= 0, ingress
= 0, egress
= 0;
72 struct tc_mirred p
= {};
78 if (matches(*argv
, "action") == 0) {
80 } else if (!egress
&& matches(*argv
, "egress") == 0) {
83 fprintf(stderr
, "Can't have both egress and ingress\n");
89 } else if (!ingress
&& matches(*argv
, "ingress") == 0) {
92 fprintf(stderr
, "Can't have both ingress and egress\n");
100 if (matches(*argv
, "index") == 0) {
102 if (get_u32(&p
.index
, *argv
, 10)) {
103 fprintf(stderr
, "Illegal \"index\"\n");
113 fprintf(stderr
, "was expecting egress or ingress (%s)\n", *argv
);
116 } else if (!mirror
&& matches(*argv
, "mirror") == 0) {
119 fprintf(stderr
, "Can't have both mirror and redir\n");
122 p
.eaction
= egress
? TCA_EGRESS_MIRROR
:
124 p
.action
= TC_ACT_PIPE
;
126 } else if (!redir
&& matches(*argv
, "redirect") == 0) {
129 fprintf(stderr
, "Can't have both mirror and redir\n");
132 p
.eaction
= egress
? TCA_EGRESS_REDIR
:
134 p
.action
= TC_ACT_STOLEN
;
136 } else if ((redir
|| mirror
) && matches(*argv
, "dev") == 0) {
139 duparg("dev", *argv
);
141 strncpy(d
, *argv
, sizeof(d
)-1);
164 if ((idx
= ll_name_to_index(d
)) == 0) {
165 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
173 if (p
.eaction
== TCA_EGRESS_MIRROR
|| p
.eaction
== TCA_INGRESS_MIRROR
)
174 parse_action_control(&argc
, &argv
, &p
.action
, false);
177 if (iok
&& matches(*argv
, "index") == 0) {
178 fprintf(stderr
, "mirred: Illegal double index\n");
181 if (matches(*argv
, "index") == 0) {
183 if (get_u32(&p
.index
, *argv
, 10)) {
184 fprintf(stderr
, "mirred: Illegal \"index\"\n");
193 tail
= NLMSG_TAIL(n
);
194 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
195 addattr_l(n
, MAX_MSG
, TCA_MIRRED_PARMS
, &p
, sizeof(p
));
196 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
205 parse_mirred(struct action_util
*a
, int *argc_p
, char ***argv_p
,
206 int tca_id
, struct nlmsghdr
*n
)
210 char **argv
= *argv_p
;
213 fprintf(stderr
, "mirred bad argument count %d\n", argc
);
217 if (matches(*argv
, "mirred") == 0) {
220 fprintf(stderr
, "mirred bad argument %s\n", *argv
);
225 if (matches(*argv
, "egress") == 0 || matches(*argv
, "ingress") == 0 ||
226 matches(*argv
, "index") == 0) {
227 int ret
= parse_direction(a
, &argc
, &argv
, tca_id
, n
);
235 } else if (matches(*argv
, "help") == 0) {
238 fprintf(stderr
, "mirred option not supported %s\n", *argv
);
246 print_mirred(struct action_util
*au
, FILE * f
, struct rtattr
*arg
)
249 struct rtattr
*tb
[TCA_MIRRED_MAX
+ 1];
255 parse_rtattr_nested(tb
, TCA_MIRRED_MAX
, arg
);
257 if (tb
[TCA_MIRRED_PARMS
] == NULL
) {
258 fprintf(f
, "[NULL mirred parameters]");
261 p
= RTA_DATA(tb
[TCA_MIRRED_PARMS
]);
268 if ((dev
= ll_index_to_name(p
->ifindex
)) == 0) {
269 fprintf(stderr
, "Cannot find device %d\n", p
->ifindex
);
273 fprintf(f
, "mirred (%s to device %s)", mirred_n2a(p
->eaction
), dev
);
274 print_action_control(f
, " ", p
->action
, "");
277 fprintf(f
, "\tindex %u ref %d bind %d", p
->index
, p
->refcnt
,
281 if (tb
[TCA_MIRRED_TM
]) {
282 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_MIRRED_TM
]);
291 struct action_util mirred_action_util
= {
293 .parse_aopt
= parse_mirred
,
294 .print_aopt
= print_mirred
,