2 * Copyright 2001 Wasabi Systems, Inc.
5 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Wasabi Systems, Inc.
19 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
20 * or promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by Jason L. Wright
51 * 4. The name of the author may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
57 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
63 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
66 * $OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp $
67 * $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $
68 * $FreeBSD: src/sys/net/if_bridge.c,v 1.26 2005/10/13 23:05:55 thompsa Exp $
69 * $DragonFly: src/sys/net/bridge/if_bridge.c,v 1.31 2008/06/14 04:35:32 sephe Exp $
73 * Network interface bridge support.
77 * - Currently only supports Ethernet-like interfaces (Ethernet,
78 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
79 * to bridge other types of interfaces (FDDI-FDDI, and maybe
80 * consider heterogenous bridges).
83 #include <sys/cdefs.h>
86 #include "opt_inet6.h"
88 #include <sys/param.h>
90 #include <sys/malloc.h>
91 #include <sys/protosw.h>
92 #include <sys/systm.h>
94 #include <sys/socket.h> /* for net/if.h */
95 #include <sys/sockio.h>
96 #include <sys/ctype.h> /* string functions */
97 #include <sys/kernel.h>
98 #include <sys/random.h>
99 #include <sys/sysctl.h>
100 #include <sys/module.h>
101 #include <sys/proc.h>
102 #include <sys/lock.h>
103 #include <sys/thread.h>
104 #include <sys/thread2.h>
105 #include <sys/mpipe.h>
109 #include <net/if_dl.h>
110 #include <net/if_types.h>
111 #include <net/if_var.h>
112 #include <net/pfil.h>
113 #include <net/ifq_var.h>
114 #include <net/if_clone.h>
116 #include <netinet/in.h> /* for struct arpcom */
117 #include <netinet/in_systm.h>
118 #include <netinet/in_var.h>
119 #include <netinet/ip.h>
120 #include <netinet/ip_var.h>
122 #include <netinet/ip6.h>
123 #include <netinet6/ip6_var.h>
125 #include <netinet/if_ether.h> /* for struct arpcom */
126 #include <net/bridge/if_bridgevar.h>
127 #include <net/if_llc.h>
128 #include <net/netmsg2.h>
130 #include <net/route.h>
131 #include <sys/in_cksum.h>
134 * Size of the route hash table. Must be a power of two.
136 #ifndef BRIDGE_RTHASH_SIZE
137 #define BRIDGE_RTHASH_SIZE 1024
140 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
143 * Maximum number of addresses to cache.
145 #ifndef BRIDGE_RTABLE_MAX
146 #define BRIDGE_RTABLE_MAX 100
150 * Spanning tree defaults.
152 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
153 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
154 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
155 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
156 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
157 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
158 #define BSTP_DEFAULT_PATH_COST 55
161 * Timeout (in seconds) for entries learned dynamically.
163 #ifndef BRIDGE_RTABLE_TIMEOUT
164 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
168 * Number of seconds between walks of the route list.
170 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
171 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
175 * List of capabilities to mask on the member interface.
177 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
179 eventhandler_tag bridge_detach_cookie = NULL;
181 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
182 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *);
183 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
185 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
187 static int bridge_clone_create(struct if_clone *, int);
188 static void bridge_clone_destroy(struct ifnet *);
190 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
191 static void bridge_mutecaps(struct bridge_iflist *, int);
192 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
193 static void bridge_init(void *);
194 static void bridge_stop(struct ifnet *);
195 static void bridge_start(struct ifnet *);
196 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
197 static int bridge_output(struct ifnet *, struct mbuf *);
199 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
201 static void bridge_timer(void *);
203 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
205 static void bridge_span(struct bridge_softc *, struct mbuf *);
207 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
208 struct ifnet *, int, uint8_t);
209 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
210 static void bridge_rttrim(struct bridge_softc *);
211 static void bridge_rtage(struct bridge_softc *);
212 static void bridge_rtflush(struct bridge_softc *, int);
213 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
215 static int bridge_rtable_init(struct bridge_softc *);
216 static void bridge_rtable_fini(struct bridge_softc *);
218 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
219 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
221 static int bridge_rtnode_insert(struct bridge_softc *,
222 struct bridge_rtnode *);
223 static void bridge_rtnode_destroy(struct bridge_softc *,
224 struct bridge_rtnode *);
226 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
228 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
230 static void bridge_delete_member(struct bridge_softc *,
231 struct bridge_iflist *, int);
232 static void bridge_delete_span(struct bridge_softc *,
233 struct bridge_iflist *);
235 static int bridge_ioctl_add(struct bridge_softc *, void *);
236 static int bridge_ioctl_del(struct bridge_softc *, void *);
237 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
238 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
239 static int bridge_ioctl_scache(struct bridge_softc *, void *);
240 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
241 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
242 static int bridge_ioctl_rts(struct bridge_softc *, void *);
243 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
244 static int bridge_ioctl_sto(struct bridge_softc *, void *);
245 static int bridge_ioctl_gto(struct bridge_softc *, void *);
246 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
247 static int bridge_ioctl_flush(struct bridge_softc *, void *);
248 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
249 static int bridge_ioctl_spri(struct bridge_softc *, void *);
250 static int bridge_ioctl_ght(struct bridge_softc *, void *);
251 static int bridge_ioctl_sht(struct bridge_softc *, void *);
252 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
253 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
254 static int bridge_ioctl_gma(struct bridge_softc *, void *);
255 static int bridge_ioctl_sma(struct bridge_softc *, void *);
256 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
257 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
258 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
259 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
260 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
262 static int bridge_ip_checkbasic(struct mbuf **mp);
264 static int bridge_ip6_checkbasic(struct mbuf **mp);
266 static int bridge_fragment(struct ifnet *, struct mbuf *,
267 struct ether_header *, int, struct llc *);
268 static void bridge_enqueue_internal(struct ifnet *, struct mbuf *m,
270 static void bridge_enqueue_handler(struct netmsg *);
271 static void bridge_pfil_enqueue_handler(struct netmsg *);
272 static void bridge_pfil_enqueue(struct ifnet *, struct mbuf *, int);
273 static void bridge_handoff_notags(struct ifnet *, struct mbuf *);
274 static void bridge_handoff(struct ifnet *, struct mbuf *);
276 SYSCTL_DECL(_net_link);
277 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
279 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
280 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
281 static int pfil_member = 1; /* run pfil hooks on the member interface */
282 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
283 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
284 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
285 &pfil_bridge, 0, "Packet filter on the bridge interface");
286 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
287 &pfil_member, 0, "Packet filter on the member interface");
289 struct bridge_control {
290 int (*bc_func)(struct bridge_softc *, void *);
295 #define BC_F_COPYIN 0x01 /* copy arguments in */
296 #define BC_F_COPYOUT 0x02 /* copy arguments out */
297 #define BC_F_SUSER 0x04 /* do super-user check */
299 const struct bridge_control bridge_control_table[] = {
300 { bridge_ioctl_add, sizeof(struct ifbreq),
301 BC_F_COPYIN|BC_F_SUSER },
302 { bridge_ioctl_del, sizeof(struct ifbreq),
303 BC_F_COPYIN|BC_F_SUSER },
305 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
306 BC_F_COPYIN|BC_F_COPYOUT },
307 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
308 BC_F_COPYIN|BC_F_SUSER },
310 { bridge_ioctl_scache, sizeof(struct ifbrparam),
311 BC_F_COPYIN|BC_F_SUSER },
312 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
315 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
316 BC_F_COPYIN|BC_F_COPYOUT },
317 { bridge_ioctl_rts, sizeof(struct ifbaconf),
318 BC_F_COPYIN|BC_F_COPYOUT },
320 { bridge_ioctl_saddr, sizeof(struct ifbareq),
321 BC_F_COPYIN|BC_F_SUSER },
323 { bridge_ioctl_sto, sizeof(struct ifbrparam),
324 BC_F_COPYIN|BC_F_SUSER },
325 { bridge_ioctl_gto, sizeof(struct ifbrparam),
328 { bridge_ioctl_daddr, sizeof(struct ifbareq),
329 BC_F_COPYIN|BC_F_SUSER },
331 { bridge_ioctl_flush, sizeof(struct ifbreq),
332 BC_F_COPYIN|BC_F_SUSER },
334 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
336 { bridge_ioctl_spri, sizeof(struct ifbrparam),
337 BC_F_COPYIN|BC_F_SUSER },
339 { bridge_ioctl_ght, sizeof(struct ifbrparam),
341 { bridge_ioctl_sht, sizeof(struct ifbrparam),
342 BC_F_COPYIN|BC_F_SUSER },
344 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
346 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
347 BC_F_COPYIN|BC_F_SUSER },
349 { bridge_ioctl_gma, sizeof(struct ifbrparam),
351 { bridge_ioctl_sma, sizeof(struct ifbrparam),
352 BC_F_COPYIN|BC_F_SUSER },
354 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
355 BC_F_COPYIN|BC_F_SUSER },
357 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
358 BC_F_COPYIN|BC_F_SUSER },
360 { bridge_ioctl_addspan, sizeof(struct ifbreq),
361 BC_F_COPYIN|BC_F_SUSER },
362 { bridge_ioctl_delspan, sizeof(struct ifbreq),
363 BC_F_COPYIN|BC_F_SUSER },
365 const int bridge_control_table_size =
366 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
368 LIST_HEAD(, bridge_softc) bridge_list;
370 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
372 bridge_clone_destroy, 0, IF_MAXUNIT);
375 bridge_modevent(module_t mod, int type, void *data)
379 LIST_INIT(&bridge_list);
380 if_clone_attach(&bridge_cloner);
381 bridge_input_p = bridge_input;
382 bridge_output_p = bridge_output;
383 bridge_detach_cookie = EVENTHANDLER_REGISTER(
384 ifnet_detach_event, bridge_ifdetach, NULL,
385 EVENTHANDLER_PRI_ANY);
387 bstp_linkstate_p = bstp_linkstate;
391 if (!LIST_EMPTY(&bridge_list))
393 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
394 bridge_detach_cookie);
395 if_clone_detach(&bridge_cloner);
396 bridge_input_p = NULL;
397 bridge_output_p = NULL;
399 bstp_linkstate_p = NULL;
408 static moduledata_t bridge_mod = {
414 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
418 * bridge_clone_create:
420 * Create a new bridge instance.
423 bridge_clone_create(struct if_clone *ifc, int unit)
425 struct bridge_softc *sc;
429 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
430 ifp = sc->sc_ifp = &sc->sc_if;
432 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
433 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
434 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
435 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
436 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
437 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
438 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
440 /* Initialize our routing table. */
441 bridge_rtable_init(sc);
443 callout_init(&sc->sc_brcallout);
444 callout_init(&sc->sc_bstpcallout);
446 LIST_INIT(&sc->sc_iflist);
447 LIST_INIT(&sc->sc_spanlist);
450 if_initname(ifp, ifc->ifc_name, unit);
451 ifp->if_mtu = ETHERMTU;
452 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
453 ifp->if_ioctl = bridge_ioctl;
454 ifp->if_start = bridge_start;
455 ifp->if_init = bridge_init;
456 ifp->if_type = IFT_BRIDGE;
457 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
458 ifp->if_snd.ifq_maxlen = ifqmaxlen;
459 ifq_set_ready(&ifp->if_snd);
460 ifp->if_hdrlen = ETHER_HDR_LEN;
463 * Generate a random ethernet address and use the private AC:DE:48
467 int rnd = karc4random();
468 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
470 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
472 eaddr[0] &= ~1; /* clear multicast bit */
473 eaddr[0] |= 2; /* set the LAA bit */
475 ether_ifattach(ifp, eaddr, NULL);
476 /* Now undo some of the damage... */
477 ifp->if_baudrate = 0;
478 ifp->if_type = IFT_BRIDGE;
481 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
488 * bridge_clone_destroy:
490 * Destroy a bridge instance.
493 bridge_clone_destroy(struct ifnet *ifp)
495 struct bridge_softc *sc = ifp->if_softc;
496 struct bridge_iflist *bif;
498 lwkt_serialize_enter(ifp->if_serializer);
501 ifp->if_flags &= ~IFF_UP;
503 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
504 bridge_delete_member(sc, bif, 0);
506 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
507 bridge_delete_span(sc, bif);
510 callout_stop(&sc->sc_brcallout);
511 callout_stop(&sc->sc_bstpcallout);
513 lwkt_serialize_exit(ifp->if_serializer);
516 LIST_REMOVE(sc, sc_list);
521 /* Tear down the routing table. */
522 bridge_rtable_fini(sc);
530 * Handle a control request from the operator.
533 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
535 struct bridge_softc *sc = ifp->if_softc;
537 struct ifbreq ifbreq;
538 struct ifbifconf ifbifconf;
539 struct ifbareq ifbareq;
540 struct ifbaconf ifbaconf;
541 struct ifbrparam ifbrparam;
543 struct ifdrv *ifd = (struct ifdrv *) data;
544 const struct bridge_control *bc;
547 ASSERT_SERIALIZED(ifp->if_serializer);
556 if (ifd->ifd_cmd >= bridge_control_table_size) {
560 bc = &bridge_control_table[ifd->ifd_cmd];
562 if (cmd == SIOCGDRVSPEC &&
563 (bc->bc_flags & BC_F_COPYOUT) == 0) {
566 } else if (cmd == SIOCSDRVSPEC &&
567 (bc->bc_flags & BC_F_COPYOUT) != 0) {
572 if (bc->bc_flags & BC_F_SUSER) {
573 error = suser_cred(cr, NULL_CRED_OKAY);
578 if (ifd->ifd_len != bc->bc_argsize ||
579 ifd->ifd_len > sizeof(args)) {
584 memset(&args, 0, sizeof(args));
585 if (bc->bc_flags & BC_F_COPYIN) {
586 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
591 error = bc->bc_func(sc, &args);
595 if (bc->bc_flags & BC_F_COPYOUT)
596 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
600 if (!(ifp->if_flags & IFF_UP) &&
601 (ifp->if_flags & IFF_RUNNING)) {
603 * If interface is marked down and it is running,
607 } else if ((ifp->if_flags & IFF_UP) &&
608 !(ifp->if_flags & IFF_RUNNING)) {
610 * If interface is marked up and it is stopped, then
618 /* Do not allow the MTU to be changed on the bridge */
623 error = ether_ioctl(ifp, cmd, data);
632 * Clear or restore unwanted capabilities on the member interface
635 bridge_mutecaps(struct bridge_iflist *bif, int mute)
637 struct ifnet *ifp = bif->bif_ifp;
641 if (ifp->if_ioctl == NULL)
644 bzero(&ifr, sizeof(ifr));
645 ifr.ifr_reqcap = ifp->if_capenable;
648 /* mask off and save capabilities */
649 bif->bif_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
650 if (bif->bif_mutecap != 0)
651 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
653 /* restore muted capabilities */
654 ifr.ifr_reqcap |= bif->bif_mutecap;
656 if (bif->bif_mutecap != 0) {
657 lwkt_serialize_enter(ifp->if_serializer);
658 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
659 lwkt_serialize_exit(ifp->if_serializer);
664 * bridge_lookup_member:
666 * Lookup a bridge member interface.
668 static struct bridge_iflist *
669 bridge_lookup_member(struct bridge_softc *sc, const char *name)
671 struct bridge_iflist *bif;
674 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
676 if (strcmp(ifp->if_xname, name) == 0)
684 * bridge_lookup_member_if:
686 * Lookup a bridge member interface by ifnet*.
688 static struct bridge_iflist *
689 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
691 struct bridge_iflist *bif;
693 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
694 if (bif->bif_ifp == member_ifp)
702 * bridge_delete_member:
704 * Delete the specified member interface.
707 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
710 struct ifnet *ifs = bif->bif_ifp;
713 switch (ifs->if_type) {
717 * Take the interface out of promiscuous mode.
719 (void) ifpromisc(ifs, 0);
720 bridge_mutecaps(bif, 0);
728 panic("bridge_delete_member: impossible");
734 ifs->if_bridge = NULL;
735 LIST_REMOVE(bif, bif_next);
737 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
739 kfree(bif, M_DEVBUF);
741 if (sc->sc_ifp->if_flags & IFF_RUNNING)
742 bstp_initialization(sc);
746 * bridge_delete_span:
748 * Delete the specified span interface.
751 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
753 KASSERT(bif->bif_ifp->if_bridge == NULL,
754 ("%s: not a span interface", __func__));
756 LIST_REMOVE(bif, bif_next);
757 kfree(bif, M_DEVBUF);
761 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
763 struct ifbreq *req = arg;
764 struct bridge_iflist *bif = NULL;
768 ifs = ifunit(req->ifbr_ifsname);
772 /* If it's in the span list, it can't be a member. */
773 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
774 if (ifs == bif->bif_ifp)
777 /* Allow the first Ethernet member to define the MTU */
778 if (ifs->if_type != IFT_GIF) {
779 if (LIST_EMPTY(&sc->sc_iflist))
780 sc->sc_ifp->if_mtu = ifs->if_mtu;
781 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
782 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
788 if (ifs->if_bridge == sc)
791 if (ifs->if_bridge != NULL)
794 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
796 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
797 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
798 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
800 switch (ifs->if_type) {
804 * Place the interface into promiscuous mode.
806 error = ifpromisc(ifs, 1);
810 bridge_mutecaps(bif, 1);
813 case IFT_GIF: /* :^) */
823 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
825 if (sc->sc_ifp->if_flags & IFF_RUNNING)
826 bstp_initialization(sc);
833 kfree(bif, M_DEVBUF);
839 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
841 struct ifbreq *req = arg;
842 struct bridge_iflist *bif;
844 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
848 bridge_delete_member(sc, bif, 0);
854 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
856 struct ifbreq *req = arg;
857 struct bridge_iflist *bif;
859 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
863 req->ifbr_ifsflags = bif->bif_flags;
864 req->ifbr_state = bif->bif_state;
865 req->ifbr_priority = bif->bif_priority;
866 req->ifbr_path_cost = bif->bif_path_cost;
867 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
873 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
875 struct ifbreq *req = arg;
876 struct bridge_iflist *bif;
878 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
882 if (req->ifbr_ifsflags & IFBIF_SPAN)
883 /* SPAN is readonly */
886 if (req->ifbr_ifsflags & IFBIF_STP) {
887 switch (bif->bif_ifp->if_type) {
889 /* These can do spanning tree. */
893 /* Nothing else can. */
898 bif->bif_flags = req->ifbr_ifsflags;
900 if (sc->sc_ifp->if_flags & IFF_RUNNING)
901 bstp_initialization(sc);
907 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
909 struct ifbrparam *param = arg;
911 sc->sc_brtmax = param->ifbrp_csize;
918 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
920 struct ifbrparam *param = arg;
922 param->ifbrp_csize = sc->sc_brtmax;
928 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
930 struct ifbifconf *bifc = arg;
931 struct bridge_iflist *bif;
933 int count, len, error = 0;
936 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
938 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
941 if (bifc->ifbic_len == 0) {
942 bifc->ifbic_len = sizeof(breq) * count;
947 len = bifc->ifbic_len;
948 memset(&breq, 0, sizeof breq);
949 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
950 if (len < sizeof(breq))
953 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
954 sizeof(breq.ifbr_ifsname));
955 breq.ifbr_ifsflags = bif->bif_flags;
956 breq.ifbr_state = bif->bif_state;
957 breq.ifbr_priority = bif->bif_priority;
958 breq.ifbr_path_cost = bif->bif_path_cost;
959 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
960 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
966 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
967 if (len < sizeof(breq))
970 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
971 sizeof(breq.ifbr_ifsname));
972 breq.ifbr_ifsflags = bif->bif_flags;
973 breq.ifbr_state = bif->bif_state;
974 breq.ifbr_priority = bif->bif_priority;
975 breq.ifbr_path_cost = bif->bif_path_cost;
976 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
977 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
984 bifc->ifbic_len = sizeof(breq) * count;
989 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
991 struct ifbaconf *bac = arg;
992 struct bridge_rtnode *brt;
993 struct ifbareq bareq;
994 int count = 0, error = 0, len;
996 if (bac->ifbac_len == 0)
999 len = bac->ifbac_len;
1000 memset(&bareq, 0, sizeof(bareq));
1001 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1002 if (len < sizeof(bareq))
1004 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1005 sizeof(bareq.ifba_ifsname));
1006 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1007 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1008 time_second < brt->brt_expire)
1009 bareq.ifba_expire = brt->brt_expire - time_second;
1011 bareq.ifba_expire = 0;
1012 bareq.ifba_flags = brt->brt_flags;
1014 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1018 len -= sizeof(bareq);
1021 bac->ifbac_len = sizeof(bareq) * count;
1026 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1028 struct ifbareq *req = arg;
1029 struct bridge_iflist *bif;
1032 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1036 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1043 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1045 struct ifbrparam *param = arg;
1047 sc->sc_brttimeout = param->ifbrp_ctime;
1053 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1055 struct ifbrparam *param = arg;
1057 param->ifbrp_ctime = sc->sc_brttimeout;
1063 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1065 struct ifbareq *req = arg;
1067 return (bridge_rtdaddr(sc, req->ifba_dst));
1071 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1073 struct ifbreq *req = arg;
1075 bridge_rtflush(sc, req->ifbr_ifsflags);
1081 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1083 struct ifbrparam *param = arg;
1085 param->ifbrp_prio = sc->sc_bridge_priority;
1091 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1093 struct ifbrparam *param = arg;
1095 sc->sc_bridge_priority = param->ifbrp_prio;
1097 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1098 bstp_initialization(sc);
1104 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1106 struct ifbrparam *param = arg;
1108 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1114 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1116 struct ifbrparam *param = arg;
1118 if (param->ifbrp_hellotime == 0)
1120 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1122 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1123 bstp_initialization(sc);
1129 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1131 struct ifbrparam *param = arg;
1133 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1139 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1141 struct ifbrparam *param = arg;
1143 if (param->ifbrp_fwddelay == 0)
1145 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1147 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1148 bstp_initialization(sc);
1154 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1156 struct ifbrparam *param = arg;
1158 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1164 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1166 struct ifbrparam *param = arg;
1168 if (param->ifbrp_maxage == 0)
1170 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1172 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1173 bstp_initialization(sc);
1179 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1181 struct ifbreq *req = arg;
1182 struct bridge_iflist *bif;
1184 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1188 bif->bif_priority = req->ifbr_priority;
1190 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1191 bstp_initialization(sc);
1197 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1199 struct ifbreq *req = arg;
1200 struct bridge_iflist *bif;
1202 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1206 bif->bif_path_cost = req->ifbr_path_cost;
1208 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1209 bstp_initialization(sc);
1215 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1217 struct ifbreq *req = arg;
1218 struct bridge_iflist *bif = NULL;
1221 ifs = ifunit(req->ifbr_ifsname);
1225 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1226 if (ifs == bif->bif_ifp)
1229 if (ifs->if_bridge != NULL)
1232 switch (ifs->if_type) {
1241 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
1244 bif->bif_flags = IFBIF_SPAN;
1246 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1252 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1254 struct ifbreq *req = arg;
1255 struct bridge_iflist *bif;
1258 ifs = ifunit(req->ifbr_ifsname);
1262 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1263 if (ifs == bif->bif_ifp)
1269 bridge_delete_span(sc, bif);
1277 * Detach an interface from a bridge. Called when a member
1278 * interface is detaching.
1281 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1283 struct bridge_softc *sc = ifp->if_bridge;
1284 struct bridge_iflist *bif;
1286 /* Check if the interface is a bridge member */
1288 lwkt_serialize_enter(ifp->if_serializer);
1290 bif = bridge_lookup_member_if(sc, ifp);
1292 bridge_delete_member(sc, bif, 1);
1294 lwkt_serialize_exit(ifp->if_serializer);
1298 /* Check if the interface is a span port */
1299 LIST_FOREACH(sc, &bridge_list, sc_list) {
1300 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1301 if (ifp == bif->bif_ifp) {
1302 bridge_delete_span(sc, bif);
1311 * Initialize a bridge interface.
1314 bridge_init(void *xsc)
1316 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1317 struct ifnet *ifp = sc->sc_ifp;
1319 ASSERT_SERIALIZED(ifp->if_serializer);
1321 if (ifp->if_flags & IFF_RUNNING)
1324 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1327 ifp->if_flags |= IFF_RUNNING;
1328 bstp_initialization(sc);
1335 * Stop the bridge interface.
1338 bridge_stop(struct ifnet *ifp)
1340 struct bridge_softc *sc = ifp->if_softc;
1342 ASSERT_SERIALIZED(ifp->if_serializer);
1344 if ((ifp->if_flags & IFF_RUNNING) == 0)
1347 callout_stop(&sc->sc_brcallout);
1350 bridge_rtflush(sc, IFBF_FLUSHDYN);
1352 ifp->if_flags &= ~IFF_RUNNING;
1356 bridge_enqueue_internal(struct ifnet *dst_ifp, struct mbuf *m,
1357 netisr_fn_t handler)
1359 struct netmsg_packet *nmp;
1361 int cpu = mycpu->gd_cpuid;
1363 while (m->m_type == MT_TAG) {
1364 /* XXX see ether_output_frame for full rules check */
1368 nmp = &m->m_hdr.mh_netmsg;
1369 netmsg_init(&nmp->nm_netmsg, &netisr_apanic_rport, 0, handler);
1371 nmp->nm_netmsg.nm_lmsg.u.ms_resultp = dst_ifp;
1373 port = cpu_portfn(cpu);
1374 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
1378 bridge_pfil_enqueue(struct ifnet *dst_ifp, struct mbuf *m,
1381 netisr_fn_t handler;
1383 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1385 || inet6_pfil_hook.ph_hashooks > 0
1388 handler = bridge_pfil_enqueue_handler;
1390 handler = bridge_enqueue_handler;
1392 bridge_enqueue_internal(dst_ifp, m, handler);
1398 * Enqueue a packet on a bridge member interface.
1402 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1404 bridge_enqueue_internal(dst_ifp, m, bridge_enqueue_handler);
1410 * Send output from a bridge member interface. This
1411 * performs the bridging function for locally originated
1414 * The mbuf has the Ethernet header already attached. We must
1415 * enqueue or free the mbuf before returning.
1418 bridge_output(struct ifnet *ifp, struct mbuf *m)
1420 struct ether_header *eh;
1421 struct ifnet *dst_if;
1422 struct bridge_softc *sc;
1424 sc = ifp->if_bridge;
1426 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
1428 if (m->m_len < ETHER_HDR_LEN) {
1429 m = m_pullup(m, ETHER_HDR_LEN);
1434 /* Serialize our bridge interface. */
1435 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1437 eh = mtod(m, struct ether_header *);
1440 * If bridge is down, but the original output interface is up,
1441 * go ahead and send out that interface. Otherwise, the packet
1444 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
1450 * If the packet is a multicast, or we don't know a better way to
1451 * get there, send to all interfaces.
1453 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1456 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1457 if (dst_if == NULL) {
1458 struct bridge_iflist *bif;
1464 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1465 dst_if = bif->bif_ifp;
1466 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1470 * If this is not the original output interface,
1471 * and the interface is participating in spanning
1472 * tree, make sure the port is in a state that
1473 * allows forwarding.
1475 if (dst_if != ifp &&
1476 (bif->bif_flags & IFBIF_STP) != 0) {
1477 switch (bif->bif_state) {
1478 case BSTP_IFSTATE_BLOCKING:
1479 case BSTP_IFSTATE_LISTENING:
1480 case BSTP_IFSTATE_DISABLED:
1485 if (LIST_NEXT(bif, bif_next) == NULL) {
1489 mc = m_copypacket(m, MB_DONTWAIT);
1491 sc->sc_ifp->if_oerrors++;
1495 bridge_enqueue(dst_if, mc);
1499 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1505 * XXX Spanning tree consideration here?
1509 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1510 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1513 bridge_enqueue(dst_if, m);
1520 * Start output on a bridge.
1524 bridge_start(struct ifnet *ifp)
1526 struct bridge_softc *sc = ifp->if_softc;
1528 ASSERT_SERIALIZED(ifp->if_serializer);
1530 ifp->if_flags |= IFF_OACTIVE;
1532 struct ifnet *dst_if = NULL;
1533 struct ether_header *eh;
1536 m = ifq_dequeue(&ifp->if_snd, NULL);
1540 if (m->m_len < sizeof(*eh)) {
1541 m = m_pullup(m, sizeof(*eh));
1547 eh = mtod(m, struct ether_header *);
1552 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1553 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1556 bridge_broadcast(sc, ifp, m, 0);
1558 bridge_enqueue(dst_if, m);
1560 ifp->if_flags &= ~IFF_OACTIVE;
1566 * The forwarding function of the bridge.
1569 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1571 struct bridge_iflist *bif;
1572 struct ifnet *src_if, *dst_if, *ifp;
1573 struct ether_header *eh;
1575 src_if = m->m_pkthdr.rcvif;
1578 ASSERT_SERIALIZED(ifp->if_serializer);
1581 ifp->if_ibytes += m->m_pkthdr.len;
1584 * Look up the bridge_iflist.
1586 bif = bridge_lookup_member_if(sc, src_if);
1588 /* Interface is not a bridge member (anymore?) */
1593 if (bif->bif_flags & IFBIF_STP) {
1594 switch (bif->bif_state) {
1595 case BSTP_IFSTATE_BLOCKING:
1596 case BSTP_IFSTATE_LISTENING:
1597 case BSTP_IFSTATE_DISABLED:
1603 eh = mtod(m, struct ether_header *);
1606 * If the interface is learning, and the source
1607 * address is valid and not multicast, record
1610 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1611 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1612 (eh->ether_shost[0] == 0 &&
1613 eh->ether_shost[1] == 0 &&
1614 eh->ether_shost[2] == 0 &&
1615 eh->ether_shost[3] == 0 &&
1616 eh->ether_shost[4] == 0 &&
1617 eh->ether_shost[5] == 0) == 0) {
1618 bridge_rtupdate(sc, eh->ether_shost, src_if, 0, IFBAF_DYNAMIC);
1621 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1622 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1628 * At this point, the port either doesn't participate
1629 * in spanning tree or it is in the forwarding state.
1633 * If the packet is unicast, destined for someone on
1634 * "this" side of the bridge, drop it.
1636 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1637 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1638 if (src_if == dst_if) {
1643 /* ...forward it to all interfaces. */
1644 sc->sc_ifp->if_imcasts++;
1648 if (dst_if == NULL) {
1649 bridge_broadcast(sc, src_if, m, 1);
1654 * At this point, we're dealing with a unicast frame
1655 * going to a different interface.
1657 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1661 bif = bridge_lookup_member_if(sc, dst_if);
1663 /* Not a member of the bridge (anymore?) */
1668 if (bif->bif_flags & IFBIF_STP) {
1669 switch (bif->bif_state) {
1670 case BSTP_IFSTATE_DISABLED:
1671 case BSTP_IFSTATE_BLOCKING:
1677 lwkt_serialize_exit(ifp->if_serializer);
1679 /* run the packet filter */
1680 if (inet_pfil_hook.ph_hashooks > 0
1682 || inet6_pfil_hook.ph_hashooks > 0
1685 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1690 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
1695 bridge_handoff(dst_if, m);
1698 * ifp's serializer was held on entry and is expected to be held
1702 lwkt_serialize_enter(ifp->if_serializer);
1708 * Receive input from a member interface. Queue the packet for
1709 * bridging if it is not for us.
1711 static struct mbuf *
1712 bridge_input(struct ifnet *ifp, struct mbuf *m)
1714 struct bridge_softc *sc = ifp->if_bridge;
1715 struct bridge_iflist *bif;
1716 struct ifnet *bifp, *new_ifp;
1717 struct ether_header *eh;
1718 struct mbuf *mc, *mc2;
1723 lwkt_serialize_enter(bifp->if_serializer);
1725 if ((bifp->if_flags & IFF_RUNNING) == 0)
1729 * Implement support for bridge monitoring. If this flag has been
1730 * set on this interface, discard the packet once we push it through
1731 * the bpf(4) machinery, but before we do, increment the byte and
1732 * packet counters associated with this interface.
1734 if ((bifp->if_flags & IFF_MONITOR) != 0) {
1735 m->m_pkthdr.rcvif = bifp;
1737 bifp->if_ipackets++;
1738 bifp->if_ibytes += m->m_pkthdr.len;
1744 eh = mtod(m, struct ether_header *);
1746 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
1748 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
1750 * If the packet is for us, set the packets source as the
1751 * bridge, and return the packet back to ifnet.if_input for
1754 KASSERT(bifp->if_bridge == NULL,
1755 ("loop created in bridge_input"));
1761 * Tap all packets arriving on the bridge, no matter if
1762 * they are local destinations or not. In is in.
1766 bif = bridge_lookup_member_if(sc, ifp);
1772 if (m->m_flags & (M_BCAST | M_MCAST)) {
1773 /* Tap off 802.1D packets; they do not get forwarded. */
1774 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1775 ETHER_ADDR_LEN) == 0) {
1776 m = bstp_input(ifp, m);
1778 ("attempt to deliver 802.1D packet\n"));
1782 if (bif->bif_flags & IFBIF_STP) {
1783 switch (bif->bif_state) {
1784 case BSTP_IFSTATE_BLOCKING:
1785 case BSTP_IFSTATE_LISTENING:
1786 case BSTP_IFSTATE_DISABLED:
1792 * Make a deep copy of the packet and enqueue the copy
1793 * for bridge processing; return the original packet for
1796 mc = m_dup(m, MB_DONTWAIT);
1800 bridge_forward(sc, mc);
1803 * Reinject the mbuf as arriving on the bridge so we have a
1804 * chance at claiming multicast packets. We can not loop back
1805 * here from ether_input as a bridge is never a member of a
1808 KASSERT(bifp->if_bridge == NULL,
1809 ("loop created in bridge_input"));
1810 mc2 = m_dup(m, MB_DONTWAIT);
1813 /* Keep the layer3 header aligned */
1814 int i = min(mc2->m_pkthdr.len, max_protohdr);
1815 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
1819 mc2->m_pkthdr.rcvif = bifp;
1820 bifp->if_ipackets++;
1821 bifp->if_input(bifp, mc2);
1824 /* Return the original packet for local processing. */
1828 if (bif->bif_flags & IFBIF_STP) {
1829 switch (bif->bif_state) {
1830 case BSTP_IFSTATE_BLOCKING:
1831 case BSTP_IFSTATE_LISTENING:
1832 case BSTP_IFSTATE_DISABLED:
1838 * Unicast. Make sure it's not for us.
1840 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1841 if (bif->bif_ifp->if_type != IFT_ETHER)
1844 /* It is destined for us. */
1845 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1846 ETHER_ADDR_LEN) == 0) {
1847 if (bif->bif_flags & IFBIF_LEARNING) {
1849 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1852 if (bif->bif_ifp != ifp) {
1853 /* XXX loop prevention */
1854 m->m_flags |= M_PROTO1;
1855 new_ifp = bif->bif_ifp;
1860 /* We just received a packet that we sent out. */
1861 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1862 ETHER_ADDR_LEN) == 0) {
1869 /* Perform the bridge forwarding function. */
1870 bridge_forward(sc, m);
1873 lwkt_serialize_exit(bifp->if_serializer);
1875 if (new_ifp != NULL) {
1876 lwkt_serialize_enter(new_ifp->if_serializer);
1878 m->m_pkthdr.rcvif = new_ifp;
1879 new_ifp->if_ipackets++;
1880 new_ifp->if_input(new_ifp, m);
1883 lwkt_serialize_exit(new_ifp->if_serializer);
1891 * Send a frame to all interfaces that are members of
1892 * the bridge, except for the one on which the packet
1896 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1897 struct mbuf *m, int runfilt)
1899 struct bridge_iflist *bif;
1901 struct ifnet *dst_if, *bifp;
1906 ASSERT_SERIALIZED(bifp->if_serializer);
1908 /* run the packet filter */
1909 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1911 || inet6_pfil_hook.ph_hashooks > 0
1914 lwkt_serialize_exit(bifp->if_serializer);
1916 /* Filter on the bridge interface before broadcasting */
1918 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
1923 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
1926 lwkt_serialize_enter(bifp->if_serializer);
1931 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1932 dst_if = bif->bif_ifp;
1933 if (dst_if == src_if)
1936 if (bif->bif_flags & IFBIF_STP) {
1937 switch (bif->bif_state) {
1938 case BSTP_IFSTATE_BLOCKING:
1939 case BSTP_IFSTATE_DISABLED:
1944 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1945 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1948 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1951 if (LIST_NEXT(bif, bif_next) == NULL) {
1955 mc = m_copypacket(m, MB_DONTWAIT);
1957 sc->sc_ifp->if_oerrors++;
1961 bridge_pfil_enqueue(dst_if, mc, runfilt);
1970 * Duplicate a packet out one or more interfaces that are in span mode,
1971 * the original mbuf is unmodified.
1974 bridge_span(struct bridge_softc *sc, struct mbuf *m)
1976 struct bridge_iflist *bif;
1977 struct ifnet *dst_if;
1980 if (LIST_EMPTY(&sc->sc_spanlist))
1983 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1984 dst_if = bif->bif_ifp;
1986 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1989 mc = m_copypacket(m, MB_DONTWAIT);
1991 sc->sc_ifp->if_oerrors++;
1995 bridge_enqueue(dst_if, mc);
2002 * Add a bridge routing entry.
2003 * Can be called from interrupt context.
2006 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2007 struct ifnet *dst_if, int setflags, uint8_t flags)
2009 struct bridge_rtnode *brt;
2013 * A route for this destination might already exist. If so,
2014 * update it, otherwise create a new one.
2016 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2017 if (sc->sc_brtcnt >= sc->sc_brtmax)
2021 * Allocate a new bridge forwarding node, and
2022 * initialize the expiration time and Ethernet
2025 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2026 M_INTNOWAIT|M_ZERO);
2030 brt->brt_flags = IFBAF_DYNAMIC;
2031 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2033 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2034 kfree(brt, M_DEVBUF);
2039 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2040 brt->brt_ifp = dst_if;
2041 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2042 brt->brt_expire = time_second + sc->sc_brttimeout;
2044 brt->brt_flags = flags;
2052 * Lookup the destination interface for an address.
2054 static struct ifnet *
2055 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2057 struct bridge_rtnode *brt;
2059 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2062 return (brt->brt_ifp);
2068 * Trim the routine table so that we have a number
2069 * of routing entries less than or equal to the
2073 bridge_rttrim(struct bridge_softc *sc)
2075 struct bridge_rtnode *brt, *nbrt;
2077 /* Make sure we actually need to do this. */
2078 if (sc->sc_brtcnt <= sc->sc_brtmax)
2081 /* Force an aging cycle; this might trim enough addresses. */
2083 if (sc->sc_brtcnt <= sc->sc_brtmax)
2086 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2087 nbrt = LIST_NEXT(brt, brt_list);
2088 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2089 bridge_rtnode_destroy(sc, brt);
2090 if (sc->sc_brtcnt <= sc->sc_brtmax)
2099 * Aging timer for the bridge.
2102 bridge_timer(void *arg)
2104 struct bridge_softc *sc = arg;
2106 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
2110 if (sc->sc_ifp->if_flags & IFF_RUNNING)
2111 callout_reset(&sc->sc_brcallout,
2112 bridge_rtable_prune_period * hz, bridge_timer, sc);
2114 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
2120 * Perform an aging cycle.
2123 bridge_rtage(struct bridge_softc *sc)
2125 struct bridge_rtnode *brt, *nbrt;
2127 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2128 nbrt = LIST_NEXT(brt, brt_list);
2129 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2130 if (time_second >= brt->brt_expire)
2131 bridge_rtnode_destroy(sc, brt);
2139 * Remove all dynamic addresses from the bridge.
2142 bridge_rtflush(struct bridge_softc *sc, int full)
2144 struct bridge_rtnode *brt, *nbrt;
2146 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2147 nbrt = LIST_NEXT(brt, brt_list);
2148 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2149 bridge_rtnode_destroy(sc, brt);
2156 * Remove an address from the table.
2159 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2161 struct bridge_rtnode *brt;
2163 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2166 bridge_rtnode_destroy(sc, brt);
2173 * Delete routes to a speicifc member interface.
2176 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2178 struct bridge_rtnode *brt, *nbrt;
2180 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2181 nbrt = LIST_NEXT(brt, brt_list);
2182 if (brt->brt_ifp == ifp && (full ||
2183 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2184 bridge_rtnode_destroy(sc, brt);
2189 * bridge_rtable_init:
2191 * Initialize the route table for this bridge.
2194 bridge_rtable_init(struct bridge_softc *sc)
2198 sc->sc_rthash = kmalloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2199 M_DEVBUF, M_WAITOK);
2201 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2202 LIST_INIT(&sc->sc_rthash[i]);
2204 sc->sc_rthash_key = karc4random();
2206 LIST_INIT(&sc->sc_rtlist);
2212 * bridge_rtable_fini:
2214 * Deconstruct the route table for this bridge.
2217 bridge_rtable_fini(struct bridge_softc *sc)
2220 kfree(sc->sc_rthash, M_DEVBUF);
2224 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2225 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2227 #define mix(a, b, c) \
2229 a -= b; a -= c; a ^= (c >> 13); \
2230 b -= c; b -= a; b ^= (a << 8); \
2231 c -= a; c -= b; c ^= (b >> 13); \
2232 a -= b; a -= c; a ^= (c >> 12); \
2233 b -= c; b -= a; b ^= (a << 16); \
2234 c -= a; c -= b; c ^= (b >> 5); \
2235 a -= b; a -= c; a ^= (c >> 3); \
2236 b -= c; b -= a; b ^= (a << 10); \
2237 c -= a; c -= b; c ^= (b >> 15); \
2238 } while (/*CONSTCOND*/0)
2240 static __inline uint32_t
2241 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2243 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2254 return (c & BRIDGE_RTHASH_MASK);
2260 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2264 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2265 d = ((int)a[i]) - ((int)b[i]);
2272 * bridge_rtnode_lookup:
2274 * Look up a bridge route node for the specified destination.
2276 static struct bridge_rtnode *
2277 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2279 struct bridge_rtnode *brt;
2283 hash = bridge_rthash(sc, addr);
2284 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2285 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2296 * bridge_rtnode_insert:
2298 * Insert the specified bridge node into the route table. We
2299 * assume the entry is not already in the table.
2302 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2304 struct bridge_rtnode *lbrt;
2308 hash = bridge_rthash(sc, brt->brt_addr);
2310 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2312 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2317 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2321 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2324 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2325 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2328 lbrt = LIST_NEXT(lbrt, brt_hash);
2329 } while (lbrt != NULL);
2332 panic("bridge_rtnode_insert: impossible");
2336 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2343 * bridge_rtnode_destroy:
2345 * Destroy a bridge rtnode.
2348 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2351 LIST_REMOVE(brt, brt_hash);
2353 LIST_REMOVE(brt, brt_list);
2355 kfree(brt, M_DEVBUF);
2359 * Send bridge packets through pfil if they are one of the types pfil can deal
2360 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2361 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2365 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2367 int snap, error, i, hlen;
2368 struct ether_header *eh1, eh2;
2371 u_int16_t ether_type;
2374 error = -1; /* Default error if not error == 0 */
2376 if (pfil_bridge == 0 && pfil_member == 0)
2377 return (0); /* filtering is disabled */
2379 i = min((*mp)->m_pkthdr.len, max_protohdr);
2380 if ((*mp)->m_len < i) {
2381 *mp = m_pullup(*mp, i);
2383 kprintf("%s: m_pullup failed\n", __func__);
2388 eh1 = mtod(*mp, struct ether_header *);
2389 ether_type = ntohs(eh1->ether_type);
2392 * Check for SNAP/LLC.
2394 if (ether_type < ETHERMTU) {
2395 struct llc *llc2 = (struct llc *)(eh1 + 1);
2397 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2398 llc2->llc_dsap == LLC_SNAP_LSAP &&
2399 llc2->llc_ssap == LLC_SNAP_LSAP &&
2400 llc2->llc_control == LLC_UI) {
2401 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2407 * If we're trying to filter bridge traffic, don't look at anything
2408 * other than IP and ARP traffic. If the filter doesn't understand
2409 * IPv6, don't allow IPv6 through the bridge either. This is lame
2410 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2411 * but of course we don't have an AppleTalk filter to begin with.
2412 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2415 switch (ether_type) {
2417 case ETHERTYPE_REVARP:
2418 return (0); /* Automatically pass */
2421 case ETHERTYPE_IPV6:
2426 * Check to see if the user wants to pass non-ip
2427 * packets, these will not be checked by pfil(9) and
2428 * passed unconditionally so the default is to drop.
2434 /* Strip off the Ethernet header and keep a copy. */
2435 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2436 m_adj(*mp, ETHER_HDR_LEN);
2438 /* Strip off snap header, if present */
2440 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2441 m_adj(*mp, sizeof(struct llc));
2445 * Check the IP header for alignment and errors
2447 if (dir == PFIL_IN) {
2448 switch (ether_type) {
2450 error = bridge_ip_checkbasic(mp);
2453 case ETHERTYPE_IPV6:
2454 error = bridge_ip6_checkbasic(mp);
2467 * Run the packet through pfil
2473 * before calling the firewall, swap fields the same as
2474 * IP does. here we assume the header is contiguous
2476 ip = mtod(*mp, struct ip *);
2478 ip->ip_len = ntohs(ip->ip_len);
2479 ip->ip_off = ntohs(ip->ip_off);
2482 * Run pfil on the member interface and the bridge, both can
2483 * be skipped by clearing pfil_member or pfil_bridge.
2486 * in_if -> bridge_if -> out_if
2488 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2489 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2492 if (*mp == NULL || error != 0) /* filter may consume */
2495 if (pfil_member && ifp != NULL)
2496 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2499 if (*mp == NULL || error != 0) /* filter may consume */
2502 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2503 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2506 if (*mp == NULL || error != 0) /* filter may consume */
2509 /* check if we need to fragment the packet */
2510 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
2511 i = (*mp)->m_pkthdr.len;
2512 if (i > ifp->if_mtu) {
2513 error = bridge_fragment(ifp, *mp, &eh2, snap,
2519 /* Recalculate the ip checksum and restore byte ordering */
2520 ip = mtod(*mp, struct ip *);
2521 hlen = ip->ip_hl << 2;
2522 if (hlen < sizeof(struct ip))
2524 if (hlen > (*mp)->m_len) {
2525 if ((*mp = m_pullup(*mp, hlen)) == 0)
2527 ip = mtod(*mp, struct ip *);
2531 ip->ip_len = htons(ip->ip_len);
2532 ip->ip_off = htons(ip->ip_off);
2534 if (hlen == sizeof(struct ip))
2535 ip->ip_sum = in_cksum_hdr(ip);
2537 ip->ip_sum = in_cksum(*mp, hlen);
2541 case ETHERTYPE_IPV6 :
2542 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2543 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2546 if (*mp == NULL || error != 0) /* filter may consume */
2549 if (pfil_member && ifp != NULL)
2550 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2553 if (*mp == NULL || error != 0) /* filter may consume */
2556 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2557 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2574 * Finally, put everything back the way it was and return
2577 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
2580 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2583 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
2586 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2597 * Perform basic checks on header size since
2598 * pfil assumes ip_input has already processed
2599 * it for it. Cut-and-pasted from ip_input.c.
2600 * Given how simple the IPv6 version is,
2601 * does the IPv4 version really need to be
2604 * XXX Should we update ipstat here, or not?
2605 * XXX Right now we update ipstat but not
2609 bridge_ip_checkbasic(struct mbuf **mp)
2611 struct mbuf *m = *mp;
2619 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2620 if ((m = m_copyup(m, sizeof(struct ip),
2621 (max_linkhdr + 3) & ~3)) == NULL) {
2622 /* XXXJRT new stat, please */
2623 ipstat.ips_toosmall++;
2628 #ifndef __predict_false
2629 #define __predict_false(x) x
2631 if (__predict_false(m->m_len < sizeof (struct ip))) {
2632 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2633 ipstat.ips_toosmall++;
2637 ip = mtod(m, struct ip *);
2638 if (ip == NULL) goto bad;
2640 if (ip->ip_v != IPVERSION) {
2641 ipstat.ips_badvers++;
2644 hlen = ip->ip_hl << 2;
2645 if (hlen < sizeof(struct ip)) { /* minimum header length */
2646 ipstat.ips_badhlen++;
2649 if (hlen > m->m_len) {
2650 if ((m = m_pullup(m, hlen)) == 0) {
2651 ipstat.ips_badhlen++;
2654 ip = mtod(m, struct ip *);
2655 if (ip == NULL) goto bad;
2658 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2659 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2661 if (hlen == sizeof(struct ip)) {
2662 sum = in_cksum_hdr(ip);
2664 sum = in_cksum(m, hlen);
2668 ipstat.ips_badsum++;
2672 /* Retrieve the packet length. */
2673 len = ntohs(ip->ip_len);
2676 * Check for additional length bogosity
2679 ipstat.ips_badlen++;
2684 * Check that the amount of data in the buffers
2685 * is as at least much as the IP header would have us expect.
2686 * Drop packet if shorter than we expect.
2688 if (m->m_pkthdr.len < len) {
2689 ipstat.ips_tooshort++;
2693 /* Checks out, proceed */
2704 * Same as above, but for IPv6.
2705 * Cut-and-pasted from ip6_input.c.
2706 * XXX Should we update ip6stat, or not?
2709 bridge_ip6_checkbasic(struct mbuf **mp)
2711 struct mbuf *m = *mp;
2712 struct ip6_hdr *ip6;
2715 * If the IPv6 header is not aligned, slurp it up into a new
2716 * mbuf with space for link headers, in the event we forward
2717 * it. Otherwise, if it is aligned, make sure the entire base
2718 * IPv6 header is in the first mbuf of the chain.
2721 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2722 struct ifnet *inifp = m->m_pkthdr.rcvif;
2723 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2724 (max_linkhdr + 3) & ~3)) == NULL) {
2725 /* XXXJRT new stat, please */
2726 ip6stat.ip6s_toosmall++;
2727 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2732 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2733 struct ifnet *inifp = m->m_pkthdr.rcvif;
2734 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2735 ip6stat.ip6s_toosmall++;
2736 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2741 ip6 = mtod(m, struct ip6_hdr *);
2743 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2744 ip6stat.ip6s_badvers++;
2745 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2749 /* Checks out, proceed */
2762 * Return a fragmented mbuf chain.
2765 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
2766 int snap, struct llc *llc)
2772 if (m->m_len < sizeof(struct ip) &&
2773 (m = m_pullup(m, sizeof(struct ip))) == NULL)
2775 ip = mtod(m, struct ip *);
2777 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
2782 /* walk the chain and re-add the Ethernet header */
2783 for (m0 = m; m0; m0 = m0->m_nextpkt) {
2786 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
2791 bcopy(llc, mtod(m0, caddr_t),
2792 sizeof(struct llc));
2794 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
2799 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
2805 ipstat.ips_fragmented++;
2816 bridge_enqueue_handler(struct netmsg *nmsg)
2818 struct netmsg_packet *nmp;
2819 struct ifnet *dst_ifp;
2822 nmp = (struct netmsg_packet *)nmsg;
2824 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2826 bridge_handoff_notags(dst_ifp, m);
2830 bridge_pfil_enqueue_handler(struct netmsg *nmsg)
2832 struct netmsg_packet *nmp;
2833 struct ifnet *dst_ifp;
2836 nmp = (struct netmsg_packet *)nmsg;
2838 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2841 * Filter on the output interface. Pass a NULL bridge interface
2842 * pointer so we do not redundantly filter on the bridge for
2843 * each interface we broadcast on.
2845 if (inet_pfil_hook.ph_hashooks > 0
2847 || inet6_pfil_hook.ph_hashooks > 0
2850 if (bridge_pfil(&m, NULL, dst_ifp, PFIL_OUT) != 0)
2855 bridge_handoff_notags(dst_ifp, m);
2859 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m)
2861 while (m->m_type == MT_TAG) {
2862 /* XXX see ether_output_frame for full rules check */
2865 bridge_handoff_notags(dst_ifp, m);
2869 bridge_handoff_notags(struct ifnet *dst_ifp, struct mbuf *m)
2873 KKASSERT(m->m_type != MT_TAG);
2875 lwkt_serialize_enter(dst_ifp->if_serializer);
2877 /* We may be sending a fragment so traverse the mbuf */
2879 struct altq_pktattr pktattr;
2882 m->m_nextpkt = NULL;
2884 if (ifq_is_enabled(&dst_ifp->if_snd))
2885 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
2887 ifq_handoff(dst_ifp, m, &pktattr);
2890 lwkt_serialize_exit(dst_ifp->if_serializer);