br_netlink.c 45.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 *	Bridge netlink control interface
 *
 *	Authors:
 *	Stephen Hemminger		<shemminger@osdl.org>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
14
#include <linux/slab.h>
15
#include <linux/etherdevice.h>
16
#include <net/rtnetlink.h>
17
#include <net/net_namespace.h>
18
#include <net/sock.h>
19
#include <uapi/linux/if_bridge.h>
20

21
#include "br_private.h"
22
#include "br_private_stp.h"
23
#include "br_private_tunnel.h"
24

25
static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
26
				u32 filter_mask)
27
{
28
29
	struct net_bridge_vlan *v;
	u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
30
	u16 flags, pvid;
31
32
33
34
35
	int num_vlans = 0;

	if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
		return 0;

36
	pvid = br_get_pvid(vg);
37
	/* Count number of vlan infos */
38
	list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
39
		flags = 0;
40
41
42
43
		/* only a context, bridge vlan not activated */
		if (!br_vlan_should_use(v))
			continue;
		if (v->vid == pvid)
44
45
			flags |= BRIDGE_VLAN_INFO_PVID;

46
		if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
47
48
49
50
			flags |= BRIDGE_VLAN_INFO_UNTAGGED;

		if (vid_range_start == 0) {
			goto initvars;
51
		} else if ((v->vid - vid_range_end) == 1 &&
52
			flags == vid_range_flags) {
53
			vid_range_end = v->vid;
54
55
56
57
58
59
60
61
			continue;
		} else {
			if ((vid_range_end - vid_range_start) > 0)
				num_vlans += 2;
			else
				num_vlans += 1;
		}
initvars:
62
63
		vid_range_start = v->vid;
		vid_range_end = v->vid;
64
65
66
67
68
69
70
71
72
73
74
75
76
		vid_range_flags = flags;
	}

	if (vid_range_start != 0) {
		if ((vid_range_end - vid_range_start) > 0)
			num_vlans += 2;
		else
			num_vlans += 1;
	}

	return num_vlans;
}

77
static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
78
				 u32 filter_mask)
79
{
80
81
	int num_vlans;

82
83
84
85
86
87
	if (!vg)
		return 0;

	if (filter_mask & RTEXT_FILTER_BRVLAN)
		return vg->num_vlans;

88
89
90
91
92
	rcu_read_lock();
	num_vlans = __get_num_vlan_infos(vg, filter_mask);
	rcu_read_unlock();

	return num_vlans;
93
94
}

95
96
static size_t br_get_link_af_size_filtered(const struct net_device *dev,
					   u32 filter_mask)
97
{
98
	struct net_bridge_vlan_group *vg = NULL;
99
	struct net_bridge_port *p = NULL;
100
	struct net_bridge *br;
101
	int num_vlan_infos;
102
	size_t vinfo_sz = 0;
103

104
	rcu_read_lock();
105
106
	if (br_port_exists(dev)) {
		p = br_port_get_rcu(dev);
107
		vg = nbp_vlan_group_rcu(p);
108
109
	} else if (dev->priv_flags & IFF_EBRIDGE) {
		br = netdev_priv(dev);
110
		vg = br_vlan_group_rcu(br);
111
	}
112
	num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
113
	rcu_read_unlock();
114

115
116
117
	if (p && (p->flags & BR_VLAN_TUNNEL))
		vinfo_sz += br_get_vlan_tunnel_info_size(vg);

118
	/* Each VLAN is returned in bridge_vlan_info along with flags */
119
120
121
	vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));

	return vinfo_sz;
122
123
}

