Home | History | Annotate | Line # | Download | only in net
if_bridge.c revision 1.141
      1  1.141     ozaki /*	$NetBSD: if_bridge.c,v 1.141 2017/11/17 07:52:07 ozaki-r Exp $	*/
      2    1.1   thorpej 
      3    1.1   thorpej /*
      4    1.1   thorpej  * Copyright 2001 Wasabi Systems, Inc.
      5    1.1   thorpej  * All rights reserved.
      6    1.1   thorpej  *
      7    1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8    1.1   thorpej  *
      9    1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10    1.1   thorpej  * modification, are permitted provided that the following conditions
     11    1.1   thorpej  * are met:
     12    1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13    1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14    1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15    1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16    1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17    1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18    1.1   thorpej  *    must display the following acknowledgement:
     19    1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     20    1.1   thorpej  *	Wasabi Systems, Inc.
     21    1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22    1.1   thorpej  *    or promote products derived from this software without specific prior
     23    1.1   thorpej  *    written permission.
     24    1.1   thorpej  *
     25    1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26    1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27    1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28    1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29    1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30    1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31    1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32    1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33    1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34    1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35    1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36    1.1   thorpej  */
     37    1.1   thorpej 
     38   1.29     perry /*
     39    1.1   thorpej  * Copyright (c) 1999, 2000 Jason L. Wright (jason (at) thought.net)
     40   1.29     perry  * All rights reserved.
     41    1.1   thorpej  *
     42   1.29     perry  * Redistribution and use in source and binary forms, with or without
     43   1.29     perry  * modification, are permitted provided that the following conditions
     44    1.1   thorpej  * are met:
     45    1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     46    1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     47    1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     48    1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     49    1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     50    1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     51    1.1   thorpej  *    must display the following acknowledgement:
     52    1.1   thorpej  *	This product includes software developed by Jason L. Wright
     53    1.1   thorpej  * 4. The name of the author may not be used to endorse or promote products
     54    1.1   thorpej  *    derived from this software without specific prior written permission.
     55    1.1   thorpej  *
     56    1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     57    1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     58    1.1   thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     59    1.1   thorpej  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     60    1.1   thorpej  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     61    1.1   thorpej  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     62    1.1   thorpej  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63    1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     64    1.1   thorpej  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     65    1.1   thorpej  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66    1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     67    1.1   thorpej  *
     68    1.1   thorpej  * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
     69    1.1   thorpej  */
     70    1.1   thorpej 
     71    1.1   thorpej /*
     72    1.1   thorpej  * Network interface bridge support.
     73    1.1   thorpej  *
     74    1.1   thorpej  * TODO:
     75    1.1   thorpej  *
     76    1.1   thorpej  *	- Currently only supports Ethernet-like interfaces (Ethernet,
     77    1.1   thorpej  *	  802.11, VLANs on Ethernet, etc.)  Figure out a nice way
     78    1.1   thorpej  *	  to bridge other types of interfaces (FDDI-FDDI, and maybe
     79    1.1   thorpej  *	  consider heterogenous bridges).
     80    1.1   thorpej  */
     81    1.3     lukem 
     82    1.3     lukem #include <sys/cdefs.h>
     83  1.141     ozaki __KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.141 2017/11/17 07:52:07 ozaki-r Exp $");
     84    1.1   thorpej 
     85   1.72     pooka #ifdef _KERNEL_OPT
     86   1.10  perseant #include "opt_bridge_ipf.h"
     87   1.16       jdc #include "opt_inet.h"
     88  1.114     ozaki #include "opt_net_mpsafe.h"
     89   1.72     pooka #endif /* _KERNEL_OPT */
     90    1.1   thorpej 
     91   1.29     perry #include <sys/param.h>
     92    1.1   thorpej #include <sys/kernel.h>
     93    1.1   thorpej #include <sys/mbuf.h>
     94    1.1   thorpej #include <sys/queue.h>
     95    1.1   thorpej #include <sys/socket.h>
     96   1.68    bouyer #include <sys/socketvar.h> /* for softnet_lock */
     97    1.1   thorpej #include <sys/sockio.h>
     98    1.1   thorpej #include <sys/systm.h>
     99   1.29     perry #include <sys/proc.h>
    100    1.1   thorpej #include <sys/pool.h>
    101   1.37      elad #include <sys/kauth.h>
    102   1.65    bouyer #include <sys/cpu.h>
    103   1.74       tls #include <sys/cprng.h>
    104   1.86     ozaki #include <sys/mutex.h>
    105   1.93     ozaki #include <sys/kmem.h>
    106    1.1   thorpej 
    107    1.1   thorpej #include <net/bpf.h>
    108    1.1   thorpej #include <net/if.h>
    109    1.1   thorpej #include <net/if_dl.h>
    110    1.1   thorpej #include <net/if_types.h>
    111    1.1   thorpej #include <net/if_llc.h>
    112    1.1   thorpej 
    113    1.1   thorpej #include <net/if_ether.h>
    114    1.1   thorpej #include <net/if_bridgevar.h>
    115    1.1   thorpej 
    116   1.77     rmind #if defined(BRIDGE_IPF)
    117   1.25  christos /* Used for bridge_ip[6]_checkbasic */
    118    1.9  perseant #include <netinet/in.h>
    119    1.9  perseant #include <netinet/in_systm.h>
    120    1.9  perseant #include <netinet/ip.h>
    121    1.9  perseant #include <netinet/ip_var.h>
    122   1.61   thorpej #include <netinet/ip_private.h>		/* XXX */
    123    1.9  perseant 
    124    1.9  perseant #include <netinet/ip6.h>
    125    1.9  perseant #include <netinet6/in6_var.h>
    126    1.9  perseant #include <netinet6/ip6_var.h>
    127   1.61   thorpej #include <netinet6/ip6_private.h>	/* XXX */
    128   1.77     rmind #endif /* BRIDGE_IPF */
    129    1.9  perseant 
    130    1.1   thorpej /*
    131    1.1   thorpej  * Size of the route hash table.  Must be a power of two.
    132    1.1   thorpej  */
    133    1.1   thorpej #ifndef BRIDGE_RTHASH_SIZE
    134    1.1   thorpej #define	BRIDGE_RTHASH_SIZE		1024
    135    1.1   thorpej #endif
    136    1.1   thorpej 
    137    1.1   thorpej #define	BRIDGE_RTHASH_MASK		(BRIDGE_RTHASH_SIZE - 1)
    138    1.1   thorpej 
    139   1.38  liamjfoy #include "carp.h"
    140   1.38  liamjfoy #if NCARP > 0
    141   1.38  liamjfoy #include <netinet/in.h>
    142   1.38  liamjfoy #include <netinet/in_var.h>
    143   1.38  liamjfoy #include <netinet/ip_carp.h>
    144   1.38  liamjfoy #endif
    145   1.38  liamjfoy 
    146  1.101  christos #include "ioconf.h"
    147  1.101  christos 
    148   1.99      matt __CTASSERT(sizeof(struct ifbifconf) == sizeof(struct ifbaconf));
    149   1.99      matt __CTASSERT(offsetof(struct ifbifconf, ifbic_len) == offsetof(struct ifbaconf, ifbac_len));
    150   1.99      matt __CTASSERT(offsetof(struct ifbifconf, ifbic_buf) == offsetof(struct ifbaconf, ifbac_buf));
    151   1.99      matt 
    152    1.1   thorpej /*
    153    1.1   thorpej  * Maximum number of addresses to cache.
    154    1.1   thorpej  */
    155    1.1   thorpej #ifndef BRIDGE_RTABLE_MAX
    156    1.1   thorpej #define	BRIDGE_RTABLE_MAX		100
    157    1.1   thorpej #endif
    158    1.1   thorpej 
    159    1.1   thorpej /*
    160    1.1   thorpej  * Spanning tree defaults.
    161    1.1   thorpej  */
    162    1.1   thorpej #define	BSTP_DEFAULT_MAX_AGE		(20 * 256)
    163    1.1   thorpej #define	BSTP_DEFAULT_HELLO_TIME		(2 * 256)
    164    1.1   thorpej #define	BSTP_DEFAULT_FORWARD_DELAY	(15 * 256)
    165    1.1   thorpej #define	BSTP_DEFAULT_HOLD_TIME		(1 * 256)
    166    1.1   thorpej #define	BSTP_DEFAULT_BRIDGE_PRIORITY	0x8000
    167    1.1   thorpej #define	BSTP_DEFAULT_PORT_PRIORITY	0x80
    168    1.1   thorpej #define	BSTP_DEFAULT_PATH_COST		55
    169    1.1   thorpej 
    170    1.1   thorpej /*
    171    1.1   thorpej  * Timeout (in seconds) for entries learned dynamically.
    172    1.1   thorpej  */
    173    1.1   thorpej #ifndef BRIDGE_RTABLE_TIMEOUT
    174    1.1   thorpej #define	BRIDGE_RTABLE_TIMEOUT		(20 * 60)	/* same as ARP */
    175    1.1   thorpej #endif
    176    1.1   thorpej 
    177    1.1   thorpej /*
    178    1.1   thorpej  * Number of seconds between walks of the route list.
    179    1.1   thorpej  */
    180    1.1   thorpej #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
    181    1.1   thorpej #define	BRIDGE_RTABLE_PRUNE_PERIOD	(5 * 60)
    182    1.1   thorpej #endif
    183    1.1   thorpej 
    184   1.97     ozaki #define BRIDGE_RT_LOCK(_sc)	if ((_sc)->sc_rtlist_lock) \
    185   1.97     ozaki 					mutex_enter((_sc)->sc_rtlist_lock)
    186   1.97     ozaki #define BRIDGE_RT_UNLOCK(_sc)	if ((_sc)->sc_rtlist_lock) \
    187   1.97     ozaki 					mutex_exit((_sc)->sc_rtlist_lock)
    188   1.97     ozaki #define BRIDGE_RT_LOCKED(_sc)	(!(_sc)->sc_rtlist_lock || \
    189   1.97     ozaki 				 mutex_owned((_sc)->sc_rtlist_lock))
    190   1.97     ozaki 
    191   1.97     ozaki #define BRIDGE_RT_PSZ_PERFORM(_sc) \
    192   1.97     ozaki 				if ((_sc)->sc_rtlist_psz != NULL) \
    193   1.97     ozaki 					pserialize_perform((_sc)->sc_rtlist_psz);
    194   1.97     ozaki 
    195  1.108     ozaki #define BRIDGE_RT_RENTER(__s)	do { __s = pserialize_read_enter(); } while (0)
    196  1.108     ozaki #define BRIDGE_RT_REXIT(__s)	do { pserialize_read_exit(__s); } while (0)
    197  1.114     ozaki 
    198  1.114     ozaki 
    199  1.114     ozaki #ifdef NET_MPSAFE
    200  1.114     ozaki #define DECLARE_LOCK_VARIABLE
    201  1.114     ozaki #define ACQUIRE_GLOBAL_LOCKS()	do { } while (0)
    202  1.114     ozaki #define RELEASE_GLOBAL_LOCKS()	do { } while (0)
    203  1.114     ozaki #else
    204  1.114     ozaki #define DECLARE_LOCK_VARIABLE	int __s
    205  1.114     ozaki #define ACQUIRE_GLOBAL_LOCKS()	do {					\
    206  1.114     ozaki 					KERNEL_LOCK(1, NULL);		\
    207  1.114     ozaki 					mutex_enter(softnet_lock);	\
    208  1.132     ozaki 					__s = splsoftnet();		\
    209  1.114     ozaki 				} while (0)
    210  1.114     ozaki #define RELEASE_GLOBAL_LOCKS()	do {					\
    211  1.114     ozaki 					splx(__s);			\
    212  1.114     ozaki 					mutex_exit(softnet_lock);	\
    213  1.114     ozaki 					KERNEL_UNLOCK_ONE(NULL);	\
    214  1.114     ozaki 				} while (0)
    215  1.114     ozaki #endif
    216   1.97     ozaki 
    217  1.115     ozaki struct psref_class *bridge_psref_class __read_mostly;
    218  1.115     ozaki 
    219    1.1   thorpej int	bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
    220    1.1   thorpej 
    221   1.33   thorpej static struct pool bridge_rtnode_pool;
    222    1.1   thorpej 
    223   1.33   thorpej static int	bridge_clone_create(struct if_clone *, int);
    224   1.33   thorpej static int	bridge_clone_destroy(struct ifnet *);
    225    1.1   thorpej 
    226   1.50  christos static int	bridge_ioctl(struct ifnet *, u_long, void *);
    227   1.33   thorpej static int	bridge_init(struct ifnet *);
    228   1.33   thorpej static void	bridge_stop(struct ifnet *, int);
    229   1.33   thorpej static void	bridge_start(struct ifnet *);
    230   1.33   thorpej 
    231   1.81     ozaki static void	bridge_input(struct ifnet *, struct mbuf *);
    232  1.108     ozaki static void	bridge_forward(struct bridge_softc *, struct mbuf *);
    233   1.33   thorpej 
    234   1.33   thorpej static void	bridge_timer(void *);
    235   1.33   thorpej 
    236   1.33   thorpej static void	bridge_broadcast(struct bridge_softc *, struct ifnet *,
    237   1.33   thorpej 				 struct mbuf *);
    238   1.33   thorpej 
    239   1.33   thorpej static int	bridge_rtupdate(struct bridge_softc *, const uint8_t *,
    240   1.33   thorpej 				struct ifnet *, int, uint8_t);
    241   1.33   thorpej static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
    242   1.33   thorpej static void	bridge_rttrim(struct bridge_softc *);
    243   1.33   thorpej static void	bridge_rtage(struct bridge_softc *);
    244   1.97     ozaki static void	bridge_rtage_work(struct work *, void *);
    245   1.33   thorpej static void	bridge_rtflush(struct bridge_softc *, int);
    246   1.33   thorpej static int	bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
    247   1.33   thorpej static void	bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp);
    248   1.33   thorpej 
    249   1.93     ozaki static void	bridge_rtable_init(struct bridge_softc *);
    250   1.33   thorpej static void	bridge_rtable_fini(struct bridge_softc *);
    251   1.33   thorpej 
    252   1.33   thorpej static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
    253   1.33   thorpej 						  const uint8_t *);
    254   1.33   thorpej static int	bridge_rtnode_insert(struct bridge_softc *,
    255   1.33   thorpej 				     struct bridge_rtnode *);
    256   1.97     ozaki static void	bridge_rtnode_remove(struct bridge_softc *,
    257   1.97     ozaki 				     struct bridge_rtnode *);
    258   1.97     ozaki static void	bridge_rtnode_destroy(struct bridge_rtnode *);
    259   1.33   thorpej 
    260   1.33   thorpej static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
    261  1.115     ozaki 						  const char *name,
    262  1.115     ozaki 						  struct psref *);
    263   1.33   thorpej static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
    264  1.115     ozaki 						     struct ifnet *ifp,
    265  1.115     ozaki 						     struct psref *);
    266  1.115     ozaki static void	bridge_release_member(struct bridge_softc *, struct bridge_iflist *,
    267  1.115     ozaki                                       struct psref *);
    268   1.33   thorpej static void	bridge_delete_member(struct bridge_softc *,
    269   1.33   thorpej 				     struct bridge_iflist *);
    270  1.115     ozaki static void	bridge_acquire_member(struct bridge_softc *sc,
    271  1.115     ozaki                                       struct bridge_iflist *,
    272  1.115     ozaki                                       struct psref *);
    273   1.33   thorpej 
    274   1.33   thorpej static int	bridge_ioctl_add(struct bridge_softc *, void *);
    275   1.33   thorpej static int	bridge_ioctl_del(struct bridge_softc *, void *);
    276   1.33   thorpej static int	bridge_ioctl_gifflags(struct bridge_softc *, void *);
    277   1.33   thorpej static int	bridge_ioctl_sifflags(struct bridge_softc *, void *);
    278   1.33   thorpej static int	bridge_ioctl_scache(struct bridge_softc *, void *);
    279   1.33   thorpej static int	bridge_ioctl_gcache(struct bridge_softc *, void *);
    280   1.33   thorpej static int	bridge_ioctl_gifs(struct bridge_softc *, void *);
    281   1.33   thorpej static int	bridge_ioctl_rts(struct bridge_softc *, void *);
    282   1.33   thorpej static int	bridge_ioctl_saddr(struct bridge_softc *, void *);
    283   1.33   thorpej static int	bridge_ioctl_sto(struct bridge_softc *, void *);
    284   1.33   thorpej static int	bridge_ioctl_gto(struct bridge_softc *, void *);
    285   1.33   thorpej static int	bridge_ioctl_daddr(struct bridge_softc *, void *);
    286   1.33   thorpej static int	bridge_ioctl_flush(struct bridge_softc *, void *);
    287   1.33   thorpej static int	bridge_ioctl_gpri(struct bridge_softc *, void *);
    288   1.33   thorpej static int	bridge_ioctl_spri(struct bridge_softc *, void *);
    289   1.33   thorpej static int	bridge_ioctl_ght(struct bridge_softc *, void *);
    290   1.33   thorpej static int	bridge_ioctl_sht(struct bridge_softc *, void *);
    291   1.33   thorpej static int	bridge_ioctl_gfd(struct bridge_softc *, void *);
    292   1.33   thorpej static int	bridge_ioctl_sfd(struct bridge_softc *, void *);
    293   1.33   thorpej static int	bridge_ioctl_gma(struct bridge_softc *, void *);
    294   1.33   thorpej static int	bridge_ioctl_sma(struct bridge_softc *, void *);
    295   1.33   thorpej static int	bridge_ioctl_sifprio(struct bridge_softc *, void *);
    296   1.33   thorpej static int	bridge_ioctl_sifcost(struct bridge_softc *, void *);
    297   1.77     rmind #if defined(BRIDGE_IPF)
    298   1.33   thorpej static int	bridge_ioctl_gfilt(struct bridge_softc *, void *);
    299   1.33   thorpej static int	bridge_ioctl_sfilt(struct bridge_softc *, void *);
    300   1.33   thorpej static int	bridge_ipf(void *, struct mbuf **, struct ifnet *, int);
    301   1.33   thorpej static int	bridge_ip_checkbasic(struct mbuf **mp);
    302    1.9  perseant # ifdef INET6
    303   1.33   thorpej static int	bridge_ip6_checkbasic(struct mbuf **mp);
    304    1.9  perseant # endif /* INET6 */
    305   1.77     rmind #endif /* BRIDGE_IPF */
    306    1.1   thorpej 
    307    1.1   thorpej struct bridge_control {
    308    1.1   thorpej 	int	(*bc_func)(struct bridge_softc *, void *);
    309    1.1   thorpej 	int	bc_argsize;
    310    1.1   thorpej 	int	bc_flags;
    311    1.1   thorpej };
    312    1.1   thorpej 
    313    1.1   thorpej #define	BC_F_COPYIN		0x01	/* copy arguments in */
    314    1.1   thorpej #define	BC_F_COPYOUT		0x02	/* copy arguments out */
    315    1.1   thorpej #define	BC_F_SUSER		0x04	/* do super-user check */
    316   1.99      matt #define BC_F_XLATEIN		0x08	/* xlate arguments in */
    317   1.99      matt #define BC_F_XLATEOUT		0x10	/* xlate arguments out */
    318    1.1   thorpej 
    319   1.33   thorpej static const struct bridge_control bridge_control_table[] = {
    320   1.55    dyoung [BRDGADD] = {bridge_ioctl_add, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    321   1.55    dyoung [BRDGDEL] = {bridge_ioctl_del, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    322    1.1   thorpej 
    323   1.55    dyoung [BRDGGIFFLGS] = {bridge_ioctl_gifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_COPYOUT},
    324   1.55    dyoung [BRDGSIFFLGS] = {bridge_ioctl_sifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    325   1.11    bouyer 
    326   1.55    dyoung [BRDGSCACHE] = {bridge_ioctl_scache, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    327   1.55    dyoung [BRDGGCACHE] = {bridge_ioctl_gcache, sizeof(struct ifbrparam), BC_F_COPYOUT},
    328   1.55    dyoung 
    329   1.99      matt [OBRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_COPYIN|BC_F_COPYOUT},
    330   1.99      matt [OBRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_COPYIN|BC_F_COPYOUT},
    331   1.55    dyoung 
    332   1.55    dyoung [BRDGSADDR] = {bridge_ioctl_saddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER},
    333   1.55    dyoung 
    334   1.55    dyoung [BRDGSTO] = {bridge_ioctl_sto, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    335   1.55    dyoung [BRDGGTO] = {bridge_ioctl_gto, sizeof(struct ifbrparam), BC_F_COPYOUT},
    336   1.55    dyoung 
    337   1.55    dyoung [BRDGDADDR] = {bridge_ioctl_daddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER},
    338   1.55    dyoung 
    339   1.55    dyoung [BRDGFLUSH] = {bridge_ioctl_flush, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    340   1.55    dyoung 
    341   1.55    dyoung [BRDGGPRI] = {bridge_ioctl_gpri, sizeof(struct ifbrparam), BC_F_COPYOUT},
    342   1.55    dyoung [BRDGSPRI] = {bridge_ioctl_spri, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    343   1.55    dyoung 
    344   1.55    dyoung [BRDGGHT] = {bridge_ioctl_ght, sizeof(struct ifbrparam), BC_F_COPYOUT},
    345   1.55    dyoung [BRDGSHT] = {bridge_ioctl_sht, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    346   1.55    dyoung 
    347   1.55    dyoung [BRDGGFD] = {bridge_ioctl_gfd, sizeof(struct ifbrparam), BC_F_COPYOUT},
    348   1.55    dyoung [BRDGSFD] = {bridge_ioctl_sfd, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    349   1.55    dyoung 
    350   1.55    dyoung [BRDGGMA] = {bridge_ioctl_gma, sizeof(struct ifbrparam), BC_F_COPYOUT},
    351   1.55    dyoung [BRDGSMA] = {bridge_ioctl_sma, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    352   1.55    dyoung 
    353   1.55    dyoung [BRDGSIFPRIO] = {bridge_ioctl_sifprio, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    354   1.55    dyoung 
    355   1.55    dyoung [BRDGSIFCOST] = {bridge_ioctl_sifcost, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    356   1.77     rmind #if defined(BRIDGE_IPF)
    357   1.55    dyoung [BRDGGFILT] = {bridge_ioctl_gfilt, sizeof(struct ifbrparam), BC_F_COPYOUT},
    358   1.55    dyoung [BRDGSFILT] = {bridge_ioctl_sfilt, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    359   1.77     rmind #endif /* BRIDGE_IPF */
    360   1.99      matt [BRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_XLATEIN|BC_F_XLATEOUT},
    361   1.99      matt [BRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_XLATEIN|BC_F_XLATEOUT},
    362    1.1   thorpej };
    363   1.99      matt 
    364   1.49    dyoung static const int bridge_control_table_size = __arraycount(bridge_control_table);
    365    1.1   thorpej 
    366   1.33   thorpej static struct if_clone bridge_cloner =
    367    1.1   thorpej     IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy);
    368    1.1   thorpej 
    369    1.1   thorpej /*
    370    1.1   thorpej  * bridgeattach:
    371    1.1   thorpej  *
    372    1.1   thorpej  *	Pseudo-device attach routine.
    373    1.1   thorpej  */
    374    1.1   thorpej void
    375   1.45  christos bridgeattach(int n)
    376    1.1   thorpej {
    377    1.1   thorpej 
    378    1.1   thorpej 	pool_init(&bridge_rtnode_pool, sizeof(struct bridge_rtnode),
    379   1.51        ad 	    0, 0, 0, "brtpl", NULL, IPL_NET);
    380    1.1   thorpej 
    381  1.115     ozaki 	bridge_psref_class = psref_class_create("bridge", IPL_SOFTNET);
    382  1.115     ozaki 
    383    1.1   thorpej 	if_clone_attach(&bridge_cloner);
    384    1.1   thorpej }
    385    1.1   thorpej 
    386    1.1   thorpej /*
    387    1.1   thorpej  * bridge_clone_create:
    388    1.1   thorpej  *
    389    1.1   thorpej  *	Create a new bridge instance.
    390    1.1   thorpej  */
    391   1.33   thorpej static int
    392    1.1   thorpej bridge_clone_create(struct if_clone *ifc, int unit)
    393    1.1   thorpej {
    394    1.1   thorpej 	struct bridge_softc *sc;
    395    1.1   thorpej 	struct ifnet *ifp;
    396  1.114     ozaki 	int error;
    397    1.1   thorpej 
    398   1.93     ozaki 	sc = kmem_zalloc(sizeof(*sc),  KM_SLEEP);
    399    1.1   thorpej 	ifp = &sc->sc_if;
    400    1.1   thorpej 
    401    1.1   thorpej 	sc->sc_brtmax = BRIDGE_RTABLE_MAX;
    402    1.1   thorpej 	sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
    403   1.29     perry 	sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
    404    1.1   thorpej 	sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
    405    1.1   thorpej 	sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
    406    1.1   thorpej 	sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
    407    1.1   thorpej 	sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
    408    1.9  perseant 	sc->sc_filter_flags = 0;
    409    1.1   thorpej 
    410    1.1   thorpej 	/* Initialize our routing table. */
    411    1.1   thorpej 	bridge_rtable_init(sc);
    412    1.1   thorpej 
    413   1.97     ozaki 	error = workqueue_create(&sc->sc_rtage_wq, "bridge_rtage",
    414  1.114     ozaki 	    bridge_rtage_work, sc, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
    415   1.97     ozaki 	if (error)
    416   1.97     ozaki 		panic("%s: workqueue_create %d\n", __func__, error);
    417   1.97     ozaki 
    418  1.139     ozaki 	callout_init(&sc->sc_brcallout, CALLOUT_MPSAFE);
    419  1.139     ozaki 	callout_init(&sc->sc_bstpcallout, CALLOUT_MPSAFE);
    420    1.1   thorpej 
    421  1.115     ozaki 	mutex_init(&sc->sc_iflist_psref.bip_lock, MUTEX_DEFAULT, IPL_NONE);
    422  1.115     ozaki 	PSLIST_INIT(&sc->sc_iflist_psref.bip_iflist);
    423  1.115     ozaki 	sc->sc_iflist_psref.bip_psz = pserialize_create();
    424    1.1   thorpej 
    425   1.62  christos 	if_initname(ifp, ifc->ifc_name, unit);
    426    1.1   thorpej 	ifp->if_softc = sc;
    427  1.141     ozaki 	ifp->if_extflags = IFEF_MPSAFE | IFEF_NO_LINK_STATE_CHANGE;
    428    1.1   thorpej 	ifp->if_mtu = ETHERMTU;
    429    1.1   thorpej 	ifp->if_ioctl = bridge_ioctl;
    430    1.1   thorpej 	ifp->if_output = bridge_output;
    431    1.1   thorpej 	ifp->if_start = bridge_start;
    432    1.1   thorpej 	ifp->if_stop = bridge_stop;
    433    1.1   thorpej 	ifp->if_init = bridge_init;
    434    1.6    itojun 	ifp->if_type = IFT_BRIDGE;
    435    1.1   thorpej 	ifp->if_addrlen = 0;
    436    1.1   thorpej 	ifp->if_dlt = DLT_EN10MB;
    437    1.1   thorpej 	ifp->if_hdrlen = ETHER_HDR_LEN;
    438    1.1   thorpej 
    439  1.136   msaitoh 	error = if_initialize(ifp);
    440  1.136   msaitoh 	if (error != 0) {
    441  1.136   msaitoh 		pserialize_destroy(sc->sc_iflist_psref.bip_psz);
    442  1.136   msaitoh 		mutex_destroy(&sc->sc_iflist_psref.bip_lock);
    443  1.136   msaitoh 		callout_destroy(&sc->sc_brcallout);
    444  1.136   msaitoh 		callout_destroy(&sc->sc_bstpcallout);
    445  1.136   msaitoh 		workqueue_destroy(sc->sc_rtage_wq);
    446  1.136   msaitoh 		bridge_rtable_fini(sc);
    447  1.136   msaitoh 		kmem_free(sc, sizeof(*sc));
    448  1.136   msaitoh 
    449  1.136   msaitoh 		return error;
    450  1.136   msaitoh 	}
    451  1.106     ozaki 	if_register(ifp);
    452    1.1   thorpej 
    453    1.1   thorpej 	if_alloc_sadl(ifp);
    454    1.1   thorpej 
    455  1.136   msaitoh 	return 0;
    456    1.1   thorpej }
    457    1.1   thorpej 
    458    1.1   thorpej /*
    459    1.1   thorpej  * bridge_clone_destroy:
    460    1.1   thorpej  *
    461    1.1   thorpej  *	Destroy a bridge instance.
    462    1.1   thorpej  */
    463   1.33   thorpej static int
    464    1.1   thorpej bridge_clone_destroy(struct ifnet *ifp)
    465    1.1   thorpej {
    466    1.1   thorpej 	struct bridge_softc *sc = ifp->if_softc;
    467    1.1   thorpej 	struct bridge_iflist *bif;
    468    1.1   thorpej 
    469    1.1   thorpej 	bridge_stop(ifp, 1);
    470    1.1   thorpej 
    471   1.98     ozaki 	BRIDGE_LOCK(sc);
    472  1.112     ozaki 	for (;;) {
    473  1.115     ozaki 		bif = PSLIST_WRITER_FIRST(&sc->sc_iflist_psref.bip_iflist, struct bridge_iflist,
    474  1.112     ozaki 		    bif_next);
    475  1.112     ozaki 		if (bif == NULL)
    476  1.112     ozaki 			break;
    477    1.1   thorpej 		bridge_delete_member(sc, bif);
    478  1.112     ozaki 	}
    479  1.115     ozaki 	PSLIST_DESTROY(&sc->sc_iflist_psref.bip_iflist);
    480   1.98     ozaki 	BRIDGE_UNLOCK(sc);
    481    1.1   thorpej 
    482    1.1   thorpej 	if_detach(ifp);
    483    1.1   thorpej 
    484    1.1   thorpej 	/* Tear down the routing table. */
    485    1.1   thorpej 	bridge_rtable_fini(sc);
    486    1.1   thorpej 
    487  1.115     ozaki 	pserialize_destroy(sc->sc_iflist_psref.bip_psz);
    488  1.115     ozaki 	mutex_destroy(&sc->sc_iflist_psref.bip_lock);
    489  1.136   msaitoh 	callout_destroy(&sc->sc_brcallout);
    490  1.136   msaitoh 	callout_destroy(&sc->sc_bstpcallout);
    491   1.97     ozaki 	workqueue_destroy(sc->sc_rtage_wq);
    492   1.93     ozaki 	kmem_free(sc, sizeof(*sc));
    493   1.27     peter 
    494  1.136   msaitoh 	return 0;
    495    1.1   thorpej }
    496    1.1   thorpej 
    497    1.1   thorpej /*
    498    1.1   thorpej  * bridge_ioctl:
    499    1.1   thorpej  *
    500    1.1   thorpej  *	Handle a control request from the operator.
    501    1.1   thorpej  */
    502   1.33   thorpej static int
    503   1.50  christos bridge_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    504    1.1   thorpej {
    505    1.1   thorpej 	struct bridge_softc *sc = ifp->if_softc;
    506   1.40        ad 	struct lwp *l = curlwp;	/* XXX */
    507    1.1   thorpej 	union {
    508    1.1   thorpej 		struct ifbreq ifbreq;
    509    1.1   thorpej 		struct ifbifconf ifbifconf;
    510    1.1   thorpej 		struct ifbareq ifbareq;
    511    1.1   thorpej 		struct ifbaconf ifbaconf;
    512    1.1   thorpej 		struct ifbrparam ifbrparam;
    513    1.1   thorpej 	} args;
    514    1.1   thorpej 	struct ifdrv *ifd = (struct ifdrv *) data;
    515   1.69      elad 	const struct bridge_control *bc = NULL; /* XXXGCC */
    516    1.1   thorpej 	int s, error = 0;
    517    1.1   thorpej 
    518  1.132     ozaki 	/* Authorize command before calling splsoftnet(). */
    519    1.1   thorpej 	switch (cmd) {
    520    1.1   thorpej 	case SIOCGDRVSPEC:
    521    1.1   thorpej 	case SIOCSDRVSPEC:
    522   1.99      matt 		if (ifd->ifd_cmd >= bridge_control_table_size
    523   1.99      matt 		    || (bc = &bridge_control_table[ifd->ifd_cmd]) == NULL) {
    524    1.1   thorpej 			error = EINVAL;
    525   1.70    cegger 			return error;
    526    1.1   thorpej 		}
    527   1.69      elad 
    528   1.69      elad 		/* We only care about BC_F_SUSER at this point. */
    529   1.69      elad 		if ((bc->bc_flags & BC_F_SUSER) == 0)
    530   1.69      elad 			break;
    531   1.69      elad 
    532   1.75      elad 		error = kauth_authorize_network(l->l_cred,
    533   1.75      elad 		    KAUTH_NETWORK_INTERFACE_BRIDGE,
    534   1.75      elad 		    cmd == SIOCGDRVSPEC ?
    535   1.75      elad 		     KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_GETPRIV :
    536   1.76       wiz 		     KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_SETPRIV,
    537   1.75      elad 		     ifd, NULL, NULL);
    538   1.69      elad 		if (error)
    539  1.136   msaitoh 			return error;
    540   1.69      elad 
    541   1.69      elad 		break;
    542   1.69      elad 	}
    543   1.69      elad 
    544  1.132     ozaki 	s = splsoftnet();
    545   1.69      elad 
    546   1.69      elad 	switch (cmd) {
    547   1.69      elad 	case SIOCGDRVSPEC:
    548   1.69      elad 	case SIOCSDRVSPEC:
    549   1.70    cegger 		KASSERT(bc != NULL);
    550    1.1   thorpej 		if (cmd == SIOCGDRVSPEC &&
    551   1.99      matt 		    (bc->bc_flags & (BC_F_COPYOUT|BC_F_XLATEOUT)) == 0) {
    552   1.14  kristerw 			error = EINVAL;
    553   1.14  kristerw 			break;
    554   1.14  kristerw 		}
    555    1.1   thorpej 		else if (cmd == SIOCSDRVSPEC &&
    556   1.99      matt 		    (bc->bc_flags & (BC_F_COPYOUT|BC_F_XLATEOUT)) != 0) {
    557   1.14  kristerw 			error = EINVAL;
    558   1.14  kristerw 			break;
    559   1.14  kristerw 		}
    560    1.1   thorpej 
    561  1.132     ozaki 		/* BC_F_SUSER is checked above, before splsoftnet(). */
    562    1.1   thorpej 
    563   1.99      matt 		if ((bc->bc_flags & (BC_F_XLATEIN|BC_F_XLATEOUT)) == 0
    564   1.99      matt 		    && (ifd->ifd_len != bc->bc_argsize
    565   1.99      matt 			|| ifd->ifd_len > sizeof(args))) {
    566    1.1   thorpej 			error = EINVAL;
    567    1.1   thorpej 			break;
    568    1.1   thorpej 		}
    569    1.1   thorpej 
    570   1.35  christos 		memset(&args, 0, sizeof(args));
    571    1.1   thorpej 		if (bc->bc_flags & BC_F_COPYIN) {
    572    1.1   thorpej 			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
    573    1.1   thorpej 			if (error)
    574    1.1   thorpej 				break;
    575   1.99      matt 		} else if (bc->bc_flags & BC_F_XLATEIN) {
    576   1.99      matt 			args.ifbifconf.ifbic_len = ifd->ifd_len;
    577   1.99      matt 			args.ifbifconf.ifbic_buf = ifd->ifd_data;
    578    1.1   thorpej 		}
    579    1.1   thorpej 
    580    1.1   thorpej 		error = (*bc->bc_func)(sc, &args);
    581    1.1   thorpej 		if (error)
    582    1.1   thorpej 			break;
    583    1.1   thorpej 
    584   1.99      matt 		if (bc->bc_flags & BC_F_COPYOUT) {
    585    1.1   thorpej 			error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
    586   1.99      matt 		} else if (bc->bc_flags & BC_F_XLATEOUT) {
    587   1.99      matt 			ifd->ifd_len = args.ifbifconf.ifbic_len;
    588   1.99      matt 			ifd->ifd_data = args.ifbifconf.ifbic_buf;
    589   1.99      matt 		}
    590    1.1   thorpej 		break;
    591    1.1   thorpej 
    592    1.1   thorpej 	case SIOCSIFFLAGS:
    593   1.63    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    594   1.63    dyoung 			break;
    595   1.63    dyoung 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    596   1.63    dyoung 		case IFF_RUNNING:
    597    1.1   thorpej 			/*
    598    1.1   thorpej 			 * If interface is marked down and it is running,
    599    1.1   thorpej 			 * then stop and disable it.
    600    1.1   thorpej 			 */
    601    1.1   thorpej 			(*ifp->if_stop)(ifp, 1);
    602   1.63    dyoung 			break;
    603   1.63    dyoung 		case IFF_UP:
    604    1.1   thorpej 			/*
    605    1.1   thorpej 			 * If interface is marked up and it is stopped, then
    606    1.1   thorpej 			 * start it.
    607    1.1   thorpej 			 */
    608    1.1   thorpej 			error = (*ifp->if_init)(ifp);
    609   1.63    dyoung 			break;
    610   1.63    dyoung 		default:
    611   1.63    dyoung 			break;
    612    1.1   thorpej 		}
    613    1.1   thorpej 		break;
    614    1.1   thorpej 
    615   1.88     ozaki 	case SIOCSIFMTU:
    616   1.88     ozaki 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    617   1.88     ozaki 			error = 0;
    618   1.88     ozaki 		break;
    619   1.88     ozaki 
    620    1.1   thorpej 	default:
    621   1.63    dyoung 		error = ifioctl_common(ifp, cmd, data);
    622    1.1   thorpej 		break;
    623    1.1   thorpej 	}
    624    1.1   thorpej 
    625    1.1   thorpej 	splx(s);
    626    1.1   thorpej 
    627  1.136   msaitoh 	return error;
    628    1.1   thorpej }
    629    1.1   thorpej 
    630    1.1   thorpej /*
    631    1.1   thorpej  * bridge_lookup_member:
    632    1.1   thorpej  *
    633   1.87     ozaki  *	Lookup a bridge member interface.
    634    1.1   thorpej  */
    635   1.33   thorpej static struct bridge_iflist *
    636  1.115     ozaki bridge_lookup_member(struct bridge_softc *sc, const char *name, struct psref *psref)
    637    1.1   thorpej {
    638    1.1   thorpej 	struct bridge_iflist *bif;
    639    1.1   thorpej 	struct ifnet *ifp;
    640   1.95     ozaki 	int s;
    641    1.1   thorpej 
    642   1.95     ozaki 	BRIDGE_PSZ_RENTER(s);
    643   1.87     ozaki 
    644  1.115     ozaki 	BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
    645    1.1   thorpej 		ifp = bif->bif_ifp;
    646    1.1   thorpej 		if (strcmp(ifp->if_xname, name) == 0)
    647   1.87     ozaki 			break;
    648    1.1   thorpej 	}
    649  1.115     ozaki 	if (bif != NULL)
    650  1.115     ozaki 		bridge_acquire_member(sc, bif, psref);
    651    1.1   thorpej 
    652   1.95     ozaki 	BRIDGE_PSZ_REXIT(s);
    653   1.87     ozaki 
    654   1.87     ozaki 	return bif;
    655    1.1   thorpej }
    656    1.1   thorpej 
    657    1.1   thorpej /*
    658    1.8    martin  * bridge_lookup_member_if:
    659    1.8    martin  *
    660   1.87     ozaki  *	Lookup a bridge member interface by ifnet*.
    661    1.8    martin  */
    662   1.33   thorpej static struct bridge_iflist *
    663  1.115     ozaki bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp,
    664  1.115     ozaki     struct psref *psref)
    665    1.8    martin {
    666    1.8    martin 	struct bridge_iflist *bif;
    667   1.95     ozaki 	int s;
    668    1.8    martin 
    669   1.95     ozaki 	BRIDGE_PSZ_RENTER(s);
    670   1.87     ozaki 
    671   1.87     ozaki 	bif = member_ifp->if_bridgeif;
    672  1.115     ozaki 	if (bif != NULL) {
    673  1.115     ozaki 		psref_acquire(psref, &bif->bif_psref,
    674  1.115     ozaki 		    bridge_psref_class);
    675  1.115     ozaki 	}
    676   1.95     ozaki 
    677   1.95     ozaki 	BRIDGE_PSZ_REXIT(s);
    678   1.95     ozaki 
    679   1.95     ozaki 	return bif;
    680   1.95     ozaki }
    681   1.87     ozaki 
    682  1.115     ozaki static void
    683  1.115     ozaki bridge_acquire_member(struct bridge_softc *sc, struct bridge_iflist *bif,
    684  1.115     ozaki     struct psref *psref)
    685   1.95     ozaki {
    686  1.114     ozaki 
    687  1.115     ozaki 	psref_acquire(psref, &bif->bif_psref, bridge_psref_class);
    688   1.87     ozaki }
    689   1.87     ozaki 
    690   1.87     ozaki /*
    691   1.87     ozaki  * bridge_release_member:
    692   1.87     ozaki  *
    693   1.87     ozaki  *	Release the specified member interface.
    694   1.87     ozaki  */
    695   1.87     ozaki static void
    696  1.115     ozaki bridge_release_member(struct bridge_softc *sc, struct bridge_iflist *bif,
    697  1.115     ozaki     struct psref *psref)
    698   1.87     ozaki {
    699   1.95     ozaki 
    700  1.115     ozaki 	psref_release(psref, &bif->bif_psref, bridge_psref_class);
    701    1.8    martin }
    702    1.8    martin 
    703    1.8    martin /*
    704    1.1   thorpej  * bridge_delete_member:
    705    1.1   thorpej  *
    706    1.1   thorpej  *	Delete the specified member interface.
    707    1.1   thorpej  */
    708   1.33   thorpej static void
    709    1.1   thorpej bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
    710    1.1   thorpej {
    711    1.1   thorpej 	struct ifnet *ifs = bif->bif_ifp;
    712    1.1   thorpej 
    713   1.98     ozaki 	KASSERT(BRIDGE_LOCKED(sc));
    714    1.1   thorpej 
    715  1.106     ozaki 	ifs->_if_input = ether_input;
    716    1.1   thorpej 	ifs->if_bridge = NULL;
    717   1.87     ozaki 	ifs->if_bridgeif = NULL;
    718   1.87     ozaki 
    719  1.112     ozaki 	PSLIST_WRITER_REMOVE(bif, bif_next);
    720   1.95     ozaki 	BRIDGE_PSZ_PERFORM(sc);
    721  1.115     ozaki 	BRIDGE_UNLOCK(sc);
    722   1.95     ozaki 
    723  1.115     ozaki 	psref_target_destroy(&bif->bif_psref, bridge_psref_class);
    724    1.1   thorpej 
    725  1.113     ozaki 	PSLIST_ENTRY_DESTROY(bif, bif_next);
    726   1.93     ozaki 	kmem_free(bif, sizeof(*bif));
    727   1.98     ozaki 
    728   1.98     ozaki 	BRIDGE_LOCK(sc);
    729    1.1   thorpej }
    730    1.1   thorpej 
    731   1.33   thorpej static int
    732    1.1   thorpej bridge_ioctl_add(struct bridge_softc *sc, void *arg)
    733    1.1   thorpej {
    734    1.1   thorpej 	struct ifbreq *req = arg;
    735    1.1   thorpej 	struct bridge_iflist *bif = NULL;
    736    1.1   thorpej 	struct ifnet *ifs;
    737    1.1   thorpej 	int error = 0;
    738  1.123     ozaki 	struct psref psref;
    739    1.1   thorpej 
    740  1.123     ozaki 	ifs = if_get(req->ifbr_ifsname, &psref);
    741    1.1   thorpej 	if (ifs == NULL)
    742  1.136   msaitoh 		return ENOENT;
    743    1.7    itojun 
    744  1.123     ozaki 	if (ifs->if_bridge == sc) {
    745  1.123     ozaki 		error = EEXIST;
    746  1.123     ozaki 		goto out;
    747  1.123     ozaki 	}
    748    1.1   thorpej 
    749  1.123     ozaki 	if (ifs->if_bridge != NULL) {
    750  1.123     ozaki 		error = EBUSY;
    751  1.123     ozaki 		goto out;
    752  1.123     ozaki 	}
    753    1.1   thorpej 
    754  1.123     ozaki 	if (ifs->_if_input != ether_input) {
    755  1.123     ozaki 		error = EINVAL;
    756  1.123     ozaki 		goto out;
    757  1.123     ozaki 	}
    758   1.81     ozaki 
    759   1.91     ozaki 	/* FIXME: doesn't work with non-IFF_SIMPLEX interfaces */
    760  1.123     ozaki 	if ((ifs->if_flags & IFF_SIMPLEX) == 0) {
    761  1.123     ozaki 		error = EINVAL;
    762  1.123     ozaki 		goto out;
    763  1.123     ozaki 	}
    764   1.91     ozaki 
    765   1.93     ozaki 	bif = kmem_alloc(sizeof(*bif), KM_SLEEP);
    766    1.1   thorpej 
    767    1.1   thorpej 	switch (ifs->if_type) {
    768    1.1   thorpej 	case IFT_ETHER:
    769  1.133  knakahar 		if (sc->sc_if.if_mtu != ifs->if_mtu) {
    770  1.133  knakahar 			error = EINVAL;
    771  1.133  knakahar 			goto out;
    772  1.133  knakahar 		}
    773  1.133  knakahar 		/* FALLTHROUGH */
    774  1.133  knakahar 	case IFT_L2TP:
    775  1.105  christos 		if ((error = ether_enable_vlan_mtu(ifs)) > 0)
    776  1.105  christos 			goto out;
    777    1.1   thorpej 		/*
    778    1.1   thorpej 		 * Place the interface into promiscuous mode.
    779    1.1   thorpej 		 */
    780    1.1   thorpej 		error = ifpromisc(ifs, 1);
    781    1.1   thorpej 		if (error)
    782    1.1   thorpej 			goto out;
    783    1.1   thorpej 		break;
    784    1.1   thorpej 	default:
    785    1.5  jdolecek 		error = EINVAL;
    786    1.5  jdolecek 		goto out;
    787    1.1   thorpej 	}
    788    1.1   thorpej 
    789    1.1   thorpej 	bif->bif_ifp = ifs;
    790    1.1   thorpej 	bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
    791    1.1   thorpej 	bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
    792    1.1   thorpej 	bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
    793  1.112     ozaki 	PSLIST_ENTRY_INIT(bif, bif_next);
    794  1.115     ozaki 	psref_target_init(&bif->bif_psref, bridge_psref_class);
    795   1.87     ozaki 
    796   1.87     ozaki 	BRIDGE_LOCK(sc);
    797    1.1   thorpej 
    798    1.1   thorpej 	ifs->if_bridge = sc;
    799   1.87     ozaki 	ifs->if_bridgeif = bif;
    800  1.115     ozaki 	PSLIST_WRITER_INSERT_HEAD(&sc->sc_iflist_psref.bip_iflist, bif, bif_next);
    801  1.106     ozaki 	ifs->_if_input = bridge_input;
    802    1.1   thorpej 
    803   1.87     ozaki 	BRIDGE_UNLOCK(sc);
    804   1.87     ozaki 
    805    1.1   thorpej 	if (sc->sc_if.if_flags & IFF_RUNNING)
    806    1.1   thorpej 		bstp_initialization(sc);
    807    1.1   thorpej 	else
    808    1.1   thorpej 		bstp_stop(sc);
    809    1.1   thorpej 
    810    1.1   thorpej  out:
    811  1.123     ozaki 	if_put(ifs, &psref);
    812    1.1   thorpej 	if (error) {
    813    1.1   thorpej 		if (bif != NULL)
    814   1.93     ozaki 			kmem_free(bif, sizeof(*bif));
    815    1.1   thorpej 	}
    816  1.136   msaitoh 	return error;
    817    1.1   thorpej }
    818    1.1   thorpej 
    819   1.33   thorpej static int
    820    1.1   thorpej bridge_ioctl_del(struct bridge_softc *sc, void *arg)
    821    1.1   thorpej {
    822    1.1   thorpej 	struct ifbreq *req = arg;
    823   1.87     ozaki 	const char *name = req->ifbr_ifsname;
    824    1.1   thorpej 	struct bridge_iflist *bif;
    825   1.87     ozaki 	struct ifnet *ifs;
    826    1.1   thorpej 
    827   1.87     ozaki 	BRIDGE_LOCK(sc);
    828   1.87     ozaki 
    829   1.87     ozaki 	/*
    830   1.87     ozaki 	 * Don't use bridge_lookup_member. We want to get a member
    831   1.87     ozaki 	 * with bif_refs == 0.
    832   1.87     ozaki 	 */
    833  1.115     ozaki 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) {
    834   1.87     ozaki 		ifs = bif->bif_ifp;
    835   1.87     ozaki 		if (strcmp(ifs->if_xname, name) == 0)
    836   1.87     ozaki 			break;
    837   1.87     ozaki 	}
    838   1.87     ozaki 
    839   1.87     ozaki 	if (bif == NULL) {
    840   1.87     ozaki 		BRIDGE_UNLOCK(sc);
    841   1.87     ozaki 		return ENOENT;
    842   1.87     ozaki 	}
    843    1.1   thorpej 
    844    1.1   thorpej 	bridge_delete_member(sc, bif);
    845    1.1   thorpej 
    846   1.98     ozaki 	BRIDGE_UNLOCK(sc);
    847   1.87     ozaki 
    848   1.87     ozaki 	switch (ifs->if_type) {
    849   1.87     ozaki 	case IFT_ETHER:
    850  1.133  knakahar 	case IFT_L2TP:
    851   1.87     ozaki 		/*
    852   1.87     ozaki 		 * Take the interface out of promiscuous mode.
    853   1.95     ozaki 		 * Don't call it with holding a spin lock.
    854   1.87     ozaki 		 */
    855   1.87     ozaki 		(void) ifpromisc(ifs, 0);
    856  1.105  christos 		(void) ether_disable_vlan_mtu(ifs);
    857   1.87     ozaki 		break;
    858   1.87     ozaki 	default:
    859   1.87     ozaki #ifdef DIAGNOSTIC
    860   1.87     ozaki 		panic("bridge_delete_member: impossible");
    861   1.87     ozaki #endif
    862   1.87     ozaki 		break;
    863   1.87     ozaki 	}
    864   1.87     ozaki 
    865   1.87     ozaki 	bridge_rtdelete(sc, ifs);
    866   1.87     ozaki 
    867   1.87     ozaki 	if (sc->sc_if.if_flags & IFF_RUNNING)
    868   1.87     ozaki 		bstp_initialization(sc);
    869   1.87     ozaki 
    870   1.87     ozaki 	return 0;
    871    1.1   thorpej }
    872    1.1   thorpej 
    873   1.33   thorpej static int
    874    1.1   thorpej bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
    875    1.1   thorpej {
    876    1.1   thorpej 	struct ifbreq *req = arg;
    877    1.1   thorpej 	struct bridge_iflist *bif;
    878  1.115     ozaki 	struct psref psref;
    879    1.1   thorpej 
    880  1.115     ozaki 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
    881    1.1   thorpej 	if (bif == NULL)
    882  1.136   msaitoh 		return ENOENT;
    883    1.1   thorpej 
    884    1.1   thorpej 	req->ifbr_ifsflags = bif->bif_flags;
    885    1.1   thorpej 	req->ifbr_state = bif->bif_state;
    886    1.1   thorpej 	req->ifbr_priority = bif->bif_priority;
    887   1.11    bouyer 	req->ifbr_path_cost = bif->bif_path_cost;
    888    1.1   thorpej 	req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
    889    1.1   thorpej 
    890  1.115     ozaki 	bridge_release_member(sc, bif, &psref);
    891   1.87     ozaki 
    892  1.136   msaitoh 	return 0;
    893    1.1   thorpej }
    894    1.1   thorpej 
    895   1.33   thorpej static int
    896    1.1   thorpej bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
    897    1.1   thorpej {
    898    1.1   thorpej 	struct ifbreq *req = arg;
    899    1.1   thorpej 	struct bridge_iflist *bif;
    900  1.115     ozaki 	struct psref psref;
    901    1.1   thorpej 
    902  1.115     ozaki 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
    903    1.1   thorpej 	if (bif == NULL)
    904  1.136   msaitoh 		return ENOENT;
    905    1.1   thorpej 
    906    1.1   thorpej 	if (req->ifbr_ifsflags & IFBIF_STP) {
    907    1.1   thorpej 		switch (bif->bif_ifp->if_type) {
    908    1.1   thorpej 		case IFT_ETHER:
    909  1.133  knakahar 		case IFT_L2TP:
    910    1.1   thorpej 			/* These can do spanning tree. */
    911    1.1   thorpej 			break;
    912    1.1   thorpej 
    913    1.1   thorpej 		default:
    914    1.1   thorpej 			/* Nothing else can. */
    915  1.115     ozaki 			bridge_release_member(sc, bif, &psref);
    916  1.136   msaitoh 			return EINVAL;
    917    1.1   thorpej 		}
    918    1.1   thorpej 	}
    919    1.1   thorpej 
    920    1.1   thorpej 	bif->bif_flags = req->ifbr_ifsflags;
    921    1.1   thorpej 
    922  1.115     ozaki 	bridge_release_member(sc, bif, &psref);
    923   1.87     ozaki 
    924    1.1   thorpej 	if (sc->sc_if.if_flags & IFF_RUNNING)
    925    1.1   thorpej 		bstp_initialization(sc);
    926    1.1   thorpej 
    927  1.136   msaitoh 	return 0;
    928    1.1   thorpej }
    929    1.1   thorpej 
    930   1.33   thorpej static int
    931    1.1   thorpej bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
    932    1.1   thorpej {
    933    1.1   thorpej 	struct ifbrparam *param = arg;
    934    1.1   thorpej 
    935    1.1   thorpej 	sc->sc_brtmax = param->ifbrp_csize;
    936    1.1   thorpej 	bridge_rttrim(sc);
    937    1.1   thorpej 
    938  1.136   msaitoh 	return 0;
    939    1.1   thorpej }
    940    1.1   thorpej 
    941   1.33   thorpej static int
    942    1.1   thorpej bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
    943    1.1   thorpej {
    944    1.1   thorpej 	struct ifbrparam *param = arg;
    945    1.1   thorpej 
    946    1.1   thorpej 	param->ifbrp_csize = sc->sc_brtmax;
    947    1.1   thorpej 
    948  1.136   msaitoh 	return 0;
    949    1.1   thorpej }
    950    1.1   thorpej 
    951   1.33   thorpej static int
    952    1.1   thorpej bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
    953    1.1   thorpej {
    954    1.1   thorpej 	struct ifbifconf *bifc = arg;
    955    1.1   thorpej 	struct bridge_iflist *bif;
    956   1.90     ozaki 	struct ifbreq *breqs;
    957   1.90     ozaki 	int i, count, error = 0;
    958    1.1   thorpej 
    959   1.93     ozaki retry:
    960   1.87     ozaki 	BRIDGE_LOCK(sc);
    961    1.1   thorpej 	count = 0;
    962  1.115     ozaki 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc)
    963    1.1   thorpej 		count++;
    964   1.93     ozaki 	BRIDGE_UNLOCK(sc);
    965   1.93     ozaki 
    966   1.93     ozaki 	if (count == 0) {
    967   1.93     ozaki 		bifc->ifbic_len = 0;
    968   1.93     ozaki 		return 0;
    969   1.93     ozaki 	}
    970    1.1   thorpej 
    971   1.90     ozaki 	if (bifc->ifbic_len == 0 || bifc->ifbic_len < (sizeof(*breqs) * count)) {
    972   1.90     ozaki 		/* Tell that a larger buffer is needed */
    973   1.90     ozaki 		bifc->ifbic_len = sizeof(*breqs) * count;
    974   1.90     ozaki 		return 0;
    975    1.1   thorpej 	}
    976    1.1   thorpej 
    977   1.93     ozaki 	breqs = kmem_alloc(sizeof(*breqs) * count, KM_SLEEP);
    978   1.93     ozaki 
    979   1.93     ozaki 	BRIDGE_LOCK(sc);
    980   1.93     ozaki 
    981   1.93     ozaki 	i = 0;
    982  1.115     ozaki 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc)
    983   1.93     ozaki 		i++;
    984   1.93     ozaki 	if (i > count) {
    985   1.93     ozaki 		/*
    986   1.93     ozaki 		 * The number of members has been increased.
    987   1.93     ozaki 		 * We need more memory!
    988   1.93     ozaki 		 */
    989   1.93     ozaki 		BRIDGE_UNLOCK(sc);
    990   1.93     ozaki 		kmem_free(breqs, sizeof(*breqs) * count);
    991   1.93     ozaki 		goto retry;
    992   1.93     ozaki 	}
    993   1.90     ozaki 
    994   1.90     ozaki 	i = 0;
    995  1.115     ozaki 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) {
    996   1.90     ozaki 		struct ifbreq *breq = &breqs[i++];
    997   1.90     ozaki 		memset(breq, 0, sizeof(*breq));
    998   1.90     ozaki 
    999   1.90     ozaki 		strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
   1000   1.90     ozaki 		    sizeof(breq->ifbr_ifsname));
   1001   1.90     ozaki 		breq->ifbr_ifsflags = bif->bif_flags;
   1002   1.90     ozaki 		breq->ifbr_state = bif->bif_state;
   1003   1.90     ozaki 		breq->ifbr_priority = bif->bif_priority;
   1004   1.90     ozaki 		breq->ifbr_path_cost = bif->bif_path_cost;
   1005   1.90     ozaki 		breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
   1006   1.90     ozaki 	}
   1007   1.90     ozaki 
   1008   1.90     ozaki 	/* Don't call copyout with holding the mutex */
   1009   1.90     ozaki 	BRIDGE_UNLOCK(sc);
   1010    1.1   thorpej 
   1011   1.90     ozaki 	for (i = 0; i < count; i++) {
   1012   1.90     ozaki 		error = copyout(&breqs[i], bifc->ifbic_req + i, sizeof(*breqs));
   1013    1.1   thorpej 		if (error)
   1014    1.1   thorpej 			break;
   1015    1.1   thorpej 	}
   1016   1.90     ozaki 	bifc->ifbic_len = sizeof(*breqs) * i;
   1017    1.1   thorpej 
   1018   1.93     ozaki 	kmem_free(breqs, sizeof(*breqs) * count);
   1019   1.87     ozaki 
   1020   1.90     ozaki 	return error;
   1021    1.1   thorpej }
   1022    1.1   thorpej 
   1023   1.33   thorpej static int
   1024    1.1   thorpej bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
   1025    1.1   thorpej {
   1026    1.1   thorpej 	struct ifbaconf *bac = arg;
   1027    1.1   thorpej 	struct bridge_rtnode *brt;
   1028    1.1   thorpej 	struct ifbareq bareq;
   1029    1.1   thorpej 	int count = 0, error = 0, len;
   1030    1.1   thorpej 
   1031    1.1   thorpej 	if (bac->ifbac_len == 0)
   1032  1.136   msaitoh 		return 0;
   1033    1.1   thorpej 
   1034  1.108     ozaki 	BRIDGE_RT_LOCK(sc);
   1035   1.87     ozaki 
   1036    1.1   thorpej 	len = bac->ifbac_len;
   1037    1.1   thorpej 	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
   1038    1.1   thorpej 		if (len < sizeof(bareq))
   1039    1.1   thorpej 			goto out;
   1040   1.35  christos 		memset(&bareq, 0, sizeof(bareq));
   1041   1.13    itojun 		strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
   1042   1.13    itojun 		    sizeof(bareq.ifba_ifsname));
   1043    1.1   thorpej 		memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
   1044   1.39    kardel 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
   1045   1.39    kardel 			bareq.ifba_expire = brt->brt_expire - time_uptime;
   1046   1.39    kardel 		} else
   1047    1.2   thorpej 			bareq.ifba_expire = 0;
   1048    1.1   thorpej 		bareq.ifba_flags = brt->brt_flags;
   1049    1.1   thorpej 
   1050    1.1   thorpej 		error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
   1051    1.1   thorpej 		if (error)
   1052    1.1   thorpej 			goto out;
   1053    1.1   thorpej 		count++;
   1054    1.1   thorpej 		len -= sizeof(bareq);
   1055    1.1   thorpej 	}
   1056    1.1   thorpej  out:
   1057  1.108     ozaki 	BRIDGE_RT_UNLOCK(sc);
   1058   1.87     ozaki 
   1059    1.1   thorpej 	bac->ifbac_len = sizeof(bareq) * count;
   1060  1.136   msaitoh 	return error;
   1061    1.1   thorpej }
   1062    1.1   thorpej 
   1063   1.33   thorpej static int
   1064    1.1   thorpej bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
   1065    1.1   thorpej {
   1066    1.1   thorpej 	struct ifbareq *req = arg;
   1067    1.1   thorpej 	struct bridge_iflist *bif;
   1068    1.1   thorpej 	int error;
   1069  1.115     ozaki 	struct psref psref;
   1070    1.1   thorpej 
   1071  1.115     ozaki 	bif = bridge_lookup_member(sc, req->ifba_ifsname, &psref);
   1072    1.1   thorpej 	if (bif == NULL)
   1073  1.136   msaitoh 		return ENOENT;
   1074    1.1   thorpej 
   1075    1.1   thorpej 	error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
   1076    1.1   thorpej 	    req->ifba_flags);
   1077    1.1   thorpej 
   1078  1.115     ozaki 	bridge_release_member(sc, bif, &psref);
   1079   1.87     ozaki 
   1080  1.136   msaitoh 	return error;
   1081    1.1   thorpej }
   1082    1.1   thorpej 
   1083   1.33   thorpej static int
   1084    1.1   thorpej bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
   1085    1.1   thorpej {
   1086    1.1   thorpej 	struct ifbrparam *param = arg;
   1087    1.1   thorpej 
   1088    1.1   thorpej 	sc->sc_brttimeout = param->ifbrp_ctime;
   1089    1.1   thorpej 
   1090  1.136   msaitoh 	return 0;
   1091    1.1   thorpej }
   1092    1.1   thorpej 
   1093   1.33   thorpej static int
   1094    1.1   thorpej bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
   1095    1.1   thorpej {
   1096    1.1   thorpej 	struct ifbrparam *param = arg;
   1097    1.1   thorpej 
   1098    1.1   thorpej 	param->ifbrp_ctime = sc->sc_brttimeout;
   1099    1.1   thorpej 
   1100  1.136   msaitoh 	return 0;
   1101    1.1   thorpej }
   1102    1.1   thorpej 
   1103   1.33   thorpej static int
   1104    1.1   thorpej bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
   1105    1.1   thorpej {
   1106    1.1   thorpej 	struct ifbareq *req = arg;
   1107    1.1   thorpej 
   1108    1.1   thorpej 	return (bridge_rtdaddr(sc, req->ifba_dst));
   1109    1.1   thorpej }
   1110    1.1   thorpej 
   1111   1.33   thorpej static int
   1112    1.1   thorpej bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
   1113    1.1   thorpej {
   1114    1.1   thorpej 	struct ifbreq *req = arg;
   1115    1.1   thorpej 
   1116    1.1   thorpej 	bridge_rtflush(sc, req->ifbr_ifsflags);
   1117    1.1   thorpej 
   1118  1.136   msaitoh 	return 0;
   1119    1.1   thorpej }
   1120    1.1   thorpej 
   1121   1.33   thorpej static int
   1122    1.1   thorpej bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
   1123    1.1   thorpej {
   1124    1.1   thorpej 	struct ifbrparam *param = arg;
   1125    1.1   thorpej 
   1126    1.1   thorpej 	param->ifbrp_prio = sc->sc_bridge_priority;
   1127    1.1   thorpej 
   1128  1.136   msaitoh 	return 0;
   1129    1.1   thorpej }
   1130    1.1   thorpej 
   1131   1.33   thorpej static int
   1132    1.1   thorpej bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
   1133    1.1   thorpej {
   1134    1.1   thorpej 	struct ifbrparam *param = arg;
   1135    1.1   thorpej 
   1136    1.1   thorpej 	sc->sc_bridge_priority = param->ifbrp_prio;
   1137    1.1   thorpej 
   1138    1.1   thorpej 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1139    1.1   thorpej 		bstp_initialization(sc);
   1140    1.1   thorpej 
   1141  1.136   msaitoh 	return 0;
   1142    1.1   thorpej }
   1143    1.1   thorpej 
   1144   1.33   thorpej static int
   1145    1.1   thorpej bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
   1146    1.1   thorpej {
   1147    1.1   thorpej 	struct ifbrparam *param = arg;
   1148    1.1   thorpej 
   1149    1.1   thorpej 	param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
   1150    1.1   thorpej 
   1151  1.136   msaitoh 	return 0;
   1152    1.1   thorpej }
   1153    1.1   thorpej 
   1154   1.33   thorpej static int
   1155    1.1   thorpej bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
   1156    1.1   thorpej {
   1157    1.1   thorpej 	struct ifbrparam *param = arg;
   1158    1.1   thorpej 
   1159    1.1   thorpej 	if (param->ifbrp_hellotime == 0)
   1160  1.136   msaitoh 		return EINVAL;
   1161    1.1   thorpej 	sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
   1162    1.1   thorpej 
   1163    1.1   thorpej 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1164    1.1   thorpej 		bstp_initialization(sc);
   1165    1.1   thorpej 
   1166  1.136   msaitoh 	return 0;
   1167    1.1   thorpej }
   1168    1.1   thorpej 
   1169   1.33   thorpej static int
   1170    1.1   thorpej bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
   1171    1.1   thorpej {
   1172    1.1   thorpej 	struct ifbrparam *param = arg;
   1173    1.1   thorpej 
   1174    1.1   thorpej 	param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
   1175    1.1   thorpej 
   1176  1.136   msaitoh 	return 0;
   1177    1.1   thorpej }
   1178    1.1   thorpej 
   1179   1.33   thorpej static int
   1180    1.1   thorpej bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
   1181    1.1   thorpej {
   1182   1.29     perry 	struct ifbrparam *param = arg;
   1183    1.1   thorpej 
   1184    1.1   thorpej 	if (param->ifbrp_fwddelay == 0)
   1185  1.136   msaitoh 		return EINVAL;
   1186    1.1   thorpej 	sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
   1187    1.1   thorpej 
   1188    1.1   thorpej 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1189    1.1   thorpej 		bstp_initialization(sc);
   1190    1.1   thorpej 
   1191  1.136   msaitoh 	return 0;
   1192    1.1   thorpej }
   1193    1.1   thorpej 
   1194   1.33   thorpej static int
   1195    1.1   thorpej bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
   1196    1.1   thorpej {
   1197    1.1   thorpej 	struct ifbrparam *param = arg;
   1198    1.1   thorpej 
   1199    1.1   thorpej 	param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
   1200    1.1   thorpej 
   1201  1.136   msaitoh 	return 0;
   1202    1.1   thorpej }
   1203    1.1   thorpej 
   1204   1.33   thorpej static int
   1205    1.1   thorpej bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
   1206    1.1   thorpej {
   1207    1.1   thorpej 	struct ifbrparam *param = arg;
   1208    1.1   thorpej 
   1209    1.1   thorpej 	if (param->ifbrp_maxage == 0)
   1210  1.136   msaitoh 		return EINVAL;
   1211    1.1   thorpej 	sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
   1212    1.1   thorpej 
   1213    1.1   thorpej 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1214    1.1   thorpej 		bstp_initialization(sc);
   1215    1.1   thorpej 
   1216  1.136   msaitoh 	return 0;
   1217    1.1   thorpej }
   1218    1.1   thorpej 
   1219   1.33   thorpej static int
   1220    1.1   thorpej bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
   1221    1.1   thorpej {
   1222    1.1   thorpej 	struct ifbreq *req = arg;
   1223    1.1   thorpej 	struct bridge_iflist *bif;
   1224  1.115     ozaki 	struct psref psref;
   1225    1.1   thorpej 
   1226  1.115     ozaki 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
   1227    1.1   thorpej 	if (bif == NULL)
   1228  1.136   msaitoh 		return ENOENT;
   1229    1.1   thorpej 
   1230    1.1   thorpej 	bif->bif_priority = req->ifbr_priority;
   1231    1.1   thorpej 
   1232    1.1   thorpej 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1233    1.1   thorpej 		bstp_initialization(sc);
   1234    1.1   thorpej 
   1235  1.115     ozaki 	bridge_release_member(sc, bif, &psref);
   1236   1.87     ozaki 
   1237  1.136   msaitoh 	return 0;
   1238    1.1   thorpej }
   1239    1.1   thorpej 
   1240   1.77     rmind #if defined(BRIDGE_IPF)
   1241   1.33   thorpej static int
   1242    1.9  perseant bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
   1243    1.9  perseant {
   1244    1.9  perseant 	struct ifbrparam *param = arg;
   1245    1.9  perseant 
   1246    1.9  perseant 	param->ifbrp_filter = sc->sc_filter_flags;
   1247    1.9  perseant 
   1248  1.136   msaitoh 	return 0;
   1249    1.9  perseant }
   1250    1.9  perseant 
   1251   1.33   thorpej static int
   1252    1.9  perseant bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
   1253    1.9  perseant {
   1254   1.29     perry 	struct ifbrparam *param = arg;
   1255    1.9  perseant 	uint32_t nflags, oflags;
   1256    1.9  perseant 
   1257    1.9  perseant 	if (param->ifbrp_filter & ~IFBF_FILT_MASK)
   1258  1.136   msaitoh 		return EINVAL;
   1259    1.9  perseant 
   1260    1.9  perseant 	nflags = param->ifbrp_filter;
   1261    1.9  perseant 	oflags = sc->sc_filter_flags;
   1262    1.9  perseant 
   1263    1.9  perseant 	if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) {
   1264    1.9  perseant 		pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
   1265   1.77     rmind 			sc->sc_if.if_pfil);
   1266    1.9  perseant 	}
   1267    1.9  perseant 	if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) {
   1268    1.9  perseant 		pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
   1269   1.77     rmind 			sc->sc_if.if_pfil);
   1270    1.9  perseant 	}
   1271    1.9  perseant 
   1272    1.9  perseant 	sc->sc_filter_flags = nflags;
   1273    1.9  perseant 
   1274  1.136   msaitoh 	return 0;
   1275    1.9  perseant }
   1276   1.77     rmind #endif /* BRIDGE_IPF */
   1277    1.9  perseant 
   1278   1.33   thorpej static int
   1279   1.11    bouyer bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
   1280   1.11    bouyer {
   1281   1.11    bouyer 	struct ifbreq *req = arg;
   1282   1.11    bouyer 	struct bridge_iflist *bif;
   1283  1.115     ozaki 	struct psref psref;
   1284   1.11    bouyer 
   1285  1.115     ozaki 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
   1286   1.11    bouyer 	if (bif == NULL)
   1287  1.136   msaitoh 		return ENOENT;
   1288   1.11    bouyer 
   1289   1.11    bouyer 	bif->bif_path_cost = req->ifbr_path_cost;
   1290   1.11    bouyer 
   1291   1.11    bouyer 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1292   1.11    bouyer 		bstp_initialization(sc);
   1293   1.11    bouyer 
   1294  1.115     ozaki 	bridge_release_member(sc, bif, &psref);
   1295   1.87     ozaki 
   1296  1.136   msaitoh 	return 0;
   1297   1.11    bouyer }
   1298   1.11    bouyer 
   1299    1.1   thorpej /*
   1300    1.1   thorpej  * bridge_ifdetach:
   1301    1.1   thorpej  *
   1302    1.1   thorpej  *	Detach an interface from a bridge.  Called when a member
   1303    1.1   thorpej  *	interface is detaching.
   1304    1.1   thorpej  */
   1305    1.1   thorpej void
   1306    1.1   thorpej bridge_ifdetach(struct ifnet *ifp)
   1307    1.1   thorpej {
   1308    1.1   thorpej 	struct bridge_softc *sc = ifp->if_bridge;
   1309    1.1   thorpej 	struct ifbreq breq;
   1310    1.1   thorpej 
   1311   1.87     ozaki 	/* ioctl_lock should prevent this from happening */
   1312   1.87     ozaki 	KASSERT(sc != NULL);
   1313   1.87     ozaki 
   1314    1.1   thorpej 	memset(&breq, 0, sizeof(breq));
   1315   1.73     joerg 	strlcpy(breq.ifbr_ifsname, ifp->if_xname, sizeof(breq.ifbr_ifsname));
   1316    1.1   thorpej 
   1317    1.1   thorpej 	(void) bridge_ioctl_del(sc, &breq);
   1318    1.1   thorpej }
   1319    1.1   thorpej 
   1320    1.1   thorpej /*
   1321    1.1   thorpej  * bridge_init:
   1322    1.1   thorpej  *
   1323    1.1   thorpej  *	Initialize a bridge interface.
   1324    1.1   thorpej  */
   1325   1.33   thorpej static int
   1326    1.1   thorpej bridge_init(struct ifnet *ifp)
   1327    1.1   thorpej {
   1328    1.1   thorpej 	struct bridge_softc *sc = ifp->if_softc;
   1329    1.1   thorpej 
   1330    1.1   thorpej 	if (ifp->if_flags & IFF_RUNNING)
   1331  1.136   msaitoh 		return 0;
   1332    1.1   thorpej 
   1333    1.1   thorpej 	callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
   1334    1.1   thorpej 	    bridge_timer, sc);
   1335    1.1   thorpej 
   1336    1.1   thorpej 	ifp->if_flags |= IFF_RUNNING;
   1337   1.11    bouyer 	bstp_initialization(sc);
   1338  1.136   msaitoh 	return 0;
   1339    1.1   thorpej }
   1340    1.1   thorpej 
   1341    1.1   thorpej /*
   1342    1.1   thorpej  * bridge_stop:
   1343    1.1   thorpej  *
   1344    1.1   thorpej  *	Stop the bridge interface.
   1345    1.1   thorpej  */
   1346   1.33   thorpej static void
   1347   1.45  christos bridge_stop(struct ifnet *ifp, int disable)
   1348    1.1   thorpej {
   1349    1.1   thorpej 	struct bridge_softc *sc = ifp->if_softc;
   1350    1.1   thorpej 
   1351    1.1   thorpej 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1352    1.1   thorpej 		return;
   1353    1.1   thorpej 
   1354    1.1   thorpej 	callout_stop(&sc->sc_brcallout);
   1355    1.1   thorpej 	bstp_stop(sc);
   1356    1.1   thorpej 
   1357    1.1   thorpej 	bridge_rtflush(sc, IFBF_FLUSHDYN);
   1358    1.1   thorpej 
   1359    1.1   thorpej 	ifp->if_flags &= ~IFF_RUNNING;
   1360    1.1   thorpej }
   1361    1.1   thorpej 
   1362    1.1   thorpej /*
   1363    1.1   thorpej  * bridge_enqueue:
   1364    1.1   thorpej  *
   1365    1.1   thorpej  *	Enqueue a packet on a bridge member interface.
   1366    1.1   thorpej  */
   1367   1.33   thorpej void
   1368   1.18       jdc bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m,
   1369   1.18       jdc     int runfilt)
   1370    1.1   thorpej {
   1371    1.1   thorpej 	int len, error;
   1372    1.1   thorpej 	short mflags;
   1373    1.1   thorpej 
   1374   1.26       bad 	/*
   1375   1.26       bad 	 * Clear any in-bound checksum flags for this packet.
   1376   1.26       bad 	 */
   1377   1.26       bad 	m->m_pkthdr.csum_flags = 0;
   1378   1.26       bad 
   1379   1.18       jdc 	if (runfilt) {
   1380   1.77     rmind 		if (pfil_run_hooks(sc->sc_if.if_pfil, &m,
   1381   1.22       jdc 		    dst_ifp, PFIL_OUT) != 0) {
   1382   1.22       jdc 			if (m != NULL)
   1383   1.22       jdc 				m_freem(m);
   1384   1.18       jdc 			return;
   1385   1.18       jdc 		}
   1386   1.22       jdc 		if (m == NULL)
   1387   1.22       jdc 			return;
   1388    1.9  perseant 	}
   1389    1.9  perseant 
   1390    1.1   thorpej #ifdef ALTQ
   1391  1.126  knakahar 	KERNEL_LOCK(1, NULL);
   1392    1.1   thorpej 	/*
   1393    1.1   thorpej 	 * If ALTQ is enabled on the member interface, do
   1394    1.1   thorpej 	 * classification; the queueing discipline might
   1395    1.1   thorpej 	 * not require classification, but might require
   1396    1.1   thorpej 	 * the address family/header pointer in the pktattr.
   1397    1.1   thorpej 	 */
   1398    1.1   thorpej 	if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
   1399    1.1   thorpej 		/* XXX IFT_ETHER */
   1400  1.116  knakahar 		altq_etherclassify(&dst_ifp->if_snd, m);
   1401    1.1   thorpej 	}
   1402  1.126  knakahar 	KERNEL_UNLOCK_ONE(NULL);
   1403    1.1   thorpej #endif /* ALTQ */
   1404    1.1   thorpej 
   1405    1.1   thorpej 	len = m->m_pkthdr.len;
   1406    1.1   thorpej 	mflags = m->m_flags;
   1407   1.87     ozaki 
   1408  1.129  knakahar 	error = if_transmit_lock(dst_ifp, m);
   1409    1.1   thorpej 	if (error) {
   1410    1.1   thorpej 		/* mbuf is already freed */
   1411    1.1   thorpej 		sc->sc_if.if_oerrors++;
   1412    1.1   thorpej 		return;
   1413    1.1   thorpej 	}
   1414    1.1   thorpej 
   1415    1.1   thorpej 	sc->sc_if.if_opackets++;
   1416    1.1   thorpej 	sc->sc_if.if_obytes += len;
   1417  1.121  knakahar 	if (mflags & M_MCAST)
   1418    1.1   thorpej 		sc->sc_if.if_omcasts++;
   1419    1.1   thorpej }
   1420    1.1   thorpej 
   1421    1.1   thorpej /*
   1422    1.1   thorpej  * bridge_output:
   1423    1.1   thorpej  *
   1424    1.1   thorpej  *	Send output from a bridge member interface.  This
   1425    1.1   thorpej  *	performs the bridging function for locally originated
   1426    1.1   thorpej  *	packets.
   1427    1.1   thorpej  *
   1428    1.1   thorpej  *	The mbuf has the Ethernet header already attached.  We must
   1429    1.1   thorpej  *	enqueue or free the mbuf before returning.
   1430    1.1   thorpej  */
   1431    1.1   thorpej int
   1432   1.48    dyoung bridge_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
   1433  1.120     ozaki     const struct rtentry *rt)
   1434    1.1   thorpej {
   1435    1.1   thorpej 	struct ether_header *eh;
   1436    1.1   thorpej 	struct ifnet *dst_if;
   1437    1.1   thorpej 	struct bridge_softc *sc;
   1438    1.1   thorpej 	int s;
   1439    1.1   thorpej 
   1440  1.128  knakahar 	/*
   1441  1.128  knakahar 	 * bridge_output() is called from ether_output(), furthermore
   1442  1.128  knakahar 	 * ifp argument doesn't point to bridge(4). So, don't assert
   1443  1.140     ozaki 	 * IFEF_MPSAFE here.
   1444  1.128  knakahar 	 */
   1445  1.128  knakahar 
   1446    1.1   thorpej 	if (m->m_len < ETHER_HDR_LEN) {
   1447    1.1   thorpej 		m = m_pullup(m, ETHER_HDR_LEN);
   1448    1.1   thorpej 		if (m == NULL)
   1449  1.136   msaitoh 			return 0;
   1450    1.1   thorpej 	}
   1451    1.1   thorpej 
   1452    1.1   thorpej 	eh = mtod(m, struct ether_header *);
   1453    1.1   thorpej 	sc = ifp->if_bridge;
   1454    1.1   thorpej 
   1455  1.122       roy 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
   1456  1.122       roy 		if (memcmp(etherbroadcastaddr,
   1457  1.122       roy 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
   1458  1.122       roy 			m->m_flags |= M_BCAST;
   1459  1.122       roy 		else
   1460  1.122       roy 			m->m_flags |= M_MCAST;
   1461  1.122       roy 	}
   1462  1.122       roy 
   1463    1.1   thorpej 	/*
   1464    1.1   thorpej 	 * If bridge is down, but the original output interface is up,
   1465    1.1   thorpej 	 * go ahead and send out that interface.  Otherwise, the packet
   1466    1.1   thorpej 	 * is dropped below.
   1467    1.1   thorpej 	 */
   1468   1.87     ozaki 	if (__predict_false(sc == NULL) ||
   1469   1.87     ozaki 	    (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   1470    1.1   thorpej 		dst_if = ifp;
   1471    1.1   thorpej 		goto sendunicast;
   1472    1.1   thorpej 	}
   1473    1.1   thorpej 
   1474    1.1   thorpej 	/*
   1475    1.1   thorpej 	 * If the packet is a multicast, or we don't know a better way to
   1476    1.1   thorpej 	 * get there, send to all interfaces.
   1477    1.1   thorpej 	 */
   1478  1.122       roy 	if ((m->m_flags & (M_MCAST | M_BCAST)) != 0)
   1479    1.1   thorpej 		dst_if = NULL;
   1480    1.1   thorpej 	else
   1481    1.1   thorpej 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
   1482    1.1   thorpej 	if (dst_if == NULL) {
   1483  1.122       roy 		/* XXX Should call bridge_broadcast, but there are locking
   1484  1.122       roy 		 * issues which need resolving first. */
   1485    1.1   thorpej 		struct bridge_iflist *bif;
   1486    1.1   thorpej 		struct mbuf *mc;
   1487  1.118       roy 		bool used = false;
   1488    1.1   thorpej 
   1489  1.114     ozaki 		BRIDGE_PSZ_RENTER(s);
   1490  1.115     ozaki 		BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
   1491  1.115     ozaki 			struct psref psref;
   1492  1.115     ozaki 
   1493  1.115     ozaki 			bridge_acquire_member(sc, bif, &psref);
   1494  1.114     ozaki 			BRIDGE_PSZ_REXIT(s);
   1495   1.87     ozaki 
   1496    1.1   thorpej 			dst_if = bif->bif_ifp;
   1497    1.1   thorpej 			if ((dst_if->if_flags & IFF_RUNNING) == 0)
   1498   1.95     ozaki 				goto next;
   1499    1.1   thorpej 
   1500    1.1   thorpej 			/*
   1501    1.1   thorpej 			 * If this is not the original output interface,
   1502    1.1   thorpej 			 * and the interface is participating in spanning
   1503    1.1   thorpej 			 * tree, make sure the port is in a state that
   1504    1.1   thorpej 			 * allows forwarding.
   1505    1.1   thorpej 			 */
   1506    1.1   thorpej 			if (dst_if != ifp &&
   1507    1.1   thorpej 			    (bif->bif_flags & IFBIF_STP) != 0) {
   1508    1.1   thorpej 				switch (bif->bif_state) {
   1509    1.1   thorpej 				case BSTP_IFSTATE_BLOCKING:
   1510    1.1   thorpej 				case BSTP_IFSTATE_LISTENING:
   1511    1.1   thorpej 				case BSTP_IFSTATE_DISABLED:
   1512   1.95     ozaki 					goto next;
   1513    1.1   thorpej 				}
   1514    1.1   thorpej 			}
   1515    1.1   thorpej 
   1516  1.112     ozaki 			if (PSLIST_READER_NEXT(bif, struct bridge_iflist,
   1517  1.122       roy 			    bif_next) == NULL &&
   1518  1.122       roy 			    ((m->m_flags & (M_MCAST | M_BCAST)) == 0 ||
   1519  1.122       roy 			    dst_if == ifp))
   1520  1.122       roy 			{
   1521  1.118       roy 				used = true;
   1522    1.1   thorpej 				mc = m;
   1523    1.1   thorpej 			} else {
   1524    1.1   thorpej 				mc = m_copym(m, 0, M_COPYALL, M_NOWAIT);
   1525    1.1   thorpej 				if (mc == NULL) {
   1526    1.1   thorpej 					sc->sc_if.if_oerrors++;
   1527   1.95     ozaki 					goto next;
   1528    1.1   thorpej 				}
   1529    1.1   thorpej 			}
   1530    1.1   thorpej 
   1531   1.18       jdc 			bridge_enqueue(sc, dst_if, mc, 0);
   1532  1.122       roy 
   1533  1.122       roy 			if ((m->m_flags & (M_MCAST | M_BCAST)) != 0 &&
   1534  1.122       roy 			    dst_if != ifp)
   1535  1.122       roy 			{
   1536  1.122       roy 				if (PSLIST_READER_NEXT(bif,
   1537  1.122       roy 				    struct bridge_iflist, bif_next) == NULL)
   1538  1.122       roy 				{
   1539  1.122       roy 					used = true;
   1540  1.122       roy 					mc = m;
   1541  1.122       roy 				} else {
   1542  1.122       roy 					mc = m_copym(m, 0, M_COPYALL,
   1543  1.122       roy 					    M_DONTWAIT);
   1544  1.122       roy 					if (mc == NULL) {
   1545  1.122       roy 						sc->sc_if.if_oerrors++;
   1546  1.122       roy 						goto next;
   1547  1.122       roy 					}
   1548  1.122       roy 				}
   1549  1.122       roy 
   1550  1.124     ozaki 				m_set_rcvif(mc, dst_if);
   1551  1.122       roy 				mc->m_flags &= ~M_PROMISC;
   1552  1.122       roy 
   1553  1.122       roy #ifndef NET_MPSAFE
   1554  1.132     ozaki 				s = splsoftnet();
   1555  1.122       roy #endif
   1556  1.122       roy 				ether_input(dst_if, mc);
   1557  1.122       roy #ifndef NET_MPSAFE
   1558  1.122       roy 				splx(s);
   1559  1.122       roy #endif
   1560  1.122       roy 			}
   1561  1.122       roy 
   1562   1.95     ozaki next:
   1563  1.114     ozaki 			BRIDGE_PSZ_RENTER(s);
   1564  1.115     ozaki 			bridge_release_member(sc, bif, &psref);
   1565  1.118       roy 
   1566  1.118       roy 			/* Guarantee we don't re-enter the loop as we already
   1567  1.118       roy 			 * decided we're at the end. */
   1568  1.118       roy 			if (used)
   1569  1.118       roy 				break;
   1570    1.1   thorpej 		}
   1571  1.114     ozaki 		BRIDGE_PSZ_REXIT(s);
   1572   1.87     ozaki 
   1573  1.118       roy 		if (!used)
   1574    1.1   thorpej 			m_freem(m);
   1575  1.136   msaitoh 		return 0;
   1576    1.1   thorpej 	}
   1577    1.1   thorpej 
   1578    1.1   thorpej  sendunicast:
   1579    1.1   thorpej 	/*
   1580    1.1   thorpej 	 * XXX Spanning tree consideration here?
   1581    1.1   thorpej 	 */
   1582    1.1   thorpej 
   1583    1.1   thorpej 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
   1584    1.1   thorpej 		m_freem(m);
   1585  1.136   msaitoh 		return 0;
   1586    1.1   thorpej 	}
   1587    1.1   thorpej 
   1588   1.18       jdc 	bridge_enqueue(sc, dst_if, m, 0);
   1589  1.114     ozaki 
   1590  1.136   msaitoh 	return 0;
   1591    1.1   thorpej }
   1592    1.1   thorpej 
   1593    1.1   thorpej /*
   1594    1.1   thorpej  * bridge_start:
   1595    1.1   thorpej  *
   1596    1.1   thorpej  *	Start output on a bridge.
   1597    1.1   thorpej  *
   1598    1.1   thorpej  *	NOTE: This routine should never be called in this implementation.
   1599    1.1   thorpej  */
   1600   1.33   thorpej static void
   1601    1.1   thorpej bridge_start(struct ifnet *ifp)
   1602    1.1   thorpej {
   1603    1.1   thorpej 
   1604    1.1   thorpej 	printf("%s: bridge_start() called\n", ifp->if_xname);
   1605    1.1   thorpej }
   1606    1.1   thorpej 
   1607    1.1   thorpej /*
   1608    1.1   thorpej  * bridge_forward:
   1609    1.1   thorpej  *
   1610   1.21  augustss  *	The forwarding function of the bridge.
   1611    1.1   thorpej  */
   1612   1.33   thorpej static void
   1613  1.108     ozaki bridge_forward(struct bridge_softc *sc, struct mbuf *m)
   1614    1.1   thorpej {
   1615    1.1   thorpej 	struct bridge_iflist *bif;
   1616    1.1   thorpej 	struct ifnet *src_if, *dst_if;
   1617    1.1   thorpej 	struct ether_header *eh;
   1618  1.115     ozaki 	struct psref psref;
   1619  1.125     ozaki 	struct psref psref_src;
   1620  1.114     ozaki 	DECLARE_LOCK_VARIABLE;
   1621    1.1   thorpej 
   1622  1.114     ozaki 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   1623   1.65    bouyer 		return;
   1624   1.65    bouyer 
   1625  1.125     ozaki 	src_if = m_get_rcvif_psref(m, &psref_src);
   1626  1.125     ozaki 	if (src_if == NULL) {
   1627  1.125     ozaki 		/* Interface is being destroyed? */
   1628  1.125     ozaki 		m_freem(m);
   1629  1.125     ozaki 		goto out;
   1630  1.125     ozaki 	}
   1631    1.1   thorpej 
   1632  1.108     ozaki 	sc->sc_if.if_ipackets++;
   1633  1.108     ozaki 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
   1634   1.65    bouyer 
   1635  1.108     ozaki 	/*
   1636  1.108     ozaki 	 * Look up the bridge_iflist.
   1637  1.108     ozaki 	 */
   1638  1.115     ozaki 	bif = bridge_lookup_member_if(sc, src_if, &psref);
   1639  1.108     ozaki 	if (bif == NULL) {
   1640  1.108     ozaki 		/* Interface is not a bridge member (anymore?) */
   1641  1.108     ozaki 		m_freem(m);
   1642  1.108     ozaki 		goto out;
   1643  1.108     ozaki 	}
   1644    1.1   thorpej 
   1645  1.108     ozaki 	if (bif->bif_flags & IFBIF_STP) {
   1646  1.108     ozaki 		switch (bif->bif_state) {
   1647  1.108     ozaki 		case BSTP_IFSTATE_BLOCKING:
   1648  1.108     ozaki 		case BSTP_IFSTATE_LISTENING:
   1649  1.108     ozaki 		case BSTP_IFSTATE_DISABLED:
   1650   1.65    bouyer 			m_freem(m);
   1651  1.115     ozaki 			bridge_release_member(sc, bif, &psref);
   1652  1.108     ozaki 			goto out;
   1653   1.65    bouyer 		}
   1654  1.108     ozaki 	}
   1655    1.1   thorpej 
   1656  1.108     ozaki 	eh = mtod(m, struct ether_header *);
   1657   1.87     ozaki 
   1658  1.108     ozaki 	/*
   1659  1.108     ozaki 	 * If the interface is learning, and the source
   1660  1.108     ozaki 	 * address is valid and not multicast, record
   1661  1.108     ozaki 	 * the address.
   1662  1.108     ozaki 	 */
   1663  1.108     ozaki 	if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
   1664  1.108     ozaki 	    ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
   1665  1.108     ozaki 	    (eh->ether_shost[0] == 0 &&
   1666  1.108     ozaki 	     eh->ether_shost[1] == 0 &&
   1667  1.108     ozaki 	     eh->ether_shost[2] == 0 &&
   1668  1.108     ozaki 	     eh->ether_shost[3] == 0 &&
   1669  1.108     ozaki 	     eh->ether_shost[4] == 0 &&
   1670  1.108     ozaki 	     eh->ether_shost[5] == 0) == 0) {
   1671  1.108     ozaki 		(void) bridge_rtupdate(sc, eh->ether_shost,
   1672  1.108     ozaki 		    src_if, 0, IFBAF_DYNAMIC);
   1673  1.108     ozaki 	}
   1674    1.1   thorpej 
   1675  1.108     ozaki 	if ((bif->bif_flags & IFBIF_STP) != 0 &&
   1676  1.108     ozaki 	    bif->bif_state == BSTP_IFSTATE_LEARNING) {
   1677  1.108     ozaki 		m_freem(m);
   1678  1.115     ozaki 		bridge_release_member(sc, bif, &psref);
   1679  1.108     ozaki 		goto out;
   1680  1.108     ozaki 	}
   1681    1.1   thorpej 
   1682  1.115     ozaki 	bridge_release_member(sc, bif, &psref);
   1683    1.9  perseant 
   1684  1.108     ozaki 	/*
   1685  1.108     ozaki 	 * At this point, the port either doesn't participate
   1686  1.108     ozaki 	 * in spanning tree or it is in the forwarding state.
   1687  1.108     ozaki 	 */
   1688    1.1   thorpej 
   1689  1.108     ozaki 	/*
   1690  1.108     ozaki 	 * If the packet is unicast, destined for someone on
   1691  1.108     ozaki 	 * "this" side of the bridge, drop it.
   1692  1.108     ozaki 	 */
   1693  1.108     ozaki 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
   1694  1.108     ozaki 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
   1695  1.108     ozaki 		if (src_if == dst_if) {
   1696   1.65    bouyer 			m_freem(m);
   1697  1.108     ozaki 			goto out;
   1698   1.65    bouyer 		}
   1699  1.108     ozaki 	} else {
   1700  1.108     ozaki 		/* ...forward it to all interfaces. */
   1701  1.108     ozaki 		sc->sc_if.if_imcasts++;
   1702  1.108     ozaki 		dst_if = NULL;
   1703  1.108     ozaki 	}
   1704   1.87     ozaki 
   1705  1.125     ozaki 	if (pfil_run_hooks(sc->sc_if.if_pfil, &m, src_if, PFIL_IN) != 0) {
   1706  1.108     ozaki 		if (m != NULL)
   1707   1.65    bouyer 			m_freem(m);
   1708  1.108     ozaki 		goto out;
   1709  1.108     ozaki 	}
   1710  1.108     ozaki 	if (m == NULL)
   1711  1.108     ozaki 		goto out;
   1712  1.108     ozaki 
   1713  1.108     ozaki 	if (dst_if == NULL) {
   1714  1.108     ozaki 		bridge_broadcast(sc, src_if, m);
   1715  1.108     ozaki 		goto out;
   1716  1.108     ozaki 	}
   1717  1.108     ozaki 
   1718  1.125     ozaki 	m_put_rcvif_psref(src_if, &psref_src);
   1719  1.125     ozaki 	src_if = NULL;
   1720  1.125     ozaki 
   1721  1.108     ozaki 	/*
   1722  1.108     ozaki 	 * At this point, we're dealing with a unicast frame
   1723  1.108     ozaki 	 * going to a different interface.
   1724  1.108     ozaki 	 */
   1725  1.108     ozaki 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
   1726  1.108     ozaki 		m_freem(m);
   1727  1.108     ozaki 		goto out;
   1728  1.108     ozaki 	}
   1729  1.108     ozaki 
   1730  1.115     ozaki 	bif = bridge_lookup_member_if(sc, dst_if, &psref);
   1731  1.108     ozaki 	if (bif == NULL) {
   1732  1.108     ozaki 		/* Not a member of the bridge (anymore?) */
   1733  1.108     ozaki 		m_freem(m);
   1734  1.108     ozaki 		goto out;
   1735  1.108     ozaki 	}
   1736    1.1   thorpej 
   1737  1.108     ozaki 	if (bif->bif_flags & IFBIF_STP) {
   1738  1.108     ozaki 		switch (bif->bif_state) {
   1739  1.108     ozaki 		case BSTP_IFSTATE_DISABLED:
   1740  1.108     ozaki 		case BSTP_IFSTATE_BLOCKING:
   1741  1.108     ozaki 			m_freem(m);
   1742  1.115     ozaki 			bridge_release_member(sc, bif, &psref);
   1743  1.108     ozaki 			goto out;
   1744    1.1   thorpej 		}
   1745  1.108     ozaki 	}
   1746   1.65    bouyer 
   1747  1.115     ozaki 	bridge_release_member(sc, bif, &psref);
   1748   1.87     ozaki 
   1749  1.114     ozaki 	ACQUIRE_GLOBAL_LOCKS();
   1750  1.108     ozaki 	bridge_enqueue(sc, dst_if, m, 1);
   1751  1.114     ozaki 	RELEASE_GLOBAL_LOCKS();
   1752  1.108     ozaki out:
   1753  1.125     ozaki 	if (src_if != NULL)
   1754  1.125     ozaki 		m_put_rcvif_psref(src_if, &psref_src);
   1755  1.108     ozaki 	return;
   1756    1.1   thorpej }
   1757    1.1   thorpej 
   1758   1.82     ozaki static bool
   1759   1.82     ozaki bstp_state_before_learning(struct bridge_iflist *bif)
   1760   1.82     ozaki {
   1761   1.82     ozaki 	if (bif->bif_flags & IFBIF_STP) {
   1762   1.82     ozaki 		switch (bif->bif_state) {
   1763   1.82     ozaki 		case BSTP_IFSTATE_BLOCKING:
   1764   1.82     ozaki 		case BSTP_IFSTATE_LISTENING:
   1765   1.82     ozaki 		case BSTP_IFSTATE_DISABLED:
   1766   1.82     ozaki 			return true;
   1767   1.82     ozaki 		}
   1768   1.82     ozaki 	}
   1769   1.82     ozaki 	return false;
   1770   1.82     ozaki }
   1771   1.82     ozaki 
   1772   1.82     ozaki static bool
   1773   1.82     ozaki bridge_ourether(struct bridge_iflist *bif, struct ether_header *eh, int src)
   1774   1.82     ozaki {
   1775   1.82     ozaki 	uint8_t *ether = src ? eh->ether_shost : eh->ether_dhost;
   1776   1.82     ozaki 
   1777   1.82     ozaki 	if (memcmp(CLLADDR(bif->bif_ifp->if_sadl), ether, ETHER_ADDR_LEN) == 0
   1778   1.82     ozaki #if NCARP > 0
   1779   1.82     ozaki 	    || (bif->bif_ifp->if_carp &&
   1780   1.82     ozaki 	        carp_ourether(bif->bif_ifp->if_carp, eh, IFT_ETHER, src) != NULL)
   1781   1.82     ozaki #endif /* NCARP > 0 */
   1782   1.82     ozaki 	    )
   1783   1.82     ozaki 		return true;
   1784   1.82     ozaki 
   1785   1.82     ozaki 	return false;
   1786   1.82     ozaki }
   1787   1.82     ozaki 
   1788    1.1   thorpej /*
   1789    1.1   thorpej  * bridge_input:
   1790    1.1   thorpej  *
   1791    1.1   thorpej  *	Receive input from a member interface.  Queue the packet for
   1792    1.1   thorpej  *	bridging if it is not for us.
   1793    1.1   thorpej  */
   1794   1.81     ozaki static void
   1795    1.1   thorpej bridge_input(struct ifnet *ifp, struct mbuf *m)
   1796    1.1   thorpej {
   1797    1.1   thorpej 	struct bridge_softc *sc = ifp->if_bridge;
   1798    1.1   thorpej 	struct bridge_iflist *bif;
   1799    1.1   thorpej 	struct ether_header *eh;
   1800  1.115     ozaki 	struct psref psref;
   1801  1.135     ozaki 	int bound;
   1802  1.114     ozaki 	DECLARE_LOCK_VARIABLE;
   1803    1.1   thorpej 
   1804  1.106     ozaki 	KASSERT(!cpu_intr_p());
   1805  1.106     ozaki 
   1806   1.87     ozaki 	if (__predict_false(sc == NULL) ||
   1807   1.87     ozaki 	    (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   1808  1.114     ozaki 		ACQUIRE_GLOBAL_LOCKS();
   1809   1.81     ozaki 		ether_input(ifp, m);
   1810  1.114     ozaki 		RELEASE_GLOBAL_LOCKS();
   1811   1.81     ozaki 		return;
   1812   1.81     ozaki 	}
   1813    1.1   thorpej 
   1814  1.135     ozaki 	bound = curlwp_bind();
   1815  1.115     ozaki 	bif = bridge_lookup_member_if(sc, ifp, &psref);
   1816   1.81     ozaki 	if (bif == NULL) {
   1817  1.135     ozaki 		curlwp_bindx(bound);
   1818  1.114     ozaki 		ACQUIRE_GLOBAL_LOCKS();
   1819   1.81     ozaki 		ether_input(ifp, m);
   1820  1.114     ozaki 		RELEASE_GLOBAL_LOCKS();
   1821   1.81     ozaki 		return;
   1822   1.81     ozaki 	}
   1823    1.1   thorpej 
   1824    1.1   thorpej 	eh = mtod(m, struct ether_header *);
   1825    1.1   thorpej 
   1826   1.81     ozaki 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
   1827   1.81     ozaki 		if (memcmp(etherbroadcastaddr,
   1828   1.81     ozaki 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
   1829   1.81     ozaki 			m->m_flags |= M_BCAST;
   1830   1.81     ozaki 		else
   1831   1.81     ozaki 			m->m_flags |= M_MCAST;
   1832   1.81     ozaki 	}
   1833   1.81     ozaki 
   1834   1.83     ozaki 	/*
   1835   1.83     ozaki 	 * A 'fast' path for packets addressed to interfaces that are
   1836   1.83     ozaki 	 * part of this bridge.
   1837   1.83     ozaki 	 */
   1838   1.83     ozaki 	if (!(m->m_flags & (M_BCAST|M_MCAST)) &&
   1839   1.83     ozaki 	    !bstp_state_before_learning(bif)) {
   1840   1.83     ozaki 		struct bridge_iflist *_bif;
   1841   1.92     ozaki 		struct ifnet *_ifp = NULL;
   1842   1.95     ozaki 		int s;
   1843  1.115     ozaki 		struct psref _psref;
   1844   1.83     ozaki 
   1845   1.95     ozaki 		BRIDGE_PSZ_RENTER(s);
   1846  1.115     ozaki 		BRIDGE_IFLIST_READER_FOREACH(_bif, sc) {
   1847   1.83     ozaki 			/* It is destined for us. */
   1848   1.83     ozaki 			if (bridge_ourether(_bif, eh, 0)) {
   1849  1.115     ozaki 				bridge_acquire_member(sc, _bif, &_psref);
   1850   1.95     ozaki 				BRIDGE_PSZ_REXIT(s);
   1851   1.83     ozaki 				if (_bif->bif_flags & IFBIF_LEARNING)
   1852   1.83     ozaki 					(void) bridge_rtupdate(sc,
   1853   1.83     ozaki 					    eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
   1854  1.124     ozaki 				m_set_rcvif(m, _bif->bif_ifp);
   1855  1.124     ozaki 				_ifp = _bif->bif_ifp;
   1856  1.115     ozaki 				bridge_release_member(sc, _bif, &_psref);
   1857   1.95     ozaki 				goto out;
   1858   1.64       mrg 			}
   1859    1.1   thorpej 
   1860   1.83     ozaki 			/* We just received a packet that we sent out. */
   1861   1.92     ozaki 			if (bridge_ourether(_bif, eh, 1))
   1862   1.87     ozaki 				break;
   1863    1.1   thorpej 		}
   1864   1.95     ozaki 		BRIDGE_PSZ_REXIT(s);
   1865   1.95     ozaki out:
   1866   1.87     ozaki 
   1867   1.87     ozaki 		if (_bif != NULL) {
   1868  1.115     ozaki 			bridge_release_member(sc, bif, &psref);
   1869  1.135     ozaki 			curlwp_bindx(bound);
   1870  1.100     ozaki 			if (_ifp != NULL) {
   1871  1.100     ozaki 				m->m_flags &= ~M_PROMISC;
   1872  1.114     ozaki 				ACQUIRE_GLOBAL_LOCKS();
   1873   1.92     ozaki 				ether_input(_ifp, m);
   1874  1.114     ozaki 				RELEASE_GLOBAL_LOCKS();
   1875  1.100     ozaki 			} else
   1876   1.92     ozaki 				m_freem(m);
   1877   1.87     ozaki 			return;
   1878   1.87     ozaki 		}
   1879   1.83     ozaki 	}
   1880    1.1   thorpej 
   1881   1.83     ozaki 	/* Tap off 802.1D packets; they do not get forwarded. */
   1882   1.83     ozaki 	if (bif->bif_flags & IFBIF_STP &&
   1883   1.83     ozaki 	    memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) {
   1884   1.83     ozaki 		bstp_input(sc, bif, m);
   1885  1.115     ozaki 		bridge_release_member(sc, bif, &psref);
   1886  1.135     ozaki 		curlwp_bindx(bound);
   1887   1.81     ozaki 		return;
   1888    1.1   thorpej 	}
   1889    1.1   thorpej 
   1890   1.83     ozaki 	/*
   1891   1.83     ozaki 	 * A normal switch would discard the packet here, but that's not what
   1892   1.83     ozaki 	 * we've done historically. This also prevents some obnoxious behaviour.
   1893   1.83     ozaki 	 */
   1894   1.82     ozaki 	if (bstp_state_before_learning(bif)) {
   1895  1.115     ozaki 		bridge_release_member(sc, bif, &psref);
   1896  1.135     ozaki 		curlwp_bindx(bound);
   1897  1.114     ozaki 		ACQUIRE_GLOBAL_LOCKS();
   1898   1.82     ozaki 		ether_input(ifp, m);
   1899  1.114     ozaki 		RELEASE_GLOBAL_LOCKS();
   1900   1.82     ozaki 		return;
   1901    1.1   thorpej 	}
   1902    1.1   thorpej 
   1903  1.115     ozaki 	bridge_release_member(sc, bif, &psref);
   1904   1.87     ozaki 
   1905  1.108     ozaki 	bridge_forward(sc, m);
   1906  1.135     ozaki 
   1907  1.135     ozaki 	curlwp_bindx(bound);
   1908    1.1   thorpej }
   1909    1.1   thorpej 
   1910    1.1   thorpej /*
   1911    1.1   thorpej  * bridge_broadcast:
   1912    1.1   thorpej  *
   1913    1.1   thorpej  *	Send a frame to all interfaces that are members of
   1914    1.1   thorpej  *	the bridge, except for the one on which the packet
   1915    1.1   thorpej  *	arrived.
   1916    1.1   thorpej  */
   1917   1.33   thorpej static void
   1918    1.1   thorpej bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
   1919    1.1   thorpej     struct mbuf *m)
   1920    1.1   thorpej {
   1921    1.1   thorpej 	struct bridge_iflist *bif;
   1922    1.1   thorpej 	struct mbuf *mc;
   1923    1.1   thorpej 	struct ifnet *dst_if;
   1924  1.100     ozaki 	bool bmcast;
   1925   1.95     ozaki 	int s;
   1926  1.114     ozaki 	DECLARE_LOCK_VARIABLE;
   1927   1.83     ozaki 
   1928  1.100     ozaki 	bmcast = m->m_flags & (M_BCAST|M_MCAST);
   1929    1.1   thorpej 
   1930   1.95     ozaki 	BRIDGE_PSZ_RENTER(s);
   1931  1.115     ozaki 	BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
   1932  1.115     ozaki 		struct psref psref;
   1933  1.115     ozaki 
   1934  1.115     ozaki 		bridge_acquire_member(sc, bif, &psref);
   1935   1.95     ozaki 		BRIDGE_PSZ_REXIT(s);
   1936   1.87     ozaki 
   1937    1.1   thorpej 		dst_if = bif->bif_ifp;
   1938    1.1   thorpej 
   1939    1.1   thorpej 		if (bif->bif_flags & IFBIF_STP) {
   1940    1.1   thorpej 			switch (bif->bif_state) {
   1941    1.1   thorpej 			case BSTP_IFSTATE_BLOCKING:
   1942    1.1   thorpej 			case BSTP_IFSTATE_DISABLED:
   1943   1.95     ozaki 				goto next;
   1944    1.1   thorpej 			}
   1945    1.1   thorpej 		}
   1946    1.1   thorpej 
   1947   1.83     ozaki 		if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && !bmcast)
   1948   1.95     ozaki 			goto next;
   1949    1.1   thorpej 
   1950    1.1   thorpej 		if ((dst_if->if_flags & IFF_RUNNING) == 0)
   1951   1.95     ozaki 			goto next;
   1952    1.1   thorpej 
   1953  1.100     ozaki 		if (dst_if != src_if) {
   1954    1.1   thorpej 			mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
   1955    1.1   thorpej 			if (mc == NULL) {
   1956    1.1   thorpej 				sc->sc_if.if_oerrors++;
   1957   1.95     ozaki 				goto next;
   1958    1.1   thorpej 			}
   1959  1.114     ozaki 			ACQUIRE_GLOBAL_LOCKS();
   1960  1.100     ozaki 			bridge_enqueue(sc, dst_if, mc, 1);
   1961  1.114     ozaki 			RELEASE_GLOBAL_LOCKS();
   1962    1.1   thorpej 		}
   1963    1.1   thorpej 
   1964  1.100     ozaki 		if (bmcast) {
   1965  1.100     ozaki 			mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
   1966  1.100     ozaki 			if (mc == NULL) {
   1967  1.100     ozaki 				sc->sc_if.if_oerrors++;
   1968  1.100     ozaki 				goto next;
   1969  1.100     ozaki 			}
   1970  1.100     ozaki 
   1971  1.124     ozaki 			m_set_rcvif(mc, dst_if);
   1972  1.100     ozaki 			mc->m_flags &= ~M_PROMISC;
   1973  1.114     ozaki 
   1974  1.114     ozaki 			ACQUIRE_GLOBAL_LOCKS();
   1975  1.100     ozaki 			ether_input(dst_if, mc);
   1976  1.114     ozaki 			RELEASE_GLOBAL_LOCKS();
   1977  1.100     ozaki 		}
   1978   1.95     ozaki next:
   1979   1.95     ozaki 		BRIDGE_PSZ_RENTER(s);
   1980  1.115     ozaki 		bridge_release_member(sc, bif, &psref);
   1981    1.1   thorpej 	}
   1982   1.95     ozaki 	BRIDGE_PSZ_REXIT(s);
   1983   1.83     ozaki 
   1984  1.100     ozaki 	m_freem(m);
   1985    1.1   thorpej }
   1986    1.1   thorpej 
   1987   1.97     ozaki static int
   1988   1.97     ozaki bridge_rtalloc(struct bridge_softc *sc, const uint8_t *dst,
   1989   1.97     ozaki     struct bridge_rtnode **brtp)
   1990   1.97     ozaki {
   1991   1.97     ozaki 	struct bridge_rtnode *brt;
   1992   1.97     ozaki 	int error;
   1993   1.97     ozaki 
   1994   1.97     ozaki 	if (sc->sc_brtcnt >= sc->sc_brtmax)
   1995   1.97     ozaki 		return ENOSPC;
   1996   1.97     ozaki 
   1997   1.97     ozaki 	/*
   1998   1.97     ozaki 	 * Allocate a new bridge forwarding node, and
   1999   1.97     ozaki 	 * initialize the expiration time and Ethernet
   2000   1.97     ozaki 	 * address.
   2001   1.97     ozaki 	 */
   2002   1.97     ozaki 	brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
   2003   1.97     ozaki 	if (brt == NULL)
   2004   1.97     ozaki 		return ENOMEM;
   2005   1.97     ozaki 
   2006   1.97     ozaki 	memset(brt, 0, sizeof(*brt));
   2007   1.97     ozaki 	brt->brt_expire = time_uptime + sc->sc_brttimeout;
   2008   1.97     ozaki 	brt->brt_flags = IFBAF_DYNAMIC;
   2009   1.97     ozaki 	memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
   2010   1.97     ozaki 
   2011  1.108     ozaki 	BRIDGE_RT_LOCK(sc);
   2012   1.97     ozaki 	error = bridge_rtnode_insert(sc, brt);
   2013  1.108     ozaki 	BRIDGE_RT_UNLOCK(sc);
   2014   1.97     ozaki 
   2015   1.97     ozaki 	if (error != 0) {
   2016   1.97     ozaki 		pool_put(&bridge_rtnode_pool, brt);
   2017   1.97     ozaki 		return error;
   2018   1.97     ozaki 	}
   2019   1.97     ozaki 
   2020   1.97     ozaki 	*brtp = brt;
   2021   1.97     ozaki 	return 0;
   2022   1.97     ozaki }
   2023   1.97     ozaki 
   2024    1.1   thorpej /*
   2025    1.1   thorpej  * bridge_rtupdate:
   2026    1.1   thorpej  *
   2027    1.1   thorpej  *	Add a bridge routing entry.
   2028    1.1   thorpej  */
   2029   1.33   thorpej static int
   2030    1.1   thorpej bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
   2031    1.1   thorpej     struct ifnet *dst_if, int setflags, uint8_t flags)
   2032    1.1   thorpej {
   2033    1.1   thorpej 	struct bridge_rtnode *brt;
   2034   1.97     ozaki 	int s;
   2035    1.1   thorpej 
   2036   1.97     ozaki again:
   2037    1.1   thorpej 	/*
   2038    1.1   thorpej 	 * A route for this destination might already exist.  If so,
   2039    1.1   thorpej 	 * update it, otherwise create a new one.
   2040    1.1   thorpej 	 */
   2041   1.97     ozaki 	BRIDGE_RT_RENTER(s);
   2042   1.97     ozaki 	brt = bridge_rtnode_lookup(sc, dst);
   2043    1.1   thorpej 
   2044   1.97     ozaki 	if (brt != NULL) {
   2045   1.97     ozaki 		brt->brt_ifp = dst_if;
   2046   1.97     ozaki 		if (setflags) {
   2047   1.97     ozaki 			brt->brt_flags = flags;
   2048   1.97     ozaki 			if (flags & IFBAF_STATIC)
   2049   1.97     ozaki 				brt->brt_expire = 0;
   2050   1.97     ozaki 			else
   2051   1.97     ozaki 				brt->brt_expire = time_uptime + sc->sc_brttimeout;
   2052   1.97     ozaki 		} else {
   2053   1.97     ozaki 			if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
   2054   1.97     ozaki 				brt->brt_expire = time_uptime + sc->sc_brttimeout;
   2055   1.87     ozaki 		}
   2056   1.97     ozaki 	}
   2057   1.97     ozaki 	BRIDGE_RT_REXIT(s);
   2058    1.1   thorpej 
   2059   1.97     ozaki 	if (brt == NULL) {
   2060   1.97     ozaki 		int r;
   2061    1.1   thorpej 
   2062   1.97     ozaki 		r = bridge_rtalloc(sc, dst, &brt);
   2063   1.97     ozaki 		if (r != 0)
   2064   1.97     ozaki 			return r;
   2065   1.97     ozaki 		goto again;
   2066    1.1   thorpej 	}
   2067    1.1   thorpej 
   2068   1.97     ozaki 	return 0;
   2069    1.1   thorpej }
   2070    1.1   thorpej 
   2071    1.1   thorpej /*
   2072    1.1   thorpej  * bridge_rtlookup:
   2073    1.1   thorpej  *
   2074    1.1   thorpej  *	Lookup the destination interface for an address.
   2075    1.1   thorpej  */
   2076   1.33   thorpej static struct ifnet *
   2077    1.1   thorpej bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
   2078    1.1   thorpej {
   2079    1.1   thorpej 	struct bridge_rtnode *brt;
   2080   1.87     ozaki 	struct ifnet *ifs = NULL;
   2081   1.97     ozaki 	int s;
   2082   1.87     ozaki 
   2083   1.97     ozaki 	BRIDGE_RT_RENTER(s);
   2084   1.87     ozaki 	brt = bridge_rtnode_lookup(sc, addr);
   2085   1.87     ozaki 	if (brt != NULL)
   2086   1.87     ozaki 		ifs = brt->brt_ifp;
   2087   1.97     ozaki 	BRIDGE_RT_REXIT(s);
   2088   1.97     ozaki 
   2089   1.97     ozaki 	return ifs;
   2090   1.97     ozaki }
   2091   1.97     ozaki 
   2092   1.97     ozaki typedef bool (*bridge_iterate_cb_t)
   2093   1.97     ozaki     (struct bridge_softc *, struct bridge_rtnode *, bool *, void *);
   2094   1.97     ozaki 
   2095   1.97     ozaki /*
   2096   1.97     ozaki  * bridge_rtlist_iterate_remove:
   2097   1.97     ozaki  *
   2098   1.97     ozaki  *	It iterates on sc->sc_rtlist and removes rtnodes of it which func
   2099   1.97     ozaki  *	callback judges to remove. Removals of rtnodes are done in a manner
   2100   1.97     ozaki  *	of pserialize. To this end, all kmem_* operations are placed out of
   2101   1.97     ozaki  *	mutexes.
   2102   1.97     ozaki  */
   2103   1.97     ozaki static void
   2104   1.97     ozaki bridge_rtlist_iterate_remove(struct bridge_softc *sc, bridge_iterate_cb_t func, void *arg)
   2105   1.97     ozaki {
   2106   1.97     ozaki 	struct bridge_rtnode *brt, *nbrt;
   2107   1.97     ozaki 	struct bridge_rtnode **brt_list;
   2108   1.97     ozaki 	int i, count;
   2109    1.1   thorpej 
   2110   1.97     ozaki retry:
   2111   1.97     ozaki 	count = sc->sc_brtcnt;
   2112   1.97     ozaki 	if (count == 0)
   2113   1.97     ozaki 		return;
   2114  1.104      maxv 	brt_list = kmem_alloc(sizeof(*brt_list) * count, KM_SLEEP);
   2115   1.97     ozaki 
   2116   1.97     ozaki 	BRIDGE_RT_LOCK(sc);
   2117   1.97     ozaki 	if (__predict_false(sc->sc_brtcnt > count)) {
   2118   1.97     ozaki 		/* The rtnodes increased, we need more memory */
   2119   1.97     ozaki 		BRIDGE_RT_UNLOCK(sc);
   2120   1.97     ozaki 		kmem_free(brt_list, sizeof(*brt_list) * count);
   2121   1.97     ozaki 		goto retry;
   2122   1.97     ozaki 	}
   2123   1.97     ozaki 
   2124   1.97     ozaki 	i = 0;
   2125   1.97     ozaki 	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
   2126   1.97     ozaki 		bool need_break = false;
   2127   1.97     ozaki 		if (func(sc, brt, &need_break, arg)) {
   2128   1.97     ozaki 			bridge_rtnode_remove(sc, brt);
   2129   1.97     ozaki 			brt_list[i++] = brt;
   2130   1.97     ozaki 		}
   2131   1.97     ozaki 		if (need_break)
   2132   1.97     ozaki 			break;
   2133   1.97     ozaki 	}
   2134   1.97     ozaki 
   2135   1.97     ozaki 	if (i > 0)
   2136   1.97     ozaki 		BRIDGE_RT_PSZ_PERFORM(sc);
   2137   1.97     ozaki 	BRIDGE_RT_UNLOCK(sc);
   2138   1.97     ozaki 
   2139   1.97     ozaki 	while (--i >= 0)
   2140   1.97     ozaki 		bridge_rtnode_destroy(brt_list[i]);
   2141   1.97     ozaki 
   2142   1.97     ozaki 	kmem_free(brt_list, sizeof(*brt_list) * count);
   2143   1.97     ozaki }
   2144   1.97     ozaki 
   2145   1.97     ozaki static bool
   2146   1.97     ozaki bridge_rttrim0_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
   2147   1.97     ozaki     bool *need_break, void *arg)
   2148   1.97     ozaki {
   2149   1.97     ozaki 	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
   2150   1.97     ozaki 		/* Take into account of the subsequent removal */
   2151   1.97     ozaki 		if ((sc->sc_brtcnt - 1) <= sc->sc_brtmax)
   2152   1.97     ozaki 			*need_break = true;
   2153   1.97     ozaki 		return true;
   2154   1.97     ozaki 	} else
   2155   1.97     ozaki 		return false;
   2156   1.97     ozaki }
   2157    1.1   thorpej 
   2158   1.97     ozaki static void
   2159   1.97     ozaki bridge_rttrim0(struct bridge_softc *sc)
   2160   1.97     ozaki {
   2161   1.97     ozaki 	bridge_rtlist_iterate_remove(sc, bridge_rttrim0_cb, NULL);
   2162    1.1   thorpej }
   2163    1.1   thorpej 
   2164    1.1   thorpej /*
   2165    1.1   thorpej  * bridge_rttrim:
   2166    1.1   thorpej  *
   2167    1.1   thorpej  *	Trim the routine table so that we have a number
   2168    1.1   thorpej  *	of routing entries less than or equal to the
   2169    1.1   thorpej  *	maximum number.
   2170    1.1   thorpej  */
   2171   1.33   thorpej static void
   2172    1.1   thorpej bridge_rttrim(struct bridge_softc *sc)
   2173    1.1   thorpej {
   2174   1.87     ozaki 
   2175    1.1   thorpej 	/* Make sure we actually need to do this. */
   2176    1.1   thorpej 	if (sc->sc_brtcnt <= sc->sc_brtmax)
   2177   1.97     ozaki 		return;
   2178    1.1   thorpej 
   2179    1.1   thorpej 	/* Force an aging cycle; this might trim enough addresses. */
   2180    1.1   thorpej 	bridge_rtage(sc);
   2181    1.1   thorpej 	if (sc->sc_brtcnt <= sc->sc_brtmax)
   2182   1.97     ozaki 		return;
   2183    1.1   thorpej 
   2184   1.97     ozaki 	bridge_rttrim0(sc);
   2185   1.87     ozaki 
   2186   1.87     ozaki 	return;
   2187    1.1   thorpej }
   2188    1.1   thorpej 
   2189    1.1   thorpej /*
   2190    1.1   thorpej  * bridge_timer:
   2191    1.1   thorpej  *
   2192    1.1   thorpej  *	Aging timer for the bridge.
   2193    1.1   thorpej  */
   2194   1.33   thorpej static void
   2195    1.1   thorpej bridge_timer(void *arg)
   2196    1.1   thorpej {
   2197    1.1   thorpej 	struct bridge_softc *sc = arg;
   2198    1.1   thorpej 
   2199  1.107     ozaki 	workqueue_enqueue(sc->sc_rtage_wq, &sc->sc_rtage_wk, NULL);
   2200   1.97     ozaki }
   2201   1.97     ozaki 
   2202   1.97     ozaki static void
   2203   1.97     ozaki bridge_rtage_work(struct work *wk, void *arg)
   2204   1.97     ozaki {
   2205   1.97     ozaki 	struct bridge_softc *sc = arg;
   2206   1.97     ozaki 
   2207  1.107     ozaki 	KASSERT(wk == &sc->sc_rtage_wk);
   2208   1.87     ozaki 
   2209    1.1   thorpej 	bridge_rtage(sc);
   2210    1.1   thorpej 
   2211    1.1   thorpej 	if (sc->sc_if.if_flags & IFF_RUNNING)
   2212    1.1   thorpej 		callout_reset(&sc->sc_brcallout,
   2213    1.1   thorpej 		    bridge_rtable_prune_period * hz, bridge_timer, sc);
   2214   1.97     ozaki }
   2215   1.87     ozaki 
   2216   1.97     ozaki static bool
   2217   1.97     ozaki bridge_rtage_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
   2218   1.97     ozaki     bool *need_break, void *arg)
   2219   1.97     ozaki {
   2220   1.97     ozaki 	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
   2221   1.97     ozaki 	    time_uptime >= brt->brt_expire)
   2222   1.97     ozaki 		return true;
   2223   1.97     ozaki 	else
   2224   1.97     ozaki 		return false;
   2225    1.1   thorpej }
   2226    1.1   thorpej 
   2227    1.1   thorpej /*
   2228    1.1   thorpej  * bridge_rtage:
   2229    1.1   thorpej  *
   2230    1.1   thorpej  *	Perform an aging cycle.
   2231    1.1   thorpej  */
   2232   1.33   thorpej static void
   2233    1.1   thorpej bridge_rtage(struct bridge_softc *sc)
   2234    1.1   thorpej {
   2235   1.97     ozaki 	bridge_rtlist_iterate_remove(sc, bridge_rtage_cb, NULL);
   2236   1.97     ozaki }
   2237   1.97     ozaki 
   2238    1.1   thorpej 
   2239   1.97     ozaki static bool
   2240   1.97     ozaki bridge_rtflush_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
   2241   1.97     ozaki     bool *need_break, void *arg)
   2242   1.97     ozaki {
   2243   1.97     ozaki 	int full = *(int*)arg;
   2244   1.87     ozaki 
   2245   1.97     ozaki 	if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
   2246   1.97     ozaki 		return true;
   2247   1.97     ozaki 	else
   2248   1.97     ozaki 		return false;
   2249    1.1   thorpej }
   2250    1.1   thorpej 
   2251    1.1   thorpej /*
   2252    1.1   thorpej  * bridge_rtflush:
   2253    1.1   thorpej  *
   2254    1.1   thorpej  *	Remove all dynamic addresses from the bridge.
   2255    1.1   thorpej  */
   2256   1.33   thorpej static void
   2257    1.1   thorpej bridge_rtflush(struct bridge_softc *sc, int full)
   2258    1.1   thorpej {
   2259   1.97     ozaki 	bridge_rtlist_iterate_remove(sc, bridge_rtflush_cb, &full);
   2260    1.1   thorpej }
   2261    1.1   thorpej 
   2262    1.1   thorpej /*
   2263    1.1   thorpej  * bridge_rtdaddr:
   2264    1.1   thorpej  *
   2265    1.1   thorpej  *	Remove an address from the table.
   2266    1.1   thorpej  */
   2267   1.33   thorpej static int
   2268    1.1   thorpej bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
   2269    1.1   thorpej {
   2270    1.1   thorpej 	struct bridge_rtnode *brt;
   2271    1.1   thorpej 
   2272   1.97     ozaki 	BRIDGE_RT_LOCK(sc);
   2273   1.87     ozaki 	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) {
   2274   1.97     ozaki 		BRIDGE_RT_UNLOCK(sc);
   2275   1.97     ozaki 		return ENOENT;
   2276   1.87     ozaki 	}
   2277   1.97     ozaki 	bridge_rtnode_remove(sc, brt);
   2278   1.97     ozaki 	BRIDGE_RT_PSZ_PERFORM(sc);
   2279   1.97     ozaki 	BRIDGE_RT_UNLOCK(sc);
   2280    1.1   thorpej 
   2281   1.97     ozaki 	bridge_rtnode_destroy(brt);
   2282   1.87     ozaki 
   2283   1.97     ozaki 	return 0;
   2284    1.1   thorpej }
   2285    1.1   thorpej 
   2286    1.1   thorpej /*
   2287    1.1   thorpej  * bridge_rtdelete:
   2288    1.1   thorpej  *
   2289    1.1   thorpej  *	Delete routes to a speicifc member interface.
   2290    1.1   thorpej  */
   2291   1.33   thorpej static void
   2292    1.1   thorpej bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
   2293    1.1   thorpej {
   2294  1.110     ozaki 	struct bridge_rtnode *brt;
   2295    1.1   thorpej 
   2296   1.97     ozaki 	BRIDGE_RT_LOCK(sc);
   2297  1.110     ozaki 	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
   2298    1.1   thorpej 		if (brt->brt_ifp == ifp)
   2299   1.97     ozaki 			break;
   2300    1.1   thorpej 	}
   2301   1.97     ozaki 	if (brt == NULL) {
   2302   1.97     ozaki 		BRIDGE_RT_UNLOCK(sc);
   2303   1.97     ozaki 		return;
   2304   1.97     ozaki 	}
   2305   1.97     ozaki 	bridge_rtnode_remove(sc, brt);
   2306   1.97     ozaki 	BRIDGE_RT_PSZ_PERFORM(sc);
   2307   1.97     ozaki 	BRIDGE_RT_UNLOCK(sc);
   2308   1.87     ozaki 
   2309   1.97     ozaki 	bridge_rtnode_destroy(brt);
   2310    1.1   thorpej }
   2311    1.1   thorpej 
   2312    1.1   thorpej /*
   2313    1.1   thorpej  * bridge_rtable_init:
   2314    1.1   thorpej  *
   2315    1.1   thorpej  *	Initialize the route table for this bridge.
   2316    1.1   thorpej  */
   2317   1.93     ozaki static void
   2318    1.1   thorpej bridge_rtable_init(struct bridge_softc *sc)
   2319    1.1   thorpej {
   2320    1.1   thorpej 	int i;
   2321    1.1   thorpej 
   2322   1.93     ozaki 	sc->sc_rthash = kmem_alloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
   2323   1.93     ozaki 	    KM_SLEEP);
   2324    1.1   thorpej 
   2325    1.1   thorpej 	for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
   2326    1.1   thorpej 		LIST_INIT(&sc->sc_rthash[i]);
   2327    1.1   thorpej 
   2328   1.74       tls 	sc->sc_rthash_key = cprng_fast32();
   2329    1.1   thorpej 
   2330    1.1   thorpej 	LIST_INIT(&sc->sc_rtlist);
   2331    1.1   thorpej 
   2332   1.97     ozaki 	sc->sc_rtlist_psz = pserialize_create();
   2333   1.97     ozaki 	sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
   2334    1.1   thorpej }
   2335    1.1   thorpej 
   2336    1.1   thorpej /*
   2337    1.1   thorpej  * bridge_rtable_fini:
   2338    1.1   thorpej  *
   2339    1.1   thorpej  *	Deconstruct the route table for this bridge.
   2340    1.1   thorpej  */
   2341   1.33   thorpej static void
   2342    1.1   thorpej bridge_rtable_fini(struct bridge_softc *sc)
   2343    1.1   thorpej {
   2344    1.1   thorpej 
   2345   1.93     ozaki 	kmem_free(sc->sc_rthash, sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE);
   2346   1.87     ozaki 	if (sc->sc_rtlist_lock)
   2347   1.87     ozaki 		mutex_obj_free(sc->sc_rtlist_lock);
   2348   1.97     ozaki 	if (sc->sc_rtlist_psz)
   2349   1.97     ozaki 		pserialize_destroy(sc->sc_rtlist_psz);
   2350    1.1   thorpej }
   2351    1.1   thorpej 
   2352    1.1   thorpej /*
   2353    1.1   thorpej  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
   2354    1.1   thorpej  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
   2355    1.1   thorpej  */
   2356    1.1   thorpej #define	mix(a, b, c)							\
   2357    1.1   thorpej do {									\
   2358    1.1   thorpej 	a -= b; a -= c; a ^= (c >> 13);					\
   2359    1.1   thorpej 	b -= c; b -= a; b ^= (a << 8);					\
   2360    1.1   thorpej 	c -= a; c -= b; c ^= (b >> 13);					\
   2361    1.1   thorpej 	a -= b; a -= c; a ^= (c >> 12);					\
   2362    1.1   thorpej 	b -= c; b -= a; b ^= (a << 16);					\
   2363    1.1   thorpej 	c -= a; c -= b; c ^= (b >> 5);					\
   2364    1.1   thorpej 	a -= b; a -= c; a ^= (c >> 3);					\
   2365    1.1   thorpej 	b -= c; b -= a; b ^= (a << 10);					\
   2366    1.1   thorpej 	c -= a; c -= b; c ^= (b >> 15);					\
   2367    1.1   thorpej } while (/*CONSTCOND*/0)
   2368    1.1   thorpej 
   2369   1.34     perry static inline uint32_t
   2370    1.1   thorpej bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
   2371    1.1   thorpej {
   2372    1.1   thorpej 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
   2373    1.1   thorpej 
   2374    1.1   thorpej 	b += addr[5] << 8;
   2375    1.1   thorpej 	b += addr[4];
   2376    1.1   thorpej 	a += addr[3] << 24;
   2377    1.1   thorpej 	a += addr[2] << 16;
   2378    1.1   thorpej 	a += addr[1] << 8;
   2379    1.1   thorpej 	a += addr[0];
   2380    1.1   thorpej 
   2381    1.1   thorpej 	mix(a, b, c);
   2382    1.1   thorpej 
   2383    1.1   thorpej 	return (c & BRIDGE_RTHASH_MASK);
   2384    1.1   thorpej }
   2385    1.1   thorpej 
   2386    1.1   thorpej #undef mix
   2387    1.1   thorpej 
   2388    1.1   thorpej /*
   2389    1.1   thorpej  * bridge_rtnode_lookup:
   2390    1.1   thorpej  *
   2391    1.1   thorpej  *	Look up a bridge route node for the specified destination.
   2392    1.1   thorpej  */
   2393   1.33   thorpej static struct bridge_rtnode *
   2394    1.1   thorpej bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
   2395    1.1   thorpej {
   2396    1.1   thorpej 	struct bridge_rtnode *brt;
   2397    1.1   thorpej 	uint32_t hash;
   2398    1.1   thorpej 	int dir;
   2399    1.1   thorpej 
   2400    1.1   thorpej 	hash = bridge_rthash(sc, addr);
   2401    1.1   thorpej 	LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
   2402    1.1   thorpej 		dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
   2403    1.1   thorpej 		if (dir == 0)
   2404  1.136   msaitoh 			return brt;
   2405    1.1   thorpej 		if (dir > 0)
   2406  1.136   msaitoh 			return NULL;
   2407    1.1   thorpej 	}
   2408    1.1   thorpej 
   2409  1.136   msaitoh 	return NULL;
   2410    1.1   thorpej }
   2411    1.1   thorpej 
   2412    1.1   thorpej /*
   2413    1.1   thorpej  * bridge_rtnode_insert:
   2414    1.1   thorpej  *
   2415    1.1   thorpej  *	Insert the specified bridge node into the route table.  We
   2416    1.1   thorpej  *	assume the entry is not already in the table.
   2417    1.1   thorpej  */
   2418   1.33   thorpej static int
   2419    1.1   thorpej bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
   2420    1.1   thorpej {
   2421    1.1   thorpej 	struct bridge_rtnode *lbrt;
   2422    1.1   thorpej 	uint32_t hash;
   2423    1.1   thorpej 	int dir;
   2424    1.1   thorpej 
   2425  1.108     ozaki 	KASSERT(BRIDGE_RT_LOCKED(sc));
   2426   1.87     ozaki 
   2427    1.1   thorpej 	hash = bridge_rthash(sc, brt->brt_addr);
   2428    1.1   thorpej 
   2429    1.1   thorpej 	lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
   2430    1.1   thorpej 	if (lbrt == NULL) {
   2431    1.1   thorpej 		LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
   2432    1.1   thorpej 		goto out;
   2433    1.1   thorpej 	}
   2434    1.1   thorpej 
   2435    1.1   thorpej 	do {
   2436    1.1   thorpej 		dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
   2437    1.1   thorpej 		if (dir == 0)
   2438  1.136   msaitoh 			return EEXIST;
   2439    1.1   thorpej 		if (dir > 0) {
   2440    1.1   thorpej 			LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
   2441    1.1   thorpej 			goto out;
   2442    1.1   thorpej 		}
   2443    1.1   thorpej 		if (LIST_NEXT(lbrt, brt_hash) == NULL) {
   2444    1.1   thorpej 			LIST_INSERT_AFTER(lbrt, brt, brt_hash);
   2445    1.1   thorpej 			goto out;
   2446    1.1   thorpej 		}
   2447    1.1   thorpej 		lbrt = LIST_NEXT(lbrt, brt_hash);
   2448    1.1   thorpej 	} while (lbrt != NULL);
   2449    1.1   thorpej 
   2450    1.1   thorpej #ifdef DIAGNOSTIC
   2451    1.1   thorpej 	panic("bridge_rtnode_insert: impossible");
   2452    1.1   thorpej #endif
   2453    1.1   thorpej 
   2454    1.1   thorpej  out:
   2455    1.1   thorpej 	LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
   2456    1.1   thorpej 	sc->sc_brtcnt++;
   2457    1.1   thorpej 
   2458  1.136   msaitoh 	return 0;
   2459    1.1   thorpej }
   2460    1.1   thorpej 
   2461    1.1   thorpej /*
   2462   1.97     ozaki  * bridge_rtnode_remove:
   2463    1.1   thorpej  *
   2464   1.97     ozaki  *	Remove a bridge rtnode from the rthash and the rtlist of a bridge.
   2465    1.1   thorpej  */
   2466   1.33   thorpej static void
   2467   1.97     ozaki bridge_rtnode_remove(struct bridge_softc *sc, struct bridge_rtnode *brt)
   2468    1.1   thorpej {
   2469   1.87     ozaki 
   2470  1.108     ozaki 	KASSERT(BRIDGE_RT_LOCKED(sc));
   2471    1.1   thorpej 
   2472    1.1   thorpej 	LIST_REMOVE(brt, brt_hash);
   2473    1.1   thorpej 	LIST_REMOVE(brt, brt_list);
   2474    1.1   thorpej 	sc->sc_brtcnt--;
   2475   1.97     ozaki }
   2476   1.97     ozaki 
   2477   1.97     ozaki /*
   2478   1.97     ozaki  * bridge_rtnode_destroy:
   2479   1.97     ozaki  *
   2480   1.97     ozaki  *	Destroy a bridge rtnode.
   2481   1.97     ozaki  */
   2482   1.97     ozaki static void
   2483   1.97     ozaki bridge_rtnode_destroy(struct bridge_rtnode *brt)
   2484   1.97     ozaki {
   2485   1.97     ozaki 
   2486    1.1   thorpej 	pool_put(&bridge_rtnode_pool, brt);
   2487    1.1   thorpej }
   2488    1.9  perseant 
   2489   1.77     rmind #if defined(BRIDGE_IPF)
   2490   1.77     rmind extern pfil_head_t *inet_pfil_hook;                 /* XXX */
   2491   1.77     rmind extern pfil_head_t *inet6_pfil_hook;                /* XXX */
   2492    1.9  perseant 
   2493    1.9  perseant /*
   2494    1.9  perseant  * Send bridge packets through IPF if they are one of the types IPF can deal
   2495    1.9  perseant  * with, or if they are ARP or REVARP.  (IPF will pass ARP and REVARP without
   2496    1.9  perseant  * question.)
   2497    1.9  perseant  */
   2498   1.33   thorpej static int
   2499   1.45  christos bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
   2500    1.9  perseant {
   2501   1.22       jdc 	int snap, error;
   2502   1.22       jdc 	struct ether_header *eh1, eh2;
   2503   1.31       jdc 	struct llc llc1;
   2504   1.56      matt 	uint16_t ether_type;
   2505    1.9  perseant 
   2506    1.9  perseant 	snap = 0;
   2507   1.22       jdc 	error = -1;	/* Default error if not error == 0 */
   2508   1.22       jdc 	eh1 = mtod(*mp, struct ether_header *);
   2509   1.22       jdc 	ether_type = ntohs(eh1->ether_type);
   2510    1.9  perseant 
   2511    1.9  perseant 	/*
   2512    1.9  perseant 	 * Check for SNAP/LLC.
   2513    1.9  perseant 	 */
   2514  1.130     ozaki 	if (ether_type < ETHERMTU) {
   2515  1.130     ozaki 		struct llc *llc2 = (struct llc *)(eh1 + 1);
   2516   1.29     perry 
   2517  1.130     ozaki 		if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
   2518  1.130     ozaki 		    llc2->llc_dsap == LLC_SNAP_LSAP &&
   2519  1.130     ozaki 		    llc2->llc_ssap == LLC_SNAP_LSAP &&
   2520  1.130     ozaki 		    llc2->llc_control == LLC_UI) {
   2521  1.130     ozaki 			ether_type = htons(llc2->llc_un.type_snap.ether_type);
   2522    1.9  perseant 			snap = 1;
   2523  1.130     ozaki 		}
   2524  1.130     ozaki 	}
   2525    1.9  perseant 
   2526    1.9  perseant 	/*
   2527    1.9  perseant 	 * If we're trying to filter bridge traffic, don't look at anything
   2528    1.9  perseant 	 * other than IP and ARP traffic.  If the filter doesn't understand
   2529    1.9  perseant 	 * IPv6, don't allow IPv6 through the bridge either.  This is lame
   2530    1.9  perseant 	 * since if we really wanted, say, an AppleTalk filter, we are hosed,
   2531    1.9  perseant 	 * but of course we don't have an AppleTalk filter to begin with.
   2532    1.9  perseant 	 * (Note that since IPF doesn't understand ARP it will pass *ALL*
   2533    1.9  perseant 	 * ARP traffic.)
   2534    1.9  perseant 	 */
   2535    1.9  perseant 	switch (ether_type) {
   2536    1.9  perseant 		case ETHERTYPE_ARP:
   2537    1.9  perseant 		case ETHERTYPE_REVARP:
   2538    1.9  perseant 			return 0; /* Automatically pass */
   2539    1.9  perseant 		case ETHERTYPE_IP:
   2540    1.9  perseant # ifdef INET6
   2541    1.9  perseant 		case ETHERTYPE_IPV6:
   2542    1.9  perseant # endif /* INET6 */
   2543    1.9  perseant 			break;
   2544    1.9  perseant 		default:
   2545    1.9  perseant 			goto bad;
   2546    1.9  perseant 	}
   2547    1.9  perseant 
   2548   1.22       jdc 	/* Strip off the Ethernet header and keep a copy. */
   2549   1.50  christos 	m_copydata(*mp, 0, ETHER_HDR_LEN, (void *) &eh2);
   2550   1.22       jdc 	m_adj(*mp, ETHER_HDR_LEN);
   2551   1.22       jdc 
   2552    1.9  perseant 	/* Strip off snap header, if present */
   2553    1.9  perseant 	if (snap) {
   2554   1.50  christos 		m_copydata(*mp, 0, sizeof(struct llc), (void *) &llc1);
   2555   1.22       jdc 		m_adj(*mp, sizeof(struct llc));
   2556   1.19  christos 	}
   2557    1.9  perseant 
   2558    1.9  perseant 	/*
   2559   1.22       jdc 	 * Check basic packet sanity and run IPF through pfil.
   2560    1.9  perseant 	 */
   2561   1.65    bouyer 	KASSERT(!cpu_intr_p());
   2562   1.22       jdc 	switch (ether_type)
   2563   1.22       jdc 	{
   2564   1.22       jdc 	case ETHERTYPE_IP :
   2565  1.131  christos 		error = bridge_ip_checkbasic(mp);
   2566   1.22       jdc 		if (error == 0)
   2567   1.77     rmind 			error = pfil_run_hooks(inet_pfil_hook, mp, ifp, dir);
   2568   1.22       jdc 		break;
   2569    1.9  perseant # ifdef INET6
   2570   1.22       jdc 	case ETHERTYPE_IPV6 :
   2571  1.131  christos 		error = bridge_ip6_checkbasic(mp);
   2572   1.22       jdc 		if (error == 0)
   2573   1.77     rmind 			error = pfil_run_hooks(inet6_pfil_hook, mp, ifp, dir);
   2574   1.22       jdc 		break;
   2575   1.22       jdc # endif
   2576   1.22       jdc 	default :
   2577   1.22       jdc 		error = 0;
   2578   1.22       jdc 		break;
   2579    1.9  perseant 	}
   2580   1.22       jdc 
   2581   1.22       jdc 	if (*mp == NULL)
   2582   1.22       jdc 		return error;
   2583   1.22       jdc 	if (error != 0)
   2584   1.22       jdc 		goto bad;
   2585   1.22       jdc 
   2586   1.22       jdc 	error = -1;
   2587    1.9  perseant 
   2588    1.9  perseant 	/*
   2589    1.9  perseant 	 * Finally, put everything back the way it was and return
   2590    1.9  perseant 	 */
   2591   1.18       jdc 	if (snap) {
   2592   1.22       jdc 		M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
   2593   1.22       jdc 		if (*mp == NULL)
   2594   1.22       jdc 			return error;
   2595   1.50  christos 		bcopy(&llc1, mtod(*mp, void *), sizeof(struct llc));
   2596   1.18       jdc 	}
   2597   1.18       jdc 
   2598   1.22       jdc 	M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
   2599   1.22       jdc 	if (*mp == NULL)
   2600   1.22       jdc 		return error;
   2601   1.50  christos 	bcopy(&eh2, mtod(*mp, void *), ETHER_HDR_LEN);
   2602   1.22       jdc 
   2603    1.9  perseant 	return 0;
   2604    1.9  perseant 
   2605    1.9  perseant     bad:
   2606    1.9  perseant 	m_freem(*mp);
   2607    1.9  perseant 	*mp = NULL;
   2608    1.9  perseant 	return error;
   2609    1.9  perseant }
   2610    1.9  perseant 
   2611    1.9  perseant /*
   2612    1.9  perseant  * Perform basic checks on header size since
   2613    1.9  perseant  * IPF assumes ip_input has already processed
   2614    1.9  perseant  * it for it.  Cut-and-pasted from ip_input.c.
   2615    1.9  perseant  * Given how simple the IPv6 version is,
   2616    1.9  perseant  * does the IPv4 version really need to be
   2617    1.9  perseant  * this complicated?
   2618    1.9  perseant  *
   2619    1.9  perseant  * XXX Should we update ipstat here, or not?
   2620    1.9  perseant  * XXX Right now we update ipstat but not
   2621    1.9  perseant  * XXX csum_counter.
   2622    1.9  perseant  */
   2623    1.9  perseant static int
   2624    1.9  perseant bridge_ip_checkbasic(struct mbuf **mp)
   2625    1.9  perseant {
   2626    1.9  perseant 	struct mbuf *m = *mp;
   2627    1.9  perseant 	struct ip *ip;
   2628    1.9  perseant 	int len, hlen;
   2629    1.9  perseant 
   2630    1.9  perseant 	if (*mp == NULL)
   2631    1.9  perseant 		return -1;
   2632    1.9  perseant 
   2633   1.50  christos 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
   2634    1.9  perseant 		if ((m = m_copyup(m, sizeof(struct ip),
   2635    1.9  perseant 			(max_linkhdr + 3) & ~3)) == NULL) {
   2636    1.9  perseant 			/* XXXJRT new stat, please */
   2637   1.59   thorpej 			ip_statinc(IP_STAT_TOOSMALL);
   2638    1.9  perseant 			goto bad;
   2639    1.9  perseant 		}
   2640    1.9  perseant 	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
   2641    1.9  perseant 		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
   2642   1.59   thorpej 			ip_statinc(IP_STAT_TOOSMALL);
   2643    1.9  perseant 			goto bad;
   2644   1.29     perry 		}
   2645    1.9  perseant 	}
   2646    1.9  perseant 	ip = mtod(m, struct ip *);
   2647    1.9  perseant 	if (ip == NULL) goto bad;
   2648    1.9  perseant 
   2649    1.9  perseant 	if (ip->ip_v != IPVERSION) {
   2650   1.59   thorpej 		ip_statinc(IP_STAT_BADVERS);
   2651    1.9  perseant 		goto bad;
   2652    1.9  perseant 	}
   2653    1.9  perseant 	hlen = ip->ip_hl << 2;
   2654    1.9  perseant 	if (hlen < sizeof(struct ip)) { /* minimum header length */
   2655   1.59   thorpej 		ip_statinc(IP_STAT_BADHLEN);
   2656    1.9  perseant 		goto bad;
   2657    1.9  perseant 	}
   2658    1.9  perseant 	if (hlen > m->m_len) {
   2659    1.9  perseant 		if ((m = m_pullup(m, hlen)) == 0) {
   2660   1.59   thorpej 			ip_statinc(IP_STAT_BADHLEN);
   2661    1.9  perseant 			goto bad;
   2662    1.9  perseant 		}
   2663    1.9  perseant 		ip = mtod(m, struct ip *);
   2664    1.9  perseant 		if (ip == NULL) goto bad;
   2665    1.9  perseant 	}
   2666    1.9  perseant 
   2667  1.130     ozaki 	switch (m->m_pkthdr.csum_flags &
   2668  1.130     ozaki 	        ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_IPv4) |
   2669  1.130     ozaki 	         M_CSUM_IPv4_BAD)) {
   2670  1.130     ozaki 	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
   2671  1.130     ozaki 		/* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
   2672  1.130     ozaki 		goto bad;
   2673  1.130     ozaki 
   2674  1.130     ozaki 	case M_CSUM_IPv4:
   2675  1.130     ozaki 		/* Checksum was okay. */
   2676  1.130     ozaki 		/* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
   2677  1.130     ozaki 		break;
   2678  1.130     ozaki 
   2679  1.130     ozaki 	default:
   2680  1.130     ozaki 		/* Must compute it ourselves. */
   2681  1.130     ozaki 		/* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
   2682  1.130     ozaki 		if (in_cksum(m, hlen) != 0)
   2683  1.130     ozaki 			goto bad;
   2684  1.130     ozaki 		break;
   2685  1.130     ozaki 	}
   2686  1.130     ozaki 
   2687  1.130     ozaki 	/* Retrieve the packet length. */
   2688  1.130     ozaki 	len = ntohs(ip->ip_len);
   2689  1.130     ozaki 
   2690  1.130     ozaki 	/*
   2691  1.130     ozaki 	 * Check for additional length bogosity
   2692  1.130     ozaki 	 */
   2693  1.130     ozaki 	if (len < hlen) {
   2694   1.59   thorpej 		ip_statinc(IP_STAT_BADLEN);
   2695  1.130     ozaki 		goto bad;
   2696  1.130     ozaki 	}
   2697    1.9  perseant 
   2698  1.130     ozaki 	/*
   2699  1.130     ozaki 	 * Check that the amount of data in the buffers
   2700  1.130     ozaki 	 * is as at least much as the IP header would have us expect.
   2701  1.130     ozaki 	 * Drop packet if shorter than we expect.
   2702  1.130     ozaki 	 */
   2703  1.130     ozaki 	if (m->m_pkthdr.len < len) {
   2704   1.59   thorpej 		ip_statinc(IP_STAT_TOOSHORT);
   2705  1.130     ozaki 		goto bad;
   2706  1.130     ozaki 	}
   2707    1.9  perseant 
   2708    1.9  perseant 	/* Checks out, proceed */
   2709    1.9  perseant 	*mp = m;
   2710    1.9  perseant 	return 0;
   2711    1.9  perseant 
   2712    1.9  perseant     bad:
   2713    1.9  perseant 	*mp = m;
   2714    1.9  perseant 	return -1;
   2715    1.9  perseant }
   2716    1.9  perseant 
   2717    1.9  perseant # ifdef INET6
   2718    1.9  perseant /*
   2719    1.9  perseant  * Same as above, but for IPv6.
   2720    1.9  perseant  * Cut-and-pasted from ip6_input.c.
   2721    1.9  perseant  * XXX Should we update ip6stat, or not?
   2722    1.9  perseant  */
   2723    1.9  perseant static int
   2724    1.9  perseant bridge_ip6_checkbasic(struct mbuf **mp)
   2725    1.9  perseant {
   2726    1.9  perseant 	struct mbuf *m = *mp;
   2727   1.16       jdc 	struct ip6_hdr *ip6;
   2728    1.9  perseant 
   2729  1.130     ozaki 	/*
   2730  1.130     ozaki 	 * If the IPv6 header is not aligned, slurp it up into a new
   2731  1.130     ozaki 	 * mbuf with space for link headers, in the event we forward
   2732  1.130     ozaki 	 * it.  Otherwise, if it is aligned, make sure the entire base
   2733  1.130     ozaki 	 * IPv6 header is in the first mbuf of the chain.
   2734  1.130     ozaki 	 */
   2735  1.130     ozaki 	if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
   2736  1.130     ozaki 		struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
   2737  1.130     ozaki 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
   2738  1.130     ozaki 		                  (max_linkhdr + 3) & ~3)) == NULL) {
   2739  1.130     ozaki 			/* XXXJRT new stat, please */
   2740   1.61   thorpej 			ip6_statinc(IP6_STAT_TOOSMALL);
   2741  1.130     ozaki 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
   2742  1.130     ozaki 			goto bad;
   2743  1.130     ozaki 		}
   2744  1.130     ozaki 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
   2745  1.130     ozaki 		struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
   2746  1.130     ozaki 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
   2747   1.61   thorpej 			ip6_statinc(IP6_STAT_TOOSMALL);
   2748  1.130     ozaki 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
   2749  1.130     ozaki 			goto bad;
   2750  1.130     ozaki 		}
   2751  1.130     ozaki 	}
   2752    1.9  perseant 
   2753  1.130     ozaki 	ip6 = mtod(m, struct ip6_hdr *);
   2754    1.9  perseant 
   2755  1.130     ozaki 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
   2756   1.61   thorpej 		ip6_statinc(IP6_STAT_BADVERS);
   2757  1.130     ozaki 		in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m), ifs6_in_hdrerr);
   2758  1.130     ozaki 		goto bad;
   2759  1.130     ozaki 	}
   2760    1.9  perseant 
   2761    1.9  perseant 	/* Checks out, proceed */
   2762    1.9  perseant 	*mp = m;
   2763    1.9  perseant 	return 0;
   2764    1.9  perseant 
   2765    1.9  perseant     bad:
   2766    1.9  perseant 	*mp = m;
   2767    1.9  perseant 	return -1;
   2768    1.9  perseant }
   2769    1.9  perseant # endif /* INET6 */
   2770   1.77     rmind #endif /* BRIDGE_IPF */
   2771