br.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Generic parts
  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/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/init.h>
  18. #include <linux/llc.h>
  19. #include <net/llc.h>
  20. #include <net/stp.h>
  21. #include <net/switchdev.h>
  22. #include "br_private.h"
  23. /*
  24. * Handle changes in state of network devices enslaved to a bridge.
  25. *
  26. * Note: don't care about up/down if bridge itself is down, because
  27. * port state is checked when bridge is brought up.
  28. */
  29. static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  30. {
  31. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  32. struct net_bridge_port *p;
  33. struct net_bridge *br;
  34. bool changed_addr;
  35. int err;
  36. /* register of bridge completed, add sysfs entries */
  37. if ((dev->priv_flags & IFF_EBRIDGE) && event == NETDEV_REGISTER) {
  38. br_sysfs_addbr(dev);
  39. return NOTIFY_DONE;
  40. }
  41. /* not a port of a bridge */
  42. p = br_port_get_rtnl(dev);
  43. if (!p)
  44. return NOTIFY_DONE;
  45. br = p->br;
  46. switch (event) {
  47. case NETDEV_CHANGEMTU:
  48. dev_set_mtu(br->dev, br_min_mtu(br));
  49. break;
  50. case NETDEV_CHANGEADDR:
  51. spin_lock_bh(&br->lock);
  52. br_fdb_changeaddr(p, dev->dev_addr);
  53. changed_addr = br_stp_recalculate_bridge_id(br);
  54. spin_unlock_bh(&br->lock);
  55. if (changed_addr)
  56. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  57. break;
  58. case NETDEV_CHANGE:
  59. br_port_carrier_check(p);
  60. break;
  61. case NETDEV_FEAT_CHANGE:
  62. netdev_update_features(br->dev);
  63. break;
  64. case NETDEV_DOWN:
  65. spin_lock_bh(&br->lock);
  66. if (br->dev->flags & IFF_UP)
  67. br_stp_disable_port(p);
  68. spin_unlock_bh(&br->lock);
  69. break;
  70. case NETDEV_UP:
  71. if (netif_running(br->dev) && netif_oper_up(dev)) {
  72. spin_lock_bh(&br->lock);
  73. br_stp_enable_port(p);
  74. spin_unlock_bh(&br->lock);
  75. }
  76. break;
  77. case NETDEV_UNREGISTER:
  78. br_del_if(br, dev);
  79. break;
  80. case NETDEV_CHANGENAME:
  81. err = br_sysfs_renameif(p);
  82. if (err)
  83. return notifier_from_errno(err);
  84. break;
  85. case NETDEV_PRE_TYPE_CHANGE:
  86. /* Forbid underlaying device to change its type. */
  87. return NOTIFY_BAD;
  88. case NETDEV_RESEND_IGMP:
  89. /* Propagate to master device */
  90. call_netdevice_notifiers(event, br->dev);
  91. break;
  92. }
  93. /* Events that may cause spanning tree to refresh */
  94. if (event == NETDEV_CHANGEADDR || event == NETDEV_UP ||
  95. event == NETDEV_CHANGE || event == NETDEV_DOWN)
  96. br_ifinfo_notify(RTM_NEWLINK, p);
  97. return NOTIFY_DONE;
  98. }
  99. static struct notifier_block br_device_notifier = {
  100. .notifier_call = br_device_event
  101. };
  102. /* called with RTNL */
  103. static int br_switchdev_event(struct notifier_block *unused,
  104. unsigned long event, void *ptr)
  105. {
  106. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  107. struct net_bridge_port *p;
  108. struct net_bridge *br;
  109. struct switchdev_notifier_fdb_info *fdb_info;
  110. int err = NOTIFY_DONE;
  111. p = br_port_get_rtnl(dev);
  112. if (!p)
  113. goto out;
  114. br = p->br;
  115. switch (event) {
  116. case SWITCHDEV_FDB_ADD:
  117. fdb_info = ptr;
  118. err = br_fdb_external_learn_add(br, p, fdb_info->addr,
  119. fdb_info->vid);
  120. if (err)
  121. err = notifier_from_errno(err);
  122. break;
  123. case SWITCHDEV_FDB_DEL:
  124. fdb_info = ptr;
  125. err = br_fdb_external_learn_del(br, p, fdb_info->addr,
  126. fdb_info->vid);
  127. if (err)
  128. err = notifier_from_errno(err);
  129. break;
  130. }
  131. out:
  132. return err;
  133. }
  134. static struct notifier_block br_switchdev_notifier = {
  135. .notifier_call = br_switchdev_event,
  136. };
  137. static void __net_exit br_net_exit(struct net *net)
  138. {
  139. struct net_device *dev;
  140. LIST_HEAD(list);
  141. rtnl_lock();
  142. for_each_netdev(net, dev)
  143. if (dev->priv_flags & IFF_EBRIDGE)
  144. br_dev_delete(dev, &list);
  145. unregister_netdevice_many(&list);
  146. rtnl_unlock();
  147. }
  148. static struct pernet_operations br_net_ops = {
  149. .exit = br_net_exit,
  150. };
  151. static const struct stp_proto br_stp_proto = {
  152. .rcv = br_stp_rcv,
  153. };
  154. static int __init br_init(void)
  155. {
  156. int err;
  157. BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  158. err = stp_proto_register(&br_stp_proto);
  159. if (err < 0) {
  160. pr_err("bridge: can't register sap for STP\n");
  161. return err;
  162. }
  163. err = br_fdb_init();
  164. if (err)
  165. goto err_out;
  166. err = register_pernet_subsys(&br_net_ops);
  167. if (err)
  168. goto err_out1;
  169. err = br_nf_core_init();
  170. if (err)
  171. goto err_out2;
  172. err = register_netdevice_notifier(&br_device_notifier);
  173. if (err)
  174. goto err_out3;
  175. err = register_switchdev_notifier(&br_switchdev_notifier);
  176. if (err)
  177. goto err_out4;
  178. err = br_netlink_init();
  179. if (err)
  180. goto err_out5;
  181. brioctl_set(br_ioctl_deviceless_stub);
  182. #if IS_ENABLED(CONFIG_ATM_LANE)
  183. br_fdb_test_addr_hook = br_fdb_test_addr;
  184. #endif
  185. pr_info("bridge: automatic filtering via arp/ip/ip6tables has been "
  186. "deprecated. Update your scripts to load br_netfilter if you "
  187. "need this.\n");
  188. return 0;
  189. err_out5:
  190. unregister_switchdev_notifier(&br_switchdev_notifier);
  191. err_out4:
  192. unregister_netdevice_notifier(&br_device_notifier);
  193. err_out3:
  194. br_nf_core_fini();
  195. err_out2:
  196. unregister_pernet_subsys(&br_net_ops);
  197. err_out1:
  198. br_fdb_fini();
  199. err_out:
  200. stp_proto_unregister(&br_stp_proto);
  201. return err;
  202. }
  203. static void __exit br_deinit(void)
  204. {
  205. stp_proto_unregister(&br_stp_proto);
  206. br_netlink_fini();
  207. unregister_switchdev_notifier(&br_switchdev_notifier);
  208. unregister_netdevice_notifier(&br_device_notifier);
  209. brioctl_set(NULL);
  210. unregister_pernet_subsys(&br_net_ops);
  211. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  212. br_nf_core_fini();
  213. #if IS_ENABLED(CONFIG_ATM_LANE)
  214. br_fdb_test_addr_hook = NULL;
  215. #endif
  216. br_fdb_fini();
  217. }
  218. module_init(br_init)
  219. module_exit(br_deinit)
  220. MODULE_LICENSE("GPL");
  221. MODULE_VERSION(BR_VERSION);
  222. MODULE_ALIAS_RTNL_LINK("bridge");