]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vlan.c
2 * iplink_vlan.c VLAN device support
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.
9 * Authors: Patrick McHardy <kaber@trash.net>
15 #include <linux/if_vlan.h>
19 #include "ip_common.h"
21 static void print_explain(FILE *f
)
24 "Usage: ... vlan [ protocol VLANPROTO ] id VLANID"
26 " [ ingress-qos-map QOS-MAP ] [ egress-qos-map QOS-MAP ]\n"
28 "VLANPROTO: [ 802.1Q / 802.1ad ]\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"
38 static void explain(void)
40 print_explain(stderr
);
43 static int on_off(const char *msg
, const char *arg
)
45 fprintf(stderr
, "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n", msg
, arg
);
49 static int vlan_parse_qos_map(int *argcp
, char ***argvp
, struct nlmsghdr
*n
,
54 struct ifla_vlan_qos_mapping m
;
58 addattr_l(n
, 1024, attrtype
, NULL
, 0);
61 char *colon
= strchr(*argv
, ':');
67 if (get_u32(&m
.from
, *argv
, 0))
69 if (get_u32(&m
.to
, colon
+ 1, 0))
73 addattr_l(n
, 1024, IFLA_VLAN_QOS_MAPPING
, &m
, sizeof(m
));
76 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *)tail
;
83 static int vlan_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
86 struct ifla_vlan_flags flags
= { 0 };
90 if (matches(*argv
, "protocol") == 0) {
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) {
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) {
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
;
108 return on_off("reorder_hdr", *argv
);
109 } else if (matches(*argv
, "gvrp") == 0) {
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
;
117 return on_off("gvrp", *argv
);
118 } else if (matches(*argv
, "mvrp") == 0) {
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
;
126 return on_off("mvrp", *argv
);
127 } else if (matches(*argv
, "loose_binding") == 0) {
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
;
135 return on_off("loose_binding", *argv
);
136 } else if (matches(*argv
, "ingress-qos-map") == 0) {
138 if (vlan_parse_qos_map(&argc
, &argv
, n
,
139 IFLA_VLAN_INGRESS_QOS
))
140 invarg("invalid ingress-qos-map", *argv
);
142 } else if (matches(*argv
, "egress-qos-map") == 0) {
144 if (vlan_parse_qos_map(&argc
, &argv
, n
,
145 IFLA_VLAN_EGRESS_QOS
))
146 invarg("invalid egress-qos-map", *argv
);
148 } else if (matches(*argv
, "help") == 0) {
152 fprintf(stderr
, "vlan: unknown command \"%s\"?\n", *argv
);
160 addattr_l(n
, 1024, IFLA_VLAN_FLAGS
, &flags
, sizeof(flags
));
165 static void vlan_print_map(FILE *f
, char *name
, struct rtattr
*attr
)
167 struct ifla_vlan_qos_mapping
*m
;
171 fprintf(f
, "\n %s { ", name
);
173 rem
= RTA_PAYLOAD(attr
);
174 for (i
= RTA_DATA(attr
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
176 fprintf(f
, "%u:%u ", m
->from
, m
->to
);
181 static void vlan_print_flags(FILE *fp
, __u32 flags
)
184 #define _PF(f) if (flags & VLAN_FLAG_##f) { \
185 flags &= ~ VLAN_FLAG_##f; \
186 fprintf(fp, #f "%s", flags ? "," : ""); \
194 fprintf(fp
, "%x", flags
);
198 static void vlan_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
200 struct ifla_vlan_flags
*flags
;
206 if (tb
[IFLA_VLAN_PROTOCOL
] &&
207 RTA_PAYLOAD(tb
[IFLA_VLAN_PROTOCOL
]) < sizeof(__u16
))
209 if (!tb
[IFLA_VLAN_ID
] ||
210 RTA_PAYLOAD(tb
[IFLA_VLAN_ID
]) < sizeof(__u16
))
213 if (tb
[IFLA_VLAN_PROTOCOL
])
214 fprintf(f
, "protocol %s ",
215 ll_proto_n2a(rta_getattr_u16(tb
[IFLA_VLAN_PROTOCOL
]),
218 fprintf(f
, "protocol 802.1q ");
220 fprintf(f
, "id %u ", rta_getattr_u16(tb
[IFLA_VLAN_ID
]));
222 if (tb
[IFLA_VLAN_FLAGS
]) {
223 if (RTA_PAYLOAD(tb
[IFLA_VLAN_FLAGS
]) < sizeof(*flags
))
225 flags
= RTA_DATA(tb
[IFLA_VLAN_FLAGS
]);
226 vlan_print_flags(f
, flags
->flags
);
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
]);
234 static void vlan_print_help(struct link_util
*lu
, int argc
, char **argv
,
240 struct link_util vlan_link_util
= {
242 .maxattr
= IFLA_VLAN_MAX
,
243 .parse_opt
= vlan_parse_opt
,
244 .print_opt
= vlan_print_opt
,
245 .print_help
= vlan_print_help
,