Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Spanning tree protocol; interface code |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * |
| 8 | * $Id: br_stp_if.c,v 1.4 2001/04/14 21:14:39 davem Exp $ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/smp_lock.h> |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 18 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include "br_private.h" |
| 21 | #include "br_private_stp.h" |
| 22 | |
| 23 | |
| 24 | /* Port id is composed of priority and port number. |
| 25 | * NB: least significant bits of priority are dropped to |
| 26 | * make room for more ports. |
| 27 | */ |
| 28 | static inline port_id br_make_port_id(__u8 priority, __u16 port_no) |
| 29 | { |
| 30 | return ((u16)priority << BR_PORT_BITS) |
| 31 | | (port_no & ((1<<BR_PORT_BITS)-1)); |
| 32 | } |
| 33 | |
| 34 | /* called under bridge lock */ |
| 35 | void br_init_port(struct net_bridge_port *p) |
| 36 | { |
| 37 | p->port_id = br_make_port_id(p->priority, p->port_no); |
| 38 | br_become_designated_port(p); |
| 39 | p->state = BR_STATE_BLOCKING; |
| 40 | p->topology_change_ack = 0; |
| 41 | p->config_pending = 0; |
| 42 | |
| 43 | br_stp_port_timer_init(p); |
| 44 | } |
| 45 | |
| 46 | /* called under bridge lock */ |
| 47 | void br_stp_enable_bridge(struct net_bridge *br) |
| 48 | { |
| 49 | struct net_bridge_port *p; |
| 50 | |
| 51 | spin_lock_bh(&br->lock); |
| 52 | mod_timer(&br->hello_timer, jiffies + br->hello_time); |
| 53 | mod_timer(&br->gc_timer, jiffies + HZ/10); |
| 54 | |
| 55 | br_config_bpdu_generation(br); |
| 56 | |
| 57 | list_for_each_entry(p, &br->port_list, list) { |
| 58 | if ((p->dev->flags & IFF_UP) && netif_carrier_ok(p->dev)) |
| 59 | br_stp_enable_port(p); |
| 60 | |
| 61 | } |
| 62 | spin_unlock_bh(&br->lock); |
| 63 | } |
| 64 | |
| 65 | /* NO locks held */ |
| 66 | void br_stp_disable_bridge(struct net_bridge *br) |
| 67 | { |
| 68 | struct net_bridge_port *p; |
| 69 | |
| 70 | spin_lock(&br->lock); |
| 71 | list_for_each_entry(p, &br->port_list, list) { |
| 72 | if (p->state != BR_STATE_DISABLED) |
| 73 | br_stp_disable_port(p); |
| 74 | |
| 75 | } |
| 76 | |
| 77 | br->topology_change = 0; |
| 78 | br->topology_change_detected = 0; |
| 79 | spin_unlock(&br->lock); |
| 80 | |
| 81 | del_timer_sync(&br->hello_timer); |
| 82 | del_timer_sync(&br->topology_change_timer); |
| 83 | del_timer_sync(&br->tcn_timer); |
| 84 | del_timer_sync(&br->gc_timer); |
| 85 | } |
| 86 | |
| 87 | /* called under bridge lock */ |
| 88 | void br_stp_enable_port(struct net_bridge_port *p) |
| 89 | { |
| 90 | br_init_port(p); |
| 91 | br_port_state_selection(p->br); |
| 92 | } |
| 93 | |
| 94 | /* called under bridge lock */ |
| 95 | void br_stp_disable_port(struct net_bridge_port *p) |
| 96 | { |
| 97 | struct net_bridge *br; |
| 98 | int wasroot; |
| 99 | |
| 100 | br = p->br; |
| 101 | printk(KERN_INFO "%s: port %i(%s) entering %s state\n", |
| 102 | br->dev->name, p->port_no, p->dev->name, "disabled"); |
| 103 | |
| 104 | wasroot = br_is_root_bridge(br); |
| 105 | br_become_designated_port(p); |
| 106 | p->state = BR_STATE_DISABLED; |
| 107 | p->topology_change_ack = 0; |
| 108 | p->config_pending = 0; |
| 109 | |
| 110 | del_timer(&p->message_age_timer); |
| 111 | del_timer(&p->forward_delay_timer); |
| 112 | del_timer(&p->hold_timer); |
| 113 | |
| 114 | br_configuration_update(br); |
| 115 | |
| 116 | br_port_state_selection(br); |
| 117 | |
| 118 | if (br_is_root_bridge(br) && !wasroot) |
| 119 | br_become_root_bridge(br); |
| 120 | } |
| 121 | |
| 122 | /* called under bridge lock */ |
| 123 | static void br_stp_change_bridge_id(struct net_bridge *br, |
| 124 | const unsigned char *addr) |
| 125 | { |
| 126 | unsigned char oldaddr[6]; |
| 127 | struct net_bridge_port *p; |
| 128 | int wasroot; |
| 129 | |
| 130 | wasroot = br_is_root_bridge(br); |
| 131 | |
| 132 | memcpy(oldaddr, br->bridge_id.addr, ETH_ALEN); |
| 133 | memcpy(br->bridge_id.addr, addr, ETH_ALEN); |
| 134 | memcpy(br->dev->dev_addr, addr, ETH_ALEN); |
| 135 | |
| 136 | list_for_each_entry(p, &br->port_list, list) { |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 137 | if (!compare_ether_addr(p->designated_bridge.addr, oldaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | memcpy(p->designated_bridge.addr, addr, ETH_ALEN); |
| 139 | |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 140 | if (!compare_ether_addr(p->designated_root.addr, oldaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | memcpy(p->designated_root.addr, addr, ETH_ALEN); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | br_configuration_update(br); |
| 146 | br_port_state_selection(br); |
| 147 | if (br_is_root_bridge(br) && !wasroot) |
| 148 | br_become_root_bridge(br); |
| 149 | } |
| 150 | |
| 151 | static const unsigned char br_mac_zero[6]; |
| 152 | |
| 153 | /* called under bridge lock */ |
| 154 | void br_stp_recalculate_bridge_id(struct net_bridge *br) |
| 155 | { |
| 156 | const unsigned char *addr = br_mac_zero; |
| 157 | struct net_bridge_port *p; |
| 158 | |
| 159 | list_for_each_entry(p, &br->port_list, list) { |
| 160 | if (addr == br_mac_zero || |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 161 | compare_ether_addr(p->dev->dev_addr, addr) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | addr = p->dev->dev_addr; |
| 163 | |
| 164 | } |
| 165 | |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 166 | if (compare_ether_addr(br->bridge_id.addr, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | br_stp_change_bridge_id(br, addr); |
| 168 | } |
| 169 | |
| 170 | /* called under bridge lock */ |
| 171 | void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio) |
| 172 | { |
| 173 | struct net_bridge_port *p; |
| 174 | int wasroot; |
| 175 | |
| 176 | wasroot = br_is_root_bridge(br); |
| 177 | |
| 178 | list_for_each_entry(p, &br->port_list, list) { |
| 179 | if (p->state != BR_STATE_DISABLED && |
| 180 | br_is_designated_port(p)) { |
| 181 | p->designated_bridge.prio[0] = (newprio >> 8) & 0xFF; |
| 182 | p->designated_bridge.prio[1] = newprio & 0xFF; |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | br->bridge_id.prio[0] = (newprio >> 8) & 0xFF; |
| 188 | br->bridge_id.prio[1] = newprio & 0xFF; |
| 189 | br_configuration_update(br); |
| 190 | br_port_state_selection(br); |
| 191 | if (br_is_root_bridge(br) && !wasroot) |
| 192 | br_become_root_bridge(br); |
| 193 | } |
| 194 | |
| 195 | /* called under bridge lock */ |
| 196 | void br_stp_set_port_priority(struct net_bridge_port *p, u8 newprio) |
| 197 | { |
| 198 | port_id new_port_id = br_make_port_id(newprio, p->port_no); |
| 199 | |
| 200 | if (br_is_designated_port(p)) |
| 201 | p->designated_port = new_port_id; |
| 202 | |
| 203 | p->port_id = new_port_id; |
| 204 | p->priority = newprio; |
| 205 | if (!memcmp(&p->br->bridge_id, &p->designated_bridge, 8) && |
| 206 | p->port_id < p->designated_port) { |
| 207 | br_become_designated_port(p); |
| 208 | br_port_state_selection(p->br); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* called under bridge lock */ |
| 213 | void br_stp_set_path_cost(struct net_bridge_port *p, u32 path_cost) |
| 214 | { |
| 215 | p->path_cost = path_cost; |
| 216 | br_configuration_update(p->br); |
| 217 | br_port_state_selection(p->br); |
| 218 | } |
| 219 | |
| 220 | ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id) |
| 221 | { |
| 222 | return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n", |
| 223 | id->prio[0], id->prio[1], |
| 224 | id->addr[0], id->addr[1], id->addr[2], |
| 225 | id->addr[3], id->addr[4], id->addr[5]); |
| 226 | } |