124
125
126
127
128
129
static inline size_t br_port_info_size(void)
{
	return nla_total_size(1)	/* IFLA_BRPORT_STATE  */
		+ nla_total_size(2)	/* IFLA_BRPORT_PRIORITY */
		+ nla_total_size(4)	/* IFLA_BRPORT_COST */
		+ nla_total_size(1)	/* IFLA_BRPORT_MODE */
130
		+ nla_total_size(1)	/* IFLA_BRPORT_GUARD */
131
		+ nla_total_size(1)	/* IFLA_BRPORT_PROTECT */
132
		+ nla_total_size(1)	/* IFLA_BRPORT_FAST_LEAVE */
Felix Fietkau's avatar
Felix Fietkau committed
133
		+ nla_total_size(1)	/* IFLA_BRPORT_MCAST_TO_UCAST */
134
		+ nla_total_size(1)	/* IFLA_BRPORT_LEARNING */
135
		+ nla_total_size(1)	/* IFLA_BRPORT_UNICAST_FLOOD */
136
137
		+ nla_total_size(1)	/* IFLA_BRPORT_MCAST_FLOOD */
		+ nla_total_size(1)	/* IFLA_BRPORT_BCAST_FLOOD */
138
		+ nla_total_size(1)	/* IFLA_BRPORT_PROXYARP */
139
		+ nla_total_size(1)	/* IFLA_BRPORT_PROXYARP_WIFI */
140
		+ nla_total_size(1)	/* IFLA_BRPORT_VLAN_TUNNEL */
141
		+ nla_total_size(1)	/* IFLA_BRPORT_NEIGH_SUPPRESS */
142
		+ nla_total_size(1)	/* IFLA_BRPORT_ISOLATED */
143
		+ nla_total_size(sizeof(struct ifla_bridge_id))	/* IFLA_BRPORT_ROOT_ID */
144
		+ nla_total_size(sizeof(struct ifla_bridge_id))	/* IFLA_BRPORT_BRIDGE_ID */
145
146
		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_DESIGNATED_PORT */
		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_DESIGNATED_COST */
147
148
		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_ID */
		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_NO */
149
150
		+ nla_total_size(sizeof(u8))	/* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
		+ nla_total_size(sizeof(u8))	/* IFLA_BRPORT_CONFIG_PENDING */
151
152
153
		+ nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
		+ nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
		+ nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
154
155
156
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
		+ nla_total_size(sizeof(u8))	/* IFLA_BRPORT_MULTICAST_ROUTER */
#endif
157
		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_GROUP_FWD_MASK */
158
159
160
		+ 0;
}

161
static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
162
163
{
	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
164
165
166
167
168
169
		+ nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
		+ nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
		+ nla_total_size(4) /* IFLA_MASTER */
		+ nla_total_size(4) /* IFLA_MTU */
		+ nla_total_size(4) /* IFLA_LINK */
		+ nla_total_size(1) /* IFLA_OPERSTATE */
170
		+ nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
171
172
		+ nla_total_size(br_get_link_af_size_filtered(dev,
				 filter_mask)); /* IFLA_AF_SPEC */
173
174
175
176
177
178
}

static int br_port_fill_attrs(struct sk_buff *skb,
			      const struct net_bridge_port *p)
{
	u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
179
	u64 timerval;
180
181
182
183

	if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
	    nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
	    nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
184
	    nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
185
	    nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
186
187
188
189
	    nla_put_u8(skb, IFLA_BRPORT_PROTECT,
		       !!(p->flags & BR_ROOT_BLOCK)) ||
	    nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
		       !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Felix Fietkau's avatar
Felix Fietkau committed
190
191
	    nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
		       !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
192
	    nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
193
194
195
196
	    nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
		       !!(p->flags & BR_FLOOD)) ||
	    nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
		       !!(p->flags & BR_MCAST_FLOOD)) ||
197
198
	    nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
		       !!(p->flags & BR_BCAST_FLOOD)) ||
199
200
	    nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
	    nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
201
202
		       !!(p->flags & BR_PROXYARP_WIFI)) ||
	    nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
203
204
		    &p->designated_root) ||
	    nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
205
206
		    &p->designated_bridge) ||
	    nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
207
208
	    nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
	    nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
209
210
211
	    nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
	    nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
		       p->topology_change_ack) ||
212
213
	    nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
	    nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
214
							BR_VLAN_TUNNEL)) ||
215
216
	    nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
	    nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
217
218
		       !!(p->flags & BR_NEIGH_SUPPRESS)) ||
	    nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)))
219
220
		return -EMSGSIZE;

221
	timerval = br_timer_value(&p->message_age_timer);
222
223
	if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
			      IFLA_BRPORT_PAD))
224
225
		return -EMSGSIZE;
	timerval = br_timer_value(&p->forward_delay_timer);
226
227
	if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
			      IFLA_BRPORT_PAD))
