2 * VLAN netlink control interface
4 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/if_vlan.h>
14 #include <net/netlink.h>
15 #include <net/rtnetlink.h>
19 static const struct nla_policy vlan_policy[IFLA_VLAN_MAX + 1] = {
20 [IFLA_VLAN_ID] = { .type = NLA_U16 },
21 [IFLA_VLAN_FLAGS] = { .len = sizeof(struct ifla_vlan_flags) },
22 [IFLA_VLAN_EGRESS_QOS] = { .type = NLA_NESTED },
23 [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
26 static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = {
27 [IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) },
31 static inline int vlan_validate_qos_map(struct nlattr *attr)
35 return nla_validate_nested(attr, IFLA_VLAN_QOS_MAX, vlan_map_policy);
38 static int vlan_validate(struct nlattr *tb[], struct nlattr *data[])
40 struct ifla_vlan_flags *flags;
44 if (tb[IFLA_ADDRESS]) {
45 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
47 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
48 return -EADDRNOTAVAIL;
54 if (data[IFLA_VLAN_ID]) {
55 id = nla_get_u16(data[IFLA_VLAN_ID]);
56 if (id >= VLAN_VID_MASK)
59 if (data[IFLA_VLAN_FLAGS]) {
60 flags = nla_data(data[IFLA_VLAN_FLAGS]);
61 if ((flags->flags & flags->mask) & ~VLAN_FLAG_REORDER_HDR)
65 err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]);
68 err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]);
74 static int vlan_changelink(struct net_device *dev,
75 struct nlattr *tb[], struct nlattr *data[])
77 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
78 struct ifla_vlan_flags *flags;
79 struct ifla_vlan_qos_mapping *m;
83 if (data[IFLA_VLAN_FLAGS]) {
84 flags = nla_data(data[IFLA_VLAN_FLAGS]);
85 vlan->flags = (vlan->flags & ~flags->mask) |
86 (flags->flags & flags->mask);
88 if (data[IFLA_VLAN_INGRESS_QOS]) {
89 nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) {
91 vlan_dev_set_ingress_priority(dev, m->to, m->from);
94 if (data[IFLA_VLAN_EGRESS_QOS]) {
95 nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) {
97 vlan_dev_set_egress_priority(dev, m->from, m->to);
103 static int vlan_newlink(struct net_device *dev,
104 struct nlattr *tb[], struct nlattr *data[])
106 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
107 struct net_device *real_dev;
110 if (!data[IFLA_VLAN_ID])
115 real_dev = __dev_get_by_index(nla_get_u32(tb[IFLA_LINK]));
119 vlan->vlan_id = nla_get_u16(data[IFLA_VLAN_ID]);
120 vlan->real_dev = real_dev;
121 vlan->flags = VLAN_FLAG_REORDER_HDR;
123 err = vlan_check_real_dev(real_dev, vlan->vlan_id);
128 dev->mtu = real_dev->mtu;
129 else if (dev->mtu > real_dev->mtu)
132 err = vlan_changelink(dev, tb, data);
136 return register_vlan_dev(dev);
139 static void vlan_dellink(struct net_device *dev)
141 unregister_vlan_device(dev);
144 static inline size_t vlan_qos_map_size(unsigned int n)
148 /* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
149 return nla_total_size(sizeof(struct nlattr)) +
150 nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
153 static size_t vlan_get_size(const struct net_device *dev)
155 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
157 return nla_total_size(2) + /* IFLA_VLAN_ID */
158 vlan_qos_map_size(vlan->nr_ingress_mappings) +
159 vlan_qos_map_size(vlan->nr_egress_mappings);
162 static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
164 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
165 struct vlan_priority_tci_mapping *pm;
166 struct ifla_vlan_flags f;
167 struct ifla_vlan_qos_mapping m;
171 NLA_PUT_U16(skb, IFLA_VLAN_ID, VLAN_DEV_INFO(dev)->vlan_id);
173 f.flags = vlan->flags;
175 NLA_PUT(skb, IFLA_VLAN_FLAGS, sizeof(f), &f);
177 if (vlan->nr_ingress_mappings) {
178 nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS);
180 goto nla_put_failure;
182 for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
183 if (!vlan->ingress_priority_map[i])
187 m.to = vlan->ingress_priority_map[i];
188 NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING,
191 nla_nest_end(skb, nest);
194 if (vlan->nr_egress_mappings) {
195 nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS);
197 goto nla_put_failure;
199 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
200 for (pm = vlan->egress_priority_map[i]; pm;
205 m.from = pm->priority;
206 m.to = (pm->vlan_qos >> 13) & 0x7;
207 NLA_PUT(skb, IFLA_VLAN_QOS_MAPPING,
211 nla_nest_end(skb, nest);
219 struct rtnl_link_ops vlan_link_ops __read_mostly = {
221 .maxtype = IFLA_VLAN_MAX,
222 .policy = vlan_policy,
223 .priv_size = sizeof(struct vlan_dev_info),
225 .validate = vlan_validate,
226 .newlink = vlan_newlink,
227 .changelink = vlan_changelink,
228 .dellink = vlan_dellink,
229 .get_size = vlan_get_size,
230 .fill_info = vlan_fill_info,
233 int __init vlan_netlink_init(void)
235 return rtnl_link_register(&vlan_link_ops);
238 void __exit vlan_netlink_fini(void)
240 rtnl_link_unregister(&vlan_link_ops);
243 MODULE_ALIAS_RTNL_LINK("vlan");