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>
23 #include <linux/if_ether.h>
26 #include "br_private.h"
29 * Determine initial path cost based on speed.
30 * using recommendations from 802.1d standard
32 * Need to simulate user ioctl because not all device's that support
33 * ethtool, use ethtool_ops. Also, since driver might sleep need to
34 * not be holding any locks.
36 static int 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 __user
*) &ecmd
;
48 err
= dev_ethtool(&ifr
);
64 /* Old silly heuristics based on name */
65 if (!strncmp(dev
->name
, "lec", 3))
68 if (!strncmp(dev
->name
, "plip", 4))
71 return 100; /* assume old 10Mbps */
76 * Check for port carrier transistions.
77 * Called from work queue to allow for calling functions that
78 * might sleep (such as speed check), and to debounce.
80 static void port_carrier_check(void *arg
)
82 struct net_bridge_port
*p
= arg
;
85 if (netif_carrier_ok(p
->dev
)) {
86 u32 cost
= port_cost(p
->dev
);
88 spin_lock_bh(&p
->br
->lock
);
89 if (p
->state
== BR_STATE_DISABLED
) {
91 br_stp_enable_port(p
);
93 spin_unlock_bh(&p
->br
->lock
);
95 spin_lock_bh(&p
->br
->lock
);
96 if (p
->state
!= BR_STATE_DISABLED
)
97 br_stp_disable_port(p
);
98 spin_unlock_bh(&p
->br
->lock
);
103 static void destroy_nbp(struct net_bridge_port
*p
)
105 struct net_device
*dev
= p
->dev
;
114 static void destroy_nbp_rcu(struct rcu_head
*head
)
116 struct net_bridge_port
*p
=
117 container_of(head
, struct net_bridge_port
, rcu
);
121 /* called with RTNL */
122 static void del_nbp(struct net_bridge_port
*p
)
124 struct net_bridge
*br
= p
->br
;
125 struct net_device
*dev
= p
->dev
;
128 dev_set_promiscuity(dev
, -1);
130 cancel_delayed_work(&p
->carrier_check
);
131 flush_scheduled_work();
133 spin_lock_bh(&br
->lock
);
134 br_stp_disable_port(p
);
135 spin_unlock_bh(&br
->lock
);
137 br_fdb_delete_by_port(br
, p
);
139 list_del_rcu(&p
->list
);
141 del_timer_sync(&p
->message_age_timer
);
142 del_timer_sync(&p
->forward_delay_timer
);
143 del_timer_sync(&p
->hold_timer
);
145 call_rcu(&p
->rcu
, destroy_nbp_rcu
);
148 /* called with RTNL */
149 static void del_br(struct net_bridge
*br
)
151 struct net_bridge_port
*p
, *n
;
153 list_for_each_entry_safe(p
, n
, &br
->port_list
, list
) {
154 br_sysfs_removeif(p
);
158 del_timer_sync(&br
->gc_timer
);
160 br_sysfs_delbr(br
->dev
);
161 unregister_netdevice(br
->dev
);
164 static struct net_device
*new_bridge_dev(const char *name
)
166 struct net_bridge
*br
;
167 struct net_device
*dev
;
169 dev
= alloc_netdev(sizeof(struct net_bridge
), name
,
175 br
= netdev_priv(dev
);
178 spin_lock_init(&br
->lock
);
179 INIT_LIST_HEAD(&br
->port_list
);
180 spin_lock_init(&br
->hash_lock
);
182 br
->bridge_id
.prio
[0] = 0x80;
183 br
->bridge_id
.prio
[1] = 0x00;
184 memset(br
->bridge_id
.addr
, 0, ETH_ALEN
);
186 br
->feature_mask
= dev
->features
;
188 br
->designated_root
= br
->bridge_id
;
189 br
->root_path_cost
= 0;
191 br
->bridge_max_age
= br
->max_age
= 20 * HZ
;
192 br
->bridge_hello_time
= br
->hello_time
= 2 * HZ
;
193 br
->bridge_forward_delay
= br
->forward_delay
= 15 * HZ
;
194 br
->topology_change
= 0;
195 br
->topology_change_detected
= 0;
196 br
->ageing_time
= 300 * HZ
;
197 INIT_LIST_HEAD(&br
->age_list
);
199 br_stp_timer_init(br
);
204 /* find an available port number */
205 static int find_portno(struct net_bridge
*br
)
208 struct net_bridge_port
*p
;
209 unsigned long *inuse
;
211 inuse
= kmalloc(BITS_TO_LONGS(BR_MAX_PORTS
)*sizeof(unsigned long),
216 memset(inuse
, 0, BITS_TO_LONGS(BR_MAX_PORTS
)*sizeof(unsigned long));
217 set_bit(0, inuse
); /* zero is reserved */
218 list_for_each_entry(p
, &br
->port_list
, list
) {
219 set_bit(p
->port_no
, inuse
);
221 index
= find_first_zero_bit(inuse
, BR_MAX_PORTS
);
224 return (index
>= BR_MAX_PORTS
) ? -EXFULL
: index
;
227 /* called with RTNL but without bridge lock */
228 static struct net_bridge_port
*new_nbp(struct net_bridge
*br
,
229 struct net_device
*dev
)
232 struct net_bridge_port
*p
;
234 index
= find_portno(br
);
236 return ERR_PTR(index
);
238 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
240 return ERR_PTR(-ENOMEM
);
242 memset(p
, 0, sizeof(*p
));
246 p
->path_cost
= port_cost(dev
);
247 p
->priority
= 0x8000 >> BR_PORT_BITS
;
251 p
->state
= BR_STATE_DISABLED
;
252 INIT_WORK(&p
->carrier_check
, port_carrier_check
, p
);
253 kobject_init(&p
->kobj
);
258 int br_add_bridge(const char *name
)
260 struct net_device
*dev
;
263 dev
= new_bridge_dev(name
);
268 if (strchr(dev
->name
, '%')) {
269 ret
= dev_alloc_name(dev
, dev
->name
);
274 ret
= register_netdevice(dev
);
278 /* network device kobject is not setup until
279 * after rtnl_unlock does it's hotplug magic.
280 * so hold reference to avoid race.
285 ret
= br_sysfs_addbr(dev
);
289 unregister_netdev(dev
);
300 int br_del_bridge(const char *name
)
302 struct net_device
*dev
;
306 dev
= __dev_get_by_name(name
);
308 ret
= -ENXIO
; /* Could not find device */
310 else if (!(dev
->priv_flags
& IFF_EBRIDGE
)) {
311 /* Attempt to delete non bridge device! */
315 else if (dev
->flags
& IFF_UP
) {
316 /* Not shutdown yet. */
321 del_br(netdev_priv(dev
));
327 /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
328 int br_min_mtu(const struct net_bridge
*br
)
330 const struct net_bridge_port
*p
;
335 if (list_empty(&br
->port_list
))
338 list_for_each_entry(p
, &br
->port_list
, list
) {
339 if (!mtu
|| p
->dev
->mtu
< mtu
)
347 * Recomputes features using slave's features
349 void br_features_recompute(struct net_bridge
*br
)
351 struct net_bridge_port
*p
;
352 unsigned long features
, checksum
;
354 features
= br
->feature_mask
&~ NETIF_F_IP_CSUM
;
355 checksum
= br
->feature_mask
& NETIF_F_IP_CSUM
;
357 list_for_each_entry(p
, &br
->port_list
, list
) {
358 if (!(p
->dev
->features
359 & (NETIF_F_IP_CSUM
|NETIF_F_NO_CSUM
|NETIF_F_HW_CSUM
)))
361 features
&= p
->dev
->features
;
364 br
->dev
->features
= features
| checksum
| NETIF_F_LLTX
;
367 /* called with RTNL */
368 int br_add_if(struct net_bridge
*br
, struct net_device
*dev
)
370 struct net_bridge_port
*p
;
373 if (dev
->flags
& IFF_LOOPBACK
|| dev
->type
!= ARPHRD_ETHER
)
376 if (dev
->hard_start_xmit
== br_dev_xmit
)
379 if (dev
->br_port
!= NULL
)
382 if (IS_ERR(p
= new_nbp(br
, dev
)))
385 if ((err
= br_fdb_insert(br
, p
, dev
->dev_addr
)))
388 else if ((err
= br_sysfs_addif(p
)))
391 dev_set_promiscuity(dev
, 1);
393 list_add_rcu(&p
->list
, &br
->port_list
);
395 spin_lock_bh(&br
->lock
);
396 br_stp_recalculate_bridge_id(br
);
397 br_features_recompute(br
);
398 if ((br
->dev
->flags
& IFF_UP
)
399 && (dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
))
400 br_stp_enable_port(p
);
401 spin_unlock_bh(&br
->lock
);
403 dev_set_mtu(br
->dev
, br_min_mtu(br
));
409 /* called with RTNL */
410 int br_del_if(struct net_bridge
*br
, struct net_device
*dev
)
412 struct net_bridge_port
*p
= dev
->br_port
;
414 if (!p
|| p
->br
!= br
)
417 br_sysfs_removeif(p
);
420 spin_lock_bh(&br
->lock
);
421 br_stp_recalculate_bridge_id(br
);
422 br_features_recompute(br
);
423 spin_unlock_bh(&br
->lock
);
428 void __exit
br_cleanup_bridges(void)
430 struct net_device
*dev
, *nxt
;
433 for (dev
= dev_base
; dev
; dev
= nxt
) {
435 if (dev
->priv_flags
& IFF_EBRIDGE
)