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.10 2006/09/05 03:48:12 dillon 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>
115 #include <netinet/in.h> /* for struct arpcom */
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_var.h>
121 #include <netinet/ip6.h>
122 #include <netinet6/ip6_var.h>
124 #include <netinet/if_ether.h> /* for struct arpcom */
125 #include <net/bridge/if_bridgevar.h>
126 #include <net/if_llc.h>
128 #include <net/route.h>
129 #include <sys/in_cksum.h>
132 * Size of the route hash table. Must be a power of two.
134 #ifndef BRIDGE_RTHASH_SIZE
135 #define BRIDGE_RTHASH_SIZE 1024
138 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
141 * Maximum number of addresses to cache.
143 #ifndef BRIDGE_RTABLE_MAX
144 #define BRIDGE_RTABLE_MAX 100
148 * Spanning tree defaults.
150 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
151 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
152 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
153 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
154 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
155 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
156 #define BSTP_DEFAULT_PATH_COST 55
159 * Timeout (in seconds) for entries learned dynamically.
161 #ifndef BRIDGE_RTABLE_TIMEOUT
162 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
166 * Number of seconds between walks of the route list.
168 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
169 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
173 * List of capabilities to mask on the member interface.
175 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
177 eventhandler_tag bridge_detach_cookie = NULL;
179 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
180 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *,
181 struct sockaddr *, struct rtentry *);
182 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
184 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
186 static int bridge_clone_create(struct if_clone *, int);
187 static void bridge_clone_destroy(struct ifnet *);
189 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
190 static void bridge_mutecaps(struct bridge_iflist *, int);
191 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
192 static void bridge_init(void *);
193 static void bridge_stop(struct ifnet *, int);
194 static void bridge_start(struct ifnet *);
195 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
196 static int bridge_output_serialized(struct ifnet *, struct mbuf *,
197 struct sockaddr *, struct rtentry *);
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 *);
269 SYSCTL_DECL(_net_link);
270 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
272 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
273 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
274 static int pfil_member = 1; /* run pfil hooks on the member interface */
275 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
276 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
277 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
278 &pfil_bridge, 0, "Packet filter on the bridge interface");
279 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
280 &pfil_member, 0, "Packet filter on the member interface");
282 struct bridge_control {
283 int (*bc_func)(struct bridge_softc *, void *);
288 #define BC_F_COPYIN 0x01 /* copy arguments in */
289 #define BC_F_COPYOUT 0x02 /* copy arguments out */
290 #define BC_F_SUSER 0x04 /* do super-user check */
292 const struct bridge_control bridge_control_table[] = {
293 { bridge_ioctl_add, sizeof(struct ifbreq),
294 BC_F_COPYIN|BC_F_SUSER },
295 { bridge_ioctl_del, sizeof(struct ifbreq),
296 BC_F_COPYIN|BC_F_SUSER },
298 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
299 BC_F_COPYIN|BC_F_COPYOUT },
300 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
301 BC_F_COPYIN|BC_F_SUSER },
303 { bridge_ioctl_scache, sizeof(struct ifbrparam),
304 BC_F_COPYIN|BC_F_SUSER },
305 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
308 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
309 BC_F_COPYIN|BC_F_COPYOUT },
310 { bridge_ioctl_rts, sizeof(struct ifbaconf),
311 BC_F_COPYIN|BC_F_COPYOUT },
313 { bridge_ioctl_saddr, sizeof(struct ifbareq),
314 BC_F_COPYIN|BC_F_SUSER },
316 { bridge_ioctl_sto, sizeof(struct ifbrparam),
317 BC_F_COPYIN|BC_F_SUSER },
318 { bridge_ioctl_gto, sizeof(struct ifbrparam),
321 { bridge_ioctl_daddr, sizeof(struct ifbareq),
322 BC_F_COPYIN|BC_F_SUSER },
324 { bridge_ioctl_flush, sizeof(struct ifbreq),
325 BC_F_COPYIN|BC_F_SUSER },
327 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
329 { bridge_ioctl_spri, sizeof(struct ifbrparam),
330 BC_F_COPYIN|BC_F_SUSER },
332 { bridge_ioctl_ght, sizeof(struct ifbrparam),
334 { bridge_ioctl_sht, sizeof(struct ifbrparam),
335 BC_F_COPYIN|BC_F_SUSER },
337 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
339 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
340 BC_F_COPYIN|BC_F_SUSER },
342 { bridge_ioctl_gma, sizeof(struct ifbrparam),
344 { bridge_ioctl_sma, sizeof(struct ifbrparam),
345 BC_F_COPYIN|BC_F_SUSER },
347 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
348 BC_F_COPYIN|BC_F_SUSER },
350 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
351 BC_F_COPYIN|BC_F_SUSER },
353 { bridge_ioctl_addspan, sizeof(struct ifbreq),
354 BC_F_COPYIN|BC_F_SUSER },
355 { bridge_ioctl_delspan, sizeof(struct ifbreq),
356 BC_F_COPYIN|BC_F_SUSER },
358 const int bridge_control_table_size =
359 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
361 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
362 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
364 LIST_HEAD(, bridge_softc) bridge_list;
366 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
368 bridge_clone_destroy, 0, IF_MAXUNIT);
371 bridge_modevent(module_t mod, int type, void *data)
376 LIST_INIT(&bridge_list);
377 if_clone_attach(&bridge_cloner);
378 bridge_input_p = bridge_input;
379 bridge_output_p = bridge_output_serialized;
380 bridge_detach_cookie = EVENTHANDLER_REGISTER(
381 ifnet_detach_event, bridge_ifdetach, NULL,
382 EVENTHANDLER_PRI_ANY);
384 bstp_linkstate_p = bstp_linkstate;
388 if (!LIST_EMPTY(&bridge_list))
390 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
391 bridge_detach_cookie);
392 if_clone_detach(&bridge_cloner);
393 bridge_input_p = NULL;
394 bridge_output_p = NULL;
396 bstp_linkstate_p = NULL;
405 static moduledata_t bridge_mod = {
411 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
415 * bridge_clone_create:
417 * Create a new bridge instance.
420 bridge_clone_create(struct if_clone *ifc, int unit)
422 struct bridge_softc *sc;
426 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
427 ifp = sc->sc_ifp = &sc->sc_if;
429 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
430 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
431 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
432 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
433 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
434 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
435 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
437 /* Initialize our routing table. */
438 bridge_rtable_init(sc);
440 callout_init(&sc->sc_brcallout);
441 callout_init(&sc->sc_bstpcallout);
443 LIST_INIT(&sc->sc_iflist);
444 LIST_INIT(&sc->sc_spanlist);
447 if_initname(ifp, ifc->ifc_name, unit);
448 ifp->if_mtu = ETHERMTU;
449 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
450 ifp->if_ioctl = bridge_ioctl;
451 ifp->if_start = bridge_start;
452 ifp->if_init = bridge_init;
453 ifp->if_type = IFT_BRIDGE;
454 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
455 ifp->if_snd.ifq_maxlen = ifqmaxlen;
456 ifq_set_ready(&ifp->if_snd);
457 ifp->if_hdrlen = ETHER_HDR_LEN;
460 * Generate a random ethernet address and use the private AC:DE:48
464 int rnd = karc4random();
465 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
467 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
469 eaddr[0] &= ~1; /* clear multicast bit */
470 eaddr[0] |= 2; /* set the LAA bit */
472 ether_ifattach(ifp, eaddr, NULL);
473 /* Now undo some of the damage... */
474 ifp->if_baudrate = 0;
475 ifp->if_type = IFT_BRIDGE;
478 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
485 * bridge_clone_destroy:
487 * Destroy a bridge instance.
490 bridge_clone_destroy(struct ifnet *ifp)
492 struct bridge_softc *sc = ifp->if_softc;
493 struct bridge_iflist *bif;
495 lwkt_serialize_enter(ifp->if_serializer);
498 ifp->if_flags &= ~IFF_UP;
500 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
501 bridge_delete_member(sc, bif, 0);
503 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
504 bridge_delete_span(sc, bif);
507 callout_stop(&sc->sc_brcallout);
508 callout_stop(&sc->sc_bstpcallout);
510 lwkt_serialize_exit(ifp->if_serializer);
513 LIST_REMOVE(sc, sc_list);
518 /* Tear down the routing table. */
519 bridge_rtable_fini(sc);
527 * Handle a control request from the operator.
530 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
532 struct bridge_softc *sc = ifp->if_softc;
533 struct thread *td = curthread;
535 struct ifbreq ifbreq;
536 struct ifbifconf ifbifconf;
537 struct ifbareq ifbareq;
538 struct ifbaconf ifbaconf;
539 struct ifbrparam ifbrparam;
541 struct ifdrv *ifd = (struct ifdrv *) data;
542 const struct bridge_control *bc;
553 if (ifd->ifd_cmd >= bridge_control_table_size) {
557 bc = &bridge_control_table[ifd->ifd_cmd];
559 if (cmd == SIOCGDRVSPEC &&
560 (bc->bc_flags & BC_F_COPYOUT) == 0) {
564 else if (cmd == SIOCSDRVSPEC &&
565 (bc->bc_flags & BC_F_COPYOUT) != 0) {
570 if (bc->bc_flags & BC_F_SUSER) {
576 if (ifd->ifd_len != bc->bc_argsize ||
577 ifd->ifd_len > sizeof(args)) {
582 memset(&args, 0, sizeof(args));
583 if (bc->bc_flags & BC_F_COPYIN) {
584 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
589 error = (*bc->bc_func)(sc, &args);
593 if (bc->bc_flags & BC_F_COPYOUT)
594 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
599 if (!(ifp->if_flags & IFF_UP) &&
600 (ifp->if_flags & IFF_RUNNING)) {
602 * If interface is marked down and it is running,
603 * then stop and disable it.
606 } else if ((ifp->if_flags & IFF_UP) &&
607 !(ifp->if_flags & IFF_RUNNING)) {
609 * If interface is marked up and it is stopped, then
617 /* Do not allow the MTU to be changed on the bridge */
623 * drop the lock as ether_ioctl() will call bridge_start() and
624 * cause the lock to be recursed.
626 error = ether_ioctl(ifp, cmd, data);
636 * Clear or restore unwanted capabilities on the member interface
639 bridge_mutecaps(struct bridge_iflist *bif, int mute)
641 struct ifnet *ifp = bif->bif_ifp;
645 if (ifp->if_ioctl == NULL)
648 bzero(&ifr, sizeof(ifr));
649 ifr.ifr_reqcap = ifp->if_capenable;
652 /* mask off and save capabilities */
653 bif->bif_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
654 if (bif->bif_mutecap != 0)
655 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
657 /* restore muted capabilities */
658 ifr.ifr_reqcap |= bif->bif_mutecap;
660 if (bif->bif_mutecap != 0) {
661 lwkt_serialize_enter(ifp->if_serializer);
662 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
663 lwkt_serialize_exit(ifp->if_serializer);
668 * bridge_lookup_member:
670 * Lookup a bridge member interface.
672 static struct bridge_iflist *
673 bridge_lookup_member(struct bridge_softc *sc, const char *name)
675 struct bridge_iflist *bif;
678 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
680 if (strcmp(ifp->if_xname, name) == 0)
688 * bridge_lookup_member_if:
690 * Lookup a bridge member interface by ifnet*.
692 static struct bridge_iflist *
693 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
695 struct bridge_iflist *bif;
697 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
698 if (bif->bif_ifp == member_ifp)
706 * bridge_delete_member:
708 * Delete the specified member interface.
711 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
714 struct ifnet *ifs = bif->bif_ifp;
717 switch (ifs->if_type) {
721 * Take the interface out of promiscuous mode.
723 (void) ifpromisc(ifs, 0);
724 bridge_mutecaps(bif, 0);
732 panic("bridge_delete_member: impossible");
738 ifs->if_bridge = NULL;
739 LIST_REMOVE(bif, bif_next);
741 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
743 kfree(bif, M_DEVBUF);
745 if (sc->sc_ifp->if_flags & IFF_RUNNING)
746 bstp_initialization(sc);
750 * bridge_delete_span:
752 * Delete the specified span interface.
755 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
757 KASSERT(bif->bif_ifp->if_bridge == NULL,
758 ("%s: not a span interface", __func__));
760 LIST_REMOVE(bif, bif_next);
761 kfree(bif, M_DEVBUF);
765 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
767 struct ifbreq *req = arg;
768 struct bridge_iflist *bif = NULL;
772 ifs = ifunit(req->ifbr_ifsname);
776 /* If it's in the span list, it can't be a member. */
777 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
778 if (ifs == bif->bif_ifp)
781 /* Allow the first Ethernet member to define the MTU */
782 if (ifs->if_type != IFT_GIF) {
783 if (LIST_EMPTY(&sc->sc_iflist))
784 sc->sc_ifp->if_mtu = ifs->if_mtu;
785 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
786 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
792 if (ifs->if_bridge == sc)
795 if (ifs->if_bridge != NULL)
798 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_RNOWAIT|M_ZERO);
803 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
804 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
805 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
807 switch (ifs->if_type) {
811 * Place the interface into promiscuous mode.
813 error = ifpromisc(ifs, 1);
817 bridge_mutecaps(bif, 1);
820 case IFT_GIF: /* :^) */
830 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
832 if (sc->sc_ifp->if_flags & IFF_RUNNING)
833 bstp_initialization(sc);
840 kfree(bif, M_DEVBUF);
846 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
848 struct ifbreq *req = arg;
849 struct bridge_iflist *bif;
851 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
855 bridge_delete_member(sc, bif, 0);
861 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
863 struct ifbreq *req = arg;
864 struct bridge_iflist *bif;
866 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
870 req->ifbr_ifsflags = bif->bif_flags;
871 req->ifbr_state = bif->bif_state;
872 req->ifbr_priority = bif->bif_priority;
873 req->ifbr_path_cost = bif->bif_path_cost;
874 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
880 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
882 struct ifbreq *req = arg;
883 struct bridge_iflist *bif;
885 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
889 if (req->ifbr_ifsflags & IFBIF_SPAN)
890 /* SPAN is readonly */
893 if (req->ifbr_ifsflags & IFBIF_STP) {
894 switch (bif->bif_ifp->if_type) {
896 /* These can do spanning tree. */
900 /* Nothing else can. */
905 bif->bif_flags = req->ifbr_ifsflags;
907 if (sc->sc_ifp->if_flags & IFF_RUNNING)
908 bstp_initialization(sc);
914 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
916 struct ifbrparam *param = arg;
918 sc->sc_brtmax = param->ifbrp_csize;
925 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
927 struct ifbrparam *param = arg;
929 param->ifbrp_csize = sc->sc_brtmax;
935 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
937 struct ifbifconf *bifc = arg;
938 struct bridge_iflist *bif;
940 int count, len, error = 0;
943 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
945 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
948 if (bifc->ifbic_len == 0) {
949 bifc->ifbic_len = sizeof(breq) * count;
954 len = bifc->ifbic_len;
955 memset(&breq, 0, sizeof breq);
956 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
957 if (len < sizeof(breq))
960 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
961 sizeof(breq.ifbr_ifsname));
962 breq.ifbr_ifsflags = bif->bif_flags;
963 breq.ifbr_state = bif->bif_state;
964 breq.ifbr_priority = bif->bif_priority;
965 breq.ifbr_path_cost = bif->bif_path_cost;
966 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
967 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
973 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
974 if (len < sizeof(breq))
977 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
978 sizeof(breq.ifbr_ifsname));
979 breq.ifbr_ifsflags = bif->bif_flags;
980 breq.ifbr_state = bif->bif_state;
981 breq.ifbr_priority = bif->bif_priority;
982 breq.ifbr_path_cost = bif->bif_path_cost;
983 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
984 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
991 bifc->ifbic_len = sizeof(breq) * count;
996 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
998 struct ifbaconf *bac = arg;
999 struct bridge_rtnode *brt;
1000 struct ifbareq bareq;
1001 int count = 0, error = 0, len;
1003 if (bac->ifbac_len == 0)
1006 len = bac->ifbac_len;
1007 memset(&bareq, 0, sizeof(bareq));
1008 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1009 if (len < sizeof(bareq))
1011 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1012 sizeof(bareq.ifba_ifsname));
1013 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1014 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1015 time_second < brt->brt_expire)
1016 bareq.ifba_expire = brt->brt_expire - time_second;
1018 bareq.ifba_expire = 0;
1019 bareq.ifba_flags = brt->brt_flags;
1021 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1025 len -= sizeof(bareq);
1028 bac->ifbac_len = sizeof(bareq) * count;
1033 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1035 struct ifbareq *req = arg;
1036 struct bridge_iflist *bif;
1039 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1043 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1050 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1052 struct ifbrparam *param = arg;
1054 sc->sc_brttimeout = param->ifbrp_ctime;
1060 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1062 struct ifbrparam *param = arg;
1064 param->ifbrp_ctime = sc->sc_brttimeout;
1070 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1072 struct ifbareq *req = arg;
1074 return (bridge_rtdaddr(sc, req->ifba_dst));
1078 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1080 struct ifbreq *req = arg;
1082 bridge_rtflush(sc, req->ifbr_ifsflags);
1088 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1090 struct ifbrparam *param = arg;
1092 param->ifbrp_prio = sc->sc_bridge_priority;
1098 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1100 struct ifbrparam *param = arg;
1102 sc->sc_bridge_priority = param->ifbrp_prio;
1104 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1105 bstp_initialization(sc);
1111 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1113 struct ifbrparam *param = arg;
1115 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1121 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1123 struct ifbrparam *param = arg;
1125 if (param->ifbrp_hellotime == 0)
1127 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1129 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1130 bstp_initialization(sc);
1136 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1138 struct ifbrparam *param = arg;
1140 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1146 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1148 struct ifbrparam *param = arg;
1150 if (param->ifbrp_fwddelay == 0)
1152 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1154 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1155 bstp_initialization(sc);
1161 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1163 struct ifbrparam *param = arg;
1165 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1171 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1173 struct ifbrparam *param = arg;
1175 if (param->ifbrp_maxage == 0)
1177 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1179 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1180 bstp_initialization(sc);
1186 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1188 struct ifbreq *req = arg;
1189 struct bridge_iflist *bif;
1191 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1195 bif->bif_priority = req->ifbr_priority;
1197 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1198 bstp_initialization(sc);
1204 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1206 struct ifbreq *req = arg;
1207 struct bridge_iflist *bif;
1209 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1213 bif->bif_path_cost = req->ifbr_path_cost;
1215 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1216 bstp_initialization(sc);
1222 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1224 struct ifbreq *req = arg;
1225 struct bridge_iflist *bif = NULL;
1228 ifs = ifunit(req->ifbr_ifsname);
1232 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1233 if (ifs == bif->bif_ifp)
1236 if (ifs->if_bridge != NULL)
1239 switch (ifs->if_type) {
1248 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_RNOWAIT|M_ZERO);
1253 bif->bif_flags = IFBIF_SPAN;
1255 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1261 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1263 struct ifbreq *req = arg;
1264 struct bridge_iflist *bif;
1267 ifs = ifunit(req->ifbr_ifsname);
1271 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1272 if (ifs == bif->bif_ifp)
1278 bridge_delete_span(sc, bif);
1286 * Detach an interface from a bridge. Called when a member
1287 * interface is detaching.
1290 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1292 struct bridge_softc *sc = ifp->if_bridge;
1293 struct bridge_iflist *bif;
1295 /* Check if the interface is a bridge member */
1297 lwkt_serialize_enter(ifp->if_serializer);
1299 bif = bridge_lookup_member_if(sc, ifp);
1301 bridge_delete_member(sc, bif, 1);
1303 lwkt_serialize_exit(ifp->if_serializer);
1307 /* Check if the interface is a span port */
1308 LIST_FOREACH(sc, &bridge_list, sc_list) {
1309 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1310 if (ifp == bif->bif_ifp) {
1311 bridge_delete_span(sc, bif);
1320 * Initialize a bridge interface.
1323 bridge_init(void *xsc)
1325 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1326 struct ifnet *ifp = sc->sc_ifp;
1328 if (ifp->if_flags & IFF_RUNNING)
1331 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1334 ifp->if_flags |= IFF_RUNNING;
1335 bstp_initialization(sc);
1342 * Stop the bridge interface.
1345 bridge_stop(struct ifnet *ifp, int disable)
1347 struct bridge_softc *sc = ifp->if_softc;
1349 ASSERT_SERIALIZED(ifp->if_serializer);
1351 if ((ifp->if_flags & IFF_RUNNING) == 0)
1354 callout_stop(&sc->sc_brcallout);
1357 bridge_rtflush(sc, IFBF_FLUSHDYN);
1359 ifp->if_flags &= ~IFF_RUNNING;
1365 * Enqueue a packet on a bridge member interface.
1369 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1371 struct altq_pktattr pktattr;
1374 while (m->m_type == MT_TAG) {
1375 /* XXX see ether_output_frame for full rules check */
1379 lwkt_serialize_enter(dst_ifp->if_serializer);
1381 /* We may be sending a fragment so traverse the mbuf */
1384 m->m_nextpkt = NULL;
1386 if (ifq_is_enabled(&dst_ifp->if_snd))
1387 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
1389 ifq_handoff(dst_ifp, m, &pktattr);
1392 lwkt_serialize_exit(dst_ifp->if_serializer);
1396 * bridge_output_serialized:
1398 * Send output from a bridge member interface. This
1399 * performs the bridging function for locally originated
1402 * The mbuf has the Ethernet header already attached. We must
1403 * enqueue or free the mbuf before returning.
1406 bridge_output_serialized(struct ifnet *ifp, struct mbuf *m,
1407 struct sockaddr *sa, struct rtentry *rt)
1409 struct ether_header *eh;
1410 struct ifnet *dst_if;
1411 struct bridge_softc *sc;
1413 sc = ifp->if_bridge;
1415 ASSERT_SERIALIZED(ifp->if_serializer);
1417 if (m->m_len < ETHER_HDR_LEN) {
1418 m = m_pullup(m, ETHER_HDR_LEN);
1424 * Serialize our bridge interface. We have to get rid of the
1425 * originating interface lock to avoid a deadlock.
1427 lwkt_serialize_exit(ifp->if_serializer);
1428 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1430 eh = mtod(m, struct ether_header *);
1433 * If bridge is down, but the original output interface is up,
1434 * go ahead and send out that interface. Otherwise, the packet
1437 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
1443 * If the packet is a multicast, or we don't know a better way to
1444 * get there, send to all interfaces.
1446 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1449 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1450 if (dst_if == NULL) {
1451 struct bridge_iflist *bif;
1457 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1458 dst_if = bif->bif_ifp;
1459 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1463 * If this is not the original output interface,
1464 * and the interface is participating in spanning
1465 * tree, make sure the port is in a state that
1466 * allows forwarding.
1468 if (dst_if != ifp &&
1469 (bif->bif_flags & IFBIF_STP) != 0) {
1470 switch (bif->bif_state) {
1471 case BSTP_IFSTATE_BLOCKING:
1472 case BSTP_IFSTATE_LISTENING:
1473 case BSTP_IFSTATE_DISABLED:
1478 if (LIST_NEXT(bif, bif_next) == NULL) {
1482 mc = m_copypacket(m, MB_DONTWAIT);
1484 sc->sc_ifp->if_oerrors++;
1488 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1489 bridge_enqueue(sc, dst_if, mc);
1490 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1494 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1500 * XXX Spanning tree consideration here?
1504 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1505 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1508 bridge_enqueue(sc, dst_if, m);
1511 lwkt_serialize_enter(ifp->if_serializer);
1518 * Start output on a bridge.
1522 bridge_start(struct ifnet *ifp)
1524 struct bridge_softc *sc;
1526 struct ether_header *eh;
1527 struct ifnet *dst_if;
1531 ifp->if_flags |= IFF_OACTIVE;
1533 m = ifq_dequeue(&ifp->if_snd, NULL);
1539 eh = mtod(m, struct ether_header *);
1542 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1543 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1547 bridge_broadcast(sc, ifp, m, 0);
1549 bridge_enqueue(sc, dst_if, m);
1551 ifp->if_flags &= ~IFF_OACTIVE;
1559 * The forwarding function of the bridge.
1562 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1564 struct bridge_iflist *bif;
1565 struct ifnet *src_if, *dst_if, *ifp;
1566 struct ether_header *eh;
1568 src_if = m->m_pkthdr.rcvif;
1571 ASSERT_SERIALIZED(ifp->if_serializer);
1573 sc->sc_ifp->if_ipackets++;
1574 sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
1577 * Look up the bridge_iflist.
1579 bif = bridge_lookup_member_if(sc, src_if);
1581 /* Interface is not a bridge member (anymore?) */
1586 if (bif->bif_flags & IFBIF_STP) {
1587 switch (bif->bif_state) {
1588 case BSTP_IFSTATE_BLOCKING:
1589 case BSTP_IFSTATE_LISTENING:
1590 case BSTP_IFSTATE_DISABLED:
1596 eh = mtod(m, struct ether_header *);
1599 * Various ifp's are used below, release the serializer for
1600 * the bridge ifp so other ifp serializers can be acquired.
1602 lwkt_serialize_exit(ifp->if_serializer);
1605 * If the interface is learning, and the source
1606 * address is valid and not multicast, record
1609 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1610 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1611 (eh->ether_shost[0] == 0 &&
1612 eh->ether_shost[1] == 0 &&
1613 eh->ether_shost[2] == 0 &&
1614 eh->ether_shost[3] == 0 &&
1615 eh->ether_shost[4] == 0 &&
1616 eh->ether_shost[5] == 0) == 0) {
1617 bridge_rtupdate(sc, eh->ether_shost, src_if, 0, IFBAF_DYNAMIC);
1620 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1621 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1627 * At this point, the port either doesn't participate
1628 * in spanning tree or it is in the forwarding state.
1632 * If the packet is unicast, destined for someone on
1633 * "this" side of the bridge, drop it.
1635 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1636 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1637 if (src_if == dst_if) {
1642 /* ...forward it to all interfaces. */
1643 sc->sc_ifp->if_imcasts++;
1647 /* run the packet filter */
1648 if (inet_pfil_hook.ph_hashooks > 0
1650 || inet6_pfil_hook.ph_hashooks > 0
1653 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1659 if (dst_if == NULL) {
1660 bridge_broadcast(sc, src_if, m, 1);
1665 * At this point, we're dealing with a unicast frame
1666 * going to a different interface.
1668 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1672 bif = bridge_lookup_member_if(sc, dst_if);
1674 /* Not a member of the bridge (anymore?) */
1679 if (bif->bif_flags & IFBIF_STP) {
1680 switch (bif->bif_state) {
1681 case BSTP_IFSTATE_DISABLED:
1682 case BSTP_IFSTATE_BLOCKING:
1688 if (inet_pfil_hook.ph_hashooks > 0
1690 || inet6_pfil_hook.ph_hashooks > 0
1693 if (bridge_pfil(&m, sc->sc_ifp, dst_if, PFIL_OUT) != 0)
1698 bridge_enqueue(sc, dst_if, m);
1701 * ifp's serializer was held on entry and is expected to be held
1705 lwkt_serialize_enter(ifp->if_serializer);
1711 * Receive input from a member interface. Queue the packet for
1712 * bridging if it is not for us.
1715 bridge_input(struct ifnet *ifp, struct mbuf *m)
1717 struct bridge_softc *sc = ifp->if_bridge;
1718 struct bridge_iflist *bif;
1720 struct ether_header *eh;
1721 struct mbuf *mc, *mc2;
1724 lwkt_serialize_enter(bifp->if_serializer);
1726 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0)
1729 bif = bridge_lookup_member_if(sc, ifp);
1733 eh = mtod(m, struct ether_header *);
1735 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
1738 * Tap all packets arriving on the bridge, no matter if
1739 * they are local destinations or not. In is in.
1743 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
1744 #define IFP2ENADDR(ifp) (IFP2AC(ifp)->ac_enaddr)
1745 if (memcmp(eh->ether_dhost, IFP2ENADDR(bifp),
1746 ETHER_ADDR_LEN) == 0) {
1748 * If the packet is for us, set the packets source as the
1749 * bridge, and return the packet back to ether_input for
1753 /* Mark the packet as arriving on the bridge interface */
1754 m->m_pkthdr.rcvif = bifp;
1755 bifp->if_ipackets++;
1762 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1763 /* Tap off 802.1D packets; they do not get forwarded. */
1764 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1765 ETHER_ADDR_LEN) == 0) {
1766 m = bstp_input(ifp, m);
1771 if (bif->bif_flags & IFBIF_STP) {
1772 switch (bif->bif_state) {
1773 case BSTP_IFSTATE_BLOCKING:
1774 case BSTP_IFSTATE_LISTENING:
1775 case BSTP_IFSTATE_DISABLED:
1780 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
1781 sizeof(etherbroadcastaddr)) == 0)
1782 m->m_flags |= M_BCAST;
1784 m->m_flags |= M_MCAST;
1787 * Make a deep copy of the packet and enqueue the copy
1788 * for bridge processing; return the original packet for
1791 mc = m_dup(m, MB_DONTWAIT);
1795 bridge_forward(sc, mc);
1798 * Reinject the mbuf as arriving on the bridge so we have a
1799 * chance at claiming multicast packets. We can not loop back
1800 * here from ether_input as a bridge is never a member of a
1803 KASSERT(bifp->if_bridge == NULL,
1804 ("loop created in bridge_input"));
1805 mc2 = m_dup(m, MB_DONTWAIT);
1808 /* Keep the layer3 header aligned */
1809 int i = min(mc2->m_pkthdr.len, max_protohdr);
1810 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
1814 mc2->m_pkthdr.rcvif = bifp;
1815 (*bifp->if_input)(bifp, mc2);
1818 /* Return the original packet for local processing. */
1822 if (bif->bif_flags & IFBIF_STP) {
1823 switch (bif->bif_state) {
1824 case BSTP_IFSTATE_BLOCKING:
1825 case BSTP_IFSTATE_LISTENING:
1826 case BSTP_IFSTATE_DISABLED:
1832 * Unicast. Make sure it's not for us.
1834 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1835 if (bif->bif_ifp->if_type != IFT_ETHER)
1837 /* It is destined for us. */
1838 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1839 ETHER_ADDR_LEN) == 0) {
1840 if (bif->bif_flags & IFBIF_LEARNING)
1842 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1843 m->m_pkthdr.rcvif = bif->bif_ifp;
1844 if (ifp->if_type == IFT_GIF) {
1845 m->m_flags |= M_PROTO1;
1847 * Avoid an interface ordering deadlock.
1849 lwkt_serialize_exit(bifp->if_serializer);
1850 lwkt_serialize_enter(bif->bif_ifp->if_serializer);
1851 (*bif->bif_ifp->if_input)(bif->bif_ifp, m);
1852 lwkt_serialize_exit(bif->bif_ifp->if_serializer);
1853 lwkt_serialize_enter(bifp->if_serializer);
1859 /* We just received a packet that we sent out. */
1860 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1861 ETHER_ADDR_LEN) == 0) {
1868 /* Perform the bridge forwarding function. */
1869 bridge_forward(sc, m);
1873 lwkt_serialize_exit(bifp->if_serializer);
1880 * Send a frame to all interfaces that are members of
1881 * the bridge, except for the one on which the packet
1885 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1886 struct mbuf *m, int runfilt)
1888 struct bridge_iflist *bif;
1890 struct ifnet *dst_if;
1893 /* Filter on the bridge interface before broadcasting */
1894 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1896 || inet6_pfil_hook.ph_hashooks > 0
1899 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
1905 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1906 dst_if = bif->bif_ifp;
1907 if (dst_if == src_if)
1910 if (bif->bif_flags & IFBIF_STP) {
1911 switch (bif->bif_state) {
1912 case BSTP_IFSTATE_BLOCKING:
1913 case BSTP_IFSTATE_DISABLED:
1918 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1919 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1922 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1925 if (LIST_NEXT(bif, bif_next) == NULL) {
1929 mc = m_copypacket(m, MB_DONTWAIT);
1931 sc->sc_ifp->if_oerrors++;
1937 * Filter on the output interface. Pass a NULL bridge interface
1938 * pointer so we do not redundantly filter on the bridge for
1939 * each interface we broadcast on.
1941 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1943 || inet6_pfil_hook.ph_hashooks > 0
1946 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
1952 bridge_enqueue(sc, dst_if, mc);
1961 * Duplicate a packet out one or more interfaces that are in span mode,
1962 * the original mbuf is unmodified.
1965 bridge_span(struct bridge_softc *sc, struct mbuf *m)
1967 struct bridge_iflist *bif;
1968 struct ifnet *dst_if;
1971 if (LIST_EMPTY(&sc->sc_spanlist))
1974 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1975 dst_if = bif->bif_ifp;
1977 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1980 mc = m_copypacket(m, MB_DONTWAIT);
1982 sc->sc_ifp->if_oerrors++;
1986 bridge_enqueue(sc, dst_if, mc);
1993 * Add a bridge routing entry.
1996 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
1997 struct ifnet *dst_if, int setflags, uint8_t flags)
1999 struct bridge_rtnode *brt;
2003 * A route for this destination might already exist. If so,
2004 * update it, otherwise create a new one.
2006 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2007 if (sc->sc_brtcnt >= sc->sc_brtmax)
2011 * Allocate a new bridge forwarding node, and
2012 * initialize the expiration time and Ethernet
2015 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF, M_RNOWAIT|M_ZERO);
2019 brt->brt_flags = IFBAF_DYNAMIC;
2020 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2022 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2023 kfree(brt, M_DEVBUF);
2028 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2029 brt->brt_ifp = dst_if;
2030 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2031 brt->brt_expire = time_second + sc->sc_brttimeout;
2033 brt->brt_flags = flags;
2041 * Lookup the destination interface for an address.
2043 static struct ifnet *
2044 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2046 struct bridge_rtnode *brt;
2048 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2051 return (brt->brt_ifp);
2057 * Trim the routine table so that we have a number
2058 * of routing entries less than or equal to the
2062 bridge_rttrim(struct bridge_softc *sc)
2064 struct bridge_rtnode *brt, *nbrt;
2066 /* Make sure we actually need to do this. */
2067 if (sc->sc_brtcnt <= sc->sc_brtmax)
2070 /* Force an aging cycle; this might trim enough addresses. */
2072 if (sc->sc_brtcnt <= sc->sc_brtmax)
2075 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2076 nbrt = LIST_NEXT(brt, brt_list);
2077 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2078 bridge_rtnode_destroy(sc, brt);
2079 if (sc->sc_brtcnt <= sc->sc_brtmax)
2088 * Aging timer for the bridge.
2091 bridge_timer(void *arg)
2093 struct bridge_softc *sc = arg;
2095 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
2099 if (sc->sc_ifp->if_flags & IFF_RUNNING)
2100 callout_reset(&sc->sc_brcallout,
2101 bridge_rtable_prune_period * hz, bridge_timer, sc);
2103 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
2109 * Perform an aging cycle.
2112 bridge_rtage(struct bridge_softc *sc)
2114 struct bridge_rtnode *brt, *nbrt;
2116 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2117 nbrt = LIST_NEXT(brt, brt_list);
2118 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2119 if (time_second >= brt->brt_expire)
2120 bridge_rtnode_destroy(sc, brt);
2128 * Remove all dynamic addresses from the bridge.
2131 bridge_rtflush(struct bridge_softc *sc, int full)
2133 struct bridge_rtnode *brt, *nbrt;
2135 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2136 nbrt = LIST_NEXT(brt, brt_list);
2137 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2138 bridge_rtnode_destroy(sc, brt);
2145 * Remove an address from the table.
2148 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2150 struct bridge_rtnode *brt;
2152 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2155 bridge_rtnode_destroy(sc, brt);
2162 * Delete routes to a speicifc member interface.
2165 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2167 struct bridge_rtnode *brt, *nbrt;
2169 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2170 nbrt = LIST_NEXT(brt, brt_list);
2171 if (brt->brt_ifp == ifp && (full ||
2172 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2173 bridge_rtnode_destroy(sc, brt);
2178 * bridge_rtable_init:
2180 * Initialize the route table for this bridge.
2183 bridge_rtable_init(struct bridge_softc *sc)
2187 sc->sc_rthash = kmalloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2188 M_DEVBUF, M_WAITOK);
2190 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2191 LIST_INIT(&sc->sc_rthash[i]);
2193 sc->sc_rthash_key = karc4random();
2195 LIST_INIT(&sc->sc_rtlist);
2201 * bridge_rtable_fini:
2203 * Deconstruct the route table for this bridge.
2206 bridge_rtable_fini(struct bridge_softc *sc)
2209 kfree(sc->sc_rthash, M_DEVBUF);
2213 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2214 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2216 #define mix(a, b, c) \
2218 a -= b; a -= c; a ^= (c >> 13); \
2219 b -= c; b -= a; b ^= (a << 8); \
2220 c -= a; c -= b; c ^= (b >> 13); \
2221 a -= b; a -= c; a ^= (c >> 12); \
2222 b -= c; b -= a; b ^= (a << 16); \
2223 c -= a; c -= b; c ^= (b >> 5); \
2224 a -= b; a -= c; a ^= (c >> 3); \
2225 b -= c; b -= a; b ^= (a << 10); \
2226 c -= a; c -= b; c ^= (b >> 15); \
2227 } while (/*CONSTCOND*/0)
2229 static __inline uint32_t
2230 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2232 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2243 return (c & BRIDGE_RTHASH_MASK);
2249 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2253 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2254 d = ((int)a[i]) - ((int)b[i]);
2261 * bridge_rtnode_lookup:
2263 * Look up a bridge route node for the specified destination.
2265 static struct bridge_rtnode *
2266 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2268 struct bridge_rtnode *brt;
2272 hash = bridge_rthash(sc, addr);
2273 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2274 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2285 * bridge_rtnode_insert:
2287 * Insert the specified bridge node into the route table. We
2288 * assume the entry is not already in the table.
2291 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2293 struct bridge_rtnode *lbrt;
2297 hash = bridge_rthash(sc, brt->brt_addr);
2299 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2301 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2306 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2310 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2313 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2314 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2317 lbrt = LIST_NEXT(lbrt, brt_hash);
2318 } while (lbrt != NULL);
2321 panic("bridge_rtnode_insert: impossible");
2325 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2332 * bridge_rtnode_destroy:
2334 * Destroy a bridge rtnode.
2337 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2340 LIST_REMOVE(brt, brt_hash);
2342 LIST_REMOVE(brt, brt_list);
2344 kfree(brt, M_DEVBUF);
2348 * Send bridge packets through pfil if they are one of the types pfil can deal
2349 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2350 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2354 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2356 int snap, error, i, hlen;
2357 struct ether_header *eh1, eh2;
2360 u_int16_t ether_type;
2363 error = -1; /* Default error if not error == 0 */
2365 /* we may return with the IP fields swapped, ensure its not shared */
2366 KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
2368 if (pfil_bridge == 0 && pfil_member == 0)
2369 return (0); /* filtering is disabled */
2371 i = min((*mp)->m_pkthdr.len, max_protohdr);
2372 if ((*mp)->m_len < i) {
2373 *mp = m_pullup(*mp, i);
2375 printf("%s: m_pullup failed\n", __func__);
2380 eh1 = mtod(*mp, struct ether_header *);
2381 ether_type = ntohs(eh1->ether_type);
2384 * Check for SNAP/LLC.
2386 if (ether_type < ETHERMTU) {
2387 struct llc *llc2 = (struct llc *)(eh1 + 1);
2389 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2390 llc2->llc_dsap == LLC_SNAP_LSAP &&
2391 llc2->llc_ssap == LLC_SNAP_LSAP &&
2392 llc2->llc_control == LLC_UI) {
2393 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2399 * If we're trying to filter bridge traffic, don't look at anything
2400 * other than IP and ARP traffic. If the filter doesn't understand
2401 * IPv6, don't allow IPv6 through the bridge either. This is lame
2402 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2403 * but of course we don't have an AppleTalk filter to begin with.
2404 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2407 switch (ether_type) {
2409 case ETHERTYPE_REVARP:
2410 return (0); /* Automatically pass */
2413 case ETHERTYPE_IPV6:
2418 * Check to see if the user wants to pass non-ip
2419 * packets, these will not be checked by pfil(9) and
2420 * passed unconditionally so the default is to drop.
2426 /* Strip off the Ethernet header and keep a copy. */
2427 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2428 m_adj(*mp, ETHER_HDR_LEN);
2430 /* Strip off snap header, if present */
2432 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2433 m_adj(*mp, sizeof(struct llc));
2437 * Check the IP header for alignment and errors
2439 if (dir == PFIL_IN) {
2440 switch (ether_type) {
2442 error = bridge_ip_checkbasic(mp);
2445 case ETHERTYPE_IPV6:
2446 error = bridge_ip6_checkbasic(mp);
2459 * Run the packet through pfil
2465 * before calling the firewall, swap fields the same as
2466 * IP does. here we assume the header is contiguous
2468 ip = mtod(*mp, struct ip *);
2470 ip->ip_len = ntohs(ip->ip_len);
2471 ip->ip_off = ntohs(ip->ip_off);
2474 * Run pfil on the member interface and the bridge, both can
2475 * be skipped by clearing pfil_member or pfil_bridge.
2478 * in_if -> bridge_if -> out_if
2480 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2481 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2484 if (*mp == NULL || error != 0) /* filter may consume */
2487 if (pfil_member && ifp != NULL)
2488 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2491 if (*mp == NULL || error != 0) /* filter may consume */
2494 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2495 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2498 if (*mp == NULL || error != 0) /* filter may consume */
2501 /* check if we need to fragment the packet */
2502 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
2503 i = (*mp)->m_pkthdr.len;
2504 if (i > ifp->if_mtu) {
2505 error = bridge_fragment(ifp, *mp, &eh2, snap,
2511 /* Recalculate the ip checksum and restore byte ordering */
2512 ip = mtod(*mp, struct ip *);
2513 hlen = ip->ip_hl << 2;
2514 if (hlen < sizeof(struct ip))
2516 if (hlen > (*mp)->m_len) {
2517 if ((*mp = m_pullup(*mp, hlen)) == 0)
2519 ip = mtod(*mp, struct ip *);
2523 ip->ip_len = htons(ip->ip_len);
2524 ip->ip_off = htons(ip->ip_off);
2526 if (hlen == sizeof(struct ip))
2527 ip->ip_sum = in_cksum_hdr(ip);
2529 ip->ip_sum = in_cksum(*mp, hlen);
2533 case ETHERTYPE_IPV6 :
2534 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2535 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2538 if (*mp == NULL || error != 0) /* filter may consume */
2541 if (pfil_member && ifp != NULL)
2542 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2545 if (*mp == NULL || error != 0) /* filter may consume */
2548 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2549 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2566 * Finally, put everything back the way it was and return
2569 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
2572 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2575 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
2578 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2589 * Perform basic checks on header size since
2590 * pfil assumes ip_input has already processed
2591 * it for it. Cut-and-pasted from ip_input.c.
2592 * Given how simple the IPv6 version is,
2593 * does the IPv4 version really need to be
2596 * XXX Should we update ipstat here, or not?
2597 * XXX Right now we update ipstat but not
2601 bridge_ip_checkbasic(struct mbuf **mp)
2603 struct mbuf *m = *mp;
2611 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2612 if ((m = m_copyup(m, sizeof(struct ip),
2613 (max_linkhdr + 3) & ~3)) == NULL) {
2614 /* XXXJRT new stat, please */
2615 ipstat.ips_toosmall++;
2620 #ifndef __predict_false
2621 #define __predict_false(x) x
2623 if (__predict_false(m->m_len < sizeof (struct ip))) {
2624 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2625 ipstat.ips_toosmall++;
2629 ip = mtod(m, struct ip *);
2630 if (ip == NULL) goto bad;
2632 if (ip->ip_v != IPVERSION) {
2633 ipstat.ips_badvers++;
2636 hlen = ip->ip_hl << 2;
2637 if (hlen < sizeof(struct ip)) { /* minimum header length */
2638 ipstat.ips_badhlen++;
2641 if (hlen > m->m_len) {
2642 if ((m = m_pullup(m, hlen)) == 0) {
2643 ipstat.ips_badhlen++;
2646 ip = mtod(m, struct ip *);
2647 if (ip == NULL) goto bad;
2650 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2651 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2653 if (hlen == sizeof(struct ip)) {
2654 sum = in_cksum_hdr(ip);
2656 sum = in_cksum(m, hlen);
2660 ipstat.ips_badsum++;
2664 /* Retrieve the packet length. */
2665 len = ntohs(ip->ip_len);
2668 * Check for additional length bogosity
2671 ipstat.ips_badlen++;
2676 * Check that the amount of data in the buffers
2677 * is as at least much as the IP header would have us expect.
2678 * Drop packet if shorter than we expect.
2680 if (m->m_pkthdr.len < len) {
2681 ipstat.ips_tooshort++;
2685 /* Checks out, proceed */
2696 * Same as above, but for IPv6.
2697 * Cut-and-pasted from ip6_input.c.
2698 * XXX Should we update ip6stat, or not?
2701 bridge_ip6_checkbasic(struct mbuf **mp)
2703 struct mbuf *m = *mp;
2704 struct ip6_hdr *ip6;
2707 * If the IPv6 header is not aligned, slurp it up into a new
2708 * mbuf with space for link headers, in the event we forward
2709 * it. Otherwise, if it is aligned, make sure the entire base
2710 * IPv6 header is in the first mbuf of the chain.
2713 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2714 struct ifnet *inifp = m->m_pkthdr.rcvif;
2715 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2716 (max_linkhdr + 3) & ~3)) == NULL) {
2717 /* XXXJRT new stat, please */
2718 ip6stat.ip6s_toosmall++;
2719 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2724 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2725 struct ifnet *inifp = m->m_pkthdr.rcvif;
2726 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2727 ip6stat.ip6s_toosmall++;
2728 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2733 ip6 = mtod(m, struct ip6_hdr *);
2735 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2736 ip6stat.ip6s_badvers++;
2737 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2741 /* Checks out, proceed */
2754 * Return a fragmented mbuf chain.
2757 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
2758 int snap, struct llc *llc)
2764 if (m->m_len < sizeof(struct ip) &&
2765 (m = m_pullup(m, sizeof(struct ip))) == NULL)
2767 ip = mtod(m, struct ip *);
2769 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
2774 /* walk the chain and re-add the Ethernet header */
2775 for (m0 = m; m0; m0 = m0->m_nextpkt) {
2778 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
2783 bcopy(llc, mtod(m0, caddr_t),
2784 sizeof(struct llc));
2786 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
2791 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
2797 ipstat.ips_fragmented++;