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)
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
21 #include <linux/netdevice.h>
26 #include <linux/tc_act/tc_skbmod.h>
28 static void skbmod_explain(void)
31 "Usage:... skbmod {[set <SETTABLE>] [swap <SWAPABLE>]} [CONTROL] [index INDEX]\n"
32 "where SETTABLE is: [dmac DMAC] [smac SMAC] [etype ETYPE]\n"
33 "where SWAPABLE is: \"mac\" to swap mac addresses\n"
34 "note: \"swap mac\" is done after any outstanding D/SMAC change\n"
35 "\tDMAC := 6 byte Destination MAC address\n"
36 "\tSMAC := optional 6 byte Source MAC address\n"
37 "\tETYPE := optional 16 bit ethertype\n"
38 "\tCONTROL := reclassify | pipe | drop | continue | ok |\n"
39 "\t goto chain <CHAIN_INDEX>\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));
69 if (matches(*argv, "skbmod") == 0) {
72 } else if (matches(*argv, "swap") == 0) {
75 } else if (matches(*argv, "mac") == 0) {
76 p.flags |= SKBMOD_F_SWAPMAC;
78 } else if (matches(*argv, "set") == 0) {
81 } else if (matches(*argv, "etype") == 0) {
83 if (get_u16(&skbmod_etype, *argv, 0))
84 invarg("ethertype is invalid", *argv);
85 fprintf(stderr, "skbmod etype 0x%x\n", skbmod_etype);
86 p.flags |= SKBMOD_F_ETYPE;
88 } else if (matches(*argv, "dmac") == 0) {
91 if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
92 dbuf, dbuf + 1, dbuf + 2,
93 dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
94 fprintf(stderr, "Invalid dst mac address %s\n",
98 p.flags |= SKBMOD_F_DMAC;
99 fprintf(stderr, "dst MAC address <%s>\n", daddr);
102 } else if (matches(*argv, "smac") == 0) {
105 if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
106 sbuf, sbuf + 1, sbuf + 2,
107 sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
108 fprintf(stderr, "Invalid smac address %s\n",
112 p.flags |= SKBMOD_F_SMAC;
113 fprintf(stderr, "src MAC address <%s>\n", saddr);
115 } else if (matches(*argv, "help") == 0) {
125 parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
128 if (matches(*argv, "index") == 0) {
130 if (get_u32(&p.index, *argv, 0)) {
131 fprintf(stderr, "skbmod: Illegal \"index\"\n");
141 fprintf(stderr, "skbmod requires at least one option\n");
145 tail = addattr_nest(n, MAX_MSG, tca_id);
146 addattr_l(n, MAX_MSG, TCA_SKBMOD_PARMS, &p, sizeof(p));
149 addattr_l(n, MAX_MSG, TCA_SKBMOD_DMAC, dbuf, ETH_ALEN);
151 addattr16(n, MAX_MSG, TCA_SKBMOD_ETYPE, skbmod_etype);
153 addattr_l(n, MAX_MSG, TCA_SKBMOD_SMAC, sbuf, ETH_ALEN);
155 addattr_nest_end(n, tail);
162 static int print_skbmod(struct action_util *au, FILE *f, struct rtattr *arg)
165 struct rtattr *tb[TCA_SKBMOD_MAX + 1];
166 __u16 skbmod_etype = 0;
167 int has_optional = 0;
174 parse_rtattr_nested(tb, TCA_SKBMOD_MAX, arg);
176 if (tb[TCA_SKBMOD_PARMS] == NULL) {
177 fprintf(stderr, "Missing skbmod parameters\n");
181 p = RTA_DATA(tb[TCA_SKBMOD_PARMS]);
183 fprintf(f, "skbmod ");
184 print_action_control(f, "", p->action, " ");
186 if (tb[TCA_SKBMOD_ETYPE]) {
187 skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]);
189 fprintf(f, "set etype 0x%X ", skbmod_etype);
195 if (tb[TCA_SKBMOD_DMAC]) {
197 fprintf(f, "set dmac %s ",
198 ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_DMAC]),
199 RTA_PAYLOAD(tb[TCA_SKBMOD_DMAC]), 0, b1,
204 if (tb[TCA_SKBMOD_SMAC]) {
206 fprintf(f, "set smac %s ",
207 ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_SMAC]),
208 RTA_PAYLOAD(tb[TCA_SKBMOD_SMAC]), 0, b2,
212 if (p->flags & SKBMOD_F_SWAPMAC)
213 fprintf(f, "swap mac ");
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,