vlan_netlink.c 7.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
P
Patrick McHardy 已提交
2 3 4 5 6 7 8 9 10
/*
 *	VLAN netlink control interface
 *
 * 	Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
 */

#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/if_vlan.h>
11
#include <linux/module.h>
12
#include <net/net_namespace.h>
P
Patrick McHardy 已提交
13 14 15 16 17 18 19 20 21 22
#include <net/netlink.h>
#include <net/rtnetlink.h>
#include "vlan.h"


static const struct nla_policy vlan_policy[IFLA_VLAN_MAX + 1] = {
	[IFLA_VLAN_ID]		= { .type = NLA_U16 },
	[IFLA_VLAN_FLAGS]	= { .len = sizeof(struct ifla_vlan_flags) },
	[IFLA_VLAN_EGRESS_QOS]	= { .type = NLA_NESTED },
	[IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
P
Patrick McHardy 已提交
23
	[IFLA_VLAN_PROTOCOL]	= { .type = NLA_U16 },
P
Patrick McHardy 已提交
24 25 26 27 28 29 30 31 32 33 34
};

static const struct nla_policy vlan_map_policy[IFLA_VLAN_QOS_MAX + 1] = {
	[IFLA_VLAN_QOS_MAPPING] = { .len = sizeof(struct ifla_vlan_qos_mapping) },
};


static inline int vlan_validate_qos_map(struct nlattr *attr)
{
	if (!attr)
		return 0;
35 36
	return nla_validate_nested_deprecated(attr, IFLA_VLAN_QOS_MAX,
					      vlan_map_policy, NULL);
P
Patrick McHardy 已提交
37 38
}

39 40
static int vlan_validate(struct nlattr *tb[], struct nlattr *data[],
			 struct netlink_ext_ack *extack)
P
Patrick McHardy 已提交
41 42 43 44 45
{
	struct ifla_vlan_flags *flags;
	u16 id;
	int err;

46
	if (tb[IFLA_ADDRESS]) {
47 48
		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
			NL_SET_ERR_MSG_MOD(extack, "Invalid link address");
49
			return -EINVAL;
50 51 52
		}
		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
			NL_SET_ERR_MSG_MOD(extack, "Invalid link address");
53
			return -EADDRNOTAVAIL;
54
		}
55 56
	}

57 58
	if (!data) {
		NL_SET_ERR_MSG_MOD(extack, "VLAN properties not specified");
P
Patrick McHardy 已提交
59
		return -EINVAL;
60
	}
P
Patrick McHardy 已提交
61

P
Patrick McHardy 已提交
62 63
	if (data[IFLA_VLAN_PROTOCOL]) {
		switch (nla_get_be16(data[IFLA_VLAN_PROTOCOL])) {
64 65
		case htons(ETH_P_8021Q):
		case htons(ETH_P_8021AD):
P
Patrick McHardy 已提交
66 67
			break;
		default:
68
			NL_SET_ERR_MSG_MOD(extack, "Invalid VLAN protocol");
P
Patrick McHardy 已提交
69 70 71 72
			return -EPROTONOSUPPORT;
		}
	}

P
Patrick McHardy 已提交
73 74
	if (data[IFLA_VLAN_ID]) {
		id = nla_get_u16(data[IFLA_VLAN_ID]);
75 76
		if (id >= VLAN_VID_MASK) {
			NL_SET_ERR_MSG_MOD(extack, "Invalid VLAN id");
P
Patrick McHardy 已提交
77
			return -ERANGE;
78
		}
P
Patrick McHardy 已提交
79 80 81
	}
	if (data[IFLA_VLAN_FLAGS]) {
		flags = nla_data(data[IFLA_VLAN_FLAGS]);
P
Patrick McHardy 已提交
82
		if ((flags->flags & flags->mask) &
83
		    ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
84 85
		      VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP |
		      VLAN_FLAG_BRIDGE_BINDING)) {
86
			NL_SET_ERR_MSG_MOD(extack, "Invalid VLAN flags");
P
Patrick McHardy 已提交
87
			return -EINVAL;
88
		}
P
Patrick McHardy 已提交
89 90 91
	}

	err = vlan_validate_qos_map(data[IFLA_VLAN_INGRESS_QOS]);
92 93
	if (err < 0) {
		NL_SET_ERR_MSG_MOD(extack, "Invalid ingress QOS map");
P
Patrick McHardy 已提交
94
		return err;
95
	}
P
Patrick McHardy 已提交
96
	err = vlan_validate_qos_map(data[IFLA_VLAN_EGRESS_QOS]);
97 98
	if (err < 0) {
		NL_SET_ERR_MSG_MOD(extack, "Invalid egress QOS map");
P
Patrick McHardy 已提交
99
		return err;
100
	}
