]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_ife.c
Merge branch 'master' into net-next
[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} ATTR] [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 "\tATTR := mark (32-bit), prio (32-bit), tcindex (16-bit)\n"
39 "\tDMAC := 6 byte Destination MAC address to encode\n"
40 "\tSMAC := optional 6 byte Source MAC address to encode\n"
41 "\tTYPE := optional 16 bit ethertype to encode\n"
42 "\tCONTROL := reclassify|pipe|drop|continue|ok\n"
43 "\tINDEX := optional IFE table index value used\n");
44 fprintf(stderr, "encode is used for sending IFE packets\n");
45 fprintf(stderr, "decode is used for receiving IFE packets\n");
46 }
47
48 static void ife_usage(void)
49 {
50 ife_explain();
51 exit(-1);
52 }
53
54 static int parse_ife(struct action_util *a, int *argc_p, char ***argv_p,
55 int tca_id, struct nlmsghdr *n)
56 {
57 int argc = *argc_p;
58 char **argv = *argv_p;
59 int ok = 0;
60 struct tc_ife p = { 0 };
61 struct rtattr *tail;
62 struct rtattr *tail2;
63 char dbuf[ETH_ALEN];
64 char sbuf[ETH_ALEN];
65 __u16 ife_type = 0;
66 __u32 ife_prio = 0;
67 __u32 ife_prio_v = 0;
68 __u32 ife_mark = 0;
69 __u32 ife_mark_v = 0;
70 __u16 ife_tcindex = 0;
71 __u16 ife_tcindex_v = 0;
72 char *daddr = NULL;
73 char *saddr = NULL;
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 if (matches(*argv, "tcindex") == 0) {
95 ife_prio = IFE_META_TCINDEX;
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);
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);
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);
127 fprintf(stderr, "IFE type 0x%x\n", ife_type);
128 } else if (matches(*argv, "dst") == 0) {
129 NEXT_ARG();
130 daddr = *argv;
131 if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
132 dbuf, dbuf + 1, dbuf + 2,
133 dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
134 fprintf(stderr, "Invalid mac address %s\n",
135 daddr);
136 }
137 fprintf(stderr, "dst MAC address <%s>\n", daddr);
138
139 } else if (matches(*argv, "src") == 0) {
140 NEXT_ARG();
141 saddr = *argv;
142 if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
143 sbuf, sbuf + 1, sbuf + 2,
144 sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
145 fprintf(stderr, "Invalid mac address %s\n",
146 saddr);
147 }
148 fprintf(stderr, "src MAC address <%s>\n", saddr);
149 } else if (matches(*argv, "help") == 0) {
150 ife_usage();
151 } else {
152 break;
153 }
154
155 argc--;
156 argv++;
157 }
158
159 parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
160
161 if (argc) {
162 if (matches(*argv, "index") == 0) {
163 NEXT_ARG();
164 if (get_u32(&p.index, *argv, 0)) {
165 fprintf(stderr, "ife: Illegal \"index\"\n");
166 return -1;
167 }
168 ok++;
169 argc--;
170 argv++;
171 }
172 }
173
174 if (!ok) {
175 fprintf(stderr, "IFE requires decode/encode specified\n");
176 ife_usage();
177 }
178
179 tail = NLMSG_TAIL(n);
180 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
181 addattr_l(n, MAX_MSG, TCA_IFE_PARMS, &p, sizeof(p));
182
183 if (!(p.flags & IFE_ENCODE))
184 goto skip_encode;
185
186 if (daddr)
187 addattr_l(n, MAX_MSG, TCA_IFE_DMAC, dbuf, ETH_ALEN);
188 if (ife_type)
189 addattr_l(n, MAX_MSG, TCA_IFE_TYPE, &ife_type, 2);
190 if (saddr)
191 addattr_l(n, MAX_MSG, TCA_IFE_SMAC, sbuf, ETH_ALEN);
192
193 tail2 = NLMSG_TAIL(n);
194 addattr_l(n, MAX_MSG, TCA_IFE_METALST, NULL, 0);
195 if (ife_mark || ife_mark_v) {
196 if (ife_mark_v)
197 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, &ife_mark_v, 4);
198 else
199 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, NULL, 0);
200 }
201 if (ife_prio || ife_prio_v) {
202 if (ife_prio_v)
203 addattr_l(n, MAX_MSG, IFE_META_PRIO, &ife_prio_v, 4);
204 else
205 addattr_l(n, MAX_MSG, IFE_META_PRIO, NULL, 0);
206 }
207 if (ife_tcindex || ife_tcindex_v) {
208 if (ife_tcindex_v)
209 addattr_l(n, MAX_MSG, IFE_META_TCINDEX, &ife_tcindex_v,
210 2);
211 else
212 addattr_l(n, MAX_MSG, IFE_META_TCINDEX, NULL, 0);
213 }
214
215 tail2->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail2;
216
217 skip_encode:
218 tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
219
220 *argc_p = argc;
221 *argv_p = argv;
222 return 0;
223 }
224
225 static int print_ife(struct action_util *au, FILE *f, struct rtattr *arg)
226 {
227 struct tc_ife *p = NULL;
228 struct rtattr *tb[TCA_IFE_MAX + 1];
229 __u16 ife_type = 0;
230 __u32 mmark = 0;
231 __u16 mtcindex = 0;
232 __u32 mprio = 0;
233 int has_optional = 0;
234 SPRINT_BUF(b2);
235
236 if (arg == NULL)
237 return -1;
238
239 parse_rtattr_nested(tb, TCA_IFE_MAX, arg);
240
241 if (tb[TCA_IFE_PARMS] == NULL) {
242 fprintf(f, "[NULL ife parameters]");
243 return -1;
244 }
245 p = RTA_DATA(tb[TCA_IFE_PARMS]);
246
247 fprintf(f, "ife %s ", p->flags & IFE_ENCODE ? "encode" : "decode");
248 print_action_control(f, "action ", p->action, " ");
249
250 if (tb[TCA_IFE_TYPE]) {
251 ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]);
252 has_optional = 1;
253 fprintf(f, "type 0x%X ", ife_type);
254 }
255
256 if (has_optional)
257 fprintf(f, "\n\t ");
258
259 if (tb[TCA_IFE_METALST]) {
260 struct rtattr *metalist[IFE_META_MAX + 1];
261 int len = 0;
262
263 parse_rtattr_nested(metalist, IFE_META_MAX,
264 tb[TCA_IFE_METALST]);
265
266 if (metalist[IFE_META_SKBMARK]) {
267 len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]);
268 if (len) {
269 mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]);
270 fprintf(f, "use mark %u ", mmark);
271 } else
272 fprintf(f, "allow mark ");
273 }
274
275 if (metalist[IFE_META_TCINDEX]) {
276 len = RTA_PAYLOAD(metalist[IFE_META_TCINDEX]);
277 if (len) {
278 mtcindex =
279 rta_getattr_u16(metalist[IFE_META_TCINDEX]);
280 fprintf(f, "use tcindex %d ", mtcindex);
281 } else
282 fprintf(f, "allow tcindex ");
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]);
289 fprintf(f, "use prio %u ", mprio);
290 } else
291 fprintf(f, "allow prio ");
292 }
293
294 }
295
296 if (tb[TCA_IFE_DMAC]) {
297 has_optional = 1;
298 fprintf(f, "dst %s ",
299 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]),
300 RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2,
301 sizeof(b2)));
302
303 }
304
305 if (tb[TCA_IFE_SMAC]) {
306 has_optional = 1;
307 fprintf(f, "src %s ",
308 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]),
309 RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2,
310 sizeof(b2)));
311 }
312
313 fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
314 p->bindcnt);
315 if (show_stats) {
316 if (tb[TCA_IFE_TM]) {
317 struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]);
318
319 print_tm(f, tm);
320 }
321 }
322
323 fprintf(f, "\n");
324
325 return 0;
326 }
327
328 struct action_util ife_action_util = {
329 .id = "ife",
330 .parse_aopt = parse_ife,
331 .print_aopt = print_ife,
332 };