blob: a76b62135558a06308cc76adff65de3d704d76bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Device event handling
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070015#include <linux/rtnetlink.h>
Eric W. Biedermane9dc8652007-09-12 13:02:17 +020016#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include "br_private.h"
19
20static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr);
21
22struct notifier_block br_device_notifier = {
23 .notifier_call = br_device_event
24};
25
26/*
27 * Handle changes in state of network devices enslaved to a bridge.
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * Note: don't care about up/down if bridge itself is down, because
30 * port state is checked when bridge is brought up.
31 */
32static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
33{
34 struct net_device *dev = ptr;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +000035 struct net_bridge_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct net_bridge *br;
Andrei Warkentin9be6dd62011-08-05 11:04:10 +000037 bool changed_addr;
Simon Arlotte0f43752010-05-10 09:31:11 +000038 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
stephen hemmingerbb900b22011-04-04 14:03:32 +000040 /* register of bridge completed, add sysfs entries */
Stephen Hemminger28674b92011-04-17 17:52:51 -070041 if ((dev->priv_flags & IFF_EBRIDGE) && event == NETDEV_REGISTER) {
stephen hemmingerbb900b22011-04-04 14:03:32 +000042 br_sysfs_addbr(dev);
43 return NOTIFY_DONE;
44 }
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 /* not a port of a bridge */
Eric Dumazetec1e5612010-11-15 06:38:14 +000047 p = br_port_get_rtnl(dev);
48 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return NOTIFY_DONE;
50
51 br = p->br;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 switch (event) {
54 case NETDEV_CHANGEMTU:
55 dev_set_mtu(br->dev, br_min_mtu(br));
56 break;
57
58 case NETDEV_CHANGEADDR:
Stephen Hemminger269def72007-02-22 01:10:18 -080059 spin_lock_bh(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 br_fdb_changeaddr(p, dev->dev_addr);
Andrei Warkentin9be6dd62011-08-05 11:04:10 +000061 changed_addr = br_stp_recalculate_bridge_id(br);
Stephen Hemminger269def72007-02-22 01:10:18 -080062 spin_unlock_bh(&br->lock);
Andrei Warkentin9be6dd62011-08-05 11:04:10 +000063
64 if (changed_addr)
65 call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
68
Stephen Hemminger4433f422005-12-20 15:19:51 -080069 case NETDEV_CHANGE:
Stephen Hemminger269def72007-02-22 01:10:18 -080070 br_port_carrier_check(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 break;
72
Stephen Hemminger81d35302005-05-29 14:15:17 -070073 case NETDEV_FEAT_CHANGE:
Michał Mirosławc4d27ef2011-04-22 06:31:16 +000074 netdev_update_features(br->dev);
Stephen Hemminger81d35302005-05-29 14:15:17 -070075 break;
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 case NETDEV_DOWN:
Stephen Hemminger269def72007-02-22 01:10:18 -080078 spin_lock_bh(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (br->dev->flags & IFF_UP)
80 br_stp_disable_port(p);
Stephen Hemminger269def72007-02-22 01:10:18 -080081 spin_unlock_bh(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83
84 case NETDEV_UP:
Stephen Hemmingerb86c4502007-03-22 14:08:46 -070085 if (netif_carrier_ok(dev) && (br->dev->flags & IFF_UP)) {
86 spin_lock_bh(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 br_stp_enable_port(p);
Stephen Hemmingerb86c4502007-03-22 14:08:46 -070088 spin_unlock_bh(&br->lock);
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break;
91
92 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 br_del_if(br, dev);
Stephen Hemminger269def72007-02-22 01:10:18 -080094 break;
Jiri Pirko1c01fe12010-03-10 10:30:19 +000095
Simon Arlotte0f43752010-05-10 09:31:11 +000096 case NETDEV_CHANGENAME:
97 err = br_sysfs_renameif(p);
98 if (err)
99 return notifier_from_errno(err);
100 break;
101
Jiri Pirko1c01fe12010-03-10 10:30:19 +0000102 case NETDEV_PRE_TYPE_CHANGE:
103 /* Forbid underlaying device to change its type. */
104 return NOTIFY_BAD;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Stephen Hemmingerb86c4502007-03-22 14:08:46 -0700107 /* Events that may cause spanning tree to refresh */
108 if (event == NETDEV_CHANGEADDR || event == NETDEV_UP ||
109 event == NETDEV_CHANGE || event == NETDEV_DOWN)
110 br_ifinfo_notify(RTM_NEWLINK, p);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return NOTIFY_DONE;
113}