]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_ife.c
fix print_0xhex on 32 bit
[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 96 } else {
0aaf62fc 97 invarg("Illegal meta define", *argv);
d3e51122
JHS
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);
8da6ff35
JHS
111 } else if (matches(*argv, "tcindex") == 0) {
112 NEXT_ARG();
113 if (get_u16(&ife_tcindex_v, *argv, 0))
114 invarg("ife tcindex val is invalid",
115 *argv);
d3e51122 116 } else {
0aaf62fc 117 invarg("Illegal meta use type", *argv);
d3e51122
JHS
118 }
119 } else if (matches(*argv, "type") == 0) {
120 NEXT_ARG();
121 if (get_u16(&ife_type, *argv, 0))
122 invarg("ife type is invalid", *argv);
664f35aa 123 fprintf(stderr, "IFE type 0x%04X\n", ife_type);
bf338b60 124 user_type = 1;
d3e51122
JHS
125 } else if (matches(*argv, "dst") == 0) {
126 NEXT_ARG();
127 daddr = *argv;
128 if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
129 dbuf, dbuf + 1, dbuf + 2,
130 dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
0aaf62fc 131 invarg("Invalid mac address", *argv);
d3e51122
JHS
132 }
133 fprintf(stderr, "dst MAC address <%s>\n", daddr);
134
135 } else if (matches(*argv, "src") == 0) {
136 NEXT_ARG();
137 saddr = *argv;
138 if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
139 sbuf, sbuf + 1, sbuf + 2,
140 sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
0aaf62fc 141 invarg("Invalid mac address", *argv);
d3e51122
JHS
142 }
143 fprintf(stderr, "src MAC address <%s>\n", saddr);
144 } else if (matches(*argv, "help") == 0) {
145 ife_usage();
146 } else {
147 break;
148 }
149
150 argc--;
151 argv++;
152 }
153
e67aba55 154 parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
d3e51122
JHS
155
156 if (argc) {
157 if (matches(*argv, "index") == 0) {
158 NEXT_ARG();
9a56cca3 159 if (get_u32(&p.index, *argv, 0)) {
d3e51122
JHS
160 fprintf(stderr, "ife: Illegal \"index\"\n");
161 return -1;
162 }
a78a2dba 163 ok++;
d3e51122
JHS
164 argc--;
165 argv++;
166 }
167 }
168
169 if (!ok) {
170 fprintf(stderr, "IFE requires decode/encode specified\n");
171 ife_usage();
172 }
173
c14f9d92 174 tail = addattr_nest(n, MAX_MSG, tca_id);
d3e51122
JHS
175 addattr_l(n, MAX_MSG, TCA_IFE_PARMS, &p, sizeof(p));
176
177 if (!(p.flags & IFE_ENCODE))
178 goto skip_encode;
179
180 if (daddr)
181 addattr_l(n, MAX_MSG, TCA_IFE_DMAC, dbuf, ETH_ALEN);
bf338b60 182 if (user_type)
d3e51122 183 addattr_l(n, MAX_MSG, TCA_IFE_TYPE, &ife_type, 2);
38060de1
AA
184 else
185 fprintf(stderr, "IFE type 0x%04X\n", ETH_P_IFE);
d3e51122
JHS
186 if (saddr)
187 addattr_l(n, MAX_MSG, TCA_IFE_SMAC, sbuf, ETH_ALEN);
188
c14f9d92 189 tail2 = addattr_nest(n, MAX_MSG, TCA_IFE_METALST);
d3e51122
JHS
190 if (ife_mark || ife_mark_v) {
191 if (ife_mark_v)
192 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, &ife_mark_v, 4);
193 else
194 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, NULL, 0);
195 }
196 if (ife_prio || ife_prio_v) {
197 if (ife_prio_v)
198 addattr_l(n, MAX_MSG, IFE_META_PRIO, &ife_prio_v, 4);
199 else
200 addattr_l(n, MAX_MSG, IFE_META_PRIO, NULL, 0);
201 }
8da6ff35
JHS
202 if (ife_tcindex || ife_tcindex_v) {
203 if (ife_tcindex_v)
204 addattr_l(n, MAX_MSG, IFE_META_TCINDEX, &ife_tcindex_v,
205 2);
206 else
207 addattr_l(n, MAX_MSG, IFE_META_TCINDEX, NULL, 0);
208 }
d3e51122 209
c14f9d92 210 addattr_nest_end(n, tail2);
d3e51122
JHS
211
212skip_encode:
c14f9d92 213 addattr_nest_end(n, tail);
d3e51122
JHS
214
215 *argc_p = argc;
216 *argv_p = argv;
217 return 0;
218}
219
220static int print_ife(struct action_util *au, FILE *f, struct rtattr *arg)
221{
222 struct tc_ife *p = NULL;
223 struct rtattr *tb[TCA_IFE_MAX + 1];
224 __u16 ife_type = 0;
225 __u32 mmark = 0;
8da6ff35 226 __u16 mtcindex = 0;
d3e51122
JHS
227 __u32 mprio = 0;
228 int has_optional = 0;
d3e51122
JHS
229 SPRINT_BUF(b2);
230
231 if (arg == NULL)
232 return -1;
233
234 parse_rtattr_nested(tb, TCA_IFE_MAX, arg);
235
236 if (tb[TCA_IFE_PARMS] == NULL) {
8744c5d3 237 print_string(PRINT_FP, NULL, "%s", "[NULL ife parameters]");
d3e51122
JHS
238 return -1;
239 }
240 p = RTA_DATA(tb[TCA_IFE_PARMS]);
241
8744c5d3 242 print_string(PRINT_ANY, "kind", "%s ", "ife");
53d34eb6 243 print_string(PRINT_ANY, "mode", "%s ",
8744c5d3 244 p->flags & IFE_ENCODE ? "encode" : "decode");
e67aba55 245 print_action_control(f, "action ", p->action, " ");
d3e51122
JHS
246
247 if (tb[TCA_IFE_TYPE]) {
248 ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]);
249 has_optional = 1;
90c5c969 250 print_0xhex(PRINT_ANY, "type", "type %#llX ", ife_type);
d3e51122
JHS
251 }
252
253 if (has_optional)
8744c5d3 254 print_string(PRINT_FP, NULL, "%s\t", _SL_);
d3e51122
JHS
255
256 if (tb[TCA_IFE_METALST]) {
257 struct rtattr *metalist[IFE_META_MAX + 1];
258 int len = 0;
259
260 parse_rtattr_nested(metalist, IFE_META_MAX,
261 tb[TCA_IFE_METALST]);
262
263 if (metalist[IFE_META_SKBMARK]) {
264 len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]);
265 if (len) {
266 mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]);
8744c5d3
RM
267 print_uint(PRINT_ANY, "mark", "use mark %u ",
268 mmark);
d3e51122 269 } else
8744c5d3
RM
270 print_string(PRINT_ANY, "mark", "%s mark ",
271 "allow");
d3e51122
JHS
272 }
273
8da6ff35
JHS
274 if (metalist[IFE_META_TCINDEX]) {
275 len = RTA_PAYLOAD(metalist[IFE_META_TCINDEX]);
d3e51122 276 if (len) {
8da6ff35
JHS
277 mtcindex =
278 rta_getattr_u16(metalist[IFE_META_TCINDEX]);
8744c5d3
RM
279 print_uint(PRINT_ANY, "tcindex",
280 "use tcindex %u ", mtcindex);
d3e51122 281 } else
8744c5d3
RM
282 print_string(PRINT_ANY, "tcindex",
283 "%s tcindex ", "allow");
d3e51122
JHS
284 }
285
286 if (metalist[IFE_META_PRIO]) {
287 len = RTA_PAYLOAD(metalist[IFE_META_PRIO]);
288 if (len) {
289 mprio = rta_getattr_u32(metalist[IFE_META_PRIO]);
8744c5d3
RM
290 print_uint(PRINT_ANY, "prio", "use prio %u ",
291 mprio);
d3e51122 292 } else
8744c5d3
RM
293 print_string(PRINT_ANY, "prio", "%s prio ",
294 "allow");
d3e51122
JHS
295 }
296
297 }
298
299 if (tb[TCA_IFE_DMAC]) {
300 has_optional = 1;
8744c5d3
RM
301 print_string(PRINT_ANY, "dst", "dst %s ",
302 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]),
303 RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2,
304 sizeof(b2)));
d3e51122
JHS
305 }
306
307 if (tb[TCA_IFE_SMAC]) {
308 has_optional = 1;
8744c5d3
RM
309 print_string(PRINT_ANY, "src", "src %s ",
310 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]),
311 RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2,
312 sizeof(b2)));
d3e51122
JHS
313 }
314
8744c5d3
RM
315 print_string(PRINT_FP, NULL, "%s", _SL_);
316 print_uint(PRINT_ANY, "index", "\t index %u", p->index);
317 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
318 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
319
d3e51122
JHS
320 if (show_stats) {
321 if (tb[TCA_IFE_TM]) {
322 struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]);
323
324 print_tm(f, tm);
325 }
326 }
327
8744c5d3 328 print_string(PRINT_FP, NULL, "%s", _SL_);
d3e51122
JHS
329
330 return 0;
331}
332
333struct action_util ife_action_util = {
334 .id = "ife",
335 .parse_aopt = parse_ife,
336 .print_aopt = print_ife,
337};