]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_skbmod.c
tc_filter: add support for chain index
[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,
557b7054
SH
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"
da651289
JHS
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");
41}
42
43static void skbmod_usage(void)
44{
45 skbmod_explain();
46 exit(-1);
47}
48
49static int parse_skbmod(struct action_util *a, int *argc_p, char ***argv_p,
50 int tca_id, struct nlmsghdr *n)
51{
52 int argc = *argc_p;
53 char **argv = *argv_p;
54 int ok = 0;
55 struct tc_skbmod p;
56 struct rtattr *tail;
57 char dbuf[ETH_ALEN];
58 char sbuf[ETH_ALEN];
59 __u16 skbmod_etype = 0;
60 char *daddr = NULL;
61 char *saddr = NULL;
62
63 memset(&p, 0, sizeof(p));
64 p.action = TC_ACT_PIPE; /* good default */
65
66 if (argc <= 0)
67 return -1;
68
69 while (argc > 0) {
70 if (matches(*argv, "skbmod") == 0) {
71 NEXT_ARG();
72 continue;
73 } else if (matches(*argv, "swap") == 0) {
74 NEXT_ARG();
75 continue;
76 } else if (matches(*argv, "mac") == 0) {
77 p.flags |= SKBMOD_F_SWAPMAC;
78 ok += 1;
79 } else if (matches(*argv, "set") == 0) {
80 NEXT_ARG();
81 continue;
82 } else if (matches(*argv, "etype") == 0) {
83 NEXT_ARG();
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;
88 ok += 1;
89 } else if (matches(*argv, "dmac") == 0) {
90 NEXT_ARG();
91 daddr = *argv;
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",
96 daddr);
97 return -1;
98 }
99 p.flags |= SKBMOD_F_DMAC;
100 fprintf(stderr, "dst MAC address <%s>\n", daddr);
101 ok += 1;
102
103 } else if (matches(*argv, "smac") == 0) {
104 NEXT_ARG();
105 saddr = *argv;
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",
110 saddr);
111 return -1;
112 }
113 p.flags |= SKBMOD_F_SMAC;
114 fprintf(stderr, "src MAC address <%s>\n", saddr);
115 ok += 1;
116 } else if (matches(*argv, "help") == 0) {
117 skbmod_usage();
118 } else {
119 break;
120 }
121
122 argc--;
123 argv++;
124 }
125
126 if (argc) {
127 if (matches(*argv, "reclassify") == 0) {
128 p.action = TC_ACT_RECLASSIFY;
129 argc--;
130 argv++;
131 } else if (matches(*argv, "pipe") == 0) {
132 p.action = TC_ACT_PIPE;
133 argc--;
134 argv++;
135 } else if (matches(*argv, "drop") == 0 ||
136 matches(*argv, "shot") == 0) {
137 p.action = TC_ACT_SHOT;
138 argc--;
139 argv++;
140 } else if (matches(*argv, "continue") == 0) {
141 p.action = TC_ACT_UNSPEC;
142 argc--;
143 argv++;
144 } else if (matches(*argv, "pass") == 0 ||
145 matches(*argv, "ok") == 0) {
146 p.action = TC_ACT_OK;
147 argc--;
148 argv++;
149 }
150 }
151
152 if (argc) {
153 if (matches(*argv, "index") == 0) {
154 NEXT_ARG();
155 if (get_u32(&p.index, *argv, 0)) {
156 fprintf(stderr, "skbmod: Illegal \"index\"\n");
157 return -1;
158 }
159 ok++;
160 argc--;
161 argv++;
162 }
163 }
164
165 if (!ok) {
166 fprintf(stderr, "skbmod requires at least one option\n");
167 skbmod_usage();
168 }
169
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));
173
174 if (daddr)
175 addattr_l(n, MAX_MSG, TCA_SKBMOD_DMAC, dbuf, ETH_ALEN);
176 if (skbmod_etype)
177 addattr16(n, MAX_MSG, TCA_SKBMOD_ETYPE, skbmod_etype);
178 if (saddr)
179 addattr_l(n, MAX_MSG, TCA_SKBMOD_SMAC, sbuf, ETH_ALEN);
180
181 tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
182
183 *argc_p = argc;
184 *argv_p = argv;
185 return 0;
186}
187
188static int print_skbmod(struct action_util *au, FILE *f, struct rtattr *arg)
189{
190 struct tc_skbmod *p = NULL;
191 struct rtattr *tb[TCA_SKBMOD_MAX + 1];
192 __u16 skbmod_etype = 0;
193 int has_optional = 0;
194 SPRINT_BUF(b1);
195 SPRINT_BUF(b2);
196
197 if (arg == NULL)
198 return -1;
199
200 parse_rtattr_nested(tb, TCA_SKBMOD_MAX, arg);
201
202 if (tb[TCA_SKBMOD_PARMS] == NULL) {
203 fprintf(f, "[NULL skbmod parameters]");
204 return -1;
205 }
206
207 p = RTA_DATA(tb[TCA_SKBMOD_PARMS]);
208
209 fprintf(f, "skbmod action %s ", action_n2a(p->action));
210
211 if (tb[TCA_SKBMOD_ETYPE]) {
212 skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]);
213 has_optional = 1;
214 fprintf(f, "set etype 0x%X ", skbmod_etype);
215 }
216
217 if (has_optional)
218 fprintf(f, "\n\t ");
219
220 if (tb[TCA_SKBMOD_DMAC]) {
221 has_optional = 1;
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,
225 sizeof(b1)));
226
227 }
228
229 if (tb[TCA_SKBMOD_SMAC]) {
230 has_optional = 1;
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,
234 sizeof(b2)));
235 }
236
237 if (p->flags & SKBMOD_F_SWAPMAC)
238 fprintf(f, "swap mac ");
239
53075318 240 fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
da651289
JHS
241 p->bindcnt);
242 if (show_stats) {
243 if (tb[TCA_SKBMOD_TM]) {
244 struct tcf_t *tm = RTA_DATA(tb[TCA_SKBMOD_TM]);
245
246 print_tm(f, tm);
247 }
248 }
249
250 fprintf(f, "\n");
251
252 return 0;
253}
254
255struct action_util skbmod_action_util = {
256 .id = "skbmod",
257 .parse_aopt = parse_skbmod,
258 .print_aopt = print_skbmod,
259};