^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; timer-related 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/times.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "br_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "br_private_stp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* called under bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int br_is_designated_for_some_port(const struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (p->state != BR_STATE_DISABLED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) !memcmp(&p->designated_bridge, &br->bridge_id, 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return 0;
^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) static void br_hello_timer_expired(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct net_bridge *br = from_timer(br, t, hello_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) br_debug(br, "hello timer expired\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) spin_lock(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (br->dev->flags & IFF_UP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) br_config_bpdu_generation(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (br->stp_enabled == BR_KERNEL_STP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) mod_timer(&br->hello_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) round_jiffies(jiffies + br->hello_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) spin_unlock(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void br_message_age_timer_expired(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct net_bridge_port *p = from_timer(p, t, message_age_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct net_bridge *br = p->br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const bridge_id *id = &p->designated_bridge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int was_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (p->state == BR_STATE_DISABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) (unsigned int) p->port_no, p->dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) id->prio[0], id->prio[1], &id->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * According to the spec, the message age timer cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * running when we are the root bridge. So.. this was_root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * check is redundant. I'm leaving it in for now, though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) spin_lock(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (p->state == BR_STATE_DISABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) was_root = br_is_root_bridge(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) br_become_designated_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) br_configuration_update(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) br_port_state_selection(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (br_is_root_bridge(br) && !was_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) br_become_root_bridge(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) spin_unlock(&br->lock);
^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) static void br_forward_delay_timer_expired(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct net_bridge_port *p = from_timer(p, t, forward_delay_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct net_bridge *br = p->br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) br_debug(br, "port %u(%s) forward delay timer\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) (unsigned int) p->port_no, p->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) spin_lock(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (p->state == BR_STATE_LISTENING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) br_set_state(p, BR_STATE_LEARNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) mod_timer(&p->forward_delay_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) jiffies + br->forward_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } else if (p->state == BR_STATE_LEARNING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) br_set_state(p, BR_STATE_FORWARDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (br_is_designated_for_some_port(br))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) br_topology_change_detection(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) netif_carrier_on(br->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) br_ifinfo_notify(RTM_NEWLINK, NULL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) spin_unlock(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static void br_tcn_timer_expired(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct net_bridge *br = from_timer(br, t, tcn_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) br_debug(br, "tcn timer expired\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) spin_lock(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!br_is_root_bridge(br) && (br->dev->flags & IFF_UP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) br_transmit_tcn(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) spin_unlock(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static void br_topology_change_timer_expired(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct net_bridge *br = from_timer(br, t, topology_change_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) br_debug(br, "topo change timer expired\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) spin_lock(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) br->topology_change_detected = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) __br_set_topology_change(br, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) spin_unlock(&br->lock);
^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 void br_hold_timer_expired(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct net_bridge_port *p = from_timer(p, t, hold_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) br_debug(p->br, "port %u(%s) hold timer expired\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) (unsigned int) p->port_no, p->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) spin_lock(&p->br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (p->config_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) br_transmit_config(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) spin_unlock(&p->br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void br_stp_timer_init(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) timer_setup(&br->hello_timer, br_hello_timer_expired, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) timer_setup(&br->tcn_timer, br_tcn_timer_expired, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) timer_setup(&br->topology_change_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) br_topology_change_timer_expired, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) void br_stp_port_timer_init(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) timer_setup(&p->message_age_timer, br_message_age_timer_expired, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) timer_setup(&p->forward_delay_timer, br_forward_delay_timer_expired, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) timer_setup(&p->hold_timer, br_hold_timer_expired, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* Report ticks left (in USER_HZ) used for API */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long br_timer_value(const struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return timer_pending(timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }