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)
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
22 #include <linux/netdevice.h>
27 #include <linux/tc_act/tc_ife.h>
29 static void ife_explain(void)
32 "Usage:... ife {decode|encode} [{ALLOW|USE} ATTR] [dst DMAC] [src SMAC] [type TYPE] [CONTROL] [index INDEX]\n");
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");
48 static void ife_usage(void)
54 static int parse_ife(struct action_util
*a
, int *argc_p
, char ***argv_p
,
55 int tca_id
, struct nlmsghdr
*n
)
58 char **argv
= *argv_p
;
60 struct tc_ife p
= { 0 };
71 __u16 ife_tcindex
= 0;
72 __u16 ife_tcindex_v
= 0;
80 if (matches(*argv
, "ife") == 0) {
83 } else if (matches(*argv
, "decode") == 0) {
84 p
.flags
= IFE_DECODE
; /* readability aid */
86 } else if (matches(*argv
, "encode") == 0) {
89 } else if (matches(*argv
, "allow") == 0) {
91 if (matches(*argv
, "mark") == 0) {
92 ife_mark
= IFE_META_SKBMARK
;
93 } else if (matches(*argv
, "prio") == 0) {
94 ife_prio
= IFE_META_PRIO
;
95 } else if (matches(*argv
, "tcindex") == 0) {
96 ife_prio
= IFE_META_TCINDEX
;
98 fprintf(stderr
, "Illegal meta define <%s>\n",
102 } else if (matches(*argv
, "use") == 0) {
104 if (matches(*argv
, "mark") == 0) {
106 if (get_u32(&ife_mark_v
, *argv
, 0))
107 invarg("ife mark val is invalid",
109 } else if (matches(*argv
, "prio") == 0) {
111 if (get_u32(&ife_prio_v
, *argv
, 0))
112 invarg("ife prio val is invalid",
114 } else if (matches(*argv
, "tcindex") == 0) {
116 if (get_u16(&ife_tcindex_v
, *argv
, 0))
117 invarg("ife tcindex val is invalid",
120 fprintf(stderr
, "Illegal meta use type <%s>\n",
124 } else if (matches(*argv
, "type") == 0) {
126 if (get_u16(&ife_type
, *argv
, 0))
127 invarg("ife type is invalid", *argv
);
128 fprintf(stderr
, "IFE type 0x%x\n", ife_type
);
130 } else if (matches(*argv
, "dst") == 0) {
133 if (sscanf(daddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
134 dbuf
, dbuf
+ 1, dbuf
+ 2,
135 dbuf
+ 3, dbuf
+ 4, dbuf
+ 5) != 6) {
136 fprintf(stderr
, "Invalid mac address %s\n",
139 fprintf(stderr
, "dst MAC address <%s>\n", daddr
);
141 } else if (matches(*argv
, "src") == 0) {
144 if (sscanf(saddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
145 sbuf
, sbuf
+ 1, sbuf
+ 2,
146 sbuf
+ 3, sbuf
+ 4, sbuf
+ 5) != 6) {
147 fprintf(stderr
, "Invalid mac address %s\n",
150 fprintf(stderr
, "src MAC address <%s>\n", saddr
);
151 } else if (matches(*argv
, "help") == 0) {
161 parse_action_control_dflt(&argc
, &argv
, &p
.action
, false, TC_ACT_PIPE
);
164 if (matches(*argv
, "index") == 0) {
166 if (get_u32(&p
.index
, *argv
, 0)) {
167 fprintf(stderr
, "ife: Illegal \"index\"\n");
177 fprintf(stderr
, "IFE requires decode/encode specified\n");
181 tail
= NLMSG_TAIL(n
);
182 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
183 addattr_l(n
, MAX_MSG
, TCA_IFE_PARMS
, &p
, sizeof(p
));
185 if (!(p
.flags
& IFE_ENCODE
))
189 addattr_l(n
, MAX_MSG
, TCA_IFE_DMAC
, dbuf
, ETH_ALEN
);
191 addattr_l(n
, MAX_MSG
, TCA_IFE_TYPE
, &ife_type
, 2);
193 addattr_l(n
, MAX_MSG
, TCA_IFE_SMAC
, sbuf
, ETH_ALEN
);
195 tail2
= NLMSG_TAIL(n
);
196 addattr_l(n
, MAX_MSG
, TCA_IFE_METALST
, NULL
, 0);
197 if (ife_mark
|| ife_mark_v
) {
199 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, &ife_mark_v
, 4);
201 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, NULL
, 0);
203 if (ife_prio
|| ife_prio_v
) {
205 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, &ife_prio_v
, 4);
207 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, NULL
, 0);
209 if (ife_tcindex
|| ife_tcindex_v
) {
211 addattr_l(n
, MAX_MSG
, IFE_META_TCINDEX
, &ife_tcindex_v
,
214 addattr_l(n
, MAX_MSG
, IFE_META_TCINDEX
, NULL
, 0);
217 tail2
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail2
;
220 tail
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail
;
227 static int print_ife(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
229 struct tc_ife
*p
= NULL
;
230 struct rtattr
*tb
[TCA_IFE_MAX
+ 1];
235 int has_optional
= 0;
241 parse_rtattr_nested(tb
, TCA_IFE_MAX
, arg
);
243 if (tb
[TCA_IFE_PARMS
] == NULL
) {
244 fprintf(f
, "[NULL ife parameters]");
247 p
= RTA_DATA(tb
[TCA_IFE_PARMS
]);
249 fprintf(f
, "ife %s ", p
->flags
& IFE_ENCODE
? "encode" : "decode");
250 print_action_control(f
, "action ", p
->action
, " ");
252 if (tb
[TCA_IFE_TYPE
]) {
253 ife_type
= rta_getattr_u16(tb
[TCA_IFE_TYPE
]);
255 fprintf(f
, "type 0x%X ", ife_type
);
261 if (tb
[TCA_IFE_METALST
]) {
262 struct rtattr
*metalist
[IFE_META_MAX
+ 1];
265 parse_rtattr_nested(metalist
, IFE_META_MAX
,
266 tb
[TCA_IFE_METALST
]);
268 if (metalist
[IFE_META_SKBMARK
]) {
269 len
= RTA_PAYLOAD(metalist
[IFE_META_SKBMARK
]);
271 mmark
= rta_getattr_u32(metalist
[IFE_META_SKBMARK
]);
272 fprintf(f
, "use mark %u ", mmark
);
274 fprintf(f
, "allow mark ");
277 if (metalist
[IFE_META_TCINDEX
]) {
278 len
= RTA_PAYLOAD(metalist
[IFE_META_TCINDEX
]);
281 rta_getattr_u16(metalist
[IFE_META_TCINDEX
]);
282 fprintf(f
, "use tcindex %d ", mtcindex
);
284 fprintf(f
, "allow tcindex ");
287 if (metalist
[IFE_META_PRIO
]) {
288 len
= RTA_PAYLOAD(metalist
[IFE_META_PRIO
]);
290 mprio
= rta_getattr_u32(metalist
[IFE_META_PRIO
]);
291 fprintf(f
, "use prio %u ", mprio
);
293 fprintf(f
, "allow prio ");
298 if (tb
[TCA_IFE_DMAC
]) {
300 fprintf(f
, "dst %s ",
301 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_DMAC
]),
302 RTA_PAYLOAD(tb
[TCA_IFE_DMAC
]), 0, b2
,
307 if (tb
[TCA_IFE_SMAC
]) {
309 fprintf(f
, "src %s ",
310 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_SMAC
]),
311 RTA_PAYLOAD(tb
[TCA_IFE_SMAC
]), 0, b2
,
315 fprintf(f
, "\n\t index %u ref %d bind %d", p
->index
, p
->refcnt
,
318 if (tb
[TCA_IFE_TM
]) {
319 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IFE_TM
]);
330 struct action_util ife_action_util
= {
332 .parse_aopt
= parse_ife
,
333 .print_aopt
= print_ife
,