[NET]: Introduce inet_connection_sock
[linux-2.6] / net / bridge / br_stp_if.c
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>
18
19 #include "br_private.h"
20 #include "br_private_stp.h"
21
22
23 /* Port id is composed of priority and port number.
24  * NB: least significant bits of priority are dropped to
25  *     make room for more ports.
26  */
27 static inline port_id br_make_port_id(__u8 priority, __u16 port_no)
28 {
29         return ((u16)priority << BR_PORT_BITS) 
30                 | (port_no & ((1<<BR_PORT_BITS)-1));
31 }
32
33 /* called under bridge lock */
34 void br_init_port(struct net_bridge_port *p)
35 {
36         p->port_id = br_make_port_id(p->priority, p->port_no);
37         br_become_designated_port(p);
38         p->state = BR_STATE_BLOCKING;
39         p->topology_change_ack = 0;
40         p->config_pending = 0;
41
42         br_stp_port_timer_init(p);
43 }
44
45 /* called under bridge lock */
46 void br_stp_enable_bridge(struct net_bridge *br)
47 {
48         struct net_bridge_port *p;
49
50         spin_lock_bh(&br->lock);
51         mod_timer(&br->hello_timer, jiffies + br->hello_time);
52         mod_timer(&br->gc_timer, jiffies + HZ/10);
53         
54         br_config_bpdu_generation(br);
55
56         list_for_each_entry(p, &br->port_list, list) {
57                 if ((p->dev->flags & IFF_UP) && netif_carrier_ok(p->dev))
58                         br_stp_enable_port(p);
59
60         }
61         spin_unlock_bh(&br->lock);
62 }
63
64 /* NO locks held */
65 void br_stp_disable_bridge(struct net_bridge *br)
66 {
67         struct net_bridge_port *p;
68
69         spin_lock(&br->lock);
70         list_for_each_entry(p, &br->port_list, list) {
71                 if (p->state != BR_STATE_DISABLED)
72                         br_stp_disable_port(p);
73
74         }
75
76         br->topology_change = 0;
77         br->topology_change_detected = 0;
78         spin_unlock(&br->lock);
79
80         del_timer_sync(&br->hello_timer);
81         del_timer_sync(&br->topology_change_timer);
82         del_timer_sync(&br->tcn_timer);
83         del_timer_sync(&br->gc_timer);
84 }
85
86 /* called under bridge lock */
87 void br_stp_enable_port(struct net_bridge_port *p)
88 {
89         br_init_port(p);
90         br_port_state_selection(p->br);
91 }
92
93 /* called under bridge lock */
94 void br_stp_disable_port(struct net_bridge_port *p)
95 {
96         struct net_bridge *br;
97         int wasroot;
98
99         br = p->br;
100         printk(KERN_INFO "%s: port %i(%s) entering %s state\n",
101                br->dev->name, p->port_no, p->dev->name, "disabled");
102
103         wasroot = br_is_root_bridge(br);
104         br_become_designated_port(p);
105         p->state = BR_STATE_DISABLED;
106         p->topology_change_ack = 0;
107         p->config_pending = 0;
108
109         del_timer(&p->message_age_timer);
110         del_timer(&p->forward_delay_timer);
111         del_timer(&p->hold_timer);
112
113         br_configuration_update(br);
114
115         br_port_state_selection(br);
116
117         if (br_is_root_bridge(br) && !wasroot)
118                 br_become_root_bridge(br);
119 }
120
121 /* called under bridge lock */
122 static void br_stp_change_bridge_id(struct net_bridge *br, 
123                                     const unsigned char *addr)
124 {
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) {
136                 if (!memcmp(p->designated_bridge.addr, oldaddr, ETH_ALEN))
137                         memcpy(p->designated_bridge.addr, addr, ETH_ALEN);
138
139                 if (!memcmp(p->designated_root.addr, oldaddr, ETH_ALEN))
140                         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
150 static const unsigned char br_mac_zero[6];
151
152 /* called under bridge lock */
153 void 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 ||
160                     memcmp(p->dev->dev_addr, addr, ETH_ALEN) < 0)
161                         addr = p->dev->dev_addr;
162
163         }
164
165         if (memcmp(br->bridge_id.addr, addr, ETH_ALEN))
166                 br_stp_change_bridge_id(br, addr);
167 }
168
169 /* called under bridge lock */
170 void 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 */
195 void 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 */
212 void 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
219 ssize_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 }