2 * m_ife.c IFE actions module
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.
9 * Authors: J Hadi Salim (jhs@mojatatu.com)
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
21 #include <linux/netdevice.h>
26 #include <linux/tc_act/tc_ife.h>
28 static void ife_explain(void)
31 "Usage:... ife {decode|encode} [{ALLOW|USE} ATTR] [dst DMAC] [src SMAC] [type TYPE] [CONTROL] [index INDEX]\n"
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"
36 "\tATTR := mark (32-bit), prio (32-bit), tcindex (16-bit)\n"
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"
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");
46 static void ife_usage(void)
52 static int parse_ife(struct action_util
*a
, int *argc_p
, char ***argv_p
,
53 int tca_id
, struct nlmsghdr
*n
)
56 char **argv
= *argv_p
;
58 struct tc_ife p
= { 0 };
69 __u16 ife_tcindex
= 0;
70 __u16 ife_tcindex_v
= 0;
78 if (matches(*argv
, "ife") == 0) {
81 } else if (matches(*argv
, "decode") == 0) {
82 p
.flags
= IFE_DECODE
; /* readability aid */
84 } else if (matches(*argv
, "encode") == 0) {
87 } else if (matches(*argv
, "allow") == 0) {
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
;
93 } else if (matches(*argv
, "tcindex") == 0) {
94 ife_tcindex
= IFE_META_TCINDEX
;
96 invarg("Illegal meta define", *argv
);
98 } else if (matches(*argv
, "use") == 0) {
100 if (matches(*argv
, "mark") == 0) {
102 if (get_u32(&ife_mark_v
, *argv
, 0))
103 invarg("ife mark val is invalid",
105 } else if (matches(*argv
, "prio") == 0) {
107 if (get_u32(&ife_prio_v
, *argv
, 0))
108 invarg("ife prio val is invalid",
110 } else if (matches(*argv
, "tcindex") == 0) {
112 if (get_u16(&ife_tcindex_v
, *argv
, 0))
113 invarg("ife tcindex val is invalid",
116 invarg("Illegal meta use type", *argv
);
118 } else if (matches(*argv
, "type") == 0) {
120 if (get_u16(&ife_type
, *argv
, 0))
121 invarg("ife type is invalid", *argv
);
122 fprintf(stderr
, "IFE type 0x%04X\n", ife_type
);
124 } else if (matches(*argv
, "dst") == 0) {
127 if (sscanf(daddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
128 dbuf
, dbuf
+ 1, dbuf
+ 2,
129 dbuf
+ 3, dbuf
+ 4, dbuf
+ 5) != 6) {
130 invarg("Invalid mac address", *argv
);
132 fprintf(stderr
, "dst MAC address <%s>\n", daddr
);
134 } else if (matches(*argv
, "src") == 0) {
137 if (sscanf(saddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
138 sbuf
, sbuf
+ 1, sbuf
+ 2,
139 sbuf
+ 3, sbuf
+ 4, sbuf
+ 5) != 6) {
140 invarg("Invalid mac address", *argv
);
142 fprintf(stderr
, "src MAC address <%s>\n", saddr
);
143 } else if (matches(*argv
, "help") == 0) {
153 parse_action_control_dflt(&argc
, &argv
, &p
.action
, false, TC_ACT_PIPE
);
156 if (matches(*argv
, "index") == 0) {
158 if (get_u32(&p
.index
, *argv
, 0)) {
159 fprintf(stderr
, "ife: Illegal \"index\"\n");
169 fprintf(stderr
, "IFE requires decode/encode specified\n");
173 tail
= addattr_nest(n
, MAX_MSG
, tca_id
);
174 addattr_l(n
, MAX_MSG
, TCA_IFE_PARMS
, &p
, sizeof(p
));
176 if (!(p
.flags
& IFE_ENCODE
))
180 addattr_l(n
, MAX_MSG
, TCA_IFE_DMAC
, dbuf
, ETH_ALEN
);
182 addattr_l(n
, MAX_MSG
, TCA_IFE_TYPE
, &ife_type
, 2);
184 fprintf(stderr
, "IFE type 0x%04X\n", ETH_P_IFE
);
186 addattr_l(n
, MAX_MSG
, TCA_IFE_SMAC
, sbuf
, ETH_ALEN
);
188 tail2
= addattr_nest(n
, MAX_MSG
, TCA_IFE_METALST
);
189 if (ife_mark
|| ife_mark_v
) {
191 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, &ife_mark_v
, 4);
193 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, NULL
, 0);
195 if (ife_prio
|| ife_prio_v
) {
197 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, &ife_prio_v
, 4);
199 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, NULL
, 0);
201 if (ife_tcindex
|| ife_tcindex_v
) {
203 addattr_l(n
, MAX_MSG
, IFE_META_TCINDEX
, &ife_tcindex_v
,
206 addattr_l(n
, MAX_MSG
, IFE_META_TCINDEX
, NULL
, 0);
209 addattr_nest_end(n
, tail2
);
212 addattr_nest_end(n
, tail
);
219 static int print_ife(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
221 struct tc_ife
*p
= NULL
;
222 struct rtattr
*tb
[TCA_IFE_MAX
+ 1];
227 int has_optional
= 0;
233 parse_rtattr_nested(tb
, TCA_IFE_MAX
, arg
);
235 if (tb
[TCA_IFE_PARMS
] == NULL
) {
236 print_string(PRINT_FP
, NULL
, "%s", "[NULL ife parameters]");
239 p
= RTA_DATA(tb
[TCA_IFE_PARMS
]);
241 print_string(PRINT_ANY
, "kind", "%s ", "ife");
242 print_string(PRINT_ANY
, "mode", "%s ",
243 p
->flags
& IFE_ENCODE
? "encode" : "decode");
244 print_action_control(f
, "action ", p
->action
, " ");
246 if (tb
[TCA_IFE_TYPE
]) {
247 ife_type
= rta_getattr_u16(tb
[TCA_IFE_TYPE
]);
249 print_0xhex(PRINT_ANY
, "type", "type %#llX ", ife_type
);
253 print_string(PRINT_FP
, NULL
, "%s\t", _SL_
);
255 if (tb
[TCA_IFE_METALST
]) {
256 struct rtattr
*metalist
[IFE_META_MAX
+ 1];
259 parse_rtattr_nested(metalist
, IFE_META_MAX
,
260 tb
[TCA_IFE_METALST
]);
262 if (metalist
[IFE_META_SKBMARK
]) {
263 len
= RTA_PAYLOAD(metalist
[IFE_META_SKBMARK
]);
265 mmark
= rta_getattr_u32(metalist
[IFE_META_SKBMARK
]);
266 print_uint(PRINT_ANY
, "mark", "use mark %u ",
269 print_string(PRINT_ANY
, "mark", "%s mark ",
273 if (metalist
[IFE_META_TCINDEX
]) {
274 len
= RTA_PAYLOAD(metalist
[IFE_META_TCINDEX
]);
277 rta_getattr_u16(metalist
[IFE_META_TCINDEX
]);
278 print_uint(PRINT_ANY
, "tcindex",
279 "use tcindex %u ", mtcindex
);
281 print_string(PRINT_ANY
, "tcindex",
282 "%s tcindex ", "allow");
285 if (metalist
[IFE_META_PRIO
]) {
286 len
= RTA_PAYLOAD(metalist
[IFE_META_PRIO
]);
288 mprio
= rta_getattr_u32(metalist
[IFE_META_PRIO
]);
289 print_uint(PRINT_ANY
, "prio", "use prio %u ",
292 print_string(PRINT_ANY
, "prio", "%s prio ",
298 if (tb
[TCA_IFE_DMAC
]) {
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
,
306 if (tb
[TCA_IFE_SMAC
]) {
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
,
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
);
320 if (tb
[TCA_IFE_TM
]) {
321 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IFE_TM
]);
327 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
332 struct action_util ife_action_util
= {
334 .parse_aopt
= parse_ife
,
335 .print_aopt
= print_ife
,