4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * This module implements a STREAMS driver that provides layer-two (Ethernet)
29 * bridging functionality. The STREAMS interface is used to provide
30 * observability (snoop/wireshark) and control, but not for interface plumbing.
33 #include <sys/types.h>
34 #include <sys/bitmap.h>
35 #include <sys/cmn_err.h>
38 #include <sys/errno.h>
39 #include <sys/kstat.h>
40 #include <sys/modctl.h>
42 #include <sys/param.h>
43 #include <sys/policy.h>
46 #include <sys/stream.h>
47 #include <sys/stropts.h>
48 #include <sys/strsun.h>
49 #include <sys/sunddi.h>
50 #include <sys/sysmacros.h>
51 #include <sys/systm.h>
55 #include <sys/mac_ether.h>
56 #include <sys/mac_provider.h>
57 #include <sys/mac_client_priv.h>
58 #include <sys/mac_impl.h>
60 #include <net/bridge.h>
61 #include <net/bridge_impl.h>
62 #include <net/trill.h>
63 #include <sys/dld_ioc.h>
66 * Locks and reference counts: object lifetime and design.
69 * Bridge mac (snoop) instances are in bmac_list, which is protected by
70 * bmac_rwlock. They're allocated by bmac_alloc and freed by bridge_timer().
71 * Every bridge_inst_t has a single bridge_mac_t, but when bridge_inst_t goes
72 * away, the bridge_mac_t remains until either all of the users go away
73 * (detected by a timer) or until the instance is picked up again by the same
74 * bridge starting back up.
77 * Bridge instances are in inst_list, which is protected by inst_lock.
78 * They're allocated by inst_alloc() and freed by inst_free(). After
79 * allocation, an instance is placed in inst_list, and the reference count is
80 * incremented to represent this. That reference is decremented when the
81 * BIF_SHUTDOWN flag is set, and no new increments may occur. When the last
82 * reference is freed, the instance is removed from the list.
84 * Bridge instances have lists of links and an AVL tree of forwarding
85 * entries. Each of these structures holds one reference on the bridge
86 * instance. These lists and tree are protected by bi_rwlock.
89 * Bridge streams are allocated by stream_alloc() and freed by stream_free().
90 * These streams are created when "bridged" opens /dev/bridgectl, and are
91 * used to create new bridge instances (via BRIOC_NEWBRIDGE) and control the
92 * links on the bridge. When a stream closes, the bridge instance created is
93 * destroyed. There's at most one bridge instance for a given control
97 * Links are allocated by bridge_add_link() and freed by link_free(). The
98 * bi_links list holds a reference to the link. When the BLF_DELETED flag is
99 * set, that reference is dropped. The link isn't removed from the list
100 * until the last reference drops. Each forwarding entry that uses a given
101 * link holds a reference, as does each thread transmitting a packet via the
102 * link. The MAC layer calls in via bridge_ref_cb() to hold a reference on
103 * a link when transmitting.
105 * It's important that once BLF_DELETED is set, there's no way for the
106 * reference count to increase again. If it can, then the link may be
107 * double-freed. The BLF_FREED flag is intended for use with assertions to
108 * guard against this in testing.
111 * Bridge forwarding entries are allocated by bridge_recv_cb() and freed by
112 * fwd_free(). The bi_fwd AVL tree holds one reference to the entry. Unlike
113 * other data structures, the reference is dropped when the entry is removed
114 * from the tree by fwd_delete(), and the BFF_INTREE flag is removed. Each
115 * thread that's forwarding a packet to a known destination holds a reference
116 * to a forwarding entry.
120 * The TRILL module does all of its I/O through bridging. It uses references
121 * on the bridge_inst_t and bridge_link_t structures, and has seven entry
122 * points and four callbacks. One entry point is for setting the callbacks
123 * (bridge_trill_register_cb). There are four entry points for taking bridge
124 * and link references (bridge_trill_{br,ln}{ref,unref}). The final two
125 * entry points are for decapsulated packets from TRILL (bridge_trill_decaps)
126 * that need to be bridged locally, and for TRILL-encapsulated output packets
127 * (bridge_trill_output).
129 * The four callbacks comprise two notification functions for bridges and
130 * links being deleted, one function for raw received TRILL packets, and one
131 * for bridge output to non-local TRILL destinations (tunnel entry).
135 * Ethernet reserved multicast addresses for TRILL; used also in TRILL module.
137 const uint8_t all_isis_rbridges
[] = ALL_ISIS_RBRIDGES
;
138 static const uint8_t all_esadi_rbridges
[] = ALL_ESADI_RBRIDGES
;
139 const uint8_t bridge_group_address
[] = BRIDGE_GROUP_ADDRESS
;
141 static const char *inst_kstats_list
[] = { KSINST_NAMES
};
142 static const char *link_kstats_list
[] = { KSLINK_NAMES
};
144 #define KREF(p, m, vn) p->m.vn.value.ui64
145 #define KINCR(p, m, vn) ++KREF(p, m, vn)
146 #define KDECR(p, m, vn) --KREF(p, m, vn)
148 #define KIPINCR(p, vn) KINCR(p, bi_kstats, vn)
149 #define KIPDECR(p, vn) KDECR(p, bi_kstats, vn)
150 #define KLPINCR(p, vn) KINCR(p, bl_kstats, vn)
152 #define KIINCR(vn) KIPINCR(bip, vn)
153 #define KIDECR(vn) KIPDECR(bip, vn)
154 #define KLINCR(vn) KLPINCR(blp, vn)
156 #define Dim(x) (sizeof (x) / sizeof (*(x)))
158 /* Amount of overhead added when encapsulating with VLAN headers */
159 #define VLAN_INCR (sizeof (struct ether_vlan_header) - \
160 sizeof (struct ether_header))
162 static dev_info_t
*bridge_dev_info
;
163 static major_t bridge_major
;
164 static ddi_taskq_t
*bridge_taskq
;
167 * These are the bridge instance management data structures. The mutex lock
168 * protects the list of bridge instances. A reference count is then used on
169 * each instance to determine when to free it. We use mac_minor_hold() to
170 * allocate minor_t values, which are used both for self-cloning /dev/net/
171 * device nodes as well as client streams. Minor node 0 is reserved for the
172 * allocation control node.
174 static list_t inst_list
;
175 static kcondvar_t inst_cv
; /* Allows us to wait for shutdown */
176 static kmutex_t inst_lock
;
178 static krwlock_t bmac_rwlock
;
179 static list_t bmac_list
;
181 /* Wait for taskq entries that use STREAMS */
182 static kcondvar_t stream_ref_cv
;
183 static kmutex_t stream_ref_lock
;
185 static timeout_id_t bridge_timerid
;
186 static clock_t bridge_scan_interval
;
187 static clock_t bridge_fwd_age
;
189 static bridge_inst_t
*bridge_find_name(const char *);
190 static void bridge_timer(void *);
191 static void bridge_unref(bridge_inst_t
*);
193 static const uint8_t zero_addr
[ETHERADDRL
] = { 0 };
195 /* Global TRILL linkage */
196 static trill_recv_pkt_t trill_recv_fn
;
197 static trill_encap_pkt_t trill_encap_fn
;
198 static trill_br_dstr_t trill_brdstr_fn
;
199 static trill_ln_dstr_t trill_lndstr_fn
;
201 /* special settings to accommodate DLD flow control; see dld_str.c */
202 static struct module_info bridge_dld_modinfo
= {
204 BRIDGE_DEV_NAME
, /* mi_idname */
206 INFPSZ
, /* mi_maxpsz */
211 static struct qinit bridge_dld_rinit
= {
214 dld_open
, /* qi_qopen */
215 dld_close
, /* qi_qclose */
216 NULL
, /* qi_qadmin */
217 &bridge_dld_modinfo
, /* qi_minfo */
221 static struct qinit bridge_dld_winit
= {
222 (int (*)())dld_wput
, /* qi_putp */
223 (int (*)())dld_wsrv
, /* qi_srvp */
225 NULL
, /* qi_qclose */
226 NULL
, /* qi_qadmin */
227 &bridge_dld_modinfo
, /* qi_minfo */
231 static int bridge_ioc_listfwd(void *, intptr_t, int, cred_t
*, int *);
233 /* GLDv3 control ioctls used by Bridging */
234 static dld_ioc_info_t bridge_ioc_list
[] = {
235 {BRIDGE_IOC_LISTFWD
, DLDCOPYINOUT
, sizeof (bridge_listfwd_t
),
236 bridge_ioc_listfwd
, NULL
},
240 * Given a bridge mac pointer, get a ref-held pointer to the corresponding
241 * bridge instance, if any. We must hold the global bmac_rwlock so that
242 * bm_inst doesn't slide out from under us.
244 static bridge_inst_t
*
245 mac_to_inst(const bridge_mac_t
*bmp
)
249 rw_enter(&bmac_rwlock
, RW_READER
);
250 if ((bip
= bmp
->bm_inst
) != NULL
)
251 atomic_inc_uint(&bip
->bi_refs
);
252 rw_exit(&bmac_rwlock
);
257 link_sdu_fail(bridge_link_t
*blp
, boolean_t failed
, mblk_t
**mlist
)
261 bridge_link_t
*blcmp
;
266 if (blp
->bl_flags
& BLF_SDUFAIL
)
268 blp
->bl_flags
|= BLF_SDUFAIL
;
270 if (!(blp
->bl_flags
& BLF_SDUFAIL
))
272 blp
->bl_flags
&= ~BLF_SDUFAIL
;
276 * If this link is otherwise up, then check if there are any other
277 * non-failed non-down links. If not, then we control the state of the
282 if (blp
->bl_linkstate
!= LINK_STATE_DOWN
) {
283 for (blcmp
= list_head(&bip
->bi_links
); blcmp
!= NULL
;
284 blcmp
= list_next(&bip
->bi_links
, blcmp
)) {
286 !(blcmp
->bl_flags
& (BLF_DELETED
|BLF_SDUFAIL
)) &&
287 blcmp
->bl_linkstate
!= LINK_STATE_DOWN
)
291 bmp
->bm_linkstate
= failed
? LINK_STATE_DOWN
:
293 mac_link_redo(bmp
->bm_mh
, bmp
->bm_linkstate
);
298 * If we're becoming failed, then the link's current true state needs
299 * to be reflected upwards to this link's clients. If we're becoming
300 * unfailed, then we get the state of the bridge instead on all
304 if (bmp
->bm_linkstate
!= blp
->bl_linkstate
)
305 mac_link_redo(blp
->bl_mh
, blp
->bl_linkstate
);
307 mac_link_redo(blp
->bl_mh
, bmp
->bm_linkstate
);
310 /* get the current mblk we're going to send up */
311 if ((mp
= blp
->bl_lfailmp
) == NULL
&&
312 (mp
= allocb(sizeof (bridge_ctl_t
), BPRI_MED
)) == NULL
)
315 /* get a new one for next time */
316 blp
->bl_lfailmp
= allocb(sizeof (bridge_ctl_t
), BPRI_MED
);
318 /* if none for next time, then report only failures */
319 if (blp
->bl_lfailmp
== NULL
&& !failed
) {
320 blp
->bl_lfailmp
= mp
;
324 /* LINTED: alignment */
325 bcp
= (bridge_ctl_t
*)mp
->b_rptr
;
326 bcp
->bc_linkid
= blp
->bl_linkid
;
327 bcp
->bc_failed
= failed
;
328 mp
->b_wptr
= (uchar_t
*)(bcp
+ 1);
334 * Send control messages (link SDU changes) using the stream to the
335 * bridge instance daemon.
338 send_up_messages(bridge_inst_t
*bip
, mblk_t
*mp
)
343 rq
= bip
->bi_control
->bs_wq
;
355 bridge_m_getstat(void *arg
, uint_t stat
, uint64_t *val
)
361 bridge_m_start(void *arg
)
363 bridge_mac_t
*bmp
= arg
;
365 bmp
->bm_flags
|= BMF_STARTED
;
370 bridge_m_stop(void *arg
)
372 bridge_mac_t
*bmp
= arg
;
374 bmp
->bm_flags
&= ~BMF_STARTED
;
379 bridge_m_setpromisc(void *arg
, boolean_t on
)
386 bridge_m_multicst(void *arg
, boolean_t add
, const uint8_t *mca
)
393 bridge_m_unicst(void *arg
, const uint8_t *macaddr
)
399 bridge_m_tx(void *arg
, mblk_t
*mp
)
401 _NOTE(ARGUNUSED(arg
));
408 bridge_ioc_listfwd(void *karg
, intptr_t arg
, int mode
, cred_t
*cred
, int *rvalp
)
410 bridge_listfwd_t
*blf
= karg
;
412 bridge_fwd_t
*bfp
, match
;
415 bip
= bridge_find_name(blf
->blf_name
);
419 bcopy(blf
->blf_dest
, match
.bf_dest
, ETHERADDRL
);
420 match
.bf_flags
|= BFF_VLANLOCAL
;
421 rw_enter(&bip
->bi_rwlock
, RW_READER
);
422 if ((bfp
= avl_find(&bip
->bi_fwd
, &match
, &where
)) == NULL
)
423 bfp
= avl_nearest(&bip
->bi_fwd
, where
, AVL_AFTER
);
425 bfp
= AVL_NEXT(&bip
->bi_fwd
, bfp
);
427 bzero(blf
, sizeof (*blf
));
429 bcopy(bfp
->bf_dest
, blf
->blf_dest
, ETHERADDRL
);
430 blf
->blf_trill_nick
= bfp
->bf_trill_nick
;
432 drv_hztousec(ddi_get_lbolt() - bfp
->bf_lastheard
) / 1000;
434 (bfp
->bf_flags
& BFF_LOCALADDR
) != 0;
435 blf
->blf_linkid
= bfp
->bf_links
[0]->bl_linkid
;
437 rw_exit(&bip
->bi_rwlock
);
443 bridge_m_setprop(void *arg
, const char *pr_name
, mac_prop_id_t pr_num
,
444 uint_t pr_valsize
, const void *pr_val
)
446 bridge_mac_t
*bmp
= arg
;
453 _NOTE(ARGUNUSED(pr_name
));
456 if (pr_valsize
< sizeof (bmp
->bm_maxsdu
)) {
460 (void) bcopy(pr_val
, &maxsdu
, sizeof (maxsdu
));
461 if (maxsdu
== bmp
->bm_maxsdu
) {
463 } else if ((bip
= mac_to_inst(bmp
)) == NULL
) {
466 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
468 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
469 blp
= list_next(&bip
->bi_links
, blp
)) {
470 if (blp
->bl_flags
& BLF_DELETED
)
472 if (blp
->bl_maxsdu
== maxsdu
)
473 link_sdu_fail(blp
, B_FALSE
, &mlist
);
474 else if (blp
->bl_maxsdu
== bmp
->bm_maxsdu
)
475 link_sdu_fail(blp
, B_TRUE
, &mlist
);
477 rw_exit(&bip
->bi_rwlock
);
478 bmp
->bm_maxsdu
= maxsdu
;
479 (void) mac_maxsdu_update(bmp
->bm_mh
, maxsdu
);
480 send_up_messages(bip
, mlist
);
494 bridge_m_getprop(void *arg
, const char *pr_name
, mac_prop_id_t pr_num
,
495 uint_t pr_valsize
, void *pr_val
)
497 bridge_mac_t
*bmp
= arg
;
500 _NOTE(ARGUNUSED(pr_name
));
502 case MAC_PROP_STATUS
:
503 ASSERT(pr_valsize
>= sizeof (bmp
->bm_linkstate
));
504 bcopy(&bmp
->bm_linkstate
, pr_val
, sizeof (&bmp
->bm_linkstate
));
515 bridge_m_propinfo(void *arg
, const char *pr_name
, mac_prop_id_t pr_num
,
516 mac_prop_info_handle_t prh
)
518 bridge_mac_t
*bmp
= arg
;
520 _NOTE(ARGUNUSED(pr_name
));
524 mac_prop_info_set_range_uint32(prh
, bmp
->bm_maxsdu
,
527 case MAC_PROP_STATUS
:
528 mac_prop_info_set_perm(prh
, MAC_PROP_PERM_READ
);
533 static mac_callbacks_t bridge_m_callbacks
= {
534 MC_SETPROP
| MC_GETPROP
| MC_PROPINFO
,
553 * Create kstats from a list.
556 kstat_setup(kstat_named_t
*knt
, const char **names
, int nstat
,
557 const char *unitname
)
562 for (i
= 0; i
< nstat
; i
++)
563 kstat_named_init(&knt
[i
], names
[i
], KSTAT_DATA_UINT64
);
565 ksp
= kstat_create_zone(BRIDGE_DEV_NAME
, 0, unitname
, "net",
566 KSTAT_TYPE_NAMED
, nstat
, KSTAT_FLAG_VIRTUAL
, GLOBAL_ZONEID
);
575 * Find an existing bridge_mac_t structure or allocate a new one for the given
576 * bridge instance. This creates the mac driver instance that snoop can use.
579 bmac_alloc(bridge_inst_t
*bip
, bridge_mac_t
**bmacp
)
581 bridge_mac_t
*bmp
, *bnew
;
586 if ((mac
= mac_alloc(MAC_VERSION
)) == NULL
)
589 bnew
= kmem_zalloc(sizeof (*bnew
), KM_SLEEP
);
591 rw_enter(&bmac_rwlock
, RW_WRITER
);
592 for (bmp
= list_head(&bmac_list
); bmp
!= NULL
;
593 bmp
= list_next(&bmac_list
, bmp
)) {
594 if (strcmp(bip
->bi_name
, bmp
->bm_name
) == 0) {
595 ASSERT(bmp
->bm_inst
== NULL
);
597 rw_exit(&bmac_rwlock
);
598 kmem_free(bnew
, sizeof (*bnew
));
605 mac
->m_type_ident
= MAC_PLUGIN_IDENT_ETHER
;
606 mac
->m_driver
= bnew
;
607 mac
->m_dip
= bridge_dev_info
;
608 mac
->m_instance
= (uint_t
)-1;
609 mac
->m_src_addr
= (uint8_t *)zero_addr
;
610 mac
->m_callbacks
= &bridge_m_callbacks
;
613 * Note that the SDU limits are irrelevant, as nobody transmits on the
614 * bridge node itself. It's mainly for monitoring but we allow
615 * setting the bridge MTU for quick transition of all links part of the
616 * bridge to a new MTU.
619 mac
->m_max_sdu
= 1500;
620 err
= mac_register(mac
, &bnew
->bm_mh
);
623 rw_exit(&bmac_rwlock
);
624 kmem_free(bnew
, sizeof (*bnew
));
629 (void) strcpy(bnew
->bm_name
, bip
->bi_name
);
630 if (list_is_empty(&bmac_list
)) {
631 bridge_timerid
= timeout(bridge_timer
, NULL
,
632 bridge_scan_interval
);
634 list_insert_tail(&bmac_list
, bnew
);
635 rw_exit(&bmac_rwlock
);
638 * Mark the MAC as unable to go "active" so that only passive clients
639 * (such as snoop) can bind to it.
641 mac_no_active(bnew
->bm_mh
);
647 * Disconnect the given bridge_mac_t from its bridge instance. The bridge
648 * instance is going away. The mac instance can't go away until the clients
649 * are gone (see bridge_timer).
652 bmac_disconnect(bridge_mac_t
*bmp
)
656 bmp
->bm_linkstate
= LINK_STATE_DOWN
;
657 mac_link_redo(bmp
->bm_mh
, LINK_STATE_DOWN
);
659 rw_enter(&bmac_rwlock
, RW_READER
);
663 rw_exit(&bmac_rwlock
);
666 /* This is used by the avl trees to sort forwarding table entries */
668 fwd_compare(const void *addr1
, const void *addr2
)
670 const bridge_fwd_t
*fwd1
= addr1
;
671 const bridge_fwd_t
*fwd2
= addr2
;
672 int diff
= memcmp(fwd1
->bf_dest
, fwd2
->bf_dest
, ETHERADDRL
);
675 return (diff
> 0 ? 1 : -1);
677 if ((fwd1
->bf_flags
^ fwd2
->bf_flags
) & BFF_VLANLOCAL
) {
678 if (fwd1
->bf_vlanid
> fwd2
->bf_vlanid
)
680 else if (fwd1
->bf_vlanid
< fwd2
->bf_vlanid
)
687 inst_free(bridge_inst_t
*bip
)
689 ASSERT(bip
->bi_mac
== NULL
);
690 rw_destroy(&bip
->bi_rwlock
);
691 list_destroy(&bip
->bi_links
);
692 cv_destroy(&bip
->bi_linkwait
);
693 avl_destroy(&bip
->bi_fwd
);
694 if (bip
->bi_ksp
!= NULL
)
695 kstat_delete(bip
->bi_ksp
);
696 kmem_free(bip
, sizeof (*bip
));
699 static bridge_inst_t
*
700 inst_alloc(const char *bridge
)
704 bip
= kmem_zalloc(sizeof (*bip
), KM_SLEEP
);
706 (void) strcpy(bip
->bi_name
, bridge
);
707 rw_init(&bip
->bi_rwlock
, NULL
, RW_DRIVER
, NULL
);
708 list_create(&bip
->bi_links
, sizeof (bridge_link_t
),
709 offsetof(bridge_link_t
, bl_node
));
710 cv_init(&bip
->bi_linkwait
, NULL
, CV_DRIVER
, NULL
);
711 avl_create(&bip
->bi_fwd
, fwd_compare
, sizeof (bridge_fwd_t
),
712 offsetof(bridge_fwd_t
, bf_node
));
716 static bridge_inst_t
*
717 bridge_find_name(const char *bridge
)
721 mutex_enter(&inst_lock
);
722 for (bip
= list_head(&inst_list
); bip
!= NULL
;
723 bip
= list_next(&inst_list
, bip
)) {
724 if (!(bip
->bi_flags
& BIF_SHUTDOWN
) &&
725 strcmp(bridge
, bip
->bi_name
) == 0) {
726 atomic_inc_uint(&bip
->bi_refs
);
730 mutex_exit(&inst_lock
);
736 bridge_create(datalink_id_t linkid
, const char *bridge
, bridge_inst_t
**bipc
,
739 bridge_inst_t
*bip
, *bipnew
;
740 bridge_mac_t
*bmp
= NULL
;
744 bipnew
= inst_alloc(bridge
);
746 mutex_enter(&inst_lock
);
748 for (bip
= list_head(&inst_list
); bip
!= NULL
;
749 bip
= list_next(&inst_list
, bip
)) {
750 if (strcmp(bridge
, bip
->bi_name
) == 0)
754 /* This should not take long; if it does, we've got a design problem */
755 if (bip
!= NULL
&& (bip
->bi_flags
& BIF_SHUTDOWN
)) {
756 cv_wait(&inst_cv
, &inst_lock
);
763 list_insert_tail(&inst_list
, bip
);
766 mutex_exit(&inst_lock
);
767 if (bipnew
!= NULL
) {
772 bip
->bi_ksp
= kstat_setup((kstat_named_t
*)&bip
->bi_kstats
,
773 inst_kstats_list
, Dim(inst_kstats_list
), bip
->bi_name
);
775 err
= bmac_alloc(bip
, &bmp
);
776 if ((bip
->bi_mac
= bmp
) == NULL
)
780 * bm_inst is set, so the timer cannot yank the DLS rug from under us.
781 * No extra locking is needed here.
783 if (!(bmp
->bm_flags
& BMF_DLS
)) {
784 err
= dls_devnet_create(bmp
->bm_mh
, linkid
, crgetzoneid(cred
));
787 bmp
->bm_flags
|= BMF_DLS
;
790 bip
->bi_dev
= makedevice(bridge_major
, mac_minor(bmp
->bm_mh
));
795 ASSERT(bip
->bi_trilldata
== NULL
);
796 bip
->bi_flags
|= BIF_SHUTDOWN
;
802 bridge_unref(bridge_inst_t
*bip
)
804 if (atomic_dec_uint_nv(&bip
->bi_refs
) == 0) {
805 ASSERT(bip
->bi_flags
& BIF_SHUTDOWN
);
806 /* free up mac for reuse before leaving global list */
807 if (bip
->bi_mac
!= NULL
)
808 bmac_disconnect(bip
->bi_mac
);
809 mutex_enter(&inst_lock
);
810 list_remove(&inst_list
, bip
);
811 cv_broadcast(&inst_cv
);
812 mutex_exit(&inst_lock
);
818 * Stream instances are used only for allocating bridges and serving as a
819 * control node. They serve no data-handling function.
821 static bridge_stream_t
*
824 bridge_stream_t
*bsp
;
827 if ((mn
= mac_minor_hold(B_FALSE
)) == 0)
829 bsp
= kmem_zalloc(sizeof (*bsp
), KM_SLEEP
);
835 stream_free(bridge_stream_t
*bsp
)
837 mac_minor_rele(bsp
->bs_minor
);
838 kmem_free(bsp
, sizeof (*bsp
));
841 /* Reference hold/release functions for STREAMS-related taskq */
843 stream_ref(bridge_stream_t
*bsp
)
845 mutex_enter(&stream_ref_lock
);
847 mutex_exit(&stream_ref_lock
);
851 stream_unref(bridge_stream_t
*bsp
)
853 mutex_enter(&stream_ref_lock
);
854 if (--bsp
->bs_taskq_cnt
== 0)
855 cv_broadcast(&stream_ref_cv
);
856 mutex_exit(&stream_ref_lock
);
860 link_free(bridge_link_t
*blp
)
862 bridge_inst_t
*bip
= blp
->bl_inst
;
864 ASSERT(!(blp
->bl_flags
& BLF_FREED
));
865 blp
->bl_flags
|= BLF_FREED
;
866 if (blp
->bl_ksp
!= NULL
)
867 kstat_delete(blp
->bl_ksp
);
868 if (blp
->bl_lfailmp
!= NULL
)
869 freeb(blp
->bl_lfailmp
);
870 cv_destroy(&blp
->bl_trillwait
);
871 mutex_destroy(&blp
->bl_trilllock
);
872 kmem_free(blp
, sizeof (*blp
));
873 /* Don't unreference the bridge until the MAC is closed */
878 link_unref(bridge_link_t
*blp
)
880 if (atomic_dec_uint_nv(&blp
->bl_refs
) == 0) {
881 bridge_inst_t
*bip
= blp
->bl_inst
;
883 ASSERT(blp
->bl_flags
& BLF_DELETED
);
884 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
885 if (blp
->bl_flags
& BLF_LINK_ADDED
)
886 list_remove(&bip
->bi_links
, blp
);
887 rw_exit(&bip
->bi_rwlock
);
888 if (bip
->bi_trilldata
!= NULL
&& list_is_empty(&bip
->bi_links
))
889 cv_broadcast(&bip
->bi_linkwait
);
894 static bridge_fwd_t
*
895 fwd_alloc(const uint8_t *addr
, uint_t nlinks
, uint16_t nick
)
899 bfp
= kmem_zalloc(sizeof (*bfp
) + (nlinks
* sizeof (bridge_link_t
*)),
902 bcopy(addr
, bfp
->bf_dest
, ETHERADDRL
);
903 bfp
->bf_lastheard
= ddi_get_lbolt();
904 bfp
->bf_maxlinks
= nlinks
;
905 bfp
->bf_links
= (bridge_link_t
**)(bfp
+ 1);
906 bfp
->bf_trill_nick
= nick
;
911 static bridge_fwd_t
*
912 fwd_find(bridge_inst_t
*bip
, const uint8_t *addr
, uint16_t vlanid
)
914 bridge_fwd_t
*bfp
, *vbfp
;
917 bcopy(addr
, match
.bf_dest
, ETHERADDRL
);
919 rw_enter(&bip
->bi_rwlock
, RW_READER
);
920 if ((bfp
= avl_find(&bip
->bi_fwd
, &match
, NULL
)) != NULL
) {
921 if (bfp
->bf_vlanid
!= vlanid
&& bfp
->bf_vcnt
> 0) {
922 match
.bf_vlanid
= vlanid
;
923 match
.bf_flags
= BFF_VLANLOCAL
;
924 vbfp
= avl_find(&bip
->bi_fwd
, &match
, NULL
);
928 atomic_inc_uint(&bfp
->bf_refs
);
930 rw_exit(&bip
->bi_rwlock
);
935 fwd_free(bridge_fwd_t
*bfp
)
938 bridge_inst_t
*bip
= bfp
->bf_links
[0]->bl_inst
;
941 for (i
= 0; i
< bfp
->bf_nlinks
; i
++)
942 link_unref(bfp
->bf_links
[i
]);
944 sizeof (*bfp
) + bfp
->bf_maxlinks
* sizeof (bridge_link_t
*));
948 fwd_unref(bridge_fwd_t
*bfp
)
950 if (atomic_dec_uint_nv(&bfp
->bf_refs
) == 0) {
951 ASSERT(!(bfp
->bf_flags
& BFF_INTREE
));
957 fwd_delete(bridge_fwd_t
*bfp
)
960 bridge_fwd_t
*bfpzero
;
962 if (bfp
->bf_flags
& BFF_INTREE
) {
963 ASSERT(bfp
->bf_nlinks
> 0);
964 bip
= bfp
->bf_links
[0]->bl_inst
;
965 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
966 /* Another thread could beat us to this */
967 if (bfp
->bf_flags
& BFF_INTREE
) {
968 avl_remove(&bip
->bi_fwd
, bfp
);
969 bfp
->bf_flags
&= ~BFF_INTREE
;
970 if (bfp
->bf_flags
& BFF_VLANLOCAL
) {
971 bfp
->bf_flags
&= ~BFF_VLANLOCAL
;
972 bfpzero
= avl_find(&bip
->bi_fwd
, bfp
, NULL
);
973 if (bfpzero
!= NULL
&& bfpzero
->bf_vcnt
> 0)
976 rw_exit(&bip
->bi_rwlock
);
977 fwd_unref(bfp
); /* no longer in avl tree */
979 rw_exit(&bip
->bi_rwlock
);
985 fwd_insert(bridge_inst_t
*bip
, bridge_fwd_t
*bfp
)
990 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
991 if (!(bip
->bi_flags
& BIF_SHUTDOWN
) &&
992 avl_numnodes(&bip
->bi_fwd
) < bip
->bi_tablemax
&&
993 avl_find(&bip
->bi_fwd
, bfp
, &idx
) == NULL
) {
994 avl_insert(&bip
->bi_fwd
, bfp
, idx
);
995 bfp
->bf_flags
|= BFF_INTREE
;
996 atomic_inc_uint(&bfp
->bf_refs
); /* avl entry */
1001 rw_exit(&bip
->bi_rwlock
);
1006 fwd_update_local(bridge_link_t
*blp
, const uint8_t *oldaddr
,
1007 const uint8_t *newaddr
)
1009 bridge_inst_t
*bip
= blp
->bl_inst
;
1010 bridge_fwd_t
*bfp
, *bfnew
;
1013 boolean_t drop_ref
= B_FALSE
;
1015 if (bcmp(oldaddr
, newaddr
, ETHERADDRL
) == 0)
1018 if (bcmp(oldaddr
, zero_addr
, ETHERADDRL
) == 0)
1022 * Find the previous entry, and remove our link from it.
1024 bcopy(oldaddr
, match
.bf_dest
, ETHERADDRL
);
1025 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
1026 if ((bfp
= avl_find(&bip
->bi_fwd
, &match
, NULL
)) != NULL
) {
1030 * See if we're in the list, and remove if so.
1032 for (i
= 0; i
< bfp
->bf_nlinks
; i
++) {
1033 if (bfp
->bf_links
[i
] == blp
) {
1035 * We assume writes are atomic, so no special
1036 * MT handling is needed. The list length is
1037 * decremented first, and then we remove
1041 for (; i
< bfp
->bf_nlinks
; i
++)
1042 bfp
->bf_links
[i
] = bfp
->bf_links
[i
+ 1];
1047 /* If no more links, then remove and free up */
1048 if (bfp
->bf_nlinks
== 0) {
1049 avl_remove(&bip
->bi_fwd
, bfp
);
1050 bfp
->bf_flags
&= ~BFF_INTREE
;
1055 rw_exit(&bip
->bi_rwlock
);
1057 fwd_unref(bfp
); /* no longer in avl tree */
1060 * Now get the new link address and add this link to the list. The
1061 * list should be of length 1 unless the user has configured multiple
1062 * NICs with the same address. (That's an incorrect configuration, but
1063 * we support it anyway.)
1067 if ((bip
->bi_flags
& BIF_SHUTDOWN
) ||
1068 bcmp(newaddr
, zero_addr
, ETHERADDRL
) == 0)
1071 bcopy(newaddr
, match
.bf_dest
, ETHERADDRL
);
1072 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
1073 if ((bfp
= avl_find(&bip
->bi_fwd
, &match
, &idx
)) == NULL
) {
1074 bfnew
= fwd_alloc(newaddr
, 1, RBRIDGE_NICKNAME_NONE
);
1077 } else if (bfp
->bf_nlinks
< bfp
->bf_maxlinks
) {
1078 /* special case: link fits in existing entry */
1081 bfnew
= fwd_alloc(newaddr
, bfp
->bf_nlinks
+ 1,
1082 RBRIDGE_NICKNAME_NONE
);
1083 if (bfnew
!= NULL
) {
1085 avl_remove(&bip
->bi_fwd
, bfp
);
1086 bfp
->bf_flags
&= ~BFF_INTREE
;
1087 bfnew
->bf_nlinks
= bfp
->bf_nlinks
;
1088 bcopy(bfp
->bf_links
, bfnew
->bf_links
,
1089 bfp
->bf_nlinks
* sizeof (bfp
));
1090 /* reset the idx value due to removal above */
1091 (void) avl_find(&bip
->bi_fwd
, &match
, &idx
);
1095 if (bfnew
!= NULL
) {
1096 bfnew
->bf_links
[bfnew
->bf_nlinks
++] = blp
;
1100 atomic_inc_uint(&blp
->bl_refs
); /* bf_links entry */
1103 /* local addresses are not subject to table limits */
1104 avl_insert(&bip
->bi_fwd
, bfnew
, idx
);
1105 bfnew
->bf_flags
|= (BFF_INTREE
| BFF_LOCALADDR
);
1106 atomic_inc_uint(&bfnew
->bf_refs
); /* avl entry */
1109 rw_exit(&bip
->bi_rwlock
);
1113 * If we found an existing entry and we replaced it with a new one,
1114 * then drop the table reference from the old one. We removed it from
1115 * the AVL tree above.
1117 if (bfnew
!= NULL
&& bfp
!= NULL
&& bfnew
!= bfp
)
1120 /* Account for removed entry. */
1126 bridge_new_unicst(bridge_link_t
*blp
)
1128 uint8_t new_mac
[ETHERADDRL
];
1130 mac_unicast_primary_get(blp
->bl_mh
, new_mac
);
1131 fwd_update_local(blp
, blp
->bl_local_mac
, new_mac
);
1132 bcopy(new_mac
, blp
->bl_local_mac
, ETHERADDRL
);
1136 * We must shut down a link prior to freeing it, and doing that requires
1137 * blocking to wait for running MAC threads while holding a reference. This is
1138 * run from a taskq to accomplish proper link shutdown followed by reference
1142 link_shutdown(void *arg
)
1144 bridge_link_t
*blp
= arg
;
1145 mac_handle_t mh
= blp
->bl_mh
;
1147 bridge_fwd_t
*bfp
, *bfnext
;
1148 avl_tree_t fwd_scavenge
;
1152 * This link is being destroyed. Notify TRILL now that it's no longer
1153 * possible to send packets. Data packets may still arrive until TRILL
1154 * calls bridge_trill_lnunref.
1156 if (blp
->bl_trilldata
!= NULL
)
1157 trill_lndstr_fn(blp
->bl_trilldata
, blp
);
1159 if (blp
->bl_flags
& BLF_PROM_ADDED
)
1160 (void) mac_promisc_remove(blp
->bl_mphp
);
1162 if (blp
->bl_flags
& BLF_SET_BRIDGE
)
1163 mac_bridge_clear(mh
, (mac_handle_t
)blp
);
1165 if (blp
->bl_flags
& BLF_MARGIN_ADDED
) {
1166 (void) mac_notify_remove(blp
->bl_mnh
, B_TRUE
);
1167 (void) mac_margin_remove(mh
, blp
->bl_margin
);
1170 /* Tell the clients the real link state when we leave */
1171 mac_link_redo(blp
->bl_mh
,
1172 mac_stat_get(blp
->bl_mh
, MAC_STAT_LOWLINK_STATE
));
1174 /* Destroy all of the forwarding entries related to this link */
1175 avl_create(&fwd_scavenge
, fwd_compare
, sizeof (bridge_fwd_t
),
1176 offsetof(bridge_fwd_t
, bf_node
));
1178 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
1179 bfnext
= avl_first(&bip
->bi_fwd
);
1180 while ((bfp
= bfnext
) != NULL
) {
1181 bfnext
= AVL_NEXT(&bip
->bi_fwd
, bfp
);
1182 for (i
= 0; i
< bfp
->bf_nlinks
; i
++) {
1183 if (bfp
->bf_links
[i
] == blp
)
1186 if (i
>= bfp
->bf_nlinks
)
1188 if (bfp
->bf_nlinks
> 1) {
1189 /* note that this can't be the last reference */
1192 for (; i
< bfp
->bf_nlinks
; i
++)
1193 bfp
->bf_links
[i
] = bfp
->bf_links
[i
+ 1];
1195 ASSERT(bfp
->bf_flags
& BFF_INTREE
);
1196 avl_remove(&bip
->bi_fwd
, bfp
);
1197 bfp
->bf_flags
&= ~BFF_INTREE
;
1198 avl_add(&fwd_scavenge
, bfp
);
1201 rw_exit(&bip
->bi_rwlock
);
1202 bfnext
= avl_first(&fwd_scavenge
);
1203 while ((bfp
= bfnext
) != NULL
) {
1204 bfnext
= AVL_NEXT(&fwd_scavenge
, bfp
);
1205 avl_remove(&fwd_scavenge
, bfp
);
1208 avl_destroy(&fwd_scavenge
);
1210 if (blp
->bl_flags
& BLF_CLIENT_OPEN
)
1211 mac_client_close(blp
->bl_mch
, 0);
1216 * We are now completely removed from the active list, so drop the
1217 * reference (see bridge_add_link).
1223 shutdown_inst(bridge_inst_t
*bip
)
1225 bridge_link_t
*blp
, *blnext
;
1228 mutex_enter(&inst_lock
);
1229 if (bip
->bi_flags
& BIF_SHUTDOWN
) {
1230 mutex_exit(&inst_lock
);
1235 * Once on the inst_list, the bridge instance must not leave that list
1236 * without having the shutdown flag set first. When the shutdown flag
1237 * is set, we own the list reference, so we must drop it before
1240 bip
->bi_flags
|= BIF_SHUTDOWN
;
1241 mutex_exit(&inst_lock
);
1243 bip
->bi_control
= NULL
;
1245 rw_enter(&bip
->bi_rwlock
, RW_READER
);
1246 blnext
= list_head(&bip
->bi_links
);
1247 while ((blp
= blnext
) != NULL
) {
1248 blnext
= list_next(&bip
->bi_links
, blp
);
1249 if (!(blp
->bl_flags
& BLF_DELETED
)) {
1250 blp
->bl_flags
|= BLF_DELETED
;
1251 (void) ddi_taskq_dispatch(bridge_taskq
, link_shutdown
,
1255 while ((bfp
= avl_first(&bip
->bi_fwd
)) != NULL
) {
1256 atomic_inc_uint(&bfp
->bf_refs
);
1257 rw_exit(&bip
->bi_rwlock
);
1260 rw_enter(&bip
->bi_rwlock
, RW_READER
);
1262 rw_exit(&bip
->bi_rwlock
);
1265 * This bridge is being destroyed. Notify TRILL once all of the
1266 * links are all gone.
1268 mutex_enter(&inst_lock
);
1269 while (bip
->bi_trilldata
!= NULL
&& !list_is_empty(&bip
->bi_links
))
1270 cv_wait(&bip
->bi_linkwait
, &inst_lock
);
1271 mutex_exit(&inst_lock
);
1272 if (bip
->bi_trilldata
!= NULL
)
1273 trill_brdstr_fn(bip
->bi_trilldata
, bip
);
1279 * This is called once by the TRILL module when it starts up. It just sets the
1280 * global TRILL callback function pointers -- data transmit/receive and bridge
1281 * and link destroy notification. There's only one TRILL module, so only one
1282 * registration is needed.
1284 * TRILL should call this function with NULL pointers before unloading. It
1285 * must not do so before dropping all references to bridges and links. We
1286 * assert that this is true on debug builds.
1289 bridge_trill_register_cb(trill_recv_pkt_t recv_fn
, trill_encap_pkt_t encap_fn
,
1290 trill_br_dstr_t brdstr_fn
, trill_ln_dstr_t lndstr_fn
)
1293 if (recv_fn
== NULL
&& trill_recv_fn
!= NULL
) {
1297 mutex_enter(&inst_lock
);
1298 for (bip
= list_head(&inst_list
); bip
!= NULL
;
1299 bip
= list_next(&inst_list
, bip
)) {
1300 ASSERT(bip
->bi_trilldata
== NULL
);
1301 rw_enter(&bip
->bi_rwlock
, RW_READER
);
1302 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
1303 blp
= list_next(&bip
->bi_links
, blp
)) {
1304 ASSERT(blp
->bl_trilldata
== NULL
);
1306 rw_exit(&bip
->bi_rwlock
);
1308 mutex_exit(&inst_lock
);
1311 trill_recv_fn
= recv_fn
;
1312 trill_encap_fn
= encap_fn
;
1313 trill_brdstr_fn
= brdstr_fn
;
1314 trill_lndstr_fn
= lndstr_fn
;
1318 * This registers the TRILL instance pointer with a bridge. Before this
1319 * pointer is set, the forwarding, TRILL receive, and bridge destructor
1320 * functions won't be called.
1322 * TRILL holds a reference on a bridge with this call. It must free the
1323 * reference by calling the unregister function below.
1326 bridge_trill_brref(const char *bname
, void *ptr
)
1328 char bridge
[MAXLINKNAMELEN
];
1331 (void) snprintf(bridge
, MAXLINKNAMELEN
, "%s0", bname
);
1332 bip
= bridge_find_name(bridge
);
1334 ASSERT(bip
->bi_trilldata
== NULL
&& ptr
!= NULL
);
1335 bip
->bi_trilldata
= ptr
;
1341 bridge_trill_brunref(bridge_inst_t
*bip
)
1343 ASSERT(bip
->bi_trilldata
!= NULL
);
1344 bip
->bi_trilldata
= NULL
;
1349 * TRILL calls this function when referencing a particular link on a bridge.
1351 * It holds a reference on the link, so TRILL must clear out the reference when
1352 * it's done with the link (on unbinding).
1355 bridge_trill_lnref(bridge_inst_t
*bip
, datalink_id_t linkid
, void *ptr
)
1359 ASSERT(ptr
!= NULL
);
1360 rw_enter(&bip
->bi_rwlock
, RW_READER
);
1361 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
1362 blp
= list_next(&bip
->bi_links
, blp
)) {
1363 if (!(blp
->bl_flags
& BLF_DELETED
) &&
1364 blp
->bl_linkid
== linkid
&& blp
->bl_trilldata
== NULL
) {
1365 blp
->bl_trilldata
= ptr
;
1366 blp
->bl_flags
&= ~BLF_TRILLACTIVE
;
1367 (void) memset(blp
->bl_afs
, 0, sizeof (blp
->bl_afs
));
1368 atomic_inc_uint(&blp
->bl_refs
);
1372 rw_exit(&bip
->bi_rwlock
);
1377 bridge_trill_lnunref(bridge_link_t
*blp
)
1379 mutex_enter(&blp
->bl_trilllock
);
1380 ASSERT(blp
->bl_trilldata
!= NULL
);
1381 blp
->bl_trilldata
= NULL
;
1382 blp
->bl_flags
&= ~BLF_TRILLACTIVE
;
1383 while (blp
->bl_trillthreads
> 0)
1384 cv_wait(&blp
->bl_trillwait
, &blp
->bl_trilllock
);
1385 mutex_exit(&blp
->bl_trilllock
);
1386 (void) memset(blp
->bl_afs
, 0xff, sizeof (blp
->bl_afs
));
1391 * This periodic timer performs three functions:
1392 * 1. It scans the list of learned forwarding entries, and removes ones that
1393 * haven't been heard from in a while. The time limit is backed down if
1394 * we're above the configured table limit.
1395 * 2. It walks the links and decays away the bl_learns counter.
1396 * 3. It scans the observability node entries looking for ones that can be
1401 bridge_timer(void *arg
)
1404 bridge_fwd_t
*bfp
, *bfnext
;
1405 bridge_mac_t
*bmp
, *bmnext
;
1408 datalink_id_t tmpid
;
1409 avl_tree_t fwd_scavenge
;
1413 avl_create(&fwd_scavenge
, fwd_compare
, sizeof (bridge_fwd_t
),
1414 offsetof(bridge_fwd_t
, bf_node
));
1415 mutex_enter(&inst_lock
);
1416 for (bip
= list_head(&inst_list
); bip
!= NULL
;
1417 bip
= list_next(&inst_list
, bip
)) {
1418 if (bip
->bi_flags
& BIF_SHUTDOWN
)
1420 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
1421 /* compute scaled maximum age based on table limit */
1422 if (avl_numnodes(&bip
->bi_fwd
) > bip
->bi_tablemax
)
1426 if ((age_limit
= bridge_fwd_age
>> bip
->bi_tshift
) == 0) {
1427 if (bip
->bi_tshift
!= 0)
1431 bfnext
= avl_first(&bip
->bi_fwd
);
1432 while ((bfp
= bfnext
) != NULL
) {
1433 bfnext
= AVL_NEXT(&bip
->bi_fwd
, bfp
);
1434 if (!(bfp
->bf_flags
& BFF_LOCALADDR
) &&
1435 (ddi_get_lbolt() - bfp
->bf_lastheard
) > age_limit
) {
1436 ASSERT(bfp
->bf_flags
& BFF_INTREE
);
1437 avl_remove(&bip
->bi_fwd
, bfp
);
1438 bfp
->bf_flags
&= ~BFF_INTREE
;
1439 avl_add(&fwd_scavenge
, bfp
);
1442 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
1443 blp
= list_next(&bip
->bi_links
, blp
)) {
1444 ldecay
= mac_get_ldecay(blp
->bl_mh
);
1445 if (ldecay
>= blp
->bl_learns
)
1448 atomic_add_int(&blp
->bl_learns
, -(int)ldecay
);
1450 rw_exit(&bip
->bi_rwlock
);
1451 bfnext
= avl_first(&fwd_scavenge
);
1452 while ((bfp
= bfnext
) != NULL
) {
1453 bfnext
= AVL_NEXT(&fwd_scavenge
, bfp
);
1454 avl_remove(&fwd_scavenge
, bfp
);
1456 fwd_unref(bfp
); /* drop tree reference */
1459 mutex_exit(&inst_lock
);
1460 avl_destroy(&fwd_scavenge
);
1463 * Scan the bridge_mac_t entries and try to free up the ones that are
1464 * no longer active. This must be done by polling, as neither DLS nor
1465 * MAC provides a driver any sort of positive control over clients.
1467 rw_enter(&bmac_rwlock
, RW_WRITER
);
1468 bmnext
= list_head(&bmac_list
);
1469 while ((bmp
= bmnext
) != NULL
) {
1470 bmnext
= list_next(&bmac_list
, bmp
);
1472 /* ignore active bridges */
1473 if (bmp
->bm_inst
!= NULL
)
1476 if (bmp
->bm_flags
& BMF_DLS
) {
1477 err
= dls_devnet_destroy(bmp
->bm_mh
, &tmpid
, B_FALSE
);
1478 ASSERT(err
== 0 || err
== EBUSY
);
1480 bmp
->bm_flags
&= ~BMF_DLS
;
1483 if (!(bmp
->bm_flags
& BMF_DLS
)) {
1484 err
= mac_unregister(bmp
->bm_mh
);
1485 ASSERT(err
== 0 || err
== EBUSY
);
1487 list_remove(&bmac_list
, bmp
);
1488 kmem_free(bmp
, sizeof (*bmp
));
1492 if (list_is_empty(&bmac_list
)) {
1495 bridge_timerid
= timeout(bridge_timer
, NULL
,
1496 bridge_scan_interval
);
1498 rw_exit(&bmac_rwlock
);
1502 bridge_open(queue_t
*rq
, dev_t
*devp
, int oflag
, int sflag
, cred_t
*credp
)
1504 bridge_stream_t
*bsp
;
1506 if (rq
->q_ptr
!= NULL
)
1509 if (sflag
& MODOPEN
)
1513 * Check the minor node number being opened. This tells us which
1514 * bridge instance the user wants.
1516 if (getminor(*devp
) != 0) {
1518 * This is a regular DLPI stream for snoop or the like.
1519 * Redirect it through DLD.
1521 rq
->q_qinfo
= &bridge_dld_rinit
;
1522 OTHERQ(rq
)->q_qinfo
= &bridge_dld_winit
;
1523 return (dld_open(rq
, devp
, oflag
, sflag
, credp
));
1526 * Allocate the bridge control stream structure.
1528 if ((bsp
= stream_alloc()) == NULL
)
1530 rq
->q_ptr
= WR(rq
)->q_ptr
= (caddr_t
)bsp
;
1531 bsp
->bs_wq
= WR(rq
);
1532 *devp
= makedevice(getmajor(*devp
), bsp
->bs_minor
);
1539 * This is used only for bridge control streams. DLPI goes through dld
1543 bridge_close(queue_t
*rq
)
1545 bridge_stream_t
*bsp
= rq
->q_ptr
;
1549 * Wait for any stray taskq (add/delete link) entries related to this
1550 * stream to leave the system.
1552 mutex_enter(&stream_ref_lock
);
1553 while (bsp
->bs_taskq_cnt
!= 0)
1554 cv_wait(&stream_ref_cv
, &stream_ref_lock
);
1555 mutex_exit(&stream_ref_lock
);
1558 if ((bip
= bsp
->bs_inst
) != NULL
)
1560 rq
->q_ptr
= WR(rq
)->q_ptr
= NULL
;
1569 bridge_learn(bridge_link_t
*blp
, const uint8_t *saddr
, uint16_t ingress_nick
,
1572 bridge_inst_t
*bip
= blp
->bl_inst
;
1573 bridge_fwd_t
*bfp
, *bfpnew
;
1575 boolean_t replaced
= B_FALSE
;
1577 /* Ignore multi-destination address used as source; it's nonsense. */
1582 * If the source is known, then check whether it belongs on this link.
1583 * If not, and this isn't a fixed local address, then we've detected a
1584 * move. If it's not known, learn it.
1586 if ((bfp
= fwd_find(bip
, saddr
, vlanid
)) != NULL
) {
1588 * If the packet has a fixed local source address, then there's
1589 * nothing we can learn. We must quit. If this was a received
1590 * packet, then the sender has stolen our address, but there's
1591 * nothing we can do. If it's a transmitted packet, then
1592 * that's the normal case.
1594 if (bfp
->bf_flags
& BFF_LOCALADDR
) {
1600 * Check if the link (and TRILL sender, if any) being used is
1601 * among the ones registered for this address. If so, then
1602 * this is information that we already know.
1604 if (bfp
->bf_trill_nick
== ingress_nick
) {
1605 for (i
= 0; i
< bfp
->bf_nlinks
; i
++) {
1606 if (bfp
->bf_links
[i
] == blp
) {
1607 bfp
->bf_lastheard
= ddi_get_lbolt();
1616 * Note that we intentionally "unlearn" things that appear to be under
1617 * attack on this link. The forwarding cache is a negative thing for
1618 * security -- it disables reachability as a performance optimization
1619 * -- so leaving out entries optimizes for success and defends against
1620 * the attack. Thus, the bare increment without a check in the delete
1621 * code above is right. (And it's ok if we skid over the limit a
1622 * little, so there's no syncronization needed on the test.)
1624 if (blp
->bl_learns
>= mac_get_llimit(blp
->bl_mh
)) {
1626 if (bfp
->bf_vcnt
== 0)
1633 atomic_inc_uint(&blp
->bl_learns
);
1635 if ((bfpnew
= fwd_alloc(saddr
, 1, ingress_nick
)) == NULL
) {
1644 * If this is a new destination for the same VLAN, then delete
1645 * so that we can update. If it's a different VLAN, then we're
1646 * not going to delete the original. Split off instead into an
1649 if (bfp
->bf_vlanid
== vlanid
) {
1650 /* save the count of IVL duplicates */
1651 bfpnew
->bf_vcnt
= bfp
->bf_vcnt
;
1653 /* entry deletes count as learning events */
1654 atomic_inc_uint(&blp
->bl_learns
);
1656 /* destroy and create anew; node moved */
1662 bfpnew
->bf_flags
|= BFF_VLANLOCAL
;
1666 bfpnew
->bf_links
[0] = blp
;
1667 bfpnew
->bf_nlinks
= 1;
1668 atomic_inc_uint(&blp
->bl_refs
); /* bf_links entry */
1669 if (!fwd_insert(bip
, bfpnew
))
1676 * Process the VLAN headers for output on a given link. There are several
1677 * cases (noting that we don't map VLANs):
1678 * 1. The input packet is good as it is; either
1679 * a. It has no tag, and output has same PVID
1680 * b. It has a non-zero priority-only tag for PVID, and b_band is same
1681 * c. It has a tag with VLAN different from PVID, and b_band is same
1682 * 2. The tag must change: non-zero b_band is different from tag priority
1683 * 3. The packet has a tag and should not (VLAN same as PVID, b_band zero)
1684 * 4. The packet has no tag and needs one:
1685 * a. VLAN ID same as PVID, but b_band is non-zero
1686 * b. VLAN ID different from PVID
1687 * We exclude case 1 first, then modify the packet. Note that output packets
1688 * get a priority set by the mblk, not by the header, because QoS in bridging
1689 * requires priority recalculation at each node.
1691 * The passed-in tci is the "impossible" value 0xFFFF when no tag is present.
1694 reform_vlan_header(mblk_t
*mp
, uint16_t vlanid
, uint16_t tci
, uint16_t pvid
)
1696 boolean_t source_has_tag
= (tci
!= 0xFFFF);
1698 size_t mlen
, minlen
;
1699 struct ether_vlan_header
*evh
;
1702 /* This helps centralize error handling in the caller. */
1706 /* No forwarded packet can have hardware checksum enabled */
1707 DB_CKSUMFLAGS(mp
) = 0;
1709 /* Get the no-modification cases out of the way first */
1710 if (!source_has_tag
&& vlanid
== pvid
) /* 1a */
1713 pri
= VLAN_PRI(tci
);
1714 if (source_has_tag
&& mp
->b_band
== pri
) {
1715 if (vlanid
!= pvid
) /* 1c */
1717 if (pri
!= 0 && VLAN_ID(tci
) == 0) /* 1b */
1722 * We now know that we must modify the packet. Prepare for that. Note
1723 * that if a tag is present, the caller has already done a pullup for
1724 * the VLAN header, so we're good to go.
1726 if (MBLKL(mp
) < sizeof (struct ether_header
)) {
1727 mpcopy
= msgpullup(mp
, sizeof (struct ether_header
));
1728 if (mpcopy
== NULL
) {
1734 if (DB_REF(mp
) > 1 || !IS_P2ALIGNED(mp
->b_rptr
, sizeof (uint16_t)) ||
1735 (!source_has_tag
&& MBLKTAIL(mp
) < VLAN_INCR
)) {
1736 minlen
= mlen
= MBLKL(mp
);
1737 if (!source_has_tag
)
1738 minlen
+= VLAN_INCR
;
1739 ASSERT(minlen
>= sizeof (struct ether_vlan_header
));
1741 * We're willing to copy some data to avoid fragmentation, but
1745 minlen
= sizeof (struct ether_vlan_header
);
1746 mpcopy
= allocb(minlen
, BPRI_MED
);
1747 if (mpcopy
== NULL
) {
1751 if (mlen
<= minlen
) {
1752 /* We toss the first mblk when we can. */
1753 bcopy(mp
->b_rptr
, mpcopy
->b_rptr
, mlen
);
1754 mpcopy
->b_wptr
+= mlen
;
1755 mpcopy
->b_cont
= mp
->b_cont
;
1758 /* If not, then just copy what we need */
1759 if (!source_has_tag
)
1760 minlen
= sizeof (struct ether_header
);
1761 bcopy(mp
->b_rptr
, mpcopy
->b_rptr
, minlen
);
1762 mpcopy
->b_wptr
+= minlen
;
1763 mpcopy
->b_cont
= mp
;
1764 mp
->b_rptr
+= minlen
;
1769 /* LINTED: pointer alignment */
1770 evh
= (struct ether_vlan_header
*)mp
->b_rptr
;
1771 if (source_has_tag
) {
1772 if (mp
->b_band
== 0 && vlanid
== pvid
) { /* 3 */
1773 evh
->ether_tpid
= evh
->ether_type
;
1775 if (mlen
> sizeof (struct ether_vlan_header
))
1776 ovbcopy(mp
->b_rptr
+
1777 sizeof (struct ether_vlan_header
),
1778 mp
->b_rptr
+ sizeof (struct ether_header
),
1779 mlen
- sizeof (struct ether_vlan_header
));
1780 mp
->b_wptr
-= VLAN_INCR
;
1783 vlanid
= VLAN_ID_NONE
;
1784 tci
= VLAN_TCI(mp
->b_band
, ETHER_CFI
, vlanid
);
1785 evh
->ether_tci
= htons(tci
);
1788 /* case 4: no header present, but one is needed */
1790 if (mlen
> sizeof (struct ether_header
))
1791 ovbcopy(mp
->b_rptr
+ sizeof (struct ether_header
),
1792 mp
->b_rptr
+ sizeof (struct ether_vlan_header
),
1793 mlen
- sizeof (struct ether_header
));
1794 mp
->b_wptr
+= VLAN_INCR
;
1795 ASSERT(mp
->b_wptr
<= DB_LIM(mp
));
1797 vlanid
= VLAN_ID_NONE
;
1798 tci
= VLAN_TCI(mp
->b_band
, ETHER_CFI
, vlanid
);
1799 evh
->ether_type
= evh
->ether_tpid
;
1800 evh
->ether_tpid
= htons(ETHERTYPE_VLAN
);
1801 evh
->ether_tci
= htons(tci
);
1806 /* Record VLAN information and strip header if requested . */
1808 update_header(mblk_t
*mp
, mac_header_info_t
*hdr_info
, boolean_t striphdr
)
1810 if (hdr_info
->mhi_bindsap
== ETHERTYPE_VLAN
) {
1811 struct ether_vlan_header
*evhp
;
1812 uint16_t ether_type
;
1814 /* LINTED: alignment */
1815 evhp
= (struct ether_vlan_header
*)mp
->b_rptr
;
1816 hdr_info
->mhi_istagged
= B_TRUE
;
1817 hdr_info
->mhi_tci
= ntohs(evhp
->ether_tci
);
1820 * For VLAN tagged frames update the ether_type
1821 * in hdr_info before stripping the header.
1823 ether_type
= ntohs(evhp
->ether_type
);
1824 hdr_info
->mhi_origsap
= ether_type
;
1825 hdr_info
->mhi_bindsap
= (ether_type
> ETHERMTU
) ?
1826 ether_type
: DLS_SAP_LLC
;
1827 mp
->b_rptr
= (uchar_t
*)(evhp
+ 1);
1830 hdr_info
->mhi_istagged
= B_FALSE
;
1831 hdr_info
->mhi_tci
= VLAN_ID_NONE
;
1833 mp
->b_rptr
+= sizeof (struct ether_header
);
1838 * Return B_TRUE if we're allowed to send on this link with the given VLAN ID.
1841 bridge_can_send(bridge_link_t
*blp
, uint16_t vlanid
)
1843 ASSERT(vlanid
!= VLAN_ID_NONE
);
1844 if (blp
->bl_flags
& BLF_DELETED
)
1846 if (blp
->bl_trilldata
== NULL
&& blp
->bl_state
!= BLS_FORWARDING
)
1848 return (BRIDGE_VLAN_ISSET(blp
, vlanid
) && BRIDGE_AF_ISSET(blp
, vlanid
));
1852 * This function scans the bridge forwarding tables in order to forward a given
1853 * packet. If the packet either doesn't need forwarding (the current link is
1854 * correct) or the current link needs a copy as well, then the packet is
1855 * returned to the caller.
1857 * If a packet has been decapsulated from TRILL, then it must *NOT* reenter a
1858 * TRILL tunnel. If the destination points there, then drop instead.
1861 bridge_forward(bridge_link_t
*blp
, mac_header_info_t
*hdr_info
, mblk_t
*mp
,
1862 uint16_t vlanid
, uint16_t tci
, boolean_t from_trill
, boolean_t is_xmit
)
1864 mblk_t
*mpsend
, *mpcopy
;
1865 bridge_inst_t
*bip
= blp
->bl_inst
;
1866 bridge_link_t
*blpsend
, *blpnext
;
1869 boolean_t selfseen
= B_FALSE
;
1871 const uint8_t *daddr
= hdr_info
->mhi_daddr
;
1874 * Check for the IEEE "reserved" multicast addresses. Messages sent to
1875 * these addresses are used for link-local control (STP and pause), and
1876 * are never forwarded or redirected.
1878 if (daddr
[0] == 1 && daddr
[1] == 0x80 && daddr
[2] == 0xc2 &&
1879 daddr
[3] == 0 && daddr
[4] == 0 && (daddr
[5] & 0xf0) == 0) {
1887 if ((bfp
= fwd_find(bip
, daddr
, vlanid
)) != NULL
) {
1890 * If trill indicates a destination for this node, then it's
1891 * clearly not intended for local delivery. We must tell TRILL
1892 * to encapsulate, as long as we didn't just decapsulate it.
1894 if (bfp
->bf_trill_nick
!= RBRIDGE_NICKNAME_NONE
) {
1896 * Error case: can't reencapsulate if the protocols are
1897 * working correctly.
1903 mutex_enter(&blp
->bl_trilllock
);
1904 if ((tdp
= blp
->bl_trilldata
) != NULL
) {
1905 blp
->bl_trillthreads
++;
1906 mutex_exit(&blp
->bl_trilllock
);
1907 update_header(mp
, hdr_info
, B_FALSE
);
1909 mp
= mac_fix_cksum(mp
);
1910 /* all trill data frames have Inner.VLAN */
1911 mp
= reform_vlan_header(mp
, vlanid
, tci
, 0);
1917 trill_encap_fn(tdp
, blp
, hdr_info
, mp
,
1918 bfp
->bf_trill_nick
);
1919 mutex_enter(&blp
->bl_trilllock
);
1920 if (--blp
->bl_trillthreads
== 0 &&
1921 blp
->bl_trilldata
== NULL
)
1922 cv_broadcast(&blp
->bl_trillwait
);
1924 mutex_exit(&blp
->bl_trilllock
);
1926 /* if TRILL has been disabled, then kill this stray */
1935 /* find first link we can send on */
1936 for (i
= 0; i
< bfp
->bf_nlinks
; i
++) {
1937 blpsend
= bfp
->bf_links
[i
];
1940 else if (bridge_can_send(blpsend
, vlanid
))
1944 while (i
< bfp
->bf_nlinks
) {
1945 blpsend
= bfp
->bf_links
[i
];
1946 for (i
++; i
< bfp
->bf_nlinks
; i
++) {
1947 blpnext
= bfp
->bf_links
[i
];
1950 else if (bridge_can_send(blpnext
, vlanid
))
1953 if (i
== bfp
->bf_nlinks
&& !selfseen
) {
1957 mpsend
= copymsg(mp
);
1960 if (!from_trill
&& is_xmit
)
1961 mpsend
= mac_fix_cksum(mpsend
);
1963 mpsend
= reform_vlan_header(mpsend
, vlanid
, tci
,
1965 if (mpsend
== NULL
) {
1970 KIINCR(bki_forwards
);
1972 * No need to bump up the link reference count, as
1973 * the forwarding entry itself holds a reference to
1976 if (bfp
->bf_flags
& BFF_LOCALADDR
) {
1977 mac_rx_common(blpsend
->bl_mh
, NULL
, mpsend
);
1979 KLPINCR(blpsend
, bkl_xmit
);
1980 MAC_RING_TX(blpsend
->bl_mh
, NULL
, mpsend
,
1986 * Handle a special case: if we're transmitting to the original
1987 * link, then check whether the localaddr flag is set. If it
1988 * is, then receive instead. This doesn't happen with ordinary
1989 * bridging, but does happen often with TRILL decapsulation.
1991 if (mp
!= NULL
&& is_xmit
&& (bfp
->bf_flags
& BFF_LOCALADDR
)) {
1992 mac_rx_common(blp
->bl_mh
, NULL
, mp
);
1998 * TRILL has two cases to handle. If the packet is off the
1999 * wire (not from TRILL), then we need to send up into the
2000 * TRILL module to have the distribution tree computed. If the
2001 * packet is from TRILL (decapsulated), then we're part of the
2002 * distribution tree, and we need to copy the packet on member
2005 * Thus, the from TRILL case is identical to the STP case.
2007 if (!from_trill
&& blp
->bl_trilldata
!= NULL
) {
2008 mutex_enter(&blp
->bl_trilllock
);
2009 if ((tdp
= blp
->bl_trilldata
) != NULL
) {
2010 blp
->bl_trillthreads
++;
2011 mutex_exit(&blp
->bl_trilllock
);
2012 if ((mpsend
= copymsg(mp
)) != NULL
) {
2013 update_header(mpsend
,
2016 * all trill data frames have
2019 mpsend
= reform_vlan_header(mpsend
,
2021 if (mpsend
== NULL
) {
2024 trill_encap_fn(tdp
, blp
,
2026 RBRIDGE_NICKNAME_NONE
);
2029 mutex_enter(&blp
->bl_trilllock
);
2030 if (--blp
->bl_trillthreads
== 0 &&
2031 blp
->bl_trilldata
== NULL
)
2032 cv_broadcast(&blp
->bl_trillwait
);
2034 mutex_exit(&blp
->bl_trilllock
);
2038 * This is an unknown destination, so flood.
2040 rw_enter(&bip
->bi_rwlock
, RW_READER
);
2041 for (blpnext
= list_head(&bip
->bi_links
); blpnext
!= NULL
;
2042 blpnext
= list_next(&bip
->bi_links
, blpnext
)) {
2045 else if (bridge_can_send(blpnext
, vlanid
))
2048 if (blpnext
!= NULL
)
2049 atomic_inc_uint(&blpnext
->bl_refs
);
2050 rw_exit(&bip
->bi_rwlock
);
2051 while ((blpsend
= blpnext
) != NULL
) {
2052 rw_enter(&bip
->bi_rwlock
, RW_READER
);
2053 for (blpnext
= list_next(&bip
->bi_links
, blpsend
);
2055 blpnext
= list_next(&bip
->bi_links
, blpnext
)) {
2058 else if (bridge_can_send(blpnext
, vlanid
))
2061 if (blpnext
!= NULL
)
2062 atomic_inc_uint(&blpnext
->bl_refs
);
2063 rw_exit(&bip
->bi_rwlock
);
2064 if (blpnext
== NULL
&& !selfseen
) {
2068 mpsend
= copymsg(mp
);
2071 if (!from_trill
&& is_xmit
)
2072 mpsend
= mac_fix_cksum(mpsend
);
2074 mpsend
= reform_vlan_header(mpsend
, vlanid
, tci
,
2076 if (mpsend
== NULL
) {
2081 if (hdr_info
->mhi_dsttype
== MAC_ADDRTYPE_UNICAST
)
2082 KIINCR(bki_unknown
);
2085 KLPINCR(blpsend
, bkl_xmit
);
2086 if ((mpcopy
= copymsg(mpsend
)) != NULL
)
2087 mac_rx_common(blpsend
->bl_mh
, NULL
, mpcopy
);
2088 MAC_RING_TX(blpsend
->bl_mh
, NULL
, mpsend
, mpsend
);
2090 link_unref(blpsend
);
2095 * At this point, if np is non-NULL, it means that the caller needs to
2096 * continue on the selected link.
2102 * Extract and validate the VLAN information for a given packet. This checks
2103 * conformance with the rules for use of the PVID on the link, and for the
2104 * allowed (configured) VLAN set.
2106 * Returns B_TRUE if the packet passes, B_FALSE if it fails.
2109 bridge_get_vlan(bridge_link_t
*blp
, mac_header_info_t
*hdr_info
, mblk_t
*mp
,
2110 uint16_t *vlanidp
, uint16_t *tcip
)
2112 uint16_t tci
, vlanid
;
2114 if (hdr_info
->mhi_bindsap
== ETHERTYPE_VLAN
) {
2115 ptrdiff_t tpos
= offsetof(struct ether_vlan_header
, ether_tci
);
2119 * Extract the VLAN ID information, regardless of alignment,
2120 * and without a pullup. This isn't attractive, but we do this
2121 * to avoid having to deal with the pointers stashed in
2122 * hdr_info moving around or having the caller deal with a new
2125 while (mp
!= NULL
) {
2127 if (mlen
> tpos
&& mlen
> 0)
2134 tci
= mp
->b_rptr
[tpos
] << 8;
2135 if (++tpos
>= mlen
) {
2138 } while (mp
!= NULL
&& MBLKL(mp
) == 0);
2143 tci
|= mp
->b_rptr
[tpos
];
2145 vlanid
= VLAN_ID(tci
);
2146 if (VLAN_CFI(tci
) != ETHER_CFI
|| vlanid
> VLAN_ID_MAX
)
2148 if (vlanid
== VLAN_ID_NONE
|| vlanid
== blp
->bl_pvid
)
2150 if (!BRIDGE_VLAN_ISSET(blp
, vlanid
))
2156 * If PVID is set to zero, then untagged traffic is not
2157 * supported here. Do not learn or forward.
2159 if ((vlanid
= blp
->bl_pvid
) == VLAN_ID_NONE
)
2169 * Handle MAC notifications.
2172 bridge_notify_cb(void *arg
, mac_notify_type_t note_type
)
2174 bridge_link_t
*blp
= arg
;
2176 switch (note_type
) {
2177 case MAC_NOTE_UNICST
:
2178 bridge_new_unicst(blp
);
2181 case MAC_NOTE_SDU_SIZE
: {
2183 bridge_inst_t
*bip
= blp
->bl_inst
;
2184 bridge_mac_t
*bmp
= bip
->bi_mac
;
2185 boolean_t notify
= B_FALSE
;
2186 mblk_t
*mlist
= NULL
;
2188 mac_sdu_get(blp
->bl_mh
, NULL
, &maxsdu
);
2189 rw_enter(&bip
->bi_rwlock
, RW_READER
);
2190 if (list_prev(&bip
->bi_links
, blp
) == NULL
&&
2191 list_next(&bip
->bi_links
, blp
) == NULL
) {
2192 notify
= (maxsdu
!= bmp
->bm_maxsdu
);
2193 bmp
->bm_maxsdu
= maxsdu
;
2195 blp
->bl_maxsdu
= maxsdu
;
2196 if (maxsdu
!= bmp
->bm_maxsdu
)
2197 link_sdu_fail(blp
, B_TRUE
, &mlist
);
2199 (void) mac_maxsdu_update(bmp
->bm_mh
, maxsdu
);
2200 rw_exit(&bip
->bi_rwlock
);
2201 send_up_messages(bip
, mlist
);
2208 * This is called by the MAC layer. As with the transmit side, we're right in
2209 * the data path for all I/O on this port, so if we don't need to forward this
2210 * packet anywhere, we have to send it upwards via mac_rx_common.
2213 bridge_recv_cb(mac_handle_t mh
, mac_resource_handle_t rsrc
, mblk_t
*mpnext
)
2215 mblk_t
*mp
, *mpcopy
;
2216 bridge_link_t
*blp
= (bridge_link_t
*)mh
;
2217 bridge_inst_t
*bip
= blp
->bl_inst
;
2218 bridge_mac_t
*bmp
= bip
->bi_mac
;
2219 mac_header_info_t hdr_info
;
2220 uint16_t vlanid
, tci
;
2221 boolean_t trillmode
= B_FALSE
;
2227 * Regardless of state, check for inbound TRILL packets when TRILL is
2228 * active. These are pulled out of band and sent for TRILL handling.
2230 if (blp
->bl_trilldata
!= NULL
) {
2233 mblk_t
*tail
= NULL
;
2235 mutex_enter(&blp
->bl_trilllock
);
2236 if ((tdp
= blp
->bl_trilldata
) != NULL
) {
2237 blp
->bl_trillthreads
++;
2238 mutex_exit(&blp
->bl_trilllock
);
2241 while ((mp
= mpnext
) != NULL
) {
2242 boolean_t raw_isis
, bridge_group
;
2244 mpnext
= mp
->b_next
;
2247 * If the header isn't readable, then leave on
2248 * the list and continue.
2250 if (mac_header_info(blp
->bl_mh
, mp
,
2257 * The TRILL document specifies that, on
2258 * Ethernet alone, IS-IS packets arrive with
2259 * LLC rather than Ethertype, and using a
2260 * specific destination address. We must check
2261 * for that here. Also, we need to give BPDUs
2262 * to TRILL for processing.
2264 raw_isis
= bridge_group
= B_FALSE
;
2265 if (hdr_info
.mhi_dsttype
==
2266 MAC_ADDRTYPE_MULTICAST
) {
2267 if (memcmp(hdr_info
.mhi_daddr
,
2268 all_isis_rbridges
, ETHERADDRL
) == 0)
2270 else if (memcmp(hdr_info
.mhi_daddr
,
2271 bridge_group_address
, ETHERADDRL
) ==
2273 bridge_group
= B_TRUE
;
2275 if (!raw_isis
&& !bridge_group
&&
2276 hdr_info
.mhi_bindsap
!= ETHERTYPE_TRILL
&&
2277 (hdr_info
.mhi_bindsap
!= ETHERTYPE_VLAN
||
2278 /* LINTED: alignment */
2279 ((struct ether_vlan_header
*)mp
->b_rptr
)->
2280 ether_type
!= htons(ETHERTYPE_TRILL
))) {
2286 * We've got TRILL input. Remove from the list
2287 * and send up through the TRILL module. (Send
2288 * a copy through promiscuous receive just to
2289 * support snooping on TRILL. Order isn't
2290 * preserved strictly, but that doesn't matter
2294 tail
->b_next
= mpnext
;
2298 mac_trill_snoop(blp
->bl_mh
, mp
);
2299 update_header(mp
, &hdr_info
, B_TRUE
);
2301 * On raw IS-IS and BPDU frames, we have to
2302 * make sure that the length is trimmed
2303 * properly. We use origsap in order to cope
2304 * with jumbograms for IS-IS. (Regular mac
2307 if (raw_isis
|| bridge_group
) {
2308 size_t msglen
= msgdsize(mp
);
2310 if (msglen
> hdr_info
.mhi_origsap
) {
2312 hdr_info
.mhi_origsap
-
2315 hdr_info
.mhi_origsap
) {
2320 trill_recv_fn(tdp
, blp
, rsrc
, mp
, &hdr_info
);
2323 mutex_enter(&blp
->bl_trilllock
);
2324 if (--blp
->bl_trillthreads
== 0 &&
2325 blp
->bl_trilldata
== NULL
)
2326 cv_broadcast(&blp
->bl_trillwait
);
2328 mutex_exit(&blp
->bl_trilllock
);
2334 * If this is a TRILL RBridge, then just check whether this link is
2335 * used at all for forwarding. If not, then we're done.
2338 if (!(blp
->bl_flags
& BLF_TRILLACTIVE
) ||
2339 (blp
->bl_flags
& BLF_SDUFAIL
)) {
2340 mac_rx_common(blp
->bl_mh
, rsrc
, mpnext
);
2345 * For regular (STP) bridges, if we're in blocking or listening
2346 * state, then do nothing. We don't learn or forward until
2349 if (blp
->bl_state
== BLS_BLOCKLISTEN
) {
2350 mac_rx_common(blp
->bl_mh
, rsrc
, mpnext
);
2356 * Send a copy of the message chain up to the observability node users.
2357 * For TRILL, we must obey the VLAN AF rules, so we go packet-by-
2360 if (!trillmode
&& blp
->bl_state
== BLS_FORWARDING
&&
2361 (bmp
->bm_flags
& BMF_STARTED
) &&
2362 (mp
= copymsgchain(mpnext
)) != NULL
) {
2363 mac_rx(bmp
->bm_mh
, NULL
, mp
);
2367 * We must be in learning or forwarding state, or using TRILL on a link
2368 * with one or more VLANs active. For each packet in the list, process
2369 * the source address, and then attempt to forward.
2371 while ((mp
= mpnext
) != NULL
) {
2372 mpnext
= mp
->b_next
;
2376 * If we can't decode the header or if the header specifies a
2377 * multicast source address (impossible!), then don't bother
2378 * learning or forwarding, but go ahead and forward up the
2379 * stack for subsequent processing.
2381 if (mac_header_info(blp
->bl_mh
, mp
, &hdr_info
) != 0 ||
2382 (hdr_info
.mhi_saddr
[0] & 1) != 0) {
2385 mac_rx_common(blp
->bl_mh
, rsrc
, mp
);
2390 * Extract and validate the VLAN ID for this packet.
2392 if (!bridge_get_vlan(blp
, &hdr_info
, mp
, &vlanid
, &tci
) ||
2393 !BRIDGE_AF_ISSET(blp
, vlanid
)) {
2394 mac_rx_common(blp
->bl_mh
, rsrc
, mp
);
2400 * Special test required by TRILL document: must
2401 * discard frames with outer address set to ESADI.
2403 if (memcmp(hdr_info
.mhi_daddr
, all_esadi_rbridges
,
2405 mac_rx_common(blp
->bl_mh
, rsrc
, mp
);
2410 * If we're in TRILL mode, then the call above to get
2411 * the VLAN ID has also checked that we're the
2412 * appointed forwarder, so report that we're handling
2413 * this packet to any observability node users.
2415 if ((bmp
->bm_flags
& BMF_STARTED
) &&
2416 (mpcopy
= copymsg(mp
)) != NULL
)
2417 mac_rx(bmp
->bm_mh
, NULL
, mpcopy
);
2421 * First process the source address and learn from it. For
2422 * TRILL, we learn only if we're the appointed forwarder.
2424 bridge_learn(blp
, hdr_info
.mhi_saddr
, RBRIDGE_NICKNAME_NONE
,
2428 * Now check whether we're forwarding and look up the
2429 * destination. If we can forward, do so.
2431 if (trillmode
|| blp
->bl_state
== BLS_FORWARDING
) {
2432 mp
= bridge_forward(blp
, &hdr_info
, mp
, vlanid
, tci
,
2436 mac_rx_common(blp
->bl_mh
, rsrc
, mp
);
2443 bridge_xmit_cb(mac_handle_t mh
, mac_ring_handle_t rh
, mblk_t
*mpnext
)
2445 bridge_link_t
*blp
= (bridge_link_t
*)mh
;
2446 bridge_inst_t
*bip
= blp
->bl_inst
;
2447 bridge_mac_t
*bmp
= bip
->bi_mac
;
2448 mac_header_info_t hdr_info
;
2449 uint16_t vlanid
, tci
;
2450 mblk_t
*mp
, *mpcopy
;
2451 boolean_t trillmode
;
2453 trillmode
= blp
->bl_trilldata
!= NULL
;
2456 * If we're using STP and we're in blocking or listening state, or if
2457 * we're using TRILL and no VLANs are active, then behave as though the
2458 * bridge isn't here at all, and send on the local link alone.
2460 if ((!trillmode
&& blp
->bl_state
== BLS_BLOCKLISTEN
) ||
2462 (!(blp
->bl_flags
& BLF_TRILLACTIVE
) ||
2463 (blp
->bl_flags
& BLF_SDUFAIL
)))) {
2466 MAC_RING_TX(blp
->bl_mh
, rh
, mpnext
, mp
);
2471 * Send a copy of the message up to the observability node users.
2472 * TRILL needs to check on a packet-by-packet basis.
2474 if (!trillmode
&& blp
->bl_state
== BLS_FORWARDING
&&
2475 (bmp
->bm_flags
& BMF_STARTED
) &&
2476 (mp
= copymsgchain(mpnext
)) != NULL
) {
2477 mac_rx(bmp
->bm_mh
, NULL
, mp
);
2480 while ((mp
= mpnext
) != NULL
) {
2481 mpnext
= mp
->b_next
;
2484 if (mac_header_info(blp
->bl_mh
, mp
, &hdr_info
) != 0) {
2490 * Extract and validate the VLAN ID for this packet.
2492 if (!bridge_get_vlan(blp
, &hdr_info
, mp
, &vlanid
, &tci
) ||
2493 !BRIDGE_AF_ISSET(blp
, vlanid
)) {
2499 * If we're using TRILL, then we've now validated that we're
2500 * the forwarder for this VLAN, so go ahead and let
2501 * observability node users know about the packet.
2503 if (trillmode
&& (bmp
->bm_flags
& BMF_STARTED
) &&
2504 (mpcopy
= copymsg(mp
)) != NULL
) {
2505 mac_rx(bmp
->bm_mh
, NULL
, mpcopy
);
2509 * We have to learn from our own transmitted packets, because
2510 * there may be a Solaris DLPI raw sender (who can specify his
2511 * own source address) using promiscuous mode for receive. The
2512 * mac layer information won't (and can't) tell us everything
2515 bridge_learn(blp
, hdr_info
.mhi_saddr
, RBRIDGE_NICKNAME_NONE
,
2518 /* attempt forwarding */
2519 if (trillmode
|| blp
->bl_state
== BLS_FORWARDING
) {
2520 mp
= bridge_forward(blp
, &hdr_info
, mp
, vlanid
, tci
,
2524 MAC_RING_TX(blp
->bl_mh
, rh
, mp
, mp
);
2531 * If we get stuck, then stop. Don't let the user's output
2532 * packets get out of order. (More importantly: don't try to
2533 * bridge the same packet multiple times if flow control is
2537 mp
->b_next
= mpnext
;
2545 * This is called by TRILL when it decapsulates an packet, and we must forward
2546 * locally. On failure, we just drop.
2548 * Note that the ingress_nick reported by TRILL must not represent this local
2552 bridge_trill_decaps(bridge_link_t
*blp
, mblk_t
*mp
, uint16_t ingress_nick
)
2554 mac_header_info_t hdr_info
;
2555 uint16_t vlanid
, tci
;
2556 bridge_inst_t
*bip
= blp
->bl_inst
; /* used by macros */
2559 if (mac_header_info(blp
->bl_mh
, mp
, &hdr_info
) != 0) {
2564 /* Extract VLAN ID for this packet. */
2565 if (hdr_info
.mhi_bindsap
== ETHERTYPE_VLAN
) {
2566 struct ether_vlan_header
*evhp
;
2568 /* LINTED: alignment */
2569 evhp
= (struct ether_vlan_header
*)mp
->b_rptr
;
2570 tci
= ntohs(evhp
->ether_tci
);
2571 vlanid
= VLAN_ID(tci
);
2573 /* Inner VLAN headers are required in TRILL data packets */
2574 DTRACE_PROBE3(bridge__trill__decaps__novlan
, bridge_link_t
*,
2575 blp
, mblk_t
*, mp
, uint16_t, ingress_nick
);
2580 /* Learn the location of this sender in the RBridge network */
2581 bridge_learn(blp
, hdr_info
.mhi_saddr
, ingress_nick
, vlanid
);
2583 /* attempt forwarding */
2584 mp
= bridge_forward(blp
, &hdr_info
, mp
, vlanid
, tci
, B_TRUE
, B_TRUE
);
2586 if (bridge_can_send(blp
, vlanid
)) {
2587 /* Deliver a copy locally as well */
2588 if ((mpcopy
= copymsg(mp
)) != NULL
)
2589 mac_rx_common(blp
->bl_mh
, NULL
, mpcopy
);
2590 MAC_RING_TX(blp
->bl_mh
, NULL
, mp
, mp
);
2602 * This function is used by TRILL _only_ to transmit TRILL-encapsulated
2603 * packets. It sends on a single underlying link and does not bridge.
2606 bridge_trill_output(bridge_link_t
*blp
, mblk_t
*mp
)
2608 bridge_inst_t
*bip
= blp
->bl_inst
; /* used by macros */
2610 mac_trill_snoop(blp
->bl_mh
, mp
);
2611 MAC_RING_TX(blp
->bl_mh
, NULL
, mp
, mp
);
2620 * Set the "appointed forwarder" flag array for this link. TRILL controls
2621 * forwarding on a VLAN basis. The "trillactive" flag is an optimization for
2625 bridge_trill_setvlans(bridge_link_t
*blp
, const uint8_t *arr
)
2628 uint_t newflags
= 0;
2630 for (i
= 0; i
< BRIDGE_VLAN_ARR_SIZE
; i
++) {
2631 if ((blp
->bl_afs
[i
] = arr
[i
]) != 0)
2632 newflags
= BLF_TRILLACTIVE
;
2634 blp
->bl_flags
= (blp
->bl_flags
& ~BLF_TRILLACTIVE
) | newflags
;
2638 bridge_trill_flush(bridge_link_t
*blp
, uint16_t vlan
, boolean_t dotrill
)
2640 bridge_inst_t
*bip
= blp
->bl_inst
;
2641 bridge_fwd_t
*bfp
, *bfnext
;
2642 avl_tree_t fwd_scavenge
;
2645 _NOTE(ARGUNUSED(vlan
));
2647 avl_create(&fwd_scavenge
, fwd_compare
, sizeof (bridge_fwd_t
),
2648 offsetof(bridge_fwd_t
, bf_node
));
2649 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
2650 bfnext
= avl_first(&bip
->bi_fwd
);
2651 while ((bfp
= bfnext
) != NULL
) {
2652 bfnext
= AVL_NEXT(&bip
->bi_fwd
, bfp
);
2653 if (bfp
->bf_flags
& BFF_LOCALADDR
)
2656 /* port doesn't matter if we're flushing TRILL */
2657 if (bfp
->bf_trill_nick
== RBRIDGE_NICKNAME_NONE
)
2660 if (bfp
->bf_trill_nick
!= RBRIDGE_NICKNAME_NONE
)
2662 for (i
= 0; i
< bfp
->bf_nlinks
; i
++) {
2663 if (bfp
->bf_links
[i
] == blp
)
2666 if (i
>= bfp
->bf_nlinks
)
2669 ASSERT(bfp
->bf_flags
& BFF_INTREE
);
2670 avl_remove(&bip
->bi_fwd
, bfp
);
2671 bfp
->bf_flags
&= ~BFF_INTREE
;
2672 avl_add(&fwd_scavenge
, bfp
);
2674 rw_exit(&bip
->bi_rwlock
);
2675 bfnext
= avl_first(&fwd_scavenge
);
2676 while ((bfp
= bfnext
) != NULL
) {
2677 bfnext
= AVL_NEXT(&fwd_scavenge
, bfp
);
2678 avl_remove(&fwd_scavenge
, bfp
);
2681 avl_destroy(&fwd_scavenge
);
2685 * Let the mac module take or drop a reference to a bridge link. When this is
2686 * called, the mac module is holding the mi_bridge_lock, so the link cannot be
2687 * in the process of entering or leaving a bridge.
2690 bridge_ref_cb(mac_handle_t mh
, boolean_t hold
)
2692 bridge_link_t
*blp
= (bridge_link_t
*)mh
;
2695 atomic_inc_uint(&blp
->bl_refs
);
2701 * Handle link state changes reported by the mac layer. This acts as a filter
2702 * for link state changes: if a link is reporting down, but there are other
2703 * links still up on the bridge, then the state is changed to "up." When the
2704 * last link goes down, all are marked down, and when the first link goes up,
2705 * all are marked up. (Recursion is avoided by the use of the "redo" function.)
2707 * We treat unknown as equivalent to "up."
2710 bridge_ls_cb(mac_handle_t mh
, link_state_t newls
)
2712 bridge_link_t
*blp
= (bridge_link_t
*)mh
;
2713 bridge_link_t
*blcmp
;
2717 if (newls
!= LINK_STATE_DOWN
&& blp
->bl_linkstate
!= LINK_STATE_DOWN
||
2718 (blp
->bl_flags
& (BLF_DELETED
|BLF_SDUFAIL
))) {
2719 blp
->bl_linkstate
= newls
;
2724 * Scan first to see if there are any other non-down links. If there
2725 * are, then we're done. Otherwise, if all others are down, then the
2726 * state of this link is the state of the bridge.
2729 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
2730 for (blcmp
= list_head(&bip
->bi_links
); blcmp
!= NULL
;
2731 blcmp
= list_next(&bip
->bi_links
, blcmp
)) {
2733 !(blcmp
->bl_flags
& (BLF_DELETED
|BLF_SDUFAIL
)) &&
2734 blcmp
->bl_linkstate
!= LINK_STATE_DOWN
)
2738 if (blcmp
!= NULL
) {
2740 * If there are other links that are considered up, then tell
2741 * the caller that the link is actually still up, regardless of
2742 * this link's underlying state.
2744 blp
->bl_linkstate
= newls
;
2745 newls
= LINK_STATE_UP
;
2746 } else if (blp
->bl_linkstate
!= newls
) {
2748 * If we've found no other 'up' links, and this link has
2749 * changed state, then report the new state of the bridge to
2750 * all other clients.
2752 blp
->bl_linkstate
= newls
;
2753 for (blcmp
= list_head(&bip
->bi_links
); blcmp
!= NULL
;
2754 blcmp
= list_next(&bip
->bi_links
, blcmp
)) {
2755 if (blcmp
!= blp
&& !(blcmp
->bl_flags
& BLF_DELETED
))
2756 mac_link_redo(blcmp
->bl_mh
, newls
);
2759 if ((bmp
->bm_linkstate
= newls
) != LINK_STATE_DOWN
)
2760 bmp
->bm_linkstate
= LINK_STATE_UP
;
2761 mac_link_redo(bmp
->bm_mh
, bmp
->bm_linkstate
);
2763 rw_exit(&bip
->bi_rwlock
);
2768 bridge_add_link(void *arg
)
2771 bridge_stream_t
*bsp
;
2772 bridge_inst_t
*bip
, *bipt
;
2774 datalink_id_t linkid
;
2778 bridge_link_t
*blp
= NULL
, *blpt
;
2779 const mac_info_t
*mip
;
2780 boolean_t macopen
= B_FALSE
;
2781 char linkname
[MAXLINKNAMELEN
];
2782 char kstatname
[KSTAT_STRLEN
];
2784 link_state_t linkstate
;
2787 bsp
= (bridge_stream_t
*)mp
->b_next
;
2790 /* LINTED: alignment */
2791 linkid
= *(datalink_id_t
*)mp
->b_cont
->b_rptr
;
2794 * First make sure that there is no other bridge that has this link.
2795 * We don't want to overlap operations from two bridges; the MAC layer
2796 * supports only one bridge on a given MAC at a time.
2798 * We rely on the fact that there's just one taskq thread for the
2799 * bridging module: once we've checked for a duplicate, we can drop the
2800 * lock, because no other thread could possibly be adding another link
2803 mutex_enter(&inst_lock
);
2804 for (bipt
= list_head(&inst_list
); bipt
!= NULL
;
2805 bipt
= list_next(&inst_list
, bipt
)) {
2806 rw_enter(&bipt
->bi_rwlock
, RW_READER
);
2807 for (blpt
= list_head(&bipt
->bi_links
); blpt
!= NULL
;
2808 blpt
= list_next(&bipt
->bi_links
, blpt
)) {
2809 if (linkid
== blpt
->bl_linkid
)
2812 rw_exit(&bipt
->bi_rwlock
);
2816 mutex_exit(&inst_lock
);
2822 if ((err
= mac_open_by_linkid(linkid
, &mh
)) != 0)
2826 /* we bridge only Ethernet */
2828 if (mip
->mi_media
!= DL_ETHER
) {
2834 * Get the current maximum SDU on this interface. If there are other
2835 * links on the bridge, then this one must match, or it errors out.
2836 * Otherwise, the first link becomes the standard for the new bridge.
2838 mac_sdu_get(mh
, NULL
, &maxsdu
);
2840 if (list_is_empty(&bip
->bi_links
)) {
2841 bmp
->bm_maxsdu
= maxsdu
;
2842 (void) mac_maxsdu_update(bmp
->bm_mh
, maxsdu
);
2845 /* figure the kstat name; also used as the mac client name */
2846 i
= MBLKL(mp
->b_cont
) - sizeof (datalink_id_t
);
2847 if (i
< 0 || i
>= MAXLINKNAMELEN
)
2848 i
= MAXLINKNAMELEN
- 1;
2849 bcopy(mp
->b_cont
->b_rptr
+ sizeof (datalink_id_t
), linkname
, i
);
2851 (void) snprintf(kstatname
, sizeof (kstatname
), "%s-%s", bip
->bi_name
,
2854 if ((blp
= kmem_zalloc(sizeof (*blp
), KM_NOSLEEP
)) == NULL
) {
2858 blp
->bl_lfailmp
= allocb(sizeof (bridge_ctl_t
), BPRI_MED
);
2859 if (blp
->bl_lfailmp
== NULL
) {
2860 kmem_free(blp
, sizeof (*blp
));
2867 atomic_inc_uint(&bip
->bi_refs
);
2870 blp
->bl_linkid
= linkid
;
2871 blp
->bl_maxsdu
= maxsdu
;
2872 cv_init(&blp
->bl_trillwait
, NULL
, CV_DRIVER
, NULL
);
2873 mutex_init(&blp
->bl_trilllock
, NULL
, MUTEX_DRIVER
, NULL
);
2874 (void) memset(blp
->bl_afs
, 0xff, sizeof (blp
->bl_afs
));
2876 err
= mac_client_open(mh
, &blp
->bl_mch
, kstatname
, 0);
2879 blp
->bl_flags
|= BLF_CLIENT_OPEN
;
2881 err
= mac_margin_add(mh
, &blp
->bl_margin
, B_TRUE
);
2884 blp
->bl_flags
|= BLF_MARGIN_ADDED
;
2886 blp
->bl_mnh
= mac_notify_add(mh
, bridge_notify_cb
, blp
);
2888 /* Enable Bridging on the link */
2889 err
= mac_bridge_set(mh
, (mac_handle_t
)blp
);
2892 blp
->bl_flags
|= BLF_SET_BRIDGE
;
2894 err
= mac_promisc_add(blp
->bl_mch
, MAC_CLIENT_PROMISC_ALL
, NULL
,
2895 blp
, &blp
->bl_mphp
, MAC_PROMISC_FLAGS_NO_TX_LOOP
);
2898 blp
->bl_flags
|= BLF_PROM_ADDED
;
2900 bridge_new_unicst(blp
);
2902 blp
->bl_ksp
= kstat_setup((kstat_named_t
*)&blp
->bl_kstats
,
2903 link_kstats_list
, Dim(link_kstats_list
), kstatname
);
2906 * The link holds a reference to the bridge instance, so that the
2907 * instance can't go away before the link is freed. The insertion into
2908 * bi_links holds a reference on the link (reference set to 1 above).
2909 * When marking as removed from bi_links (BLF_DELETED), drop the
2910 * reference on the link. When freeing the link, drop the reference on
2911 * the instance. BLF_LINK_ADDED tracks link insertion in bi_links list.
2913 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
2914 list_insert_tail(&bip
->bi_links
, blp
);
2915 blp
->bl_flags
|= BLF_LINK_ADDED
;
2918 * If the new link is no good on this bridge, then let the daemon know
2919 * about the problem.
2922 if (maxsdu
!= bmp
->bm_maxsdu
)
2923 link_sdu_fail(blp
, B_TRUE
, &mlist
);
2924 rw_exit(&bip
->bi_rwlock
);
2925 send_up_messages(bip
, mlist
);
2928 * Trigger a link state update so that if this link is the first one
2929 * "up" in the bridge, then we notify everyone. This triggers a trip
2930 * through bridge_ls_cb.
2932 linkstate
= mac_stat_get(mh
, MAC_STAT_LOWLINK_STATE
);
2933 blp
->bl_linkstate
= LINK_STATE_DOWN
;
2934 mac_link_update(mh
, linkstate
);
2937 * We now need to report back to the stream that invoked us, and then
2938 * drop the reference on the stream that we're holding.
2940 miocack(bsp
->bs_wq
, mp
, 0, 0);
2951 miocnak(bsp
->bs_wq
, mp
, 0, err
);
2956 bridge_rem_link(void *arg
)
2959 bridge_stream_t
*bsp
;
2962 datalink_id_t linkid
;
2963 bridge_link_t
*blp
, *blsave
;
2967 bsp
= (bridge_stream_t
*)mp
->b_next
;
2970 /* LINTED: alignment */
2971 linkid
= *(datalink_id_t
*)mp
->b_cont
->b_rptr
;
2974 * We become reader here so that we can loop over the other links and
2975 * deliver link up/down notification.
2977 rw_enter(&bip
->bi_rwlock
, RW_READER
);
2979 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
2980 blp
= list_next(&bip
->bi_links
, blp
)) {
2981 if (blp
->bl_linkid
== linkid
&&
2982 !(blp
->bl_flags
& BLF_DELETED
)) {
2983 blp
->bl_flags
|= BLF_DELETED
;
2984 (void) ddi_taskq_dispatch(bridge_taskq
, link_shutdown
,
2992 * Check if this link is up and the remainder of the links are all
2995 if (blp
!= NULL
&& blp
->bl_linkstate
!= LINK_STATE_DOWN
) {
2996 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
2997 blp
= list_next(&bip
->bi_links
, blp
)) {
2998 if (blp
->bl_linkstate
!= LINK_STATE_DOWN
&&
2999 !(blp
->bl_flags
& (BLF_DELETED
|BLF_SDUFAIL
)))
3003 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
3004 blp
= list_next(&bip
->bi_links
, blp
)) {
3005 if (!(blp
->bl_flags
& BLF_DELETED
))
3006 mac_link_redo(blp
->bl_mh
,
3010 bmp
->bm_linkstate
= LINK_STATE_DOWN
;
3011 mac_link_redo(bmp
->bm_mh
, LINK_STATE_DOWN
);
3016 * Check if there's just one working link left on the bridge. If so,
3017 * then that link is now authoritative for bridge MTU.
3020 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
3021 blp
= list_next(&bip
->bi_links
, blp
)) {
3022 if (!(blp
->bl_flags
& BLF_DELETED
)) {
3031 if (blsave
!= NULL
&& blp
== NULL
&&
3032 blsave
->bl_maxsdu
!= bmp
->bm_maxsdu
) {
3033 bmp
->bm_maxsdu
= blsave
->bl_maxsdu
;
3034 (void) mac_maxsdu_update(bmp
->bm_mh
, blsave
->bl_maxsdu
);
3035 link_sdu_fail(blsave
, B_FALSE
, &mlist
);
3037 rw_exit(&bip
->bi_rwlock
);
3038 send_up_messages(bip
, mlist
);
3041 miocack(bsp
->bs_wq
, mp
, 0, 0);
3043 miocnak(bsp
->bs_wq
, mp
, 0, ENOENT
);
3048 * This function intentionally returns with bi_rwlock held; it is intended for
3049 * quick checks and updates.
3051 static bridge_link_t
*
3052 enter_link(bridge_inst_t
*bip
, datalink_id_t linkid
)
3056 rw_enter(&bip
->bi_rwlock
, RW_READER
);
3057 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
3058 blp
= list_next(&bip
->bi_links
, blp
)) {
3059 if (blp
->bl_linkid
== linkid
&& !(blp
->bl_flags
& BLF_DELETED
))
3066 bridge_ioctl(queue_t
*wq
, mblk_t
*mp
)
3068 bridge_stream_t
*bsp
= wq
->q_ptr
;
3076 /* LINTED: alignment */
3077 iop
= (struct iocblk
*)mp
->b_rptr
;
3080 * For now, all of the bridge ioctls are privileged.
3082 if ((cr
= msg_getcred(mp
, NULL
)) == NULL
)
3084 if (cr
!= NULL
&& secpolicy_net_config(cr
, B_FALSE
) != 0) {
3085 miocnak(wq
, mp
, 0, EPERM
);
3089 switch (iop
->ioc_cmd
) {
3090 case BRIOC_NEWBRIDGE
: {
3091 bridge_newbridge_t
*bnb
;
3093 if (bsp
->bs_inst
!= NULL
||
3094 (rc
= miocpullup(mp
, sizeof (bridge_newbridge_t
))) != 0)
3096 /* LINTED: alignment */
3097 bnb
= (bridge_newbridge_t
*)mp
->b_cont
->b_rptr
;
3098 bnb
->bnb_name
[MAXNAMELEN
-1] = '\0';
3099 rc
= bridge_create(bnb
->bnb_linkid
, bnb
->bnb_name
, &bip
, cr
);
3103 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
3104 if (bip
->bi_control
!= NULL
) {
3105 rw_exit(&bip
->bi_rwlock
);
3109 atomic_inc_uint(&bip
->bi_refs
);
3110 bsp
->bs_inst
= bip
; /* stream holds reference */
3111 bip
->bi_control
= bsp
;
3112 rw_exit(&bip
->bi_rwlock
);
3119 if ((bip
= bsp
->bs_inst
) == NULL
||
3120 (rc
= miocpullup(mp
, sizeof (datalink_id_t
))) != 0)
3123 * We cannot perform the action in this thread, because we're
3124 * not in process context, and we may already be holding
3125 * MAC-related locks. Place the request on taskq.
3127 mp
->b_next
= (mblk_t
*)bsp
;
3129 (void) ddi_taskq_dispatch(bridge_taskq
, bridge_add_link
, mp
,
3134 if ((bip
= bsp
->bs_inst
) == NULL
||
3135 (rc
= miocpullup(mp
, sizeof (datalink_id_t
))) != 0)
3138 * We cannot perform the action in this thread, because we're
3139 * not in process context, and we may already be holding
3140 * MAC-related locks. Place the request on taskq.
3142 mp
->b_next
= (mblk_t
*)bsp
;
3144 (void) ddi_taskq_dispatch(bridge_taskq
, bridge_rem_link
, mp
,
3148 case BRIOC_SETSTATE
: {
3149 bridge_setstate_t
*bss
;
3151 if ((bip
= bsp
->bs_inst
) == NULL
||
3152 (rc
= miocpullup(mp
, sizeof (*bss
))) != 0)
3154 /* LINTED: alignment */
3155 bss
= (bridge_setstate_t
*)mp
->b_cont
->b_rptr
;
3156 if ((blp
= enter_link(bip
, bss
->bss_linkid
)) == NULL
) {
3160 blp
->bl_state
= bss
->bss_state
;
3162 rw_exit(&bip
->bi_rwlock
);
3166 case BRIOC_SETPVID
: {
3167 bridge_setpvid_t
*bsv
;
3169 if ((bip
= bsp
->bs_inst
) == NULL
||
3170 (rc
= miocpullup(mp
, sizeof (*bsv
))) != 0)
3172 /* LINTED: alignment */
3173 bsv
= (bridge_setpvid_t
*)mp
->b_cont
->b_rptr
;
3174 if (bsv
->bsv_vlan
> VLAN_ID_MAX
)
3176 if ((blp
= enter_link(bip
, bsv
->bsv_linkid
)) == NULL
) {
3178 } else if (blp
->bl_pvid
== bsv
->bsv_vlan
) {
3182 BRIDGE_VLAN_CLR(blp
, blp
->bl_pvid
);
3183 blp
->bl_pvid
= bsv
->bsv_vlan
;
3184 if (blp
->bl_pvid
!= 0)
3185 BRIDGE_VLAN_SET(blp
, blp
->bl_pvid
);
3187 rw_exit(&bip
->bi_rwlock
);
3191 case BRIOC_VLANENAB
: {
3192 bridge_vlanenab_t
*bve
;
3194 if ((bip
= bsp
->bs_inst
) == NULL
||
3195 (rc
= miocpullup(mp
, sizeof (*bve
))) != 0)
3197 /* LINTED: alignment */
3198 bve
= (bridge_vlanenab_t
*)mp
->b_cont
->b_rptr
;
3199 if (bve
->bve_vlan
> VLAN_ID_MAX
)
3201 if ((blp
= enter_link(bip
, bve
->bve_linkid
)) == NULL
) {
3205 /* special case: vlan 0 means "all" */
3206 if (bve
->bve_vlan
== 0) {
3207 (void) memset(blp
->bl_vlans
,
3208 bve
->bve_onoff
? ~0 : 0,
3209 sizeof (blp
->bl_vlans
));
3210 BRIDGE_VLAN_CLR(blp
, 0);
3211 if (blp
->bl_pvid
!= 0)
3212 BRIDGE_VLAN_SET(blp
, blp
->bl_pvid
);
3213 } else if (bve
->bve_vlan
== blp
->bl_pvid
) {
3215 } else if (bve
->bve_onoff
) {
3216 BRIDGE_VLAN_SET(blp
, bve
->bve_vlan
);
3218 BRIDGE_VLAN_CLR(blp
, bve
->bve_vlan
);
3221 rw_exit(&bip
->bi_rwlock
);
3225 case BRIOC_FLUSHFWD
: {
3226 bridge_flushfwd_t
*bff
;
3227 bridge_fwd_t
*bfp
, *bfnext
;
3228 avl_tree_t fwd_scavenge
;
3231 if ((bip
= bsp
->bs_inst
) == NULL
||
3232 (rc
= miocpullup(mp
, sizeof (*bff
))) != 0)
3234 /* LINTED: alignment */
3235 bff
= (bridge_flushfwd_t
*)mp
->b_cont
->b_rptr
;
3236 rw_enter(&bip
->bi_rwlock
, RW_WRITER
);
3237 /* This case means "all" */
3238 if (bff
->bff_linkid
== DATALINK_INVALID_LINKID
) {
3241 for (blp
= list_head(&bip
->bi_links
); blp
!= NULL
;
3242 blp
= list_next(&bip
->bi_links
, blp
)) {
3243 if (blp
->bl_linkid
== bff
->bff_linkid
&&
3244 !(blp
->bl_flags
& BLF_DELETED
))
3249 rw_exit(&bip
->bi_rwlock
);
3253 avl_create(&fwd_scavenge
, fwd_compare
, sizeof (bridge_fwd_t
),
3254 offsetof(bridge_fwd_t
, bf_node
));
3255 bfnext
= avl_first(&bip
->bi_fwd
);
3256 while ((bfp
= bfnext
) != NULL
) {
3257 bfnext
= AVL_NEXT(&bip
->bi_fwd
, bfp
);
3258 if (bfp
->bf_flags
& BFF_LOCALADDR
)
3261 for (i
= 0; i
< bfp
->bf_maxlinks
; i
++) {
3262 if (bfp
->bf_links
[i
] == blp
)
3266 * If the link is there and we're excluding,
3267 * then skip. If the link is not there and
3268 * we're doing only that link, then skip.
3270 if ((i
< bfp
->bf_maxlinks
) == bff
->bff_exclude
)
3273 ASSERT(bfp
->bf_flags
& BFF_INTREE
);
3274 avl_remove(&bip
->bi_fwd
, bfp
);
3275 bfp
->bf_flags
&= ~BFF_INTREE
;
3276 avl_add(&fwd_scavenge
, bfp
);
3278 rw_exit(&bip
->bi_rwlock
);
3279 bfnext
= avl_first(&fwd_scavenge
);
3280 while ((bfp
= bfnext
) != NULL
) {
3281 bfnext
= AVL_NEXT(&fwd_scavenge
, bfp
);
3282 avl_remove(&fwd_scavenge
, bfp
);
3283 fwd_unref(bfp
); /* drop tree reference */
3285 avl_destroy(&fwd_scavenge
);
3289 case BRIOC_TABLEMAX
:
3290 if ((bip
= bsp
->bs_inst
) == NULL
||
3291 (rc
= miocpullup(mp
, sizeof (uint32_t))) != 0)
3293 /* LINTED: alignment */
3294 bip
->bi_tablemax
= *(uint32_t *)mp
->b_cont
->b_rptr
;
3299 miocack(wq
, mp
, len
, 0);
3301 miocnak(wq
, mp
, 0, rc
);
3305 bridge_wput(queue_t
*wq
, mblk_t
*mp
)
3307 switch (DB_TYPE(mp
)) {
3309 bridge_ioctl(wq
, mp
);
3312 if (*mp
->b_rptr
& FLUSHW
)
3313 *mp
->b_rptr
&= ~FLUSHW
;
3314 if (*mp
->b_rptr
& FLUSHR
)
3326 * This function allocates the main data structures for the bridge driver and
3327 * connects us into devfs.
3330 bridge_inst_init(void)
3332 bridge_scan_interval
= 5 * drv_usectohz(1000000);
3333 bridge_fwd_age
= 25 * drv_usectohz(1000000);
3335 rw_init(&bmac_rwlock
, NULL
, RW_DRIVER
, NULL
);
3336 list_create(&bmac_list
, sizeof (bridge_mac_t
),
3337 offsetof(bridge_mac_t
, bm_node
));
3338 list_create(&inst_list
, sizeof (bridge_inst_t
),
3339 offsetof(bridge_inst_t
, bi_node
));
3340 cv_init(&inst_cv
, NULL
, CV_DRIVER
, NULL
);
3341 mutex_init(&inst_lock
, NULL
, MUTEX_DRIVER
, NULL
);
3342 cv_init(&stream_ref_cv
, NULL
, CV_DRIVER
, NULL
);
3343 mutex_init(&stream_ref_lock
, NULL
, MUTEX_DRIVER
, NULL
);
3345 mac_bridge_vectors(bridge_xmit_cb
, bridge_recv_cb
, bridge_ref_cb
,
3350 * This function disconnects from devfs and destroys all data structures in
3351 * preparation for unload. It's assumed that there are no active bridge
3352 * references left at this point.
3355 bridge_inst_fini(void)
3357 mac_bridge_vectors(NULL
, NULL
, NULL
, NULL
);
3358 if (bridge_timerid
!= 0)
3359 (void) untimeout(bridge_timerid
);
3360 rw_destroy(&bmac_rwlock
);
3361 list_destroy(&bmac_list
);
3362 list_destroy(&inst_list
);
3363 cv_destroy(&inst_cv
);
3364 mutex_destroy(&inst_lock
);
3365 cv_destroy(&stream_ref_cv
);
3366 mutex_destroy(&stream_ref_lock
);
3373 * Attach bridge driver to the system.
3376 bridge_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
3378 if (cmd
!= DDI_ATTACH
)
3379 return (DDI_FAILURE
);
3381 if (ddi_create_minor_node(dip
, BRIDGE_CTL
, S_IFCHR
, 0, DDI_PSEUDO
,
3382 CLONE_DEV
) == DDI_FAILURE
) {
3383 return (DDI_FAILURE
);
3386 if (dld_ioc_register(BRIDGE_IOC
, bridge_ioc_list
,
3387 DLDIOCCNT(bridge_ioc_list
)) != 0) {
3388 ddi_remove_minor_node(dip
, BRIDGE_CTL
);
3389 return (DDI_FAILURE
);
3392 bridge_dev_info
= dip
;
3393 bridge_major
= ddi_driver_major(dip
);
3394 bridge_taskq
= ddi_taskq_create(dip
, BRIDGE_DEV_NAME
, 1,
3395 TASKQ_DEFAULTPRI
, 0);
3396 return (DDI_SUCCESS
);
3403 * Detach an interface to the system.
3406 bridge_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
3408 if (cmd
!= DDI_DETACH
)
3409 return (DDI_FAILURE
);
3411 ddi_remove_minor_node(dip
, NULL
);
3412 ddi_taskq_destroy(bridge_taskq
);
3413 bridge_dev_info
= NULL
;
3414 return (DDI_SUCCESS
);
3421 * Translate "dev_t" to a pointer to the associated "dev_info_t".
3425 bridge_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
,
3431 case DDI_INFO_DEVT2DEVINFO
:
3432 if (bridge_dev_info
== NULL
) {
3435 *result
= (void *)bridge_dev_info
;
3439 case DDI_INFO_DEVT2INSTANCE
:
3450 static struct module_info bridge_modinfo
= {
3451 2105, /* mi_idnum */
3452 BRIDGE_DEV_NAME
, /* mi_idname */
3454 16384, /* mi_maxpsz */
3455 65536, /* mi_hiwat */
3459 static struct qinit bridge_rinit
= {
3462 bridge_open
, /* qi_qopen */
3463 bridge_close
, /* qi_qclose */
3464 NULL
, /* qi_qadmin */
3465 &bridge_modinfo
, /* qi_minfo */
3469 static struct qinit bridge_winit
= {
3470 (int (*)())bridge_wput
, /* qi_putp */
3472 NULL
, /* qi_qopen */
3473 NULL
, /* qi_qclose */
3474 NULL
, /* qi_qadmin */
3475 &bridge_modinfo
, /* qi_minfo */
3479 static struct streamtab bridge_tab
= {
3480 &bridge_rinit
, /* st_rdinit */
3481 &bridge_winit
/* st_wrinit */
3484 /* No STREAMS perimeters; we do all our own locking */
3485 DDI_DEFINE_STREAM_OPS(bridge_ops
, nulldev
, nulldev
, bridge_attach
,
3486 bridge_detach
, nodev
, bridge_info
, D_NEW
| D_MP
, &bridge_tab
,
3487 ddi_quiesce_not_supported
);
3489 static struct modldrv modldrv
= {
3495 static struct modlinkage modlinkage
= {
3506 mac_init_ops(NULL
, BRIDGE_DEV_NAME
);
3508 if ((retv
= mod_install(&modlinkage
)) != 0)
3518 rw_enter(&bmac_rwlock
, RW_READER
);
3519 retv
= list_is_empty(&bmac_list
) ? 0 : EBUSY
;
3520 rw_exit(&bmac_rwlock
);
3522 (retv
= mod_remove(&modlinkage
)) == 0)
3528 _info(struct modinfo
*modinfop
)
3530 return (mod_info(&modlinkage
, modinfop
));