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"
84 #include <sys/param.h>
86 #include <sys/malloc.h>
87 #include <sys/protosw.h>
88 #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>
103 #include <sys/rwlock.h>
104 #include <sys/vimage.h>
108 #include <net/if_clone.h>
109 #include <net/if_dl.h>
110 #include <net/if_types.h>
111 #include <net/if_var.h>
112 #include <net/pfil.h>
114 #include <netinet/in.h> /* for struct arpcom */
115 #include <netinet/in_systm.h>
116 #include <netinet/in_var.h>
117 #include <netinet/ip.h>
118 #include <netinet/ip_var.h>
119 #include <netinet/vinet.h>
121 #include <netinet/ip6.h>
122 #include <netinet6/ip6_var.h>
123 #include <netinet6/vinet6.h>
126 #include <netinet/ip_carp.h>
128 #include <machine/in_cksum.h>
129 #include <netinet/if_ether.h> /* for struct arpcom */
130 #include <net/bridgestp.h>
131 #include <net/if_bridgevar.h>
132 #include <net/if_llc.h>
133 #include <net/if_vlan_var.h>
135 #include <net/route.h>
136 #include <netinet/ip_fw.h>
137 #include <netinet/ip_dummynet.h>
140 * Size of the route hash table. Must be a power of two.
142 #ifndef BRIDGE_RTHASH_SIZE
143 #define BRIDGE_RTHASH_SIZE 1024
146 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
149 * Maximum number of addresses to cache.
151 #ifndef BRIDGE_RTABLE_MAX
152 #define BRIDGE_RTABLE_MAX 100
156 * Timeout (in seconds) for entries learned dynamically.
158 #ifndef BRIDGE_RTABLE_TIMEOUT
159 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
163 * Number of seconds between walks of the route list.
165 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
166 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
170 * List of capabilities to possibly mask on the member interface.
172 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM)
175 * Bridge interface list entry.
177 struct bridge_iflist {
178 LIST_ENTRY(bridge_iflist) bif_next;
179 struct ifnet *bif_ifp; /* member if */
180 struct bstp_port bif_stp; /* STP state */
181 uint32_t bif_flags; /* member if flags */
182 int bif_savedcaps; /* saved capabilities */
183 uint32_t bif_addrmax; /* max # of addresses */
184 uint32_t bif_addrcnt; /* cur. # of addresses */
185 uint32_t bif_addrexceeded;/* # of address violations */
191 struct bridge_rtnode {
192 LIST_ENTRY(bridge_rtnode) brt_hash; /* hash table linkage */
193 LIST_ENTRY(bridge_rtnode) brt_list; /* list linkage */
194 struct bridge_iflist *brt_dst; /* destination if */
195 unsigned long brt_expire; /* expiration time */
196 uint8_t brt_flags; /* address flags */
197 uint8_t brt_addr[ETHER_ADDR_LEN];
198 uint16_t brt_vlan; /* vlan id */
200 #define brt_ifp brt_dst->bif_ifp
203 * Software state for each bridge.
205 struct bridge_softc {
206 struct ifnet *sc_ifp; /* make this an interface */
207 LIST_ENTRY(bridge_softc) sc_list;
210 uint32_t sc_brtmax; /* max # of addresses */
211 uint32_t sc_brtcnt; /* cur. # of addresses */
212 uint32_t sc_brttimeout; /* rt timeout in seconds */
213 struct callout sc_brcallout; /* bridge callout */
214 uint32_t sc_iflist_ref; /* refcount for sc_iflist */
215 uint32_t sc_iflist_xcnt; /* refcount for sc_iflist */
216 LIST_HEAD(, bridge_iflist) sc_iflist; /* member interface list */
217 LIST_HEAD(, bridge_rtnode) *sc_rthash; /* our forwarding table */
218 LIST_HEAD(, bridge_rtnode) sc_rtlist; /* list version of above */
219 uint32_t sc_rthash_key; /* key for hash */
220 LIST_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */
221 struct bstp_state sc_stp; /* STP state */
222 uint32_t sc_brtexceeded; /* # of cache drops */
223 struct ifnet *sc_ifaddr; /* member mac copied from */
224 u_char sc_defaddr[6]; /* Default MAC address */
227 static struct mtx bridge_list_mtx;
228 eventhandler_tag bridge_detach_cookie = NULL;
230 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
232 uma_zone_t bridge_rtnode_zone;
234 static int bridge_clone_create(struct if_clone *, int, caddr_t);
235 static void bridge_clone_destroy(struct ifnet *);
237 static int bridge_ioctl(struct ifnet *, u_long, caddr_t);
238 static void bridge_mutecaps(struct bridge_softc *);
239 static void bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *,
241 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
242 static void bridge_init(void *);
243 static void bridge_dummynet(struct mbuf *, struct ifnet *);
244 static void bridge_stop(struct ifnet *, int);
245 static void bridge_start(struct ifnet *);
246 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
247 static int bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
249 static void bridge_enqueue(struct bridge_softc *, struct ifnet *,
251 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
253 static void bridge_forward(struct bridge_softc *, struct bridge_iflist *,
256 static void bridge_timer(void *);
258 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
260 static void bridge_span(struct bridge_softc *, struct mbuf *);
262 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
263 uint16_t, struct bridge_iflist *, int, uint8_t);
264 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *,
266 static void bridge_rttrim(struct bridge_softc *);
267 static void bridge_rtage(struct bridge_softc *);
268 static void bridge_rtflush(struct bridge_softc *, int);
269 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *,
272 static int bridge_rtable_init(struct bridge_softc *);
273 static void bridge_rtable_fini(struct bridge_softc *);
275 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
276 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
277 const uint8_t *, uint16_t);
278 static int bridge_rtnode_insert(struct bridge_softc *,
279 struct bridge_rtnode *);
280 static void bridge_rtnode_destroy(struct bridge_softc *,
281 struct bridge_rtnode *);
282 static void bridge_rtable_expire(struct ifnet *, int);
283 static void bridge_state_change(struct ifnet *, int);
285 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
287 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
289 static void bridge_delete_member(struct bridge_softc *,
290 struct bridge_iflist *, int);
291 static void bridge_delete_span(struct bridge_softc *,
292 struct bridge_iflist *);
294 static int bridge_ioctl_add(struct bridge_softc *, void *);
295 static int bridge_ioctl_del(struct bridge_softc *, void *);
296 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
297 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
298 static int bridge_ioctl_scache(struct bridge_softc *, void *);
299 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
300 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
301 static int bridge_ioctl_rts(struct bridge_softc *, void *);
302 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
303 static int bridge_ioctl_sto(struct bridge_softc *, void *);
304 static int bridge_ioctl_gto(struct bridge_softc *, void *);
305 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
306 static int bridge_ioctl_flush(struct bridge_softc *, void *);
307 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
308 static int bridge_ioctl_spri(struct bridge_softc *, void *);
309 static int bridge_ioctl_ght(struct bridge_softc *, void *);
310 static int bridge_ioctl_sht(struct bridge_softc *, void *);
311 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
312 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
313 static int bridge_ioctl_gma(struct bridge_softc *, void *);
314 static int bridge_ioctl_sma(struct bridge_softc *, void *);
315 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
316 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
317 static int bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *);
318 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
319 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
320 static int bridge_ioctl_gbparam(struct bridge_softc *, void *);
321 static int bridge_ioctl_grte(struct bridge_softc *, void *);
322 static int bridge_ioctl_gifsstp(struct bridge_softc *, void *);
323 static int bridge_ioctl_sproto(struct bridge_softc *, void *);
324 static int bridge_ioctl_stxhc(struct bridge_softc *, void *);
325 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
327 static int bridge_ip_checkbasic(struct mbuf **mp);
329 static int bridge_ip6_checkbasic(struct mbuf **mp);
331 static int bridge_fragment(struct ifnet *, struct mbuf *,
332 struct ether_header *, int, struct llc *);
334 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
335 #define VLANTAGOF(_m) \
336 (_m->m_flags & M_VLANTAG) ? EVL_VLANOFTAG(_m->m_pkthdr.ether_vtag) : 1
338 static struct bstp_cb_ops bridge_ops = {
339 .bcb_state = bridge_state_change,
340 .bcb_rtage = bridge_rtable_expire
343 SYSCTL_DECL(_net_link);
344 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
346 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
347 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
348 static int pfil_member = 1; /* run pfil hooks on the member interface */
349 static int pfil_ipfw = 0; /* layer2 filter with ipfw */
350 static int pfil_ipfw_arp = 0; /* layer2 filter with ipfw */
351 static int pfil_local_phys = 0; /* run pfil hooks on the physical interface for
352 locally destined packets */
353 static int log_stp = 0; /* log STP state changes */
354 static int bridge_inherit_mac = 0; /* share MAC with first bridge member */
355 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
356 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
357 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp, CTLFLAG_RW,
358 &pfil_ipfw_arp, 0, "Filter ARP packets through IPFW layer2");
359 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
360 &pfil_bridge, 0, "Packet filter on the bridge interface");
361 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
362 &pfil_member, 0, "Packet filter on the member interface");
363 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys, CTLFLAG_RW,
365 "Packet filter on the physical interface for locally destined packets");
366 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, CTLFLAG_RW,
367 &log_stp, 0, "Log STP state changes");
368 SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac, CTLFLAG_RW,
369 &bridge_inherit_mac, 0,
370 "Inherit MAC address from the first bridge member");
372 struct bridge_control {
373 int (*bc_func)(struct bridge_softc *, void *);
378 #define BC_F_COPYIN 0x01 /* copy arguments in */
379 #define BC_F_COPYOUT 0x02 /* copy arguments out */
380 #define BC_F_SUSER 0x04 /* do super-user check */
382 const struct bridge_control bridge_control_table[] = {
383 { bridge_ioctl_add, sizeof(struct ifbreq),
384 BC_F_COPYIN|BC_F_SUSER },
385 { bridge_ioctl_del, sizeof(struct ifbreq),
386 BC_F_COPYIN|BC_F_SUSER },
388 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
389 BC_F_COPYIN|BC_F_COPYOUT },
390 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
391 BC_F_COPYIN|BC_F_SUSER },
393 { bridge_ioctl_scache, sizeof(struct ifbrparam),
394 BC_F_COPYIN|BC_F_SUSER },
395 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
398 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
399 BC_F_COPYIN|BC_F_COPYOUT },
400 { bridge_ioctl_rts, sizeof(struct ifbaconf),
401 BC_F_COPYIN|BC_F_COPYOUT },
403 { bridge_ioctl_saddr, sizeof(struct ifbareq),
404 BC_F_COPYIN|BC_F_SUSER },
406 { bridge_ioctl_sto, sizeof(struct ifbrparam),
407 BC_F_COPYIN|BC_F_SUSER },
408 { bridge_ioctl_gto, sizeof(struct ifbrparam),
411 { bridge_ioctl_daddr, sizeof(struct ifbareq),
412 BC_F_COPYIN|BC_F_SUSER },
414 { bridge_ioctl_flush, sizeof(struct ifbreq),
415 BC_F_COPYIN|BC_F_SUSER },
417 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
419 { bridge_ioctl_spri, sizeof(struct ifbrparam),
420 BC_F_COPYIN|BC_F_SUSER },
422 { bridge_ioctl_ght, sizeof(struct ifbrparam),
424 { bridge_ioctl_sht, sizeof(struct ifbrparam),
425 BC_F_COPYIN|BC_F_SUSER },
427 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
429 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
430 BC_F_COPYIN|BC_F_SUSER },
432 { bridge_ioctl_gma, sizeof(struct ifbrparam),
434 { bridge_ioctl_sma, sizeof(struct ifbrparam),
435 BC_F_COPYIN|BC_F_SUSER },
437 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
438 BC_F_COPYIN|BC_F_SUSER },
440 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
441 BC_F_COPYIN|BC_F_SUSER },
443 { bridge_ioctl_addspan, sizeof(struct ifbreq),
444 BC_F_COPYIN|BC_F_SUSER },
445 { bridge_ioctl_delspan, sizeof(struct ifbreq),
446 BC_F_COPYIN|BC_F_SUSER },
448 { bridge_ioctl_gbparam, sizeof(struct ifbropreq),
451 { bridge_ioctl_grte, sizeof(struct ifbrparam),
454 { bridge_ioctl_gifsstp, sizeof(struct ifbpstpconf),
455 BC_F_COPYIN|BC_F_COPYOUT },
457 { bridge_ioctl_sproto, sizeof(struct ifbrparam),
458 BC_F_COPYIN|BC_F_SUSER },
460 { bridge_ioctl_stxhc, sizeof(struct ifbrparam),
461 BC_F_COPYIN|BC_F_SUSER },
463 { bridge_ioctl_sifmaxaddr, sizeof(struct ifbreq),
464 BC_F_COPYIN|BC_F_SUSER },
467 const int bridge_control_table_size =
468 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
470 LIST_HEAD(, bridge_softc) bridge_list;
472 IFC_SIMPLE_DECLARE(bridge, 0);
475 bridge_modevent(module_t mod, int type, void *data)
480 mtx_init(&bridge_list_mtx, "if_bridge list", NULL, MTX_DEF);
481 if_clone_attach(&bridge_cloner);
482 bridge_rtnode_zone = uma_zcreate("bridge_rtnode",
483 sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL,
485 LIST_INIT(&bridge_list);
486 bridge_input_p = bridge_input;
487 bridge_output_p = bridge_output;
488 bridge_dn_p = bridge_dummynet;
489 bridge_detach_cookie = EVENTHANDLER_REGISTER(
490 ifnet_departure_event, bridge_ifdetach, NULL,
491 EVENTHANDLER_PRI_ANY);
494 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
495 bridge_detach_cookie);
496 if_clone_detach(&bridge_cloner);
497 uma_zdestroy(bridge_rtnode_zone);
498 bridge_input_p = NULL;
499 bridge_output_p = NULL;
501 mtx_destroy(&bridge_list_mtx);
509 static moduledata_t bridge_mod = {
515 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
516 MODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1);
519 * handler for net.link.bridge.pfil_ipfw
522 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS)
524 int enable = pfil_ipfw;
527 error = sysctl_handle_int(oidp, &enable, 0, req);
528 enable = (enable) ? 1 : 0;
530 if (enable != pfil_ipfw) {
534 * Disable pfil so that ipfw doesnt run twice, if the user
535 * really wants both then they can re-enable pfil_bridge and/or
536 * pfil_member. Also allow non-ip packets as ipfw can filter by
548 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, CTLTYPE_INT|CTLFLAG_RW,
549 &pfil_ipfw, 0, &sysctl_pfil_ipfw, "I", "Layer2 filter with IPFW");
552 * bridge_clone_create:
554 * Create a new bridge instance.
557 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t params)
559 struct bridge_softc *sc, *sc2;
560 struct ifnet *bifp, *ifp;
563 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
564 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
570 BRIDGE_LOCK_INIT(sc);
571 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
572 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
574 /* Initialize our routing table. */
575 bridge_rtable_init(sc);
577 callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0);
579 LIST_INIT(&sc->sc_iflist);
580 LIST_INIT(&sc->sc_spanlist);
583 if_initname(ifp, ifc->ifc_name, unit);
584 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
585 ifp->if_ioctl = bridge_ioctl;
586 ifp->if_start = bridge_start;
587 ifp->if_init = bridge_init;
588 ifp->if_type = IFT_BRIDGE;
589 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
590 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
591 IFQ_SET_READY(&ifp->if_snd);
594 * Generate a random ethernet address with a locally administered
597 * Since we are using random ethernet addresses for the bridge, it is
598 * possible that we might have address collisions, so make sure that
599 * this hardware address isn't already in use on another bridge.
601 for (retry = 1; retry != 0;) {
602 arc4rand(sc->sc_defaddr, ETHER_ADDR_LEN, 1);
603 sc->sc_defaddr[0] &= ~1; /* clear multicast bit */
604 sc->sc_defaddr[0] |= 2; /* set the LAA bit */
606 mtx_lock(&bridge_list_mtx);
607 LIST_FOREACH(sc2, &bridge_list, sc_list) {
609 if (memcmp(sc->sc_defaddr,
610 IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0)
613 mtx_unlock(&bridge_list_mtx);
616 bstp_attach(&sc->sc_stp, &bridge_ops);
617 ether_ifattach(ifp, sc->sc_defaddr);
618 /* Now undo some of the damage... */
619 ifp->if_baudrate = 0;
620 ifp->if_type = IFT_BRIDGE;
622 mtx_lock(&bridge_list_mtx);
623 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
624 mtx_unlock(&bridge_list_mtx);
630 * bridge_clone_destroy:
632 * Destroy a bridge instance.
635 bridge_clone_destroy(struct ifnet *ifp)
637 struct bridge_softc *sc = ifp->if_softc;
638 struct bridge_iflist *bif;
643 ifp->if_flags &= ~IFF_UP;
645 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
646 bridge_delete_member(sc, bif, 0);
648 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
649 bridge_delete_span(sc, bif);
654 callout_drain(&sc->sc_brcallout);
656 mtx_lock(&bridge_list_mtx);
657 LIST_REMOVE(sc, sc_list);
658 mtx_unlock(&bridge_list_mtx);
660 bstp_detach(&sc->sc_stp);
662 if_free_type(ifp, IFT_ETHER);
664 /* Tear down the routing table. */
665 bridge_rtable_fini(sc);
667 BRIDGE_LOCK_DESTROY(sc);
674 * Handle a control request from the operator.
677 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
679 struct bridge_softc *sc = ifp->if_softc;
680 struct thread *td = curthread;
682 struct ifbreq ifbreq;
683 struct ifbifconf ifbifconf;
684 struct ifbareq ifbareq;
685 struct ifbaconf ifbaconf;
686 struct ifbrparam ifbrparam;
687 struct ifbropreq ifbropreq;
689 struct ifdrv *ifd = (struct ifdrv *) data;
690 const struct bridge_control *bc;
701 if (ifd->ifd_cmd >= bridge_control_table_size) {
705 bc = &bridge_control_table[ifd->ifd_cmd];
707 if (cmd == SIOCGDRVSPEC &&
708 (bc->bc_flags & BC_F_COPYOUT) == 0) {
712 else if (cmd == SIOCSDRVSPEC &&
713 (bc->bc_flags & BC_F_COPYOUT) != 0) {
718 if (bc->bc_flags & BC_F_SUSER) {
719 error = priv_check(td, PRIV_NET_BRIDGE);
724 if (ifd->ifd_len != bc->bc_argsize ||
725 ifd->ifd_len > sizeof(args)) {
730 bzero(&args, sizeof(args));
731 if (bc->bc_flags & BC_F_COPYIN) {
732 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
738 error = (*bc->bc_func)(sc, &args);
743 if (bc->bc_flags & BC_F_COPYOUT)
744 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
749 if (!(ifp->if_flags & IFF_UP) &&
750 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
752 * If interface is marked down and it is running,
753 * then stop and disable it.
758 } else if ((ifp->if_flags & IFF_UP) &&
759 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
761 * If interface is marked up and it is stopped, then
769 /* Do not allow the MTU to be changed on the bridge */
775 * drop the lock as ether_ioctl() will call bridge_start() and
776 * cause the lock to be recursed.
778 error = ether_ioctl(ifp, cmd, data);
788 * Clear or restore unwanted capabilities on the member interface
791 bridge_mutecaps(struct bridge_softc *sc)
793 struct bridge_iflist *bif;
796 /* Initial bitmask of capabilities to test */
797 mask = BRIDGE_IFCAPS_MASK;
799 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
800 /* Every member must support it or its disabled */
801 mask &= bif->bif_savedcaps;
804 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
805 enabled = bif->bif_ifp->if_capenable;
806 /* strip off mask bits and enable them again if allowed */
807 enabled &= ~BRIDGE_IFCAPS_MASK;
810 * Receive offload can only be enabled if all members also
811 * support send offload.
813 if ((enabled & IFCAP_TSO) == 0)
814 enabled &= ~IFCAP_LRO;
816 bridge_set_ifcap(sc, bif, enabled);
822 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set)
824 struct ifnet *ifp = bif->bif_ifp;
828 bzero(&ifr, sizeof(ifr));
829 ifr.ifr_reqcap = set;
831 if (ifp->if_capenable != set) {
832 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
834 if_printf(sc->sc_ifp,
835 "error setting interface capabilities on %s\n",
841 * bridge_lookup_member:
843 * Lookup a bridge member interface.
845 static struct bridge_iflist *
846 bridge_lookup_member(struct bridge_softc *sc, const char *name)
848 struct bridge_iflist *bif;
851 BRIDGE_LOCK_ASSERT(sc);
853 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
855 if (strcmp(ifp->if_xname, name) == 0)
863 * bridge_lookup_member_if:
865 * Lookup a bridge member interface by ifnet*.
867 static struct bridge_iflist *
868 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
870 struct bridge_iflist *bif;
872 BRIDGE_LOCK_ASSERT(sc);
874 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
875 if (bif->bif_ifp == member_ifp)
883 * bridge_delete_member:
885 * Delete the specified member interface.
888 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
891 struct ifnet *ifs = bif->bif_ifp;
892 struct ifnet *fif = NULL;
894 BRIDGE_LOCK_ASSERT(sc);
896 if (bif->bif_flags & IFBIF_STP)
897 bstp_disable(&bif->bif_stp);
899 ifs->if_bridge = NULL;
901 LIST_REMOVE(bif, bif_next);
905 * If removing the interface that gave the bridge its mac address, set
906 * the mac address of the bridge to the address of the next member, or
907 * to its default address if no members are left.
909 if (bridge_inherit_mac && sc->sc_ifaddr == ifs) {
910 if (LIST_EMPTY(&sc->sc_iflist)) {
911 bcopy(sc->sc_defaddr,
912 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
913 sc->sc_ifaddr = NULL;
915 fif = LIST_FIRST(&sc->sc_iflist)->bif_ifp;
916 bcopy(IF_LLADDR(fif),
917 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
922 bridge_mutecaps(sc); /* recalcuate now this interface is removed */
923 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
924 KASSERT(bif->bif_addrcnt == 0,
925 ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt));
929 switch (ifs->if_type) {
933 * Take the interface out of promiscuous mode.
935 (void) ifpromisc(ifs, 0);
943 panic("bridge_delete_member: impossible");
947 /* reneable any interface capabilities */
948 bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
950 bstp_destroy(&bif->bif_stp); /* prepare to free */
956 * bridge_delete_span:
958 * Delete the specified span interface.
961 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
963 BRIDGE_LOCK_ASSERT(sc);
965 KASSERT(bif->bif_ifp->if_bridge == NULL,
966 ("%s: not a span interface", __func__));
968 LIST_REMOVE(bif, bif_next);
973 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
975 struct ifbreq *req = arg;
976 struct bridge_iflist *bif = NULL;
980 ifs = ifunit(req->ifbr_ifsname);
983 if (ifs->if_ioctl == NULL) /* must be supported */
986 /* If it's in the span list, it can't be a member. */
987 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
988 if (ifs == bif->bif_ifp)
991 /* Allow the first Ethernet member to define the MTU */
992 if (ifs->if_type != IFT_GIF) {
993 if (LIST_EMPTY(&sc->sc_iflist))
994 sc->sc_ifp->if_mtu = ifs->if_mtu;
995 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
996 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
1002 if (ifs->if_bridge == sc)
1005 if (ifs->if_bridge != NULL)
1008 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1013 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
1014 bif->bif_savedcaps = ifs->if_capenable;
1016 switch (ifs->if_type) {
1020 /* permitted interface types */
1028 * Assign the interface's MAC address to the bridge if it's the first
1029 * member and the MAC address of the bridge has not been changed from
1030 * the default randomly generated one.
1032 if (bridge_inherit_mac && LIST_EMPTY(&sc->sc_iflist) &&
1033 !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr, ETHER_ADDR_LEN)) {
1034 bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1035 sc->sc_ifaddr = ifs;
1038 ifs->if_bridge = sc;
1039 bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
1041 * XXX: XLOCK HERE!?!
1043 * NOTE: insert_***HEAD*** should be safe for the traversals.
1045 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
1047 /* Set interface capabilities to the intersection set of all members */
1048 bridge_mutecaps(sc);
1050 switch (ifs->if_type) {
1054 * Place the interface into promiscuous mode.
1057 error = ifpromisc(ifs, 1);
1062 bridge_delete_member(sc, bif, 0);
1066 free(bif, M_DEVBUF);
1072 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1074 struct ifbreq *req = arg;
1075 struct bridge_iflist *bif;
1077 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1081 bridge_delete_member(sc, bif, 0);
1087 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1089 struct ifbreq *req = arg;
1090 struct bridge_iflist *bif;
1091 struct bstp_port *bp;
1093 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1098 req->ifbr_ifsflags = bif->bif_flags;
1099 req->ifbr_state = bp->bp_state;
1100 req->ifbr_priority = bp->bp_priority;
1101 req->ifbr_path_cost = bp->bp_path_cost;
1102 req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1103 req->ifbr_proto = bp->bp_protover;
1104 req->ifbr_role = bp->bp_role;
1105 req->ifbr_stpflags = bp->bp_flags;
1106 req->ifbr_addrcnt = bif->bif_addrcnt;
1107 req->ifbr_addrmax = bif->bif_addrmax;
1108 req->ifbr_addrexceeded = bif->bif_addrexceeded;
1110 /* Copy STP state options as flags */
1111 if (bp->bp_operedge)
1112 req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
1113 if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
1114 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
1115 if (bp->bp_ptp_link)
1116 req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
1117 if (bp->bp_flags & BSTP_PORT_AUTOPTP)
1118 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
1119 if (bp->bp_flags & BSTP_PORT_ADMEDGE)
1120 req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
1121 if (bp->bp_flags & BSTP_PORT_ADMCOST)
1122 req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
1127 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1129 struct ifbreq *req = arg;
1130 struct bridge_iflist *bif;
1131 struct bstp_port *bp;
1134 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1139 if (req->ifbr_ifsflags & IFBIF_SPAN)
1140 /* SPAN is readonly */
1143 if (req->ifbr_ifsflags & IFBIF_STP) {
1144 if ((bif->bif_flags & IFBIF_STP) == 0) {
1145 error = bstp_enable(&bif->bif_stp);
1150 if ((bif->bif_flags & IFBIF_STP) != 0)
1151 bstp_disable(&bif->bif_stp);
1154 /* Pass on STP flags */
1155 bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
1156 bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
1157 bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
1158 bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
1160 /* Save the bits relating to the bridge */
1161 bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK;
1167 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1169 struct ifbrparam *param = arg;
1171 sc->sc_brtmax = param->ifbrp_csize;
1178 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1180 struct ifbrparam *param = arg;
1182 param->ifbrp_csize = sc->sc_brtmax;
1188 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1190 struct ifbifconf *bifc = arg;
1191 struct bridge_iflist *bif;
1194 int count, buflen, len, error = 0;
1197 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
1199 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1202 buflen = sizeof(breq) * count;
1203 if (bifc->ifbic_len == 0) {
1204 bifc->ifbic_len = buflen;
1208 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1213 len = min(bifc->ifbic_len, buflen);
1214 bzero(&breq, sizeof(breq));
1215 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1216 if (len < sizeof(breq))
1219 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1220 sizeof(breq.ifbr_ifsname));
1221 /* Fill in the ifbreq structure */
1222 error = bridge_ioctl_gifflags(sc, &breq);
1225 memcpy(buf, &breq, sizeof(breq));
1227 buf += sizeof(breq);
1228 len -= sizeof(breq);
1230 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1231 if (len < sizeof(breq))
1234 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1235 sizeof(breq.ifbr_ifsname));
1236 breq.ifbr_ifsflags = bif->bif_flags;
1237 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1238 memcpy(buf, &breq, sizeof(breq));
1240 buf += sizeof(breq);
1241 len -= sizeof(breq);
1245 bifc->ifbic_len = sizeof(breq) * count;
1246 error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len);
1248 free(outbuf, M_TEMP);
1253 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1255 struct ifbaconf *bac = arg;
1256 struct bridge_rtnode *brt;
1257 struct ifbareq bareq;
1259 int count, buflen, len, error = 0;
1261 if (bac->ifbac_len == 0)
1265 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list)
1267 buflen = sizeof(bareq) * count;
1270 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1275 len = min(bac->ifbac_len, buflen);
1276 bzero(&bareq, sizeof(bareq));
1277 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1278 if (len < sizeof(bareq))
1280 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1281 sizeof(bareq.ifba_ifsname));
1282 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1283 bareq.ifba_vlan = brt->brt_vlan;
1284 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1285 time_uptime < brt->brt_expire)
1286 bareq.ifba_expire = brt->brt_expire - time_uptime;
1288 bareq.ifba_expire = 0;
1289 bareq.ifba_flags = brt->brt_flags;
1291 memcpy(buf, &bareq, sizeof(bareq));
1293 buf += sizeof(bareq);
1294 len -= sizeof(bareq);
1298 bac->ifbac_len = sizeof(bareq) * count;
1299 error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len);
1301 free(outbuf, M_TEMP);
1306 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1308 struct ifbareq *req = arg;
1309 struct bridge_iflist *bif;
1312 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1316 error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
1323 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1325 struct ifbrparam *param = arg;
1327 sc->sc_brttimeout = param->ifbrp_ctime;
1332 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1334 struct ifbrparam *param = arg;
1336 param->ifbrp_ctime = sc->sc_brttimeout;
1341 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1343 struct ifbareq *req = arg;
1345 return (bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan));
1349 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1351 struct ifbreq *req = arg;
1353 bridge_rtflush(sc, req->ifbr_ifsflags);
1358 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1360 struct ifbrparam *param = arg;
1361 struct bstp_state *bs = &sc->sc_stp;
1363 param->ifbrp_prio = bs->bs_bridge_priority;
1368 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1370 struct ifbrparam *param = arg;
1372 return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio));
1376 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1378 struct ifbrparam *param = arg;
1379 struct bstp_state *bs = &sc->sc_stp;
1381 param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
1386 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1388 struct ifbrparam *param = arg;
1390 return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime));
1394 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1396 struct ifbrparam *param = arg;
1397 struct bstp_state *bs = &sc->sc_stp;
1399 param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
1404 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1406 struct ifbrparam *param = arg;
1408 return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay));
1412 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1414 struct ifbrparam *param = arg;
1415 struct bstp_state *bs = &sc->sc_stp;
1417 param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
1422 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1424 struct ifbrparam *param = arg;
1426 return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage));
1430 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1432 struct ifbreq *req = arg;
1433 struct bridge_iflist *bif;
1435 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1439 return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority));
1443 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1445 struct ifbreq *req = arg;
1446 struct bridge_iflist *bif;
1448 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1452 return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost));
1456 bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg)
1458 struct ifbreq *req = arg;
1459 struct bridge_iflist *bif;
1461 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1465 bif->bif_addrmax = req->ifbr_addrmax;
1470 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1472 struct ifbreq *req = arg;
1473 struct bridge_iflist *bif = NULL;
1476 ifs = ifunit(req->ifbr_ifsname);
1480 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1481 if (ifs == bif->bif_ifp)
1484 if (ifs->if_bridge != NULL)
1487 switch (ifs->if_type) {
1496 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1501 bif->bif_flags = IFBIF_SPAN;
1503 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1509 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1511 struct ifbreq *req = arg;
1512 struct bridge_iflist *bif;
1515 ifs = ifunit(req->ifbr_ifsname);
1519 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1520 if (ifs == bif->bif_ifp)
1526 bridge_delete_span(sc, bif);
1532 bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg)
1534 struct ifbropreq *req = arg;
1535 struct bstp_state *bs = &sc->sc_stp;
1536 struct bstp_port *root_port;
1538 req->ifbop_maxage = bs->bs_bridge_max_age >> 8;
1539 req->ifbop_hellotime = bs->bs_bridge_htime >> 8;
1540 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;
1542 root_port = bs->bs_root_port;
1543 if (root_port == NULL)
1544 req->ifbop_root_port = 0;
1546 req->ifbop_root_port = root_port->bp_ifp->if_index;
1548 req->ifbop_holdcount = bs->bs_txholdcount;
1549 req->ifbop_priority = bs->bs_bridge_priority;
1550 req->ifbop_protocol = bs->bs_protover;
1551 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;
1552 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id;
1553 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id;
1554 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id;
1555 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;
1556 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;
1562 bridge_ioctl_grte(struct bridge_softc *sc, void *arg)
1564 struct ifbrparam *param = arg;
1566 param->ifbrp_cexceeded = sc->sc_brtexceeded;
1571 bridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg)
1573 struct ifbpstpconf *bifstp = arg;
1574 struct bridge_iflist *bif;
1575 struct bstp_port *bp;
1576 struct ifbpstpreq bpreq;
1578 int count, buflen, len, error = 0;
1581 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1582 if ((bif->bif_flags & IFBIF_STP) != 0)
1586 buflen = sizeof(bpreq) * count;
1587 if (bifstp->ifbpstp_len == 0) {
1588 bifstp->ifbpstp_len = buflen;
1593 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1598 len = min(bifstp->ifbpstp_len, buflen);
1599 bzero(&bpreq, sizeof(bpreq));
1600 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1601 if (len < sizeof(bpreq))
1604 if ((bif->bif_flags & IFBIF_STP) == 0)
1608 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff;
1609 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions;
1610 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost;
1611 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id;
1612 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id;
1613 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id;
1615 memcpy(buf, &bpreq, sizeof(bpreq));
1617 buf += sizeof(bpreq);
1618 len -= sizeof(bpreq);
1622 bifstp->ifbpstp_len = sizeof(bpreq) * count;
1623 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len);
1625 free(outbuf, M_TEMP);
1630 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
1632 struct ifbrparam *param = arg;
1634 return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto));
1638 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
1640 struct ifbrparam *param = arg;
1642 return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc));
1648 * Detach an interface from a bridge. Called when a member
1649 * interface is detaching.
1652 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1654 struct bridge_softc *sc = ifp->if_bridge;
1655 struct bridge_iflist *bif;
1657 /* Check if the interface is a bridge member */
1661 bif = bridge_lookup_member_if(sc, ifp);
1663 bridge_delete_member(sc, bif, 1);
1669 /* Check if the interface is a span port */
1670 mtx_lock(&bridge_list_mtx);
1671 LIST_FOREACH(sc, &bridge_list, sc_list) {
1673 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1674 if (ifp == bif->bif_ifp) {
1675 bridge_delete_span(sc, bif);
1681 mtx_unlock(&bridge_list_mtx);
1687 * Initialize a bridge interface.
1690 bridge_init(void *xsc)
1692 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1693 struct ifnet *ifp = sc->sc_ifp;
1695 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1699 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1702 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1703 bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */
1711 * Stop the bridge interface.
1714 bridge_stop(struct ifnet *ifp, int disable)
1716 struct bridge_softc *sc = ifp->if_softc;
1718 BRIDGE_LOCK_ASSERT(sc);
1720 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1723 callout_stop(&sc->sc_brcallout);
1724 bstp_stop(&sc->sc_stp);
1726 bridge_rtflush(sc, IFBF_FLUSHDYN);
1728 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1734 * Enqueue a packet on a bridge member interface.
1738 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1744 len = m->m_pkthdr.len;
1745 mflags = m->m_flags;
1747 /* We may be sending a fragment so traverse the mbuf */
1750 m->m_nextpkt = NULL;
1753 * If underlying interface can not do VLAN tag insertion itself
1754 * then attach a packet tag that holds it.
1756 if ((m->m_flags & M_VLANTAG) &&
1757 (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) {
1758 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1761 "unable to prepend VLAN header\n");
1762 dst_ifp->if_oerrors++;
1765 m->m_flags &= ~M_VLANTAG;
1769 dst_ifp->if_transmit(dst_ifp, m);
1773 sc->sc_ifp->if_opackets++;
1774 sc->sc_ifp->if_obytes += len;
1775 if (mflags & M_MCAST)
1776 sc->sc_ifp->if_omcasts++;
1783 * Receive a queued packet from dummynet and pass it on to the output
1786 * The mbuf has the Ethernet header already attached.
1789 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
1791 struct bridge_softc *sc;
1793 sc = ifp->if_bridge;
1796 * The packet didnt originate from a member interface. This should only
1797 * ever happen if a member interface is removed while packets are
1805 if (PFIL_HOOKED(&inet_pfil_hook)
1807 || PFIL_HOOKED(&inet6_pfil_hook)
1810 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
1816 bridge_enqueue(sc, ifp, m);
1822 * Send output from a bridge member interface. This
1823 * performs the bridging function for locally originated
1826 * The mbuf has the Ethernet header already attached. We must
1827 * enqueue or free the mbuf before returning.
1830 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1833 struct ether_header *eh;
1834 struct ifnet *dst_if;
1835 struct bridge_softc *sc;
1838 if (m->m_len < ETHER_HDR_LEN) {
1839 m = m_pullup(m, ETHER_HDR_LEN);
1844 eh = mtod(m, struct ether_header *);
1845 sc = ifp->if_bridge;
1846 vlan = VLANTAGOF(m);
1851 * If bridge is down, but the original output interface is up,
1852 * go ahead and send out that interface. Otherwise, the packet
1855 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1861 * If the packet is a multicast, or we don't know a better way to
1862 * get there, send to all interfaces.
1864 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1867 dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan);
1868 if (dst_if == NULL) {
1869 struct bridge_iflist *bif;
1871 int error = 0, used = 0;
1875 BRIDGE_LOCK2REF(sc, error);
1881 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1882 dst_if = bif->bif_ifp;
1884 if (dst_if->if_type == IFT_GIF)
1886 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
1890 * If this is not the original output interface,
1891 * and the interface is participating in spanning
1892 * tree, make sure the port is in a state that
1893 * allows forwarding.
1895 if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) &&
1896 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
1899 if (LIST_NEXT(bif, bif_next) == NULL) {
1903 mc = m_copypacket(m, M_DONTWAIT);
1905 sc->sc_ifp->if_oerrors++;
1910 bridge_enqueue(sc, dst_if, mc);
1920 * XXX Spanning tree consideration here?
1924 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1931 bridge_enqueue(sc, dst_if, m);
1938 * Start output on a bridge.
1942 bridge_start(struct ifnet *ifp)
1944 struct bridge_softc *sc;
1946 struct ether_header *eh;
1947 struct ifnet *dst_if;
1951 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1953 IFQ_DEQUEUE(&ifp->if_snd, m);
1956 ETHER_BPF_MTAP(ifp, m);
1958 eh = mtod(m, struct ether_header *);
1962 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1963 dst_if = bridge_rtlookup(sc, eh->ether_dhost, 1);
1967 bridge_broadcast(sc, ifp, m, 0);
1970 bridge_enqueue(sc, dst_if, m);
1973 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1979 * The forwarding function of the bridge.
1981 * NOTE: Releases the lock on return.
1984 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
1987 struct bridge_iflist *dbif;
1988 struct ifnet *src_if, *dst_if, *ifp;
1989 struct ether_header *eh;
1994 src_if = m->m_pkthdr.rcvif;
1998 ifp->if_ibytes += m->m_pkthdr.len;
1999 vlan = VLANTAGOF(m);
2001 if ((sbif->bif_flags & IFBIF_STP) &&
2002 sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2005 eh = mtod(m, struct ether_header *);
2006 dst = eh->ether_dhost;
2008 /* If the interface is learning, record the address. */
2009 if (sbif->bif_flags & IFBIF_LEARNING) {
2010 error = bridge_rtupdate(sc, eh->ether_shost, vlan,
2011 sbif, 0, IFBAF_DYNAMIC);
2013 * If the interface has addresses limits then deny any source
2014 * that is not in the cache.
2016 if (error && sbif->bif_addrmax)
2020 if ((sbif->bif_flags & IFBIF_STP) != 0 &&
2021 sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING)
2025 * At this point, the port either doesn't participate
2026 * in spanning tree or it is in the forwarding state.
2030 * If the packet is unicast, destined for someone on
2031 * "this" side of the bridge, drop it.
2033 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2034 dst_if = bridge_rtlookup(sc, dst, vlan);
2035 if (src_if == dst_if)
2039 * Check if its a reserved multicast address, any address
2040 * listed in 802.1D section 7.12.6 may not be forwarded by the
2042 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
2044 if (dst[0] == 0x01 && dst[1] == 0x80 &&
2045 dst[2] == 0xc2 && dst[3] == 0x00 &&
2046 dst[4] == 0x00 && dst[5] <= 0x0f)
2049 /* ...forward it to all interfaces. */
2055 * If we have a destination interface which is a member of our bridge,
2056 * OR this is a unicast packet, push it through the bpf(4) machinery.
2057 * For broadcast or multicast packets, don't bother because it will
2058 * be reinjected into ether_input. We do this before we pass the packets
2059 * through the pfil(9) framework, as it is possible that pfil(9) will
2060 * drop the packet, or possibly modify it, making it difficult to debug
2061 * firewall issues on the bridge.
2063 if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0)
2064 ETHER_BPF_MTAP(ifp, m);
2066 /* run the packet filter */
2067 if (PFIL_HOOKED(&inet_pfil_hook)
2069 || PFIL_HOOKED(&inet6_pfil_hook)
2073 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2080 if (dst_if == NULL) {
2081 bridge_broadcast(sc, src_if, m, 1);
2086 * At this point, we're dealing with a unicast frame
2087 * going to a different interface.
2089 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2092 dbif = bridge_lookup_member_if(sc, dst_if);
2094 /* Not a member of the bridge (anymore?) */
2097 /* Private segments can not talk to each other */
2098 if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)
2101 if ((dbif->bif_flags & IFBIF_STP) &&
2102 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2107 if (PFIL_HOOKED(&inet_pfil_hook)
2109 || PFIL_HOOKED(&inet6_pfil_hook)
2112 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2118 bridge_enqueue(sc, dst_if, m);
2129 * Receive input from a member interface. Queue the packet for
2130 * bridging if it is not for us.
2132 static struct mbuf *
2133 bridge_input(struct ifnet *ifp, struct mbuf *m)
2135 struct bridge_softc *sc = ifp->if_bridge;
2136 struct bridge_iflist *bif, *bif2;
2138 struct ether_header *eh;
2139 struct mbuf *mc, *mc2;
2143 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2147 vlan = VLANTAGOF(m);
2150 * Implement support for bridge monitoring. If this flag has been
2151 * set on this interface, discard the packet once we push it through
2152 * the bpf(4) machinery, but before we do, increment the byte and
2153 * packet counters associated with this interface.
2155 if ((bifp->if_flags & IFF_MONITOR) != 0) {
2156 m->m_pkthdr.rcvif = bifp;
2157 ETHER_BPF_MTAP(bifp, m);
2158 bifp->if_ipackets++;
2159 bifp->if_ibytes += m->m_pkthdr.len;
2164 bif = bridge_lookup_member_if(sc, ifp);
2170 eh = mtod(m, struct ether_header *);
2174 if (m->m_flags & (M_BCAST|M_MCAST)) {
2175 /* Tap off 802.1D packets; they do not get forwarded. */
2176 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2177 ETHER_ADDR_LEN) == 0) {
2178 m = bstp_input(&bif->bif_stp, ifp, m);
2185 if ((bif->bif_flags & IFBIF_STP) &&
2186 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2192 * Make a deep copy of the packet and enqueue the copy
2193 * for bridge processing; return the original packet for
2196 mc = m_dup(m, M_DONTWAIT);
2202 /* Perform the bridge forwarding function with the copy. */
2203 bridge_forward(sc, bif, mc);
2206 * Reinject the mbuf as arriving on the bridge so we have a
2207 * chance at claiming multicast packets. We can not loop back
2208 * here from ether_input as a bridge is never a member of a
2211 KASSERT(bifp->if_bridge == NULL,
2212 ("loop created in bridge_input"));
2213 mc2 = m_dup(m, M_DONTWAIT);
2215 /* Keep the layer3 header aligned */
2216 int i = min(mc2->m_pkthdr.len, max_protohdr);
2217 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2220 mc2->m_pkthdr.rcvif = bifp;
2221 (*bifp->if_input)(bifp, mc2);
2224 /* Return the original packet for local processing. */
2228 if ((bif->bif_flags & IFBIF_STP) &&
2229 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2235 # define OR_CARP_CHECK_WE_ARE_DST(iface) \
2236 || ((iface)->if_carp \
2237 && carp_forus((iface)->if_carp, eh->ether_dhost))
2238 # define OR_CARP_CHECK_WE_ARE_SRC(iface) \
2239 || ((iface)->if_carp \
2240 && carp_forus((iface)->if_carp, eh->ether_shost))
2242 # define OR_CARP_CHECK_WE_ARE_DST(iface)
2243 # define OR_CARP_CHECK_WE_ARE_SRC(iface)
2247 # define OR_PFIL_HOOKED_INET6 \
2248 || PFIL_HOOKED(&inet6_pfil_hook)
2250 # define OR_PFIL_HOOKED_INET6
2253 #define GRAB_OUR_PACKETS(iface) \
2254 if ((iface)->if_type == IFT_GIF) \
2256 /* It is destined for us. */ \
2257 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, ETHER_ADDR_LEN) == 0 \
2258 OR_CARP_CHECK_WE_ARE_DST((iface)) \
2260 if ((iface)->if_type == IFT_BRIDGE) { \
2261 ETHER_BPF_MTAP(iface, m); \
2262 iface->if_ipackets++; \
2263 /* Filter on the physical interface. */ \
2264 if (pfil_local_phys && \
2265 (PFIL_HOOKED(&inet_pfil_hook) \
2266 OR_PFIL_HOOKED_INET6)) { \
2267 if (bridge_pfil(&m, NULL, ifp, \
2268 PFIL_IN) != 0 || m == NULL) { \
2269 BRIDGE_UNLOCK(sc); \
2274 if (bif->bif_flags & IFBIF_LEARNING) { \
2275 error = bridge_rtupdate(sc, eh->ether_shost, \
2276 vlan, bif, 0, IFBAF_DYNAMIC); \
2277 if (error && bif->bif_addrmax) { \
2278 BRIDGE_UNLOCK(sc); \
2283 m->m_pkthdr.rcvif = iface; \
2284 BRIDGE_UNLOCK(sc); \
2288 /* We just received a packet that we sent out. */ \
2289 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, ETHER_ADDR_LEN) == 0 \
2290 OR_CARP_CHECK_WE_ARE_SRC((iface)) \
2292 BRIDGE_UNLOCK(sc); \
2298 * Unicast. Make sure it's not for the bridge.
2300 do { GRAB_OUR_PACKETS(bifp) } while (0);
2303 * Give a chance for ifp at first priority. This will help when the
2304 * packet comes through the interface like VLAN's with the same MACs
2305 * on several interfaces from the same bridge. This also will save
2306 * some CPU cycles in case the destination interface and the input
2307 * interface (eq ifp) are the same.
2309 do { GRAB_OUR_PACKETS(ifp) } while (0);
2311 /* Now check the all bridge members. */
2312 LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) {
2313 GRAB_OUR_PACKETS(bif2->bif_ifp)
2316 #undef OR_CARP_CHECK_WE_ARE_DST
2317 #undef OR_CARP_CHECK_WE_ARE_SRC
2318 #undef OR_PFIL_HOOKED_INET6
2319 #undef GRAB_OUR_PACKETS
2321 /* Perform the bridge forwarding function. */
2322 bridge_forward(sc, bif, m);
2330 * Send a frame to all interfaces that are members of
2331 * the bridge, except for the one on which the packet
2334 * NOTE: Releases the lock on return.
2337 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2338 struct mbuf *m, int runfilt)
2340 struct bridge_iflist *dbif, *sbif;
2342 struct ifnet *dst_if;
2343 int error = 0, used = 0, i;
2345 sbif = bridge_lookup_member_if(sc, src_if);
2347 BRIDGE_LOCK2REF(sc, error);
2353 /* Filter on the bridge interface before broadcasting */
2354 if (runfilt && (PFIL_HOOKED(&inet_pfil_hook)
2356 || PFIL_HOOKED(&inet6_pfil_hook)
2359 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
2365 LIST_FOREACH(dbif, &sc->sc_iflist, bif_next) {
2366 dst_if = dbif->bif_ifp;
2367 if (dst_if == src_if)
2370 /* Private segments can not talk to each other */
2371 if (sbif && (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE))
2374 if ((dbif->bif_flags & IFBIF_STP) &&
2375 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2378 if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 &&
2379 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2382 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2385 if (LIST_NEXT(dbif, bif_next) == NULL) {
2389 mc = m_dup(m, M_DONTWAIT);
2391 sc->sc_ifp->if_oerrors++;
2397 * Filter on the output interface. Pass a NULL bridge interface
2398 * pointer so we do not redundantly filter on the bridge for
2399 * each interface we broadcast on.
2401 if (runfilt && (PFIL_HOOKED(&inet_pfil_hook)
2403 || PFIL_HOOKED(&inet6_pfil_hook)
2407 /* Keep the layer3 header aligned */
2408 i = min(mc->m_pkthdr.len, max_protohdr);
2409 mc = m_copyup(mc, i, ETHER_ALIGN);
2411 sc->sc_ifp->if_oerrors++;
2415 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2421 bridge_enqueue(sc, dst_if, mc);
2433 * Duplicate a packet out one or more interfaces that are in span mode,
2434 * the original mbuf is unmodified.
2437 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2439 struct bridge_iflist *bif;
2440 struct ifnet *dst_if;
2443 if (LIST_EMPTY(&sc->sc_spanlist))
2446 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2447 dst_if = bif->bif_ifp;
2449 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2452 mc = m_copypacket(m, M_DONTWAIT);
2454 sc->sc_ifp->if_oerrors++;
2458 bridge_enqueue(sc, dst_if, mc);
2465 * Add a bridge routing entry.
2468 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan,
2469 struct bridge_iflist *bif, int setflags, uint8_t flags)
2471 struct bridge_rtnode *brt;
2474 BRIDGE_LOCK_ASSERT(sc);
2476 /* Check the source address is valid and not multicast. */
2477 if (ETHER_IS_MULTICAST(dst) ||
2478 (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
2479 dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0)
2482 /* 802.1p frames map to vlan 1 */
2487 * A route for this destination might already exist. If so,
2488 * update it, otherwise create a new one.
2490 if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) {
2491 if (sc->sc_brtcnt >= sc->sc_brtmax) {
2492 sc->sc_brtexceeded++;
2495 /* Check per interface address limits (if enabled) */
2496 if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) {
2497 bif->bif_addrexceeded++;
2502 * Allocate a new bridge forwarding node, and
2503 * initialize the expiration time and Ethernet
2506 brt = uma_zalloc(bridge_rtnode_zone, M_NOWAIT | M_ZERO);
2510 if (bif->bif_flags & IFBIF_STICKY)
2511 brt->brt_flags = IFBAF_STICKY;
2513 brt->brt_flags = IFBAF_DYNAMIC;
2515 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2516 brt->brt_vlan = vlan;
2518 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2519 uma_zfree(bridge_rtnode_zone, brt);
2526 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2527 brt->brt_dst != bif) {
2528 brt->brt_dst->bif_addrcnt--;
2530 brt->brt_dst->bif_addrcnt++;
2533 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2534 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2536 brt->brt_flags = flags;
2544 * Lookup the destination interface for an address.
2546 static struct ifnet *
2547 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2549 struct bridge_rtnode *brt;
2551 BRIDGE_LOCK_ASSERT(sc);
2553 if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL)
2556 return (brt->brt_ifp);
2562 * Trim the routine table so that we have a number
2563 * of routing entries less than or equal to the
2567 bridge_rttrim(struct bridge_softc *sc)
2569 struct bridge_rtnode *brt, *nbrt;
2571 BRIDGE_LOCK_ASSERT(sc);
2573 /* Make sure we actually need to do this. */
2574 if (sc->sc_brtcnt <= sc->sc_brtmax)
2577 /* Force an aging cycle; this might trim enough addresses. */
2579 if (sc->sc_brtcnt <= sc->sc_brtmax)
2582 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2583 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2584 bridge_rtnode_destroy(sc, brt);
2585 if (sc->sc_brtcnt <= sc->sc_brtmax)
2594 * Aging timer for the bridge.
2597 bridge_timer(void *arg)
2599 struct bridge_softc *sc = arg;
2601 BRIDGE_LOCK_ASSERT(sc);
2605 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
2606 callout_reset(&sc->sc_brcallout,
2607 bridge_rtable_prune_period * hz, bridge_timer, sc);
2613 * Perform an aging cycle.
2616 bridge_rtage(struct bridge_softc *sc)
2618 struct bridge_rtnode *brt, *nbrt;
2620 BRIDGE_LOCK_ASSERT(sc);
2622 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2623 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2624 if (time_uptime >= brt->brt_expire)
2625 bridge_rtnode_destroy(sc, brt);
2633 * Remove all dynamic addresses from the bridge.
2636 bridge_rtflush(struct bridge_softc *sc, int full)
2638 struct bridge_rtnode *brt, *nbrt;
2640 BRIDGE_LOCK_ASSERT(sc);
2642 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2643 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2644 bridge_rtnode_destroy(sc, brt);
2651 * Remove an address from the table.
2654 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2656 struct bridge_rtnode *brt;
2659 BRIDGE_LOCK_ASSERT(sc);
2662 * If vlan is zero then we want to delete for all vlans so the lookup
2663 * may return more than one.
2665 while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) {
2666 bridge_rtnode_destroy(sc, brt);
2670 return (found ? 0 : ENOENT);
2676 * Delete routes to a speicifc member interface.
2679 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2681 struct bridge_rtnode *brt, *nbrt;
2683 BRIDGE_LOCK_ASSERT(sc);
2685 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2686 if (brt->brt_ifp == ifp && (full ||
2687 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2688 bridge_rtnode_destroy(sc, brt);
2693 * bridge_rtable_init:
2695 * Initialize the route table for this bridge.
2698 bridge_rtable_init(struct bridge_softc *sc)
2702 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2703 M_DEVBUF, M_NOWAIT);
2704 if (sc->sc_rthash == NULL)
2707 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2708 LIST_INIT(&sc->sc_rthash[i]);
2710 sc->sc_rthash_key = arc4random();
2712 LIST_INIT(&sc->sc_rtlist);
2718 * bridge_rtable_fini:
2720 * Deconstruct the route table for this bridge.
2723 bridge_rtable_fini(struct bridge_softc *sc)
2726 KASSERT(sc->sc_brtcnt == 0,
2727 ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt));
2728 free(sc->sc_rthash, M_DEVBUF);
2732 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2733 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2735 #define mix(a, b, c) \
2737 a -= b; a -= c; a ^= (c >> 13); \
2738 b -= c; b -= a; b ^= (a << 8); \
2739 c -= a; c -= b; c ^= (b >> 13); \
2740 a -= b; a -= c; a ^= (c >> 12); \
2741 b -= c; b -= a; b ^= (a << 16); \
2742 c -= a; c -= b; c ^= (b >> 5); \
2743 a -= b; a -= c; a ^= (c >> 3); \
2744 b -= c; b -= a; b ^= (a << 10); \
2745 c -= a; c -= b; c ^= (b >> 15); \
2746 } while (/*CONSTCOND*/0)
2748 static __inline uint32_t
2749 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2751 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2762 return (c & BRIDGE_RTHASH_MASK);
2768 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2772 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2773 d = ((int)a[i]) - ((int)b[i]);
2780 * bridge_rtnode_lookup:
2782 * Look up a bridge route node for the specified destination. Compare the
2783 * vlan id or if zero then just return the first match.
2785 static struct bridge_rtnode *
2786 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
2788 struct bridge_rtnode *brt;
2792 BRIDGE_LOCK_ASSERT(sc);
2794 hash = bridge_rthash(sc, addr);
2795 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2796 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2797 if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0))
2807 * bridge_rtnode_insert:
2809 * Insert the specified bridge node into the route table. We
2810 * assume the entry is not already in the table.
2813 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2815 struct bridge_rtnode *lbrt;
2819 BRIDGE_LOCK_ASSERT(sc);
2821 hash = bridge_rthash(sc, brt->brt_addr);
2823 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2825 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2830 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2831 if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan)
2834 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2837 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2838 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2841 lbrt = LIST_NEXT(lbrt, brt_hash);
2842 } while (lbrt != NULL);
2845 panic("bridge_rtnode_insert: impossible");
2849 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2856 * bridge_rtnode_destroy:
2858 * Destroy a bridge rtnode.
2861 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2863 BRIDGE_LOCK_ASSERT(sc);
2865 LIST_REMOVE(brt, brt_hash);
2867 LIST_REMOVE(brt, brt_list);
2869 brt->brt_dst->bif_addrcnt--;
2870 uma_zfree(bridge_rtnode_zone, brt);
2874 * bridge_rtable_expire:
2876 * Set the expiry time for all routes on an interface.
2879 bridge_rtable_expire(struct ifnet *ifp, int age)
2881 struct bridge_softc *sc = ifp->if_bridge;
2882 struct bridge_rtnode *brt;
2887 * If the age is zero then flush, otherwise set all the expiry times to
2888 * age for the interface
2891 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
2893 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
2894 /* Cap the expiry time to 'age' */
2895 if (brt->brt_ifp == ifp &&
2896 brt->brt_expire > time_uptime + age &&
2897 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2898 brt->brt_expire = time_uptime + age;
2905 * bridge_state_change:
2907 * Callback from the bridgestp code when a port changes states.
2910 bridge_state_change(struct ifnet *ifp, int state)
2912 struct bridge_softc *sc = ifp->if_bridge;
2913 static const char *stpstates[] = {
2923 log(LOG_NOTICE, "%s: state changed to %s on %s\n",
2924 sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname);
2928 * Send bridge packets through pfil if they are one of the types pfil can deal
2929 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2930 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2934 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2936 int snap, error, i, hlen;
2937 struct ether_header *eh1, eh2;
2938 struct ip_fw_args args;
2941 u_int16_t ether_type;
2944 error = -1; /* Default error if not error == 0 */
2947 /* we may return with the IP fields swapped, ensure its not shared */
2948 KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
2951 if (pfil_bridge == 0 && pfil_member == 0 && pfil_ipfw == 0)
2952 return (0); /* filtering is disabled */
2954 i = min((*mp)->m_pkthdr.len, max_protohdr);
2955 if ((*mp)->m_len < i) {
2956 *mp = m_pullup(*mp, i);
2958 printf("%s: m_pullup failed\n", __func__);
2963 eh1 = mtod(*mp, struct ether_header *);
2964 ether_type = ntohs(eh1->ether_type);
2967 * Check for SNAP/LLC.
2969 if (ether_type < ETHERMTU) {
2970 struct llc *llc2 = (struct llc *)(eh1 + 1);
2972 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2973 llc2->llc_dsap == LLC_SNAP_LSAP &&
2974 llc2->llc_ssap == LLC_SNAP_LSAP &&
2975 llc2->llc_control == LLC_UI) {
2976 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2982 * If we're trying to filter bridge traffic, don't look at anything
2983 * other than IP and ARP traffic. If the filter doesn't understand
2984 * IPv6, don't allow IPv6 through the bridge either. This is lame
2985 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2986 * but of course we don't have an AppleTalk filter to begin with.
2987 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2990 switch (ether_type) {
2992 case ETHERTYPE_REVARP:
2993 if (pfil_ipfw_arp == 0)
2994 return (0); /* Automatically pass */
2999 case ETHERTYPE_IPV6:
3004 * Check to see if the user wants to pass non-ip
3005 * packets, these will not be checked by pfil(9) and
3006 * passed unconditionally so the default is to drop.
3012 /* Strip off the Ethernet header and keep a copy. */
3013 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3014 m_adj(*mp, ETHER_HDR_LEN);
3016 /* Strip off snap header, if present */
3018 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3019 m_adj(*mp, sizeof(struct llc));
3023 * Check the IP header for alignment and errors
3025 if (dir == PFIL_IN) {
3026 switch (ether_type) {
3028 error = bridge_ip_checkbasic(mp);
3031 case ETHERTYPE_IPV6:
3032 error = bridge_ip6_checkbasic(mp);
3042 if (IPFW_LOADED && pfil_ipfw != 0 && dir == PFIL_OUT && ifp != NULL) {
3043 INIT_VNET_INET(curvnet);
3046 args.rule = ip_dn_claim_rule(*mp);
3047 if (args.rule != NULL && V_fw_one_pass)
3048 goto ipfwpass; /* packet already partially processed */
3052 args.next_hop = NULL;
3054 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
3055 i = ip_fw_chk_ptr(&args);
3061 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
3063 /* put the Ethernet header back on */
3064 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
3067 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3070 * Pass the pkt to dummynet, which consumes it. The
3071 * packet will return to us via bridge_dummynet().
3074 ip_dn_io_ptr(mp, DN_TO_IFB_FWD, &args);
3078 if (i != IP_FW_PASS) /* drop */
3086 * Run the packet through pfil
3088 switch (ether_type) {
3091 * before calling the firewall, swap fields the same as
3092 * IP does. here we assume the header is contiguous
3094 ip = mtod(*mp, struct ip *);
3096 ip->ip_len = ntohs(ip->ip_len);
3097 ip->ip_off = ntohs(ip->ip_off);
3100 * Run pfil on the member interface and the bridge, both can
3101 * be skipped by clearing pfil_member or pfil_bridge.
3104 * in_if -> bridge_if -> out_if
3106 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3107 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
3110 if (*mp == NULL || error != 0) /* filter may consume */
3113 if (pfil_member && ifp != NULL)
3114 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
3117 if (*mp == NULL || error != 0) /* filter may consume */
3120 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3121 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
3124 if (*mp == NULL || error != 0) /* filter may consume */
3127 /* check if we need to fragment the packet */
3128 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3129 i = (*mp)->m_pkthdr.len;
3130 if (i > ifp->if_mtu) {
3131 error = bridge_fragment(ifp, *mp, &eh2, snap,
3137 /* Recalculate the ip checksum and restore byte ordering */
3138 ip = mtod(*mp, struct ip *);
3139 hlen = ip->ip_hl << 2;
3140 if (hlen < sizeof(struct ip))
3142 if (hlen > (*mp)->m_len) {
3143 if ((*mp = m_pullup(*mp, hlen)) == 0)
3145 ip = mtod(*mp, struct ip *);
3149 ip->ip_len = htons(ip->ip_len);
3150 ip->ip_off = htons(ip->ip_off);
3152 if (hlen == sizeof(struct ip))
3153 ip->ip_sum = in_cksum_hdr(ip);
3155 ip->ip_sum = in_cksum(*mp, hlen);
3159 case ETHERTYPE_IPV6:
3160 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3161 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3164 if (*mp == NULL || error != 0) /* filter may consume */
3167 if (pfil_member && ifp != NULL)
3168 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
3171 if (*mp == NULL || error != 0) /* filter may consume */
3174 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3175 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3192 * Finally, put everything back the way it was and return
3195 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
3198 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3201 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
3204 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3215 * Perform basic checks on header size since
3216 * pfil assumes ip_input has already processed
3217 * it for it. Cut-and-pasted from ip_input.c.
3218 * Given how simple the IPv6 version is,
3219 * does the IPv4 version really need to be
3222 * XXX Should we update ipstat here, or not?
3223 * XXX Right now we update ipstat but not
3227 bridge_ip_checkbasic(struct mbuf **mp)
3229 INIT_VNET_INET(curvnet);
3230 struct mbuf *m = *mp;
3238 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3239 if ((m = m_copyup(m, sizeof(struct ip),
3240 (max_linkhdr + 3) & ~3)) == NULL) {
3241 /* XXXJRT new stat, please */
3242 IPSTAT_INC(ips_toosmall);
3245 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
3246 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3247 IPSTAT_INC(ips_toosmall);
3251 ip = mtod(m, struct ip *);
3252 if (ip == NULL) goto bad;
3254 if (ip->ip_v != IPVERSION) {
3255 IPSTAT_INC(ips_badvers);
3258 hlen = ip->ip_hl << 2;
3259 if (hlen < sizeof(struct ip)) { /* minimum header length */
3260 IPSTAT_INC(ips_badhlen);
3263 if (hlen > m->m_len) {
3264 if ((m = m_pullup(m, hlen)) == 0) {
3265 IPSTAT_INC(ips_badhlen);
3268 ip = mtod(m, struct ip *);
3269 if (ip == NULL) goto bad;
3272 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3273 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3275 if (hlen == sizeof(struct ip)) {
3276 sum = in_cksum_hdr(ip);
3278 sum = in_cksum(m, hlen);
3282 IPSTAT_INC(ips_badsum);
3286 /* Retrieve the packet length. */
3287 len = ntohs(ip->ip_len);
3290 * Check for additional length bogosity
3293 IPSTAT_INC(ips_badlen);
3298 * Check that the amount of data in the buffers
3299 * is as at least much as the IP header would have us expect.
3300 * Drop packet if shorter than we expect.
3302 if (m->m_pkthdr.len < len) {
3303 IPSTAT_INC(ips_tooshort);
3307 /* Checks out, proceed */
3318 * Same as above, but for IPv6.
3319 * Cut-and-pasted from ip6_input.c.
3320 * XXX Should we update ip6stat, or not?
3323 bridge_ip6_checkbasic(struct mbuf **mp)
3325 INIT_VNET_INET6(curvnet);
3326 struct mbuf *m = *mp;
3327 struct ip6_hdr *ip6;
3330 * If the IPv6 header is not aligned, slurp it up into a new
3331 * mbuf with space for link headers, in the event we forward
3332 * it. Otherwise, if it is aligned, make sure the entire base
3333 * IPv6 header is in the first mbuf of the chain.
3335 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3336 struct ifnet *inifp = m->m_pkthdr.rcvif;
3337 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3338 (max_linkhdr + 3) & ~3)) == NULL) {
3339 /* XXXJRT new stat, please */
3340 V_ip6stat.ip6s_toosmall++;
3341 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3344 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3345 struct ifnet *inifp = m->m_pkthdr.rcvif;
3346 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3347 V_ip6stat.ip6s_toosmall++;
3348 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3353 ip6 = mtod(m, struct ip6_hdr *);
3355 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3356 V_ip6stat.ip6s_badvers++;
3357 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3361 /* Checks out, proceed */
3374 * Return a fragmented mbuf chain.
3377 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
3378 int snap, struct llc *llc)
3380 INIT_VNET_INET(curvnet);
3385 if (m->m_len < sizeof(struct ip) &&
3386 (m = m_pullup(m, sizeof(struct ip))) == NULL)
3388 ip = mtod(m, struct ip *);
3390 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
3395 /* walk the chain and re-add the Ethernet header */
3396 for (m0 = m; m0; m0 = m0->m_nextpkt) {
3399 M_PREPEND(m0, sizeof(struct llc), M_DONTWAIT);
3404 bcopy(llc, mtod(m0, caddr_t),
3405 sizeof(struct llc));
3407 M_PREPEND(m0, ETHER_HDR_LEN, M_DONTWAIT);
3412 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
3418 IPSTAT_INC(ips_fragmented);