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");
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"
37 "\tATTR := mark (32-bit), prio (32-bit), tcindex (16-bit)\n"
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");
47 static void ife_usage(void)
53 static int parse_ife(struct action_util
*a
, int *argc_p
, char ***argv_p
,
54 int tca_id
, struct nlmsghdr
*n
)
57 char **argv
= *argv_p
;
59 struct tc_ife p
= { 0 };
70 __u16 ife_tcindex
= 0;
71 __u16 ife_tcindex_v
= 0;
79 if (matches(*argv
, "ife") == 0) {
82 } else if (matches(*argv
, "decode") == 0) {
83 p
.flags
= IFE_DECODE
; /* readability aid */
85 } else if (matches(*argv
, "encode") == 0) {
88 } else if (matches(*argv
, "allow") == 0) {
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_tcindex
= IFE_META_TCINDEX
;
97 invarg("Illegal meta define", *argv
);
99 } else if (matches(*argv
, "use") == 0) {
101 if (matches(*argv
, "mark") == 0) {
103 if (get_u32(&ife_mark_v
, *argv
, 0))
104 invarg("ife mark val is invalid",
106 } else if (matches(*argv
, "prio") == 0) {
108 if (get_u32(&ife_prio_v
, *argv
, 0))
109 invarg("ife prio val is invalid",
111 } else if (matches(*argv
, "tcindex") == 0) {
113 if (get_u16(&ife_tcindex_v
, *argv
, 0))
114 invarg("ife tcindex val is invalid",
117 invarg("Illegal meta use type", *argv
);
119 } else if (matches(*argv
, "type") == 0) {
121 if (get_u16(&ife_type
, *argv
, 0))
122 invarg("ife type is invalid", *argv
);
123 fprintf(stderr
, "IFE type 0x%04X\n", ife_type
);
125 } else if (matches(*argv
, "dst") == 0) {
128 if (sscanf(daddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
129 dbuf
, dbuf
+ 1, dbuf
+ 2,
130 dbuf
+ 3, dbuf
+ 4, dbuf
+ 5) != 6) {
131 invarg("Invalid mac address", *argv
);
133 fprintf(stderr
, "dst MAC address <%s>\n", daddr
);
135 } else if (matches(*argv
, "src") == 0) {
138 if (sscanf(saddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
139 sbuf
, sbuf
+ 1, sbuf
+ 2,
140 sbuf
+ 3, sbuf
+ 4, sbuf
+ 5) != 6) {
141 invarg("Invalid mac address", *argv
);
143 fprintf(stderr
, "src MAC address <%s>\n", saddr
);
144 } else if (matches(*argv
, "help") == 0) {
154 parse_action_control_dflt(&argc
, &argv
, &p
.action
, false, TC_ACT_PIPE
);
157 if (matches(*argv
, "index") == 0) {
159 if (get_u32(&p
.index
, *argv
, 0)) {
160 fprintf(stderr
, "ife: Illegal \"index\"\n");
170 fprintf(stderr
, "IFE requires decode/encode specified\n");
174 tail
= addattr_nest(n
, MAX_MSG
, tca_id
);
175 addattr_l(n
, MAX_MSG
, TCA_IFE_PARMS
, &p
, sizeof(p
));
177 if (!(p
.flags
& IFE_ENCODE
))
181 addattr_l(n
, MAX_MSG
, TCA_IFE_DMAC
, dbuf
, ETH_ALEN
);
183 addattr_l(n
, MAX_MSG
, TCA_IFE_TYPE
, &ife_type
, 2);
185 fprintf(stderr
, "IFE type 0x%04X\n", ETH_P_IFE
);
187 addattr_l(n
, MAX_MSG
, TCA_IFE_SMAC
, sbuf
, ETH_ALEN
);
189 tail2
= addattr_nest(n
, MAX_MSG
, TCA_IFE_METALST
);
190 if (ife_mark
|| ife_mark_v
) {
192 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, &ife_mark_v
, 4);
194 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, NULL
, 0);
196 if (ife_prio
|| ife_prio_v
) {
198 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, &ife_prio_v
, 4);
200 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, NULL
, 0);
202 if (ife_tcindex
|| ife_tcindex_v
) {
204 addattr_l(n
, MAX_MSG
, IFE_META_TCINDEX
, &ife_tcindex_v
,
207 addattr_l(n
, MAX_MSG
, IFE_META_TCINDEX
, NULL
, 0);
210 addattr_nest_end(n
, tail2
);
213 addattr_nest_end(n
, tail
);
220 static int print_ife(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
222 struct tc_ife
*p
= NULL
;
223 struct rtattr
*tb
[TCA_IFE_MAX
+ 1];
228 int has_optional
= 0;
234 parse_rtattr_nested(tb
, TCA_IFE_MAX
, arg
);
236 if (tb
[TCA_IFE_PARMS
] == NULL
) {
237 print_string(PRINT_FP
, NULL
, "%s", "[NULL ife parameters]");
240 p
= RTA_DATA(tb
[TCA_IFE_PARMS
]);
242 print_string(PRINT_ANY
, "kind", "%s ", "ife");
243 print_string(PRINT_ANY
, "mode", "%s ",
244 p
->flags
& IFE_ENCODE
? "encode" : "decode");
245 print_action_control(f
, "action ", p
->action
, " ");
247 if (tb
[TCA_IFE_TYPE
]) {
248 ife_type
= rta_getattr_u16(tb
[TCA_IFE_TYPE
]);
250 print_0xhex(PRINT_ANY
, "type", "type %#llX ", ife_type
);
254 print_string(PRINT_FP
, NULL
, "%s\t", _SL_
);
256 if (tb
[TCA_IFE_METALST
]) {
257 struct rtattr
*metalist
[IFE_META_MAX
+ 1];
260 parse_rtattr_nested(metalist
, IFE_META_MAX
,
261 tb
[TCA_IFE_METALST
]);
263 if (metalist
[IFE_META_SKBMARK
]) {
264 len
= RTA_PAYLOAD(metalist
[IFE_META_SKBMARK
]);
266 mmark
= rta_getattr_u32(metalist
[IFE_META_SKBMARK
]);
267 print_uint(PRINT_ANY
, "mark", "use mark %u ",
270 print_string(PRINT_ANY
, "mark", "%s mark ",
274 if (metalist
[IFE_META_TCINDEX
]) {
275 len
= RTA_PAYLOAD(metalist
[IFE_META_TCINDEX
]);
278 rta_getattr_u16(metalist
[IFE_META_TCINDEX
]);
279 print_uint(PRINT_ANY
, "tcindex",
280 "use tcindex %u ", mtcindex
);
282 print_string(PRINT_ANY
, "tcindex",
283 "%s tcindex ", "allow");
286 if (metalist
[IFE_META_PRIO
]) {
287 len
= RTA_PAYLOAD(metalist
[IFE_META_PRIO
]);
289 mprio
= rta_getattr_u32(metalist
[IFE_META_PRIO
]);
290 print_uint(PRINT_ANY
, "prio", "use prio %u ",
293 print_string(PRINT_ANY
, "prio", "%s prio ",
299 if (tb
[TCA_IFE_DMAC
]) {
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
,
307 if (tb
[TCA_IFE_SMAC
]) {
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
,
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
);
321 if (tb
[TCA_IFE_TM
]) {
322 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IFE_TM
]);
328 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
333 struct action_util ife_action_util
= {
335 .parse_aopt
= parse_ife
,
336 .print_aopt
= print_ife
,