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 port_cost(struct net_device *dev)
37 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
42 strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
43 ifr.ifr_data = (void __user *) &ecmd;
47 err = dev_ethtool(&ifr);
63 /* Old silly heuristics based on name */
64 if (!strncmp(dev->name, "lec", 3))
67 if (!strncmp(dev->name, "plip", 4))
70 return 100; /* assume old 10Mbps */
75 * Check for port carrier transistions.
76 * Called from work queue to allow for calling functions that
77 * might sleep (such as speed check), and to debounce.
79 static void port_carrier_check(void *arg)
81 struct net_bridge_port *p = arg;
84 if (netif_carrier_ok(p->dev)) {
85 u32 cost = port_cost(p->dev);
87 spin_lock_bh(&p->br->lock);
88 if (p->state == BR_STATE_DISABLED) {
90 br_stp_enable_port(p);
92 spin_unlock_bh(&p->br->lock);
94 spin_lock_bh(&p->br->lock);
95 if (p->state != BR_STATE_DISABLED)
96 br_stp_disable_port(p);
97 spin_unlock_bh(&p->br->lock);
102 static void destroy_nbp(struct net_bridge_port *p)
104 struct net_device *dev = p->dev;
113 static void destroy_nbp_rcu(struct rcu_head *head)
115 struct net_bridge_port *p =
116 container_of(head, struct net_bridge_port, rcu);
120 /* called with RTNL */
121 static void del_nbp(struct net_bridge_port *p)
123 struct net_bridge *br = p->br;
124 struct net_device *dev = p->dev;
127 dev_set_promiscuity(dev, -1);
129 cancel_delayed_work(&p->carrier_check);
130 flush_scheduled_work();
132 spin_lock_bh(&br->lock);
133 br_stp_disable_port(p);
134 spin_unlock_bh(&br->lock);
136 br_fdb_delete_by_port(br, p);
138 list_del_rcu(&p->list);
140 del_timer_sync(&p->message_age_timer);
141 del_timer_sync(&p->forward_delay_timer);
142 del_timer_sync(&p->hold_timer);
144 call_rcu(&p->rcu, destroy_nbp_rcu);
147 /* called with RTNL */
148 static void del_br(struct net_bridge *br)
150 struct net_bridge_port *p, *n;
152 list_for_each_entry_safe(p, n, &br->port_list, list) {
153 br_sysfs_removeif(p);
157 del_timer_sync(&br->gc_timer);
159 br_sysfs_delbr(br->dev);
160 unregister_netdevice(br->dev);
163 static struct net_device *new_bridge_dev(const char *name)
165 struct net_bridge *br;
166 struct net_device *dev;
168 dev = alloc_netdev(sizeof(struct net_bridge), name,
174 br = netdev_priv(dev);
177 spin_lock_init(&br->lock);
178 INIT_LIST_HEAD(&br->port_list);
179 spin_lock_init(&br->hash_lock);
181 br->bridge_id.prio[0] = 0x80;
182 br->bridge_id.prio[1] = 0x00;
183 memset(br->bridge_id.addr, 0, ETH_ALEN);
186 br->designated_root = br->bridge_id;
187 br->root_path_cost = 0;
189 br->bridge_max_age = br->max_age = 20 * HZ;
190 br->bridge_hello_time = br->hello_time = 2 * HZ;
191 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
192 br->topology_change = 0;
193 br->topology_change_detected = 0;
194 br->ageing_time = 300 * HZ;
195 INIT_LIST_HEAD(&br->age_list);
197 br_stp_timer_init(br);
202 /* find an available port number */
203 static int find_portno(struct net_bridge *br)
206 struct net_bridge_port *p;
207 unsigned long *inuse;
209 inuse = kmalloc(BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long),
214 memset(inuse, 0, BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long));
215 set_bit(0, inuse); /* zero is reserved */
216 list_for_each_entry(p, &br->port_list, list) {
217 set_bit(p->port_no, inuse);
219 index = find_first_zero_bit(inuse, BR_MAX_PORTS);
222 return (index >= BR_MAX_PORTS) ? -EXFULL : index;
225 /* called with RTNL but without bridge lock */
226 static struct net_bridge_port *new_nbp(struct net_bridge *br,
227 struct net_device *dev)
230 struct net_bridge_port *p;
232 index = find_portno(br);
234 return ERR_PTR(index);
236 p = kmalloc(sizeof(*p), GFP_KERNEL);
238 return ERR_PTR(-ENOMEM);
240 memset(p, 0, sizeof(*p));
244 p->path_cost = port_cost(dev);
245 p->priority = 0x8000 >> BR_PORT_BITS;
249 p->state = BR_STATE_DISABLED;
250 INIT_WORK(&p->carrier_check, port_carrier_check, p);
251 kobject_init(&p->kobj);
256 int br_add_bridge(const char *name)
258 struct net_device *dev;
261 dev = new_bridge_dev(name);
266 if (strchr(dev->name, '%')) {
267 ret = dev_alloc_name(dev, dev->name);
272 ret = register_netdevice(dev);
276 /* network device kobject is not setup until
277 * after rtnl_unlock does it's hotplug magic.
278 * so hold reference to avoid race.
283 ret = br_sysfs_addbr(dev);
287 unregister_netdev(dev);
298 int br_del_bridge(const char *name)
300 struct net_device *dev;
304 dev = __dev_get_by_name(name);
306 ret = -ENXIO; /* Could not find device */
308 else if (!(dev->priv_flags & IFF_EBRIDGE)) {
309 /* Attempt to delete non bridge device! */
313 else if (dev->flags & IFF_UP) {
314 /* Not shutdown yet. */
319 del_br(netdev_priv(dev));
325 /* Mtu of the bridge pseudo-device 1500 or the minimum of the ports */
326 int br_min_mtu(const struct net_bridge *br)
328 const struct net_bridge_port *p;
333 if (list_empty(&br->port_list))
336 list_for_each_entry(p, &br->port_list, list) {
337 if (!mtu || p->dev->mtu < mtu)
345 * Recomputes features using slave's features
347 void br_features_recompute(struct net_bridge *br)
349 struct net_bridge_port *p;
350 unsigned long features, checksum;
352 features = NETIF_F_SG | NETIF_F_FRAGLIST
353 | NETIF_F_HIGHDMA | NETIF_F_TSO;
354 checksum = NETIF_F_IP_CSUM; /* least commmon subset */
356 list_for_each_entry(p, &br->port_list, list) {
357 if (!(p->dev->features
358 & (NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM)))
360 features &= p->dev->features;
363 br->dev->features = features | checksum | NETIF_F_LLTX;
366 /* called with RTNL */
367 int br_add_if(struct net_bridge *br, struct net_device *dev)
369 struct net_bridge_port *p;
372 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
375 if (dev->hard_start_xmit == br_dev_xmit)
378 if (dev->br_port != NULL)
381 if (IS_ERR(p = new_nbp(br, dev)))
384 if ((err = br_fdb_insert(br, p, dev->dev_addr)))
387 else if ((err = br_sysfs_addif(p)))
390 dev_set_promiscuity(dev, 1);
392 list_add_rcu(&p->list, &br->port_list);
394 spin_lock_bh(&br->lock);
395 br_stp_recalculate_bridge_id(br);
396 br_features_recompute(br);
397 if ((br->dev->flags & IFF_UP)
398 && (dev->flags & IFF_UP) && netif_carrier_ok(dev))
399 br_stp_enable_port(p);
400 spin_unlock_bh(&br->lock);
402 dev_set_mtu(br->dev, br_min_mtu(br));
408 /* called with RTNL */
409 int br_del_if(struct net_bridge *br, struct net_device *dev)
411 struct net_bridge_port *p = dev->br_port;
413 if (!p || p->br != br)
416 br_sysfs_removeif(p);
419 spin_lock_bh(&br->lock);
420 br_stp_recalculate_bridge_id(br);
421 br_features_recompute(br);
422 spin_unlock_bh(&br->lock);
427 void __exit br_cleanup_bridges(void)
429 struct net_device *dev, *nxt;
432 for (dev = dev_base; dev; dev = nxt) {
434 if (dev->priv_flags & IFF_EBRIDGE)