blob: 0066905ace553704ff6ac7ba83a914ec2531a4f8 [file] [log] [blame]
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +00001/*
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002 * m_egress.c ingress/egress packet mirror/redir actions module
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +00003 *
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.
8 *
Stephen Hemmingerae665a52006-12-05 10:10:22 -08009 * Authors: J Hadi Salim (hadi@cyberus.ca)
10 *
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000011 * TODO: Add Ingress support
12 *
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <unistd.h>
18#include <syslog.h>
19#include <fcntl.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <string.h>
24#include "utils.h"
25#include "tc_util.h"
site!shemmingerc1027a72005-03-14 22:19:16 +000026#include "tc_common.h"
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000027#include <linux/tc_act/tc_mirred.h>
28
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000029static void
30explain(void)
31{
Stephen Hemminger32a121c2016-03-21 11:48:36 -070032 fprintf(stderr, "Usage: mirred <DIRECTION> <ACTION> [index INDEX] <dev DEVICENAME>\n");
33 fprintf(stderr, "where:\n");
Jamal Hadi Salimebf32082006-08-08 12:10:08 -070034 fprintf(stderr, "\tDIRECTION := <ingress | egress>\n");
35 fprintf(stderr, "\tACTION := <mirror | redirect>\n");
36 fprintf(stderr, "\tINDEX is the specific policy instance id\n");
Stephen Hemminger32a121c2016-03-21 11:48:36 -070037 fprintf(stderr, "\tDEVICENAME is the devicename\n");
Jamal Hadi Salimebf32082006-08-08 12:10:08 -070038
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000039}
40
Jamal Hadi Salimebf32082006-08-08 12:10:08 -070041static void
42usage(void)
43{
44 explain();
45 exit(-1);
46}
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000047
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080048static const char *mirred_n2a(int action)
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000049{
50 switch (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";
59 default:
60 return "unknown";
61 }
62}
63
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080064static int
65parse_egress(struct action_util *a, int *argc_p, char ***argv_p,
66 int tca_id, struct nlmsghdr *n)
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000067{
68
69 int argc = *argc_p;
70 char **argv = *argv_p;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070071 int ok = 0, iok = 0, mirror = 0, redir = 0;
Phil Sutterd17b1362016-07-18 16:48:42 +020072 struct tc_mirred p = {};
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000073 struct rtattr *tail;
Phil Sutterd17b1362016-07-18 16:48:42 +020074 char d[16] = {};
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000075
76 while (argc > 0) {
77
78 if (matches(*argv, "action") == 0) {
79 break;
80 } else if (matches(*argv, "egress") == 0) {
81 NEXT_ARG();
82 ok++;
83 continue;
84 } else {
85
86 if (matches(*argv, "index") == 0) {
87 NEXT_ARG();
88 if (get_u32(&p.index, *argv, 10)) {
89 fprintf(stderr, "Illegal \"index\"\n");
90 return -1;
91 }
92 iok++;
93 if (!ok) {
94 argc--;
95 argv++;
96 break;
97 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -070098 } else if (!ok) {
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +000099 fprintf(stderr, "was expecting egress (%s)\n", *argv);
100 break;
101
102 } else if (!mirror && matches(*argv, "mirror") == 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700103 mirror = 1;
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000104 if (redir) {
Stephen Hemmingerb8a45892013-08-04 15:10:05 -0700105 fprintf(stderr, "Can't have both mirror and redir\n");
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000106 return -1;
107 }
108 p.eaction = TCA_EGRESS_MIRROR;
109 p.action = TC_ACT_PIPE;
110 ok++;
111 } else if (!redir && matches(*argv, "redirect") == 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700112 redir = 1;
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000113 if (mirror) {
Stephen Hemmingerb8a45892013-08-04 15:10:05 -0700114 fprintf(stderr, "Can't have both mirror and redir\n");
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000115 return -1;
116 }
117 p.eaction = TCA_EGRESS_REDIR;
118 p.action = TC_ACT_STOLEN;
119 ok++;
120 } else if ((redir || mirror) && matches(*argv, "dev") == 0) {
121 NEXT_ARG();
122 if (strlen(d))
123 duparg("dev", *argv);
124
125 strncpy(d, *argv, sizeof(d)-1);
126 argc--;
127 argv++;
128
129 break;
130
131 }
132 }
133
134 NEXT_ARG();
135 }
136
137 if (!ok && !iok) {
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000138 return -1;
139 }
140
141
142
143 if (d[0]) {
144 int idx;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700145
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000146 ll_init_map(&rth);
147
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000148 if ((idx = ll_name_to_index(d)) == 0) {
149 fprintf(stderr, "Cannot find device \"%s\"\n", d);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000150 return -1;
151 }
152
153 p.ifindex = idx;
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000154 }
155
156
157 if (argc && p.eaction == TCA_EGRESS_MIRROR) {
158
159 if (matches(*argv, "reclassify") == 0) {
160 p.action = TC_POLICE_RECLASSIFY;
161 NEXT_ARG();
162 } else if (matches(*argv, "pipe") == 0) {
163 p.action = TC_POLICE_PIPE;
164 NEXT_ARG();
165 } else if (matches(*argv, "drop") == 0 ||
166 matches(*argv, "shot") == 0) {
167 p.action = TC_POLICE_SHOT;
168 NEXT_ARG();
169 } else if (matches(*argv, "continue") == 0) {
170 p.action = TC_POLICE_UNSPEC;
171 NEXT_ARG();
Jamal Hadi Salim43726b72016-05-07 09:39:36 -0400172 } else if (matches(*argv, "pass") == 0 ||
173 matches(*argv, "ok") == 0) {
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000174 p.action = TC_POLICE_OK;
175 NEXT_ARG();
176 }
177
178 }
179
180 if (argc) {
181 if (iok && matches(*argv, "index") == 0) {
182 fprintf(stderr, "mirred: Illegal double index\n");
183 return -1;
184 } else {
185 if (matches(*argv, "index") == 0) {
186 NEXT_ARG();
187 if (get_u32(&p.index, *argv, 10)) {
188 fprintf(stderr, "mirred: Illegal \"index\"\n");
189 return -1;
190 }
191 argc--;
192 argv++;
193 }
194 }
195 }
196
3!tgraf60a383a2005-01-18 01:24:18 +0000197 tail = NLMSG_TAIL(n);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000198 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700199 addattr_l(n, MAX_MSG, TCA_MIRRED_PARMS, &p, sizeof(p));
3!tgraf60a383a2005-01-18 01:24:18 +0000200 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000201
202 *argc_p = argc;
203 *argv_p = argv;
204 return 0;
205}
206
207
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800208static int
209parse_mirred(struct action_util *a, int *argc_p, char ***argv_p,
210 int tca_id, struct nlmsghdr *n)
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000211{
212
213 int argc = *argc_p;
214 char **argv = *argv_p;
215
216 if (argc < 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700217 fprintf(stderr, "mirred bad argument count %d\n", argc);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000218 return -1;
219 }
220
221 if (matches(*argv, "mirred") == 0) {
222 NEXT_ARG();
223 } else {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700224 fprintf(stderr, "mirred bad argument %s\n", *argv);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000225 return -1;
226 }
227
228
229 if (matches(*argv, "egress") == 0 || matches(*argv, "index") == 0) {
230 int ret = parse_egress(a, &argc, &argv, tca_id, n);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700231
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000232 if (ret == 0) {
233 *argc_p = argc;
234 *argv_p = argv;
235 return 0;
236 }
237
238 } else if (matches(*argv, "ingress") == 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700239 fprintf(stderr, "mirred ingress not supported at the moment\n");
Jamal Hadi Salimebf32082006-08-08 12:10:08 -0700240 } else if (matches(*argv, "help") == 0) {
241 usage();
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000242 } else {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700243 fprintf(stderr, "mirred option not supported %s\n", *argv);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000244 }
245
246 return -1;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800247
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000248}
249
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800250static int
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700251print_mirred(struct action_util *au, FILE * f, struct rtattr *arg)
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000252{
253 struct tc_mirred *p;
254 struct rtattr *tb[TCA_MIRRED_MAX + 1];
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000255 const char *dev;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700256
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000257 SPRINT_BUF(b1);
258
259 if (arg == NULL)
260 return -1;
261
3!tgraf5cb5ee32005-01-18 22:11:58 +0000262 parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000263
264 if (tb[TCA_MIRRED_PARMS] == NULL) {
265 fprintf(f, "[NULL mirred parameters]");
266 return -1;
267 }
268 p = RTA_DATA(tb[TCA_MIRRED_PARMS]);
269
osdl.net!shemminger6ce88ca2005-03-30 18:43:30 +0000270 /*
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000271 ll_init_map(&rth);
osdl.net!shemminger6ce88ca2005-03-30 18:43:30 +0000272 */
273
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000274
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000275 if ((dev = ll_index_to_name(p->ifindex)) == 0) {
276 fprintf(stderr, "Cannot find device %d\n", p->ifindex);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000277 return -1;
278 }
279
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700280 fprintf(f, "mirred (%s to device %s) %s", mirred_n2a(p->eaction), dev, action_n2a(p->action, b1, sizeof (b1)));
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000281
282 fprintf(f, "\n ");
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700283 fprintf(f, "\tindex %d ref %d bind %d", p->index, p->refcnt, p->bindcnt);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000284
285 if (show_stats) {
286 if (tb[TCA_MIRRED_TM]) {
287 struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700288
289 print_tm(f, tm);
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000290 }
291 }
292 fprintf(f, "\n ");
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000293 return 0;
294}
295
osdl.net!shemminger6ce88ca2005-03-30 18:43:30 +0000296struct action_util mirred_action_util = {
net[shemminger]!shemminger00fa8482004-12-08 20:13:56 +0000297 .id = "mirred",
298 .parse_aopt = parse_mirred,
299 .print_aopt = print_mirred,
300};