File Index
Files and directories
Sign in
FILE

br.c

File preview with syntax highlighting.

linux-6.19.14/net/bridge/br.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *	Generic parts
  4 *	Linux ethernet bridge
  5 *
  6 *	Authors:
  7 *	Lennert Buytenhek		<buytenh@gnu.org>
  8 */
  9
 10#include <linux/module.h>
 11#include <linux/kernel.h>
 12#include <linux/netdevice.h>
 13#include <linux/etherdevice.h>
 14#include <linux/init.h>
 15#include <linux/llc.h>
 16#include <net/llc.h>
 17#include <net/stp.h>
 18#include <net/switchdev.h>
 19
 20#include "br_private.h"
 21
 22/*
 23 * Handle changes in state of network devices enslaved to a bridge.
 24 *
 25 * Note: don't care about up/down if bridge itself is down, because
 26 *     port state is checked when bridge is brought up.
 27 */
 28static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
 29{
 30	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
 31	struct netdev_notifier_pre_changeaddr_info *prechaddr_info;
 32	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
 33	struct net_bridge_port *p;
 34	struct net_bridge *br;
 35	bool notified = false;
 36	bool changed_addr;
 37	int err;
 38
 39	if (netif_is_bridge_master(dev)) {
 40		struct net_bridge *br = netdev_priv(dev);
 41
 42		if (event == NETDEV_REGISTER)
 43			br_fdb_change_mac_address(br, dev->dev_addr);
 44
 45		err = br_vlan_bridge_event(dev, event, ptr);
 46		if (err)
 47			return notifier_from_errno(err);
 48
 49		if (event == NETDEV_REGISTER) {
 50			/* register of bridge completed, add sysfs entries */
 51			err = br_sysfs_addbr(dev);
 52			if (err)
 53				return notifier_from_errno(err);
 54
 55			return NOTIFY_DONE;
 56		}
 57	}
 58
 59	if (is_vlan_dev(dev)) {
 60		struct net_device *real_dev = vlan_dev_real_dev(dev);
 61
 62		if (netif_is_bridge_master(real_dev))
 63			br_vlan_vlan_upper_event(real_dev, dev, event);
 64	}
 65
 66	/* not a port of a bridge */
 67	p = br_port_get_rtnl(dev);
 68	if (!p)
 69		return NOTIFY_DONE;
 70
 71	br = p->br;
 72
 73	switch (event) {
 74	case NETDEV_CHANGEMTU:
 75		br_mtu_auto_adjust(br);
 76		break;
 77
 78	case NETDEV_PRE_CHANGEADDR:
 79		if (br->dev->addr_assign_type == NET_ADDR_SET)
 80			break;
 81		prechaddr_info = ptr;
 82		err = netif_pre_changeaddr_notify(br->dev,
 83						  prechaddr_info->dev_addr,
 84						  extack);
 85		if (err)
 86			return notifier_from_errno(err);
 87		break;
 88
 89	case NETDEV_CHANGEADDR:
 90		spin_lock_bh(&br->lock);
 91		br_fdb_changeaddr(p, dev->dev_addr);
 92		changed_addr = br_stp_recalculate_bridge_id(br);
 93		spin_unlock_bh(&br->lock);
 94
 95		if (changed_addr)
 96			call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
 97
 98		break;
 99
100	case NETDEV_CHANGE:
101		br_port_carrier_check(p, &notified);
102		break;
103
104	case NETDEV_FEAT_CHANGE:
105		netdev_update_features(br->dev);
106		break;
107
108	case NETDEV_DOWN:
109		spin_lock_bh(&br->lock);
110		if (br->dev->flags & IFF_UP) {
111			br_stp_disable_port(p);
112			notified = true;
113		}
114		spin_unlock_bh(&br->lock);
115		break;
116
117	case NETDEV_UP:
118		if (netif_running(br->dev) && netif_oper_up(dev)) {
119			spin_lock_bh(&br->lock);
120			br_stp_enable_port(p);
121			notified = true;
122			spin_unlock_bh(&br->lock);
123		}
124		break;
125
126	case NETDEV_UNREGISTER:
127		br_del_if(br, dev);
128		break;
129
130	case NETDEV_CHANGENAME:
131		err = br_sysfs_renameif(p);
132		if (err)
133			return notifier_from_errno(err);
134		break;
135
136	case NETDEV_PRE_TYPE_CHANGE:
137		/* Forbid underlying device to change its type. */
138		return NOTIFY_BAD;
139
140	case NETDEV_RESEND_IGMP:
141		/* Propagate to master device */
142		call_netdevice_notifiers(event, br->dev);
143		break;
144	}
145
146	if (event != NETDEV_UNREGISTER)
147		br_vlan_port_event(p, event);
148
149	/* Events that may cause spanning tree to refresh */
150	if (!notified && (event == NETDEV_CHANGEADDR || event == NETDEV_UP ||
151			  event == NETDEV_CHANGE || event == NETDEV_DOWN))
152		br_ifinfo_notify(RTM_NEWLINK, NULL, p);
153
154	return NOTIFY_DONE;
155}
156
157static struct notifier_block br_device_notifier = {
158	.notifier_call = br_device_event
159};
160
161/* called with RTNL or RCU */
162static int br_switchdev_event(struct notifier_block *unused,
163			      unsigned long event, void *ptr)
164{
165	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
166	struct net_bridge_port *p;
167	struct net_bridge *br;
168	struct switchdev_notifier_fdb_info *fdb_info;
169	int err = NOTIFY_DONE;
170
171	p = br_port_get_rtnl_rcu(dev);
172	if (!p)
173		goto out;
174
175	br = p->br;
176
177	switch (event) {
178	case SWITCHDEV_FDB_ADD_TO_BRIDGE:
179		fdb_info = ptr;
180		err = br_fdb_external_learn_add(br, p, fdb_info->addr,
181						fdb_info->vid,
182						fdb_info->locked, false);
183		if (err) {
184			err = notifier_from_errno(err);
185			break;
186		}
187		br_fdb_offloaded_set(br, p, fdb_info->addr,
188				     fdb_info->vid, fdb_info->offloaded);
189		break;
190	case SWITCHDEV_FDB_DEL_TO_BRIDGE:
191		fdb_info = ptr;
192		err = br_fdb_external_learn_del(br, p, fdb_info->addr,
193						fdb_info->vid, false);
194		if (err)
195			err = notifier_from_errno(err);
196		break;
197	case SWITCHDEV_FDB_OFFLOADED:
198		fdb_info = ptr;
199		br_fdb_offloaded_set(br, p, fdb_info->addr,
200				     fdb_info->vid, fdb_info->offloaded);
201		break;
202	case SWITCHDEV_FDB_FLUSH_TO_BRIDGE:
203		fdb_info = ptr;
204		/* Don't delete static entries */
205		br_fdb_delete_by_port(br, p, fdb_info->vid, 0);
206		break;
207	}
208
209out:
210	return err;
211}
212
213static struct notifier_block br_switchdev_notifier = {
214	.notifier_call = br_switchdev_event,
215};
216
217/* called under rtnl_mutex */
218static int br_switchdev_blocking_event(struct notifier_block *nb,
219				       unsigned long event, void *ptr)
220{
221	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
222	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
223	struct switchdev_notifier_brport_info *brport_info;
224	const struct switchdev_brport *b;
225	struct net_bridge_port *p;
226	int err = NOTIFY_DONE;
227
228	p = br_port_get_rtnl(dev);
229	if (!p)
230		goto out;
231
232	switch (event) {
233	case SWITCHDEV_BRPORT_OFFLOADED:
234		brport_info = ptr;
235		b = &brport_info->brport;
236
237		err = br_switchdev_port_offload(p, b->dev, b->ctx,
238						b->atomic_nb, b->blocking_nb,
239						b->tx_fwd_offload, extack);
240		err = notifier_from_errno(err);
241		break;
242	case SWITCHDEV_BRPORT_UNOFFLOADED:
243		brport_info = ptr;
244		b = &brport_info->brport;
245
246		br_switchdev_port_unoffload(p, b->ctx, b->atomic_nb,
247					    b->blocking_nb);
248		break;
249	case SWITCHDEV_BRPORT_REPLAY:
250		brport_info = ptr;
251		b = &brport_info->brport;
252
253		err = br_switchdev_port_replay(p, b->dev, b->ctx, b->atomic_nb,
254					       b->blocking_nb, extack);
255		err = notifier_from_errno(err);
256		break;
257	}
258
259out:
260	return err;
261}
262
263static struct notifier_block br_switchdev_blocking_notifier = {
264	.notifier_call = br_switchdev_blocking_event,
265};
266
267static int
268br_toggle_fdb_local_vlan_0(struct net_bridge *br, bool on,
269			   struct netlink_ext_ack *extack)
270{
271	int err;
272
273	if (br_opt_get(br, BROPT_FDB_LOCAL_VLAN_0) == on)
274		return 0;
275
276	err = br_fdb_toggle_local_vlan_0(br, on, extack);
277	if (err)
278		return err;
279
280	br_opt_toggle(br, BROPT_FDB_LOCAL_VLAN_0, on);
281	return 0;
282}
283
284/* br_boolopt_toggle - change user-controlled boolean option
285 *
286 * @br: bridge device
287 * @opt: id of the option to change
288 * @on: new option value
289 * @extack: extack for error messages
290 *
291 * Changes the value of the respective boolean option to @on taking care of
292 * any internal option value mapping and configuration.
293 */
294int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
295		      struct netlink_ext_ack *extack)
296{
297	int err = 0;
298
299	switch (opt) {
300	case BR_BOOLOPT_NO_LL_LEARN:
301		br_opt_toggle(br, BROPT_NO_LL_LEARN, on);
302		break;
303	case BR_BOOLOPT_MCAST_VLAN_SNOOPING:
304		err = br_multicast_toggle_vlan_snooping(br, on, extack);
305		break;
306	case BR_BOOLOPT_MST_ENABLE:
307		err = br_mst_set_enabled(br, on, extack);
308		break;
309	case BR_BOOLOPT_MDB_OFFLOAD_FAIL_NOTIFICATION:
310		br_opt_toggle(br, BROPT_MDB_OFFLOAD_FAIL_NOTIFICATION, on);
311		break;
312	case BR_BOOLOPT_FDB_LOCAL_VLAN_0:
313		err = br_toggle_fdb_local_vlan_0(br, on, extack);
314		break;
315	default:
316		/* shouldn't be called with unsupported options */
317		WARN_ON(1);
318		break;
319	}
320
321	return err;
322}
323
324int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt)
325{
326	switch (opt) {
327	case BR_BOOLOPT_NO_LL_LEARN:
328		return br_opt_get(br, BROPT_NO_LL_LEARN);
329	case BR_BOOLOPT_MCAST_VLAN_SNOOPING:
330		return br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED);
331	case BR_BOOLOPT_MST_ENABLE:
332		return br_opt_get(br, BROPT_MST_ENABLED);
333	case BR_BOOLOPT_MDB_OFFLOAD_FAIL_NOTIFICATION:
334		return br_opt_get(br, BROPT_MDB_OFFLOAD_FAIL_NOTIFICATION);
335	case BR_BOOLOPT_FDB_LOCAL_VLAN_0:
336		return br_opt_get(br, BROPT_FDB_LOCAL_VLAN_0);
337	default:
338		/* shouldn't be called with unsupported options */
339		WARN_ON(1);
340		break;
341	}
342
343	return 0;
344}
345
346int br_boolopt_multi_toggle(struct net_bridge *br,
347			    struct br_boolopt_multi *bm,
348			    struct netlink_ext_ack *extack)
349{
350	unsigned long bitmap = bm->optmask;
351	int err = 0;
352	int opt_id;
353
354	opt_id = find_next_bit(&bitmap, BITS_PER_LONG, BR_BOOLOPT_MAX);
355	if (opt_id != BITS_PER_LONG) {
356		NL_SET_ERR_MSG_FMT_MOD(extack, "Unknown boolean option %d",
357				       opt_id);
358		return -EINVAL;
359	}
360
361	for_each_set_bit(opt_id, &bitmap, BR_BOOLOPT_MAX) {
362		bool on = !!(bm->optval & BIT(opt_id));
363
364		err = br_boolopt_toggle(br, opt_id, on, extack);
365		if (err) {
366			br_debug(br, "boolopt multi-toggle error: option: %d current: %d new: %d error: %d\n",
367				 opt_id, br_boolopt_get(br, opt_id), on, err);
368			break;
369		}
370	}
371
372	return err;
373}
374
375void br_boolopt_multi_get(const struct net_bridge *br,
376			  struct br_boolopt_multi *bm)
377{
378	u32 optval = 0;
379	int opt_id;
380
381	for (opt_id = 0; opt_id < BR_BOOLOPT_MAX; opt_id++)
382		optval |= (br_boolopt_get(br, opt_id) << opt_id);
383
384	bm->optval = optval;
385	bm->optmask = GENMASK((BR_BOOLOPT_MAX - 1), 0);
386}
387
388/* private bridge options, controlled by the kernel */
389void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on)
390{
391	bool cur = !!br_opt_get(br, opt);
392
393	br_debug(br, "toggle option: %d state: %d -> %d\n",
394		 opt, cur, on);
395
396	if (cur == on)
397		return;
398
399	if (on)
400		set_bit(opt, &br->options);
401	else
402		clear_bit(opt, &br->options);
403}
404
405static void __net_exit br_net_exit_rtnl(struct net *net,
406					struct list_head *dev_to_kill)
407{
408	struct net_device *dev;
409
410	ASSERT_RTNL_NET(net);
411
412	for_each_netdev(net, dev)
413		if (netif_is_bridge_master(dev))
414			br_dev_delete(dev, dev_to_kill);
415}
416
417static struct pernet_operations br_net_ops = {
418	.exit_rtnl = br_net_exit_rtnl,
419};
420
421static const struct stp_proto br_stp_proto = {
422	.rcv	= br_stp_rcv,
423};
424
425static int __init br_init(void)
426{
427	int err;
428
429	BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > sizeof_field(struct sk_buff, cb));
430
431	err = stp_proto_register(&br_stp_proto);
432	if (err < 0) {
433		pr_err("bridge: can't register sap for STP\n");
434		return err;
435	}
436
437	err = br_fdb_init();
438	if (err)
439		goto err_out;
440
441	err = register_pernet_subsys(&br_net_ops);
442	if (err)
443		goto err_out1;
444
445	err = br_nf_core_init();
446	if (err)
447		goto err_out2;
448
449	err = register_netdevice_notifier(&br_device_notifier);
450	if (err)
451		goto err_out3;
452
453	err = register_switchdev_notifier(&br_switchdev_notifier);
454	if (err)
455		goto err_out4;
456
457	err = register_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
458	if (err)
459		goto err_out5;
460
461	err = br_netlink_init();
462	if (err)
463		goto err_out6;
464
465	brioctl_set(br_ioctl_stub);
466
467#if IS_ENABLED(CONFIG_ATM_LANE)
468	br_fdb_test_addr_hook = br_fdb_test_addr;
469#endif
470
471#if IS_MODULE(CONFIG_BRIDGE_NETFILTER)
472	pr_info("bridge: filtering via arp/ip/ip6tables is no longer available "
473		"by default. Update your scripts to load br_netfilter if you "
474		"need this.\n");
475#endif
476
477	return 0;
478
479err_out6:
480	unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
481err_out5:
482	unregister_switchdev_notifier(&br_switchdev_notifier);
483err_out4:
484	unregister_netdevice_notifier(&br_device_notifier);
485err_out3:
486	br_nf_core_fini();
487err_out2:
488	unregister_pernet_subsys(&br_net_ops);
489err_out1:
490	br_fdb_fini();
491err_out:
492	stp_proto_unregister(&br_stp_proto);
493	return err;
494}
495
496static void __exit br_deinit(void)
497{
498	stp_proto_unregister(&br_stp_proto);
499	br_netlink_fini();
500	unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
501	unregister_switchdev_notifier(&br_switchdev_notifier);
502	unregister_netdevice_notifier(&br_device_notifier);
503	brioctl_set(NULL);
504	unregister_pernet_subsys(&br_net_ops);
505
506	rcu_barrier(); /* Wait for completion of call_rcu()'s */
507
508	br_nf_core_fini();
509#if IS_ENABLED(CONFIG_ATM_LANE)
510	br_fdb_test_addr_hook = NULL;
511#endif
512	br_fdb_fini();
513}
514
515module_init(br_init)
516module_exit(br_deinit)
517MODULE_LICENSE("GPL");
518MODULE_VERSION(BR_VERSION);
519MODULE_ALIAS_RTNL_LINK("bridge");
520MODULE_DESCRIPTION("Ethernet bridge driver");
521MODULE_IMPORT_NS("NETDEV_INTERNAL");
~/ Read-only file preview.