br_notify.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Device event handling
  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. #include <linux/kernel.h>
  14. #include <linux/rtnetlink.h>
  15. #include <net/net_namespace.h>
  16. #include "br_private.h"
  17. static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr);
  18. struct notifier_block br_device_notifier = {
  19. .notifier_call = br_device_event
  20. };
  21. /*
  22. * Handle changes in state of network devices enslaved to a bridge.
  23. *
  24. * Note: don't care about up/down if bridge itself is down, because
  25. * port state is checked when bridge is brought up.
  26. */
  27. static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  28. {
  29. struct net_device *dev = ptr;
  30. struct net_bridge_port *p;
  31. struct net_bridge *br;
  32. bool changed_addr;
  33. int err;
  34. /* register of bridge completed, add sysfs entries */
  35. if ((dev->priv_flags & IFF_EBRIDGE) && event == NETDEV_REGISTER) {
  36. br_sysfs_addbr(dev);
  37. return NOTIFY_DONE;
  38. }
  39. /* not a port of a bridge */
  40. p = br_port_get_rtnl(dev);
  41. if (!p)
  42. return NOTIFY_DONE;
  43. br = p->br;
  44. switch (event) {
  45. case NETDEV_CHANGEMTU:
  46. dev_set_mtu(br->dev, br_min_mtu(br));
  47. break;
  48. case NETDEV_CHANGEADDR:
  49. spin_lock_bh(&br->lock);
  50. br_fdb_changeaddr(p, dev->dev_addr);
  51. changed_addr = br_stp_recalculate_bridge_id(br);
  52. spin_unlock_bh(&br->lock);
  53. if (changed_addr)
  54. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  55. break;
  56. case NETDEV_CHANGE:
  57. br_port_carrier_check(p);
  58. break;
  59. case NETDEV_FEAT_CHANGE:
  60. netdev_update_features(br->dev);
  61. break;
  62. case NETDEV_DOWN:
  63. spin_lock_bh(&br->lock);
  64. if (br->dev->flags & IFF_UP)
  65. br_stp_disable_port(p);
  66. spin_unlock_bh(&br->lock);
  67. break;
  68. case NETDEV_UP:
  69. if (netif_carrier_ok(dev) && (br->dev->flags & IFF_UP)) {
  70. spin_lock_bh(&br->lock);
  71. br_stp_enable_port(p);
  72. spin_unlock_bh(&br->lock);
  73. }
  74. break;
  75. case NETDEV_UNREGISTER:
  76. br_del_if(br, dev);
  77. break;
  78. case NETDEV_CHANGENAME:
  79. err = br_sysfs_renameif(p);
  80. if (err)
  81. return notifier_from_errno(err);
  82. break;
  83. case NETDEV_PRE_TYPE_CHANGE:
  84. /* Forbid underlaying device to change its type. */
  85. return NOTIFY_BAD;
  86. }
  87. /* Events that may cause spanning tree to refresh */
  88. if (event == NETDEV_CHANGEADDR || event == NETDEV_UP ||
  89. event == NETDEV_CHANGE || event == NETDEV_DOWN)
  90. br_ifinfo_notify(RTM_NEWLINK, p);
  91. return NOTIFY_DONE;
  92. }