3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * $Id: br_if.c,v 1.7 2001/12/24 00:59:55 davem Exp $
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/if_arp.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/rtnetlink.h>
25 #include "br_private.h"
28 * Determine initial path cost based on speed.
29 * using recommendations from 802.1d standard
31 * Need to simulate user ioctl because not all device's that support
32 * ethtool, use ethtool_ops. Also, since driver might sleep need to
33 * not be holding any locks.
35 static int br_initial_port_cost(struct net_device *dev)
38 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
43 strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
44 ifr.ifr_data = (void *) &ecmd;
48 err = dev_ethtool(&ifr);
62 pr_info("bridge: can't decode speed from %s: %d\n",
63 dev->name, ecmd.speed);
68 /* Old silly heuristics based on name */
69 if (!strncmp(dev->name, "lec", 3))
72 if (!strncmp(dev->name, "plip", 4))
75 return 100; /* assume old 10Mbps */
78 static void destroy_nbp(void *arg)
80 struct net_bridge_port *p = arg;
81 struct net_device *dev = p->dev;
91 /* called with RTNL */
92 static void del_nbp(struct net_bridge_port *p)
94 struct net_bridge *br = p->br;
95 struct net_device *dev = p->dev;
97 dev_set_promiscuity(dev, -1);
99 spin_lock_bh(&br->lock);
100 br_stp_disable_port(p);
101 spin_unlock_bh(&br->lock);
103 br_fdb_delete_by_port(br, p);
105 list_del_rcu(&p->list);
107 del_timer_sync(&p->message_age_timer);
108 del_timer_sync(&p->forward_delay_timer);
109 del_timer_sync(&p->hold_timer);
111 call_rcu(&p->rcu, destroy_nbp, p);
114 /* called with RTNL */
115 static void del_br(struct net_bridge *br)
117 struct net_bridge_port *p, *n;
119 list_for_each_entry_safe(p, n, &br->port_list, list) {
120 br_sysfs_removeif(p);
124 del_timer_sync(&br->gc_timer);
126 br_sysfs_delbr(br->dev);
127 unregister_netdevice(br->dev);
130 static struct net_device *new_bridge_dev(const char *name)
132 struct net_bridge *br;
133 struct net_device *dev;
135 dev = alloc_netdev(sizeof(struct net_bridge), name,
141 br = netdev_priv(dev);
144 br->lock = SPIN_LOCK_UNLOCKED;
145 INIT_LIST_HEAD(&br->port_list);
146 br->hash_lock = RW_LOCK_UNLOCKED;
148 br->bridge_id.prio[0] = 0x80;
149 br->bridge_id.prio[1] = 0x00;
150 memset(br->bridge_id.addr, 0, ETH_ALEN);
153 br->designated_root = br->bridge_id;
154 br->root_path_cost = 0;
156 br->bridge_max_age = br->max_age = 20 * HZ;
157 br->bridge_hello_time = br->hello_time = 2 * HZ;
158 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
159 br->topology_change = 0;
160 br->topology_change_detected = 0;
161 br->ageing_time = 300 * HZ;
162 INIT_LIST_HEAD(&br->age_list);
164 br_stp_timer_init(br);
169 /* find an available port number */
170 static int find_portno(struct net_bridge *br)
173 struct net_bridge_port *p;
174 unsigned long *inuse;
176 inuse = kmalloc(BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long),
181 memset(inuse, 0, BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long));
182 set_bit(0, inuse); /* zero is reserved */
183 list_for_each_entry(p, &br->port_list, list) {
184 set_bit(p->port_no, inuse);
186 index = find_first_zero_bit(inuse, BR_MAX_PORTS);
189 return (index >= BR_MAX_PORTS) ? -EXFULL : index;
192 /* called with RTNL */
193 static struct net_bridge_port *new_nbp(struct net_bridge *br,
194 struct net_device *dev,
198 struct net_bridge_port *p;
200 index = find_portno(br);
202 return ERR_PTR(index);
204 p = kmalloc(sizeof(*p), GFP_KERNEL);
206 return ERR_PTR(-ENOMEM);
208 memset(p, 0, sizeof(*p));
213 p->priority = 0x8000 >> BR_PORT_BITS;
217 p->state = BR_STATE_DISABLED;
218 kobject_init(&p->kobj);
223 int br_add_bridge(const char *name)
225 struct net_device *dev;
228 dev = new_bridge_dev(name);
233 if (strchr(dev->name, '%')) {
234 ret = dev_alloc_name(dev, dev->name);
239 ret = register_netdevice(dev);
243 /* network device kobject is not setup until
244 * after rtnl_unlock does it's hotplug magic.
245 * so hold reference to avoid race.
250 ret = br_sysfs_addbr(dev);
254 unregister_netdev(dev);
265 int br_del_bridge(const char *name)
267 struct net_device *dev;
271 dev = __dev_get_by_name(name);
273 ret = -ENXIO; /* Could not find device */
275 else if (!(dev->priv_flags & IFF_EBRIDGE)) {
276 /* Attempt to delete non bridge device! */
280 else if (dev->flags & IFF_UP) {
281 /* Not shutdown yet. */
286 del_br(netdev_priv(dev));
292 /* called with RTNL */
293 int br_add_if(struct net_bridge *br, struct net_device *dev)
295 struct net_bridge_port *p;
298 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
301 if (dev->hard_start_xmit == br_dev_xmit)
304 if (dev->br_port != NULL)
307 if (IS_ERR(p = new_nbp(br, dev, br_initial_port_cost(dev))))
310 if ((err = br_fdb_insert(br, p, dev->dev_addr, 1)))
313 else if ((err = br_sysfs_addif(p)))
316 dev_set_promiscuity(dev, 1);
318 list_add_rcu(&p->list, &br->port_list);
320 spin_lock_bh(&br->lock);
321 br_stp_recalculate_bridge_id(br);
322 if ((br->dev->flags & IFF_UP) && (dev->flags & IFF_UP))
323 br_stp_enable_port(p);
324 spin_unlock_bh(&br->lock);
330 /* called with RTNL */
331 int br_del_if(struct net_bridge *br, struct net_device *dev)
333 struct net_bridge_port *p = dev->br_port;
335 if (!p || p->br != br)
338 br_sysfs_removeif(p);
341 spin_lock_bh(&br->lock);
342 br_stp_recalculate_bridge_id(br);
343 spin_unlock_bh(&br->lock);
348 void __exit br_cleanup_bridges(void)
350 struct net_device *dev, *nxt;
353 for (dev = dev_base; dev; dev = nxt) {
355 if (dev->priv_flags & IFF_EBRIDGE)