]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_vlan.c
Merge branch 'main' into next
[mirror_iproute2.git] / tc / m_vlan.c
CommitLineData
8b1c0216
JP
1/*
2 * m_vlan.c vlan manipulation module
3 *
4 * This program is free software; you can redistribute 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: Jiri Pirko <jiri@resnulli.us>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <string.h>
16#include <linux/if_ether.h>
17#include "utils.h"
18#include "rt_names.h"
19#include "tc_util.h"
20#include <linux/tc_act/tc_vlan.h>
21
4654173e
SL
22static const char * const action_names[] = {
23 [TCA_VLAN_ACT_POP] = "pop",
24 [TCA_VLAN_ACT_PUSH] = "push",
25 [TCA_VLAN_ACT_MODIFY] = "modify",
d61167dd
GN
26 [TCA_VLAN_ACT_POP_ETH] = "pop_eth",
27 [TCA_VLAN_ACT_PUSH_ETH] = "push_eth",
4654173e
SL
28};
29
8b1c0216
JP
30static void explain(void)
31{
eb4bccf1 32 fprintf(stderr,
7c7a0fe0 33 "Usage: vlan pop [CONTROL]\n"
eb4bccf1
SH
34 " vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
35 " vlan modify [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
d61167dd
GN
36 " vlan pop_eth [CONTROL]\n"
37 " vlan push_eth dst_mac LLADDR src_mac LLADDR [CONTROL]\n"
eb4bccf1
SH
38 " VLANPROTO is one of 802.1Q or 802.1AD\n"
39 " with default: 802.1Q\n"
40 " CONTROL := reclassify | pipe | drop | continue | pass |\n"
41 " goto chain <CHAIN_INDEX>\n");
8b1c0216
JP
42}
43
44static void usage(void)
45{
46 explain();
47 exit(-1);
48}
49
4654173e
SL
50static bool has_push_attribs(int action)
51{
52 return action == TCA_VLAN_ACT_PUSH || action == TCA_VLAN_ACT_MODIFY;
53}
54
eb4bccf1
SH
55static void unexpected(const char *arg)
56{
57 fprintf(stderr,
58 "unexpected \"%s\" - action already specified\n",
59 arg);
60 explain();
61}
62
8b1c0216
JP
63static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
64 int tca_id, struct nlmsghdr *n)
65{
66 int argc = *argc_p;
67 char **argv = *argv_p;
68 struct rtattr *tail;
69 int action = 0;
d61167dd
GN
70 char dst_mac[ETH_ALEN] = {};
71 int dst_mac_set = 0;
72 char src_mac[ETH_ALEN] = {};
73 int src_mac_set = 0;
8b1c0216
JP
74 __u16 id;
75 int id_set = 0;
76 __u16 proto;
77 int proto_set = 0;
0e43ed9d
HHZ
78 __u8 prio;
79 int prio_set = 0;
e67aba55 80 struct tc_vlan parm = {};
8b1c0216
JP
81
82 if (matches(*argv, "vlan") != 0)
83 return -1;
84
85 NEXT_ARG();
86
87 while (argc > 0) {
88 if (matches(*argv, "pop") == 0) {
89 if (action) {
eb4bccf1 90 unexpected(*argv);
8b1c0216
JP
91 return -1;
92 }
93 action = TCA_VLAN_ACT_POP;
94 } else if (matches(*argv, "push") == 0) {
95 if (action) {
eb4bccf1 96 unexpected(*argv);
8b1c0216
JP
97 return -1;
98 }
99 action = TCA_VLAN_ACT_PUSH;
4654173e
SL
100 } else if (matches(*argv, "modify") == 0) {
101 if (action) {
eb4bccf1 102 unexpected(*argv);
4654173e
SL
103 return -1;
104 }
105 action = TCA_VLAN_ACT_MODIFY;
d61167dd
GN
106 } else if (matches(*argv, "pop_eth") == 0) {
107 if (action) {
108 unexpected(*argv);
109 return -1;
110 }
111 action = TCA_VLAN_ACT_POP_ETH;
112 } else if (matches(*argv, "push_eth") == 0) {
113 if (action) {
114 unexpected(*argv);
115 return -1;
116 }
117 action = TCA_VLAN_ACT_PUSH_ETH;
8b1c0216 118 } else if (matches(*argv, "id") == 0) {
eb4bccf1
SH
119 if (!has_push_attribs(action))
120 invarg("only valid for push/modify", *argv);
121
8b1c0216
JP
122 NEXT_ARG();
123 if (get_u16(&id, *argv, 0))
124 invarg("id is invalid", *argv);
125 id_set = 1;
126 } else if (matches(*argv, "protocol") == 0) {
eb4bccf1
SH
127 if (!has_push_attribs(action))
128 invarg("only valid for push/modify", *argv);
129
8b1c0216
JP
130 NEXT_ARG();
131 if (ll_proto_a2n(&proto, *argv))
132 invarg("protocol is invalid", *argv);
133 proto_set = 1;
0e43ed9d 134 } else if (matches(*argv, "priority") == 0) {
eb4bccf1
SH
135 if (!has_push_attribs(action))
136 invarg("only valid for push/modify", *argv);
137
0e43ed9d
HHZ
138 NEXT_ARG();
139 if (get_u8(&prio, *argv, 0) || (prio & ~0x7))
140 invarg("prio is invalid", *argv);
141 prio_set = 1;
d61167dd
GN
142 } else if (matches(*argv, "dst_mac") == 0) {
143 if (action != TCA_VLAN_ACT_PUSH_ETH)
144 invarg("only valid for push_eth", *argv);
145
146 NEXT_ARG();
147 if (ll_addr_a2n(dst_mac, sizeof(dst_mac), *argv) < 0)
148 invarg("dst_mac is invalid", *argv);
149 dst_mac_set = 1;
150 } else if (matches(*argv, "src_mac") == 0) {
151 if (action != TCA_VLAN_ACT_PUSH_ETH)
152 invarg("only valid for push_eth", *argv);
153
154 NEXT_ARG();
155 if (ll_addr_a2n(src_mac, sizeof(src_mac), *argv) < 0)
156 invarg("src_mac is invalid", *argv);
157 src_mac_set = 1;
8b1c0216
JP
158 } else if (matches(*argv, "help") == 0) {
159 usage();
160 } else {
161 break;
162 }
163 argc--;
164 argv++;
165 }
166
e67aba55
JP
167 parse_action_control_dflt(&argc, &argv, &parm.action,
168 false, TC_ACT_PIPE);
8b1c0216
JP
169
170 if (argc) {
171 if (matches(*argv, "index") == 0) {
172 NEXT_ARG();
173 if (get_u32(&parm.index, *argv, 10)) {
174 fprintf(stderr, "vlan: Illegal \"index\"\n");
175 return -1;
176 }
177 argc--;
178 argv++;
179 }
180 }
181
4654173e
SL
182 if (has_push_attribs(action) && !id_set) {
183 fprintf(stderr, "id needs to be set for %s\n",
184 action_names[action]);
8b1c0216
JP
185 explain();
186 return -1;
187 }
188
d61167dd
GN
189 if (action == TCA_VLAN_ACT_PUSH_ETH) {
190 if (!dst_mac_set) {
191 fprintf(stderr, "dst_mac needs to be set for %s\n",
192 action_names[action]);
193 explain();
194 return -1;
195 } else if (!src_mac_set) {
196 fprintf(stderr, "src_mac needs to be set for %s\n",
197 action_names[action]);
198 explain();
199 return -1;
200 }
201 }
202
8b1c0216 203 parm.v_action = action;
c14f9d92 204 tail = addattr_nest(n, MAX_MSG, tca_id);
8b1c0216
JP
205 addattr_l(n, MAX_MSG, TCA_VLAN_PARMS, &parm, sizeof(parm));
206 if (id_set)
207 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_ID, &id, 2);
208 if (proto_set) {
209 if (proto != htons(ETH_P_8021Q) &&
210 proto != htons(ETH_P_8021AD)) {
211 fprintf(stderr, "protocol not supported\n");
212 explain();
213 return -1;
214 }
215
216 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PROTOCOL, &proto, 2);
217 }
0e43ed9d
HHZ
218 if (prio_set)
219 addattr8(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PRIORITY, prio);
d61167dd
GN
220 if (dst_mac_set)
221 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_ETH_DST, dst_mac,
222 sizeof(dst_mac));
223 if (src_mac_set)
224 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_ETH_SRC, src_mac,
225 sizeof(src_mac));
0e43ed9d 226
c14f9d92 227 addattr_nest_end(n, tail);
8b1c0216
JP
228
229 *argc_p = argc;
230 *argv_p = argv;
231 return 0;
232}
233
234static int print_vlan(struct action_util *au, FILE *f, struct rtattr *arg)
235{
236 SPRINT_BUF(b1);
237 struct rtattr *tb[TCA_VLAN_MAX + 1];
238 __u16 val;
239 struct tc_vlan *parm;
240
a99ebeee 241 print_string(PRINT_ANY, "kind", "%s ", "vlan");
8b1c0216 242 if (arg == NULL)
a99ebeee 243 return 0;
8b1c0216
JP
244
245 parse_rtattr_nested(tb, TCA_VLAN_MAX, arg);
246
247 if (!tb[TCA_VLAN_PARMS]) {
7c7a0fe0 248 fprintf(stderr, "Missing vlan parameters\n");
8b1c0216
JP
249 return -1;
250 }
251 parm = RTA_DATA(tb[TCA_VLAN_PARMS]);
252
eb4bccf1
SH
253 print_string(PRINT_ANY, "vlan_action", " %s",
254 action_names[parm->v_action]);
8b1c0216 255
32a121cb 256 switch (parm->v_action) {
8b1c0216 257 case TCA_VLAN_ACT_PUSH:
4654173e 258 case TCA_VLAN_ACT_MODIFY:
8b1c0216
JP
259 if (tb[TCA_VLAN_PUSH_VLAN_ID]) {
260 val = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
b021ee40 261 print_uint(PRINT_ANY, "id", " id %u", val);
8b1c0216
JP
262 }
263 if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
eb4bccf1
SH
264 __u16 proto;
265
266 proto = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
b021ee40 267 print_string(PRINT_ANY, "protocol", " protocol %s",
eb4bccf1 268 ll_proto_n2a(proto, b1, sizeof(b1)));
8b1c0216 269 }
0e43ed9d
HHZ
270 if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) {
271 val = rta_getattr_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
b021ee40 272 print_uint(PRINT_ANY, "priority", " priority %u", val);
0e43ed9d 273 }
8b1c0216 274 break;
d61167dd
GN
275 case TCA_VLAN_ACT_PUSH_ETH:
276 if (tb[TCA_VLAN_PUSH_ETH_DST] &&
277 RTA_PAYLOAD(tb[TCA_VLAN_PUSH_ETH_DST]) == ETH_ALEN) {
278 ll_addr_n2a(RTA_DATA(tb[TCA_VLAN_PUSH_ETH_DST]),
279 ETH_ALEN, 0, b1, sizeof(b1));
280 print_string(PRINT_ANY, "dst_mac", " dst_mac %s", b1);
281 }
282 if (tb[TCA_VLAN_PUSH_ETH_SRC &&
283 RTA_PAYLOAD(tb[TCA_VLAN_PUSH_ETH_SRC]) == ETH_ALEN]) {
284 ll_addr_n2a(RTA_DATA(tb[TCA_VLAN_PUSH_ETH_SRC]),
285 ETH_ALEN, 0, b1, sizeof(b1));
286 print_string(PRINT_ANY, "src_mac", " src_mac %s", b1);
287 }
8b1c0216 288 }
e67aba55 289 print_action_control(f, " ", parm->action, "");
8b1c0216 290
7b0d424a
SH
291 print_nl();
292 print_uint(PRINT_ANY, "index", "\t index %u", parm->index);
b021ee40
JP
293 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
294 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
8b1c0216
JP
295
296 if (show_stats) {
297 if (tb[TCA_VLAN_TM]) {
298 struct tcf_t *tm = RTA_DATA(tb[TCA_VLAN_TM]);
32a121cb 299
8b1c0216
JP
300 print_tm(f, tm);
301 }
302 }
303
7b0d424a 304 print_nl();
8b1c0216
JP
305
306 return 0;
307}
308
309struct action_util vlan_action_util = {
310 .id = "vlan",
311 .parse_aopt = parse_vlan,
312 .print_aopt = print_vlan,
313};