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.37 2008/06/19 11:33:07 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 typedef int (*bridge_ctl_t)(struct bridge_softc *, void *);
187 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
189 static int bridge_clone_create(struct if_clone *, int);
190 static void bridge_clone_destroy(struct ifnet *);
192 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
193 static void bridge_mutecaps(struct bridge_iflist *, int);
194 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
195 static void bridge_init(void *);
196 static void bridge_stop(struct ifnet *);
197 static void bridge_start(struct ifnet *);
198 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
199 static int bridge_output(struct ifnet *, struct mbuf *);
201 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
203 static void bridge_timer(void *);
205 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
207 static void bridge_span(struct bridge_softc *, struct mbuf *);
209 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
210 struct ifnet *, int, uint8_t);
211 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
212 static void bridge_rttrim(struct bridge_softc *);
213 static void bridge_rtage(struct bridge_softc *);
214 static void bridge_rtflush(struct bridge_softc *, int);
215 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
217 static int bridge_rtable_init(struct bridge_softc *);
218 static void bridge_rtable_fini(struct bridge_softc *);
220 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
221 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
223 static int bridge_rtnode_insert(struct bridge_softc *,
224 struct bridge_rtnode *);
225 static void bridge_rtnode_destroy(struct bridge_softc *,
226 struct bridge_rtnode *);
228 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
230 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
232 static void bridge_delete_member(struct bridge_softc *,
233 struct bridge_iflist *, int);
234 static void bridge_delete_span(struct bridge_softc *,
235 struct bridge_iflist *);
237 static int bridge_control(struct bridge_softc *, u_long,
238 bridge_ctl_t, void *);
239 static int bridge_ioctl_add(struct bridge_softc *, void *);
240 static int bridge_ioctl_del(struct bridge_softc *, void *);
241 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
242 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
243 static int bridge_ioctl_scache(struct bridge_softc *, void *);
244 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
245 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
246 static int bridge_ioctl_rts(struct bridge_softc *, void *);
247 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
248 static int bridge_ioctl_sto(struct bridge_softc *, void *);
249 static int bridge_ioctl_gto(struct bridge_softc *, void *);
250 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
251 static int bridge_ioctl_flush(struct bridge_softc *, void *);
252 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
253 static int bridge_ioctl_spri(struct bridge_softc *, void *);
254 static int bridge_ioctl_ght(struct bridge_softc *, void *);
255 static int bridge_ioctl_sht(struct bridge_softc *, void *);
256 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
257 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
258 static int bridge_ioctl_gma(struct bridge_softc *, void *);
259 static int bridge_ioctl_sma(struct bridge_softc *, void *);
260 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
261 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
262 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
263 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
264 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
266 static int bridge_ip_checkbasic(struct mbuf **mp);
268 static int bridge_ip6_checkbasic(struct mbuf **mp);
270 static int bridge_fragment(struct ifnet *, struct mbuf *,
271 struct ether_header *, int, struct llc *);
272 static void bridge_enqueue_internal(struct ifnet *, struct mbuf *m,
274 static void bridge_enqueue_handler(struct netmsg *);
275 static void bridge_pfil_enqueue_handler(struct netmsg *);
276 static void bridge_pfil_enqueue(struct ifnet *, struct mbuf *, int);
277 static void bridge_handoff_notags(struct ifnet *, struct mbuf *);
278 static void bridge_handoff(struct ifnet *, struct mbuf *);
280 SYSCTL_DECL(_net_link);
281 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
283 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
284 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
285 static int pfil_member = 1; /* run pfil hooks on the member interface */
286 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
287 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
288 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
289 &pfil_bridge, 0, "Packet filter on the bridge interface");
290 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
291 &pfil_member, 0, "Packet filter on the member interface");
293 struct bridge_control {
294 bridge_ctl_t bc_func;
299 #define BC_F_COPYIN 0x01 /* copy arguments in */
300 #define BC_F_COPYOUT 0x02 /* copy arguments out */
301 #define BC_F_SUSER 0x04 /* do super-user check */
303 const struct bridge_control bridge_control_table[] = {
304 { bridge_ioctl_add, sizeof(struct ifbreq),
305 BC_F_COPYIN|BC_F_SUSER },
306 { bridge_ioctl_del, sizeof(struct ifbreq),
307 BC_F_COPYIN|BC_F_SUSER },
309 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
310 BC_F_COPYIN|BC_F_COPYOUT },
311 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
312 BC_F_COPYIN|BC_F_SUSER },
314 { bridge_ioctl_scache, sizeof(struct ifbrparam),
315 BC_F_COPYIN|BC_F_SUSER },
316 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
319 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
320 BC_F_COPYIN|BC_F_COPYOUT },
321 { bridge_ioctl_rts, sizeof(struct ifbaconf),
322 BC_F_COPYIN|BC_F_COPYOUT },
324 { bridge_ioctl_saddr, sizeof(struct ifbareq),
325 BC_F_COPYIN|BC_F_SUSER },
327 { bridge_ioctl_sto, sizeof(struct ifbrparam),
328 BC_F_COPYIN|BC_F_SUSER },
329 { bridge_ioctl_gto, sizeof(struct ifbrparam),
332 { bridge_ioctl_daddr, sizeof(struct ifbareq),
333 BC_F_COPYIN|BC_F_SUSER },
335 { bridge_ioctl_flush, sizeof(struct ifbreq),
336 BC_F_COPYIN|BC_F_SUSER },
338 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
340 { bridge_ioctl_spri, sizeof(struct ifbrparam),
341 BC_F_COPYIN|BC_F_SUSER },
343 { bridge_ioctl_ght, sizeof(struct ifbrparam),
345 { bridge_ioctl_sht, sizeof(struct ifbrparam),
346 BC_F_COPYIN|BC_F_SUSER },
348 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
350 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
351 BC_F_COPYIN|BC_F_SUSER },
353 { bridge_ioctl_gma, sizeof(struct ifbrparam),
355 { bridge_ioctl_sma, sizeof(struct ifbrparam),
356 BC_F_COPYIN|BC_F_SUSER },
358 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
359 BC_F_COPYIN|BC_F_SUSER },
361 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
362 BC_F_COPYIN|BC_F_SUSER },
364 { bridge_ioctl_addspan, sizeof(struct ifbreq),
365 BC_F_COPYIN|BC_F_SUSER },
366 { bridge_ioctl_delspan, sizeof(struct ifbreq),
367 BC_F_COPYIN|BC_F_SUSER },
369 const int bridge_control_table_size =
370 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
372 LIST_HEAD(, bridge_softc) bridge_list;
374 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
376 bridge_clone_destroy, 0, IF_MAXUNIT);
379 bridge_modevent(module_t mod, int type, void *data)
383 LIST_INIT(&bridge_list);
384 if_clone_attach(&bridge_cloner);
385 bridge_input_p = bridge_input;
386 bridge_output_p = bridge_output;
387 bridge_detach_cookie = EVENTHANDLER_REGISTER(
388 ifnet_detach_event, bridge_ifdetach, NULL,
389 EVENTHANDLER_PRI_ANY);
391 bstp_linkstate_p = bstp_linkstate;
395 if (!LIST_EMPTY(&bridge_list))
397 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
398 bridge_detach_cookie);
399 if_clone_detach(&bridge_cloner);
400 bridge_input_p = NULL;
401 bridge_output_p = NULL;
403 bstp_linkstate_p = NULL;
412 static moduledata_t bridge_mod = {
418 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
422 * bridge_clone_create:
424 * Create a new bridge instance.
427 bridge_clone_create(struct if_clone *ifc, int unit)
429 struct bridge_softc *sc;
433 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
434 ifp = sc->sc_ifp = &sc->sc_if;
436 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
437 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
438 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
439 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
440 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
441 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
442 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
444 /* Initialize our routing table. */
445 bridge_rtable_init(sc);
447 callout_init(&sc->sc_brcallout);
448 callout_init(&sc->sc_bstpcallout);
450 LIST_INIT(&sc->sc_iflist);
451 LIST_INIT(&sc->sc_spanlist);
454 if_initname(ifp, ifc->ifc_name, unit);
455 ifp->if_mtu = ETHERMTU;
456 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
457 ifp->if_ioctl = bridge_ioctl;
458 ifp->if_start = bridge_start;
459 ifp->if_init = bridge_init;
460 ifp->if_type = IFT_BRIDGE;
461 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
462 ifp->if_snd.ifq_maxlen = ifqmaxlen;
463 ifq_set_ready(&ifp->if_snd);
464 ifp->if_hdrlen = ETHER_HDR_LEN;
467 * Generate a random ethernet address and use the private AC:DE:48
471 int rnd = karc4random();
472 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
474 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
476 eaddr[0] &= ~1; /* clear multicast bit */
477 eaddr[0] |= 2; /* set the LAA bit */
479 ether_ifattach(ifp, eaddr, NULL);
480 /* Now undo some of the damage... */
481 ifp->if_baudrate = 0;
482 ifp->if_type = IFT_BRIDGE;
485 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
492 * bridge_clone_destroy:
494 * Destroy a bridge instance.
497 bridge_clone_destroy(struct ifnet *ifp)
499 struct bridge_softc *sc = ifp->if_softc;
500 struct bridge_iflist *bif;
502 lwkt_serialize_enter(ifp->if_serializer);
505 ifp->if_flags &= ~IFF_UP;
507 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
508 bridge_delete_member(sc, bif, 0);
510 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
511 bridge_delete_span(sc, bif);
514 callout_stop(&sc->sc_brcallout);
515 callout_stop(&sc->sc_bstpcallout);
517 lwkt_serialize_exit(ifp->if_serializer);
520 LIST_REMOVE(sc, sc_list);
525 /* Tear down the routing table. */
526 bridge_rtable_fini(sc);
534 * Handle a control request from the operator.
537 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
539 struct bridge_softc *sc = ifp->if_softc;
541 struct ifbreq ifbreq;
542 struct ifbifconf ifbifconf;
543 struct ifbareq ifbareq;
544 struct ifbaconf ifbaconf;
545 struct ifbrparam ifbrparam;
547 struct ifdrv *ifd = (struct ifdrv *) data;
548 const struct bridge_control *bc;
551 ASSERT_SERIALIZED(ifp->if_serializer);
560 if (ifd->ifd_cmd >= bridge_control_table_size) {
564 bc = &bridge_control_table[ifd->ifd_cmd];
566 if (cmd == SIOCGDRVSPEC &&
567 (bc->bc_flags & BC_F_COPYOUT) == 0) {
570 } else if (cmd == SIOCSDRVSPEC &&
571 (bc->bc_flags & BC_F_COPYOUT) != 0) {
576 if (bc->bc_flags & BC_F_SUSER) {
577 error = suser_cred(cr, NULL_CRED_OKAY);
582 if (ifd->ifd_len != bc->bc_argsize ||
583 ifd->ifd_len > sizeof(args)) {
588 memset(&args, 0, sizeof(args));
589 if (bc->bc_flags & BC_F_COPYIN) {
590 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
595 error = bridge_control(sc, cmd, bc->bc_func, &args);
599 if (bc->bc_flags & BC_F_COPYOUT)
600 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
604 if (!(ifp->if_flags & IFF_UP) &&
605 (ifp->if_flags & IFF_RUNNING)) {
607 * If interface is marked down and it is running,
611 } else if ((ifp->if_flags & IFF_UP) &&
612 !(ifp->if_flags & IFF_RUNNING)) {
614 * If interface is marked up and it is stopped, then
622 /* Do not allow the MTU to be changed on the bridge */
627 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;
661 if (bif->bif_mutecap != 0) {
662 lwkt_serialize_enter(ifp->if_serializer);
663 error = ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
664 lwkt_serialize_exit(ifp->if_serializer);
669 * bridge_lookup_member:
671 * Lookup a bridge member interface.
673 static struct bridge_iflist *
674 bridge_lookup_member(struct bridge_softc *sc, const char *name)
676 struct bridge_iflist *bif;
679 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
681 if (strcmp(ifp->if_xname, name) == 0)
689 * bridge_lookup_member_if:
691 * Lookup a bridge member interface by ifnet*.
693 static struct bridge_iflist *
694 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
696 struct bridge_iflist *bif;
698 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
699 if (bif->bif_ifp == member_ifp)
707 * bridge_delete_member:
709 * Delete the specified member interface.
712 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
715 struct ifnet *ifs = bif->bif_ifp;
718 switch (ifs->if_type) {
722 * Take the interface out of promiscuous mode.
724 (void) ifpromisc(ifs, 0);
725 bridge_mutecaps(bif, 0);
733 panic("bridge_delete_member: impossible");
739 ifs->if_bridge = NULL;
740 LIST_REMOVE(bif, bif_next);
742 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
744 kfree(bif, M_DEVBUF);
746 if (sc->sc_ifp->if_flags & IFF_RUNNING)
747 bstp_initialization(sc);
751 * bridge_delete_span:
753 * Delete the specified span interface.
756 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
758 KASSERT(bif->bif_ifp->if_bridge == NULL,
759 ("%s: not a span interface", __func__));
761 LIST_REMOVE(bif, bif_next);
762 kfree(bif, M_DEVBUF);
766 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
768 struct ifbreq *req = arg;
769 struct bridge_iflist *bif = NULL;
770 struct ifnet *ifs, *bifp;
774 ASSERT_SERIALIZED(bifp->if_serializer);
776 ifs = ifunit(req->ifbr_ifsname);
780 /* If it's in the span list, it can't be a member. */
781 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
782 if (ifs == bif->bif_ifp)
785 /* Allow the first Ethernet member to define the MTU */
786 if (ifs->if_type != IFT_GIF) {
787 if (LIST_EMPTY(&sc->sc_iflist)) {
788 bifp->if_mtu = ifs->if_mtu;
789 } else if (bifp->if_mtu != ifs->if_mtu) {
790 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
795 if (ifs->if_bridge == sc)
798 if (ifs->if_bridge != NULL)
801 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|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 * Release bridge interface's serializer to
812 * avoid possible dead lock.
814 lwkt_serialize_exit(bifp->if_serializer);
817 * Place the interface into promiscuous mode.
819 error = ifpromisc(ifs, 1);
821 lwkt_serialize_enter(bifp->if_serializer);
825 bridge_mutecaps(bif, 1);
827 lwkt_serialize_enter(bifp->if_serializer);
830 case IFT_GIF: /* :^) */
838 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
840 if (bifp->if_flags & IFF_RUNNING)
841 bstp_initialization(sc);
846 * Everything has been setup, so let the member interface
847 * deliver packets to this bridge on its input/output path.
853 kfree(bif, M_DEVBUF);
859 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
861 struct ifbreq *req = arg;
862 struct bridge_iflist *bif;
864 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
868 bridge_delete_member(sc, bif, 0);
874 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
876 struct ifbreq *req = arg;
877 struct bridge_iflist *bif;
879 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
883 req->ifbr_ifsflags = bif->bif_flags;
884 req->ifbr_state = bif->bif_state;
885 req->ifbr_priority = bif->bif_priority;
886 req->ifbr_path_cost = bif->bif_path_cost;
887 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
893 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
895 struct ifbreq *req = arg;
896 struct bridge_iflist *bif;
898 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
902 if (req->ifbr_ifsflags & IFBIF_SPAN)
903 /* SPAN is readonly */
906 if (req->ifbr_ifsflags & IFBIF_STP) {
907 switch (bif->bif_ifp->if_type) {
909 /* These can do spanning tree. */
913 /* Nothing else can. */
918 bif->bif_flags = req->ifbr_ifsflags;
920 if (sc->sc_ifp->if_flags & IFF_RUNNING)
921 bstp_initialization(sc);
927 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
929 struct ifbrparam *param = arg;
931 sc->sc_brtmax = param->ifbrp_csize;
938 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
940 struct ifbrparam *param = arg;
942 param->ifbrp_csize = sc->sc_brtmax;
948 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
950 struct ifbifconf *bifc = arg;
951 struct bridge_iflist *bif;
953 int count, len, error = 0;
956 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
958 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
961 if (bifc->ifbic_len == 0) {
962 bifc->ifbic_len = sizeof(breq) * count;
967 len = bifc->ifbic_len;
968 memset(&breq, 0, sizeof breq);
969 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
970 if (len < sizeof(breq))
973 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
974 sizeof(breq.ifbr_ifsname));
975 breq.ifbr_ifsflags = bif->bif_flags;
976 breq.ifbr_state = bif->bif_state;
977 breq.ifbr_priority = bif->bif_priority;
978 breq.ifbr_path_cost = bif->bif_path_cost;
979 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
980 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
986 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
987 if (len < sizeof(breq))
990 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
991 sizeof(breq.ifbr_ifsname));
992 breq.ifbr_ifsflags = bif->bif_flags;
993 breq.ifbr_state = bif->bif_state;
994 breq.ifbr_priority = bif->bif_priority;
995 breq.ifbr_path_cost = bif->bif_path_cost;
996 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
997 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
1001 len -= sizeof(breq);
1004 bifc->ifbic_len = sizeof(breq) * count;
1009 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1011 struct ifbaconf *bac = arg;
1012 struct bridge_rtnode *brt;
1013 struct ifbareq bareq;
1014 int count = 0, error = 0, len;
1016 if (bac->ifbac_len == 0)
1019 len = bac->ifbac_len;
1020 memset(&bareq, 0, sizeof(bareq));
1021 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1022 if (len < sizeof(bareq))
1024 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1025 sizeof(bareq.ifba_ifsname));
1026 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1027 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1028 time_second < brt->brt_expire)
1029 bareq.ifba_expire = brt->brt_expire - time_second;
1031 bareq.ifba_expire = 0;
1032 bareq.ifba_flags = brt->brt_flags;
1034 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1038 len -= sizeof(bareq);
1041 bac->ifbac_len = sizeof(bareq) * count;
1046 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1048 struct ifbareq *req = arg;
1049 struct bridge_iflist *bif;
1052 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1056 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1063 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1065 struct ifbrparam *param = arg;
1067 sc->sc_brttimeout = param->ifbrp_ctime;
1073 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1075 struct ifbrparam *param = arg;
1077 param->ifbrp_ctime = sc->sc_brttimeout;
1083 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1085 struct ifbareq *req = arg;
1087 return (bridge_rtdaddr(sc, req->ifba_dst));
1091 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1093 struct ifbreq *req = arg;
1095 bridge_rtflush(sc, req->ifbr_ifsflags);
1101 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1103 struct ifbrparam *param = arg;
1105 param->ifbrp_prio = sc->sc_bridge_priority;
1111 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1113 struct ifbrparam *param = arg;
1115 sc->sc_bridge_priority = param->ifbrp_prio;
1117 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1118 bstp_initialization(sc);
1124 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1126 struct ifbrparam *param = arg;
1128 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1134 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1136 struct ifbrparam *param = arg;
1138 if (param->ifbrp_hellotime == 0)
1140 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1142 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1143 bstp_initialization(sc);
1149 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1151 struct ifbrparam *param = arg;
1153 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1159 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1161 struct ifbrparam *param = arg;
1163 if (param->ifbrp_fwddelay == 0)
1165 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1167 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1168 bstp_initialization(sc);
1174 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1176 struct ifbrparam *param = arg;
1178 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1184 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1186 struct ifbrparam *param = arg;
1188 if (param->ifbrp_maxage == 0)
1190 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1192 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1193 bstp_initialization(sc);
1199 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1201 struct ifbreq *req = arg;
1202 struct bridge_iflist *bif;
1204 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1208 bif->bif_priority = req->ifbr_priority;
1210 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1211 bstp_initialization(sc);
1217 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1219 struct ifbreq *req = arg;
1220 struct bridge_iflist *bif;
1222 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1226 bif->bif_path_cost = req->ifbr_path_cost;
1228 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1229 bstp_initialization(sc);
1235 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1237 struct ifbreq *req = arg;
1238 struct bridge_iflist *bif = NULL;
1241 ifs = ifunit(req->ifbr_ifsname);
1245 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1246 if (ifs == bif->bif_ifp)
1249 if (ifs->if_bridge != NULL)
1252 switch (ifs->if_type) {
1261 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
1264 bif->bif_flags = IFBIF_SPAN;
1266 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1272 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1274 struct ifbreq *req = arg;
1275 struct bridge_iflist *bif;
1278 ifs = ifunit(req->ifbr_ifsname);
1282 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1283 if (ifs == bif->bif_ifp)
1289 bridge_delete_span(sc, bif);
1297 * Detach an interface from a bridge. Called when a member
1298 * interface is detaching.
1301 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1303 struct bridge_softc *sc = ifp->if_bridge;
1304 struct bridge_iflist *bif;
1306 /* Check if the interface is a bridge member */
1308 lwkt_serialize_enter(ifp->if_serializer);
1310 bif = bridge_lookup_member_if(sc, ifp);
1312 bridge_delete_member(sc, bif, 1);
1314 lwkt_serialize_exit(ifp->if_serializer);
1318 /* Check if the interface is a span port */
1319 LIST_FOREACH(sc, &bridge_list, sc_list) {
1320 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1321 if (ifp == bif->bif_ifp) {
1322 bridge_delete_span(sc, bif);
1331 * Initialize a bridge interface.
1334 bridge_init(void *xsc)
1336 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1337 struct ifnet *ifp = sc->sc_ifp;
1339 ASSERT_SERIALIZED(ifp->if_serializer);
1341 if (ifp->if_flags & IFF_RUNNING)
1344 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1347 ifp->if_flags |= IFF_RUNNING;
1348 bstp_initialization(sc);
1355 * Stop the bridge interface.
1358 bridge_stop(struct ifnet *ifp)
1360 struct bridge_softc *sc = ifp->if_softc;
1362 ASSERT_SERIALIZED(ifp->if_serializer);
1364 if ((ifp->if_flags & IFF_RUNNING) == 0)
1367 callout_stop(&sc->sc_brcallout);
1370 bridge_rtflush(sc, IFBF_FLUSHDYN);
1372 ifp->if_flags &= ~IFF_RUNNING;
1376 bridge_enqueue_internal(struct ifnet *dst_ifp, struct mbuf *m,
1377 netisr_fn_t handler)
1379 struct netmsg_packet *nmp;
1381 int cpu = mycpu->gd_cpuid;
1383 while (m->m_type == MT_TAG) {
1384 /* XXX see ether_output_frame for full rules check */
1388 nmp = &m->m_hdr.mh_netmsg;
1389 netmsg_init(&nmp->nm_netmsg, &netisr_apanic_rport, 0, handler);
1391 nmp->nm_netmsg.nm_lmsg.u.ms_resultp = dst_ifp;
1393 port = cpu_portfn(cpu);
1394 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
1398 bridge_pfil_enqueue(struct ifnet *dst_ifp, struct mbuf *m,
1401 netisr_fn_t handler;
1403 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1405 || inet6_pfil_hook.ph_hashooks > 0
1408 handler = bridge_pfil_enqueue_handler;
1410 handler = bridge_enqueue_handler;
1412 bridge_enqueue_internal(dst_ifp, m, handler);
1418 * Enqueue a packet on a bridge member interface.
1422 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1424 bridge_enqueue_internal(dst_ifp, m, bridge_enqueue_handler);
1430 * Send output from a bridge member interface. This
1431 * performs the bridging function for locally originated
1434 * The mbuf has the Ethernet header already attached. We must
1435 * enqueue or free the mbuf before returning.
1438 bridge_output(struct ifnet *ifp, struct mbuf *m)
1440 struct bridge_softc *sc = ifp->if_bridge;
1441 struct ether_header *eh;
1442 struct ifnet *dst_if;
1444 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
1447 * Make sure that we are still a member of a bridge interface.
1454 if (m->m_len < ETHER_HDR_LEN) {
1455 m = m_pullup(m, ETHER_HDR_LEN);
1460 /* Serialize our bridge interface. */
1461 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1463 eh = mtod(m, struct ether_header *);
1466 * If bridge is down, but the original output interface is up,
1467 * go ahead and send out that interface. Otherwise, the packet
1470 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
1476 * If the packet is a multicast, or we don't know a better way to
1477 * get there, send to all interfaces.
1479 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1482 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1483 if (dst_if == NULL) {
1484 struct bridge_iflist *bif;
1490 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1491 dst_if = bif->bif_ifp;
1492 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1496 * If this is not the original output interface,
1497 * and the interface is participating in spanning
1498 * tree, make sure the port is in a state that
1499 * allows forwarding.
1501 if (dst_if != ifp &&
1502 (bif->bif_flags & IFBIF_STP) != 0) {
1503 switch (bif->bif_state) {
1504 case BSTP_IFSTATE_BLOCKING:
1505 case BSTP_IFSTATE_LISTENING:
1506 case BSTP_IFSTATE_DISABLED:
1511 if (LIST_NEXT(bif, bif_next) == NULL) {
1515 mc = m_copypacket(m, MB_DONTWAIT);
1517 sc->sc_ifp->if_oerrors++;
1521 bridge_enqueue(dst_if, mc);
1525 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1531 * XXX Spanning tree consideration here?
1535 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1536 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1539 bridge_enqueue(dst_if, m);
1546 * Start output on a bridge.
1550 bridge_start(struct ifnet *ifp)
1552 struct bridge_softc *sc = ifp->if_softc;
1554 ASSERT_SERIALIZED(ifp->if_serializer);
1556 ifp->if_flags |= IFF_OACTIVE;
1558 struct ifnet *dst_if = NULL;
1559 struct ether_header *eh;
1562 m = ifq_dequeue(&ifp->if_snd, NULL);
1566 if (m->m_len < sizeof(*eh)) {
1567 m = m_pullup(m, sizeof(*eh));
1573 eh = mtod(m, struct ether_header *);
1578 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1579 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1582 bridge_broadcast(sc, ifp, m, 0);
1584 bridge_enqueue(dst_if, m);
1586 ifp->if_flags &= ~IFF_OACTIVE;
1592 * The forwarding function of the bridge.
1595 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1597 struct bridge_iflist *bif;
1598 struct ifnet *src_if, *dst_if, *ifp;
1599 struct ether_header *eh;
1601 src_if = m->m_pkthdr.rcvif;
1604 ASSERT_SERIALIZED(ifp->if_serializer);
1607 ifp->if_ibytes += m->m_pkthdr.len;
1610 * Look up the bridge_iflist.
1612 bif = bridge_lookup_member_if(sc, src_if);
1614 /* Interface is not a bridge member (anymore?) */
1619 if (bif->bif_flags & IFBIF_STP) {
1620 switch (bif->bif_state) {
1621 case BSTP_IFSTATE_BLOCKING:
1622 case BSTP_IFSTATE_LISTENING:
1623 case BSTP_IFSTATE_DISABLED:
1629 eh = mtod(m, struct ether_header *);
1632 * If the interface is learning, and the source
1633 * address is valid and not multicast, record
1636 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1637 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1638 (eh->ether_shost[0] == 0 &&
1639 eh->ether_shost[1] == 0 &&
1640 eh->ether_shost[2] == 0 &&
1641 eh->ether_shost[3] == 0 &&
1642 eh->ether_shost[4] == 0 &&
1643 eh->ether_shost[5] == 0) == 0) {
1644 bridge_rtupdate(sc, eh->ether_shost, src_if, 0, IFBAF_DYNAMIC);
1647 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1648 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1654 * At this point, the port either doesn't participate
1655 * in spanning tree or it is in the forwarding state.
1659 * If the packet is unicast, destined for someone on
1660 * "this" side of the bridge, drop it.
1662 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1663 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1664 if (src_if == dst_if) {
1669 /* ...forward it to all interfaces. */
1670 sc->sc_ifp->if_imcasts++;
1674 if (dst_if == NULL) {
1675 bridge_broadcast(sc, src_if, m, 1);
1680 * At this point, we're dealing with a unicast frame
1681 * going to a different interface.
1683 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1687 bif = bridge_lookup_member_if(sc, dst_if);
1689 /* Not a member of the bridge (anymore?) */
1694 if (bif->bif_flags & IFBIF_STP) {
1695 switch (bif->bif_state) {
1696 case BSTP_IFSTATE_DISABLED:
1697 case BSTP_IFSTATE_BLOCKING:
1703 lwkt_serialize_exit(ifp->if_serializer);
1705 /* run the packet filter */
1706 if (inet_pfil_hook.ph_hashooks > 0
1708 || inet6_pfil_hook.ph_hashooks > 0
1711 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1716 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
1721 bridge_handoff(dst_if, m);
1724 * ifp's serializer was held on entry and is expected to be held
1728 lwkt_serialize_enter(ifp->if_serializer);
1734 * Receive input from a member interface. Queue the packet for
1735 * bridging if it is not for us.
1737 static struct mbuf *
1738 bridge_input(struct ifnet *ifp, struct mbuf *m)
1740 struct bridge_softc *sc = ifp->if_bridge;
1741 struct bridge_iflist *bif;
1742 struct ifnet *bifp, *new_ifp;
1743 struct ether_header *eh;
1744 struct mbuf *mc, *mc2;
1747 * Make sure that we are still a member of a bridge interface.
1755 lwkt_serialize_enter(bifp->if_serializer);
1757 if ((bifp->if_flags & IFF_RUNNING) == 0)
1761 * Implement support for bridge monitoring. If this flag has been
1762 * set on this interface, discard the packet once we push it through
1763 * the bpf(4) machinery, but before we do, increment the byte and
1764 * packet counters associated with this interface.
1766 if ((bifp->if_flags & IFF_MONITOR) != 0) {
1767 m->m_pkthdr.rcvif = bifp;
1769 bifp->if_ipackets++;
1770 bifp->if_ibytes += m->m_pkthdr.len;
1776 eh = mtod(m, struct ether_header *);
1778 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
1780 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
1782 * If the packet is for us, set the packets source as the
1783 * bridge, and return the packet back to ifnet.if_input for
1786 KASSERT(bifp->if_bridge == NULL,
1787 ("loop created in bridge_input"));
1793 * Tap all packets arriving on the bridge, no matter if
1794 * they are local destinations or not. In is in.
1798 bif = bridge_lookup_member_if(sc, ifp);
1804 if (m->m_flags & (M_BCAST | M_MCAST)) {
1805 /* Tap off 802.1D packets; they do not get forwarded. */
1806 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1807 ETHER_ADDR_LEN) == 0) {
1808 m = bstp_input(sc, bif, m);
1810 ("attempt to deliver 802.1D packet\n"));
1814 if (bif->bif_flags & IFBIF_STP) {
1815 switch (bif->bif_state) {
1816 case BSTP_IFSTATE_BLOCKING:
1817 case BSTP_IFSTATE_LISTENING:
1818 case BSTP_IFSTATE_DISABLED:
1824 * Make a deep copy of the packet and enqueue the copy
1825 * for bridge processing; return the original packet for
1828 mc = m_dup(m, MB_DONTWAIT);
1832 bridge_forward(sc, mc);
1835 * Reinject the mbuf as arriving on the bridge so we have a
1836 * chance at claiming multicast packets. We can not loop back
1837 * here from ether_input as a bridge is never a member of a
1840 KASSERT(bifp->if_bridge == NULL,
1841 ("loop created in bridge_input"));
1842 mc2 = m_dup(m, MB_DONTWAIT);
1845 /* Keep the layer3 header aligned */
1846 int i = min(mc2->m_pkthdr.len, max_protohdr);
1847 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
1851 mc2->m_pkthdr.rcvif = bifp;
1852 bifp->if_ipackets++;
1853 bifp->if_input(bifp, mc2);
1856 /* Return the original packet for local processing. */
1860 if (bif->bif_flags & IFBIF_STP) {
1861 switch (bif->bif_state) {
1862 case BSTP_IFSTATE_BLOCKING:
1863 case BSTP_IFSTATE_LISTENING:
1864 case BSTP_IFSTATE_DISABLED:
1870 * Unicast. Make sure it's not for us.
1872 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1873 if (bif->bif_ifp->if_type != IFT_ETHER)
1876 /* It is destined for us. */
1877 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1878 ETHER_ADDR_LEN) == 0) {
1879 if (bif->bif_flags & IFBIF_LEARNING) {
1881 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1884 if (bif->bif_ifp != ifp) {
1885 /* XXX loop prevention */
1886 m->m_flags |= M_PROTO1;
1887 new_ifp = bif->bif_ifp;
1892 /* We just received a packet that we sent out. */
1893 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1894 ETHER_ADDR_LEN) == 0) {
1901 /* Perform the bridge forwarding function. */
1902 bridge_forward(sc, m);
1905 lwkt_serialize_exit(bifp->if_serializer);
1907 if (new_ifp != NULL) {
1908 lwkt_serialize_enter(new_ifp->if_serializer);
1910 m->m_pkthdr.rcvif = new_ifp;
1911 new_ifp->if_ipackets++;
1912 new_ifp->if_input(new_ifp, m);
1915 lwkt_serialize_exit(new_ifp->if_serializer);
1923 * Send a frame to all interfaces that are members of
1924 * the bridge, except for the one on which the packet
1928 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1929 struct mbuf *m, int runfilt)
1931 struct bridge_iflist *bif;
1933 struct ifnet *dst_if, *bifp;
1938 ASSERT_SERIALIZED(bifp->if_serializer);
1940 /* run the packet filter */
1941 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1943 || inet6_pfil_hook.ph_hashooks > 0
1946 lwkt_serialize_exit(bifp->if_serializer);
1948 /* Filter on the bridge interface before broadcasting */
1950 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
1955 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
1958 lwkt_serialize_enter(bifp->if_serializer);
1963 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1964 dst_if = bif->bif_ifp;
1965 if (dst_if == src_if)
1968 if (bif->bif_flags & IFBIF_STP) {
1969 switch (bif->bif_state) {
1970 case BSTP_IFSTATE_BLOCKING:
1971 case BSTP_IFSTATE_DISABLED:
1976 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1977 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1980 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1983 if (LIST_NEXT(bif, bif_next) == NULL) {
1987 mc = m_copypacket(m, MB_DONTWAIT);
1989 sc->sc_ifp->if_oerrors++;
1993 bridge_pfil_enqueue(dst_if, mc, runfilt);
2002 * Duplicate a packet out one or more interfaces that are in span mode,
2003 * the original mbuf is unmodified.
2006 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2008 struct bridge_iflist *bif;
2009 struct ifnet *dst_if;
2012 if (LIST_EMPTY(&sc->sc_spanlist))
2015 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2016 dst_if = bif->bif_ifp;
2018 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2021 mc = m_copypacket(m, MB_DONTWAIT);
2023 sc->sc_ifp->if_oerrors++;
2027 bridge_enqueue(dst_if, mc);
2034 * Add a bridge routing entry.
2035 * Can be called from interrupt context.
2038 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2039 struct ifnet *dst_if, int setflags, uint8_t flags)
2041 struct bridge_rtnode *brt;
2045 * A route for this destination might already exist. If so,
2046 * update it, otherwise create a new one.
2048 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2049 if (sc->sc_brtcnt >= sc->sc_brtmax)
2053 * Allocate a new bridge forwarding node, and
2054 * initialize the expiration time and Ethernet
2057 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2058 M_INTNOWAIT|M_ZERO);
2062 brt->brt_flags = IFBAF_DYNAMIC;
2063 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2065 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2066 kfree(brt, M_DEVBUF);
2071 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2072 brt->brt_ifp = dst_if;
2073 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2074 brt->brt_expire = time_second + sc->sc_brttimeout;
2076 brt->brt_flags = flags;
2084 * Lookup the destination interface for an address.
2086 static struct ifnet *
2087 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2089 struct bridge_rtnode *brt;
2091 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2094 return (brt->brt_ifp);
2100 * Trim the routine table so that we have a number
2101 * of routing entries less than or equal to the
2105 bridge_rttrim(struct bridge_softc *sc)
2107 struct bridge_rtnode *brt, *nbrt;
2109 /* Make sure we actually need to do this. */
2110 if (sc->sc_brtcnt <= sc->sc_brtmax)
2113 /* Force an aging cycle; this might trim enough addresses. */
2115 if (sc->sc_brtcnt <= sc->sc_brtmax)
2118 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2119 nbrt = LIST_NEXT(brt, brt_list);
2120 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2121 bridge_rtnode_destroy(sc, brt);
2122 if (sc->sc_brtcnt <= sc->sc_brtmax)
2131 * Aging timer for the bridge.
2134 bridge_timer(void *arg)
2136 struct bridge_softc *sc = arg;
2138 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
2142 if (sc->sc_ifp->if_flags & IFF_RUNNING)
2143 callout_reset(&sc->sc_brcallout,
2144 bridge_rtable_prune_period * hz, bridge_timer, sc);
2146 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
2152 * Perform an aging cycle.
2155 bridge_rtage(struct bridge_softc *sc)
2157 struct bridge_rtnode *brt, *nbrt;
2159 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2160 nbrt = LIST_NEXT(brt, brt_list);
2161 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2162 if (time_second >= brt->brt_expire)
2163 bridge_rtnode_destroy(sc, brt);
2171 * Remove all dynamic addresses from the bridge.
2174 bridge_rtflush(struct bridge_softc *sc, int full)
2176 struct bridge_rtnode *brt, *nbrt;
2178 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2179 nbrt = LIST_NEXT(brt, brt_list);
2180 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2181 bridge_rtnode_destroy(sc, brt);
2188 * Remove an address from the table.
2191 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2193 struct bridge_rtnode *brt;
2195 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2198 bridge_rtnode_destroy(sc, brt);
2205 * Delete routes to a speicifc member interface.
2208 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2210 struct bridge_rtnode *brt, *nbrt;
2212 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2213 nbrt = LIST_NEXT(brt, brt_list);
2214 if (brt->brt_ifp == ifp && (full ||
2215 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2216 bridge_rtnode_destroy(sc, brt);
2221 * bridge_rtable_init:
2223 * Initialize the route table for this bridge.
2226 bridge_rtable_init(struct bridge_softc *sc)
2230 sc->sc_rthash = kmalloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2231 M_DEVBUF, M_WAITOK);
2233 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2234 LIST_INIT(&sc->sc_rthash[i]);
2236 sc->sc_rthash_key = karc4random();
2238 LIST_INIT(&sc->sc_rtlist);
2244 * bridge_rtable_fini:
2246 * Deconstruct the route table for this bridge.
2249 bridge_rtable_fini(struct bridge_softc *sc)
2252 kfree(sc->sc_rthash, M_DEVBUF);
2256 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2257 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2259 #define mix(a, b, c) \
2261 a -= b; a -= c; a ^= (c >> 13); \
2262 b -= c; b -= a; b ^= (a << 8); \
2263 c -= a; c -= b; c ^= (b >> 13); \
2264 a -= b; a -= c; a ^= (c >> 12); \
2265 b -= c; b -= a; b ^= (a << 16); \
2266 c -= a; c -= b; c ^= (b >> 5); \
2267 a -= b; a -= c; a ^= (c >> 3); \
2268 b -= c; b -= a; b ^= (a << 10); \
2269 c -= a; c -= b; c ^= (b >> 15); \
2270 } while (/*CONSTCOND*/0)
2272 static __inline uint32_t
2273 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2275 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2286 return (c & BRIDGE_RTHASH_MASK);
2292 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2296 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2297 d = ((int)a[i]) - ((int)b[i]);
2304 * bridge_rtnode_lookup:
2306 * Look up a bridge route node for the specified destination.
2308 static struct bridge_rtnode *
2309 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2311 struct bridge_rtnode *brt;
2315 hash = bridge_rthash(sc, addr);
2316 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2317 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2328 * bridge_rtnode_insert:
2330 * Insert the specified bridge node into the route table. We
2331 * assume the entry is not already in the table.
2334 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2336 struct bridge_rtnode *lbrt;
2340 hash = bridge_rthash(sc, brt->brt_addr);
2342 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2344 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2349 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2353 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2356 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2357 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2360 lbrt = LIST_NEXT(lbrt, brt_hash);
2361 } while (lbrt != NULL);
2364 panic("bridge_rtnode_insert: impossible");
2368 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2375 * bridge_rtnode_destroy:
2377 * Destroy a bridge rtnode.
2380 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2383 LIST_REMOVE(brt, brt_hash);
2385 LIST_REMOVE(brt, brt_list);
2387 kfree(brt, M_DEVBUF);
2391 * Send bridge packets through pfil if they are one of the types pfil can deal
2392 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2393 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2397 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2399 int snap, error, i, hlen;
2400 struct ether_header *eh1, eh2;
2403 u_int16_t ether_type;
2406 error = -1; /* Default error if not error == 0 */
2408 if (pfil_bridge == 0 && pfil_member == 0)
2409 return (0); /* filtering is disabled */
2411 i = min((*mp)->m_pkthdr.len, max_protohdr);
2412 if ((*mp)->m_len < i) {
2413 *mp = m_pullup(*mp, i);
2415 kprintf("%s: m_pullup failed\n", __func__);
2420 eh1 = mtod(*mp, struct ether_header *);
2421 ether_type = ntohs(eh1->ether_type);
2424 * Check for SNAP/LLC.
2426 if (ether_type < ETHERMTU) {
2427 struct llc *llc2 = (struct llc *)(eh1 + 1);
2429 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2430 llc2->llc_dsap == LLC_SNAP_LSAP &&
2431 llc2->llc_ssap == LLC_SNAP_LSAP &&
2432 llc2->llc_control == LLC_UI) {
2433 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2439 * If we're trying to filter bridge traffic, don't look at anything
2440 * other than IP and ARP traffic. If the filter doesn't understand
2441 * IPv6, don't allow IPv6 through the bridge either. This is lame
2442 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2443 * but of course we don't have an AppleTalk filter to begin with.
2444 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2447 switch (ether_type) {
2449 case ETHERTYPE_REVARP:
2450 return (0); /* Automatically pass */
2453 case ETHERTYPE_IPV6:
2458 * Check to see if the user wants to pass non-ip
2459 * packets, these will not be checked by pfil(9) and
2460 * passed unconditionally so the default is to drop.
2466 /* Strip off the Ethernet header and keep a copy. */
2467 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2468 m_adj(*mp, ETHER_HDR_LEN);
2470 /* Strip off snap header, if present */
2472 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2473 m_adj(*mp, sizeof(struct llc));
2477 * Check the IP header for alignment and errors
2479 if (dir == PFIL_IN) {
2480 switch (ether_type) {
2482 error = bridge_ip_checkbasic(mp);
2485 case ETHERTYPE_IPV6:
2486 error = bridge_ip6_checkbasic(mp);
2499 * Run the packet through pfil
2505 * before calling the firewall, swap fields the same as
2506 * IP does. here we assume the header is contiguous
2508 ip = mtod(*mp, struct ip *);
2510 ip->ip_len = ntohs(ip->ip_len);
2511 ip->ip_off = ntohs(ip->ip_off);
2514 * Run pfil on the member interface and the bridge, both can
2515 * be skipped by clearing pfil_member or pfil_bridge.
2518 * in_if -> bridge_if -> out_if
2520 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2521 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2524 if (*mp == NULL || error != 0) /* filter may consume */
2527 if (pfil_member && ifp != NULL)
2528 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2531 if (*mp == NULL || error != 0) /* filter may consume */
2534 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2535 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2538 if (*mp == NULL || error != 0) /* filter may consume */
2541 /* check if we need to fragment the packet */
2542 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
2543 i = (*mp)->m_pkthdr.len;
2544 if (i > ifp->if_mtu) {
2545 error = bridge_fragment(ifp, *mp, &eh2, snap,
2551 /* Recalculate the ip checksum and restore byte ordering */
2552 ip = mtod(*mp, struct ip *);
2553 hlen = ip->ip_hl << 2;
2554 if (hlen < sizeof(struct ip))
2556 if (hlen > (*mp)->m_len) {
2557 if ((*mp = m_pullup(*mp, hlen)) == 0)
2559 ip = mtod(*mp, struct ip *);
2563 ip->ip_len = htons(ip->ip_len);
2564 ip->ip_off = htons(ip->ip_off);
2566 if (hlen == sizeof(struct ip))
2567 ip->ip_sum = in_cksum_hdr(ip);
2569 ip->ip_sum = in_cksum(*mp, hlen);
2573 case ETHERTYPE_IPV6 :
2574 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2575 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2578 if (*mp == NULL || error != 0) /* filter may consume */
2581 if (pfil_member && ifp != NULL)
2582 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2585 if (*mp == NULL || error != 0) /* filter may consume */
2588 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2589 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2606 * Finally, put everything back the way it was and return
2609 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
2612 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2615 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
2618 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2629 * Perform basic checks on header size since
2630 * pfil assumes ip_input has already processed
2631 * it for it. Cut-and-pasted from ip_input.c.
2632 * Given how simple the IPv6 version is,
2633 * does the IPv4 version really need to be
2636 * XXX Should we update ipstat here, or not?
2637 * XXX Right now we update ipstat but not
2641 bridge_ip_checkbasic(struct mbuf **mp)
2643 struct mbuf *m = *mp;
2651 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2652 if ((m = m_copyup(m, sizeof(struct ip),
2653 (max_linkhdr + 3) & ~3)) == NULL) {
2654 /* XXXJRT new stat, please */
2655 ipstat.ips_toosmall++;
2660 #ifndef __predict_false
2661 #define __predict_false(x) x
2663 if (__predict_false(m->m_len < sizeof (struct ip))) {
2664 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2665 ipstat.ips_toosmall++;
2669 ip = mtod(m, struct ip *);
2670 if (ip == NULL) goto bad;
2672 if (ip->ip_v != IPVERSION) {
2673 ipstat.ips_badvers++;
2676 hlen = ip->ip_hl << 2;
2677 if (hlen < sizeof(struct ip)) { /* minimum header length */
2678 ipstat.ips_badhlen++;
2681 if (hlen > m->m_len) {
2682 if ((m = m_pullup(m, hlen)) == 0) {
2683 ipstat.ips_badhlen++;
2686 ip = mtod(m, struct ip *);
2687 if (ip == NULL) goto bad;
2690 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2691 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2693 if (hlen == sizeof(struct ip)) {
2694 sum = in_cksum_hdr(ip);
2696 sum = in_cksum(m, hlen);
2700 ipstat.ips_badsum++;
2704 /* Retrieve the packet length. */
2705 len = ntohs(ip->ip_len);
2708 * Check for additional length bogosity
2711 ipstat.ips_badlen++;
2716 * Check that the amount of data in the buffers
2717 * is as at least much as the IP header would have us expect.
2718 * Drop packet if shorter than we expect.
2720 if (m->m_pkthdr.len < len) {
2721 ipstat.ips_tooshort++;
2725 /* Checks out, proceed */
2736 * Same as above, but for IPv6.
2737 * Cut-and-pasted from ip6_input.c.
2738 * XXX Should we update ip6stat, or not?
2741 bridge_ip6_checkbasic(struct mbuf **mp)
2743 struct mbuf *m = *mp;
2744 struct ip6_hdr *ip6;
2747 * If the IPv6 header is not aligned, slurp it up into a new
2748 * mbuf with space for link headers, in the event we forward
2749 * it. Otherwise, if it is aligned, make sure the entire base
2750 * IPv6 header is in the first mbuf of the chain.
2753 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2754 struct ifnet *inifp = m->m_pkthdr.rcvif;
2755 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2756 (max_linkhdr + 3) & ~3)) == NULL) {
2757 /* XXXJRT new stat, please */
2758 ip6stat.ip6s_toosmall++;
2759 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2764 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2765 struct ifnet *inifp = m->m_pkthdr.rcvif;
2766 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2767 ip6stat.ip6s_toosmall++;
2768 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2773 ip6 = mtod(m, struct ip6_hdr *);
2775 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2776 ip6stat.ip6s_badvers++;
2777 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2781 /* Checks out, proceed */
2794 * Return a fragmented mbuf chain.
2797 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
2798 int snap, struct llc *llc)
2804 if (m->m_len < sizeof(struct ip) &&
2805 (m = m_pullup(m, sizeof(struct ip))) == NULL)
2807 ip = mtod(m, struct ip *);
2809 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
2814 /* walk the chain and re-add the Ethernet header */
2815 for (m0 = m; m0; m0 = m0->m_nextpkt) {
2818 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
2823 bcopy(llc, mtod(m0, caddr_t),
2824 sizeof(struct llc));
2826 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
2831 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
2837 ipstat.ips_fragmented++;
2848 bridge_enqueue_handler(struct netmsg *nmsg)
2850 struct netmsg_packet *nmp;
2851 struct ifnet *dst_ifp;
2854 nmp = (struct netmsg_packet *)nmsg;
2856 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2858 bridge_handoff_notags(dst_ifp, m);
2862 bridge_pfil_enqueue_handler(struct netmsg *nmsg)
2864 struct netmsg_packet *nmp;
2865 struct ifnet *dst_ifp;
2868 nmp = (struct netmsg_packet *)nmsg;
2870 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2873 * Filter on the output interface. Pass a NULL bridge interface
2874 * pointer so we do not redundantly filter on the bridge for
2875 * each interface we broadcast on.
2877 if (inet_pfil_hook.ph_hashooks > 0
2879 || inet6_pfil_hook.ph_hashooks > 0
2882 if (bridge_pfil(&m, NULL, dst_ifp, PFIL_OUT) != 0)
2887 bridge_handoff_notags(dst_ifp, m);
2891 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m)
2893 while (m->m_type == MT_TAG) {
2894 /* XXX see ether_output_frame for full rules check */
2897 bridge_handoff_notags(dst_ifp, m);
2901 bridge_handoff_notags(struct ifnet *dst_ifp, struct mbuf *m)
2905 KKASSERT(m->m_type != MT_TAG);
2907 lwkt_serialize_enter(dst_ifp->if_serializer);
2909 /* We may be sending a fragment so traverse the mbuf */
2911 struct altq_pktattr pktattr;
2914 m->m_nextpkt = NULL;
2916 if (ifq_is_enabled(&dst_ifp->if_snd))
2917 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
2919 ifq_handoff(dst_ifp, m, &pktattr);
2922 lwkt_serialize_exit(dst_ifp->if_serializer);
2925 struct netmsg_brgctl {
2926 struct netmsg bc_nmsg;
2927 bridge_ctl_t bc_func;
2928 struct bridge_softc *bc_sc;
2933 bridge_control_dispatch(struct netmsg *nmsg)
2935 struct netmsg_brgctl *bc_msg = (struct netmsg_brgctl *)nmsg;
2936 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
2939 lwkt_serialize_enter(bifp->if_serializer);
2940 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
2941 lwkt_serialize_exit(bifp->if_serializer);
2943 lwkt_replymsg(&nmsg->nm_lmsg, error);
2947 bridge_control(struct bridge_softc *sc, u_long cmd,
2948 bridge_ctl_t bc_func, void *bc_arg)
2950 struct ifnet *bifp = sc->sc_ifp;
2951 struct netmsg_brgctl bc_msg;
2952 struct netmsg *nmsg;
2955 ASSERT_SERIALIZED(bifp->if_serializer);
2957 if (cmd == SIOCGDRVSPEC) {
2959 * Don't dispatch 'get' ioctl to netisr0;
2960 * there are copyouts down deep inside
2961 * specific bridge ioctl functions.
2963 return bc_func(sc, bc_arg);
2966 bzero(&bc_msg, sizeof(bc_msg));
2967 nmsg = &bc_msg.bc_nmsg;
2969 netmsg_init(nmsg, &curthread->td_msgport, 0, bridge_control_dispatch);
2970 bc_msg.bc_func = bc_func;
2972 bc_msg.bc_arg = bc_arg;
2974 lwkt_serialize_exit(bifp->if_serializer);
2975 error = lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
2976 lwkt_serialize_enter(bifp->if_serializer);