P
Patrick McHardy 已提交
101 102 103
	return 0;
}

104 105 106
static int vlan_changelink(struct net_device *dev, struct nlattr *tb[],
			   struct nlattr *data[],
			   struct netlink_ext_ack *extack)
P
Patrick McHardy 已提交
107 108 109 110
{
	struct ifla_vlan_flags *flags;
	struct ifla_vlan_qos_mapping *m;
	struct nlattr *attr;
111
	int rem, err;
P
Patrick McHardy 已提交
112 113 114

	if (data[IFLA_VLAN_FLAGS]) {
		flags = nla_data(data[IFLA_VLAN_FLAGS]);
115 116 117
		err = vlan_dev_change_flags(dev, flags->flags, flags->mask);
		if (err)
			return err;
P
Patrick McHardy 已提交
118 119 120 121 122 123 124 125 126 127
	}
	if (data[IFLA_VLAN_INGRESS_QOS]) {
		nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) {
			m = nla_data(attr);
			vlan_dev_set_ingress_priority(dev, m->to, m->from);
		}
	}
	if (data[IFLA_VLAN_EGRESS_QOS]) {
		nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) {
			m = nla_data(attr);
128 129 130
			err = vlan_dev_set_egress_priority(dev, m->from, m->to);
			if (err)
				return err;
P
Patrick McHardy 已提交
131 132 133 134 135
		}
	}
	return 0;
}

136
static int vlan_newlink(struct net *src_net, struct net_device *dev,
137 138
			struct nlattr *tb[], struct nlattr *data[],
			struct netlink_ext_ack *extack)
P
Patrick McHardy 已提交
139
{
140
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
P
Patrick McHardy 已提交
141
	struct net_device *real_dev;
142
	unsigned int max_mtu;
P
Patrick McHardy 已提交
143
	__be16 proto;
P
Patrick McHardy 已提交
144 145
	int err;

146 147
	if (!data[IFLA_VLAN_ID]) {
		NL_SET_ERR_MSG_MOD(extack, "VLAN id not specified");
P
Patrick McHardy 已提交
148
		return -EINVAL;
149
	}
P
Patrick McHardy 已提交
150

151 152
	if (!tb[IFLA_LINK]) {
		NL_SET_ERR_MSG_MOD(extack, "link not specified");
P
Patrick McHardy 已提交
153
		return -EINVAL;
154 155
	}

156
	real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
157 158
	if (!real_dev) {
		NL_SET_ERR_MSG_MOD(extack, "link does not exist");
P
Patrick McHardy 已提交
159
		return -ENODEV;
160
	}
P
Patrick McHardy 已提交
161

P
Patrick McHardy 已提交
162 163 164 165 166 167
	if (data[IFLA_VLAN_PROTOCOL])
		proto = nla_get_be16(data[IFLA_VLAN_PROTOCOL]);
	else
		proto = htons(ETH_P_8021Q);

	vlan->vlan_proto = proto;
168 169
	vlan->vlan_id	 = nla_get_u16(data[IFLA_VLAN_ID]);
	vlan->real_dev	 = real_dev;
170
	dev->priv_flags |= (real_dev->priv_flags & IFF_XMIT_DST_RELEASE);
171
	vlan->flags	 = VLAN_FLAG_REORDER_HDR;
P
Patrick McHardy 已提交
172

173 174
	err = vlan_check_real_dev(real_dev, vlan->vlan_proto, vlan->vlan_id,
				  extack);
P
Patrick McHardy 已提交
175 176 177
	if (err < 0)
		return err;

178 179
	max_mtu = netif_reduces_vlan_mtu(real_dev) ? real_dev->mtu - VLAN_HLEN :
						     real_dev->mtu;
P
Patrick McHardy 已提交
180
	if (!tb[IFLA_MTU])
181 182
		dev->mtu = max_mtu;
	else if (dev->mtu > max_mtu)
P
Patrick McHardy 已提交
183 184
		return -EINVAL;

185
	err = vlan_changelink(dev, tb, data, extack);
186
	if (err)
187 188 189 190
		return err;
	err = register_vlan_dev(dev, extack);
	if (err)
		vlan_dev_free_egress_priority(dev);
191
	return err;
P
Patrick McHardy 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204
}

static inline size_t vlan_qos_map_size(unsigned int n)
{
	if (n == 0)
		return 0;
	/* IFLA_VLAN_{EGRESS,INGRESS}_QOS + n * IFLA_VLAN_QOS_MAPPING */
	return nla_total_size(sizeof(struct nlattr)) +
	       nla_total_size(sizeof(struct ifla_vlan_qos_mapping)) * n;
}

