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