1 /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright 2001 Wasabi Systems, Inc.
9 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed for the NetBSD Project by
22 * Wasabi Systems, Inc.
23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24 * or promote products derived from this software without specific prior
27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
42 * All rights reserved.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
54 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
55 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
57 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
61 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
62 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
65 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
69 * Network interface bridge support.
73 * - Currently only supports Ethernet-like interfaces (Ethernet,
74 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
75 * to bridge other types of interfaces (maybe consider
76 * heterogeneous bridges).
79 #include <sys/cdefs.h>
80 __FBSDID("$FreeBSD$");
83 #include "opt_inet6.h"
85 #include <sys/param.h>
86 #include <sys/eventhandler.h>
88 #include <sys/malloc.h>
89 #include <sys/protosw.h>
90 #include <sys/systm.h>
93 #include <sys/socket.h> /* for net/if.h */
94 #include <sys/sockio.h>
95 #include <sys/ctype.h> /* string functions */
96 #include <sys/kernel.h>
97 #include <sys/random.h>
98 #include <sys/syslog.h>
99 #include <sys/sysctl.h>
101 #include <sys/module.h>
102 #include <sys/priv.h>
103 #include <sys/proc.h>
104 #include <sys/lock.h>
105 #include <sys/mutex.h>
109 #include <net/if_clone.h>
110 #include <net/if_dl.h>
111 #include <net/if_types.h>
112 #include <net/if_var.h>
113 #include <net/pfil.h>
114 #include <net/vnet.h>
116 #include <netinet/in.h>
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>
124 #include <netinet6/in6_ifattach.h>
126 #if defined(INET) || defined(INET6)
127 #include <netinet/ip_carp.h>
129 #include <machine/in_cksum.h>
130 #include <netinet/if_ether.h>
131 #include <net/bridgestp.h>
132 #include <net/if_bridgevar.h>
133 #include <net/if_llc.h>
134 #include <net/if_vlan_var.h>
136 #include <net/route.h>
140 * XXX: declare here to avoid to include many inet6 related files..
141 * should be more generalized?
143 extern void nd6_setmtu(struct ifnet *);
147 * Size of the route hash table. Must be a power of two.
149 #ifndef BRIDGE_RTHASH_SIZE
150 #define BRIDGE_RTHASH_SIZE 1024
153 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
156 * Default maximum number of addresses to cache.
158 #ifndef BRIDGE_RTABLE_MAX
159 #define BRIDGE_RTABLE_MAX 2000
163 * Timeout (in seconds) for entries learned dynamically.
165 #ifndef BRIDGE_RTABLE_TIMEOUT
166 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
170 * Number of seconds between walks of the route list.
172 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
173 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
177 * List of capabilities to possibly mask on the member interface.
179 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM|\
183 * List of capabilities to strip
185 #define BRIDGE_IFCAPS_STRIP IFCAP_LRO
190 #define BRIDGE_LOCK_INIT(_sc) do { \
191 mtx_init(&(_sc)->sc_mtx, "if_bridge", NULL, MTX_DEF); \
192 cv_init(&(_sc)->sc_cv, "if_bridge_cv"); \
194 #define BRIDGE_LOCK_DESTROY(_sc) do { \
195 mtx_destroy(&(_sc)->sc_mtx); \
196 cv_destroy(&(_sc)->sc_cv); \
198 #define BRIDGE_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
199 #define BRIDGE_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
200 #define BRIDGE_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
201 #define BRIDGE_UNLOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED)
202 #define BRIDGE_LOCK2REF(_sc, _err) do { \
203 mtx_assert(&(_sc)->sc_mtx, MA_OWNED); \
204 if ((_sc)->sc_iflist_xcnt > 0) \
207 (_sc)->sc_iflist_ref++; \
208 mtx_unlock(&(_sc)->sc_mtx); \
210 #define BRIDGE_UNREF(_sc) do { \
211 mtx_lock(&(_sc)->sc_mtx); \
212 (_sc)->sc_iflist_ref--; \
213 if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0)) \
214 cv_broadcast(&(_sc)->sc_cv); \
215 mtx_unlock(&(_sc)->sc_mtx); \
217 #define BRIDGE_XLOCK(_sc) do { \
218 mtx_assert(&(_sc)->sc_mtx, MA_OWNED); \
219 (_sc)->sc_iflist_xcnt++; \
220 while ((_sc)->sc_iflist_ref > 0) \
221 cv_wait(&(_sc)->sc_cv, &(_sc)->sc_mtx); \
223 #define BRIDGE_XDROP(_sc) do { \
224 mtx_assert(&(_sc)->sc_mtx, MA_OWNED); \
225 (_sc)->sc_iflist_xcnt--; \
229 * Bridge interface list entry.
231 struct bridge_iflist {
232 LIST_ENTRY(bridge_iflist) bif_next;
233 struct ifnet *bif_ifp; /* member if */
234 struct bstp_port bif_stp; /* STP state */
235 uint32_t bif_flags; /* member if flags */
236 int bif_savedcaps; /* saved capabilities */
237 uint32_t bif_addrmax; /* max # of addresses */
238 uint32_t bif_addrcnt; /* cur. # of addresses */
239 uint32_t bif_addrexceeded;/* # of address violations */
245 struct bridge_rtnode {
246 LIST_ENTRY(bridge_rtnode) brt_hash; /* hash table linkage */
247 LIST_ENTRY(bridge_rtnode) brt_list; /* list linkage */
248 struct bridge_iflist *brt_dst; /* destination if */
249 unsigned long brt_expire; /* expiration time */
250 uint8_t brt_flags; /* address flags */
251 uint8_t brt_addr[ETHER_ADDR_LEN];
252 uint16_t brt_vlan; /* vlan id */
254 #define brt_ifp brt_dst->bif_ifp
257 * Software state for each bridge.
259 struct bridge_softc {
260 struct ifnet *sc_ifp; /* make this an interface */
261 LIST_ENTRY(bridge_softc) sc_list;
264 uint32_t sc_brtmax; /* max # of addresses */
265 uint32_t sc_brtcnt; /* cur. # of addresses */
266 uint32_t sc_brttimeout; /* rt timeout in seconds */
267 struct callout sc_brcallout; /* bridge callout */
268 uint32_t sc_iflist_ref; /* refcount for sc_iflist */
269 uint32_t sc_iflist_xcnt; /* refcount for sc_iflist */
270 LIST_HEAD(, bridge_iflist) sc_iflist; /* member interface list */
271 LIST_HEAD(, bridge_rtnode) *sc_rthash; /* our forwarding table */
272 LIST_HEAD(, bridge_rtnode) sc_rtlist; /* list version of above */
273 uint32_t sc_rthash_key; /* key for hash */
274 LIST_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */
275 struct bstp_state sc_stp; /* STP state */
276 uint32_t sc_brtexceeded; /* # of cache drops */
277 struct ifnet *sc_ifaddr; /* member mac copied from */
278 struct ether_addr sc_defaddr; /* Default MAC address */
281 VNET_DEFINE_STATIC(struct mtx, bridge_list_mtx);
282 #define V_bridge_list_mtx VNET(bridge_list_mtx)
283 static eventhandler_tag bridge_detach_cookie;
285 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
287 VNET_DEFINE_STATIC(uma_zone_t, bridge_rtnode_zone);
288 #define V_bridge_rtnode_zone VNET(bridge_rtnode_zone)
290 static int bridge_clone_create(struct if_clone *, int, caddr_t);
291 static void bridge_clone_destroy(struct ifnet *);
293 static int bridge_ioctl(struct ifnet *, u_long, caddr_t);
294 static void bridge_mutecaps(struct bridge_softc *);
295 static void bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *,
297 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
298 static void bridge_init(void *);
299 static void bridge_dummynet(struct mbuf *, struct ifnet *);
300 static void bridge_stop(struct ifnet *, int);
301 static int bridge_transmit(struct ifnet *, struct mbuf *);
302 static void bridge_qflush(struct ifnet *);
303 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
304 static int bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
306 static int bridge_enqueue(struct bridge_softc *, struct ifnet *,
308 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
310 static void bridge_forward(struct bridge_softc *, struct bridge_iflist *,
313 static void bridge_timer(void *);
315 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
317 static void bridge_span(struct bridge_softc *, struct mbuf *);
319 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
320 uint16_t, struct bridge_iflist *, int, uint8_t);
321 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *,
323 static void bridge_rttrim(struct bridge_softc *);
324 static void bridge_rtage(struct bridge_softc *);
325 static void bridge_rtflush(struct bridge_softc *, int);
326 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *,
329 static void bridge_rtable_init(struct bridge_softc *);
330 static void bridge_rtable_fini(struct bridge_softc *);
332 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
333 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
334 const uint8_t *, uint16_t);
335 static int bridge_rtnode_insert(struct bridge_softc *,
336 struct bridge_rtnode *);
337 static void bridge_rtnode_destroy(struct bridge_softc *,
338 struct bridge_rtnode *);
339 static void bridge_rtable_expire(struct ifnet *, int);
340 static void bridge_state_change(struct ifnet *, int);
342 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
344 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
346 static void bridge_delete_member(struct bridge_softc *,
347 struct bridge_iflist *, int);
348 static void bridge_delete_span(struct bridge_softc *,
349 struct bridge_iflist *);
351 static int bridge_ioctl_add(struct bridge_softc *, void *);
352 static int bridge_ioctl_del(struct bridge_softc *, void *);
353 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
354 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
355 static int bridge_ioctl_scache(struct bridge_softc *, void *);
356 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
357 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
358 static int bridge_ioctl_rts(struct bridge_softc *, void *);
359 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
360 static int bridge_ioctl_sto(struct bridge_softc *, void *);
361 static int bridge_ioctl_gto(struct bridge_softc *, void *);
362 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
363 static int bridge_ioctl_flush(struct bridge_softc *, void *);
364 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
365 static int bridge_ioctl_spri(struct bridge_softc *, void *);
366 static int bridge_ioctl_ght(struct bridge_softc *, void *);
367 static int bridge_ioctl_sht(struct bridge_softc *, void *);
368 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
369 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
370 static int bridge_ioctl_gma(struct bridge_softc *, void *);
371 static int bridge_ioctl_sma(struct bridge_softc *, void *);
372 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
373 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
374 static int bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *);
375 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
376 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
377 static int bridge_ioctl_gbparam(struct bridge_softc *, void *);
378 static int bridge_ioctl_grte(struct bridge_softc *, void *);
379 static int bridge_ioctl_gifsstp(struct bridge_softc *, void *);
380 static int bridge_ioctl_sproto(struct bridge_softc *, void *);
381 static int bridge_ioctl_stxhc(struct bridge_softc *, void *);
382 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
384 static int bridge_ip_checkbasic(struct mbuf **mp);
386 static int bridge_ip6_checkbasic(struct mbuf **mp);
388 static int bridge_fragment(struct ifnet *, struct mbuf **mp,
389 struct ether_header *, int, struct llc *);
390 static void bridge_linkstate(struct ifnet *ifp);
391 static void bridge_linkcheck(struct bridge_softc *sc);
394 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
395 #define VLANTAGOF(_m) \
396 (_m->m_flags & M_VLANTAG) ? EVL_VLANOFTAG(_m->m_pkthdr.ether_vtag) : 1
398 static struct bstp_cb_ops bridge_ops = {
399 .bcb_state = bridge_state_change,
400 .bcb_rtage = bridge_rtable_expire
403 SYSCTL_DECL(_net_link);
404 static SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
407 /* only pass IP[46] packets when pfil is enabled */
408 VNET_DEFINE_STATIC(int, pfil_onlyip) = 1;
409 #define V_pfil_onlyip VNET(pfil_onlyip)
410 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip,
411 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_onlyip), 0,
412 "Only pass IP packets when pfil is enabled");
414 /* run pfil hooks on the bridge interface */
415 VNET_DEFINE_STATIC(int, pfil_bridge) = 1;
416 #define V_pfil_bridge VNET(pfil_bridge)
417 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge,
418 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_bridge), 0,
419 "Packet filter on the bridge interface");
421 /* layer2 filter with ipfw */
422 VNET_DEFINE_STATIC(int, pfil_ipfw);
423 #define V_pfil_ipfw VNET(pfil_ipfw)
425 /* layer2 ARP filter with ipfw */
426 VNET_DEFINE_STATIC(int, pfil_ipfw_arp);
427 #define V_pfil_ipfw_arp VNET(pfil_ipfw_arp)
428 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp,
429 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_ipfw_arp), 0,
430 "Filter ARP packets through IPFW layer2");
432 /* run pfil hooks on the member interface */
433 VNET_DEFINE_STATIC(int, pfil_member) = 1;
434 #define V_pfil_member VNET(pfil_member)
435 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member,
436 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_member), 0,
437 "Packet filter on the member interface");
439 /* run pfil hooks on the physical interface for locally destined packets */
440 VNET_DEFINE_STATIC(int, pfil_local_phys);
441 #define V_pfil_local_phys VNET(pfil_local_phys)
442 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys,
443 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_local_phys), 0,
444 "Packet filter on the physical interface for locally destined packets");
446 /* log STP state changes */
447 VNET_DEFINE_STATIC(int, log_stp);
448 #define V_log_stp VNET(log_stp)
449 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp,
450 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(log_stp), 0,
451 "Log STP state changes");
453 /* share MAC with first bridge member */
454 VNET_DEFINE_STATIC(int, bridge_inherit_mac);
455 #define V_bridge_inherit_mac VNET(bridge_inherit_mac)
456 SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac,
457 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(bridge_inherit_mac), 0,
458 "Inherit MAC address from the first bridge member");
460 VNET_DEFINE_STATIC(int, allow_llz_overlap) = 0;
461 #define V_allow_llz_overlap VNET(allow_llz_overlap)
462 SYSCTL_INT(_net_link_bridge, OID_AUTO, allow_llz_overlap,
463 CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(allow_llz_overlap), 0,
464 "Allow overlap of link-local scope "
465 "zones of a bridge interface and the member interfaces");
467 struct bridge_control {
468 int (*bc_func)(struct bridge_softc *, void *);
473 #define BC_F_COPYIN 0x01 /* copy arguments in */
474 #define BC_F_COPYOUT 0x02 /* copy arguments out */
475 #define BC_F_SUSER 0x04 /* do super-user check */
477 const struct bridge_control bridge_control_table[] = {
478 { bridge_ioctl_add, sizeof(struct ifbreq),
479 BC_F_COPYIN|BC_F_SUSER },
480 { bridge_ioctl_del, sizeof(struct ifbreq),
481 BC_F_COPYIN|BC_F_SUSER },
483 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
484 BC_F_COPYIN|BC_F_COPYOUT },
485 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
486 BC_F_COPYIN|BC_F_SUSER },
488 { bridge_ioctl_scache, sizeof(struct ifbrparam),
489 BC_F_COPYIN|BC_F_SUSER },
490 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
493 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
494 BC_F_COPYIN|BC_F_COPYOUT },
495 { bridge_ioctl_rts, sizeof(struct ifbaconf),
496 BC_F_COPYIN|BC_F_COPYOUT },
498 { bridge_ioctl_saddr, sizeof(struct ifbareq),
499 BC_F_COPYIN|BC_F_SUSER },
501 { bridge_ioctl_sto, sizeof(struct ifbrparam),
502 BC_F_COPYIN|BC_F_SUSER },
503 { bridge_ioctl_gto, sizeof(struct ifbrparam),
506 { bridge_ioctl_daddr, sizeof(struct ifbareq),
507 BC_F_COPYIN|BC_F_SUSER },
509 { bridge_ioctl_flush, sizeof(struct ifbreq),
510 BC_F_COPYIN|BC_F_SUSER },
512 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
514 { bridge_ioctl_spri, sizeof(struct ifbrparam),
515 BC_F_COPYIN|BC_F_SUSER },
517 { bridge_ioctl_ght, sizeof(struct ifbrparam),
519 { bridge_ioctl_sht, sizeof(struct ifbrparam),
520 BC_F_COPYIN|BC_F_SUSER },
522 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
524 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
525 BC_F_COPYIN|BC_F_SUSER },
527 { bridge_ioctl_gma, sizeof(struct ifbrparam),
529 { bridge_ioctl_sma, sizeof(struct ifbrparam),
530 BC_F_COPYIN|BC_F_SUSER },
532 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
533 BC_F_COPYIN|BC_F_SUSER },
535 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
536 BC_F_COPYIN|BC_F_SUSER },
538 { bridge_ioctl_addspan, sizeof(struct ifbreq),
539 BC_F_COPYIN|BC_F_SUSER },
540 { bridge_ioctl_delspan, sizeof(struct ifbreq),
541 BC_F_COPYIN|BC_F_SUSER },
543 { bridge_ioctl_gbparam, sizeof(struct ifbropreq),
546 { bridge_ioctl_grte, sizeof(struct ifbrparam),
549 { bridge_ioctl_gifsstp, sizeof(struct ifbpstpconf),
550 BC_F_COPYIN|BC_F_COPYOUT },
552 { bridge_ioctl_sproto, sizeof(struct ifbrparam),
553 BC_F_COPYIN|BC_F_SUSER },
555 { bridge_ioctl_stxhc, sizeof(struct ifbrparam),
556 BC_F_COPYIN|BC_F_SUSER },
558 { bridge_ioctl_sifmaxaddr, sizeof(struct ifbreq),
559 BC_F_COPYIN|BC_F_SUSER },
562 const int bridge_control_table_size = nitems(bridge_control_table);
564 VNET_DEFINE_STATIC(LIST_HEAD(, bridge_softc), bridge_list);
565 #define V_bridge_list VNET(bridge_list)
566 #define BRIDGE_LIST_LOCK_INIT(x) mtx_init(&V_bridge_list_mtx, \
567 "if_bridge list", NULL, MTX_DEF)
568 #define BRIDGE_LIST_LOCK_DESTROY(x) mtx_destroy(&V_bridge_list_mtx)
569 #define BRIDGE_LIST_LOCK(x) mtx_lock(&V_bridge_list_mtx)
570 #define BRIDGE_LIST_UNLOCK(x) mtx_unlock(&V_bridge_list_mtx)
572 VNET_DEFINE_STATIC(struct if_clone *, bridge_cloner);
573 #define V_bridge_cloner VNET(bridge_cloner)
575 static const char bridge_name[] = "bridge";
578 vnet_bridge_init(const void *unused __unused)
581 V_bridge_rtnode_zone = uma_zcreate("bridge_rtnode",
582 sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL,
584 BRIDGE_LIST_LOCK_INIT();
585 LIST_INIT(&V_bridge_list);
586 V_bridge_cloner = if_clone_simple(bridge_name,
587 bridge_clone_create, bridge_clone_destroy, 0);
589 VNET_SYSINIT(vnet_bridge_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
590 vnet_bridge_init, NULL);
593 vnet_bridge_uninit(const void *unused __unused)
596 if_clone_detach(V_bridge_cloner);
597 V_bridge_cloner = NULL;
598 BRIDGE_LIST_LOCK_DESTROY();
599 uma_zdestroy(V_bridge_rtnode_zone);
601 VNET_SYSUNINIT(vnet_bridge_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
602 vnet_bridge_uninit, NULL);
605 bridge_modevent(module_t mod, int type, void *data)
610 bridge_dn_p = bridge_dummynet;
611 bridge_detach_cookie = EVENTHANDLER_REGISTER(
612 ifnet_departure_event, bridge_ifdetach, NULL,
613 EVENTHANDLER_PRI_ANY);
616 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
617 bridge_detach_cookie);
626 static moduledata_t bridge_mod = {
632 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
633 MODULE_VERSION(if_bridge, 1);
634 MODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1);
637 * handler for net.link.bridge.ipfw
640 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS)
642 int enable = V_pfil_ipfw;
645 error = sysctl_handle_int(oidp, &enable, 0, req);
648 if (enable != V_pfil_ipfw) {
649 V_pfil_ipfw = enable;
652 * Disable pfil so that ipfw doesnt run twice, if the user
653 * really wants both then they can re-enable pfil_bridge and/or
654 * pfil_member. Also allow non-ip packets as ipfw can filter by
666 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw,
667 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET | CTLFLAG_NEEDGIANT,
668 &VNET_NAME(pfil_ipfw), 0, &sysctl_pfil_ipfw, "I",
669 "Layer2 filter with IPFW");
672 * bridge_clone_create:
674 * Create a new bridge instance.
677 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t params)
679 struct bridge_softc *sc, *sc2;
680 struct ifnet *bifp, *ifp;
682 unsigned long hostid;
684 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
685 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
691 BRIDGE_LOCK_INIT(sc);
692 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
693 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
695 /* Initialize our routing table. */
696 bridge_rtable_init(sc);
698 callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0);
700 LIST_INIT(&sc->sc_iflist);
701 LIST_INIT(&sc->sc_spanlist);
704 if_initname(ifp, bridge_name, unit);
705 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
706 ifp->if_ioctl = bridge_ioctl;
707 ifp->if_transmit = bridge_transmit;
708 ifp->if_qflush = bridge_qflush;
709 ifp->if_init = bridge_init;
710 ifp->if_type = IFT_BRIDGE;
713 * Generate an ethernet address with a locally administered address.
715 * Since we are using random ethernet addresses for the bridge, it is
716 * possible that we might have address collisions, so make sure that
717 * this hardware address isn't already in use on another bridge.
718 * The first try uses the hostid and falls back to arc4rand().
721 getcredhostid(curthread->td_ucred, &hostid);
723 if (fb || hostid == 0) {
724 ether_gen_addr(ifp, &sc->sc_defaddr);
726 sc->sc_defaddr.octet[0] = 0x2;
727 sc->sc_defaddr.octet[1] = (hostid >> 24) & 0xff;
728 sc->sc_defaddr.octet[2] = (hostid >> 16) & 0xff;
729 sc->sc_defaddr.octet[3] = (hostid >> 8 ) & 0xff;
730 sc->sc_defaddr.octet[4] = hostid & 0xff;
731 sc->sc_defaddr.octet[5] = ifp->if_dunit & 0xff;
737 LIST_FOREACH(sc2, &V_bridge_list, sc_list) {
739 if (memcmp(sc->sc_defaddr.octet,
740 IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
745 BRIDGE_LIST_UNLOCK();
746 } while (retry == 1);
748 bstp_attach(&sc->sc_stp, &bridge_ops);
749 ether_ifattach(ifp, sc->sc_defaddr.octet);
750 /* Now undo some of the damage... */
751 ifp->if_baudrate = 0;
752 ifp->if_type = IFT_BRIDGE;
755 LIST_INSERT_HEAD(&V_bridge_list, sc, sc_list);
756 BRIDGE_LIST_UNLOCK();
762 * bridge_clone_destroy:
764 * Destroy a bridge instance.
767 bridge_clone_destroy(struct ifnet *ifp)
769 struct bridge_softc *sc = ifp->if_softc;
770 struct bridge_iflist *bif;
775 ifp->if_flags &= ~IFF_UP;
777 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
778 bridge_delete_member(sc, bif, 0);
780 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
781 bridge_delete_span(sc, bif);
784 /* Tear down the routing table. */
785 bridge_rtable_fini(sc);
789 callout_drain(&sc->sc_brcallout);
792 LIST_REMOVE(sc, sc_list);
793 BRIDGE_LIST_UNLOCK();
795 bstp_detach(&sc->sc_stp);
799 BRIDGE_LOCK_DESTROY(sc);
806 * Handle a control request from the operator.
809 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
811 struct bridge_softc *sc = ifp->if_softc;
812 struct ifreq *ifr = (struct ifreq *)data;
813 struct bridge_iflist *bif;
814 struct thread *td = curthread;
816 struct ifbreq ifbreq;
817 struct ifbifconf ifbifconf;
818 struct ifbareq ifbareq;
819 struct ifbaconf ifbaconf;
820 struct ifbrparam ifbrparam;
821 struct ifbropreq ifbropreq;
823 struct ifdrv *ifd = (struct ifdrv *) data;
824 const struct bridge_control *bc;
825 int error = 0, oldmtu;
835 if (ifd->ifd_cmd >= bridge_control_table_size) {
839 bc = &bridge_control_table[ifd->ifd_cmd];
841 if (cmd == SIOCGDRVSPEC &&
842 (bc->bc_flags & BC_F_COPYOUT) == 0) {
846 else if (cmd == SIOCSDRVSPEC &&
847 (bc->bc_flags & BC_F_COPYOUT) != 0) {
852 if (bc->bc_flags & BC_F_SUSER) {
853 error = priv_check(td, PRIV_NET_BRIDGE);
858 if (ifd->ifd_len != bc->bc_argsize ||
859 ifd->ifd_len > sizeof(args)) {
864 bzero(&args, sizeof(args));
865 if (bc->bc_flags & BC_F_COPYIN) {
866 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
871 oldmtu = ifp->if_mtu;
873 error = (*bc->bc_func)(sc, &args);
879 * Bridge MTU may change during addition of the first port.
880 * If it did, do network layer specific procedure.
882 if (ifp->if_mtu != oldmtu) {
889 if (bc->bc_flags & BC_F_COPYOUT)
890 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
895 if (!(ifp->if_flags & IFF_UP) &&
896 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
898 * If interface is marked down and it is running,
899 * then stop and disable it.
904 } else if ((ifp->if_flags & IFF_UP) &&
905 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
907 * If interface is marked up and it is stopped, then
915 if (ifr->ifr_mtu < 576) {
919 if (LIST_EMPTY(&sc->sc_iflist)) {
920 sc->sc_ifp->if_mtu = ifr->ifr_mtu;
924 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
925 if (bif->bif_ifp->if_mtu != ifr->ifr_mtu) {
926 log(LOG_NOTICE, "%s: invalid MTU: %u(%s)"
927 " != %d\n", sc->sc_ifp->if_xname,
928 bif->bif_ifp->if_mtu,
929 bif->bif_ifp->if_xname, ifr->ifr_mtu);
935 sc->sc_ifp->if_mtu = ifr->ifr_mtu;
940 * drop the lock as ether_ioctl() will call bridge_start() and
941 * cause the lock to be recursed.
943 error = ether_ioctl(ifp, cmd, data);
953 * Clear or restore unwanted capabilities on the member interface
956 bridge_mutecaps(struct bridge_softc *sc)
958 struct bridge_iflist *bif;
961 /* Initial bitmask of capabilities to test */
962 mask = BRIDGE_IFCAPS_MASK;
964 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
965 /* Every member must support it or its disabled */
966 mask &= bif->bif_savedcaps;
970 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
971 enabled = bif->bif_ifp->if_capenable;
972 enabled &= ~BRIDGE_IFCAPS_STRIP;
973 /* strip off mask bits and enable them again if allowed */
974 enabled &= ~BRIDGE_IFCAPS_MASK;
977 bridge_set_ifcap(sc, bif, enabled);
985 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set)
987 struct ifnet *ifp = bif->bif_ifp;
989 int error, mask, stuck;
991 BRIDGE_UNLOCK_ASSERT(sc);
993 bzero(&ifr, sizeof(ifr));
994 ifr.ifr_reqcap = set;
996 if (ifp->if_capenable != set) {
997 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
999 if_printf(sc->sc_ifp,
1000 "error setting capabilities on %s: %d\n",
1001 ifp->if_xname, error);
1002 mask = BRIDGE_IFCAPS_MASK | BRIDGE_IFCAPS_STRIP;
1003 stuck = ifp->if_capenable & mask & ~set;
1005 if_printf(sc->sc_ifp,
1006 "can't disable some capabilities on %s: 0x%x\n",
1007 ifp->if_xname, stuck);
1012 * bridge_lookup_member:
1014 * Lookup a bridge member interface.
1016 static struct bridge_iflist *
1017 bridge_lookup_member(struct bridge_softc *sc, const char *name)
1019 struct bridge_iflist *bif;
1022 BRIDGE_LOCK_ASSERT(sc);
1024 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1026 if (strcmp(ifp->if_xname, name) == 0)
1034 * bridge_lookup_member_if:
1036 * Lookup a bridge member interface by ifnet*.
1038 static struct bridge_iflist *
1039 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
1041 struct bridge_iflist *bif;
1043 BRIDGE_LOCK_ASSERT(sc);
1045 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1046 if (bif->bif_ifp == member_ifp)
1054 * bridge_delete_member:
1056 * Delete the specified member interface.
1059 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
1062 struct ifnet *ifs = bif->bif_ifp;
1063 struct ifnet *fif = NULL;
1065 BRIDGE_LOCK_ASSERT(sc);
1067 if (bif->bif_flags & IFBIF_STP)
1068 bstp_disable(&bif->bif_stp);
1070 ifs->if_bridge = NULL;
1072 LIST_REMOVE(bif, bif_next);
1076 * If removing the interface that gave the bridge its mac address, set
1077 * the mac address of the bridge to the address of the next member, or
1078 * to its default address if no members are left.
1080 if (V_bridge_inherit_mac && sc->sc_ifaddr == ifs) {
1081 if (LIST_EMPTY(&sc->sc_iflist)) {
1082 bcopy(&sc->sc_defaddr,
1083 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1084 sc->sc_ifaddr = NULL;
1086 fif = LIST_FIRST(&sc->sc_iflist)->bif_ifp;
1087 bcopy(IF_LLADDR(fif),
1088 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1089 sc->sc_ifaddr = fif;
1091 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
1094 bridge_linkcheck(sc);
1095 bridge_mutecaps(sc); /* recalcuate now this interface is removed */
1096 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
1097 KASSERT(bif->bif_addrcnt == 0,
1098 ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt));
1100 ifs->if_bridge_output = NULL;
1101 ifs->if_bridge_input = NULL;
1102 ifs->if_bridge_linkstate = NULL;
1105 switch (ifs->if_type) {
1109 * Take the interface out of promiscuous mode, but only
1110 * if it was promiscuous in the first place. It might
1111 * not be if we're in the bridge_ioctl_add() error path.
1113 if (ifs->if_flags & IFF_PROMISC)
1114 (void) ifpromisc(ifs, 0);
1122 panic("bridge_delete_member: impossible");
1126 /* reneable any interface capabilities */
1127 bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
1129 bstp_destroy(&bif->bif_stp); /* prepare to free */
1131 free(bif, M_DEVBUF);
1135 * bridge_delete_span:
1137 * Delete the specified span interface.
1140 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1142 BRIDGE_LOCK_ASSERT(sc);
1144 KASSERT(bif->bif_ifp->if_bridge == NULL,
1145 ("%s: not a span interface", __func__));
1147 LIST_REMOVE(bif, bif_next);
1148 free(bif, M_DEVBUF);
1152 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1154 struct ifbreq *req = arg;
1155 struct bridge_iflist *bif = NULL;
1159 ifs = ifunit(req->ifbr_ifsname);
1162 if (ifs->if_ioctl == NULL) /* must be supported */
1165 /* If it's in the span list, it can't be a member. */
1166 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1167 if (ifs == bif->bif_ifp)
1170 if (ifs->if_bridge == sc)
1173 if (ifs->if_bridge != NULL)
1176 switch (ifs->if_type) {
1180 /* permitted interface types */
1188 * Two valid inet6 addresses with link-local scope must not be
1189 * on the parent interface and the member interfaces at the
1190 * same time. This restriction is needed to prevent violation
1191 * of link-local scope zone. Attempts to add a member
1192 * interface which has inet6 addresses when the parent has
1193 * inet6 triggers removal of all inet6 addresses on the member
1197 /* Check if the parent interface has a link-local scope addr. */
1198 if (V_allow_llz_overlap == 0 &&
1199 in6ifa_llaonifp(sc->sc_ifp) != NULL) {
1201 * If any, remove all inet6 addresses from the member
1205 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1206 if (in6ifa_llaonifp(bif->bif_ifp)) {
1208 in6_ifdetach(bif->bif_ifp);
1210 if_printf(sc->sc_ifp,
1211 "IPv6 addresses on %s have been removed "
1212 "before adding it as a member to prevent "
1213 "IPv6 address scope violation.\n",
1214 bif->bif_ifp->if_xname);
1218 if (in6ifa_llaonifp(ifs)) {
1222 if_printf(sc->sc_ifp,
1223 "IPv6 addresses on %s have been removed "
1224 "before adding it as a member to prevent "
1225 "IPv6 address scope violation.\n",
1230 /* Allow the first Ethernet member to define the MTU */
1231 if (LIST_EMPTY(&sc->sc_iflist))
1232 sc->sc_ifp->if_mtu = ifs->if_mtu;
1233 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
1234 if_printf(sc->sc_ifp, "invalid MTU: %u(%s) != %u\n",
1235 ifs->if_mtu, ifs->if_xname, sc->sc_ifp->if_mtu);
1239 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1244 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
1245 bif->bif_savedcaps = ifs->if_capenable;
1248 * Assign the interface's MAC address to the bridge if it's the first
1249 * member and the MAC address of the bridge has not been changed from
1250 * the default randomly generated one.
1252 if (V_bridge_inherit_mac && LIST_EMPTY(&sc->sc_iflist) &&
1253 !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr.octet, ETHER_ADDR_LEN)) {
1254 bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1255 sc->sc_ifaddr = ifs;
1256 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
1259 ifs->if_bridge = sc;
1260 ifs->if_bridge_output = bridge_output;
1261 ifs->if_bridge_input = bridge_input;
1262 ifs->if_bridge_linkstate = bridge_linkstate;
1263 bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
1265 * XXX: XLOCK HERE!?!
1267 * NOTE: insert_***HEAD*** should be safe for the traversals.
1269 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
1271 /* Set interface capabilities to the intersection set of all members */
1272 bridge_mutecaps(sc);
1273 bridge_linkcheck(sc);
1275 /* Place the interface into promiscuous mode */
1276 switch (ifs->if_type) {
1280 error = ifpromisc(ifs, 1);
1286 bridge_delete_member(sc, bif, 0);
1291 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1293 struct ifbreq *req = arg;
1294 struct bridge_iflist *bif;
1296 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1300 bridge_delete_member(sc, bif, 0);
1306 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1308 struct ifbreq *req = arg;
1309 struct bridge_iflist *bif;
1310 struct bstp_port *bp;
1312 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1317 req->ifbr_ifsflags = bif->bif_flags;
1318 req->ifbr_state = bp->bp_state;
1319 req->ifbr_priority = bp->bp_priority;
1320 req->ifbr_path_cost = bp->bp_path_cost;
1321 req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1322 req->ifbr_proto = bp->bp_protover;
1323 req->ifbr_role = bp->bp_role;
1324 req->ifbr_stpflags = bp->bp_flags;
1325 req->ifbr_addrcnt = bif->bif_addrcnt;
1326 req->ifbr_addrmax = bif->bif_addrmax;
1327 req->ifbr_addrexceeded = bif->bif_addrexceeded;
1329 /* Copy STP state options as flags */
1330 if (bp->bp_operedge)
1331 req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
1332 if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
1333 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
1334 if (bp->bp_ptp_link)
1335 req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
1336 if (bp->bp_flags & BSTP_PORT_AUTOPTP)
1337 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
1338 if (bp->bp_flags & BSTP_PORT_ADMEDGE)
1339 req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
1340 if (bp->bp_flags & BSTP_PORT_ADMCOST)
1341 req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
1346 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1348 struct ifbreq *req = arg;
1349 struct bridge_iflist *bif;
1350 struct bstp_port *bp;
1353 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1358 if (req->ifbr_ifsflags & IFBIF_SPAN)
1359 /* SPAN is readonly */
1362 if (req->ifbr_ifsflags & IFBIF_STP) {
1363 if ((bif->bif_flags & IFBIF_STP) == 0) {
1364 error = bstp_enable(&bif->bif_stp);
1369 if ((bif->bif_flags & IFBIF_STP) != 0)
1370 bstp_disable(&bif->bif_stp);
1373 /* Pass on STP flags */
1374 bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
1375 bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
1376 bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
1377 bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
1379 /* Save the bits relating to the bridge */
1380 bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK;
1386 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1388 struct ifbrparam *param = arg;
1390 sc->sc_brtmax = param->ifbrp_csize;
1397 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1399 struct ifbrparam *param = arg;
1401 param->ifbrp_csize = sc->sc_brtmax;
1407 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1409 struct ifbifconf *bifc = arg;
1410 struct bridge_iflist *bif;
1413 int count, buflen, len, error = 0;
1416 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
1418 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1421 buflen = sizeof(breq) * count;
1422 if (bifc->ifbic_len == 0) {
1423 bifc->ifbic_len = buflen;
1427 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1432 len = min(bifc->ifbic_len, buflen);
1433 bzero(&breq, sizeof(breq));
1434 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1435 if (len < sizeof(breq))
1438 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1439 sizeof(breq.ifbr_ifsname));
1440 /* Fill in the ifbreq structure */
1441 error = bridge_ioctl_gifflags(sc, &breq);
1444 memcpy(buf, &breq, sizeof(breq));
1446 buf += sizeof(breq);
1447 len -= sizeof(breq);
1449 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1450 if (len < sizeof(breq))
1453 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1454 sizeof(breq.ifbr_ifsname));
1455 breq.ifbr_ifsflags = bif->bif_flags;
1456 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1457 memcpy(buf, &breq, sizeof(breq));
1459 buf += sizeof(breq);
1460 len -= sizeof(breq);
1464 bifc->ifbic_len = sizeof(breq) * count;
1465 error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len);
1467 free(outbuf, M_TEMP);
1472 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1474 struct ifbaconf *bac = arg;
1475 struct bridge_rtnode *brt;
1476 struct ifbareq bareq;
1478 int count, buflen, len, error = 0;
1480 if (bac->ifbac_len == 0)
1484 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list)
1486 buflen = sizeof(bareq) * count;
1489 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1494 len = min(bac->ifbac_len, buflen);
1495 bzero(&bareq, sizeof(bareq));
1496 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1497 if (len < sizeof(bareq))
1499 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1500 sizeof(bareq.ifba_ifsname));
1501 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1502 bareq.ifba_vlan = brt->brt_vlan;
1503 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1504 time_uptime < brt->brt_expire)
1505 bareq.ifba_expire = brt->brt_expire - time_uptime;
1507 bareq.ifba_expire = 0;
1508 bareq.ifba_flags = brt->brt_flags;
1510 memcpy(buf, &bareq, sizeof(bareq));
1512 buf += sizeof(bareq);
1513 len -= sizeof(bareq);
1517 bac->ifbac_len = sizeof(bareq) * count;
1518 error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len);
1520 free(outbuf, M_TEMP);
1525 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1527 struct ifbareq *req = arg;
1528 struct bridge_iflist *bif;
1531 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1535 error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
1542 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1544 struct ifbrparam *param = arg;
1546 sc->sc_brttimeout = param->ifbrp_ctime;
1551 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1553 struct ifbrparam *param = arg;
1555 param->ifbrp_ctime = sc->sc_brttimeout;
1560 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1562 struct ifbareq *req = arg;
1564 return (bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan));
1568 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1570 struct ifbreq *req = arg;
1572 bridge_rtflush(sc, req->ifbr_ifsflags);
1577 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1579 struct ifbrparam *param = arg;
1580 struct bstp_state *bs = &sc->sc_stp;
1582 param->ifbrp_prio = bs->bs_bridge_priority;
1587 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1589 struct ifbrparam *param = arg;
1591 return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio));
1595 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1597 struct ifbrparam *param = arg;
1598 struct bstp_state *bs = &sc->sc_stp;
1600 param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
1605 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1607 struct ifbrparam *param = arg;
1609 return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime));
1613 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1615 struct ifbrparam *param = arg;
1616 struct bstp_state *bs = &sc->sc_stp;
1618 param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
1623 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1625 struct ifbrparam *param = arg;
1627 return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay));
1631 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1633 struct ifbrparam *param = arg;
1634 struct bstp_state *bs = &sc->sc_stp;
1636 param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
1641 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1643 struct ifbrparam *param = arg;
1645 return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage));
1649 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1651 struct ifbreq *req = arg;
1652 struct bridge_iflist *bif;
1654 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1658 return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority));
1662 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1664 struct ifbreq *req = arg;
1665 struct bridge_iflist *bif;
1667 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1671 return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost));
1675 bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg)
1677 struct ifbreq *req = arg;
1678 struct bridge_iflist *bif;
1680 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1684 bif->bif_addrmax = req->ifbr_addrmax;
1689 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1691 struct ifbreq *req = arg;
1692 struct bridge_iflist *bif = NULL;
1695 ifs = ifunit(req->ifbr_ifsname);
1699 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1700 if (ifs == bif->bif_ifp)
1703 if (ifs->if_bridge != NULL)
1706 switch (ifs->if_type) {
1715 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1720 bif->bif_flags = IFBIF_SPAN;
1722 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1728 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1730 struct ifbreq *req = arg;
1731 struct bridge_iflist *bif;
1734 ifs = ifunit(req->ifbr_ifsname);
1738 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1739 if (ifs == bif->bif_ifp)
1745 bridge_delete_span(sc, bif);
1751 bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg)
1753 struct ifbropreq *req = arg;
1754 struct bstp_state *bs = &sc->sc_stp;
1755 struct bstp_port *root_port;
1757 req->ifbop_maxage = bs->bs_bridge_max_age >> 8;
1758 req->ifbop_hellotime = bs->bs_bridge_htime >> 8;
1759 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;
1761 root_port = bs->bs_root_port;
1762 if (root_port == NULL)
1763 req->ifbop_root_port = 0;
1765 req->ifbop_root_port = root_port->bp_ifp->if_index;
1767 req->ifbop_holdcount = bs->bs_txholdcount;
1768 req->ifbop_priority = bs->bs_bridge_priority;
1769 req->ifbop_protocol = bs->bs_protover;
1770 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;
1771 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id;
1772 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id;
1773 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id;
1774 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;
1775 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;
1781 bridge_ioctl_grte(struct bridge_softc *sc, void *arg)
1783 struct ifbrparam *param = arg;
1785 param->ifbrp_cexceeded = sc->sc_brtexceeded;
1790 bridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg)
1792 struct ifbpstpconf *bifstp = arg;
1793 struct bridge_iflist *bif;
1794 struct bstp_port *bp;
1795 struct ifbpstpreq bpreq;
1797 int count, buflen, len, error = 0;
1800 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1801 if ((bif->bif_flags & IFBIF_STP) != 0)
1805 buflen = sizeof(bpreq) * count;
1806 if (bifstp->ifbpstp_len == 0) {
1807 bifstp->ifbpstp_len = buflen;
1812 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1817 len = min(bifstp->ifbpstp_len, buflen);
1818 bzero(&bpreq, sizeof(bpreq));
1819 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1820 if (len < sizeof(bpreq))
1823 if ((bif->bif_flags & IFBIF_STP) == 0)
1827 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff;
1828 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions;
1829 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost;
1830 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id;
1831 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id;
1832 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id;
1834 memcpy(buf, &bpreq, sizeof(bpreq));
1836 buf += sizeof(bpreq);
1837 len -= sizeof(bpreq);
1841 bifstp->ifbpstp_len = sizeof(bpreq) * count;
1842 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len);
1844 free(outbuf, M_TEMP);
1849 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
1851 struct ifbrparam *param = arg;
1853 return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto));
1857 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
1859 struct ifbrparam *param = arg;
1861 return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc));
1867 * Detach an interface from a bridge. Called when a member
1868 * interface is detaching.
1871 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1873 struct bridge_softc *sc = ifp->if_bridge;
1874 struct bridge_iflist *bif;
1876 if (ifp->if_flags & IFF_RENAMING)
1878 if (V_bridge_cloner == NULL) {
1880 * This detach handler can be called after
1881 * vnet_bridge_uninit(). Just return in that case.
1885 /* Check if the interface is a bridge member */
1889 bif = bridge_lookup_member_if(sc, ifp);
1891 bridge_delete_member(sc, bif, 1);
1897 /* Check if the interface is a span port */
1899 LIST_FOREACH(sc, &V_bridge_list, sc_list) {
1901 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1902 if (ifp == bif->bif_ifp) {
1903 bridge_delete_span(sc, bif);
1909 BRIDGE_LIST_UNLOCK();
1915 * Initialize a bridge interface.
1918 bridge_init(void *xsc)
1920 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1921 struct ifnet *ifp = sc->sc_ifp;
1923 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1927 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1930 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1931 bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */
1939 * Stop the bridge interface.
1942 bridge_stop(struct ifnet *ifp, int disable)
1944 struct bridge_softc *sc = ifp->if_softc;
1946 BRIDGE_LOCK_ASSERT(sc);
1948 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1951 callout_stop(&sc->sc_brcallout);
1952 bstp_stop(&sc->sc_stp);
1954 bridge_rtflush(sc, IFBF_FLUSHDYN);
1956 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1962 * Enqueue a packet on a bridge member interface.
1966 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1972 /* We may be sending a fragment so traverse the mbuf */
1975 m->m_nextpkt = NULL;
1976 len = m->m_pkthdr.len;
1977 mflags = m->m_flags;
1980 * If underlying interface can not do VLAN tag insertion itself
1981 * then attach a packet tag that holds it.
1983 if ((m->m_flags & M_VLANTAG) &&
1984 (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) {
1985 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1988 "unable to prepend VLAN header\n");
1989 if_inc_counter(dst_ifp, IFCOUNTER_OERRORS, 1);
1992 m->m_flags &= ~M_VLANTAG;
1995 M_ASSERTPKTHDR(m); /* We shouldn't transmit mbuf without pkthdr */
1996 if ((err = dst_ifp->if_transmit(dst_ifp, m))) {
1998 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2002 if_inc_counter(sc->sc_ifp, IFCOUNTER_OPACKETS, 1);
2003 if_inc_counter(sc->sc_ifp, IFCOUNTER_OBYTES, len);
2004 if (mflags & M_MCAST)
2005 if_inc_counter(sc->sc_ifp, IFCOUNTER_OMCASTS, 1);
2014 * Receive a queued packet from dummynet and pass it on to the output
2017 * The mbuf has the Ethernet header already attached.
2020 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
2022 struct bridge_softc *sc;
2024 sc = ifp->if_bridge;
2027 * The packet didnt originate from a member interface. This should only
2028 * ever happen if a member interface is removed while packets are
2036 if (PFIL_HOOKED_OUT(V_inet_pfil_head)
2038 || PFIL_HOOKED_OUT(V_inet6_pfil_head)
2041 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
2047 bridge_enqueue(sc, ifp, m);
2053 * Send output from a bridge member interface. This
2054 * performs the bridging function for locally originated
2057 * The mbuf has the Ethernet header already attached. We must
2058 * enqueue or free the mbuf before returning.
2061 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2064 struct ether_header *eh;
2065 struct ifnet *bifp, *dst_if;
2066 struct bridge_softc *sc;
2069 if (m->m_len < ETHER_HDR_LEN) {
2070 m = m_pullup(m, ETHER_HDR_LEN);
2075 eh = mtod(m, struct ether_header *);
2076 sc = ifp->if_bridge;
2077 vlan = VLANTAGOF(m);
2083 * If bridge is down, but the original output interface is up,
2084 * go ahead and send out that interface. Otherwise, the packet
2087 if ((bifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2093 * If the packet is a multicast, or we don't know a better way to
2094 * get there, send to all interfaces.
2096 if (ETHER_IS_MULTICAST(eh->ether_dhost))
2099 dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan);
2100 /* Tap any traffic not passing back out the originating interface */
2102 ETHER_BPF_MTAP(bifp, m);
2103 if (dst_if == NULL) {
2104 struct bridge_iflist *bif;
2106 int error = 0, used = 0;
2110 BRIDGE_LOCK2REF(sc, error);
2116 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
2117 dst_if = bif->bif_ifp;
2119 if (dst_if->if_type == IFT_GIF)
2121 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2125 * If this is not the original output interface,
2126 * and the interface is participating in spanning
2127 * tree, make sure the port is in a state that
2128 * allows forwarding.
2130 if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) &&
2131 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2134 if (LIST_NEXT(bif, bif_next) == NULL) {
2138 mc = m_copypacket(m, M_NOWAIT);
2140 if_inc_counter(bifp, IFCOUNTER_OERRORS, 1);
2145 bridge_enqueue(sc, dst_if, mc);
2155 * XXX Spanning tree consideration here?
2159 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2166 bridge_enqueue(sc, dst_if, m);
2173 * Do output on a bridge.
2177 bridge_transmit(struct ifnet *ifp, struct mbuf *m)
2179 struct bridge_softc *sc;
2180 struct ether_header *eh;
2181 struct ifnet *dst_if;
2186 ETHER_BPF_MTAP(ifp, m);
2188 eh = mtod(m, struct ether_header *);
2191 if (((m->m_flags & (M_BCAST|M_MCAST)) == 0) &&
2192 (dst_if = bridge_rtlookup(sc, eh->ether_dhost, 1)) != NULL) {
2194 error = bridge_enqueue(sc, dst_if, m);
2196 bridge_broadcast(sc, ifp, m, 0);
2202 * The ifp->if_qflush entry point for if_bridge(4) is no-op.
2205 bridge_qflush(struct ifnet *ifp __unused)
2212 * The forwarding function of the bridge.
2214 * NOTE: Releases the lock on return.
2217 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
2220 struct bridge_iflist *dbif;
2221 struct ifnet *src_if, *dst_if, *ifp;
2222 struct ether_header *eh;
2227 src_if = m->m_pkthdr.rcvif;
2230 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2231 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
2232 vlan = VLANTAGOF(m);
2234 if ((sbif->bif_flags & IFBIF_STP) &&
2235 sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2238 eh = mtod(m, struct ether_header *);
2239 dst = eh->ether_dhost;
2241 /* If the interface is learning, record the address. */
2242 if (sbif->bif_flags & IFBIF_LEARNING) {
2243 error = bridge_rtupdate(sc, eh->ether_shost, vlan,
2244 sbif, 0, IFBAF_DYNAMIC);
2246 * If the interface has addresses limits then deny any source
2247 * that is not in the cache.
2249 if (error && sbif->bif_addrmax)
2253 if ((sbif->bif_flags & IFBIF_STP) != 0 &&
2254 sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING)
2258 * At this point, the port either doesn't participate
2259 * in spanning tree or it is in the forwarding state.
2263 * If the packet is unicast, destined for someone on
2264 * "this" side of the bridge, drop it.
2266 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2267 dst_if = bridge_rtlookup(sc, dst, vlan);
2268 if (src_if == dst_if)
2272 * Check if its a reserved multicast address, any address
2273 * listed in 802.1D section 7.12.6 may not be forwarded by the
2275 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
2277 if (dst[0] == 0x01 && dst[1] == 0x80 &&
2278 dst[2] == 0xc2 && dst[3] == 0x00 &&
2279 dst[4] == 0x00 && dst[5] <= 0x0f)
2282 /* ...forward it to all interfaces. */
2283 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
2288 * If we have a destination interface which is a member of our bridge,
2289 * OR this is a unicast packet, push it through the bpf(4) machinery.
2290 * For broadcast or multicast packets, don't bother because it will
2291 * be reinjected into ether_input. We do this before we pass the packets
2292 * through the pfil(9) framework, as it is possible that pfil(9) will
2293 * drop the packet, or possibly modify it, making it difficult to debug
2294 * firewall issues on the bridge.
2296 if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0)
2297 ETHER_BPF_MTAP(ifp, m);
2299 /* run the packet filter */
2300 if (PFIL_HOOKED_IN(V_inet_pfil_head)
2302 || PFIL_HOOKED_IN(V_inet6_pfil_head)
2306 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2313 if (dst_if == NULL) {
2314 bridge_broadcast(sc, src_if, m, 1);
2319 * At this point, we're dealing with a unicast frame
2320 * going to a different interface.
2322 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2325 dbif = bridge_lookup_member_if(sc, dst_if);
2327 /* Not a member of the bridge (anymore?) */
2330 /* Private segments can not talk to each other */
2331 if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)
2334 if ((dbif->bif_flags & IFBIF_STP) &&
2335 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2340 if (PFIL_HOOKED_OUT(V_inet_pfil_head)
2342 || PFIL_HOOKED_OUT(V_inet6_pfil_head)
2345 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2351 bridge_enqueue(sc, dst_if, m);
2362 * Receive input from a member interface. Queue the packet for
2363 * bridging if it is not for us.
2365 static struct mbuf *
2366 bridge_input(struct ifnet *ifp, struct mbuf *m)
2368 struct bridge_softc *sc = ifp->if_bridge;
2369 struct bridge_iflist *bif, *bif2;
2371 struct ether_header *eh;
2372 struct mbuf *mc, *mc2;
2376 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2380 vlan = VLANTAGOF(m);
2383 * Implement support for bridge monitoring. If this flag has been
2384 * set on this interface, discard the packet once we push it through
2385 * the bpf(4) machinery, but before we do, increment the byte and
2386 * packet counters associated with this interface.
2388 if ((bifp->if_flags & IFF_MONITOR) != 0) {
2389 m->m_pkthdr.rcvif = bifp;
2390 ETHER_BPF_MTAP(bifp, m);
2391 if_inc_counter(bifp, IFCOUNTER_IPACKETS, 1);
2392 if_inc_counter(bifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
2397 bif = bridge_lookup_member_if(sc, ifp);
2403 eh = mtod(m, struct ether_header *);
2407 if (m->m_flags & (M_BCAST|M_MCAST)) {
2408 /* Tap off 802.1D packets; they do not get forwarded. */
2409 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2410 ETHER_ADDR_LEN) == 0) {
2411 bstp_input(&bif->bif_stp, ifp, m); /* consumes mbuf */
2416 if ((bif->bif_flags & IFBIF_STP) &&
2417 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2423 * Make a deep copy of the packet and enqueue the copy
2424 * for bridge processing; return the original packet for
2427 mc = m_dup(m, M_NOWAIT);
2433 /* Perform the bridge forwarding function with the copy. */
2434 bridge_forward(sc, bif, mc);
2437 * Reinject the mbuf as arriving on the bridge so we have a
2438 * chance at claiming multicast packets. We can not loop back
2439 * here from ether_input as a bridge is never a member of a
2442 KASSERT(bifp->if_bridge == NULL,
2443 ("loop created in bridge_input"));
2444 mc2 = m_dup(m, M_NOWAIT);
2446 /* Keep the layer3 header aligned */
2447 int i = min(mc2->m_pkthdr.len, max_protohdr);
2448 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2451 mc2->m_pkthdr.rcvif = bifp;
2452 (*bifp->if_input)(bifp, mc2);
2455 /* Return the original packet for local processing. */
2459 if ((bif->bif_flags & IFBIF_STP) &&
2460 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2465 #if (defined(INET) || defined(INET6))
2466 # define OR_CARP_CHECK_WE_ARE_DST(iface) \
2467 || ((iface)->if_carp \
2468 && (*carp_forus_p)((iface), eh->ether_dhost))
2469 # define OR_CARP_CHECK_WE_ARE_SRC(iface) \
2470 || ((iface)->if_carp \
2471 && (*carp_forus_p)((iface), eh->ether_shost))
2473 # define OR_CARP_CHECK_WE_ARE_DST(iface)
2474 # define OR_CARP_CHECK_WE_ARE_SRC(iface)
2478 # define OR_PFIL_HOOKED_INET6 \
2479 || PFIL_HOOKED_IN(V_inet6_pfil_head)
2481 # define OR_PFIL_HOOKED_INET6
2484 #define GRAB_OUR_PACKETS(iface) \
2485 if ((iface)->if_type == IFT_GIF) \
2487 /* It is destined for us. */ \
2488 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, ETHER_ADDR_LEN) == 0 \
2489 OR_CARP_CHECK_WE_ARE_DST((iface)) \
2491 if (bif->bif_flags & IFBIF_LEARNING) { \
2492 error = bridge_rtupdate(sc, eh->ether_shost, \
2493 vlan, bif, 0, IFBAF_DYNAMIC); \
2494 if (error && bif->bif_addrmax) { \
2495 BRIDGE_UNLOCK(sc); \
2500 m->m_pkthdr.rcvif = iface; \
2501 if ((iface) == ifp) { \
2502 /* Skip bridge processing... src == dest */ \
2503 BRIDGE_UNLOCK(sc); \
2506 /* It's passing over or to the bridge, locally. */ \
2507 ETHER_BPF_MTAP(bifp, m); \
2508 if_inc_counter(bifp, IFCOUNTER_IPACKETS, 1); \
2509 if_inc_counter(bifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); \
2510 /* Filter on the physical interface. */ \
2511 if (V_pfil_local_phys && (PFIL_HOOKED_IN(V_inet_pfil_head) \
2512 OR_PFIL_HOOKED_INET6)) { \
2513 if (bridge_pfil(&m, NULL, ifp, \
2514 PFIL_IN) != 0 || m == NULL) { \
2515 BRIDGE_UNLOCK(sc); \
2519 if ((iface) != bifp) \
2520 ETHER_BPF_MTAP(iface, m); \
2521 BRIDGE_UNLOCK(sc); \
2525 /* We just received a packet that we sent out. */ \
2526 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, ETHER_ADDR_LEN) == 0 \
2527 OR_CARP_CHECK_WE_ARE_SRC((iface)) \
2529 BRIDGE_UNLOCK(sc); \
2535 * Unicast. Make sure it's not for the bridge.
2537 do { GRAB_OUR_PACKETS(bifp) } while (0);
2540 * Give a chance for ifp at first priority. This will help when the
2541 * packet comes through the interface like VLAN's with the same MACs
2542 * on several interfaces from the same bridge. This also will save
2543 * some CPU cycles in case the destination interface and the input
2544 * interface (eq ifp) are the same.
2546 do { GRAB_OUR_PACKETS(ifp) } while (0);
2548 /* Now check the all bridge members. */
2549 LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) {
2550 GRAB_OUR_PACKETS(bif2->bif_ifp)
2553 #undef OR_CARP_CHECK_WE_ARE_DST
2554 #undef OR_CARP_CHECK_WE_ARE_SRC
2555 #undef OR_PFIL_HOOKED_INET6
2556 #undef GRAB_OUR_PACKETS
2558 /* Perform the bridge forwarding function. */
2559 bridge_forward(sc, bif, m);
2567 * Send a frame to all interfaces that are members of
2568 * the bridge, except for the one on which the packet
2571 * NOTE: Releases the lock on return.
2574 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2575 struct mbuf *m, int runfilt)
2577 struct bridge_iflist *dbif, *sbif;
2579 struct ifnet *dst_if;
2580 int error = 0, used = 0, i;
2582 sbif = bridge_lookup_member_if(sc, src_if);
2584 BRIDGE_LOCK2REF(sc, error);
2590 /* Filter on the bridge interface before broadcasting */
2591 if (runfilt && (PFIL_HOOKED_OUT(V_inet_pfil_head)
2593 || PFIL_HOOKED_OUT(V_inet6_pfil_head)
2596 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
2602 LIST_FOREACH(dbif, &sc->sc_iflist, bif_next) {
2603 dst_if = dbif->bif_ifp;
2604 if (dst_if == src_if)
2607 /* Private segments can not talk to each other */
2608 if (sbif && (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE))
2611 if ((dbif->bif_flags & IFBIF_STP) &&
2612 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2615 if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 &&
2616 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2619 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2622 if (LIST_NEXT(dbif, bif_next) == NULL) {
2626 mc = m_dup(m, M_NOWAIT);
2628 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2634 * Filter on the output interface. Pass a NULL bridge interface
2635 * pointer so we do not redundantly filter on the bridge for
2636 * each interface we broadcast on.
2638 if (runfilt && (PFIL_HOOKED_OUT(V_inet_pfil_head)
2640 || PFIL_HOOKED_OUT(V_inet6_pfil_head)
2644 /* Keep the layer3 header aligned */
2645 i = min(mc->m_pkthdr.len, max_protohdr);
2646 mc = m_copyup(mc, i, ETHER_ALIGN);
2648 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2652 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2658 bridge_enqueue(sc, dst_if, mc);
2670 * Duplicate a packet out one or more interfaces that are in span mode,
2671 * the original mbuf is unmodified.
2674 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2676 struct bridge_iflist *bif;
2677 struct ifnet *dst_if;
2680 if (LIST_EMPTY(&sc->sc_spanlist))
2683 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2684 dst_if = bif->bif_ifp;
2686 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2689 mc = m_copypacket(m, M_NOWAIT);
2691 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2695 bridge_enqueue(sc, dst_if, mc);
2702 * Add a bridge routing entry.
2705 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan,
2706 struct bridge_iflist *bif, int setflags, uint8_t flags)
2708 struct bridge_rtnode *brt;
2711 BRIDGE_LOCK_ASSERT(sc);
2713 /* Check the source address is valid and not multicast. */
2714 if (ETHER_IS_MULTICAST(dst) ||
2715 (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
2716 dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0)
2719 /* 802.1p frames map to vlan 1 */
2724 * A route for this destination might already exist. If so,
2725 * update it, otherwise create a new one.
2727 if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) {
2728 if (sc->sc_brtcnt >= sc->sc_brtmax) {
2729 sc->sc_brtexceeded++;
2732 /* Check per interface address limits (if enabled) */
2733 if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) {
2734 bif->bif_addrexceeded++;
2739 * Allocate a new bridge forwarding node, and
2740 * initialize the expiration time and Ethernet
2743 brt = uma_zalloc(V_bridge_rtnode_zone, M_NOWAIT | M_ZERO);
2747 if (bif->bif_flags & IFBIF_STICKY)
2748 brt->brt_flags = IFBAF_STICKY;
2750 brt->brt_flags = IFBAF_DYNAMIC;
2752 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2753 brt->brt_vlan = vlan;
2755 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2756 uma_zfree(V_bridge_rtnode_zone, brt);
2763 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2764 brt->brt_dst != bif) {
2765 brt->brt_dst->bif_addrcnt--;
2767 brt->brt_dst->bif_addrcnt++;
2770 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2771 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2773 brt->brt_flags = flags;
2781 * Lookup the destination interface for an address.
2783 static struct ifnet *
2784 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2786 struct bridge_rtnode *brt;
2788 BRIDGE_LOCK_ASSERT(sc);
2790 if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL)
2793 return (brt->brt_ifp);
2799 * Trim the routine table so that we have a number
2800 * of routing entries less than or equal to the
2804 bridge_rttrim(struct bridge_softc *sc)
2806 struct bridge_rtnode *brt, *nbrt;
2808 BRIDGE_LOCK_ASSERT(sc);
2810 /* Make sure we actually need to do this. */
2811 if (sc->sc_brtcnt <= sc->sc_brtmax)
2814 /* Force an aging cycle; this might trim enough addresses. */
2816 if (sc->sc_brtcnt <= sc->sc_brtmax)
2819 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2820 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2821 bridge_rtnode_destroy(sc, brt);
2822 if (sc->sc_brtcnt <= sc->sc_brtmax)
2831 * Aging timer for the bridge.
2834 bridge_timer(void *arg)
2836 struct bridge_softc *sc = arg;
2838 BRIDGE_LOCK_ASSERT(sc);
2840 /* Destruction of rtnodes requires a proper vnet context */
2841 CURVNET_SET(sc->sc_ifp->if_vnet);
2844 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
2845 callout_reset(&sc->sc_brcallout,
2846 bridge_rtable_prune_period * hz, bridge_timer, sc);
2853 * Perform an aging cycle.
2856 bridge_rtage(struct bridge_softc *sc)
2858 struct bridge_rtnode *brt, *nbrt;
2860 BRIDGE_LOCK_ASSERT(sc);
2862 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2863 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2864 if (time_uptime >= brt->brt_expire)
2865 bridge_rtnode_destroy(sc, brt);
2873 * Remove all dynamic addresses from the bridge.
2876 bridge_rtflush(struct bridge_softc *sc, int full)
2878 struct bridge_rtnode *brt, *nbrt;
2880 BRIDGE_LOCK_ASSERT(sc);
2882 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2883 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2884 bridge_rtnode_destroy(sc, brt);
2891 * Remove an address from the table.
2894 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2896 struct bridge_rtnode *brt;
2899 BRIDGE_LOCK_ASSERT(sc);
2902 * If vlan is zero then we want to delete for all vlans so the lookup
2903 * may return more than one.
2905 while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) {
2906 bridge_rtnode_destroy(sc, brt);
2910 return (found ? 0 : ENOENT);
2916 * Delete routes to a speicifc member interface.
2919 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2921 struct bridge_rtnode *brt, *nbrt;
2923 BRIDGE_LOCK_ASSERT(sc);
2925 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2926 if (brt->brt_ifp == ifp && (full ||
2927 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2928 bridge_rtnode_destroy(sc, brt);
2933 * bridge_rtable_init:
2935 * Initialize the route table for this bridge.
2938 bridge_rtable_init(struct bridge_softc *sc)
2942 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2943 M_DEVBUF, M_WAITOK);
2945 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2946 LIST_INIT(&sc->sc_rthash[i]);
2948 sc->sc_rthash_key = arc4random();
2949 LIST_INIT(&sc->sc_rtlist);
2953 * bridge_rtable_fini:
2955 * Deconstruct the route table for this bridge.
2958 bridge_rtable_fini(struct bridge_softc *sc)
2961 KASSERT(sc->sc_brtcnt == 0,
2962 ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt));
2963 free(sc->sc_rthash, M_DEVBUF);
2967 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2968 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2970 #define mix(a, b, c) \
2972 a -= b; a -= c; a ^= (c >> 13); \
2973 b -= c; b -= a; b ^= (a << 8); \
2974 c -= a; c -= b; c ^= (b >> 13); \
2975 a -= b; a -= c; a ^= (c >> 12); \
2976 b -= c; b -= a; b ^= (a << 16); \
2977 c -= a; c -= b; c ^= (b >> 5); \
2978 a -= b; a -= c; a ^= (c >> 3); \
2979 b -= c; b -= a; b ^= (a << 10); \
2980 c -= a; c -= b; c ^= (b >> 15); \
2981 } while (/*CONSTCOND*/0)
2983 static __inline uint32_t
2984 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2986 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2997 return (c & BRIDGE_RTHASH_MASK);
3003 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
3007 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
3008 d = ((int)a[i]) - ((int)b[i]);
3015 * bridge_rtnode_lookup:
3017 * Look up a bridge route node for the specified destination. Compare the
3018 * vlan id or if zero then just return the first match.
3020 static struct bridge_rtnode *
3021 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
3023 struct bridge_rtnode *brt;
3027 BRIDGE_LOCK_ASSERT(sc);
3029 hash = bridge_rthash(sc, addr);
3030 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
3031 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
3032 if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0))
3042 * bridge_rtnode_insert:
3044 * Insert the specified bridge node into the route table. We
3045 * assume the entry is not already in the table.
3048 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3050 struct bridge_rtnode *lbrt;
3054 BRIDGE_LOCK_ASSERT(sc);
3056 hash = bridge_rthash(sc, brt->brt_addr);
3058 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
3060 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
3065 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
3066 if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan)
3069 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3072 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
3073 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3076 lbrt = LIST_NEXT(lbrt, brt_hash);
3077 } while (lbrt != NULL);
3080 panic("bridge_rtnode_insert: impossible");
3084 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
3091 * bridge_rtnode_destroy:
3093 * Destroy a bridge rtnode.
3096 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3098 BRIDGE_LOCK_ASSERT(sc);
3100 LIST_REMOVE(brt, brt_hash);
3102 LIST_REMOVE(brt, brt_list);
3104 brt->brt_dst->bif_addrcnt--;
3105 uma_zfree(V_bridge_rtnode_zone, brt);
3109 * bridge_rtable_expire:
3111 * Set the expiry time for all routes on an interface.
3114 bridge_rtable_expire(struct ifnet *ifp, int age)
3116 struct bridge_softc *sc = ifp->if_bridge;
3117 struct bridge_rtnode *brt;
3119 CURVNET_SET(ifp->if_vnet);
3123 * If the age is zero then flush, otherwise set all the expiry times to
3124 * age for the interface
3127 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
3129 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
3130 /* Cap the expiry time to 'age' */
3131 if (brt->brt_ifp == ifp &&
3132 brt->brt_expire > time_uptime + age &&
3133 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
3134 brt->brt_expire = time_uptime + age;
3142 * bridge_state_change:
3144 * Callback from the bridgestp code when a port changes states.
3147 bridge_state_change(struct ifnet *ifp, int state)
3149 struct bridge_softc *sc = ifp->if_bridge;
3150 static const char *stpstates[] = {
3159 CURVNET_SET(ifp->if_vnet);
3161 log(LOG_NOTICE, "%s: state changed to %s on %s\n",
3162 sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname);
3167 * Send bridge packets through pfil if they are one of the types pfil can deal
3168 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3169 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3173 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3175 int snap, error, i, hlen;
3176 struct ether_header *eh1, eh2;
3179 u_int16_t ether_type;
3183 error = -1; /* Default error if not error == 0 */
3186 /* we may return with the IP fields swapped, ensure its not shared */
3187 KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
3190 if (V_pfil_bridge == 0 && V_pfil_member == 0 && V_pfil_ipfw == 0)
3191 return (0); /* filtering is disabled */
3193 i = min((*mp)->m_pkthdr.len, max_protohdr);
3194 if ((*mp)->m_len < i) {
3195 *mp = m_pullup(*mp, i);
3197 printf("%s: m_pullup failed\n", __func__);
3202 eh1 = mtod(*mp, struct ether_header *);
3203 ether_type = ntohs(eh1->ether_type);
3206 * Check for SNAP/LLC.
3208 if (ether_type < ETHERMTU) {
3209 struct llc *llc2 = (struct llc *)(eh1 + 1);
3211 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3212 llc2->llc_dsap == LLC_SNAP_LSAP &&
3213 llc2->llc_ssap == LLC_SNAP_LSAP &&
3214 llc2->llc_control == LLC_UI) {
3215 ether_type = htons(llc2->llc_un.type_snap.ether_type);
3221 * If we're trying to filter bridge traffic, don't look at anything
3222 * other than IP and ARP traffic. If the filter doesn't understand
3223 * IPv6, don't allow IPv6 through the bridge either. This is lame
3224 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3225 * but of course we don't have an AppleTalk filter to begin with.
3226 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3229 switch (ether_type) {
3231 case ETHERTYPE_REVARP:
3232 if (V_pfil_ipfw_arp == 0)
3233 return (0); /* Automatically pass */
3238 case ETHERTYPE_IPV6:
3243 * Check to see if the user wants to pass non-ip
3244 * packets, these will not be checked by pfil(9) and
3245 * passed unconditionally so the default is to drop.
3251 /* Run the packet through pfil before stripping link headers */
3252 if (PFIL_HOOKED_OUT(V_link_pfil_head) && V_pfil_ipfw != 0 &&
3253 dir == PFIL_OUT && ifp != NULL) {
3254 switch (pfil_run_hooks(V_link_pfil_head, mp, ifp, dir, NULL)) {
3262 /* Strip off the Ethernet header and keep a copy. */
3263 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3264 m_adj(*mp, ETHER_HDR_LEN);
3266 /* Strip off snap header, if present */
3268 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3269 m_adj(*mp, sizeof(struct llc));
3273 * Check the IP header for alignment and errors
3275 if (dir == PFIL_IN) {
3276 switch (ether_type) {
3278 error = bridge_ip_checkbasic(mp);
3281 case ETHERTYPE_IPV6:
3282 error = bridge_ip6_checkbasic(mp);
3295 * Run the packet through pfil
3298 switch (ether_type) {
3301 * Run pfil on the member interface and the bridge, both can
3302 * be skipped by clearing pfil_member or pfil_bridge.
3305 * in_if -> bridge_if -> out_if
3307 if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL && (rv =
3308 pfil_run_hooks(V_inet_pfil_head, mp, bifp, dir, NULL)) !=
3312 if (V_pfil_member && ifp != NULL && (rv =
3313 pfil_run_hooks(V_inet_pfil_head, mp, ifp, dir, NULL)) !=
3317 if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL && (rv =
3318 pfil_run_hooks(V_inet_pfil_head, mp, bifp, dir, NULL)) !=
3322 /* check if we need to fragment the packet */
3323 /* bridge_fragment generates a mbuf chain of packets */
3324 /* that already include eth headers */
3325 if (V_pfil_member && ifp != NULL && dir == PFIL_OUT) {
3326 i = (*mp)->m_pkthdr.len;
3327 if (i > ifp->if_mtu) {
3328 error = bridge_fragment(ifp, mp, &eh2, snap,
3334 /* Recalculate the ip checksum. */
3335 ip = mtod(*mp, struct ip *);
3336 hlen = ip->ip_hl << 2;
3337 if (hlen < sizeof(struct ip))
3339 if (hlen > (*mp)->m_len) {
3340 if ((*mp = m_pullup(*mp, hlen)) == NULL)
3342 ip = mtod(*mp, struct ip *);
3347 if (hlen == sizeof(struct ip))
3348 ip->ip_sum = in_cksum_hdr(ip);
3350 ip->ip_sum = in_cksum(*mp, hlen);
3354 case ETHERTYPE_IPV6:
3355 if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL && (rv =
3356 pfil_run_hooks(V_inet6_pfil_head, mp, bifp, dir, NULL)) !=
3360 if (V_pfil_member && ifp != NULL && (rv =
3361 pfil_run_hooks(V_inet6_pfil_head, mp, ifp, dir, NULL)) !=
3365 if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL && (rv =
3366 pfil_run_hooks(V_inet6_pfil_head, mp, bifp, dir, NULL)) !=
3385 * Finally, put everything back the way it was and return
3388 M_PREPEND(*mp, sizeof(struct llc), M_NOWAIT);
3391 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3394 M_PREPEND(*mp, ETHER_HDR_LEN, M_NOWAIT);
3397 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3408 * Perform basic checks on header size since
3409 * pfil assumes ip_input has already processed
3410 * it for it. Cut-and-pasted from ip_input.c.
3411 * Given how simple the IPv6 version is,
3412 * does the IPv4 version really need to be
3415 * XXX Should we update ipstat here, or not?
3416 * XXX Right now we update ipstat but not
3420 bridge_ip_checkbasic(struct mbuf **mp)
3422 struct mbuf *m = *mp;
3430 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3431 if ((m = m_copyup(m, sizeof(struct ip),
3432 (max_linkhdr + 3) & ~3)) == NULL) {
3433 /* XXXJRT new stat, please */
3434 KMOD_IPSTAT_INC(ips_toosmall);
3437 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
3438 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3439 KMOD_IPSTAT_INC(ips_toosmall);
3443 ip = mtod(m, struct ip *);
3444 if (ip == NULL) goto bad;
3446 if (ip->ip_v != IPVERSION) {
3447 KMOD_IPSTAT_INC(ips_badvers);
3450 hlen = ip->ip_hl << 2;
3451 if (hlen < sizeof(struct ip)) { /* minimum header length */
3452 KMOD_IPSTAT_INC(ips_badhlen);
3455 if (hlen > m->m_len) {
3456 if ((m = m_pullup(m, hlen)) == NULL) {
3457 KMOD_IPSTAT_INC(ips_badhlen);
3460 ip = mtod(m, struct ip *);
3461 if (ip == NULL) goto bad;
3464 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3465 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3467 if (hlen == sizeof(struct ip)) {
3468 sum = in_cksum_hdr(ip);
3470 sum = in_cksum(m, hlen);
3474 KMOD_IPSTAT_INC(ips_badsum);
3478 /* Retrieve the packet length. */
3479 len = ntohs(ip->ip_len);
3482 * Check for additional length bogosity
3485 KMOD_IPSTAT_INC(ips_badlen);
3490 * Check that the amount of data in the buffers
3491 * is as at least much as the IP header would have us expect.
3492 * Drop packet if shorter than we expect.
3494 if (m->m_pkthdr.len < len) {
3495 KMOD_IPSTAT_INC(ips_tooshort);
3499 /* Checks out, proceed */
3510 * Same as above, but for IPv6.
3511 * Cut-and-pasted from ip6_input.c.
3512 * XXX Should we update ip6stat, or not?
3515 bridge_ip6_checkbasic(struct mbuf **mp)
3517 struct mbuf *m = *mp;
3518 struct ip6_hdr *ip6;
3521 * If the IPv6 header is not aligned, slurp it up into a new
3522 * mbuf with space for link headers, in the event we forward
3523 * it. Otherwise, if it is aligned, make sure the entire base
3524 * IPv6 header is in the first mbuf of the chain.
3526 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3527 struct ifnet *inifp = m->m_pkthdr.rcvif;
3528 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3529 (max_linkhdr + 3) & ~3)) == NULL) {
3530 /* XXXJRT new stat, please */
3531 IP6STAT_INC(ip6s_toosmall);
3532 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3535 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3536 struct ifnet *inifp = m->m_pkthdr.rcvif;
3537 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3538 IP6STAT_INC(ip6s_toosmall);
3539 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3544 ip6 = mtod(m, struct ip6_hdr *);
3546 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3547 IP6STAT_INC(ip6s_badvers);
3548 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3552 /* Checks out, proceed */
3565 * Fragment mbuf chain in multiple packets and prepend ethernet header.
3568 bridge_fragment(struct ifnet *ifp, struct mbuf **mp, struct ether_header *eh,
3569 int snap, struct llc *llc)
3571 struct mbuf *m = *mp, *nextpkt = NULL, *mprev = NULL, *mcur = NULL;
3575 if (m->m_len < sizeof(struct ip) &&
3576 (m = m_pullup(m, sizeof(struct ip))) == NULL)
3578 ip = mtod(m, struct ip *);
3580 m->m_pkthdr.csum_flags |= CSUM_IP;
3581 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist);
3586 * Walk the chain and re-add the Ethernet header for
3589 for (mcur = m; mcur; mcur = mcur->m_nextpkt) {
3590 nextpkt = mcur->m_nextpkt;
3591 mcur->m_nextpkt = NULL;
3593 M_PREPEND(mcur, sizeof(struct llc), M_NOWAIT);
3597 mprev->m_nextpkt = nextpkt;
3600 bcopy(llc, mtod(mcur, caddr_t),sizeof(struct llc));
3603 M_PREPEND(mcur, ETHER_HDR_LEN, M_NOWAIT);
3607 mprev->m_nextpkt = nextpkt;
3610 bcopy(eh, mtod(mcur, caddr_t), ETHER_HDR_LEN);
3613 * The previous two M_PREPEND could have inserted one or two
3614 * mbufs in front so we have to update the previous packet's
3617 mcur->m_nextpkt = nextpkt;
3619 mprev->m_nextpkt = mcur;
3621 /* The first mbuf in the original chain needs to be
3628 KMOD_IPSTAT_INC(ips_fragmented);
3632 for (mcur = *mp; mcur; mcur = m) { /* droping the full packet chain */
3633 m = mcur->m_nextpkt;
3640 bridge_linkstate(struct ifnet *ifp)
3642 struct bridge_softc *sc = ifp->if_bridge;
3643 struct bridge_iflist *bif;
3646 bif = bridge_lookup_member_if(sc, ifp);
3651 bridge_linkcheck(sc);
3654 bstp_linkstate(&bif->bif_stp);
3658 bridge_linkcheck(struct bridge_softc *sc)
3660 struct bridge_iflist *bif;
3661 int new_link, hasls;
3663 BRIDGE_LOCK_ASSERT(sc);
3664 new_link = LINK_STATE_DOWN;
3666 /* Our link is considered up if at least one of our ports is active */
3667 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
3668 if (bif->bif_ifp->if_capabilities & IFCAP_LINKSTATE)
3670 if (bif->bif_ifp->if_link_state == LINK_STATE_UP) {
3671 new_link = LINK_STATE_UP;
3675 if (!LIST_EMPTY(&sc->sc_iflist) && !hasls) {
3676 /* If no interfaces support link-state then we default to up */
3677 new_link = LINK_STATE_UP;
3679 if_link_state_change(sc->sc_ifp, new_link);