if_bridge.c revision 1.17
1/*	$NetBSD: if_bridge.c,v 1.17 2003/08/11 15:14:16 itojun 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 *	- Add packet filter hooks.
82 */
83
84#include <sys/cdefs.h>
85__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.17 2003/08/11 15:14:16 itojun Exp $");
86
87#include "opt_bridge_ipf.h"
88#include "opt_inet.h"
89#include "opt_pfil_hooks.h"
90#include "bpfilter.h"
91
92#include <sys/param.h>
93#include <sys/kernel.h>
94#include <sys/mbuf.h>
95#include <sys/queue.h>
96#include <sys/socket.h>
97#include <sys/sockio.h>
98#include <sys/systm.h>
99#include <sys/proc.h>
100#include <sys/pool.h>
101
102#if NBPFILTER > 0
103#include <net/bpf.h>
104#endif
105#include <net/if.h>
106#include <net/if_dl.h>
107#include <net/if_types.h>
108#include <net/if_llc.h>
109
110#include <net/if_ether.h>
111#include <net/if_bridgevar.h>
112
113#ifdef BRIDGE_IPF /* Used for bridge_ip[6]_checkbasic */
114#include <netinet/in.h>
115#include <netinet/in_systm.h>
116#include <netinet/ip.h>
117#include <netinet/ip_var.h>
118
119#include <netinet/ip6.h>
120#include <netinet6/in6_var.h>
121#include <netinet6/ip6_var.h>
122#endif /* BRIDGE_IPF */
123
124/*
125 * Size of the route hash table.  Must be a power of two.
126 */
127#ifndef BRIDGE_RTHASH_SIZE
128#define	BRIDGE_RTHASH_SIZE		1024
129#endif
130
131#define	BRIDGE_RTHASH_MASK		(BRIDGE_RTHASH_SIZE - 1)
132
133/*
134 * Maximum number of addresses to cache.
135 */
136#ifndef BRIDGE_RTABLE_MAX
137#define	BRIDGE_RTABLE_MAX		100
138#endif
139
140/*
141 * Spanning tree defaults.
142 */
143#define	BSTP_DEFAULT_MAX_AGE		(20 * 256)
144#define	BSTP_DEFAULT_HELLO_TIME		(2 * 256)
145#define	BSTP_DEFAULT_FORWARD_DELAY	(15 * 256)
146#define	BSTP_DEFAULT_HOLD_TIME		(1 * 256)
147#define	BSTP_DEFAULT_BRIDGE_PRIORITY	0x8000
148#define	BSTP_DEFAULT_PORT_PRIORITY	0x80
149#define	BSTP_DEFAULT_PATH_COST		55
150
151/*
152 * Timeout (in seconds) for entries learned dynamically.
153 */
154#ifndef BRIDGE_RTABLE_TIMEOUT
155#define	BRIDGE_RTABLE_TIMEOUT		(20 * 60)	/* same as ARP */
156#endif
157
158/*
159 * Number of seconds between walks of the route list.
160 */
161#ifndef BRIDGE_RTABLE_PRUNE_PERIOD
162#define	BRIDGE_RTABLE_PRUNE_PERIOD	(5 * 60)
163#endif
164
165int	bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
166
167struct pool bridge_rtnode_pool;
168
169void	bridgeattach(int);
170
171int	bridge_clone_create(struct if_clone *, int);
172void	bridge_clone_destroy(struct ifnet *);
173
174int	bridge_ioctl(struct ifnet *, u_long, caddr_t);
175int	bridge_init(struct ifnet *);
176void	bridge_stop(struct ifnet *, int);
177void	bridge_start(struct ifnet *);
178
179void	bridge_forward(struct bridge_softc *, struct mbuf *m);
180
181void	bridge_timer(void *);
182
183void	bridge_broadcast(struct bridge_softc *, struct ifnet *, struct mbuf *);
184
185int	bridge_rtupdate(struct bridge_softc *, const uint8_t *,
186	    struct ifnet *, int, uint8_t);
187struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
188void	bridge_rttrim(struct bridge_softc *);
189void	bridge_rtage(struct bridge_softc *);
190void	bridge_rtflush(struct bridge_softc *, int);
191int	bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
192void	bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp);
193
194int	bridge_rtable_init(struct bridge_softc *);
195void	bridge_rtable_fini(struct bridge_softc *);
196
197struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
198	    const uint8_t *);
199int	bridge_rtnode_insert(struct bridge_softc *, struct bridge_rtnode *);
200void	bridge_rtnode_destroy(struct bridge_softc *, struct bridge_rtnode *);
201
202struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
203	    const char *name);
204struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
205	    struct ifnet *ifp);
206void	bridge_delete_member(struct bridge_softc *, struct bridge_iflist *);
207
208int	bridge_ioctl_add(struct bridge_softc *, void *);
209int	bridge_ioctl_del(struct bridge_softc *, void *);
210int	bridge_ioctl_gifflags(struct bridge_softc *, void *);
211int	bridge_ioctl_sifflags(struct bridge_softc *, void *);
212int	bridge_ioctl_scache(struct bridge_softc *, void *);
213int	bridge_ioctl_gcache(struct bridge_softc *, void *);
214int	bridge_ioctl_gifs(struct bridge_softc *, void *);
215int	bridge_ioctl_rts(struct bridge_softc *, void *);
216int	bridge_ioctl_saddr(struct bridge_softc *, void *);
217int	bridge_ioctl_sto(struct bridge_softc *, void *);
218int	bridge_ioctl_gto(struct bridge_softc *, void *);
219int	bridge_ioctl_daddr(struct bridge_softc *, void *);
220int	bridge_ioctl_flush(struct bridge_softc *, void *);
221int	bridge_ioctl_gpri(struct bridge_softc *, void *);
222int	bridge_ioctl_spri(struct bridge_softc *, void *);
223int	bridge_ioctl_ght(struct bridge_softc *, void *);
224int	bridge_ioctl_sht(struct bridge_softc *, void *);
225int	bridge_ioctl_gfd(struct bridge_softc *, void *);
226int	bridge_ioctl_sfd(struct bridge_softc *, void *);
227int	bridge_ioctl_gma(struct bridge_softc *, void *);
228int	bridge_ioctl_sma(struct bridge_softc *, void *);
229int	bridge_ioctl_sifprio(struct bridge_softc *, void *);
230int	bridge_ioctl_sifcost(struct bridge_softc *, void *);
231#ifdef BRIDGE_IPF
232int	bridge_ioctl_gfilt(struct bridge_softc *, void *);
233int	bridge_ioctl_sfilt(struct bridge_softc *, void *);
234static int bridge_ipf(void *, struct mbuf **, struct ifnet *, int);
235static int bridge_ip_checkbasic(struct mbuf **mp);
236# ifdef INET6
237static int bridge_ip6_checkbasic(struct mbuf **mp);
238# endif /* INET6 */
239#endif /* BRIDGE_IPF */
240
241struct bridge_control {
242	int	(*bc_func)(struct bridge_softc *, void *);
243	int	bc_argsize;
244	int	bc_flags;
245};
246
247#define	BC_F_COPYIN		0x01	/* copy arguments in */
248#define	BC_F_COPYOUT		0x02	/* copy arguments out */
249#define	BC_F_SUSER		0x04	/* do super-user check */
250
251const struct bridge_control bridge_control_table[] = {
252	{ bridge_ioctl_add,		sizeof(struct ifbreq),
253	  BC_F_COPYIN|BC_F_SUSER },
254	{ bridge_ioctl_del,		sizeof(struct ifbreq),
255	  BC_F_COPYIN|BC_F_SUSER },
256
257	{ bridge_ioctl_gifflags,	sizeof(struct ifbreq),
258	  BC_F_COPYIN|BC_F_COPYOUT },
259	{ bridge_ioctl_sifflags,	sizeof(struct ifbreq),
260	  BC_F_COPYIN|BC_F_SUSER },
261
262	{ bridge_ioctl_scache,		sizeof(struct ifbrparam),
263	  BC_F_COPYIN|BC_F_SUSER },
264	{ bridge_ioctl_gcache,		sizeof(struct ifbrparam),
265	  BC_F_COPYOUT },
266
267	{ bridge_ioctl_gifs,		sizeof(struct ifbifconf),
268	  BC_F_COPYIN|BC_F_COPYOUT },
269	{ bridge_ioctl_rts,		sizeof(struct ifbaconf),
270	  BC_F_COPYIN|BC_F_COPYOUT },
271
272	{ bridge_ioctl_saddr,		sizeof(struct ifbareq),
273	  BC_F_COPYIN|BC_F_SUSER },
274
275	{ bridge_ioctl_sto,		sizeof(struct ifbrparam),
276	  BC_F_COPYIN|BC_F_SUSER },
277	{ bridge_ioctl_gto,		sizeof(struct ifbrparam),
278	  BC_F_COPYOUT },
279
280	{ bridge_ioctl_daddr,		sizeof(struct ifbareq),
281	  BC_F_COPYIN|BC_F_SUSER },
282
283	{ bridge_ioctl_flush,		sizeof(struct ifbreq),
284	  BC_F_COPYIN|BC_F_SUSER },
285
286	{ bridge_ioctl_gpri,		sizeof(struct ifbrparam),
287	  BC_F_COPYOUT },
288	{ bridge_ioctl_spri,		sizeof(struct ifbrparam),
289	  BC_F_COPYIN|BC_F_SUSER },
290
291	{ bridge_ioctl_ght,		sizeof(struct ifbrparam),
292	  BC_F_COPYOUT },
293	{ bridge_ioctl_sht,		sizeof(struct ifbrparam),
294	  BC_F_COPYIN|BC_F_SUSER },
295
296	{ bridge_ioctl_gfd,		sizeof(struct ifbrparam),
297	  BC_F_COPYOUT },
298	{ bridge_ioctl_sfd,		sizeof(struct ifbrparam),
299	  BC_F_COPYIN|BC_F_SUSER },
300
301	{ bridge_ioctl_gma,		sizeof(struct ifbrparam),
302	  BC_F_COPYOUT },
303	{ bridge_ioctl_sma,		sizeof(struct ifbrparam),
304	  BC_F_COPYIN|BC_F_SUSER },
305
306	{ bridge_ioctl_sifprio,		sizeof(struct ifbreq),
307	  BC_F_COPYIN|BC_F_SUSER },
308
309	{ bridge_ioctl_sifcost,		sizeof(struct ifbreq),
310	  BC_F_COPYIN|BC_F_SUSER },
311#ifdef BRIDGE_IPF
312	{ bridge_ioctl_gfilt,		sizeof(struct ifbrparam),
313	  BC_F_COPYOUT },
314	{ bridge_ioctl_sfilt,		sizeof(struct ifbrparam),
315	  BC_F_COPYIN|BC_F_SUSER },
316#endif /* BRIDGE_IPF */
317};
318const int bridge_control_table_size =
319    sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
320
321LIST_HEAD(, bridge_softc) bridge_list;
322
323struct if_clone bridge_cloner =
324    IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy);
325
326/*
327 * bridgeattach:
328 *
329 *	Pseudo-device attach routine.
330 */
331void
332bridgeattach(int n)
333{
334
335	pool_init(&bridge_rtnode_pool, sizeof(struct bridge_rtnode),
336	    0, 0, 0, "brtpl", NULL);
337
338	LIST_INIT(&bridge_list);
339	if_clone_attach(&bridge_cloner);
340}
341
342/*
343 * bridge_clone_create:
344 *
345 *	Create a new bridge instance.
346 */
347int
348bridge_clone_create(struct if_clone *ifc, int unit)
349{
350	struct bridge_softc *sc;
351	struct ifnet *ifp;
352	int s;
353
354	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
355	memset(sc, 0, sizeof(*sc));
356	ifp = &sc->sc_if;
357
358	sc->sc_brtmax = BRIDGE_RTABLE_MAX;
359	sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
360	sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
361	sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
362	sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
363	sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
364	sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
365	sc->sc_filter_flags = 0;
366
367	/* Initialize our routing table. */
368	bridge_rtable_init(sc);
369
370	callout_init(&sc->sc_brcallout);
371	callout_init(&sc->sc_bstpcallout);
372
373	LIST_INIT(&sc->sc_iflist);
374
375	sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
376	ifp->if_softc = sc;
377	ifp->if_mtu = ETHERMTU;
378	ifp->if_ioctl = bridge_ioctl;
379	ifp->if_output = bridge_output;
380	ifp->if_start = bridge_start;
381	ifp->if_stop = bridge_stop;
382	ifp->if_init = bridge_init;
383	ifp->if_type = IFT_BRIDGE;
384	ifp->if_addrlen = 0;
385	ifp->if_dlt = DLT_EN10MB;
386	ifp->if_hdrlen = ETHER_HDR_LEN;
387
388	if_attach(ifp);
389
390	if_alloc_sadl(ifp);
391
392	s = splnet();
393	LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
394	splx(s);
395
396	return (0);
397}
398
399/*
400 * bridge_clone_destroy:
401 *
402 *	Destroy a bridge instance.
403 */
404void
405bridge_clone_destroy(struct ifnet *ifp)
406{
407	struct bridge_softc *sc = ifp->if_softc;
408	struct bridge_iflist *bif;
409	int s;
410
411	s = splnet();
412
413	bridge_stop(ifp, 1);
414
415	while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
416		bridge_delete_member(sc, bif);
417
418	LIST_REMOVE(sc, sc_list);
419
420	splx(s);
421
422	if_detach(ifp);
423
424	/* Tear down the routing table. */
425	bridge_rtable_fini(sc);
426
427	free(sc, M_DEVBUF);
428}
429
430/*
431 * bridge_ioctl:
432 *
433 *	Handle a control request from the operator.
434 */
435int
436bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
437{
438	struct bridge_softc *sc = ifp->if_softc;
439	struct proc *p = curproc;	/* XXX */
440	union {
441		struct ifbreq ifbreq;
442		struct ifbifconf ifbifconf;
443		struct ifbareq ifbareq;
444		struct ifbaconf ifbaconf;
445		struct ifbrparam ifbrparam;
446	} args;
447	struct ifdrv *ifd = (struct ifdrv *) data;
448	const struct bridge_control *bc;
449	int s, error = 0;
450
451	s = splnet();
452
453	switch (cmd) {
454	case SIOCGDRVSPEC:
455	case SIOCSDRVSPEC:
456		if (ifd->ifd_cmd >= bridge_control_table_size) {
457			error = EINVAL;
458			break;
459		}
460		bc = &bridge_control_table[ifd->ifd_cmd];
461
462		if (cmd == SIOCGDRVSPEC &&
463		    (bc->bc_flags & BC_F_COPYOUT) == 0) {
464			error = EINVAL;
465			break;
466		}
467		else if (cmd == SIOCSDRVSPEC &&
468		    (bc->bc_flags & BC_F_COPYOUT) != 0) {
469			error = EINVAL;
470			break;
471		}
472
473		if (bc->bc_flags & BC_F_SUSER) {
474			error = suser(p->p_ucred, &p->p_acflag);
475			if (error)
476				break;
477		}
478
479		if (ifd->ifd_len != bc->bc_argsize ||
480		    ifd->ifd_len > sizeof(args)) {
481			error = EINVAL;
482			break;
483		}
484
485		if (bc->bc_flags & BC_F_COPYIN) {
486			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
487			if (error)
488				break;
489		}
490
491		error = (*bc->bc_func)(sc, &args);
492		if (error)
493			break;
494
495		if (bc->bc_flags & BC_F_COPYOUT)
496			error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
497
498		break;
499
500	case SIOCSIFFLAGS:
501		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
502			/*
503			 * If interface is marked down and it is running,
504			 * then stop and disable it.
505			 */
506			(*ifp->if_stop)(ifp, 1);
507		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
508			/*
509			 * If interface is marked up and it is stopped, then
510			 * start it.
511			 */
512			error = (*ifp->if_init)(ifp);
513		}
514		break;
515
516	default:
517		error = ENOTTY;
518		break;
519	}
520
521	splx(s);
522
523	return (error);
524}
525
526/*
527 * bridge_lookup_member:
528 *
529 *	Lookup a bridge member interface.  Must be called at splnet().
530 */
531struct bridge_iflist *
532bridge_lookup_member(struct bridge_softc *sc, const char *name)
533{
534	struct bridge_iflist *bif;
535	struct ifnet *ifp;
536
537	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
538		ifp = bif->bif_ifp;
539		if (strcmp(ifp->if_xname, name) == 0)
540			return (bif);
541	}
542
543	return (NULL);
544}
545
546/*
547 * bridge_lookup_member_if:
548 *
549 *	Lookup a bridge member interface by ifnet*.  Must be called at splnet().
550 */
551struct bridge_iflist *
552bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
553{
554	struct bridge_iflist *bif;
555
556	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
557		if (bif->bif_ifp == member_ifp)
558			return (bif);
559	}
560
561	return (NULL);
562}
563
564/*
565 * bridge_delete_member:
566 *
567 *	Delete the specified member interface.
568 */
569void
570bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
571{
572	struct ifnet *ifs = bif->bif_ifp;
573
574	switch (ifs->if_type) {
575	case IFT_ETHER:
576		/*
577		 * Take the interface out of promiscuous mode.
578		 */
579		(void) ifpromisc(ifs, 0);
580		break;
581
582	default:
583#ifdef DIAGNOSTIC
584		panic("bridge_delete_member: impossible");
585#endif
586		break;
587	}
588
589	ifs->if_bridge = NULL;
590	LIST_REMOVE(bif, bif_next);
591
592	bridge_rtdelete(sc, ifs);
593
594	free(bif, M_DEVBUF);
595
596	if (sc->sc_if.if_flags & IFF_RUNNING)
597		bstp_initialization(sc);
598}
599
600int
601bridge_ioctl_add(struct bridge_softc *sc, void *arg)
602{
603	struct ifbreq *req = arg;
604	struct bridge_iflist *bif = NULL;
605	struct ifnet *ifs;
606	int error = 0;
607
608	ifs = ifunit(req->ifbr_ifsname);
609	if (ifs == NULL)
610		return (ENOENT);
611
612	if (sc->sc_if.if_mtu != ifs->if_mtu)
613		return (EINVAL);
614
615	if (ifs->if_bridge == sc)
616		return (EEXIST);
617
618	if (ifs->if_bridge != NULL)
619		return (EBUSY);
620
621	bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT);
622	if (bif == NULL)
623		return (ENOMEM);
624
625	switch (ifs->if_type) {
626	case IFT_ETHER:
627		/*
628		 * Place the interface into promiscuous mode.
629		 */
630		error = ifpromisc(ifs, 1);
631		if (error)
632			goto out;
633		break;
634
635	default:
636		error = EINVAL;
637		goto out;
638	}
639
640	bif->bif_ifp = ifs;
641	bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
642	bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
643	bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
644
645	ifs->if_bridge = sc;
646	LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
647
648	if (sc->sc_if.if_flags & IFF_RUNNING)
649		bstp_initialization(sc);
650	else
651		bstp_stop(sc);
652
653 out:
654	if (error) {
655		if (bif != NULL)
656			free(bif, M_DEVBUF);
657	}
658	return (error);
659}
660
661int
662bridge_ioctl_del(struct bridge_softc *sc, void *arg)
663{
664	struct ifbreq *req = arg;
665	struct bridge_iflist *bif;
666
667	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
668	if (bif == NULL)
669		return (ENOENT);
670
671	bridge_delete_member(sc, bif);
672
673	return (0);
674}
675
676int
677bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
678{
679	struct ifbreq *req = arg;
680	struct bridge_iflist *bif;
681
682	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
683	if (bif == NULL)
684		return (ENOENT);
685
686	req->ifbr_ifsflags = bif->bif_flags;
687	req->ifbr_state = bif->bif_state;
688	req->ifbr_priority = bif->bif_priority;
689	req->ifbr_path_cost = bif->bif_path_cost;
690	req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
691
692	return (0);
693}
694
695int
696bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
697{
698	struct ifbreq *req = arg;
699	struct bridge_iflist *bif;
700
701	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
702	if (bif == NULL)
703		return (ENOENT);
704
705	if (req->ifbr_ifsflags & IFBIF_STP) {
706		switch (bif->bif_ifp->if_type) {
707		case IFT_ETHER:
708			/* These can do spanning tree. */
709			break;
710
711		default:
712			/* Nothing else can. */
713			return (EINVAL);
714		}
715	}
716
717	bif->bif_flags = req->ifbr_ifsflags;
718
719	if (sc->sc_if.if_flags & IFF_RUNNING)
720		bstp_initialization(sc);
721
722	return (0);
723}
724
725int
726bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
727{
728	struct ifbrparam *param = arg;
729
730	sc->sc_brtmax = param->ifbrp_csize;
731	bridge_rttrim(sc);
732
733	return (0);
734}
735
736int
737bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
738{
739	struct ifbrparam *param = arg;
740
741	param->ifbrp_csize = sc->sc_brtmax;
742
743	return (0);
744}
745
746int
747bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
748{
749	struct ifbifconf *bifc = arg;
750	struct bridge_iflist *bif;
751	struct ifbreq breq;
752	int count, len, error = 0;
753
754	count = 0;
755	LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
756		count++;
757
758	if (bifc->ifbic_len == 0) {
759		bifc->ifbic_len = sizeof(breq) * count;
760		return (0);
761	}
762
763	count = 0;
764	len = bifc->ifbic_len;
765	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
766		if (len < sizeof(breq))
767			break;
768
769		strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
770		    sizeof(breq.ifbr_ifsname));
771		breq.ifbr_ifsflags = bif->bif_flags;
772		breq.ifbr_state = bif->bif_state;
773		breq.ifbr_priority = bif->bif_priority;
774		breq.ifbr_path_cost = bif->bif_path_cost;
775		breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
776		error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
777		if (error)
778			break;
779		count++;
780		len -= sizeof(breq);
781	}
782
783	bifc->ifbic_len = sizeof(breq) * count;
784	return (error);
785}
786
787int
788bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
789{
790	struct ifbaconf *bac = arg;
791	struct bridge_rtnode *brt;
792	struct ifbareq bareq;
793	int count = 0, error = 0, len;
794
795	if (bac->ifbac_len == 0)
796		return (0);
797
798	len = bac->ifbac_len;
799	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
800		if (len < sizeof(bareq))
801			goto out;
802		strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
803		    sizeof(bareq.ifba_ifsname));
804		memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
805		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
806			bareq.ifba_expire = brt->brt_expire - mono_time.tv_sec;
807		else
808			bareq.ifba_expire = 0;
809		bareq.ifba_flags = brt->brt_flags;
810
811		error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
812		if (error)
813			goto out;
814		count++;
815		len -= sizeof(bareq);
816	}
817 out:
818	bac->ifbac_len = sizeof(bareq) * count;
819	return (error);
820}
821
822int
823bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
824{
825	struct ifbareq *req = arg;
826	struct bridge_iflist *bif;
827	int error;
828
829	bif = bridge_lookup_member(sc, req->ifba_ifsname);
830	if (bif == NULL)
831		return (ENOENT);
832
833	error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
834	    req->ifba_flags);
835
836	return (error);
837}
838
839int
840bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
841{
842	struct ifbrparam *param = arg;
843
844	sc->sc_brttimeout = param->ifbrp_ctime;
845
846	return (0);
847}
848
849int
850bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
851{
852	struct ifbrparam *param = arg;
853
854	param->ifbrp_ctime = sc->sc_brttimeout;
855
856	return (0);
857}
858
859int
860bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
861{
862	struct ifbareq *req = arg;
863
864	return (bridge_rtdaddr(sc, req->ifba_dst));
865}
866
867int
868bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
869{
870	struct ifbreq *req = arg;
871
872	bridge_rtflush(sc, req->ifbr_ifsflags);
873
874	return (0);
875}
876
877int
878bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
879{
880	struct ifbrparam *param = arg;
881
882	param->ifbrp_prio = sc->sc_bridge_priority;
883
884	return (0);
885}
886
887int
888bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
889{
890	struct ifbrparam *param = arg;
891
892	sc->sc_bridge_priority = param->ifbrp_prio;
893
894	if (sc->sc_if.if_flags & IFF_RUNNING)
895		bstp_initialization(sc);
896
897	return (0);
898}
899
900int
901bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
902{
903	struct ifbrparam *param = arg;
904
905	param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
906
907	return (0);
908}
909
910int
911bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
912{
913	struct ifbrparam *param = arg;
914
915	if (param->ifbrp_hellotime == 0)
916		return (EINVAL);
917	sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
918
919	if (sc->sc_if.if_flags & IFF_RUNNING)
920		bstp_initialization(sc);
921
922	return (0);
923}
924
925int
926bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
927{
928	struct ifbrparam *param = arg;
929
930	param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
931
932	return (0);
933}
934
935int
936bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
937{
938	struct ifbrparam *param = arg;
939
940	if (param->ifbrp_fwddelay == 0)
941		return (EINVAL);
942	sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
943
944	if (sc->sc_if.if_flags & IFF_RUNNING)
945		bstp_initialization(sc);
946
947	return (0);
948}
949
950int
951bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
952{
953	struct ifbrparam *param = arg;
954
955	param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
956
957	return (0);
958}
959
960int
961bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
962{
963	struct ifbrparam *param = arg;
964
965	if (param->ifbrp_maxage == 0)
966		return (EINVAL);
967	sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
968
969	if (sc->sc_if.if_flags & IFF_RUNNING)
970		bstp_initialization(sc);
971
972	return (0);
973}
974
975int
976bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
977{
978	struct ifbreq *req = arg;
979	struct bridge_iflist *bif;
980
981	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
982	if (bif == NULL)
983		return (ENOENT);
984
985	bif->bif_priority = req->ifbr_priority;
986
987	if (sc->sc_if.if_flags & IFF_RUNNING)
988		bstp_initialization(sc);
989
990	return (0);
991}
992
993#ifdef BRIDGE_IPF
994int
995bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
996{
997	struct ifbrparam *param = arg;
998
999	param->ifbrp_filter = sc->sc_filter_flags;
1000
1001	return (0);
1002}
1003
1004int
1005bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
1006{
1007	struct ifbrparam *param = arg;
1008	uint32_t nflags, oflags;
1009
1010	if (param->ifbrp_filter & ~IFBF_FILT_MASK)
1011		return (EINVAL);
1012
1013	nflags = param->ifbrp_filter;
1014	oflags = sc->sc_filter_flags;
1015
1016	if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) {
1017		pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1018			&sc->sc_if.if_pfil);
1019	}
1020	if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) {
1021		pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1022			&sc->sc_if.if_pfil);
1023	}
1024
1025	sc->sc_filter_flags = nflags;
1026
1027	return (0);
1028}
1029#endif /* BRIDGE_IPF */
1030
1031int
1032bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1033{
1034	struct ifbreq *req = arg;
1035	struct bridge_iflist *bif;
1036
1037	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1038	if (bif == NULL)
1039		return (ENOENT);
1040
1041	bif->bif_path_cost = req->ifbr_path_cost;
1042
1043	if (sc->sc_if.if_flags & IFF_RUNNING)
1044		bstp_initialization(sc);
1045
1046	return (0);
1047}
1048
1049/*
1050 * bridge_ifdetach:
1051 *
1052 *	Detach an interface from a bridge.  Called when a member
1053 *	interface is detaching.
1054 */
1055void
1056bridge_ifdetach(struct ifnet *ifp)
1057{
1058	struct bridge_softc *sc = ifp->if_bridge;
1059	struct ifbreq breq;
1060
1061	memset(&breq, 0, sizeof(breq));
1062	sprintf(breq.ifbr_ifsname, ifp->if_xname);
1063
1064	(void) bridge_ioctl_del(sc, &breq);
1065}
1066
1067/*
1068 * bridge_init:
1069 *
1070 *	Initialize a bridge interface.
1071 */
1072int
1073bridge_init(struct ifnet *ifp)
1074{
1075	struct bridge_softc *sc = ifp->if_softc;
1076
1077	if (ifp->if_flags & IFF_RUNNING)
1078		return (0);
1079
1080	callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1081	    bridge_timer, sc);
1082
1083	ifp->if_flags |= IFF_RUNNING;
1084	bstp_initialization(sc);
1085	return (0);
1086}
1087
1088/*
1089 * bridge_stop:
1090 *
1091 *	Stop the bridge interface.
1092 */
1093void
1094bridge_stop(struct ifnet *ifp, int disable)
1095{
1096	struct bridge_softc *sc = ifp->if_softc;
1097
1098	if ((ifp->if_flags & IFF_RUNNING) == 0)
1099		return;
1100
1101	callout_stop(&sc->sc_brcallout);
1102	bstp_stop(sc);
1103
1104	IF_PURGE(&ifp->if_snd);
1105
1106	bridge_rtflush(sc, IFBF_FLUSHDYN);
1107
1108	ifp->if_flags &= ~IFF_RUNNING;
1109}
1110
1111/*
1112 * bridge_enqueue:
1113 *
1114 *	Enqueue a packet on a bridge member interface.
1115 *
1116 *	NOTE: must be called at splnet().
1117 */
1118__inline void
1119bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1120{
1121	ALTQ_DECL(struct altq_pktattr pktattr;)
1122	int len, error;
1123	short mflags;
1124
1125#ifdef PFIL_HOOKS
1126	if (pfil_run_hooks(&sc->sc_if.if_pfil, &m, dst_ifp, PFIL_OUT) != 0) {
1127		m_freem(m);
1128		return;
1129	}
1130	if (m == NULL)
1131		return;
1132#endif /* PFIL_HOOKS */
1133
1134#ifdef ALTQ
1135	/*
1136	 * If ALTQ is enabled on the member interface, do
1137	 * classification; the queueing discipline might
1138	 * not require classification, but might require
1139	 * the address family/header pointer in the pktattr.
1140	 */
1141	if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
1142		/* XXX IFT_ETHER */
1143		altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
1144	}
1145#endif /* ALTQ */
1146
1147	len = m->m_pkthdr.len;
1148	mflags = m->m_flags;
1149	IFQ_ENQUEUE(&dst_ifp->if_snd, m, &pktattr, error);
1150	if (error) {
1151		/* mbuf is already freed */
1152		sc->sc_if.if_oerrors++;
1153		return;
1154	}
1155
1156	sc->sc_if.if_opackets++;
1157	sc->sc_if.if_obytes += len;
1158
1159	dst_ifp->if_obytes += len;
1160
1161	if (mflags & M_MCAST) {
1162		sc->sc_if.if_omcasts++;
1163		dst_ifp->if_omcasts++;
1164	}
1165
1166	if ((dst_ifp->if_flags & IFF_OACTIVE) == 0)
1167		(*dst_ifp->if_start)(dst_ifp);
1168}
1169
1170/*
1171 * bridge_output:
1172 *
1173 *	Send output from a bridge member interface.  This
1174 *	performs the bridging function for locally originated
1175 *	packets.
1176 *
1177 *	The mbuf has the Ethernet header already attached.  We must
1178 *	enqueue or free the mbuf before returning.
1179 */
1180int
1181bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1182    struct rtentry *rt)
1183{
1184	struct ether_header *eh;
1185	struct ifnet *dst_if;
1186	struct bridge_softc *sc;
1187	int s;
1188
1189	if (m->m_len < ETHER_HDR_LEN) {
1190		m = m_pullup(m, ETHER_HDR_LEN);
1191		if (m == NULL)
1192			return (0);
1193	}
1194
1195	eh = mtod(m, struct ether_header *);
1196	sc = ifp->if_bridge;
1197
1198	s = splnet();
1199
1200	/*
1201	 * If bridge is down, but the original output interface is up,
1202	 * go ahead and send out that interface.  Otherwise, the packet
1203	 * is dropped below.
1204	 */
1205	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1206		dst_if = ifp;
1207		goto sendunicast;
1208	}
1209
1210	/*
1211	 * If the packet is a multicast, or we don't know a better way to
1212	 * get there, send to all interfaces.
1213	 */
1214	if (ETHER_IS_MULTICAST(eh->ether_dhost))
1215		dst_if = NULL;
1216	else
1217		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1218	if (dst_if == NULL) {
1219		struct bridge_iflist *bif;
1220		struct mbuf *mc;
1221		int used = 0;
1222
1223		LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1224			dst_if = bif->bif_ifp;
1225			if ((dst_if->if_flags & IFF_RUNNING) == 0)
1226				continue;
1227
1228			/*
1229			 * If this is not the original output interface,
1230			 * and the interface is participating in spanning
1231			 * tree, make sure the port is in a state that
1232			 * allows forwarding.
1233			 */
1234			if (dst_if != ifp &&
1235			    (bif->bif_flags & IFBIF_STP) != 0) {
1236				switch (bif->bif_state) {
1237				case BSTP_IFSTATE_BLOCKING:
1238				case BSTP_IFSTATE_LISTENING:
1239				case BSTP_IFSTATE_DISABLED:
1240					continue;
1241				}
1242			}
1243
1244			if (LIST_NEXT(bif, bif_next) == NULL) {
1245				used = 1;
1246				mc = m;
1247			} else {
1248				mc = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1249				if (mc == NULL) {
1250					sc->sc_if.if_oerrors++;
1251					continue;
1252				}
1253			}
1254
1255			bridge_enqueue(sc, dst_if, mc);
1256		}
1257		if (used == 0)
1258			m_freem(m);
1259		splx(s);
1260		return (0);
1261	}
1262
1263 sendunicast:
1264	/*
1265	 * XXX Spanning tree consideration here?
1266	 */
1267
1268	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1269		m_freem(m);
1270		splx(s);
1271		return (0);
1272	}
1273
1274	bridge_enqueue(sc, dst_if, m);
1275
1276	splx(s);
1277	return (0);
1278}
1279
1280/*
1281 * bridge_start:
1282 *
1283 *	Start output on a bridge.
1284 *
1285 *	NOTE: This routine should never be called in this implementation.
1286 */
1287void
1288bridge_start(struct ifnet *ifp)
1289{
1290
1291	printf("%s: bridge_start() called\n", ifp->if_xname);
1292}
1293
1294/*
1295 * bridge_forward:
1296 *
1297 *	The fowarding function of the bridge.
1298 */
1299void
1300bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1301{
1302	struct bridge_iflist *bif;
1303	struct ifnet *src_if, *dst_if;
1304	struct ether_header *eh;
1305
1306	src_if = m->m_pkthdr.rcvif;
1307
1308	sc->sc_if.if_ipackets++;
1309	sc->sc_if.if_ibytes += m->m_pkthdr.len;
1310
1311	/*
1312	 * Look up the bridge_iflist.
1313	 */
1314	bif = bridge_lookup_member_if(sc, src_if);
1315	if (bif == NULL) {
1316		/* Interface is not a bridge member (anymore?) */
1317		m_freem(m);
1318		return;
1319	}
1320
1321	if (bif->bif_flags & IFBIF_STP) {
1322		switch (bif->bif_state) {
1323		case BSTP_IFSTATE_BLOCKING:
1324		case BSTP_IFSTATE_LISTENING:
1325		case BSTP_IFSTATE_DISABLED:
1326			m_freem(m);
1327			return;
1328		}
1329	}
1330
1331	eh = mtod(m, struct ether_header *);
1332
1333	/*
1334	 * If the interface is learning, and the source
1335	 * address is valid and not multicast, record
1336	 * the address.
1337	 */
1338	if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1339	    ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1340	    (eh->ether_shost[0] == 0 &&
1341	     eh->ether_shost[1] == 0 &&
1342	     eh->ether_shost[2] == 0 &&
1343	     eh->ether_shost[3] == 0 &&
1344	     eh->ether_shost[4] == 0 &&
1345	     eh->ether_shost[5] == 0) == 0) {
1346		(void) bridge_rtupdate(sc, eh->ether_shost,
1347		    src_if, 0, IFBAF_DYNAMIC);
1348	}
1349
1350	if ((bif->bif_flags & IFBIF_STP) != 0 &&
1351	    bif->bif_state == BSTP_IFSTATE_LEARNING) {
1352		m_freem(m);
1353		return;
1354	}
1355
1356	/*
1357	 * At this point, the port either doesn't participate
1358	 * in spanning tree or it is in the forwarding state.
1359	 */
1360
1361	/*
1362	 * If the packet is unicast, destined for someone on
1363	 * "this" side of the bridge, drop it.
1364	 */
1365	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1366		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1367		if (src_if == dst_if) {
1368			m_freem(m);
1369			return;
1370		}
1371	} else {
1372		/* ...forward it to all interfaces. */
1373		sc->sc_if.if_imcasts++;
1374		dst_if = NULL;
1375	}
1376
1377#ifdef PFIL_HOOKS
1378	if (pfil_run_hooks(&sc->sc_if.if_pfil, &m, m->m_pkthdr.rcvif, PFIL_IN) != 0) {
1379		m_freem(m);
1380		return;
1381	}
1382	if (m == NULL)
1383		return;
1384#endif /* PFIL_HOOKS */
1385
1386	if (dst_if == NULL) {
1387		bridge_broadcast(sc, src_if, m);
1388		return;
1389	}
1390
1391	/*
1392	 * At this point, we're dealing with a unicast frame
1393	 * going to a different interface.
1394	 */
1395	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1396		m_freem(m);
1397		return;
1398	}
1399	bif = bridge_lookup_member_if(sc, dst_if);
1400	if (bif == NULL) {
1401		/* Not a member of the bridge (anymore?) */
1402		m_freem(m);
1403		return;
1404	}
1405
1406	if (bif->bif_flags & IFBIF_STP) {
1407		switch (bif->bif_state) {
1408		case BSTP_IFSTATE_DISABLED:
1409		case BSTP_IFSTATE_BLOCKING:
1410			m_freem(m);
1411			return;
1412		}
1413	}
1414
1415	bridge_enqueue(sc, dst_if, m);
1416}
1417
1418/*
1419 * bridge_input:
1420 *
1421 *	Receive input from a member interface.  Queue the packet for
1422 *	bridging if it is not for us.
1423 */
1424struct mbuf *
1425bridge_input(struct ifnet *ifp, struct mbuf *m)
1426{
1427	struct bridge_softc *sc = ifp->if_bridge;
1428	struct bridge_iflist *bif;
1429	struct ether_header *eh;
1430	struct mbuf *mc;
1431
1432	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
1433		return (m);
1434
1435	bif = bridge_lookup_member_if(sc, ifp);
1436	if (bif == NULL)
1437		return (m);
1438
1439	eh = mtod(m, struct ether_header *);
1440
1441	if (m->m_flags & (M_BCAST|M_MCAST)) {
1442		/* Tap off 802.1D packets; they do not get forwarded. */
1443		if (memcmp(eh->ether_dhost, bstp_etheraddr,
1444		    ETHER_ADDR_LEN) == 0) {
1445			m = bstp_input(ifp, m);
1446			if (m == NULL)
1447				return (NULL);
1448		}
1449
1450		if (bif->bif_flags & IFBIF_STP) {
1451			switch (bif->bif_state) {
1452			case BSTP_IFSTATE_BLOCKING:
1453			case BSTP_IFSTATE_LISTENING:
1454			case BSTP_IFSTATE_DISABLED:
1455				return (m);
1456			}
1457		}
1458
1459		/*
1460		 * Make a deep copy of the packet and enqueue the copy
1461		 * for bridge processing; return the original packet for
1462		 * local processing.
1463		 */
1464		mc = m_dup(m, 0, M_COPYALL, M_NOWAIT);
1465		if (mc == NULL)
1466			return (m);
1467
1468		/* Perform the bridge forwarding function with the copy. */
1469		bridge_forward(sc, mc);
1470
1471		/* Return the original packet for local processing. */
1472		return (m);
1473	}
1474
1475	if (bif->bif_flags & IFBIF_STP) {
1476		switch (bif->bif_state) {
1477		case BSTP_IFSTATE_BLOCKING:
1478		case BSTP_IFSTATE_LISTENING:
1479		case BSTP_IFSTATE_DISABLED:
1480			return (m);
1481		}
1482	}
1483
1484	/*
1485	 * Unicast.  Make sure it's not for us.
1486	 */
1487	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1488		/* It is destined for us. */
1489		if (memcmp(LLADDR(bif->bif_ifp->if_sadl), eh->ether_dhost,
1490		    ETHER_ADDR_LEN) == 0) {
1491			if (bif->bif_flags & IFBIF_LEARNING)
1492				(void) bridge_rtupdate(sc,
1493				    eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1494			m->m_pkthdr.rcvif = bif->bif_ifp;
1495			return (m);
1496		}
1497
1498		/* We just received a packet that we sent out. */
1499		if (memcmp(LLADDR(bif->bif_ifp->if_sadl), eh->ether_shost,
1500		    ETHER_ADDR_LEN) == 0) {
1501			m_freem(m);
1502			return (NULL);
1503		}
1504	}
1505
1506	/* Perform the bridge forwarding function. */
1507	bridge_forward(sc, m);
1508
1509	return (NULL);
1510}
1511
1512/*
1513 * bridge_broadcast:
1514 *
1515 *	Send a frame to all interfaces that are members of
1516 *	the bridge, except for the one on which the packet
1517 *	arrived.
1518 */
1519void
1520bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1521    struct mbuf *m)
1522{
1523	struct bridge_iflist *bif;
1524	struct mbuf *mc;
1525	struct ifnet *dst_if;
1526	int used = 0;
1527
1528	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1529		dst_if = bif->bif_ifp;
1530		if (dst_if == src_if)
1531			continue;
1532
1533		if (bif->bif_flags & IFBIF_STP) {
1534			switch (bif->bif_state) {
1535			case BSTP_IFSTATE_BLOCKING:
1536			case BSTP_IFSTATE_DISABLED:
1537				continue;
1538			}
1539		}
1540
1541		if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1542		    (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1543			continue;
1544
1545		if ((dst_if->if_flags & IFF_RUNNING) == 0)
1546			continue;
1547
1548		if (LIST_NEXT(bif, bif_next) == NULL) {
1549			mc = m;
1550			used = 1;
1551		} else {
1552			mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
1553			if (mc == NULL) {
1554				sc->sc_if.if_oerrors++;
1555				continue;
1556			}
1557		}
1558
1559		bridge_enqueue(sc, dst_if, mc);
1560	}
1561	if (used == 0)
1562		m_freem(m);
1563}
1564
1565/*
1566 * bridge_rtupdate:
1567 *
1568 *	Add a bridge routing entry.
1569 */
1570int
1571bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
1572    struct ifnet *dst_if, int setflags, uint8_t flags)
1573{
1574	struct bridge_rtnode *brt;
1575	int error;
1576
1577	/*
1578	 * A route for this destination might already exist.  If so,
1579	 * update it, otherwise create a new one.
1580	 */
1581	if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
1582		if (sc->sc_brtcnt >= sc->sc_brtmax)
1583			return (ENOSPC);
1584
1585		/*
1586		 * Allocate a new bridge forwarding node, and
1587		 * initialize the expiration time and Ethernet
1588		 * address.
1589		 */
1590		brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
1591		if (brt == NULL)
1592			return (ENOMEM);
1593
1594		memset(brt, 0, sizeof(*brt));
1595		brt->brt_expire = mono_time.tv_sec + sc->sc_brttimeout;
1596		brt->brt_flags = IFBAF_DYNAMIC;
1597		memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
1598
1599		if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
1600			pool_put(&bridge_rtnode_pool, brt);
1601			return (error);
1602		}
1603	}
1604
1605	brt->brt_ifp = dst_if;
1606	if (setflags) {
1607		brt->brt_flags = flags;
1608		brt->brt_expire = (flags & IFBAF_STATIC) ? 0 :
1609		    mono_time.tv_sec + sc->sc_brttimeout;
1610	}
1611
1612	return (0);
1613}
1614
1615/*
1616 * bridge_rtlookup:
1617 *
1618 *	Lookup the destination interface for an address.
1619 */
1620struct ifnet *
1621bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
1622{
1623	struct bridge_rtnode *brt;
1624
1625	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
1626		return (NULL);
1627
1628	return (brt->brt_ifp);
1629}
1630
1631/*
1632 * bridge_rttrim:
1633 *
1634 *	Trim the routine table so that we have a number
1635 *	of routing entries less than or equal to the
1636 *	maximum number.
1637 */
1638void
1639bridge_rttrim(struct bridge_softc *sc)
1640{
1641	struct bridge_rtnode *brt, *nbrt;
1642
1643	/* Make sure we actually need to do this. */
1644	if (sc->sc_brtcnt <= sc->sc_brtmax)
1645		return;
1646
1647	/* Force an aging cycle; this might trim enough addresses. */
1648	bridge_rtage(sc);
1649	if (sc->sc_brtcnt <= sc->sc_brtmax)
1650		return;
1651
1652	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1653		nbrt = LIST_NEXT(brt, brt_list);
1654		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1655			bridge_rtnode_destroy(sc, brt);
1656			if (sc->sc_brtcnt <= sc->sc_brtmax)
1657				return;
1658		}
1659	}
1660}
1661
1662/*
1663 * bridge_timer:
1664 *
1665 *	Aging timer for the bridge.
1666 */
1667void
1668bridge_timer(void *arg)
1669{
1670	struct bridge_softc *sc = arg;
1671	int s;
1672
1673	s = splnet();
1674	bridge_rtage(sc);
1675	splx(s);
1676
1677	if (sc->sc_if.if_flags & IFF_RUNNING)
1678		callout_reset(&sc->sc_brcallout,
1679		    bridge_rtable_prune_period * hz, bridge_timer, sc);
1680}
1681
1682/*
1683 * bridge_rtage:
1684 *
1685 *	Perform an aging cycle.
1686 */
1687void
1688bridge_rtage(struct bridge_softc *sc)
1689{
1690	struct bridge_rtnode *brt, *nbrt;
1691
1692	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1693		nbrt = LIST_NEXT(brt, brt_list);
1694		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1695			if (mono_time.tv_sec >= brt->brt_expire)
1696				bridge_rtnode_destroy(sc, brt);
1697		}
1698	}
1699}
1700
1701/*
1702 * bridge_rtflush:
1703 *
1704 *	Remove all dynamic addresses from the bridge.
1705 */
1706void
1707bridge_rtflush(struct bridge_softc *sc, int full)
1708{
1709	struct bridge_rtnode *brt, *nbrt;
1710
1711	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1712		nbrt = LIST_NEXT(brt, brt_list);
1713		if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
1714			bridge_rtnode_destroy(sc, brt);
1715	}
1716}
1717
1718/*
1719 * bridge_rtdaddr:
1720 *
1721 *	Remove an address from the table.
1722 */
1723int
1724bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
1725{
1726	struct bridge_rtnode *brt;
1727
1728	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
1729		return (ENOENT);
1730
1731	bridge_rtnode_destroy(sc, brt);
1732	return (0);
1733}
1734
1735/*
1736 * bridge_rtdelete:
1737 *
1738 *	Delete routes to a speicifc member interface.
1739 */
1740void
1741bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
1742{
1743	struct bridge_rtnode *brt, *nbrt;
1744
1745	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1746		nbrt = LIST_NEXT(brt, brt_list);
1747		if (brt->brt_ifp == ifp)
1748			bridge_rtnode_destroy(sc, brt);
1749	}
1750}
1751
1752/*
1753 * bridge_rtable_init:
1754 *
1755 *	Initialize the route table for this bridge.
1756 */
1757int
1758bridge_rtable_init(struct bridge_softc *sc)
1759{
1760	int i;
1761
1762	sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
1763	    M_DEVBUF, M_NOWAIT);
1764	if (sc->sc_rthash == NULL)
1765		return (ENOMEM);
1766
1767	for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
1768		LIST_INIT(&sc->sc_rthash[i]);
1769
1770	sc->sc_rthash_key = arc4random();
1771
1772	LIST_INIT(&sc->sc_rtlist);
1773
1774	return (0);
1775}
1776
1777/*
1778 * bridge_rtable_fini:
1779 *
1780 *	Deconstruct the route table for this bridge.
1781 */
1782void
1783bridge_rtable_fini(struct bridge_softc *sc)
1784{
1785
1786	free(sc->sc_rthash, M_DEVBUF);
1787}
1788
1789/*
1790 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1791 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1792 */
1793#define	mix(a, b, c)							\
1794do {									\
1795	a -= b; a -= c; a ^= (c >> 13);					\
1796	b -= c; b -= a; b ^= (a << 8);					\
1797	c -= a; c -= b; c ^= (b >> 13);					\
1798	a -= b; a -= c; a ^= (c >> 12);					\
1799	b -= c; b -= a; b ^= (a << 16);					\
1800	c -= a; c -= b; c ^= (b >> 5);					\
1801	a -= b; a -= c; a ^= (c >> 3);					\
1802	b -= c; b -= a; b ^= (a << 10);					\
1803	c -= a; c -= b; c ^= (b >> 15);					\
1804} while (/*CONSTCOND*/0)
1805
1806static __inline uint32_t
1807bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
1808{
1809	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
1810
1811	b += addr[5] << 8;
1812	b += addr[4];
1813	a += addr[3] << 24;
1814	a += addr[2] << 16;
1815	a += addr[1] << 8;
1816	a += addr[0];
1817
1818	mix(a, b, c);
1819
1820	return (c & BRIDGE_RTHASH_MASK);
1821}
1822
1823#undef mix
1824
1825/*
1826 * bridge_rtnode_lookup:
1827 *
1828 *	Look up a bridge route node for the specified destination.
1829 */
1830struct bridge_rtnode *
1831bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
1832{
1833	struct bridge_rtnode *brt;
1834	uint32_t hash;
1835	int dir;
1836
1837	hash = bridge_rthash(sc, addr);
1838	LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
1839		dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
1840		if (dir == 0)
1841			return (brt);
1842		if (dir > 0)
1843			return (NULL);
1844	}
1845
1846	return (NULL);
1847}
1848
1849/*
1850 * bridge_rtnode_insert:
1851 *
1852 *	Insert the specified bridge node into the route table.  We
1853 *	assume the entry is not already in the table.
1854 */
1855int
1856bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
1857{
1858	struct bridge_rtnode *lbrt;
1859	uint32_t hash;
1860	int dir;
1861
1862	hash = bridge_rthash(sc, brt->brt_addr);
1863
1864	lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
1865	if (lbrt == NULL) {
1866		LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
1867		goto out;
1868	}
1869
1870	do {
1871		dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
1872		if (dir == 0)
1873			return (EEXIST);
1874		if (dir > 0) {
1875			LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
1876			goto out;
1877		}
1878		if (LIST_NEXT(lbrt, brt_hash) == NULL) {
1879			LIST_INSERT_AFTER(lbrt, brt, brt_hash);
1880			goto out;
1881		}
1882		lbrt = LIST_NEXT(lbrt, brt_hash);
1883	} while (lbrt != NULL);
1884
1885#ifdef DIAGNOSTIC
1886	panic("bridge_rtnode_insert: impossible");
1887#endif
1888
1889 out:
1890	LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
1891	sc->sc_brtcnt++;
1892
1893	return (0);
1894}
1895
1896/*
1897 * bridge_rtnode_destroy:
1898 *
1899 *	Destroy a bridge rtnode.
1900 */
1901void
1902bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
1903{
1904
1905	LIST_REMOVE(brt, brt_hash);
1906
1907	LIST_REMOVE(brt, brt_list);
1908	sc->sc_brtcnt--;
1909	pool_put(&bridge_rtnode_pool, brt);
1910}
1911
1912#ifdef BRIDGE_IPF
1913extern struct pfil_head inet_pfil_hook;                 /* XXX */
1914extern struct pfil_head inet6_pfil_hook;                /* XXX */
1915
1916/*
1917 * Send bridge packets through IPF if they are one of the types IPF can deal
1918 * with, or if they are ARP or REVARP.  (IPF will pass ARP and REVARP without
1919 * question.)
1920 */
1921static int bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
1922{
1923	int snap, error;
1924	struct ether_header *eh;
1925	struct mbuf *m1, *m2;
1926	u_int16_t ether_type;
1927
1928	snap = 0;
1929	error = -1; /* Default error if not error == 0 */
1930	eh = mtod(*mp, struct ether_header *);
1931	ether_type = ntohs(eh->ether_type);
1932
1933	/*
1934	 * Check for SNAP/LLC.
1935	 */
1936        if (ether_type < ETHERMTU) {
1937                struct llc *llc = (struct llc *)(eh + 1);
1938
1939                if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
1940                    llc->llc_dsap == LLC_SNAP_LSAP &&
1941                    llc->llc_ssap == LLC_SNAP_LSAP &&
1942                    llc->llc_control == LLC_UI) {
1943                	ether_type = htons(llc->llc_un.type_snap.ether_type);
1944			snap = 1;
1945                }
1946        }
1947
1948	/*
1949	 * If we're trying to filter bridge traffic, don't look at anything
1950	 * other than IP and ARP traffic.  If the filter doesn't understand
1951	 * IPv6, don't allow IPv6 through the bridge either.  This is lame
1952	 * since if we really wanted, say, an AppleTalk filter, we are hosed,
1953	 * but of course we don't have an AppleTalk filter to begin with.
1954	 * (Note that since IPF doesn't understand ARP it will pass *ALL*
1955	 * ARP traffic.)
1956	 */
1957	switch (ether_type) {
1958		case ETHERTYPE_ARP:
1959		case ETHERTYPE_REVARP:
1960			return 0; /* Automatically pass */
1961		case ETHERTYPE_IP:
1962# ifdef INET6
1963		case ETHERTYPE_IPV6:
1964# endif /* INET6 */
1965			break;
1966		default:
1967			goto bad;
1968	}
1969
1970	/* Strip off the Ethernet header---but keep a copy. */
1971	if ((m1 = m_split(*mp, sizeof(struct ether_header), M_NOWAIT)) == NULL)
1972		goto bad;
1973	/* Strip off snap header, if present */
1974	if (snap) {
1975		if ((m2 = m_split(m1, sizeof(struct llc), M_NOWAIT)) == NULL)
1976			goto bad2;
1977	} else
1978		m2 = m1;
1979
1980	/*
1981	 * Check basic packet sanity, if the packet is outbound, and
1982	 * run IPF filter.
1983	 */
1984	if (ether_type == ETHERTYPE_IP &&
1985	    (dir == PFIL_OUT || bridge_ip_checkbasic(&m2) == 0)) {
1986		error = pfil_run_hooks(&inet_pfil_hook, &m2, ifp, dir);
1987		if (error) goto bad2;
1988	}
1989# ifdef INET6
1990	if (ether_type == ETHERTYPE_IPV6 &&
1991	    (dir == PFIL_OUT || bridge_ip6_checkbasic(&m2) == 0)) {
1992		error = pfil_run_hooks(&inet6_pfil_hook, &m2, ifp, dir);
1993		if (error) goto bad2;
1994	}
1995# endif
1996	if (m2 == NULL) goto bad2;
1997
1998	/*
1999	 * Finally, put everything back the way it was and return
2000	 */
2001	if (snap)
2002		m_cat(m1, m2);
2003	else
2004		m1 = m2;
2005	m_cat(*mp, m1);
2006	return 0;
2007
2008    bad2:
2009	if (snap)
2010		m_freem(m1);
2011	m_freem(m2);
2012    bad:
2013	m_freem(*mp);
2014	*mp = NULL;
2015	return error;
2016}
2017
2018/*
2019 * Perform basic checks on header size since
2020 * IPF assumes ip_input has already processed
2021 * it for it.  Cut-and-pasted from ip_input.c.
2022 * Given how simple the IPv6 version is,
2023 * does the IPv4 version really need to be
2024 * this complicated?
2025 *
2026 * XXX Should we update ipstat here, or not?
2027 * XXX Right now we update ipstat but not
2028 * XXX csum_counter.
2029 */
2030static int
2031bridge_ip_checkbasic(struct mbuf **mp)
2032{
2033	struct mbuf *m = *mp;
2034	struct ip *ip;
2035	int len, hlen;
2036
2037	if (*mp == NULL)
2038		return -1;
2039
2040	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2041		if ((m = m_copyup(m, sizeof(struct ip),
2042			(max_linkhdr + 3) & ~3)) == NULL) {
2043			/* XXXJRT new stat, please */
2044			ipstat.ips_toosmall++;
2045			goto bad;
2046		}
2047	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
2048		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2049			ipstat.ips_toosmall++;
2050			goto bad;
2051		}
2052	}
2053	ip = mtod(m, struct ip *);
2054	if (ip == NULL) goto bad;
2055
2056	if (ip->ip_v != IPVERSION) {
2057		ipstat.ips_badvers++;
2058		goto bad;
2059	}
2060	hlen = ip->ip_hl << 2;
2061	if (hlen < sizeof(struct ip)) { /* minimum header length */
2062		ipstat.ips_badhlen++;
2063		goto bad;
2064	}
2065	if (hlen > m->m_len) {
2066		if ((m = m_pullup(m, hlen)) == 0) {
2067			ipstat.ips_badhlen++;
2068			goto bad;
2069		}
2070		ip = mtod(m, struct ip *);
2071		if (ip == NULL) goto bad;
2072	}
2073
2074        switch (m->m_pkthdr.csum_flags &
2075                ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
2076                 M_CSUM_IPv4_BAD)) {
2077        case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
2078                /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
2079                goto bad;
2080
2081        case M_CSUM_IPv4:
2082                /* Checksum was okay. */
2083                /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
2084                break;
2085
2086        default:
2087                /* Must compute it ourselves. */
2088                /* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
2089                if (in_cksum(m, hlen) != 0)
2090                        goto bad;
2091                break;
2092        }
2093
2094        /* Retrieve the packet length. */
2095        len = ntohs(ip->ip_len);
2096
2097        /*
2098         * Check for additional length bogosity
2099         */
2100        if (len < hlen) {
2101                ipstat.ips_badlen++;
2102                goto bad;
2103        }
2104
2105        /*
2106         * Check that the amount of data in the buffers
2107         * is as at least much as the IP header would have us expect.
2108         * Drop packet if shorter than we expect.
2109         */
2110        if (m->m_pkthdr.len < len) {
2111                ipstat.ips_tooshort++;
2112                goto bad;
2113        }
2114
2115	/* Checks out, proceed */
2116	*mp = m;
2117	return 0;
2118
2119    bad:
2120	*mp = m;
2121	return -1;
2122}
2123
2124# ifdef INET6
2125/*
2126 * Same as above, but for IPv6.
2127 * Cut-and-pasted from ip6_input.c.
2128 * XXX Should we update ip6stat, or not?
2129 */
2130static int
2131bridge_ip6_checkbasic(struct mbuf **mp)
2132{
2133	struct mbuf *m = *mp;
2134	struct ip6_hdr *ip6;
2135
2136        /*
2137         * If the IPv6 header is not aligned, slurp it up into a new
2138         * mbuf with space for link headers, in the event we forward
2139         * it.  Otherwise, if it is aligned, make sure the entire base
2140         * IPv6 header is in the first mbuf of the chain.
2141         */
2142        if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2143                struct ifnet *inifp = m->m_pkthdr.rcvif;
2144                if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2145                                  (max_linkhdr + 3) & ~3)) == NULL) {
2146                        /* XXXJRT new stat, please */
2147                        ip6stat.ip6s_toosmall++;
2148                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2149                        goto bad;
2150                }
2151        } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2152                struct ifnet *inifp = m->m_pkthdr.rcvif;
2153                if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2154                        ip6stat.ip6s_toosmall++;
2155                        in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2156                        goto bad;
2157                }
2158        }
2159
2160        ip6 = mtod(m, struct ip6_hdr *);
2161
2162        if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2163                ip6stat.ip6s_badvers++;
2164                in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2165                goto bad;
2166        }
2167
2168	/* Checks out, proceed */
2169	*mp = m;
2170	return 0;
2171
2172    bad:
2173	*mp = m;
2174	return -1;
2175}
2176# endif /* INET6 */
2177#endif /* BRIDGE_IPF */
2178