228
229
		return -EMSGSIZE;
	timerval = br_timer_value(&p->hold_timer);
230
231
	if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
			      IFLA_BRPORT_PAD))
232
233
		return -EMSGSIZE;

234
235
236
237
238
239
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
	if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
		       p->multicast_router))
		return -EMSGSIZE;
#endif

240
	return 0;
241
242
}

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
				    u16 vid_end, u16 flags)
{
	struct  bridge_vlan_info vinfo;

	if ((vid_end - vid_start) > 0) {
		/* add range to skb */
		vinfo.vid = vid_start;
		vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
			    sizeof(vinfo), &vinfo))
			goto nla_put_failure;

		vinfo.vid = vid_end;
		vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
			    sizeof(vinfo), &vinfo))
			goto nla_put_failure;
	} else {
		vinfo.vid = vid_start;
		vinfo.flags = flags;
		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
			    sizeof(vinfo), &vinfo))
			goto nla_put_failure;
	}

	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
276
					 struct net_bridge_vlan_group *vg)
277
{
278
279
	struct net_bridge_vlan *v;
	u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
280
	u16 flags, pvid;
281
282
283
284
285
286
	int err = 0;

	/* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
	 * and mark vlan info with begin and end flags
	 * if vlaninfo represents a range
	 */
287
	pvid = br_get_pvid(vg);
288
	list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
289
		flags = 0;
290
291
292
		if (!br_vlan_should_use(v))
			continue;
		if (v->vid == pvid)
293
294
			flags |= BRIDGE_VLAN_INFO_PVID;

295
		if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
296
297
298
299
			flags |= BRIDGE_VLAN_INFO_UNTAGGED;

		if (vid_range_start == 0) {
			goto initvars;
300
		} else if ((v->vid - vid_range_end) == 1 &&
301
			flags == vid_range_flags) {
302
			vid_range_end = v->vid;
303
304
305
306
307
308
309
310
311
312
			continue;
		} else {
			err = br_fill_ifvlaninfo_range(skb, vid_range_start,
						       vid_range_end,
						       vid_range_flags);
			if (err)
				return err;
		}

initvars:
313
314
		vid_range_start = v->vid;
		vid_range_end = v->vid;
315
316
317
		vid_range_flags = flags;
	}

318
319
320
321
322
323
324
325
	if (vid_range_start != 0) {
		/* Call it once more to send any left over vlans */
		err = br_fill_ifvlaninfo_range(skb, vid_range_start,
					       vid_range_end,
					       vid_range_flags);
		if (err)
			return err;
	}
326
327
328
329
330

	return 0;
}

static int br_fill_ifvlaninfo(struct sk_buff *skb,
331
			      struct net_bridge_vlan_group *vg)
332
333
{
	struct bridge_vlan_info vinfo;
334
	struct net_bridge_vlan *v;
335
	u16 pvid;
336

337
	pvid = br_get_pvid(vg);
338
	list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
339
340
341
342
		if (!br_vlan_should_use(v))
			continue;

		vinfo.vid = v->vid;
343
		vinfo.flags = 0;
344
		if (v->vid == pvid)
345
346
			vinfo.flags |= BRIDGE_VLAN_INFO_PVID;

347
		if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
348
349
350
351
352
353
354
355
356
357
358
359
360
			vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;

		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
			    sizeof(vinfo), &vinfo))
			goto nla_put_failure;
	}

	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

361
362
363
364
/*
 * Create one netlink message for one interface
 * Contains port and master info as well as carrier and bridge state.
 */
365
static int br_fill_ifinfo(struct sk_buff *skb,
366
			  const struct net_bridge_port *port,
367
368
			  u32 pid, u32 seq, int event, unsigned int flags,
			  u32 filter_mask, const struct net_device *dev)
