]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_vlan.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / tc / m_vlan.c
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
22 static void explain(void)
23 {
24 fprintf(stderr, "Usage: vlan pop\n");
25 fprintf(stderr, " vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n");
26 fprintf(stderr, " VLANPROTO is one of 802.1Q or 802.1AD\n");
27 fprintf(stderr, " with default: 802.1Q\n");
28 fprintf(stderr, " CONTROL := reclassify | pipe | drop | continue | pass\n");
29 }
30
31 static void usage(void)
32 {
33 explain();
34 exit(-1);
35 }
36
37 static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
38 int tca_id, struct nlmsghdr *n)
39 {
40 int argc = *argc_p;
41 char **argv = *argv_p;
42 struct rtattr *tail;
43 int action = 0;
44 __u16 id;
45 int id_set = 0;
46 __u16 proto;
47 int proto_set = 0;
48 __u8 prio;
49 int prio_set = 0;
50 struct tc_vlan parm = { 0 };
51
52 if (matches(*argv, "vlan") != 0)
53 return -1;
54
55 NEXT_ARG();
56
57 while (argc > 0) {
58 if (matches(*argv, "pop") == 0) {
59 if (action) {
60 fprintf(stderr, "unexpected \"%s\" - action already specified\n",
61 *argv);
62 explain();
63 return -1;
64 }
65 action = TCA_VLAN_ACT_POP;
66 } else if (matches(*argv, "push") == 0) {
67 if (action) {
68 fprintf(stderr, "unexpected \"%s\" - action already specified\n",
69 *argv);
70 explain();
71 return -1;
72 }
73 action = TCA_VLAN_ACT_PUSH;
74 } else if (matches(*argv, "id") == 0) {
75 if (action != TCA_VLAN_ACT_PUSH) {
76 fprintf(stderr, "\"%s\" is only valid for push\n",
77 *argv);
78 explain();
79 return -1;
80 }
81 NEXT_ARG();
82 if (get_u16(&id, *argv, 0))
83 invarg("id is invalid", *argv);
84 id_set = 1;
85 } else if (matches(*argv, "protocol") == 0) {
86 if (action != TCA_VLAN_ACT_PUSH) {
87 fprintf(stderr, "\"%s\" is only valid for push\n",
88 *argv);
89 explain();
90 return -1;
91 }
92 NEXT_ARG();
93 if (ll_proto_a2n(&proto, *argv))
94 invarg("protocol is invalid", *argv);
95 proto_set = 1;
96 } else if (matches(*argv, "priority") == 0) {
97 if (action != TCA_VLAN_ACT_PUSH) {
98 fprintf(stderr, "\"%s\" is only valid for push\n",
99 *argv);
100 explain();
101 return -1;
102 }
103 NEXT_ARG();
104 if (get_u8(&prio, *argv, 0) || (prio & ~0x7))
105 invarg("prio is invalid", *argv);
106 prio_set = 1;
107 } else if (matches(*argv, "help") == 0) {
108 usage();
109 } else {
110 break;
111 }
112 argc--;
113 argv++;
114 }
115
116 parm.action = TC_ACT_PIPE;
117 if (argc && !action_a2n(*argv, &parm.action, false))
118 NEXT_ARG_FWD();
119
120 if (argc) {
121 if (matches(*argv, "index") == 0) {
122 NEXT_ARG();
123 if (get_u32(&parm.index, *argv, 10)) {
124 fprintf(stderr, "vlan: Illegal \"index\"\n");
125 return -1;
126 }
127 argc--;
128 argv++;
129 }
130 }
131
132 if (action == TCA_VLAN_ACT_PUSH && !id_set) {
133 fprintf(stderr, "id needs to be set for push\n");
134 explain();
135 return -1;
136 }
137
138 parm.v_action = action;
139 tail = NLMSG_TAIL(n);
140 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
141 addattr_l(n, MAX_MSG, TCA_VLAN_PARMS, &parm, sizeof(parm));
142 if (id_set)
143 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_ID, &id, 2);
144 if (proto_set) {
145 if (proto != htons(ETH_P_8021Q) &&
146 proto != htons(ETH_P_8021AD)) {
147 fprintf(stderr, "protocol not supported\n");
148 explain();
149 return -1;
150 }
151
152 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PROTOCOL, &proto, 2);
153 }
154 if (prio_set)
155 addattr8(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PRIORITY, prio);
156
157 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
158
159 *argc_p = argc;
160 *argv_p = argv;
161 return 0;
162 }
163
164 static int print_vlan(struct action_util *au, FILE *f, struct rtattr *arg)
165 {
166 SPRINT_BUF(b1);
167 struct rtattr *tb[TCA_VLAN_MAX + 1];
168 __u16 val;
169 struct tc_vlan *parm;
170
171 if (arg == NULL)
172 return -1;
173
174 parse_rtattr_nested(tb, TCA_VLAN_MAX, arg);
175
176 if (!tb[TCA_VLAN_PARMS]) {
177 fprintf(f, "[NULL vlan parameters]");
178 return -1;
179 }
180 parm = RTA_DATA(tb[TCA_VLAN_PARMS]);
181
182 fprintf(f, " vlan");
183
184 switch (parm->v_action) {
185 case TCA_VLAN_ACT_POP:
186 fprintf(f, " pop");
187 break;
188 case TCA_VLAN_ACT_PUSH:
189 fprintf(f, " push");
190 if (tb[TCA_VLAN_PUSH_VLAN_ID]) {
191 val = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
192 fprintf(f, " id %u", val);
193 }
194 if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
195 fprintf(f, " protocol %s",
196 ll_proto_n2a(rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]),
197 b1, sizeof(b1)));
198 }
199 if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) {
200 val = rta_getattr_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
201 fprintf(f, " priority %u", val);
202 }
203 break;
204 }
205 fprintf(f, " %s", action_n2a(parm->action));
206
207 fprintf(f, "\n\t index %d ref %d bind %d", parm->index, parm->refcnt,
208 parm->bindcnt);
209
210 if (show_stats) {
211 if (tb[TCA_VLAN_TM]) {
212 struct tcf_t *tm = RTA_DATA(tb[TCA_VLAN_TM]);
213
214 print_tm(f, tm);
215 }
216 }
217
218 fprintf(f, "\n ");
219
220 return 0;
221 }
222
223 struct action_util vlan_action_util = {
224 .id = "vlan",
225 .parse_aopt = parse_vlan,
226 .print_aopt = print_vlan,
227 };