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 fprintf(stderr
, "Illegal meta define <%s>\n",
101 } else if (matches(*argv
, "use") == 0) {
103 if (matches(*argv
, "mark") == 0) {
105 if (get_u32(&ife_mark_v
, *argv
, 0))
106 invarg("ife mark val is invalid",
108 } else if (matches(*argv
, "prio") == 0) {
110 if (get_u32(&ife_prio_v
, *argv
, 0))
111 invarg("ife prio val is invalid",
113 } else if (matches(*argv
, "tcindex") == 0) {
115 if (get_u16(&ife_tcindex_v
, *argv
, 0))
116 invarg("ife tcindex val is invalid",
119 fprintf(stderr
, "Illegal meta use type <%s>\n",
123 } else if (matches(*argv
, "type") == 0) {
125 if (get_u16(&ife_type
, *argv
, 0))
126 invarg("ife type is invalid", *argv
);
127 fprintf(stderr
, "IFE type 0x%04X\n", ife_type
);
129 } else if (matches(*argv
, "dst") == 0) {
132 if (sscanf(daddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
133 dbuf
, dbuf
+ 1, dbuf
+ 2,
134 dbuf
+ 3, dbuf
+ 4, dbuf
+ 5) != 6) {
135 fprintf(stderr
, "Invalid mac address %s\n",
138 fprintf(stderr
, "dst MAC address <%s>\n", daddr
);
140 } else if (matches(*argv
, "src") == 0) {
143 if (sscanf(saddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
144 sbuf
, sbuf
+ 1, sbuf
+ 2,
145 sbuf
+ 3, sbuf
+ 4, sbuf
+ 5) != 6) {
146 fprintf(stderr
, "Invalid mac address %s\n",
149 fprintf(stderr
, "src MAC address <%s>\n", saddr
);
150 } else if (matches(*argv
, "help") == 0) {
160 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 fprintf(stderr
, "IFE type 0x%04X\n", ETH_P_IFE
);
195 addattr_l(n
, MAX_MSG
, TCA_IFE_SMAC
, sbuf
, ETH_ALEN
);
197 tail2
= NLMSG_TAIL(n
);
198 addattr_l(n
, MAX_MSG
, TCA_IFE_METALST
, NULL
, 0);
199 if (ife_mark
|| ife_mark_v
) {
201 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, &ife_mark_v
, 4);
203 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, NULL
, 0);
205 if (ife_prio
|| ife_prio_v
) {
207 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, &ife_prio_v
, 4);
209 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, NULL
, 0);
211 if (ife_tcindex
|| ife_tcindex_v
) {
213 addattr_l(n
, MAX_MSG
, IFE_META_TCINDEX
, &ife_tcindex_v
,
216 addattr_l(n
, MAX_MSG
, IFE_META_TCINDEX
, NULL
, 0);
219 tail2
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail2
;
222 tail
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail
;
229 static int print_ife(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
231 struct tc_ife
*p
= NULL
;
232 struct rtattr
*tb
[TCA_IFE_MAX
+ 1];
237 int has_optional
= 0;
243 parse_rtattr_nested(tb
, TCA_IFE_MAX
, arg
);
245 if (tb
[TCA_IFE_PARMS
] == NULL
) {
246 fprintf(f
, "[NULL ife parameters]");
249 p
= RTA_DATA(tb
[TCA_IFE_PARMS
]);
251 fprintf(f
, "ife %s ", p
->flags
& IFE_ENCODE
? "encode" : "decode");
252 print_action_control(f
, "action ", p
->action
, " ");
254 if (tb
[TCA_IFE_TYPE
]) {
255 ife_type
= rta_getattr_u16(tb
[TCA_IFE_TYPE
]);
257 fprintf(f
, "type 0x%X ", ife_type
);
263 if (tb
[TCA_IFE_METALST
]) {
264 struct rtattr
*metalist
[IFE_META_MAX
+ 1];
267 parse_rtattr_nested(metalist
, IFE_META_MAX
,
268 tb
[TCA_IFE_METALST
]);
270 if (metalist
[IFE_META_SKBMARK
]) {
271 len
= RTA_PAYLOAD(metalist
[IFE_META_SKBMARK
]);
273 mmark
= rta_getattr_u32(metalist
[IFE_META_SKBMARK
]);
274 fprintf(f
, "use mark %u ", mmark
);
276 fprintf(f
, "allow mark ");
279 if (metalist
[IFE_META_TCINDEX
]) {
280 len
= RTA_PAYLOAD(metalist
[IFE_META_TCINDEX
]);
283 rta_getattr_u16(metalist
[IFE_META_TCINDEX
]);
284 fprintf(f
, "use tcindex %d ", mtcindex
);
286 fprintf(f
, "allow tcindex ");
289 if (metalist
[IFE_META_PRIO
]) {
290 len
= RTA_PAYLOAD(metalist
[IFE_META_PRIO
]);
292 mprio
= rta_getattr_u32(metalist
[IFE_META_PRIO
]);
293 fprintf(f
, "use prio %u ", mprio
);
295 fprintf(f
, "allow prio ");
300 if (tb
[TCA_IFE_DMAC
]) {
302 fprintf(f
, "dst %s ",
303 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_DMAC
]),
304 RTA_PAYLOAD(tb
[TCA_IFE_DMAC
]), 0, b2
,
309 if (tb
[TCA_IFE_SMAC
]) {
311 fprintf(f
, "src %s ",
312 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_SMAC
]),
313 RTA_PAYLOAD(tb
[TCA_IFE_SMAC
]), 0, b2
,
317 fprintf(f
, "\n\t index %u ref %d bind %d", p
->index
, p
->refcnt
,
320 if (tb
[TCA_IFE_TM
]) {
321 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IFE_TM
]);
332 struct action_util ife_action_util
= {
334 .parse_aopt
= parse_ife
,
335 .print_aopt
= print_ife
,