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