]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_mirred.c
tc: util: No need for action_n2a() to be reentrant
[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 && !action_a2n(*argv, &p.action, false))
159 NEXT_ARG();
160
161 if (argc) {
162 if (iok && matches(*argv, "index") == 0) {
163 fprintf(stderr, "mirred: Illegal double index\n");
164 return -1;
165 } else {
166 if (matches(*argv, "index") == 0) {
167 NEXT_ARG();
168 if (get_u32(&p.index, *argv, 10)) {
169 fprintf(stderr, "mirred: Illegal \"index\"\n");
170 return -1;
171 }
172 argc--;
173 argv++;
174 }
175 }
176 }
177
178 tail = NLMSG_TAIL(n);
179 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
180 addattr_l(n, MAX_MSG, TCA_MIRRED_PARMS, &p, sizeof(p));
181 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
182
183 *argc_p = argc;
184 *argv_p = argv;
185 return 0;
186 }
187
188
189 static int
190 parse_mirred(struct action_util *a, int *argc_p, char ***argv_p,
191 int tca_id, struct nlmsghdr *n)
192 {
193
194 int argc = *argc_p;
195 char **argv = *argv_p;
196
197 if (argc < 0) {
198 fprintf(stderr, "mirred bad argument count %d\n", argc);
199 return -1;
200 }
201
202 if (matches(*argv, "mirred") == 0) {
203 NEXT_ARG();
204 } else {
205 fprintf(stderr, "mirred bad argument %s\n", *argv);
206 return -1;
207 }
208
209
210 if (matches(*argv, "egress") == 0 || matches(*argv, "index") == 0) {
211 int ret = parse_egress(a, &argc, &argv, tca_id, n);
212
213 if (ret == 0) {
214 *argc_p = argc;
215 *argv_p = argv;
216 return 0;
217 }
218
219 } else if (matches(*argv, "ingress") == 0) {
220 fprintf(stderr, "mirred ingress not supported at the moment\n");
221 } else if (matches(*argv, "help") == 0) {
222 usage();
223 } else {
224 fprintf(stderr, "mirred option not supported %s\n", *argv);
225 }
226
227 return -1;
228
229 }
230
231 static int
232 print_mirred(struct action_util *au, FILE * f, struct rtattr *arg)
233 {
234 struct tc_mirred *p;
235 struct rtattr *tb[TCA_MIRRED_MAX + 1];
236 const char *dev;
237
238 if (arg == NULL)
239 return -1;
240
241 parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg);
242
243 if (tb[TCA_MIRRED_PARMS] == NULL) {
244 fprintf(f, "[NULL mirred parameters]");
245 return -1;
246 }
247 p = RTA_DATA(tb[TCA_MIRRED_PARMS]);
248
249 /*
250 ll_init_map(&rth);
251 */
252
253
254 if ((dev = ll_index_to_name(p->ifindex)) == 0) {
255 fprintf(stderr, "Cannot find device %d\n", p->ifindex);
256 return -1;
257 }
258
259 fprintf(f, "mirred (%s to device %s) %s",
260 mirred_n2a(p->eaction), dev, action_n2a(p->action));
261
262 fprintf(f, "\n ");
263 fprintf(f, "\tindex %d ref %d bind %d", p->index, p->refcnt, p->bindcnt);
264
265 if (show_stats) {
266 if (tb[TCA_MIRRED_TM]) {
267 struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]);
268
269 print_tm(f, tm);
270 }
271 }
272 fprintf(f, "\n ");
273 return 0;
274 }
275
276 struct action_util mirred_action_util = {
277 .id = "mirred",
278 .parse_aopt = parse_mirred,
279 .print_aopt = print_mirred,
280 };