]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_mirred.c
tc: pass correct conversion specifier to print 'unsigned int' action index.
[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_direction(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, ingress = 0, egress = 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 (!egress && matches(*argv, "egress") == 0) {
81 egress = 1;
82 if (ingress) {
83 fprintf(stderr, "Can't have both egress and ingress\n");
84 return -1;
85 }
86 NEXT_ARG();
87 ok++;
88 continue;
89 } else if (!ingress && matches(*argv, "ingress") == 0) {
90 ingress = 1;
91 if (egress) {
92 fprintf(stderr, "Can't have both ingress and egress\n");
93 return -1;
94 }
95 NEXT_ARG();
96 ok++;
97 continue;
98 } else {
99
100 if (matches(*argv, "index") == 0) {
101 NEXT_ARG();
102 if (get_u32(&p.index, *argv, 10)) {
103 fprintf(stderr, "Illegal \"index\"\n");
104 return -1;
105 }
106 iok++;
107 if (!ok) {
108 argc--;
109 argv++;
110 break;
111 }
112 } else if (!ok) {
113 fprintf(stderr, "was expecting egress or ingress (%s)\n", *argv);
114 break;
115
116 } else if (!mirror && matches(*argv, "mirror") == 0) {
117 mirror = 1;
118 if (redir) {
119 fprintf(stderr, "Can't have both mirror and redir\n");
120 return -1;
121 }
122 p.eaction = egress ? TCA_EGRESS_MIRROR :
123 TCA_INGRESS_MIRROR;
124 p.action = TC_ACT_PIPE;
125 ok++;
126 } else if (!redir && matches(*argv, "redirect") == 0) {
127 redir = 1;
128 if (mirror) {
129 fprintf(stderr, "Can't have both mirror and redir\n");
130 return -1;
131 }
132 p.eaction = egress ? TCA_EGRESS_REDIR :
133 TCA_INGRESS_REDIR;
134 p.action = TC_ACT_STOLEN;
135 ok++;
136 } else if ((redir || mirror) && matches(*argv, "dev") == 0) {
137 NEXT_ARG();
138 if (strlen(d))
139 duparg("dev", *argv);
140
141 strncpy(d, *argv, sizeof(d)-1);
142 argc--;
143 argv++;
144
145 break;
146
147 }
148 }
149
150 NEXT_ARG();
151 }
152
153 if (!ok && !iok) {
154 return -1;
155 }
156
157
158
159 if (d[0]) {
160 int idx;
161
162 ll_init_map(&rth);
163
164 if ((idx = ll_name_to_index(d)) == 0) {
165 fprintf(stderr, "Cannot find device \"%s\"\n", d);
166 return -1;
167 }
168
169 p.ifindex = idx;
170 }
171
172
173 if (argc &&
174 (p.eaction == TCA_EGRESS_MIRROR || p.eaction == TCA_INGRESS_MIRROR)
175 && !action_a2n(*argv, &p.action, false))
176 NEXT_ARG();
177
178 if (argc) {
179 if (iok && matches(*argv, "index") == 0) {
180 fprintf(stderr, "mirred: Illegal double index\n");
181 return -1;
182 } else {
183 if (matches(*argv, "index") == 0) {
184 NEXT_ARG();
185 if (get_u32(&p.index, *argv, 10)) {
186 fprintf(stderr, "mirred: Illegal \"index\"\n");
187 return -1;
188 }
189 argc--;
190 argv++;
191 }
192 }
193 }
194
195 tail = NLMSG_TAIL(n);
196 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
197 addattr_l(n, MAX_MSG, TCA_MIRRED_PARMS, &p, sizeof(p));
198 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
199
200 *argc_p = argc;
201 *argv_p = argv;
202 return 0;
203 }
204
205
206 static int
207 parse_mirred(struct action_util *a, int *argc_p, char ***argv_p,
208 int tca_id, struct nlmsghdr *n)
209 {
210
211 int argc = *argc_p;
212 char **argv = *argv_p;
213
214 if (argc < 0) {
215 fprintf(stderr, "mirred bad argument count %d\n", argc);
216 return -1;
217 }
218
219 if (matches(*argv, "mirred") == 0) {
220 NEXT_ARG();
221 } else {
222 fprintf(stderr, "mirred bad argument %s\n", *argv);
223 return -1;
224 }
225
226
227 if (matches(*argv, "egress") == 0 || matches(*argv, "ingress") == 0 ||
228 matches(*argv, "index") == 0) {
229 int ret = parse_direction(a, &argc, &argv, tca_id, n);
230
231 if (ret == 0) {
232 *argc_p = argc;
233 *argv_p = argv;
234 return 0;
235 }
236
237 } else if (matches(*argv, "help") == 0) {
238 usage();
239 } else {
240 fprintf(stderr, "mirred option not supported %s\n", *argv);
241 }
242
243 return -1;
244
245 }
246
247 static int
248 print_mirred(struct action_util *au, FILE * f, struct rtattr *arg)
249 {
250 struct tc_mirred *p;
251 struct rtattr *tb[TCA_MIRRED_MAX + 1];
252 const char *dev;
253
254 if (arg == NULL)
255 return -1;
256
257 parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg);
258
259 if (tb[TCA_MIRRED_PARMS] == NULL) {
260 fprintf(f, "[NULL mirred parameters]");
261 return -1;
262 }
263 p = RTA_DATA(tb[TCA_MIRRED_PARMS]);
264
265 /*
266 ll_init_map(&rth);
267 */
268
269
270 if ((dev = ll_index_to_name(p->ifindex)) == 0) {
271 fprintf(stderr, "Cannot find device %d\n", p->ifindex);
272 return -1;
273 }
274
275 fprintf(f, "mirred (%s to device %s) %s",
276 mirred_n2a(p->eaction), dev, action_n2a(p->action));
277
278 fprintf(f, "\n ");
279 fprintf(f, "\tindex %u ref %d bind %d", p->index, p->refcnt,
280 p->bindcnt);
281
282 if (show_stats) {
283 if (tb[TCA_MIRRED_TM]) {
284 struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]);
285
286 print_tm(f, tm);
287 }
288 }
289 fprintf(f, "\n ");
290 return 0;
291 }
292
293 struct action_util mirred_action_util = {
294 .id = "mirred",
295 .parse_aopt = parse_mirred,
296 .print_aopt = print_mirred,
297 };