]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_ife.c
tc: don't ignore ok as an action branch
[mirror_iproute2.git] / tc / m_ife.c
1 /*
2 * m_ife.c IFE 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 (jhs@mojatatu.com)
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <linux/netdevice.h>
23
24 #include "rt_names.h"
25 #include "utils.h"
26 #include "tc_util.h"
27 #include <linux/tc_act/tc_ife.h>
28
29 static void ife_explain(void)
30 {
31 fprintf(stderr,
32 "Usage:... ife {decode|encode} {ALLOW|USE} [dst DMAC] [src SMAC] [type TYPE] [CONTROL] [index INDEX]\n");
33 fprintf(stderr,
34 "\tALLOW := Encode direction. Allows encoding specified metadata\n"
35 "\t\t e.g \"allow mark\"\n"
36 "\tUSE := Encode direction. Enforce Static encoding of specified metadata\n"
37 "\t\t e.g \"use mark 0x12\"\n"
38 "\tDMAC := 6 byte Destination MAC address to encode\n"
39 "\tSMAC := optional 6 byte Source MAC address to encode\n"
40 "\tTYPE := optional 16 bit ethertype to encode\n"
41 "\tCONTROL := reclassify|pipe|drop|continue|ok\n"
42 "\tINDEX := optional IFE table index value used\n");
43 fprintf(stderr, "encode is used for sending IFE packets\n");
44 fprintf(stderr, "decode is used for receiving IFE packets\n");
45 }
46
47 static void ife_usage(void)
48 {
49 ife_explain();
50 exit(-1);
51 }
52
53 static int parse_ife(struct action_util *a, int *argc_p, char ***argv_p,
54 int tca_id, struct nlmsghdr *n)
55 {
56 int argc = *argc_p;
57 char **argv = *argv_p;
58 int ok = 0;
59 struct tc_ife p;
60 struct rtattr *tail;
61 struct rtattr *tail2;
62 char dbuf[ETH_ALEN];
63 char sbuf[ETH_ALEN];
64 __u16 ife_type = 0;
65 __u32 ife_prio = 0;
66 __u32 ife_prio_v = 0;
67 __u32 ife_mark = 0;
68 __u32 ife_mark_v = 0;
69 char *daddr = NULL;
70 char *saddr = NULL;
71
72 memset(&p, 0, sizeof(p));
73 p.action = TC_ACT_PIPE; /* good default */
74
75 if (argc <= 0)
76 return -1;
77
78 while (argc > 0) {
79 if (matches(*argv, "ife") == 0) {
80 NEXT_ARG();
81 continue;
82 } else if (matches(*argv, "decode") == 0) {
83 p.flags = IFE_DECODE; /* readability aid */
84 ok++;
85 } else if (matches(*argv, "encode") == 0) {
86 p.flags = IFE_ENCODE;
87 ok++;
88 } else if (matches(*argv, "allow") == 0) {
89 NEXT_ARG();
90 if (matches(*argv, "mark") == 0) {
91 ife_mark = IFE_META_SKBMARK;
92 } else if (matches(*argv, "prio") == 0) {
93 ife_prio = IFE_META_PRIO;
94 } else {
95 fprintf(stderr, "Illegal meta define <%s>\n",
96 *argv);
97 return -1;
98 }
99 } else if (matches(*argv, "use") == 0) {
100 NEXT_ARG();
101 if (matches(*argv, "mark") == 0) {
102 NEXT_ARG();
103 if (get_u32(&ife_mark_v, *argv, 0))
104 invarg("ife mark val is invalid",
105 *argv);
106 } else if (matches(*argv, "prio") == 0) {
107 NEXT_ARG();
108 if (get_u32(&ife_prio_v, *argv, 0))
109 invarg("ife prio val is invalid",
110 *argv);
111 } else {
112 fprintf(stderr, "Illegal meta use type <%s>\n",
113 *argv);
114 return -1;
115 }
116 } else if (matches(*argv, "type") == 0) {
117 NEXT_ARG();
118 if (get_u16(&ife_type, *argv, 0))
119 invarg("ife type is invalid", *argv);
120 fprintf(stderr, "IFE type 0x%x\n", ife_type);
121 } else if (matches(*argv, "dst") == 0) {
122 NEXT_ARG();
123 daddr = *argv;
124 if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
125 dbuf, dbuf + 1, dbuf + 2,
126 dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
127 fprintf(stderr, "Invalid mac address %s\n",
128 daddr);
129 }
130 fprintf(stderr, "dst MAC address <%s>\n", daddr);
131
132 } else if (matches(*argv, "src") == 0) {
133 NEXT_ARG();
134 saddr = *argv;
135 if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
136 sbuf, sbuf + 1, sbuf + 2,
137 sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
138 fprintf(stderr, "Invalid mac address %s\n",
139 saddr);
140 }
141 fprintf(stderr, "src MAC address <%s>\n", saddr);
142 } else if (matches(*argv, "help") == 0) {
143 ife_usage();
144 } else {
145 break;
146 }
147
148 argc--;
149 argv++;
150 }
151
152 if (argc) {
153 if (matches(*argv, "reclassify") == 0) {
154 p.action = TC_ACT_RECLASSIFY;
155 argc--;
156 argv++;
157 } else if (matches(*argv, "pipe") == 0) {
158 p.action = TC_ACT_PIPE;
159 argc--;
160 argv++;
161 } else if (matches(*argv, "drop") == 0 ||
162 matches(*argv, "shot") == 0) {
163 p.action = TC_ACT_SHOT;
164 argc--;
165 argv++;
166 } else if (matches(*argv, "continue") == 0) {
167 p.action = TC_ACT_UNSPEC;
168 argc--;
169 argv++;
170 } else if (matches(*argv, "pass") == 0 ||
171 matches(*argv, "ok") == 0) {
172 p.action = TC_ACT_OK;
173 argc--;
174 argv++;
175 }
176 }
177
178 if (argc) {
179 if (matches(*argv, "index") == 0) {
180 NEXT_ARG();
181 if (get_u32(&p.index, *argv, 10)) {
182 fprintf(stderr, "ife: Illegal \"index\"\n");
183 return -1;
184 }
185 argc--;
186 argv++;
187 }
188 }
189
190 if (!ok) {
191 fprintf(stderr, "IFE requires decode/encode specified\n");
192 ife_usage();
193 }
194
195 tail = NLMSG_TAIL(n);
196 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
197 addattr_l(n, MAX_MSG, TCA_IFE_PARMS, &p, sizeof(p));
198
199 if (!(p.flags & IFE_ENCODE))
200 goto skip_encode;
201
202 if (daddr)
203 addattr_l(n, MAX_MSG, TCA_IFE_DMAC, dbuf, ETH_ALEN);
204 if (ife_type)
205 addattr_l(n, MAX_MSG, TCA_IFE_TYPE, &ife_type, 2);
206 if (saddr)
207 addattr_l(n, MAX_MSG, TCA_IFE_SMAC, sbuf, ETH_ALEN);
208
209 tail2 = NLMSG_TAIL(n);
210 addattr_l(n, MAX_MSG, TCA_IFE_METALST, NULL, 0);
211 if (ife_mark || ife_mark_v) {
212 if (ife_mark_v)
213 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, &ife_mark_v, 4);
214 else
215 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, NULL, 0);
216 }
217 if (ife_prio || ife_prio_v) {
218 if (ife_prio_v)
219 addattr_l(n, MAX_MSG, IFE_META_PRIO, &ife_prio_v, 4);
220 else
221 addattr_l(n, MAX_MSG, IFE_META_PRIO, NULL, 0);
222 }
223
224 tail2->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail2;
225
226 skip_encode:
227 tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
228
229 *argc_p = argc;
230 *argv_p = argv;
231 return 0;
232 }
233
234 static int print_ife(struct action_util *au, FILE *f, struct rtattr *arg)
235 {
236 struct tc_ife *p = NULL;
237 struct rtattr *tb[TCA_IFE_MAX + 1];
238 __u16 ife_type = 0;
239 __u32 mmark = 0;
240 __u32 mhash = 0;
241 __u32 mprio = 0;
242 int has_optional = 0;
243 SPRINT_BUF(b1);
244 SPRINT_BUF(b2);
245
246 if (arg == NULL)
247 return -1;
248
249 parse_rtattr_nested(tb, TCA_IFE_MAX, arg);
250
251 if (tb[TCA_IFE_PARMS] == NULL) {
252 fprintf(f, "[NULL ife parameters]");
253 return -1;
254 }
255 p = RTA_DATA(tb[TCA_IFE_PARMS]);
256
257 fprintf(f, "ife %s action %s ",
258 (p->flags & IFE_ENCODE) ? "encode" : "decode",
259 action_n2a(p->action, b1, sizeof(b1)));
260
261 if (tb[TCA_IFE_TYPE]) {
262 ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]);
263 has_optional = 1;
264 fprintf(f, "type 0x%X ", ife_type);
265 }
266
267 if (has_optional)
268 fprintf(f, "\n\t ");
269
270 if (tb[TCA_IFE_METALST]) {
271 struct rtattr *metalist[IFE_META_MAX + 1];
272 int len = 0;
273
274 parse_rtattr_nested(metalist, IFE_META_MAX,
275 tb[TCA_IFE_METALST]);
276
277 if (metalist[IFE_META_SKBMARK]) {
278 len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]);
279 if (len) {
280 mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]);
281 fprintf(f, "use mark %d ", mmark);
282 } else
283 fprintf(f, "allow mark ");
284 }
285
286 if (metalist[IFE_META_HASHID]) {
287 len = RTA_PAYLOAD(metalist[IFE_META_HASHID]);
288 if (len) {
289 mhash = rta_getattr_u32(metalist[IFE_META_HASHID]);
290 fprintf(f, "use hash %d ", mhash);
291 } else
292 fprintf(f, "allow hash ");
293 }
294
295 if (metalist[IFE_META_PRIO]) {
296 len = RTA_PAYLOAD(metalist[IFE_META_PRIO]);
297 if (len) {
298 mprio = rta_getattr_u32(metalist[IFE_META_PRIO]);
299 fprintf(f, "use prio %d ", mprio);
300 } else
301 fprintf(f, "allow prio ");
302 }
303
304 }
305
306 if (tb[TCA_IFE_DMAC]) {
307 has_optional = 1;
308 fprintf(f, "dst %s ",
309 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]),
310 RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2,
311 sizeof(b2)));
312
313 }
314
315 if (tb[TCA_IFE_SMAC]) {
316 has_optional = 1;
317 fprintf(f, "src %s ",
318 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]),
319 RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2,
320 sizeof(b2)));
321 }
322
323 fprintf(f, "\n\t index %d ref %d bind %d", p->index, p->refcnt,
324 p->bindcnt);
325 if (show_stats) {
326 if (tb[TCA_IFE_TM]) {
327 struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]);
328
329 print_tm(f, tm);
330 }
331 }
332
333 fprintf(f, "\n");
334
335 return 0;
336 }
337
338 struct action_util ife_action_util = {
339 .id = "ife",
340 .parse_aopt = parse_ife,
341 .print_aopt = print_ife,
342 };