369
{
370
	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
371
	struct net_bridge *br;
372
	struct ifinfomsg *hdr;
373
374
	struct nlmsghdr *nlh;

375
376
377
378
379
	if (port)
		br = port->br;
	else
		br = netdev_priv(dev);

380
381
	br_debug(br, "br_fill_info event %d port %s master %s\n",
		     event, dev->name, br->dev->name);
382

383
384
	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
	if (nlh == NULL)
385
		return -EMSGSIZE;
386

387
388
389
390
391
392
393
	hdr = nlmsg_data(nlh);
	hdr->ifi_family = AF_BRIDGE;
	hdr->__ifi_pad = 0;
	hdr->ifi_type = dev->type;
	hdr->ifi_index = dev->ifindex;
	hdr->ifi_flags = dev_get_flags(dev);
	hdr->ifi_change = 0;
394

395
396
397
398
399
400
	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
	    nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
	    (dev->addr_len &&
	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
401
402
	    (dev->ifindex != dev_get_iflink(dev) &&
	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
403
		goto nla_put_failure;
404

405
	if (event == RTM_NEWLINK && port) {
406
407
408
409
410
411
412
413
		struct nlattr *nest
			= nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);

		if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
			goto nla_put_failure;
		nla_nest_end(skb, nest);
	}

414
	/* Check if  the VID information is requested */
415
416
	if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
	    (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
417
		struct net_bridge_vlan_group *vg;
418
419
		struct nlattr *af;
		int err;
420

421
422
		/* RCU needed because of the VLAN locking rules (rcu || rtnl) */
		rcu_read_lock();
423
		if (port)
424
			vg = nbp_vlan_group_rcu(port);
425
		else
426
			vg = br_vlan_group_rcu(br);
427

428
429
		if (!vg || !vg->num_vlans) {
			rcu_read_unlock();
430
			goto done;
431
		}
432
		af = nla_nest_start(skb, IFLA_AF_SPEC);
433
434
		if (!af) {
			rcu_read_unlock();
435
			goto nla_put_failure;
436
		}
437
		if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
438
			err = br_fill_ifvlaninfo_compressed(skb, vg);
439
		else
440
			err = br_fill_ifvlaninfo(skb, vg);
441
442
443

		if (port && (port->flags & BR_VLAN_TUNNEL))
			err = br_fill_vlan_tunnel_info(skb, vg);
444
		rcu_read_unlock();
445
446
		if (err)
			goto nla_put_failure;
447
448
449
450
		nla_nest_end(skb, af);
	}

done:
451
452
	nlmsg_end(skb, nlh);
	return 0;
453

454
nla_put_failure:
455
456
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
457
458
}

459
460
461
/* Notify listeners of a change in bridge or port information */
void br_ifinfo_notify(int event, const struct net_bridge *br,
		      const struct net_bridge_port *port)
462
{
463
464
	u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
	struct net_device *dev;
465
	struct sk_buff *skb;
466
	int err = -ENOBUFS;
467
468
	struct net *net;
	u16 port_no = 0;
469

470
	if (WARN_ON(!port && !br))
471
472
		return;

473
474
475
476
477
478
479
480
481
482
	if (port) {
		dev = port->dev;
		br = port->br;
		port_no = port->port_no;
	} else {
		dev = br->dev;
	}

	net = dev_net(dev);
	br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
483

484
	skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
485
486
487
	if (skb == NULL)
		goto errout;

488
	err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev);
489
490
491
492
493
494
	if (err < 0) {
		/* -EMSGSIZE implies BUG in br_nlmsg_size() */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
		goto errout;
	}
495
496
	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
	return;
497
errout:
498
	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
499
500
501
502
503
}

/*
 * Dump information about all ports, in response to GETLINK
 */
504
int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
505
	       struct net_device *dev, u32 filter_mask, int nlflags)
506
{
507
	struct net_bridge_port *port = br_port_get_rtnl(dev);
508

509
510
	if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
	    !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
511
		return 0;
512

513
	return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
514
			      filter_mask, dev);
515
516
}

517
static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
518
			int cmd, struct bridge_vlan_info *vinfo, bool *changed)
519
{
520
	bool curr_change;
521
522
523
524
525
	int err = 0;

	switch (cmd) {
	case RTM_SETLINK:
		if (p) {
526
527
528
			/* if the MASTER flag is set this will act on the global
			 * per-VLAN entry as well
			 */
529
530
			err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
					   &curr_change);
531
		} else {
532
			vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
533
534
			err = br_vlan_add(br, vinfo->vid, vinfo->flags,
					  &curr_change);
535
		}
536
		if (curr_change)
537
			*changed = true;
538
539
540
541
		break;

	case RTM_DELLINK:
		if (p) {
542
543
544
545
546
547
548
549
			if (!nbp_vlan_delete(p, vinfo->vid))
				*changed = true;

			if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
			    !br_vlan_delete(p->br, vinfo->vid))
				*changed = true;
		} else if (!br_vlan_delete(br, vinfo->vid)) {
			*changed = true;
550
551
552
553
554
555
		}
		break;
	}

	return err;
}
556

