kernel - Major bridging functionality added (bug fixes 2)
[dragonfly.git] / sys / net / bridge / bridgestp.c
CommitLineData
ac93838f
SS
1/*
2 * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Jason L. Wright
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * $OpenBSD: bridgestp.c,v 1.5 2001/03/22 03:48:29 jason Exp $
32 * $NetBSD: bridgestp.c,v 1.5 2003/11/28 08:56:48 keihan Exp $
33 * $FreeBSD: src/sys/net/bridgestp.c,v 1.7 2005/10/11 02:58:32 thompsa Exp $
ac93838f
SS
34 */
35
36/*
37 * Implementation of the spanning tree protocol as defined in
38 * ISO/IEC Final DIS 15802-3 (IEEE P802.1D/D17), May 25, 1998.
39 * (In English: IEEE 802.1D, Draft 17, 1998)
40 */
41
ac93838f
SS
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/mbuf.h>
45#include <sys/socket.h>
46#include <sys/sockio.h>
47#include <sys/kernel.h>
48#include <sys/callout.h>
49#include <sys/proc.h>
50#include <sys/lock.h>
51#include <sys/thread.h>
52#include <sys/thread2.h>
e9d22060 53#include <sys/msgport2.h>
ac93838f
SS
54
55#include <net/if.h>
56#include <net/if_dl.h>
57#include <net/if_types.h>
58#include <net/if_llc.h>
59#include <net/if_media.h>
60
61#include <netinet/in.h>
62#include <netinet/in_systm.h>
63#include <netinet/in_var.h>
64#include <netinet/if_ether.h>
65#include <net/bridge/if_bridgevar.h>
66
67/* BPDU message types */
68#define BSTP_MSGTYPE_CFG 0x00 /* Configuration */
69#define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */
70
71/* BPDU flags */
72#define BSTP_FLAG_TC 0x01 /* Topology change */
73#define BSTP_FLAG_TCA 0x80 /* Topology change ack */
74
75#define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */
76#define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */
77
78/*
79 * Because BPDU's do not make nicely aligned structures, two different
80 * declarations are used: bstp_?bpdu (wire representation, packed) and
81 * bstp_*_unit (internal, nicely aligned version).
82 */
83
84/* configuration bridge protocol data unit */
85struct bstp_cbpdu {
86 uint8_t cbu_dsap; /* LLC: destination sap */
87 uint8_t cbu_ssap; /* LLC: source sap */
88 uint8_t cbu_ctl; /* LLC: control */
89 uint16_t cbu_protoid; /* protocol id */
90 uint8_t cbu_protover; /* protocol version */
91 uint8_t cbu_bpdutype; /* message type */
92 uint8_t cbu_flags; /* flags (below) */
93
94 /* root id */
95 uint16_t cbu_rootpri; /* root priority */
96 uint8_t cbu_rootaddr[6]; /* root address */
97
98 uint32_t cbu_rootpathcost; /* root path cost */
99
100 /* bridge id */
101 uint16_t cbu_bridgepri; /* bridge priority */
102 uint8_t cbu_bridgeaddr[6]; /* bridge address */
103
104 uint16_t cbu_portid; /* port id */
105 uint16_t cbu_messageage; /* current message age */
106 uint16_t cbu_maxage; /* maximum age */
107 uint16_t cbu_hellotime; /* hello time */
108 uint16_t cbu_forwarddelay; /* forwarding delay */
109} __attribute__((__packed__));
110
111/* topology change notification bridge protocol data unit */
112struct bstp_tbpdu {
113 uint8_t tbu_dsap; /* LLC: destination sap */
114 uint8_t tbu_ssap; /* LLC: source sap */
115 uint8_t tbu_ctl; /* LLC: control */
116 uint16_t tbu_protoid; /* protocol id */
117 uint8_t tbu_protover; /* protocol version */
118 uint8_t tbu_bpdutype; /* message type */
119} __attribute__((__packed__));
120
121const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
122
d217f5e5
SU
123static void bstp_initialize_port(struct bridge_softc *,
124 struct bridge_iflist *);
125static void bstp_ifupdstatus(struct bridge_softc *, struct bridge_iflist *);
126static void bstp_enable_port(struct bridge_softc *, struct bridge_iflist *);
127static void bstp_disable_port(struct bridge_softc *,
128 struct bridge_iflist *);
129#ifdef notused
130static void bstp_enable_change_detection(struct bridge_iflist *);
131static void bstp_disable_change_detection(struct bridge_iflist *);
132#endif /* notused */
133static int bstp_root_bridge(struct bridge_softc *sc);
d217f5e5
SU
134static void bstp_transmit_config(struct bridge_softc *,
135 struct bridge_iflist *);
136static void bstp_transmit_tcn(struct bridge_softc *);
137static void bstp_received_config_bpdu(struct bridge_softc *,
138 struct bridge_iflist *, struct bstp_config_unit *);
139static void bstp_received_tcn_bpdu(struct bridge_softc *,
140 struct bridge_iflist *, struct bstp_tcn_unit *);
141static void bstp_record_config_information(struct bridge_softc *,
142 struct bridge_iflist *, struct bstp_config_unit *);
143static void bstp_record_config_timeout_values(struct bridge_softc *,
144 struct bstp_config_unit *);
145static void bstp_config_bpdu_generation(struct bridge_softc *);
146static void bstp_send_config_bpdu(struct bridge_softc *,
147 struct bridge_iflist *, struct bstp_config_unit *);
148static void bstp_configuration_update(struct bridge_softc *);
d217f5e5 149static void bstp_port_state_selection(struct bridge_softc *);
70d9a675
MD
150static void bstp_clear_peer_info(struct bridge_softc *,
151 struct bridge_iflist *);
d217f5e5
SU
152static void bstp_make_forwarding(struct bridge_softc *,
153 struct bridge_iflist *);
154static void bstp_make_blocking(struct bridge_softc *,
155 struct bridge_iflist *);
3677aae9
MD
156static void bstp_make_l1blocking(struct bridge_softc *sc,
157 struct bridge_iflist *bif);
1e858374
MD
158static void bstp_adjust_bonded_states(struct bridge_softc *sc,
159 struct bridge_iflist *obif);
d217f5e5
SU
160static void bstp_set_port_state(struct bridge_iflist *, uint8_t);
161#ifdef notused
162static void bstp_set_bridge_priority(struct bridge_softc *, uint64_t);
163static void bstp_set_port_priority(struct bridge_softc *,
164 struct bridge_iflist *, uint16_t);
165static void bstp_set_path_cost(struct bridge_softc *,
166 struct bridge_iflist *, uint32_t);
167#endif /* notused */
168static void bstp_topology_change_detection(struct bridge_softc *);
169static void bstp_topology_change_acknowledged(struct bridge_softc *);
170static void bstp_acknowledge_topology_change(struct bridge_softc *,
171 struct bridge_iflist *);
172
173static void bstp_tick(void *);
174static void bstp_timer_start(struct bridge_timer *, uint16_t);
175static void bstp_timer_stop(struct bridge_timer *);
176static int bstp_timer_expired(struct bridge_timer *, uint16_t);
177
178static void bstp_hold_timer_expiry(struct bridge_softc *,
179 struct bridge_iflist *);
180static void bstp_message_age_timer_expiry(struct bridge_softc *,
181 struct bridge_iflist *);
182static void bstp_forward_delay_timer_expiry(struct bridge_softc *,
183 struct bridge_iflist *);
184static void bstp_topology_change_timer_expiry(struct bridge_softc *);
185static void bstp_tcn_timer_expiry(struct bridge_softc *);
186static void bstp_hello_timer_expiry(struct bridge_softc *);
187static int bstp_addr_cmp(const uint8_t *, const uint8_t *);
188
70d9a675
MD
189/*
190 * When transmitting a config we tack on our path cost to
191 * our aggregated path-to-root cost.
192 */
d217f5e5 193static void
ac93838f
SS
194bstp_transmit_config(struct bridge_softc *sc, struct bridge_iflist *bif)
195{
196 if (bif->bif_hold_timer.active) {
197 bif->bif_config_pending = 1;
198 return;
199 }
200
201 bif->bif_config_bpdu.cu_message_type = BSTP_MSGTYPE_CFG;
202 bif->bif_config_bpdu.cu_rootid = sc->sc_designated_root;
70d9a675
MD
203 bif->bif_config_bpdu.cu_root_path_cost = sc->sc_designated_cost +
204 bif->bif_path_cost;
ac93838f
SS
205 bif->bif_config_bpdu.cu_bridge_id = sc->sc_bridge_id;
206 bif->bif_config_bpdu.cu_port_id = bif->bif_port_id;
207
8f7b13ef 208 if (bstp_root_bridge(sc)) {
ac93838f 209 bif->bif_config_bpdu.cu_message_age = 0;
70d9a675 210 } else if (sc->sc_root_port) {
ac93838f 211 bif->bif_config_bpdu.cu_message_age =
70d9a675
MD
212 sc->sc_root_port->bif_message_age_timer.value +
213 BSTP_MESSAGE_AGE_INCR;
214 } else {
215 bif->bif_config_bpdu.cu_message_age = BSTP_MESSAGE_AGE_INCR;
8f7b13ef 216 }
ac93838f
SS
217
218 bif->bif_config_bpdu.cu_max_age = sc->sc_max_age;
219 bif->bif_config_bpdu.cu_hello_time = sc->sc_hello_time;
220 bif->bif_config_bpdu.cu_forward_delay = sc->sc_forward_delay;
221 bif->bif_config_bpdu.cu_topology_change_acknowledgment
222 = bif->bif_topology_change_acknowledge;
223 bif->bif_config_bpdu.cu_topology_change = sc->sc_topology_change;
224
3677aae9
MD
225 if (bif->bif_config_bpdu.cu_message_age < sc->sc_max_age ||
226 (sc->sc_ifp->if_flags & IFF_LINK1)) {
ac93838f
SS
227 bif->bif_topology_change_acknowledge = 0;
228 bif->bif_config_pending = 0;
229 bstp_send_config_bpdu(sc, bif, &bif->bif_config_bpdu);
230 bstp_timer_start(&bif->bif_hold_timer, 0);
231 }
232}
233
d217f5e5 234static void
ac93838f 235bstp_send_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
3677aae9 236 struct bstp_config_unit *cu)
ac93838f
SS
237{
238 struct ifnet *ifp;
239 struct mbuf *m;
240 struct ether_header *eh;
241 struct bstp_cbpdu bpdu;
242
243 ifp = bif->bif_ifp;
244
245 if ((ifp->if_flags & IFF_RUNNING) == 0)
246 return;
247
248 MGETHDR(m, MB_DONTWAIT, MT_DATA);
249 if (m == NULL)
250 return;
251
252 eh = mtod(m, struct ether_header *);
253
254 m->m_pkthdr.rcvif = ifp;
255 m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
256 m->m_len = m->m_pkthdr.len;
257
258 bpdu.cbu_ssap = bpdu.cbu_dsap = LLC_8021D_LSAP;
259 bpdu.cbu_ctl = LLC_UI;
260 bpdu.cbu_protoid = htons(0);
261 bpdu.cbu_protover = 0;
262 bpdu.cbu_bpdutype = cu->cu_message_type;
263 bpdu.cbu_flags = (cu->cu_topology_change ? BSTP_FLAG_TC : 0) |
264 (cu->cu_topology_change_acknowledgment ? BSTP_FLAG_TCA : 0);
265
266 bpdu.cbu_rootpri = htons(cu->cu_rootid >> 48);
267 bpdu.cbu_rootaddr[0] = cu->cu_rootid >> 40;
268 bpdu.cbu_rootaddr[1] = cu->cu_rootid >> 32;
269 bpdu.cbu_rootaddr[2] = cu->cu_rootid >> 24;
270 bpdu.cbu_rootaddr[3] = cu->cu_rootid >> 16;
271 bpdu.cbu_rootaddr[4] = cu->cu_rootid >> 8;
272 bpdu.cbu_rootaddr[5] = cu->cu_rootid >> 0;
273
274 bpdu.cbu_rootpathcost = htonl(cu->cu_root_path_cost);
275
d217f5e5
SU
276 bpdu.cbu_bridgepri = htons(cu->cu_bridge_id >> 48);
277 bpdu.cbu_bridgeaddr[0] = cu->cu_bridge_id >> 40;
278 bpdu.cbu_bridgeaddr[1] = cu->cu_bridge_id >> 32;
279 bpdu.cbu_bridgeaddr[2] = cu->cu_bridge_id >> 24;
280 bpdu.cbu_bridgeaddr[3] = cu->cu_bridge_id >> 16;
281 bpdu.cbu_bridgeaddr[4] = cu->cu_bridge_id >> 8;
282 bpdu.cbu_bridgeaddr[5] = cu->cu_bridge_id >> 0;
ac93838f
SS
283
284 bpdu.cbu_portid = htons(cu->cu_port_id);
285 bpdu.cbu_messageage = htons(cu->cu_message_age);
286 bpdu.cbu_maxage = htons(cu->cu_max_age);
287 bpdu.cbu_hellotime = htons(cu->cu_hello_time);
288 bpdu.cbu_forwarddelay = htons(cu->cu_forward_delay);
289
70d9a675
MD
290 /*
291 * Packets sent from the bridge always use the bridge MAC
292 * as the source.
293 */
294 memcpy(eh->ether_shost, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
ac93838f
SS
295 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
296 eh->ether_type = htons(sizeof(bpdu));
297
298 memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
299
708a3bfa 300 bridge_enqueue(ifp, m);
ac93838f
SS
301}
302
d217f5e5 303static int
ac93838f
SS
304bstp_root_bridge(struct bridge_softc *sc)
305{
306 return (sc->sc_designated_root == sc->sc_bridge_id);
307}
308
70d9a675
MD
309/*
310 * Returns TRUE if the recorded information from our peer has a shorter
311 * graph distance than our current best.
312 */
313int
314bstp_supersedes_port_info(struct bridge_softc *sc, struct bridge_iflist *bif)
ac93838f 315{
70d9a675 316 if (bif->bif_peer_root < sc->sc_designated_root)
ac93838f 317 return (1);
70d9a675 318 if (bif->bif_peer_root > sc->sc_designated_root)
ac93838f
SS
319 return (0);
320
70d9a675
MD
321 /*
322 * Both bif_peer_cost and sc_designated_cost have NOT added in
323 * bif->bif_path_cost, so we can optimize it out.
324 */
325 if (bif->bif_peer_cost < sc->sc_designated_cost)
ac93838f 326 return (1);
70d9a675 327 if (bif->bif_peer_cost > sc->sc_designated_cost)
ac93838f
SS
328 return (0);
329
70d9a675 330 if (bif->bif_peer_bridge < sc->sc_designated_bridge)
ac93838f 331 return (1);
70d9a675 332 if (bif->bif_peer_bridge > sc->sc_designated_bridge)
ac93838f
SS
333 return (0);
334
70d9a675
MD
335 /* bridge_id or bridge+port collision w/peer returns TRUE */
336 if (bif->bif_peer_bridge != sc->sc_bridge_id)
ac93838f 337 return (1);
70d9a675 338 if (bif->bif_peer_port <= sc->sc_designated_port)
ac93838f
SS
339 return (1);
340 return (0);
341}
342
70d9a675
MD
343/*
344 * The shorter graph distance represented by cu (most of which is also
345 * already stored in our bif_peer_* fields) becomes the designated info.
346 *
347 * NOTE: sc_designated_cost does not include bif_path_cost, it is added
348 * in later on a port-by-port basis as needed.
349 */
d217f5e5 350static void
ac93838f 351bstp_record_config_information(struct bridge_softc *sc,
70d9a675
MD
352 struct bridge_iflist *bif,
353 struct bstp_config_unit *cu)
ac93838f 354{
70d9a675
MD
355 sc->sc_designated_root = bif->bif_peer_root;
356 sc->sc_designated_cost = bif->bif_peer_cost;
357 sc->sc_designated_bridge = bif->bif_peer_bridge;
358 sc->sc_designated_port = bif->bif_peer_port;
ac93838f
SS
359 bstp_timer_start(&bif->bif_message_age_timer, cu->cu_message_age);
360}
361
d217f5e5 362static void
ac93838f 363bstp_record_config_timeout_values(struct bridge_softc *sc,
70d9a675 364 struct bstp_config_unit *config)
ac93838f
SS
365{
366 sc->sc_max_age = config->cu_max_age;
367 sc->sc_hello_time = config->cu_hello_time;
368 sc->sc_forward_delay = config->cu_forward_delay;
369 sc->sc_topology_change = config->cu_topology_change;
370}
371
d217f5e5 372static void
ac93838f
SS
373bstp_config_bpdu_generation(struct bridge_softc *sc)
374{
8f7b13ef 375 struct bridge_iflist *bif, *nbif;
ac93838f 376
70d9a675 377 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
ac93838f
SS
378 if ((bif->bif_flags & IFBIF_STP) == 0)
379 continue;
3677aae9
MD
380 if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
381 ((sc->sc_ifp->if_flags & IFF_LINK1) ||
70d9a675 382 (bif->bif_flags & IFBIF_DESIGNATED))) {
ac93838f 383 bstp_transmit_config(sc, bif);
3677aae9 384 }
8f7b13ef
SZ
385
386 if (nbif != NULL && !nbif->bif_onlist) {
387 KKASSERT(bif->bif_onlist);
70d9a675 388 nbif = TAILQ_NEXT(bif, bif_next);
8f7b13ef 389 }
ac93838f
SS
390 }
391}
392
d217f5e5 393static void
ac93838f
SS
394bstp_transmit_tcn(struct bridge_softc *sc)
395{
396 struct bstp_tbpdu bpdu;
70d9a675 397 struct ifnet *ifp;
ac93838f
SS
398 struct ether_header *eh;
399 struct mbuf *m;
400
70d9a675
MD
401 if (sc->sc_root_port == NULL) /* all iterfaces disabled */
402 return;
403
404 ifp = sc->sc_root_port->bif_ifp;
ac93838f
SS
405 if ((ifp->if_flags & IFF_RUNNING) == 0)
406 return;
407
408 MGETHDR(m, MB_DONTWAIT, MT_DATA);
409 if (m == NULL)
410 return;
411
412 m->m_pkthdr.rcvif = ifp;
413 m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
414 m->m_len = m->m_pkthdr.len;
415
416 eh = mtod(m, struct ether_header *);
417
70d9a675
MD
418 /*
419 * Packets sent from the bridge always use the bridge MAC
420 * as the source.
421 */
422 memcpy(eh->ether_shost, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
ac93838f
SS
423 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
424 eh->ether_type = htons(sizeof(bpdu));
425
426 bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP;
427 bpdu.tbu_ctl = LLC_UI;
428 bpdu.tbu_protoid = 0;
429 bpdu.tbu_protover = 0;
430 bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN;
431
432 memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
433
708a3bfa 434 bridge_enqueue(ifp, m);
ac93838f
SS
435}
436
70d9a675
MD
437/*
438 * Recalculate sc->sc_designated* and sc->sc_root_port (if our bridge
439 * is calculated to be the root bridge). We do this by initializing
440 * the designated variables to point at us and then scan our peers.
441 * Any uninitialized peers will have a max-value root.
442 *
443 * Clear IFBIF_DESIGNATED on any ports which no longer match the criteria
444 * required to be a designated port. Only aged out ports and the root
445 * port can be designated.
446 *
447 * If we win we do a second scan to determine which port on our bridge
448 * is the best.
449 */
d217f5e5 450static void
ac93838f
SS
451bstp_configuration_update(struct bridge_softc *sc)
452{
70d9a675
MD
453 uint64_t designated_root = sc->sc_bridge_id;
454 uint64_t designated_bridge = sc->sc_bridge_id;
455 uint32_t designated_cost = 0xFFFFFFFFU;
0e66e711 456 uint32_t designated_root_cost = 0xFFFFFFFFU;
70d9a675
MD
457 uint16_t designated_port = 65535;
458 struct bridge_iflist *root_port = NULL;
459 struct bridge_iflist *bif;
ac93838f 460
70d9a675
MD
461 /*
462 * Resolve information from our peers. Aged peers will have
463 * a maxed bif_peer_root and not come under consideration.
464 */
465 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
ac93838f
SS
466 if ((bif->bif_flags & IFBIF_STP) == 0)
467 continue;
70d9a675
MD
468 if (bif->bif_state == BSTP_IFSTATE_DISABLED ||
469 bif->bif_state == BSTP_IFSTATE_L1BLOCKING) {
ac93838f 470 continue;
70d9a675
MD
471 }
472
473 if (bif->bif_peer_root > designated_root)
ac93838f 474 continue;
70d9a675 475 if (bif->bif_peer_root < designated_root)
ac93838f
SS
476 goto set_port;
477
70d9a675
MD
478 /*
479 * NOTE: The designated_cost temporary variable already added
480 * in the path code of the related root port.
481 */
482 if (bif->bif_peer_cost + bif->bif_path_cost > designated_cost)
ac93838f 483 continue;
70d9a675 484 if (bif->bif_peer_cost + bif->bif_path_cost < designated_cost)
ac93838f 485 goto set_port;
ac93838f 486
70d9a675 487 if (bif->bif_peer_bridge > designated_bridge)
ac93838f 488 continue;
70d9a675 489 if (bif->bif_peer_bridge < designated_bridge)
ac93838f 490 goto set_port;
70d9a675
MD
491
492 if (bif->bif_peer_port > designated_port)
ac93838f 493 continue;
70d9a675
MD
494 if (bif->bif_peer_port < designated_port)
495 goto set_port;
ac93838f 496
70d9a675
MD
497 /*
498 * Same root, path cost, bridge, and port. Set the root
499 * only if we do not already have it.
500 */
501 if (root_port)
ac93838f 502 continue;
70d9a675
MD
503
504 /*
505 * New root port (from peers)
506 *
507 * NOTE: Temporarily add bif_path_cost into the designated
508 * cost to reduce complexity in the loop, it will be
509 * subtracted out when we are done.
510 */
ac93838f 511set_port:
70d9a675
MD
512 designated_root = bif->bif_peer_root;
513 designated_cost = bif->bif_peer_cost + bif->bif_path_cost;
0e66e711 514 designated_root_cost = bif->bif_peer_cost;
70d9a675
MD
515 designated_bridge = bif->bif_peer_bridge;
516 designated_port = bif->bif_peer_port;
ac93838f
SS
517 root_port = bif;
518 }
519
70d9a675
MD
520 /*
521 * root_port will be NULL at the start here if all of our
522 * peers are aged or are not as good a root as our bridge would
523 * be. It can also be NULL due to all related links being
524 * disabled.
525 *
526 * If the root winds up being our bridge scan again against local
527 * information. Unconditionally update IFBIF_DESIGNATED.
528 */
529 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
530 bif->bif_flags &= ~(IFBIF_DESIGNATED | IFBIF_ROOT);
ac93838f
SS
531 if ((bif->bif_flags & IFBIF_STP) == 0)
532 continue;
70d9a675
MD
533 if (bif->bif_state == BSTP_IFSTATE_DISABLED ||
534 bif->bif_state == BSTP_IFSTATE_L1BLOCKING) {
535 continue;
536 }
537
538 /*
539 * Set DESIGNATED for an aged or unknown peer.
540 */
541 if (bif->bif_peer_bridge == 0xFFFFFFFFFFFFFFFFLLU)
542 bif->bif_flags |= IFBIF_DESIGNATED;
543 if (designated_root != sc->sc_bridge_id)
ac93838f
SS
544 continue;
545
70d9a675
MD
546 /*
547 * This is only reached if our bridge is the root bridge,
548 * select the root port (IFBIF_DESIGNATED is set at the
549 * end).
550 *
0e66e711
MD
551 * Since we are the root the peer cost should already include
552 * our path cost. We still need the combined costs from both
553 * our point of view and the peer's point of view to match
554 * up with the peer.
555 *
556 * If we used ONLY our path cost here we would have no peer
557 * path cost in the calculation and would reach a different
558 * conclusion than our peer has reached.
70d9a675 559 */
0e66e711 560 if (bif->bif_peer_cost > designated_cost)
ac93838f 561 continue;
0e66e711 562 if (bif->bif_peer_cost < designated_cost)
70d9a675 563 goto set_port2;
ac93838f 564
70d9a675 565 if (bif->bif_port_id > designated_port)
ac93838f 566 continue;
70d9a675
MD
567 if (bif->bif_port_id < designated_port)
568 goto set_port2;
569 /* degenerate case (possible peer collision w/our key */
570
571 /*
0e66e711
MD
572 * New port. Since we are the root, the root cost is always
573 * 0. Since we are the root the peer path should be our
574 * path cost + peer path cost.
70d9a675
MD
575 */
576set_port2:
0e66e711
MD
577 designated_cost = bif->bif_peer_cost;
578 designated_root_cost = 0;
70d9a675
MD
579 designated_bridge = sc->sc_bridge_id;
580 designated_port = bif->bif_port_id;
581 root_port = bif;
ac93838f 582 }
ac93838f 583
70d9a675
MD
584 /*
585 * Update aggregate information. The selected root port always
586 * becomes a designated port (along with aged ports). This can
587 * either be the port whos peer is closest to the root or it
588 * can be one of our ports if our bridge is the root.
589 *
590 * The root cost we record in sc_designated_root does not include
591 * bif_path_cost of the root port, since we may transmit this
592 * out of any port we will add the cost back in later on on
593 * a per-port basis.
594 *
595 * root_port can be NULL here (if all links are disabled)
596 */
597 if (root_port) {
598 sc->sc_designated_root = designated_root;
0e66e711 599 sc->sc_designated_cost = designated_root_cost;
70d9a675
MD
600 sc->sc_designated_bridge = designated_bridge;
601 sc->sc_designated_port = designated_port;
602 root_port->bif_flags |= IFBIF_DESIGNATED | IFBIF_ROOT;
603 } else {
604 sc->sc_designated_root = designated_root;
605 sc->sc_designated_cost = designated_cost;
606 sc->sc_designated_bridge = designated_bridge;
607 sc->sc_designated_port = designated_port;
608 }
609 sc->sc_root_port = root_port;
ac93838f
SS
610}
611
70d9a675
MD
612/*
613 * Calculate the desired state for each interface link on our bridge.
614 *
615 * The best port will match against sc->sc_root_port (whether we are root
616 * or whether that port is the closest to the root). We push this port
617 * towards a FORWARDING state.
618 *
619 * Next come designated ports, either aged ports or ports with no peer info
620 * (yet), or the peer who is closest to the root. We push this port towards
621 * a FORWARDING state as well.
622 *
1e858374 623 * Any remaining ports are pushed towards a BLOCKING state. Both sides of
70d9a675
MD
624 * the port (us and our peer) should wind up placing the two ends in this
625 * state or bad things happen.
626 */
d217f5e5 627static void
ac93838f
SS
628bstp_port_state_selection(struct bridge_softc *sc)
629{
8f7b13ef 630 struct bridge_iflist *bif, *nbif;
ac93838f 631
70d9a675 632 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
ac93838f
SS
633 if ((bif->bif_flags & IFBIF_STP) == 0)
634 continue;
70d9a675
MD
635 if (sc->sc_root_port &&
636 bif->bif_info == sc->sc_root_port->bif_info) {
ac93838f
SS
637 bif->bif_config_pending = 0;
638 bif->bif_topology_change_acknowledge = 0;
639 bstp_make_forwarding(sc, bif);
70d9a675 640 } else if (bif->bif_flags & IFBIF_DESIGNATED) {
ac93838f
SS
641 bstp_timer_stop(&bif->bif_message_age_timer);
642 bstp_make_forwarding(sc, bif);
643 } else {
644 bif->bif_config_pending = 0;
645 bif->bif_topology_change_acknowledge = 0;
646 bstp_make_blocking(sc, bif);
647 }
8f7b13ef
SZ
648
649 if (nbif != NULL && !nbif->bif_onlist) {
650 KKASSERT(bif->bif_onlist);
70d9a675 651 nbif = TAILQ_NEXT(bif, bif_next);
8f7b13ef 652 }
ac93838f
SS
653 }
654}
655
70d9a675
MD
656/*
657 * Clear peer info, effectively makes the port looked aged out.
658 * It becomes a designated go-to port.
659 */
660static void
661bstp_clear_peer_info(struct bridge_softc *sc, struct bridge_iflist *bif)
662{
663 bif->bif_peer_root = 0xFFFFFFFFFFFFFFFFLLU;
664 bif->bif_peer_cost = 0xFFFFFFFFU;
665 bif->bif_peer_bridge = 0xFFFFFFFFFFFFFFFFLLU;
666 bif->bif_peer_port = 0xFFFFU;
667
668 if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
669 bif->bif_state != BSTP_IFSTATE_L1BLOCKING) {
670 bif->bif_flags |= IFBIF_DESIGNATED;
671 }
672}
673
d217f5e5 674static void
ac93838f
SS
675bstp_make_forwarding(struct bridge_softc *sc, struct bridge_iflist *bif)
676{
1e858374
MD
677 if (bif->bif_state == BSTP_IFSTATE_BLOCKING ||
678 bif->bif_state == BSTP_IFSTATE_BONDED) {
ac93838f
SS
679 bstp_set_port_state(bif, BSTP_IFSTATE_LISTENING);
680 bstp_timer_start(&bif->bif_forward_delay_timer, 0);
681 }
682}
683
d217f5e5 684static void
ac93838f
SS
685bstp_make_blocking(struct bridge_softc *sc, struct bridge_iflist *bif)
686{
3677aae9
MD
687 if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
688 bif->bif_state != BSTP_IFSTATE_BLOCKING &&
1e858374 689 bif->bif_state != BSTP_IFSTATE_BONDED &&
3677aae9 690 bif->bif_state != BSTP_IFSTATE_L1BLOCKING) {
ac93838f
SS
691 if ((bif->bif_state == BSTP_IFSTATE_FORWARDING) ||
692 (bif->bif_state == BSTP_IFSTATE_LEARNING)) {
693 if (bif->bif_change_detection_enabled) {
694 bstp_topology_change_detection(sc);
695 }
696 }
697 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
30ced003 698 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
ac93838f 699 bstp_timer_stop(&bif->bif_forward_delay_timer);
1e858374
MD
700 if (sc->sc_ifp->if_flags & IFF_LINK2)
701 bstp_adjust_bonded_states(sc, bif);
ac93838f
SS
702 }
703}
704
d217f5e5 705static void
3677aae9
MD
706bstp_make_l1blocking(struct bridge_softc *sc, struct bridge_iflist *bif)
707{
1e858374
MD
708 int was_forwarding = (bif->bif_state == BSTP_IFSTATE_FORWARDING);
709
3677aae9
MD
710 switch(bif->bif_state) {
711 case BSTP_IFSTATE_LISTENING:
712 case BSTP_IFSTATE_LEARNING:
713 case BSTP_IFSTATE_FORWARDING:
714 case BSTP_IFSTATE_BLOCKING:
1e858374 715 case BSTP_IFSTATE_BONDED:
3677aae9
MD
716 bstp_set_port_state(bif, BSTP_IFSTATE_L1BLOCKING);
717 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
718 bstp_timer_stop(&bif->bif_forward_delay_timer);
719 bstp_timer_stop(&bif->bif_link1_timer);
70d9a675
MD
720 if (bif->bif_flags & IFBIF_DESIGNATED) {
721 bif->bif_flags &= ~IFBIF_DESIGNATED;
722 bstp_configuration_update(sc);
723 bstp_port_state_selection(sc);
724 }
1e858374
MD
725 if (was_forwarding && (sc->sc_ifp->if_flags & IFF_LINK2))
726 bstp_adjust_bonded_states(sc, bif);
3677aae9
MD
727 break;
728 default:
729 break;
730 }
731}
732
1e858374
MD
733/*
734 * Member (bif) changes to or from a FORWARDING state. All members in the
735 * same bonding group which are in a BLOCKING or BONDED state must be set
736 * to either BLOCKING or BONDED based on whether any members in the bonding
737 * group remain in the FORWARDING state.
738 *
739 * Going between the BLOCKING and BONDED states does not require a
740 * configuration update.
741 */
742static void
743bstp_adjust_bonded_states(struct bridge_softc *sc, struct bridge_iflist *obif)
744{
745 struct bridge_iflist *bif;
746 int state = BSTP_IFSTATE_BLOCKING;
747
748 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
749 if ((bif->bif_flags & IFBIF_STP) == 0)
750 continue;
751 if (bif->bif_state != BSTP_IFSTATE_FORWARDING)
752 continue;
753 if (memcmp(IF_LLADDR(bif->bif_ifp), IF_LLADDR(obif->bif_ifp),
754 ETHER_ADDR_LEN) != 0) {
755 continue;
756 }
757 state = BSTP_IFSTATE_BONDED;
758 break;
759 }
760 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
761 if ((bif->bif_flags & IFBIF_STP) == 0)
762 continue;
763 if (bif->bif_state != BSTP_IFSTATE_BLOCKING &&
764 bif->bif_state != BSTP_IFSTATE_BONDED) {
765 continue;
766 }
767 if (memcmp(IF_LLADDR(bif->bif_ifp), IF_LLADDR(obif->bif_ifp),
768 ETHER_ADDR_LEN) != 0) {
769 continue;
770 }
771 if (bif->bif_bond_weight == 0)
772 bif->bif_state = BSTP_IFSTATE_BLOCKING;
773 else
774 bif->bif_state = state;
775 }
776}
777
3677aae9 778static void
ac93838f
SS
779bstp_set_port_state(struct bridge_iflist *bif, uint8_t state)
780{
781 bif->bif_state = state;
782}
783
d217f5e5 784static void
ac93838f
SS
785bstp_topology_change_detection(struct bridge_softc *sc)
786{
787 if (bstp_root_bridge(sc)) {
788 sc->sc_topology_change = 1;
789 bstp_timer_start(&sc->sc_topology_change_timer, 0);
790 } else if (!sc->sc_topology_change_detected) {
791 bstp_transmit_tcn(sc);
792 bstp_timer_start(&sc->sc_tcn_timer, 0);
793 }
794 sc->sc_topology_change_detected = 1;
795}
796
d217f5e5 797static void
ac93838f
SS
798bstp_topology_change_acknowledged(struct bridge_softc *sc)
799{
800 sc->sc_topology_change_detected = 0;
801 bstp_timer_stop(&sc->sc_tcn_timer);
802}
803
d217f5e5 804static void
ac93838f 805bstp_acknowledge_topology_change(struct bridge_softc *sc,
70d9a675 806 struct bridge_iflist *bif)
ac93838f
SS
807{
808 bif->bif_topology_change_acknowledge = 1;
809 bstp_transmit_config(sc, bif);
810}
811
b2417333 812void
89ea766d 813bstp_input(struct bridge_softc *sc, struct bridge_iflist *bif, struct mbuf *m)
ac93838f 814{
ac93838f
SS
815 struct ether_header *eh;
816 struct bstp_tbpdu tpdu;
817 struct bstp_cbpdu cpdu;
818 struct bstp_config_unit cu;
819 struct bstp_tcn_unit tu;
820 uint16_t len;
821
89ea766d 822 if ((bif->bif_flags & IFBIF_STP) == 0)
ac93838f
SS
823 goto out;
824
3677aae9
MD
825 /*
826 * The L1BLOCKING (ping pong failover) test needs to reset the
827 * timer if LINK1 is active.
828 */
829 if (bif->bif_state == BSTP_IFSTATE_L1BLOCKING) {
830 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
831 if (sc->sc_ifp->if_flags & IFF_LINK1)
832 bstp_timer_start(&bif->bif_link1_timer, 0);
833 bstp_make_forwarding(sc, bif);
834 } else if (sc->sc_ifp->if_flags & IFF_LINK1) {
835 bstp_timer_start(&bif->bif_link1_timer, 0);
836 }
837
89ea766d
SZ
838 eh = mtod(m, struct ether_header *);
839
ac93838f
SS
840 len = ntohs(eh->ether_type);
841 if (len < sizeof(tpdu))
842 goto out;
843
844 m_adj(m, ETHER_HDR_LEN);
845
846 if (m->m_pkthdr.len > len)
847 m_adj(m, len - m->m_pkthdr.len);
848 if (m->m_len < sizeof(tpdu) &&
849 (m = m_pullup(m, sizeof(tpdu))) == NULL)
850 goto out;
851
852 memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu));
853
854 if (tpdu.tbu_dsap != LLC_8021D_LSAP ||
855 tpdu.tbu_ssap != LLC_8021D_LSAP ||
856 tpdu.tbu_ctl != LLC_UI)
857 goto out;
858 if (tpdu.tbu_protoid != 0 || tpdu.tbu_protover != 0)
859 goto out;
860
861 switch (tpdu.tbu_bpdutype) {
862 case BSTP_MSGTYPE_TCN:
863 tu.tu_message_type = tpdu.tbu_bpdutype;
864 bstp_received_tcn_bpdu(sc, bif, &tu);
865 break;
866 case BSTP_MSGTYPE_CFG:
867 if (m->m_len < sizeof(cpdu) &&
868 (m = m_pullup(m, sizeof(cpdu))) == NULL)
869 goto out;
870 memcpy(&cpdu, mtod(m, caddr_t), sizeof(cpdu));
871
872 cu.cu_rootid =
873 (((uint64_t)ntohs(cpdu.cbu_rootpri)) << 48) |
874 (((uint64_t)cpdu.cbu_rootaddr[0]) << 40) |
875 (((uint64_t)cpdu.cbu_rootaddr[1]) << 32) |
876 (((uint64_t)cpdu.cbu_rootaddr[2]) << 24) |
877 (((uint64_t)cpdu.cbu_rootaddr[3]) << 16) |
878 (((uint64_t)cpdu.cbu_rootaddr[4]) << 8) |
879 (((uint64_t)cpdu.cbu_rootaddr[5]) << 0);
880
881 cu.cu_bridge_id =
882 (((uint64_t)ntohs(cpdu.cbu_bridgepri)) << 48) |
883 (((uint64_t)cpdu.cbu_bridgeaddr[0]) << 40) |
884 (((uint64_t)cpdu.cbu_bridgeaddr[1]) << 32) |
885 (((uint64_t)cpdu.cbu_bridgeaddr[2]) << 24) |
886 (((uint64_t)cpdu.cbu_bridgeaddr[3]) << 16) |
887 (((uint64_t)cpdu.cbu_bridgeaddr[4]) << 8) |
888 (((uint64_t)cpdu.cbu_bridgeaddr[5]) << 0);
889
890 cu.cu_root_path_cost = ntohl(cpdu.cbu_rootpathcost);
891 cu.cu_message_age = ntohs(cpdu.cbu_messageage);
892 cu.cu_max_age = ntohs(cpdu.cbu_maxage);
893 cu.cu_hello_time = ntohs(cpdu.cbu_hellotime);
894 cu.cu_forward_delay = ntohs(cpdu.cbu_forwarddelay);
895 cu.cu_port_id = ntohs(cpdu.cbu_portid);
896 cu.cu_message_type = cpdu.cbu_bpdutype;
897 cu.cu_topology_change_acknowledgment =
898 (cpdu.cbu_flags & BSTP_FLAG_TCA) ? 1 : 0;
899 cu.cu_topology_change =
900 (cpdu.cbu_flags & BSTP_FLAG_TC) ? 1 : 0;
901 bstp_received_config_bpdu(sc, bif, &cu);
902 break;
903 default:
904 goto out;
905 }
d217f5e5 906out:
ac93838f
SS
907 if (m)
908 m_freem(m);
ac93838f
SS
909}
910
d217f5e5 911static void
ac93838f 912bstp_received_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
70d9a675 913 struct bstp_config_unit *cu)
ac93838f 914{
70d9a675 915 int iamroot;
ac93838f 916
70d9a675 917 iamroot = bstp_root_bridge(sc);
ac93838f
SS
918
919 if (bif->bif_state != BSTP_IFSTATE_DISABLED) {
70d9a675
MD
920 /*
921 * Record information from peer. The peer_cost field
922 * does not include the local bif->bif_path_cost, it will
923 * be added in as needed (since it can be modified manually
924 * this way we don't have to worry about fixups).
925 */
926 bif->bif_peer_root = cu->cu_rootid;
927 bif->bif_peer_cost = cu->cu_root_path_cost;
928 bif->bif_peer_bridge = cu->cu_bridge_id;
929 bif->bif_peer_port = cu->cu_port_id;
930
931 if (bstp_supersedes_port_info(sc, bif)) {
ac93838f
SS
932 bstp_record_config_information(sc, bif, cu);
933 bstp_configuration_update(sc);
934 bstp_port_state_selection(sc);
935
70d9a675
MD
936 /*
937 * If our bridge loses its root status (?)
938 *
939 * Hello's (unsolicited CFG packets) are generated
940 * every hello period of LINK1 is set, otherwise
941 * we are no longer the root bridge and must stop
942 * generating unsolicited CFG packets.
943 */
944 if (iamroot && bstp_root_bridge(sc) == 0) {
3677aae9
MD
945 if ((sc->sc_ifp->if_flags & IFF_LINK1) == 0)
946 bstp_timer_stop(&sc->sc_hello_timer);
ac93838f
SS
947
948 if (sc->sc_topology_change_detected) {
949 bstp_timer_stop(
950 &sc->sc_topology_change_timer);
951 bstp_transmit_tcn(sc);
952 bstp_timer_start(&sc->sc_tcn_timer, 0);
953 }
954 }
955
70d9a675
MD
956 if (sc->sc_root_port &&
957 bif->bif_info == sc->sc_root_port->bif_info) {
ac93838f
SS
958 bstp_record_config_timeout_values(sc, cu);
959 bstp_config_bpdu_generation(sc);
960
961 if (cu->cu_topology_change_acknowledgment)
962 bstp_topology_change_acknowledged(sc);
963 }
70d9a675
MD
964 } else if (bif->bif_flags & IFBIF_DESIGNATED) {
965 /*
966 * Update designated ports (aged out peers or
967 * the port closest to the root) at a faster pace.
968 *
969 * Clear our designated flag if we aren't marked
970 * as the root port.
971 */
ac93838f 972 bstp_transmit_config(sc, bif);
70d9a675
MD
973 if ((bif->bif_flags & IFBIF_ROOT) == 0) {
974 bif->bif_flags &= ~IFBIF_DESIGNATED;
975 bstp_configuration_update(sc);
976 bstp_port_state_selection(sc);
977 }
978 }
ac93838f
SS
979 }
980}
981
d217f5e5 982static void
ac93838f 983bstp_received_tcn_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
70d9a675 984 struct bstp_tcn_unit *tcn)
ac93838f
SS
985{
986 if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
70d9a675 987 (bif->bif_flags & IFBIF_DESIGNATED)) {
ac93838f
SS
988 bstp_topology_change_detection(sc);
989 bstp_acknowledge_topology_change(sc, bif);
990 }
991}
992
3677aae9
MD
993/*
994 * link1 forces continuous hello's (the bridge interface must be cycled
995 * to start them up), so keep the timer hot if that is the case, otherwise
996 * only send HELLO's if we are the root.
997 */
d217f5e5 998static void
ac93838f
SS
999bstp_hello_timer_expiry(struct bridge_softc *sc)
1000{
1001 bstp_config_bpdu_generation(sc);
3677aae9
MD
1002
1003 if ((sc->sc_ifp->if_flags & IFF_LINK1) || bstp_root_bridge(sc))
1004 bstp_timer_start(&sc->sc_hello_timer, 0);
ac93838f
SS
1005}
1006
d217f5e5 1007static void
ac93838f 1008bstp_message_age_timer_expiry(struct bridge_softc *sc,
3677aae9 1009 struct bridge_iflist *bif)
ac93838f 1010{
70d9a675 1011 int iamroot;
ac93838f 1012
70d9a675
MD
1013 iamroot = bstp_root_bridge(sc);
1014 bstp_clear_peer_info(sc, bif);
ac93838f
SS
1015 bstp_configuration_update(sc);
1016 bstp_port_state_selection(sc);
1017
3677aae9
MD
1018 /*
1019 * If we've become the root and were not the root before
1020 * we have some cleanup to do. This also occurs if we
1021 * wind up being completely isolated.
1022 */
70d9a675 1023 if (iamroot == 0 && bstp_root_bridge(sc)) {
ac93838f
SS
1024 sc->sc_max_age = sc->sc_bridge_max_age;
1025 sc->sc_hello_time = sc->sc_bridge_hello_time;
1026 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
1027
1028 bstp_topology_change_detection(sc);
1029 bstp_timer_stop(&sc->sc_tcn_timer);
1030 bstp_config_bpdu_generation(sc);
1031 bstp_timer_start(&sc->sc_hello_timer, 0);
1032 }
1033}
1034
d217f5e5 1035static void
ac93838f 1036bstp_forward_delay_timer_expiry(struct bridge_softc *sc,
70d9a675 1037 struct bridge_iflist *bif)
ac93838f
SS
1038{
1039 if (bif->bif_state == BSTP_IFSTATE_LISTENING) {
1040 bstp_set_port_state(bif, BSTP_IFSTATE_LEARNING);
1041 bstp_timer_start(&bif->bif_forward_delay_timer, 0);
1042 } else if (bif->bif_state == BSTP_IFSTATE_LEARNING) {
1043 bstp_set_port_state(bif, BSTP_IFSTATE_FORWARDING);
70d9a675
MD
1044 if (sc->sc_designated_bridge == sc->sc_bridge_id &&
1045 bif->bif_change_detection_enabled) {
ac93838f 1046 bstp_topology_change_detection(sc);
70d9a675 1047 }
1e858374
MD
1048 if (sc->sc_ifp->if_flags & IFF_LINK2)
1049 bstp_adjust_bonded_states(sc, bif);
ac93838f
SS
1050 }
1051}
1052
d217f5e5 1053static void
ac93838f
SS
1054bstp_tcn_timer_expiry(struct bridge_softc *sc)
1055{
1056 bstp_transmit_tcn(sc);
1057 bstp_timer_start(&sc->sc_tcn_timer, 0);
1058}
1059
d217f5e5 1060static void
ac93838f
SS
1061bstp_topology_change_timer_expiry(struct bridge_softc *sc)
1062{
1063 sc->sc_topology_change_detected = 0;
1064 sc->sc_topology_change = 0;
1065}
1066
d217f5e5 1067static void
ac93838f
SS
1068bstp_hold_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif)
1069{
1070 if (bif->bif_config_pending)
1071 bstp_transmit_config(sc, bif);
1072}
1073
3677aae9
MD
1074/*
1075 * If no traffic received directly on this port for the specified
1076 * period with link1 set we go into a special blocking mode to
1077 * fail-over traffic to another port.
1078 */
1079static void
1080bstp_link1_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif)
1081{
1082 if (sc->sc_ifp->if_flags & IFF_LINK1)
1083 bstp_make_l1blocking(sc, bif);
1084}
1085
d217f5e5
SU
1086static int
1087bstp_addr_cmp(const uint8_t *a, const uint8_t *b)
1088{
1089 int i, d;
1090
1091 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
1092 d = ((int)a[i]) - ((int)b[i]);
1093 }
1094
1095 return (d);
1096}
1097
ac93838f
SS
1098void
1099bstp_initialization(struct bridge_softc *sc)
1100{
8f7b13ef 1101 struct bridge_iflist *bif, *mif, *nbif;
d217f5e5 1102 u_char *e_addr;
ac93838f 1103
e9d22060
SZ
1104 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
1105
70d9a675
MD
1106 /*
1107 * Figure out our bridge ID, use the lowest-valued MAC.
1108 * Include the bridge's own random MAC in the calculation.
1109 */
ac93838f 1110 mif = NULL;
70d9a675
MD
1111
1112 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
ac93838f
SS
1113 if ((bif->bif_flags & IFBIF_STP) == 0)
1114 continue;
1115 if (bif->bif_ifp->if_type != IFT_ETHER)
1116 continue;
ac93838f
SS
1117 if (mif == NULL) {
1118 mif = bif;
1119 continue;
1120 }
70d9a675
MD
1121
1122 bif->bif_port_id = (bif->bif_priority << 8) |
1123 (bif->bif_ifp->if_index & 0xff);
d217f5e5 1124 if (bstp_addr_cmp(IF_LLADDR(bif->bif_ifp),
70d9a675 1125 IF_LLADDR(mif->bif_ifp)) < 0) {
ac93838f
SS
1126 mif = bif;
1127 continue;
1128 }
1129 }
1130 if (mif == NULL) {
1131 bstp_stop(sc);
1132 return;
1133 }
1134
70d9a675
MD
1135 if (bstp_addr_cmp(IF_LLADDR(sc->sc_ifp), IF_LLADDR(mif->bif_ifp)) < 0)
1136 e_addr = IF_LLADDR(sc->sc_ifp);
1137 else
1138 e_addr = IF_LLADDR(mif->bif_ifp);
1139
ac93838f
SS
1140 sc->sc_bridge_id =
1141 (((uint64_t)sc->sc_bridge_priority) << 48) |
d217f5e5
SU
1142 (((uint64_t)e_addr[0]) << 40) |
1143 (((uint64_t)e_addr[1]) << 32) |
1144 (((uint64_t)e_addr[2]) << 24) |
1145 (((uint64_t)e_addr[3]) << 16) |
1146 (((uint64_t)e_addr[4]) << 8) |
1147 (((uint64_t)e_addr[5]));
ac93838f 1148
70d9a675
MD
1149 /*
1150 * Remainder of setup.
1151 */
1152
ac93838f 1153 sc->sc_designated_root = sc->sc_bridge_id;
70d9a675 1154 sc->sc_designated_cost = 0;
ac93838f
SS
1155 sc->sc_root_port = NULL;
1156
1157 sc->sc_max_age = sc->sc_bridge_max_age;
1158 sc->sc_hello_time = sc->sc_bridge_hello_time;
1159 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
1160 sc->sc_topology_change_detected = 0;
1161 sc->sc_topology_change = 0;
1162 bstp_timer_stop(&sc->sc_tcn_timer);
1163 bstp_timer_stop(&sc->sc_topology_change_timer);
1164
1165 if (callout_pending(&sc->sc_bstpcallout) == 0)
1166 callout_reset(&sc->sc_bstpcallout, hz,
1167 bstp_tick, sc);
1168
70d9a675 1169 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
3677aae9
MD
1170 if (sc->sc_ifp->if_flags & IFF_LINK1)
1171 bstp_timer_start(&bif->bif_link1_timer, 0);
ac93838f
SS
1172 if (bif->bif_flags & IFBIF_STP)
1173 bstp_ifupdstatus(sc, bif);
1174 else
1175 bstp_disable_port(sc, bif);
8f7b13ef
SZ
1176
1177 if (nbif != NULL && !nbif->bif_onlist) {
1178 KKASSERT(bif->bif_onlist);
70d9a675 1179 nbif = TAILQ_NEXT(bif, bif_next);
8f7b13ef 1180 }
ac93838f
SS
1181 }
1182
1183 bstp_port_state_selection(sc);
1184 bstp_config_bpdu_generation(sc);
1185 bstp_timer_start(&sc->sc_hello_timer, 0);
1186}
1187
1188void
1189bstp_stop(struct bridge_softc *sc)
1190{
1191 struct bridge_iflist *bif;
e9d22060
SZ
1192 struct lwkt_msg *lmsg;
1193
1194 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
ac93838f 1195
70d9a675 1196 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
ac93838f
SS
1197 bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED);
1198 bstp_timer_stop(&bif->bif_hold_timer);
1199 bstp_timer_stop(&bif->bif_message_age_timer);
1200 bstp_timer_stop(&bif->bif_forward_delay_timer);
3677aae9 1201 bstp_timer_stop(&bif->bif_link1_timer);
ac93838f
SS
1202 }
1203
1204 callout_stop(&sc->sc_bstpcallout);
1205
1206 bstp_timer_stop(&sc->sc_topology_change_timer);
1207 bstp_timer_stop(&sc->sc_tcn_timer);
1208 bstp_timer_stop(&sc->sc_hello_timer);
1209
e9d22060 1210 crit_enter();
002c1265 1211 lmsg = &sc->sc_bstptimemsg.lmsg;
e9d22060
SZ
1212 if ((lmsg->ms_flags & MSGF_DONE) == 0) {
1213 /* Pending to be processed; drop it */
1214 lwkt_dropmsg(lmsg);
1215 }
1216 crit_exit();
ac93838f
SS
1217}
1218
d217f5e5 1219static void
ac93838f
SS
1220bstp_initialize_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1221{
1e858374
MD
1222 int needs_adjust = (bif->bif_state == BSTP_IFSTATE_FORWARDING ||
1223 bif->bif_state == BSTP_IFSTATE_BLOCKING ||
1224 bif->bif_state == BSTP_IFSTATE_BONDED);
1225
ac93838f 1226 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
70d9a675 1227 bstp_clear_peer_info(sc, bif);
ac93838f
SS
1228 bif->bif_topology_change_acknowledge = 0;
1229 bif->bif_config_pending = 0;
1230 bif->bif_change_detection_enabled = 1;
1231 bstp_timer_stop(&bif->bif_message_age_timer);
1232 bstp_timer_stop(&bif->bif_forward_delay_timer);
1233 bstp_timer_stop(&bif->bif_hold_timer);
3677aae9 1234 bstp_timer_stop(&bif->bif_link1_timer);
1e858374
MD
1235 if (needs_adjust && (sc->sc_ifp->if_flags & IFF_LINK2))
1236 bstp_adjust_bonded_states(sc, bif);
ac93838f
SS
1237}
1238
d217f5e5 1239static void
ac93838f
SS
1240bstp_enable_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1241{
1242 bstp_initialize_port(sc, bif);
3677aae9
MD
1243 if (sc->sc_ifp->if_flags & IFF_LINK1)
1244 bstp_timer_start(&bif->bif_link1_timer, 0);
9b42fdc9 1245 bstp_configuration_update(sc);
ac93838f
SS
1246 bstp_port_state_selection(sc);
1247}
1248
d217f5e5 1249static void
ac93838f
SS
1250bstp_disable_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1251{
1e858374 1252 int was_forwarding = (bif->bif_state == BSTP_IFSTATE_FORWARDING);
70d9a675 1253 int iamroot;
ac93838f 1254
70d9a675
MD
1255 iamroot = bstp_root_bridge(sc);
1256
1257 bstp_clear_peer_info(sc, bif);
ac93838f
SS
1258 bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED);
1259 bif->bif_topology_change_acknowledge = 0;
1260 bif->bif_config_pending = 0;
1261 bstp_timer_stop(&bif->bif_message_age_timer);
1262 bstp_timer_stop(&bif->bif_forward_delay_timer);
3677aae9 1263 bstp_timer_stop(&bif->bif_link1_timer);
ac93838f
SS
1264 bstp_configuration_update(sc);
1265 bstp_port_state_selection(sc);
30ced003 1266 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
1e858374
MD
1267 if (was_forwarding && (sc->sc_ifp->if_flags & IFF_LINK2))
1268 bstp_adjust_bonded_states(sc, bif);
ac93838f 1269
70d9a675 1270 if (iamroot == 0 && bstp_root_bridge(sc)) {
ac93838f
SS
1271 sc->sc_max_age = sc->sc_bridge_max_age;
1272 sc->sc_hello_time = sc->sc_bridge_hello_time;
1273 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
1274
1275 bstp_topology_change_detection(sc);
1276 bstp_timer_stop(&sc->sc_tcn_timer);
1277 bstp_config_bpdu_generation(sc);
1278 bstp_timer_start(&sc->sc_hello_timer, 0);
1279 }
1280}
1281
ac93838f
SS
1282void
1283bstp_linkstate(struct ifnet *ifp, int state)
1284{
1285 struct bridge_softc *sc;
1286 struct bridge_iflist *bif;
1287
1288 sc = ifp->if_bridge;
a3dd34d2 1289 ifnet_serialize_all(sc->sc_ifp);
ac93838f 1290
8f7b13ef
SZ
1291 /*
1292 * bstp_ifupdstatus() may block, but it is the last
1293 * operation of the member iface iteration, so we
1294 * don't need to use LIST_FOREACH_MUTABLE()+bif_onlist
1295 * check here.
1296 */
70d9a675 1297 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
ac93838f
SS
1298 if ((bif->bif_flags & IFBIF_STP) == 0)
1299 continue;
1300
1301 if (bif->bif_ifp == ifp) {
1302 bstp_ifupdstatus(sc, bif);
1303 break;
1304 }
1305 }
a3dd34d2 1306 ifnet_deserialize_all(sc->sc_ifp);
ac93838f
SS
1307}
1308
d217f5e5 1309static void
ac93838f
SS
1310bstp_ifupdstatus(struct bridge_softc *sc, struct bridge_iflist *bif)
1311{
1312 struct ifnet *ifp = bif->bif_ifp;
1313 struct ifmediareq ifmr;
1314 int error = 0;
1315
1316 bzero((char *)&ifmr, sizeof(ifmr));
a3dd34d2 1317 ifnet_serialize_all(ifp);
d217f5e5 1318 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL);
a3dd34d2 1319 ifnet_deserialize_all(ifp);
ac93838f
SS
1320
1321 if ((error == 0) && (ifp->if_flags & IFF_UP)) {
1322 if (ifmr.ifm_status & IFM_ACTIVE) {
1323 if (bif->bif_state == BSTP_IFSTATE_DISABLED)
1324 bstp_enable_port(sc, bif);
1325
1326 } else {
1327 if (bif->bif_state != BSTP_IFSTATE_DISABLED)
1328 bstp_disable_port(sc, bif);
1329 }
1330 return;
1331 }
1332
1333 if (bif->bif_state != BSTP_IFSTATE_DISABLED)
1334 bstp_disable_port(sc, bif);
1335}
1336
d217f5e5 1337static void
ac93838f
SS
1338bstp_tick(void *arg)
1339{
1340 struct bridge_softc *sc = arg;
e9d22060
SZ
1341 struct lwkt_msg *lmsg;
1342
1343 KKASSERT(mycpuid == BRIDGE_CFGCPU);
1344
1345 crit_enter();
1346
1347 if (callout_pending(&sc->sc_bstpcallout) ||
1348 !callout_active(&sc->sc_bstpcallout)) {
1349 crit_exit();
1350 return;
1351 }
1352 callout_deactivate(&sc->sc_bstpcallout);
1353
002c1265 1354 lmsg = &sc->sc_bstptimemsg.lmsg;
e9d22060
SZ
1355 KKASSERT(lmsg->ms_flags & MSGF_DONE);
1356 lwkt_sendmsg(BRIDGE_CFGPORT, lmsg);
1357
1358 crit_exit();
1359}
1360
1361void
002c1265 1362bstp_tick_handler(netmsg_t msg)
e9d22060 1363{
002c1265 1364 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
ac93838f
SS
1365 struct bridge_iflist *bif;
1366
e9d22060
SZ
1367 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
1368 crit_enter();
1369 /* Reply ASAP */
002c1265 1370 lwkt_replymsg(&msg->lmsg, 0);
e9d22060
SZ
1371 crit_exit();
1372
a3dd34d2 1373 ifnet_serialize_all(sc->sc_ifp);
ac93838f 1374
8f7b13ef
SZ
1375 /*
1376 * NOTE:
1377 * We don't need to worry that member iface is ripped
1378 * from the per-cpu list during the blocking operation
1379 * in the loop body, since deletion is serialized by
1380 * BRIDGE_CFGPORT
1381 */
1382
70d9a675 1383 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
ac93838f
SS
1384 if ((bif->bif_flags & IFBIF_STP) == 0)
1385 continue;
1386 /*
1387 * XXX This can cause a lag in "link does away"
1388 * XXX and "spanning tree gets updated". We need
1389 * XXX come sort of callback from the link state
1390 * XXX update code to kick spanning tree.
1391 * XXX --thorpej@NetBSD.org
1392 */
1393 bstp_ifupdstatus(sc, bif);
1394 }
ac93838f
SS
1395
1396 if (bstp_timer_expired(&sc->sc_hello_timer, sc->sc_hello_time))
1397 bstp_hello_timer_expiry(sc);
1398
1399 if (bstp_timer_expired(&sc->sc_tcn_timer, sc->sc_bridge_hello_time))
1400 bstp_tcn_timer_expiry(sc);
1401
1402 if (bstp_timer_expired(&sc->sc_topology_change_timer,
1403 sc->sc_topology_change_time))
1404 bstp_topology_change_timer_expiry(sc);
1405
70d9a675 1406 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
ac93838f
SS
1407 if ((bif->bif_flags & IFBIF_STP) == 0)
1408 continue;
1409 if (bstp_timer_expired(&bif->bif_message_age_timer,
1410 sc->sc_max_age))
1411 bstp_message_age_timer_expiry(sc, bif);
1412 }
1413
70d9a675 1414 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
ac93838f
SS
1415 if ((bif->bif_flags & IFBIF_STP) == 0)
1416 continue;
1417 if (bstp_timer_expired(&bif->bif_forward_delay_timer,
1418 sc->sc_forward_delay))
1419 bstp_forward_delay_timer_expiry(sc, bif);
1420
1421 if (bstp_timer_expired(&bif->bif_hold_timer,
1422 sc->sc_hold_time))
1423 bstp_hold_timer_expiry(sc, bif);
3677aae9
MD
1424
1425 if (bstp_timer_expired(&bif->bif_link1_timer,
1426 sc->sc_hello_time * 10))
1427 bstp_link1_timer_expiry(sc, bif);
ac93838f
SS
1428 }
1429
1430 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1431 callout_reset(&sc->sc_bstpcallout, hz, bstp_tick, sc);
1432
a3dd34d2 1433 ifnet_deserialize_all(sc->sc_ifp);
ac93838f
SS
1434}
1435
d217f5e5 1436static void
ac93838f
SS
1437bstp_timer_start(struct bridge_timer *t, uint16_t v)
1438{
1439 t->value = v;
1440 t->active = 1;
1441}
1442
d217f5e5 1443static void
ac93838f
SS
1444bstp_timer_stop(struct bridge_timer *t)
1445{
1446 t->value = 0;
1447 t->active = 0;
1448}
1449
d217f5e5 1450static int
ac93838f
SS
1451bstp_timer_expired(struct bridge_timer *t, uint16_t v)
1452{
1453 if (t->active == 0)
1454 return (0);
1455 t->value += BSTP_TICK_VAL;
1456 if (t->value >= v) {
1457 bstp_timer_stop(t);
1458 return (1);
1459 }
1460 return (0);
1461
1462}