]> git.ipfire.org Git - thirdparty/iproute2.git/blob - tc/m_skbmod.c
tc: make action_util arg const
[thirdparty/iproute2.git] / tc / m_skbmod.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * m_skbmod.c skb modifier action module
4 *
5 * Authors: J Hadi Salim (jhs@mojatatu.com)
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16 #include <linux/netdevice.h>
17
18 #include "rt_names.h"
19 #include "utils.h"
20 #include "tc_util.h"
21 #include <linux/tc_act/tc_skbmod.h>
22
23 static void skbmod_explain(void)
24 {
25 fprintf(stderr,
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");
35 }
36
37 static void skbmod_usage(void)
38 {
39 skbmod_explain();
40 exit(-1);
41 }
42
43 static int parse_skbmod(const struct action_util *a, int *argc_p, char ***argv_p,
44 int tca_id, struct nlmsghdr *n)
45 {
46 int argc = *argc_p;
47 char **argv = *argv_p;
48 int ok = 0;
49 struct tc_skbmod p;
50 struct rtattr *tail;
51 char dbuf[ETH_ALEN];
52 char sbuf[ETH_ALEN];
53 __u16 skbmod_etype = 0;
54 char *daddr = NULL;
55 char *saddr = NULL;
56
57 memset(&p, 0, sizeof(p));
58
59 if (argc <= 0)
60 return -1;
61
62 while (argc > 0) {
63 if (matches(*argv, "skbmod") == 0) {
64 NEXT_ARG();
65 continue;
66 } else if (matches(*argv, "swap") == 0) {
67 NEXT_ARG();
68 continue;
69 } else if (matches(*argv, "mac") == 0) {
70 p.flags |= SKBMOD_F_SWAPMAC;
71 ok += 1;
72 } else if (matches(*argv, "set") == 0) {
73 NEXT_ARG();
74 continue;
75 } else if (matches(*argv, "etype") == 0) {
76 NEXT_ARG();
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;
81 ok += 1;
82 } else if (matches(*argv, "dmac") == 0) {
83 NEXT_ARG();
84 daddr = *argv;
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",
89 daddr);
90 return -1;
91 }
92 p.flags |= SKBMOD_F_DMAC;
93 fprintf(stderr, "dst MAC address <%s>\n", daddr);
94 ok += 1;
95
96 } else if (matches(*argv, "smac") == 0) {
97 NEXT_ARG();
98 saddr = *argv;
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",
103 saddr);
104 return -1;
105 }
106 p.flags |= SKBMOD_F_SMAC;
107 fprintf(stderr, "src MAC address <%s>\n", saddr);
108 ok += 1;
109 } else if (matches(*argv, "ecn") == 0) {
110 p.flags |= SKBMOD_F_ECN;
111 ok += 1;
112 } else if (matches(*argv, "help") == 0) {
113 skbmod_usage();
114 } else {
115 break;
116 }
117
118 argc--;
119 argv++;
120 }
121
122 parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
123
124 if (argc) {
125 if (matches(*argv, "index") == 0) {
126 NEXT_ARG();
127 if (get_u32(&p.index, *argv, 0)) {
128 fprintf(stderr, "skbmod: Illegal \"index\"\n");
129 return -1;
130 }
131 ok++;
132 argc--;
133 argv++;
134 }
135 }
136
137 if (!ok) {
138 fprintf(stderr, "skbmod requires at least one option\n");
139 skbmod_usage();
140 }
141
142 tail = addattr_nest(n, MAX_MSG, tca_id);
143 addattr_l(n, MAX_MSG, TCA_SKBMOD_PARMS, &p, sizeof(p));
144
145 if (daddr)
146 addattr_l(n, MAX_MSG, TCA_SKBMOD_DMAC, dbuf, ETH_ALEN);
147 if (skbmod_etype)
148 addattr16(n, MAX_MSG, TCA_SKBMOD_ETYPE, skbmod_etype);
149 if (saddr)
150 addattr_l(n, MAX_MSG, TCA_SKBMOD_SMAC, sbuf, ETH_ALEN);
151
152 addattr_nest_end(n, tail);
153
154 *argc_p = argc;
155 *argv_p = argv;
156 return 0;
157 }
158
159 static int print_skbmod(const struct action_util *au, FILE *f, struct rtattr *arg)
160 {
161 struct tc_skbmod *p;
162 struct rtattr *tb[TCA_SKBMOD_MAX + 1];
163 __u16 skbmod_etype = 0;
164 int has_optional = 0;
165 SPRINT_BUF(b1);
166 SPRINT_BUF(b2);
167
168 if (arg == NULL)
169 return 0;
170
171 parse_rtattr_nested(tb, TCA_SKBMOD_MAX, arg);
172
173 if (tb[TCA_SKBMOD_PARMS] == NULL) {
174 fprintf(stderr, "Missing skbmod parameters\n");
175 return -1;
176 }
177
178 p = RTA_DATA(tb[TCA_SKBMOD_PARMS]);
179
180 fprintf(f, "skbmod ");
181 print_action_control(f, "", p->action, " ");
182
183 if (tb[TCA_SKBMOD_ETYPE]) {
184 skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]);
185 has_optional = 1;
186 fprintf(f, "set etype 0x%X ", skbmod_etype);
187 }
188
189 if (has_optional)
190 fprintf(f, "\n\t ");
191
192 if (tb[TCA_SKBMOD_DMAC]) {
193 has_optional = 1;
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,
197 sizeof(b1)));
198
199 }
200
201 if (tb[TCA_SKBMOD_SMAC]) {
202 has_optional = 1;
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,
206 sizeof(b2)));
207 }
208
209 if (p->flags & SKBMOD_F_SWAPMAC)
210 fprintf(f, "swap mac ");
211
212 if (p->flags & SKBMOD_F_ECN)
213 fprintf(f, "ecn ");
214
215 fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
216 p->bindcnt);
217 if (show_stats) {
218 if (tb[TCA_SKBMOD_TM]) {
219 struct tcf_t *tm = RTA_DATA(tb[TCA_SKBMOD_TM]);
220
221 print_tm(f, tm);
222 }
223 }
224
225 fprintf(f, "\n");
226
227 return 0;
228 }
229
230 struct action_util skbmod_action_util = {
231 .id = "skbmod",
232 .parse_aopt = parse_skbmod,
233 .print_aopt = print_skbmod,
234 };