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