]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vlan.c
iproute: Set ip/ip6 lwtunnel flags
[mirror_iproute2.git] / ip / iplink_vlan.c
1 /*
2 * iplink_vlan.c VLAN device support
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: Patrick McHardy <kaber@trash.net>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <linux/if_vlan.h>
16
17 #include "rt_names.h"
18 #include "utils.h"
19 #include "ip_common.h"
20
21 static void print_explain(FILE *f)
22 {
23 fprintf(f,
24 "Usage: ... vlan id VLANID\n"
25 " [ protocol VLANPROTO ]\n"
26 " [ reorder_hdr { on | off } ]\n"
27 " [ gvrp { on | off } ]\n"
28 " [ mvrp { on | off } ]\n"
29 " [ loose_binding { on | off } ]\n"
30 " [ ingress-qos-map QOS-MAP ]\n"
31 " [ egress-qos-map QOS-MAP ]\n"
32 "\n"
33 "VLANID := 0-4095\n"
34 "VLANPROTO: [ 802.1Q / 802.1ad ]\n"
35 "QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n"
36 "QOS-MAPPING := FROM:TO\n"
37 );
38 }
39
40 static void explain(void)
41 {
42 print_explain(stderr);
43 }
44
45 static int on_off(const char *msg, const char *arg)
46 {
47 fprintf(stderr, "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n", msg, arg);
48 return -1;
49 }
50
51 static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n,
52 int attrtype)
53 {
54 int argc = *argcp;
55 char **argv = *argvp;
56 struct ifla_vlan_qos_mapping m;
57 struct rtattr *tail;
58
59 tail = addattr_nest(n, 1024, attrtype);
60
61 while (argc > 0) {
62 char *colon = strchr(*argv, ':');
63
64 if (!colon)
65 break;
66 *colon = '\0';
67
68 if (get_u32(&m.from, *argv, 0))
69 return 1;
70 if (get_u32(&m.to, colon + 1, 0))
71 return 1;
72 argc--, argv++;
73
74 addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
75 }
76
77 addattr_nest_end(n, tail);
78
79 *argcp = argc;
80 *argvp = argv;
81 return 0;
82 }
83
84 static int vlan_parse_opt(struct link_util *lu, int argc, char **argv,
85 struct nlmsghdr *n)
86 {
87 struct ifla_vlan_flags flags = { 0 };
88 __u16 id, proto;
89
90 while (argc > 0) {
91 if (matches(*argv, "protocol") == 0) {
92 NEXT_ARG();
93 if (ll_proto_a2n(&proto, *argv))
94 invarg("protocol is invalid", *argv);
95 addattr_l(n, 1024, IFLA_VLAN_PROTOCOL, &proto, 2);
96 } else if (matches(*argv, "id") == 0) {
97 NEXT_ARG();
98 if (get_u16(&id, *argv, 0))
99 invarg("id is invalid", *argv);
100 addattr_l(n, 1024, IFLA_VLAN_ID, &id, 2);
101 } else if (matches(*argv, "reorder_hdr") == 0) {
102 NEXT_ARG();
103 flags.mask |= VLAN_FLAG_REORDER_HDR;
104 if (strcmp(*argv, "on") == 0)
105 flags.flags |= VLAN_FLAG_REORDER_HDR;
106 else if (strcmp(*argv, "off") == 0)
107 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
108 else
109 return on_off("reorder_hdr", *argv);
110 } else if (matches(*argv, "gvrp") == 0) {
111 NEXT_ARG();
112 flags.mask |= VLAN_FLAG_GVRP;
113 if (strcmp(*argv, "on") == 0)
114 flags.flags |= VLAN_FLAG_GVRP;
115 else if (strcmp(*argv, "off") == 0)
116 flags.flags &= ~VLAN_FLAG_GVRP;
117 else
118 return on_off("gvrp", *argv);
119 } else if (matches(*argv, "mvrp") == 0) {
120 NEXT_ARG();
121 flags.mask |= VLAN_FLAG_MVRP;
122 if (strcmp(*argv, "on") == 0)
123 flags.flags |= VLAN_FLAG_MVRP;
124 else if (strcmp(*argv, "off") == 0)
125 flags.flags &= ~VLAN_FLAG_MVRP;
126 else
127 return on_off("mvrp", *argv);
128 } else if (matches(*argv, "loose_binding") == 0) {
129 NEXT_ARG();
130 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
131 if (strcmp(*argv, "on") == 0)
132 flags.flags |= VLAN_FLAG_LOOSE_BINDING;
133 else if (strcmp(*argv, "off") == 0)
134 flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
135 else
136 return on_off("loose_binding", *argv);
137 } else if (matches(*argv, "ingress-qos-map") == 0) {
138 NEXT_ARG();
139 if (vlan_parse_qos_map(&argc, &argv, n,
140 IFLA_VLAN_INGRESS_QOS))
141 invarg("invalid ingress-qos-map", *argv);
142 continue;
143 } else if (matches(*argv, "egress-qos-map") == 0) {
144 NEXT_ARG();
145 if (vlan_parse_qos_map(&argc, &argv, n,
146 IFLA_VLAN_EGRESS_QOS))
147 invarg("invalid egress-qos-map", *argv);
148 continue;
149 } else if (matches(*argv, "help") == 0) {
150 explain();
151 return -1;
152 } else {
153 fprintf(stderr, "vlan: unknown command \"%s\"?\n", *argv);
154 explain();
155 return -1;
156 }
157 argc--, argv++;
158 }
159
160 if (flags.mask)
161 addattr_l(n, 1024, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
162
163 return 0;
164 }
165
166 static void vlan_print_map(FILE *f,
167 const char *name_json,
168 const char *name_fp,
169 struct rtattr *attr)
170 {
171 struct ifla_vlan_qos_mapping *m;
172 struct rtattr *i;
173 int rem;
174
175 open_json_array(PRINT_JSON, name_json);
176 print_string(PRINT_FP, NULL, "\n %s { ", name_fp);
177
178 rem = RTA_PAYLOAD(attr);
179 for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
180 m = RTA_DATA(i);
181
182 if (is_json_context()) {
183 open_json_object(NULL);
184 print_uint(PRINT_JSON, "from", NULL, m->from);
185 print_uint(PRINT_JSON, "to", NULL, m->to);
186 close_json_object();
187 } else {
188 fprintf(f, "%u:%u ", m->from, m->to);
189 }
190 }
191
192 close_json_array(PRINT_JSON, NULL);
193 print_string(PRINT_FP, NULL, "%s ", "}");
194 }
195
196 static void vlan_print_flags(FILE *fp, __u32 flags)
197 {
198 open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<");
199 #define _PF(f) if (flags & VLAN_FLAG_##f) { \
200 flags &= ~VLAN_FLAG_##f; \
201 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); \
202 }
203 _PF(REORDER_HDR);
204 _PF(GVRP);
205 _PF(MVRP);
206 _PF(LOOSE_BINDING);
207 #undef _PF
208 if (flags)
209 print_hex(PRINT_ANY, NULL, "%x", flags);
210 close_json_array(PRINT_ANY, "> ");
211 }
212
213 static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
214 {
215 struct ifla_vlan_flags *flags;
216
217 SPRINT_BUF(b1);
218
219 if (!tb)
220 return;
221
222 if (tb[IFLA_VLAN_PROTOCOL] &&
223 RTA_PAYLOAD(tb[IFLA_VLAN_PROTOCOL]) < sizeof(__u16))
224 return;
225 if (!tb[IFLA_VLAN_ID] ||
226 RTA_PAYLOAD(tb[IFLA_VLAN_ID]) < sizeof(__u16))
227 return;
228
229 if (tb[IFLA_VLAN_PROTOCOL])
230 print_string(PRINT_ANY,
231 "protocol",
232 "protocol %s ",
233 ll_proto_n2a(
234 rta_getattr_u16(tb[IFLA_VLAN_PROTOCOL]),
235 b1, sizeof(b1)));
236 else
237 print_string(PRINT_ANY, "protocol", "protocol %s ", "802.1q");
238
239 print_uint(PRINT_ANY,
240 "id",
241 "id %u ",
242 rta_getattr_u16(tb[IFLA_VLAN_ID]));
243
244 if (tb[IFLA_VLAN_FLAGS]) {
245 if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags))
246 return;
247 flags = RTA_DATA(tb[IFLA_VLAN_FLAGS]);
248 vlan_print_flags(f, flags->flags);
249 }
250 if (tb[IFLA_VLAN_INGRESS_QOS])
251 vlan_print_map(f,
252 "ingress_qos",
253 "ingress-qos-map",
254 tb[IFLA_VLAN_INGRESS_QOS]);
255 if (tb[IFLA_VLAN_EGRESS_QOS])
256 vlan_print_map(f,
257 "egress_qos",
258 "egress-qos-map",
259 tb[IFLA_VLAN_EGRESS_QOS]);
260 }
261
262 static void vlan_print_help(struct link_util *lu, int argc, char **argv,
263 FILE *f)
264 {
265 print_explain(f);
266 }
267
268 struct link_util vlan_link_util = {
269 .id = "vlan",
270 .maxattr = IFLA_VLAN_MAX,
271 .parse_opt = vlan_parse_opt,
272 .print_opt = vlan_print_opt,
273 .print_help = vlan_print_help,
274 };