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");
192 fprintf(stderr
, "IFE requires decode/encode specified\n");
196 tail
= NLMSG_TAIL(n
);
197 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
198 addattr_l(n
, MAX_MSG
, TCA_IFE_PARMS
, &p
, sizeof(p
));
200 if (!(p
.flags
& IFE_ENCODE
))
204 addattr_l(n
, MAX_MSG
, TCA_IFE_DMAC
, dbuf
, ETH_ALEN
);
206 addattr_l(n
, MAX_MSG
, TCA_IFE_TYPE
, &ife_type
, 2);
208 addattr_l(n
, MAX_MSG
, TCA_IFE_SMAC
, sbuf
, ETH_ALEN
);
210 tail2
= NLMSG_TAIL(n
);
211 addattr_l(n
, MAX_MSG
, TCA_IFE_METALST
, NULL
, 0);
212 if (ife_mark
|| ife_mark_v
) {
214 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, &ife_mark_v
, 4);
216 addattr_l(n
, MAX_MSG
, IFE_META_SKBMARK
, NULL
, 0);
218 if (ife_prio
|| ife_prio_v
) {
220 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, &ife_prio_v
, 4);
222 addattr_l(n
, MAX_MSG
, IFE_META_PRIO
, NULL
, 0);
225 tail2
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail2
;
228 tail
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail
;
235 static int print_ife(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
237 struct tc_ife
*p
= NULL
;
238 struct rtattr
*tb
[TCA_IFE_MAX
+ 1];
243 int has_optional
= 0;
250 parse_rtattr_nested(tb
, TCA_IFE_MAX
, arg
);
252 if (tb
[TCA_IFE_PARMS
] == NULL
) {
253 fprintf(f
, "[NULL ife parameters]");
256 p
= RTA_DATA(tb
[TCA_IFE_PARMS
]);
258 fprintf(f
, "ife %s action %s ",
259 (p
->flags
& IFE_ENCODE
) ? "encode" : "decode",
260 action_n2a(p
->action
, b1
, sizeof(b1
)));
262 if (tb
[TCA_IFE_TYPE
]) {
263 ife_type
= rta_getattr_u16(tb
[TCA_IFE_TYPE
]);
265 fprintf(f
, "type 0x%X ", ife_type
);
271 if (tb
[TCA_IFE_METALST
]) {
272 struct rtattr
*metalist
[IFE_META_MAX
+ 1];
275 parse_rtattr_nested(metalist
, IFE_META_MAX
,
276 tb
[TCA_IFE_METALST
]);
278 if (metalist
[IFE_META_SKBMARK
]) {
279 len
= RTA_PAYLOAD(metalist
[IFE_META_SKBMARK
]);
281 mmark
= rta_getattr_u32(metalist
[IFE_META_SKBMARK
]);
282 fprintf(f
, "use mark %d ", mmark
);
284 fprintf(f
, "allow mark ");
287 if (metalist
[IFE_META_HASHID
]) {
288 len
= RTA_PAYLOAD(metalist
[IFE_META_HASHID
]);
290 mhash
= rta_getattr_u32(metalist
[IFE_META_HASHID
]);
291 fprintf(f
, "use hash %d ", mhash
);
293 fprintf(f
, "allow hash ");
296 if (metalist
[IFE_META_PRIO
]) {
297 len
= RTA_PAYLOAD(metalist
[IFE_META_PRIO
]);
299 mprio
= rta_getattr_u32(metalist
[IFE_META_PRIO
]);
300 fprintf(f
, "use prio %d ", mprio
);
302 fprintf(f
, "allow prio ");
307 if (tb
[TCA_IFE_DMAC
]) {
309 fprintf(f
, "dst %s ",
310 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_DMAC
]),
311 RTA_PAYLOAD(tb
[TCA_IFE_DMAC
]), 0, b2
,
316 if (tb
[TCA_IFE_SMAC
]) {
318 fprintf(f
, "src %s ",
319 ll_addr_n2a(RTA_DATA(tb
[TCA_IFE_SMAC
]),
320 RTA_PAYLOAD(tb
[TCA_IFE_SMAC
]), 0, b2
,
324 fprintf(f
, "\n\t index %d ref %d bind %d", p
->index
, p
->refcnt
,
327 if (tb
[TCA_IFE_TM
]) {
328 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IFE_TM
]);
339 struct action_util ife_action_util
= {
341 .parse_aopt
= parse_ife
,
342 .print_aopt
= print_ife
,