2 * m_vlan.c vlan manipulation module
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.
9 * Authors: Jiri Pirko <jiri@resnulli.us>
16 #include <linux/if_ether.h>
20 #include <linux/tc_act/tc_vlan.h>
22 static const char * const action_names
[] = {
23 [TCA_VLAN_ACT_POP
] = "pop",
24 [TCA_VLAN_ACT_PUSH
] = "push",
25 [TCA_VLAN_ACT_MODIFY
] = "modify",
28 static void explain(void)
30 fprintf(stderr
, "Usage: vlan pop\n");
31 fprintf(stderr
, " vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n");
32 fprintf(stderr
, " vlan modify [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n");
33 fprintf(stderr
, " VLANPROTO is one of 802.1Q or 802.1AD\n");
34 fprintf(stderr
, " with default: 802.1Q\n");
35 fprintf(stderr
, " CONTROL := reclassify | pipe | drop | continue | pass\n");
38 static void usage(void)
44 static bool has_push_attribs(int action
)
46 return action
== TCA_VLAN_ACT_PUSH
|| action
== TCA_VLAN_ACT_MODIFY
;
49 static int parse_vlan(struct action_util
*a
, int *argc_p
, char ***argv_p
,
50 int tca_id
, struct nlmsghdr
*n
)
53 char **argv
= *argv_p
;
62 struct tc_vlan parm
= {};
64 if (matches(*argv
, "vlan") != 0)
70 if (matches(*argv
, "pop") == 0) {
72 fprintf(stderr
, "unexpected \"%s\" - action already specified\n",
77 action
= TCA_VLAN_ACT_POP
;
78 } else if (matches(*argv
, "push") == 0) {
80 fprintf(stderr
, "unexpected \"%s\" - action already specified\n",
85 action
= TCA_VLAN_ACT_PUSH
;
86 } else if (matches(*argv
, "modify") == 0) {
88 fprintf(stderr
, "unexpected \"%s\" - action already specified\n",
93 action
= TCA_VLAN_ACT_MODIFY
;
94 } else if (matches(*argv
, "id") == 0) {
95 if (!has_push_attribs(action
)) {
96 fprintf(stderr
, "\"%s\" is only valid for push/modify\n",
102 if (get_u16(&id
, *argv
, 0))
103 invarg("id is invalid", *argv
);
105 } else if (matches(*argv
, "protocol") == 0) {
106 if (!has_push_attribs(action
)) {
107 fprintf(stderr
, "\"%s\" is only valid for push/modify\n",
113 if (ll_proto_a2n(&proto
, *argv
))
114 invarg("protocol is invalid", *argv
);
116 } else if (matches(*argv
, "priority") == 0) {
117 if (!has_push_attribs(action
)) {
118 fprintf(stderr
, "\"%s\" is only valid for push/modify\n",
124 if (get_u8(&prio
, *argv
, 0) || (prio
& ~0x7))
125 invarg("prio is invalid", *argv
);
127 } else if (matches(*argv
, "help") == 0) {
136 parse_action_control_dflt(&argc
, &argv
, &parm
.action
,
140 if (matches(*argv
, "index") == 0) {
142 if (get_u32(&parm
.index
, *argv
, 10)) {
143 fprintf(stderr
, "vlan: Illegal \"index\"\n");
151 if (has_push_attribs(action
) && !id_set
) {
152 fprintf(stderr
, "id needs to be set for %s\n",
153 action_names
[action
]);
158 parm
.v_action
= action
;
159 tail
= NLMSG_TAIL(n
);
160 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
161 addattr_l(n
, MAX_MSG
, TCA_VLAN_PARMS
, &parm
, sizeof(parm
));
163 addattr_l(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_ID
, &id
, 2);
165 if (proto
!= htons(ETH_P_8021Q
) &&
166 proto
!= htons(ETH_P_8021AD
)) {
167 fprintf(stderr
, "protocol not supported\n");
172 addattr_l(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_PROTOCOL
, &proto
, 2);
175 addattr8(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_PRIORITY
, prio
);
177 tail
->rta_len
= (char *)NLMSG_TAIL(n
) - (char *)tail
;
184 static int print_vlan(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
187 struct rtattr
*tb
[TCA_VLAN_MAX
+ 1];
189 struct tc_vlan
*parm
;
194 parse_rtattr_nested(tb
, TCA_VLAN_MAX
, arg
);
196 if (!tb
[TCA_VLAN_PARMS
]) {
197 fprintf(f
, "[NULL vlan parameters]");
200 parm
= RTA_DATA(tb
[TCA_VLAN_PARMS
]);
204 switch (parm
->v_action
) {
205 case TCA_VLAN_ACT_POP
:
208 case TCA_VLAN_ACT_PUSH
:
209 case TCA_VLAN_ACT_MODIFY
:
210 fprintf(f
, " %s", action_names
[parm
->v_action
]);
211 if (tb
[TCA_VLAN_PUSH_VLAN_ID
]) {
212 val
= rta_getattr_u16(tb
[TCA_VLAN_PUSH_VLAN_ID
]);
213 fprintf(f
, " id %u", val
);
215 if (tb
[TCA_VLAN_PUSH_VLAN_PROTOCOL
]) {
216 fprintf(f
, " protocol %s",
217 ll_proto_n2a(rta_getattr_u16(tb
[TCA_VLAN_PUSH_VLAN_PROTOCOL
]),
220 if (tb
[TCA_VLAN_PUSH_VLAN_PRIORITY
]) {
221 val
= rta_getattr_u8(tb
[TCA_VLAN_PUSH_VLAN_PRIORITY
]);
222 fprintf(f
, " priority %u", val
);
226 print_action_control(f
, " ", parm
->action
, "");
228 fprintf(f
, "\n\t index %u ref %d bind %d", parm
->index
, parm
->refcnt
,
232 if (tb
[TCA_VLAN_TM
]) {
233 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_VLAN_TM
]);
244 struct action_util vlan_action_util
= {
246 .parse_aopt
= parse_vlan
,
247 .print_aopt
= print_vlan
,