^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Spanning tree protocol; interface code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Linux ethernet bridge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Lennert Buytenhek <buytenh@gnu.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kmod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/switchdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "br_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "br_private_stp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Port id is composed of priority and port number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * NB: some bits of priority are dropped to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * make room for more ports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static inline port_id br_make_port_id(__u8 priority, __u16 port_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return ((u16)priority << BR_PORT_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) | (port_no & ((1<<BR_PORT_BITS)-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define BR_MAX_PORT_PRIORITY ((u16)~0 >> BR_PORT_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* called under bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void br_init_port(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) p->port_id = br_make_port_id(p->priority, p->port_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) br_become_designated_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) br_set_state(p, BR_STATE_BLOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) p->topology_change_ack = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) p->config_pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) err = __set_ageing_time(p->dev, p->br->ageing_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) netdev_err(p->dev, "failed to offload ageing time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* NO locks held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) void br_stp_enable_bridge(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (br->stp_enabled == BR_KERNEL_STP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) mod_timer(&br->hello_timer, jiffies + br->hello_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) mod_delayed_work(system_long_wq, &br->gc_work, HZ / 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) br_config_bpdu_generation(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (netif_running(p->dev) && netif_oper_up(p->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) br_stp_enable_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* NO locks held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) void br_stp_disable_bridge(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (p->state != BR_STATE_DISABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) br_stp_disable_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) __br_set_topology_change(br, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) br->topology_change_detected = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) del_timer_sync(&br->hello_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) del_timer_sync(&br->topology_change_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) del_timer_sync(&br->tcn_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) cancel_delayed_work_sync(&br->gc_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* called under bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) void br_stp_enable_port(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) br_init_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) br_port_state_selection(p->br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) br_ifinfo_notify(RTM_NEWLINK, NULL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* called under bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) void br_stp_disable_port(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct net_bridge *br = p->br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int wasroot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) wasroot = br_is_root_bridge(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) br_become_designated_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) br_set_state(p, BR_STATE_DISABLED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) p->topology_change_ack = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) p->config_pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) br_ifinfo_notify(RTM_NEWLINK, NULL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) del_timer(&p->message_age_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) del_timer(&p->forward_delay_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) del_timer(&p->hold_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!rcu_access_pointer(p->backup_port))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) br_fdb_delete_by_port(br, p, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) br_multicast_disable_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) br_configuration_update(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) br_port_state_selection(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (br_is_root_bridge(br) && !wasroot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) br_become_root_bridge(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int br_stp_call_user(struct net_bridge *br, char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) char *argv[] = { BR_STP_PROG, br->dev->name, arg, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) char *envp[] = { NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* call userspace STP and report program errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) rc = call_usermodehelper(BR_STP_PROG, argv, envp, UMH_WAIT_PROC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (rc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (rc & 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) br_debug(br, BR_STP_PROG " received signal %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) rc & 0x7f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) br_debug(br, BR_STP_PROG " exited with code %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) (rc >> 8) & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static void br_stp_start(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (net_eq(dev_net(br->dev), &init_net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) err = br_stp_call_user(br, "start");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (err && err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) br_err(br, "failed to start userspace STP (%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (br->bridge_forward_delay < BR_MIN_FORWARD_DELAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) __br_set_forward_delay(br, BR_MIN_FORWARD_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) else if (br->bridge_forward_delay > BR_MAX_FORWARD_DELAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) __br_set_forward_delay(br, BR_MAX_FORWARD_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) br->stp_enabled = BR_USER_STP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) br_debug(br, "userspace STP started\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) br->stp_enabled = BR_KERNEL_STP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) br_debug(br, "using kernel STP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* To start timers on any ports left in blocking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (br->dev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) mod_timer(&br->hello_timer, jiffies + br->hello_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) br_port_state_selection(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static void br_stp_stop(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (br->stp_enabled == BR_USER_STP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) err = br_stp_call_user(br, "stop");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) br_err(br, "failed to stop userspace STP (%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* To start timers on any ports left in blocking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) br_port_state_selection(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) br->stp_enabled = BR_NO_STP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (br_mrp_enabled(br)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) NL_SET_ERR_MSG_MOD(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) "STP can't be enabled if MRP is already enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (br->stp_enabled == BR_NO_STP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) br_stp_start(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (br->stp_enabled != BR_NO_STP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) br_stp_stop(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* called under bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* should be aligned on 2 bytes for ether_addr_equal() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) unsigned short oldaddr_aligned[ETH_ALEN >> 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) unsigned char *oldaddr = (unsigned char *)oldaddr_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int wasroot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) wasroot = br_is_root_bridge(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) br_fdb_change_mac_address(br, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) memcpy(oldaddr, br->bridge_id.addr, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) memcpy(br->bridge_id.addr, addr, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) memcpy(br->dev->dev_addr, addr, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (ether_addr_equal(p->designated_bridge.addr, oldaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) memcpy(p->designated_bridge.addr, addr, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (ether_addr_equal(p->designated_root.addr, oldaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) memcpy(p->designated_root.addr, addr, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) br_configuration_update(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) br_port_state_selection(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (br_is_root_bridge(br) && !wasroot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) br_become_root_bridge(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* should be aligned on 2 bytes for ether_addr_equal() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static const unsigned short br_mac_zero_aligned[ETH_ALEN >> 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* called under bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) bool br_stp_recalculate_bridge_id(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) const unsigned char *br_mac_zero =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) (const unsigned char *)br_mac_zero_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) const unsigned char *addr = br_mac_zero;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* user has chosen a value so keep it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (br->dev->addr_assign_type == NET_ADDR_SET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (addr == br_mac_zero ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) memcmp(p->dev->dev_addr, addr, ETH_ALEN) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) addr = p->dev->dev_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (ether_addr_equal(br->bridge_id.addr, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return false; /* no change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) br_stp_change_bridge_id(br, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* Acquires and releases bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int wasroot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) wasroot = br_is_root_bridge(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (p->state != BR_STATE_DISABLED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) br_is_designated_port(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) p->designated_bridge.prio[0] = (newprio >> 8) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) p->designated_bridge.prio[1] = newprio & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) br->bridge_id.prio[0] = (newprio >> 8) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) br->bridge_id.prio[1] = newprio & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) br_configuration_update(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) br_port_state_selection(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (br_is_root_bridge(br) && !wasroot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) br_become_root_bridge(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* called under bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) port_id new_port_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (newprio > BR_MAX_PORT_PRIORITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) new_port_id = br_make_port_id(newprio, p->port_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (br_is_designated_port(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) p->designated_port = new_port_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) p->port_id = new_port_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) p->priority = newprio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (!memcmp(&p->br->bridge_id, &p->designated_bridge, 8) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) p->port_id < p->designated_port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) br_become_designated_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) br_port_state_selection(p->br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /* called under bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (path_cost < BR_MIN_PATH_COST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) path_cost > BR_MAX_PATH_COST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) p->flags |= BR_ADMIN_COST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) p->path_cost = path_cost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) br_configuration_update(p->br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) br_port_state_selection(p->br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) id->prio[0], id->prio[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) id->addr[0], id->addr[1], id->addr[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) id->addr[3], id->addr[4], id->addr[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }