br.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic parts
  4. * Linux ethernet bridge
  5. *
  6. * Authors:
  7. * Lennert Buytenhek <[email protected]>
  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 (netif_is_bridge_master(dev)) {
  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 underlying 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. /* called under rtnl_mutex */
  173. static int br_switchdev_blocking_event(struct notifier_block *nb,
  174. unsigned long event, void *ptr)
  175. {
  176. struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
  177. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  178. struct switchdev_notifier_brport_info *brport_info;
  179. const struct switchdev_brport *b;
  180. struct net_bridge_port *p;
  181. int err = NOTIFY_DONE;
  182. p = br_port_get_rtnl(dev);
  183. if (!p)
  184. goto out;
  185. switch (event) {
  186. case SWITCHDEV_BRPORT_OFFLOADED:
  187. brport_info = ptr;
  188. b = &brport_info->brport;
  189. err = br_switchdev_port_offload(p, b->dev, b->ctx,
  190. b->atomic_nb, b->blocking_nb,
  191. b->tx_fwd_offload, extack);
  192. err = notifier_from_errno(err);
  193. break;
  194. case SWITCHDEV_BRPORT_UNOFFLOADED:
  195. brport_info = ptr;
  196. b = &brport_info->brport;
  197. br_switchdev_port_unoffload(p, b->ctx, b->atomic_nb,
  198. b->blocking_nb);
  199. break;
  200. }
  201. out:
  202. return err;
  203. }
  204. static struct notifier_block br_switchdev_blocking_notifier = {
  205. .notifier_call = br_switchdev_blocking_event,
  206. };
  207. /* br_boolopt_toggle - change user-controlled boolean option
  208. *
  209. * @br: bridge device
  210. * @opt: id of the option to change
  211. * @on: new option value
  212. * @extack: extack for error messages
  213. *
  214. * Changes the value of the respective boolean option to @on taking care of
  215. * any internal option value mapping and configuration.
  216. */
  217. int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
  218. struct netlink_ext_ack *extack)
  219. {
  220. int err = 0;
  221. switch (opt) {
  222. case BR_BOOLOPT_NO_LL_LEARN:
  223. br_opt_toggle(br, BROPT_NO_LL_LEARN, on);
  224. break;
  225. case BR_BOOLOPT_MCAST_VLAN_SNOOPING:
  226. err = br_multicast_toggle_vlan_snooping(br, on, extack);
  227. break;
  228. case BR_BOOLOPT_MST_ENABLE:
  229. err = br_mst_set_enabled(br, on, extack);
  230. break;
  231. default:
  232. /* shouldn't be called with unsupported options */
  233. WARN_ON(1);
  234. break;
  235. }
  236. return err;
  237. }
  238. int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt)
  239. {
  240. switch (opt) {
  241. case BR_BOOLOPT_NO_LL_LEARN:
  242. return br_opt_get(br, BROPT_NO_LL_LEARN);
  243. case BR_BOOLOPT_MCAST_VLAN_SNOOPING:
  244. return br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED);
  245. case BR_BOOLOPT_MST_ENABLE:
  246. return br_opt_get(br, BROPT_MST_ENABLED);
  247. default:
  248. /* shouldn't be called with unsupported options */
  249. WARN_ON(1);
  250. break;
  251. }
  252. return 0;
  253. }
  254. int br_boolopt_multi_toggle(struct net_bridge *br,
  255. struct br_boolopt_multi *bm,
  256. struct netlink_ext_ack *extack)
  257. {
  258. unsigned long bitmap = bm->optmask;
  259. int err = 0;
  260. int opt_id;
  261. for_each_set_bit(opt_id, &bitmap, BR_BOOLOPT_MAX) {
  262. bool on = !!(bm->optval & BIT(opt_id));
  263. err = br_boolopt_toggle(br, opt_id, on, extack);
  264. if (err) {
  265. br_debug(br, "boolopt multi-toggle error: option: %d current: %d new: %d error: %d\n",
  266. opt_id, br_boolopt_get(br, opt_id), on, err);
  267. break;
  268. }
  269. }
  270. return err;
  271. }
  272. void br_boolopt_multi_get(const struct net_bridge *br,
  273. struct br_boolopt_multi *bm)
  274. {
  275. u32 optval = 0;
  276. int opt_id;
  277. for (opt_id = 0; opt_id < BR_BOOLOPT_MAX; opt_id++)
  278. optval |= (br_boolopt_get(br, opt_id) << opt_id);
  279. bm->optval = optval;
  280. bm->optmask = GENMASK((BR_BOOLOPT_MAX - 1), 0);
  281. }
  282. /* private bridge options, controlled by the kernel */
  283. void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on)
  284. {
  285. bool cur = !!br_opt_get(br, opt);
  286. br_debug(br, "toggle option: %d state: %d -> %d\n",
  287. opt, cur, on);
  288. if (cur == on)
  289. return;
  290. if (on)
  291. set_bit(opt, &br->options);
  292. else
  293. clear_bit(opt, &br->options);
  294. }
  295. static void __net_exit br_net_exit_batch(struct list_head *net_list)
  296. {
  297. struct net_device *dev;
  298. struct net *net;
  299. LIST_HEAD(list);
  300. rtnl_lock();
  301. list_for_each_entry(net, net_list, exit_list)
  302. for_each_netdev(net, dev)
  303. if (netif_is_bridge_master(dev))
  304. br_dev_delete(dev, &list);
  305. unregister_netdevice_many(&list);
  306. rtnl_unlock();
  307. }
  308. static struct pernet_operations br_net_ops = {
  309. .exit_batch = br_net_exit_batch,
  310. };
  311. static const struct stp_proto br_stp_proto = {
  312. .rcv = br_stp_rcv,
  313. };
  314. static int __init br_init(void)
  315. {
  316. int err;
  317. BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > sizeof_field(struct sk_buff, cb));
  318. err = stp_proto_register(&br_stp_proto);
  319. if (err < 0) {
  320. pr_err("bridge: can't register sap for STP\n");
  321. return err;
  322. }
  323. err = br_fdb_init();
  324. if (err)
  325. goto err_out;
  326. err = register_pernet_subsys(&br_net_ops);
  327. if (err)
  328. goto err_out1;
  329. err = br_nf_core_init();
  330. if (err)
  331. goto err_out2;
  332. err = register_netdevice_notifier(&br_device_notifier);
  333. if (err)
  334. goto err_out3;
  335. err = register_switchdev_notifier(&br_switchdev_notifier);
  336. if (err)
  337. goto err_out4;
  338. err = register_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
  339. if (err)
  340. goto err_out5;
  341. err = br_netlink_init();
  342. if (err)
  343. goto err_out6;
  344. brioctl_set(br_ioctl_stub);
  345. #if IS_ENABLED(CONFIG_ATM_LANE)
  346. br_fdb_test_addr_hook = br_fdb_test_addr;
  347. #endif
  348. #if IS_MODULE(CONFIG_BRIDGE_NETFILTER)
  349. pr_info("bridge: filtering via arp/ip/ip6tables is no longer available "
  350. "by default. Update your scripts to load br_netfilter if you "
  351. "need this.\n");
  352. #endif
  353. return 0;
  354. err_out6:
  355. unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
  356. err_out5:
  357. unregister_switchdev_notifier(&br_switchdev_notifier);
  358. err_out4:
  359. unregister_netdevice_notifier(&br_device_notifier);
  360. err_out3:
  361. br_nf_core_fini();
  362. err_out2:
  363. unregister_pernet_subsys(&br_net_ops);
  364. err_out1:
  365. br_fdb_fini();
  366. err_out:
  367. stp_proto_unregister(&br_stp_proto);
  368. return err;
  369. }
  370. static void __exit br_deinit(void)
  371. {
  372. stp_proto_unregister(&br_stp_proto);
  373. br_netlink_fini();
  374. unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
  375. unregister_switchdev_notifier(&br_switchdev_notifier);
  376. unregister_netdevice_notifier(&br_device_notifier);
  377. brioctl_set(NULL);
  378. unregister_pernet_subsys(&br_net_ops);
  379. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  380. br_nf_core_fini();
  381. #if IS_ENABLED(CONFIG_ATM_LANE)
  382. br_fdb_test_addr_hook = NULL;
  383. #endif
  384. br_fdb_fini();
  385. }
  386. module_init(br_init)
  387. module_exit(br_deinit)
  388. MODULE_LICENSE("GPL");
  389. MODULE_VERSION(BR_VERSION);
  390. MODULE_ALIAS_RTNL_LINK("bridge");