557
558
559
static int br_process_vlan_info(struct net_bridge *br,
				struct net_bridge_port *p, int cmd,
				struct bridge_vlan_info *vinfo_curr,
560
561
				struct bridge_vlan_info **vinfo_last,
				bool *changed)
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
{
	if (!vinfo_curr->vid || vinfo_curr->vid >= VLAN_VID_MASK)
		return -EINVAL;

	if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
		/* check if we are already processing a range */
		if (*vinfo_last)
			return -EINVAL;
		*vinfo_last = vinfo_curr;
		/* don't allow range of pvids */
		if ((*vinfo_last)->flags & BRIDGE_VLAN_INFO_PVID)
			return -EINVAL;
		return 0;
	}

	if (*vinfo_last) {
		struct bridge_vlan_info tmp_vinfo;
		int v, err;

		if (!(vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END))
			return -EINVAL;

		if (vinfo_curr->vid <= (*vinfo_last)->vid)
			return -EINVAL;

		memcpy(&tmp_vinfo, *vinfo_last,
		       sizeof(struct bridge_vlan_info));
		for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
			tmp_vinfo.vid = v;
591
			err = br_vlan_info(br, p, cmd, &tmp_vinfo, changed);
592
593
594
595
596
			if (err)
				break;
		}
		*vinfo_last = NULL;

597
		return err;
598
599
	}

600
	return br_vlan_info(br, p, cmd, vinfo_curr, changed);
601
602
}

603
604
605
static int br_afspec(struct net_bridge *br,
		     struct net_bridge_port *p,
		     struct nlattr *af_spec,
606
		     int cmd, bool *changed)
607
{
608
609
	struct bridge_vlan_info *vinfo_curr = NULL;
	struct bridge_vlan_info *vinfo_last = NULL;
610
	struct nlattr *attr;
611
612
613
	struct vtunnel_info tinfo_last = {};
	struct vtunnel_info tinfo_curr = {};
	int err = 0, rem;
614

615
	nla_for_each_nested(attr, af_spec, rem) {
616
617
618
		err = 0;
		switch (nla_type(attr)) {
		case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
619
			if (!p || !(p->flags & BR_VLAN_TUNNEL))
620
				return -EINVAL;
621
622
623
624
625
			err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
			if (err)
				return err;
			err = br_process_vlan_tunnel_info(br, p, cmd,
							  &tinfo_curr,
626
627
							  &tinfo_last,
							  changed);
628
629
630
631
632
			if (err)
				return err;
			break;
		case IFLA_BRIDGE_VLAN_INFO:
			if (nla_len(attr) != sizeof(struct bridge_vlan_info))
633
				return -EINVAL;
634
635
			vinfo_curr = nla_data(attr);
			err = br_process_vlan_info(br, p, cmd, vinfo_curr,
636
						   &vinfo_last, changed);
637
638
639
			if (err)
				return err;
			break;
640
		}
641
642
643
644
645
	}

	return err;
}

646
static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
647
648
649
650
	[IFLA_BRPORT_STATE]	= { .type = NLA_U8 },
	[IFLA_BRPORT_COST]	= { .type = NLA_U32 },
	[IFLA_BRPORT_PRIORITY]	= { .type = NLA_U16 },
	[IFLA_BRPORT_MODE]	= { .type = NLA_U8 },
651
	[IFLA_BRPORT_GUARD]	= { .type = NLA_U8 },
652
	[IFLA_BRPORT_PROTECT]	= { .type = NLA_U8 },