static size_t vlan_get_size(const struct net_device *dev)
{
205
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
P
Patrick McHardy 已提交
206

P
Patrick McHardy 已提交
207 208
	return nla_total_size(2) +	/* IFLA_VLAN_PROTOCOL */
	       nla_total_size(2) +	/* IFLA_VLAN_ID */
209
	       nla_total_size(sizeof(struct ifla_vlan_flags)) + /* IFLA_VLAN_FLAGS */
P
Patrick McHardy 已提交
210 211 212 213 214 215
	       vlan_qos_map_size(vlan->nr_ingress_mappings) +
	       vlan_qos_map_size(vlan->nr_egress_mappings);
}

static int vlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
{
216
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
P
Patrick McHardy 已提交
217 218 219 220 221 222
	struct vlan_priority_tci_mapping *pm;
	struct ifla_vlan_flags f;
	struct ifla_vlan_qos_mapping m;
	struct nlattr *nest;
	unsigned int i;

P
Patrick McHardy 已提交
223 224
	if (nla_put_be16(skb, IFLA_VLAN_PROTOCOL, vlan->vlan_proto) ||
	    nla_put_u16(skb, IFLA_VLAN_ID, vlan->vlan_id))
D
David S. Miller 已提交
225
		goto nla_put_failure;
P
Patrick McHardy 已提交
226 227 228
	if (vlan->flags) {
		f.flags = vlan->flags;
		f.mask  = ~0;
D
David S. Miller 已提交
229 230
		if (nla_put(skb, IFLA_VLAN_FLAGS, sizeof(f), &f))
			goto nla_put_failure;
P
Patrick McHardy 已提交
231 232
	}
	if (vlan->nr_ingress_mappings) {
233
		nest = nla_nest_start_noflag(skb, IFLA_VLAN_INGRESS_QOS);
P
Patrick McHardy 已提交
234 235 236 237 238 239 240 241 242
		if (nest == NULL)
			goto nla_put_failure;

		for (i = 0; i < ARRAY_SIZE(vlan->ingress_priority_map); i++) {
			if (!vlan->ingress_priority_map[i])
				continue;

			m.from = i;
			m.to   = vlan->ingress_priority_map[i];
D
David S. Miller 已提交
243 244 245
			if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
				    sizeof(m), &m))
				goto nla_put_failure;
P
Patrick McHardy 已提交
246 247 248 249 250
		}
		nla_nest_end(skb, nest);
	}

	if (vlan->nr_egress_mappings) {
251
		nest = nla_nest_start_noflag(skb, IFLA_VLAN_EGRESS_QOS);
P
Patrick McHardy 已提交
252 253 254 255 256 257 258 259 260 261 262
		if (nest == NULL)
			goto nla_put_failure;

		for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
			for (pm = vlan->egress_priority_map[i]; pm;
			     pm = pm->next) {
				if (!pm->vlan_qos)
					continue;

				m.from = pm->priority;
				m.to   = (pm->vlan_qos >> 13) & 0x7;
D
David S. Miller 已提交
263 264 265
				if (nla_put(skb, IFLA_VLAN_QOS_MAPPING,
					    sizeof(m), &m))
					goto nla_put_failure;
P
Patrick McHardy 已提交
266 267 268 269 270 271 272 273 274 275
			}
		}
		nla_nest_end(skb, nest);
	}
	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

276 277 278 279 280 281 282
static struct net *vlan_get_link_net(const struct net_device *dev)
{
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;

	return dev_net(real_dev);
}

P
Patrick McHardy 已提交
283 284 285 286
struct rtnl_link_ops vlan_link_ops __read_mostly = {
	.kind		= "vlan",
	.maxtype	= IFLA_VLAN_MAX,
	.policy		= vlan_policy,
287
	.priv_size	= sizeof(struct vlan_dev_priv),
P
Patrick McHardy 已提交
288 289 290 291
	.setup		= vlan_setup,
	.validate	= vlan_validate,
	.newlink	= vlan_newlink,
	.changelink	= vlan_changelink,
292
	.dellink	= unregister_vlan_dev,
P
Patrick McHardy 已提交
293 294
	.get_size	= vlan_get_size,
	.fill_info	= vlan_fill_info,
295
	.get_link_net	= vlan_get_link_net,
P
Patrick McHardy 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308
};

int __init vlan_netlink_init(void)
{
	return rtnl_link_register(&vlan_link_ops);
}

void __exit vlan_netlink_fini(void)
{
	rtnl_link_unregister(&vlan_link_ops);
}

MODULE_ALIAS_RTNL_LINK("vlan");