Save 'ipfw forward' information in mtag, use m_pkthdr.fw_flags to indicate
[dragonfly/netmp.git] / sys / net / bridge / if_bridge.c
blobe5ae711d71455e7a693906ea5baa3096d15ee35c
1 /*
2 * Copyright 2001 Wasabi Systems, Inc.
3 * All rights reserved.
5 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Wasabi Systems, Inc.
19 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
20 * or promote products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by Jason L. Wright
51 * 4. The name of the author may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
57 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
63 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
66 * $OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp $
67 * $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $
68 * $FreeBSD: src/sys/net/if_bridge.c,v 1.26 2005/10/13 23:05:55 thompsa Exp $
69 * $DragonFly: src/sys/net/bridge/if_bridge.c,v 1.43 2008/08/22 09:14:17 sephe Exp $
73 * Network interface bridge support.
75 * TODO:
77 * - Currently only supports Ethernet-like interfaces (Ethernet,
78 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
79 * to bridge other types of interfaces (FDDI-FDDI, and maybe
80 * consider heterogenous bridges).
83 #include <sys/cdefs.h>
85 #include "opt_inet.h"
86 #include "opt_inet6.h"
88 #include <sys/param.h>
89 #include <sys/mbuf.h>
90 #include <sys/malloc.h>
91 #include <sys/protosw.h>
92 #include <sys/systm.h>
93 #include <sys/time.h>
94 #include <sys/socket.h> /* for net/if.h */
95 #include <sys/sockio.h>
96 #include <sys/ctype.h> /* string functions */
97 #include <sys/kernel.h>
98 #include <sys/random.h>
99 #include <sys/sysctl.h>
100 #include <sys/module.h>
101 #include <sys/proc.h>
102 #include <sys/lock.h>
103 #include <sys/thread.h>
104 #include <sys/thread2.h>
105 #include <sys/mpipe.h>
107 #include <net/bpf.h>
108 #include <net/if.h>
109 #include <net/if_dl.h>
110 #include <net/if_types.h>
111 #include <net/if_var.h>
112 #include <net/pfil.h>
113 #include <net/ifq_var.h>
114 #include <net/if_clone.h>
116 #include <netinet/in.h> /* for struct arpcom */
117 #include <netinet/in_systm.h>
118 #include <netinet/in_var.h>
119 #include <netinet/ip.h>
120 #include <netinet/ip_var.h>
121 #ifdef INET6
122 #include <netinet/ip6.h>
123 #include <netinet6/ip6_var.h>
124 #endif
125 #include <netinet/if_ether.h> /* for struct arpcom */
126 #include <net/bridge/if_bridgevar.h>
127 #include <net/if_llc.h>
128 #include <net/netmsg2.h>
130 #include <net/route.h>
131 #include <sys/in_cksum.h>
134 * Size of the route hash table. Must be a power of two.
136 #ifndef BRIDGE_RTHASH_SIZE
137 #define BRIDGE_RTHASH_SIZE 1024
138 #endif
140 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
143 * Maximum number of addresses to cache.
145 #ifndef BRIDGE_RTABLE_MAX
146 #define BRIDGE_RTABLE_MAX 100
147 #endif
150 * Spanning tree defaults.
152 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
153 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
154 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
155 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
156 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
157 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
158 #define BSTP_DEFAULT_PATH_COST 55
161 * Timeout (in seconds) for entries learned dynamically.
163 #ifndef BRIDGE_RTABLE_TIMEOUT
164 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
165 #endif
168 * Number of seconds between walks of the route list.
170 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
171 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
172 #endif
175 * List of capabilities to mask on the member interface.
177 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
179 eventhandler_tag bridge_detach_cookie = NULL;
181 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
182 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *);
183 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
185 typedef int (*bridge_ctl_t)(struct bridge_softc *, void *);
187 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
189 static int bridge_clone_create(struct if_clone *, int);
190 static void bridge_clone_destroy(struct ifnet *);
192 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
193 static void bridge_mutecaps(struct bridge_iflist *, int);
194 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
195 static void bridge_init(void *);
196 static void bridge_stop(struct ifnet *);
197 static void bridge_start(struct ifnet *);
198 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
199 static int bridge_output(struct ifnet *, struct mbuf *);
201 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
203 static void bridge_timer(void *);
205 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
206 struct mbuf *, int);
207 static void bridge_span(struct bridge_softc *, struct mbuf *);
209 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
210 struct ifnet *, int, uint8_t);
211 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
212 static void bridge_rttrim(struct bridge_softc *);
213 static void bridge_rtage(struct bridge_softc *);
214 static void bridge_rtflush(struct bridge_softc *, int);
215 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
217 static int bridge_rtable_init(struct bridge_softc *);
218 static void bridge_rtable_fini(struct bridge_softc *);
220 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
221 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
222 const uint8_t *);
223 static int bridge_rtnode_insert(struct bridge_softc *,
224 struct bridge_rtnode *);
225 static void bridge_rtnode_destroy(struct bridge_softc *,
226 struct bridge_rtnode *);
228 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
229 const char *name);
230 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
231 struct ifnet *ifp);
232 static void bridge_delete_member(struct bridge_softc *,
233 struct bridge_iflist *, int);
234 static void bridge_delete_span(struct bridge_softc *,
235 struct bridge_iflist *);
237 static int bridge_control(struct bridge_softc *, u_long,
238 bridge_ctl_t, void *);
239 static int bridge_ioctl_add(struct bridge_softc *, void *);
240 static int bridge_ioctl_del(struct bridge_softc *, void *);
241 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
242 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
243 static int bridge_ioctl_scache(struct bridge_softc *, void *);
244 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
245 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
246 static int bridge_ioctl_rts(struct bridge_softc *, void *);
247 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
248 static int bridge_ioctl_sto(struct bridge_softc *, void *);
249 static int bridge_ioctl_gto(struct bridge_softc *, void *);
250 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
251 static int bridge_ioctl_flush(struct bridge_softc *, void *);
252 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
253 static int bridge_ioctl_spri(struct bridge_softc *, void *);
254 static int bridge_ioctl_ght(struct bridge_softc *, void *);
255 static int bridge_ioctl_sht(struct bridge_softc *, void *);
256 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
257 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
258 static int bridge_ioctl_gma(struct bridge_softc *, void *);
259 static int bridge_ioctl_sma(struct bridge_softc *, void *);
260 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
261 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
262 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
263 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
264 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
265 int);
266 static int bridge_ip_checkbasic(struct mbuf **mp);
267 #ifdef INET6
268 static int bridge_ip6_checkbasic(struct mbuf **mp);
269 #endif /* INET6 */
270 static int bridge_fragment(struct ifnet *, struct mbuf *,
271 struct ether_header *, int, struct llc *);
272 static void bridge_enqueue_internal(struct ifnet *, struct mbuf *m,
273 netisr_fn_t);
274 static void bridge_enqueue_handler(struct netmsg *);
275 static void bridge_pfil_enqueue_handler(struct netmsg *);
276 static void bridge_pfil_enqueue(struct ifnet *, struct mbuf *, int);
277 static void bridge_handoff_notags(struct ifnet *, struct mbuf *);
278 static void bridge_handoff(struct ifnet *, struct mbuf *);
280 SYSCTL_DECL(_net_link);
281 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
283 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
284 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
285 static int pfil_member = 1; /* run pfil hooks on the member interface */
286 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
287 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
288 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
289 &pfil_bridge, 0, "Packet filter on the bridge interface");
290 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
291 &pfil_member, 0, "Packet filter on the member interface");
293 struct bridge_control {
294 bridge_ctl_t bc_func;
295 int bc_argsize;
296 int bc_flags;
299 #define BC_F_COPYIN 0x01 /* copy arguments in */
300 #define BC_F_COPYOUT 0x02 /* copy arguments out */
301 #define BC_F_SUSER 0x04 /* do super-user check */
303 const struct bridge_control bridge_control_table[] = {
304 { bridge_ioctl_add, sizeof(struct ifbreq),
305 BC_F_COPYIN|BC_F_SUSER },
306 { bridge_ioctl_del, sizeof(struct ifbreq),
307 BC_F_COPYIN|BC_F_SUSER },
309 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
310 BC_F_COPYIN|BC_F_COPYOUT },
311 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
312 BC_F_COPYIN|BC_F_SUSER },
314 { bridge_ioctl_scache, sizeof(struct ifbrparam),
315 BC_F_COPYIN|BC_F_SUSER },
316 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
317 BC_F_COPYOUT },
319 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
320 BC_F_COPYIN|BC_F_COPYOUT },
321 { bridge_ioctl_rts, sizeof(struct ifbaconf),
322 BC_F_COPYIN|BC_F_COPYOUT },
324 { bridge_ioctl_saddr, sizeof(struct ifbareq),
325 BC_F_COPYIN|BC_F_SUSER },
327 { bridge_ioctl_sto, sizeof(struct ifbrparam),
328 BC_F_COPYIN|BC_F_SUSER },
329 { bridge_ioctl_gto, sizeof(struct ifbrparam),
330 BC_F_COPYOUT },
332 { bridge_ioctl_daddr, sizeof(struct ifbareq),
333 BC_F_COPYIN|BC_F_SUSER },
335 { bridge_ioctl_flush, sizeof(struct ifbreq),
336 BC_F_COPYIN|BC_F_SUSER },
338 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
339 BC_F_COPYOUT },
340 { bridge_ioctl_spri, sizeof(struct ifbrparam),
341 BC_F_COPYIN|BC_F_SUSER },
343 { bridge_ioctl_ght, sizeof(struct ifbrparam),
344 BC_F_COPYOUT },
345 { bridge_ioctl_sht, sizeof(struct ifbrparam),
346 BC_F_COPYIN|BC_F_SUSER },
348 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
349 BC_F_COPYOUT },
350 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
351 BC_F_COPYIN|BC_F_SUSER },
353 { bridge_ioctl_gma, sizeof(struct ifbrparam),
354 BC_F_COPYOUT },
355 { bridge_ioctl_sma, sizeof(struct ifbrparam),
356 BC_F_COPYIN|BC_F_SUSER },
358 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
359 BC_F_COPYIN|BC_F_SUSER },
361 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
362 BC_F_COPYIN|BC_F_SUSER },
364 { bridge_ioctl_addspan, sizeof(struct ifbreq),
365 BC_F_COPYIN|BC_F_SUSER },
366 { bridge_ioctl_delspan, sizeof(struct ifbreq),
367 BC_F_COPYIN|BC_F_SUSER },
369 const int bridge_control_table_size =
370 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
372 LIST_HEAD(, bridge_softc) bridge_list;
374 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
375 bridge_clone_create,
376 bridge_clone_destroy, 0, IF_MAXUNIT);
378 static int
379 bridge_modevent(module_t mod, int type, void *data)
381 switch (type) {
382 case MOD_LOAD:
383 LIST_INIT(&bridge_list);
384 if_clone_attach(&bridge_cloner);
385 bridge_input_p = bridge_input;
386 bridge_output_p = bridge_output;
387 bridge_detach_cookie = EVENTHANDLER_REGISTER(
388 ifnet_detach_event, bridge_ifdetach, NULL,
389 EVENTHANDLER_PRI_ANY);
390 #if notyet
391 bstp_linkstate_p = bstp_linkstate;
392 #endif
393 break;
394 case MOD_UNLOAD:
395 if (!LIST_EMPTY(&bridge_list))
396 return (EBUSY);
397 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
398 bridge_detach_cookie);
399 if_clone_detach(&bridge_cloner);
400 bridge_input_p = NULL;
401 bridge_output_p = NULL;
402 #if notyet
403 bstp_linkstate_p = NULL;
404 #endif
405 break;
406 default:
407 return (EOPNOTSUPP);
409 return (0);
412 static moduledata_t bridge_mod = {
413 "if_bridge",
414 bridge_modevent,
418 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
422 * bridge_clone_create:
424 * Create a new bridge instance.
426 static int
427 bridge_clone_create(struct if_clone *ifc, int unit)
429 struct bridge_softc *sc;
430 struct ifnet *ifp;
431 u_char eaddr[6];
433 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
434 ifp = sc->sc_ifp = &sc->sc_if;
436 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
437 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
438 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
439 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
440 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
441 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
442 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
444 /* Initialize our routing table. */
445 bridge_rtable_init(sc);
447 callout_init(&sc->sc_brcallout);
448 callout_init(&sc->sc_bstpcallout);
450 LIST_INIT(&sc->sc_iflist);
451 LIST_INIT(&sc->sc_spanlist);
453 ifp->if_softc = sc;
454 if_initname(ifp, ifc->ifc_name, unit);
455 ifp->if_mtu = ETHERMTU;
456 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
457 ifp->if_ioctl = bridge_ioctl;
458 ifp->if_start = bridge_start;
459 ifp->if_init = bridge_init;
460 ifp->if_type = IFT_BRIDGE;
461 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
462 ifp->if_snd.ifq_maxlen = ifqmaxlen;
463 ifq_set_ready(&ifp->if_snd);
464 ifp->if_hdrlen = ETHER_HDR_LEN;
467 * Generate a random ethernet address and use the private AC:DE:48
468 * OUI code.
471 int rnd = karc4random();
472 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
473 rnd = karc4random();
474 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
476 eaddr[0] &= ~1; /* clear multicast bit */
477 eaddr[0] |= 2; /* set the LAA bit */
479 ether_ifattach(ifp, eaddr, NULL);
480 /* Now undo some of the damage... */
481 ifp->if_baudrate = 0;
482 ifp->if_type = IFT_BRIDGE;
484 crit_enter(); /* XXX MP */
485 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
486 crit_exit();
488 return (0);
491 static void
492 bridge_delete_dispatch(struct netmsg *nmsg)
494 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
495 struct bridge_softc *sc = lmsg->u.ms_resultp;
496 struct ifnet *bifp = sc->sc_ifp;
497 struct bridge_iflist *bif;
499 lwkt_serialize_enter(bifp->if_serializer);
501 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
502 bridge_delete_member(sc, bif, 0);
504 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL)
505 bridge_delete_span(sc, bif);
507 lwkt_serialize_exit(bifp->if_serializer);
509 lwkt_replymsg(lmsg, 0);
513 * bridge_clone_destroy:
515 * Destroy a bridge instance.
517 static void
518 bridge_clone_destroy(struct ifnet *ifp)
520 struct bridge_softc *sc = ifp->if_softc;
521 struct lwkt_msg *lmsg;
522 struct netmsg nmsg;
524 lwkt_serialize_enter(ifp->if_serializer);
526 bridge_stop(ifp);
527 ifp->if_flags &= ~IFF_UP;
529 callout_stop(&sc->sc_brcallout);
530 callout_stop(&sc->sc_bstpcallout);
532 lwkt_serialize_exit(ifp->if_serializer);
534 netmsg_init(&nmsg, &curthread->td_msgport, 0, bridge_delete_dispatch);
535 lmsg = &nmsg.nm_lmsg;
536 lmsg->u.ms_resultp = sc;
537 lwkt_domsg(cpu_portfn(0), lmsg, 0);
539 crit_enter(); /* XXX MP */
540 LIST_REMOVE(sc, sc_list);
541 crit_exit();
543 ether_ifdetach(ifp);
545 /* Tear down the routing table. */
546 bridge_rtable_fini(sc);
548 kfree(sc, M_DEVBUF);
552 * bridge_ioctl:
554 * Handle a control request from the operator.
556 static int
557 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
559 struct bridge_softc *sc = ifp->if_softc;
560 union {
561 struct ifbreq ifbreq;
562 struct ifbifconf ifbifconf;
563 struct ifbareq ifbareq;
564 struct ifbaconf ifbaconf;
565 struct ifbrparam ifbrparam;
566 } args;
567 struct ifdrv *ifd = (struct ifdrv *) data;
568 const struct bridge_control *bc;
569 int error = 0;
571 ASSERT_SERIALIZED(ifp->if_serializer);
573 switch (cmd) {
574 case SIOCADDMULTI:
575 case SIOCDELMULTI:
576 break;
578 case SIOCGDRVSPEC:
579 case SIOCSDRVSPEC:
580 if (ifd->ifd_cmd >= bridge_control_table_size) {
581 error = EINVAL;
582 break;
584 bc = &bridge_control_table[ifd->ifd_cmd];
586 if (cmd == SIOCGDRVSPEC &&
587 (bc->bc_flags & BC_F_COPYOUT) == 0) {
588 error = EINVAL;
589 break;
590 } else if (cmd == SIOCSDRVSPEC &&
591 (bc->bc_flags & BC_F_COPYOUT) != 0) {
592 error = EINVAL;
593 break;
596 if (bc->bc_flags & BC_F_SUSER) {
597 error = suser_cred(cr, NULL_CRED_OKAY);
598 if (error)
599 break;
602 if (ifd->ifd_len != bc->bc_argsize ||
603 ifd->ifd_len > sizeof(args)) {
604 error = EINVAL;
605 break;
608 memset(&args, 0, sizeof(args));
609 if (bc->bc_flags & BC_F_COPYIN) {
610 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
611 if (error)
612 break;
615 error = bridge_control(sc, cmd, bc->bc_func, &args);
616 if (error)
617 break;
619 if (bc->bc_flags & BC_F_COPYOUT)
620 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
621 break;
623 case SIOCSIFFLAGS:
624 if (!(ifp->if_flags & IFF_UP) &&
625 (ifp->if_flags & IFF_RUNNING)) {
627 * If interface is marked down and it is running,
628 * then stop it.
630 bridge_stop(ifp);
631 } else if ((ifp->if_flags & IFF_UP) &&
632 !(ifp->if_flags & IFF_RUNNING)) {
634 * If interface is marked up and it is stopped, then
635 * start it.
637 ifp->if_init(sc);
639 break;
641 case SIOCSIFMTU:
642 /* Do not allow the MTU to be changed on the bridge */
643 error = EINVAL;
644 break;
646 default:
647 error = ether_ioctl(ifp, cmd, data);
648 break;
650 return (error);
654 * bridge_mutecaps:
656 * Clear or restore unwanted capabilities on the member interface
658 static void
659 bridge_mutecaps(struct bridge_iflist *bif, int mute)
661 struct ifnet *ifp = bif->bif_ifp;
662 struct ifreq ifr;
663 int error;
665 if (ifp->if_ioctl == NULL)
666 return;
668 bzero(&ifr, sizeof(ifr));
669 ifr.ifr_reqcap = ifp->if_capenable;
671 if (mute) {
672 /* mask off and save capabilities */
673 bif->bif_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
674 if (bif->bif_mutecap != 0)
675 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
676 } else {
677 /* restore muted capabilities */
678 ifr.ifr_reqcap |= bif->bif_mutecap;
681 if (bif->bif_mutecap != 0) {
682 lwkt_serialize_enter(ifp->if_serializer);
683 error = ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
684 lwkt_serialize_exit(ifp->if_serializer);
689 * bridge_lookup_member:
691 * Lookup a bridge member interface.
693 static struct bridge_iflist *
694 bridge_lookup_member(struct bridge_softc *sc, const char *name)
696 struct bridge_iflist *bif;
697 struct ifnet *ifp;
699 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
700 ifp = bif->bif_ifp;
701 if (strcmp(ifp->if_xname, name) == 0)
702 return (bif);
705 return (NULL);
709 * bridge_lookup_member_if:
711 * Lookup a bridge member interface by ifnet*.
713 static struct bridge_iflist *
714 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
716 struct bridge_iflist *bif;
718 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
719 if (bif->bif_ifp == member_ifp)
720 return (bif);
723 return (NULL);
727 * bridge_delete_member:
729 * Delete the specified member interface.
731 static void
732 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
733 int gone)
735 struct ifnet *ifs = bif->bif_ifp;
736 struct ifnet *bifp = sc->sc_ifp;
738 ASSERT_SERIALIZED(bifp->if_serializer);
740 ifs->if_bridge = NULL;
743 * Release bridge interface's serializer:
744 * - To avoid possible dead lock.
745 * - netmsg_service_sync will block current thread.
747 lwkt_serialize_exit(bifp->if_serializer);
750 * Make sure that all protocol threads see 'ifs' if_bridge change.
752 netmsg_service_sync();
754 if (!gone) {
755 switch (ifs->if_type) {
756 case IFT_ETHER:
757 case IFT_L2VLAN:
759 * Take the interface out of promiscuous mode.
761 ifpromisc(ifs, 0);
762 bridge_mutecaps(bif, 0);
763 break;
765 case IFT_GIF:
766 break;
768 default:
769 panic("bridge_delete_member: impossible");
770 break;
774 lwkt_serialize_enter(bifp->if_serializer);
776 LIST_REMOVE(bif, bif_next);
778 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
780 kfree(bif, M_DEVBUF);
782 if (sc->sc_ifp->if_flags & IFF_RUNNING)
783 bstp_initialization(sc);
787 * bridge_delete_span:
789 * Delete the specified span interface.
791 static void
792 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
794 KASSERT(bif->bif_ifp->if_bridge == NULL,
795 ("%s: not a span interface", __func__));
797 LIST_REMOVE(bif, bif_next);
798 kfree(bif, M_DEVBUF);
801 static int
802 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
804 struct ifbreq *req = arg;
805 struct bridge_iflist *bif = NULL;
806 struct ifnet *ifs, *bifp;
807 int error = 0;
809 bifp = sc->sc_ifp;
810 ASSERT_SERIALIZED(bifp->if_serializer);
812 ifs = ifunit(req->ifbr_ifsname);
813 if (ifs == NULL)
814 return (ENOENT);
816 /* If it's in the span list, it can't be a member. */
817 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
818 if (ifs == bif->bif_ifp)
819 return (EBUSY);
821 /* Allow the first Ethernet member to define the MTU */
822 if (ifs->if_type != IFT_GIF) {
823 if (LIST_EMPTY(&sc->sc_iflist)) {
824 bifp->if_mtu = ifs->if_mtu;
825 } else if (bifp->if_mtu != ifs->if_mtu) {
826 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
827 return (EINVAL);
831 if (ifs->if_bridge == sc)
832 return (EEXIST);
834 if (ifs->if_bridge != NULL)
835 return (EBUSY);
837 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
838 bif->bif_ifp = ifs;
839 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
840 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
841 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
843 switch (ifs->if_type) {
844 case IFT_ETHER:
845 case IFT_L2VLAN:
847 * Release bridge interface's serializer to
848 * avoid possible dead lock.
850 lwkt_serialize_exit(bifp->if_serializer);
853 * Place the interface into promiscuous mode.
855 error = ifpromisc(ifs, 1);
856 if (error) {
857 lwkt_serialize_enter(bifp->if_serializer);
858 goto out;
861 bridge_mutecaps(bif, 1);
863 lwkt_serialize_enter(bifp->if_serializer);
864 break;
866 case IFT_GIF: /* :^) */
867 break;
869 default:
870 error = EINVAL;
871 goto out;
874 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
876 if (bifp->if_flags & IFF_RUNNING)
877 bstp_initialization(sc);
878 else
879 bstp_stop(sc);
882 * Everything has been setup, so let the member interface
883 * deliver packets to this bridge on its input/output path.
885 ifs->if_bridge = sc;
886 out:
887 if (error) {
888 if (bif != NULL)
889 kfree(bif, M_DEVBUF);
891 return (error);
894 static int
895 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
897 struct ifbreq *req = arg;
898 struct bridge_iflist *bif;
900 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
901 if (bif == NULL)
902 return (ENOENT);
904 bridge_delete_member(sc, bif, 0);
906 return (0);
909 static int
910 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
912 struct ifbreq *req = arg;
913 struct bridge_iflist *bif;
915 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
916 if (bif == NULL)
917 return (ENOENT);
919 req->ifbr_ifsflags = bif->bif_flags;
920 req->ifbr_state = bif->bif_state;
921 req->ifbr_priority = bif->bif_priority;
922 req->ifbr_path_cost = bif->bif_path_cost;
923 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
925 return (0);
928 static int
929 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
931 struct ifbreq *req = arg;
932 struct bridge_iflist *bif;
934 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
935 if (bif == NULL)
936 return (ENOENT);
938 if (req->ifbr_ifsflags & IFBIF_SPAN)
939 /* SPAN is readonly */
940 return (EINVAL);
942 if (req->ifbr_ifsflags & IFBIF_STP) {
943 switch (bif->bif_ifp->if_type) {
944 case IFT_ETHER:
945 /* These can do spanning tree. */
946 break;
948 default:
949 /* Nothing else can. */
950 return (EINVAL);
954 bif->bif_flags = req->ifbr_ifsflags;
956 if (sc->sc_ifp->if_flags & IFF_RUNNING)
957 bstp_initialization(sc);
959 return (0);
962 static int
963 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
965 struct ifbrparam *param = arg;
967 sc->sc_brtmax = param->ifbrp_csize;
968 bridge_rttrim(sc);
970 return (0);
973 static int
974 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
976 struct ifbrparam *param = arg;
978 param->ifbrp_csize = sc->sc_brtmax;
980 return (0);
983 static int
984 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
986 struct ifbifconf *bifc = arg;
987 struct bridge_iflist *bif;
988 struct ifbreq breq;
989 int count, len, error = 0;
991 count = 0;
992 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
993 count++;
994 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
995 count++;
997 if (bifc->ifbic_len == 0) {
998 bifc->ifbic_len = sizeof(breq) * count;
999 return (0);
1002 count = 0;
1003 len = bifc->ifbic_len;
1004 memset(&breq, 0, sizeof breq);
1005 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1006 if (len < sizeof(breq))
1007 break;
1009 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1010 sizeof(breq.ifbr_ifsname));
1011 breq.ifbr_ifsflags = bif->bif_flags;
1012 breq.ifbr_state = bif->bif_state;
1013 breq.ifbr_priority = bif->bif_priority;
1014 breq.ifbr_path_cost = bif->bif_path_cost;
1015 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
1016 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
1017 if (error)
1018 break;
1019 count++;
1020 len -= sizeof(breq);
1022 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1023 if (len < sizeof(breq))
1024 break;
1026 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1027 sizeof(breq.ifbr_ifsname));
1028 breq.ifbr_ifsflags = bif->bif_flags;
1029 breq.ifbr_state = bif->bif_state;
1030 breq.ifbr_priority = bif->bif_priority;
1031 breq.ifbr_path_cost = bif->bif_path_cost;
1032 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
1033 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
1034 if (error)
1035 break;
1036 count++;
1037 len -= sizeof(breq);
1040 bifc->ifbic_len = sizeof(breq) * count;
1041 return (error);
1044 static int
1045 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1047 struct ifbaconf *bac = arg;
1048 struct bridge_rtnode *brt;
1049 struct ifbareq bareq;
1050 int count = 0, error = 0, len;
1052 if (bac->ifbac_len == 0)
1053 return (0);
1055 len = bac->ifbac_len;
1056 memset(&bareq, 0, sizeof(bareq));
1057 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1058 if (len < sizeof(bareq))
1059 goto out;
1060 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1061 sizeof(bareq.ifba_ifsname));
1062 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1063 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1064 time_second < brt->brt_expire)
1065 bareq.ifba_expire = brt->brt_expire - time_second;
1066 else
1067 bareq.ifba_expire = 0;
1068 bareq.ifba_flags = brt->brt_flags;
1070 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1071 if (error)
1072 goto out;
1073 count++;
1074 len -= sizeof(bareq);
1076 out:
1077 bac->ifbac_len = sizeof(bareq) * count;
1078 return (error);
1081 static int
1082 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1084 struct ifbareq *req = arg;
1085 struct bridge_iflist *bif;
1086 int error;
1088 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1089 if (bif == NULL)
1090 return (ENOENT);
1092 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1093 req->ifba_flags);
1095 return (error);
1098 static int
1099 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1101 struct ifbrparam *param = arg;
1103 sc->sc_brttimeout = param->ifbrp_ctime;
1105 return (0);
1108 static int
1109 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1111 struct ifbrparam *param = arg;
1113 param->ifbrp_ctime = sc->sc_brttimeout;
1115 return (0);
1118 static int
1119 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1121 struct ifbareq *req = arg;
1123 return (bridge_rtdaddr(sc, req->ifba_dst));
1126 static int
1127 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1129 struct ifbreq *req = arg;
1131 bridge_rtflush(sc, req->ifbr_ifsflags);
1133 return (0);
1136 static int
1137 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1139 struct ifbrparam *param = arg;
1141 param->ifbrp_prio = sc->sc_bridge_priority;
1143 return (0);
1146 static int
1147 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1149 struct ifbrparam *param = arg;
1151 sc->sc_bridge_priority = param->ifbrp_prio;
1153 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1154 bstp_initialization(sc);
1156 return (0);
1159 static int
1160 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1162 struct ifbrparam *param = arg;
1164 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1166 return (0);
1169 static int
1170 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1172 struct ifbrparam *param = arg;
1174 if (param->ifbrp_hellotime == 0)
1175 return (EINVAL);
1176 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1178 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1179 bstp_initialization(sc);
1181 return (0);
1184 static int
1185 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1187 struct ifbrparam *param = arg;
1189 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1191 return (0);
1194 static int
1195 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1197 struct ifbrparam *param = arg;
1199 if (param->ifbrp_fwddelay == 0)
1200 return (EINVAL);
1201 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1203 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1204 bstp_initialization(sc);
1206 return (0);
1209 static int
1210 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1212 struct ifbrparam *param = arg;
1214 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1216 return (0);
1219 static int
1220 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1222 struct ifbrparam *param = arg;
1224 if (param->ifbrp_maxage == 0)
1225 return (EINVAL);
1226 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1228 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1229 bstp_initialization(sc);
1231 return (0);
1234 static int
1235 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1237 struct ifbreq *req = arg;
1238 struct bridge_iflist *bif;
1240 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1241 if (bif == NULL)
1242 return (ENOENT);
1244 bif->bif_priority = req->ifbr_priority;
1246 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1247 bstp_initialization(sc);
1249 return (0);
1252 static int
1253 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1255 struct ifbreq *req = arg;
1256 struct bridge_iflist *bif;
1258 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1259 if (bif == NULL)
1260 return (ENOENT);
1262 bif->bif_path_cost = req->ifbr_path_cost;
1264 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1265 bstp_initialization(sc);
1267 return (0);
1270 static int
1271 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1273 struct ifbreq *req = arg;
1274 struct bridge_iflist *bif = NULL;
1275 struct ifnet *ifs;
1277 ifs = ifunit(req->ifbr_ifsname);
1278 if (ifs == NULL)
1279 return (ENOENT);
1281 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1282 if (ifs == bif->bif_ifp)
1283 return (EBUSY);
1285 if (ifs->if_bridge != NULL)
1286 return (EBUSY);
1288 switch (ifs->if_type) {
1289 case IFT_ETHER:
1290 case IFT_GIF:
1291 case IFT_L2VLAN:
1292 break;
1293 default:
1294 return (EINVAL);
1297 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
1299 bif->bif_ifp = ifs;
1300 bif->bif_flags = IFBIF_SPAN;
1302 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1304 return (0);
1307 static int
1308 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1310 struct ifbreq *req = arg;
1311 struct bridge_iflist *bif;
1312 struct ifnet *ifs;
1314 ifs = ifunit(req->ifbr_ifsname);
1315 if (ifs == NULL)
1316 return (ENOENT);
1318 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1319 if (ifs == bif->bif_ifp)
1320 break;
1322 if (bif == NULL)
1323 return (ENOENT);
1325 bridge_delete_span(sc, bif);
1327 return (0);
1330 static void
1331 bridge_ifdetach_dispatch(struct netmsg *nmsg)
1333 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
1334 struct ifnet *ifp, *bifp;
1335 struct bridge_softc *sc;
1336 struct bridge_iflist *bif;
1338 ifp = lmsg->u.ms_resultp;
1339 sc = ifp->if_bridge;
1341 /* Check if the interface is a bridge member */
1342 if (sc != NULL) {
1343 bifp = sc->sc_ifp;
1345 lwkt_serialize_enter(bifp->if_serializer);
1347 bif = bridge_lookup_member_if(sc, ifp);
1348 if (bif != NULL) {
1349 bridge_delete_member(sc, bif, 1);
1350 } else {
1351 /* XXX Why bif will be NULL? */
1354 lwkt_serialize_exit(bifp->if_serializer);
1355 goto reply;
1358 crit_enter(); /* XXX MP */
1360 /* Check if the interface is a span port */
1361 LIST_FOREACH(sc, &bridge_list, sc_list) {
1362 bifp = sc->sc_ifp;
1364 lwkt_serialize_enter(bifp->if_serializer);
1366 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1367 if (ifp == bif->bif_ifp) {
1368 bridge_delete_span(sc, bif);
1369 break;
1372 lwkt_serialize_exit(bifp->if_serializer);
1375 crit_exit();
1377 reply:
1378 lwkt_replymsg(lmsg, 0);
1382 * bridge_ifdetach:
1384 * Detach an interface from a bridge. Called when a member
1385 * interface is detaching.
1387 static void
1388 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1390 struct lwkt_msg *lmsg;
1391 struct netmsg nmsg;
1393 netmsg_init(&nmsg, &curthread->td_msgport, 0, bridge_ifdetach_dispatch);
1394 lmsg = &nmsg.nm_lmsg;
1395 lmsg->u.ms_resultp = ifp;
1397 lwkt_domsg(cpu_portfn(0), lmsg, 0);
1401 * bridge_init:
1403 * Initialize a bridge interface.
1405 static void
1406 bridge_init(void *xsc)
1408 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1409 struct ifnet *ifp = sc->sc_ifp;
1411 ASSERT_SERIALIZED(ifp->if_serializer);
1413 if (ifp->if_flags & IFF_RUNNING)
1414 return;
1416 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1417 bridge_timer, sc);
1419 ifp->if_flags |= IFF_RUNNING;
1420 bstp_initialization(sc);
1421 return;
1425 * bridge_stop:
1427 * Stop the bridge interface.
1429 static void
1430 bridge_stop(struct ifnet *ifp)
1432 struct bridge_softc *sc = ifp->if_softc;
1434 ASSERT_SERIALIZED(ifp->if_serializer);
1436 if ((ifp->if_flags & IFF_RUNNING) == 0)
1437 return;
1439 callout_stop(&sc->sc_brcallout);
1440 bstp_stop(sc);
1442 bridge_rtflush(sc, IFBF_FLUSHDYN);
1444 ifp->if_flags &= ~IFF_RUNNING;
1447 static void
1448 bridge_enqueue_internal(struct ifnet *dst_ifp, struct mbuf *m,
1449 netisr_fn_t handler)
1451 struct netmsg_packet *nmp;
1452 lwkt_port_t port;
1453 int cpu = mycpu->gd_cpuid;
1455 nmp = &m->m_hdr.mh_netmsg;
1456 netmsg_init(&nmp->nm_netmsg, &netisr_apanic_rport, 0, handler);
1457 nmp->nm_packet = m;
1458 nmp->nm_netmsg.nm_lmsg.u.ms_resultp = dst_ifp;
1460 port = cpu_portfn(cpu);
1461 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
1464 static void
1465 bridge_pfil_enqueue(struct ifnet *dst_ifp, struct mbuf *m,
1466 int runfilt)
1468 netisr_fn_t handler;
1470 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1471 #ifdef INET6
1472 || inet6_pfil_hook.ph_hashooks > 0
1473 #endif
1474 )) {
1475 handler = bridge_pfil_enqueue_handler;
1476 } else {
1477 handler = bridge_enqueue_handler;
1479 bridge_enqueue_internal(dst_ifp, m, handler);
1483 * bridge_enqueue:
1485 * Enqueue a packet on a bridge member interface.
1488 void
1489 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1491 bridge_enqueue_internal(dst_ifp, m, bridge_enqueue_handler);
1495 * bridge_output:
1497 * Send output from a bridge member interface. This
1498 * performs the bridging function for locally originated
1499 * packets.
1501 * The mbuf has the Ethernet header already attached. We must
1502 * enqueue or free the mbuf before returning.
1504 static int
1505 bridge_output(struct ifnet *ifp, struct mbuf *m)
1507 struct bridge_softc *sc = ifp->if_bridge;
1508 struct ether_header *eh;
1509 struct ifnet *dst_if;
1511 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
1514 * Make sure that we are still a member of a bridge interface.
1516 if (sc == NULL) {
1517 m_freem(m);
1518 return (0);
1521 if (m->m_len < ETHER_HDR_LEN) {
1522 m = m_pullup(m, ETHER_HDR_LEN);
1523 if (m == NULL)
1524 return (0);
1527 /* Serialize our bridge interface. */
1528 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1530 eh = mtod(m, struct ether_header *);
1533 * If bridge is down, but the original output interface is up,
1534 * go ahead and send out that interface. Otherwise, the packet
1535 * is dropped below.
1537 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
1538 dst_if = ifp;
1539 goto sendunicast;
1543 * If the packet is a multicast, or we don't know a better way to
1544 * get there, send to all interfaces.
1546 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1547 dst_if = NULL;
1548 else
1549 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1550 if (dst_if == NULL) {
1551 struct bridge_iflist *bif;
1552 struct mbuf *mc;
1553 int used = 0;
1555 bridge_span(sc, m);
1557 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1558 dst_if = bif->bif_ifp;
1559 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1560 continue;
1563 * If this is not the original output interface,
1564 * and the interface is participating in spanning
1565 * tree, make sure the port is in a state that
1566 * allows forwarding.
1568 if (dst_if != ifp &&
1569 (bif->bif_flags & IFBIF_STP) != 0) {
1570 switch (bif->bif_state) {
1571 case BSTP_IFSTATE_BLOCKING:
1572 case BSTP_IFSTATE_LISTENING:
1573 case BSTP_IFSTATE_DISABLED:
1574 continue;
1578 if (LIST_NEXT(bif, bif_next) == NULL) {
1579 used = 1;
1580 mc = m;
1581 } else {
1582 mc = m_copypacket(m, MB_DONTWAIT);
1583 if (mc == NULL) {
1584 sc->sc_ifp->if_oerrors++;
1585 continue;
1588 bridge_enqueue(dst_if, mc);
1590 if (used == 0)
1591 m_freem(m);
1592 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1593 return (0);
1596 sendunicast:
1598 * XXX Spanning tree consideration here?
1601 bridge_span(sc, m);
1602 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1603 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1604 m_freem(m);
1605 else
1606 bridge_enqueue(dst_if, m);
1607 return (0);
1611 * bridge_start:
1613 * Start output on a bridge.
1616 static void
1617 bridge_start(struct ifnet *ifp)
1619 struct bridge_softc *sc = ifp->if_softc;
1621 ASSERT_SERIALIZED(ifp->if_serializer);
1623 ifp->if_flags |= IFF_OACTIVE;
1624 for (;;) {
1625 struct ifnet *dst_if = NULL;
1626 struct ether_header *eh;
1627 struct mbuf *m;
1629 m = ifq_dequeue(&ifp->if_snd, NULL);
1630 if (m == NULL)
1631 break;
1633 if (m->m_len < sizeof(*eh)) {
1634 m = m_pullup(m, sizeof(*eh));
1635 if (m == NULL) {
1636 ifp->if_oerrors++;
1637 continue;
1640 eh = mtod(m, struct ether_header *);
1642 BPF_MTAP(ifp, m);
1643 ifp->if_opackets++;
1645 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1646 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1648 if (dst_if == NULL)
1649 bridge_broadcast(sc, ifp, m, 0);
1650 else
1651 bridge_enqueue(dst_if, m);
1653 ifp->if_flags &= ~IFF_OACTIVE;
1657 * bridge_forward:
1659 * The forwarding function of the bridge.
1661 static void
1662 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1664 struct bridge_iflist *bif;
1665 struct ifnet *src_if, *dst_if, *ifp;
1666 struct ether_header *eh;
1668 src_if = m->m_pkthdr.rcvif;
1669 ifp = sc->sc_ifp;
1671 ASSERT_SERIALIZED(ifp->if_serializer);
1673 ifp->if_ipackets++;
1674 ifp->if_ibytes += m->m_pkthdr.len;
1677 * Look up the bridge_iflist.
1679 bif = bridge_lookup_member_if(sc, src_if);
1680 if (bif == NULL) {
1681 /* Interface is not a bridge member (anymore?) */
1682 m_freem(m);
1683 return;
1686 if (bif->bif_flags & IFBIF_STP) {
1687 switch (bif->bif_state) {
1688 case BSTP_IFSTATE_BLOCKING:
1689 case BSTP_IFSTATE_LISTENING:
1690 case BSTP_IFSTATE_DISABLED:
1691 m_freem(m);
1692 return;
1696 eh = mtod(m, struct ether_header *);
1699 * If the interface is learning, and the source
1700 * address is valid and not multicast, record
1701 * the address.
1703 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1704 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1705 (eh->ether_shost[0] == 0 &&
1706 eh->ether_shost[1] == 0 &&
1707 eh->ether_shost[2] == 0 &&
1708 eh->ether_shost[3] == 0 &&
1709 eh->ether_shost[4] == 0 &&
1710 eh->ether_shost[5] == 0) == 0) {
1711 bridge_rtupdate(sc, eh->ether_shost, src_if, 0, IFBAF_DYNAMIC);
1714 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1715 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1716 m_freem(m);
1717 return;
1721 * At this point, the port either doesn't participate
1722 * in spanning tree or it is in the forwarding state.
1726 * If the packet is unicast, destined for someone on
1727 * "this" side of the bridge, drop it.
1729 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1730 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1731 if (src_if == dst_if) {
1732 m_freem(m);
1733 return;
1735 } else {
1736 /* ...forward it to all interfaces. */
1737 sc->sc_ifp->if_imcasts++;
1738 dst_if = NULL;
1741 if (dst_if == NULL) {
1742 bridge_broadcast(sc, src_if, m, 1);
1743 return;
1747 * At this point, we're dealing with a unicast frame
1748 * going to a different interface.
1750 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1751 m_freem(m);
1752 return;
1754 bif = bridge_lookup_member_if(sc, dst_if);
1755 if (bif == NULL) {
1756 /* Not a member of the bridge (anymore?) */
1757 m_freem(m);
1758 return;
1761 if (bif->bif_flags & IFBIF_STP) {
1762 switch (bif->bif_state) {
1763 case BSTP_IFSTATE_DISABLED:
1764 case BSTP_IFSTATE_BLOCKING:
1765 m_freem(m);
1766 return;
1770 lwkt_serialize_exit(ifp->if_serializer);
1772 /* run the packet filter */
1773 if (inet_pfil_hook.ph_hashooks > 0
1774 #ifdef INET6
1775 || inet6_pfil_hook.ph_hashooks > 0
1776 #endif
1778 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1779 goto done;
1780 if (m == NULL)
1781 goto done;
1783 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
1784 goto done;
1785 if (m == NULL)
1786 goto done;
1788 bridge_handoff(dst_if, m);
1791 * ifp's serializer was held on entry and is expected to be held
1792 * on return.
1794 done:
1795 lwkt_serialize_enter(ifp->if_serializer);
1799 * bridge_input:
1801 * Receive input from a member interface. Queue the packet for
1802 * bridging if it is not for us.
1804 static struct mbuf *
1805 bridge_input(struct ifnet *ifp, struct mbuf *m)
1807 struct bridge_softc *sc = ifp->if_bridge;
1808 struct bridge_iflist *bif;
1809 struct ifnet *bifp, *new_ifp;
1810 struct ether_header *eh;
1811 struct mbuf *mc, *mc2;
1814 * Make sure that we are still a member of a bridge interface.
1816 if (sc == NULL)
1817 return m;
1819 new_ifp = NULL;
1820 bifp = sc->sc_ifp;
1822 lwkt_serialize_enter(bifp->if_serializer);
1824 if ((bifp->if_flags & IFF_RUNNING) == 0)
1825 goto out;
1828 * Implement support for bridge monitoring. If this flag has been
1829 * set on this interface, discard the packet once we push it through
1830 * the bpf(4) machinery, but before we do, increment the byte and
1831 * packet counters associated with this interface.
1833 if ((bifp->if_flags & IFF_MONITOR) != 0) {
1834 m->m_pkthdr.rcvif = bifp;
1835 BPF_MTAP(bifp, m);
1836 bifp->if_ipackets++;
1837 bifp->if_ibytes += m->m_pkthdr.len;
1838 m_freem(m);
1839 m = NULL;
1840 goto out;
1843 eh = mtod(m, struct ether_header *);
1845 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
1847 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
1849 * If the packet is for us, set the packets source as the
1850 * bridge, and return the packet back to ifnet.if_input for
1851 * local processing.
1853 KASSERT(bifp->if_bridge == NULL,
1854 ("loop created in bridge_input"));
1855 new_ifp = bifp;
1856 goto out;
1860 * Tap all packets arriving on the bridge, no matter if
1861 * they are local destinations or not. In is in.
1863 BPF_MTAP(bifp, m);
1865 bif = bridge_lookup_member_if(sc, ifp);
1866 if (bif == NULL)
1867 goto out;
1869 bridge_span(sc, m);
1871 if (m->m_flags & (M_BCAST | M_MCAST)) {
1872 /* Tap off 802.1D packets; they do not get forwarded. */
1873 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1874 ETHER_ADDR_LEN) == 0) {
1875 m = bstp_input(sc, bif, m);
1876 KASSERT(m == NULL,
1877 ("attempt to deliver 802.1D packet\n"));
1878 goto out;
1881 if (bif->bif_flags & IFBIF_STP) {
1882 switch (bif->bif_state) {
1883 case BSTP_IFSTATE_BLOCKING:
1884 case BSTP_IFSTATE_LISTENING:
1885 case BSTP_IFSTATE_DISABLED:
1886 goto out;
1891 * Make a deep copy of the packet and enqueue the copy
1892 * for bridge processing; return the original packet for
1893 * local processing.
1895 mc = m_dup(m, MB_DONTWAIT);
1896 if (mc == NULL)
1897 goto out;
1899 bridge_forward(sc, mc);
1902 * Reinject the mbuf as arriving on the bridge so we have a
1903 * chance at claiming multicast packets. We can not loop back
1904 * here from ether_input as a bridge is never a member of a
1905 * bridge.
1907 KASSERT(bifp->if_bridge == NULL,
1908 ("loop created in bridge_input"));
1909 mc2 = m_dup(m, MB_DONTWAIT);
1910 #ifdef notyet
1911 if (mc2 != NULL) {
1912 /* Keep the layer3 header aligned */
1913 int i = min(mc2->m_pkthdr.len, max_protohdr);
1914 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
1916 #endif
1917 if (mc2 != NULL) {
1918 mc2->m_pkthdr.rcvif = bifp;
1919 bifp->if_ipackets++;
1920 bifp->if_input(bifp, mc2);
1923 /* Return the original packet for local processing. */
1924 goto out;
1927 if (bif->bif_flags & IFBIF_STP) {
1928 switch (bif->bif_state) {
1929 case BSTP_IFSTATE_BLOCKING:
1930 case BSTP_IFSTATE_LISTENING:
1931 case BSTP_IFSTATE_DISABLED:
1932 goto out;
1937 * Unicast. Make sure it's not for us.
1939 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1940 if (bif->bif_ifp->if_type != IFT_ETHER)
1941 continue;
1943 /* It is destined for us. */
1944 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1945 ETHER_ADDR_LEN) == 0) {
1946 if (bif->bif_flags & IFBIF_LEARNING) {
1947 bridge_rtupdate(sc,
1948 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1951 if (bif->bif_ifp != ifp) {
1952 /* XXX loop prevention */
1953 m->m_flags |= M_PROTO1;
1954 new_ifp = bif->bif_ifp;
1956 goto out;
1959 /* We just received a packet that we sent out. */
1960 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1961 ETHER_ADDR_LEN) == 0) {
1962 m_freem(m);
1963 m = NULL;
1964 goto out;
1968 /* Perform the bridge forwarding function. */
1969 bridge_forward(sc, m);
1970 m = NULL;
1971 out:
1972 lwkt_serialize_exit(bifp->if_serializer);
1974 if (new_ifp != NULL) {
1975 lwkt_serialize_enter(new_ifp->if_serializer);
1977 m->m_pkthdr.rcvif = new_ifp;
1978 new_ifp->if_ipackets++;
1979 new_ifp->if_input(new_ifp, m);
1980 m = NULL;
1982 lwkt_serialize_exit(new_ifp->if_serializer);
1984 return (m);
1988 * bridge_broadcast:
1990 * Send a frame to all interfaces that are members of
1991 * the bridge, except for the one on which the packet
1992 * arrived.
1994 static void
1995 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1996 struct mbuf *m, int runfilt)
1998 struct bridge_iflist *bif;
1999 struct mbuf *mc;
2000 struct ifnet *dst_if, *bifp;
2001 int used = 0;
2003 bifp = sc->sc_ifp;
2005 ASSERT_SERIALIZED(bifp->if_serializer);
2007 /* run the packet filter */
2008 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
2009 #ifdef INET6
2010 || inet6_pfil_hook.ph_hashooks > 0
2011 #endif
2012 )) {
2013 lwkt_serialize_exit(bifp->if_serializer);
2015 /* Filter on the bridge interface before broadcasting */
2017 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
2018 goto filt;
2019 if (m == NULL)
2020 goto filt;
2022 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
2023 m = NULL;
2024 filt:
2025 lwkt_serialize_enter(bifp->if_serializer);
2026 if (m == NULL)
2027 return;
2030 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
2031 dst_if = bif->bif_ifp;
2032 if (dst_if == src_if)
2033 continue;
2035 if (bif->bif_flags & IFBIF_STP) {
2036 switch (bif->bif_state) {
2037 case BSTP_IFSTATE_BLOCKING:
2038 case BSTP_IFSTATE_DISABLED:
2039 continue;
2043 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2044 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2045 continue;
2047 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2048 continue;
2050 if (LIST_NEXT(bif, bif_next) == NULL) {
2051 mc = m;
2052 used = 1;
2053 } else {
2054 mc = m_copypacket(m, MB_DONTWAIT);
2055 if (mc == NULL) {
2056 sc->sc_ifp->if_oerrors++;
2057 continue;
2060 bridge_pfil_enqueue(dst_if, mc, runfilt);
2062 if (used == 0)
2063 m_freem(m);
2067 * bridge_span:
2069 * Duplicate a packet out one or more interfaces that are in span mode,
2070 * the original mbuf is unmodified.
2072 static void
2073 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2075 struct bridge_iflist *bif;
2076 struct ifnet *dst_if;
2077 struct mbuf *mc;
2079 if (LIST_EMPTY(&sc->sc_spanlist))
2080 return;
2082 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2083 dst_if = bif->bif_ifp;
2085 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2086 continue;
2088 mc = m_copypacket(m, MB_DONTWAIT);
2089 if (mc == NULL) {
2090 sc->sc_ifp->if_oerrors++;
2091 continue;
2094 bridge_enqueue(dst_if, mc);
2099 * bridge_rtupdate:
2101 * Add a bridge routing entry.
2102 * Can be called from interrupt context.
2104 static int
2105 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2106 struct ifnet *dst_if, int setflags, uint8_t flags)
2108 struct bridge_rtnode *brt;
2109 int error;
2112 * A route for this destination might already exist. If so,
2113 * update it, otherwise create a new one.
2115 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2116 if (sc->sc_brtcnt >= sc->sc_brtmax)
2117 return (ENOSPC);
2120 * Allocate a new bridge forwarding node, and
2121 * initialize the expiration time and Ethernet
2122 * address.
2124 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2125 M_INTNOWAIT|M_ZERO);
2126 if (brt == NULL)
2127 return (ENOMEM);
2129 brt->brt_flags = IFBAF_DYNAMIC;
2130 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2132 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2133 kfree(brt, M_DEVBUF);
2134 return (error);
2138 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2139 brt->brt_ifp = dst_if;
2140 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2141 brt->brt_expire = time_second + sc->sc_brttimeout;
2142 if (setflags)
2143 brt->brt_flags = flags;
2145 return (0);
2149 * bridge_rtlookup:
2151 * Lookup the destination interface for an address.
2153 static struct ifnet *
2154 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2156 struct bridge_rtnode *brt;
2158 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2159 return (NULL);
2161 return (brt->brt_ifp);
2165 * bridge_rttrim:
2167 * Trim the routine table so that we have a number
2168 * of routing entries less than or equal to the
2169 * maximum number.
2171 static void
2172 bridge_rttrim(struct bridge_softc *sc)
2174 struct bridge_rtnode *brt, *nbrt;
2176 /* Make sure we actually need to do this. */
2177 if (sc->sc_brtcnt <= sc->sc_brtmax)
2178 return;
2180 /* Force an aging cycle; this might trim enough addresses. */
2181 bridge_rtage(sc);
2182 if (sc->sc_brtcnt <= sc->sc_brtmax)
2183 return;
2185 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2186 nbrt = LIST_NEXT(brt, brt_list);
2187 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2188 bridge_rtnode_destroy(sc, brt);
2189 if (sc->sc_brtcnt <= sc->sc_brtmax)
2190 return;
2196 * bridge_timer:
2198 * Aging timer for the bridge.
2200 static void
2201 bridge_timer(void *arg)
2203 struct bridge_softc *sc = arg;
2205 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
2207 bridge_rtage(sc);
2209 if (sc->sc_ifp->if_flags & IFF_RUNNING)
2210 callout_reset(&sc->sc_brcallout,
2211 bridge_rtable_prune_period * hz, bridge_timer, sc);
2213 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
2217 * bridge_rtage:
2219 * Perform an aging cycle.
2221 static void
2222 bridge_rtage(struct bridge_softc *sc)
2224 struct bridge_rtnode *brt, *nbrt;
2226 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2227 nbrt = LIST_NEXT(brt, brt_list);
2228 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2229 if (time_second >= brt->brt_expire)
2230 bridge_rtnode_destroy(sc, brt);
2236 * bridge_rtflush:
2238 * Remove all dynamic addresses from the bridge.
2240 static void
2241 bridge_rtflush(struct bridge_softc *sc, int full)
2243 struct bridge_rtnode *brt, *nbrt;
2245 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2246 nbrt = LIST_NEXT(brt, brt_list);
2247 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2248 bridge_rtnode_destroy(sc, brt);
2253 * bridge_rtdaddr:
2255 * Remove an address from the table.
2257 static int
2258 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2260 struct bridge_rtnode *brt;
2262 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2263 return (ENOENT);
2265 bridge_rtnode_destroy(sc, brt);
2266 return (0);
2270 * bridge_rtdelete:
2272 * Delete routes to a speicifc member interface.
2274 void
2275 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2277 struct bridge_rtnode *brt, *nbrt;
2279 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2280 nbrt = LIST_NEXT(brt, brt_list);
2281 if (brt->brt_ifp == ifp && (full ||
2282 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2283 bridge_rtnode_destroy(sc, brt);
2288 * bridge_rtable_init:
2290 * Initialize the route table for this bridge.
2292 static int
2293 bridge_rtable_init(struct bridge_softc *sc)
2295 int i;
2297 sc->sc_rthash = kmalloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2298 M_DEVBUF, M_WAITOK);
2300 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2301 LIST_INIT(&sc->sc_rthash[i]);
2303 sc->sc_rthash_key = karc4random();
2305 LIST_INIT(&sc->sc_rtlist);
2307 return (0);
2311 * bridge_rtable_fini:
2313 * Deconstruct the route table for this bridge.
2315 static void
2316 bridge_rtable_fini(struct bridge_softc *sc)
2319 kfree(sc->sc_rthash, M_DEVBUF);
2323 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2324 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2326 #define mix(a, b, c) \
2327 do { \
2328 a -= b; a -= c; a ^= (c >> 13); \
2329 b -= c; b -= a; b ^= (a << 8); \
2330 c -= a; c -= b; c ^= (b >> 13); \
2331 a -= b; a -= c; a ^= (c >> 12); \
2332 b -= c; b -= a; b ^= (a << 16); \
2333 c -= a; c -= b; c ^= (b >> 5); \
2334 a -= b; a -= c; a ^= (c >> 3); \
2335 b -= c; b -= a; b ^= (a << 10); \
2336 c -= a; c -= b; c ^= (b >> 15); \
2337 } while (/*CONSTCOND*/0)
2339 static __inline uint32_t
2340 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2342 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2344 b += addr[5] << 8;
2345 b += addr[4];
2346 a += addr[3] << 24;
2347 a += addr[2] << 16;
2348 a += addr[1] << 8;
2349 a += addr[0];
2351 mix(a, b, c);
2353 return (c & BRIDGE_RTHASH_MASK);
2356 #undef mix
2358 static int
2359 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2361 int i, d;
2363 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2364 d = ((int)a[i]) - ((int)b[i]);
2367 return (d);
2371 * bridge_rtnode_lookup:
2373 * Look up a bridge route node for the specified destination.
2375 static struct bridge_rtnode *
2376 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2378 struct bridge_rtnode *brt;
2379 uint32_t hash;
2380 int dir;
2382 hash = bridge_rthash(sc, addr);
2383 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2384 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2385 if (dir == 0)
2386 return (brt);
2387 if (dir > 0)
2388 return (NULL);
2391 return (NULL);
2395 * bridge_rtnode_insert:
2397 * Insert the specified bridge node into the route table. We
2398 * assume the entry is not already in the table.
2400 static int
2401 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2403 struct bridge_rtnode *lbrt;
2404 uint32_t hash;
2405 int dir;
2407 hash = bridge_rthash(sc, brt->brt_addr);
2409 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2410 if (lbrt == NULL) {
2411 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2412 goto out;
2415 do {
2416 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2417 if (dir == 0)
2418 return (EEXIST);
2419 if (dir > 0) {
2420 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2421 goto out;
2423 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2424 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2425 goto out;
2427 lbrt = LIST_NEXT(lbrt, brt_hash);
2428 } while (lbrt != NULL);
2430 #ifdef DIAGNOSTIC
2431 panic("bridge_rtnode_insert: impossible");
2432 #endif
2434 out:
2435 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2436 sc->sc_brtcnt++;
2438 return (0);
2442 * bridge_rtnode_destroy:
2444 * Destroy a bridge rtnode.
2446 static void
2447 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2450 LIST_REMOVE(brt, brt_hash);
2452 LIST_REMOVE(brt, brt_list);
2453 sc->sc_brtcnt--;
2454 kfree(brt, M_DEVBUF);
2458 * Send bridge packets through pfil if they are one of the types pfil can deal
2459 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2460 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2461 * that interface.
2463 static int
2464 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2466 int snap, error, i, hlen;
2467 struct ether_header *eh1, eh2;
2468 struct ip *ip;
2469 struct llc llc1;
2470 u_int16_t ether_type;
2472 snap = 0;
2473 error = -1; /* Default error if not error == 0 */
2475 if (pfil_bridge == 0 && pfil_member == 0)
2476 return (0); /* filtering is disabled */
2478 i = min((*mp)->m_pkthdr.len, max_protohdr);
2479 if ((*mp)->m_len < i) {
2480 *mp = m_pullup(*mp, i);
2481 if (*mp == NULL) {
2482 kprintf("%s: m_pullup failed\n", __func__);
2483 return (-1);
2487 eh1 = mtod(*mp, struct ether_header *);
2488 ether_type = ntohs(eh1->ether_type);
2491 * Check for SNAP/LLC.
2493 if (ether_type < ETHERMTU) {
2494 struct llc *llc2 = (struct llc *)(eh1 + 1);
2496 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2497 llc2->llc_dsap == LLC_SNAP_LSAP &&
2498 llc2->llc_ssap == LLC_SNAP_LSAP &&
2499 llc2->llc_control == LLC_UI) {
2500 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2501 snap = 1;
2506 * If we're trying to filter bridge traffic, don't look at anything
2507 * other than IP and ARP traffic. If the filter doesn't understand
2508 * IPv6, don't allow IPv6 through the bridge either. This is lame
2509 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2510 * but of course we don't have an AppleTalk filter to begin with.
2511 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2512 * ARP traffic.)
2514 switch (ether_type) {
2515 case ETHERTYPE_ARP:
2516 case ETHERTYPE_REVARP:
2517 return (0); /* Automatically pass */
2518 case ETHERTYPE_IP:
2519 #ifdef INET6
2520 case ETHERTYPE_IPV6:
2521 #endif /* INET6 */
2522 break;
2523 default:
2525 * Check to see if the user wants to pass non-ip
2526 * packets, these will not be checked by pfil(9) and
2527 * passed unconditionally so the default is to drop.
2529 if (pfil_onlyip)
2530 goto bad;
2533 /* Strip off the Ethernet header and keep a copy. */
2534 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2535 m_adj(*mp, ETHER_HDR_LEN);
2537 /* Strip off snap header, if present */
2538 if (snap) {
2539 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2540 m_adj(*mp, sizeof(struct llc));
2544 * Check the IP header for alignment and errors
2546 if (dir == PFIL_IN) {
2547 switch (ether_type) {
2548 case ETHERTYPE_IP:
2549 error = bridge_ip_checkbasic(mp);
2550 break;
2551 #ifdef INET6
2552 case ETHERTYPE_IPV6:
2553 error = bridge_ip6_checkbasic(mp);
2554 break;
2555 #endif /* INET6 */
2556 default:
2557 error = 0;
2559 if (error)
2560 goto bad;
2563 error = 0;
2566 * Run the packet through pfil
2568 switch (ether_type)
2570 case ETHERTYPE_IP :
2572 * before calling the firewall, swap fields the same as
2573 * IP does. here we assume the header is contiguous
2575 ip = mtod(*mp, struct ip *);
2577 ip->ip_len = ntohs(ip->ip_len);
2578 ip->ip_off = ntohs(ip->ip_off);
2581 * Run pfil on the member interface and the bridge, both can
2582 * be skipped by clearing pfil_member or pfil_bridge.
2584 * Keep the order:
2585 * in_if -> bridge_if -> out_if
2587 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2588 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2589 dir);
2591 if (*mp == NULL || error != 0) /* filter may consume */
2592 break;
2594 if (pfil_member && ifp != NULL)
2595 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2596 dir);
2598 if (*mp == NULL || error != 0) /* filter may consume */
2599 break;
2601 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2602 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2603 dir);
2605 if (*mp == NULL || error != 0) /* filter may consume */
2606 break;
2608 /* check if we need to fragment the packet */
2609 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
2610 i = (*mp)->m_pkthdr.len;
2611 if (i > ifp->if_mtu) {
2612 error = bridge_fragment(ifp, *mp, &eh2, snap,
2613 &llc1);
2614 return (error);
2618 /* Recalculate the ip checksum and restore byte ordering */
2619 ip = mtod(*mp, struct ip *);
2620 hlen = ip->ip_hl << 2;
2621 if (hlen < sizeof(struct ip))
2622 goto bad;
2623 if (hlen > (*mp)->m_len) {
2624 if ((*mp = m_pullup(*mp, hlen)) == 0)
2625 goto bad;
2626 ip = mtod(*mp, struct ip *);
2627 if (ip == NULL)
2628 goto bad;
2630 ip->ip_len = htons(ip->ip_len);
2631 ip->ip_off = htons(ip->ip_off);
2632 ip->ip_sum = 0;
2633 if (hlen == sizeof(struct ip))
2634 ip->ip_sum = in_cksum_hdr(ip);
2635 else
2636 ip->ip_sum = in_cksum(*mp, hlen);
2638 break;
2639 #ifdef INET6
2640 case ETHERTYPE_IPV6 :
2641 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2642 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2643 dir);
2645 if (*mp == NULL || error != 0) /* filter may consume */
2646 break;
2648 if (pfil_member && ifp != NULL)
2649 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2650 dir);
2652 if (*mp == NULL || error != 0) /* filter may consume */
2653 break;
2655 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2656 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2657 dir);
2658 break;
2659 #endif
2660 default :
2661 error = 0;
2662 break;
2665 if (*mp == NULL)
2666 return (error);
2667 if (error != 0)
2668 goto bad;
2670 error = -1;
2673 * Finally, put everything back the way it was and return
2675 if (snap) {
2676 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
2677 if (*mp == NULL)
2678 return (error);
2679 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2682 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
2683 if (*mp == NULL)
2684 return (error);
2685 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2687 return (0);
2689 bad:
2690 m_freem(*mp);
2691 *mp = NULL;
2692 return (error);
2696 * Perform basic checks on header size since
2697 * pfil assumes ip_input has already processed
2698 * it for it. Cut-and-pasted from ip_input.c.
2699 * Given how simple the IPv6 version is,
2700 * does the IPv4 version really need to be
2701 * this complicated?
2703 * XXX Should we update ipstat here, or not?
2704 * XXX Right now we update ipstat but not
2705 * XXX csum_counter.
2707 static int
2708 bridge_ip_checkbasic(struct mbuf **mp)
2710 struct mbuf *m = *mp;
2711 struct ip *ip;
2712 int len, hlen;
2713 u_short sum;
2715 if (*mp == NULL)
2716 return (-1);
2717 #if notyet
2718 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2719 if ((m = m_copyup(m, sizeof(struct ip),
2720 (max_linkhdr + 3) & ~3)) == NULL) {
2721 /* XXXJRT new stat, please */
2722 ipstat.ips_toosmall++;
2723 goto bad;
2725 } else
2726 #endif
2727 #ifndef __predict_false
2728 #define __predict_false(x) x
2729 #endif
2730 if (__predict_false(m->m_len < sizeof (struct ip))) {
2731 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2732 ipstat.ips_toosmall++;
2733 goto bad;
2736 ip = mtod(m, struct ip *);
2737 if (ip == NULL) goto bad;
2739 if (ip->ip_v != IPVERSION) {
2740 ipstat.ips_badvers++;
2741 goto bad;
2743 hlen = ip->ip_hl << 2;
2744 if (hlen < sizeof(struct ip)) { /* minimum header length */
2745 ipstat.ips_badhlen++;
2746 goto bad;
2748 if (hlen > m->m_len) {
2749 if ((m = m_pullup(m, hlen)) == 0) {
2750 ipstat.ips_badhlen++;
2751 goto bad;
2753 ip = mtod(m, struct ip *);
2754 if (ip == NULL) goto bad;
2757 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2758 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2759 } else {
2760 if (hlen == sizeof(struct ip)) {
2761 sum = in_cksum_hdr(ip);
2762 } else {
2763 sum = in_cksum(m, hlen);
2766 if (sum) {
2767 ipstat.ips_badsum++;
2768 goto bad;
2771 /* Retrieve the packet length. */
2772 len = ntohs(ip->ip_len);
2775 * Check for additional length bogosity
2777 if (len < hlen) {
2778 ipstat.ips_badlen++;
2779 goto bad;
2783 * Check that the amount of data in the buffers
2784 * is as at least much as the IP header would have us expect.
2785 * Drop packet if shorter than we expect.
2787 if (m->m_pkthdr.len < len) {
2788 ipstat.ips_tooshort++;
2789 goto bad;
2792 /* Checks out, proceed */
2793 *mp = m;
2794 return (0);
2796 bad:
2797 *mp = m;
2798 return (-1);
2801 #ifdef INET6
2803 * Same as above, but for IPv6.
2804 * Cut-and-pasted from ip6_input.c.
2805 * XXX Should we update ip6stat, or not?
2807 static int
2808 bridge_ip6_checkbasic(struct mbuf **mp)
2810 struct mbuf *m = *mp;
2811 struct ip6_hdr *ip6;
2814 * If the IPv6 header is not aligned, slurp it up into a new
2815 * mbuf with space for link headers, in the event we forward
2816 * it. Otherwise, if it is aligned, make sure the entire base
2817 * IPv6 header is in the first mbuf of the chain.
2819 #if notyet
2820 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2821 struct ifnet *inifp = m->m_pkthdr.rcvif;
2822 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2823 (max_linkhdr + 3) & ~3)) == NULL) {
2824 /* XXXJRT new stat, please */
2825 ip6stat.ip6s_toosmall++;
2826 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2827 goto bad;
2829 } else
2830 #endif
2831 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2832 struct ifnet *inifp = m->m_pkthdr.rcvif;
2833 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2834 ip6stat.ip6s_toosmall++;
2835 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2836 goto bad;
2840 ip6 = mtod(m, struct ip6_hdr *);
2842 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2843 ip6stat.ip6s_badvers++;
2844 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2845 goto bad;
2848 /* Checks out, proceed */
2849 *mp = m;
2850 return (0);
2852 bad:
2853 *mp = m;
2854 return (-1);
2856 #endif /* INET6 */
2859 * bridge_fragment:
2861 * Return a fragmented mbuf chain.
2863 static int
2864 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
2865 int snap, struct llc *llc)
2867 struct mbuf *m0;
2868 struct ip *ip;
2869 int error = -1;
2871 if (m->m_len < sizeof(struct ip) &&
2872 (m = m_pullup(m, sizeof(struct ip))) == NULL)
2873 goto out;
2874 ip = mtod(m, struct ip *);
2876 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
2877 CSUM_DELAY_IP);
2878 if (error)
2879 goto out;
2881 /* walk the chain and re-add the Ethernet header */
2882 for (m0 = m; m0; m0 = m0->m_nextpkt) {
2883 if (error == 0) {
2884 if (snap) {
2885 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
2886 if (m0 == NULL) {
2887 error = ENOBUFS;
2888 continue;
2890 bcopy(llc, mtod(m0, caddr_t),
2891 sizeof(struct llc));
2893 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
2894 if (m0 == NULL) {
2895 error = ENOBUFS;
2896 continue;
2898 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
2899 } else
2900 m_freem(m);
2903 if (error == 0)
2904 ipstat.ips_fragmented++;
2906 return (error);
2908 out:
2909 if (m != NULL)
2910 m_freem(m);
2911 return (error);
2914 static void
2915 bridge_enqueue_handler(struct netmsg *nmsg)
2917 struct netmsg_packet *nmp;
2918 struct ifnet *dst_ifp;
2919 struct mbuf *m;
2921 nmp = (struct netmsg_packet *)nmsg;
2922 m = nmp->nm_packet;
2923 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2925 bridge_handoff_notags(dst_ifp, m);
2928 static void
2929 bridge_pfil_enqueue_handler(struct netmsg *nmsg)
2931 struct netmsg_packet *nmp;
2932 struct ifnet *dst_ifp;
2933 struct mbuf *m;
2935 nmp = (struct netmsg_packet *)nmsg;
2936 m = nmp->nm_packet;
2937 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2940 * Filter on the output interface. Pass a NULL bridge interface
2941 * pointer so we do not redundantly filter on the bridge for
2942 * each interface we broadcast on.
2944 if (inet_pfil_hook.ph_hashooks > 0
2945 #ifdef INET6
2946 || inet6_pfil_hook.ph_hashooks > 0
2947 #endif
2949 if (bridge_pfil(&m, NULL, dst_ifp, PFIL_OUT) != 0)
2950 return;
2951 if (m == NULL)
2952 return;
2954 bridge_handoff_notags(dst_ifp, m);
2957 static void
2958 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m)
2960 bridge_handoff_notags(dst_ifp, m);
2963 static void
2964 bridge_handoff_notags(struct ifnet *dst_ifp, struct mbuf *m)
2966 struct mbuf *m0;
2968 lwkt_serialize_enter(dst_ifp->if_serializer);
2970 /* We may be sending a fragment so traverse the mbuf */
2971 for (; m; m = m0) {
2972 struct altq_pktattr pktattr;
2974 m0 = m->m_nextpkt;
2975 m->m_nextpkt = NULL;
2977 if (ifq_is_enabled(&dst_ifp->if_snd))
2978 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
2980 ifq_handoff(dst_ifp, m, &pktattr);
2983 lwkt_serialize_exit(dst_ifp->if_serializer);
2986 struct netmsg_brgctl {
2987 struct netmsg bc_nmsg;
2988 bridge_ctl_t bc_func;
2989 struct bridge_softc *bc_sc;
2990 void *bc_arg;
2993 static void
2994 bridge_control_dispatch(struct netmsg *nmsg)
2996 struct netmsg_brgctl *bc_msg = (struct netmsg_brgctl *)nmsg;
2997 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
2998 int error;
3000 lwkt_serialize_enter(bifp->if_serializer);
3001 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
3002 lwkt_serialize_exit(bifp->if_serializer);
3004 lwkt_replymsg(&nmsg->nm_lmsg, error);
3007 static int
3008 bridge_control(struct bridge_softc *sc, u_long cmd,
3009 bridge_ctl_t bc_func, void *bc_arg)
3011 struct ifnet *bifp = sc->sc_ifp;
3012 struct netmsg_brgctl bc_msg;
3013 struct netmsg *nmsg;
3014 int error;
3016 ASSERT_SERIALIZED(bifp->if_serializer);
3018 if (cmd == SIOCGDRVSPEC) {
3020 * Don't dispatch 'get' ioctl to netisr0;
3021 * there are copyouts down deep inside
3022 * specific bridge ioctl functions.
3024 return bc_func(sc, bc_arg);
3027 bzero(&bc_msg, sizeof(bc_msg));
3028 nmsg = &bc_msg.bc_nmsg;
3030 netmsg_init(nmsg, &curthread->td_msgport, 0, bridge_control_dispatch);
3031 bc_msg.bc_func = bc_func;
3032 bc_msg.bc_sc = sc;
3033 bc_msg.bc_arg = bc_arg;
3035 lwkt_serialize_exit(bifp->if_serializer);
3036 error = lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
3037 lwkt_serialize_enter(bifp->if_serializer);
3038 return error;