1147665Sthompsa/*	$NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $	*/
2146985Sthompsa
3146985Sthompsa/*
4146985Sthompsa * Copyright 2001 Wasabi Systems, Inc.
5146985Sthompsa * All rights reserved.
6146985Sthompsa *
7146985Sthompsa * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8146985Sthompsa *
9146985Sthompsa * Redistribution and use in source and binary forms, with or without
10146985Sthompsa * modification, are permitted provided that the following conditions
11146985Sthompsa * are met:
12146985Sthompsa * 1. Redistributions of source code must retain the above copyright
13146985Sthompsa *    notice, this list of conditions and the following disclaimer.
14146985Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
15146985Sthompsa *    notice, this list of conditions and the following disclaimer in the
16146985Sthompsa *    documentation and/or other materials provided with the distribution.
17146985Sthompsa * 3. All advertising materials mentioning features or use of this software
18146985Sthompsa *    must display the following acknowledgement:
19146985Sthompsa *	This product includes software developed for the NetBSD Project by
20146985Sthompsa *	Wasabi Systems, Inc.
21146985Sthompsa * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22146985Sthompsa *    or promote products derived from this software without specific prior
23146985Sthompsa *    written permission.
24146985Sthompsa *
25146985Sthompsa * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26146985Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27146985Sthompsa * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28146985Sthompsa * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29146985Sthompsa * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30146985Sthompsa * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31146985Sthompsa * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32146985Sthompsa * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33146985Sthompsa * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34146985Sthompsa * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35146985Sthompsa * POSSIBILITY OF SUCH DAMAGE.
36146985Sthompsa */
37146985Sthompsa
38146985Sthompsa/*
39146985Sthompsa * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
40146985Sthompsa * All rights reserved.
41146985Sthompsa *
42146985Sthompsa * Redistribution and use in source and binary forms, with or without
43146985Sthompsa * modification, are permitted provided that the following conditions
44146985Sthompsa * are met:
45146985Sthompsa * 1. Redistributions of source code must retain the above copyright
46146985Sthompsa *    notice, this list of conditions and the following disclaimer.
47146985Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
48146985Sthompsa *    notice, this list of conditions and the following disclaimer in the
49146985Sthompsa *    documentation and/or other materials provided with the distribution.
50146985Sthompsa *
51146985Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52146985Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53146985Sthompsa * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54146985Sthompsa * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
55146985Sthompsa * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56146985Sthompsa * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57146985Sthompsa * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58146985Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59146985Sthompsa * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
60146985Sthompsa * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61146985Sthompsa * POSSIBILITY OF SUCH DAMAGE.
62146985Sthompsa *
63146985Sthompsa * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
64146985Sthompsa */
65146985Sthompsa
66146985Sthompsa/*
67146985Sthompsa * Network interface bridge support.
68146985Sthompsa *
69146985Sthompsa * TODO:
70146985Sthompsa *
71146985Sthompsa *	- Currently only supports Ethernet-like interfaces (Ethernet,
72146985Sthompsa *	  802.11, VLANs on Ethernet, etc.)  Figure out a nice way
73146985Sthompsa *	  to bridge other types of interfaces (FDDI-FDDI, and maybe
74146985Sthompsa *	  consider heterogenous bridges).
75146985Sthompsa */
76146985Sthompsa
77146985Sthompsa#include <sys/cdefs.h>
78146985Sthompsa__FBSDID("$FreeBSD$");
79146985Sthompsa
80146985Sthompsa#include "opt_inet.h"
81146985Sthompsa#include "opt_inet6.h"
82146985Sthompsa
83146985Sthompsa#include <sys/param.h>
84146985Sthompsa#include <sys/mbuf.h>
85146985Sthompsa#include <sys/malloc.h>
86146985Sthompsa#include <sys/protosw.h>
87146985Sthompsa#include <sys/systm.h>
88225380Sthompsa#include <sys/jail.h>
89146985Sthompsa#include <sys/time.h>
90146985Sthompsa#include <sys/socket.h> /* for net/if.h */
91146985Sthompsa#include <sys/sockio.h>
92146985Sthompsa#include <sys/ctype.h>  /* string functions */
93146985Sthompsa#include <sys/kernel.h>
94146985Sthompsa#include <sys/random.h>
95160902Sthompsa#include <sys/syslog.h>
96146985Sthompsa#include <sys/sysctl.h>
97146985Sthompsa#include <vm/uma.h>
98146985Sthompsa#include <sys/module.h>
99164033Srwatson#include <sys/priv.h>
100146985Sthompsa#include <sys/proc.h>
101146985Sthompsa#include <sys/lock.h>
102146985Sthompsa#include <sys/mutex.h>
103146985Sthompsa
104146985Sthompsa#include <net/bpf.h>
105146985Sthompsa#include <net/if.h>
106146985Sthompsa#include <net/if_clone.h>
107146985Sthompsa#include <net/if_dl.h>
108146985Sthompsa#include <net/if_types.h>
109146985Sthompsa#include <net/if_var.h>
110146985Sthompsa#include <net/pfil.h>
111197952Sjulian#include <net/vnet.h>
112146985Sthompsa
113146985Sthompsa#include <netinet/in.h> /* for struct arpcom */
114146985Sthompsa#include <netinet/in_systm.h>
115146985Sthompsa#include <netinet/in_var.h>
116146985Sthompsa#include <netinet/ip.h>
117146985Sthompsa#include <netinet/ip_var.h>
118146985Sthompsa#ifdef INET6
119146985Sthompsa#include <netinet/ip6.h>
120146985Sthompsa#include <netinet6/ip6_var.h>
121252511Shrs#include <netinet6/in6_ifattach.h>
122146985Sthompsa#endif
123193983Sbz#if defined(INET) || defined(INET6)
124159446Sthompsa#include <netinet/ip_carp.h>
125159446Sthompsa#endif
126146985Sthompsa#include <machine/in_cksum.h>
127146985Sthompsa#include <netinet/if_ether.h> /* for struct arpcom */
128160703Sthompsa#include <net/bridgestp.h>
129146985Sthompsa#include <net/if_bridgevar.h>
130146985Sthompsa#include <net/if_llc.h>
131170681Sthompsa#include <net/if_vlan_var.h>
132146985Sthompsa
133146985Sthompsa#include <net/route.h>
134146985Sthompsa
135146985Sthompsa/*
136146985Sthompsa * Size of the route hash table.  Must be a power of two.
137146985Sthompsa */
138146985Sthompsa#ifndef BRIDGE_RTHASH_SIZE
139146985Sthompsa#define	BRIDGE_RTHASH_SIZE		1024
140146985Sthompsa#endif
141146985Sthompsa
142146985Sthompsa#define	BRIDGE_RTHASH_MASK		(BRIDGE_RTHASH_SIZE - 1)
143146985Sthompsa
144146985Sthompsa/*
145232315Sthompsa * Default maximum number of addresses to cache.
146146985Sthompsa */
147146985Sthompsa#ifndef BRIDGE_RTABLE_MAX
148232315Sthompsa#define	BRIDGE_RTABLE_MAX		2000
149146985Sthompsa#endif
150146985Sthompsa
151146985Sthompsa/*
152146985Sthompsa * Timeout (in seconds) for entries learned dynamically.
153146985Sthompsa */
154146985Sthompsa#ifndef BRIDGE_RTABLE_TIMEOUT
155146985Sthompsa#define	BRIDGE_RTABLE_TIMEOUT		(20 * 60)	/* same as ARP */
156146985Sthompsa#endif
157146985Sthompsa
158146985Sthompsa/*
159146985Sthompsa * Number of seconds between walks of the route list.
160146985Sthompsa */
161146985Sthompsa#ifndef BRIDGE_RTABLE_PRUNE_PERIOD
162146985Sthompsa#define	BRIDGE_RTABLE_PRUNE_PERIOD	(5 * 60)
163146985Sthompsa#endif
164146985Sthompsa
165154336Sthompsa/*
166180220Sthompsa * List of capabilities to possibly mask on the member interface.
167154336Sthompsa */
168180220Sthompsa#define	BRIDGE_IFCAPS_MASK		(IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM)
169154336Sthompsa
170160769Sthompsa/*
171196519Sjfv * List of capabilities to strip
172196519Sjfv */
173196519Sjfv#define	BRIDGE_IFCAPS_STRIP		IFCAP_LRO
174196519Sjfv
175196519Sjfv/*
176160769Sthompsa * Bridge interface list entry.
177160769Sthompsa */
178160769Sthompsastruct bridge_iflist {
179160769Sthompsa	LIST_ENTRY(bridge_iflist) bif_next;
180160769Sthompsa	struct ifnet		*bif_ifp;	/* member if */
181160769Sthompsa	struct bstp_port	bif_stp;	/* STP state */
182160769Sthompsa	uint32_t		bif_flags;	/* member if flags */
183180220Sthompsa	int			bif_savedcaps;	/* saved capabilities */
184173320Sthompsa	uint32_t		bif_addrmax;	/* max # of addresses */
185173320Sthompsa	uint32_t		bif_addrcnt;	/* cur. # of addresses */
186173320Sthompsa	uint32_t		bif_addrexceeded;/* # of address violations */
187160769Sthompsa};
188160769Sthompsa
189160769Sthompsa/*
190160769Sthompsa * Bridge route node.
191160769Sthompsa */
192160769Sthompsastruct bridge_rtnode {
193160769Sthompsa	LIST_ENTRY(bridge_rtnode) brt_hash;	/* hash table linkage */
194160769Sthompsa	LIST_ENTRY(bridge_rtnode) brt_list;	/* list linkage */
195173320Sthompsa	struct bridge_iflist	*brt_dst;	/* destination if */
196160769Sthompsa	unsigned long		brt_expire;	/* expiration time */
197160769Sthompsa	uint8_t			brt_flags;	/* address flags */
198160769Sthompsa	uint8_t			brt_addr[ETHER_ADDR_LEN];
199170681Sthompsa	uint16_t		brt_vlan;	/* vlan id */
200160769Sthompsa};
201173320Sthompsa#define	brt_ifp			brt_dst->bif_ifp
202160769Sthompsa
203160769Sthompsa/*
204160769Sthompsa * Software state for each bridge.
205160769Sthompsa */
206160769Sthompsastruct bridge_softc {
207160769Sthompsa	struct ifnet		*sc_ifp;	/* make this an interface */
208160769Sthompsa	LIST_ENTRY(bridge_softc) sc_list;
209160769Sthompsa	struct mtx		sc_mtx;
210160769Sthompsa	struct cv		sc_cv;
211160769Sthompsa	uint32_t		sc_brtmax;	/* max # of addresses */
212160769Sthompsa	uint32_t		sc_brtcnt;	/* cur. # of addresses */
213160769Sthompsa	uint32_t		sc_brttimeout;	/* rt timeout in seconds */
214160769Sthompsa	struct callout		sc_brcallout;	/* bridge callout */
215160769Sthompsa	uint32_t		sc_iflist_ref;	/* refcount for sc_iflist */
216160769Sthompsa	uint32_t		sc_iflist_xcnt;	/* refcount for sc_iflist */
217160769Sthompsa	LIST_HEAD(, bridge_iflist) sc_iflist;	/* member interface list */
218160769Sthompsa	LIST_HEAD(, bridge_rtnode) *sc_rthash;	/* our forwarding table */
219160769Sthompsa	LIST_HEAD(, bridge_rtnode) sc_rtlist;	/* list version of above */
220160769Sthompsa	uint32_t		sc_rthash_key;	/* key for hash */
221160769Sthompsa	LIST_HEAD(, bridge_iflist) sc_spanlist;	/* span ports list */
222160769Sthompsa	struct bstp_state	sc_stp;		/* STP state */
223160867Sthompsa	uint32_t		sc_brtexceeded;	/* # of cache drops */
224188594Sthompsa	struct ifnet		*sc_ifaddr;	/* member mac copied from */
225180140Sphilip	u_char			sc_defaddr[6];	/* Default MAC address */
226160769Sthompsa};
227160769Sthompsa
228153494Sthompsastatic struct mtx 	bridge_list_mtx;
229153494Sthompsaeventhandler_tag	bridge_detach_cookie = NULL;
230153494Sthompsa
231146985Sthompsaint	bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
232146985Sthompsa
233146985Sthompsauma_zone_t bridge_rtnode_zone;
234146985Sthompsa
235160195Ssamstatic int	bridge_clone_create(struct if_clone *, int, caddr_t);
236151313Sthompsastatic void	bridge_clone_destroy(struct ifnet *);
237146985Sthompsa
238151313Sthompsastatic int	bridge_ioctl(struct ifnet *, u_long, caddr_t);
239180220Sthompsastatic void	bridge_mutecaps(struct bridge_softc *);
240180220Sthompsastatic void	bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *,
241180220Sthompsa		    int);
242153494Sthompsastatic void	bridge_ifdetach(void *arg __unused, struct ifnet *);
243146985Sthompsastatic void	bridge_init(void *);
244151313Sthompsastatic void	bridge_dummynet(struct mbuf *, struct ifnet *);
245151313Sthompsastatic void	bridge_stop(struct ifnet *, int);
246240071Sglebiusstatic int	bridge_transmit(struct ifnet *, struct mbuf *);
247240071Sglebiusstatic void	bridge_qflush(struct ifnet *);
248151313Sthompsastatic struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
249151313Sthompsastatic int	bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
250151313Sthompsa		    struct rtentry *);
251240071Sglebiusstatic int	bridge_enqueue(struct bridge_softc *, struct ifnet *,
252160769Sthompsa		    struct mbuf *);
253160769Sthompsastatic void	bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
254146985Sthompsa
255171678Sthompsastatic void	bridge_forward(struct bridge_softc *, struct bridge_iflist *,
256171678Sthompsa		    struct mbuf *m);
257146985Sthompsa
258151313Sthompsastatic void	bridge_timer(void *);
259146985Sthompsa
260151313Sthompsastatic void	bridge_broadcast(struct bridge_softc *, struct ifnet *,
261151313Sthompsa		    struct mbuf *, int);
262153408Sthompsastatic void	bridge_span(struct bridge_softc *, struct mbuf *);
263146985Sthompsa
264151313Sthompsastatic int	bridge_rtupdate(struct bridge_softc *, const uint8_t *,
265170681Sthompsa		    uint16_t, struct bridge_iflist *, int, uint8_t);
266170681Sthompsastatic struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *,
267170681Sthompsa		    uint16_t);
268151313Sthompsastatic void	bridge_rttrim(struct bridge_softc *);
269151313Sthompsastatic void	bridge_rtage(struct bridge_softc *);
270151313Sthompsastatic void	bridge_rtflush(struct bridge_softc *, int);
271170681Sthompsastatic int	bridge_rtdaddr(struct bridge_softc *, const uint8_t *,
272170681Sthompsa		    uint16_t);
273146985Sthompsa
274241183Sthompsastatic void	bridge_rtable_init(struct bridge_softc *);
275151313Sthompsastatic void	bridge_rtable_fini(struct bridge_softc *);
276146985Sthompsa
277155143Sthompsastatic int	bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
278151313Sthompsastatic struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
279170681Sthompsa		    const uint8_t *, uint16_t);
280151313Sthompsastatic int	bridge_rtnode_insert(struct bridge_softc *,
281151313Sthompsa		    struct bridge_rtnode *);
282151313Sthompsastatic void	bridge_rtnode_destroy(struct bridge_softc *,
283151313Sthompsa		    struct bridge_rtnode *);
284163863Sthompsastatic void	bridge_rtable_expire(struct ifnet *, int);
285160902Sthompsastatic void	bridge_state_change(struct ifnet *, int);
286146985Sthompsa
287151313Sthompsastatic struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
288151313Sthompsa		    const char *name);
289151313Sthompsastatic struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
290151313Sthompsa		    struct ifnet *ifp);
291151313Sthompsastatic void	bridge_delete_member(struct bridge_softc *,
292151594Sthompsa		    struct bridge_iflist *, int);
293153494Sthompsastatic void	bridge_delete_span(struct bridge_softc *,
294153494Sthompsa		    struct bridge_iflist *);
295146985Sthompsa
296151313Sthompsastatic int	bridge_ioctl_add(struct bridge_softc *, void *);
297151313Sthompsastatic int	bridge_ioctl_del(struct bridge_softc *, void *);
298151313Sthompsastatic int	bridge_ioctl_gifflags(struct bridge_softc *, void *);
299151313Sthompsastatic int	bridge_ioctl_sifflags(struct bridge_softc *, void *);
300151313Sthompsastatic int	bridge_ioctl_scache(struct bridge_softc *, void *);
301151313Sthompsastatic int	bridge_ioctl_gcache(struct bridge_softc *, void *);
302151313Sthompsastatic int	bridge_ioctl_gifs(struct bridge_softc *, void *);
303151313Sthompsastatic int	bridge_ioctl_rts(struct bridge_softc *, void *);
304151313Sthompsastatic int	bridge_ioctl_saddr(struct bridge_softc *, void *);
305151313Sthompsastatic int	bridge_ioctl_sto(struct bridge_softc *, void *);
306151313Sthompsastatic int	bridge_ioctl_gto(struct bridge_softc *, void *);
307151313Sthompsastatic int	bridge_ioctl_daddr(struct bridge_softc *, void *);
308151313Sthompsastatic int	bridge_ioctl_flush(struct bridge_softc *, void *);
309151313Sthompsastatic int	bridge_ioctl_gpri(struct bridge_softc *, void *);
310151313Sthompsastatic int	bridge_ioctl_spri(struct bridge_softc *, void *);
311151313Sthompsastatic int	bridge_ioctl_ght(struct bridge_softc *, void *);
312151313Sthompsastatic int	bridge_ioctl_sht(struct bridge_softc *, void *);
313151313Sthompsastatic int	bridge_ioctl_gfd(struct bridge_softc *, void *);
314151313Sthompsastatic int	bridge_ioctl_sfd(struct bridge_softc *, void *);
315151313Sthompsastatic int	bridge_ioctl_gma(struct bridge_softc *, void *);
316151313Sthompsastatic int	bridge_ioctl_sma(struct bridge_softc *, void *);
317151313Sthompsastatic int	bridge_ioctl_sifprio(struct bridge_softc *, void *);
318151313Sthompsastatic int	bridge_ioctl_sifcost(struct bridge_softc *, void *);
319173320Sthompsastatic int	bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *);
320153408Sthompsastatic int	bridge_ioctl_addspan(struct bridge_softc *, void *);
321153408Sthompsastatic int	bridge_ioctl_delspan(struct bridge_softc *, void *);
322160867Sthompsastatic int	bridge_ioctl_gbparam(struct bridge_softc *, void *);
323160867Sthompsastatic int	bridge_ioctl_grte(struct bridge_softc *, void *);
324160867Sthompsastatic int	bridge_ioctl_gifsstp(struct bridge_softc *, void *);
325163863Sthompsastatic int	bridge_ioctl_sproto(struct bridge_softc *, void *);
326163863Sthompsastatic int	bridge_ioctl_stxhc(struct bridge_softc *, void *);
327151313Sthompsastatic int	bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
328151313Sthompsa		    int);
329151313Sthompsastatic int	bridge_ip_checkbasic(struct mbuf **mp);
330158667Sthompsa#ifdef INET6
331151313Sthompsastatic int	bridge_ip6_checkbasic(struct mbuf **mp);
332158667Sthompsa#endif /* INET6 */
333158140Sthompsastatic int	bridge_fragment(struct ifnet *, struct mbuf *,
334158140Sthompsa		    struct ether_header *, int, struct llc *);
335234487Sthompsastatic void	bridge_linkstate(struct ifnet *ifp);
336236916Sthompsastatic void	bridge_linkcheck(struct bridge_softc *sc);
337146985Sthompsa
338234487Sthompsaextern void (*bridge_linkstate_p)(struct ifnet *ifp);
339234487Sthompsa
340170681Sthompsa/* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
341170681Sthompsa#define	VLANTAGOF(_m)	\
342170681Sthompsa    (_m->m_flags & M_VLANTAG) ? EVL_VLANOFTAG(_m->m_pkthdr.ether_vtag) : 1
343170681Sthompsa
344167379Sthompsastatic struct bstp_cb_ops bridge_ops = {
345167379Sthompsa	.bcb_state = bridge_state_change,
346167379Sthompsa	.bcb_rtage = bridge_rtable_expire
347167379Sthompsa};
348167379Sthompsa
349146985SthompsaSYSCTL_DECL(_net_link);
350227309Sedstatic SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
351146985Sthompsa
352153831Sthompsastatic int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
353146985Sthompsastatic int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
354146985Sthompsastatic int pfil_member = 1; /* run pfil hooks on the member interface */
355147111Sthompsastatic int pfil_ipfw = 0;   /* layer2 filter with ipfw */
356162561Sthompsastatic int pfil_ipfw_arp = 0;   /* layer2 filter with ipfw */
357172201Sthompsastatic int pfil_local_phys = 0; /* run pfil hooks on the physical interface for
358172201Sthompsa                                   locally destined packets */
359160902Sthompsastatic int log_stp   = 0;   /* log STP state changes */
360182862Sthompsastatic int bridge_inherit_mac = 0;   /* share MAC with first bridge member */
361231130SpjdTUNABLE_INT("net.link.bridge.pfil_onlyip", &pfil_onlyip);
362153831SthompsaSYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
363153831Sthompsa    &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
364231130SpjdTUNABLE_INT("net.link.bridge.ipfw_arp", &pfil_ipfw_arp);
365162561SthompsaSYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp, CTLFLAG_RW,
366162561Sthompsa    &pfil_ipfw_arp, 0, "Filter ARP packets through IPFW layer2");
367231130SpjdTUNABLE_INT("net.link.bridge.pfil_bridge", &pfil_bridge);
368146985SthompsaSYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
369146985Sthompsa    &pfil_bridge, 0, "Packet filter on the bridge interface");
370231130SpjdTUNABLE_INT("net.link.bridge.pfil_member", &pfil_member);
371146985SthompsaSYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
372146985Sthompsa    &pfil_member, 0, "Packet filter on the member interface");
373231130SpjdTUNABLE_INT("net.link.bridge.pfil_local_phys", &pfil_local_phys);
374172201SthompsaSYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys, CTLFLAG_RW,
375172201Sthompsa    &pfil_local_phys, 0,
376172201Sthompsa    "Packet filter on the physical interface for locally destined packets");
377231130SpjdTUNABLE_INT("net.link.bridge.log_stp", &log_stp);
378160902SthompsaSYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, CTLFLAG_RW,
379160902Sthompsa    &log_stp, 0, "Log STP state changes");
380231130SpjdTUNABLE_INT("net.link.bridge.inherit_mac", &bridge_inherit_mac);
381182862SthompsaSYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac, CTLFLAG_RW,
382182862Sthompsa    &bridge_inherit_mac, 0,
383182862Sthompsa    "Inherit MAC address from the first bridge member");
384146985Sthompsa
385253751Shrsstatic VNET_DEFINE(int, allow_llz_overlap) = 0;
386253751Shrs#define	V_allow_llz_overlap	VNET(allow_llz_overlap)
387253751ShrsSYSCTL_VNET_INT(_net_link_bridge, OID_AUTO, allow_llz_overlap, CTLFLAG_RW,
388253751Shrs    &VNET_NAME(allow_llz_overlap), 0, "Allow overlap of link-local scope "
389253751Shrs    "zones of a bridge interface and the member interfaces");
390253751Shrs
391146985Sthompsastruct bridge_control {
392146985Sthompsa	int	(*bc_func)(struct bridge_softc *, void *);
393146985Sthompsa	int	bc_argsize;
394146985Sthompsa	int	bc_flags;
395146985Sthompsa};
396146985Sthompsa
397146985Sthompsa#define	BC_F_COPYIN		0x01	/* copy arguments in */
398146985Sthompsa#define	BC_F_COPYOUT		0x02	/* copy arguments out */
399146985Sthompsa#define	BC_F_SUSER		0x04	/* do super-user check */
400146985Sthompsa
401146985Sthompsaconst struct bridge_control bridge_control_table[] = {
402146985Sthompsa	{ bridge_ioctl_add,		sizeof(struct ifbreq),
403146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
404146985Sthompsa	{ bridge_ioctl_del,		sizeof(struct ifbreq),
405146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
406146985Sthompsa
407146985Sthompsa	{ bridge_ioctl_gifflags,	sizeof(struct ifbreq),
408146985Sthompsa	  BC_F_COPYIN|BC_F_COPYOUT },
409146985Sthompsa	{ bridge_ioctl_sifflags,	sizeof(struct ifbreq),
410146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
411146985Sthompsa
412146985Sthompsa	{ bridge_ioctl_scache,		sizeof(struct ifbrparam),
413146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
414146985Sthompsa	{ bridge_ioctl_gcache,		sizeof(struct ifbrparam),
415146985Sthompsa	  BC_F_COPYOUT },
416146985Sthompsa
417146985Sthompsa	{ bridge_ioctl_gifs,		sizeof(struct ifbifconf),
418146985Sthompsa	  BC_F_COPYIN|BC_F_COPYOUT },
419146985Sthompsa	{ bridge_ioctl_rts,		sizeof(struct ifbaconf),
420146985Sthompsa	  BC_F_COPYIN|BC_F_COPYOUT },
421146985Sthompsa
422146985Sthompsa	{ bridge_ioctl_saddr,		sizeof(struct ifbareq),
423146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
424146985Sthompsa
425146985Sthompsa	{ bridge_ioctl_sto,		sizeof(struct ifbrparam),
426146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
427146985Sthompsa	{ bridge_ioctl_gto,		sizeof(struct ifbrparam),
428146985Sthompsa	  BC_F_COPYOUT },
429146985Sthompsa
430146985Sthompsa	{ bridge_ioctl_daddr,		sizeof(struct ifbareq),
431146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
432146985Sthompsa
433146985Sthompsa	{ bridge_ioctl_flush,		sizeof(struct ifbreq),
434146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
435146985Sthompsa
436146985Sthompsa	{ bridge_ioctl_gpri,		sizeof(struct ifbrparam),
437146985Sthompsa	  BC_F_COPYOUT },
438146985Sthompsa	{ bridge_ioctl_spri,		sizeof(struct ifbrparam),
439146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
440146985Sthompsa
441146985Sthompsa	{ bridge_ioctl_ght,		sizeof(struct ifbrparam),
442146985Sthompsa	  BC_F_COPYOUT },
443146985Sthompsa	{ bridge_ioctl_sht,		sizeof(struct ifbrparam),
444146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
445146985Sthompsa
446146985Sthompsa	{ bridge_ioctl_gfd,		sizeof(struct ifbrparam),
447146985Sthompsa	  BC_F_COPYOUT },
448146985Sthompsa	{ bridge_ioctl_sfd,		sizeof(struct ifbrparam),
449146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
450146985Sthompsa
451146985Sthompsa	{ bridge_ioctl_gma,		sizeof(struct ifbrparam),
452146985Sthompsa	  BC_F_COPYOUT },
453146985Sthompsa	{ bridge_ioctl_sma,		sizeof(struct ifbrparam),
454146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
455146985Sthompsa
456146985Sthompsa	{ bridge_ioctl_sifprio,		sizeof(struct ifbreq),
457146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
458146985Sthompsa
459146985Sthompsa	{ bridge_ioctl_sifcost,		sizeof(struct ifbreq),
460146985Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
461153408Sthompsa
462153408Sthompsa	{ bridge_ioctl_addspan,		sizeof(struct ifbreq),
463153408Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
464153408Sthompsa	{ bridge_ioctl_delspan,		sizeof(struct ifbreq),
465153408Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
466160867Sthompsa
467160867Sthompsa	{ bridge_ioctl_gbparam,		sizeof(struct ifbropreq),
468160867Sthompsa	  BC_F_COPYOUT },
469160867Sthompsa
470160867Sthompsa	{ bridge_ioctl_grte,		sizeof(struct ifbrparam),
471160867Sthompsa	  BC_F_COPYOUT },
472160867Sthompsa
473160867Sthompsa	{ bridge_ioctl_gifsstp,		sizeof(struct ifbpstpconf),
474164861Ssyrinx	  BC_F_COPYIN|BC_F_COPYOUT },
475163863Sthompsa
476163863Sthompsa	{ bridge_ioctl_sproto,		sizeof(struct ifbrparam),
477163863Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
478163863Sthompsa
479163863Sthompsa	{ bridge_ioctl_stxhc,		sizeof(struct ifbrparam),
480163863Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
481173320Sthompsa
482173320Sthompsa	{ bridge_ioctl_sifmaxaddr,	sizeof(struct ifbreq),
483173320Sthompsa	  BC_F_COPYIN|BC_F_SUSER },
484173320Sthompsa
485146985Sthompsa};
486146985Sthompsaconst int bridge_control_table_size =
487146985Sthompsa    sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
488146985Sthompsa
489153494SthompsaLIST_HEAD(, bridge_softc) bridge_list;
490153494Sthompsa
491241610Sglebiusstatic struct if_clone *bridge_cloner;
492241610Sglebiusstatic const char bridge_name[] = "bridge";
493146985Sthompsa
494146985Sthompsastatic int
495146985Sthompsabridge_modevent(module_t mod, int type, void *data)
496146985Sthompsa{
497146985Sthompsa
498146985Sthompsa	switch (type) {
499146985Sthompsa	case MOD_LOAD:
500153494Sthompsa		mtx_init(&bridge_list_mtx, "if_bridge list", NULL, MTX_DEF);
501241610Sglebius		bridge_cloner = if_clone_simple(bridge_name,
502241610Sglebius		    bridge_clone_create, bridge_clone_destroy, 0);
503146985Sthompsa		bridge_rtnode_zone = uma_zcreate("bridge_rtnode",
504146985Sthompsa		    sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL,
505146985Sthompsa		    UMA_ALIGN_PTR, 0);
506153494Sthompsa		LIST_INIT(&bridge_list);
507146985Sthompsa		bridge_input_p = bridge_input;
508146985Sthompsa		bridge_output_p = bridge_output;
509147205Sthompsa		bridge_dn_p = bridge_dummynet;
510234487Sthompsa		bridge_linkstate_p = bridge_linkstate;
511153494Sthompsa		bridge_detach_cookie = EVENTHANDLER_REGISTER(
512153494Sthompsa		    ifnet_departure_event, bridge_ifdetach, NULL,
513153494Sthompsa		    EVENTHANDLER_PRI_ANY);
514146985Sthompsa		break;
515146985Sthompsa	case MOD_UNLOAD:
516153494Sthompsa		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
517153494Sthompsa		    bridge_detach_cookie);
518241610Sglebius		if_clone_detach(bridge_cloner);
519146985Sthompsa		uma_zdestroy(bridge_rtnode_zone);
520146985Sthompsa		bridge_input_p = NULL;
521146985Sthompsa		bridge_output_p = NULL;
522147205Sthompsa		bridge_dn_p = NULL;
523234487Sthompsa		bridge_linkstate_p = NULL;
524153494Sthompsa		mtx_destroy(&bridge_list_mtx);
525146985Sthompsa		break;
526146985Sthompsa	default:
527158667Sthompsa		return (EOPNOTSUPP);
528146985Sthompsa	}
529158667Sthompsa	return (0);
530146985Sthompsa}
531146985Sthompsa
532146985Sthompsastatic moduledata_t bridge_mod = {
533153497Sthompsa	"if_bridge",
534153497Sthompsa	bridge_modevent,
535241394Skevlo	0
536146985Sthompsa};
537146985Sthompsa
538146985SthompsaDECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
539160730SthompsaMODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1);
540146985Sthompsa
541147111Sthompsa/*
542244378Skevlo * handler for net.link.bridge.ipfw
543147111Sthompsa */
544147111Sthompsastatic int
545147111Sthompsasysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS)
546147111Sthompsa{
547153497Sthompsa	int enable = pfil_ipfw;
548153497Sthompsa	int error;
549146985Sthompsa
550153497Sthompsa	error = sysctl_handle_int(oidp, &enable, 0, req);
551153497Sthompsa	enable = (enable) ? 1 : 0;
552147111Sthompsa
553153497Sthompsa	if (enable != pfil_ipfw) {
554153497Sthompsa		pfil_ipfw = enable;
555147111Sthompsa
556153497Sthompsa		/*
557153497Sthompsa		 * Disable pfil so that ipfw doesnt run twice, if the user
558153497Sthompsa		 * really wants both then they can re-enable pfil_bridge and/or
559153831Sthompsa		 * pfil_member. Also allow non-ip packets as ipfw can filter by
560153831Sthompsa		 * layer2 type.
561153497Sthompsa		 */
562153497Sthompsa		if (pfil_ipfw) {
563153831Sthompsa			pfil_onlyip = 0;
564153497Sthompsa			pfil_bridge = 0;
565153497Sthompsa			pfil_member = 0;
566153497Sthompsa		}
567147111Sthompsa	}
568147111Sthompsa
569158667Sthompsa	return (error);
570147111Sthompsa}
571147111SthompsaSYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, CTLTYPE_INT|CTLFLAG_RW,
572147111Sthompsa	    &pfil_ipfw, 0, &sysctl_pfil_ipfw, "I", "Layer2 filter with IPFW");
573147111Sthompsa
574146985Sthompsa/*
575146985Sthompsa * bridge_clone_create:
576146985Sthompsa *
577146985Sthompsa *	Create a new bridge instance.
578146985Sthompsa */
579151313Sthompsastatic int
580160195Ssambridge_clone_create(struct if_clone *ifc, int unit, caddr_t params)
581146985Sthompsa{
582156238Sthompsa	struct bridge_softc *sc, *sc2;
583156238Sthompsa	struct ifnet *bifp, *ifp;
584225380Sthompsa	int fb, retry;
585225380Sthompsa	unsigned long hostid;
586146985Sthompsa
587146985Sthompsa	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
588147281Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
589147281Sthompsa	if (ifp == NULL) {
590147281Sthompsa		free(sc, M_DEVBUF);
591147281Sthompsa		return (ENOSPC);
592147281Sthompsa	}
593146985Sthompsa
594166916Sthompsa	BRIDGE_LOCK_INIT(sc);
595146985Sthompsa	sc->sc_brtmax = BRIDGE_RTABLE_MAX;
596146985Sthompsa	sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
597146985Sthompsa
598146985Sthompsa	/* Initialize our routing table. */
599146985Sthompsa	bridge_rtable_init(sc);
600146985Sthompsa
601149253Sthompsa	callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0);
602146985Sthompsa
603146985Sthompsa	LIST_INIT(&sc->sc_iflist);
604153408Sthompsa	LIST_INIT(&sc->sc_spanlist);
605146985Sthompsa
606146985Sthompsa	ifp->if_softc = sc;
607241610Sglebius	if_initname(ifp, bridge_name, unit);
608161625Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
609146985Sthompsa	ifp->if_ioctl = bridge_ioctl;
610240071Sglebius	ifp->if_transmit = bridge_transmit;
611240071Sglebius	ifp->if_qflush = bridge_qflush;
612146985Sthompsa	ifp->if_init = bridge_init;
613146985Sthompsa	ifp->if_type = IFT_BRIDGE;
614146985Sthompsa
615146985Sthompsa	/*
616225380Sthompsa	 * Generate an ethernet address with a locally administered address.
617156238Sthompsa	 *
618156238Sthompsa	 * Since we are using random ethernet addresses for the bridge, it is
619156238Sthompsa	 * possible that we might have address collisions, so make sure that
620156238Sthompsa	 * this hardware address isn't already in use on another bridge.
621225380Sthompsa	 * The first try uses the hostid and falls back to arc4rand().
622146985Sthompsa	 */
623225380Sthompsa	fb = 0;
624225380Sthompsa	getcredhostid(curthread->td_ucred, &hostid);
625243669Spjd	do {
626225380Sthompsa		if (fb || hostid == 0) {
627225380Sthompsa			arc4rand(sc->sc_defaddr, ETHER_ADDR_LEN, 1);
628225380Sthompsa			sc->sc_defaddr[0] &= ~1;/* clear multicast bit */
629225380Sthompsa			sc->sc_defaddr[0] |= 2;	/* set the LAA bit */
630225380Sthompsa		} else {
631225380Sthompsa			sc->sc_defaddr[0] = 0x2;
632225380Sthompsa			sc->sc_defaddr[1] = (hostid >> 24) & 0xff;
633225380Sthompsa			sc->sc_defaddr[2] = (hostid >> 16) & 0xff;
634225380Sthompsa			sc->sc_defaddr[3] = (hostid >> 8 ) & 0xff;
635225380Sthompsa			sc->sc_defaddr[4] =  hostid        & 0xff;
636225380Sthompsa			sc->sc_defaddr[5] = ifp->if_dunit & 0xff;
637225380Sthompsa		}
638225380Sthompsa
639225380Sthompsa		fb = 1;
640156238Sthompsa		retry = 0;
641156238Sthompsa		mtx_lock(&bridge_list_mtx);
642156238Sthompsa		LIST_FOREACH(sc2, &bridge_list, sc_list) {
643156238Sthompsa			bifp = sc2->sc_ifp;
644180140Sphilip			if (memcmp(sc->sc_defaddr,
645243669Spjd			    IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
646156238Sthompsa				retry = 1;
647243669Spjd				break;
648243669Spjd			}
649156238Sthompsa		}
650156238Sthompsa		mtx_unlock(&bridge_list_mtx);
651243669Spjd	} while (retry == 1);
652146985Sthompsa
653167379Sthompsa	bstp_attach(&sc->sc_stp, &bridge_ops);
654180140Sphilip	ether_ifattach(ifp, sc->sc_defaddr);
655146985Sthompsa	/* Now undo some of the damage... */
656146985Sthompsa	ifp->if_baudrate = 0;
657146985Sthompsa	ifp->if_type = IFT_BRIDGE;
658146985Sthompsa
659153494Sthompsa	mtx_lock(&bridge_list_mtx);
660153494Sthompsa	LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
661153494Sthompsa	mtx_unlock(&bridge_list_mtx);
662153494Sthompsa
663146985Sthompsa	return (0);
664146985Sthompsa}
665146985Sthompsa
666146985Sthompsa/*
667146985Sthompsa * bridge_clone_destroy:
668146985Sthompsa *
669146985Sthompsa *	Destroy a bridge instance.
670146985Sthompsa */
671151313Sthompsastatic void
672146985Sthompsabridge_clone_destroy(struct ifnet *ifp)
673146985Sthompsa{
674146985Sthompsa	struct bridge_softc *sc = ifp->if_softc;
675146985Sthompsa	struct bridge_iflist *bif;
676146985Sthompsa
677146985Sthompsa	BRIDGE_LOCK(sc);
678146985Sthompsa
679146985Sthompsa	bridge_stop(ifp, 1);
680149522Sthompsa	ifp->if_flags &= ~IFF_UP;
681146985Sthompsa
682146985Sthompsa	while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
683151594Sthompsa		bridge_delete_member(sc, bif, 0);
684146985Sthompsa
685153408Sthompsa	while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
686153494Sthompsa		bridge_delete_span(sc, bif);
687153408Sthompsa	}
688153408Sthompsa
689146985Sthompsa	BRIDGE_UNLOCK(sc);
690146985Sthompsa
691149253Sthompsa	callout_drain(&sc->sc_brcallout);
692149253Sthompsa
693153494Sthompsa	mtx_lock(&bridge_list_mtx);
694153494Sthompsa	LIST_REMOVE(sc, sc_list);
695153494Sthompsa	mtx_unlock(&bridge_list_mtx);
696153494Sthompsa
697160703Sthompsa	bstp_detach(&sc->sc_stp);
698146985Sthompsa	ether_ifdetach(ifp);
699227459Sbrooks	if_free(ifp);
700146985Sthompsa
701146985Sthompsa	/* Tear down the routing table. */
702146985Sthompsa	bridge_rtable_fini(sc);
703146985Sthompsa
704146985Sthompsa	BRIDGE_LOCK_DESTROY(sc);
705146985Sthompsa	free(sc, M_DEVBUF);
706146985Sthompsa}
707146985Sthompsa
708146985Sthompsa/*
709146985Sthompsa * bridge_ioctl:
710146985Sthompsa *
711146985Sthompsa *	Handle a control request from the operator.
712146985Sthompsa */
713151313Sthompsastatic int
714146985Sthompsabridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
715146985Sthompsa{
716146985Sthompsa	struct bridge_softc *sc = ifp->if_softc;
717203272Shrs	struct ifreq *ifr = (struct ifreq *)data;
718203272Shrs	struct bridge_iflist *bif;
719146985Sthompsa	struct thread *td = curthread;
720146985Sthompsa	union {
721146985Sthompsa		struct ifbreq ifbreq;
722146985Sthompsa		struct ifbifconf ifbifconf;
723146985Sthompsa		struct ifbareq ifbareq;
724146985Sthompsa		struct ifbaconf ifbaconf;
725146985Sthompsa		struct ifbrparam ifbrparam;
726164653Sthompsa		struct ifbropreq ifbropreq;
727146985Sthompsa	} args;
728146985Sthompsa	struct ifdrv *ifd = (struct ifdrv *) data;
729146985Sthompsa	const struct bridge_control *bc;
730146985Sthompsa	int error = 0;
731146985Sthompsa
732146985Sthompsa	switch (cmd) {
733146985Sthompsa
734149829Sthompsa	case SIOCADDMULTI:
735149829Sthompsa	case SIOCDELMULTI:
736149829Sthompsa		break;
737149829Sthompsa
738146985Sthompsa	case SIOCGDRVSPEC:
739146985Sthompsa	case SIOCSDRVSPEC:
740146985Sthompsa		if (ifd->ifd_cmd >= bridge_control_table_size) {
741146985Sthompsa			error = EINVAL;
742146985Sthompsa			break;
743146985Sthompsa		}
744146985Sthompsa		bc = &bridge_control_table[ifd->ifd_cmd];
745146985Sthompsa
746146985Sthompsa		if (cmd == SIOCGDRVSPEC &&
747146985Sthompsa		    (bc->bc_flags & BC_F_COPYOUT) == 0) {
748146985Sthompsa			error = EINVAL;
749146985Sthompsa			break;
750146985Sthompsa		}
751146985Sthompsa		else if (cmd == SIOCSDRVSPEC &&
752146985Sthompsa		    (bc->bc_flags & BC_F_COPYOUT) != 0) {
753146985Sthompsa			error = EINVAL;
754146985Sthompsa			break;
755146985Sthompsa		}
756146985Sthompsa
757146985Sthompsa		if (bc->bc_flags & BC_F_SUSER) {
758164033Srwatson			error = priv_check(td, PRIV_NET_BRIDGE);
759146985Sthompsa			if (error)
760146985Sthompsa				break;
761146985Sthompsa		}
762146985Sthompsa
763146985Sthompsa		if (ifd->ifd_len != bc->bc_argsize ||
764146985Sthompsa		    ifd->ifd_len > sizeof(args)) {
765146985Sthompsa			error = EINVAL;
766146985Sthompsa			break;
767146985Sthompsa		}
768146985Sthompsa
769158667Sthompsa		bzero(&args, sizeof(args));
770146985Sthompsa		if (bc->bc_flags & BC_F_COPYIN) {
771146985Sthompsa			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
772146985Sthompsa			if (error)
773146985Sthompsa				break;
774146985Sthompsa		}
775146985Sthompsa
776171603Sthompsa		BRIDGE_LOCK(sc);
777146985Sthompsa		error = (*bc->bc_func)(sc, &args);
778171603Sthompsa		BRIDGE_UNLOCK(sc);
779146985Sthompsa		if (error)
780146985Sthompsa			break;
781146985Sthompsa
782146985Sthompsa		if (bc->bc_flags & BC_F_COPYOUT)
783146985Sthompsa			error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
784146985Sthompsa
785146985Sthompsa		break;
786146985Sthompsa
787146985Sthompsa	case SIOCSIFFLAGS:
788148887Srwatson		if (!(ifp->if_flags & IFF_UP) &&
789148887Srwatson		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
790146985Sthompsa			/*
791146985Sthompsa			 * If interface is marked down and it is running,
792146985Sthompsa			 * then stop and disable it.
793146985Sthompsa			 */
794171603Sthompsa			BRIDGE_LOCK(sc);
795146985Sthompsa			bridge_stop(ifp, 1);
796171603Sthompsa			BRIDGE_UNLOCK(sc);
797148887Srwatson		} else if ((ifp->if_flags & IFF_UP) &&
798148887Srwatson		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
799146985Sthompsa			/*
800146985Sthompsa			 * If interface is marked up and it is stopped, then
801146985Sthompsa			 * start it.
802146985Sthompsa			 */
803147634Sthompsa			(*ifp->if_init)(sc);
804146985Sthompsa		}
805146985Sthompsa		break;
806146985Sthompsa
807146985Sthompsa	case SIOCSIFMTU:
808203272Shrs		if (ifr->ifr_mtu < 576) {
809203272Shrs			error = EINVAL;
810203272Shrs			break;
811203272Shrs		}
812203272Shrs		if (LIST_EMPTY(&sc->sc_iflist)) {
813203272Shrs			sc->sc_ifp->if_mtu = ifr->ifr_mtu;
814203272Shrs			break;
815203272Shrs		}
816203272Shrs		BRIDGE_LOCK(sc);
817203272Shrs		LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
818203272Shrs			if (bif->bif_ifp->if_mtu != ifr->ifr_mtu) {
819203272Shrs				log(LOG_NOTICE, "%s: invalid MTU: %lu(%s)"
820203272Shrs				    " != %d\n", sc->sc_ifp->if_xname,
821203272Shrs				    bif->bif_ifp->if_mtu,
822203272Shrs				    bif->bif_ifp->if_xname, ifr->ifr_mtu);
823203272Shrs				error = EINVAL;
824203272Shrs				break;
825203272Shrs			}
826203272Shrs		}
827203272Shrs		if (!error)
828203272Shrs			sc->sc_ifp->if_mtu = ifr->ifr_mtu;
829203272Shrs		BRIDGE_UNLOCK(sc);
830146985Sthompsa		break;
831146985Sthompsa	default:
832153497Sthompsa		/*
833146985Sthompsa		 * drop the lock as ether_ioctl() will call bridge_start() and
834146985Sthompsa		 * cause the lock to be recursed.
835146985Sthompsa		 */
836146985Sthompsa		error = ether_ioctl(ifp, cmd, data);
837146985Sthompsa		break;
838146985Sthompsa	}
839146985Sthompsa
840146985Sthompsa	return (error);
841146985Sthompsa}
842146985Sthompsa
843146985Sthompsa/*
844154336Sthompsa * bridge_mutecaps:
845154336Sthompsa *
846154336Sthompsa *	Clear or restore unwanted capabilities on the member interface
847154336Sthompsa */
848154336Sthompsastatic void
849180220Sthompsabridge_mutecaps(struct bridge_softc *sc)
850154336Sthompsa{
851180220Sthompsa	struct bridge_iflist *bif;
852180220Sthompsa	int enabled, mask;
853180220Sthompsa
854180220Sthompsa	/* Initial bitmask of capabilities to test */
855180220Sthompsa	mask = BRIDGE_IFCAPS_MASK;
856180220Sthompsa
857180220Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
858180220Sthompsa		/* Every member must support it or its disabled */
859180220Sthompsa		mask &= bif->bif_savedcaps;
860180220Sthompsa	}
861180220Sthompsa
862180220Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
863180220Sthompsa		enabled = bif->bif_ifp->if_capenable;
864196519Sjfv		enabled &= ~BRIDGE_IFCAPS_STRIP;
865180220Sthompsa		/* strip off mask bits and enable them again if allowed */
866180220Sthompsa		enabled &= ~BRIDGE_IFCAPS_MASK;
867180220Sthompsa		enabled |= mask;
868180220Sthompsa		bridge_set_ifcap(sc, bif, enabled);
869180220Sthompsa	}
870180220Sthompsa
871180220Sthompsa}
872180220Sthompsa
873180220Sthompsastatic void
874180220Sthompsabridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set)
875180220Sthompsa{
876154336Sthompsa	struct ifnet *ifp = bif->bif_ifp;
877154336Sthompsa	struct ifreq ifr;
878154336Sthompsa	int error;
879154336Sthompsa
880158667Sthompsa	bzero(&ifr, sizeof(ifr));
881180220Sthompsa	ifr.ifr_reqcap = set;
882154336Sthompsa
883180220Sthompsa	if (ifp->if_capenable != set) {
884154336Sthompsa		error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
885180220Sthompsa		if (error)
886180220Sthompsa			if_printf(sc->sc_ifp,
887180220Sthompsa			    "error setting interface capabilities on %s\n",
888180220Sthompsa			    ifp->if_xname);
889154336Sthompsa	}
890154336Sthompsa}
891154336Sthompsa
892154336Sthompsa/*
893146985Sthompsa * bridge_lookup_member:
894146985Sthompsa *
895146985Sthompsa *	Lookup a bridge member interface.
896146985Sthompsa */
897151345Sthompsastatic struct bridge_iflist *
898146985Sthompsabridge_lookup_member(struct bridge_softc *sc, const char *name)
899146985Sthompsa{
900146985Sthompsa	struct bridge_iflist *bif;
901146985Sthompsa	struct ifnet *ifp;
902146985Sthompsa
903146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
904146985Sthompsa
905146985Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
906146985Sthompsa		ifp = bif->bif_ifp;
907146985Sthompsa		if (strcmp(ifp->if_xname, name) == 0)
908146985Sthompsa			return (bif);
909146985Sthompsa	}
910146985Sthompsa
911146985Sthompsa	return (NULL);
912146985Sthompsa}
913146985Sthompsa
914146985Sthompsa/*
915146985Sthompsa * bridge_lookup_member_if:
916146985Sthompsa *
917146985Sthompsa *	Lookup a bridge member interface by ifnet*.
918146985Sthompsa */
919151345Sthompsastatic struct bridge_iflist *
920146985Sthompsabridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
921146985Sthompsa{
922146985Sthompsa	struct bridge_iflist *bif;
923146985Sthompsa
924146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
925146985Sthompsa
926146985Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
927146985Sthompsa		if (bif->bif_ifp == member_ifp)
928146985Sthompsa			return (bif);
929146985Sthompsa	}
930146985Sthompsa
931146985Sthompsa	return (NULL);
932146985Sthompsa}
933146985Sthompsa
934146985Sthompsa/*
935146985Sthompsa * bridge_delete_member:
936146985Sthompsa *
937146985Sthompsa *	Delete the specified member interface.
938146985Sthompsa */
939151313Sthompsastatic void
940151594Sthompsabridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
941151594Sthompsa    int gone)
942146985Sthompsa{
943146985Sthompsa	struct ifnet *ifs = bif->bif_ifp;
944180140Sphilip	struct ifnet *fif = NULL;
945146985Sthompsa
946146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
947146985Sthompsa
948160703Sthompsa	if (bif->bif_flags & IFBIF_STP)
949164626Sthompsa		bstp_disable(&bif->bif_stp);
950160703Sthompsa
951146985Sthompsa	ifs->if_bridge = NULL;
952146985Sthompsa	BRIDGE_XLOCK(sc);
953146985Sthompsa	LIST_REMOVE(bif, bif_next);
954146985Sthompsa	BRIDGE_XDROP(sc);
955146985Sthompsa
956180140Sphilip	/*
957180140Sphilip	 * If removing the interface that gave the bridge its mac address, set
958180140Sphilip	 * the mac address of the bridge to the address of the next member, or
959180140Sphilip	 * to its default address if no members are left.
960180140Sphilip	 */
961188594Sthompsa	if (bridge_inherit_mac && sc->sc_ifaddr == ifs) {
962188594Sthompsa		if (LIST_EMPTY(&sc->sc_iflist)) {
963180140Sphilip			bcopy(sc->sc_defaddr,
964180140Sphilip			    IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
965188594Sthompsa			sc->sc_ifaddr = NULL;
966188594Sthompsa		} else {
967180140Sphilip			fif = LIST_FIRST(&sc->sc_iflist)->bif_ifp;
968180140Sphilip			bcopy(IF_LLADDR(fif),
969180140Sphilip			    IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
970188594Sthompsa			sc->sc_ifaddr = fif;
971180140Sphilip		}
972202588Sthompsa		EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
973180140Sphilip	}
974180140Sphilip
975236916Sthompsa	bridge_linkcheck(sc);
976180220Sthompsa	bridge_mutecaps(sc);	/* recalcuate now this interface is removed */
977146985Sthompsa	bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
978173320Sthompsa	KASSERT(bif->bif_addrcnt == 0,
979173320Sthompsa	    ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt));
980146985Sthompsa
981160901Sthompsa	BRIDGE_UNLOCK(sc);
982191729Sthompsa	if (!gone) {
983191729Sthompsa		switch (ifs->if_type) {
984191729Sthompsa		case IFT_ETHER:
985191729Sthompsa		case IFT_L2VLAN:
986191729Sthompsa			/*
987191729Sthompsa			 * Take the interface out of promiscuous mode.
988191729Sthompsa			 */
989191729Sthompsa			(void) ifpromisc(ifs, 0);
990191729Sthompsa			break;
991191729Sthompsa
992191729Sthompsa		case IFT_GIF:
993191729Sthompsa			break;
994191729Sthompsa
995191729Sthompsa		default:
996191729Sthompsa#ifdef DIAGNOSTIC
997191729Sthompsa			panic("bridge_delete_member: impossible");
998191729Sthompsa#endif
999191729Sthompsa			break;
1000191729Sthompsa		}
1001191729Sthompsa		/* reneable any interface capabilities */
1002191729Sthompsa		bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
1003191729Sthompsa	}
1004164626Sthompsa	bstp_destroy(&bif->bif_stp);	/* prepare to free */
1005160901Sthompsa	BRIDGE_LOCK(sc);
1006146985Sthompsa	free(bif, M_DEVBUF);
1007146985Sthompsa}
1008146985Sthompsa
1009153494Sthompsa/*
1010153494Sthompsa * bridge_delete_span:
1011153494Sthompsa *
1012153494Sthompsa *	Delete the specified span interface.
1013153494Sthompsa */
1014153494Sthompsastatic void
1015153494Sthompsabridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1016153494Sthompsa{
1017153494Sthompsa	BRIDGE_LOCK_ASSERT(sc);
1018153494Sthompsa
1019153494Sthompsa	KASSERT(bif->bif_ifp->if_bridge == NULL,
1020153494Sthompsa	    ("%s: not a span interface", __func__));
1021153494Sthompsa
1022153494Sthompsa	LIST_REMOVE(bif, bif_next);
1023153494Sthompsa	free(bif, M_DEVBUF);
1024153494Sthompsa}
1025153494Sthompsa
1026151313Sthompsastatic int
1027146985Sthompsabridge_ioctl_add(struct bridge_softc *sc, void *arg)
1028146985Sthompsa{
1029146985Sthompsa	struct ifbreq *req = arg;
1030146985Sthompsa	struct bridge_iflist *bif = NULL;
1031146985Sthompsa	struct ifnet *ifs;
1032146985Sthompsa	int error = 0;
1033146985Sthompsa
1034146985Sthompsa	ifs = ifunit(req->ifbr_ifsname);
1035146985Sthompsa	if (ifs == NULL)
1036146985Sthompsa		return (ENOENT);
1037180220Sthompsa	if (ifs->if_ioctl == NULL)	/* must be supported */
1038180220Sthompsa		return (EINVAL);
1039146985Sthompsa
1040153408Sthompsa	/* If it's in the span list, it can't be a member. */
1041153408Sthompsa	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1042153408Sthompsa		if (ifs == bif->bif_ifp)
1043153408Sthompsa			return (EBUSY);
1044153408Sthompsa
1045146985Sthompsa	if (ifs->if_bridge == sc)
1046146985Sthompsa		return (EEXIST);
1047146985Sthompsa
1048146985Sthompsa	if (ifs->if_bridge != NULL)
1049146985Sthompsa		return (EBUSY);
1050146985Sthompsa
1051146985Sthompsa	switch (ifs->if_type) {
1052146985Sthompsa	case IFT_ETHER:
1053146985Sthompsa	case IFT_L2VLAN:
1054153621Sthompsa	case IFT_GIF:
1055191729Sthompsa		/* permitted interface types */
1056153621Sthompsa		break;
1057146985Sthompsa	default:
1058252511Shrs		return (EINVAL);
1059146985Sthompsa	}
1060146985Sthompsa
1061252511Shrs#ifdef INET6
1062252511Shrs	/*
1063252511Shrs	 * Two valid inet6 addresses with link-local scope must not be
1064252511Shrs	 * on the parent interface and the member interfaces at the
1065252511Shrs	 * same time.  This restriction is needed to prevent violation
1066252511Shrs	 * of link-local scope zone.  Attempts to add a member
1067252511Shrs	 * interface which has inet6 addresses when the parent has
1068252511Shrs	 * inet6 triggers removal of all inet6 addresses on the member
1069252511Shrs	 * interface.
1070252511Shrs	 */
1071252511Shrs
1072252511Shrs	/* Check if the parent interface has a link-local scope addr. */
1073253751Shrs	if (V_allow_llz_overlap == 0 &&
1074253751Shrs	    in6ifa_llaonifp(sc->sc_ifp) != NULL) {
1075252511Shrs		/*
1076252511Shrs		 * If any, remove all inet6 addresses from the member
1077252511Shrs		 * interfaces.
1078252511Shrs		 */
1079252511Shrs		BRIDGE_XLOCK(sc);
1080252511Shrs		LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1081252511Shrs 			if (in6ifa_llaonifp(bif->bif_ifp)) {
1082252511Shrs				BRIDGE_UNLOCK(sc);
1083252511Shrs				in6_ifdetach(bif->bif_ifp);
1084252511Shrs				BRIDGE_LOCK(sc);
1085252511Shrs				if_printf(sc->sc_ifp,
1086252511Shrs				    "IPv6 addresses on %s have been removed "
1087252511Shrs				    "before adding it as a member to prevent "
1088252511Shrs				    "IPv6 address scope violation.\n",
1089252511Shrs				    bif->bif_ifp->if_xname);
1090252511Shrs			}
1091252511Shrs		}
1092252511Shrs		BRIDGE_XDROP(sc);
1093252511Shrs		if (in6ifa_llaonifp(ifs)) {
1094252511Shrs			BRIDGE_UNLOCK(sc);
1095252511Shrs			in6_ifdetach(ifs);
1096252511Shrs			BRIDGE_LOCK(sc);
1097252511Shrs			if_printf(sc->sc_ifp,
1098252511Shrs			    "IPv6 addresses on %s have been removed "
1099252511Shrs			    "before adding it as a member to prevent "
1100252511Shrs			    "IPv6 address scope violation.\n",
1101252511Shrs			    ifs->if_xname);
1102252511Shrs		}
1103252511Shrs	}
1104252511Shrs#endif
1105203272Shrs	/* Allow the first Ethernet member to define the MTU */
1106203272Shrs	if (LIST_EMPTY(&sc->sc_iflist))
1107203272Shrs		sc->sc_ifp->if_mtu = ifs->if_mtu;
1108203272Shrs	else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
1109203272Shrs		if_printf(sc->sc_ifp, "invalid MTU: %lu(%s) != %lu\n",
1110203272Shrs		    ifs->if_mtu, ifs->if_xname, sc->sc_ifp->if_mtu);
1111252511Shrs		return (EINVAL);
1112203272Shrs	}
1113203272Shrs
1114252511Shrs	bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1115252511Shrs	if (bif == NULL)
1116252511Shrs		return (ENOMEM);
1117252511Shrs
1118252511Shrs	bif->bif_ifp = ifs;
1119252511Shrs	bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
1120252511Shrs	bif->bif_savedcaps = ifs->if_capenable;
1121252511Shrs
1122180140Sphilip	/*
1123180140Sphilip	 * Assign the interface's MAC address to the bridge if it's the first
1124180140Sphilip	 * member and the MAC address of the bridge has not been changed from
1125180140Sphilip	 * the default randomly generated one.
1126180140Sphilip	 */
1127182862Sthompsa	if (bridge_inherit_mac && LIST_EMPTY(&sc->sc_iflist) &&
1128188594Sthompsa	    !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr, ETHER_ADDR_LEN)) {
1129180140Sphilip		bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1130188594Sthompsa		sc->sc_ifaddr = ifs;
1131202588Sthompsa		EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
1132188594Sthompsa	}
1133180140Sphilip
1134146985Sthompsa	ifs->if_bridge = sc;
1135164626Sthompsa	bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
1136146985Sthompsa	/*
1137146985Sthompsa	 * XXX: XLOCK HERE!?!
1138146985Sthompsa	 *
1139146985Sthompsa	 * NOTE: insert_***HEAD*** should be safe for the traversals.
1140146985Sthompsa	 */
1141146985Sthompsa	LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
1142146985Sthompsa
1143180220Sthompsa	/* Set interface capabilities to the intersection set of all members */
1144180220Sthompsa	bridge_mutecaps(sc);
1145236916Sthompsa	bridge_linkcheck(sc);
1146191729Sthompsa
1147234487Sthompsa	/* Place the interface into promiscuous mode */
1148191729Sthompsa	switch (ifs->if_type) {
1149234487Sthompsa		case IFT_ETHER:
1150234487Sthompsa		case IFT_L2VLAN:
1151236916Sthompsa			BRIDGE_UNLOCK(sc);
1152234487Sthompsa			error = ifpromisc(ifs, 1);
1153236916Sthompsa			BRIDGE_LOCK(sc);
1154234487Sthompsa			break;
1155191729Sthompsa	}
1156252511Shrs
1157252511Shrs	if (error) {
1158191729Sthompsa		bridge_delete_member(sc, bif, 0);
1159252511Shrs		free(bif, M_DEVBUF);
1160146985Sthompsa	}
1161146985Sthompsa	return (error);
1162146985Sthompsa}
1163146985Sthompsa
1164151313Sthompsastatic int
1165146985Sthompsabridge_ioctl_del(struct bridge_softc *sc, void *arg)
1166146985Sthompsa{
1167146985Sthompsa	struct ifbreq *req = arg;
1168146985Sthompsa	struct bridge_iflist *bif;
1169146985Sthompsa
1170146985Sthompsa	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1171146985Sthompsa	if (bif == NULL)
1172146985Sthompsa		return (ENOENT);
1173146985Sthompsa
1174151594Sthompsa	bridge_delete_member(sc, bif, 0);
1175146985Sthompsa
1176146985Sthompsa	return (0);
1177146985Sthompsa}
1178146985Sthompsa
1179151313Sthompsastatic int
1180146985Sthompsabridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1181146985Sthompsa{
1182146985Sthompsa	struct ifbreq *req = arg;
1183146985Sthompsa	struct bridge_iflist *bif;
1184163863Sthompsa	struct bstp_port *bp;
1185146985Sthompsa
1186146985Sthompsa	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1187146985Sthompsa	if (bif == NULL)
1188146985Sthompsa		return (ENOENT);
1189146985Sthompsa
1190163863Sthompsa	bp = &bif->bif_stp;
1191146985Sthompsa	req->ifbr_ifsflags = bif->bif_flags;
1192163863Sthompsa	req->ifbr_state = bp->bp_state;
1193163863Sthompsa	req->ifbr_priority = bp->bp_priority;
1194163863Sthompsa	req->ifbr_path_cost = bp->bp_path_cost;
1195163863Sthompsa	req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1196163863Sthompsa	req->ifbr_proto = bp->bp_protover;
1197163863Sthompsa	req->ifbr_role = bp->bp_role;
1198163863Sthompsa	req->ifbr_stpflags = bp->bp_flags;
1199173320Sthompsa	req->ifbr_addrcnt = bif->bif_addrcnt;
1200173320Sthompsa	req->ifbr_addrmax = bif->bif_addrmax;
1201173320Sthompsa	req->ifbr_addrexceeded = bif->bif_addrexceeded;
1202146985Sthompsa
1203164653Sthompsa	/* Copy STP state options as flags */
1204164653Sthompsa	if (bp->bp_operedge)
1205164653Sthompsa		req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
1206164653Sthompsa	if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
1207164653Sthompsa		req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
1208165105Sthompsa	if (bp->bp_ptp_link)
1209165105Sthompsa		req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
1210165105Sthompsa	if (bp->bp_flags & BSTP_PORT_AUTOPTP)
1211165105Sthompsa		req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
1212164880Ssyrinx	if (bp->bp_flags & BSTP_PORT_ADMEDGE)
1213164880Ssyrinx		req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
1214164880Ssyrinx	if (bp->bp_flags & BSTP_PORT_ADMCOST)
1215164880Ssyrinx		req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
1216146985Sthompsa	return (0);
1217146985Sthompsa}
1218146985Sthompsa
1219151313Sthompsastatic int
1220146985Sthompsabridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1221146985Sthompsa{
1222146985Sthompsa	struct ifbreq *req = arg;
1223146985Sthompsa	struct bridge_iflist *bif;
1224164653Sthompsa	struct bstp_port *bp;
1225160703Sthompsa	int error;
1226146985Sthompsa
1227146985Sthompsa	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1228146985Sthompsa	if (bif == NULL)
1229146985Sthompsa		return (ENOENT);
1230164653Sthompsa	bp = &bif->bif_stp;
1231146985Sthompsa
1232153408Sthompsa	if (req->ifbr_ifsflags & IFBIF_SPAN)
1233153408Sthompsa		/* SPAN is readonly */
1234153408Sthompsa		return (EINVAL);
1235153408Sthompsa
1236146985Sthompsa	if (req->ifbr_ifsflags & IFBIF_STP) {
1237160703Sthompsa		if ((bif->bif_flags & IFBIF_STP) == 0) {
1238164626Sthompsa			error = bstp_enable(&bif->bif_stp);
1239160703Sthompsa			if (error)
1240160703Sthompsa				return (error);
1241146985Sthompsa		}
1242160703Sthompsa	} else {
1243160703Sthompsa		if ((bif->bif_flags & IFBIF_STP) != 0)
1244164626Sthompsa			bstp_disable(&bif->bif_stp);
1245146985Sthompsa	}
1246146985Sthompsa
1247164653Sthompsa	/* Pass on STP flags */
1248164653Sthompsa	bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
1249164653Sthompsa	bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
1250165105Sthompsa	bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
1251165105Sthompsa	bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
1252146985Sthompsa
1253164653Sthompsa	/* Save the bits relating to the bridge */
1254164653Sthompsa	bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK;
1255164653Sthompsa
1256146985Sthompsa	return (0);
1257146985Sthompsa}
1258146985Sthompsa
1259151313Sthompsastatic int
1260146985Sthompsabridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1261146985Sthompsa{
1262146985Sthompsa	struct ifbrparam *param = arg;
1263146985Sthompsa
1264146985Sthompsa	sc->sc_brtmax = param->ifbrp_csize;
1265146985Sthompsa	bridge_rttrim(sc);
1266146985Sthompsa
1267146985Sthompsa	return (0);
1268146985Sthompsa}
1269146985Sthompsa
1270151313Sthompsastatic int
1271146985Sthompsabridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1272146985Sthompsa{
1273146985Sthompsa	struct ifbrparam *param = arg;
1274146985Sthompsa
1275146985Sthompsa	param->ifbrp_csize = sc->sc_brtmax;
1276146985Sthompsa
1277146985Sthompsa	return (0);
1278146985Sthompsa}
1279146985Sthompsa
1280151313Sthompsastatic int
1281146985Sthompsabridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1282146985Sthompsa{
1283146985Sthompsa	struct ifbifconf *bifc = arg;
1284146985Sthompsa	struct bridge_iflist *bif;
1285146985Sthompsa	struct ifbreq breq;
1286171603Sthompsa	char *buf, *outbuf;
1287171603Sthompsa	int count, buflen, len, error = 0;
1288146985Sthompsa
1289146985Sthompsa	count = 0;
1290146985Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
1291146985Sthompsa		count++;
1292153408Sthompsa	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1293153408Sthompsa		count++;
1294146985Sthompsa
1295171603Sthompsa	buflen = sizeof(breq) * count;
1296146985Sthompsa	if (bifc->ifbic_len == 0) {
1297171603Sthompsa		bifc->ifbic_len = buflen;
1298146985Sthompsa		return (0);
1299146985Sthompsa	}
1300171603Sthompsa	BRIDGE_UNLOCK(sc);
1301171603Sthompsa	outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1302171603Sthompsa	BRIDGE_LOCK(sc);
1303146985Sthompsa
1304146985Sthompsa	count = 0;
1305171603Sthompsa	buf = outbuf;
1306171603Sthompsa	len = min(bifc->ifbic_len, buflen);
1307158667Sthompsa	bzero(&breq, sizeof(breq));
1308146985Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1309146985Sthompsa		if (len < sizeof(breq))
1310146985Sthompsa			break;
1311146985Sthompsa
1312146985Sthompsa		strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1313146985Sthompsa		    sizeof(breq.ifbr_ifsname));
1314164653Sthompsa		/* Fill in the ifbreq structure */
1315164653Sthompsa		error = bridge_ioctl_gifflags(sc, &breq);
1316164653Sthompsa		if (error)
1317164653Sthompsa			break;
1318171603Sthompsa		memcpy(buf, &breq, sizeof(breq));
1319146985Sthompsa		count++;
1320171603Sthompsa		buf += sizeof(breq);
1321146985Sthompsa		len -= sizeof(breq);
1322146985Sthompsa	}
1323153408Sthompsa	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1324153408Sthompsa		if (len < sizeof(breq))
1325153408Sthompsa			break;
1326146985Sthompsa
1327153408Sthompsa		strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1328153408Sthompsa		    sizeof(breq.ifbr_ifsname));
1329153408Sthompsa		breq.ifbr_ifsflags = bif->bif_flags;
1330163863Sthompsa		breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1331171603Sthompsa		memcpy(buf, &breq, sizeof(breq));
1332153408Sthompsa		count++;
1333171603Sthompsa		buf += sizeof(breq);
1334153408Sthompsa		len -= sizeof(breq);
1335153408Sthompsa	}
1336153408Sthompsa
1337171603Sthompsa	BRIDGE_UNLOCK(sc);
1338146985Sthompsa	bifc->ifbic_len = sizeof(breq) * count;
1339171603Sthompsa	error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len);
1340171603Sthompsa	BRIDGE_LOCK(sc);
1341171603Sthompsa	free(outbuf, M_TEMP);
1342146985Sthompsa	return (error);
1343146985Sthompsa}
1344146985Sthompsa
1345151313Sthompsastatic int
1346146985Sthompsabridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1347146985Sthompsa{
1348146985Sthompsa	struct ifbaconf *bac = arg;
1349146985Sthompsa	struct bridge_rtnode *brt;
1350146985Sthompsa	struct ifbareq bareq;
1351171603Sthompsa	char *buf, *outbuf;
1352171603Sthompsa	int count, buflen, len, error = 0;
1353146985Sthompsa
1354146985Sthompsa	if (bac->ifbac_len == 0)
1355146985Sthompsa		return (0);
1356146985Sthompsa
1357171603Sthompsa	count = 0;
1358171603Sthompsa	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list)
1359171603Sthompsa		count++;
1360171603Sthompsa	buflen = sizeof(bareq) * count;
1361171603Sthompsa
1362171603Sthompsa	BRIDGE_UNLOCK(sc);
1363171603Sthompsa	outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1364171603Sthompsa	BRIDGE_LOCK(sc);
1365171603Sthompsa
1366171603Sthompsa	count = 0;
1367171603Sthompsa	buf = outbuf;
1368171603Sthompsa	len = min(bac->ifbac_len, buflen);
1369158667Sthompsa	bzero(&bareq, sizeof(bareq));
1370146985Sthompsa	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1371146985Sthompsa		if (len < sizeof(bareq))
1372146985Sthompsa			goto out;
1373146985Sthompsa		strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1374146985Sthompsa		    sizeof(bareq.ifba_ifsname));
1375146985Sthompsa		memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1376170681Sthompsa		bareq.ifba_vlan = brt->brt_vlan;
1377146985Sthompsa		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1378153976Sthompsa				time_uptime < brt->brt_expire)
1379153976Sthompsa			bareq.ifba_expire = brt->brt_expire - time_uptime;
1380146985Sthompsa		else
1381146985Sthompsa			bareq.ifba_expire = 0;
1382146985Sthompsa		bareq.ifba_flags = brt->brt_flags;
1383146985Sthompsa
1384171603Sthompsa		memcpy(buf, &bareq, sizeof(bareq));
1385146985Sthompsa		count++;
1386171603Sthompsa		buf += sizeof(bareq);
1387146985Sthompsa		len -= sizeof(bareq);
1388146985Sthompsa	}
1389153497Sthompsaout:
1390171603Sthompsa	BRIDGE_UNLOCK(sc);
1391146985Sthompsa	bac->ifbac_len = sizeof(bareq) * count;
1392171603Sthompsa	error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len);
1393171603Sthompsa	BRIDGE_LOCK(sc);
1394171603Sthompsa	free(outbuf, M_TEMP);
1395146985Sthompsa	return (error);
1396146985Sthompsa}
1397146985Sthompsa
1398151313Sthompsastatic int
1399146985Sthompsabridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1400146985Sthompsa{
1401146985Sthompsa	struct ifbareq *req = arg;
1402146985Sthompsa	struct bridge_iflist *bif;
1403146985Sthompsa	int error;
1404146985Sthompsa
1405146985Sthompsa	bif = bridge_lookup_member(sc, req->ifba_ifsname);
1406146985Sthompsa	if (bif == NULL)
1407146985Sthompsa		return (ENOENT);
1408146985Sthompsa
1409170681Sthompsa	error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
1410146985Sthompsa	    req->ifba_flags);
1411146985Sthompsa
1412146985Sthompsa	return (error);
1413146985Sthompsa}
1414146985Sthompsa
1415151313Sthompsastatic int
1416146985Sthompsabridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1417146985Sthompsa{
1418146985Sthompsa	struct ifbrparam *param = arg;
1419146985Sthompsa
1420146985Sthompsa	sc->sc_brttimeout = param->ifbrp_ctime;
1421146985Sthompsa	return (0);
1422146985Sthompsa}
1423146985Sthompsa
1424151313Sthompsastatic int
1425146985Sthompsabridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1426146985Sthompsa{
1427146985Sthompsa	struct ifbrparam *param = arg;
1428146985Sthompsa
1429146985Sthompsa	param->ifbrp_ctime = sc->sc_brttimeout;
1430146985Sthompsa	return (0);
1431146985Sthompsa}
1432146985Sthompsa
1433151313Sthompsastatic int
1434146985Sthompsabridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1435146985Sthompsa{
1436146985Sthompsa	struct ifbareq *req = arg;
1437146985Sthompsa
1438170681Sthompsa	return (bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan));
1439146985Sthompsa}
1440146985Sthompsa
1441151313Sthompsastatic int
1442146985Sthompsabridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1443146985Sthompsa{
1444146985Sthompsa	struct ifbreq *req = arg;
1445146985Sthompsa
1446146985Sthompsa	bridge_rtflush(sc, req->ifbr_ifsflags);
1447146985Sthompsa	return (0);
1448146985Sthompsa}
1449146985Sthompsa
1450151313Sthompsastatic int
1451146985Sthompsabridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1452146985Sthompsa{
1453146985Sthompsa	struct ifbrparam *param = arg;
1454160703Sthompsa	struct bstp_state *bs = &sc->sc_stp;
1455146985Sthompsa
1456160703Sthompsa	param->ifbrp_prio = bs->bs_bridge_priority;
1457146985Sthompsa	return (0);
1458146985Sthompsa}
1459146985Sthompsa
1460151313Sthompsastatic int
1461146985Sthompsabridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1462146985Sthompsa{
1463146985Sthompsa	struct ifbrparam *param = arg;
1464146985Sthompsa
1465163863Sthompsa	return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio));
1466146985Sthompsa}
1467146985Sthompsa
1468151313Sthompsastatic int
1469146985Sthompsabridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1470146985Sthompsa{
1471146985Sthompsa	struct ifbrparam *param = arg;
1472160703Sthompsa	struct bstp_state *bs = &sc->sc_stp;
1473146985Sthompsa
1474163863Sthompsa	param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
1475146985Sthompsa	return (0);
1476146985Sthompsa}
1477146985Sthompsa
1478151313Sthompsastatic int
1479146985Sthompsabridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1480146985Sthompsa{
1481146985Sthompsa	struct ifbrparam *param = arg;
1482146985Sthompsa
1483163863Sthompsa	return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime));
1484146985Sthompsa}
1485146985Sthompsa
1486151313Sthompsastatic int
1487146985Sthompsabridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1488146985Sthompsa{
1489146985Sthompsa	struct ifbrparam *param = arg;
1490160703Sthompsa	struct bstp_state *bs = &sc->sc_stp;
1491146985Sthompsa
1492163863Sthompsa	param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
1493146985Sthompsa	return (0);
1494146985Sthompsa}
1495146985Sthompsa
1496151313Sthompsastatic int
1497146985Sthompsabridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1498146985Sthompsa{
1499146985Sthompsa	struct ifbrparam *param = arg;
1500146985Sthompsa
1501163863Sthompsa	return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay));
1502146985Sthompsa}
1503146985Sthompsa
1504151313Sthompsastatic int
1505146985Sthompsabridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1506146985Sthompsa{
1507146985Sthompsa	struct ifbrparam *param = arg;
1508160703Sthompsa	struct bstp_state *bs = &sc->sc_stp;
1509146985Sthompsa
1510160703Sthompsa	param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
1511146985Sthompsa	return (0);
1512146985Sthompsa}
1513146985Sthompsa
1514151313Sthompsastatic int
1515146985Sthompsabridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1516146985Sthompsa{
1517146985Sthompsa	struct ifbrparam *param = arg;
1518146985Sthompsa
1519163863Sthompsa	return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage));
1520146985Sthompsa}
1521146985Sthompsa
1522151313Sthompsastatic int
1523146985Sthompsabridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1524146985Sthompsa{
1525146985Sthompsa	struct ifbreq *req = arg;
1526146985Sthompsa	struct bridge_iflist *bif;
1527146985Sthompsa
1528146985Sthompsa	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1529146985Sthompsa	if (bif == NULL)
1530146985Sthompsa		return (ENOENT);
1531146985Sthompsa
1532163863Sthompsa	return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority));
1533146985Sthompsa}
1534146985Sthompsa
1535151313Sthompsastatic int
1536146985Sthompsabridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1537146985Sthompsa{
1538146985Sthompsa	struct ifbreq *req = arg;
1539146985Sthompsa	struct bridge_iflist *bif;
1540146985Sthompsa
1541146985Sthompsa	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1542146985Sthompsa	if (bif == NULL)
1543146985Sthompsa		return (ENOENT);
1544146985Sthompsa
1545163863Sthompsa	return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost));
1546146985Sthompsa}
1547146985Sthompsa
1548153408Sthompsastatic int
1549173320Sthompsabridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg)
1550173320Sthompsa{
1551173320Sthompsa	struct ifbreq *req = arg;
1552173320Sthompsa	struct bridge_iflist *bif;
1553173320Sthompsa
1554173320Sthompsa	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1555173320Sthompsa	if (bif == NULL)
1556173320Sthompsa		return (ENOENT);
1557173320Sthompsa
1558173320Sthompsa	bif->bif_addrmax = req->ifbr_addrmax;
1559173320Sthompsa	return (0);
1560173320Sthompsa}
1561173320Sthompsa
1562173320Sthompsastatic int
1563153408Sthompsabridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1564153408Sthompsa{
1565153408Sthompsa	struct ifbreq *req = arg;
1566153408Sthompsa	struct bridge_iflist *bif = NULL;
1567153408Sthompsa	struct ifnet *ifs;
1568153408Sthompsa
1569153408Sthompsa	ifs = ifunit(req->ifbr_ifsname);
1570153408Sthompsa	if (ifs == NULL)
1571153408Sthompsa		return (ENOENT);
1572153408Sthompsa
1573153408Sthompsa	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1574153408Sthompsa		if (ifs == bif->bif_ifp)
1575153408Sthompsa			return (EBUSY);
1576153408Sthompsa
1577153408Sthompsa	if (ifs->if_bridge != NULL)
1578153408Sthompsa		return (EBUSY);
1579153408Sthompsa
1580153408Sthompsa	switch (ifs->if_type) {
1581153408Sthompsa		case IFT_ETHER:
1582159807Sthompsa		case IFT_GIF:
1583153408Sthompsa		case IFT_L2VLAN:
1584153408Sthompsa			break;
1585153408Sthompsa		default:
1586153408Sthompsa			return (EINVAL);
1587153408Sthompsa	}
1588153408Sthompsa
1589153408Sthompsa	bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1590153408Sthompsa	if (bif == NULL)
1591153408Sthompsa		return (ENOMEM);
1592153408Sthompsa
1593153408Sthompsa	bif->bif_ifp = ifs;
1594153408Sthompsa	bif->bif_flags = IFBIF_SPAN;
1595153408Sthompsa
1596153408Sthompsa	LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1597153408Sthompsa
1598153408Sthompsa	return (0);
1599153408Sthompsa}
1600153408Sthompsa
1601153408Sthompsastatic int
1602153408Sthompsabridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1603153408Sthompsa{
1604153408Sthompsa	struct ifbreq *req = arg;
1605153408Sthompsa	struct bridge_iflist *bif;
1606153408Sthompsa	struct ifnet *ifs;
1607153408Sthompsa
1608153408Sthompsa	ifs = ifunit(req->ifbr_ifsname);
1609153408Sthompsa	if (ifs == NULL)
1610153408Sthompsa		return (ENOENT);
1611153408Sthompsa
1612153408Sthompsa	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1613153408Sthompsa		if (ifs == bif->bif_ifp)
1614153408Sthompsa			break;
1615153408Sthompsa
1616153408Sthompsa	if (bif == NULL)
1617153408Sthompsa		return (ENOENT);
1618153408Sthompsa
1619153494Sthompsa	bridge_delete_span(sc, bif);
1620153408Sthompsa
1621153408Sthompsa	return (0);
1622153408Sthompsa}
1623153408Sthompsa
1624160867Sthompsastatic int
1625160867Sthompsabridge_ioctl_gbparam(struct bridge_softc *sc, void *arg)
1626160867Sthompsa{
1627160867Sthompsa	struct ifbropreq *req = arg;
1628163863Sthompsa	struct bstp_state *bs = &sc->sc_stp;
1629160867Sthompsa	struct bstp_port *root_port;
1630160867Sthompsa
1631163863Sthompsa	req->ifbop_maxage = bs->bs_bridge_max_age >> 8;
1632163863Sthompsa	req->ifbop_hellotime = bs->bs_bridge_htime >> 8;
1633163863Sthompsa	req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;
1634160867Sthompsa
1635163863Sthompsa	root_port = bs->bs_root_port;
1636160867Sthompsa	if (root_port == NULL)
1637160867Sthompsa		req->ifbop_root_port = 0;
1638160867Sthompsa	else
1639160867Sthompsa		req->ifbop_root_port = root_port->bp_ifp->if_index;
1640160867Sthompsa
1641163863Sthompsa	req->ifbop_holdcount = bs->bs_txholdcount;
1642163863Sthompsa	req->ifbop_priority = bs->bs_bridge_priority;
1643163863Sthompsa	req->ifbop_protocol = bs->bs_protover;
1644163863Sthompsa	req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;
1645164653Sthompsa	req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id;
1646163863Sthompsa	req->ifbop_designated_root = bs->bs_root_pv.pv_root_id;
1647164653Sthompsa	req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id;
1648163863Sthompsa	req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;
1649163863Sthompsa	req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;
1650160867Sthompsa
1651160867Sthompsa	return (0);
1652160867Sthompsa}
1653160867Sthompsa
1654160867Sthompsastatic int
1655160867Sthompsabridge_ioctl_grte(struct bridge_softc *sc, void *arg)
1656160867Sthompsa{
1657160867Sthompsa	struct ifbrparam *param = arg;
1658160867Sthompsa
1659160867Sthompsa	param->ifbrp_cexceeded = sc->sc_brtexceeded;
1660160867Sthompsa	return (0);
1661160867Sthompsa}
1662160867Sthompsa
1663160867Sthompsastatic int
1664160867Sthompsabridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg)
1665160867Sthompsa{
1666160867Sthompsa	struct ifbpstpconf *bifstp = arg;
1667160867Sthompsa	struct bridge_iflist *bif;
1668163863Sthompsa	struct bstp_port *bp;
1669160867Sthompsa	struct ifbpstpreq bpreq;
1670171603Sthompsa	char *buf, *outbuf;
1671171603Sthompsa	int count, buflen, len, error = 0;
1672160867Sthompsa
1673160867Sthompsa	count = 0;
1674160867Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1675160867Sthompsa		if ((bif->bif_flags & IFBIF_STP) != 0)
1676160867Sthompsa			count++;
1677160867Sthompsa	}
1678160867Sthompsa
1679171603Sthompsa	buflen = sizeof(bpreq) * count;
1680160867Sthompsa	if (bifstp->ifbpstp_len == 0) {
1681171603Sthompsa		bifstp->ifbpstp_len = buflen;
1682160867Sthompsa		return (0);
1683160867Sthompsa	}
1684160867Sthompsa
1685171603Sthompsa	BRIDGE_UNLOCK(sc);
1686171603Sthompsa	outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1687171603Sthompsa	BRIDGE_LOCK(sc);
1688171603Sthompsa
1689160867Sthompsa	count = 0;
1690171603Sthompsa	buf = outbuf;
1691171603Sthompsa	len = min(bifstp->ifbpstp_len, buflen);
1692160867Sthompsa	bzero(&bpreq, sizeof(bpreq));
1693160867Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1694160867Sthompsa		if (len < sizeof(bpreq))
1695160867Sthompsa			break;
1696160867Sthompsa
1697160867Sthompsa		if ((bif->bif_flags & IFBIF_STP) == 0)
1698160867Sthompsa			continue;
1699160867Sthompsa
1700163863Sthompsa		bp = &bif->bif_stp;
1701163863Sthompsa		bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff;
1702163863Sthompsa		bpreq.ifbp_fwd_trans = bp->bp_forward_transitions;
1703163863Sthompsa		bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost;
1704163863Sthompsa		bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id;
1705163863Sthompsa		bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id;
1706163863Sthompsa		bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id;
1707160867Sthompsa
1708171603Sthompsa		memcpy(buf, &bpreq, sizeof(bpreq));
1709160867Sthompsa		count++;
1710171603Sthompsa		buf += sizeof(bpreq);
1711160867Sthompsa		len -= sizeof(bpreq);
1712160867Sthompsa	}
1713160867Sthompsa
1714171603Sthompsa	BRIDGE_UNLOCK(sc);
1715160867Sthompsa	bifstp->ifbpstp_len = sizeof(bpreq) * count;
1716171603Sthompsa	error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len);
1717171603Sthompsa	BRIDGE_LOCK(sc);
1718171603Sthompsa	free(outbuf, M_TEMP);
1719160867Sthompsa	return (error);
1720160867Sthompsa}
1721160867Sthompsa
1722163863Sthompsastatic int
1723163863Sthompsabridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
1724163863Sthompsa{
1725163863Sthompsa	struct ifbrparam *param = arg;
1726163863Sthompsa
1727163863Sthompsa	return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto));
1728163863Sthompsa}
1729163863Sthompsa
1730163863Sthompsastatic int
1731163863Sthompsabridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
1732163863Sthompsa{
1733163863Sthompsa	struct ifbrparam *param = arg;
1734163863Sthompsa
1735163863Sthompsa	return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc));
1736163863Sthompsa}
1737163863Sthompsa
1738146985Sthompsa/*
1739146985Sthompsa * bridge_ifdetach:
1740146985Sthompsa *
1741146985Sthompsa *	Detach an interface from a bridge.  Called when a member
1742146985Sthompsa *	interface is detaching.
1743146985Sthompsa */
1744151313Sthompsastatic void
1745153494Sthompsabridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1746146985Sthompsa{
1747146985Sthompsa	struct bridge_softc *sc = ifp->if_bridge;
1748151594Sthompsa	struct bridge_iflist *bif;
1749146985Sthompsa
1750248851Smarkj	if (ifp->if_flags & IFF_RENAMING)
1751248851Smarkj		return;
1752248851Smarkj
1753153494Sthompsa	/* Check if the interface is a bridge member */
1754153494Sthompsa	if (sc != NULL) {
1755153494Sthompsa		BRIDGE_LOCK(sc);
1756146985Sthompsa
1757153494Sthompsa		bif = bridge_lookup_member_if(sc, ifp);
1758153494Sthompsa		if (bif != NULL)
1759153494Sthompsa			bridge_delete_member(sc, bif, 1);
1760153494Sthompsa
1761153494Sthompsa		BRIDGE_UNLOCK(sc);
1762151594Sthompsa		return;
1763153494Sthompsa	}
1764146985Sthompsa
1765153494Sthompsa	/* Check if the interface is a span port */
1766153494Sthompsa	mtx_lock(&bridge_list_mtx);
1767153494Sthompsa	LIST_FOREACH(sc, &bridge_list, sc_list) {
1768153494Sthompsa		BRIDGE_LOCK(sc);
1769153494Sthompsa		LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1770153494Sthompsa			if (ifp == bif->bif_ifp) {
1771153494Sthompsa				bridge_delete_span(sc, bif);
1772153494Sthompsa				break;
1773153494Sthompsa			}
1774151594Sthompsa
1775153494Sthompsa		BRIDGE_UNLOCK(sc);
1776153494Sthompsa	}
1777153494Sthompsa	mtx_unlock(&bridge_list_mtx);
1778146985Sthompsa}
1779146985Sthompsa
1780146985Sthompsa/*
1781146985Sthompsa * bridge_init:
1782146985Sthompsa *
1783146985Sthompsa *	Initialize a bridge interface.
1784146985Sthompsa */
1785146985Sthompsastatic void
1786146985Sthompsabridge_init(void *xsc)
1787146985Sthompsa{
1788146985Sthompsa	struct bridge_softc *sc = (struct bridge_softc *)xsc;
1789147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1790146985Sthompsa
1791148887Srwatson	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1792146985Sthompsa		return;
1793146985Sthompsa
1794149253Sthompsa	BRIDGE_LOCK(sc);
1795146985Sthompsa	callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1796146985Sthompsa	    bridge_timer, sc);
1797146985Sthompsa
1798148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1799160703Sthompsa	bstp_init(&sc->sc_stp);		/* Initialize Spanning Tree */
1800160703Sthompsa
1801149064Sthompsa	BRIDGE_UNLOCK(sc);
1802146985Sthompsa}
1803146985Sthompsa
1804146985Sthompsa/*
1805146985Sthompsa * bridge_stop:
1806146985Sthompsa *
1807146985Sthompsa *	Stop the bridge interface.
1808146985Sthompsa */
1809151313Sthompsastatic void
1810146985Sthompsabridge_stop(struct ifnet *ifp, int disable)
1811146985Sthompsa{
1812146985Sthompsa	struct bridge_softc *sc = ifp->if_softc;
1813146985Sthompsa
1814149064Sthompsa	BRIDGE_LOCK_ASSERT(sc);
1815149064Sthompsa
1816148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1817146985Sthompsa		return;
1818146985Sthompsa
1819146985Sthompsa	callout_stop(&sc->sc_brcallout);
1820160703Sthompsa	bstp_stop(&sc->sc_stp);
1821146985Sthompsa
1822146985Sthompsa	bridge_rtflush(sc, IFBF_FLUSHDYN);
1823146985Sthompsa
1824148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1825146985Sthompsa}
1826146985Sthompsa
1827146985Sthompsa/*
1828146985Sthompsa * bridge_enqueue:
1829146985Sthompsa *
1830146985Sthompsa *	Enqueue a packet on a bridge member interface.
1831146985Sthompsa *
1832146985Sthompsa */
1833240071Sglebiusstatic int
1834147786Sthompsabridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1835146985Sthompsa{
1836158140Sthompsa	int len, err = 0;
1837146985Sthompsa	short mflags;
1838158140Sthompsa	struct mbuf *m0;
1839146985Sthompsa
1840159759Sthompsa	/* We may be sending a fragment so traverse the mbuf */
1841158140Sthompsa	for (; m; m = m0) {
1842158140Sthompsa		m0 = m->m_nextpkt;
1843158140Sthompsa		m->m_nextpkt = NULL;
1844240971Sglebius		len = m->m_pkthdr.len;
1845240971Sglebius		mflags = m->m_flags;
1846172770Sthompsa
1847172770Sthompsa		/*
1848172770Sthompsa		 * If underlying interface can not do VLAN tag insertion itself
1849172770Sthompsa		 * then attach a packet tag that holds it.
1850172770Sthompsa		 */
1851172770Sthompsa		if ((m->m_flags & M_VLANTAG) &&
1852172770Sthompsa		    (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) {
1853172770Sthompsa			m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1854172770Sthompsa			if (m == NULL) {
1855172770Sthompsa				if_printf(dst_ifp,
1856172770Sthompsa				    "unable to prepend VLAN header\n");
1857172770Sthompsa				dst_ifp->if_oerrors++;
1858172770Sthompsa				continue;
1859172770Sthompsa			}
1860172770Sthompsa			m->m_flags &= ~M_VLANTAG;
1861172770Sthompsa		}
1862172770Sthompsa
1863238355Semaste		if ((err = dst_ifp->if_transmit(dst_ifp, m))) {
1864238355Semaste			m_freem(m0);
1865240971Sglebius			sc->sc_ifp->if_oerrors++;
1866238355Semaste			break;
1867238355Semaste		}
1868158140Sthompsa
1869147256Sbrooks		sc->sc_ifp->if_opackets++;
1870147256Sbrooks		sc->sc_ifp->if_obytes += len;
1871191603Ssam		if (mflags & M_MCAST)
1872147256Sbrooks			sc->sc_ifp->if_omcasts++;
1873146985Sthompsa	}
1874240071Sglebius
1875240071Sglebius	return (err);
1876146985Sthompsa}
1877146985Sthompsa
1878146985Sthompsa/*
1879147205Sthompsa * bridge_dummynet:
1880147205Sthompsa *
1881147205Sthompsa * 	Receive a queued packet from dummynet and pass it on to the output
1882147205Sthompsa * 	interface.
1883147205Sthompsa *
1884147205Sthompsa *	The mbuf has the Ethernet header already attached.
1885147205Sthompsa */
1886151313Sthompsastatic void
1887147205Sthompsabridge_dummynet(struct mbuf *m, struct ifnet *ifp)
1888147205Sthompsa{
1889147205Sthompsa	struct bridge_softc *sc;
1890147205Sthompsa
1891147205Sthompsa	sc = ifp->if_bridge;
1892147205Sthompsa
1893147205Sthompsa	/*
1894147205Sthompsa	 * The packet didnt originate from a member interface. This should only
1895147205Sthompsa	 * ever happen if a member interface is removed while packets are
1896147205Sthompsa	 * queued for it.
1897147205Sthompsa	 */
1898147251Smlaier	if (sc == NULL) {
1899147205Sthompsa		m_freem(m);
1900147205Sthompsa		return;
1901147251Smlaier	}
1902147205Sthompsa
1903197952Sjulian	if (PFIL_HOOKED(&V_inet_pfil_hook)
1904147786Sthompsa#ifdef INET6
1905197952Sjulian	    || PFIL_HOOKED(&V_inet6_pfil_hook)
1906147786Sthompsa#endif
1907147786Sthompsa	    ) {
1908147786Sthompsa		if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
1909147786Sthompsa			return;
1910147786Sthompsa		if (m == NULL)
1911147786Sthompsa			return;
1912147786Sthompsa	}
1913147786Sthompsa
1914147786Sthompsa	bridge_enqueue(sc, ifp, m);
1915147205Sthompsa}
1916147205Sthompsa
1917147205Sthompsa/*
1918146985Sthompsa * bridge_output:
1919146985Sthompsa *
1920146985Sthompsa *	Send output from a bridge member interface.  This
1921146985Sthompsa *	performs the bridging function for locally originated
1922146985Sthompsa *	packets.
1923146985Sthompsa *
1924146985Sthompsa *	The mbuf has the Ethernet header already attached.  We must
1925146985Sthompsa *	enqueue or free the mbuf before returning.
1926146985Sthompsa */
1927151313Sthompsastatic int
1928146985Sthompsabridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1929146985Sthompsa    struct rtentry *rt)
1930146985Sthompsa{
1931146985Sthompsa	struct ether_header *eh;
1932146985Sthompsa	struct ifnet *dst_if;
1933146985Sthompsa	struct bridge_softc *sc;
1934170681Sthompsa	uint16_t vlan;
1935146985Sthompsa
1936146985Sthompsa	if (m->m_len < ETHER_HDR_LEN) {
1937146985Sthompsa		m = m_pullup(m, ETHER_HDR_LEN);
1938146985Sthompsa		if (m == NULL)
1939146985Sthompsa			return (0);
1940146985Sthompsa	}
1941146985Sthompsa
1942146985Sthompsa	eh = mtod(m, struct ether_header *);
1943146985Sthompsa	sc = ifp->if_bridge;
1944170681Sthompsa	vlan = VLANTAGOF(m);
1945146985Sthompsa
1946146985Sthompsa	BRIDGE_LOCK(sc);
1947146985Sthompsa
1948146985Sthompsa	/*
1949146985Sthompsa	 * If bridge is down, but the original output interface is up,
1950146985Sthompsa	 * go ahead and send out that interface.  Otherwise, the packet
1951146985Sthompsa	 * is dropped below.
1952146985Sthompsa	 */
1953148887Srwatson	if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1954146985Sthompsa		dst_if = ifp;
1955146985Sthompsa		goto sendunicast;
1956146985Sthompsa	}
1957146985Sthompsa
1958146985Sthompsa	/*
1959146985Sthompsa	 * If the packet is a multicast, or we don't know a better way to
1960146985Sthompsa	 * get there, send to all interfaces.
1961146985Sthompsa	 */
1962146985Sthompsa	if (ETHER_IS_MULTICAST(eh->ether_dhost))
1963146985Sthompsa		dst_if = NULL;
1964146985Sthompsa	else
1965170681Sthompsa		dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan);
1966146985Sthompsa	if (dst_if == NULL) {
1967146985Sthompsa		struct bridge_iflist *bif;
1968146985Sthompsa		struct mbuf *mc;
1969146985Sthompsa		int error = 0, used = 0;
1970146985Sthompsa
1971161401Sthompsa		bridge_span(sc, m);
1972161401Sthompsa
1973146985Sthompsa		BRIDGE_LOCK2REF(sc, error);
1974146985Sthompsa		if (error) {
1975146985Sthompsa			m_freem(m);
1976146985Sthompsa			return (0);
1977146985Sthompsa		}
1978153408Sthompsa
1979146985Sthompsa		LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1980146985Sthompsa			dst_if = bif->bif_ifp;
1981153621Sthompsa
1982153621Sthompsa			if (dst_if->if_type == IFT_GIF)
1983153621Sthompsa				continue;
1984148887Srwatson			if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
1985146985Sthompsa				continue;
1986146985Sthompsa
1987146985Sthompsa			/*
1988146985Sthompsa			 * If this is not the original output interface,
1989146985Sthompsa			 * and the interface is participating in spanning
1990146985Sthompsa			 * tree, make sure the port is in a state that
1991146985Sthompsa			 * allows forwarding.
1992146985Sthompsa			 */
1993163863Sthompsa			if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) &&
1994163863Sthompsa			    bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
1995163863Sthompsa				continue;
1996146985Sthompsa
1997146985Sthompsa			if (LIST_NEXT(bif, bif_next) == NULL) {
1998146985Sthompsa				used = 1;
1999146985Sthompsa				mc = m;
2000146985Sthompsa			} else {
2001243882Sglebius				mc = m_copypacket(m, M_NOWAIT);
2002146985Sthompsa				if (mc == NULL) {
2003147256Sbrooks					sc->sc_ifp->if_oerrors++;
2004146985Sthompsa					continue;
2005146985Sthompsa				}
2006146985Sthompsa			}
2007146985Sthompsa
2008147786Sthompsa			bridge_enqueue(sc, dst_if, mc);
2009146985Sthompsa		}
2010146985Sthompsa		if (used == 0)
2011146985Sthompsa			m_freem(m);
2012146985Sthompsa		BRIDGE_UNREF(sc);
2013146985Sthompsa		return (0);
2014146985Sthompsa	}
2015146985Sthompsa
2016153497Sthompsasendunicast:
2017146985Sthompsa	/*
2018146985Sthompsa	 * XXX Spanning tree consideration here?
2019146985Sthompsa	 */
2020146985Sthompsa
2021153408Sthompsa	bridge_span(sc, m);
2022148887Srwatson	if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2023146985Sthompsa		m_freem(m);
2024146985Sthompsa		BRIDGE_UNLOCK(sc);
2025146985Sthompsa		return (0);
2026146985Sthompsa	}
2027146985Sthompsa
2028146985Sthompsa	BRIDGE_UNLOCK(sc);
2029147786Sthompsa	bridge_enqueue(sc, dst_if, m);
2030146985Sthompsa	return (0);
2031146985Sthompsa}
2032146985Sthompsa
2033146985Sthompsa/*
2034240071Sglebius * bridge_transmit:
2035146985Sthompsa *
2036240071Sglebius *	Do output on a bridge.
2037146985Sthompsa *
2038146985Sthompsa */
2039240071Sglebiusstatic int
2040240071Sglebiusbridge_transmit(struct ifnet *ifp, struct mbuf *m)
2041146985Sthompsa{
2042146985Sthompsa	struct bridge_softc *sc;
2043242013Sglebius	struct ether_header *eh;
2044242013Sglebius	struct ifnet *dst_if;
2045240071Sglebius	int error = 0;
2046146985Sthompsa
2047146985Sthompsa	sc = ifp->if_softc;
2048146985Sthompsa
2049240071Sglebius	ETHER_BPF_MTAP(ifp, m);
2050146985Sthompsa
2051242013Sglebius	eh = mtod(m, struct ether_header *);
2052240071Sglebius
2053240071Sglebius	BRIDGE_LOCK(sc);
2054242013Sglebius	if (((m->m_flags & (M_BCAST|M_MCAST)) == 0) &&
2055242013Sglebius	    (dst_if = bridge_rtlookup(sc, eh->ether_dhost, 1)) != NULL) {
2056240071Sglebius		BRIDGE_UNLOCK(sc);
2057240071Sglebius		error = bridge_enqueue(sc, dst_if, m);
2058240071Sglebius	} else
2059240071Sglebius		bridge_broadcast(sc, ifp, m, 0);
2060146985Sthompsa
2061240071Sglebius	return (error);
2062240071Sglebius}
2063146985Sthompsa
2064240071Sglebius/*
2065240071Sglebius * The ifp->if_qflush entry point for if_bridge(4) is no-op.
2066240071Sglebius */
2067240071Sglebiusstatic void
2068240071Sglebiusbridge_qflush(struct ifnet *ifp __unused)
2069240071Sglebius{
2070146985Sthompsa}
2071146985Sthompsa
2072146985Sthompsa/*
2073146985Sthompsa * bridge_forward:
2074146985Sthompsa *
2075146985Sthompsa *	The forwarding function of the bridge.
2076146985Sthompsa *
2077146985Sthompsa *	NOTE: Releases the lock on return.
2078146985Sthompsa */
2079151313Sthompsastatic void
2080171678Sthompsabridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
2081171678Sthompsa    struct mbuf *m)
2082146985Sthompsa{
2083171678Sthompsa	struct bridge_iflist *dbif;
2084146985Sthompsa	struct ifnet *src_if, *dst_if, *ifp;
2085146985Sthompsa	struct ether_header *eh;
2086170681Sthompsa	uint16_t vlan;
2087175419Sthompsa	uint8_t *dst;
2088173320Sthompsa	int error;
2089146985Sthompsa
2090146985Sthompsa	src_if = m->m_pkthdr.rcvif;
2091147256Sbrooks	ifp = sc->sc_ifp;
2092146985Sthompsa
2093174749Sthompsa	ifp->if_ipackets++;
2094174749Sthompsa	ifp->if_ibytes += m->m_pkthdr.len;
2095170681Sthompsa	vlan = VLANTAGOF(m);
2096146985Sthompsa
2097171678Sthompsa	if ((sbif->bif_flags & IFBIF_STP) &&
2098174749Sthompsa	    sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2099174749Sthompsa		goto drop;
2100146985Sthompsa
2101146985Sthompsa	eh = mtod(m, struct ether_header *);
2102175419Sthompsa	dst = eh->ether_dhost;
2103146985Sthompsa
2104173320Sthompsa	/* If the interface is learning, record the address. */
2105173320Sthompsa	if (sbif->bif_flags & IFBIF_LEARNING) {
2106173320Sthompsa		error = bridge_rtupdate(sc, eh->ether_shost, vlan,
2107171678Sthompsa		    sbif, 0, IFBAF_DYNAMIC);
2108173320Sthompsa		/*
2109173320Sthompsa		 * If the interface has addresses limits then deny any source
2110173320Sthompsa		 * that is not in the cache.
2111173320Sthompsa		 */
2112174749Sthompsa		if (error && sbif->bif_addrmax)
2113174749Sthompsa			goto drop;
2114146985Sthompsa	}
2115146985Sthompsa
2116171678Sthompsa	if ((sbif->bif_flags & IFBIF_STP) != 0 &&
2117174749Sthompsa	    sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING)
2118174749Sthompsa		goto drop;
2119146985Sthompsa
2120146985Sthompsa	/*
2121146985Sthompsa	 * At this point, the port either doesn't participate
2122146985Sthompsa	 * in spanning tree or it is in the forwarding state.
2123146985Sthompsa	 */
2124146985Sthompsa
2125146985Sthompsa	/*
2126146985Sthompsa	 * If the packet is unicast, destined for someone on
2127146985Sthompsa	 * "this" side of the bridge, drop it.
2128146985Sthompsa	 */
2129146985Sthompsa	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2130175419Sthompsa		dst_if = bridge_rtlookup(sc, dst, vlan);
2131174749Sthompsa		if (src_if == dst_if)
2132174749Sthompsa			goto drop;
2133146985Sthompsa	} else {
2134175419Sthompsa		/*
2135175419Sthompsa		 * Check if its a reserved multicast address, any address
2136175419Sthompsa		 * listed in 802.1D section 7.12.6 may not be forwarded by the
2137175419Sthompsa		 * bridge.
2138175419Sthompsa		 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
2139175419Sthompsa		 */
2140175419Sthompsa		if (dst[0] == 0x01 && dst[1] == 0x80 &&
2141175419Sthompsa		    dst[2] == 0xc2 && dst[3] == 0x00 &&
2142175419Sthompsa		    dst[4] == 0x00 && dst[5] <= 0x0f)
2143175419Sthompsa			goto drop;
2144175419Sthompsa
2145146985Sthompsa		/* ...forward it to all interfaces. */
2146174749Sthompsa		ifp->if_imcasts++;
2147146985Sthompsa		dst_if = NULL;
2148146985Sthompsa	}
2149146985Sthompsa
2150156235Scsjp	/*
2151156235Scsjp	 * If we have a destination interface which is a member of our bridge,
2152156235Scsjp	 * OR this is a unicast packet, push it through the bpf(4) machinery.
2153156235Scsjp	 * For broadcast or multicast packets, don't bother because it will
2154156235Scsjp	 * be reinjected into ether_input. We do this before we pass the packets
2155156235Scsjp	 * through the pfil(9) framework, as it is possible that pfil(9) will
2156156235Scsjp	 * drop the packet, or possibly modify it, making it difficult to debug
2157156235Scsjp	 * firewall issues on the bridge.
2158156235Scsjp	 */
2159156235Scsjp	if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0)
2160172824Sthompsa		ETHER_BPF_MTAP(ifp, m);
2161156235Scsjp
2162146985Sthompsa	/* run the packet filter */
2163197952Sjulian	if (PFIL_HOOKED(&V_inet_pfil_hook)
2164147786Sthompsa#ifdef INET6
2165197952Sjulian	    || PFIL_HOOKED(&V_inet6_pfil_hook)
2166147786Sthompsa#endif
2167147786Sthompsa	    ) {
2168146985Sthompsa		BRIDGE_UNLOCK(sc);
2169146985Sthompsa		if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2170146985Sthompsa			return;
2171147786Sthompsa		if (m == NULL)
2172147786Sthompsa			return;
2173146985Sthompsa		BRIDGE_LOCK(sc);
2174146985Sthompsa	}
2175146985Sthompsa
2176146985Sthompsa	if (dst_if == NULL) {
2177150837Sthompsa		bridge_broadcast(sc, src_if, m, 1);
2178146985Sthompsa		return;
2179146985Sthompsa	}
2180146985Sthompsa
2181146985Sthompsa	/*
2182146985Sthompsa	 * At this point, we're dealing with a unicast frame
2183146985Sthompsa	 * going to a different interface.
2184146985Sthompsa	 */
2185174749Sthompsa	if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2186174749Sthompsa		goto drop;
2187174749Sthompsa
2188171678Sthompsa	dbif = bridge_lookup_member_if(sc, dst_if);
2189174749Sthompsa	if (dbif == NULL)
2190146985Sthompsa		/* Not a member of the bridge (anymore?) */
2191174749Sthompsa		goto drop;
2192146985Sthompsa
2193171678Sthompsa	/* Private segments can not talk to each other */
2194174749Sthompsa	if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)
2195174749Sthompsa		goto drop;
2196146985Sthompsa
2197171678Sthompsa	if ((dbif->bif_flags & IFBIF_STP) &&
2198174749Sthompsa	    dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2199174749Sthompsa		goto drop;
2200171678Sthompsa
2201146985Sthompsa	BRIDGE_UNLOCK(sc);
2202147786Sthompsa
2203197952Sjulian	if (PFIL_HOOKED(&V_inet_pfil_hook)
2204147786Sthompsa#ifdef INET6
2205197952Sjulian	    || PFIL_HOOKED(&V_inet6_pfil_hook)
2206147786Sthompsa#endif
2207147786Sthompsa	    ) {
2208174749Sthompsa		if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2209147786Sthompsa			return;
2210147786Sthompsa		if (m == NULL)
2211147786Sthompsa			return;
2212147786Sthompsa	}
2213147786Sthompsa
2214147786Sthompsa	bridge_enqueue(sc, dst_if, m);
2215174749Sthompsa	return;
2216174749Sthompsa
2217174749Sthompsadrop:
2218174749Sthompsa	BRIDGE_UNLOCK(sc);
2219174749Sthompsa	m_freem(m);
2220146985Sthompsa}
2221146985Sthompsa
2222146985Sthompsa/*
2223146985Sthompsa * bridge_input:
2224146985Sthompsa *
2225146985Sthompsa *	Receive input from a member interface.  Queue the packet for
2226146985Sthompsa *	bridging if it is not for us.
2227146985Sthompsa */
2228151345Sthompsastatic struct mbuf *
2229146985Sthompsabridge_input(struct ifnet *ifp, struct mbuf *m)
2230146985Sthompsa{
2231146985Sthompsa	struct bridge_softc *sc = ifp->if_bridge;
2232164112Sthompsa	struct bridge_iflist *bif, *bif2;
2233149829Sthompsa	struct ifnet *bifp;
2234146985Sthompsa	struct ether_header *eh;
2235149829Sthompsa	struct mbuf *mc, *mc2;
2236170681Sthompsa	uint16_t vlan;
2237173320Sthompsa	int error;
2238146985Sthompsa
2239148887Srwatson	if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2240146985Sthompsa		return (m);
2241146985Sthompsa
2242149829Sthompsa	bifp = sc->sc_ifp;
2243170681Sthompsa	vlan = VLANTAGOF(m);
2244149829Sthompsa
2245156235Scsjp	/*
2246156235Scsjp	 * Implement support for bridge monitoring. If this flag has been
2247156235Scsjp	 * set on this interface, discard the packet once we push it through
2248156235Scsjp	 * the bpf(4) machinery, but before we do, increment the byte and
2249156235Scsjp	 * packet counters associated with this interface.
2250156235Scsjp	 */
2251156235Scsjp	if ((bifp->if_flags & IFF_MONITOR) != 0) {
2252156235Scsjp		m->m_pkthdr.rcvif  = bifp;
2253172824Sthompsa		ETHER_BPF_MTAP(bifp, m);
2254156235Scsjp		bifp->if_ipackets++;
2255156235Scsjp		bifp->if_ibytes += m->m_pkthdr.len;
2256164002Scsjp		m_freem(m);
2257156235Scsjp		return (NULL);
2258156235Scsjp	}
2259146985Sthompsa	BRIDGE_LOCK(sc);
2260146985Sthompsa	bif = bridge_lookup_member_if(sc, ifp);
2261146985Sthompsa	if (bif == NULL) {
2262146985Sthompsa		BRIDGE_UNLOCK(sc);
2263146985Sthompsa		return (m);
2264146985Sthompsa	}
2265146985Sthompsa
2266146985Sthompsa	eh = mtod(m, struct ether_header *);
2267146985Sthompsa
2268153408Sthompsa	bridge_span(sc, m);
2269153408Sthompsa
2270167722Sthompsa	if (m->m_flags & (M_BCAST|M_MCAST)) {
2271146985Sthompsa		/* Tap off 802.1D packets; they do not get forwarded. */
2272146985Sthompsa		if (memcmp(eh->ether_dhost, bstp_etheraddr,
2273146985Sthompsa		    ETHER_ADDR_LEN) == 0) {
2274232014Sthompsa			bstp_input(&bif->bif_stp, ifp, m); /* consumes mbuf */
2275232014Sthompsa			BRIDGE_UNLOCK(sc);
2276232014Sthompsa			return (NULL);
2277146985Sthompsa		}
2278146985Sthompsa
2279163863Sthompsa		if ((bif->bif_flags & IFBIF_STP) &&
2280163863Sthompsa		    bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2281163863Sthompsa			BRIDGE_UNLOCK(sc);
2282163863Sthompsa			return (m);
2283146985Sthompsa		}
2284146985Sthompsa
2285146985Sthompsa		/*
2286146985Sthompsa		 * Make a deep copy of the packet and enqueue the copy
2287146985Sthompsa		 * for bridge processing; return the original packet for
2288146985Sthompsa		 * local processing.
2289146985Sthompsa		 */
2290243882Sglebius		mc = m_dup(m, M_NOWAIT);
2291146985Sthompsa		if (mc == NULL) {
2292146985Sthompsa			BRIDGE_UNLOCK(sc);
2293146985Sthompsa			return (m);
2294146985Sthompsa		}
2295146985Sthompsa
2296146985Sthompsa		/* Perform the bridge forwarding function with the copy. */
2297171678Sthompsa		bridge_forward(sc, bif, mc);
2298146985Sthompsa
2299149829Sthompsa		/*
2300149829Sthompsa		 * Reinject the mbuf as arriving on the bridge so we have a
2301149829Sthompsa		 * chance at claiming multicast packets. We can not loop back
2302149829Sthompsa		 * here from ether_input as a bridge is never a member of a
2303149829Sthompsa		 * bridge.
2304149829Sthompsa		 */
2305149829Sthompsa		KASSERT(bifp->if_bridge == NULL,
2306149829Sthompsa		    ("loop created in bridge_input"));
2307243882Sglebius		mc2 = m_dup(m, M_NOWAIT);
2308149829Sthompsa		if (mc2 != NULL) {
2309153458Sthompsa			/* Keep the layer3 header aligned */
2310153458Sthompsa			int i = min(mc2->m_pkthdr.len, max_protohdr);
2311153458Sthompsa			mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2312153458Sthompsa		}
2313153458Sthompsa		if (mc2 != NULL) {
2314149829Sthompsa			mc2->m_pkthdr.rcvif = bifp;
2315149829Sthompsa			(*bifp->if_input)(bifp, mc2);
2316149829Sthompsa		}
2317149829Sthompsa
2318146985Sthompsa		/* Return the original packet for local processing. */
2319146985Sthompsa		return (m);
2320146985Sthompsa	}
2321146985Sthompsa
2322163863Sthompsa	if ((bif->bif_flags & IFBIF_STP) &&
2323163863Sthompsa	    bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2324163863Sthompsa		BRIDGE_UNLOCK(sc);
2325163863Sthompsa		return (m);
2326146985Sthompsa	}
2327146985Sthompsa
2328211157Swill#if (defined(INET) || defined(INET6))
2329167683Srik#   define OR_CARP_CHECK_WE_ARE_DST(iface) \
2330167683Srik	|| ((iface)->if_carp \
2331211193Swill	    && (*carp_forus_p)((iface), eh->ether_dhost))
2332167683Srik#   define OR_CARP_CHECK_WE_ARE_SRC(iface) \
2333167683Srik	|| ((iface)->if_carp \
2334211193Swill	    && (*carp_forus_p)((iface), eh->ether_shost))
2335167683Srik#else
2336167683Srik#   define OR_CARP_CHECK_WE_ARE_DST(iface)
2337167683Srik#   define OR_CARP_CHECK_WE_ARE_SRC(iface)
2338167683Srik#endif
2339167683Srik
2340175432Sthompsa#ifdef INET6
2341175432Sthompsa#   define OR_PFIL_HOOKED_INET6 \
2342197952Sjulian	|| PFIL_HOOKED(&V_inet6_pfil_hook)
2343175432Sthompsa#else
2344175432Sthompsa#   define OR_PFIL_HOOKED_INET6
2345175432Sthompsa#endif
2346175432Sthompsa
2347167683Srik#define GRAB_OUR_PACKETS(iface) \
2348167683Srik	if ((iface)->if_type == IFT_GIF) \
2349167683Srik		continue; \
2350167683Srik	/* It is destined for us. */ \
2351167683Srik	if (memcmp(IF_LLADDR((iface)), eh->ether_dhost,  ETHER_ADDR_LEN) == 0 \
2352167683Srik	    OR_CARP_CHECK_WE_ARE_DST((iface))				\
2353167683Srik	    ) {								\
2354175432Sthompsa		if ((iface)->if_type == IFT_BRIDGE) {			\
2355175432Sthompsa			ETHER_BPF_MTAP(iface, m);			\
2356175432Sthompsa			iface->if_ipackets++;				\
2357240971Sglebius			iface->if_ibytes += m->m_pkthdr.len;		\
2358175432Sthompsa			/* Filter on the physical interface. */		\
2359175432Sthompsa			if (pfil_local_phys &&				\
2360197952Sjulian			    (PFIL_HOOKED(&V_inet_pfil_hook)		\
2361175432Sthompsa			     OR_PFIL_HOOKED_INET6)) {			\
2362175432Sthompsa				if (bridge_pfil(&m, NULL, ifp,		\
2363175432Sthompsa				    PFIL_IN) != 0 || m == NULL) {	\
2364175432Sthompsa					BRIDGE_UNLOCK(sc);		\
2365175432Sthompsa					return (NULL);			\
2366175432Sthompsa				}					\
2367248155Sglebius				eh = mtod(m, struct ether_header *);	\
2368175432Sthompsa			}						\
2369175432Sthompsa		}							\
2370173320Sthompsa		if (bif->bif_flags & IFBIF_LEARNING) {			\
2371173320Sthompsa			error = bridge_rtupdate(sc, eh->ether_shost,	\
2372170681Sthompsa			    vlan, bif, 0, IFBAF_DYNAMIC);		\
2373173320Sthompsa			if (error && bif->bif_addrmax) {		\
2374173320Sthompsa				BRIDGE_UNLOCK(sc);			\
2375173320Sthompsa				m_freem(m);				\
2376173320Sthompsa				return (NULL);				\
2377173320Sthompsa			}						\
2378173320Sthompsa		}							\
2379167683Srik		m->m_pkthdr.rcvif = iface;				\
2380167683Srik		BRIDGE_UNLOCK(sc);					\
2381167683Srik		return (m);						\
2382167683Srik	}								\
2383167683Srik									\
2384167683Srik	/* We just received a packet that we sent out. */		\
2385167683Srik	if (memcmp(IF_LLADDR((iface)), eh->ether_shost, ETHER_ADDR_LEN) == 0 \
2386167683Srik	    OR_CARP_CHECK_WE_ARE_SRC((iface))			\
2387167683Srik	    ) {								\
2388167683Srik		BRIDGE_UNLOCK(sc);					\
2389167683Srik		m_freem(m);						\
2390167683Srik		return (NULL);						\
2391167683Srik	}
2392167683Srik
2393146985Sthompsa	/*
2394175432Sthompsa	 * Unicast.  Make sure it's not for the bridge.
2395175432Sthompsa	 */
2396175432Sthompsa	do { GRAB_OUR_PACKETS(bifp) } while (0);
2397175432Sthompsa
2398175432Sthompsa	/*
2399167683Srik	 * Give a chance for ifp at first priority. This will help when	the
2400167683Srik	 * packet comes through the interface like VLAN's with the same MACs
2401167683Srik	 * on several interfaces from the same bridge. This also will save
2402167683Srik	 * some CPU cycles in case the destination interface and the input
2403167683Srik	 * interface (eq ifp) are the same.
2404146985Sthompsa	 */
2405167683Srik	do { GRAB_OUR_PACKETS(ifp) } while (0);
2406167683Srik
2407167683Srik	/* Now check the all bridge members. */
2408164112Sthompsa	LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) {
2409167683Srik		GRAB_OUR_PACKETS(bif2->bif_ifp)
2410146985Sthompsa	}
2411146985Sthompsa
2412167683Srik#undef OR_CARP_CHECK_WE_ARE_DST
2413167683Srik#undef OR_CARP_CHECK_WE_ARE_SRC
2414175432Sthompsa#undef OR_PFIL_HOOKED_INET6
2415167683Srik#undef GRAB_OUR_PACKETS
2416167683Srik
2417146985Sthompsa	/* Perform the bridge forwarding function. */
2418171678Sthompsa	bridge_forward(sc, bif, m);
2419146985Sthompsa
2420146985Sthompsa	return (NULL);
2421146985Sthompsa}
2422146985Sthompsa
2423146985Sthompsa/*
2424146985Sthompsa * bridge_broadcast:
2425146985Sthompsa *
2426146985Sthompsa *	Send a frame to all interfaces that are members of
2427146985Sthompsa *	the bridge, except for the one on which the packet
2428146985Sthompsa *	arrived.
2429146985Sthompsa *
2430146985Sthompsa *	NOTE: Releases the lock on return.
2431146985Sthompsa */
2432151313Sthompsastatic void
2433146985Sthompsabridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2434150837Sthompsa    struct mbuf *m, int runfilt)
2435146985Sthompsa{
2436171678Sthompsa	struct bridge_iflist *dbif, *sbif;
2437146985Sthompsa	struct mbuf *mc;
2438146985Sthompsa	struct ifnet *dst_if;
2439157057Srik	int error = 0, used = 0, i;
2440146985Sthompsa
2441171678Sthompsa	sbif = bridge_lookup_member_if(sc, src_if);
2442171678Sthompsa
2443146985Sthompsa	BRIDGE_LOCK2REF(sc, error);
2444146985Sthompsa	if (error) {
2445146985Sthompsa		m_freem(m);
2446146985Sthompsa		return;
2447146985Sthompsa	}
2448146985Sthompsa
2449147786Sthompsa	/* Filter on the bridge interface before broadcasting */
2450197952Sjulian	if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook)
2451147786Sthompsa#ifdef INET6
2452197952Sjulian	    || PFIL_HOOKED(&V_inet6_pfil_hook)
2453147786Sthompsa#endif
2454150837Sthompsa	    )) {
2455147786Sthompsa		if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
2456152393Sthompsa			goto out;
2457147786Sthompsa		if (m == NULL)
2458152393Sthompsa			goto out;
2459147786Sthompsa	}
2460147786Sthompsa
2461171678Sthompsa	LIST_FOREACH(dbif, &sc->sc_iflist, bif_next) {
2462171678Sthompsa		dst_if = dbif->bif_ifp;
2463146985Sthompsa		if (dst_if == src_if)
2464146985Sthompsa			continue;
2465146985Sthompsa
2466171678Sthompsa		/* Private segments can not talk to each other */
2467171678Sthompsa		if (sbif && (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE))
2468163863Sthompsa			continue;
2469146985Sthompsa
2470171678Sthompsa		if ((dbif->bif_flags & IFBIF_STP) &&
2471171678Sthompsa		    dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2472171678Sthompsa			continue;
2473171678Sthompsa
2474171678Sthompsa		if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 &&
2475146985Sthompsa		    (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2476146985Sthompsa			continue;
2477146985Sthompsa
2478148887Srwatson		if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2479146985Sthompsa			continue;
2480146985Sthompsa
2481171678Sthompsa		if (LIST_NEXT(dbif, bif_next) == NULL) {
2482146985Sthompsa			mc = m;
2483146985Sthompsa			used = 1;
2484146985Sthompsa		} else {
2485243882Sglebius			mc = m_dup(m, M_NOWAIT);
2486146985Sthompsa			if (mc == NULL) {
2487147256Sbrooks				sc->sc_ifp->if_oerrors++;
2488146985Sthompsa				continue;
2489146985Sthompsa			}
2490146985Sthompsa		}
2491146985Sthompsa
2492147786Sthompsa		/*
2493147786Sthompsa		 * Filter on the output interface. Pass a NULL bridge interface
2494153497Sthompsa		 * pointer so we do not redundantly filter on the bridge for
2495147786Sthompsa		 * each interface we broadcast on.
2496147786Sthompsa		 */
2497197952Sjulian		if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook)
2498147786Sthompsa#ifdef INET6
2499197952Sjulian		    || PFIL_HOOKED(&V_inet6_pfil_hook)
2500147786Sthompsa#endif
2501150837Sthompsa		    )) {
2502157057Srik			if (used == 0) {
2503157057Srik				/* Keep the layer3 header aligned */
2504157057Srik				i = min(mc->m_pkthdr.len, max_protohdr);
2505157057Srik				mc = m_copyup(mc, i, ETHER_ALIGN);
2506157057Srik				if (mc == NULL) {
2507157057Srik					sc->sc_ifp->if_oerrors++;
2508157057Srik					continue;
2509157057Srik				}
2510157057Srik			}
2511152392Sthompsa			if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2512152392Sthompsa				continue;
2513152392Sthompsa			if (mc == NULL)
2514152392Sthompsa				continue;
2515147786Sthompsa		}
2516151305Sthompsa
2517147786Sthompsa		bridge_enqueue(sc, dst_if, mc);
2518146985Sthompsa	}
2519146985Sthompsa	if (used == 0)
2520146985Sthompsa		m_freem(m);
2521146985Sthompsa
2522152393Sthompsaout:
2523146985Sthompsa	BRIDGE_UNREF(sc);
2524146985Sthompsa}
2525146985Sthompsa
2526146985Sthompsa/*
2527153408Sthompsa * bridge_span:
2528153408Sthompsa *
2529153408Sthompsa *	Duplicate a packet out one or more interfaces that are in span mode,
2530153408Sthompsa *	the original mbuf is unmodified.
2531153408Sthompsa */
2532153408Sthompsastatic void
2533153408Sthompsabridge_span(struct bridge_softc *sc, struct mbuf *m)
2534153408Sthompsa{
2535153408Sthompsa	struct bridge_iflist *bif;
2536153408Sthompsa	struct ifnet *dst_if;
2537153408Sthompsa	struct mbuf *mc;
2538153408Sthompsa
2539153408Sthompsa	if (LIST_EMPTY(&sc->sc_spanlist))
2540153408Sthompsa		return;
2541153408Sthompsa
2542153408Sthompsa	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2543153408Sthompsa		dst_if = bif->bif_ifp;
2544158667Sthompsa
2545153408Sthompsa		if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2546153408Sthompsa			continue;
2547153408Sthompsa
2548243882Sglebius		mc = m_copypacket(m, M_NOWAIT);
2549153408Sthompsa		if (mc == NULL) {
2550153408Sthompsa			sc->sc_ifp->if_oerrors++;
2551153408Sthompsa			continue;
2552153408Sthompsa		}
2553153408Sthompsa
2554153408Sthompsa		bridge_enqueue(sc, dst_if, mc);
2555153408Sthompsa	}
2556153408Sthompsa}
2557153408Sthompsa
2558153408Sthompsa/*
2559146985Sthompsa * bridge_rtupdate:
2560146985Sthompsa *
2561146985Sthompsa *	Add a bridge routing entry.
2562146985Sthompsa */
2563151313Sthompsastatic int
2564170681Sthompsabridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan,
2565164112Sthompsa    struct bridge_iflist *bif, int setflags, uint8_t flags)
2566146985Sthompsa{
2567146985Sthompsa	struct bridge_rtnode *brt;
2568146985Sthompsa	int error;
2569146985Sthompsa
2570146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2571146985Sthompsa
2572173320Sthompsa	/* Check the source address is valid and not multicast. */
2573173320Sthompsa	if (ETHER_IS_MULTICAST(dst) ||
2574173320Sthompsa	    (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
2575173320Sthompsa	     dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0)
2576173320Sthompsa		return (EINVAL);
2577173320Sthompsa
2578170681Sthompsa	/* 802.1p frames map to vlan 1 */
2579170681Sthompsa	if (vlan == 0)
2580170681Sthompsa		vlan = 1;
2581170681Sthompsa
2582146985Sthompsa	/*
2583146985Sthompsa	 * A route for this destination might already exist.  If so,
2584146985Sthompsa	 * update it, otherwise create a new one.
2585146985Sthompsa	 */
2586170681Sthompsa	if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) {
2587160867Sthompsa		if (sc->sc_brtcnt >= sc->sc_brtmax) {
2588160867Sthompsa			sc->sc_brtexceeded++;
2589146985Sthompsa			return (ENOSPC);
2590160867Sthompsa		}
2591173320Sthompsa		/* Check per interface address limits (if enabled) */
2592173320Sthompsa		if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) {
2593173320Sthompsa			bif->bif_addrexceeded++;
2594173320Sthompsa			return (ENOSPC);
2595173320Sthompsa		}
2596146985Sthompsa
2597146985Sthompsa		/*
2598146985Sthompsa		 * Allocate a new bridge forwarding node, and
2599146985Sthompsa		 * initialize the expiration time and Ethernet
2600146985Sthompsa		 * address.
2601146985Sthompsa		 */
2602146985Sthompsa		brt = uma_zalloc(bridge_rtnode_zone, M_NOWAIT | M_ZERO);
2603146985Sthompsa		if (brt == NULL)
2604146985Sthompsa			return (ENOMEM);
2605146985Sthompsa
2606164112Sthompsa		if (bif->bif_flags & IFBIF_STICKY)
2607164112Sthompsa			brt->brt_flags = IFBAF_STICKY;
2608164112Sthompsa		else
2609164112Sthompsa			brt->brt_flags = IFBAF_DYNAMIC;
2610164112Sthompsa
2611146985Sthompsa		memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2612170681Sthompsa		brt->brt_vlan = vlan;
2613146985Sthompsa
2614146985Sthompsa		if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2615146985Sthompsa			uma_zfree(bridge_rtnode_zone, brt);
2616146985Sthompsa			return (error);
2617146985Sthompsa		}
2618173320Sthompsa		brt->brt_dst = bif;
2619173320Sthompsa		bif->bif_addrcnt++;
2620146985Sthompsa	}
2621146985Sthompsa
2622173320Sthompsa	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2623173320Sthompsa	    brt->brt_dst != bif) {
2624173320Sthompsa		brt->brt_dst->bif_addrcnt--;
2625173320Sthompsa		brt->brt_dst = bif;
2626173320Sthompsa		brt->brt_dst->bif_addrcnt++;
2627173320Sthompsa	}
2628173320Sthompsa
2629153979Sthompsa	if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2630153978Sthompsa		brt->brt_expire = time_uptime + sc->sc_brttimeout;
2631153977Sthompsa	if (setflags)
2632146985Sthompsa		brt->brt_flags = flags;
2633146985Sthompsa
2634146985Sthompsa	return (0);
2635146985Sthompsa}
2636146985Sthompsa
2637146985Sthompsa/*
2638146985Sthompsa * bridge_rtlookup:
2639146985Sthompsa *
2640146985Sthompsa *	Lookup the destination interface for an address.
2641146985Sthompsa */
2642151345Sthompsastatic struct ifnet *
2643170681Sthompsabridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2644146985Sthompsa{
2645146985Sthompsa	struct bridge_rtnode *brt;
2646146985Sthompsa
2647146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2648146985Sthompsa
2649170681Sthompsa	if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL)
2650146985Sthompsa		return (NULL);
2651146985Sthompsa
2652146985Sthompsa	return (brt->brt_ifp);
2653146985Sthompsa}
2654146985Sthompsa
2655146985Sthompsa/*
2656146985Sthompsa * bridge_rttrim:
2657146985Sthompsa *
2658146985Sthompsa *	Trim the routine table so that we have a number
2659146985Sthompsa *	of routing entries less than or equal to the
2660146985Sthompsa *	maximum number.
2661146985Sthompsa */
2662151313Sthompsastatic void
2663146985Sthompsabridge_rttrim(struct bridge_softc *sc)
2664146985Sthompsa{
2665146985Sthompsa	struct bridge_rtnode *brt, *nbrt;
2666146985Sthompsa
2667146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2668146985Sthompsa
2669146985Sthompsa	/* Make sure we actually need to do this. */
2670146985Sthompsa	if (sc->sc_brtcnt <= sc->sc_brtmax)
2671146985Sthompsa		return;
2672146985Sthompsa
2673146985Sthompsa	/* Force an aging cycle; this might trim enough addresses. */
2674146985Sthompsa	bridge_rtage(sc);
2675146985Sthompsa	if (sc->sc_brtcnt <= sc->sc_brtmax)
2676146985Sthompsa		return;
2677146985Sthompsa
2678163142Sthompsa	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2679146985Sthompsa		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2680146985Sthompsa			bridge_rtnode_destroy(sc, brt);
2681146985Sthompsa			if (sc->sc_brtcnt <= sc->sc_brtmax)
2682146985Sthompsa				return;
2683146985Sthompsa		}
2684146985Sthompsa	}
2685146985Sthompsa}
2686146985Sthompsa
2687146985Sthompsa/*
2688146985Sthompsa * bridge_timer:
2689146985Sthompsa *
2690146985Sthompsa *	Aging timer for the bridge.
2691146985Sthompsa */
2692151313Sthompsastatic void
2693146985Sthompsabridge_timer(void *arg)
2694146985Sthompsa{
2695146985Sthompsa	struct bridge_softc *sc = arg;
2696146985Sthompsa
2697149253Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2698149253Sthompsa
2699146985Sthompsa	bridge_rtage(sc);
2700146985Sthompsa
2701148887Srwatson	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
2702146985Sthompsa		callout_reset(&sc->sc_brcallout,
2703146985Sthompsa		    bridge_rtable_prune_period * hz, bridge_timer, sc);
2704146985Sthompsa}
2705146985Sthompsa
2706146985Sthompsa/*
2707146985Sthompsa * bridge_rtage:
2708146985Sthompsa *
2709146985Sthompsa *	Perform an aging cycle.
2710146985Sthompsa */
2711151313Sthompsastatic void
2712146985Sthompsabridge_rtage(struct bridge_softc *sc)
2713146985Sthompsa{
2714146985Sthompsa	struct bridge_rtnode *brt, *nbrt;
2715146985Sthompsa
2716146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2717146985Sthompsa
2718163142Sthompsa	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2719146985Sthompsa		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2720153976Sthompsa			if (time_uptime >= brt->brt_expire)
2721146985Sthompsa				bridge_rtnode_destroy(sc, brt);
2722146985Sthompsa		}
2723146985Sthompsa	}
2724146985Sthompsa}
2725146985Sthompsa
2726146985Sthompsa/*
2727146985Sthompsa * bridge_rtflush:
2728146985Sthompsa *
2729146985Sthompsa *	Remove all dynamic addresses from the bridge.
2730146985Sthompsa */
2731151313Sthompsastatic void
2732146985Sthompsabridge_rtflush(struct bridge_softc *sc, int full)
2733146985Sthompsa{
2734146985Sthompsa	struct bridge_rtnode *brt, *nbrt;
2735146985Sthompsa
2736146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2737146985Sthompsa
2738163142Sthompsa	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2739146985Sthompsa		if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2740146985Sthompsa			bridge_rtnode_destroy(sc, brt);
2741146985Sthompsa	}
2742146985Sthompsa}
2743146985Sthompsa
2744146985Sthompsa/*
2745146985Sthompsa * bridge_rtdaddr:
2746146985Sthompsa *
2747146985Sthompsa *	Remove an address from the table.
2748146985Sthompsa */
2749151313Sthompsastatic int
2750170681Sthompsabridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2751146985Sthompsa{
2752146985Sthompsa	struct bridge_rtnode *brt;
2753170681Sthompsa	int found = 0;
2754146985Sthompsa
2755146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2756146985Sthompsa
2757170681Sthompsa	/*
2758170681Sthompsa	 * If vlan is zero then we want to delete for all vlans so the lookup
2759170681Sthompsa	 * may return more than one.
2760170681Sthompsa	 */
2761170681Sthompsa	while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) {
2762170681Sthompsa		bridge_rtnode_destroy(sc, brt);
2763170681Sthompsa		found = 1;
2764170681Sthompsa	}
2765146985Sthompsa
2766170681Sthompsa	return (found ? 0 : ENOENT);
2767146985Sthompsa}
2768146985Sthompsa
2769146985Sthompsa/*
2770146985Sthompsa * bridge_rtdelete:
2771146985Sthompsa *
2772146985Sthompsa *	Delete routes to a speicifc member interface.
2773146985Sthompsa */
2774160769Sthompsastatic void
2775146985Sthompsabridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2776146985Sthompsa{
2777146985Sthompsa	struct bridge_rtnode *brt, *nbrt;
2778146985Sthompsa
2779146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2780146985Sthompsa
2781163142Sthompsa	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2782153497Sthompsa		if (brt->brt_ifp == ifp && (full ||
2783146985Sthompsa			    (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2784146985Sthompsa			bridge_rtnode_destroy(sc, brt);
2785146985Sthompsa	}
2786146985Sthompsa}
2787146985Sthompsa
2788146985Sthompsa/*
2789146985Sthompsa * bridge_rtable_init:
2790146985Sthompsa *
2791146985Sthompsa *	Initialize the route table for this bridge.
2792146985Sthompsa */
2793241183Sthompsastatic void
2794146985Sthompsabridge_rtable_init(struct bridge_softc *sc)
2795146985Sthompsa{
2796146985Sthompsa	int i;
2797146985Sthompsa
2798146985Sthompsa	sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2799241183Sthompsa	    M_DEVBUF, M_WAITOK);
2800146985Sthompsa
2801146985Sthompsa	for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2802146985Sthompsa		LIST_INIT(&sc->sc_rthash[i]);
2803146985Sthompsa
2804146985Sthompsa	sc->sc_rthash_key = arc4random();
2805146985Sthompsa	LIST_INIT(&sc->sc_rtlist);
2806146985Sthompsa}
2807146985Sthompsa
2808146985Sthompsa/*
2809146985Sthompsa * bridge_rtable_fini:
2810146985Sthompsa *
2811146985Sthompsa *	Deconstruct the route table for this bridge.
2812146985Sthompsa */
2813151313Sthompsastatic void
2814146985Sthompsabridge_rtable_fini(struct bridge_softc *sc)
2815146985Sthompsa{
2816146985Sthompsa
2817173320Sthompsa	KASSERT(sc->sc_brtcnt == 0,
2818173320Sthompsa	    ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt));
2819146985Sthompsa	free(sc->sc_rthash, M_DEVBUF);
2820146985Sthompsa}
2821146985Sthompsa
2822146985Sthompsa/*
2823146985Sthompsa * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2824146985Sthompsa * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2825146985Sthompsa */
2826146985Sthompsa#define	mix(a, b, c)							\
2827146985Sthompsado {									\
2828146985Sthompsa	a -= b; a -= c; a ^= (c >> 13);					\
2829146985Sthompsa	b -= c; b -= a; b ^= (a << 8);					\
2830146985Sthompsa	c -= a; c -= b; c ^= (b >> 13);					\
2831146985Sthompsa	a -= b; a -= c; a ^= (c >> 12);					\
2832146985Sthompsa	b -= c; b -= a; b ^= (a << 16);					\
2833146985Sthompsa	c -= a; c -= b; c ^= (b >> 5);					\
2834146985Sthompsa	a -= b; a -= c; a ^= (c >> 3);					\
2835146985Sthompsa	b -= c; b -= a; b ^= (a << 10);					\
2836146985Sthompsa	c -= a; c -= b; c ^= (b >> 15);					\
2837146985Sthompsa} while (/*CONSTCOND*/0)
2838146985Sthompsa
2839146985Sthompsastatic __inline uint32_t
2840146985Sthompsabridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2841146985Sthompsa{
2842146985Sthompsa	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2843146985Sthompsa
2844146985Sthompsa	b += addr[5] << 8;
2845146985Sthompsa	b += addr[4];
2846146985Sthompsa	a += addr[3] << 24;
2847146985Sthompsa	a += addr[2] << 16;
2848146985Sthompsa	a += addr[1] << 8;
2849146985Sthompsa	a += addr[0];
2850146985Sthompsa
2851146985Sthompsa	mix(a, b, c);
2852146985Sthompsa
2853146985Sthompsa	return (c & BRIDGE_RTHASH_MASK);
2854146985Sthompsa}
2855146985Sthompsa
2856146985Sthompsa#undef mix
2857146985Sthompsa
2858155143Sthompsastatic int
2859155143Sthompsabridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2860155143Sthompsa{
2861155143Sthompsa	int i, d;
2862155143Sthompsa
2863155143Sthompsa	for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2864155143Sthompsa		d = ((int)a[i]) - ((int)b[i]);
2865155143Sthompsa	}
2866155143Sthompsa
2867155143Sthompsa	return (d);
2868155143Sthompsa}
2869155143Sthompsa
2870146985Sthompsa/*
2871146985Sthompsa * bridge_rtnode_lookup:
2872146985Sthompsa *
2873170681Sthompsa *	Look up a bridge route node for the specified destination. Compare the
2874170681Sthompsa *	vlan id or if zero then just return the first match.
2875146985Sthompsa */
2876151345Sthompsastatic struct bridge_rtnode *
2877170681Sthompsabridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2878146985Sthompsa{
2879146985Sthompsa	struct bridge_rtnode *brt;
2880146985Sthompsa	uint32_t hash;
2881146985Sthompsa	int dir;
2882146985Sthompsa
2883146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2884146985Sthompsa
2885146985Sthompsa	hash = bridge_rthash(sc, addr);
2886146985Sthompsa	LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2887155143Sthompsa		dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2888170681Sthompsa		if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0))
2889146985Sthompsa			return (brt);
2890146985Sthompsa		if (dir > 0)
2891146985Sthompsa			return (NULL);
2892146985Sthompsa	}
2893146985Sthompsa
2894146985Sthompsa	return (NULL);
2895146985Sthompsa}
2896146985Sthompsa
2897146985Sthompsa/*
2898146985Sthompsa * bridge_rtnode_insert:
2899146985Sthompsa *
2900146985Sthompsa *	Insert the specified bridge node into the route table.  We
2901146985Sthompsa *	assume the entry is not already in the table.
2902146985Sthompsa */
2903151313Sthompsastatic int
2904146985Sthompsabridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2905146985Sthompsa{
2906146985Sthompsa	struct bridge_rtnode *lbrt;
2907146985Sthompsa	uint32_t hash;
2908146985Sthompsa	int dir;
2909146985Sthompsa
2910146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2911146985Sthompsa
2912146985Sthompsa	hash = bridge_rthash(sc, brt->brt_addr);
2913146985Sthompsa
2914146985Sthompsa	lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2915146985Sthompsa	if (lbrt == NULL) {
2916146985Sthompsa		LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2917146985Sthompsa		goto out;
2918146985Sthompsa	}
2919146985Sthompsa
2920146985Sthompsa	do {
2921155143Sthompsa		dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2922170681Sthompsa		if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan)
2923146985Sthompsa			return (EEXIST);
2924146985Sthompsa		if (dir > 0) {
2925146985Sthompsa			LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2926146985Sthompsa			goto out;
2927146985Sthompsa		}
2928146985Sthompsa		if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2929146985Sthompsa			LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2930146985Sthompsa			goto out;
2931146985Sthompsa		}
2932146985Sthompsa		lbrt = LIST_NEXT(lbrt, brt_hash);
2933146985Sthompsa	} while (lbrt != NULL);
2934146985Sthompsa
2935146985Sthompsa#ifdef DIAGNOSTIC
2936146985Sthompsa	panic("bridge_rtnode_insert: impossible");
2937146985Sthompsa#endif
2938146985Sthompsa
2939153497Sthompsaout:
2940146985Sthompsa	LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2941146985Sthompsa	sc->sc_brtcnt++;
2942146985Sthompsa
2943146985Sthompsa	return (0);
2944146985Sthompsa}
2945146985Sthompsa
2946146985Sthompsa/*
2947146985Sthompsa * bridge_rtnode_destroy:
2948146985Sthompsa *
2949146985Sthompsa *	Destroy a bridge rtnode.
2950146985Sthompsa */
2951151313Sthompsastatic void
2952146985Sthompsabridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2953146985Sthompsa{
2954146985Sthompsa	BRIDGE_LOCK_ASSERT(sc);
2955146985Sthompsa
2956146985Sthompsa	LIST_REMOVE(brt, brt_hash);
2957146985Sthompsa
2958146985Sthompsa	LIST_REMOVE(brt, brt_list);
2959146985Sthompsa	sc->sc_brtcnt--;
2960173320Sthompsa	brt->brt_dst->bif_addrcnt--;
2961146985Sthompsa	uma_zfree(bridge_rtnode_zone, brt);
2962146985Sthompsa}
2963146985Sthompsa
2964146985Sthompsa/*
2965163863Sthompsa * bridge_rtable_expire:
2966163863Sthompsa *
2967163863Sthompsa *	Set the expiry time for all routes on an interface.
2968163863Sthompsa */
2969163863Sthompsastatic void
2970163863Sthompsabridge_rtable_expire(struct ifnet *ifp, int age)
2971163863Sthompsa{
2972163863Sthompsa	struct bridge_softc *sc = ifp->if_bridge;
2973163863Sthompsa	struct bridge_rtnode *brt;
2974163863Sthompsa
2975163863Sthompsa	BRIDGE_LOCK(sc);
2976163863Sthompsa
2977163863Sthompsa	/*
2978163863Sthompsa	 * If the age is zero then flush, otherwise set all the expiry times to
2979163863Sthompsa	 * age for the interface
2980163863Sthompsa	 */
2981163863Sthompsa	if (age == 0)
2982163863Sthompsa		bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
2983163863Sthompsa	else {
2984163863Sthompsa		LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
2985163863Sthompsa			/* Cap the expiry time to 'age' */
2986163863Sthompsa			if (brt->brt_ifp == ifp &&
2987163863Sthompsa			    brt->brt_expire > time_uptime + age &&
2988163863Sthompsa			    (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2989163863Sthompsa				brt->brt_expire = time_uptime + age;
2990163863Sthompsa		}
2991163863Sthompsa	}
2992163863Sthompsa	BRIDGE_UNLOCK(sc);
2993163863Sthompsa}
2994163863Sthompsa
2995163863Sthompsa/*
2996160902Sthompsa * bridge_state_change:
2997160902Sthompsa *
2998160902Sthompsa *	Callback from the bridgestp code when a port changes states.
2999160902Sthompsa */
3000160902Sthompsastatic void
3001160902Sthompsabridge_state_change(struct ifnet *ifp, int state)
3002160902Sthompsa{
3003160902Sthompsa	struct bridge_softc *sc = ifp->if_bridge;
3004160902Sthompsa	static const char *stpstates[] = {
3005160902Sthompsa		"disabled",
3006160902Sthompsa		"listening",
3007160902Sthompsa		"learning",
3008160902Sthompsa		"forwarding",
3009160902Sthompsa		"blocking",
3010163863Sthompsa		"discarding"
3011160902Sthompsa	};
3012160902Sthompsa
3013160902Sthompsa	if (log_stp)
3014160902Sthompsa		log(LOG_NOTICE, "%s: state changed to %s on %s\n",
3015160902Sthompsa		    sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname);
3016160902Sthompsa}
3017160902Sthompsa
3018160902Sthompsa/*
3019146985Sthompsa * Send bridge packets through pfil if they are one of the types pfil can deal
3020146985Sthompsa * with, or if they are ARP or REVARP.  (pfil will pass ARP and REVARP without
3021147786Sthompsa * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3022147786Sthompsa * that interface.
3023146985Sthompsa */
3024153497Sthompsastatic int
3025153497Sthompsabridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3026146985Sthompsa{
3027158592Sdhartmei	int snap, error, i, hlen;
3028146985Sthompsa	struct ether_header *eh1, eh2;
3029146985Sthompsa	struct ip *ip;
3030147665Sthompsa	struct llc llc1;
3031146985Sthompsa	u_int16_t ether_type;
3032146985Sthompsa
3033146985Sthompsa	snap = 0;
3034146985Sthompsa	error = -1;	/* Default error if not error == 0 */
3035147111Sthompsa
3036170139Sthompsa#if 0
3037157155Sthompsa	/* we may return with the IP fields swapped, ensure its not shared */
3038157155Sthompsa	KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
3039170139Sthompsa#endif
3040157155Sthompsa
3041153831Sthompsa	if (pfil_bridge == 0 && pfil_member == 0 && pfil_ipfw == 0)
3042158667Sthompsa		return (0); /* filtering is disabled */
3043153831Sthompsa
3044147111Sthompsa	i = min((*mp)->m_pkthdr.len, max_protohdr);
3045147111Sthompsa	if ((*mp)->m_len < i) {
3046147111Sthompsa	    *mp = m_pullup(*mp, i);
3047147111Sthompsa	    if (*mp == NULL) {
3048147111Sthompsa		printf("%s: m_pullup failed\n", __func__);
3049158667Sthompsa		return (-1);
3050147111Sthompsa	    }
3051147111Sthompsa	}
3052147111Sthompsa
3053146985Sthompsa	eh1 = mtod(*mp, struct ether_header *);
3054146985Sthompsa	ether_type = ntohs(eh1->ether_type);
3055146985Sthompsa
3056146985Sthompsa	/*
3057146985Sthompsa	 * Check for SNAP/LLC.
3058146985Sthompsa	 */
3059146985Sthompsa	if (ether_type < ETHERMTU) {
3060147665Sthompsa		struct llc *llc2 = (struct llc *)(eh1 + 1);
3061146985Sthompsa
3062146985Sthompsa		if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3063147665Sthompsa		    llc2->llc_dsap == LLC_SNAP_LSAP &&
3064147665Sthompsa		    llc2->llc_ssap == LLC_SNAP_LSAP &&
3065147665Sthompsa		    llc2->llc_control == LLC_UI) {
3066147665Sthompsa			ether_type = htons(llc2->llc_un.type_snap.ether_type);
3067146985Sthompsa			snap = 1;
3068146985Sthompsa		}
3069146985Sthompsa	}
3070146985Sthompsa
3071162561Sthompsa	/*
3072162561Sthompsa	 * If we're trying to filter bridge traffic, don't look at anything
3073162561Sthompsa	 * other than IP and ARP traffic.  If the filter doesn't understand
3074162561Sthompsa	 * IPv6, don't allow IPv6 through the bridge either.  This is lame
3075162561Sthompsa	 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3076162561Sthompsa	 * but of course we don't have an AppleTalk filter to begin with.
3077162561Sthompsa	 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3078162561Sthompsa	 * ARP traffic.)
3079162561Sthompsa	 */
3080162561Sthompsa	switch (ether_type) {
3081162561Sthompsa		case ETHERTYPE_ARP:
3082162561Sthompsa		case ETHERTYPE_REVARP:
3083162561Sthompsa			if (pfil_ipfw_arp == 0)
3084162561Sthompsa				return (0); /* Automatically pass */
3085162561Sthompsa			break;
3086162561Sthompsa
3087162561Sthompsa		case ETHERTYPE_IP:
3088162561Sthompsa#ifdef INET6
3089162561Sthompsa		case ETHERTYPE_IPV6:
3090162561Sthompsa#endif /* INET6 */
3091162561Sthompsa			break;
3092162561Sthompsa		default:
3093162561Sthompsa			/*
3094162561Sthompsa			 * Check to see if the user wants to pass non-ip
3095162561Sthompsa			 * packets, these will not be checked by pfil(9) and
3096162561Sthompsa			 * passed unconditionally so the default is to drop.
3097162561Sthompsa			 */
3098162561Sthompsa			if (pfil_onlyip)
3099162561Sthompsa				goto bad;
3100162561Sthompsa	}
3101162561Sthompsa
3102240099Smelifaro	/* Run the packet through pfil before stripping link headers */
3103240099Smelifaro	if (PFIL_HOOKED(&V_link_pfil_hook) && pfil_ipfw != 0 &&
3104240099Smelifaro			dir == PFIL_OUT && ifp != NULL) {
3105240099Smelifaro
3106240099Smelifaro		error = pfil_run_hooks(&V_link_pfil_hook, mp, ifp, dir, NULL);
3107240099Smelifaro
3108240099Smelifaro		if (*mp == NULL || error != 0) /* packet consumed by filter */
3109240099Smelifaro			return (error);
3110240099Smelifaro	}
3111240099Smelifaro
3112146985Sthompsa	/* Strip off the Ethernet header and keep a copy. */
3113146985Sthompsa	m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3114146985Sthompsa	m_adj(*mp, ETHER_HDR_LEN);
3115146985Sthompsa
3116146985Sthompsa	/* Strip off snap header, if present */
3117146985Sthompsa	if (snap) {
3118147665Sthompsa		m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3119146985Sthompsa		m_adj(*mp, sizeof(struct llc));
3120146985Sthompsa	}
3121146985Sthompsa
3122147744Sthompsa	/*
3123147744Sthompsa	 * Check the IP header for alignment and errors
3124147744Sthompsa	 */
3125147744Sthompsa	if (dir == PFIL_IN) {
3126147744Sthompsa		switch (ether_type) {
3127147744Sthompsa			case ETHERTYPE_IP:
3128147744Sthompsa				error = bridge_ip_checkbasic(mp);
3129147744Sthompsa				break;
3130158667Sthompsa#ifdef INET6
3131147744Sthompsa			case ETHERTYPE_IPV6:
3132147744Sthompsa				error = bridge_ip6_checkbasic(mp);
3133147744Sthompsa				break;
3134158667Sthompsa#endif /* INET6 */
3135147744Sthompsa			default:
3136147744Sthompsa				error = 0;
3137147744Sthompsa		}
3138147744Sthompsa		if (error)
3139147744Sthompsa			goto bad;
3140147744Sthompsa	}
3141147744Sthompsa
3142147744Sthompsa	error = 0;
3143147744Sthompsa
3144146985Sthompsa	/*
3145162561Sthompsa	 * Run the packet through pfil
3146146985Sthompsa	 */
3147158667Sthompsa	switch (ether_type) {
3148158667Sthompsa	case ETHERTYPE_IP:
3149146985Sthompsa		/*
3150146985Sthompsa		 * Run pfil on the member interface and the bridge, both can
3151146985Sthompsa		 * be skipped by clearing pfil_member or pfil_bridge.
3152146985Sthompsa		 *
3153146985Sthompsa		 * Keep the order:
3154146985Sthompsa		 *   in_if -> bridge_if -> out_if
3155146985Sthompsa		 */
3156147786Sthompsa		if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3157197952Sjulian			error = pfil_run_hooks(&V_inet_pfil_hook, mp, bifp,
3158146985Sthompsa					dir, NULL);
3159146985Sthompsa
3160147744Sthompsa		if (*mp == NULL || error != 0) /* filter may consume */
3161147205Sthompsa			break;
3162147205Sthompsa
3163147786Sthompsa		if (pfil_member && ifp != NULL)
3164197952Sjulian			error = pfil_run_hooks(&V_inet_pfil_hook, mp, ifp,
3165146985Sthompsa					dir, NULL);
3166146985Sthompsa
3167147744Sthompsa		if (*mp == NULL || error != 0) /* filter may consume */
3168147205Sthompsa			break;
3169147205Sthompsa
3170147786Sthompsa		if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3171197952Sjulian			error = pfil_run_hooks(&V_inet_pfil_hook, mp, bifp,
3172146985Sthompsa					dir, NULL);
3173146985Sthompsa
3174158140Sthompsa		if (*mp == NULL || error != 0) /* filter may consume */
3175158140Sthompsa			break;
3176158140Sthompsa
3177158140Sthompsa		/* check if we need to fragment the packet */
3178158140Sthompsa		if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3179158140Sthompsa			i = (*mp)->m_pkthdr.len;
3180158140Sthompsa			if (i > ifp->if_mtu) {
3181158140Sthompsa				error = bridge_fragment(ifp, *mp, &eh2, snap,
3182158140Sthompsa					    &llc1);
3183158140Sthompsa				return (error);
3184158140Sthompsa			}
3185146985Sthompsa		}
3186146985Sthompsa
3187241245Sglebius		/* Recalculate the ip checksum. */
3188158140Sthompsa		ip = mtod(*mp, struct ip *);
3189158592Sdhartmei		hlen = ip->ip_hl << 2;
3190158667Sthompsa		if (hlen < sizeof(struct ip))
3191158667Sthompsa			goto bad;
3192158667Sthompsa		if (hlen > (*mp)->m_len) {
3193158667Sthompsa			if ((*mp = m_pullup(*mp, hlen)) == 0)
3194158667Sthompsa				goto bad;
3195158667Sthompsa			ip = mtod(*mp, struct ip *);
3196158667Sthompsa			if (ip == NULL)
3197158667Sthompsa				goto bad;
3198158667Sthompsa		}
3199158667Sthompsa		ip->ip_sum = 0;
3200158667Sthompsa		if (hlen == sizeof(struct ip))
3201158667Sthompsa			ip->ip_sum = in_cksum_hdr(ip);
3202158667Sthompsa		else
3203158667Sthompsa			ip->ip_sum = in_cksum(*mp, hlen);
3204158140Sthompsa
3205146985Sthompsa		break;
3206158667Sthompsa#ifdef INET6
3207158667Sthompsa	case ETHERTYPE_IPV6:
3208147786Sthompsa		if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3209197952Sjulian			error = pfil_run_hooks(&V_inet6_pfil_hook, mp, bifp,
3210147040Sthompsa					dir, NULL);
3211147040Sthompsa
3212147744Sthompsa		if (*mp == NULL || error != 0) /* filter may consume */
3213147205Sthompsa			break;
3214147205Sthompsa
3215147786Sthompsa		if (pfil_member && ifp != NULL)
3216197952Sjulian			error = pfil_run_hooks(&V_inet6_pfil_hook, mp, ifp,
3217146985Sthompsa					dir, NULL);
3218147040Sthompsa
3219147744Sthompsa		if (*mp == NULL || error != 0) /* filter may consume */
3220147205Sthompsa			break;
3221147205Sthompsa
3222147786Sthompsa		if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3223197952Sjulian			error = pfil_run_hooks(&V_inet6_pfil_hook, mp, bifp,
3224147040Sthompsa					dir, NULL);
3225146985Sthompsa		break;
3226158667Sthompsa#endif
3227158667Sthompsa	default:
3228162561Sthompsa		error = 0;
3229146985Sthompsa		break;
3230146985Sthompsa	}
3231146985Sthompsa
3232146985Sthompsa	if (*mp == NULL)
3233158667Sthompsa		return (error);
3234146985Sthompsa	if (error != 0)
3235146985Sthompsa		goto bad;
3236146985Sthompsa
3237146985Sthompsa	error = -1;
3238146985Sthompsa
3239146985Sthompsa	/*
3240146985Sthompsa	 * Finally, put everything back the way it was and return
3241146985Sthompsa	 */
3242146985Sthompsa	if (snap) {
3243243882Sglebius		M_PREPEND(*mp, sizeof(struct llc), M_NOWAIT);
3244146985Sthompsa		if (*mp == NULL)
3245158667Sthompsa			return (error);
3246147665Sthompsa		bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3247146985Sthompsa	}
3248146985Sthompsa
3249243882Sglebius	M_PREPEND(*mp, ETHER_HDR_LEN, M_NOWAIT);
3250146985Sthompsa	if (*mp == NULL)
3251158667Sthompsa		return (error);
3252146985Sthompsa	bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3253146985Sthompsa
3254158667Sthompsa	return (0);
3255146985Sthompsa
3256153497Sthompsabad:
3257146985Sthompsa	m_freem(*mp);
3258146985Sthompsa	*mp = NULL;
3259158667Sthompsa	return (error);
3260146985Sthompsa}
3261146985Sthompsa
3262146985Sthompsa/*
3263146985Sthompsa * Perform basic checks on header size since
3264146985Sthompsa * pfil assumes ip_input has already processed
3265146985Sthompsa * it for it.  Cut-and-pasted from ip_input.c.
3266146985Sthompsa * Given how simple the IPv6 version is,
3267146985Sthompsa * does the IPv4 version really need to be
3268146985Sthompsa * this complicated?
3269146985Sthompsa *
3270146985Sthompsa * XXX Should we update ipstat here, or not?
3271146985Sthompsa * XXX Right now we update ipstat but not
3272146985Sthompsa * XXX csum_counter.
3273146985Sthompsa */
3274146985Sthompsastatic int
3275146985Sthompsabridge_ip_checkbasic(struct mbuf **mp)
3276146985Sthompsa{
3277146985Sthompsa	struct mbuf *m = *mp;
3278146985Sthompsa	struct ip *ip;
3279146985Sthompsa	int len, hlen;
3280146985Sthompsa	u_short sum;
3281146985Sthompsa
3282146985Sthompsa	if (*mp == NULL)
3283158667Sthompsa		return (-1);
3284146985Sthompsa
3285147744Sthompsa	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3286147744Sthompsa		if ((m = m_copyup(m, sizeof(struct ip),
3287147744Sthompsa			(max_linkhdr + 3) & ~3)) == NULL) {
3288147744Sthompsa			/* XXXJRT new stat, please */
3289196039Srwatson			KMOD_IPSTAT_INC(ips_toosmall);
3290147744Sthompsa			goto bad;
3291147744Sthompsa		}
3292147744Sthompsa	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
3293146985Sthompsa		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3294196039Srwatson			KMOD_IPSTAT_INC(ips_toosmall);
3295146985Sthompsa			goto bad;
3296146985Sthompsa		}
3297146985Sthompsa	}
3298146985Sthompsa	ip = mtod(m, struct ip *);
3299146985Sthompsa	if (ip == NULL) goto bad;
3300146985Sthompsa
3301146985Sthompsa	if (ip->ip_v != IPVERSION) {
3302196039Srwatson		KMOD_IPSTAT_INC(ips_badvers);
3303146985Sthompsa		goto bad;
3304146985Sthompsa	}
3305146985Sthompsa	hlen = ip->ip_hl << 2;
3306146985Sthompsa	if (hlen < sizeof(struct ip)) { /* minimum header length */
3307196039Srwatson		KMOD_IPSTAT_INC(ips_badhlen);
3308146985Sthompsa		goto bad;
3309146985Sthompsa	}
3310146985Sthompsa	if (hlen > m->m_len) {
3311146985Sthompsa		if ((m = m_pullup(m, hlen)) == 0) {
3312196039Srwatson			KMOD_IPSTAT_INC(ips_badhlen);
3313146985Sthompsa			goto bad;
3314146985Sthompsa		}
3315146985Sthompsa		ip = mtod(m, struct ip *);
3316146985Sthompsa		if (ip == NULL) goto bad;
3317146985Sthompsa	}
3318146985Sthompsa
3319146985Sthompsa	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3320146985Sthompsa		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3321146985Sthompsa	} else {
3322146985Sthompsa		if (hlen == sizeof(struct ip)) {
3323146985Sthompsa			sum = in_cksum_hdr(ip);
3324146985Sthompsa		} else {
3325146985Sthompsa			sum = in_cksum(m, hlen);
3326146985Sthompsa		}
3327146985Sthompsa	}
3328146985Sthompsa	if (sum) {
3329196039Srwatson		KMOD_IPSTAT_INC(ips_badsum);
3330146985Sthompsa		goto bad;
3331146985Sthompsa	}
3332146985Sthompsa
3333146985Sthompsa	/* Retrieve the packet length. */
3334146985Sthompsa	len = ntohs(ip->ip_len);
3335146985Sthompsa
3336146985Sthompsa	/*
3337146985Sthompsa	 * Check for additional length bogosity
3338146985Sthompsa	 */
3339146985Sthompsa	if (len < hlen) {
3340196039Srwatson		KMOD_IPSTAT_INC(ips_badlen);
3341146985Sthompsa		goto bad;
3342146985Sthompsa	}
3343146985Sthompsa
3344146985Sthompsa	/*
3345146985Sthompsa	 * Check that the amount of data in the buffers
3346146985Sthompsa	 * is as at least much as the IP header would have us expect.
3347146985Sthompsa	 * Drop packet if shorter than we expect.
3348146985Sthompsa	 */
3349146985Sthompsa	if (m->m_pkthdr.len < len) {
3350196039Srwatson		KMOD_IPSTAT_INC(ips_tooshort);
3351146985Sthompsa		goto bad;
3352146985Sthompsa	}
3353146985Sthompsa
3354146985Sthompsa	/* Checks out, proceed */
3355146985Sthompsa	*mp = m;
3356158667Sthompsa	return (0);
3357146985Sthompsa
3358153497Sthompsabad:
3359146985Sthompsa	*mp = m;
3360158667Sthompsa	return (-1);
3361146985Sthompsa}
3362146985Sthompsa
3363158667Sthompsa#ifdef INET6
3364146985Sthompsa/*
3365146985Sthompsa * Same as above, but for IPv6.
3366146985Sthompsa * Cut-and-pasted from ip6_input.c.
3367146985Sthompsa * XXX Should we update ip6stat, or not?
3368146985Sthompsa */
3369146985Sthompsastatic int
3370146985Sthompsabridge_ip6_checkbasic(struct mbuf **mp)
3371146985Sthompsa{
3372146985Sthompsa	struct mbuf *m = *mp;
3373146985Sthompsa	struct ip6_hdr *ip6;
3374146985Sthompsa
3375146985Sthompsa	/*
3376146985Sthompsa	 * If the IPv6 header is not aligned, slurp it up into a new
3377146985Sthompsa	 * mbuf with space for link headers, in the event we forward
3378146985Sthompsa	 * it.  Otherwise, if it is aligned, make sure the entire base
3379146985Sthompsa	 * IPv6 header is in the first mbuf of the chain.
3380147744Sthompsa	 */
3381146985Sthompsa	if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3382146985Sthompsa		struct ifnet *inifp = m->m_pkthdr.rcvif;
3383146985Sthompsa		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3384146985Sthompsa			    (max_linkhdr + 3) & ~3)) == NULL) {
3385147744Sthompsa			/* XXXJRT new stat, please */
3386249294Sae			IP6STAT_INC(ip6s_toosmall);
3387146985Sthompsa			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3388146985Sthompsa			goto bad;
3389146985Sthompsa		}
3390147744Sthompsa	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3391146985Sthompsa		struct ifnet *inifp = m->m_pkthdr.rcvif;
3392146985Sthompsa		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3393249294Sae			IP6STAT_INC(ip6s_toosmall);
3394146985Sthompsa			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3395146985Sthompsa			goto bad;
3396146985Sthompsa		}
3397146985Sthompsa	}
3398146985Sthompsa
3399146985Sthompsa	ip6 = mtod(m, struct ip6_hdr *);
3400146985Sthompsa
3401146985Sthompsa	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3402249294Sae		IP6STAT_INC(ip6s_badvers);
3403146985Sthompsa		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3404146985Sthompsa		goto bad;
3405146985Sthompsa	}
3406146985Sthompsa
3407146985Sthompsa	/* Checks out, proceed */
3408146985Sthompsa	*mp = m;
3409158667Sthompsa	return (0);
3410146985Sthompsa
3411153497Sthompsabad:
3412146985Sthompsa	*mp = m;
3413158667Sthompsa	return (-1);
3414146985Sthompsa}
3415158667Sthompsa#endif /* INET6 */
3416158140Sthompsa
3417158140Sthompsa/*
3418158140Sthompsa * bridge_fragment:
3419158140Sthompsa *
3420158140Sthompsa *	Return a fragmented mbuf chain.
3421158140Sthompsa */
3422158140Sthompsastatic int
3423158140Sthompsabridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
3424158140Sthompsa    int snap, struct llc *llc)
3425158140Sthompsa{
3426158140Sthompsa	struct mbuf *m0;
3427158140Sthompsa	struct ip *ip;
3428158140Sthompsa	int error = -1;
3429158140Sthompsa
3430158140Sthompsa	if (m->m_len < sizeof(struct ip) &&
3431158140Sthompsa	    (m = m_pullup(m, sizeof(struct ip))) == NULL)
3432158140Sthompsa		goto out;
3433158140Sthompsa	ip = mtod(m, struct ip *);
3434158140Sthompsa
3435242161Sglebius	m->m_pkthdr.csum_flags |= CSUM_IP;
3436242161Sglebius	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist);
3437158140Sthompsa	if (error)
3438158140Sthompsa		goto out;
3439158140Sthompsa
3440158140Sthompsa	/* walk the chain and re-add the Ethernet header */
3441158140Sthompsa	for (m0 = m; m0; m0 = m0->m_nextpkt) {
3442158140Sthompsa		if (error == 0) {
3443158140Sthompsa			if (snap) {
3444243882Sglebius				M_PREPEND(m0, sizeof(struct llc), M_NOWAIT);
3445158140Sthompsa				if (m0 == NULL) {
3446158140Sthompsa					error = ENOBUFS;
3447158140Sthompsa					continue;
3448158140Sthompsa				}
3449158140Sthompsa				bcopy(llc, mtod(m0, caddr_t),
3450158140Sthompsa				    sizeof(struct llc));
3451158140Sthompsa			}
3452243882Sglebius			M_PREPEND(m0, ETHER_HDR_LEN, M_NOWAIT);
3453158140Sthompsa			if (m0 == NULL) {
3454158140Sthompsa				error = ENOBUFS;
3455158140Sthompsa				continue;
3456158140Sthompsa			}
3457158140Sthompsa			bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
3458252511Shrs		} else
3459158140Sthompsa			m_freem(m);
3460158140Sthompsa	}
3461158140Sthompsa
3462158140Sthompsa	if (error == 0)
3463196039Srwatson		KMOD_IPSTAT_INC(ips_fragmented);
3464158140Sthompsa
3465158140Sthompsa	return (error);
3466158140Sthompsa
3467158140Sthompsaout:
3468158140Sthompsa	if (m != NULL)
3469158140Sthompsa		m_freem(m);
3470158140Sthompsa	return (error);
3471158140Sthompsa}
3472234487Sthompsa
3473234487Sthompsastatic void
3474234487Sthompsabridge_linkstate(struct ifnet *ifp)
3475234487Sthompsa{
3476234487Sthompsa	struct bridge_softc *sc = ifp->if_bridge;
3477236916Sthompsa	struct bridge_iflist *bif;
3478234487Sthompsa
3479234487Sthompsa	BRIDGE_LOCK(sc);
3480234487Sthompsa	bif = bridge_lookup_member_if(sc, ifp);
3481234487Sthompsa	if (bif == NULL) {
3482234487Sthompsa		BRIDGE_UNLOCK(sc);
3483234487Sthompsa		return;
3484234487Sthompsa	}
3485236916Sthompsa	bridge_linkcheck(sc);
3486236916Sthompsa	BRIDGE_UNLOCK(sc);
3487236916Sthompsa
3488236916Sthompsa	bstp_linkstate(&bif->bif_stp);
3489236916Sthompsa}
3490236916Sthompsa
3491236916Sthompsastatic void
3492236916Sthompsabridge_linkcheck(struct bridge_softc *sc)
3493236916Sthompsa{
3494236916Sthompsa	struct bridge_iflist *bif;
3495236916Sthompsa	int new_link, hasls;
3496236916Sthompsa
3497236916Sthompsa	BRIDGE_LOCK_ASSERT(sc);
3498234487Sthompsa	new_link = LINK_STATE_DOWN;
3499234487Sthompsa	hasls = 0;
3500234487Sthompsa	/* Our link is considered up if at least one of our ports is active */
3501236916Sthompsa	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
3502236916Sthompsa		if (bif->bif_ifp->if_capabilities & IFCAP_LINKSTATE)
3503234487Sthompsa			hasls++;
3504236916Sthompsa		if (bif->bif_ifp->if_link_state == LINK_STATE_UP) {
3505234487Sthompsa			new_link = LINK_STATE_UP;
3506234487Sthompsa			break;
3507234487Sthompsa		}
3508234487Sthompsa	}
3509234487Sthompsa	if (!LIST_EMPTY(&sc->sc_iflist) && !hasls) {
3510234487Sthompsa		/* If no interfaces support link-state then we default to up */
3511234487Sthompsa		new_link = LINK_STATE_UP;
3512234487Sthompsa	}
3513234487Sthompsa	if_link_state_change(sc->sc_ifp, new_link);
3514234487Sthompsa}
3515