blob: 521439333316abf8488560b03c343176cbdb69b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Userspace interface
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>
15#include <linux/netdevice.h>
16#include <linux/ethtool.h>
17#include <linux/if_arp.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/rtnetlink.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080021#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/sock.h>
24
25#include "br_private.h"
26
27/*
28 * Determine initial path cost based on speed.
29 * using recommendations from 802.1d standard
30 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -070031 * Since driver might sleep need to not be holding any locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
Stephen Hemminger4433f422005-12-20 15:19:51 -080033static int port_cost(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Stephen Hemmingerb4a488d2007-08-30 22:16:22 -070035 if (dev->ethtool_ops && dev->ethtool_ops->get_settings) {
36 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET, };
37
38 if (!dev->ethtool_ops->get_settings(dev, &ecmd)) {
Matthew Wilcox61a44b92007-07-31 14:00:02 -070039 switch(ecmd.speed) {
Matthew Wilcox61a44b92007-07-31 14:00:02 -070040 case SPEED_10000:
41 return 2;
Stephen Hemmingerb4a488d2007-08-30 22:16:22 -070042 case SPEED_1000:
43 return 4;
44 case SPEED_100:
45 return 19;
Matthew Wilcox61a44b92007-07-31 14:00:02 -070046 case SPEED_10:
47 return 100;
48 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50 }
51
52 /* Old silly heuristics based on name */
53 if (!strncmp(dev->name, "lec", 3))
54 return 7;
55
56 if (!strncmp(dev->name, "plip", 4))
57 return 2500;
58
59 return 100; /* assume old 10Mbps */
60}
61
Stephen Hemminger4433f422005-12-20 15:19:51 -080062
63/*
64 * Check for port carrier transistions.
65 * Called from work queue to allow for calling functions that
66 * might sleep (such as speed check), and to debounce.
67 */
Stephen Hemminger269def72007-02-22 01:10:18 -080068void br_port_carrier_check(struct net_bridge_port *p)
Stephen Hemminger4433f422005-12-20 15:19:51 -080069{
Stephen Hemminger269def72007-02-22 01:10:18 -080070 struct net_device *dev = p->dev;
71 struct net_bridge *br = p->br;
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -080072
Stephen Hemminger6e86b8902006-03-03 17:14:51 -080073 if (netif_carrier_ok(dev))
74 p->path_cost = port_cost(dev);
Stephen Hemminger4433f422005-12-20 15:19:51 -080075
Stephen Hemminger269def72007-02-22 01:10:18 -080076 if (netif_running(br->dev)) {
Stephen Hemminger6e86b8902006-03-03 17:14:51 -080077 spin_lock_bh(&br->lock);
78 if (netif_carrier_ok(dev)) {
79 if (p->state == BR_STATE_DISABLED)
80 br_stp_enable_port(p);
81 } else {
82 if (p->state != BR_STATE_DISABLED)
83 br_stp_disable_port(p);
Stephen Hemminger4433f422005-12-20 15:19:51 -080084 }
Stephen Hemminger6e86b8902006-03-03 17:14:51 -080085 spin_unlock_bh(&br->lock);
Stephen Hemminger4433f422005-12-20 15:19:51 -080086 }
Stephen Hemminger4433f422005-12-20 15:19:51 -080087}
88
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -080089static void release_nbp(struct kobject *kobj)
90{
91 struct net_bridge_port *p
92 = container_of(kobj, struct net_bridge_port, kobj);
93 kfree(p);
94}
95
96static struct kobj_type brport_ktype = {
97#ifdef CONFIG_SYSFS
98 .sysfs_ops = &brport_sysfs_ops,
99#endif
100 .release = release_nbp,
101};
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static void destroy_nbp(struct net_bridge_port *p)
104{
105 struct net_device *dev = p->dev;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 p->br = NULL;
108 p->dev = NULL;
109 dev_put(dev);
110
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800111 kobject_put(&p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114static void destroy_nbp_rcu(struct rcu_head *head)
115{
116 struct net_bridge_port *p =
117 container_of(head, struct net_bridge_port, rcu);
118 destroy_nbp(p);
119}
120
Stephen Hemminger3f4cfc22006-01-31 17:44:07 -0800121/* Delete port(interface) from bridge is done in two steps.
122 * via RCU. First step, marks device as down. That deletes
123 * all the timers and stops new packets from flowing through.
124 *
125 * Final cleanup doesn't occur until after all CPU's finished
126 * processing packets.
127 *
128 * Protected from multiple admin operations by RTNL mutex
129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void del_nbp(struct net_bridge_port *p)
131{
132 struct net_bridge *br = p->br;
133 struct net_device *dev = p->dev;
134
Greg Kroah-Hartman43b98c42007-12-17 15:54:39 -0400135 sysfs_remove_link(br->ifobj, dev->name);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 dev_set_promiscuity(dev, -1);
138
139 spin_lock_bh(&br->lock);
140 br_stp_disable_port(p);
141 spin_unlock_bh(&br->lock);
142
Stephen Hemmingerb86c4502007-03-22 14:08:46 -0700143 br_ifinfo_notify(RTM_DELLINK, p);
144
Stephen Hemminger1a620692006-10-12 14:45:38 -0700145 br_fdb_delete_by_port(br, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 list_del_rcu(&p->list);
148
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -0800149 rcu_assign_pointer(dev->br_port, NULL);
150
Herbert Xu3fe2d7c2010-02-28 00:49:38 -0800151 br_multicast_del_port(p);
152
Stephen Hemminger125a12c2006-03-03 17:16:15 -0800153 kobject_uevent(&p->kobj, KOBJ_REMOVE);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800154 kobject_del(&p->kobj);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 call_rcu(&p->rcu, destroy_nbp_rcu);
157}
158
159/* called with RTNL */
Eric Dumazet8c56ba02009-10-28 05:35:35 +0000160static void del_br(struct net_bridge *br, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct net_bridge_port *p, *n;
163
164 list_for_each_entry_safe(p, n, &br->port_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 del_nbp(p);
166 }
167
168 del_timer_sync(&br->gc_timer);
169
170 br_sysfs_delbr(br->dev);
Eric Dumazet8c56ba02009-10-28 05:35:35 +0000171 unregister_netdevice_queue(br->dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700174static struct net_device *new_bridge_dev(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct net_bridge *br;
177 struct net_device *dev;
178
179 dev = alloc_netdev(sizeof(struct net_bridge), name,
180 br_dev_setup);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (!dev)
183 return NULL;
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700184 dev_net_set(dev, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 br = netdev_priv(dev);
187 br->dev = dev;
188
stephen hemminger14bb4782010-03-02 13:32:09 +0000189 br->stats = alloc_percpu(struct br_cpu_netstats);
190 if (!br->stats) {
191 free_netdev(dev);
192 return NULL;
193 }
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 spin_lock_init(&br->lock);
196 INIT_LIST_HEAD(&br->port_list);
197 spin_lock_init(&br->hash_lock);
198
199 br->bridge_id.prio[0] = 0x80;
200 br->bridge_id.prio[1] = 0x00;
Stephen Hemmingercee48542006-03-20 22:57:03 -0800201
202 memcpy(br->group_addr, br_group_address, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800204 br->feature_mask = dev->features;
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700205 br->stp_enabled = BR_NO_STP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 br->designated_root = br->bridge_id;
207 br->root_path_cost = 0;
208 br->root_port = 0;
209 br->bridge_max_age = br->max_age = 20 * HZ;
210 br->bridge_hello_time = br->hello_time = 2 * HZ;
211 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
212 br->topology_change = 0;
213 br->topology_change_detected = 0;
214 br->ageing_time = 300 * HZ;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700215
216 br_netfilter_rtable_init(br);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 br_stp_timer_init(br);
Herbert Xu3fe2d7c2010-02-28 00:49:38 -0800219 br_multicast_init(br);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 return dev;
222}
223
224/* find an available port number */
225static int find_portno(struct net_bridge *br)
226{
227 int index;
228 struct net_bridge_port *p;
229 unsigned long *inuse;
230
Stephen Hemminger3b781fa2006-03-20 22:56:50 -0800231 inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 GFP_KERNEL);
233 if (!inuse)
234 return -ENOMEM;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 set_bit(0, inuse); /* zero is reserved */
237 list_for_each_entry(p, &br->port_list, list) {
238 set_bit(p->port_no, inuse);
239 }
240 index = find_first_zero_bit(inuse, BR_MAX_PORTS);
241 kfree(inuse);
242
243 return (index >= BR_MAX_PORTS) ? -EXFULL : index;
244}
245
Stephen Hemminger4433f422005-12-20 15:19:51 -0800246/* called with RTNL but without bridge lock */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900247static struct net_bridge_port *new_nbp(struct net_bridge *br,
Stephen Hemminger4433f422005-12-20 15:19:51 -0800248 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 int index;
251 struct net_bridge_port *p;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 index = find_portno(br);
254 if (index < 0)
255 return ERR_PTR(index);
256
Stephen Hemmingercee48542006-03-20 22:57:03 -0800257 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (p == NULL)
259 return ERR_PTR(-ENOMEM);
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 p->br = br;
262 dev_hold(dev);
263 p->dev = dev;
Stephen Hemminger4433f422005-12-20 15:19:51 -0800264 p->path_cost = port_cost(dev);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900265 p->priority = 0x8000 >> BR_PORT_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 p->port_no = index;
Fischer, Anna3982d3d2009-08-13 06:55:16 +0000267 p->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 br_init_port(p);
269 p->state = BR_STATE_DISABLED;
Stephen Hemmingerd32439c2006-03-03 17:15:34 -0800270 br_stp_port_timer_init(p);
Herbert Xu3fe2d7c2010-02-28 00:49:38 -0800271 br_multicast_add_port(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 return p;
274}
275
Marcel Holtmann384912e2009-08-31 21:08:19 +0000276static struct device_type br_type = {
277 .name = "bridge",
278};
279
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700280int br_add_bridge(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct net_device *dev;
283 int ret;
284
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700285 dev = new_bridge_dev(net, name);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900286 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -ENOMEM;
288
289 rtnl_lock();
290 if (strchr(dev->name, '%')) {
291 ret = dev_alloc_name(dev, dev->name);
Pavel Emelyanove340a902008-05-04 17:58:07 -0700292 if (ret < 0)
293 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Marcel Holtmann384912e2009-08-31 21:08:19 +0000296 SET_NETDEV_DEVTYPE(dev, &br_type);
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ret = register_netdevice(dev);
Pavel Emelyanove340a902008-05-04 17:58:07 -0700299 if (ret)
300 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 ret = br_sysfs_addbr(dev);
Stephen Hemmingerac052022006-05-10 13:21:53 -0700303 if (ret)
Jiri Benc36485702006-06-05 16:39:34 -0700304 unregister_netdevice(dev);
305 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 rtnl_unlock();
Stephen Hemmingerac052022006-05-10 13:21:53 -0700307 return ret;
Pavel Emelyanove340a902008-05-04 17:58:07 -0700308
309out_free:
310 free_netdev(dev);
311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700314int br_del_bridge(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct net_device *dev;
317 int ret = 0;
318
319 rtnl_lock();
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700320 dev = __dev_get_by_name(net, name);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900321 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 ret = -ENXIO; /* Could not find device */
323
324 else if (!(dev->priv_flags & IFF_EBRIDGE)) {
325 /* Attempt to delete non bridge device! */
326 ret = -EPERM;
327 }
328
329 else if (dev->flags & IFF_UP) {
330 /* Not shutdown yet. */
331 ret = -EBUSY;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900334 else
Eric Dumazet8c56ba02009-10-28 05:35:35 +0000335 del_br(netdev_priv(dev), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 rtnl_unlock();
338 return ret;
339}
340
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800341/* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342int br_min_mtu(const struct net_bridge *br)
343{
344 const struct net_bridge_port *p;
345 int mtu = 0;
346
347 ASSERT_RTNL();
348
349 if (list_empty(&br->port_list))
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800350 mtu = ETH_DATA_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 else {
352 list_for_each_entry(p, &br->port_list, list) {
353 if (!mtu || p->dev->mtu < mtu)
354 mtu = p->dev->mtu;
355 }
356 }
357 return mtu;
358}
359
Stephen Hemminger81d35302005-05-29 14:15:17 -0700360/*
361 * Recomputes features using slave's features
362 */
363void br_features_recompute(struct net_bridge *br)
364{
365 struct net_bridge_port *p;
Herbert Xub63365a2008-10-23 01:11:29 -0700366 unsigned long features, mask;
Stephen Hemminger81d35302005-05-29 14:15:17 -0700367
Herbert Xub63365a2008-10-23 01:11:29 -0700368 features = mask = br->feature_mask;
369 if (list_empty(&br->port_list))
370 goto done;
371
372 features &= ~NETIF_F_ONE_FOR_ALL;
Stephen Hemminger81d35302005-05-29 14:15:17 -0700373
374 list_for_each_entry(p, &br->port_list, list) {
Herbert Xub63365a2008-10-23 01:11:29 -0700375 features = netdev_increment_features(features,
376 p->dev->features, mask);
Stephen Hemminger81d35302005-05-29 14:15:17 -0700377 }
378
Herbert Xub63365a2008-10-23 01:11:29 -0700379done:
380 br->dev->features = netdev_fix_features(features, NULL);
Stephen Hemminger81d35302005-05-29 14:15:17 -0700381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/* called with RTNL */
384int br_add_if(struct net_bridge *br, struct net_device *dev)
385{
386 struct net_bridge_port *p;
387 int err = 0;
388
Stephen Hemminger1056bd52009-11-05 20:46:52 -0800389 /* Don't allow bridging non-ethernet like devices */
390 if ((dev->flags & IFF_LOOPBACK) ||
391 dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EINVAL;
393
Stephen Hemminger1056bd52009-11-05 20:46:52 -0800394 /* No bridging of bridges */
Stephen Hemminger00829822008-11-20 20:14:53 -0800395 if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -ELOOP;
397
Stephen Hemminger1056bd52009-11-05 20:46:52 -0800398 /* Device is already being bridged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (dev->br_port != NULL)
400 return -EBUSY;
401
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100402 /* No bridging devices that dislike that (e.g. wireless) */
403 if (dev->priv_flags & IFF_DONT_BRIDGE)
404 return -EOPNOTSUPP;
405
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800406 p = new_nbp(br, dev);
407 if (IS_ERR(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return PTR_ERR(p);
409
Wang Chenbc3f9072008-07-14 20:53:13 -0700410 err = dev_set_promiscuity(dev, 1);
411 if (err)
412 goto put_back;
413
Greg Kroah-Hartmane32cc732007-12-17 23:05:35 -0700414 err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
415 SYSFS_BRIDGE_PORT_ATTR);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800416 if (err)
417 goto err0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900419 err = br_fdb_insert(br, p, dev->dev_addr);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800420 if (err)
421 goto err1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800423 err = br_sysfs_addif(p);
424 if (err)
425 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800427 rcu_assign_pointer(dev->br_port, p);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700428 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800430 list_add_rcu(&p->list, &br->port_list);
431
432 spin_lock_bh(&br->lock);
433 br_stp_recalculate_bridge_id(br);
434 br_features_recompute(br);
Aji Srinivasde790592007-03-07 16:10:53 -0800435
436 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
437 (br->dev->flags & IFF_UP))
438 br_stp_enable_port(p);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800439 spin_unlock_bh(&br->lock);
440
Stephen Hemmingerb86c4502007-03-22 14:08:46 -0700441 br_ifinfo_notify(RTM_NEWLINK, p);
442
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800443 dev_set_mtu(br->dev, br_min_mtu(br));
Stephen Hemminger269def72007-02-22 01:10:18 -0800444
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800445 kobject_uevent(&p->kobj, KOBJ_ADD);
446
447 return 0;
448err2:
Stephen Hemminger1a620692006-10-12 14:45:38 -0700449 br_fdb_delete_by_port(br, p, 1);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800450err1:
Xiaotian Fengc587aea2009-07-23 23:06:32 +0000451 kobject_put(&p->kobj);
Jeff Hansen30df94f2009-09-28 12:54:25 -0700452 p = NULL; /* kobject_put frees */
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800453err0:
Wang Chenbc3f9072008-07-14 20:53:13 -0700454 dev_set_promiscuity(dev, -1);
Volodymyr G Lukiianyk43af8532008-04-29 03:17:42 -0700455put_back:
456 dev_put(dev);
Wang Chenbc3f9072008-07-14 20:53:13 -0700457 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return err;
459}
460
461/* called with RTNL */
462int br_del_if(struct net_bridge *br, struct net_device *dev)
463{
464 struct net_bridge_port *p = dev->br_port;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900465
466 if (!p || p->br != br)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -EINVAL;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 del_nbp(p);
470
471 spin_lock_bh(&br->lock);
472 br_stp_recalculate_bridge_id(br);
Stephen Hemminger81d35302005-05-29 14:15:17 -0700473 br_features_recompute(br);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 spin_unlock_bh(&br->lock);
475
476 return 0;
477}
478
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000479void __net_exit br_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Patrick McHardyab1b2042008-07-03 03:53:42 -0700481 struct net_device *dev;
Eric Dumazet8c56ba02009-10-28 05:35:35 +0000482 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 rtnl_lock();
Eric Dumazet8c56ba02009-10-28 05:35:35 +0000485 for_each_netdev(net, dev)
486 if (dev->priv_flags & IFF_EBRIDGE)
487 del_br(netdev_priv(dev), &list);
488
489 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 rtnl_unlock();
491
492}