]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_ife.c
tc: fix parsing of the control action
[mirror_iproute2.git] / tc / m_ife.c
CommitLineData
d3e51122
JHS
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>
d3e51122
JHS
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
21#include <linux/netdevice.h>
22
23#include "rt_names.h"
24#include "utils.h"
25#include "tc_util.h"
26#include <linux/tc_act/tc_ife.h>
27
28static void ife_explain(void)
29{
30 fprintf(stderr,
1b600f4b 31 "Usage:... ife {decode|encode} [{ALLOW|USE} ATTR] [dst DMAC] [src SMAC] [type TYPE] [CONTROL] [index INDEX]\n");
d3e51122
JHS
32 fprintf(stderr,
33 "\tALLOW := Encode direction. Allows encoding specified metadata\n"
34 "\t\t e.g \"allow mark\"\n"
35 "\tUSE := Encode direction. Enforce Static encoding of specified metadata\n"
36 "\t\t e.g \"use mark 0x12\"\n"
1b600f4b 37 "\tATTR := mark (32-bit), prio (32-bit), tcindex (16-bit)\n"
d3e51122
JHS
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
47static void ife_usage(void)
48{
49 ife_explain();
50 exit(-1);
51}
52
53static 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;
e67aba55 59 struct tc_ife p = { 0 };
d3e51122
JHS
60 struct rtattr *tail;
61 struct rtattr *tail2;
62 char dbuf[ETH_ALEN];
63 char sbuf[ETH_ALEN];
64 __u16 ife_type = 0;
bf338b60 65 int user_type = 0;
d3e51122
JHS
66 __u32 ife_prio = 0;
67 __u32 ife_prio_v = 0;
68 __u32 ife_mark = 0;
69 __u32 ife_mark_v = 0;
8da6ff35
JHS
70 __u16 ife_tcindex = 0;
71 __u16 ife_tcindex_v = 0;
d3e51122
JHS
72 char *daddr = NULL;
73 char *saddr = NULL;
74
d3e51122
JHS
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;
8da6ff35 94 } else if (matches(*argv, "tcindex") == 0) {
25a24934 95 ife_tcindex = IFE_META_TCINDEX;
d3e51122
JHS
96 } else {
97 fprintf(stderr, "Illegal meta define <%s>\n",
98 *argv);
99 return -1;
100 }
101 } else if (matches(*argv, "use") == 0) {
102 NEXT_ARG();
103 if (matches(*argv, "mark") == 0) {
104 NEXT_ARG();
105 if (get_u32(&ife_mark_v, *argv, 0))
106 invarg("ife mark val is invalid",
107 *argv);
108 } else if (matches(*argv, "prio") == 0) {
109 NEXT_ARG();
110 if (get_u32(&ife_prio_v, *argv, 0))
111 invarg("ife prio val is invalid",
112 *argv);
8da6ff35
JHS
113 } else if (matches(*argv, "tcindex") == 0) {
114 NEXT_ARG();
115 if (get_u16(&ife_tcindex_v, *argv, 0))
116 invarg("ife tcindex val is invalid",
117 *argv);
d3e51122
JHS
118 } else {
119 fprintf(stderr, "Illegal meta use type <%s>\n",
120 *argv);
121 return -1;
122 }
123 } else if (matches(*argv, "type") == 0) {
124 NEXT_ARG();
125 if (get_u16(&ife_type, *argv, 0))
126 invarg("ife type is invalid", *argv);
664f35aa 127 fprintf(stderr, "IFE type 0x%04X\n", ife_type);
bf338b60 128 user_type = 1;
d3e51122
JHS
129 } else if (matches(*argv, "dst") == 0) {
130 NEXT_ARG();
131 daddr = *argv;
132 if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
133 dbuf, dbuf + 1, dbuf + 2,
134 dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
135 fprintf(stderr, "Invalid mac address %s\n",
136 daddr);
137 }
138 fprintf(stderr, "dst MAC address <%s>\n", daddr);
139
140 } else if (matches(*argv, "src") == 0) {
141 NEXT_ARG();
142 saddr = *argv;
143 if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
144 sbuf, sbuf + 1, sbuf + 2,
145 sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
146 fprintf(stderr, "Invalid mac address %s\n",
147 saddr);
148 }
149 fprintf(stderr, "src MAC address <%s>\n", saddr);
150 } else if (matches(*argv, "help") == 0) {
151 ife_usage();
152 } else {
153 break;
154 }
155
156 argc--;
157 argv++;
158 }
159
e67aba55 160 parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
d3e51122
JHS
161
162 if (argc) {
163 if (matches(*argv, "index") == 0) {
164 NEXT_ARG();
9a56cca3 165 if (get_u32(&p.index, *argv, 0)) {
d3e51122
JHS
166 fprintf(stderr, "ife: Illegal \"index\"\n");
167 return -1;
168 }
a78a2dba 169 ok++;
d3e51122
JHS
170 argc--;
171 argv++;
172 }
173 }
174
175 if (!ok) {
176 fprintf(stderr, "IFE requires decode/encode specified\n");
177 ife_usage();
178 }
179
180 tail = NLMSG_TAIL(n);
181 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
182 addattr_l(n, MAX_MSG, TCA_IFE_PARMS, &p, sizeof(p));
183
184 if (!(p.flags & IFE_ENCODE))
185 goto skip_encode;
186
187 if (daddr)
188 addattr_l(n, MAX_MSG, TCA_IFE_DMAC, dbuf, ETH_ALEN);
bf338b60 189 if (user_type)
d3e51122 190 addattr_l(n, MAX_MSG, TCA_IFE_TYPE, &ife_type, 2);
38060de1
AA
191 else
192 fprintf(stderr, "IFE type 0x%04X\n", ETH_P_IFE);
d3e51122
JHS
193 if (saddr)
194 addattr_l(n, MAX_MSG, TCA_IFE_SMAC, sbuf, ETH_ALEN);
195
196 tail2 = NLMSG_TAIL(n);
197 addattr_l(n, MAX_MSG, TCA_IFE_METALST, NULL, 0);
198 if (ife_mark || ife_mark_v) {
199 if (ife_mark_v)
200 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, &ife_mark_v, 4);
201 else
202 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, NULL, 0);
203 }
204 if (ife_prio || ife_prio_v) {
205 if (ife_prio_v)
206 addattr_l(n, MAX_MSG, IFE_META_PRIO, &ife_prio_v, 4);
207 else
208 addattr_l(n, MAX_MSG, IFE_META_PRIO, NULL, 0);
209 }
8da6ff35
JHS
210 if (ife_tcindex || ife_tcindex_v) {
211 if (ife_tcindex_v)
212 addattr_l(n, MAX_MSG, IFE_META_TCINDEX, &ife_tcindex_v,
213 2);
214 else
215 addattr_l(n, MAX_MSG, IFE_META_TCINDEX, NULL, 0);
216 }
d3e51122
JHS
217
218 tail2->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail2;
219
220skip_encode:
221 tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
222
223 *argc_p = argc;
224 *argv_p = argv;
225 return 0;
226}
227
228static int print_ife(struct action_util *au, FILE *f, struct rtattr *arg)
229{
230 struct tc_ife *p = NULL;
231 struct rtattr *tb[TCA_IFE_MAX + 1];
232 __u16 ife_type = 0;
233 __u32 mmark = 0;
8da6ff35 234 __u16 mtcindex = 0;
d3e51122
JHS
235 __u32 mprio = 0;
236 int has_optional = 0;
d3e51122
JHS
237 SPRINT_BUF(b2);
238
239 if (arg == NULL)
240 return -1;
241
242 parse_rtattr_nested(tb, TCA_IFE_MAX, arg);
243
244 if (tb[TCA_IFE_PARMS] == NULL) {
245 fprintf(f, "[NULL ife parameters]");
246 return -1;
247 }
248 p = RTA_DATA(tb[TCA_IFE_PARMS]);
249
e67aba55
JP
250 fprintf(f, "ife %s ", p->flags & IFE_ENCODE ? "encode" : "decode");
251 print_action_control(f, "action ", p->action, " ");
d3e51122
JHS
252
253 if (tb[TCA_IFE_TYPE]) {
254 ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]);
255 has_optional = 1;
256 fprintf(f, "type 0x%X ", ife_type);
257 }
258
259 if (has_optional)
260 fprintf(f, "\n\t ");
261
262 if (tb[TCA_IFE_METALST]) {
263 struct rtattr *metalist[IFE_META_MAX + 1];
264 int len = 0;
265
266 parse_rtattr_nested(metalist, IFE_META_MAX,
267 tb[TCA_IFE_METALST]);
268
269 if (metalist[IFE_META_SKBMARK]) {
270 len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]);
271 if (len) {
272 mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]);
57ee4430 273 fprintf(f, "use mark %u ", mmark);
d3e51122
JHS
274 } else
275 fprintf(f, "allow mark ");
276 }
277
8da6ff35
JHS
278 if (metalist[IFE_META_TCINDEX]) {
279 len = RTA_PAYLOAD(metalist[IFE_META_TCINDEX]);
d3e51122 280 if (len) {
8da6ff35
JHS
281 mtcindex =
282 rta_getattr_u16(metalist[IFE_META_TCINDEX]);
283 fprintf(f, "use tcindex %d ", mtcindex);
d3e51122 284 } else
8da6ff35 285 fprintf(f, "allow tcindex ");
d3e51122
JHS
286 }
287
288 if (metalist[IFE_META_PRIO]) {
289 len = RTA_PAYLOAD(metalist[IFE_META_PRIO]);
290 if (len) {
291 mprio = rta_getattr_u32(metalist[IFE_META_PRIO]);
57ee4430 292 fprintf(f, "use prio %u ", mprio);
d3e51122
JHS
293 } else
294 fprintf(f, "allow prio ");
295 }
296
297 }
298
299 if (tb[TCA_IFE_DMAC]) {
300 has_optional = 1;
301 fprintf(f, "dst %s ",
302 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]),
303 RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2,
304 sizeof(b2)));
305
306 }
307
308 if (tb[TCA_IFE_SMAC]) {
309 has_optional = 1;
310 fprintf(f, "src %s ",
311 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]),
312 RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2,
313 sizeof(b2)));
314 }
315
53075318 316 fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
d3e51122
JHS
317 p->bindcnt);
318 if (show_stats) {
319 if (tb[TCA_IFE_TM]) {
320 struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]);
321
322 print_tm(f, tm);
323 }
324 }
325
326 fprintf(f, "\n");
327
328 return 0;
329}
330
331struct action_util ife_action_util = {
332 .id = "ife",
333 .parse_aopt = parse_ife,
334 .print_aopt = print_ife,
335};