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 explain(void)
24 "Usage: ... vlan id VLANID [ FLAG-LIST ]\n"
25 " [ ingress-qos-map QOS-MAP ] [ egress-qos-map QOS-MAP ]\n"
28 "FLAG-LIST := [ FLAG-LIST ] FLAG\n"
29 "FLAG := [ reorder_hdr { on | off } ] [ gvrp { on | off } ]\n"
30 "QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n"
31 "QOS-MAPPING := FROM:TO\n"
35 static int on_off(char *msg)
37 fprintf(stderr, "Error: argument of \"%s\" must be \"on\" or \"off\"\n", msg);
41 static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n,
46 struct ifla_vlan_qos_mapping m;
50 addattr_l(n, 1024, attrtype, NULL, 0);
53 char *colon = strchr(*argv, ':');
59 if (get_u32(&m.from, *argv, 0))
61 if (get_u32(&m.to, colon + 1, 0))
65 addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
68 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *)tail;
75 static int vlan_parse_opt(struct link_util *lu, int argc, char **argv,
78 struct ifla_vlan_flags flags = { 0 };
82 if (matches(*argv, "id") == 0) {
84 if (get_u16(&id, *argv, 0))
85 invarg("id is invalid", *argv);
86 addattr_l(n, 1024, IFLA_VLAN_ID, &id, 2);
87 } else if (matches(*argv, "reorder_hdr") == 0) {
89 flags.mask |= VLAN_FLAG_REORDER_HDR;
90 if (strcmp(*argv, "on") == 0)
91 flags.flags |= VLAN_FLAG_REORDER_HDR;
92 else if (strcmp(*argv, "off") == 0)
93 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
95 return on_off("reorder_hdr");
96 } else if (matches(*argv, "gvrp") == 0) {
98 flags.mask |= VLAN_FLAG_GVRP;
99 if (strcmp(*argv, "on") == 0)
100 flags.flags |= VLAN_FLAG_GVRP;
101 else if (strcmp(*argv, "off") == 0)
102 flags.flags &= ~VLAN_FLAG_GVRP;
104 return on_off("gvrp");
105 } else if (matches(*argv, "ingress-qos-map") == 0) {
107 if (vlan_parse_qos_map(&argc, &argv, n,
108 IFLA_VLAN_INGRESS_QOS))
109 invarg("invalid ingress-qos-map", *argv);
111 } else if (matches(*argv, "egress-qos-map") == 0) {
113 if (vlan_parse_qos_map(&argc, &argv, n,
114 IFLA_VLAN_EGRESS_QOS))
115 invarg("invalid egress-qos-map", *argv);
117 } else if (matches(*argv, "help") == 0) {
121 fprintf(stderr, "vlan: what is \"%s\"?\n", *argv);
129 addattr_l(n, 1024, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
134 static void vlan_print_map(FILE *f, char *name, struct rtattr *attr)
136 struct ifla_vlan_qos_mapping *m;
140 fprintf(f, "\n %s { ", name);
142 rem = RTA_PAYLOAD(attr);
143 for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
145 fprintf(f, "%u:%u ", m->from, m->to);
150 static void vlan_print_flags(FILE *fp, __u32 flags)
153 #define _PF(f) if (flags & VLAN_FLAG_##f) { \
154 flags &= ~ VLAN_FLAG_##f; \
155 fprintf(fp, #f "%s", flags ? "," : ""); \
161 fprintf(fp, "%x", flags);
165 static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
167 struct ifla_vlan_flags *flags;
171 if (!tb[IFLA_VLAN_ID] ||
172 RTA_PAYLOAD(tb[IFLA_VLAN_ID]) < sizeof(__u16))
175 fprintf(f, "id %u ", *(__u16 *)RTA_DATA(tb[IFLA_VLAN_ID]));
177 if (tb[IFLA_VLAN_FLAGS]) {
178 if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags))
180 flags = RTA_DATA(tb[IFLA_VLAN_FLAGS]);
181 vlan_print_flags(f, flags->flags);
183 if (tb[IFLA_VLAN_INGRESS_QOS])
184 vlan_print_map(f, "ingress-qos-map", tb[IFLA_VLAN_INGRESS_QOS]);
185 if (tb[IFLA_VLAN_EGRESS_QOS])
186 vlan_print_map(f, "egress-qos-map", tb[IFLA_VLAN_EGRESS_QOS]);
189 struct link_util vlan_link_util = {
191 .maxattr = IFLA_VLAN_MAX,
192 .parse_opt = vlan_parse_opt,
193 .print_opt = vlan_print_opt,