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.30 2008/06/14 04:00:51 sephe Exp $
73 * Network interface bridge support.
77 * - Currently only supports Ethernet-like interfaces (Ethernet,
78 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
79 * to bridge other types of interfaces (FDDI-FDDI, and maybe
80 * consider heterogenous bridges).
83 #include <sys/cdefs.h>
86 #include "opt_inet6.h"
88 #include <sys/param.h>
90 #include <sys/malloc.h>
91 #include <sys/protosw.h>
92 #include <sys/systm.h>
94 #include <sys/socket.h> /* for net/if.h */
95 #include <sys/sockio.h>
96 #include <sys/ctype.h> /* string functions */
97 #include <sys/kernel.h>
98 #include <sys/random.h>
99 #include <sys/sysctl.h>
100 #include <sys/module.h>
101 #include <sys/proc.h>
102 #include <sys/lock.h>
103 #include <sys/thread.h>
104 #include <sys/thread2.h>
105 #include <sys/mpipe.h>
109 #include <net/if_dl.h>
110 #include <net/if_types.h>
111 #include <net/if_var.h>
112 #include <net/pfil.h>
113 #include <net/ifq_var.h>
114 #include <net/if_clone.h>
116 #include <netinet/in.h> /* for struct arpcom */
117 #include <netinet/in_systm.h>
118 #include <netinet/in_var.h>
119 #include <netinet/ip.h>
120 #include <netinet/ip_var.h>
122 #include <netinet/ip6.h>
123 #include <netinet6/ip6_var.h>
125 #include <netinet/if_ether.h> /* for struct arpcom */
126 #include <net/bridge/if_bridgevar.h>
127 #include <net/if_llc.h>
128 #include <net/netmsg2.h>
130 #include <net/route.h>
131 #include <sys/in_cksum.h>
134 * Size of the route hash table. Must be a power of two.
136 #ifndef BRIDGE_RTHASH_SIZE
137 #define BRIDGE_RTHASH_SIZE 1024
140 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
143 * Maximum number of addresses to cache.
145 #ifndef BRIDGE_RTABLE_MAX
146 #define BRIDGE_RTABLE_MAX 100
150 * Spanning tree defaults.
152 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
153 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
154 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
155 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
156 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
157 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
158 #define BSTP_DEFAULT_PATH_COST 55
161 * Timeout (in seconds) for entries learned dynamically.
163 #ifndef BRIDGE_RTABLE_TIMEOUT
164 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
168 * Number of seconds between walks of the route list.
170 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
171 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
175 * List of capabilities to mask on the member interface.
177 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
179 eventhandler_tag bridge_detach_cookie
= NULL
;
181 extern struct mbuf
*(*bridge_input_p
)(struct ifnet
*, struct mbuf
*);
182 extern int (*bridge_output_p
)(struct ifnet
*, struct mbuf
*);
183 extern void (*bridge_dn_p
)(struct mbuf
*, struct ifnet
*);
185 static int bridge_rtable_prune_period
= BRIDGE_RTABLE_PRUNE_PERIOD
;
187 static int bridge_clone_create(struct if_clone
*, int);
188 static void bridge_clone_destroy(struct ifnet
*);
190 static int bridge_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
191 static void bridge_mutecaps(struct bridge_iflist
*, int);
192 static void bridge_ifdetach(void *arg __unused
, struct ifnet
*);
193 static void bridge_init(void *);
194 static void bridge_stop(struct ifnet
*);
195 static void bridge_start(struct ifnet
*);
196 static struct mbuf
*bridge_input(struct ifnet
*, struct mbuf
*);
197 static int bridge_output(struct ifnet
*, struct mbuf
*);
199 static void bridge_forward(struct bridge_softc
*, struct mbuf
*m
);
201 static void bridge_timer(void *);
203 static void bridge_broadcast(struct bridge_softc
*, struct ifnet
*,
205 static void bridge_span(struct bridge_softc
*, struct mbuf
*);
207 static int bridge_rtupdate(struct bridge_softc
*, const uint8_t *,
208 struct ifnet
*, int, uint8_t);
209 static struct ifnet
*bridge_rtlookup(struct bridge_softc
*, const uint8_t *);
210 static void bridge_rttrim(struct bridge_softc
*);
211 static void bridge_rtage(struct bridge_softc
*);
212 static void bridge_rtflush(struct bridge_softc
*, int);
213 static int bridge_rtdaddr(struct bridge_softc
*, const uint8_t *);
215 static int bridge_rtable_init(struct bridge_softc
*);
216 static void bridge_rtable_fini(struct bridge_softc
*);
218 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
219 static struct bridge_rtnode
*bridge_rtnode_lookup(struct bridge_softc
*,
221 static int bridge_rtnode_insert(struct bridge_softc
*,
222 struct bridge_rtnode
*);
223 static void bridge_rtnode_destroy(struct bridge_softc
*,
224 struct bridge_rtnode
*);
226 static struct bridge_iflist
*bridge_lookup_member(struct bridge_softc
*,
228 static struct bridge_iflist
*bridge_lookup_member_if(struct bridge_softc
*,
230 static void bridge_delete_member(struct bridge_softc
*,
231 struct bridge_iflist
*, int);
232 static void bridge_delete_span(struct bridge_softc
*,
233 struct bridge_iflist
*);
235 static int bridge_ioctl_add(struct bridge_softc
*, void *);
236 static int bridge_ioctl_del(struct bridge_softc
*, void *);
237 static int bridge_ioctl_gifflags(struct bridge_softc
*, void *);
238 static int bridge_ioctl_sifflags(struct bridge_softc
*, void *);
239 static int bridge_ioctl_scache(struct bridge_softc
*, void *);
240 static int bridge_ioctl_gcache(struct bridge_softc
*, void *);
241 static int bridge_ioctl_gifs(struct bridge_softc
*, void *);
242 static int bridge_ioctl_rts(struct bridge_softc
*, void *);
243 static int bridge_ioctl_saddr(struct bridge_softc
*, void *);
244 static int bridge_ioctl_sto(struct bridge_softc
*, void *);
245 static int bridge_ioctl_gto(struct bridge_softc
*, void *);
246 static int bridge_ioctl_daddr(struct bridge_softc
*, void *);
247 static int bridge_ioctl_flush(struct bridge_softc
*, void *);
248 static int bridge_ioctl_gpri(struct bridge_softc
*, void *);
249 static int bridge_ioctl_spri(struct bridge_softc
*, void *);
250 static int bridge_ioctl_ght(struct bridge_softc
*, void *);
251 static int bridge_ioctl_sht(struct bridge_softc
*, void *);
252 static int bridge_ioctl_gfd(struct bridge_softc
*, void *);
253 static int bridge_ioctl_sfd(struct bridge_softc
*, void *);
254 static int bridge_ioctl_gma(struct bridge_softc
*, void *);
255 static int bridge_ioctl_sma(struct bridge_softc
*, void *);
256 static int bridge_ioctl_sifprio(struct bridge_softc
*, void *);
257 static int bridge_ioctl_sifcost(struct bridge_softc
*, void *);
258 static int bridge_ioctl_addspan(struct bridge_softc
*, void *);
259 static int bridge_ioctl_delspan(struct bridge_softc
*, void *);
260 static int bridge_pfil(struct mbuf
**, struct ifnet
*, struct ifnet
*,
262 static int bridge_ip_checkbasic(struct mbuf
**mp
);
264 static int bridge_ip6_checkbasic(struct mbuf
**mp
);
266 static int bridge_fragment(struct ifnet
*, struct mbuf
*,
267 struct ether_header
*, int, struct llc
*);
268 static void bridge_enqueue_internal(struct ifnet
*, struct mbuf
*m
,
270 static void bridge_enqueue_handler(struct netmsg
*);
271 static void bridge_pfil_enqueue_handler(struct netmsg
*);
272 static void bridge_pfil_enqueue(struct ifnet
*, struct mbuf
*, int);
273 static void bridge_handoff_notags(struct ifnet
*, struct mbuf
*);
274 static void bridge_handoff(struct ifnet
*, struct mbuf
*);
276 SYSCTL_DECL(_net_link
);
277 SYSCTL_NODE(_net_link
, IFT_BRIDGE
, bridge
, CTLFLAG_RW
, 0, "Bridge");
279 static int pfil_onlyip
= 1; /* only pass IP[46] packets when pfil is enabled */
280 static int pfil_bridge
= 1; /* run pfil hooks on the bridge interface */
281 static int pfil_member
= 1; /* run pfil hooks on the member interface */
282 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_onlyip
, CTLFLAG_RW
,
283 &pfil_onlyip
, 0, "Only pass IP packets when pfil is enabled");
284 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_bridge
, CTLFLAG_RW
,
285 &pfil_bridge
, 0, "Packet filter on the bridge interface");
286 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_member
, CTLFLAG_RW
,
287 &pfil_member
, 0, "Packet filter on the member interface");
289 struct bridge_control
{
290 int (*bc_func
)(struct bridge_softc
*, void *);
295 #define BC_F_COPYIN 0x01 /* copy arguments in */
296 #define BC_F_COPYOUT 0x02 /* copy arguments out */
297 #define BC_F_SUSER 0x04 /* do super-user check */
299 const struct bridge_control bridge_control_table
[] = {
300 { bridge_ioctl_add
, sizeof(struct ifbreq
),
301 BC_F_COPYIN
|BC_F_SUSER
},
302 { bridge_ioctl_del
, sizeof(struct ifbreq
),
303 BC_F_COPYIN
|BC_F_SUSER
},
305 { bridge_ioctl_gifflags
, sizeof(struct ifbreq
),
306 BC_F_COPYIN
|BC_F_COPYOUT
},
307 { bridge_ioctl_sifflags
, sizeof(struct ifbreq
),
308 BC_F_COPYIN
|BC_F_SUSER
},
310 { bridge_ioctl_scache
, sizeof(struct ifbrparam
),
311 BC_F_COPYIN
|BC_F_SUSER
},
312 { bridge_ioctl_gcache
, sizeof(struct ifbrparam
),
315 { bridge_ioctl_gifs
, sizeof(struct ifbifconf
),
316 BC_F_COPYIN
|BC_F_COPYOUT
},
317 { bridge_ioctl_rts
, sizeof(struct ifbaconf
),
318 BC_F_COPYIN
|BC_F_COPYOUT
},
320 { bridge_ioctl_saddr
, sizeof(struct ifbareq
),
321 BC_F_COPYIN
|BC_F_SUSER
},
323 { bridge_ioctl_sto
, sizeof(struct ifbrparam
),
324 BC_F_COPYIN
|BC_F_SUSER
},
325 { bridge_ioctl_gto
, sizeof(struct ifbrparam
),
328 { bridge_ioctl_daddr
, sizeof(struct ifbareq
),
329 BC_F_COPYIN
|BC_F_SUSER
},
331 { bridge_ioctl_flush
, sizeof(struct ifbreq
),
332 BC_F_COPYIN
|BC_F_SUSER
},
334 { bridge_ioctl_gpri
, sizeof(struct ifbrparam
),
336 { bridge_ioctl_spri
, sizeof(struct ifbrparam
),
337 BC_F_COPYIN
|BC_F_SUSER
},
339 { bridge_ioctl_ght
, sizeof(struct ifbrparam
),
341 { bridge_ioctl_sht
, sizeof(struct ifbrparam
),
342 BC_F_COPYIN
|BC_F_SUSER
},
344 { bridge_ioctl_gfd
, sizeof(struct ifbrparam
),
346 { bridge_ioctl_sfd
, sizeof(struct ifbrparam
),
347 BC_F_COPYIN
|BC_F_SUSER
},
349 { bridge_ioctl_gma
, sizeof(struct ifbrparam
),
351 { bridge_ioctl_sma
, sizeof(struct ifbrparam
),
352 BC_F_COPYIN
|BC_F_SUSER
},
354 { bridge_ioctl_sifprio
, sizeof(struct ifbreq
),
355 BC_F_COPYIN
|BC_F_SUSER
},
357 { bridge_ioctl_sifcost
, sizeof(struct ifbreq
),
358 BC_F_COPYIN
|BC_F_SUSER
},
360 { bridge_ioctl_addspan
, sizeof(struct ifbreq
),
361 BC_F_COPYIN
|BC_F_SUSER
},
362 { bridge_ioctl_delspan
, sizeof(struct ifbreq
),
363 BC_F_COPYIN
|BC_F_SUSER
},
365 const int bridge_control_table_size
=
366 sizeof(bridge_control_table
) / sizeof(bridge_control_table
[0]);
368 LIST_HEAD(, bridge_softc
) bridge_list
;
370 struct if_clone bridge_cloner
= IF_CLONE_INITIALIZER("bridge",
372 bridge_clone_destroy
, 0, IF_MAXUNIT
);
375 bridge_modevent(module_t mod
, int type
, void *data
)
379 LIST_INIT(&bridge_list
);
380 if_clone_attach(&bridge_cloner
);
381 bridge_input_p
= bridge_input
;
382 bridge_output_p
= bridge_output
;
383 bridge_detach_cookie
= EVENTHANDLER_REGISTER(
384 ifnet_detach_event
, bridge_ifdetach
, NULL
,
385 EVENTHANDLER_PRI_ANY
);
387 bstp_linkstate_p
= bstp_linkstate
;
391 if (!LIST_EMPTY(&bridge_list
))
393 EVENTHANDLER_DEREGISTER(ifnet_detach_event
,
394 bridge_detach_cookie
);
395 if_clone_detach(&bridge_cloner
);
396 bridge_input_p
= NULL
;
397 bridge_output_p
= NULL
;
399 bstp_linkstate_p
= NULL
;
408 static moduledata_t bridge_mod
= {
414 DECLARE_MODULE(if_bridge
, bridge_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);
418 * bridge_clone_create:
420 * Create a new bridge instance.
423 bridge_clone_create(struct if_clone
*ifc
, int unit
)
425 struct bridge_softc
*sc
;
429 sc
= kmalloc(sizeof(*sc
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
430 ifp
= sc
->sc_ifp
= &sc
->sc_if
;
432 sc
->sc_brtmax
= BRIDGE_RTABLE_MAX
;
433 sc
->sc_brttimeout
= BRIDGE_RTABLE_TIMEOUT
;
434 sc
->sc_bridge_max_age
= BSTP_DEFAULT_MAX_AGE
;
435 sc
->sc_bridge_hello_time
= BSTP_DEFAULT_HELLO_TIME
;
436 sc
->sc_bridge_forward_delay
= BSTP_DEFAULT_FORWARD_DELAY
;
437 sc
->sc_bridge_priority
= BSTP_DEFAULT_BRIDGE_PRIORITY
;
438 sc
->sc_hold_time
= BSTP_DEFAULT_HOLD_TIME
;
440 /* Initialize our routing table. */
441 bridge_rtable_init(sc
);
443 callout_init(&sc
->sc_brcallout
);
444 callout_init(&sc
->sc_bstpcallout
);
446 LIST_INIT(&sc
->sc_iflist
);
447 LIST_INIT(&sc
->sc_spanlist
);
450 if_initname(ifp
, ifc
->ifc_name
, unit
);
451 ifp
->if_mtu
= ETHERMTU
;
452 ifp
->if_flags
= IFF_BROADCAST
| IFF_MULTICAST
;
453 ifp
->if_ioctl
= bridge_ioctl
;
454 ifp
->if_start
= bridge_start
;
455 ifp
->if_init
= bridge_init
;
456 ifp
->if_type
= IFT_BRIDGE
;
457 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
458 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
459 ifq_set_ready(&ifp
->if_snd
);
460 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
463 * Generate a random ethernet address and use the private AC:DE:48
467 int rnd
= karc4random();
468 bcopy(&rnd
, &eaddr
[0], 4); /* ETHER_ADDR_LEN == 6 */
470 bcopy(&rnd
, &eaddr
[2], 4); /* ETHER_ADDR_LEN == 6 */
472 eaddr
[0] &= ~1; /* clear multicast bit */
473 eaddr
[0] |= 2; /* set the LAA bit */
475 ether_ifattach(ifp
, eaddr
, NULL
);
476 /* Now undo some of the damage... */
477 ifp
->if_baudrate
= 0;
478 ifp
->if_type
= IFT_BRIDGE
;
481 LIST_INSERT_HEAD(&bridge_list
, sc
, sc_list
);
488 * bridge_clone_destroy:
490 * Destroy a bridge instance.
493 bridge_clone_destroy(struct ifnet
*ifp
)
495 struct bridge_softc
*sc
= ifp
->if_softc
;
496 struct bridge_iflist
*bif
;
498 lwkt_serialize_enter(ifp
->if_serializer
);
501 ifp
->if_flags
&= ~IFF_UP
;
503 while ((bif
= LIST_FIRST(&sc
->sc_iflist
)) != NULL
)
504 bridge_delete_member(sc
, bif
, 0);
506 while ((bif
= LIST_FIRST(&sc
->sc_spanlist
)) != NULL
) {
507 bridge_delete_span(sc
, bif
);
510 callout_stop(&sc
->sc_brcallout
);
511 callout_stop(&sc
->sc_bstpcallout
);
513 lwkt_serialize_exit(ifp
->if_serializer
);
516 LIST_REMOVE(sc
, sc_list
);
521 /* Tear down the routing table. */
522 bridge_rtable_fini(sc
);
530 * Handle a control request from the operator.
533 bridge_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
535 struct bridge_softc
*sc
= ifp
->if_softc
;
537 struct ifbreq ifbreq
;
538 struct ifbifconf ifbifconf
;
539 struct ifbareq ifbareq
;
540 struct ifbaconf ifbaconf
;
541 struct ifbrparam ifbrparam
;
543 struct ifdrv
*ifd
= (struct ifdrv
*) data
;
544 const struct bridge_control
*bc
;
547 ASSERT_SERIALIZED(ifp
->if_serializer
);
556 if (ifd
->ifd_cmd
>= bridge_control_table_size
) {
560 bc
= &bridge_control_table
[ifd
->ifd_cmd
];
562 if (cmd
== SIOCGDRVSPEC
&&
563 (bc
->bc_flags
& BC_F_COPYOUT
) == 0) {
566 } else if (cmd
== SIOCSDRVSPEC
&&
567 (bc
->bc_flags
& BC_F_COPYOUT
) != 0) {
572 if (bc
->bc_flags
& BC_F_SUSER
) {
573 error
= suser_cred(cr
, NULL_CRED_OKAY
);
578 if (ifd
->ifd_len
!= bc
->bc_argsize
||
579 ifd
->ifd_len
> sizeof(args
)) {
584 memset(&args
, 0, sizeof(args
));
585 if (bc
->bc_flags
& BC_F_COPYIN
) {
586 error
= copyin(ifd
->ifd_data
, &args
, ifd
->ifd_len
);
591 error
= bc
->bc_func(sc
, &args
);
595 if (bc
->bc_flags
& BC_F_COPYOUT
)
596 error
= copyout(&args
, ifd
->ifd_data
, ifd
->ifd_len
);
600 if (!(ifp
->if_flags
& IFF_UP
) &&
601 (ifp
->if_flags
& IFF_RUNNING
)) {
603 * If interface is marked down and it is running,
607 } else if ((ifp
->if_flags
& IFF_UP
) &&
608 !(ifp
->if_flags
& IFF_RUNNING
)) {
610 * If interface is marked up and it is stopped, then
618 /* Do not allow the MTU to be changed on the bridge */
623 error
= ether_ioctl(ifp
, cmd
, data
);
632 * Clear or restore unwanted capabilities on the member interface
635 bridge_mutecaps(struct bridge_iflist
*bif
, int mute
)
637 struct ifnet
*ifp
= bif
->bif_ifp
;
641 if (ifp
->if_ioctl
== NULL
)
644 bzero(&ifr
, sizeof(ifr
));
645 ifr
.ifr_reqcap
= ifp
->if_capenable
;
648 /* mask off and save capabilities */
649 bif
->bif_mutecap
= ifr
.ifr_reqcap
& BRIDGE_IFCAPS_MASK
;
650 if (bif
->bif_mutecap
!= 0)
651 ifr
.ifr_reqcap
&= ~BRIDGE_IFCAPS_MASK
;
653 /* restore muted capabilities */
654 ifr
.ifr_reqcap
|= bif
->bif_mutecap
;
656 if (bif
->bif_mutecap
!= 0) {
657 lwkt_serialize_enter(ifp
->if_serializer
);
658 error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFCAP
, (caddr_t
)&ifr
, NULL
);
659 lwkt_serialize_exit(ifp
->if_serializer
);
664 * bridge_lookup_member:
666 * Lookup a bridge member interface.
668 static struct bridge_iflist
*
669 bridge_lookup_member(struct bridge_softc
*sc
, const char *name
)
671 struct bridge_iflist
*bif
;
674 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
676 if (strcmp(ifp
->if_xname
, name
) == 0)
684 * bridge_lookup_member_if:
686 * Lookup a bridge member interface by ifnet*.
688 static struct bridge_iflist
*
689 bridge_lookup_member_if(struct bridge_softc
*sc
, struct ifnet
*member_ifp
)
691 struct bridge_iflist
*bif
;
693 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
694 if (bif
->bif_ifp
== member_ifp
)
702 * bridge_delete_member:
704 * Delete the specified member interface.
707 bridge_delete_member(struct bridge_softc
*sc
, struct bridge_iflist
*bif
,
710 struct ifnet
*ifs
= bif
->bif_ifp
;
713 switch (ifs
->if_type
) {
717 * Take the interface out of promiscuous mode.
719 (void) ifpromisc(ifs
, 0);
720 bridge_mutecaps(bif
, 0);
728 panic("bridge_delete_member: impossible");
734 ifs
->if_bridge
= NULL
;
735 LIST_REMOVE(bif
, bif_next
);
737 bridge_rtdelete(sc
, ifs
, IFBF_FLUSHALL
);
739 kfree(bif
, M_DEVBUF
);
741 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
742 bstp_initialization(sc
);
746 * bridge_delete_span:
748 * Delete the specified span interface.
751 bridge_delete_span(struct bridge_softc
*sc
, struct bridge_iflist
*bif
)
753 KASSERT(bif
->bif_ifp
->if_bridge
== NULL
,
754 ("%s: not a span interface", __func__
));
756 LIST_REMOVE(bif
, bif_next
);
757 kfree(bif
, M_DEVBUF
);
761 bridge_ioctl_add(struct bridge_softc
*sc
, void *arg
)
763 struct ifbreq
*req
= arg
;
764 struct bridge_iflist
*bif
= NULL
;
768 ifs
= ifunit(req
->ifbr_ifsname
);
772 /* If it's in the span list, it can't be a member. */
773 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
774 if (ifs
== bif
->bif_ifp
)
777 /* Allow the first Ethernet member to define the MTU */
778 if (ifs
->if_type
!= IFT_GIF
) {
779 if (LIST_EMPTY(&sc
->sc_iflist
))
780 sc
->sc_ifp
->if_mtu
= ifs
->if_mtu
;
781 else if (sc
->sc_ifp
->if_mtu
!= ifs
->if_mtu
) {
782 if_printf(sc
->sc_ifp
, "invalid MTU for %s\n",
788 if (ifs
->if_bridge
== sc
)
791 if (ifs
->if_bridge
!= NULL
)
794 bif
= kmalloc(sizeof(*bif
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
796 bif
->bif_flags
= IFBIF_LEARNING
| IFBIF_DISCOVER
;
797 bif
->bif_priority
= BSTP_DEFAULT_PORT_PRIORITY
;
798 bif
->bif_path_cost
= BSTP_DEFAULT_PATH_COST
;
800 switch (ifs
->if_type
) {
804 * Place the interface into promiscuous mode.
806 error
= ifpromisc(ifs
, 1);
810 bridge_mutecaps(bif
, 1);
813 case IFT_GIF
: /* :^) */
823 LIST_INSERT_HEAD(&sc
->sc_iflist
, bif
, bif_next
);
825 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
826 bstp_initialization(sc
);
833 kfree(bif
, M_DEVBUF
);
839 bridge_ioctl_del(struct bridge_softc
*sc
, void *arg
)
841 struct ifbreq
*req
= arg
;
842 struct bridge_iflist
*bif
;
844 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
848 bridge_delete_member(sc
, bif
, 0);
854 bridge_ioctl_gifflags(struct bridge_softc
*sc
, void *arg
)
856 struct ifbreq
*req
= arg
;
857 struct bridge_iflist
*bif
;
859 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
863 req
->ifbr_ifsflags
= bif
->bif_flags
;
864 req
->ifbr_state
= bif
->bif_state
;
865 req
->ifbr_priority
= bif
->bif_priority
;
866 req
->ifbr_path_cost
= bif
->bif_path_cost
;
867 req
->ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
873 bridge_ioctl_sifflags(struct bridge_softc
*sc
, void *arg
)
875 struct ifbreq
*req
= arg
;
876 struct bridge_iflist
*bif
;
878 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
882 if (req
->ifbr_ifsflags
& IFBIF_SPAN
)
883 /* SPAN is readonly */
886 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
887 switch (bif
->bif_ifp
->if_type
) {
889 /* These can do spanning tree. */
893 /* Nothing else can. */
898 bif
->bif_flags
= req
->ifbr_ifsflags
;
900 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
901 bstp_initialization(sc
);
907 bridge_ioctl_scache(struct bridge_softc
*sc
, void *arg
)
909 struct ifbrparam
*param
= arg
;
911 sc
->sc_brtmax
= param
->ifbrp_csize
;
918 bridge_ioctl_gcache(struct bridge_softc
*sc
, void *arg
)
920 struct ifbrparam
*param
= arg
;
922 param
->ifbrp_csize
= sc
->sc_brtmax
;
928 bridge_ioctl_gifs(struct bridge_softc
*sc
, void *arg
)
930 struct ifbifconf
*bifc
= arg
;
931 struct bridge_iflist
*bif
;
933 int count
, len
, error
= 0;
936 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
)
938 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
941 if (bifc
->ifbic_len
== 0) {
942 bifc
->ifbic_len
= sizeof(breq
) * count
;
947 len
= bifc
->ifbic_len
;
948 memset(&breq
, 0, sizeof breq
);
949 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
950 if (len
< sizeof(breq
))
953 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
954 sizeof(breq
.ifbr_ifsname
));
955 breq
.ifbr_ifsflags
= bif
->bif_flags
;
956 breq
.ifbr_state
= bif
->bif_state
;
957 breq
.ifbr_priority
= bif
->bif_priority
;
958 breq
.ifbr_path_cost
= bif
->bif_path_cost
;
959 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
960 error
= copyout(&breq
, bifc
->ifbic_req
+ count
, sizeof(breq
));
966 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
967 if (len
< sizeof(breq
))
970 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
971 sizeof(breq
.ifbr_ifsname
));
972 breq
.ifbr_ifsflags
= bif
->bif_flags
;
973 breq
.ifbr_state
= bif
->bif_state
;
974 breq
.ifbr_priority
= bif
->bif_priority
;
975 breq
.ifbr_path_cost
= bif
->bif_path_cost
;
976 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
977 error
= copyout(&breq
, bifc
->ifbic_req
+ count
, sizeof(breq
));
984 bifc
->ifbic_len
= sizeof(breq
) * count
;
989 bridge_ioctl_rts(struct bridge_softc
*sc
, void *arg
)
991 struct ifbaconf
*bac
= arg
;
992 struct bridge_rtnode
*brt
;
993 struct ifbareq bareq
;
994 int count
= 0, error
= 0, len
;
996 if (bac
->ifbac_len
== 0)
999 len
= bac
->ifbac_len
;
1000 memset(&bareq
, 0, sizeof(bareq
));
1001 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
1002 if (len
< sizeof(bareq
))
1004 strlcpy(bareq
.ifba_ifsname
, brt
->brt_ifp
->if_xname
,
1005 sizeof(bareq
.ifba_ifsname
));
1006 memcpy(bareq
.ifba_dst
, brt
->brt_addr
, sizeof(brt
->brt_addr
));
1007 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
&&
1008 time_second
< brt
->brt_expire
)
1009 bareq
.ifba_expire
= brt
->brt_expire
- time_second
;
1011 bareq
.ifba_expire
= 0;
1012 bareq
.ifba_flags
= brt
->brt_flags
;
1014 error
= copyout(&bareq
, bac
->ifbac_req
+ count
, sizeof(bareq
));
1018 len
-= sizeof(bareq
);
1021 bac
->ifbac_len
= sizeof(bareq
) * count
;
1026 bridge_ioctl_saddr(struct bridge_softc
*sc
, void *arg
)
1028 struct ifbareq
*req
= arg
;
1029 struct bridge_iflist
*bif
;
1032 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
1036 error
= bridge_rtupdate(sc
, req
->ifba_dst
, bif
->bif_ifp
, 1,
1043 bridge_ioctl_sto(struct bridge_softc
*sc
, void *arg
)
1045 struct ifbrparam
*param
= arg
;
1047 sc
->sc_brttimeout
= param
->ifbrp_ctime
;
1053 bridge_ioctl_gto(struct bridge_softc
*sc
, void *arg
)
1055 struct ifbrparam
*param
= arg
;
1057 param
->ifbrp_ctime
= sc
->sc_brttimeout
;
1063 bridge_ioctl_daddr(struct bridge_softc
*sc
, void *arg
)
1065 struct ifbareq
*req
= arg
;
1067 return (bridge_rtdaddr(sc
, req
->ifba_dst
));
1071 bridge_ioctl_flush(struct bridge_softc
*sc
, void *arg
)
1073 struct ifbreq
*req
= arg
;
1075 bridge_rtflush(sc
, req
->ifbr_ifsflags
);
1081 bridge_ioctl_gpri(struct bridge_softc
*sc
, void *arg
)
1083 struct ifbrparam
*param
= arg
;
1085 param
->ifbrp_prio
= sc
->sc_bridge_priority
;
1091 bridge_ioctl_spri(struct bridge_softc
*sc
, void *arg
)
1093 struct ifbrparam
*param
= arg
;
1095 sc
->sc_bridge_priority
= param
->ifbrp_prio
;
1097 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1098 bstp_initialization(sc
);
1104 bridge_ioctl_ght(struct bridge_softc
*sc
, void *arg
)
1106 struct ifbrparam
*param
= arg
;
1108 param
->ifbrp_hellotime
= sc
->sc_bridge_hello_time
>> 8;
1114 bridge_ioctl_sht(struct bridge_softc
*sc
, void *arg
)
1116 struct ifbrparam
*param
= arg
;
1118 if (param
->ifbrp_hellotime
== 0)
1120 sc
->sc_bridge_hello_time
= param
->ifbrp_hellotime
<< 8;
1122 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1123 bstp_initialization(sc
);
1129 bridge_ioctl_gfd(struct bridge_softc
*sc
, void *arg
)
1131 struct ifbrparam
*param
= arg
;
1133 param
->ifbrp_fwddelay
= sc
->sc_bridge_forward_delay
>> 8;
1139 bridge_ioctl_sfd(struct bridge_softc
*sc
, void *arg
)
1141 struct ifbrparam
*param
= arg
;
1143 if (param
->ifbrp_fwddelay
== 0)
1145 sc
->sc_bridge_forward_delay
= param
->ifbrp_fwddelay
<< 8;
1147 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1148 bstp_initialization(sc
);
1154 bridge_ioctl_gma(struct bridge_softc
*sc
, void *arg
)
1156 struct ifbrparam
*param
= arg
;
1158 param
->ifbrp_maxage
= sc
->sc_bridge_max_age
>> 8;
1164 bridge_ioctl_sma(struct bridge_softc
*sc
, void *arg
)
1166 struct ifbrparam
*param
= arg
;
1168 if (param
->ifbrp_maxage
== 0)
1170 sc
->sc_bridge_max_age
= param
->ifbrp_maxage
<< 8;
1172 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1173 bstp_initialization(sc
);
1179 bridge_ioctl_sifprio(struct bridge_softc
*sc
, void *arg
)
1181 struct ifbreq
*req
= arg
;
1182 struct bridge_iflist
*bif
;
1184 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1188 bif
->bif_priority
= req
->ifbr_priority
;
1190 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1191 bstp_initialization(sc
);
1197 bridge_ioctl_sifcost(struct bridge_softc
*sc
, void *arg
)
1199 struct ifbreq
*req
= arg
;
1200 struct bridge_iflist
*bif
;
1202 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1206 bif
->bif_path_cost
= req
->ifbr_path_cost
;
1208 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1209 bstp_initialization(sc
);
1215 bridge_ioctl_addspan(struct bridge_softc
*sc
, void *arg
)
1217 struct ifbreq
*req
= arg
;
1218 struct bridge_iflist
*bif
= NULL
;
1221 ifs
= ifunit(req
->ifbr_ifsname
);
1225 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1226 if (ifs
== bif
->bif_ifp
)
1229 if (ifs
->if_bridge
!= NULL
)
1232 switch (ifs
->if_type
) {
1241 bif
= kmalloc(sizeof(*bif
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
1244 bif
->bif_flags
= IFBIF_SPAN
;
1246 LIST_INSERT_HEAD(&sc
->sc_spanlist
, bif
, bif_next
);
1252 bridge_ioctl_delspan(struct bridge_softc
*sc
, void *arg
)
1254 struct ifbreq
*req
= arg
;
1255 struct bridge_iflist
*bif
;
1258 ifs
= ifunit(req
->ifbr_ifsname
);
1262 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1263 if (ifs
== bif
->bif_ifp
)
1269 bridge_delete_span(sc
, bif
);
1277 * Detach an interface from a bridge. Called when a member
1278 * interface is detaching.
1281 bridge_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
1283 struct bridge_softc
*sc
= ifp
->if_bridge
;
1284 struct bridge_iflist
*bif
;
1286 /* Check if the interface is a bridge member */
1288 lwkt_serialize_enter(ifp
->if_serializer
);
1290 bif
= bridge_lookup_member_if(sc
, ifp
);
1292 bridge_delete_member(sc
, bif
, 1);
1294 lwkt_serialize_exit(ifp
->if_serializer
);
1298 /* Check if the interface is a span port */
1299 LIST_FOREACH(sc
, &bridge_list
, sc_list
) {
1300 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1301 if (ifp
== bif
->bif_ifp
) {
1302 bridge_delete_span(sc
, bif
);
1311 * Initialize a bridge interface.
1314 bridge_init(void *xsc
)
1316 struct bridge_softc
*sc
= (struct bridge_softc
*)xsc
;
1317 struct ifnet
*ifp
= sc
->sc_ifp
;
1319 ASSERT_SERIALIZED(ifp
->if_serializer
);
1321 if (ifp
->if_flags
& IFF_RUNNING
)
1324 callout_reset(&sc
->sc_brcallout
, bridge_rtable_prune_period
* hz
,
1327 ifp
->if_flags
|= IFF_RUNNING
;
1328 bstp_initialization(sc
);
1335 * Stop the bridge interface.
1338 bridge_stop(struct ifnet
*ifp
)
1340 struct bridge_softc
*sc
= ifp
->if_softc
;
1342 ASSERT_SERIALIZED(ifp
->if_serializer
);
1344 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
1347 callout_stop(&sc
->sc_brcallout
);
1350 bridge_rtflush(sc
, IFBF_FLUSHDYN
);
1352 ifp
->if_flags
&= ~IFF_RUNNING
;
1356 bridge_enqueue_internal(struct ifnet
*dst_ifp
, struct mbuf
*m
,
1357 netisr_fn_t handler
)
1359 struct netmsg_packet
*nmp
;
1361 int cpu
= mycpu
->gd_cpuid
;
1363 while (m
->m_type
== MT_TAG
) {
1364 /* XXX see ether_output_frame for full rules check */
1368 nmp
= &m
->m_hdr
.mh_netmsg
;
1369 netmsg_init(&nmp
->nm_netmsg
, &netisr_apanic_rport
, 0, handler
);
1371 nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
= dst_ifp
;
1373 port
= cpu_portfn(cpu
);
1374 lwkt_sendmsg(port
, &nmp
->nm_netmsg
.nm_lmsg
);
1378 bridge_pfil_enqueue(struct ifnet
*dst_ifp
, struct mbuf
*m
,
1381 netisr_fn_t handler
;
1383 if (runfilt
&& (inet_pfil_hook
.ph_hashooks
> 0
1385 || inet6_pfil_hook
.ph_hashooks
> 0
1388 handler
= bridge_pfil_enqueue_handler
;
1390 handler
= bridge_enqueue_handler
;
1392 bridge_enqueue_internal(dst_ifp
, m
, handler
);
1398 * Enqueue a packet on a bridge member interface.
1402 bridge_enqueue(struct ifnet
*dst_ifp
, struct mbuf
*m
)
1404 bridge_enqueue_internal(dst_ifp
, m
, bridge_enqueue_handler
);
1410 * Send output from a bridge member interface. This
1411 * performs the bridging function for locally originated
1414 * The mbuf has the Ethernet header already attached. We must
1415 * enqueue or free the mbuf before returning.
1418 bridge_output(struct ifnet
*ifp
, struct mbuf
*m
)
1420 struct ether_header
*eh
;
1421 struct ifnet
*dst_if
;
1422 struct bridge_softc
*sc
;
1424 sc
= ifp
->if_bridge
;
1426 ASSERT_SERIALIZED(ifp
->if_serializer
);
1428 if (m
->m_len
< ETHER_HDR_LEN
) {
1429 m
= m_pullup(m
, ETHER_HDR_LEN
);
1435 * Serialize our bridge interface. We have to get rid of the
1436 * originating interface lock to avoid a deadlock.
1438 lwkt_serialize_exit(ifp
->if_serializer
);
1439 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
1441 eh
= mtod(m
, struct ether_header
*);
1444 * If bridge is down, but the original output interface is up,
1445 * go ahead and send out that interface. Otherwise, the packet
1448 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) == 0) {
1454 * If the packet is a multicast, or we don't know a better way to
1455 * get there, send to all interfaces.
1457 if (ETHER_IS_MULTICAST(eh
->ether_dhost
))
1460 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1461 if (dst_if
== NULL
) {
1462 struct bridge_iflist
*bif
;
1468 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1469 dst_if
= bif
->bif_ifp
;
1470 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1474 * If this is not the original output interface,
1475 * and the interface is participating in spanning
1476 * tree, make sure the port is in a state that
1477 * allows forwarding.
1479 if (dst_if
!= ifp
&&
1480 (bif
->bif_flags
& IFBIF_STP
) != 0) {
1481 switch (bif
->bif_state
) {
1482 case BSTP_IFSTATE_BLOCKING
:
1483 case BSTP_IFSTATE_LISTENING
:
1484 case BSTP_IFSTATE_DISABLED
:
1489 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
1493 mc
= m_copypacket(m
, MB_DONTWAIT
);
1495 sc
->sc_ifp
->if_oerrors
++;
1499 bridge_enqueue(dst_if
, mc
);
1503 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1509 * XXX Spanning tree consideration here?
1513 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1514 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
1517 bridge_enqueue(dst_if
, m
);
1520 lwkt_serialize_enter(ifp
->if_serializer
);
1527 * Start output on a bridge.
1531 bridge_start(struct ifnet
*ifp
)
1533 struct bridge_softc
*sc
= ifp
->if_softc
;
1535 ASSERT_SERIALIZED(ifp
->if_serializer
);
1537 ifp
->if_flags
|= IFF_OACTIVE
;
1539 struct ifnet
*dst_if
= NULL
;
1540 struct ether_header
*eh
;
1543 m
= ifq_dequeue(&ifp
->if_snd
, NULL
);
1547 if (m
->m_len
< sizeof(*eh
)) {
1548 m
= m_pullup(m
, sizeof(*eh
));
1554 eh
= mtod(m
, struct ether_header
*);
1559 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
1560 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1563 bridge_broadcast(sc
, ifp
, m
, 0);
1565 bridge_enqueue(dst_if
, m
);
1567 ifp
->if_flags
&= ~IFF_OACTIVE
;
1573 * The forwarding function of the bridge.
1576 bridge_forward(struct bridge_softc
*sc
, struct mbuf
*m
)
1578 struct bridge_iflist
*bif
;
1579 struct ifnet
*src_if
, *dst_if
, *ifp
;
1580 struct ether_header
*eh
;
1582 src_if
= m
->m_pkthdr
.rcvif
;
1585 ASSERT_SERIALIZED(ifp
->if_serializer
);
1588 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1591 * Look up the bridge_iflist.
1593 bif
= bridge_lookup_member_if(sc
, src_if
);
1595 /* Interface is not a bridge member (anymore?) */
1600 if (bif
->bif_flags
& IFBIF_STP
) {
1601 switch (bif
->bif_state
) {
1602 case BSTP_IFSTATE_BLOCKING
:
1603 case BSTP_IFSTATE_LISTENING
:
1604 case BSTP_IFSTATE_DISABLED
:
1610 eh
= mtod(m
, struct ether_header
*);
1613 * If the interface is learning, and the source
1614 * address is valid and not multicast, record
1617 if ((bif
->bif_flags
& IFBIF_LEARNING
) != 0 &&
1618 ETHER_IS_MULTICAST(eh
->ether_shost
) == 0 &&
1619 (eh
->ether_shost
[0] == 0 &&
1620 eh
->ether_shost
[1] == 0 &&
1621 eh
->ether_shost
[2] == 0 &&
1622 eh
->ether_shost
[3] == 0 &&
1623 eh
->ether_shost
[4] == 0 &&
1624 eh
->ether_shost
[5] == 0) == 0) {
1625 bridge_rtupdate(sc
, eh
->ether_shost
, src_if
, 0, IFBAF_DYNAMIC
);
1628 if ((bif
->bif_flags
& IFBIF_STP
) != 0 &&
1629 bif
->bif_state
== BSTP_IFSTATE_LEARNING
) {
1635 * At this point, the port either doesn't participate
1636 * in spanning tree or it is in the forwarding state.
1640 * If the packet is unicast, destined for someone on
1641 * "this" side of the bridge, drop it.
1643 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) {
1644 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1645 if (src_if
== dst_if
) {
1650 /* ...forward it to all interfaces. */
1651 sc
->sc_ifp
->if_imcasts
++;
1655 if (dst_if
== NULL
) {
1656 bridge_broadcast(sc
, src_if
, m
, 1);
1661 * At this point, we're dealing with a unicast frame
1662 * going to a different interface.
1664 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
1668 bif
= bridge_lookup_member_if(sc
, dst_if
);
1670 /* Not a member of the bridge (anymore?) */
1675 if (bif
->bif_flags
& IFBIF_STP
) {
1676 switch (bif
->bif_state
) {
1677 case BSTP_IFSTATE_DISABLED
:
1678 case BSTP_IFSTATE_BLOCKING
:
1684 lwkt_serialize_exit(ifp
->if_serializer
);
1686 /* run the packet filter */
1687 if (inet_pfil_hook
.ph_hashooks
> 0
1689 || inet6_pfil_hook
.ph_hashooks
> 0
1692 if (bridge_pfil(&m
, ifp
, src_if
, PFIL_IN
) != 0)
1697 if (bridge_pfil(&m
, ifp
, dst_if
, PFIL_OUT
) != 0)
1702 bridge_handoff(dst_if
, m
);
1705 * ifp's serializer was held on entry and is expected to be held
1709 lwkt_serialize_enter(ifp
->if_serializer
);
1715 * Receive input from a member interface. Queue the packet for
1716 * bridging if it is not for us.
1718 static struct mbuf
*
1719 bridge_input(struct ifnet
*ifp
, struct mbuf
*m
)
1721 struct bridge_softc
*sc
= ifp
->if_bridge
;
1722 struct bridge_iflist
*bif
;
1723 struct ifnet
*bifp
, *new_ifp
;
1724 struct ether_header
*eh
;
1725 struct mbuf
*mc
, *mc2
;
1730 lwkt_serialize_enter(bifp
->if_serializer
);
1732 if ((bifp
->if_flags
& IFF_RUNNING
) == 0)
1736 * Implement support for bridge monitoring. If this flag has been
1737 * set on this interface, discard the packet once we push it through
1738 * the bpf(4) machinery, but before we do, increment the byte and
1739 * packet counters associated with this interface.
1741 if ((bifp
->if_flags
& IFF_MONITOR
) != 0) {
1742 m
->m_pkthdr
.rcvif
= bifp
;
1744 bifp
->if_ipackets
++;
1745 bifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1751 eh
= mtod(m
, struct ether_header
*);
1753 m
->m_flags
&= ~M_PROTO1
; /* XXX Hack - loop prevention */
1755 if (memcmp(eh
->ether_dhost
, IF_LLADDR(bifp
), ETHER_ADDR_LEN
) == 0) {
1757 * If the packet is for us, set the packets source as the
1758 * bridge, and return the packet back to ifnet.if_input for
1761 KASSERT(bifp
->if_bridge
== NULL
,
1762 ("loop created in bridge_input"));
1768 * Tap all packets arriving on the bridge, no matter if
1769 * they are local destinations or not. In is in.
1773 bif
= bridge_lookup_member_if(sc
, ifp
);
1779 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
1780 /* Tap off 802.1D packets; they do not get forwarded. */
1781 if (memcmp(eh
->ether_dhost
, bstp_etheraddr
,
1782 ETHER_ADDR_LEN
) == 0) {
1783 m
= bstp_input(ifp
, m
);
1785 ("attempt to deliver 802.1D packet\n"));
1789 if (bif
->bif_flags
& IFBIF_STP
) {
1790 switch (bif
->bif_state
) {
1791 case BSTP_IFSTATE_BLOCKING
:
1792 case BSTP_IFSTATE_LISTENING
:
1793 case BSTP_IFSTATE_DISABLED
:
1799 * Make a deep copy of the packet and enqueue the copy
1800 * for bridge processing; return the original packet for
1803 mc
= m_dup(m
, MB_DONTWAIT
);
1807 bridge_forward(sc
, mc
);
1810 * Reinject the mbuf as arriving on the bridge so we have a
1811 * chance at claiming multicast packets. We can not loop back
1812 * here from ether_input as a bridge is never a member of a
1815 KASSERT(bifp
->if_bridge
== NULL
,
1816 ("loop created in bridge_input"));
1817 mc2
= m_dup(m
, MB_DONTWAIT
);
1820 /* Keep the layer3 header aligned */
1821 int i
= min(mc2
->m_pkthdr
.len
, max_protohdr
);
1822 mc2
= m_copyup(mc2
, i
, ETHER_ALIGN
);
1826 mc2
->m_pkthdr
.rcvif
= bifp
;
1827 bifp
->if_ipackets
++;
1828 bifp
->if_input(bifp
, mc2
);
1831 /* Return the original packet for local processing. */
1835 if (bif
->bif_flags
& IFBIF_STP
) {
1836 switch (bif
->bif_state
) {
1837 case BSTP_IFSTATE_BLOCKING
:
1838 case BSTP_IFSTATE_LISTENING
:
1839 case BSTP_IFSTATE_DISABLED
:
1845 * Unicast. Make sure it's not for us.
1847 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1848 if (bif
->bif_ifp
->if_type
!= IFT_ETHER
)
1851 /* It is destined for us. */
1852 if (memcmp(IF_LLADDR(bif
->bif_ifp
), eh
->ether_dhost
,
1853 ETHER_ADDR_LEN
) == 0) {
1854 if (bif
->bif_flags
& IFBIF_LEARNING
) {
1856 eh
->ether_shost
, ifp
, 0, IFBAF_DYNAMIC
);
1859 if (bif
->bif_ifp
!= ifp
) {
1860 /* XXX loop prevention */
1861 m
->m_flags
|= M_PROTO1
;
1862 new_ifp
= bif
->bif_ifp
;
1867 /* We just received a packet that we sent out. */
1868 if (memcmp(IF_LLADDR(bif
->bif_ifp
), eh
->ether_shost
,
1869 ETHER_ADDR_LEN
) == 0) {
1876 /* Perform the bridge forwarding function. */
1877 bridge_forward(sc
, m
);
1880 lwkt_serialize_exit(bifp
->if_serializer
);
1882 if (new_ifp
!= NULL
) {
1883 lwkt_serialize_enter(new_ifp
->if_serializer
);
1885 m
->m_pkthdr
.rcvif
= new_ifp
;
1886 new_ifp
->if_ipackets
++;
1887 new_ifp
->if_input(new_ifp
, m
);
1890 lwkt_serialize_exit(new_ifp
->if_serializer
);
1898 * Send a frame to all interfaces that are members of
1899 * the bridge, except for the one on which the packet
1903 bridge_broadcast(struct bridge_softc
*sc
, struct ifnet
*src_if
,
1904 struct mbuf
*m
, int runfilt
)
1906 struct bridge_iflist
*bif
;
1908 struct ifnet
*dst_if
, *bifp
;
1913 ASSERT_SERIALIZED(bifp
->if_serializer
);
1915 /* run the packet filter */
1916 if (runfilt
&& (inet_pfil_hook
.ph_hashooks
> 0
1918 || inet6_pfil_hook
.ph_hashooks
> 0
1921 lwkt_serialize_exit(bifp
->if_serializer
);
1923 /* Filter on the bridge interface before broadcasting */
1925 if (bridge_pfil(&m
, bifp
, src_if
, PFIL_IN
) != 0)
1930 if (bridge_pfil(&m
, bifp
, NULL
, PFIL_OUT
) != 0)
1933 lwkt_serialize_enter(bifp
->if_serializer
);
1938 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1939 dst_if
= bif
->bif_ifp
;
1940 if (dst_if
== src_if
)
1943 if (bif
->bif_flags
& IFBIF_STP
) {
1944 switch (bif
->bif_state
) {
1945 case BSTP_IFSTATE_BLOCKING
:
1946 case BSTP_IFSTATE_DISABLED
:
1951 if ((bif
->bif_flags
& IFBIF_DISCOVER
) == 0 &&
1952 (m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
1955 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1958 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
1962 mc
= m_copypacket(m
, MB_DONTWAIT
);
1964 sc
->sc_ifp
->if_oerrors
++;
1968 bridge_pfil_enqueue(dst_if
, mc
, runfilt
);
1977 * Duplicate a packet out one or more interfaces that are in span mode,
1978 * the original mbuf is unmodified.
1981 bridge_span(struct bridge_softc
*sc
, struct mbuf
*m
)
1983 struct bridge_iflist
*bif
;
1984 struct ifnet
*dst_if
;
1987 if (LIST_EMPTY(&sc
->sc_spanlist
))
1990 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
1991 dst_if
= bif
->bif_ifp
;
1993 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1996 mc
= m_copypacket(m
, MB_DONTWAIT
);
1998 sc
->sc_ifp
->if_oerrors
++;
2002 bridge_enqueue(dst_if
, mc
);
2009 * Add a bridge routing entry.
2010 * Can be called from interrupt context.
2013 bridge_rtupdate(struct bridge_softc
*sc
, const uint8_t *dst
,
2014 struct ifnet
*dst_if
, int setflags
, uint8_t flags
)
2016 struct bridge_rtnode
*brt
;
2020 * A route for this destination might already exist. If so,
2021 * update it, otherwise create a new one.
2023 if ((brt
= bridge_rtnode_lookup(sc
, dst
)) == NULL
) {
2024 if (sc
->sc_brtcnt
>= sc
->sc_brtmax
)
2028 * Allocate a new bridge forwarding node, and
2029 * initialize the expiration time and Ethernet
2032 brt
= kmalloc(sizeof(struct bridge_rtnode
), M_DEVBUF
,
2033 M_INTNOWAIT
|M_ZERO
);
2037 brt
->brt_flags
= IFBAF_DYNAMIC
;
2038 memcpy(brt
->brt_addr
, dst
, ETHER_ADDR_LEN
);
2040 if ((error
= bridge_rtnode_insert(sc
, brt
)) != 0) {
2041 kfree(brt
, M_DEVBUF
);
2046 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2047 brt
->brt_ifp
= dst_if
;
2048 if ((flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2049 brt
->brt_expire
= time_second
+ sc
->sc_brttimeout
;
2051 brt
->brt_flags
= flags
;
2059 * Lookup the destination interface for an address.
2061 static struct ifnet
*
2062 bridge_rtlookup(struct bridge_softc
*sc
, const uint8_t *addr
)
2064 struct bridge_rtnode
*brt
;
2066 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
2069 return (brt
->brt_ifp
);
2075 * Trim the routine table so that we have a number
2076 * of routing entries less than or equal to the
2080 bridge_rttrim(struct bridge_softc
*sc
)
2082 struct bridge_rtnode
*brt
, *nbrt
;
2084 /* Make sure we actually need to do this. */
2085 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2088 /* Force an aging cycle; this might trim enough addresses. */
2090 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2093 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2094 nbrt
= LIST_NEXT(brt
, brt_list
);
2095 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2096 bridge_rtnode_destroy(sc
, brt
);
2097 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2106 * Aging timer for the bridge.
2109 bridge_timer(void *arg
)
2111 struct bridge_softc
*sc
= arg
;
2113 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
2117 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
2118 callout_reset(&sc
->sc_brcallout
,
2119 bridge_rtable_prune_period
* hz
, bridge_timer
, sc
);
2121 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
2127 * Perform an aging cycle.
2130 bridge_rtage(struct bridge_softc
*sc
)
2132 struct bridge_rtnode
*brt
, *nbrt
;
2134 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2135 nbrt
= LIST_NEXT(brt
, brt_list
);
2136 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2137 if (time_second
>= brt
->brt_expire
)
2138 bridge_rtnode_destroy(sc
, brt
);
2146 * Remove all dynamic addresses from the bridge.
2149 bridge_rtflush(struct bridge_softc
*sc
, int full
)
2151 struct bridge_rtnode
*brt
, *nbrt
;
2153 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2154 nbrt
= LIST_NEXT(brt
, brt_list
);
2155 if (full
|| (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2156 bridge_rtnode_destroy(sc
, brt
);
2163 * Remove an address from the table.
2166 bridge_rtdaddr(struct bridge_softc
*sc
, const uint8_t *addr
)
2168 struct bridge_rtnode
*brt
;
2170 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
2173 bridge_rtnode_destroy(sc
, brt
);
2180 * Delete routes to a speicifc member interface.
2183 bridge_rtdelete(struct bridge_softc
*sc
, struct ifnet
*ifp
, int full
)
2185 struct bridge_rtnode
*brt
, *nbrt
;
2187 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2188 nbrt
= LIST_NEXT(brt
, brt_list
);
2189 if (brt
->brt_ifp
== ifp
&& (full
||
2190 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
))
2191 bridge_rtnode_destroy(sc
, brt
);
2196 * bridge_rtable_init:
2198 * Initialize the route table for this bridge.
2201 bridge_rtable_init(struct bridge_softc
*sc
)
2205 sc
->sc_rthash
= kmalloc(sizeof(*sc
->sc_rthash
) * BRIDGE_RTHASH_SIZE
,
2206 M_DEVBUF
, M_WAITOK
);
2208 for (i
= 0; i
< BRIDGE_RTHASH_SIZE
; i
++)
2209 LIST_INIT(&sc
->sc_rthash
[i
]);
2211 sc
->sc_rthash_key
= karc4random();
2213 LIST_INIT(&sc
->sc_rtlist
);
2219 * bridge_rtable_fini:
2221 * Deconstruct the route table for this bridge.
2224 bridge_rtable_fini(struct bridge_softc
*sc
)
2227 kfree(sc
->sc_rthash
, M_DEVBUF
);
2231 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2232 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2234 #define mix(a, b, c) \
2236 a -= b; a -= c; a ^= (c >> 13); \
2237 b -= c; b -= a; b ^= (a << 8); \
2238 c -= a; c -= b; c ^= (b >> 13); \
2239 a -= b; a -= c; a ^= (c >> 12); \
2240 b -= c; b -= a; b ^= (a << 16); \
2241 c -= a; c -= b; c ^= (b >> 5); \
2242 a -= b; a -= c; a ^= (c >> 3); \
2243 b -= c; b -= a; b ^= (a << 10); \
2244 c -= a; c -= b; c ^= (b >> 15); \
2245 } while (/*CONSTCOND*/0)
2247 static __inline
uint32_t
2248 bridge_rthash(struct bridge_softc
*sc
, const uint8_t *addr
)
2250 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= sc
->sc_rthash_key
;
2261 return (c
& BRIDGE_RTHASH_MASK
);
2267 bridge_rtnode_addr_cmp(const uint8_t *a
, const uint8_t *b
)
2271 for (i
= 0, d
= 0; i
< ETHER_ADDR_LEN
&& d
== 0; i
++) {
2272 d
= ((int)a
[i
]) - ((int)b
[i
]);
2279 * bridge_rtnode_lookup:
2281 * Look up a bridge route node for the specified destination.
2283 static struct bridge_rtnode
*
2284 bridge_rtnode_lookup(struct bridge_softc
*sc
, const uint8_t *addr
)
2286 struct bridge_rtnode
*brt
;
2290 hash
= bridge_rthash(sc
, addr
);
2291 LIST_FOREACH(brt
, &sc
->sc_rthash
[hash
], brt_hash
) {
2292 dir
= bridge_rtnode_addr_cmp(addr
, brt
->brt_addr
);
2303 * bridge_rtnode_insert:
2305 * Insert the specified bridge node into the route table. We
2306 * assume the entry is not already in the table.
2309 bridge_rtnode_insert(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2311 struct bridge_rtnode
*lbrt
;
2315 hash
= bridge_rthash(sc
, brt
->brt_addr
);
2317 lbrt
= LIST_FIRST(&sc
->sc_rthash
[hash
]);
2319 LIST_INSERT_HEAD(&sc
->sc_rthash
[hash
], brt
, brt_hash
);
2324 dir
= bridge_rtnode_addr_cmp(brt
->brt_addr
, lbrt
->brt_addr
);
2328 LIST_INSERT_BEFORE(lbrt
, brt
, brt_hash
);
2331 if (LIST_NEXT(lbrt
, brt_hash
) == NULL
) {
2332 LIST_INSERT_AFTER(lbrt
, brt
, brt_hash
);
2335 lbrt
= LIST_NEXT(lbrt
, brt_hash
);
2336 } while (lbrt
!= NULL
);
2339 panic("bridge_rtnode_insert: impossible");
2343 LIST_INSERT_HEAD(&sc
->sc_rtlist
, brt
, brt_list
);
2350 * bridge_rtnode_destroy:
2352 * Destroy a bridge rtnode.
2355 bridge_rtnode_destroy(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2358 LIST_REMOVE(brt
, brt_hash
);
2360 LIST_REMOVE(brt
, brt_list
);
2362 kfree(brt
, M_DEVBUF
);
2366 * Send bridge packets through pfil if they are one of the types pfil can deal
2367 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2368 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2372 bridge_pfil(struct mbuf
**mp
, struct ifnet
*bifp
, struct ifnet
*ifp
, int dir
)
2374 int snap
, error
, i
, hlen
;
2375 struct ether_header
*eh1
, eh2
;
2378 u_int16_t ether_type
;
2381 error
= -1; /* Default error if not error == 0 */
2383 if (pfil_bridge
== 0 && pfil_member
== 0)
2384 return (0); /* filtering is disabled */
2386 i
= min((*mp
)->m_pkthdr
.len
, max_protohdr
);
2387 if ((*mp
)->m_len
< i
) {
2388 *mp
= m_pullup(*mp
, i
);
2390 kprintf("%s: m_pullup failed\n", __func__
);
2395 eh1
= mtod(*mp
, struct ether_header
*);
2396 ether_type
= ntohs(eh1
->ether_type
);
2399 * Check for SNAP/LLC.
2401 if (ether_type
< ETHERMTU
) {
2402 struct llc
*llc2
= (struct llc
*)(eh1
+ 1);
2404 if ((*mp
)->m_len
>= ETHER_HDR_LEN
+ 8 &&
2405 llc2
->llc_dsap
== LLC_SNAP_LSAP
&&
2406 llc2
->llc_ssap
== LLC_SNAP_LSAP
&&
2407 llc2
->llc_control
== LLC_UI
) {
2408 ether_type
= htons(llc2
->llc_un
.type_snap
.ether_type
);
2414 * If we're trying to filter bridge traffic, don't look at anything
2415 * other than IP and ARP traffic. If the filter doesn't understand
2416 * IPv6, don't allow IPv6 through the bridge either. This is lame
2417 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2418 * but of course we don't have an AppleTalk filter to begin with.
2419 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2422 switch (ether_type
) {
2424 case ETHERTYPE_REVARP
:
2425 return (0); /* Automatically pass */
2428 case ETHERTYPE_IPV6
:
2433 * Check to see if the user wants to pass non-ip
2434 * packets, these will not be checked by pfil(9) and
2435 * passed unconditionally so the default is to drop.
2441 /* Strip off the Ethernet header and keep a copy. */
2442 m_copydata(*mp
, 0, ETHER_HDR_LEN
, (caddr_t
) &eh2
);
2443 m_adj(*mp
, ETHER_HDR_LEN
);
2445 /* Strip off snap header, if present */
2447 m_copydata(*mp
, 0, sizeof(struct llc
), (caddr_t
) &llc1
);
2448 m_adj(*mp
, sizeof(struct llc
));
2452 * Check the IP header for alignment and errors
2454 if (dir
== PFIL_IN
) {
2455 switch (ether_type
) {
2457 error
= bridge_ip_checkbasic(mp
);
2460 case ETHERTYPE_IPV6
:
2461 error
= bridge_ip6_checkbasic(mp
);
2474 * Run the packet through pfil
2480 * before calling the firewall, swap fields the same as
2481 * IP does. here we assume the header is contiguous
2483 ip
= mtod(*mp
, struct ip
*);
2485 ip
->ip_len
= ntohs(ip
->ip_len
);
2486 ip
->ip_off
= ntohs(ip
->ip_off
);
2489 * Run pfil on the member interface and the bridge, both can
2490 * be skipped by clearing pfil_member or pfil_bridge.
2493 * in_if -> bridge_if -> out_if
2495 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
2496 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
2499 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2502 if (pfil_member
&& ifp
!= NULL
)
2503 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, ifp
,
2506 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2509 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
2510 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
2513 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2516 /* check if we need to fragment the packet */
2517 if (pfil_member
&& ifp
!= NULL
&& dir
== PFIL_OUT
) {
2518 i
= (*mp
)->m_pkthdr
.len
;
2519 if (i
> ifp
->if_mtu
) {
2520 error
= bridge_fragment(ifp
, *mp
, &eh2
, snap
,
2526 /* Recalculate the ip checksum and restore byte ordering */
2527 ip
= mtod(*mp
, struct ip
*);
2528 hlen
= ip
->ip_hl
<< 2;
2529 if (hlen
< sizeof(struct ip
))
2531 if (hlen
> (*mp
)->m_len
) {
2532 if ((*mp
= m_pullup(*mp
, hlen
)) == 0)
2534 ip
= mtod(*mp
, struct ip
*);
2538 ip
->ip_len
= htons(ip
->ip_len
);
2539 ip
->ip_off
= htons(ip
->ip_off
);
2541 if (hlen
== sizeof(struct ip
))
2542 ip
->ip_sum
= in_cksum_hdr(ip
);
2544 ip
->ip_sum
= in_cksum(*mp
, hlen
);
2548 case ETHERTYPE_IPV6
:
2549 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
2550 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
2553 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2556 if (pfil_member
&& ifp
!= NULL
)
2557 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, ifp
,
2560 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2563 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
2564 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
2581 * Finally, put everything back the way it was and return
2584 M_PREPEND(*mp
, sizeof(struct llc
), MB_DONTWAIT
);
2587 bcopy(&llc1
, mtod(*mp
, caddr_t
), sizeof(struct llc
));
2590 M_PREPEND(*mp
, ETHER_HDR_LEN
, MB_DONTWAIT
);
2593 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
2604 * Perform basic checks on header size since
2605 * pfil assumes ip_input has already processed
2606 * it for it. Cut-and-pasted from ip_input.c.
2607 * Given how simple the IPv6 version is,
2608 * does the IPv4 version really need to be
2611 * XXX Should we update ipstat here, or not?
2612 * XXX Right now we update ipstat but not
2616 bridge_ip_checkbasic(struct mbuf
**mp
)
2618 struct mbuf
*m
= *mp
;
2626 if (IP_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
2627 if ((m
= m_copyup(m
, sizeof(struct ip
),
2628 (max_linkhdr
+ 3) & ~3)) == NULL
) {
2629 /* XXXJRT new stat, please */
2630 ipstat
.ips_toosmall
++;
2635 #ifndef __predict_false
2636 #define __predict_false(x) x
2638 if (__predict_false(m
->m_len
< sizeof (struct ip
))) {
2639 if ((m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
2640 ipstat
.ips_toosmall
++;
2644 ip
= mtod(m
, struct ip
*);
2645 if (ip
== NULL
) goto bad
;
2647 if (ip
->ip_v
!= IPVERSION
) {
2648 ipstat
.ips_badvers
++;
2651 hlen
= ip
->ip_hl
<< 2;
2652 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
2653 ipstat
.ips_badhlen
++;
2656 if (hlen
> m
->m_len
) {
2657 if ((m
= m_pullup(m
, hlen
)) == 0) {
2658 ipstat
.ips_badhlen
++;
2661 ip
= mtod(m
, struct ip
*);
2662 if (ip
== NULL
) goto bad
;
2665 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
2666 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
2668 if (hlen
== sizeof(struct ip
)) {
2669 sum
= in_cksum_hdr(ip
);
2671 sum
= in_cksum(m
, hlen
);
2675 ipstat
.ips_badsum
++;
2679 /* Retrieve the packet length. */
2680 len
= ntohs(ip
->ip_len
);
2683 * Check for additional length bogosity
2686 ipstat
.ips_badlen
++;
2691 * Check that the amount of data in the buffers
2692 * is as at least much as the IP header would have us expect.
2693 * Drop packet if shorter than we expect.
2695 if (m
->m_pkthdr
.len
< len
) {
2696 ipstat
.ips_tooshort
++;
2700 /* Checks out, proceed */
2711 * Same as above, but for IPv6.
2712 * Cut-and-pasted from ip6_input.c.
2713 * XXX Should we update ip6stat, or not?
2716 bridge_ip6_checkbasic(struct mbuf
**mp
)
2718 struct mbuf
*m
= *mp
;
2719 struct ip6_hdr
*ip6
;
2722 * If the IPv6 header is not aligned, slurp it up into a new
2723 * mbuf with space for link headers, in the event we forward
2724 * it. Otherwise, if it is aligned, make sure the entire base
2725 * IPv6 header is in the first mbuf of the chain.
2728 if (IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
2729 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
2730 if ((m
= m_copyup(m
, sizeof(struct ip6_hdr
),
2731 (max_linkhdr
+ 3) & ~3)) == NULL
) {
2732 /* XXXJRT new stat, please */
2733 ip6stat
.ip6s_toosmall
++;
2734 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
2739 if (__predict_false(m
->m_len
< sizeof(struct ip6_hdr
))) {
2740 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
2741 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == NULL
) {
2742 ip6stat
.ip6s_toosmall
++;
2743 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
2748 ip6
= mtod(m
, struct ip6_hdr
*);
2750 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
2751 ip6stat
.ip6s_badvers
++;
2752 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
2756 /* Checks out, proceed */
2769 * Return a fragmented mbuf chain.
2772 bridge_fragment(struct ifnet
*ifp
, struct mbuf
*m
, struct ether_header
*eh
,
2773 int snap
, struct llc
*llc
)
2779 if (m
->m_len
< sizeof(struct ip
) &&
2780 (m
= m_pullup(m
, sizeof(struct ip
))) == NULL
)
2782 ip
= mtod(m
, struct ip
*);
2784 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
,
2789 /* walk the chain and re-add the Ethernet header */
2790 for (m0
= m
; m0
; m0
= m0
->m_nextpkt
) {
2793 M_PREPEND(m0
, sizeof(struct llc
), MB_DONTWAIT
);
2798 bcopy(llc
, mtod(m0
, caddr_t
),
2799 sizeof(struct llc
));
2801 M_PREPEND(m0
, ETHER_HDR_LEN
, MB_DONTWAIT
);
2806 bcopy(eh
, mtod(m0
, caddr_t
), ETHER_HDR_LEN
);
2812 ipstat
.ips_fragmented
++;
2823 bridge_enqueue_handler(struct netmsg
*nmsg
)
2825 struct netmsg_packet
*nmp
;
2826 struct ifnet
*dst_ifp
;
2829 nmp
= (struct netmsg_packet
*)nmsg
;
2831 dst_ifp
= nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
;
2833 bridge_handoff_notags(dst_ifp
, m
);
2837 bridge_pfil_enqueue_handler(struct netmsg
*nmsg
)
2839 struct netmsg_packet
*nmp
;
2840 struct ifnet
*dst_ifp
;
2843 nmp
= (struct netmsg_packet
*)nmsg
;
2845 dst_ifp
= nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
;
2848 * Filter on the output interface. Pass a NULL bridge interface
2849 * pointer so we do not redundantly filter on the bridge for
2850 * each interface we broadcast on.
2852 if (inet_pfil_hook
.ph_hashooks
> 0
2854 || inet6_pfil_hook
.ph_hashooks
> 0
2857 if (bridge_pfil(&m
, NULL
, dst_ifp
, PFIL_OUT
) != 0)
2862 bridge_handoff_notags(dst_ifp
, m
);
2866 bridge_handoff(struct ifnet
*dst_ifp
, struct mbuf
*m
)
2868 while (m
->m_type
== MT_TAG
) {
2869 /* XXX see ether_output_frame for full rules check */
2872 bridge_handoff_notags(dst_ifp
, m
);
2876 bridge_handoff_notags(struct ifnet
*dst_ifp
, struct mbuf
*m
)
2880 KKASSERT(m
->m_type
!= MT_TAG
);
2882 lwkt_serialize_enter(dst_ifp
->if_serializer
);
2884 /* We may be sending a fragment so traverse the mbuf */
2886 struct altq_pktattr pktattr
;
2889 m
->m_nextpkt
= NULL
;
2891 if (ifq_is_enabled(&dst_ifp
->if_snd
))
2892 altq_etherclassify(&dst_ifp
->if_snd
, m
, &pktattr
);
2894 ifq_handoff(dst_ifp
, m
, &pktattr
);
2897 lwkt_serialize_exit(dst_ifp
->if_serializer
);