1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * m_vlan.c vlan manipulation module
5 * Authors: Jiri Pirko <jiri@resnulli.us>
12 #include <linux/if_ether.h>
16 #include <linux/tc_act/tc_vlan.h>
18 static const char * const action_names
[] = {
19 [TCA_VLAN_ACT_POP
] = "pop",
20 [TCA_VLAN_ACT_PUSH
] = "push",
21 [TCA_VLAN_ACT_MODIFY
] = "modify",
22 [TCA_VLAN_ACT_POP_ETH
] = "pop_eth",
23 [TCA_VLAN_ACT_PUSH_ETH
] = "push_eth",
26 static void explain(void)
29 "Usage: vlan pop [CONTROL]\n"
30 " vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
31 " vlan modify [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
32 " vlan pop_eth [CONTROL]\n"
33 " vlan push_eth dst_mac LLADDR src_mac LLADDR [CONTROL]\n"
34 " VLANPROTO is one of 802.1Q or 802.1AD\n"
35 " with default: 802.1Q\n"
36 " CONTROL := reclassify | pipe | drop | continue | pass |\n"
37 " goto chain <CHAIN_INDEX>\n");
40 static void usage(void)
46 static bool has_push_attribs(int action
)
48 return action
== TCA_VLAN_ACT_PUSH
|| action
== TCA_VLAN_ACT_MODIFY
;
51 static void unexpected(const char *arg
)
54 "unexpected \"%s\" - action already specified\n",
59 static int parse_vlan(const struct action_util
*a
, int *argc_p
, char ***argv_p
,
60 int tca_id
, struct nlmsghdr
*n
)
63 char **argv
= *argv_p
;
66 char dst_mac
[ETH_ALEN
] = {};
68 char src_mac
[ETH_ALEN
] = {};
76 struct tc_vlan parm
= {};
78 if (matches(*argv
, "vlan") != 0)
84 if (matches(*argv
, "pop") == 0) {
89 action
= TCA_VLAN_ACT_POP
;
90 } else if (matches(*argv
, "push") == 0) {
95 action
= TCA_VLAN_ACT_PUSH
;
96 } else if (matches(*argv
, "modify") == 0) {
101 action
= TCA_VLAN_ACT_MODIFY
;
102 } else if (matches(*argv
, "pop_eth") == 0) {
107 action
= TCA_VLAN_ACT_POP_ETH
;
108 } else if (matches(*argv
, "push_eth") == 0) {
113 action
= TCA_VLAN_ACT_PUSH_ETH
;
114 } else if (matches(*argv
, "id") == 0) {
115 if (!has_push_attribs(action
))
116 invarg("only valid for push/modify", *argv
);
119 if (get_u16(&id
, *argv
, 0))
120 invarg("id is invalid", *argv
);
122 } else if (matches(*argv
, "protocol") == 0) {
123 if (!has_push_attribs(action
))
124 invarg("only valid for push/modify", *argv
);
127 if (ll_proto_a2n(&proto
, *argv
))
128 invarg("protocol is invalid", *argv
);
130 } else if (matches(*argv
, "priority") == 0) {
131 if (!has_push_attribs(action
))
132 invarg("only valid for push/modify", *argv
);
135 if (get_u8(&prio
, *argv
, 0) || (prio
& ~0x7))
136 invarg("prio is invalid", *argv
);
138 } else if (matches(*argv
, "dst_mac") == 0) {
139 if (action
!= TCA_VLAN_ACT_PUSH_ETH
)
140 invarg("only valid for push_eth", *argv
);
143 if (ll_addr_a2n(dst_mac
, sizeof(dst_mac
), *argv
) < 0)
144 invarg("dst_mac is invalid", *argv
);
146 } else if (matches(*argv
, "src_mac") == 0) {
147 if (action
!= TCA_VLAN_ACT_PUSH_ETH
)
148 invarg("only valid for push_eth", *argv
);
151 if (ll_addr_a2n(src_mac
, sizeof(src_mac
), *argv
) < 0)
152 invarg("src_mac is invalid", *argv
);
154 } else if (matches(*argv
, "help") == 0) {
163 parse_action_control_dflt(&argc
, &argv
, &parm
.action
,
167 if (matches(*argv
, "index") == 0) {
169 if (get_u32(&parm
.index
, *argv
, 10)) {
170 fprintf(stderr
, "vlan: Illegal \"index\"\n");
178 if (has_push_attribs(action
) && !id_set
) {
179 fprintf(stderr
, "id needs to be set for %s\n",
180 action_names
[action
]);
185 if (action
== TCA_VLAN_ACT_PUSH_ETH
) {
187 fprintf(stderr
, "dst_mac needs to be set for %s\n",
188 action_names
[action
]);
191 } else if (!src_mac_set
) {
192 fprintf(stderr
, "src_mac needs to be set for %s\n",
193 action_names
[action
]);
199 parm
.v_action
= action
;
200 tail
= addattr_nest(n
, MAX_MSG
, tca_id
);
201 addattr_l(n
, MAX_MSG
, TCA_VLAN_PARMS
, &parm
, sizeof(parm
));
203 addattr_l(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_ID
, &id
, 2);
205 if (proto
!= htons(ETH_P_8021Q
) &&
206 proto
!= htons(ETH_P_8021AD
)) {
207 fprintf(stderr
, "protocol not supported\n");
212 addattr_l(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_PROTOCOL
, &proto
, 2);
215 addattr8(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_PRIORITY
, prio
);
217 addattr_l(n
, MAX_MSG
, TCA_VLAN_PUSH_ETH_DST
, dst_mac
,
220 addattr_l(n
, MAX_MSG
, TCA_VLAN_PUSH_ETH_SRC
, src_mac
,
223 addattr_nest_end(n
, tail
);
230 static int print_vlan(const struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
233 struct rtattr
*tb
[TCA_VLAN_MAX
+ 1];
235 struct tc_vlan
*parm
;
237 print_string(PRINT_ANY
, "kind", "%s ", "vlan");
241 parse_rtattr_nested(tb
, TCA_VLAN_MAX
, arg
);
243 if (!tb
[TCA_VLAN_PARMS
]) {
244 fprintf(stderr
, "Missing vlan parameters\n");
247 parm
= RTA_DATA(tb
[TCA_VLAN_PARMS
]);
249 print_string(PRINT_ANY
, "vlan_action", " %s",
250 action_names
[parm
->v_action
]);
252 switch (parm
->v_action
) {
253 case TCA_VLAN_ACT_PUSH
:
254 case TCA_VLAN_ACT_MODIFY
:
255 if (tb
[TCA_VLAN_PUSH_VLAN_ID
]) {
256 val
= rta_getattr_u16(tb
[TCA_VLAN_PUSH_VLAN_ID
]);
257 print_uint(PRINT_ANY
, "id", " id %u", val
);
259 if (tb
[TCA_VLAN_PUSH_VLAN_PROTOCOL
]) {
262 proto
= rta_getattr_u16(tb
[TCA_VLAN_PUSH_VLAN_PROTOCOL
]);
263 print_string(PRINT_ANY
, "protocol", " protocol %s",
264 ll_proto_n2a(proto
, b1
, sizeof(b1
)));
266 if (tb
[TCA_VLAN_PUSH_VLAN_PRIORITY
]) {
267 val
= rta_getattr_u8(tb
[TCA_VLAN_PUSH_VLAN_PRIORITY
]);
268 print_uint(PRINT_ANY
, "priority", " priority %u", val
);
271 case TCA_VLAN_ACT_PUSH_ETH
:
272 if (tb
[TCA_VLAN_PUSH_ETH_DST
] &&
273 RTA_PAYLOAD(tb
[TCA_VLAN_PUSH_ETH_DST
]) == ETH_ALEN
) {
274 ll_addr_n2a(RTA_DATA(tb
[TCA_VLAN_PUSH_ETH_DST
]),
275 ETH_ALEN
, 0, b1
, sizeof(b1
));
276 print_string(PRINT_ANY
, "dst_mac", " dst_mac %s", b1
);
278 if (tb
[TCA_VLAN_PUSH_ETH_SRC
] &&
279 RTA_PAYLOAD(tb
[TCA_VLAN_PUSH_ETH_SRC
]) == ETH_ALEN
) {
280 ll_addr_n2a(RTA_DATA(tb
[TCA_VLAN_PUSH_ETH_SRC
]),
281 ETH_ALEN
, 0, b1
, sizeof(b1
));
282 print_string(PRINT_ANY
, "src_mac", " src_mac %s", b1
);
285 print_action_control(f
, " ", parm
->action
, "");
288 print_uint(PRINT_ANY
, "index", "\t index %u", parm
->index
);
289 print_int(PRINT_ANY
, "ref", " ref %d", parm
->refcnt
);
290 print_int(PRINT_ANY
, "bind", " bind %d", parm
->bindcnt
);
293 if (tb
[TCA_VLAN_TM
]) {
294 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_VLAN_TM
]);
305 struct action_util vlan_action_util
= {
307 .parse_aopt
= parse_vlan
,
308 .print_aopt
= print_vlan
,