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