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