• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Userspace interface
4  *	Linux ethernet bridge
5  *
6  *	Authors:
7  *	Lennert Buytenhek		<buytenh@gnu.org>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netpoll.h>
14 #include <linux/ethtool.h>
15 #include <linux/if_arp.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/if_ether.h>
20 #include <linux/slab.h>
21 #include <net/dsa.h>
22 #include <net/sock.h>
23 #include <linux/if_vlan.h>
24 #include <net/switchdev.h>
25 #include <net/net_namespace.h>
26 
27 #include "br_private.h"
28 
29 /*
30  * Determine initial path cost based on speed.
31  * using recommendations from 802.1d standard
32  *
33  * Since driver might sleep need to not be holding any locks.
34  */
port_cost(struct net_device * dev)35 static int port_cost(struct net_device *dev)
36 {
37 	struct ethtool_link_ksettings ecmd;
38 
39 	if (!__ethtool_get_link_ksettings(dev, &ecmd)) {
40 		switch (ecmd.base.speed) {
41 		case SPEED_10000:
42 			return 2;
43 		case SPEED_1000:
44 			return 4;
45 		case SPEED_100:
46 			return 19;
47 		case SPEED_10:
48 			return 100;
49 		}
50 	}
51 
52 	/* Old silly heuristics based on name */
53 	if (!strncmp(dev->name, "lec", 3))
54 		return 7;
55 
56 	if (!strncmp(dev->name, "plip", 4))
57 		return 2500;
58 
59 	return 100;	/* assume old 10Mbps */
60 }
61 
62 
63 /* Check for port carrier transitions. */
br_port_carrier_check(struct net_bridge_port * p,bool * notified)64 void br_port_carrier_check(struct net_bridge_port *p, bool *notified)
65 {
66 	struct net_device *dev = p->dev;
67 	struct net_bridge *br = p->br;
68 
69 	if (!(p->flags & BR_ADMIN_COST) &&
70 	    netif_running(dev) && netif_oper_up(dev))
71 		p->path_cost = port_cost(dev);
72 
73 	*notified = false;
74 	if (!netif_running(br->dev))
75 		return;
76 
77 	spin_lock_bh(&br->lock);
78 	if (netif_running(dev) && netif_oper_up(dev)) {
79 		if (p->state == BR_STATE_DISABLED) {
80 			br_stp_enable_port(p);
81 			*notified = true;
82 		}
83 	} else {
84 		if (p->state != BR_STATE_DISABLED) {
85 			br_stp_disable_port(p);
86 			*notified = true;
87 		}
88 	}
89 	spin_unlock_bh(&br->lock);
90 }
91 
br_port_set_promisc(struct net_bridge_port * p)92 static void br_port_set_promisc(struct net_bridge_port *p)
93 {
94 	int err = 0;
95 
96 	if (br_promisc_port(p))
97 		return;
98 
99 	err = dev_set_promiscuity(p->dev, 1);
100 	if (err)
101 		return;
102 
103 	br_fdb_unsync_static(p->br, p);
104 	p->flags |= BR_PROMISC;
105 }
106 
br_port_clear_promisc(struct net_bridge_port * p)107 static void br_port_clear_promisc(struct net_bridge_port *p)
108 {
109 	int err;
110 
111 	/* Check if the port is already non-promisc or if it doesn't
112 	 * support UNICAST filtering.  Without unicast filtering support
113 	 * we'll end up re-enabling promisc mode anyway, so just check for
114 	 * it here.
115 	 */
116 	if (!br_promisc_port(p) || !(p->dev->priv_flags & IFF_UNICAST_FLT))
117 		return;
118 
119 	/* Since we'll be clearing the promisc mode, program the port
120 	 * first so that we don't have interruption in traffic.
121 	 */
122 	err = br_fdb_sync_static(p->br, p);
123 	if (err)
124 		return;
125 
126 	dev_set_promiscuity(p->dev, -1);
127 	p->flags &= ~BR_PROMISC;
128 }
129 
130 /* When a port is added or removed or when certain port flags
131  * change, this function is called to automatically manage
132  * promiscuity setting of all the bridge ports.  We are always called
133  * under RTNL so can skip using rcu primitives.
134  */
br_manage_promisc(struct net_bridge * br)135 void br_manage_promisc(struct net_bridge *br)
136 {
137 	struct net_bridge_port *p;
138 	bool set_all = false;
139 
140 	/* If vlan filtering is disabled or bridge interface is placed
141 	 * into promiscuous mode, place all ports in promiscuous mode.
142 	 */
143 	if ((br->dev->flags & IFF_PROMISC) || !br_vlan_enabled(br->dev))
144 		set_all = true;
145 
146 	list_for_each_entry(p, &br->port_list, list) {
147 		if (set_all) {
148 			br_port_set_promisc(p);
149 		} else {
150 			/* If the number of auto-ports is <= 1, then all other
151 			 * ports will have their output configuration
152 			 * statically specified through fdbs.  Since ingress
153 			 * on the auto-port becomes forwarding/egress to other
154 			 * ports and egress configuration is statically known,
155 			 * we can say that ingress configuration of the
156 			 * auto-port is also statically known.
157 			 * This lets us disable promiscuous mode and write
158 			 * this config to hw.
159 			 */
160 			if ((p->dev->priv_flags & IFF_UNICAST_FLT) &&
161 			    (br->auto_cnt == 0 ||
162 			     (br->auto_cnt == 1 && br_auto_port(p))))
163 				br_port_clear_promisc(p);
164 			else
165 				br_port_set_promisc(p);
166 		}
167 	}
168 }
169 
nbp_backup_change(struct net_bridge_port * p,struct net_device * backup_dev)170 int nbp_backup_change(struct net_bridge_port *p,
171 		      struct net_device *backup_dev)
172 {
173 	struct net_bridge_port *old_backup = rtnl_dereference(p->backup_port);
174 	struct net_bridge_port *backup_p = NULL;
175 
176 	ASSERT_RTNL();
177 
178 	if (backup_dev) {
179 		if (!netif_is_bridge_port(backup_dev))
180 			return -ENOENT;
181 
182 		backup_p = br_port_get_rtnl(backup_dev);
183 		if (backup_p->br != p->br)
184 			return -EINVAL;
185 	}
186 
187 	if (p == backup_p)
188 		return -EINVAL;
189 
190 	if (old_backup == backup_p)
191 		return 0;
192 
193 	/* if the backup link is already set, clear it */
194 	if (old_backup)
195 		old_backup->backup_redirected_cnt--;
196 
197 	if (backup_p)
198 		backup_p->backup_redirected_cnt++;
199 	rcu_assign_pointer(p->backup_port, backup_p);
200 
201 	return 0;
202 }
203 
nbp_backup_clear(struct net_bridge_port * p)204 static void nbp_backup_clear(struct net_bridge_port *p)
205 {
206 	nbp_backup_change(p, NULL);
207 	if (p->backup_redirected_cnt) {
208 		struct net_bridge_port *cur_p;
209 
210 		list_for_each_entry(cur_p, &p->br->port_list, list) {
211 			struct net_bridge_port *backup_p;
212 
213 			backup_p = rtnl_dereference(cur_p->backup_port);
214 			if (backup_p == p)
215 				nbp_backup_change(cur_p, NULL);
216 		}
217 	}
218 
219 	WARN_ON(rcu_access_pointer(p->backup_port) || p->backup_redirected_cnt);
220 }
221 
nbp_update_port_count(struct net_bridge * br)222 static void nbp_update_port_count(struct net_bridge *br)
223 {
224 	struct net_bridge_port *p;
225 	u32 cnt = 0;
226 
227 	list_for_each_entry(p, &br->port_list, list) {
228 		if (br_auto_port(p))
229 			cnt++;
230 	}
231 	if (br->auto_cnt != cnt) {
232 		br->auto_cnt = cnt;
233 		br_manage_promisc(br);
234 	}
235 }
236 
nbp_delete_promisc(struct net_bridge_port * p)237 static void nbp_delete_promisc(struct net_bridge_port *p)
238 {
239 	/* If port is currently promiscuous, unset promiscuity.
240 	 * Otherwise, it is a static port so remove all addresses
241 	 * from it.
242 	 */
243 	dev_set_allmulti(p->dev, -1);
244 	if (br_promisc_port(p))
245 		dev_set_promiscuity(p->dev, -1);
246 	else
247 		br_fdb_unsync_static(p->br, p);
248 }
249 
release_nbp(struct kobject * kobj)250 static void release_nbp(struct kobject *kobj)
251 {
252 	struct net_bridge_port *p
253 		= container_of(kobj, struct net_bridge_port, kobj);
254 	kfree(p);
255 }
256 
brport_get_ownership(struct kobject * kobj,kuid_t * uid,kgid_t * gid)257 static void brport_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
258 {
259 	struct net_bridge_port *p = kobj_to_brport(kobj);
260 
261 	net_ns_get_ownership(dev_net(p->dev), uid, gid);
262 }
263 
264 static struct kobj_type brport_ktype = {
265 #ifdef CONFIG_SYSFS
266 	.sysfs_ops = &brport_sysfs_ops,
267 #endif
268 	.release = release_nbp,
269 	.get_ownership = brport_get_ownership,
270 };
271 
destroy_nbp(struct net_bridge_port * p)272 static void destroy_nbp(struct net_bridge_port *p)
273 {
274 	struct net_device *dev = p->dev;
275 
276 	p->br = NULL;
277 	p->dev = NULL;
278 	dev_put(dev);
279 
280 	kobject_put(&p->kobj);
281 }
282 
destroy_nbp_rcu(struct rcu_head * head)283 static void destroy_nbp_rcu(struct rcu_head *head)
284 {
285 	struct net_bridge_port *p =
286 			container_of(head, struct net_bridge_port, rcu);
287 	destroy_nbp(p);
288 }
289 
get_max_headroom(struct net_bridge * br)290 static unsigned get_max_headroom(struct net_bridge *br)
291 {
292 	unsigned max_headroom = 0;
293 	struct net_bridge_port *p;
294 
295 	list_for_each_entry(p, &br->port_list, list) {
296 		unsigned dev_headroom = netdev_get_fwd_headroom(p->dev);
297 
298 		if (dev_headroom > max_headroom)
299 			max_headroom = dev_headroom;
300 	}
301 
302 	return max_headroom;
303 }
304 
update_headroom(struct net_bridge * br,int new_hr)305 static void update_headroom(struct net_bridge *br, int new_hr)
306 {
307 	struct net_bridge_port *p;
308 
309 	list_for_each_entry(p, &br->port_list, list)
310 		netdev_set_rx_headroom(p->dev, new_hr);
311 
312 	br->dev->needed_headroom = new_hr;
313 }
314 
315 /* Delete port(interface) from bridge is done in two steps.
316  * via RCU. First step, marks device as down. That deletes
317  * all the timers and stops new packets from flowing through.
318  *
319  * Final cleanup doesn't occur until after all CPU's finished
320  * processing packets.
321  *
322  * Protected from multiple admin operations by RTNL mutex
323  */
del_nbp(struct net_bridge_port * p)324 static void del_nbp(struct net_bridge_port *p)
325 {
326 	struct net_bridge *br = p->br;
327 	struct net_device *dev = p->dev;
328 
329 	sysfs_remove_link(br->ifobj, p->dev->name);
330 
331 	nbp_delete_promisc(p);
332 
333 	spin_lock_bh(&br->lock);
334 	br_stp_disable_port(p);
335 	spin_unlock_bh(&br->lock);
336 
337 	br_mrp_port_del(br, p);
338 
339 	br_ifinfo_notify(RTM_DELLINK, NULL, p);
340 
341 	list_del_rcu(&p->list);
342 	if (netdev_get_fwd_headroom(dev) == br->dev->needed_headroom)
343 		update_headroom(br, get_max_headroom(br));
344 	netdev_reset_rx_headroom(dev);
345 
346 	nbp_vlan_flush(p);
347 	br_fdb_delete_by_port(br, p, 0, 1);
348 	switchdev_deferred_process();
349 	nbp_backup_clear(p);
350 
351 	nbp_update_port_count(br);
352 
353 	netdev_upper_dev_unlink(dev, br->dev);
354 
355 	dev->priv_flags &= ~IFF_BRIDGE_PORT;
356 
357 	netdev_rx_handler_unregister(dev);
358 
359 	br_multicast_del_port(p);
360 
361 	kobject_uevent(&p->kobj, KOBJ_REMOVE);
362 	kobject_del(&p->kobj);
363 
364 	br_netpoll_disable(p);
365 
366 	call_rcu(&p->rcu, destroy_nbp_rcu);
367 }
368 
369 /* Delete bridge device */
br_dev_delete(struct net_device * dev,struct list_head * head)370 void br_dev_delete(struct net_device *dev, struct list_head *head)
371 {
372 	struct net_bridge *br = netdev_priv(dev);
373 	struct net_bridge_port *p, *n;
374 
375 	list_for_each_entry_safe(p, n, &br->port_list, list) {
376 		del_nbp(p);
377 	}
378 
379 	br_recalculate_neigh_suppress_enabled(br);
380 
381 	br_fdb_delete_by_port(br, NULL, 0, 1);
382 
383 	cancel_delayed_work_sync(&br->gc_work);
384 
385 	br_sysfs_delbr(br->dev);
386 	unregister_netdevice_queue(br->dev, head);
387 }
388 
389 /* find an available port number */
find_portno(struct net_bridge * br)390 static int find_portno(struct net_bridge *br)
391 {
392 	int index;
393 	struct net_bridge_port *p;
394 	unsigned long *inuse;
395 
396 	inuse = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
397 	if (!inuse)
398 		return -ENOMEM;
399 
400 	set_bit(0, inuse);	/* zero is reserved */
401 	list_for_each_entry(p, &br->port_list, list) {
402 		set_bit(p->port_no, inuse);
403 	}
404 	index = find_first_zero_bit(inuse, BR_MAX_PORTS);
405 	bitmap_free(inuse);
406 
407 	return (index >= BR_MAX_PORTS) ? -EXFULL : index;
408 }
409 
410 /* called with RTNL but without bridge lock */
new_nbp(struct net_bridge * br,struct net_device * dev)411 static struct net_bridge_port *new_nbp(struct net_bridge *br,
412 				       struct net_device *dev)
413 {
414 	struct net_bridge_port *p;
415 	int index, err;
416 
417 	index = find_portno(br);
418 	if (index < 0)
419 		return ERR_PTR(index);
420 
421 	p = kzalloc(sizeof(*p), GFP_KERNEL);
422 	if (p == NULL)
423 		return ERR_PTR(-ENOMEM);
424 
425 	p->br = br;
426 	dev_hold(dev);
427 	p->dev = dev;
428 	p->path_cost = port_cost(dev);
429 	p->priority = 0x8000 >> BR_PORT_BITS;
430 	p->port_no = index;
431 	p->flags = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
432 	br_init_port(p);
433 	br_set_state(p, BR_STATE_DISABLED);
434 	br_stp_port_timer_init(p);
435 	err = br_multicast_add_port(p);
436 	if (err) {
437 		dev_put(dev);
438 		kfree(p);
439 		p = ERR_PTR(err);
440 	}
441 
442 	return p;
443 }
444 
br_add_bridge(struct net * net,const char * name)445 int br_add_bridge(struct net *net, const char *name)
446 {
447 	struct net_device *dev;
448 	int res;
449 
450 	dev = alloc_netdev(sizeof(struct net_bridge), name, NET_NAME_UNKNOWN,
451 			   br_dev_setup);
452 
453 	if (!dev)
454 		return -ENOMEM;
455 
456 	dev_net_set(dev, net);
457 	dev->rtnl_link_ops = &br_link_ops;
458 
459 	res = register_netdev(dev);
460 	if (res)
461 		free_netdev(dev);
462 	return res;
463 }
464 
br_del_bridge(struct net * net,const char * name)465 int br_del_bridge(struct net *net, const char *name)
466 {
467 	struct net_device *dev;
468 	int ret = 0;
469 
470 	rtnl_lock();
471 	dev = __dev_get_by_name(net, name);
472 	if (dev == NULL)
473 		ret =  -ENXIO; 	/* Could not find device */
474 
475 	else if (!(dev->priv_flags & IFF_EBRIDGE)) {
476 		/* Attempt to delete non bridge device! */
477 		ret = -EPERM;
478 	}
479 
480 	else if (dev->flags & IFF_UP) {
481 		/* Not shutdown yet. */
482 		ret = -EBUSY;
483 	}
484 
485 	else
486 		br_dev_delete(dev, NULL);
487 
488 	rtnl_unlock();
489 	return ret;
490 }
491 
492 /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
br_mtu_min(const struct net_bridge * br)493 static int br_mtu_min(const struct net_bridge *br)
494 {
495 	const struct net_bridge_port *p;
496 	int ret_mtu = 0;
497 
498 	list_for_each_entry(p, &br->port_list, list)
499 		if (!ret_mtu || ret_mtu > p->dev->mtu)
500 			ret_mtu = p->dev->mtu;
501 
502 	return ret_mtu ? ret_mtu : ETH_DATA_LEN;
503 }
504 
br_mtu_auto_adjust(struct net_bridge * br)505 void br_mtu_auto_adjust(struct net_bridge *br)
506 {
507 	ASSERT_RTNL();
508 
509 	/* if the bridge MTU was manually configured don't mess with it */
510 	if (br_opt_get(br, BROPT_MTU_SET_BY_USER))
511 		return;
512 
513 	/* change to the minimum MTU and clear the flag which was set by
514 	 * the bridge ndo_change_mtu callback
515 	 */
516 	dev_set_mtu(br->dev, br_mtu_min(br));
517 	br_opt_toggle(br, BROPT_MTU_SET_BY_USER, false);
518 }
519 
br_set_gso_limits(struct net_bridge * br)520 static void br_set_gso_limits(struct net_bridge *br)
521 {
522 	unsigned int gso_max_size = GSO_MAX_SIZE;
523 	u16 gso_max_segs = GSO_MAX_SEGS;
524 	const struct net_bridge_port *p;
525 
526 	list_for_each_entry(p, &br->port_list, list) {
527 		gso_max_size = min(gso_max_size, p->dev->gso_max_size);
528 		gso_max_segs = min(gso_max_segs, p->dev->gso_max_segs);
529 	}
530 	br->dev->gso_max_size = gso_max_size;
531 	br->dev->gso_max_segs = gso_max_segs;
532 }
533 
534 /*
535  * Recomputes features using slave's features
536  */
br_features_recompute(struct net_bridge * br,netdev_features_t features)537 netdev_features_t br_features_recompute(struct net_bridge *br,
538 	netdev_features_t features)
539 {
540 	struct net_bridge_port *p;
541 	netdev_features_t mask;
542 
543 	if (list_empty(&br->port_list))
544 		return features;
545 
546 	mask = features;
547 	features &= ~NETIF_F_ONE_FOR_ALL;
548 
549 	list_for_each_entry(p, &br->port_list, list) {
550 		features = netdev_increment_features(features,
551 						     p->dev->features, mask);
552 	}
553 	features = netdev_add_tso_features(features, mask);
554 
555 	return features;
556 }
557 
558 /* called with RTNL */
br_add_if(struct net_bridge * br,struct net_device * dev,struct netlink_ext_ack * extack)559 int br_add_if(struct net_bridge *br, struct net_device *dev,
560 	      struct netlink_ext_ack *extack)
561 {
562 	struct net_bridge_port *p;
563 	int err = 0;
564 	unsigned br_hr, dev_hr;
565 	bool changed_addr, fdb_synced = false;
566 
567 	/* Don't allow bridging non-ethernet like devices. */
568 	if ((dev->flags & IFF_LOOPBACK) ||
569 	    dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
570 	    !is_valid_ether_addr(dev->dev_addr))
571 		return -EINVAL;
572 
573 	/* Also don't allow bridging of net devices that are DSA masters, since
574 	 * the bridge layer rx_handler prevents the DSA fake ethertype handler
575 	 * to be invoked, so we don't get the chance to strip off and parse the
576 	 * DSA switch tag protocol header (the bridge layer just returns
577 	 * RX_HANDLER_CONSUMED, stopping RX processing for these frames).
578 	 * The only case where that would not be an issue is when bridging can
579 	 * already be offloaded, such as when the DSA master is itself a DSA
580 	 * or plain switchdev port, and is bridged only with other ports from
581 	 * the same hardware device.
582 	 */
583 	if (netdev_uses_dsa(dev)) {
584 		list_for_each_entry(p, &br->port_list, list) {
585 			if (!netdev_port_same_parent_id(dev, p->dev)) {
586 				NL_SET_ERR_MSG(extack,
587 					       "Cannot do software bridging with a DSA master");
588 				return -EINVAL;
589 			}
590 		}
591 	}
592 
593 	/* No bridging of bridges */
594 	if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit) {
595 		NL_SET_ERR_MSG(extack,
596 			       "Can not enslave a bridge to a bridge");
597 		return -ELOOP;
598 	}
599 
600 	/* Device has master upper dev */
601 	if (netdev_master_upper_dev_get(dev))
602 		return -EBUSY;
603 
604 	/* No bridging devices that dislike that (e.g. wireless) */
605 	if (dev->priv_flags & IFF_DONT_BRIDGE) {
606 		NL_SET_ERR_MSG(extack,
607 			       "Device does not allow enslaving to a bridge");
608 		return -EOPNOTSUPP;
609 	}
610 
611 	p = new_nbp(br, dev);
612 	if (IS_ERR(p))
613 		return PTR_ERR(p);
614 
615 	call_netdevice_notifiers(NETDEV_JOIN, dev);
616 
617 	err = dev_set_allmulti(dev, 1);
618 	if (err) {
619 		br_multicast_del_port(p);
620 		kfree(p);	/* kobject not yet init'd, manually free */
621 		goto err1;
622 	}
623 
624 	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
625 				   SYSFS_BRIDGE_PORT_ATTR);
626 	if (err)
627 		goto err2;
628 
629 	err = br_sysfs_addif(p);
630 	if (err)
631 		goto err2;
632 
633 	err = br_netpoll_enable(p);
634 	if (err)
635 		goto err3;
636 
637 	err = netdev_rx_handler_register(dev, br_get_rx_handler(dev), p);
638 	if (err)
639 		goto err4;
640 
641 	dev->priv_flags |= IFF_BRIDGE_PORT;
642 
643 	err = netdev_master_upper_dev_link(dev, br->dev, NULL, NULL, extack);
644 	if (err)
645 		goto err5;
646 
647 	err = nbp_switchdev_mark_set(p);
648 	if (err)
649 		goto err6;
650 
651 	dev_disable_lro(dev);
652 
653 	list_add_rcu(&p->list, &br->port_list);
654 
655 	nbp_update_port_count(br);
656 	if (!br_promisc_port(p) && (p->dev->priv_flags & IFF_UNICAST_FLT)) {
657 		/* When updating the port count we also update all ports'
658 		 * promiscuous mode.
659 		 * A port leaving promiscuous mode normally gets the bridge's
660 		 * fdb synced to the unicast filter (if supported), however,
661 		 * `br_port_clear_promisc` does not distinguish between
662 		 * non-promiscuous ports and *new* ports, so we need to
663 		 * sync explicitly here.
664 		 */
665 		fdb_synced = br_fdb_sync_static(br, p) == 0;
666 		if (!fdb_synced)
667 			netdev_err(dev, "failed to sync bridge static fdb addresses to this port\n");
668 	}
669 
670 	netdev_update_features(br->dev);
671 
672 	br_hr = br->dev->needed_headroom;
673 	dev_hr = netdev_get_fwd_headroom(dev);
674 	if (br_hr < dev_hr)
675 		update_headroom(br, dev_hr);
676 	else
677 		netdev_set_rx_headroom(dev, br_hr);
678 
679 	if (br_fdb_insert(br, p, dev->dev_addr, 0))
680 		netdev_err(dev, "failed insert local address bridge forwarding table\n");
681 
682 	if (br->dev->addr_assign_type != NET_ADDR_SET) {
683 		/* Ask for permission to use this MAC address now, even if we
684 		 * don't end up choosing it below.
685 		 */
686 		err = dev_pre_changeaddr_notify(br->dev, dev->dev_addr, extack);
687 		if (err)
688 			goto err7;
689 	}
690 
691 	err = nbp_vlan_init(p, extack);
692 	if (err) {
693 		netdev_err(dev, "failed to initialize vlan filtering on this port\n");
694 		goto err7;
695 	}
696 
697 	spin_lock_bh(&br->lock);
698 	changed_addr = br_stp_recalculate_bridge_id(br);
699 
700 	if (netif_running(dev) && netif_oper_up(dev) &&
701 	    (br->dev->flags & IFF_UP))
702 		br_stp_enable_port(p);
703 	spin_unlock_bh(&br->lock);
704 
705 	br_ifinfo_notify(RTM_NEWLINK, NULL, p);
706 
707 	if (changed_addr)
708 		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
709 
710 	br_mtu_auto_adjust(br);
711 	br_set_gso_limits(br);
712 
713 	kobject_uevent(&p->kobj, KOBJ_ADD);
714 
715 	return 0;
716 
717 err7:
718 	if (fdb_synced)
719 		br_fdb_unsync_static(br, p);
720 	list_del_rcu(&p->list);
721 	br_fdb_delete_by_port(br, p, 0, 1);
722 	nbp_update_port_count(br);
723 err6:
724 	netdev_upper_dev_unlink(dev, br->dev);
725 err5:
726 	dev->priv_flags &= ~IFF_BRIDGE_PORT;
727 	netdev_rx_handler_unregister(dev);
728 err4:
729 	br_netpoll_disable(p);
730 err3:
731 	sysfs_remove_link(br->ifobj, p->dev->name);
732 err2:
733 	br_multicast_del_port(p);
734 	kobject_put(&p->kobj);
735 	dev_set_allmulti(dev, -1);
736 err1:
737 	dev_put(dev);
738 	return err;
739 }
740 
741 /* called with RTNL */
br_del_if(struct net_bridge * br,struct net_device * dev)742 int br_del_if(struct net_bridge *br, struct net_device *dev)
743 {
744 	struct net_bridge_port *p;
745 	bool changed_addr;
746 
747 	p = br_port_get_rtnl(dev);
748 	if (!p || p->br != br)
749 		return -EINVAL;
750 
751 	/* Since more than one interface can be attached to a bridge,
752 	 * there still maybe an alternate path for netconsole to use;
753 	 * therefore there is no reason for a NETDEV_RELEASE event.
754 	 */
755 	del_nbp(p);
756 
757 	br_mtu_auto_adjust(br);
758 	br_set_gso_limits(br);
759 
760 	spin_lock_bh(&br->lock);
761 	changed_addr = br_stp_recalculate_bridge_id(br);
762 	spin_unlock_bh(&br->lock);
763 
764 	if (changed_addr)
765 		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
766 
767 	netdev_update_features(br->dev);
768 
769 	return 0;
770 }
771 
br_port_flags_change(struct net_bridge_port * p,unsigned long mask)772 void br_port_flags_change(struct net_bridge_port *p, unsigned long mask)
773 {
774 	struct net_bridge *br = p->br;
775 
776 	if (mask & BR_AUTO_MASK)
777 		nbp_update_port_count(br);
778 
779 	if (mask & BR_NEIGH_SUPPRESS)
780 		br_recalculate_neigh_suppress_enabled(br);
781 }
782 
br_port_flag_is_set(const struct net_device * dev,unsigned long flag)783 bool br_port_flag_is_set(const struct net_device *dev, unsigned long flag)
784 {
785 	struct net_bridge_port *p;
786 
787 	p = br_port_get_rtnl_rcu(dev);
788 	if (!p)
789 		return false;
790 
791 	return p->flags & flag;
792 }
793 EXPORT_SYMBOL_GPL(br_port_flag_is_set);
794