653
	[IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
654
	[IFLA_BRPORT_LEARNING]	= { .type = NLA_U8 },
655
	[IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
656
	[IFLA_BRPORT_PROXYARP]	= { .type = NLA_U8 },
657
	[IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
658
	[IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
Felix Fietkau's avatar
Felix Fietkau committed
659
	[IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
660
661
	[IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
	[IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
662
	[IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
663
	[IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
664
	[IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
665
	[IFLA_BRPORT_ISOLATED]	= { .type = NLA_U8 },
666
667
668
669
670
671
672
673
674
675
676
677
};

/* Change the state of the port and notify spanning tree */
static int br_set_port_state(struct net_bridge_port *p, u8 state)
{
	if (state > BR_STATE_BLOCKING)
		return -EINVAL;

	/* if kernel STP is running, don't allow changes */
	if (p->br->stp_enabled == BR_KERNEL_STP)
		return -EBUSY;

678
679
680
	/* if device is not up, change is not allowed
	 * if link is not present, only allowable state is disabled
	 */
681
	if (!netif_running(p->dev) ||
682
	    (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
683
684
		return -ENETDOWN;

685
	br_set_state(p, state);
686
687
688
689
690
	br_port_state_selection(p->br);
	return 0;
}

/* Set/clear or port flags based on attribute */
691
692
static int br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
			    int attrtype, unsigned long mask)
693
{
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
	unsigned long flags;
	int err;

	if (!tb[attrtype])
		return 0;

	if (nla_get_u8(tb[attrtype]))
		flags = p->flags | mask;
	else
		flags = p->flags & ~mask;

	err = br_switchdev_set_port_flag(p, flags, mask);
	if (err)
		return err;

	p->flags = flags;
	return 0;
711
712
713
714
715
}

/* Process bridge protocol info on port */
static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
{
716
	unsigned long old_flags = p->flags;
717
718
	bool br_vlan_tunnel_old = false;
	int err;
719

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
	err = br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
	if (err)
		return err;

	err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
	if (err)
		return err;
763

764
	br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
765
766
767
768
	err = br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
	if (err)
		return err;

769
770
771
	if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
		nbp_vlan_tunnel_info_flush(p);

772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
	if (tb[IFLA_BRPORT_COST]) {
		err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
		if (err)
			return err;
	}

	if (tb[IFLA_BRPORT_PRIORITY]) {
		err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
		if (err)
			return err;
	}

	if (tb[IFLA_BRPORT_STATE]) {
		err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
		if (err)
			return err;
	}
789

790
791
792
	if (tb[IFLA_BRPORT_FLUSH])
		br_fdb_delete_by_port(p->br, p, 0, 0);

793
794
795
796
797
798
799
800
801
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
	if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
		u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);

		err = br_multicast_set_port_router(p, mcast_router);
		if (err)
			return err;
	}
#endif
802
803
804
805
806
807
808
809
810

	if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
		u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);

		if (fwd_mask & BR_GROUPFWD_MACPAUSE)
			return -EINVAL;
		p->group_fwd_mask = fwd_mask;
	}

811
812
813
814
815
	err = br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS,
			       BR_NEIGH_SUPPRESS);
	if (err)
		return err;

816
817
818
819
	err = br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
	if (err)
		return err;

820
	br_port_flags_change(p, old_flags ^ p->flags);
821
822
823
824
	return 0;
}

/* Change state and parameters on port. */
825
int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
826
{
827
828
829
	struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
	struct nlattr *tb[IFLA_BRPORT_MAX + 1];
	struct net_bridge_port *p;
830
	struct nlattr *protinfo;
831
	struct nlattr *afspec;
832
	bool changed = false;
833
	int err = 0;
834

835
836
	protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
	afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
837
	if (!protinfo && !afspec)
838
		return 0;
839

840
	p = br_port_get_rtnl(dev);
841
	/* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger's avatar
stephen hemminger committed
842
	 * is set to see if someone is setting vlan info on the bridge
843
	 */
844
	if (!p && !afspec)
845
		return -EINVAL;
846

847
848
	if (p && protinfo) {
		if (protinfo->nla_type & NLA_F_NESTED) {
849
850
			err = nla_parse_nested(tb, IFLA_BRPORT_MAX, protinfo,
					       br_port_policy, NULL);
851
852
853
854
855
856
857
			if (err)
				return err;

			spin_lock_bh(&p->br->lock);
			err = br_setport(p, tb);
			spin_unlock_bh(&p->br->lock);
		} else {
stephen hemminger's avatar
stephen hemminger committed
858
			/* Binary compatibility with old RSTP */
859
860
861
862
863
864
865
			if (nla_len(protinfo) < sizeof(u8))
				return -EINVAL;

			spin_lock_bh(&p->br->lock);
			err = br_set_port_state(p, nla_get_u8(protinfo));
			spin_unlock_bh(&p->br->lock);
		}
866
		if (err)
867
			goto out;
868
		changed = true;
869
	}
870

871
872
	if (afspec)
		err = br_afspec(br, p, afspec, RTM_SETLINK, &changed);
873

874
	if (changed)
875
		br_ifinfo_notify(RTM_NEWLINK, br, p);
876
out:
877
	return err;
878
879
}

880
/* Delete port information */
881
int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
882
{
883
	struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
884
	struct net_bridge_port *p;
885
	struct nlattr *afspec;
886
	bool changed = false;
887
	int err = 0;
888

889
	afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
890
891
892
893
894
895
896
897
	if (!afspec)
		return 0;

	p = br_port_get_rtnl(dev);
	/* We want to accept dev as bridge itself as well */
	if (!p && !(dev->priv_flags & IFF_EBRIDGE))
		return -EINVAL;

898
	err = br_afspec(br, p, afspec, RTM_DELLINK, &changed);
899
	if (changed)
900
901
902
		/* Send RTM_NEWLINK because userspace
		 * expects RTM_NEWLINK for vlan dels
		 */
903
		br_ifinfo_notify(RTM_NEWLINK, br, p);
904
905
906

	return err;
}
907
908
909

static int br_validate(struct nlattr *tb[], struct nlattr *data[],
		       struct netlink_ext_ack *extack)
910
911
912
913
914
915
916
917
{
	if (tb[IFLA_ADDRESS]) {
		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
			return -EINVAL;
		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
			return -EADDRNOTAVAIL;
	}

918
919
920
921
922
923
924
925
926
927
928
929
930
	if (!data)
		return 0;

#ifdef CONFIG_BRIDGE_VLAN_FILTERING
	if (data[IFLA_BR_VLAN_PROTOCOL]) {
		switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
		case htons(ETH_P_8021Q):
		case htons(ETH_P_8021AD):
			break;
		default:
			return -EPROTONOSUPPORT;
		}
	}
931
932
933
934
935
936
937

	if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
		__u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);

		if (defpvid >= VLAN_VID_MASK)
			return -EINVAL;
	}
938
939
#endif

940
941
942
	return 0;
}

943
944
945
static int br_port_slave_changelink(struct net_device *brdev,
				    struct net_device *dev,
				    struct nlattr *tb[],
946
947
				    struct nlattr *data[],
				    struct netlink_ext_ack *extack)
948
{
949
950
951
	struct net_bridge *br = netdev_priv(brdev);
	int ret;

952
953
	if (!data)
		return 0;
954
955
956
957
958
959

	spin_lock_bh(&br->lock);
	ret = br_setport(br_port_get_rtnl(dev), data);
	spin_unlock_bh(&br->lock);

	return ret;
960
961
}

962
963
964
965
966
967
968
969
970
971
972
973
974
static int br_port_fill_slave_info(struct sk_buff *skb,
				   const struct net_device *brdev,
				   const struct net_device *dev)
{
	return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
}

static size_t br_port_get_slave_size(const struct net_device *brdev,
				     const struct net_device *dev)
{
	return br_port_info_size();
}

975
976
977
978
static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
	[IFLA_BR_FORWARD_DELAY]	= { .type = NLA_U32 },
	[IFLA_BR_HELLO_TIME]	= { .type = NLA_U32 },
	[IFLA_BR_MAX_AGE]	= { .type = NLA_U32 },
979
980
981
	[IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
	[IFLA_BR_STP_STATE] = { .type = NLA_U32 },
	[IFLA_BR_PRIORITY] = { .type = NLA_U16 },
982
	[IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
983
	[IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
984
	[IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
985
986
	[IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
				 .len  = ETH_ALEN },
987
	[IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
988
	[IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
989
	[IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
990
	[IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
991
	[IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
992
	[IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
993
	[IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
994
	[IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
995
996
997
998
999
1000
	[IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
	[IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
	[IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
	[IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
	[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
	[IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },