f3a46902864feddab4d294a37ca8c2f83161abf6
[dragonfly.git] / sys / net / bridge / bridgestp.c
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 $
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
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>
53 #include <sys/msgport2.h>
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 */
85 struct 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 */
112 struct 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
121 const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
122
123 static void     bstp_initialize_port(struct bridge_softc *,
124                     struct bridge_iflist *);
125 static void     bstp_ifupdstatus(struct bridge_softc *, struct bridge_iflist *);
126 static void     bstp_enable_port(struct bridge_softc *, struct bridge_iflist *);
127 static void     bstp_disable_port(struct bridge_softc *,
128                     struct bridge_iflist *);
129 #ifdef notused
130 static void     bstp_enable_change_detection(struct bridge_iflist *);
131 static void     bstp_disable_change_detection(struct bridge_iflist *);
132 #endif /* notused */
133 static int      bstp_root_bridge(struct bridge_softc *sc);
134 static int      bstp_supersedes_port_info(struct bridge_softc *,
135                     struct bridge_iflist *, struct bstp_config_unit *);
136 static int      bstp_designated_port(struct bridge_softc *,
137                     struct bridge_iflist *);
138 static int      bstp_designated_for_some_port(struct bridge_softc *);
139 static void     bstp_transmit_config(struct bridge_softc *,
140                     struct bridge_iflist *);
141 static void     bstp_transmit_tcn(struct bridge_softc *);
142 static void     bstp_received_config_bpdu(struct bridge_softc *,
143                     struct bridge_iflist *, struct bstp_config_unit *);
144 static void     bstp_received_tcn_bpdu(struct bridge_softc *,
145                     struct bridge_iflist *, struct bstp_tcn_unit *);
146 static void     bstp_record_config_information(struct bridge_softc *,
147                     struct bridge_iflist *, struct bstp_config_unit *);
148 static void     bstp_record_config_timeout_values(struct bridge_softc *,
149                     struct bstp_config_unit *);
150 static void     bstp_config_bpdu_generation(struct bridge_softc *);
151 static void     bstp_send_config_bpdu(struct bridge_softc *,
152                     struct bridge_iflist *, struct bstp_config_unit *);
153 static void     bstp_configuration_update(struct bridge_softc *);
154 static void     bstp_root_selection(struct bridge_softc *);
155 static void     bstp_designated_port_selection(struct bridge_softc *);
156 static void     bstp_become_designated_port(struct bridge_softc *,
157                     struct bridge_iflist *);
158 static void     bstp_port_state_selection(struct bridge_softc *);
159 static void     bstp_make_forwarding(struct bridge_softc *,
160                     struct bridge_iflist *);
161 static void     bstp_make_blocking(struct bridge_softc *,
162                     struct bridge_iflist *);
163 static void     bstp_make_l1blocking(struct bridge_softc *sc,
164                     struct bridge_iflist *bif);
165 static void     bstp_set_port_state(struct bridge_iflist *, uint8_t);
166 #ifdef notused
167 static void     bstp_set_bridge_priority(struct bridge_softc *, uint64_t);
168 static void     bstp_set_port_priority(struct bridge_softc *,
169                     struct bridge_iflist *, uint16_t);
170 static void     bstp_set_path_cost(struct bridge_softc *,
171                     struct bridge_iflist *, uint32_t);
172 #endif /* notused */
173 static void     bstp_topology_change_detection(struct bridge_softc *);
174 static void     bstp_topology_change_acknowledged(struct bridge_softc *);
175 static void     bstp_acknowledge_topology_change(struct bridge_softc *,
176                     struct bridge_iflist *);
177
178 static void     bstp_tick(void *);
179 static void     bstp_timer_start(struct bridge_timer *, uint16_t);
180 static void     bstp_timer_stop(struct bridge_timer *);
181 static int      bstp_timer_expired(struct bridge_timer *, uint16_t);
182
183 static void     bstp_hold_timer_expiry(struct bridge_softc *,
184                     struct bridge_iflist *);
185 static void     bstp_message_age_timer_expiry(struct bridge_softc *,
186                     struct bridge_iflist *);
187 static void     bstp_forward_delay_timer_expiry(struct bridge_softc *,
188                     struct bridge_iflist *);
189 static void     bstp_topology_change_timer_expiry(struct bridge_softc *);
190 static void     bstp_tcn_timer_expiry(struct bridge_softc *);
191 static void     bstp_hello_timer_expiry(struct bridge_softc *);
192 static int      bstp_addr_cmp(const uint8_t *, const uint8_t *);
193
194 static void
195 bstp_transmit_config(struct bridge_softc *sc, struct bridge_iflist *bif)
196 {
197         if (bif->bif_hold_timer.active) {
198                 bif->bif_config_pending = 1;
199                 return;
200         }
201
202         bif->bif_config_bpdu.cu_message_type = BSTP_MSGTYPE_CFG;
203         bif->bif_config_bpdu.cu_rootid = sc->sc_designated_root;
204         bif->bif_config_bpdu.cu_root_path_cost = sc->sc_root_path_cost;
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
208         if (bstp_root_bridge(sc)) {
209                 bif->bif_config_bpdu.cu_message_age = 0;
210         } else {
211                 bif->bif_config_bpdu.cu_message_age =
212                     sc->sc_root_port->bifi_message_age_timer.value +
213                     BSTP_MESSAGE_AGE_INCR;
214         }
215
216         bif->bif_config_bpdu.cu_max_age = sc->sc_max_age;
217         bif->bif_config_bpdu.cu_hello_time = sc->sc_hello_time;
218         bif->bif_config_bpdu.cu_forward_delay = sc->sc_forward_delay;
219         bif->bif_config_bpdu.cu_topology_change_acknowledgment
220             = bif->bif_topology_change_acknowledge;
221         bif->bif_config_bpdu.cu_topology_change = sc->sc_topology_change;
222
223         if (bif->bif_config_bpdu.cu_message_age < sc->sc_max_age ||
224             (sc->sc_ifp->if_flags & IFF_LINK1)) {
225                 bif->bif_topology_change_acknowledge = 0;
226                 bif->bif_config_pending = 0;
227                 bstp_send_config_bpdu(sc, bif, &bif->bif_config_bpdu);
228                 bstp_timer_start(&bif->bif_hold_timer, 0);
229         }
230 }
231
232 static void
233 bstp_send_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
234                       struct bstp_config_unit *cu)
235 {
236         struct ifnet *ifp;
237         struct mbuf *m;
238         struct ether_header *eh;
239         struct bstp_cbpdu bpdu;
240
241         ifp = bif->bif_ifp;
242
243         if ((ifp->if_flags & IFF_RUNNING) == 0)
244                 return;
245
246         MGETHDR(m, MB_DONTWAIT, MT_DATA);
247         if (m == NULL)
248                 return;
249
250         eh = mtod(m, struct ether_header *);
251
252         m->m_pkthdr.rcvif = ifp;
253         m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
254         m->m_len = m->m_pkthdr.len;
255
256         bpdu.cbu_ssap = bpdu.cbu_dsap = LLC_8021D_LSAP;
257         bpdu.cbu_ctl = LLC_UI;
258         bpdu.cbu_protoid = htons(0);
259         bpdu.cbu_protover = 0;
260         bpdu.cbu_bpdutype = cu->cu_message_type;
261         bpdu.cbu_flags = (cu->cu_topology_change ? BSTP_FLAG_TC : 0) |
262             (cu->cu_topology_change_acknowledgment ? BSTP_FLAG_TCA : 0);
263
264         bpdu.cbu_rootpri = htons(cu->cu_rootid >> 48);
265         bpdu.cbu_rootaddr[0] = cu->cu_rootid >> 40;
266         bpdu.cbu_rootaddr[1] = cu->cu_rootid >> 32;
267         bpdu.cbu_rootaddr[2] = cu->cu_rootid >> 24;
268         bpdu.cbu_rootaddr[3] = cu->cu_rootid >> 16;
269         bpdu.cbu_rootaddr[4] = cu->cu_rootid >> 8;
270         bpdu.cbu_rootaddr[5] = cu->cu_rootid >> 0;
271
272         bpdu.cbu_rootpathcost = htonl(cu->cu_root_path_cost);
273
274         bpdu.cbu_bridgepri = htons(cu->cu_bridge_id >> 48);
275         bpdu.cbu_bridgeaddr[0] = cu->cu_bridge_id >> 40;
276         bpdu.cbu_bridgeaddr[1] = cu->cu_bridge_id >> 32;
277         bpdu.cbu_bridgeaddr[2] = cu->cu_bridge_id >> 24;
278         bpdu.cbu_bridgeaddr[3] = cu->cu_bridge_id >> 16;
279         bpdu.cbu_bridgeaddr[4] = cu->cu_bridge_id >> 8;
280         bpdu.cbu_bridgeaddr[5] = cu->cu_bridge_id >> 0;
281
282         bpdu.cbu_portid = htons(cu->cu_port_id);
283         bpdu.cbu_messageage = htons(cu->cu_message_age);
284         bpdu.cbu_maxage = htons(cu->cu_max_age);
285         bpdu.cbu_hellotime = htons(cu->cu_hello_time);
286         bpdu.cbu_forwarddelay = htons(cu->cu_forward_delay);
287
288         memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
289         memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
290         eh->ether_type = htons(sizeof(bpdu));
291
292         memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
293
294         bridge_enqueue(ifp, m);
295 }
296
297 static int
298 bstp_root_bridge(struct bridge_softc *sc)
299 {
300         return (sc->sc_designated_root == sc->sc_bridge_id);
301 }
302
303 static int
304 bstp_supersedes_port_info(struct bridge_softc *sc, struct bridge_iflist *bif,
305     struct bstp_config_unit *cu)
306 {
307         if (cu->cu_rootid < bif->bif_designated_root)
308                 return (1);
309         if (cu->cu_rootid > bif->bif_designated_root)
310                 return (0);
311
312         if (cu->cu_root_path_cost < bif->bif_designated_cost)
313                 return (1);
314         if (cu->cu_root_path_cost > bif->bif_designated_cost)
315                 return (0);
316
317         if (cu->cu_bridge_id < bif->bif_designated_bridge)
318                 return (1);
319         if (cu->cu_bridge_id > bif->bif_designated_bridge)
320                 return (0);
321
322         if (sc->sc_bridge_id != cu->cu_bridge_id)
323                 return (1);
324         if (cu->cu_port_id <= bif->bif_designated_port)
325                 return (1);
326         return (0);
327 }
328
329 static void
330 bstp_record_config_information(struct bridge_softc *sc,
331     struct bridge_iflist *bif, struct bstp_config_unit *cu)
332 {
333         bif->bif_designated_root = cu->cu_rootid;
334         bif->bif_designated_cost = cu->cu_root_path_cost;
335         bif->bif_designated_bridge = cu->cu_bridge_id;
336         bif->bif_designated_port = cu->cu_port_id;
337         bstp_timer_start(&bif->bif_message_age_timer, cu->cu_message_age);
338 }
339
340 static void
341 bstp_record_config_timeout_values(struct bridge_softc *sc,
342     struct bstp_config_unit *config)
343 {
344         sc->sc_max_age = config->cu_max_age;
345         sc->sc_hello_time = config->cu_hello_time;
346         sc->sc_forward_delay = config->cu_forward_delay;
347         sc->sc_topology_change = config->cu_topology_change;
348 }
349
350 static void
351 bstp_config_bpdu_generation(struct bridge_softc *sc)
352 {
353         struct bridge_iflist *bif, *nbif;
354
355         LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
356                 if ((bif->bif_flags & IFBIF_STP) == 0)
357                         continue;
358                 if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
359                     ((sc->sc_ifp->if_flags & IFF_LINK1) ||
360                      bstp_designated_port(sc, bif))) {
361                         bstp_transmit_config(sc, bif);
362                 }
363
364                 if (nbif != NULL && !nbif->bif_onlist) {
365                         KKASSERT(bif->bif_onlist);
366                         nbif = LIST_NEXT(bif, bif_next);
367                 }
368         }
369 }
370
371 static int
372 bstp_designated_port(struct bridge_softc *sc, struct bridge_iflist *bif)
373 {
374         return ((bif->bif_designated_bridge == sc->sc_bridge_id)
375             && (bif->bif_designated_port == bif->bif_port_id));
376 }
377
378 static void
379 bstp_transmit_tcn(struct bridge_softc *sc)
380 {
381         struct bstp_tbpdu bpdu;
382         struct ifnet *ifp = sc->sc_root_port->bifi_ifp;
383         struct ether_header *eh;
384         struct mbuf *m;
385
386         if ((ifp->if_flags & IFF_RUNNING) == 0)
387                 return;
388
389         MGETHDR(m, MB_DONTWAIT, MT_DATA);
390         if (m == NULL)
391                 return;
392
393         m->m_pkthdr.rcvif = ifp;
394         m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
395         m->m_len = m->m_pkthdr.len;
396
397         eh = mtod(m, struct ether_header *);
398
399         memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
400         memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
401         eh->ether_type = htons(sizeof(bpdu));
402
403         bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP;
404         bpdu.tbu_ctl = LLC_UI;
405         bpdu.tbu_protoid = 0;
406         bpdu.tbu_protover = 0;
407         bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN;
408
409         memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
410
411         bridge_enqueue(ifp, m);
412 }
413
414 static void
415 bstp_configuration_update(struct bridge_softc *sc)
416 {
417         bstp_root_selection(sc);
418         bstp_designated_port_selection(sc);
419 }
420
421 static void
422 bstp_root_selection(struct bridge_softc *sc)
423 {
424         struct bridge_iflist *root_port = NULL, *bif;
425
426         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
427                 if ((bif->bif_flags & IFBIF_STP) == 0)
428                         continue;
429                 if (bstp_designated_port(sc, bif))
430                         continue;
431                 if (bif->bif_state == BSTP_IFSTATE_DISABLED)
432                         continue;
433                 if (bif->bif_designated_root >= sc->sc_bridge_id)
434                         continue;
435                 if (root_port == NULL)
436                         goto set_port;
437
438                 if (bif->bif_designated_root < root_port->bif_designated_root)
439                         goto set_port;
440                 if (bif->bif_designated_root > root_port->bif_designated_root)
441                         continue;
442
443                 if ((bif->bif_designated_cost + bif->bif_path_cost) <
444                     (root_port->bif_designated_cost + root_port->bif_path_cost))
445                         goto set_port;
446                 if ((bif->bif_designated_cost + bif->bif_path_cost) >
447                     (root_port->bif_designated_cost + root_port->bif_path_cost))
448                         continue;
449
450                 if (bif->bif_designated_bridge <
451                     root_port->bif_designated_bridge)
452                         goto set_port;
453                 if (bif->bif_designated_bridge >
454                     root_port->bif_designated_bridge)
455                         continue;
456
457                 if (bif->bif_designated_port < root_port->bif_designated_port)
458                         goto set_port;
459                 if (bif->bif_designated_port > root_port->bif_designated_port)
460                         continue;
461
462                 if (bif->bif_port_id >= root_port->bif_port_id)
463                         continue;
464 set_port:
465                 root_port = bif;
466         }
467
468         if (root_port == NULL) {
469                 sc->sc_root_port = NULL;
470                 sc->sc_designated_root = sc->sc_bridge_id;
471                 sc->sc_root_path_cost = 0;
472         } else {
473                 sc->sc_root_port = root_port->bif_info;
474                 sc->sc_designated_root = root_port->bif_designated_root;
475                 sc->sc_root_path_cost = root_port->bif_designated_cost +
476                     root_port->bif_path_cost;
477         }
478 }
479
480 static void
481 bstp_designated_port_selection(struct bridge_softc *sc)
482 {
483         struct bridge_iflist *bif;
484
485         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
486                 if ((bif->bif_flags & IFBIF_STP) == 0)
487                         continue;
488                 if (bstp_designated_port(sc, bif))
489                         goto designated;
490                 if (bif->bif_designated_root != sc->sc_designated_root)
491                         goto designated;
492
493                 if (sc->sc_root_path_cost < bif->bif_designated_cost)
494                         goto designated;
495                 if (sc->sc_root_path_cost > bif->bif_designated_cost)
496                         continue;
497
498                 if (sc->sc_bridge_id < bif->bif_designated_bridge)
499                         goto designated;
500                 if (sc->sc_bridge_id > bif->bif_designated_bridge)
501                         continue;
502
503                 if (bif->bif_port_id > bif->bif_designated_port)
504                         continue;
505 designated:
506                 bstp_become_designated_port(sc, bif);
507         }
508 }
509
510 static void
511 bstp_become_designated_port(struct bridge_softc *sc, struct bridge_iflist *bif)
512 {
513         bif->bif_designated_root = sc->sc_designated_root;
514         bif->bif_designated_cost = sc->sc_root_path_cost;
515         bif->bif_designated_bridge = sc->sc_bridge_id;
516         bif->bif_designated_port = bif->bif_port_id;
517 }
518
519 static void
520 bstp_port_state_selection(struct bridge_softc *sc)
521 {
522         struct bridge_iflist *bif, *nbif;
523
524         LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
525                 if ((bif->bif_flags & IFBIF_STP) == 0)
526                         continue;
527                 if (bif->bif_info == sc->sc_root_port) {
528                         bif->bif_config_pending = 0;
529                         bif->bif_topology_change_acknowledge = 0;
530                         bstp_make_forwarding(sc, bif);
531                 } else if (bstp_designated_port(sc, bif)) {
532                         bstp_timer_stop(&bif->bif_message_age_timer);
533                         bstp_make_forwarding(sc, bif);
534                 } else {
535                         bif->bif_config_pending = 0;
536                         bif->bif_topology_change_acknowledge = 0;
537                         bstp_make_blocking(sc, bif);
538                 }
539
540                 if (nbif != NULL && !nbif->bif_onlist) {
541                         KKASSERT(bif->bif_onlist);
542                         nbif = LIST_NEXT(bif, bif_next);
543                 }
544         }
545 }
546
547 static void
548 bstp_make_forwarding(struct bridge_softc *sc, struct bridge_iflist *bif)
549 {
550         if (bif->bif_state == BSTP_IFSTATE_BLOCKING) {
551                 bstp_set_port_state(bif, BSTP_IFSTATE_LISTENING);
552                 bstp_timer_start(&bif->bif_forward_delay_timer, 0);
553         }
554 }
555
556 static void
557 bstp_make_blocking(struct bridge_softc *sc, struct bridge_iflist *bif)
558 {
559         if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
560             bif->bif_state != BSTP_IFSTATE_BLOCKING &&
561             bif->bif_state != BSTP_IFSTATE_L1BLOCKING) {
562                 if ((bif->bif_state == BSTP_IFSTATE_FORWARDING) ||
563                     (bif->bif_state == BSTP_IFSTATE_LEARNING)) {
564                         if (bif->bif_change_detection_enabled) {
565                                 bstp_topology_change_detection(sc);
566                         }
567                 }
568                 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
569                 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
570                 bstp_timer_stop(&bif->bif_forward_delay_timer);
571         }
572 }
573
574 static void
575 bstp_make_l1blocking(struct bridge_softc *sc, struct bridge_iflist *bif)
576 {
577         switch(bif->bif_state) {
578         case BSTP_IFSTATE_LISTENING:
579         case BSTP_IFSTATE_LEARNING:
580         case BSTP_IFSTATE_FORWARDING:
581         case BSTP_IFSTATE_BLOCKING:
582                 bstp_set_port_state(bif, BSTP_IFSTATE_L1BLOCKING);
583                 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
584                 bstp_timer_stop(&bif->bif_forward_delay_timer);
585                 bstp_timer_stop(&bif->bif_link1_timer);
586                 break;
587         default:
588                 break;
589         }
590 }
591
592 static void
593 bstp_set_port_state(struct bridge_iflist *bif, uint8_t state)
594 {
595         bif->bif_state = state;
596 }
597
598 static void
599 bstp_topology_change_detection(struct bridge_softc *sc)
600 {
601         if (bstp_root_bridge(sc)) {
602                 sc->sc_topology_change = 1;
603                 bstp_timer_start(&sc->sc_topology_change_timer, 0);
604         } else if (!sc->sc_topology_change_detected) {
605                 bstp_transmit_tcn(sc);
606                 bstp_timer_start(&sc->sc_tcn_timer, 0);
607         }
608         sc->sc_topology_change_detected = 1;
609 }
610
611 static void
612 bstp_topology_change_acknowledged(struct bridge_softc *sc)
613 {
614         sc->sc_topology_change_detected = 0;
615         bstp_timer_stop(&sc->sc_tcn_timer);
616 }
617
618 static void
619 bstp_acknowledge_topology_change(struct bridge_softc *sc,
620     struct bridge_iflist *bif)
621 {
622         bif->bif_topology_change_acknowledge = 1;
623         bstp_transmit_config(sc, bif);
624 }
625
626 void
627 bstp_input(struct bridge_softc *sc, struct bridge_iflist *bif, struct mbuf *m)
628 {
629         struct ether_header *eh;
630         struct bstp_tbpdu tpdu;
631         struct bstp_cbpdu cpdu;
632         struct bstp_config_unit cu;
633         struct bstp_tcn_unit tu;
634         uint16_t len;
635
636         if ((bif->bif_flags & IFBIF_STP) == 0)
637                 goto out;
638
639         /*
640          * The L1BLOCKING (ping pong failover) test needs to reset the
641          * timer if LINK1 is active.
642          */
643         if (bif->bif_state == BSTP_IFSTATE_L1BLOCKING) {
644                 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
645                 if (sc->sc_ifp->if_flags & IFF_LINK1)
646                         bstp_timer_start(&bif->bif_link1_timer, 0);
647                 bstp_make_forwarding(sc, bif);
648         } else if (sc->sc_ifp->if_flags & IFF_LINK1) {
649                 bstp_timer_start(&bif->bif_link1_timer, 0);
650         }
651
652         eh = mtod(m, struct ether_header *);
653
654         len = ntohs(eh->ether_type);
655         if (len < sizeof(tpdu))
656                 goto out;
657
658         m_adj(m, ETHER_HDR_LEN);
659
660         if (m->m_pkthdr.len > len)
661                 m_adj(m, len - m->m_pkthdr.len);
662         if (m->m_len < sizeof(tpdu) &&
663             (m = m_pullup(m, sizeof(tpdu))) == NULL)
664                 goto out;
665
666         memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu));
667
668         if (tpdu.tbu_dsap != LLC_8021D_LSAP ||
669             tpdu.tbu_ssap != LLC_8021D_LSAP ||
670             tpdu.tbu_ctl != LLC_UI)
671                 goto out;
672         if (tpdu.tbu_protoid != 0 || tpdu.tbu_protover != 0)
673                 goto out;
674
675         switch (tpdu.tbu_bpdutype) {
676         case BSTP_MSGTYPE_TCN:
677                 tu.tu_message_type = tpdu.tbu_bpdutype;
678                 bstp_received_tcn_bpdu(sc, bif, &tu);
679                 break;
680         case BSTP_MSGTYPE_CFG:
681                 if (m->m_len < sizeof(cpdu) &&
682                     (m = m_pullup(m, sizeof(cpdu))) == NULL)
683                         goto out;
684                 memcpy(&cpdu, mtod(m, caddr_t), sizeof(cpdu));
685
686                 cu.cu_rootid =
687                     (((uint64_t)ntohs(cpdu.cbu_rootpri)) << 48) |
688                     (((uint64_t)cpdu.cbu_rootaddr[0]) << 40) |
689                     (((uint64_t)cpdu.cbu_rootaddr[1]) << 32) |
690                     (((uint64_t)cpdu.cbu_rootaddr[2]) << 24) |
691                     (((uint64_t)cpdu.cbu_rootaddr[3]) << 16) |
692                     (((uint64_t)cpdu.cbu_rootaddr[4]) << 8) |
693                     (((uint64_t)cpdu.cbu_rootaddr[5]) << 0);
694
695                 cu.cu_bridge_id =
696                     (((uint64_t)ntohs(cpdu.cbu_bridgepri)) << 48) |
697                     (((uint64_t)cpdu.cbu_bridgeaddr[0]) << 40) |
698                     (((uint64_t)cpdu.cbu_bridgeaddr[1]) << 32) |
699                     (((uint64_t)cpdu.cbu_bridgeaddr[2]) << 24) |
700                     (((uint64_t)cpdu.cbu_bridgeaddr[3]) << 16) |
701                     (((uint64_t)cpdu.cbu_bridgeaddr[4]) << 8) |
702                     (((uint64_t)cpdu.cbu_bridgeaddr[5]) << 0);
703
704                 cu.cu_root_path_cost = ntohl(cpdu.cbu_rootpathcost);
705                 cu.cu_message_age = ntohs(cpdu.cbu_messageage);
706                 cu.cu_max_age = ntohs(cpdu.cbu_maxage);
707                 cu.cu_hello_time = ntohs(cpdu.cbu_hellotime);
708                 cu.cu_forward_delay = ntohs(cpdu.cbu_forwarddelay);
709                 cu.cu_port_id = ntohs(cpdu.cbu_portid);
710                 cu.cu_message_type = cpdu.cbu_bpdutype;
711                 cu.cu_topology_change_acknowledgment =
712                     (cpdu.cbu_flags & BSTP_FLAG_TCA) ? 1 : 0;
713                 cu.cu_topology_change =
714                     (cpdu.cbu_flags & BSTP_FLAG_TC) ? 1 : 0;
715                 bstp_received_config_bpdu(sc, bif, &cu);
716                 break;
717         default:
718                 goto out;
719         }
720 out:
721         if (m)
722                 m_freem(m);
723 }
724
725 static void
726 bstp_received_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
727     struct bstp_config_unit *cu)
728 {
729         int root;
730
731         root = bstp_root_bridge(sc);
732
733         if (bif->bif_state != BSTP_IFSTATE_DISABLED) {
734                 if (bstp_supersedes_port_info(sc, bif, cu)) {
735                         bstp_record_config_information(sc, bif, cu);
736                         bstp_configuration_update(sc);
737                         bstp_port_state_selection(sc);
738
739                         if ((bstp_root_bridge(sc) == 0) && root) {
740                                 /*
741                                  * We continuously transmit hello's if
742                                  * link1 is set (topology change bit will
743                                  * be zero so they shouldn't propagate).
744                                  */
745                                 if ((sc->sc_ifp->if_flags & IFF_LINK1) == 0)
746                                         bstp_timer_stop(&sc->sc_hello_timer);
747
748                                 if (sc->sc_topology_change_detected) {
749                                         bstp_timer_stop(
750                                             &sc->sc_topology_change_timer);
751                                         bstp_transmit_tcn(sc);
752                                         bstp_timer_start(&sc->sc_tcn_timer, 0);
753                                 }
754                         }
755
756                         if (bif->bif_info == sc->sc_root_port) {
757                                 bstp_record_config_timeout_values(sc, cu);
758                                 bstp_config_bpdu_generation(sc);
759
760                                 if (cu->cu_topology_change_acknowledgment)
761                                         bstp_topology_change_acknowledged(sc);
762                         }
763                 } else if (bstp_designated_port(sc, bif))
764                         bstp_transmit_config(sc, bif);
765         }
766 }
767
768 static void
769 bstp_received_tcn_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
770     struct bstp_tcn_unit *tcn)
771 {
772         if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
773             bstp_designated_port(sc, bif)) {
774                 bstp_topology_change_detection(sc);
775                 bstp_acknowledge_topology_change(sc, bif);
776         }
777 }
778
779 /*
780  * link1 forces continuous hello's (the bridge interface must be cycled
781  * to start them up), so keep the timer hot if that is the case, otherwise
782  * only send HELLO's if we are the root.
783  */
784 static void
785 bstp_hello_timer_expiry(struct bridge_softc *sc)
786 {
787         bstp_config_bpdu_generation(sc);
788
789         if ((sc->sc_ifp->if_flags & IFF_LINK1) || bstp_root_bridge(sc))
790                 bstp_timer_start(&sc->sc_hello_timer, 0);
791 }
792
793 static void
794 bstp_message_age_timer_expiry(struct bridge_softc *sc,
795                               struct bridge_iflist *bif)
796 {
797         int root;
798
799         root = bstp_root_bridge(sc);
800         bstp_become_designated_port(sc, bif);
801         bstp_configuration_update(sc);
802         bstp_port_state_selection(sc);
803
804         /*
805          * If we've become the root and were not the root before
806          * we have some cleanup to do.  This also occurs if we
807          * wind up being completely isolated.
808          */
809         if ((bstp_root_bridge(sc)) && (root == 0)) {
810                 sc->sc_max_age = sc->sc_bridge_max_age;
811                 sc->sc_hello_time = sc->sc_bridge_hello_time;
812                 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
813
814                 bstp_topology_change_detection(sc);
815                 bstp_timer_stop(&sc->sc_tcn_timer);
816                 bstp_config_bpdu_generation(sc);
817                 bstp_timer_start(&sc->sc_hello_timer, 0);
818         }
819 }
820
821 static void
822 bstp_forward_delay_timer_expiry(struct bridge_softc *sc,
823     struct bridge_iflist *bif)
824 {
825         if (bif->bif_state == BSTP_IFSTATE_LISTENING) {
826                 bstp_set_port_state(bif, BSTP_IFSTATE_LEARNING);
827                 bstp_timer_start(&bif->bif_forward_delay_timer, 0);
828         } else if (bif->bif_state == BSTP_IFSTATE_LEARNING) {
829                 bstp_set_port_state(bif, BSTP_IFSTATE_FORWARDING);
830                 if (bstp_designated_for_some_port(sc) &&
831                     bif->bif_change_detection_enabled)
832                         bstp_topology_change_detection(sc);
833         }
834 }
835
836 static int
837 bstp_designated_for_some_port(struct bridge_softc *sc)
838 {
839
840         struct bridge_iflist *bif;
841
842         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
843                 if ((bif->bif_flags & IFBIF_STP) == 0)
844                         continue;
845                 if (bif->bif_designated_bridge == sc->sc_bridge_id)
846                         return (1);
847         }
848         return (0);
849 }
850
851 static void
852 bstp_tcn_timer_expiry(struct bridge_softc *sc)
853 {
854         bstp_transmit_tcn(sc);
855         bstp_timer_start(&sc->sc_tcn_timer, 0);
856 }
857
858 static void
859 bstp_topology_change_timer_expiry(struct bridge_softc *sc)
860 {
861         sc->sc_topology_change_detected = 0;
862         sc->sc_topology_change = 0;
863 }
864
865 static void
866 bstp_hold_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif)
867 {
868         if (bif->bif_config_pending)
869                 bstp_transmit_config(sc, bif);
870 }
871
872 /*
873  * If no traffic received directly on this port for the specified
874  * period with link1 set we go into a special blocking mode to
875  * fail-over traffic to another port.
876  */
877 static void
878 bstp_link1_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif)
879 {
880         if (sc->sc_ifp->if_flags & IFF_LINK1)
881                 bstp_make_l1blocking(sc, bif);
882 }
883
884 static int
885 bstp_addr_cmp(const uint8_t *a, const uint8_t *b)
886 {
887         int i, d;
888
889         for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
890                 d = ((int)a[i]) - ((int)b[i]);
891         }
892
893         return (d);
894 }
895
896 void
897 bstp_initialization(struct bridge_softc *sc)
898 {
899         struct bridge_iflist *bif, *mif, *nbif;
900         u_char *e_addr;
901
902         KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
903
904         mif = NULL;
905         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
906                 if ((bif->bif_flags & IFBIF_STP) == 0)
907                         continue;
908                 if (bif->bif_ifp->if_type != IFT_ETHER)
909                         continue;
910                 bif->bif_port_id = (bif->bif_priority << 8) |
911                     (bif->bif_ifp->if_index & 0xff);
912
913                 if (mif == NULL) {
914                         mif = bif;
915                         continue;
916                 }
917                 if (bstp_addr_cmp(IF_LLADDR(bif->bif_ifp),
918                     IF_LLADDR(mif->bif_ifp)) < 0) {
919                         mif = bif;
920                         continue;
921                 }
922         }
923         if (mif == NULL) {
924                 bstp_stop(sc);
925                 return;
926         }
927
928         e_addr = IF_LLADDR(mif->bif_ifp);
929         sc->sc_bridge_id =
930             (((uint64_t)sc->sc_bridge_priority) << 48) |
931             (((uint64_t)e_addr[0]) << 40) |
932             (((uint64_t)e_addr[1]) << 32) |
933             (((uint64_t)e_addr[2]) << 24) |
934             (((uint64_t)e_addr[3]) << 16) |
935             (((uint64_t)e_addr[4]) << 8) |
936             (((uint64_t)e_addr[5]));
937
938         sc->sc_designated_root = sc->sc_bridge_id;
939         sc->sc_root_path_cost = 0;
940         sc->sc_root_port = NULL;
941
942         sc->sc_max_age = sc->sc_bridge_max_age;
943         sc->sc_hello_time = sc->sc_bridge_hello_time;
944         sc->sc_forward_delay = sc->sc_bridge_forward_delay;
945         sc->sc_topology_change_detected = 0;
946         sc->sc_topology_change = 0;
947         bstp_timer_stop(&sc->sc_tcn_timer);
948         bstp_timer_stop(&sc->sc_topology_change_timer);
949
950         if (callout_pending(&sc->sc_bstpcallout) == 0)
951                 callout_reset(&sc->sc_bstpcallout, hz,
952                     bstp_tick, sc);
953
954         LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
955                 if (sc->sc_ifp->if_flags & IFF_LINK1)
956                         bstp_timer_start(&bif->bif_link1_timer, 0);
957                 if (bif->bif_flags & IFBIF_STP)
958                         bstp_ifupdstatus(sc, bif);
959                 else
960                         bstp_disable_port(sc, bif);
961
962                 if (nbif != NULL && !nbif->bif_onlist) {
963                         KKASSERT(bif->bif_onlist);
964                         nbif = LIST_NEXT(bif, bif_next);
965                 }
966         }
967
968         bstp_port_state_selection(sc);
969         bstp_config_bpdu_generation(sc);
970         bstp_timer_start(&sc->sc_hello_timer, 0);
971 }
972
973 void
974 bstp_stop(struct bridge_softc *sc)
975 {
976         struct bridge_iflist *bif;
977         struct lwkt_msg *lmsg;
978
979         KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
980
981         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
982                 bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED);
983                 bstp_timer_stop(&bif->bif_hold_timer);
984                 bstp_timer_stop(&bif->bif_message_age_timer);
985                 bstp_timer_stop(&bif->bif_forward_delay_timer);
986                 bstp_timer_stop(&bif->bif_link1_timer);
987         }
988
989         callout_stop(&sc->sc_bstpcallout);
990
991         bstp_timer_stop(&sc->sc_topology_change_timer);
992         bstp_timer_stop(&sc->sc_tcn_timer);
993         bstp_timer_stop(&sc->sc_hello_timer);
994
995         crit_enter();
996         lmsg = &sc->sc_bstptimemsg.lmsg;
997         if ((lmsg->ms_flags & MSGF_DONE) == 0) {
998                 /* Pending to be processed; drop it */
999                 lwkt_dropmsg(lmsg);
1000         }
1001         crit_exit();
1002 }
1003
1004 static void
1005 bstp_initialize_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1006 {
1007         bstp_become_designated_port(sc, bif);
1008         bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
1009         bif->bif_topology_change_acknowledge = 0;
1010         bif->bif_config_pending = 0;
1011         bif->bif_change_detection_enabled = 1;
1012         bstp_timer_stop(&bif->bif_message_age_timer);
1013         bstp_timer_stop(&bif->bif_forward_delay_timer);
1014         bstp_timer_stop(&bif->bif_hold_timer);
1015         bstp_timer_stop(&bif->bif_link1_timer);
1016 }
1017
1018 static void
1019 bstp_enable_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1020 {
1021         bstp_initialize_port(sc, bif);
1022         if (sc->sc_ifp->if_flags & IFF_LINK1)
1023                 bstp_timer_start(&bif->bif_link1_timer, 0);
1024         bstp_port_state_selection(sc);
1025 }
1026
1027 static void
1028 bstp_disable_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1029 {
1030         int root;
1031
1032         root = bstp_root_bridge(sc);
1033         bstp_become_designated_port(sc, bif);
1034         bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED);
1035         bif->bif_topology_change_acknowledge = 0;
1036         bif->bif_config_pending = 0;
1037         bstp_timer_stop(&bif->bif_message_age_timer);
1038         bstp_timer_stop(&bif->bif_forward_delay_timer);
1039         bstp_timer_stop(&bif->bif_link1_timer);
1040         bstp_configuration_update(sc);
1041         bstp_port_state_selection(sc);
1042         bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
1043
1044         if (bstp_root_bridge(sc) && (root == 0)) {
1045                 sc->sc_max_age = sc->sc_bridge_max_age;
1046                 sc->sc_hello_time = sc->sc_bridge_hello_time;
1047                 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
1048
1049                 bstp_topology_change_detection(sc);
1050                 bstp_timer_stop(&sc->sc_tcn_timer);
1051                 bstp_config_bpdu_generation(sc);
1052                 bstp_timer_start(&sc->sc_hello_timer, 0);
1053         }
1054 }
1055
1056 #ifdef notused
1057 static void
1058 bstp_set_bridge_priority(struct bridge_softc *sc, uint64_t new_bridge_id)
1059 {
1060         struct bridge_iflist *bif;
1061         int root;
1062
1063         root = bstp_root_bridge(sc);
1064
1065         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1066                 if ((bif->bif_flags & IFBIF_STP) == 0)
1067                         continue;
1068                 if (bstp_designated_port(sc, bif))
1069                         bif->bif_designated_bridge = new_bridge_id;
1070         }
1071
1072         sc->sc_bridge_id = new_bridge_id;
1073
1074         bstp_configuration_update(sc);
1075         bstp_port_state_selection(sc);
1076
1077         if (bstp_root_bridge(sc) && (root == 0)) {
1078                 sc->sc_max_age = sc->sc_bridge_max_age;
1079                 sc->sc_hello_time = sc->sc_bridge_hello_time;
1080                 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
1081
1082                 bstp_topology_change_detection(sc);
1083                 bstp_timer_stop(&sc->sc_tcn_timer);
1084                 bstp_config_bpdu_generation(sc);
1085                 bstp_timer_start(&sc->sc_hello_timer, 0);
1086         }
1087 }
1088
1089 static void
1090 bstp_set_port_priority(struct bridge_softc *sc, struct bridge_iflist *bif,
1091     uint16_t new_port_id)
1092 {
1093         if (bstp_designated_port(sc, bif))
1094                 bif->bif_designated_port = new_port_id;
1095
1096         bif->bif_port_id = new_port_id;
1097
1098         if ((sc->sc_bridge_id == bif->bif_designated_bridge) &&
1099             (bif->bif_port_id < bif->bif_designated_port)) {
1100                 bstp_become_designated_port(sc, bif);
1101                 bstp_port_state_selection(sc);
1102         }
1103 }
1104
1105 static void
1106 bstp_set_path_cost(struct bridge_softc *sc, struct bridge_iflist *bif,
1107     uint32_t path_cost)
1108 {
1109         bif->bif_path_cost = path_cost;
1110         bstp_configuration_update(sc);
1111         bstp_port_state_selection(sc);
1112 }
1113
1114 static void
1115 bstp_enable_change_detection(struct bridge_iflist *bif)
1116 {
1117         bif->bif_change_detection_enabled = 1;
1118 }
1119
1120 static void
1121 bstp_disable_change_detection(struct bridge_iflist *bif)
1122 {
1123         bif->bif_change_detection_enabled = 0;
1124 }
1125 #endif /* notused */
1126
1127 void
1128 bstp_linkstate(struct ifnet *ifp, int state)
1129 {
1130         struct bridge_softc *sc;
1131         struct bridge_iflist *bif;
1132
1133         sc = ifp->if_bridge;
1134         ifnet_serialize_all(sc->sc_ifp);
1135
1136         /*
1137          * bstp_ifupdstatus() may block, but it is the last
1138          * operation of the member iface iteration, so we
1139          * don't need to use LIST_FOREACH_MUTABLE()+bif_onlist
1140          * check here.
1141          */
1142         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1143                 if ((bif->bif_flags & IFBIF_STP) == 0)
1144                         continue;
1145
1146                 if (bif->bif_ifp == ifp) {
1147                         bstp_ifupdstatus(sc, bif);
1148                         break;
1149                 }
1150         }
1151         ifnet_deserialize_all(sc->sc_ifp);
1152 }
1153
1154 static void
1155 bstp_ifupdstatus(struct bridge_softc *sc, struct bridge_iflist *bif)
1156 {
1157         struct ifnet *ifp = bif->bif_ifp;
1158         struct ifmediareq ifmr;
1159         int error = 0;
1160
1161         bzero((char *)&ifmr, sizeof(ifmr));
1162         ifnet_serialize_all(ifp);
1163         error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL);
1164         ifnet_deserialize_all(ifp);
1165
1166         if ((error == 0) && (ifp->if_flags & IFF_UP)) {
1167                 if (ifmr.ifm_status & IFM_ACTIVE) {
1168                         if (bif->bif_state == BSTP_IFSTATE_DISABLED)
1169                                 bstp_enable_port(sc, bif);
1170
1171                 } else {
1172                         if (bif->bif_state != BSTP_IFSTATE_DISABLED)
1173                                 bstp_disable_port(sc, bif);
1174                 }
1175                 return;
1176         }
1177
1178         if (bif->bif_state != BSTP_IFSTATE_DISABLED)
1179                 bstp_disable_port(sc, bif);
1180 }
1181
1182 static void
1183 bstp_tick(void *arg)
1184 {
1185         struct bridge_softc *sc = arg;
1186         struct lwkt_msg *lmsg;
1187
1188         KKASSERT(mycpuid == BRIDGE_CFGCPU);
1189
1190         crit_enter();
1191
1192         if (callout_pending(&sc->sc_bstpcallout) ||
1193             !callout_active(&sc->sc_bstpcallout)) {
1194                 crit_exit();
1195                 return;
1196         }
1197         callout_deactivate(&sc->sc_bstpcallout);
1198
1199         lmsg = &sc->sc_bstptimemsg.lmsg;
1200         KKASSERT(lmsg->ms_flags & MSGF_DONE);
1201         lwkt_sendmsg(BRIDGE_CFGPORT, lmsg);
1202
1203         crit_exit();
1204 }
1205
1206 void
1207 bstp_tick_handler(netmsg_t msg)
1208 {
1209         struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
1210         struct bridge_iflist *bif;
1211
1212         KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
1213         crit_enter();
1214         /* Reply ASAP */
1215         lwkt_replymsg(&msg->lmsg, 0);
1216         crit_exit();
1217
1218         ifnet_serialize_all(sc->sc_ifp);
1219
1220         /*
1221          * NOTE:
1222          * We don't need to worry that member iface is ripped
1223          * from the per-cpu list during the blocking operation
1224          * in the loop body, since deletion is serialized by
1225          * BRIDGE_CFGPORT
1226          */
1227
1228         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1229                 if ((bif->bif_flags & IFBIF_STP) == 0)
1230                         continue;
1231                 /*
1232                  * XXX This can cause a lag in "link does away"
1233                  * XXX and "spanning tree gets updated".  We need
1234                  * XXX come sort of callback from the link state
1235                  * XXX update code to kick spanning tree.
1236                  * XXX --thorpej@NetBSD.org
1237                  */
1238                 bstp_ifupdstatus(sc, bif);
1239         }
1240
1241         if (bstp_timer_expired(&sc->sc_hello_timer, sc->sc_hello_time))
1242                 bstp_hello_timer_expiry(sc);
1243
1244         if (bstp_timer_expired(&sc->sc_tcn_timer, sc->sc_bridge_hello_time))
1245                 bstp_tcn_timer_expiry(sc);
1246
1247         if (bstp_timer_expired(&sc->sc_topology_change_timer,
1248             sc->sc_topology_change_time))
1249                 bstp_topology_change_timer_expiry(sc);
1250
1251         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1252                 if ((bif->bif_flags & IFBIF_STP) == 0)
1253                         continue;
1254                 if (bstp_timer_expired(&bif->bif_message_age_timer,
1255                     sc->sc_max_age))
1256                         bstp_message_age_timer_expiry(sc, bif);
1257         }
1258
1259         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1260                 if ((bif->bif_flags & IFBIF_STP) == 0)
1261                         continue;
1262                 if (bstp_timer_expired(&bif->bif_forward_delay_timer,
1263                     sc->sc_forward_delay))
1264                         bstp_forward_delay_timer_expiry(sc, bif);
1265
1266                 if (bstp_timer_expired(&bif->bif_hold_timer,
1267                     sc->sc_hold_time))
1268                         bstp_hold_timer_expiry(sc, bif);
1269
1270                 if (bstp_timer_expired(&bif->bif_link1_timer,
1271                     sc->sc_hello_time * 10))
1272                         bstp_link1_timer_expiry(sc, bif);
1273         }
1274
1275         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1276                 callout_reset(&sc->sc_bstpcallout, hz, bstp_tick, sc);
1277
1278         ifnet_deserialize_all(sc->sc_ifp);
1279 }
1280
1281 static void
1282 bstp_timer_start(struct bridge_timer *t, uint16_t v)
1283 {
1284         t->value = v;
1285         t->active = 1;
1286 }
1287
1288 static void
1289 bstp_timer_stop(struct bridge_timer *t)
1290 {
1291         t->value = 0;
1292         t->active = 0;
1293 }
1294
1295 static int
1296 bstp_timer_expired(struct bridge_timer *t, uint16_t v)
1297 {
1298         if (t->active == 0)
1299                 return (0);
1300         t->value += BSTP_TICK_VAL;
1301         if (t->value >= v) {
1302                 bstp_timer_stop(t);
1303                 return (1);
1304         }
1305         return (0);
1306
1307 }