]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_mirred.c
Use C99 style initializers everywhere
[mirror_iproute2.git] / tc / m_mirred.c
1 /*
2 * m_egress.c ingress/egress packet mirror/redir actions module
3 *
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 *
9 * Authors: J Hadi Salim (hadi@cyberus.ca)
10 *
11 * 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"
26 #include "tc_common.h"
27 #include <linux/tc_act/tc_mirred.h>
28
29 static void
30 explain(void)
31 {
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");
38
39 }
40
41 static void
42 usage(void)
43 {
44 explain();
45 exit(-1);
46 }
47
48 static const char *mirred_n2a(int action)
49 {
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
64 static int
65 parse_egress(struct action_util *a, int *argc_p, char ***argv_p,
66 int tca_id, struct nlmsghdr *n)
67 {
68
69 int argc = *argc_p;
70 char **argv = *argv_p;
71 int ok = 0, iok = 0, mirror = 0, redir = 0;
72 struct tc_mirred p = {};
73 struct rtattr *tail;
74 char d[16] = {};
75
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 }
98 } else if (!ok) {
99 fprintf(stderr, "was expecting egress (%s)\n", *argv);
100 break;
101
102 } else if (!mirror && matches(*argv, "mirror") == 0) {
103 mirror = 1;
104 if (redir) {
105 fprintf(stderr, "Can't have both mirror and redir\n");
106 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) {
112 redir = 1;
113 if (mirror) {
114 fprintf(stderr, "Can't have both mirror and redir\n");
115 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) {
138 return -1;
139 }
140
141
142
143 if (d[0]) {
144 int idx;
145
146 ll_init_map(&rth);
147
148 if ((idx = ll_name_to_index(d)) == 0) {
149 fprintf(stderr, "Cannot find device \"%s\"\n", d);
150 return -1;
151 }
152
153 p.ifindex = idx;
154 }
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();
172 } else if (matches(*argv, "pass") == 0 ||
173 matches(*argv, "ok") == 0) {
174 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
197 tail = NLMSG_TAIL(n);
198 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
199 addattr_l(n, MAX_MSG, TCA_MIRRED_PARMS, &p, sizeof(p));
200 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
201
202 *argc_p = argc;
203 *argv_p = argv;
204 return 0;
205 }
206
207
208 static int
209 parse_mirred(struct action_util *a, int *argc_p, char ***argv_p,
210 int tca_id, struct nlmsghdr *n)
211 {
212
213 int argc = *argc_p;
214 char **argv = *argv_p;
215
216 if (argc < 0) {
217 fprintf(stderr, "mirred bad argument count %d\n", argc);
218 return -1;
219 }
220
221 if (matches(*argv, "mirred") == 0) {
222 NEXT_ARG();
223 } else {
224 fprintf(stderr, "mirred bad argument %s\n", *argv);
225 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);
231
232 if (ret == 0) {
233 *argc_p = argc;
234 *argv_p = argv;
235 return 0;
236 }
237
238 } else if (matches(*argv, "ingress") == 0) {
239 fprintf(stderr, "mirred ingress not supported at the moment\n");
240 } else if (matches(*argv, "help") == 0) {
241 usage();
242 } else {
243 fprintf(stderr, "mirred option not supported %s\n", *argv);
244 }
245
246 return -1;
247
248 }
249
250 static int
251 print_mirred(struct action_util *au, FILE * f, struct rtattr *arg)
252 {
253 struct tc_mirred *p;
254 struct rtattr *tb[TCA_MIRRED_MAX + 1];
255 const char *dev;
256
257 SPRINT_BUF(b1);
258
259 if (arg == NULL)
260 return -1;
261
262 parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg);
263
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
270 /*
271 ll_init_map(&rth);
272 */
273
274
275 if ((dev = ll_index_to_name(p->ifindex)) == 0) {
276 fprintf(stderr, "Cannot find device %d\n", p->ifindex);
277 return -1;
278 }
279
280 fprintf(f, "mirred (%s to device %s) %s", mirred_n2a(p->eaction), dev, action_n2a(p->action, b1, sizeof (b1)));
281
282 fprintf(f, "\n ");
283 fprintf(f, "\tindex %d ref %d bind %d", p->index, p->refcnt, p->bindcnt);
284
285 if (show_stats) {
286 if (tb[TCA_MIRRED_TM]) {
287 struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]);
288
289 print_tm(f, tm);
290 }
291 }
292 fprintf(f, "\n ");
293 return 0;
294 }
295
296 struct action_util mirred_action_util = {
297 .id = "mirred",
298 .parse_aopt = parse_mirred,
299 .print_aopt = print_mirred,
300 };