]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_skbmod.c
actions: add skbmod action
[mirror_iproute2.git] / tc / m_skbmod.c
CommitLineData
da651289
JHS
1/*
2 * m_skbmod.c skb modifier action module
3 *
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.
8 *
9 * Authors: J Hadi Salim (jhs@mojatatu.com)
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include <linux/netdevice.h>
23
24#include "rt_names.h"
25#include "utils.h"
26#include "tc_util.h"
27#include <linux/tc_act/tc_skbmod.h>
28
29static void skbmod_explain(void)
30{
31 fprintf(stderr,
32 "Usage:... skbmod {[set <SETTABLE>] [swap <SWAPABLE>]} [CONTROL] [index INDEX]\n");
33 fprintf(stderr, "where SETTABLE is: [dmac DMAC] [smac SMAC] [etype ETYPE] \n");
34 fprintf(stderr, "where SWAPABLE is: \"mac\" to swap mac addresses\n");
35 fprintf(stderr, "note: \"swap mac\" is done after any outstanding D/SMAC change\n");
36 fprintf(stderr,
37 "\tDMAC := 6 byte Destination MAC address\n"
38 "\tSMAC := optional 6 byte Source MAC address\n"
39 "\tETYPE := optional 16 bit ethertype\n"
40 "\tCONTROL := reclassify|pipe|drop|continue|ok\n"
41 "\tINDEX := skbmod index value to use\n");
42}
43
44static void skbmod_usage(void)
45{
46 skbmod_explain();
47 exit(-1);
48}
49
50static int parse_skbmod(struct action_util *a, int *argc_p, char ***argv_p,
51 int tca_id, struct nlmsghdr *n)
52{
53 int argc = *argc_p;
54 char **argv = *argv_p;
55 int ok = 0;
56 struct tc_skbmod p;
57 struct rtattr *tail;
58 char dbuf[ETH_ALEN];
59 char sbuf[ETH_ALEN];
60 __u16 skbmod_etype = 0;
61 char *daddr = NULL;
62 char *saddr = NULL;
63
64 memset(&p, 0, sizeof(p));
65 p.action = TC_ACT_PIPE; /* good default */
66
67 if (argc <= 0)
68 return -1;
69
70 while (argc > 0) {
71 if (matches(*argv, "skbmod") == 0) {
72 NEXT_ARG();
73 continue;
74 } else if (matches(*argv, "swap") == 0) {
75 NEXT_ARG();
76 continue;
77 } else if (matches(*argv, "mac") == 0) {
78 p.flags |= SKBMOD_F_SWAPMAC;
79 ok += 1;
80 } else if (matches(*argv, "set") == 0) {
81 NEXT_ARG();
82 continue;
83 } else if (matches(*argv, "etype") == 0) {
84 NEXT_ARG();
85 if (get_u16(&skbmod_etype, *argv, 0))
86 invarg("ethertype is invalid", *argv);
87 fprintf(stderr, "skbmod etype 0x%x\n", skbmod_etype);
88 p.flags |= SKBMOD_F_ETYPE;
89 ok += 1;
90 } else if (matches(*argv, "dmac") == 0) {
91 NEXT_ARG();
92 daddr = *argv;
93 if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
94 dbuf, dbuf + 1, dbuf + 2,
95 dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
96 fprintf(stderr, "Invalid dst mac address %s\n",
97 daddr);
98 return -1;
99 }
100 p.flags |= SKBMOD_F_DMAC;
101 fprintf(stderr, "dst MAC address <%s>\n", daddr);
102 ok += 1;
103
104 } else if (matches(*argv, "smac") == 0) {
105 NEXT_ARG();
106 saddr = *argv;
107 if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
108 sbuf, sbuf + 1, sbuf + 2,
109 sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
110 fprintf(stderr, "Invalid smac address %s\n",
111 saddr);
112 return -1;
113 }
114 p.flags |= SKBMOD_F_SMAC;
115 fprintf(stderr, "src MAC address <%s>\n", saddr);
116 ok += 1;
117 } else if (matches(*argv, "help") == 0) {
118 skbmod_usage();
119 } else {
120 break;
121 }
122
123 argc--;
124 argv++;
125 }
126
127 if (argc) {
128 if (matches(*argv, "reclassify") == 0) {
129 p.action = TC_ACT_RECLASSIFY;
130 argc--;
131 argv++;
132 } else if (matches(*argv, "pipe") == 0) {
133 p.action = TC_ACT_PIPE;
134 argc--;
135 argv++;
136 } else if (matches(*argv, "drop") == 0 ||
137 matches(*argv, "shot") == 0) {
138 p.action = TC_ACT_SHOT;
139 argc--;
140 argv++;
141 } else if (matches(*argv, "continue") == 0) {
142 p.action = TC_ACT_UNSPEC;
143 argc--;
144 argv++;
145 } else if (matches(*argv, "pass") == 0 ||
146 matches(*argv, "ok") == 0) {
147 p.action = TC_ACT_OK;
148 argc--;
149 argv++;
150 }
151 }
152
153 if (argc) {
154 if (matches(*argv, "index") == 0) {
155 NEXT_ARG();
156 if (get_u32(&p.index, *argv, 0)) {
157 fprintf(stderr, "skbmod: Illegal \"index\"\n");
158 return -1;
159 }
160 ok++;
161 argc--;
162 argv++;
163 }
164 }
165
166 if (!ok) {
167 fprintf(stderr, "skbmod requires at least one option\n");
168 skbmod_usage();
169 }
170
171 tail = NLMSG_TAIL(n);
172 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
173 addattr_l(n, MAX_MSG, TCA_SKBMOD_PARMS, &p, sizeof(p));
174
175 if (daddr)
176 addattr_l(n, MAX_MSG, TCA_SKBMOD_DMAC, dbuf, ETH_ALEN);
177 if (skbmod_etype)
178 addattr16(n, MAX_MSG, TCA_SKBMOD_ETYPE, skbmod_etype);
179 if (saddr)
180 addattr_l(n, MAX_MSG, TCA_SKBMOD_SMAC, sbuf, ETH_ALEN);
181
182 tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
183
184 *argc_p = argc;
185 *argv_p = argv;
186 return 0;
187}
188
189static int print_skbmod(struct action_util *au, FILE *f, struct rtattr *arg)
190{
191 struct tc_skbmod *p = NULL;
192 struct rtattr *tb[TCA_SKBMOD_MAX + 1];
193 __u16 skbmod_etype = 0;
194 int has_optional = 0;
195 SPRINT_BUF(b1);
196 SPRINT_BUF(b2);
197
198 if (arg == NULL)
199 return -1;
200
201 parse_rtattr_nested(tb, TCA_SKBMOD_MAX, arg);
202
203 if (tb[TCA_SKBMOD_PARMS] == NULL) {
204 fprintf(f, "[NULL skbmod parameters]");
205 return -1;
206 }
207
208 p = RTA_DATA(tb[TCA_SKBMOD_PARMS]);
209
210 fprintf(f, "skbmod action %s ", action_n2a(p->action));
211
212 if (tb[TCA_SKBMOD_ETYPE]) {
213 skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]);
214 has_optional = 1;
215 fprintf(f, "set etype 0x%X ", skbmod_etype);
216 }
217
218 if (has_optional)
219 fprintf(f, "\n\t ");
220
221 if (tb[TCA_SKBMOD_DMAC]) {
222 has_optional = 1;
223 fprintf(f, "set dmac %s ",
224 ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_DMAC]),
225 RTA_PAYLOAD(tb[TCA_SKBMOD_DMAC]), 0, b1,
226 sizeof(b1)));
227
228 }
229
230 if (tb[TCA_SKBMOD_SMAC]) {
231 has_optional = 1;
232 fprintf(f, "set smac %s ",
233 ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_SMAC]),
234 RTA_PAYLOAD(tb[TCA_SKBMOD_SMAC]), 0, b2,
235 sizeof(b2)));
236 }
237
238 if (p->flags & SKBMOD_F_SWAPMAC)
239 fprintf(f, "swap mac ");
240
241 fprintf(f, "\n\t index %d ref %d bind %d", p->index, p->refcnt,
242 p->bindcnt);
243 if (show_stats) {
244 if (tb[TCA_SKBMOD_TM]) {
245 struct tcf_t *tm = RTA_DATA(tb[TCA_SKBMOD_TM]);
246
247 print_tm(f, tm);
248 }
249 }
250
251 fprintf(f, "\n");
252
253 return 0;
254}
255
256struct action_util skbmod_action_util = {
257 .id = "skbmod",
258 .parse_aopt = parse_skbmod,
259 .print_aopt = print_skbmod,
260};