2 * m_skbmod.c skb modifier action 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_skbmod.h>
29 static void skbmod_explain(void)
32 "Usage:... skbmod {[set <SETTABLE>] [swap <SWAPABLE>]} [CONTROL] [index INDEX]\n"
33 "where SETTABLE is: [dmac DMAC] [smac SMAC] [etype ETYPE]\n"
34 "where SWAPABLE is: \"mac\" to swap mac addresses\n"
35 "note: \"swap mac\" is done after any outstanding D/SMAC change\n"
36 "\tDMAC := 6 byte Destination MAC address\n"
37 "\tSMAC := optional 6 byte Source MAC address\n"
38 "\tETYPE := optional 16 bit ethertype\n"
39 "\tCONTROL := reclassify|pipe|drop|continue|ok\n"
40 "\tINDEX := skbmod index value to use\n");
43 static void skbmod_usage(void)
49 static int parse_skbmod(struct action_util
*a
, int *argc_p
, char ***argv_p
,
50 int tca_id
, struct nlmsghdr
*n
)
53 char **argv
= *argv_p
;
59 __u16 skbmod_etype
= 0;
63 memset(&p
, 0, sizeof(p
));
64 p
.action
= TC_ACT_PIPE
; /* good default */
70 if (matches(*argv
, "skbmod") == 0) {
73 } else if (matches(*argv
, "swap") == 0) {
76 } else if (matches(*argv
, "mac") == 0) {
77 p
.flags
|= SKBMOD_F_SWAPMAC
;
79 } else if (matches(*argv
, "set") == 0) {
82 } else if (matches(*argv
, "etype") == 0) {
84 if (get_u16(&skbmod_etype
, *argv
, 0))
85 invarg("ethertype is invalid", *argv
);
86 fprintf(stderr
, "skbmod etype 0x%x\n", skbmod_etype
);
87 p
.flags
|= SKBMOD_F_ETYPE
;
89 } else if (matches(*argv
, "dmac") == 0) {
92 if (sscanf(daddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
93 dbuf
, dbuf
+ 1, dbuf
+ 2,
94 dbuf
+ 3, dbuf
+ 4, dbuf
+ 5) != 6) {
95 fprintf(stderr
, "Invalid dst mac address %s\n",
99 p
.flags
|= SKBMOD_F_DMAC
;
100 fprintf(stderr
, "dst MAC address <%s>\n", daddr
);
103 } else if (matches(*argv
, "smac") == 0) {
106 if (sscanf(saddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
107 sbuf
, sbuf
+ 1, sbuf
+ 2,
108 sbuf
+ 3, sbuf
+ 4, sbuf
+ 5) != 6) {
109 fprintf(stderr
, "Invalid smac address %s\n",
113 p
.flags
|= SKBMOD_F_SMAC
;
114 fprintf(stderr
, "src MAC address <%s>\n", saddr
);
116 } else if (matches(*argv
, "help") == 0) {
127 if (matches(*argv
, "reclassify") == 0) {
128 p
.action
= TC_ACT_RECLASSIFY
;
131 } else if (matches(*argv
, "pipe") == 0) {
132 p
.action
= TC_ACT_PIPE
;
135 } else if (matches(*argv
, "drop") == 0 ||
136 matches(*argv
, "shot") == 0) {
137 p
.action
= TC_ACT_SHOT
;
140 } else if (matches(*argv
, "continue") == 0) {
141 p
.action
= TC_ACT_UNSPEC
;
144 } else if (matches(*argv
, "pass") == 0 ||
145 matches(*argv
, "ok") == 0) {
146 p
.action
= TC_ACT_OK
;
153 if (matches(*argv
, "index") == 0) {
155 if (get_u32(&p
.index
, *argv
, 0)) {
156 fprintf(stderr
, "skbmod: Illegal \"index\"\n");
166 fprintf(stderr
, "skbmod requires at least one option\n");
170 tail
= NLMSG_TAIL(n
);
171 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
172 addattr_l(n
, MAX_MSG
, TCA_SKBMOD_PARMS
, &p
, sizeof(p
));
175 addattr_l(n
, MAX_MSG
, TCA_SKBMOD_DMAC
, dbuf
, ETH_ALEN
);
177 addattr16(n
, MAX_MSG
, TCA_SKBMOD_ETYPE
, skbmod_etype
);
179 addattr_l(n
, MAX_MSG
, TCA_SKBMOD_SMAC
, sbuf
, ETH_ALEN
);
181 tail
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail
;
188 static int print_skbmod(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
190 struct tc_skbmod
*p
= NULL
;
191 struct rtattr
*tb
[TCA_SKBMOD_MAX
+ 1];
192 __u16 skbmod_etype
= 0;
193 int has_optional
= 0;
200 parse_rtattr_nested(tb
, TCA_SKBMOD_MAX
, arg
);
202 if (tb
[TCA_SKBMOD_PARMS
] == NULL
) {
203 fprintf(f
, "[NULL skbmod parameters]");
207 p
= RTA_DATA(tb
[TCA_SKBMOD_PARMS
]);
209 fprintf(f
, "skbmod action %s ", action_n2a(p
->action
));
211 if (tb
[TCA_SKBMOD_ETYPE
]) {
212 skbmod_etype
= rta_getattr_u16(tb
[TCA_SKBMOD_ETYPE
]);
214 fprintf(f
, "set etype 0x%X ", skbmod_etype
);
220 if (tb
[TCA_SKBMOD_DMAC
]) {
222 fprintf(f
, "set dmac %s ",
223 ll_addr_n2a(RTA_DATA(tb
[TCA_SKBMOD_DMAC
]),
224 RTA_PAYLOAD(tb
[TCA_SKBMOD_DMAC
]), 0, b1
,
229 if (tb
[TCA_SKBMOD_SMAC
]) {
231 fprintf(f
, "set smac %s ",
232 ll_addr_n2a(RTA_DATA(tb
[TCA_SKBMOD_SMAC
]),
233 RTA_PAYLOAD(tb
[TCA_SKBMOD_SMAC
]), 0, b2
,
237 if (p
->flags
& SKBMOD_F_SWAPMAC
)
238 fprintf(f
, "swap mac ");
240 fprintf(f
, "\n\t index %u ref %d bind %d", p
->index
, p
->refcnt
,
243 if (tb
[TCA_SKBMOD_TM
]) {
244 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_SKBMOD_TM
]);
255 struct action_util skbmod_action_util
= {
257 .parse_aopt
= parse_skbmod
,
258 .print_aopt
= print_skbmod
,