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>
85 #include <sys/malloc.h>
86 #include <sys/protosw.h>
87 #include <sys/systm.h>
90 #include <sys/socket.h> /* for net/if.h */
91 #include <sys/sockio.h>
92 #include <sys/ctype.h> /* string functions */
93 #include <sys/kernel.h>
94 #include <sys/random.h>
95 #include <sys/syslog.h>
96 #include <sys/sysctl.h>
98 #include <sys/module.h>
100 #include <sys/proc.h>
101 #include <sys/lock.h>
102 #include <sys/mutex.h>
106 #include <net/if_clone.h>
107 #include <net/if_dl.h>
108 #include <net/if_types.h>
109 #include <net/if_var.h>
110 #include <net/pfil.h>
111 #include <net/vnet.h>
113 #include <netinet/in.h> /* for struct arpcom */
114 #include <netinet/in_systm.h>
115 #include <netinet/in_var.h>
116 #include <netinet/ip.h>
117 #include <netinet/ip_var.h>
119 #include <netinet/ip6.h>
120 #include <netinet6/ip6_var.h>
121 #include <netinet6/in6_ifattach.h>
123 #if defined(INET) || defined(INET6)
124 #include <netinet/ip_carp.h>
126 #include <machine/in_cksum.h>
127 #include <netinet/if_ether.h> /* for struct arpcom */
128 #include <net/bridgestp.h>
129 #include <net/if_bridgevar.h>
130 #include <net/if_llc.h>
131 #include <net/if_vlan_var.h>
133 #include <net/route.h>
136 * Size of the route hash table. Must be a power of two.
138 #ifndef BRIDGE_RTHASH_SIZE
139 #define BRIDGE_RTHASH_SIZE 1024
142 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
145 * Default maximum number of addresses to cache.
147 #ifndef BRIDGE_RTABLE_MAX
148 #define BRIDGE_RTABLE_MAX 2000
152 * Timeout (in seconds) for entries learned dynamically.
154 #ifndef BRIDGE_RTABLE_TIMEOUT
155 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
159 * Number of seconds between walks of the route list.
161 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
162 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
166 * List of capabilities to possibly mask on the member interface.
168 #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 struct mtx bridge_list_mtx;
230 eventhandler_tag bridge_detach_cookie = NULL;
232 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
234 uma_zone_t bridge_rtnode_zone;
236 static int bridge_clone_create(struct if_clone *, int, caddr_t);
237 static void bridge_clone_destroy(struct ifnet *);
239 static int bridge_ioctl(struct ifnet *, u_long, caddr_t);
240 static void bridge_mutecaps(struct bridge_softc *);
241 static void bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *,
243 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
244 static void bridge_init(void *);
245 static void bridge_dummynet(struct mbuf *, struct ifnet *);
246 static void bridge_stop(struct ifnet *, int);
247 static int bridge_transmit(struct ifnet *, struct mbuf *);
248 static void bridge_qflush(struct ifnet *);
249 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
250 static int bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
252 static int bridge_enqueue(struct bridge_softc *, struct ifnet *,
254 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
256 static void bridge_forward(struct bridge_softc *, struct bridge_iflist *,
259 static void bridge_timer(void *);
261 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
263 static void bridge_span(struct bridge_softc *, struct mbuf *);
265 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
266 uint16_t, struct bridge_iflist *, int, uint8_t);
267 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *,
269 static void bridge_rttrim(struct bridge_softc *);
270 static void bridge_rtage(struct bridge_softc *);
271 static void bridge_rtflush(struct bridge_softc *, int);
272 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *,
275 static void bridge_rtable_init(struct bridge_softc *);
276 static void bridge_rtable_fini(struct bridge_softc *);
278 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
279 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
280 const uint8_t *, uint16_t);
281 static int bridge_rtnode_insert(struct bridge_softc *,
282 struct bridge_rtnode *);
283 static void bridge_rtnode_destroy(struct bridge_softc *,
284 struct bridge_rtnode *);
285 static void bridge_rtable_expire(struct ifnet *, int);
286 static void bridge_state_change(struct ifnet *, int);
288 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
290 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
292 static void bridge_delete_member(struct bridge_softc *,
293 struct bridge_iflist *, int);
294 static void bridge_delete_span(struct bridge_softc *,
295 struct bridge_iflist *);
297 static int bridge_ioctl_add(struct bridge_softc *, void *);
298 static int bridge_ioctl_del(struct bridge_softc *, void *);
299 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
300 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
301 static int bridge_ioctl_scache(struct bridge_softc *, void *);
302 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
303 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
304 static int bridge_ioctl_rts(struct bridge_softc *, void *);
305 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
306 static int bridge_ioctl_sto(struct bridge_softc *, void *);
307 static int bridge_ioctl_gto(struct bridge_softc *, void *);
308 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
309 static int bridge_ioctl_flush(struct bridge_softc *, void *);
310 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
311 static int bridge_ioctl_spri(struct bridge_softc *, void *);
312 static int bridge_ioctl_ght(struct bridge_softc *, void *);
313 static int bridge_ioctl_sht(struct bridge_softc *, void *);
314 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
315 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
316 static int bridge_ioctl_gma(struct bridge_softc *, void *);
317 static int bridge_ioctl_sma(struct bridge_softc *, void *);
318 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
319 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
320 static int bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *);
321 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
322 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
323 static int bridge_ioctl_gbparam(struct bridge_softc *, void *);
324 static int bridge_ioctl_grte(struct bridge_softc *, void *);
325 static int bridge_ioctl_gifsstp(struct bridge_softc *, void *);
326 static int bridge_ioctl_sproto(struct bridge_softc *, void *);
327 static int bridge_ioctl_stxhc(struct bridge_softc *, void *);
328 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
330 static int bridge_ip_checkbasic(struct mbuf **mp);
332 static int bridge_ip6_checkbasic(struct mbuf **mp);
334 static int bridge_fragment(struct ifnet *, struct mbuf **mp,
335 struct ether_header *, int, struct llc *);
336 static void bridge_linkstate(struct ifnet *ifp);
337 static void bridge_linkcheck(struct bridge_softc *sc);
339 extern void (*bridge_linkstate_p)(struct ifnet *ifp);
341 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
342 #define VLANTAGOF(_m) \
343 (_m->m_flags & M_VLANTAG) ? EVL_VLANOFTAG(_m->m_pkthdr.ether_vtag) : 1
345 static struct bstp_cb_ops bridge_ops = {
346 .bcb_state = bridge_state_change,
347 .bcb_rtage = bridge_rtable_expire
350 SYSCTL_DECL(_net_link);
351 static SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
353 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
354 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
355 static int pfil_member = 1; /* run pfil hooks on the member interface */
356 static int pfil_ipfw = 0; /* layer2 filter with ipfw */
357 static int pfil_ipfw_arp = 0; /* layer2 filter with ipfw */
358 static int pfil_local_phys = 0; /* run pfil hooks on the physical interface for
359 locally destined packets */
360 static int log_stp = 0; /* log STP state changes */
361 static int bridge_inherit_mac = 0; /* share MAC with first bridge member */
362 TUNABLE_INT("net.link.bridge.pfil_onlyip", &pfil_onlyip);
363 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
364 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
365 TUNABLE_INT("net.link.bridge.ipfw_arp", &pfil_ipfw_arp);
366 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp, CTLFLAG_RW,
367 &pfil_ipfw_arp, 0, "Filter ARP packets through IPFW layer2");
368 TUNABLE_INT("net.link.bridge.pfil_bridge", &pfil_bridge);
369 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
370 &pfil_bridge, 0, "Packet filter on the bridge interface");
371 TUNABLE_INT("net.link.bridge.pfil_member", &pfil_member);
372 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
373 &pfil_member, 0, "Packet filter on the member interface");
374 TUNABLE_INT("net.link.bridge.pfil_local_phys", &pfil_local_phys);
375 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys, CTLFLAG_RW,
377 "Packet filter on the physical interface for locally destined packets");
378 TUNABLE_INT("net.link.bridge.log_stp", &log_stp);
379 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, CTLFLAG_RW,
380 &log_stp, 0, "Log STP state changes");
381 TUNABLE_INT("net.link.bridge.inherit_mac", &bridge_inherit_mac);
382 SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac, CTLFLAG_RW,
383 &bridge_inherit_mac, 0,
384 "Inherit MAC address from the first bridge member");
386 static VNET_DEFINE(int, allow_llz_overlap) = 0;
387 #define V_allow_llz_overlap VNET(allow_llz_overlap)
388 SYSCTL_VNET_INT(_net_link_bridge, OID_AUTO, allow_llz_overlap, CTLFLAG_RW,
389 &VNET_NAME(allow_llz_overlap), 0, "Allow overlap of link-local scope "
390 "zones of a bridge interface and the member interfaces");
392 struct bridge_control {
393 int (*bc_func)(struct bridge_softc *, void *);
398 #define BC_F_COPYIN 0x01 /* copy arguments in */
399 #define BC_F_COPYOUT 0x02 /* copy arguments out */
400 #define BC_F_SUSER 0x04 /* do super-user check */
402 const struct bridge_control bridge_control_table[] = {
403 { bridge_ioctl_add, sizeof(struct ifbreq),
404 BC_F_COPYIN|BC_F_SUSER },
405 { bridge_ioctl_del, sizeof(struct ifbreq),
406 BC_F_COPYIN|BC_F_SUSER },
408 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
409 BC_F_COPYIN|BC_F_COPYOUT },
410 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
411 BC_F_COPYIN|BC_F_SUSER },
413 { bridge_ioctl_scache, sizeof(struct ifbrparam),
414 BC_F_COPYIN|BC_F_SUSER },
415 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
418 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
419 BC_F_COPYIN|BC_F_COPYOUT },
420 { bridge_ioctl_rts, sizeof(struct ifbaconf),
421 BC_F_COPYIN|BC_F_COPYOUT },
423 { bridge_ioctl_saddr, sizeof(struct ifbareq),
424 BC_F_COPYIN|BC_F_SUSER },
426 { bridge_ioctl_sto, sizeof(struct ifbrparam),
427 BC_F_COPYIN|BC_F_SUSER },
428 { bridge_ioctl_gto, sizeof(struct ifbrparam),
431 { bridge_ioctl_daddr, sizeof(struct ifbareq),
432 BC_F_COPYIN|BC_F_SUSER },
434 { bridge_ioctl_flush, sizeof(struct ifbreq),
435 BC_F_COPYIN|BC_F_SUSER },
437 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
439 { bridge_ioctl_spri, sizeof(struct ifbrparam),
440 BC_F_COPYIN|BC_F_SUSER },
442 { bridge_ioctl_ght, sizeof(struct ifbrparam),
444 { bridge_ioctl_sht, sizeof(struct ifbrparam),
445 BC_F_COPYIN|BC_F_SUSER },
447 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
449 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
450 BC_F_COPYIN|BC_F_SUSER },
452 { bridge_ioctl_gma, sizeof(struct ifbrparam),
454 { bridge_ioctl_sma, sizeof(struct ifbrparam),
455 BC_F_COPYIN|BC_F_SUSER },
457 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
458 BC_F_COPYIN|BC_F_SUSER },
460 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
461 BC_F_COPYIN|BC_F_SUSER },
463 { bridge_ioctl_addspan, sizeof(struct ifbreq),
464 BC_F_COPYIN|BC_F_SUSER },
465 { bridge_ioctl_delspan, sizeof(struct ifbreq),
466 BC_F_COPYIN|BC_F_SUSER },
468 { bridge_ioctl_gbparam, sizeof(struct ifbropreq),
471 { bridge_ioctl_grte, sizeof(struct ifbrparam),
474 { bridge_ioctl_gifsstp, sizeof(struct ifbpstpconf),
475 BC_F_COPYIN|BC_F_COPYOUT },
477 { bridge_ioctl_sproto, sizeof(struct ifbrparam),
478 BC_F_COPYIN|BC_F_SUSER },
480 { bridge_ioctl_stxhc, sizeof(struct ifbrparam),
481 BC_F_COPYIN|BC_F_SUSER },
483 { bridge_ioctl_sifmaxaddr, sizeof(struct ifbreq),
484 BC_F_COPYIN|BC_F_SUSER },
487 const int bridge_control_table_size =
488 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
490 LIST_HEAD(, bridge_softc) bridge_list;
492 static struct if_clone *bridge_cloner;
493 static const char bridge_name[] = "bridge";
496 bridge_modevent(module_t mod, int type, void *data)
501 mtx_init(&bridge_list_mtx, "if_bridge list", NULL, MTX_DEF);
502 bridge_cloner = if_clone_simple(bridge_name,
503 bridge_clone_create, bridge_clone_destroy, 0);
504 bridge_rtnode_zone = uma_zcreate("bridge_rtnode",
505 sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL,
507 LIST_INIT(&bridge_list);
508 bridge_input_p = bridge_input;
509 bridge_output_p = bridge_output;
510 bridge_dn_p = bridge_dummynet;
511 bridge_linkstate_p = bridge_linkstate;
512 bridge_detach_cookie = EVENTHANDLER_REGISTER(
513 ifnet_departure_event, bridge_ifdetach, NULL,
514 EVENTHANDLER_PRI_ANY);
517 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
518 bridge_detach_cookie);
519 if_clone_detach(bridge_cloner);
520 uma_zdestroy(bridge_rtnode_zone);
521 bridge_input_p = NULL;
522 bridge_output_p = NULL;
524 bridge_linkstate_p = NULL;
525 mtx_destroy(&bridge_list_mtx);
533 static moduledata_t bridge_mod = {
539 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
540 MODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1);
543 * handler for net.link.bridge.ipfw
546 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS)
548 int enable = pfil_ipfw;
551 error = sysctl_handle_int(oidp, &enable, 0, req);
552 enable = (enable) ? 1 : 0;
554 if (enable != pfil_ipfw) {
558 * Disable pfil so that ipfw doesnt run twice, if the user
559 * really wants both then they can re-enable pfil_bridge and/or
560 * pfil_member. Also allow non-ip packets as ipfw can filter by
572 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, CTLTYPE_INT|CTLFLAG_RW,
573 &pfil_ipfw, 0, &sysctl_pfil_ipfw, "I", "Layer2 filter with IPFW");
576 * bridge_clone_create:
578 * Create a new bridge instance.
581 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t params)
583 struct bridge_softc *sc, *sc2;
584 struct ifnet *bifp, *ifp;
586 unsigned long hostid;
588 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
589 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
595 BRIDGE_LOCK_INIT(sc);
596 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
597 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
599 /* Initialize our routing table. */
600 bridge_rtable_init(sc);
602 callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0);
604 LIST_INIT(&sc->sc_iflist);
605 LIST_INIT(&sc->sc_spanlist);
608 if_initname(ifp, bridge_name, unit);
609 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
610 ifp->if_ioctl = bridge_ioctl;
611 ifp->if_transmit = bridge_transmit;
612 ifp->if_qflush = bridge_qflush;
613 ifp->if_init = bridge_init;
614 ifp->if_type = IFT_BRIDGE;
617 * Generate an ethernet address with a locally administered address.
619 * Since we are using random ethernet addresses for the bridge, it is
620 * possible that we might have address collisions, so make sure that
621 * this hardware address isn't already in use on another bridge.
622 * The first try uses the hostid and falls back to arc4rand().
625 getcredhostid(curthread->td_ucred, &hostid);
627 if (fb || hostid == 0) {
628 arc4rand(sc->sc_defaddr, ETHER_ADDR_LEN, 1);
629 sc->sc_defaddr[0] &= ~1;/* clear multicast bit */
630 sc->sc_defaddr[0] |= 2; /* set the LAA bit */
632 sc->sc_defaddr[0] = 0x2;
633 sc->sc_defaddr[1] = (hostid >> 24) & 0xff;
634 sc->sc_defaddr[2] = (hostid >> 16) & 0xff;
635 sc->sc_defaddr[3] = (hostid >> 8 ) & 0xff;
636 sc->sc_defaddr[4] = hostid & 0xff;
637 sc->sc_defaddr[5] = ifp->if_dunit & 0xff;
642 mtx_lock(&bridge_list_mtx);
643 LIST_FOREACH(sc2, &bridge_list, sc_list) {
645 if (memcmp(sc->sc_defaddr,
646 IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
651 mtx_unlock(&bridge_list_mtx);
652 } while (retry == 1);
654 bstp_attach(&sc->sc_stp, &bridge_ops);
655 ether_ifattach(ifp, sc->sc_defaddr);
656 /* Now undo some of the damage... */
657 ifp->if_baudrate = 0;
658 ifp->if_type = IFT_BRIDGE;
660 mtx_lock(&bridge_list_mtx);
661 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
662 mtx_unlock(&bridge_list_mtx);
668 * bridge_clone_destroy:
670 * Destroy a bridge instance.
673 bridge_clone_destroy(struct ifnet *ifp)
675 struct bridge_softc *sc = ifp->if_softc;
676 struct bridge_iflist *bif;
681 ifp->if_flags &= ~IFF_UP;
683 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
684 bridge_delete_member(sc, bif, 0);
686 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
687 bridge_delete_span(sc, bif);
692 callout_drain(&sc->sc_brcallout);
694 mtx_lock(&bridge_list_mtx);
695 LIST_REMOVE(sc, sc_list);
696 mtx_unlock(&bridge_list_mtx);
698 bstp_detach(&sc->sc_stp);
702 /* Tear down the routing table. */
703 bridge_rtable_fini(sc);
705 BRIDGE_LOCK_DESTROY(sc);
712 * Handle a control request from the operator.
715 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
717 struct bridge_softc *sc = ifp->if_softc;
718 struct ifreq *ifr = (struct ifreq *)data;
719 struct bridge_iflist *bif;
720 struct thread *td = curthread;
722 struct ifbreq ifbreq;
723 struct ifbifconf ifbifconf;
724 struct ifbareq ifbareq;
725 struct ifbaconf ifbaconf;
726 struct ifbrparam ifbrparam;
727 struct ifbropreq ifbropreq;
729 struct ifdrv *ifd = (struct ifdrv *) data;
730 const struct bridge_control *bc;
741 if (ifd->ifd_cmd >= bridge_control_table_size) {
745 bc = &bridge_control_table[ifd->ifd_cmd];
747 if (cmd == SIOCGDRVSPEC &&
748 (bc->bc_flags & BC_F_COPYOUT) == 0) {
752 else if (cmd == SIOCSDRVSPEC &&
753 (bc->bc_flags & BC_F_COPYOUT) != 0) {
758 if (bc->bc_flags & BC_F_SUSER) {
759 error = priv_check(td, PRIV_NET_BRIDGE);
764 if (ifd->ifd_len != bc->bc_argsize ||
765 ifd->ifd_len > sizeof(args)) {
770 bzero(&args, sizeof(args));
771 if (bc->bc_flags & BC_F_COPYIN) {
772 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
778 error = (*bc->bc_func)(sc, &args);
783 if (bc->bc_flags & BC_F_COPYOUT)
784 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
789 if (!(ifp->if_flags & IFF_UP) &&
790 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
792 * If interface is marked down and it is running,
793 * then stop and disable it.
798 } else if ((ifp->if_flags & IFF_UP) &&
799 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
801 * If interface is marked up and it is stopped, then
809 if (ifr->ifr_mtu < 576) {
813 if (LIST_EMPTY(&sc->sc_iflist)) {
814 sc->sc_ifp->if_mtu = ifr->ifr_mtu;
818 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
819 if (bif->bif_ifp->if_mtu != ifr->ifr_mtu) {
820 log(LOG_NOTICE, "%s: invalid MTU: %lu(%s)"
821 " != %d\n", sc->sc_ifp->if_xname,
822 bif->bif_ifp->if_mtu,
823 bif->bif_ifp->if_xname, ifr->ifr_mtu);
829 sc->sc_ifp->if_mtu = ifr->ifr_mtu;
834 * drop the lock as ether_ioctl() will call bridge_start() and
835 * cause the lock to be recursed.
837 error = ether_ioctl(ifp, cmd, data);
847 * Clear or restore unwanted capabilities on the member interface
850 bridge_mutecaps(struct bridge_softc *sc)
852 struct bridge_iflist *bif;
855 /* Initial bitmask of capabilities to test */
856 mask = BRIDGE_IFCAPS_MASK;
858 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
859 /* Every member must support it or its disabled */
860 mask &= bif->bif_savedcaps;
863 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
864 enabled = bif->bif_ifp->if_capenable;
865 enabled &= ~BRIDGE_IFCAPS_STRIP;
866 /* strip off mask bits and enable them again if allowed */
867 enabled &= ~BRIDGE_IFCAPS_MASK;
869 bridge_set_ifcap(sc, bif, enabled);
875 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set)
877 struct ifnet *ifp = bif->bif_ifp;
881 bzero(&ifr, sizeof(ifr));
882 ifr.ifr_reqcap = set;
884 if (ifp->if_capenable != set) {
885 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
887 if_printf(sc->sc_ifp,
888 "error setting interface capabilities on %s\n",
894 * bridge_lookup_member:
896 * Lookup a bridge member interface.
898 static struct bridge_iflist *
899 bridge_lookup_member(struct bridge_softc *sc, const char *name)
901 struct bridge_iflist *bif;
904 BRIDGE_LOCK_ASSERT(sc);
906 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
908 if (strcmp(ifp->if_xname, name) == 0)
916 * bridge_lookup_member_if:
918 * Lookup a bridge member interface by ifnet*.
920 static struct bridge_iflist *
921 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
923 struct bridge_iflist *bif;
925 BRIDGE_LOCK_ASSERT(sc);
927 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
928 if (bif->bif_ifp == member_ifp)
936 * bridge_delete_member:
938 * Delete the specified member interface.
941 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
944 struct ifnet *ifs = bif->bif_ifp;
945 struct ifnet *fif = NULL;
947 BRIDGE_LOCK_ASSERT(sc);
949 if (bif->bif_flags & IFBIF_STP)
950 bstp_disable(&bif->bif_stp);
952 ifs->if_bridge = NULL;
954 LIST_REMOVE(bif, bif_next);
958 * If removing the interface that gave the bridge its mac address, set
959 * the mac address of the bridge to the address of the next member, or
960 * to its default address if no members are left.
962 if (bridge_inherit_mac && sc->sc_ifaddr == ifs) {
963 if (LIST_EMPTY(&sc->sc_iflist)) {
964 bcopy(sc->sc_defaddr,
965 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
966 sc->sc_ifaddr = NULL;
968 fif = LIST_FIRST(&sc->sc_iflist)->bif_ifp;
969 bcopy(IF_LLADDR(fif),
970 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
973 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
976 bridge_linkcheck(sc);
977 bridge_mutecaps(sc); /* recalcuate now this interface is removed */
978 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
979 KASSERT(bif->bif_addrcnt == 0,
980 ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt));
984 switch (ifs->if_type) {
988 * Take the interface out of promiscuous mode, but only
989 * if it was promiscuous in the first place. It might
990 * not be if we're in the bridge_ioctl_add() error path.
992 if (ifs->if_flags & IFF_PROMISC)
993 (void) ifpromisc(ifs, 0);
1001 panic("bridge_delete_member: impossible");
1005 /* reneable any interface capabilities */
1006 bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
1008 bstp_destroy(&bif->bif_stp); /* prepare to free */
1010 free(bif, M_DEVBUF);
1014 * bridge_delete_span:
1016 * Delete the specified span interface.
1019 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1021 BRIDGE_LOCK_ASSERT(sc);
1023 KASSERT(bif->bif_ifp->if_bridge == NULL,
1024 ("%s: not a span interface", __func__));
1026 LIST_REMOVE(bif, bif_next);
1027 free(bif, M_DEVBUF);
1031 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1033 struct ifbreq *req = arg;
1034 struct bridge_iflist *bif = NULL;
1038 ifs = ifunit(req->ifbr_ifsname);
1041 if (ifs->if_ioctl == NULL) /* must be supported */
1044 /* If it's in the span list, it can't be a member. */
1045 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1046 if (ifs == bif->bif_ifp)
1049 if (ifs->if_bridge == sc)
1052 if (ifs->if_bridge != NULL)
1055 switch (ifs->if_type) {
1059 /* permitted interface types */
1067 * Two valid inet6 addresses with link-local scope must not be
1068 * on the parent interface and the member interfaces at the
1069 * same time. This restriction is needed to prevent violation
1070 * of link-local scope zone. Attempts to add a member
1071 * interface which has inet6 addresses when the parent has
1072 * inet6 triggers removal of all inet6 addresses on the member
1076 /* Check if the parent interface has a link-local scope addr. */
1077 if (V_allow_llz_overlap == 0 &&
1078 in6ifa_llaonifp(sc->sc_ifp) != NULL) {
1080 * If any, remove all inet6 addresses from the member
1084 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1085 if (in6ifa_llaonifp(bif->bif_ifp)) {
1087 in6_ifdetach(bif->bif_ifp);
1089 if_printf(sc->sc_ifp,
1090 "IPv6 addresses on %s have been removed "
1091 "before adding it as a member to prevent "
1092 "IPv6 address scope violation.\n",
1093 bif->bif_ifp->if_xname);
1097 if (in6ifa_llaonifp(ifs)) {
1101 if_printf(sc->sc_ifp,
1102 "IPv6 addresses on %s have been removed "
1103 "before adding it as a member to prevent "
1104 "IPv6 address scope violation.\n",
1109 /* Allow the first Ethernet member to define the MTU */
1110 if (LIST_EMPTY(&sc->sc_iflist))
1111 sc->sc_ifp->if_mtu = ifs->if_mtu;
1112 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
1113 if_printf(sc->sc_ifp, "invalid MTU: %lu(%s) != %lu\n",
1114 ifs->if_mtu, ifs->if_xname, sc->sc_ifp->if_mtu);
1118 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1123 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
1124 bif->bif_savedcaps = ifs->if_capenable;
1127 * Assign the interface's MAC address to the bridge if it's the first
1128 * member and the MAC address of the bridge has not been changed from
1129 * the default randomly generated one.
1131 if (bridge_inherit_mac && LIST_EMPTY(&sc->sc_iflist) &&
1132 !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr, ETHER_ADDR_LEN)) {
1133 bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1134 sc->sc_ifaddr = ifs;
1135 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
1138 ifs->if_bridge = sc;
1139 bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
1141 * XXX: XLOCK HERE!?!
1143 * NOTE: insert_***HEAD*** should be safe for the traversals.
1145 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
1147 /* Set interface capabilities to the intersection set of all members */
1148 bridge_mutecaps(sc);
1149 bridge_linkcheck(sc);
1151 /* Place the interface into promiscuous mode */
1152 switch (ifs->if_type) {
1156 error = ifpromisc(ifs, 1);
1162 bridge_delete_member(sc, bif, 0);
1167 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1169 struct ifbreq *req = arg;
1170 struct bridge_iflist *bif;
1172 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1176 bridge_delete_member(sc, bif, 0);
1182 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1184 struct ifbreq *req = arg;
1185 struct bridge_iflist *bif;
1186 struct bstp_port *bp;
1188 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1193 req->ifbr_ifsflags = bif->bif_flags;
1194 req->ifbr_state = bp->bp_state;
1195 req->ifbr_priority = bp->bp_priority;
1196 req->ifbr_path_cost = bp->bp_path_cost;
1197 req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1198 req->ifbr_proto = bp->bp_protover;
1199 req->ifbr_role = bp->bp_role;
1200 req->ifbr_stpflags = bp->bp_flags;
1201 req->ifbr_addrcnt = bif->bif_addrcnt;
1202 req->ifbr_addrmax = bif->bif_addrmax;
1203 req->ifbr_addrexceeded = bif->bif_addrexceeded;
1205 /* Copy STP state options as flags */
1206 if (bp->bp_operedge)
1207 req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
1208 if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
1209 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
1210 if (bp->bp_ptp_link)
1211 req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
1212 if (bp->bp_flags & BSTP_PORT_AUTOPTP)
1213 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
1214 if (bp->bp_flags & BSTP_PORT_ADMEDGE)
1215 req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
1216 if (bp->bp_flags & BSTP_PORT_ADMCOST)
1217 req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
1222 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1224 struct ifbreq *req = arg;
1225 struct bridge_iflist *bif;
1226 struct bstp_port *bp;
1229 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1234 if (req->ifbr_ifsflags & IFBIF_SPAN)
1235 /* SPAN is readonly */
1238 if (req->ifbr_ifsflags & IFBIF_STP) {
1239 if ((bif->bif_flags & IFBIF_STP) == 0) {
1240 error = bstp_enable(&bif->bif_stp);
1245 if ((bif->bif_flags & IFBIF_STP) != 0)
1246 bstp_disable(&bif->bif_stp);
1249 /* Pass on STP flags */
1250 bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
1251 bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
1252 bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
1253 bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
1255 /* Save the bits relating to the bridge */
1256 bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK;
1262 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1264 struct ifbrparam *param = arg;
1266 sc->sc_brtmax = param->ifbrp_csize;
1273 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1275 struct ifbrparam *param = arg;
1277 param->ifbrp_csize = sc->sc_brtmax;
1283 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1285 struct ifbifconf *bifc = arg;
1286 struct bridge_iflist *bif;
1289 int count, buflen, len, error = 0;
1292 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
1294 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1297 buflen = sizeof(breq) * count;
1298 if (bifc->ifbic_len == 0) {
1299 bifc->ifbic_len = buflen;
1303 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1308 len = min(bifc->ifbic_len, buflen);
1309 bzero(&breq, sizeof(breq));
1310 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1311 if (len < sizeof(breq))
1314 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1315 sizeof(breq.ifbr_ifsname));
1316 /* Fill in the ifbreq structure */
1317 error = bridge_ioctl_gifflags(sc, &breq);
1320 memcpy(buf, &breq, sizeof(breq));
1322 buf += sizeof(breq);
1323 len -= sizeof(breq);
1325 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1326 if (len < sizeof(breq))
1329 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1330 sizeof(breq.ifbr_ifsname));
1331 breq.ifbr_ifsflags = bif->bif_flags;
1332 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1333 memcpy(buf, &breq, sizeof(breq));
1335 buf += sizeof(breq);
1336 len -= sizeof(breq);
1340 bifc->ifbic_len = sizeof(breq) * count;
1341 error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len);
1343 free(outbuf, M_TEMP);
1348 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1350 struct ifbaconf *bac = arg;
1351 struct bridge_rtnode *brt;
1352 struct ifbareq bareq;
1354 int count, buflen, len, error = 0;
1356 if (bac->ifbac_len == 0)
1360 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list)
1362 buflen = sizeof(bareq) * count;
1365 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1370 len = min(bac->ifbac_len, buflen);
1371 bzero(&bareq, sizeof(bareq));
1372 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1373 if (len < sizeof(bareq))
1375 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1376 sizeof(bareq.ifba_ifsname));
1377 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1378 bareq.ifba_vlan = brt->brt_vlan;
1379 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1380 time_uptime < brt->brt_expire)
1381 bareq.ifba_expire = brt->brt_expire - time_uptime;
1383 bareq.ifba_expire = 0;
1384 bareq.ifba_flags = brt->brt_flags;
1386 memcpy(buf, &bareq, sizeof(bareq));
1388 buf += sizeof(bareq);
1389 len -= sizeof(bareq);
1393 bac->ifbac_len = sizeof(bareq) * count;
1394 error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len);
1396 free(outbuf, M_TEMP);
1401 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1403 struct ifbareq *req = arg;
1404 struct bridge_iflist *bif;
1407 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1411 error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
1418 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1420 struct ifbrparam *param = arg;
1422 sc->sc_brttimeout = param->ifbrp_ctime;
1427 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1429 struct ifbrparam *param = arg;
1431 param->ifbrp_ctime = sc->sc_brttimeout;
1436 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1438 struct ifbareq *req = arg;
1440 return (bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan));
1444 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1446 struct ifbreq *req = arg;
1448 bridge_rtflush(sc, req->ifbr_ifsflags);
1453 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1455 struct ifbrparam *param = arg;
1456 struct bstp_state *bs = &sc->sc_stp;
1458 param->ifbrp_prio = bs->bs_bridge_priority;
1463 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1465 struct ifbrparam *param = arg;
1467 return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio));
1471 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1473 struct ifbrparam *param = arg;
1474 struct bstp_state *bs = &sc->sc_stp;
1476 param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
1481 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1483 struct ifbrparam *param = arg;
1485 return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime));
1489 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1491 struct ifbrparam *param = arg;
1492 struct bstp_state *bs = &sc->sc_stp;
1494 param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
1499 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1501 struct ifbrparam *param = arg;
1503 return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay));
1507 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1509 struct ifbrparam *param = arg;
1510 struct bstp_state *bs = &sc->sc_stp;
1512 param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
1517 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1519 struct ifbrparam *param = arg;
1521 return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage));
1525 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1527 struct ifbreq *req = arg;
1528 struct bridge_iflist *bif;
1530 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1534 return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority));
1538 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1540 struct ifbreq *req = arg;
1541 struct bridge_iflist *bif;
1543 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1547 return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost));
1551 bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg)
1553 struct ifbreq *req = arg;
1554 struct bridge_iflist *bif;
1556 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1560 bif->bif_addrmax = req->ifbr_addrmax;
1565 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1567 struct ifbreq *req = arg;
1568 struct bridge_iflist *bif = NULL;
1571 ifs = ifunit(req->ifbr_ifsname);
1575 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1576 if (ifs == bif->bif_ifp)
1579 if (ifs->if_bridge != NULL)
1582 switch (ifs->if_type) {
1591 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1596 bif->bif_flags = IFBIF_SPAN;
1598 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1604 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1606 struct ifbreq *req = arg;
1607 struct bridge_iflist *bif;
1610 ifs = ifunit(req->ifbr_ifsname);
1614 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1615 if (ifs == bif->bif_ifp)
1621 bridge_delete_span(sc, bif);
1627 bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg)
1629 struct ifbropreq *req = arg;
1630 struct bstp_state *bs = &sc->sc_stp;
1631 struct bstp_port *root_port;
1633 req->ifbop_maxage = bs->bs_bridge_max_age >> 8;
1634 req->ifbop_hellotime = bs->bs_bridge_htime >> 8;
1635 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;
1637 root_port = bs->bs_root_port;
1638 if (root_port == NULL)
1639 req->ifbop_root_port = 0;
1641 req->ifbop_root_port = root_port->bp_ifp->if_index;
1643 req->ifbop_holdcount = bs->bs_txholdcount;
1644 req->ifbop_priority = bs->bs_bridge_priority;
1645 req->ifbop_protocol = bs->bs_protover;
1646 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;
1647 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id;
1648 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id;
1649 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id;
1650 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;
1651 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;
1657 bridge_ioctl_grte(struct bridge_softc *sc, void *arg)
1659 struct ifbrparam *param = arg;
1661 param->ifbrp_cexceeded = sc->sc_brtexceeded;
1666 bridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg)
1668 struct ifbpstpconf *bifstp = arg;
1669 struct bridge_iflist *bif;
1670 struct bstp_port *bp;
1671 struct ifbpstpreq bpreq;
1673 int count, buflen, len, error = 0;
1676 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1677 if ((bif->bif_flags & IFBIF_STP) != 0)
1681 buflen = sizeof(bpreq) * count;
1682 if (bifstp->ifbpstp_len == 0) {
1683 bifstp->ifbpstp_len = buflen;
1688 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1693 len = min(bifstp->ifbpstp_len, buflen);
1694 bzero(&bpreq, sizeof(bpreq));
1695 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1696 if (len < sizeof(bpreq))
1699 if ((bif->bif_flags & IFBIF_STP) == 0)
1703 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff;
1704 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions;
1705 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost;
1706 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id;
1707 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id;
1708 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id;
1710 memcpy(buf, &bpreq, sizeof(bpreq));
1712 buf += sizeof(bpreq);
1713 len -= sizeof(bpreq);
1717 bifstp->ifbpstp_len = sizeof(bpreq) * count;
1718 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len);
1720 free(outbuf, M_TEMP);
1725 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
1727 struct ifbrparam *param = arg;
1729 return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto));
1733 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
1735 struct ifbrparam *param = arg;
1737 return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc));
1743 * Detach an interface from a bridge. Called when a member
1744 * interface is detaching.
1747 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1749 struct bridge_softc *sc = ifp->if_bridge;
1750 struct bridge_iflist *bif;
1752 if (ifp->if_flags & IFF_RENAMING)
1755 /* Check if the interface is a bridge member */
1759 bif = bridge_lookup_member_if(sc, ifp);
1761 bridge_delete_member(sc, bif, 1);
1767 /* Check if the interface is a span port */
1768 mtx_lock(&bridge_list_mtx);
1769 LIST_FOREACH(sc, &bridge_list, sc_list) {
1771 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1772 if (ifp == bif->bif_ifp) {
1773 bridge_delete_span(sc, bif);
1779 mtx_unlock(&bridge_list_mtx);
1785 * Initialize a bridge interface.
1788 bridge_init(void *xsc)
1790 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1791 struct ifnet *ifp = sc->sc_ifp;
1793 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1797 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1800 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1801 bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */
1809 * Stop the bridge interface.
1812 bridge_stop(struct ifnet *ifp, int disable)
1814 struct bridge_softc *sc = ifp->if_softc;
1816 BRIDGE_LOCK_ASSERT(sc);
1818 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1821 callout_stop(&sc->sc_brcallout);
1822 bstp_stop(&sc->sc_stp);
1824 bridge_rtflush(sc, IFBF_FLUSHDYN);
1826 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1832 * Enqueue a packet on a bridge member interface.
1836 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1842 /* We may be sending a fragment so traverse the mbuf */
1845 m->m_nextpkt = NULL;
1846 len = m->m_pkthdr.len;
1847 mflags = m->m_flags;
1850 * If underlying interface can not do VLAN tag insertion itself
1851 * then attach a packet tag that holds it.
1853 if ((m->m_flags & M_VLANTAG) &&
1854 (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) {
1855 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1858 "unable to prepend VLAN header\n");
1859 dst_ifp->if_oerrors++;
1862 m->m_flags &= ~M_VLANTAG;
1865 M_ASSERTPKTHDR(m); /* We shouldn't transmit mbuf without pkthdr */
1866 if ((err = dst_ifp->if_transmit(dst_ifp, m))) {
1868 sc->sc_ifp->if_oerrors++;
1872 sc->sc_ifp->if_opackets++;
1873 sc->sc_ifp->if_obytes += len;
1874 if (mflags & M_MCAST)
1875 sc->sc_ifp->if_omcasts++;
1884 * Receive a queued packet from dummynet and pass it on to the output
1887 * The mbuf has the Ethernet header already attached.
1890 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
1892 struct bridge_softc *sc;
1894 sc = ifp->if_bridge;
1897 * The packet didnt originate from a member interface. This should only
1898 * ever happen if a member interface is removed while packets are
1906 if (PFIL_HOOKED(&V_inet_pfil_hook)
1908 || PFIL_HOOKED(&V_inet6_pfil_hook)
1911 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
1917 bridge_enqueue(sc, ifp, m);
1923 * Send output from a bridge member interface. This
1924 * performs the bridging function for locally originated
1927 * The mbuf has the Ethernet header already attached. We must
1928 * enqueue or free the mbuf before returning.
1931 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1934 struct ether_header *eh;
1935 struct ifnet *dst_if;
1936 struct bridge_softc *sc;
1939 if (m->m_len < ETHER_HDR_LEN) {
1940 m = m_pullup(m, ETHER_HDR_LEN);
1945 eh = mtod(m, struct ether_header *);
1946 sc = ifp->if_bridge;
1947 vlan = VLANTAGOF(m);
1952 * If bridge is down, but the original output interface is up,
1953 * go ahead and send out that interface. Otherwise, the packet
1956 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1962 * If the packet is a multicast, or we don't know a better way to
1963 * get there, send to all interfaces.
1965 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1968 dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan);
1969 if (dst_if == NULL) {
1970 struct bridge_iflist *bif;
1972 int error = 0, used = 0;
1976 BRIDGE_LOCK2REF(sc, error);
1982 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1983 dst_if = bif->bif_ifp;
1985 if (dst_if->if_type == IFT_GIF)
1987 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
1991 * If this is not the original output interface,
1992 * and the interface is participating in spanning
1993 * tree, make sure the port is in a state that
1994 * allows forwarding.
1996 if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) &&
1997 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2000 if (LIST_NEXT(bif, bif_next) == NULL) {
2004 mc = m_copypacket(m, M_NOWAIT);
2006 sc->sc_ifp->if_oerrors++;
2011 bridge_enqueue(sc, dst_if, mc);
2021 * XXX Spanning tree consideration here?
2025 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2032 bridge_enqueue(sc, dst_if, m);
2039 * Do output on a bridge.
2043 bridge_transmit(struct ifnet *ifp, struct mbuf *m)
2045 struct bridge_softc *sc;
2046 struct ether_header *eh;
2047 struct ifnet *dst_if;
2052 ETHER_BPF_MTAP(ifp, m);
2054 eh = mtod(m, struct ether_header *);
2057 if (((m->m_flags & (M_BCAST|M_MCAST)) == 0) &&
2058 (dst_if = bridge_rtlookup(sc, eh->ether_dhost, 1)) != NULL) {
2060 error = bridge_enqueue(sc, dst_if, m);
2062 bridge_broadcast(sc, ifp, m, 0);
2068 * The ifp->if_qflush entry point for if_bridge(4) is no-op.
2071 bridge_qflush(struct ifnet *ifp __unused)
2078 * The forwarding function of the bridge.
2080 * NOTE: Releases the lock on return.
2083 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
2086 struct bridge_iflist *dbif;
2087 struct ifnet *src_if, *dst_if, *ifp;
2088 struct ether_header *eh;
2093 src_if = m->m_pkthdr.rcvif;
2097 ifp->if_ibytes += m->m_pkthdr.len;
2098 vlan = VLANTAGOF(m);
2100 if ((sbif->bif_flags & IFBIF_STP) &&
2101 sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2104 eh = mtod(m, struct ether_header *);
2105 dst = eh->ether_dhost;
2107 /* If the interface is learning, record the address. */
2108 if (sbif->bif_flags & IFBIF_LEARNING) {
2109 error = bridge_rtupdate(sc, eh->ether_shost, vlan,
2110 sbif, 0, IFBAF_DYNAMIC);
2112 * If the interface has addresses limits then deny any source
2113 * that is not in the cache.
2115 if (error && sbif->bif_addrmax)
2119 if ((sbif->bif_flags & IFBIF_STP) != 0 &&
2120 sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING)
2124 * At this point, the port either doesn't participate
2125 * in spanning tree or it is in the forwarding state.
2129 * If the packet is unicast, destined for someone on
2130 * "this" side of the bridge, drop it.
2132 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2133 dst_if = bridge_rtlookup(sc, dst, vlan);
2134 if (src_if == dst_if)
2138 * Check if its a reserved multicast address, any address
2139 * listed in 802.1D section 7.12.6 may not be forwarded by the
2141 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
2143 if (dst[0] == 0x01 && dst[1] == 0x80 &&
2144 dst[2] == 0xc2 && dst[3] == 0x00 &&
2145 dst[4] == 0x00 && dst[5] <= 0x0f)
2148 /* ...forward it to all interfaces. */
2154 * If we have a destination interface which is a member of our bridge,
2155 * OR this is a unicast packet, push it through the bpf(4) machinery.
2156 * For broadcast or multicast packets, don't bother because it will
2157 * be reinjected into ether_input. We do this before we pass the packets
2158 * through the pfil(9) framework, as it is possible that pfil(9) will
2159 * drop the packet, or possibly modify it, making it difficult to debug
2160 * firewall issues on the bridge.
2162 if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0)
2163 ETHER_BPF_MTAP(ifp, m);
2165 /* run the packet filter */
2166 if (PFIL_HOOKED(&V_inet_pfil_hook)
2168 || PFIL_HOOKED(&V_inet6_pfil_hook)
2172 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2179 if (dst_if == NULL) {
2180 bridge_broadcast(sc, src_if, m, 1);
2185 * At this point, we're dealing with a unicast frame
2186 * going to a different interface.
2188 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2191 dbif = bridge_lookup_member_if(sc, dst_if);
2193 /* Not a member of the bridge (anymore?) */
2196 /* Private segments can not talk to each other */
2197 if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)
2200 if ((dbif->bif_flags & IFBIF_STP) &&
2201 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2206 if (PFIL_HOOKED(&V_inet_pfil_hook)
2208 || PFIL_HOOKED(&V_inet6_pfil_hook)
2211 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2217 bridge_enqueue(sc, dst_if, m);
2228 * Receive input from a member interface. Queue the packet for
2229 * bridging if it is not for us.
2231 static struct mbuf *
2232 bridge_input(struct ifnet *ifp, struct mbuf *m)
2234 struct bridge_softc *sc = ifp->if_bridge;
2235 struct bridge_iflist *bif, *bif2;
2237 struct ether_header *eh;
2238 struct mbuf *mc, *mc2;
2242 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2246 vlan = VLANTAGOF(m);
2249 * Implement support for bridge monitoring. If this flag has been
2250 * set on this interface, discard the packet once we push it through
2251 * the bpf(4) machinery, but before we do, increment the byte and
2252 * packet counters associated with this interface.
2254 if ((bifp->if_flags & IFF_MONITOR) != 0) {
2255 m->m_pkthdr.rcvif = bifp;
2256 ETHER_BPF_MTAP(bifp, m);
2257 bifp->if_ipackets++;
2258 bifp->if_ibytes += m->m_pkthdr.len;
2263 bif = bridge_lookup_member_if(sc, ifp);
2269 eh = mtod(m, struct ether_header *);
2273 if (m->m_flags & (M_BCAST|M_MCAST)) {
2274 /* Tap off 802.1D packets; they do not get forwarded. */
2275 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2276 ETHER_ADDR_LEN) == 0) {
2277 bstp_input(&bif->bif_stp, ifp, m); /* consumes mbuf */
2282 if ((bif->bif_flags & IFBIF_STP) &&
2283 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2289 * Make a deep copy of the packet and enqueue the copy
2290 * for bridge processing; return the original packet for
2293 mc = m_dup(m, M_NOWAIT);
2299 /* Perform the bridge forwarding function with the copy. */
2300 bridge_forward(sc, bif, mc);
2303 * Reinject the mbuf as arriving on the bridge so we have a
2304 * chance at claiming multicast packets. We can not loop back
2305 * here from ether_input as a bridge is never a member of a
2308 KASSERT(bifp->if_bridge == NULL,
2309 ("loop created in bridge_input"));
2310 mc2 = m_dup(m, M_NOWAIT);
2312 /* Keep the layer3 header aligned */
2313 int i = min(mc2->m_pkthdr.len, max_protohdr);
2314 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2317 mc2->m_pkthdr.rcvif = bifp;
2318 (*bifp->if_input)(bifp, mc2);
2321 /* Return the original packet for local processing. */
2325 if ((bif->bif_flags & IFBIF_STP) &&
2326 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2331 #if (defined(INET) || defined(INET6))
2332 # define OR_CARP_CHECK_WE_ARE_DST(iface) \
2333 || ((iface)->if_carp \
2334 && (*carp_forus_p)((iface), eh->ether_dhost))
2335 # define OR_CARP_CHECK_WE_ARE_SRC(iface) \
2336 || ((iface)->if_carp \
2337 && (*carp_forus_p)((iface), eh->ether_shost))
2339 # define OR_CARP_CHECK_WE_ARE_DST(iface)
2340 # define OR_CARP_CHECK_WE_ARE_SRC(iface)
2344 # define OR_PFIL_HOOKED_INET6 \
2345 || PFIL_HOOKED(&V_inet6_pfil_hook)
2347 # define OR_PFIL_HOOKED_INET6
2350 #define GRAB_OUR_PACKETS(iface) \
2351 if ((iface)->if_type == IFT_GIF) \
2353 /* It is destined for us. */ \
2354 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, ETHER_ADDR_LEN) == 0 \
2355 OR_CARP_CHECK_WE_ARE_DST((iface)) \
2357 if ((iface)->if_type == IFT_BRIDGE) { \
2358 ETHER_BPF_MTAP(iface, m); \
2359 iface->if_ipackets++; \
2360 iface->if_ibytes += m->m_pkthdr.len; \
2361 /* Filter on the physical interface. */ \
2362 if (pfil_local_phys && \
2363 (PFIL_HOOKED(&V_inet_pfil_hook) \
2364 OR_PFIL_HOOKED_INET6)) { \
2365 if (bridge_pfil(&m, NULL, ifp, \
2366 PFIL_IN) != 0 || m == NULL) { \
2367 BRIDGE_UNLOCK(sc); \
2370 eh = mtod(m, struct ether_header *); \
2373 if (bif->bif_flags & IFBIF_LEARNING) { \
2374 error = bridge_rtupdate(sc, eh->ether_shost, \
2375 vlan, bif, 0, IFBAF_DYNAMIC); \
2376 if (error && bif->bif_addrmax) { \
2377 BRIDGE_UNLOCK(sc); \
2382 m->m_pkthdr.rcvif = iface; \
2383 BRIDGE_UNLOCK(sc); \
2387 /* We just received a packet that we sent out. */ \
2388 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, ETHER_ADDR_LEN) == 0 \
2389 OR_CARP_CHECK_WE_ARE_SRC((iface)) \
2391 BRIDGE_UNLOCK(sc); \
2397 * Unicast. Make sure it's not for the bridge.
2399 do { GRAB_OUR_PACKETS(bifp) } while (0);
2402 * Give a chance for ifp at first priority. This will help when the
2403 * packet comes through the interface like VLAN's with the same MACs
2404 * on several interfaces from the same bridge. This also will save
2405 * some CPU cycles in case the destination interface and the input
2406 * interface (eq ifp) are the same.
2408 do { GRAB_OUR_PACKETS(ifp) } while (0);
2410 /* Now check the all bridge members. */
2411 LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) {
2412 GRAB_OUR_PACKETS(bif2->bif_ifp)
2415 #undef OR_CARP_CHECK_WE_ARE_DST
2416 #undef OR_CARP_CHECK_WE_ARE_SRC
2417 #undef OR_PFIL_HOOKED_INET6
2418 #undef GRAB_OUR_PACKETS
2420 /* Perform the bridge forwarding function. */
2421 bridge_forward(sc, bif, m);
2429 * Send a frame to all interfaces that are members of
2430 * the bridge, except for the one on which the packet
2433 * NOTE: Releases the lock on return.
2436 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2437 struct mbuf *m, int runfilt)
2439 struct bridge_iflist *dbif, *sbif;
2441 struct ifnet *dst_if;
2442 int error = 0, used = 0, i;
2444 sbif = bridge_lookup_member_if(sc, src_if);
2446 BRIDGE_LOCK2REF(sc, error);
2452 /* Filter on the bridge interface before broadcasting */
2453 if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook)
2455 || PFIL_HOOKED(&V_inet6_pfil_hook)
2458 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
2464 LIST_FOREACH(dbif, &sc->sc_iflist, bif_next) {
2465 dst_if = dbif->bif_ifp;
2466 if (dst_if == src_if)
2469 /* Private segments can not talk to each other */
2470 if (sbif && (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE))
2473 if ((dbif->bif_flags & IFBIF_STP) &&
2474 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2477 if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 &&
2478 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2481 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2484 if (LIST_NEXT(dbif, bif_next) == NULL) {
2488 mc = m_dup(m, M_NOWAIT);
2490 sc->sc_ifp->if_oerrors++;
2496 * Filter on the output interface. Pass a NULL bridge interface
2497 * pointer so we do not redundantly filter on the bridge for
2498 * each interface we broadcast on.
2500 if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook)
2502 || PFIL_HOOKED(&V_inet6_pfil_hook)
2506 /* Keep the layer3 header aligned */
2507 i = min(mc->m_pkthdr.len, max_protohdr);
2508 mc = m_copyup(mc, i, ETHER_ALIGN);
2510 sc->sc_ifp->if_oerrors++;
2514 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2520 bridge_enqueue(sc, dst_if, mc);
2532 * Duplicate a packet out one or more interfaces that are in span mode,
2533 * the original mbuf is unmodified.
2536 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2538 struct bridge_iflist *bif;
2539 struct ifnet *dst_if;
2542 if (LIST_EMPTY(&sc->sc_spanlist))
2545 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2546 dst_if = bif->bif_ifp;
2548 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2551 mc = m_copypacket(m, M_NOWAIT);
2553 sc->sc_ifp->if_oerrors++;
2557 bridge_enqueue(sc, dst_if, mc);
2564 * Add a bridge routing entry.
2567 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan,
2568 struct bridge_iflist *bif, int setflags, uint8_t flags)
2570 struct bridge_rtnode *brt;
2573 BRIDGE_LOCK_ASSERT(sc);
2575 /* Check the source address is valid and not multicast. */
2576 if (ETHER_IS_MULTICAST(dst) ||
2577 (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
2578 dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0)
2581 /* 802.1p frames map to vlan 1 */
2586 * A route for this destination might already exist. If so,
2587 * update it, otherwise create a new one.
2589 if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) {
2590 if (sc->sc_brtcnt >= sc->sc_brtmax) {
2591 sc->sc_brtexceeded++;
2594 /* Check per interface address limits (if enabled) */
2595 if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) {
2596 bif->bif_addrexceeded++;
2601 * Allocate a new bridge forwarding node, and
2602 * initialize the expiration time and Ethernet
2605 brt = uma_zalloc(bridge_rtnode_zone, M_NOWAIT | M_ZERO);
2609 if (bif->bif_flags & IFBIF_STICKY)
2610 brt->brt_flags = IFBAF_STICKY;
2612 brt->brt_flags = IFBAF_DYNAMIC;
2614 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2615 brt->brt_vlan = vlan;
2617 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2618 uma_zfree(bridge_rtnode_zone, brt);
2625 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2626 brt->brt_dst != bif) {
2627 brt->brt_dst->bif_addrcnt--;
2629 brt->brt_dst->bif_addrcnt++;
2632 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2633 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2635 brt->brt_flags = flags;
2643 * Lookup the destination interface for an address.
2645 static struct ifnet *
2646 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2648 struct bridge_rtnode *brt;
2650 BRIDGE_LOCK_ASSERT(sc);
2652 if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL)
2655 return (brt->brt_ifp);
2661 * Trim the routine table so that we have a number
2662 * of routing entries less than or equal to the
2666 bridge_rttrim(struct bridge_softc *sc)
2668 struct bridge_rtnode *brt, *nbrt;
2670 BRIDGE_LOCK_ASSERT(sc);
2672 /* Make sure we actually need to do this. */
2673 if (sc->sc_brtcnt <= sc->sc_brtmax)
2676 /* Force an aging cycle; this might trim enough addresses. */
2678 if (sc->sc_brtcnt <= sc->sc_brtmax)
2681 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2682 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2683 bridge_rtnode_destroy(sc, brt);
2684 if (sc->sc_brtcnt <= sc->sc_brtmax)
2693 * Aging timer for the bridge.
2696 bridge_timer(void *arg)
2698 struct bridge_softc *sc = arg;
2700 BRIDGE_LOCK_ASSERT(sc);
2704 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
2705 callout_reset(&sc->sc_brcallout,
2706 bridge_rtable_prune_period * hz, bridge_timer, sc);
2712 * Perform an aging cycle.
2715 bridge_rtage(struct bridge_softc *sc)
2717 struct bridge_rtnode *brt, *nbrt;
2719 BRIDGE_LOCK_ASSERT(sc);
2721 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2722 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2723 if (time_uptime >= brt->brt_expire)
2724 bridge_rtnode_destroy(sc, brt);
2732 * Remove all dynamic addresses from the bridge.
2735 bridge_rtflush(struct bridge_softc *sc, int full)
2737 struct bridge_rtnode *brt, *nbrt;
2739 BRIDGE_LOCK_ASSERT(sc);
2741 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2742 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2743 bridge_rtnode_destroy(sc, brt);
2750 * Remove an address from the table.
2753 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2755 struct bridge_rtnode *brt;
2758 BRIDGE_LOCK_ASSERT(sc);
2761 * If vlan is zero then we want to delete for all vlans so the lookup
2762 * may return more than one.
2764 while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) {
2765 bridge_rtnode_destroy(sc, brt);
2769 return (found ? 0 : ENOENT);
2775 * Delete routes to a speicifc member interface.
2778 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2780 struct bridge_rtnode *brt, *nbrt;
2782 BRIDGE_LOCK_ASSERT(sc);
2784 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2785 if (brt->brt_ifp == ifp && (full ||
2786 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2787 bridge_rtnode_destroy(sc, brt);
2792 * bridge_rtable_init:
2794 * Initialize the route table for this bridge.
2797 bridge_rtable_init(struct bridge_softc *sc)
2801 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2802 M_DEVBUF, M_WAITOK);
2804 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2805 LIST_INIT(&sc->sc_rthash[i]);
2807 sc->sc_rthash_key = arc4random();
2808 LIST_INIT(&sc->sc_rtlist);
2812 * bridge_rtable_fini:
2814 * Deconstruct the route table for this bridge.
2817 bridge_rtable_fini(struct bridge_softc *sc)
2820 KASSERT(sc->sc_brtcnt == 0,
2821 ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt));
2822 free(sc->sc_rthash, M_DEVBUF);
2826 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2827 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2829 #define mix(a, b, c) \
2831 a -= b; a -= c; a ^= (c >> 13); \
2832 b -= c; b -= a; b ^= (a << 8); \
2833 c -= a; c -= b; c ^= (b >> 13); \
2834 a -= b; a -= c; a ^= (c >> 12); \
2835 b -= c; b -= a; b ^= (a << 16); \
2836 c -= a; c -= b; c ^= (b >> 5); \
2837 a -= b; a -= c; a ^= (c >> 3); \
2838 b -= c; b -= a; b ^= (a << 10); \
2839 c -= a; c -= b; c ^= (b >> 15); \
2840 } while (/*CONSTCOND*/0)
2842 static __inline uint32_t
2843 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2845 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2856 return (c & BRIDGE_RTHASH_MASK);
2862 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2866 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2867 d = ((int)a[i]) - ((int)b[i]);
2874 * bridge_rtnode_lookup:
2876 * Look up a bridge route node for the specified destination. Compare the
2877 * vlan id or if zero then just return the first match.
2879 static struct bridge_rtnode *
2880 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2882 struct bridge_rtnode *brt;
2886 BRIDGE_LOCK_ASSERT(sc);
2888 hash = bridge_rthash(sc, addr);
2889 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2890 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2891 if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0))
2901 * bridge_rtnode_insert:
2903 * Insert the specified bridge node into the route table. We
2904 * assume the entry is not already in the table.
2907 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2909 struct bridge_rtnode *lbrt;
2913 BRIDGE_LOCK_ASSERT(sc);
2915 hash = bridge_rthash(sc, brt->brt_addr);
2917 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2919 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2924 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2925 if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan)
2928 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2931 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2932 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2935 lbrt = LIST_NEXT(lbrt, brt_hash);
2936 } while (lbrt != NULL);
2939 panic("bridge_rtnode_insert: impossible");
2943 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2950 * bridge_rtnode_destroy:
2952 * Destroy a bridge rtnode.
2955 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2957 BRIDGE_LOCK_ASSERT(sc);
2959 LIST_REMOVE(brt, brt_hash);
2961 LIST_REMOVE(brt, brt_list);
2963 brt->brt_dst->bif_addrcnt--;
2964 uma_zfree(bridge_rtnode_zone, brt);
2968 * bridge_rtable_expire:
2970 * Set the expiry time for all routes on an interface.
2973 bridge_rtable_expire(struct ifnet *ifp, int age)
2975 struct bridge_softc *sc = ifp->if_bridge;
2976 struct bridge_rtnode *brt;
2981 * If the age is zero then flush, otherwise set all the expiry times to
2982 * age for the interface
2985 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
2987 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
2988 /* Cap the expiry time to 'age' */
2989 if (brt->brt_ifp == ifp &&
2990 brt->brt_expire > time_uptime + age &&
2991 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2992 brt->brt_expire = time_uptime + age;
2999 * bridge_state_change:
3001 * Callback from the bridgestp code when a port changes states.
3004 bridge_state_change(struct ifnet *ifp, int state)
3006 struct bridge_softc *sc = ifp->if_bridge;
3007 static const char *stpstates[] = {
3017 log(LOG_NOTICE, "%s: state changed to %s on %s\n",
3018 sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname);
3022 * Send bridge packets through pfil if they are one of the types pfil can deal
3023 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3024 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3028 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3030 int snap, error, i, hlen;
3031 struct ether_header *eh1, eh2;
3034 u_int16_t ether_type;
3037 error = -1; /* Default error if not error == 0 */
3040 /* we may return with the IP fields swapped, ensure its not shared */
3041 KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
3044 if (pfil_bridge == 0 && pfil_member == 0 && pfil_ipfw == 0)
3045 return (0); /* filtering is disabled */
3047 i = min((*mp)->m_pkthdr.len, max_protohdr);
3048 if ((*mp)->m_len < i) {
3049 *mp = m_pullup(*mp, i);
3051 printf("%s: m_pullup failed\n", __func__);
3056 eh1 = mtod(*mp, struct ether_header *);
3057 ether_type = ntohs(eh1->ether_type);
3060 * Check for SNAP/LLC.
3062 if (ether_type < ETHERMTU) {
3063 struct llc *llc2 = (struct llc *)(eh1 + 1);
3065 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3066 llc2->llc_dsap == LLC_SNAP_LSAP &&
3067 llc2->llc_ssap == LLC_SNAP_LSAP &&
3068 llc2->llc_control == LLC_UI) {
3069 ether_type = htons(llc2->llc_un.type_snap.ether_type);
3075 * If we're trying to filter bridge traffic, don't look at anything
3076 * other than IP and ARP traffic. If the filter doesn't understand
3077 * IPv6, don't allow IPv6 through the bridge either. This is lame
3078 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3079 * but of course we don't have an AppleTalk filter to begin with.
3080 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3083 switch (ether_type) {
3085 case ETHERTYPE_REVARP:
3086 if (pfil_ipfw_arp == 0)
3087 return (0); /* Automatically pass */
3092 case ETHERTYPE_IPV6:
3097 * Check to see if the user wants to pass non-ip
3098 * packets, these will not be checked by pfil(9) and
3099 * passed unconditionally so the default is to drop.
3105 /* Run the packet through pfil before stripping link headers */
3106 if (PFIL_HOOKED(&V_link_pfil_hook) && pfil_ipfw != 0 &&
3107 dir == PFIL_OUT && ifp != NULL) {
3109 error = pfil_run_hooks(&V_link_pfil_hook, mp, ifp, dir, NULL);
3111 if (*mp == NULL || error != 0) /* packet consumed by filter */
3115 /* Strip off the Ethernet header and keep a copy. */
3116 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3117 m_adj(*mp, ETHER_HDR_LEN);
3119 /* Strip off snap header, if present */
3121 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3122 m_adj(*mp, sizeof(struct llc));
3126 * Check the IP header for alignment and errors
3128 if (dir == PFIL_IN) {
3129 switch (ether_type) {
3131 error = bridge_ip_checkbasic(mp);
3134 case ETHERTYPE_IPV6:
3135 error = bridge_ip6_checkbasic(mp);
3148 * Run the packet through pfil
3150 switch (ether_type) {
3153 * Run pfil on the member interface and the bridge, both can
3154 * be skipped by clearing pfil_member or pfil_bridge.
3157 * in_if -> bridge_if -> out_if
3159 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3160 error = pfil_run_hooks(&V_inet_pfil_hook, mp, bifp,
3163 if (*mp == NULL || error != 0) /* filter may consume */
3166 if (pfil_member && ifp != NULL)
3167 error = pfil_run_hooks(&V_inet_pfil_hook, mp, ifp,
3170 if (*mp == NULL || error != 0) /* filter may consume */
3173 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3174 error = pfil_run_hooks(&V_inet_pfil_hook, mp, bifp,
3177 if (*mp == NULL || error != 0) /* filter may consume */
3180 /* check if we need to fragment the packet */
3181 /* bridge_fragment generates a mbuf chain of packets */
3182 /* that already include eth headers */
3183 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3184 i = (*mp)->m_pkthdr.len;
3185 if (i > ifp->if_mtu) {
3186 error = bridge_fragment(ifp, mp, &eh2, snap,
3192 /* Recalculate the ip checksum. */
3193 ip = mtod(*mp, struct ip *);
3194 hlen = ip->ip_hl << 2;
3195 if (hlen < sizeof(struct ip))
3197 if (hlen > (*mp)->m_len) {
3198 if ((*mp = m_pullup(*mp, hlen)) == 0)
3200 ip = mtod(*mp, struct ip *);
3205 if (hlen == sizeof(struct ip))
3206 ip->ip_sum = in_cksum_hdr(ip);
3208 ip->ip_sum = in_cksum(*mp, hlen);
3212 case ETHERTYPE_IPV6:
3213 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3214 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, bifp,
3217 if (*mp == NULL || error != 0) /* filter may consume */
3220 if (pfil_member && ifp != NULL)
3221 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, ifp,
3224 if (*mp == NULL || error != 0) /* filter may consume */
3227 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3228 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, bifp,
3245 * Finally, put everything back the way it was and return
3248 M_PREPEND(*mp, sizeof(struct llc), M_NOWAIT);
3251 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3254 M_PREPEND(*mp, ETHER_HDR_LEN, M_NOWAIT);
3257 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3268 * Perform basic checks on header size since
3269 * pfil assumes ip_input has already processed
3270 * it for it. Cut-and-pasted from ip_input.c.
3271 * Given how simple the IPv6 version is,
3272 * does the IPv4 version really need to be
3275 * XXX Should we update ipstat here, or not?
3276 * XXX Right now we update ipstat but not
3280 bridge_ip_checkbasic(struct mbuf **mp)
3282 struct mbuf *m = *mp;
3290 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3291 if ((m = m_copyup(m, sizeof(struct ip),
3292 (max_linkhdr + 3) & ~3)) == NULL) {
3293 /* XXXJRT new stat, please */
3294 KMOD_IPSTAT_INC(ips_toosmall);
3297 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
3298 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3299 KMOD_IPSTAT_INC(ips_toosmall);
3303 ip = mtod(m, struct ip *);
3304 if (ip == NULL) goto bad;
3306 if (ip->ip_v != IPVERSION) {
3307 KMOD_IPSTAT_INC(ips_badvers);
3310 hlen = ip->ip_hl << 2;
3311 if (hlen < sizeof(struct ip)) { /* minimum header length */
3312 KMOD_IPSTAT_INC(ips_badhlen);
3315 if (hlen > m->m_len) {
3316 if ((m = m_pullup(m, hlen)) == 0) {
3317 KMOD_IPSTAT_INC(ips_badhlen);
3320 ip = mtod(m, struct ip *);
3321 if (ip == NULL) goto bad;
3324 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3325 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3327 if (hlen == sizeof(struct ip)) {
3328 sum = in_cksum_hdr(ip);
3330 sum = in_cksum(m, hlen);
3334 KMOD_IPSTAT_INC(ips_badsum);
3338 /* Retrieve the packet length. */
3339 len = ntohs(ip->ip_len);
3342 * Check for additional length bogosity
3345 KMOD_IPSTAT_INC(ips_badlen);
3350 * Check that the amount of data in the buffers
3351 * is as at least much as the IP header would have us expect.
3352 * Drop packet if shorter than we expect.
3354 if (m->m_pkthdr.len < len) {
3355 KMOD_IPSTAT_INC(ips_tooshort);
3359 /* Checks out, proceed */
3370 * Same as above, but for IPv6.
3371 * Cut-and-pasted from ip6_input.c.
3372 * XXX Should we update ip6stat, or not?
3375 bridge_ip6_checkbasic(struct mbuf **mp)
3377 struct mbuf *m = *mp;
3378 struct ip6_hdr *ip6;
3381 * If the IPv6 header is not aligned, slurp it up into a new
3382 * mbuf with space for link headers, in the event we forward
3383 * it. Otherwise, if it is aligned, make sure the entire base
3384 * IPv6 header is in the first mbuf of the chain.
3386 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3387 struct ifnet *inifp = m->m_pkthdr.rcvif;
3388 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3389 (max_linkhdr + 3) & ~3)) == NULL) {
3390 /* XXXJRT new stat, please */
3391 IP6STAT_INC(ip6s_toosmall);
3392 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3395 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3396 struct ifnet *inifp = m->m_pkthdr.rcvif;
3397 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3398 IP6STAT_INC(ip6s_toosmall);
3399 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3404 ip6 = mtod(m, struct ip6_hdr *);
3406 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3407 IP6STAT_INC(ip6s_badvers);
3408 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3412 /* Checks out, proceed */
3425 * Fragment mbuf chain in multiple packets and prepend ethernet header.
3428 bridge_fragment(struct ifnet *ifp, struct mbuf **mp, struct ether_header *eh,
3429 int snap, struct llc *llc)
3431 struct mbuf *m = *mp, *nextpkt = NULL, *mprev = NULL, *mcur = NULL;
3435 if (m->m_len < sizeof(struct ip) &&
3436 (m = m_pullup(m, sizeof(struct ip))) == NULL)
3438 ip = mtod(m, struct ip *);
3440 m->m_pkthdr.csum_flags |= CSUM_IP;
3441 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist);
3446 * Walk the chain and re-add the Ethernet header for
3449 for (mcur = m; mcur; mcur = mcur->m_nextpkt) {
3450 nextpkt = mcur->m_nextpkt;
3451 mcur->m_nextpkt = NULL;
3453 M_PREPEND(mcur, sizeof(struct llc), M_NOWAIT);
3457 mprev->m_nextpkt = nextpkt;
3460 bcopy(llc, mtod(mcur, caddr_t),sizeof(struct llc));
3463 M_PREPEND(mcur, ETHER_HDR_LEN, M_NOWAIT);
3467 mprev->m_nextpkt = nextpkt;
3470 bcopy(eh, mtod(mcur, caddr_t), ETHER_HDR_LEN);
3473 * The previous two M_PREPEND could have inserted one or two
3474 * mbufs in front so we have to update the previous packet's
3477 mcur->m_nextpkt = nextpkt;
3479 mprev->m_nextpkt = mcur;
3481 /* The first mbuf in the original chain needs to be
3488 KMOD_IPSTAT_INC(ips_fragmented);
3492 for (mcur = *mp; mcur; mcur = m) { /* droping the full packet chain */
3493 m = mcur->m_nextpkt;
3500 bridge_linkstate(struct ifnet *ifp)
3502 struct bridge_softc *sc = ifp->if_bridge;
3503 struct bridge_iflist *bif;
3506 bif = bridge_lookup_member_if(sc, ifp);
3511 bridge_linkcheck(sc);
3514 bstp_linkstate(&bif->bif_stp);
3518 bridge_linkcheck(struct bridge_softc *sc)
3520 struct bridge_iflist *bif;
3521 int new_link, hasls;
3523 BRIDGE_LOCK_ASSERT(sc);
3524 new_link = LINK_STATE_DOWN;
3526 /* Our link is considered up if at least one of our ports is active */
3527 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
3528 if (bif->bif_ifp->if_capabilities & IFCAP_LINKSTATE)
3530 if (bif->bif_ifp->if_link_state == LINK_STATE_UP) {
3531 new_link = LINK_STATE_UP;
3535 if (!LIST_EMPTY(&sc->sc_iflist) && !hasls) {
3536 /* If no interfaces support link-state then we default to up */
3537 new_link = LINK_STATE_UP;
3539 if_link_state_change(sc->sc_ifp, new_link);