1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * m_egress.c ingress/egress packet mirror/redir actions module
5 * Authors: J Hadi Salim (hadi@cyberus.ca)
7 * TODO: Add Ingress support
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
20 #include "tc_common.h"
21 #include <linux/tc_act/tc_mirred.h>
27 "Usage: mirred <DIRECTION> <ACTION> [index INDEX] <TARGET>\n"
29 "\tDIRECTION := <ingress | egress>\n"
30 "\tACTION := <mirror | redirect>\n"
31 "\tINDEX is the specific policy instance id\n"
32 "\tTARGET := <BLOCK | DEVICE>\n"
33 "\tDEVICE := dev DEVICENAME\n"
34 "\tDEVICENAME is the devicename\n"
35 "\tBLOCK := blockid BLOCKID\n"
36 "\tBLOCKID := 32-bit unsigned block ID\n");
46 static const char *mirred_n2a(int action
)
49 case TCA_EGRESS_REDIR
:
50 return "Egress Redirect";
51 case TCA_INGRESS_REDIR
:
52 return "Ingress Redirect";
53 case TCA_EGRESS_MIRROR
:
54 return "Egress Mirror";
55 case TCA_INGRESS_MIRROR
:
56 return "Ingress Mirror";
62 static const char *mirred_direction(int action
)
65 case TCA_EGRESS_REDIR
:
66 case TCA_EGRESS_MIRROR
:
68 case TCA_INGRESS_REDIR
:
69 case TCA_INGRESS_MIRROR
:
76 static const char *mirred_action(int action
)
79 case TCA_EGRESS_REDIR
:
80 case TCA_INGRESS_REDIR
:
82 case TCA_EGRESS_MIRROR
:
83 case TCA_INGRESS_MIRROR
:
91 parse_direction(const struct action_util
*a
, int *argc_p
, char ***argv_p
,
92 int tca_id
, struct nlmsghdr
*n
)
96 char **argv
= *argv_p
;
97 int ok
= 0, iok
= 0, mirror
= 0, redir
= 0, ingress
= 0, egress
= 0;
98 struct tc_mirred p
= {};
100 char d
[IFNAMSIZ
] = {};
105 if (matches(*argv
, "action") == 0) {
108 } else if (!egress
&& matches(*argv
, "egress") == 0) {
112 "Can't have both egress and ingress\n");
118 } else if (!ingress
&& matches(*argv
, "ingress") == 0) {
122 "Can't have both ingress and egress\n");
130 if (matches(*argv
, "index") == 0) {
132 if (get_u32(&p
.index
, *argv
, 10)) {
133 fprintf(stderr
, "Illegal \"index\"\n");
144 "was expecting egress or ingress (%s)\n",
148 } else if (!mirror
&& matches(*argv
, "mirror") == 0) {
152 "Can't have both mirror and redir\n");
155 p
.eaction
= egress
? TCA_EGRESS_MIRROR
:
157 p
.action
= TC_ACT_PIPE
;
159 } else if (!redir
&& matches(*argv
, "redirect") == 0) {
163 "Can't have both mirror and redir\n");
166 p
.eaction
= egress
? TCA_EGRESS_REDIR
:
168 p
.action
= TC_ACT_STOLEN
;
170 } else if ((redir
|| mirror
)) {
171 if (strcmp(*argv
, "blockid") == 0) {
174 "blockid and device are mutually exclusive.\n");
178 if (get_u32(&blockid
, *argv
, 0) ||
187 if (argc
&& matches(*argv
, "dev") == 0) {
190 "blockid and device are mutually exclusive.\n");
195 duparg("dev", *argv
);
197 strncpy(d
, *argv
, sizeof(d
)-1);
218 idx
= ll_name_to_index(d
);
226 if (p
.eaction
== TCA_EGRESS_MIRROR
|| p
.eaction
== TCA_INGRESS_MIRROR
)
227 parse_action_control_dflt(&argc
, &argv
, &p
.action
, false,
231 if (iok
&& matches(*argv
, "index") == 0) {
232 fprintf(stderr
, "mirred: Illegal double index\n");
236 if (matches(*argv
, "index") == 0) {
238 if (get_u32(&p
.index
, *argv
, 10)) {
240 "mirred: Illegal \"index\"\n");
248 tail
= addattr_nest(n
, MAX_MSG
, tca_id
);
249 addattr_l(n
, MAX_MSG
, TCA_MIRRED_PARMS
, &p
, sizeof(p
));
251 addattr32(n
, MAX_MSG
, TCA_MIRRED_BLOCKID
, blockid
);
252 addattr_nest_end(n
, tail
);
261 parse_mirred(const struct action_util
*a
, int *argc_p
, char ***argv_p
,
262 int tca_id
, struct nlmsghdr
*n
)
266 char **argv
= *argv_p
;
269 fprintf(stderr
, "mirred bad argument count %d\n", argc
);
273 if (matches(*argv
, "mirred") == 0) {
276 fprintf(stderr
, "mirred bad argument %s\n", *argv
);
281 if (matches(*argv
, "egress") == 0 || matches(*argv
, "ingress") == 0 ||
282 matches(*argv
, "index") == 0) {
283 int ret
= parse_direction(a
, &argc
, &argv
, tca_id
, n
);
291 } else if (matches(*argv
, "help") == 0) {
294 fprintf(stderr
, "mirred option not supported %s\n", *argv
);
302 print_mirred(const struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
305 struct rtattr
*tb
[TCA_MIRRED_MAX
+ 1];
308 print_string(PRINT_ANY
, "kind", "%s ", "mirred");
312 parse_rtattr_nested(tb
, TCA_MIRRED_MAX
, arg
);
314 if (tb
[TCA_MIRRED_PARMS
] == NULL
) {
315 fprintf(stderr
, "Missing mirred parameters\n");
318 p
= RTA_DATA(tb
[TCA_MIRRED_PARMS
]);
320 dev
= ll_index_to_name(p
->ifindex
);
322 fprintf(stderr
, "Cannot find device %d\n", p
->ifindex
);
326 print_string(PRINT_FP
, NULL
, "(%s", mirred_n2a(p
->eaction
));
327 print_string(PRINT_JSON
, "mirred_action", NULL
,
328 mirred_action(p
->eaction
));
329 print_string(PRINT_JSON
, "direction", NULL
,
330 mirred_direction(p
->eaction
));
331 if (tb
[TCA_MIRRED_BLOCKID
]) {
332 const __u32
*blockid
= RTA_DATA(tb
[TCA_MIRRED_BLOCKID
]);
334 print_uint(PRINT_ANY
, "to_blockid", " to blockid %u)",
337 print_string(PRINT_ANY
, "to_dev", " to device %s)", dev
);
340 print_action_control(f
, " ", p
->action
, "");
343 print_uint(PRINT_ANY
, "index", "\tindex %u", p
->index
);
344 print_int(PRINT_ANY
, "ref", " ref %d", p
->refcnt
);
345 print_int(PRINT_ANY
, "bind", " bind %d", p
->bindcnt
);
348 if (tb
[TCA_MIRRED_TM
]) {
349 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_MIRRED_TM
]);
358 struct action_util mirred_action_util
= {
360 .parse_aopt
= parse_mirred
,
361 .print_aopt
= print_mirred
,