]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/bridge/br_stp_if.c
2 * Spanning tree protocol; interface code
3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * $Id: br_stp_if.c,v 1.4 2001/04/14 21:14:39 davem Exp $
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.
16 #include <linux/kernel.h>
17 #include <linux/smp_lock.h>
18 #include <linux/etherdevice.h>
20 #include "br_private.h"
21 #include "br_private_stp.h"
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.
28 static inline port_id
br_make_port_id(__u8 priority
, __u16 port_no
)
30 return ((u16
)priority
<< BR_PORT_BITS
)
31 | (port_no
& ((1<<BR_PORT_BITS
)-1));
34 /* called under bridge lock */
35 void br_init_port(struct net_bridge_port
*p
)
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;
43 br_stp_port_timer_init(p
);
46 /* called under bridge lock */
47 void br_stp_enable_bridge(struct net_bridge
*br
)
49 struct net_bridge_port
*p
;
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);
55 br_config_bpdu_generation(br
);
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
);
62 spin_unlock_bh(&br
->lock
);
66 void br_stp_disable_bridge(struct net_bridge
*br
)
68 struct net_bridge_port
*p
;
71 list_for_each_entry(p
, &br
->port_list
, list
) {
72 if (p
->state
!= BR_STATE_DISABLED
)
73 br_stp_disable_port(p
);
77 br
->topology_change
= 0;
78 br
->topology_change_detected
= 0;
79 spin_unlock(&br
->lock
);
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
);
87 /* called under bridge lock */
88 void br_stp_enable_port(struct net_bridge_port
*p
)
91 br_port_state_selection(p
->br
);
94 /* called under bridge lock */
95 void br_stp_disable_port(struct net_bridge_port
*p
)
97 struct net_bridge
*br
;
101 printk(KERN_INFO
"%s: port %i(%s) entering %s state\n",
102 br
->dev
->name
, p
->port_no
, p
->dev
->name
, "disabled");
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;
110 del_timer(&p
->message_age_timer
);
111 del_timer(&p
->forward_delay_timer
);
112 del_timer(&p
->hold_timer
);
114 br_configuration_update(br
);
116 br_port_state_selection(br
);
118 if (br_is_root_bridge(br
) && !wasroot
)
119 br_become_root_bridge(br
);
122 /* called under bridge lock */
123 static void br_stp_change_bridge_id(struct net_bridge
*br
,
124 const unsigned char *addr
)
126 unsigned char oldaddr
[6];
127 struct net_bridge_port
*p
;
130 wasroot
= br_is_root_bridge(br
);
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
);
136 list_for_each_entry(p
, &br
->port_list
, list
) {
137 if (!compare_ether_addr(p
->designated_bridge
.addr
, oldaddr
))
138 memcpy(p
->designated_bridge
.addr
, addr
, ETH_ALEN
);
140 if (!compare_ether_addr(p
->designated_root
.addr
, oldaddr
))
141 memcpy(p
->designated_root
.addr
, addr
, ETH_ALEN
);
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
);
151 static const unsigned char br_mac_zero
[6];
153 /* called under bridge lock */
154 void br_stp_recalculate_bridge_id(struct net_bridge
*br
)
156 const unsigned char *addr
= br_mac_zero
;
157 struct net_bridge_port
*p
;
159 list_for_each_entry(p
, &br
->port_list
, list
) {
160 if (addr
== br_mac_zero
||
161 compare_ether_addr(p
->dev
->dev_addr
, addr
) < 0)
162 addr
= p
->dev
->dev_addr
;
166 if (compare_ether_addr(br
->bridge_id
.addr
, addr
))
167 br_stp_change_bridge_id(br
, addr
);
170 /* called under bridge lock */
171 void br_stp_set_bridge_priority(struct net_bridge
*br
, u16 newprio
)
173 struct net_bridge_port
*p
;
176 wasroot
= br_is_root_bridge(br
);
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;
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
);
195 /* called under bridge lock */
196 void br_stp_set_port_priority(struct net_bridge_port
*p
, u8 newprio
)
198 port_id new_port_id
= br_make_port_id(newprio
, p
->port_no
);
200 if (br_is_designated_port(p
))
201 p
->designated_port
= new_port_id
;
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
);
212 /* called under bridge lock */
213 void br_stp_set_path_cost(struct net_bridge_port
*p
, u32 path_cost
)
215 p
->path_cost
= path_cost
;
216 br_configuration_update(p
->br
);
217 br_port_state_selection(p
->br
);
220 ssize_t
br_show_bridge_id(char *buf
, const struct bridge_id
*id
)
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]);