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
;
72 memset(&p
, 0, sizeof(p
));
73 p
.action
= TC_ACT_PIPE
; /* good default */
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
;
95 fprintf(stderr
, "Illegal meta define <%s>\n",
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",
112 fprintf(stderr
, "Illegal meta use type <%s>\n",
116 } else if (matches(*argv
, "type") == 0) {
118 if (get_u16(&ife_type
, *argv
, 0))
119 invarg("ife type is invalid", *argv
);
120 fprintf(stderr
, "IFE type 0x%x\n", ife_type
);
121 } else if (matches(*argv
, "dst") == 0) {
124 if (sscanf(daddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
125 dbuf
, dbuf
+ 1, dbuf
+ 2,
126 dbuf
+ 3, dbuf
+ 4, dbuf
+ 5) != 6) {
127 fprintf(stderr
, "Invalid mac address %s\n",
130 fprintf(stderr
, "dst MAC address <%s>\n", daddr
);
132 } else if (matches(*argv
, "src") == 0) {
135 if (sscanf(saddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
136 sbuf
, sbuf
+ 1, sbuf
+ 2,
137 sbuf
+ 3, sbuf
+ 4, sbuf
+ 5) != 6) {
138 fprintf(stderr
, "Invalid mac address %s\n",
141 fprintf(stderr
, "src MAC address <%s>\n", saddr
);
142 } else if (matches(*argv
, "help") == 0) {
153 if (matches(*argv
, "reclassify") == 0) {
154 p
.action
= TC_ACT_RECLASSIFY
;
157 } else if (matches(*argv
, "pipe") == 0) {
158 p
.action
= TC_ACT_PIPE
;
161 } else if (matches(*argv
, "drop") == 0 ||
162 matches(*argv
, "shot") == 0) {
163 p
.action
= TC_ACT_SHOT
;
166 } else if (matches(*argv
, "continue") == 0) {
167 p
.action
= TC_ACT_UNSPEC
;
170 } else if (matches(*argv
, "pass") == 0 ||
171 matches(*argv
, "ok") == 0) {
172 p
.action
= TC_ACT_OK
;
179 if (matches(*argv
, "index") == 0) {
181 if (get_u32(&p
.index
, *argv
, 10)) {
182 fprintf(stderr
, "ife: Illegal \"index\"\n");
191 fprintf(stderr
, "IFE requires decode/encode specified\n");
195 tail
= NLMSG_TAIL(n
);
196 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
197 addattr_l(n
, MAX_MSG
, TCA_IFE_PARMS
, &p
, sizeof(p
));
199 if (!(p
.flags
& IFE_ENCODE
))
203 addattr_l(n
, MAX_MSG
, TCA_IFE_DMAC
, dbuf
, ETH_ALEN
);
205 addattr_l(n
, MAX_MSG
, TCA_IFE_TYPE
, &ife_type
, 2);
207 addattr_l(n
, MAX_MSG
, TCA_IFE_SMAC
, sbuf
, ETH_ALEN
);
209 tail2
= NLMSG_TAIL(n
);
210 addattr_l(n
, MAX_MSG
, TCA_IFE_METALST
, NULL
, 0);
211 if (ife_mark
|| ife_mark_v
) {
213 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, &ife_mark_v
, 4);
215 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, NULL
, 0);
217 if (ife_prio
|| ife_prio_v
) {
219 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, &ife_prio_v
, 4);
221 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, NULL
, 0);
224 tail2
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail2
;
227 tail
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail
;
234 static int print_ife(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
236 struct tc_ife
*p
= NULL
;
237 struct rtattr
*tb
[TCA_IFE_MAX
+ 1];
242 int has_optional
= 0;
249 parse_rtattr_nested(tb
, TCA_IFE_MAX
, arg
);
251 if (tb
[TCA_IFE_PARMS
] == NULL
) {
252 fprintf(f
, "[NULL ife parameters]");
255 p
= RTA_DATA(tb
[TCA_IFE_PARMS
]);
257 fprintf(f
, "ife %s action %s ",
258 (p
->flags
& IFE_ENCODE
) ? "encode" : "decode",
259 action_n2a(p
->action
, b1
, sizeof(b1
)));
261 if (tb
[TCA_IFE_TYPE
]) {
262 ife_type
= rta_getattr_u16(tb
[TCA_IFE_TYPE
]);
264 fprintf(f
, "type 0x%X ", ife_type
);
270 if (tb
[TCA_IFE_METALST
]) {
271 struct rtattr
*metalist
[IFE_META_MAX
+ 1];
274 parse_rtattr_nested(metalist
, IFE_META_MAX
,
275 tb
[TCA_IFE_METALST
]);
277 if (metalist
[IFE_META_SKBMARK
]) {
278 len
= RTA_PAYLOAD(metalist
[IFE_META_SKBMARK
]);
280 mmark
= rta_getattr_u32(metalist
[IFE_META_SKBMARK
]);
281 fprintf(f
, "use mark %d ", mmark
);
283 fprintf(f
, "allow mark ");
286 if (metalist
[IFE_META_HASHID
]) {
287 len
= RTA_PAYLOAD(metalist
[IFE_META_HASHID
]);
289 mhash
= rta_getattr_u32(metalist
[IFE_META_HASHID
]);
290 fprintf(f
, "use hash %d ", mhash
);
292 fprintf(f
, "allow hash ");
295 if (metalist
[IFE_META_PRIO
]) {
296 len
= RTA_PAYLOAD(metalist
[IFE_META_PRIO
]);
298 mprio
= rta_getattr_u32(metalist
[IFE_META_PRIO
]);
299 fprintf(f
, "use prio %d ", mprio
);
301 fprintf(f
, "allow prio ");
306 if (tb
[TCA_IFE_DMAC
]) {
308 fprintf(f
, "dst %s ",
309 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_DMAC
]),
310 RTA_PAYLOAD(tb
[TCA_IFE_DMAC
]), 0, b2
,
315 if (tb
[TCA_IFE_SMAC
]) {
317 fprintf(f
, "src %s ",
318 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_SMAC
]),
319 RTA_PAYLOAD(tb
[TCA_IFE_SMAC
]), 0, b2
,
323 fprintf(f
, "\n\t index %d ref %d bind %d", p
->index
, p
->refcnt
,
326 if (tb
[TCA_IFE_TM
]) {
327 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IFE_TM
]);
338 struct action_util ife_action_util
= {
340 .parse_aopt
= parse_ife
,
341 .print_aopt
= print_ife
,