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