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.25 2007/06/16 15:27:27 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>
115 #include <netinet/in.h> /* for struct arpcom */
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_var.h>
121 #include <netinet/ip6.h>
122 #include <netinet6/ip6_var.h>
124 #include <netinet/if_ether.h> /* for struct arpcom */
125 #include <net/bridge/if_bridgevar.h>
126 #include <net/if_llc.h>
127 #include <net/netmsg2.h>
129 #include <net/route.h>
130 #include <sys/in_cksum.h>
133 * Size of the route hash table. Must be a power of two.
135 #ifndef BRIDGE_RTHASH_SIZE
136 #define BRIDGE_RTHASH_SIZE 1024
139 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
142 * Maximum number of addresses to cache.
144 #ifndef BRIDGE_RTABLE_MAX
145 #define BRIDGE_RTABLE_MAX 100
149 * Spanning tree defaults.
151 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
152 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
153 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
154 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
155 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
156 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
157 #define BSTP_DEFAULT_PATH_COST 55
160 * Timeout (in seconds) for entries learned dynamically.
162 #ifndef BRIDGE_RTABLE_TIMEOUT
163 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
167 * Number of seconds between walks of the route list.
169 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
170 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
174 * List of capabilities to mask on the member interface.
176 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
178 eventhandler_tag bridge_detach_cookie
= NULL
;
180 extern struct mbuf
*(*bridge_input_p
)(struct ifnet
*, struct mbuf
*);
181 extern int (*bridge_output_p
)(struct ifnet
*, struct mbuf
*,
182 struct sockaddr
*, struct rtentry
*);
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_serialized(struct ifnet
*, struct mbuf
*,
198 struct sockaddr
*, struct rtentry
*);
200 static void bridge_forward(struct bridge_softc
*, struct mbuf
*m
);
202 static void bridge_timer(void *);
204 static void bridge_broadcast(struct bridge_softc
*, struct ifnet
*,
206 static void bridge_span(struct bridge_softc
*, struct mbuf
*);
208 static int bridge_rtupdate(struct bridge_softc
*, const uint8_t *,
209 struct ifnet
*, int, uint8_t);
210 static struct ifnet
*bridge_rtlookup(struct bridge_softc
*, const uint8_t *);
211 static void bridge_rttrim(struct bridge_softc
*);
212 static void bridge_rtage(struct bridge_softc
*);
213 static void bridge_rtflush(struct bridge_softc
*, int);
214 static int bridge_rtdaddr(struct bridge_softc
*, const uint8_t *);
216 static int bridge_rtable_init(struct bridge_softc
*);
217 static void bridge_rtable_fini(struct bridge_softc
*);
219 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
220 static struct bridge_rtnode
*bridge_rtnode_lookup(struct bridge_softc
*,
222 static int bridge_rtnode_insert(struct bridge_softc
*,
223 struct bridge_rtnode
*);
224 static void bridge_rtnode_destroy(struct bridge_softc
*,
225 struct bridge_rtnode
*);
227 static struct bridge_iflist
*bridge_lookup_member(struct bridge_softc
*,
229 static struct bridge_iflist
*bridge_lookup_member_if(struct bridge_softc
*,
231 static void bridge_delete_member(struct bridge_softc
*,
232 struct bridge_iflist
*, int);
233 static void bridge_delete_span(struct bridge_softc
*,
234 struct bridge_iflist
*);
236 static int bridge_ioctl_add(struct bridge_softc
*, void *);
237 static int bridge_ioctl_del(struct bridge_softc
*, void *);
238 static int bridge_ioctl_gifflags(struct bridge_softc
*, void *);
239 static int bridge_ioctl_sifflags(struct bridge_softc
*, void *);
240 static int bridge_ioctl_scache(struct bridge_softc
*, void *);
241 static int bridge_ioctl_gcache(struct bridge_softc
*, void *);
242 static int bridge_ioctl_gifs(struct bridge_softc
*, void *);
243 static int bridge_ioctl_rts(struct bridge_softc
*, void *);
244 static int bridge_ioctl_saddr(struct bridge_softc
*, void *);
245 static int bridge_ioctl_sto(struct bridge_softc
*, void *);
246 static int bridge_ioctl_gto(struct bridge_softc
*, void *);
247 static int bridge_ioctl_daddr(struct bridge_softc
*, void *);
248 static int bridge_ioctl_flush(struct bridge_softc
*, void *);
249 static int bridge_ioctl_gpri(struct bridge_softc
*, void *);
250 static int bridge_ioctl_spri(struct bridge_softc
*, void *);
251 static int bridge_ioctl_ght(struct bridge_softc
*, void *);
252 static int bridge_ioctl_sht(struct bridge_softc
*, void *);
253 static int bridge_ioctl_gfd(struct bridge_softc
*, void *);
254 static int bridge_ioctl_sfd(struct bridge_softc
*, void *);
255 static int bridge_ioctl_gma(struct bridge_softc
*, void *);
256 static int bridge_ioctl_sma(struct bridge_softc
*, void *);
257 static int bridge_ioctl_sifprio(struct bridge_softc
*, void *);
258 static int bridge_ioctl_sifcost(struct bridge_softc
*, void *);
259 static int bridge_ioctl_addspan(struct bridge_softc
*, void *);
260 static int bridge_ioctl_delspan(struct bridge_softc
*, void *);
261 static int bridge_pfil(struct mbuf
**, struct ifnet
*, struct ifnet
*,
263 static int bridge_ip_checkbasic(struct mbuf
**mp
);
265 static int bridge_ip6_checkbasic(struct mbuf
**mp
);
267 static int bridge_fragment(struct ifnet
*, struct mbuf
*,
268 struct ether_header
*, int, struct llc
*);
269 static void bridge_enqueue_internal(struct ifnet
*, struct mbuf
*m
,
271 static void bridge_enqueue_handler(struct netmsg
*);
272 static void bridge_pfil_enqueue_handler(struct netmsg
*);
273 static void bridge_pfil_enqueue(struct ifnet
*, struct mbuf
*, int);
274 static void bridge_handoff_notags(struct ifnet
*, struct mbuf
*);
275 static void bridge_handoff(struct ifnet
*, struct mbuf
*);
277 SYSCTL_DECL(_net_link
);
278 SYSCTL_NODE(_net_link
, IFT_BRIDGE
, bridge
, CTLFLAG_RW
, 0, "Bridge");
280 static int pfil_onlyip
= 1; /* only pass IP[46] packets when pfil is enabled */
281 static int pfil_bridge
= 1; /* run pfil hooks on the bridge interface */
282 static int pfil_member
= 1; /* run pfil hooks on the member interface */
283 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_onlyip
, CTLFLAG_RW
,
284 &pfil_onlyip
, 0, "Only pass IP packets when pfil is enabled");
285 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_bridge
, CTLFLAG_RW
,
286 &pfil_bridge
, 0, "Packet filter on the bridge interface");
287 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_member
, CTLFLAG_RW
,
288 &pfil_member
, 0, "Packet filter on the member interface");
290 struct bridge_control
{
291 int (*bc_func
)(struct bridge_softc
*, void *);
296 #define BC_F_COPYIN 0x01 /* copy arguments in */
297 #define BC_F_COPYOUT 0x02 /* copy arguments out */
298 #define BC_F_SUSER 0x04 /* do super-user check */
300 const struct bridge_control bridge_control_table
[] = {
301 { bridge_ioctl_add
, sizeof(struct ifbreq
),
302 BC_F_COPYIN
|BC_F_SUSER
},
303 { bridge_ioctl_del
, sizeof(struct ifbreq
),
304 BC_F_COPYIN
|BC_F_SUSER
},
306 { bridge_ioctl_gifflags
, sizeof(struct ifbreq
),
307 BC_F_COPYIN
|BC_F_COPYOUT
},
308 { bridge_ioctl_sifflags
, sizeof(struct ifbreq
),
309 BC_F_COPYIN
|BC_F_SUSER
},
311 { bridge_ioctl_scache
, sizeof(struct ifbrparam
),
312 BC_F_COPYIN
|BC_F_SUSER
},
313 { bridge_ioctl_gcache
, sizeof(struct ifbrparam
),
316 { bridge_ioctl_gifs
, sizeof(struct ifbifconf
),
317 BC_F_COPYIN
|BC_F_COPYOUT
},
318 { bridge_ioctl_rts
, sizeof(struct ifbaconf
),
319 BC_F_COPYIN
|BC_F_COPYOUT
},
321 { bridge_ioctl_saddr
, sizeof(struct ifbareq
),
322 BC_F_COPYIN
|BC_F_SUSER
},
324 { bridge_ioctl_sto
, sizeof(struct ifbrparam
),
325 BC_F_COPYIN
|BC_F_SUSER
},
326 { bridge_ioctl_gto
, sizeof(struct ifbrparam
),
329 { bridge_ioctl_daddr
, sizeof(struct ifbareq
),
330 BC_F_COPYIN
|BC_F_SUSER
},
332 { bridge_ioctl_flush
, sizeof(struct ifbreq
),
333 BC_F_COPYIN
|BC_F_SUSER
},
335 { bridge_ioctl_gpri
, sizeof(struct ifbrparam
),
337 { bridge_ioctl_spri
, sizeof(struct ifbrparam
),
338 BC_F_COPYIN
|BC_F_SUSER
},
340 { bridge_ioctl_ght
, sizeof(struct ifbrparam
),
342 { bridge_ioctl_sht
, sizeof(struct ifbrparam
),
343 BC_F_COPYIN
|BC_F_SUSER
},
345 { bridge_ioctl_gfd
, sizeof(struct ifbrparam
),
347 { bridge_ioctl_sfd
, sizeof(struct ifbrparam
),
348 BC_F_COPYIN
|BC_F_SUSER
},
350 { bridge_ioctl_gma
, sizeof(struct ifbrparam
),
352 { bridge_ioctl_sma
, sizeof(struct ifbrparam
),
353 BC_F_COPYIN
|BC_F_SUSER
},
355 { bridge_ioctl_sifprio
, sizeof(struct ifbreq
),
356 BC_F_COPYIN
|BC_F_SUSER
},
358 { bridge_ioctl_sifcost
, sizeof(struct ifbreq
),
359 BC_F_COPYIN
|BC_F_SUSER
},
361 { bridge_ioctl_addspan
, sizeof(struct ifbreq
),
362 BC_F_COPYIN
|BC_F_SUSER
},
363 { bridge_ioctl_delspan
, sizeof(struct ifbreq
),
364 BC_F_COPYIN
|BC_F_SUSER
},
366 const int bridge_control_table_size
=
367 sizeof(bridge_control_table
) / sizeof(bridge_control_table
[0]);
369 LIST_HEAD(, bridge_softc
) bridge_list
;
371 struct if_clone bridge_cloner
= IF_CLONE_INITIALIZER("bridge",
373 bridge_clone_destroy
, 0, IF_MAXUNIT
);
376 bridge_modevent(module_t mod
, int type
, void *data
)
380 LIST_INIT(&bridge_list
);
381 if_clone_attach(&bridge_cloner
);
382 bridge_input_p
= bridge_input
;
383 bridge_output_p
= bridge_output_serialized
;
384 bridge_detach_cookie
= EVENTHANDLER_REGISTER(
385 ifnet_detach_event
, bridge_ifdetach
, NULL
,
386 EVENTHANDLER_PRI_ANY
);
388 bstp_linkstate_p
= bstp_linkstate
;
392 if (!LIST_EMPTY(&bridge_list
))
394 EVENTHANDLER_DEREGISTER(ifnet_detach_event
,
395 bridge_detach_cookie
);
396 if_clone_detach(&bridge_cloner
);
397 bridge_input_p
= NULL
;
398 bridge_output_p
= NULL
;
400 bstp_linkstate_p
= NULL
;
409 static moduledata_t bridge_mod
= {
415 DECLARE_MODULE(if_bridge
, bridge_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);
419 * bridge_clone_create:
421 * Create a new bridge instance.
424 bridge_clone_create(struct if_clone
*ifc
, int unit
)
426 struct bridge_softc
*sc
;
430 sc
= kmalloc(sizeof(*sc
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
431 ifp
= sc
->sc_ifp
= &sc
->sc_if
;
433 sc
->sc_brtmax
= BRIDGE_RTABLE_MAX
;
434 sc
->sc_brttimeout
= BRIDGE_RTABLE_TIMEOUT
;
435 sc
->sc_bridge_max_age
= BSTP_DEFAULT_MAX_AGE
;
436 sc
->sc_bridge_hello_time
= BSTP_DEFAULT_HELLO_TIME
;
437 sc
->sc_bridge_forward_delay
= BSTP_DEFAULT_FORWARD_DELAY
;
438 sc
->sc_bridge_priority
= BSTP_DEFAULT_BRIDGE_PRIORITY
;
439 sc
->sc_hold_time
= BSTP_DEFAULT_HOLD_TIME
;
441 /* Initialize our routing table. */
442 bridge_rtable_init(sc
);
444 callout_init(&sc
->sc_brcallout
);
445 callout_init(&sc
->sc_bstpcallout
);
447 LIST_INIT(&sc
->sc_iflist
);
448 LIST_INIT(&sc
->sc_spanlist
);
451 if_initname(ifp
, ifc
->ifc_name
, unit
);
452 ifp
->if_mtu
= ETHERMTU
;
453 ifp
->if_flags
= IFF_BROADCAST
| IFF_MULTICAST
;
454 ifp
->if_ioctl
= bridge_ioctl
;
455 ifp
->if_start
= bridge_start
;
456 ifp
->if_init
= bridge_init
;
457 ifp
->if_type
= IFT_BRIDGE
;
458 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
459 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
460 ifq_set_ready(&ifp
->if_snd
);
461 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
464 * Generate a random ethernet address and use the private AC:DE:48
468 int rnd
= karc4random();
469 bcopy(&rnd
, &eaddr
[0], 4); /* ETHER_ADDR_LEN == 6 */
471 bcopy(&rnd
, &eaddr
[2], 4); /* ETHER_ADDR_LEN == 6 */
473 eaddr
[0] &= ~1; /* clear multicast bit */
474 eaddr
[0] |= 2; /* set the LAA bit */
476 ether_ifattach(ifp
, eaddr
, NULL
);
477 /* Now undo some of the damage... */
478 ifp
->if_baudrate
= 0;
479 ifp
->if_type
= IFT_BRIDGE
;
482 LIST_INSERT_HEAD(&bridge_list
, sc
, sc_list
);
489 * bridge_clone_destroy:
491 * Destroy a bridge instance.
494 bridge_clone_destroy(struct ifnet
*ifp
)
496 struct bridge_softc
*sc
= ifp
->if_softc
;
497 struct bridge_iflist
*bif
;
499 lwkt_serialize_enter(ifp
->if_serializer
);
502 ifp
->if_flags
&= ~IFF_UP
;
504 while ((bif
= LIST_FIRST(&sc
->sc_iflist
)) != NULL
)
505 bridge_delete_member(sc
, bif
, 0);
507 while ((bif
= LIST_FIRST(&sc
->sc_spanlist
)) != NULL
) {
508 bridge_delete_span(sc
, bif
);
511 callout_stop(&sc
->sc_brcallout
);
512 callout_stop(&sc
->sc_bstpcallout
);
514 lwkt_serialize_exit(ifp
->if_serializer
);
517 LIST_REMOVE(sc
, sc_list
);
522 /* Tear down the routing table. */
523 bridge_rtable_fini(sc
);
531 * Handle a control request from the operator.
534 bridge_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
536 struct bridge_softc
*sc
= ifp
->if_softc
;
538 struct ifbreq ifbreq
;
539 struct ifbifconf ifbifconf
;
540 struct ifbareq ifbareq
;
541 struct ifbaconf ifbaconf
;
542 struct ifbrparam ifbrparam
;
544 struct ifdrv
*ifd
= (struct ifdrv
*) data
;
545 const struct bridge_control
*bc
;
548 ASSERT_SERIALIZED(ifp
->if_serializer
);
557 if (ifd
->ifd_cmd
>= bridge_control_table_size
) {
561 bc
= &bridge_control_table
[ifd
->ifd_cmd
];
563 if (cmd
== SIOCGDRVSPEC
&&
564 (bc
->bc_flags
& BC_F_COPYOUT
) == 0) {
567 } else if (cmd
== SIOCSDRVSPEC
&&
568 (bc
->bc_flags
& BC_F_COPYOUT
) != 0) {
573 if (bc
->bc_flags
& BC_F_SUSER
) {
574 error
= suser_cred(cr
, NULL_CRED_OKAY
);
579 if (ifd
->ifd_len
!= bc
->bc_argsize
||
580 ifd
->ifd_len
> sizeof(args
)) {
585 memset(&args
, 0, sizeof(args
));
586 if (bc
->bc_flags
& BC_F_COPYIN
) {
587 error
= copyin(ifd
->ifd_data
, &args
, ifd
->ifd_len
);
592 error
= bc
->bc_func(sc
, &args
);
596 if (bc
->bc_flags
& BC_F_COPYOUT
)
597 error
= copyout(&args
, ifd
->ifd_data
, ifd
->ifd_len
);
601 if (!(ifp
->if_flags
& IFF_UP
) &&
602 (ifp
->if_flags
& IFF_RUNNING
)) {
604 * If interface is marked down and it is running,
608 } else if ((ifp
->if_flags
& IFF_UP
) &&
609 !(ifp
->if_flags
& IFF_RUNNING
)) {
611 * If interface is marked up and it is stopped, then
619 /* Do not allow the MTU to be changed on the bridge */
624 error
= ether_ioctl(ifp
, cmd
, data
);
633 * Clear or restore unwanted capabilities on the member interface
636 bridge_mutecaps(struct bridge_iflist
*bif
, int mute
)
638 struct ifnet
*ifp
= bif
->bif_ifp
;
642 if (ifp
->if_ioctl
== NULL
)
645 bzero(&ifr
, sizeof(ifr
));
646 ifr
.ifr_reqcap
= ifp
->if_capenable
;
649 /* mask off and save capabilities */
650 bif
->bif_mutecap
= ifr
.ifr_reqcap
& BRIDGE_IFCAPS_MASK
;
651 if (bif
->bif_mutecap
!= 0)
652 ifr
.ifr_reqcap
&= ~BRIDGE_IFCAPS_MASK
;
654 /* restore muted capabilities */
655 ifr
.ifr_reqcap
|= bif
->bif_mutecap
;
657 if (bif
->bif_mutecap
!= 0) {
658 lwkt_serialize_enter(ifp
->if_serializer
);
659 error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFCAP
, (caddr_t
)&ifr
, NULL
);
660 lwkt_serialize_exit(ifp
->if_serializer
);
665 * bridge_lookup_member:
667 * Lookup a bridge member interface.
669 static struct bridge_iflist
*
670 bridge_lookup_member(struct bridge_softc
*sc
, const char *name
)
672 struct bridge_iflist
*bif
;
675 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
677 if (strcmp(ifp
->if_xname
, name
) == 0)
685 * bridge_lookup_member_if:
687 * Lookup a bridge member interface by ifnet*.
689 static struct bridge_iflist
*
690 bridge_lookup_member_if(struct bridge_softc
*sc
, struct ifnet
*member_ifp
)
692 struct bridge_iflist
*bif
;
694 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
695 if (bif
->bif_ifp
== member_ifp
)
703 * bridge_delete_member:
705 * Delete the specified member interface.
708 bridge_delete_member(struct bridge_softc
*sc
, struct bridge_iflist
*bif
,
711 struct ifnet
*ifs
= bif
->bif_ifp
;
714 switch (ifs
->if_type
) {
718 * Take the interface out of promiscuous mode.
720 (void) ifpromisc(ifs
, 0);
721 bridge_mutecaps(bif
, 0);
729 panic("bridge_delete_member: impossible");
735 ifs
->if_bridge
= NULL
;
736 LIST_REMOVE(bif
, bif_next
);
738 bridge_rtdelete(sc
, ifs
, IFBF_FLUSHALL
);
740 kfree(bif
, M_DEVBUF
);
742 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
743 bstp_initialization(sc
);
747 * bridge_delete_span:
749 * Delete the specified span interface.
752 bridge_delete_span(struct bridge_softc
*sc
, struct bridge_iflist
*bif
)
754 KASSERT(bif
->bif_ifp
->if_bridge
== NULL
,
755 ("%s: not a span interface", __func__
));
757 LIST_REMOVE(bif
, bif_next
);
758 kfree(bif
, M_DEVBUF
);
762 bridge_ioctl_add(struct bridge_softc
*sc
, void *arg
)
764 struct ifbreq
*req
= arg
;
765 struct bridge_iflist
*bif
= NULL
;
769 ifs
= ifunit(req
->ifbr_ifsname
);
773 /* If it's in the span list, it can't be a member. */
774 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
775 if (ifs
== bif
->bif_ifp
)
778 /* Allow the first Ethernet member to define the MTU */
779 if (ifs
->if_type
!= IFT_GIF
) {
780 if (LIST_EMPTY(&sc
->sc_iflist
))
781 sc
->sc_ifp
->if_mtu
= ifs
->if_mtu
;
782 else if (sc
->sc_ifp
->if_mtu
!= ifs
->if_mtu
) {
783 if_printf(sc
->sc_ifp
, "invalid MTU for %s\n",
789 if (ifs
->if_bridge
== sc
)
792 if (ifs
->if_bridge
!= NULL
)
795 bif
= kmalloc(sizeof(*bif
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
797 bif
->bif_flags
= IFBIF_LEARNING
| IFBIF_DISCOVER
;
798 bif
->bif_priority
= BSTP_DEFAULT_PORT_PRIORITY
;
799 bif
->bif_path_cost
= BSTP_DEFAULT_PATH_COST
;
801 switch (ifs
->if_type
) {
805 * Place the interface into promiscuous mode.
807 error
= ifpromisc(ifs
, 1);
811 bridge_mutecaps(bif
, 1);
814 case IFT_GIF
: /* :^) */
824 LIST_INSERT_HEAD(&sc
->sc_iflist
, bif
, bif_next
);
826 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
827 bstp_initialization(sc
);
834 kfree(bif
, M_DEVBUF
);
840 bridge_ioctl_del(struct bridge_softc
*sc
, void *arg
)
842 struct ifbreq
*req
= arg
;
843 struct bridge_iflist
*bif
;
845 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
849 bridge_delete_member(sc
, bif
, 0);
855 bridge_ioctl_gifflags(struct bridge_softc
*sc
, void *arg
)
857 struct ifbreq
*req
= arg
;
858 struct bridge_iflist
*bif
;
860 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
864 req
->ifbr_ifsflags
= bif
->bif_flags
;
865 req
->ifbr_state
= bif
->bif_state
;
866 req
->ifbr_priority
= bif
->bif_priority
;
867 req
->ifbr_path_cost
= bif
->bif_path_cost
;
868 req
->ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
874 bridge_ioctl_sifflags(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 if (req
->ifbr_ifsflags
& IFBIF_SPAN
)
884 /* SPAN is readonly */
887 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
888 switch (bif
->bif_ifp
->if_type
) {
890 /* These can do spanning tree. */
894 /* Nothing else can. */
899 bif
->bif_flags
= req
->ifbr_ifsflags
;
901 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
902 bstp_initialization(sc
);
908 bridge_ioctl_scache(struct bridge_softc
*sc
, void *arg
)
910 struct ifbrparam
*param
= arg
;
912 sc
->sc_brtmax
= param
->ifbrp_csize
;
919 bridge_ioctl_gcache(struct bridge_softc
*sc
, void *arg
)
921 struct ifbrparam
*param
= arg
;
923 param
->ifbrp_csize
= sc
->sc_brtmax
;
929 bridge_ioctl_gifs(struct bridge_softc
*sc
, void *arg
)
931 struct ifbifconf
*bifc
= arg
;
932 struct bridge_iflist
*bif
;
934 int count
, len
, error
= 0;
937 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
)
939 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
942 if (bifc
->ifbic_len
== 0) {
943 bifc
->ifbic_len
= sizeof(breq
) * count
;
948 len
= bifc
->ifbic_len
;
949 memset(&breq
, 0, sizeof breq
);
950 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
951 if (len
< sizeof(breq
))
954 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
955 sizeof(breq
.ifbr_ifsname
));
956 breq
.ifbr_ifsflags
= bif
->bif_flags
;
957 breq
.ifbr_state
= bif
->bif_state
;
958 breq
.ifbr_priority
= bif
->bif_priority
;
959 breq
.ifbr_path_cost
= bif
->bif_path_cost
;
960 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
961 error
= copyout(&breq
, bifc
->ifbic_req
+ count
, sizeof(breq
));
967 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
968 if (len
< sizeof(breq
))
971 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
972 sizeof(breq
.ifbr_ifsname
));
973 breq
.ifbr_ifsflags
= bif
->bif_flags
;
974 breq
.ifbr_state
= bif
->bif_state
;
975 breq
.ifbr_priority
= bif
->bif_priority
;
976 breq
.ifbr_path_cost
= bif
->bif_path_cost
;
977 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
978 error
= copyout(&breq
, bifc
->ifbic_req
+ count
, sizeof(breq
));
985 bifc
->ifbic_len
= sizeof(breq
) * count
;
990 bridge_ioctl_rts(struct bridge_softc
*sc
, void *arg
)
992 struct ifbaconf
*bac
= arg
;
993 struct bridge_rtnode
*brt
;
994 struct ifbareq bareq
;
995 int count
= 0, error
= 0, len
;
997 if (bac
->ifbac_len
== 0)
1000 len
= bac
->ifbac_len
;
1001 memset(&bareq
, 0, sizeof(bareq
));
1002 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
1003 if (len
< sizeof(bareq
))
1005 strlcpy(bareq
.ifba_ifsname
, brt
->brt_ifp
->if_xname
,
1006 sizeof(bareq
.ifba_ifsname
));
1007 memcpy(bareq
.ifba_dst
, brt
->brt_addr
, sizeof(brt
->brt_addr
));
1008 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
&&
1009 time_second
< brt
->brt_expire
)
1010 bareq
.ifba_expire
= brt
->brt_expire
- time_second
;
1012 bareq
.ifba_expire
= 0;
1013 bareq
.ifba_flags
= brt
->brt_flags
;
1015 error
= copyout(&bareq
, bac
->ifbac_req
+ count
, sizeof(bareq
));
1019 len
-= sizeof(bareq
);
1022 bac
->ifbac_len
= sizeof(bareq
) * count
;
1027 bridge_ioctl_saddr(struct bridge_softc
*sc
, void *arg
)
1029 struct ifbareq
*req
= arg
;
1030 struct bridge_iflist
*bif
;
1033 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
1037 error
= bridge_rtupdate(sc
, req
->ifba_dst
, bif
->bif_ifp
, 1,
1044 bridge_ioctl_sto(struct bridge_softc
*sc
, void *arg
)
1046 struct ifbrparam
*param
= arg
;
1048 sc
->sc_brttimeout
= param
->ifbrp_ctime
;
1054 bridge_ioctl_gto(struct bridge_softc
*sc
, void *arg
)
1056 struct ifbrparam
*param
= arg
;
1058 param
->ifbrp_ctime
= sc
->sc_brttimeout
;
1064 bridge_ioctl_daddr(struct bridge_softc
*sc
, void *arg
)
1066 struct ifbareq
*req
= arg
;
1068 return (bridge_rtdaddr(sc
, req
->ifba_dst
));
1072 bridge_ioctl_flush(struct bridge_softc
*sc
, void *arg
)
1074 struct ifbreq
*req
= arg
;
1076 bridge_rtflush(sc
, req
->ifbr_ifsflags
);
1082 bridge_ioctl_gpri(struct bridge_softc
*sc
, void *arg
)
1084 struct ifbrparam
*param
= arg
;
1086 param
->ifbrp_prio
= sc
->sc_bridge_priority
;
1092 bridge_ioctl_spri(struct bridge_softc
*sc
, void *arg
)
1094 struct ifbrparam
*param
= arg
;
1096 sc
->sc_bridge_priority
= param
->ifbrp_prio
;
1098 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1099 bstp_initialization(sc
);
1105 bridge_ioctl_ght(struct bridge_softc
*sc
, void *arg
)
1107 struct ifbrparam
*param
= arg
;
1109 param
->ifbrp_hellotime
= sc
->sc_bridge_hello_time
>> 8;
1115 bridge_ioctl_sht(struct bridge_softc
*sc
, void *arg
)
1117 struct ifbrparam
*param
= arg
;
1119 if (param
->ifbrp_hellotime
== 0)
1121 sc
->sc_bridge_hello_time
= param
->ifbrp_hellotime
<< 8;
1123 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1124 bstp_initialization(sc
);
1130 bridge_ioctl_gfd(struct bridge_softc
*sc
, void *arg
)
1132 struct ifbrparam
*param
= arg
;
1134 param
->ifbrp_fwddelay
= sc
->sc_bridge_forward_delay
>> 8;
1140 bridge_ioctl_sfd(struct bridge_softc
*sc
, void *arg
)
1142 struct ifbrparam
*param
= arg
;
1144 if (param
->ifbrp_fwddelay
== 0)
1146 sc
->sc_bridge_forward_delay
= param
->ifbrp_fwddelay
<< 8;
1148 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1149 bstp_initialization(sc
);
1155 bridge_ioctl_gma(struct bridge_softc
*sc
, void *arg
)
1157 struct ifbrparam
*param
= arg
;
1159 param
->ifbrp_maxage
= sc
->sc_bridge_max_age
>> 8;
1165 bridge_ioctl_sma(struct bridge_softc
*sc
, void *arg
)
1167 struct ifbrparam
*param
= arg
;
1169 if (param
->ifbrp_maxage
== 0)
1171 sc
->sc_bridge_max_age
= param
->ifbrp_maxage
<< 8;
1173 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1174 bstp_initialization(sc
);
1180 bridge_ioctl_sifprio(struct bridge_softc
*sc
, void *arg
)
1182 struct ifbreq
*req
= arg
;
1183 struct bridge_iflist
*bif
;
1185 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1189 bif
->bif_priority
= req
->ifbr_priority
;
1191 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1192 bstp_initialization(sc
);
1198 bridge_ioctl_sifcost(struct bridge_softc
*sc
, void *arg
)
1200 struct ifbreq
*req
= arg
;
1201 struct bridge_iflist
*bif
;
1203 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1207 bif
->bif_path_cost
= req
->ifbr_path_cost
;
1209 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1210 bstp_initialization(sc
);
1216 bridge_ioctl_addspan(struct bridge_softc
*sc
, void *arg
)
1218 struct ifbreq
*req
= arg
;
1219 struct bridge_iflist
*bif
= NULL
;
1222 ifs
= ifunit(req
->ifbr_ifsname
);
1226 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1227 if (ifs
== bif
->bif_ifp
)
1230 if (ifs
->if_bridge
!= NULL
)
1233 switch (ifs
->if_type
) {
1242 bif
= kmalloc(sizeof(*bif
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
1247 bif
->bif_flags
= IFBIF_SPAN
;
1249 LIST_INSERT_HEAD(&sc
->sc_spanlist
, bif
, bif_next
);
1255 bridge_ioctl_delspan(struct bridge_softc
*sc
, void *arg
)
1257 struct ifbreq
*req
= arg
;
1258 struct bridge_iflist
*bif
;
1261 ifs
= ifunit(req
->ifbr_ifsname
);
1265 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1266 if (ifs
== bif
->bif_ifp
)
1272 bridge_delete_span(sc
, bif
);
1280 * Detach an interface from a bridge. Called when a member
1281 * interface is detaching.
1284 bridge_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
1286 struct bridge_softc
*sc
= ifp
->if_bridge
;
1287 struct bridge_iflist
*bif
;
1289 /* Check if the interface is a bridge member */
1291 lwkt_serialize_enter(ifp
->if_serializer
);
1293 bif
= bridge_lookup_member_if(sc
, ifp
);
1295 bridge_delete_member(sc
, bif
, 1);
1297 lwkt_serialize_exit(ifp
->if_serializer
);
1301 /* Check if the interface is a span port */
1302 LIST_FOREACH(sc
, &bridge_list
, sc_list
) {
1303 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1304 if (ifp
== bif
->bif_ifp
) {
1305 bridge_delete_span(sc
, bif
);
1314 * Initialize a bridge interface.
1317 bridge_init(void *xsc
)
1319 struct bridge_softc
*sc
= (struct bridge_softc
*)xsc
;
1320 struct ifnet
*ifp
= sc
->sc_ifp
;
1322 ASSERT_SERIALIZED(ifp
->if_serializer
);
1324 if (ifp
->if_flags
& IFF_RUNNING
)
1327 callout_reset(&sc
->sc_brcallout
, bridge_rtable_prune_period
* hz
,
1330 ifp
->if_flags
|= IFF_RUNNING
;
1331 bstp_initialization(sc
);
1338 * Stop the bridge interface.
1341 bridge_stop(struct ifnet
*ifp
)
1343 struct bridge_softc
*sc
= ifp
->if_softc
;
1345 ASSERT_SERIALIZED(ifp
->if_serializer
);
1347 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
1350 callout_stop(&sc
->sc_brcallout
);
1353 bridge_rtflush(sc
, IFBF_FLUSHDYN
);
1355 ifp
->if_flags
&= ~IFF_RUNNING
;
1359 bridge_enqueue_internal(struct ifnet
*dst_ifp
, struct mbuf
*m
,
1360 netisr_fn_t handler
)
1362 struct netmsg_packet
*nmp
;
1364 int cpu
= mycpu
->gd_cpuid
;
1366 while (m
->m_type
== MT_TAG
) {
1367 /* XXX see ether_output_frame for full rules check */
1371 nmp
= &m
->m_hdr
.mh_netmsg
;
1372 netmsg_init(&nmp
->nm_netmsg
, &netisr_apanic_rport
, 0, handler
);
1374 nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
= dst_ifp
;
1376 port
= cpu_portfn(cpu
);
1377 lwkt_sendmsg(port
, &nmp
->nm_netmsg
.nm_lmsg
);
1381 bridge_pfil_enqueue(struct ifnet
*dst_ifp
, struct mbuf
*m
,
1384 netisr_fn_t handler
;
1386 if (runfilt
&& (inet_pfil_hook
.ph_hashooks
> 0
1388 || inet6_pfil_hook
.ph_hashooks
> 0
1391 handler
= bridge_pfil_enqueue_handler
;
1393 handler
= bridge_enqueue_handler
;
1395 bridge_enqueue_internal(dst_ifp
, m
, handler
);
1401 * Enqueue a packet on a bridge member interface.
1405 bridge_enqueue(struct ifnet
*dst_ifp
, struct mbuf
*m
)
1407 bridge_enqueue_internal(dst_ifp
, m
, bridge_enqueue_handler
);
1411 * bridge_output_serialized:
1413 * Send output from a bridge member interface. This
1414 * performs the bridging function for locally originated
1417 * The mbuf has the Ethernet header already attached. We must
1418 * enqueue or free the mbuf before returning.
1421 bridge_output_serialized(struct ifnet
*ifp
, struct mbuf
*m
,
1422 struct sockaddr
*sa
, struct rtentry
*rt
)
1424 struct ether_header
*eh
;
1425 struct ifnet
*dst_if
;
1426 struct bridge_softc
*sc
;
1428 sc
= ifp
->if_bridge
;
1430 ASSERT_SERIALIZED(ifp
->if_serializer
);
1432 if (m
->m_len
< ETHER_HDR_LEN
) {
1433 m
= m_pullup(m
, ETHER_HDR_LEN
);
1439 * Serialize our bridge interface. We have to get rid of the
1440 * originating interface lock to avoid a deadlock.
1442 lwkt_serialize_exit(ifp
->if_serializer
);
1443 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
1445 eh
= mtod(m
, struct ether_header
*);
1448 * If bridge is down, but the original output interface is up,
1449 * go ahead and send out that interface. Otherwise, the packet
1452 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) == 0) {
1458 * If the packet is a multicast, or we don't know a better way to
1459 * get there, send to all interfaces.
1461 if (ETHER_IS_MULTICAST(eh
->ether_dhost
))
1464 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1465 if (dst_if
== NULL
) {
1466 struct bridge_iflist
*bif
;
1472 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1473 dst_if
= bif
->bif_ifp
;
1474 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1478 * If this is not the original output interface,
1479 * and the interface is participating in spanning
1480 * tree, make sure the port is in a state that
1481 * allows forwarding.
1483 if (dst_if
!= ifp
&&
1484 (bif
->bif_flags
& IFBIF_STP
) != 0) {
1485 switch (bif
->bif_state
) {
1486 case BSTP_IFSTATE_BLOCKING
:
1487 case BSTP_IFSTATE_LISTENING
:
1488 case BSTP_IFSTATE_DISABLED
:
1493 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
1497 mc
= m_copypacket(m
, MB_DONTWAIT
);
1499 sc
->sc_ifp
->if_oerrors
++;
1503 bridge_enqueue(dst_if
, mc
);
1507 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1513 * XXX Spanning tree consideration here?
1517 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1518 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
1521 bridge_enqueue(dst_if
, m
);
1524 lwkt_serialize_enter(ifp
->if_serializer
);
1531 * Start output on a bridge.
1535 bridge_start(struct ifnet
*ifp
)
1537 struct bridge_softc
*sc
= ifp
->if_softc
;
1539 ASSERT_SERIALIZED(ifp
->if_serializer
);
1541 ifp
->if_flags
|= IFF_OACTIVE
;
1543 struct ifnet
*dst_if
= NULL
;
1544 struct ether_header
*eh
;
1547 m
= ifq_dequeue(&ifp
->if_snd
, NULL
);
1551 if (m
->m_len
< sizeof(*eh
)) {
1552 m
= m_pullup(m
, sizeof(*eh
));
1558 eh
= mtod(m
, struct ether_header
*);
1563 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
1564 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1567 bridge_broadcast(sc
, ifp
, m
, 0);
1569 bridge_enqueue(dst_if
, m
);
1571 ifp
->if_flags
&= ~IFF_OACTIVE
;
1577 * The forwarding function of the bridge.
1580 bridge_forward(struct bridge_softc
*sc
, struct mbuf
*m
)
1582 struct bridge_iflist
*bif
;
1583 struct ifnet
*src_if
, *dst_if
, *ifp
;
1584 struct ether_header
*eh
;
1586 src_if
= m
->m_pkthdr
.rcvif
;
1589 ASSERT_SERIALIZED(ifp
->if_serializer
);
1592 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1595 * Look up the bridge_iflist.
1597 bif
= bridge_lookup_member_if(sc
, src_if
);
1599 /* Interface is not a bridge member (anymore?) */
1604 if (bif
->bif_flags
& IFBIF_STP
) {
1605 switch (bif
->bif_state
) {
1606 case BSTP_IFSTATE_BLOCKING
:
1607 case BSTP_IFSTATE_LISTENING
:
1608 case BSTP_IFSTATE_DISABLED
:
1614 eh
= mtod(m
, struct ether_header
*);
1617 * If the interface is learning, and the source
1618 * address is valid and not multicast, record
1621 if ((bif
->bif_flags
& IFBIF_LEARNING
) != 0 &&
1622 ETHER_IS_MULTICAST(eh
->ether_shost
) == 0 &&
1623 (eh
->ether_shost
[0] == 0 &&
1624 eh
->ether_shost
[1] == 0 &&
1625 eh
->ether_shost
[2] == 0 &&
1626 eh
->ether_shost
[3] == 0 &&
1627 eh
->ether_shost
[4] == 0 &&
1628 eh
->ether_shost
[5] == 0) == 0) {
1629 bridge_rtupdate(sc
, eh
->ether_shost
, src_if
, 0, IFBAF_DYNAMIC
);
1632 if ((bif
->bif_flags
& IFBIF_STP
) != 0 &&
1633 bif
->bif_state
== BSTP_IFSTATE_LEARNING
) {
1639 * At this point, the port either doesn't participate
1640 * in spanning tree or it is in the forwarding state.
1644 * If the packet is unicast, destined for someone on
1645 * "this" side of the bridge, drop it.
1647 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) {
1648 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1649 if (src_if
== dst_if
) {
1654 /* ...forward it to all interfaces. */
1655 sc
->sc_ifp
->if_imcasts
++;
1659 if (dst_if
== NULL
) {
1660 bridge_broadcast(sc
, src_if
, m
, 1);
1665 * At this point, we're dealing with a unicast frame
1666 * going to a different interface.
1668 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
1672 bif
= bridge_lookup_member_if(sc
, dst_if
);
1674 /* Not a member of the bridge (anymore?) */
1679 if (bif
->bif_flags
& IFBIF_STP
) {
1680 switch (bif
->bif_state
) {
1681 case BSTP_IFSTATE_DISABLED
:
1682 case BSTP_IFSTATE_BLOCKING
:
1688 lwkt_serialize_exit(ifp
->if_serializer
);
1690 /* run the packet filter */
1691 if (inet_pfil_hook
.ph_hashooks
> 0
1693 || inet6_pfil_hook
.ph_hashooks
> 0
1696 if (bridge_pfil(&m
, ifp
, src_if
, PFIL_IN
) != 0)
1701 if (bridge_pfil(&m
, ifp
, dst_if
, PFIL_OUT
) != 0)
1706 bridge_handoff(dst_if
, m
);
1709 * ifp's serializer was held on entry and is expected to be held
1713 lwkt_serialize_enter(ifp
->if_serializer
);
1719 * Receive input from a member interface. Queue the packet for
1720 * bridging if it is not for us.
1722 static struct mbuf
*
1723 bridge_input(struct ifnet
*ifp
, struct mbuf
*m
)
1725 struct bridge_softc
*sc
= ifp
->if_bridge
;
1726 struct bridge_iflist
*bif
;
1727 struct ifnet
*bifp
, *new_ifp
;
1728 struct ether_header
*eh
;
1729 struct mbuf
*mc
, *mc2
;
1734 lwkt_serialize_enter(bifp
->if_serializer
);
1736 if ((bifp
->if_flags
& IFF_RUNNING
) == 0)
1740 * Implement support for bridge monitoring. If this flag has been
1741 * set on this interface, discard the packet once we push it through
1742 * the bpf(4) machinery, but before we do, increment the byte and
1743 * packet counters associated with this interface.
1745 if ((bifp
->if_flags
& IFF_MONITOR
) != 0) {
1746 m
->m_pkthdr
.rcvif
= bifp
;
1748 bifp
->if_ipackets
++;
1749 bifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1755 eh
= mtod(m
, struct ether_header
*);
1757 m
->m_flags
&= ~M_PROTO1
; /* XXX Hack - loop prevention */
1759 if (memcmp(eh
->ether_dhost
, IF_LLADDR(bifp
), ETHER_ADDR_LEN
) == 0) {
1761 * If the packet is for us, set the packets source as the
1762 * bridge, and return the packet back to ifnet.if_input for
1765 KASSERT(bifp
->if_bridge
== NULL
,
1766 ("loop created in bridge_input"));
1772 * Tap all packets arriving on the bridge, no matter if
1773 * they are local destinations or not. In is in.
1777 bif
= bridge_lookup_member_if(sc
, ifp
);
1783 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
1784 /* Tap off 802.1D packets; they do not get forwarded. */
1785 if (memcmp(eh
->ether_dhost
, bstp_etheraddr
,
1786 ETHER_ADDR_LEN
) == 0) {
1787 m
= bstp_input(ifp
, m
);
1789 ("attempt to deliver 802.1D packet\n"));
1793 if (bif
->bif_flags
& IFBIF_STP
) {
1794 switch (bif
->bif_state
) {
1795 case BSTP_IFSTATE_BLOCKING
:
1796 case BSTP_IFSTATE_LISTENING
:
1797 case BSTP_IFSTATE_DISABLED
:
1802 if (bcmp(etherbroadcastaddr
, eh
->ether_dhost
,
1803 sizeof(etherbroadcastaddr
)) == 0)
1804 m
->m_flags
|= M_BCAST
;
1806 m
->m_flags
|= M_MCAST
;
1809 * Make a deep copy of the packet and enqueue the copy
1810 * for bridge processing; return the original packet for
1813 mc
= m_dup(m
, MB_DONTWAIT
);
1817 bridge_forward(sc
, mc
);
1820 * Reinject the mbuf as arriving on the bridge so we have a
1821 * chance at claiming multicast packets. We can not loop back
1822 * here from ether_input as a bridge is never a member of a
1825 KASSERT(bifp
->if_bridge
== NULL
,
1826 ("loop created in bridge_input"));
1827 mc2
= m_dup(m
, MB_DONTWAIT
);
1830 /* Keep the layer3 header aligned */
1831 int i
= min(mc2
->m_pkthdr
.len
, max_protohdr
);
1832 mc2
= m_copyup(mc2
, i
, ETHER_ALIGN
);
1836 mc2
->m_pkthdr
.rcvif
= bifp
;
1837 bifp
->if_ipackets
++;
1838 bifp
->if_input(bifp
, mc2
);
1841 /* Return the original packet for local processing. */
1845 if (bif
->bif_flags
& IFBIF_STP
) {
1846 switch (bif
->bif_state
) {
1847 case BSTP_IFSTATE_BLOCKING
:
1848 case BSTP_IFSTATE_LISTENING
:
1849 case BSTP_IFSTATE_DISABLED
:
1855 * Unicast. Make sure it's not for us.
1857 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1858 if (bif
->bif_ifp
->if_type
!= IFT_ETHER
)
1861 /* It is destined for us. */
1862 if (memcmp(IF_LLADDR(bif
->bif_ifp
), eh
->ether_dhost
,
1863 ETHER_ADDR_LEN
) == 0) {
1864 if (bif
->bif_flags
& IFBIF_LEARNING
) {
1866 eh
->ether_shost
, ifp
, 0, IFBAF_DYNAMIC
);
1869 if (bif
->bif_ifp
!= ifp
) {
1870 /* XXX loop prevention */
1871 m
->m_flags
|= M_PROTO1
;
1872 new_ifp
= bif
->bif_ifp
;
1877 /* We just received a packet that we sent out. */
1878 if (memcmp(IF_LLADDR(bif
->bif_ifp
), eh
->ether_shost
,
1879 ETHER_ADDR_LEN
) == 0) {
1886 /* Perform the bridge forwarding function. */
1887 bridge_forward(sc
, m
);
1890 lwkt_serialize_exit(bifp
->if_serializer
);
1892 if (new_ifp
!= NULL
) {
1893 lwkt_serialize_enter(new_ifp
->if_serializer
);
1895 m
->m_pkthdr
.rcvif
= new_ifp
;
1896 new_ifp
->if_ipackets
++;
1897 new_ifp
->if_input(new_ifp
, m
);
1900 lwkt_serialize_exit(new_ifp
->if_serializer
);
1908 * Send a frame to all interfaces that are members of
1909 * the bridge, except for the one on which the packet
1913 bridge_broadcast(struct bridge_softc
*sc
, struct ifnet
*src_if
,
1914 struct mbuf
*m
, int runfilt
)
1916 struct bridge_iflist
*bif
;
1918 struct ifnet
*dst_if
, *bifp
;
1923 ASSERT_SERIALIZED(bifp
->if_serializer
);
1925 /* run the packet filter */
1926 if (runfilt
&& (inet_pfil_hook
.ph_hashooks
> 0
1928 || inet6_pfil_hook
.ph_hashooks
> 0
1931 lwkt_serialize_exit(bifp
->if_serializer
);
1933 /* Filter on the bridge interface before broadcasting */
1935 if (bridge_pfil(&m
, bifp
, src_if
, PFIL_IN
) != 0)
1940 if (bridge_pfil(&m
, bifp
, NULL
, PFIL_OUT
) != 0)
1943 lwkt_serialize_enter(bifp
->if_serializer
);
1948 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1949 dst_if
= bif
->bif_ifp
;
1950 if (dst_if
== src_if
)
1953 if (bif
->bif_flags
& IFBIF_STP
) {
1954 switch (bif
->bif_state
) {
1955 case BSTP_IFSTATE_BLOCKING
:
1956 case BSTP_IFSTATE_DISABLED
:
1961 if ((bif
->bif_flags
& IFBIF_DISCOVER
) == 0 &&
1962 (m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
1965 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1968 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
1972 mc
= m_copypacket(m
, MB_DONTWAIT
);
1974 sc
->sc_ifp
->if_oerrors
++;
1978 bridge_pfil_enqueue(dst_if
, mc
, runfilt
);
1987 * Duplicate a packet out one or more interfaces that are in span mode,
1988 * the original mbuf is unmodified.
1991 bridge_span(struct bridge_softc
*sc
, struct mbuf
*m
)
1993 struct bridge_iflist
*bif
;
1994 struct ifnet
*dst_if
;
1997 if (LIST_EMPTY(&sc
->sc_spanlist
))
2000 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
2001 dst_if
= bif
->bif_ifp
;
2003 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
2006 mc
= m_copypacket(m
, MB_DONTWAIT
);
2008 sc
->sc_ifp
->if_oerrors
++;
2012 bridge_enqueue(dst_if
, mc
);
2019 * Add a bridge routing entry.
2020 * Can be called from interrupt context.
2023 bridge_rtupdate(struct bridge_softc
*sc
, const uint8_t *dst
,
2024 struct ifnet
*dst_if
, int setflags
, uint8_t flags
)
2026 struct bridge_rtnode
*brt
;
2030 * A route for this destination might already exist. If so,
2031 * update it, otherwise create a new one.
2033 if ((brt
= bridge_rtnode_lookup(sc
, dst
)) == NULL
) {
2034 if (sc
->sc_brtcnt
>= sc
->sc_brtmax
)
2038 * Allocate a new bridge forwarding node, and
2039 * initialize the expiration time and Ethernet
2042 brt
= kmalloc(sizeof(struct bridge_rtnode
), M_DEVBUF
,
2043 M_INTNOWAIT
|M_ZERO
);
2047 brt
->brt_flags
= IFBAF_DYNAMIC
;
2048 memcpy(brt
->brt_addr
, dst
, ETHER_ADDR_LEN
);
2050 if ((error
= bridge_rtnode_insert(sc
, brt
)) != 0) {
2051 kfree(brt
, M_DEVBUF
);
2056 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2057 brt
->brt_ifp
= dst_if
;
2058 if ((flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2059 brt
->brt_expire
= time_second
+ sc
->sc_brttimeout
;
2061 brt
->brt_flags
= flags
;
2069 * Lookup the destination interface for an address.
2071 static struct ifnet
*
2072 bridge_rtlookup(struct bridge_softc
*sc
, const uint8_t *addr
)
2074 struct bridge_rtnode
*brt
;
2076 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
2079 return (brt
->brt_ifp
);
2085 * Trim the routine table so that we have a number
2086 * of routing entries less than or equal to the
2090 bridge_rttrim(struct bridge_softc
*sc
)
2092 struct bridge_rtnode
*brt
, *nbrt
;
2094 /* Make sure we actually need to do this. */
2095 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2098 /* Force an aging cycle; this might trim enough addresses. */
2100 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2103 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2104 nbrt
= LIST_NEXT(brt
, brt_list
);
2105 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2106 bridge_rtnode_destroy(sc
, brt
);
2107 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2116 * Aging timer for the bridge.
2119 bridge_timer(void *arg
)
2121 struct bridge_softc
*sc
= arg
;
2123 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
2127 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
2128 callout_reset(&sc
->sc_brcallout
,
2129 bridge_rtable_prune_period
* hz
, bridge_timer
, sc
);
2131 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
2137 * Perform an aging cycle.
2140 bridge_rtage(struct bridge_softc
*sc
)
2142 struct bridge_rtnode
*brt
, *nbrt
;
2144 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2145 nbrt
= LIST_NEXT(brt
, brt_list
);
2146 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2147 if (time_second
>= brt
->brt_expire
)
2148 bridge_rtnode_destroy(sc
, brt
);
2156 * Remove all dynamic addresses from the bridge.
2159 bridge_rtflush(struct bridge_softc
*sc
, int full
)
2161 struct bridge_rtnode
*brt
, *nbrt
;
2163 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2164 nbrt
= LIST_NEXT(brt
, brt_list
);
2165 if (full
|| (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2166 bridge_rtnode_destroy(sc
, brt
);
2173 * Remove an address from the table.
2176 bridge_rtdaddr(struct bridge_softc
*sc
, const uint8_t *addr
)
2178 struct bridge_rtnode
*brt
;
2180 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
2183 bridge_rtnode_destroy(sc
, brt
);
2190 * Delete routes to a speicifc member interface.
2193 bridge_rtdelete(struct bridge_softc
*sc
, struct ifnet
*ifp
, int full
)
2195 struct bridge_rtnode
*brt
, *nbrt
;
2197 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2198 nbrt
= LIST_NEXT(brt
, brt_list
);
2199 if (brt
->brt_ifp
== ifp
&& (full
||
2200 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
))
2201 bridge_rtnode_destroy(sc
, brt
);
2206 * bridge_rtable_init:
2208 * Initialize the route table for this bridge.
2211 bridge_rtable_init(struct bridge_softc
*sc
)
2215 sc
->sc_rthash
= kmalloc(sizeof(*sc
->sc_rthash
) * BRIDGE_RTHASH_SIZE
,
2216 M_DEVBUF
, M_WAITOK
);
2218 for (i
= 0; i
< BRIDGE_RTHASH_SIZE
; i
++)
2219 LIST_INIT(&sc
->sc_rthash
[i
]);
2221 sc
->sc_rthash_key
= karc4random();
2223 LIST_INIT(&sc
->sc_rtlist
);
2229 * bridge_rtable_fini:
2231 * Deconstruct the route table for this bridge.
2234 bridge_rtable_fini(struct bridge_softc
*sc
)
2237 kfree(sc
->sc_rthash
, M_DEVBUF
);
2241 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2242 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2244 #define mix(a, b, c) \
2246 a -= b; a -= c; a ^= (c >> 13); \
2247 b -= c; b -= a; b ^= (a << 8); \
2248 c -= a; c -= b; c ^= (b >> 13); \
2249 a -= b; a -= c; a ^= (c >> 12); \
2250 b -= c; b -= a; b ^= (a << 16); \
2251 c -= a; c -= b; c ^= (b >> 5); \
2252 a -= b; a -= c; a ^= (c >> 3); \
2253 b -= c; b -= a; b ^= (a << 10); \
2254 c -= a; c -= b; c ^= (b >> 15); \
2255 } while (/*CONSTCOND*/0)
2257 static __inline
uint32_t
2258 bridge_rthash(struct bridge_softc
*sc
, const uint8_t *addr
)
2260 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= sc
->sc_rthash_key
;
2271 return (c
& BRIDGE_RTHASH_MASK
);
2277 bridge_rtnode_addr_cmp(const uint8_t *a
, const uint8_t *b
)
2281 for (i
= 0, d
= 0; i
< ETHER_ADDR_LEN
&& d
== 0; i
++) {
2282 d
= ((int)a
[i
]) - ((int)b
[i
]);
2289 * bridge_rtnode_lookup:
2291 * Look up a bridge route node for the specified destination.
2293 static struct bridge_rtnode
*
2294 bridge_rtnode_lookup(struct bridge_softc
*sc
, const uint8_t *addr
)
2296 struct bridge_rtnode
*brt
;
2300 hash
= bridge_rthash(sc
, addr
);
2301 LIST_FOREACH(brt
, &sc
->sc_rthash
[hash
], brt_hash
) {
2302 dir
= bridge_rtnode_addr_cmp(addr
, brt
->brt_addr
);
2313 * bridge_rtnode_insert:
2315 * Insert the specified bridge node into the route table. We
2316 * assume the entry is not already in the table.
2319 bridge_rtnode_insert(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2321 struct bridge_rtnode
*lbrt
;
2325 hash
= bridge_rthash(sc
, brt
->brt_addr
);
2327 lbrt
= LIST_FIRST(&sc
->sc_rthash
[hash
]);
2329 LIST_INSERT_HEAD(&sc
->sc_rthash
[hash
], brt
, brt_hash
);
2334 dir
= bridge_rtnode_addr_cmp(brt
->brt_addr
, lbrt
->brt_addr
);
2338 LIST_INSERT_BEFORE(lbrt
, brt
, brt_hash
);
2341 if (LIST_NEXT(lbrt
, brt_hash
) == NULL
) {
2342 LIST_INSERT_AFTER(lbrt
, brt
, brt_hash
);
2345 lbrt
= LIST_NEXT(lbrt
, brt_hash
);
2346 } while (lbrt
!= NULL
);
2349 panic("bridge_rtnode_insert: impossible");
2353 LIST_INSERT_HEAD(&sc
->sc_rtlist
, brt
, brt_list
);
2360 * bridge_rtnode_destroy:
2362 * Destroy a bridge rtnode.
2365 bridge_rtnode_destroy(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2368 LIST_REMOVE(brt
, brt_hash
);
2370 LIST_REMOVE(brt
, brt_list
);
2372 kfree(brt
, M_DEVBUF
);
2376 * Send bridge packets through pfil if they are one of the types pfil can deal
2377 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2378 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2382 bridge_pfil(struct mbuf
**mp
, struct ifnet
*bifp
, struct ifnet
*ifp
, int dir
)
2384 int snap
, error
, i
, hlen
;
2385 struct ether_header
*eh1
, eh2
;
2388 u_int16_t ether_type
;
2391 error
= -1; /* Default error if not error == 0 */
2393 if (pfil_bridge
== 0 && pfil_member
== 0)
2394 return (0); /* filtering is disabled */
2396 i
= min((*mp
)->m_pkthdr
.len
, max_protohdr
);
2397 if ((*mp
)->m_len
< i
) {
2398 *mp
= m_pullup(*mp
, i
);
2400 kprintf("%s: m_pullup failed\n", __func__
);
2405 eh1
= mtod(*mp
, struct ether_header
*);
2406 ether_type
= ntohs(eh1
->ether_type
);
2409 * Check for SNAP/LLC.
2411 if (ether_type
< ETHERMTU
) {
2412 struct llc
*llc2
= (struct llc
*)(eh1
+ 1);
2414 if ((*mp
)->m_len
>= ETHER_HDR_LEN
+ 8 &&
2415 llc2
->llc_dsap
== LLC_SNAP_LSAP
&&
2416 llc2
->llc_ssap
== LLC_SNAP_LSAP
&&
2417 llc2
->llc_control
== LLC_UI
) {
2418 ether_type
= htons(llc2
->llc_un
.type_snap
.ether_type
);
2424 * If we're trying to filter bridge traffic, don't look at anything
2425 * other than IP and ARP traffic. If the filter doesn't understand
2426 * IPv6, don't allow IPv6 through the bridge either. This is lame
2427 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2428 * but of course we don't have an AppleTalk filter to begin with.
2429 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2432 switch (ether_type
) {
2434 case ETHERTYPE_REVARP
:
2435 return (0); /* Automatically pass */
2438 case ETHERTYPE_IPV6
:
2443 * Check to see if the user wants to pass non-ip
2444 * packets, these will not be checked by pfil(9) and
2445 * passed unconditionally so the default is to drop.
2451 /* Strip off the Ethernet header and keep a copy. */
2452 m_copydata(*mp
, 0, ETHER_HDR_LEN
, (caddr_t
) &eh2
);
2453 m_adj(*mp
, ETHER_HDR_LEN
);
2455 /* Strip off snap header, if present */
2457 m_copydata(*mp
, 0, sizeof(struct llc
), (caddr_t
) &llc1
);
2458 m_adj(*mp
, sizeof(struct llc
));
2462 * Check the IP header for alignment and errors
2464 if (dir
== PFIL_IN
) {
2465 switch (ether_type
) {
2467 error
= bridge_ip_checkbasic(mp
);
2470 case ETHERTYPE_IPV6
:
2471 error
= bridge_ip6_checkbasic(mp
);
2484 * Run the packet through pfil
2490 * before calling the firewall, swap fields the same as
2491 * IP does. here we assume the header is contiguous
2493 ip
= mtod(*mp
, struct ip
*);
2495 ip
->ip_len
= ntohs(ip
->ip_len
);
2496 ip
->ip_off
= ntohs(ip
->ip_off
);
2499 * Run pfil on the member interface and the bridge, both can
2500 * be skipped by clearing pfil_member or pfil_bridge.
2503 * in_if -> bridge_if -> out_if
2505 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
2506 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
2509 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2512 if (pfil_member
&& ifp
!= NULL
)
2513 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, ifp
,
2516 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2519 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
2520 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
2523 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2526 /* check if we need to fragment the packet */
2527 if (pfil_member
&& ifp
!= NULL
&& dir
== PFIL_OUT
) {
2528 i
= (*mp
)->m_pkthdr
.len
;
2529 if (i
> ifp
->if_mtu
) {
2530 error
= bridge_fragment(ifp
, *mp
, &eh2
, snap
,
2536 /* Recalculate the ip checksum and restore byte ordering */
2537 ip
= mtod(*mp
, struct ip
*);
2538 hlen
= ip
->ip_hl
<< 2;
2539 if (hlen
< sizeof(struct ip
))
2541 if (hlen
> (*mp
)->m_len
) {
2542 if ((*mp
= m_pullup(*mp
, hlen
)) == 0)
2544 ip
= mtod(*mp
, struct ip
*);
2548 ip
->ip_len
= htons(ip
->ip_len
);
2549 ip
->ip_off
= htons(ip
->ip_off
);
2551 if (hlen
== sizeof(struct ip
))
2552 ip
->ip_sum
= in_cksum_hdr(ip
);
2554 ip
->ip_sum
= in_cksum(*mp
, hlen
);
2558 case ETHERTYPE_IPV6
:
2559 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
2560 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
2563 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2566 if (pfil_member
&& ifp
!= NULL
)
2567 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, ifp
,
2570 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2573 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
2574 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
2591 * Finally, put everything back the way it was and return
2594 M_PREPEND(*mp
, sizeof(struct llc
), MB_DONTWAIT
);
2597 bcopy(&llc1
, mtod(*mp
, caddr_t
), sizeof(struct llc
));
2600 M_PREPEND(*mp
, ETHER_HDR_LEN
, MB_DONTWAIT
);
2603 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
2614 * Perform basic checks on header size since
2615 * pfil assumes ip_input has already processed
2616 * it for it. Cut-and-pasted from ip_input.c.
2617 * Given how simple the IPv6 version is,
2618 * does the IPv4 version really need to be
2621 * XXX Should we update ipstat here, or not?
2622 * XXX Right now we update ipstat but not
2626 bridge_ip_checkbasic(struct mbuf
**mp
)
2628 struct mbuf
*m
= *mp
;
2636 if (IP_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
2637 if ((m
= m_copyup(m
, sizeof(struct ip
),
2638 (max_linkhdr
+ 3) & ~3)) == NULL
) {
2639 /* XXXJRT new stat, please */
2640 ipstat
.ips_toosmall
++;
2645 #ifndef __predict_false
2646 #define __predict_false(x) x
2648 if (__predict_false(m
->m_len
< sizeof (struct ip
))) {
2649 if ((m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
2650 ipstat
.ips_toosmall
++;
2654 ip
= mtod(m
, struct ip
*);
2655 if (ip
== NULL
) goto bad
;
2657 if (ip
->ip_v
!= IPVERSION
) {
2658 ipstat
.ips_badvers
++;
2661 hlen
= ip
->ip_hl
<< 2;
2662 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
2663 ipstat
.ips_badhlen
++;
2666 if (hlen
> m
->m_len
) {
2667 if ((m
= m_pullup(m
, hlen
)) == 0) {
2668 ipstat
.ips_badhlen
++;
2671 ip
= mtod(m
, struct ip
*);
2672 if (ip
== NULL
) goto bad
;
2675 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
2676 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
2678 if (hlen
== sizeof(struct ip
)) {
2679 sum
= in_cksum_hdr(ip
);
2681 sum
= in_cksum(m
, hlen
);
2685 ipstat
.ips_badsum
++;
2689 /* Retrieve the packet length. */
2690 len
= ntohs(ip
->ip_len
);
2693 * Check for additional length bogosity
2696 ipstat
.ips_badlen
++;
2701 * Check that the amount of data in the buffers
2702 * is as at least much as the IP header would have us expect.
2703 * Drop packet if shorter than we expect.
2705 if (m
->m_pkthdr
.len
< len
) {
2706 ipstat
.ips_tooshort
++;
2710 /* Checks out, proceed */
2721 * Same as above, but for IPv6.
2722 * Cut-and-pasted from ip6_input.c.
2723 * XXX Should we update ip6stat, or not?
2726 bridge_ip6_checkbasic(struct mbuf
**mp
)
2728 struct mbuf
*m
= *mp
;
2729 struct ip6_hdr
*ip6
;
2732 * If the IPv6 header is not aligned, slurp it up into a new
2733 * mbuf with space for link headers, in the event we forward
2734 * it. Otherwise, if it is aligned, make sure the entire base
2735 * IPv6 header is in the first mbuf of the chain.
2738 if (IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
2739 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
2740 if ((m
= m_copyup(m
, sizeof(struct ip6_hdr
),
2741 (max_linkhdr
+ 3) & ~3)) == NULL
) {
2742 /* XXXJRT new stat, please */
2743 ip6stat
.ip6s_toosmall
++;
2744 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
2749 if (__predict_false(m
->m_len
< sizeof(struct ip6_hdr
))) {
2750 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
2751 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == NULL
) {
2752 ip6stat
.ip6s_toosmall
++;
2753 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
2758 ip6
= mtod(m
, struct ip6_hdr
*);
2760 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
2761 ip6stat
.ip6s_badvers
++;
2762 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
2766 /* Checks out, proceed */
2779 * Return a fragmented mbuf chain.
2782 bridge_fragment(struct ifnet
*ifp
, struct mbuf
*m
, struct ether_header
*eh
,
2783 int snap
, struct llc
*llc
)
2789 if (m
->m_len
< sizeof(struct ip
) &&
2790 (m
= m_pullup(m
, sizeof(struct ip
))) == NULL
)
2792 ip
= mtod(m
, struct ip
*);
2794 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
,
2799 /* walk the chain and re-add the Ethernet header */
2800 for (m0
= m
; m0
; m0
= m0
->m_nextpkt
) {
2803 M_PREPEND(m0
, sizeof(struct llc
), MB_DONTWAIT
);
2808 bcopy(llc
, mtod(m0
, caddr_t
),
2809 sizeof(struct llc
));
2811 M_PREPEND(m0
, ETHER_HDR_LEN
, MB_DONTWAIT
);
2816 bcopy(eh
, mtod(m0
, caddr_t
), ETHER_HDR_LEN
);
2822 ipstat
.ips_fragmented
++;
2833 bridge_enqueue_handler(struct netmsg
*nmsg
)
2835 struct netmsg_packet
*nmp
;
2836 struct ifnet
*dst_ifp
;
2839 nmp
= (struct netmsg_packet
*)nmsg
;
2841 dst_ifp
= nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
;
2843 bridge_handoff_notags(dst_ifp
, m
);
2847 bridge_pfil_enqueue_handler(struct netmsg
*nmsg
)
2849 struct netmsg_packet
*nmp
;
2850 struct ifnet
*dst_ifp
;
2853 nmp
= (struct netmsg_packet
*)nmsg
;
2855 dst_ifp
= nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
;
2858 * Filter on the output interface. Pass a NULL bridge interface
2859 * pointer so we do not redundantly filter on the bridge for
2860 * each interface we broadcast on.
2862 if (inet_pfil_hook
.ph_hashooks
> 0
2864 || inet6_pfil_hook
.ph_hashooks
> 0
2867 if (bridge_pfil(&m
, NULL
, dst_ifp
, PFIL_OUT
) != 0)
2872 bridge_handoff_notags(dst_ifp
, m
);
2876 bridge_handoff(struct ifnet
*dst_ifp
, struct mbuf
*m
)
2878 while (m
->m_type
== MT_TAG
) {
2879 /* XXX see ether_output_frame for full rules check */
2882 bridge_handoff_notags(dst_ifp
, m
);
2886 bridge_handoff_notags(struct ifnet
*dst_ifp
, struct mbuf
*m
)
2890 KKASSERT(m
->m_type
!= MT_TAG
);
2892 lwkt_serialize_enter(dst_ifp
->if_serializer
);
2894 /* We may be sending a fragment so traverse the mbuf */
2896 struct altq_pktattr pktattr
;
2899 m
->m_nextpkt
= NULL
;
2901 if (ifq_is_enabled(&dst_ifp
->if_snd
))
2902 altq_etherclassify(&dst_ifp
->if_snd
, m
, &pktattr
);
2904 ifq_handoff(dst_ifp
, m
, &pktattr
);
2907 lwkt_serialize_exit(dst_ifp
->if_serializer
);