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} [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 "\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
= { .action
= TC_ACT_PIPE
}; /* good default */
76 if (matches(*argv
, "ife") == 0) {
79 } else if (matches(*argv
, "decode") == 0) {
80 p
.flags
= IFE_DECODE
; /* readability aid */
82 } else if (matches(*argv
, "encode") == 0) {
85 } else if (matches(*argv
, "allow") == 0) {
87 if (matches(*argv
, "mark") == 0) {
88 ife_mark
= IFE_META_SKBMARK
;
89 } else if (matches(*argv
, "prio") == 0) {
90 ife_prio
= IFE_META_PRIO
;
92 fprintf(stderr
, "Illegal meta define <%s>\n",
96 } else if (matches(*argv
, "use") == 0) {
98 if (matches(*argv
, "mark") == 0) {
100 if (get_u32(&ife_mark_v
, *argv
, 0))
101 invarg("ife mark val is invalid",
103 } else if (matches(*argv
, "prio") == 0) {
105 if (get_u32(&ife_prio_v
, *argv
, 0))
106 invarg("ife prio val is invalid",
109 fprintf(stderr
, "Illegal meta use type <%s>\n",
113 } else if (matches(*argv
, "type") == 0) {
115 if (get_u16(&ife_type
, *argv
, 0))
116 invarg("ife type is invalid", *argv
);
117 fprintf(stderr
, "IFE type 0x%x\n", ife_type
);
118 } else if (matches(*argv
, "dst") == 0) {
121 if (sscanf(daddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
122 dbuf
, dbuf
+ 1, dbuf
+ 2,
123 dbuf
+ 3, dbuf
+ 4, dbuf
+ 5) != 6) {
124 fprintf(stderr
, "Invalid mac address %s\n",
127 fprintf(stderr
, "dst MAC address <%s>\n", daddr
);
129 } else if (matches(*argv
, "src") == 0) {
132 if (sscanf(saddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
133 sbuf
, sbuf
+ 1, sbuf
+ 2,
134 sbuf
+ 3, sbuf
+ 4, sbuf
+ 5) != 6) {
135 fprintf(stderr
, "Invalid mac address %s\n",
138 fprintf(stderr
, "src MAC address <%s>\n", saddr
);
139 } else if (matches(*argv
, "help") == 0) {
149 if (argc
&& !action_a2n(*argv
, &p
.action
, false))
153 if (matches(*argv
, "index") == 0) {
155 if (get_u32(&p
.index
, *argv
, 10)) {
156 fprintf(stderr
, "ife: Illegal \"index\"\n");
166 fprintf(stderr
, "IFE requires decode/encode specified\n");
170 tail
= NLMSG_TAIL(n
);
171 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
172 addattr_l(n
, MAX_MSG
, TCA_IFE_PARMS
, &p
, sizeof(p
));
174 if (!(p
.flags
& IFE_ENCODE
))
178 addattr_l(n
, MAX_MSG
, TCA_IFE_DMAC
, dbuf
, ETH_ALEN
);
180 addattr_l(n
, MAX_MSG
, TCA_IFE_TYPE
, &ife_type
, 2);
182 addattr_l(n
, MAX_MSG
, TCA_IFE_SMAC
, sbuf
, ETH_ALEN
);
184 tail2
= NLMSG_TAIL(n
);
185 addattr_l(n
, MAX_MSG
, TCA_IFE_METALST
, NULL
, 0);
186 if (ife_mark
|| ife_mark_v
) {
188 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, &ife_mark_v
, 4);
190 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, NULL
, 0);
192 if (ife_prio
|| ife_prio_v
) {
194 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, &ife_prio_v
, 4);
196 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, NULL
, 0);
199 tail2
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail2
;
202 tail
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail
;
209 static int print_ife(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
211 struct tc_ife
*p
= NULL
;
212 struct rtattr
*tb
[TCA_IFE_MAX
+ 1];
217 int has_optional
= 0;
224 parse_rtattr_nested(tb
, TCA_IFE_MAX
, arg
);
226 if (tb
[TCA_IFE_PARMS
] == NULL
) {
227 fprintf(f
, "[NULL ife parameters]");
230 p
= RTA_DATA(tb
[TCA_IFE_PARMS
]);
232 fprintf(f
, "ife %s action %s ",
233 (p
->flags
& IFE_ENCODE
) ? "encode" : "decode",
234 action_n2a(p
->action
, b1
, sizeof(b1
)));
236 if (tb
[TCA_IFE_TYPE
]) {
237 ife_type
= rta_getattr_u16(tb
[TCA_IFE_TYPE
]);
239 fprintf(f
, "type 0x%X ", ife_type
);
245 if (tb
[TCA_IFE_METALST
]) {
246 struct rtattr
*metalist
[IFE_META_MAX
+ 1];
249 parse_rtattr_nested(metalist
, IFE_META_MAX
,
250 tb
[TCA_IFE_METALST
]);
252 if (metalist
[IFE_META_SKBMARK
]) {
253 len
= RTA_PAYLOAD(metalist
[IFE_META_SKBMARK
]);
255 mmark
= rta_getattr_u32(metalist
[IFE_META_SKBMARK
]);
256 fprintf(f
, "use mark %d ", mmark
);
258 fprintf(f
, "allow mark ");
261 if (metalist
[IFE_META_HASHID
]) {
262 len
= RTA_PAYLOAD(metalist
[IFE_META_HASHID
]);
264 mhash
= rta_getattr_u32(metalist
[IFE_META_HASHID
]);
265 fprintf(f
, "use hash %d ", mhash
);
267 fprintf(f
, "allow hash ");
270 if (metalist
[IFE_META_PRIO
]) {
271 len
= RTA_PAYLOAD(metalist
[IFE_META_PRIO
]);
273 mprio
= rta_getattr_u32(metalist
[IFE_META_PRIO
]);
274 fprintf(f
, "use prio %d ", mprio
);
276 fprintf(f
, "allow prio ");
281 if (tb
[TCA_IFE_DMAC
]) {
283 fprintf(f
, "dst %s ",
284 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_DMAC
]),
285 RTA_PAYLOAD(tb
[TCA_IFE_DMAC
]), 0, b2
,
290 if (tb
[TCA_IFE_SMAC
]) {
292 fprintf(f
, "src %s ",
293 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_SMAC
]),
294 RTA_PAYLOAD(tb
[TCA_IFE_SMAC
]), 0, b2
,
298 fprintf(f
, "\n\t index %d ref %d bind %d", p
->index
, p
->refcnt
,
301 if (tb
[TCA_IFE_TM
]) {
302 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IFE_TM
]);
313 struct action_util ife_action_util
= {
315 .parse_aopt
= parse_ife
,
316 .print_aopt
= print_ife
,