Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Userspace interface
4 * Linux ethernet bridge
5 *
6 * Authors:
7 * Lennert Buytenhek <buytenh@gnu.org>
8 */
9
10#include <linux/kernel.h>
11#include <linux/netdevice.h>
12#include <linux/etherdevice.h>
13#include <linux/netpoll.h>
14#include <linux/ethtool.h>
15#include <linux/if_arp.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/rtnetlink.h>
19#include <linux/if_ether.h>
20#include <linux/slab.h>
21#include <net/dsa.h>
22#include <net/sock.h>
23#include <linux/if_vlan.h>
24#include <net/switchdev.h>
25#include <net/net_namespace.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 */
35static int port_cost(struct net_device *dev)
36{
37 struct ethtool_link_ksettings ecmd;
38
39 if (!__ethtool_get_link_ksettings(dev, &ecmd)) {
40 switch (ecmd.base.speed) {
41 case SPEED_10000:
42 return 2;
43 case SPEED_5000:
44 return 3;
45 case SPEED_2500:
46 return 4;
47 case SPEED_1000:
48 return 5;
49 case SPEED_100:
50 return 19;
51 case SPEED_10:
52 return 100;
53 case SPEED_UNKNOWN:
54 return 100;
55 default:
56 if (ecmd.base.speed > SPEED_10000)
57 return 1;
58 }
59 }
60
61 /* Old silly heuristics based on name */
62 if (!strncmp(dev->name, "lec", 3))
63 return 7;
64
65 if (!strncmp(dev->name, "plip", 4))
66 return 2500;
67
68 return 100; /* assume old 10Mbps */
69}
70
71
72/* Check for port carrier transitions. */
73void br_port_carrier_check(struct net_bridge_port *p, bool *notified)
74{
75 struct net_device *dev = p->dev;
76 struct net_bridge *br = p->br;
77
78 if (!(p->flags & BR_ADMIN_COST) &&
79 netif_running(dev) && netif_oper_up(dev))
80 p->path_cost = port_cost(dev);
81
82 *notified = false;
83 if (!netif_running(br->dev))
84 return;
85
86 spin_lock_bh(&br->lock);
87 if (netif_running(dev) && netif_oper_up(dev)) {
88 if (p->state == BR_STATE_DISABLED) {
89 br_stp_enable_port(p);
90 *notified = true;
91 }
92 } else {
93 if (p->state != BR_STATE_DISABLED) {
94 br_stp_disable_port(p);
95 *notified = true;
96 }
97 }
98 spin_unlock_bh(&br->lock);
99}
100
101static void br_port_set_promisc(struct net_bridge_port *p)
102{
103 int err = 0;
104
105 if (br_promisc_port(p))
106 return;
107
108 err = dev_set_promiscuity(p->dev, 1);
109 if (err)
110 return;
111
112 br_fdb_unsync_static(p->br, p);
113 p->flags |= BR_PROMISC;
114}
115
116static void br_port_clear_promisc(struct net_bridge_port *p)
117{
118 int err;
119
120 /* Check if the port is already non-promisc or if it doesn't
121 * support UNICAST filtering. Without unicast filtering support
122 * we'll end up re-enabling promisc mode anyway, so just check for
123 * it here.
124 */
125 if (!br_promisc_port(p) || !(p->dev->priv_flags & IFF_UNICAST_FLT))
126 return;
127
128 /* Since we'll be clearing the promisc mode, program the port
129 * first so that we don't have interruption in traffic.
130 */
131 err = br_fdb_sync_static(p->br, p);
132 if (err)
133 return;
134
135 dev_set_promiscuity(p->dev, -1);
136 p->flags &= ~BR_PROMISC;
137}
138
139/* When a port is added or removed or when certain port flags
140 * change, this function is called to automatically manage
141 * promiscuity setting of all the bridge ports. We are always called
142 * under RTNL so can skip using rcu primitives.
143 */
144void br_manage_promisc(struct net_bridge *br)
145{
146 struct net_bridge_port *p;
147 bool set_all = false;
148
149 /* If vlan filtering is disabled or bridge interface is placed
150 * into promiscuous mode, place all ports in promiscuous mode.
151 */
152 if ((br->dev->flags & IFF_PROMISC) || !br_vlan_enabled(br->dev))
153 set_all = true;
154
155 list_for_each_entry(p, &br->port_list, list) {
156 if (set_all) {
157 br_port_set_promisc(p);
158 } else {
159 /* If the number of auto-ports is <= 1, then all other
160 * ports will have their output configuration
161 * statically specified through fdbs. Since ingress
162 * on the auto-port becomes forwarding/egress to other
163 * ports and egress configuration is statically known,
164 * we can say that ingress configuration of the
165 * auto-port is also statically known.
166 * This lets us disable promiscuous mode and write
167 * this config to hw.
168 */
169 if ((p->dev->priv_flags & IFF_UNICAST_FLT) &&
170 (br->auto_cnt == 0 ||
171 (br->auto_cnt == 1 && br_auto_port(p))))
172 br_port_clear_promisc(p);
173 else
174 br_port_set_promisc(p);
175 }
176 }
177}
178
179int nbp_backup_change(struct net_bridge_port *p,
180 struct net_device *backup_dev)
181{
182 struct net_bridge_port *old_backup = rtnl_dereference(p->backup_port);
183 struct net_bridge_port *backup_p = NULL;
184
185 ASSERT_RTNL();
186
187 if (backup_dev) {
188 if (!netif_is_bridge_port(backup_dev))
189 return -ENOENT;
190
191 backup_p = br_port_get_rtnl(backup_dev);
192 if (backup_p->br != p->br)
193 return -EINVAL;
194 }
195
196 if (p == backup_p)
197 return -EINVAL;
198
199 if (old_backup == backup_p)
200 return 0;
201
202 /* if the backup link is already set, clear it */
203 if (old_backup)
204 old_backup->backup_redirected_cnt--;
205
206 if (backup_p)
207 backup_p->backup_redirected_cnt++;
208 rcu_assign_pointer(p->backup_port, backup_p);
209
210 return 0;
211}
212
213static void nbp_backup_clear(struct net_bridge_port *p)
214{
215 nbp_backup_change(p, NULL);
216 if (p->backup_redirected_cnt) {
217 struct net_bridge_port *cur_p;
218
219 list_for_each_entry(cur_p, &p->br->port_list, list) {
220 struct net_bridge_port *backup_p;
221
222 backup_p = rtnl_dereference(cur_p->backup_port);
223 if (backup_p == p)
224 nbp_backup_change(cur_p, NULL);
225 }
226 }
227
228 WARN_ON(rcu_access_pointer(p->backup_port) || p->backup_redirected_cnt);
229}
230
231static void nbp_update_port_count(struct net_bridge *br)
232{
233 struct net_bridge_port *p;
234 u32 cnt = 0;
235
236 list_for_each_entry(p, &br->port_list, list) {
237 if (br_auto_port(p))
238 cnt++;
239 }
240 if (br->auto_cnt != cnt) {
241 br->auto_cnt = cnt;
242 br_manage_promisc(br);
243 }
244}
245
246static void nbp_delete_promisc(struct net_bridge_port *p)
247{
248 /* If port is currently promiscuous, unset promiscuity.
249 * Otherwise, it is a static port so remove all addresses
250 * from it.
251 */
252 dev_set_allmulti(p->dev, -1);
253 if (br_promisc_port(p))
254 dev_set_promiscuity(p->dev, -1);
255 else
256 br_fdb_unsync_static(p->br, p);
257}
258
259static void release_nbp(struct kobject *kobj)
260{
261 struct net_bridge_port *p
262 = container_of(kobj, struct net_bridge_port, kobj);
263 kfree(p);
264}
265
266static void brport_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
267{
268 struct net_bridge_port *p = kobj_to_brport(kobj);
269
270 net_ns_get_ownership(dev_net(p->dev), uid, gid);
271}
272
273static const struct kobj_type brport_ktype = {
274#ifdef CONFIG_SYSFS
275 .sysfs_ops = &brport_sysfs_ops,
276#endif
277 .release = release_nbp,
278 .get_ownership = brport_get_ownership,
279};
280
281static void destroy_nbp(struct net_bridge_port *p)
282{
283 struct net_device *dev = p->dev;
284
285 p->br = NULL;
286 p->dev = NULL;
287 netdev_put(dev, &p->dev_tracker);
288
289 kobject_put(&p->kobj);
290}
291
292static void destroy_nbp_rcu(struct rcu_head *head)
293{
294 struct net_bridge_port *p =
295 container_of(head, struct net_bridge_port, rcu);
296 destroy_nbp(p);
297}
298
299static unsigned get_max_headroom(struct net_bridge *br)
300{
301 unsigned max_headroom = 0;
302 struct net_bridge_port *p;
303
304 list_for_each_entry(p, &br->port_list, list) {
305 unsigned dev_headroom = netdev_get_fwd_headroom(p->dev);
306
307 if (dev_headroom > max_headroom)
308 max_headroom = dev_headroom;
309 }
310
311 return max_headroom;
312}
313
314static void update_headroom(struct net_bridge *br, int new_hr)
315{
316 struct net_bridge_port *p;
317
318 list_for_each_entry(p, &br->port_list, list)
319 netdev_set_rx_headroom(p->dev, new_hr);
320
321 br->dev->needed_headroom = new_hr;
322}
323
324/* Delete port(interface) from bridge is done in two steps.
325 * via RCU. First step, marks device as down. That deletes
326 * all the timers and stops new packets from flowing through.
327 *
328 * Final cleanup doesn't occur until after all CPU's finished
329 * processing packets.
330 *
331 * Protected from multiple admin operations by RTNL mutex
332 */
333static void del_nbp(struct net_bridge_port *p)
334{
335 struct net_bridge *br = p->br;
336 struct net_device *dev = p->dev;
337
338 sysfs_remove_link(br->ifobj, p->dev->name);
339
340 nbp_delete_promisc(p);
341
342 spin_lock_bh(&br->lock);
343 br_stp_disable_port(p);
344 spin_unlock_bh(&br->lock);
345
346 br_mrp_port_del(br, p);
347 br_cfm_port_del(br, p);
348
349 br_ifinfo_notify(RTM_DELLINK, NULL, p);
350
351 list_del_rcu(&p->list);
352 if (netdev_get_fwd_headroom(dev) == br->dev->needed_headroom)
353 update_headroom(br, get_max_headroom(br));
354 netdev_reset_rx_headroom(dev);
355
356 nbp_vlan_flush(p);
357 br_fdb_delete_by_port(br, p, 0, 1);
358 switchdev_deferred_process();
359 nbp_backup_clear(p);
360
361 nbp_update_port_count(br);
362
363 netdev_upper_dev_unlink(dev, br->dev);
364
365 dev->priv_flags &= ~IFF_BRIDGE_PORT;
366
367 netdev_rx_handler_unregister(dev);
368
369 br_multicast_del_port(p);
370
371 kobject_uevent(&p->kobj, KOBJ_REMOVE);
372 kobject_del(&p->kobj);
373
374 br_netpoll_disable(p);
375
376 call_rcu(&p->rcu, destroy_nbp_rcu);
377}
378
379/* Delete bridge device */
380void br_dev_delete(struct net_device *dev, struct list_head *head)
381{
382 struct net_bridge *br = netdev_priv(dev);
383 struct net_bridge_port *p, *n;
384
385 list_for_each_entry_safe(p, n, &br->port_list, list) {
386 del_nbp(p);
387 }
388
389 br_recalculate_neigh_suppress_enabled(br);
390
391 br_fdb_delete_by_port(br, NULL, 0, 1);
392
393 cancel_delayed_work_sync(&br->gc_work);
394
395 br_sysfs_delbr(br->dev);
396 unregister_netdevice_queue(br->dev, head);
397}
398
399/* find an available port number */
400static int find_portno(struct net_bridge *br)
401{
402 int index;
403 struct net_bridge_port *p;
404 unsigned long *inuse;
405
406 inuse = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
407 if (!inuse)
408 return -ENOMEM;
409
410 __set_bit(0, inuse); /* zero is reserved */
411 list_for_each_entry(p, &br->port_list, list)
412 __set_bit(p->port_no, inuse);
413
414 index = find_first_zero_bit(inuse, BR_MAX_PORTS);
415 bitmap_free(inuse);
416
417 return (index >= BR_MAX_PORTS) ? -EXFULL : index;
418}
419
420/* called with RTNL but without bridge lock */
421static struct net_bridge_port *new_nbp(struct net_bridge *br,
422 struct net_device *dev)
423{
424 struct net_bridge_port *p;
425 int index, err;
426
427 index = find_portno(br);
428 if (index < 0)
429 return ERR_PTR(index);
430
431 p = kzalloc(sizeof(*p), GFP_KERNEL);
432 if (p == NULL)
433 return ERR_PTR(-ENOMEM);
434
435 p->br = br;
436 netdev_hold(dev, &p->dev_tracker, GFP_KERNEL);
437 p->dev = dev;
438 p->path_cost = port_cost(dev);
439 p->priority = 0x8000 >> BR_PORT_BITS;
440 p->port_no = index;
441 p->flags = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
442 br_init_port(p);
443 br_set_state(p, BR_STATE_DISABLED);
444 br_stp_port_timer_init(p);
445 err = br_multicast_add_port(p);
446 if (err) {
447 netdev_put(dev, &p->dev_tracker);
448 kfree(p);
449 p = ERR_PTR(err);
450 }
451
452 return p;
453}
454
455int br_add_bridge(struct net *net, const char *name)
456{
457 struct net_device *dev;
458 int res;
459
460 dev = alloc_netdev(sizeof(struct net_bridge), name, NET_NAME_UNKNOWN,
461 br_dev_setup);
462
463 if (!dev)
464 return -ENOMEM;
465
466 dev_net_set(dev, net);
467 dev->rtnl_link_ops = &br_link_ops;
468
469 res = register_netdevice(dev);
470 if (res)
471 free_netdev(dev);
472 return res;
473}
474
475int br_del_bridge(struct net *net, const char *name)
476{
477 struct net_device *dev;
478 int ret = 0;
479
480 dev = __dev_get_by_name(net, name);
481 if (dev == NULL)
482 ret = -ENXIO; /* Could not find device */
483
484 else if (!netif_is_bridge_master(dev)) {
485 /* Attempt to delete non bridge device! */
486 ret = -EPERM;
487 }
488
489 else if (dev->flags & IFF_UP) {
490 /* Not shutdown yet. */
491 ret = -EBUSY;
492 }
493
494 else
495 br_dev_delete(dev, NULL);
496
497 return ret;
498}
499
500/* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
501static int br_mtu_min(const struct net_bridge *br)
502{
503 const struct net_bridge_port *p;
504 int ret_mtu = 0;
505
506 list_for_each_entry(p, &br->port_list, list)
507 if (!ret_mtu || ret_mtu > p->dev->mtu)
508 ret_mtu = p->dev->mtu;
509
510 return ret_mtu ? ret_mtu : ETH_DATA_LEN;
511}
512
513void br_mtu_auto_adjust(struct net_bridge *br)
514{
515 ASSERT_RTNL();
516
517 /* if the bridge MTU was manually configured don't mess with it */
518 if (br_opt_get(br, BROPT_MTU_SET_BY_USER))
519 return;
520
521 /* change to the minimum MTU and clear the flag which was set by
522 * the bridge ndo_change_mtu callback
523 */
524 dev_set_mtu(br->dev, br_mtu_min(br));
525 br_opt_toggle(br, BROPT_MTU_SET_BY_USER, false);
526}
527
528static void br_set_gso_limits(struct net_bridge *br)
529{
530 unsigned int tso_max_size = TSO_MAX_SIZE;
531 const struct net_bridge_port *p;
532 u16 tso_max_segs = TSO_MAX_SEGS;
533
534 list_for_each_entry(p, &br->port_list, list) {
535 tso_max_size = min(tso_max_size, p->dev->tso_max_size);
536 tso_max_segs = min(tso_max_segs, p->dev->tso_max_segs);
537 }
538 netif_set_tso_max_size(br->dev, tso_max_size);
539 netif_set_tso_max_segs(br->dev, tso_max_segs);
540}
541
542/*
543 * Recomputes features using slave's features
544 */
545netdev_features_t br_features_recompute(struct net_bridge *br,
546 netdev_features_t features)
547{
548 struct net_bridge_port *p;
549 netdev_features_t mask;
550
551 if (list_empty(&br->port_list))
552 return features;
553
554 mask = features;
555 features &= ~NETIF_F_ONE_FOR_ALL;
556
557 list_for_each_entry(p, &br->port_list, list) {
558 features = netdev_increment_features(features,
559 p->dev->features, mask);
560 }
561 features = netdev_add_tso_features(features, mask);
562
563 return features;
564}
565
566/* called with RTNL */
567int br_add_if(struct net_bridge *br, struct net_device *dev,
568 struct netlink_ext_ack *extack)
569{
570 struct net_bridge_port *p;
571 int err = 0;
572 unsigned br_hr, dev_hr;
573 bool changed_addr, fdb_synced = false;
574
575 /* Don't allow bridging non-ethernet like devices. */
576 if ((dev->flags & IFF_LOOPBACK) ||
577 dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
578 !is_valid_ether_addr(dev->dev_addr))
579 return -EINVAL;
580
581 /* No bridging of bridges */
582 if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit) {
583 NL_SET_ERR_MSG(extack,
584 "Can not enslave a bridge to a bridge");
585 return -ELOOP;
586 }
587
588 /* Device has master upper dev */
589 if (netdev_master_upper_dev_get(dev))
590 return -EBUSY;
591
592 /* No bridging devices that dislike that (e.g. wireless) */
593 if (dev->priv_flags & IFF_DONT_BRIDGE) {
594 NL_SET_ERR_MSG(extack,
595 "Device does not allow enslaving to a bridge");
596 return -EOPNOTSUPP;
597 }
598
599 p = new_nbp(br, dev);
600 if (IS_ERR(p))
601 return PTR_ERR(p);
602
603 call_netdevice_notifiers(NETDEV_JOIN, dev);
604
605 err = dev_set_allmulti(dev, 1);
606 if (err) {
607 br_multicast_del_port(p);
608 netdev_put(dev, &p->dev_tracker);
609 kfree(p); /* kobject not yet init'd, manually free */
610 goto err1;
611 }
612
613 err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
614 SYSFS_BRIDGE_PORT_ATTR);
615 if (err)
616 goto err2;
617
618 err = br_sysfs_addif(p);
619 if (err)
620 goto err2;
621
622 err = br_netpoll_enable(p);
623 if (err)
624 goto err3;
625
626 err = netdev_rx_handler_register(dev, br_get_rx_handler(dev), p);
627 if (err)
628 goto err4;
629
630 dev->priv_flags |= IFF_BRIDGE_PORT;
631
632 err = netdev_master_upper_dev_link(dev, br->dev, NULL, NULL, extack);
633 if (err)
634 goto err5;
635
636 dev_disable_lro(dev);
637
638 list_add_rcu(&p->list, &br->port_list);
639
640 nbp_update_port_count(br);
641 if (!br_promisc_port(p) && (p->dev->priv_flags & IFF_UNICAST_FLT)) {
642 /* When updating the port count we also update all ports'
643 * promiscuous mode.
644 * A port leaving promiscuous mode normally gets the bridge's
645 * fdb synced to the unicast filter (if supported), however,
646 * `br_port_clear_promisc` does not distinguish between
647 * non-promiscuous ports and *new* ports, so we need to
648 * sync explicitly here.
649 */
650 fdb_synced = br_fdb_sync_static(br, p) == 0;
651 if (!fdb_synced)
652 netdev_err(dev, "failed to sync bridge static fdb addresses to this port\n");
653 }
654
655 netdev_update_features(br->dev);
656
657 br_hr = br->dev->needed_headroom;
658 dev_hr = netdev_get_fwd_headroom(dev);
659 if (br_hr < dev_hr)
660 update_headroom(br, dev_hr);
661 else
662 netdev_set_rx_headroom(dev, br_hr);
663
664 if (br_fdb_add_local(br, p, dev->dev_addr, 0))
665 netdev_err(dev, "failed insert local address bridge forwarding table\n");
666
667 if (br->dev->addr_assign_type != NET_ADDR_SET) {
668 /* Ask for permission to use this MAC address now, even if we
669 * don't end up choosing it below.
670 */
671 err = dev_pre_changeaddr_notify(br->dev, dev->dev_addr, extack);
672 if (err)
673 goto err6;
674 }
675
676 err = nbp_vlan_init(p, extack);
677 if (err) {
678 netdev_err(dev, "failed to initialize vlan filtering on this port\n");
679 goto err6;
680 }
681
682 spin_lock_bh(&br->lock);
683 changed_addr = br_stp_recalculate_bridge_id(br);
684
685 if (netif_running(dev) && netif_oper_up(dev) &&
686 (br->dev->flags & IFF_UP))
687 br_stp_enable_port(p);
688 spin_unlock_bh(&br->lock);
689
690 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
691
692 if (changed_addr)
693 call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
694
695 br_mtu_auto_adjust(br);
696 br_set_gso_limits(br);
697
698 kobject_uevent(&p->kobj, KOBJ_ADD);
699
700 return 0;
701
702err6:
703 if (fdb_synced)
704 br_fdb_unsync_static(br, p);
705 list_del_rcu(&p->list);
706 br_fdb_delete_by_port(br, p, 0, 1);
707 nbp_update_port_count(br);
708 netdev_upper_dev_unlink(dev, br->dev);
709err5:
710 dev->priv_flags &= ~IFF_BRIDGE_PORT;
711 netdev_rx_handler_unregister(dev);
712err4:
713 br_netpoll_disable(p);
714err3:
715 sysfs_remove_link(br->ifobj, p->dev->name);
716err2:
717 br_multicast_del_port(p);
718 netdev_put(dev, &p->dev_tracker);
719 kobject_put(&p->kobj);
720 dev_set_allmulti(dev, -1);
721err1:
722 return err;
723}
724
725/* called with RTNL */
726int br_del_if(struct net_bridge *br, struct net_device *dev)
727{
728 struct net_bridge_port *p;
729 bool changed_addr;
730
731 p = br_port_get_rtnl(dev);
732 if (!p || p->br != br)
733 return -EINVAL;
734
735 /* Since more than one interface can be attached to a bridge,
736 * there still maybe an alternate path for netconsole to use;
737 * therefore there is no reason for a NETDEV_RELEASE event.
738 */
739 del_nbp(p);
740
741 br_mtu_auto_adjust(br);
742 br_set_gso_limits(br);
743
744 spin_lock_bh(&br->lock);
745 changed_addr = br_stp_recalculate_bridge_id(br);
746 spin_unlock_bh(&br->lock);
747
748 if (changed_addr)
749 call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
750
751 netdev_update_features(br->dev);
752
753 return 0;
754}
755
756void br_port_flags_change(struct net_bridge_port *p, unsigned long mask)
757{
758 struct net_bridge *br = p->br;
759
760 if (mask & BR_AUTO_MASK)
761 nbp_update_port_count(br);
762
763 if (mask & (BR_NEIGH_SUPPRESS | BR_NEIGH_VLAN_SUPPRESS))
764 br_recalculate_neigh_suppress_enabled(br);
765}
766
767bool br_port_flag_is_set(const struct net_device *dev, unsigned long flag)
768{
769 struct net_bridge_port *p;
770
771 p = br_port_get_rtnl_rcu(dev);
772 if (!p)
773 return false;
774
775 return p->flags & flag;
776}
777EXPORT_SYMBOL_GPL(br_port_flag_is_set);
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 */
35static 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. */
64void 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
86static 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
93static struct kobj_type brport_ktype = {
94#ifdef CONFIG_SYSFS
95 .sysfs_ops = &brport_sysfs_ops,
96#endif
97 .release = release_nbp,
98};
99
100static 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
111static 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 */
127static 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 */
164void 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 del_timer_sync(&br->gc_timer);
174
175 br_sysfs_delbr(br->dev);
176 unregister_netdevice_queue(br->dev, head);
177}
178
179/* find an available port number */
180static int find_portno(struct net_bridge *br)
181{
182 int index;
183 struct net_bridge_port *p;
184 unsigned long *inuse;
185
186 inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
187 GFP_KERNEL);
188 if (!inuse)
189 return -ENOMEM;
190
191 set_bit(0, inuse); /* zero is reserved */
192 list_for_each_entry(p, &br->port_list, list) {
193 set_bit(p->port_no, inuse);
194 }
195 index = find_first_zero_bit(inuse, BR_MAX_PORTS);
196 kfree(inuse);
197
198 return (index >= BR_MAX_PORTS) ? -EXFULL : index;
199}
200
201/* called with RTNL but without bridge lock */
202static struct net_bridge_port *new_nbp(struct net_bridge *br,
203 struct net_device *dev)
204{
205 int index;
206 struct net_bridge_port *p;
207
208 index = find_portno(br);
209 if (index < 0)
210 return ERR_PTR(index);
211
212 p = kzalloc(sizeof(*p), GFP_KERNEL);
213 if (p == NULL)
214 return ERR_PTR(-ENOMEM);
215
216 p->br = br;
217 dev_hold(dev);
218 p->dev = dev;
219 p->path_cost = port_cost(dev);
220 p->priority = 0x8000 >> BR_PORT_BITS;
221 p->port_no = index;
222 p->flags = 0;
223 br_init_port(p);
224 p->state = BR_STATE_DISABLED;
225 br_stp_port_timer_init(p);
226 br_multicast_add_port(p);
227
228 return p;
229}
230
231int br_add_bridge(struct net *net, const char *name)
232{
233 struct net_device *dev;
234 int res;
235
236 dev = alloc_netdev(sizeof(struct net_bridge), name,
237 br_dev_setup);
238
239 if (!dev)
240 return -ENOMEM;
241
242 dev_net_set(dev, net);
243 dev->rtnl_link_ops = &br_link_ops;
244
245 res = register_netdev(dev);
246 if (res)
247 free_netdev(dev);
248 return res;
249}
250
251int br_del_bridge(struct net *net, const char *name)
252{
253 struct net_device *dev;
254 int ret = 0;
255
256 rtnl_lock();
257 dev = __dev_get_by_name(net, name);
258 if (dev == NULL)
259 ret = -ENXIO; /* Could not find device */
260
261 else if (!(dev->priv_flags & IFF_EBRIDGE)) {
262 /* Attempt to delete non bridge device! */
263 ret = -EPERM;
264 }
265
266 else if (dev->flags & IFF_UP) {
267 /* Not shutdown yet. */
268 ret = -EBUSY;
269 }
270
271 else
272 br_dev_delete(dev, NULL);
273
274 rtnl_unlock();
275 return ret;
276}
277
278/* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
279int br_min_mtu(const struct net_bridge *br)
280{
281 const struct net_bridge_port *p;
282 int mtu = 0;
283
284 ASSERT_RTNL();
285
286 if (list_empty(&br->port_list))
287 mtu = ETH_DATA_LEN;
288 else {
289 list_for_each_entry(p, &br->port_list, list) {
290 if (!mtu || p->dev->mtu < mtu)
291 mtu = p->dev->mtu;
292 }
293 }
294 return mtu;
295}
296
297/*
298 * Recomputes features using slave's features
299 */
300netdev_features_t br_features_recompute(struct net_bridge *br,
301 netdev_features_t features)
302{
303 struct net_bridge_port *p;
304 netdev_features_t mask;
305
306 if (list_empty(&br->port_list))
307 return features;
308
309 mask = features;
310 features &= ~NETIF_F_ONE_FOR_ALL;
311
312 list_for_each_entry(p, &br->port_list, list) {
313 features = netdev_increment_features(features,
314 p->dev->features, mask);
315 }
316
317 return features;
318}
319
320/* called with RTNL */
321int br_add_if(struct net_bridge *br, struct net_device *dev)
322{
323 struct net_bridge_port *p;
324 int err = 0;
325 bool changed_addr;
326
327 /* Don't allow bridging non-ethernet like devices */
328 if ((dev->flags & IFF_LOOPBACK) ||
329 dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
330 !is_valid_ether_addr(dev->dev_addr))
331 return -EINVAL;
332
333 /* No bridging of bridges */
334 if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit)
335 return -ELOOP;
336
337 /* Device is already being bridged */
338 if (br_port_exists(dev))
339 return -EBUSY;
340
341 /* No bridging devices that dislike that (e.g. wireless) */
342 if (dev->priv_flags & IFF_DONT_BRIDGE)
343 return -EOPNOTSUPP;
344
345 p = new_nbp(br, dev);
346 if (IS_ERR(p))
347 return PTR_ERR(p);
348
349 call_netdevice_notifiers(NETDEV_JOIN, dev);
350
351 err = dev_set_promiscuity(dev, 1);
352 if (err)
353 goto put_back;
354
355 err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
356 SYSFS_BRIDGE_PORT_ATTR);
357 if (err)
358 goto err1;
359
360 err = br_sysfs_addif(p);
361 if (err)
362 goto err2;
363
364 if (br_netpoll_info(br) && ((err = br_netpoll_enable(p))))
365 goto err3;
366
367 err = netdev_set_master(dev, br->dev);
368 if (err)
369 goto err3;
370
371 err = netdev_rx_handler_register(dev, br_handle_frame, p);
372 if (err)
373 goto err4;
374
375 dev->priv_flags |= IFF_BRIDGE_PORT;
376
377 dev_disable_lro(dev);
378
379 list_add_rcu(&p->list, &br->port_list);
380
381 netdev_update_features(br->dev);
382
383 spin_lock_bh(&br->lock);
384 changed_addr = br_stp_recalculate_bridge_id(br);
385
386 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
387 (br->dev->flags & IFF_UP))
388 br_stp_enable_port(p);
389 spin_unlock_bh(&br->lock);
390
391 br_ifinfo_notify(RTM_NEWLINK, p);
392
393 if (changed_addr)
394 call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
395
396 dev_set_mtu(br->dev, br_min_mtu(br));
397
398 if (br_fdb_insert(br, p, dev->dev_addr))
399 netdev_err(dev, "failed insert local address bridge forwarding table\n");
400
401 kobject_uevent(&p->kobj, KOBJ_ADD);
402
403 return 0;
404
405err4:
406 netdev_set_master(dev, NULL);
407err3:
408 sysfs_remove_link(br->ifobj, p->dev->name);
409err2:
410 kobject_put(&p->kobj);
411 p = NULL; /* kobject_put frees */
412err1:
413 dev_set_promiscuity(dev, -1);
414put_back:
415 dev_put(dev);
416 kfree(p);
417 return err;
418}
419
420/* called with RTNL */
421int br_del_if(struct net_bridge *br, struct net_device *dev)
422{
423 struct net_bridge_port *p;
424 bool changed_addr;
425
426 p = br_port_get_rtnl(dev);
427 if (!p || p->br != br)
428 return -EINVAL;
429
430 del_nbp(p);
431
432 spin_lock_bh(&br->lock);
433 changed_addr = br_stp_recalculate_bridge_id(br);
434 spin_unlock_bh(&br->lock);
435
436 if (changed_addr)
437 call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
438
439 netdev_update_features(br->dev);
440
441 return 0;
442}
443
444void __net_exit br_net_exit(struct net *net)
445{
446 struct net_device *dev;
447 LIST_HEAD(list);
448
449 rtnl_lock();
450 for_each_netdev(net, dev)
451 if (dev->priv_flags & IFF_EBRIDGE)
452 br_dev_delete(dev, &list);
453
454 unregister_netdevice_many(&list);
455 rtnl_unlock();
456
457}