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 __user
*) &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(struct net_bridge_port
*p
)
80 struct net_device
*dev
= p
->dev
;
89 static void destroy_nbp_rcu(struct rcu_head
*head
)
91 struct net_bridge_port
*p
=
92 container_of(head
, struct net_bridge_port
, rcu
);
96 /* called with RTNL */
97 static void del_nbp(struct net_bridge_port
*p
)
99 struct net_bridge
*br
= p
->br
;
100 struct net_device
*dev
= p
->dev
;
103 dev_set_promiscuity(dev
, -1);
105 spin_lock_bh(&br
->lock
);
106 br_stp_disable_port(p
);
107 spin_unlock_bh(&br
->lock
);
109 br_fdb_delete_by_port(br
, p
);
111 list_del_rcu(&p
->list
);
113 del_timer_sync(&p
->message_age_timer
);
114 del_timer_sync(&p
->forward_delay_timer
);
115 del_timer_sync(&p
->hold_timer
);
117 call_rcu(&p
->rcu
, destroy_nbp_rcu
);
120 /* called with RTNL */
121 static void del_br(struct net_bridge
*br
)
123 struct net_bridge_port
*p
, *n
;
125 list_for_each_entry_safe(p
, n
, &br
->port_list
, list
) {
126 br_sysfs_removeif(p
);
130 del_timer_sync(&br
->gc_timer
);
132 br_sysfs_delbr(br
->dev
);
133 unregister_netdevice(br
->dev
);
136 static struct net_device
*new_bridge_dev(const char *name
)
138 struct net_bridge
*br
;
139 struct net_device
*dev
;
141 dev
= alloc_netdev(sizeof(struct net_bridge
), name
,
147 br
= netdev_priv(dev
);
150 spin_lock_init(&br
->lock
);
151 INIT_LIST_HEAD(&br
->port_list
);
152 spin_lock_init(&br
->hash_lock
);
154 br
->bridge_id
.prio
[0] = 0x80;
155 br
->bridge_id
.prio
[1] = 0x00;
156 memset(br
->bridge_id
.addr
, 0, ETH_ALEN
);
159 br
->designated_root
= br
->bridge_id
;
160 br
->root_path_cost
= 0;
162 br
->bridge_max_age
= br
->max_age
= 20 * HZ
;
163 br
->bridge_hello_time
= br
->hello_time
= 2 * HZ
;
164 br
->bridge_forward_delay
= br
->forward_delay
= 15 * HZ
;
165 br
->topology_change
= 0;
166 br
->topology_change_detected
= 0;
167 br
->ageing_time
= 300 * HZ
;
168 INIT_LIST_HEAD(&br
->age_list
);
170 br_stp_timer_init(br
);
175 /* find an available port number */
176 static int find_portno(struct net_bridge
*br
)
179 struct net_bridge_port
*p
;
180 unsigned long *inuse
;
182 inuse
= kmalloc(BITS_TO_LONGS(BR_MAX_PORTS
)*sizeof(unsigned long),
187 memset(inuse
, 0, BITS_TO_LONGS(BR_MAX_PORTS
)*sizeof(unsigned long));
188 set_bit(0, inuse
); /* zero is reserved */
189 list_for_each_entry(p
, &br
->port_list
, list
) {
190 set_bit(p
->port_no
, inuse
);
192 index
= find_first_zero_bit(inuse
, BR_MAX_PORTS
);
195 return (index
>= BR_MAX_PORTS
) ? -EXFULL
: index
;
198 /* called with RTNL */
199 static struct net_bridge_port
*new_nbp(struct net_bridge
*br
,
200 struct net_device
*dev
,
204 struct net_bridge_port
*p
;
206 index
= find_portno(br
);
208 return ERR_PTR(index
);
210 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
212 return ERR_PTR(-ENOMEM
);
214 memset(p
, 0, sizeof(*p
));
219 p
->priority
= 0x8000 >> BR_PORT_BITS
;
223 p
->state
= BR_STATE_DISABLED
;
224 kobject_init(&p
->kobj
);
229 int br_add_bridge(const char *name
)
231 struct net_device
*dev
;
234 dev
= new_bridge_dev(name
);
239 if (strchr(dev
->name
, '%')) {
240 ret
= dev_alloc_name(dev
, dev
->name
);
245 ret
= register_netdevice(dev
);
249 /* network device kobject is not setup until
250 * after rtnl_unlock does it's hotplug magic.
251 * so hold reference to avoid race.
256 ret
= br_sysfs_addbr(dev
);
260 unregister_netdev(dev
);
271 int br_del_bridge(const char *name
)
273 struct net_device
*dev
;
277 dev
= __dev_get_by_name(name
);
279 ret
= -ENXIO
; /* Could not find device */
281 else if (!(dev
->priv_flags
& IFF_EBRIDGE
)) {
282 /* Attempt to delete non bridge device! */
286 else if (dev
->flags
& IFF_UP
) {
287 /* Not shutdown yet. */
292 del_br(netdev_priv(dev
));
298 /* Mtu of the bridge pseudo-device 1500 or the minimum of the ports */
299 int br_min_mtu(const struct net_bridge
*br
)
301 const struct net_bridge_port
*p
;
306 if (list_empty(&br
->port_list
))
309 list_for_each_entry(p
, &br
->port_list
, list
) {
310 if (!mtu
|| p
->dev
->mtu
< mtu
)
318 * Recomputes features using slave's features
320 void br_features_recompute(struct net_bridge
*br
)
322 struct net_bridge_port
*p
;
323 unsigned long features
, checksum
;
325 features
= NETIF_F_SG
| NETIF_F_FRAGLIST
326 | NETIF_F_HIGHDMA
| NETIF_F_TSO
;
327 checksum
= NETIF_F_IP_CSUM
; /* least commmon subset */
329 list_for_each_entry(p
, &br
->port_list
, list
) {
330 if (!(p
->dev
->features
331 & (NETIF_F_IP_CSUM
|NETIF_F_NO_CSUM
|NETIF_F_HW_CSUM
)))
333 features
&= p
->dev
->features
;
336 br
->dev
->features
= features
| checksum
| NETIF_F_LLTX
;
339 /* called with RTNL */
340 int br_add_if(struct net_bridge
*br
, struct net_device
*dev
)
342 struct net_bridge_port
*p
;
345 if (dev
->flags
& IFF_LOOPBACK
|| dev
->type
!= ARPHRD_ETHER
)
348 if (dev
->hard_start_xmit
== br_dev_xmit
)
351 if (dev
->br_port
!= NULL
)
354 if (IS_ERR(p
= new_nbp(br
, dev
, br_initial_port_cost(dev
))))
357 if ((err
= br_fdb_insert(br
, p
, dev
->dev_addr
)))
360 else if ((err
= br_sysfs_addif(p
)))
363 dev_set_promiscuity(dev
, 1);
365 list_add_rcu(&p
->list
, &br
->port_list
);
367 spin_lock_bh(&br
->lock
);
368 br_stp_recalculate_bridge_id(br
);
369 if ((br
->dev
->flags
& IFF_UP
)
370 && (dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
))
371 br_stp_enable_port(p
);
372 spin_unlock_bh(&br
->lock
);
374 dev_set_mtu(br
->dev
, br_min_mtu(br
));
380 /* called with RTNL */
381 int br_del_if(struct net_bridge
*br
, struct net_device
*dev
)
383 struct net_bridge_port
*p
= dev
->br_port
;
385 if (!p
|| p
->br
!= br
)
388 br_sysfs_removeif(p
);
391 spin_lock_bh(&br
->lock
);
392 br_stp_recalculate_bridge_id(br
);
393 br_features_recompute(br
);
394 spin_unlock_bh(&br
->lock
);
399 void __exit
br_cleanup_bridges(void)
401 struct net_device
*dev
, *nxt
;
404 for (dev
= dev_base
; dev
; dev
= nxt
) {
406 if (dev
->priv_flags
& IFF_EBRIDGE
)