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");
36 fprintf(stderr
, " goto chain <CHAIN_INDEX>\n");
39 static void usage(void)
45 static bool has_push_attribs(int action
)
47 return action
== TCA_VLAN_ACT_PUSH
|| action
== TCA_VLAN_ACT_MODIFY
;
50 static int parse_vlan(struct action_util
*a
, int *argc_p
, char ***argv_p
,
51 int tca_id
, struct nlmsghdr
*n
)
54 char **argv
= *argv_p
;
63 struct tc_vlan parm
= {};
65 if (matches(*argv
, "vlan") != 0)
71 if (matches(*argv
, "pop") == 0) {
73 fprintf(stderr
, "unexpected \"%s\" - action already specified\n",
78 action
= TCA_VLAN_ACT_POP
;
79 } else if (matches(*argv
, "push") == 0) {
81 fprintf(stderr
, "unexpected \"%s\" - action already specified\n",
86 action
= TCA_VLAN_ACT_PUSH
;
87 } else if (matches(*argv
, "modify") == 0) {
89 fprintf(stderr
, "unexpected \"%s\" - action already specified\n",
94 action
= TCA_VLAN_ACT_MODIFY
;
95 } else if (matches(*argv
, "id") == 0) {
96 if (!has_push_attribs(action
)) {
97 fprintf(stderr
, "\"%s\" is only valid for push/modify\n",
103 if (get_u16(&id
, *argv
, 0))
104 invarg("id is invalid", *argv
);
106 } else if (matches(*argv
, "protocol") == 0) {
107 if (!has_push_attribs(action
)) {
108 fprintf(stderr
, "\"%s\" is only valid for push/modify\n",
114 if (ll_proto_a2n(&proto
, *argv
))
115 invarg("protocol is invalid", *argv
);
117 } else if (matches(*argv
, "priority") == 0) {
118 if (!has_push_attribs(action
)) {
119 fprintf(stderr
, "\"%s\" is only valid for push/modify\n",
125 if (get_u8(&prio
, *argv
, 0) || (prio
& ~0x7))
126 invarg("prio is invalid", *argv
);
128 } else if (matches(*argv
, "help") == 0) {
137 parse_action_control_dflt(&argc
, &argv
, &parm
.action
,
141 if (matches(*argv
, "index") == 0) {
143 if (get_u32(&parm
.index
, *argv
, 10)) {
144 fprintf(stderr
, "vlan: Illegal \"index\"\n");
152 if (has_push_attribs(action
) && !id_set
) {
153 fprintf(stderr
, "id needs to be set for %s\n",
154 action_names
[action
]);
159 parm
.v_action
= action
;
160 tail
= NLMSG_TAIL(n
);
161 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
162 addattr_l(n
, MAX_MSG
, TCA_VLAN_PARMS
, &parm
, sizeof(parm
));
164 addattr_l(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_ID
, &id
, 2);
166 if (proto
!= htons(ETH_P_8021Q
) &&
167 proto
!= htons(ETH_P_8021AD
)) {
168 fprintf(stderr
, "protocol not supported\n");
173 addattr_l(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_PROTOCOL
, &proto
, 2);
176 addattr8(n
, MAX_MSG
, TCA_VLAN_PUSH_VLAN_PRIORITY
, prio
);
178 tail
->rta_len
= (char *)NLMSG_TAIL(n
) - (char *)tail
;
185 static int print_vlan(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
188 struct rtattr
*tb
[TCA_VLAN_MAX
+ 1];
190 struct tc_vlan
*parm
;
195 parse_rtattr_nested(tb
, TCA_VLAN_MAX
, arg
);
197 if (!tb
[TCA_VLAN_PARMS
]) {
198 fprintf(f
, "[NULL vlan parameters]");
201 parm
= RTA_DATA(tb
[TCA_VLAN_PARMS
]);
205 switch (parm
->v_action
) {
206 case TCA_VLAN_ACT_POP
:
209 case TCA_VLAN_ACT_PUSH
:
210 case TCA_VLAN_ACT_MODIFY
:
211 fprintf(f
, " %s", action_names
[parm
->v_action
]);
212 if (tb
[TCA_VLAN_PUSH_VLAN_ID
]) {
213 val
= rta_getattr_u16(tb
[TCA_VLAN_PUSH_VLAN_ID
]);
214 fprintf(f
, " id %u", val
);
216 if (tb
[TCA_VLAN_PUSH_VLAN_PROTOCOL
]) {
217 fprintf(f
, " protocol %s",
218 ll_proto_n2a(rta_getattr_u16(tb
[TCA_VLAN_PUSH_VLAN_PROTOCOL
]),
221 if (tb
[TCA_VLAN_PUSH_VLAN_PRIORITY
]) {
222 val
= rta_getattr_u8(tb
[TCA_VLAN_PUSH_VLAN_PRIORITY
]);
223 fprintf(f
, " priority %u", val
);
227 print_action_control(f
, " ", parm
->action
, "");
229 fprintf(f
, "\n\t index %u ref %d bind %d", parm
->index
, parm
->refcnt
,
233 if (tb
[TCA_VLAN_TM
]) {
234 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_VLAN_TM
]);
245 struct action_util vlan_action_util
= {
247 .parse_aopt
= parse_vlan
,
248 .print_aopt
= print_vlan
,