br.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 (netif_is_bridge_master(dev)) {
  36. struct net_bridge *br = netdev_priv(dev);
  37. if (event == NETDEV_REGISTER)
  38. br_fdb_change_mac_address(br, dev->dev_addr);
  39. err = br_vlan_bridge_event(dev, event, ptr);
  40. if (err)
  41. return notifier_from_errno(err);
  42. if (event == NETDEV_REGISTER) {
  43. /* register of bridge completed, add sysfs entries */
  44. err = br_sysfs_addbr(dev);
  45. if (err)
  46. return notifier_from_errno(err);
  47. return NOTIFY_DONE;
  48. }
  49. }
  50. if (is_vlan_dev(dev)) {
  51. struct net_device *real_dev = vlan_dev_real_dev(dev);
  52. if (netif_is_bridge_master(real_dev))
  53. br_vlan_vlan_upper_event(real_dev, dev, event);
  54. }
  55. /* not a port of a bridge */
  56. p = br_port_get_rtnl(dev);
  57. if (!p)
  58. return NOTIFY_DONE;
  59. br = p->br;
  60. switch (event) {
  61. case NETDEV_CHANGEMTU:
  62. br_mtu_auto_adjust(br);
  63. break;
  64. case NETDEV_PRE_CHANGEADDR:
  65. if (br->dev->addr_assign_type == NET_ADDR_SET)
  66. break;
  67. prechaddr_info = ptr;
  68. err = netif_pre_changeaddr_notify(br->dev,
  69. prechaddr_info->dev_addr,
  70. extack);
  71. if (err)
  72. return notifier_from_errno(err);
  73. break;
  74. case NETDEV_CHANGEADDR:
  75. spin_lock_bh(&br->lock);
  76. br_fdb_changeaddr(p, dev->dev_addr);
  77. changed_addr = br_stp_recalculate_bridge_id(br);
  78. spin_unlock_bh(&br->lock);
  79. if (changed_addr)
  80. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  81. break;
  82. case NETDEV_CHANGE:
  83. br_port_carrier_check(p, &notified);
  84. break;
  85. case NETDEV_FEAT_CHANGE:
  86. netdev_update_features(br->dev);
  87. break;
  88. case NETDEV_DOWN:
  89. spin_lock_bh(&br->lock);
  90. if (br->dev->flags & IFF_UP) {
  91. br_stp_disable_port(p);
  92. notified = true;
  93. }
  94. spin_unlock_bh(&br->lock);
  95. break;
  96. case NETDEV_UP:
  97. if (netif_running(br->dev) && netif_oper_up(dev)) {
  98. spin_lock_bh(&br->lock);
  99. br_stp_enable_port(p);
  100. notified = true;
  101. spin_unlock_bh(&br->lock);
  102. }
  103. break;
  104. case NETDEV_UNREGISTER:
  105. br_del_if(br, dev);
  106. break;
  107. case NETDEV_CHANGENAME:
  108. err = br_sysfs_renameif(p);
  109. if (err)
  110. return notifier_from_errno(err);
  111. break;
  112. case NETDEV_PRE_TYPE_CHANGE:
  113. /* Forbid underlying device to change its type. */
  114. return NOTIFY_BAD;
  115. case NETDEV_RESEND_IGMP:
  116. /* Propagate to master device */
  117. call_netdevice_notifiers(event, br->dev);
  118. break;
  119. }
  120. if (event != NETDEV_UNREGISTER)
  121. br_vlan_port_event(p, event);
  122. /* Events that may cause spanning tree to refresh */
  123. if (!notified && (event == NETDEV_CHANGEADDR || event == NETDEV_UP ||
  124. event == NETDEV_CHANGE || event == NETDEV_DOWN))
  125. br_ifinfo_notify(RTM_NEWLINK, NULL, p);
  126. return NOTIFY_DONE;
  127. }
  128. static struct notifier_block br_device_notifier = {
  129. .notifier_call = br_device_event
  130. };
  131. /* called with RTNL or RCU */
  132. static int br_switchdev_event(struct notifier_block *unused,
  133. unsigned long event, void *ptr)
  134. {
  135. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  136. struct net_bridge_port *p;
  137. struct net_bridge *br;
  138. struct switchdev_notifier_fdb_info *fdb_info;
  139. int err = NOTIFY_DONE;
  140. p = br_port_get_rtnl_rcu(dev);
  141. if (!p)
  142. goto out;
  143. br = p->br;
  144. switch (event) {
  145. case SWITCHDEV_FDB_ADD_TO_BRIDGE:
  146. fdb_info = ptr;
  147. err = br_fdb_external_learn_add(br, p, fdb_info->addr,
  148. fdb_info->vid,
  149. fdb_info->locked, false);
  150. if (err) {
  151. err = notifier_from_errno(err);
  152. break;
  153. }
  154. br_fdb_offloaded_set(br, p, fdb_info->addr,
  155. fdb_info->vid, fdb_info->offloaded);
  156. break;
  157. case SWITCHDEV_FDB_DEL_TO_BRIDGE:
  158. fdb_info = ptr;
  159. err = br_fdb_external_learn_del(br, p, fdb_info->addr,
  160. fdb_info->vid, false);
  161. if (err)
  162. err = notifier_from_errno(err);
  163. break;
  164. case SWITCHDEV_FDB_OFFLOADED:
  165. fdb_info = ptr;
  166. br_fdb_offloaded_set(br, p, fdb_info->addr,
  167. fdb_info->vid, fdb_info->offloaded);
  168. break;
  169. case SWITCHDEV_FDB_FLUSH_TO_BRIDGE:
  170. fdb_info = ptr;
  171. /* Don't delete static entries */
  172. br_fdb_delete_by_port(br, p, fdb_info->vid, 0);
  173. break;
  174. }
  175. out:
  176. return err;
  177. }
  178. static struct notifier_block br_switchdev_notifier = {
  179. .notifier_call = br_switchdev_event,
  180. };
  181. /* called under rtnl_mutex */
  182. static int br_switchdev_blocking_event(struct notifier_block *nb,
  183. unsigned long event, void *ptr)
  184. {
  185. struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
  186. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  187. struct switchdev_notifier_brport_info *brport_info;
  188. const struct switchdev_brport *b;
  189. struct net_bridge_port *p;
  190. int err = NOTIFY_DONE;
  191. p = br_port_get_rtnl(dev);
  192. if (!p)
  193. goto out;
  194. switch (event) {
  195. case SWITCHDEV_BRPORT_OFFLOADED:
  196. brport_info = ptr;
  197. b = &brport_info->brport;
  198. err = br_switchdev_port_offload(p, b->dev, b->ctx,
  199. b->atomic_nb, b->blocking_nb,
  200. b->tx_fwd_offload, extack);
  201. err = notifier_from_errno(err);
  202. break;
  203. case SWITCHDEV_BRPORT_UNOFFLOADED:
  204. brport_info = ptr;
  205. b = &brport_info->brport;
  206. br_switchdev_port_unoffload(p, b->ctx, b->atomic_nb,
  207. b->blocking_nb);
  208. break;
  209. case SWITCHDEV_BRPORT_REPLAY:
  210. brport_info = ptr;
  211. b = &brport_info->brport;
  212. err = br_switchdev_port_replay(p, b->dev, b->ctx, b->atomic_nb,
  213. b->blocking_nb, extack);
  214. err = notifier_from_errno(err);
  215. break;
  216. }
  217. out:
  218. return err;
  219. }
  220. static struct notifier_block br_switchdev_blocking_notifier = {
  221. .notifier_call = br_switchdev_blocking_event,
  222. };
  223. static int
  224. br_toggle_fdb_local_vlan_0(struct net_bridge *br, bool on,
  225. struct netlink_ext_ack *extack)
  226. {
  227. int err;
  228. if (br_opt_get(br, BROPT_FDB_LOCAL_VLAN_0) == on)
  229. return 0;
  230. err = br_fdb_toggle_local_vlan_0(br, on, extack);
  231. if (err)
  232. return err;
  233. br_opt_toggle(br, BROPT_FDB_LOCAL_VLAN_0, on);
  234. return 0;
  235. }
  236. /* br_boolopt_toggle - change user-controlled boolean option
  237. *
  238. * @br: bridge device
  239. * @opt: id of the option to change
  240. * @on: new option value
  241. * @extack: extack for error messages
  242. *
  243. * Changes the value of the respective boolean option to @on taking care of
  244. * any internal option value mapping and configuration.
  245. */
  246. int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
  247. struct netlink_ext_ack *extack)
  248. {
  249. int err = 0;
  250. switch (opt) {
  251. case BR_BOOLOPT_NO_LL_LEARN:
  252. br_opt_toggle(br, BROPT_NO_LL_LEARN, on);
  253. break;
  254. case BR_BOOLOPT_MCAST_VLAN_SNOOPING:
  255. err = br_multicast_toggle_vlan_snooping(br, on, extack);
  256. break;
  257. case BR_BOOLOPT_MST_ENABLE:
  258. err = br_mst_set_enabled(br, on, extack);
  259. break;
  260. case BR_BOOLOPT_MDB_OFFLOAD_FAIL_NOTIFICATION:
  261. br_opt_toggle(br, BROPT_MDB_OFFLOAD_FAIL_NOTIFICATION, on);
  262. break;
  263. case BR_BOOLOPT_FDB_LOCAL_VLAN_0:
  264. err = br_toggle_fdb_local_vlan_0(br, on, extack);
  265. break;
  266. default:
  267. /* shouldn't be called with unsupported options */
  268. WARN_ON(1);
  269. break;
  270. }
  271. return err;
  272. }
  273. int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt)
  274. {
  275. switch (opt) {
  276. case BR_BOOLOPT_NO_LL_LEARN:
  277. return br_opt_get(br, BROPT_NO_LL_LEARN);
  278. case BR_BOOLOPT_MCAST_VLAN_SNOOPING:
  279. return br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED);
  280. case BR_BOOLOPT_MST_ENABLE:
  281. return br_opt_get(br, BROPT_MST_ENABLED);
  282. case BR_BOOLOPT_MDB_OFFLOAD_FAIL_NOTIFICATION:
  283. return br_opt_get(br, BROPT_MDB_OFFLOAD_FAIL_NOTIFICATION);
  284. case BR_BOOLOPT_FDB_LOCAL_VLAN_0:
  285. return br_opt_get(br, BROPT_FDB_LOCAL_VLAN_0);
  286. default:
  287. /* shouldn't be called with unsupported options */
  288. WARN_ON(1);
  289. break;
  290. }
  291. return 0;
  292. }
  293. int br_boolopt_multi_toggle(struct net_bridge *br,
  294. struct br_boolopt_multi *bm,
  295. struct netlink_ext_ack *extack)
  296. {
  297. unsigned long bitmap = bm->optmask;
  298. int err = 0;
  299. int opt_id;
  300. opt_id = find_next_bit(&bitmap, BITS_PER_LONG, BR_BOOLOPT_MAX);
  301. if (opt_id != BITS_PER_LONG) {
  302. NL_SET_ERR_MSG_FMT_MOD(extack, "Unknown boolean option %d",
  303. opt_id);
  304. return -EINVAL;
  305. }
  306. for_each_set_bit(opt_id, &bitmap, BR_BOOLOPT_MAX) {
  307. bool on = !!(bm->optval & BIT(opt_id));
  308. err = br_boolopt_toggle(br, opt_id, on, extack);
  309. if (err) {
  310. br_debug(br, "boolopt multi-toggle error: option: %d current: %d new: %d error: %d\n",
  311. opt_id, br_boolopt_get(br, opt_id), on, err);
  312. break;
  313. }
  314. }
  315. return err;
  316. }
  317. void br_boolopt_multi_get(const struct net_bridge *br,
  318. struct br_boolopt_multi *bm)
  319. {
  320. u32 optval = 0;
  321. int opt_id;
  322. for (opt_id = 0; opt_id < BR_BOOLOPT_MAX; opt_id++)
  323. optval |= (br_boolopt_get(br, opt_id) << opt_id);
  324. bm->optval = optval;
  325. bm->optmask = GENMASK((BR_BOOLOPT_MAX - 1), 0);
  326. }
  327. /* private bridge options, controlled by the kernel */
  328. void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on)
  329. {
  330. bool cur = !!br_opt_get(br, opt);
  331. br_debug(br, "toggle option: %d state: %d -> %d\n",
  332. opt, cur, on);
  333. if (cur == on)
  334. return;
  335. if (on)
  336. set_bit(opt, &br->options);
  337. else
  338. clear_bit(opt, &br->options);
  339. }
  340. static void __net_exit br_net_exit_rtnl(struct net *net,
  341. struct list_head *dev_to_kill)
  342. {
  343. struct net_device *dev;
  344. ASSERT_RTNL_NET(net);
  345. for_each_netdev(net, dev)
  346. if (netif_is_bridge_master(dev))
  347. br_dev_delete(dev, dev_to_kill);
  348. }
  349. static struct pernet_operations br_net_ops = {
  350. .exit_rtnl = br_net_exit_rtnl,
  351. };
  352. static const struct stp_proto br_stp_proto = {
  353. .rcv = br_stp_rcv,
  354. };
  355. static int __init br_init(void)
  356. {
  357. int err;
  358. BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > sizeof_field(struct sk_buff, cb));
  359. err = stp_proto_register(&br_stp_proto);
  360. if (err < 0) {
  361. pr_err("bridge: can't register sap for STP\n");
  362. return err;
  363. }
  364. err = br_fdb_init();
  365. if (err)
  366. goto err_out;
  367. err = register_pernet_subsys(&br_net_ops);
  368. if (err)
  369. goto err_out1;
  370. err = br_nf_core_init();
  371. if (err)
  372. goto err_out2;
  373. err = register_netdevice_notifier(&br_device_notifier);
  374. if (err)
  375. goto err_out3;
  376. err = register_switchdev_notifier(&br_switchdev_notifier);
  377. if (err)
  378. goto err_out4;
  379. err = register_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
  380. if (err)
  381. goto err_out5;
  382. err = br_netlink_init();
  383. if (err)
  384. goto err_out6;
  385. brioctl_set(br_ioctl_stub);
  386. #if IS_ENABLED(CONFIG_ATM_LANE)
  387. br_fdb_test_addr_hook = br_fdb_test_addr;
  388. #endif
  389. #if IS_MODULE(CONFIG_BRIDGE_NETFILTER)
  390. pr_info("bridge: filtering via arp/ip/ip6tables is no longer available "
  391. "by default. Update your scripts to load br_netfilter if you "
  392. "need this.\n");
  393. #endif
  394. return 0;
  395. err_out6:
  396. unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
  397. err_out5:
  398. unregister_switchdev_notifier(&br_switchdev_notifier);
  399. err_out4:
  400. unregister_netdevice_notifier(&br_device_notifier);
  401. err_out3:
  402. br_nf_core_fini();
  403. err_out2:
  404. unregister_pernet_subsys(&br_net_ops);
  405. err_out1:
  406. br_fdb_fini();
  407. err_out:
  408. stp_proto_unregister(&br_stp_proto);
  409. return err;
  410. }
  411. static void __exit br_deinit(void)
  412. {
  413. stp_proto_unregister(&br_stp_proto);
  414. br_netlink_fini();
  415. unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
  416. unregister_switchdev_notifier(&br_switchdev_notifier);
  417. unregister_netdevice_notifier(&br_device_notifier);
  418. brioctl_set(NULL);
  419. unregister_pernet_subsys(&br_net_ops);
  420. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  421. br_nf_core_fini();
  422. #if IS_ENABLED(CONFIG_ATM_LANE)
  423. br_fdb_test_addr_hook = NULL;
  424. #endif
  425. br_fdb_fini();
  426. }
  427. module_init(br_init)
  428. module_exit(br_deinit)
  429. MODULE_LICENSE("GPL");
  430. MODULE_VERSION(BR_VERSION);
  431. MODULE_ALIAS_RTNL_LINK("bridge");
  432. MODULE_DESCRIPTION("Ethernet bridge driver");
  433. MODULE_IMPORT_NS("NETDEV_INTERNAL");