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