br_notify.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. int err;
  33. /* register of bridge completed, add sysfs entries */
  34. if ((dev->priv_flags & IFF_EBRIDGE) && event == NETDEV_REGISTER) {
  35. br_sysfs_addbr(dev);
  36. return NOTIFY_DONE;
  37. }
  38. /* not a port of a bridge */
  39. p = br_port_get_rtnl(dev);
  40. if (!p)
  41. return NOTIFY_DONE;
  42. br = p->br;
  43. switch (event) {
  44. case NETDEV_CHANGEMTU:
  45. dev_set_mtu(br->dev, br_min_mtu(br));
  46. break;
  47. case NETDEV_CHANGEADDR:
  48. spin_lock_bh(&br->lock);
  49. br_fdb_changeaddr(p, dev->dev_addr);
  50. br_stp_recalculate_bridge_id(br);
  51. spin_unlock_bh(&br->lock);
  52. break;
  53. case NETDEV_CHANGE:
  54. br_port_carrier_check(p);
  55. break;
  56. case NETDEV_FEAT_CHANGE:
  57. netdev_update_features(br->dev);
  58. break;
  59. case NETDEV_DOWN:
  60. spin_lock_bh(&br->lock);
  61. if (br->dev->flags & IFF_UP)
  62. br_stp_disable_port(p);
  63. spin_unlock_bh(&br->lock);
  64. break;
  65. case NETDEV_UP:
  66. if (netif_carrier_ok(dev) && (br->dev->flags & IFF_UP)) {
  67. spin_lock_bh(&br->lock);
  68. br_stp_enable_port(p);
  69. spin_unlock_bh(&br->lock);
  70. }
  71. break;
  72. case NETDEV_UNREGISTER:
  73. br_del_if(br, dev);
  74. break;
  75. case NETDEV_CHANGENAME:
  76. err = br_sysfs_renameif(p);
  77. if (err)
  78. return notifier_from_errno(err);
  79. break;
  80. case NETDEV_PRE_TYPE_CHANGE:
  81. /* Forbid underlaying device to change its type. */
  82. return NOTIFY_BAD;
  83. }
  84. /* Events that may cause spanning tree to refresh */
  85. if (event == NETDEV_CHANGEADDR || event == NETDEV_UP ||
  86. event == NETDEV_CHANGE || event == NETDEV_DOWN)
  87. br_ifinfo_notify(RTM_NEWLINK, p);
  88. return NOTIFY_DONE;
  89. }