blob: cc047f7fb6efc9c4deb42d6aab17f48a778d0474 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hemminger6ede2462005-10-25 15:04:59 -070018#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
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 */
28static 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 */
35void 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 */
47void 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 */
66void 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 */
88void 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 */
95void 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 */
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800123void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 unsigned char oldaddr[6];
126 struct net_bridge_port *p;
127 int wasroot;
128
129 wasroot = br_is_root_bridge(br);
130
131 memcpy(oldaddr, br->bridge_id.addr, ETH_ALEN);
132 memcpy(br->bridge_id.addr, addr, ETH_ALEN);
133 memcpy(br->dev->dev_addr, addr, ETH_ALEN);
134
135 list_for_each_entry(p, &br->port_list, list) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700136 if (!compare_ether_addr(p->designated_bridge.addr, oldaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 memcpy(p->designated_bridge.addr, addr, ETH_ALEN);
138
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700139 if (!compare_ether_addr(p->designated_root.addr, oldaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 memcpy(p->designated_root.addr, addr, ETH_ALEN);
141
142 }
143
144 br_configuration_update(br);
145 br_port_state_selection(br);
146 if (br_is_root_bridge(br) && !wasroot)
147 br_become_root_bridge(br);
148}
149
150static const unsigned char br_mac_zero[6];
151
152/* called under bridge lock */
153void br_stp_recalculate_bridge_id(struct net_bridge *br)
154{
155 const unsigned char *addr = br_mac_zero;
156 struct net_bridge_port *p;
157
158 list_for_each_entry(p, &br->port_list, list) {
159 if (addr == br_mac_zero ||
Stephen Hemminger554c9a82006-01-03 14:35:54 -0800160 memcmp(p->dev->dev_addr, addr, ETH_ALEN) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 addr = p->dev->dev_addr;
162
163 }
164
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700165 if (compare_ether_addr(br->bridge_id.addr, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 br_stp_change_bridge_id(br, addr);
167}
168
169/* called under bridge lock */
170void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio)
171{
172 struct net_bridge_port *p;
173 int wasroot;
174
175 wasroot = br_is_root_bridge(br);
176
177 list_for_each_entry(p, &br->port_list, list) {
178 if (p->state != BR_STATE_DISABLED &&
179 br_is_designated_port(p)) {
180 p->designated_bridge.prio[0] = (newprio >> 8) & 0xFF;
181 p->designated_bridge.prio[1] = newprio & 0xFF;
182 }
183
184 }
185
186 br->bridge_id.prio[0] = (newprio >> 8) & 0xFF;
187 br->bridge_id.prio[1] = newprio & 0xFF;
188 br_configuration_update(br);
189 br_port_state_selection(br);
190 if (br_is_root_bridge(br) && !wasroot)
191 br_become_root_bridge(br);
192}
193
194/* called under bridge lock */
195void br_stp_set_port_priority(struct net_bridge_port *p, u8 newprio)
196{
197 port_id new_port_id = br_make_port_id(newprio, p->port_no);
198
199 if (br_is_designated_port(p))
200 p->designated_port = new_port_id;
201
202 p->port_id = new_port_id;
203 p->priority = newprio;
204 if (!memcmp(&p->br->bridge_id, &p->designated_bridge, 8) &&
205 p->port_id < p->designated_port) {
206 br_become_designated_port(p);
207 br_port_state_selection(p->br);
208 }
209}
210
211/* called under bridge lock */
212void br_stp_set_path_cost(struct net_bridge_port *p, u32 path_cost)
213{
214 p->path_cost = path_cost;
215 br_configuration_update(p->br);
216 br_port_state_selection(p->br);
217}
218
219ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id)
220{
221 return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
222 id->prio[0], id->prio[1],
223 id->addr[0], id->addr[1], id->addr[2],
224 id->addr[3], id->addr[4], id->addr[5]);
225}