1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * m_skbmod.c skb modifier action module
5 * Authors: J Hadi Salim (jhs@mojatatu.com)
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
16 #include <linux/netdevice.h>
21 #include <linux/tc_act/tc_skbmod.h>
23 static void skbmod_explain(void)
26 "Usage:... skbmod { set <SETTABLE> | swap <SWAPPABLE> | ecn } [CONTROL] [index INDEX]\n"
27 "where SETTABLE is: [dmac DMAC] [smac SMAC] [etype ETYPE]\n"
28 "where SWAPPABLE is: \"mac\" to swap mac addresses\n"
29 "\tDMAC := 6 byte Destination MAC address\n"
30 "\tSMAC := optional 6 byte Source MAC address\n"
31 "\tETYPE := optional 16 bit ethertype\n"
32 "\tCONTROL := reclassify | pipe | drop | continue | ok |\n"
33 "\t goto chain <CHAIN_INDEX>\n"
34 "\tINDEX := skbmod index value to use\n");
37 static void skbmod_usage(void)
43 static int parse_skbmod(const struct action_util
*a
, int *argc_p
, char ***argv_p
,
44 int tca_id
, struct nlmsghdr
*n
)
47 char **argv
= *argv_p
;
53 __u16 skbmod_etype
= 0;
57 memset(&p
, 0, sizeof(p
));
63 if (matches(*argv
, "skbmod") == 0) {
66 } else if (matches(*argv
, "swap") == 0) {
69 } else if (matches(*argv
, "mac") == 0) {
70 p
.flags
|= SKBMOD_F_SWAPMAC
;
72 } else if (matches(*argv
, "set") == 0) {
75 } else if (matches(*argv
, "etype") == 0) {
77 if (get_u16(&skbmod_etype
, *argv
, 0))
78 invarg("ethertype is invalid", *argv
);
79 fprintf(stderr
, "skbmod etype 0x%x\n", skbmod_etype
);
80 p
.flags
|= SKBMOD_F_ETYPE
;
82 } else if (matches(*argv
, "dmac") == 0) {
85 if (sscanf(daddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
86 dbuf
, dbuf
+ 1, dbuf
+ 2,
87 dbuf
+ 3, dbuf
+ 4, dbuf
+ 5) != 6) {
88 fprintf(stderr
, "Invalid dst mac address %s\n",
92 p
.flags
|= SKBMOD_F_DMAC
;
93 fprintf(stderr
, "dst MAC address <%s>\n", daddr
);
96 } else if (matches(*argv
, "smac") == 0) {
99 if (sscanf(saddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
100 sbuf
, sbuf
+ 1, sbuf
+ 2,
101 sbuf
+ 3, sbuf
+ 4, sbuf
+ 5) != 6) {
102 fprintf(stderr
, "Invalid smac address %s\n",
106 p
.flags
|= SKBMOD_F_SMAC
;
107 fprintf(stderr
, "src MAC address <%s>\n", saddr
);
109 } else if (matches(*argv
, "ecn") == 0) {
110 p
.flags
|= SKBMOD_F_ECN
;
112 } else if (matches(*argv
, "help") == 0) {
122 parse_action_control_dflt(&argc
, &argv
, &p
.action
, false, TC_ACT_PIPE
);
125 if (matches(*argv
, "index") == 0) {
127 if (get_u32(&p
.index
, *argv
, 0)) {
128 fprintf(stderr
, "skbmod: Illegal \"index\"\n");
138 fprintf(stderr
, "skbmod requires at least one option\n");
142 tail
= addattr_nest(n
, MAX_MSG
, tca_id
);
143 addattr_l(n
, MAX_MSG
, TCA_SKBMOD_PARMS
, &p
, sizeof(p
));
146 addattr_l(n
, MAX_MSG
, TCA_SKBMOD_DMAC
, dbuf
, ETH_ALEN
);
148 addattr16(n
, MAX_MSG
, TCA_SKBMOD_ETYPE
, skbmod_etype
);
150 addattr_l(n
, MAX_MSG
, TCA_SKBMOD_SMAC
, sbuf
, ETH_ALEN
);
152 addattr_nest_end(n
, tail
);
159 static int print_skbmod(const struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
162 struct rtattr
*tb
[TCA_SKBMOD_MAX
+ 1];
163 __u16 skbmod_etype
= 0;
164 int has_optional
= 0;
171 parse_rtattr_nested(tb
, TCA_SKBMOD_MAX
, arg
);
173 if (tb
[TCA_SKBMOD_PARMS
] == NULL
) {
174 fprintf(stderr
, "Missing skbmod parameters\n");
178 p
= RTA_DATA(tb
[TCA_SKBMOD_PARMS
]);
180 fprintf(f
, "skbmod ");
181 print_action_control(f
, "", p
->action
, " ");
183 if (tb
[TCA_SKBMOD_ETYPE
]) {
184 skbmod_etype
= rta_getattr_u16(tb
[TCA_SKBMOD_ETYPE
]);
186 fprintf(f
, "set etype 0x%X ", skbmod_etype
);
192 if (tb
[TCA_SKBMOD_DMAC
]) {
194 fprintf(f
, "set dmac %s ",
195 ll_addr_n2a(RTA_DATA(tb
[TCA_SKBMOD_DMAC
]),
196 RTA_PAYLOAD(tb
[TCA_SKBMOD_DMAC
]), 0, b1
,
201 if (tb
[TCA_SKBMOD_SMAC
]) {
203 fprintf(f
, "set smac %s ",
204 ll_addr_n2a(RTA_DATA(tb
[TCA_SKBMOD_SMAC
]),
205 RTA_PAYLOAD(tb
[TCA_SKBMOD_SMAC
]), 0, b2
,
209 if (p
->flags
& SKBMOD_F_SWAPMAC
)
210 fprintf(f
, "swap mac ");
212 if (p
->flags
& SKBMOD_F_ECN
)
215 fprintf(f
, "\n\t index %u ref %d bind %d", p
->index
, p
->refcnt
,
218 if (tb
[TCA_SKBMOD_TM
]) {
219 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_SKBMOD_TM
]);
230 struct action_util skbmod_action_util
= {
232 .parse_aopt
= parse_skbmod
,
233 .print_aopt
= print_skbmod
,