2004-12-09 04:13:56 +08:00
|
|
|
/*
|
2006-12-06 02:10:22 +08:00
|
|
|
* m_egress.c ingress/egress packet mirror/redir actions module
|
2004-12-09 04:13:56 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can distribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
2006-12-06 02:10:22 +08:00
|
|
|
* Authors: J Hadi Salim (hadi@cyberus.ca)
|
|
|
|
*
|
2004-12-09 04:13:56 +08:00
|
|
|
* TODO: Add Ingress support
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "utils.h"
|
|
|
|
#include "tc_util.h"
|
2005-03-15 06:19:16 +08:00
|
|
|
#include "tc_common.h"
|
2004-12-09 04:13:56 +08:00
|
|
|
#include <linux/tc_act/tc_mirred.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
explain(void)
|
|
|
|
{
|
2017-11-27 04:42:17 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: mirred <DIRECTION> <ACTION> [index INDEX] <dev DEVICENAME>\n"
|
|
|
|
"where:\n"
|
|
|
|
"\tDIRECTION := <ingress | egress>\n"
|
|
|
|
"\tACTION := <mirror | redirect>\n"
|
|
|
|
"\tINDEX is the specific policy instance id\n"
|
|
|
|
"\tDEVICENAME is the devicename\n");
|
2004-12-09 04:13:56 +08:00
|
|
|
}
|
|
|
|
|
2006-08-09 03:10:08 +08:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
explain();
|
|
|
|
exit(-1);
|
|
|
|
}
|
2004-12-09 04:13:56 +08:00
|
|
|
|
2013-02-13 03:09:03 +08:00
|
|
|
static const char *mirred_n2a(int action)
|
2004-12-09 04:13:56 +08:00
|
|
|
{
|
|
|
|
switch (action) {
|
|
|
|
case TCA_EGRESS_REDIR:
|
|
|
|
return "Egress Redirect";
|
|
|
|
case TCA_INGRESS_REDIR:
|
|
|
|
return "Ingress Redirect";
|
|
|
|
case TCA_EGRESS_MIRROR:
|
|
|
|
return "Egress Mirror";
|
|
|
|
case TCA_INGRESS_MIRROR:
|
|
|
|
return "Ingress Mirror";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 22:48:34 +08:00
|
|
|
static const char *mirred_direction(int action)
|
|
|
|
{
|
|
|
|
switch (action) {
|
|
|
|
case TCA_EGRESS_REDIR:
|
|
|
|
case TCA_EGRESS_MIRROR:
|
|
|
|
return "egress";
|
|
|
|
case TCA_INGRESS_REDIR:
|
|
|
|
case TCA_INGRESS_MIRROR:
|
|
|
|
return "ingress";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *mirred_action(int action)
|
|
|
|
{
|
|
|
|
switch (action) {
|
|
|
|
case TCA_EGRESS_REDIR:
|
|
|
|
case TCA_INGRESS_REDIR:
|
|
|
|
return "redirect";
|
|
|
|
case TCA_EGRESS_MIRROR:
|
|
|
|
case TCA_INGRESS_MIRROR:
|
|
|
|
return "mirror";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:09:03 +08:00
|
|
|
static int
|
2016-10-19 22:14:09 +08:00
|
|
|
parse_direction(struct action_util *a, int *argc_p, char ***argv_p,
|
|
|
|
int tca_id, struct nlmsghdr *n)
|
2004-12-09 04:13:56 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
int argc = *argc_p;
|
|
|
|
char **argv = *argv_p;
|
2016-10-19 22:14:09 +08:00
|
|
|
int ok = 0, iok = 0, mirror = 0, redir = 0, ingress = 0, egress = 0;
|
2016-07-18 22:48:42 +08:00
|
|
|
struct tc_mirred p = {};
|
2004-12-09 04:13:56 +08:00
|
|
|
struct rtattr *tail;
|
2017-11-25 03:18:31 +08:00
|
|
|
char d[IFNAMSIZ] = {};
|
2004-12-09 04:13:56 +08:00
|
|
|
|
|
|
|
while (argc > 0) {
|
|
|
|
|
|
|
|
if (matches(*argv, "action") == 0) {
|
2018-03-03 02:36:16 +08:00
|
|
|
NEXT_ARG();
|
2004-12-09 04:13:56 +08:00
|
|
|
break;
|
2016-10-19 22:14:09 +08:00
|
|
|
} else if (!egress && matches(*argv, "egress") == 0) {
|
|
|
|
egress = 1;
|
|
|
|
if (ingress) {
|
2017-11-27 04:42:17 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Can't have both egress and ingress\n");
|
2016-10-19 22:14:09 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARG();
|
|
|
|
ok++;
|
|
|
|
continue;
|
|
|
|
} else if (!ingress && matches(*argv, "ingress") == 0) {
|
|
|
|
ingress = 1;
|
|
|
|
if (egress) {
|
2017-11-27 04:42:17 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Can't have both ingress and egress\n");
|
2016-10-19 22:14:09 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2004-12-09 04:13:56 +08:00
|
|
|
NEXT_ARG();
|
|
|
|
ok++;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (matches(*argv, "index") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (get_u32(&p.index, *argv, 10)) {
|
|
|
|
fprintf(stderr, "Illegal \"index\"\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
iok++;
|
|
|
|
if (!ok) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-22 02:48:36 +08:00
|
|
|
} else if (!ok) {
|
2017-11-27 04:42:17 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"was expecting egress or ingress (%s)\n",
|
|
|
|
*argv);
|
2004-12-09 04:13:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
} else if (!mirror && matches(*argv, "mirror") == 0) {
|
2016-03-22 02:48:36 +08:00
|
|
|
mirror = 1;
|
2004-12-09 04:13:56 +08:00
|
|
|
if (redir) {
|
2017-11-27 04:42:17 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Can't have both mirror and redir\n");
|
2004-12-09 04:13:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2016-10-19 22:14:09 +08:00
|
|
|
p.eaction = egress ? TCA_EGRESS_MIRROR :
|
2017-11-27 04:42:17 +08:00
|
|
|
TCA_INGRESS_MIRROR;
|
2004-12-09 04:13:56 +08:00
|
|
|
p.action = TC_ACT_PIPE;
|
|
|
|
ok++;
|
|
|
|
} else if (!redir && matches(*argv, "redirect") == 0) {
|
2016-03-22 02:48:36 +08:00
|
|
|
redir = 1;
|
2004-12-09 04:13:56 +08:00
|
|
|
if (mirror) {
|
2017-11-27 04:42:17 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Can't have both mirror and redir\n");
|
2004-12-09 04:13:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2016-10-19 22:14:09 +08:00
|
|
|
p.eaction = egress ? TCA_EGRESS_REDIR :
|
2017-11-27 04:42:17 +08:00
|
|
|
TCA_INGRESS_REDIR;
|
2004-12-09 04:13:56 +08:00
|
|
|
p.action = TC_ACT_STOLEN;
|
|
|
|
ok++;
|
2017-11-27 04:42:17 +08:00
|
|
|
} else if ((redir || mirror) &&
|
|
|
|
matches(*argv, "dev") == 0) {
|
2004-12-09 04:13:56 +08:00
|
|
|
NEXT_ARG();
|
|
|
|
if (strlen(d))
|
|
|
|
duparg("dev", *argv);
|
|
|
|
|
|
|
|
strncpy(d, *argv, sizeof(d)-1);
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
|
2017-11-27 04:42:17 +08:00
|
|
|
if (!ok && !iok)
|
2004-12-09 04:13:56 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (d[0]) {
|
|
|
|
int idx;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-12-09 04:13:56 +08:00
|
|
|
ll_init_map(&rth);
|
|
|
|
|
2017-11-27 04:42:17 +08:00
|
|
|
idx = ll_name_to_index(d);
|
2018-03-07 16:40:36 +08:00
|
|
|
if (!idx)
|
|
|
|
return nodev(d);
|
2004-12-09 04:13:56 +08:00
|
|
|
|
|
|
|
p.ifindex = idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-03 02:36:16 +08:00
|
|
|
if (p.eaction == TCA_EGRESS_MIRROR || p.eaction == TCA_INGRESS_MIRROR)
|
2017-05-17 01:29:36 +08:00
|
|
|
parse_action_control(&argc, &argv, &p.action, false);
|
2004-12-09 04:13:56 +08:00
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
if (iok && matches(*argv, "index") == 0) {
|
|
|
|
fprintf(stderr, "mirred: Illegal double index\n");
|
|
|
|
return -1;
|
2017-11-27 04:42:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (matches(*argv, "index") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (get_u32(&p.index, *argv, 10)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"mirred: Illegal \"index\"\n");
|
|
|
|
return -1;
|
2004-12-09 04:13:56 +08:00
|
|
|
}
|
2017-11-27 04:42:17 +08:00
|
|
|
argc--;
|
|
|
|
argv++;
|
2004-12-09 04:13:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:15:08 +08:00
|
|
|
tail = addattr_nest(n, MAX_MSG, tca_id);
|
2016-03-22 02:48:36 +08:00
|
|
|
addattr_l(n, MAX_MSG, TCA_MIRRED_PARMS, &p, sizeof(p));
|
2018-01-31 16:15:08 +08:00
|
|
|
addattr_nest_end(n, tail);
|
2004-12-09 04:13:56 +08:00
|
|
|
|
|
|
|
*argc_p = argc;
|
|
|
|
*argv_p = argv;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-13 03:09:03 +08:00
|
|
|
static int
|
|
|
|
parse_mirred(struct action_util *a, int *argc_p, char ***argv_p,
|
|
|
|
int tca_id, struct nlmsghdr *n)
|
2004-12-09 04:13:56 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
int argc = *argc_p;
|
|
|
|
char **argv = *argv_p;
|
|
|
|
|
|
|
|
if (argc < 0) {
|
2016-03-22 02:48:36 +08:00
|
|
|
fprintf(stderr, "mirred bad argument count %d\n", argc);
|
2004-12-09 04:13:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matches(*argv, "mirred") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
} else {
|
2016-03-22 02:48:36 +08:00
|
|
|
fprintf(stderr, "mirred bad argument %s\n", *argv);
|
2004-12-09 04:13:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-19 22:14:09 +08:00
|
|
|
if (matches(*argv, "egress") == 0 || matches(*argv, "ingress") == 0 ||
|
|
|
|
matches(*argv, "index") == 0) {
|
|
|
|
int ret = parse_direction(a, &argc, &argv, tca_id, n);
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-12-09 04:13:56 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
*argc_p = argc;
|
|
|
|
*argv_p = argv;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-09 03:10:08 +08:00
|
|
|
} else if (matches(*argv, "help") == 0) {
|
|
|
|
usage();
|
2004-12-09 04:13:56 +08:00
|
|
|
} else {
|
2016-03-22 02:48:36 +08:00
|
|
|
fprintf(stderr, "mirred option not supported %s\n", *argv);
|
2004-12-09 04:13:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2006-12-06 02:10:22 +08:00
|
|
|
|
2004-12-09 04:13:56 +08:00
|
|
|
}
|
|
|
|
|
2013-02-13 03:09:03 +08:00
|
|
|
static int
|
2017-11-27 04:42:17 +08:00
|
|
|
print_mirred(struct action_util *au, FILE *f, struct rtattr *arg)
|
2004-12-09 04:13:56 +08:00
|
|
|
{
|
|
|
|
struct tc_mirred *p;
|
|
|
|
struct rtattr *tb[TCA_MIRRED_MAX + 1];
|
|
|
|
const char *dev;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-12-09 04:13:56 +08:00
|
|
|
if (arg == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2005-01-19 06:11:58 +08:00
|
|
|
parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg);
|
2004-12-09 04:13:56 +08:00
|
|
|
|
|
|
|
if (tb[TCA_MIRRED_PARMS] == NULL) {
|
2017-11-25 22:48:34 +08:00
|
|
|
print_string(PRINT_FP, NULL, "%s", "[NULL mirred parameters]");
|
2004-12-09 04:13:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p = RTA_DATA(tb[TCA_MIRRED_PARMS]);
|
|
|
|
|
2017-11-27 04:42:17 +08:00
|
|
|
dev = ll_index_to_name(p->ifindex);
|
|
|
|
if (dev == 0) {
|
2004-12-09 04:13:56 +08:00
|
|
|
fprintf(stderr, "Cannot find device %d\n", p->ifindex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-25 22:48:34 +08:00
|
|
|
print_string(PRINT_ANY, "kind", "%s ", "mirred");
|
|
|
|
print_string(PRINT_FP, NULL, "(%s", mirred_n2a(p->eaction));
|
|
|
|
print_string(PRINT_JSON, "mirred_action", NULL,
|
|
|
|
mirred_action(p->eaction));
|
|
|
|
print_string(PRINT_JSON, "direction", NULL,
|
|
|
|
mirred_direction(p->eaction));
|
|
|
|
print_string(PRINT_ANY, "to_dev", " to device %s)", dev);
|
2017-05-17 01:29:36 +08:00
|
|
|
print_action_control(f, " ", p->action, "");
|
2004-12-09 04:13:56 +08:00
|
|
|
|
2017-11-25 22:48:34 +08:00
|
|
|
print_uint(PRINT_ANY, "index", "\n \tindex %u", p->index);
|
|
|
|
print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
|
|
|
|
print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
|
2004-12-09 04:13:56 +08:00
|
|
|
|
|
|
|
if (show_stats) {
|
|
|
|
if (tb[TCA_MIRRED_TM]) {
|
|
|
|
struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]);
|
2016-03-22 02:48:36 +08:00
|
|
|
|
|
|
|
print_tm(f, tm);
|
2004-12-09 04:13:56 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-25 22:48:34 +08:00
|
|
|
print_string(PRINT_FP, NULL, "%s", "\n ");
|
2004-12-09 04:13:56 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-31 02:43:30 +08:00
|
|
|
struct action_util mirred_action_util = {
|
2004-12-09 04:13:56 +08:00
|
|
|
.id = "mirred",
|
|
|
|
.parse_aopt = parse_mirred,
|
|
|
|
.print_aopt = print_mirred,
|
|
|
|
};
|