Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Generic parts
0004  *  Linux ethernet bridge
0005  *
0006  *  Authors:
0007  *  Lennert Buytenhek       <buytenh@gnu.org>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/netdevice.h>
0013 #include <linux/etherdevice.h>
0014 #include <linux/init.h>
0015 #include <linux/llc.h>
0016 #include <net/llc.h>
0017 #include <net/stp.h>
0018 #include <net/switchdev.h>
0019 
0020 #include "br_private.h"
0021 
0022 /*
0023  * Handle changes in state of network devices enslaved to a bridge.
0024  *
0025  * Note: don't care about up/down if bridge itself is down, because
0026  *     port state is checked when bridge is brought up.
0027  */
0028 static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
0029 {
0030     struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
0031     struct netdev_notifier_pre_changeaddr_info *prechaddr_info;
0032     struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0033     struct net_bridge_port *p;
0034     struct net_bridge *br;
0035     bool notified = false;
0036     bool changed_addr;
0037     int err;
0038 
0039     if (netif_is_bridge_master(dev)) {
0040         err = br_vlan_bridge_event(dev, event, ptr);
0041         if (err)
0042             return notifier_from_errno(err);
0043 
0044         if (event == NETDEV_REGISTER) {
0045             /* register of bridge completed, add sysfs entries */
0046             err = br_sysfs_addbr(dev);
0047             if (err)
0048                 return notifier_from_errno(err);
0049 
0050             return NOTIFY_DONE;
0051         }
0052     }
0053 
0054     /* not a port of a bridge */
0055     p = br_port_get_rtnl(dev);
0056     if (!p)
0057         return NOTIFY_DONE;
0058 
0059     br = p->br;
0060 
0061     switch (event) {
0062     case NETDEV_CHANGEMTU:
0063         br_mtu_auto_adjust(br);
0064         break;
0065 
0066     case NETDEV_PRE_CHANGEADDR:
0067         if (br->dev->addr_assign_type == NET_ADDR_SET)
0068             break;
0069         prechaddr_info = ptr;
0070         err = dev_pre_changeaddr_notify(br->dev,
0071                         prechaddr_info->dev_addr,
0072                         extack);
0073         if (err)
0074             return notifier_from_errno(err);
0075         break;
0076 
0077     case NETDEV_CHANGEADDR:
0078         spin_lock_bh(&br->lock);
0079         br_fdb_changeaddr(p, dev->dev_addr);
0080         changed_addr = br_stp_recalculate_bridge_id(br);
0081         spin_unlock_bh(&br->lock);
0082 
0083         if (changed_addr)
0084             call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
0085 
0086         break;
0087 
0088     case NETDEV_CHANGE:
0089         br_port_carrier_check(p, &notified);
0090         break;
0091 
0092     case NETDEV_FEAT_CHANGE:
0093         netdev_update_features(br->dev);
0094         break;
0095 
0096     case NETDEV_DOWN:
0097         spin_lock_bh(&br->lock);
0098         if (br->dev->flags & IFF_UP) {
0099             br_stp_disable_port(p);
0100             notified = true;
0101         }
0102         spin_unlock_bh(&br->lock);
0103         break;
0104 
0105     case NETDEV_UP:
0106         if (netif_running(br->dev) && netif_oper_up(dev)) {
0107             spin_lock_bh(&br->lock);
0108             br_stp_enable_port(p);
0109             notified = true;
0110             spin_unlock_bh(&br->lock);
0111         }
0112         break;
0113 
0114     case NETDEV_UNREGISTER:
0115         br_del_if(br, dev);
0116         break;
0117 
0118     case NETDEV_CHANGENAME:
0119         err = br_sysfs_renameif(p);
0120         if (err)
0121             return notifier_from_errno(err);
0122         break;
0123 
0124     case NETDEV_PRE_TYPE_CHANGE:
0125         /* Forbid underlying device to change its type. */
0126         return NOTIFY_BAD;
0127 
0128     case NETDEV_RESEND_IGMP:
0129         /* Propagate to master device */
0130         call_netdevice_notifiers(event, br->dev);
0131         break;
0132     }
0133 
0134     if (event != NETDEV_UNREGISTER)
0135         br_vlan_port_event(p, event);
0136 
0137     /* Events that may cause spanning tree to refresh */
0138     if (!notified && (event == NETDEV_CHANGEADDR || event == NETDEV_UP ||
0139               event == NETDEV_CHANGE || event == NETDEV_DOWN))
0140         br_ifinfo_notify(RTM_NEWLINK, NULL, p);
0141 
0142     return NOTIFY_DONE;
0143 }
0144 
0145 static struct notifier_block br_device_notifier = {
0146     .notifier_call = br_device_event
0147 };
0148 
0149 /* called with RTNL or RCU */
0150 static int br_switchdev_event(struct notifier_block *unused,
0151                   unsigned long event, void *ptr)
0152 {
0153     struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
0154     struct net_bridge_port *p;
0155     struct net_bridge *br;
0156     struct switchdev_notifier_fdb_info *fdb_info;
0157     int err = NOTIFY_DONE;
0158 
0159     p = br_port_get_rtnl_rcu(dev);
0160     if (!p)
0161         goto out;
0162 
0163     br = p->br;
0164 
0165     switch (event) {
0166     case SWITCHDEV_FDB_ADD_TO_BRIDGE:
0167         fdb_info = ptr;
0168         err = br_fdb_external_learn_add(br, p, fdb_info->addr,
0169                         fdb_info->vid, false);
0170         if (err) {
0171             err = notifier_from_errno(err);
0172             break;
0173         }
0174         br_fdb_offloaded_set(br, p, fdb_info->addr,
0175                      fdb_info->vid, true);
0176         break;
0177     case SWITCHDEV_FDB_DEL_TO_BRIDGE:
0178         fdb_info = ptr;
0179         err = br_fdb_external_learn_del(br, p, fdb_info->addr,
0180                         fdb_info->vid, false);
0181         if (err)
0182             err = notifier_from_errno(err);
0183         break;
0184     case SWITCHDEV_FDB_OFFLOADED:
0185         fdb_info = ptr;
0186         br_fdb_offloaded_set(br, p, fdb_info->addr,
0187                      fdb_info->vid, fdb_info->offloaded);
0188         break;
0189     case SWITCHDEV_FDB_FLUSH_TO_BRIDGE:
0190         fdb_info = ptr;
0191         /* Don't delete static entries */
0192         br_fdb_delete_by_port(br, p, fdb_info->vid, 0);
0193         break;
0194     }
0195 
0196 out:
0197     return err;
0198 }
0199 
0200 static struct notifier_block br_switchdev_notifier = {
0201     .notifier_call = br_switchdev_event,
0202 };
0203 
0204 /* called under rtnl_mutex */
0205 static int br_switchdev_blocking_event(struct notifier_block *nb,
0206                        unsigned long event, void *ptr)
0207 {
0208     struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
0209     struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
0210     struct switchdev_notifier_brport_info *brport_info;
0211     const struct switchdev_brport *b;
0212     struct net_bridge_port *p;
0213     int err = NOTIFY_DONE;
0214 
0215     p = br_port_get_rtnl(dev);
0216     if (!p)
0217         goto out;
0218 
0219     switch (event) {
0220     case SWITCHDEV_BRPORT_OFFLOADED:
0221         brport_info = ptr;
0222         b = &brport_info->brport;
0223 
0224         err = br_switchdev_port_offload(p, b->dev, b->ctx,
0225                         b->atomic_nb, b->blocking_nb,
0226                         b->tx_fwd_offload, extack);
0227         err = notifier_from_errno(err);
0228         break;
0229     case SWITCHDEV_BRPORT_UNOFFLOADED:
0230         brport_info = ptr;
0231         b = &brport_info->brport;
0232 
0233         br_switchdev_port_unoffload(p, b->ctx, b->atomic_nb,
0234                         b->blocking_nb);
0235         break;
0236     }
0237 
0238 out:
0239     return err;
0240 }
0241 
0242 static struct notifier_block br_switchdev_blocking_notifier = {
0243     .notifier_call = br_switchdev_blocking_event,
0244 };
0245 
0246 /* br_boolopt_toggle - change user-controlled boolean option
0247  *
0248  * @br: bridge device
0249  * @opt: id of the option to change
0250  * @on: new option value
0251  * @extack: extack for error messages
0252  *
0253  * Changes the value of the respective boolean option to @on taking care of
0254  * any internal option value mapping and configuration.
0255  */
0256 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
0257               struct netlink_ext_ack *extack)
0258 {
0259     int err = 0;
0260 
0261     switch (opt) {
0262     case BR_BOOLOPT_NO_LL_LEARN:
0263         br_opt_toggle(br, BROPT_NO_LL_LEARN, on);
0264         break;
0265     case BR_BOOLOPT_MCAST_VLAN_SNOOPING:
0266         err = br_multicast_toggle_vlan_snooping(br, on, extack);
0267         break;
0268     case BR_BOOLOPT_MST_ENABLE:
0269         err = br_mst_set_enabled(br, on, extack);
0270         break;
0271     default:
0272         /* shouldn't be called with unsupported options */
0273         WARN_ON(1);
0274         break;
0275     }
0276 
0277     return err;
0278 }
0279 
0280 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt)
0281 {
0282     switch (opt) {
0283     case BR_BOOLOPT_NO_LL_LEARN:
0284         return br_opt_get(br, BROPT_NO_LL_LEARN);
0285     case BR_BOOLOPT_MCAST_VLAN_SNOOPING:
0286         return br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED);
0287     case BR_BOOLOPT_MST_ENABLE:
0288         return br_opt_get(br, BROPT_MST_ENABLED);
0289     default:
0290         /* shouldn't be called with unsupported options */
0291         WARN_ON(1);
0292         break;
0293     }
0294 
0295     return 0;
0296 }
0297 
0298 int br_boolopt_multi_toggle(struct net_bridge *br,
0299                 struct br_boolopt_multi *bm,
0300                 struct netlink_ext_ack *extack)
0301 {
0302     unsigned long bitmap = bm->optmask;
0303     int err = 0;
0304     int opt_id;
0305 
0306     for_each_set_bit(opt_id, &bitmap, BR_BOOLOPT_MAX) {
0307         bool on = !!(bm->optval & BIT(opt_id));
0308 
0309         err = br_boolopt_toggle(br, opt_id, on, extack);
0310         if (err) {
0311             br_debug(br, "boolopt multi-toggle error: option: %d current: %d new: %d error: %d\n",
0312                  opt_id, br_boolopt_get(br, opt_id), on, err);
0313             break;
0314         }
0315     }
0316 
0317     return err;
0318 }
0319 
0320 void br_boolopt_multi_get(const struct net_bridge *br,
0321               struct br_boolopt_multi *bm)
0322 {
0323     u32 optval = 0;
0324     int opt_id;
0325 
0326     for (opt_id = 0; opt_id < BR_BOOLOPT_MAX; opt_id++)
0327         optval |= (br_boolopt_get(br, opt_id) << opt_id);
0328 
0329     bm->optval = optval;
0330     bm->optmask = GENMASK((BR_BOOLOPT_MAX - 1), 0);
0331 }
0332 
0333 /* private bridge options, controlled by the kernel */
0334 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on)
0335 {
0336     bool cur = !!br_opt_get(br, opt);
0337 
0338     br_debug(br, "toggle option: %d state: %d -> %d\n",
0339          opt, cur, on);
0340 
0341     if (cur == on)
0342         return;
0343 
0344     if (on)
0345         set_bit(opt, &br->options);
0346     else
0347         clear_bit(opt, &br->options);
0348 }
0349 
0350 static void __net_exit br_net_exit_batch(struct list_head *net_list)
0351 {
0352     struct net_device *dev;
0353     struct net *net;
0354     LIST_HEAD(list);
0355 
0356     rtnl_lock();
0357 
0358     list_for_each_entry(net, net_list, exit_list)
0359         for_each_netdev(net, dev)
0360             if (netif_is_bridge_master(dev))
0361                 br_dev_delete(dev, &list);
0362 
0363     unregister_netdevice_many(&list);
0364 
0365     rtnl_unlock();
0366 }
0367 
0368 static struct pernet_operations br_net_ops = {
0369     .exit_batch = br_net_exit_batch,
0370 };
0371 
0372 static const struct stp_proto br_stp_proto = {
0373     .rcv    = br_stp_rcv,
0374 };
0375 
0376 static int __init br_init(void)
0377 {
0378     int err;
0379 
0380     BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > sizeof_field(struct sk_buff, cb));
0381 
0382     err = stp_proto_register(&br_stp_proto);
0383     if (err < 0) {
0384         pr_err("bridge: can't register sap for STP\n");
0385         return err;
0386     }
0387 
0388     err = br_fdb_init();
0389     if (err)
0390         goto err_out;
0391 
0392     err = register_pernet_subsys(&br_net_ops);
0393     if (err)
0394         goto err_out1;
0395 
0396     err = br_nf_core_init();
0397     if (err)
0398         goto err_out2;
0399 
0400     err = register_netdevice_notifier(&br_device_notifier);
0401     if (err)
0402         goto err_out3;
0403 
0404     err = register_switchdev_notifier(&br_switchdev_notifier);
0405     if (err)
0406         goto err_out4;
0407 
0408     err = register_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
0409     if (err)
0410         goto err_out5;
0411 
0412     err = br_netlink_init();
0413     if (err)
0414         goto err_out6;
0415 
0416     brioctl_set(br_ioctl_stub);
0417 
0418 #if IS_ENABLED(CONFIG_ATM_LANE)
0419     br_fdb_test_addr_hook = br_fdb_test_addr;
0420 #endif
0421 
0422 #if IS_MODULE(CONFIG_BRIDGE_NETFILTER)
0423     pr_info("bridge: filtering via arp/ip/ip6tables is no longer available "
0424         "by default. Update your scripts to load br_netfilter if you "
0425         "need this.\n");
0426 #endif
0427 
0428     return 0;
0429 
0430 err_out6:
0431     unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
0432 err_out5:
0433     unregister_switchdev_notifier(&br_switchdev_notifier);
0434 err_out4:
0435     unregister_netdevice_notifier(&br_device_notifier);
0436 err_out3:
0437     br_nf_core_fini();
0438 err_out2:
0439     unregister_pernet_subsys(&br_net_ops);
0440 err_out1:
0441     br_fdb_fini();
0442 err_out:
0443     stp_proto_unregister(&br_stp_proto);
0444     return err;
0445 }
0446 
0447 static void __exit br_deinit(void)
0448 {
0449     stp_proto_unregister(&br_stp_proto);
0450     br_netlink_fini();
0451     unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
0452     unregister_switchdev_notifier(&br_switchdev_notifier);
0453     unregister_netdevice_notifier(&br_device_notifier);
0454     brioctl_set(NULL);
0455     unregister_pernet_subsys(&br_net_ops);
0456 
0457     rcu_barrier(); /* Wait for completion of call_rcu()'s */
0458 
0459     br_nf_core_fini();
0460 #if IS_ENABLED(CONFIG_ATM_LANE)
0461     br_fdb_test_addr_hook = NULL;
0462 #endif
0463     br_fdb_fini();
0464 }
0465 
0466 module_init(br_init)
0467 module_exit(br_deinit)
0468 MODULE_LICENSE("GPL");
0469 MODULE_VERSION(BR_VERSION);
0470 MODULE_ALIAS_RTNL_LINK("bridge");