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