• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *	VLAN netlink control interface
3  *
4  * 	Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
5  *
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.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/if_vlan.h>
14 #include <linux/module.h>
15 #include <net/net_namespace.h>
16 #include <net/netlink.h>
17 #include <net/rtnetlink.h>
18 #include "vlan.h"
19 
20 
21 static const struct nla_policy vlan_policy[IFLA_VLAN_MAX + 1] = {
22 	[IFLA_VLAN_ID]		= { .type = NLA_U16 },
23 	[IFLA_VLAN_FLAGS]	= { .len = sizeof(struct ifla_vlan_flags) },
24 	[IFLA_VLAN_EGRESS_QOS]	= { .type = NLA_NESTED },
25 	[IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
26 	[IFLA_VLAN_PROTOCOL]	= { .type = NLA_U16 },
27 };
28 
29 static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = {
30 	[IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) },
31 };
32 
33 
vlan_validate_qos_map(struct nlattr * attr)34 static inline int vlan_validate_qos_map(struct nlattr *attr)
35 {
36 	if (!attr)
37 		return 0;
38 	return nla_validate_nested(attr, IFLA_VLAN_QOS_MAX, vlan_map_policy);
39 }
40 
vlan_validate(struct nlattr * tb[],struct nlattr * data[])41 static int vlan_validate(struct nlattr *tb[], struct nlattr *data[])
42 {
43 	struct ifla_vlan_flags *flags;
44 	u16 id;
45 	int err;
46 
47 	if (tb[IFLA_ADDRESS]) {
48 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
49 			return -EINVAL;
50 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
51 			return -EADDRNOTAVAIL;
52 	}
53 
54 	if (!data)
55 		return -EINVAL;
56 
57 	if (data[IFLA_VLAN_PROTOCOL]) {
58 		switch (nla_get_be16(data[IFLA_VLAN_PROTOCOL])) {
59 		case htons(ETH_P_8021Q):
60 		case htons(ETH_P_8021AD):
61 			break;
62 		default:
63 			return -EPROTONOSUPPORT;
64 		}
65 	}
66 
67 	if (data[IFLA_VLAN_ID]) {
68 		id = nla_get_u16(data[IFLA_VLAN_ID]);
69 		if (id >= VLAN_VID_MASK)
70 			return -ERANGE;
71 	}
72 	if (data[IFLA_VLAN_FLAGS]) {
73 		flags = nla_data(data[IFLA_VLAN_FLAGS]);
74 		if ((flags->flags & flags->mask) &
75 		    ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
76 		      VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP))
77 			return -EINVAL;
78 	}
79 
80 	err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]);
81 	if (err < 0)
82 		return err;
83 	err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]);
84 	if (err < 0)
85 		return err;
86 	return 0;
87 }
88 
vlan_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])89 static int vlan_changelink(struct net_device *dev,
90 			   struct nlattr *tb[], struct nlattr *data[])
91 {
92 	struct ifla_vlan_flags *flags;
93 	struct ifla_vlan_qos_mapping *m;
94 	struct nlattr *attr;
95 	int rem, err;
96 
97 	if (data[IFLA_VLAN_FLAGS]) {
98 		flags = nla_data(data[IFLA_VLAN_FLAGS]);
99 		err = vlan_dev_change_flags(dev, flags->flags, flags->mask);
100 		if (err)
101 			return err;
102 	}
103 	if (data[IFLA_VLAN_INGRESS_QOS]) {
104 		nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) {
105 			m = nla_data(attr);
106 			vlan_dev_set_ingress_priority(dev, m->to, m->from);
107 		}
108 	}
109 	if (data[IFLA_VLAN_EGRESS_QOS]) {
110 		nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) {
111 			m = nla_data(attr);
112 			err = vlan_dev_set_egress_priority(dev, m->from, m->to);
113 			if (err)
114 				return err;
115 		}
116 	}
117 	return 0;
118 }
119 
vlan_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])120 static int vlan_newlink(struct net *src_net, struct net_device *dev,
121 			struct nlattr *tb[], struct nlattr *data[])
122 {
123 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
124 	struct net_device *real_dev;
125 	__be16 proto;
126 	int err;
127 
128 	if (!data[IFLA_VLAN_ID])
129 		return -EINVAL;
130 
131 	if (!tb[IFLA_LINK])
132 		return -EINVAL;
133 	real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
134 	if (!real_dev)
135 		return -ENODEV;
136 
137 	if (data[IFLA_VLAN_PROTOCOL])
138 		proto = nla_get_be16(data[IFLA_VLAN_PROTOCOL]);
139 	else
140 		proto = htons(ETH_P_8021Q);
141 
142 	vlan->vlan_proto = proto;
143 	vlan->vlan_id	 = nla_get_u16(data[IFLA_VLAN_ID]);
144 	vlan->real_dev	 = real_dev;
145 	vlan->flags	 = VLAN_FLAG_REORDER_HDR;
146 
147 	err = vlan_check_real_dev(real_dev, vlan->vlan_proto, vlan->vlan_id);
148 	if (err < 0)
149 		return err;
150 
151 	if (!tb[IFLA_MTU])
152 		dev->mtu = real_dev->mtu;
153 	else if (dev->mtu > real_dev->mtu)
154 		return -EINVAL;
155 
156 	err = vlan_changelink(dev, tb, data);
157 	if (!err)
158 		err = register_vlan_dev(dev);
159 	if (err)
160 		vlan_dev_uninit(dev);
161 	return err;
162 }
163 
vlan_qos_map_size(unsigned int n)164 static inline size_t vlan_qos_map_size(unsigned int n)
165 {
166 	if (n == 0)
167 		return 0;
168 	/* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
169 	return nla_total_size(sizeof(struct nlattr)) +
170 	       nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
171 }
172 
vlan_get_size(const struct net_device * dev)173 static size_t vlan_get_size(const struct net_device *dev)
174 {
175 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
176 
177 	return nla_total_size(2) +	/* IFLA_VLAN_PROTOCOL */
178 	       nla_total_size(2) +	/* IFLA_VLAN_ID */
179 	       nla_total_size(sizeof(struct ifla_vlan_flags)) + /* IFLA_VLAN_FLAGS */
180 	       vlan_qos_map_size(vlan->nr_ingress_mappings) +
181 	       vlan_qos_map_size(vlan->nr_egress_mappings);
182 }
183 
vlan_fill_info(struct sk_buff * skb,const struct net_device * dev)184 static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
185 {
186 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
187 	struct vlan_priority_tci_mapping *pm;
188 	struct ifla_vlan_flags f;
189 	struct ifla_vlan_qos_mapping m;
190 	struct nlattr *nest;
191 	unsigned int i;
192 
193 	if (nla_put_be16(skb, IFLA_VLAN_PROTOCOL, vlan->vlan_proto) ||
194 	    nla_put_u16(skb, IFLA_VLAN_ID, vlan->vlan_id))
195 		goto nla_put_failure;
196 	if (vlan->flags) {
197 		f.flags = vlan->flags;
198 		f.mask  = ~0;
199 		if (nla_put(skb, IFLA_VLAN_FLAGS, sizeof(f), &f))
200 			goto nla_put_failure;
201 	}
202 	if (vlan->nr_ingress_mappings) {
203 		nest = nla_nest_start(skb, IFLA_VLAN_INGRESS_QOS);
204 		if (nest == NULL)
205 			goto nla_put_failure;
206 
207 		for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
208 			if (!vlan->ingress_priority_map[i])
209 				continue;
210 
211 			m.from = i;
212 			m.to   = vlan->ingress_priority_map[i];
213 			if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
214 				    sizeof(m), &m))
215 				goto nla_put_failure;
216 		}
217 		nla_nest_end(skb, nest);
218 	}
219 
220 	if (vlan->nr_egress_mappings) {
221 		nest = nla_nest_start(skb, IFLA_VLAN_EGRESS_QOS);
222 		if (nest == NULL)
223 			goto nla_put_failure;
224 
225 		for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
226 			for (pm = vlan->egress_priority_map[i]; pm;
227 			     pm = pm->next) {
228 				if (!pm->vlan_qos)
229 					continue;
230 
231 				m.from = pm->priority;
232 				m.to   = (pm->vlan_qos >> 13) & 0x7;
233 				if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
234 					    sizeof(m), &m))
235 					goto nla_put_failure;
236 			}
237 		}
238 		nla_nest_end(skb, nest);
239 	}
240 	return 0;
241 
242 nla_put_failure:
243 	return -EMSGSIZE;
244 }
245 
vlan_get_link_net(const struct net_device * dev)246 static struct net *vlan_get_link_net(const struct net_device *dev)
247 {
248 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
249 
250 	return dev_net(real_dev);
251 }
252 
253 struct rtnl_link_ops vlan_link_ops __read_mostly = {
254 	.kind		= "vlan",
255 	.maxtype	= IFLA_VLAN_MAX,
256 	.policy		= vlan_policy,
257 	.priv_size	= sizeof(struct vlan_dev_priv),
258 	.setup		= vlan_setup,
259 	.validate	= vlan_validate,
260 	.newlink	= vlan_newlink,
261 	.changelink	= vlan_changelink,
262 	.dellink	= unregister_vlan_dev,
263 	.get_size	= vlan_get_size,
264 	.fill_info	= vlan_fill_info,
265 	.get_link_net	= vlan_get_link_net,
266 };
267 
vlan_netlink_init(void)268 int __init vlan_netlink_init(void)
269 {
270 	return rtnl_link_register(&vlan_link_ops);
271 }
272 
vlan_netlink_fini(void)273 void __exit vlan_netlink_fini(void)
274 {
275 	rtnl_link_unregister(&vlan_link_ops);
276 }
277 
278 MODULE_ALIAS_RTNL_LINK("vlan");
279