br.c 6.2 KB

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