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.28 2008/01/11 11:59:40 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 struct sockaddr
*, struct rtentry
*);
184 extern void (*bridge_dn_p
)(struct mbuf
*, struct ifnet
*);
186 static int bridge_rtable_prune_period
= BRIDGE_RTABLE_PRUNE_PERIOD
;
188 static int bridge_clone_create(struct if_clone
*, int);
189 static void bridge_clone_destroy(struct ifnet
*);
191 static int bridge_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
192 static void bridge_mutecaps(struct bridge_iflist
*, int);
193 static void bridge_ifdetach(void *arg __unused
, struct ifnet
*);
194 static void bridge_init(void *);
195 static void bridge_stop(struct ifnet
*);
196 static void bridge_start(struct ifnet
*);
197 static struct mbuf
*bridge_input(struct ifnet
*, struct mbuf
*);
198 static int bridge_output_serialized(struct ifnet
*, struct mbuf
*,
199 struct sockaddr
*, struct rtentry
*);
201 static void bridge_forward(struct bridge_softc
*, struct mbuf
*m
);
203 static void bridge_timer(void *);
205 static void bridge_broadcast(struct bridge_softc
*, struct ifnet
*,
207 static void bridge_span(struct bridge_softc
*, struct mbuf
*);
209 static int bridge_rtupdate(struct bridge_softc
*, const uint8_t *,
210 struct ifnet
*, int, uint8_t);
211 static struct ifnet
*bridge_rtlookup(struct bridge_softc
*, const uint8_t *);
212 static void bridge_rttrim(struct bridge_softc
*);
213 static void bridge_rtage(struct bridge_softc
*);
214 static void bridge_rtflush(struct bridge_softc
*, int);
215 static int bridge_rtdaddr(struct bridge_softc
*, const uint8_t *);
217 static int bridge_rtable_init(struct bridge_softc
*);
218 static void bridge_rtable_fini(struct bridge_softc
*);
220 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
221 static struct bridge_rtnode
*bridge_rtnode_lookup(struct bridge_softc
*,
223 static int bridge_rtnode_insert(struct bridge_softc
*,
224 struct bridge_rtnode
*);
225 static void bridge_rtnode_destroy(struct bridge_softc
*,
226 struct bridge_rtnode
*);
228 static struct bridge_iflist
*bridge_lookup_member(struct bridge_softc
*,
230 static struct bridge_iflist
*bridge_lookup_member_if(struct bridge_softc
*,
232 static void bridge_delete_member(struct bridge_softc
*,
233 struct bridge_iflist
*, int);
234 static void bridge_delete_span(struct bridge_softc
*,
235 struct bridge_iflist
*);
237 static int bridge_ioctl_add(struct bridge_softc
*, void *);
238 static int bridge_ioctl_del(struct bridge_softc
*, void *);
239 static int bridge_ioctl_gifflags(struct bridge_softc
*, void *);
240 static int bridge_ioctl_sifflags(struct bridge_softc
*, void *);
241 static int bridge_ioctl_scache(struct bridge_softc
*, void *);
242 static int bridge_ioctl_gcache(struct bridge_softc
*, void *);
243 static int bridge_ioctl_gifs(struct bridge_softc
*, void *);
244 static int bridge_ioctl_rts(struct bridge_softc
*, void *);
245 static int bridge_ioctl_saddr(struct bridge_softc
*, void *);
246 static int bridge_ioctl_sto(struct bridge_softc
*, void *);
247 static int bridge_ioctl_gto(struct bridge_softc
*, void *);
248 static int bridge_ioctl_daddr(struct bridge_softc
*, void *);
249 static int bridge_ioctl_flush(struct bridge_softc
*, void *);
250 static int bridge_ioctl_gpri(struct bridge_softc
*, void *);
251 static int bridge_ioctl_spri(struct bridge_softc
*, void *);
252 static int bridge_ioctl_ght(struct bridge_softc
*, void *);
253 static int bridge_ioctl_sht(struct bridge_softc
*, void *);
254 static int bridge_ioctl_gfd(struct bridge_softc
*, void *);
255 static int bridge_ioctl_sfd(struct bridge_softc
*, void *);
256 static int bridge_ioctl_gma(struct bridge_softc
*, void *);
257 static int bridge_ioctl_sma(struct bridge_softc
*, void *);
258 static int bridge_ioctl_sifprio(struct bridge_softc
*, void *);
259 static int bridge_ioctl_sifcost(struct bridge_softc
*, void *);
260 static int bridge_ioctl_addspan(struct bridge_softc
*, void *);
261 static int bridge_ioctl_delspan(struct bridge_softc
*, void *);
262 static int bridge_pfil(struct mbuf
**, struct ifnet
*, struct ifnet
*,
264 static int bridge_ip_checkbasic(struct mbuf
**mp
);
266 static int bridge_ip6_checkbasic(struct mbuf
**mp
);
268 static int bridge_fragment(struct ifnet
*, struct mbuf
*,
269 struct ether_header
*, int, struct llc
*);
270 static void bridge_enqueue_internal(struct ifnet
*, struct mbuf
*m
,
272 static void bridge_enqueue_handler(struct netmsg
*);
273 static void bridge_pfil_enqueue_handler(struct netmsg
*);
274 static void bridge_pfil_enqueue(struct ifnet
*, struct mbuf
*, int);
275 static void bridge_handoff_notags(struct ifnet
*, struct mbuf
*);
276 static void bridge_handoff(struct ifnet
*, struct mbuf
*);
278 SYSCTL_DECL(_net_link
);
279 SYSCTL_NODE(_net_link
, IFT_BRIDGE
, bridge
, CTLFLAG_RW
, 0, "Bridge");
281 static int pfil_onlyip
= 1; /* only pass IP[46] packets when pfil is enabled */
282 static int pfil_bridge
= 1; /* run pfil hooks on the bridge interface */
283 static int pfil_member
= 1; /* run pfil hooks on the member interface */
284 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_onlyip
, CTLFLAG_RW
,
285 &pfil_onlyip
, 0, "Only pass IP packets when pfil is enabled");
286 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_bridge
, CTLFLAG_RW
,
287 &pfil_bridge
, 0, "Packet filter on the bridge interface");
288 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_member
, CTLFLAG_RW
,
289 &pfil_member
, 0, "Packet filter on the member interface");
291 struct bridge_control
{
292 int (*bc_func
)(struct bridge_softc
*, void *);
297 #define BC_F_COPYIN 0x01 /* copy arguments in */
298 #define BC_F_COPYOUT 0x02 /* copy arguments out */
299 #define BC_F_SUSER 0x04 /* do super-user check */
301 const struct bridge_control bridge_control_table
[] = {
302 { bridge_ioctl_add
, sizeof(struct ifbreq
),
303 BC_F_COPYIN
|BC_F_SUSER
},
304 { bridge_ioctl_del
, sizeof(struct ifbreq
),
305 BC_F_COPYIN
|BC_F_SUSER
},
307 { bridge_ioctl_gifflags
, sizeof(struct ifbreq
),
308 BC_F_COPYIN
|BC_F_COPYOUT
},
309 { bridge_ioctl_sifflags
, sizeof(struct ifbreq
),
310 BC_F_COPYIN
|BC_F_SUSER
},
312 { bridge_ioctl_scache
, sizeof(struct ifbrparam
),
313 BC_F_COPYIN
|BC_F_SUSER
},
314 { bridge_ioctl_gcache
, sizeof(struct ifbrparam
),
317 { bridge_ioctl_gifs
, sizeof(struct ifbifconf
),
318 BC_F_COPYIN
|BC_F_COPYOUT
},
319 { bridge_ioctl_rts
, sizeof(struct ifbaconf
),
320 BC_F_COPYIN
|BC_F_COPYOUT
},
322 { bridge_ioctl_saddr
, sizeof(struct ifbareq
),
323 BC_F_COPYIN
|BC_F_SUSER
},
325 { bridge_ioctl_sto
, sizeof(struct ifbrparam
),
326 BC_F_COPYIN
|BC_F_SUSER
},
327 { bridge_ioctl_gto
, sizeof(struct ifbrparam
),
330 { bridge_ioctl_daddr
, sizeof(struct ifbareq
),
331 BC_F_COPYIN
|BC_F_SUSER
},
333 { bridge_ioctl_flush
, sizeof(struct ifbreq
),
334 BC_F_COPYIN
|BC_F_SUSER
},
336 { bridge_ioctl_gpri
, sizeof(struct ifbrparam
),
338 { bridge_ioctl_spri
, sizeof(struct ifbrparam
),
339 BC_F_COPYIN
|BC_F_SUSER
},
341 { bridge_ioctl_ght
, sizeof(struct ifbrparam
),
343 { bridge_ioctl_sht
, sizeof(struct ifbrparam
),
344 BC_F_COPYIN
|BC_F_SUSER
},
346 { bridge_ioctl_gfd
, sizeof(struct ifbrparam
),
348 { bridge_ioctl_sfd
, sizeof(struct ifbrparam
),
349 BC_F_COPYIN
|BC_F_SUSER
},
351 { bridge_ioctl_gma
, sizeof(struct ifbrparam
),
353 { bridge_ioctl_sma
, sizeof(struct ifbrparam
),
354 BC_F_COPYIN
|BC_F_SUSER
},
356 { bridge_ioctl_sifprio
, sizeof(struct ifbreq
),
357 BC_F_COPYIN
|BC_F_SUSER
},
359 { bridge_ioctl_sifcost
, sizeof(struct ifbreq
),
360 BC_F_COPYIN
|BC_F_SUSER
},
362 { bridge_ioctl_addspan
, sizeof(struct ifbreq
),
363 BC_F_COPYIN
|BC_F_SUSER
},
364 { bridge_ioctl_delspan
, sizeof(struct ifbreq
),
365 BC_F_COPYIN
|BC_F_SUSER
},
367 const int bridge_control_table_size
=
368 sizeof(bridge_control_table
) / sizeof(bridge_control_table
[0]);
370 LIST_HEAD(, bridge_softc
) bridge_list
;
372 struct if_clone bridge_cloner
= IF_CLONE_INITIALIZER("bridge",
374 bridge_clone_destroy
, 0, IF_MAXUNIT
);
377 bridge_modevent(module_t mod
, int type
, void *data
)
381 LIST_INIT(&bridge_list
);
382 if_clone_attach(&bridge_cloner
);
383 bridge_input_p
= bridge_input
;
384 bridge_output_p
= bridge_output_serialized
;
385 bridge_detach_cookie
= EVENTHANDLER_REGISTER(
386 ifnet_detach_event
, bridge_ifdetach
, NULL
,
387 EVENTHANDLER_PRI_ANY
);
389 bstp_linkstate_p
= bstp_linkstate
;
393 if (!LIST_EMPTY(&bridge_list
))
395 EVENTHANDLER_DEREGISTER(ifnet_detach_event
,
396 bridge_detach_cookie
);
397 if_clone_detach(&bridge_cloner
);
398 bridge_input_p
= NULL
;
399 bridge_output_p
= NULL
;
401 bstp_linkstate_p
= NULL
;
410 static moduledata_t bridge_mod
= {
416 DECLARE_MODULE(if_bridge
, bridge_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);
420 * bridge_clone_create:
422 * Create a new bridge instance.
425 bridge_clone_create(struct if_clone
*ifc
, int unit
)
427 struct bridge_softc
*sc
;
431 sc
= kmalloc(sizeof(*sc
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
432 ifp
= sc
->sc_ifp
= &sc
->sc_if
;
434 sc
->sc_brtmax
= BRIDGE_RTABLE_MAX
;
435 sc
->sc_brttimeout
= BRIDGE_RTABLE_TIMEOUT
;
436 sc
->sc_bridge_max_age
= BSTP_DEFAULT_MAX_AGE
;
437 sc
->sc_bridge_hello_time
= BSTP_DEFAULT_HELLO_TIME
;
438 sc
->sc_bridge_forward_delay
= BSTP_DEFAULT_FORWARD_DELAY
;
439 sc
->sc_bridge_priority
= BSTP_DEFAULT_BRIDGE_PRIORITY
;
440 sc
->sc_hold_time
= BSTP_DEFAULT_HOLD_TIME
;
442 /* Initialize our routing table. */
443 bridge_rtable_init(sc
);
445 callout_init(&sc
->sc_brcallout
);
446 callout_init(&sc
->sc_bstpcallout
);
448 LIST_INIT(&sc
->sc_iflist
);
449 LIST_INIT(&sc
->sc_spanlist
);
452 if_initname(ifp
, ifc
->ifc_name
, unit
);
453 ifp
->if_mtu
= ETHERMTU
;
454 ifp
->if_flags
= IFF_BROADCAST
| IFF_MULTICAST
;
455 ifp
->if_ioctl
= bridge_ioctl
;
456 ifp
->if_start
= bridge_start
;
457 ifp
->if_init
= bridge_init
;
458 ifp
->if_type
= IFT_BRIDGE
;
459 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
460 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
461 ifq_set_ready(&ifp
->if_snd
);
462 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
465 * Generate a random ethernet address and use the private AC:DE:48
469 int rnd
= karc4random();
470 bcopy(&rnd
, &eaddr
[0], 4); /* ETHER_ADDR_LEN == 6 */
472 bcopy(&rnd
, &eaddr
[2], 4); /* ETHER_ADDR_LEN == 6 */
474 eaddr
[0] &= ~1; /* clear multicast bit */
475 eaddr
[0] |= 2; /* set the LAA bit */
477 ether_ifattach(ifp
, eaddr
, NULL
);
478 /* Now undo some of the damage... */
479 ifp
->if_baudrate
= 0;
480 ifp
->if_type
= IFT_BRIDGE
;
483 LIST_INSERT_HEAD(&bridge_list
, sc
, sc_list
);
490 * bridge_clone_destroy:
492 * Destroy a bridge instance.
495 bridge_clone_destroy(struct ifnet
*ifp
)
497 struct bridge_softc
*sc
= ifp
->if_softc
;
498 struct bridge_iflist
*bif
;
500 lwkt_serialize_enter(ifp
->if_serializer
);
503 ifp
->if_flags
&= ~IFF_UP
;
505 while ((bif
= LIST_FIRST(&sc
->sc_iflist
)) != NULL
)
506 bridge_delete_member(sc
, bif
, 0);
508 while ((bif
= LIST_FIRST(&sc
->sc_spanlist
)) != NULL
) {
509 bridge_delete_span(sc
, bif
);
512 callout_stop(&sc
->sc_brcallout
);
513 callout_stop(&sc
->sc_bstpcallout
);
515 lwkt_serialize_exit(ifp
->if_serializer
);
518 LIST_REMOVE(sc
, sc_list
);
523 /* Tear down the routing table. */
524 bridge_rtable_fini(sc
);
532 * Handle a control request from the operator.
535 bridge_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
537 struct bridge_softc
*sc
= ifp
->if_softc
;
539 struct ifbreq ifbreq
;
540 struct ifbifconf ifbifconf
;
541 struct ifbareq ifbareq
;
542 struct ifbaconf ifbaconf
;
543 struct ifbrparam ifbrparam
;
545 struct ifdrv
*ifd
= (struct ifdrv
*) data
;
546 const struct bridge_control
*bc
;
549 ASSERT_SERIALIZED(ifp
->if_serializer
);
558 if (ifd
->ifd_cmd
>= bridge_control_table_size
) {
562 bc
= &bridge_control_table
[ifd
->ifd_cmd
];
564 if (cmd
== SIOCGDRVSPEC
&&
565 (bc
->bc_flags
& BC_F_COPYOUT
) == 0) {
568 } else if (cmd
== SIOCSDRVSPEC
&&
569 (bc
->bc_flags
& BC_F_COPYOUT
) != 0) {
574 if (bc
->bc_flags
& BC_F_SUSER
) {
575 error
= suser_cred(cr
, NULL_CRED_OKAY
);
580 if (ifd
->ifd_len
!= bc
->bc_argsize
||
581 ifd
->ifd_len
> sizeof(args
)) {
586 memset(&args
, 0, sizeof(args
));
587 if (bc
->bc_flags
& BC_F_COPYIN
) {
588 error
= copyin(ifd
->ifd_data
, &args
, ifd
->ifd_len
);
593 error
= bc
->bc_func(sc
, &args
);
597 if (bc
->bc_flags
& BC_F_COPYOUT
)
598 error
= copyout(&args
, ifd
->ifd_data
, ifd
->ifd_len
);
602 if (!(ifp
->if_flags
& IFF_UP
) &&
603 (ifp
->if_flags
& IFF_RUNNING
)) {
605 * If interface is marked down and it is running,
609 } else if ((ifp
->if_flags
& IFF_UP
) &&
610 !(ifp
->if_flags
& IFF_RUNNING
)) {
612 * If interface is marked up and it is stopped, then
620 /* Do not allow the MTU to be changed on the bridge */
625 error
= ether_ioctl(ifp
, cmd
, data
);
634 * Clear or restore unwanted capabilities on the member interface
637 bridge_mutecaps(struct bridge_iflist
*bif
, int mute
)
639 struct ifnet
*ifp
= bif
->bif_ifp
;
643 if (ifp
->if_ioctl
== NULL
)
646 bzero(&ifr
, sizeof(ifr
));
647 ifr
.ifr_reqcap
= ifp
->if_capenable
;
650 /* mask off and save capabilities */
651 bif
->bif_mutecap
= ifr
.ifr_reqcap
& BRIDGE_IFCAPS_MASK
;
652 if (bif
->bif_mutecap
!= 0)
653 ifr
.ifr_reqcap
&= ~BRIDGE_IFCAPS_MASK
;
655 /* restore muted capabilities */
656 ifr
.ifr_reqcap
|= bif
->bif_mutecap
;
658 if (bif
->bif_mutecap
!= 0) {
659 lwkt_serialize_enter(ifp
->if_serializer
);
660 error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFCAP
, (caddr_t
)&ifr
, NULL
);
661 lwkt_serialize_exit(ifp
->if_serializer
);
666 * bridge_lookup_member:
668 * Lookup a bridge member interface.
670 static struct bridge_iflist
*
671 bridge_lookup_member(struct bridge_softc
*sc
, const char *name
)
673 struct bridge_iflist
*bif
;
676 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
678 if (strcmp(ifp
->if_xname
, name
) == 0)
686 * bridge_lookup_member_if:
688 * Lookup a bridge member interface by ifnet*.
690 static struct bridge_iflist
*
691 bridge_lookup_member_if(struct bridge_softc
*sc
, struct ifnet
*member_ifp
)
693 struct bridge_iflist
*bif
;
695 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
696 if (bif
->bif_ifp
== member_ifp
)
704 * bridge_delete_member:
706 * Delete the specified member interface.
709 bridge_delete_member(struct bridge_softc
*sc
, struct bridge_iflist
*bif
,
712 struct ifnet
*ifs
= bif
->bif_ifp
;
715 switch (ifs
->if_type
) {
719 * Take the interface out of promiscuous mode.
721 (void) ifpromisc(ifs
, 0);
722 bridge_mutecaps(bif
, 0);
730 panic("bridge_delete_member: impossible");
736 ifs
->if_bridge
= NULL
;
737 LIST_REMOVE(bif
, bif_next
);
739 bridge_rtdelete(sc
, ifs
, IFBF_FLUSHALL
);
741 kfree(bif
, M_DEVBUF
);
743 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
744 bstp_initialization(sc
);
748 * bridge_delete_span:
750 * Delete the specified span interface.
753 bridge_delete_span(struct bridge_softc
*sc
, struct bridge_iflist
*bif
)
755 KASSERT(bif
->bif_ifp
->if_bridge
== NULL
,
756 ("%s: not a span interface", __func__
));
758 LIST_REMOVE(bif
, bif_next
);
759 kfree(bif
, M_DEVBUF
);
763 bridge_ioctl_add(struct bridge_softc
*sc
, void *arg
)
765 struct ifbreq
*req
= arg
;
766 struct bridge_iflist
*bif
= NULL
;
770 ifs
= ifunit(req
->ifbr_ifsname
);
774 /* If it's in the span list, it can't be a member. */
775 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
776 if (ifs
== bif
->bif_ifp
)
779 /* Allow the first Ethernet member to define the MTU */
780 if (ifs
->if_type
!= IFT_GIF
) {
781 if (LIST_EMPTY(&sc
->sc_iflist
))
782 sc
->sc_ifp
->if_mtu
= ifs
->if_mtu
;
783 else if (sc
->sc_ifp
->if_mtu
!= ifs
->if_mtu
) {
784 if_printf(sc
->sc_ifp
, "invalid MTU for %s\n",
790 if (ifs
->if_bridge
== sc
)
793 if (ifs
->if_bridge
!= NULL
)
796 bif
= kmalloc(sizeof(*bif
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
798 bif
->bif_flags
= IFBIF_LEARNING
| IFBIF_DISCOVER
;
799 bif
->bif_priority
= BSTP_DEFAULT_PORT_PRIORITY
;
800 bif
->bif_path_cost
= BSTP_DEFAULT_PATH_COST
;
802 switch (ifs
->if_type
) {
806 * Place the interface into promiscuous mode.
808 error
= ifpromisc(ifs
, 1);
812 bridge_mutecaps(bif
, 1);
815 case IFT_GIF
: /* :^) */
825 LIST_INSERT_HEAD(&sc
->sc_iflist
, bif
, bif_next
);
827 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
828 bstp_initialization(sc
);
835 kfree(bif
, M_DEVBUF
);
841 bridge_ioctl_del(struct bridge_softc
*sc
, void *arg
)
843 struct ifbreq
*req
= arg
;
844 struct bridge_iflist
*bif
;
846 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
850 bridge_delete_member(sc
, bif
, 0);
856 bridge_ioctl_gifflags(struct bridge_softc
*sc
, void *arg
)
858 struct ifbreq
*req
= arg
;
859 struct bridge_iflist
*bif
;
861 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
865 req
->ifbr_ifsflags
= bif
->bif_flags
;
866 req
->ifbr_state
= bif
->bif_state
;
867 req
->ifbr_priority
= bif
->bif_priority
;
868 req
->ifbr_path_cost
= bif
->bif_path_cost
;
869 req
->ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
875 bridge_ioctl_sifflags(struct bridge_softc
*sc
, void *arg
)
877 struct ifbreq
*req
= arg
;
878 struct bridge_iflist
*bif
;
880 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
884 if (req
->ifbr_ifsflags
& IFBIF_SPAN
)
885 /* SPAN is readonly */
888 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
889 switch (bif
->bif_ifp
->if_type
) {
891 /* These can do spanning tree. */
895 /* Nothing else can. */
900 bif
->bif_flags
= req
->ifbr_ifsflags
;
902 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
903 bstp_initialization(sc
);
909 bridge_ioctl_scache(struct bridge_softc
*sc
, void *arg
)
911 struct ifbrparam
*param
= arg
;
913 sc
->sc_brtmax
= param
->ifbrp_csize
;
920 bridge_ioctl_gcache(struct bridge_softc
*sc
, void *arg
)
922 struct ifbrparam
*param
= arg
;
924 param
->ifbrp_csize
= sc
->sc_brtmax
;
930 bridge_ioctl_gifs(struct bridge_softc
*sc
, void *arg
)
932 struct ifbifconf
*bifc
= arg
;
933 struct bridge_iflist
*bif
;
935 int count
, len
, error
= 0;
938 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
)
940 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
943 if (bifc
->ifbic_len
== 0) {
944 bifc
->ifbic_len
= sizeof(breq
) * count
;
949 len
= bifc
->ifbic_len
;
950 memset(&breq
, 0, sizeof breq
);
951 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
952 if (len
< sizeof(breq
))
955 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
956 sizeof(breq
.ifbr_ifsname
));
957 breq
.ifbr_ifsflags
= bif
->bif_flags
;
958 breq
.ifbr_state
= bif
->bif_state
;
959 breq
.ifbr_priority
= bif
->bif_priority
;
960 breq
.ifbr_path_cost
= bif
->bif_path_cost
;
961 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
962 error
= copyout(&breq
, bifc
->ifbic_req
+ count
, sizeof(breq
));
968 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
969 if (len
< sizeof(breq
))
972 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
973 sizeof(breq
.ifbr_ifsname
));
974 breq
.ifbr_ifsflags
= bif
->bif_flags
;
975 breq
.ifbr_state
= bif
->bif_state
;
976 breq
.ifbr_priority
= bif
->bif_priority
;
977 breq
.ifbr_path_cost
= bif
->bif_path_cost
;
978 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
979 error
= copyout(&breq
, bifc
->ifbic_req
+ count
, sizeof(breq
));
986 bifc
->ifbic_len
= sizeof(breq
) * count
;
991 bridge_ioctl_rts(struct bridge_softc
*sc
, void *arg
)
993 struct ifbaconf
*bac
= arg
;
994 struct bridge_rtnode
*brt
;
995 struct ifbareq bareq
;
996 int count
= 0, error
= 0, len
;
998 if (bac
->ifbac_len
== 0)
1001 len
= bac
->ifbac_len
;
1002 memset(&bareq
, 0, sizeof(bareq
));
1003 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
1004 if (len
< sizeof(bareq
))
1006 strlcpy(bareq
.ifba_ifsname
, brt
->brt_ifp
->if_xname
,
1007 sizeof(bareq
.ifba_ifsname
));
1008 memcpy(bareq
.ifba_dst
, brt
->brt_addr
, sizeof(brt
->brt_addr
));
1009 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
&&
1010 time_second
< brt
->brt_expire
)
1011 bareq
.ifba_expire
= brt
->brt_expire
- time_second
;
1013 bareq
.ifba_expire
= 0;
1014 bareq
.ifba_flags
= brt
->brt_flags
;
1016 error
= copyout(&bareq
, bac
->ifbac_req
+ count
, sizeof(bareq
));
1020 len
-= sizeof(bareq
);
1023 bac
->ifbac_len
= sizeof(bareq
) * count
;
1028 bridge_ioctl_saddr(struct bridge_softc
*sc
, void *arg
)
1030 struct ifbareq
*req
= arg
;
1031 struct bridge_iflist
*bif
;
1034 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
1038 error
= bridge_rtupdate(sc
, req
->ifba_dst
, bif
->bif_ifp
, 1,
1045 bridge_ioctl_sto(struct bridge_softc
*sc
, void *arg
)
1047 struct ifbrparam
*param
= arg
;
1049 sc
->sc_brttimeout
= param
->ifbrp_ctime
;
1055 bridge_ioctl_gto(struct bridge_softc
*sc
, void *arg
)
1057 struct ifbrparam
*param
= arg
;
1059 param
->ifbrp_ctime
= sc
->sc_brttimeout
;
1065 bridge_ioctl_daddr(struct bridge_softc
*sc
, void *arg
)
1067 struct ifbareq
*req
= arg
;
1069 return (bridge_rtdaddr(sc
, req
->ifba_dst
));
1073 bridge_ioctl_flush(struct bridge_softc
*sc
, void *arg
)
1075 struct ifbreq
*req
= arg
;
1077 bridge_rtflush(sc
, req
->ifbr_ifsflags
);
1083 bridge_ioctl_gpri(struct bridge_softc
*sc
, void *arg
)
1085 struct ifbrparam
*param
= arg
;
1087 param
->ifbrp_prio
= sc
->sc_bridge_priority
;
1093 bridge_ioctl_spri(struct bridge_softc
*sc
, void *arg
)
1095 struct ifbrparam
*param
= arg
;
1097 sc
->sc_bridge_priority
= param
->ifbrp_prio
;
1099 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1100 bstp_initialization(sc
);
1106 bridge_ioctl_ght(struct bridge_softc
*sc
, void *arg
)
1108 struct ifbrparam
*param
= arg
;
1110 param
->ifbrp_hellotime
= sc
->sc_bridge_hello_time
>> 8;
1116 bridge_ioctl_sht(struct bridge_softc
*sc
, void *arg
)
1118 struct ifbrparam
*param
= arg
;
1120 if (param
->ifbrp_hellotime
== 0)
1122 sc
->sc_bridge_hello_time
= param
->ifbrp_hellotime
<< 8;
1124 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1125 bstp_initialization(sc
);
1131 bridge_ioctl_gfd(struct bridge_softc
*sc
, void *arg
)
1133 struct ifbrparam
*param
= arg
;
1135 param
->ifbrp_fwddelay
= sc
->sc_bridge_forward_delay
>> 8;
1141 bridge_ioctl_sfd(struct bridge_softc
*sc
, void *arg
)
1143 struct ifbrparam
*param
= arg
;
1145 if (param
->ifbrp_fwddelay
== 0)
1147 sc
->sc_bridge_forward_delay
= param
->ifbrp_fwddelay
<< 8;
1149 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1150 bstp_initialization(sc
);
1156 bridge_ioctl_gma(struct bridge_softc
*sc
, void *arg
)
1158 struct ifbrparam
*param
= arg
;
1160 param
->ifbrp_maxage
= sc
->sc_bridge_max_age
>> 8;
1166 bridge_ioctl_sma(struct bridge_softc
*sc
, void *arg
)
1168 struct ifbrparam
*param
= arg
;
1170 if (param
->ifbrp_maxage
== 0)
1172 sc
->sc_bridge_max_age
= param
->ifbrp_maxage
<< 8;
1174 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1175 bstp_initialization(sc
);
1181 bridge_ioctl_sifprio(struct bridge_softc
*sc
, void *arg
)
1183 struct ifbreq
*req
= arg
;
1184 struct bridge_iflist
*bif
;
1186 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1190 bif
->bif_priority
= req
->ifbr_priority
;
1192 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1193 bstp_initialization(sc
);
1199 bridge_ioctl_sifcost(struct bridge_softc
*sc
, void *arg
)
1201 struct ifbreq
*req
= arg
;
1202 struct bridge_iflist
*bif
;
1204 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1208 bif
->bif_path_cost
= req
->ifbr_path_cost
;
1210 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1211 bstp_initialization(sc
);
1217 bridge_ioctl_addspan(struct bridge_softc
*sc
, void *arg
)
1219 struct ifbreq
*req
= arg
;
1220 struct bridge_iflist
*bif
= NULL
;
1223 ifs
= ifunit(req
->ifbr_ifsname
);
1227 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1228 if (ifs
== bif
->bif_ifp
)
1231 if (ifs
->if_bridge
!= NULL
)
1234 switch (ifs
->if_type
) {
1243 bif
= kmalloc(sizeof(*bif
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
1246 bif
->bif_flags
= IFBIF_SPAN
;
1248 LIST_INSERT_HEAD(&sc
->sc_spanlist
, bif
, bif_next
);
1254 bridge_ioctl_delspan(struct bridge_softc
*sc
, void *arg
)
1256 struct ifbreq
*req
= arg
;
1257 struct bridge_iflist
*bif
;
1260 ifs
= ifunit(req
->ifbr_ifsname
);
1264 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1265 if (ifs
== bif
->bif_ifp
)
1271 bridge_delete_span(sc
, bif
);
1279 * Detach an interface from a bridge. Called when a member
1280 * interface is detaching.
1283 bridge_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
1285 struct bridge_softc
*sc
= ifp
->if_bridge
;
1286 struct bridge_iflist
*bif
;
1288 /* Check if the interface is a bridge member */
1290 lwkt_serialize_enter(ifp
->if_serializer
);
1292 bif
= bridge_lookup_member_if(sc
, ifp
);
1294 bridge_delete_member(sc
, bif
, 1);
1296 lwkt_serialize_exit(ifp
->if_serializer
);
1300 /* Check if the interface is a span port */
1301 LIST_FOREACH(sc
, &bridge_list
, sc_list
) {
1302 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1303 if (ifp
== bif
->bif_ifp
) {
1304 bridge_delete_span(sc
, bif
);
1313 * Initialize a bridge interface.
1316 bridge_init(void *xsc
)
1318 struct bridge_softc
*sc
= (struct bridge_softc
*)xsc
;
1319 struct ifnet
*ifp
= sc
->sc_ifp
;
1321 ASSERT_SERIALIZED(ifp
->if_serializer
);
1323 if (ifp
->if_flags
& IFF_RUNNING
)
1326 callout_reset(&sc
->sc_brcallout
, bridge_rtable_prune_period
* hz
,
1329 ifp
->if_flags
|= IFF_RUNNING
;
1330 bstp_initialization(sc
);
1337 * Stop the bridge interface.
1340 bridge_stop(struct ifnet
*ifp
)
1342 struct bridge_softc
*sc
= ifp
->if_softc
;
1344 ASSERT_SERIALIZED(ifp
->if_serializer
);
1346 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
1349 callout_stop(&sc
->sc_brcallout
);
1352 bridge_rtflush(sc
, IFBF_FLUSHDYN
);
1354 ifp
->if_flags
&= ~IFF_RUNNING
;
1358 bridge_enqueue_internal(struct ifnet
*dst_ifp
, struct mbuf
*m
,
1359 netisr_fn_t handler
)
1361 struct netmsg_packet
*nmp
;
1363 int cpu
= mycpu
->gd_cpuid
;
1365 while (m
->m_type
== MT_TAG
) {
1366 /* XXX see ether_output_frame for full rules check */
1370 nmp
= &m
->m_hdr
.mh_netmsg
;
1371 netmsg_init(&nmp
->nm_netmsg
, &netisr_apanic_rport
, 0, handler
);
1373 nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
= dst_ifp
;
1375 port
= cpu_portfn(cpu
);
1376 lwkt_sendmsg(port
, &nmp
->nm_netmsg
.nm_lmsg
);
1380 bridge_pfil_enqueue(struct ifnet
*dst_ifp
, struct mbuf
*m
,
1383 netisr_fn_t handler
;
1385 if (runfilt
&& (inet_pfil_hook
.ph_hashooks
> 0
1387 || inet6_pfil_hook
.ph_hashooks
> 0
1390 handler
= bridge_pfil_enqueue_handler
;
1392 handler
= bridge_enqueue_handler
;
1394 bridge_enqueue_internal(dst_ifp
, m
, handler
);
1400 * Enqueue a packet on a bridge member interface.
1404 bridge_enqueue(struct ifnet
*dst_ifp
, struct mbuf
*m
)
1406 bridge_enqueue_internal(dst_ifp
, m
, bridge_enqueue_handler
);
1410 * bridge_output_serialized:
1412 * Send output from a bridge member interface. This
1413 * performs the bridging function for locally originated
1416 * The mbuf has the Ethernet header already attached. We must
1417 * enqueue or free the mbuf before returning.
1420 bridge_output_serialized(struct ifnet
*ifp
, struct mbuf
*m
,
1421 struct sockaddr
*sa
, struct rtentry
*rt
)
1423 struct ether_header
*eh
;
1424 struct ifnet
*dst_if
;
1425 struct bridge_softc
*sc
;
1427 sc
= ifp
->if_bridge
;
1429 ASSERT_SERIALIZED(ifp
->if_serializer
);
1431 if (m
->m_len
< ETHER_HDR_LEN
) {
1432 m
= m_pullup(m
, ETHER_HDR_LEN
);
1438 * Serialize our bridge interface. We have to get rid of the
1439 * originating interface lock to avoid a deadlock.
1441 lwkt_serialize_exit(ifp
->if_serializer
);
1442 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
1444 eh
= mtod(m
, struct ether_header
*);
1447 * If bridge is down, but the original output interface is up,
1448 * go ahead and send out that interface. Otherwise, the packet
1451 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) == 0) {
1457 * If the packet is a multicast, or we don't know a better way to
1458 * get there, send to all interfaces.
1460 if (ETHER_IS_MULTICAST(eh
->ether_dhost
))
1463 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1464 if (dst_if
== NULL
) {
1465 struct bridge_iflist
*bif
;
1471 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1472 dst_if
= bif
->bif_ifp
;
1473 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1477 * If this is not the original output interface,
1478 * and the interface is participating in spanning
1479 * tree, make sure the port is in a state that
1480 * allows forwarding.
1482 if (dst_if
!= ifp
&&
1483 (bif
->bif_flags
& IFBIF_STP
) != 0) {
1484 switch (bif
->bif_state
) {
1485 case BSTP_IFSTATE_BLOCKING
:
1486 case BSTP_IFSTATE_LISTENING
:
1487 case BSTP_IFSTATE_DISABLED
:
1492 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
1496 mc
= m_copypacket(m
, MB_DONTWAIT
);
1498 sc
->sc_ifp
->if_oerrors
++;
1502 bridge_enqueue(dst_if
, mc
);
1506 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1512 * XXX Spanning tree consideration here?
1516 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1517 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
1520 bridge_enqueue(dst_if
, m
);
1523 lwkt_serialize_enter(ifp
->if_serializer
);
1530 * Start output on a bridge.
1534 bridge_start(struct ifnet
*ifp
)
1536 struct bridge_softc
*sc
= ifp
->if_softc
;
1538 ASSERT_SERIALIZED(ifp
->if_serializer
);
1540 ifp
->if_flags
|= IFF_OACTIVE
;
1542 struct ifnet
*dst_if
= NULL
;
1543 struct ether_header
*eh
;
1546 m
= ifq_dequeue(&ifp
->if_snd
, NULL
);
1550 if (m
->m_len
< sizeof(*eh
)) {
1551 m
= m_pullup(m
, sizeof(*eh
));
1557 eh
= mtod(m
, struct ether_header
*);
1562 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
1563 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1566 bridge_broadcast(sc
, ifp
, m
, 0);
1568 bridge_enqueue(dst_if
, m
);
1570 ifp
->if_flags
&= ~IFF_OACTIVE
;
1576 * The forwarding function of the bridge.
1579 bridge_forward(struct bridge_softc
*sc
, struct mbuf
*m
)
1581 struct bridge_iflist
*bif
;
1582 struct ifnet
*src_if
, *dst_if
, *ifp
;
1583 struct ether_header
*eh
;
1585 src_if
= m
->m_pkthdr
.rcvif
;
1588 ASSERT_SERIALIZED(ifp
->if_serializer
);
1591 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1594 * Look up the bridge_iflist.
1596 bif
= bridge_lookup_member_if(sc
, src_if
);
1598 /* Interface is not a bridge member (anymore?) */
1603 if (bif
->bif_flags
& IFBIF_STP
) {
1604 switch (bif
->bif_state
) {
1605 case BSTP_IFSTATE_BLOCKING
:
1606 case BSTP_IFSTATE_LISTENING
:
1607 case BSTP_IFSTATE_DISABLED
:
1613 eh
= mtod(m
, struct ether_header
*);
1616 * If the interface is learning, and the source
1617 * address is valid and not multicast, record
1620 if ((bif
->bif_flags
& IFBIF_LEARNING
) != 0 &&
1621 ETHER_IS_MULTICAST(eh
->ether_shost
) == 0 &&
1622 (eh
->ether_shost
[0] == 0 &&
1623 eh
->ether_shost
[1] == 0 &&
1624 eh
->ether_shost
[2] == 0 &&
1625 eh
->ether_shost
[3] == 0 &&
1626 eh
->ether_shost
[4] == 0 &&
1627 eh
->ether_shost
[5] == 0) == 0) {
1628 bridge_rtupdate(sc
, eh
->ether_shost
, src_if
, 0, IFBAF_DYNAMIC
);
1631 if ((bif
->bif_flags
& IFBIF_STP
) != 0 &&
1632 bif
->bif_state
== BSTP_IFSTATE_LEARNING
) {
1638 * At this point, the port either doesn't participate
1639 * in spanning tree or it is in the forwarding state.
1643 * If the packet is unicast, destined for someone on
1644 * "this" side of the bridge, drop it.
1646 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) {
1647 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1648 if (src_if
== dst_if
) {
1653 /* ...forward it to all interfaces. */
1654 sc
->sc_ifp
->if_imcasts
++;
1658 if (dst_if
== NULL
) {
1659 bridge_broadcast(sc
, src_if
, m
, 1);
1664 * At this point, we're dealing with a unicast frame
1665 * going to a different interface.
1667 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
1671 bif
= bridge_lookup_member_if(sc
, dst_if
);
1673 /* Not a member of the bridge (anymore?) */
1678 if (bif
->bif_flags
& IFBIF_STP
) {
1679 switch (bif
->bif_state
) {
1680 case BSTP_IFSTATE_DISABLED
:
1681 case BSTP_IFSTATE_BLOCKING
:
1687 lwkt_serialize_exit(ifp
->if_serializer
);
1689 /* run the packet filter */
1690 if (inet_pfil_hook
.ph_hashooks
> 0
1692 || inet6_pfil_hook
.ph_hashooks
> 0
1695 if (bridge_pfil(&m
, ifp
, src_if
, PFIL_IN
) != 0)
1700 if (bridge_pfil(&m
, ifp
, dst_if
, PFIL_OUT
) != 0)
1705 bridge_handoff(dst_if
, m
);
1708 * ifp's serializer was held on entry and is expected to be held
1712 lwkt_serialize_enter(ifp
->if_serializer
);
1718 * Receive input from a member interface. Queue the packet for
1719 * bridging if it is not for us.
1721 static struct mbuf
*
1722 bridge_input(struct ifnet
*ifp
, struct mbuf
*m
)
1724 struct bridge_softc
*sc
= ifp
->if_bridge
;
1725 struct bridge_iflist
*bif
;
1726 struct ifnet
*bifp
, *new_ifp
;
1727 struct ether_header
*eh
;
1728 struct mbuf
*mc
, *mc2
;
1733 lwkt_serialize_enter(bifp
->if_serializer
);
1735 if ((bifp
->if_flags
& IFF_RUNNING
) == 0)
1739 * Implement support for bridge monitoring. If this flag has been
1740 * set on this interface, discard the packet once we push it through
1741 * the bpf(4) machinery, but before we do, increment the byte and
1742 * packet counters associated with this interface.
1744 if ((bifp
->if_flags
& IFF_MONITOR
) != 0) {
1745 m
->m_pkthdr
.rcvif
= bifp
;
1747 bifp
->if_ipackets
++;
1748 bifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1754 eh
= mtod(m
, struct ether_header
*);
1756 m
->m_flags
&= ~M_PROTO1
; /* XXX Hack - loop prevention */
1758 if (memcmp(eh
->ether_dhost
, IF_LLADDR(bifp
), ETHER_ADDR_LEN
) == 0) {
1760 * If the packet is for us, set the packets source as the
1761 * bridge, and return the packet back to ifnet.if_input for
1764 KASSERT(bifp
->if_bridge
== NULL
,
1765 ("loop created in bridge_input"));
1771 * Tap all packets arriving on the bridge, no matter if
1772 * they are local destinations or not. In is in.
1776 bif
= bridge_lookup_member_if(sc
, ifp
);
1782 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
1783 /* Tap off 802.1D packets; they do not get forwarded. */
1784 if (memcmp(eh
->ether_dhost
, bstp_etheraddr
,
1785 ETHER_ADDR_LEN
) == 0) {
1786 m
= bstp_input(ifp
, m
);
1788 ("attempt to deliver 802.1D packet\n"));
1792 if (bif
->bif_flags
& IFBIF_STP
) {
1793 switch (bif
->bif_state
) {
1794 case BSTP_IFSTATE_BLOCKING
:
1795 case BSTP_IFSTATE_LISTENING
:
1796 case BSTP_IFSTATE_DISABLED
:
1802 * Make a deep copy of the packet and enqueue the copy
1803 * for bridge processing; return the original packet for
1806 mc
= m_dup(m
, MB_DONTWAIT
);
1810 bridge_forward(sc
, mc
);
1813 * Reinject the mbuf as arriving on the bridge so we have a
1814 * chance at claiming multicast packets. We can not loop back
1815 * here from ether_input as a bridge is never a member of a
1818 KASSERT(bifp
->if_bridge
== NULL
,
1819 ("loop created in bridge_input"));
1820 mc2
= m_dup(m
, MB_DONTWAIT
);
1823 /* Keep the layer3 header aligned */
1824 int i
= min(mc2
->m_pkthdr
.len
, max_protohdr
);
1825 mc2
= m_copyup(mc2
, i
, ETHER_ALIGN
);
1829 mc2
->m_pkthdr
.rcvif
= bifp
;
1830 bifp
->if_ipackets
++;
1831 bifp
->if_input(bifp
, mc2
);
1834 /* Return the original packet for local processing. */
1838 if (bif
->bif_flags
& IFBIF_STP
) {
1839 switch (bif
->bif_state
) {
1840 case BSTP_IFSTATE_BLOCKING
:
1841 case BSTP_IFSTATE_LISTENING
:
1842 case BSTP_IFSTATE_DISABLED
:
1848 * Unicast. Make sure it's not for us.
1850 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1851 if (bif
->bif_ifp
->if_type
!= IFT_ETHER
)
1854 /* It is destined for us. */
1855 if (memcmp(IF_LLADDR(bif
->bif_ifp
), eh
->ether_dhost
,
1856 ETHER_ADDR_LEN
) == 0) {
1857 if (bif
->bif_flags
& IFBIF_LEARNING
) {
1859 eh
->ether_shost
, ifp
, 0, IFBAF_DYNAMIC
);
1862 if (bif
->bif_ifp
!= ifp
) {
1863 /* XXX loop prevention */
1864 m
->m_flags
|= M_PROTO1
;
1865 new_ifp
= bif
->bif_ifp
;
1870 /* We just received a packet that we sent out. */
1871 if (memcmp(IF_LLADDR(bif
->bif_ifp
), eh
->ether_shost
,
1872 ETHER_ADDR_LEN
) == 0) {
1879 /* Perform the bridge forwarding function. */
1880 bridge_forward(sc
, m
);
1883 lwkt_serialize_exit(bifp
->if_serializer
);
1885 if (new_ifp
!= NULL
) {
1886 lwkt_serialize_enter(new_ifp
->if_serializer
);
1888 m
->m_pkthdr
.rcvif
= new_ifp
;
1889 new_ifp
->if_ipackets
++;
1890 new_ifp
->if_input(new_ifp
, m
);
1893 lwkt_serialize_exit(new_ifp
->if_serializer
);
1901 * Send a frame to all interfaces that are members of
1902 * the bridge, except for the one on which the packet
1906 bridge_broadcast(struct bridge_softc
*sc
, struct ifnet
*src_if
,
1907 struct mbuf
*m
, int runfilt
)
1909 struct bridge_iflist
*bif
;
1911 struct ifnet
*dst_if
, *bifp
;
1916 ASSERT_SERIALIZED(bifp
->if_serializer
);
1918 /* run the packet filter */
1919 if (runfilt
&& (inet_pfil_hook
.ph_hashooks
> 0
1921 || inet6_pfil_hook
.ph_hashooks
> 0
1924 lwkt_serialize_exit(bifp
->if_serializer
);
1926 /* Filter on the bridge interface before broadcasting */
1928 if (bridge_pfil(&m
, bifp
, src_if
, PFIL_IN
) != 0)
1933 if (bridge_pfil(&m
, bifp
, NULL
, PFIL_OUT
) != 0)
1936 lwkt_serialize_enter(bifp
->if_serializer
);
1941 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1942 dst_if
= bif
->bif_ifp
;
1943 if (dst_if
== src_if
)
1946 if (bif
->bif_flags
& IFBIF_STP
) {
1947 switch (bif
->bif_state
) {
1948 case BSTP_IFSTATE_BLOCKING
:
1949 case BSTP_IFSTATE_DISABLED
:
1954 if ((bif
->bif_flags
& IFBIF_DISCOVER
) == 0 &&
1955 (m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
1958 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1961 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
1965 mc
= m_copypacket(m
, MB_DONTWAIT
);
1967 sc
->sc_ifp
->if_oerrors
++;
1971 bridge_pfil_enqueue(dst_if
, mc
, runfilt
);
1980 * Duplicate a packet out one or more interfaces that are in span mode,
1981 * the original mbuf is unmodified.
1984 bridge_span(struct bridge_softc
*sc
, struct mbuf
*m
)
1986 struct bridge_iflist
*bif
;
1987 struct ifnet
*dst_if
;
1990 if (LIST_EMPTY(&sc
->sc_spanlist
))
1993 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
1994 dst_if
= bif
->bif_ifp
;
1996 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1999 mc
= m_copypacket(m
, MB_DONTWAIT
);
2001 sc
->sc_ifp
->if_oerrors
++;
2005 bridge_enqueue(dst_if
, mc
);
2012 * Add a bridge routing entry.
2013 * Can be called from interrupt context.
2016 bridge_rtupdate(struct bridge_softc
*sc
, const uint8_t *dst
,
2017 struct ifnet
*dst_if
, int setflags
, uint8_t flags
)
2019 struct bridge_rtnode
*brt
;
2023 * A route for this destination might already exist. If so,
2024 * update it, otherwise create a new one.
2026 if ((brt
= bridge_rtnode_lookup(sc
, dst
)) == NULL
) {
2027 if (sc
->sc_brtcnt
>= sc
->sc_brtmax
)
2031 * Allocate a new bridge forwarding node, and
2032 * initialize the expiration time and Ethernet
2035 brt
= kmalloc(sizeof(struct bridge_rtnode
), M_DEVBUF
,
2036 M_INTNOWAIT
|M_ZERO
);
2040 brt
->brt_flags
= IFBAF_DYNAMIC
;
2041 memcpy(brt
->brt_addr
, dst
, ETHER_ADDR_LEN
);
2043 if ((error
= bridge_rtnode_insert(sc
, brt
)) != 0) {
2044 kfree(brt
, M_DEVBUF
);
2049 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2050 brt
->brt_ifp
= dst_if
;
2051 if ((flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2052 brt
->brt_expire
= time_second
+ sc
->sc_brttimeout
;
2054 brt
->brt_flags
= flags
;
2062 * Lookup the destination interface for an address.
2064 static struct ifnet
*
2065 bridge_rtlookup(struct bridge_softc
*sc
, const uint8_t *addr
)
2067 struct bridge_rtnode
*brt
;
2069 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
2072 return (brt
->brt_ifp
);
2078 * Trim the routine table so that we have a number
2079 * of routing entries less than or equal to the
2083 bridge_rttrim(struct bridge_softc
*sc
)
2085 struct bridge_rtnode
*brt
, *nbrt
;
2087 /* Make sure we actually need to do this. */
2088 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2091 /* Force an aging cycle; this might trim enough addresses. */
2093 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2096 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2097 nbrt
= LIST_NEXT(brt
, brt_list
);
2098 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2099 bridge_rtnode_destroy(sc
, brt
);
2100 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2109 * Aging timer for the bridge.
2112 bridge_timer(void *arg
)
2114 struct bridge_softc
*sc
= arg
;
2116 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
2120 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
2121 callout_reset(&sc
->sc_brcallout
,
2122 bridge_rtable_prune_period
* hz
, bridge_timer
, sc
);
2124 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
2130 * Perform an aging cycle.
2133 bridge_rtage(struct bridge_softc
*sc
)
2135 struct bridge_rtnode
*brt
, *nbrt
;
2137 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2138 nbrt
= LIST_NEXT(brt
, brt_list
);
2139 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2140 if (time_second
>= brt
->brt_expire
)
2141 bridge_rtnode_destroy(sc
, brt
);
2149 * Remove all dynamic addresses from the bridge.
2152 bridge_rtflush(struct bridge_softc
*sc
, int full
)
2154 struct bridge_rtnode
*brt
, *nbrt
;
2156 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2157 nbrt
= LIST_NEXT(brt
, brt_list
);
2158 if (full
|| (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2159 bridge_rtnode_destroy(sc
, brt
);
2166 * Remove an address from the table.
2169 bridge_rtdaddr(struct bridge_softc
*sc
, const uint8_t *addr
)
2171 struct bridge_rtnode
*brt
;
2173 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
2176 bridge_rtnode_destroy(sc
, brt
);
2183 * Delete routes to a speicifc member interface.
2186 bridge_rtdelete(struct bridge_softc
*sc
, struct ifnet
*ifp
, int full
)
2188 struct bridge_rtnode
*brt
, *nbrt
;
2190 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2191 nbrt
= LIST_NEXT(brt
, brt_list
);
2192 if (brt
->brt_ifp
== ifp
&& (full
||
2193 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
))
2194 bridge_rtnode_destroy(sc
, brt
);
2199 * bridge_rtable_init:
2201 * Initialize the route table for this bridge.
2204 bridge_rtable_init(struct bridge_softc
*sc
)
2208 sc
->sc_rthash
= kmalloc(sizeof(*sc
->sc_rthash
) * BRIDGE_RTHASH_SIZE
,
2209 M_DEVBUF
, M_WAITOK
);
2211 for (i
= 0; i
< BRIDGE_RTHASH_SIZE
; i
++)
2212 LIST_INIT(&sc
->sc_rthash
[i
]);
2214 sc
->sc_rthash_key
= karc4random();
2216 LIST_INIT(&sc
->sc_rtlist
);
2222 * bridge_rtable_fini:
2224 * Deconstruct the route table for this bridge.
2227 bridge_rtable_fini(struct bridge_softc
*sc
)
2230 kfree(sc
->sc_rthash
, M_DEVBUF
);
2234 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2235 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2237 #define mix(a, b, c) \
2239 a -= b; a -= c; a ^= (c >> 13); \
2240 b -= c; b -= a; b ^= (a << 8); \
2241 c -= a; c -= b; c ^= (b >> 13); \
2242 a -= b; a -= c; a ^= (c >> 12); \
2243 b -= c; b -= a; b ^= (a << 16); \
2244 c -= a; c -= b; c ^= (b >> 5); \
2245 a -= b; a -= c; a ^= (c >> 3); \
2246 b -= c; b -= a; b ^= (a << 10); \
2247 c -= a; c -= b; c ^= (b >> 15); \
2248 } while (/*CONSTCOND*/0)
2250 static __inline
uint32_t
2251 bridge_rthash(struct bridge_softc
*sc
, const uint8_t *addr
)
2253 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= sc
->sc_rthash_key
;
2264 return (c
& BRIDGE_RTHASH_MASK
);
2270 bridge_rtnode_addr_cmp(const uint8_t *a
, const uint8_t *b
)
2274 for (i
= 0, d
= 0; i
< ETHER_ADDR_LEN
&& d
== 0; i
++) {
2275 d
= ((int)a
[i
]) - ((int)b
[i
]);
2282 * bridge_rtnode_lookup:
2284 * Look up a bridge route node for the specified destination.
2286 static struct bridge_rtnode
*
2287 bridge_rtnode_lookup(struct bridge_softc
*sc
, const uint8_t *addr
)
2289 struct bridge_rtnode
*brt
;
2293 hash
= bridge_rthash(sc
, addr
);
2294 LIST_FOREACH(brt
, &sc
->sc_rthash
[hash
], brt_hash
) {
2295 dir
= bridge_rtnode_addr_cmp(addr
, brt
->brt_addr
);
2306 * bridge_rtnode_insert:
2308 * Insert the specified bridge node into the route table. We
2309 * assume the entry is not already in the table.
2312 bridge_rtnode_insert(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2314 struct bridge_rtnode
*lbrt
;
2318 hash
= bridge_rthash(sc
, brt
->brt_addr
);
2320 lbrt
= LIST_FIRST(&sc
->sc_rthash
[hash
]);
2322 LIST_INSERT_HEAD(&sc
->sc_rthash
[hash
], brt
, brt_hash
);
2327 dir
= bridge_rtnode_addr_cmp(brt
->brt_addr
, lbrt
->brt_addr
);
2331 LIST_INSERT_BEFORE(lbrt
, brt
, brt_hash
);
2334 if (LIST_NEXT(lbrt
, brt_hash
) == NULL
) {
2335 LIST_INSERT_AFTER(lbrt
, brt
, brt_hash
);
2338 lbrt
= LIST_NEXT(lbrt
, brt_hash
);
2339 } while (lbrt
!= NULL
);
2342 panic("bridge_rtnode_insert: impossible");
2346 LIST_INSERT_HEAD(&sc
->sc_rtlist
, brt
, brt_list
);
2353 * bridge_rtnode_destroy:
2355 * Destroy a bridge rtnode.
2358 bridge_rtnode_destroy(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2361 LIST_REMOVE(brt
, brt_hash
);
2363 LIST_REMOVE(brt
, brt_list
);
2365 kfree(brt
, M_DEVBUF
);
2369 * Send bridge packets through pfil if they are one of the types pfil can deal
2370 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2371 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2375 bridge_pfil(struct mbuf
**mp
, struct ifnet
*bifp
, struct ifnet
*ifp
, int dir
)
2377 int snap
, error
, i
, hlen
;
2378 struct ether_header
*eh1
, eh2
;
2381 u_int16_t ether_type
;
2384 error
= -1; /* Default error if not error == 0 */
2386 if (pfil_bridge
== 0 && pfil_member
== 0)
2387 return (0); /* filtering is disabled */
2389 i
= min((*mp
)->m_pkthdr
.len
, max_protohdr
);
2390 if ((*mp
)->m_len
< i
) {
2391 *mp
= m_pullup(*mp
, i
);
2393 kprintf("%s: m_pullup failed\n", __func__
);
2398 eh1
= mtod(*mp
, struct ether_header
*);
2399 ether_type
= ntohs(eh1
->ether_type
);
2402 * Check for SNAP/LLC.
2404 if (ether_type
< ETHERMTU
) {
2405 struct llc
*llc2
= (struct llc
*)(eh1
+ 1);
2407 if ((*mp
)->m_len
>= ETHER_HDR_LEN
+ 8 &&
2408 llc2
->llc_dsap
== LLC_SNAP_LSAP
&&
2409 llc2
->llc_ssap
== LLC_SNAP_LSAP
&&
2410 llc2
->llc_control
== LLC_UI
) {
2411 ether_type
= htons(llc2
->llc_un
.type_snap
.ether_type
);
2417 * If we're trying to filter bridge traffic, don't look at anything
2418 * other than IP and ARP traffic. If the filter doesn't understand
2419 * IPv6, don't allow IPv6 through the bridge either. This is lame
2420 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2421 * but of course we don't have an AppleTalk filter to begin with.
2422 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2425 switch (ether_type
) {
2427 case ETHERTYPE_REVARP
:
2428 return (0); /* Automatically pass */
2431 case ETHERTYPE_IPV6
:
2436 * Check to see if the user wants to pass non-ip
2437 * packets, these will not be checked by pfil(9) and
2438 * passed unconditionally so the default is to drop.
2444 /* Strip off the Ethernet header and keep a copy. */
2445 m_copydata(*mp
, 0, ETHER_HDR_LEN
, (caddr_t
) &eh2
);
2446 m_adj(*mp
, ETHER_HDR_LEN
);
2448 /* Strip off snap header, if present */
2450 m_copydata(*mp
, 0, sizeof(struct llc
), (caddr_t
) &llc1
);
2451 m_adj(*mp
, sizeof(struct llc
));
2455 * Check the IP header for alignment and errors
2457 if (dir
== PFIL_IN
) {
2458 switch (ether_type
) {
2460 error
= bridge_ip_checkbasic(mp
);
2463 case ETHERTYPE_IPV6
:
2464 error
= bridge_ip6_checkbasic(mp
);
2477 * Run the packet through pfil
2483 * before calling the firewall, swap fields the same as
2484 * IP does. here we assume the header is contiguous
2486 ip
= mtod(*mp
, struct ip
*);
2488 ip
->ip_len
= ntohs(ip
->ip_len
);
2489 ip
->ip_off
= ntohs(ip
->ip_off
);
2492 * Run pfil on the member interface and the bridge, both can
2493 * be skipped by clearing pfil_member or pfil_bridge.
2496 * in_if -> bridge_if -> out_if
2498 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
2499 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
2502 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2505 if (pfil_member
&& ifp
!= NULL
)
2506 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, ifp
,
2509 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2512 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
2513 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
2516 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2519 /* check if we need to fragment the packet */
2520 if (pfil_member
&& ifp
!= NULL
&& dir
== PFIL_OUT
) {
2521 i
= (*mp
)->m_pkthdr
.len
;
2522 if (i
> ifp
->if_mtu
) {
2523 error
= bridge_fragment(ifp
, *mp
, &eh2
, snap
,
2529 /* Recalculate the ip checksum and restore byte ordering */
2530 ip
= mtod(*mp
, struct ip
*);
2531 hlen
= ip
->ip_hl
<< 2;
2532 if (hlen
< sizeof(struct ip
))
2534 if (hlen
> (*mp
)->m_len
) {
2535 if ((*mp
= m_pullup(*mp
, hlen
)) == 0)
2537 ip
= mtod(*mp
, struct ip
*);
2541 ip
->ip_len
= htons(ip
->ip_len
);
2542 ip
->ip_off
= htons(ip
->ip_off
);
2544 if (hlen
== sizeof(struct ip
))
2545 ip
->ip_sum
= in_cksum_hdr(ip
);
2547 ip
->ip_sum
= in_cksum(*mp
, hlen
);
2551 case ETHERTYPE_IPV6
:
2552 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
2553 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
2556 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2559 if (pfil_member
&& ifp
!= NULL
)
2560 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, ifp
,
2563 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2566 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
2567 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
2584 * Finally, put everything back the way it was and return
2587 M_PREPEND(*mp
, sizeof(struct llc
), MB_DONTWAIT
);
2590 bcopy(&llc1
, mtod(*mp
, caddr_t
), sizeof(struct llc
));
2593 M_PREPEND(*mp
, ETHER_HDR_LEN
, MB_DONTWAIT
);
2596 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
2607 * Perform basic checks on header size since
2608 * pfil assumes ip_input has already processed
2609 * it for it. Cut-and-pasted from ip_input.c.
2610 * Given how simple the IPv6 version is,
2611 * does the IPv4 version really need to be
2614 * XXX Should we update ipstat here, or not?
2615 * XXX Right now we update ipstat but not
2619 bridge_ip_checkbasic(struct mbuf
**mp
)
2621 struct mbuf
*m
= *mp
;
2629 if (IP_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
2630 if ((m
= m_copyup(m
, sizeof(struct ip
),
2631 (max_linkhdr
+ 3) & ~3)) == NULL
) {
2632 /* XXXJRT new stat, please */
2633 ipstat
.ips_toosmall
++;
2638 #ifndef __predict_false
2639 #define __predict_false(x) x
2641 if (__predict_false(m
->m_len
< sizeof (struct ip
))) {
2642 if ((m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
2643 ipstat
.ips_toosmall
++;
2647 ip
= mtod(m
, struct ip
*);
2648 if (ip
== NULL
) goto bad
;
2650 if (ip
->ip_v
!= IPVERSION
) {
2651 ipstat
.ips_badvers
++;
2654 hlen
= ip
->ip_hl
<< 2;
2655 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
2656 ipstat
.ips_badhlen
++;
2659 if (hlen
> m
->m_len
) {
2660 if ((m
= m_pullup(m
, hlen
)) == 0) {
2661 ipstat
.ips_badhlen
++;
2664 ip
= mtod(m
, struct ip
*);
2665 if (ip
== NULL
) goto bad
;
2668 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
2669 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
2671 if (hlen
== sizeof(struct ip
)) {
2672 sum
= in_cksum_hdr(ip
);
2674 sum
= in_cksum(m
, hlen
);
2678 ipstat
.ips_badsum
++;
2682 /* Retrieve the packet length. */
2683 len
= ntohs(ip
->ip_len
);
2686 * Check for additional length bogosity
2689 ipstat
.ips_badlen
++;
2694 * Check that the amount of data in the buffers
2695 * is as at least much as the IP header would have us expect.
2696 * Drop packet if shorter than we expect.
2698 if (m
->m_pkthdr
.len
< len
) {
2699 ipstat
.ips_tooshort
++;
2703 /* Checks out, proceed */
2714 * Same as above, but for IPv6.
2715 * Cut-and-pasted from ip6_input.c.
2716 * XXX Should we update ip6stat, or not?
2719 bridge_ip6_checkbasic(struct mbuf
**mp
)
2721 struct mbuf
*m
= *mp
;
2722 struct ip6_hdr
*ip6
;
2725 * If the IPv6 header is not aligned, slurp it up into a new
2726 * mbuf with space for link headers, in the event we forward
2727 * it. Otherwise, if it is aligned, make sure the entire base
2728 * IPv6 header is in the first mbuf of the chain.
2731 if (IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
2732 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
2733 if ((m
= m_copyup(m
, sizeof(struct ip6_hdr
),
2734 (max_linkhdr
+ 3) & ~3)) == NULL
) {
2735 /* XXXJRT new stat, please */
2736 ip6stat
.ip6s_toosmall
++;
2737 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
2742 if (__predict_false(m
->m_len
< sizeof(struct ip6_hdr
))) {
2743 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
2744 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == NULL
) {
2745 ip6stat
.ip6s_toosmall
++;
2746 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
2751 ip6
= mtod(m
, struct ip6_hdr
*);
2753 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
2754 ip6stat
.ip6s_badvers
++;
2755 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
2759 /* Checks out, proceed */
2772 * Return a fragmented mbuf chain.
2775 bridge_fragment(struct ifnet
*ifp
, struct mbuf
*m
, struct ether_header
*eh
,
2776 int snap
, struct llc
*llc
)
2782 if (m
->m_len
< sizeof(struct ip
) &&
2783 (m
= m_pullup(m
, sizeof(struct ip
))) == NULL
)
2785 ip
= mtod(m
, struct ip
*);
2787 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
,
2792 /* walk the chain and re-add the Ethernet header */
2793 for (m0
= m
; m0
; m0
= m0
->m_nextpkt
) {
2796 M_PREPEND(m0
, sizeof(struct llc
), MB_DONTWAIT
);
2801 bcopy(llc
, mtod(m0
, caddr_t
),
2802 sizeof(struct llc
));
2804 M_PREPEND(m0
, ETHER_HDR_LEN
, MB_DONTWAIT
);
2809 bcopy(eh
, mtod(m0
, caddr_t
), ETHER_HDR_LEN
);
2815 ipstat
.ips_fragmented
++;
2826 bridge_enqueue_handler(struct netmsg
*nmsg
)
2828 struct netmsg_packet
*nmp
;
2829 struct ifnet
*dst_ifp
;
2832 nmp
= (struct netmsg_packet
*)nmsg
;
2834 dst_ifp
= nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
;
2836 bridge_handoff_notags(dst_ifp
, m
);
2840 bridge_pfil_enqueue_handler(struct netmsg
*nmsg
)
2842 struct netmsg_packet
*nmp
;
2843 struct ifnet
*dst_ifp
;
2846 nmp
= (struct netmsg_packet
*)nmsg
;
2848 dst_ifp
= nmp
->nm_netmsg
.nm_lmsg
.u
.ms_resultp
;
2851 * Filter on the output interface. Pass a NULL bridge interface
2852 * pointer so we do not redundantly filter on the bridge for
2853 * each interface we broadcast on.
2855 if (inet_pfil_hook
.ph_hashooks
> 0
2857 || inet6_pfil_hook
.ph_hashooks
> 0
2860 if (bridge_pfil(&m
, NULL
, dst_ifp
, PFIL_OUT
) != 0)
2865 bridge_handoff_notags(dst_ifp
, m
);
2869 bridge_handoff(struct ifnet
*dst_ifp
, struct mbuf
*m
)
2871 while (m
->m_type
== MT_TAG
) {
2872 /* XXX see ether_output_frame for full rules check */
2875 bridge_handoff_notags(dst_ifp
, m
);
2879 bridge_handoff_notags(struct ifnet
*dst_ifp
, struct mbuf
*m
)
2883 KKASSERT(m
->m_type
!= MT_TAG
);
2885 lwkt_serialize_enter(dst_ifp
->if_serializer
);
2887 /* We may be sending a fragment so traverse the mbuf */
2889 struct altq_pktattr pktattr
;
2892 m
->m_nextpkt
= NULL
;
2894 if (ifq_is_enabled(&dst_ifp
->if_snd
))
2895 altq_etherclassify(&dst_ifp
->if_snd
, m
, &pktattr
);
2897 ifq_handoff(dst_ifp
, m
, &pktattr
);
2900 lwkt_serialize_exit(dst_ifp
->if_serializer
);