1 /*
2  *	Userspace interface
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Lennert Buytenhek		<buytenh@gnu.org>
7  *
8  *	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/etherdevice.h>
17 #include <linux/netpoll.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>
24 #include <linux/slab.h>
25 #include <net/sock.h>
26 
27 #include "br_private.h"
28 
29 /*
30  * Determine initial path cost based on speed.
31  * using recommendations from 802.1d standard
32  *
33  * Since driver might sleep need to not be holding any locks.
34  */
port_cost(struct net_device * dev)35 static int port_cost(struct net_device *dev)
36 {
37 	struct ethtool_cmd ecmd;
38 
39 	if (!__ethtool_get_settings(dev, &ecmd)) {
40 		switch (ethtool_cmd_speed(&ecmd)) {
41 		case SPEED_10000:
42 			return 2;
43 		case SPEED_1000:
44 			return 4;
45 		case SPEED_100:
46 			return 19;
47 		case SPEED_10:
48 			return 100;
49 		}
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 
62 
63 /* Check for port carrier transistions. */
br_port_carrier_check(struct net_bridge_port * p)64 void br_port_carrier_check(struct net_bridge_port *p)
65 {
66 	struct net_device *dev = p->dev;
67 	struct net_bridge *br = p->br;
68 
69 	if (netif_running(dev) && netif_carrier_ok(dev))
70 		p->path_cost = port_cost(dev);
71 
72 	if (!netif_running(br->dev))
73 		return;
74 
75 	spin_lock_bh(&br->lock);
76 	if (netif_running(dev) && netif_carrier_ok(dev)) {
77 		if (p->state == BR_STATE_DISABLED)
78 			br_stp_enable_port(p);
79 	} else {
80 		if (p->state != BR_STATE_DISABLED)
81 			br_stp_disable_port(p);
82 	}
83 	spin_unlock_bh(&br->lock);
84 }
85 
release_nbp(struct kobject * kobj)86 static void release_nbp(struct kobject *kobj)
87 {
88 	struct net_bridge_port *p
89 		= container_of(kobj, struct net_bridge_port, kobj);
90 	kfree(p);
91 }
92 
93 static struct kobj_type brport_ktype = {
94 #ifdef CONFIG_SYSFS
95 	.sysfs_ops = &brport_sysfs_ops,
96 #endif
97 	.release = release_nbp,
98 };
99 
destroy_nbp(struct net_bridge_port * p)100 static void destroy_nbp(struct net_bridge_port *p)
101 {
102 	struct net_device *dev = p->dev;
103 
104 	p->br = NULL;
105 	p->dev = NULL;
106 	dev_put(dev);
107 
108 	kobject_put(&p->kobj);
109 }
110 
destroy_nbp_rcu(struct rcu_head * head)111 static void destroy_nbp_rcu(struct rcu_head *head)
112 {
113 	struct net_bridge_port *p =
114 			container_of(head, struct net_bridge_port, rcu);
115 	destroy_nbp(p);
116 }
117 
118 /* Delete port(interface) from bridge is done in two steps.
119  * via RCU. First step, marks device as down. That deletes
120  * all the timers and stops new packets from flowing through.
121  *
122  * Final cleanup doesn't occur until after all CPU's finished
123  * processing packets.
124  *
125  * Protected from multiple admin operations by RTNL mutex
126  */
del_nbp(struct net_bridge_port * p)127 static void del_nbp(struct net_bridge_port *p)
128 {
129 	struct net_bridge *br = p->br;
130 	struct net_device *dev = p->dev;
131 
132 	sysfs_remove_link(br->ifobj, p->dev->name);
133 
134 	dev_set_promiscuity(dev, -1);
135 
136 	spin_lock_bh(&br->lock);
137 	br_stp_disable_port(p);
138 	spin_unlock_bh(&br->lock);
139 
140 	br_ifinfo_notify(RTM_DELLINK, p);
141 
142 	br_fdb_delete_by_port(br, p, 1);
143 
144 	list_del_rcu(&p->list);
145 
146 	dev->priv_flags &= ~IFF_BRIDGE_PORT;
147 
148 	netdev_rx_handler_unregister(dev);
149 	synchronize_net();
150 
151 	netdev_set_master(dev, NULL);
152 
153 	br_multicast_del_port(p);
154 
155 	kobject_uevent(&p->kobj, KOBJ_REMOVE);
156 	kobject_del(&p->kobj);
157 
158 	br_netpoll_disable(p);
159 
160 	call_rcu(&p->rcu, destroy_nbp_rcu);
161 }
162 
163 /* Delete bridge device */
br_dev_delete(struct net_device * dev,struct list_head * head)164 void br_dev_delete(struct net_device *dev, struct list_head *head)
165 {
166 	struct net_bridge *br = netdev_priv(dev);
167 	struct net_bridge_port *p, *n;
168 
169 	list_for_each_entry_safe(p, n, &br->port_list, list) {
170 		del_nbp(p);
171 	}
172 
173 	br_fdb_delete_by_port(br, NULL, 1);
174 
175 	del_timer_sync(&br->gc_timer);
176 
177 	br_sysfs_delbr(br->dev);
178 	unregister_netdevice_queue(br->dev, head);
179 }
180 
181 /* find an available port number */
find_portno(struct net_bridge * br)182 static int find_portno(struct net_bridge *br)
183 {
184 	int index;
185 	struct net_bridge_port *p;
186 	unsigned long *inuse;
187 
188 	inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
189 			GFP_KERNEL);
190 	if (!inuse)
191 		return -ENOMEM;
192 
193 	set_bit(0, inuse);	/* zero is reserved */
194 	list_for_each_entry(p, &br->port_list, list) {
195 		set_bit(p->port_no, inuse);
196 	}
197 	index = find_first_zero_bit(inuse, BR_MAX_PORTS);
198 	kfree(inuse);
199 
200 	return (index >= BR_MAX_PORTS) ? -EXFULL : index;
201 }
202 
203 /* called with RTNL but without bridge lock */
new_nbp(struct net_bridge * br,struct net_device * dev)204 static struct net_bridge_port *new_nbp(struct net_bridge *br,
205 				       struct net_device *dev)
206 {
207 	int index;
208 	struct net_bridge_port *p;
209 
210 	index = find_portno(br);
211 	if (index < 0)
212 		return ERR_PTR(index);
213 
214 	p = kzalloc(sizeof(*p), GFP_KERNEL);
215 	if (p == NULL)
216 		return ERR_PTR(-ENOMEM);
217 
218 	p->br = br;
219 	dev_hold(dev);
220 	p->dev = dev;
221 	p->path_cost = port_cost(dev);
222 	p->priority = 0x8000 >> BR_PORT_BITS;
223 	p->port_no = index;
224 	p->flags = 0;
225 	br_init_port(p);
226 	p->state = BR_STATE_DISABLED;
227 	br_stp_port_timer_init(p);
228 	br_multicast_add_port(p);
229 
230 	return p;
231 }
232 
br_add_bridge(struct net * net,const char * name)233 int br_add_bridge(struct net *net, const char *name)
234 {
235 	struct net_device *dev;
236 	int res;
237 
238 	dev = alloc_netdev(sizeof(struct net_bridge), name,
239 			   br_dev_setup);
240 
241 	if (!dev)
242 		return -ENOMEM;
243 
244 	dev_net_set(dev, net);
245 	dev->rtnl_link_ops = &br_link_ops;
246 
247 	res = register_netdev(dev);
248 	if (res)
249 		free_netdev(dev);
250 	return res;
251 }
252 
br_del_bridge(struct net * net,const char * name)253 int br_del_bridge(struct net *net, const char *name)
254 {
255 	struct net_device *dev;
256 	int ret = 0;
257 
258 	rtnl_lock();
259 	dev = __dev_get_by_name(net, name);
260 	if (dev == NULL)
261 		ret =  -ENXIO; 	/* Could not find device */
262 
263 	else if (!(dev->priv_flags & IFF_EBRIDGE)) {
264 		/* Attempt to delete non bridge device! */
265 		ret = -EPERM;
266 	}
267 
268 	else if (dev->flags & IFF_UP) {
269 		/* Not shutdown yet. */
270 		ret = -EBUSY;
271 	}
272 
273 	else
274 		br_dev_delete(dev, NULL);
275 
276 	rtnl_unlock();
277 	return ret;
278 }
279 
280 /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
br_min_mtu(const struct net_bridge * br)281 int br_min_mtu(const struct net_bridge *br)
282 {
283 	const struct net_bridge_port *p;
284 	int mtu = 0;
285 
286 	ASSERT_RTNL();
287 
288 	if (list_empty(&br->port_list))
289 		mtu = ETH_DATA_LEN;
290 	else {
291 		list_for_each_entry(p, &br->port_list, list) {
292 			if (!mtu  || p->dev->mtu < mtu)
293 				mtu = p->dev->mtu;
294 		}
295 	}
296 	return mtu;
297 }
298 
299 /*
300  * Recomputes features using slave's features
301  */
br_features_recompute(struct net_bridge * br,netdev_features_t features)302 netdev_features_t br_features_recompute(struct net_bridge *br,
303 	netdev_features_t features)
304 {
305 	struct net_bridge_port *p;
306 	netdev_features_t mask;
307 
308 	if (list_empty(&br->port_list))
309 		return features;
310 
311 	mask = features;
312 	features &= ~NETIF_F_ONE_FOR_ALL;
313 
314 	list_for_each_entry(p, &br->port_list, list) {
315 		features = netdev_increment_features(features,
316 						     p->dev->features, mask);
317 	}
318 
319 	return features;
320 }
321 
322 /* called with RTNL */
br_add_if(struct net_bridge * br,struct net_device * dev)323 int br_add_if(struct net_bridge *br, struct net_device *dev)
324 {
325 	struct net_bridge_port *p;
326 	int err = 0;
327 	bool changed_addr;
328 
329 	/* Don't allow bridging non-ethernet like devices */
330 	if ((dev->flags & IFF_LOOPBACK) ||
331 	    dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
332 	    !is_valid_ether_addr(dev->dev_addr))
333 		return -EINVAL;
334 
335 	/* No bridging of bridges */
336 	if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit)
337 		return -ELOOP;
338 
339 	/* Device is already being bridged */
340 	if (br_port_exists(dev))
341 		return -EBUSY;
342 
343 	/* No bridging devices that dislike that (e.g. wireless) */
344 	if (dev->priv_flags & IFF_DONT_BRIDGE)
345 		return -EOPNOTSUPP;
346 
347 	p = new_nbp(br, dev);
348 	if (IS_ERR(p))
349 		return PTR_ERR(p);
350 
351 	call_netdevice_notifiers(NETDEV_JOIN, dev);
352 
353 	err = dev_set_promiscuity(dev, 1);
354 	if (err)
355 		goto put_back;
356 
357 	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
358 				   SYSFS_BRIDGE_PORT_ATTR);
359 	if (err)
360 		goto err1;
361 
362 	err = br_sysfs_addif(p);
363 	if (err)
364 		goto err2;
365 
366 	if (br_netpoll_info(br) && ((err = br_netpoll_enable(p))))
367 		goto err3;
368 
369 	err = netdev_set_master(dev, br->dev);
370 	if (err)
371 		goto err3;
372 
373 	err = netdev_rx_handler_register(dev, br_handle_frame, p);
374 	if (err)
375 		goto err4;
376 
377 	dev->priv_flags |= IFF_BRIDGE_PORT;
378 
379 	dev_disable_lro(dev);
380 
381 	list_add_rcu(&p->list, &br->port_list);
382 
383 	netdev_update_features(br->dev);
384 
385 	spin_lock_bh(&br->lock);
386 	changed_addr = br_stp_recalculate_bridge_id(br);
387 
388 	if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
389 	    (br->dev->flags & IFF_UP))
390 		br_stp_enable_port(p);
391 	spin_unlock_bh(&br->lock);
392 
393 	br_ifinfo_notify(RTM_NEWLINK, p);
394 
395 	if (changed_addr)
396 		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
397 
398 	dev_set_mtu(br->dev, br_min_mtu(br));
399 
400 	if (br_fdb_insert(br, p, dev->dev_addr))
401 		netdev_err(dev, "failed insert local address bridge forwarding table\n");
402 
403 	kobject_uevent(&p->kobj, KOBJ_ADD);
404 
405 	return 0;
406 
407 err4:
408 	netdev_set_master(dev, NULL);
409 err3:
410 	sysfs_remove_link(br->ifobj, p->dev->name);
411 err2:
412 	kobject_put(&p->kobj);
413 	p = NULL; /* kobject_put frees */
414 err1:
415 	dev_set_promiscuity(dev, -1);
416 put_back:
417 	dev_put(dev);
418 	kfree(p);
419 	return err;
420 }
421 
422 /* called with RTNL */
br_del_if(struct net_bridge * br,struct net_device * dev)423 int br_del_if(struct net_bridge *br, struct net_device *dev)
424 {
425 	struct net_bridge_port *p;
426 	bool changed_addr;
427 
428 	p = br_port_get_rtnl(dev);
429 	if (!p || p->br != br)
430 		return -EINVAL;
431 
432 	del_nbp(p);
433 
434 	spin_lock_bh(&br->lock);
435 	changed_addr = br_stp_recalculate_bridge_id(br);
436 	spin_unlock_bh(&br->lock);
437 
438 	if (changed_addr)
439 		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
440 
441 	netdev_update_features(br->dev);
442 
443 	return 0;
444 }
445 
br_net_exit(struct net * net)446 void __net_exit br_net_exit(struct net *net)
447 {
448 	struct net_device *dev;
449 	LIST_HEAD(list);
450 
451 	rtnl_lock();
452 	for_each_netdev(net, dev)
453 		if (dev->priv_flags & IFF_EBRIDGE)
454 			br_dev_delete(dev, &list);
455 
456 	unregister_netdevice_many(&list);
457 	rtnl_unlock();
458 
459 }
460