br.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic parts
  4. * Linux ethernet bridge
  5. *
  6. * Authors:
  7. * Lennert Buytenhek <buytenh@gnu.org>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/init.h>
  14. #include <linux/llc.h>
  15. #include <net/llc.h>
  16. #include <net/stp.h>
  17. #include <net/switchdev.h>
  18. #include "br_private.h"
  19. /*
  20. * Handle changes in state of network devices enslaved to a bridge.
  21. *
  22. * Note: don't care about up/down if bridge itself is down, because
  23. * port state is checked when bridge is brought up.
  24. */
  25. static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  26. {
  27. struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
  28. struct netdev_notifier_pre_changeaddr_info *prechaddr_info;
  29. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  30. struct net_bridge_port *p;
  31. struct net_bridge *br;
  32. bool notified = false;
  33. bool changed_addr;
  34. int err;
  35. if (dev->priv_flags & IFF_EBRIDGE) {
  36. err = br_vlan_bridge_event(dev, event, ptr);
  37. if (err)
  38. return notifier_from_errno(err);
  39. if (event == NETDEV_REGISTER) {
  40. /* register of bridge completed, add sysfs entries */
  41. err = br_sysfs_addbr(dev);
  42. if (err)
  43. return notifier_from_errno(err);
  44. return NOTIFY_DONE;
  45. }
  46. }
  47. /* not a port of a bridge */
  48. p = br_port_get_rtnl(dev);
  49. if (!p)
  50. return NOTIFY_DONE;
  51. br = p->br;
  52. switch (event) {
  53. case NETDEV_CHANGEMTU:
  54. br_mtu_auto_adjust(br);
  55. break;
  56. case NETDEV_PRE_CHANGEADDR:
  57. if (br->dev->addr_assign_type == NET_ADDR_SET)
  58. break;
  59. prechaddr_info = ptr;
  60. err = dev_pre_changeaddr_notify(br->dev,
  61. prechaddr_info->dev_addr,
  62. extack);
  63. if (err)
  64. return notifier_from_errno(err);
  65. break;
  66. case NETDEV_CHANGEADDR:
  67. spin_lock_bh(&br->lock);
  68. br_fdb_changeaddr(p, dev->dev_addr);
  69. changed_addr = br_stp_recalculate_bridge_id(br);
  70. spin_unlock_bh(&br->lock);
  71. if (changed_addr)
  72. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  73. break;
  74. case NETDEV_CHANGE:
  75. br_port_carrier_check(p, &notified);
  76. break;
  77. case NETDEV_FEAT_CHANGE:
  78. netdev_update_features(br->dev);
  79. break;
  80. case NETDEV_DOWN:
  81. spin_lock_bh(&br->lock);
  82. if (br->dev->flags & IFF_UP) {
  83. br_stp_disable_port(p);
  84. notified = true;
  85. }
  86. spin_unlock_bh(&br->lock);
  87. break;
  88. case NETDEV_UP:
  89. if (netif_running(br->dev) && netif_oper_up(dev)) {
  90. spin_lock_bh(&br->lock);
  91. br_stp_enable_port(p);
  92. notified = true;
  93. spin_unlock_bh(&br->lock);
  94. }
  95. break;
  96. case NETDEV_UNREGISTER:
  97. br_del_if(br, dev);
  98. break;
  99. case NETDEV_CHANGENAME:
  100. err = br_sysfs_renameif(p);
  101. if (err)
  102. return notifier_from_errno(err);
  103. break;
  104. case NETDEV_PRE_TYPE_CHANGE:
  105. /* Forbid underlaying device to change its type. */
  106. return NOTIFY_BAD;
  107. case NETDEV_RESEND_IGMP:
  108. /* Propagate to master device */
  109. call_netdevice_notifiers(event, br->dev);
  110. break;
  111. }
  112. if (event != NETDEV_UNREGISTER)
  113. br_vlan_port_event(p, event);
  114. /* Events that may cause spanning tree to refresh */
  115. if (!notified && (event == NETDEV_CHANGEADDR || event == NETDEV_UP ||
  116. event == NETDEV_CHANGE || event == NETDEV_DOWN))
  117. br_ifinfo_notify(RTM_NEWLINK, NULL, p);
  118. return NOTIFY_DONE;
  119. }
  120. static struct notifier_block br_device_notifier = {
  121. .notifier_call = br_device_event
  122. };
  123. /* called with RTNL or RCU */
  124. static int br_switchdev_event(struct notifier_block *unused,
  125. unsigned long event, void *ptr)
  126. {
  127. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  128. struct net_bridge_port *p;
  129. struct net_bridge *br;
  130. struct switchdev_notifier_fdb_info *fdb_info;
  131. int err = NOTIFY_DONE;
  132. p = br_port_get_rtnl_rcu(dev);
  133. if (!p)
  134. goto out;
  135. br = p->br;
  136. switch (event) {
  137. case SWITCHDEV_FDB_ADD_TO_BRIDGE:
  138. fdb_info = ptr;
  139. err = br_fdb_external_learn_add(br, p, fdb_info->addr,
  140. fdb_info->vid, false);
  141. if (err) {
  142. err = notifier_from_errno(err);
  143. break;
  144. }
  145. br_fdb_offloaded_set(br, p, fdb_info->addr,
  146. fdb_info->vid, true);
  147. break;
  148. case SWITCHDEV_FDB_DEL_TO_BRIDGE:
  149. fdb_info = ptr;
  150. err = br_fdb_external_learn_del(br, p, fdb_info->addr,
  151. fdb_info->vid, false);
  152. if (err)
  153. err = notifier_from_errno(err);
  154. break;
  155. case SWITCHDEV_FDB_OFFLOADED:
  156. fdb_info = ptr;
  157. br_fdb_offloaded_set(br, p, fdb_info->addr,
  158. fdb_info->vid, fdb_info->offloaded);
  159. break;
  160. case SWITCHDEV_FDB_FLUSH_TO_BRIDGE:
  161. fdb_info = ptr;
  162. /* Don't delete static entries */
  163. br_fdb_delete_by_port(br, p, fdb_info->vid, 0);
  164. break;
  165. }
  166. out:
  167. return err;
  168. }
  169. static struct notifier_block br_switchdev_notifier = {
  170. .notifier_call = br_switchdev_event,
  171. };
  172. /* br_boolopt_toggle - change user-controlled boolean option
  173. *
  174. * @br: bridge device
  175. * @opt: id of the option to change
  176. * @on: new option value
  177. * @extack: extack for error messages
  178. *
  179. * Changes the value of the respective boolean option to @on taking care of
  180. * any internal option value mapping and configuration.
  181. */
  182. int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
  183. struct netlink_ext_ack *extack)
  184. {
  185. switch (opt) {
  186. case BR_BOOLOPT_NO_LL_LEARN:
  187. br_opt_toggle(br, BROPT_NO_LL_LEARN, on);
  188. break;
  189. default:
  190. /* shouldn't be called with unsupported options */
  191. WARN_ON(1);
  192. break;
  193. }
  194. return 0;
  195. }
  196. int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt)
  197. {
  198. switch (opt) {
  199. case BR_BOOLOPT_NO_LL_LEARN:
  200. return br_opt_get(br, BROPT_NO_LL_LEARN);
  201. default:
  202. /* shouldn't be called with unsupported options */
  203. WARN_ON(1);
  204. break;
  205. }
  206. return 0;
  207. }
  208. int br_boolopt_multi_toggle(struct net_bridge *br,
  209. struct br_boolopt_multi *bm,
  210. struct netlink_ext_ack *extack)
  211. {
  212. unsigned long bitmap = bm->optmask;
  213. int err = 0;
  214. int opt_id;
  215. for_each_set_bit(opt_id, &bitmap, BR_BOOLOPT_MAX) {
  216. bool on = !!(bm->optval & BIT(opt_id));
  217. err = br_boolopt_toggle(br, opt_id, on, extack);
  218. if (err) {
  219. br_debug(br, "boolopt multi-toggle error: option: %d current: %d new: %d error: %d\n",
  220. opt_id, br_boolopt_get(br, opt_id), on, err);
  221. break;
  222. }
  223. }
  224. return err;
  225. }
  226. void br_boolopt_multi_get(const struct net_bridge *br,
  227. struct br_boolopt_multi *bm)
  228. {
  229. u32 optval = 0;
  230. int opt_id;
  231. for (opt_id = 0; opt_id < BR_BOOLOPT_MAX; opt_id++)
  232. optval |= (br_boolopt_get(br, opt_id) << opt_id);
  233. bm->optval = optval;
  234. bm->optmask = GENMASK((BR_BOOLOPT_MAX - 1), 0);
  235. }
  236. /* private bridge options, controlled by the kernel */
  237. void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on)
  238. {
  239. bool cur = !!br_opt_get(br, opt);
  240. br_debug(br, "toggle option: %d state: %d -> %d\n",
  241. opt, cur, on);
  242. if (cur == on)
  243. return;
  244. if (on)
  245. set_bit(opt, &br->options);
  246. else
  247. clear_bit(opt, &br->options);
  248. }
  249. static void __net_exit br_net_exit(struct net *net)
  250. {
  251. struct net_device *dev;
  252. LIST_HEAD(list);
  253. rtnl_lock();
  254. for_each_netdev(net, dev)
  255. if (dev->priv_flags & IFF_EBRIDGE)
  256. br_dev_delete(dev, &list);
  257. unregister_netdevice_many(&list);
  258. rtnl_unlock();
  259. }
  260. static struct pernet_operations br_net_ops = {
  261. .exit = br_net_exit,
  262. };
  263. static const struct stp_proto br_stp_proto = {
  264. .rcv = br_stp_rcv,
  265. };
  266. static int __init br_init(void)
  267. {
  268. int err;
  269. BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > sizeof_field(struct sk_buff, cb));
  270. err = stp_proto_register(&br_stp_proto);
  271. if (err < 0) {
  272. pr_err("bridge: can't register sap for STP\n");
  273. return err;
  274. }
  275. err = br_fdb_init();
  276. if (err)
  277. goto err_out;
  278. err = register_pernet_subsys(&br_net_ops);
  279. if (err)
  280. goto err_out1;
  281. err = br_nf_core_init();
  282. if (err)
  283. goto err_out2;
  284. err = register_netdevice_notifier(&br_device_notifier);
  285. if (err)
  286. goto err_out3;
  287. err = register_switchdev_notifier(&br_switchdev_notifier);
  288. if (err)
  289. goto err_out4;
  290. err = br_netlink_init();
  291. if (err)
  292. goto err_out5;
  293. brioctl_set(br_ioctl_deviceless_stub);
  294. #if IS_ENABLED(CONFIG_ATM_LANE)
  295. br_fdb_test_addr_hook = br_fdb_test_addr;
  296. #endif
  297. #if IS_MODULE(CONFIG_BRIDGE_NETFILTER)
  298. pr_info("bridge: filtering via arp/ip/ip6tables is no longer available "
  299. "by default. Update your scripts to load br_netfilter if you "
  300. "need this.\n");
  301. #endif
  302. return 0;
  303. err_out5:
  304. unregister_switchdev_notifier(&br_switchdev_notifier);
  305. err_out4:
  306. unregister_netdevice_notifier(&br_device_notifier);
  307. err_out3:
  308. br_nf_core_fini();
  309. err_out2:
  310. unregister_pernet_subsys(&br_net_ops);
  311. err_out1:
  312. br_fdb_fini();
  313. err_out:
  314. stp_proto_unregister(&br_stp_proto);
  315. return err;
  316. }
  317. static void __exit br_deinit(void)
  318. {
  319. stp_proto_unregister(&br_stp_proto);
  320. br_netlink_fini();
  321. unregister_switchdev_notifier(&br_switchdev_notifier);
  322. unregister_netdevice_notifier(&br_device_notifier);
  323. brioctl_set(NULL);
  324. unregister_pernet_subsys(&br_net_ops);
  325. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  326. br_nf_core_fini();
  327. #if IS_ENABLED(CONFIG_ATM_LANE)
  328. br_fdb_test_addr_hook = NULL;
  329. #endif
  330. br_fdb_fini();
  331. }
  332. module_init(br_init)
  333. module_exit(br_deinit)
  334. MODULE_LICENSE("GPL");
  335. MODULE_VERSION(BR_VERSION);
  336. MODULE_ALIAS_RTNL_LINK("bridge");