1 /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ */
4 * Copyright 2001 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
55 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
60 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
63 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
67 * Network interface bridge support.
71 * - Currently only supports Ethernet-like interfaces (Ethernet,
72 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
73 * to bridge other types of interfaces (FDDI-FDDI, and maybe
74 * consider heterogenous bridges).
77 #include <sys/cdefs.h>
78 __FBSDID("$FreeBSD$");
81 #include "opt_inet6.h"
83 #include <sys/param.h>
84 #include <sys/eventhandler.h>
86 #include <sys/malloc.h>
87 #include <sys/protosw.h>
88 #include <sys/systm.h>
91 #include <sys/socket.h> /* for net/if.h */
92 #include <sys/sockio.h>
93 #include <sys/ctype.h> /* string functions */
94 #include <sys/kernel.h>
95 #include <sys/random.h>
96 #include <sys/syslog.h>
97 #include <sys/sysctl.h>
99 #include <sys/module.h>
100 #include <sys/priv.h>
101 #include <sys/proc.h>
102 #include <sys/lock.h>
103 #include <sys/mutex.h>
107 #include <net/if_clone.h>
108 #include <net/if_dl.h>
109 #include <net/if_types.h>
110 #include <net/if_var.h>
111 #include <net/pfil.h>
112 #include <net/vnet.h>
114 #include <netinet/in.h>
115 #include <netinet/in_systm.h>
116 #include <netinet/in_var.h>
117 #include <netinet/ip.h>
118 #include <netinet/ip_var.h>
120 #include <netinet/ip6.h>
121 #include <netinet6/ip6_var.h>
122 #include <netinet6/in6_ifattach.h>
124 #if defined(INET) || defined(INET6)
125 #include <netinet/ip_carp.h>
127 #include <machine/in_cksum.h>
128 #include <netinet/if_ether.h>
129 #include <net/bridgestp.h>
130 #include <net/if_bridgevar.h>
131 #include <net/if_llc.h>
132 #include <net/if_vlan_var.h>
134 #include <net/route.h>
137 * Size of the route hash table. Must be a power of two.
139 #ifndef BRIDGE_RTHASH_SIZE
140 #define BRIDGE_RTHASH_SIZE 1024
143 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
146 * Default maximum number of addresses to cache.
148 #ifndef BRIDGE_RTABLE_MAX
149 #define BRIDGE_RTABLE_MAX 2000
153 * Timeout (in seconds) for entries learned dynamically.
155 #ifndef BRIDGE_RTABLE_TIMEOUT
156 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
160 * Number of seconds between walks of the route list.
162 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
163 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
167 * List of capabilities to possibly mask on the member interface.
169 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM)
172 * List of capabilities to strip
174 #define BRIDGE_IFCAPS_STRIP IFCAP_LRO
177 * Bridge interface list entry.
179 struct bridge_iflist {
180 LIST_ENTRY(bridge_iflist) bif_next;
181 struct ifnet *bif_ifp; /* member if */
182 struct bstp_port bif_stp; /* STP state */
183 uint32_t bif_flags; /* member if flags */
184 int bif_savedcaps; /* saved capabilities */
185 uint32_t bif_addrmax; /* max # of addresses */
186 uint32_t bif_addrcnt; /* cur. # of addresses */
187 uint32_t bif_addrexceeded;/* # of address violations */
193 struct bridge_rtnode {
194 LIST_ENTRY(bridge_rtnode) brt_hash; /* hash table linkage */
195 LIST_ENTRY(bridge_rtnode) brt_list; /* list linkage */
196 struct bridge_iflist *brt_dst; /* destination if */
197 unsigned long brt_expire; /* expiration time */
198 uint8_t brt_flags; /* address flags */
199 uint8_t brt_addr[ETHER_ADDR_LEN];
200 uint16_t brt_vlan; /* vlan id */
202 #define brt_ifp brt_dst->bif_ifp
205 * Software state for each bridge.
207 struct bridge_softc {
208 struct ifnet *sc_ifp; /* make this an interface */
209 LIST_ENTRY(bridge_softc) sc_list;
212 uint32_t sc_brtmax; /* max # of addresses */
213 uint32_t sc_brtcnt; /* cur. # of addresses */
214 uint32_t sc_brttimeout; /* rt timeout in seconds */
215 struct callout sc_brcallout; /* bridge callout */
216 uint32_t sc_iflist_ref; /* refcount for sc_iflist */
217 uint32_t sc_iflist_xcnt; /* refcount for sc_iflist */
218 LIST_HEAD(, bridge_iflist) sc_iflist; /* member interface list */
219 LIST_HEAD(, bridge_rtnode) *sc_rthash; /* our forwarding table */
220 LIST_HEAD(, bridge_rtnode) sc_rtlist; /* list version of above */
221 uint32_t sc_rthash_key; /* key for hash */
222 LIST_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */
223 struct bstp_state sc_stp; /* STP state */
224 uint32_t sc_brtexceeded; /* # of cache drops */
225 struct ifnet *sc_ifaddr; /* member mac copied from */
226 u_char sc_defaddr[6]; /* Default MAC address */
229 static VNET_DEFINE(struct mtx, bridge_list_mtx);
230 #define V_bridge_list_mtx VNET(bridge_list_mtx)
231 eventhandler_tag bridge_detach_cookie = NULL;
233 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
235 uma_zone_t bridge_rtnode_zone;
237 static int bridge_clone_create(struct if_clone *, int, caddr_t);
238 static void bridge_clone_destroy(struct ifnet *);
240 static int bridge_ioctl(struct ifnet *, u_long, caddr_t);
241 static void bridge_mutecaps(struct bridge_softc *);
242 static void bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *,
244 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
245 static void bridge_init(void *);
246 static void bridge_dummynet(struct mbuf *, struct ifnet *);
247 static void bridge_stop(struct ifnet *, int);
248 static int bridge_transmit(struct ifnet *, struct mbuf *);
249 static void bridge_qflush(struct ifnet *);
250 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
251 static int bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
253 static int bridge_enqueue(struct bridge_softc *, struct ifnet *,
255 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
257 static void bridge_forward(struct bridge_softc *, struct bridge_iflist *,
260 static void bridge_timer(void *);
262 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
264 static void bridge_span(struct bridge_softc *, struct mbuf *);
266 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
267 uint16_t, struct bridge_iflist *, int, uint8_t);
268 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *,
270 static void bridge_rttrim(struct bridge_softc *);
271 static void bridge_rtage(struct bridge_softc *);
272 static void bridge_rtflush(struct bridge_softc *, int);
273 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *,
276 static void bridge_rtable_init(struct bridge_softc *);
277 static void bridge_rtable_fini(struct bridge_softc *);
279 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
280 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
281 const uint8_t *, uint16_t);
282 static int bridge_rtnode_insert(struct bridge_softc *,
283 struct bridge_rtnode *);
284 static void bridge_rtnode_destroy(struct bridge_softc *,
285 struct bridge_rtnode *);
286 static void bridge_rtable_expire(struct ifnet *, int);
287 static void bridge_state_change(struct ifnet *, int);
289 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
291 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
293 static void bridge_delete_member(struct bridge_softc *,
294 struct bridge_iflist *, int);
295 static void bridge_delete_span(struct bridge_softc *,
296 struct bridge_iflist *);
298 static int bridge_ioctl_add(struct bridge_softc *, void *);
299 static int bridge_ioctl_del(struct bridge_softc *, void *);
300 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
301 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
302 static int bridge_ioctl_scache(struct bridge_softc *, void *);
303 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
304 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
305 static int bridge_ioctl_rts(struct bridge_softc *, void *);
306 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
307 static int bridge_ioctl_sto(struct bridge_softc *, void *);
308 static int bridge_ioctl_gto(struct bridge_softc *, void *);
309 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
310 static int bridge_ioctl_flush(struct bridge_softc *, void *);
311 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
312 static int bridge_ioctl_spri(struct bridge_softc *, void *);
313 static int bridge_ioctl_ght(struct bridge_softc *, void *);
314 static int bridge_ioctl_sht(struct bridge_softc *, void *);
315 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
316 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
317 static int bridge_ioctl_gma(struct bridge_softc *, void *);
318 static int bridge_ioctl_sma(struct bridge_softc *, void *);
319 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
320 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
321 static int bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *);
322 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
323 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
324 static int bridge_ioctl_gbparam(struct bridge_softc *, void *);
325 static int bridge_ioctl_grte(struct bridge_softc *, void *);
326 static int bridge_ioctl_gifsstp(struct bridge_softc *, void *);
327 static int bridge_ioctl_sproto(struct bridge_softc *, void *);
328 static int bridge_ioctl_stxhc(struct bridge_softc *, void *);
329 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
331 static int bridge_ip_checkbasic(struct mbuf **mp);
333 static int bridge_ip6_checkbasic(struct mbuf **mp);
335 static int bridge_fragment(struct ifnet *, struct mbuf *,
336 struct ether_header *, int, struct llc *);
337 static void bridge_linkstate(struct ifnet *ifp);
338 static void bridge_linkcheck(struct bridge_softc *sc);
340 extern void (*bridge_linkstate_p)(struct ifnet *ifp);
342 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
343 #define VLANTAGOF(_m) \
344 (_m->m_flags & M_VLANTAG) ? EVL_VLANOFTAG(_m->m_pkthdr.ether_vtag) : 1
346 static struct bstp_cb_ops bridge_ops = {
347 .bcb_state = bridge_state_change,
348 .bcb_rtage = bridge_rtable_expire
351 SYSCTL_DECL(_net_link);
352 static SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
354 /* only pass IP[46] packets when pfil is enabled */
355 static VNET_DEFINE(int, pfil_onlyip) = 1;
356 #define V_pfil_onlyip VNET(pfil_onlyip)
357 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip,
358 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_onlyip), 0,
359 "Only pass IP packets when pfil is enabled");
361 /* run pfil hooks on the bridge interface */
362 static VNET_DEFINE(int, pfil_bridge) = 1;
363 #define V_pfil_bridge VNET(pfil_bridge)
364 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge,
365 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_bridge), 0,
366 "Packet filter on the bridge interface");
368 /* layer2 filter with ipfw */
369 static VNET_DEFINE(int, pfil_ipfw);
370 #define V_pfil_ipfw VNET(pfil_ipfw)
372 /* layer2 ARP filter with ipfw */
373 static VNET_DEFINE(int, pfil_ipfw_arp);
374 #define V_pfil_ipfw_arp VNET(pfil_ipfw_arp)
375 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp,
376 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_ipfw_arp), 0,
377 "Filter ARP packets through IPFW layer2");
379 /* run pfil hooks on the member interface */
380 static VNET_DEFINE(int, pfil_member) = 1;
381 #define V_pfil_member VNET(pfil_member)
382 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member,
383 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_member), 0,
384 "Packet filter on the member interface");
386 /* run pfil hooks on the physical interface for locally destined packets */
387 static VNET_DEFINE(int, pfil_local_phys);
388 #define V_pfil_local_phys VNET(pfil_local_phys)
389 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys,
390 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_local_phys), 0,
391 "Packet filter on the physical interface for locally destined packets");
393 /* log STP state changes */
394 static VNET_DEFINE(int, log_stp);
395 #define V_log_stp VNET(log_stp)
396 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp,
397 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(log_stp), 0,
398 "Log STP state changes");
400 /* share MAC with first bridge member */
401 static VNET_DEFINE(int, bridge_inherit_mac);
402 #define V_bridge_inherit_mac VNET(bridge_inherit_mac)
403 SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac,
404 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(bridge_inherit_mac), 0,
405 "Inherit MAC address from the first bridge member");
407 static VNET_DEFINE(int, allow_llz_overlap) = 0;
408 #define V_allow_llz_overlap VNET(allow_llz_overlap)
409 SYSCTL_INT(_net_link_bridge, OID_AUTO, allow_llz_overlap,
410 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(allow_llz_overlap), 0,
411 "Allow overlap of link-local scope "
412 "zones of a bridge interface and the member interfaces");
414 struct bridge_control {
415 int (*bc_func)(struct bridge_softc *, void *);
420 #define BC_F_COPYIN 0x01 /* copy arguments in */
421 #define BC_F_COPYOUT 0x02 /* copy arguments out */
422 #define BC_F_SUSER 0x04 /* do super-user check */
424 const struct bridge_control bridge_control_table[] = {
425 { bridge_ioctl_add, sizeof(struct ifbreq),
426 BC_F_COPYIN|BC_F_SUSER },
427 { bridge_ioctl_del, sizeof(struct ifbreq),
428 BC_F_COPYIN|BC_F_SUSER },
430 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
431 BC_F_COPYIN|BC_F_COPYOUT },
432 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
433 BC_F_COPYIN|BC_F_SUSER },
435 { bridge_ioctl_scache, sizeof(struct ifbrparam),
436 BC_F_COPYIN|BC_F_SUSER },
437 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
440 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
441 BC_F_COPYIN|BC_F_COPYOUT },
442 { bridge_ioctl_rts, sizeof(struct ifbaconf),
443 BC_F_COPYIN|BC_F_COPYOUT },
445 { bridge_ioctl_saddr, sizeof(struct ifbareq),
446 BC_F_COPYIN|BC_F_SUSER },
448 { bridge_ioctl_sto, sizeof(struct ifbrparam),
449 BC_F_COPYIN|BC_F_SUSER },
450 { bridge_ioctl_gto, sizeof(struct ifbrparam),
453 { bridge_ioctl_daddr, sizeof(struct ifbareq),
454 BC_F_COPYIN|BC_F_SUSER },
456 { bridge_ioctl_flush, sizeof(struct ifbreq),
457 BC_F_COPYIN|BC_F_SUSER },
459 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
461 { bridge_ioctl_spri, sizeof(struct ifbrparam),
462 BC_F_COPYIN|BC_F_SUSER },
464 { bridge_ioctl_ght, sizeof(struct ifbrparam),
466 { bridge_ioctl_sht, sizeof(struct ifbrparam),
467 BC_F_COPYIN|BC_F_SUSER },
469 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
471 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
472 BC_F_COPYIN|BC_F_SUSER },
474 { bridge_ioctl_gma, sizeof(struct ifbrparam),
476 { bridge_ioctl_sma, sizeof(struct ifbrparam),
477 BC_F_COPYIN|BC_F_SUSER },
479 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
480 BC_F_COPYIN|BC_F_SUSER },
482 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
483 BC_F_COPYIN|BC_F_SUSER },
485 { bridge_ioctl_addspan, sizeof(struct ifbreq),
486 BC_F_COPYIN|BC_F_SUSER },
487 { bridge_ioctl_delspan, sizeof(struct ifbreq),
488 BC_F_COPYIN|BC_F_SUSER },
490 { bridge_ioctl_gbparam, sizeof(struct ifbropreq),
493 { bridge_ioctl_grte, sizeof(struct ifbrparam),
496 { bridge_ioctl_gifsstp, sizeof(struct ifbpstpconf),
497 BC_F_COPYIN|BC_F_COPYOUT },
499 { bridge_ioctl_sproto, sizeof(struct ifbrparam),
500 BC_F_COPYIN|BC_F_SUSER },
502 { bridge_ioctl_stxhc, sizeof(struct ifbrparam),
503 BC_F_COPYIN|BC_F_SUSER },
505 { bridge_ioctl_sifmaxaddr, sizeof(struct ifbreq),
506 BC_F_COPYIN|BC_F_SUSER },
509 const int bridge_control_table_size = nitems(bridge_control_table);
511 static VNET_DEFINE(LIST_HEAD(, bridge_softc), bridge_list);
512 #define V_bridge_list VNET(bridge_list)
513 #define BRIDGE_LIST_LOCK_INIT(x) mtx_init(&V_bridge_list_mtx, \
514 "if_bridge list", NULL, MTX_DEF)
515 #define BRIDGE_LIST_LOCK_DESTROY(x) mtx_destroy(&V_bridge_list_mtx)
516 #define BRIDGE_LIST_LOCK(x) mtx_lock(&V_bridge_list_mtx)
517 #define BRIDGE_LIST_UNLOCK(x) mtx_unlock(&V_bridge_list_mtx)
519 static VNET_DEFINE(struct if_clone *, bridge_cloner);
520 #define V_bridge_cloner VNET(bridge_cloner)
522 static const char bridge_name[] = "bridge";
525 vnet_bridge_init(const void *unused __unused)
528 BRIDGE_LIST_LOCK_INIT();
529 LIST_INIT(&V_bridge_list);
530 V_bridge_cloner = if_clone_simple(bridge_name,
531 bridge_clone_create, bridge_clone_destroy, 0);
533 VNET_SYSINIT(vnet_bridge_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
534 vnet_bridge_init, NULL);
537 vnet_bridge_uninit(const void *unused __unused)
540 if_clone_detach(V_bridge_cloner);
541 BRIDGE_LIST_LOCK_DESTROY();
543 VNET_SYSUNINIT(vnet_bridge_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
544 vnet_bridge_uninit, NULL);
547 bridge_modevent(module_t mod, int type, void *data)
552 bridge_rtnode_zone = uma_zcreate("bridge_rtnode",
553 sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL,
555 bridge_input_p = bridge_input;
556 bridge_output_p = bridge_output;
557 bridge_dn_p = bridge_dummynet;
558 bridge_linkstate_p = bridge_linkstate;
559 bridge_detach_cookie = EVENTHANDLER_REGISTER(
560 ifnet_departure_event, bridge_ifdetach, NULL,
561 EVENTHANDLER_PRI_ANY);
564 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
565 bridge_detach_cookie);
566 uma_zdestroy(bridge_rtnode_zone);
567 bridge_input_p = NULL;
568 bridge_output_p = NULL;
570 bridge_linkstate_p = NULL;
578 static moduledata_t bridge_mod = {
584 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
585 MODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1);
588 * handler for net.link.bridge.ipfw
591 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS)
593 int enable = V_pfil_ipfw;
596 error = sysctl_handle_int(oidp, &enable, 0, req);
599 if (enable != V_pfil_ipfw) {
600 V_pfil_ipfw = enable;
603 * Disable pfil so that ipfw doesnt run twice, if the user
604 * really wants both then they can re-enable pfil_bridge and/or
605 * pfil_member. Also allow non-ip packets as ipfw can filter by
617 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw,
618 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET,
619 &VNET_NAME(pfil_ipfw), 0, &sysctl_pfil_ipfw, "I",
620 "Layer2 filter with IPFW");
623 * bridge_clone_create:
625 * Create a new bridge instance.
628 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t params)
630 struct bridge_softc *sc, *sc2;
631 struct ifnet *bifp, *ifp;
633 unsigned long hostid;
635 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
636 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
642 BRIDGE_LOCK_INIT(sc);
643 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
644 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
646 /* Initialize our routing table. */
647 bridge_rtable_init(sc);
649 callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0);
651 LIST_INIT(&sc->sc_iflist);
652 LIST_INIT(&sc->sc_spanlist);
655 if_initname(ifp, bridge_name, unit);
656 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
657 ifp->if_ioctl = bridge_ioctl;
658 ifp->if_transmit = bridge_transmit;
659 ifp->if_qflush = bridge_qflush;
660 ifp->if_init = bridge_init;
661 ifp->if_type = IFT_BRIDGE;
664 * Generate an ethernet address with a locally administered address.
666 * Since we are using random ethernet addresses for the bridge, it is
667 * possible that we might have address collisions, so make sure that
668 * this hardware address isn't already in use on another bridge.
669 * The first try uses the hostid and falls back to arc4rand().
672 getcredhostid(curthread->td_ucred, &hostid);
674 if (fb || hostid == 0) {
675 arc4rand(sc->sc_defaddr, ETHER_ADDR_LEN, 1);
676 sc->sc_defaddr[0] &= ~1;/* clear multicast bit */
677 sc->sc_defaddr[0] |= 2; /* set the LAA bit */
679 sc->sc_defaddr[0] = 0x2;
680 sc->sc_defaddr[1] = (hostid >> 24) & 0xff;
681 sc->sc_defaddr[2] = (hostid >> 16) & 0xff;
682 sc->sc_defaddr[3] = (hostid >> 8 ) & 0xff;
683 sc->sc_defaddr[4] = hostid & 0xff;
684 sc->sc_defaddr[5] = ifp->if_dunit & 0xff;
690 LIST_FOREACH(sc2, &V_bridge_list, sc_list) {
692 if (memcmp(sc->sc_defaddr,
693 IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
698 BRIDGE_LIST_UNLOCK();
699 } while (retry == 1);
701 bstp_attach(&sc->sc_stp, &bridge_ops);
702 ether_ifattach(ifp, sc->sc_defaddr);
703 /* Now undo some of the damage... */
704 ifp->if_baudrate = 0;
705 ifp->if_type = IFT_BRIDGE;
708 LIST_INSERT_HEAD(&V_bridge_list, sc, sc_list);
709 BRIDGE_LIST_UNLOCK();
715 * bridge_clone_destroy:
717 * Destroy a bridge instance.
720 bridge_clone_destroy(struct ifnet *ifp)
722 struct bridge_softc *sc = ifp->if_softc;
723 struct bridge_iflist *bif;
728 ifp->if_flags &= ~IFF_UP;
730 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
731 bridge_delete_member(sc, bif, 0);
733 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
734 bridge_delete_span(sc, bif);
739 callout_drain(&sc->sc_brcallout);
742 LIST_REMOVE(sc, sc_list);
743 BRIDGE_LIST_UNLOCK();
745 bstp_detach(&sc->sc_stp);
749 /* Tear down the routing table. */
750 bridge_rtable_fini(sc);
752 BRIDGE_LOCK_DESTROY(sc);
759 * Handle a control request from the operator.
762 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
764 struct bridge_softc *sc = ifp->if_softc;
765 struct ifreq *ifr = (struct ifreq *)data;
766 struct bridge_iflist *bif;
767 struct thread *td = curthread;
769 struct ifbreq ifbreq;
770 struct ifbifconf ifbifconf;
771 struct ifbareq ifbareq;
772 struct ifbaconf ifbaconf;
773 struct ifbrparam ifbrparam;
774 struct ifbropreq ifbropreq;
776 struct ifdrv *ifd = (struct ifdrv *) data;
777 const struct bridge_control *bc;
788 if (ifd->ifd_cmd >= bridge_control_table_size) {
792 bc = &bridge_control_table[ifd->ifd_cmd];
794 if (cmd == SIOCGDRVSPEC &&
795 (bc->bc_flags & BC_F_COPYOUT) == 0) {
799 else if (cmd == SIOCSDRVSPEC &&
800 (bc->bc_flags & BC_F_COPYOUT) != 0) {
805 if (bc->bc_flags & BC_F_SUSER) {
806 error = priv_check(td, PRIV_NET_BRIDGE);
811 if (ifd->ifd_len != bc->bc_argsize ||
812 ifd->ifd_len > sizeof(args)) {
817 bzero(&args, sizeof(args));
818 if (bc->bc_flags & BC_F_COPYIN) {
819 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
825 error = (*bc->bc_func)(sc, &args);
830 if (bc->bc_flags & BC_F_COPYOUT)
831 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
836 if (!(ifp->if_flags & IFF_UP) &&
837 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
839 * If interface is marked down and it is running,
840 * then stop and disable it.
845 } else if ((ifp->if_flags & IFF_UP) &&
846 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
848 * If interface is marked up and it is stopped, then
856 if (ifr->ifr_mtu < 576) {
860 if (LIST_EMPTY(&sc->sc_iflist)) {
861 sc->sc_ifp->if_mtu = ifr->ifr_mtu;
865 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
866 if (bif->bif_ifp->if_mtu != ifr->ifr_mtu) {
867 log(LOG_NOTICE, "%s: invalid MTU: %u(%s)"
868 " != %d\n", sc->sc_ifp->if_xname,
869 bif->bif_ifp->if_mtu,
870 bif->bif_ifp->if_xname, ifr->ifr_mtu);
876 sc->sc_ifp->if_mtu = ifr->ifr_mtu;
881 * drop the lock as ether_ioctl() will call bridge_start() and
882 * cause the lock to be recursed.
884 error = ether_ioctl(ifp, cmd, data);
894 * Clear or restore unwanted capabilities on the member interface
897 bridge_mutecaps(struct bridge_softc *sc)
899 struct bridge_iflist *bif;
902 /* Initial bitmask of capabilities to test */
903 mask = BRIDGE_IFCAPS_MASK;
905 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
906 /* Every member must support it or its disabled */
907 mask &= bif->bif_savedcaps;
910 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
911 enabled = bif->bif_ifp->if_capenable;
912 enabled &= ~BRIDGE_IFCAPS_STRIP;
913 /* strip off mask bits and enable them again if allowed */
914 enabled &= ~BRIDGE_IFCAPS_MASK;
916 bridge_set_ifcap(sc, bif, enabled);
922 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set)
924 struct ifnet *ifp = bif->bif_ifp;
928 bzero(&ifr, sizeof(ifr));
929 ifr.ifr_reqcap = set;
931 if (ifp->if_capenable != set) {
932 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
934 if_printf(sc->sc_ifp,
935 "error setting interface capabilities on %s\n",
941 * bridge_lookup_member:
943 * Lookup a bridge member interface.
945 static struct bridge_iflist *
946 bridge_lookup_member(struct bridge_softc *sc, const char *name)
948 struct bridge_iflist *bif;
951 BRIDGE_LOCK_ASSERT(sc);
953 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
955 if (strcmp(ifp->if_xname, name) == 0)
963 * bridge_lookup_member_if:
965 * Lookup a bridge member interface by ifnet*.
967 static struct bridge_iflist *
968 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
970 struct bridge_iflist *bif;
972 BRIDGE_LOCK_ASSERT(sc);
974 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
975 if (bif->bif_ifp == member_ifp)
983 * bridge_delete_member:
985 * Delete the specified member interface.
988 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
991 struct ifnet *ifs = bif->bif_ifp;
992 struct ifnet *fif = NULL;
994 BRIDGE_LOCK_ASSERT(sc);
996 if (bif->bif_flags & IFBIF_STP)
997 bstp_disable(&bif->bif_stp);
999 ifs->if_bridge = NULL;
1001 LIST_REMOVE(bif, bif_next);
1005 * If removing the interface that gave the bridge its mac address, set
1006 * the mac address of the bridge to the address of the next member, or
1007 * to its default address if no members are left.
1009 if (V_bridge_inherit_mac && sc->sc_ifaddr == ifs) {
1010 if (LIST_EMPTY(&sc->sc_iflist)) {
1011 bcopy(sc->sc_defaddr,
1012 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1013 sc->sc_ifaddr = NULL;
1015 fif = LIST_FIRST(&sc->sc_iflist)->bif_ifp;
1016 bcopy(IF_LLADDR(fif),
1017 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1018 sc->sc_ifaddr = fif;
1020 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
1023 bridge_linkcheck(sc);
1024 bridge_mutecaps(sc); /* recalcuate now this interface is removed */
1025 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
1026 KASSERT(bif->bif_addrcnt == 0,
1027 ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt));
1031 switch (ifs->if_type) {
1035 * Take the interface out of promiscuous mode.
1037 (void) ifpromisc(ifs, 0);
1045 panic("bridge_delete_member: impossible");
1049 /* reneable any interface capabilities */
1050 bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
1052 bstp_destroy(&bif->bif_stp); /* prepare to free */
1054 free(bif, M_DEVBUF);
1058 * bridge_delete_span:
1060 * Delete the specified span interface.
1063 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1065 BRIDGE_LOCK_ASSERT(sc);
1067 KASSERT(bif->bif_ifp->if_bridge == NULL,
1068 ("%s: not a span interface", __func__));
1070 LIST_REMOVE(bif, bif_next);
1071 free(bif, M_DEVBUF);
1075 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1077 struct ifbreq *req = arg;
1078 struct bridge_iflist *bif = NULL;
1082 ifs = ifunit(req->ifbr_ifsname);
1085 if (ifs->if_ioctl == NULL) /* must be supported */
1088 /* If it's in the span list, it can't be a member. */
1089 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1090 if (ifs == bif->bif_ifp)
1093 if (ifs->if_bridge == sc)
1096 if (ifs->if_bridge != NULL)
1099 switch (ifs->if_type) {
1103 /* permitted interface types */
1111 * Two valid inet6 addresses with link-local scope must not be
1112 * on the parent interface and the member interfaces at the
1113 * same time. This restriction is needed to prevent violation
1114 * of link-local scope zone. Attempts to add a member
1115 * interface which has inet6 addresses when the parent has
1116 * inet6 triggers removal of all inet6 addresses on the member
1120 /* Check if the parent interface has a link-local scope addr. */
1121 if (V_allow_llz_overlap == 0 &&
1122 in6ifa_llaonifp(sc->sc_ifp) != NULL) {
1124 * If any, remove all inet6 addresses from the member
1128 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1129 if (in6ifa_llaonifp(bif->bif_ifp)) {
1131 in6_ifdetach(bif->bif_ifp);
1133 if_printf(sc->sc_ifp,
1134 "IPv6 addresses on %s have been removed "
1135 "before adding it as a member to prevent "
1136 "IPv6 address scope violation.\n",
1137 bif->bif_ifp->if_xname);
1141 if (in6ifa_llaonifp(ifs)) {
1145 if_printf(sc->sc_ifp,
1146 "IPv6 addresses on %s have been removed "
1147 "before adding it as a member to prevent "
1148 "IPv6 address scope violation.\n",
1153 /* Allow the first Ethernet member to define the MTU */
1154 if (LIST_EMPTY(&sc->sc_iflist))
1155 sc->sc_ifp->if_mtu = ifs->if_mtu;
1156 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
1157 if_printf(sc->sc_ifp, "invalid MTU: %u(%s) != %u\n",
1158 ifs->if_mtu, ifs->if_xname, sc->sc_ifp->if_mtu);
1162 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1167 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
1168 bif->bif_savedcaps = ifs->if_capenable;
1171 * Assign the interface's MAC address to the bridge if it's the first
1172 * member and the MAC address of the bridge has not been changed from
1173 * the default randomly generated one.
1175 if (V_bridge_inherit_mac && LIST_EMPTY(&sc->sc_iflist) &&
1176 !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr, ETHER_ADDR_LEN)) {
1177 bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1178 sc->sc_ifaddr = ifs;
1179 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
1182 ifs->if_bridge = sc;
1183 bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
1185 * XXX: XLOCK HERE!?!
1187 * NOTE: insert_***HEAD*** should be safe for the traversals.
1189 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
1191 /* Set interface capabilities to the intersection set of all members */
1192 bridge_mutecaps(sc);
1193 bridge_linkcheck(sc);
1195 /* Place the interface into promiscuous mode */
1196 switch (ifs->if_type) {
1200 error = ifpromisc(ifs, 1);
1206 bridge_delete_member(sc, bif, 0);
1207 free(bif, M_DEVBUF);
1213 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1215 struct ifbreq *req = arg;
1216 struct bridge_iflist *bif;
1218 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1222 bridge_delete_member(sc, bif, 0);
1228 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1230 struct ifbreq *req = arg;
1231 struct bridge_iflist *bif;
1232 struct bstp_port *bp;
1234 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1239 req->ifbr_ifsflags = bif->bif_flags;
1240 req->ifbr_state = bp->bp_state;
1241 req->ifbr_priority = bp->bp_priority;
1242 req->ifbr_path_cost = bp->bp_path_cost;
1243 req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1244 req->ifbr_proto = bp->bp_protover;
1245 req->ifbr_role = bp->bp_role;
1246 req->ifbr_stpflags = bp->bp_flags;
1247 req->ifbr_addrcnt = bif->bif_addrcnt;
1248 req->ifbr_addrmax = bif->bif_addrmax;
1249 req->ifbr_addrexceeded = bif->bif_addrexceeded;
1251 /* Copy STP state options as flags */
1252 if (bp->bp_operedge)
1253 req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
1254 if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
1255 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
1256 if (bp->bp_ptp_link)
1257 req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
1258 if (bp->bp_flags & BSTP_PORT_AUTOPTP)
1259 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
1260 if (bp->bp_flags & BSTP_PORT_ADMEDGE)
1261 req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
1262 if (bp->bp_flags & BSTP_PORT_ADMCOST)
1263 req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
1268 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1270 struct ifbreq *req = arg;
1271 struct bridge_iflist *bif;
1272 struct bstp_port *bp;
1275 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1280 if (req->ifbr_ifsflags & IFBIF_SPAN)
1281 /* SPAN is readonly */
1284 if (req->ifbr_ifsflags & IFBIF_STP) {
1285 if ((bif->bif_flags & IFBIF_STP) == 0) {
1286 error = bstp_enable(&bif->bif_stp);
1291 if ((bif->bif_flags & IFBIF_STP) != 0)
1292 bstp_disable(&bif->bif_stp);
1295 /* Pass on STP flags */
1296 bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
1297 bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
1298 bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
1299 bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
1301 /* Save the bits relating to the bridge */
1302 bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK;
1308 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1310 struct ifbrparam *param = arg;
1312 sc->sc_brtmax = param->ifbrp_csize;
1319 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1321 struct ifbrparam *param = arg;
1323 param->ifbrp_csize = sc->sc_brtmax;
1329 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1331 struct ifbifconf *bifc = arg;
1332 struct bridge_iflist *bif;
1335 int count, buflen, len, error = 0;
1338 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
1340 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1343 buflen = sizeof(breq) * count;
1344 if (bifc->ifbic_len == 0) {
1345 bifc->ifbic_len = buflen;
1349 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1354 len = min(bifc->ifbic_len, buflen);
1355 bzero(&breq, sizeof(breq));
1356 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1357 if (len < sizeof(breq))
1360 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1361 sizeof(breq.ifbr_ifsname));
1362 /* Fill in the ifbreq structure */
1363 error = bridge_ioctl_gifflags(sc, &breq);
1366 memcpy(buf, &breq, sizeof(breq));
1368 buf += sizeof(breq);
1369 len -= sizeof(breq);
1371 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1372 if (len < sizeof(breq))
1375 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1376 sizeof(breq.ifbr_ifsname));
1377 breq.ifbr_ifsflags = bif->bif_flags;
1378 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1379 memcpy(buf, &breq, sizeof(breq));
1381 buf += sizeof(breq);
1382 len -= sizeof(breq);
1386 bifc->ifbic_len = sizeof(breq) * count;
1387 error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len);
1389 free(outbuf, M_TEMP);
1394 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1396 struct ifbaconf *bac = arg;
1397 struct bridge_rtnode *brt;
1398 struct ifbareq bareq;
1400 int count, buflen, len, error = 0;
1402 if (bac->ifbac_len == 0)
1406 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list)
1408 buflen = sizeof(bareq) * count;
1411 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1416 len = min(bac->ifbac_len, buflen);
1417 bzero(&bareq, sizeof(bareq));
1418 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1419 if (len < sizeof(bareq))
1421 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1422 sizeof(bareq.ifba_ifsname));
1423 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1424 bareq.ifba_vlan = brt->brt_vlan;
1425 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1426 time_uptime < brt->brt_expire)
1427 bareq.ifba_expire = brt->brt_expire - time_uptime;
1429 bareq.ifba_expire = 0;
1430 bareq.ifba_flags = brt->brt_flags;
1432 memcpy(buf, &bareq, sizeof(bareq));
1434 buf += sizeof(bareq);
1435 len -= sizeof(bareq);
1439 bac->ifbac_len = sizeof(bareq) * count;
1440 error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len);
1442 free(outbuf, M_TEMP);
1447 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1449 struct ifbareq *req = arg;
1450 struct bridge_iflist *bif;
1453 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1457 error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
1464 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1466 struct ifbrparam *param = arg;
1468 sc->sc_brttimeout = param->ifbrp_ctime;
1473 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1475 struct ifbrparam *param = arg;
1477 param->ifbrp_ctime = sc->sc_brttimeout;
1482 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1484 struct ifbareq *req = arg;
1486 return (bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan));
1490 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1492 struct ifbreq *req = arg;
1494 bridge_rtflush(sc, req->ifbr_ifsflags);
1499 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1501 struct ifbrparam *param = arg;
1502 struct bstp_state *bs = &sc->sc_stp;
1504 param->ifbrp_prio = bs->bs_bridge_priority;
1509 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1511 struct ifbrparam *param = arg;
1513 return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio));
1517 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1519 struct ifbrparam *param = arg;
1520 struct bstp_state *bs = &sc->sc_stp;
1522 param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
1527 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1529 struct ifbrparam *param = arg;
1531 return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime));
1535 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1537 struct ifbrparam *param = arg;
1538 struct bstp_state *bs = &sc->sc_stp;
1540 param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
1545 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1547 struct ifbrparam *param = arg;
1549 return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay));
1553 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1555 struct ifbrparam *param = arg;
1556 struct bstp_state *bs = &sc->sc_stp;
1558 param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
1563 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1565 struct ifbrparam *param = arg;
1567 return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage));
1571 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1573 struct ifbreq *req = arg;
1574 struct bridge_iflist *bif;
1576 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1580 return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority));
1584 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1586 struct ifbreq *req = arg;
1587 struct bridge_iflist *bif;
1589 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1593 return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost));
1597 bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg)
1599 struct ifbreq *req = arg;
1600 struct bridge_iflist *bif;
1602 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1606 bif->bif_addrmax = req->ifbr_addrmax;
1611 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1613 struct ifbreq *req = arg;
1614 struct bridge_iflist *bif = NULL;
1617 ifs = ifunit(req->ifbr_ifsname);
1621 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1622 if (ifs == bif->bif_ifp)
1625 if (ifs->if_bridge != NULL)
1628 switch (ifs->if_type) {
1637 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1642 bif->bif_flags = IFBIF_SPAN;
1644 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1650 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1652 struct ifbreq *req = arg;
1653 struct bridge_iflist *bif;
1656 ifs = ifunit(req->ifbr_ifsname);
1660 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1661 if (ifs == bif->bif_ifp)
1667 bridge_delete_span(sc, bif);
1673 bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg)
1675 struct ifbropreq *req = arg;
1676 struct bstp_state *bs = &sc->sc_stp;
1677 struct bstp_port *root_port;
1679 req->ifbop_maxage = bs->bs_bridge_max_age >> 8;
1680 req->ifbop_hellotime = bs->bs_bridge_htime >> 8;
1681 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;
1683 root_port = bs->bs_root_port;
1684 if (root_port == NULL)
1685 req->ifbop_root_port = 0;
1687 req->ifbop_root_port = root_port->bp_ifp->if_index;
1689 req->ifbop_holdcount = bs->bs_txholdcount;
1690 req->ifbop_priority = bs->bs_bridge_priority;
1691 req->ifbop_protocol = bs->bs_protover;
1692 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;
1693 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id;
1694 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id;
1695 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id;
1696 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;
1697 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;
1703 bridge_ioctl_grte(struct bridge_softc *sc, void *arg)
1705 struct ifbrparam *param = arg;
1707 param->ifbrp_cexceeded = sc->sc_brtexceeded;
1712 bridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg)
1714 struct ifbpstpconf *bifstp = arg;
1715 struct bridge_iflist *bif;
1716 struct bstp_port *bp;
1717 struct ifbpstpreq bpreq;
1719 int count, buflen, len, error = 0;
1722 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1723 if ((bif->bif_flags & IFBIF_STP) != 0)
1727 buflen = sizeof(bpreq) * count;
1728 if (bifstp->ifbpstp_len == 0) {
1729 bifstp->ifbpstp_len = buflen;
1734 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1739 len = min(bifstp->ifbpstp_len, buflen);
1740 bzero(&bpreq, sizeof(bpreq));
1741 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1742 if (len < sizeof(bpreq))
1745 if ((bif->bif_flags & IFBIF_STP) == 0)
1749 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff;
1750 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions;
1751 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost;
1752 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id;
1753 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id;
1754 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id;
1756 memcpy(buf, &bpreq, sizeof(bpreq));
1758 buf += sizeof(bpreq);
1759 len -= sizeof(bpreq);
1763 bifstp->ifbpstp_len = sizeof(bpreq) * count;
1764 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len);
1766 free(outbuf, M_TEMP);
1771 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
1773 struct ifbrparam *param = arg;
1775 return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto));
1779 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
1781 struct ifbrparam *param = arg;
1783 return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc));
1789 * Detach an interface from a bridge. Called when a member
1790 * interface is detaching.
1793 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1795 struct bridge_softc *sc = ifp->if_bridge;
1796 struct bridge_iflist *bif;
1798 if (ifp->if_flags & IFF_RENAMING)
1801 /* Check if the interface is a bridge member */
1805 bif = bridge_lookup_member_if(sc, ifp);
1807 bridge_delete_member(sc, bif, 1);
1813 /* Check if the interface is a span port */
1815 LIST_FOREACH(sc, &V_bridge_list, sc_list) {
1817 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1818 if (ifp == bif->bif_ifp) {
1819 bridge_delete_span(sc, bif);
1825 BRIDGE_LIST_UNLOCK();
1831 * Initialize a bridge interface.
1834 bridge_init(void *xsc)
1836 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1837 struct ifnet *ifp = sc->sc_ifp;
1839 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1843 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1846 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1847 bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */
1855 * Stop the bridge interface.
1858 bridge_stop(struct ifnet *ifp, int disable)
1860 struct bridge_softc *sc = ifp->if_softc;
1862 BRIDGE_LOCK_ASSERT(sc);
1864 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1867 callout_stop(&sc->sc_brcallout);
1868 bstp_stop(&sc->sc_stp);
1870 bridge_rtflush(sc, IFBF_FLUSHDYN);
1872 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1878 * Enqueue a packet on a bridge member interface.
1882 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1888 /* We may be sending a fragment so traverse the mbuf */
1891 m->m_nextpkt = NULL;
1892 len = m->m_pkthdr.len;
1893 mflags = m->m_flags;
1896 * If underlying interface can not do VLAN tag insertion itself
1897 * then attach a packet tag that holds it.
1899 if ((m->m_flags & M_VLANTAG) &&
1900 (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) {
1901 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1904 "unable to prepend VLAN header\n");
1905 if_inc_counter(dst_ifp, IFCOUNTER_OERRORS, 1);
1908 m->m_flags &= ~M_VLANTAG;
1911 if ((err = dst_ifp->if_transmit(dst_ifp, m))) {
1913 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
1917 if_inc_counter(sc->sc_ifp, IFCOUNTER_OPACKETS, 1);
1918 if_inc_counter(sc->sc_ifp, IFCOUNTER_OBYTES, len);
1919 if (mflags & M_MCAST)
1920 if_inc_counter(sc->sc_ifp, IFCOUNTER_OMCASTS, 1);
1929 * Receive a queued packet from dummynet and pass it on to the output
1932 * The mbuf has the Ethernet header already attached.
1935 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
1937 struct bridge_softc *sc;
1939 sc = ifp->if_bridge;
1942 * The packet didnt originate from a member interface. This should only
1943 * ever happen if a member interface is removed while packets are
1951 if (PFIL_HOOKED(&V_inet_pfil_hook)
1953 || PFIL_HOOKED(&V_inet6_pfil_hook)
1956 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
1962 bridge_enqueue(sc, ifp, m);
1968 * Send output from a bridge member interface. This
1969 * performs the bridging function for locally originated
1972 * The mbuf has the Ethernet header already attached. We must
1973 * enqueue or free the mbuf before returning.
1976 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1979 struct ether_header *eh;
1980 struct ifnet *dst_if;
1981 struct bridge_softc *sc;
1984 if (m->m_len < ETHER_HDR_LEN) {
1985 m = m_pullup(m, ETHER_HDR_LEN);
1990 eh = mtod(m, struct ether_header *);
1991 sc = ifp->if_bridge;
1992 vlan = VLANTAGOF(m);
1997 * If bridge is down, but the original output interface is up,
1998 * go ahead and send out that interface. Otherwise, the packet
2001 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2007 * If the packet is a multicast, or we don't know a better way to
2008 * get there, send to all interfaces.
2010 if (ETHER_IS_MULTICAST(eh->ether_dhost))
2013 dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan);
2014 if (dst_if == NULL) {
2015 struct bridge_iflist *bif;
2017 int error = 0, used = 0;
2021 BRIDGE_LOCK2REF(sc, error);
2027 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
2028 dst_if = bif->bif_ifp;
2030 if (dst_if->if_type == IFT_GIF)
2032 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2036 * If this is not the original output interface,
2037 * and the interface is participating in spanning
2038 * tree, make sure the port is in a state that
2039 * allows forwarding.
2041 if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) &&
2042 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2045 if (LIST_NEXT(bif, bif_next) == NULL) {
2049 mc = m_copypacket(m, M_NOWAIT);
2051 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2056 bridge_enqueue(sc, dst_if, mc);
2066 * XXX Spanning tree consideration here?
2070 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2077 bridge_enqueue(sc, dst_if, m);
2084 * Do output on a bridge.
2088 bridge_transmit(struct ifnet *ifp, struct mbuf *m)
2090 struct bridge_softc *sc;
2091 struct ether_header *eh;
2092 struct ifnet *dst_if;
2097 ETHER_BPF_MTAP(ifp, m);
2099 eh = mtod(m, struct ether_header *);
2102 if (((m->m_flags & (M_BCAST|M_MCAST)) == 0) &&
2103 (dst_if = bridge_rtlookup(sc, eh->ether_dhost, 1)) != NULL) {
2105 error = bridge_enqueue(sc, dst_if, m);
2107 bridge_broadcast(sc, ifp, m, 0);
2113 * The ifp->if_qflush entry point for if_bridge(4) is no-op.
2116 bridge_qflush(struct ifnet *ifp __unused)
2123 * The forwarding function of the bridge.
2125 * NOTE: Releases the lock on return.
2128 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
2131 struct bridge_iflist *dbif;
2132 struct ifnet *src_if, *dst_if, *ifp;
2133 struct ether_header *eh;
2138 src_if = m->m_pkthdr.rcvif;
2141 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2142 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
2143 vlan = VLANTAGOF(m);
2145 if ((sbif->bif_flags & IFBIF_STP) &&
2146 sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2149 eh = mtod(m, struct ether_header *);
2150 dst = eh->ether_dhost;
2152 /* If the interface is learning, record the address. */
2153 if (sbif->bif_flags & IFBIF_LEARNING) {
2154 error = bridge_rtupdate(sc, eh->ether_shost, vlan,
2155 sbif, 0, IFBAF_DYNAMIC);
2157 * If the interface has addresses limits then deny any source
2158 * that is not in the cache.
2160 if (error && sbif->bif_addrmax)
2164 if ((sbif->bif_flags & IFBIF_STP) != 0 &&
2165 sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING)
2169 * At this point, the port either doesn't participate
2170 * in spanning tree or it is in the forwarding state.
2174 * If the packet is unicast, destined for someone on
2175 * "this" side of the bridge, drop it.
2177 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2178 dst_if = bridge_rtlookup(sc, dst, vlan);
2179 if (src_if == dst_if)
2183 * Check if its a reserved multicast address, any address
2184 * listed in 802.1D section 7.12.6 may not be forwarded by the
2186 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
2188 if (dst[0] == 0x01 && dst[1] == 0x80 &&
2189 dst[2] == 0xc2 && dst[3] == 0x00 &&
2190 dst[4] == 0x00 && dst[5] <= 0x0f)
2193 /* ...forward it to all interfaces. */
2194 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
2199 * If we have a destination interface which is a member of our bridge,
2200 * OR this is a unicast packet, push it through the bpf(4) machinery.
2201 * For broadcast or multicast packets, don't bother because it will
2202 * be reinjected into ether_input. We do this before we pass the packets
2203 * through the pfil(9) framework, as it is possible that pfil(9) will
2204 * drop the packet, or possibly modify it, making it difficult to debug
2205 * firewall issues on the bridge.
2207 if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0)
2208 ETHER_BPF_MTAP(ifp, m);
2210 /* run the packet filter */
2211 if (PFIL_HOOKED(&V_inet_pfil_hook)
2213 || PFIL_HOOKED(&V_inet6_pfil_hook)
2217 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2224 if (dst_if == NULL) {
2225 bridge_broadcast(sc, src_if, m, 1);
2230 * At this point, we're dealing with a unicast frame
2231 * going to a different interface.
2233 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2236 dbif = bridge_lookup_member_if(sc, dst_if);
2238 /* Not a member of the bridge (anymore?) */
2241 /* Private segments can not talk to each other */
2242 if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)
2245 if ((dbif->bif_flags & IFBIF_STP) &&
2246 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2251 if (PFIL_HOOKED(&V_inet_pfil_hook)
2253 || PFIL_HOOKED(&V_inet6_pfil_hook)
2256 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2262 bridge_enqueue(sc, dst_if, m);
2273 * Receive input from a member interface. Queue the packet for
2274 * bridging if it is not for us.
2276 static struct mbuf *
2277 bridge_input(struct ifnet *ifp, struct mbuf *m)
2279 struct bridge_softc *sc = ifp->if_bridge;
2280 struct bridge_iflist *bif, *bif2;
2282 struct ether_header *eh;
2283 struct mbuf *mc, *mc2;
2287 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2291 vlan = VLANTAGOF(m);
2294 * Implement support for bridge monitoring. If this flag has been
2295 * set on this interface, discard the packet once we push it through
2296 * the bpf(4) machinery, but before we do, increment the byte and
2297 * packet counters associated with this interface.
2299 if ((bifp->if_flags & IFF_MONITOR) != 0) {
2300 m->m_pkthdr.rcvif = bifp;
2301 ETHER_BPF_MTAP(bifp, m);
2302 if_inc_counter(bifp, IFCOUNTER_IPACKETS, 1);
2303 if_inc_counter(bifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
2308 bif = bridge_lookup_member_if(sc, ifp);
2314 eh = mtod(m, struct ether_header *);
2318 if (m->m_flags & (M_BCAST|M_MCAST)) {
2319 /* Tap off 802.1D packets; they do not get forwarded. */
2320 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2321 ETHER_ADDR_LEN) == 0) {
2322 bstp_input(&bif->bif_stp, ifp, m); /* consumes mbuf */
2327 if ((bif->bif_flags & IFBIF_STP) &&
2328 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2334 * Make a deep copy of the packet and enqueue the copy
2335 * for bridge processing; return the original packet for
2338 mc = m_dup(m, M_NOWAIT);
2344 /* Perform the bridge forwarding function with the copy. */
2345 bridge_forward(sc, bif, mc);
2348 * Reinject the mbuf as arriving on the bridge so we have a
2349 * chance at claiming multicast packets. We can not loop back
2350 * here from ether_input as a bridge is never a member of a
2353 KASSERT(bifp->if_bridge == NULL,
2354 ("loop created in bridge_input"));
2355 mc2 = m_dup(m, M_NOWAIT);
2357 /* Keep the layer3 header aligned */
2358 int i = min(mc2->m_pkthdr.len, max_protohdr);
2359 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2362 mc2->m_pkthdr.rcvif = bifp;
2363 (*bifp->if_input)(bifp, mc2);
2366 /* Return the original packet for local processing. */
2370 if ((bif->bif_flags & IFBIF_STP) &&
2371 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2376 #if (defined(INET) || defined(INET6))
2377 # define OR_CARP_CHECK_WE_ARE_DST(iface) \
2378 || ((iface)->if_carp \
2379 && (*carp_forus_p)((iface), eh->ether_dhost))
2380 # define OR_CARP_CHECK_WE_ARE_SRC(iface) \
2381 || ((iface)->if_carp \
2382 && (*carp_forus_p)((iface), eh->ether_shost))
2384 # define OR_CARP_CHECK_WE_ARE_DST(iface)
2385 # define OR_CARP_CHECK_WE_ARE_SRC(iface)
2389 # define OR_PFIL_HOOKED_INET6 \
2390 || PFIL_HOOKED(&V_inet6_pfil_hook)
2392 # define OR_PFIL_HOOKED_INET6
2395 #define GRAB_OUR_PACKETS(iface) \
2396 if ((iface)->if_type == IFT_GIF) \
2398 /* It is destined for us. */ \
2399 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, ETHER_ADDR_LEN) == 0 \
2400 OR_CARP_CHECK_WE_ARE_DST((iface)) \
2402 if ((iface)->if_type == IFT_BRIDGE) { \
2403 ETHER_BPF_MTAP(iface, m); \
2404 if_inc_counter(iface, IFCOUNTER_IPACKETS, 1); \
2405 if_inc_counter(iface, IFCOUNTER_IBYTES, m->m_pkthdr.len); \
2406 /* Filter on the physical interface. */ \
2407 if (V_pfil_local_phys && \
2408 (PFIL_HOOKED(&V_inet_pfil_hook) \
2409 OR_PFIL_HOOKED_INET6)) { \
2410 if (bridge_pfil(&m, NULL, ifp, \
2411 PFIL_IN) != 0 || m == NULL) { \
2412 BRIDGE_UNLOCK(sc); \
2415 eh = mtod(m, struct ether_header *); \
2418 if (bif->bif_flags & IFBIF_LEARNING) { \
2419 error = bridge_rtupdate(sc, eh->ether_shost, \
2420 vlan, bif, 0, IFBAF_DYNAMIC); \
2421 if (error && bif->bif_addrmax) { \
2422 BRIDGE_UNLOCK(sc); \
2427 m->m_pkthdr.rcvif = iface; \
2428 BRIDGE_UNLOCK(sc); \
2432 /* We just received a packet that we sent out. */ \
2433 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, ETHER_ADDR_LEN) == 0 \
2434 OR_CARP_CHECK_WE_ARE_SRC((iface)) \
2436 BRIDGE_UNLOCK(sc); \
2442 * Unicast. Make sure it's not for the bridge.
2444 do { GRAB_OUR_PACKETS(bifp) } while (0);
2447 * Give a chance for ifp at first priority. This will help when the
2448 * packet comes through the interface like VLAN's with the same MACs
2449 * on several interfaces from the same bridge. This also will save
2450 * some CPU cycles in case the destination interface and the input
2451 * interface (eq ifp) are the same.
2453 do { GRAB_OUR_PACKETS(ifp) } while (0);
2455 /* Now check the all bridge members. */
2456 LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) {
2457 GRAB_OUR_PACKETS(bif2->bif_ifp)
2460 #undef OR_CARP_CHECK_WE_ARE_DST
2461 #undef OR_CARP_CHECK_WE_ARE_SRC
2462 #undef OR_PFIL_HOOKED_INET6
2463 #undef GRAB_OUR_PACKETS
2465 /* Perform the bridge forwarding function. */
2466 bridge_forward(sc, bif, m);
2474 * Send a frame to all interfaces that are members of
2475 * the bridge, except for the one on which the packet
2478 * NOTE: Releases the lock on return.
2481 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2482 struct mbuf *m, int runfilt)
2484 struct bridge_iflist *dbif, *sbif;
2486 struct ifnet *dst_if;
2487 int error = 0, used = 0, i;
2489 sbif = bridge_lookup_member_if(sc, src_if);
2491 BRIDGE_LOCK2REF(sc, error);
2497 /* Filter on the bridge interface before broadcasting */
2498 if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook)
2500 || PFIL_HOOKED(&V_inet6_pfil_hook)
2503 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
2509 LIST_FOREACH(dbif, &sc->sc_iflist, bif_next) {
2510 dst_if = dbif->bif_ifp;
2511 if (dst_if == src_if)
2514 /* Private segments can not talk to each other */
2515 if (sbif && (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE))
2518 if ((dbif->bif_flags & IFBIF_STP) &&
2519 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2522 if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 &&
2523 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2526 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2529 if (LIST_NEXT(dbif, bif_next) == NULL) {
2533 mc = m_dup(m, M_NOWAIT);
2535 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2541 * Filter on the output interface. Pass a NULL bridge interface
2542 * pointer so we do not redundantly filter on the bridge for
2543 * each interface we broadcast on.
2545 if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook)
2547 || PFIL_HOOKED(&V_inet6_pfil_hook)
2551 /* Keep the layer3 header aligned */
2552 i = min(mc->m_pkthdr.len, max_protohdr);
2553 mc = m_copyup(mc, i, ETHER_ALIGN);
2555 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2559 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2565 bridge_enqueue(sc, dst_if, mc);
2577 * Duplicate a packet out one or more interfaces that are in span mode,
2578 * the original mbuf is unmodified.
2581 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2583 struct bridge_iflist *bif;
2584 struct ifnet *dst_if;
2587 if (LIST_EMPTY(&sc->sc_spanlist))
2590 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2591 dst_if = bif->bif_ifp;
2593 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2596 mc = m_copypacket(m, M_NOWAIT);
2598 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2602 bridge_enqueue(sc, dst_if, mc);
2609 * Add a bridge routing entry.
2612 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan,
2613 struct bridge_iflist *bif, int setflags, uint8_t flags)
2615 struct bridge_rtnode *brt;
2618 BRIDGE_LOCK_ASSERT(sc);
2620 /* Check the source address is valid and not multicast. */
2621 if (ETHER_IS_MULTICAST(dst) ||
2622 (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
2623 dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0)
2626 /* 802.1p frames map to vlan 1 */
2631 * A route for this destination might already exist. If so,
2632 * update it, otherwise create a new one.
2634 if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) {
2635 if (sc->sc_brtcnt >= sc->sc_brtmax) {
2636 sc->sc_brtexceeded++;
2639 /* Check per interface address limits (if enabled) */
2640 if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) {
2641 bif->bif_addrexceeded++;
2646 * Allocate a new bridge forwarding node, and
2647 * initialize the expiration time and Ethernet
2650 brt = uma_zalloc(bridge_rtnode_zone, M_NOWAIT | M_ZERO);
2654 if (bif->bif_flags & IFBIF_STICKY)
2655 brt->brt_flags = IFBAF_STICKY;
2657 brt->brt_flags = IFBAF_DYNAMIC;
2659 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2660 brt->brt_vlan = vlan;
2662 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2663 uma_zfree(bridge_rtnode_zone, brt);
2670 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2671 brt->brt_dst != bif) {
2672 brt->brt_dst->bif_addrcnt--;
2674 brt->brt_dst->bif_addrcnt++;
2677 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2678 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2680 brt->brt_flags = flags;
2688 * Lookup the destination interface for an address.
2690 static struct ifnet *
2691 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2693 struct bridge_rtnode *brt;
2695 BRIDGE_LOCK_ASSERT(sc);
2697 if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL)
2700 return (brt->brt_ifp);
2706 * Trim the routine table so that we have a number
2707 * of routing entries less than or equal to the
2711 bridge_rttrim(struct bridge_softc *sc)
2713 struct bridge_rtnode *brt, *nbrt;
2715 BRIDGE_LOCK_ASSERT(sc);
2717 /* Make sure we actually need to do this. */
2718 if (sc->sc_brtcnt <= sc->sc_brtmax)
2721 /* Force an aging cycle; this might trim enough addresses. */
2723 if (sc->sc_brtcnt <= sc->sc_brtmax)
2726 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2727 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2728 bridge_rtnode_destroy(sc, brt);
2729 if (sc->sc_brtcnt <= sc->sc_brtmax)
2738 * Aging timer for the bridge.
2741 bridge_timer(void *arg)
2743 struct bridge_softc *sc = arg;
2745 BRIDGE_LOCK_ASSERT(sc);
2749 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
2750 callout_reset(&sc->sc_brcallout,
2751 bridge_rtable_prune_period * hz, bridge_timer, sc);
2757 * Perform an aging cycle.
2760 bridge_rtage(struct bridge_softc *sc)
2762 struct bridge_rtnode *brt, *nbrt;
2764 BRIDGE_LOCK_ASSERT(sc);
2766 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2767 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2768 if (time_uptime >= brt->brt_expire)
2769 bridge_rtnode_destroy(sc, brt);
2777 * Remove all dynamic addresses from the bridge.
2780 bridge_rtflush(struct bridge_softc *sc, int full)
2782 struct bridge_rtnode *brt, *nbrt;
2784 BRIDGE_LOCK_ASSERT(sc);
2786 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2787 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2788 bridge_rtnode_destroy(sc, brt);
2795 * Remove an address from the table.
2798 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2800 struct bridge_rtnode *brt;
2803 BRIDGE_LOCK_ASSERT(sc);
2806 * If vlan is zero then we want to delete for all vlans so the lookup
2807 * may return more than one.
2809 while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) {
2810 bridge_rtnode_destroy(sc, brt);
2814 return (found ? 0 : ENOENT);
2820 * Delete routes to a speicifc member interface.
2823 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2825 struct bridge_rtnode *brt, *nbrt;
2827 BRIDGE_LOCK_ASSERT(sc);
2829 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2830 if (brt->brt_ifp == ifp && (full ||
2831 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2832 bridge_rtnode_destroy(sc, brt);
2837 * bridge_rtable_init:
2839 * Initialize the route table for this bridge.
2842 bridge_rtable_init(struct bridge_softc *sc)
2846 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2847 M_DEVBUF, M_WAITOK);
2849 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2850 LIST_INIT(&sc->sc_rthash[i]);
2852 sc->sc_rthash_key = arc4random();
2853 LIST_INIT(&sc->sc_rtlist);
2857 * bridge_rtable_fini:
2859 * Deconstruct the route table for this bridge.
2862 bridge_rtable_fini(struct bridge_softc *sc)
2865 KASSERT(sc->sc_brtcnt == 0,
2866 ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt));
2867 free(sc->sc_rthash, M_DEVBUF);
2871 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2872 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2874 #define mix(a, b, c) \
2876 a -= b; a -= c; a ^= (c >> 13); \
2877 b -= c; b -= a; b ^= (a << 8); \
2878 c -= a; c -= b; c ^= (b >> 13); \
2879 a -= b; a -= c; a ^= (c >> 12); \
2880 b -= c; b -= a; b ^= (a << 16); \
2881 c -= a; c -= b; c ^= (b >> 5); \
2882 a -= b; a -= c; a ^= (c >> 3); \
2883 b -= c; b -= a; b ^= (a << 10); \
2884 c -= a; c -= b; c ^= (b >> 15); \
2885 } while (/*CONSTCOND*/0)
2887 static __inline uint32_t
2888 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2890 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2901 return (c & BRIDGE_RTHASH_MASK);
2907 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2911 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2912 d = ((int)a[i]) - ((int)b[i]);
2919 * bridge_rtnode_lookup:
2921 * Look up a bridge route node for the specified destination. Compare the
2922 * vlan id or if zero then just return the first match.
2924 static struct bridge_rtnode *
2925 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2927 struct bridge_rtnode *brt;
2931 BRIDGE_LOCK_ASSERT(sc);
2933 hash = bridge_rthash(sc, addr);
2934 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2935 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2936 if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0))
2946 * bridge_rtnode_insert:
2948 * Insert the specified bridge node into the route table. We
2949 * assume the entry is not already in the table.
2952 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2954 struct bridge_rtnode *lbrt;
2958 BRIDGE_LOCK_ASSERT(sc);
2960 hash = bridge_rthash(sc, brt->brt_addr);
2962 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2964 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2969 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2970 if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan)
2973 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2976 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2977 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2980 lbrt = LIST_NEXT(lbrt, brt_hash);
2981 } while (lbrt != NULL);
2984 panic("bridge_rtnode_insert: impossible");
2988 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2995 * bridge_rtnode_destroy:
2997 * Destroy a bridge rtnode.
3000 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3002 BRIDGE_LOCK_ASSERT(sc);
3004 LIST_REMOVE(brt, brt_hash);
3006 LIST_REMOVE(brt, brt_list);
3008 brt->brt_dst->bif_addrcnt--;
3009 uma_zfree(bridge_rtnode_zone, brt);
3013 * bridge_rtable_expire:
3015 * Set the expiry time for all routes on an interface.
3018 bridge_rtable_expire(struct ifnet *ifp, int age)
3020 struct bridge_softc *sc = ifp->if_bridge;
3021 struct bridge_rtnode *brt;
3026 * If the age is zero then flush, otherwise set all the expiry times to
3027 * age for the interface
3030 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
3032 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
3033 /* Cap the expiry time to 'age' */
3034 if (brt->brt_ifp == ifp &&
3035 brt->brt_expire > time_uptime + age &&
3036 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
3037 brt->brt_expire = time_uptime + age;
3044 * bridge_state_change:
3046 * Callback from the bridgestp code when a port changes states.
3049 bridge_state_change(struct ifnet *ifp, int state)
3051 struct bridge_softc *sc = ifp->if_bridge;
3052 static const char *stpstates[] = {
3062 log(LOG_NOTICE, "%s: state changed to %s on %s\n",
3063 sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname);
3067 * Send bridge packets through pfil if they are one of the types pfil can deal
3068 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3069 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3073 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3075 int snap, error, i, hlen;
3076 struct ether_header *eh1, eh2;
3079 u_int16_t ether_type;
3082 error = -1; /* Default error if not error == 0 */
3085 /* we may return with the IP fields swapped, ensure its not shared */
3086 KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
3089 if (V_pfil_bridge == 0 && V_pfil_member == 0 && V_pfil_ipfw == 0)
3090 return (0); /* filtering is disabled */
3092 i = min((*mp)->m_pkthdr.len, max_protohdr);
3093 if ((*mp)->m_len < i) {
3094 *mp = m_pullup(*mp, i);
3096 printf("%s: m_pullup failed\n", __func__);
3101 eh1 = mtod(*mp, struct ether_header *);
3102 ether_type = ntohs(eh1->ether_type);
3105 * Check for SNAP/LLC.
3107 if (ether_type < ETHERMTU) {
3108 struct llc *llc2 = (struct llc *)(eh1 + 1);
3110 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3111 llc2->llc_dsap == LLC_SNAP_LSAP &&
3112 llc2->llc_ssap == LLC_SNAP_LSAP &&
3113 llc2->llc_control == LLC_UI) {
3114 ether_type = htons(llc2->llc_un.type_snap.ether_type);
3120 * If we're trying to filter bridge traffic, don't look at anything
3121 * other than IP and ARP traffic. If the filter doesn't understand
3122 * IPv6, don't allow IPv6 through the bridge either. This is lame
3123 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3124 * but of course we don't have an AppleTalk filter to begin with.
3125 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3128 switch (ether_type) {
3130 case ETHERTYPE_REVARP:
3131 if (V_pfil_ipfw_arp == 0)
3132 return (0); /* Automatically pass */
3137 case ETHERTYPE_IPV6:
3142 * Check to see if the user wants to pass non-ip
3143 * packets, these will not be checked by pfil(9) and
3144 * passed unconditionally so the default is to drop.
3150 /* Run the packet through pfil before stripping link headers */
3151 if (PFIL_HOOKED(&V_link_pfil_hook) && V_pfil_ipfw != 0 &&
3152 dir == PFIL_OUT && ifp != NULL) {
3154 error = pfil_run_hooks(&V_link_pfil_hook, mp, ifp, dir, NULL);
3156 if (*mp == NULL || error != 0) /* packet consumed by filter */
3160 /* Strip off the Ethernet header and keep a copy. */
3161 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3162 m_adj(*mp, ETHER_HDR_LEN);
3164 /* Strip off snap header, if present */
3166 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3167 m_adj(*mp, sizeof(struct llc));
3171 * Check the IP header for alignment and errors
3173 if (dir == PFIL_IN) {
3174 switch (ether_type) {
3176 error = bridge_ip_checkbasic(mp);
3179 case ETHERTYPE_IPV6:
3180 error = bridge_ip6_checkbasic(mp);
3193 * Run the packet through pfil
3195 switch (ether_type) {
3198 * Run pfil on the member interface and the bridge, both can
3199 * be skipped by clearing pfil_member or pfil_bridge.
3202 * in_if -> bridge_if -> out_if
3204 if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3205 error = pfil_run_hooks(&V_inet_pfil_hook, mp, bifp,
3208 if (*mp == NULL || error != 0) /* filter may consume */
3211 if (V_pfil_member && ifp != NULL)
3212 error = pfil_run_hooks(&V_inet_pfil_hook, mp, ifp,
3215 if (*mp == NULL || error != 0) /* filter may consume */
3218 if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL)
3219 error = pfil_run_hooks(&V_inet_pfil_hook, mp, bifp,
3222 if (*mp == NULL || error != 0) /* filter may consume */
3225 /* check if we need to fragment the packet */
3226 if (V_pfil_member && ifp != NULL && dir == PFIL_OUT) {
3227 i = (*mp)->m_pkthdr.len;
3228 if (i > ifp->if_mtu) {
3229 error = bridge_fragment(ifp, *mp, &eh2, snap,
3235 /* Recalculate the ip checksum. */
3236 ip = mtod(*mp, struct ip *);
3237 hlen = ip->ip_hl << 2;
3238 if (hlen < sizeof(struct ip))
3240 if (hlen > (*mp)->m_len) {
3241 if ((*mp = m_pullup(*mp, hlen)) == 0)
3243 ip = mtod(*mp, struct ip *);
3248 if (hlen == sizeof(struct ip))
3249 ip->ip_sum = in_cksum_hdr(ip);
3251 ip->ip_sum = in_cksum(*mp, hlen);
3255 case ETHERTYPE_IPV6:
3256 if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3257 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, bifp,
3260 if (*mp == NULL || error != 0) /* filter may consume */
3263 if (V_pfil_member && ifp != NULL)
3264 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, ifp,
3267 if (*mp == NULL || error != 0) /* filter may consume */
3270 if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL)
3271 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, bifp,
3288 * Finally, put everything back the way it was and return
3291 M_PREPEND(*mp, sizeof(struct llc), M_NOWAIT);
3294 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3297 M_PREPEND(*mp, ETHER_HDR_LEN, M_NOWAIT);
3300 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3311 * Perform basic checks on header size since
3312 * pfil assumes ip_input has already processed
3313 * it for it. Cut-and-pasted from ip_input.c.
3314 * Given how simple the IPv6 version is,
3315 * does the IPv4 version really need to be
3318 * XXX Should we update ipstat here, or not?
3319 * XXX Right now we update ipstat but not
3323 bridge_ip_checkbasic(struct mbuf **mp)
3325 struct mbuf *m = *mp;
3333 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3334 if ((m = m_copyup(m, sizeof(struct ip),
3335 (max_linkhdr + 3) & ~3)) == NULL) {
3336 /* XXXJRT new stat, please */
3337 KMOD_IPSTAT_INC(ips_toosmall);
3340 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
3341 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3342 KMOD_IPSTAT_INC(ips_toosmall);
3346 ip = mtod(m, struct ip *);
3347 if (ip == NULL) goto bad;
3349 if (ip->ip_v != IPVERSION) {
3350 KMOD_IPSTAT_INC(ips_badvers);
3353 hlen = ip->ip_hl << 2;
3354 if (hlen < sizeof(struct ip)) { /* minimum header length */
3355 KMOD_IPSTAT_INC(ips_badhlen);
3358 if (hlen > m->m_len) {
3359 if ((m = m_pullup(m, hlen)) == 0) {
3360 KMOD_IPSTAT_INC(ips_badhlen);
3363 ip = mtod(m, struct ip *);
3364 if (ip == NULL) goto bad;
3367 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3368 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3370 if (hlen == sizeof(struct ip)) {
3371 sum = in_cksum_hdr(ip);
3373 sum = in_cksum(m, hlen);
3377 KMOD_IPSTAT_INC(ips_badsum);
3381 /* Retrieve the packet length. */
3382 len = ntohs(ip->ip_len);
3385 * Check for additional length bogosity
3388 KMOD_IPSTAT_INC(ips_badlen);
3393 * Check that the amount of data in the buffers
3394 * is as at least much as the IP header would have us expect.
3395 * Drop packet if shorter than we expect.
3397 if (m->m_pkthdr.len < len) {
3398 KMOD_IPSTAT_INC(ips_tooshort);
3402 /* Checks out, proceed */
3413 * Same as above, but for IPv6.
3414 * Cut-and-pasted from ip6_input.c.
3415 * XXX Should we update ip6stat, or not?
3418 bridge_ip6_checkbasic(struct mbuf **mp)
3420 struct mbuf *m = *mp;
3421 struct ip6_hdr *ip6;
3424 * If the IPv6 header is not aligned, slurp it up into a new
3425 * mbuf with space for link headers, in the event we forward
3426 * it. Otherwise, if it is aligned, make sure the entire base
3427 * IPv6 header is in the first mbuf of the chain.
3429 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3430 struct ifnet *inifp = m->m_pkthdr.rcvif;
3431 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3432 (max_linkhdr + 3) & ~3)) == NULL) {
3433 /* XXXJRT new stat, please */
3434 IP6STAT_INC(ip6s_toosmall);
3435 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3438 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3439 struct ifnet *inifp = m->m_pkthdr.rcvif;
3440 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3441 IP6STAT_INC(ip6s_toosmall);
3442 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3447 ip6 = mtod(m, struct ip6_hdr *);
3449 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3450 IP6STAT_INC(ip6s_badvers);
3451 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3455 /* Checks out, proceed */
3468 * Return a fragmented mbuf chain.
3471 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
3472 int snap, struct llc *llc)
3478 if (m->m_len < sizeof(struct ip) &&
3479 (m = m_pullup(m, sizeof(struct ip))) == NULL)
3481 ip = mtod(m, struct ip *);
3483 m->m_pkthdr.csum_flags |= CSUM_IP;
3484 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist);
3488 /* walk the chain and re-add the Ethernet header */
3489 for (m0 = m; m0; m0 = m0->m_nextpkt) {
3492 M_PREPEND(m0, sizeof(struct llc), M_NOWAIT);
3497 bcopy(llc, mtod(m0, caddr_t),
3498 sizeof(struct llc));
3500 M_PREPEND(m0, ETHER_HDR_LEN, M_NOWAIT);
3505 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
3511 KMOD_IPSTAT_INC(ips_fragmented);
3522 bridge_linkstate(struct ifnet *ifp)
3524 struct bridge_softc *sc = ifp->if_bridge;
3525 struct bridge_iflist *bif;
3528 bif = bridge_lookup_member_if(sc, ifp);
3533 bridge_linkcheck(sc);
3536 bstp_linkstate(&bif->bif_stp);
3540 bridge_linkcheck(struct bridge_softc *sc)
3542 struct bridge_iflist *bif;
3543 int new_link, hasls;
3545 BRIDGE_LOCK_ASSERT(sc);
3546 new_link = LINK_STATE_DOWN;
3548 /* Our link is considered up if at least one of our ports is active */
3549 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
3550 if (bif->bif_ifp->if_capabilities & IFCAP_LINKSTATE)
3552 if (bif->bif_ifp->if_link_state == LINK_STATE_UP) {
3553 new_link = LINK_STATE_UP;
3557 if (!LIST_EMPTY(&sc->sc_iflist) && !hasls) {
3558 /* If no interfaces support link-state then we default to up */
3559 new_link = LINK_STATE_UP;
3561 if_link_state_change(sc->sc_ifp, new_link);