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.18 2007/06/02 05:09:18 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>
128 #include <net/route.h>
129 #include <sys/in_cksum.h>
132 * Size of the route hash table. Must be a power of two.
134 #ifndef BRIDGE_RTHASH_SIZE
135 #define BRIDGE_RTHASH_SIZE 1024
138 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
141 * Maximum number of addresses to cache.
143 #ifndef BRIDGE_RTABLE_MAX
144 #define BRIDGE_RTABLE_MAX 100
148 * Spanning tree defaults.
150 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
151 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
152 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
153 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
154 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
155 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
156 #define BSTP_DEFAULT_PATH_COST 55
159 * Timeout (in seconds) for entries learned dynamically.
161 #ifndef BRIDGE_RTABLE_TIMEOUT
162 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
166 * Number of seconds between walks of the route list.
168 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
169 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
173 * List of capabilities to mask on the member interface.
175 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
177 eventhandler_tag bridge_detach_cookie
= NULL
;
179 extern struct mbuf
*(*bridge_input_p
)(struct ifnet
*, struct mbuf
*);
180 extern int (*bridge_output_p
)(struct ifnet
*, struct mbuf
*,
181 struct sockaddr
*, struct rtentry
*);
182 extern void (*bridge_dn_p
)(struct mbuf
*, struct ifnet
*);
184 static int bridge_rtable_prune_period
= BRIDGE_RTABLE_PRUNE_PERIOD
;
186 static int bridge_clone_create(struct if_clone
*, int);
187 static void bridge_clone_destroy(struct ifnet
*);
189 static int bridge_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
190 static void bridge_mutecaps(struct bridge_iflist
*, int);
191 static void bridge_ifdetach(void *arg __unused
, struct ifnet
*);
192 static void bridge_init(void *);
193 static void bridge_stop(struct ifnet
*, int);
194 static void bridge_start(struct ifnet
*);
195 static struct mbuf
*bridge_input(struct ifnet
*, struct mbuf
*);
196 static int bridge_output_serialized(struct ifnet
*, struct mbuf
*,
197 struct sockaddr
*, struct rtentry
*);
199 static void bridge_forward(struct bridge_softc
*, struct mbuf
*m
);
201 static void bridge_timer(void *);
203 static void bridge_broadcast(struct bridge_softc
*, struct ifnet
*,
205 static void bridge_span(struct bridge_softc
*, struct mbuf
*);
207 static int bridge_rtupdate(struct bridge_softc
*, const uint8_t *,
208 struct ifnet
*, int, uint8_t);
209 static struct ifnet
*bridge_rtlookup(struct bridge_softc
*, const uint8_t *);
210 static void bridge_rttrim(struct bridge_softc
*);
211 static void bridge_rtage(struct bridge_softc
*);
212 static void bridge_rtflush(struct bridge_softc
*, int);
213 static int bridge_rtdaddr(struct bridge_softc
*, const uint8_t *);
215 static int bridge_rtable_init(struct bridge_softc
*);
216 static void bridge_rtable_fini(struct bridge_softc
*);
218 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
219 static struct bridge_rtnode
*bridge_rtnode_lookup(struct bridge_softc
*,
221 static int bridge_rtnode_insert(struct bridge_softc
*,
222 struct bridge_rtnode
*);
223 static void bridge_rtnode_destroy(struct bridge_softc
*,
224 struct bridge_rtnode
*);
226 static struct bridge_iflist
*bridge_lookup_member(struct bridge_softc
*,
228 static struct bridge_iflist
*bridge_lookup_member_if(struct bridge_softc
*,
230 static void bridge_delete_member(struct bridge_softc
*,
231 struct bridge_iflist
*, int);
232 static void bridge_delete_span(struct bridge_softc
*,
233 struct bridge_iflist
*);
235 static int bridge_ioctl_add(struct bridge_softc
*, void *);
236 static int bridge_ioctl_del(struct bridge_softc
*, void *);
237 static int bridge_ioctl_gifflags(struct bridge_softc
*, void *);
238 static int bridge_ioctl_sifflags(struct bridge_softc
*, void *);
239 static int bridge_ioctl_scache(struct bridge_softc
*, void *);
240 static int bridge_ioctl_gcache(struct bridge_softc
*, void *);
241 static int bridge_ioctl_gifs(struct bridge_softc
*, void *);
242 static int bridge_ioctl_rts(struct bridge_softc
*, void *);
243 static int bridge_ioctl_saddr(struct bridge_softc
*, void *);
244 static int bridge_ioctl_sto(struct bridge_softc
*, void *);
245 static int bridge_ioctl_gto(struct bridge_softc
*, void *);
246 static int bridge_ioctl_daddr(struct bridge_softc
*, void *);
247 static int bridge_ioctl_flush(struct bridge_softc
*, void *);
248 static int bridge_ioctl_gpri(struct bridge_softc
*, void *);
249 static int bridge_ioctl_spri(struct bridge_softc
*, void *);
250 static int bridge_ioctl_ght(struct bridge_softc
*, void *);
251 static int bridge_ioctl_sht(struct bridge_softc
*, void *);
252 static int bridge_ioctl_gfd(struct bridge_softc
*, void *);
253 static int bridge_ioctl_sfd(struct bridge_softc
*, void *);
254 static int bridge_ioctl_gma(struct bridge_softc
*, void *);
255 static int bridge_ioctl_sma(struct bridge_softc
*, void *);
256 static int bridge_ioctl_sifprio(struct bridge_softc
*, void *);
257 static int bridge_ioctl_sifcost(struct bridge_softc
*, void *);
258 static int bridge_ioctl_addspan(struct bridge_softc
*, void *);
259 static int bridge_ioctl_delspan(struct bridge_softc
*, void *);
260 static int bridge_pfil(struct mbuf
**, struct ifnet
*, struct ifnet
*,
262 static int bridge_ip_checkbasic(struct mbuf
**mp
);
264 static int bridge_ip6_checkbasic(struct mbuf
**mp
);
266 static int bridge_fragment(struct ifnet
*, struct mbuf
*,
267 struct ether_header
*, int, struct llc
*);
269 SYSCTL_DECL(_net_link
);
270 SYSCTL_NODE(_net_link
, IFT_BRIDGE
, bridge
, CTLFLAG_RW
, 0, "Bridge");
272 static int pfil_onlyip
= 1; /* only pass IP[46] packets when pfil is enabled */
273 static int pfil_bridge
= 1; /* run pfil hooks on the bridge interface */
274 static int pfil_member
= 1; /* run pfil hooks on the member interface */
275 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_onlyip
, CTLFLAG_RW
,
276 &pfil_onlyip
, 0, "Only pass IP packets when pfil is enabled");
277 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_bridge
, CTLFLAG_RW
,
278 &pfil_bridge
, 0, "Packet filter on the bridge interface");
279 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_member
, CTLFLAG_RW
,
280 &pfil_member
, 0, "Packet filter on the member interface");
282 struct bridge_control
{
283 int (*bc_func
)(struct bridge_softc
*, void *);
288 #define BC_F_COPYIN 0x01 /* copy arguments in */
289 #define BC_F_COPYOUT 0x02 /* copy arguments out */
290 #define BC_F_SUSER 0x04 /* do super-user check */
292 const struct bridge_control bridge_control_table
[] = {
293 { bridge_ioctl_add
, sizeof(struct ifbreq
),
294 BC_F_COPYIN
|BC_F_SUSER
},
295 { bridge_ioctl_del
, sizeof(struct ifbreq
),
296 BC_F_COPYIN
|BC_F_SUSER
},
298 { bridge_ioctl_gifflags
, sizeof(struct ifbreq
),
299 BC_F_COPYIN
|BC_F_COPYOUT
},
300 { bridge_ioctl_sifflags
, sizeof(struct ifbreq
),
301 BC_F_COPYIN
|BC_F_SUSER
},
303 { bridge_ioctl_scache
, sizeof(struct ifbrparam
),
304 BC_F_COPYIN
|BC_F_SUSER
},
305 { bridge_ioctl_gcache
, sizeof(struct ifbrparam
),
308 { bridge_ioctl_gifs
, sizeof(struct ifbifconf
),
309 BC_F_COPYIN
|BC_F_COPYOUT
},
310 { bridge_ioctl_rts
, sizeof(struct ifbaconf
),
311 BC_F_COPYIN
|BC_F_COPYOUT
},
313 { bridge_ioctl_saddr
, sizeof(struct ifbareq
),
314 BC_F_COPYIN
|BC_F_SUSER
},
316 { bridge_ioctl_sto
, sizeof(struct ifbrparam
),
317 BC_F_COPYIN
|BC_F_SUSER
},
318 { bridge_ioctl_gto
, sizeof(struct ifbrparam
),
321 { bridge_ioctl_daddr
, sizeof(struct ifbareq
),
322 BC_F_COPYIN
|BC_F_SUSER
},
324 { bridge_ioctl_flush
, sizeof(struct ifbreq
),
325 BC_F_COPYIN
|BC_F_SUSER
},
327 { bridge_ioctl_gpri
, sizeof(struct ifbrparam
),
329 { bridge_ioctl_spri
, sizeof(struct ifbrparam
),
330 BC_F_COPYIN
|BC_F_SUSER
},
332 { bridge_ioctl_ght
, sizeof(struct ifbrparam
),
334 { bridge_ioctl_sht
, sizeof(struct ifbrparam
),
335 BC_F_COPYIN
|BC_F_SUSER
},
337 { bridge_ioctl_gfd
, sizeof(struct ifbrparam
),
339 { bridge_ioctl_sfd
, sizeof(struct ifbrparam
),
340 BC_F_COPYIN
|BC_F_SUSER
},
342 { bridge_ioctl_gma
, sizeof(struct ifbrparam
),
344 { bridge_ioctl_sma
, sizeof(struct ifbrparam
),
345 BC_F_COPYIN
|BC_F_SUSER
},
347 { bridge_ioctl_sifprio
, sizeof(struct ifbreq
),
348 BC_F_COPYIN
|BC_F_SUSER
},
350 { bridge_ioctl_sifcost
, sizeof(struct ifbreq
),
351 BC_F_COPYIN
|BC_F_SUSER
},
353 { bridge_ioctl_addspan
, sizeof(struct ifbreq
),
354 BC_F_COPYIN
|BC_F_SUSER
},
355 { bridge_ioctl_delspan
, sizeof(struct ifbreq
),
356 BC_F_COPYIN
|BC_F_SUSER
},
358 const int bridge_control_table_size
=
359 sizeof(bridge_control_table
) / sizeof(bridge_control_table
[0]);
361 LIST_HEAD(, bridge_softc
) bridge_list
;
363 struct if_clone bridge_cloner
= IF_CLONE_INITIALIZER("bridge",
365 bridge_clone_destroy
, 0, IF_MAXUNIT
);
368 bridge_modevent(module_t mod
, int type
, void *data
)
373 LIST_INIT(&bridge_list
);
374 if_clone_attach(&bridge_cloner
);
375 bridge_input_p
= bridge_input
;
376 bridge_output_p
= bridge_output_serialized
;
377 bridge_detach_cookie
= EVENTHANDLER_REGISTER(
378 ifnet_detach_event
, bridge_ifdetach
, NULL
,
379 EVENTHANDLER_PRI_ANY
);
381 bstp_linkstate_p
= bstp_linkstate
;
385 if (!LIST_EMPTY(&bridge_list
))
387 EVENTHANDLER_DEREGISTER(ifnet_detach_event
,
388 bridge_detach_cookie
);
389 if_clone_detach(&bridge_cloner
);
390 bridge_input_p
= NULL
;
391 bridge_output_p
= NULL
;
393 bstp_linkstate_p
= NULL
;
402 static moduledata_t bridge_mod
= {
408 DECLARE_MODULE(if_bridge
, bridge_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);
412 * bridge_clone_create:
414 * Create a new bridge instance.
417 bridge_clone_create(struct if_clone
*ifc
, int unit
)
419 struct bridge_softc
*sc
;
423 sc
= kmalloc(sizeof(*sc
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
424 ifp
= sc
->sc_ifp
= &sc
->sc_if
;
426 sc
->sc_brtmax
= BRIDGE_RTABLE_MAX
;
427 sc
->sc_brttimeout
= BRIDGE_RTABLE_TIMEOUT
;
428 sc
->sc_bridge_max_age
= BSTP_DEFAULT_MAX_AGE
;
429 sc
->sc_bridge_hello_time
= BSTP_DEFAULT_HELLO_TIME
;
430 sc
->sc_bridge_forward_delay
= BSTP_DEFAULT_FORWARD_DELAY
;
431 sc
->sc_bridge_priority
= BSTP_DEFAULT_BRIDGE_PRIORITY
;
432 sc
->sc_hold_time
= BSTP_DEFAULT_HOLD_TIME
;
434 /* Initialize our routing table. */
435 bridge_rtable_init(sc
);
437 callout_init(&sc
->sc_brcallout
);
438 callout_init(&sc
->sc_bstpcallout
);
440 LIST_INIT(&sc
->sc_iflist
);
441 LIST_INIT(&sc
->sc_spanlist
);
444 if_initname(ifp
, ifc
->ifc_name
, unit
);
445 ifp
->if_mtu
= ETHERMTU
;
446 ifp
->if_flags
= IFF_BROADCAST
| IFF_MULTICAST
;
447 ifp
->if_ioctl
= bridge_ioctl
;
448 ifp
->if_start
= bridge_start
;
449 ifp
->if_init
= bridge_init
;
450 ifp
->if_type
= IFT_BRIDGE
;
451 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
452 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
453 ifq_set_ready(&ifp
->if_snd
);
454 ifp
->if_hdrlen
= ETHER_HDR_LEN
;
457 * Generate a random ethernet address and use the private AC:DE:48
461 int rnd
= karc4random();
462 bcopy(&rnd
, &eaddr
[0], 4); /* ETHER_ADDR_LEN == 6 */
464 bcopy(&rnd
, &eaddr
[2], 4); /* ETHER_ADDR_LEN == 6 */
466 eaddr
[0] &= ~1; /* clear multicast bit */
467 eaddr
[0] |= 2; /* set the LAA bit */
469 ether_ifattach(ifp
, eaddr
, NULL
);
470 /* Now undo some of the damage... */
471 ifp
->if_baudrate
= 0;
472 ifp
->if_type
= IFT_BRIDGE
;
475 LIST_INSERT_HEAD(&bridge_list
, sc
, sc_list
);
482 * bridge_clone_destroy:
484 * Destroy a bridge instance.
487 bridge_clone_destroy(struct ifnet
*ifp
)
489 struct bridge_softc
*sc
= ifp
->if_softc
;
490 struct bridge_iflist
*bif
;
492 lwkt_serialize_enter(ifp
->if_serializer
);
495 ifp
->if_flags
&= ~IFF_UP
;
497 while ((bif
= LIST_FIRST(&sc
->sc_iflist
)) != NULL
)
498 bridge_delete_member(sc
, bif
, 0);
500 while ((bif
= LIST_FIRST(&sc
->sc_spanlist
)) != NULL
) {
501 bridge_delete_span(sc
, bif
);
504 callout_stop(&sc
->sc_brcallout
);
505 callout_stop(&sc
->sc_bstpcallout
);
507 lwkt_serialize_exit(ifp
->if_serializer
);
510 LIST_REMOVE(sc
, sc_list
);
515 /* Tear down the routing table. */
516 bridge_rtable_fini(sc
);
524 * Handle a control request from the operator.
527 bridge_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
529 struct bridge_softc
*sc
= ifp
->if_softc
;
530 struct thread
*td
= curthread
;
532 struct ifbreq ifbreq
;
533 struct ifbifconf ifbifconf
;
534 struct ifbareq ifbareq
;
535 struct ifbaconf ifbaconf
;
536 struct ifbrparam ifbrparam
;
538 struct ifdrv
*ifd
= (struct ifdrv
*) data
;
539 const struct bridge_control
*bc
;
550 if (ifd
->ifd_cmd
>= bridge_control_table_size
) {
554 bc
= &bridge_control_table
[ifd
->ifd_cmd
];
556 if (cmd
== SIOCGDRVSPEC
&&
557 (bc
->bc_flags
& BC_F_COPYOUT
) == 0) {
561 else if (cmd
== SIOCSDRVSPEC
&&
562 (bc
->bc_flags
& BC_F_COPYOUT
) != 0) {
567 if (bc
->bc_flags
& BC_F_SUSER
) {
573 if (ifd
->ifd_len
!= bc
->bc_argsize
||
574 ifd
->ifd_len
> sizeof(args
)) {
579 memset(&args
, 0, sizeof(args
));
580 if (bc
->bc_flags
& BC_F_COPYIN
) {
581 error
= copyin(ifd
->ifd_data
, &args
, ifd
->ifd_len
);
586 error
= (*bc
->bc_func
)(sc
, &args
);
590 if (bc
->bc_flags
& BC_F_COPYOUT
)
591 error
= copyout(&args
, ifd
->ifd_data
, ifd
->ifd_len
);
596 if (!(ifp
->if_flags
& IFF_UP
) &&
597 (ifp
->if_flags
& IFF_RUNNING
)) {
599 * If interface is marked down and it is running,
600 * then stop and disable it.
603 } else if ((ifp
->if_flags
& IFF_UP
) &&
604 !(ifp
->if_flags
& IFF_RUNNING
)) {
606 * If interface is marked up and it is stopped, then
614 /* Do not allow the MTU to be changed on the bridge */
620 * drop the lock as ether_ioctl() will call bridge_start() and
621 * cause the lock to be recursed.
623 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
);
800 bif
->bif_flags
= IFBIF_LEARNING
| IFBIF_DISCOVER
;
801 bif
->bif_priority
= BSTP_DEFAULT_PORT_PRIORITY
;
802 bif
->bif_path_cost
= BSTP_DEFAULT_PATH_COST
;
804 switch (ifs
->if_type
) {
808 * Place the interface into promiscuous mode.
810 error
= ifpromisc(ifs
, 1);
814 bridge_mutecaps(bif
, 1);
817 case IFT_GIF
: /* :^) */
827 LIST_INSERT_HEAD(&sc
->sc_iflist
, bif
, bif_next
);
829 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
830 bstp_initialization(sc
);
837 kfree(bif
, M_DEVBUF
);
843 bridge_ioctl_del(struct bridge_softc
*sc
, void *arg
)
845 struct ifbreq
*req
= arg
;
846 struct bridge_iflist
*bif
;
848 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
852 bridge_delete_member(sc
, bif
, 0);
858 bridge_ioctl_gifflags(struct bridge_softc
*sc
, void *arg
)
860 struct ifbreq
*req
= arg
;
861 struct bridge_iflist
*bif
;
863 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
867 req
->ifbr_ifsflags
= bif
->bif_flags
;
868 req
->ifbr_state
= bif
->bif_state
;
869 req
->ifbr_priority
= bif
->bif_priority
;
870 req
->ifbr_path_cost
= bif
->bif_path_cost
;
871 req
->ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
877 bridge_ioctl_sifflags(struct bridge_softc
*sc
, void *arg
)
879 struct ifbreq
*req
= arg
;
880 struct bridge_iflist
*bif
;
882 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
886 if (req
->ifbr_ifsflags
& IFBIF_SPAN
)
887 /* SPAN is readonly */
890 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
891 switch (bif
->bif_ifp
->if_type
) {
893 /* These can do spanning tree. */
897 /* Nothing else can. */
902 bif
->bif_flags
= req
->ifbr_ifsflags
;
904 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
905 bstp_initialization(sc
);
911 bridge_ioctl_scache(struct bridge_softc
*sc
, void *arg
)
913 struct ifbrparam
*param
= arg
;
915 sc
->sc_brtmax
= param
->ifbrp_csize
;
922 bridge_ioctl_gcache(struct bridge_softc
*sc
, void *arg
)
924 struct ifbrparam
*param
= arg
;
926 param
->ifbrp_csize
= sc
->sc_brtmax
;
932 bridge_ioctl_gifs(struct bridge_softc
*sc
, void *arg
)
934 struct ifbifconf
*bifc
= arg
;
935 struct bridge_iflist
*bif
;
937 int count
, len
, error
= 0;
940 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
)
942 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
945 if (bifc
->ifbic_len
== 0) {
946 bifc
->ifbic_len
= sizeof(breq
) * count
;
951 len
= bifc
->ifbic_len
;
952 memset(&breq
, 0, sizeof breq
);
953 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
954 if (len
< sizeof(breq
))
957 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
958 sizeof(breq
.ifbr_ifsname
));
959 breq
.ifbr_ifsflags
= bif
->bif_flags
;
960 breq
.ifbr_state
= bif
->bif_state
;
961 breq
.ifbr_priority
= bif
->bif_priority
;
962 breq
.ifbr_path_cost
= bif
->bif_path_cost
;
963 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
964 error
= copyout(&breq
, bifc
->ifbic_req
+ count
, sizeof(breq
));
970 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
971 if (len
< sizeof(breq
))
974 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
975 sizeof(breq
.ifbr_ifsname
));
976 breq
.ifbr_ifsflags
= bif
->bif_flags
;
977 breq
.ifbr_state
= bif
->bif_state
;
978 breq
.ifbr_priority
= bif
->bif_priority
;
979 breq
.ifbr_path_cost
= bif
->bif_path_cost
;
980 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xff;
981 error
= copyout(&breq
, bifc
->ifbic_req
+ count
, sizeof(breq
));
988 bifc
->ifbic_len
= sizeof(breq
) * count
;
993 bridge_ioctl_rts(struct bridge_softc
*sc
, void *arg
)
995 struct ifbaconf
*bac
= arg
;
996 struct bridge_rtnode
*brt
;
997 struct ifbareq bareq
;
998 int count
= 0, error
= 0, len
;
1000 if (bac
->ifbac_len
== 0)
1003 len
= bac
->ifbac_len
;
1004 memset(&bareq
, 0, sizeof(bareq
));
1005 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
1006 if (len
< sizeof(bareq
))
1008 strlcpy(bareq
.ifba_ifsname
, brt
->brt_ifp
->if_xname
,
1009 sizeof(bareq
.ifba_ifsname
));
1010 memcpy(bareq
.ifba_dst
, brt
->brt_addr
, sizeof(brt
->brt_addr
));
1011 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
&&
1012 time_second
< brt
->brt_expire
)
1013 bareq
.ifba_expire
= brt
->brt_expire
- time_second
;
1015 bareq
.ifba_expire
= 0;
1016 bareq
.ifba_flags
= brt
->brt_flags
;
1018 error
= copyout(&bareq
, bac
->ifbac_req
+ count
, sizeof(bareq
));
1022 len
-= sizeof(bareq
);
1025 bac
->ifbac_len
= sizeof(bareq
) * count
;
1030 bridge_ioctl_saddr(struct bridge_softc
*sc
, void *arg
)
1032 struct ifbareq
*req
= arg
;
1033 struct bridge_iflist
*bif
;
1036 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
1040 error
= bridge_rtupdate(sc
, req
->ifba_dst
, bif
->bif_ifp
, 1,
1047 bridge_ioctl_sto(struct bridge_softc
*sc
, void *arg
)
1049 struct ifbrparam
*param
= arg
;
1051 sc
->sc_brttimeout
= param
->ifbrp_ctime
;
1057 bridge_ioctl_gto(struct bridge_softc
*sc
, void *arg
)
1059 struct ifbrparam
*param
= arg
;
1061 param
->ifbrp_ctime
= sc
->sc_brttimeout
;
1067 bridge_ioctl_daddr(struct bridge_softc
*sc
, void *arg
)
1069 struct ifbareq
*req
= arg
;
1071 return (bridge_rtdaddr(sc
, req
->ifba_dst
));
1075 bridge_ioctl_flush(struct bridge_softc
*sc
, void *arg
)
1077 struct ifbreq
*req
= arg
;
1079 bridge_rtflush(sc
, req
->ifbr_ifsflags
);
1085 bridge_ioctl_gpri(struct bridge_softc
*sc
, void *arg
)
1087 struct ifbrparam
*param
= arg
;
1089 param
->ifbrp_prio
= sc
->sc_bridge_priority
;
1095 bridge_ioctl_spri(struct bridge_softc
*sc
, void *arg
)
1097 struct ifbrparam
*param
= arg
;
1099 sc
->sc_bridge_priority
= param
->ifbrp_prio
;
1101 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1102 bstp_initialization(sc
);
1108 bridge_ioctl_ght(struct bridge_softc
*sc
, void *arg
)
1110 struct ifbrparam
*param
= arg
;
1112 param
->ifbrp_hellotime
= sc
->sc_bridge_hello_time
>> 8;
1118 bridge_ioctl_sht(struct bridge_softc
*sc
, void *arg
)
1120 struct ifbrparam
*param
= arg
;
1122 if (param
->ifbrp_hellotime
== 0)
1124 sc
->sc_bridge_hello_time
= param
->ifbrp_hellotime
<< 8;
1126 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1127 bstp_initialization(sc
);
1133 bridge_ioctl_gfd(struct bridge_softc
*sc
, void *arg
)
1135 struct ifbrparam
*param
= arg
;
1137 param
->ifbrp_fwddelay
= sc
->sc_bridge_forward_delay
>> 8;
1143 bridge_ioctl_sfd(struct bridge_softc
*sc
, void *arg
)
1145 struct ifbrparam
*param
= arg
;
1147 if (param
->ifbrp_fwddelay
== 0)
1149 sc
->sc_bridge_forward_delay
= param
->ifbrp_fwddelay
<< 8;
1151 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1152 bstp_initialization(sc
);
1158 bridge_ioctl_gma(struct bridge_softc
*sc
, void *arg
)
1160 struct ifbrparam
*param
= arg
;
1162 param
->ifbrp_maxage
= sc
->sc_bridge_max_age
>> 8;
1168 bridge_ioctl_sma(struct bridge_softc
*sc
, void *arg
)
1170 struct ifbrparam
*param
= arg
;
1172 if (param
->ifbrp_maxage
== 0)
1174 sc
->sc_bridge_max_age
= param
->ifbrp_maxage
<< 8;
1176 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1177 bstp_initialization(sc
);
1183 bridge_ioctl_sifprio(struct bridge_softc
*sc
, void *arg
)
1185 struct ifbreq
*req
= arg
;
1186 struct bridge_iflist
*bif
;
1188 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1192 bif
->bif_priority
= req
->ifbr_priority
;
1194 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1195 bstp_initialization(sc
);
1201 bridge_ioctl_sifcost(struct bridge_softc
*sc
, void *arg
)
1203 struct ifbreq
*req
= arg
;
1204 struct bridge_iflist
*bif
;
1206 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1210 bif
->bif_path_cost
= req
->ifbr_path_cost
;
1212 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
1213 bstp_initialization(sc
);
1219 bridge_ioctl_addspan(struct bridge_softc
*sc
, void *arg
)
1221 struct ifbreq
*req
= arg
;
1222 struct bridge_iflist
*bif
= NULL
;
1225 ifs
= ifunit(req
->ifbr_ifsname
);
1229 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1230 if (ifs
== bif
->bif_ifp
)
1233 if (ifs
->if_bridge
!= NULL
)
1236 switch (ifs
->if_type
) {
1245 bif
= kmalloc(sizeof(*bif
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
1250 bif
->bif_flags
= IFBIF_SPAN
;
1252 LIST_INSERT_HEAD(&sc
->sc_spanlist
, bif
, bif_next
);
1258 bridge_ioctl_delspan(struct bridge_softc
*sc
, void *arg
)
1260 struct ifbreq
*req
= arg
;
1261 struct bridge_iflist
*bif
;
1264 ifs
= ifunit(req
->ifbr_ifsname
);
1268 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1269 if (ifs
== bif
->bif_ifp
)
1275 bridge_delete_span(sc
, bif
);
1283 * Detach an interface from a bridge. Called when a member
1284 * interface is detaching.
1287 bridge_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
1289 struct bridge_softc
*sc
= ifp
->if_bridge
;
1290 struct bridge_iflist
*bif
;
1292 /* Check if the interface is a bridge member */
1294 lwkt_serialize_enter(ifp
->if_serializer
);
1296 bif
= bridge_lookup_member_if(sc
, ifp
);
1298 bridge_delete_member(sc
, bif
, 1);
1300 lwkt_serialize_exit(ifp
->if_serializer
);
1304 /* Check if the interface is a span port */
1305 LIST_FOREACH(sc
, &bridge_list
, sc_list
) {
1306 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1307 if (ifp
== bif
->bif_ifp
) {
1308 bridge_delete_span(sc
, bif
);
1317 * Initialize a bridge interface.
1320 bridge_init(void *xsc
)
1322 struct bridge_softc
*sc
= (struct bridge_softc
*)xsc
;
1323 struct ifnet
*ifp
= sc
->sc_ifp
;
1325 if (ifp
->if_flags
& IFF_RUNNING
)
1328 callout_reset(&sc
->sc_brcallout
, bridge_rtable_prune_period
* hz
,
1331 ifp
->if_flags
|= IFF_RUNNING
;
1332 bstp_initialization(sc
);
1339 * Stop the bridge interface.
1342 bridge_stop(struct ifnet
*ifp
, int disable
)
1344 struct bridge_softc
*sc
= ifp
->if_softc
;
1346 ASSERT_SERIALIZED(ifp
->if_serializer
);
1348 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
1351 callout_stop(&sc
->sc_brcallout
);
1354 bridge_rtflush(sc
, IFBF_FLUSHDYN
);
1356 ifp
->if_flags
&= ~IFF_RUNNING
;
1362 * Enqueue a packet on a bridge member interface.
1366 bridge_enqueue(struct bridge_softc
*sc
, struct ifnet
*dst_ifp
, struct mbuf
*m
)
1368 struct altq_pktattr pktattr
;
1371 while (m
->m_type
== MT_TAG
) {
1372 /* XXX see ether_output_frame for full rules check */
1376 lwkt_serialize_enter(dst_ifp
->if_serializer
);
1378 /* We may be sending a fragment so traverse the mbuf */
1381 m
->m_nextpkt
= NULL
;
1383 if (ifq_is_enabled(&dst_ifp
->if_snd
))
1384 altq_etherclassify(&dst_ifp
->if_snd
, m
, &pktattr
);
1386 ifq_handoff(dst_ifp
, m
, &pktattr
);
1389 lwkt_serialize_exit(dst_ifp
->if_serializer
);
1393 * bridge_output_serialized:
1395 * Send output from a bridge member interface. This
1396 * performs the bridging function for locally originated
1399 * The mbuf has the Ethernet header already attached. We must
1400 * enqueue or free the mbuf before returning.
1403 bridge_output_serialized(struct ifnet
*ifp
, struct mbuf
*m
,
1404 struct sockaddr
*sa
, struct rtentry
*rt
)
1406 struct ether_header
*eh
;
1407 struct ifnet
*dst_if
;
1408 struct bridge_softc
*sc
;
1410 sc
= ifp
->if_bridge
;
1412 ASSERT_SERIALIZED(ifp
->if_serializer
);
1414 if (m
->m_len
< ETHER_HDR_LEN
) {
1415 m
= m_pullup(m
, ETHER_HDR_LEN
);
1421 * Serialize our bridge interface. We have to get rid of the
1422 * originating interface lock to avoid a deadlock.
1424 lwkt_serialize_exit(ifp
->if_serializer
);
1425 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
1427 eh
= mtod(m
, struct ether_header
*);
1430 * If bridge is down, but the original output interface is up,
1431 * go ahead and send out that interface. Otherwise, the packet
1434 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) == 0) {
1440 * If the packet is a multicast, or we don't know a better way to
1441 * get there, send to all interfaces.
1443 if (ETHER_IS_MULTICAST(eh
->ether_dhost
))
1446 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1447 if (dst_if
== NULL
) {
1448 struct bridge_iflist
*bif
;
1454 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1455 dst_if
= bif
->bif_ifp
;
1456 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1460 * If this is not the original output interface,
1461 * and the interface is participating in spanning
1462 * tree, make sure the port is in a state that
1463 * allows forwarding.
1465 if (dst_if
!= ifp
&&
1466 (bif
->bif_flags
& IFBIF_STP
) != 0) {
1467 switch (bif
->bif_state
) {
1468 case BSTP_IFSTATE_BLOCKING
:
1469 case BSTP_IFSTATE_LISTENING
:
1470 case BSTP_IFSTATE_DISABLED
:
1475 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
1479 mc
= m_copypacket(m
, MB_DONTWAIT
);
1481 sc
->sc_ifp
->if_oerrors
++;
1485 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1486 bridge_enqueue(sc
, dst_if
, mc
);
1487 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
1491 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1497 * XXX Spanning tree consideration here?
1501 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
1502 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
1505 bridge_enqueue(sc
, dst_if
, m
);
1508 lwkt_serialize_enter(ifp
->if_serializer
);
1515 * Start output on a bridge.
1519 bridge_start(struct ifnet
*ifp
)
1521 struct bridge_softc
*sc
;
1523 struct ether_header
*eh
;
1524 struct ifnet
*dst_if
;
1528 ifp
->if_flags
|= IFF_OACTIVE
;
1530 m
= ifq_dequeue(&ifp
->if_snd
, NULL
);
1536 eh
= mtod(m
, struct ether_header
*);
1539 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) {
1540 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1544 bridge_broadcast(sc
, ifp
, m
, 0);
1546 bridge_enqueue(sc
, dst_if
, m
);
1548 ifp
->if_flags
&= ~IFF_OACTIVE
;
1556 * The forwarding function of the bridge.
1559 bridge_forward(struct bridge_softc
*sc
, struct mbuf
*m
)
1561 struct bridge_iflist
*bif
;
1562 struct ifnet
*src_if
, *dst_if
, *ifp
;
1563 struct ether_header
*eh
;
1565 src_if
= m
->m_pkthdr
.rcvif
;
1568 ASSERT_SERIALIZED(ifp
->if_serializer
);
1570 sc
->sc_ifp
->if_ipackets
++;
1571 sc
->sc_ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1574 * Look up the bridge_iflist.
1576 bif
= bridge_lookup_member_if(sc
, src_if
);
1578 /* Interface is not a bridge member (anymore?) */
1583 if (bif
->bif_flags
& IFBIF_STP
) {
1584 switch (bif
->bif_state
) {
1585 case BSTP_IFSTATE_BLOCKING
:
1586 case BSTP_IFSTATE_LISTENING
:
1587 case BSTP_IFSTATE_DISABLED
:
1593 eh
= mtod(m
, struct ether_header
*);
1596 * Various ifp's are used below, release the serializer for
1597 * the bridge ifp so other ifp serializers can be acquired.
1599 lwkt_serialize_exit(ifp
->if_serializer
);
1602 * If the interface is learning, and the source
1603 * address is valid and not multicast, record
1606 if ((bif
->bif_flags
& IFBIF_LEARNING
) != 0 &&
1607 ETHER_IS_MULTICAST(eh
->ether_shost
) == 0 &&
1608 (eh
->ether_shost
[0] == 0 &&
1609 eh
->ether_shost
[1] == 0 &&
1610 eh
->ether_shost
[2] == 0 &&
1611 eh
->ether_shost
[3] == 0 &&
1612 eh
->ether_shost
[4] == 0 &&
1613 eh
->ether_shost
[5] == 0) == 0) {
1614 bridge_rtupdate(sc
, eh
->ether_shost
, src_if
, 0, IFBAF_DYNAMIC
);
1617 if ((bif
->bif_flags
& IFBIF_STP
) != 0 &&
1618 bif
->bif_state
== BSTP_IFSTATE_LEARNING
) {
1624 * At this point, the port either doesn't participate
1625 * in spanning tree or it is in the forwarding state.
1629 * If the packet is unicast, destined for someone on
1630 * "this" side of the bridge, drop it.
1632 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) {
1633 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
1634 if (src_if
== dst_if
) {
1639 /* ...forward it to all interfaces. */
1640 sc
->sc_ifp
->if_imcasts
++;
1644 /* run the packet filter */
1645 if (inet_pfil_hook
.ph_hashooks
> 0
1647 || inet6_pfil_hook
.ph_hashooks
> 0
1650 if (bridge_pfil(&m
, ifp
, src_if
, PFIL_IN
) != 0)
1656 if (dst_if
== NULL
) {
1657 bridge_broadcast(sc
, src_if
, m
, 1);
1662 * At this point, we're dealing with a unicast frame
1663 * going to a different interface.
1665 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
1669 bif
= bridge_lookup_member_if(sc
, dst_if
);
1671 /* Not a member of the bridge (anymore?) */
1676 if (bif
->bif_flags
& IFBIF_STP
) {
1677 switch (bif
->bif_state
) {
1678 case BSTP_IFSTATE_DISABLED
:
1679 case BSTP_IFSTATE_BLOCKING
:
1685 if (inet_pfil_hook
.ph_hashooks
> 0
1687 || inet6_pfil_hook
.ph_hashooks
> 0
1690 if (bridge_pfil(&m
, sc
->sc_ifp
, dst_if
, PFIL_OUT
) != 0)
1695 bridge_enqueue(sc
, dst_if
, m
);
1698 * ifp's serializer was held on entry and is expected to be held
1702 lwkt_serialize_enter(ifp
->if_serializer
);
1708 * Receive input from a member interface. Queue the packet for
1709 * bridging if it is not for us.
1712 bridge_input(struct ifnet
*ifp
, struct mbuf
*m
)
1714 struct bridge_softc
*sc
= ifp
->if_bridge
;
1715 struct bridge_iflist
*bif
;
1717 struct ether_header
*eh
;
1718 struct mbuf
*mc
, *mc2
;
1721 lwkt_serialize_enter(bifp
->if_serializer
);
1723 if ((bifp
->if_flags
& IFF_RUNNING
) == 0)
1727 * Implement support for bridge monitoring. If this flag has been
1728 * set on this interface, discard the packet once we push it through
1729 * the bpf(4) machinery, but before we do, increment the byte and
1730 * packet counters associated with this interface.
1732 if ((bifp
->if_flags
& IFF_MONITOR
) != 0) {
1733 m
->m_pkthdr
.rcvif
= bifp
;
1735 bifp
->if_ipackets
++;
1736 bifp
->if_ibytes
+= m
->m_pkthdr
.len
;
1742 bif
= bridge_lookup_member_if(sc
, ifp
);
1746 eh
= mtod(m
, struct ether_header
*);
1748 m
->m_flags
&= ~M_PROTO1
; /* XXX Hack - loop prevention */
1751 * Tap all packets arriving on the bridge, no matter if
1752 * they are local destinations or not. In is in.
1756 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
1757 #define IFP2ENADDR(ifp) (IFP2AC(ifp)->ac_enaddr)
1758 if (memcmp(eh
->ether_dhost
, IFP2ENADDR(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 ether_input for
1765 /* Mark the packet as arriving on the bridge interface */
1766 m
->m_pkthdr
.rcvif
= bifp
;
1767 bifp
->if_ipackets
++;
1774 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
1775 /* Tap off 802.1D packets; they do not get forwarded. */
1776 if (memcmp(eh
->ether_dhost
, bstp_etheraddr
,
1777 ETHER_ADDR_LEN
) == 0) {
1778 m
= bstp_input(ifp
, m
);
1783 if (bif
->bif_flags
& IFBIF_STP
) {
1784 switch (bif
->bif_state
) {
1785 case BSTP_IFSTATE_BLOCKING
:
1786 case BSTP_IFSTATE_LISTENING
:
1787 case BSTP_IFSTATE_DISABLED
:
1792 if (bcmp(etherbroadcastaddr
, eh
->ether_dhost
,
1793 sizeof(etherbroadcastaddr
)) == 0)
1794 m
->m_flags
|= M_BCAST
;
1796 m
->m_flags
|= M_MCAST
;
1799 * Make a deep copy of the packet and enqueue the copy
1800 * for bridge processing; return the original packet for
1803 mc
= m_dup(m
, MB_DONTWAIT
);
1807 bridge_forward(sc
, mc
);
1810 * Reinject the mbuf as arriving on the bridge so we have a
1811 * chance at claiming multicast packets. We can not loop back
1812 * here from ether_input as a bridge is never a member of a
1815 KASSERT(bifp
->if_bridge
== NULL
,
1816 ("loop created in bridge_input"));
1817 mc2
= m_dup(m
, MB_DONTWAIT
);
1820 /* Keep the layer3 header aligned */
1821 int i
= min(mc2
->m_pkthdr
.len
, max_protohdr
);
1822 mc2
= m_copyup(mc2
, i
, ETHER_ALIGN
);
1826 mc2
->m_pkthdr
.rcvif
= bifp
;
1827 (*bifp
->if_input
)(bifp
, mc2
);
1830 /* Return the original packet for local processing. */
1834 if (bif
->bif_flags
& IFBIF_STP
) {
1835 switch (bif
->bif_state
) {
1836 case BSTP_IFSTATE_BLOCKING
:
1837 case BSTP_IFSTATE_LISTENING
:
1838 case BSTP_IFSTATE_DISABLED
:
1844 * Unicast. Make sure it's not for us.
1846 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1847 if (bif
->bif_ifp
->if_type
!= IFT_ETHER
)
1849 /* It is destined for us. */
1850 if (memcmp(IF_LLADDR(bif
->bif_ifp
), eh
->ether_dhost
,
1851 ETHER_ADDR_LEN
) == 0) {
1852 if (bif
->bif_flags
& IFBIF_LEARNING
)
1854 eh
->ether_shost
, ifp
, 0, IFBAF_DYNAMIC
);
1855 m
->m_pkthdr
.rcvif
= bif
->bif_ifp
;
1856 if (ifp
->if_type
== IFT_GIF
) {
1857 m
->m_flags
|= M_PROTO1
;
1859 * Avoid an interface ordering deadlock.
1861 lwkt_serialize_exit(bifp
->if_serializer
);
1862 lwkt_serialize_enter(bif
->bif_ifp
->if_serializer
);
1863 (*bif
->bif_ifp
->if_input
)(bif
->bif_ifp
, m
);
1864 lwkt_serialize_exit(bif
->bif_ifp
->if_serializer
);
1865 lwkt_serialize_enter(bifp
->if_serializer
);
1871 /* We just received a packet that we sent out. */
1872 if (memcmp(IF_LLADDR(bif
->bif_ifp
), eh
->ether_shost
,
1873 ETHER_ADDR_LEN
) == 0) {
1880 /* Perform the bridge forwarding function. */
1881 bridge_forward(sc
, m
);
1885 lwkt_serialize_exit(bifp
->if_serializer
);
1892 * Send a frame to all interfaces that are members of
1893 * the bridge, except for the one on which the packet
1897 bridge_broadcast(struct bridge_softc
*sc
, struct ifnet
*src_if
,
1898 struct mbuf
*m
, int runfilt
)
1900 struct bridge_iflist
*bif
;
1902 struct ifnet
*dst_if
;
1905 /* Filter on the bridge interface before broadcasting */
1906 if (runfilt
&& (inet_pfil_hook
.ph_hashooks
> 0
1908 || inet6_pfil_hook
.ph_hashooks
> 0
1911 if (bridge_pfil(&m
, sc
->sc_ifp
, NULL
, PFIL_OUT
) != 0)
1917 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1918 dst_if
= bif
->bif_ifp
;
1919 if (dst_if
== src_if
)
1922 if (bif
->bif_flags
& IFBIF_STP
) {
1923 switch (bif
->bif_state
) {
1924 case BSTP_IFSTATE_BLOCKING
:
1925 case BSTP_IFSTATE_DISABLED
:
1930 if ((bif
->bif_flags
& IFBIF_DISCOVER
) == 0 &&
1931 (m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
1934 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1937 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
1941 mc
= m_copypacket(m
, MB_DONTWAIT
);
1943 sc
->sc_ifp
->if_oerrors
++;
1949 * Filter on the output interface. Pass a NULL bridge interface
1950 * pointer so we do not redundantly filter on the bridge for
1951 * each interface we broadcast on.
1953 if (runfilt
&& (inet_pfil_hook
.ph_hashooks
> 0
1955 || inet6_pfil_hook
.ph_hashooks
> 0
1958 if (bridge_pfil(&mc
, NULL
, dst_if
, PFIL_OUT
) != 0)
1964 bridge_enqueue(sc
, dst_if
, mc
);
1973 * Duplicate a packet out one or more interfaces that are in span mode,
1974 * the original mbuf is unmodified.
1977 bridge_span(struct bridge_softc
*sc
, struct mbuf
*m
)
1979 struct bridge_iflist
*bif
;
1980 struct ifnet
*dst_if
;
1983 if (LIST_EMPTY(&sc
->sc_spanlist
))
1986 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
1987 dst_if
= bif
->bif_ifp
;
1989 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
1992 mc
= m_copypacket(m
, MB_DONTWAIT
);
1994 sc
->sc_ifp
->if_oerrors
++;
1998 bridge_enqueue(sc
, dst_if
, mc
);
2005 * Add a bridge routing entry.
2006 * Can be called from interrupt context.
2009 bridge_rtupdate(struct bridge_softc
*sc
, const uint8_t *dst
,
2010 struct ifnet
*dst_if
, int setflags
, uint8_t flags
)
2012 struct bridge_rtnode
*brt
;
2016 * A route for this destination might already exist. If so,
2017 * update it, otherwise create a new one.
2019 if ((brt
= bridge_rtnode_lookup(sc
, dst
)) == NULL
) {
2020 if (sc
->sc_brtcnt
>= sc
->sc_brtmax
)
2024 * Allocate a new bridge forwarding node, and
2025 * initialize the expiration time and Ethernet
2028 brt
= kmalloc(sizeof(struct bridge_rtnode
), M_DEVBUF
,
2029 M_INTNOWAIT
|M_ZERO
);
2033 brt
->brt_flags
= IFBAF_DYNAMIC
;
2034 memcpy(brt
->brt_addr
, dst
, ETHER_ADDR_LEN
);
2036 if ((error
= bridge_rtnode_insert(sc
, brt
)) != 0) {
2037 kfree(brt
, M_DEVBUF
);
2042 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2043 brt
->brt_ifp
= dst_if
;
2044 if ((flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2045 brt
->brt_expire
= time_second
+ sc
->sc_brttimeout
;
2047 brt
->brt_flags
= flags
;
2055 * Lookup the destination interface for an address.
2057 static struct ifnet
*
2058 bridge_rtlookup(struct bridge_softc
*sc
, const uint8_t *addr
)
2060 struct bridge_rtnode
*brt
;
2062 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
2065 return (brt
->brt_ifp
);
2071 * Trim the routine table so that we have a number
2072 * of routing entries less than or equal to the
2076 bridge_rttrim(struct bridge_softc
*sc
)
2078 struct bridge_rtnode
*brt
, *nbrt
;
2080 /* Make sure we actually need to do this. */
2081 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2084 /* Force an aging cycle; this might trim enough addresses. */
2086 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2089 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2090 nbrt
= LIST_NEXT(brt
, brt_list
);
2091 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2092 bridge_rtnode_destroy(sc
, brt
);
2093 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2102 * Aging timer for the bridge.
2105 bridge_timer(void *arg
)
2107 struct bridge_softc
*sc
= arg
;
2109 lwkt_serialize_enter(sc
->sc_ifp
->if_serializer
);
2113 if (sc
->sc_ifp
->if_flags
& IFF_RUNNING
)
2114 callout_reset(&sc
->sc_brcallout
,
2115 bridge_rtable_prune_period
* hz
, bridge_timer
, sc
);
2117 lwkt_serialize_exit(sc
->sc_ifp
->if_serializer
);
2123 * Perform an aging cycle.
2126 bridge_rtage(struct bridge_softc
*sc
)
2128 struct bridge_rtnode
*brt
, *nbrt
;
2130 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2131 nbrt
= LIST_NEXT(brt
, brt_list
);
2132 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2133 if (time_second
>= brt
->brt_expire
)
2134 bridge_rtnode_destroy(sc
, brt
);
2142 * Remove all dynamic addresses from the bridge.
2145 bridge_rtflush(struct bridge_softc
*sc
, int full
)
2147 struct bridge_rtnode
*brt
, *nbrt
;
2149 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2150 nbrt
= LIST_NEXT(brt
, brt_list
);
2151 if (full
|| (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2152 bridge_rtnode_destroy(sc
, brt
);
2159 * Remove an address from the table.
2162 bridge_rtdaddr(struct bridge_softc
*sc
, const uint8_t *addr
)
2164 struct bridge_rtnode
*brt
;
2166 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
2169 bridge_rtnode_destroy(sc
, brt
);
2176 * Delete routes to a speicifc member interface.
2179 bridge_rtdelete(struct bridge_softc
*sc
, struct ifnet
*ifp
, int full
)
2181 struct bridge_rtnode
*brt
, *nbrt
;
2183 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
2184 nbrt
= LIST_NEXT(brt
, brt_list
);
2185 if (brt
->brt_ifp
== ifp
&& (full
||
2186 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
))
2187 bridge_rtnode_destroy(sc
, brt
);
2192 * bridge_rtable_init:
2194 * Initialize the route table for this bridge.
2197 bridge_rtable_init(struct bridge_softc
*sc
)
2201 sc
->sc_rthash
= kmalloc(sizeof(*sc
->sc_rthash
) * BRIDGE_RTHASH_SIZE
,
2202 M_DEVBUF
, M_WAITOK
);
2204 for (i
= 0; i
< BRIDGE_RTHASH_SIZE
; i
++)
2205 LIST_INIT(&sc
->sc_rthash
[i
]);
2207 sc
->sc_rthash_key
= karc4random();
2209 LIST_INIT(&sc
->sc_rtlist
);
2215 * bridge_rtable_fini:
2217 * Deconstruct the route table for this bridge.
2220 bridge_rtable_fini(struct bridge_softc
*sc
)
2223 kfree(sc
->sc_rthash
, M_DEVBUF
);
2227 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2228 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2230 #define mix(a, b, c) \
2232 a -= b; a -= c; a ^= (c >> 13); \
2233 b -= c; b -= a; b ^= (a << 8); \
2234 c -= a; c -= b; c ^= (b >> 13); \
2235 a -= b; a -= c; a ^= (c >> 12); \
2236 b -= c; b -= a; b ^= (a << 16); \
2237 c -= a; c -= b; c ^= (b >> 5); \
2238 a -= b; a -= c; a ^= (c >> 3); \
2239 b -= c; b -= a; b ^= (a << 10); \
2240 c -= a; c -= b; c ^= (b >> 15); \
2241 } while (/*CONSTCOND*/0)
2243 static __inline
uint32_t
2244 bridge_rthash(struct bridge_softc
*sc
, const uint8_t *addr
)
2246 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= sc
->sc_rthash_key
;
2257 return (c
& BRIDGE_RTHASH_MASK
);
2263 bridge_rtnode_addr_cmp(const uint8_t *a
, const uint8_t *b
)
2267 for (i
= 0, d
= 0; i
< ETHER_ADDR_LEN
&& d
== 0; i
++) {
2268 d
= ((int)a
[i
]) - ((int)b
[i
]);
2275 * bridge_rtnode_lookup:
2277 * Look up a bridge route node for the specified destination.
2279 static struct bridge_rtnode
*
2280 bridge_rtnode_lookup(struct bridge_softc
*sc
, const uint8_t *addr
)
2282 struct bridge_rtnode
*brt
;
2286 hash
= bridge_rthash(sc
, addr
);
2287 LIST_FOREACH(brt
, &sc
->sc_rthash
[hash
], brt_hash
) {
2288 dir
= bridge_rtnode_addr_cmp(addr
, brt
->brt_addr
);
2299 * bridge_rtnode_insert:
2301 * Insert the specified bridge node into the route table. We
2302 * assume the entry is not already in the table.
2305 bridge_rtnode_insert(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2307 struct bridge_rtnode
*lbrt
;
2311 hash
= bridge_rthash(sc
, brt
->brt_addr
);
2313 lbrt
= LIST_FIRST(&sc
->sc_rthash
[hash
]);
2315 LIST_INSERT_HEAD(&sc
->sc_rthash
[hash
], brt
, brt_hash
);
2320 dir
= bridge_rtnode_addr_cmp(brt
->brt_addr
, lbrt
->brt_addr
);
2324 LIST_INSERT_BEFORE(lbrt
, brt
, brt_hash
);
2327 if (LIST_NEXT(lbrt
, brt_hash
) == NULL
) {
2328 LIST_INSERT_AFTER(lbrt
, brt
, brt_hash
);
2331 lbrt
= LIST_NEXT(lbrt
, brt_hash
);
2332 } while (lbrt
!= NULL
);
2335 panic("bridge_rtnode_insert: impossible");
2339 LIST_INSERT_HEAD(&sc
->sc_rtlist
, brt
, brt_list
);
2346 * bridge_rtnode_destroy:
2348 * Destroy a bridge rtnode.
2351 bridge_rtnode_destroy(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2354 LIST_REMOVE(brt
, brt_hash
);
2356 LIST_REMOVE(brt
, brt_list
);
2358 kfree(brt
, M_DEVBUF
);
2362 * Send bridge packets through pfil if they are one of the types pfil can deal
2363 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2364 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2368 bridge_pfil(struct mbuf
**mp
, struct ifnet
*bifp
, struct ifnet
*ifp
, int dir
)
2370 int snap
, error
, i
, hlen
;
2371 struct ether_header
*eh1
, eh2
;
2374 u_int16_t ether_type
;
2377 error
= -1; /* Default error if not error == 0 */
2379 if (pfil_bridge
== 0 && pfil_member
== 0)
2380 return (0); /* filtering is disabled */
2382 i
= min((*mp
)->m_pkthdr
.len
, max_protohdr
);
2383 if ((*mp
)->m_len
< i
) {
2384 *mp
= m_pullup(*mp
, i
);
2386 kprintf("%s: m_pullup failed\n", __func__
);
2391 eh1
= mtod(*mp
, struct ether_header
*);
2392 ether_type
= ntohs(eh1
->ether_type
);
2395 * Check for SNAP/LLC.
2397 if (ether_type
< ETHERMTU
) {
2398 struct llc
*llc2
= (struct llc
*)(eh1
+ 1);
2400 if ((*mp
)->m_len
>= ETHER_HDR_LEN
+ 8 &&
2401 llc2
->llc_dsap
== LLC_SNAP_LSAP
&&
2402 llc2
->llc_ssap
== LLC_SNAP_LSAP
&&
2403 llc2
->llc_control
== LLC_UI
) {
2404 ether_type
= htons(llc2
->llc_un
.type_snap
.ether_type
);
2410 * If we're trying to filter bridge traffic, don't look at anything
2411 * other than IP and ARP traffic. If the filter doesn't understand
2412 * IPv6, don't allow IPv6 through the bridge either. This is lame
2413 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2414 * but of course we don't have an AppleTalk filter to begin with.
2415 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2418 switch (ether_type
) {
2420 case ETHERTYPE_REVARP
:
2421 return (0); /* Automatically pass */
2424 case ETHERTYPE_IPV6
:
2429 * Check to see if the user wants to pass non-ip
2430 * packets, these will not be checked by pfil(9) and
2431 * passed unconditionally so the default is to drop.
2437 /* Strip off the Ethernet header and keep a copy. */
2438 m_copydata(*mp
, 0, ETHER_HDR_LEN
, (caddr_t
) &eh2
);
2439 m_adj(*mp
, ETHER_HDR_LEN
);
2441 /* Strip off snap header, if present */
2443 m_copydata(*mp
, 0, sizeof(struct llc
), (caddr_t
) &llc1
);
2444 m_adj(*mp
, sizeof(struct llc
));
2448 * Check the IP header for alignment and errors
2450 if (dir
== PFIL_IN
) {
2451 switch (ether_type
) {
2453 error
= bridge_ip_checkbasic(mp
);
2456 case ETHERTYPE_IPV6
:
2457 error
= bridge_ip6_checkbasic(mp
);
2470 * Run the packet through pfil
2476 * before calling the firewall, swap fields the same as
2477 * IP does. here we assume the header is contiguous
2479 ip
= mtod(*mp
, struct ip
*);
2481 ip
->ip_len
= ntohs(ip
->ip_len
);
2482 ip
->ip_off
= ntohs(ip
->ip_off
);
2485 * Run pfil on the member interface and the bridge, both can
2486 * be skipped by clearing pfil_member or pfil_bridge.
2489 * in_if -> bridge_if -> out_if
2491 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
2492 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
2495 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2498 if (pfil_member
&& ifp
!= NULL
)
2499 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, ifp
,
2502 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2505 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
2506 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
2509 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2512 /* check if we need to fragment the packet */
2513 if (pfil_member
&& ifp
!= NULL
&& dir
== PFIL_OUT
) {
2514 i
= (*mp
)->m_pkthdr
.len
;
2515 if (i
> ifp
->if_mtu
) {
2516 error
= bridge_fragment(ifp
, *mp
, &eh2
, snap
,
2522 /* Recalculate the ip checksum and restore byte ordering */
2523 ip
= mtod(*mp
, struct ip
*);
2524 hlen
= ip
->ip_hl
<< 2;
2525 if (hlen
< sizeof(struct ip
))
2527 if (hlen
> (*mp
)->m_len
) {
2528 if ((*mp
= m_pullup(*mp
, hlen
)) == 0)
2530 ip
= mtod(*mp
, struct ip
*);
2534 ip
->ip_len
= htons(ip
->ip_len
);
2535 ip
->ip_off
= htons(ip
->ip_off
);
2537 if (hlen
== sizeof(struct ip
))
2538 ip
->ip_sum
= in_cksum_hdr(ip
);
2540 ip
->ip_sum
= in_cksum(*mp
, hlen
);
2544 case ETHERTYPE_IPV6
:
2545 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
2546 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
2549 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2552 if (pfil_member
&& ifp
!= NULL
)
2553 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, ifp
,
2556 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
2559 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
2560 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
2577 * Finally, put everything back the way it was and return
2580 M_PREPEND(*mp
, sizeof(struct llc
), MB_DONTWAIT
);
2583 bcopy(&llc1
, mtod(*mp
, caddr_t
), sizeof(struct llc
));
2586 M_PREPEND(*mp
, ETHER_HDR_LEN
, MB_DONTWAIT
);
2589 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
2600 * Perform basic checks on header size since
2601 * pfil assumes ip_input has already processed
2602 * it for it. Cut-and-pasted from ip_input.c.
2603 * Given how simple the IPv6 version is,
2604 * does the IPv4 version really need to be
2607 * XXX Should we update ipstat here, or not?
2608 * XXX Right now we update ipstat but not
2612 bridge_ip_checkbasic(struct mbuf
**mp
)
2614 struct mbuf
*m
= *mp
;
2622 if (IP_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
2623 if ((m
= m_copyup(m
, sizeof(struct ip
),
2624 (max_linkhdr
+ 3) & ~3)) == NULL
) {
2625 /* XXXJRT new stat, please */
2626 ipstat
.ips_toosmall
++;
2631 #ifndef __predict_false
2632 #define __predict_false(x) x
2634 if (__predict_false(m
->m_len
< sizeof (struct ip
))) {
2635 if ((m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
2636 ipstat
.ips_toosmall
++;
2640 ip
= mtod(m
, struct ip
*);
2641 if (ip
== NULL
) goto bad
;
2643 if (ip
->ip_v
!= IPVERSION
) {
2644 ipstat
.ips_badvers
++;
2647 hlen
= ip
->ip_hl
<< 2;
2648 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
2649 ipstat
.ips_badhlen
++;
2652 if (hlen
> m
->m_len
) {
2653 if ((m
= m_pullup(m
, hlen
)) == 0) {
2654 ipstat
.ips_badhlen
++;
2657 ip
= mtod(m
, struct ip
*);
2658 if (ip
== NULL
) goto bad
;
2661 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
2662 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
2664 if (hlen
== sizeof(struct ip
)) {
2665 sum
= in_cksum_hdr(ip
);
2667 sum
= in_cksum(m
, hlen
);
2671 ipstat
.ips_badsum
++;
2675 /* Retrieve the packet length. */
2676 len
= ntohs(ip
->ip_len
);
2679 * Check for additional length bogosity
2682 ipstat
.ips_badlen
++;
2687 * Check that the amount of data in the buffers
2688 * is as at least much as the IP header would have us expect.
2689 * Drop packet if shorter than we expect.
2691 if (m
->m_pkthdr
.len
< len
) {
2692 ipstat
.ips_tooshort
++;
2696 /* Checks out, proceed */
2707 * Same as above, but for IPv6.
2708 * Cut-and-pasted from ip6_input.c.
2709 * XXX Should we update ip6stat, or not?
2712 bridge_ip6_checkbasic(struct mbuf
**mp
)
2714 struct mbuf
*m
= *mp
;
2715 struct ip6_hdr
*ip6
;
2718 * If the IPv6 header is not aligned, slurp it up into a new
2719 * mbuf with space for link headers, in the event we forward
2720 * it. Otherwise, if it is aligned, make sure the entire base
2721 * IPv6 header is in the first mbuf of the chain.
2724 if (IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
2725 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
2726 if ((m
= m_copyup(m
, sizeof(struct ip6_hdr
),
2727 (max_linkhdr
+ 3) & ~3)) == NULL
) {
2728 /* XXXJRT new stat, please */
2729 ip6stat
.ip6s_toosmall
++;
2730 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
2735 if (__predict_false(m
->m_len
< sizeof(struct ip6_hdr
))) {
2736 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
2737 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == NULL
) {
2738 ip6stat
.ip6s_toosmall
++;
2739 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
2744 ip6
= mtod(m
, struct ip6_hdr
*);
2746 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
2747 ip6stat
.ip6s_badvers
++;
2748 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
2752 /* Checks out, proceed */
2765 * Return a fragmented mbuf chain.
2768 bridge_fragment(struct ifnet
*ifp
, struct mbuf
*m
, struct ether_header
*eh
,
2769 int snap
, struct llc
*llc
)
2775 if (m
->m_len
< sizeof(struct ip
) &&
2776 (m
= m_pullup(m
, sizeof(struct ip
))) == NULL
)
2778 ip
= mtod(m
, struct ip
*);
2780 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
,
2785 /* walk the chain and re-add the Ethernet header */
2786 for (m0
= m
; m0
; m0
= m0
->m_nextpkt
) {
2789 M_PREPEND(m0
, sizeof(struct llc
), MB_DONTWAIT
);
2794 bcopy(llc
, mtod(m0
, caddr_t
),
2795 sizeof(struct llc
));
2797 M_PREPEND(m0
, ETHER_HDR_LEN
, MB_DONTWAIT
);
2802 bcopy(eh
, mtod(m0
, caddr_t
), ETHER_HDR_LEN
);
2808 ipstat
.ips_fragmented
++;