br.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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, true);
  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, fdb_info->offloaded);
  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. void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on)
  151. {
  152. bool cur = !!br_opt_get(br, opt);
  153. br_debug(br, "toggle option: %d state: %d -> %d\n",
  154. opt, cur, on);
  155. if (cur == on)
  156. return;
  157. if (on)
  158. set_bit(opt, &br->options);
  159. else
  160. clear_bit(opt, &br->options);
  161. }
  162. static void __net_exit br_net_exit(struct net *net)
  163. {
  164. struct net_device *dev;
  165. LIST_HEAD(list);
  166. rtnl_lock();
  167. for_each_netdev(net, dev)
  168. if (dev->priv_flags & IFF_EBRIDGE)
  169. br_dev_delete(dev, &list);
  170. unregister_netdevice_many(&list);
  171. rtnl_unlock();
  172. }
  173. static struct pernet_operations br_net_ops = {
  174. .exit = br_net_exit,
  175. };
  176. static const struct stp_proto br_stp_proto = {
  177. .rcv = br_stp_rcv,
  178. };
  179. static int __init br_init(void)
  180. {
  181. int err;
  182. BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  183. err = stp_proto_register(&br_stp_proto);
  184. if (err < 0) {
  185. pr_err("bridge: can't register sap for STP\n");
  186. return err;
  187. }
  188. err = br_fdb_init();
  189. if (err)
  190. goto err_out;
  191. err = register_pernet_subsys(&br_net_ops);
  192. if (err)
  193. goto err_out1;
  194. err = br_nf_core_init();
  195. if (err)
  196. goto err_out2;
  197. err = register_netdevice_notifier(&br_device_notifier);
  198. if (err)
  199. goto err_out3;
  200. err = register_switchdev_notifier(&br_switchdev_notifier);
  201. if (err)
  202. goto err_out4;
  203. err = br_netlink_init();
  204. if (err)
  205. goto err_out5;
  206. brioctl_set(br_ioctl_deviceless_stub);
  207. #if IS_ENABLED(CONFIG_ATM_LANE)
  208. br_fdb_test_addr_hook = br_fdb_test_addr;
  209. #endif
  210. #if IS_MODULE(CONFIG_BRIDGE_NETFILTER)
  211. pr_info("bridge: filtering via arp/ip/ip6tables is no longer available "
  212. "by default. Update your scripts to load br_netfilter if you "
  213. "need this.\n");
  214. #endif
  215. return 0;
  216. err_out5:
  217. unregister_switchdev_notifier(&br_switchdev_notifier);
  218. err_out4:
  219. unregister_netdevice_notifier(&br_device_notifier);
  220. err_out3:
  221. br_nf_core_fini();
  222. err_out2:
  223. unregister_pernet_subsys(&br_net_ops);
  224. err_out1:
  225. br_fdb_fini();
  226. err_out:
  227. stp_proto_unregister(&br_stp_proto);
  228. return err;
  229. }
  230. static void __exit br_deinit(void)
  231. {
  232. stp_proto_unregister(&br_stp_proto);
  233. br_netlink_fini();
  234. unregister_switchdev_notifier(&br_switchdev_notifier);
  235. unregister_netdevice_notifier(&br_device_notifier);
  236. brioctl_set(NULL);
  237. unregister_pernet_subsys(&br_net_ops);
  238. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  239. br_nf_core_fini();
  240. #if IS_ENABLED(CONFIG_ATM_LANE)
  241. br_fdb_test_addr_hook = NULL;
  242. #endif
  243. br_fdb_fini();
  244. }
  245. module_init(br_init)
  246. module_exit(br_deinit)
  247. MODULE_LICENSE("GPL");
  248. MODULE_VERSION(BR_VERSION);
  249. MODULE_ALIAS_RTNL_LINK("bridge");